2 * Copyright (c) 2009, 2010, 2011, 2012, 2013 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 "command-line.h"
33 #include "dynamic-string.h"
36 #include "ovsdb-data.h"
37 #include "ovsdb-idl.h"
38 #include "poll-loop.h"
41 #include "stream-ssl.h"
45 #include "lib/vswitch-idl.h"
52 VLOG_DEFINE_THIS_MODULE(vsctl);
54 /* vsctl_fatal() also logs the error, so it is preferred in this file. */
55 #define ovs_fatal please_use_vsctl_fatal_instead_of_ovs_fatal
59 /* A command supported by ovs-vsctl. */
60 struct vsctl_command_syntax {
61 const char *name; /* e.g. "add-br" */
62 int min_args; /* Min number of arguments following name. */
63 int max_args; /* Max number of arguments following name. */
65 /* If nonnull, calls ovsdb_idl_add_column() or ovsdb_idl_add_table() for
66 * each column or table in ctx->idl that it uses. */
67 void (*prerequisites)(struct vsctl_context *ctx);
69 /* Does the actual work of the command and puts the command's output, if
70 * any, in ctx->output or ctx->table.
72 * Alternatively, if some prerequisite of the command is not met and the
73 * caller should wait for something to change and then retry, it may set
74 * ctx->try_again to true. (Only the "wait-until" command currently does
76 void (*run)(struct vsctl_context *ctx);
78 /* If nonnull, called after the transaction has been successfully
79 * committed. ctx->output is the output from the "run" function, which
80 * this function may modify and otherwise postprocess as needed. (Only the
81 * "create" command currently does any postprocessing.) */
82 void (*postprocess)(struct vsctl_context *ctx);
84 /* A comma-separated list of supported options, e.g. "--a,--b", or the
85 * empty string if the command does not support any options. */
87 enum { RO, RW } mode; /* Does this command modify the database? */
90 struct vsctl_command {
91 /* Data that remains constant after initialization. */
92 const struct vsctl_command_syntax *syntax;
97 /* Data modified by commands. */
102 /* --db: The database server to contact. */
103 static const char *db;
105 /* --oneline: Write each command's output as a single line? */
108 /* --dry-run: Do not commit any changes. */
111 /* --no-wait: Wait for ovs-vswitchd to reload its configuration? */
112 static bool wait_for_reload = true;
114 /* --timeout: Time to wait for a connection to 'db'. */
117 /* --retry: If true, ovs-vsctl will retry connecting to the database forever.
118 * If false and --db says to use an active connection method (e.g. "unix:",
119 * "tcp:", "ssl:"), then ovs-vsctl will try to connect once and exit with an
120 * error if the database server cannot be contacted (e.g. ovsdb-server is not
123 * Regardless of this setting, --timeout always limits how long ovs-vsctl will
127 /* Format for table output. */
128 static struct table_style table_style = TABLE_STYLE_DEFAULT;
130 /* All supported commands. */
131 static const struct vsctl_command_syntax all_commands[];
133 /* The IDL we're using and the current transaction, if any.
134 * This is for use by vsctl_exit() only, to allow it to clean up.
135 * Other code should use its context arguments. */
136 static struct ovsdb_idl *the_idl;
137 static struct ovsdb_idl_txn *the_idl_txn;
139 static void vsctl_exit(int status) NO_RETURN;
140 static void vsctl_fatal(const char *, ...) PRINTF_FORMAT(1, 2) NO_RETURN;
141 static char *default_db(void);
142 static void usage(void) NO_RETURN;
143 static void parse_options(int argc, char *argv[], struct shash *local_options);
144 static bool might_write_to_db(char **argv);
146 static struct vsctl_command *parse_commands(int argc, char *argv[],
147 struct shash *local_options,
148 size_t *n_commandsp);
149 static void parse_command(int argc, char *argv[], struct shash *local_options,
150 struct vsctl_command *);
151 static const struct vsctl_command_syntax *find_command(const char *name);
152 static void run_prerequisites(struct vsctl_command[], size_t n_commands,
154 static void do_vsctl(const char *args, struct vsctl_command *, size_t n,
157 static const struct vsctl_table_class *get_table(const char *table_name);
158 static void set_column(const struct vsctl_table_class *,
159 const struct ovsdb_idl_row *, const char *arg,
160 struct ovsdb_symbol_table *);
162 static bool is_condition_satisfied(const struct vsctl_table_class *,
163 const struct ovsdb_idl_row *,
165 struct ovsdb_symbol_table *);
168 main(int argc, char *argv[])
170 extern struct vlog_module VLM_reconnect;
171 struct ovsdb_idl *idl;
172 struct vsctl_command *commands;
173 struct shash local_options;
178 set_program_name(argv[0]);
179 signal(SIGPIPE, SIG_IGN);
180 vlog_set_levels(NULL, VLF_CONSOLE, VLL_WARN);
181 vlog_set_levels(&VLM_reconnect, VLF_ANY_FACILITY, VLL_WARN);
184 /* Log our arguments. This is often valuable for debugging systems. */
185 args = process_escape_args(argv);
186 VLOG(might_write_to_db(argv) ? VLL_INFO : VLL_DBG, "Called as %s", args);
188 /* Parse command line. */
189 shash_init(&local_options);
190 parse_options(argc, argv, &local_options);
191 commands = parse_commands(argc - optind, argv + optind, &local_options,
198 /* Initialize IDL. */
199 idl = the_idl = ovsdb_idl_create(db, &ovsrec_idl_class, false, retry);
200 run_prerequisites(commands, n_commands, idl);
202 /* Execute the commands.
204 * 'seqno' is the database sequence number for which we last tried to
205 * execute our transaction. There's no point in trying to commit more than
206 * once for any given sequence number, because if the transaction fails
207 * it's because the database changed and we need to obtain an up-to-date
208 * view of the database before we try the transaction again. */
209 seqno = ovsdb_idl_get_seqno(idl);
212 if (!ovsdb_idl_is_alive(idl)) {
213 int retval = ovsdb_idl_get_last_error(idl);
214 vsctl_fatal("%s: database connection failed (%s)",
215 db, ovs_retval_to_string(retval));
218 if (seqno != ovsdb_idl_get_seqno(idl)) {
219 seqno = ovsdb_idl_get_seqno(idl);
220 do_vsctl(args, commands, n_commands, idl);
223 if (seqno == ovsdb_idl_get_seqno(idl)) {
230 static struct option *
231 find_option(const char *name, struct option *options, size_t n_options)
235 for (i = 0; i < n_options; i++) {
236 if (!strcmp(options[i].name, name)) {
243 static struct option *
244 add_option(struct option **optionsp, size_t *n_optionsp,
245 size_t *allocated_optionsp)
247 if (*n_optionsp >= *allocated_optionsp) {
248 *optionsp = x2nrealloc(*optionsp, allocated_optionsp,
251 return &(*optionsp)[(*n_optionsp)++];
255 parse_options(int argc, char *argv[], struct shash *local_options)
258 OPT_DB = UCHAR_MAX + 1,
269 static const struct option global_long_options[] = {
270 {"db", required_argument, NULL, OPT_DB},
271 {"no-syslog", no_argument, NULL, OPT_NO_SYSLOG},
272 {"no-wait", no_argument, NULL, OPT_NO_WAIT},
273 {"dry-run", no_argument, NULL, OPT_DRY_RUN},
274 {"oneline", no_argument, NULL, OPT_ONELINE},
275 {"timeout", required_argument, NULL, 't'},
276 {"retry", no_argument, NULL, OPT_RETRY},
277 {"help", no_argument, NULL, 'h'},
278 {"version", no_argument, NULL, 'V'},
281 STREAM_SSL_LONG_OPTIONS,
282 {"peer-ca-cert", required_argument, NULL, OPT_PEER_CA_CERT},
285 const int n_global_long_options = ARRAY_SIZE(global_long_options) - 1;
286 char *tmp, *short_options;
288 const struct vsctl_command_syntax *p;
289 struct option *options, *o;
290 size_t allocated_options;
294 tmp = long_options_to_short_options(global_long_options);
295 short_options = xasprintf("+%s", tmp);
298 /* We want to parse both global and command-specific options here, but
299 * getopt_long() isn't too convenient for the job. We copy our global
300 * options into a dynamic array, then append all of the command-specific
302 options = xmemdup(global_long_options, sizeof global_long_options);
303 allocated_options = ARRAY_SIZE(global_long_options);
304 n_options = n_global_long_options;
305 for (p = all_commands; p->name; p++) {
307 char *save_ptr = NULL;
311 s = xstrdup(p->options);
312 for (name = strtok_r(s, ",", &save_ptr); name != NULL;
313 name = strtok_r(NULL, ",", &save_ptr)) {
317 ovs_assert(name[0] == '-' && name[1] == '-' && name[2]);
320 equals = strchr(name, '=');
322 has_arg = required_argument;
325 has_arg = no_argument;
328 o = find_option(name, options, n_options);
330 ovs_assert(o - options >= n_global_long_options);
331 ovs_assert(o->has_arg == has_arg);
333 o = add_option(&options, &n_options, &allocated_options);
334 o->name = xstrdup(name);
335 o->has_arg = has_arg;
344 o = add_option(&options, &n_options, &allocated_options);
345 memset(o, 0, sizeof *o);
347 table_style.format = TF_LIST;
353 c = getopt_long(argc, argv, short_options, options, &idx);
368 vlog_set_levels(&VLM_vsctl, VLF_SYSLOG, VLL_WARN);
372 wait_for_reload = false;
380 if (shash_find(local_options, options[idx].name)) {
381 vsctl_fatal("'%s' option specified multiple times",
384 shash_add_nocopy(local_options,
385 xasprintf("--%s", options[idx].name),
386 optarg ? xstrdup(optarg) : NULL);
393 ovs_print_version(0, 0);
397 timeout = strtoul(optarg, NULL, 10);
399 vsctl_fatal("value %s on -t or --timeout is invalid",
409 TABLE_OPTION_HANDLERS(&table_style)
411 STREAM_SSL_OPTION_HANDLERS
413 case OPT_PEER_CA_CERT:
414 stream_ssl_set_peer_ca_cert_file(optarg);
430 for (i = n_global_long_options; options[i].name; i++) {
431 free(CONST_CAST(char *, options[i].name));
436 static struct vsctl_command *
437 parse_commands(int argc, char *argv[], struct shash *local_options,
440 struct vsctl_command *commands;
441 size_t n_commands, allocated_commands;
445 n_commands = allocated_commands = 0;
447 for (start = i = 0; i <= argc; i++) {
448 if (i == argc || !strcmp(argv[i], "--")) {
450 if (n_commands >= allocated_commands) {
451 struct vsctl_command *c;
453 commands = x2nrealloc(commands, &allocated_commands,
455 for (c = commands; c < &commands[n_commands]; c++) {
456 shash_moved(&c->options);
459 parse_command(i - start, &argv[start], local_options,
460 &commands[n_commands++]);
461 } else if (!shash_is_empty(local_options)) {
462 vsctl_fatal("missing command name (use --help for help)");
468 vsctl_fatal("missing command name (use --help for help)");
470 *n_commandsp = n_commands;
475 parse_command(int argc, char *argv[], struct shash *local_options,
476 struct vsctl_command *command)
478 const struct vsctl_command_syntax *p;
479 struct shash_node *node;
483 shash_init(&command->options);
484 shash_swap(local_options, &command->options);
485 for (i = 0; i < argc; i++) {
486 const char *option = argv[i];
490 if (option[0] != '-') {
494 equals = strchr(option, '=');
496 key = xmemdup0(option, equals - option);
497 value = xstrdup(equals + 1);
499 key = xstrdup(option);
503 if (shash_find(&command->options, key)) {
504 vsctl_fatal("'%s' option specified multiple times", argv[i]);
506 shash_add_nocopy(&command->options, key, value);
509 vsctl_fatal("missing command name (use --help for help)");
512 p = find_command(argv[i]);
514 vsctl_fatal("unknown command '%s'; use --help for help", argv[i]);
517 SHASH_FOR_EACH (node, &command->options) {
518 const char *s = strstr(p->options, node->name);
519 int end = s ? s[strlen(node->name)] : EOF;
521 if (end != '=' && end != ',' && end != ' ' && end != '\0') {
522 vsctl_fatal("'%s' command has no '%s' option",
523 argv[i], node->name);
525 if ((end == '=') != (node->data != NULL)) {
527 vsctl_fatal("missing argument to '%s' option on '%s' "
528 "command", node->name, argv[i]);
530 vsctl_fatal("'%s' option on '%s' does not accept an "
531 "argument", node->name, argv[i]);
536 n_arg = argc - i - 1;
537 if (n_arg < p->min_args) {
538 vsctl_fatal("'%s' command requires at least %d arguments",
539 p->name, p->min_args);
540 } else if (n_arg > p->max_args) {
543 for (j = i + 1; j < argc; j++) {
544 if (argv[j][0] == '-') {
545 vsctl_fatal("'%s' command takes at most %d arguments "
546 "(note that options must precede command "
547 "names and follow a \"--\" argument)",
548 p->name, p->max_args);
552 vsctl_fatal("'%s' command takes at most %d arguments",
553 p->name, p->max_args);
557 command->argc = n_arg + 1;
558 command->argv = &argv[i];
561 /* Returns the "struct vsctl_command_syntax" for a given command 'name', or a
562 * null pointer if there is none. */
563 static const struct vsctl_command_syntax *
564 find_command(const char *name)
566 static struct shash commands = SHASH_INITIALIZER(&commands);
568 if (shash_is_empty(&commands)) {
569 const struct vsctl_command_syntax *p;
571 for (p = all_commands; p->name; p++) {
572 shash_add_assert(&commands, p->name, p);
576 return shash_find_data(&commands, name);
580 vsctl_fatal(const char *format, ...)
585 va_start(args, format);
586 message = xvasprintf(format, args);
589 vlog_set_levels(&VLM_vsctl, VLF_CONSOLE, VLL_OFF);
590 VLOG_ERR("%s", message);
591 ovs_error(0, "%s", message);
592 vsctl_exit(EXIT_FAILURE);
595 /* Frees the current transaction and the underlying IDL and then calls
598 * Freeing the transaction and the IDL is not strictly necessary, but it makes
599 * for a clean memory leak report from valgrind in the normal case. That makes
600 * it easier to notice real memory leaks. */
602 vsctl_exit(int status)
605 ovsdb_idl_txn_abort(the_idl_txn);
606 ovsdb_idl_txn_destroy(the_idl_txn);
608 ovsdb_idl_destroy(the_idl);
616 %s: ovs-vswitchd management utility\n\
617 usage: %s [OPTIONS] COMMAND [ARG...]\n\
619 Open vSwitch commands:\n\
620 init initialize database, if not yet initialized\n\
621 show print overview of database contents\n\
622 emer-reset reset configuration to clean state\n\
625 add-br BRIDGE create a new bridge named BRIDGE\n\
626 add-br BRIDGE PARENT VLAN create new fake BRIDGE in PARENT on VLAN\n\
627 del-br BRIDGE delete BRIDGE and all of its ports\n\
628 list-br print the names of all the bridges\n\
629 br-exists BRIDGE exit 2 if BRIDGE does not exist\n\
630 br-to-vlan BRIDGE print the VLAN which BRIDGE is on\n\
631 br-to-parent BRIDGE print the parent of BRIDGE\n\
632 br-set-external-id BRIDGE KEY VALUE set KEY on BRIDGE to VALUE\n\
633 br-set-external-id BRIDGE KEY unset KEY on BRIDGE\n\
634 br-get-external-id BRIDGE KEY print value of KEY on BRIDGE\n\
635 br-get-external-id BRIDGE list key-value pairs on BRIDGE\n\
637 Port commands (a bond is considered to be a single port):\n\
638 list-ports BRIDGE print the names of all the ports on BRIDGE\n\
639 add-port BRIDGE PORT add network device PORT to BRIDGE\n\
640 add-bond BRIDGE PORT IFACE... add bonded port PORT in BRIDGE from IFACES\n\
641 del-port [BRIDGE] PORT delete PORT (which may be bonded) from BRIDGE\n\
642 port-to-br PORT print name of bridge that contains PORT\n\
644 Interface commands (a bond consists of multiple interfaces):\n\
645 list-ifaces BRIDGE print the names of all interfaces on BRIDGE\n\
646 iface-to-br IFACE print name of bridge that contains IFACE\n\
648 Controller commands:\n\
649 get-controller BRIDGE print the controllers for BRIDGE\n\
650 del-controller BRIDGE delete the controllers for BRIDGE\n\
651 set-controller BRIDGE TARGET... set the controllers for BRIDGE\n\
652 get-fail-mode BRIDGE print the fail-mode for BRIDGE\n\
653 del-fail-mode BRIDGE delete the fail-mode for BRIDGE\n\
654 set-fail-mode BRIDGE MODE set the fail-mode for BRIDGE to MODE\n\
657 get-manager print the managers\n\
658 del-manager delete the managers\n\
659 set-manager TARGET... set the list of managers to TARGET...\n\
662 get-ssl print the SSL configuration\n\
663 del-ssl delete the SSL configuration\n\
664 set-ssl PRIV-KEY CERT CA-CERT set the SSL configuration\n\
667 emer-reset reset switch to known good state\n\
669 Database commands:\n\
670 list TBL [REC] list RECord (or all records) in TBL\n\
671 find TBL CONDITION... list records satisfying CONDITION in TBL\n\
672 get TBL REC COL[:KEY] print values of COLumns in RECord in TBL\n\
673 set TBL REC COL[:KEY]=VALUE set COLumn values in RECord in TBL\n\
674 add TBL REC COL [KEY=]VALUE add (KEY=)VALUE to COLumn in RECord in TBL\n\
675 remove TBL REC COL [KEY=]VALUE remove (KEY=)VALUE from COLumn\n\
676 clear TBL REC COL clear values from COLumn in RECord in TBL\n\
677 create TBL COL[:KEY]=VALUE create and initialize new record\n\
678 destroy TBL REC delete RECord from TBL\n\
679 wait-until TBL REC [COL[:KEY]=VALUE] wait until condition is true\n\
680 Potentially unsafe database commands require --force option.\n\
683 --db=DATABASE connect to DATABASE\n\
685 --no-wait do not wait for ovs-vswitchd to reconfigure\n\
686 --retry keep trying to connect to server forever\n\
687 -t, --timeout=SECS wait at most SECS seconds for ovs-vswitchd\n\
688 --dry-run do not commit changes to database\n\
689 --oneline print exactly one line of output per command\n",
690 program_name, program_name, default_db());
693 --no-syslog equivalent to --verbose=vsctl:syslog:warn\n");
694 stream_usage("database", true, true, false);
697 -h, --help display this help message\n\
698 -V, --version display version information\n");
707 def = xasprintf("unix:%s/db.sock", ovs_rundir());
712 /* Returns true if it looks like this set of arguments might modify the
713 * database, otherwise false. (Not very smart, so it's prone to false
716 might_write_to_db(char **argv)
718 for (; *argv; argv++) {
719 const struct vsctl_command_syntax *p = find_command(*argv);
720 if (p && p->mode == RW) {
727 struct vsctl_context {
731 struct shash options;
733 /* Modifiable state. */
736 struct ovsdb_idl *idl;
737 struct ovsdb_idl_txn *txn;
738 struct ovsdb_symbol_table *symtab;
739 const struct ovsrec_open_vswitch *ovs;
742 /* A cache of the contents of the database.
744 * A command that needs to use any of this information must first call
745 * vsctl_context_populate_cache(). A command that changes anything that
746 * could invalidate the cache must either call
747 * vsctl_context_invalidate_cache() or manually update the cache to
748 * maintain its correctness. */
750 struct shash bridges; /* Maps from bridge name to struct vsctl_bridge. */
751 struct shash ports; /* Maps from port name to struct vsctl_port. */
752 struct shash ifaces; /* Maps from port name to struct vsctl_iface. */
754 /* A command may set this member to true if some prerequisite is not met
755 * and the caller should wait for something to change and then retry. */
759 struct vsctl_bridge {
760 struct ovsrec_bridge *br_cfg;
762 struct list ports; /* Contains "struct vsctl_port"s. */
764 /* VLAN ("fake") bridge support.
766 * Use 'parent != NULL' to detect a fake bridge, because 'vlan' can be 0
768 struct hmap children; /* VLAN bridges indexed by 'vlan'. */
769 struct hmap_node children_node; /* Node in parent's 'children' hmap. */
770 struct vsctl_bridge *parent; /* Real bridge, or NULL. */
771 int vlan; /* VLAN VID (0...4095), or 0. */
775 struct list ports_node; /* In struct vsctl_bridge's 'ports' list. */
776 struct list ifaces; /* Contains "struct vsctl_iface"s. */
777 struct ovsrec_port *port_cfg;
778 struct vsctl_bridge *bridge;
782 struct list ifaces_node; /* In struct vsctl_port's 'ifaces' list. */
783 struct ovsrec_interface *iface_cfg;
784 struct vsctl_port *port;
788 vsctl_context_to_string(const struct vsctl_context *ctx)
790 const struct shash_node *node;
796 SHASH_FOR_EACH (node, &ctx->options) {
797 svec_add(&words, node->name);
799 for (i = 0; i < ctx->argc; i++) {
800 svec_add(&words, ctx->argv[i]);
802 svec_terminate(&words);
804 s = process_escape_args(words.names);
806 svec_destroy(&words);
812 verify_ports(struct vsctl_context *ctx)
814 if (!ctx->verified_ports) {
815 const struct ovsrec_bridge *bridge;
816 const struct ovsrec_port *port;
818 ovsrec_open_vswitch_verify_bridges(ctx->ovs);
819 OVSREC_BRIDGE_FOR_EACH (bridge, ctx->idl) {
820 ovsrec_bridge_verify_ports(bridge);
822 OVSREC_PORT_FOR_EACH (port, ctx->idl) {
823 ovsrec_port_verify_interfaces(port);
826 ctx->verified_ports = true;
830 static struct vsctl_bridge *
831 add_bridge_to_cache(struct vsctl_context *ctx,
832 struct ovsrec_bridge *br_cfg, const char *name,
833 struct vsctl_bridge *parent, int vlan)
835 struct vsctl_bridge *br = xmalloc(sizeof *br);
837 br->name = xstrdup(name);
838 list_init(&br->ports);
841 hmap_init(&br->children);
843 hmap_insert(&parent->children, &br->children_node, hash_int(vlan, 0));
845 shash_add(&ctx->bridges, br->name, br);
850 ovs_delete_bridge(const struct ovsrec_open_vswitch *ovs,
851 struct ovsrec_bridge *bridge)
853 struct ovsrec_bridge **bridges;
856 bridges = xmalloc(sizeof *ovs->bridges * ovs->n_bridges);
857 for (i = n = 0; i < ovs->n_bridges; i++) {
858 if (ovs->bridges[i] != bridge) {
859 bridges[n++] = ovs->bridges[i];
862 ovsrec_open_vswitch_set_bridges(ovs, bridges, n);
867 del_cached_bridge(struct vsctl_context *ctx, struct vsctl_bridge *br)
869 ovs_assert(list_is_empty(&br->ports));
870 ovs_assert(hmap_is_empty(&br->children));
872 hmap_remove(&br->parent->children, &br->children_node);
875 ovsrec_bridge_delete(br->br_cfg);
876 ovs_delete_bridge(ctx->ovs, br->br_cfg);
878 shash_find_and_delete(&ctx->bridges, br->name);
879 hmap_destroy(&br->children);
885 port_is_fake_bridge(const struct ovsrec_port *port_cfg)
887 return (port_cfg->fake_bridge
889 && *port_cfg->tag >= 0 && *port_cfg->tag <= 4095);
892 static struct vsctl_bridge *
893 find_vlan_bridge(struct vsctl_bridge *parent, int vlan)
895 struct vsctl_bridge *child;
897 HMAP_FOR_EACH_IN_BUCKET (child, children_node, hash_int(vlan, 0),
899 if (child->vlan == vlan) {
907 static struct vsctl_port *
908 add_port_to_cache(struct vsctl_context *ctx, struct vsctl_bridge *parent,
909 struct ovsrec_port *port_cfg)
911 struct vsctl_port *port;
914 && *port_cfg->tag >= 0 && *port_cfg->tag <= 4095) {
915 struct vsctl_bridge *vlan_bridge;
917 vlan_bridge = find_vlan_bridge(parent, *port_cfg->tag);
919 parent = vlan_bridge;
923 port = xmalloc(sizeof *port);
924 list_push_back(&parent->ports, &port->ports_node);
925 list_init(&port->ifaces);
926 port->port_cfg = port_cfg;
927 port->bridge = parent;
928 shash_add(&ctx->ports, port_cfg->name, port);
934 del_cached_port(struct vsctl_context *ctx, struct vsctl_port *port)
936 ovs_assert(list_is_empty(&port->ifaces));
937 list_remove(&port->ports_node);
938 shash_find_and_delete(&ctx->ports, port->port_cfg->name);
939 ovsrec_port_delete(port->port_cfg);
943 static struct vsctl_iface *
944 add_iface_to_cache(struct vsctl_context *ctx, struct vsctl_port *parent,
945 struct ovsrec_interface *iface_cfg)
947 struct vsctl_iface *iface;
949 iface = xmalloc(sizeof *iface);
950 list_push_back(&parent->ifaces, &iface->ifaces_node);
951 iface->iface_cfg = iface_cfg;
952 iface->port = parent;
953 shash_add(&ctx->ifaces, iface_cfg->name, iface);
959 del_cached_iface(struct vsctl_context *ctx, struct vsctl_iface *iface)
961 list_remove(&iface->ifaces_node);
962 shash_find_and_delete(&ctx->ifaces, iface->iface_cfg->name);
963 ovsrec_interface_delete(iface->iface_cfg);
968 vsctl_context_invalidate_cache(struct vsctl_context *ctx)
970 struct shash_node *node;
972 if (!ctx->cache_valid) {
975 ctx->cache_valid = false;
977 SHASH_FOR_EACH (node, &ctx->bridges) {
978 struct vsctl_bridge *bridge = node->data;
979 hmap_destroy(&bridge->children);
983 shash_destroy(&ctx->bridges);
985 shash_destroy_free_data(&ctx->ports);
986 shash_destroy_free_data(&ctx->ifaces);
990 pre_get_info(struct vsctl_context *ctx)
992 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_bridges);
994 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_name);
995 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_controller);
996 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_fail_mode);
997 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_ports);
999 ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_name);
1000 ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_fake_bridge);
1001 ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_tag);
1002 ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_interfaces);
1004 ovsdb_idl_add_column(ctx->idl, &ovsrec_interface_col_name);
1008 vsctl_context_populate_cache(struct vsctl_context *ctx)
1010 const struct ovsrec_open_vswitch *ovs = ctx->ovs;
1011 struct sset bridges, ports;
1014 if (ctx->cache_valid) {
1015 /* Cache is already populated. */
1018 ctx->cache_valid = true;
1019 shash_init(&ctx->bridges);
1020 shash_init(&ctx->ports);
1021 shash_init(&ctx->ifaces);
1023 sset_init(&bridges);
1025 for (i = 0; i < ovs->n_bridges; i++) {
1026 struct ovsrec_bridge *br_cfg = ovs->bridges[i];
1027 struct vsctl_bridge *br;
1030 if (!sset_add(&bridges, br_cfg->name)) {
1031 VLOG_WARN("%s: database contains duplicate bridge name",
1035 br = add_bridge_to_cache(ctx, br_cfg, br_cfg->name, NULL, 0);
1040 for (j = 0; j < br_cfg->n_ports; j++) {
1041 struct ovsrec_port *port_cfg = br_cfg->ports[j];
1043 if (!sset_add(&ports, port_cfg->name)) {
1044 /* Duplicate port name. (We will warn about that later.) */
1048 if (port_is_fake_bridge(port_cfg)
1049 && sset_add(&bridges, port_cfg->name)) {
1050 add_bridge_to_cache(ctx, NULL, port_cfg->name, br,
1055 sset_destroy(&bridges);
1056 sset_destroy(&ports);
1058 sset_init(&bridges);
1059 for (i = 0; i < ovs->n_bridges; i++) {
1060 struct ovsrec_bridge *br_cfg = ovs->bridges[i];
1061 struct vsctl_bridge *br;
1064 if (!sset_add(&bridges, br_cfg->name)) {
1067 br = shash_find_data(&ctx->bridges, br_cfg->name);
1068 for (j = 0; j < br_cfg->n_ports; j++) {
1069 struct ovsrec_port *port_cfg = br_cfg->ports[j];
1070 struct vsctl_port *port;
1073 port = shash_find_data(&ctx->ports, port_cfg->name);
1075 if (port_cfg == port->port_cfg) {
1076 VLOG_WARN("%s: port is in multiple bridges (%s and %s)",
1077 port_cfg->name, br->name, port->bridge->name);
1079 /* Log as an error because this violates the database's
1080 * uniqueness constraints, so the database server shouldn't
1081 * have allowed it. */
1082 VLOG_ERR("%s: database contains duplicate port name",
1088 if (port_is_fake_bridge(port_cfg)
1089 && !sset_add(&bridges, port_cfg->name)) {
1093 port = add_port_to_cache(ctx, br, port_cfg);
1094 for (k = 0; k < port_cfg->n_interfaces; k++) {
1095 struct ovsrec_interface *iface_cfg = port_cfg->interfaces[k];
1096 struct vsctl_iface *iface;
1098 iface = shash_find_data(&ctx->ifaces, iface_cfg->name);
1100 if (iface_cfg == iface->iface_cfg) {
1101 VLOG_WARN("%s: interface is in multiple ports "
1104 iface->port->port_cfg->name,
1105 port->port_cfg->name);
1107 /* Log as an error because this violates the database's
1108 * uniqueness constraints, so the database server
1109 * shouldn't have allowed it. */
1110 VLOG_ERR("%s: database contains duplicate interface "
1111 "name", iface_cfg->name);
1116 add_iface_to_cache(ctx, port, iface_cfg);
1120 sset_destroy(&bridges);
1124 check_conflicts(struct vsctl_context *ctx, const char *name,
1127 struct vsctl_iface *iface;
1128 struct vsctl_port *port;
1132 if (shash_find(&ctx->bridges, name)) {
1133 vsctl_fatal("%s because a bridge named %s already exists",
1137 port = shash_find_data(&ctx->ports, name);
1139 vsctl_fatal("%s because a port named %s already exists on "
1140 "bridge %s", msg, name, port->bridge->name);
1143 iface = shash_find_data(&ctx->ifaces, name);
1145 vsctl_fatal("%s because an interface named %s already exists "
1146 "on bridge %s", msg, name, iface->port->bridge->name);
1152 static struct vsctl_bridge *
1153 find_bridge(struct vsctl_context *ctx, const char *name, bool must_exist)
1155 struct vsctl_bridge *br;
1157 ovs_assert(ctx->cache_valid);
1159 br = shash_find_data(&ctx->bridges, name);
1160 if (must_exist && !br) {
1161 vsctl_fatal("no bridge named %s", name);
1163 ovsrec_open_vswitch_verify_bridges(ctx->ovs);
1167 static struct vsctl_bridge *
1168 find_real_bridge(struct vsctl_context *ctx, const char *name, bool must_exist)
1170 struct vsctl_bridge *br = find_bridge(ctx, name, must_exist);
1171 if (br && br->parent) {
1172 vsctl_fatal("%s is a fake bridge", name);
1177 static struct vsctl_port *
1178 find_port(struct vsctl_context *ctx, const char *name, bool must_exist)
1180 struct vsctl_port *port;
1182 ovs_assert(ctx->cache_valid);
1184 port = shash_find_data(&ctx->ports, name);
1185 if (port && !strcmp(name, port->bridge->name)) {
1188 if (must_exist && !port) {
1189 vsctl_fatal("no port named %s", name);
1195 static struct vsctl_iface *
1196 find_iface(struct vsctl_context *ctx, const char *name, bool must_exist)
1198 struct vsctl_iface *iface;
1200 ovs_assert(ctx->cache_valid);
1202 iface = shash_find_data(&ctx->ifaces, name);
1203 if (iface && !strcmp(name, iface->port->bridge->name)) {
1206 if (must_exist && !iface) {
1207 vsctl_fatal("no interface named %s", name);
1214 bridge_insert_port(struct ovsrec_bridge *br, struct ovsrec_port *port)
1216 struct ovsrec_port **ports;
1219 ports = xmalloc(sizeof *br->ports * (br->n_ports + 1));
1220 for (i = 0; i < br->n_ports; i++) {
1221 ports[i] = br->ports[i];
1223 ports[br->n_ports] = port;
1224 ovsrec_bridge_set_ports(br, ports, br->n_ports + 1);
1229 bridge_delete_port(struct ovsrec_bridge *br, struct ovsrec_port *port)
1231 struct ovsrec_port **ports;
1234 ports = xmalloc(sizeof *br->ports * br->n_ports);
1235 for (i = n = 0; i < br->n_ports; i++) {
1236 if (br->ports[i] != port) {
1237 ports[n++] = br->ports[i];
1240 ovsrec_bridge_set_ports(br, ports, n);
1245 ovs_insert_bridge(const struct ovsrec_open_vswitch *ovs,
1246 struct ovsrec_bridge *bridge)
1248 struct ovsrec_bridge **bridges;
1251 bridges = xmalloc(sizeof *ovs->bridges * (ovs->n_bridges + 1));
1252 for (i = 0; i < ovs->n_bridges; i++) {
1253 bridges[i] = ovs->bridges[i];
1255 bridges[ovs->n_bridges] = bridge;
1256 ovsrec_open_vswitch_set_bridges(ovs, bridges, ovs->n_bridges + 1);
1261 cmd_init(struct vsctl_context *ctx OVS_UNUSED)
1265 struct cmd_show_table {
1266 const struct ovsdb_idl_table_class *table;
1267 const struct ovsdb_idl_column *name_column;
1268 const struct ovsdb_idl_column *columns[3];
1272 static struct cmd_show_table cmd_show_tables[] = {
1273 {&ovsrec_table_open_vswitch,
1275 {&ovsrec_open_vswitch_col_manager_options,
1276 &ovsrec_open_vswitch_col_bridges,
1277 &ovsrec_open_vswitch_col_ovs_version},
1280 {&ovsrec_table_bridge,
1281 &ovsrec_bridge_col_name,
1282 {&ovsrec_bridge_col_controller,
1283 &ovsrec_bridge_col_fail_mode,
1284 &ovsrec_bridge_col_ports},
1287 {&ovsrec_table_port,
1288 &ovsrec_port_col_name,
1289 {&ovsrec_port_col_tag,
1290 &ovsrec_port_col_trunks,
1291 &ovsrec_port_col_interfaces},
1294 {&ovsrec_table_interface,
1295 &ovsrec_interface_col_name,
1296 {&ovsrec_interface_col_type,
1297 &ovsrec_interface_col_options,
1301 {&ovsrec_table_controller,
1302 &ovsrec_controller_col_target,
1303 {&ovsrec_controller_col_is_connected,
1308 {&ovsrec_table_manager,
1309 &ovsrec_manager_col_target,
1310 {&ovsrec_manager_col_is_connected,
1317 pre_cmd_show(struct vsctl_context *ctx)
1319 struct cmd_show_table *show;
1321 for (show = cmd_show_tables;
1322 show < &cmd_show_tables[ARRAY_SIZE(cmd_show_tables)];
1326 ovsdb_idl_add_table(ctx->idl, show->table);
1327 if (show->name_column) {
1328 ovsdb_idl_add_column(ctx->idl, show->name_column);
1330 for (i = 0; i < ARRAY_SIZE(show->columns); i++) {
1331 const struct ovsdb_idl_column *column = show->columns[i];
1333 ovsdb_idl_add_column(ctx->idl, column);
1339 static struct cmd_show_table *
1340 cmd_show_find_table_by_row(const struct ovsdb_idl_row *row)
1342 struct cmd_show_table *show;
1344 for (show = cmd_show_tables;
1345 show < &cmd_show_tables[ARRAY_SIZE(cmd_show_tables)];
1347 if (show->table == row->table->class) {
1354 static struct cmd_show_table *
1355 cmd_show_find_table_by_name(const char *name)
1357 struct cmd_show_table *show;
1359 for (show = cmd_show_tables;
1360 show < &cmd_show_tables[ARRAY_SIZE(cmd_show_tables)];
1362 if (!strcmp(show->table->name, name)) {
1370 cmd_show_row(struct vsctl_context *ctx, const struct ovsdb_idl_row *row,
1373 struct cmd_show_table *show = cmd_show_find_table_by_row(row);
1376 ds_put_char_multiple(&ctx->output, ' ', level * 4);
1377 if (show && show->name_column) {
1378 const struct ovsdb_datum *datum;
1380 ds_put_format(&ctx->output, "%s ", show->table->name);
1381 datum = ovsdb_idl_read(row, show->name_column);
1382 ovsdb_datum_to_string(datum, &show->name_column->type, &ctx->output);
1384 ds_put_format(&ctx->output, UUID_FMT, UUID_ARGS(&row->uuid));
1386 ds_put_char(&ctx->output, '\n');
1388 if (!show || show->recurse) {
1392 show->recurse = true;
1393 for (i = 0; i < ARRAY_SIZE(show->columns); i++) {
1394 const struct ovsdb_idl_column *column = show->columns[i];
1395 const struct ovsdb_datum *datum;
1401 datum = ovsdb_idl_read(row, column);
1402 if (column->type.key.type == OVSDB_TYPE_UUID &&
1403 column->type.key.u.uuid.refTableName) {
1404 struct cmd_show_table *ref_show;
1407 ref_show = cmd_show_find_table_by_name(
1408 column->type.key.u.uuid.refTableName);
1410 for (j = 0; j < datum->n; j++) {
1411 const struct ovsdb_idl_row *ref_row;
1413 ref_row = ovsdb_idl_get_row_for_uuid(ctx->idl,
1415 &datum->keys[j].uuid);
1417 cmd_show_row(ctx, ref_row, level + 1);
1424 if (!ovsdb_datum_is_default(datum, &column->type)) {
1425 ds_put_char_multiple(&ctx->output, ' ', (level + 1) * 4);
1426 ds_put_format(&ctx->output, "%s: ", column->name);
1427 ovsdb_datum_to_string(datum, &column->type, &ctx->output);
1428 ds_put_char(&ctx->output, '\n');
1431 show->recurse = false;
1435 cmd_show(struct vsctl_context *ctx)
1437 const struct ovsdb_idl_row *row;
1439 for (row = ovsdb_idl_first_row(ctx->idl, cmd_show_tables[0].table);
1440 row; row = ovsdb_idl_next_row(row)) {
1441 cmd_show_row(ctx, row, 0);
1446 pre_cmd_emer_reset(struct vsctl_context *ctx)
1448 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_manager_options);
1449 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_ssl);
1451 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_controller);
1452 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_fail_mode);
1453 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_mirrors);
1454 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_netflow);
1455 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_sflow);
1456 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_flood_vlans);
1457 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_other_config);
1459 ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_other_config);
1461 ovsdb_idl_add_column(ctx->idl,
1462 &ovsrec_interface_col_ingress_policing_rate);
1463 ovsdb_idl_add_column(ctx->idl,
1464 &ovsrec_interface_col_ingress_policing_burst);
1468 cmd_emer_reset(struct vsctl_context *ctx)
1470 const struct ovsdb_idl *idl = ctx->idl;
1471 const struct ovsrec_bridge *br;
1472 const struct ovsrec_port *port;
1473 const struct ovsrec_interface *iface;
1474 const struct ovsrec_mirror *mirror, *next_mirror;
1475 const struct ovsrec_controller *ctrl, *next_ctrl;
1476 const struct ovsrec_manager *mgr, *next_mgr;
1477 const struct ovsrec_netflow *nf, *next_nf;
1478 const struct ovsrec_ssl *ssl, *next_ssl;
1479 const struct ovsrec_sflow *sflow, *next_sflow;
1481 /* Reset the Open_vSwitch table. */
1482 ovsrec_open_vswitch_set_manager_options(ctx->ovs, NULL, 0);
1483 ovsrec_open_vswitch_set_ssl(ctx->ovs, NULL);
1485 OVSREC_BRIDGE_FOR_EACH (br, idl) {
1488 ovsrec_bridge_set_controller(br, NULL, 0);
1489 ovsrec_bridge_set_fail_mode(br, NULL);
1490 ovsrec_bridge_set_mirrors(br, NULL, 0);
1491 ovsrec_bridge_set_netflow(br, NULL);
1492 ovsrec_bridge_set_sflow(br, NULL);
1493 ovsrec_bridge_set_flood_vlans(br, NULL, 0);
1495 /* We only want to save the "hwaddr" key from other_config. */
1496 hwaddr = smap_get(&br->other_config, "hwaddr");
1498 struct smap smap = SMAP_INITIALIZER(&smap);
1499 smap_add(&smap, "hwaddr", hwaddr);
1500 ovsrec_bridge_set_other_config(br, &smap);
1501 smap_destroy(&smap);
1503 ovsrec_bridge_set_other_config(br, NULL);
1507 OVSREC_PORT_FOR_EACH (port, idl) {
1508 ovsrec_port_set_other_config(port, NULL);
1511 OVSREC_INTERFACE_FOR_EACH (iface, idl) {
1512 /* xxx What do we do about gre/patch devices created by mgr? */
1514 ovsrec_interface_set_ingress_policing_rate(iface, 0);
1515 ovsrec_interface_set_ingress_policing_burst(iface, 0);
1518 OVSREC_MIRROR_FOR_EACH_SAFE (mirror, next_mirror, idl) {
1519 ovsrec_mirror_delete(mirror);
1522 OVSREC_CONTROLLER_FOR_EACH_SAFE (ctrl, next_ctrl, idl) {
1523 ovsrec_controller_delete(ctrl);
1526 OVSREC_MANAGER_FOR_EACH_SAFE (mgr, next_mgr, idl) {
1527 ovsrec_manager_delete(mgr);
1530 OVSREC_NETFLOW_FOR_EACH_SAFE (nf, next_nf, idl) {
1531 ovsrec_netflow_delete(nf);
1534 OVSREC_SSL_FOR_EACH_SAFE (ssl, next_ssl, idl) {
1535 ovsrec_ssl_delete(ssl);
1538 OVSREC_SFLOW_FOR_EACH_SAFE (sflow, next_sflow, idl) {
1539 ovsrec_sflow_delete(sflow);
1542 vsctl_context_invalidate_cache(ctx);
1546 cmd_add_br(struct vsctl_context *ctx)
1548 bool may_exist = shash_find(&ctx->options, "--may-exist") != NULL;
1549 const char *br_name, *parent_name;
1552 br_name = ctx->argv[1];
1553 if (ctx->argc == 2) {
1556 } else if (ctx->argc == 4) {
1557 parent_name = ctx->argv[2];
1558 vlan = atoi(ctx->argv[3]);
1559 if (vlan < 0 || vlan > 4095) {
1560 vsctl_fatal("%s: vlan must be between 0 and 4095", ctx->argv[0]);
1563 vsctl_fatal("'%s' command takes exactly 1 or 3 arguments",
1567 vsctl_context_populate_cache(ctx);
1569 struct vsctl_bridge *br;
1571 br = find_bridge(ctx, br_name, false);
1575 vsctl_fatal("\"--may-exist add-br %s\" but %s is "
1576 "a VLAN bridge for VLAN %d",
1577 br_name, br_name, br->vlan);
1581 vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
1582 "is not a VLAN bridge",
1583 br_name, parent_name, vlan, br_name);
1584 } else if (strcmp(br->parent->name, parent_name)) {
1585 vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
1586 "has the wrong parent %s",
1587 br_name, parent_name, vlan,
1588 br_name, br->parent->name);
1589 } else if (br->vlan != vlan) {
1590 vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
1591 "is a VLAN bridge for the wrong VLAN %d",
1592 br_name, parent_name, vlan, br_name, br->vlan);
1598 check_conflicts(ctx, br_name,
1599 xasprintf("cannot create a bridge named %s", br_name));
1602 struct ovsrec_port *port;
1603 struct ovsrec_interface *iface;
1604 struct ovsrec_bridge *br;
1606 iface = ovsrec_interface_insert(ctx->txn);
1607 ovsrec_interface_set_name(iface, br_name);
1608 ovsrec_interface_set_type(iface, "internal");
1610 port = ovsrec_port_insert(ctx->txn);
1611 ovsrec_port_set_name(port, br_name);
1612 ovsrec_port_set_interfaces(port, &iface, 1);
1614 br = ovsrec_bridge_insert(ctx->txn);
1615 ovsrec_bridge_set_name(br, br_name);
1616 ovsrec_bridge_set_ports(br, &port, 1);
1618 ovs_insert_bridge(ctx->ovs, br);
1620 struct vsctl_bridge *parent;
1621 struct ovsrec_port *port;
1622 struct ovsrec_interface *iface;
1623 struct ovsrec_bridge *br;
1626 parent = find_bridge(ctx, parent_name, false);
1627 if (parent && parent->parent) {
1628 vsctl_fatal("cannot create bridge with fake bridge as parent");
1631 vsctl_fatal("parent bridge %s does not exist", parent_name);
1633 br = parent->br_cfg;
1635 iface = ovsrec_interface_insert(ctx->txn);
1636 ovsrec_interface_set_name(iface, br_name);
1637 ovsrec_interface_set_type(iface, "internal");
1639 port = ovsrec_port_insert(ctx->txn);
1640 ovsrec_port_set_name(port, br_name);
1641 ovsrec_port_set_interfaces(port, &iface, 1);
1642 ovsrec_port_set_fake_bridge(port, true);
1643 ovsrec_port_set_tag(port, &tag, 1);
1645 bridge_insert_port(br, port);
1648 vsctl_context_invalidate_cache(ctx);
1652 del_port(struct vsctl_context *ctx, struct vsctl_port *port)
1654 struct vsctl_iface *iface, *next_iface;
1656 bridge_delete_port((port->bridge->parent
1657 ? port->bridge->parent->br_cfg
1658 : port->bridge->br_cfg), port->port_cfg);
1660 LIST_FOR_EACH_SAFE (iface, next_iface, ifaces_node, &port->ifaces) {
1661 del_cached_iface(ctx, iface);
1663 del_cached_port(ctx, port);
1667 del_bridge(struct vsctl_context *ctx, struct vsctl_bridge *br)
1669 struct vsctl_bridge *child, *next_child;
1670 struct vsctl_port *port, *next_port;
1672 HMAP_FOR_EACH_SAFE (child, next_child, children_node, &br->children) {
1673 del_bridge(ctx, child);
1676 LIST_FOR_EACH_SAFE (port, next_port, ports_node, &br->ports) {
1677 del_port(ctx, port);
1680 del_cached_bridge(ctx, br);
1684 cmd_del_br(struct vsctl_context *ctx)
1686 bool must_exist = !shash_find(&ctx->options, "--if-exists");
1687 struct vsctl_bridge *bridge;
1689 vsctl_context_populate_cache(ctx);
1690 bridge = find_bridge(ctx, ctx->argv[1], must_exist);
1692 del_bridge(ctx, bridge);
1697 output_sorted(struct svec *svec, struct ds *output)
1703 SVEC_FOR_EACH (i, name, svec) {
1704 ds_put_format(output, "%s\n", name);
1709 cmd_list_br(struct vsctl_context *ctx)
1711 struct shash_node *node;
1712 struct svec bridges;
1713 bool real = shash_find(&ctx->options, "--real");
1714 bool fake = shash_find(&ctx->options, "--fake");
1716 /* If neither fake nor real were requested, return both. */
1717 if (!real && !fake) {
1721 vsctl_context_populate_cache(ctx);
1723 svec_init(&bridges);
1724 SHASH_FOR_EACH (node, &ctx->bridges) {
1725 struct vsctl_bridge *br = node->data;
1727 if (br->parent ? fake : real) {
1728 svec_add(&bridges, br->name);
1731 output_sorted(&bridges, &ctx->output);
1732 svec_destroy(&bridges);
1736 cmd_br_exists(struct vsctl_context *ctx)
1738 vsctl_context_populate_cache(ctx);
1739 if (!find_bridge(ctx, ctx->argv[1], false)) {
1745 set_external_id(struct smap *old, struct smap *new,
1746 char *key, char *value)
1748 smap_clone(new, old);
1751 smap_replace(new, key, value);
1753 smap_remove(new, key);
1758 pre_cmd_br_set_external_id(struct vsctl_context *ctx)
1761 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_external_ids);
1762 ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_external_ids);
1766 cmd_br_set_external_id(struct vsctl_context *ctx)
1768 struct vsctl_bridge *bridge;
1771 vsctl_context_populate_cache(ctx);
1772 bridge = find_bridge(ctx, ctx->argv[1], true);
1773 if (bridge->br_cfg) {
1775 set_external_id(&bridge->br_cfg->external_ids, &new, ctx->argv[2],
1776 ctx->argc >= 4 ? ctx->argv[3] : NULL);
1777 ovsrec_bridge_verify_external_ids(bridge->br_cfg);
1778 ovsrec_bridge_set_external_ids(bridge->br_cfg, &new);
1780 char *key = xasprintf("fake-bridge-%s", ctx->argv[2]);
1781 struct vsctl_port *port = shash_find_data(&ctx->ports, ctx->argv[1]);
1782 set_external_id(&port->port_cfg->external_ids, &new,
1783 key, ctx->argc >= 4 ? ctx->argv[3] : NULL);
1784 ovsrec_port_verify_external_ids(port->port_cfg);
1785 ovsrec_port_set_external_ids(port->port_cfg, &new);
1792 get_external_id(struct smap *smap, const char *prefix, const char *key,
1796 char *prefix_key = xasprintf("%s%s", prefix, key);
1797 const char *value = smap_get(smap, prefix_key);
1800 ds_put_format(output, "%s\n", value);
1804 const struct smap_node **sorted = smap_sort(smap);
1805 size_t prefix_len = strlen(prefix);
1808 for (i = 0; i < smap_count(smap); i++) {
1809 const struct smap_node *node = sorted[i];
1810 if (!strncmp(node->key, prefix, prefix_len)) {
1811 ds_put_format(output, "%s=%s\n", node->key + prefix_len,
1820 pre_cmd_br_get_external_id(struct vsctl_context *ctx)
1822 pre_cmd_br_set_external_id(ctx);
1826 cmd_br_get_external_id(struct vsctl_context *ctx)
1828 struct vsctl_bridge *bridge;
1830 vsctl_context_populate_cache(ctx);
1832 bridge = find_bridge(ctx, ctx->argv[1], true);
1833 if (bridge->br_cfg) {
1834 ovsrec_bridge_verify_external_ids(bridge->br_cfg);
1835 get_external_id(&bridge->br_cfg->external_ids, "",
1836 ctx->argc >= 3 ? ctx->argv[2] : NULL, &ctx->output);
1838 struct vsctl_port *port = shash_find_data(&ctx->ports, ctx->argv[1]);
1839 ovsrec_port_verify_external_ids(port->port_cfg);
1840 get_external_id(&port->port_cfg->external_ids, "fake-bridge-",
1841 ctx->argc >= 3 ? ctx->argv[2] : NULL, &ctx->output);
1846 cmd_list_ports(struct vsctl_context *ctx)
1848 struct vsctl_bridge *br;
1849 struct vsctl_port *port;
1852 vsctl_context_populate_cache(ctx);
1853 br = find_bridge(ctx, ctx->argv[1], true);
1854 ovsrec_bridge_verify_ports(br->br_cfg ? br->br_cfg : br->parent->br_cfg);
1857 LIST_FOR_EACH (port, ports_node, &br->ports) {
1858 if (strcmp(port->port_cfg->name, br->name)) {
1859 svec_add(&ports, port->port_cfg->name);
1862 output_sorted(&ports, &ctx->output);
1863 svec_destroy(&ports);
1867 add_port(struct vsctl_context *ctx,
1868 const char *br_name, const char *port_name,
1869 bool may_exist, bool fake_iface,
1870 char *iface_names[], int n_ifaces,
1871 char *settings[], int n_settings)
1873 struct vsctl_port *vsctl_port;
1874 struct vsctl_bridge *bridge;
1875 struct ovsrec_interface **ifaces;
1876 struct ovsrec_port *port;
1879 vsctl_context_populate_cache(ctx);
1881 struct vsctl_port *vsctl_port;
1883 vsctl_port = find_port(ctx, port_name, false);
1885 struct svec want_names, have_names;
1887 svec_init(&want_names);
1888 for (i = 0; i < n_ifaces; i++) {
1889 svec_add(&want_names, iface_names[i]);
1891 svec_sort(&want_names);
1893 svec_init(&have_names);
1894 for (i = 0; i < vsctl_port->port_cfg->n_interfaces; i++) {
1895 svec_add(&have_names,
1896 vsctl_port->port_cfg->interfaces[i]->name);
1898 svec_sort(&have_names);
1900 if (strcmp(vsctl_port->bridge->name, br_name)) {
1901 char *command = vsctl_context_to_string(ctx);
1902 vsctl_fatal("\"%s\" but %s is actually attached to bridge %s",
1903 command, port_name, vsctl_port->bridge->name);
1906 if (!svec_equal(&want_names, &have_names)) {
1907 char *have_names_string = svec_join(&have_names, ", ", "");
1908 char *command = vsctl_context_to_string(ctx);
1910 vsctl_fatal("\"%s\" but %s actually has interface(s) %s",
1911 command, port_name, have_names_string);
1914 svec_destroy(&want_names);
1915 svec_destroy(&have_names);
1920 check_conflicts(ctx, port_name,
1921 xasprintf("cannot create a port named %s", port_name));
1922 for (i = 0; i < n_ifaces; i++) {
1923 check_conflicts(ctx, iface_names[i],
1924 xasprintf("cannot create an interface named %s",
1927 bridge = find_bridge(ctx, br_name, true);
1929 ifaces = xmalloc(n_ifaces * sizeof *ifaces);
1930 for (i = 0; i < n_ifaces; i++) {
1931 ifaces[i] = ovsrec_interface_insert(ctx->txn);
1932 ovsrec_interface_set_name(ifaces[i], iface_names[i]);
1935 port = ovsrec_port_insert(ctx->txn);
1936 ovsrec_port_set_name(port, port_name);
1937 ovsrec_port_set_interfaces(port, ifaces, n_ifaces);
1938 ovsrec_port_set_bond_fake_iface(port, fake_iface);
1940 if (bridge->parent) {
1941 int64_t tag = bridge->vlan;
1942 ovsrec_port_set_tag(port, &tag, 1);
1945 for (i = 0; i < n_settings; i++) {
1946 set_column(get_table("Port"), &port->header_, settings[i],
1950 bridge_insert_port((bridge->parent ? bridge->parent->br_cfg
1951 : bridge->br_cfg), port);
1953 vsctl_port = add_port_to_cache(ctx, bridge, port);
1954 for (i = 0; i < n_ifaces; i++) {
1955 add_iface_to_cache(ctx, vsctl_port, ifaces[i]);
1961 cmd_add_port(struct vsctl_context *ctx)
1963 bool may_exist = shash_find(&ctx->options, "--may-exist") != NULL;
1965 add_port(ctx, ctx->argv[1], ctx->argv[2], may_exist, false,
1966 &ctx->argv[2], 1, &ctx->argv[3], ctx->argc - 3);
1970 cmd_add_bond(struct vsctl_context *ctx)
1972 bool may_exist = shash_find(&ctx->options, "--may-exist") != NULL;
1973 bool fake_iface = shash_find(&ctx->options, "--fake-iface");
1977 n_ifaces = ctx->argc - 3;
1978 for (i = 3; i < ctx->argc; i++) {
1979 if (strchr(ctx->argv[i], '=')) {
1985 vsctl_fatal("add-bond requires at least 2 interfaces, but only "
1986 "%d were specified", n_ifaces);
1989 add_port(ctx, ctx->argv[1], ctx->argv[2], may_exist, fake_iface,
1990 &ctx->argv[3], n_ifaces,
1991 &ctx->argv[n_ifaces + 3], ctx->argc - 3 - n_ifaces);
1995 cmd_del_port(struct vsctl_context *ctx)
1997 bool must_exist = !shash_find(&ctx->options, "--if-exists");
1998 bool with_iface = shash_find(&ctx->options, "--with-iface") != NULL;
1999 struct vsctl_port *port;
2001 vsctl_context_populate_cache(ctx);
2003 port = find_port(ctx, ctx->argv[ctx->argc - 1], must_exist);
2005 const char *target = ctx->argv[ctx->argc - 1];
2006 struct vsctl_iface *iface;
2008 port = find_port(ctx, target, false);
2010 iface = find_iface(ctx, target, false);
2015 if (must_exist && !port) {
2016 vsctl_fatal("no port or interface named %s", target);
2021 if (ctx->argc == 3) {
2022 struct vsctl_bridge *bridge;
2024 bridge = find_bridge(ctx, ctx->argv[1], true);
2025 if (port->bridge != bridge) {
2026 if (port->bridge->parent == bridge) {
2027 vsctl_fatal("bridge %s does not have a port %s (although "
2028 "its parent bridge %s does)",
2029 ctx->argv[1], ctx->argv[2],
2030 bridge->parent->name);
2032 vsctl_fatal("bridge %s does not have a port %s",
2033 ctx->argv[1], ctx->argv[2]);
2038 del_port(ctx, port);
2043 cmd_port_to_br(struct vsctl_context *ctx)
2045 struct vsctl_port *port;
2047 vsctl_context_populate_cache(ctx);
2049 port = find_port(ctx, ctx->argv[1], true);
2050 ds_put_format(&ctx->output, "%s\n", port->bridge->name);
2054 cmd_br_to_vlan(struct vsctl_context *ctx)
2056 struct vsctl_bridge *bridge;
2058 vsctl_context_populate_cache(ctx);
2060 bridge = find_bridge(ctx, ctx->argv[1], true);
2061 ds_put_format(&ctx->output, "%d\n", bridge->vlan);
2065 cmd_br_to_parent(struct vsctl_context *ctx)
2067 struct vsctl_bridge *bridge;
2069 vsctl_context_populate_cache(ctx);
2071 bridge = find_bridge(ctx, ctx->argv[1], true);
2072 if (bridge->parent) {
2073 bridge = bridge->parent;
2075 ds_put_format(&ctx->output, "%s\n", bridge->name);
2079 cmd_list_ifaces(struct vsctl_context *ctx)
2081 struct vsctl_bridge *br;
2082 struct vsctl_port *port;
2085 vsctl_context_populate_cache(ctx);
2087 br = find_bridge(ctx, ctx->argv[1], true);
2091 LIST_FOR_EACH (port, ports_node, &br->ports) {
2092 struct vsctl_iface *iface;
2094 LIST_FOR_EACH (iface, ifaces_node, &port->ifaces) {
2095 if (strcmp(iface->iface_cfg->name, br->name)) {
2096 svec_add(&ifaces, iface->iface_cfg->name);
2100 output_sorted(&ifaces, &ctx->output);
2101 svec_destroy(&ifaces);
2105 cmd_iface_to_br(struct vsctl_context *ctx)
2107 struct vsctl_iface *iface;
2109 vsctl_context_populate_cache(ctx);
2111 iface = find_iface(ctx, ctx->argv[1], true);
2112 ds_put_format(&ctx->output, "%s\n", iface->port->bridge->name);
2116 verify_controllers(struct ovsrec_bridge *bridge)
2120 ovsrec_bridge_verify_controller(bridge);
2121 for (i = 0; i < bridge->n_controller; i++) {
2122 ovsrec_controller_verify_target(bridge->controller[i]);
2127 pre_controller(struct vsctl_context *ctx)
2131 ovsdb_idl_add_column(ctx->idl, &ovsrec_controller_col_target);
2135 cmd_get_controller(struct vsctl_context *ctx)
2137 struct vsctl_bridge *br;
2138 struct svec targets;
2141 vsctl_context_populate_cache(ctx);
2143 br = find_bridge(ctx, ctx->argv[1], true);
2147 verify_controllers(br->br_cfg);
2149 /* Print the targets in sorted order for reproducibility. */
2150 svec_init(&targets);
2151 for (i = 0; i < br->br_cfg->n_controller; i++) {
2152 svec_add(&targets, br->br_cfg->controller[i]->target);
2155 svec_sort(&targets);
2156 for (i = 0; i < targets.n; i++) {
2157 ds_put_format(&ctx->output, "%s\n", targets.names[i]);
2159 svec_destroy(&targets);
2163 delete_controllers(struct ovsrec_controller **controllers,
2164 size_t n_controllers)
2168 for (i = 0; i < n_controllers; i++) {
2169 ovsrec_controller_delete(controllers[i]);
2174 cmd_del_controller(struct vsctl_context *ctx)
2176 struct ovsrec_bridge *br;
2178 vsctl_context_populate_cache(ctx);
2180 br = find_real_bridge(ctx, ctx->argv[1], true)->br_cfg;
2181 verify_controllers(br);
2183 if (br->controller) {
2184 delete_controllers(br->controller, br->n_controller);
2185 ovsrec_bridge_set_controller(br, NULL, 0);
2189 static struct ovsrec_controller **
2190 insert_controllers(struct ovsdb_idl_txn *txn, char *targets[], size_t n)
2192 struct ovsrec_controller **controllers;
2195 controllers = xmalloc(n * sizeof *controllers);
2196 for (i = 0; i < n; i++) {
2197 if (vconn_verify_name(targets[i]) && pvconn_verify_name(targets[i])) {
2198 VLOG_WARN("target type \"%s\" is possibly erroneous", targets[i]);
2200 controllers[i] = ovsrec_controller_insert(txn);
2201 ovsrec_controller_set_target(controllers[i], targets[i]);
2208 cmd_set_controller(struct vsctl_context *ctx)
2210 struct ovsrec_controller **controllers;
2211 struct ovsrec_bridge *br;
2214 vsctl_context_populate_cache(ctx);
2216 br = find_real_bridge(ctx, ctx->argv[1], true)->br_cfg;
2217 verify_controllers(br);
2219 delete_controllers(br->controller, br->n_controller);
2222 controllers = insert_controllers(ctx->txn, &ctx->argv[2], n);
2223 ovsrec_bridge_set_controller(br, controllers, n);
2228 cmd_get_fail_mode(struct vsctl_context *ctx)
2230 struct vsctl_bridge *br;
2231 const char *fail_mode;
2233 vsctl_context_populate_cache(ctx);
2234 br = find_bridge(ctx, ctx->argv[1], true);
2239 ovsrec_bridge_verify_fail_mode(br->br_cfg);
2241 fail_mode = br->br_cfg->fail_mode;
2242 if (fail_mode && strlen(fail_mode)) {
2243 ds_put_format(&ctx->output, "%s\n", fail_mode);
2248 cmd_del_fail_mode(struct vsctl_context *ctx)
2250 struct vsctl_bridge *br;
2252 vsctl_context_populate_cache(ctx);
2254 br = find_real_bridge(ctx, ctx->argv[1], true);
2256 ovsrec_bridge_set_fail_mode(br->br_cfg, NULL);
2260 cmd_set_fail_mode(struct vsctl_context *ctx)
2262 struct vsctl_bridge *br;
2263 const char *fail_mode = ctx->argv[2];
2265 vsctl_context_populate_cache(ctx);
2267 br = find_real_bridge(ctx, ctx->argv[1], true);
2269 if (strcmp(fail_mode, "standalone") && strcmp(fail_mode, "secure")) {
2270 vsctl_fatal("fail-mode must be \"standalone\" or \"secure\"");
2273 ovsrec_bridge_set_fail_mode(br->br_cfg, fail_mode);
2277 verify_managers(const struct ovsrec_open_vswitch *ovs)
2281 ovsrec_open_vswitch_verify_manager_options(ovs);
2283 for (i = 0; i < ovs->n_manager_options; ++i) {
2284 const struct ovsrec_manager *mgr = ovs->manager_options[i];
2286 ovsrec_manager_verify_target(mgr);
2291 pre_manager(struct vsctl_context *ctx)
2293 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_manager_options);
2294 ovsdb_idl_add_column(ctx->idl, &ovsrec_manager_col_target);
2298 cmd_get_manager(struct vsctl_context *ctx)
2300 const struct ovsrec_open_vswitch *ovs = ctx->ovs;
2301 struct svec targets;
2304 verify_managers(ovs);
2306 /* Print the targets in sorted order for reproducibility. */
2307 svec_init(&targets);
2309 for (i = 0; i < ovs->n_manager_options; i++) {
2310 svec_add(&targets, ovs->manager_options[i]->target);
2313 svec_sort_unique(&targets);
2314 for (i = 0; i < targets.n; i++) {
2315 ds_put_format(&ctx->output, "%s\n", targets.names[i]);
2317 svec_destroy(&targets);
2321 delete_managers(const struct vsctl_context *ctx)
2323 const struct ovsrec_open_vswitch *ovs = ctx->ovs;
2326 /* Delete Manager rows pointed to by 'manager_options' column. */
2327 for (i = 0; i < ovs->n_manager_options; i++) {
2328 ovsrec_manager_delete(ovs->manager_options[i]);
2331 /* Delete 'Manager' row refs in 'manager_options' column. */
2332 ovsrec_open_vswitch_set_manager_options(ovs, NULL, 0);
2336 cmd_del_manager(struct vsctl_context *ctx)
2338 const struct ovsrec_open_vswitch *ovs = ctx->ovs;
2340 verify_managers(ovs);
2341 delete_managers(ctx);
2345 insert_managers(struct vsctl_context *ctx, char *targets[], size_t n)
2347 struct ovsrec_manager **managers;
2350 /* Insert each manager in a new row in Manager table. */
2351 managers = xmalloc(n * sizeof *managers);
2352 for (i = 0; i < n; i++) {
2353 if (stream_verify_name(targets[i]) && pstream_verify_name(targets[i])) {
2354 VLOG_WARN("target type \"%s\" is possibly erroneous", targets[i]);
2356 managers[i] = ovsrec_manager_insert(ctx->txn);
2357 ovsrec_manager_set_target(managers[i], targets[i]);
2360 /* Store uuids of new Manager rows in 'manager_options' column. */
2361 ovsrec_open_vswitch_set_manager_options(ctx->ovs, managers, n);
2366 cmd_set_manager(struct vsctl_context *ctx)
2368 const size_t n = ctx->argc - 1;
2370 verify_managers(ctx->ovs);
2371 delete_managers(ctx);
2372 insert_managers(ctx, &ctx->argv[1], n);
2376 pre_cmd_get_ssl(struct vsctl_context *ctx)
2378 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_ssl);
2380 ovsdb_idl_add_column(ctx->idl, &ovsrec_ssl_col_private_key);
2381 ovsdb_idl_add_column(ctx->idl, &ovsrec_ssl_col_certificate);
2382 ovsdb_idl_add_column(ctx->idl, &ovsrec_ssl_col_ca_cert);
2383 ovsdb_idl_add_column(ctx->idl, &ovsrec_ssl_col_bootstrap_ca_cert);
2387 cmd_get_ssl(struct vsctl_context *ctx)
2389 struct ovsrec_ssl *ssl = ctx->ovs->ssl;
2391 ovsrec_open_vswitch_verify_ssl(ctx->ovs);
2393 ovsrec_ssl_verify_private_key(ssl);
2394 ovsrec_ssl_verify_certificate(ssl);
2395 ovsrec_ssl_verify_ca_cert(ssl);
2396 ovsrec_ssl_verify_bootstrap_ca_cert(ssl);
2398 ds_put_format(&ctx->output, "Private key: %s\n", ssl->private_key);
2399 ds_put_format(&ctx->output, "Certificate: %s\n", ssl->certificate);
2400 ds_put_format(&ctx->output, "CA Certificate: %s\n", ssl->ca_cert);
2401 ds_put_format(&ctx->output, "Bootstrap: %s\n",
2402 ssl->bootstrap_ca_cert ? "true" : "false");
2407 pre_cmd_del_ssl(struct vsctl_context *ctx)
2409 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_ssl);
2413 cmd_del_ssl(struct vsctl_context *ctx)
2415 struct ovsrec_ssl *ssl = ctx->ovs->ssl;
2418 ovsrec_open_vswitch_verify_ssl(ctx->ovs);
2419 ovsrec_ssl_delete(ssl);
2420 ovsrec_open_vswitch_set_ssl(ctx->ovs, NULL);
2425 pre_cmd_set_ssl(struct vsctl_context *ctx)
2427 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_ssl);
2431 cmd_set_ssl(struct vsctl_context *ctx)
2433 bool bootstrap = shash_find(&ctx->options, "--bootstrap");
2434 struct ovsrec_ssl *ssl = ctx->ovs->ssl;
2436 ovsrec_open_vswitch_verify_ssl(ctx->ovs);
2438 ovsrec_ssl_delete(ssl);
2440 ssl = ovsrec_ssl_insert(ctx->txn);
2442 ovsrec_ssl_set_private_key(ssl, ctx->argv[1]);
2443 ovsrec_ssl_set_certificate(ssl, ctx->argv[2]);
2444 ovsrec_ssl_set_ca_cert(ssl, ctx->argv[3]);
2446 ovsrec_ssl_set_bootstrap_ca_cert(ssl, bootstrap);
2448 ovsrec_open_vswitch_set_ssl(ctx->ovs, ssl);
2451 /* Parameter commands. */
2453 struct vsctl_row_id {
2454 const struct ovsdb_idl_table_class *table;
2455 const struct ovsdb_idl_column *name_column;
2456 const struct ovsdb_idl_column *uuid_column;
2459 struct vsctl_table_class {
2460 struct ovsdb_idl_table_class *class;
2461 struct vsctl_row_id row_ids[2];
2464 static const struct vsctl_table_class tables[] = {
2465 {&ovsrec_table_bridge,
2466 {{&ovsrec_table_bridge, &ovsrec_bridge_col_name, NULL},
2467 {NULL, NULL, NULL}}},
2469 {&ovsrec_table_controller,
2470 {{&ovsrec_table_bridge,
2471 &ovsrec_bridge_col_name,
2472 &ovsrec_bridge_col_controller}}},
2474 {&ovsrec_table_interface,
2475 {{&ovsrec_table_interface, &ovsrec_interface_col_name, NULL},
2476 {NULL, NULL, NULL}}},
2478 {&ovsrec_table_mirror,
2479 {{&ovsrec_table_mirror, &ovsrec_mirror_col_name, NULL},
2480 {NULL, NULL, NULL}}},
2482 {&ovsrec_table_manager,
2483 {{&ovsrec_table_manager, &ovsrec_manager_col_target, NULL},
2484 {NULL, NULL, NULL}}},
2486 {&ovsrec_table_netflow,
2487 {{&ovsrec_table_bridge,
2488 &ovsrec_bridge_col_name,
2489 &ovsrec_bridge_col_netflow},
2490 {NULL, NULL, NULL}}},
2492 {&ovsrec_table_open_vswitch,
2493 {{&ovsrec_table_open_vswitch, NULL, NULL},
2494 {NULL, NULL, NULL}}},
2496 {&ovsrec_table_port,
2497 {{&ovsrec_table_port, &ovsrec_port_col_name, NULL},
2498 {NULL, NULL, NULL}}},
2501 {{&ovsrec_table_port, &ovsrec_port_col_name, &ovsrec_port_col_qos},
2502 {NULL, NULL, NULL}}},
2504 {&ovsrec_table_queue,
2505 {{NULL, NULL, NULL},
2506 {NULL, NULL, NULL}}},
2509 {{&ovsrec_table_open_vswitch, NULL, &ovsrec_open_vswitch_col_ssl}}},
2511 {&ovsrec_table_sflow,
2512 {{&ovsrec_table_bridge,
2513 &ovsrec_bridge_col_name,
2514 &ovsrec_bridge_col_sflow},
2515 {NULL, NULL, NULL}}},
2517 {&ovsrec_table_flow_table,
2518 {{&ovsrec_table_flow_table, &ovsrec_flow_table_col_name, NULL},
2519 {NULL, NULL, NULL}}},
2521 {NULL, {{NULL, NULL, NULL}, {NULL, NULL, NULL}}}
2525 die_if_error(char *error)
2528 vsctl_fatal("%s", error);
2533 to_lower_and_underscores(unsigned c)
2535 return c == '-' ? '_' : tolower(c);
2539 score_partial_match(const char *name, const char *s)
2543 if (!strcmp(name, s)) {
2546 for (score = 0; ; score++, name++, s++) {
2547 if (to_lower_and_underscores(*name) != to_lower_and_underscores(*s)) {
2549 } else if (*name == '\0') {
2550 return UINT_MAX - 1;
2553 return *s == '\0' ? score : 0;
2556 static const struct vsctl_table_class *
2557 get_table(const char *table_name)
2559 const struct vsctl_table_class *table;
2560 const struct vsctl_table_class *best_match = NULL;
2561 unsigned int best_score = 0;
2563 for (table = tables; table->class; table++) {
2564 unsigned int score = score_partial_match(table->class->name,
2566 if (score > best_score) {
2569 } else if (score == best_score) {
2575 } else if (best_score) {
2576 vsctl_fatal("multiple table names match \"%s\"", table_name);
2578 vsctl_fatal("unknown table \"%s\"", table_name);
2582 static const struct vsctl_table_class *
2583 pre_get_table(struct vsctl_context *ctx, const char *table_name)
2585 const struct vsctl_table_class *table_class;
2588 table_class = get_table(table_name);
2589 ovsdb_idl_add_table(ctx->idl, table_class->class);
2591 for (i = 0; i < ARRAY_SIZE(table_class->row_ids); i++) {
2592 const struct vsctl_row_id *id = &table_class->row_ids[i];
2594 ovsdb_idl_add_table(ctx->idl, id->table);
2596 if (id->name_column) {
2597 ovsdb_idl_add_column(ctx->idl, id->name_column);
2599 if (id->uuid_column) {
2600 ovsdb_idl_add_column(ctx->idl, id->uuid_column);
2607 static const struct ovsdb_idl_row *
2608 get_row_by_id(struct vsctl_context *ctx, const struct vsctl_table_class *table,
2609 const struct vsctl_row_id *id, const char *record_id)
2611 const struct ovsdb_idl_row *referrer, *final;
2617 if (!id->name_column) {
2618 if (strcmp(record_id, ".")) {
2621 referrer = ovsdb_idl_first_row(ctx->idl, id->table);
2622 if (!referrer || ovsdb_idl_next_row(referrer)) {
2626 const struct ovsdb_idl_row *row;
2629 for (row = ovsdb_idl_first_row(ctx->idl, id->table);
2631 row = ovsdb_idl_next_row(row))
2633 const struct ovsdb_datum *name;
2635 name = ovsdb_idl_get(row, id->name_column,
2636 OVSDB_TYPE_STRING, OVSDB_TYPE_VOID);
2637 if (name->n == 1 && !strcmp(name->keys[0].string, record_id)) {
2639 vsctl_fatal("multiple rows in %s match \"%s\"",
2640 table->class->name, record_id);
2651 if (id->uuid_column) {
2652 const struct ovsdb_datum *uuid;
2654 ovsdb_idl_txn_verify(referrer, id->uuid_column);
2655 uuid = ovsdb_idl_get(referrer, id->uuid_column,
2656 OVSDB_TYPE_UUID, OVSDB_TYPE_VOID);
2658 final = ovsdb_idl_get_row_for_uuid(ctx->idl, table->class,
2659 &uuid->keys[0].uuid);
2668 static const struct ovsdb_idl_row *
2669 get_row (struct vsctl_context *ctx,
2670 const struct vsctl_table_class *table, const char *record_id,
2673 const struct ovsdb_idl_row *row;
2676 if (uuid_from_string(&uuid, record_id)) {
2677 row = ovsdb_idl_get_row_for_uuid(ctx->idl, table->class, &uuid);
2681 for (i = 0; i < ARRAY_SIZE(table->row_ids); i++) {
2682 row = get_row_by_id(ctx, table, &table->row_ids[i], record_id);
2688 if (must_exist && !row) {
2689 vsctl_fatal("no row \"%s\" in table %s",
2690 record_id, table->class->name);
2696 get_column(const struct vsctl_table_class *table, const char *column_name,
2697 const struct ovsdb_idl_column **columnp)
2699 const struct ovsdb_idl_column *best_match = NULL;
2700 unsigned int best_score = 0;
2703 for (i = 0; i < table->class->n_columns; i++) {
2704 const struct ovsdb_idl_column *column = &table->class->columns[i];
2705 unsigned int score = score_partial_match(column->name, column_name);
2706 if (score > best_score) {
2707 best_match = column;
2709 } else if (score == best_score) {
2714 *columnp = best_match;
2717 } else if (best_score) {
2718 return xasprintf("%s contains more than one column whose name "
2719 "matches \"%s\"", table->class->name, column_name);
2721 return xasprintf("%s does not contain a column whose name matches "
2722 "\"%s\"", table->class->name, column_name);
2726 static struct ovsdb_symbol *
2727 create_symbol(struct ovsdb_symbol_table *symtab, const char *id, bool *newp)
2729 struct ovsdb_symbol *symbol;
2732 vsctl_fatal("row id \"%s\" does not begin with \"@\"", id);
2736 *newp = ovsdb_symbol_table_get(symtab, id) == NULL;
2739 symbol = ovsdb_symbol_table_insert(symtab, id);
2740 if (symbol->created) {
2741 vsctl_fatal("row id \"%s\" may only be specified on one --id option",
2744 symbol->created = true;
2749 pre_get_column(struct vsctl_context *ctx,
2750 const struct vsctl_table_class *table, const char *column_name,
2751 const struct ovsdb_idl_column **columnp)
2753 die_if_error(get_column(table, column_name, columnp));
2754 ovsdb_idl_add_column(ctx->idl, *columnp);
2758 missing_operator_error(const char *arg, const char **allowed_operators,
2764 ds_put_format(&s, "%s: argument does not end in ", arg);
2765 ds_put_format(&s, "\"%s\"", allowed_operators[0]);
2766 if (n_allowed == 2) {
2767 ds_put_format(&s, " or \"%s\"", allowed_operators[1]);
2768 } else if (n_allowed > 2) {
2771 for (i = 1; i < n_allowed - 1; i++) {
2772 ds_put_format(&s, ", \"%s\"", allowed_operators[i]);
2774 ds_put_format(&s, ", or \"%s\"", allowed_operators[i]);
2776 ds_put_format(&s, " followed by a value.");
2778 return ds_steal_cstr(&s);
2781 /* Breaks 'arg' apart into a number of fields in the following order:
2783 * - The name of a column in 'table', stored into '*columnp'. The column
2784 * name may be abbreviated.
2786 * - Optionally ':' followed by a key string. The key is stored as a
2787 * malloc()'d string into '*keyp', or NULL if no key is present in
2790 * - If 'valuep' is nonnull, an operator followed by a value string. The
2791 * allowed operators are the 'n_allowed' string in 'allowed_operators',
2792 * or just "=" if 'n_allowed' is 0. If 'operatorp' is nonnull, then the
2793 * index of the operator within 'allowed_operators' is stored into
2794 * '*operatorp'. The value is stored as a malloc()'d string into
2795 * '*valuep', or NULL if no value is present in 'arg'.
2797 * On success, returns NULL. On failure, returned a malloc()'d string error
2798 * message and stores NULL into all of the nonnull output arguments. */
2799 static char * WARN_UNUSED_RESULT
2800 parse_column_key_value(const char *arg,
2801 const struct vsctl_table_class *table,
2802 const struct ovsdb_idl_column **columnp, char **keyp,
2804 const char **allowed_operators, size_t n_allowed,
2807 const char *p = arg;
2811 ovs_assert(!(operatorp && !valuep));
2817 /* Parse column name. */
2818 error = ovsdb_token_parse(&p, &column_name);
2822 if (column_name[0] == '\0') {
2824 error = xasprintf("%s: missing column name", arg);
2827 error = get_column(table, column_name, columnp);
2833 /* Parse key string. */
2836 error = ovsdb_token_parse(&p, keyp);
2842 /* Parse value string. */
2848 if (!allowed_operators) {
2849 static const char *equals = "=";
2850 allowed_operators = =
2856 for (i = 0; i < n_allowed; i++) {
2857 const char *op = allowed_operators[i];
2858 size_t op_len = strlen(op);
2860 if (op_len > best_len && !strncmp(op, p, op_len) && p[op_len]) {
2866 error = missing_operator_error(arg, allowed_operators, n_allowed);
2873 *valuep = xstrdup(p + best_len);
2876 error = xasprintf("%s: trailing garbage \"%s\" in argument",
2897 static const struct ovsdb_idl_column *
2898 pre_parse_column_key_value(struct vsctl_context *ctx,
2900 const struct vsctl_table_class *table)
2902 const struct ovsdb_idl_column *column;
2907 die_if_error(ovsdb_token_parse(&p, &column_name));
2908 if (column_name[0] == '\0') {
2909 vsctl_fatal("%s: missing column name", arg);
2912 pre_get_column(ctx, table, column_name, &column);
2919 check_mutable(const struct vsctl_table_class *table,
2920 const struct ovsdb_idl_column *column)
2922 if (!column->mutable) {
2923 vsctl_fatal("cannot modify read-only column %s in table %s",
2924 column->name, table->class->name);
2929 pre_cmd_get(struct vsctl_context *ctx)
2931 const char *id = shash_find_data(&ctx->options, "--id");
2932 const char *table_name = ctx->argv[1];
2933 const struct vsctl_table_class *table;
2936 /* Using "get" without --id or a column name could possibly make sense.
2937 * Maybe, for example, a ovs-vsctl run wants to assert that a row exists.
2938 * But it is unlikely that an interactive user would want to do that, so
2939 * issue a warning if we're running on a terminal. */
2940 if (!id && ctx->argc <= 3 && isatty(STDOUT_FILENO)) {
2941 VLOG_WARN("\"get\" command without row arguments or \"--id\" is "
2942 "possibly erroneous");
2945 table = pre_get_table(ctx, table_name);
2946 for (i = 3; i < ctx->argc; i++) {
2947 if (!strcasecmp(ctx->argv[i], "_uuid")
2948 || !strcasecmp(ctx->argv[i], "-uuid")) {
2952 pre_parse_column_key_value(ctx, ctx->argv[i], table);
2957 cmd_get(struct vsctl_context *ctx)
2959 const char *id = shash_find_data(&ctx->options, "--id");
2960 bool must_exist = !shash_find(&ctx->options, "--if-exists");
2961 const char *table_name = ctx->argv[1];
2962 const char *record_id = ctx->argv[2];
2963 const struct vsctl_table_class *table;
2964 const struct ovsdb_idl_row *row;
2965 struct ds *out = &ctx->output;
2968 if (id && !must_exist) {
2969 vsctl_fatal("--if-exists and --id may not be specified together");
2972 table = get_table(table_name);
2973 row = get_row(ctx, table, record_id, must_exist);
2979 struct ovsdb_symbol *symbol;
2982 symbol = create_symbol(ctx->symtab, id, &new);
2984 vsctl_fatal("row id \"%s\" specified on \"get\" command was used "
2985 "before it was defined", id);
2987 symbol->uuid = row->uuid;
2989 /* This symbol refers to a row that already exists, so disable warnings
2990 * about it being unreferenced. */
2991 symbol->strong_ref = true;
2993 for (i = 3; i < ctx->argc; i++) {
2994 const struct ovsdb_idl_column *column;
2995 const struct ovsdb_datum *datum;
2998 /* Special case for obtaining the UUID of a row. We can't just do this
2999 * through parse_column_key_value() below since it returns a "struct
3000 * ovsdb_idl_column" and the UUID column doesn't have one. */
3001 if (!strcasecmp(ctx->argv[i], "_uuid")
3002 || !strcasecmp(ctx->argv[i], "-uuid")) {
3003 ds_put_format(out, UUID_FMT"\n", UUID_ARGS(&row->uuid));
3007 die_if_error(parse_column_key_value(ctx->argv[i], table,
3008 &column, &key_string,
3009 NULL, NULL, 0, NULL));
3011 ovsdb_idl_txn_verify(row, column);
3012 datum = ovsdb_idl_read(row, column);
3014 union ovsdb_atom key;
3017 if (column->type.value.type == OVSDB_TYPE_VOID) {
3018 vsctl_fatal("cannot specify key to get for non-map column %s",
3022 die_if_error(ovsdb_atom_from_string(&key,
3024 key_string, ctx->symtab));
3026 idx = ovsdb_datum_find_key(datum, &key,
3027 column->type.key.type);
3028 if (idx == UINT_MAX) {
3030 vsctl_fatal("no key \"%s\" in %s record \"%s\" column %s",
3031 key_string, table->class->name, record_id,
3035 ovsdb_atom_to_string(&datum->values[idx],
3036 column->type.value.type, out);
3038 ovsdb_atom_destroy(&key, column->type.key.type);
3040 ovsdb_datum_to_string(datum, &column->type, out);
3042 ds_put_char(out, '\n');
3049 parse_column_names(const char *column_names,
3050 const struct vsctl_table_class *table,
3051 const struct ovsdb_idl_column ***columnsp,
3054 const struct ovsdb_idl_column **columns;
3057 if (!column_names) {
3060 n_columns = table->class->n_columns + 1;
3061 columns = xmalloc(n_columns * sizeof *columns);
3063 for (i = 0; i < table->class->n_columns; i++) {
3064 columns[i + 1] = &table->class->columns[i];
3067 char *s = xstrdup(column_names);
3068 size_t allocated_columns;
3069 char *save_ptr = NULL;
3073 allocated_columns = n_columns = 0;
3074 for (column_name = strtok_r(s, ", ", &save_ptr); column_name;
3075 column_name = strtok_r(NULL, ", ", &save_ptr)) {
3076 const struct ovsdb_idl_column *column;
3078 if (!strcasecmp(column_name, "_uuid")) {
3081 die_if_error(get_column(table, column_name, &column));
3083 if (n_columns >= allocated_columns) {
3084 columns = x2nrealloc(columns, &allocated_columns,
3087 columns[n_columns++] = column;
3092 vsctl_fatal("must specify at least one column name");
3095 *columnsp = columns;
3096 *n_columnsp = n_columns;
3101 pre_list_columns(struct vsctl_context *ctx,
3102 const struct vsctl_table_class *table,
3103 const char *column_names)
3105 const struct ovsdb_idl_column **columns;
3109 parse_column_names(column_names, table, &columns, &n_columns);
3110 for (i = 0; i < n_columns; i++) {
3112 ovsdb_idl_add_column(ctx->idl, columns[i]);
3119 pre_cmd_list(struct vsctl_context *ctx)
3121 const char *column_names = shash_find_data(&ctx->options, "--columns");
3122 const char *table_name = ctx->argv[1];
3123 const struct vsctl_table_class *table;
3125 table = pre_get_table(ctx, table_name);
3126 pre_list_columns(ctx, table, column_names);
3129 static struct table *
3130 list_make_table(const struct ovsdb_idl_column **columns, size_t n_columns)
3135 out = xmalloc(sizeof *out);
3138 for (i = 0; i < n_columns; i++) {
3139 const struct ovsdb_idl_column *column = columns[i];
3140 const char *column_name = column ? column->name : "_uuid";
3142 table_add_column(out, "%s", column_name);
3149 list_record(const struct ovsdb_idl_row *row,
3150 const struct ovsdb_idl_column **columns, size_t n_columns,
3160 for (i = 0; i < n_columns; i++) {
3161 const struct ovsdb_idl_column *column = columns[i];
3162 struct cell *cell = table_add_cell(out);
3165 struct ovsdb_datum datum;
3166 union ovsdb_atom atom;
3168 atom.uuid = row->uuid;
3171 datum.values = NULL;
3174 cell->json = ovsdb_datum_to_json(&datum, &ovsdb_type_uuid);
3175 cell->type = &ovsdb_type_uuid;
3177 const struct ovsdb_datum *datum = ovsdb_idl_read(row, column);
3179 cell->json = ovsdb_datum_to_json(datum, &column->type);
3180 cell->type = &column->type;
3186 cmd_list(struct vsctl_context *ctx)
3188 const char *column_names = shash_find_data(&ctx->options, "--columns");
3189 bool must_exist = !shash_find(&ctx->options, "--if-exists");
3190 const struct ovsdb_idl_column **columns;
3191 const char *table_name = ctx->argv[1];
3192 const struct vsctl_table_class *table;
3197 table = get_table(table_name);
3198 parse_column_names(column_names, table, &columns, &n_columns);
3199 out = ctx->table = list_make_table(columns, n_columns);
3200 if (ctx->argc > 2) {
3201 for (i = 2; i < ctx->argc; i++) {
3202 list_record(get_row(ctx, table, ctx->argv[i], must_exist),
3203 columns, n_columns, out);
3206 const struct ovsdb_idl_row *row;
3208 for (row = ovsdb_idl_first_row(ctx->idl, table->class); row != NULL;
3209 row = ovsdb_idl_next_row(row)) {
3210 list_record(row, columns, n_columns, out);
3217 pre_cmd_find(struct vsctl_context *ctx)
3219 const char *column_names = shash_find_data(&ctx->options, "--columns");
3220 const char *table_name = ctx->argv[1];
3221 const struct vsctl_table_class *table;
3224 table = pre_get_table(ctx, table_name);
3225 pre_list_columns(ctx, table, column_names);
3226 for (i = 2; i < ctx->argc; i++) {
3227 pre_parse_column_key_value(ctx, ctx->argv[i], table);
3232 cmd_find(struct vsctl_context *ctx)
3234 const char *column_names = shash_find_data(&ctx->options, "--columns");
3235 const struct ovsdb_idl_column **columns;
3236 const char *table_name = ctx->argv[1];
3237 const struct vsctl_table_class *table;
3238 const struct ovsdb_idl_row *row;
3242 table = get_table(table_name);
3243 parse_column_names(column_names, table, &columns, &n_columns);
3244 out = ctx->table = list_make_table(columns, n_columns);
3245 for (row = ovsdb_idl_first_row(ctx->idl, table->class); row;
3246 row = ovsdb_idl_next_row(row)) {
3249 for (i = 2; i < ctx->argc; i++) {
3250 if (!is_condition_satisfied(table, row, ctx->argv[i],
3255 list_record(row, columns, n_columns, out);
3263 pre_cmd_set(struct vsctl_context *ctx)
3265 const char *table_name = ctx->argv[1];
3266 const struct vsctl_table_class *table;
3269 table = pre_get_table(ctx, table_name);
3270 for (i = 3; i < ctx->argc; i++) {
3271 const struct ovsdb_idl_column *column;
3273 column = pre_parse_column_key_value(ctx, ctx->argv[i], table);
3274 check_mutable(table, column);
3279 set_column(const struct vsctl_table_class *table,
3280 const struct ovsdb_idl_row *row, const char *arg,
3281 struct ovsdb_symbol_table *symtab)
3283 const struct ovsdb_idl_column *column;
3284 char *key_string, *value_string;
3287 error = parse_column_key_value(arg, table, &column, &key_string,
3288 NULL, NULL, 0, &value_string);
3289 die_if_error(error);
3290 if (!value_string) {
3291 vsctl_fatal("%s: missing value", arg);
3295 union ovsdb_atom key, value;
3296 struct ovsdb_datum datum;
3298 if (column->type.value.type == OVSDB_TYPE_VOID) {
3299 vsctl_fatal("cannot specify key to set for non-map column %s",
3303 die_if_error(ovsdb_atom_from_string(&key, &column->type.key,
3304 key_string, symtab));
3305 die_if_error(ovsdb_atom_from_string(&value, &column->type.value,
3306 value_string, symtab));
3308 ovsdb_datum_init_empty(&datum);
3309 ovsdb_datum_add_unsafe(&datum, &key, &value, &column->type);
3311 ovsdb_atom_destroy(&key, column->type.key.type);
3312 ovsdb_atom_destroy(&value, column->type.value.type);
3314 ovsdb_datum_union(&datum, ovsdb_idl_read(row, column),
3315 &column->type, false);
3316 ovsdb_idl_txn_write(row, column, &datum);
3318 struct ovsdb_datum datum;
3320 die_if_error(ovsdb_datum_from_string(&datum, &column->type,
3321 value_string, symtab));
3322 ovsdb_idl_txn_write(row, column, &datum);
3330 cmd_set(struct vsctl_context *ctx)
3332 bool must_exist = !shash_find(&ctx->options, "--if-exists");
3333 const char *table_name = ctx->argv[1];
3334 const char *record_id = ctx->argv[2];
3335 const struct vsctl_table_class *table;
3336 const struct ovsdb_idl_row *row;
3339 table = get_table(table_name);
3340 row = get_row(ctx, table, record_id, must_exist);
3345 for (i = 3; i < ctx->argc; i++) {
3346 set_column(table, row, ctx->argv[i], ctx->symtab);
3349 vsctl_context_invalidate_cache(ctx);
3353 pre_cmd_add(struct vsctl_context *ctx)
3355 const char *table_name = ctx->argv[1];
3356 const char *column_name = ctx->argv[3];
3357 const struct vsctl_table_class *table;
3358 const struct ovsdb_idl_column *column;
3360 table = pre_get_table(ctx, table_name);
3361 pre_get_column(ctx, table, column_name, &column);
3362 check_mutable(table, column);
3366 cmd_add(struct vsctl_context *ctx)
3368 bool must_exist = !shash_find(&ctx->options, "--if-exists");
3369 const char *table_name = ctx->argv[1];
3370 const char *record_id = ctx->argv[2];
3371 const char *column_name = ctx->argv[3];
3372 const struct vsctl_table_class *table;
3373 const struct ovsdb_idl_column *column;
3374 const struct ovsdb_idl_row *row;
3375 const struct ovsdb_type *type;
3376 struct ovsdb_datum old;
3379 table = get_table(table_name);
3380 die_if_error(get_column(table, column_name, &column));
3381 row = get_row(ctx, table, record_id, must_exist);
3386 type = &column->type;
3387 ovsdb_datum_clone(&old, ovsdb_idl_read(row, column), &column->type);
3388 for (i = 4; i < ctx->argc; i++) {
3389 struct ovsdb_type add_type;
3390 struct ovsdb_datum add;
3394 add_type.n_max = UINT_MAX;
3395 die_if_error(ovsdb_datum_from_string(&add, &add_type, ctx->argv[i],
3397 ovsdb_datum_union(&old, &add, type, false);
3398 ovsdb_datum_destroy(&add, type);
3400 if (old.n > type->n_max) {
3401 vsctl_fatal("\"add\" operation would put %u %s in column %s of "
3402 "table %s but the maximum number is %u",
3404 type->value.type == OVSDB_TYPE_VOID ? "values" : "pairs",
3405 column->name, table->class->name, type->n_max);
3407 ovsdb_idl_txn_verify(row, column);
3408 ovsdb_idl_txn_write(row, column, &old);
3410 vsctl_context_invalidate_cache(ctx);
3414 pre_cmd_remove(struct vsctl_context *ctx)
3416 const char *table_name = ctx->argv[1];
3417 const char *column_name = ctx->argv[3];
3418 const struct vsctl_table_class *table;
3419 const struct ovsdb_idl_column *column;
3421 table = pre_get_table(ctx, table_name);
3422 pre_get_column(ctx, table, column_name, &column);
3423 check_mutable(table, column);
3427 cmd_remove(struct vsctl_context *ctx)
3429 bool must_exist = !shash_find(&ctx->options, "--if-exists");
3430 const char *table_name = ctx->argv[1];
3431 const char *record_id = ctx->argv[2];
3432 const char *column_name = ctx->argv[3];
3433 const struct vsctl_table_class *table;
3434 const struct ovsdb_idl_column *column;
3435 const struct ovsdb_idl_row *row;
3436 const struct ovsdb_type *type;
3437 struct ovsdb_datum old;
3440 table = get_table(table_name);
3441 die_if_error(get_column(table, column_name, &column));
3442 row = get_row(ctx, table, record_id, must_exist);
3447 type = &column->type;
3448 ovsdb_datum_clone(&old, ovsdb_idl_read(row, column), &column->type);
3449 for (i = 4; i < ctx->argc; i++) {
3450 struct ovsdb_type rm_type;
3451 struct ovsdb_datum rm;
3456 rm_type.n_max = UINT_MAX;
3457 error = ovsdb_datum_from_string(&rm, &rm_type,
3458 ctx->argv[i], ctx->symtab);
3459 if (error && ovsdb_type_is_map(&rm_type)) {
3461 rm_type.value.type = OVSDB_TYPE_VOID;
3462 die_if_error(ovsdb_datum_from_string(&rm, &rm_type,
3463 ctx->argv[i], ctx->symtab));
3465 ovsdb_datum_subtract(&old, type, &rm, &rm_type);
3466 ovsdb_datum_destroy(&rm, &rm_type);
3468 if (old.n < type->n_min) {
3469 vsctl_fatal("\"remove\" operation would put %u %s in column %s of "
3470 "table %s but the minimum number is %u",
3472 type->value.type == OVSDB_TYPE_VOID ? "values" : "pairs",
3473 column->name, table->class->name, type->n_min);
3475 ovsdb_idl_txn_verify(row, column);
3476 ovsdb_idl_txn_write(row, column, &old);
3478 vsctl_context_invalidate_cache(ctx);
3482 pre_cmd_clear(struct vsctl_context *ctx)
3484 const char *table_name = ctx->argv[1];
3485 const struct vsctl_table_class *table;
3488 table = pre_get_table(ctx, table_name);
3489 for (i = 3; i < ctx->argc; i++) {
3490 const struct ovsdb_idl_column *column;
3492 pre_get_column(ctx, table, ctx->argv[i], &column);
3493 check_mutable(table, column);
3498 cmd_clear(struct vsctl_context *ctx)
3500 bool must_exist = !shash_find(&ctx->options, "--if-exists");
3501 const char *table_name = ctx->argv[1];
3502 const char *record_id = ctx->argv[2];
3503 const struct vsctl_table_class *table;
3504 const struct ovsdb_idl_row *row;
3507 table = get_table(table_name);
3508 row = get_row(ctx, table, record_id, must_exist);
3513 for (i = 3; i < ctx->argc; i++) {
3514 const struct ovsdb_idl_column *column;
3515 const struct ovsdb_type *type;
3516 struct ovsdb_datum datum;
3518 die_if_error(get_column(table, ctx->argv[i], &column));
3520 type = &column->type;
3521 if (type->n_min > 0) {
3522 vsctl_fatal("\"clear\" operation cannot be applied to column %s "
3523 "of table %s, which is not allowed to be empty",
3524 column->name, table->class->name);
3527 ovsdb_datum_init_empty(&datum);
3528 ovsdb_idl_txn_write(row, column, &datum);
3531 vsctl_context_invalidate_cache(ctx);
3535 pre_create(struct vsctl_context *ctx)
3537 const char *id = shash_find_data(&ctx->options, "--id");
3538 const char *table_name = ctx->argv[1];
3539 const struct vsctl_table_class *table;
3541 table = get_table(table_name);
3542 if (!id && !table->class->is_root) {
3543 VLOG_WARN("applying \"create\" command to table %s without --id "
3544 "option will have no effect", table->class->name);
3549 cmd_create(struct vsctl_context *ctx)
3551 const char *id = shash_find_data(&ctx->options, "--id");
3552 const char *table_name = ctx->argv[1];
3553 const struct vsctl_table_class *table = get_table(table_name);
3554 const struct ovsdb_idl_row *row;
3555 const struct uuid *uuid;
3559 struct ovsdb_symbol *symbol = create_symbol(ctx->symtab, id, NULL);
3560 if (table->class->is_root) {
3561 /* This table is in the root set, meaning that rows created in it
3562 * won't disappear even if they are unreferenced, so disable
3563 * warnings about that by pretending that there is a reference. */
3564 symbol->strong_ref = true;
3566 uuid = &symbol->uuid;
3571 row = ovsdb_idl_txn_insert(ctx->txn, table->class, uuid);
3572 for (i = 2; i < ctx->argc; i++) {
3573 set_column(table, row, ctx->argv[i], ctx->symtab);
3575 ds_put_format(&ctx->output, UUID_FMT, UUID_ARGS(&row->uuid));
3578 /* This function may be used as the 'postprocess' function for commands that
3579 * insert new rows into the database. It expects that the command's 'run'
3580 * function prints the UUID reported by ovsdb_idl_txn_insert() as the command's
3581 * sole output. It replaces that output by the row's permanent UUID assigned
3582 * by the database server and appends a new-line.
3584 * Currently we use this only for "create", because the higher-level commands
3585 * are supposed to be independent of the actual structure of the vswitch
3588 post_create(struct vsctl_context *ctx)
3590 const struct uuid *real;
3593 if (!uuid_from_string(&dummy, ds_cstr(&ctx->output))) {
3596 real = ovsdb_idl_txn_get_insert_uuid(ctx->txn, &dummy);
3598 ds_clear(&ctx->output);
3599 ds_put_format(&ctx->output, UUID_FMT, UUID_ARGS(real));
3601 ds_put_char(&ctx->output, '\n');
3605 pre_cmd_destroy(struct vsctl_context *ctx)
3607 const char *table_name = ctx->argv[1];
3609 pre_get_table(ctx, table_name);
3613 cmd_destroy(struct vsctl_context *ctx)
3615 bool must_exist = !shash_find(&ctx->options, "--if-exists");
3616 bool delete_all = shash_find(&ctx->options, "--all");
3617 const char *table_name = ctx->argv[1];
3618 const struct vsctl_table_class *table;
3621 table = get_table(table_name);
3623 if (delete_all && ctx->argc > 2) {
3624 vsctl_fatal("--all and records argument should not be specified together");
3627 if (delete_all && !must_exist) {
3628 vsctl_fatal("--all and --if-exists should not be specified together");
3632 const struct ovsdb_idl_row *row;
3633 const struct ovsdb_idl_row *next_row;
3635 for (row = ovsdb_idl_first_row(ctx->idl, table->class);
3637 next_row = ovsdb_idl_next_row(row);
3638 ovsdb_idl_txn_delete(row);
3642 for (i = 2; i < ctx->argc; i++) {
3643 const struct ovsdb_idl_row *row;
3645 row = get_row(ctx, table, ctx->argv[i], must_exist);
3647 ovsdb_idl_txn_delete(row);
3651 vsctl_context_invalidate_cache(ctx);
3655 RELOP(RELOP_EQ, "=") \
3656 RELOP(RELOP_NE, "!=") \
3657 RELOP(RELOP_LT, "<") \
3658 RELOP(RELOP_GT, ">") \
3659 RELOP(RELOP_LE, "<=") \
3660 RELOP(RELOP_GE, ">=") \
3661 RELOP(RELOP_SET_EQ, "{=}") \
3662 RELOP(RELOP_SET_NE, "{!=}") \
3663 RELOP(RELOP_SET_LT, "{<}") \
3664 RELOP(RELOP_SET_GT, "{>}") \
3665 RELOP(RELOP_SET_LE, "{<=}") \
3666 RELOP(RELOP_SET_GE, "{>=}")
3669 #define RELOP(ENUM, STRING) ENUM,
3675 is_set_operator(enum relop op)
3677 return (op == RELOP_SET_EQ || op == RELOP_SET_NE ||
3678 op == RELOP_SET_LT || op == RELOP_SET_GT ||
3679 op == RELOP_SET_LE || op == RELOP_SET_GE);
3683 evaluate_relop(const struct ovsdb_datum *a, const struct ovsdb_datum *b,
3684 const struct ovsdb_type *type, enum relop op)
3689 return ovsdb_datum_compare_3way(a, b, type) == 0;
3692 return ovsdb_datum_compare_3way(a, b, type) != 0;
3694 return ovsdb_datum_compare_3way(a, b, type) < 0;
3696 return ovsdb_datum_compare_3way(a, b, type) > 0;
3698 return ovsdb_datum_compare_3way(a, b, type) <= 0;
3700 return ovsdb_datum_compare_3way(a, b, type) >= 0;
3703 return b->n > a->n && ovsdb_datum_includes_all(a, b, type);
3705 return a->n > b->n && ovsdb_datum_includes_all(b, a, type);
3707 return ovsdb_datum_includes_all(a, b, type);
3709 return ovsdb_datum_includes_all(b, a, type);
3717 is_condition_satisfied(const struct vsctl_table_class *table,
3718 const struct ovsdb_idl_row *row, const char *arg,
3719 struct ovsdb_symbol_table *symtab)
3721 static const char *operators[] = {
3722 #define RELOP(ENUM, STRING) STRING,
3727 const struct ovsdb_idl_column *column;
3728 const struct ovsdb_datum *have_datum;
3729 char *key_string, *value_string;
3730 struct ovsdb_type type;
3735 error = parse_column_key_value(arg, table, &column, &key_string,
3736 &operator, operators, ARRAY_SIZE(operators),
3738 die_if_error(error);
3739 if (!value_string) {
3740 vsctl_fatal("%s: missing value", arg);
3743 type = column->type;
3744 type.n_max = UINT_MAX;
3746 have_datum = ovsdb_idl_read(row, column);
3748 union ovsdb_atom want_key;
3749 struct ovsdb_datum b;
3752 if (column->type.value.type == OVSDB_TYPE_VOID) {
3753 vsctl_fatal("cannot specify key to check for non-map column %s",
3757 die_if_error(ovsdb_atom_from_string(&want_key, &column->type.key,
3758 key_string, symtab));
3760 type.key = type.value;
3761 type.value.type = OVSDB_TYPE_VOID;
3762 die_if_error(ovsdb_datum_from_string(&b, &type, value_string, symtab));
3764 idx = ovsdb_datum_find_key(have_datum,
3765 &want_key, column->type.key.type);
3766 if (idx == UINT_MAX && !is_set_operator(operator)) {
3769 struct ovsdb_datum a;
3771 if (idx != UINT_MAX) {
3773 a.keys = &have_datum->values[idx];
3781 retval = evaluate_relop(&a, &b, &type, operator);
3784 ovsdb_atom_destroy(&want_key, column->type.key.type);
3785 ovsdb_datum_destroy(&b, &type);
3787 struct ovsdb_datum want_datum;
3789 die_if_error(ovsdb_datum_from_string(&want_datum, &column->type,
3790 value_string, symtab));
3791 retval = evaluate_relop(have_datum, &want_datum, &type, operator);
3792 ovsdb_datum_destroy(&want_datum, &column->type);
3802 pre_cmd_wait_until(struct vsctl_context *ctx)
3804 const char *table_name = ctx->argv[1];
3805 const struct vsctl_table_class *table;
3808 table = pre_get_table(ctx, table_name);
3810 for (i = 3; i < ctx->argc; i++) {
3811 pre_parse_column_key_value(ctx, ctx->argv[i], table);
3816 cmd_wait_until(struct vsctl_context *ctx)
3818 const char *table_name = ctx->argv[1];
3819 const char *record_id = ctx->argv[2];
3820 const struct vsctl_table_class *table;
3821 const struct ovsdb_idl_row *row;
3824 table = get_table(table_name);
3826 row = get_row(ctx, table, record_id, false);
3828 ctx->try_again = true;
3832 for (i = 3; i < ctx->argc; i++) {
3833 if (!is_condition_satisfied(table, row, ctx->argv[i], ctx->symtab)) {
3834 ctx->try_again = true;
3840 /* Prepares 'ctx', which has already been initialized with
3841 * vsctl_context_init(), for processing 'command'. */
3843 vsctl_context_init_command(struct vsctl_context *ctx,
3844 struct vsctl_command *command)
3846 ctx->argc = command->argc;
3847 ctx->argv = command->argv;
3848 ctx->options = command->options;
3850 ds_swap(&ctx->output, &command->output);
3851 ctx->table = command->table;
3853 ctx->verified_ports = false;
3855 ctx->try_again = false;
3858 /* Prepares 'ctx' for processing commands, initializing its members with the
3859 * values passed in as arguments.
3861 * If 'command' is nonnull, calls vsctl_context_init_command() to prepare for
3862 * that particular command. */
3864 vsctl_context_init(struct vsctl_context *ctx, struct vsctl_command *command,
3865 struct ovsdb_idl *idl, struct ovsdb_idl_txn *txn,
3866 const struct ovsrec_open_vswitch *ovs,
3867 struct ovsdb_symbol_table *symtab)
3870 vsctl_context_init_command(ctx, command);
3875 ctx->symtab = symtab;
3876 ctx->cache_valid = false;
3879 /* Completes processing of 'command' within 'ctx'. */
3881 vsctl_context_done_command(struct vsctl_context *ctx,
3882 struct vsctl_command *command)
3884 ds_swap(&ctx->output, &command->output);
3885 command->table = ctx->table;
3888 /* Finishes up with 'ctx'.
3890 * If command is nonnull, first calls vsctl_context_done_command() to complete
3891 * processing that command within 'ctx'. */
3893 vsctl_context_done(struct vsctl_context *ctx, struct vsctl_command *command)
3896 vsctl_context_done_command(ctx, command);
3898 vsctl_context_invalidate_cache(ctx);
3902 run_prerequisites(struct vsctl_command *commands, size_t n_commands,
3903 struct ovsdb_idl *idl)
3905 struct vsctl_command *c;
3907 ovsdb_idl_add_table(idl, &ovsrec_table_open_vswitch);
3908 if (wait_for_reload) {
3909 ovsdb_idl_add_column(idl, &ovsrec_open_vswitch_col_cur_cfg);
3911 for (c = commands; c < &commands[n_commands]; c++) {
3912 if (c->syntax->prerequisites) {
3913 struct vsctl_context ctx;
3915 ds_init(&c->output);
3918 vsctl_context_init(&ctx, c, idl, NULL, NULL, NULL);
3919 (c->syntax->prerequisites)(&ctx);
3920 vsctl_context_done(&ctx, c);
3922 ovs_assert(!c->output.string);
3923 ovs_assert(!c->table);
3929 do_vsctl(const char *args, struct vsctl_command *commands, size_t n_commands,
3930 struct ovsdb_idl *idl)
3932 struct ovsdb_idl_txn *txn;
3933 const struct ovsrec_open_vswitch *ovs;
3934 enum ovsdb_idl_txn_status status;
3935 struct ovsdb_symbol_table *symtab;
3936 struct vsctl_context ctx;
3937 struct vsctl_command *c;
3938 struct shash_node *node;
3939 int64_t next_cfg = 0;
3942 txn = the_idl_txn = ovsdb_idl_txn_create(idl);
3944 ovsdb_idl_txn_set_dry_run(txn);
3947 ovsdb_idl_txn_add_comment(txn, "ovs-vsctl: %s", args);
3949 ovs = ovsrec_open_vswitch_first(idl);
3951 /* XXX add verification that table is empty */
3952 ovs = ovsrec_open_vswitch_insert(txn);
3955 if (wait_for_reload) {
3956 ovsdb_idl_txn_increment(txn, &ovs->header_,
3957 &ovsrec_open_vswitch_col_next_cfg);
3960 symtab = ovsdb_symbol_table_create();
3961 for (c = commands; c < &commands[n_commands]; c++) {
3962 ds_init(&c->output);
3965 vsctl_context_init(&ctx, NULL, idl, txn, ovs, symtab);
3966 for (c = commands; c < &commands[n_commands]; c++) {
3967 vsctl_context_init_command(&ctx, c);
3968 if (c->syntax->run) {
3969 (c->syntax->run)(&ctx);
3971 vsctl_context_done_command(&ctx, c);
3973 if (ctx.try_again) {
3974 vsctl_context_done(&ctx, NULL);
3978 vsctl_context_done(&ctx, NULL);
3980 SHASH_FOR_EACH (node, &symtab->sh) {
3981 struct ovsdb_symbol *symbol = node->data;
3982 if (!symbol->created) {
3983 vsctl_fatal("row id \"%s\" is referenced but never created (e.g. "
3984 "with \"-- --id=%s create ...\")",
3985 node->name, node->name);
3987 if (!symbol->strong_ref) {
3988 if (!symbol->weak_ref) {
3989 VLOG_WARN("row id \"%s\" was created but no reference to it "
3990 "was inserted, so it will not actually appear in "
3991 "the database", node->name);
3993 VLOG_WARN("row id \"%s\" was created but only a weak "
3994 "reference to it was inserted, so it will not "
3995 "actually appear in the database", node->name);
4000 status = ovsdb_idl_txn_commit_block(txn);
4001 if (wait_for_reload && status == TXN_SUCCESS) {
4002 next_cfg = ovsdb_idl_txn_get_increment_new_value(txn);
4004 if (status == TXN_UNCHANGED || status == TXN_SUCCESS) {
4005 for (c = commands; c < &commands[n_commands]; c++) {
4006 if (c->syntax->postprocess) {
4007 struct vsctl_context ctx;
4009 vsctl_context_init(&ctx, c, idl, txn, ovs, symtab);
4010 (c->syntax->postprocess)(&ctx);
4011 vsctl_context_done(&ctx, c);
4015 error = xstrdup(ovsdb_idl_txn_get_error(txn));
4016 ovsdb_idl_txn_destroy(txn);
4017 txn = the_idl_txn = NULL;
4020 case TXN_UNCOMMITTED:
4021 case TXN_INCOMPLETE:
4025 /* Should not happen--we never call ovsdb_idl_txn_abort(). */
4026 vsctl_fatal("transaction aborted");
4036 vsctl_fatal("transaction error: %s", error);
4038 case TXN_NOT_LOCKED:
4039 /* Should not happen--we never call ovsdb_idl_set_lock(). */
4040 vsctl_fatal("database not locked");
4047 ovsdb_symbol_table_destroy(symtab);
4049 for (c = commands; c < &commands[n_commands]; c++) {
4050 struct ds *ds = &c->output;
4053 table_print(c->table, &table_style);
4054 } else if (oneline) {
4058 for (j = 0; j < ds->length; j++) {
4059 int ch = ds->string[j];
4062 fputs("\\n", stdout);
4066 fputs("\\\\", stdout);
4075 fputs(ds_cstr(ds), stdout);
4077 ds_destroy(&c->output);
4078 table_destroy(c->table);
4081 shash_destroy_free_data(&c->options);
4085 if (wait_for_reload && status != TXN_UNCHANGED) {
4088 OVSREC_OPEN_VSWITCH_FOR_EACH (ovs, idl) {
4089 if (ovs->cur_cfg >= next_cfg) {
4093 ovsdb_idl_wait(idl);
4098 ovsdb_idl_destroy(idl);
4103 /* Our transaction needs to be rerun, or a prerequisite was not met. Free
4104 * resources and return so that the caller can try again. */
4106 ovsdb_idl_txn_abort(txn);
4107 ovsdb_idl_txn_destroy(txn);
4109 ovsdb_symbol_table_destroy(symtab);
4110 for (c = commands; c < &commands[n_commands]; c++) {
4111 ds_destroy(&c->output);
4112 table_destroy(c->table);
4118 static const struct vsctl_command_syntax all_commands[] = {
4119 /* Open vSwitch commands. */
4120 {"init", 0, 0, NULL, cmd_init, NULL, "", RW},
4121 {"show", 0, 0, pre_cmd_show, cmd_show, NULL, "", RO},
4123 /* Bridge commands. */
4124 {"add-br", 1, 3, pre_get_info, cmd_add_br, NULL, "--may-exist", RW},
4125 {"del-br", 1, 1, pre_get_info, cmd_del_br, NULL, "--if-exists", RW},
4126 {"list-br", 0, 0, pre_get_info, cmd_list_br, NULL, "--real,--fake", RO},
4127 {"br-exists", 1, 1, pre_get_info, cmd_br_exists, NULL, "", RO},
4128 {"br-to-vlan", 1, 1, pre_get_info, cmd_br_to_vlan, NULL, "", RO},
4129 {"br-to-parent", 1, 1, pre_get_info, cmd_br_to_parent, NULL, "", RO},
4130 {"br-set-external-id", 2, 3, pre_cmd_br_set_external_id,
4131 cmd_br_set_external_id, NULL, "", RW},
4132 {"br-get-external-id", 1, 2, pre_cmd_br_get_external_id,
4133 cmd_br_get_external_id, NULL, "", RO},
4135 /* Port commands. */
4136 {"list-ports", 1, 1, pre_get_info, cmd_list_ports, NULL, "", RO},
4137 {"add-port", 2, INT_MAX, pre_get_info, cmd_add_port, NULL, "--may-exist",
4139 {"add-bond", 4, INT_MAX, pre_get_info, cmd_add_bond, NULL,
4140 "--may-exist,--fake-iface", RW},
4141 {"del-port", 1, 2, pre_get_info, cmd_del_port, NULL,
4142 "--if-exists,--with-iface", RW},
4143 {"port-to-br", 1, 1, pre_get_info, cmd_port_to_br, NULL, "", RO},
4145 /* Interface commands. */
4146 {"list-ifaces", 1, 1, pre_get_info, cmd_list_ifaces, NULL, "", RO},
4147 {"iface-to-br", 1, 1, pre_get_info, cmd_iface_to_br, NULL, "", RO},
4149 /* Controller commands. */
4150 {"get-controller", 1, 1, pre_controller, cmd_get_controller, NULL, "", RO},
4151 {"del-controller", 1, 1, pre_controller, cmd_del_controller, NULL, "", RW},
4152 {"set-controller", 1, INT_MAX, pre_controller, cmd_set_controller, NULL,
4154 {"get-fail-mode", 1, 1, pre_get_info, cmd_get_fail_mode, NULL, "", RO},
4155 {"del-fail-mode", 1, 1, pre_get_info, cmd_del_fail_mode, NULL, "", RW},
4156 {"set-fail-mode", 2, 2, pre_get_info, cmd_set_fail_mode, NULL, "", RW},
4158 /* Manager commands. */
4159 {"get-manager", 0, 0, pre_manager, cmd_get_manager, NULL, "", RO},
4160 {"del-manager", 0, 0, pre_manager, cmd_del_manager, NULL, "", RW},
4161 {"set-manager", 1, INT_MAX, pre_manager, cmd_set_manager, NULL, "", RW},
4164 {"get-ssl", 0, 0, pre_cmd_get_ssl, cmd_get_ssl, NULL, "", RO},
4165 {"del-ssl", 0, 0, pre_cmd_del_ssl, cmd_del_ssl, NULL, "", RW},
4166 {"set-ssl", 3, 3, pre_cmd_set_ssl, cmd_set_ssl, NULL, "--bootstrap", RW},
4168 /* Switch commands. */
4169 {"emer-reset", 0, 0, pre_cmd_emer_reset, cmd_emer_reset, NULL, "", RW},
4171 /* Database commands. */
4172 {"comment", 0, INT_MAX, NULL, NULL, NULL, "", RO},
4173 {"get", 2, INT_MAX, pre_cmd_get, cmd_get, NULL, "--if-exists,--id=", RO},
4174 {"list", 1, INT_MAX, pre_cmd_list, cmd_list, NULL,
4175 "--if-exists,--columns=", RO},
4176 {"find", 1, INT_MAX, pre_cmd_find, cmd_find, NULL, "--columns=", RO},
4177 {"set", 3, INT_MAX, pre_cmd_set, cmd_set, NULL, "--if-exists", RW},
4178 {"add", 4, INT_MAX, pre_cmd_add, cmd_add, NULL, "--if-exists", RW},
4179 {"remove", 4, INT_MAX, pre_cmd_remove, cmd_remove, NULL, "--if-exists",
4181 {"clear", 3, INT_MAX, pre_cmd_clear, cmd_clear, NULL, "--if-exists", RW},
4182 {"create", 2, INT_MAX, pre_create, cmd_create, post_create, "--id=", RW},
4183 {"destroy", 1, INT_MAX, pre_cmd_destroy, cmd_destroy, NULL,
4184 "--if-exists,--all", RW},
4185 {"wait-until", 2, INT_MAX, pre_cmd_wait_until, cmd_wait_until, NULL, "",
4188 {NULL, 0, 0, NULL, NULL, NULL, NULL, RO},