forked from openvswitch/ovs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathActions.c
1597 lines (1450 loc) · 58.1 KB
/
Actions.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2014 VMware, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "precomp.h"
#include "Switch.h"
#include "Vport.h"
#include "Event.h"
#include "User.h"
#include "NetProto.h"
#include "Flow.h"
#include "Vxlan.h"
#include "Stt.h"
#include "Checksum.h"
#include "PacketIO.h"
#ifdef OVS_DBG_MOD
#undef OVS_DBG_MOD
#endif
#define OVS_DBG_MOD OVS_DBG_ACTION
#include "Debug.h"
typedef struct _OVS_ACTION_STATS {
UINT64 rxVxlan;
UINT64 txVxlan;
UINT64 rxStt;
UINT64 txStt;
UINT64 flowMiss;
UINT64 flowUserspace;
UINT64 txTcp;
UINT32 failedFlowMiss;
UINT32 noVport;
UINT32 failedFlowExtract;
UINT32 noResource;
UINT32 noCopiedNbl;
UINT32 failedEncap;
UINT32 failedDecap;
UINT32 cannotGrowDest;
UINT32 zeroActionLen;
UINT32 failedChecksum;
} OVS_ACTION_STATS, *POVS_ACTION_STATS;
OVS_ACTION_STATS ovsActionStats;
/*
* There a lot of data that needs to be maintained while executing the pipeline
* as dictated by the actions of a flow, across different functions at different
* levels. Such data is put together in a 'context' structure. Care should be
* exercised while adding new members to the structure - only add ones that get
* used across multiple stages in the pipeline/get used in multiple functions.
*/
#define OVS_DEST_PORTS_ARRAY_MIN_SIZE 2
typedef struct OvsForwardingContext {
POVS_SWITCH_CONTEXT switchContext;
/* The NBL currently used in the pipeline. */
PNET_BUFFER_LIST curNbl;
/* NDIS forwarding detail for 'curNbl'. */
PNDIS_SWITCH_FORWARDING_DETAIL_NET_BUFFER_LIST_INFO fwdDetail;
/* Array of destination ports for 'curNbl'. */
PNDIS_SWITCH_FORWARDING_DESTINATION_ARRAY destinationPorts;
/* send flags while sending 'curNbl' into NDIS. */
ULONG sendFlags;
/* Total number of output ports, used + unused, in 'curNbl'. */
UINT32 destPortsSizeIn;
/* Total number of used output ports in 'curNbl'. */
UINT32 destPortsSizeOut;
/*
* If 'curNbl' is not owned by OVS, they need to be tracked, if they need to
* be freed/completed.
*/
OvsCompletionList *completionList;
/*
* vport number of 'curNbl' when it is passed from the PIF bridge to the INT
* bridge. ie. during tunneling on the Rx side.
*/
UINT32 srcVportNo;
/*
* Tunnel key:
* - specified in actions during tunneling Tx
* - extracted from an NBL during tunneling Rx
*/
OvsIPv4TunnelKey tunKey;
/*
* Tunneling - Tx:
* To store the output port, when it is a tunneled port. We don't foresee
* multiple tunneled ports as outport for any given NBL.
*/
POVS_VPORT_ENTRY tunnelTxNic;
/*
* Tunneling - Rx:
* Points to the Internal port on the PIF Bridge, if the packet needs to be
* de-tunneled.
*/
POVS_VPORT_ENTRY tunnelRxNic;
/* header information */
OVS_PACKET_HDR_INFO layers;
} OvsForwardingContext;
/*
* --------------------------------------------------------------------------
* OvsInitForwardingCtx --
* Function to init/re-init the 'ovsFwdCtx' context as the actions pipeline
* is being executed.
*
* Result:
* NDIS_STATUS_SUCCESS on success
* Other NDIS_STATUS upon failure. Upon failure, it is safe to call
* OvsCompleteNBLForwardingCtx(), since 'ovsFwdCtx' has been initialized
* enough for OvsCompleteNBLForwardingCtx() to do its work.
* --------------------------------------------------------------------------
*/
static __inline NDIS_STATUS
OvsInitForwardingCtx(OvsForwardingContext *ovsFwdCtx,
POVS_SWITCH_CONTEXT switchContext,
PNET_BUFFER_LIST curNbl,
UINT32 srcVportNo,
ULONG sendFlags,
PNDIS_SWITCH_FORWARDING_DETAIL_NET_BUFFER_LIST_INFO fwdDetail,
OvsCompletionList *completionList,
OVS_PACKET_HDR_INFO *layers,
BOOLEAN resetTunnelInfo)
{
ASSERT(ovsFwdCtx);
ASSERT(switchContext);
ASSERT(curNbl);
ASSERT(fwdDetail);
/*
* Set values for curNbl and switchContext so upon failures, we have enough
* information to do cleanup.
*/
ovsFwdCtx->curNbl = curNbl;
ovsFwdCtx->switchContext = switchContext;
ovsFwdCtx->completionList = completionList;
ovsFwdCtx->fwdDetail = fwdDetail;
if (fwdDetail->NumAvailableDestinations > 0) {
/*
* XXX: even though MSDN says GetNetBufferListDestinations() returns
* NDIS_STATUS, the header files say otherwise.
*/
switchContext->NdisSwitchHandlers.GetNetBufferListDestinations(
switchContext->NdisSwitchContext, curNbl,
&ovsFwdCtx->destinationPorts);
ASSERT(ovsFwdCtx->destinationPorts);
/* Ensure that none of the elements are consumed yet. */
ASSERT(ovsFwdCtx->destinationPorts->NumElements ==
fwdDetail->NumAvailableDestinations);
} else {
ovsFwdCtx->destinationPorts = NULL;
}
ovsFwdCtx->destPortsSizeIn = fwdDetail->NumAvailableDestinations;
ovsFwdCtx->destPortsSizeOut = 0;
ovsFwdCtx->srcVportNo = srcVportNo;
ovsFwdCtx->sendFlags = sendFlags;
if (layers) {
ovsFwdCtx->layers = *layers;
} else {
RtlZeroMemory(&ovsFwdCtx->layers, sizeof ovsFwdCtx->layers);
}
if (resetTunnelInfo) {
ovsFwdCtx->tunnelTxNic = NULL;
ovsFwdCtx->tunnelRxNic = NULL;
RtlZeroMemory(&ovsFwdCtx->tunKey, sizeof ovsFwdCtx->tunKey);
}
return NDIS_STATUS_SUCCESS;
}
/*
* --------------------------------------------------------------------------
* OvsDetectTunnelRxPkt --
* Utility function for an RX packet to detect its tunnel type.
*
* Result:
* True - if the tunnel type was detected.
* False - if not a tunnel packet or tunnel type not supported.
* --------------------------------------------------------------------------
*/
static __inline BOOLEAN
OvsDetectTunnelRxPkt(OvsForwardingContext *ovsFwdCtx,
const OvsFlowKey *flowKey)
{
POVS_VPORT_ENTRY tunnelVport = NULL;
/* XXX: we should also check for the length of the UDP payload to pick
* packets only if they are at least VXLAN header size.
*/
if (!flowKey->ipKey.nwFrag &&
flowKey->ipKey.nwProto == IPPROTO_UDP) {
UINT16 dstPort = ntohs(flowKey->ipKey.l4.tpDst);
tunnelVport = OvsFindTunnelVportByDstPort(ovsFwdCtx->switchContext,
dstPort,
OVS_VPORT_TYPE_VXLAN);
if (tunnelVport) {
ovsActionStats.rxVxlan++;
}
} else if (!flowKey->ipKey.nwFrag &&
flowKey->ipKey.nwProto == IPPROTO_TCP) {
UINT16 dstPort = htons(flowKey->ipKey.l4.tpDst);
tunnelVport = OvsFindTunnelVportByDstPort(ovsFwdCtx->switchContext,
dstPort,
OVS_VPORT_TYPE_STT);
if (tunnelVport) {
ovsActionStats.rxStt++;
}
}
// We might get tunnel packets even before the tunnel gets initialized.
if (tunnelVport) {
ASSERT(ovsFwdCtx->tunnelRxNic == NULL);
ovsFwdCtx->tunnelRxNic = tunnelVport;
return TRUE;
}
return FALSE;
}
/*
* --------------------------------------------------------------------------
* OvsDetectTunnelPkt --
* Utility function to detect if a packet is to be subjected to
* tunneling (Tx) or de-tunneling (Rx). Various factors such as source
* port, destination port, packet contents, and previously setup tunnel
* context are used.
*
* Result:
* True - If the packet is to be subjected to tunneling.
* In case of invalid tunnel context, the tunneling functionality is
* a no-op and is completed within this function itself by consuming
* all of the tunneling context.
* False - If not a tunnel packet or tunnel type not supported. Caller should
* process the packet as a non-tunnel packet.
* --------------------------------------------------------------------------
*/
static __inline BOOLEAN
OvsDetectTunnelPkt(OvsForwardingContext *ovsFwdCtx,
const POVS_VPORT_ENTRY dstVport,
const OvsFlowKey *flowKey)
{
if (OvsIsInternalVportType(dstVport->ovsType)) {
/*
* Rx:
* The source of NBL during tunneling Rx could be the external
* port or if it is being executed from userspace, the source port is
* default port.
*/
BOOLEAN validSrcPort =
(ovsFwdCtx->fwdDetail->SourcePortId ==
ovsFwdCtx->switchContext->virtualExternalPortId) ||
(ovsFwdCtx->fwdDetail->SourcePortId ==
NDIS_SWITCH_DEFAULT_PORT_ID);
if (validSrcPort && OvsDetectTunnelRxPkt(ovsFwdCtx, flowKey)) {
ASSERT(ovsFwdCtx->tunnelTxNic == NULL);
ASSERT(ovsFwdCtx->tunnelRxNic != NULL);
return TRUE;
}
} else if (OvsIsTunnelVportType(dstVport->ovsType)) {
ASSERT(ovsFwdCtx->tunnelTxNic == NULL);
ASSERT(ovsFwdCtx->tunnelRxNic == NULL);
/*
* Tx:
* The destination port is a tunnel port. Encapsulation must be
* performed only on packets that originate from:
* - a VIF port
* - a bridge-internal port (packets generated from userspace)
* - no port.
*
* If the packet will not be encapsulated, consume the tunnel context
* by clearing it.
*/
if (ovsFwdCtx->srcVportNo != OVS_DEFAULT_PORT_NO) {
POVS_VPORT_ENTRY vport = OvsFindVportByPortNo(
ovsFwdCtx->switchContext, ovsFwdCtx->srcVportNo);
if (!vport ||
(vport->ovsType != OVS_VPORT_TYPE_NETDEV &&
!OvsIsBridgeInternalVport(vport))) {
ovsFwdCtx->tunKey.dst = 0;
}
}
/* Tunnel the packet only if tunnel context is set. */
if (ovsFwdCtx->tunKey.dst != 0) {
switch(dstVport->ovsType) {
case OVS_VPORT_TYPE_VXLAN:
ovsActionStats.txVxlan++;
break;
case OVS_VPORT_TYPE_STT:
ovsActionStats.txStt++;
break;
}
ovsFwdCtx->tunnelTxNic = dstVport;
}
return TRUE;
}
return FALSE;
}
/*
* --------------------------------------------------------------------------
* OvsAddPorts --
* Add the specified destination vport into the forwarding context. If the
* vport is a VIF/external port, it is added directly to the NBL. If it is
* a tunneling port, it is NOT added to the NBL.
*
* Result:
* NDIS_STATUS_SUCCESS on success
* Other NDIS_STATUS upon failure.
* --------------------------------------------------------------------------
*/
static __inline NDIS_STATUS
OvsAddPorts(OvsForwardingContext *ovsFwdCtx,
OvsFlowKey *flowKey,
NDIS_SWITCH_PORT_ID dstPortId,
BOOLEAN preserveVLAN,
BOOLEAN preservePriority)
{
POVS_VPORT_ENTRY vport;
PNDIS_SWITCH_PORT_DESTINATION fwdPort;
NDIS_STATUS status;
POVS_SWITCH_CONTEXT switchContext = ovsFwdCtx->switchContext;
/*
* We hold the dispatch lock that protects the list of vports, so vports
* validated here can be added as destinations safely before we call into
* NDIS.
*
* Some of the vports can be tunnelled ports as well in which case
* they should be added to a separate list of tunnelled destination ports
* instead of the VIF ports. The context for the tunnel is settable
* in OvsForwardingContext.
*/
vport = OvsFindVportByPortNo(ovsFwdCtx->switchContext, dstPortId);
if (vport == NULL || vport->ovsState != OVS_STATE_CONNECTED) {
/*
* There may be some latency between a port disappearing, and userspace
* updating the recalculated flows. In the meantime, handle invalid
* ports gracefully.
*/
ovsActionStats.noVport++;
return NDIS_STATUS_SUCCESS;
}
ASSERT(vport->nicState == NdisSwitchNicStateConnected);
vport->stats.txPackets++;
vport->stats.txBytes +=
NET_BUFFER_DATA_LENGTH(NET_BUFFER_LIST_FIRST_NB(ovsFwdCtx->curNbl));
if (OvsIsBridgeInternalVport(vport)) {
return NDIS_STATUS_SUCCESS;
}
if (OvsDetectTunnelPkt(ovsFwdCtx, vport, flowKey)) {
return NDIS_STATUS_SUCCESS;
}
if (ovsFwdCtx->destPortsSizeOut == ovsFwdCtx->destPortsSizeIn) {
if (ovsFwdCtx->destPortsSizeIn == 0) {
ASSERT(ovsFwdCtx->destinationPorts == NULL);
ASSERT(ovsFwdCtx->fwdDetail->NumAvailableDestinations == 0);
status =
switchContext->NdisSwitchHandlers.GrowNetBufferListDestinations(
switchContext->NdisSwitchContext, ovsFwdCtx->curNbl,
OVS_DEST_PORTS_ARRAY_MIN_SIZE,
&ovsFwdCtx->destinationPorts);
if (status != NDIS_STATUS_SUCCESS) {
ovsActionStats.cannotGrowDest++;
return status;
}
ovsFwdCtx->destPortsSizeIn =
ovsFwdCtx->fwdDetail->NumAvailableDestinations;
ASSERT(ovsFwdCtx->destinationPorts);
} else {
ASSERT(ovsFwdCtx->destinationPorts != NULL);
/*
* NumElements:
* A ULONG value that specifies the total number of
* NDIS_SWITCH_PORT_DESTINATION elements in the
* NDIS_SWITCH_FORWARDING_DESTINATION_ARRAY structure.
*
* NumDestinations:
* A ULONG value that specifies the number of
* NDIS_SWITCH_PORT_DESTINATION elements in the
* NDIS_SWITCH_FORWARDING_DESTINATION_ARRAY structure that
* specify port destinations.
*
* NumAvailableDestinations:
* A value that specifies the number of unused extensible switch
* destination ports elements within an NET_BUFFER_LIST structure.
*/
ASSERT(ovsFwdCtx->destinationPorts->NumElements ==
ovsFwdCtx->destPortsSizeIn);
ASSERT(ovsFwdCtx->destinationPorts->NumDestinations ==
ovsFwdCtx->destPortsSizeOut -
ovsFwdCtx->fwdDetail->NumAvailableDestinations);
ASSERT(ovsFwdCtx->fwdDetail->NumAvailableDestinations > 0);
/*
* Before we grow the array of destination ports, the current set
* of ports needs to be committed. Only the ports added since the
* last commit need to be part of the new update.
*/
status = switchContext->NdisSwitchHandlers.UpdateNetBufferListDestinations(
switchContext->NdisSwitchContext, ovsFwdCtx->curNbl,
ovsFwdCtx->fwdDetail->NumAvailableDestinations,
ovsFwdCtx->destinationPorts);
if (status != NDIS_STATUS_SUCCESS) {
ovsActionStats.cannotGrowDest++;
return status;
}
ASSERT(ovsFwdCtx->destinationPorts->NumElements ==
ovsFwdCtx->destPortsSizeIn);
ASSERT(ovsFwdCtx->destinationPorts->NumDestinations ==
ovsFwdCtx->destPortsSizeOut);
ASSERT(ovsFwdCtx->fwdDetail->NumAvailableDestinations == 0);
status = switchContext->NdisSwitchHandlers.GrowNetBufferListDestinations(
switchContext->NdisSwitchContext, ovsFwdCtx->curNbl,
ovsFwdCtx->destPortsSizeIn, &ovsFwdCtx->destinationPorts);
if (status != NDIS_STATUS_SUCCESS) {
ovsActionStats.cannotGrowDest++;
return status;
}
ASSERT(ovsFwdCtx->destinationPorts != NULL);
ovsFwdCtx->destPortsSizeIn <<= 1;
}
}
ASSERT(ovsFwdCtx->destPortsSizeOut < ovsFwdCtx->destPortsSizeIn);
fwdPort =
NDIS_SWITCH_PORT_DESTINATION_AT_ARRAY_INDEX(ovsFwdCtx->destinationPorts,
ovsFwdCtx->destPortsSizeOut);
fwdPort->PortId = vport->portId;
fwdPort->NicIndex = vport->nicIndex;
fwdPort->IsExcluded = 0;
fwdPort->PreserveVLAN = preserveVLAN;
fwdPort->PreservePriority = preservePriority;
ovsFwdCtx->destPortsSizeOut += 1;
return NDIS_STATUS_SUCCESS;
}
/*
* --------------------------------------------------------------------------
* OvsClearTunTxCtx --
* Utility function to clear tx tunneling context.
* --------------------------------------------------------------------------
*/
static __inline VOID
OvsClearTunTxCtx(OvsForwardingContext *ovsFwdCtx)
{
ovsFwdCtx->tunnelTxNic = NULL;
ovsFwdCtx->tunKey.dst = 0;
}
/*
* --------------------------------------------------------------------------
* OvsClearTunRxCtx --
* Utility function to clear rx tunneling context.
* --------------------------------------------------------------------------
*/
static __inline VOID
OvsClearTunRxCtx(OvsForwardingContext *ovsFwdCtx)
{
ovsFwdCtx->tunnelRxNic = NULL;
ovsFwdCtx->tunKey.dst = 0;
}
/*
* --------------------------------------------------------------------------
* OvsCompleteNBLForwardingCtx --
* This utility function is responsible for freeing/completing an NBL - either
* by adding it to a completion list or by freeing it.
*
* Side effects:
* It also resets the necessary fields in 'ovsFwdCtx'.
* --------------------------------------------------------------------------
*/
static __inline VOID
OvsCompleteNBLForwardingCtx(OvsForwardingContext *ovsFwdCtx,
PCWSTR dropReason)
{
NDIS_STRING filterReason;
RtlInitUnicodeString(&filterReason, dropReason);
if (ovsFwdCtx->completionList) {
OvsAddPktCompletionList(ovsFwdCtx->completionList, TRUE,
ovsFwdCtx->fwdDetail->SourcePortId, ovsFwdCtx->curNbl, 1,
&filterReason);
ovsFwdCtx->curNbl = NULL;
} else {
/* If there is no completionList, we assume this is ovs created NBL */
ovsFwdCtx->curNbl = OvsCompleteNBL(ovsFwdCtx->switchContext,
ovsFwdCtx->curNbl, TRUE);
ASSERT(ovsFwdCtx->curNbl == NULL);
}
/* XXX: these can be made debug only to save cycles. Ideally the pipeline
* using these fields should reset the values at the end of the pipeline. */
ovsFwdCtx->destPortsSizeOut = 0;
ovsFwdCtx->tunnelTxNic = NULL;
ovsFwdCtx->tunnelRxNic = NULL;
}
/*
* --------------------------------------------------------------------------
* OvsDoFlowLookupOutput --
* Function to be used for the second stage of a tunneling workflow, ie.:
* - On the encapsulated packet on Tx path, to do a flow extract, flow
* lookup and excuting the actions.
* - On the decapsulated packet on Rx path, to do a flow extract, flow
* lookup and excuting the actions.
*
* XXX: It is assumed that the NBL in 'ovsFwdCtx' is owned by OVS. This is
* until the new buffer management framework is adopted.
*
* Side effects:
* The NBL in 'ovsFwdCtx' is consumed.
* --------------------------------------------------------------------------
*/
static __inline NDIS_STATUS
OvsDoFlowLookupOutput(OvsForwardingContext *ovsFwdCtx)
{
OvsFlowKey key;
OvsFlow *flow;
UINT64 hash;
NDIS_STATUS status;
POVS_VPORT_ENTRY vport =
OvsFindVportByPortNo(ovsFwdCtx->switchContext, ovsFwdCtx->srcVportNo);
if (vport == NULL || vport->ovsState != OVS_STATE_CONNECTED) {
ASSERT(FALSE); // XXX: let's catch this for now
OvsCompleteNBLForwardingCtx(ovsFwdCtx,
L"OVS-Dropped due to internal/tunnel port removal");
ovsActionStats.noVport++;
return NDIS_STATUS_SUCCESS;
}
ASSERT(vport->nicState == NdisSwitchNicStateConnected);
/* Assert that in the Rx direction, key is always setup. */
ASSERT(ovsFwdCtx->tunnelRxNic == NULL || ovsFwdCtx->tunKey.dst != 0);
status =
OvsExtractFlow(ovsFwdCtx->curNbl, ovsFwdCtx->srcVportNo,
&key, &ovsFwdCtx->layers,
ovsFwdCtx->tunKey.dst != 0 ? &ovsFwdCtx->tunKey : NULL);
if (status != NDIS_STATUS_SUCCESS) {
OvsCompleteNBLForwardingCtx(ovsFwdCtx,
L"OVS-Flow extract failed");
ovsActionStats.failedFlowExtract++;
return status;
}
flow = OvsLookupFlow(&ovsFwdCtx->switchContext->datapath, &key, &hash, FALSE);
if (flow) {
OvsFlowUsed(flow, ovsFwdCtx->curNbl, &ovsFwdCtx->layers);
ovsFwdCtx->switchContext->datapath.hits++;
status = OvsActionsExecute(ovsFwdCtx->switchContext,
ovsFwdCtx->completionList, ovsFwdCtx->curNbl,
ovsFwdCtx->srcVportNo, ovsFwdCtx->sendFlags,
&key, &hash, &ovsFwdCtx->layers,
flow->actions, flow->actionsLen);
ovsFwdCtx->curNbl = NULL;
} else {
LIST_ENTRY missedPackets;
UINT32 num = 0;
ovsFwdCtx->switchContext->datapath.misses++;
InitializeListHead(&missedPackets);
status = OvsCreateAndAddPackets(NULL, 0, OVS_PACKET_CMD_MISS, vport,
&key,ovsFwdCtx->curNbl,
ovsFwdCtx->tunnelRxNic != NULL, &ovsFwdCtx->layers,
ovsFwdCtx->switchContext, &missedPackets, &num);
if (num) {
OvsQueuePackets(&missedPackets, num);
}
if (status == NDIS_STATUS_SUCCESS) {
/* Complete the packet since it was copied to user buffer. */
OvsCompleteNBLForwardingCtx(ovsFwdCtx,
L"OVS-Dropped since packet was copied to userspace");
ovsActionStats.flowMiss++;
status = NDIS_STATUS_SUCCESS;
} else {
OvsCompleteNBLForwardingCtx(ovsFwdCtx,
L"OVS-Dropped due to failure to queue to userspace");
status = NDIS_STATUS_FAILURE;
ovsActionStats.failedFlowMiss++;
}
}
return status;
}
/*
* --------------------------------------------------------------------------
* OvsTunnelPortTx --
* The start function for Tx tunneling - encapsulates the packet, and
* outputs the packet on the PIF bridge.
*
* Side effects:
* The NBL in 'ovsFwdCtx' is consumed.
* --------------------------------------------------------------------------
*/
static __inline NDIS_STATUS
OvsTunnelPortTx(OvsForwardingContext *ovsFwdCtx)
{
NDIS_STATUS status = NDIS_STATUS_FAILURE;
PNET_BUFFER_LIST newNbl = NULL;
/*
* Setup the source port to be the internal port to as to facilitate the
* second OvsLookupFlow.
*/
if (ovsFwdCtx->switchContext->internalVport == NULL ||
ovsFwdCtx->switchContext->virtualExternalVport == NULL) {
OvsClearTunTxCtx(ovsFwdCtx);
OvsCompleteNBLForwardingCtx(ovsFwdCtx,
L"OVS-Dropped since either internal or external port is absent");
return NDIS_STATUS_FAILURE;
}
ovsFwdCtx->srcVportNo =
((POVS_VPORT_ENTRY)ovsFwdCtx->switchContext->internalVport)->portNo;
ovsFwdCtx->fwdDetail->SourcePortId = ovsFwdCtx->switchContext->internalPortId;
ovsFwdCtx->fwdDetail->SourceNicIndex =
((POVS_VPORT_ENTRY)ovsFwdCtx->switchContext->internalVport)->nicIndex;
/* Do the encap. Encap function does not consume the NBL. */
switch(ovsFwdCtx->tunnelTxNic->ovsType) {
case OVS_VPORT_TYPE_VXLAN:
status = OvsEncapVxlan(ovsFwdCtx->tunnelTxNic, ovsFwdCtx->curNbl,
&ovsFwdCtx->tunKey, ovsFwdCtx->switchContext,
&ovsFwdCtx->layers, &newNbl);
break;
case OVS_VPORT_TYPE_STT:
status = OvsEncapStt(ovsFwdCtx->tunnelTxNic, ovsFwdCtx->curNbl,
&ovsFwdCtx->tunKey, ovsFwdCtx->switchContext,
&ovsFwdCtx->layers, &newNbl);
break;
default:
ASSERT(! "Tx: Unhandled tunnel type");
}
/* Reset the tunnel context so that it doesn't get used after this point. */
OvsClearTunTxCtx(ovsFwdCtx);
if (status == NDIS_STATUS_SUCCESS) {
ASSERT(newNbl);
OvsCompleteNBLForwardingCtx(ovsFwdCtx,
L"Complete after cloning NBL for encapsulation");
ovsFwdCtx->curNbl = newNbl;
status = OvsDoFlowLookupOutput(ovsFwdCtx);
ASSERT(ovsFwdCtx->curNbl == NULL);
} else {
/*
* XXX: Temporary freeing of the packet until we register a
* callback to IP helper.
*/
OvsCompleteNBLForwardingCtx(ovsFwdCtx,
L"OVS-Dropped due to encap failure");
ovsActionStats.failedEncap++;
status = NDIS_STATUS_SUCCESS;
}
return status;
}
/*
* --------------------------------------------------------------------------
* OvsTunnelPortRx --
* Decapsulate the incoming NBL based on the tunnel type and goes through
* the flow lookup for the inner packet.
*
* Note: IP checksum is validate here, but L4 checksum validation needs
* to be done by the corresponding tunnel types.
*
* Side effects:
* The NBL in 'ovsFwdCtx' is consumed.
* --------------------------------------------------------------------------
*/
static __inline NDIS_STATUS
OvsTunnelPortRx(OvsForwardingContext *ovsFwdCtx)
{
NDIS_STATUS status = NDIS_STATUS_SUCCESS;
PNET_BUFFER_LIST newNbl = NULL;
POVS_VPORT_ENTRY tunnelRxVport = ovsFwdCtx->tunnelRxNic;
if (OvsValidateIPChecksum(ovsFwdCtx->curNbl, &ovsFwdCtx->layers)
!= NDIS_STATUS_SUCCESS) {
ovsActionStats.failedChecksum++;
OVS_LOG_INFO("Packet dropped due to IP checksum failure.");
goto dropNbl;
}
/*
* Decap port functions should return a new NBL if it was copied, and
* this new NBL should be setup as the ovsFwdCtx->curNbl.
*/
switch(tunnelRxVport->ovsType) {
case OVS_VPORT_TYPE_VXLAN:
status = OvsDecapVxlan(ovsFwdCtx->switchContext, ovsFwdCtx->curNbl,
&ovsFwdCtx->tunKey, &newNbl);
break;
case OVS_VPORT_TYPE_STT:
status = OvsDecapStt(ovsFwdCtx->switchContext, ovsFwdCtx->curNbl,
&ovsFwdCtx->tunKey, &newNbl);
break;
default:
OVS_LOG_ERROR("Rx: Unhandled tunnel type: %d\n",
tunnelRxVport->ovsType);
ASSERT(! "Rx: Unhandled tunnel type");
status = NDIS_STATUS_NOT_SUPPORTED;
}
if (status != NDIS_STATUS_SUCCESS) {
ovsActionStats.failedDecap++;
goto dropNbl;
}
/*
* tunnelRxNic and other fields will be cleared, re-init the context
* before usage.
*/
OvsCompleteNBLForwardingCtx(ovsFwdCtx,
L"OVS-dropped due to new decap packet");
/* Decapsulated packet is in a new NBL */
ovsFwdCtx->tunnelRxNic = tunnelRxVport;
OvsInitForwardingCtx(ovsFwdCtx, ovsFwdCtx->switchContext,
newNbl, tunnelRxVport->portNo, 0,
NET_BUFFER_LIST_SWITCH_FORWARDING_DETAIL(newNbl),
ovsFwdCtx->completionList,
&ovsFwdCtx->layers, FALSE);
/*
* Set the NBL's SourcePortId and SourceNicIndex to default values to
* keep NDIS happy when we forward the packet.
*/
ovsFwdCtx->fwdDetail->SourcePortId = NDIS_SWITCH_DEFAULT_PORT_ID;
ovsFwdCtx->fwdDetail->SourceNicIndex = 0;
status = OvsDoFlowLookupOutput(ovsFwdCtx);
ASSERT(ovsFwdCtx->curNbl == NULL);
OvsClearTunRxCtx(ovsFwdCtx);
return status;
dropNbl:
OvsCompleteNBLForwardingCtx(ovsFwdCtx,
L"OVS-dropped due to decap failure");
OvsClearTunRxCtx(ovsFwdCtx);
return status;
}
/*
* --------------------------------------------------------------------------
* OvsOutputForwardingCtx --
* This function outputs an NBL to NDIS or to a tunneling pipeline based on
* the ports added so far into 'ovsFwdCtx'.
*
* Side effects:
* This function consumes the NBL - either by forwarding it successfully to
* NDIS, or adding it to the completion list in 'ovsFwdCtx', or freeing it.
*
* Also makes sure that the list of destination ports - tunnel or otherwise is
* drained.
* --------------------------------------------------------------------------
*/
static __inline NDIS_STATUS
OvsOutputForwardingCtx(OvsForwardingContext *ovsFwdCtx)
{
NDIS_STATUS status = STATUS_SUCCESS;
POVS_SWITCH_CONTEXT switchContext = ovsFwdCtx->switchContext;
PCWSTR dropReason;
/*
* Handle the case where the some of the destination ports are tunneled
* ports - the non-tunneled ports get a unmodified copy of the NBL, and the
* tunneling pipeline starts when we output the packet to tunneled port.
*/
if (ovsFwdCtx->destPortsSizeOut > 0) {
PNET_BUFFER_LIST newNbl = NULL;
PNET_BUFFER nb;
UINT32 portsToUpdate =
ovsFwdCtx->fwdDetail->NumAvailableDestinations -
(ovsFwdCtx->destPortsSizeIn - ovsFwdCtx->destPortsSizeOut);
ASSERT(ovsFwdCtx->destinationPorts != NULL);
/*
* Create a copy of the packet in order to do encap on it later. Also,
* don't copy the offload context since the encap'd packet has a
* different set of headers. This will change when we implement offloads
* before doing encapsulation.
*/
if (ovsFwdCtx->tunnelTxNic != NULL || ovsFwdCtx->tunnelRxNic != NULL) {
nb = NET_BUFFER_LIST_FIRST_NB(ovsFwdCtx->curNbl);
newNbl = OvsPartialCopyNBL(ovsFwdCtx->switchContext, ovsFwdCtx->curNbl,
0, 0, TRUE /*copy NBL info*/);
if (newNbl == NULL) {
status = NDIS_STATUS_RESOURCES;
ovsActionStats.noCopiedNbl++;
dropReason = L"Dropped due to failure to create NBL copy.";
goto dropit;
}
}
/* It does not seem like we'll get here unless 'portsToUpdate' > 0. */
ASSERT(portsToUpdate > 0);
status = switchContext->NdisSwitchHandlers.UpdateNetBufferListDestinations(
switchContext->NdisSwitchContext, ovsFwdCtx->curNbl,
portsToUpdate, ovsFwdCtx->destinationPorts);
if (status != NDIS_STATUS_SUCCESS) {
OvsCompleteNBL(ovsFwdCtx->switchContext, newNbl, TRUE);
ovsActionStats.cannotGrowDest++;
dropReason = L"Dropped due to failure to update destinations.";
goto dropit;
}
OvsSendNBLIngress(ovsFwdCtx->switchContext, ovsFwdCtx->curNbl,
ovsFwdCtx->sendFlags);
/* End this pipeline by resetting the corresponding context. */
ovsFwdCtx->destPortsSizeOut = 0;
ovsFwdCtx->curNbl = NULL;
if (newNbl) {
status = OvsInitForwardingCtx(ovsFwdCtx, ovsFwdCtx->switchContext,
newNbl, ovsFwdCtx->srcVportNo, 0,
NET_BUFFER_LIST_SWITCH_FORWARDING_DETAIL(newNbl),
ovsFwdCtx->completionList,
&ovsFwdCtx->layers, FALSE);
if (status != NDIS_STATUS_SUCCESS) {
dropReason = L"Dropped due to resouces.";
goto dropit;
}
}
}
if (ovsFwdCtx->tunnelTxNic != NULL) {
status = OvsTunnelPortTx(ovsFwdCtx);
ASSERT(ovsFwdCtx->tunnelTxNic == NULL);
ASSERT(ovsFwdCtx->tunKey.dst == 0);
} else if (ovsFwdCtx->tunnelRxNic != NULL) {
status = OvsTunnelPortRx(ovsFwdCtx);
ASSERT(ovsFwdCtx->tunnelRxNic == NULL);
ASSERT(ovsFwdCtx->tunKey.dst == 0);
}
ASSERT(ovsFwdCtx->curNbl == NULL);
return status;
dropit:
if (status != NDIS_STATUS_SUCCESS) {
OvsCompleteNBLForwardingCtx(ovsFwdCtx, dropReason);
}
return status;
}
/*
* --------------------------------------------------------------------------
* OvsLookupFlowOutput --
* Utility function for external callers to do flow extract, lookup,
* actions execute on a given NBL.
*
* Note: If this is being used from a callback function, make sure that the
* arguments specified are still valid in the asynchronous context.
*
* Side effects:
* This function consumes the NBL.
* --------------------------------------------------------------------------
*/
VOID
OvsLookupFlowOutput(POVS_SWITCH_CONTEXT switchContext,
VOID *compList,
PNET_BUFFER_LIST curNbl)
{
NDIS_STATUS status;
OvsForwardingContext ovsFwdCtx;
POVS_VPORT_ENTRY internalVport =
(POVS_VPORT_ENTRY)switchContext->internalVport;
/* XXX: make sure comp list was not a stack variable previously. */
OvsCompletionList *completionList = (OvsCompletionList *)compList;
/*
* XXX: can internal port disappear while we are busy doing ARP resolution?
* It could, but will we get this callback from IP helper in that case. Need
* to check.
*/
ASSERT(switchContext->internalVport);
status = OvsInitForwardingCtx(&ovsFwdCtx, switchContext, curNbl,
internalVport->portNo, 0,
NET_BUFFER_LIST_SWITCH_FORWARDING_DETAIL(curNbl),
completionList, NULL, TRUE);
if (status != NDIS_STATUS_SUCCESS) {
OvsCompleteNBLForwardingCtx(&ovsFwdCtx,
L"OVS-Dropped due to resources");
return;
}
ASSERT(FALSE);
/*
* XXX: We need to acquire the dispatch lock and the datapath lock.
*/
OvsDoFlowLookupOutput(&ovsFwdCtx);
}
/*
* --------------------------------------------------------------------------
* OvsOutputBeforeSetAction --
* Function to be called to complete one set of actions on an NBL, before
* we start the next one.
* --------------------------------------------------------------------------
*/
static __inline NDIS_STATUS
OvsOutputBeforeSetAction(OvsForwardingContext *ovsFwdCtx)
{
PNET_BUFFER_LIST newNbl;
NDIS_STATUS status = NDIS_STATUS_SUCCESS;
/*
* Create a copy and work on the copy after this point. The original NBL is
* forwarded. One reason to not use the copy for forwarding is that
* ports have already been added to the original NBL, and it might be
* inefficient/impossible to remove/re-add them to the copy. There's no
* notion of removing the ports, the ports need to be marked as
* "isExcluded". There's seems no real advantage to retaining the original
* and sending out the copy instead.
*
* XXX: We are copying the offload context here. This is to handle actions
* such as:
* outport, pop_vlan(), outport, push_vlan(), outport
*
* copy size needs to include inner ether + IP + TCP, need to revisit
* if we support IP options.
* XXX Head room needs to include the additional encap.
* XXX copySize check is not considering multiple NBs.
*/
newNbl = OvsPartialCopyNBL(ovsFwdCtx->switchContext, ovsFwdCtx->curNbl,
0, 0, TRUE /*copy NBL info*/);
ASSERT(ovsFwdCtx->destPortsSizeOut > 0 ||
ovsFwdCtx->tunnelTxNic != NULL || ovsFwdCtx->tunnelRxNic != NULL);
/* Send the original packet out and save the original source port number */
UINT32 tempVportNo = ovsFwdCtx->srcVportNo;
status = OvsOutputForwardingCtx(ovsFwdCtx);
ASSERT(ovsFwdCtx->curNbl == NULL);
ASSERT(ovsFwdCtx->destPortsSizeOut == 0);
ASSERT(ovsFwdCtx->tunnelRxNic == NULL);
ASSERT(ovsFwdCtx->tunnelTxNic == NULL);
/* If we didn't make a copy, can't continue. */
if (newNbl == NULL) {
ovsActionStats.noCopiedNbl++;
return NDIS_STATUS_RESOURCES;
}
/* Finish the remaining actions with the new NBL */
if (status != NDIS_STATUS_SUCCESS) {
OvsCompleteNBL(ovsFwdCtx->switchContext, newNbl, TRUE);
} else {
status = OvsInitForwardingCtx(ovsFwdCtx, ovsFwdCtx->switchContext,
newNbl, tempVportNo, 0,
NET_BUFFER_LIST_SWITCH_FORWARDING_DETAIL(newNbl),
ovsFwdCtx->completionList,
&ovsFwdCtx->layers, FALSE);
}
return status;