/
ovn-controller.c
4177 lines (3609 loc) · 152 KB
/
ovn-controller.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (c) 2015, 2016, 2017 Nicira, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <config.h>
#include "ovn-controller.h"
#include <errno.h>
#include <getopt.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include "bfd.h"
#include "binding.h"
#include "chassis.h"
#include "command-line.h"
#include "compiler.h"
#include "daemon.h"
#include "dirs.h"
#include "openvswitch/dynamic-string.h"
#include "encaps.h"
#include "fatal-signal.h"
#include "lib/id-pool.h"
#include "if-status.h"
#include "ip-mcast.h"
#include "openvswitch/hmap.h"
#include "lflow.h"
#include "lflow-cache.h"
#include "lib/vswitch-idl.h"
#include "local_data.h"
#include "lport.h"
#include "memory.h"
#include "ofctrl.h"
#include "ofctrl-seqno.h"
#include "openvswitch/vconn.h"
#include "openvswitch/vlog.h"
#include "ovn/actions.h"
#include "ovn/features.h"
#include "lib/chassis-index.h"
#include "lib/extend-table.h"
#include "lib/ip-mcast-index.h"
#include "lib/mcast-group-index.h"
#include "lib/ovn-sb-idl.h"
#include "lib/ovn-util.h"
#include "patch.h"
#include "physical.h"
#include "pinctrl.h"
#include "openvswitch/poll-loop.h"
#include "lib/bitmap.h"
#include "lib/hash.h"
#include "simap.h"
#include "smap.h"
#include "sset.h"
#include "stream-ssl.h"
#include "stream.h"
#include "unixctl.h"
#include "util.h"
#include "timeval.h"
#include "timer.h"
#include "stopwatch.h"
#include "lib/inc-proc-eng.h"
#include "hmapx.h"
VLOG_DEFINE_THIS_MODULE(main);
static unixctl_cb_func ovn_controller_exit;
static unixctl_cb_func ct_zone_list;
static unixctl_cb_func extend_table_list;
static unixctl_cb_func inject_pkt;
static unixctl_cb_func engine_recompute_cmd;
static unixctl_cb_func cluster_state_reset_cmd;
static unixctl_cb_func debug_pause_execution;
static unixctl_cb_func debug_resume_execution;
static unixctl_cb_func debug_status_execution;
static unixctl_cb_func debug_dump_local_bindings;
static unixctl_cb_func lflow_cache_flush_cmd;
static unixctl_cb_func lflow_cache_show_stats_cmd;
static unixctl_cb_func debug_delay_nb_cfg_report;
#define DEFAULT_BRIDGE_NAME "br-int"
#define DEFAULT_DATAPATH "system"
#define DEFAULT_PROBE_INTERVAL_MSEC 5000
#define OFCTRL_DEFAULT_PROBE_INTERVAL_SEC 0
#define CONTROLLER_LOOP_STOPWATCH_NAME "flow-generation"
#define OFCTRL_PUT_STOPWATCH_NAME "flow-installation"
#define PINCTRL_RUN_STOPWATCH_NAME "pinctrl-run"
#define PATCH_RUN_STOPWATCH_NAME "patch-run"
#define CT_ZONE_COMMIT_STOPWATCH_NAME "ct-zone-commit"
#define IF_STATUS_MGR_RUN_STOPWATCH_NAME "if-status-mgr-run"
#define IF_STATUS_MGR_UPDATE_STOPWATCH_NAME "if-status-mgr-update"
#define OFCTRL_SEQNO_RUN_STOPWATCH_NAME "ofctrl-seqno-run"
#define BFD_RUN_STOPWATCH_NAME "bfd-run"
#define OVS_NB_CFG_NAME "ovn-nb-cfg"
#define OVS_NB_CFG_TS_NAME "ovn-nb-cfg-ts"
#define OVS_STARTUP_TS_NAME "ovn-startup-ts"
static char *parse_options(int argc, char *argv[]);
OVS_NO_RETURN static void usage(void);
/* SSL options */
static const char *ssl_private_key_file;
static const char *ssl_certificate_file;
static const char *ssl_ca_cert_file;
/* By default don't set an upper bound for the lflow cache and enable auto
* trimming above 10K logical flows when reducing cache size by 50%.
*/
#define DEFAULT_LFLOW_CACHE_MAX_ENTRIES UINT32_MAX
#define DEFAULT_LFLOW_CACHE_MAX_MEM_KB (UINT64_MAX / 1024)
#define DEFAULT_LFLOW_CACHE_TRIM_LIMIT 10000
#define DEFAULT_LFLOW_CACHE_WMARK_PERC 50
struct controller_engine_ctx {
struct lflow_cache *lflow_cache;
struct if_status_mgr *if_mgr;
};
/* Pending packet to be injected into connected OVS. */
struct pending_pkt {
/* Setting 'conn' indicates that a request is pending. */
struct unixctl_conn *conn;
char *flow_s;
};
/* Registered ofctrl seqno type for nb_cfg propagation. */
static size_t ofctrl_seq_type_nb_cfg;
static unsigned int
update_sb_monitors(struct ovsdb_idl *ovnsb_idl,
const struct sbrec_chassis *chassis,
const struct sset *local_ifaces,
struct hmap *local_datapaths,
bool monitor_all)
{
/* Monitor Port_Bindings rows for local interfaces and local datapaths.
*
* Monitor Logical_Flow, MAC_Binding, Multicast_Group, and DNS tables for
* local datapaths.
*
* Monitor Controller_Event rows for local chassis.
*
* Monitor IP_Multicast for local datapaths.
*
* Monitor IGMP_Groups for local chassis.
*
* We always monitor patch ports because they allow us to see the linkages
* between related logical datapaths. That way, when we know that we have
* a VIF on a particular logical switch, we immediately know to monitor all
* the connected logical routers and logical switches. */
struct ovsdb_idl_condition pb = OVSDB_IDL_CONDITION_INIT(&pb);
struct ovsdb_idl_condition lf = OVSDB_IDL_CONDITION_INIT(&lf);
struct ovsdb_idl_condition ldpg = OVSDB_IDL_CONDITION_INIT(&ldpg);
struct ovsdb_idl_condition mb = OVSDB_IDL_CONDITION_INIT(&mb);
struct ovsdb_idl_condition mg = OVSDB_IDL_CONDITION_INIT(&mg);
struct ovsdb_idl_condition dns = OVSDB_IDL_CONDITION_INIT(&dns);
struct ovsdb_idl_condition ce = OVSDB_IDL_CONDITION_INIT(&ce);
struct ovsdb_idl_condition ip_mcast = OVSDB_IDL_CONDITION_INIT(&ip_mcast);
struct ovsdb_idl_condition igmp = OVSDB_IDL_CONDITION_INIT(&igmp);
struct ovsdb_idl_condition chprv = OVSDB_IDL_CONDITION_INIT(&chprv);
/* Always monitor all logical datapath groups. Otherwise, DPG updates may
* be received *after* the lflows using it are seen by ovn-controller.
* Since the number of DPGs are relatively small, we monitor all DPGs to
* avoid the unnecessarily extra wake-ups of ovn-controller. */
ovsdb_idl_condition_add_clause_true(&ldpg);
if (monitor_all) {
ovsdb_idl_condition_add_clause_true(&pb);
ovsdb_idl_condition_add_clause_true(&lf);
ovsdb_idl_condition_add_clause_true(&mb);
ovsdb_idl_condition_add_clause_true(&mg);
ovsdb_idl_condition_add_clause_true(&dns);
ovsdb_idl_condition_add_clause_true(&ce);
ovsdb_idl_condition_add_clause_true(&ip_mcast);
ovsdb_idl_condition_add_clause_true(&igmp);
ovsdb_idl_condition_add_clause_true(&chprv);
goto out;
}
sbrec_port_binding_add_clause_type(&pb, OVSDB_F_EQ, "patch");
/* XXX: We can optimize this, if we find a way to only monitor
* ports that have a Gateway_Chassis that point's to our own
* chassis */
sbrec_port_binding_add_clause_type(&pb, OVSDB_F_EQ, "chassisredirect");
sbrec_port_binding_add_clause_type(&pb, OVSDB_F_EQ, "external");
if (chassis) {
/* This should be mostly redundant with the other clauses for port
* bindings, but it allows us to catch any ports that are assigned to
* us but should not be. That way, we can clear their chassis
* assignments. */
sbrec_port_binding_add_clause_chassis(&pb, OVSDB_F_EQ,
&chassis->header_.uuid);
sbrec_port_binding_add_clause_requested_chassis(
&pb, OVSDB_F_EQ, &chassis->header_.uuid);
/* Ensure that we find out about l2gateway and l3gateway ports that
* should be present on this chassis. Otherwise, we might never find
* out about those ports, if their datapaths don't otherwise have a VIF
* in this chassis. */
const char *id = chassis->name;
const struct smap l2 = SMAP_CONST1(&l2, "l2gateway-chassis", id);
sbrec_port_binding_add_clause_options(&pb, OVSDB_F_INCLUDES, &l2);
const struct smap l3 = SMAP_CONST1(&l3, "l3gateway-chassis", id);
sbrec_port_binding_add_clause_options(&pb, OVSDB_F_INCLUDES, &l3);
sbrec_controller_event_add_clause_chassis(&ce, OVSDB_F_EQ,
&chassis->header_.uuid);
sbrec_igmp_group_add_clause_chassis(&igmp, OVSDB_F_EQ,
&chassis->header_.uuid);
/* Monitors Chassis_Private record for current chassis only. */
sbrec_chassis_private_add_clause_name(&chprv, OVSDB_F_EQ,
chassis->name);
} else {
/* During initialization, we monitor all records in Chassis_Private so
* that we don't try to recreate existing ones. */
ovsdb_idl_condition_add_clause_true(&chprv);
}
if (local_ifaces) {
const char *name;
SSET_FOR_EACH (name, local_ifaces) {
sbrec_port_binding_add_clause_logical_port(&pb, OVSDB_F_EQ, name);
sbrec_port_binding_add_clause_parent_port(&pb, OVSDB_F_EQ, name);
}
}
if (local_datapaths) {
const struct local_datapath *ld;
HMAP_FOR_EACH (ld, hmap_node, local_datapaths) {
struct uuid *uuid = CONST_CAST(struct uuid *,
&ld->datapath->header_.uuid);
sbrec_port_binding_add_clause_datapath(&pb, OVSDB_F_EQ, uuid);
sbrec_logical_flow_add_clause_logical_datapath(&lf, OVSDB_F_EQ,
uuid);
sbrec_mac_binding_add_clause_datapath(&mb, OVSDB_F_EQ, uuid);
sbrec_multicast_group_add_clause_datapath(&mg, OVSDB_F_EQ, uuid);
sbrec_dns_add_clause_datapaths(&dns, OVSDB_F_INCLUDES, &uuid, 1);
sbrec_ip_multicast_add_clause_datapath(&ip_mcast, OVSDB_F_EQ,
uuid);
}
/* Datapath groups are immutable, which means a new group record is
* created when a datapath is added to a group. The logical flows
* referencing a datapath group are also updated in such cases but the
* new group UUID is not known by ovn-controller until the SB update
* is received. To avoid unnecessarily removing and adding lflows
* that reference datapath groups, set the monitor condition to always
* request all of them.
*/
sbrec_logical_flow_add_clause_logical_dp_group(&lf, OVSDB_F_NE, NULL);
}
out:;
unsigned int cond_seqnos[] = {
sbrec_port_binding_set_condition(ovnsb_idl, &pb),
sbrec_logical_flow_set_condition(ovnsb_idl, &lf),
sbrec_logical_dp_group_set_condition(ovnsb_idl, &ldpg),
sbrec_mac_binding_set_condition(ovnsb_idl, &mb),
sbrec_multicast_group_set_condition(ovnsb_idl, &mg),
sbrec_dns_set_condition(ovnsb_idl, &dns),
sbrec_controller_event_set_condition(ovnsb_idl, &ce),
sbrec_ip_multicast_set_condition(ovnsb_idl, &ip_mcast),
sbrec_igmp_group_set_condition(ovnsb_idl, &igmp),
sbrec_chassis_private_set_condition(ovnsb_idl, &chprv),
};
unsigned int expected_cond_seqno = 0;
for (size_t i = 0; i < ARRAY_SIZE(cond_seqnos); i++) {
expected_cond_seqno = MAX(expected_cond_seqno, cond_seqnos[i]);
}
ovsdb_idl_condition_destroy(&pb);
ovsdb_idl_condition_destroy(&lf);
ovsdb_idl_condition_destroy(&ldpg);
ovsdb_idl_condition_destroy(&mb);
ovsdb_idl_condition_destroy(&mg);
ovsdb_idl_condition_destroy(&dns);
ovsdb_idl_condition_destroy(&ce);
ovsdb_idl_condition_destroy(&ip_mcast);
ovsdb_idl_condition_destroy(&igmp);
ovsdb_idl_condition_destroy(&chprv);
return expected_cond_seqno;
}
static const char *
br_int_name(const struct ovsrec_open_vswitch *cfg)
{
return smap_get_def(&cfg->external_ids, "ovn-bridge", DEFAULT_BRIDGE_NAME);
}
static const struct ovsrec_bridge *
create_br_int(struct ovsdb_idl_txn *ovs_idl_txn,
const struct ovsrec_open_vswitch_table *ovs_table)
{
const struct ovsrec_open_vswitch *cfg;
cfg = ovsrec_open_vswitch_table_first(ovs_table);
if (!cfg) {
return NULL;
}
const char *bridge_name = br_int_name(cfg);
ovsdb_idl_txn_add_comment(ovs_idl_txn,
"ovn-controller: creating integration bridge '%s'", bridge_name);
struct ovsrec_interface *iface;
iface = ovsrec_interface_insert(ovs_idl_txn);
ovsrec_interface_set_name(iface, bridge_name);
ovsrec_interface_set_type(iface, "internal");
struct ovsrec_port *port;
port = ovsrec_port_insert(ovs_idl_txn);
ovsrec_port_set_name(port, bridge_name);
ovsrec_port_set_interfaces(port, &iface, 1);
struct ovsrec_bridge *bridge;
bridge = ovsrec_bridge_insert(ovs_idl_txn);
ovsrec_bridge_set_name(bridge, bridge_name);
ovsrec_bridge_set_fail_mode(bridge, "secure");
ovsrec_bridge_set_ports(bridge, &port, 1);
struct smap oc = SMAP_INITIALIZER(&oc);
smap_add(&oc, "disable-in-band", "true");
/* When a first non-local port is added to the integration bridge, it
* results in the recalculation of datapath-id by ovs-vswitchd forcing all
* active connections to the controllers to reconnect.
*
* We can avoid the disconnection by setting the 'other_config:hwaddr' for
* the integration bridge. ovs-vswitchd uses this hwaddr to calculate the
* datapath-id and it doesn't recalculate the datapath-id later when the
* first non-local port is added.
*
* So generate a random mac and set the 'hwaddr' option in the
* other_config.
* */
struct eth_addr br_hwaddr;
eth_addr_random(&br_hwaddr);
char ea_s[ETH_ADDR_STRLEN + 1];
snprintf(ea_s, sizeof ea_s, ETH_ADDR_FMT,
ETH_ADDR_ARGS(br_hwaddr));
smap_add(&oc, "hwaddr", ea_s);
ovsrec_bridge_set_other_config(bridge, &oc);
smap_destroy(&oc);
struct ovsrec_bridge **bridges;
size_t bytes = sizeof *bridges * cfg->n_bridges;
bridges = xmalloc(bytes + sizeof *bridges);
memcpy(bridges, cfg->bridges, bytes);
bridges[cfg->n_bridges] = bridge;
ovsrec_open_vswitch_verify_bridges(cfg);
ovsrec_open_vswitch_set_bridges(cfg, bridges, cfg->n_bridges + 1);
free(bridges);
return bridge;
}
static const struct ovsrec_datapath *
create_br_datapath(struct ovsdb_idl_txn *ovs_idl_txn,
const struct ovsrec_open_vswitch *cfg,
const char *datapath_type)
{
ovsdb_idl_txn_add_comment(ovs_idl_txn,
"ovn-controller: creating bridge datapath '%s'",
datapath_type);
struct ovsrec_datapath *dp = ovsrec_datapath_insert(ovs_idl_txn);
ovsrec_open_vswitch_verify_datapaths(cfg);
ovsrec_open_vswitch_update_datapaths_setkey(cfg, datapath_type, dp);
return dp;
}
static const struct ovsrec_bridge *
get_br_int(const struct ovsrec_bridge_table *bridge_table,
const struct ovsrec_open_vswitch_table *ovs_table)
{
const struct ovsrec_open_vswitch *cfg;
cfg = ovsrec_open_vswitch_table_first(ovs_table);
if (!cfg) {
return NULL;
}
return get_bridge(bridge_table, br_int_name(cfg));
}
static const struct ovsrec_datapath *
get_br_datapath(const struct ovsrec_open_vswitch *cfg,
const char *datapath_type)
{
for (size_t i = 0; i < cfg->n_datapaths; i++) {
if (!strcmp(cfg->key_datapaths[i], datapath_type)) {
return cfg->value_datapaths[i];
}
}
return NULL;
}
static void
process_br_int(struct ovsdb_idl_txn *ovs_idl_txn,
const struct ovsrec_bridge_table *bridge_table,
const struct ovsrec_open_vswitch_table *ovs_table,
const struct ovsrec_bridge **br_int_,
const struct ovsrec_datapath **br_int_dp)
{
const struct ovsrec_bridge *br_int = get_br_int(bridge_table, ovs_table);
ovs_assert(br_int_);
if (ovs_idl_txn) {
if (!br_int) {
br_int = create_br_int(ovs_idl_txn, ovs_table);
}
if (br_int) {
const struct ovsrec_open_vswitch *cfg =
ovsrec_open_vswitch_table_first(ovs_table);
ovs_assert(cfg);
/* Propagate "ovn-bridge-datapath-type" from OVS table, if any.
* Otherwise use the datapath-type set in br-int, if any.
* Finally, assume "system" datapath if none configured.
*/
const char *datapath_type =
smap_get(&cfg->external_ids, "ovn-bridge-datapath-type");
if (!datapath_type) {
if (br_int->datapath_type[0]) {
datapath_type = br_int->datapath_type;
} else {
datapath_type = DEFAULT_DATAPATH;
}
}
if (strcmp(br_int->datapath_type, datapath_type)) {
ovsrec_bridge_set_datapath_type(br_int, datapath_type);
}
if (!br_int->fail_mode || strcmp(br_int->fail_mode, "secure")) {
ovsrec_bridge_set_fail_mode(br_int, "secure");
VLOG_WARN("Integration bridge fail-mode changed to 'secure'.");
}
if (br_int_dp) {
*br_int_dp = get_br_datapath(cfg, datapath_type);
if (!(*br_int_dp)) {
*br_int_dp = create_br_datapath(ovs_idl_txn, cfg,
datapath_type);
}
}
}
}
*br_int_ = br_int;
}
static const char *
get_ovs_chassis_id(const struct ovsrec_open_vswitch_table *ovs_table)
{
const struct ovsrec_open_vswitch *cfg
= ovsrec_open_vswitch_table_first(ovs_table);
const char *chassis_id = cfg ? smap_get(&cfg->external_ids, "system-id")
: NULL;
if (!chassis_id) {
static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 1);
VLOG_WARN_RL(&rl, "'system-id' in Open_vSwitch database is missing.");
}
return chassis_id;
}
static void
update_ssl_config(const struct ovsrec_ssl_table *ssl_table)
{
const struct ovsrec_ssl *ssl = ovsrec_ssl_table_first(ssl_table);
if (ssl) {
stream_ssl_set_key_and_cert(ssl->private_key, ssl->certificate);
stream_ssl_set_ca_cert_file(ssl->ca_cert, ssl->bootstrap_ca_cert);
} else {
if (ssl_private_key_file && ssl_certificate_file) {
stream_ssl_set_key_and_cert(ssl_private_key_file,
ssl_certificate_file);
}
if (ssl_ca_cert_file) {
stream_ssl_set_ca_cert_file(ssl_ca_cert_file, false);
}
}
}
static int
get_ofctrl_probe_interval(struct ovsdb_idl *ovs_idl)
{
const struct ovsrec_open_vswitch *cfg = ovsrec_open_vswitch_first(ovs_idl);
return !cfg ? OFCTRL_DEFAULT_PROBE_INTERVAL_SEC :
smap_get_int(&cfg->external_ids,
"ovn-openflow-probe-interval",
OFCTRL_DEFAULT_PROBE_INTERVAL_SEC);
}
/* Retrieves the pointer to the OVN Southbound database from 'ovs_idl' and
* updates 'sbdb_idl' with that pointer. */
static void
update_sb_db(struct ovsdb_idl *ovs_idl, struct ovsdb_idl *ovnsb_idl,
bool *monitor_all_p, bool *reset_ovnsb_idl_min_index,
struct controller_engine_ctx *ctx,
unsigned int *sb_cond_seqno)
{
const struct ovsrec_open_vswitch *cfg = ovsrec_open_vswitch_first(ovs_idl);
if (!cfg) {
return;
}
/* Set remote based on user configuration. */
const char *remote = smap_get(&cfg->external_ids, "ovn-remote");
ovsdb_idl_set_remote(ovnsb_idl, remote, true);
/* Set probe interval, based on user configuration and the remote. */
int default_interval = (remote && !stream_or_pstream_needs_probes(remote)
? 0 : DEFAULT_PROBE_INTERVAL_MSEC);
int interval = smap_get_int(&cfg->external_ids,
"ovn-remote-probe-interval", default_interval);
ovsdb_idl_set_probe_interval(ovnsb_idl, interval);
bool monitor_all = smap_get_bool(&cfg->external_ids, "ovn-monitor-all",
false);
if (monitor_all) {
/* Always call update_sb_monitors when monitor_all is true.
* Otherwise, don't call it here, because there would be unnecessary
* extra cost. Instead, it is called after the engine execution only
* when it is necessary. */
unsigned int next_cond_seqno =
update_sb_monitors(ovnsb_idl, NULL, NULL, NULL, true);
if (sb_cond_seqno) {
*sb_cond_seqno = next_cond_seqno;
}
}
if (monitor_all_p) {
*monitor_all_p = monitor_all;
}
if (reset_ovnsb_idl_min_index && *reset_ovnsb_idl_min_index) {
VLOG_INFO("Resetting southbound database cluster state");
engine_set_force_recompute(true);
ovsdb_idl_reset_min_index(ovnsb_idl);
*reset_ovnsb_idl_min_index = false;
}
if (ctx) {
lflow_cache_enable(ctx->lflow_cache,
smap_get_bool(&cfg->external_ids,
"ovn-enable-lflow-cache",
true),
smap_get_uint(&cfg->external_ids,
"ovn-limit-lflow-cache",
DEFAULT_LFLOW_CACHE_MAX_ENTRIES),
smap_get_ullong(&cfg->external_ids,
"ovn-memlimit-lflow-cache-kb",
DEFAULT_LFLOW_CACHE_MAX_MEM_KB),
smap_get_uint(&cfg->external_ids,
"ovn-trim-limit-lflow-cache",
DEFAULT_LFLOW_CACHE_TRIM_LIMIT),
smap_get_uint(&cfg->external_ids,
"ovn-trim-wmark-perc-lflow-cache",
DEFAULT_LFLOW_CACHE_WMARK_PERC));
}
}
static void
add_pending_ct_zone_entry(struct shash *pending_ct_zones,
enum ct_zone_pending_state state,
int zone, bool add, const char *name)
{
VLOG_DBG("%s ct zone %"PRId32" for '%s'",
add ? "assigning" : "removing", zone, name);
struct ct_zone_pending_entry *pending = xmalloc(sizeof *pending);
pending->state = state; /* Skip flushing zone. */
pending->zone = zone;
pending->add = add;
/* Its important that we add only one entry for the key 'name'.
* Replace 'pending' with 'existing' and free up 'existing'.
* Otherwise, we may end up in a continuous loop of adding
* and deleting the zone entry in the 'external_ids' of
* integration bridge.
*/
struct ct_zone_pending_entry *existing =
shash_replace(pending_ct_zones, name, pending);
if (existing) {
free(existing);
}
}
static bool
alloc_id_to_ct_zone(const char *zone_name, struct simap *ct_zones,
unsigned long *ct_zone_bitmap, int *scan_start,
struct shash *pending_ct_zones)
{
/* We assume that there are 64K zones and that we own them all. */
int zone = bitmap_scan(ct_zone_bitmap, 0, *scan_start, MAX_CT_ZONES + 1);
if (zone == MAX_CT_ZONES + 1) {
static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
VLOG_WARN_RL(&rl, "exhausted all ct zones");
return false;
}
*scan_start = zone + 1;
add_pending_ct_zone_entry(pending_ct_zones, CT_ZONE_OF_QUEUED,
zone, true, zone_name);
bitmap_set1(ct_zone_bitmap, zone);
simap_put(ct_zones, zone_name, zone);
return true;
}
static void
update_ct_zones(const struct shash *binding_lports,
const struct hmap *local_datapaths,
struct simap *ct_zones, unsigned long *ct_zone_bitmap,
struct shash *pending_ct_zones)
{
struct simap_node *ct_zone, *ct_zone_next;
int scan_start = 1;
const char *user;
struct sset all_users = SSET_INITIALIZER(&all_users);
struct simap req_snat_zones = SIMAP_INITIALIZER(&req_snat_zones);
unsigned long unreq_snat_zones[BITMAP_N_LONGS(MAX_CT_ZONES)];
struct shash_node *shash_node;
SHASH_FOR_EACH (shash_node, binding_lports) {
sset_add(&all_users, shash_node->name);
}
/* Local patched datapath (gateway routers) need zones assigned. */
struct shash all_lds = SHASH_INITIALIZER(&all_lds);
const struct local_datapath *ld;
HMAP_FOR_EACH (ld, hmap_node, local_datapaths) {
/* XXX Add method to limit zone assignment to logical router
* datapaths with NAT */
char *dnat = alloc_nat_zone_key(&ld->datapath->header_.uuid, "dnat");
char *snat = alloc_nat_zone_key(&ld->datapath->header_.uuid, "snat");
sset_add(&all_users, dnat);
sset_add(&all_users, snat);
shash_add(&all_lds, dnat, ld);
shash_add(&all_lds, snat, ld);
int req_snat_zone = datapath_snat_ct_zone(ld->datapath);
if (req_snat_zone >= 0) {
simap_put(&req_snat_zones, snat, req_snat_zone);
}
free(dnat);
free(snat);
}
/* Delete zones that do not exist in above sset. */
SIMAP_FOR_EACH_SAFE(ct_zone, ct_zone_next, ct_zones) {
if (!sset_contains(&all_users, ct_zone->name)) {
VLOG_DBG("removing ct zone %"PRId32" for '%s'",
ct_zone->data, ct_zone->name);
add_pending_ct_zone_entry(pending_ct_zones, CT_ZONE_DB_QUEUED,
ct_zone->data, false, ct_zone->name);
bitmap_set0(ct_zone_bitmap, ct_zone->data);
simap_delete(ct_zones, ct_zone);
} else if (!simap_find(&req_snat_zones, ct_zone->name)) {
bitmap_set1(unreq_snat_zones, ct_zone->data);
}
}
/* Prioritize requested CT zones */
struct simap_node *snat_req_node;
SIMAP_FOR_EACH (snat_req_node, &req_snat_zones) {
struct simap_node *node = simap_find(ct_zones, snat_req_node->name);
if (node) {
if (node->data == snat_req_node->data) {
/* No change to this request, so no action needed */
continue;
} else {
/* Zone request has changed for this node. delete old entry */
bitmap_set0(ct_zone_bitmap, node->data);
simap_delete(ct_zones, node);
}
}
/* Determine if someone already had this zone auto-assigned.
* If so, then they need to give up their assignment since
* that zone is being explicitly requested now.
*/
if (bitmap_is_set(unreq_snat_zones, snat_req_node->data)) {
struct simap_node *dup;
struct simap_node *next;
SIMAP_FOR_EACH_SAFE (dup, next, ct_zones) {
if (dup != snat_req_node && dup->data == snat_req_node->data) {
simap_delete(ct_zones, dup);
break;
}
}
/* Set this bit to 0 so that if multiple datapaths have requested
* this zone, we don't needlessly double-detect this condition.
*/
bitmap_set0(unreq_snat_zones, snat_req_node->data);
}
add_pending_ct_zone_entry(pending_ct_zones, CT_ZONE_OF_QUEUED,
snat_req_node->data, true,
snat_req_node->name);
bitmap_set1(ct_zone_bitmap, snat_req_node->data);
simap_put(ct_zones, snat_req_node->name, snat_req_node->data);
}
/* xxx This is wasteful to assign a zone to each port--even if no
* xxx security policy is applied. */
/* Assign a unique zone id for each logical port and two zones
* to a gateway router. */
SSET_FOR_EACH(user, &all_users) {
if (simap_contains(ct_zones, user)) {
continue;
}
alloc_id_to_ct_zone(user, ct_zones, ct_zone_bitmap, &scan_start,
pending_ct_zones);
}
simap_destroy(&req_snat_zones);
sset_destroy(&all_users);
shash_destroy(&all_lds);
}
static void
commit_ct_zones(const struct ovsrec_bridge *br_int,
struct shash *pending_ct_zones)
{
struct shash_node *iter;
SHASH_FOR_EACH(iter, pending_ct_zones) {
struct ct_zone_pending_entry *ctzpe = iter->data;
/* The transaction is open, so any pending entries in the
* CT_ZONE_DB_QUEUED must be sent and any in CT_ZONE_DB_QUEUED
* need to be retried. */
if (ctzpe->state != CT_ZONE_DB_QUEUED
&& ctzpe->state != CT_ZONE_DB_SENT) {
continue;
}
char *user_str = xasprintf("ct-zone-%s", iter->name);
if (ctzpe->add) {
char *zone_str = xasprintf("%"PRId32, ctzpe->zone);
struct smap_node *node =
smap_get_node(&br_int->external_ids, user_str);
if (!node || strcmp(node->value, zone_str)) {
ovsrec_bridge_update_external_ids_setkey(br_int,
user_str, zone_str);
}
free(zone_str);
} else {
if (smap_get(&br_int->external_ids, user_str)) {
ovsrec_bridge_update_external_ids_delkey(br_int, user_str);
}
}
free(user_str);
ctzpe->state = CT_ZONE_DB_SENT;
}
}
static void
restore_ct_zones(const struct ovsrec_bridge_table *bridge_table,
const struct ovsrec_open_vswitch_table *ovs_table,
struct simap *ct_zones, unsigned long *ct_zone_bitmap)
{
const struct ovsrec_open_vswitch *cfg;
cfg = ovsrec_open_vswitch_table_first(ovs_table);
if (!cfg) {
return;
}
const struct ovsrec_bridge *br_int;
br_int = get_bridge(bridge_table, br_int_name(cfg));
if (!br_int) {
/* If the integration bridge hasn't been defined, assume that
* any existing ct-zone definitions aren't valid. */
return;
}
struct smap_node *node;
SMAP_FOR_EACH(node, &br_int->external_ids) {
if (strncmp(node->key, "ct-zone-", 8)) {
continue;
}
const char *user = node->key + 8;
int zone = atoi(node->value);
if (user[0] && zone) {
VLOG_DBG("restoring ct zone %"PRId32" for '%s'", zone, user);
bitmap_set1(ct_zone_bitmap, zone);
simap_put(ct_zones, user, zone);
}
}
}
static uint64_t
get_nb_cfg(const struct sbrec_sb_global_table *sb_global_table,
unsigned int cond_seqno, unsigned int expected_cond_seqno)
{
static uint64_t nb_cfg = 0;
/* Delay getting nb_cfg if there are monitor condition changes
* in flight. It might be that those changes would instruct the
* server to send updates that happened before SB_Global.nb_cfg.
*/
if (cond_seqno != expected_cond_seqno) {
return nb_cfg;
}
const struct sbrec_sb_global *sb
= sbrec_sb_global_table_first(sb_global_table);
nb_cfg = sb ? sb->nb_cfg : 0;
return nb_cfg;
}
/* Propagates the local cfg seqno, 'cur_cfg', to the chassis_private record
* and to the local OVS DB.
*/
static void
store_nb_cfg(struct ovsdb_idl_txn *sb_txn, struct ovsdb_idl_txn *ovs_txn,
const struct sbrec_chassis_private *chassis,
const struct ovsrec_bridge *br_int,
unsigned int delay_nb_cfg_report, int64_t startup_ts)
{
struct ofctrl_acked_seqnos *acked_nb_cfg_seqnos =
ofctrl_acked_seqnos_get(ofctrl_seq_type_nb_cfg);
uint64_t cur_cfg = acked_nb_cfg_seqnos->last_acked;
if (ovs_txn && br_int
&& startup_ts != smap_get_ullong(&br_int->external_ids,
OVS_STARTUP_TS_NAME, 0)) {
char *startup_ts_str = xasprintf("%"PRId64, startup_ts);
ovsrec_bridge_update_external_ids_setkey(br_int, OVS_STARTUP_TS_NAME,
startup_ts_str);
free(startup_ts_str);
}
if (!cur_cfg) {
goto done;
}
long long ts_now = time_wall_msec();
if (sb_txn && chassis && cur_cfg != chassis->nb_cfg) {
sbrec_chassis_private_set_nb_cfg(chassis, cur_cfg);
sbrec_chassis_private_set_nb_cfg_timestamp(chassis, ts_now);
if (delay_nb_cfg_report) {
VLOG_INFO("Sleep for %u sec", delay_nb_cfg_report);
xsleep(delay_nb_cfg_report);
}
}
if (ovs_txn && br_int && cur_cfg != smap_get_ullong(&br_int->external_ids,
OVS_NB_CFG_NAME, 0)) {
char *cur_cfg_ts_str = xasprintf("%lld", ts_now);
char *cur_cfg_str = xasprintf("%"PRId64, cur_cfg);
ovsrec_bridge_update_external_ids_setkey(br_int, OVS_NB_CFG_NAME,
cur_cfg_str);
ovsrec_bridge_update_external_ids_setkey(br_int, OVS_NB_CFG_TS_NAME,
cur_cfg_ts_str);
free(cur_cfg_ts_str);
free(cur_cfg_str);
}
done:
ofctrl_acked_seqnos_destroy(acked_nb_cfg_seqnos);
}
static const char *
get_transport_zones(const struct ovsrec_open_vswitch_table *ovs_table)
{
const struct ovsrec_open_vswitch *cfg
= ovsrec_open_vswitch_table_first(ovs_table);
return smap_get_def(&cfg->external_ids, "ovn-transport-zones", "");
}
static void
ctrl_register_ovs_idl(struct ovsdb_idl *ovs_idl)
{
/* We do not monitor all tables by default, so modules must register
* their interest explicitly. */
ovsdb_idl_add_table(ovs_idl, &ovsrec_table_open_vswitch);
ovsdb_idl_add_column(ovs_idl, &ovsrec_open_vswitch_col_external_ids);
ovsdb_idl_add_column(ovs_idl, &ovsrec_open_vswitch_col_other_config);
ovsdb_idl_add_column(ovs_idl, &ovsrec_open_vswitch_col_bridges);
ovsdb_idl_add_column(ovs_idl, &ovsrec_open_vswitch_col_datapaths);
ovsdb_idl_add_table(ovs_idl, &ovsrec_table_interface);
ovsdb_idl_track_add_column(ovs_idl, &ovsrec_interface_col_name);
ovsdb_idl_track_add_column(ovs_idl, &ovsrec_interface_col_bfd);
ovsdb_idl_track_add_column(ovs_idl, &ovsrec_interface_col_bfd_status);
ovsdb_idl_track_add_column(ovs_idl, &ovsrec_interface_col_type);
ovsdb_idl_track_add_column(ovs_idl, &ovsrec_interface_col_options);
ovsdb_idl_track_add_column(ovs_idl, &ovsrec_interface_col_ofport);
ovsdb_idl_add_table(ovs_idl, &ovsrec_table_port);
ovsdb_idl_track_add_column(ovs_idl, &ovsrec_port_col_name);
ovsdb_idl_track_add_column(ovs_idl, &ovsrec_port_col_interfaces);
ovsdb_idl_track_add_column(ovs_idl, &ovsrec_port_col_external_ids);
ovsdb_idl_add_table(ovs_idl, &ovsrec_table_bridge);
ovsdb_idl_add_column(ovs_idl, &ovsrec_bridge_col_ports);
ovsdb_idl_add_column(ovs_idl, &ovsrec_bridge_col_name);
ovsdb_idl_add_column(ovs_idl, &ovsrec_bridge_col_fail_mode);
ovsdb_idl_add_column(ovs_idl, &ovsrec_bridge_col_other_config);
ovsdb_idl_add_column(ovs_idl, &ovsrec_bridge_col_external_ids);
ovsdb_idl_add_table(ovs_idl, &ovsrec_table_ssl);
ovsdb_idl_add_column(ovs_idl, &ovsrec_ssl_col_bootstrap_ca_cert);
ovsdb_idl_add_column(ovs_idl, &ovsrec_ssl_col_ca_cert);
ovsdb_idl_add_column(ovs_idl, &ovsrec_ssl_col_certificate);
ovsdb_idl_add_column(ovs_idl, &ovsrec_ssl_col_private_key);
ovsdb_idl_add_table(ovs_idl, &ovsrec_table_datapath);
ovsdb_idl_add_column(ovs_idl, &ovsrec_datapath_col_capabilities);
chassis_register_ovs_idl(ovs_idl);
encaps_register_ovs_idl(ovs_idl);
binding_register_ovs_idl(ovs_idl);
bfd_register_ovs_idl(ovs_idl);
physical_register_ovs_idl(ovs_idl);
}
#define SB_NODES \
SB_NODE(chassis, "chassis") \
SB_NODE(encap, "encap") \
SB_NODE(address_set, "address_set") \
SB_NODE(port_group, "port_group") \
SB_NODE(multicast_group, "multicast_group") \
SB_NODE(datapath_binding, "datapath_binding") \
SB_NODE(logical_dp_group, "logical_dp_group") \
SB_NODE(port_binding, "port_binding") \
SB_NODE(mac_binding, "mac_binding") \
SB_NODE(logical_flow, "logical_flow") \
SB_NODE(dhcp_options, "dhcp_options") \
SB_NODE(dhcpv6_options, "dhcpv6_options") \
SB_NODE(dns, "dns") \
SB_NODE(load_balancer, "load_balancer") \
SB_NODE(fdb, "fdb")
enum sb_engine_node {
#define SB_NODE(NAME, NAME_STR) SB_##NAME,
SB_NODES
#undef SB_NODE
};
#define SB_NODE(NAME, NAME_STR) ENGINE_FUNC_SB(NAME);
SB_NODES
#undef SB_NODE
#define OVS_NODES \
OVS_NODE(open_vswitch, "open_vswitch") \
OVS_NODE(bridge, "bridge") \
OVS_NODE(port, "port") \
OVS_NODE(interface, "interface") \
OVS_NODE(qos, "qos")
enum ovs_engine_node {
#define OVS_NODE(NAME, NAME_STR) OVS_##NAME,
OVS_NODES
#undef OVS_NODE
};
#define OVS_NODE(NAME, NAME_STR) ENGINE_FUNC_OVS(NAME);
OVS_NODES
#undef OVS_NODE
struct ed_type_ofctrl_is_connected {
bool connected;
};
static void *
en_ofctrl_is_connected_init(struct engine_node *node OVS_UNUSED,
struct engine_arg *arg OVS_UNUSED)
{
struct ed_type_ofctrl_is_connected *data = xzalloc(sizeof *data);
return data;
}
static void
en_ofctrl_is_connected_cleanup(void *data OVS_UNUSED)
{
}