forked from openvswitch/ovs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtep-ctl.c
2551 lines (2135 loc) · 77.1 KB
/
vtep-ctl.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) 2009, 2010, 2011, 2012, 2014, 2015, 2016 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 <ctype.h>
#include <errno.h>
#include <float.h>
#include <getopt.h>
#include <inttypes.h>
#include <signal.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "db-ctl-base.h"
#include "command-line.h"
#include "compiler.h"
#include "openvswitch/dynamic-string.h"
#include "fatal-signal.h"
#include "hash.h"
#include "openvswitch/json.h"
#include "ovsdb-data.h"
#include "ovsdb-idl.h"
#include "poll-loop.h"
#include "process.h"
#include "stream.h"
#include "stream-ssl.h"
#include "smap.h"
#include "sset.h"
#include "svec.h"
#include "vtep/vtep-idl.h"
#include "table.h"
#include "timeval.h"
#include "util.h"
#include "openvswitch/vconn.h"
#include "openvswitch/vlog.h"
VLOG_DEFINE_THIS_MODULE(vtep_ctl);
struct vtep_ctl_context;
/* --db: The database server to contact. */
static const char *db;
/* --oneline: Write each command's output as a single line? */
static bool oneline;
/* --dry-run: Do not commit any changes. */
static bool dry_run;
/* --timeout: Time to wait for a connection to 'db'. */
static int timeout;
/* Format for table output. */
static struct table_style table_style = TABLE_STYLE_DEFAULT;
/* The IDL we're using and the current transaction, if any.
* This is for use by vtep_ctl_exit() only, to allow it to clean up.
* Other code should use its context arguments. */
static struct ovsdb_idl *the_idl;
static struct ovsdb_idl_txn *the_idl_txn;
OVS_NO_RETURN static void vtep_ctl_exit(int status);
static void vtep_ctl_cmd_init(void);
OVS_NO_RETURN static void usage(void);
static void parse_options(int argc, char *argv[], struct shash *local_options);
static void run_prerequisites(struct ctl_command[], size_t n_commands,
struct ovsdb_idl *);
static void do_vtep_ctl(const char *args, struct ctl_command *, size_t n,
struct ovsdb_idl *);
static struct vtep_ctl_lswitch *find_lswitch(struct vtep_ctl_context *,
const char *name,
bool must_exist);
static struct vtep_ctl_lrouter *find_lrouter(struct vtep_ctl_context *,
const char *name,
bool must_exist);
int
main(int argc, char *argv[])
{
struct ovsdb_idl *idl;
struct ctl_command *commands;
struct shash local_options;
unsigned int seqno;
size_t n_commands;
char *args;
set_program_name(argv[0]);
fatal_ignore_sigpipe();
vlog_set_levels(NULL, VLF_CONSOLE, VLL_WARN);
vlog_set_levels_from_string_assert("reconnect:warn");
vteprec_init();
vtep_ctl_cmd_init();
/* Log our arguments. This is often valuable for debugging systems. */
args = process_escape_args(argv);
VLOG(ctl_might_write_to_db(argv) ? VLL_INFO : VLL_DBG, "Called as %s", args);
/* Parse command line. */
shash_init(&local_options);
parse_options(argc, argv, &local_options);
commands = ctl_parse_commands(argc - optind, argv + optind, &local_options,
&n_commands);
if (timeout) {
time_alarm(timeout);
}
/* Initialize IDL. */
idl = the_idl = ovsdb_idl_create(db, &vteprec_idl_class, false, false);
run_prerequisites(commands, n_commands, idl);
/* Execute the commands.
*
* 'seqno' is the database sequence number for which we last tried to
* execute our transaction. There's no point in trying to commit more than
* once for any given sequence number, because if the transaction fails
* it's because the database changed and we need to obtain an up-to-date
* view of the database before we try the transaction again. */
seqno = ovsdb_idl_get_seqno(idl);
for (;;) {
ovsdb_idl_run(idl);
if (!ovsdb_idl_is_alive(idl)) {
int retval = ovsdb_idl_get_last_error(idl);
ctl_fatal("%s: database connection failed (%s)",
db, ovs_retval_to_string(retval));
}
if (seqno != ovsdb_idl_get_seqno(idl)) {
seqno = ovsdb_idl_get_seqno(idl);
do_vtep_ctl(args, commands, n_commands, idl);
}
if (seqno == ovsdb_idl_get_seqno(idl)) {
ovsdb_idl_wait(idl);
poll_block();
}
}
}
static void
parse_options(int argc, char *argv[], struct shash *local_options)
{
enum {
OPT_DB = UCHAR_MAX + 1,
OPT_ONELINE,
OPT_NO_SYSLOG,
OPT_DRY_RUN,
OPT_PEER_CA_CERT,
OPT_LOCAL,
VLOG_OPTION_ENUMS,
TABLE_OPTION_ENUMS
};
static const struct option global_long_options[] = {
{"db", required_argument, NULL, OPT_DB},
{"no-syslog", no_argument, NULL, OPT_NO_SYSLOG},
{"dry-run", no_argument, NULL, OPT_DRY_RUN},
{"oneline", no_argument, NULL, OPT_ONELINE},
{"timeout", required_argument, NULL, 't'},
{"help", no_argument, NULL, 'h'},
{"version", no_argument, NULL, 'V'},
VLOG_LONG_OPTIONS,
TABLE_LONG_OPTIONS,
STREAM_SSL_LONG_OPTIONS,
{"peer-ca-cert", required_argument, NULL, OPT_PEER_CA_CERT},
{NULL, 0, NULL, 0},
};
const int n_global_long_options = ARRAY_SIZE(global_long_options) - 1;
char *tmp, *short_options;
struct option *options;
size_t allocated_options;
size_t n_options;
size_t i;
tmp = ovs_cmdl_long_options_to_short_options(global_long_options);
short_options = xasprintf("+%s", tmp);
free(tmp);
/* We want to parse both global and command-specific options here, but
* getopt_long() isn't too convenient for the job. We copy our global
* options into a dynamic array, then append all of the command-specific
* options. */
options = xmemdup(global_long_options, sizeof global_long_options);
allocated_options = ARRAY_SIZE(global_long_options);
n_options = n_global_long_options;
ctl_add_cmd_options(&options, &n_options, &allocated_options, OPT_LOCAL);
table_style.format = TF_LIST;
for (;;) {
int idx;
int c;
c = getopt_long(argc, argv, short_options, options, &idx);
if (c == -1) {
break;
}
switch (c) {
case OPT_DB:
db = optarg;
break;
case OPT_ONELINE:
oneline = true;
break;
case OPT_NO_SYSLOG:
vlog_set_levels(&this_module, VLF_SYSLOG, VLL_WARN);
break;
case OPT_DRY_RUN:
dry_run = true;
break;
case OPT_LOCAL:
if (shash_find(local_options, options[idx].name)) {
ctl_fatal("'%s' option specified multiple times",
options[idx].name);
}
shash_add_nocopy(local_options,
xasprintf("--%s", options[idx].name),
nullable_xstrdup(optarg));
break;
case 'h':
usage();
case 'V':
ovs_print_version(0, 0);
printf("DB Schema %s\n", vteprec_get_db_version());
exit(EXIT_SUCCESS);
case 't':
timeout = strtoul(optarg, NULL, 10);
if (timeout < 0) {
ctl_fatal("value %s on -t or --timeout is invalid",
optarg);
}
break;
VLOG_OPTION_HANDLERS
TABLE_OPTION_HANDLERS(&table_style)
STREAM_SSL_OPTION_HANDLERS
case OPT_PEER_CA_CERT:
stream_ssl_set_peer_ca_cert_file(optarg);
break;
case '?':
exit(EXIT_FAILURE);
default:
abort();
}
}
free(short_options);
if (!db) {
db = ctl_default_db();
}
for (i = n_global_long_options; options[i].name; i++) {
free(CONST_CAST(char *, options[i].name));
}
free(options);
}
/* Frees the current transaction and the underlying IDL and then calls
* exit(status).
*
* Freeing the transaction and the IDL is not strictly necessary, but it makes
* for a clean memory leak report from valgrind in the normal case. That makes
* it easier to notice real memory leaks. */
static void
vtep_ctl_exit(int status)
{
if (the_idl_txn) {
ovsdb_idl_txn_abort(the_idl_txn);
ovsdb_idl_txn_destroy(the_idl_txn);
}
ovsdb_idl_destroy(the_idl);
exit(status);
}
static void
usage(void)
{
printf("\
%s: VTEP configuration utility\n\
usage: %s [OPTIONS] COMMAND [ARG...]\n\
\n\
VTEP commands:\n\
show print overview of database contents\n\
\n\
Manager commands:\n\
get-manager print the managers\n\
del-manager delete the managers\n\
set-manager TARGET... set the list of managers to TARGET...\n\
\n\
Physical Switch commands:\n\
add-ps PS create a new physical switch named PS\n\
del-ps PS delete PS and all of its ports\n\
list-ps print the names of all the physical switches\n\
ps-exists PS exit 2 if PS does not exist\n\
\n\
Port commands:\n\
list-ports PS print the names of all the ports on PS\n\
add-port PS PORT add network device PORT to PS\n\
del-port PS PORT delete PORT from PS\n\
\n\
Logical Switch commands:\n\
add-ls LS create a new logical switch named LS\n\
del-ls LS delete LS and all of its ports\n\
list-ls print the names of all the logical switches\n\
ls-exists LS exit 2 if LS does not exist\n\
bind-ls PS PORT VLAN LS bind LS to VLAN on PORT\n\
unbind-ls PS PORT VLAN unbind logical switch on VLAN from PORT\n\
list-bindings PS PORT list bindings for PORT on PS\n\
set-replication-mode LS MODE set replication mode on LS\n\
get-replication-mode LS get replication mode on LS\n\
\n\
Logical Router commands:\n\
add-lr LR create a new logical router named LR\n\
del-lr LR delete LR\n\
list-lr print the names of all the logical routers\n\
lr-exists LR exit 2 if LR does not exist\n\
\n\
MAC binding commands:\n\
add-ucast-local LS MAC [ENCAP] IP add ucast local entry in LS\n\
del-ucast-local LS MAC del ucast local entry from LS\n\
add-mcast-local LS MAC [ENCAP] IP add mcast local entry in LS\n\
del-mcast-local LS MAC [ENCAP] IP del mcast local entry from LS\n\
clear-local-macs LS clear local mac entries\n\
list-local-macs LS list local mac entries\n\
add-ucast-remote LS MAC [ENCAP] IP add ucast remote entry in LS\n\
del-ucast-remote LS MAC del ucast remote entry from LS\n\
add-mcast-remote LS MAC [ENCAP] IP add mcast remote entry in LS\n\
del-mcast-remote LS MAC [ENCAP] IP del mcast remote entry from LS\n\
clear-remote-macs LS clear remote mac entries\n\
list-remote-macs LS list remote mac entries\n\
\n\
%s\
\n\
Options:\n\
--db=DATABASE connect to DATABASE\n\
(default: %s)\n\
-t, --timeout=SECS wait at most SECS seconds\n\
--dry-run do not commit changes to database\n\
--oneline print exactly one line of output per command\n",
program_name, program_name, ctl_get_db_cmd_usage(), ctl_default_db());
vlog_usage();
printf("\
--no-syslog equivalent to --verbose=vtep_ctl:syslog:warn\n");
stream_usage("database", true, true, false);
printf("\n\
Other options:\n\
-h, --help display this help message\n\
-V, --version display version information\n");
exit(EXIT_SUCCESS);
}
static struct cmd_show_table cmd_show_tables[] = {
{&vteprec_table_global,
NULL,
{&vteprec_global_col_managers,
&vteprec_global_col_switches,
NULL},
{NULL, NULL, NULL}
},
{&vteprec_table_manager,
&vteprec_manager_col_target,
{&vteprec_manager_col_is_connected,
NULL,
NULL},
{NULL, NULL, NULL}
},
{&vteprec_table_physical_switch,
&vteprec_physical_switch_col_name,
{&vteprec_physical_switch_col_management_ips,
&vteprec_physical_switch_col_tunnel_ips,
&vteprec_physical_switch_col_ports},
{NULL, NULL, NULL}
},
{&vteprec_table_physical_port,
&vteprec_physical_port_col_name,
{&vteprec_physical_port_col_vlan_bindings,
NULL,
NULL},
{NULL, NULL, NULL}
},
{&vteprec_table_logical_switch,
&vteprec_logical_switch_col_name,
{NULL,
NULL,
NULL},
{NULL, NULL, NULL}
},
{NULL, NULL, {NULL, NULL, NULL}, {NULL, NULL, NULL}}
};
/* vtep-ctl specific context. Inherits the 'struct ctl_context' as base. */
struct vtep_ctl_context {
struct ctl_context base;
/* Modifiable state. */
const struct vteprec_global *vtep_global;
bool verified_ports;
/* A cache of the contents of the database.
*
* A command that needs to use any of this information must first
* call vtep_ctl_context_populate_cache(). A command that changes
* anything that could invalidate the cache must either call
* vtep_ctl_context_invalidate_cache() or manually update the cache
* to maintain its correctness. */
bool cache_valid;
struct shash pswitches; /* Maps from physical switch name to
* struct vtep_ctl_pswitch. */
struct shash ports; /* Maps from port name to struct vtep_ctl_port. */
struct shash lswitches; /* Maps from logical switch name to
* struct vtep_ctl_lswitch. */
struct shash plocs; /* Maps from "<encap>+<dst_ip>" to
* struct vteprec_physical_locator. */
struct shash lrouters; /* Maps from logical router name to
* struct vtep_ctl_lrouter. */
};
/* Casts 'base' into 'struct vtep_ctl_context'. */
static struct vtep_ctl_context *
vtep_ctl_context_cast(struct ctl_context *base)
{
return CONTAINER_OF(base, struct vtep_ctl_context, base);
}
struct vtep_ctl_pswitch {
const struct vteprec_physical_switch *ps_cfg;
char *name;
struct ovs_list ports; /* Contains "struct vteprec_physical_port"s. */
};
struct vtep_ctl_port {
struct ovs_list ports_node; /* In struct vtep_ctl_pswitch's 'ports' list. */
const struct vteprec_physical_port *port_cfg;
struct vtep_ctl_pswitch *ps;
struct shash bindings; /* Maps from vlan to vtep_ctl_lswitch. */
};
struct vtep_ctl_lswitch {
const struct vteprec_logical_switch *ls_cfg;
char *name;
struct shash ucast_local; /* Maps from mac to vteprec_ucast_macs_local. */
struct shash ucast_remote; /* Maps from mac to vteprec_ucast_macs_remote.*/
struct shash mcast_local; /* Maps from mac to vtep_ctl_mcast_mac. */
struct shash mcast_remote; /* Maps from mac to vtep_ctl_mcast_mac. */
};
struct vtep_ctl_lrouter {
const struct vteprec_logical_router *lr_cfg;
char *name;
};
struct vtep_ctl_mcast_mac {
const struct vteprec_mcast_macs_local *local_cfg;
const struct vteprec_mcast_macs_remote *remote_cfg;
const struct vteprec_physical_locator_set *ploc_set_cfg;
struct ovs_list locators; /* Contains 'vtep_ctl_ploc's. */
};
struct vtep_ctl_ploc {
struct ovs_list locators_node; /* In struct vtep_ctl_ploc_set's 'locators'
list. */
const struct vteprec_physical_locator *ploc_cfg;
};
static void
verify_ports(struct vtep_ctl_context *vtepctl_ctx)
{
if (!vtepctl_ctx->verified_ports) {
const struct vteprec_physical_switch *ps;
vteprec_global_verify_switches(vtepctl_ctx->vtep_global);
VTEPREC_PHYSICAL_SWITCH_FOR_EACH (ps, vtepctl_ctx->base.idl) {
vteprec_physical_switch_verify_ports(ps);
}
vtepctl_ctx->verified_ports = true;
}
}
static struct vtep_ctl_port *
add_port_to_cache(struct vtep_ctl_context *vtepctl_ctx,
struct vtep_ctl_pswitch *ps,
struct vteprec_physical_port *port_cfg)
{
char *cache_name = xasprintf("%s+%s", ps->name, port_cfg->name);
struct vtep_ctl_port *port;
port = xmalloc(sizeof *port);
ovs_list_push_back(&ps->ports, &port->ports_node);
port->port_cfg = port_cfg;
port->ps = ps;
shash_add(&vtepctl_ctx->ports, cache_name, port);
free(cache_name);
shash_init(&port->bindings);
return port;
}
static void
del_cached_port(struct vtep_ctl_context *vtepctl_ctx,
struct vtep_ctl_port *port)
{
char *cache_name = xasprintf("%s+%s", port->ps->name, port->port_cfg->name);
ovs_list_remove(&port->ports_node);
shash_find_and_delete(&vtepctl_ctx->ports, cache_name);
vteprec_physical_port_delete(port->port_cfg);
free(cache_name);
free(port);
}
static void
add_pswitch_to_cache(struct vtep_ctl_context *vtepctl_ctx,
struct vteprec_physical_switch *ps_cfg)
{
struct vtep_ctl_pswitch *ps = xmalloc(sizeof *ps);
ps->ps_cfg = ps_cfg;
ps->name = xstrdup(ps_cfg->name);
ovs_list_init(&ps->ports);
shash_add(&vtepctl_ctx->pswitches, ps->name, ps);
}
static void
vtep_delete_pswitch(const struct vteprec_global *vtep_global,
const struct vteprec_physical_switch *ps)
{
struct vteprec_physical_switch **pswitches;
size_t i, n;
pswitches = xmalloc(sizeof *vtep_global->switches
* vtep_global->n_switches);
for (i = n = 0; i < vtep_global->n_switches; i++) {
if (vtep_global->switches[i] != ps) {
pswitches[n++] = vtep_global->switches[i];
}
}
vteprec_global_set_switches(vtep_global, pswitches, n);
free(pswitches);
}
static void
del_cached_pswitch(struct vtep_ctl_context *ctx, struct vtep_ctl_pswitch *ps)
{
ovs_assert(ovs_list_is_empty(&ps->ports));
if (ps->ps_cfg) {
vteprec_physical_switch_delete(ps->ps_cfg);
vtep_delete_pswitch(ctx->vtep_global, ps->ps_cfg);
}
shash_find_and_delete(&ctx->pswitches, ps->name);
free(ps->name);
free(ps);
}
static struct vtep_ctl_lswitch *
add_lswitch_to_cache(struct vtep_ctl_context *vtepctl_ctx,
const struct vteprec_logical_switch *ls_cfg)
{
struct vtep_ctl_lswitch *ls = xmalloc(sizeof *ls);
ls->ls_cfg = ls_cfg;
ls->name = xstrdup(ls_cfg->name);
shash_add(&vtepctl_ctx->lswitches, ls->name, ls);
shash_init(&ls->ucast_local);
shash_init(&ls->ucast_remote);
shash_init(&ls->mcast_local);
shash_init(&ls->mcast_remote);
return ls;
}
static void
del_cached_lswitch(struct vtep_ctl_context *ctx, struct vtep_ctl_lswitch *ls)
{
if (ls->ls_cfg) {
vteprec_logical_switch_delete(ls->ls_cfg);
}
shash_find_and_delete(&ctx->lswitches, ls->name);
free(ls->name);
free(ls);
}
static void
commit_ls_bindings(struct vtep_ctl_port *port)
{
struct vteprec_logical_switch **binding_values;
int64_t *binding_keys;
size_t n_bindings;
struct shash_node *node;
int i;
n_bindings = shash_count(&port->bindings);
binding_keys = xmalloc(n_bindings * sizeof *binding_keys);
binding_values = xmalloc(n_bindings * sizeof *binding_values);
i = 0;
SHASH_FOR_EACH(node, &port->bindings) {
struct vtep_ctl_lswitch *ls_entry = node->data;
binding_keys[i] = strtoll(node->name, NULL, 0);
binding_values[i] = (struct vteprec_logical_switch *)ls_entry->ls_cfg;
i++;
}
vteprec_physical_port_set_vlan_bindings(port->port_cfg,
binding_keys, binding_values,
n_bindings);
free(binding_values);
free(binding_keys);
}
static void
add_ls_binding_to_cache(struct vtep_ctl_port *port,
const char *vlan,
struct vtep_ctl_lswitch *ls)
{
if (shash_find(&port->bindings, vlan)) {
ctl_fatal("multiple bindings for vlan %s", vlan);
}
shash_add(&port->bindings, vlan, ls);
}
static void
del_cached_ls_binding(struct vtep_ctl_port *port, const char *vlan)
{
if (!shash_find(&port->bindings, vlan)) {
ctl_fatal("no binding for vlan %s", vlan);
}
shash_find_and_delete(&port->bindings, vlan);
}
static struct vtep_ctl_lrouter *
add_lrouter_to_cache(struct vtep_ctl_context *vtepctl_ctx,
const struct vteprec_logical_router *lr_cfg)
{
struct vtep_ctl_lrouter *lr = xmalloc(sizeof *lr);
lr->lr_cfg = lr_cfg;
lr->name = xstrdup(lr_cfg->name);
shash_add(&vtepctl_ctx->lrouters, lr->name, lr);
return lr;
}
static void
del_cached_lrouter(struct vtep_ctl_context *ctx, struct vtep_ctl_lrouter *lr)
{
if (lr->lr_cfg) {
vteprec_logical_router_delete(lr->lr_cfg);
}
shash_find_and_delete(&ctx->lrouters, lr->name);
free(lr->name);
free(lr);
}
static struct vteprec_physical_locator *
find_ploc(struct vtep_ctl_context *vtepctl_ctx, const char *encap,
const char *dst_ip)
{
struct vteprec_physical_locator *ploc;
char *name = xasprintf("%s+%s", encap, dst_ip);
ovs_assert(vtepctl_ctx->cache_valid);
ploc = shash_find_data(&vtepctl_ctx->plocs, name);
free(name);
return ploc;
}
static void
add_ploc_to_cache(struct vtep_ctl_context *vtepctl_ctx,
struct vteprec_physical_locator *ploc)
{
char *name = xasprintf("%s+%s", ploc->encapsulation_type, ploc->dst_ip);
struct vteprec_physical_locator *orig_ploc;
orig_ploc = find_ploc(vtepctl_ctx, ploc->encapsulation_type, ploc->dst_ip);
if (!orig_ploc) {
shash_add(&vtepctl_ctx->plocs, name, ploc);
}
free(name);
}
static void
add_ploc_to_mcast_mac(struct vtep_ctl_mcast_mac *mcast_mac,
struct vteprec_physical_locator *ploc_cfg)
{
struct vtep_ctl_ploc *ploc;
ploc = xmalloc(sizeof *ploc);
ploc->ploc_cfg = ploc_cfg;
ovs_list_push_back(&mcast_mac->locators, &ploc->locators_node);
}
static void
del_ploc_from_mcast_mac(struct vtep_ctl_mcast_mac *mcast_mac,
struct vteprec_physical_locator *ploc_cfg)
{
struct vtep_ctl_ploc *ploc;
LIST_FOR_EACH (ploc, locators_node, &mcast_mac->locators) {
if (ploc->ploc_cfg == ploc_cfg) {
ovs_list_remove(&ploc->locators_node);
free(ploc);
return;
}
}
}
static struct vtep_ctl_mcast_mac *
add_mcast_mac_to_cache(struct vtep_ctl_context *vtepctl_ctx,
struct vtep_ctl_lswitch *ls, const char *mac,
struct vteprec_physical_locator_set *ploc_set_cfg,
bool local)
{
struct vtep_ctl_mcast_mac *mcast_mac;
struct shash *mcast_shash;
size_t i;
mcast_mac = xmalloc(sizeof *mcast_mac);
mcast_shash = local ? &ls->mcast_local : &ls->mcast_remote;
mcast_mac->ploc_set_cfg = ploc_set_cfg;
ovs_list_init(&mcast_mac->locators);
shash_add(mcast_shash, mac, mcast_mac);
for (i = 0; i < ploc_set_cfg->n_locators; i++) {
struct vteprec_physical_locator *ploc_cfg;
ploc_cfg = ploc_set_cfg->locators[i];
add_ploc_to_mcast_mac(mcast_mac, ploc_cfg);
add_ploc_to_cache(vtepctl_ctx, ploc_cfg);
}
return mcast_mac;
}
static void
vtep_ctl_context_invalidate_cache(struct ctl_context *ctx)
{
struct vtep_ctl_context *vtepctl_ctx = vtep_ctl_context_cast(ctx);
struct shash_node *node;
if (!vtepctl_ctx->cache_valid) {
return;
}
vtepctl_ctx->cache_valid = false;
SHASH_FOR_EACH (node, &vtepctl_ctx->pswitches) {
struct vtep_ctl_pswitch *ps = node->data;
free(ps->name);
free(ps);
}
shash_destroy(&vtepctl_ctx->pswitches);
SHASH_FOR_EACH (node, &vtepctl_ctx->ports) {
struct vtep_ctl_port *port = node->data;
shash_destroy(&port->bindings);
}
shash_destroy_free_data(&vtepctl_ctx->ports);
SHASH_FOR_EACH (node, &vtepctl_ctx->lswitches) {
struct vtep_ctl_lswitch *ls = node->data;
struct shash_node *node2, *next_node2;
shash_destroy(&ls->ucast_local);
shash_destroy(&ls->ucast_remote);
SHASH_FOR_EACH_SAFE (node2, next_node2, &ls->mcast_local) {
struct vtep_ctl_mcast_mac *mcast_mac = node2->data;
struct vtep_ctl_ploc *ploc, *next_ploc;
LIST_FOR_EACH_SAFE (ploc, next_ploc, locators_node,
&mcast_mac->locators) {
free(ploc);
}
free(mcast_mac);
}
shash_destroy(&ls->mcast_local);
SHASH_FOR_EACH_SAFE (node2, next_node2, &ls->mcast_remote) {
struct vtep_ctl_mcast_mac *mcast_mac = node2->data;
struct vtep_ctl_ploc *ploc, *next_ploc;
LIST_FOR_EACH_SAFE (ploc, next_ploc, locators_node,
&mcast_mac->locators) {
free(ploc);
}
free(mcast_mac);
}
shash_destroy(&ls->mcast_remote);
free(ls->name);
free(ls);
}
shash_destroy(&vtepctl_ctx->lswitches);
shash_destroy(&vtepctl_ctx->plocs);
SHASH_FOR_EACH (node, &vtepctl_ctx->lrouters) {
struct vtep_ctl_lrouter *lr = node->data;
free(lr->name);
free(lr);
}
shash_destroy(&vtepctl_ctx->lrouters);
}
static void
pre_get_info(struct ctl_context *ctx)
{
ovsdb_idl_add_column(ctx->idl, &vteprec_global_col_switches);
ovsdb_idl_add_column(ctx->idl, &vteprec_physical_switch_col_name);
ovsdb_idl_add_column(ctx->idl, &vteprec_physical_switch_col_ports);
ovsdb_idl_add_column(ctx->idl, &vteprec_physical_switch_col_tunnels);
ovsdb_idl_add_column(ctx->idl, &vteprec_physical_port_col_name);
ovsdb_idl_add_column(ctx->idl, &vteprec_physical_port_col_vlan_bindings);
ovsdb_idl_add_column(ctx->idl, &vteprec_logical_switch_col_name);
ovsdb_idl_add_column(ctx->idl,
&vteprec_logical_switch_col_replication_mode);
ovsdb_idl_add_column(ctx->idl, &vteprec_logical_router_col_name);
ovsdb_idl_add_column(ctx->idl, &vteprec_ucast_macs_local_col_MAC);
ovsdb_idl_add_column(ctx->idl, &vteprec_ucast_macs_local_col_locator);
ovsdb_idl_add_column(ctx->idl,
&vteprec_ucast_macs_local_col_logical_switch);
ovsdb_idl_add_column(ctx->idl, &vteprec_ucast_macs_remote_col_MAC);
ovsdb_idl_add_column(ctx->idl, &vteprec_ucast_macs_remote_col_locator);
ovsdb_idl_add_column(ctx->idl,
&vteprec_ucast_macs_remote_col_logical_switch);
ovsdb_idl_add_column(ctx->idl, &vteprec_mcast_macs_local_col_MAC);
ovsdb_idl_add_column(ctx->idl,
&vteprec_mcast_macs_local_col_locator_set);
ovsdb_idl_add_column(ctx->idl,
&vteprec_mcast_macs_local_col_logical_switch);
ovsdb_idl_add_column(ctx->idl, &vteprec_mcast_macs_remote_col_MAC);
ovsdb_idl_add_column(ctx->idl,
&vteprec_mcast_macs_remote_col_locator_set);
ovsdb_idl_add_column(ctx->idl,
&vteprec_mcast_macs_remote_col_logical_switch);
ovsdb_idl_add_column(ctx->idl,
&vteprec_physical_locator_set_col_locators);
ovsdb_idl_add_column(ctx->idl,
&vteprec_physical_locator_col_dst_ip);
ovsdb_idl_add_column(ctx->idl,
&vteprec_physical_locator_col_encapsulation_type);
ovsdb_idl_add_column(ctx->idl, &vteprec_tunnel_col_local);
ovsdb_idl_add_column(ctx->idl, &vteprec_tunnel_col_remote);
}
static void
vtep_ctl_context_populate_cache(struct ctl_context *ctx)
{
struct vtep_ctl_context *vtepctl_ctx = vtep_ctl_context_cast(ctx);
const struct vteprec_global *vtep_global = vtepctl_ctx->vtep_global;
const struct vteprec_logical_switch *ls_cfg;
const struct vteprec_logical_router *lr_cfg;
const struct vteprec_ucast_macs_local *ucast_local_cfg;
const struct vteprec_ucast_macs_remote *ucast_remote_cfg;
const struct vteprec_mcast_macs_local *mcast_local_cfg;
const struct vteprec_mcast_macs_remote *mcast_remote_cfg;
const struct vteprec_tunnel *tunnel_cfg;
struct sset pswitches, ports, lswitches;
struct sset lrouters;
size_t i;
if (vtepctl_ctx->cache_valid) {
/* Cache is already populated. */
return;
}
vtepctl_ctx->cache_valid = true;
shash_init(&vtepctl_ctx->pswitches);
shash_init(&vtepctl_ctx->ports);
shash_init(&vtepctl_ctx->lswitches);
shash_init(&vtepctl_ctx->plocs);
shash_init(&vtepctl_ctx->lrouters);
sset_init(&pswitches);
sset_init(&ports);
for (i = 0; i < vtep_global->n_switches; i++) {
struct vteprec_physical_switch *ps_cfg = vtep_global->switches[i];
size_t j;
if (!sset_add(&pswitches, ps_cfg->name)) {
VLOG_WARN("%s: database contains duplicate physical switch name",
ps_cfg->name);
continue;
}
add_pswitch_to_cache(vtepctl_ctx, ps_cfg);
for (j = 0; j < ps_cfg->n_ports; j++) {
struct vteprec_physical_port *port_cfg = ps_cfg->ports[j];
if (!sset_add(&ports, port_cfg->name)) {
/* Duplicate port name. (We will warn about that later.) */
continue;
}
}
}
sset_destroy(&pswitches);
sset_destroy(&ports);
sset_init(&lswitches);
VTEPREC_LOGICAL_SWITCH_FOR_EACH (ls_cfg, ctx->idl) {
if (!sset_add(&lswitches, ls_cfg->name)) {
VLOG_WARN("%s: database contains duplicate logical switch name",
ls_cfg->name);
continue;
}
add_lswitch_to_cache(vtepctl_ctx, ls_cfg);
}
sset_destroy(&lswitches);
sset_init(&lrouters);
VTEPREC_LOGICAL_ROUTER_FOR_EACH (lr_cfg, ctx->idl) {
if (!sset_add(&lrouters, lr_cfg->name)) {
VLOG_WARN("%s: database contains duplicate logical router name",
lr_cfg->name);
continue;
}
add_lrouter_to_cache(vtepctl_ctx, lr_cfg);
}
sset_destroy(&lrouters);
VTEPREC_UCAST_MACS_LOCAL_FOR_EACH (ucast_local_cfg, ctx->idl) {
struct vtep_ctl_lswitch *ls;
if (!ucast_local_cfg->logical_switch) {
continue;
}
ls = find_lswitch(vtepctl_ctx, ucast_local_cfg->logical_switch->name,
false);
if (!ls) {
continue;
}
if (ucast_local_cfg->locator) {
add_ploc_to_cache(vtepctl_ctx, ucast_local_cfg->locator);
}
shash_add(&ls->ucast_local, ucast_local_cfg->MAC, ucast_local_cfg);
}
VTEPREC_UCAST_MACS_REMOTE_FOR_EACH (ucast_remote_cfg, ctx->idl) {
struct vtep_ctl_lswitch *ls;
if (!ucast_remote_cfg->logical_switch) {
continue;
}
ls = find_lswitch(vtepctl_ctx, ucast_remote_cfg->logical_switch->name,
false);
if (!ls) {
continue;
}
if (ucast_remote_cfg->locator) {