c0611dad532c266c7546d0a409572dca4584d8dd
[cascardo/ovs.git] / datapath-windows / ovsext / Vxlan.c
1 /*
2  * Copyright (c) 2014 VMware, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "precomp.h"
18 #include "Atomic.h"
19 #include "NetProto.h"
20 #include "Switch.h"
21 #include "Vport.h"
22 #include "Flow.h"
23 #include "Vxlan.h"
24 #include "IpHelper.h"
25 #include "Checksum.h"
26 #include "User.h"
27 #include "PacketIO.h"
28 #include "Flow.h"
29 #include "PacketParser.h"
30
31 #pragma warning( push )
32 #pragma warning( disable:4127 )
33
34
35 #ifdef OVS_DBG_MOD
36 #undef OVS_DBG_MOD
37 #endif
38 #define OVS_DBG_MOD OVS_DBG_VXLAN
39 #include "Debug.h"
40
41 /* Helper macro to check if a VXLAN ID is valid. */
42 #define VXLAN_ID_IS_VALID(vxlanID) (0 < (vxlanID) && (vxlanID) <= 0xffffff)
43 #define VXLAN_TUNNELID_TO_VNI(_tID)   (UINT32)(((UINT64)(_tID)) >> 40)
44 #define VXLAN_VNI_TO_TUNNELID(_vni) (((UINT64)(_vni)) << 40)
45 #define IP_DF_NBO 0x0040
46 #define VXLAN_DEFAULT_TTL 64
47 #define VXLAN_MULTICAST_TTL 64
48 #define VXLAN_DEFAULT_INSTANCE_ID 1
49
50 /* Move to a header file */
51 extern POVS_SWITCH_CONTEXT gOvsSwitchContext;
52
53 /*
54  *----------------------------------------------------------------------------
55  * This function verifies if the VXLAN tunnel already exists, in order to
56  * avoid sending a duplicate request to the WFP base filtering engine.
57  *----------------------------------------------------------------------------
58  */
59 static BOOLEAN
60 OvsIsTunnelFilterCreated(POVS_SWITCH_CONTEXT switchContext,
61                          UINT16 udpPortDest)
62 {
63     for (UINT hash = 0; hash < OVS_MAX_VPORT_ARRAY_SIZE; hash++) {
64         PLIST_ENTRY head, link, next;
65
66         head = &(switchContext->portNoHashArray[hash & OVS_VPORT_MASK]);
67         LIST_FORALL_SAFE(head, link, next) {
68             POVS_VPORT_ENTRY vport = NULL;
69             POVS_VXLAN_VPORT vxlanPort = NULL;
70             vport = CONTAINING_RECORD(link, OVS_VPORT_ENTRY, portNoLink);
71             vxlanPort = (POVS_VXLAN_VPORT)vport->priv;
72             if (vxlanPort) {
73                 if ((udpPortDest == vxlanPort->dstPort)) {
74                     /* The VXLAN tunnel was already created. */
75                     return TRUE;
76                 }
77             }
78         }
79     }
80
81     return FALSE;
82 }
83
84 /*
85  *----------------------------------------------------------------------------
86  * This function allocates and initializes the OVS_VXLAN_VPORT. The function
87  * also creates a WFP tunnel filter for the necessary destination port. The
88  * tunnel filter create request is passed to the tunnel filter threads that
89  * will complete the request at a later time when IRQL is lowered to
90  * PASSIVE_LEVEL.
91  *
92  * udpDestPort: the vxlan is set as payload to a udp frame. If the destination
93  * port of an udp frame is udpDestPort, we understand it to be vxlan.
94  *----------------------------------------------------------------------------
95  */
96 NTSTATUS
97 OvsInitVxlanTunnel(PIRP irp,
98                    POVS_VPORT_ENTRY vport,
99                    UINT16 udpDestPort,
100                    PFNTunnelVportPendingOp callback,
101                    PVOID tunnelContext)
102 {
103     NTSTATUS status = STATUS_SUCCESS;
104     POVS_VXLAN_VPORT vxlanPort = NULL;
105
106     vxlanPort = OvsAllocateMemoryWithTag(sizeof (*vxlanPort),
107                                          OVS_VXLAN_POOL_TAG);
108     if (vxlanPort == NULL) {
109         return STATUS_INSUFFICIENT_RESOURCES;
110     }
111
112     RtlZeroMemory(vxlanPort, sizeof(*vxlanPort));
113     vxlanPort->dstPort = udpDestPort;
114     vport->priv = (PVOID)vxlanPort;
115
116     if (!OvsIsTunnelFilterCreated(gOvsSwitchContext, udpDestPort)) {
117         status = OvsTunnelFilterCreate(irp,
118                                        udpDestPort,
119                                        &vxlanPort->filterID,
120                                        callback,
121                                        tunnelContext);
122     } else {
123         status = STATUS_OBJECT_NAME_EXISTS;
124     }
125
126     return status;
127 }
128
129 /*
130  *----------------------------------------------------------------------------
131  * This function releases the OVS_VXLAN_VPORT. The function also deletes the
132  * WFP tunnel filter previously created. The tunnel filter delete request is
133  * passed to the tunnel filter threads that will complete the request at a
134  * later time when IRQL is lowered to PASSIVE_LEVEL.
135  *----------------------------------------------------------------------------
136  */
137 NTSTATUS
138 OvsCleanupVxlanTunnel(PIRP irp,
139                       POVS_VPORT_ENTRY vport,
140                       PFNTunnelVportPendingOp callback,
141                       PVOID tunnelContext)
142 {
143     NTSTATUS status = STATUS_SUCCESS;
144     POVS_VXLAN_VPORT vxlanPort = NULL;
145
146     if (vport->ovsType != OVS_VPORT_TYPE_VXLAN ||
147         vport->priv == NULL) {
148         return STATUS_SUCCESS;
149     }
150
151     vxlanPort = (POVS_VXLAN_VPORT)vport->priv;
152
153     if (vxlanPort->filterID != 0) {
154         status = OvsTunnelFilterDelete(irp,
155                                        vxlanPort->filterID,
156                                        callback,
157                                        tunnelContext);
158     } else {
159         OvsFreeMemoryWithTag(vport->priv, OVS_VXLAN_POOL_TAG);
160         vport->priv = NULL;
161     }
162
163     return status;
164 }
165
166
167 /*
168  *----------------------------------------------------------------------------
169  * OvsDoEncapVxlan
170  *     Encapsulates the packet.
171  *----------------------------------------------------------------------------
172  */
173 static __inline NDIS_STATUS
174 OvsDoEncapVxlan(POVS_VPORT_ENTRY vport,
175                 PNET_BUFFER_LIST curNbl,
176                 OvsIPv4TunnelKey *tunKey,
177                 POVS_FWD_INFO fwdInfo,
178                 POVS_PACKET_HDR_INFO layers,
179                 POVS_SWITCH_CONTEXT switchContext,
180                 PNET_BUFFER_LIST *newNbl)
181 {
182     NDIS_STATUS status;
183     PNET_BUFFER curNb;
184     PMDL curMdl;
185     PUINT8 bufferStart;
186     EthHdr *ethHdr;
187     IPHdr *ipHdr;
188     UDPHdr *udpHdr;
189     VXLANHdr *vxlanHdr;
190     POVS_VXLAN_VPORT vportVxlan;
191     UINT32 headRoom = OvsGetVxlanTunHdrSize();
192     UINT32 packetLength;
193
194     /*
195      * XXX: the assumption currently is that the NBL is owned by OVS, and
196      * headroom has already been allocated as part of allocating the NBL and
197      * MDL.
198      */
199     curNb = NET_BUFFER_LIST_FIRST_NB(curNbl);
200     packetLength = NET_BUFFER_DATA_LENGTH(curNb);
201
202     if (layers->isTcp) {
203         NDIS_TCP_LARGE_SEND_OFFLOAD_NET_BUFFER_LIST_INFO tsoInfo;
204
205         tsoInfo.Value = NET_BUFFER_LIST_INFO(curNbl,
206                                              TcpLargeSendNetBufferListInfo);
207         OVS_LOG_TRACE("MSS %u packet len %u", tsoInfo.LsoV1Transmit.MSS,
208                       packetLength);
209         if (tsoInfo.LsoV1Transmit.MSS) {
210             OVS_LOG_TRACE("l4Offset %d", layers->l4Offset);
211             *newNbl = OvsTcpSegmentNBL(switchContext, curNbl, layers,
212                                        tsoInfo.LsoV1Transmit.MSS, headRoom);
213             if (*newNbl == NULL) {
214                 OVS_LOG_ERROR("Unable to segment NBL");
215                 return NDIS_STATUS_FAILURE;
216             }
217             /* Clear out LSO flags after this point */
218             NET_BUFFER_LIST_INFO(*newNbl, TcpLargeSendNetBufferListInfo) = 0;
219         }
220     }
221
222     vportVxlan = (POVS_VXLAN_VPORT) GetOvsVportPriv(vport);
223     ASSERT(vportVxlan);
224
225     /* If we didn't split the packet above, make a copy now */
226     if (*newNbl == NULL) {
227         *newNbl = OvsPartialCopyNBL(switchContext, curNbl, 0, headRoom,
228                                     FALSE /*NBL info*/);
229         if (*newNbl == NULL) {
230             OVS_LOG_ERROR("Unable to copy NBL");
231             return NDIS_STATUS_FAILURE;
232         }
233         /*
234          * To this point we do not have VXLAN offloading.
235          * Apply defined checksums
236          */
237         curNb = NET_BUFFER_LIST_FIRST_NB(*newNbl);
238         curMdl = NET_BUFFER_CURRENT_MDL(curNb);
239         bufferStart = (PUINT8)MmGetSystemAddressForMdlSafe(curMdl, LowPagePriority);
240         if (!bufferStart) {
241             status = NDIS_STATUS_RESOURCES;
242             goto ret_error;
243         }
244
245         NDIS_TCP_IP_CHECKSUM_NET_BUFFER_LIST_INFO csumInfo;
246         csumInfo.Value = NET_BUFFER_LIST_INFO(curNbl,
247                                               TcpIpChecksumNetBufferListInfo);
248
249         bufferStart += NET_BUFFER_CURRENT_MDL_OFFSET(curNb);
250
251         if (layers->isIPv4) {
252             IPHdr *ip = (IPHdr *)(bufferStart + layers->l3Offset);
253
254             if (csumInfo.Transmit.IpHeaderChecksum) {
255                 ip->check = 0;
256                 ip->check = IPChecksum((UINT8 *)ip, 4 * ip->ihl, 0);
257             }
258
259             if (layers->isTcp && csumInfo.Transmit.TcpChecksum) {
260                 UINT16 csumLength = (UINT16)(packetLength - layers->l4Offset);
261                 TCPHdr *tcp = (TCPHdr *)(bufferStart + layers->l4Offset);
262                 tcp->check = IPPseudoChecksum(&ip->saddr, &ip->daddr,
263                                               IPPROTO_TCP, csumLength);
264                 tcp->check = CalculateChecksumNB(curNb, csumLength,
265                                                  (UINT32)(layers->l4Offset));
266             } else if (layers->isUdp && csumInfo.Transmit.UdpChecksum) {
267                 UINT16 csumLength = (UINT16)(packetLength - layers->l4Offset);
268                 UDPHdr *udp = (UDPHdr *)((PCHAR)ip + sizeof *ip);
269                 udp->check = IPPseudoChecksum(&ip->saddr, &ip->daddr,
270                                               IPPROTO_UDP, csumLength);
271                 udp->check = CalculateChecksumNB(curNb, csumLength,
272                                                  (UINT32)(layers->l4Offset));
273             }
274         } else if (layers->isIPv6) {
275             IPv6Hdr *ip = (IPv6Hdr *)(bufferStart + layers->l3Offset);
276
277             if (layers->isTcp && csumInfo.Transmit.TcpChecksum) {
278                 UINT16 csumLength = (UINT16)(packetLength - layers->l4Offset);
279                 TCPHdr *tcp = (TCPHdr *)(bufferStart + layers->l4Offset);
280                 tcp->check = IPv6PseudoChecksum((UINT32 *) &ip->saddr,
281                                                 (UINT32 *) &ip->daddr,
282                                                 IPPROTO_TCP, csumLength);
283                 tcp->check = CalculateChecksumNB(curNb, csumLength,
284                                                  (UINT32)(layers->l4Offset));
285             } else if (layers->isUdp && csumInfo.Transmit.UdpChecksum) {
286                 UINT16 csumLength = (UINT16)(packetLength - layers->l4Offset);
287                 UDPHdr *udp = (UDPHdr *)((PCHAR)ip + sizeof *ip);
288                 udp->check = IPv6PseudoChecksum((UINT32 *) &ip->saddr,
289                                                 (UINT32 *) &ip->daddr,
290                                                 IPPROTO_UDP, csumLength);
291                 udp->check = CalculateChecksumNB(curNb, csumLength,
292                                                  (UINT32)(layers->l4Offset));
293             }
294         }
295         /* Clear out TcpIpChecksumNetBufferListInfo flag */
296         NET_BUFFER_LIST_INFO(*newNbl, TcpIpChecksumNetBufferListInfo) = 0;
297     }
298
299     curNbl = *newNbl;
300     for (curNb = NET_BUFFER_LIST_FIRST_NB(curNbl); curNb != NULL;
301             curNb = curNb->Next) {
302         status = NdisRetreatNetBufferDataStart(curNb, headRoom, 0, NULL);
303         if (status != NDIS_STATUS_SUCCESS) {
304             goto ret_error;
305         }
306
307         curMdl = NET_BUFFER_CURRENT_MDL(curNb);
308         bufferStart = (PUINT8)MmGetSystemAddressForMdlSafe(curMdl, LowPagePriority);
309         if (!bufferStart) {
310             status = NDIS_STATUS_RESOURCES;
311             goto ret_error;
312         }
313
314         bufferStart += NET_BUFFER_CURRENT_MDL_OFFSET(curNb);
315         if (NET_BUFFER_NEXT_NB(curNb)) {
316             OVS_LOG_TRACE("nb length %u next %u", NET_BUFFER_DATA_LENGTH(curNb),
317                           NET_BUFFER_DATA_LENGTH(curNb->Next));
318         }
319
320         /* L2 header */
321         ethHdr = (EthHdr *)bufferStart;
322         ASSERT(((PCHAR)&fwdInfo->dstMacAddr + sizeof fwdInfo->dstMacAddr) ==
323                (PCHAR)&fwdInfo->srcMacAddr);
324         NdisMoveMemory(ethHdr->Destination, fwdInfo->dstMacAddr,
325                        sizeof ethHdr->Destination + sizeof ethHdr->Source);
326         ethHdr->Type = htons(ETH_TYPE_IPV4);
327
328         /* IP header */
329         ipHdr = (IPHdr *)((PCHAR)ethHdr + sizeof *ethHdr);
330
331         ipHdr->ihl = sizeof *ipHdr / 4;
332         ipHdr->version = IPPROTO_IPV4;
333         ipHdr->tos = tunKey->tos;
334         ipHdr->tot_len = htons(NET_BUFFER_DATA_LENGTH(curNb) - sizeof *ethHdr);
335         ipHdr->id = (uint16)atomic_add64(&vportVxlan->ipId,
336                                          NET_BUFFER_DATA_LENGTH(curNb));
337         ipHdr->frag_off = (tunKey->flags & OVS_TNL_F_DONT_FRAGMENT) ?
338                           IP_DF_NBO : 0;
339         ipHdr->ttl = tunKey->ttl ? tunKey->ttl : VXLAN_DEFAULT_TTL;
340         ipHdr->protocol = IPPROTO_UDP;
341         ASSERT(tunKey->dst == fwdInfo->dstIpAddr);
342         ASSERT(tunKey->src == fwdInfo->srcIpAddr || tunKey->src == 0);
343         ipHdr->saddr = fwdInfo->srcIpAddr;
344         ipHdr->daddr = fwdInfo->dstIpAddr;
345
346         ipHdr->check = 0;
347         ipHdr->check = IPChecksum((UINT8 *)ipHdr, sizeof *ipHdr, 0);
348
349         /* UDP header */
350         udpHdr = (UDPHdr *)((PCHAR)ipHdr + sizeof *ipHdr);
351         udpHdr->source = htons(tunKey->flow_hash | MAXINT16);
352         udpHdr->dest = htons(vportVxlan->dstPort);
353         udpHdr->len = htons(NET_BUFFER_DATA_LENGTH(curNb) - headRoom +
354                             sizeof *udpHdr + sizeof *vxlanHdr);
355         udpHdr->check = 0;
356
357         /* VXLAN header */
358         vxlanHdr = (VXLANHdr *)((PCHAR)udpHdr + sizeof *udpHdr);
359         vxlanHdr->flags1 = 0;
360         vxlanHdr->locallyReplicate = 0;
361         vxlanHdr->flags2 = 0;
362         vxlanHdr->reserved1 = 0;
363         if (tunKey->flags | OVS_TNL_F_KEY) {
364             vxlanHdr->vxlanID = VXLAN_TUNNELID_TO_VNI(tunKey->tunnelId);
365             vxlanHdr->instanceID = 1;
366         }
367         vxlanHdr->reserved2 = 0;
368     }
369     return STATUS_SUCCESS;
370
371 ret_error:
372     OvsCompleteNBL(switchContext, *newNbl, TRUE);
373     *newNbl = NULL;
374     return status;
375 }
376
377
378 /*
379  *----------------------------------------------------------------------------
380  * OvsEncapVxlan --
381  *     Encapsulates the packet if L2/L3 for destination resolves. Otherwise,
382  *     enqueues a callback that does encapsulatation after resolution.
383  *----------------------------------------------------------------------------
384  */
385 NDIS_STATUS
386 OvsEncapVxlan(POVS_VPORT_ENTRY vport,
387               PNET_BUFFER_LIST curNbl,
388               OvsIPv4TunnelKey *tunKey,
389               POVS_SWITCH_CONTEXT switchContext,
390               POVS_PACKET_HDR_INFO layers,
391               PNET_BUFFER_LIST *newNbl)
392 {
393     NTSTATUS status;
394     OVS_FWD_INFO fwdInfo;
395
396     status = OvsLookupIPFwdInfo(tunKey->dst, &fwdInfo);
397     if (status != STATUS_SUCCESS) {
398         OvsFwdIPHelperRequest(NULL, 0, tunKey, NULL, NULL, NULL);
399         // return NDIS_STATUS_PENDING;
400         /*
401          * XXX: Don't know if the completionList will make any sense when
402          * accessed in the callback. Make sure the caveats are known.
403          *
404          * XXX: This code will work once we are able to grab locks in the
405          * callback.
406          */
407         return NDIS_STATUS_FAILURE;
408     }
409
410     return OvsDoEncapVxlan(vport, curNbl, tunKey, &fwdInfo, layers,
411                            switchContext, newNbl);
412 }
413
414 /*
415  *----------------------------------------------------------------------------
416  * OvsCalculateUDPChecksum
417  *     Calculate UDP checksum
418  *----------------------------------------------------------------------------
419  */
420 static __inline NDIS_STATUS
421 OvsCalculateUDPChecksum(PNET_BUFFER_LIST curNbl,
422                         PNET_BUFFER curNb,
423                         IPHdr *ipHdr,
424                         UDPHdr *udpHdr,
425                         UINT32 packetLength)
426 {
427     NDIS_TCP_IP_CHECKSUM_NET_BUFFER_LIST_INFO csumInfo;
428     UINT16 checkSum;
429
430     csumInfo.Value = NET_BUFFER_LIST_INFO(curNbl, TcpIpChecksumNetBufferListInfo);
431
432     /* Next check if UDP checksum has been calculated. */
433     if (!csumInfo.Receive.UdpChecksumSucceeded) {
434         UINT32 l4Payload;
435
436         checkSum = udpHdr->check;
437
438         l4Payload = packetLength - sizeof(EthHdr) - ipHdr->ihl * 4;
439         udpHdr->check = 0;
440         udpHdr->check =
441             IPPseudoChecksum((UINT32 *)&ipHdr->saddr,
442                              (UINT32 *)&ipHdr->daddr,
443                              IPPROTO_UDP, (UINT16)l4Payload);
444         udpHdr->check = CalculateChecksumNB(curNb, (UINT16)l4Payload,
445             sizeof(EthHdr) + ipHdr->ihl * 4);
446         if (checkSum != udpHdr->check) {
447             OVS_LOG_TRACE("UDP checksum incorrect.");
448             return NDIS_STATUS_INVALID_PACKET;
449         }
450     }
451
452     csumInfo.Receive.UdpChecksumSucceeded = 1;
453     NET_BUFFER_LIST_INFO(curNbl, TcpIpChecksumNetBufferListInfo) = csumInfo.Value;
454     return NDIS_STATUS_SUCCESS;
455 }
456
457 /*
458  *----------------------------------------------------------------------------
459  * OvsDecapVxlan
460  *     Decapsulates to tunnel header in 'curNbl' and puts into 'tunKey'.
461  *----------------------------------------------------------------------------
462  */
463 NDIS_STATUS
464 OvsDecapVxlan(POVS_SWITCH_CONTEXT switchContext,
465               PNET_BUFFER_LIST curNbl,
466               OvsIPv4TunnelKey *tunKey,
467               PNET_BUFFER_LIST *newNbl)
468 {
469     PNET_BUFFER curNb;
470     PMDL curMdl;
471     EthHdr *ethHdr;
472     IPHdr *ipHdr;
473     UDPHdr *udpHdr;
474     VXLANHdr *vxlanHdr;
475     UINT32 tunnelSize = 0, packetLength = 0;
476     PUINT8 bufferStart;
477     NDIS_STATUS status;
478
479     /* Check the length of the UDP payload */
480     curNb = NET_BUFFER_LIST_FIRST_NB(curNbl);
481     packetLength = NET_BUFFER_DATA_LENGTH(curNb);
482     tunnelSize = OvsGetVxlanTunHdrSize();
483     if (packetLength <= tunnelSize) {
484         return NDIS_STATUS_INVALID_LENGTH;
485     }
486
487     /*
488      * Create a copy of the NBL so that we have all the headers in one MDL.
489      */
490     *newNbl = OvsPartialCopyNBL(switchContext, curNbl,
491                                 tunnelSize + OVS_DEFAULT_COPY_SIZE, 0,
492                                 TRUE /*copy NBL info */);
493
494     if (*newNbl == NULL) {
495         return NDIS_STATUS_RESOURCES;
496     }
497
498     /* XXX: Handle VLAN header. */
499     curNbl = *newNbl;
500     curNb = NET_BUFFER_LIST_FIRST_NB(curNbl);
501     curMdl = NET_BUFFER_CURRENT_MDL(curNb);
502     bufferStart = (PUINT8)MmGetSystemAddressForMdlSafe(curMdl, LowPagePriority) +
503                   NET_BUFFER_CURRENT_MDL_OFFSET(curNb);
504     if (!bufferStart) {
505         status = NDIS_STATUS_RESOURCES;
506         goto dropNbl;
507     }
508
509     ethHdr = (EthHdr *)bufferStart;
510     /* XXX: Handle IP options. */
511     ipHdr = (IPHdr *)((PCHAR)ethHdr + sizeof *ethHdr);
512     tunKey->src = ipHdr->saddr;
513     tunKey->dst = ipHdr->daddr;
514     tunKey->tos = ipHdr->tos;
515     tunKey->ttl = ipHdr->ttl;
516     tunKey->pad = 0;
517     udpHdr = (UDPHdr *)((PCHAR)ipHdr + sizeof *ipHdr);
518
519     /* Validate if NIC has indicated checksum failure. */
520     status = OvsValidateUDPChecksum(curNbl, udpHdr->check == 0);
521     if (status != NDIS_STATUS_SUCCESS) {
522         goto dropNbl;
523     }
524
525     /* Calculate and verify UDP checksum if NIC didn't do it. */
526     if (udpHdr->check != 0) {
527         status = OvsCalculateUDPChecksum(curNbl, curNb, ipHdr, udpHdr, packetLength);
528         if (status != NDIS_STATUS_SUCCESS) {
529             goto dropNbl;
530         }
531     }
532
533     vxlanHdr = (VXLANHdr *)((PCHAR)udpHdr + sizeof *udpHdr);
534     if (vxlanHdr->instanceID) {
535         tunKey->flags = OVS_TNL_F_KEY;
536         tunKey->tunnelId = VXLAN_VNI_TO_TUNNELID(vxlanHdr->vxlanID);
537     } else {
538         tunKey->flags = 0;
539         tunKey->tunnelId = 0;
540     }
541
542     /* Clear out the receive flag for the inner packet. */
543     NET_BUFFER_LIST_INFO(curNbl, TcpIpChecksumNetBufferListInfo) = 0;
544     NdisAdvanceNetBufferDataStart(curNb, tunnelSize, FALSE, NULL);
545     return NDIS_STATUS_SUCCESS;
546
547 dropNbl:
548     OvsCompleteNBL(switchContext, *newNbl, TRUE);
549     *newNbl = NULL;
550     return status;
551 }
552
553
554 NDIS_STATUS
555 OvsSlowPathDecapVxlan(const PNET_BUFFER_LIST packet,
556                    OvsIPv4TunnelKey *tunnelKey)
557 {
558     NDIS_STATUS status = NDIS_STATUS_FAILURE;
559     UDPHdr udpStorage;
560     const UDPHdr *udp;
561     VXLANHdr *VxlanHeader;
562     VXLANHdr  VxlanHeaderBuffer;
563     struct IPHdr ip_storage;
564     const struct IPHdr *nh;
565     OVS_PACKET_HDR_INFO layers;
566
567     layers.value = 0;
568
569     do {
570         nh = OvsGetIp(packet, layers.l3Offset, &ip_storage);
571         if (nh) {
572             layers.l4Offset = layers.l3Offset + nh->ihl * 4;
573         } else {
574             break;
575         }
576
577         /* make sure it's a VXLAN packet */
578         udp = OvsGetUdp(packet, layers.l4Offset, &udpStorage);
579         if (udp) {
580             layers.l7Offset = layers.l4Offset + sizeof *udp;
581         } else {
582             break;
583         }
584
585         VxlanHeader = (VXLANHdr *)OvsGetPacketBytes(packet,
586                                                     sizeof(*VxlanHeader),
587                                                     layers.l7Offset,
588                                                     &VxlanHeaderBuffer);
589
590         if (VxlanHeader) {
591             tunnelKey->src = nh->saddr;
592             tunnelKey->dst = nh->daddr;
593             tunnelKey->ttl = nh->ttl;
594             tunnelKey->tos = nh->tos;
595             if (VxlanHeader->instanceID) {
596                 tunnelKey->flags = OVS_TNL_F_KEY;
597                 tunnelKey->tunnelId = VXLAN_VNI_TO_TUNNELID(VxlanHeader->vxlanID);
598             } else {
599                 tunnelKey->flags = 0;
600                 tunnelKey->tunnelId = 0;
601             }
602         } else {
603             break;
604         }
605         status = NDIS_STATUS_SUCCESS;
606
607     } while(FALSE);
608
609     return status;
610 }
611
612 #pragma warning( pop )