2 * Copyright (c) 2014 VMware, Inc.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
29 #include "PacketParser.h"
31 #pragma warning( push )
32 #pragma warning( disable:4127 )
38 #define OVS_DBG_MOD OVS_DBG_VXLAN
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
50 /* Move to a header file */
51 extern POVS_SWITCH_CONTEXT gOvsSwitchContext;
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 *----------------------------------------------------------------------------
60 OvsIsTunnelFilterCreated(POVS_SWITCH_CONTEXT switchContext,
63 for (UINT hash = 0; hash < OVS_MAX_VPORT_ARRAY_SIZE; hash++) {
64 PLIST_ENTRY head, link, next;
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;
73 if ((udpPortDest == vxlanPort->dstPort)) {
74 /* The VXLAN tunnel was already created. */
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
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 *----------------------------------------------------------------------------
97 OvsInitVxlanTunnel(PIRP irp,
98 POVS_VPORT_ENTRY vport,
100 PFNTunnelVportPendingOp callback,
103 NTSTATUS status = STATUS_SUCCESS;
104 POVS_VXLAN_VPORT vxlanPort = NULL;
106 vxlanPort = OvsAllocateMemoryWithTag(sizeof (*vxlanPort),
108 if (vxlanPort == NULL) {
109 return STATUS_INSUFFICIENT_RESOURCES;
112 RtlZeroMemory(vxlanPort, sizeof(*vxlanPort));
113 vxlanPort->dstPort = udpDestPort;
114 vport->priv = (PVOID)vxlanPort;
116 if (!OvsIsTunnelFilterCreated(gOvsSwitchContext, udpDestPort)) {
117 status = OvsTunnelFilterCreate(irp,
119 &vxlanPort->filterID,
123 status = STATUS_OBJECT_NAME_EXISTS;
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 *----------------------------------------------------------------------------
138 OvsCleanupVxlanTunnel(PIRP irp,
139 POVS_VPORT_ENTRY vport,
140 PFNTunnelVportPendingOp callback,
143 NTSTATUS status = STATUS_SUCCESS;
144 POVS_VXLAN_VPORT vxlanPort = NULL;
146 if (vport->ovsType != OVS_VPORT_TYPE_VXLAN ||
147 vport->priv == NULL) {
148 return STATUS_SUCCESS;
151 vxlanPort = (POVS_VXLAN_VPORT)vport->priv;
153 if (vxlanPort->filterID != 0) {
154 status = OvsTunnelFilterDelete(irp,
159 OvsFreeMemoryWithTag(vport->priv, OVS_VXLAN_POOL_TAG);
168 *----------------------------------------------------------------------------
170 * Encapsulates the packet.
171 *----------------------------------------------------------------------------
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)
190 POVS_VXLAN_VPORT vportVxlan;
191 UINT32 headRoom = OvsGetVxlanTunHdrSize();
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
199 curNb = NET_BUFFER_LIST_FIRST_NB(curNbl);
200 packetLength = NET_BUFFER_DATA_LENGTH(curNb);
203 NDIS_TCP_LARGE_SEND_OFFLOAD_NET_BUFFER_LIST_INFO tsoInfo;
205 tsoInfo.Value = NET_BUFFER_LIST_INFO(curNbl,
206 TcpLargeSendNetBufferListInfo);
207 OVS_LOG_TRACE("MSS %u packet len %u", tsoInfo.LsoV1Transmit.MSS,
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;
217 /* Clear out LSO flags after this point */
218 NET_BUFFER_LIST_INFO(*newNbl, TcpLargeSendNetBufferListInfo) = 0;
222 vportVxlan = (POVS_VXLAN_VPORT) GetOvsVportPriv(vport);
225 /* If we didn't split the packet above, make a copy now */
226 if (*newNbl == NULL) {
227 *newNbl = OvsPartialCopyNBL(switchContext, curNbl, 0, headRoom,
229 if (*newNbl == NULL) {
230 OVS_LOG_ERROR("Unable to copy NBL");
231 return NDIS_STATUS_FAILURE;
234 * To this point we do not have VXLAN offloading.
235 * Apply defined checksums
237 curNb = NET_BUFFER_LIST_FIRST_NB(*newNbl);
238 curMdl = NET_BUFFER_CURRENT_MDL(curNb);
239 bufferStart = (PUINT8)MmGetSystemAddressForMdlSafe(curMdl, LowPagePriority);
241 status = NDIS_STATUS_RESOURCES;
245 NDIS_TCP_IP_CHECKSUM_NET_BUFFER_LIST_INFO csumInfo;
246 csumInfo.Value = NET_BUFFER_LIST_INFO(curNbl,
247 TcpIpChecksumNetBufferListInfo);
249 bufferStart += NET_BUFFER_CURRENT_MDL_OFFSET(curNb);
251 if (layers->isIPv4) {
252 IPHdr *ip = (IPHdr *)(bufferStart + layers->l3Offset);
254 if (csumInfo.Transmit.IpHeaderChecksum) {
256 ip->check = IPChecksum((UINT8 *)ip, 4 * ip->ihl, 0);
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));
274 } else if (layers->isIPv6) {
275 IPv6Hdr *ip = (IPv6Hdr *)(bufferStart + layers->l3Offset);
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));
295 /* Clear out TcpIpChecksumNetBufferListInfo flag */
296 NET_BUFFER_LIST_INFO(*newNbl, TcpIpChecksumNetBufferListInfo) = 0;
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) {
307 curMdl = NET_BUFFER_CURRENT_MDL(curNb);
308 bufferStart = (PUINT8)MmGetSystemAddressForMdlSafe(curMdl, LowPagePriority);
310 status = NDIS_STATUS_RESOURCES;
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));
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);
329 ipHdr = (IPHdr *)((PCHAR)ethHdr + sizeof *ethHdr);
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) ?
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;
347 ipHdr->check = IPChecksum((UINT8 *)ipHdr, sizeof *ipHdr, 0);
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);
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;
367 vxlanHdr->reserved2 = 0;
369 return STATUS_SUCCESS;
372 OvsCompleteNBL(switchContext, *newNbl, TRUE);
379 *----------------------------------------------------------------------------
381 * Encapsulates the packet if L2/L3 for destination resolves. Otherwise,
382 * enqueues a callback that does encapsulatation after resolution.
383 *----------------------------------------------------------------------------
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)
394 OVS_FWD_INFO fwdInfo;
396 status = OvsLookupIPFwdInfo(tunKey->dst, &fwdInfo);
397 if (status != STATUS_SUCCESS) {
398 OvsFwdIPHelperRequest(NULL, 0, tunKey, NULL, NULL, NULL);
399 // return NDIS_STATUS_PENDING;
401 * XXX: Don't know if the completionList will make any sense when
402 * accessed in the callback. Make sure the caveats are known.
404 * XXX: This code will work once we are able to grab locks in the
407 return NDIS_STATUS_FAILURE;
410 return OvsDoEncapVxlan(vport, curNbl, tunKey, &fwdInfo, layers,
411 switchContext, newNbl);
415 *----------------------------------------------------------------------------
416 * OvsCalculateUDPChecksum
417 * Calculate UDP checksum
418 *----------------------------------------------------------------------------
420 static __inline NDIS_STATUS
421 OvsCalculateUDPChecksum(PNET_BUFFER_LIST curNbl,
427 NDIS_TCP_IP_CHECKSUM_NET_BUFFER_LIST_INFO csumInfo;
430 csumInfo.Value = NET_BUFFER_LIST_INFO(curNbl, TcpIpChecksumNetBufferListInfo);
432 /* Next check if UDP checksum has been calculated. */
433 if (!csumInfo.Receive.UdpChecksumSucceeded) {
436 checkSum = udpHdr->check;
438 l4Payload = packetLength - sizeof(EthHdr) - ipHdr->ihl * 4;
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;
452 csumInfo.Receive.UdpChecksumSucceeded = 1;
453 NET_BUFFER_LIST_INFO(curNbl, TcpIpChecksumNetBufferListInfo) = csumInfo.Value;
454 return NDIS_STATUS_SUCCESS;
458 *----------------------------------------------------------------------------
460 * Decapsulates to tunnel header in 'curNbl' and puts into 'tunKey'.
461 *----------------------------------------------------------------------------
464 OvsDecapVxlan(POVS_SWITCH_CONTEXT switchContext,
465 PNET_BUFFER_LIST curNbl,
466 OvsIPv4TunnelKey *tunKey,
467 PNET_BUFFER_LIST *newNbl)
475 UINT32 tunnelSize = 0, packetLength = 0;
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;
488 * Create a copy of the NBL so that we have all the headers in one MDL.
490 *newNbl = OvsPartialCopyNBL(switchContext, curNbl,
491 tunnelSize + OVS_DEFAULT_COPY_SIZE, 0,
492 TRUE /*copy NBL info */);
494 if (*newNbl == NULL) {
495 return NDIS_STATUS_RESOURCES;
498 /* XXX: Handle VLAN header. */
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);
505 status = NDIS_STATUS_RESOURCES;
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;
517 udpHdr = (UDPHdr *)((PCHAR)ipHdr + sizeof *ipHdr);
519 /* Validate if NIC has indicated checksum failure. */
520 status = OvsValidateUDPChecksum(curNbl, udpHdr->check == 0);
521 if (status != NDIS_STATUS_SUCCESS) {
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) {
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);
539 tunKey->tunnelId = 0;
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;
548 OvsCompleteNBL(switchContext, *newNbl, TRUE);
555 OvsSlowPathDecapVxlan(const PNET_BUFFER_LIST packet,
556 OvsIPv4TunnelKey *tunnelKey)
558 NDIS_STATUS status = NDIS_STATUS_FAILURE;
561 VXLANHdr *VxlanHeader;
562 VXLANHdr VxlanHeaderBuffer;
563 struct IPHdr ip_storage;
564 const struct IPHdr *nh;
565 OVS_PACKET_HDR_INFO layers;
570 nh = OvsGetIp(packet, layers.l3Offset, &ip_storage);
572 layers.l4Offset = layers.l3Offset + nh->ihl * 4;
577 /* make sure it's a VXLAN packet */
578 udp = OvsGetUdp(packet, layers.l4Offset, &udpStorage);
580 layers.l7Offset = layers.l4Offset + sizeof *udp;
585 VxlanHeader = (VXLANHdr *)OvsGetPacketBytes(packet,
586 sizeof(*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);
599 tunnelKey->flags = 0;
600 tunnelKey->tunnelId = 0;
605 status = NDIS_STATUS_SUCCESS;
612 #pragma warning( pop )