forked from openvswitch/ovs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ovs-vsctl.c
2807 lines (2369 loc) · 84.7 KB
/
ovs-vsctl.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, 2013, 2014, 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 <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 "dirs.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 "lib/vswitch-idl.h"
#include "table.h"
#include "timeval.h"
#include "util.h"
#include "openvswitch/vconn.h"
#include "openvswitch/vlog.h"
VLOG_DEFINE_THIS_MODULE(vsctl);
struct vsctl_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;
/* --no-wait: Wait for ovs-vswitchd to reload its configuration? */
static bool wait_for_reload = true;
/* --timeout: Time to wait for a connection to 'db'. */
static int timeout;
/* --retry: If true, ovs-vsctl will retry connecting to the database forever.
* If false and --db says to use an active connection method (e.g. "unix:",
* "tcp:", "ssl:"), then ovs-vsctl will try to connect once and exit with an
* error if the database server cannot be contacted (e.g. ovsdb-server is not
* running).
*
* Regardless of this setting, --timeout always limits how long ovs-vsctl will
* wait. */
static bool retry;
/* Format for table output. */
static struct table_style table_style = TABLE_STYLE_DEFAULT;
static void vsctl_cmd_init(void);
/* The IDL we're using and the current transaction, if any.
* This is for use by vsctl_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 vsctl_exit(int status);
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_vsctl(const char *args, struct ctl_command *, size_t n,
struct ovsdb_idl *);
/* post_db_reload_check frame work is to allow ovs-vsctl to do additional
* checks after OVSDB transactions are successfully recorded and reload by
* ovs-vswitchd.
*
* For example, When a new interface is added to OVSDB, ovs-vswitchd will
* either store a positive values on successful implementing the new
* interface, or -1 on failure.
*
* Unless --no-wait command line option is specified,
* post_db_reload_do_checks() is called right after any configuration
* changes is picked up (i.e. reload) by ovs-vswitchd. Any error detected
* post OVSDB reload is reported as ovs-vsctl errors. OVS-vswitchd logs
* more detailed messages about those errors.
*
* Current implementation only check for Post OVSDB reload failures on new
* interface additions with 'add-br' and 'add-port' commands.
*
* post_db_reload_expect_iface()
*
* keep track of interfaces to be checked post OVSDB reload. */
static void post_db_reload_check_init(void);
static void post_db_reload_do_checks(const struct vsctl_context *);
static void post_db_reload_expect_iface(const struct ovsrec_interface *);
static struct uuid *neoteric_ifaces;
static size_t n_neoteric_ifaces;
static size_t allocated_neoteric_ifaces;
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");
vsctl_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, &ovsrec_idl_class, false, retry);
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_vsctl(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_NO_WAIT,
OPT_DRY_RUN,
OPT_BOOTSTRAP_CA_CERT,
OPT_PEER_CA_CERT,
OPT_LOCAL,
OPT_RETRY,
OPT_COMMANDS,
OPT_OPTIONS,
VLOG_OPTION_ENUMS,
TABLE_OPTION_ENUMS,
SSL_OPTION_ENUMS,
};
static const struct option global_long_options[] = {
{"db", required_argument, NULL, OPT_DB},
{"no-syslog", no_argument, NULL, OPT_NO_SYSLOG},
{"no-wait", no_argument, NULL, OPT_NO_WAIT},
{"dry-run", no_argument, NULL, OPT_DRY_RUN},
{"oneline", no_argument, NULL, OPT_ONELINE},
{"timeout", required_argument, NULL, 't'},
{"retry", no_argument, NULL, OPT_RETRY},
{"help", no_argument, NULL, 'h'},
{"commands", no_argument, NULL, OPT_COMMANDS},
{"options", no_argument, NULL, OPT_OPTIONS},
{"version", no_argument, NULL, 'V'},
VLOG_LONG_OPTIONS,
TABLE_LONG_OPTIONS,
STREAM_SSL_LONG_OPTIONS,
{"bootstrap-ca-cert", required_argument, NULL, OPT_BOOTSTRAP_CA_CERT},
{"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);
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_NO_WAIT:
wait_for_reload = false;
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 OPT_COMMANDS:
ctl_print_commands();
/* fall through */
case OPT_OPTIONS:
ctl_print_options(global_long_options);
/* fall through */
case 'V':
ovs_print_version(0, 0);
printf("DB Schema %s\n", ovsrec_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;
case OPT_RETRY:
retry = true;
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 OPT_BOOTSTRAP_CA_CERT:
stream_ssl_set_ca_cert_file(optarg, true);
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);
}
static void
usage(void)
{
printf("\
%s: ovs-vswitchd management utility\n\
usage: %s [OPTIONS] COMMAND [ARG...]\n\
\n\
Open vSwitch commands:\n\
init initialize database, if not yet initialized\n\
show print overview of database contents\n\
emer-reset reset configuration to clean state\n\
\n\
Bridge commands:\n\
add-br BRIDGE create a new bridge named BRIDGE\n\
add-br BRIDGE PARENT VLAN create new fake BRIDGE in PARENT on VLAN\n\
del-br BRIDGE delete BRIDGE and all of its ports\n\
list-br print the names of all the bridges\n\
br-exists BRIDGE exit 2 if BRIDGE does not exist\n\
br-to-vlan BRIDGE print the VLAN which BRIDGE is on\n\
br-to-parent BRIDGE print the parent of BRIDGE\n\
br-set-external-id BRIDGE KEY VALUE set KEY on BRIDGE to VALUE\n\
br-set-external-id BRIDGE KEY unset KEY on BRIDGE\n\
br-get-external-id BRIDGE KEY print value of KEY on BRIDGE\n\
br-get-external-id BRIDGE list key-value pairs on BRIDGE\n\
\n\
Port commands (a bond is considered to be a single port):\n\
list-ports BRIDGE print the names of all the ports on BRIDGE\n\
add-port BRIDGE PORT add network device PORT to BRIDGE\n\
add-bond BRIDGE PORT IFACE... add bonded port PORT in BRIDGE from IFACES\n\
del-port [BRIDGE] PORT delete PORT (which may be bonded) from BRIDGE\n\
port-to-br PORT print name of bridge that contains PORT\n\
\n\
Interface commands (a bond consists of multiple interfaces):\n\
list-ifaces BRIDGE print the names of all interfaces on BRIDGE\n\
iface-to-br IFACE print name of bridge that contains IFACE\n\
\n\
Controller commands:\n\
get-controller BRIDGE print the controllers for BRIDGE\n\
del-controller BRIDGE delete the controllers for BRIDGE\n\
set-controller BRIDGE TARGET... set the controllers for BRIDGE\n\
get-fail-mode BRIDGE print the fail-mode for BRIDGE\n\
del-fail-mode BRIDGE delete the fail-mode for BRIDGE\n\
set-fail-mode BRIDGE MODE set the fail-mode for BRIDGE to MODE\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\
SSL commands:\n\
get-ssl print the SSL configuration\n\
del-ssl delete the SSL configuration\n\
set-ssl PRIV-KEY CERT CA-CERT set the SSL configuration\n\
\n\
Auto Attach commands:\n\
add-aa-mapping BRIDGE I-SID VLAN add Auto Attach mapping to BRIDGE\n\
del-aa-mapping BRIDGE I-SID VLAN delete Auto Attach mapping VLAN from BRIDGE\n\
get-aa-mapping BRIDGE get Auto Attach mappings from BRIDGE\n\
\n\
Switch commands:\n\
emer-reset reset switch to known good state\n\
\n\
%s\
\n\
Options:\n\
--db=DATABASE connect to DATABASE\n\
(default: %s)\n\
--no-wait do not wait for ovs-vswitchd to reconfigure\n\
--retry keep trying to connect to server forever\n\
-t, --timeout=SECS wait at most SECS seconds for ovs-vswitchd\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());
table_usage();
vlog_usage();
printf("\
--no-syslog equivalent to --verbose=vsctl: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);
}
/* ovs-vsctl specific context. Inherits the 'struct ctl_context' as base. */
struct vsctl_context {
struct ctl_context base;
/* Modifiable state. */
const struct ovsrec_open_vswitch *ovs;
bool verified_ports;
/* A cache of the contents of the database.
*
* A command that needs to use any of this information must first call
* vsctl_context_populate_cache(). A command that changes anything that
* could invalidate the cache must either call
* vsctl_context_invalidate_cache() or manually update the cache to
* maintain its correctness. */
bool cache_valid;
struct shash bridges; /* Maps from bridge name to struct vsctl_bridge. */
struct shash ports; /* Maps from port name to struct vsctl_port. */
struct shash ifaces; /* Maps from port name to struct vsctl_iface. */
};
struct vsctl_bridge {
struct ovsrec_bridge *br_cfg;
char *name;
struct ovs_list ports; /* Contains "struct vsctl_port"s. */
/* VLAN ("fake") bridge support.
*
* Use 'parent != NULL' to detect a fake bridge, because 'vlan' can be 0
* in either case. */
struct hmap children; /* VLAN bridges indexed by 'vlan'. */
struct hmap_node children_node; /* Node in parent's 'children' hmap. */
struct vsctl_bridge *parent; /* Real bridge, or NULL. */
int vlan; /* VLAN VID (0...4095), or 0. */
};
struct vsctl_port {
struct ovs_list ports_node; /* In struct vsctl_bridge's 'ports' list. */
struct ovs_list ifaces; /* Contains "struct vsctl_iface"s. */
struct ovsrec_port *port_cfg;
struct vsctl_bridge *bridge;
};
struct vsctl_iface {
struct ovs_list ifaces_node; /* In struct vsctl_port's 'ifaces' list. */
struct ovsrec_interface *iface_cfg;
struct vsctl_port *port;
};
/* Casts 'base' into 'struct vsctl_context'. */
static struct vsctl_context *
vsctl_context_cast(struct ctl_context *base)
{
return CONTAINER_OF(base, struct vsctl_context, base);
}
static struct vsctl_bridge *find_vlan_bridge(struct vsctl_bridge *parent,
int vlan);
static char *
vsctl_context_to_string(const struct ctl_context *ctx)
{
const struct shash_node *node;
struct svec words;
char *s;
int i;
svec_init(&words);
SHASH_FOR_EACH (node, &ctx->options) {
svec_add(&words, node->name);
}
for (i = 0; i < ctx->argc; i++) {
svec_add(&words, ctx->argv[i]);
}
svec_terminate(&words);
s = process_escape_args(words.names);
svec_destroy(&words);
return s;
}
static void
verify_ports(struct vsctl_context *vsctl_ctx)
{
if (!vsctl_ctx->verified_ports) {
const struct ovsrec_bridge *bridge;
const struct ovsrec_port *port;
ovsrec_open_vswitch_verify_bridges(vsctl_ctx->ovs);
OVSREC_BRIDGE_FOR_EACH (bridge, vsctl_ctx->base.idl) {
ovsrec_bridge_verify_ports(bridge);
}
OVSREC_PORT_FOR_EACH (port, vsctl_ctx->base.idl) {
ovsrec_port_verify_interfaces(port);
}
vsctl_ctx->verified_ports = true;
}
}
static struct vsctl_bridge *
add_bridge_to_cache(struct vsctl_context *vsctl_ctx,
struct ovsrec_bridge *br_cfg, const char *name,
struct vsctl_bridge *parent, int vlan)
{
struct vsctl_bridge *br = xmalloc(sizeof *br);
br->br_cfg = br_cfg;
br->name = xstrdup(name);
ovs_list_init(&br->ports);
br->parent = parent;
br->vlan = vlan;
hmap_init(&br->children);
if (parent) {
struct vsctl_bridge *conflict = find_vlan_bridge(parent, vlan);
if (conflict) {
VLOG_WARN("%s: bridge has multiple VLAN bridges (%s and %s) "
"for VLAN %d, but only one is allowed",
parent->name, name, conflict->name, vlan);
} else {
hmap_insert(&parent->children, &br->children_node,
hash_int(vlan, 0));
}
}
shash_add(&vsctl_ctx->bridges, br->name, br);
return br;
}
static void
ovs_delete_bridge(const struct ovsrec_open_vswitch *ovs,
struct ovsrec_bridge *bridge)
{
struct ovsrec_bridge **bridges;
size_t i, n;
bridges = xmalloc(sizeof *ovs->bridges * ovs->n_bridges);
for (i = n = 0; i < ovs->n_bridges; i++) {
if (ovs->bridges[i] != bridge) {
bridges[n++] = ovs->bridges[i];
}
}
ovsrec_open_vswitch_set_bridges(ovs, bridges, n);
free(bridges);
}
static void
del_cached_bridge(struct vsctl_context *vsctl_ctx, struct vsctl_bridge *br)
{
ovs_assert(ovs_list_is_empty(&br->ports));
ovs_assert(hmap_is_empty(&br->children));
if (br->parent) {
hmap_remove(&br->parent->children, &br->children_node);
}
if (br->br_cfg) {
ovsrec_bridge_delete(br->br_cfg);
ovs_delete_bridge(vsctl_ctx->ovs, br->br_cfg);
}
shash_find_and_delete(&vsctl_ctx->bridges, br->name);
hmap_destroy(&br->children);
free(br->name);
free(br);
}
static bool
port_is_fake_bridge(const struct ovsrec_port *port_cfg)
{
return (port_cfg->fake_bridge
&& port_cfg->tag
&& *port_cfg->tag >= 0 && *port_cfg->tag <= 4095);
}
static struct vsctl_bridge *
find_vlan_bridge(struct vsctl_bridge *parent, int vlan)
{
struct vsctl_bridge *child;
HMAP_FOR_EACH_IN_BUCKET (child, children_node, hash_int(vlan, 0),
&parent->children) {
if (child->vlan == vlan) {
return child;
}
}
return NULL;
}
static struct vsctl_port *
add_port_to_cache(struct vsctl_context *vsctl_ctx, struct vsctl_bridge *parent,
struct ovsrec_port *port_cfg)
{
struct vsctl_port *port;
if (port_cfg->tag
&& *port_cfg->tag >= 0 && *port_cfg->tag <= 4095) {
struct vsctl_bridge *vlan_bridge;
vlan_bridge = find_vlan_bridge(parent, *port_cfg->tag);
if (vlan_bridge) {
parent = vlan_bridge;
}
}
port = xmalloc(sizeof *port);
ovs_list_push_back(&parent->ports, &port->ports_node);
ovs_list_init(&port->ifaces);
port->port_cfg = port_cfg;
port->bridge = parent;
shash_add(&vsctl_ctx->ports, port_cfg->name, port);
return port;
}
static void
del_cached_port(struct vsctl_context *vsctl_ctx, struct vsctl_port *port)
{
ovs_assert(ovs_list_is_empty(&port->ifaces));
ovs_list_remove(&port->ports_node);
shash_find_and_delete(&vsctl_ctx->ports, port->port_cfg->name);
ovsrec_port_delete(port->port_cfg);
free(port);
}
static struct vsctl_iface *
add_iface_to_cache(struct vsctl_context *vsctl_ctx, struct vsctl_port *parent,
struct ovsrec_interface *iface_cfg)
{
struct vsctl_iface *iface;
iface = xmalloc(sizeof *iface);
ovs_list_push_back(&parent->ifaces, &iface->ifaces_node);
iface->iface_cfg = iface_cfg;
iface->port = parent;
shash_add(&vsctl_ctx->ifaces, iface_cfg->name, iface);
return iface;
}
static void
del_cached_iface(struct vsctl_context *vsctl_ctx, struct vsctl_iface *iface)
{
ovs_list_remove(&iface->ifaces_node);
shash_find_and_delete(&vsctl_ctx->ifaces, iface->iface_cfg->name);
ovsrec_interface_delete(iface->iface_cfg);
free(iface);
}
static void
vsctl_context_invalidate_cache(struct ctl_context *ctx)
{
struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
struct shash_node *node;
if (!vsctl_ctx->cache_valid) {
return;
}
vsctl_ctx->cache_valid = false;
SHASH_FOR_EACH (node, &vsctl_ctx->bridges) {
struct vsctl_bridge *bridge = node->data;
hmap_destroy(&bridge->children);
free(bridge->name);
free(bridge);
}
shash_destroy(&vsctl_ctx->bridges);
shash_destroy_free_data(&vsctl_ctx->ports);
shash_destroy_free_data(&vsctl_ctx->ifaces);
}
static void
pre_get_info(struct ctl_context *ctx)
{
ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_bridges);
ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_name);
ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_controller);
ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_fail_mode);
ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_ports);
ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_name);
ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_fake_bridge);
ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_tag);
ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_interfaces);
ovsdb_idl_add_column(ctx->idl, &ovsrec_interface_col_name);
ovsdb_idl_add_column(ctx->idl, &ovsrec_interface_col_ofport);
ovsdb_idl_add_column(ctx->idl, &ovsrec_interface_col_error);
}
static void
vsctl_context_populate_cache(struct ctl_context *ctx)
{
struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
const struct ovsrec_open_vswitch *ovs = vsctl_ctx->ovs;
struct sset bridges, ports;
size_t i;
if (vsctl_ctx->cache_valid) {
/* Cache is already populated. */
return;
}
vsctl_ctx->cache_valid = true;
shash_init(&vsctl_ctx->bridges);
shash_init(&vsctl_ctx->ports);
shash_init(&vsctl_ctx->ifaces);
sset_init(&bridges);
sset_init(&ports);
for (i = 0; i < ovs->n_bridges; i++) {
struct ovsrec_bridge *br_cfg = ovs->bridges[i];
struct vsctl_bridge *br;
size_t j;
if (!sset_add(&bridges, br_cfg->name)) {
VLOG_WARN("%s: database contains duplicate bridge name",
br_cfg->name);
continue;
}
br = add_bridge_to_cache(vsctl_ctx, br_cfg, br_cfg->name, NULL, 0);
for (j = 0; j < br_cfg->n_ports; j++) {
struct ovsrec_port *port_cfg = br_cfg->ports[j];
if (!sset_add(&ports, port_cfg->name)) {
/* Duplicate port name. (We will warn about that later.) */
continue;
}
if (port_is_fake_bridge(port_cfg)
&& sset_add(&bridges, port_cfg->name)) {
add_bridge_to_cache(vsctl_ctx, NULL, port_cfg->name, br,
*port_cfg->tag);
}
}
}
sset_destroy(&bridges);
sset_destroy(&ports);
sset_init(&bridges);
for (i = 0; i < ovs->n_bridges; i++) {
struct ovsrec_bridge *br_cfg = ovs->bridges[i];
struct vsctl_bridge *br;
size_t j;
if (!sset_add(&bridges, br_cfg->name)) {
continue;
}
br = shash_find_data(&vsctl_ctx->bridges, br_cfg->name);
for (j = 0; j < br_cfg->n_ports; j++) {
struct ovsrec_port *port_cfg = br_cfg->ports[j];
struct vsctl_port *port;
size_t k;
port = shash_find_data(&vsctl_ctx->ports, port_cfg->name);
if (port) {
if (port_cfg == port->port_cfg) {
VLOG_WARN("%s: port is in multiple bridges (%s and %s)",
port_cfg->name, br->name, port->bridge->name);
} else {
/* Log as an error because this violates the database's
* uniqueness constraints, so the database server shouldn't
* have allowed it. */
VLOG_ERR("%s: database contains duplicate port name",
port_cfg->name);
}
continue;
}
if (port_is_fake_bridge(port_cfg)
&& !sset_add(&bridges, port_cfg->name)) {
continue;
}
port = add_port_to_cache(vsctl_ctx, br, port_cfg);
for (k = 0; k < port_cfg->n_interfaces; k++) {
struct ovsrec_interface *iface_cfg = port_cfg->interfaces[k];
struct vsctl_iface *iface;
iface = shash_find_data(&vsctl_ctx->ifaces, iface_cfg->name);
if (iface) {
if (iface_cfg == iface->iface_cfg) {
VLOG_WARN("%s: interface is in multiple ports "
"(%s and %s)",
iface_cfg->name,
iface->port->port_cfg->name,
port->port_cfg->name);
} else {
/* Log as an error because this violates the database's
* uniqueness constraints, so the database server
* shouldn't have allowed it. */
VLOG_ERR("%s: database contains duplicate interface "
"name", iface_cfg->name);
}
continue;
}
add_iface_to_cache(vsctl_ctx, port, iface_cfg);
}
}
}
sset_destroy(&bridges);
}
static void
check_conflicts(struct vsctl_context *vsctl_ctx, const char *name,
char *msg)
{
struct vsctl_iface *iface;
struct vsctl_port *port;
verify_ports(vsctl_ctx);
if (shash_find(&vsctl_ctx->bridges, name)) {
ctl_fatal("%s because a bridge named %s already exists",
msg, name);
}
port = shash_find_data(&vsctl_ctx->ports, name);
if (port) {
ctl_fatal("%s because a port named %s already exists on "
"bridge %s", msg, name, port->bridge->name);
}
iface = shash_find_data(&vsctl_ctx->ifaces, name);
if (iface) {
ctl_fatal("%s because an interface named %s already exists "
"on bridge %s", msg, name, iface->port->bridge->name);
}
free(msg);
}
static struct vsctl_bridge *
find_bridge(struct vsctl_context *vsctl_ctx, const char *name, bool must_exist)
{
struct vsctl_bridge *br;
ovs_assert(vsctl_ctx->cache_valid);
br = shash_find_data(&vsctl_ctx->bridges, name);
if (must_exist && !br) {
ctl_fatal("no bridge named %s", name);
}
ovsrec_open_vswitch_verify_bridges(vsctl_ctx->ovs);
return br;
}
static struct vsctl_bridge *
find_real_bridge(struct vsctl_context *vsctl_ctx,
const char *name, bool must_exist)
{
struct vsctl_bridge *br = find_bridge(vsctl_ctx, name, must_exist);
if (br && br->parent) {
ctl_fatal("%s is a fake bridge", name);
}
return br;
}
static struct vsctl_port *
find_port(struct vsctl_context *vsctl_ctx, const char *name, bool must_exist)
{
struct vsctl_port *port;
ovs_assert(vsctl_ctx->cache_valid);
port = shash_find_data(&vsctl_ctx->ports, name);
if (port && !strcmp(name, port->bridge->name)) {
port = NULL;
}
if (must_exist && !port) {
ctl_fatal("no port named %s", name);
}
verify_ports(vsctl_ctx);
return port;
}
static struct vsctl_iface *
find_iface(struct vsctl_context *vsctl_ctx, const char *name, bool must_exist)
{
struct vsctl_iface *iface;
ovs_assert(vsctl_ctx->cache_valid);
iface = shash_find_data(&vsctl_ctx->ifaces, name);
if (iface && !strcmp(name, iface->port->bridge->name)) {
iface = NULL;
}
if (must_exist && !iface) {
ctl_fatal("no interface named %s", name);
}
verify_ports(vsctl_ctx);
return iface;
}
static void
bridge_insert_port(struct ovsrec_bridge *br, struct ovsrec_port *port)
{
struct ovsrec_port **ports;
size_t i;
ports = xmalloc(sizeof *br->ports * (br->n_ports + 1));
for (i = 0; i < br->n_ports; i++) {
ports[i] = br->ports[i];
}
ports[br->n_ports] = port;
ovsrec_bridge_set_ports(br, ports, br->n_ports + 1);
free(ports);
}
static void
bridge_delete_port(struct ovsrec_bridge *br, struct ovsrec_port *port)
{
struct ovsrec_port **ports;
size_t i, n;
ports = xmalloc(sizeof *br->ports * br->n_ports);
for (i = n = 0; i < br->n_ports; i++) {
if (br->ports[i] != port) {
ports[n++] = br->ports[i];
}
}
ovsrec_bridge_set_ports(br, ports, n);
free(ports);
}
static void
ovs_insert_bridge(const struct ovsrec_open_vswitch *ovs,
struct ovsrec_bridge *bridge)
{
struct ovsrec_bridge **bridges;
size_t i;
bridges = xmalloc(sizeof *ovs->bridges * (ovs->n_bridges + 1));
for (i = 0; i < ovs->n_bridges; i++) {
bridges[i] = ovs->bridges[i];
}
bridges[ovs->n_bridges] = bridge;
ovsrec_open_vswitch_set_bridges(ovs, bridges, ovs->n_bridges + 1);
free(bridges);
}
static void
cmd_init(struct ctl_context *ctx OVS_UNUSED)
{
}
static struct cmd_show_table cmd_show_tables[] = {
{&ovsrec_table_open_vswitch,
NULL,
{&ovsrec_open_vswitch_col_manager_options,
&ovsrec_open_vswitch_col_bridges,
&ovsrec_open_vswitch_col_ovs_version},
{NULL, NULL, NULL}
},
{&ovsrec_table_bridge,
&ovsrec_bridge_col_name,
{&ovsrec_bridge_col_controller,
&ovsrec_bridge_col_fail_mode,
&ovsrec_bridge_col_ports},