forked from openvswitch/ovs
/
ovn-northd.c
3032 lines (2670 loc) · 109 KB
/
ovn-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 <getopt.h>
#include <stdlib.h>
#include <stdio.h>
#include "command-line.h"
#include "daemon.h"
#include "dirs.h"
#include "openvswitch/dynamic-string.h"
#include "fatal-signal.h"
#include "hash.h"
#include "hmap.h"
#include "json.h"
#include "ovn/lib/lex.h"
#include "ovn/lib/ovn-nb-idl.h"
#include "ovn/lib/ovn-sb-idl.h"
#include "ovn/lib/ovn-util.h"
#include "packets.h"
#include "poll-loop.h"
#include "smap.h"
#include "sset.h"
#include "stream.h"
#include "stream-ssl.h"
#include "unixctl.h"
#include "util.h"
#include "uuid.h"
#include "openvswitch/vlog.h"
VLOG_DEFINE_THIS_MODULE(ovn_northd);
static unixctl_cb_func ovn_northd_exit;
struct northd_context {
struct ovsdb_idl *ovnnb_idl;
struct ovsdb_idl *ovnsb_idl;
struct ovsdb_idl_txn *ovnnb_txn;
struct ovsdb_idl_txn *ovnsb_txn;
};
static const char *ovnnb_db;
static const char *ovnsb_db;
static const char *default_nb_db(void);
static const char *default_sb_db(void);
/* Pipeline stages. */
/* The two pipelines in an OVN logical flow table. */
enum ovn_pipeline {
P_IN, /* Ingress pipeline. */
P_OUT /* Egress pipeline. */
};
/* 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, PRE_ACL, 3, "ls_in_pre_acl") \
PIPELINE_STAGE(SWITCH, IN, PRE_LB, 4, "ls_in_pre_lb") \
PIPELINE_STAGE(SWITCH, IN, PRE_STATEFUL, 5, "ls_in_pre_stateful") \
PIPELINE_STAGE(SWITCH, IN, ACL, 6, "ls_in_acl") \
PIPELINE_STAGE(SWITCH, IN, LB, 7, "ls_in_lb") \
PIPELINE_STAGE(SWITCH, IN, STATEFUL, 8, "ls_in_stateful") \
PIPELINE_STAGE(SWITCH, IN, ARP_RSP, 9, "ls_in_arp_rsp") \
PIPELINE_STAGE(SWITCH, IN, L2_LKUP, 10, "ls_in_l2_lkup") \
\
/* 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, LB, 3, "ls_out_lb") \
PIPELINE_STAGE(SWITCH, OUT, ACL, 4, "ls_out_acl") \
PIPELINE_STAGE(SWITCH, OUT, STATEFUL, 5, "ls_out_stateful") \
PIPELINE_STAGE(SWITCH, OUT, PORT_SEC_IP, 6, "ls_out_port_sec_ip") \
PIPELINE_STAGE(SWITCH, OUT, PORT_SEC_L2, 7, "ls_out_port_sec_l2") \
\
/* Logical router ingress stages. */ \
PIPELINE_STAGE(ROUTER, IN, ADMISSION, 0, "lr_in_admission") \
PIPELINE_STAGE(ROUTER, IN, IP_INPUT, 1, "lr_in_ip_input") \
PIPELINE_STAGE(ROUTER, IN, UNSNAT, 2, "lr_in_unsnat") \
PIPELINE_STAGE(ROUTER, IN, DNAT, 3, "lr_in_dnat") \
PIPELINE_STAGE(ROUTER, IN, IP_ROUTING, 4, "lr_in_ip_routing") \
PIPELINE_STAGE(ROUTER, IN, ARP_RESOLVE, 5, "lr_in_arp_resolve") \
PIPELINE_STAGE(ROUTER, IN, ARP_REQUEST, 6, "lr_in_arp_request") \
\
/* Logical router egress stages. */ \
PIPELINE_STAGE(ROUTER, OUT, SNAT, 0, "lr_out_snat") \
PIPELINE_STAGE(ROUTER, OUT, DELIVERY, 1, "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
/* 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 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>";
}
}
static void
usage(void)
{
printf("\
%s: OVN northbound management daemon\n\
usage: %s [OPTIONS]\n\
\n\
Options:\n\
--ovnnb-db=DATABASE connect to ovn-nb database at DATABASE\n\
(default: %s)\n\
--ovnsb-db=DATABASE connect to ovn-sb database at DATABASE\n\
(default: %s)\n\
-h, --help display this help message\n\
-o, --options list available options\n\
-V, --version display version information\n\
", program_name, program_name, default_nb_db(), default_sb_db());
daemon_usage();
vlog_usage();
stream_usage("database", true, true, false);
}
struct tnlid_node {
struct hmap_node hmap_node;
uint32_t tnlid;
};
static void
destroy_tnlids(struct hmap *tnlids)
{
struct tnlid_node *node;
HMAP_FOR_EACH_POP (node, hmap_node, tnlids) {
free(node);
}
hmap_destroy(tnlids);
}
static void
add_tnlid(struct hmap *set, uint32_t tnlid)
{
struct tnlid_node *node = xmalloc(sizeof *node);
hmap_insert(set, &node->hmap_node, hash_int(tnlid, 0));
node->tnlid = tnlid;
}
static bool
tnlid_in_use(const struct hmap *set, uint32_t tnlid)
{
const struct tnlid_node *node;
HMAP_FOR_EACH_IN_BUCKET (node, hmap_node, hash_int(tnlid, 0), set) {
if (node->tnlid == tnlid) {
return true;
}
}
return false;
}
static uint32_t
allocate_tnlid(struct hmap *set, const char *name, uint32_t max,
uint32_t *hint)
{
for (uint32_t tnlid = *hint + 1; tnlid != *hint;
tnlid = tnlid + 1 <= max ? tnlid + 1 : 1) {
if (!tnlid_in_use(set, tnlid)) {
add_tnlid(set, tnlid);
*hint = tnlid;
return tnlid;
}
}
static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
VLOG_WARN_RL(&rl, "all %s tunnel ids exhausted", name);
return 0;
}
/* 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. */
/* Logical router data (digested from nbr). */
const struct ovn_port *gateway_port;
ovs_be32 gateway;
/* Logical switch data. */
struct ovn_port **router_ports;
size_t n_router_ports;
struct hmap port_tnlids;
uint32_t port_key_hint;
bool has_unknown;
};
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);
od->port_key_hint = 0;
hmap_insert(datapaths, &od->key_node, uuid_hash(&od->key));
return 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);
destroy_tnlids(&od->port_tnlids);
free(od->router_ports);
free(od);
}
}
static struct ovn_datapath *
ovn_datapath_find(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_from_sbrec(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;
}
return ovn_datapath_find(datapaths, &key);
}
static bool
lrouter_is_enabled(const struct nbrec_logical_router *lrouter)
{
return !lrouter->enabled || *lrouter->enabled;
}
static void
join_datapaths(struct northd_context *ctx, struct hmap *datapaths,
struct ovs_list *sb_only, struct ovs_list *nb_only,
struct ovs_list *both)
{
hmap_init(datapaths);
ovs_list_init(sb_only);
ovs_list_init(nb_only);
ovs_list_init(both);
const struct sbrec_datapath_binding *sb, *sb_next;
SBREC_DATAPATH_BINDING_FOR_EACH_SAFE (sb, sb_next, ctx->ovnsb_idl) {
struct uuid key;
if (!smap_get_uuid(&sb->external_ids, "logical-switch", &key) &&
!smap_get_uuid(&sb->external_ids, "logical-router", &key)) {
ovsdb_idl_txn_add_comment(
ctx->ovnsb_txn,
"deleting Datapath_Binding "UUID_FMT" that lacks "
"external-ids:logical-switch and "
"external-ids:logical-router",
UUID_ARGS(&sb->header_.uuid));
sbrec_datapath_binding_delete(sb);
continue;
}
if (ovn_datapath_find(datapaths, &key)) {
static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 1);
VLOG_INFO_RL(
&rl, "deleting Datapath_Binding "UUID_FMT" with "
"duplicate external-ids:logical-switch/router "UUID_FMT,
UUID_ARGS(&sb->header_.uuid), UUID_ARGS(&key));
sbrec_datapath_binding_delete(sb);
continue;
}
struct ovn_datapath *od = ovn_datapath_create(datapaths, &key,
NULL, NULL, sb);
ovs_list_push_back(sb_only, &od->list);
}
const struct nbrec_logical_switch *nbs;
NBREC_LOGICAL_SWITCH_FOR_EACH (nbs, ctx->ovnnb_idl) {
struct ovn_datapath *od = ovn_datapath_find(datapaths,
&nbs->header_.uuid);
if (od) {
od->nbs = nbs;
ovs_list_remove(&od->list);
ovs_list_push_back(both, &od->list);
} else {
od = ovn_datapath_create(datapaths, &nbs->header_.uuid,
nbs, NULL, NULL);
ovs_list_push_back(nb_only, &od->list);
}
}
const struct nbrec_logical_router *nbr;
NBREC_LOGICAL_ROUTER_FOR_EACH (nbr, ctx->ovnnb_idl) {
if (!lrouter_is_enabled(nbr)) {
continue;
}
struct ovn_datapath *od = ovn_datapath_find(datapaths,
&nbr->header_.uuid);
if (od) {
if (!od->nbs) {
od->nbr = nbr;
ovs_list_remove(&od->list);
ovs_list_push_back(both, &od->list);
} else {
/* Can't happen! */
static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 1);
VLOG_WARN_RL(&rl,
"duplicate UUID "UUID_FMT" in OVN_Northbound",
UUID_ARGS(&nbr->header_.uuid));
continue;
}
} else {
od = ovn_datapath_create(datapaths, &nbr->header_.uuid,
NULL, nbr, NULL);
ovs_list_push_back(nb_only, &od->list);
}
od->gateway = 0;
if (nbr->default_gw) {
ovs_be32 ip;
if (!ip_parse(nbr->default_gw, &ip) || !ip) {
static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 1);
VLOG_WARN_RL(&rl, "bad 'gateway' %s", nbr->default_gw);
} else {
od->gateway = ip;
}
}
/* Set the gateway port to NULL. If there is a gateway, it will get
* filled in as we go through the ports later. */
od->gateway_port = NULL;
}
}
static uint32_t
ovn_datapath_allocate_key(struct hmap *dp_tnlids)
{
static uint32_t hint;
return allocate_tnlid(dp_tnlids, "datapath", (1u << 24) - 1, &hint);
}
/* Updates the southbound Datapath_Binding table so that it contains the
* logical switches and routers specified by the northbound database.
*
* Initializes 'datapaths' to contain a "struct ovn_datapath" for every logical
* switch and router. */
static void
build_datapaths(struct northd_context *ctx, struct hmap *datapaths)
{
struct ovs_list sb_only, nb_only, both;
join_datapaths(ctx, datapaths, &sb_only, &nb_only, &both);
if (!ovs_list_is_empty(&nb_only)) {
/* First index the in-use datapath tunnel IDs. */
struct hmap dp_tnlids = HMAP_INITIALIZER(&dp_tnlids);
struct ovn_datapath *od;
LIST_FOR_EACH (od, list, &both) {
add_tnlid(&dp_tnlids, od->sb->tunnel_key);
}
/* Add southbound record for each unmatched northbound record. */
LIST_FOR_EACH (od, list, &nb_only) {
uint16_t tunnel_key = ovn_datapath_allocate_key(&dp_tnlids);
if (!tunnel_key) {
break;
}
od->sb = sbrec_datapath_binding_insert(ctx->ovnsb_txn);
char uuid_s[UUID_LEN + 1];
sprintf(uuid_s, UUID_FMT, UUID_ARGS(&od->key));
const char *key = od->nbs ? "logical-switch" : "logical-router";
const struct smap id = SMAP_CONST1(&id, key, uuid_s);
sbrec_datapath_binding_set_external_ids(od->sb, &id);
sbrec_datapath_binding_set_tunnel_key(od->sb, tunnel_key);
}
destroy_tnlids(&dp_tnlids);
}
/* Delete southbound records without northbound matches. */
struct ovn_datapath *od, *next;
LIST_FOR_EACH_SAFE (od, next, list, &sb_only) {
ovs_list_remove(&od->list);
sbrec_datapath_binding_delete(od->sb);
ovn_datapath_destroy(datapaths, od);
}
}
struct ovn_port {
struct hmap_node key_node; /* Index on 'key'. */
char *key; /* nbs->name, nbr->name, sb->logical_port. */
char *json_key; /* 'key', quoted for use in JSON. */
const struct nbrec_logical_switch_port *nbs; /* May be NULL. */
const struct nbrec_logical_router_port *nbr; /* May be NULL. */
const struct sbrec_port_binding *sb; /* May be NULL. */
/* Logical router port data. */
ovs_be32 ip, mask; /* 192.168.10.123/24. */
ovs_be32 network; /* 192.168.10.0. */
ovs_be32 bcast; /* 192.168.10.255. */
struct eth_addr mac;
struct ovn_port *peer;
struct ovn_datapath *od;
struct ovs_list list; /* In list of similar records. */
};
static struct ovn_port *
ovn_port_create(struct hmap *ports, const char *key,
const struct nbrec_logical_switch_port *nbs,
const struct nbrec_logical_router_port *nbr,
const struct sbrec_port_binding *sb)
{
struct ovn_port *op = xzalloc(sizeof *op);
struct ds json_key = DS_EMPTY_INITIALIZER;
json_string_escape(key, &json_key);
op->json_key = ds_steal_cstr(&json_key);
op->key = xstrdup(key);
op->sb = sb;
op->nbs = nbs;
op->nbr = nbr;
hmap_insert(ports, &op->key_node, hash_string(op->key, 0));
return op;
}
static void
ovn_port_destroy(struct hmap *ports, struct ovn_port *port)
{
if (port) {
/* Don't remove port->list. It is used within build_ports() as a
* private list and once we've exited that function it is not safe to
* use it. */
hmap_remove(ports, &port->key_node);
free(port->json_key);
free(port->key);
free(port);
}
}
static struct ovn_port *
ovn_port_find(struct hmap *ports, const char *name)
{
struct ovn_port *op;
HMAP_FOR_EACH_WITH_HASH (op, key_node, hash_string(name, 0), ports) {
if (!strcmp(op->key, name)) {
return op;
}
}
return NULL;
}
static uint32_t
ovn_port_allocate_key(struct ovn_datapath *od)
{
return allocate_tnlid(&od->port_tnlids, "port",
(1u << 15) - 1, &od->port_key_hint);
}
static void
join_logical_ports(struct northd_context *ctx,
struct hmap *datapaths, struct hmap *ports,
struct ovs_list *sb_only, struct ovs_list *nb_only,
struct ovs_list *both)
{
hmap_init(ports);
ovs_list_init(sb_only);
ovs_list_init(nb_only);
ovs_list_init(both);
const struct sbrec_port_binding *sb;
SBREC_PORT_BINDING_FOR_EACH (sb, ctx->ovnsb_idl) {
struct ovn_port *op = ovn_port_create(ports, sb->logical_port,
NULL, NULL, sb);
ovs_list_push_back(sb_only, &op->list);
}
struct ovn_datapath *od;
HMAP_FOR_EACH (od, key_node, datapaths) {
if (od->nbs) {
for (size_t i = 0; i < od->nbs->n_ports; i++) {
const struct nbrec_logical_switch_port *nbs = od->nbs->ports[i];
struct ovn_port *op = ovn_port_find(ports, nbs->name);
if (op) {
if (op->nbs || op->nbr) {
static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(5, 1);
VLOG_WARN_RL(&rl, "duplicate logical port %s",
nbs->name);
continue;
}
op->nbs = nbs;
ovs_list_remove(&op->list);
ovs_list_push_back(both, &op->list);
} else {
op = ovn_port_create(ports, nbs->name, nbs, NULL, NULL);
ovs_list_push_back(nb_only, &op->list);
}
op->od = od;
}
} else {
for (size_t i = 0; i < od->nbr->n_ports; i++) {
const struct nbrec_logical_router_port *nbr
= od->nbr->ports[i];
struct eth_addr mac;
if (!eth_addr_from_string(nbr->mac, &mac)) {
static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(5, 1);
VLOG_WARN_RL(&rl, "bad 'mac' %s", nbr->mac);
continue;
}
ovs_be32 ip, mask;
char *error = ip_parse_masked(nbr->network, &ip, &mask);
if (error || mask == OVS_BE32_MAX || !ip_is_cidr(mask)) {
static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(5, 1);
VLOG_WARN_RL(&rl, "bad 'network' %s", nbr->network);
free(error);
continue;
}
struct ovn_port *op = ovn_port_find(ports, nbr->name);
if (op) {
if (op->nbs || op->nbr) {
static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(5, 1);
VLOG_WARN_RL(&rl, "duplicate logical router port %s",
nbr->name);
continue;
}
op->nbr = nbr;
ovs_list_remove(&op->list);
ovs_list_push_back(both, &op->list);
} else {
op = ovn_port_create(ports, nbr->name, NULL, nbr, NULL);
ovs_list_push_back(nb_only, &op->list);
}
op->ip = ip;
op->mask = mask;
op->network = ip & mask;
op->bcast = ip | ~mask;
op->mac = mac;
op->od = od;
/* If 'od' has a gateway and 'op' routes to it... */
if (od->gateway && !((op->network ^ od->gateway) & op->mask)) {
/* ...and if 'op' is a longer match than the current
* choice... */
const struct ovn_port *gw = od->gateway_port;
int len = gw ? ip_count_cidr_bits(gw->mask) : 0;
if (ip_count_cidr_bits(op->mask) > len) {
/* ...then it's the default gateway port. */
od->gateway_port = op;
}
}
}
}
}
/* Connect logical router ports, and logical switch ports of type "router",
* to their peers. */
struct ovn_port *op;
HMAP_FOR_EACH (op, key_node, ports) {
if (op->nbs && !strcmp(op->nbs->type, "router")) {
const char *peer_name = smap_get(&op->nbs->options, "router-port");
if (!peer_name) {
continue;
}
struct ovn_port *peer = ovn_port_find(ports, peer_name);
if (!peer || !peer->nbr) {
continue;
}
peer->peer = op;
op->peer = peer;
op->od->router_ports = xrealloc(
op->od->router_ports,
sizeof *op->od->router_ports * (op->od->n_router_ports + 1));
op->od->router_ports[op->od->n_router_ports++] = op;
} else if (op->nbr && op->nbr->peer) {
op->peer = ovn_port_find(ports, op->nbr->peer);
}
}
}
static void
ovn_port_update_sbrec(const struct ovn_port *op)
{
sbrec_port_binding_set_datapath(op->sb, op->od->sb);
if (op->nbr) {
/* If the router is for l3 gateway, it resides on a chassis
* and its port type is "gateway". */
const char *chassis = smap_get(&op->od->nbr->options, "chassis");
if (chassis) {
sbrec_port_binding_set_type(op->sb, "gateway");
} else {
sbrec_port_binding_set_type(op->sb, "patch");
}
const char *peer = op->peer ? op->peer->key : "<error>";
struct smap new;
smap_init(&new);
smap_add(&new, "peer", peer);
if (chassis) {
smap_add(&new, "gateway-chassis", chassis);
}
sbrec_port_binding_set_options(op->sb, &new);
smap_destroy(&new);
sbrec_port_binding_set_parent_port(op->sb, NULL);
sbrec_port_binding_set_tag(op->sb, NULL, 0);
sbrec_port_binding_set_mac(op->sb, NULL, 0);
} else {
if (strcmp(op->nbs->type, "router")) {
sbrec_port_binding_set_type(op->sb, op->nbs->type);
sbrec_port_binding_set_options(op->sb, &op->nbs->options);
} else {
const char *chassis = NULL;
if (op->peer && op->peer->od && op->peer->od->nbr) {
chassis = smap_get(&op->peer->od->nbr->options, "chassis");
}
/* A switch port connected to a gateway router is also of
* type "gateway". */
if (chassis) {
sbrec_port_binding_set_type(op->sb, "gateway");
} else {
sbrec_port_binding_set_type(op->sb, "patch");
}
const char *router_port = smap_get(&op->nbs->options,
"router-port");
if (!router_port) {
router_port = "<error>";
}
struct smap new;
smap_init(&new);
smap_add(&new, "peer", router_port);
if (chassis) {
smap_add(&new, "gateway-chassis", chassis);
}
sbrec_port_binding_set_options(op->sb, &new);
smap_destroy(&new);
}
sbrec_port_binding_set_parent_port(op->sb, op->nbs->parent_name);
sbrec_port_binding_set_tag(op->sb, op->nbs->tag, op->nbs->n_tag);
sbrec_port_binding_set_mac(op->sb, (const char **) op->nbs->addresses,
op->nbs->n_addresses);
}
}
/* Updates the southbound Port_Binding table so that it contains the logical
* switch ports specified by the northbound database.
*
* Initializes 'ports' to contain a "struct ovn_port" for every logical port,
* using the "struct ovn_datapath"s in 'datapaths' to look up logical
* datapaths. */
static void
build_ports(struct northd_context *ctx, struct hmap *datapaths,
struct hmap *ports)
{
struct ovs_list sb_only, nb_only, both;
join_logical_ports(ctx, datapaths, ports, &sb_only, &nb_only, &both);
/* For logical ports that are in both databases, update the southbound
* record based on northbound data. Also index the in-use tunnel_keys. */
struct ovn_port *op, *next;
LIST_FOR_EACH_SAFE (op, next, list, &both) {
ovn_port_update_sbrec(op);
add_tnlid(&op->od->port_tnlids, op->sb->tunnel_key);
if (op->sb->tunnel_key > op->od->port_key_hint) {
op->od->port_key_hint = op->sb->tunnel_key;
}
}
/* Add southbound record for each unmatched northbound record. */
LIST_FOR_EACH_SAFE (op, next, list, &nb_only) {
uint16_t tunnel_key = ovn_port_allocate_key(op->od);
if (!tunnel_key) {
continue;
}
op->sb = sbrec_port_binding_insert(ctx->ovnsb_txn);
ovn_port_update_sbrec(op);
sbrec_port_binding_set_logical_port(op->sb, op->key);
sbrec_port_binding_set_tunnel_key(op->sb, tunnel_key);
}
/* Delete southbound records without northbound matches. */
LIST_FOR_EACH_SAFE(op, next, list, &sb_only) {
ovs_list_remove(&op->list);
sbrec_port_binding_delete(op->sb);
ovn_port_destroy(ports, op);
}
}
#define OVN_MIN_MULTICAST 32768
#define OVN_MAX_MULTICAST 65535
struct multicast_group {
const char *name;
uint16_t key; /* OVN_MIN_MULTICAST...OVN_MAX_MULTICAST. */
};
#define MC_FLOOD "_MC_flood"
static const struct multicast_group mc_flood = { MC_FLOOD, 65535 };
#define MC_UNKNOWN "_MC_unknown"
static const struct multicast_group mc_unknown = { MC_UNKNOWN, 65534 };
static bool
multicast_group_equal(const struct multicast_group *a,
const struct multicast_group *b)
{
return !strcmp(a->name, b->name) && a->key == b->key;
}
/* Multicast group entry. */
struct ovn_multicast {
struct hmap_node hmap_node; /* Index on 'datapath' and 'key'. */
struct ovn_datapath *datapath;
const struct multicast_group *group;
struct ovn_port **ports;
size_t n_ports, allocated_ports;
};
static uint32_t
ovn_multicast_hash(const struct ovn_datapath *datapath,
const struct multicast_group *group)
{
return hash_pointer(datapath, group->key);
}
static struct ovn_multicast *
ovn_multicast_find(struct hmap *mcgroups, struct ovn_datapath *datapath,
const struct multicast_group *group)
{
struct ovn_multicast *mc;
HMAP_FOR_EACH_WITH_HASH (mc, hmap_node,
ovn_multicast_hash(datapath, group), mcgroups) {
if (mc->datapath == datapath
&& multicast_group_equal(mc->group, group)) {
return mc;
}
}
return NULL;
}
static void
ovn_multicast_add(struct hmap *mcgroups, const struct multicast_group *group,
struct ovn_port *port)
{
struct ovn_datapath *od = port->od;
struct ovn_multicast *mc = ovn_multicast_find(mcgroups, od, group);
if (!mc) {
mc = xmalloc(sizeof *mc);
hmap_insert(mcgroups, &mc->hmap_node, ovn_multicast_hash(od, group));
mc->datapath = od;
mc->group = group;
mc->n_ports = 0;
mc->allocated_ports = 4;
mc->ports = xmalloc(mc->allocated_ports * sizeof *mc->ports);
}
if (mc->n_ports >= mc->allocated_ports) {
mc->ports = x2nrealloc(mc->ports, &mc->allocated_ports,
sizeof *mc->ports);
}
mc->ports[mc->n_ports++] = port;
}
static void
ovn_multicast_destroy(struct hmap *mcgroups, struct ovn_multicast *mc)
{
if (mc) {
hmap_remove(mcgroups, &mc->hmap_node);
free(mc->ports);
free(mc);
}
}
static void
ovn_multicast_update_sbrec(const struct ovn_multicast *mc,
const struct sbrec_multicast_group *sb)
{
struct sbrec_port_binding **ports = xmalloc(mc->n_ports * sizeof *ports);
for (size_t i = 0; i < mc->n_ports; i++) {
ports[i] = CONST_CAST(struct sbrec_port_binding *, mc->ports[i]->sb);
}
sbrec_multicast_group_set_ports(sb, ports, mc->n_ports);
free(ports);
}
/* Logical flow generation.
*
* This code generates the Logical_Flow table in the southbound database, as a
* function of most of the northbound database.
*/
struct ovn_lflow {
struct hmap_node hmap_node;
struct ovn_datapath *od;
enum ovn_stage stage;
uint16_t priority;
char *match;
char *actions;
};
static size_t
ovn_lflow_hash(const struct ovn_lflow *lflow)
{
size_t hash = uuid_hash(&lflow->od->key);
hash = hash_2words((lflow->stage << 16) | lflow->priority, hash);
hash = hash_string(lflow->match, hash);
return hash_string(lflow->actions, hash);
}
static bool
ovn_lflow_equal(const struct ovn_lflow *a, const struct ovn_lflow *b)
{
return (a->od == b->od
&& a->stage == b->stage
&& a->priority == b->priority
&& !strcmp(a->match, b->match)
&& !strcmp(a->actions, b->actions));
}
static void
ovn_lflow_init(struct ovn_lflow *lflow, struct ovn_datapath *od,
enum ovn_stage stage, uint16_t priority,
char *match, char *actions)
{
lflow->od = od;
lflow->stage = stage;
lflow->priority = priority;
lflow->match = match;
lflow->actions = actions;
}
/* Adds a row with the specified contents to the Logical_Flow table. */
static void
ovn_lflow_add(struct hmap *lflow_map, struct ovn_datapath *od,
enum ovn_stage stage, uint16_t priority,
const char *match, const char *actions)
{
struct ovn_lflow *lflow = xmalloc(sizeof *lflow);
ovn_lflow_init(lflow, od, stage, priority,
xstrdup(match), xstrdup(actions));
hmap_insert(lflow_map, &lflow->hmap_node, ovn_lflow_hash(lflow));
}
static struct ovn_lflow *
ovn_lflow_find(struct hmap *lflows, struct ovn_datapath *od,
enum ovn_stage stage, uint16_t priority,
const char *match, const char *actions)
{
struct ovn_lflow target;
ovn_lflow_init(&target, od, stage, priority,
CONST_CAST(char *, match), CONST_CAST(char *, actions));
struct ovn_lflow *lflow;
HMAP_FOR_EACH_WITH_HASH (lflow, hmap_node, ovn_lflow_hash(&target),
lflows) {
if (ovn_lflow_equal(lflow, &target)) {
return lflow;
}
}
return NULL;
}
static void
ovn_lflow_destroy(struct hmap *lflows, struct ovn_lflow *lflow)
{
if (lflow) {
hmap_remove(lflows, &lflow->hmap_node);
free(lflow->match);
free(lflow->actions);
free(lflow);
}
}