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 vsctl_context *ctx;
538 struct shash bridges;
544 vsctl_context_to_string(const struct vsctl_context *ctx)
546 const struct shash_node *node;
552 SHASH_FOR_EACH (node, &ctx->options) {
553 svec_add(&words, node->name);
555 for (i = 0; i < ctx->argc; i++) {
556 svec_add(&words, ctx->argv[i]);
558 svec_terminate(&words);
560 s = process_escape_args(words.names);
562 svec_destroy(&words);
567 static struct vsctl_bridge *
568 add_bridge(struct vsctl_info *b,
569 struct ovsrec_bridge *br_cfg, const char *name,
570 struct vsctl_bridge *parent, int vlan)
572 struct vsctl_bridge *br = xmalloc(sizeof *br);
574 br->name = xstrdup(name);
578 br->ctrl = parent->br_cfg->controller;
579 br->n_ctrl = parent->br_cfg->n_controller;
580 br->fail_mode = parent->br_cfg->fail_mode;
582 br->ctrl = br_cfg->controller;
583 br->n_ctrl = br_cfg->n_controller;
584 br->fail_mode = br_cfg->fail_mode;
586 shash_add(&b->bridges, br->name, br);
591 port_is_fake_bridge(const struct ovsrec_port *port_cfg)
593 return (port_cfg->fake_bridge
595 && *port_cfg->tag >= 1 && *port_cfg->tag <= 4095);
598 static struct vsctl_bridge *
599 find_vlan_bridge(struct vsctl_info *info,
600 struct vsctl_bridge *parent, int vlan)
602 struct shash_node *node;
604 SHASH_FOR_EACH (node, &info->bridges) {
605 struct vsctl_bridge *br = node->data;
606 if (br->parent == parent && br->vlan == vlan) {
615 free_info(struct vsctl_info *info)
617 struct shash_node *node;
619 SHASH_FOR_EACH (node, &info->bridges) {
620 struct vsctl_bridge *bridge = node->data;
624 shash_destroy(&info->bridges);
626 shash_destroy_free_data(&info->ports);
627 shash_destroy_free_data(&info->ifaces);
631 get_info(struct vsctl_context *ctx, struct vsctl_info *info)
633 const struct ovsrec_open_vswitch *ovs = ctx->ovs;
634 struct shash bridges, ports;
638 shash_init(&info->bridges);
639 shash_init(&info->ports);
640 shash_init(&info->ifaces);
642 shash_init(&bridges);
644 for (i = 0; i < ovs->n_bridges; i++) {
645 struct ovsrec_bridge *br_cfg = ovs->bridges[i];
646 struct vsctl_bridge *br;
649 if (!shash_add_once(&bridges, br_cfg->name, NULL)) {
650 VLOG_WARN("%s: database contains duplicate bridge name",
654 br = add_bridge(info, br_cfg, br_cfg->name, NULL, 0);
659 for (j = 0; j < br_cfg->n_ports; j++) {
660 struct ovsrec_port *port_cfg = br_cfg->ports[j];
662 if (!shash_add_once(&ports, port_cfg->name, NULL)) {
663 VLOG_WARN("%s: database contains duplicate port name",
668 if (port_is_fake_bridge(port_cfg)
669 && shash_add_once(&bridges, port_cfg->name, NULL)) {
670 add_bridge(info, NULL, port_cfg->name, br, *port_cfg->tag);
674 shash_destroy(&bridges);
675 shash_destroy(&ports);
677 shash_init(&bridges);
679 for (i = 0; i < ovs->n_bridges; i++) {
680 struct ovsrec_bridge *br_cfg = ovs->bridges[i];
681 struct vsctl_bridge *br;
684 if (!shash_add_once(&bridges, br_cfg->name, NULL)) {
687 br = shash_find_data(&info->bridges, br_cfg->name);
688 for (j = 0; j < br_cfg->n_ports; j++) {
689 struct ovsrec_port *port_cfg = br_cfg->ports[j];
690 struct vsctl_port *port;
693 if (!shash_add_once(&ports, port_cfg->name, NULL)) {
697 if (port_is_fake_bridge(port_cfg)
698 && !shash_add_once(&bridges, port_cfg->name, NULL)) {
702 port = xmalloc(sizeof *port);
703 port->port_cfg = port_cfg;
705 && *port_cfg->tag >= 1 && *port_cfg->tag <= 4095) {
706 port->bridge = find_vlan_bridge(info, br, *port_cfg->tag);
713 shash_add(&info->ports, port_cfg->name, port);
715 for (k = 0; k < port_cfg->n_interfaces; k++) {
716 struct ovsrec_interface *iface_cfg = port_cfg->interfaces[k];
717 struct vsctl_iface *iface;
719 if (shash_find(&info->ifaces, iface_cfg->name)) {
720 VLOG_WARN("%s: database contains duplicate interface name",
725 iface = xmalloc(sizeof *iface);
726 iface->iface_cfg = iface_cfg;
728 shash_add(&info->ifaces, iface_cfg->name, iface);
732 shash_destroy(&bridges);
733 shash_destroy(&ports);
737 check_conflicts(struct vsctl_info *info, const char *name,
740 struct vsctl_iface *iface;
741 struct vsctl_port *port;
743 if (shash_find(&info->bridges, name)) {
744 vsctl_fatal("%s because a bridge named %s already exists",
748 port = shash_find_data(&info->ports, name);
750 vsctl_fatal("%s because a port named %s already exists on "
751 "bridge %s", msg, name, port->bridge->name);
754 iface = shash_find_data(&info->ifaces, name);
756 vsctl_fatal("%s because an interface named %s already exists "
757 "on bridge %s", msg, name, iface->port->bridge->name);
763 static struct vsctl_bridge *
764 find_bridge(struct vsctl_info *info, const char *name, bool must_exist)
766 struct vsctl_bridge *br = shash_find_data(&info->bridges, name);
767 if (must_exist && !br) {
768 vsctl_fatal("no bridge named %s", name);
773 static struct vsctl_bridge *
774 find_real_bridge(struct vsctl_info *info, const char *name, bool must_exist)
776 struct vsctl_bridge *br = find_bridge(info, name, must_exist);
777 if (br && br->parent) {
778 vsctl_fatal("%s is a fake bridge", name);
783 static struct vsctl_port *
784 find_port(struct vsctl_info *info, const char *name, bool must_exist)
786 struct vsctl_port *port = shash_find_data(&info->ports, name);
787 if (port && !strcmp(name, port->bridge->name)) {
790 if (must_exist && !port) {
791 vsctl_fatal("no port named %s", name);
796 static struct vsctl_iface *
797 find_iface(struct vsctl_info *info, const char *name, bool must_exist)
799 struct vsctl_iface *iface = shash_find_data(&info->ifaces, name);
800 if (iface && !strcmp(name, iface->port->bridge->name)) {
803 if (must_exist && !iface) {
804 vsctl_fatal("no interface named %s", name);
810 bridge_insert_port(struct ovsrec_bridge *br, struct ovsrec_port *port)
812 struct ovsrec_port **ports;
815 ports = xmalloc(sizeof *br->ports * (br->n_ports + 1));
816 for (i = 0; i < br->n_ports; i++) {
817 ports[i] = br->ports[i];
819 ports[br->n_ports] = port;
820 ovsrec_bridge_set_ports(br, ports, br->n_ports + 1);
825 bridge_delete_port(struct ovsrec_bridge *br, struct ovsrec_port *port)
827 struct ovsrec_port **ports;
830 ports = xmalloc(sizeof *br->ports * br->n_ports);
831 for (i = n = 0; i < br->n_ports; i++) {
832 if (br->ports[i] != port) {
833 ports[n++] = br->ports[i];
836 ovsrec_bridge_set_ports(br, ports, n);
841 ovs_insert_bridge(const struct ovsrec_open_vswitch *ovs,
842 struct ovsrec_bridge *bridge)
844 struct ovsrec_bridge **bridges;
847 bridges = xmalloc(sizeof *ovs->bridges * (ovs->n_bridges + 1));
848 for (i = 0; i < ovs->n_bridges; i++) {
849 bridges[i] = ovs->bridges[i];
851 bridges[ovs->n_bridges] = bridge;
852 ovsrec_open_vswitch_set_bridges(ovs, bridges, ovs->n_bridges + 1);
857 ovs_delete_bridge(const struct ovsrec_open_vswitch *ovs,
858 struct ovsrec_bridge *bridge)
860 struct ovsrec_bridge **bridges;
863 bridges = xmalloc(sizeof *ovs->bridges * ovs->n_bridges);
864 for (i = n = 0; i < ovs->n_bridges; i++) {
865 if (ovs->bridges[i] != bridge) {
866 bridges[n++] = ovs->bridges[i];
869 ovsrec_open_vswitch_set_bridges(ovs, bridges, n);
874 cmd_init(struct vsctl_context *ctx OVS_UNUSED)
879 cmd_emer_reset(struct vsctl_context *ctx)
881 const struct ovsdb_idl *idl = ctx->idl;
882 const struct ovsrec_bridge *br;
883 const struct ovsrec_port *port;
884 const struct ovsrec_interface *iface;
885 const struct ovsrec_mirror *mirror, *next_mirror;
886 const struct ovsrec_controller *ctrl, *next_ctrl;
887 const struct ovsrec_netflow *nf, *next_nf;
888 const struct ovsrec_ssl *ssl, *next_ssl;
889 const struct ovsrec_sflow *sflow, *next_sflow;
892 /* Reset the Open_vSwitch table. */
893 ovsrec_open_vswitch_set_managers(ctx->ovs, NULL, 0);
894 ovsrec_open_vswitch_set_ssl(ctx->ovs, NULL);
896 OVSREC_BRIDGE_FOR_EACH (br, idl) {
898 char *hw_key = "hwaddr";
901 ovsrec_bridge_set_controller(br, NULL, 0);
902 ovsrec_bridge_set_mirrors(br, NULL, 0);
903 ovsrec_bridge_set_netflow(br, NULL);
904 ovsrec_bridge_set_sflow(br, NULL);
905 ovsrec_bridge_set_flood_vlans(br, NULL, 0);
907 /* We only want to save the "hwaddr" key from other_config. */
908 for (i=0; i < br->n_other_config; i++) {
909 if (!strcmp(br->key_other_config[i], hw_key)) {
910 hw_val = br->value_other_config[i];
915 char *val = xstrdup(hw_val);
916 ovsrec_bridge_set_other_config(br, &hw_key, &val, 1);
919 ovsrec_bridge_set_other_config(br, NULL, NULL, 0);
923 OVSREC_PORT_FOR_EACH (port, idl) {
924 ovsrec_port_set_other_config(port, NULL, NULL, 0);
927 OVSREC_INTERFACE_FOR_EACH (iface, idl) {
928 /* xxx What do we do about gre/patch devices created by mgr? */
930 ovsrec_interface_set_ingress_policing_rate(iface, 0);
931 ovsrec_interface_set_ingress_policing_burst(iface, 0);
934 OVSREC_MIRROR_FOR_EACH_SAFE (mirror, next_mirror, idl) {
935 ovsrec_mirror_delete(mirror);
938 OVSREC_CONTROLLER_FOR_EACH_SAFE (ctrl, next_ctrl, idl) {
939 ovsrec_controller_delete(ctrl);
942 OVSREC_NETFLOW_FOR_EACH_SAFE (nf, next_nf, idl) {
943 ovsrec_netflow_delete(nf);
946 OVSREC_SSL_FOR_EACH_SAFE (ssl, next_ssl, idl) {
947 ovsrec_ssl_delete(ssl);
950 OVSREC_SFLOW_FOR_EACH_SAFE (sflow, next_sflow, idl) {
951 ovsrec_sflow_delete(sflow);
956 cmd_add_br(struct vsctl_context *ctx)
958 bool may_exist = shash_find(&ctx->options, "--may-exist") != 0;
959 const char *br_name, *parent_name;
960 struct vsctl_info info;
963 br_name = ctx->argv[1];
964 if (ctx->argc == 2) {
967 } else if (ctx->argc == 4) {
968 parent_name = ctx->argv[2];
969 vlan = atoi(ctx->argv[3]);
970 if (vlan < 1 || vlan > 4095) {
971 vsctl_fatal("%s: vlan must be between 1 and 4095", ctx->argv[0]);
974 vsctl_fatal("'%s' command takes exactly 1 or 3 arguments",
978 get_info(ctx, &info);
980 struct vsctl_bridge *br;
982 br = find_bridge(&info, br_name, false);
986 vsctl_fatal("\"--may-exist add-br %s\" but %s is "
987 "a VLAN bridge for VLAN %d",
988 br_name, br_name, br->vlan);
992 vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
993 "is not a VLAN bridge",
994 br_name, parent_name, vlan, br_name);
995 } else if (strcmp(br->parent->name, parent_name)) {
996 vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
997 "has the wrong parent %s",
998 br_name, parent_name, vlan,
999 br_name, br->parent->name);
1000 } else if (br->vlan != vlan) {
1001 vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
1002 "is a VLAN bridge for the wrong VLAN %d",
1003 br_name, parent_name, vlan, br_name, br->vlan);
1009 check_conflicts(&info, br_name,
1010 xasprintf("cannot create a bridge named %s", br_name));
1013 struct ovsrec_port *port;
1014 struct ovsrec_interface *iface;
1015 struct ovsrec_bridge *br;
1017 iface = ovsrec_interface_insert(ctx->txn);
1018 ovsrec_interface_set_name(iface, br_name);
1020 port = ovsrec_port_insert(ctx->txn);
1021 ovsrec_port_set_name(port, br_name);
1022 ovsrec_port_set_interfaces(port, &iface, 1);
1024 br = ovsrec_bridge_insert(ctx->txn);
1025 ovsrec_bridge_set_name(br, br_name);
1026 ovsrec_bridge_set_ports(br, &port, 1);
1028 ovs_insert_bridge(ctx->ovs, br);
1030 struct vsctl_bridge *parent;
1031 struct ovsrec_port *port;
1032 struct ovsrec_interface *iface;
1033 struct ovsrec_bridge *br;
1036 parent = find_bridge(&info, parent_name, false);
1037 if (parent && parent->vlan) {
1038 vsctl_fatal("cannot create bridge with fake bridge as parent");
1041 vsctl_fatal("parent bridge %s does not exist", parent_name);
1043 br = parent->br_cfg;
1045 iface = ovsrec_interface_insert(ctx->txn);
1046 ovsrec_interface_set_name(iface, br_name);
1047 ovsrec_interface_set_type(iface, "internal");
1049 port = ovsrec_port_insert(ctx->txn);
1050 ovsrec_port_set_name(port, br_name);
1051 ovsrec_port_set_interfaces(port, &iface, 1);
1052 ovsrec_port_set_fake_bridge(port, true);
1053 ovsrec_port_set_tag(port, &tag, 1);
1055 bridge_insert_port(br, port);
1062 del_port(struct vsctl_info *info, struct vsctl_port *port)
1064 struct shash_node *node;
1066 SHASH_FOR_EACH (node, &info->ifaces) {
1067 struct vsctl_iface *iface = node->data;
1068 if (iface->port == port) {
1069 ovsrec_interface_delete(iface->iface_cfg);
1072 ovsrec_port_delete(port->port_cfg);
1074 bridge_delete_port((port->bridge->parent
1075 ? port->bridge->parent->br_cfg
1076 : port->bridge->br_cfg), port->port_cfg);
1080 cmd_del_br(struct vsctl_context *ctx)
1082 bool must_exist = !shash_find(&ctx->options, "--if-exists");
1083 struct vsctl_bridge *bridge;
1084 struct vsctl_info info;
1086 get_info(ctx, &info);
1087 bridge = find_bridge(&info, ctx->argv[1], must_exist);
1089 struct shash_node *node;
1091 SHASH_FOR_EACH (node, &info.ports) {
1092 struct vsctl_port *port = node->data;
1093 if (port->bridge == bridge || port->bridge->parent == bridge
1094 || !strcmp(port->port_cfg->name, bridge->name)) {
1095 del_port(&info, port);
1098 if (bridge->br_cfg) {
1099 ovsrec_bridge_delete(bridge->br_cfg);
1100 ovs_delete_bridge(ctx->ovs, bridge->br_cfg);
1107 output_sorted(struct svec *svec, struct ds *output)
1113 SVEC_FOR_EACH (i, name, svec) {
1114 ds_put_format(output, "%s\n", name);
1119 cmd_list_br(struct vsctl_context *ctx)
1121 struct shash_node *node;
1122 struct vsctl_info info;
1123 struct svec bridges;
1125 get_info(ctx, &info);
1127 svec_init(&bridges);
1128 SHASH_FOR_EACH (node, &info.bridges) {
1129 struct vsctl_bridge *br = node->data;
1130 svec_add(&bridges, br->name);
1132 output_sorted(&bridges, &ctx->output);
1133 svec_destroy(&bridges);
1139 cmd_br_exists(struct vsctl_context *ctx)
1141 struct vsctl_info info;
1143 get_info(ctx, &info);
1144 if (!find_bridge(&info, ctx->argv[1], false)) {
1150 /* Returns true if 'b_prefix' (of length 'b_prefix_len') concatenated with 'b'
1151 * equals 'a', false otherwise. */
1153 key_matches(const char *a,
1154 const char *b_prefix, size_t b_prefix_len, const char *b)
1156 return !strncmp(a, b_prefix, b_prefix_len) && !strcmp(a + b_prefix_len, b);
1160 set_external_id(char **old_keys, char **old_values, size_t old_n,
1161 char *key, char *value,
1162 char ***new_keysp, char ***new_valuesp, size_t *new_np)
1169 new_keys = xmalloc(sizeof *new_keys * (old_n + 1));
1170 new_values = xmalloc(sizeof *new_values * (old_n + 1));
1172 for (i = 0; i < old_n; i++) {
1173 if (strcmp(key, old_keys[i])) {
1174 new_keys[new_n] = old_keys[i];
1175 new_values[new_n] = old_values[i];
1180 new_keys[new_n] = key;
1181 new_values[new_n] = value;
1184 *new_keysp = new_keys;
1185 *new_valuesp = new_values;
1190 cmd_br_set_external_id(struct vsctl_context *ctx)
1192 struct vsctl_info info;
1193 struct vsctl_bridge *bridge;
1194 char **keys, **values;
1197 get_info(ctx, &info);
1198 bridge = find_bridge(&info, ctx->argv[1], true);
1199 if (bridge->br_cfg) {
1200 set_external_id(bridge->br_cfg->key_external_ids,
1201 bridge->br_cfg->value_external_ids,
1202 bridge->br_cfg->n_external_ids,
1203 ctx->argv[2], ctx->argc >= 4 ? ctx->argv[3] : NULL,
1204 &keys, &values, &n);
1205 ovsrec_bridge_set_external_ids(bridge->br_cfg, keys, values, n);
1207 char *key = xasprintf("fake-bridge-%s", ctx->argv[2]);
1208 struct vsctl_port *port = shash_find_data(&info.ports, ctx->argv[1]);
1209 set_external_id(port->port_cfg->key_external_ids,
1210 port->port_cfg->value_external_ids,
1211 port->port_cfg->n_external_ids,
1212 key, ctx->argc >= 4 ? ctx->argv[3] : NULL,
1213 &keys, &values, &n);
1214 ovsrec_port_set_external_ids(port->port_cfg, keys, values, n);
1224 get_external_id(char **keys, char **values, size_t n,
1225 const char *prefix, const char *key,
1228 size_t prefix_len = strlen(prefix);
1233 for (i = 0; i < n; i++) {
1234 if (!key && !strncmp(keys[i], prefix, prefix_len)) {
1235 svec_add_nocopy(&svec, xasprintf("%s=%s",
1236 keys[i] + prefix_len, values[i]));
1237 } else if (key_matches(keys[i], prefix, prefix_len, key)) {
1238 svec_add(&svec, values[i]);
1242 output_sorted(&svec, output);
1243 svec_destroy(&svec);
1247 cmd_br_get_external_id(struct vsctl_context *ctx)
1249 struct vsctl_info info;
1250 struct vsctl_bridge *bridge;
1252 get_info(ctx, &info);
1253 bridge = find_bridge(&info, ctx->argv[1], true);
1254 if (bridge->br_cfg) {
1255 get_external_id(bridge->br_cfg->key_external_ids,
1256 bridge->br_cfg->value_external_ids,
1257 bridge->br_cfg->n_external_ids,
1258 "", ctx->argc >= 3 ? ctx->argv[2] : NULL,
1261 struct vsctl_port *port = shash_find_data(&info.ports, ctx->argv[1]);
1262 get_external_id(port->port_cfg->key_external_ids,
1263 port->port_cfg->value_external_ids,
1264 port->port_cfg->n_external_ids,
1265 "fake-bridge-", ctx->argc >= 3 ? ctx->argv[2] : NULL, &ctx->output);
1272 cmd_list_ports(struct vsctl_context *ctx)
1274 struct vsctl_bridge *br;
1275 struct shash_node *node;
1276 struct vsctl_info info;
1279 get_info(ctx, &info);
1280 br = find_bridge(&info, ctx->argv[1], true);
1283 SHASH_FOR_EACH (node, &info.ports) {
1284 struct vsctl_port *port = node->data;
1286 if (strcmp(port->port_cfg->name, br->name) && br == port->bridge) {
1287 svec_add(&ports, port->port_cfg->name);
1290 output_sorted(&ports, &ctx->output);
1291 svec_destroy(&ports);
1297 add_port(struct vsctl_context *ctx,
1298 const char *br_name, const char *port_name,
1299 bool may_exist, bool fake_iface,
1300 char *iface_names[], int n_ifaces,
1301 char *settings[], int n_settings)
1303 struct vsctl_info info;
1304 struct vsctl_bridge *bridge;
1305 struct ovsrec_interface **ifaces;
1306 struct ovsrec_port *port;
1309 get_info(ctx, &info);
1311 struct vsctl_port *port;
1313 port = find_port(&info, port_name, false);
1315 struct svec want_names, have_names;
1318 svec_init(&want_names);
1319 for (i = 0; i < n_ifaces; i++) {
1320 svec_add(&want_names, iface_names[i]);
1322 svec_sort(&want_names);
1324 svec_init(&have_names);
1325 for (i = 0; i < port->port_cfg->n_interfaces; i++) {
1326 svec_add(&have_names, port->port_cfg->interfaces[i]->name);
1328 svec_sort(&have_names);
1330 if (strcmp(port->bridge->name, br_name)) {
1331 char *command = vsctl_context_to_string(ctx);
1332 vsctl_fatal("\"%s\" but %s is actually attached to bridge %s",
1333 command, port_name, port->bridge->name);
1336 if (!svec_equal(&want_names, &have_names)) {
1337 char *have_names_string = svec_join(&have_names, ", ", "");
1338 char *command = vsctl_context_to_string(ctx);
1340 vsctl_fatal("\"%s\" but %s actually has interface(s) %s",
1341 command, port_name, have_names_string);
1344 svec_destroy(&want_names);
1345 svec_destroy(&have_names);
1350 check_conflicts(&info, port_name,
1351 xasprintf("cannot create a port named %s", port_name));
1352 for (i = 0; i < n_ifaces; i++) {
1353 check_conflicts(&info, iface_names[i],
1354 xasprintf("cannot create an interface named %s",
1357 bridge = find_bridge(&info, br_name, true);
1359 ifaces = xmalloc(n_ifaces * sizeof *ifaces);
1360 for (i = 0; i < n_ifaces; i++) {
1361 ifaces[i] = ovsrec_interface_insert(ctx->txn);
1362 ovsrec_interface_set_name(ifaces[i], iface_names[i]);
1365 port = ovsrec_port_insert(ctx->txn);
1366 ovsrec_port_set_name(port, port_name);
1367 ovsrec_port_set_interfaces(port, ifaces, n_ifaces);
1368 ovsrec_port_set_bond_fake_iface(port, fake_iface);
1372 int64_t tag = bridge->vlan;
1373 ovsrec_port_set_tag(port, &tag, 1);
1376 for (i = 0; i < n_settings; i++) {
1377 set_column(get_table("Port"), &port->header_, settings[i],
1381 bridge_insert_port((bridge->parent ? bridge->parent->br_cfg
1382 : bridge->br_cfg), port);
1388 cmd_add_port(struct vsctl_context *ctx)
1390 bool may_exist = shash_find(&ctx->options, "--may-exist") != 0;
1392 add_port(ctx, ctx->argv[1], ctx->argv[2], may_exist, false,
1393 &ctx->argv[2], 1, &ctx->argv[3], ctx->argc - 3);
1397 cmd_add_bond(struct vsctl_context *ctx)
1399 bool may_exist = shash_find(&ctx->options, "--may-exist") != 0;
1400 bool fake_iface = shash_find(&ctx->options, "--fake-iface");
1404 n_ifaces = ctx->argc - 3;
1405 for (i = 3; i < ctx->argc; i++) {
1406 if (strchr(ctx->argv[i], '=')) {
1412 vsctl_fatal("add-bond requires at least 2 interfaces, but only "
1413 "%d were specified", n_ifaces);
1416 add_port(ctx, ctx->argv[1], ctx->argv[2], may_exist, fake_iface,
1417 &ctx->argv[3], n_ifaces,
1418 &ctx->argv[n_ifaces + 3], ctx->argc - 3 - n_ifaces);
1422 cmd_del_port(struct vsctl_context *ctx)
1424 bool must_exist = !shash_find(&ctx->options, "--if-exists");
1425 bool with_iface = shash_find(&ctx->options, "--with-iface") != NULL;
1426 struct vsctl_port *port;
1427 struct vsctl_info info;
1429 get_info(ctx, &info);
1431 port = find_port(&info, ctx->argv[ctx->argc - 1], must_exist);
1433 const char *target = ctx->argv[ctx->argc - 1];
1434 struct vsctl_iface *iface;
1436 port = find_port(&info, target, false);
1438 iface = find_iface(&info, target, false);
1443 if (must_exist && !port) {
1444 vsctl_fatal("no port or interface named %s", target);
1449 if (ctx->argc == 3) {
1450 struct vsctl_bridge *bridge;
1452 bridge = find_bridge(&info, ctx->argv[1], true);
1453 if (port->bridge != bridge) {
1454 if (port->bridge->parent == bridge) {
1455 vsctl_fatal("bridge %s does not have a port %s (although "
1456 "its parent bridge %s does)",
1457 ctx->argv[1], ctx->argv[2],
1458 bridge->parent->name);
1460 vsctl_fatal("bridge %s does not have a port %s",
1461 ctx->argv[1], ctx->argv[2]);
1466 del_port(&info, port);
1473 cmd_port_to_br(struct vsctl_context *ctx)
1475 struct vsctl_port *port;
1476 struct vsctl_info info;
1478 get_info(ctx, &info);
1479 port = find_port(&info, ctx->argv[1], true);
1480 ds_put_format(&ctx->output, "%s\n", port->bridge->name);
1485 cmd_br_to_vlan(struct vsctl_context *ctx)
1487 struct vsctl_bridge *bridge;
1488 struct vsctl_info info;
1490 get_info(ctx, &info);
1491 bridge = find_bridge(&info, ctx->argv[1], true);
1492 ds_put_format(&ctx->output, "%d\n", bridge->vlan);
1497 cmd_br_to_parent(struct vsctl_context *ctx)
1499 struct vsctl_bridge *bridge;
1500 struct vsctl_info info;
1502 get_info(ctx, &info);
1503 bridge = find_bridge(&info, ctx->argv[1], true);
1504 if (bridge->parent) {
1505 bridge = bridge->parent;
1507 ds_put_format(&ctx->output, "%s\n", bridge->name);
1512 cmd_list_ifaces(struct vsctl_context *ctx)
1514 struct vsctl_bridge *br;
1515 struct shash_node *node;
1516 struct vsctl_info info;
1519 get_info(ctx, &info);
1520 br = find_bridge(&info, ctx->argv[1], true);
1523 SHASH_FOR_EACH (node, &info.ifaces) {
1524 struct vsctl_iface *iface = node->data;
1526 if (strcmp(iface->iface_cfg->name, br->name)
1527 && br == iface->port->bridge) {
1528 svec_add(&ifaces, iface->iface_cfg->name);
1531 output_sorted(&ifaces, &ctx->output);
1532 svec_destroy(&ifaces);
1538 cmd_iface_to_br(struct vsctl_context *ctx)
1540 struct vsctl_iface *iface;
1541 struct vsctl_info info;
1543 get_info(ctx, &info);
1544 iface = find_iface(&info, ctx->argv[1], true);
1545 ds_put_format(&ctx->output, "%s\n", iface->port->bridge->name);
1550 cmd_get_controller(struct vsctl_context *ctx)
1552 struct vsctl_info info;
1553 struct vsctl_bridge *br;
1554 struct svec targets;
1557 get_info(ctx, &info);
1558 br = find_bridge(&info, ctx->argv[1], true);
1560 /* Print the targets in sorted order for reproducibility. */
1561 svec_init(&targets);
1562 for (i = 0; i < br->n_ctrl; i++) {
1563 svec_add(&targets, br->ctrl[i]->target);
1566 svec_sort(&targets);
1567 for (i = 0; i < targets.n; i++) {
1568 ds_put_format(&ctx->output, "%s\n", targets.names[i]);
1570 svec_destroy(&targets);
1576 delete_controllers(struct ovsrec_controller **controllers,
1577 size_t n_controllers)
1581 for (i = 0; i < n_controllers; i++) {
1582 ovsrec_controller_delete(controllers[i]);
1587 cmd_del_controller(struct vsctl_context *ctx)
1589 struct vsctl_info info;
1590 struct vsctl_bridge *br;
1592 get_info(ctx, &info);
1593 br = find_real_bridge(&info, ctx->argv[1], true);
1596 delete_controllers(br->ctrl, br->n_ctrl);
1597 ovsrec_bridge_set_controller(br->br_cfg, NULL, 0);
1603 static struct ovsrec_controller **
1604 insert_controllers(struct ovsdb_idl_txn *txn, char *targets[], size_t n)
1606 struct ovsrec_controller **controllers;
1609 controllers = xmalloc(n * sizeof *controllers);
1610 for (i = 0; i < n; i++) {
1611 controllers[i] = ovsrec_controller_insert(txn);
1612 ovsrec_controller_set_target(controllers[i], targets[i]);
1619 cmd_set_controller(struct vsctl_context *ctx)
1621 struct vsctl_info info;
1622 struct vsctl_bridge *br;
1623 struct ovsrec_controller **controllers;
1626 get_info(ctx, &info);
1627 br = find_real_bridge(&info, ctx->argv[1], true);
1629 delete_controllers(br->ctrl, br->n_ctrl);
1632 controllers = insert_controllers(ctx->txn, &ctx->argv[2], n);
1633 ovsrec_bridge_set_controller(br->br_cfg, controllers, n);
1640 cmd_get_fail_mode(struct vsctl_context *ctx)
1642 struct vsctl_info info;
1643 struct vsctl_bridge *br;
1645 get_info(ctx, &info);
1646 br = find_bridge(&info, ctx->argv[1], true);
1648 if (br->fail_mode && strlen(br->fail_mode)) {
1649 ds_put_format(&ctx->output, "%s\n", br->fail_mode);
1656 cmd_del_fail_mode(struct vsctl_context *ctx)
1658 struct vsctl_info info;
1659 struct vsctl_bridge *br;
1661 get_info(ctx, &info);
1662 br = find_real_bridge(&info, ctx->argv[1], true);
1664 ovsrec_bridge_set_fail_mode(br->br_cfg, NULL);
1670 cmd_set_fail_mode(struct vsctl_context *ctx)
1672 struct vsctl_info info;
1673 struct vsctl_bridge *br;
1674 const char *fail_mode = ctx->argv[2];
1676 get_info(ctx, &info);
1677 br = find_real_bridge(&info, ctx->argv[1], true);
1679 if (strcmp(fail_mode, "standalone") && strcmp(fail_mode, "secure")) {
1680 vsctl_fatal("fail-mode must be \"standalone\" or \"secure\"");
1683 ovsrec_bridge_set_fail_mode(br->br_cfg, fail_mode);
1689 cmd_get_ssl(struct vsctl_context *ctx)
1691 struct ovsrec_ssl *ssl = ctx->ovs->ssl;
1694 ds_put_format(&ctx->output, "Private key: %s\n", ssl->private_key);
1695 ds_put_format(&ctx->output, "Certificate: %s\n", ssl->certificate);
1696 ds_put_format(&ctx->output, "CA Certificate: %s\n", ssl->ca_cert);
1697 ds_put_format(&ctx->output, "Bootstrap: %s\n",
1698 ssl->bootstrap_ca_cert ? "true" : "false");
1703 cmd_del_ssl(struct vsctl_context *ctx)
1705 struct ovsrec_ssl *ssl = ctx->ovs->ssl;
1708 ovsrec_ssl_delete(ssl);
1709 ovsrec_open_vswitch_set_ssl(ctx->ovs, NULL);
1714 cmd_set_ssl(struct vsctl_context *ctx)
1716 bool bootstrap = shash_find(&ctx->options, "--bootstrap");
1717 struct ovsrec_ssl *ssl = ctx->ovs->ssl;
1720 ovsrec_ssl_delete(ssl);
1722 ssl = ovsrec_ssl_insert(ctx->txn);
1724 ovsrec_ssl_set_private_key(ssl, ctx->argv[1]);
1725 ovsrec_ssl_set_certificate(ssl, ctx->argv[2]);
1726 ovsrec_ssl_set_ca_cert(ssl, ctx->argv[3]);
1728 ovsrec_ssl_set_bootstrap_ca_cert(ssl, bootstrap);
1730 ovsrec_open_vswitch_set_ssl(ctx->ovs, ssl);
1733 /* Parameter commands. */
1735 struct vsctl_row_id {
1736 const struct ovsdb_idl_table_class *table;
1737 const struct ovsdb_idl_column *name_column;
1738 const struct ovsdb_idl_column *uuid_column;
1741 struct vsctl_table_class {
1742 struct ovsdb_idl_table_class *class;
1743 struct vsctl_row_id row_ids[2];
1746 static const struct vsctl_table_class tables[] = {
1747 {&ovsrec_table_bridge,
1748 {{&ovsrec_table_bridge, &ovsrec_bridge_col_name, NULL},
1749 {NULL, NULL, NULL}}},
1751 {&ovsrec_table_controller,
1752 {{&ovsrec_table_bridge,
1753 &ovsrec_bridge_col_name,
1754 &ovsrec_bridge_col_controller}}},
1756 {&ovsrec_table_interface,
1757 {{&ovsrec_table_interface, &ovsrec_interface_col_name, NULL},
1758 {NULL, NULL, NULL}}},
1760 {&ovsrec_table_mirror,
1761 {{&ovsrec_table_mirror, &ovsrec_mirror_col_name, NULL},
1762 {NULL, NULL, NULL}}},
1764 {&ovsrec_table_netflow,
1765 {{&ovsrec_table_bridge,
1766 &ovsrec_bridge_col_name,
1767 &ovsrec_bridge_col_netflow},
1768 {NULL, NULL, NULL}}},
1770 {&ovsrec_table_open_vswitch,
1771 {{&ovsrec_table_open_vswitch, NULL, NULL},
1772 {NULL, NULL, NULL}}},
1774 {&ovsrec_table_port,
1775 {{&ovsrec_table_port, &ovsrec_port_col_name, NULL},
1776 {NULL, NULL, NULL}}},
1779 {{&ovsrec_table_port, &ovsrec_port_col_name, &ovsrec_port_col_qos},
1780 {NULL, NULL, NULL}}},
1782 {&ovsrec_table_queue,
1783 {{NULL, NULL, NULL},
1784 {NULL, NULL, NULL}}},
1787 {{&ovsrec_table_open_vswitch, NULL, &ovsrec_open_vswitch_col_ssl}}},
1789 {&ovsrec_table_sflow,
1790 {{&ovsrec_table_bridge,
1791 &ovsrec_bridge_col_name,
1792 &ovsrec_bridge_col_sflow},
1793 {NULL, NULL, NULL}}},
1795 {NULL, {{NULL, NULL, NULL}, {NULL, NULL, NULL}}}
1799 die_if_error(char *error)
1802 vsctl_fatal("%s", error);
1807 to_lower_and_underscores(unsigned c)
1809 return c == '-' ? '_' : tolower(c);
1813 score_partial_match(const char *name, const char *s)
1817 if (!strcmp(name, s)) {
1820 for (score = 0; ; score++, name++, s++) {
1821 if (to_lower_and_underscores(*name) != to_lower_and_underscores(*s)) {
1823 } else if (*name == '\0') {
1824 return UINT_MAX - 1;
1827 return *s == '\0' ? score : 0;
1830 static const struct vsctl_table_class *
1831 get_table(const char *table_name)
1833 const struct vsctl_table_class *table;
1834 const struct vsctl_table_class *best_match = NULL;
1835 unsigned int best_score = 0;
1837 for (table = tables; table->class; table++) {
1838 unsigned int score = score_partial_match(table->class->name,
1840 if (score > best_score) {
1843 } else if (score == best_score) {
1849 } else if (best_score) {
1850 vsctl_fatal("multiple table names match \"%s\"", table_name);
1852 vsctl_fatal("unknown table \"%s\"", table_name);
1856 static const struct ovsdb_idl_row *
1857 get_row_by_id(struct vsctl_context *ctx, const struct vsctl_table_class *table,
1858 const struct vsctl_row_id *id, const char *record_id)
1860 const struct ovsdb_idl_row *referrer, *final;
1866 if (!id->name_column) {
1867 if (strcmp(record_id, ".")) {
1870 referrer = ovsdb_idl_first_row(ctx->idl, id->table);
1871 if (!referrer || ovsdb_idl_next_row(referrer)) {
1875 const struct ovsdb_idl_row *row;
1878 for (row = ovsdb_idl_first_row(ctx->idl, id->table);
1880 row = ovsdb_idl_next_row(row))
1882 const struct ovsdb_datum *name;
1884 name = ovsdb_idl_get(row, id->name_column,
1885 OVSDB_TYPE_STRING, OVSDB_TYPE_VOID);
1886 if (name->n == 1 && !strcmp(name->keys[0].string, record_id)) {
1888 vsctl_fatal("multiple rows in %s match \"%s\"",
1889 table->class->name, record_id);
1900 if (id->uuid_column) {
1901 const struct ovsdb_datum *uuid;
1903 uuid = ovsdb_idl_get(referrer, id->uuid_column,
1904 OVSDB_TYPE_UUID, OVSDB_TYPE_VOID);
1906 final = ovsdb_idl_get_row_for_uuid(ctx->idl, table->class,
1907 &uuid->keys[0].uuid);
1916 static const struct ovsdb_idl_row *
1917 get_row (struct vsctl_context *ctx,
1918 const struct vsctl_table_class *table, const char *record_id)
1920 const struct ovsdb_idl_row *row;
1923 if (uuid_from_string(&uuid, record_id)) {
1924 row = ovsdb_idl_get_row_for_uuid(ctx->idl, table->class, &uuid);
1928 for (i = 0; i < ARRAY_SIZE(table->row_ids); i++) {
1929 row = get_row_by_id(ctx, table, &table->row_ids[i], record_id);
1938 static const struct ovsdb_idl_row *
1939 must_get_row(struct vsctl_context *ctx,
1940 const struct vsctl_table_class *table, const char *record_id)
1942 const struct ovsdb_idl_row *row = get_row(ctx, table, record_id);
1944 vsctl_fatal("no row \"%s\" in table %s",
1945 record_id, table->class->name);
1951 get_column(const struct vsctl_table_class *table, const char *column_name,
1952 const struct ovsdb_idl_column **columnp)
1954 const struct ovsdb_idl_column *best_match = NULL;
1955 unsigned int best_score = 0;
1958 for (i = 0; i < table->class->n_columns; i++) {
1959 const struct ovsdb_idl_column *column = &table->class->columns[i];
1960 unsigned int score = score_partial_match(column->name, column_name);
1961 if (score > best_score) {
1962 best_match = column;
1964 } else if (score == best_score) {
1969 *columnp = best_match;
1972 } else if (best_score) {
1973 return xasprintf("%s contains more than one column whose name "
1974 "matches \"%s\"", table->class->name, column_name);
1976 return xasprintf("%s does not contain a column whose name matches "
1977 "\"%s\"", table->class->name, column_name);
1982 missing_operator_error(const char *arg, const char **allowed_operators,
1988 ds_put_format(&s, "%s: argument does not end in ", arg);
1989 ds_put_format(&s, "\"%s\"", allowed_operators[0]);
1990 if (n_allowed == 2) {
1991 ds_put_format(&s, " or \"%s\"", allowed_operators[1]);
1992 } else if (n_allowed > 2) {
1995 for (i = 1; i < n_allowed - 1; i++) {
1996 ds_put_format(&s, ", \"%s\"", allowed_operators[i]);
1998 ds_put_format(&s, ", or \"%s\"", allowed_operators[i]);
2000 ds_put_format(&s, " followed by a value.");
2002 return ds_steal_cstr(&s);
2005 /* Breaks 'arg' apart into a number of fields in the following order:
2007 * - If 'columnp' is nonnull, the name of a column in 'table'. The column
2008 * is stored into '*columnp'. The column name may be abbreviated.
2010 * - If 'keyp' is nonnull, optionally a key string. (If both 'columnp'
2011 * and 'keyp' are nonnull, then the column and key names are expected to
2012 * be separated by ':'). The key is stored as a malloc()'d string into
2013 * '*keyp', or NULL if no key is present in 'arg'.
2015 * - If 'valuep' is nonnull, an operator followed by a value string. The
2016 * allowed operators are the 'n_allowed' string in 'allowed_operators',
2017 * or just "=" if 'n_allowed' is 0. If 'operatorp' is nonnull, then the
2018 * operator is stored into '*operatorp' (one of the pointers from
2019 * 'allowed_operators' is stored; nothing is malloc()'d). The value is
2020 * stored as a malloc()'d string into '*valuep', or NULL if no value is
2023 * At least 'columnp' or 'keyp' must be nonnull.
2025 * On success, returns NULL. On failure, returned a malloc()'d string error
2026 * message and stores NULL into all of the nonnull output arguments. */
2027 static char * WARN_UNUSED_RESULT
2028 parse_column_key_value(const char *arg,
2029 const struct vsctl_table_class *table,
2030 const struct ovsdb_idl_column **columnp, char **keyp,
2031 const char **operatorp,
2032 const char **allowed_operators, size_t n_allowed,
2035 const char *p = arg;
2038 assert(columnp || keyp);
2039 assert(!(operatorp && !valuep));
2047 /* Parse column name. */
2051 error = ovsdb_token_parse(&p, &column_name);
2055 if (column_name[0] == '\0') {
2057 error = xasprintf("%s: missing column name", arg);
2060 error = get_column(table, column_name, columnp);
2067 /* Parse key string. */
2068 if (*p == ':' || !columnp) {
2072 error = xasprintf("%s: key not accepted here", arg);
2075 error = ovsdb_token_parse(&p, keyp);
2083 /* Parse value string. */
2089 if (!allowed_operators) {
2090 static const char *equals = "=";
2091 allowed_operators = =
2097 for (i = 0; i < n_allowed; i++) {
2098 const char *op = allowed_operators[i];
2099 size_t op_len = strlen(op);
2101 if (op_len > best_len && !strncmp(op, p, op_len) && p[op_len]) {
2107 error = missing_operator_error(arg, allowed_operators, n_allowed);
2114 *valuep = xstrdup(p + best_len);
2120 error = xasprintf("%s: trailing garbage \"%s\" in argument",
2146 cmd_get(struct vsctl_context *ctx)
2148 bool if_exists = shash_find(&ctx->options, "--if-exists");
2149 const char *table_name = ctx->argv[1];
2150 const char *record_id = ctx->argv[2];
2151 const struct vsctl_table_class *table;
2152 const struct ovsdb_idl_row *row;
2153 struct ds *out = &ctx->output;
2156 table = get_table(table_name);
2157 row = must_get_row(ctx, table, record_id);
2158 for (i = 3; i < ctx->argc; i++) {
2159 const struct ovsdb_idl_column *column;
2160 const struct ovsdb_datum *datum;
2163 /* Special case for obtaining the UUID of a row. We can't just do this
2164 * through parse_column_key_value() below since it returns a "struct
2165 * ovsdb_idl_column" and the UUID column doesn't have one. */
2166 if (!strcasecmp(ctx->argv[i], "_uuid")
2167 || !strcasecmp(ctx->argv[i], "-uuid")) {
2168 ds_put_format(out, UUID_FMT"\n", UUID_ARGS(&row->uuid));
2172 die_if_error(parse_column_key_value(ctx->argv[i], table,
2173 &column, &key_string,
2174 NULL, NULL, 0, NULL));
2176 datum = ovsdb_idl_read(row, column);
2178 union ovsdb_atom key;
2181 if (column->type.value.type == OVSDB_TYPE_VOID) {
2182 vsctl_fatal("cannot specify key to get for non-map column %s",
2186 die_if_error(ovsdb_atom_from_string(&key,
2188 key_string, ctx->symtab));
2190 idx = ovsdb_datum_find_key(datum, &key,
2191 column->type.key.type);
2192 if (idx == UINT_MAX) {
2194 vsctl_fatal("no key \"%s\" in %s record \"%s\" column %s",
2195 key_string, table->class->name, record_id,
2199 ovsdb_atom_to_string(&datum->values[idx],
2200 column->type.value.type, out);
2202 ovsdb_atom_destroy(&key, column->type.key.type);
2204 ovsdb_datum_to_string(datum, &column->type, out);
2206 ds_put_char(out, '\n');
2213 list_record(const struct vsctl_table_class *table,
2214 const struct ovsdb_idl_row *row, struct ds *out)
2218 ds_put_format(out, "%-20s: "UUID_FMT"\n", "_uuid",
2219 UUID_ARGS(&row->uuid));
2220 for (i = 0; i < table->class->n_columns; i++) {
2221 const struct ovsdb_idl_column *column = &table->class->columns[i];
2222 const struct ovsdb_datum *datum;
2224 datum = ovsdb_idl_read(row, column);
2226 ds_put_format(out, "%-20s: ", column->name);
2227 ovsdb_datum_to_string(datum, &column->type, out);
2228 ds_put_char(out, '\n');
2233 cmd_list(struct vsctl_context *ctx)
2235 const char *table_name = ctx->argv[1];
2236 const struct vsctl_table_class *table;
2237 struct ds *out = &ctx->output;
2240 table = get_table(table_name);
2241 if (ctx->argc > 2) {
2242 for (i = 2; i < ctx->argc; i++) {
2244 ds_put_char(out, '\n');
2246 list_record(table, must_get_row(ctx, table, ctx->argv[i]), out);
2249 const struct ovsdb_idl_row *row;
2252 for (row = ovsdb_idl_first_row(ctx->idl, table->class), first = true;
2254 row = ovsdb_idl_next_row(row), first = false) {
2256 ds_put_char(out, '\n');
2258 list_record(table, row, out);
2264 set_column(const struct vsctl_table_class *table,
2265 const struct ovsdb_idl_row *row, const char *arg,
2266 struct ovsdb_symbol_table *symtab)
2268 const struct ovsdb_idl_column *column;
2269 char *key_string, *value_string;
2272 error = parse_column_key_value(arg, table, &column, &key_string,
2273 NULL, NULL, 0, &value_string);
2274 die_if_error(error);
2275 if (!value_string) {
2276 vsctl_fatal("%s: missing value", arg);
2280 union ovsdb_atom key, value;
2281 struct ovsdb_datum datum;
2283 if (column->type.value.type == OVSDB_TYPE_VOID) {
2284 vsctl_fatal("cannot specify key to set for non-map column %s",
2288 die_if_error(ovsdb_atom_from_string(&key, &column->type.key,
2289 key_string, symtab));
2290 die_if_error(ovsdb_atom_from_string(&value, &column->type.value,
2291 value_string, symtab));
2293 ovsdb_datum_init_empty(&datum);
2294 ovsdb_datum_add_unsafe(&datum, &key, &value, &column->type);
2296 ovsdb_atom_destroy(&key, column->type.key.type);
2297 ovsdb_atom_destroy(&value, column->type.value.type);
2299 ovsdb_datum_union(&datum, ovsdb_idl_read(row, column),
2300 &column->type, false);
2301 ovsdb_idl_txn_write(row, column, &datum);
2303 struct ovsdb_datum datum;
2305 die_if_error(ovsdb_datum_from_string(&datum, &column->type,
2306 value_string, symtab));
2307 ovsdb_idl_txn_write(row, column, &datum);
2315 cmd_set(struct vsctl_context *ctx)
2317 const char *table_name = ctx->argv[1];
2318 const char *record_id = ctx->argv[2];
2319 const struct vsctl_table_class *table;
2320 const struct ovsdb_idl_row *row;
2323 table = get_table(table_name);
2324 row = must_get_row(ctx, table, record_id);
2325 for (i = 3; i < ctx->argc; i++) {
2326 set_column(table, row, ctx->argv[i], ctx->symtab);
2331 cmd_add(struct vsctl_context *ctx)
2333 const char *table_name = ctx->argv[1];
2334 const char *record_id = ctx->argv[2];
2335 const char *column_name = ctx->argv[3];
2336 const struct vsctl_table_class *table;
2337 const struct ovsdb_idl_column *column;
2338 const struct ovsdb_idl_row *row;
2339 const struct ovsdb_type *type;
2340 struct ovsdb_datum old;
2343 table = get_table(table_name);
2344 row = must_get_row(ctx, table, record_id);
2345 die_if_error(get_column(table, column_name, &column));
2347 type = &column->type;
2348 ovsdb_datum_clone(&old, ovsdb_idl_read(row, column), &column->type);
2349 for (i = 4; i < ctx->argc; i++) {
2350 struct ovsdb_type add_type;
2351 struct ovsdb_datum add;
2355 add_type.n_max = UINT_MAX;
2356 die_if_error(ovsdb_datum_from_string(&add, &add_type, ctx->argv[i],
2358 ovsdb_datum_union(&old, &add, type, false);
2359 ovsdb_datum_destroy(&add, type);
2361 if (old.n > type->n_max) {
2362 vsctl_fatal("\"add\" operation would put %u %s in column %s of "
2363 "table %s but the maximum number is %u",
2365 type->value.type == OVSDB_TYPE_VOID ? "values" : "pairs",
2366 column->name, table->class->name, type->n_max);
2368 ovsdb_idl_txn_write(row, column, &old);
2372 cmd_remove(struct vsctl_context *ctx)
2374 const char *table_name = ctx->argv[1];
2375 const char *record_id = ctx->argv[2];
2376 const char *column_name = ctx->argv[3];
2377 const struct vsctl_table_class *table;
2378 const struct ovsdb_idl_column *column;
2379 const struct ovsdb_idl_row *row;
2380 const struct ovsdb_type *type;
2381 struct ovsdb_datum old;
2384 table = get_table(table_name);
2385 row = must_get_row(ctx, table, record_id);
2386 die_if_error(get_column(table, column_name, &column));
2388 type = &column->type;
2389 ovsdb_datum_clone(&old, ovsdb_idl_read(row, column), &column->type);
2390 for (i = 4; i < ctx->argc; i++) {
2391 struct ovsdb_type rm_type;
2392 struct ovsdb_datum rm;
2397 rm_type.n_max = UINT_MAX;
2398 error = ovsdb_datum_from_string(&rm, &rm_type,
2399 ctx->argv[i], ctx->symtab);
2400 if (error && ovsdb_type_is_map(&rm_type)) {
2402 rm_type.value.type = OVSDB_TYPE_VOID;
2403 die_if_error(ovsdb_datum_from_string(&rm, &rm_type,
2404 ctx->argv[i], ctx->symtab));
2406 ovsdb_datum_subtract(&old, type, &rm, &rm_type);
2407 ovsdb_datum_destroy(&rm, &rm_type);
2409 if (old.n < type->n_min) {
2410 vsctl_fatal("\"remove\" operation would put %u %s in column %s of "
2411 "table %s but the minimum number is %u",
2413 type->value.type == OVSDB_TYPE_VOID ? "values" : "pairs",
2414 column->name, table->class->name, type->n_min);
2416 ovsdb_idl_txn_write(row, column, &old);
2420 cmd_clear(struct vsctl_context *ctx)
2422 const char *table_name = ctx->argv[1];
2423 const char *record_id = ctx->argv[2];
2424 const struct vsctl_table_class *table;
2425 const struct ovsdb_idl_row *row;
2428 table = get_table(table_name);
2429 row = must_get_row(ctx, table, record_id);
2430 for (i = 3; i < ctx->argc; i++) {
2431 const struct ovsdb_idl_column *column;
2432 const struct ovsdb_type *type;
2433 struct ovsdb_datum datum;
2435 die_if_error(get_column(table, ctx->argv[i], &column));
2437 type = &column->type;
2438 if (type->n_min > 0) {
2439 vsctl_fatal("\"clear\" operation cannot be applied to column %s "
2440 "of table %s, which is not allowed to be empty",
2441 column->name, table->class->name);
2444 ovsdb_datum_init_empty(&datum);
2445 ovsdb_idl_txn_write(row, column, &datum);
2450 cmd_create(struct vsctl_context *ctx)
2452 const char *id = shash_find_data(&ctx->options, "--id");
2453 const char *table_name = ctx->argv[1];
2454 const struct vsctl_table_class *table;
2455 const struct ovsdb_idl_row *row;
2456 const struct uuid *uuid;
2460 struct ovsdb_symbol *symbol;
2463 vsctl_fatal("row id \"%s\" does not begin with \"@\"", id);
2466 symbol = ovsdb_symbol_table_insert(ctx->symtab, id);
2468 vsctl_fatal("row id \"%s\" may only be used to insert a single "
2471 symbol->used = true;
2473 uuid = &symbol->uuid;
2478 table = get_table(table_name);
2479 row = ovsdb_idl_txn_insert(ctx->txn, table->class, uuid);
2480 for (i = 2; i < ctx->argc; i++) {
2481 set_column(table, row, ctx->argv[i], ctx->symtab);
2483 ds_put_format(&ctx->output, UUID_FMT, UUID_ARGS(&row->uuid));
2486 /* This function may be used as the 'postprocess' function for commands that
2487 * insert new rows into the database. It expects that the command's 'run'
2488 * function prints the UUID reported by ovsdb_idl_txn_insert() as the command's
2489 * sole output. It replaces that output by the row's permanent UUID assigned
2490 * by the database server and appends a new-line.
2492 * Currently we use this only for "create", because the higher-level commands
2493 * are supposed to be independent of the actual structure of the vswitch
2496 post_create(struct vsctl_context *ctx)
2498 const struct uuid *real;
2501 uuid_from_string(&dummy, ds_cstr(&ctx->output));
2502 real = ovsdb_idl_txn_get_insert_uuid(ctx->txn, &dummy);
2504 ds_clear(&ctx->output);
2505 ds_put_format(&ctx->output, UUID_FMT, UUID_ARGS(real));
2507 ds_put_char(&ctx->output, '\n');
2511 cmd_destroy(struct vsctl_context *ctx)
2513 bool must_exist = !shash_find(&ctx->options, "--if-exists");
2514 const char *table_name = ctx->argv[1];
2515 const struct vsctl_table_class *table;
2518 table = get_table(table_name);
2519 for (i = 2; i < ctx->argc; i++) {
2520 const struct ovsdb_idl_row *row;
2522 row = (must_exist ? must_get_row : get_row)(ctx, table, ctx->argv[i]);
2524 ovsdb_idl_txn_delete(row);
2530 is_condition_satified(const struct vsctl_table_class *table,
2531 const struct ovsdb_idl_row *row, const char *arg,
2532 struct ovsdb_symbol_table *symtab)
2534 static const char *operators[] = {
2535 "=", "!=", "<", ">", "<=", ">="
2538 const struct ovsdb_idl_column *column;
2539 const struct ovsdb_datum *have_datum;
2540 char *key_string, *value_string;
2541 const char *operator;
2546 error = parse_column_key_value(arg, table, &column, &key_string,
2547 &operator, operators, ARRAY_SIZE(operators),
2549 die_if_error(error);
2550 if (!value_string) {
2551 vsctl_fatal("%s: missing value", arg);
2554 have_datum = ovsdb_idl_read(row, column);
2556 union ovsdb_atom want_key, want_value;
2558 if (column->type.value.type == OVSDB_TYPE_VOID) {
2559 vsctl_fatal("cannot specify key to check for non-map column %s",
2563 die_if_error(ovsdb_atom_from_string(&want_key, &column->type.key,
2564 key_string, symtab));
2565 die_if_error(ovsdb_atom_from_string(&want_value, &column->type.value,
2566 value_string, symtab));
2568 idx = ovsdb_datum_find_key(have_datum,
2569 &want_key, column->type.key.type);
2570 if (idx != UINT_MAX) {
2571 cmp = ovsdb_atom_compare_3way(&have_datum->values[idx],
2573 column->type.value.type);
2576 ovsdb_atom_destroy(&want_key, column->type.key.type);
2577 ovsdb_atom_destroy(&want_value, column->type.value.type);
2579 struct ovsdb_datum want_datum;
2581 die_if_error(ovsdb_datum_from_string(&want_datum, &column->type,
2582 value_string, symtab));
2584 cmp = ovsdb_datum_compare_3way(have_datum, &want_datum,
2586 ovsdb_datum_destroy(&want_datum, &column->type);
2592 return (idx == UINT_MAX ? false
2593 : !strcmp(operator, "=") ? cmp == 0
2594 : !strcmp(operator, "!=") ? cmp != 0
2595 : !strcmp(operator, "<") ? cmp < 0
2596 : !strcmp(operator, ">") ? cmp > 0
2597 : !strcmp(operator, "<=") ? cmp <= 0
2598 : !strcmp(operator, ">=") ? cmp >= 0
2603 cmd_wait_until(struct vsctl_context *ctx)
2605 const char *table_name = ctx->argv[1];
2606 const char *record_id = ctx->argv[2];
2607 const struct vsctl_table_class *table;
2608 const struct ovsdb_idl_row *row;
2611 table = get_table(table_name);
2613 row = get_row(ctx, table, record_id);
2615 ctx->try_again = true;
2619 for (i = 3; i < ctx->argc; i++) {
2620 if (!is_condition_satified(table, row, ctx->argv[i], ctx->symtab)) {
2621 ctx->try_again = true;
2627 static struct json *
2628 where_uuid_equals(const struct uuid *uuid)
2631 json_array_create_1(
2632 json_array_create_3(
2633 json_string_create("_uuid"),
2634 json_string_create("=="),
2635 json_array_create_2(
2636 json_string_create("uuid"),
2637 json_string_create_nocopy(
2638 xasprintf(UUID_FMT, UUID_ARGS(uuid))))));
2642 vsctl_context_init(struct vsctl_context *ctx, struct vsctl_command *command,
2643 struct ovsdb_idl *idl, struct ovsdb_idl_txn *txn,
2644 const struct ovsrec_open_vswitch *ovs,
2645 struct ovsdb_symbol_table *symtab)
2647 ctx->argc = command->argc;
2648 ctx->argv = command->argv;
2649 ctx->options = command->options;
2651 ds_swap(&ctx->output, &command->output);
2655 ctx->symtab = symtab;
2657 ctx->try_again = false;
2661 vsctl_context_done(struct vsctl_context *ctx, struct vsctl_command *command)
2663 ds_swap(&ctx->output, &command->output);
2667 do_vsctl(const char *args, struct vsctl_command *commands, size_t n_commands,
2668 struct ovsdb_idl *idl)
2670 struct ovsdb_idl_txn *txn;
2671 const struct ovsrec_open_vswitch *ovs;
2672 enum ovsdb_idl_txn_status status;
2673 struct ovsdb_symbol_table *symtab;
2675 struct vsctl_command *c;
2676 int64_t next_cfg = 0;
2679 txn = the_idl_txn = ovsdb_idl_txn_create(idl);
2681 ovsdb_idl_txn_set_dry_run(txn);
2684 ovsdb_idl_txn_add_comment(txn, "ovs-vsctl: %s", args);
2686 ovs = ovsrec_open_vswitch_first(idl);
2688 /* XXX add verification that table is empty */
2689 ovs = ovsrec_open_vswitch_insert(txn);
2692 if (wait_for_reload) {
2693 struct json *where = where_uuid_equals(&ovs->header_.uuid);
2694 ovsdb_idl_txn_increment(txn, "Open_vSwitch", "next_cfg", where);
2695 json_destroy(where);
2698 symtab = ovsdb_symbol_table_create();
2699 for (c = commands; c < &commands[n_commands]; c++) {
2700 ds_init(&c->output);
2702 for (c = commands; c < &commands[n_commands]; c++) {
2703 struct vsctl_context ctx;
2705 vsctl_context_init(&ctx, c, idl, txn, ovs, symtab);
2706 (c->syntax->run)(&ctx);
2707 vsctl_context_done(&ctx, c);
2709 if (ctx.try_again) {
2714 status = ovsdb_idl_txn_commit_block(txn);
2715 if (wait_for_reload && status == TXN_SUCCESS) {
2716 next_cfg = ovsdb_idl_txn_get_increment_new_value(txn);
2718 if (status == TXN_UNCHANGED || status == TXN_SUCCESS) {
2719 for (c = commands; c < &commands[n_commands]; c++) {
2720 if (c->syntax->postprocess) {
2721 struct vsctl_context ctx;
2723 vsctl_context_init(&ctx, c, idl, txn, ovs, symtab);
2724 (c->syntax->postprocess)(&ctx);
2725 vsctl_context_done(&ctx, c);
2729 error = xstrdup(ovsdb_idl_txn_get_error(txn));
2730 ovsdb_idl_txn_destroy(txn);
2733 unused = ovsdb_symbol_table_find_unused(symtab);
2735 vsctl_fatal("row id \"%s\" is referenced but never created (e.g. "
2736 "with \"-- --id=%s create ...\")", unused, unused);
2740 case TXN_INCOMPLETE:
2744 /* Should not happen--we never call ovsdb_idl_txn_abort(). */
2745 vsctl_fatal("transaction aborted");
2755 vsctl_fatal("transaction error: %s", error);
2762 ovsdb_symbol_table_destroy(symtab);
2764 for (c = commands; c < &commands[n_commands]; c++) {
2765 struct ds *ds = &c->output;
2766 struct shash_node *node;
2772 for (j = 0; j < ds->length; j++) {
2773 int c = ds->string[j];
2776 fputs("\\n", stdout);
2780 fputs("\\\\", stdout);
2789 fputs(ds_cstr(ds), stdout);
2791 ds_destroy(&c->output);
2793 SHASH_FOR_EACH (node, &c->options) {
2796 shash_destroy(&c->options);
2800 if (wait_for_reload && status != TXN_UNCHANGED) {
2802 const struct ovsrec_open_vswitch *ovs;
2805 OVSREC_OPEN_VSWITCH_FOR_EACH (ovs, idl) {
2806 if (ovs->cur_cfg >= next_cfg) {
2810 ovsdb_idl_wait(idl);
2815 ovsdb_idl_destroy(idl);
2820 /* Our transaction needs to be rerun, or a prerequisite was not met. Free
2821 * resources and return so that the caller can try again. */
2822 ovsdb_idl_txn_abort(txn);
2823 ovsdb_idl_txn_destroy(txn);
2824 ovsdb_symbol_table_destroy(symtab);
2825 for (c = commands; c < &commands[n_commands]; c++) {
2826 ds_destroy(&c->output);
2831 static const struct vsctl_command_syntax all_commands[] = {
2832 /* Open vSwitch commands. */
2833 {"init", 0, 0, cmd_init, NULL, ""},
2835 /* Bridge commands. */
2836 {"add-br", 1, 3, cmd_add_br, NULL, "--may-exist"},
2837 {"del-br", 1, 1, cmd_del_br, NULL, "--if-exists"},
2838 {"list-br", 0, 0, cmd_list_br, NULL, ""},
2839 {"br-exists", 1, 1, cmd_br_exists, NULL, ""},
2840 {"br-to-vlan", 1, 1, cmd_br_to_vlan, NULL, ""},
2841 {"br-to-parent", 1, 1, cmd_br_to_parent, NULL, ""},
2842 {"br-set-external-id", 2, 3, cmd_br_set_external_id, NULL, ""},
2843 {"br-get-external-id", 1, 2, cmd_br_get_external_id, NULL, ""},
2845 /* Port commands. */
2846 {"list-ports", 1, 1, cmd_list_ports, NULL, ""},
2847 {"add-port", 2, INT_MAX, cmd_add_port, NULL, "--may-exist"},
2848 {"add-bond", 4, INT_MAX, cmd_add_bond, NULL, "--may-exist,--fake-iface"},
2849 {"del-port", 1, 2, cmd_del_port, NULL, "--if-exists,--with-iface"},
2850 {"port-to-br", 1, 1, cmd_port_to_br, NULL, ""},
2852 /* Interface commands. */
2853 {"list-ifaces", 1, 1, cmd_list_ifaces, NULL, ""},
2854 {"iface-to-br", 1, 1, cmd_iface_to_br, NULL, ""},
2856 /* Controller commands. */
2857 {"get-controller", 1, 1, cmd_get_controller, NULL, ""},
2858 {"del-controller", 1, 1, cmd_del_controller, NULL, ""},
2859 {"set-controller", 1, INT_MAX, cmd_set_controller, NULL, ""},
2860 {"get-fail-mode", 1, 1, cmd_get_fail_mode, NULL, ""},
2861 {"del-fail-mode", 1, 1, cmd_del_fail_mode, NULL, ""},
2862 {"set-fail-mode", 2, 2, cmd_set_fail_mode, NULL, ""},
2865 {"get-ssl", 0, 0, cmd_get_ssl, NULL, ""},
2866 {"del-ssl", 0, 0, cmd_del_ssl, NULL, ""},
2867 {"set-ssl", 3, 3, cmd_set_ssl, NULL, "--bootstrap"},
2869 /* Switch commands. */
2870 {"emer-reset", 0, 0, cmd_emer_reset, NULL, ""},
2872 /* Parameter commands. */
2873 {"get", 3, INT_MAX, cmd_get, NULL, "--if-exists"},
2874 {"list", 1, INT_MAX, cmd_list, NULL, ""},
2875 {"set", 3, INT_MAX, cmd_set, NULL, ""},
2876 {"add", 4, INT_MAX, cmd_add, NULL, ""},
2877 {"remove", 4, INT_MAX, cmd_remove, NULL, ""},
2878 {"clear", 3, INT_MAX, cmd_clear, NULL, ""},
2879 {"create", 2, INT_MAX, cmd_create, post_create, "--id="},
2880 {"destroy", 1, INT_MAX, cmd_destroy, NULL, "--if-exists"},
2881 {"wait-until", 2, INT_MAX, cmd_wait_until, NULL, ""},
2883 {NULL, 0, 0, NULL, NULL, NULL},