2 * Copyright (c) 2009, 2010, 2011 Nicira Networks.
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"
35 #include "ovsdb-data.h"
36 #include "ovsdb-idl.h"
37 #include "poll-loop.h"
40 #include "stream-ssl.h"
43 #include "vswitchd/vswitch-idl.h"
49 VLOG_DEFINE_THIS_MODULE(vsctl);
51 /* vsctl_fatal() also logs the error, so it is preferred in this file. */
52 #define ovs_fatal please_use_vsctl_fatal_instead_of_ovs_fatal
56 /* A command supported by ovs-vsctl. */
57 struct vsctl_command_syntax {
58 const char *name; /* e.g. "add-br" */
59 int min_args; /* Min number of arguments following name. */
60 int max_args; /* Max number of arguments following name. */
62 /* If nonnull, calls ovsdb_idl_add_column() or ovsdb_idl_add_table() for
63 * each column or table in ctx->idl that it uses. */
64 void (*prerequisites)(struct vsctl_context *ctx);
66 /* Does the actual work of the command and puts the command's output, if
67 * any, in ctx->output or ctx->table.
69 * Alternatively, if some prerequisite of the command is not met and the
70 * caller should wait for something to change and then retry, it may set
71 * ctx->try_again to true. (Only the "wait-until" command currently does
73 void (*run)(struct vsctl_context *ctx);
75 /* If nonnull, called after the transaction has been successfully
76 * committed. ctx->output is the output from the "run" function, which
77 * this function may modify and otherwise postprocess as needed. (Only the
78 * "create" command currently does any postprocessing.) */
79 void (*postprocess)(struct vsctl_context *ctx);
81 /* A comma-separated list of supported options, e.g. "--a,--b", or the
82 * empty string if the command does not support any options. */
84 enum { RO, RW } mode; /* Does this command modify the database? */
87 struct vsctl_command {
88 /* Data that remains constant after initialization. */
89 const struct vsctl_command_syntax *syntax;
94 /* Data modified by commands. */
99 /* --db: The database server to contact. */
100 static const char *db;
102 /* --oneline: Write each command's output as a single line? */
105 /* --dry-run: Do not commit any changes. */
108 /* --no-wait: Wait for ovs-vswitchd to reload its configuration? */
109 static bool wait_for_reload = true;
111 /* --timeout: Time to wait for a connection to 'db'. */
114 /* Format for table output. */
115 static struct table_style table_style = TABLE_STYLE_DEFAULT;
117 /* All supported commands. */
118 static const struct vsctl_command_syntax all_commands[];
120 /* The IDL we're using and the current transaction, if any.
121 * This is for use by vsctl_exit() only, to allow it to clean up.
122 * Other code should use its context arguments. */
123 static struct ovsdb_idl *the_idl;
124 static struct ovsdb_idl_txn *the_idl_txn;
126 static void vsctl_exit(int status) NO_RETURN;
127 static void vsctl_fatal(const char *, ...) PRINTF_FORMAT(1, 2) NO_RETURN;
128 static char *default_db(void);
129 static void usage(void) NO_RETURN;
130 static void parse_options(int argc, char *argv[]);
131 static bool might_write_to_db(char **argv);
133 static struct vsctl_command *parse_commands(int argc, char *argv[],
134 size_t *n_commandsp);
135 static void parse_command(int argc, char *argv[], struct vsctl_command *);
136 static const struct vsctl_command_syntax *find_command(const char *name);
137 static void run_prerequisites(struct vsctl_command[], size_t n_commands,
139 static void do_vsctl(const char *args,
140 struct vsctl_command *, size_t n_commands,
143 static const struct vsctl_table_class *get_table(const char *table_name);
144 static void set_column(const struct vsctl_table_class *,
145 const struct ovsdb_idl_row *, const char *arg,
146 struct ovsdb_symbol_table *);
148 static bool is_condition_satisfied(const struct vsctl_table_class *,
149 const struct ovsdb_idl_row *,
151 struct ovsdb_symbol_table *);
154 main(int argc, char *argv[])
156 extern struct vlog_module VLM_reconnect;
157 struct ovsdb_idl *idl;
158 struct vsctl_command *commands;
162 set_program_name(argv[0]);
163 signal(SIGPIPE, SIG_IGN);
164 vlog_set_levels(NULL, VLF_CONSOLE, VLL_WARN);
165 vlog_set_levels(&VLM_reconnect, VLF_ANY_FACILITY, VLL_WARN);
168 /* Log our arguments. This is often valuable for debugging systems. */
169 args = process_escape_args(argv);
170 VLOG(might_write_to_db(argv) ? VLL_INFO : VLL_DBG, "Called as %s", args);
172 /* Parse command line. */
173 parse_options(argc, argv);
174 commands = parse_commands(argc - optind, argv + optind, &n_commands);
180 /* Initialize IDL. */
181 idl = the_idl = ovsdb_idl_create(db, &ovsrec_idl_class, false);
182 run_prerequisites(commands, n_commands, idl);
184 /* Now execute the commands. */
186 if (ovsdb_idl_run(idl)) {
187 do_vsctl(args, commands, n_commands, idl);
196 parse_options(int argc, char *argv[])
199 OPT_DB = UCHAR_MAX + 1,
208 static struct option long_options[] = {
209 {"db", required_argument, 0, OPT_DB},
210 {"no-syslog", no_argument, 0, OPT_NO_SYSLOG},
211 {"no-wait", no_argument, 0, OPT_NO_WAIT},
212 {"dry-run", no_argument, 0, OPT_DRY_RUN},
213 {"oneline", no_argument, 0, OPT_ONELINE},
214 {"timeout", required_argument, 0, 't'},
215 {"help", no_argument, 0, 'h'},
216 {"version", no_argument, 0, 'V'},
220 STREAM_SSL_LONG_OPTIONS
221 {"peer-ca-cert", required_argument, 0, OPT_PEER_CA_CERT},
225 char *tmp, *short_options;
227 tmp = long_options_to_short_options(long_options);
228 short_options = xasprintf("+%s", tmp);
231 table_style.format = TF_LIST;
236 c = getopt_long(argc, argv, short_options, long_options, NULL);
251 vlog_set_levels(&VLM_vsctl, VLF_SYSLOG, VLL_WARN);
255 wait_for_reload = false;
266 OVS_PRINT_VERSION(0, 0);
270 timeout = strtoul(optarg, NULL, 10);
272 vsctl_fatal("value %s on -t or --timeout is invalid",
278 TABLE_OPTION_HANDLERS(&table_style)
281 STREAM_SSL_OPTION_HANDLERS
283 case OPT_PEER_CA_CERT:
284 stream_ssl_set_peer_ca_cert_file(optarg);
302 static struct vsctl_command *
303 parse_commands(int argc, char *argv[], size_t *n_commandsp)
305 struct vsctl_command *commands;
306 size_t n_commands, allocated_commands;
310 n_commands = allocated_commands = 0;
312 for (start = i = 0; i <= argc; i++) {
313 if (i == argc || !strcmp(argv[i], "--")) {
315 if (n_commands >= allocated_commands) {
316 struct vsctl_command *c;
318 commands = x2nrealloc(commands, &allocated_commands,
320 for (c = commands; c < &commands[n_commands]; c++) {
321 shash_moved(&c->options);
324 parse_command(i - start, &argv[start],
325 &commands[n_commands++]);
331 vsctl_fatal("missing command name (use --help for help)");
333 *n_commandsp = n_commands;
338 parse_command(int argc, char *argv[], struct vsctl_command *command)
340 const struct vsctl_command_syntax *p;
341 struct shash_node *node;
345 shash_init(&command->options);
346 for (i = 0; i < argc; i++) {
347 const char *option = argv[i];
351 if (option[0] != '-') {
355 equals = strchr(option, '=');
357 key = xmemdup0(option, equals - option);
358 value = xstrdup(equals + 1);
360 key = xstrdup(option);
364 if (shash_find(&command->options, key)) {
365 vsctl_fatal("'%s' option specified multiple times", argv[i]);
367 shash_add_nocopy(&command->options, key, value);
370 vsctl_fatal("missing command name");
373 p = find_command(argv[i]);
375 vsctl_fatal("unknown command '%s'; use --help for help", argv[i]);
378 SHASH_FOR_EACH (node, &command->options) {
379 const char *s = strstr(p->options, node->name);
380 int end = s ? s[strlen(node->name)] : EOF;
382 if (end != '=' && end != ',' && end != ' ' && end != '\0') {
383 vsctl_fatal("'%s' command has no '%s' option",
384 argv[i], node->name);
386 if ((end == '=') != (node->data != NULL)) {
388 vsctl_fatal("missing argument to '%s' option on '%s' "
389 "command", node->name, argv[i]);
391 vsctl_fatal("'%s' option on '%s' does not accept an "
392 "argument", node->name, argv[i]);
397 n_arg = argc - i - 1;
398 if (n_arg < p->min_args) {
399 vsctl_fatal("'%s' command requires at least %d arguments",
400 p->name, p->min_args);
401 } else if (n_arg > p->max_args) {
404 for (j = i + 1; j < argc; j++) {
405 if (argv[j][0] == '-') {
406 vsctl_fatal("'%s' command takes at most %d arguments "
407 "(note that options must precede command "
408 "names and follow a \"--\" argument)",
409 p->name, p->max_args);
413 vsctl_fatal("'%s' command takes at most %d arguments",
414 p->name, p->max_args);
418 command->argc = n_arg + 1;
419 command->argv = &argv[i];
422 /* Returns the "struct vsctl_command_syntax" for a given command 'name', or a
423 * null pointer if there is none. */
424 static const struct vsctl_command_syntax *
425 find_command(const char *name)
427 static struct shash commands = SHASH_INITIALIZER(&commands);
429 if (shash_is_empty(&commands)) {
430 const struct vsctl_command_syntax *p;
432 for (p = all_commands; p->name; p++) {
433 shash_add_assert(&commands, p->name, p);
437 return shash_find_data(&commands, name);
441 vsctl_fatal(const char *format, ...)
446 va_start(args, format);
447 message = xvasprintf(format, args);
450 vlog_set_levels(&VLM_vsctl, VLF_CONSOLE, VLL_EMER);
451 VLOG_ERR("%s", message);
452 ovs_error(0, "%s", message);
453 vsctl_exit(EXIT_FAILURE);
456 /* Frees the current transaction and the underlying IDL and then calls
459 * Freeing the transaction and the IDL is not strictly necessary, but it makes
460 * for a clean memory leak report from valgrind in the normal case. That makes
461 * it easier to notice real memory leaks. */
463 vsctl_exit(int status)
466 ovsdb_idl_txn_abort(the_idl_txn);
467 ovsdb_idl_txn_destroy(the_idl_txn);
469 ovsdb_idl_destroy(the_idl);
477 %s: ovs-vswitchd management utility\n\
478 usage: %s [OPTIONS] COMMAND [ARG...]\n\
480 Open vSwitch commands:\n\
481 init initialize database, if not yet initialized\n\
482 emer-reset reset configuration to clean state\n\
485 add-br BRIDGE create a new bridge named BRIDGE\n\
486 add-br BRIDGE PARENT VLAN create new fake BRIDGE in PARENT on VLAN\n\
487 del-br BRIDGE delete BRIDGE and all of its ports\n\
488 list-br print the names of all the bridges\n\
489 br-exists BRIDGE test whether BRIDGE exists\n\
490 br-to-vlan BRIDGE print the VLAN which BRIDGE is on\n\
491 br-to-parent BRIDGE print the parent of BRIDGE\n\
492 br-set-external-id BRIDGE KEY VALUE set KEY on BRIDGE to VALUE\n\
493 br-set-external-id BRIDGE KEY unset KEY on BRIDGE\n\
494 br-get-external-id BRIDGE KEY print value of KEY on BRIDGE\n\
495 br-get-external-id BRIDGE list key-value pairs on BRIDGE\n\
497 Port commands (a bond is considered to be a single port):\n\
498 list-ports BRIDGE print the names of all the ports on BRIDGE\n\
499 add-port BRIDGE PORT add network device PORT to BRIDGE\n\
500 add-bond BRIDGE PORT IFACE... add bonded port PORT in BRIDGE from IFACES\n\
501 del-port [BRIDGE] PORT delete PORT (which may be bonded) from BRIDGE\n\
502 port-to-br PORT print name of bridge that contains PORT\n\
504 Interface commands (a bond consists of multiple interfaces):\n\
505 list-ifaces BRIDGE print the names of all interfaces on BRIDGE\n\
506 iface-to-br IFACE print name of bridge that contains IFACE\n\
508 Controller commands:\n\
509 get-controller BRIDGE print the controller for BRIDGE\n\
510 del-controller BRIDGE delete the controller for BRIDGE\n\
511 set-controller BRIDGE TARGET set the controller for BRIDGE to TARGET\n\
512 get-fail-mode BRIDGE print the fail-mode for BRIDGE\n\
513 del-fail-mode BRIDGE delete the fail-mode for BRIDGE\n\
514 set-fail-mode BRIDGE MODE set the fail-mode for BRIDGE to MODE\n\
517 get-manager print all manager(s)\n\
518 del-manager delete all manager(s)\n\
519 set-manager TARGET... set the list of manager(s) to TARGET(s)\n\
522 get-ssl print the SSL configuration\n\
523 del-ssl delete the SSL configuration\n\
524 set-ssl PRIV-KEY CERT CA-CERT set the SSL configuration\n\
527 emer-reset reset switch to known good state\n\
529 Database commands:\n\
530 list TBL [REC] list RECord (or all records) in TBL\n\
531 find TBL CONDITION... list records satisfying CONDITION in TBL\n\
532 get TBL REC COL[:KEY] print values of COLumns in RECord in TBL\n\
533 set TBL REC COL[:KEY]=VALUE set COLumn values in RECord in TBL\n\
534 add TBL REC COL [KEY=]VALUE add (KEY=)VALUE to COLumn in RECord in TBL\n\
535 remove TBL REC COL [KEY=]VALUE remove (KEY=)VALUE from COLumn\n\
536 clear TBL REC COL clear values from COLumn in RECord in TBL\n\
537 create TBL COL[:KEY]=VALUE create and initialize new record\n\
538 destroy TBL REC delete RECord from TBL\n\
539 wait-until TBL REC [COL[:KEY]=VALUE] wait until condition is true\n\
540 Potentially unsafe database commands require --force option.\n\
543 --db=DATABASE connect to DATABASE\n\
545 --no-wait do not wait for ovs-vswitchd to reconfigure\n\
546 -t, --timeout=SECS wait at most SECS seconds for ovs-vswitchd\n\
547 --dry-run do not commit changes to database\n\
548 --oneline print exactly one line of output per command\n",
549 program_name, program_name, default_db());
552 --no-syslog equivalent to --verbose=vsctl:syslog:warn\n");
553 stream_usage("database", true, true, false);
556 -h, --help display this help message\n\
557 -V, --version display version information\n");
566 def = xasprintf("unix:%s/db.sock", ovs_rundir());
571 /* Returns true if it looks like this set of arguments might modify the
572 * database, otherwise false. (Not very smart, so it's prone to false
575 might_write_to_db(char **argv)
577 for (; *argv; argv++) {
578 const struct vsctl_command_syntax *p = find_command(*argv);
579 if (p && p->mode == RW) {
586 struct vsctl_context {
590 struct shash options;
592 /* Modifiable state. */
595 struct ovsdb_idl *idl;
596 struct ovsdb_idl_txn *txn;
597 struct ovsdb_symbol_table *symtab;
598 const struct ovsrec_open_vswitch *ovs;
601 /* A command may set this member to true if some prerequisite is not met
602 * and the caller should wait for something to change and then retry. */
606 struct vsctl_bridge {
607 struct ovsrec_bridge *br_cfg;
609 struct ovsrec_controller **ctrl;
612 struct vsctl_bridge *parent;
617 struct ovsrec_port *port_cfg;
618 struct vsctl_bridge *bridge;
622 struct ovsrec_interface *iface_cfg;
623 struct vsctl_port *port;
627 struct vsctl_context *ctx;
628 struct shash bridges; /* Maps from bridge name to struct vsctl_bridge. */
629 struct shash ports; /* Maps from port name to struct vsctl_port. */
630 struct shash ifaces; /* Maps from port name to struct vsctl_iface. */
634 vsctl_context_to_string(const struct vsctl_context *ctx)
636 const struct shash_node *node;
642 SHASH_FOR_EACH (node, &ctx->options) {
643 svec_add(&words, node->name);
645 for (i = 0; i < ctx->argc; i++) {
646 svec_add(&words, ctx->argv[i]);
648 svec_terminate(&words);
650 s = process_escape_args(words.names);
652 svec_destroy(&words);
658 verify_ports(struct vsctl_context *ctx)
660 if (!ctx->verified_ports) {
661 const struct ovsrec_bridge *bridge;
662 const struct ovsrec_port *port;
664 ovsrec_open_vswitch_verify_bridges(ctx->ovs);
665 OVSREC_BRIDGE_FOR_EACH (bridge, ctx->idl) {
666 ovsrec_bridge_verify_ports(bridge);
668 OVSREC_PORT_FOR_EACH (port, ctx->idl) {
669 ovsrec_port_verify_interfaces(port);
672 ctx->verified_ports = true;
676 static struct vsctl_bridge *
677 add_bridge(struct vsctl_info *b,
678 struct ovsrec_bridge *br_cfg, const char *name,
679 struct vsctl_bridge *parent, int vlan)
681 struct vsctl_bridge *br = xmalloc(sizeof *br);
683 br->name = xstrdup(name);
687 br->ctrl = parent->br_cfg->controller;
688 br->n_ctrl = parent->br_cfg->n_controller;
689 br->fail_mode = parent->br_cfg->fail_mode;
691 br->ctrl = br_cfg->controller;
692 br->n_ctrl = br_cfg->n_controller;
693 br->fail_mode = br_cfg->fail_mode;
695 shash_add(&b->bridges, br->name, br);
700 port_is_fake_bridge(const struct ovsrec_port *port_cfg)
702 return (port_cfg->fake_bridge
704 && *port_cfg->tag >= 1 && *port_cfg->tag <= 4095);
707 static struct vsctl_bridge *
708 find_vlan_bridge(struct vsctl_info *info,
709 struct vsctl_bridge *parent, int vlan)
711 struct shash_node *node;
713 SHASH_FOR_EACH (node, &info->bridges) {
714 struct vsctl_bridge *br = node->data;
715 if (br->parent == parent && br->vlan == vlan) {
724 free_info(struct vsctl_info *info)
726 struct shash_node *node;
728 SHASH_FOR_EACH (node, &info->bridges) {
729 struct vsctl_bridge *bridge = node->data;
733 shash_destroy(&info->bridges);
735 shash_destroy_free_data(&info->ports);
736 shash_destroy_free_data(&info->ifaces);
740 pre_get_info(struct vsctl_context *ctx)
742 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_bridges);
744 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_name);
745 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_controller);
746 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_fail_mode);
747 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_ports);
749 ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_name);
750 ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_fake_bridge);
751 ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_tag);
752 ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_interfaces);
754 ovsdb_idl_add_column(ctx->idl, &ovsrec_interface_col_name);
758 get_info(struct vsctl_context *ctx, struct vsctl_info *info)
760 const struct ovsrec_open_vswitch *ovs = ctx->ovs;
761 struct sset bridges, ports;
765 shash_init(&info->bridges);
766 shash_init(&info->ports);
767 shash_init(&info->ifaces);
771 for (i = 0; i < ovs->n_bridges; i++) {
772 struct ovsrec_bridge *br_cfg = ovs->bridges[i];
773 struct vsctl_bridge *br;
776 if (!sset_add(&bridges, br_cfg->name)) {
777 VLOG_WARN("%s: database contains duplicate bridge name",
781 br = add_bridge(info, br_cfg, br_cfg->name, NULL, 0);
786 for (j = 0; j < br_cfg->n_ports; j++) {
787 struct ovsrec_port *port_cfg = br_cfg->ports[j];
789 if (!sset_add(&ports, port_cfg->name)) {
790 VLOG_WARN("%s: database contains duplicate port name",
795 if (port_is_fake_bridge(port_cfg)
796 && sset_add(&bridges, port_cfg->name)) {
797 add_bridge(info, NULL, port_cfg->name, br, *port_cfg->tag);
801 sset_destroy(&bridges);
802 sset_destroy(&ports);
806 for (i = 0; i < ovs->n_bridges; i++) {
807 struct ovsrec_bridge *br_cfg = ovs->bridges[i];
808 struct vsctl_bridge *br;
811 if (!sset_add(&bridges, br_cfg->name)) {
814 br = shash_find_data(&info->bridges, br_cfg->name);
815 for (j = 0; j < br_cfg->n_ports; j++) {
816 struct ovsrec_port *port_cfg = br_cfg->ports[j];
817 struct vsctl_port *port;
820 if (!sset_add(&ports, port_cfg->name)) {
824 if (port_is_fake_bridge(port_cfg)
825 && !sset_add(&bridges, port_cfg->name)) {
829 port = xmalloc(sizeof *port);
830 port->port_cfg = port_cfg;
832 && *port_cfg->tag >= 1 && *port_cfg->tag <= 4095) {
833 port->bridge = find_vlan_bridge(info, br, *port_cfg->tag);
840 shash_add(&info->ports, port_cfg->name, port);
842 for (k = 0; k < port_cfg->n_interfaces; k++) {
843 struct ovsrec_interface *iface_cfg = port_cfg->interfaces[k];
844 struct vsctl_iface *iface;
846 if (shash_find(&info->ifaces, iface_cfg->name)) {
847 VLOG_WARN("%s: database contains duplicate interface name",
852 iface = xmalloc(sizeof *iface);
853 iface->iface_cfg = iface_cfg;
855 shash_add(&info->ifaces, iface_cfg->name, iface);
859 sset_destroy(&bridges);
860 sset_destroy(&ports);
864 check_conflicts(struct vsctl_info *info, const char *name,
867 struct vsctl_iface *iface;
868 struct vsctl_port *port;
870 verify_ports(info->ctx);
872 if (shash_find(&info->bridges, name)) {
873 vsctl_fatal("%s because a bridge named %s already exists",
877 port = shash_find_data(&info->ports, name);
879 vsctl_fatal("%s because a port named %s already exists on "
880 "bridge %s", msg, name, port->bridge->name);
883 iface = shash_find_data(&info->ifaces, name);
885 vsctl_fatal("%s because an interface named %s already exists "
886 "on bridge %s", msg, name, iface->port->bridge->name);
892 static struct vsctl_bridge *
893 find_bridge(struct vsctl_info *info, const char *name, bool must_exist)
895 struct vsctl_bridge *br = shash_find_data(&info->bridges, name);
896 if (must_exist && !br) {
897 vsctl_fatal("no bridge named %s", name);
899 ovsrec_open_vswitch_verify_bridges(info->ctx->ovs);
903 static struct vsctl_bridge *
904 find_real_bridge(struct vsctl_info *info, const char *name, bool must_exist)
906 struct vsctl_bridge *br = find_bridge(info, name, must_exist);
907 if (br && br->parent) {
908 vsctl_fatal("%s is a fake bridge", name);
913 static struct vsctl_port *
914 find_port(struct vsctl_info *info, const char *name, bool must_exist)
916 struct vsctl_port *port = shash_find_data(&info->ports, name);
917 if (port && !strcmp(name, port->bridge->name)) {
920 if (must_exist && !port) {
921 vsctl_fatal("no port named %s", name);
923 verify_ports(info->ctx);
927 static struct vsctl_iface *
928 find_iface(struct vsctl_info *info, const char *name, bool must_exist)
930 struct vsctl_iface *iface = shash_find_data(&info->ifaces, name);
931 if (iface && !strcmp(name, iface->port->bridge->name)) {
934 if (must_exist && !iface) {
935 vsctl_fatal("no interface named %s", name);
937 verify_ports(info->ctx);
942 bridge_insert_port(struct ovsrec_bridge *br, struct ovsrec_port *port)
944 struct ovsrec_port **ports;
947 ports = xmalloc(sizeof *br->ports * (br->n_ports + 1));
948 for (i = 0; i < br->n_ports; i++) {
949 ports[i] = br->ports[i];
951 ports[br->n_ports] = port;
952 ovsrec_bridge_set_ports(br, ports, br->n_ports + 1);
957 bridge_delete_port(struct ovsrec_bridge *br, struct ovsrec_port *port)
959 struct ovsrec_port **ports;
962 ports = xmalloc(sizeof *br->ports * br->n_ports);
963 for (i = n = 0; i < br->n_ports; i++) {
964 if (br->ports[i] != port) {
965 ports[n++] = br->ports[i];
968 ovsrec_bridge_set_ports(br, ports, n);
973 ovs_insert_bridge(const struct ovsrec_open_vswitch *ovs,
974 struct ovsrec_bridge *bridge)
976 struct ovsrec_bridge **bridges;
979 bridges = xmalloc(sizeof *ovs->bridges * (ovs->n_bridges + 1));
980 for (i = 0; i < ovs->n_bridges; i++) {
981 bridges[i] = ovs->bridges[i];
983 bridges[ovs->n_bridges] = bridge;
984 ovsrec_open_vswitch_set_bridges(ovs, bridges, ovs->n_bridges + 1);
989 ovs_delete_bridge(const struct ovsrec_open_vswitch *ovs,
990 struct ovsrec_bridge *bridge)
992 struct ovsrec_bridge **bridges;
995 bridges = xmalloc(sizeof *ovs->bridges * ovs->n_bridges);
996 for (i = n = 0; i < ovs->n_bridges; i++) {
997 if (ovs->bridges[i] != bridge) {
998 bridges[n++] = ovs->bridges[i];
1001 ovsrec_open_vswitch_set_bridges(ovs, bridges, n);
1006 cmd_init(struct vsctl_context *ctx OVS_UNUSED)
1011 pre_cmd_emer_reset(struct vsctl_context *ctx)
1013 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_manager_options);
1014 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_ssl);
1016 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_controller);
1017 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_fail_mode);
1018 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_mirrors);
1019 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_netflow);
1020 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_sflow);
1021 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_flood_vlans);
1022 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_other_config);
1024 ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_other_config);
1026 ovsdb_idl_add_column(ctx->idl,
1027 &ovsrec_interface_col_ingress_policing_rate);
1028 ovsdb_idl_add_column(ctx->idl,
1029 &ovsrec_interface_col_ingress_policing_burst);
1033 cmd_emer_reset(struct vsctl_context *ctx)
1035 const struct ovsdb_idl *idl = ctx->idl;
1036 const struct ovsrec_bridge *br;
1037 const struct ovsrec_port *port;
1038 const struct ovsrec_interface *iface;
1039 const struct ovsrec_mirror *mirror, *next_mirror;
1040 const struct ovsrec_controller *ctrl, *next_ctrl;
1041 const struct ovsrec_manager *mgr, *next_mgr;
1042 const struct ovsrec_netflow *nf, *next_nf;
1043 const struct ovsrec_ssl *ssl, *next_ssl;
1044 const struct ovsrec_sflow *sflow, *next_sflow;
1046 /* Reset the Open_vSwitch table. */
1047 ovsrec_open_vswitch_set_manager_options(ctx->ovs, NULL, 0);
1048 ovsrec_open_vswitch_set_ssl(ctx->ovs, NULL);
1050 OVSREC_BRIDGE_FOR_EACH (br, idl) {
1052 char *hw_key = "hwaddr";
1053 char *hw_val = NULL;
1055 ovsrec_bridge_set_controller(br, NULL, 0);
1056 ovsrec_bridge_set_fail_mode(br, NULL);
1057 ovsrec_bridge_set_mirrors(br, NULL, 0);
1058 ovsrec_bridge_set_netflow(br, NULL);
1059 ovsrec_bridge_set_sflow(br, NULL);
1060 ovsrec_bridge_set_flood_vlans(br, NULL, 0);
1062 /* We only want to save the "hwaddr" key from other_config. */
1063 for (i=0; i < br->n_other_config; i++) {
1064 if (!strcmp(br->key_other_config[i], hw_key)) {
1065 hw_val = br->value_other_config[i];
1070 char *val = xstrdup(hw_val);
1071 ovsrec_bridge_set_other_config(br, &hw_key, &val, 1);
1074 ovsrec_bridge_set_other_config(br, NULL, NULL, 0);
1078 OVSREC_PORT_FOR_EACH (port, idl) {
1079 ovsrec_port_set_other_config(port, NULL, NULL, 0);
1082 OVSREC_INTERFACE_FOR_EACH (iface, idl) {
1083 /* xxx What do we do about gre/patch devices created by mgr? */
1085 ovsrec_interface_set_ingress_policing_rate(iface, 0);
1086 ovsrec_interface_set_ingress_policing_burst(iface, 0);
1089 OVSREC_MIRROR_FOR_EACH_SAFE (mirror, next_mirror, idl) {
1090 ovsrec_mirror_delete(mirror);
1093 OVSREC_CONTROLLER_FOR_EACH_SAFE (ctrl, next_ctrl, idl) {
1094 ovsrec_controller_delete(ctrl);
1097 OVSREC_MANAGER_FOR_EACH_SAFE (mgr, next_mgr, idl) {
1098 ovsrec_manager_delete(mgr);
1101 OVSREC_NETFLOW_FOR_EACH_SAFE (nf, next_nf, idl) {
1102 ovsrec_netflow_delete(nf);
1105 OVSREC_SSL_FOR_EACH_SAFE (ssl, next_ssl, idl) {
1106 ovsrec_ssl_delete(ssl);
1109 OVSREC_SFLOW_FOR_EACH_SAFE (sflow, next_sflow, idl) {
1110 ovsrec_sflow_delete(sflow);
1115 cmd_add_br(struct vsctl_context *ctx)
1117 bool may_exist = shash_find(&ctx->options, "--may-exist") != 0;
1118 const char *br_name, *parent_name;
1119 struct vsctl_info info;
1122 br_name = ctx->argv[1];
1123 if (ctx->argc == 2) {
1126 } else if (ctx->argc == 4) {
1127 parent_name = ctx->argv[2];
1128 vlan = atoi(ctx->argv[3]);
1129 if (vlan < 1 || vlan > 4095) {
1130 vsctl_fatal("%s: vlan must be between 1 and 4095", ctx->argv[0]);
1133 vsctl_fatal("'%s' command takes exactly 1 or 3 arguments",
1137 get_info(ctx, &info);
1139 struct vsctl_bridge *br;
1141 br = find_bridge(&info, br_name, false);
1145 vsctl_fatal("\"--may-exist add-br %s\" but %s is "
1146 "a VLAN bridge for VLAN %d",
1147 br_name, br_name, br->vlan);
1151 vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
1152 "is not a VLAN bridge",
1153 br_name, parent_name, vlan, br_name);
1154 } else if (strcmp(br->parent->name, parent_name)) {
1155 vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
1156 "has the wrong parent %s",
1157 br_name, parent_name, vlan,
1158 br_name, br->parent->name);
1159 } else if (br->vlan != vlan) {
1160 vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
1161 "is a VLAN bridge for the wrong VLAN %d",
1162 br_name, parent_name, vlan, br_name, br->vlan);
1168 check_conflicts(&info, br_name,
1169 xasprintf("cannot create a bridge named %s", br_name));
1172 struct ovsrec_port *port;
1173 struct ovsrec_interface *iface;
1174 struct ovsrec_bridge *br;
1176 iface = ovsrec_interface_insert(ctx->txn);
1177 ovsrec_interface_set_name(iface, br_name);
1178 ovsrec_interface_set_type(iface, "internal");
1180 port = ovsrec_port_insert(ctx->txn);
1181 ovsrec_port_set_name(port, br_name);
1182 ovsrec_port_set_interfaces(port, &iface, 1);
1184 br = ovsrec_bridge_insert(ctx->txn);
1185 ovsrec_bridge_set_name(br, br_name);
1186 ovsrec_bridge_set_ports(br, &port, 1);
1188 ovs_insert_bridge(ctx->ovs, br);
1190 struct vsctl_bridge *parent;
1191 struct ovsrec_port *port;
1192 struct ovsrec_interface *iface;
1193 struct ovsrec_bridge *br;
1196 parent = find_bridge(&info, parent_name, false);
1197 if (parent && parent->vlan) {
1198 vsctl_fatal("cannot create bridge with fake bridge as parent");
1201 vsctl_fatal("parent bridge %s does not exist", parent_name);
1203 br = parent->br_cfg;
1205 iface = ovsrec_interface_insert(ctx->txn);
1206 ovsrec_interface_set_name(iface, br_name);
1207 ovsrec_interface_set_type(iface, "internal");
1209 port = ovsrec_port_insert(ctx->txn);
1210 ovsrec_port_set_name(port, br_name);
1211 ovsrec_port_set_interfaces(port, &iface, 1);
1212 ovsrec_port_set_fake_bridge(port, true);
1213 ovsrec_port_set_tag(port, &tag, 1);
1215 bridge_insert_port(br, port);
1222 del_port(struct vsctl_info *info, struct vsctl_port *port)
1224 struct shash_node *node;
1226 SHASH_FOR_EACH (node, &info->ifaces) {
1227 struct vsctl_iface *iface = node->data;
1228 if (iface->port == port) {
1229 ovsrec_interface_delete(iface->iface_cfg);
1232 ovsrec_port_delete(port->port_cfg);
1234 bridge_delete_port((port->bridge->parent
1235 ? port->bridge->parent->br_cfg
1236 : port->bridge->br_cfg), port->port_cfg);
1240 cmd_del_br(struct vsctl_context *ctx)
1242 bool must_exist = !shash_find(&ctx->options, "--if-exists");
1243 struct vsctl_bridge *bridge;
1244 struct vsctl_info info;
1246 get_info(ctx, &info);
1247 bridge = find_bridge(&info, ctx->argv[1], must_exist);
1249 struct shash_node *node;
1251 SHASH_FOR_EACH (node, &info.ports) {
1252 struct vsctl_port *port = node->data;
1253 if (port->bridge == bridge || port->bridge->parent == bridge
1254 || !strcmp(port->port_cfg->name, bridge->name)) {
1255 del_port(&info, port);
1258 if (bridge->br_cfg) {
1259 ovsrec_bridge_delete(bridge->br_cfg);
1260 ovs_delete_bridge(ctx->ovs, bridge->br_cfg);
1267 output_sorted(struct svec *svec, struct ds *output)
1273 SVEC_FOR_EACH (i, name, svec) {
1274 ds_put_format(output, "%s\n", name);
1279 cmd_list_br(struct vsctl_context *ctx)
1281 struct shash_node *node;
1282 struct vsctl_info info;
1283 struct svec bridges;
1285 get_info(ctx, &info);
1287 svec_init(&bridges);
1288 SHASH_FOR_EACH (node, &info.bridges) {
1289 struct vsctl_bridge *br = node->data;
1290 svec_add(&bridges, br->name);
1292 output_sorted(&bridges, &ctx->output);
1293 svec_destroy(&bridges);
1299 cmd_br_exists(struct vsctl_context *ctx)
1301 struct vsctl_info info;
1303 get_info(ctx, &info);
1304 if (!find_bridge(&info, ctx->argv[1], false)) {
1310 /* Returns true if 'b_prefix' (of length 'b_prefix_len') concatenated with 'b'
1311 * equals 'a', false otherwise. */
1313 key_matches(const char *a,
1314 const char *b_prefix, size_t b_prefix_len, const char *b)
1316 return !strncmp(a, b_prefix, b_prefix_len) && !strcmp(a + b_prefix_len, b);
1320 set_external_id(char **old_keys, char **old_values, size_t old_n,
1321 char *key, char *value,
1322 char ***new_keysp, char ***new_valuesp, size_t *new_np)
1329 new_keys = xmalloc(sizeof *new_keys * (old_n + 1));
1330 new_values = xmalloc(sizeof *new_values * (old_n + 1));
1332 for (i = 0; i < old_n; i++) {
1333 if (strcmp(key, old_keys[i])) {
1334 new_keys[new_n] = old_keys[i];
1335 new_values[new_n] = old_values[i];
1340 new_keys[new_n] = key;
1341 new_values[new_n] = value;
1344 *new_keysp = new_keys;
1345 *new_valuesp = new_values;
1350 pre_cmd_br_set_external_id(struct vsctl_context *ctx)
1353 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_external_ids);
1354 ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_external_ids);
1358 cmd_br_set_external_id(struct vsctl_context *ctx)
1360 struct vsctl_info info;
1361 struct vsctl_bridge *bridge;
1362 char **keys, **values;
1365 get_info(ctx, &info);
1366 bridge = find_bridge(&info, ctx->argv[1], true);
1367 if (bridge->br_cfg) {
1368 set_external_id(bridge->br_cfg->key_external_ids,
1369 bridge->br_cfg->value_external_ids,
1370 bridge->br_cfg->n_external_ids,
1371 ctx->argv[2], ctx->argc >= 4 ? ctx->argv[3] : NULL,
1372 &keys, &values, &n);
1373 ovsrec_bridge_verify_external_ids(bridge->br_cfg);
1374 ovsrec_bridge_set_external_ids(bridge->br_cfg, keys, values, n);
1376 char *key = xasprintf("fake-bridge-%s", ctx->argv[2]);
1377 struct vsctl_port *port = shash_find_data(&info.ports, ctx->argv[1]);
1378 set_external_id(port->port_cfg->key_external_ids,
1379 port->port_cfg->value_external_ids,
1380 port->port_cfg->n_external_ids,
1381 key, ctx->argc >= 4 ? ctx->argv[3] : NULL,
1382 &keys, &values, &n);
1383 ovsrec_port_verify_external_ids(port->port_cfg);
1384 ovsrec_port_set_external_ids(port->port_cfg, keys, values, n);
1394 get_external_id(char **keys, char **values, size_t n,
1395 const char *prefix, const char *key,
1398 size_t prefix_len = strlen(prefix);
1403 for (i = 0; i < n; i++) {
1404 if (!key && !strncmp(keys[i], prefix, prefix_len)) {
1405 svec_add_nocopy(&svec, xasprintf("%s=%s",
1406 keys[i] + prefix_len, values[i]));
1407 } else if (key && key_matches(keys[i], prefix, prefix_len, key)) {
1408 svec_add(&svec, values[i]);
1412 output_sorted(&svec, output);
1413 svec_destroy(&svec);
1417 pre_cmd_br_get_external_id(struct vsctl_context *ctx)
1419 pre_cmd_br_set_external_id(ctx);
1423 cmd_br_get_external_id(struct vsctl_context *ctx)
1425 struct vsctl_info info;
1426 struct vsctl_bridge *bridge;
1428 get_info(ctx, &info);
1429 bridge = find_bridge(&info, ctx->argv[1], true);
1430 if (bridge->br_cfg) {
1431 ovsrec_bridge_verify_external_ids(bridge->br_cfg);
1432 get_external_id(bridge->br_cfg->key_external_ids,
1433 bridge->br_cfg->value_external_ids,
1434 bridge->br_cfg->n_external_ids,
1435 "", ctx->argc >= 3 ? ctx->argv[2] : NULL,
1438 struct vsctl_port *port = shash_find_data(&info.ports, ctx->argv[1]);
1439 ovsrec_port_verify_external_ids(port->port_cfg);
1440 get_external_id(port->port_cfg->key_external_ids,
1441 port->port_cfg->value_external_ids,
1442 port->port_cfg->n_external_ids,
1443 "fake-bridge-", ctx->argc >= 3 ? ctx->argv[2] : NULL, &ctx->output);
1450 cmd_list_ports(struct vsctl_context *ctx)
1452 struct vsctl_bridge *br;
1453 struct shash_node *node;
1454 struct vsctl_info info;
1457 get_info(ctx, &info);
1458 br = find_bridge(&info, ctx->argv[1], true);
1459 ovsrec_bridge_verify_ports(br->br_cfg ? br->br_cfg : br->parent->br_cfg);
1462 SHASH_FOR_EACH (node, &info.ports) {
1463 struct vsctl_port *port = node->data;
1465 if (strcmp(port->port_cfg->name, br->name) && br == port->bridge) {
1466 svec_add(&ports, port->port_cfg->name);
1469 output_sorted(&ports, &ctx->output);
1470 svec_destroy(&ports);
1476 add_port(struct vsctl_context *ctx,
1477 const char *br_name, const char *port_name,
1478 bool may_exist, bool fake_iface,
1479 char *iface_names[], int n_ifaces,
1480 char *settings[], int n_settings)
1482 struct vsctl_info info;
1483 struct vsctl_bridge *bridge;
1484 struct ovsrec_interface **ifaces;
1485 struct ovsrec_port *port;
1488 get_info(ctx, &info);
1490 struct vsctl_port *vsctl_port;
1492 vsctl_port = find_port(&info, port_name, false);
1494 struct svec want_names, have_names;
1496 svec_init(&want_names);
1497 for (i = 0; i < n_ifaces; i++) {
1498 svec_add(&want_names, iface_names[i]);
1500 svec_sort(&want_names);
1502 svec_init(&have_names);
1503 for (i = 0; i < vsctl_port->port_cfg->n_interfaces; i++) {
1504 svec_add(&have_names,
1505 vsctl_port->port_cfg->interfaces[i]->name);
1507 svec_sort(&have_names);
1509 if (strcmp(vsctl_port->bridge->name, br_name)) {
1510 char *command = vsctl_context_to_string(ctx);
1511 vsctl_fatal("\"%s\" but %s is actually attached to bridge %s",
1512 command, port_name, vsctl_port->bridge->name);
1515 if (!svec_equal(&want_names, &have_names)) {
1516 char *have_names_string = svec_join(&have_names, ", ", "");
1517 char *command = vsctl_context_to_string(ctx);
1519 vsctl_fatal("\"%s\" but %s actually has interface(s) %s",
1520 command, port_name, have_names_string);
1523 svec_destroy(&want_names);
1524 svec_destroy(&have_names);
1529 check_conflicts(&info, port_name,
1530 xasprintf("cannot create a port named %s", port_name));
1531 for (i = 0; i < n_ifaces; i++) {
1532 check_conflicts(&info, iface_names[i],
1533 xasprintf("cannot create an interface named %s",
1536 bridge = find_bridge(&info, br_name, true);
1538 ifaces = xmalloc(n_ifaces * sizeof *ifaces);
1539 for (i = 0; i < n_ifaces; i++) {
1540 ifaces[i] = ovsrec_interface_insert(ctx->txn);
1541 ovsrec_interface_set_name(ifaces[i], iface_names[i]);
1544 port = ovsrec_port_insert(ctx->txn);
1545 ovsrec_port_set_name(port, port_name);
1546 ovsrec_port_set_interfaces(port, ifaces, n_ifaces);
1547 ovsrec_port_set_bond_fake_iface(port, fake_iface);
1551 int64_t tag = bridge->vlan;
1552 ovsrec_port_set_tag(port, &tag, 1);
1555 for (i = 0; i < n_settings; i++) {
1556 set_column(get_table("Port"), &port->header_, settings[i],
1560 bridge_insert_port((bridge->parent ? bridge->parent->br_cfg
1561 : bridge->br_cfg), port);
1567 cmd_add_port(struct vsctl_context *ctx)
1569 bool may_exist = shash_find(&ctx->options, "--may-exist") != 0;
1571 add_port(ctx, ctx->argv[1], ctx->argv[2], may_exist, false,
1572 &ctx->argv[2], 1, &ctx->argv[3], ctx->argc - 3);
1576 cmd_add_bond(struct vsctl_context *ctx)
1578 bool may_exist = shash_find(&ctx->options, "--may-exist") != 0;
1579 bool fake_iface = shash_find(&ctx->options, "--fake-iface");
1583 n_ifaces = ctx->argc - 3;
1584 for (i = 3; i < ctx->argc; i++) {
1585 if (strchr(ctx->argv[i], '=')) {
1591 vsctl_fatal("add-bond requires at least 2 interfaces, but only "
1592 "%d were specified", n_ifaces);
1595 add_port(ctx, ctx->argv[1], ctx->argv[2], may_exist, fake_iface,
1596 &ctx->argv[3], n_ifaces,
1597 &ctx->argv[n_ifaces + 3], ctx->argc - 3 - n_ifaces);
1601 cmd_del_port(struct vsctl_context *ctx)
1603 bool must_exist = !shash_find(&ctx->options, "--if-exists");
1604 bool with_iface = shash_find(&ctx->options, "--with-iface") != NULL;
1605 struct vsctl_port *port;
1606 struct vsctl_info info;
1608 get_info(ctx, &info);
1610 port = find_port(&info, ctx->argv[ctx->argc - 1], must_exist);
1612 const char *target = ctx->argv[ctx->argc - 1];
1613 struct vsctl_iface *iface;
1615 port = find_port(&info, target, false);
1617 iface = find_iface(&info, target, false);
1622 if (must_exist && !port) {
1623 vsctl_fatal("no port or interface named %s", target);
1628 if (ctx->argc == 3) {
1629 struct vsctl_bridge *bridge;
1631 bridge = find_bridge(&info, ctx->argv[1], true);
1632 if (port->bridge != bridge) {
1633 if (port->bridge->parent == bridge) {
1634 vsctl_fatal("bridge %s does not have a port %s (although "
1635 "its parent bridge %s does)",
1636 ctx->argv[1], ctx->argv[2],
1637 bridge->parent->name);
1639 vsctl_fatal("bridge %s does not have a port %s",
1640 ctx->argv[1], ctx->argv[2]);
1645 del_port(&info, port);
1652 cmd_port_to_br(struct vsctl_context *ctx)
1654 struct vsctl_port *port;
1655 struct vsctl_info info;
1657 get_info(ctx, &info);
1658 port = find_port(&info, ctx->argv[1], true);
1659 ds_put_format(&ctx->output, "%s\n", port->bridge->name);
1664 cmd_br_to_vlan(struct vsctl_context *ctx)
1666 struct vsctl_bridge *bridge;
1667 struct vsctl_info info;
1669 get_info(ctx, &info);
1670 bridge = find_bridge(&info, ctx->argv[1], true);
1671 ds_put_format(&ctx->output, "%d\n", bridge->vlan);
1676 cmd_br_to_parent(struct vsctl_context *ctx)
1678 struct vsctl_bridge *bridge;
1679 struct vsctl_info info;
1681 get_info(ctx, &info);
1682 bridge = find_bridge(&info, ctx->argv[1], true);
1683 if (bridge->parent) {
1684 bridge = bridge->parent;
1686 ds_put_format(&ctx->output, "%s\n", bridge->name);
1691 cmd_list_ifaces(struct vsctl_context *ctx)
1693 struct vsctl_bridge *br;
1694 struct shash_node *node;
1695 struct vsctl_info info;
1698 get_info(ctx, &info);
1699 br = find_bridge(&info, ctx->argv[1], true);
1703 SHASH_FOR_EACH (node, &info.ifaces) {
1704 struct vsctl_iface *iface = node->data;
1706 if (strcmp(iface->iface_cfg->name, br->name)
1707 && br == iface->port->bridge) {
1708 svec_add(&ifaces, iface->iface_cfg->name);
1711 output_sorted(&ifaces, &ctx->output);
1712 svec_destroy(&ifaces);
1718 cmd_iface_to_br(struct vsctl_context *ctx)
1720 struct vsctl_iface *iface;
1721 struct vsctl_info info;
1723 get_info(ctx, &info);
1724 iface = find_iface(&info, ctx->argv[1], true);
1725 ds_put_format(&ctx->output, "%s\n", iface->port->bridge->name);
1730 verify_controllers(struct ovsrec_bridge *bridge)
1735 ovsrec_bridge_verify_controller(bridge);
1736 for (i = 0; i < bridge->n_controller; i++) {
1737 ovsrec_controller_verify_target(bridge->controller[i]);
1743 pre_controller(struct vsctl_context *ctx)
1747 ovsdb_idl_add_column(ctx->idl, &ovsrec_controller_col_target);
1751 cmd_get_controller(struct vsctl_context *ctx)
1753 struct vsctl_info info;
1754 struct vsctl_bridge *br;
1755 struct svec targets;
1758 get_info(ctx, &info);
1759 br = find_bridge(&info, ctx->argv[1], true);
1760 verify_controllers(br->br_cfg);
1762 /* Print the targets in sorted order for reproducibility. */
1763 svec_init(&targets);
1764 for (i = 0; i < br->n_ctrl; i++) {
1765 svec_add(&targets, br->ctrl[i]->target);
1768 svec_sort(&targets);
1769 for (i = 0; i < targets.n; i++) {
1770 ds_put_format(&ctx->output, "%s\n", targets.names[i]);
1772 svec_destroy(&targets);
1778 delete_controllers(struct ovsrec_controller **controllers,
1779 size_t n_controllers)
1783 for (i = 0; i < n_controllers; i++) {
1784 ovsrec_controller_delete(controllers[i]);
1789 cmd_del_controller(struct vsctl_context *ctx)
1791 struct vsctl_info info;
1792 struct vsctl_bridge *br;
1794 get_info(ctx, &info);
1796 br = find_real_bridge(&info, ctx->argv[1], true);
1797 verify_controllers(br->br_cfg);
1800 delete_controllers(br->ctrl, br->n_ctrl);
1801 ovsrec_bridge_set_controller(br->br_cfg, NULL, 0);
1807 static struct ovsrec_controller **
1808 insert_controllers(struct ovsdb_idl_txn *txn, char *targets[], size_t n)
1810 struct ovsrec_controller **controllers;
1813 controllers = xmalloc(n * sizeof *controllers);
1814 for (i = 0; i < n; i++) {
1815 controllers[i] = ovsrec_controller_insert(txn);
1816 ovsrec_controller_set_target(controllers[i], targets[i]);
1823 cmd_set_controller(struct vsctl_context *ctx)
1825 struct vsctl_info info;
1826 struct vsctl_bridge *br;
1827 struct ovsrec_controller **controllers;
1830 get_info(ctx, &info);
1831 br = find_real_bridge(&info, ctx->argv[1], true);
1832 verify_controllers(br->br_cfg);
1834 delete_controllers(br->ctrl, br->n_ctrl);
1837 controllers = insert_controllers(ctx->txn, &ctx->argv[2], n);
1838 ovsrec_bridge_set_controller(br->br_cfg, controllers, n);
1845 cmd_get_fail_mode(struct vsctl_context *ctx)
1847 struct vsctl_info info;
1848 struct vsctl_bridge *br;
1850 get_info(ctx, &info);
1851 br = find_bridge(&info, ctx->argv[1], true);
1854 ovsrec_bridge_verify_fail_mode(br->br_cfg);
1856 if (br->fail_mode && strlen(br->fail_mode)) {
1857 ds_put_format(&ctx->output, "%s\n", br->fail_mode);
1864 cmd_del_fail_mode(struct vsctl_context *ctx)
1866 struct vsctl_info info;
1867 struct vsctl_bridge *br;
1869 get_info(ctx, &info);
1870 br = find_real_bridge(&info, ctx->argv[1], true);
1872 ovsrec_bridge_set_fail_mode(br->br_cfg, NULL);
1878 cmd_set_fail_mode(struct vsctl_context *ctx)
1880 struct vsctl_info info;
1881 struct vsctl_bridge *br;
1882 const char *fail_mode = ctx->argv[2];
1884 get_info(ctx, &info);
1885 br = find_real_bridge(&info, ctx->argv[1], true);
1887 if (strcmp(fail_mode, "standalone") && strcmp(fail_mode, "secure")) {
1888 vsctl_fatal("fail-mode must be \"standalone\" or \"secure\"");
1891 ovsrec_bridge_set_fail_mode(br->br_cfg, fail_mode);
1897 verify_managers(const struct ovsrec_open_vswitch *ovs)
1901 ovsrec_open_vswitch_verify_manager_options(ovs);
1903 for (i = 0; i < ovs->n_manager_options; ++i) {
1904 const struct ovsrec_manager *mgr = ovs->manager_options[i];
1906 ovsrec_manager_verify_target(mgr);
1911 pre_manager(struct vsctl_context *ctx)
1913 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_manager_options);
1914 ovsdb_idl_add_column(ctx->idl, &ovsrec_manager_col_target);
1918 cmd_get_manager(struct vsctl_context *ctx)
1920 const struct ovsrec_open_vswitch *ovs = ctx->ovs;
1921 struct svec targets;
1924 verify_managers(ovs);
1926 /* Print the targets in sorted order for reproducibility. */
1927 svec_init(&targets);
1929 for (i = 0; i < ovs->n_manager_options; i++) {
1930 svec_add(&targets, ovs->manager_options[i]->target);
1933 svec_sort_unique(&targets);
1934 for (i = 0; i < targets.n; i++) {
1935 ds_put_format(&ctx->output, "%s\n", targets.names[i]);
1937 svec_destroy(&targets);
1941 delete_managers(const struct vsctl_context *ctx)
1943 const struct ovsrec_open_vswitch *ovs = ctx->ovs;
1946 /* Delete Manager rows pointed to by 'manager_options' column. */
1947 for (i = 0; i < ovs->n_manager_options; i++) {
1948 ovsrec_manager_delete(ovs->manager_options[i]);
1951 /* Delete 'Manager' row refs in 'manager_options' column. */
1952 ovsrec_open_vswitch_set_manager_options(ovs, NULL, 0);
1956 cmd_del_manager(struct vsctl_context *ctx)
1958 const struct ovsrec_open_vswitch *ovs = ctx->ovs;
1960 verify_managers(ovs);
1961 delete_managers(ctx);
1965 insert_managers(struct vsctl_context *ctx, char *targets[], size_t n)
1967 struct ovsrec_manager **managers;
1970 /* Insert each manager in a new row in Manager table. */
1971 managers = xmalloc(n * sizeof *managers);
1972 for (i = 0; i < n; i++) {
1973 managers[i] = ovsrec_manager_insert(ctx->txn);
1974 ovsrec_manager_set_target(managers[i], targets[i]);
1977 /* Store uuids of new Manager rows in 'manager_options' column. */
1978 ovsrec_open_vswitch_set_manager_options(ctx->ovs, managers, n);
1983 cmd_set_manager(struct vsctl_context *ctx)
1985 const size_t n = ctx->argc - 1;
1987 verify_managers(ctx->ovs);
1988 delete_managers(ctx);
1989 insert_managers(ctx, &ctx->argv[1], n);
1993 pre_cmd_get_ssl(struct vsctl_context *ctx)
1995 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_ssl);
1997 ovsdb_idl_add_column(ctx->idl, &ovsrec_ssl_col_private_key);
1998 ovsdb_idl_add_column(ctx->idl, &ovsrec_ssl_col_certificate);
1999 ovsdb_idl_add_column(ctx->idl, &ovsrec_ssl_col_ca_cert);
2000 ovsdb_idl_add_column(ctx->idl, &ovsrec_ssl_col_bootstrap_ca_cert);
2004 cmd_get_ssl(struct vsctl_context *ctx)
2006 struct ovsrec_ssl *ssl = ctx->ovs->ssl;
2008 ovsrec_open_vswitch_verify_ssl(ctx->ovs);
2010 ovsrec_ssl_verify_private_key(ssl);
2011 ovsrec_ssl_verify_certificate(ssl);
2012 ovsrec_ssl_verify_ca_cert(ssl);
2013 ovsrec_ssl_verify_bootstrap_ca_cert(ssl);
2015 ds_put_format(&ctx->output, "Private key: %s\n", ssl->private_key);
2016 ds_put_format(&ctx->output, "Certificate: %s\n", ssl->certificate);
2017 ds_put_format(&ctx->output, "CA Certificate: %s\n", ssl->ca_cert);
2018 ds_put_format(&ctx->output, "Bootstrap: %s\n",
2019 ssl->bootstrap_ca_cert ? "true" : "false");
2024 pre_cmd_del_ssl(struct vsctl_context *ctx)
2026 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_ssl);
2030 cmd_del_ssl(struct vsctl_context *ctx)
2032 struct ovsrec_ssl *ssl = ctx->ovs->ssl;
2035 ovsrec_open_vswitch_verify_ssl(ctx->ovs);
2036 ovsrec_ssl_delete(ssl);
2037 ovsrec_open_vswitch_set_ssl(ctx->ovs, NULL);
2042 pre_cmd_set_ssl(struct vsctl_context *ctx)
2044 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_ssl);
2048 cmd_set_ssl(struct vsctl_context *ctx)
2050 bool bootstrap = shash_find(&ctx->options, "--bootstrap");
2051 struct ovsrec_ssl *ssl = ctx->ovs->ssl;
2053 ovsrec_open_vswitch_verify_ssl(ctx->ovs);
2055 ovsrec_ssl_delete(ssl);
2057 ssl = ovsrec_ssl_insert(ctx->txn);
2059 ovsrec_ssl_set_private_key(ssl, ctx->argv[1]);
2060 ovsrec_ssl_set_certificate(ssl, ctx->argv[2]);
2061 ovsrec_ssl_set_ca_cert(ssl, ctx->argv[3]);
2063 ovsrec_ssl_set_bootstrap_ca_cert(ssl, bootstrap);
2065 ovsrec_open_vswitch_set_ssl(ctx->ovs, ssl);
2068 /* Parameter commands. */
2070 struct vsctl_row_id {
2071 const struct ovsdb_idl_table_class *table;
2072 const struct ovsdb_idl_column *name_column;
2073 const struct ovsdb_idl_column *uuid_column;
2076 struct vsctl_table_class {
2077 struct ovsdb_idl_table_class *class;
2078 struct vsctl_row_id row_ids[2];
2081 static const struct vsctl_table_class tables[] = {
2082 {&ovsrec_table_bridge,
2083 {{&ovsrec_table_bridge, &ovsrec_bridge_col_name, NULL},
2084 {NULL, NULL, NULL}}},
2086 {&ovsrec_table_controller,
2087 {{&ovsrec_table_bridge,
2088 &ovsrec_bridge_col_name,
2089 &ovsrec_bridge_col_controller}}},
2091 {&ovsrec_table_interface,
2092 {{&ovsrec_table_interface, &ovsrec_interface_col_name, NULL},
2093 {NULL, NULL, NULL}}},
2095 {&ovsrec_table_mirror,
2096 {{&ovsrec_table_mirror, &ovsrec_mirror_col_name, NULL},
2097 {NULL, NULL, NULL}}},
2099 {&ovsrec_table_manager,
2100 {{&ovsrec_table_manager, &ovsrec_manager_col_target, NULL},
2101 {NULL, NULL, NULL}}},
2103 {&ovsrec_table_netflow,
2104 {{&ovsrec_table_bridge,
2105 &ovsrec_bridge_col_name,
2106 &ovsrec_bridge_col_netflow},
2107 {NULL, NULL, NULL}}},
2109 {&ovsrec_table_open_vswitch,
2110 {{&ovsrec_table_open_vswitch, NULL, NULL},
2111 {NULL, NULL, NULL}}},
2113 {&ovsrec_table_port,
2114 {{&ovsrec_table_port, &ovsrec_port_col_name, NULL},
2115 {NULL, NULL, NULL}}},
2118 {{&ovsrec_table_port, &ovsrec_port_col_name, &ovsrec_port_col_qos},
2119 {NULL, NULL, NULL}}},
2121 {&ovsrec_table_monitor,
2122 {{&ovsrec_table_interface,
2123 &ovsrec_interface_col_name,
2124 &ovsrec_interface_col_monitor},
2125 {NULL, NULL, NULL}}},
2127 {&ovsrec_table_maintenance_point,
2128 {{NULL, NULL, NULL},
2129 {NULL, NULL, NULL}}},
2131 {&ovsrec_table_queue,
2132 {{NULL, NULL, NULL},
2133 {NULL, NULL, NULL}}},
2136 {{&ovsrec_table_open_vswitch, NULL, &ovsrec_open_vswitch_col_ssl}}},
2138 {&ovsrec_table_sflow,
2139 {{&ovsrec_table_bridge,
2140 &ovsrec_bridge_col_name,
2141 &ovsrec_bridge_col_sflow},
2142 {NULL, NULL, NULL}}},
2144 {NULL, {{NULL, NULL, NULL}, {NULL, NULL, NULL}}}
2148 die_if_error(char *error)
2151 vsctl_fatal("%s", error);
2156 to_lower_and_underscores(unsigned c)
2158 return c == '-' ? '_' : tolower(c);
2162 score_partial_match(const char *name, const char *s)
2166 if (!strcmp(name, s)) {
2169 for (score = 0; ; score++, name++, s++) {
2170 if (to_lower_and_underscores(*name) != to_lower_and_underscores(*s)) {
2172 } else if (*name == '\0') {
2173 return UINT_MAX - 1;
2176 return *s == '\0' ? score : 0;
2179 static const struct vsctl_table_class *
2180 get_table(const char *table_name)
2182 const struct vsctl_table_class *table;
2183 const struct vsctl_table_class *best_match = NULL;
2184 unsigned int best_score = 0;
2186 for (table = tables; table->class; table++) {
2187 unsigned int score = score_partial_match(table->class->name,
2189 if (score > best_score) {
2192 } else if (score == best_score) {
2198 } else if (best_score) {
2199 vsctl_fatal("multiple table names match \"%s\"", table_name);
2201 vsctl_fatal("unknown table \"%s\"", table_name);
2205 static const struct vsctl_table_class *
2206 pre_get_table(struct vsctl_context *ctx, const char *table_name)
2208 const struct vsctl_table_class *table_class;
2211 table_class = get_table(table_name);
2212 ovsdb_idl_add_table(ctx->idl, table_class->class);
2214 for (i = 0; i < ARRAY_SIZE(table_class->row_ids); i++) {
2215 const struct vsctl_row_id *id = &table_class->row_ids[i];
2217 ovsdb_idl_add_table(ctx->idl, id->table);
2219 if (id->name_column) {
2220 ovsdb_idl_add_column(ctx->idl, id->name_column);
2222 if (id->uuid_column) {
2223 ovsdb_idl_add_column(ctx->idl, id->uuid_column);
2230 static const struct ovsdb_idl_row *
2231 get_row_by_id(struct vsctl_context *ctx, const struct vsctl_table_class *table,
2232 const struct vsctl_row_id *id, const char *record_id)
2234 const struct ovsdb_idl_row *referrer, *final;
2240 if (!id->name_column) {
2241 if (strcmp(record_id, ".")) {
2244 referrer = ovsdb_idl_first_row(ctx->idl, id->table);
2245 if (!referrer || ovsdb_idl_next_row(referrer)) {
2249 const struct ovsdb_idl_row *row;
2252 for (row = ovsdb_idl_first_row(ctx->idl, id->table);
2254 row = ovsdb_idl_next_row(row))
2256 const struct ovsdb_datum *name;
2258 name = ovsdb_idl_get(row, id->name_column,
2259 OVSDB_TYPE_STRING, OVSDB_TYPE_VOID);
2260 if (name->n == 1 && !strcmp(name->keys[0].string, record_id)) {
2262 vsctl_fatal("multiple rows in %s match \"%s\"",
2263 table->class->name, record_id);
2274 if (id->uuid_column) {
2275 const struct ovsdb_datum *uuid;
2277 ovsdb_idl_txn_verify(referrer, id->uuid_column);
2278 uuid = ovsdb_idl_get(referrer, id->uuid_column,
2279 OVSDB_TYPE_UUID, OVSDB_TYPE_VOID);
2281 final = ovsdb_idl_get_row_for_uuid(ctx->idl, table->class,
2282 &uuid->keys[0].uuid);
2291 static const struct ovsdb_idl_row *
2292 get_row (struct vsctl_context *ctx,
2293 const struct vsctl_table_class *table, const char *record_id)
2295 const struct ovsdb_idl_row *row;
2298 if (uuid_from_string(&uuid, record_id)) {
2299 row = ovsdb_idl_get_row_for_uuid(ctx->idl, table->class, &uuid);
2303 for (i = 0; i < ARRAY_SIZE(table->row_ids); i++) {
2304 row = get_row_by_id(ctx, table, &table->row_ids[i], record_id);
2313 static const struct ovsdb_idl_row *
2314 must_get_row(struct vsctl_context *ctx,
2315 const struct vsctl_table_class *table, const char *record_id)
2317 const struct ovsdb_idl_row *row = get_row(ctx, table, record_id);
2319 vsctl_fatal("no row \"%s\" in table %s",
2320 record_id, table->class->name);
2326 get_column(const struct vsctl_table_class *table, const char *column_name,
2327 const struct ovsdb_idl_column **columnp)
2329 const struct ovsdb_idl_column *best_match = NULL;
2330 unsigned int best_score = 0;
2333 for (i = 0; i < table->class->n_columns; i++) {
2334 const struct ovsdb_idl_column *column = &table->class->columns[i];
2335 unsigned int score = score_partial_match(column->name, column_name);
2336 if (score > best_score) {
2337 best_match = column;
2339 } else if (score == best_score) {
2344 *columnp = best_match;
2347 } else if (best_score) {
2348 return xasprintf("%s contains more than one column whose name "
2349 "matches \"%s\"", table->class->name, column_name);
2351 return xasprintf("%s does not contain a column whose name matches "
2352 "\"%s\"", table->class->name, column_name);
2356 static struct ovsdb_symbol *
2357 create_symbol(struct ovsdb_symbol_table *symtab, const char *id, bool *newp)
2359 struct ovsdb_symbol *symbol;
2362 vsctl_fatal("row id \"%s\" does not begin with \"@\"", id);
2366 *newp = ovsdb_symbol_table_get(symtab, id) == NULL;
2369 symbol = ovsdb_symbol_table_insert(symtab, id);
2370 if (symbol->created) {
2371 vsctl_fatal("row id \"%s\" may only be specified on one --id option",
2374 symbol->created = true;
2379 pre_get_column(struct vsctl_context *ctx,
2380 const struct vsctl_table_class *table, const char *column_name,
2381 const struct ovsdb_idl_column **columnp)
2383 die_if_error(get_column(table, column_name, columnp));
2384 ovsdb_idl_add_column(ctx->idl, *columnp);
2388 missing_operator_error(const char *arg, const char **allowed_operators,
2394 ds_put_format(&s, "%s: argument does not end in ", arg);
2395 ds_put_format(&s, "\"%s\"", allowed_operators[0]);
2396 if (n_allowed == 2) {
2397 ds_put_format(&s, " or \"%s\"", allowed_operators[1]);
2398 } else if (n_allowed > 2) {
2401 for (i = 1; i < n_allowed - 1; i++) {
2402 ds_put_format(&s, ", \"%s\"", allowed_operators[i]);
2404 ds_put_format(&s, ", or \"%s\"", allowed_operators[i]);
2406 ds_put_format(&s, " followed by a value.");
2408 return ds_steal_cstr(&s);
2411 /* Breaks 'arg' apart into a number of fields in the following order:
2413 * - The name of a column in 'table', stored into '*columnp'. The column
2414 * name may be abbreviated.
2416 * - Optionally ':' followed by a key string. The key is stored as a
2417 * malloc()'d string into '*keyp', or NULL if no key is present in
2420 * - If 'valuep' is nonnull, an operator followed by a value string. The
2421 * allowed operators are the 'n_allowed' string in 'allowed_operators',
2422 * or just "=" if 'n_allowed' is 0. If 'operatorp' is nonnull, then the
2423 * operator is stored into '*operatorp' (one of the pointers from
2424 * 'allowed_operators' is stored; nothing is malloc()'d). The value is
2425 * stored as a malloc()'d string into '*valuep', or NULL if no value is
2428 * On success, returns NULL. On failure, returned a malloc()'d string error
2429 * message and stores NULL into all of the nonnull output arguments. */
2430 static char * WARN_UNUSED_RESULT
2431 parse_column_key_value(const char *arg,
2432 const struct vsctl_table_class *table,
2433 const struct ovsdb_idl_column **columnp, char **keyp,
2434 const char **operatorp,
2435 const char **allowed_operators, size_t n_allowed,
2438 const char *p = arg;
2442 assert(!(operatorp && !valuep));
2448 /* Parse column name. */
2449 error = ovsdb_token_parse(&p, &column_name);
2453 if (column_name[0] == '\0') {
2455 error = xasprintf("%s: missing column name", arg);
2458 error = get_column(table, column_name, columnp);
2464 /* Parse key string. */
2467 error = ovsdb_token_parse(&p, keyp);
2473 /* Parse value string. */
2479 if (!allowed_operators) {
2480 static const char *equals = "=";
2481 allowed_operators = =
2487 for (i = 0; i < n_allowed; i++) {
2488 const char *op = allowed_operators[i];
2489 size_t op_len = strlen(op);
2491 if (op_len > best_len && !strncmp(op, p, op_len) && p[op_len]) {
2497 error = missing_operator_error(arg, allowed_operators, n_allowed);
2504 *valuep = xstrdup(p + best_len);
2507 error = xasprintf("%s: trailing garbage \"%s\" in argument",
2529 pre_parse_column_key_value(struct vsctl_context *ctx,
2531 const struct vsctl_table_class *table)
2533 const struct ovsdb_idl_column *column;
2538 die_if_error(ovsdb_token_parse(&p, &column_name));
2539 if (column_name[0] == '\0') {
2540 vsctl_fatal("%s: missing column name", arg);
2543 pre_get_column(ctx, table, column_name, &column);
2548 pre_cmd_get(struct vsctl_context *ctx)
2550 const char *table_name = ctx->argv[1];
2551 const struct vsctl_table_class *table;
2554 table = pre_get_table(ctx, table_name);
2555 for (i = 3; i < ctx->argc; i++) {
2556 if (!strcasecmp(ctx->argv[i], "_uuid")
2557 || !strcasecmp(ctx->argv[i], "-uuid")) {
2561 pre_parse_column_key_value(ctx, ctx->argv[i], table);
2566 cmd_get(struct vsctl_context *ctx)
2568 const char *id = shash_find_data(&ctx->options, "--id");
2569 bool if_exists = shash_find(&ctx->options, "--if-exists");
2570 const char *table_name = ctx->argv[1];
2571 const char *record_id = ctx->argv[2];
2572 const struct vsctl_table_class *table;
2573 const struct ovsdb_idl_row *row;
2574 struct ds *out = &ctx->output;
2577 table = get_table(table_name);
2578 row = must_get_row(ctx, table, record_id);
2580 struct ovsdb_symbol *symbol;
2583 symbol = create_symbol(ctx->symtab, id, &new);
2585 vsctl_fatal("row id \"%s\" specified on \"get\" command was used "
2586 "before it was defined", id);
2588 symbol->uuid = row->uuid;
2590 /* This symbol refers to a row that already exists, so disable warnings
2591 * about it being unreferenced. */
2592 symbol->strong_ref = true;
2594 for (i = 3; i < ctx->argc; i++) {
2595 const struct ovsdb_idl_column *column;
2596 const struct ovsdb_datum *datum;
2599 /* Special case for obtaining the UUID of a row. We can't just do this
2600 * through parse_column_key_value() below since it returns a "struct
2601 * ovsdb_idl_column" and the UUID column doesn't have one. */
2602 if (!strcasecmp(ctx->argv[i], "_uuid")
2603 || !strcasecmp(ctx->argv[i], "-uuid")) {
2604 ds_put_format(out, UUID_FMT"\n", UUID_ARGS(&row->uuid));
2608 die_if_error(parse_column_key_value(ctx->argv[i], table,
2609 &column, &key_string,
2610 NULL, NULL, 0, NULL));
2612 ovsdb_idl_txn_verify(row, column);
2613 datum = ovsdb_idl_read(row, column);
2615 union ovsdb_atom key;
2618 if (column->type.value.type == OVSDB_TYPE_VOID) {
2619 vsctl_fatal("cannot specify key to get for non-map column %s",
2623 die_if_error(ovsdb_atom_from_string(&key,
2625 key_string, ctx->symtab));
2627 idx = ovsdb_datum_find_key(datum, &key,
2628 column->type.key.type);
2629 if (idx == UINT_MAX) {
2631 vsctl_fatal("no key \"%s\" in %s record \"%s\" column %s",
2632 key_string, table->class->name, record_id,
2636 ovsdb_atom_to_string(&datum->values[idx],
2637 column->type.value.type, out);
2639 ovsdb_atom_destroy(&key, column->type.key.type);
2641 ovsdb_datum_to_string(datum, &column->type, out);
2643 ds_put_char(out, '\n');
2650 parse_column_names(const char *column_names,
2651 const struct vsctl_table_class *table,
2652 const struct ovsdb_idl_column ***columnsp,
2655 const struct ovsdb_idl_column **columns;
2658 if (!column_names) {
2661 n_columns = table->class->n_columns + 1;
2662 columns = xmalloc(n_columns * sizeof *columns);
2664 for (i = 0; i < table->class->n_columns; i++) {
2665 columns[i + 1] = &table->class->columns[i];
2668 char *s = xstrdup(column_names);
2669 size_t allocated_columns;
2670 char *save_ptr = NULL;
2674 allocated_columns = n_columns = 0;
2675 for (column_name = strtok_r(s, ", ", &save_ptr); column_name;
2676 column_name = strtok_r(NULL, ", ", &save_ptr)) {
2677 const struct ovsdb_idl_column *column;
2679 if (!strcasecmp(column_name, "_uuid")) {
2682 die_if_error(get_column(table, column_name, &column));
2684 if (n_columns >= allocated_columns) {
2685 columns = x2nrealloc(columns, &allocated_columns,
2688 columns[n_columns++] = column;
2693 vsctl_fatal("must specify at least one column name");
2696 *columnsp = columns;
2697 *n_columnsp = n_columns;
2702 pre_list_columns(struct vsctl_context *ctx,
2703 const struct vsctl_table_class *table,
2704 const char *column_names)
2706 const struct ovsdb_idl_column **columns;
2710 parse_column_names(column_names, table, &columns, &n_columns);
2711 for (i = 0; i < n_columns; i++) {
2713 ovsdb_idl_add_column(ctx->idl, columns[i]);
2720 pre_cmd_list(struct vsctl_context *ctx)
2722 const char *column_names = shash_find_data(&ctx->options, "--columns");
2723 const char *table_name = ctx->argv[1];
2724 const struct vsctl_table_class *table;
2726 table = pre_get_table(ctx, table_name);
2727 pre_list_columns(ctx, table, column_names);
2730 static struct table *
2731 list_make_table(const struct ovsdb_idl_column **columns, size_t n_columns)
2736 out = xmalloc(sizeof *out);
2739 for (i = 0; i < n_columns; i++) {
2740 const struct ovsdb_idl_column *column = columns[i];
2741 const char *column_name = column ? column->name : "_uuid";
2743 table_add_column(out, "%s", column_name);
2750 list_record(const struct ovsdb_idl_row *row,
2751 const struct ovsdb_idl_column **columns, size_t n_columns,
2757 for (i = 0; i < n_columns; i++) {
2758 const struct ovsdb_idl_column *column = columns[i];
2759 struct cell *cell = table_add_cell(out);
2762 struct ovsdb_datum datum;
2763 union ovsdb_atom atom;
2765 atom.uuid = row->uuid;
2768 datum.values = NULL;
2771 cell->json = ovsdb_datum_to_json(&datum, &ovsdb_type_uuid);
2772 cell->type = &ovsdb_type_uuid;
2774 const struct ovsdb_datum *datum = ovsdb_idl_read(row, column);
2776 cell->json = ovsdb_datum_to_json(datum, &column->type);
2777 cell->type = &column->type;
2783 cmd_list(struct vsctl_context *ctx)
2785 const char *column_names = shash_find_data(&ctx->options, "--columns");
2786 const struct ovsdb_idl_column **columns;
2787 const char *table_name = ctx->argv[1];
2788 const struct vsctl_table_class *table;
2793 table = get_table(table_name);
2794 parse_column_names(column_names, table, &columns, &n_columns);
2795 out = ctx->table = list_make_table(columns, n_columns);
2796 if (ctx->argc > 2) {
2797 for (i = 2; i < ctx->argc; i++) {
2798 list_record(must_get_row(ctx, table, ctx->argv[i]),
2799 columns, n_columns, out);
2802 const struct ovsdb_idl_row *row;
2804 for (row = ovsdb_idl_first_row(ctx->idl, table->class); row != NULL;
2805 row = ovsdb_idl_next_row(row)) {
2806 list_record(row, columns, n_columns, out);
2813 pre_cmd_find(struct vsctl_context *ctx)
2815 const char *column_names = shash_find_data(&ctx->options, "--columns");
2816 const char *table_name = ctx->argv[1];
2817 const struct vsctl_table_class *table;
2820 table = pre_get_table(ctx, table_name);
2821 pre_list_columns(ctx, table, column_names);
2822 for (i = 2; i < ctx->argc; i++) {
2823 pre_parse_column_key_value(ctx, ctx->argv[i], table);
2828 cmd_find(struct vsctl_context *ctx)
2830 const char *column_names = shash_find_data(&ctx->options, "--columns");
2831 const struct ovsdb_idl_column **columns;
2832 const char *table_name = ctx->argv[1];
2833 const struct vsctl_table_class *table;
2834 const struct ovsdb_idl_row *row;
2838 table = get_table(table_name);
2839 parse_column_names(column_names, table, &columns, &n_columns);
2840 out = ctx->table = list_make_table(columns, n_columns);
2841 for (row = ovsdb_idl_first_row(ctx->idl, table->class); row;
2842 row = ovsdb_idl_next_row(row)) {
2845 for (i = 2; i < ctx->argc; i++) {
2846 if (!is_condition_satisfied(table, row, ctx->argv[i],
2851 list_record(row, columns, n_columns, out);
2859 pre_cmd_set(struct vsctl_context *ctx)
2861 const char *table_name = ctx->argv[1];
2862 const struct vsctl_table_class *table;
2865 table = pre_get_table(ctx, table_name);
2866 for (i = 3; i < ctx->argc; i++) {
2867 pre_parse_column_key_value(ctx, ctx->argv[i], table);
2872 set_column(const struct vsctl_table_class *table,
2873 const struct ovsdb_idl_row *row, const char *arg,
2874 struct ovsdb_symbol_table *symtab)
2876 const struct ovsdb_idl_column *column;
2877 char *key_string, *value_string;
2880 error = parse_column_key_value(arg, table, &column, &key_string,
2881 NULL, NULL, 0, &value_string);
2882 die_if_error(error);
2883 if (!value_string) {
2884 vsctl_fatal("%s: missing value", arg);
2888 union ovsdb_atom key, value;
2889 struct ovsdb_datum datum;
2891 if (column->type.value.type == OVSDB_TYPE_VOID) {
2892 vsctl_fatal("cannot specify key to set for non-map column %s",
2896 die_if_error(ovsdb_atom_from_string(&key, &column->type.key,
2897 key_string, symtab));
2898 die_if_error(ovsdb_atom_from_string(&value, &column->type.value,
2899 value_string, symtab));
2901 ovsdb_datum_init_empty(&datum);
2902 ovsdb_datum_add_unsafe(&datum, &key, &value, &column->type);
2904 ovsdb_atom_destroy(&key, column->type.key.type);
2905 ovsdb_atom_destroy(&value, column->type.value.type);
2907 ovsdb_datum_union(&datum, ovsdb_idl_read(row, column),
2908 &column->type, false);
2909 ovsdb_idl_txn_write(row, column, &datum);
2911 struct ovsdb_datum datum;
2913 die_if_error(ovsdb_datum_from_string(&datum, &column->type,
2914 value_string, symtab));
2915 ovsdb_idl_txn_write(row, column, &datum);
2923 cmd_set(struct vsctl_context *ctx)
2925 const char *table_name = ctx->argv[1];
2926 const char *record_id = ctx->argv[2];
2927 const struct vsctl_table_class *table;
2928 const struct ovsdb_idl_row *row;
2931 table = get_table(table_name);
2932 row = must_get_row(ctx, table, record_id);
2933 for (i = 3; i < ctx->argc; i++) {
2934 set_column(table, row, ctx->argv[i], ctx->symtab);
2939 pre_cmd_add(struct vsctl_context *ctx)
2941 const char *table_name = ctx->argv[1];
2942 const char *column_name = ctx->argv[3];
2943 const struct vsctl_table_class *table;
2944 const struct ovsdb_idl_column *column;
2946 table = pre_get_table(ctx, table_name);
2947 pre_get_column(ctx, table, column_name, &column);
2951 cmd_add(struct vsctl_context *ctx)
2953 const char *table_name = ctx->argv[1];
2954 const char *record_id = ctx->argv[2];
2955 const char *column_name = ctx->argv[3];
2956 const struct vsctl_table_class *table;
2957 const struct ovsdb_idl_column *column;
2958 const struct ovsdb_idl_row *row;
2959 const struct ovsdb_type *type;
2960 struct ovsdb_datum old;
2963 table = get_table(table_name);
2964 row = must_get_row(ctx, table, record_id);
2965 die_if_error(get_column(table, column_name, &column));
2967 type = &column->type;
2968 ovsdb_datum_clone(&old, ovsdb_idl_read(row, column), &column->type);
2969 for (i = 4; i < ctx->argc; i++) {
2970 struct ovsdb_type add_type;
2971 struct ovsdb_datum add;
2975 add_type.n_max = UINT_MAX;
2976 die_if_error(ovsdb_datum_from_string(&add, &add_type, ctx->argv[i],
2978 ovsdb_datum_union(&old, &add, type, false);
2979 ovsdb_datum_destroy(&add, type);
2981 if (old.n > type->n_max) {
2982 vsctl_fatal("\"add\" operation would put %u %s in column %s of "
2983 "table %s but the maximum number is %u",
2985 type->value.type == OVSDB_TYPE_VOID ? "values" : "pairs",
2986 column->name, table->class->name, type->n_max);
2988 ovsdb_idl_txn_verify(row, column);
2989 ovsdb_idl_txn_write(row, column, &old);
2993 pre_cmd_remove(struct vsctl_context *ctx)
2995 const char *table_name = ctx->argv[1];
2996 const char *column_name = ctx->argv[3];
2997 const struct vsctl_table_class *table;
2998 const struct ovsdb_idl_column *column;
3000 table = pre_get_table(ctx, table_name);
3001 pre_get_column(ctx, table, column_name, &column);
3005 cmd_remove(struct vsctl_context *ctx)
3007 const char *table_name = ctx->argv[1];
3008 const char *record_id = ctx->argv[2];
3009 const char *column_name = ctx->argv[3];
3010 const struct vsctl_table_class *table;
3011 const struct ovsdb_idl_column *column;
3012 const struct ovsdb_idl_row *row;
3013 const struct ovsdb_type *type;
3014 struct ovsdb_datum old;
3017 table = get_table(table_name);
3018 row = must_get_row(ctx, table, record_id);
3019 die_if_error(get_column(table, column_name, &column));
3021 type = &column->type;
3022 ovsdb_datum_clone(&old, ovsdb_idl_read(row, column), &column->type);
3023 for (i = 4; i < ctx->argc; i++) {
3024 struct ovsdb_type rm_type;
3025 struct ovsdb_datum rm;
3030 rm_type.n_max = UINT_MAX;
3031 error = ovsdb_datum_from_string(&rm, &rm_type,
3032 ctx->argv[i], ctx->symtab);
3033 if (error && ovsdb_type_is_map(&rm_type)) {
3035 rm_type.value.type = OVSDB_TYPE_VOID;
3036 die_if_error(ovsdb_datum_from_string(&rm, &rm_type,
3037 ctx->argv[i], ctx->symtab));
3039 ovsdb_datum_subtract(&old, type, &rm, &rm_type);
3040 ovsdb_datum_destroy(&rm, &rm_type);
3042 if (old.n < type->n_min) {
3043 vsctl_fatal("\"remove\" operation would put %u %s in column %s of "
3044 "table %s but the minimum number is %u",
3046 type->value.type == OVSDB_TYPE_VOID ? "values" : "pairs",
3047 column->name, table->class->name, type->n_min);
3049 ovsdb_idl_txn_verify(row, column);
3050 ovsdb_idl_txn_write(row, column, &old);
3054 pre_cmd_clear(struct vsctl_context *ctx)
3056 const char *table_name = ctx->argv[1];
3057 const struct vsctl_table_class *table;
3060 table = pre_get_table(ctx, table_name);
3061 for (i = 3; i < ctx->argc; i++) {
3062 const struct ovsdb_idl_column *column;
3064 pre_get_column(ctx, table, ctx->argv[i], &column);
3069 cmd_clear(struct vsctl_context *ctx)
3071 const char *table_name = ctx->argv[1];
3072 const char *record_id = ctx->argv[2];
3073 const struct vsctl_table_class *table;
3074 const struct ovsdb_idl_row *row;
3077 table = get_table(table_name);
3078 row = must_get_row(ctx, table, record_id);
3079 for (i = 3; i < ctx->argc; i++) {
3080 const struct ovsdb_idl_column *column;
3081 const struct ovsdb_type *type;
3082 struct ovsdb_datum datum;
3084 die_if_error(get_column(table, ctx->argv[i], &column));
3086 type = &column->type;
3087 if (type->n_min > 0) {
3088 vsctl_fatal("\"clear\" operation cannot be applied to column %s "
3089 "of table %s, which is not allowed to be empty",
3090 column->name, table->class->name);
3093 ovsdb_datum_init_empty(&datum);
3094 ovsdb_idl_txn_write(row, column, &datum);
3099 pre_create(struct vsctl_context *ctx)
3101 const char *id = shash_find_data(&ctx->options, "--id");
3102 const char *table_name = ctx->argv[1];
3103 const struct vsctl_table_class *table;
3105 table = get_table(table_name);
3106 if (!id && !table->class->is_root) {
3107 VLOG_WARN("applying \"create\" command to table %s without --id "
3108 "option will have no effect", table->class->name);
3113 cmd_create(struct vsctl_context *ctx)
3115 const char *id = shash_find_data(&ctx->options, "--id");
3116 const char *table_name = ctx->argv[1];
3117 const struct vsctl_table_class *table = get_table(table_name);
3118 const struct ovsdb_idl_row *row;
3119 const struct uuid *uuid;
3123 struct ovsdb_symbol *symbol = create_symbol(ctx->symtab, id, NULL);
3124 if (table->class->is_root) {
3125 /* This table is in the root set, meaning that rows created in it
3126 * won't disappear even if they are unreferenced, so disable
3127 * warnings about that by pretending that there is a reference. */
3128 symbol->strong_ref = true;
3130 uuid = &symbol->uuid;
3135 row = ovsdb_idl_txn_insert(ctx->txn, table->class, uuid);
3136 for (i = 2; i < ctx->argc; i++) {
3137 set_column(table, row, ctx->argv[i], ctx->symtab);
3139 ds_put_format(&ctx->output, UUID_FMT, UUID_ARGS(&row->uuid));
3142 /* This function may be used as the 'postprocess' function for commands that
3143 * insert new rows into the database. It expects that the command's 'run'
3144 * function prints the UUID reported by ovsdb_idl_txn_insert() as the command's
3145 * sole output. It replaces that output by the row's permanent UUID assigned
3146 * by the database server and appends a new-line.
3148 * Currently we use this only for "create", because the higher-level commands
3149 * are supposed to be independent of the actual structure of the vswitch
3152 post_create(struct vsctl_context *ctx)
3154 const struct uuid *real;
3157 if (!uuid_from_string(&dummy, ds_cstr(&ctx->output))) {
3160 real = ovsdb_idl_txn_get_insert_uuid(ctx->txn, &dummy);
3162 ds_clear(&ctx->output);
3163 ds_put_format(&ctx->output, UUID_FMT, UUID_ARGS(real));
3165 ds_put_char(&ctx->output, '\n');
3169 pre_cmd_destroy(struct vsctl_context *ctx)
3171 const char *table_name = ctx->argv[1];
3173 pre_get_table(ctx, table_name);
3177 cmd_destroy(struct vsctl_context *ctx)
3179 bool must_exist = !shash_find(&ctx->options, "--if-exists");
3180 const char *table_name = ctx->argv[1];
3181 const struct vsctl_table_class *table;
3184 table = get_table(table_name);
3185 for (i = 2; i < ctx->argc; i++) {
3186 const struct ovsdb_idl_row *row;
3188 row = (must_exist ? must_get_row : get_row)(ctx, table, ctx->argv[i]);
3190 ovsdb_idl_txn_delete(row);
3196 is_condition_satisfied(const struct vsctl_table_class *table,
3197 const struct ovsdb_idl_row *row, const char *arg,
3198 struct ovsdb_symbol_table *symtab)
3200 static const char *operators[] = {
3201 "=", "!=", "<", ">", "<=", ">="
3204 const struct ovsdb_idl_column *column;
3205 const struct ovsdb_datum *have_datum;
3206 char *key_string, *value_string;
3207 const char *operator;
3212 error = parse_column_key_value(arg, table, &column, &key_string,
3213 &operator, operators, ARRAY_SIZE(operators),
3215 die_if_error(error);
3216 if (!value_string) {
3217 vsctl_fatal("%s: missing value", arg);
3220 have_datum = ovsdb_idl_read(row, column);
3222 union ovsdb_atom want_key, want_value;
3224 if (column->type.value.type == OVSDB_TYPE_VOID) {
3225 vsctl_fatal("cannot specify key to check for non-map column %s",
3229 die_if_error(ovsdb_atom_from_string(&want_key, &column->type.key,
3230 key_string, symtab));
3231 die_if_error(ovsdb_atom_from_string(&want_value, &column->type.value,
3232 value_string, symtab));
3234 idx = ovsdb_datum_find_key(have_datum,
3235 &want_key, column->type.key.type);
3236 if (idx != UINT_MAX) {
3237 cmp = ovsdb_atom_compare_3way(&have_datum->values[idx],
3239 column->type.value.type);
3242 ovsdb_atom_destroy(&want_key, column->type.key.type);
3243 ovsdb_atom_destroy(&want_value, column->type.value.type);
3245 struct ovsdb_datum want_datum;
3247 die_if_error(ovsdb_datum_from_string(&want_datum, &column->type,
3248 value_string, symtab));
3250 cmp = ovsdb_datum_compare_3way(have_datum, &want_datum,
3252 ovsdb_datum_destroy(&want_datum, &column->type);
3258 return (idx == UINT_MAX ? false
3259 : !strcmp(operator, "=") ? cmp == 0
3260 : !strcmp(operator, "!=") ? cmp != 0
3261 : !strcmp(operator, "<") ? cmp < 0
3262 : !strcmp(operator, ">") ? cmp > 0
3263 : !strcmp(operator, "<=") ? cmp <= 0
3264 : !strcmp(operator, ">=") ? cmp >= 0
3269 pre_cmd_wait_until(struct vsctl_context *ctx)
3271 const char *table_name = ctx->argv[1];
3272 const struct vsctl_table_class *table;
3275 table = pre_get_table(ctx, table_name);
3277 for (i = 3; i < ctx->argc; i++) {
3278 pre_parse_column_key_value(ctx, ctx->argv[i], table);
3283 cmd_wait_until(struct vsctl_context *ctx)
3285 const char *table_name = ctx->argv[1];
3286 const char *record_id = ctx->argv[2];
3287 const struct vsctl_table_class *table;
3288 const struct ovsdb_idl_row *row;
3291 table = get_table(table_name);
3293 row = get_row(ctx, table, record_id);
3295 ctx->try_again = true;
3299 for (i = 3; i < ctx->argc; i++) {
3300 if (!is_condition_satisfied(table, row, ctx->argv[i], ctx->symtab)) {
3301 ctx->try_again = true;
3307 static struct json *
3308 where_uuid_equals(const struct uuid *uuid)
3311 json_array_create_1(
3312 json_array_create_3(
3313 json_string_create("_uuid"),
3314 json_string_create("=="),
3315 json_array_create_2(
3316 json_string_create("uuid"),
3317 json_string_create_nocopy(
3318 xasprintf(UUID_FMT, UUID_ARGS(uuid))))));
3322 vsctl_context_init(struct vsctl_context *ctx, struct vsctl_command *command,
3323 struct ovsdb_idl *idl, struct ovsdb_idl_txn *txn,
3324 const struct ovsrec_open_vswitch *ovs,
3325 struct ovsdb_symbol_table *symtab)
3327 ctx->argc = command->argc;
3328 ctx->argv = command->argv;
3329 ctx->options = command->options;
3331 ds_swap(&ctx->output, &command->output);
3332 ctx->table = command->table;
3336 ctx->symtab = symtab;
3337 ctx->verified_ports = false;
3339 ctx->try_again = false;
3343 vsctl_context_done(struct vsctl_context *ctx, struct vsctl_command *command)
3345 ds_swap(&ctx->output, &command->output);
3346 command->table = ctx->table;
3350 run_prerequisites(struct vsctl_command *commands, size_t n_commands,
3351 struct ovsdb_idl *idl)
3353 struct vsctl_command *c;
3355 ovsdb_idl_add_table(idl, &ovsrec_table_open_vswitch);
3356 if (wait_for_reload) {
3357 ovsdb_idl_add_column(idl, &ovsrec_open_vswitch_col_cur_cfg);
3359 for (c = commands; c < &commands[n_commands]; c++) {
3360 if (c->syntax->prerequisites) {
3361 struct vsctl_context ctx;
3363 ds_init(&c->output);
3366 vsctl_context_init(&ctx, c, idl, NULL, NULL, NULL);
3367 (c->syntax->prerequisites)(&ctx);
3368 vsctl_context_done(&ctx, c);
3370 assert(!c->output.string);
3377 do_vsctl(const char *args, struct vsctl_command *commands, size_t n_commands,
3378 struct ovsdb_idl *idl)
3380 struct ovsdb_idl_txn *txn;
3381 const struct ovsrec_open_vswitch *ovs;
3382 enum ovsdb_idl_txn_status status;
3383 struct ovsdb_symbol_table *symtab;
3384 struct vsctl_command *c;
3385 struct shash_node *node;
3386 int64_t next_cfg = 0;
3389 txn = the_idl_txn = ovsdb_idl_txn_create(idl);
3391 ovsdb_idl_txn_set_dry_run(txn);
3394 ovsdb_idl_txn_add_comment(txn, "ovs-vsctl: %s", args);
3396 ovs = ovsrec_open_vswitch_first(idl);
3398 /* XXX add verification that table is empty */
3399 ovs = ovsrec_open_vswitch_insert(txn);
3402 if (wait_for_reload) {
3403 struct json *where = where_uuid_equals(&ovs->header_.uuid);
3404 ovsdb_idl_txn_increment(txn, "Open_vSwitch", "next_cfg", where);
3405 json_destroy(where);
3408 symtab = ovsdb_symbol_table_create();
3409 for (c = commands; c < &commands[n_commands]; c++) {
3410 ds_init(&c->output);
3413 for (c = commands; c < &commands[n_commands]; c++) {
3414 struct vsctl_context ctx;
3416 vsctl_context_init(&ctx, c, idl, txn, ovs, symtab);
3417 (c->syntax->run)(&ctx);
3418 vsctl_context_done(&ctx, c);
3420 if (ctx.try_again) {
3425 SHASH_FOR_EACH (node, &symtab->sh) {
3426 struct ovsdb_symbol *symbol = node->data;
3427 if (!symbol->created) {
3428 vsctl_fatal("row id \"%s\" is referenced but never created (e.g. "
3429 "with \"-- --id=%s create ...\")",
3430 node->name, node->name);
3432 if (!symbol->strong_ref) {
3433 if (!symbol->weak_ref) {
3434 VLOG_WARN("row id \"%s\" was created but no reference to it "
3435 "was inserted, so it will not actually appear in "
3436 "the database", node->name);
3438 VLOG_WARN("row id \"%s\" was created but only a weak "
3439 "reference to it was inserted, so it will not "
3440 "actually appear in the database", node->name);
3445 status = ovsdb_idl_txn_commit_block(txn);
3446 if (wait_for_reload && status == TXN_SUCCESS) {
3447 next_cfg = ovsdb_idl_txn_get_increment_new_value(txn);
3449 if (status == TXN_UNCHANGED || status == TXN_SUCCESS) {
3450 for (c = commands; c < &commands[n_commands]; c++) {
3451 if (c->syntax->postprocess) {
3452 struct vsctl_context ctx;
3454 vsctl_context_init(&ctx, c, idl, txn, ovs, symtab);
3455 (c->syntax->postprocess)(&ctx);
3456 vsctl_context_done(&ctx, c);
3460 error = xstrdup(ovsdb_idl_txn_get_error(txn));
3461 ovsdb_idl_txn_destroy(txn);
3462 txn = the_idl_txn = NULL;
3465 case TXN_INCOMPLETE:
3469 /* Should not happen--we never call ovsdb_idl_txn_abort(). */
3470 vsctl_fatal("transaction aborted");
3480 vsctl_fatal("transaction error: %s", error);
3487 ovsdb_symbol_table_destroy(symtab);
3489 for (c = commands; c < &commands[n_commands]; c++) {
3490 struct ds *ds = &c->output;
3493 table_print(c->table, &table_style);
3494 } else if (oneline) {
3498 for (j = 0; j < ds->length; j++) {
3499 int ch = ds->string[j];
3502 fputs("\\n", stdout);
3506 fputs("\\\\", stdout);
3515 fputs(ds_cstr(ds), stdout);
3517 ds_destroy(&c->output);
3518 table_destroy(c->table);
3521 smap_destroy(&c->options);
3525 if (wait_for_reload && status != TXN_UNCHANGED) {
3528 OVSREC_OPEN_VSWITCH_FOR_EACH (ovs, idl) {
3529 if (ovs->cur_cfg >= next_cfg) {
3533 ovsdb_idl_wait(idl);
3538 ovsdb_idl_destroy(idl);
3543 /* Our transaction needs to be rerun, or a prerequisite was not met. Free
3544 * resources and return so that the caller can try again. */
3546 ovsdb_idl_txn_abort(txn);
3547 ovsdb_idl_txn_destroy(txn);
3549 ovsdb_symbol_table_destroy(symtab);
3550 for (c = commands; c < &commands[n_commands]; c++) {
3551 ds_destroy(&c->output);
3552 table_destroy(c->table);
3558 static const struct vsctl_command_syntax all_commands[] = {
3559 /* Open vSwitch commands. */
3560 {"init", 0, 0, NULL, cmd_init, NULL, "", RW},
3562 /* Bridge commands. */
3563 {"add-br", 1, 3, pre_get_info, cmd_add_br, NULL, "--may-exist", RW},
3564 {"del-br", 1, 1, pre_get_info, cmd_del_br, NULL, "--if-exists", RW},
3565 {"list-br", 0, 0, pre_get_info, cmd_list_br, NULL, "", RO},
3566 {"br-exists", 1, 1, pre_get_info, cmd_br_exists, NULL, "", RO},
3567 {"br-to-vlan", 1, 1, pre_get_info, cmd_br_to_vlan, NULL, "", RO},
3568 {"br-to-parent", 1, 1, pre_get_info, cmd_br_to_parent, NULL, "", RO},
3569 {"br-set-external-id", 2, 3, pre_cmd_br_set_external_id,
3570 cmd_br_set_external_id, NULL, "", RW},
3571 {"br-get-external-id", 1, 2, pre_cmd_br_get_external_id,
3572 cmd_br_get_external_id, NULL, "", RO},
3574 /* Port commands. */
3575 {"list-ports", 1, 1, pre_get_info, cmd_list_ports, NULL, "", RO},
3576 {"add-port", 2, INT_MAX, pre_get_info, cmd_add_port, NULL, "--may-exist",
3578 {"add-bond", 4, INT_MAX, pre_get_info, cmd_add_bond, NULL,
3579 "--may-exist,--fake-iface", RW},
3580 {"del-port", 1, 2, pre_get_info, cmd_del_port, NULL,
3581 "--if-exists,--with-iface", RW},
3582 {"port-to-br", 1, 1, pre_get_info, cmd_port_to_br, NULL, "", RO},
3584 /* Interface commands. */
3585 {"list-ifaces", 1, 1, pre_get_info, cmd_list_ifaces, NULL, "", RO},
3586 {"iface-to-br", 1, 1, pre_get_info, cmd_iface_to_br, NULL, "", RO},
3588 /* Controller commands. */
3589 {"get-controller", 1, 1, pre_controller, cmd_get_controller, NULL, "", RO},
3590 {"del-controller", 1, 1, pre_controller, cmd_del_controller, NULL, "", RW},
3591 {"set-controller", 1, INT_MAX, pre_controller, cmd_set_controller, NULL,
3593 {"get-fail-mode", 1, 1, pre_get_info, cmd_get_fail_mode, NULL, "", RO},
3594 {"del-fail-mode", 1, 1, pre_get_info, cmd_del_fail_mode, NULL, "", RW},
3595 {"set-fail-mode", 2, 2, pre_get_info, cmd_set_fail_mode, NULL, "", RW},
3597 /* Manager commands. */
3598 {"get-manager", 0, 0, pre_manager, cmd_get_manager, NULL, "", RO},
3599 {"del-manager", 0, INT_MAX, pre_manager, cmd_del_manager, NULL, "", RW},
3600 {"set-manager", 1, INT_MAX, pre_manager, cmd_set_manager, NULL, "", RW},
3603 {"get-ssl", 0, 0, pre_cmd_get_ssl, cmd_get_ssl, NULL, "", RO},
3604 {"del-ssl", 0, 0, pre_cmd_del_ssl, cmd_del_ssl, NULL, "", RW},
3605 {"set-ssl", 3, 3, pre_cmd_set_ssl, cmd_set_ssl, NULL, "--bootstrap", RW},
3607 /* Switch commands. */
3608 {"emer-reset", 0, 0, pre_cmd_emer_reset, cmd_emer_reset, NULL, "", RW},
3610 /* Parameter commands. */
3611 {"get", 2, INT_MAX, pre_cmd_get, cmd_get, NULL, "--if-exists,--id=", RO},
3612 {"list", 1, INT_MAX, pre_cmd_list, cmd_list, NULL, "--columns=", RO},
3613 {"find", 1, INT_MAX, pre_cmd_find, cmd_find, NULL, "--columns=", RO},
3614 {"set", 3, INT_MAX, pre_cmd_set, cmd_set, NULL, "", RW},
3615 {"add", 4, INT_MAX, pre_cmd_add, cmd_add, NULL, "", RW},
3616 {"remove", 4, INT_MAX, pre_cmd_remove, cmd_remove, NULL, "", RW},
3617 {"clear", 3, INT_MAX, pre_cmd_clear, cmd_clear, NULL, "", RW},
3618 {"create", 2, INT_MAX, pre_create, cmd_create, post_create, "--id=", RW},
3619 {"destroy", 1, INT_MAX, pre_cmd_destroy, cmd_destroy, NULL, "--if-exists",
3621 {"wait-until", 2, INT_MAX, pre_cmd_wait_until, cmd_wait_until, NULL, "",
3624 {NULL, 0, 0, NULL, NULL, NULL, NULL, RO},