-
Notifications
You must be signed in to change notification settings - Fork 237
/
northd.c
16560 lines (14719 loc) · 634 KB
/
northd.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
/*
* 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 <config.h>
#include <stdlib.h>
#include <stdio.h>
#include "debug.h"
#include "bitmap.h"
#include "dirs.h"
#include "ipam.h"
#include "openvswitch/dynamic-string.h"
#include "hash.h"
#include "hmapx.h"
#include "openvswitch/hmap.h"
#include "openvswitch/json.h"
#include "ovn/lex.h"
#include "lib/chassis-index.h"
#include "lib/ip-mcast-index.h"
#include "lib/static-mac-binding-index.h"
#include "lib/copp.h"
#include "lib/mcast-group-index.h"
#include "lib/ovn-l7.h"
#include "lib/ovn-nb-idl.h"
#include "lib/ovn-sb-idl.h"
#include "lib/ovn-util.h"
#include "lib/lb.h"
#include "memory.h"
#include "northd.h"
#include "lib/ovn-parallel-hmap.h"
#include "ovn/actions.h"
#include "ovn/features.h"
#include "ovn/logical-fields.h"
#include "packets.h"
#include "simap.h"
#include "smap.h"
#include "sset.h"
#include "svec.h"
#include "stopwatch.h"
#include "lib/stopwatch-names.h"
#include "stream.h"
#include "timeval.h"
#include "util.h"
#include "uuid.h"
#include "ovs-thread.h"
#include "openvswitch/vlog.h"
VLOG_DEFINE_THIS_MODULE(northd);
static bool controller_event_en;
static bool lflow_hash_lock_initialized = false;
static bool check_lsp_is_up;
static bool install_ls_lb_from_router;
/* MAC allocated for service monitor usage. Just one mac is allocated
* for this purpose and ovn-controller's on each chassis will make use
* of this mac when sending out the packets to monitor the services
* defined in Service_Monitor Southbound table. Since these packets
* all locally handled, having just one mac is good enough. */
static char svc_monitor_mac[ETH_ADDR_STRLEN + 1];
static struct eth_addr svc_monitor_mac_ea;
/* If this option is 'true' northd will make use of ct.inv match fields.
* Otherwise, it will avoid using it. The default is true. */
static bool use_ct_inv_match = true;
/* If this option is 'true' northd will implicitly add a lowest-priority
* drop rule in the ACL stage of logical switches that have at least one
* ACL.
*/
static bool default_acl_drop;
#define MAX_OVN_TAGS 4096
/* Pipeline stages. */
/* The two purposes for which ovn-northd uses OVN logical datapaths. */
enum ovn_datapath_type {
DP_SWITCH, /* OVN logical switch. */
DP_ROUTER /* OVN logical router. */
};
/* Returns an "enum ovn_stage" built from the arguments.
*
* (It's better to use ovn_stage_build() for type-safety reasons, but inline
* functions can't be used in enums or switch cases.) */
#define OVN_STAGE_BUILD(DP_TYPE, PIPELINE, TABLE) \
(((DP_TYPE) << 9) | ((PIPELINE) << 8) | (TABLE))
/* A stage within an OVN logical switch or router.
*
* An "enum ovn_stage" indicates whether the stage is part of a logical switch
* or router, whether the stage is part of the ingress or egress pipeline, and
* the table within that pipeline. The first three components are combined to
* form the stage's full name, e.g. S_SWITCH_IN_PORT_SEC_L2,
* S_ROUTER_OUT_DELIVERY. */
enum ovn_stage {
#define PIPELINE_STAGES \
/* Logical switch ingress stages. */ \
PIPELINE_STAGE(SWITCH, IN, CHECK_PORT_SEC, 0, "ls_in_check_port_sec") \
PIPELINE_STAGE(SWITCH, IN, APPLY_PORT_SEC, 1, "ls_in_apply_port_sec") \
PIPELINE_STAGE(SWITCH, IN, LOOKUP_FDB , 2, "ls_in_lookup_fdb") \
PIPELINE_STAGE(SWITCH, IN, PUT_FDB, 3, "ls_in_put_fdb") \
PIPELINE_STAGE(SWITCH, IN, PRE_ACL, 4, "ls_in_pre_acl") \
PIPELINE_STAGE(SWITCH, IN, PRE_LB, 5, "ls_in_pre_lb") \
PIPELINE_STAGE(SWITCH, IN, PRE_STATEFUL, 6, "ls_in_pre_stateful") \
PIPELINE_STAGE(SWITCH, IN, ACL_HINT, 7, "ls_in_acl_hint") \
PIPELINE_STAGE(SWITCH, IN, ACL, 8, "ls_in_acl") \
PIPELINE_STAGE(SWITCH, IN, QOS_MARK, 9, "ls_in_qos_mark") \
PIPELINE_STAGE(SWITCH, IN, QOS_METER, 10, "ls_in_qos_meter") \
PIPELINE_STAGE(SWITCH, IN, LB_AFF_CHECK, 11, "ls_in_lb_aff_check") \
PIPELINE_STAGE(SWITCH, IN, LB, 12, "ls_in_lb") \
PIPELINE_STAGE(SWITCH, IN, LB_AFF_LEARN, 13, "ls_in_lb_aff_learn") \
PIPELINE_STAGE(SWITCH, IN, PRE_HAIRPIN, 14, "ls_in_pre_hairpin") \
PIPELINE_STAGE(SWITCH, IN, NAT_HAIRPIN, 15, "ls_in_nat_hairpin") \
PIPELINE_STAGE(SWITCH, IN, HAIRPIN, 16, "ls_in_hairpin") \
PIPELINE_STAGE(SWITCH, IN, ACL_AFTER_LB, 17, "ls_in_acl_after_lb") \
PIPELINE_STAGE(SWITCH, IN, STATEFUL, 18, "ls_in_stateful") \
PIPELINE_STAGE(SWITCH, IN, ARP_ND_RSP, 19, "ls_in_arp_rsp") \
PIPELINE_STAGE(SWITCH, IN, DHCP_OPTIONS, 20, "ls_in_dhcp_options") \
PIPELINE_STAGE(SWITCH, IN, DHCP_RESPONSE, 21, "ls_in_dhcp_response") \
PIPELINE_STAGE(SWITCH, IN, DNS_LOOKUP, 22, "ls_in_dns_lookup") \
PIPELINE_STAGE(SWITCH, IN, DNS_RESPONSE, 23, "ls_in_dns_response") \
PIPELINE_STAGE(SWITCH, IN, EXTERNAL_PORT, 24, "ls_in_external_port") \
PIPELINE_STAGE(SWITCH, IN, L2_LKUP, 25, "ls_in_l2_lkup") \
PIPELINE_STAGE(SWITCH, IN, L2_UNKNOWN, 26, "ls_in_l2_unknown") \
\
/* Logical switch egress stages. */ \
PIPELINE_STAGE(SWITCH, OUT, PRE_ACL, 0, "ls_out_pre_acl") \
PIPELINE_STAGE(SWITCH, OUT, PRE_LB, 1, "ls_out_pre_lb") \
PIPELINE_STAGE(SWITCH, OUT, PRE_STATEFUL, 2, "ls_out_pre_stateful") \
PIPELINE_STAGE(SWITCH, OUT, ACL_HINT, 3, "ls_out_acl_hint") \
PIPELINE_STAGE(SWITCH, OUT, ACL, 4, "ls_out_acl") \
PIPELINE_STAGE(SWITCH, OUT, QOS_MARK, 5, "ls_out_qos_mark") \
PIPELINE_STAGE(SWITCH, OUT, QOS_METER, 6, "ls_out_qos_meter") \
PIPELINE_STAGE(SWITCH, OUT, STATEFUL, 7, "ls_out_stateful") \
PIPELINE_STAGE(SWITCH, OUT, CHECK_PORT_SEC, 8, "ls_out_check_port_sec") \
PIPELINE_STAGE(SWITCH, OUT, APPLY_PORT_SEC, 9, "ls_out_apply_port_sec") \
\
/* Logical router ingress stages. */ \
PIPELINE_STAGE(ROUTER, IN, ADMISSION, 0, "lr_in_admission") \
PIPELINE_STAGE(ROUTER, IN, LOOKUP_NEIGHBOR, 1, "lr_in_lookup_neighbor") \
PIPELINE_STAGE(ROUTER, IN, LEARN_NEIGHBOR, 2, "lr_in_learn_neighbor") \
PIPELINE_STAGE(ROUTER, IN, IP_INPUT, 3, "lr_in_ip_input") \
PIPELINE_STAGE(ROUTER, IN, UNSNAT, 4, "lr_in_unsnat") \
PIPELINE_STAGE(ROUTER, IN, DEFRAG, 5, "lr_in_defrag") \
PIPELINE_STAGE(ROUTER, IN, LB_AFF_CHECK, 6, "lr_in_lb_aff_check") \
PIPELINE_STAGE(ROUTER, IN, DNAT, 7, "lr_in_dnat") \
PIPELINE_STAGE(ROUTER, IN, LB_AFF_LEARN, 8, "lr_in_lb_aff_learn") \
PIPELINE_STAGE(ROUTER, IN, ECMP_STATEFUL, 9, "lr_in_ecmp_stateful") \
PIPELINE_STAGE(ROUTER, IN, ND_RA_OPTIONS, 10, "lr_in_nd_ra_options") \
PIPELINE_STAGE(ROUTER, IN, ND_RA_RESPONSE, 11, "lr_in_nd_ra_response") \
PIPELINE_STAGE(ROUTER, IN, IP_ROUTING_PRE, 12, "lr_in_ip_routing_pre") \
PIPELINE_STAGE(ROUTER, IN, IP_ROUTING, 13, "lr_in_ip_routing") \
PIPELINE_STAGE(ROUTER, IN, IP_ROUTING_ECMP, 14, "lr_in_ip_routing_ecmp") \
PIPELINE_STAGE(ROUTER, IN, POLICY, 15, "lr_in_policy") \
PIPELINE_STAGE(ROUTER, IN, POLICY_ECMP, 16, "lr_in_policy_ecmp") \
PIPELINE_STAGE(ROUTER, IN, ARP_RESOLVE, 17, "lr_in_arp_resolve") \
PIPELINE_STAGE(ROUTER, IN, CHK_PKT_LEN, 18, "lr_in_chk_pkt_len") \
PIPELINE_STAGE(ROUTER, IN, LARGER_PKTS, 19, "lr_in_larger_pkts") \
PIPELINE_STAGE(ROUTER, IN, GW_REDIRECT, 20, "lr_in_gw_redirect") \
PIPELINE_STAGE(ROUTER, IN, ARP_REQUEST, 21, "lr_in_arp_request") \
\
/* Logical router egress stages. */ \
PIPELINE_STAGE(ROUTER, OUT, CHECK_DNAT_LOCAL, 0, \
"lr_out_chk_dnat_local") \
PIPELINE_STAGE(ROUTER, OUT, UNDNAT, 1, "lr_out_undnat") \
PIPELINE_STAGE(ROUTER, OUT, POST_UNDNAT, 2, "lr_out_post_undnat") \
PIPELINE_STAGE(ROUTER, OUT, SNAT, 3, "lr_out_snat") \
PIPELINE_STAGE(ROUTER, OUT, POST_SNAT, 4, "lr_out_post_snat") \
PIPELINE_STAGE(ROUTER, OUT, EGR_LOOP, 5, "lr_out_egr_loop") \
PIPELINE_STAGE(ROUTER, OUT, DELIVERY, 6, "lr_out_delivery")
#define PIPELINE_STAGE(DP_TYPE, PIPELINE, STAGE, TABLE, NAME) \
S_##DP_TYPE##_##PIPELINE##_##STAGE \
= OVN_STAGE_BUILD(DP_##DP_TYPE, P_##PIPELINE, TABLE),
PIPELINE_STAGES
#undef PIPELINE_STAGE
};
/* Due to various hard-coded priorities need to implement ACLs, the
* northbound database supports a smaller range of ACL priorities than
* are available to logical flows. This value is added to an ACL
* priority to determine the ACL's logical flow priority. */
#define OVN_ACL_PRI_OFFSET 1000
/* Register definitions specific to switches. */
#define REGBIT_CONNTRACK_DEFRAG "reg0[0]"
#define REGBIT_CONNTRACK_COMMIT "reg0[1]"
#define REGBIT_CONNTRACK_NAT "reg0[2]"
#define REGBIT_DHCP_OPTS_RESULT "reg0[3]"
#define REGBIT_DNS_LOOKUP_RESULT "reg0[4]"
#define REGBIT_ND_RA_OPTS_RESULT "reg0[5]"
#define REGBIT_HAIRPIN "reg0[6]"
#define REGBIT_ACL_HINT_ALLOW_NEW "reg0[7]"
#define REGBIT_ACL_HINT_ALLOW "reg0[8]"
#define REGBIT_ACL_HINT_DROP "reg0[9]"
#define REGBIT_ACL_HINT_BLOCK "reg0[10]"
#define REGBIT_LKUP_FDB "reg0[11]"
#define REGBIT_HAIRPIN_REPLY "reg0[12]"
#define REGBIT_ACL_LABEL "reg0[13]"
#define REGBIT_FROM_RAMP "reg0[14]"
#define REGBIT_PORT_SEC_DROP "reg0[15]"
#define REGBIT_ACL_STATELESS "reg0[16]"
#define REG_ORIG_DIP_IPV4 "reg1"
#define REG_ORIG_DIP_IPV6 "xxreg1"
#define REG_ORIG_TP_DPORT "reg2[0..15]"
/* Register used to store backend ipv6 address
* for load balancer affinity. */
#define REG_LB_L2_AFF_BACKEND_IP6 "xxreg0"
/* Register definitions for switches and routers. */
/* Register used to store backend ipv4 address
* for load balancer affinity. */
#define REG_LB_AFF_BACKEND_IP4 "reg4"
#define REG_LB_AFF_MATCH_PORT "reg8[0..15]"
/* Indicate that this packet has been recirculated using egress
* loopback. This allows certain checks to be bypassed, such as a
* logical router dropping packets with source IP address equals
* one of the logical router's own IP addresses. */
#define REGBIT_EGRESS_LOOPBACK "reg9[0]"
/* Register to store the result of check_pkt_larger action. */
#define REGBIT_PKT_LARGER "reg9[1]"
#define REGBIT_LOOKUP_NEIGHBOR_RESULT "reg9[2]"
#define REGBIT_LOOKUP_NEIGHBOR_IP_RESULT "reg9[3]"
#define REGBIT_DST_NAT_IP_LOCAL "reg9[4]"
#define REGBIT_KNOWN_ECMP_NH "reg9[5]"
#define REGBIT_KNOWN_LB_SESSION "reg9[6]"
/* Register to store the eth address associated to a router port for packets
* received in S_ROUTER_IN_ADMISSION.
*/
#define REG_INPORT_ETH_ADDR "xreg0[0..47]"
/* Register for ECMP bucket selection. */
#define REG_ECMP_GROUP_ID "reg8[0..15]"
#define REG_ECMP_MEMBER_ID "reg8[16..31]"
/* Registers used for routing. */
#define REG_NEXT_HOP_IPV4 "reg0"
#define REG_NEXT_HOP_IPV6 "xxreg0"
#define REG_SRC_IPV4 "reg1"
#define REG_SRC_IPV6 "xxreg1"
#define REG_ROUTE_TABLE_ID "reg7"
/* Register used to store backend ipv6 address
* for load balancer affinity. */
#define REG_LB_L3_AFF_BACKEND_IP6 "xxreg1"
#define REG_ORIG_TP_DPORT_ROUTER "reg9[16..31]"
/* Register used for setting a label for ACLs in a Logical Switch. */
#define REG_LABEL "reg3"
/* Register used for temporarily store ECMP eth.src to avoid masked ct_label
* access. It doesn't really occupy registers because the content of the
* register is saved to stack and then restored in the same flow.
* Note: the bits must match ct_label.ecmp_reply_eth defined in
* logical-fields.c */
#define REG_ECMP_ETH_FULL "xxreg1"
#define REG_ECMP_ETH_FIELD REG_ECMP_ETH_FULL "[" \
OVN_CT_STR(OVN_CT_ECMP_ETH_1ST_BIT) \
".." \
OVN_CT_STR(OVN_CT_ECMP_ETH_END_BIT) "]"
#define FLAGBIT_NOT_VXLAN "flags[1] == 0"
/*
* OVS register usage:
*
* Logical Switch pipeline:
* +----+----------------------------------------------+---+-----------------------------------+
* | R0 | REGBIT_{CONNTRACK/DHCP/DNS} | | |
* | | REGBIT_{HAIRPIN/HAIRPIN_REPLY} | | |
* | | REGBIT_ACL_HINT_{ALLOW_NEW/ALLOW/DROP/BLOCK} | | |
* | | REGBIT_ACL_{LABEL/STATELESS} | X | |
* +----+----------------------------------------------+ X | |
* | R5 | UNUSED | X | LB_L2_AFF_BACKEND_IP6 |
* | R1 | ORIG_DIP_IPV4 (>= IN_PRE_STATEFUL) | R | |
* +----+----------------------------------------------+ E | |
* | R2 | ORIG_TP_DPORT (>= IN_PRE_STATEFUL) | G | |
* +----+----------------------------------------------+ 0 | |
* | R3 | ACL LABEL | | |
* +----+----------------------------------------------+---+-----------------------------------+
* | R4 | REG_LB_AFF_BACKEND_IP4 | | |
* +----+----------------------------------------------+ X | |
* | R5 | UNUSED | X | ORIG_DIP_IPV6(>= IN_PRE_STATEFUL) |
* +----+----------------------------------------------+ R | |
* | R6 | UNUSED | E | |
* +----+----------------------------------------------+ G | |
* | R7 | UNUSED | 1 | |
* +----+----------------------------------------------+---+-----------------------------------+
* | R8 | LB_AFF_MATCH_PORT |
* +----+----------------------------------------------+
* | R9 | UNUSED |
* +----+----------------------------------------------+
*
* Logical Router pipeline:
* +-----+---------------------------+---+-----------------+---+------------------------------------+
* | R0 | REGBIT_ND_RA_OPTS_RESULT | | | | |
* | | (= IN_ND_RA_OPTIONS) | X | | | |
* | | NEXT_HOP_IPV4 | R | | | |
* | | (>= IP_INPUT) | E | INPORT_ETH_ADDR | X | |
* +-----+---------------------------+ G | (< IP_INPUT) | X | |
* | R1 | SRC_IPV4 for ARP-REQ | 0 | | R | |
* | | (>= IP_INPUT) | | | E | NEXT_HOP_IPV6 (>= DEFRAG ) |
* +-----+---------------------------+---+-----------------+ G | |
* | R2 | UNUSED | X | | 0 | |
* | | | R | | | |
* +-----+---------------------------+ E | UNUSED | | |
* | R3 | UNUSED | G | | | |
* | | | 1 | | | |
* +-----+---------------------------+---+-----------------+---+------------------------------------+
* | R4 | REG_LB_AFF_BACKEND_IP4 | X | | | |
* | | | R | | | |
* +-----+---------------------------+ E | UNUSED | X | |
* | R5 | UNUSED | G | | X | |
* | | | 2 | | R | LB_L3_AFF_BACKEND_IP6 |
* +-----+---------------------------+---+-----------------+ E | (<= IN_DNAT) |
* | R6 | UNUSED | X | | G | |
* | | | R | | 1 | |
* +-----+---------------------------+ E | UNUSED | | |
* | R7 | ROUTE_TABLE_ID | G | | | |
* | | (>= IN_IP_ROUTING_PRE && | 3 | | | |
* | | <= IN_IP_ROUTING) | | | | |
* +-----+---------------------------+---+-----------------+---+------------------------------------+
* | R8 | ECMP_GROUP_ID | | |
* | | ECMP_MEMBER_ID | | |
* | | LB_AFF_MATCH_PORT | X | |
* +-----+---------------------------+ R | |
* | | REGBIT_{ | E | |
* | | EGRESS_LOOPBACK/ | G | UNUSED |
* | R9 | PKT_LARGER/ | 4 | |
* | | LOOKUP_NEIGHBOR_RESULT/ | | |
* | | SKIP_LOOKUP_NEIGHBOR/ | | |
* | | KNOWN_ECMP_NH} | | |
* | | | | |
* | | REG_ORIG_TP_DPORT_ROUTER | | |
* | | | | |
* +-----+---------------------------+---+-----------------+
*
*/
/*
* Route offsets implement logic to prioritize traffic for routes with
* same ip_prefix values:
* - connected route overrides static one;
* - static route overrides connected route. */
#define ROUTE_PRIO_OFFSET_MULTIPLIER 3
#define ROUTE_PRIO_OFFSET_STATIC 1
#define ROUTE_PRIO_OFFSET_CONNECTED 2
/* Returns an "enum ovn_stage" built from the arguments. */
static enum ovn_stage
ovn_stage_build(enum ovn_datapath_type dp_type, enum ovn_pipeline pipeline,
uint8_t table)
{
return OVN_STAGE_BUILD(dp_type, pipeline, table);
}
/* Returns the pipeline to which 'stage' belongs. */
static enum ovn_pipeline
ovn_stage_get_pipeline(enum ovn_stage stage)
{
return (stage >> 8) & 1;
}
/* Returns the pipeline name to which 'stage' belongs. */
static const char *
ovn_stage_get_pipeline_name(enum ovn_stage stage)
{
return ovn_stage_get_pipeline(stage) == P_IN ? "ingress" : "egress";
}
/* Returns the table to which 'stage' belongs. */
static uint8_t
ovn_stage_get_table(enum ovn_stage stage)
{
return stage & 0xff;
}
/* Returns a string name for 'stage'. */
static const char *
ovn_stage_to_str(enum ovn_stage stage)
{
switch (stage) {
#define PIPELINE_STAGE(DP_TYPE, PIPELINE, STAGE, TABLE, NAME) \
case S_##DP_TYPE##_##PIPELINE##_##STAGE: return NAME;
PIPELINE_STAGES
#undef PIPELINE_STAGE
default: return "<unknown>";
}
}
/* Returns the type of the datapath to which a flow with the given 'stage' may
* be added. */
static enum ovn_datapath_type
ovn_stage_to_datapath_type(enum ovn_stage stage)
{
switch (stage) {
#define PIPELINE_STAGE(DP_TYPE, PIPELINE, STAGE, TABLE, NAME) \
case S_##DP_TYPE##_##PIPELINE##_##STAGE: return DP_##DP_TYPE;
PIPELINE_STAGES
#undef PIPELINE_STAGE
default: OVS_NOT_REACHED();
}
}
static void
build_chassis_features(const struct northd_input *input_data,
struct chassis_features *chassis_features)
{
const struct sbrec_chassis *chassis;
SBREC_CHASSIS_TABLE_FOR_EACH (chassis, input_data->sbrec_chassis) {
bool ct_no_masked_label =
smap_get_bool(&chassis->other_config,
OVN_FEATURE_CT_NO_MASKED_LABEL,
false);
if (!ct_no_masked_label && chassis_features->ct_no_masked_label) {
chassis_features->ct_no_masked_label = false;
}
bool mac_binding_timestamp =
smap_get_bool(&chassis->other_config,
OVN_FEATURE_MAC_BINDING_TIMESTAMP,
false);
if (!mac_binding_timestamp &&
chassis_features->mac_binding_timestamp) {
chassis_features->mac_binding_timestamp = false;
}
bool ct_lb_related =
smap_get_bool(&chassis->other_config,
OVN_FEATURE_CT_LB_RELATED,
false);
if (!ct_lb_related &&
chassis_features->ct_lb_related) {
chassis_features->ct_lb_related = false;
}
}
}
struct ovn_chassis_qdisc_queues {
struct hmap_node key_node;
uint32_t queue_id;
struct uuid chassis_uuid;
};
static uint32_t
hash_chassis_queue(const struct uuid *chassis_uuid, uint32_t queue_id)
{
return hash_2words(uuid_hash(chassis_uuid), queue_id);
}
static void
destroy_chassis_queues(struct hmap *set)
{
struct ovn_chassis_qdisc_queues *node;
HMAP_FOR_EACH_POP (node, key_node, set) {
free(node);
}
hmap_destroy(set);
}
static void
add_chassis_queue(struct hmap *set, const struct uuid *chassis_uuid,
uint32_t queue_id)
{
struct ovn_chassis_qdisc_queues *node = xmalloc(sizeof *node);
node->queue_id = queue_id;
node->chassis_uuid = *chassis_uuid;
hmap_insert(set, &node->key_node,
hash_chassis_queue(chassis_uuid, queue_id));
}
static bool
chassis_queueid_in_use(const struct hmap *set, const struct uuid *chassis_uuid,
uint32_t queue_id)
{
const struct ovn_chassis_qdisc_queues *node;
HMAP_FOR_EACH_WITH_HASH (node, key_node,
hash_chassis_queue(chassis_uuid, queue_id), set) {
if (uuid_equals(chassis_uuid, &node->chassis_uuid)
&& node->queue_id == queue_id) {
return true;
}
}
return false;
}
static uint32_t
allocate_chassis_queueid(struct hmap *set, const struct uuid *uuid, char *name)
{
if (!uuid) {
return 0;
}
for (uint32_t queue_id = QDISC_MIN_QUEUE_ID + 1;
queue_id <= QDISC_MAX_QUEUE_ID;
queue_id++) {
if (!chassis_queueid_in_use(set, uuid, queue_id)) {
add_chassis_queue(set, uuid, queue_id);
return queue_id;
}
}
static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
VLOG_WARN_RL(&rl, "all %s queue ids exhausted", name);
return 0;
}
static void
free_chassis_queueid(struct hmap *set, const struct uuid *uuid,
uint32_t queue_id)
{
if (!uuid) {
return;
}
struct ovn_chassis_qdisc_queues *node;
HMAP_FOR_EACH_WITH_HASH (node, key_node,
hash_chassis_queue(uuid, queue_id), set) {
if (uuid_equals(uuid, &node->chassis_uuid)
&& node->queue_id == queue_id) {
hmap_remove(set, &node->key_node);
free(node);
break;
}
}
}
static inline bool
port_has_qos_params(const struct smap *opts)
{
return (smap_get(opts, "qos_max_rate") ||
smap_get(opts, "qos_burst"));
}
static void
init_mcast_port_info(struct mcast_port_info *mcast_info,
const struct nbrec_logical_switch_port *nbsp,
const struct nbrec_logical_router_port *nbrp)
{
if (nbsp) {
mcast_info->flood =
smap_get_bool( ->options, "mcast_flood", false);
mcast_info->flood_reports =
smap_get_bool( ->options, "mcast_flood_reports",
false);
} else if (nbrp) {
/* We don't process multicast reports in any special way on logical
* routers so just treat them as regular multicast traffic.
*/
mcast_info->flood =
smap_get_bool(&nbrp->options, "mcast_flood", false);
mcast_info->flood_reports = mcast_info->flood;
}
}
static uint32_t
ovn_mcast_group_allocate_key(struct mcast_info *mcast_info)
{
return ovn_allocate_tnlid(&mcast_info->group_tnlids, "multicast group",
OVN_MIN_IP_MULTICAST, OVN_MAX_IP_MULTICAST,
&mcast_info->group_tnlid_hint);
}
/* Contains a NAT entry with the external addresses pre-parsed. */
struct ovn_nat {
const struct nbrec_nat *nb;
struct lport_addresses ext_addrs;
struct ovs_list ext_addr_list_node; /* Linkage in the per-external IP
* list of nat entries. Currently
* only used for SNAT.
*/
};
/* Stores the list of SNAT entries referencing a unique SNAT IP address.
* The 'snat_entries' list will be empty if the SNAT IP is used only for
* dnat_force_snat_ip or lb_force_snat_ip.
*/
struct ovn_snat_ip {
struct ovs_list snat_entries;
};
static bool
get_force_snat_ip(struct ovn_datapath *od, const char *key_type,
struct lport_addresses *laddrs);
/* Returns true if a 'nat_entry' is valid, i.e.:
* - parsing was successful.
* - the string yielded exactly one IPv4 address or exactly one IPv6 address.
*/
static bool
nat_entry_is_valid(const struct ovn_nat *nat_entry)
{
const struct lport_addresses *ext_addrs = &nat_entry->ext_addrs;
return (ext_addrs->n_ipv4_addrs == 1 && ext_addrs->n_ipv6_addrs == 0) ||
(ext_addrs->n_ipv4_addrs == 0 && ext_addrs->n_ipv6_addrs == 1);
}
static bool
nat_entry_is_v6(const struct ovn_nat *nat_entry)
{
return nat_entry->ext_addrs.n_ipv6_addrs > 0;
}
static void
snat_ip_add(struct ovn_datapath *od, const char *ip, struct ovn_nat *nat_entry)
{
struct ovn_snat_ip *snat_ip = shash_find_data(&od->snat_ips, ip);
if (!snat_ip) {
snat_ip = xzalloc(sizeof *snat_ip);
ovs_list_init(&snat_ip->snat_entries);
shash_add(&od->snat_ips, ip, snat_ip);
}
if (nat_entry) {
ovs_list_push_back(&snat_ip->snat_entries,
&nat_entry->ext_addr_list_node);
}
}
static void
init_nat_entries(struct ovn_datapath *od)
{
if (!od->nbr) {
return;
}
shash_init(&od->snat_ips);
if (get_force_snat_ip(od, "dnat", &od->dnat_force_snat_addrs)) {
if (od->dnat_force_snat_addrs.n_ipv4_addrs) {
snat_ip_add(od, od->dnat_force_snat_addrs.ipv4_addrs[0].addr_s,
NULL);
}
if (od->dnat_force_snat_addrs.n_ipv6_addrs) {
snat_ip_add(od, od->dnat_force_snat_addrs.ipv6_addrs[0].addr_s,
NULL);
}
}
/* Check if 'lb_force_snat_ip' is configured with 'router_ip'. */
const char *lb_force_snat =
smap_get(&od->nbr->options, "lb_force_snat_ip");
if (lb_force_snat && !strcmp(lb_force_snat, "router_ip")
&& smap_get(&od->nbr->options, "chassis")) {
/* Set it to true only if its gateway router and
* options:lb_force_snat_ip=router_ip. */
od->lb_force_snat_router_ip = true;
} else {
od->lb_force_snat_router_ip = false;
/* Check if 'lb_force_snat_ip' is configured with a set of
* IP address(es). */
if (get_force_snat_ip(od, "lb", &od->lb_force_snat_addrs)) {
if (od->lb_force_snat_addrs.n_ipv4_addrs) {
snat_ip_add(od, od->lb_force_snat_addrs.ipv4_addrs[0].addr_s,
NULL);
}
if (od->lb_force_snat_addrs.n_ipv6_addrs) {
snat_ip_add(od, od->lb_force_snat_addrs.ipv6_addrs[0].addr_s,
NULL);
}
}
}
if (!od->nbr->n_nat) {
return;
}
od->nat_entries = xmalloc(od->nbr->n_nat * sizeof *od->nat_entries);
for (size_t i = 0; i < od->nbr->n_nat; i++) {
const struct nbrec_nat *nat = od->nbr->nat[i];
struct ovn_nat *nat_entry = &od->nat_entries[i];
nat_entry->nb = nat;
if (!extract_ip_addresses(nat->external_ip,
&nat_entry->ext_addrs) ||
!nat_entry_is_valid(nat_entry)) {
static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 1);
VLOG_WARN_RL(&rl,
"Bad ip address %s in nat configuration "
"for router %s", nat->external_ip, od->nbr->name);
continue;
}
/* If this is a SNAT rule add the IP to the set of unique SNAT IPs. */
if (!strcmp(nat->type, "snat")) {
if (!nat_entry_is_v6(nat_entry)) {
snat_ip_add(od, nat_entry->ext_addrs.ipv4_addrs[0].addr_s,
nat_entry);
} else {
snat_ip_add(od, nat_entry->ext_addrs.ipv6_addrs[0].addr_s,
nat_entry);
}
}
if (!strcmp(nat->type, "dnat_and_snat")
&& nat->logical_port && nat->external_mac) {
od->has_distributed_nat = true;
}
}
od->n_nat_entries = od->nbr->n_nat;
}
static void
destroy_nat_entries(struct ovn_datapath *od)
{
if (!od->nbr) {
return;
}
shash_destroy_free_data(&od->snat_ips);
destroy_lport_addresses(&od->dnat_force_snat_addrs);
destroy_lport_addresses(&od->lb_force_snat_addrs);
for (size_t i = 0; i < od->n_nat_entries; i++) {
destroy_lport_addresses(&od->nat_entries[i].ext_addrs);
}
}
static void
init_router_external_ips(struct ovn_datapath *od)
{
if (!od->nbr) {
return;
}
sset_init(&od->external_ips);
for (size_t i = 0; i < od->nbr->n_nat; i++) {
sset_add(&od->external_ips, od->nbr->nat[i]->external_ip);
}
}
static void
destroy_router_external_ips(struct ovn_datapath *od)
{
if (!od->nbr) {
return;
}
sset_destroy(&od->external_ips);
}
static bool
lb_has_vip(const struct nbrec_load_balancer *lb)
{
return !smap_is_empty(&lb->vips);
}
static bool
lb_group_has_vip(const struct nbrec_load_balancer_group *lb_group)
{
for (size_t i = 0; i < lb_group->n_load_balancer; i++) {
if (lb_has_vip(lb_group->load_balancer[i])) {
return true;
}
}
return false;
}
static bool
ls_has_lb_vip(struct ovn_datapath *od)
{
for (size_t i = 0; i < od->nbs->n_load_balancer; i++) {
if (lb_has_vip(od->nbs->load_balancer[i])) {
return true;
}
}
for (size_t i = 0; i < od->nbs->n_load_balancer_group; i++) {
if (lb_group_has_vip(od->nbs->load_balancer_group[i])) {
return true;
}
}
return false;
}
static bool
lr_has_lb_vip(struct ovn_datapath *od)
{
for (size_t i = 0; i < od->nbr->n_load_balancer; i++) {
if (lb_has_vip(od->nbr->load_balancer[i])) {
return true;
}
}
for (size_t i = 0; i < od->nbr->n_load_balancer_group; i++) {
if (lb_group_has_vip(od->nbr->load_balancer_group[i])) {
return true;
}
}
return false;
}
static void
init_lb_for_datapath(struct ovn_datapath *od)
{
if (od->nbs) {
od->has_lb_vip = ls_has_lb_vip(od);
} else {
od->has_lb_vip = lr_has_lb_vip(od);
}
}
static void
destroy_lb_for_datapath(struct ovn_datapath *od)
{
ovn_lb_ip_set_destroy(od->lb_ips);
od->lb_ips = NULL;
if (!od->nbs && !od->nbr) {
return;
}
}
/* A group of logical router datapaths which are connected - either
* directly or indirectly.
* Each logical router can belong to only one group. */
struct lrouter_group {
struct ovn_datapath **router_dps;
int n_router_dps;
/* Set of ha_chassis_groups which are associated with the router dps. */
struct sset ha_chassis_groups;
};
static struct ovn_datapath *
ovn_datapath_create(struct hmap *datapaths, const struct uuid *key,
const struct nbrec_logical_switch *nbs,
const struct nbrec_logical_router *nbr,
const struct sbrec_datapath_binding *sb)
{
struct ovn_datapath *od = xzalloc(sizeof *od);
od->key = *key;
od->sb = sb;
od->nbs = nbs;
od->nbr = nbr;
hmap_init(&od->port_tnlids);
hmap_init(&od->nb_pgs);
od->port_key_hint = 0;
hmap_insert(datapaths, &od->key_node, uuid_hash(&od->key));
od->lr_group = NULL;
ovs_list_init(&od->port_list);
return od;
}
static void ovn_ls_port_group_destroy(struct hmap *nb_pgs);
static void destroy_mcast_info_for_datapath(struct ovn_datapath *od);
static void
ovn_datapath_destroy(struct hmap *datapaths, struct ovn_datapath *od)
{
if (od) {
/* Don't remove od->list. It is used within build_datapaths() as a
* private list and once we've exited that function it is not safe to
* use it. */
hmap_remove(datapaths, &od->key_node);
ovn_destroy_tnlids(&od->port_tnlids);
destroy_ipam_info(&od->ipam_info);
free(od->router_ports);
free(od->ls_peers);
destroy_nat_entries(od);
destroy_router_external_ips(od);
destroy_lb_for_datapath(od);
free(od->nat_entries);
free(od->localnet_ports);
free(od->l3dgw_ports);
ovn_ls_port_group_destroy(&od->nb_pgs);
destroy_mcast_info_for_datapath(od);
free(od);
}
}
/* Returns 'od''s datapath type. */
static enum ovn_datapath_type
ovn_datapath_get_type(const struct ovn_datapath *od)
{
return od->nbs ? DP_SWITCH : DP_ROUTER;
}
static struct ovn_datapath *
ovn_datapath_find(const struct hmap *datapaths,
const struct uuid *uuid)
{
struct ovn_datapath *od;
HMAP_FOR_EACH_WITH_HASH (od, key_node, uuid_hash(uuid), datapaths) {
if (uuid_equals(uuid, &od->key)) {
return od;
}
}
return NULL;
}
static struct ovn_datapath *
ovn_datapath_find_by_key(struct hmap *datapaths, uint32_t dp_key)
{
struct ovn_datapath *od;
HMAP_FOR_EACH (od, key_node, datapaths) {
if (od->tunnel_key == dp_key) {
return od;
}
}
return NULL;
}
static bool
ovn_datapath_is_stale(const struct ovn_datapath *od)
{
return !od->nbr && !od->nbs;
}
static struct ovn_datapath *
ovn_datapath_from_sbrec(const struct hmap *datapaths,
const struct sbrec_datapath_binding *sb)
{
struct uuid key;
if (!smap_get_uuid(&sb->external_ids, "logical-switch", &key) &&
!smap_get_uuid(&sb->external_ids, "logical-router", &key)) {
return NULL;
}
struct ovn_datapath *od = ovn_datapath_find(datapaths, &key);
if (od && (od->sb == sb)) {
return od;
}
return NULL;
}
static void
ovn_datapath_add_router_port(struct ovn_datapath *od, struct ovn_port *op)
{
if (od->n_router_ports == od->n_allocated_router_ports) {
od->router_ports = x2nrealloc(od->router_ports,
&od->n_allocated_router_ports,
sizeof *od->router_ports);
}
od->router_ports[od->n_router_ports++] = op;
}
static void
ovn_datapath_add_ls_peer(struct ovn_datapath *od, struct ovn_datapath *peer)
{
if (od->n_ls_peers == od->n_allocated_ls_peers) {
od->ls_peers = x2nrealloc(od->ls_peers, &od->n_allocated_ls_peers,
sizeof *od->ls_peers);
}
od->ls_peers[od->n_ls_peers++] = peer;
}
static bool
lrouter_is_enabled(const struct nbrec_logical_router *lrouter)
{
return !lrouter->enabled || *lrouter->enabled;
}
static void
init_ipam_info_for_datapath(struct ovn_datapath *od)
{
if (!od->nbs) {
return;
}
char uuid_s[UUID_LEN + 1];
sprintf(uuid_s, UUID_FMT, UUID_ARGS(&od->key));
init_ipam_info(&od->ipam_info, &od->nbs->other_config, uuid_s);
}
static void
init_mcast_info_for_router_datapath(struct ovn_datapath *od)
{