/
northd.c
14849 lines (13190 loc) · 562 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 "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/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 check_lsp_is_up;
/* 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;
#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, PORT_SEC_L2, 0, "ls_in_port_sec_l2") \
PIPELINE_STAGE(SWITCH, IN, PORT_SEC_IP, 1, "ls_in_port_sec_ip") \
PIPELINE_STAGE(SWITCH, IN, PORT_SEC_ND, 2, "ls_in_port_sec_nd") \
PIPELINE_STAGE(SWITCH, IN, LOOKUP_FDB , 3, "ls_in_lookup_fdb") \
PIPELINE_STAGE(SWITCH, IN, PUT_FDB, 4, "ls_in_put_fdb") \
PIPELINE_STAGE(SWITCH, IN, PRE_ACL, 5, "ls_in_pre_acl") \
PIPELINE_STAGE(SWITCH, IN, PRE_LB, 6, "ls_in_pre_lb") \
PIPELINE_STAGE(SWITCH, IN, PRE_STATEFUL, 7, "ls_in_pre_stateful") \
PIPELINE_STAGE(SWITCH, IN, ACL_HINT, 8, "ls_in_acl_hint") \
PIPELINE_STAGE(SWITCH, IN, ACL, 9, "ls_in_acl") \
PIPELINE_STAGE(SWITCH, IN, QOS_MARK, 10, "ls_in_qos_mark") \
PIPELINE_STAGE(SWITCH, IN, QOS_METER, 11, "ls_in_qos_meter") \
PIPELINE_STAGE(SWITCH, IN, STATEFUL, 12, "ls_in_stateful") \
PIPELINE_STAGE(SWITCH, IN, PRE_HAIRPIN, 13, "ls_in_pre_hairpin") \
PIPELINE_STAGE(SWITCH, IN, NAT_HAIRPIN, 14, "ls_in_nat_hairpin") \
PIPELINE_STAGE(SWITCH, IN, HAIRPIN, 15, "ls_in_hairpin") \
PIPELINE_STAGE(SWITCH, IN, ARP_ND_RSP, 16, "ls_in_arp_rsp") \
PIPELINE_STAGE(SWITCH, IN, DHCP_OPTIONS, 17, "ls_in_dhcp_options") \
PIPELINE_STAGE(SWITCH, IN, DHCP_RESPONSE, 18, "ls_in_dhcp_response") \
PIPELINE_STAGE(SWITCH, IN, DNS_LOOKUP, 19, "ls_in_dns_lookup") \
PIPELINE_STAGE(SWITCH, IN, DNS_RESPONSE, 20, "ls_in_dns_response") \
PIPELINE_STAGE(SWITCH, IN, EXTERNAL_PORT, 21, "ls_in_external_port") \
PIPELINE_STAGE(SWITCH, IN, L2_LKUP, 22, "ls_in_l2_lkup") \
PIPELINE_STAGE(SWITCH, IN, L2_UNKNOWN, 23, "ls_in_l2_unknown") \
\
/* Logical switch egress stages. */ \
PIPELINE_STAGE(SWITCH, OUT, PRE_LB, 0, "ls_out_pre_lb") \
PIPELINE_STAGE(SWITCH, OUT, PRE_ACL, 1, "ls_out_pre_acl") \
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, PORT_SEC_IP, 8, "ls_out_port_sec_ip") \
PIPELINE_STAGE(SWITCH, OUT, PORT_SEC_L2, 9, "ls_out_port_sec_l2") \
\
/* 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, DNAT, 6, "lr_in_dnat") \
PIPELINE_STAGE(ROUTER, IN, ECMP_STATEFUL, 7, "lr_in_ecmp_stateful") \
PIPELINE_STAGE(ROUTER, IN, ND_RA_OPTIONS, 8, "lr_in_nd_ra_options") \
PIPELINE_STAGE(ROUTER, IN, ND_RA_RESPONSE, 9, "lr_in_nd_ra_response") \
PIPELINE_STAGE(ROUTER, IN, IP_ROUTING, 10, "lr_in_ip_routing") \
PIPELINE_STAGE(ROUTER, IN, IP_ROUTING_ECMP, 11, "lr_in_ip_routing_ecmp") \
PIPELINE_STAGE(ROUTER, IN, POLICY, 12, "lr_in_policy") \
PIPELINE_STAGE(ROUTER, IN, POLICY_ECMP, 13, "lr_in_policy_ecmp") \
PIPELINE_STAGE(ROUTER, IN, ARP_RESOLVE, 14, "lr_in_arp_resolve") \
PIPELINE_STAGE(ROUTER, IN, CHK_PKT_LEN , 15, "lr_in_chk_pkt_len") \
PIPELINE_STAGE(ROUTER, IN, LARGER_PKTS, 16, "lr_in_larger_pkts") \
PIPELINE_STAGE(ROUTER, IN, GW_REDIRECT, 17, "lr_in_gw_redirect") \
PIPELINE_STAGE(ROUTER, IN, ARP_REQUEST, 18, "lr_in_arp_request") \
\
/* Logical router egress stages. */ \
PIPELINE_STAGE(ROUTER, OUT, UNDNAT, 0, "lr_out_undnat") \
PIPELINE_STAGE(ROUTER, OUT, POST_UNDNAT, 1, "lr_out_post_undnat") \
PIPELINE_STAGE(ROUTER, OUT, SNAT, 2, "lr_out_snat") \
PIPELINE_STAGE(ROUTER, OUT, EGR_LOOP, 3, "lr_out_egr_loop") \
PIPELINE_STAGE(ROUTER, OUT, DELIVERY, 4, "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 REG_ORIG_DIP_IPV4 "reg1"
#define REG_ORIG_DIP_IPV6 "xxreg1"
#define REG_ORIG_TP_DPORT "reg2[0..15]"
/* Register definitions for switches and routers. */
/* 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]"
/* 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_ORIG_TP_DPORT_ROUTER "reg9[16..31]"
/* Register used for setting a label for ACLs in a Logical Switch. */
#define REG_LABEL "reg3"
#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 | X | |
* +----+----------------------------------------------+ X | |
* | R1 | ORIG_DIP_IPV4 (>= IN_STATEFUL) | R | |
* +----+----------------------------------------------+ E | |
* | R2 | ORIG_TP_DPORT (>= IN_STATEFUL) | G | |
* +----+----------------------------------------------+ 0 | |
* | R3 | ACL LABEL | | |
* +----+----------------------------------------------+---+------------------+
* | R4 | UNUSED | | |
* +----+----------------------------------------------+ X | ORIG_DIP_IPV6 |
* | R5 | UNUSED | X | (>= IN_STATEFUL) |
* +----+----------------------------------------------+ R | |
* | R6 | UNUSED | E | |
* +----+----------------------------------------------+ G | |
* | R7 | UNUSED | 1 | |
* +----+----------------------------------------------+---+------------------+
* | R8 | UNUSED |
* +----+----------------------------------------------+
* | 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 |
* +-----+--------------------------+---+-----------------+ G | ( >= DEFRAG ) |
* | R2 | UNUSED | X | | 0 | |
* | | | R | | | |
* +-----+--------------------------+ E | UNUSED | | |
* | R3 | UNUSED | G | | | |
* | | | 1 | | | |
* +-----+--------------------------+---+-----------------+---+---------------+
* | R4 | UNUSED | X | | | |
* | | | R | | | |
* +-----+--------------------------+ E | UNUSED | X | |
* | R5 | UNUSED | G | | X | |
* | | | 2 | | R |SRC_IPV6 for NS|
* +-----+--------------------------+---+-----------------+ E | ( >= |
* | R6 | UNUSED | X | | G | IN_IP_ROUTING)|
* | | | R | | 1 | |
* +-----+--------------------------+ E | UNUSED | | |
* | R7 | UNUSED | G | | | |
* | | | 3 | | | |
* +-----+--------------------------+---+-----------------+---+---------------+
* | R8 | ECMP_GROUP_ID | | |
* | | ECMP_MEMBER_ID | X | |
* +-----+--------------------------+ R | |
* | | REGBIT_{ | E | |
* | | EGRESS_LOOPBACK/ | G | UNUSED |
* | R9 | PKT_LARGER/ | 4 | |
* | | LOOKUP_NEIGHBOR_RESULT/| | |
* | | SKIP_LOOKUP_NEIGHBOR} | | |
* | | | | |
* | | REG_ORIG_TP_DPORT_ROUTER | | |
* | | | | |
* +-----+--------------------------+---+-----------------+
*
*/
/* 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();
}
}
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"));
}
/*
* Multicast snooping and querier per datapath configuration.
*/
struct mcast_switch_info {
bool enabled; /* True if snooping enabled. */
bool querier; /* True if querier enabled. */
bool flood_unregistered; /* True if unregistered multicast should be
* flooded.
*/
bool flood_relay; /* True if the switch is connected to a
* multicast router and unregistered multicast
* should be flooded to the mrouter. Only
* applicable if flood_unregistered == false.
*/
bool flood_reports; /* True if the switch has at least one port
* configured to flood reports.
*/
bool flood_static; /* True if the switch has at least one port
* configured to flood traffic.
*/
int64_t table_size; /* Max number of IP multicast groups. */
int64_t idle_timeout; /* Timeout after which an idle group is
* flushed.
*/
int64_t query_interval; /* Interval between multicast queries. */
char *eth_src; /* ETH src address of the queries. */
char *ipv4_src; /* IPv4 src address of the queries. */
char *ipv6_src; /* IPv6 src address of the queries. */
int64_t query_max_response; /* Expected time after which reports should
* be received for queries that were sent out.
*/
atomic_uint64_t active_v4_flows; /* Current number of active IPv4 multicast
* flows.
*/
atomic_uint64_t active_v6_flows; /* Current number of active IPv6 multicast
* flows.
*/
};
struct mcast_router_info {
bool relay; /* True if the router should relay IP multicast. */
bool flood_static; /* True if the router has at least one port configured
* to flood traffic.
*/
};
struct mcast_info {
struct hmap group_tnlids; /* Group tunnel IDs in use on this DP. */
uint32_t group_tnlid_hint; /* Hint for allocating next group tunnel ID. */
struct ovs_list groups; /* List of groups learnt on this DP. */
union {
struct mcast_switch_info sw; /* Switch specific multicast info. */
struct mcast_router_info rtr; /* Router specific multicast info. */
};
};
struct mcast_port_info {
bool flood; /* True if the port should flood IP multicast traffic
* regardless if it's registered or not. */
bool flood_reports; /* True if the port should flood IP multicast reports
* (e.g., IGMP join/leave). */
};
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);
}
/* The 'key' comes from nbs->header_.uuid or nbr->header_.uuid or
* sb->external_ids:logical-switch. */
struct ovn_datapath {
struct hmap_node key_node; /* Index on 'key'. */
struct uuid key; /* (nbs/nbr)->header_.uuid. */
const struct nbrec_logical_switch *nbs; /* May be NULL. */
const struct nbrec_logical_router *nbr; /* May be NULL. */
const struct sbrec_datapath_binding *sb; /* May be NULL. */
struct ovs_list list; /* In list of similar records. */
uint32_t tunnel_key;
/* Logical switch data. */
struct ovn_port **router_ports;
size_t n_router_ports;
struct hmap port_tnlids;
uint32_t port_key_hint;
bool has_stateful_acl;
bool has_lb_vip;
bool has_unknown;
bool has_acls;
/* IPAM data. */
struct ipam_info ipam_info;
/* Multicast data. */
struct mcast_info mcast_info;
/* Applies to only logical router datapath.
* True if logical router is a gateway router. i.e options:chassis is set.
* If this is true, then 'l3dgw_port' and 'l3redirect_port' will be
* ignored. */
bool is_gw_router;
/* OVN northd only needs to know about the logical router gateway port for
* NAT on a distributed router. The "distributed gateway ports" are
* populated only when there is a gateway chassis or ha chassis group
* specified for some of the ports on the logical router. Otherwise this
* will be NULL. */
struct ovn_port **l3dgw_ports;
size_t n_l3dgw_ports;
/* NAT entries configured on the router. */
struct ovn_nat *nat_entries;
bool has_distributed_nat;
/* Set of nat external ips on the router. */
struct sset external_ips;
/* SNAT IPs owned by the router (shash of 'struct ovn_snat_ip'). */
struct shash snat_ips;
struct lport_addresses dnat_force_snat_addrs;
struct lport_addresses lb_force_snat_addrs;
bool lb_force_snat_router_ip;
/* The "routable" ssets are subsets of the load balancer
* IPs for which IP routes and ARP resolution flows are automatically
* added
*/
struct sset lb_ips_v4;
struct sset lb_ips_v4_routable;
struct sset lb_ips_v6;
struct sset lb_ips_v6_routable;
struct ovn_port **localnet_ports;
size_t n_localnet_ports;
struct ovs_list lr_list; /* In list of logical router datapaths. */
/* The logical router group to which this datapath belongs.
* Valid only if it is logical router datapath. NULL otherwise. */
struct lrouter_group *lr_group;
/* Port groups related to the datapath, used only when nbs is NOT NULL. */
struct hmap nb_pgs;
struct ovs_list port_list;
};
/* 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;
}
if (od->n_l3dgw_ports > 1) {
static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
VLOG_WARN_RL(&rl, "NAT is configured on logical router %s, which has %"
PRIuSIZE" distributed gateway ports. NAT is not supported"
" yet when there is more than one distributed gateway "
"port on the router.",
od->nbr->name, od->n_l3dgw_ports);
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;
}
}
}
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->nbr->n_nat; 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_enternal_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;
}
/* Builds a unique address set compatible name ([a-zA-Z_.][a-zA-Z_.0-9]*)
* for the router's load balancer VIP address set, combining the logical
* router's datapath tunnel key and address family.
*
* Also prefixes the name with 'prefix'.
*/
static char *
lr_lb_address_set_name_(const struct ovn_datapath *od, const char *prefix,
int addr_family)
{
ovs_assert(od->nbr);
return xasprintf("%s_rtr_lb_%"PRIu32"_ip%s", prefix, od->tunnel_key,
addr_family == AF_INET ? "4" : "6");
}
/* Builds the router's load balancer VIP address set name. */
static char *
lr_lb_address_set_name(const struct ovn_datapath *od, int addr_family)
{
return lr_lb_address_set_name_(od, "", addr_family);
}
/* Builds a string that refers to the the router's load balancer VIP address
* set name, that is: $<address_set_name>.
*/
static char *
lr_lb_address_set_ref(const struct ovn_datapath *od, int addr_family)
{
return lr_lb_address_set_name_(od, "$", addr_family);
}
static void
init_lb_for_datapath(struct ovn_datapath *od)
{
sset_init(&od->lb_ips_v4);
sset_init(&od->lb_ips_v4_routable);
sset_init(&od->lb_ips_v6);
sset_init(&od->lb_ips_v6_routable);
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)
{
if (!od->nbs && !od->nbr) {
return;
}
sset_destroy(&od->lb_ips_v4);
sset_destroy(&od->lb_ips_v4_routable);
sset_destroy(&od->lb_ips_v6);
sset_destroy(&od->lb_ips_v6_routable);
}
/* 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);
destroy_nat_entries(od);
destroy_router_enternal_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)
{