]> git.proxmox.com Git - ovs.git/blob - utilities/ovs-vsctl.c
ovs-vsctl: Disallow empty bridge, port, and interface names.
[ovs.git] / utilities / ovs-vsctl.c
1 /*
2 * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017 Nicira, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <config.h>
18
19 #include <ctype.h>
20 #include <errno.h>
21 #include <float.h>
22 #include <getopt.h>
23 #include <inttypes.h>
24 #include <signal.h>
25 #include <stdarg.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
29
30 #include "db-ctl-base.h"
31
32 #include "command-line.h"
33 #include "compiler.h"
34 #include "dirs.h"
35 #include "openvswitch/dynamic-string.h"
36 #include "fatal-signal.h"
37 #include "hash.h"
38 #include "openvswitch/json.h"
39 #include "ovsdb-data.h"
40 #include "ovsdb-idl.h"
41 #include "poll-loop.h"
42 #include "process.h"
43 #include "stream.h"
44 #include "stream-ssl.h"
45 #include "smap.h"
46 #include "sset.h"
47 #include "svec.h"
48 #include "lib/vswitch-idl.h"
49 #include "table.h"
50 #include "timeval.h"
51 #include "util.h"
52 #include "openvswitch/vconn.h"
53 #include "openvswitch/vlog.h"
54
55 VLOG_DEFINE_THIS_MODULE(vsctl);
56
57 struct vsctl_context;
58
59 /* --db: The database server to contact. */
60 static const char *db;
61
62 /* --oneline: Write each command's output as a single line? */
63 static bool oneline;
64
65 /* --dry-run: Do not commit any changes. */
66 static bool dry_run;
67
68 /* --no-wait: Wait for ovs-vswitchd to reload its configuration? */
69 static bool wait_for_reload = true;
70
71 /* --timeout: Time to wait for a connection to 'db'. */
72 static int timeout;
73
74 /* --retry: If true, ovs-vsctl will retry connecting to the database forever.
75 * If false and --db says to use an active connection method (e.g. "unix:",
76 * "tcp:", "ssl:"), then ovs-vsctl will try to connect once and exit with an
77 * error if the database server cannot be contacted (e.g. ovsdb-server is not
78 * running).
79 *
80 * Regardless of this setting, --timeout always limits how long ovs-vsctl will
81 * wait. */
82 static bool retry;
83
84 /* Format for table output. */
85 static struct table_style table_style = TABLE_STYLE_DEFAULT;
86
87 static void vsctl_cmd_init(void);
88
89 /* The IDL we're using and the current transaction, if any.
90 * This is for use by vsctl_exit() only, to allow it to clean up.
91 * Other code should use its context arguments. */
92 static struct ovsdb_idl *the_idl;
93 static struct ovsdb_idl_txn *the_idl_txn;
94 OVS_NO_RETURN static void vsctl_exit(int status);
95
96 OVS_NO_RETURN static void usage(void);
97 static void parse_options(int argc, char *argv[], struct shash *local_options);
98 static void run_prerequisites(struct ctl_command[], size_t n_commands,
99 struct ovsdb_idl *);
100 static void do_vsctl(const char *args, struct ctl_command *, size_t n,
101 struct ovsdb_idl *);
102
103 /* post_db_reload_check frame work is to allow ovs-vsctl to do additional
104 * checks after OVSDB transactions are successfully recorded and reload by
105 * ovs-vswitchd.
106 *
107 * For example, When a new interface is added to OVSDB, ovs-vswitchd will
108 * either store a positive values on successful implementing the new
109 * interface, or -1 on failure.
110 *
111 * Unless --no-wait command line option is specified,
112 * post_db_reload_do_checks() is called right after any configuration
113 * changes is picked up (i.e. reload) by ovs-vswitchd. Any error detected
114 * post OVSDB reload is reported as ovs-vsctl errors. OVS-vswitchd logs
115 * more detailed messages about those errors.
116 *
117 * Current implementation only check for Post OVSDB reload failures on new
118 * interface additions with 'add-br' and 'add-port' commands.
119 *
120 * post_db_reload_expect_iface()
121 *
122 * keep track of interfaces to be checked post OVSDB reload. */
123 static void post_db_reload_check_init(void);
124 static void post_db_reload_do_checks(const struct vsctl_context *);
125 static void post_db_reload_expect_iface(const struct ovsrec_interface *);
126
127 static struct uuid *neoteric_ifaces;
128 static size_t n_neoteric_ifaces;
129 static size_t allocated_neoteric_ifaces;
130
131 int
132 main(int argc, char *argv[])
133 {
134 struct ovsdb_idl *idl;
135 struct ctl_command *commands;
136 struct shash local_options;
137 unsigned int seqno;
138 size_t n_commands;
139 char *args;
140
141 set_program_name(argv[0]);
142 fatal_ignore_sigpipe();
143 vlog_set_levels(NULL, VLF_CONSOLE, VLL_WARN);
144 vlog_set_levels_from_string_assert("reconnect:warn");
145
146 vsctl_cmd_init();
147
148 /* Log our arguments. This is often valuable for debugging systems. */
149 args = process_escape_args(argv);
150 VLOG(ctl_might_write_to_db(argv) ? VLL_INFO : VLL_DBG, "Called as %s", args);
151
152 /* Parse command line. */
153 shash_init(&local_options);
154 parse_options(argc, argv, &local_options);
155 commands = ctl_parse_commands(argc - optind, argv + optind, &local_options,
156 &n_commands);
157
158 if (timeout) {
159 time_alarm(timeout);
160 }
161
162 /* Initialize IDL. */
163 idl = the_idl = ovsdb_idl_create(db, &ovsrec_idl_class, false, retry);
164 run_prerequisites(commands, n_commands, idl);
165
166 /* Execute the commands.
167 *
168 * 'seqno' is the database sequence number for which we last tried to
169 * execute our transaction. There's no point in trying to commit more than
170 * once for any given sequence number, because if the transaction fails
171 * it's because the database changed and we need to obtain an up-to-date
172 * view of the database before we try the transaction again. */
173 seqno = ovsdb_idl_get_seqno(idl);
174 for (;;) {
175 ovsdb_idl_run(idl);
176 if (!ovsdb_idl_is_alive(idl)) {
177 int retval = ovsdb_idl_get_last_error(idl);
178 ctl_fatal("%s: database connection failed (%s)",
179 db, ovs_retval_to_string(retval));
180 }
181
182 if (seqno != ovsdb_idl_get_seqno(idl)) {
183 seqno = ovsdb_idl_get_seqno(idl);
184 do_vsctl(args, commands, n_commands, idl);
185 }
186
187 if (seqno == ovsdb_idl_get_seqno(idl)) {
188 ovsdb_idl_wait(idl);
189 poll_block();
190 }
191 }
192 }
193
194 static void
195 parse_options(int argc, char *argv[], struct shash *local_options)
196 {
197 enum {
198 OPT_DB = UCHAR_MAX + 1,
199 OPT_ONELINE,
200 OPT_NO_SYSLOG,
201 OPT_NO_WAIT,
202 OPT_DRY_RUN,
203 OPT_BOOTSTRAP_CA_CERT,
204 OPT_PEER_CA_CERT,
205 OPT_LOCAL,
206 OPT_RETRY,
207 OPT_COMMANDS,
208 OPT_OPTIONS,
209 VLOG_OPTION_ENUMS,
210 TABLE_OPTION_ENUMS,
211 SSL_OPTION_ENUMS,
212 };
213 static const struct option global_long_options[] = {
214 {"db", required_argument, NULL, OPT_DB},
215 {"no-syslog", no_argument, NULL, OPT_NO_SYSLOG},
216 {"no-wait", no_argument, NULL, OPT_NO_WAIT},
217 {"dry-run", no_argument, NULL, OPT_DRY_RUN},
218 {"oneline", no_argument, NULL, OPT_ONELINE},
219 {"timeout", required_argument, NULL, 't'},
220 {"retry", no_argument, NULL, OPT_RETRY},
221 {"help", no_argument, NULL, 'h'},
222 {"commands", no_argument, NULL, OPT_COMMANDS},
223 {"options", no_argument, NULL, OPT_OPTIONS},
224 {"version", no_argument, NULL, 'V'},
225 VLOG_LONG_OPTIONS,
226 TABLE_LONG_OPTIONS,
227 STREAM_SSL_LONG_OPTIONS,
228 {"bootstrap-ca-cert", required_argument, NULL, OPT_BOOTSTRAP_CA_CERT},
229 {"peer-ca-cert", required_argument, NULL, OPT_PEER_CA_CERT},
230 {NULL, 0, NULL, 0},
231 };
232 const int n_global_long_options = ARRAY_SIZE(global_long_options) - 1;
233 char *tmp, *short_options;
234
235 struct option *options;
236 size_t allocated_options;
237 size_t n_options;
238 size_t i;
239
240 tmp = ovs_cmdl_long_options_to_short_options(global_long_options);
241 short_options = xasprintf("+%s", tmp);
242 free(tmp);
243
244 /* We want to parse both global and command-specific options here, but
245 * getopt_long() isn't too convenient for the job. We copy our global
246 * options into a dynamic array, then append all of the command-specific
247 * options. */
248 options = xmemdup(global_long_options, sizeof global_long_options);
249 allocated_options = ARRAY_SIZE(global_long_options);
250 n_options = n_global_long_options;
251 ctl_add_cmd_options(&options, &n_options, &allocated_options, OPT_LOCAL);
252
253 for (;;) {
254 int idx;
255 int c;
256
257 c = getopt_long(argc, argv, short_options, options, &idx);
258 if (c == -1) {
259 break;
260 }
261
262 switch (c) {
263 case OPT_DB:
264 db = optarg;
265 break;
266
267 case OPT_ONELINE:
268 oneline = true;
269 break;
270
271 case OPT_NO_SYSLOG:
272 vlog_set_levels(&this_module, VLF_SYSLOG, VLL_WARN);
273 break;
274
275 case OPT_NO_WAIT:
276 wait_for_reload = false;
277 break;
278
279 case OPT_DRY_RUN:
280 dry_run = true;
281 break;
282
283 case OPT_LOCAL:
284 if (shash_find(local_options, options[idx].name)) {
285 ctl_fatal("'%s' option specified multiple times",
286 options[idx].name);
287 }
288 shash_add_nocopy(local_options,
289 xasprintf("--%s", options[idx].name),
290 nullable_xstrdup(optarg));
291 break;
292
293 case 'h':
294 usage();
295
296 case OPT_COMMANDS:
297 ctl_print_commands();
298
299 case OPT_OPTIONS:
300 ctl_print_options(global_long_options);
301
302 case 'V':
303 ovs_print_version(0, 0);
304 printf("DB Schema %s\n", ovsrec_get_db_version());
305 exit(EXIT_SUCCESS);
306
307 case 't':
308 timeout = strtoul(optarg, NULL, 10);
309 if (timeout < 0) {
310 ctl_fatal("value %s on -t or --timeout is invalid",
311 optarg);
312 }
313 break;
314
315 case OPT_RETRY:
316 retry = true;
317 break;
318
319 VLOG_OPTION_HANDLERS
320 TABLE_OPTION_HANDLERS(&table_style)
321
322 STREAM_SSL_OPTION_HANDLERS
323
324 case OPT_PEER_CA_CERT:
325 stream_ssl_set_peer_ca_cert_file(optarg);
326 break;
327
328 case OPT_BOOTSTRAP_CA_CERT:
329 stream_ssl_set_ca_cert_file(optarg, true);
330 break;
331
332 case '?':
333 exit(EXIT_FAILURE);
334
335 default:
336 abort();
337 }
338 }
339 free(short_options);
340
341 if (!db) {
342 db = ctl_default_db();
343 }
344
345 for (i = n_global_long_options; options[i].name; i++) {
346 free(CONST_CAST(char *, options[i].name));
347 }
348 free(options);
349 }
350
351 static void
352 usage(void)
353 {
354 printf("\
355 %s: ovs-vswitchd management utility\n\
356 usage: %s [OPTIONS] COMMAND [ARG...]\n\
357 \n\
358 Open vSwitch commands:\n\
359 init initialize database, if not yet initialized\n\
360 show print overview of database contents\n\
361 emer-reset reset configuration to clean state\n\
362 \n\
363 Bridge commands:\n\
364 add-br BRIDGE create a new bridge named BRIDGE\n\
365 add-br BRIDGE PARENT VLAN create new fake BRIDGE in PARENT on VLAN\n\
366 del-br BRIDGE delete BRIDGE and all of its ports\n\
367 list-br print the names of all the bridges\n\
368 br-exists BRIDGE exit 2 if BRIDGE does not exist\n\
369 br-to-vlan BRIDGE print the VLAN which BRIDGE is on\n\
370 br-to-parent BRIDGE print the parent of BRIDGE\n\
371 br-set-external-id BRIDGE KEY VALUE set KEY on BRIDGE to VALUE\n\
372 br-set-external-id BRIDGE KEY unset KEY on BRIDGE\n\
373 br-get-external-id BRIDGE KEY print value of KEY on BRIDGE\n\
374 br-get-external-id BRIDGE list key-value pairs on BRIDGE\n\
375 \n\
376 Port commands (a bond is considered to be a single port):\n\
377 list-ports BRIDGE print the names of all the ports on BRIDGE\n\
378 add-port BRIDGE PORT add network device PORT to BRIDGE\n\
379 add-bond BRIDGE PORT IFACE... add bonded port PORT in BRIDGE from IFACES\n\
380 del-port [BRIDGE] PORT delete PORT (which may be bonded) from BRIDGE\n\
381 port-to-br PORT print name of bridge that contains PORT\n\
382 \n\
383 Interface commands (a bond consists of multiple interfaces):\n\
384 list-ifaces BRIDGE print the names of all interfaces on BRIDGE\n\
385 iface-to-br IFACE print name of bridge that contains IFACE\n\
386 \n\
387 Controller commands:\n\
388 get-controller BRIDGE print the controllers for BRIDGE\n\
389 del-controller BRIDGE delete the controllers for BRIDGE\n\
390 set-controller BRIDGE TARGET... set the controllers for BRIDGE\n\
391 get-fail-mode BRIDGE print the fail-mode for BRIDGE\n\
392 del-fail-mode BRIDGE delete the fail-mode for BRIDGE\n\
393 set-fail-mode BRIDGE MODE set the fail-mode for BRIDGE to MODE\n\
394 \n\
395 Manager commands:\n\
396 get-manager print the managers\n\
397 del-manager delete the managers\n\
398 set-manager TARGET... set the list of managers to TARGET...\n\
399 \n\
400 SSL commands:\n\
401 get-ssl print the SSL configuration\n\
402 del-ssl delete the SSL configuration\n\
403 set-ssl PRIV-KEY CERT CA-CERT set the SSL configuration\n\
404 \n\
405 Auto Attach commands:\n\
406 add-aa-mapping BRIDGE I-SID VLAN add Auto Attach mapping to BRIDGE\n\
407 del-aa-mapping BRIDGE I-SID VLAN delete Auto Attach mapping VLAN from BRIDGE\n\
408 get-aa-mapping BRIDGE get Auto Attach mappings from BRIDGE\n\
409 \n\
410 Switch commands:\n\
411 emer-reset reset switch to known good state\n\
412 \n\
413 %s\
414 \n\
415 Options:\n\
416 --db=DATABASE connect to DATABASE\n\
417 (default: %s)\n\
418 --no-wait do not wait for ovs-vswitchd to reconfigure\n\
419 --retry keep trying to connect to server forever\n\
420 -t, --timeout=SECS wait at most SECS seconds for ovs-vswitchd\n\
421 --dry-run do not commit changes to database\n\
422 --oneline print exactly one line of output per command\n",
423 program_name, program_name, ctl_get_db_cmd_usage(), ctl_default_db());
424 vlog_usage();
425 printf("\
426 --no-syslog equivalent to --verbose=vsctl:syslog:warn\n");
427 stream_usage("database", true, true, false);
428 printf("\n\
429 Other options:\n\
430 -h, --help display this help message\n\
431 -V, --version display version information\n");
432 exit(EXIT_SUCCESS);
433 }
434
435 \f
436 /* ovs-vsctl specific context. Inherits the 'struct ctl_context' as base. */
437 struct vsctl_context {
438 struct ctl_context base;
439
440 /* Modifiable state. */
441 const struct ovsrec_open_vswitch *ovs;
442 bool verified_ports;
443
444 /* A cache of the contents of the database.
445 *
446 * A command that needs to use any of this information must first call
447 * vsctl_context_populate_cache(). A command that changes anything that
448 * could invalidate the cache must either call
449 * vsctl_context_invalidate_cache() or manually update the cache to
450 * maintain its correctness. */
451 bool cache_valid;
452 struct shash bridges; /* Maps from bridge name to struct vsctl_bridge. */
453 struct shash ports; /* Maps from port name to struct vsctl_port. */
454 struct shash ifaces; /* Maps from port name to struct vsctl_iface. */
455 };
456
457 struct vsctl_bridge {
458 struct ovsrec_bridge *br_cfg;
459 char *name;
460 struct ovs_list ports; /* Contains "struct vsctl_port"s. */
461
462 /* VLAN ("fake") bridge support.
463 *
464 * Use 'parent != NULL' to detect a fake bridge, because 'vlan' can be 0
465 * in either case. */
466 struct hmap children; /* VLAN bridges indexed by 'vlan'. */
467 struct hmap_node children_node; /* Node in parent's 'children' hmap. */
468 struct vsctl_bridge *parent; /* Real bridge, or NULL. */
469 int vlan; /* VLAN VID (0...4095), or 0. */
470 };
471
472 struct vsctl_port {
473 struct ovs_list ports_node; /* In struct vsctl_bridge's 'ports' list. */
474 struct ovs_list ifaces; /* Contains "struct vsctl_iface"s. */
475 struct ovsrec_port *port_cfg;
476 struct vsctl_bridge *bridge;
477 };
478
479 struct vsctl_iface {
480 struct ovs_list ifaces_node; /* In struct vsctl_port's 'ifaces' list. */
481 struct ovsrec_interface *iface_cfg;
482 struct vsctl_port *port;
483 };
484
485 /* Casts 'base' into 'struct vsctl_context'. */
486 static struct vsctl_context *
487 vsctl_context_cast(struct ctl_context *base)
488 {
489 return CONTAINER_OF(base, struct vsctl_context, base);
490 }
491
492 static struct vsctl_bridge *find_vlan_bridge(struct vsctl_bridge *parent,
493 int vlan);
494
495 static char *
496 vsctl_context_to_string(const struct ctl_context *ctx)
497 {
498 const struct shash_node *node;
499 struct svec words;
500 char *s;
501 int i;
502
503 svec_init(&words);
504 SHASH_FOR_EACH (node, &ctx->options) {
505 svec_add(&words, node->name);
506 }
507 for (i = 0; i < ctx->argc; i++) {
508 svec_add(&words, ctx->argv[i]);
509 }
510 svec_terminate(&words);
511
512 s = process_escape_args(words.names);
513
514 svec_destroy(&words);
515
516 return s;
517 }
518
519 static void
520 verify_ports(struct vsctl_context *vsctl_ctx)
521 {
522 if (!vsctl_ctx->verified_ports) {
523 const struct ovsrec_bridge *bridge;
524 const struct ovsrec_port *port;
525
526 ovsrec_open_vswitch_verify_bridges(vsctl_ctx->ovs);
527 OVSREC_BRIDGE_FOR_EACH (bridge, vsctl_ctx->base.idl) {
528 ovsrec_bridge_verify_ports(bridge);
529 }
530 OVSREC_PORT_FOR_EACH (port, vsctl_ctx->base.idl) {
531 ovsrec_port_verify_interfaces(port);
532 }
533
534 vsctl_ctx->verified_ports = true;
535 }
536 }
537
538 static struct vsctl_bridge *
539 add_bridge_to_cache(struct vsctl_context *vsctl_ctx,
540 struct ovsrec_bridge *br_cfg, const char *name,
541 struct vsctl_bridge *parent, int vlan)
542 {
543 struct vsctl_bridge *br = xmalloc(sizeof *br);
544 br->br_cfg = br_cfg;
545 br->name = xstrdup(name);
546 ovs_list_init(&br->ports);
547 br->parent = parent;
548 br->vlan = vlan;
549 hmap_init(&br->children);
550 if (parent) {
551 struct vsctl_bridge *conflict = find_vlan_bridge(parent, vlan);
552 if (conflict) {
553 VLOG_WARN("%s: bridge has multiple VLAN bridges (%s and %s) "
554 "for VLAN %d, but only one is allowed",
555 parent->name, name, conflict->name, vlan);
556 } else {
557 hmap_insert(&parent->children, &br->children_node,
558 hash_int(vlan, 0));
559 }
560 }
561 shash_add(&vsctl_ctx->bridges, br->name, br);
562 return br;
563 }
564
565 static void
566 ovs_delete_bridge(const struct ovsrec_open_vswitch *ovs,
567 struct ovsrec_bridge *bridge)
568 {
569 struct ovsrec_bridge **bridges;
570 size_t i, n;
571
572 bridges = xmalloc(sizeof *ovs->bridges * ovs->n_bridges);
573 for (i = n = 0; i < ovs->n_bridges; i++) {
574 if (ovs->bridges[i] != bridge) {
575 bridges[n++] = ovs->bridges[i];
576 }
577 }
578 ovsrec_open_vswitch_set_bridges(ovs, bridges, n);
579 free(bridges);
580 }
581
582 static void
583 del_cached_bridge(struct vsctl_context *vsctl_ctx, struct vsctl_bridge *br)
584 {
585 ovs_assert(ovs_list_is_empty(&br->ports));
586 ovs_assert(hmap_is_empty(&br->children));
587 if (br->parent) {
588 hmap_remove(&br->parent->children, &br->children_node);
589 }
590 if (br->br_cfg) {
591 ovsrec_bridge_delete(br->br_cfg);
592 ovs_delete_bridge(vsctl_ctx->ovs, br->br_cfg);
593 }
594 shash_find_and_delete(&vsctl_ctx->bridges, br->name);
595 hmap_destroy(&br->children);
596 free(br->name);
597 free(br);
598 }
599
600 static bool
601 port_is_fake_bridge(const struct ovsrec_port *port_cfg)
602 {
603 return (port_cfg->fake_bridge
604 && port_cfg->tag
605 && *port_cfg->tag >= 0 && *port_cfg->tag <= 4095);
606 }
607
608 static struct vsctl_bridge *
609 find_vlan_bridge(struct vsctl_bridge *parent, int vlan)
610 {
611 struct vsctl_bridge *child;
612
613 HMAP_FOR_EACH_IN_BUCKET (child, children_node, hash_int(vlan, 0),
614 &parent->children) {
615 if (child->vlan == vlan) {
616 return child;
617 }
618 }
619
620 return NULL;
621 }
622
623 static struct vsctl_port *
624 add_port_to_cache(struct vsctl_context *vsctl_ctx, struct vsctl_bridge *parent,
625 struct ovsrec_port *port_cfg)
626 {
627 struct vsctl_port *port;
628
629 if (port_cfg->tag
630 && *port_cfg->tag >= 0 && *port_cfg->tag <= 4095) {
631 struct vsctl_bridge *vlan_bridge;
632
633 vlan_bridge = find_vlan_bridge(parent, *port_cfg->tag);
634 if (vlan_bridge) {
635 parent = vlan_bridge;
636 }
637 }
638
639 port = xmalloc(sizeof *port);
640 ovs_list_push_back(&parent->ports, &port->ports_node);
641 ovs_list_init(&port->ifaces);
642 port->port_cfg = port_cfg;
643 port->bridge = parent;
644 shash_add(&vsctl_ctx->ports, port_cfg->name, port);
645
646 return port;
647 }
648
649 static void
650 del_cached_port(struct vsctl_context *vsctl_ctx, struct vsctl_port *port)
651 {
652 ovs_assert(ovs_list_is_empty(&port->ifaces));
653 ovs_list_remove(&port->ports_node);
654 shash_find_and_delete(&vsctl_ctx->ports, port->port_cfg->name);
655 ovsrec_port_delete(port->port_cfg);
656 free(port);
657 }
658
659 static struct vsctl_iface *
660 add_iface_to_cache(struct vsctl_context *vsctl_ctx, struct vsctl_port *parent,
661 struct ovsrec_interface *iface_cfg)
662 {
663 struct vsctl_iface *iface;
664
665 iface = xmalloc(sizeof *iface);
666 ovs_list_push_back(&parent->ifaces, &iface->ifaces_node);
667 iface->iface_cfg = iface_cfg;
668 iface->port = parent;
669 shash_add(&vsctl_ctx->ifaces, iface_cfg->name, iface);
670
671 return iface;
672 }
673
674 static void
675 del_cached_iface(struct vsctl_context *vsctl_ctx, struct vsctl_iface *iface)
676 {
677 ovs_list_remove(&iface->ifaces_node);
678 shash_find_and_delete(&vsctl_ctx->ifaces, iface->iface_cfg->name);
679 ovsrec_interface_delete(iface->iface_cfg);
680 free(iface);
681 }
682
683 static void
684 vsctl_context_invalidate_cache(struct ctl_context *ctx)
685 {
686 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
687 struct shash_node *node;
688
689 if (!vsctl_ctx->cache_valid) {
690 return;
691 }
692 vsctl_ctx->cache_valid = false;
693
694 SHASH_FOR_EACH (node, &vsctl_ctx->bridges) {
695 struct vsctl_bridge *bridge = node->data;
696 hmap_destroy(&bridge->children);
697 free(bridge->name);
698 free(bridge);
699 }
700 shash_destroy(&vsctl_ctx->bridges);
701
702 shash_destroy_free_data(&vsctl_ctx->ports);
703 shash_destroy_free_data(&vsctl_ctx->ifaces);
704 }
705
706 static void
707 pre_get_info(struct ctl_context *ctx)
708 {
709 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_bridges);
710
711 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_name);
712 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_controller);
713 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_fail_mode);
714 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_ports);
715
716 ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_name);
717 ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_fake_bridge);
718 ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_tag);
719 ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_interfaces);
720
721 ovsdb_idl_add_column(ctx->idl, &ovsrec_interface_col_name);
722
723 ovsdb_idl_add_column(ctx->idl, &ovsrec_interface_col_ofport);
724 ovsdb_idl_add_column(ctx->idl, &ovsrec_interface_col_error);
725 }
726
727 static void
728 vsctl_context_populate_cache(struct ctl_context *ctx)
729 {
730 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
731 const struct ovsrec_open_vswitch *ovs = vsctl_ctx->ovs;
732 struct sset bridges, ports;
733 size_t i;
734
735 if (vsctl_ctx->cache_valid) {
736 /* Cache is already populated. */
737 return;
738 }
739 vsctl_ctx->cache_valid = true;
740 shash_init(&vsctl_ctx->bridges);
741 shash_init(&vsctl_ctx->ports);
742 shash_init(&vsctl_ctx->ifaces);
743
744 sset_init(&bridges);
745 sset_init(&ports);
746 for (i = 0; i < ovs->n_bridges; i++) {
747 struct ovsrec_bridge *br_cfg = ovs->bridges[i];
748 struct vsctl_bridge *br;
749 size_t j;
750
751 if (!sset_add(&bridges, br_cfg->name)) {
752 VLOG_WARN("%s: database contains duplicate bridge name",
753 br_cfg->name);
754 continue;
755 }
756 br = add_bridge_to_cache(vsctl_ctx, br_cfg, br_cfg->name, NULL, 0);
757
758 for (j = 0; j < br_cfg->n_ports; j++) {
759 struct ovsrec_port *port_cfg = br_cfg->ports[j];
760
761 if (!sset_add(&ports, port_cfg->name)) {
762 /* Duplicate port name. (We will warn about that later.) */
763 continue;
764 }
765
766 if (port_is_fake_bridge(port_cfg)
767 && sset_add(&bridges, port_cfg->name)) {
768 add_bridge_to_cache(vsctl_ctx, NULL, port_cfg->name, br,
769 *port_cfg->tag);
770 }
771 }
772 }
773 sset_destroy(&bridges);
774 sset_destroy(&ports);
775
776 sset_init(&bridges);
777 for (i = 0; i < ovs->n_bridges; i++) {
778 struct ovsrec_bridge *br_cfg = ovs->bridges[i];
779 struct vsctl_bridge *br;
780 size_t j;
781
782 if (!sset_add(&bridges, br_cfg->name)) {
783 continue;
784 }
785 br = shash_find_data(&vsctl_ctx->bridges, br_cfg->name);
786 for (j = 0; j < br_cfg->n_ports; j++) {
787 struct ovsrec_port *port_cfg = br_cfg->ports[j];
788 struct vsctl_port *port;
789 size_t k;
790
791 port = shash_find_data(&vsctl_ctx->ports, port_cfg->name);
792 if (port) {
793 if (port_cfg == port->port_cfg) {
794 VLOG_WARN("%s: port is in multiple bridges (%s and %s)",
795 port_cfg->name, br->name, port->bridge->name);
796 } else {
797 /* Log as an error because this violates the database's
798 * uniqueness constraints, so the database server shouldn't
799 * have allowed it. */
800 VLOG_ERR("%s: database contains duplicate port name",
801 port_cfg->name);
802 }
803 continue;
804 }
805
806 if (port_is_fake_bridge(port_cfg)
807 && !sset_add(&bridges, port_cfg->name)) {
808 continue;
809 }
810
811 port = add_port_to_cache(vsctl_ctx, br, port_cfg);
812 for (k = 0; k < port_cfg->n_interfaces; k++) {
813 struct ovsrec_interface *iface_cfg = port_cfg->interfaces[k];
814 struct vsctl_iface *iface;
815
816 iface = shash_find_data(&vsctl_ctx->ifaces, iface_cfg->name);
817 if (iface) {
818 if (iface_cfg == iface->iface_cfg) {
819 VLOG_WARN("%s: interface is in multiple ports "
820 "(%s and %s)",
821 iface_cfg->name,
822 iface->port->port_cfg->name,
823 port->port_cfg->name);
824 } else {
825 /* Log as an error because this violates the database's
826 * uniqueness constraints, so the database server
827 * shouldn't have allowed it. */
828 VLOG_ERR("%s: database contains duplicate interface "
829 "name", iface_cfg->name);
830 }
831 continue;
832 }
833
834 add_iface_to_cache(vsctl_ctx, port, iface_cfg);
835 }
836 }
837 }
838 sset_destroy(&bridges);
839 }
840
841 static void
842 check_conflicts(struct vsctl_context *vsctl_ctx, const char *name,
843 char *msg)
844 {
845 struct vsctl_iface *iface;
846 struct vsctl_port *port;
847
848 verify_ports(vsctl_ctx);
849
850 if (shash_find(&vsctl_ctx->bridges, name)) {
851 ctl_fatal("%s because a bridge named %s already exists",
852 msg, name);
853 }
854
855 port = shash_find_data(&vsctl_ctx->ports, name);
856 if (port) {
857 ctl_fatal("%s because a port named %s already exists on "
858 "bridge %s", msg, name, port->bridge->name);
859 }
860
861 iface = shash_find_data(&vsctl_ctx->ifaces, name);
862 if (iface) {
863 ctl_fatal("%s because an interface named %s already exists "
864 "on bridge %s", msg, name, iface->port->bridge->name);
865 }
866
867 free(msg);
868 }
869
870 static struct vsctl_bridge *
871 find_bridge(struct vsctl_context *vsctl_ctx, const char *name, bool must_exist)
872 {
873 struct vsctl_bridge *br;
874
875 ovs_assert(vsctl_ctx->cache_valid);
876
877 br = shash_find_data(&vsctl_ctx->bridges, name);
878 if (must_exist && !br) {
879 ctl_fatal("no bridge named %s", name);
880 }
881 ovsrec_open_vswitch_verify_bridges(vsctl_ctx->ovs);
882 return br;
883 }
884
885 static struct vsctl_bridge *
886 find_real_bridge(struct vsctl_context *vsctl_ctx,
887 const char *name, bool must_exist)
888 {
889 struct vsctl_bridge *br = find_bridge(vsctl_ctx, name, must_exist);
890 if (br && br->parent) {
891 ctl_fatal("%s is a fake bridge", name);
892 }
893 return br;
894 }
895
896 static struct vsctl_port *
897 find_port(struct vsctl_context *vsctl_ctx, const char *name, bool must_exist)
898 {
899 struct vsctl_port *port;
900
901 ovs_assert(vsctl_ctx->cache_valid);
902
903 port = shash_find_data(&vsctl_ctx->ports, name);
904 if (port && !strcmp(name, port->bridge->name)) {
905 port = NULL;
906 }
907 if (must_exist && !port) {
908 ctl_fatal("no port named %s", name);
909 }
910 verify_ports(vsctl_ctx);
911 return port;
912 }
913
914 static struct vsctl_iface *
915 find_iface(struct vsctl_context *vsctl_ctx, const char *name, bool must_exist)
916 {
917 struct vsctl_iface *iface;
918
919 ovs_assert(vsctl_ctx->cache_valid);
920
921 iface = shash_find_data(&vsctl_ctx->ifaces, name);
922 if (iface && !strcmp(name, iface->port->bridge->name)) {
923 iface = NULL;
924 }
925 if (must_exist && !iface) {
926 ctl_fatal("no interface named %s", name);
927 }
928 verify_ports(vsctl_ctx);
929 return iface;
930 }
931
932 static void
933 bridge_insert_port(struct ovsrec_bridge *br, struct ovsrec_port *port)
934 {
935 struct ovsrec_port **ports;
936 size_t i;
937
938 ports = xmalloc(sizeof *br->ports * (br->n_ports + 1));
939 for (i = 0; i < br->n_ports; i++) {
940 ports[i] = br->ports[i];
941 }
942 ports[br->n_ports] = port;
943 ovsrec_bridge_set_ports(br, ports, br->n_ports + 1);
944 free(ports);
945 }
946
947 static void
948 bridge_delete_port(struct ovsrec_bridge *br, struct ovsrec_port *port)
949 {
950 struct ovsrec_port **ports;
951 size_t i, n;
952
953 ports = xmalloc(sizeof *br->ports * br->n_ports);
954 for (i = n = 0; i < br->n_ports; i++) {
955 if (br->ports[i] != port) {
956 ports[n++] = br->ports[i];
957 }
958 }
959 ovsrec_bridge_set_ports(br, ports, n);
960 free(ports);
961 }
962
963 static void
964 ovs_insert_bridge(const struct ovsrec_open_vswitch *ovs,
965 struct ovsrec_bridge *bridge)
966 {
967 struct ovsrec_bridge **bridges;
968 size_t i;
969
970 bridges = xmalloc(sizeof *ovs->bridges * (ovs->n_bridges + 1));
971 for (i = 0; i < ovs->n_bridges; i++) {
972 bridges[i] = ovs->bridges[i];
973 }
974 bridges[ovs->n_bridges] = bridge;
975 ovsrec_open_vswitch_set_bridges(ovs, bridges, ovs->n_bridges + 1);
976 free(bridges);
977 }
978
979 static void
980 cmd_init(struct ctl_context *ctx OVS_UNUSED)
981 {
982 }
983
984 static struct cmd_show_table cmd_show_tables[] = {
985 {&ovsrec_table_open_vswitch,
986 NULL,
987 {&ovsrec_open_vswitch_col_manager_options,
988 &ovsrec_open_vswitch_col_bridges,
989 &ovsrec_open_vswitch_col_ovs_version},
990 {NULL, NULL, NULL}
991 },
992
993 {&ovsrec_table_bridge,
994 &ovsrec_bridge_col_name,
995 {&ovsrec_bridge_col_controller,
996 &ovsrec_bridge_col_fail_mode,
997 &ovsrec_bridge_col_ports},
998 {NULL, NULL, NULL}
999 },
1000
1001 {&ovsrec_table_port,
1002 &ovsrec_port_col_name,
1003 {&ovsrec_port_col_tag,
1004 &ovsrec_port_col_trunks,
1005 &ovsrec_port_col_interfaces},
1006 {NULL, NULL, NULL}
1007 },
1008
1009 {&ovsrec_table_interface,
1010 &ovsrec_interface_col_name,
1011 {&ovsrec_interface_col_type,
1012 &ovsrec_interface_col_options,
1013 &ovsrec_interface_col_error},
1014 {NULL, NULL, NULL}
1015 },
1016
1017 {&ovsrec_table_controller,
1018 &ovsrec_controller_col_target,
1019 {&ovsrec_controller_col_is_connected,
1020 NULL,
1021 NULL},
1022 {NULL, NULL, NULL}
1023 },
1024
1025 {&ovsrec_table_manager,
1026 &ovsrec_manager_col_target,
1027 {&ovsrec_manager_col_is_connected,
1028 NULL,
1029 NULL},
1030 {NULL, NULL, NULL}
1031 },
1032
1033 {NULL, NULL, {NULL, NULL, NULL}, {NULL, NULL, NULL}}
1034 };
1035
1036 static void
1037 pre_cmd_emer_reset(struct ctl_context *ctx)
1038 {
1039 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_manager_options);
1040 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_ssl);
1041
1042 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_controller);
1043 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_fail_mode);
1044 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_mirrors);
1045 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_netflow);
1046 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_sflow);
1047 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_ipfix);
1048 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_flood_vlans);
1049 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_other_config);
1050
1051 ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_other_config);
1052
1053 ovsdb_idl_add_column(ctx->idl,
1054 &ovsrec_interface_col_ingress_policing_rate);
1055 ovsdb_idl_add_column(ctx->idl,
1056 &ovsrec_interface_col_ingress_policing_burst);
1057 }
1058
1059 static void
1060 cmd_emer_reset(struct ctl_context *ctx)
1061 {
1062 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
1063 const struct ovsdb_idl *idl = ctx->idl;
1064 const struct ovsrec_bridge *br;
1065 const struct ovsrec_port *port;
1066 const struct ovsrec_interface *iface;
1067 const struct ovsrec_mirror *mirror, *next_mirror;
1068 const struct ovsrec_controller *ctrl, *next_ctrl;
1069 const struct ovsrec_manager *mgr, *next_mgr;
1070 const struct ovsrec_netflow *nf, *next_nf;
1071 const struct ovsrec_ssl *ssl, *next_ssl;
1072 const struct ovsrec_sflow *sflow, *next_sflow;
1073 const struct ovsrec_ipfix *ipfix, *next_ipfix;
1074 const struct ovsrec_flow_sample_collector_set *fscset, *next_fscset;
1075
1076 /* Reset the Open_vSwitch table. */
1077 ovsrec_open_vswitch_set_manager_options(vsctl_ctx->ovs, NULL, 0);
1078 ovsrec_open_vswitch_set_ssl(vsctl_ctx->ovs, NULL);
1079
1080 OVSREC_BRIDGE_FOR_EACH (br, idl) {
1081 const char *hwaddr;
1082
1083 ovsrec_bridge_set_controller(br, NULL, 0);
1084 ovsrec_bridge_set_fail_mode(br, NULL);
1085 ovsrec_bridge_set_mirrors(br, NULL, 0);
1086 ovsrec_bridge_set_netflow(br, NULL);
1087 ovsrec_bridge_set_sflow(br, NULL);
1088 ovsrec_bridge_set_ipfix(br, NULL);
1089 ovsrec_bridge_set_flood_vlans(br, NULL, 0);
1090
1091 /* We only want to save the "hwaddr" key from other_config. */
1092 hwaddr = smap_get(&br->other_config, "hwaddr");
1093 if (hwaddr) {
1094 const struct smap smap = SMAP_CONST1(&smap, "hwaddr", hwaddr);
1095 ovsrec_bridge_set_other_config(br, &smap);
1096 } else {
1097 ovsrec_bridge_set_other_config(br, NULL);
1098 }
1099 }
1100
1101 OVSREC_PORT_FOR_EACH (port, idl) {
1102 ovsrec_port_set_other_config(port, NULL);
1103 }
1104
1105 OVSREC_INTERFACE_FOR_EACH (iface, idl) {
1106 /* xxx What do we do about gre/patch devices created by mgr? */
1107
1108 ovsrec_interface_set_ingress_policing_rate(iface, 0);
1109 ovsrec_interface_set_ingress_policing_burst(iface, 0);
1110 }
1111
1112 OVSREC_MIRROR_FOR_EACH_SAFE (mirror, next_mirror, idl) {
1113 ovsrec_mirror_delete(mirror);
1114 }
1115
1116 OVSREC_CONTROLLER_FOR_EACH_SAFE (ctrl, next_ctrl, idl) {
1117 ovsrec_controller_delete(ctrl);
1118 }
1119
1120 OVSREC_MANAGER_FOR_EACH_SAFE (mgr, next_mgr, idl) {
1121 ovsrec_manager_delete(mgr);
1122 }
1123
1124 OVSREC_NETFLOW_FOR_EACH_SAFE (nf, next_nf, idl) {
1125 ovsrec_netflow_delete(nf);
1126 }
1127
1128 OVSREC_SSL_FOR_EACH_SAFE (ssl, next_ssl, idl) {
1129 ovsrec_ssl_delete(ssl);
1130 }
1131
1132 OVSREC_SFLOW_FOR_EACH_SAFE (sflow, next_sflow, idl) {
1133 ovsrec_sflow_delete(sflow);
1134 }
1135
1136 OVSREC_IPFIX_FOR_EACH_SAFE (ipfix, next_ipfix, idl) {
1137 ovsrec_ipfix_delete(ipfix);
1138 }
1139
1140 OVSREC_FLOW_SAMPLE_COLLECTOR_SET_FOR_EACH_SAFE (fscset, next_fscset, idl) {
1141 ovsrec_flow_sample_collector_set_delete(fscset);
1142 }
1143
1144 vsctl_context_invalidate_cache(ctx);
1145 }
1146
1147 static void
1148 cmd_add_br(struct ctl_context *ctx)
1149 {
1150 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
1151 bool may_exist = shash_find(&ctx->options, "--may-exist") != NULL;
1152 const char *br_name, *parent_name;
1153 struct ovsrec_interface *iface;
1154 int vlan;
1155
1156 br_name = ctx->argv[1];
1157 if (!br_name[0]) {
1158 ctl_fatal("bridge name must not be empty string");
1159 }
1160 if (ctx->argc == 2) {
1161 parent_name = NULL;
1162 vlan = 0;
1163 } else if (ctx->argc == 4) {
1164 parent_name = ctx->argv[2];
1165 vlan = atoi(ctx->argv[3]);
1166 if (vlan < 0 || vlan > 4095) {
1167 ctl_fatal("%s: vlan must be between 0 and 4095", ctx->argv[0]);
1168 }
1169 } else {
1170 ctl_fatal("'%s' command takes exactly 1 or 3 arguments",
1171 ctx->argv[0]);
1172 }
1173
1174 vsctl_context_populate_cache(ctx);
1175 if (may_exist) {
1176 struct vsctl_bridge *br;
1177
1178 br = find_bridge(vsctl_ctx, br_name, false);
1179 if (br) {
1180 if (!parent_name) {
1181 if (br->parent) {
1182 ctl_fatal("\"--may-exist add-br %s\" but %s is "
1183 "a VLAN bridge for VLAN %d",
1184 br_name, br_name, br->vlan);
1185 }
1186 } else {
1187 if (!br->parent) {
1188 ctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
1189 "is not a VLAN bridge",
1190 br_name, parent_name, vlan, br_name);
1191 } else if (strcmp(br->parent->name, parent_name)) {
1192 ctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
1193 "has the wrong parent %s",
1194 br_name, parent_name, vlan,
1195 br_name, br->parent->name);
1196 } else if (br->vlan != vlan) {
1197 ctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
1198 "is a VLAN bridge for the wrong VLAN %d",
1199 br_name, parent_name, vlan, br_name, br->vlan);
1200 }
1201 }
1202 return;
1203 }
1204 }
1205 check_conflicts(vsctl_ctx, br_name,
1206 xasprintf("cannot create a bridge named %s", br_name));
1207
1208 if (!parent_name) {
1209 struct ovsrec_port *port;
1210 struct ovsrec_bridge *br;
1211
1212 iface = ovsrec_interface_insert(ctx->txn);
1213 ovsrec_interface_set_name(iface, br_name);
1214 ovsrec_interface_set_type(iface, "internal");
1215
1216 port = ovsrec_port_insert(ctx->txn);
1217 ovsrec_port_set_name(port, br_name);
1218 ovsrec_port_set_interfaces(port, &iface, 1);
1219
1220 br = ovsrec_bridge_insert(ctx->txn);
1221 ovsrec_bridge_set_name(br, br_name);
1222 ovsrec_bridge_set_ports(br, &port, 1);
1223
1224 ovs_insert_bridge(vsctl_ctx->ovs, br);
1225 } else {
1226 struct vsctl_bridge *conflict;
1227 struct vsctl_bridge *parent;
1228 struct ovsrec_port *port;
1229 struct ovsrec_bridge *br;
1230 int64_t tag = vlan;
1231
1232 parent = find_bridge(vsctl_ctx, parent_name, false);
1233 if (parent && parent->parent) {
1234 ctl_fatal("cannot create bridge with fake bridge as parent");
1235 }
1236 if (!parent) {
1237 ctl_fatal("parent bridge %s does not exist", parent_name);
1238 }
1239 conflict = find_vlan_bridge(parent, vlan);
1240 if (conflict) {
1241 ctl_fatal("bridge %s already has a child VLAN bridge %s "
1242 "on VLAN %d", parent_name, conflict->name, vlan);
1243 }
1244 br = parent->br_cfg;
1245
1246 iface = ovsrec_interface_insert(ctx->txn);
1247 ovsrec_interface_set_name(iface, br_name);
1248 ovsrec_interface_set_type(iface, "internal");
1249
1250 port = ovsrec_port_insert(ctx->txn);
1251 ovsrec_port_set_name(port, br_name);
1252 ovsrec_port_set_interfaces(port, &iface, 1);
1253 ovsrec_port_set_fake_bridge(port, true);
1254 ovsrec_port_set_tag(port, &tag, 1);
1255
1256 bridge_insert_port(br, port);
1257 }
1258
1259 post_db_reload_expect_iface(iface);
1260 vsctl_context_invalidate_cache(ctx);
1261 }
1262
1263 static void
1264 del_port(struct vsctl_context *vsctl_ctx, struct vsctl_port *port)
1265 {
1266 struct vsctl_iface *iface, *next_iface;
1267
1268 bridge_delete_port((port->bridge->parent
1269 ? port->bridge->parent->br_cfg
1270 : port->bridge->br_cfg), port->port_cfg);
1271
1272 LIST_FOR_EACH_SAFE (iface, next_iface, ifaces_node, &port->ifaces) {
1273 del_cached_iface(vsctl_ctx, iface);
1274 }
1275 del_cached_port(vsctl_ctx, port);
1276 }
1277
1278 static void
1279 del_bridge(struct vsctl_context *vsctl_ctx, struct vsctl_bridge *br)
1280 {
1281 struct vsctl_bridge *child, *next_child;
1282 struct vsctl_port *port, *next_port;
1283 const struct ovsrec_flow_sample_collector_set *fscset, *next_fscset;
1284
1285 HMAP_FOR_EACH_SAFE (child, next_child, children_node, &br->children) {
1286 del_bridge(vsctl_ctx, child);
1287 }
1288
1289 LIST_FOR_EACH_SAFE (port, next_port, ports_node, &br->ports) {
1290 del_port(vsctl_ctx, port);
1291 }
1292
1293 OVSREC_FLOW_SAMPLE_COLLECTOR_SET_FOR_EACH_SAFE (fscset, next_fscset,
1294 vsctl_ctx->base.idl) {
1295 if (fscset->bridge == br->br_cfg) {
1296 ovsrec_flow_sample_collector_set_delete(fscset);
1297 }
1298 }
1299
1300 del_cached_bridge(vsctl_ctx, br);
1301 }
1302
1303 static void
1304 cmd_del_br(struct ctl_context *ctx)
1305 {
1306 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
1307 bool must_exist = !shash_find(&ctx->options, "--if-exists");
1308 struct vsctl_bridge *bridge;
1309
1310 vsctl_context_populate_cache(ctx);
1311 bridge = find_bridge(vsctl_ctx, ctx->argv[1], must_exist);
1312 if (bridge) {
1313 del_bridge(vsctl_ctx, bridge);
1314 }
1315 }
1316
1317 static void
1318 output_sorted(struct svec *svec, struct ds *output)
1319 {
1320 const char *name;
1321 size_t i;
1322
1323 svec_sort(svec);
1324 SVEC_FOR_EACH (i, name, svec) {
1325 ds_put_format(output, "%s\n", name);
1326 }
1327 }
1328
1329 static void
1330 cmd_list_br(struct ctl_context *ctx)
1331 {
1332 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
1333 struct shash_node *node;
1334 struct svec bridges;
1335 bool real = shash_find(&ctx->options, "--real");
1336 bool fake = shash_find(&ctx->options, "--fake");
1337
1338 /* If neither fake nor real were requested, return both. */
1339 if (!real && !fake) {
1340 real = fake = true;
1341 }
1342
1343 vsctl_context_populate_cache(ctx);
1344
1345 svec_init(&bridges);
1346 SHASH_FOR_EACH (node, &vsctl_ctx->bridges) {
1347 struct vsctl_bridge *br = node->data;
1348
1349 if (br->parent ? fake : real) {
1350 svec_add(&bridges, br->name);
1351 }
1352 }
1353 output_sorted(&bridges, &ctx->output);
1354 svec_destroy(&bridges);
1355 }
1356
1357 static void
1358 cmd_br_exists(struct ctl_context *ctx)
1359 {
1360 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
1361
1362 vsctl_context_populate_cache(ctx);
1363 if (!find_bridge(vsctl_ctx, ctx->argv[1], false)) {
1364 vsctl_exit(2);
1365 }
1366 }
1367
1368 static void
1369 set_external_id(struct smap *old, struct smap *new,
1370 char *key, char *value)
1371 {
1372 smap_clone(new, old);
1373
1374 if (value) {
1375 smap_replace(new, key, value);
1376 } else {
1377 smap_remove(new, key);
1378 }
1379 }
1380
1381 static void
1382 pre_cmd_br_set_external_id(struct ctl_context *ctx)
1383 {
1384 pre_get_info(ctx);
1385 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_external_ids);
1386 ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_external_ids);
1387 }
1388
1389 static void
1390 cmd_br_set_external_id(struct ctl_context *ctx)
1391 {
1392 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
1393 struct vsctl_bridge *bridge;
1394 struct smap new;
1395
1396 vsctl_context_populate_cache(ctx);
1397 bridge = find_bridge(vsctl_ctx, ctx->argv[1], true);
1398 if (bridge->br_cfg) {
1399
1400 set_external_id(&bridge->br_cfg->external_ids, &new, ctx->argv[2],
1401 ctx->argc >= 4 ? ctx->argv[3] : NULL);
1402 ovsrec_bridge_verify_external_ids(bridge->br_cfg);
1403 ovsrec_bridge_set_external_ids(bridge->br_cfg, &new);
1404 } else {
1405 char *key = xasprintf("fake-bridge-%s", ctx->argv[2]);
1406 struct vsctl_port *port = shash_find_data(&vsctl_ctx->ports,
1407 ctx->argv[1]);
1408 set_external_id(&port->port_cfg->external_ids, &new,
1409 key, ctx->argc >= 4 ? ctx->argv[3] : NULL);
1410 ovsrec_port_verify_external_ids(port->port_cfg);
1411 ovsrec_port_set_external_ids(port->port_cfg, &new);
1412 free(key);
1413 }
1414 smap_destroy(&new);
1415 }
1416
1417 static void
1418 get_external_id(struct smap *smap, const char *prefix, const char *key,
1419 struct ds *output)
1420 {
1421 if (key) {
1422 char *prefix_key = xasprintf("%s%s", prefix, key);
1423 const char *value = smap_get(smap, prefix_key);
1424
1425 if (value) {
1426 ds_put_format(output, "%s\n", value);
1427 }
1428 free(prefix_key);
1429 } else {
1430 const struct smap_node **sorted = smap_sort(smap);
1431 size_t prefix_len = strlen(prefix);
1432 size_t i;
1433
1434 for (i = 0; i < smap_count(smap); i++) {
1435 const struct smap_node *node = sorted[i];
1436 if (!strncmp(node->key, prefix, prefix_len)) {
1437 ds_put_format(output, "%s=%s\n", node->key + prefix_len,
1438 node->value);
1439 }
1440 }
1441 free(sorted);
1442 }
1443 }
1444
1445 static void
1446 pre_cmd_br_get_external_id(struct ctl_context *ctx)
1447 {
1448 pre_cmd_br_set_external_id(ctx);
1449 }
1450
1451 static void
1452 cmd_br_get_external_id(struct ctl_context *ctx)
1453 {
1454 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
1455 struct vsctl_bridge *bridge;
1456
1457 vsctl_context_populate_cache(ctx);
1458
1459 bridge = find_bridge(vsctl_ctx, ctx->argv[1], true);
1460 if (bridge->br_cfg) {
1461 ovsrec_bridge_verify_external_ids(bridge->br_cfg);
1462 get_external_id(&bridge->br_cfg->external_ids, "",
1463 ctx->argc >= 3 ? ctx->argv[2] : NULL, &ctx->output);
1464 } else {
1465 struct vsctl_port *port = shash_find_data(&vsctl_ctx->ports,
1466 ctx->argv[1]);
1467 ovsrec_port_verify_external_ids(port->port_cfg);
1468 get_external_id(&port->port_cfg->external_ids, "fake-bridge-",
1469 ctx->argc >= 3 ? ctx->argv[2] : NULL, &ctx->output);
1470 }
1471 }
1472
1473 static void
1474 cmd_list_ports(struct ctl_context *ctx)
1475 {
1476 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
1477 struct vsctl_bridge *br;
1478 struct vsctl_port *port;
1479 struct svec ports;
1480
1481 vsctl_context_populate_cache(ctx);
1482 br = find_bridge(vsctl_ctx, ctx->argv[1], true);
1483 ovsrec_bridge_verify_ports(br->br_cfg ? br->br_cfg : br->parent->br_cfg);
1484
1485 svec_init(&ports);
1486 LIST_FOR_EACH (port, ports_node, &br->ports) {
1487 if (strcmp(port->port_cfg->name, br->name)) {
1488 svec_add(&ports, port->port_cfg->name);
1489 }
1490 }
1491 output_sorted(&ports, &ctx->output);
1492 svec_destroy(&ports);
1493 }
1494
1495 static void
1496 add_port(struct ctl_context *ctx,
1497 const char *br_name, const char *port_name,
1498 bool may_exist, bool fake_iface,
1499 char *iface_names[], int n_ifaces,
1500 char *settings[], int n_settings)
1501 {
1502 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
1503 struct vsctl_port *vsctl_port;
1504 struct vsctl_bridge *bridge;
1505 struct ovsrec_interface **ifaces;
1506 struct ovsrec_port *port;
1507 size_t i;
1508
1509 if (!port_name[0]) {
1510 ctl_fatal("port name must not be empty string");
1511 }
1512 for (i = 0; i < n_ifaces; i++) {
1513 if (!iface_names[i][0]) {
1514 ctl_fatal("interface name must not be empty string");
1515 }
1516 }
1517
1518 vsctl_context_populate_cache(ctx);
1519 if (may_exist) {
1520 struct vsctl_port *vsctl_port;
1521
1522 vsctl_port = find_port(vsctl_ctx, port_name, false);
1523 if (vsctl_port) {
1524 struct svec want_names, have_names;
1525
1526 svec_init(&want_names);
1527 for (i = 0; i < n_ifaces; i++) {
1528 svec_add(&want_names, iface_names[i]);
1529 }
1530 svec_sort(&want_names);
1531
1532 svec_init(&have_names);
1533 for (i = 0; i < vsctl_port->port_cfg->n_interfaces; i++) {
1534 svec_add(&have_names,
1535 vsctl_port->port_cfg->interfaces[i]->name);
1536 }
1537 svec_sort(&have_names);
1538
1539 if (strcmp(vsctl_port->bridge->name, br_name)) {
1540 char *command = vsctl_context_to_string(ctx);
1541 ctl_fatal("\"%s\" but %s is actually attached to bridge %s",
1542 command, port_name, vsctl_port->bridge->name);
1543 }
1544
1545 if (!svec_equal(&want_names, &have_names)) {
1546 char *have_names_string = svec_join(&have_names, ", ", "");
1547 char *command = vsctl_context_to_string(ctx);
1548
1549 ctl_fatal("\"%s\" but %s actually has interface(s) %s",
1550 command, port_name, have_names_string);
1551 }
1552
1553 svec_destroy(&want_names);
1554 svec_destroy(&have_names);
1555
1556 return;
1557 }
1558 }
1559 check_conflicts(vsctl_ctx, port_name,
1560 xasprintf("cannot create a port named %s", port_name));
1561 for (i = 0; i < n_ifaces; i++) {
1562 check_conflicts(vsctl_ctx, iface_names[i],
1563 xasprintf("cannot create an interface named %s",
1564 iface_names[i]));
1565 }
1566 bridge = find_bridge(vsctl_ctx, br_name, true);
1567
1568 ifaces = xmalloc(n_ifaces * sizeof *ifaces);
1569 for (i = 0; i < n_ifaces; i++) {
1570 ifaces[i] = ovsrec_interface_insert(ctx->txn);
1571 ovsrec_interface_set_name(ifaces[i], iface_names[i]);
1572 post_db_reload_expect_iface(ifaces[i]);
1573 }
1574
1575 port = ovsrec_port_insert(ctx->txn);
1576 ovsrec_port_set_name(port, port_name);
1577 ovsrec_port_set_interfaces(port, ifaces, n_ifaces);
1578 ovsrec_port_set_bond_fake_iface(port, fake_iface);
1579
1580 if (bridge->parent) {
1581 int64_t tag = bridge->vlan;
1582 ovsrec_port_set_tag(port, &tag, 1);
1583 }
1584
1585 for (i = 0; i < n_settings; i++) {
1586 ctl_set_column("Port", &port->header_, settings[i],
1587 ctx->symtab);
1588 }
1589
1590 bridge_insert_port((bridge->parent ? bridge->parent->br_cfg
1591 : bridge->br_cfg), port);
1592
1593 vsctl_port = add_port_to_cache(vsctl_ctx, bridge, port);
1594 for (i = 0; i < n_ifaces; i++) {
1595 add_iface_to_cache(vsctl_ctx, vsctl_port, ifaces[i]);
1596 }
1597 free(ifaces);
1598 }
1599
1600 static void
1601 cmd_add_port(struct ctl_context *ctx)
1602 {
1603 bool may_exist = shash_find(&ctx->options, "--may-exist") != NULL;
1604
1605 add_port(ctx, ctx->argv[1], ctx->argv[2], may_exist, false,
1606 &ctx->argv[2], 1, &ctx->argv[3], ctx->argc - 3);
1607 }
1608
1609 static void
1610 cmd_add_bond(struct ctl_context *ctx)
1611 {
1612 bool may_exist = shash_find(&ctx->options, "--may-exist") != NULL;
1613 bool fake_iface = shash_find(&ctx->options, "--fake-iface");
1614 int n_ifaces;
1615 int i;
1616
1617 n_ifaces = ctx->argc - 3;
1618 for (i = 3; i < ctx->argc; i++) {
1619 if (strchr(ctx->argv[i], '=')) {
1620 n_ifaces = i - 3;
1621 break;
1622 }
1623 }
1624 if (n_ifaces < 2) {
1625 ctl_fatal("add-bond requires at least 2 interfaces, but only "
1626 "%d were specified", n_ifaces);
1627 }
1628
1629 add_port(ctx, ctx->argv[1], ctx->argv[2], may_exist, fake_iface,
1630 &ctx->argv[3], n_ifaces,
1631 &ctx->argv[n_ifaces + 3], ctx->argc - 3 - n_ifaces);
1632 }
1633
1634 static void
1635 cmd_del_port(struct ctl_context *ctx)
1636 {
1637 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
1638 bool must_exist = !shash_find(&ctx->options, "--if-exists");
1639 bool with_iface = shash_find(&ctx->options, "--with-iface") != NULL;
1640 const char *target = ctx->argv[ctx->argc - 1];
1641 struct vsctl_port *port;
1642
1643 vsctl_context_populate_cache(ctx);
1644 if (find_bridge(vsctl_ctx, target, false)) {
1645 if (must_exist) {
1646 ctl_fatal("cannot delete port %s because it is the local port "
1647 "for bridge %s (deleting this port requires deleting "
1648 "the entire bridge)", target, target);
1649 }
1650 port = NULL;
1651 } else if (!with_iface) {
1652 port = find_port(vsctl_ctx, target, must_exist);
1653 } else {
1654 struct vsctl_iface *iface;
1655
1656 port = find_port(vsctl_ctx, target, false);
1657 if (!port) {
1658 iface = find_iface(vsctl_ctx, target, false);
1659 if (iface) {
1660 port = iface->port;
1661 }
1662 }
1663 if (must_exist && !port) {
1664 ctl_fatal("no port or interface named %s", target);
1665 }
1666 }
1667
1668 if (port) {
1669 if (ctx->argc == 3) {
1670 struct vsctl_bridge *bridge;
1671
1672 bridge = find_bridge(vsctl_ctx, ctx->argv[1], true);
1673 if (port->bridge != bridge) {
1674 if (port->bridge->parent == bridge) {
1675 ctl_fatal("bridge %s does not have a port %s (although "
1676 "its parent bridge %s does)",
1677 ctx->argv[1], ctx->argv[2],
1678 bridge->parent->name);
1679 } else {
1680 ctl_fatal("bridge %s does not have a port %s",
1681 ctx->argv[1], ctx->argv[2]);
1682 }
1683 }
1684 }
1685
1686 del_port(vsctl_ctx, port);
1687 }
1688 }
1689
1690 static void
1691 cmd_port_to_br(struct ctl_context *ctx)
1692 {
1693 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
1694 struct vsctl_port *port;
1695
1696 vsctl_context_populate_cache(ctx);
1697
1698 port = find_port(vsctl_ctx, ctx->argv[1], true);
1699 ds_put_format(&ctx->output, "%s\n", port->bridge->name);
1700 }
1701
1702 static void
1703 cmd_br_to_vlan(struct ctl_context *ctx)
1704 {
1705 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
1706 struct vsctl_bridge *bridge;
1707
1708 vsctl_context_populate_cache(ctx);
1709
1710 bridge = find_bridge(vsctl_ctx, ctx->argv[1], true);
1711 ds_put_format(&ctx->output, "%d\n", bridge->vlan);
1712 }
1713
1714 static void
1715 cmd_br_to_parent(struct ctl_context *ctx)
1716 {
1717 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
1718 struct vsctl_bridge *bridge;
1719
1720 vsctl_context_populate_cache(ctx);
1721
1722 bridge = find_bridge(vsctl_ctx, ctx->argv[1], true);
1723 if (bridge->parent) {
1724 bridge = bridge->parent;
1725 }
1726 ds_put_format(&ctx->output, "%s\n", bridge->name);
1727 }
1728
1729 static void
1730 cmd_list_ifaces(struct ctl_context *ctx)
1731 {
1732 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
1733 struct vsctl_bridge *br;
1734 struct vsctl_port *port;
1735 struct svec ifaces;
1736
1737 vsctl_context_populate_cache(ctx);
1738
1739 br = find_bridge(vsctl_ctx, ctx->argv[1], true);
1740 verify_ports(vsctl_ctx);
1741
1742 svec_init(&ifaces);
1743 LIST_FOR_EACH (port, ports_node, &br->ports) {
1744 struct vsctl_iface *iface;
1745
1746 LIST_FOR_EACH (iface, ifaces_node, &port->ifaces) {
1747 if (strcmp(iface->iface_cfg->name, br->name)) {
1748 svec_add(&ifaces, iface->iface_cfg->name);
1749 }
1750 }
1751 }
1752 output_sorted(&ifaces, &ctx->output);
1753 svec_destroy(&ifaces);
1754 }
1755
1756 static void
1757 cmd_iface_to_br(struct ctl_context *ctx)
1758 {
1759 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
1760 struct vsctl_iface *iface;
1761
1762 vsctl_context_populate_cache(ctx);
1763
1764 iface = find_iface(vsctl_ctx, ctx->argv[1], true);
1765 ds_put_format(&ctx->output, "%s\n", iface->port->bridge->name);
1766 }
1767
1768 static void
1769 verify_controllers(struct ovsrec_bridge *bridge)
1770 {
1771 size_t i;
1772
1773 ovsrec_bridge_verify_controller(bridge);
1774 for (i = 0; i < bridge->n_controller; i++) {
1775 ovsrec_controller_verify_target(bridge->controller[i]);
1776 }
1777 }
1778
1779 static void
1780 pre_controller(struct ctl_context *ctx)
1781 {
1782 pre_get_info(ctx);
1783
1784 ovsdb_idl_add_column(ctx->idl, &ovsrec_controller_col_target);
1785 }
1786
1787 static void
1788 cmd_get_controller(struct ctl_context *ctx)
1789 {
1790 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
1791 struct vsctl_bridge *br;
1792 struct svec targets;
1793 size_t i;
1794
1795 vsctl_context_populate_cache(ctx);
1796
1797 br = find_bridge(vsctl_ctx, ctx->argv[1], true);
1798 if (br->parent) {
1799 br = br->parent;
1800 }
1801 verify_controllers(br->br_cfg);
1802
1803 /* Print the targets in sorted order for reproducibility. */
1804 svec_init(&targets);
1805 for (i = 0; i < br->br_cfg->n_controller; i++) {
1806 svec_add(&targets, br->br_cfg->controller[i]->target);
1807 }
1808
1809 svec_sort(&targets);
1810 for (i = 0; i < targets.n; i++) {
1811 ds_put_format(&ctx->output, "%s\n", targets.names[i]);
1812 }
1813 svec_destroy(&targets);
1814 }
1815
1816 static void
1817 delete_controllers(struct ovsrec_controller **controllers,
1818 size_t n_controllers)
1819 {
1820 size_t i;
1821
1822 for (i = 0; i < n_controllers; i++) {
1823 ovsrec_controller_delete(controllers[i]);
1824 }
1825 }
1826
1827 static void
1828 cmd_del_controller(struct ctl_context *ctx)
1829 {
1830 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
1831 struct ovsrec_bridge *br;
1832
1833 vsctl_context_populate_cache(ctx);
1834
1835 br = find_real_bridge(vsctl_ctx, ctx->argv[1], true)->br_cfg;
1836 verify_controllers(br);
1837
1838 if (br->controller) {
1839 delete_controllers(br->controller, br->n_controller);
1840 ovsrec_bridge_set_controller(br, NULL, 0);
1841 }
1842 }
1843
1844 static struct ovsrec_controller **
1845 insert_controllers(struct ovsdb_idl_txn *txn, char *targets[], size_t n)
1846 {
1847 struct ovsrec_controller **controllers;
1848 size_t i;
1849
1850 controllers = xmalloc(n * sizeof *controllers);
1851 for (i = 0; i < n; i++) {
1852 if (vconn_verify_name(targets[i]) && pvconn_verify_name(targets[i])) {
1853 VLOG_WARN("target type \"%s\" is possibly erroneous", targets[i]);
1854 }
1855 controllers[i] = ovsrec_controller_insert(txn);
1856 ovsrec_controller_set_target(controllers[i], targets[i]);
1857 }
1858
1859 return controllers;
1860 }
1861
1862 static void
1863 cmd_set_controller(struct ctl_context *ctx)
1864 {
1865 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
1866 struct ovsrec_controller **controllers;
1867 struct ovsrec_bridge *br;
1868 size_t n;
1869
1870 vsctl_context_populate_cache(ctx);
1871
1872 br = find_real_bridge(vsctl_ctx, ctx->argv[1], true)->br_cfg;
1873 verify_controllers(br);
1874
1875 delete_controllers(br->controller, br->n_controller);
1876
1877 n = ctx->argc - 2;
1878 controllers = insert_controllers(ctx->txn, &ctx->argv[2], n);
1879 ovsrec_bridge_set_controller(br, controllers, n);
1880 free(controllers);
1881 }
1882
1883 static void
1884 cmd_get_fail_mode(struct ctl_context *ctx)
1885 {
1886 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
1887 struct vsctl_bridge *br;
1888 const char *fail_mode;
1889
1890 vsctl_context_populate_cache(ctx);
1891 br = find_bridge(vsctl_ctx, ctx->argv[1], true);
1892
1893 if (br->parent) {
1894 br = br->parent;
1895 }
1896 ovsrec_bridge_verify_fail_mode(br->br_cfg);
1897
1898 fail_mode = br->br_cfg->fail_mode;
1899 if (fail_mode && strlen(fail_mode)) {
1900 ds_put_format(&ctx->output, "%s\n", fail_mode);
1901 }
1902 }
1903
1904 static void
1905 cmd_del_fail_mode(struct ctl_context *ctx)
1906 {
1907 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
1908 struct vsctl_bridge *br;
1909
1910 vsctl_context_populate_cache(ctx);
1911
1912 br = find_real_bridge(vsctl_ctx, ctx->argv[1], true);
1913
1914 ovsrec_bridge_set_fail_mode(br->br_cfg, NULL);
1915 }
1916
1917 static void
1918 cmd_set_fail_mode(struct ctl_context *ctx)
1919 {
1920 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
1921 struct vsctl_bridge *br;
1922 const char *fail_mode = ctx->argv[2];
1923
1924 vsctl_context_populate_cache(ctx);
1925
1926 br = find_real_bridge(vsctl_ctx, ctx->argv[1], true);
1927
1928 if (strcmp(fail_mode, "standalone") && strcmp(fail_mode, "secure")) {
1929 ctl_fatal("fail-mode must be \"standalone\" or \"secure\"");
1930 }
1931
1932 ovsrec_bridge_set_fail_mode(br->br_cfg, fail_mode);
1933 }
1934
1935 static void
1936 verify_managers(const struct ovsrec_open_vswitch *ovs)
1937 {
1938 size_t i;
1939
1940 ovsrec_open_vswitch_verify_manager_options(ovs);
1941
1942 for (i = 0; i < ovs->n_manager_options; ++i) {
1943 const struct ovsrec_manager *mgr = ovs->manager_options[i];
1944
1945 ovsrec_manager_verify_target(mgr);
1946 }
1947 }
1948
1949 static void
1950 pre_manager(struct ctl_context *ctx)
1951 {
1952 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_manager_options);
1953 ovsdb_idl_add_column(ctx->idl, &ovsrec_manager_col_target);
1954 }
1955
1956 static void
1957 cmd_get_manager(struct ctl_context *ctx)
1958 {
1959 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
1960 const struct ovsrec_open_vswitch *ovs = vsctl_ctx->ovs;
1961 struct svec targets;
1962 size_t i;
1963
1964 verify_managers(ovs);
1965
1966 /* Print the targets in sorted order for reproducibility. */
1967 svec_init(&targets);
1968
1969 for (i = 0; i < ovs->n_manager_options; i++) {
1970 svec_add(&targets, ovs->manager_options[i]->target);
1971 }
1972
1973 svec_sort_unique(&targets);
1974 for (i = 0; i < targets.n; i++) {
1975 ds_put_format(&ctx->output, "%s\n", targets.names[i]);
1976 }
1977 svec_destroy(&targets);
1978 }
1979
1980 static void
1981 delete_managers(const struct ovsrec_open_vswitch *ovs)
1982 {
1983 size_t i;
1984
1985 /* Delete Manager rows pointed to by 'manager_options' column. */
1986 for (i = 0; i < ovs->n_manager_options; i++) {
1987 ovsrec_manager_delete(ovs->manager_options[i]);
1988 }
1989
1990 /* Delete 'Manager' row refs in 'manager_options' column. */
1991 ovsrec_open_vswitch_set_manager_options(ovs, NULL, 0);
1992 }
1993
1994 static void
1995 cmd_del_manager(struct ctl_context *ctx)
1996 {
1997 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
1998 const struct ovsrec_open_vswitch *ovs = vsctl_ctx->ovs;
1999
2000 verify_managers(ovs);
2001 delete_managers(ovs);
2002 }
2003
2004 static void
2005 insert_managers(struct vsctl_context *vsctl_ctx, char *targets[], size_t n)
2006 {
2007 struct ovsrec_manager **managers;
2008 size_t i;
2009
2010 /* Insert each manager in a new row in Manager table. */
2011 managers = xmalloc(n * sizeof *managers);
2012 for (i = 0; i < n; i++) {
2013 if (stream_verify_name(targets[i]) && pstream_verify_name(targets[i])) {
2014 VLOG_WARN("target type \"%s\" is possibly erroneous", targets[i]);
2015 }
2016 managers[i] = ovsrec_manager_insert(vsctl_ctx->base.txn);
2017 ovsrec_manager_set_target(managers[i], targets[i]);
2018 }
2019
2020 /* Store uuids of new Manager rows in 'manager_options' column. */
2021 ovsrec_open_vswitch_set_manager_options(vsctl_ctx->ovs, managers, n);
2022 free(managers);
2023 }
2024
2025 static void
2026 cmd_set_manager(struct ctl_context *ctx)
2027 {
2028 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
2029 const size_t n = ctx->argc - 1;
2030
2031 verify_managers(vsctl_ctx->ovs);
2032 delete_managers(vsctl_ctx->ovs);
2033 insert_managers(vsctl_ctx, &ctx->argv[1], n);
2034 }
2035
2036 static void
2037 pre_cmd_get_ssl(struct ctl_context *ctx)
2038 {
2039 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_ssl);
2040
2041 ovsdb_idl_add_column(ctx->idl, &ovsrec_ssl_col_private_key);
2042 ovsdb_idl_add_column(ctx->idl, &ovsrec_ssl_col_certificate);
2043 ovsdb_idl_add_column(ctx->idl, &ovsrec_ssl_col_ca_cert);
2044 ovsdb_idl_add_column(ctx->idl, &ovsrec_ssl_col_bootstrap_ca_cert);
2045 }
2046
2047 static void
2048 cmd_get_ssl(struct ctl_context *ctx)
2049 {
2050 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
2051 struct ovsrec_ssl *ssl = vsctl_ctx->ovs->ssl;
2052
2053 ovsrec_open_vswitch_verify_ssl(vsctl_ctx->ovs);
2054 if (ssl) {
2055 ovsrec_ssl_verify_private_key(ssl);
2056 ovsrec_ssl_verify_certificate(ssl);
2057 ovsrec_ssl_verify_ca_cert(ssl);
2058 ovsrec_ssl_verify_bootstrap_ca_cert(ssl);
2059
2060 ds_put_format(&ctx->output, "Private key: %s\n", ssl->private_key);
2061 ds_put_format(&ctx->output, "Certificate: %s\n", ssl->certificate);
2062 ds_put_format(&ctx->output, "CA Certificate: %s\n", ssl->ca_cert);
2063 ds_put_format(&ctx->output, "Bootstrap: %s\n",
2064 ssl->bootstrap_ca_cert ? "true" : "false");
2065 }
2066 }
2067
2068 static void
2069 pre_cmd_del_ssl(struct ctl_context *ctx)
2070 {
2071 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_ssl);
2072 }
2073
2074 static void
2075 cmd_del_ssl(struct ctl_context *ctx)
2076 {
2077 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
2078 struct ovsrec_ssl *ssl = vsctl_ctx->ovs->ssl;
2079
2080 if (ssl) {
2081 ovsrec_open_vswitch_verify_ssl(vsctl_ctx->ovs);
2082 ovsrec_ssl_delete(ssl);
2083 ovsrec_open_vswitch_set_ssl(vsctl_ctx->ovs, NULL);
2084 }
2085 }
2086
2087 static void
2088 pre_cmd_set_ssl(struct ctl_context *ctx)
2089 {
2090 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_ssl);
2091 }
2092
2093 static void
2094 cmd_set_ssl(struct ctl_context *ctx)
2095 {
2096 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
2097 bool bootstrap = shash_find(&ctx->options, "--bootstrap");
2098 struct ovsrec_ssl *ssl = vsctl_ctx->ovs->ssl;
2099
2100 ovsrec_open_vswitch_verify_ssl(vsctl_ctx->ovs);
2101 if (ssl) {
2102 ovsrec_ssl_delete(ssl);
2103 }
2104 ssl = ovsrec_ssl_insert(ctx->txn);
2105
2106 ovsrec_ssl_set_private_key(ssl, ctx->argv[1]);
2107 ovsrec_ssl_set_certificate(ssl, ctx->argv[2]);
2108 ovsrec_ssl_set_ca_cert(ssl, ctx->argv[3]);
2109
2110 ovsrec_ssl_set_bootstrap_ca_cert(ssl, bootstrap);
2111
2112 ovsrec_open_vswitch_set_ssl(vsctl_ctx->ovs, ssl);
2113 }
2114
2115 static void
2116 autoattach_insert_mapping(struct ovsrec_autoattach *aa,
2117 int64_t isid,
2118 int64_t vlan)
2119 {
2120 int64_t *key_mappings, *value_mappings;
2121 size_t i;
2122
2123 key_mappings = xmalloc(sizeof *aa->key_mappings * (aa->n_mappings + 1));
2124 value_mappings = xmalloc(sizeof *aa->value_mappings * (aa->n_mappings + 1));
2125
2126 for (i = 0; i < aa->n_mappings; i++) {
2127 key_mappings[i] = aa->key_mappings[i];
2128 value_mappings[i] = aa->value_mappings[i];
2129 }
2130 key_mappings[aa->n_mappings] = isid;
2131 value_mappings[aa->n_mappings] = vlan;
2132
2133 ovsrec_autoattach_set_mappings(aa, key_mappings, value_mappings,
2134 aa->n_mappings + 1);
2135
2136 free(key_mappings);
2137 free(value_mappings);
2138 }
2139
2140 static void
2141 cmd_add_aa_mapping(struct ctl_context *ctx)
2142 {
2143 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
2144 struct vsctl_bridge *br;
2145 int64_t isid, vlan;
2146 char *nptr = NULL;
2147
2148 isid = strtoull(ctx->argv[2], &nptr, 10);
2149 if (nptr == ctx->argv[2] || nptr == NULL) {
2150 ctl_fatal("Invalid argument %s", ctx->argv[2]);
2151 return;
2152 }
2153
2154 vlan = strtoull(ctx->argv[3], &nptr, 10);
2155 if (nptr == ctx->argv[3] || nptr == NULL) {
2156 ctl_fatal("Invalid argument %s", ctx->argv[3]);
2157 return;
2158 }
2159
2160 vsctl_context_populate_cache(ctx);
2161
2162 br = find_bridge(vsctl_ctx, ctx->argv[1], true);
2163 if (br->parent) {
2164 br = br->parent;
2165 }
2166
2167 if (br->br_cfg) {
2168 if (!br->br_cfg->auto_attach) {
2169 struct ovsrec_autoattach *aa = ovsrec_autoattach_insert(ctx->txn);
2170 ovsrec_bridge_set_auto_attach(br->br_cfg, aa);
2171 }
2172 autoattach_insert_mapping(br->br_cfg->auto_attach, isid, vlan);
2173 }
2174 }
2175
2176 static void
2177 del_aa_mapping(struct ovsrec_autoattach *aa,
2178 int64_t isid,
2179 int64_t vlan)
2180 {
2181 int64_t *key_mappings, *value_mappings;
2182 size_t i, n;
2183
2184 key_mappings = xmalloc(sizeof *aa->key_mappings * (aa->n_mappings));
2185 value_mappings = xmalloc(sizeof *value_mappings * (aa->n_mappings));
2186
2187 for (i = n = 0; i < aa->n_mappings; i++) {
2188 if (aa->key_mappings[i] != isid && aa->value_mappings[i] != vlan) {
2189 key_mappings[n] = aa->key_mappings[i];
2190 value_mappings[n++] = aa->value_mappings[i];
2191 }
2192 }
2193
2194 ovsrec_autoattach_set_mappings(aa, key_mappings, value_mappings, n);
2195
2196 free(key_mappings);
2197 free(value_mappings);
2198 }
2199
2200 static void
2201 cmd_del_aa_mapping(struct ctl_context *ctx)
2202 {
2203 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
2204 struct vsctl_bridge *br;
2205 int64_t isid, vlan;
2206 char *nptr = NULL;
2207
2208 isid = strtoull(ctx->argv[2], &nptr, 10);
2209 if (nptr == ctx->argv[2] || nptr == NULL) {
2210 ctl_fatal("Invalid argument %s", ctx->argv[2]);
2211 return;
2212 }
2213
2214 vlan = strtoull(ctx->argv[3], &nptr, 10);
2215 if (nptr == ctx->argv[3] || nptr == NULL) {
2216 ctl_fatal("Invalid argument %s", ctx->argv[3]);
2217 return;
2218 }
2219
2220 vsctl_context_populate_cache(ctx);
2221
2222 br = find_bridge(vsctl_ctx, ctx->argv[1], true);
2223 if (br->parent) {
2224 br = br->parent;
2225 }
2226
2227 if (br->br_cfg && br->br_cfg->auto_attach &&
2228 br->br_cfg->auto_attach->key_mappings &&
2229 br->br_cfg->auto_attach->value_mappings) {
2230 size_t i;
2231
2232 for (i = 0; i < br->br_cfg->auto_attach->n_mappings; i++) {
2233 if (br->br_cfg->auto_attach->key_mappings[i] == isid &&
2234 br->br_cfg->auto_attach->value_mappings[i] == vlan) {
2235 del_aa_mapping(br->br_cfg->auto_attach, isid, vlan);
2236 break;
2237 }
2238 }
2239 }
2240 }
2241
2242 static void
2243 pre_aa_mapping(struct ctl_context *ctx)
2244 {
2245 pre_get_info(ctx);
2246
2247 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_auto_attach);
2248 ovsdb_idl_add_column(ctx->idl, &ovsrec_autoattach_col_mappings);
2249 }
2250
2251 static void
2252 verify_auto_attach(struct ovsrec_bridge *bridge)
2253 {
2254 if (bridge) {
2255 ovsrec_bridge_verify_auto_attach(bridge);
2256
2257 if (bridge->auto_attach) {
2258 ovsrec_autoattach_verify_mappings(bridge->auto_attach);
2259 }
2260 }
2261 }
2262
2263 static void
2264 cmd_get_aa_mapping(struct ctl_context *ctx)
2265 {
2266 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
2267 struct vsctl_bridge *br;
2268
2269 vsctl_context_populate_cache(ctx);
2270
2271 br = find_bridge(vsctl_ctx, ctx->argv[1], true);
2272 if (br->parent) {
2273 br = br->parent;
2274 }
2275
2276 verify_auto_attach(br->br_cfg);
2277
2278 if (br->br_cfg && br->br_cfg->auto_attach &&
2279 br->br_cfg->auto_attach->key_mappings &&
2280 br->br_cfg->auto_attach->value_mappings) {
2281 size_t i;
2282
2283 for (i = 0; i < br->br_cfg->auto_attach->n_mappings; i++) {
2284 ds_put_format(&ctx->output, "%"PRId64" %"PRId64"\n",
2285 br->br_cfg->auto_attach->key_mappings[i],
2286 br->br_cfg->auto_attach->value_mappings[i]);
2287 }
2288 }
2289 }
2290
2291 \f
2292 static const struct ctl_table_class tables[OVSREC_N_TABLES] = {
2293 [OVSREC_TABLE_BRIDGE].row_ids = {
2294 {&ovsrec_table_bridge, &ovsrec_bridge_col_name, NULL},
2295 {&ovsrec_table_flow_sample_collector_set, NULL,
2296 &ovsrec_flow_sample_collector_set_col_bridge}},
2297
2298 [OVSREC_TABLE_CONTROLLER].row_ids[0]
2299 = {&ovsrec_table_bridge, &ovsrec_bridge_col_name,
2300 &ovsrec_bridge_col_controller},
2301
2302 [OVSREC_TABLE_INTERFACE].row_ids[0]
2303 = {&ovsrec_table_interface, &ovsrec_interface_col_name, NULL},
2304
2305 [OVSREC_TABLE_MIRROR].row_ids[0]
2306 = {&ovsrec_table_mirror, &ovsrec_mirror_col_name, NULL},
2307
2308 [OVSREC_TABLE_MANAGER].row_ids[0]
2309 = {&ovsrec_table_manager, &ovsrec_manager_col_target, NULL},
2310
2311 [OVSREC_TABLE_NETFLOW].row_ids[0]
2312 = {&ovsrec_table_bridge, &ovsrec_bridge_col_name,
2313 &ovsrec_bridge_col_netflow},
2314
2315 [OVSREC_TABLE_PORT].row_ids[0]
2316 = {&ovsrec_table_port, &ovsrec_port_col_name, NULL},
2317
2318 [OVSREC_TABLE_QOS].row_ids[0]
2319 = {&ovsrec_table_port, &ovsrec_port_col_name, &ovsrec_port_col_qos},
2320
2321 [OVSREC_TABLE_SSL].row_ids[0]
2322 = {&ovsrec_table_open_vswitch, NULL, &ovsrec_open_vswitch_col_ssl},
2323
2324 [OVSREC_TABLE_SFLOW].row_ids[0]
2325 = {&ovsrec_table_bridge, &ovsrec_bridge_col_name,
2326 &ovsrec_bridge_col_sflow},
2327
2328 [OVSREC_TABLE_FLOW_TABLE].row_ids[0]
2329 = {&ovsrec_table_flow_table, &ovsrec_flow_table_col_name, NULL},
2330
2331 [OVSREC_TABLE_IPFIX].row_ids = {
2332 {&ovsrec_table_bridge, &ovsrec_bridge_col_name, &ovsrec_bridge_col_ipfix},
2333 {&ovsrec_table_flow_sample_collector_set, NULL,
2334 &ovsrec_flow_sample_collector_set_col_ipfix}},
2335
2336 [OVSREC_TABLE_AUTOATTACH].row_ids[0]
2337 = {&ovsrec_table_bridge, &ovsrec_bridge_col_name,
2338 &ovsrec_bridge_col_auto_attach},
2339
2340 [OVSREC_TABLE_FLOW_SAMPLE_COLLECTOR_SET].row_ids[0]
2341 = {&ovsrec_table_flow_sample_collector_set,
2342 &ovsrec_flow_sample_collector_set_col_id,
2343 NULL},
2344 };
2345
2346 static void
2347 post_db_reload_check_init(void)
2348 {
2349 n_neoteric_ifaces = 0;
2350 }
2351
2352 static void
2353 post_db_reload_expect_iface(const struct ovsrec_interface *iface)
2354 {
2355 if (n_neoteric_ifaces >= allocated_neoteric_ifaces) {
2356 neoteric_ifaces = x2nrealloc(neoteric_ifaces,
2357 &allocated_neoteric_ifaces,
2358 sizeof *neoteric_ifaces);
2359 }
2360 neoteric_ifaces[n_neoteric_ifaces++] = iface->header_.uuid;
2361 }
2362
2363 static void
2364 post_db_reload_do_checks(const struct vsctl_context *vsctl_ctx)
2365 {
2366 bool print_error = false;
2367 size_t i;
2368
2369 for (i = 0; i < n_neoteric_ifaces; i++) {
2370 const struct uuid *uuid;
2371
2372 uuid = ovsdb_idl_txn_get_insert_uuid(vsctl_ctx->base.txn,
2373 &neoteric_ifaces[i]);
2374 if (uuid) {
2375 const struct ovsrec_interface *iface;
2376
2377 iface = ovsrec_interface_get_for_uuid(vsctl_ctx->base.idl, uuid);
2378 if (iface && (!iface->ofport || *iface->ofport == -1)) {
2379 if (iface->error && *iface->error) {
2380 ovs_error(0, "Error detected while setting up '%s': %s. "
2381 "See ovs-vswitchd log for details.",
2382 iface->name, iface->error);
2383 } else {
2384 ovs_error(0, "Error detected while setting up '%s'. "
2385 "See ovs-vswitchd log for details.",
2386 iface->name);
2387 }
2388 print_error = true;
2389 }
2390 }
2391 }
2392
2393 if (print_error) {
2394 ovs_error(0, "The default log directory is \"%s\".", ovs_logdir());
2395 }
2396 }
2397
2398 \f
2399 static void
2400 vsctl_context_init_command(struct vsctl_context *vsctl_ctx,
2401 struct ctl_command *command)
2402 {
2403 ctl_context_init_command(&vsctl_ctx->base, command);
2404 vsctl_ctx->verified_ports = false;
2405 }
2406
2407 static void
2408 vsctl_context_init(struct vsctl_context *vsctl_ctx,
2409 struct ctl_command *command, struct ovsdb_idl *idl,
2410 struct ovsdb_idl_txn *txn,
2411 const struct ovsrec_open_vswitch *ovs,
2412 struct ovsdb_symbol_table *symtab)
2413 {
2414 ctl_context_init(&vsctl_ctx->base, command, idl, txn, symtab,
2415 vsctl_context_invalidate_cache);
2416 if (command) {
2417 vsctl_ctx->verified_ports = false;
2418 }
2419 vsctl_ctx->ovs = ovs;
2420 vsctl_ctx->cache_valid = false;
2421 }
2422
2423 static void
2424 vsctl_context_done_command(struct vsctl_context *vsctl_ctx,
2425 struct ctl_command *command)
2426 {
2427 ctl_context_done_command(&vsctl_ctx->base, command);
2428 }
2429
2430 static void
2431 vsctl_context_done(struct vsctl_context *vsctl_ctx,
2432 struct ctl_command *command)
2433 {
2434 ctl_context_done(&vsctl_ctx->base, command);
2435 }
2436
2437 static void
2438 run_prerequisites(struct ctl_command *commands, size_t n_commands,
2439 struct ovsdb_idl *idl)
2440 {
2441 struct ctl_command *c;
2442
2443 ovsdb_idl_add_table(idl, &ovsrec_table_open_vswitch);
2444 if (wait_for_reload) {
2445 ovsdb_idl_add_column(idl, &ovsrec_open_vswitch_col_cur_cfg);
2446 }
2447 for (c = commands; c < &commands[n_commands]; c++) {
2448 if (c->syntax->prerequisites) {
2449 struct vsctl_context vsctl_ctx;
2450
2451 ds_init(&c->output);
2452 c->table = NULL;
2453
2454 vsctl_context_init(&vsctl_ctx, c, idl, NULL, NULL, NULL);
2455 (c->syntax->prerequisites)(&vsctl_ctx.base);
2456 vsctl_context_done(&vsctl_ctx, c);
2457
2458 ovs_assert(!c->output.string);
2459 ovs_assert(!c->table);
2460 }
2461 }
2462 }
2463
2464 static char *
2465 vsctl_parent_process_info(void)
2466 {
2467 #ifdef __linux__
2468 pid_t parent_pid;
2469 struct ds s;
2470
2471 parent_pid = getppid();
2472 ds_init(&s);
2473
2474 /* Retrive the command line of the parent process, except the init
2475 * process since /proc/0 does not exist. */
2476 if (parent_pid) {
2477 char *procfile;
2478 FILE *f;
2479
2480 procfile = xasprintf("/proc/%d/cmdline", parent_pid);
2481
2482 f = fopen(procfile, "r");
2483 free(procfile);
2484 if (f) {
2485 ds_get_line(&s, f);
2486 fclose(f);
2487 }
2488 } else {
2489 ds_put_cstr(&s, "init");
2490 }
2491
2492 ds_put_format(&s, " (pid %d)", parent_pid);
2493 return ds_steal_cstr(&s);
2494 #else
2495 return NULL;
2496 #endif
2497 }
2498
2499 static void
2500 do_vsctl(const char *args, struct ctl_command *commands, size_t n_commands,
2501 struct ovsdb_idl *idl)
2502 {
2503 struct ovsdb_idl_txn *txn;
2504 const struct ovsrec_open_vswitch *ovs;
2505 enum ovsdb_idl_txn_status status;
2506 struct ovsdb_symbol_table *symtab;
2507 struct vsctl_context vsctl_ctx;
2508 struct ctl_command *c;
2509 struct shash_node *node;
2510 int64_t next_cfg = 0;
2511 char *error = NULL;
2512 char *ppid_info = NULL;
2513
2514 txn = the_idl_txn = ovsdb_idl_txn_create(idl);
2515 if (dry_run) {
2516 ovsdb_idl_txn_set_dry_run(txn);
2517 }
2518
2519 ppid_info = vsctl_parent_process_info();
2520 if (ppid_info) {
2521 ovsdb_idl_txn_add_comment(txn, "ovs-vsctl (invoked by %s): %s",
2522 ppid_info, args);
2523 free(ppid_info);
2524 } else {
2525 ovsdb_idl_txn_add_comment(txn, "ovs-vsctl: %s", args);
2526 }
2527
2528 ovs = ovsrec_open_vswitch_first(idl);
2529 if (!ovs) {
2530 /* XXX add verification that table is empty */
2531 ovs = ovsrec_open_vswitch_insert(txn);
2532 }
2533
2534 if (wait_for_reload) {
2535 ovsdb_idl_txn_increment(txn, &ovs->header_,
2536 &ovsrec_open_vswitch_col_next_cfg, false);
2537 }
2538
2539 post_db_reload_check_init();
2540 symtab = ovsdb_symbol_table_create();
2541 for (c = commands; c < &commands[n_commands]; c++) {
2542 ds_init(&c->output);
2543 c->table = NULL;
2544 }
2545 vsctl_context_init(&vsctl_ctx, NULL, idl, txn, ovs, symtab);
2546 for (c = commands; c < &commands[n_commands]; c++) {
2547 vsctl_context_init_command(&vsctl_ctx, c);
2548 if (c->syntax->run) {
2549 (c->syntax->run)(&vsctl_ctx.base);
2550 }
2551 vsctl_context_done_command(&vsctl_ctx, c);
2552
2553 if (vsctl_ctx.base.try_again) {
2554 vsctl_context_done(&vsctl_ctx, NULL);
2555 goto try_again;
2556 }
2557 }
2558 vsctl_context_done(&vsctl_ctx, NULL);
2559
2560 SHASH_FOR_EACH (node, &symtab->sh) {
2561 struct ovsdb_symbol *symbol = node->data;
2562 if (!symbol->created) {
2563 ctl_fatal("row id \"%s\" is referenced but never created (e.g. "
2564 "with \"-- --id=%s create ...\")",
2565 node->name, node->name);
2566 }
2567 if (!symbol->strong_ref) {
2568 if (!symbol->weak_ref) {
2569 VLOG_WARN("row id \"%s\" was created but no reference to it "
2570 "was inserted, so it will not actually appear in "
2571 "the database", node->name);
2572 } else {
2573 VLOG_WARN("row id \"%s\" was created but only a weak "
2574 "reference to it was inserted, so it will not "
2575 "actually appear in the database", node->name);
2576 }
2577 }
2578 }
2579
2580 status = ovsdb_idl_txn_commit_block(txn);
2581 if (wait_for_reload && status == TXN_SUCCESS) {
2582 next_cfg = ovsdb_idl_txn_get_increment_new_value(txn);
2583 }
2584 if (status == TXN_UNCHANGED || status == TXN_SUCCESS) {
2585 for (c = commands; c < &commands[n_commands]; c++) {
2586 if (c->syntax->postprocess) {
2587 vsctl_context_init(&vsctl_ctx, c, idl, txn, ovs, symtab);
2588 (c->syntax->postprocess)(&vsctl_ctx.base);
2589 vsctl_context_done(&vsctl_ctx, c);
2590 }
2591 }
2592 }
2593 error = xstrdup(ovsdb_idl_txn_get_error(txn));
2594
2595 switch (status) {
2596 case TXN_UNCOMMITTED:
2597 case TXN_INCOMPLETE:
2598 OVS_NOT_REACHED();
2599
2600 case TXN_ABORTED:
2601 /* Should not happen--we never call ovsdb_idl_txn_abort(). */
2602 ctl_fatal("transaction aborted");
2603
2604 case TXN_UNCHANGED:
2605 case TXN_SUCCESS:
2606 break;
2607
2608 case TXN_TRY_AGAIN:
2609 goto try_again;
2610
2611 case TXN_ERROR:
2612 ctl_fatal("transaction error: %s", error);
2613
2614 case TXN_NOT_LOCKED:
2615 /* Should not happen--we never call ovsdb_idl_set_lock(). */
2616 ctl_fatal("database not locked");
2617
2618 default:
2619 OVS_NOT_REACHED();
2620 }
2621 free(error);
2622
2623 ovsdb_symbol_table_destroy(symtab);
2624
2625 for (c = commands; c < &commands[n_commands]; c++) {
2626 struct ds *ds = &c->output;
2627
2628 if (c->table) {
2629 table_print(c->table, &table_style);
2630 } else if (oneline) {
2631 size_t j;
2632
2633 ds_chomp(ds, '\n');
2634 for (j = 0; j < ds->length; j++) {
2635 int ch = ds->string[j];
2636 switch (ch) {
2637 case '\n':
2638 fputs("\\n", stdout);
2639 break;
2640
2641 case '\\':
2642 fputs("\\\\", stdout);
2643 break;
2644
2645 default:
2646 putchar(ch);
2647 }
2648 }
2649 putchar('\n');
2650 } else {
2651 fputs(ds_cstr(ds), stdout);
2652 }
2653 ds_destroy(&c->output);
2654 table_destroy(c->table);
2655 free(c->table);
2656
2657 shash_destroy_free_data(&c->options);
2658 }
2659 free(commands);
2660
2661 if (wait_for_reload && status != TXN_UNCHANGED) {
2662 /* Even, if --retry flag was not specified, ovs-vsctl still
2663 * has to retry to establish OVSDB connection, if wait_for_reload
2664 * was set. Otherwise, ovs-vsctl would end up waiting forever
2665 * until cur_cfg would be updated. */
2666 ovsdb_idl_enable_reconnect(idl);
2667 for (;;) {
2668 ovsdb_idl_run(idl);
2669 OVSREC_OPEN_VSWITCH_FOR_EACH (ovs, idl) {
2670 if (ovs->cur_cfg >= next_cfg) {
2671 post_db_reload_do_checks(&vsctl_ctx);
2672 goto done;
2673 }
2674 }
2675 ovsdb_idl_wait(idl);
2676 poll_block();
2677 }
2678 done: ;
2679 }
2680 ovsdb_idl_txn_destroy(txn);
2681 ovsdb_idl_destroy(idl);
2682
2683 exit(EXIT_SUCCESS);
2684
2685 try_again:
2686 /* Our transaction needs to be rerun, or a prerequisite was not met. Free
2687 * resources and return so that the caller can try again. */
2688 if (txn) {
2689 ovsdb_idl_txn_abort(txn);
2690 ovsdb_idl_txn_destroy(txn);
2691 the_idl_txn = NULL;
2692 }
2693 ovsdb_symbol_table_destroy(symtab);
2694 for (c = commands; c < &commands[n_commands]; c++) {
2695 ds_destroy(&c->output);
2696 table_destroy(c->table);
2697 free(c->table);
2698 }
2699 free(error);
2700 }
2701
2702 /* Frees the current transaction and the underlying IDL and then calls
2703 * exit(status).
2704 *
2705 * Freeing the transaction and the IDL is not strictly necessary, but it makes
2706 * for a clean memory leak report from valgrind in the normal case. That makes
2707 * it easier to notice real memory leaks. */
2708 static void
2709 vsctl_exit(int status)
2710 {
2711 if (the_idl_txn) {
2712 ovsdb_idl_txn_abort(the_idl_txn);
2713 ovsdb_idl_txn_destroy(the_idl_txn);
2714 }
2715 ovsdb_idl_destroy(the_idl);
2716 exit(status);
2717 }
2718
2719 /*
2720 * Developers who add new commands to the 'struct ctl_command_syntax' must
2721 * define the 'arguments' member of the struct. The following keywords are
2722 * available for composing the argument format:
2723 *
2724 * TABLE RECORD BRIDGE PARENT PORT
2725 * KEY VALUE ARG KEY=VALUE ?KEY=VALUE
2726 * IFACE SYSIFACE COLUMN COLUMN?:KEY COLUMN?:KEY=VALUE
2727 * MODE CA-CERT CERTIFICATE PRIVATE-KEY
2728 * TARGET NEW-* (e.g. NEW-PORT)
2729 *
2730 * For argument types not listed above, just uses 'ARG' as place holder.
2731 *
2732 * Encloses the keyword with '[]' if it is optional. Appends '...' to
2733 * keyword or enclosed keyword to indicate that the argument can be specified
2734 * multiple times.
2735 *
2736 * */
2737 static const struct ctl_command_syntax vsctl_commands[] = {
2738 /* Open vSwitch commands. */
2739 {"init", 0, 0, "", NULL, cmd_init, NULL, "", RW},
2740
2741 /* Bridge commands. */
2742 {"add-br", 1, 3, "NEW-BRIDGE [PARENT] [NEW-VLAN]", pre_get_info,
2743 cmd_add_br, NULL, "--may-exist", RW},
2744 {"del-br", 1, 1, "BRIDGE", pre_get_info, cmd_del_br,
2745 NULL, "--if-exists", RW},
2746 {"list-br", 0, 0, "", pre_get_info, cmd_list_br, NULL, "--real,--fake",
2747 RO},
2748 {"br-exists", 1, 1, "BRIDGE", pre_get_info, cmd_br_exists, NULL, "", RO},
2749 {"br-to-vlan", 1, 1, "BRIDGE", pre_get_info, cmd_br_to_vlan, NULL, "",
2750 RO},
2751 {"br-to-parent", 1, 1, "BRIDGE", pre_get_info, cmd_br_to_parent, NULL,
2752 "", RO},
2753 {"br-set-external-id", 2, 3, "BRIDGE KEY [VALUE]",
2754 pre_cmd_br_set_external_id, cmd_br_set_external_id, NULL, "", RW},
2755 {"br-get-external-id", 1, 2, "BRIDGE [KEY]", pre_cmd_br_get_external_id,
2756 cmd_br_get_external_id, NULL, "", RO},
2757
2758 /* Port commands. */
2759 {"list-ports", 1, 1, "BRIDGE", pre_get_info, cmd_list_ports, NULL, "",
2760 RO},
2761 {"add-port", 2, INT_MAX, "BRIDGE NEW-PORT [COLUMN[:KEY]=VALUE]...",
2762 pre_get_info, cmd_add_port, NULL, "--may-exist", RW},
2763 {"add-bond", 4, INT_MAX,
2764 "BRIDGE NEW-BOND-PORT SYSIFACE... [COLUMN[:KEY]=VALUE]...", pre_get_info,
2765 cmd_add_bond, NULL, "--may-exist,--fake-iface", RW},
2766 {"del-port", 1, 2, "[BRIDGE] PORT|IFACE", pre_get_info, cmd_del_port, NULL,
2767 "--if-exists,--with-iface", RW},
2768 {"port-to-br", 1, 1, "PORT", pre_get_info, cmd_port_to_br, NULL, "", RO},
2769
2770 /* Interface commands. */
2771 {"list-ifaces", 1, 1, "BRIDGE", pre_get_info, cmd_list_ifaces, NULL, "",
2772 RO},
2773 {"iface-to-br", 1, 1, "IFACE", pre_get_info, cmd_iface_to_br, NULL, "",
2774 RO},
2775
2776 /* Controller commands. */
2777 {"get-controller", 1, 1, "BRIDGE", pre_controller, cmd_get_controller,
2778 NULL, "", RO},
2779 {"del-controller", 1, 1, "BRIDGE", pre_controller, cmd_del_controller,
2780 NULL, "", RW},
2781 {"set-controller", 1, INT_MAX, "BRIDGE TARGET...", pre_controller,
2782 cmd_set_controller, NULL, "", RW},
2783 {"get-fail-mode", 1, 1, "BRIDGE", pre_get_info, cmd_get_fail_mode, NULL,
2784 "", RO},
2785 {"del-fail-mode", 1, 1, "BRIDGE", pre_get_info, cmd_del_fail_mode, NULL,
2786 "", RW},
2787 {"set-fail-mode", 2, 2, "BRIDGE MODE", pre_get_info, cmd_set_fail_mode,
2788 NULL, "", RW},
2789
2790 /* Manager commands. */
2791 {"get-manager", 0, 0, "", pre_manager, cmd_get_manager, NULL, "", RO},
2792 {"del-manager", 0, 0, "", pre_manager, cmd_del_manager, NULL, "", RW},
2793 {"set-manager", 1, INT_MAX, "TARGET...", pre_manager, cmd_set_manager,
2794 NULL, "", RW},
2795
2796 /* SSL commands. */
2797 {"get-ssl", 0, 0, "", pre_cmd_get_ssl, cmd_get_ssl, NULL, "", RO},
2798 {"del-ssl", 0, 0, "", pre_cmd_del_ssl, cmd_del_ssl, NULL, "", RW},
2799 {"set-ssl", 3, 3, "PRIVATE-KEY CERTIFICATE CA-CERT", pre_cmd_set_ssl,
2800 cmd_set_ssl, NULL, "--bootstrap", RW},
2801
2802 /* Auto Attach commands. */
2803 {"add-aa-mapping", 3, 3, "BRIDGE ARG ARG", pre_aa_mapping, cmd_add_aa_mapping,
2804 NULL, "", RW},
2805 {"del-aa-mapping", 3, 3, "BRIDGE ARG ARG", pre_aa_mapping, cmd_del_aa_mapping,
2806 NULL, "", RW},
2807 {"get-aa-mapping", 1, 1, "BRIDGE", pre_aa_mapping, cmd_get_aa_mapping,
2808 NULL, "", RO},
2809
2810 /* Switch commands. */
2811 {"emer-reset", 0, 0, "", pre_cmd_emer_reset, cmd_emer_reset, NULL, "", RW},
2812
2813 {NULL, 0, 0, NULL, NULL, NULL, NULL, NULL, RO},
2814 };
2815
2816 /* Registers vsctl and common db commands. */
2817 static void
2818 vsctl_cmd_init(void)
2819 {
2820 ctl_init(ovsrec_table_classes, tables, cmd_show_tables, vsctl_exit);
2821 ctl_register_commands(vsctl_commands);
2822 }