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