forked from openvswitch/ovs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathovn-northd.c
1379 lines (1194 loc) · 43.4 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 "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 "poll-loop.h"
#include "smap.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_db(void);
/* Ingress pipeline stages.
*
* These must be listed in the order that the stages will be executed. */
#define INGRESS_STAGES \
INGRESS_STAGE(PORT_SEC, port_sec) \
INGRESS_STAGE(ACL, acl) \
INGRESS_STAGE(L2_LKUP, l2_lkup)
enum ingress_stage {
#define INGRESS_STAGE(NAME, STR) S_IN_##NAME,
INGRESS_STAGES
#undef INGRESS_STAGE
INGRESS_N_STAGES
};
/* Egress pipeline stages.
*
* These must be listed in the order that the stages will be executed. */
#define EGRESS_STAGES \
EGRESS_STAGE(ACL, acl) \
EGRESS_STAGE(PORT_SEC, port_sec)
enum egress_stage {
#define EGRESS_STAGE(NAME, STR) S_OUT_##NAME,
EGRESS_STAGES
#undef EGRESS_STAGE
EGRESS_N_STAGES
};
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_db(), default_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, *next;
HMAP_FOR_EACH_SAFE (node, next, hmap_node, tnlids) {
hmap_remove(tnlids, &node->hmap_node);
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 nb->header_.uuid or sb->external_ids:logical-switch. */
struct ovn_datapath {
struct hmap_node key_node; /* Index on 'key'. */
struct uuid key; /* nb->header_.uuid. */
const struct nbrec_logical_switch *nb; /* May be NULL. */
const struct sbrec_datapath_binding *sb; /* May be NULL. */
struct ovs_list list; /* In list of similar records. */
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 *nb,
const struct sbrec_datapath_binding *sb)
{
struct ovn_datapath *od = xzalloc(sizeof *od);
od->key = *key;
od->sb = sb;
od->nb = nb;
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);
}
}
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)) {
return NULL;
}
return ovn_datapath_find(datapaths, &key);
}
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);
list_init(sb_only);
list_init(nb_only);
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)) {
ovsdb_idl_txn_add_comment(ctx->ovnsb_txn,
"deleting Datapath_Binding "UUID_FMT" that "
"lacks external-ids:logical-switch",
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 "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, sb);
list_push_back(sb_only, &od->list);
}
const struct nbrec_logical_switch *nb;
NBREC_LOGICAL_SWITCH_FOR_EACH (nb, ctx->ovnnb_idl) {
struct ovn_datapath *od = ovn_datapath_find(datapaths,
&nb->header_.uuid);
if (od) {
od->nb = nb;
list_remove(&od->list);
list_push_back(both, &od->list);
} else {
od = ovn_datapath_create(datapaths, &nb->header_.uuid, nb, NULL);
list_push_back(nb_only, &od->list);
}
}
}
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);
}
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 (!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->nb->header_.uuid));
const struct smap id = SMAP_CONST1(&id, "logical-switch", 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) {
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'. */
const char *key; /* nb->name and sb->logical_port */
const struct nbrec_logical_port *nb; /* May be NULL. */
const struct sbrec_port_binding *sb; /* May be NULL. */
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_port *nb,
const struct sbrec_port_binding *sb)
{
struct ovn_port *op = xzalloc(sizeof *op);
op->key = key;
op->sb = sb;
op->nb = nb;
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);
}
}
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);
list_init(sb_only);
list_init(nb_only);
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, sb);
list_push_back(sb_only, &op->list);
}
struct ovn_datapath *od;
HMAP_FOR_EACH (od, key_node, datapaths) {
for (size_t i = 0; i < od->nb->n_ports; i++) {
const struct nbrec_logical_port *nb = od->nb->ports[i];
struct ovn_port *op = ovn_port_find(ports, nb->name);
if (op) {
op->nb = nb;
list_remove(&op->list);
list_push_back(both, &op->list);
} else {
op = ovn_port_create(ports, nb->name, nb, NULL);
list_push_back(nb_only, &op->list);
}
op->od = od;
}
}
}
static void
ovn_port_update_sbrec(const struct ovn_port *op)
{
sbrec_port_binding_set_type(op->sb, op->nb->type);
sbrec_port_binding_set_options(op->sb, &op->nb->options);
sbrec_port_binding_set_datapath(op->sb, op->od->sb);
sbrec_port_binding_set_parent_port(op->sb, op->nb->parent_name);
sbrec_port_binding_set_tag(op->sb, op->nb->tag, op->nb->n_tag);
sbrec_port_binding_set_mac(op->sb, (const char **) op->nb->macs,
op->nb->n_macs);
}
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) {
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_pipeline { P_IN, P_OUT } pipeline;
uint8_t table_id;
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->table_id << 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->pipeline == b->pipeline
&& a->table_id == b->table_id
&& 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_pipeline pipeline, uint8_t table_id, uint16_t priority,
char *match, char *actions)
{
lflow->od = od;
lflow->pipeline = pipeline;
lflow->table_id = table_id;
lflow->priority = priority;
lflow->match = match;
lflow->actions = actions;
}
static const char *
ingress_stage_to_str(int stage) {
switch (stage) {
#define INGRESS_STAGE(NAME, STR) case S_IN_##NAME: return #STR;
INGRESS_STAGES
#undef INGRESS_STAGE
default: return "<unknown>";
}
}
static const char *
egress_stage_to_str(int stage) {
switch (stage) {
#define EGRESS_STAGE(NAME, STR) case S_OUT_##NAME: return #STR;
EGRESS_STAGES
#undef EGRESS_STAGE
default: return "<unknown>";
}
}
/* 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_pipeline pipeline, uint8_t table_id, uint16_t priority,
const char *match, const char *actions)
{
struct ovn_lflow *lflow = xmalloc(sizeof *lflow);
ovn_lflow_init(lflow, od, pipeline, table_id, 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_pipeline pipeline, uint8_t table_id, uint16_t priority,
const char *match, const char *actions)
{
struct ovn_lflow target;
ovn_lflow_init(&target, od, pipeline, table_id, 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);
}
}
/* Appends port security constraints on L2 address field 'eth_addr_field'
* (e.g. "eth.src" or "eth.dst") to 'match'. 'port_security', with
* 'n_port_security' elements, is the collection of port_security constraints
* from an OVN_NB Logical_Port row. */
static void
build_port_security(const char *eth_addr_field,
char **port_security, size_t n_port_security,
struct ds *match)
{
size_t base_len = match->length;
ds_put_format(match, " && %s == {", eth_addr_field);
size_t n = 0;
for (size_t i = 0; i < n_port_security; i++) {
struct eth_addr ea;
if (eth_addr_from_string(port_security[i], &ea)) {
ds_put_format(match, ETH_ADDR_FMT, ETH_ADDR_ARGS(ea));
ds_put_char(match, ' ');
n++;
}
}
ds_chomp(match, ' ');
ds_put_cstr(match, "}");
if (!n) {
match->length = base_len;
}
}
static bool
lport_is_enabled(const struct nbrec_logical_port *lport)
{
return !lport->enabled || *lport->enabled;
}
/* Updates the Logical_Flow and Multicast_Group tables in the OVN_SB database,
* constructing their contents based on the OVN_NB database. */
static void
build_lflows(struct northd_context *ctx, struct hmap *datapaths,
struct hmap *ports)
{
/* This flow table structure is documented in ovn-northd(8), so please
* update ovn-northd.8.xml if you change anything. */
struct hmap lflows = HMAP_INITIALIZER(&lflows);
struct hmap mcgroups = HMAP_INITIALIZER(&mcgroups);
/* Ingress table 0: Admission control framework (priorities 0 and 100). */
struct ovn_datapath *od;
HMAP_FOR_EACH (od, key_node, datapaths) {
/* Logical VLANs not supported. */
ovn_lflow_add(&lflows, od, P_IN, S_IN_PORT_SEC, 100, "vlan.present",
"drop;");
/* Broadcast/multicast source address is invalid. */
ovn_lflow_add(&lflows, od, P_IN, S_IN_PORT_SEC, 100, "eth.src[40]",
"drop;");
/* Port security flows have priority 50 (see below) and will continue
* to the next table if packet source is acceptable. */
}
/* Ingress table 0: Ingress port security (priority 50). */
struct ovn_port *op;
HMAP_FOR_EACH (op, key_node, ports) {
if (!lport_is_enabled(op->nb)) {
/* Drop packets from disabled logical ports (since logical flow
* tables are default-drop). */
continue;
}
struct ds match = DS_EMPTY_INITIALIZER;
ds_put_cstr(&match, "inport == ");
json_string_escape(op->key, &match);
build_port_security("eth.src",
op->nb->port_security, op->nb->n_port_security,
&match);
ovn_lflow_add(&lflows, op->od, P_IN, S_IN_PORT_SEC, 50,
ds_cstr(&match), "next;");
ds_destroy(&match);
}
/* Ingress table 1: ACLs (any priority). */
HMAP_FOR_EACH (od, key_node, datapaths) {
for (size_t i = 0; i < od->nb->n_acls; i++) {
const struct nbrec_acl *acl = od->nb->acls[i];
const char *action;
if (strcmp(acl->direction, "from-lport")) {
continue;
}
action = (!strcmp(acl->action, "allow") ||
!strcmp(acl->action, "allow-related"))
? "next;" : "drop;";
ovn_lflow_add(&lflows, od, P_IN, S_IN_ACL, acl->priority,
acl->match, action);
}
}
HMAP_FOR_EACH (od, key_node, datapaths) {
ovn_lflow_add(&lflows, od, P_IN, S_IN_ACL, 0, "1", "next;");
}
/* Ingress table 2: Destination lookup, broadcast and multicast handling
* (priority 100). */
HMAP_FOR_EACH (op, key_node, ports) {
if (lport_is_enabled(op->nb)) {
ovn_multicast_add(&mcgroups, &mc_flood, op);
}
}
HMAP_FOR_EACH (od, key_node, datapaths) {
ovn_lflow_add(&lflows, od, P_IN, S_IN_L2_LKUP, 100, "eth.dst[40]",
"outport = \""MC_FLOOD"\"; output;");
}
/* Ingress table 2: Destination lookup, unicast handling (priority 50), */
HMAP_FOR_EACH (op, key_node, ports) {
for (size_t i = 0; i < op->nb->n_macs; i++) {
struct eth_addr mac;
if (eth_addr_from_string(op->nb->macs[i], &mac)) {
struct ds match, actions;
ds_init(&match);
ds_put_format(&match, "eth.dst == %s", op->nb->macs[i]);
ds_init(&actions);
ds_put_cstr(&actions, "outport = ");
json_string_escape(op->nb->name, &actions);
ds_put_cstr(&actions, "; output;");
ovn_lflow_add(&lflows, op->od, P_IN, S_IN_L2_LKUP, 50,
ds_cstr(&match), ds_cstr(&actions));
ds_destroy(&actions);
ds_destroy(&match);
} else if (!strcmp(op->nb->macs[i], "unknown")) {
if (lport_is_enabled(op->nb)) {
ovn_multicast_add(&mcgroups, &mc_unknown, op);
op->od->has_unknown = true;
}
} else {
static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
VLOG_INFO_RL(&rl, "%s: invalid syntax '%s' in macs column",
op->nb->name, op->nb->macs[i]);
}
}
}
/* Ingress table 2: Destination lookup for unknown MACs (priority 0). */
HMAP_FOR_EACH (od, key_node, datapaths) {
if (od->has_unknown) {
ovn_lflow_add(&lflows, od, P_IN, S_IN_L2_LKUP, 0, "1",
"outport = \""MC_UNKNOWN"\"; output;");
}
}
/* Egress table 0: ACLs (any priority). */
HMAP_FOR_EACH (od, key_node, datapaths) {
for (size_t i = 0; i < od->nb->n_acls; i++) {
const struct nbrec_acl *acl = od->nb->acls[i];
const char *action;
if (strcmp(acl->direction, "to-lport")) {
continue;
}
action = (!strcmp(acl->action, "allow") ||
!strcmp(acl->action, "allow-related"))
? "next;" : "drop;";
ovn_lflow_add(&lflows, od, P_OUT, S_OUT_ACL, acl->priority,
acl->match, action);
}
}
HMAP_FOR_EACH (od, key_node, datapaths) {
ovn_lflow_add(&lflows, od, P_OUT, S_OUT_ACL, 0, "1", "next;");
}
/* Egress table 1: Egress port security multicast/broadcast (priority
* 100). */
HMAP_FOR_EACH (od, key_node, datapaths) {
ovn_lflow_add(&lflows, od, P_OUT, S_OUT_PORT_SEC, 100, "eth.dst[40]",
"output;");
}
/* Egress table 1: Egress port security (priorities 50 and 150).
*
* Priority 50 rules implement port security for enabled logical port.
*
* Priority 150 rules drop packets to disabled logical ports, so that they
* don't even receive multicast or broadcast packets. */
HMAP_FOR_EACH (op, key_node, ports) {
struct ds match;
ds_init(&match);
ds_put_cstr(&match, "outport == ");
json_string_escape(op->key, &match);
if (lport_is_enabled(op->nb)) {
build_port_security("eth.dst",
op->nb->port_security, op->nb->n_port_security,
&match);
ovn_lflow_add(&lflows, op->od, P_OUT, S_OUT_PORT_SEC, 50,
ds_cstr(&match), "output;");
} else {
ovn_lflow_add(&lflows, op->od, P_OUT, S_OUT_PORT_SEC, 150,
ds_cstr(&match), "drop;");
}
ds_destroy(&match);
}
/* Push changes to the Logical_Flow table to database. */
const struct sbrec_logical_flow *sbflow, *next_sbflow;
SBREC_LOGICAL_FLOW_FOR_EACH_SAFE (sbflow, next_sbflow, ctx->ovnsb_idl) {
struct ovn_datapath *od
= ovn_datapath_from_sbrec(datapaths, sbflow->logical_datapath);
if (!od) {
sbrec_logical_flow_delete(sbflow);
continue;
}
struct ovn_lflow *lflow = ovn_lflow_find(
&lflows, od, (!strcmp(sbflow->pipeline, "ingress") ? P_IN : P_OUT),
sbflow->table_id, sbflow->priority,
sbflow->match, sbflow->actions);
if (lflow) {
ovn_lflow_destroy(&lflows, lflow);
} else {
sbrec_logical_flow_delete(sbflow);
}
}
struct ovn_lflow *lflow, *next_lflow;
HMAP_FOR_EACH_SAFE (lflow, next_lflow, hmap_node, &lflows) {
sbflow = sbrec_logical_flow_insert(ctx->ovnsb_txn);
sbrec_logical_flow_set_logical_datapath(sbflow, lflow->od->sb);
sbrec_logical_flow_set_pipeline(
sbflow, lflow->pipeline == P_IN ? "ingress" : "egress");
sbrec_logical_flow_set_table_id(sbflow, lflow->table_id);
sbrec_logical_flow_set_priority(sbflow, lflow->priority);
sbrec_logical_flow_set_match(sbflow, lflow->match);
sbrec_logical_flow_set_actions(sbflow, lflow->actions);
const struct smap ids = SMAP_CONST1(
&ids, "stage-name",
(lflow->pipeline == P_IN
? ingress_stage_to_str(lflow->table_id)
: egress_stage_to_str(lflow->table_id)));
sbrec_logical_flow_set_external_ids(sbflow, &ids);
ovn_lflow_destroy(&lflows, lflow);
}
hmap_destroy(&lflows);
/* Push changes to the Multicast_Group table to database. */
const struct sbrec_multicast_group *sbmc, *next_sbmc;
SBREC_MULTICAST_GROUP_FOR_EACH_SAFE (sbmc, next_sbmc, ctx->ovnsb_idl) {
struct ovn_datapath *od = ovn_datapath_from_sbrec(datapaths,
sbmc->datapath);
if (!od) {
sbrec_multicast_group_delete(sbmc);
continue;
}
struct multicast_group group = { .name = sbmc->name,
.key = sbmc->tunnel_key };
struct ovn_multicast *mc = ovn_multicast_find(&mcgroups, od, &group);
if (mc) {
ovn_multicast_update_sbrec(mc, sbmc);
ovn_multicast_destroy(&mcgroups, mc);
} else {
sbrec_multicast_group_delete(sbmc);
}
}
struct ovn_multicast *mc, *next_mc;
HMAP_FOR_EACH_SAFE (mc, next_mc, hmap_node, &mcgroups) {
sbmc = sbrec_multicast_group_insert(ctx->ovnsb_txn);
sbrec_multicast_group_set_datapath(sbmc, mc->datapath->sb);
sbrec_multicast_group_set_name(sbmc, mc->group->name);
sbrec_multicast_group_set_tunnel_key(sbmc, mc->group->key);
ovn_multicast_update_sbrec(mc, sbmc);
ovn_multicast_destroy(&mcgroups, mc);
}
hmap_destroy(&mcgroups);
}
static void
ovnnb_db_changed(struct northd_context *ctx)
{
VLOG_DBG("ovn-nb db contents have changed.");
struct hmap datapaths, ports;
build_datapaths(ctx, &datapaths);
build_ports(ctx, &datapaths, &ports);
build_lflows(ctx, &datapaths, &ports);
struct ovn_datapath *dp, *next_dp;
HMAP_FOR_EACH_SAFE (dp, next_dp, key_node, &datapaths) {
ovn_datapath_destroy(&datapaths, dp);
}
hmap_destroy(&datapaths);
struct ovn_port *port, *next_port;
HMAP_FOR_EACH_SAFE (port, next_port, key_node, &ports) {
ovn_port_destroy(&ports, port);
}
hmap_destroy(&ports);
}
/*
* The only change we get notified about is if the 'chassis' column of the
* 'Port_Binding' table changes. When this column is not empty, it means we
* need to set the corresponding logical port as 'up' in the northbound DB.