2 * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015 Nicira, Inc.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
30 #include "db-ctl-base.h"
32 #include "command-line.h"
34 #include "dynamic-string.h"
35 #include "fatal-signal.h"
38 #include "ovsdb-data.h"
39 #include "ovsdb-idl.h"
40 #include "poll-loop.h"
43 #include "stream-ssl.h"
47 #include "lib/vswitch-idl.h"
51 #include "openvswitch/vconn.h"
52 #include "openvswitch/vlog.h"
54 VLOG_DEFINE_THIS_MODULE(vsctl);
58 /* --db: The database server to contact. */
59 static const char *db;
61 /* --oneline: Write each command's output as a single line? */
64 /* --dry-run: Do not commit any changes. */
67 /* --no-wait: Wait for ovs-vswitchd to reload its configuration? */
68 static bool wait_for_reload = true;
70 /* --timeout: Time to wait for a connection to 'db'. */
73 /* --retry: If true, ovs-vsctl will retry connecting to the database forever.
74 * If false and --db says to use an active connection method (e.g. "unix:",
75 * "tcp:", "ssl:"), then ovs-vsctl will try to connect once and exit with an
76 * error if the database server cannot be contacted (e.g. ovsdb-server is not
79 * Regardless of this setting, --timeout always limits how long ovs-vsctl will
83 /* Format for table output. */
84 static struct table_style table_style = TABLE_STYLE_DEFAULT;
86 static void vsctl_cmd_init(void);
87 OVS_NO_RETURN static void usage(void);
88 static void parse_options(int argc, char *argv[], struct shash *local_options);
89 static void run_prerequisites(struct ctl_command[], size_t n_commands,
91 static void do_vsctl(const char *args, struct ctl_command *, size_t n,
94 /* post_db_reload_check frame work is to allow ovs-vsctl to do additional
95 * checks after OVSDB transactions are successfully recorded and reload by
98 * For example, When a new interface is added to OVSDB, ovs-vswitchd will
99 * either store a positive values on successful implementing the new
100 * interface, or -1 on failure.
102 * Unless -no-wait command line option is specified,
103 * post_db_reload_do_checks() is called right after any configuration
104 * changes is picked up (i.e. reload) by ovs-vswitchd. Any error detected
105 * post OVSDB reload is reported as ovs-vsctl errors. OVS-vswitchd logs
106 * more detailed messages about those errors.
108 * Current implementation only check for Post OVSDB reload failures on new
109 * interface additions with 'add-br' and 'add-port' commands.
111 * post_db_reload_expect_iface()
113 * keep track of interfaces to be checked post OVSDB reload. */
114 static void post_db_reload_check_init(void);
115 static void post_db_reload_do_checks(const struct vsctl_context *);
116 static void post_db_reload_expect_iface(const struct ovsrec_interface *);
118 static struct uuid *neoteric_ifaces;
119 static size_t n_neoteric_ifaces;
120 static size_t allocated_neoteric_ifaces;
123 main(int argc, char *argv[])
125 extern struct vlog_module VLM_reconnect;
126 struct ovsdb_idl *idl;
127 struct ctl_command *commands;
128 struct shash local_options;
133 set_program_name(argv[0]);
134 fatal_ignore_sigpipe();
135 vlog_set_levels(NULL, VLF_CONSOLE, VLL_WARN);
136 vlog_set_levels(&VLM_reconnect, VLF_ANY_DESTINATION, VLL_WARN);
141 /* Log our arguments. This is often valuable for debugging systems. */
142 args = process_escape_args(argv);
143 VLOG(ctl_might_write_to_db(argv) ? VLL_INFO : VLL_DBG, "Called as %s", args);
145 /* Parse command line. */
146 shash_init(&local_options);
147 parse_options(argc, argv, &local_options);
148 commands = ctl_parse_commands(argc - optind, argv + optind, &local_options,
155 /* Initialize IDL. */
156 idl = the_idl = ovsdb_idl_create(db, &ovsrec_idl_class, false, retry);
157 run_prerequisites(commands, n_commands, idl);
159 /* Execute the commands.
161 * 'seqno' is the database sequence number for which we last tried to
162 * execute our transaction. There's no point in trying to commit more than
163 * once for any given sequence number, because if the transaction fails
164 * it's because the database changed and we need to obtain an up-to-date
165 * view of the database before we try the transaction again. */
166 seqno = ovsdb_idl_get_seqno(idl);
169 if (!ovsdb_idl_is_alive(idl)) {
170 int retval = ovsdb_idl_get_last_error(idl);
171 ctl_fatal("%s: database connection failed (%s)",
172 db, ovs_retval_to_string(retval));
175 if (seqno != ovsdb_idl_get_seqno(idl)) {
176 seqno = ovsdb_idl_get_seqno(idl);
177 do_vsctl(args, commands, n_commands, idl);
180 if (seqno == ovsdb_idl_get_seqno(idl)) {
188 parse_options(int argc, char *argv[], struct shash *local_options)
191 OPT_DB = UCHAR_MAX + 1,
204 static const struct option global_long_options[] = {
205 {"db", required_argument, NULL, OPT_DB},
206 {"no-syslog", no_argument, NULL, OPT_NO_SYSLOG},
207 {"no-wait", no_argument, NULL, OPT_NO_WAIT},
208 {"dry-run", no_argument, NULL, OPT_DRY_RUN},
209 {"oneline", no_argument, NULL, OPT_ONELINE},
210 {"timeout", required_argument, NULL, 't'},
211 {"retry", no_argument, NULL, OPT_RETRY},
212 {"help", no_argument, NULL, 'h'},
213 {"commands", no_argument, NULL, OPT_COMMANDS},
214 {"options", no_argument, NULL, OPT_OPTIONS},
215 {"version", no_argument, NULL, 'V'},
218 STREAM_SSL_LONG_OPTIONS,
219 {"peer-ca-cert", required_argument, NULL, OPT_PEER_CA_CERT},
222 const int n_global_long_options = ARRAY_SIZE(global_long_options) - 1;
223 char *tmp, *short_options;
225 struct option *options;
226 size_t allocated_options;
230 tmp = ovs_cmdl_long_options_to_short_options(global_long_options);
231 short_options = xasprintf("+%s", tmp);
234 /* We want to parse both global and command-specific options here, but
235 * getopt_long() isn't too convenient for the job. We copy our global
236 * options into a dynamic array, then append all of the command-specific
238 options = xmemdup(global_long_options, sizeof global_long_options);
239 allocated_options = ARRAY_SIZE(global_long_options);
240 n_options = n_global_long_options;
241 ctl_add_cmd_options(&options, &n_options, &allocated_options, OPT_LOCAL);
242 table_style.format = TF_LIST;
248 c = getopt_long(argc, argv, short_options, options, &idx);
263 vlog_set_levels(&VLM_vsctl, VLF_SYSLOG, VLL_WARN);
267 wait_for_reload = false;
275 if (shash_find(local_options, options[idx].name)) {
276 ctl_fatal("'%s' option specified multiple times",
279 shash_add_nocopy(local_options,
280 xasprintf("--%s", options[idx].name),
281 optarg ? xstrdup(optarg) : NULL);
288 ctl_print_commands();
291 ctl_print_options(global_long_options);
294 ovs_print_version(0, 0);
295 printf("DB Schema %s\n", ovsrec_get_db_version());
299 timeout = strtoul(optarg, NULL, 10);
301 ctl_fatal("value %s on -t or --timeout is invalid",
311 TABLE_OPTION_HANDLERS(&table_style)
313 STREAM_SSL_OPTION_HANDLERS
315 case OPT_PEER_CA_CERT:
316 stream_ssl_set_peer_ca_cert_file(optarg);
329 db = ctl_default_db();
332 for (i = n_global_long_options; options[i].name; i++) {
333 free(CONST_CAST(char *, options[i].name));
342 %s: ovs-vswitchd management utility\n\
343 usage: %s [OPTIONS] COMMAND [ARG...]\n\
345 Open vSwitch commands:\n\
346 init initialize database, if not yet initialized\n\
347 show print overview of database contents\n\
348 emer-reset reset configuration to clean state\n\
351 add-br BRIDGE create a new bridge named BRIDGE\n\
352 add-br BRIDGE PARENT VLAN create new fake BRIDGE in PARENT on VLAN\n\
353 del-br BRIDGE delete BRIDGE and all of its ports\n\
354 list-br print the names of all the bridges\n\
355 br-exists BRIDGE exit 2 if BRIDGE does not exist\n\
356 br-to-vlan BRIDGE print the VLAN which BRIDGE is on\n\
357 br-to-parent BRIDGE print the parent of BRIDGE\n\
358 br-set-external-id BRIDGE KEY VALUE set KEY on BRIDGE to VALUE\n\
359 br-set-external-id BRIDGE KEY unset KEY on BRIDGE\n\
360 br-get-external-id BRIDGE KEY print value of KEY on BRIDGE\n\
361 br-get-external-id BRIDGE list key-value pairs on BRIDGE\n\
363 Port commands (a bond is considered to be a single port):\n\
364 list-ports BRIDGE print the names of all the ports on BRIDGE\n\
365 add-port BRIDGE PORT add network device PORT to BRIDGE\n\
366 add-bond BRIDGE PORT IFACE... add bonded port PORT in BRIDGE from IFACES\n\
367 del-port [BRIDGE] PORT delete PORT (which may be bonded) from BRIDGE\n\
368 port-to-br PORT print name of bridge that contains PORT\n\
370 Interface commands (a bond consists of multiple interfaces):\n\
371 list-ifaces BRIDGE print the names of all interfaces on BRIDGE\n\
372 iface-to-br IFACE print name of bridge that contains IFACE\n\
374 Controller commands:\n\
375 get-controller BRIDGE print the controllers for BRIDGE\n\
376 del-controller BRIDGE delete the controllers for BRIDGE\n\
377 set-controller BRIDGE TARGET... set the controllers for BRIDGE\n\
378 get-fail-mode BRIDGE print the fail-mode for BRIDGE\n\
379 del-fail-mode BRIDGE delete the fail-mode for BRIDGE\n\
380 set-fail-mode BRIDGE MODE set the fail-mode for BRIDGE to MODE\n\
383 get-manager print the managers\n\
384 del-manager delete the managers\n\
385 set-manager TARGET... set the list of managers to TARGET...\n\
388 get-ssl print the SSL configuration\n\
389 del-ssl delete the SSL configuration\n\
390 set-ssl PRIV-KEY CERT CA-CERT set the SSL configuration\n\
392 Auto Attach commands:\n\
393 add-aa-mapping BRIDGE I-SID VLAN add Auto Attach mapping to BRIDGE\n\
394 del-aa-mapping BRIDGE I-SID VLAN delete Auto Attach mapping VLAN from BRIDGE\n\
395 get-aa-mapping BRIDGE get Auto Attach mappings from BRIDGE\n\
398 emer-reset reset switch to known good state\n\
403 --db=DATABASE connect to DATABASE\n\
405 --no-wait do not wait for ovs-vswitchd to reconfigure\n\
406 --retry keep trying to connect to server forever\n\
407 -t, --timeout=SECS wait at most SECS seconds for ovs-vswitchd\n\
408 --dry-run do not commit changes to database\n\
409 --oneline print exactly one line of output per command\n",
410 program_name, program_name, ctl_get_db_cmd_usage(), ctl_default_db());
413 --no-syslog equivalent to --verbose=vsctl:syslog:warn\n");
414 stream_usage("database", true, true, false);
417 -h, --help display this help message\n\
418 -V, --version display version information\n");
423 /* ovs-vsctl specific context. Inherits the 'struct ctl_context' as base. */
424 struct vsctl_context {
425 struct ctl_context base;
427 /* Modifiable state. */
428 const struct ovsrec_open_vswitch *ovs;
431 /* A cache of the contents of the database.
433 * A command that needs to use any of this information must first call
434 * vsctl_context_populate_cache(). A command that changes anything that
435 * could invalidate the cache must either call
436 * vsctl_context_invalidate_cache() or manually update the cache to
437 * maintain its correctness. */
439 struct shash bridges; /* Maps from bridge name to struct vsctl_bridge. */
440 struct shash ports; /* Maps from port name to struct vsctl_port. */
441 struct shash ifaces; /* Maps from port name to struct vsctl_iface. */
444 struct vsctl_bridge {
445 struct ovsrec_bridge *br_cfg;
447 struct ovs_list ports; /* Contains "struct vsctl_port"s. */
449 /* VLAN ("fake") bridge support.
451 * Use 'parent != NULL' to detect a fake bridge, because 'vlan' can be 0
453 struct hmap children; /* VLAN bridges indexed by 'vlan'. */
454 struct hmap_node children_node; /* Node in parent's 'children' hmap. */
455 struct vsctl_bridge *parent; /* Real bridge, or NULL. */
456 int vlan; /* VLAN VID (0...4095), or 0. */
460 struct ovs_list ports_node; /* In struct vsctl_bridge's 'ports' list. */
461 struct ovs_list ifaces; /* Contains "struct vsctl_iface"s. */
462 struct ovsrec_port *port_cfg;
463 struct vsctl_bridge *bridge;
467 struct ovs_list ifaces_node; /* In struct vsctl_port's 'ifaces' list. */
468 struct ovsrec_interface *iface_cfg;
469 struct vsctl_port *port;
472 /* Casts 'base' into 'strcut vsctl_context'. */
473 static struct vsctl_context *
474 vsctl_context_cast(struct ctl_context *base)
476 return CONTAINER_OF(base, struct vsctl_context, base);
479 static struct vsctl_bridge *find_vlan_bridge(struct vsctl_bridge *parent,
483 vsctl_context_to_string(const struct ctl_context *ctx)
485 const struct shash_node *node;
491 SHASH_FOR_EACH (node, &ctx->options) {
492 svec_add(&words, node->name);
494 for (i = 0; i < ctx->argc; i++) {
495 svec_add(&words, ctx->argv[i]);
497 svec_terminate(&words);
499 s = process_escape_args(words.names);
501 svec_destroy(&words);
507 verify_ports(struct vsctl_context *vsctl_ctx)
509 if (!vsctl_ctx->verified_ports) {
510 const struct ovsrec_bridge *bridge;
511 const struct ovsrec_port *port;
513 ovsrec_open_vswitch_verify_bridges(vsctl_ctx->ovs);
514 OVSREC_BRIDGE_FOR_EACH (bridge, vsctl_ctx->base.idl) {
515 ovsrec_bridge_verify_ports(bridge);
517 OVSREC_PORT_FOR_EACH (port, vsctl_ctx->base.idl) {
518 ovsrec_port_verify_interfaces(port);
521 vsctl_ctx->verified_ports = true;
525 static struct vsctl_bridge *
526 add_bridge_to_cache(struct vsctl_context *vsctl_ctx,
527 struct ovsrec_bridge *br_cfg, const char *name,
528 struct vsctl_bridge *parent, int vlan)
530 struct vsctl_bridge *br = xmalloc(sizeof *br);
532 br->name = xstrdup(name);
533 list_init(&br->ports);
536 hmap_init(&br->children);
538 struct vsctl_bridge *conflict = find_vlan_bridge(parent, vlan);
540 VLOG_WARN("%s: bridge has multiple VLAN bridges (%s and %s) "
541 "for VLAN %d, but only one is allowed",
542 parent->name, name, conflict->name, vlan);
544 hmap_insert(&parent->children, &br->children_node,
548 shash_add(&vsctl_ctx->bridges, br->name, br);
553 ovs_delete_bridge(const struct ovsrec_open_vswitch *ovs,
554 struct ovsrec_bridge *bridge)
556 struct ovsrec_bridge **bridges;
559 bridges = xmalloc(sizeof *ovs->bridges * ovs->n_bridges);
560 for (i = n = 0; i < ovs->n_bridges; i++) {
561 if (ovs->bridges[i] != bridge) {
562 bridges[n++] = ovs->bridges[i];
565 ovsrec_open_vswitch_set_bridges(ovs, bridges, n);
570 del_cached_bridge(struct vsctl_context *vsctl_ctx, struct vsctl_bridge *br)
572 ovs_assert(list_is_empty(&br->ports));
573 ovs_assert(hmap_is_empty(&br->children));
575 hmap_remove(&br->parent->children, &br->children_node);
578 ovsrec_bridge_delete(br->br_cfg);
579 ovs_delete_bridge(vsctl_ctx->ovs, br->br_cfg);
581 shash_find_and_delete(&vsctl_ctx->bridges, br->name);
582 hmap_destroy(&br->children);
588 port_is_fake_bridge(const struct ovsrec_port *port_cfg)
590 return (port_cfg->fake_bridge
592 && *port_cfg->tag >= 0 && *port_cfg->tag <= 4095);
595 static struct vsctl_bridge *
596 find_vlan_bridge(struct vsctl_bridge *parent, int vlan)
598 struct vsctl_bridge *child;
600 HMAP_FOR_EACH_IN_BUCKET (child, children_node, hash_int(vlan, 0),
602 if (child->vlan == vlan) {
610 static struct vsctl_port *
611 add_port_to_cache(struct vsctl_context *vsctl_ctx, struct vsctl_bridge *parent,
612 struct ovsrec_port *port_cfg)
614 struct vsctl_port *port;
617 && *port_cfg->tag >= 0 && *port_cfg->tag <= 4095) {
618 struct vsctl_bridge *vlan_bridge;
620 vlan_bridge = find_vlan_bridge(parent, *port_cfg->tag);
622 parent = vlan_bridge;
626 port = xmalloc(sizeof *port);
627 list_push_back(&parent->ports, &port->ports_node);
628 list_init(&port->ifaces);
629 port->port_cfg = port_cfg;
630 port->bridge = parent;
631 shash_add(&vsctl_ctx->ports, port_cfg->name, port);
637 del_cached_port(struct vsctl_context *vsctl_ctx, struct vsctl_port *port)
639 ovs_assert(list_is_empty(&port->ifaces));
640 list_remove(&port->ports_node);
641 shash_find_and_delete(&vsctl_ctx->ports, port->port_cfg->name);
642 ovsrec_port_delete(port->port_cfg);
646 static struct vsctl_iface *
647 add_iface_to_cache(struct vsctl_context *vsctl_ctx, struct vsctl_port *parent,
648 struct ovsrec_interface *iface_cfg)
650 struct vsctl_iface *iface;
652 iface = xmalloc(sizeof *iface);
653 list_push_back(&parent->ifaces, &iface->ifaces_node);
654 iface->iface_cfg = iface_cfg;
655 iface->port = parent;
656 shash_add(&vsctl_ctx->ifaces, iface_cfg->name, iface);
662 del_cached_iface(struct vsctl_context *vsctl_ctx, struct vsctl_iface *iface)
664 list_remove(&iface->ifaces_node);
665 shash_find_and_delete(&vsctl_ctx->ifaces, iface->iface_cfg->name);
666 ovsrec_interface_delete(iface->iface_cfg);
671 vsctl_context_invalidate_cache(struct ctl_context *ctx)
673 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
674 struct shash_node *node;
676 if (!vsctl_ctx->cache_valid) {
679 vsctl_ctx->cache_valid = false;
681 SHASH_FOR_EACH (node, &vsctl_ctx->bridges) {
682 struct vsctl_bridge *bridge = node->data;
683 hmap_destroy(&bridge->children);
687 shash_destroy(&vsctl_ctx->bridges);
689 shash_destroy_free_data(&vsctl_ctx->ports);
690 shash_destroy_free_data(&vsctl_ctx->ifaces);
694 pre_get_info(struct ctl_context *ctx)
696 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_bridges);
698 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_name);
699 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_controller);
700 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_fail_mode);
701 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_ports);
703 ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_name);
704 ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_fake_bridge);
705 ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_tag);
706 ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_interfaces);
708 ovsdb_idl_add_column(ctx->idl, &ovsrec_interface_col_name);
710 ovsdb_idl_add_column(ctx->idl, &ovsrec_interface_col_ofport);
714 vsctl_context_populate_cache(struct ctl_context *ctx)
716 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
717 const struct ovsrec_open_vswitch *ovs = vsctl_ctx->ovs;
718 struct sset bridges, ports;
721 if (vsctl_ctx->cache_valid) {
722 /* Cache is already populated. */
725 vsctl_ctx->cache_valid = true;
726 shash_init(&vsctl_ctx->bridges);
727 shash_init(&vsctl_ctx->ports);
728 shash_init(&vsctl_ctx->ifaces);
732 for (i = 0; i < ovs->n_bridges; i++) {
733 struct ovsrec_bridge *br_cfg = ovs->bridges[i];
734 struct vsctl_bridge *br;
737 if (!sset_add(&bridges, br_cfg->name)) {
738 VLOG_WARN("%s: database contains duplicate bridge name",
742 br = add_bridge_to_cache(vsctl_ctx, br_cfg, br_cfg->name, NULL, 0);
747 for (j = 0; j < br_cfg->n_ports; j++) {
748 struct ovsrec_port *port_cfg = br_cfg->ports[j];
750 if (!sset_add(&ports, port_cfg->name)) {
751 /* Duplicate port name. (We will warn about that later.) */
755 if (port_is_fake_bridge(port_cfg)
756 && sset_add(&bridges, port_cfg->name)) {
757 add_bridge_to_cache(vsctl_ctx, NULL, port_cfg->name, br,
762 sset_destroy(&bridges);
763 sset_destroy(&ports);
766 for (i = 0; i < ovs->n_bridges; i++) {
767 struct ovsrec_bridge *br_cfg = ovs->bridges[i];
768 struct vsctl_bridge *br;
771 if (!sset_add(&bridges, br_cfg->name)) {
774 br = shash_find_data(&vsctl_ctx->bridges, br_cfg->name);
775 for (j = 0; j < br_cfg->n_ports; j++) {
776 struct ovsrec_port *port_cfg = br_cfg->ports[j];
777 struct vsctl_port *port;
780 port = shash_find_data(&vsctl_ctx->ports, port_cfg->name);
782 if (port_cfg == port->port_cfg) {
783 VLOG_WARN("%s: port is in multiple bridges (%s and %s)",
784 port_cfg->name, br->name, port->bridge->name);
786 /* Log as an error because this violates the database's
787 * uniqueness constraints, so the database server shouldn't
788 * have allowed it. */
789 VLOG_ERR("%s: database contains duplicate port name",
795 if (port_is_fake_bridge(port_cfg)
796 && !sset_add(&bridges, port_cfg->name)) {
800 port = add_port_to_cache(vsctl_ctx, br, port_cfg);
801 for (k = 0; k < port_cfg->n_interfaces; k++) {
802 struct ovsrec_interface *iface_cfg = port_cfg->interfaces[k];
803 struct vsctl_iface *iface;
805 iface = shash_find_data(&vsctl_ctx->ifaces, iface_cfg->name);
807 if (iface_cfg == iface->iface_cfg) {
808 VLOG_WARN("%s: interface is in multiple ports "
811 iface->port->port_cfg->name,
812 port->port_cfg->name);
814 /* Log as an error because this violates the database's
815 * uniqueness constraints, so the database server
816 * shouldn't have allowed it. */
817 VLOG_ERR("%s: database contains duplicate interface "
818 "name", iface_cfg->name);
823 add_iface_to_cache(vsctl_ctx, port, iface_cfg);
827 sset_destroy(&bridges);
831 check_conflicts(struct vsctl_context *vsctl_ctx, const char *name,
834 struct vsctl_iface *iface;
835 struct vsctl_port *port;
837 verify_ports(vsctl_ctx);
839 if (shash_find(&vsctl_ctx->bridges, name)) {
840 ctl_fatal("%s because a bridge named %s already exists",
844 port = shash_find_data(&vsctl_ctx->ports, name);
846 ctl_fatal("%s because a port named %s already exists on "
847 "bridge %s", msg, name, port->bridge->name);
850 iface = shash_find_data(&vsctl_ctx->ifaces, name);
852 ctl_fatal("%s because an interface named %s already exists "
853 "on bridge %s", msg, name, iface->port->bridge->name);
859 static struct vsctl_bridge *
860 find_bridge(struct vsctl_context *vsctl_ctx, const char *name, bool must_exist)
862 struct vsctl_bridge *br;
864 ovs_assert(vsctl_ctx->cache_valid);
866 br = shash_find_data(&vsctl_ctx->bridges, name);
867 if (must_exist && !br) {
868 ctl_fatal("no bridge named %s", name);
870 ovsrec_open_vswitch_verify_bridges(vsctl_ctx->ovs);
874 static struct vsctl_bridge *
875 find_real_bridge(struct vsctl_context *vsctl_ctx,
876 const char *name, bool must_exist)
878 struct vsctl_bridge *br = find_bridge(vsctl_ctx, name, must_exist);
879 if (br && br->parent) {
880 ctl_fatal("%s is a fake bridge", name);
885 static struct vsctl_port *
886 find_port(struct vsctl_context *vsctl_ctx, const char *name, bool must_exist)
888 struct vsctl_port *port;
890 ovs_assert(vsctl_ctx->cache_valid);
892 port = shash_find_data(&vsctl_ctx->ports, name);
893 if (port && !strcmp(name, port->bridge->name)) {
896 if (must_exist && !port) {
897 ctl_fatal("no port named %s", name);
899 verify_ports(vsctl_ctx);
903 static struct vsctl_iface *
904 find_iface(struct vsctl_context *vsctl_ctx, const char *name, bool must_exist)
906 struct vsctl_iface *iface;
908 ovs_assert(vsctl_ctx->cache_valid);
910 iface = shash_find_data(&vsctl_ctx->ifaces, name);
911 if (iface && !strcmp(name, iface->port->bridge->name)) {
914 if (must_exist && !iface) {
915 ctl_fatal("no interface named %s", name);
917 verify_ports(vsctl_ctx);
922 bridge_insert_port(struct ovsrec_bridge *br, struct ovsrec_port *port)
924 struct ovsrec_port **ports;
927 ports = xmalloc(sizeof *br->ports * (br->n_ports + 1));
928 for (i = 0; i < br->n_ports; i++) {
929 ports[i] = br->ports[i];
931 ports[br->n_ports] = port;
932 ovsrec_bridge_set_ports(br, ports, br->n_ports + 1);
937 bridge_delete_port(struct ovsrec_bridge *br, struct ovsrec_port *port)
939 struct ovsrec_port **ports;
942 ports = xmalloc(sizeof *br->ports * br->n_ports);
943 for (i = n = 0; i < br->n_ports; i++) {
944 if (br->ports[i] != port) {
945 ports[n++] = br->ports[i];
948 ovsrec_bridge_set_ports(br, ports, n);
953 ovs_insert_bridge(const struct ovsrec_open_vswitch *ovs,
954 struct ovsrec_bridge *bridge)
956 struct ovsrec_bridge **bridges;
959 bridges = xmalloc(sizeof *ovs->bridges * (ovs->n_bridges + 1));
960 for (i = 0; i < ovs->n_bridges; i++) {
961 bridges[i] = ovs->bridges[i];
963 bridges[ovs->n_bridges] = bridge;
964 ovsrec_open_vswitch_set_bridges(ovs, bridges, ovs->n_bridges + 1);
969 cmd_init(struct ctl_context *ctx OVS_UNUSED)
973 struct cmd_show_table cmd_show_tables[] = {
974 {&ovsrec_table_open_vswitch,
976 {&ovsrec_open_vswitch_col_manager_options,
977 &ovsrec_open_vswitch_col_bridges,
978 &ovsrec_open_vswitch_col_ovs_version},
981 {&ovsrec_table_bridge,
982 &ovsrec_bridge_col_name,
983 {&ovsrec_bridge_col_controller,
984 &ovsrec_bridge_col_fail_mode,
985 &ovsrec_bridge_col_ports},
989 &ovsrec_port_col_name,
990 {&ovsrec_port_col_tag,
991 &ovsrec_port_col_trunks,
992 &ovsrec_port_col_interfaces},
995 {&ovsrec_table_interface,
996 &ovsrec_interface_col_name,
997 {&ovsrec_interface_col_type,
998 &ovsrec_interface_col_options,
999 &ovsrec_interface_col_error},
1002 {&ovsrec_table_controller,
1003 &ovsrec_controller_col_target,
1004 {&ovsrec_controller_col_is_connected,
1009 {&ovsrec_table_manager,
1010 &ovsrec_manager_col_target,
1011 {&ovsrec_manager_col_is_connected,
1016 {NULL, NULL, {NULL, NULL, NULL}, false}
1020 pre_cmd_emer_reset(struct ctl_context *ctx)
1022 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_manager_options);
1023 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_ssl);
1025 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_controller);
1026 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_fail_mode);
1027 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_mirrors);
1028 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_netflow);
1029 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_sflow);
1030 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_ipfix);
1031 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_flood_vlans);
1032 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_other_config);
1034 ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_other_config);
1036 ovsdb_idl_add_column(ctx->idl,
1037 &ovsrec_interface_col_ingress_policing_rate);
1038 ovsdb_idl_add_column(ctx->idl,
1039 &ovsrec_interface_col_ingress_policing_burst);
1043 cmd_emer_reset(struct ctl_context *ctx)
1045 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
1046 const struct ovsdb_idl *idl = ctx->idl;
1047 const struct ovsrec_bridge *br;
1048 const struct ovsrec_port *port;
1049 const struct ovsrec_interface *iface;
1050 const struct ovsrec_mirror *mirror, *next_mirror;
1051 const struct ovsrec_controller *ctrl, *next_ctrl;
1052 const struct ovsrec_manager *mgr, *next_mgr;
1053 const struct ovsrec_netflow *nf, *next_nf;
1054 const struct ovsrec_ssl *ssl, *next_ssl;
1055 const struct ovsrec_sflow *sflow, *next_sflow;
1056 const struct ovsrec_ipfix *ipfix, *next_ipfix;
1057 const struct ovsrec_flow_sample_collector_set *fscset, *next_fscset;
1059 /* Reset the Open_vSwitch table. */
1060 ovsrec_open_vswitch_set_manager_options(vsctl_ctx->ovs, NULL, 0);
1061 ovsrec_open_vswitch_set_ssl(vsctl_ctx->ovs, NULL);
1063 OVSREC_BRIDGE_FOR_EACH (br, idl) {
1066 ovsrec_bridge_set_controller(br, NULL, 0);
1067 ovsrec_bridge_set_fail_mode(br, NULL);
1068 ovsrec_bridge_set_mirrors(br, NULL, 0);
1069 ovsrec_bridge_set_netflow(br, NULL);
1070 ovsrec_bridge_set_sflow(br, NULL);
1071 ovsrec_bridge_set_ipfix(br, NULL);
1072 ovsrec_bridge_set_flood_vlans(br, NULL, 0);
1074 /* We only want to save the "hwaddr" key from other_config. */
1075 hwaddr = smap_get(&br->other_config, "hwaddr");
1077 struct smap smap = SMAP_INITIALIZER(&smap);
1078 smap_add(&smap, "hwaddr", hwaddr);
1079 ovsrec_bridge_set_other_config(br, &smap);
1080 smap_destroy(&smap);
1082 ovsrec_bridge_set_other_config(br, NULL);
1086 OVSREC_PORT_FOR_EACH (port, idl) {
1087 ovsrec_port_set_other_config(port, NULL);
1090 OVSREC_INTERFACE_FOR_EACH (iface, idl) {
1091 /* xxx What do we do about gre/patch devices created by mgr? */
1093 ovsrec_interface_set_ingress_policing_rate(iface, 0);
1094 ovsrec_interface_set_ingress_policing_burst(iface, 0);
1097 OVSREC_MIRROR_FOR_EACH_SAFE (mirror, next_mirror, idl) {
1098 ovsrec_mirror_delete(mirror);
1101 OVSREC_CONTROLLER_FOR_EACH_SAFE (ctrl, next_ctrl, idl) {
1102 ovsrec_controller_delete(ctrl);
1105 OVSREC_MANAGER_FOR_EACH_SAFE (mgr, next_mgr, idl) {
1106 ovsrec_manager_delete(mgr);
1109 OVSREC_NETFLOW_FOR_EACH_SAFE (nf, next_nf, idl) {
1110 ovsrec_netflow_delete(nf);
1113 OVSREC_SSL_FOR_EACH_SAFE (ssl, next_ssl, idl) {
1114 ovsrec_ssl_delete(ssl);
1117 OVSREC_SFLOW_FOR_EACH_SAFE (sflow, next_sflow, idl) {
1118 ovsrec_sflow_delete(sflow);
1121 OVSREC_IPFIX_FOR_EACH_SAFE (ipfix, next_ipfix, idl) {
1122 ovsrec_ipfix_delete(ipfix);
1125 OVSREC_FLOW_SAMPLE_COLLECTOR_SET_FOR_EACH_SAFE (fscset, next_fscset, idl) {
1126 ovsrec_flow_sample_collector_set_delete(fscset);
1129 vsctl_context_invalidate_cache(ctx);
1133 cmd_add_br(struct ctl_context *ctx)
1135 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
1136 bool may_exist = shash_find(&ctx->options, "--may-exist") != NULL;
1137 const char *br_name, *parent_name;
1138 struct ovsrec_interface *iface;
1141 br_name = ctx->argv[1];
1142 if (ctx->argc == 2) {
1145 } else if (ctx->argc == 4) {
1146 parent_name = ctx->argv[2];
1147 vlan = atoi(ctx->argv[3]);
1148 if (vlan < 0 || vlan > 4095) {
1149 ctl_fatal("%s: vlan must be between 0 and 4095", ctx->argv[0]);
1152 ctl_fatal("'%s' command takes exactly 1 or 3 arguments",
1156 vsctl_context_populate_cache(ctx);
1158 struct vsctl_bridge *br;
1160 br = find_bridge(vsctl_ctx, br_name, false);
1164 ctl_fatal("\"--may-exist add-br %s\" but %s is "
1165 "a VLAN bridge for VLAN %d",
1166 br_name, br_name, br->vlan);
1170 ctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
1171 "is not a VLAN bridge",
1172 br_name, parent_name, vlan, br_name);
1173 } else if (strcmp(br->parent->name, parent_name)) {
1174 ctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
1175 "has the wrong parent %s",
1176 br_name, parent_name, vlan,
1177 br_name, br->parent->name);
1178 } else if (br->vlan != vlan) {
1179 ctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
1180 "is a VLAN bridge for the wrong VLAN %d",
1181 br_name, parent_name, vlan, br_name, br->vlan);
1187 check_conflicts(vsctl_ctx, br_name,
1188 xasprintf("cannot create a bridge named %s", br_name));
1191 struct ovsrec_port *port;
1192 struct ovsrec_bridge *br;
1194 iface = ovsrec_interface_insert(ctx->txn);
1195 ovsrec_interface_set_name(iface, br_name);
1196 ovsrec_interface_set_type(iface, "internal");
1198 port = ovsrec_port_insert(ctx->txn);
1199 ovsrec_port_set_name(port, br_name);
1200 ovsrec_port_set_interfaces(port, &iface, 1);
1202 br = ovsrec_bridge_insert(ctx->txn);
1203 ovsrec_bridge_set_name(br, br_name);
1204 ovsrec_bridge_set_ports(br, &port, 1);
1206 ovs_insert_bridge(vsctl_ctx->ovs, br);
1208 struct vsctl_bridge *conflict;
1209 struct vsctl_bridge *parent;
1210 struct ovsrec_port *port;
1211 struct ovsrec_bridge *br;
1214 parent = find_bridge(vsctl_ctx, parent_name, false);
1215 if (parent && parent->parent) {
1216 ctl_fatal("cannot create bridge with fake bridge as parent");
1219 ctl_fatal("parent bridge %s does not exist", parent_name);
1221 conflict = find_vlan_bridge(parent, vlan);
1223 ctl_fatal("bridge %s already has a child VLAN bridge %s "
1224 "on VLAN %d", parent_name, conflict->name, vlan);
1226 br = parent->br_cfg;
1228 iface = ovsrec_interface_insert(ctx->txn);
1229 ovsrec_interface_set_name(iface, br_name);
1230 ovsrec_interface_set_type(iface, "internal");
1232 port = ovsrec_port_insert(ctx->txn);
1233 ovsrec_port_set_name(port, br_name);
1234 ovsrec_port_set_interfaces(port, &iface, 1);
1235 ovsrec_port_set_fake_bridge(port, true);
1236 ovsrec_port_set_tag(port, &tag, 1);
1238 bridge_insert_port(br, port);
1241 post_db_reload_expect_iface(iface);
1242 vsctl_context_invalidate_cache(ctx);
1246 del_port(struct vsctl_context *vsctl_ctx, struct vsctl_port *port)
1248 struct vsctl_iface *iface, *next_iface;
1250 bridge_delete_port((port->bridge->parent
1251 ? port->bridge->parent->br_cfg
1252 : port->bridge->br_cfg), port->port_cfg);
1254 LIST_FOR_EACH_SAFE (iface, next_iface, ifaces_node, &port->ifaces) {
1255 del_cached_iface(vsctl_ctx, iface);
1257 del_cached_port(vsctl_ctx, port);
1261 del_bridge(struct vsctl_context *vsctl_ctx, struct vsctl_bridge *br)
1263 struct vsctl_bridge *child, *next_child;
1264 struct vsctl_port *port, *next_port;
1265 const struct ovsrec_flow_sample_collector_set *fscset, *next_fscset;
1267 HMAP_FOR_EACH_SAFE (child, next_child, children_node, &br->children) {
1268 del_bridge(vsctl_ctx, child);
1271 LIST_FOR_EACH_SAFE (port, next_port, ports_node, &br->ports) {
1272 del_port(vsctl_ctx, port);
1275 OVSREC_FLOW_SAMPLE_COLLECTOR_SET_FOR_EACH_SAFE (fscset, next_fscset,
1276 vsctl_ctx->base.idl) {
1277 if (fscset->bridge == br->br_cfg) {
1278 ovsrec_flow_sample_collector_set_delete(fscset);
1282 del_cached_bridge(vsctl_ctx, br);
1286 cmd_del_br(struct ctl_context *ctx)
1288 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
1289 bool must_exist = !shash_find(&ctx->options, "--if-exists");
1290 struct vsctl_bridge *bridge;
1292 vsctl_context_populate_cache(ctx);
1293 bridge = find_bridge(vsctl_ctx, ctx->argv[1], must_exist);
1295 del_bridge(vsctl_ctx, bridge);
1300 output_sorted(struct svec *svec, struct ds *output)
1306 SVEC_FOR_EACH (i, name, svec) {
1307 ds_put_format(output, "%s\n", name);
1312 cmd_list_br(struct ctl_context *ctx)
1314 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
1315 struct shash_node *node;
1316 struct svec bridges;
1317 bool real = shash_find(&ctx->options, "--real");
1318 bool fake = shash_find(&ctx->options, "--fake");
1320 /* If neither fake nor real were requested, return both. */
1321 if (!real && !fake) {
1325 vsctl_context_populate_cache(ctx);
1327 svec_init(&bridges);
1328 SHASH_FOR_EACH (node, &vsctl_ctx->bridges) {
1329 struct vsctl_bridge *br = node->data;
1331 if (br->parent ? fake : real) {
1332 svec_add(&bridges, br->name);
1335 output_sorted(&bridges, &ctx->output);
1336 svec_destroy(&bridges);
1340 cmd_br_exists(struct ctl_context *ctx)
1342 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
1344 vsctl_context_populate_cache(ctx);
1345 if (!find_bridge(vsctl_ctx, ctx->argv[1], false)) {
1351 set_external_id(struct smap *old, struct smap *new,
1352 char *key, char *value)
1354 smap_clone(new, old);
1357 smap_replace(new, key, value);
1359 smap_remove(new, key);
1364 pre_cmd_br_set_external_id(struct ctl_context *ctx)
1367 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_external_ids);
1368 ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_external_ids);
1372 cmd_br_set_external_id(struct ctl_context *ctx)
1374 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
1375 struct vsctl_bridge *bridge;
1378 vsctl_context_populate_cache(ctx);
1379 bridge = find_bridge(vsctl_ctx, ctx->argv[1], true);
1380 if (bridge->br_cfg) {
1382 set_external_id(&bridge->br_cfg->external_ids, &new, ctx->argv[2],
1383 ctx->argc >= 4 ? ctx->argv[3] : NULL);
1384 ovsrec_bridge_verify_external_ids(bridge->br_cfg);
1385 ovsrec_bridge_set_external_ids(bridge->br_cfg, &new);
1387 char *key = xasprintf("fake-bridge-%s", ctx->argv[2]);
1388 struct vsctl_port *port = shash_find_data(&vsctl_ctx->ports,
1390 set_external_id(&port->port_cfg->external_ids, &new,
1391 key, ctx->argc >= 4 ? ctx->argv[3] : NULL);
1392 ovsrec_port_verify_external_ids(port->port_cfg);
1393 ovsrec_port_set_external_ids(port->port_cfg, &new);
1400 get_external_id(struct smap *smap, const char *prefix, const char *key,
1404 char *prefix_key = xasprintf("%s%s", prefix, key);
1405 const char *value = smap_get(smap, prefix_key);
1408 ds_put_format(output, "%s\n", value);
1412 const struct smap_node **sorted = smap_sort(smap);
1413 size_t prefix_len = strlen(prefix);
1416 for (i = 0; i < smap_count(smap); i++) {
1417 const struct smap_node *node = sorted[i];
1418 if (!strncmp(node->key, prefix, prefix_len)) {
1419 ds_put_format(output, "%s=%s\n", node->key + prefix_len,
1428 pre_cmd_br_get_external_id(struct ctl_context *ctx)
1430 pre_cmd_br_set_external_id(ctx);
1434 cmd_br_get_external_id(struct ctl_context *ctx)
1436 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
1437 struct vsctl_bridge *bridge;
1439 vsctl_context_populate_cache(ctx);
1441 bridge = find_bridge(vsctl_ctx, ctx->argv[1], true);
1442 if (bridge->br_cfg) {
1443 ovsrec_bridge_verify_external_ids(bridge->br_cfg);
1444 get_external_id(&bridge->br_cfg->external_ids, "",
1445 ctx->argc >= 3 ? ctx->argv[2] : NULL, &ctx->output);
1447 struct vsctl_port *port = shash_find_data(&vsctl_ctx->ports,
1449 ovsrec_port_verify_external_ids(port->port_cfg);
1450 get_external_id(&port->port_cfg->external_ids, "fake-bridge-",
1451 ctx->argc >= 3 ? ctx->argv[2] : NULL, &ctx->output);
1456 cmd_list_ports(struct ctl_context *ctx)
1458 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
1459 struct vsctl_bridge *br;
1460 struct vsctl_port *port;
1463 vsctl_context_populate_cache(ctx);
1464 br = find_bridge(vsctl_ctx, ctx->argv[1], true);
1465 ovsrec_bridge_verify_ports(br->br_cfg ? br->br_cfg : br->parent->br_cfg);
1468 LIST_FOR_EACH (port, ports_node, &br->ports) {
1469 if (strcmp(port->port_cfg->name, br->name)) {
1470 svec_add(&ports, port->port_cfg->name);
1473 output_sorted(&ports, &ctx->output);
1474 svec_destroy(&ports);
1478 add_port(struct ctl_context *ctx,
1479 const char *br_name, const char *port_name,
1480 bool may_exist, bool fake_iface,
1481 char *iface_names[], int n_ifaces,
1482 char *settings[], int n_settings)
1484 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
1485 struct vsctl_port *vsctl_port;
1486 struct vsctl_bridge *bridge;
1487 struct ovsrec_interface **ifaces;
1488 struct ovsrec_port *port;
1491 vsctl_context_populate_cache(ctx);
1493 struct vsctl_port *vsctl_port;
1495 vsctl_port = find_port(vsctl_ctx, port_name, false);
1497 struct svec want_names, have_names;
1499 svec_init(&want_names);
1500 for (i = 0; i < n_ifaces; i++) {
1501 svec_add(&want_names, iface_names[i]);
1503 svec_sort(&want_names);
1505 svec_init(&have_names);
1506 for (i = 0; i < vsctl_port->port_cfg->n_interfaces; i++) {
1507 svec_add(&have_names,
1508 vsctl_port->port_cfg->interfaces[i]->name);
1510 svec_sort(&have_names);
1512 if (strcmp(vsctl_port->bridge->name, br_name)) {
1513 char *command = vsctl_context_to_string(ctx);
1514 ctl_fatal("\"%s\" but %s is actually attached to bridge %s",
1515 command, port_name, vsctl_port->bridge->name);
1518 if (!svec_equal(&want_names, &have_names)) {
1519 char *have_names_string = svec_join(&have_names, ", ", "");
1520 char *command = vsctl_context_to_string(ctx);
1522 ctl_fatal("\"%s\" but %s actually has interface(s) %s",
1523 command, port_name, have_names_string);
1526 svec_destroy(&want_names);
1527 svec_destroy(&have_names);
1532 check_conflicts(vsctl_ctx, port_name,
1533 xasprintf("cannot create a port named %s", port_name));
1534 for (i = 0; i < n_ifaces; i++) {
1535 check_conflicts(vsctl_ctx, iface_names[i],
1536 xasprintf("cannot create an interface named %s",
1539 bridge = find_bridge(vsctl_ctx, br_name, true);
1541 ifaces = xmalloc(n_ifaces * sizeof *ifaces);
1542 for (i = 0; i < n_ifaces; i++) {
1543 ifaces[i] = ovsrec_interface_insert(ctx->txn);
1544 ovsrec_interface_set_name(ifaces[i], iface_names[i]);
1545 post_db_reload_expect_iface(ifaces[i]);
1548 port = ovsrec_port_insert(ctx->txn);
1549 ovsrec_port_set_name(port, port_name);
1550 ovsrec_port_set_interfaces(port, ifaces, n_ifaces);
1551 ovsrec_port_set_bond_fake_iface(port, fake_iface);
1553 if (bridge->parent) {
1554 int64_t tag = bridge->vlan;
1555 ovsrec_port_set_tag(port, &tag, 1);
1558 for (i = 0; i < n_settings; i++) {
1559 ctl_set_column("Port", &port->header_, settings[i],
1563 bridge_insert_port((bridge->parent ? bridge->parent->br_cfg
1564 : bridge->br_cfg), port);
1566 vsctl_port = add_port_to_cache(vsctl_ctx, bridge, port);
1567 for (i = 0; i < n_ifaces; i++) {
1568 add_iface_to_cache(vsctl_ctx, vsctl_port, ifaces[i]);
1574 cmd_add_port(struct ctl_context *ctx)
1576 bool may_exist = shash_find(&ctx->options, "--may-exist") != NULL;
1578 add_port(ctx, ctx->argv[1], ctx->argv[2], may_exist, false,
1579 &ctx->argv[2], 1, &ctx->argv[3], ctx->argc - 3);
1583 cmd_add_bond(struct ctl_context *ctx)
1585 bool may_exist = shash_find(&ctx->options, "--may-exist") != NULL;
1586 bool fake_iface = shash_find(&ctx->options, "--fake-iface");
1590 n_ifaces = ctx->argc - 3;
1591 for (i = 3; i < ctx->argc; i++) {
1592 if (strchr(ctx->argv[i], '=')) {
1598 ctl_fatal("add-bond requires at least 2 interfaces, but only "
1599 "%d were specified", n_ifaces);
1602 add_port(ctx, ctx->argv[1], ctx->argv[2], may_exist, fake_iface,
1603 &ctx->argv[3], n_ifaces,
1604 &ctx->argv[n_ifaces + 3], ctx->argc - 3 - n_ifaces);
1608 cmd_del_port(struct ctl_context *ctx)
1610 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
1611 bool must_exist = !shash_find(&ctx->options, "--if-exists");
1612 bool with_iface = shash_find(&ctx->options, "--with-iface") != NULL;
1613 const char *target = ctx->argv[ctx->argc - 1];
1614 struct vsctl_port *port;
1616 vsctl_context_populate_cache(ctx);
1617 if (find_bridge(vsctl_ctx, target, false)) {
1619 ctl_fatal("cannot delete port %s because it is the local port "
1620 "for bridge %s (deleting this port requires deleting "
1621 "the entire bridge)", target, target);
1624 } else if (!with_iface) {
1625 port = find_port(vsctl_ctx, target, must_exist);
1627 struct vsctl_iface *iface;
1629 port = find_port(vsctl_ctx, target, false);
1631 iface = find_iface(vsctl_ctx, target, false);
1636 if (must_exist && !port) {
1637 ctl_fatal("no port or interface named %s", target);
1642 if (ctx->argc == 3) {
1643 struct vsctl_bridge *bridge;
1645 bridge = find_bridge(vsctl_ctx, ctx->argv[1], true);
1646 if (port->bridge != bridge) {
1647 if (port->bridge->parent == bridge) {
1648 ctl_fatal("bridge %s does not have a port %s (although "
1649 "its parent bridge %s does)",
1650 ctx->argv[1], ctx->argv[2],
1651 bridge->parent->name);
1653 ctl_fatal("bridge %s does not have a port %s",
1654 ctx->argv[1], ctx->argv[2]);
1659 del_port(vsctl_ctx, port);
1664 cmd_port_to_br(struct ctl_context *ctx)
1666 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
1667 struct vsctl_port *port;
1669 vsctl_context_populate_cache(ctx);
1671 port = find_port(vsctl_ctx, ctx->argv[1], true);
1672 ds_put_format(&ctx->output, "%s\n", port->bridge->name);
1676 cmd_br_to_vlan(struct ctl_context *ctx)
1678 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
1679 struct vsctl_bridge *bridge;
1681 vsctl_context_populate_cache(ctx);
1683 bridge = find_bridge(vsctl_ctx, ctx->argv[1], true);
1684 ds_put_format(&ctx->output, "%d\n", bridge->vlan);
1688 cmd_br_to_parent(struct ctl_context *ctx)
1690 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
1691 struct vsctl_bridge *bridge;
1693 vsctl_context_populate_cache(ctx);
1695 bridge = find_bridge(vsctl_ctx, ctx->argv[1], true);
1696 if (bridge->parent) {
1697 bridge = bridge->parent;
1699 ds_put_format(&ctx->output, "%s\n", bridge->name);
1703 cmd_list_ifaces(struct ctl_context *ctx)
1705 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
1706 struct vsctl_bridge *br;
1707 struct vsctl_port *port;
1710 vsctl_context_populate_cache(ctx);
1712 br = find_bridge(vsctl_ctx, ctx->argv[1], true);
1713 verify_ports(vsctl_ctx);
1716 LIST_FOR_EACH (port, ports_node, &br->ports) {
1717 struct vsctl_iface *iface;
1719 LIST_FOR_EACH (iface, ifaces_node, &port->ifaces) {
1720 if (strcmp(iface->iface_cfg->name, br->name)) {
1721 svec_add(&ifaces, iface->iface_cfg->name);
1725 output_sorted(&ifaces, &ctx->output);
1726 svec_destroy(&ifaces);
1730 cmd_iface_to_br(struct ctl_context *ctx)
1732 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
1733 struct vsctl_iface *iface;
1735 vsctl_context_populate_cache(ctx);
1737 iface = find_iface(vsctl_ctx, ctx->argv[1], true);
1738 ds_put_format(&ctx->output, "%s\n", iface->port->bridge->name);
1742 verify_controllers(struct ovsrec_bridge *bridge)
1746 ovsrec_bridge_verify_controller(bridge);
1747 for (i = 0; i < bridge->n_controller; i++) {
1748 ovsrec_controller_verify_target(bridge->controller[i]);
1753 pre_controller(struct ctl_context *ctx)
1757 ovsdb_idl_add_column(ctx->idl, &ovsrec_controller_col_target);
1761 cmd_get_controller(struct ctl_context *ctx)
1763 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
1764 struct vsctl_bridge *br;
1765 struct svec targets;
1768 vsctl_context_populate_cache(ctx);
1770 br = find_bridge(vsctl_ctx, ctx->argv[1], true);
1774 verify_controllers(br->br_cfg);
1776 /* Print the targets in sorted order for reproducibility. */
1777 svec_init(&targets);
1778 for (i = 0; i < br->br_cfg->n_controller; i++) {
1779 svec_add(&targets, br->br_cfg->controller[i]->target);
1782 svec_sort(&targets);
1783 for (i = 0; i < targets.n; i++) {
1784 ds_put_format(&ctx->output, "%s\n", targets.names[i]);
1786 svec_destroy(&targets);
1790 delete_controllers(struct ovsrec_controller **controllers,
1791 size_t n_controllers)
1795 for (i = 0; i < n_controllers; i++) {
1796 ovsrec_controller_delete(controllers[i]);
1801 cmd_del_controller(struct ctl_context *ctx)
1803 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
1804 struct ovsrec_bridge *br;
1806 vsctl_context_populate_cache(ctx);
1808 br = find_real_bridge(vsctl_ctx, ctx->argv[1], true)->br_cfg;
1809 verify_controllers(br);
1811 if (br->controller) {
1812 delete_controllers(br->controller, br->n_controller);
1813 ovsrec_bridge_set_controller(br, NULL, 0);
1817 static struct ovsrec_controller **
1818 insert_controllers(struct ovsdb_idl_txn *txn, char *targets[], size_t n)
1820 struct ovsrec_controller **controllers;
1823 controllers = xmalloc(n * sizeof *controllers);
1824 for (i = 0; i < n; i++) {
1825 if (vconn_verify_name(targets[i]) && pvconn_verify_name(targets[i])) {
1826 VLOG_WARN("target type \"%s\" is possibly erroneous", targets[i]);
1828 controllers[i] = ovsrec_controller_insert(txn);
1829 ovsrec_controller_set_target(controllers[i], targets[i]);
1836 cmd_set_controller(struct ctl_context *ctx)
1838 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
1839 struct ovsrec_controller **controllers;
1840 struct ovsrec_bridge *br;
1843 vsctl_context_populate_cache(ctx);
1845 br = find_real_bridge(vsctl_ctx, ctx->argv[1], true)->br_cfg;
1846 verify_controllers(br);
1848 delete_controllers(br->controller, br->n_controller);
1851 controllers = insert_controllers(ctx->txn, &ctx->argv[2], n);
1852 ovsrec_bridge_set_controller(br, controllers, n);
1857 cmd_get_fail_mode(struct ctl_context *ctx)
1859 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
1860 struct vsctl_bridge *br;
1861 const char *fail_mode;
1863 vsctl_context_populate_cache(ctx);
1864 br = find_bridge(vsctl_ctx, ctx->argv[1], true);
1869 ovsrec_bridge_verify_fail_mode(br->br_cfg);
1871 fail_mode = br->br_cfg->fail_mode;
1872 if (fail_mode && strlen(fail_mode)) {
1873 ds_put_format(&ctx->output, "%s\n", fail_mode);
1878 cmd_del_fail_mode(struct ctl_context *ctx)
1880 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
1881 struct vsctl_bridge *br;
1883 vsctl_context_populate_cache(ctx);
1885 br = find_real_bridge(vsctl_ctx, ctx->argv[1], true);
1887 ovsrec_bridge_set_fail_mode(br->br_cfg, NULL);
1891 cmd_set_fail_mode(struct ctl_context *ctx)
1893 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
1894 struct vsctl_bridge *br;
1895 const char *fail_mode = ctx->argv[2];
1897 vsctl_context_populate_cache(ctx);
1899 br = find_real_bridge(vsctl_ctx, ctx->argv[1], true);
1901 if (strcmp(fail_mode, "standalone") && strcmp(fail_mode, "secure")) {
1902 ctl_fatal("fail-mode must be \"standalone\" or \"secure\"");
1905 ovsrec_bridge_set_fail_mode(br->br_cfg, fail_mode);
1909 verify_managers(const struct ovsrec_open_vswitch *ovs)
1913 ovsrec_open_vswitch_verify_manager_options(ovs);
1915 for (i = 0; i < ovs->n_manager_options; ++i) {
1916 const struct ovsrec_manager *mgr = ovs->manager_options[i];
1918 ovsrec_manager_verify_target(mgr);
1923 pre_manager(struct ctl_context *ctx)
1925 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_manager_options);
1926 ovsdb_idl_add_column(ctx->idl, &ovsrec_manager_col_target);
1930 cmd_get_manager(struct ctl_context *ctx)
1932 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
1933 const struct ovsrec_open_vswitch *ovs = vsctl_ctx->ovs;
1934 struct svec targets;
1937 verify_managers(ovs);
1939 /* Print the targets in sorted order for reproducibility. */
1940 svec_init(&targets);
1942 for (i = 0; i < ovs->n_manager_options; i++) {
1943 svec_add(&targets, ovs->manager_options[i]->target);
1946 svec_sort_unique(&targets);
1947 for (i = 0; i < targets.n; i++) {
1948 ds_put_format(&ctx->output, "%s\n", targets.names[i]);
1950 svec_destroy(&targets);
1954 delete_managers(const struct ovsrec_open_vswitch *ovs)
1958 /* Delete Manager rows pointed to by 'manager_options' column. */
1959 for (i = 0; i < ovs->n_manager_options; i++) {
1960 ovsrec_manager_delete(ovs->manager_options[i]);
1963 /* Delete 'Manager' row refs in 'manager_options' column. */
1964 ovsrec_open_vswitch_set_manager_options(ovs, NULL, 0);
1968 cmd_del_manager(struct ctl_context *ctx)
1970 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
1971 const struct ovsrec_open_vswitch *ovs = vsctl_ctx->ovs;
1973 verify_managers(ovs);
1974 delete_managers(ovs);
1978 insert_managers(struct vsctl_context *vsctl_ctx, char *targets[], size_t n)
1980 struct ovsrec_manager **managers;
1983 /* Insert each manager in a new row in Manager table. */
1984 managers = xmalloc(n * sizeof *managers);
1985 for (i = 0; i < n; i++) {
1986 if (stream_verify_name(targets[i]) && pstream_verify_name(targets[i])) {
1987 VLOG_WARN("target type \"%s\" is possibly erroneous", targets[i]);
1989 managers[i] = ovsrec_manager_insert(vsctl_ctx->base.txn);
1990 ovsrec_manager_set_target(managers[i], targets[i]);
1993 /* Store uuids of new Manager rows in 'manager_options' column. */
1994 ovsrec_open_vswitch_set_manager_options(vsctl_ctx->ovs, managers, n);
1999 cmd_set_manager(struct ctl_context *ctx)
2001 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
2002 const size_t n = ctx->argc - 1;
2004 verify_managers(vsctl_ctx->ovs);
2005 delete_managers(vsctl_ctx->ovs);
2006 insert_managers(vsctl_ctx, &ctx->argv[1], n);
2010 pre_cmd_get_ssl(struct ctl_context *ctx)
2012 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_ssl);
2014 ovsdb_idl_add_column(ctx->idl, &ovsrec_ssl_col_private_key);
2015 ovsdb_idl_add_column(ctx->idl, &ovsrec_ssl_col_certificate);
2016 ovsdb_idl_add_column(ctx->idl, &ovsrec_ssl_col_ca_cert);
2017 ovsdb_idl_add_column(ctx->idl, &ovsrec_ssl_col_bootstrap_ca_cert);
2021 cmd_get_ssl(struct ctl_context *ctx)
2023 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
2024 struct ovsrec_ssl *ssl = vsctl_ctx->ovs->ssl;
2026 ovsrec_open_vswitch_verify_ssl(vsctl_ctx->ovs);
2028 ovsrec_ssl_verify_private_key(ssl);
2029 ovsrec_ssl_verify_certificate(ssl);
2030 ovsrec_ssl_verify_ca_cert(ssl);
2031 ovsrec_ssl_verify_bootstrap_ca_cert(ssl);
2033 ds_put_format(&ctx->output, "Private key: %s\n", ssl->private_key);
2034 ds_put_format(&ctx->output, "Certificate: %s\n", ssl->certificate);
2035 ds_put_format(&ctx->output, "CA Certificate: %s\n", ssl->ca_cert);
2036 ds_put_format(&ctx->output, "Bootstrap: %s\n",
2037 ssl->bootstrap_ca_cert ? "true" : "false");
2042 pre_cmd_del_ssl(struct ctl_context *ctx)
2044 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_ssl);
2048 cmd_del_ssl(struct ctl_context *ctx)
2050 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
2051 struct ovsrec_ssl *ssl = vsctl_ctx->ovs->ssl;
2054 ovsrec_open_vswitch_verify_ssl(vsctl_ctx->ovs);
2055 ovsrec_ssl_delete(ssl);
2056 ovsrec_open_vswitch_set_ssl(vsctl_ctx->ovs, NULL);
2061 pre_cmd_set_ssl(struct ctl_context *ctx)
2063 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_ssl);
2067 cmd_set_ssl(struct ctl_context *ctx)
2069 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
2070 bool bootstrap = shash_find(&ctx->options, "--bootstrap");
2071 struct ovsrec_ssl *ssl = vsctl_ctx->ovs->ssl;
2073 ovsrec_open_vswitch_verify_ssl(vsctl_ctx->ovs);
2075 ovsrec_ssl_delete(ssl);
2077 ssl = ovsrec_ssl_insert(ctx->txn);
2079 ovsrec_ssl_set_private_key(ssl, ctx->argv[1]);
2080 ovsrec_ssl_set_certificate(ssl, ctx->argv[2]);
2081 ovsrec_ssl_set_ca_cert(ssl, ctx->argv[3]);
2083 ovsrec_ssl_set_bootstrap_ca_cert(ssl, bootstrap);
2085 ovsrec_open_vswitch_set_ssl(vsctl_ctx->ovs, ssl);
2089 autoattach_insert_mapping(struct ovsrec_autoattach *aa,
2093 int64_t *key_mappings, *value_mappings;
2096 key_mappings = xmalloc(sizeof *aa->key_mappings * (aa->n_mappings + 1));
2097 value_mappings = xmalloc(sizeof *aa->value_mappings * (aa->n_mappings + 1));
2099 for (i = 0; i < aa->n_mappings; i++) {
2100 key_mappings[i] = aa->key_mappings[i];
2101 value_mappings[i] = aa->value_mappings[i];
2103 key_mappings[aa->n_mappings] = isid;
2104 value_mappings[aa->n_mappings] = vlan;
2106 ovsrec_autoattach_set_mappings(aa, key_mappings, value_mappings,
2107 aa->n_mappings + 1);
2110 free(value_mappings);
2114 cmd_add_aa_mapping(struct ctl_context *ctx)
2116 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
2117 struct vsctl_bridge *br;
2121 isid = strtoull(ctx->argv[2], &nptr, 10);
2122 if (nptr == ctx->argv[2] || nptr == NULL) {
2123 ctl_fatal("Invalid argument %s", ctx->argv[2]);
2127 vlan = strtoull(ctx->argv[3], &nptr, 10);
2128 if (nptr == ctx->argv[3] || nptr == NULL) {
2129 ctl_fatal("Invalid argument %s", ctx->argv[3]);
2133 vsctl_context_populate_cache(ctx);
2135 br = find_bridge(vsctl_ctx, ctx->argv[1], true);
2140 if (br && br->br_cfg) {
2141 if (!br->br_cfg->auto_attach) {
2142 struct ovsrec_autoattach *aa = ovsrec_autoattach_insert(ctx->txn);
2143 ovsrec_bridge_set_auto_attach(br->br_cfg, aa);
2145 autoattach_insert_mapping(br->br_cfg->auto_attach, isid, vlan);
2150 del_aa_mapping(struct ovsrec_autoattach *aa,
2154 int64_t *key_mappings, *value_mappings;
2157 key_mappings = xmalloc(sizeof *aa->key_mappings * (aa->n_mappings));
2158 value_mappings = xmalloc(sizeof *value_mappings * (aa->n_mappings));
2160 for (i = n = 0; i < aa->n_mappings; i++) {
2161 if (aa->key_mappings[i] != isid && aa->value_mappings[i] != vlan) {
2162 key_mappings[n] = aa->key_mappings[i];
2163 value_mappings[n++] = aa->value_mappings[i];
2167 ovsrec_autoattach_set_mappings(aa, key_mappings, value_mappings, n);
2170 free(value_mappings);
2174 cmd_del_aa_mapping(struct ctl_context *ctx)
2176 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
2177 struct vsctl_bridge *br;
2181 isid = strtoull(ctx->argv[2], &nptr, 10);
2182 if (nptr == ctx->argv[2] || nptr == NULL) {
2183 ctl_fatal("Invalid argument %s", ctx->argv[2]);
2187 vlan = strtoull(ctx->argv[3], &nptr, 10);
2188 if (nptr == ctx->argv[3] || nptr == NULL) {
2189 ctl_fatal("Invalid argument %s", ctx->argv[3]);
2193 vsctl_context_populate_cache(ctx);
2195 br = find_bridge(vsctl_ctx, ctx->argv[1], true);
2200 if (br && br->br_cfg && br->br_cfg->auto_attach &&
2201 br->br_cfg->auto_attach->key_mappings &&
2202 br->br_cfg->auto_attach->value_mappings) {
2205 for (i = 0; i < br->br_cfg->auto_attach->n_mappings; i++) {
2206 if (br->br_cfg->auto_attach->key_mappings[i] == isid &&
2207 br->br_cfg->auto_attach->value_mappings[i] == vlan) {
2208 del_aa_mapping(br->br_cfg->auto_attach, isid, vlan);
2216 pre_aa_mapping(struct ctl_context *ctx)
2220 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_auto_attach);
2221 ovsdb_idl_add_column(ctx->idl, &ovsrec_autoattach_col_mappings);
2225 verify_auto_attach(struct ovsrec_bridge *bridge)
2228 ovsrec_bridge_verify_auto_attach(bridge);
2230 if (bridge->auto_attach) {
2231 ovsrec_autoattach_verify_mappings(bridge->auto_attach);
2237 cmd_get_aa_mapping(struct ctl_context *ctx)
2239 struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
2240 struct vsctl_bridge *br;
2242 vsctl_context_populate_cache(ctx);
2244 br = find_bridge(vsctl_ctx, ctx->argv[1], true);
2249 verify_auto_attach(br->br_cfg);
2251 if (br && br->br_cfg && br->br_cfg->auto_attach &&
2252 br->br_cfg->auto_attach->key_mappings &&
2253 br->br_cfg->auto_attach->value_mappings) {
2256 for (i = 0; i < br->br_cfg->auto_attach->n_mappings; i++) {
2257 ds_put_format(&ctx->output, "%"PRId64" %"PRId64"\n",
2258 br->br_cfg->auto_attach->key_mappings[i],
2259 br->br_cfg->auto_attach->value_mappings[i]);
2265 static const struct ctl_table_class tables[] = {
2266 {&ovsrec_table_bridge,
2267 {{&ovsrec_table_bridge, &ovsrec_bridge_col_name, NULL},
2268 {&ovsrec_table_flow_sample_collector_set, NULL,
2269 &ovsrec_flow_sample_collector_set_col_bridge}}},
2271 {&ovsrec_table_controller,
2272 {{&ovsrec_table_bridge,
2273 &ovsrec_bridge_col_name,
2274 &ovsrec_bridge_col_controller}}},
2276 {&ovsrec_table_interface,
2277 {{&ovsrec_table_interface, &ovsrec_interface_col_name, NULL},
2278 {NULL, NULL, NULL}}},
2280 {&ovsrec_table_mirror,
2281 {{&ovsrec_table_mirror, &ovsrec_mirror_col_name, NULL},
2282 {NULL, NULL, NULL}}},
2284 {&ovsrec_table_manager,
2285 {{&ovsrec_table_manager, &ovsrec_manager_col_target, NULL},
2286 {NULL, NULL, NULL}}},
2288 {&ovsrec_table_netflow,
2289 {{&ovsrec_table_bridge,
2290 &ovsrec_bridge_col_name,
2291 &ovsrec_bridge_col_netflow},
2292 {NULL, NULL, NULL}}},
2294 {&ovsrec_table_open_vswitch,
2295 {{&ovsrec_table_open_vswitch, NULL, NULL},
2296 {NULL, NULL, NULL}}},
2298 {&ovsrec_table_port,
2299 {{&ovsrec_table_port, &ovsrec_port_col_name, NULL},
2300 {NULL, NULL, NULL}}},
2303 {{&ovsrec_table_port, &ovsrec_port_col_name, &ovsrec_port_col_qos},
2304 {NULL, NULL, NULL}}},
2306 {&ovsrec_table_queue,
2307 {{NULL, NULL, NULL},
2308 {NULL, NULL, NULL}}},
2311 {{&ovsrec_table_open_vswitch, NULL, &ovsrec_open_vswitch_col_ssl}}},
2313 {&ovsrec_table_sflow,
2314 {{&ovsrec_table_bridge,
2315 &ovsrec_bridge_col_name,
2316 &ovsrec_bridge_col_sflow},
2317 {NULL, NULL, NULL}}},
2319 {&ovsrec_table_flow_table,
2320 {{&ovsrec_table_flow_table, &ovsrec_flow_table_col_name, NULL},
2321 {NULL, NULL, NULL}}},
2323 {&ovsrec_table_ipfix,
2324 {{&ovsrec_table_bridge,
2325 &ovsrec_bridge_col_name,
2326 &ovsrec_bridge_col_ipfix},
2327 {&ovsrec_table_flow_sample_collector_set, NULL,
2328 &ovsrec_flow_sample_collector_set_col_ipfix}}},
2330 {&ovsrec_table_autoattach,
2331 {{&ovsrec_table_bridge,
2332 &ovsrec_bridge_col_name,
2333 &ovsrec_bridge_col_auto_attach},
2334 {NULL, NULL, NULL}}},
2336 {&ovsrec_table_flow_sample_collector_set,
2337 {{NULL, NULL, NULL},
2338 {NULL, NULL, NULL}}},
2340 {NULL, {{NULL, NULL, NULL}, {NULL, NULL, NULL}}}
2344 post_db_reload_check_init(void)
2346 n_neoteric_ifaces = 0;
2350 post_db_reload_expect_iface(const struct ovsrec_interface *iface)
2352 if (n_neoteric_ifaces >= allocated_neoteric_ifaces) {
2353 neoteric_ifaces = x2nrealloc(neoteric_ifaces,
2354 &allocated_neoteric_ifaces,
2355 sizeof *neoteric_ifaces);
2357 neoteric_ifaces[n_neoteric_ifaces++] = iface->header_.uuid;
2361 post_db_reload_do_checks(const struct vsctl_context *vsctl_ctx)
2363 struct ds dead_ifaces = DS_EMPTY_INITIALIZER;
2366 for (i = 0; i < n_neoteric_ifaces; i++) {
2367 const struct uuid *uuid;
2369 uuid = ovsdb_idl_txn_get_insert_uuid(vsctl_ctx->base.txn,
2370 &neoteric_ifaces[i]);
2372 const struct ovsrec_interface *iface;
2374 iface = ovsrec_interface_get_for_uuid(vsctl_ctx->base.idl, uuid);
2375 if (iface && (!iface->ofport || *iface->ofport == -1)) {
2376 ds_put_format(&dead_ifaces, "'%s', ", iface->name);
2381 if (dead_ifaces.length) {
2382 dead_ifaces.length -= 2; /* Strip off trailing comma and space. */
2383 ovs_error(0, "Error detected while setting up %s. See ovs-vswitchd "
2384 "log for details.", ds_cstr(&dead_ifaces));
2387 ds_destroy(&dead_ifaces);
2392 vsctl_context_init_command(struct vsctl_context *vsctl_ctx,
2393 struct ctl_command *command)
2395 ctl_context_init_command(&vsctl_ctx->base, command);
2396 vsctl_ctx->verified_ports = false;
2400 vsctl_context_init(struct vsctl_context *vsctl_ctx,
2401 struct ctl_command *command, struct ovsdb_idl *idl,
2402 struct ovsdb_idl_txn *txn,
2403 const struct ovsrec_open_vswitch *ovs,
2404 struct ovsdb_symbol_table *symtab)
2406 ctl_context_init(&vsctl_ctx->base, command, idl, txn, symtab,
2407 vsctl_context_invalidate_cache);
2409 vsctl_ctx->verified_ports = false;
2411 vsctl_ctx->ovs = ovs;
2412 vsctl_ctx->cache_valid = false;
2416 vsctl_context_done_command(struct vsctl_context *vsctl_ctx,
2417 struct ctl_command *command)
2419 ctl_context_done_command(&vsctl_ctx->base, command);
2423 vsctl_context_done(struct vsctl_context *vsctl_ctx,
2424 struct ctl_command *command)
2426 ctl_context_done(&vsctl_ctx->base, command);
2430 run_prerequisites(struct ctl_command *commands, size_t n_commands,
2431 struct ovsdb_idl *idl)
2433 struct ctl_command *c;
2435 ovsdb_idl_add_table(idl, &ovsrec_table_open_vswitch);
2436 if (wait_for_reload) {
2437 ovsdb_idl_add_column(idl, &ovsrec_open_vswitch_col_cur_cfg);
2439 for (c = commands; c < &commands[n_commands]; c++) {
2440 if (c->syntax->prerequisites) {
2441 struct vsctl_context vsctl_ctx;
2443 ds_init(&c->output);
2446 vsctl_context_init(&vsctl_ctx, c, idl, NULL, NULL, NULL);
2447 (c->syntax->prerequisites)(&vsctl_ctx.base);
2448 vsctl_context_done(&vsctl_ctx, c);
2450 ovs_assert(!c->output.string);
2451 ovs_assert(!c->table);
2457 do_vsctl(const char *args, struct ctl_command *commands, size_t n_commands,
2458 struct ovsdb_idl *idl)
2460 struct ovsdb_idl_txn *txn;
2461 const struct ovsrec_open_vswitch *ovs;
2462 enum ovsdb_idl_txn_status status;
2463 struct ovsdb_symbol_table *symtab;
2464 struct vsctl_context vsctl_ctx;
2465 struct ctl_command *c;
2466 struct shash_node *node;
2467 int64_t next_cfg = 0;
2470 txn = the_idl_txn = ovsdb_idl_txn_create(idl);
2472 ovsdb_idl_txn_set_dry_run(txn);
2475 ovsdb_idl_txn_add_comment(txn, "ovs-vsctl: %s", args);
2477 ovs = ovsrec_open_vswitch_first(idl);
2479 /* XXX add verification that table is empty */
2480 ovs = ovsrec_open_vswitch_insert(txn);
2483 if (wait_for_reload) {
2484 ovsdb_idl_txn_increment(txn, &ovs->header_,
2485 &ovsrec_open_vswitch_col_next_cfg);
2488 post_db_reload_check_init();
2489 symtab = ovsdb_symbol_table_create();
2490 for (c = commands; c < &commands[n_commands]; c++) {
2491 ds_init(&c->output);
2494 vsctl_context_init(&vsctl_ctx, NULL, idl, txn, ovs, symtab);
2495 for (c = commands; c < &commands[n_commands]; c++) {
2496 vsctl_context_init_command(&vsctl_ctx, c);
2497 if (c->syntax->run) {
2498 (c->syntax->run)(&vsctl_ctx.base);
2500 vsctl_context_done_command(&vsctl_ctx, c);
2502 if (vsctl_ctx.base.try_again) {
2503 vsctl_context_done(&vsctl_ctx, NULL);
2507 vsctl_context_done(&vsctl_ctx, NULL);
2509 SHASH_FOR_EACH (node, &symtab->sh) {
2510 struct ovsdb_symbol *symbol = node->data;
2511 if (!symbol->created) {
2512 ctl_fatal("row id \"%s\" is referenced but never created (e.g. "
2513 "with \"-- --id=%s create ...\")",
2514 node->name, node->name);
2516 if (!symbol->strong_ref) {
2517 if (!symbol->weak_ref) {
2518 VLOG_WARN("row id \"%s\" was created but no reference to it "
2519 "was inserted, so it will not actually appear in "
2520 "the database", node->name);
2522 VLOG_WARN("row id \"%s\" was created but only a weak "
2523 "reference to it was inserted, so it will not "
2524 "actually appear in the database", node->name);
2529 status = ovsdb_idl_txn_commit_block(txn);
2530 if (wait_for_reload && status == TXN_SUCCESS) {
2531 next_cfg = ovsdb_idl_txn_get_increment_new_value(txn);
2533 if (status == TXN_UNCHANGED || status == TXN_SUCCESS) {
2534 for (c = commands; c < &commands[n_commands]; c++) {
2535 if (c->syntax->postprocess) {
2536 vsctl_context_init(&vsctl_ctx, c, idl, txn, ovs, symtab);
2537 (c->syntax->postprocess)(&vsctl_ctx.base);
2538 vsctl_context_done(&vsctl_ctx, c);
2542 error = xstrdup(ovsdb_idl_txn_get_error(txn));
2545 case TXN_UNCOMMITTED:
2546 case TXN_INCOMPLETE:
2550 /* Should not happen--we never call ovsdb_idl_txn_abort(). */
2551 ctl_fatal("transaction aborted");
2561 ctl_fatal("transaction error: %s", error);
2563 case TXN_NOT_LOCKED:
2564 /* Should not happen--we never call ovsdb_idl_set_lock(). */
2565 ctl_fatal("database not locked");
2572 ovsdb_symbol_table_destroy(symtab);
2574 for (c = commands; c < &commands[n_commands]; c++) {
2575 struct ds *ds = &c->output;
2578 table_print(c->table, &table_style);
2579 } else if (oneline) {
2583 for (j = 0; j < ds->length; j++) {
2584 int ch = ds->string[j];
2587 fputs("\\n", stdout);
2591 fputs("\\\\", stdout);
2600 fputs(ds_cstr(ds), stdout);
2602 ds_destroy(&c->output);
2603 table_destroy(c->table);
2606 shash_destroy_free_data(&c->options);
2610 if (wait_for_reload && status != TXN_UNCHANGED) {
2611 /* Even, if --retry flag was not specified, ovs-vsctl still
2612 * has to retry to establish OVSDB connection, if wait_for_reload
2613 * was set. Otherwise, ovs-vsctl would end up waiting forever
2614 * until cur_cfg would be updated. */
2615 ovsdb_idl_enable_reconnect(idl);
2618 OVSREC_OPEN_VSWITCH_FOR_EACH (ovs, idl) {
2619 if (ovs->cur_cfg >= next_cfg) {
2620 post_db_reload_do_checks(&vsctl_ctx);
2624 ovsdb_idl_wait(idl);
2629 ovsdb_idl_txn_destroy(txn);
2630 ovsdb_idl_destroy(idl);
2635 /* Our transaction needs to be rerun, or a prerequisite was not met. Free
2636 * resources and return so that the caller can try again. */
2638 ovsdb_idl_txn_abort(txn);
2639 ovsdb_idl_txn_destroy(txn);
2642 ovsdb_symbol_table_destroy(symtab);
2643 for (c = commands; c < &commands[n_commands]; c++) {
2644 ds_destroy(&c->output);
2645 table_destroy(c->table);
2652 * Developers who add new commands to the 'struct ctl_command_syntax' must
2653 * define the 'arguments' member of the struct. The following keywords are
2654 * available for composing the argument format:
2656 * TABLE RECORD BRIDGE PARENT PORT
2657 * KEY VALUE ARG KEY=VALUE ?KEY=VALUE
2658 * IFACE SYSIFACE COLUMN COLUMN?:KEY COLUMN?:KEY=VALUE
2659 * MODE CA-CERT CERTIFICATE PRIVATE-KEY
2660 * TARGET NEW-* (e.g. NEW-PORT)
2662 * For argument types not listed above, just uses 'ARG' as place holder.
2664 * Encloses the keyword with '[]' if it is optional. Appends '...' to
2665 * keyword or enclosed keyword to indicate that the argument can be specified
2669 static const struct ctl_command_syntax vsctl_commands[] = {
2670 /* Open vSwitch commands. */
2671 {"init", 0, 0, "", NULL, cmd_init, NULL, "", RW},
2673 /* Bridge commands. */
2674 {"add-br", 1, 3, "NEW-BRIDGE [PARENT] [NEW-VLAN]", pre_get_info,
2675 cmd_add_br, NULL, "--may-exist", RW},
2676 {"del-br", 1, 1, "BRIDGE", pre_get_info, cmd_del_br,
2677 NULL, "--if-exists", RW},
2678 {"list-br", 0, 0, "", pre_get_info, cmd_list_br, NULL, "--real,--fake",
2680 {"br-exists", 1, 1, "BRIDGE", pre_get_info, cmd_br_exists, NULL, "", RO},
2681 {"br-to-vlan", 1, 1, "BRIDGE", pre_get_info, cmd_br_to_vlan, NULL, "",
2683 {"br-to-parent", 1, 1, "BRIDGE", pre_get_info, cmd_br_to_parent, NULL,
2685 {"br-set-external-id", 2, 3, "BRIDGE KEY [VALUE]",
2686 pre_cmd_br_set_external_id, cmd_br_set_external_id, NULL, "", RW},
2687 {"br-get-external-id", 1, 2, "BRIDGE [KEY]", pre_cmd_br_get_external_id,
2688 cmd_br_get_external_id, NULL, "", RO},
2690 /* Port commands. */
2691 {"list-ports", 1, 1, "BRIDGE", pre_get_info, cmd_list_ports, NULL, "",
2693 {"add-port", 2, INT_MAX, "BRIDGE NEW-PORT [COLUMN[:KEY]=VALUE]...",
2694 pre_get_info, cmd_add_port, NULL, "--may-exist", RW},
2695 {"add-bond", 4, INT_MAX,
2696 "BRIDGE NEW-BOND-PORT SYSIFACE... [COLUMN[:KEY]=VALUE]...", pre_get_info,
2697 cmd_add_bond, NULL, "--may-exist,--fake-iface", RW},
2698 {"del-port", 1, 2, "[BRIDGE] PORT|IFACE", pre_get_info, cmd_del_port, NULL,
2699 "--if-exists,--with-iface", RW},
2700 {"port-to-br", 1, 1, "PORT", pre_get_info, cmd_port_to_br, NULL, "", RO},
2702 /* Interface commands. */
2703 {"list-ifaces", 1, 1, "BRIDGE", pre_get_info, cmd_list_ifaces, NULL, "",
2705 {"iface-to-br", 1, 1, "IFACE", pre_get_info, cmd_iface_to_br, NULL, "",
2708 /* Controller commands. */
2709 {"get-controller", 1, 1, "BRIDGE", pre_controller, cmd_get_controller,
2711 {"del-controller", 1, 1, "BRIDGE", pre_controller, cmd_del_controller,
2713 {"set-controller", 1, INT_MAX, "BRIDGE TARGET...", pre_controller,
2714 cmd_set_controller, NULL, "", RW},
2715 {"get-fail-mode", 1, 1, "BRIDGE", pre_get_info, cmd_get_fail_mode, NULL,
2717 {"del-fail-mode", 1, 1, "BRIDGE", pre_get_info, cmd_del_fail_mode, NULL,
2719 {"set-fail-mode", 2, 2, "BRIDGE MODE", pre_get_info, cmd_set_fail_mode,
2722 /* Manager commands. */
2723 {"get-manager", 0, 0, "", pre_manager, cmd_get_manager, NULL, "", RO},
2724 {"del-manager", 0, 0, "", pre_manager, cmd_del_manager, NULL, "", RW},
2725 {"set-manager", 1, INT_MAX, "TARGET...", pre_manager, cmd_set_manager,
2729 {"get-ssl", 0, 0, "", pre_cmd_get_ssl, cmd_get_ssl, NULL, "", RO},
2730 {"del-ssl", 0, 0, "", pre_cmd_del_ssl, cmd_del_ssl, NULL, "", RW},
2731 {"set-ssl", 3, 3, "PRIVATE-KEY CERTIFICATE CA-CERT", pre_cmd_set_ssl,
2732 cmd_set_ssl, NULL, "--bootstrap", RW},
2734 /* Auto Attach commands. */
2735 {"add-aa-mapping", 3, 3, "BRIDGE ARG ARG", pre_aa_mapping, cmd_add_aa_mapping,
2737 {"del-aa-mapping", 3, 3, "BRIDGE ARG ARG", pre_aa_mapping, cmd_del_aa_mapping,
2739 {"get-aa-mapping", 1, 1, "BRIDGE", pre_aa_mapping, cmd_get_aa_mapping,
2742 /* Switch commands. */
2743 {"emer-reset", 0, 0, "", pre_cmd_emer_reset, cmd_emer_reset, NULL, "", RW},
2745 {NULL, 0, 0, NULL, NULL, NULL, NULL, NULL, RO},
2748 /* Registers vsctl and common db commands. */
2750 vsctl_cmd_init(void)
2753 ctl_register_commands(vsctl_commands);