2 * Copyright (c) 2009, 2010 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"
39 #include "stream-ssl.h"
41 #include "vswitchd/vswitch-idl.h"
46 VLOG_DEFINE_THIS_MODULE(vsctl)
48 /* vsctl_fatal() also logs the error, so it is preferred in this file. */
49 #define ovs_fatal please_use_vsctl_fatal_instead_of_ovs_fatal
53 typedef void vsctl_handler_func(struct vsctl_context *);
55 struct vsctl_command_syntax {
59 vsctl_handler_func *run;
60 vsctl_handler_func *postprocess;
64 struct vsctl_command {
65 /* Data that remains constant after initialization. */
66 const struct vsctl_command_syntax *syntax;
71 /* Data modified by commands. */
75 /* --db: The database server to contact. */
76 static const char *db;
78 /* --oneline: Write each command's output as a single line? */
81 /* --dry-run: Do not commit any changes. */
84 /* --no-wait: Wait for ovs-vswitchd to reload its configuration? */
85 static bool wait_for_reload = true;
87 /* --timeout: Time to wait for a connection to 'db'. */
90 /* All supported commands. */
91 static const struct vsctl_command_syntax all_commands[];
93 /* The IDL we're using and the current transaction, if any.
94 * This is for use by vsctl_exit() only, to allow it to clean up.
95 * Other code should use its context arguments. */
96 static struct ovsdb_idl *the_idl;
97 static struct ovsdb_idl_txn *the_idl_txn;
99 static void vsctl_exit(int status) NO_RETURN;
100 static void vsctl_fatal(const char *, ...) PRINTF_FORMAT(1, 2) NO_RETURN;
101 static char *default_db(void);
102 static void usage(void) NO_RETURN;
103 static void parse_options(int argc, char *argv[]);
105 static struct vsctl_command *parse_commands(int argc, char *argv[],
106 size_t *n_commandsp);
107 static void parse_command(int argc, char *argv[], struct vsctl_command *);
108 static void do_vsctl(const char *args,
109 struct vsctl_command *, size_t n_commands,
112 static const struct vsctl_table_class *get_table(const char *table_name);
113 static void set_column(const struct vsctl_table_class *,
114 const struct ovsdb_idl_row *, const char *arg,
115 struct ovsdb_symbol_table *);
119 main(int argc, char *argv[])
121 extern struct vlog_module VLM_reconnect;
122 struct ovsdb_idl *idl;
123 struct vsctl_command *commands;
127 set_program_name(argv[0]);
128 signal(SIGPIPE, SIG_IGN);
129 vlog_set_levels(NULL, VLF_CONSOLE, VLL_WARN);
130 vlog_set_levels(&VLM_reconnect, VLF_ANY_FACILITY, VLL_WARN);
133 /* Log our arguments. This is often valuable for debugging systems. */
134 args = process_escape_args(argv);
135 VLOG_INFO("Called as %s", args);
137 /* Parse command line. */
138 parse_options(argc, argv);
139 commands = parse_commands(argc - optind, argv + optind, &n_commands);
145 /* Now execute the commands. */
146 idl = the_idl = ovsdb_idl_create(db, &ovsrec_idl_class);
148 if (ovsdb_idl_run(idl)) {
149 do_vsctl(args, commands, n_commands, idl);
158 parse_options(int argc, char *argv[])
161 OPT_DB = UCHAR_MAX + 1,
169 static struct option long_options[] = {
170 {"db", required_argument, 0, OPT_DB},
171 {"no-syslog", no_argument, 0, OPT_NO_SYSLOG},
172 {"no-wait", no_argument, 0, OPT_NO_WAIT},
173 {"dry-run", no_argument, 0, OPT_DRY_RUN},
174 {"oneline", no_argument, 0, OPT_ONELINE},
175 {"timeout", required_argument, 0, 't'},
176 {"help", no_argument, 0, 'h'},
177 {"version", no_argument, 0, 'V'},
180 STREAM_SSL_LONG_OPTIONS
181 {"peer-ca-cert", required_argument, 0, OPT_PEER_CA_CERT},
185 char *tmp, *short_options;
187 tmp = long_options_to_short_options(long_options);
188 short_options = xasprintf("+%s", tmp);
194 c = getopt_long(argc, argv, short_options, long_options, NULL);
209 vlog_set_levels(&VLM_vsctl, VLF_SYSLOG, VLL_WARN);
213 wait_for_reload = false;
224 OVS_PRINT_VERSION(0, 0);
228 timeout = strtoul(optarg, NULL, 10);
230 vsctl_fatal("value %s on -t or --timeout is invalid",
238 STREAM_SSL_OPTION_HANDLERS
240 case OPT_PEER_CA_CERT:
241 stream_ssl_set_peer_ca_cert_file(optarg);
259 static struct vsctl_command *
260 parse_commands(int argc, char *argv[], size_t *n_commandsp)
262 struct vsctl_command *commands;
263 size_t n_commands, allocated_commands;
267 n_commands = allocated_commands = 0;
269 for (start = i = 0; i <= argc; i++) {
270 if (i == argc || !strcmp(argv[i], "--")) {
272 if (n_commands >= allocated_commands) {
273 struct vsctl_command *c;
275 commands = x2nrealloc(commands, &allocated_commands,
277 for (c = commands; c < &commands[n_commands]; c++) {
278 shash_moved(&c->options);
281 parse_command(i - start, &argv[start],
282 &commands[n_commands++]);
288 vsctl_fatal("missing command name (use --help for help)");
290 *n_commandsp = n_commands;
295 parse_command(int argc, char *argv[], struct vsctl_command *command)
297 const struct vsctl_command_syntax *p;
300 shash_init(&command->options);
301 for (i = 0; i < argc; i++) {
302 const char *option = argv[i];
306 if (option[0] != '-') {
310 equals = strchr(option, '=');
312 key = xmemdup0(option, equals - option);
313 value = xstrdup(equals + 1);
315 key = xstrdup(option);
319 if (shash_find(&command->options, key)) {
320 vsctl_fatal("'%s' option specified multiple times", argv[i]);
322 shash_add_nocopy(&command->options, key, value);
325 vsctl_fatal("missing command name");
328 for (p = all_commands; p->name; p++) {
329 if (!strcmp(p->name, argv[i])) {
330 struct shash_node *node;
333 SHASH_FOR_EACH (node, &command->options) {
334 const char *s = strstr(p->options, node->name);
335 int end = s ? s[strlen(node->name)] : EOF;
337 if (end != '=' && end != ',' && end != ' ' && end != '\0') {
338 vsctl_fatal("'%s' command has no '%s' option",
339 argv[i], node->name);
341 if ((end == '=') != (node->data != NULL)) {
343 vsctl_fatal("missing argument to '%s' option on '%s' "
344 "command", node->name, argv[i]);
346 vsctl_fatal("'%s' option on '%s' does not accept an "
347 "argument", node->name, argv[i]);
352 n_arg = argc - i - 1;
353 if (n_arg < p->min_args) {
354 vsctl_fatal("'%s' command requires at least %d arguments",
355 p->name, p->min_args);
356 } else if (n_arg > p->max_args) {
359 for (j = i + 1; j < argc; j++) {
360 if (argv[j][0] == '-') {
361 vsctl_fatal("'%s' command takes at most %d arguments "
362 "(note that options must precede command "
363 "names and follow a \"--\" argument)",
364 p->name, p->max_args);
368 vsctl_fatal("'%s' command takes at most %d arguments",
369 p->name, p->max_args);
372 command->argc = n_arg + 1;
373 command->argv = &argv[i];
379 vsctl_fatal("unknown command '%s'; use --help for help", argv[i]);
383 vsctl_fatal(const char *format, ...)
388 va_start(args, format);
389 message = xvasprintf(format, args);
392 vlog_set_levels(&VLM_vsctl, VLF_CONSOLE, VLL_EMER);
393 VLOG_ERR("%s", message);
394 ovs_error(0, "%s", message);
395 vsctl_exit(EXIT_FAILURE);
398 /* Frees the current transaction and the underlying IDL and then calls
401 * Freeing the transaction and the IDL is not strictly necessary, but it makes
402 * for a clean memory leak report from valgrind in the normal case. That makes
403 * it easier to notice real memory leaks. */
405 vsctl_exit(int status)
408 ovsdb_idl_txn_abort(the_idl_txn);
409 ovsdb_idl_txn_destroy(the_idl_txn);
411 ovsdb_idl_destroy(the_idl);
419 %s: ovs-vswitchd management utility\n\
420 usage: %s [OPTIONS] COMMAND [ARG...]\n\
423 add-br BRIDGE create a new bridge named BRIDGE\n\
424 add-br BRIDGE PARENT VLAN create new fake BRIDGE in PARENT on VLAN\n\
425 del-br BRIDGE delete BRIDGE and all of its ports\n\
426 list-br print the names of all the bridges\n\
427 br-exists BRIDGE test whether BRIDGE exists\n\
428 br-to-vlan BRIDGE print the VLAN which BRIDGE is on\n\
429 br-to-parent BRIDGE print the parent of BRIDGE\n\
430 br-set-external-id BRIDGE KEY VALUE set KEY on BRIDGE to VALUE\n\
431 br-set-external-id BRIDGE KEY unset KEY on BRIDGE\n\
432 br-get-external-id BRIDGE KEY print value of KEY on BRIDGE\n\
433 br-get-external-id BRIDGE list key-value pairs on BRIDGE\n\
436 list-ports BRIDGE print the names of all the ports on BRIDGE\n\
437 add-port BRIDGE PORT add network device PORT to BRIDGE\n\
438 add-bond BRIDGE PORT IFACE... add bonded port PORT in BRIDGE from IFACES\n\
439 del-port [BRIDGE] PORT delete PORT (which may be bonded) from BRIDGE\n\
440 port-to-br PORT print name of bridge that contains PORT\n\
441 A bond is considered to be a single port.\n\
443 Interface commands (a bond consists of multiple interfaces):\n\
444 list-ifaces BRIDGE print the names of all interfaces on BRIDGE\n\
445 iface-to-br IFACE print name of bridge that contains IFACE\n\
447 Controller commands:\n\
448 get-controller BRIDGE print the controller for BRIDGE\n\
449 del-controller BRIDGE delete the controller for BRIDGE\n\
450 set-controller BRIDGE TARGET set the controller for BRIDGE to TARGET\n\
451 get-fail-mode BRIDGE print the fail-mode for BRIDGE\n\
452 del-fail-mode BRIDGE delete the fail-mode for BRIDGE\n\
453 set-fail-mode BRIDGE MODE set the fail-mode for BRIDGE to MODE\n\
456 get-ssl print the SSL configuration\n\
457 del-ssl delete the SSL configuration\n\
458 set-ssl PRIV-KEY CERT CA-CERT set the SSL configuration\n\
461 emer-reset reset switch to known good state\n\
463 Database commands:\n\
464 list TBL [REC] list RECord (or all records) in TBL\n\
465 get TBL REC COL[:KEY] print values of COLumns in RECord in TBL\n\
466 set TBL REC COL[:KEY]=VALUE set COLumn values in RECord in TBL\n\
467 add TBL REC COL [KEY=]VALUE add (KEY=)VALUE to COLumn in RECord in TBL\n\
468 remove TBL REC COL [KEY=]VALUE remove (KEY=)VALUE from COLumn\n\
469 clear TBL REC COL clear values from COLumn in RECord in TBL\n\
470 create TBL COL[:KEY]=VALUE create and initialize new record\n\
471 destroy TBL REC delete RECord from TBL\n\
472 wait-until TBL REC [COL[:KEY]=VALUE] wait until condition is true\n\
473 Potentially unsafe database commands require --force option.\n\
476 --db=DATABASE connect to DATABASE\n\
478 --oneline print exactly one line of output per command\n",
479 program_name, program_name, default_db());
483 -h, --help display this help message\n\
484 -V, --version display version information\n");
493 def = xasprintf("unix:%s/db.sock", ovs_rundir);
498 struct vsctl_context {
502 struct shash options;
504 /* Modifiable state. */
506 struct ovsdb_idl *idl;
507 struct ovsdb_idl_txn *txn;
508 struct ovsdb_symbol_table *symtab;
509 const struct ovsrec_open_vswitch *ovs;
511 /* A command may set this member to true if some prerequisite is not met
512 * and the caller should wait for something to change and then retry. */
516 struct vsctl_bridge {
517 struct ovsrec_bridge *br_cfg;
519 struct ovsrec_controller **ctrl;
522 struct vsctl_bridge *parent;
527 struct ovsrec_port *port_cfg;
528 struct vsctl_bridge *bridge;
532 struct ovsrec_interface *iface_cfg;
533 struct vsctl_port *port;
537 struct shash bridges;
543 vsctl_context_to_string(const struct vsctl_context *ctx)
545 const struct shash_node *node;
551 SHASH_FOR_EACH (node, &ctx->options) {
552 svec_add(&words, node->name);
554 for (i = 0; i < ctx->argc; i++) {
555 svec_add(&words, ctx->argv[i]);
557 svec_terminate(&words);
559 s = process_escape_args(words.names);
561 svec_destroy(&words);
566 static struct vsctl_bridge *
567 add_bridge(struct vsctl_info *b,
568 struct ovsrec_bridge *br_cfg, const char *name,
569 struct vsctl_bridge *parent, int vlan)
571 struct vsctl_bridge *br = xmalloc(sizeof *br);
573 br->name = xstrdup(name);
577 br->ctrl = parent->br_cfg->controller;
578 br->n_ctrl = parent->br_cfg->n_controller;
579 br->fail_mode = parent->br_cfg->fail_mode;
581 br->ctrl = br_cfg->controller;
582 br->n_ctrl = br_cfg->n_controller;
583 br->fail_mode = br_cfg->fail_mode;
585 shash_add(&b->bridges, br->name, br);
590 port_is_fake_bridge(const struct ovsrec_port *port_cfg)
592 return (port_cfg->fake_bridge
594 && *port_cfg->tag >= 1 && *port_cfg->tag <= 4095);
597 static struct vsctl_bridge *
598 find_vlan_bridge(struct vsctl_info *info,
599 struct vsctl_bridge *parent, int vlan)
601 struct shash_node *node;
603 SHASH_FOR_EACH (node, &info->bridges) {
604 struct vsctl_bridge *br = node->data;
605 if (br->parent == parent && br->vlan == vlan) {
614 free_info(struct vsctl_info *info)
616 struct shash_node *node;
618 SHASH_FOR_EACH (node, &info->bridges) {
619 struct vsctl_bridge *bridge = node->data;
623 shash_destroy(&info->bridges);
625 shash_destroy_free_data(&info->ports);
626 shash_destroy_free_data(&info->ifaces);
630 get_info(const struct ovsrec_open_vswitch *ovs, struct vsctl_info *info)
632 struct shash bridges, ports;
635 shash_init(&info->bridges);
636 shash_init(&info->ports);
637 shash_init(&info->ifaces);
639 shash_init(&bridges);
641 for (i = 0; i < ovs->n_bridges; i++) {
642 struct ovsrec_bridge *br_cfg = ovs->bridges[i];
643 struct vsctl_bridge *br;
646 if (!shash_add_once(&bridges, br_cfg->name, NULL)) {
647 VLOG_WARN("%s: database contains duplicate bridge name",
651 br = add_bridge(info, br_cfg, br_cfg->name, NULL, 0);
656 for (j = 0; j < br_cfg->n_ports; j++) {
657 struct ovsrec_port *port_cfg = br_cfg->ports[j];
659 if (!shash_add_once(&ports, port_cfg->name, NULL)) {
660 VLOG_WARN("%s: database contains duplicate port name",
665 if (port_is_fake_bridge(port_cfg)
666 && shash_add_once(&bridges, port_cfg->name, NULL)) {
667 add_bridge(info, NULL, port_cfg->name, br, *port_cfg->tag);
671 shash_destroy(&bridges);
672 shash_destroy(&ports);
674 shash_init(&bridges);
676 for (i = 0; i < ovs->n_bridges; i++) {
677 struct ovsrec_bridge *br_cfg = ovs->bridges[i];
678 struct vsctl_bridge *br;
681 if (!shash_add_once(&bridges, br_cfg->name, NULL)) {
684 br = shash_find_data(&info->bridges, br_cfg->name);
685 for (j = 0; j < br_cfg->n_ports; j++) {
686 struct ovsrec_port *port_cfg = br_cfg->ports[j];
687 struct vsctl_port *port;
690 if (!shash_add_once(&ports, port_cfg->name, NULL)) {
694 if (port_is_fake_bridge(port_cfg)
695 && !shash_add_once(&bridges, port_cfg->name, NULL)) {
699 port = xmalloc(sizeof *port);
700 port->port_cfg = port_cfg;
702 && *port_cfg->tag >= 1 && *port_cfg->tag <= 4095) {
703 port->bridge = find_vlan_bridge(info, br, *port_cfg->tag);
710 shash_add(&info->ports, port_cfg->name, port);
712 for (k = 0; k < port_cfg->n_interfaces; k++) {
713 struct ovsrec_interface *iface_cfg = port_cfg->interfaces[k];
714 struct vsctl_iface *iface;
716 if (shash_find(&info->ifaces, iface_cfg->name)) {
717 VLOG_WARN("%s: database contains duplicate interface name",
722 iface = xmalloc(sizeof *iface);
723 iface->iface_cfg = iface_cfg;
725 shash_add(&info->ifaces, iface_cfg->name, iface);
729 shash_destroy(&bridges);
730 shash_destroy(&ports);
734 check_conflicts(struct vsctl_info *info, const char *name,
737 struct vsctl_iface *iface;
738 struct vsctl_port *port;
740 if (shash_find(&info->bridges, name)) {
741 vsctl_fatal("%s because a bridge named %s already exists",
745 port = shash_find_data(&info->ports, name);
747 vsctl_fatal("%s because a port named %s already exists on "
748 "bridge %s", msg, name, port->bridge->name);
751 iface = shash_find_data(&info->ifaces, name);
753 vsctl_fatal("%s because an interface named %s already exists "
754 "on bridge %s", msg, name, iface->port->bridge->name);
760 static struct vsctl_bridge *
761 find_bridge(struct vsctl_info *info, const char *name, bool must_exist)
763 struct vsctl_bridge *br = shash_find_data(&info->bridges, name);
764 if (must_exist && !br) {
765 vsctl_fatal("no bridge named %s", name);
770 static struct vsctl_bridge *
771 find_real_bridge(struct vsctl_info *info, const char *name, bool must_exist)
773 struct vsctl_bridge *br = find_bridge(info, name, must_exist);
774 if (br && br->parent) {
775 vsctl_fatal("%s is a fake bridge", name);
780 static struct vsctl_port *
781 find_port(struct vsctl_info *info, const char *name, bool must_exist)
783 struct vsctl_port *port = shash_find_data(&info->ports, name);
784 if (port && !strcmp(name, port->bridge->name)) {
787 if (must_exist && !port) {
788 vsctl_fatal("no port named %s", name);
793 static struct vsctl_iface *
794 find_iface(struct vsctl_info *info, const char *name, bool must_exist)
796 struct vsctl_iface *iface = shash_find_data(&info->ifaces, name);
797 if (iface && !strcmp(name, iface->port->bridge->name)) {
800 if (must_exist && !iface) {
801 vsctl_fatal("no interface named %s", name);
807 bridge_insert_port(struct ovsrec_bridge *br, struct ovsrec_port *port)
809 struct ovsrec_port **ports;
812 ports = xmalloc(sizeof *br->ports * (br->n_ports + 1));
813 for (i = 0; i < br->n_ports; i++) {
814 ports[i] = br->ports[i];
816 ports[br->n_ports] = port;
817 ovsrec_bridge_set_ports(br, ports, br->n_ports + 1);
822 bridge_delete_port(struct ovsrec_bridge *br, struct ovsrec_port *port)
824 struct ovsrec_port **ports;
827 ports = xmalloc(sizeof *br->ports * br->n_ports);
828 for (i = n = 0; i < br->n_ports; i++) {
829 if (br->ports[i] != port) {
830 ports[n++] = br->ports[i];
833 ovsrec_bridge_set_ports(br, ports, n);
838 ovs_insert_bridge(const struct ovsrec_open_vswitch *ovs,
839 struct ovsrec_bridge *bridge)
841 struct ovsrec_bridge **bridges;
844 bridges = xmalloc(sizeof *ovs->bridges * (ovs->n_bridges + 1));
845 for (i = 0; i < ovs->n_bridges; i++) {
846 bridges[i] = ovs->bridges[i];
848 bridges[ovs->n_bridges] = bridge;
849 ovsrec_open_vswitch_set_bridges(ovs, bridges, ovs->n_bridges + 1);
854 ovs_delete_bridge(const struct ovsrec_open_vswitch *ovs,
855 struct ovsrec_bridge *bridge)
857 struct ovsrec_bridge **bridges;
860 bridges = xmalloc(sizeof *ovs->bridges * ovs->n_bridges);
861 for (i = n = 0; i < ovs->n_bridges; i++) {
862 if (ovs->bridges[i] != bridge) {
863 bridges[n++] = ovs->bridges[i];
866 ovsrec_open_vswitch_set_bridges(ovs, bridges, n);
871 cmd_init(struct vsctl_context *ctx OVS_UNUSED)
876 cmd_emer_reset(struct vsctl_context *ctx)
878 const struct ovsdb_idl *idl = ctx->idl;
879 const struct ovsrec_bridge *br;
880 const struct ovsrec_port *port;
881 const struct ovsrec_interface *iface;
882 const struct ovsrec_mirror *mirror, *next_mirror;
883 const struct ovsrec_controller *ctrl, *next_ctrl;
884 const struct ovsrec_netflow *nf, *next_nf;
885 const struct ovsrec_ssl *ssl, *next_ssl;
886 const struct ovsrec_sflow *sflow, *next_sflow;
889 /* Reset the Open_vSwitch table. */
890 ovsrec_open_vswitch_set_managers(ctx->ovs, NULL, 0);
891 ovsrec_open_vswitch_set_ssl(ctx->ovs, NULL);
893 OVSREC_BRIDGE_FOR_EACH (br, idl) {
895 char *hw_key = "hwaddr";
898 ovsrec_bridge_set_controller(br, NULL, 0);
899 ovsrec_bridge_set_mirrors(br, NULL, 0);
900 ovsrec_bridge_set_netflow(br, NULL);
901 ovsrec_bridge_set_sflow(br, NULL);
902 ovsrec_bridge_set_flood_vlans(br, NULL, 0);
904 /* We only want to save the "hwaddr" key from other_config. */
905 for (i=0; i < br->n_other_config; i++) {
906 if (!strcmp(br->key_other_config[i], hw_key)) {
907 hw_val = br->value_other_config[i];
912 char *val = xstrdup(hw_val);
913 ovsrec_bridge_set_other_config(br, &hw_key, &val, 1);
916 ovsrec_bridge_set_other_config(br, NULL, NULL, 0);
920 OVSREC_PORT_FOR_EACH (port, idl) {
921 ovsrec_port_set_other_config(port, NULL, NULL, 0);
924 OVSREC_INTERFACE_FOR_EACH (iface, idl) {
925 /* xxx What do we do about gre/patch devices created by mgr? */
927 ovsrec_interface_set_ingress_policing_rate(iface, 0);
928 ovsrec_interface_set_ingress_policing_burst(iface, 0);
931 OVSREC_MIRROR_FOR_EACH_SAFE (mirror, next_mirror, idl) {
932 ovsrec_mirror_delete(mirror);
935 OVSREC_CONTROLLER_FOR_EACH_SAFE (ctrl, next_ctrl, idl) {
936 ovsrec_controller_delete(ctrl);
939 OVSREC_NETFLOW_FOR_EACH_SAFE (nf, next_nf, idl) {
940 ovsrec_netflow_delete(nf);
943 OVSREC_SSL_FOR_EACH_SAFE (ssl, next_ssl, idl) {
944 ovsrec_ssl_delete(ssl);
947 OVSREC_SFLOW_FOR_EACH_SAFE (sflow, next_sflow, idl) {
948 ovsrec_sflow_delete(sflow);
953 cmd_add_br(struct vsctl_context *ctx)
955 bool may_exist = shash_find(&ctx->options, "--may-exist") != 0;
956 const char *br_name, *parent_name;
957 struct vsctl_info info;
960 br_name = ctx->argv[1];
961 if (ctx->argc == 2) {
964 } else if (ctx->argc == 4) {
965 parent_name = ctx->argv[2];
966 vlan = atoi(ctx->argv[3]);
967 if (vlan < 1 || vlan > 4095) {
968 vsctl_fatal("%s: vlan must be between 1 and 4095", ctx->argv[0]);
971 vsctl_fatal("'%s' command takes exactly 1 or 3 arguments",
975 get_info(ctx->ovs, &info);
977 struct vsctl_bridge *br;
979 br = find_bridge(&info, br_name, false);
983 vsctl_fatal("\"--may-exist add-br %s\" but %s is "
984 "a VLAN bridge for VLAN %d",
985 br_name, br_name, br->vlan);
989 vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
990 "is not a VLAN bridge",
991 br_name, parent_name, vlan, br_name);
992 } else if (strcmp(br->parent->name, parent_name)) {
993 vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
994 "has the wrong parent %s",
995 br_name, parent_name, vlan,
996 br_name, br->parent->name);
997 } else if (br->vlan != vlan) {
998 vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
999 "is a VLAN bridge for the wrong VLAN %d",
1000 br_name, parent_name, vlan, br_name, br->vlan);
1006 check_conflicts(&info, br_name,
1007 xasprintf("cannot create a bridge named %s", br_name));
1010 struct ovsrec_port *port;
1011 struct ovsrec_interface *iface;
1012 struct ovsrec_bridge *br;
1014 iface = ovsrec_interface_insert(ctx->txn);
1015 ovsrec_interface_set_name(iface, br_name);
1017 port = ovsrec_port_insert(ctx->txn);
1018 ovsrec_port_set_name(port, br_name);
1019 ovsrec_port_set_interfaces(port, &iface, 1);
1021 br = ovsrec_bridge_insert(ctx->txn);
1022 ovsrec_bridge_set_name(br, br_name);
1023 ovsrec_bridge_set_ports(br, &port, 1);
1025 ovs_insert_bridge(ctx->ovs, br);
1027 struct vsctl_bridge *parent;
1028 struct ovsrec_port *port;
1029 struct ovsrec_interface *iface;
1030 struct ovsrec_bridge *br;
1033 parent = find_bridge(&info, parent_name, false);
1034 if (parent && parent->vlan) {
1035 vsctl_fatal("cannot create bridge with fake bridge as parent");
1038 vsctl_fatal("parent bridge %s does not exist", parent_name);
1040 br = parent->br_cfg;
1042 iface = ovsrec_interface_insert(ctx->txn);
1043 ovsrec_interface_set_name(iface, br_name);
1044 ovsrec_interface_set_type(iface, "internal");
1046 port = ovsrec_port_insert(ctx->txn);
1047 ovsrec_port_set_name(port, br_name);
1048 ovsrec_port_set_interfaces(port, &iface, 1);
1049 ovsrec_port_set_fake_bridge(port, true);
1050 ovsrec_port_set_tag(port, &tag, 1);
1052 bridge_insert_port(br, port);
1059 del_port(struct vsctl_info *info, struct vsctl_port *port)
1061 struct shash_node *node;
1063 SHASH_FOR_EACH (node, &info->ifaces) {
1064 struct vsctl_iface *iface = node->data;
1065 if (iface->port == port) {
1066 ovsrec_interface_delete(iface->iface_cfg);
1069 ovsrec_port_delete(port->port_cfg);
1071 bridge_delete_port((port->bridge->parent
1072 ? port->bridge->parent->br_cfg
1073 : port->bridge->br_cfg), port->port_cfg);
1077 cmd_del_br(struct vsctl_context *ctx)
1079 bool must_exist = !shash_find(&ctx->options, "--if-exists");
1080 struct vsctl_bridge *bridge;
1081 struct vsctl_info info;
1083 get_info(ctx->ovs, &info);
1084 bridge = find_bridge(&info, ctx->argv[1], must_exist);
1086 struct shash_node *node;
1088 SHASH_FOR_EACH (node, &info.ports) {
1089 struct vsctl_port *port = node->data;
1090 if (port->bridge == bridge || port->bridge->parent == bridge
1091 || !strcmp(port->port_cfg->name, bridge->name)) {
1092 del_port(&info, port);
1095 if (bridge->br_cfg) {
1096 ovsrec_bridge_delete(bridge->br_cfg);
1097 ovs_delete_bridge(ctx->ovs, bridge->br_cfg);
1104 output_sorted(struct svec *svec, struct ds *output)
1110 SVEC_FOR_EACH (i, name, svec) {
1111 ds_put_format(output, "%s\n", name);
1116 cmd_list_br(struct vsctl_context *ctx)
1118 struct shash_node *node;
1119 struct vsctl_info info;
1120 struct svec bridges;
1122 get_info(ctx->ovs, &info);
1124 svec_init(&bridges);
1125 SHASH_FOR_EACH (node, &info.bridges) {
1126 struct vsctl_bridge *br = node->data;
1127 svec_add(&bridges, br->name);
1129 output_sorted(&bridges, &ctx->output);
1130 svec_destroy(&bridges);
1136 cmd_br_exists(struct vsctl_context *ctx)
1138 struct vsctl_info info;
1140 get_info(ctx->ovs, &info);
1141 if (!find_bridge(&info, ctx->argv[1], false)) {
1147 /* Returns true if 'b_prefix' (of length 'b_prefix_len') concatenated with 'b'
1148 * equals 'a', false otherwise. */
1150 key_matches(const char *a,
1151 const char *b_prefix, size_t b_prefix_len, const char *b)
1153 return !strncmp(a, b_prefix, b_prefix_len) && !strcmp(a + b_prefix_len, b);
1157 set_external_id(char **old_keys, char **old_values, size_t old_n,
1158 char *key, char *value,
1159 char ***new_keysp, char ***new_valuesp, size_t *new_np)
1166 new_keys = xmalloc(sizeof *new_keys * (old_n + 1));
1167 new_values = xmalloc(sizeof *new_values * (old_n + 1));
1169 for (i = 0; i < old_n; i++) {
1170 if (strcmp(key, old_keys[i])) {
1171 new_keys[new_n] = old_keys[i];
1172 new_values[new_n] = old_values[i];
1177 new_keys[new_n] = key;
1178 new_values[new_n] = value;
1181 *new_keysp = new_keys;
1182 *new_valuesp = new_values;
1187 cmd_br_set_external_id(struct vsctl_context *ctx)
1189 struct vsctl_info info;
1190 struct vsctl_bridge *bridge;
1191 char **keys, **values;
1194 get_info(ctx->ovs, &info);
1195 bridge = find_bridge(&info, ctx->argv[1], true);
1196 if (bridge->br_cfg) {
1197 set_external_id(bridge->br_cfg->key_external_ids,
1198 bridge->br_cfg->value_external_ids,
1199 bridge->br_cfg->n_external_ids,
1200 ctx->argv[2], ctx->argc >= 4 ? ctx->argv[3] : NULL,
1201 &keys, &values, &n);
1202 ovsrec_bridge_set_external_ids(bridge->br_cfg, keys, values, n);
1204 char *key = xasprintf("fake-bridge-%s", ctx->argv[2]);
1205 struct vsctl_port *port = shash_find_data(&info.ports, ctx->argv[1]);
1206 set_external_id(port->port_cfg->key_external_ids,
1207 port->port_cfg->value_external_ids,
1208 port->port_cfg->n_external_ids,
1209 key, ctx->argc >= 4 ? ctx->argv[3] : NULL,
1210 &keys, &values, &n);
1211 ovsrec_port_set_external_ids(port->port_cfg, keys, values, n);
1221 get_external_id(char **keys, char **values, size_t n,
1222 const char *prefix, const char *key,
1225 size_t prefix_len = strlen(prefix);
1230 for (i = 0; i < n; i++) {
1231 if (!key && !strncmp(keys[i], prefix, prefix_len)) {
1232 svec_add_nocopy(&svec, xasprintf("%s=%s",
1233 keys[i] + prefix_len, values[i]));
1234 } else if (key_matches(keys[i], prefix, prefix_len, key)) {
1235 svec_add(&svec, values[i]);
1239 output_sorted(&svec, output);
1240 svec_destroy(&svec);
1244 cmd_br_get_external_id(struct vsctl_context *ctx)
1246 struct vsctl_info info;
1247 struct vsctl_bridge *bridge;
1249 get_info(ctx->ovs, &info);
1250 bridge = find_bridge(&info, ctx->argv[1], true);
1251 if (bridge->br_cfg) {
1252 get_external_id(bridge->br_cfg->key_external_ids,
1253 bridge->br_cfg->value_external_ids,
1254 bridge->br_cfg->n_external_ids,
1255 "", ctx->argc >= 3 ? ctx->argv[2] : NULL,
1258 struct vsctl_port *port = shash_find_data(&info.ports, ctx->argv[1]);
1259 get_external_id(port->port_cfg->key_external_ids,
1260 port->port_cfg->value_external_ids,
1261 port->port_cfg->n_external_ids,
1262 "fake-bridge-", ctx->argc >= 3 ? ctx->argv[2] : NULL, &ctx->output);
1269 cmd_list_ports(struct vsctl_context *ctx)
1271 struct vsctl_bridge *br;
1272 struct shash_node *node;
1273 struct vsctl_info info;
1276 get_info(ctx->ovs, &info);
1277 br = find_bridge(&info, ctx->argv[1], true);
1280 SHASH_FOR_EACH (node, &info.ports) {
1281 struct vsctl_port *port = node->data;
1283 if (strcmp(port->port_cfg->name, br->name) && br == port->bridge) {
1284 svec_add(&ports, port->port_cfg->name);
1287 output_sorted(&ports, &ctx->output);
1288 svec_destroy(&ports);
1294 add_port(struct vsctl_context *ctx,
1295 const char *br_name, const char *port_name,
1296 bool may_exist, bool fake_iface,
1297 char *iface_names[], int n_ifaces,
1298 char *settings[], int n_settings)
1300 struct vsctl_info info;
1301 struct vsctl_bridge *bridge;
1302 struct ovsrec_interface **ifaces;
1303 struct ovsrec_port *port;
1306 get_info(ctx->ovs, &info);
1308 struct vsctl_port *port;
1310 port = find_port(&info, port_name, false);
1312 struct svec want_names, have_names;
1315 svec_init(&want_names);
1316 for (i = 0; i < n_ifaces; i++) {
1317 svec_add(&want_names, iface_names[i]);
1319 svec_sort(&want_names);
1321 svec_init(&have_names);
1322 for (i = 0; i < port->port_cfg->n_interfaces; i++) {
1323 svec_add(&have_names, port->port_cfg->interfaces[i]->name);
1325 svec_sort(&have_names);
1327 if (strcmp(port->bridge->name, br_name)) {
1328 char *command = vsctl_context_to_string(ctx);
1329 vsctl_fatal("\"%s\" but %s is actually attached to bridge %s",
1330 command, port_name, port->bridge->name);
1333 if (!svec_equal(&want_names, &have_names)) {
1334 char *have_names_string = svec_join(&have_names, ", ", "");
1335 char *command = vsctl_context_to_string(ctx);
1337 vsctl_fatal("\"%s\" but %s actually has interface(s) %s",
1338 command, port_name, have_names_string);
1341 svec_destroy(&want_names);
1342 svec_destroy(&have_names);
1347 check_conflicts(&info, port_name,
1348 xasprintf("cannot create a port named %s", port_name));
1349 for (i = 0; i < n_ifaces; i++) {
1350 check_conflicts(&info, iface_names[i],
1351 xasprintf("cannot create an interface named %s",
1354 bridge = find_bridge(&info, br_name, true);
1356 ifaces = xmalloc(n_ifaces * sizeof *ifaces);
1357 for (i = 0; i < n_ifaces; i++) {
1358 ifaces[i] = ovsrec_interface_insert(ctx->txn);
1359 ovsrec_interface_set_name(ifaces[i], iface_names[i]);
1362 port = ovsrec_port_insert(ctx->txn);
1363 ovsrec_port_set_name(port, port_name);
1364 ovsrec_port_set_interfaces(port, ifaces, n_ifaces);
1365 ovsrec_port_set_bond_fake_iface(port, fake_iface);
1369 int64_t tag = bridge->vlan;
1370 ovsrec_port_set_tag(port, &tag, 1);
1373 for (i = 0; i < n_settings; i++) {
1374 set_column(get_table("Port"), &port->header_, settings[i],
1378 bridge_insert_port((bridge->parent ? bridge->parent->br_cfg
1379 : bridge->br_cfg), port);
1385 cmd_add_port(struct vsctl_context *ctx)
1387 bool may_exist = shash_find(&ctx->options, "--may-exist") != 0;
1389 add_port(ctx, ctx->argv[1], ctx->argv[2], may_exist, false,
1390 &ctx->argv[2], 1, &ctx->argv[3], ctx->argc - 3);
1394 cmd_add_bond(struct vsctl_context *ctx)
1396 bool may_exist = shash_find(&ctx->options, "--may-exist") != 0;
1397 bool fake_iface = shash_find(&ctx->options, "--fake-iface");
1401 n_ifaces = ctx->argc - 3;
1402 for (i = 3; i < ctx->argc; i++) {
1403 if (strchr(ctx->argv[i], '=')) {
1409 vsctl_fatal("add-bond requires at least 2 interfaces, but only "
1410 "%d were specified", n_ifaces);
1413 add_port(ctx, ctx->argv[1], ctx->argv[2], may_exist, fake_iface,
1414 &ctx->argv[3], n_ifaces,
1415 &ctx->argv[n_ifaces + 3], ctx->argc - 3 - n_ifaces);
1419 cmd_del_port(struct vsctl_context *ctx)
1421 bool must_exist = !shash_find(&ctx->options, "--if-exists");
1422 bool with_iface = shash_find(&ctx->options, "--with-iface") != NULL;
1423 struct vsctl_port *port;
1424 struct vsctl_info info;
1426 get_info(ctx->ovs, &info);
1428 port = find_port(&info, ctx->argv[ctx->argc - 1], must_exist);
1430 const char *target = ctx->argv[ctx->argc - 1];
1431 struct vsctl_iface *iface;
1433 port = find_port(&info, target, false);
1435 iface = find_iface(&info, target, false);
1440 if (must_exist && !port) {
1441 vsctl_fatal("no port or interface named %s", target);
1446 if (ctx->argc == 3) {
1447 struct vsctl_bridge *bridge;
1449 bridge = find_bridge(&info, ctx->argv[1], true);
1450 if (port->bridge != bridge) {
1451 if (port->bridge->parent == bridge) {
1452 vsctl_fatal("bridge %s does not have a port %s (although "
1453 "its parent bridge %s does)",
1454 ctx->argv[1], ctx->argv[2],
1455 bridge->parent->name);
1457 vsctl_fatal("bridge %s does not have a port %s",
1458 ctx->argv[1], ctx->argv[2]);
1463 del_port(&info, port);
1470 cmd_port_to_br(struct vsctl_context *ctx)
1472 struct vsctl_port *port;
1473 struct vsctl_info info;
1475 get_info(ctx->ovs, &info);
1476 port = find_port(&info, ctx->argv[1], true);
1477 ds_put_format(&ctx->output, "%s\n", port->bridge->name);
1482 cmd_br_to_vlan(struct vsctl_context *ctx)
1484 struct vsctl_bridge *bridge;
1485 struct vsctl_info info;
1487 get_info(ctx->ovs, &info);
1488 bridge = find_bridge(&info, ctx->argv[1], true);
1489 ds_put_format(&ctx->output, "%d\n", bridge->vlan);
1494 cmd_br_to_parent(struct vsctl_context *ctx)
1496 struct vsctl_bridge *bridge;
1497 struct vsctl_info info;
1499 get_info(ctx->ovs, &info);
1500 bridge = find_bridge(&info, ctx->argv[1], true);
1501 if (bridge->parent) {
1502 bridge = bridge->parent;
1504 ds_put_format(&ctx->output, "%s\n", bridge->name);
1509 cmd_list_ifaces(struct vsctl_context *ctx)
1511 struct vsctl_bridge *br;
1512 struct shash_node *node;
1513 struct vsctl_info info;
1516 get_info(ctx->ovs, &info);
1517 br = find_bridge(&info, ctx->argv[1], true);
1520 SHASH_FOR_EACH (node, &info.ifaces) {
1521 struct vsctl_iface *iface = node->data;
1523 if (strcmp(iface->iface_cfg->name, br->name)
1524 && br == iface->port->bridge) {
1525 svec_add(&ifaces, iface->iface_cfg->name);
1528 output_sorted(&ifaces, &ctx->output);
1529 svec_destroy(&ifaces);
1535 cmd_iface_to_br(struct vsctl_context *ctx)
1537 struct vsctl_iface *iface;
1538 struct vsctl_info info;
1540 get_info(ctx->ovs, &info);
1541 iface = find_iface(&info, ctx->argv[1], true);
1542 ds_put_format(&ctx->output, "%s\n", iface->port->bridge->name);
1547 cmd_get_controller(struct vsctl_context *ctx)
1549 struct vsctl_info info;
1550 struct vsctl_bridge *br;
1551 struct svec targets;
1554 get_info(ctx->ovs, &info);
1555 br = find_bridge(&info, ctx->argv[1], true);
1557 /* Print the targets in sorted order for reproducibility. */
1558 svec_init(&targets);
1559 for (i = 0; i < br->n_ctrl; i++) {
1560 svec_add(&targets, br->ctrl[i]->target);
1563 svec_sort(&targets);
1564 for (i = 0; i < targets.n; i++) {
1565 ds_put_format(&ctx->output, "%s\n", targets.names[i]);
1567 svec_destroy(&targets);
1573 delete_controllers(struct ovsrec_controller **controllers,
1574 size_t n_controllers)
1578 for (i = 0; i < n_controllers; i++) {
1579 ovsrec_controller_delete(controllers[i]);
1584 cmd_del_controller(struct vsctl_context *ctx)
1586 struct vsctl_info info;
1587 struct vsctl_bridge *br;
1589 get_info(ctx->ovs, &info);
1590 br = find_real_bridge(&info, ctx->argv[1], true);
1593 delete_controllers(br->ctrl, br->n_ctrl);
1594 ovsrec_bridge_set_controller(br->br_cfg, NULL, 0);
1600 static struct ovsrec_controller **
1601 insert_controllers(struct ovsdb_idl_txn *txn, char *targets[], size_t n)
1603 struct ovsrec_controller **controllers;
1606 controllers = xmalloc(n * sizeof *controllers);
1607 for (i = 0; i < n; i++) {
1608 controllers[i] = ovsrec_controller_insert(txn);
1609 ovsrec_controller_set_target(controllers[i], targets[i]);
1616 cmd_set_controller(struct vsctl_context *ctx)
1618 struct vsctl_info info;
1619 struct vsctl_bridge *br;
1620 struct ovsrec_controller **controllers;
1623 get_info(ctx->ovs, &info);
1624 br = find_real_bridge(&info, ctx->argv[1], true);
1626 delete_controllers(br->ctrl, br->n_ctrl);
1629 controllers = insert_controllers(ctx->txn, &ctx->argv[2], n);
1630 ovsrec_bridge_set_controller(br->br_cfg, controllers, n);
1637 cmd_get_fail_mode(struct vsctl_context *ctx)
1639 struct vsctl_info info;
1640 struct vsctl_bridge *br;
1642 get_info(ctx->ovs, &info);
1643 br = find_bridge(&info, ctx->argv[1], true);
1645 if (br->fail_mode && strlen(br->fail_mode)) {
1646 ds_put_format(&ctx->output, "%s\n", br->fail_mode);
1653 cmd_del_fail_mode(struct vsctl_context *ctx)
1655 struct vsctl_info info;
1656 struct vsctl_bridge *br;
1658 get_info(ctx->ovs, &info);
1659 br = find_real_bridge(&info, ctx->argv[1], true);
1661 ovsrec_bridge_set_fail_mode(br->br_cfg, NULL);
1667 cmd_set_fail_mode(struct vsctl_context *ctx)
1669 struct vsctl_info info;
1670 struct vsctl_bridge *br;
1671 const char *fail_mode = ctx->argv[2];
1673 get_info(ctx->ovs, &info);
1674 br = find_real_bridge(&info, ctx->argv[1], true);
1676 if (strcmp(fail_mode, "standalone") && strcmp(fail_mode, "secure")) {
1677 vsctl_fatal("fail-mode must be \"standalone\" or \"secure\"");
1680 ovsrec_bridge_set_fail_mode(br->br_cfg, fail_mode);
1686 cmd_get_ssl(struct vsctl_context *ctx)
1688 struct ovsrec_ssl *ssl = ctx->ovs->ssl;
1691 ds_put_format(&ctx->output, "Private key: %s\n", ssl->private_key);
1692 ds_put_format(&ctx->output, "Certificate: %s\n", ssl->certificate);
1693 ds_put_format(&ctx->output, "CA Certificate: %s\n", ssl->ca_cert);
1694 ds_put_format(&ctx->output, "Bootstrap: %s\n",
1695 ssl->bootstrap_ca_cert ? "true" : "false");
1700 cmd_del_ssl(struct vsctl_context *ctx)
1702 struct ovsrec_ssl *ssl = ctx->ovs->ssl;
1705 ovsrec_ssl_delete(ssl);
1706 ovsrec_open_vswitch_set_ssl(ctx->ovs, NULL);
1711 cmd_set_ssl(struct vsctl_context *ctx)
1713 bool bootstrap = shash_find(&ctx->options, "--bootstrap");
1714 struct ovsrec_ssl *ssl = ctx->ovs->ssl;
1717 ovsrec_ssl_delete(ssl);
1719 ssl = ovsrec_ssl_insert(ctx->txn);
1721 ovsrec_ssl_set_private_key(ssl, ctx->argv[1]);
1722 ovsrec_ssl_set_certificate(ssl, ctx->argv[2]);
1723 ovsrec_ssl_set_ca_cert(ssl, ctx->argv[3]);
1725 ovsrec_ssl_set_bootstrap_ca_cert(ssl, bootstrap);
1727 ovsrec_open_vswitch_set_ssl(ctx->ovs, ssl);
1730 /* Parameter commands. */
1732 struct vsctl_row_id {
1733 const struct ovsdb_idl_table_class *table;
1734 const struct ovsdb_idl_column *name_column;
1735 const struct ovsdb_idl_column *uuid_column;
1738 struct vsctl_table_class {
1739 struct ovsdb_idl_table_class *class;
1740 struct vsctl_row_id row_ids[2];
1743 static const struct vsctl_table_class tables[] = {
1744 {&ovsrec_table_bridge,
1745 {{&ovsrec_table_bridge, &ovsrec_bridge_col_name, NULL},
1746 {NULL, NULL, NULL}}},
1748 {&ovsrec_table_controller,
1749 {{&ovsrec_table_bridge,
1750 &ovsrec_bridge_col_name,
1751 &ovsrec_bridge_col_controller}}},
1753 {&ovsrec_table_interface,
1754 {{&ovsrec_table_interface, &ovsrec_interface_col_name, NULL},
1755 {NULL, NULL, NULL}}},
1757 {&ovsrec_table_mirror,
1758 {{&ovsrec_table_mirror, &ovsrec_mirror_col_name, NULL},
1759 {NULL, NULL, NULL}}},
1761 {&ovsrec_table_netflow,
1762 {{&ovsrec_table_bridge,
1763 &ovsrec_bridge_col_name,
1764 &ovsrec_bridge_col_netflow},
1765 {NULL, NULL, NULL}}},
1767 {&ovsrec_table_open_vswitch,
1768 {{&ovsrec_table_open_vswitch, NULL, NULL},
1769 {NULL, NULL, NULL}}},
1771 {&ovsrec_table_port,
1772 {{&ovsrec_table_port, &ovsrec_port_col_name, NULL},
1773 {NULL, NULL, NULL}}},
1776 {{&ovsrec_table_port, &ovsrec_port_col_name, &ovsrec_port_col_qos},
1777 {NULL, NULL, NULL}}},
1779 {&ovsrec_table_queue,
1780 {{NULL, NULL, NULL},
1781 {NULL, NULL, NULL}}},
1784 {{&ovsrec_table_open_vswitch, NULL, &ovsrec_open_vswitch_col_ssl}}},
1786 {&ovsrec_table_sflow,
1787 {{&ovsrec_table_bridge,
1788 &ovsrec_bridge_col_name,
1789 &ovsrec_bridge_col_sflow},
1790 {NULL, NULL, NULL}}},
1792 {NULL, {{NULL, NULL, NULL}, {NULL, NULL, NULL}}}
1796 die_if_error(char *error)
1799 vsctl_fatal("%s", error);
1804 to_lower_and_underscores(unsigned c)
1806 return c == '-' ? '_' : tolower(c);
1810 score_partial_match(const char *name, const char *s)
1814 if (!strcmp(name, s)) {
1817 for (score = 0; ; score++, name++, s++) {
1818 if (to_lower_and_underscores(*name) != to_lower_and_underscores(*s)) {
1820 } else if (*name == '\0') {
1821 return UINT_MAX - 1;
1824 return *s == '\0' ? score : 0;
1827 static const struct vsctl_table_class *
1828 get_table(const char *table_name)
1830 const struct vsctl_table_class *table;
1831 const struct vsctl_table_class *best_match = NULL;
1832 unsigned int best_score = 0;
1834 for (table = tables; table->class; table++) {
1835 unsigned int score = score_partial_match(table->class->name,
1837 if (score > best_score) {
1840 } else if (score == best_score) {
1846 } else if (best_score) {
1847 vsctl_fatal("multiple table names match \"%s\"", table_name);
1849 vsctl_fatal("unknown table \"%s\"", table_name);
1853 static const struct ovsdb_idl_row *
1854 get_row_by_id(struct vsctl_context *ctx, const struct vsctl_table_class *table,
1855 const struct vsctl_row_id *id, const char *record_id)
1857 const struct ovsdb_idl_row *referrer, *final;
1863 if (!id->name_column) {
1864 if (strcmp(record_id, ".")) {
1867 referrer = ovsdb_idl_first_row(ctx->idl, id->table);
1868 if (!referrer || ovsdb_idl_next_row(referrer)) {
1872 const struct ovsdb_idl_row *row;
1875 for (row = ovsdb_idl_first_row(ctx->idl, id->table);
1877 row = ovsdb_idl_next_row(row))
1879 const struct ovsdb_datum *name;
1881 name = ovsdb_idl_get(row, id->name_column,
1882 OVSDB_TYPE_STRING, OVSDB_TYPE_VOID);
1883 if (name->n == 1 && !strcmp(name->keys[0].string, record_id)) {
1885 vsctl_fatal("multiple rows in %s match \"%s\"",
1886 table->class->name, record_id);
1897 if (id->uuid_column) {
1898 const struct ovsdb_datum *uuid;
1900 uuid = ovsdb_idl_get(referrer, id->uuid_column,
1901 OVSDB_TYPE_UUID, OVSDB_TYPE_VOID);
1903 final = ovsdb_idl_get_row_for_uuid(ctx->idl, table->class,
1904 &uuid->keys[0].uuid);
1913 static const struct ovsdb_idl_row *
1914 get_row (struct vsctl_context *ctx,
1915 const struct vsctl_table_class *table, const char *record_id)
1917 const struct ovsdb_idl_row *row;
1920 if (uuid_from_string(&uuid, record_id)) {
1921 row = ovsdb_idl_get_row_for_uuid(ctx->idl, table->class, &uuid);
1925 for (i = 0; i < ARRAY_SIZE(table->row_ids); i++) {
1926 row = get_row_by_id(ctx, table, &table->row_ids[i], record_id);
1935 static const struct ovsdb_idl_row *
1936 must_get_row(struct vsctl_context *ctx,
1937 const struct vsctl_table_class *table, const char *record_id)
1939 const struct ovsdb_idl_row *row = get_row(ctx, table, record_id);
1941 vsctl_fatal("no row \"%s\" in table %s",
1942 record_id, table->class->name);
1948 get_column(const struct vsctl_table_class *table, const char *column_name,
1949 const struct ovsdb_idl_column **columnp)
1951 const struct ovsdb_idl_column *best_match = NULL;
1952 unsigned int best_score = 0;
1955 for (i = 0; i < table->class->n_columns; i++) {
1956 const struct ovsdb_idl_column *column = &table->class->columns[i];
1957 unsigned int score = score_partial_match(column->name, column_name);
1958 if (score > best_score) {
1959 best_match = column;
1961 } else if (score == best_score) {
1966 *columnp = best_match;
1969 } else if (best_score) {
1970 return xasprintf("%s contains more than one column whose name "
1971 "matches \"%s\"", table->class->name, column_name);
1973 return xasprintf("%s does not contain a column whose name matches "
1974 "\"%s\"", table->class->name, column_name);
1979 missing_operator_error(const char *arg, const char **allowed_operators,
1985 ds_put_format(&s, "%s: argument does not end in ", arg);
1986 ds_put_format(&s, "\"%s\"", allowed_operators[0]);
1987 if (n_allowed == 2) {
1988 ds_put_format(&s, " or \"%s\"", allowed_operators[1]);
1989 } else if (n_allowed > 2) {
1992 for (i = 1; i < n_allowed - 1; i++) {
1993 ds_put_format(&s, ", \"%s\"", allowed_operators[i]);
1995 ds_put_format(&s, ", or \"%s\"", allowed_operators[i]);
1997 ds_put_format(&s, " followed by a value.");
1999 return ds_steal_cstr(&s);
2002 /* Breaks 'arg' apart into a number of fields in the following order:
2004 * - If 'columnp' is nonnull, the name of a column in 'table'. The column
2005 * is stored into '*columnp'. The column name may be abbreviated.
2007 * - If 'keyp' is nonnull, optionally a key string. (If both 'columnp'
2008 * and 'keyp' are nonnull, then the column and key names are expected to
2009 * be separated by ':'). The key is stored as a malloc()'d string into
2010 * '*keyp', or NULL if no key is present in 'arg'.
2012 * - If 'valuep' is nonnull, an operator followed by a value string. The
2013 * allowed operators are the 'n_allowed' string in 'allowed_operators',
2014 * or just "=" if 'n_allowed' is 0. If 'operatorp' is nonnull, then the
2015 * operator is stored into '*operatorp' (one of the pointers from
2016 * 'allowed_operators' is stored; nothing is malloc()'d). The value is
2017 * stored as a malloc()'d string into '*valuep', or NULL if no value is
2020 * At least 'columnp' or 'keyp' must be nonnull.
2022 * On success, returns NULL. On failure, returned a malloc()'d string error
2023 * message and stores NULL into all of the nonnull output arguments. */
2024 static char * WARN_UNUSED_RESULT
2025 parse_column_key_value(const char *arg,
2026 const struct vsctl_table_class *table,
2027 const struct ovsdb_idl_column **columnp, char **keyp,
2028 const char **operatorp,
2029 const char **allowed_operators, size_t n_allowed,
2032 const char *p = arg;
2035 assert(columnp || keyp);
2036 assert(!(operatorp && !valuep));
2044 /* Parse column name. */
2048 error = ovsdb_token_parse(&p, &column_name);
2052 if (column_name[0] == '\0') {
2054 error = xasprintf("%s: missing column name", arg);
2057 error = get_column(table, column_name, columnp);
2064 /* Parse key string. */
2065 if (*p == ':' || !columnp) {
2069 error = xasprintf("%s: key not accepted here", arg);
2072 error = ovsdb_token_parse(&p, keyp);
2080 /* Parse value string. */
2086 if (!allowed_operators) {
2087 static const char *equals = "=";
2088 allowed_operators = =
2094 for (i = 0; i < n_allowed; i++) {
2095 const char *op = allowed_operators[i];
2096 size_t op_len = strlen(op);
2098 if (op_len > best_len && !strncmp(op, p, op_len) && p[op_len]) {
2104 error = missing_operator_error(arg, allowed_operators, n_allowed);
2111 *valuep = xstrdup(p + best_len);
2117 error = xasprintf("%s: trailing garbage \"%s\" in argument",
2143 cmd_get(struct vsctl_context *ctx)
2145 bool if_exists = shash_find(&ctx->options, "--if-exists");
2146 const char *table_name = ctx->argv[1];
2147 const char *record_id = ctx->argv[2];
2148 const struct vsctl_table_class *table;
2149 const struct ovsdb_idl_row *row;
2150 struct ds *out = &ctx->output;
2153 table = get_table(table_name);
2154 row = must_get_row(ctx, table, record_id);
2155 for (i = 3; i < ctx->argc; i++) {
2156 const struct ovsdb_idl_column *column;
2157 const struct ovsdb_datum *datum;
2160 /* Special case for obtaining the UUID of a row. We can't just do this
2161 * through parse_column_key_value() below since it returns a "struct
2162 * ovsdb_idl_column" and the UUID column doesn't have one. */
2163 if (!strcasecmp(ctx->argv[i], "_uuid")
2164 || !strcasecmp(ctx->argv[i], "-uuid")) {
2165 ds_put_format(out, UUID_FMT"\n", UUID_ARGS(&row->uuid));
2169 die_if_error(parse_column_key_value(ctx->argv[i], table,
2170 &column, &key_string,
2171 NULL, NULL, 0, NULL));
2173 datum = ovsdb_idl_read(row, column);
2175 union ovsdb_atom key;
2178 if (column->type.value.type == OVSDB_TYPE_VOID) {
2179 vsctl_fatal("cannot specify key to get for non-map column %s",
2183 die_if_error(ovsdb_atom_from_string(&key,
2185 key_string, ctx->symtab));
2187 idx = ovsdb_datum_find_key(datum, &key,
2188 column->type.key.type);
2189 if (idx == UINT_MAX) {
2191 vsctl_fatal("no key \"%s\" in %s record \"%s\" column %s",
2192 key_string, table->class->name, record_id,
2196 ovsdb_atom_to_string(&datum->values[idx],
2197 column->type.value.type, out);
2199 ovsdb_atom_destroy(&key, column->type.key.type);
2201 ovsdb_datum_to_string(datum, &column->type, out);
2203 ds_put_char(out, '\n');
2210 list_record(const struct vsctl_table_class *table,
2211 const struct ovsdb_idl_row *row, struct ds *out)
2215 ds_put_format(out, "%-20s: "UUID_FMT"\n", "_uuid",
2216 UUID_ARGS(&row->uuid));
2217 for (i = 0; i < table->class->n_columns; i++) {
2218 const struct ovsdb_idl_column *column = &table->class->columns[i];
2219 const struct ovsdb_datum *datum;
2221 datum = ovsdb_idl_read(row, column);
2223 ds_put_format(out, "%-20s: ", column->name);
2224 ovsdb_datum_to_string(datum, &column->type, out);
2225 ds_put_char(out, '\n');
2230 cmd_list(struct vsctl_context *ctx)
2232 const char *table_name = ctx->argv[1];
2233 const struct vsctl_table_class *table;
2234 struct ds *out = &ctx->output;
2237 table = get_table(table_name);
2238 if (ctx->argc > 2) {
2239 for (i = 2; i < ctx->argc; i++) {
2241 ds_put_char(out, '\n');
2243 list_record(table, must_get_row(ctx, table, ctx->argv[i]), out);
2246 const struct ovsdb_idl_row *row;
2249 for (row = ovsdb_idl_first_row(ctx->idl, table->class), first = true;
2251 row = ovsdb_idl_next_row(row), first = false) {
2253 ds_put_char(out, '\n');
2255 list_record(table, row, out);
2261 set_column(const struct vsctl_table_class *table,
2262 const struct ovsdb_idl_row *row, const char *arg,
2263 struct ovsdb_symbol_table *symtab)
2265 const struct ovsdb_idl_column *column;
2266 char *key_string, *value_string;
2269 error = parse_column_key_value(arg, table, &column, &key_string,
2270 NULL, NULL, 0, &value_string);
2271 die_if_error(error);
2272 if (!value_string) {
2273 vsctl_fatal("%s: missing value", arg);
2277 union ovsdb_atom key, value;
2278 struct ovsdb_datum datum;
2280 if (column->type.value.type == OVSDB_TYPE_VOID) {
2281 vsctl_fatal("cannot specify key to set for non-map column %s",
2285 die_if_error(ovsdb_atom_from_string(&key, &column->type.key,
2286 key_string, symtab));
2287 die_if_error(ovsdb_atom_from_string(&value, &column->type.value,
2288 value_string, symtab));
2290 ovsdb_datum_init_empty(&datum);
2291 ovsdb_datum_add_unsafe(&datum, &key, &value, &column->type);
2293 ovsdb_atom_destroy(&key, column->type.key.type);
2294 ovsdb_atom_destroy(&value, column->type.value.type);
2296 ovsdb_datum_union(&datum, ovsdb_idl_read(row, column),
2297 &column->type, false);
2298 ovsdb_idl_txn_write(row, column, &datum);
2300 struct ovsdb_datum datum;
2302 die_if_error(ovsdb_datum_from_string(&datum, &column->type,
2303 value_string, symtab));
2304 ovsdb_idl_txn_write(row, column, &datum);
2312 cmd_set(struct vsctl_context *ctx)
2314 const char *table_name = ctx->argv[1];
2315 const char *record_id = ctx->argv[2];
2316 const struct vsctl_table_class *table;
2317 const struct ovsdb_idl_row *row;
2320 table = get_table(table_name);
2321 row = must_get_row(ctx, table, record_id);
2322 for (i = 3; i < ctx->argc; i++) {
2323 set_column(table, row, ctx->argv[i], ctx->symtab);
2328 cmd_add(struct vsctl_context *ctx)
2330 const char *table_name = ctx->argv[1];
2331 const char *record_id = ctx->argv[2];
2332 const char *column_name = ctx->argv[3];
2333 const struct vsctl_table_class *table;
2334 const struct ovsdb_idl_column *column;
2335 const struct ovsdb_idl_row *row;
2336 const struct ovsdb_type *type;
2337 struct ovsdb_datum old;
2340 table = get_table(table_name);
2341 row = must_get_row(ctx, table, record_id);
2342 die_if_error(get_column(table, column_name, &column));
2344 type = &column->type;
2345 ovsdb_datum_clone(&old, ovsdb_idl_read(row, column), &column->type);
2346 for (i = 4; i < ctx->argc; i++) {
2347 struct ovsdb_type add_type;
2348 struct ovsdb_datum add;
2352 add_type.n_max = UINT_MAX;
2353 die_if_error(ovsdb_datum_from_string(&add, &add_type, ctx->argv[i],
2355 ovsdb_datum_union(&old, &add, type, false);
2356 ovsdb_datum_destroy(&add, type);
2358 if (old.n > type->n_max) {
2359 vsctl_fatal("\"add\" operation would put %u %s in column %s of "
2360 "table %s but the maximum number is %u",
2362 type->value.type == OVSDB_TYPE_VOID ? "values" : "pairs",
2363 column->name, table->class->name, type->n_max);
2365 ovsdb_idl_txn_write(row, column, &old);
2369 cmd_remove(struct vsctl_context *ctx)
2371 const char *table_name = ctx->argv[1];
2372 const char *record_id = ctx->argv[2];
2373 const char *column_name = ctx->argv[3];
2374 const struct vsctl_table_class *table;
2375 const struct ovsdb_idl_column *column;
2376 const struct ovsdb_idl_row *row;
2377 const struct ovsdb_type *type;
2378 struct ovsdb_datum old;
2381 table = get_table(table_name);
2382 row = must_get_row(ctx, table, record_id);
2383 die_if_error(get_column(table, column_name, &column));
2385 type = &column->type;
2386 ovsdb_datum_clone(&old, ovsdb_idl_read(row, column), &column->type);
2387 for (i = 4; i < ctx->argc; i++) {
2388 struct ovsdb_type rm_type;
2389 struct ovsdb_datum rm;
2394 rm_type.n_max = UINT_MAX;
2395 error = ovsdb_datum_from_string(&rm, &rm_type,
2396 ctx->argv[i], ctx->symtab);
2397 if (error && ovsdb_type_is_map(&rm_type)) {
2399 rm_type.value.type = OVSDB_TYPE_VOID;
2400 die_if_error(ovsdb_datum_from_string(&rm, &rm_type,
2401 ctx->argv[i], ctx->symtab));
2403 ovsdb_datum_subtract(&old, type, &rm, &rm_type);
2404 ovsdb_datum_destroy(&rm, &rm_type);
2406 if (old.n < type->n_min) {
2407 vsctl_fatal("\"remove\" operation would put %u %s in column %s of "
2408 "table %s but the minimum number is %u",
2410 type->value.type == OVSDB_TYPE_VOID ? "values" : "pairs",
2411 column->name, table->class->name, type->n_min);
2413 ovsdb_idl_txn_write(row, column, &old);
2417 cmd_clear(struct vsctl_context *ctx)
2419 const char *table_name = ctx->argv[1];
2420 const char *record_id = ctx->argv[2];
2421 const struct vsctl_table_class *table;
2422 const struct ovsdb_idl_row *row;
2425 table = get_table(table_name);
2426 row = must_get_row(ctx, table, record_id);
2427 for (i = 3; i < ctx->argc; i++) {
2428 const struct ovsdb_idl_column *column;
2429 const struct ovsdb_type *type;
2430 struct ovsdb_datum datum;
2432 die_if_error(get_column(table, ctx->argv[i], &column));
2434 type = &column->type;
2435 if (type->n_min > 0) {
2436 vsctl_fatal("\"clear\" operation cannot be applied to column %s "
2437 "of table %s, which is not allowed to be empty",
2438 column->name, table->class->name);
2441 ovsdb_datum_init_empty(&datum);
2442 ovsdb_idl_txn_write(row, column, &datum);
2447 cmd_create(struct vsctl_context *ctx)
2449 const char *id = shash_find_data(&ctx->options, "--id");
2450 const char *table_name = ctx->argv[1];
2451 const struct vsctl_table_class *table;
2452 const struct ovsdb_idl_row *row;
2453 const struct uuid *uuid;
2457 struct ovsdb_symbol *symbol;
2460 vsctl_fatal("row id \"%s\" does not begin with \"@\"", id);
2463 symbol = ovsdb_symbol_table_insert(ctx->symtab, id);
2465 vsctl_fatal("row id \"%s\" may only be used to insert a single "
2468 symbol->used = true;
2470 uuid = &symbol->uuid;
2475 table = get_table(table_name);
2476 row = ovsdb_idl_txn_insert(ctx->txn, table->class, uuid);
2477 for (i = 2; i < ctx->argc; i++) {
2478 set_column(table, row, ctx->argv[i], ctx->symtab);
2480 ds_put_format(&ctx->output, UUID_FMT, UUID_ARGS(&row->uuid));
2483 /* This function may be used as the 'postprocess' function for commands that
2484 * insert new rows into the database. It expects that the command's 'run'
2485 * function prints the UUID reported by ovsdb_idl_txn_insert() as the command's
2486 * sole output. It replaces that output by the row's permanent UUID assigned
2487 * by the database server and appends a new-line.
2489 * Currently we use this only for "create", because the higher-level commands
2490 * are supposed to be independent of the actual structure of the vswitch
2493 post_create(struct vsctl_context *ctx)
2495 const struct uuid *real;
2498 uuid_from_string(&dummy, ds_cstr(&ctx->output));
2499 real = ovsdb_idl_txn_get_insert_uuid(ctx->txn, &dummy);
2501 ds_clear(&ctx->output);
2502 ds_put_format(&ctx->output, UUID_FMT, UUID_ARGS(real));
2504 ds_put_char(&ctx->output, '\n');
2508 cmd_destroy(struct vsctl_context *ctx)
2510 bool must_exist = !shash_find(&ctx->options, "--if-exists");
2511 const char *table_name = ctx->argv[1];
2512 const struct vsctl_table_class *table;
2515 table = get_table(table_name);
2516 for (i = 2; i < ctx->argc; i++) {
2517 const struct ovsdb_idl_row *row;
2519 row = (must_exist ? must_get_row : get_row)(ctx, table, ctx->argv[i]);
2521 ovsdb_idl_txn_delete(row);
2527 is_condition_satified(const struct vsctl_table_class *table,
2528 const struct ovsdb_idl_row *row, const char *arg,
2529 struct ovsdb_symbol_table *symtab)
2531 static const char *operators[] = {
2532 "=", "!=", "<", ">", "<=", ">="
2535 const struct ovsdb_idl_column *column;
2536 const struct ovsdb_datum *have_datum;
2537 char *key_string, *value_string;
2538 const char *operator;
2543 error = parse_column_key_value(arg, table, &column, &key_string,
2544 &operator, operators, ARRAY_SIZE(operators),
2546 die_if_error(error);
2547 if (!value_string) {
2548 vsctl_fatal("%s: missing value", arg);
2551 have_datum = ovsdb_idl_read(row, column);
2553 union ovsdb_atom want_key, want_value;
2555 if (column->type.value.type == OVSDB_TYPE_VOID) {
2556 vsctl_fatal("cannot specify key to check for non-map column %s",
2560 die_if_error(ovsdb_atom_from_string(&want_key, &column->type.key,
2561 key_string, symtab));
2562 die_if_error(ovsdb_atom_from_string(&want_value, &column->type.value,
2563 value_string, symtab));
2565 idx = ovsdb_datum_find_key(have_datum,
2566 &want_key, column->type.key.type);
2567 if (idx != UINT_MAX) {
2568 cmp = ovsdb_atom_compare_3way(&have_datum->values[idx],
2570 column->type.value.type);
2573 ovsdb_atom_destroy(&want_key, column->type.key.type);
2574 ovsdb_atom_destroy(&want_value, column->type.value.type);
2576 struct ovsdb_datum want_datum;
2578 die_if_error(ovsdb_datum_from_string(&want_datum, &column->type,
2579 value_string, symtab));
2581 cmp = ovsdb_datum_compare_3way(have_datum, &want_datum,
2583 ovsdb_datum_destroy(&want_datum, &column->type);
2589 return (idx == UINT_MAX ? false
2590 : !strcmp(operator, "=") ? cmp == 0
2591 : !strcmp(operator, "!=") ? cmp != 0
2592 : !strcmp(operator, "<") ? cmp < 0
2593 : !strcmp(operator, ">") ? cmp > 0
2594 : !strcmp(operator, "<=") ? cmp <= 0
2595 : !strcmp(operator, ">=") ? cmp >= 0
2600 cmd_wait_until(struct vsctl_context *ctx)
2602 const char *table_name = ctx->argv[1];
2603 const char *record_id = ctx->argv[2];
2604 const struct vsctl_table_class *table;
2605 const struct ovsdb_idl_row *row;
2608 table = get_table(table_name);
2610 row = get_row(ctx, table, record_id);
2612 ctx->try_again = true;
2616 for (i = 3; i < ctx->argc; i++) {
2617 if (!is_condition_satified(table, row, ctx->argv[i], ctx->symtab)) {
2618 ctx->try_again = true;
2624 static struct json *
2625 where_uuid_equals(const struct uuid *uuid)
2628 json_array_create_1(
2629 json_array_create_3(
2630 json_string_create("_uuid"),
2631 json_string_create("=="),
2632 json_array_create_2(
2633 json_string_create("uuid"),
2634 json_string_create_nocopy(
2635 xasprintf(UUID_FMT, UUID_ARGS(uuid))))));
2639 vsctl_context_init(struct vsctl_context *ctx, struct vsctl_command *command,
2640 struct ovsdb_idl *idl, struct ovsdb_idl_txn *txn,
2641 const struct ovsrec_open_vswitch *ovs,
2642 struct ovsdb_symbol_table *symtab)
2644 ctx->argc = command->argc;
2645 ctx->argv = command->argv;
2646 ctx->options = command->options;
2648 ds_swap(&ctx->output, &command->output);
2652 ctx->symtab = symtab;
2654 ctx->try_again = false;
2658 vsctl_context_done(struct vsctl_context *ctx, struct vsctl_command *command)
2660 ds_swap(&ctx->output, &command->output);
2664 do_vsctl(const char *args, struct vsctl_command *commands, size_t n_commands,
2665 struct ovsdb_idl *idl)
2667 struct ovsdb_idl_txn *txn;
2668 const struct ovsrec_open_vswitch *ovs;
2669 enum ovsdb_idl_txn_status status;
2670 struct ovsdb_symbol_table *symtab;
2672 struct vsctl_command *c;
2673 int64_t next_cfg = 0;
2676 txn = the_idl_txn = ovsdb_idl_txn_create(idl);
2678 ovsdb_idl_txn_set_dry_run(txn);
2681 ovsdb_idl_txn_add_comment(txn, "ovs-vsctl: %s", args);
2683 ovs = ovsrec_open_vswitch_first(idl);
2685 /* XXX add verification that table is empty */
2686 ovs = ovsrec_open_vswitch_insert(txn);
2689 if (wait_for_reload) {
2690 struct json *where = where_uuid_equals(&ovs->header_.uuid);
2691 ovsdb_idl_txn_increment(txn, "Open_vSwitch", "next_cfg", where);
2692 json_destroy(where);
2695 symtab = ovsdb_symbol_table_create();
2696 for (c = commands; c < &commands[n_commands]; c++) {
2697 ds_init(&c->output);
2699 for (c = commands; c < &commands[n_commands]; c++) {
2700 struct vsctl_context ctx;
2702 vsctl_context_init(&ctx, c, idl, txn, ovs, symtab);
2703 (c->syntax->run)(&ctx);
2704 vsctl_context_done(&ctx, c);
2706 if (ctx.try_again) {
2711 status = ovsdb_idl_txn_commit_block(txn);
2712 if (wait_for_reload && status == TXN_SUCCESS) {
2713 next_cfg = ovsdb_idl_txn_get_increment_new_value(txn);
2715 if (status == TXN_UNCHANGED || status == TXN_SUCCESS) {
2716 for (c = commands; c < &commands[n_commands]; c++) {
2717 if (c->syntax->postprocess) {
2718 struct vsctl_context ctx;
2720 vsctl_context_init(&ctx, c, idl, txn, ovs, symtab);
2721 (c->syntax->postprocess)(&ctx);
2722 vsctl_context_done(&ctx, c);
2726 error = xstrdup(ovsdb_idl_txn_get_error(txn));
2727 ovsdb_idl_txn_destroy(txn);
2730 unused = ovsdb_symbol_table_find_unused(symtab);
2732 vsctl_fatal("row id \"%s\" is referenced but never created (e.g. "
2733 "with \"-- --id=%s create ...\")", unused, unused);
2737 case TXN_INCOMPLETE:
2741 /* Should not happen--we never call ovsdb_idl_txn_abort(). */
2742 vsctl_fatal("transaction aborted");
2752 vsctl_fatal("transaction error: %s", error);
2759 ovsdb_symbol_table_destroy(symtab);
2761 for (c = commands; c < &commands[n_commands]; c++) {
2762 struct ds *ds = &c->output;
2763 struct shash_node *node;
2769 for (j = 0; j < ds->length; j++) {
2770 int c = ds->string[j];
2773 fputs("\\n", stdout);
2777 fputs("\\\\", stdout);
2786 fputs(ds_cstr(ds), stdout);
2788 ds_destroy(&c->output);
2790 SHASH_FOR_EACH (node, &c->options) {
2793 shash_destroy(&c->options);
2797 if (wait_for_reload && status != TXN_UNCHANGED) {
2799 const struct ovsrec_open_vswitch *ovs;
2802 OVSREC_OPEN_VSWITCH_FOR_EACH (ovs, idl) {
2803 if (ovs->cur_cfg >= next_cfg) {
2807 ovsdb_idl_wait(idl);
2812 ovsdb_idl_destroy(idl);
2817 /* Our transaction needs to be rerun, or a prerequisite was not met. Free
2818 * resources and return so that the caller can try again. */
2819 ovsdb_idl_txn_abort(txn);
2820 ovsdb_idl_txn_destroy(txn);
2821 ovsdb_symbol_table_destroy(symtab);
2822 for (c = commands; c < &commands[n_commands]; c++) {
2823 ds_destroy(&c->output);
2828 static const struct vsctl_command_syntax all_commands[] = {
2829 /* Open vSwitch commands. */
2830 {"init", 0, 0, cmd_init, NULL, ""},
2832 /* Bridge commands. */
2833 {"add-br", 1, 3, cmd_add_br, NULL, "--may-exist"},
2834 {"del-br", 1, 1, cmd_del_br, NULL, "--if-exists"},
2835 {"list-br", 0, 0, cmd_list_br, NULL, ""},
2836 {"br-exists", 1, 1, cmd_br_exists, NULL, ""},
2837 {"br-to-vlan", 1, 1, cmd_br_to_vlan, NULL, ""},
2838 {"br-to-parent", 1, 1, cmd_br_to_parent, NULL, ""},
2839 {"br-set-external-id", 2, 3, cmd_br_set_external_id, NULL, ""},
2840 {"br-get-external-id", 1, 2, cmd_br_get_external_id, NULL, ""},
2842 /* Port commands. */
2843 {"list-ports", 1, 1, cmd_list_ports, NULL, ""},
2844 {"add-port", 2, INT_MAX, cmd_add_port, NULL, "--may-exist"},
2845 {"add-bond", 4, INT_MAX, cmd_add_bond, NULL, "--may-exist,--fake-iface"},
2846 {"del-port", 1, 2, cmd_del_port, NULL, "--if-exists,--with-iface"},
2847 {"port-to-br", 1, 1, cmd_port_to_br, NULL, ""},
2849 /* Interface commands. */
2850 {"list-ifaces", 1, 1, cmd_list_ifaces, NULL, ""},
2851 {"iface-to-br", 1, 1, cmd_iface_to_br, NULL, ""},
2853 /* Controller commands. */
2854 {"get-controller", 1, 1, cmd_get_controller, NULL, ""},
2855 {"del-controller", 1, 1, cmd_del_controller, NULL, ""},
2856 {"set-controller", 1, INT_MAX, cmd_set_controller, NULL, ""},
2857 {"get-fail-mode", 1, 1, cmd_get_fail_mode, NULL, ""},
2858 {"del-fail-mode", 1, 1, cmd_del_fail_mode, NULL, ""},
2859 {"set-fail-mode", 2, 2, cmd_set_fail_mode, NULL, ""},
2862 {"get-ssl", 0, 0, cmd_get_ssl, NULL, ""},
2863 {"del-ssl", 0, 0, cmd_del_ssl, NULL, ""},
2864 {"set-ssl", 3, 3, cmd_set_ssl, NULL, "--bootstrap"},
2866 /* Switch commands. */
2867 {"emer-reset", 0, 0, cmd_emer_reset, NULL, ""},
2869 /* Parameter commands. */
2870 {"get", 3, INT_MAX, cmd_get, NULL, "--if-exists"},
2871 {"list", 1, INT_MAX, cmd_list, NULL, ""},
2872 {"set", 3, INT_MAX, cmd_set, NULL, ""},
2873 {"add", 4, INT_MAX, cmd_add, NULL, ""},
2874 {"remove", 4, INT_MAX, cmd_remove, NULL, ""},
2875 {"clear", 3, INT_MAX, cmd_clear, NULL, ""},
2876 {"create", 2, INT_MAX, cmd_create, post_create, "--id="},
2877 {"destroy", 1, INT_MAX, cmd_destroy, NULL, "--if-exists"},
2878 {"wait-until", 2, INT_MAX, cmd_wait_until, NULL, ""},
2880 {NULL, 0, 0, NULL, NULL, NULL},