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.
32 #define OVS_DBG_MOD OVS_DBG_ACTION
35 typedef struct _OVS_ACTION_STATS {
41 UINT32 failedFlowMiss;
43 UINT32 failedFlowExtract;
48 UINT32 cannotGrowDest;
50 UINT32 failedChecksum;
51 } OVS_ACTION_STATS, *POVS_ACTION_STATS;
53 OVS_ACTION_STATS ovsActionStats;
56 * There a lot of data that needs to be maintained while executing the pipeline
57 * as dictated by the actions of a flow, across different functions at different
58 * levels. Such data is put together in a 'context' structure. Care should be
59 * exercised while adding new members to the structure - only add ones that get
60 * used across multiple stages in the pipeline/get used in multiple functions.
62 #define OVS_DEST_PORTS_ARRAY_MIN_SIZE 2
63 typedef struct OvsForwardingContext {
64 POVS_SWITCH_CONTEXT switchContext;
65 /* The NBL currently used in the pipeline. */
66 PNET_BUFFER_LIST curNbl;
67 /* NDIS forwarding detail for 'curNbl'. */
68 PNDIS_SWITCH_FORWARDING_DETAIL_NET_BUFFER_LIST_INFO fwdDetail;
69 /* Array of destination ports for 'curNbl'. */
70 PNDIS_SWITCH_FORWARDING_DESTINATION_ARRAY destinationPorts;
71 /* send flags while sending 'curNbl' into NDIS. */
73 /* Total number of output ports, used + unused, in 'curNbl'. */
74 UINT32 destPortsSizeIn;
75 /* Total number of used output ports in 'curNbl'. */
76 UINT32 destPortsSizeOut;
78 * If 'curNbl' is not owned by OVS, they need to be tracked, if they need to
81 OvsCompletionList *completionList;
83 * vport number of 'curNbl' when it is passed from the PIF bridge to the INT
84 * bridge. ie. during tunneling on the Rx side.
90 * - specified in actions during tunneling Tx
91 * - extracted from an NBL during tunneling Rx
93 OvsIPv4TunnelKey tunKey;
97 * To store the output port, when it is a tunneled port. We don't foresee
98 * multiple tunneled ports as outport for any given NBL.
100 POVS_VPORT_ENTRY tunnelTxNic;
104 * Points to the Internal port on the PIF Bridge, if the packet needs to be
107 POVS_VPORT_ENTRY tunnelRxNic;
109 /* header information */
110 OVS_PACKET_HDR_INFO layers;
111 } OvsForwardingContext;
115 * --------------------------------------------------------------------------
116 * OvsInitForwardingCtx --
117 * Function to init/re-init the 'ovsFwdCtx' context as the actions pipeline
121 * NDIS_STATUS_SUCCESS on success
122 * Other NDIS_STATUS upon failure. Upon failure, it is safe to call
123 * OvsCompleteNBLForwardingCtx(), since 'ovsFwdCtx' has been initialized
124 * enough for OvsCompleteNBLForwardingCtx() to do its work.
125 * --------------------------------------------------------------------------
127 static __inline NDIS_STATUS
128 OvsInitForwardingCtx(OvsForwardingContext *ovsFwdCtx,
129 POVS_SWITCH_CONTEXT switchContext,
130 PNET_BUFFER_LIST curNbl,
133 PNDIS_SWITCH_FORWARDING_DETAIL_NET_BUFFER_LIST_INFO fwdDetail,
134 OvsCompletionList *completionList,
135 OVS_PACKET_HDR_INFO *layers,
136 BOOLEAN resetTunnelInfo)
139 ASSERT(switchContext);
144 * Set values for curNbl and switchContext so upon failures, we have enough
145 * information to do cleanup.
147 ovsFwdCtx->curNbl = curNbl;
148 ovsFwdCtx->switchContext = switchContext;
149 ovsFwdCtx->completionList = completionList;
150 ovsFwdCtx->fwdDetail = fwdDetail;
152 if (fwdDetail->NumAvailableDestinations > 0) {
154 * XXX: even though MSDN says GetNetBufferListDestinations() returns
155 * NDIS_STATUS, the header files say otherwise.
157 switchContext->NdisSwitchHandlers.GetNetBufferListDestinations(
158 switchContext->NdisSwitchContext, curNbl,
159 &ovsFwdCtx->destinationPorts);
161 ASSERT(ovsFwdCtx->destinationPorts);
162 /* Ensure that none of the elements are consumed yet. */
163 ASSERT(ovsFwdCtx->destinationPorts->NumElements ==
164 fwdDetail->NumAvailableDestinations);
166 ovsFwdCtx->destinationPorts = NULL;
168 ovsFwdCtx->destPortsSizeIn = fwdDetail->NumAvailableDestinations;
169 ovsFwdCtx->destPortsSizeOut = 0;
170 ovsFwdCtx->srcVportNo = srcVportNo;
171 ovsFwdCtx->sendFlags = sendFlags;
173 ovsFwdCtx->layers = *layers;
175 RtlZeroMemory(&ovsFwdCtx->layers, sizeof ovsFwdCtx->layers);
177 if (resetTunnelInfo) {
178 ovsFwdCtx->tunnelTxNic = NULL;
179 ovsFwdCtx->tunnelRxNic = NULL;
180 RtlZeroMemory(&ovsFwdCtx->tunKey, sizeof ovsFwdCtx->tunKey);
183 return NDIS_STATUS_SUCCESS;
187 * --------------------------------------------------------------------------
188 * OvsDetectTunnelRxPkt --
189 * Utility function for an RX packet to detect its tunnel type.
192 * True - if the tunnel type was detected.
193 * False - if not a tunnel packet or tunnel type not supported.
194 * --------------------------------------------------------------------------
196 static __inline BOOLEAN
197 OvsDetectTunnelRxPkt(OvsForwardingContext *ovsFwdCtx,
198 const OvsFlowKey *flowKey)
200 POVS_VPORT_ENTRY tunnelVport = NULL;
202 /* XXX: we should also check for the length of the UDP payload to pick
203 * packets only if they are at least VXLAN header size.
205 if (!flowKey->ipKey.nwFrag &&
206 flowKey->ipKey.nwProto == IPPROTO_UDP &&
207 flowKey->ipKey.l4.tpDst == VXLAN_UDP_PORT_NBO) {
208 tunnelVport = ovsFwdCtx->switchContext->vxlanVport;
209 ovsActionStats.rxVxlan++;
212 // We might get tunnel packets even before the tunnel gets initialized.
214 ASSERT(ovsFwdCtx->tunnelRxNic == NULL);
215 ovsFwdCtx->tunnelRxNic = tunnelVport;
223 * --------------------------------------------------------------------------
224 * OvsDetectTunnelPkt --
225 * Utility function to detect if a packet is to be subjected to
226 * tunneling (Tx) or de-tunneling (Rx). Various factors such as source
227 * port, destination port, packet contents, and previously setup tunnel
231 * True - If the packet is to be subjected to tunneling.
232 * In case of invalid tunnel context, the tunneling functionality is
233 * a no-op and is completed within this function itself by consuming
234 * all of the tunneling context.
235 * False - If not a tunnel packet or tunnel type not supported. Caller should
236 * process the packet as a non-tunnel packet.
237 * --------------------------------------------------------------------------
239 static __inline BOOLEAN
240 OvsDetectTunnelPkt(OvsForwardingContext *ovsFwdCtx,
241 const POVS_VPORT_ENTRY dstVport,
242 const OvsFlowKey *flowKey)
244 if (OvsIsInternalVportType(dstVport->ovsType)) {
247 * The source of NBL during tunneling Rx could be the external
248 * port or if it is being executed from userspace, the source port is
251 BOOLEAN validSrcPort =
252 (ovsFwdCtx->fwdDetail->SourcePortId ==
253 ovsFwdCtx->switchContext->virtualExternalPortId) ||
254 (ovsFwdCtx->fwdDetail->SourcePortId ==
255 NDIS_SWITCH_DEFAULT_PORT_ID);
257 if (validSrcPort && OvsDetectTunnelRxPkt(ovsFwdCtx, flowKey)) {
258 ASSERT(ovsFwdCtx->tunnelTxNic == NULL);
259 ASSERT(ovsFwdCtx->tunnelRxNic != NULL);
262 } else if (OvsIsTunnelVportType(dstVport->ovsType)) {
263 ASSERT(ovsFwdCtx->tunnelTxNic == NULL);
264 ASSERT(ovsFwdCtx->tunnelRxNic == NULL);
268 * The destination port is a tunnel port. Encapsulation must be
269 * performed only on packets that originate from a VIF port or from
270 * userspace (default port)
272 * If the packet will not be encapsulated, consume the tunnel context
275 if (ovsFwdCtx->srcVportNo != OVS_DEFAULT_PORT_NO) {
277 POVS_VPORT_ENTRY vport = OvsFindVportByPortNo(
278 ovsFwdCtx->switchContext, ovsFwdCtx->srcVportNo);
280 if (!vport || vport->ovsType != OVS_VPORT_TYPE_NETDEV) {
281 ovsFwdCtx->tunKey.dst = 0;
285 /* Tunnel the packet only if tunnel context is set. */
286 if (ovsFwdCtx->tunKey.dst != 0) {
287 ovsActionStats.txVxlan++;
288 ovsFwdCtx->tunnelTxNic = dstVport;
299 * --------------------------------------------------------------------------
301 * Add the specified destination vport into the forwarding context. If the
302 * vport is a VIF/external port, it is added directly to the NBL. If it is
303 * a tunneling port, it is NOT added to the NBL.
306 * NDIS_STATUS_SUCCESS on success
307 * Other NDIS_STATUS upon failure.
308 * --------------------------------------------------------------------------
310 static __inline NDIS_STATUS
311 OvsAddPorts(OvsForwardingContext *ovsFwdCtx,
313 NDIS_SWITCH_PORT_ID dstPortId,
314 BOOLEAN preserveVLAN,
315 BOOLEAN preservePriority)
317 POVS_VPORT_ENTRY vport;
318 PNDIS_SWITCH_PORT_DESTINATION fwdPort;
320 POVS_SWITCH_CONTEXT switchContext = ovsFwdCtx->switchContext;
323 * We hold the dispatch lock that protects the list of vports, so vports
324 * validated here can be added as destinations safely before we call into
327 * Some of the vports can be tunnelled ports as well in which case
328 * they should be added to a separate list of tunnelled destination ports
329 * instead of the VIF ports. The context for the tunnel is settable
330 * in OvsForwardingContext.
332 vport = OvsFindVportByPortNo(ovsFwdCtx->switchContext, dstPortId);
333 if (vport == NULL || vport->ovsState != OVS_STATE_CONNECTED) {
335 * There may be some latency between a port disappearing, and userspace
336 * updating the recalculated flows. In the meantime, handle invalid
339 ovsActionStats.noVport++;
340 return NDIS_STATUS_SUCCESS;
342 ASSERT(vport->nicState == NdisSwitchNicStateConnected);
343 vport->stats.txPackets++;
344 vport->stats.txBytes +=
345 NET_BUFFER_DATA_LENGTH(NET_BUFFER_LIST_FIRST_NB(ovsFwdCtx->curNbl));
347 if (OvsIsBridgeInternalVport(vport)) {
348 return NDIS_STATUS_SUCCESS;
351 if (OvsDetectTunnelPkt(ovsFwdCtx, vport, flowKey)) {
352 return NDIS_STATUS_SUCCESS;
355 if (ovsFwdCtx->destPortsSizeOut == ovsFwdCtx->destPortsSizeIn) {
356 if (ovsFwdCtx->destPortsSizeIn == 0) {
357 ASSERT(ovsFwdCtx->destinationPorts == NULL);
358 ASSERT(ovsFwdCtx->fwdDetail->NumAvailableDestinations == 0);
360 switchContext->NdisSwitchHandlers.GrowNetBufferListDestinations(
361 switchContext->NdisSwitchContext, ovsFwdCtx->curNbl,
362 OVS_DEST_PORTS_ARRAY_MIN_SIZE,
363 &ovsFwdCtx->destinationPorts);
364 if (status != NDIS_STATUS_SUCCESS) {
365 ovsActionStats.cannotGrowDest++;
368 ovsFwdCtx->destPortsSizeIn =
369 ovsFwdCtx->fwdDetail->NumAvailableDestinations;
370 ASSERT(ovsFwdCtx->destinationPorts);
372 ASSERT(ovsFwdCtx->destinationPorts != NULL);
375 * A ULONG value that specifies the total number of
376 * NDIS_SWITCH_PORT_DESTINATION elements in the
377 * NDIS_SWITCH_FORWARDING_DESTINATION_ARRAY structure.
380 * A ULONG value that specifies the number of
381 * NDIS_SWITCH_PORT_DESTINATION elements in the
382 * NDIS_SWITCH_FORWARDING_DESTINATION_ARRAY structure that
383 * specify port destinations.
385 * NumAvailableDestinations:
386 * A value that specifies the number of unused extensible switch
387 * destination ports elements within an NET_BUFFER_LIST structure.
389 ASSERT(ovsFwdCtx->destinationPorts->NumElements ==
390 ovsFwdCtx->destPortsSizeIn);
391 ASSERT(ovsFwdCtx->destinationPorts->NumDestinations ==
392 ovsFwdCtx->destPortsSizeOut -
393 ovsFwdCtx->fwdDetail->NumAvailableDestinations);
394 ASSERT(ovsFwdCtx->fwdDetail->NumAvailableDestinations > 0);
396 * Before we grow the array of destination ports, the current set
397 * of ports needs to be committed. Only the ports added since the
398 * last commit need to be part of the new update.
400 status = switchContext->NdisSwitchHandlers.UpdateNetBufferListDestinations(
401 switchContext->NdisSwitchContext, ovsFwdCtx->curNbl,
402 ovsFwdCtx->fwdDetail->NumAvailableDestinations,
403 ovsFwdCtx->destinationPorts);
404 if (status != NDIS_STATUS_SUCCESS) {
405 ovsActionStats.cannotGrowDest++;
408 ASSERT(ovsFwdCtx->destinationPorts->NumElements ==
409 ovsFwdCtx->destPortsSizeIn);
410 ASSERT(ovsFwdCtx->destinationPorts->NumDestinations ==
411 ovsFwdCtx->destPortsSizeOut);
412 ASSERT(ovsFwdCtx->fwdDetail->NumAvailableDestinations == 0);
414 status = switchContext->NdisSwitchHandlers.GrowNetBufferListDestinations(
415 switchContext->NdisSwitchContext, ovsFwdCtx->curNbl,
416 ovsFwdCtx->destPortsSizeIn, &ovsFwdCtx->destinationPorts);
417 if (status != NDIS_STATUS_SUCCESS) {
418 ovsActionStats.cannotGrowDest++;
421 ASSERT(ovsFwdCtx->destinationPorts != NULL);
422 ovsFwdCtx->destPortsSizeIn <<= 1;
426 ASSERT(ovsFwdCtx->destPortsSizeOut < ovsFwdCtx->destPortsSizeIn);
428 NDIS_SWITCH_PORT_DESTINATION_AT_ARRAY_INDEX(ovsFwdCtx->destinationPorts,
429 ovsFwdCtx->destPortsSizeOut);
431 fwdPort->PortId = vport->portId;
432 fwdPort->NicIndex = vport->nicIndex;
433 fwdPort->IsExcluded = 0;
434 fwdPort->PreserveVLAN = preserveVLAN;
435 fwdPort->PreservePriority = preservePriority;
436 ovsFwdCtx->destPortsSizeOut += 1;
438 return NDIS_STATUS_SUCCESS;
443 * --------------------------------------------------------------------------
444 * OvsClearTunTxCtx --
445 * Utility function to clear tx tunneling context.
446 * --------------------------------------------------------------------------
449 OvsClearTunTxCtx(OvsForwardingContext *ovsFwdCtx)
451 ovsFwdCtx->tunnelTxNic = NULL;
452 ovsFwdCtx->tunKey.dst = 0;
457 * --------------------------------------------------------------------------
458 * OvsClearTunRxCtx --
459 * Utility function to clear rx tunneling context.
460 * --------------------------------------------------------------------------
463 OvsClearTunRxCtx(OvsForwardingContext *ovsFwdCtx)
465 ovsFwdCtx->tunnelRxNic = NULL;
466 ovsFwdCtx->tunKey.dst = 0;
471 * --------------------------------------------------------------------------
472 * OvsCompleteNBLForwardingCtx --
473 * This utility function is responsible for freeing/completing an NBL - either
474 * by adding it to a completion list or by freeing it.
477 * It also resets the necessary fields in 'ovsFwdCtx'.
478 * --------------------------------------------------------------------------
481 OvsCompleteNBLForwardingCtx(OvsForwardingContext *ovsFwdCtx,
484 NDIS_STRING filterReason;
486 RtlInitUnicodeString(&filterReason, dropReason);
487 if (ovsFwdCtx->completionList) {
488 OvsAddPktCompletionList(ovsFwdCtx->completionList, TRUE,
489 ovsFwdCtx->fwdDetail->SourcePortId, ovsFwdCtx->curNbl, 1,
491 ovsFwdCtx->curNbl = NULL;
493 /* If there is no completionList, we assume this is ovs created NBL */
494 ovsFwdCtx->curNbl = OvsCompleteNBL(ovsFwdCtx->switchContext,
495 ovsFwdCtx->curNbl, TRUE);
496 ASSERT(ovsFwdCtx->curNbl == NULL);
498 /* XXX: these can be made debug only to save cycles. Ideally the pipeline
499 * using these fields should reset the values at the end of the pipeline. */
500 ovsFwdCtx->destPortsSizeOut = 0;
501 ovsFwdCtx->tunnelTxNic = NULL;
502 ovsFwdCtx->tunnelRxNic = NULL;
506 * --------------------------------------------------------------------------
507 * OvsDoFlowLookupOutput --
508 * Function to be used for the second stage of a tunneling workflow, ie.:
509 * - On the encapsulated packet on Tx path, to do a flow extract, flow
510 * lookup and excuting the actions.
511 * - On the decapsulated packet on Rx path, to do a flow extract, flow
512 * lookup and excuting the actions.
514 * XXX: It is assumed that the NBL in 'ovsFwdCtx' is owned by OVS. This is
515 * until the new buffer management framework is adopted.
518 * The NBL in 'ovsFwdCtx' is consumed.
519 * --------------------------------------------------------------------------
521 static __inline NDIS_STATUS
522 OvsDoFlowLookupOutput(OvsForwardingContext *ovsFwdCtx)
528 POVS_VPORT_ENTRY vport =
529 OvsFindVportByPortNo(ovsFwdCtx->switchContext, ovsFwdCtx->srcVportNo);
530 if (vport == NULL || vport->ovsState != OVS_STATE_CONNECTED) {
531 ASSERT(FALSE); // XXX: let's catch this for now
532 OvsCompleteNBLForwardingCtx(ovsFwdCtx,
533 L"OVS-Dropped due to internal/tunnel port removal");
534 ovsActionStats.noVport++;
535 return NDIS_STATUS_SUCCESS;
537 ASSERT(vport->nicState == NdisSwitchNicStateConnected);
539 /* Assert that in the Rx direction, key is always setup. */
540 ASSERT(ovsFwdCtx->tunnelRxNic == NULL || ovsFwdCtx->tunKey.dst != 0);
541 status = OvsExtractFlow(ovsFwdCtx->curNbl, ovsFwdCtx->srcVportNo,
542 &key, &ovsFwdCtx->layers, ovsFwdCtx->tunKey.dst != 0 ?
543 &ovsFwdCtx->tunKey : NULL);
544 if (status != NDIS_STATUS_SUCCESS) {
545 OvsCompleteNBLForwardingCtx(ovsFwdCtx,
546 L"OVS-Flow extract failed");
547 ovsActionStats.failedFlowExtract++;
551 flow = OvsLookupFlow(&ovsFwdCtx->switchContext->datapath, &key, &hash, FALSE);
553 OvsFlowUsed(flow, ovsFwdCtx->curNbl, &ovsFwdCtx->layers);
554 ovsFwdCtx->switchContext->datapath.hits++;
555 status = OvsActionsExecute(ovsFwdCtx->switchContext,
556 ovsFwdCtx->completionList, ovsFwdCtx->curNbl,
557 ovsFwdCtx->srcVportNo, ovsFwdCtx->sendFlags,
558 &key, &hash, &ovsFwdCtx->layers,
559 flow->actions, flow->actionsLen);
560 ovsFwdCtx->curNbl = NULL;
562 LIST_ENTRY missedPackets;
564 ovsFwdCtx->switchContext->datapath.misses++;
565 InitializeListHead(&missedPackets);
566 status = OvsCreateAndAddPackets(NULL, 0, OVS_PACKET_CMD_MISS,
567 ovsFwdCtx->srcVportNo,
568 &key,ovsFwdCtx->curNbl,
569 ovsFwdCtx->tunnelRxNic != NULL, &ovsFwdCtx->layers,
570 ovsFwdCtx->switchContext, &missedPackets, &num);
572 OvsQueuePackets(&missedPackets, num);
574 if (status == NDIS_STATUS_SUCCESS) {
575 /* Complete the packet since it was copied to user buffer. */
576 OvsCompleteNBLForwardingCtx(ovsFwdCtx,
577 L"OVS-Dropped since packet was copied to userspace");
578 ovsActionStats.flowMiss++;
579 status = NDIS_STATUS_SUCCESS;
581 OvsCompleteNBLForwardingCtx(ovsFwdCtx,
582 L"OVS-Dropped due to failure to queue to userspace");
583 status = NDIS_STATUS_FAILURE;
584 ovsActionStats.failedFlowMiss++;
592 * --------------------------------------------------------------------------
594 * The start function for Tx tunneling - encapsulates the packet, and
595 * outputs the packet on the PIF bridge.
598 * The NBL in 'ovsFwdCtx' is consumed.
599 * --------------------------------------------------------------------------
601 static __inline NDIS_STATUS
602 OvsTunnelPortTx(OvsForwardingContext *ovsFwdCtx)
604 NDIS_STATUS status = NDIS_STATUS_FAILURE;
605 PNET_BUFFER_LIST newNbl = NULL;
608 * Setup the source port to be the internal port to as to facilitate the
609 * second OvsLookupFlow.
611 if (ovsFwdCtx->switchContext->internalVport == NULL) {
612 OvsClearTunTxCtx(ovsFwdCtx);
613 OvsCompleteNBLForwardingCtx(ovsFwdCtx,
614 L"OVS-Dropped since internal port is absent");
615 return NDIS_STATUS_FAILURE;
617 ovsFwdCtx->srcVportNo =
618 ((POVS_VPORT_ENTRY)ovsFwdCtx->switchContext->internalVport)->portNo;
620 ovsFwdCtx->fwdDetail->SourcePortId = ovsFwdCtx->switchContext->internalPortId;
621 ovsFwdCtx->fwdDetail->SourceNicIndex =
622 ((POVS_VPORT_ENTRY)ovsFwdCtx->switchContext->internalVport)->nicIndex;
624 /* Do the encap. Encap function does not consume the NBL. */
625 switch(ovsFwdCtx->tunnelTxNic->ovsType) {
626 case OVS_VPORT_TYPE_VXLAN:
627 status = OvsEncapVxlan(ovsFwdCtx->curNbl, &ovsFwdCtx->tunKey,
628 ovsFwdCtx->switchContext,
629 (VOID *)ovsFwdCtx->completionList,
630 &ovsFwdCtx->layers, &newNbl);
633 ASSERT(! "Tx: Unhandled tunnel type");
636 /* Reset the tunnel context so that it doesn't get used after this point. */
637 OvsClearTunTxCtx(ovsFwdCtx);
639 if (status == NDIS_STATUS_SUCCESS) {
641 OvsCompleteNBLForwardingCtx(ovsFwdCtx,
642 L"Complete after cloning NBL for encapsulation");
643 ovsFwdCtx->curNbl = newNbl;
644 status = OvsDoFlowLookupOutput(ovsFwdCtx);
645 ASSERT(ovsFwdCtx->curNbl == NULL);
648 * XXX: Temporary freeing of the packet until we register a
649 * callback to IP helper.
651 OvsCompleteNBLForwardingCtx(ovsFwdCtx,
652 L"OVS-Dropped due to encap failure");
653 ovsActionStats.failedEncap++;
654 status = NDIS_STATUS_SUCCESS;
661 * --------------------------------------------------------------------------
663 * Decapsulate the incoming NBL based on the tunnel type and goes through
664 * the flow lookup for the inner packet.
666 * Note: IP checksum is validate here, but L4 checksum validation needs
667 * to be done by the corresponding tunnel types.
670 * The NBL in 'ovsFwdCtx' is consumed.
671 * --------------------------------------------------------------------------
673 static __inline NDIS_STATUS
674 OvsTunnelPortRx(OvsForwardingContext *ovsFwdCtx)
676 NDIS_STATUS status = NDIS_STATUS_SUCCESS;
677 PNET_BUFFER_LIST newNbl = NULL;
678 POVS_VPORT_ENTRY tunnelRxVport = ovsFwdCtx->tunnelRxNic;
680 if (OvsValidateIPChecksum(ovsFwdCtx->curNbl, &ovsFwdCtx->layers)
681 != NDIS_STATUS_SUCCESS) {
682 ovsActionStats.failedChecksum++;
683 OVS_LOG_INFO("Packet dropped due to IP checksum failure.");
687 switch(tunnelRxVport->ovsType) {
688 case OVS_VPORT_TYPE_VXLAN:
690 * OvsDoDecapVxlan should return a new NBL if it was copied, and
691 * this new NBL should be setup as the ovsFwdCtx->curNbl.
693 status = OvsDoDecapVxlan(ovsFwdCtx->switchContext, ovsFwdCtx->curNbl,
694 &ovsFwdCtx->tunKey, &newNbl);
697 OVS_LOG_ERROR("Rx: Unhandled tunnel type: %d\n",
698 tunnelRxVport->ovsType);
699 ASSERT(! "Rx: Unhandled tunnel type");
700 status = NDIS_STATUS_NOT_SUPPORTED;
703 if (status != NDIS_STATUS_SUCCESS) {
704 ovsActionStats.failedDecap++;
709 * tunnelRxNic and other fields will be cleared, re-init the context
712 OvsCompleteNBLForwardingCtx(ovsFwdCtx,
713 L"OVS-dropped due to new decap packet");
715 /* Decapsulated packet is in a new NBL */
716 ovsFwdCtx->tunnelRxNic = tunnelRxVport;
717 OvsInitForwardingCtx(ovsFwdCtx, ovsFwdCtx->switchContext,
718 newNbl, tunnelRxVport->portNo, 0,
719 NET_BUFFER_LIST_SWITCH_FORWARDING_DETAIL(newNbl),
720 ovsFwdCtx->completionList,
721 &ovsFwdCtx->layers, FALSE);
724 * Set the NBL's SourcePortId and SourceNicIndex to default values to
725 * keep NDIS happy when we forward the packet.
727 ovsFwdCtx->fwdDetail->SourcePortId = NDIS_SWITCH_DEFAULT_PORT_ID;
728 ovsFwdCtx->fwdDetail->SourceNicIndex = 0;
730 status = OvsDoFlowLookupOutput(ovsFwdCtx);
731 ASSERT(ovsFwdCtx->curNbl == NULL);
732 OvsClearTunRxCtx(ovsFwdCtx);
737 OvsCompleteNBLForwardingCtx(ovsFwdCtx,
738 L"OVS-dropped due to decap failure");
739 OvsClearTunRxCtx(ovsFwdCtx);
745 * --------------------------------------------------------------------------
746 * OvsOutputForwardingCtx --
747 * This function outputs an NBL to NDIS or to a tunneling pipeline based on
748 * the ports added so far into 'ovsFwdCtx'.
751 * This function consumes the NBL - either by forwarding it successfully to
752 * NDIS, or adding it to the completion list in 'ovsFwdCtx', or freeing it.
754 * Also makes sure that the list of destination ports - tunnel or otherwise is
756 * --------------------------------------------------------------------------
758 static __inline NDIS_STATUS
759 OvsOutputForwardingCtx(OvsForwardingContext *ovsFwdCtx)
761 NDIS_STATUS status = STATUS_SUCCESS;
762 POVS_SWITCH_CONTEXT switchContext = ovsFwdCtx->switchContext;
765 * Handle the case where the some of the destination ports are tunneled
766 * ports - the non-tunneled ports get a unmodified copy of the NBL, and the
767 * tunneling pipeline starts when we output the packet to tunneled port.
769 if (ovsFwdCtx->destPortsSizeOut > 0) {
770 PNET_BUFFER_LIST newNbl = NULL;
772 UINT32 portsToUpdate =
773 ovsFwdCtx->fwdDetail->NumAvailableDestinations -
774 (ovsFwdCtx->destPortsSizeIn - ovsFwdCtx->destPortsSizeOut);
776 ASSERT(ovsFwdCtx->destinationPorts != NULL);
779 * Create a copy of the packet in order to do encap on it later. Also,
780 * don't copy the offload context since the encap'd packet has a
781 * different set of headers. This will change when we implement offloads
782 * before doing encapsulation.
784 if (ovsFwdCtx->tunnelTxNic != NULL || ovsFwdCtx->tunnelRxNic != NULL) {
785 nb = NET_BUFFER_LIST_FIRST_NB(ovsFwdCtx->curNbl);
786 newNbl = OvsPartialCopyNBL(ovsFwdCtx->switchContext, ovsFwdCtx->curNbl,
787 0, 0, TRUE /*copy NBL info*/);
788 if (newNbl == NULL) {
789 status = NDIS_STATUS_RESOURCES;
790 ovsActionStats.noCopiedNbl++;
795 /* It does not seem like we'll get here unless 'portsToUpdate' > 0. */
796 ASSERT(portsToUpdate > 0);
797 status = switchContext->NdisSwitchHandlers.UpdateNetBufferListDestinations(
798 switchContext->NdisSwitchContext, ovsFwdCtx->curNbl,
799 portsToUpdate, ovsFwdCtx->destinationPorts);
800 if (status != NDIS_STATUS_SUCCESS) {
801 OvsCompleteNBL(ovsFwdCtx->switchContext, newNbl, TRUE);
802 ovsActionStats.cannotGrowDest++;
806 OvsSendNBLIngress(ovsFwdCtx->switchContext, ovsFwdCtx->curNbl,
807 ovsFwdCtx->sendFlags);
808 /* End this pipeline by resetting the corresponding context. */
809 ovsFwdCtx->destPortsSizeOut = 0;
810 ovsFwdCtx->curNbl = NULL;
812 status = OvsInitForwardingCtx(ovsFwdCtx, ovsFwdCtx->switchContext,
813 newNbl, ovsFwdCtx->srcVportNo, 0,
814 NET_BUFFER_LIST_SWITCH_FORWARDING_DETAIL(newNbl),
815 ovsFwdCtx->completionList,
816 &ovsFwdCtx->layers, FALSE);
817 if (status != NDIS_STATUS_SUCCESS) {
818 OvsCompleteNBLForwardingCtx(ovsFwdCtx,
819 L"Dropped due to resouces");
825 if (ovsFwdCtx->tunnelTxNic != NULL) {
826 status = OvsTunnelPortTx(ovsFwdCtx);
827 ASSERT(ovsFwdCtx->tunnelTxNic == NULL);
828 ASSERT(ovsFwdCtx->tunKey.dst == 0);
829 } else if (ovsFwdCtx->tunnelRxNic != NULL) {
830 status = OvsTunnelPortRx(ovsFwdCtx);
831 ASSERT(ovsFwdCtx->tunnelRxNic == NULL);
832 ASSERT(ovsFwdCtx->tunKey.dst == 0);
834 ASSERT(ovsFwdCtx->curNbl == NULL);
839 if (status != NDIS_STATUS_SUCCESS) {
840 OvsCompleteNBLForwardingCtx(ovsFwdCtx, L"Dropped due to XXX");
848 * --------------------------------------------------------------------------
849 * OvsLookupFlowOutput --
850 * Utility function for external callers to do flow extract, lookup,
851 * actions execute on a given NBL.
853 * Note: If this is being used from a callback function, make sure that the
854 * arguments specified are still valid in the asynchronous context.
857 * This function consumes the NBL.
858 * --------------------------------------------------------------------------
861 OvsLookupFlowOutput(POVS_SWITCH_CONTEXT switchContext,
863 PNET_BUFFER_LIST curNbl)
866 OvsForwardingContext ovsFwdCtx;
867 POVS_VPORT_ENTRY internalVport =
868 (POVS_VPORT_ENTRY)switchContext->internalVport;
870 /* XXX: make sure comp list was not a stack variable previously. */
871 OvsCompletionList *completionList = (OvsCompletionList *)compList;
874 * XXX: can internal port disappear while we are busy doing ARP resolution?
875 * It could, but will we get this callback from IP helper in that case. Need
878 ASSERT(switchContext->internalVport);
879 status = OvsInitForwardingCtx(&ovsFwdCtx, switchContext, curNbl,
880 internalVport->portNo, 0,
881 NET_BUFFER_LIST_SWITCH_FORWARDING_DETAIL(curNbl),
882 completionList, NULL, TRUE);
883 if (status != NDIS_STATUS_SUCCESS) {
884 OvsCompleteNBLForwardingCtx(&ovsFwdCtx,
885 L"OVS-Dropped due to resources");
891 * XXX: We need to acquire the dispatch lock and the datapath lock.
894 OvsDoFlowLookupOutput(&ovsFwdCtx);
899 * --------------------------------------------------------------------------
900 * OvsOutputBeforeSetAction --
901 * Function to be called to complete one set of actions on an NBL, before
902 * we start the next one.
903 * --------------------------------------------------------------------------
905 static __inline NDIS_STATUS
906 OvsOutputBeforeSetAction(OvsForwardingContext *ovsFwdCtx)
908 PNET_BUFFER_LIST newNbl;
909 NDIS_STATUS status = NDIS_STATUS_SUCCESS;
913 * Create a copy and work on the copy after this point. The original NBL is
914 * forwarded. One reason to not use the copy for forwarding is that
915 * ports have already been added to the original NBL, and it might be
916 * inefficient/impossible to remove/re-add them to the copy. There's no
917 * notion of removing the ports, the ports need to be marked as
918 * "isExcluded". There's seems no real advantage to retaining the original
919 * and sending out the copy instead.
921 * XXX: We are copying the offload context here. This is to handle actions
923 * outport, pop_vlan(), outport, push_vlan(), outport
925 * copy size needs to include inner ether + IP + TCP, need to revisit
926 * if we support IP options.
927 * XXX Head room needs to include the additional encap.
928 * XXX copySize check is not considering multiple NBs.
930 nb = NET_BUFFER_LIST_FIRST_NB(ovsFwdCtx->curNbl);
931 newNbl = OvsPartialCopyNBL(ovsFwdCtx->switchContext, ovsFwdCtx->curNbl,
932 0, 0, TRUE /*copy NBL info*/);
934 ASSERT(ovsFwdCtx->destPortsSizeOut > 0 ||
935 ovsFwdCtx->tunnelTxNic != NULL || ovsFwdCtx->tunnelRxNic != NULL);
937 /* Send the original packet out */
938 status = OvsOutputForwardingCtx(ovsFwdCtx);
939 ASSERT(ovsFwdCtx->curNbl == NULL);
940 ASSERT(ovsFwdCtx->destPortsSizeOut == 0);
941 ASSERT(ovsFwdCtx->tunnelRxNic == NULL);
942 ASSERT(ovsFwdCtx->tunnelTxNic == NULL);
944 /* If we didn't make a copy, can't continue. */
945 if (newNbl == NULL) {
946 ovsActionStats.noCopiedNbl++;
947 return NDIS_STATUS_RESOURCES;
950 /* Finish the remaining actions with the new NBL */
951 if (status != NDIS_STATUS_SUCCESS) {
952 OvsCompleteNBL(ovsFwdCtx->switchContext, newNbl, TRUE);
954 status = OvsInitForwardingCtx(ovsFwdCtx, ovsFwdCtx->switchContext,
955 newNbl, ovsFwdCtx->srcVportNo, 0,
956 NET_BUFFER_LIST_SWITCH_FORWARDING_DETAIL(newNbl),
957 ovsFwdCtx->completionList,
958 &ovsFwdCtx->layers, FALSE);
966 * --------------------------------------------------------------------------
967 * OvsPopVlanInPktBuf --
968 * Function to pop a VLAN tag when the tag is in the packet buffer.
969 * --------------------------------------------------------------------------
971 static __inline NDIS_STATUS
972 OvsPopVlanInPktBuf(OvsForwardingContext *ovsFwdCtx)
977 ULONG dataLength = sizeof (DL_EUI48) + sizeof (DL_EUI48);
978 UINT32 packetLen, mdlLen;
979 PNET_BUFFER_LIST newNbl;
983 * Declare a dummy vlanTag structure since we need to compute the size
984 * of shiftLength. The NDIS one is a unionized structure.
986 NDIS_PACKET_8021Q_INFO vlanTag = {0};
987 ULONG shiftLength = sizeof (vlanTag.TagHeader);
988 PUINT8 tempBuffer[sizeof (DL_EUI48) + sizeof (DL_EUI48)];
990 newNbl = OvsPartialCopyNBL(ovsFwdCtx->switchContext, ovsFwdCtx->curNbl,
991 0, 0, TRUE /* copy NBL info */);
993 ovsActionStats.noCopiedNbl++;
994 return NDIS_STATUS_RESOURCES;
997 /* Complete the original NBL and create a copy to modify. */
998 OvsCompleteNBLForwardingCtx(ovsFwdCtx, L"OVS-Dropped due to copy");
1000 status = OvsInitForwardingCtx(ovsFwdCtx, ovsFwdCtx->switchContext,
1001 newNbl, ovsFwdCtx->srcVportNo, 0,
1002 NET_BUFFER_LIST_SWITCH_FORWARDING_DETAIL(newNbl),
1003 NULL, &ovsFwdCtx->layers, FALSE);
1004 if (status != NDIS_STATUS_SUCCESS) {
1005 OvsCompleteNBLForwardingCtx(ovsFwdCtx,
1006 L"Dropped due to resouces");
1007 return NDIS_STATUS_RESOURCES;
1010 curNb = NET_BUFFER_LIST_FIRST_NB(ovsFwdCtx->curNbl);
1011 packetLen = NET_BUFFER_DATA_LENGTH(curNb);
1012 ASSERT(curNb->Next == NULL);
1013 curMdl = NET_BUFFER_CURRENT_MDL(curNb);
1014 NdisQueryMdl(curMdl, &bufferStart, &mdlLen, LowPagePriority);
1016 return NDIS_STATUS_RESOURCES;
1018 mdlLen -= NET_BUFFER_CURRENT_MDL_OFFSET(curNb);
1019 /* Bail out if L2 + VLAN header is not contiguous in the first buffer. */
1020 if (MIN(packetLen, mdlLen) < sizeof (EthHdr) + shiftLength) {
1022 return NDIS_STATUS_FAILURE;
1024 bufferStart += NET_BUFFER_CURRENT_MDL_OFFSET(curNb);
1025 RtlCopyMemory(tempBuffer, bufferStart, dataLength);
1026 RtlCopyMemory(bufferStart + shiftLength, tempBuffer, dataLength);
1027 NdisAdvanceNetBufferDataStart(curNb, shiftLength, FALSE, NULL);
1029 return NDIS_STATUS_SUCCESS;
1033 * --------------------------------------------------------------------------
1034 * OvsTunnelAttrToIPv4TunnelKey --
1035 * Convert tunnel attribute to OvsIPv4TunnelKey.
1036 * --------------------------------------------------------------------------
1038 static __inline NDIS_STATUS
1039 OvsTunnelAttrToIPv4TunnelKey(PNL_ATTR attr,
1040 OvsIPv4TunnelKey *tunKey)
1045 tunKey->attr[0] = 0;
1046 tunKey->attr[1] = 0;
1047 tunKey->attr[2] = 0;
1048 ASSERT(NlAttrType(attr) == OVS_KEY_ATTR_TUNNEL);
1050 NL_ATTR_FOR_EACH_UNSAFE (a, rem, NlAttrData(attr),
1051 NlAttrGetSize(attr)) {
1052 switch (NlAttrType(a)) {
1053 case OVS_TUNNEL_KEY_ATTR_ID:
1054 tunKey->tunnelId = NlAttrGetBe64(a);
1055 tunKey->flags |= OVS_TNL_F_KEY;
1057 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
1058 tunKey->src = NlAttrGetBe32(a);
1060 case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
1061 tunKey->dst = NlAttrGetBe32(a);
1063 case OVS_TUNNEL_KEY_ATTR_TOS:
1064 tunKey->tos = NlAttrGetU8(a);
1066 case OVS_TUNNEL_KEY_ATTR_TTL:
1067 tunKey->ttl = NlAttrGetU8(a);
1069 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
1070 tunKey->flags |= OVS_TNL_F_DONT_FRAGMENT;
1072 case OVS_TUNNEL_KEY_ATTR_CSUM:
1073 tunKey->flags |= OVS_TNL_F_CSUM;
1080 return NDIS_STATUS_SUCCESS;
1084 *----------------------------------------------------------------------------
1085 * OvsUpdateEthHeader --
1086 * Updates the ethernet header in ovsFwdCtx.curNbl inline based on the
1088 *----------------------------------------------------------------------------
1090 static __inline NDIS_STATUS
1091 OvsUpdateEthHeader(OvsForwardingContext *ovsFwdCtx,
1092 const struct ovs_key_ethernet *ethAttr)
1098 UINT32 packetLen, mdlLen;
1100 curNb = NET_BUFFER_LIST_FIRST_NB(ovsFwdCtx->curNbl);
1101 ASSERT(curNb->Next == NULL);
1102 packetLen = NET_BUFFER_DATA_LENGTH(curNb);
1103 curMdl = NET_BUFFER_CURRENT_MDL(curNb);
1104 NdisQueryMdl(curMdl, &bufferStart, &mdlLen, LowPagePriority);
1106 ovsActionStats.noResource++;
1107 return NDIS_STATUS_RESOURCES;
1109 mdlLen -= NET_BUFFER_CURRENT_MDL_OFFSET(curNb);
1111 /* Bail out if the L2 header is not in a contiguous buffer. */
1112 if (MIN(packetLen, mdlLen) < sizeof *ethHdr) {
1114 return NDIS_STATUS_FAILURE;
1116 ethHdr = (EthHdr *)(bufferStart + NET_BUFFER_CURRENT_MDL_OFFSET(curNb));
1118 RtlCopyMemory(ethHdr->Destination, ethAttr->eth_dst,
1119 sizeof ethHdr->Destination);
1120 RtlCopyMemory(ethHdr->Source, ethAttr->eth_src, sizeof ethHdr->Source);
1122 return NDIS_STATUS_SUCCESS;
1126 *----------------------------------------------------------------------------
1127 * OvsUpdateIPv4Header --
1128 * Updates the IPv4 header in ovsFwdCtx.curNbl inline based on the
1130 *----------------------------------------------------------------------------
1132 static __inline NDIS_STATUS
1133 OvsUpdateIPv4Header(OvsForwardingContext *ovsFwdCtx,
1134 const struct ovs_key_ipv4 *ipAttr)
1140 UINT32 mdlLen, hdrSize, packetLen;
1141 OVS_PACKET_HDR_INFO *layers = &ovsFwdCtx->layers;
1144 TCPHdr *tcpHdr = NULL;
1145 UDPHdr *udpHdr = NULL;
1147 ASSERT(layers->value != 0);
1150 * Peek into the MDL to get a handle to the IP header and if required
1151 * the TCP/UDP header as well. We check if the required headers are in one
1152 * contiguous MDL, and if not, we copy them over to one MDL.
1154 curNb = NET_BUFFER_LIST_FIRST_NB(ovsFwdCtx->curNbl);
1155 ASSERT(curNb->Next == NULL);
1156 packetLen = NET_BUFFER_DATA_LENGTH(curNb);
1157 curMdl = NET_BUFFER_CURRENT_MDL(curNb);
1158 NdisQueryMdl(curMdl, &bufferStart, &mdlLen, LowPagePriority);
1160 ovsActionStats.noResource++;
1161 return NDIS_STATUS_RESOURCES;
1163 curMdlOffset = NET_BUFFER_CURRENT_MDL_OFFSET(curNb);
1164 mdlLen -= curMdlOffset;
1165 ASSERT((INT)mdlLen >= 0);
1167 if (layers->isTcp || layers->isUdp) {
1168 hdrSize = layers->l4Offset +
1169 layers->isTcp ? sizeof (*tcpHdr) : sizeof (*udpHdr);
1171 hdrSize = layers->l3Offset + sizeof (*ipHdr);
1174 /* Count of number of bytes of valid data there are in the first MDL. */
1175 mdlLen = MIN(packetLen, mdlLen);
1176 if (mdlLen < hdrSize) {
1177 PNET_BUFFER_LIST newNbl;
1178 newNbl = OvsPartialCopyNBL(ovsFwdCtx->switchContext, ovsFwdCtx->curNbl,
1179 hdrSize, 0, TRUE /*copy NBL info*/);
1181 ovsActionStats.noCopiedNbl++;
1182 return NDIS_STATUS_RESOURCES;
1184 OvsCompleteNBLForwardingCtx(ovsFwdCtx,
1185 L"Complete after partial copy.");
1187 status = OvsInitForwardingCtx(ovsFwdCtx, ovsFwdCtx->switchContext,
1188 newNbl, ovsFwdCtx->srcVportNo, 0,
1189 NET_BUFFER_LIST_SWITCH_FORWARDING_DETAIL(newNbl),
1190 NULL, &ovsFwdCtx->layers, FALSE);
1191 if (status != NDIS_STATUS_SUCCESS) {
1192 OvsCompleteNBLForwardingCtx(ovsFwdCtx,
1193 L"OVS-Dropped due to resources");
1194 return NDIS_STATUS_RESOURCES;
1197 curNb = NET_BUFFER_LIST_FIRST_NB(ovsFwdCtx->curNbl);
1198 ASSERT(curNb->Next == NULL);
1199 curMdl = NET_BUFFER_CURRENT_MDL(curNb);
1200 NdisQueryMdl(curMdl, &bufferStart, &mdlLen, LowPagePriority);
1202 ovsActionStats.noResource++;
1203 return NDIS_STATUS_RESOURCES;
1205 curMdlOffset = NET_BUFFER_CURRENT_MDL_OFFSET(curNb);
1206 mdlLen -= curMdlOffset;
1207 ASSERT(mdlLen >= hdrSize);
1210 ipHdr = (IPHdr *)(bufferStart + curMdlOffset + layers->l3Offset);
1212 if (layers->isTcp) {
1213 tcpHdr = (TCPHdr *)(bufferStart + curMdlOffset + layers->l4Offset);
1214 } else if (layers->isUdp) {
1215 udpHdr = (UDPHdr *)(bufferStart + curMdlOffset + layers->l4Offset);
1219 * Adjust the IP header inline as dictated by the action, nad also update
1220 * the IP and the TCP checksum for the data modified.
1222 * In the future, this could be optimized to make one call to
1223 * ChecksumUpdate32(). Ignoring this for now, since for the most common
1224 * case, we only update the TTL.
1226 if (ipHdr->saddr != ipAttr->ipv4_src) {
1228 tcpHdr->check = ChecksumUpdate32(tcpHdr->check, ipHdr->saddr,
1230 } else if (udpHdr && udpHdr->check) {
1231 udpHdr->check = ChecksumUpdate32(udpHdr->check, ipHdr->saddr,
1235 if (ipHdr->check != 0) {
1236 ipHdr->check = ChecksumUpdate32(ipHdr->check, ipHdr->saddr,
1239 ipHdr->saddr = ipAttr->ipv4_src;
1241 if (ipHdr->daddr != ipAttr->ipv4_dst) {
1243 tcpHdr->check = ChecksumUpdate32(tcpHdr->check, ipHdr->daddr,
1245 } else if (udpHdr && udpHdr->check) {
1246 udpHdr->check = ChecksumUpdate32(udpHdr->check, ipHdr->daddr,
1250 if (ipHdr->check != 0) {
1251 ipHdr->check = ChecksumUpdate32(ipHdr->check, ipHdr->daddr,
1254 ipHdr->daddr = ipAttr->ipv4_dst;
1256 if (ipHdr->protocol != ipAttr->ipv4_proto) {
1257 UINT16 oldProto = (ipHdr->protocol << 16) & 0xff00;
1258 UINT16 newProto = (ipAttr->ipv4_proto << 16) & 0xff00;
1260 tcpHdr->check = ChecksumUpdate16(tcpHdr->check, oldProto, newProto);
1261 } else if (udpHdr && udpHdr->check) {
1262 udpHdr->check = ChecksumUpdate16(udpHdr->check, oldProto, newProto);
1265 if (ipHdr->check != 0) {
1266 ipHdr->check = ChecksumUpdate16(ipHdr->check, oldProto, newProto);
1268 ipHdr->protocol = ipAttr->ipv4_proto;
1270 if (ipHdr->ttl != ipAttr->ipv4_ttl) {
1271 UINT16 oldTtl = (ipHdr->ttl) & 0xff;
1272 UINT16 newTtl = (ipAttr->ipv4_ttl) & 0xff;
1273 if (ipHdr->check != 0) {
1274 ipHdr->check = ChecksumUpdate16(ipHdr->check, oldTtl, newTtl);
1276 ipHdr->ttl = ipAttr->ipv4_ttl;
1279 return NDIS_STATUS_SUCCESS;
1283 * --------------------------------------------------------------------------
1284 * OvsExecuteSetAction --
1285 * Executes a set() action, but storing the actions into 'ovsFwdCtx'
1286 * --------------------------------------------------------------------------
1288 static __inline NDIS_STATUS
1289 OvsExecuteSetAction(OvsForwardingContext *ovsFwdCtx,
1294 enum ovs_key_attr type = NlAttrType(a);
1295 NDIS_STATUS status = NDIS_STATUS_SUCCESS;
1298 case OVS_KEY_ATTR_ETHERNET:
1299 status = OvsUpdateEthHeader(ovsFwdCtx,
1300 NlAttrGetUnspec(a, sizeof(struct ovs_key_ethernet)));
1303 case OVS_KEY_ATTR_IPV4:
1304 status = OvsUpdateIPv4Header(ovsFwdCtx,
1305 NlAttrGetUnspec(a, sizeof(struct ovs_key_ipv4)));
1308 case OVS_KEY_ATTR_TUNNEL:
1310 OvsIPv4TunnelKey tunKey;
1312 status = OvsTunnelAttrToIPv4TunnelKey((PNL_ATTR)a, &tunKey);
1313 ASSERT(status == NDIS_STATUS_SUCCESS);
1314 tunKey.flow_hash = (uint16)(hash ? *hash : OvsHashFlow(key));
1315 RtlCopyMemory(&ovsFwdCtx->tunKey, &tunKey, sizeof ovsFwdCtx->tunKey);
1319 case OVS_KEY_ATTR_SKB_MARK:
1320 /* XXX: Not relevant to Hyper-V. Return OK */
1322 case OVS_KEY_ATTR_UNSPEC:
1323 case OVS_KEY_ATTR_ENCAP:
1324 case OVS_KEY_ATTR_ETHERTYPE:
1325 case OVS_KEY_ATTR_IN_PORT:
1326 case OVS_KEY_ATTR_VLAN:
1327 case OVS_KEY_ATTR_ICMP:
1328 case OVS_KEY_ATTR_ICMPV6:
1329 case OVS_KEY_ATTR_ARP:
1330 case OVS_KEY_ATTR_ND:
1331 case __OVS_KEY_ATTR_MAX:
1333 OVS_LOG_INFO("Unhandled attribute %#x", type);
1340 * --------------------------------------------------------------------------
1341 * OvsActionsExecute --
1342 * Interpret and execute the specified 'actions' on the specifed packet
1343 * 'curNbl'. The expectation is that if the packet needs to be dropped
1344 * (completed) for some reason, it is added to 'completionList' so that the
1345 * caller can complete the packet. If 'completionList' is NULL, the NBL is
1346 * assumed to be generated by OVS and freed up. Otherwise, the function
1347 * consumes the NBL by generating a NDIS send indication for the packet.
1349 * There are one or more of "clone" NBLs that may get generated while
1350 * executing the actions. Upon any failures, the "cloned" NBLs are freed up,
1351 * and the caller does not have to worry about them.
1353 * Success or failure is returned based on whether the specified actions
1354 * were executed successfully on the packet or not.
1355 * --------------------------------------------------------------------------
1358 OvsActionsExecute(POVS_SWITCH_CONTEXT switchContext,
1359 OvsCompletionList *completionList,
1360 PNET_BUFFER_LIST curNbl,
1365 OVS_PACKET_HDR_INFO *layers,
1366 const PNL_ATTR actions,
1372 OvsForwardingContext ovsFwdCtx;
1373 PCWSTR dropReason = L"";
1375 PNDIS_SWITCH_FORWARDING_DETAIL_NET_BUFFER_LIST_INFO fwdDetail =
1376 NET_BUFFER_LIST_SWITCH_FORWARDING_DETAIL(curNbl);
1378 /* XXX: ASSERT that the flow table lock is held. */
1379 status = OvsInitForwardingCtx(&ovsFwdCtx, switchContext, curNbl, portNo,
1380 sendFlags, fwdDetail, completionList,
1382 if (status != NDIS_STATUS_SUCCESS) {
1383 dropReason = L"OVS-initing destination port list failed";
1387 if (actionsLen == 0) {
1388 dropReason = L"OVS-Dropped due to Flow action";
1389 ovsActionStats.zeroActionLen++;
1393 NL_ATTR_FOR_EACH_UNSAFE (a, rem, actions, actionsLen) {
1394 switch(NlAttrType(a)) {
1395 case OVS_ACTION_ATTR_OUTPUT:
1396 dstPortID = NlAttrGetU32(a);
1397 status = OvsAddPorts(&ovsFwdCtx, key, dstPortID,
1399 if (status != NDIS_STATUS_SUCCESS) {
1400 dropReason = L"OVS-adding destination port failed";
1405 case OVS_ACTION_ATTR_PUSH_VLAN:
1407 struct ovs_action_push_vlan *vlan;
1409 PNDIS_NET_BUFFER_LIST_8021Q_INFO vlanTag;
1411 if (ovsFwdCtx.destPortsSizeOut > 0 || ovsFwdCtx.tunnelTxNic != NULL
1412 || ovsFwdCtx.tunnelRxNic != NULL) {
1413 status = OvsOutputBeforeSetAction(&ovsFwdCtx);
1414 if (status != NDIS_STATUS_SUCCESS) {
1415 dropReason = L"OVS-adding destination failed";
1420 vlanTagValue = NET_BUFFER_LIST_INFO(ovsFwdCtx.curNbl,
1421 Ieee8021QNetBufferListInfo);
1422 if (vlanTagValue != NULL) {
1424 * XXX: We don't support double VLAN tag offload. In such cases,
1425 * we need to insert the existing one into the packet buffer,
1426 * and add the new one as offload. This will take care of
1427 * guest tag-in-tag case as well as OVS rules that specify
1432 vlanTag = (PNDIS_NET_BUFFER_LIST_8021Q_INFO)(PVOID *)&vlanTagValue;
1433 vlan = (struct ovs_action_push_vlan *)NlAttrGet((const PNL_ATTR)a);
1434 vlanTag->TagHeader.VlanId = ntohs(vlan->vlan_tci) & 0xfff;
1435 vlanTag->TagHeader.UserPriority = ntohs(vlan->vlan_tci) >> 13;
1437 NET_BUFFER_LIST_INFO(ovsFwdCtx.curNbl,
1438 Ieee8021QNetBufferListInfo) = vlanTagValue;
1443 case OVS_ACTION_ATTR_POP_VLAN:
1445 if (ovsFwdCtx.destPortsSizeOut > 0 || ovsFwdCtx.tunnelTxNic != NULL
1446 || ovsFwdCtx.tunnelRxNic != NULL) {
1447 status = OvsOutputBeforeSetAction(&ovsFwdCtx);
1448 if (status != NDIS_STATUS_SUCCESS) {
1449 dropReason = L"OVS-adding destination failed";
1454 if (NET_BUFFER_LIST_INFO(ovsFwdCtx.curNbl,
1455 Ieee8021QNetBufferListInfo) != 0) {
1456 NET_BUFFER_LIST_INFO(ovsFwdCtx.curNbl,
1457 Ieee8021QNetBufferListInfo) = 0;
1460 * The VLAN tag is inserted into the packet buffer. Pop the tag
1461 * by packet buffer modification.
1463 status = OvsPopVlanInPktBuf(&ovsFwdCtx);
1464 if (status != NDIS_STATUS_SUCCESS) {
1465 dropReason = L"OVS-pop vlan action failed";
1472 case OVS_ACTION_ATTR_USERSPACE:
1474 PNL_ATTR userdataAttr;
1476 POVS_PACKET_QUEUE_ELEM elem;
1477 BOOLEAN isRecv = FALSE;
1479 POVS_VPORT_ENTRY vport = OvsFindVportByPortNo(switchContext,
1483 if (vport->isExternal ||
1484 OvsIsTunnelVportType(vport->ovsType)) {
1489 queueAttr = NlAttrFindNested(a, OVS_USERSPACE_ATTR_PID);
1490 userdataAttr = NlAttrFindNested(a, OVS_USERSPACE_ATTR_USERDATA);
1492 elem = OvsCreateQueueNlPacket((PVOID)userdataAttr,
1493 userdataAttr->nlaLen,
1494 OVS_PACKET_CMD_ACTION,
1495 portNo, key,ovsFwdCtx.curNbl,
1496 NET_BUFFER_LIST_FIRST_NB(ovsFwdCtx.curNbl),
1500 LIST_ENTRY missedPackets;
1501 InitializeListHead(&missedPackets);
1502 InsertTailList(&missedPackets, &elem->link);
1503 OvsQueuePackets(&missedPackets, 1);
1504 dropReason = L"OVS-Completed since packet was copied to "
1507 dropReason = L"OVS-Dropped due to failure to queue to "
1513 case OVS_ACTION_ATTR_SET:
1515 if (ovsFwdCtx.destPortsSizeOut > 0 || ovsFwdCtx.tunnelTxNic != NULL
1516 || ovsFwdCtx.tunnelRxNic != NULL) {
1517 status = OvsOutputBeforeSetAction(&ovsFwdCtx);
1518 if (status != NDIS_STATUS_SUCCESS) {
1519 dropReason = L"OVS-adding destination failed";
1524 status = OvsExecuteSetAction(&ovsFwdCtx, key, hash,
1525 (const PNL_ATTR)NlAttrGet
1526 ((const PNL_ATTR)a));
1527 if (status != NDIS_STATUS_SUCCESS) {
1528 dropReason = L"OVS-set action failed";
1533 case OVS_ACTION_ATTR_SAMPLE:
1535 status = NDIS_STATUS_NOT_SUPPORTED;
1540 if (ovsFwdCtx.destPortsSizeOut > 0 || ovsFwdCtx.tunnelTxNic != NULL
1541 || ovsFwdCtx.tunnelRxNic != NULL) {
1542 status = OvsOutputForwardingCtx(&ovsFwdCtx);
1543 ASSERT(ovsFwdCtx.curNbl == NULL);
1546 ASSERT(ovsFwdCtx.destPortsSizeOut == 0);
1547 ASSERT(ovsFwdCtx.tunnelRxNic == NULL);
1548 ASSERT(ovsFwdCtx.tunnelTxNic == NULL);
1552 * If curNbl != NULL, it implies the NBL has not been not freed up so far.
1554 if (ovsFwdCtx.curNbl) {
1555 OvsCompleteNBLForwardingCtx(&ovsFwdCtx, dropReason);