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'. */
88 static int timeout = 5;
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},
190 c = getopt_long(argc, argv, "+v::hVt:", long_options, NULL);
205 vlog_set_levels(&VLM_vsctl, VLF_SYSLOG, VLL_WARN);
209 wait_for_reload = false;
220 OVS_PRINT_VERSION(0, 0);
224 timeout = strtoul(optarg, NULL, 10);
226 vsctl_fatal("value %s on -t or --timeout is invalid",
234 STREAM_SSL_OPTION_HANDLERS
236 case OPT_PEER_CA_CERT:
237 stream_ssl_set_peer_ca_cert_file(optarg);
254 static struct vsctl_command *
255 parse_commands(int argc, char *argv[], size_t *n_commandsp)
257 struct vsctl_command *commands;
258 size_t n_commands, allocated_commands;
262 n_commands = allocated_commands = 0;
264 for (start = i = 0; i <= argc; i++) {
265 if (i == argc || !strcmp(argv[i], "--")) {
267 if (n_commands >= allocated_commands) {
268 struct vsctl_command *c;
270 commands = x2nrealloc(commands, &allocated_commands,
272 for (c = commands; c < &commands[n_commands]; c++) {
273 shash_moved(&c->options);
276 parse_command(i - start, &argv[start],
277 &commands[n_commands++]);
283 vsctl_fatal("missing command name (use --help for help)");
285 *n_commandsp = n_commands;
290 parse_command(int argc, char *argv[], struct vsctl_command *command)
292 const struct vsctl_command_syntax *p;
295 shash_init(&command->options);
296 for (i = 0; i < argc; i++) {
297 const char *option = argv[i];
301 if (option[0] != '-') {
305 equals = strchr(option, '=');
307 key = xmemdup0(option, equals - option);
308 value = xstrdup(equals + 1);
310 key = xstrdup(option);
314 if (shash_find(&command->options, key)) {
315 vsctl_fatal("'%s' option specified multiple times", argv[i]);
317 shash_add_nocopy(&command->options, key, value);
320 vsctl_fatal("missing command name");
323 for (p = all_commands; p->name; p++) {
324 if (!strcmp(p->name, argv[i])) {
325 struct shash_node *node;
328 SHASH_FOR_EACH (node, &command->options) {
329 const char *s = strstr(p->options, node->name);
330 int end = s ? s[strlen(node->name)] : EOF;
332 if (end != '=' && end != ',' && end != ' ' && end != '\0') {
333 vsctl_fatal("'%s' command has no '%s' option",
334 argv[i], node->name);
336 if ((end == '=') != (node->data != NULL)) {
338 vsctl_fatal("missing argument to '%s' option on '%s' "
339 "command", node->name, argv[i]);
341 vsctl_fatal("'%s' option on '%s' does not accept an "
342 "argument", node->name, argv[i]);
347 n_arg = argc - i - 1;
348 if (n_arg < p->min_args) {
349 vsctl_fatal("'%s' command requires at least %d arguments",
350 p->name, p->min_args);
351 } else if (n_arg > p->max_args) {
354 for (j = i + 1; j < argc; j++) {
355 if (argv[j][0] == '-') {
356 vsctl_fatal("'%s' command takes at most %d arguments "
357 "(note that options must precede command "
358 "names and follow a \"--\" argument)",
359 p->name, p->max_args);
363 vsctl_fatal("'%s' command takes at most %d arguments",
364 p->name, p->max_args);
367 command->argc = n_arg + 1;
368 command->argv = &argv[i];
374 vsctl_fatal("unknown command '%s'; use --help for help", argv[i]);
378 vsctl_fatal(const char *format, ...)
383 va_start(args, format);
384 message = xvasprintf(format, args);
387 vlog_set_levels(&VLM_vsctl, VLF_CONSOLE, VLL_EMER);
388 VLOG_ERR("%s", message);
389 ovs_error(0, "%s", message);
390 vsctl_exit(EXIT_FAILURE);
393 /* Frees the current transaction and the underlying IDL and then calls
396 * Freeing the transaction and the IDL is not strictly necessary, but it makes
397 * for a clean memory leak report from valgrind in the normal case. That makes
398 * it easier to notice real memory leaks. */
400 vsctl_exit(int status)
403 ovsdb_idl_txn_abort(the_idl_txn);
404 ovsdb_idl_txn_destroy(the_idl_txn);
406 ovsdb_idl_destroy(the_idl);
414 %s: ovs-vswitchd management utility\n\
415 usage: %s [OPTIONS] COMMAND [ARG...]\n\
418 add-br BRIDGE create a new bridge named BRIDGE\n\
419 add-br BRIDGE PARENT VLAN create new fake BRIDGE in PARENT on VLAN\n\
420 del-br BRIDGE delete BRIDGE and all of its ports\n\
421 list-br print the names of all the bridges\n\
422 br-exists BRIDGE test whether BRIDGE exists\n\
423 br-to-vlan BRIDGE print the VLAN which BRIDGE is on\n\
424 br-to-parent BRIDGE print the parent of BRIDGE\n\
425 br-set-external-id BRIDGE KEY VALUE set KEY on BRIDGE to VALUE\n\
426 br-set-external-id BRIDGE KEY unset KEY on BRIDGE\n\
427 br-get-external-id BRIDGE KEY print value of KEY on BRIDGE\n\
428 br-get-external-id BRIDGE list key-value pairs on BRIDGE\n\
431 list-ports BRIDGE print the names of all the ports on BRIDGE\n\
432 add-port BRIDGE PORT add network device PORT to BRIDGE\n\
433 add-bond BRIDGE PORT IFACE... add bonded port PORT in BRIDGE from IFACES\n\
434 del-port [BRIDGE] PORT delete PORT (which may be bonded) from BRIDGE\n\
435 port-to-br PORT print name of bridge that contains PORT\n\
436 A bond is considered to be a single port.\n\
438 Interface commands (a bond consists of multiple interfaces):\n\
439 list-ifaces BRIDGE print the names of all interfaces on BRIDGE\n\
440 iface-to-br IFACE print name of bridge that contains IFACE\n\
442 Controller commands:\n\
443 get-controller BRIDGE print the controller for BRIDGE\n\
444 del-controller BRIDGE delete the controller for BRIDGE\n\
445 set-controller BRIDGE TARGET set the controller for BRIDGE to TARGET\n\
446 get-fail-mode BRIDGE print the fail-mode for BRIDGE\n\
447 del-fail-mode BRIDGE delete the fail-mode for BRIDGE\n\
448 set-fail-mode BRIDGE MODE set the fail-mode for BRIDGE to MODE\n\
451 get-ssl print the SSL configuration\n\
452 del-ssl delete the SSL configuration\n\
453 set-ssl PRIV-KEY CERT CA-CERT set the SSL configuration\n\
456 emer-reset reset switch to known good state\n\
458 Database commands:\n\
459 list TBL [REC] list RECord (or all records) in TBL\n\
460 get TBL REC COL[:KEY] print values of COLumns in RECord in TBL\n\
461 set TBL REC COL[:KEY]=VALUE set COLumn values in RECord in TBL\n\
462 add TBL REC COL [KEY=]VALUE add (KEY=)VALUE to COLumn in RECord in TBL\n\
463 remove TBL REC COL [KEY=]VALUE remove (KEY=)VALUE from COLumn\n\
464 clear TBL REC COL clear values from COLumn in RECord in TBL\n\
465 create TBL COL[:KEY]=VALUE create and initialize new record\n\
466 destroy TBL REC delete RECord from TBL\n\
467 wait-until TBL REC [COL[:KEY]=VALUE] wait until condition is true\n\
468 Potentially unsafe database commands require --force option.\n\
471 --db=DATABASE connect to DATABASE\n\
473 --oneline print exactly one line of output per command\n",
474 program_name, program_name, default_db());
478 -h, --help display this help message\n\
479 -V, --version display version information\n");
488 def = xasprintf("unix:%s/db.sock", ovs_rundir);
493 struct vsctl_context {
497 struct shash options;
499 /* Modifiable state. */
501 struct ovsdb_idl *idl;
502 struct ovsdb_idl_txn *txn;
503 struct ovsdb_symbol_table *symtab;
504 const struct ovsrec_open_vswitch *ovs;
506 /* A command may set this member to true if some prerequisite is not met
507 * and the caller should wait for something to change and then retry. */
511 struct vsctl_bridge {
512 struct ovsrec_bridge *br_cfg;
514 struct ovsrec_controller **ctrl;
516 struct vsctl_bridge *parent;
521 struct ovsrec_port *port_cfg;
522 struct vsctl_bridge *bridge;
526 struct ovsrec_interface *iface_cfg;
527 struct vsctl_port *port;
531 struct shash bridges;
537 vsctl_context_to_string(const struct vsctl_context *ctx)
539 const struct shash_node *node;
545 SHASH_FOR_EACH (node, &ctx->options) {
546 svec_add(&words, node->name);
548 for (i = 0; i < ctx->argc; i++) {
549 svec_add(&words, ctx->argv[i]);
551 svec_terminate(&words);
553 s = process_escape_args(words.names);
555 svec_destroy(&words);
560 static struct vsctl_bridge *
561 add_bridge(struct vsctl_info *b,
562 struct ovsrec_bridge *br_cfg, const char *name,
563 struct vsctl_bridge *parent, int vlan)
565 struct vsctl_bridge *br = xmalloc(sizeof *br);
567 br->name = xstrdup(name);
571 br->ctrl = parent->br_cfg->controller;
572 br->n_ctrl = parent->br_cfg->n_controller;
574 br->ctrl = br_cfg->controller;
575 br->n_ctrl = br_cfg->n_controller;
577 shash_add(&b->bridges, br->name, br);
582 port_is_fake_bridge(const struct ovsrec_port *port_cfg)
584 return (port_cfg->fake_bridge
586 && *port_cfg->tag >= 1 && *port_cfg->tag <= 4095);
589 static struct vsctl_bridge *
590 find_vlan_bridge(struct vsctl_info *info,
591 struct vsctl_bridge *parent, int vlan)
593 struct shash_node *node;
595 SHASH_FOR_EACH (node, &info->bridges) {
596 struct vsctl_bridge *br = node->data;
597 if (br->parent == parent && br->vlan == vlan) {
606 free_info(struct vsctl_info *info)
608 struct shash_node *node;
610 SHASH_FOR_EACH (node, &info->bridges) {
611 struct vsctl_bridge *bridge = node->data;
615 shash_destroy(&info->bridges);
617 shash_destroy_free_data(&info->ports);
618 shash_destroy_free_data(&info->ifaces);
622 get_info(const struct ovsrec_open_vswitch *ovs, struct vsctl_info *info)
624 struct shash bridges, ports;
627 shash_init(&info->bridges);
628 shash_init(&info->ports);
629 shash_init(&info->ifaces);
631 shash_init(&bridges);
633 for (i = 0; i < ovs->n_bridges; i++) {
634 struct ovsrec_bridge *br_cfg = ovs->bridges[i];
635 struct vsctl_bridge *br;
638 if (!shash_add_once(&bridges, br_cfg->name, NULL)) {
639 VLOG_WARN("%s: database contains duplicate bridge name",
643 br = add_bridge(info, br_cfg, br_cfg->name, NULL, 0);
648 for (j = 0; j < br_cfg->n_ports; j++) {
649 struct ovsrec_port *port_cfg = br_cfg->ports[j];
651 if (!shash_add_once(&ports, port_cfg->name, NULL)) {
652 VLOG_WARN("%s: database contains duplicate port name",
657 if (port_is_fake_bridge(port_cfg)
658 && shash_add_once(&bridges, port_cfg->name, NULL)) {
659 add_bridge(info, NULL, port_cfg->name, br, *port_cfg->tag);
663 shash_destroy(&bridges);
664 shash_destroy(&ports);
666 shash_init(&bridges);
668 for (i = 0; i < ovs->n_bridges; i++) {
669 struct ovsrec_bridge *br_cfg = ovs->bridges[i];
670 struct vsctl_bridge *br;
673 if (!shash_add_once(&bridges, br_cfg->name, NULL)) {
676 br = shash_find_data(&info->bridges, br_cfg->name);
677 for (j = 0; j < br_cfg->n_ports; j++) {
678 struct ovsrec_port *port_cfg = br_cfg->ports[j];
679 struct vsctl_port *port;
682 if (!shash_add_once(&ports, port_cfg->name, NULL)) {
686 if (port_is_fake_bridge(port_cfg)
687 && !shash_add_once(&bridges, port_cfg->name, NULL)) {
691 port = xmalloc(sizeof *port);
692 port->port_cfg = port_cfg;
694 && *port_cfg->tag >= 1 && *port_cfg->tag <= 4095) {
695 port->bridge = find_vlan_bridge(info, br, *port_cfg->tag);
702 shash_add(&info->ports, port_cfg->name, port);
704 for (k = 0; k < port_cfg->n_interfaces; k++) {
705 struct ovsrec_interface *iface_cfg = port_cfg->interfaces[k];
706 struct vsctl_iface *iface;
708 if (shash_find(&info->ifaces, iface_cfg->name)) {
709 VLOG_WARN("%s: database contains duplicate interface name",
714 iface = xmalloc(sizeof *iface);
715 iface->iface_cfg = iface_cfg;
717 shash_add(&info->ifaces, iface_cfg->name, iface);
721 shash_destroy(&bridges);
722 shash_destroy(&ports);
726 check_conflicts(struct vsctl_info *info, const char *name,
729 struct vsctl_iface *iface;
730 struct vsctl_port *port;
732 if (shash_find(&info->bridges, name)) {
733 vsctl_fatal("%s because a bridge named %s already exists",
737 port = shash_find_data(&info->ports, name);
739 vsctl_fatal("%s because a port named %s already exists on "
740 "bridge %s", msg, name, port->bridge->name);
743 iface = shash_find_data(&info->ifaces, name);
745 vsctl_fatal("%s because an interface named %s already exists "
746 "on bridge %s", msg, name, iface->port->bridge->name);
752 static struct vsctl_bridge *
753 find_bridge(struct vsctl_info *info, const char *name, bool must_exist)
755 struct vsctl_bridge *br = shash_find_data(&info->bridges, name);
756 if (must_exist && !br) {
757 vsctl_fatal("no bridge named %s", name);
762 static struct vsctl_bridge *
763 find_real_bridge(struct vsctl_info *info, const char *name, bool must_exist)
765 struct vsctl_bridge *br = find_bridge(info, name, must_exist);
766 if (br && br->parent) {
767 vsctl_fatal("%s is a fake bridge", name);
772 static struct vsctl_port *
773 find_port(struct vsctl_info *info, const char *name, bool must_exist)
775 struct vsctl_port *port = shash_find_data(&info->ports, name);
776 if (port && !strcmp(name, port->bridge->name)) {
779 if (must_exist && !port) {
780 vsctl_fatal("no port named %s", name);
785 static struct vsctl_iface *
786 find_iface(struct vsctl_info *info, const char *name, bool must_exist)
788 struct vsctl_iface *iface = shash_find_data(&info->ifaces, name);
789 if (iface && !strcmp(name, iface->port->bridge->name)) {
792 if (must_exist && !iface) {
793 vsctl_fatal("no interface named %s", name);
799 bridge_insert_port(struct ovsrec_bridge *br, struct ovsrec_port *port)
801 struct ovsrec_port **ports;
804 ports = xmalloc(sizeof *br->ports * (br->n_ports + 1));
805 for (i = 0; i < br->n_ports; i++) {
806 ports[i] = br->ports[i];
808 ports[br->n_ports] = port;
809 ovsrec_bridge_set_ports(br, ports, br->n_ports + 1);
814 bridge_delete_port(struct ovsrec_bridge *br, struct ovsrec_port *port)
816 struct ovsrec_port **ports;
819 ports = xmalloc(sizeof *br->ports * br->n_ports);
820 for (i = n = 0; i < br->n_ports; i++) {
821 if (br->ports[i] != port) {
822 ports[n++] = br->ports[i];
825 ovsrec_bridge_set_ports(br, ports, n);
830 ovs_insert_bridge(const struct ovsrec_open_vswitch *ovs,
831 struct ovsrec_bridge *bridge)
833 struct ovsrec_bridge **bridges;
836 bridges = xmalloc(sizeof *ovs->bridges * (ovs->n_bridges + 1));
837 for (i = 0; i < ovs->n_bridges; i++) {
838 bridges[i] = ovs->bridges[i];
840 bridges[ovs->n_bridges] = bridge;
841 ovsrec_open_vswitch_set_bridges(ovs, bridges, ovs->n_bridges + 1);
846 ovs_delete_bridge(const struct ovsrec_open_vswitch *ovs,
847 struct ovsrec_bridge *bridge)
849 struct ovsrec_bridge **bridges;
852 bridges = xmalloc(sizeof *ovs->bridges * ovs->n_bridges);
853 for (i = n = 0; i < ovs->n_bridges; i++) {
854 if (ovs->bridges[i] != bridge) {
855 bridges[n++] = ovs->bridges[i];
858 ovsrec_open_vswitch_set_bridges(ovs, bridges, n);
863 cmd_init(struct vsctl_context *ctx OVS_UNUSED)
868 cmd_emer_reset(struct vsctl_context *ctx)
870 const struct ovsdb_idl *idl = ctx->idl;
871 const struct ovsrec_bridge *br;
872 const struct ovsrec_port *port;
873 const struct ovsrec_interface *iface;
874 const struct ovsrec_mirror *mirror, *next_mirror;
875 const struct ovsrec_controller *ctrl, *next_ctrl;
876 const struct ovsrec_netflow *nf, *next_nf;
877 const struct ovsrec_ssl *ssl, *next_ssl;
878 const struct ovsrec_sflow *sflow, *next_sflow;
881 /* Reset the Open_vSwitch table. */
882 ovsrec_open_vswitch_set_managers(ctx->ovs, NULL, 0);
883 ovsrec_open_vswitch_set_ssl(ctx->ovs, NULL);
885 OVSREC_BRIDGE_FOR_EACH (br, idl) {
887 char *hw_key = "hwaddr";
890 ovsrec_bridge_set_controller(br, NULL, 0);
891 ovsrec_bridge_set_mirrors(br, NULL, 0);
892 ovsrec_bridge_set_netflow(br, NULL);
893 ovsrec_bridge_set_sflow(br, NULL);
894 ovsrec_bridge_set_flood_vlans(br, NULL, 0);
896 /* We only want to save the "hwaddr" key from other_config. */
897 for (i=0; i < br->n_other_config; i++) {
898 if (!strcmp(br->key_other_config[i], hw_key)) {
899 hw_val = br->value_other_config[i];
904 char *val = xstrdup(hw_val);
905 ovsrec_bridge_set_other_config(br, &hw_key, &val, 1);
908 ovsrec_bridge_set_other_config(br, NULL, NULL, 0);
912 OVSREC_PORT_FOR_EACH (port, idl) {
913 ovsrec_port_set_other_config(port, NULL, NULL, 0);
916 OVSREC_INTERFACE_FOR_EACH (iface, idl) {
917 /* xxx What do we do about gre/patch devices created by mgr? */
919 ovsrec_interface_set_ingress_policing_rate(iface, 0);
920 ovsrec_interface_set_ingress_policing_burst(iface, 0);
923 OVSREC_MIRROR_FOR_EACH_SAFE (mirror, next_mirror, idl) {
924 ovsrec_mirror_delete(mirror);
927 OVSREC_CONTROLLER_FOR_EACH_SAFE (ctrl, next_ctrl, idl) {
928 ovsrec_controller_delete(ctrl);
931 OVSREC_NETFLOW_FOR_EACH_SAFE (nf, next_nf, idl) {
932 ovsrec_netflow_delete(nf);
935 OVSREC_SSL_FOR_EACH_SAFE (ssl, next_ssl, idl) {
936 ovsrec_ssl_delete(ssl);
939 OVSREC_SFLOW_FOR_EACH_SAFE (sflow, next_sflow, idl) {
940 ovsrec_sflow_delete(sflow);
945 cmd_add_br(struct vsctl_context *ctx)
947 bool may_exist = shash_find(&ctx->options, "--may-exist") != 0;
948 const char *br_name, *parent_name;
949 struct vsctl_info info;
952 br_name = ctx->argv[1];
953 if (ctx->argc == 2) {
956 } else if (ctx->argc == 4) {
957 parent_name = ctx->argv[2];
958 vlan = atoi(ctx->argv[3]);
959 if (vlan < 1 || vlan > 4095) {
960 vsctl_fatal("%s: vlan must be between 1 and 4095", ctx->argv[0]);
963 vsctl_fatal("'%s' command takes exactly 1 or 3 arguments",
967 get_info(ctx->ovs, &info);
969 struct vsctl_bridge *br;
971 br = find_bridge(&info, br_name, false);
975 vsctl_fatal("\"--may-exist add-br %s\" but %s is "
976 "a VLAN bridge for VLAN %d",
977 br_name, br_name, br->vlan);
981 vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
982 "is not a VLAN bridge",
983 br_name, parent_name, vlan, br_name);
984 } else if (strcmp(br->parent->name, parent_name)) {
985 vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
986 "has the wrong parent %s",
987 br_name, parent_name, vlan,
988 br_name, br->parent->name);
989 } else if (br->vlan != vlan) {
990 vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
991 "is a VLAN bridge for the wrong VLAN %d",
992 br_name, parent_name, vlan, br_name, br->vlan);
998 check_conflicts(&info, br_name,
999 xasprintf("cannot create a bridge named %s", br_name));
1002 struct ovsrec_port *port;
1003 struct ovsrec_interface *iface;
1004 struct ovsrec_bridge *br;
1006 iface = ovsrec_interface_insert(ctx->txn);
1007 ovsrec_interface_set_name(iface, br_name);
1009 port = ovsrec_port_insert(ctx->txn);
1010 ovsrec_port_set_name(port, br_name);
1011 ovsrec_port_set_interfaces(port, &iface, 1);
1013 br = ovsrec_bridge_insert(ctx->txn);
1014 ovsrec_bridge_set_name(br, br_name);
1015 ovsrec_bridge_set_ports(br, &port, 1);
1017 ovs_insert_bridge(ctx->ovs, br);
1019 struct vsctl_bridge *parent;
1020 struct ovsrec_port *port;
1021 struct ovsrec_interface *iface;
1022 struct ovsrec_bridge *br;
1025 parent = find_bridge(&info, parent_name, false);
1026 if (parent && parent->vlan) {
1027 vsctl_fatal("cannot create bridge with fake bridge as parent");
1030 vsctl_fatal("parent bridge %s does not exist", parent_name);
1032 br = parent->br_cfg;
1034 iface = ovsrec_interface_insert(ctx->txn);
1035 ovsrec_interface_set_name(iface, br_name);
1036 ovsrec_interface_set_type(iface, "internal");
1038 port = ovsrec_port_insert(ctx->txn);
1039 ovsrec_port_set_name(port, br_name);
1040 ovsrec_port_set_interfaces(port, &iface, 1);
1041 ovsrec_port_set_fake_bridge(port, true);
1042 ovsrec_port_set_tag(port, &tag, 1);
1044 bridge_insert_port(br, port);
1051 del_port(struct vsctl_info *info, struct vsctl_port *port)
1053 struct shash_node *node;
1055 SHASH_FOR_EACH (node, &info->ifaces) {
1056 struct vsctl_iface *iface = node->data;
1057 if (iface->port == port) {
1058 ovsrec_interface_delete(iface->iface_cfg);
1061 ovsrec_port_delete(port->port_cfg);
1063 bridge_delete_port((port->bridge->parent
1064 ? port->bridge->parent->br_cfg
1065 : port->bridge->br_cfg), port->port_cfg);
1069 cmd_del_br(struct vsctl_context *ctx)
1071 bool must_exist = !shash_find(&ctx->options, "--if-exists");
1072 struct vsctl_bridge *bridge;
1073 struct vsctl_info info;
1075 get_info(ctx->ovs, &info);
1076 bridge = find_bridge(&info, ctx->argv[1], must_exist);
1078 struct shash_node *node;
1080 SHASH_FOR_EACH (node, &info.ports) {
1081 struct vsctl_port *port = node->data;
1082 if (port->bridge == bridge || port->bridge->parent == bridge
1083 || !strcmp(port->port_cfg->name, bridge->name)) {
1084 del_port(&info, port);
1087 if (bridge->br_cfg) {
1088 ovsrec_bridge_delete(bridge->br_cfg);
1089 ovs_delete_bridge(ctx->ovs, bridge->br_cfg);
1096 output_sorted(struct svec *svec, struct ds *output)
1102 SVEC_FOR_EACH (i, name, svec) {
1103 ds_put_format(output, "%s\n", name);
1108 cmd_list_br(struct vsctl_context *ctx)
1110 struct shash_node *node;
1111 struct vsctl_info info;
1112 struct svec bridges;
1114 get_info(ctx->ovs, &info);
1116 svec_init(&bridges);
1117 SHASH_FOR_EACH (node, &info.bridges) {
1118 struct vsctl_bridge *br = node->data;
1119 svec_add(&bridges, br->name);
1121 output_sorted(&bridges, &ctx->output);
1122 svec_destroy(&bridges);
1128 cmd_br_exists(struct vsctl_context *ctx)
1130 struct vsctl_info info;
1132 get_info(ctx->ovs, &info);
1133 if (!find_bridge(&info, ctx->argv[1], false)) {
1139 /* Returns true if 'b_prefix' (of length 'b_prefix_len') concatenated with 'b'
1140 * equals 'a', false otherwise. */
1142 key_matches(const char *a,
1143 const char *b_prefix, size_t b_prefix_len, const char *b)
1145 return !strncmp(a, b_prefix, b_prefix_len) && !strcmp(a + b_prefix_len, b);
1149 set_external_id(char **old_keys, char **old_values, size_t old_n,
1150 char *key, char *value,
1151 char ***new_keysp, char ***new_valuesp, size_t *new_np)
1158 new_keys = xmalloc(sizeof *new_keys * (old_n + 1));
1159 new_values = xmalloc(sizeof *new_values * (old_n + 1));
1161 for (i = 0; i < old_n; i++) {
1162 if (strcmp(key, old_keys[i])) {
1163 new_keys[new_n] = old_keys[i];
1164 new_values[new_n] = old_values[i];
1169 new_keys[new_n] = key;
1170 new_values[new_n] = value;
1173 *new_keysp = new_keys;
1174 *new_valuesp = new_values;
1179 cmd_br_set_external_id(struct vsctl_context *ctx)
1181 struct vsctl_info info;
1182 struct vsctl_bridge *bridge;
1183 char **keys, **values;
1186 get_info(ctx->ovs, &info);
1187 bridge = find_bridge(&info, ctx->argv[1], true);
1188 if (bridge->br_cfg) {
1189 set_external_id(bridge->br_cfg->key_external_ids,
1190 bridge->br_cfg->value_external_ids,
1191 bridge->br_cfg->n_external_ids,
1192 ctx->argv[2], ctx->argc >= 4 ? ctx->argv[3] : NULL,
1193 &keys, &values, &n);
1194 ovsrec_bridge_set_external_ids(bridge->br_cfg, keys, values, n);
1196 char *key = xasprintf("fake-bridge-%s", ctx->argv[2]);
1197 struct vsctl_port *port = shash_find_data(&info.ports, ctx->argv[1]);
1198 set_external_id(port->port_cfg->key_external_ids,
1199 port->port_cfg->value_external_ids,
1200 port->port_cfg->n_external_ids,
1201 key, ctx->argc >= 4 ? ctx->argv[3] : NULL,
1202 &keys, &values, &n);
1203 ovsrec_port_set_external_ids(port->port_cfg, keys, values, n);
1213 get_external_id(char **keys, char **values, size_t n,
1214 const char *prefix, const char *key,
1217 size_t prefix_len = strlen(prefix);
1222 for (i = 0; i < n; i++) {
1223 if (!key && !strncmp(keys[i], prefix, prefix_len)) {
1224 svec_add_nocopy(&svec, xasprintf("%s=%s",
1225 keys[i] + prefix_len, values[i]));
1226 } else if (key_matches(keys[i], prefix, prefix_len, key)) {
1227 svec_add(&svec, values[i]);
1231 output_sorted(&svec, output);
1232 svec_destroy(&svec);
1236 cmd_br_get_external_id(struct vsctl_context *ctx)
1238 struct vsctl_info info;
1239 struct vsctl_bridge *bridge;
1241 get_info(ctx->ovs, &info);
1242 bridge = find_bridge(&info, ctx->argv[1], true);
1243 if (bridge->br_cfg) {
1244 get_external_id(bridge->br_cfg->key_external_ids,
1245 bridge->br_cfg->value_external_ids,
1246 bridge->br_cfg->n_external_ids,
1247 "", ctx->argc >= 3 ? ctx->argv[2] : NULL,
1250 struct vsctl_port *port = shash_find_data(&info.ports, ctx->argv[1]);
1251 get_external_id(port->port_cfg->key_external_ids,
1252 port->port_cfg->value_external_ids,
1253 port->port_cfg->n_external_ids,
1254 "fake-bridge-", ctx->argc >= 3 ? ctx->argv[2] : NULL, &ctx->output);
1261 cmd_list_ports(struct vsctl_context *ctx)
1263 struct vsctl_bridge *br;
1264 struct shash_node *node;
1265 struct vsctl_info info;
1268 get_info(ctx->ovs, &info);
1269 br = find_bridge(&info, ctx->argv[1], true);
1272 SHASH_FOR_EACH (node, &info.ports) {
1273 struct vsctl_port *port = node->data;
1275 if (strcmp(port->port_cfg->name, br->name) && br == port->bridge) {
1276 svec_add(&ports, port->port_cfg->name);
1279 output_sorted(&ports, &ctx->output);
1280 svec_destroy(&ports);
1286 add_port(struct vsctl_context *ctx,
1287 const char *br_name, const char *port_name,
1288 bool may_exist, bool fake_iface,
1289 char *iface_names[], int n_ifaces,
1290 char *settings[], int n_settings)
1292 struct vsctl_info info;
1293 struct vsctl_bridge *bridge;
1294 struct ovsrec_interface **ifaces;
1295 struct ovsrec_port *port;
1298 get_info(ctx->ovs, &info);
1300 struct vsctl_port *port;
1302 port = find_port(&info, port_name, false);
1304 struct svec want_names, have_names;
1307 svec_init(&want_names);
1308 for (i = 0; i < n_ifaces; i++) {
1309 svec_add(&want_names, iface_names[i]);
1311 svec_sort(&want_names);
1313 svec_init(&have_names);
1314 for (i = 0; i < port->port_cfg->n_interfaces; i++) {
1315 svec_add(&have_names, port->port_cfg->interfaces[i]->name);
1317 svec_sort(&have_names);
1319 if (strcmp(port->bridge->name, br_name)) {
1320 char *command = vsctl_context_to_string(ctx);
1321 vsctl_fatal("\"%s\" but %s is actually attached to bridge %s",
1322 command, port_name, port->bridge->name);
1325 if (!svec_equal(&want_names, &have_names)) {
1326 char *have_names_string = svec_join(&have_names, ", ", "");
1327 char *command = vsctl_context_to_string(ctx);
1329 vsctl_fatal("\"%s\" but %s actually has interface(s) %s",
1330 command, port_name, have_names_string);
1333 svec_destroy(&want_names);
1334 svec_destroy(&have_names);
1339 check_conflicts(&info, port_name,
1340 xasprintf("cannot create a port named %s", port_name));
1341 for (i = 0; i < n_ifaces; i++) {
1342 check_conflicts(&info, iface_names[i],
1343 xasprintf("cannot create an interface named %s",
1346 bridge = find_bridge(&info, br_name, true);
1348 ifaces = xmalloc(n_ifaces * sizeof *ifaces);
1349 for (i = 0; i < n_ifaces; i++) {
1350 ifaces[i] = ovsrec_interface_insert(ctx->txn);
1351 ovsrec_interface_set_name(ifaces[i], iface_names[i]);
1354 port = ovsrec_port_insert(ctx->txn);
1355 ovsrec_port_set_name(port, port_name);
1356 ovsrec_port_set_interfaces(port, ifaces, n_ifaces);
1357 ovsrec_port_set_bond_fake_iface(port, fake_iface);
1361 int64_t tag = bridge->vlan;
1362 ovsrec_port_set_tag(port, &tag, 1);
1365 for (i = 0; i < n_settings; i++) {
1366 set_column(get_table("Port"), &port->header_, settings[i],
1370 bridge_insert_port((bridge->parent ? bridge->parent->br_cfg
1371 : bridge->br_cfg), port);
1377 cmd_add_port(struct vsctl_context *ctx)
1379 bool may_exist = shash_find(&ctx->options, "--may-exist") != 0;
1381 add_port(ctx, ctx->argv[1], ctx->argv[2], may_exist, false,
1382 &ctx->argv[2], 1, &ctx->argv[3], ctx->argc - 3);
1386 cmd_add_bond(struct vsctl_context *ctx)
1388 bool may_exist = shash_find(&ctx->options, "--may-exist") != 0;
1389 bool fake_iface = shash_find(&ctx->options, "--fake-iface");
1393 n_ifaces = ctx->argc - 3;
1394 for (i = 3; i < ctx->argc; i++) {
1395 if (strchr(ctx->argv[i], '=')) {
1401 vsctl_fatal("add-bond requires at least 2 interfaces, but only "
1402 "%d were specified", n_ifaces);
1405 add_port(ctx, ctx->argv[1], ctx->argv[2], may_exist, fake_iface,
1406 &ctx->argv[3], n_ifaces,
1407 &ctx->argv[n_ifaces + 3], ctx->argc - 3 - n_ifaces);
1411 cmd_del_port(struct vsctl_context *ctx)
1413 bool must_exist = !shash_find(&ctx->options, "--if-exists");
1414 bool with_iface = shash_find(&ctx->options, "--with-iface") != NULL;
1415 struct vsctl_port *port;
1416 struct vsctl_info info;
1418 get_info(ctx->ovs, &info);
1420 port = find_port(&info, ctx->argv[ctx->argc - 1], must_exist);
1422 const char *target = ctx->argv[ctx->argc - 1];
1423 struct vsctl_iface *iface;
1425 port = find_port(&info, target, false);
1427 iface = find_iface(&info, target, false);
1432 if (must_exist && !port) {
1433 vsctl_fatal("no port or interface named %s", target);
1438 if (ctx->argc == 3) {
1439 struct vsctl_bridge *bridge;
1441 bridge = find_bridge(&info, ctx->argv[1], true);
1442 if (port->bridge != bridge) {
1443 if (port->bridge->parent == bridge) {
1444 vsctl_fatal("bridge %s does not have a port %s (although "
1445 "its parent bridge %s does)",
1446 ctx->argv[1], ctx->argv[2],
1447 bridge->parent->name);
1449 vsctl_fatal("bridge %s does not have a port %s",
1450 ctx->argv[1], ctx->argv[2]);
1455 del_port(&info, port);
1462 cmd_port_to_br(struct vsctl_context *ctx)
1464 struct vsctl_port *port;
1465 struct vsctl_info info;
1467 get_info(ctx->ovs, &info);
1468 port = find_port(&info, ctx->argv[1], true);
1469 ds_put_format(&ctx->output, "%s\n", port->bridge->name);
1474 cmd_br_to_vlan(struct vsctl_context *ctx)
1476 struct vsctl_bridge *bridge;
1477 struct vsctl_info info;
1479 get_info(ctx->ovs, &info);
1480 bridge = find_bridge(&info, ctx->argv[1], true);
1481 ds_put_format(&ctx->output, "%d\n", bridge->vlan);
1486 cmd_br_to_parent(struct vsctl_context *ctx)
1488 struct vsctl_bridge *bridge;
1489 struct vsctl_info info;
1491 get_info(ctx->ovs, &info);
1492 bridge = find_bridge(&info, ctx->argv[1], true);
1493 if (bridge->parent) {
1494 bridge = bridge->parent;
1496 ds_put_format(&ctx->output, "%s\n", bridge->name);
1501 cmd_list_ifaces(struct vsctl_context *ctx)
1503 struct vsctl_bridge *br;
1504 struct shash_node *node;
1505 struct vsctl_info info;
1508 get_info(ctx->ovs, &info);
1509 br = find_bridge(&info, ctx->argv[1], true);
1512 SHASH_FOR_EACH (node, &info.ifaces) {
1513 struct vsctl_iface *iface = node->data;
1515 if (strcmp(iface->iface_cfg->name, br->name)
1516 && br == iface->port->bridge) {
1517 svec_add(&ifaces, iface->iface_cfg->name);
1520 output_sorted(&ifaces, &ctx->output);
1521 svec_destroy(&ifaces);
1527 cmd_iface_to_br(struct vsctl_context *ctx)
1529 struct vsctl_iface *iface;
1530 struct vsctl_info info;
1532 get_info(ctx->ovs, &info);
1533 iface = find_iface(&info, ctx->argv[1], true);
1534 ds_put_format(&ctx->output, "%s\n", iface->port->bridge->name);
1539 cmd_get_controller(struct vsctl_context *ctx)
1541 struct vsctl_info info;
1542 struct vsctl_bridge *br;
1543 struct svec targets;
1546 get_info(ctx->ovs, &info);
1547 br = find_bridge(&info, ctx->argv[1], true);
1549 /* Print the targets in sorted order for reproducibility. */
1550 svec_init(&targets);
1551 for (i = 0; i < br->n_ctrl; i++) {
1552 svec_add(&targets, br->ctrl[i]->target);
1555 svec_sort(&targets);
1556 for (i = 0; i < targets.n; i++) {
1557 ds_put_format(&ctx->output, "%s\n", targets.names[i]);
1559 svec_destroy(&targets);
1565 delete_controllers(struct ovsrec_controller **controllers,
1566 size_t n_controllers)
1570 for (i = 0; i < n_controllers; i++) {
1571 ovsrec_controller_delete(controllers[i]);
1576 cmd_del_controller(struct vsctl_context *ctx)
1578 struct vsctl_info info;
1579 struct vsctl_bridge *br;
1581 get_info(ctx->ovs, &info);
1582 br = find_real_bridge(&info, ctx->argv[1], true);
1585 delete_controllers(br->ctrl, br->n_ctrl);
1586 ovsrec_bridge_set_controller(br->br_cfg, NULL, 0);
1592 static struct ovsrec_controller **
1593 insert_controllers(struct ovsdb_idl_txn *txn, char *targets[], size_t n)
1595 struct ovsrec_controller **controllers;
1598 controllers = xmalloc(n * sizeof *controllers);
1599 for (i = 0; i < n; i++) {
1600 controllers[i] = ovsrec_controller_insert(txn);
1601 ovsrec_controller_set_target(controllers[i], targets[i]);
1608 cmd_set_controller(struct vsctl_context *ctx)
1610 struct vsctl_info info;
1611 struct vsctl_bridge *br;
1612 struct ovsrec_controller **controllers;
1615 get_info(ctx->ovs, &info);
1616 br = find_real_bridge(&info, ctx->argv[1], true);
1618 delete_controllers(br->ctrl, br->n_ctrl);
1621 controllers = insert_controllers(ctx->txn, &ctx->argv[2], n);
1622 ovsrec_bridge_set_controller(br->br_cfg, controllers, n);
1629 get_fail_mode(struct ovsrec_controller **controllers, size_t n_controllers)
1631 const char *fail_mode;
1635 for (i = 0; i < n_controllers; i++) {
1636 const char *s = controllers[i]->fail_mode;
1638 if (!strcmp(s, "secure")) {
1650 cmd_get_fail_mode(struct vsctl_context *ctx)
1652 struct vsctl_info info;
1653 struct vsctl_bridge *br;
1654 const char *fail_mode = NULL;
1656 get_info(ctx->ovs, &info);
1657 br = find_bridge(&info, ctx->argv[1], true);
1659 fail_mode = get_fail_mode(br->ctrl, br->n_ctrl);
1661 if (fail_mode && strlen(fail_mode)) {
1662 ds_put_format(&ctx->output, "%s\n", fail_mode);
1669 set_fail_mode(struct ovsrec_controller **controllers, size_t n_controllers,
1670 const char *fail_mode)
1674 for (i = 0; i < n_controllers; i++) {
1675 ovsrec_controller_set_fail_mode(controllers[i], fail_mode);
1680 cmd_del_fail_mode(struct vsctl_context *ctx)
1682 struct vsctl_info info;
1683 struct vsctl_bridge *br;
1685 get_info(ctx->ovs, &info);
1686 br = find_real_bridge(&info, ctx->argv[1], true);
1688 set_fail_mode(br->ctrl, br->n_ctrl, NULL);
1694 cmd_set_fail_mode(struct vsctl_context *ctx)
1696 struct vsctl_info info;
1697 struct vsctl_bridge *br;
1698 const char *fail_mode = ctx->argv[2];
1700 get_info(ctx->ovs, &info);
1701 br = find_real_bridge(&info, ctx->argv[1], true);
1703 if (strcmp(fail_mode, "standalone") && strcmp(fail_mode, "secure")) {
1704 vsctl_fatal("fail-mode must be \"standalone\" or \"secure\"");
1708 vsctl_fatal("no controller declared for %s", br->name);
1710 set_fail_mode(br->ctrl, br->n_ctrl, fail_mode);
1716 cmd_get_ssl(struct vsctl_context *ctx)
1718 struct ovsrec_ssl *ssl = ctx->ovs->ssl;
1721 ds_put_format(&ctx->output, "Private key: %s\n", ssl->private_key);
1722 ds_put_format(&ctx->output, "Certificate: %s\n", ssl->certificate);
1723 ds_put_format(&ctx->output, "CA Certificate: %s\n", ssl->ca_cert);
1724 ds_put_format(&ctx->output, "Bootstrap: %s\n",
1725 ssl->bootstrap_ca_cert ? "true" : "false");
1730 cmd_del_ssl(struct vsctl_context *ctx)
1732 struct ovsrec_ssl *ssl = ctx->ovs->ssl;
1735 ovsrec_ssl_delete(ssl);
1736 ovsrec_open_vswitch_set_ssl(ctx->ovs, NULL);
1741 cmd_set_ssl(struct vsctl_context *ctx)
1743 bool bootstrap = shash_find(&ctx->options, "--bootstrap");
1744 struct ovsrec_ssl *ssl = ctx->ovs->ssl;
1747 ovsrec_ssl_delete(ssl);
1749 ssl = ovsrec_ssl_insert(ctx->txn);
1751 ovsrec_ssl_set_private_key(ssl, ctx->argv[1]);
1752 ovsrec_ssl_set_certificate(ssl, ctx->argv[2]);
1753 ovsrec_ssl_set_ca_cert(ssl, ctx->argv[3]);
1755 ovsrec_ssl_set_bootstrap_ca_cert(ssl, bootstrap);
1757 ovsrec_open_vswitch_set_ssl(ctx->ovs, ssl);
1760 /* Parameter commands. */
1762 struct vsctl_row_id {
1763 const struct ovsdb_idl_table_class *table;
1764 const struct ovsdb_idl_column *name_column;
1765 const struct ovsdb_idl_column *uuid_column;
1768 struct vsctl_table_class {
1769 struct ovsdb_idl_table_class *class;
1770 struct vsctl_row_id row_ids[2];
1773 static const struct vsctl_table_class tables[] = {
1774 {&ovsrec_table_bridge,
1775 {{&ovsrec_table_bridge, &ovsrec_bridge_col_name, NULL},
1776 {NULL, NULL, NULL}}},
1778 {&ovsrec_table_controller,
1779 {{&ovsrec_table_bridge,
1780 &ovsrec_bridge_col_name,
1781 &ovsrec_bridge_col_controller}}},
1783 {&ovsrec_table_interface,
1784 {{&ovsrec_table_interface, &ovsrec_interface_col_name, NULL},
1785 {NULL, NULL, NULL}}},
1787 {&ovsrec_table_mirror,
1788 {{&ovsrec_table_mirror, &ovsrec_mirror_col_name, NULL},
1789 {NULL, NULL, NULL}}},
1791 {&ovsrec_table_netflow,
1792 {{&ovsrec_table_bridge,
1793 &ovsrec_bridge_col_name,
1794 &ovsrec_bridge_col_netflow},
1795 {NULL, NULL, NULL}}},
1797 {&ovsrec_table_open_vswitch,
1798 {{&ovsrec_table_open_vswitch, NULL, NULL},
1799 {NULL, NULL, NULL}}},
1801 {&ovsrec_table_port,
1802 {{&ovsrec_table_port, &ovsrec_port_col_name, NULL},
1803 {NULL, NULL, NULL}}},
1806 {{&ovsrec_table_port, &ovsrec_port_col_name, &ovsrec_port_col_qos},
1807 {NULL, NULL, NULL}}},
1809 {&ovsrec_table_queue,
1810 {{NULL, NULL, NULL},
1811 {NULL, NULL, NULL}}},
1814 {{&ovsrec_table_open_vswitch, NULL, &ovsrec_open_vswitch_col_ssl}}},
1816 {&ovsrec_table_sflow,
1817 {{&ovsrec_table_bridge,
1818 &ovsrec_bridge_col_name,
1819 &ovsrec_bridge_col_sflow},
1820 {NULL, NULL, NULL}}},
1822 {NULL, {{NULL, NULL, NULL}, {NULL, NULL, NULL}}}
1826 die_if_error(char *error)
1829 vsctl_fatal("%s", error);
1834 to_lower_and_underscores(unsigned c)
1836 return c == '-' ? '_' : tolower(c);
1840 score_partial_match(const char *name, const char *s)
1844 if (!strcmp(name, s)) {
1847 for (score = 0; ; score++, name++, s++) {
1848 if (to_lower_and_underscores(*name) != to_lower_and_underscores(*s)) {
1850 } else if (*name == '\0') {
1851 return UINT_MAX - 1;
1854 return *s == '\0' ? score : 0;
1857 static const struct vsctl_table_class *
1858 get_table(const char *table_name)
1860 const struct vsctl_table_class *table;
1861 const struct vsctl_table_class *best_match = NULL;
1862 unsigned int best_score = 0;
1864 for (table = tables; table->class; table++) {
1865 unsigned int score = score_partial_match(table->class->name,
1867 if (score > best_score) {
1870 } else if (score == best_score) {
1876 } else if (best_score) {
1877 vsctl_fatal("multiple table names match \"%s\"", table_name);
1879 vsctl_fatal("unknown table \"%s\"", table_name);
1883 static const struct ovsdb_idl_row *
1884 get_row_by_id(struct vsctl_context *ctx, const struct vsctl_table_class *table,
1885 const struct vsctl_row_id *id, const char *record_id)
1887 const struct ovsdb_idl_row *referrer, *final;
1893 if (!id->name_column) {
1894 if (strcmp(record_id, ".")) {
1897 referrer = ovsdb_idl_first_row(ctx->idl, id->table);
1898 if (!referrer || ovsdb_idl_next_row(referrer)) {
1902 const struct ovsdb_idl_row *row;
1905 for (row = ovsdb_idl_first_row(ctx->idl, id->table);
1907 row = ovsdb_idl_next_row(row))
1909 const struct ovsdb_datum *name;
1911 name = ovsdb_idl_get(row, id->name_column,
1912 OVSDB_TYPE_STRING, OVSDB_TYPE_VOID);
1913 if (name->n == 1 && !strcmp(name->keys[0].string, record_id)) {
1915 vsctl_fatal("multiple rows in %s match \"%s\"",
1916 table->class->name, record_id);
1927 if (id->uuid_column) {
1928 const struct ovsdb_datum *uuid;
1930 uuid = ovsdb_idl_get(referrer, id->uuid_column,
1931 OVSDB_TYPE_UUID, OVSDB_TYPE_VOID);
1933 final = ovsdb_idl_get_row_for_uuid(ctx->idl, table->class,
1934 &uuid->keys[0].uuid);
1943 static const struct ovsdb_idl_row *
1944 get_row (struct vsctl_context *ctx,
1945 const struct vsctl_table_class *table, const char *record_id)
1947 const struct ovsdb_idl_row *row;
1950 if (uuid_from_string(&uuid, record_id)) {
1951 row = ovsdb_idl_get_row_for_uuid(ctx->idl, table->class, &uuid);
1955 for (i = 0; i < ARRAY_SIZE(table->row_ids); i++) {
1956 row = get_row_by_id(ctx, table, &table->row_ids[i], record_id);
1965 static const struct ovsdb_idl_row *
1966 must_get_row(struct vsctl_context *ctx,
1967 const struct vsctl_table_class *table, const char *record_id)
1969 const struct ovsdb_idl_row *row = get_row(ctx, table, record_id);
1971 vsctl_fatal("no row \"%s\" in table %s",
1972 record_id, table->class->name);
1978 get_column(const struct vsctl_table_class *table, const char *column_name,
1979 const struct ovsdb_idl_column **columnp)
1981 const struct ovsdb_idl_column *best_match = NULL;
1982 unsigned int best_score = 0;
1985 for (i = 0; i < table->class->n_columns; i++) {
1986 const struct ovsdb_idl_column *column = &table->class->columns[i];
1987 unsigned int score = score_partial_match(column->name, column_name);
1988 if (score > best_score) {
1989 best_match = column;
1991 } else if (score == best_score) {
1996 *columnp = best_match;
1999 } else if (best_score) {
2000 return xasprintf("%s contains more than one column whose name "
2001 "matches \"%s\"", table->class->name, column_name);
2003 return xasprintf("%s does not contain a column whose name matches "
2004 "\"%s\"", table->class->name, column_name);
2009 missing_operator_error(const char *arg, const char **allowed_operators,
2015 ds_put_format(&s, "%s: argument does not end in ", arg);
2016 ds_put_format(&s, "\"%s\"", allowed_operators[0]);
2017 if (n_allowed == 2) {
2018 ds_put_format(&s, " or \"%s\"", allowed_operators[1]);
2019 } else if (n_allowed > 2) {
2022 for (i = 1; i < n_allowed - 1; i++) {
2023 ds_put_format(&s, ", \"%s\"", allowed_operators[i]);
2025 ds_put_format(&s, ", or \"%s\"", allowed_operators[i]);
2027 ds_put_format(&s, " followed by a value.");
2029 return ds_steal_cstr(&s);
2032 /* Breaks 'arg' apart into a number of fields in the following order:
2034 * - If 'columnp' is nonnull, the name of a column in 'table'. The column
2035 * is stored into '*columnp'. The column name may be abbreviated.
2037 * - If 'keyp' is nonnull, optionally a key string. (If both 'columnp'
2038 * and 'keyp' are nonnull, then the column and key names are expected to
2039 * be separated by ':'). The key is stored as a malloc()'d string into
2040 * '*keyp', or NULL if no key is present in 'arg'.
2042 * - If 'valuep' is nonnull, an operator followed by a value string. The
2043 * allowed operators are the 'n_allowed' string in 'allowed_operators',
2044 * or just "=" if 'n_allowed' is 0. If 'operatorp' is nonnull, then the
2045 * operator is stored into '*operatorp' (one of the pointers from
2046 * 'allowed_operators' is stored; nothing is malloc()'d). The value is
2047 * stored as a malloc()'d string into '*valuep', or NULL if no value is
2050 * At least 'columnp' or 'keyp' must be nonnull.
2052 * On success, returns NULL. On failure, returned a malloc()'d string error
2053 * message and stores NULL into all of the nonnull output arguments. */
2054 static char * WARN_UNUSED_RESULT
2055 parse_column_key_value(const char *arg,
2056 const struct vsctl_table_class *table,
2057 const struct ovsdb_idl_column **columnp, char **keyp,
2058 const char **operatorp,
2059 const char **allowed_operators, size_t n_allowed,
2062 const char *p = arg;
2065 assert(columnp || keyp);
2066 assert(!(operatorp && !valuep));
2074 /* Parse column name. */
2078 error = ovsdb_token_parse(&p, &column_name);
2082 if (column_name[0] == '\0') {
2084 error = xasprintf("%s: missing column name", arg);
2087 error = get_column(table, column_name, columnp);
2094 /* Parse key string. */
2095 if (*p == ':' || !columnp) {
2099 error = xasprintf("%s: key not accepted here", arg);
2102 error = ovsdb_token_parse(&p, keyp);
2110 /* Parse value string. */
2116 if (!allowed_operators) {
2117 static const char *equals = "=";
2118 allowed_operators = =
2124 for (i = 0; i < n_allowed; i++) {
2125 const char *op = allowed_operators[i];
2126 size_t op_len = strlen(op);
2128 if (op_len > best_len && !strncmp(op, p, op_len) && p[op_len]) {
2134 error = missing_operator_error(arg, allowed_operators, n_allowed);
2141 *valuep = xstrdup(p + best_len);
2147 error = xasprintf("%s: trailing garbage \"%s\" in argument",
2173 cmd_get(struct vsctl_context *ctx)
2175 bool if_exists = shash_find(&ctx->options, "--if-exists");
2176 const char *table_name = ctx->argv[1];
2177 const char *record_id = ctx->argv[2];
2178 const struct vsctl_table_class *table;
2179 const struct ovsdb_idl_row *row;
2180 struct ds *out = &ctx->output;
2183 table = get_table(table_name);
2184 row = must_get_row(ctx, table, record_id);
2185 for (i = 3; i < ctx->argc; i++) {
2186 const struct ovsdb_idl_column *column;
2187 const struct ovsdb_datum *datum;
2190 /* Special case for obtaining the UUID of a row. We can't just do this
2191 * through parse_column_key_value() below since it returns a "struct
2192 * ovsdb_idl_column" and the UUID column doesn't have one. */
2193 if (!strcasecmp(ctx->argv[i], "_uuid")
2194 || !strcasecmp(ctx->argv[i], "-uuid")) {
2195 ds_put_format(out, UUID_FMT"\n", UUID_ARGS(&row->uuid));
2199 die_if_error(parse_column_key_value(ctx->argv[i], table,
2200 &column, &key_string,
2201 NULL, NULL, 0, NULL));
2203 datum = ovsdb_idl_read(row, column);
2205 union ovsdb_atom key;
2208 if (column->type.value.type == OVSDB_TYPE_VOID) {
2209 vsctl_fatal("cannot specify key to get for non-map column %s",
2213 die_if_error(ovsdb_atom_from_string(&key,
2215 key_string, ctx->symtab));
2217 idx = ovsdb_datum_find_key(datum, &key,
2218 column->type.key.type);
2219 if (idx == UINT_MAX) {
2221 vsctl_fatal("no key \"%s\" in %s record \"%s\" column %s",
2222 key_string, table->class->name, record_id,
2226 ovsdb_atom_to_string(&datum->values[idx],
2227 column->type.value.type, out);
2229 ovsdb_atom_destroy(&key, column->type.key.type);
2231 ovsdb_datum_to_string(datum, &column->type, out);
2233 ds_put_char(out, '\n');
2240 list_record(const struct vsctl_table_class *table,
2241 const struct ovsdb_idl_row *row, struct ds *out)
2245 ds_put_format(out, "%-20s: "UUID_FMT"\n", "_uuid",
2246 UUID_ARGS(&row->uuid));
2247 for (i = 0; i < table->class->n_columns; i++) {
2248 const struct ovsdb_idl_column *column = &table->class->columns[i];
2249 const struct ovsdb_datum *datum;
2251 datum = ovsdb_idl_read(row, column);
2253 ds_put_format(out, "%-20s: ", column->name);
2254 ovsdb_datum_to_string(datum, &column->type, out);
2255 ds_put_char(out, '\n');
2260 cmd_list(struct vsctl_context *ctx)
2262 const char *table_name = ctx->argv[1];
2263 const struct vsctl_table_class *table;
2264 struct ds *out = &ctx->output;
2267 table = get_table(table_name);
2268 if (ctx->argc > 2) {
2269 for (i = 2; i < ctx->argc; i++) {
2271 ds_put_char(out, '\n');
2273 list_record(table, must_get_row(ctx, table, ctx->argv[i]), out);
2276 const struct ovsdb_idl_row *row;
2279 for (row = ovsdb_idl_first_row(ctx->idl, table->class), first = true;
2281 row = ovsdb_idl_next_row(row), first = false) {
2283 ds_put_char(out, '\n');
2285 list_record(table, row, out);
2291 set_column(const struct vsctl_table_class *table,
2292 const struct ovsdb_idl_row *row, const char *arg,
2293 struct ovsdb_symbol_table *symtab)
2295 const struct ovsdb_idl_column *column;
2296 char *key_string, *value_string;
2299 error = parse_column_key_value(arg, table, &column, &key_string,
2300 NULL, NULL, 0, &value_string);
2301 die_if_error(error);
2302 if (!value_string) {
2303 vsctl_fatal("%s: missing value", arg);
2307 union ovsdb_atom key, value;
2308 struct ovsdb_datum datum;
2310 if (column->type.value.type == OVSDB_TYPE_VOID) {
2311 vsctl_fatal("cannot specify key to set for non-map column %s",
2315 die_if_error(ovsdb_atom_from_string(&key, &column->type.key,
2316 key_string, symtab));
2317 die_if_error(ovsdb_atom_from_string(&value, &column->type.value,
2318 value_string, symtab));
2320 ovsdb_datum_init_empty(&datum);
2321 ovsdb_datum_add_unsafe(&datum, &key, &value, &column->type);
2323 ovsdb_atom_destroy(&key, column->type.key.type);
2324 ovsdb_atom_destroy(&value, column->type.value.type);
2326 ovsdb_datum_union(&datum, ovsdb_idl_read(row, column),
2327 &column->type, false);
2328 ovsdb_idl_txn_write(row, column, &datum);
2330 struct ovsdb_datum datum;
2332 die_if_error(ovsdb_datum_from_string(&datum, &column->type,
2333 value_string, symtab));
2334 ovsdb_idl_txn_write(row, column, &datum);
2342 cmd_set(struct vsctl_context *ctx)
2344 const char *table_name = ctx->argv[1];
2345 const char *record_id = ctx->argv[2];
2346 const struct vsctl_table_class *table;
2347 const struct ovsdb_idl_row *row;
2350 table = get_table(table_name);
2351 row = must_get_row(ctx, table, record_id);
2352 for (i = 3; i < ctx->argc; i++) {
2353 set_column(table, row, ctx->argv[i], ctx->symtab);
2358 cmd_add(struct vsctl_context *ctx)
2360 const char *table_name = ctx->argv[1];
2361 const char *record_id = ctx->argv[2];
2362 const char *column_name = ctx->argv[3];
2363 const struct vsctl_table_class *table;
2364 const struct ovsdb_idl_column *column;
2365 const struct ovsdb_idl_row *row;
2366 const struct ovsdb_type *type;
2367 struct ovsdb_datum old;
2370 table = get_table(table_name);
2371 row = must_get_row(ctx, table, record_id);
2372 die_if_error(get_column(table, column_name, &column));
2374 type = &column->type;
2375 ovsdb_datum_clone(&old, ovsdb_idl_read(row, column), &column->type);
2376 for (i = 4; i < ctx->argc; i++) {
2377 struct ovsdb_type add_type;
2378 struct ovsdb_datum add;
2382 add_type.n_max = UINT_MAX;
2383 die_if_error(ovsdb_datum_from_string(&add, &add_type, ctx->argv[i],
2385 ovsdb_datum_union(&old, &add, type, false);
2386 ovsdb_datum_destroy(&add, type);
2388 if (old.n > type->n_max) {
2389 vsctl_fatal("\"add\" operation would put %u %s in column %s of "
2390 "table %s but the maximum number is %u",
2392 type->value.type == OVSDB_TYPE_VOID ? "values" : "pairs",
2393 column->name, table->class->name, type->n_max);
2395 ovsdb_idl_txn_write(row, column, &old);
2399 cmd_remove(struct vsctl_context *ctx)
2401 const char *table_name = ctx->argv[1];
2402 const char *record_id = ctx->argv[2];
2403 const char *column_name = ctx->argv[3];
2404 const struct vsctl_table_class *table;
2405 const struct ovsdb_idl_column *column;
2406 const struct ovsdb_idl_row *row;
2407 const struct ovsdb_type *type;
2408 struct ovsdb_datum old;
2411 table = get_table(table_name);
2412 row = must_get_row(ctx, table, record_id);
2413 die_if_error(get_column(table, column_name, &column));
2415 type = &column->type;
2416 ovsdb_datum_clone(&old, ovsdb_idl_read(row, column), &column->type);
2417 for (i = 4; i < ctx->argc; i++) {
2418 struct ovsdb_type rm_type;
2419 struct ovsdb_datum rm;
2424 rm_type.n_max = UINT_MAX;
2425 error = ovsdb_datum_from_string(&rm, &rm_type,
2426 ctx->argv[i], ctx->symtab);
2427 if (error && ovsdb_type_is_map(&rm_type)) {
2429 rm_type.value.type = OVSDB_TYPE_VOID;
2430 die_if_error(ovsdb_datum_from_string(&rm, &rm_type,
2431 ctx->argv[i], ctx->symtab));
2433 ovsdb_datum_subtract(&old, type, &rm, &rm_type);
2434 ovsdb_datum_destroy(&rm, &rm_type);
2436 if (old.n < type->n_min) {
2437 vsctl_fatal("\"remove\" operation would put %u %s in column %s of "
2438 "table %s but the minimum number is %u",
2440 type->value.type == OVSDB_TYPE_VOID ? "values" : "pairs",
2441 column->name, table->class->name, type->n_min);
2443 ovsdb_idl_txn_write(row, column, &old);
2447 cmd_clear(struct vsctl_context *ctx)
2449 const char *table_name = ctx->argv[1];
2450 const char *record_id = ctx->argv[2];
2451 const struct vsctl_table_class *table;
2452 const struct ovsdb_idl_row *row;
2455 table = get_table(table_name);
2456 row = must_get_row(ctx, table, record_id);
2457 for (i = 3; i < ctx->argc; i++) {
2458 const struct ovsdb_idl_column *column;
2459 const struct ovsdb_type *type;
2460 struct ovsdb_datum datum;
2462 die_if_error(get_column(table, ctx->argv[i], &column));
2464 type = &column->type;
2465 if (type->n_min > 0) {
2466 vsctl_fatal("\"clear\" operation cannot be applied to column %s "
2467 "of table %s, which is not allowed to be empty",
2468 column->name, table->class->name);
2471 ovsdb_datum_init_empty(&datum);
2472 ovsdb_idl_txn_write(row, column, &datum);
2477 cmd_create(struct vsctl_context *ctx)
2479 const char *id = shash_find_data(&ctx->options, "--id");
2480 const char *table_name = ctx->argv[1];
2481 const struct vsctl_table_class *table;
2482 const struct ovsdb_idl_row *row;
2483 const struct uuid *uuid;
2487 struct ovsdb_symbol *symbol;
2490 vsctl_fatal("row id \"%s\" does not begin with \"@\"", id);
2493 symbol = ovsdb_symbol_table_insert(ctx->symtab, id);
2495 vsctl_fatal("row id \"%s\" may only be used to insert a single "
2498 symbol->used = true;
2500 uuid = &symbol->uuid;
2505 table = get_table(table_name);
2506 row = ovsdb_idl_txn_insert(ctx->txn, table->class, uuid);
2507 for (i = 2; i < ctx->argc; i++) {
2508 set_column(table, row, ctx->argv[i], ctx->symtab);
2510 ds_put_format(&ctx->output, UUID_FMT, UUID_ARGS(&row->uuid));
2513 /* This function may be used as the 'postprocess' function for commands that
2514 * insert new rows into the database. It expects that the command's 'run'
2515 * function prints the UUID reported by ovsdb_idl_txn_insert() as the command's
2516 * sole output. It replaces that output by the row's permanent UUID assigned
2517 * by the database server and appends a new-line.
2519 * Currently we use this only for "create", because the higher-level commands
2520 * are supposed to be independent of the actual structure of the vswitch
2523 post_create(struct vsctl_context *ctx)
2525 const struct uuid *real;
2528 uuid_from_string(&dummy, ds_cstr(&ctx->output));
2529 real = ovsdb_idl_txn_get_insert_uuid(ctx->txn, &dummy);
2531 ds_clear(&ctx->output);
2532 ds_put_format(&ctx->output, UUID_FMT, UUID_ARGS(real));
2534 ds_put_char(&ctx->output, '\n');
2538 cmd_destroy(struct vsctl_context *ctx)
2540 bool must_exist = !shash_find(&ctx->options, "--if-exists");
2541 const char *table_name = ctx->argv[1];
2542 const struct vsctl_table_class *table;
2545 table = get_table(table_name);
2546 for (i = 2; i < ctx->argc; i++) {
2547 const struct ovsdb_idl_row *row;
2549 row = (must_exist ? must_get_row : get_row)(ctx, table, ctx->argv[i]);
2551 ovsdb_idl_txn_delete(row);
2557 is_condition_satified(const struct vsctl_table_class *table,
2558 const struct ovsdb_idl_row *row, const char *arg,
2559 struct ovsdb_symbol_table *symtab)
2561 static const char *operators[] = {
2562 "=", "!=", "<", ">", "<=", ">="
2565 const struct ovsdb_idl_column *column;
2566 const struct ovsdb_datum *have_datum;
2567 char *key_string, *value_string;
2568 const char *operator;
2573 error = parse_column_key_value(arg, table, &column, &key_string,
2574 &operator, operators, ARRAY_SIZE(operators),
2576 die_if_error(error);
2577 if (!value_string) {
2578 vsctl_fatal("%s: missing value", arg);
2581 have_datum = ovsdb_idl_read(row, column);
2583 union ovsdb_atom want_key, want_value;
2585 if (column->type.value.type == OVSDB_TYPE_VOID) {
2586 vsctl_fatal("cannot specify key to check for non-map column %s",
2590 die_if_error(ovsdb_atom_from_string(&want_key, &column->type.key,
2591 key_string, symtab));
2592 die_if_error(ovsdb_atom_from_string(&want_value, &column->type.value,
2593 value_string, symtab));
2595 idx = ovsdb_datum_find_key(have_datum,
2596 &want_key, column->type.key.type);
2597 if (idx != UINT_MAX) {
2598 cmp = ovsdb_atom_compare_3way(&have_datum->values[idx],
2600 column->type.value.type);
2603 ovsdb_atom_destroy(&want_key, column->type.key.type);
2604 ovsdb_atom_destroy(&want_value, column->type.value.type);
2606 struct ovsdb_datum want_datum;
2608 die_if_error(ovsdb_datum_from_string(&want_datum, &column->type,
2609 value_string, symtab));
2611 cmp = ovsdb_datum_compare_3way(have_datum, &want_datum,
2613 ovsdb_datum_destroy(&want_datum, &column->type);
2619 return (idx == UINT_MAX ? false
2620 : !strcmp(operator, "=") ? cmp == 0
2621 : !strcmp(operator, "!=") ? cmp != 0
2622 : !strcmp(operator, "<") ? cmp < 0
2623 : !strcmp(operator, ">") ? cmp > 0
2624 : !strcmp(operator, "<=") ? cmp <= 0
2625 : !strcmp(operator, ">=") ? cmp >= 0
2630 cmd_wait_until(struct vsctl_context *ctx)
2632 const char *table_name = ctx->argv[1];
2633 const char *record_id = ctx->argv[2];
2634 const struct vsctl_table_class *table;
2635 const struct ovsdb_idl_row *row;
2638 table = get_table(table_name);
2640 row = get_row(ctx, table, record_id);
2642 ctx->try_again = true;
2646 for (i = 3; i < ctx->argc; i++) {
2647 if (!is_condition_satified(table, row, ctx->argv[i], ctx->symtab)) {
2648 ctx->try_again = true;
2654 static struct json *
2655 where_uuid_equals(const struct uuid *uuid)
2658 json_array_create_1(
2659 json_array_create_3(
2660 json_string_create("_uuid"),
2661 json_string_create("=="),
2662 json_array_create_2(
2663 json_string_create("uuid"),
2664 json_string_create_nocopy(
2665 xasprintf(UUID_FMT, UUID_ARGS(uuid))))));
2669 vsctl_context_init(struct vsctl_context *ctx, struct vsctl_command *command,
2670 struct ovsdb_idl *idl, struct ovsdb_idl_txn *txn,
2671 const struct ovsrec_open_vswitch *ovs,
2672 struct ovsdb_symbol_table *symtab)
2674 ctx->argc = command->argc;
2675 ctx->argv = command->argv;
2676 ctx->options = command->options;
2678 ds_swap(&ctx->output, &command->output);
2682 ctx->symtab = symtab;
2684 ctx->try_again = false;
2688 vsctl_context_done(struct vsctl_context *ctx, struct vsctl_command *command)
2690 ds_swap(&ctx->output, &command->output);
2694 do_vsctl(const char *args, struct vsctl_command *commands, size_t n_commands,
2695 struct ovsdb_idl *idl)
2697 struct ovsdb_idl_txn *txn;
2698 const struct ovsrec_open_vswitch *ovs;
2699 enum ovsdb_idl_txn_status status;
2700 struct ovsdb_symbol_table *symtab;
2702 struct vsctl_command *c;
2703 int64_t next_cfg = 0;
2706 txn = the_idl_txn = ovsdb_idl_txn_create(idl);
2708 ovsdb_idl_txn_set_dry_run(txn);
2711 ovsdb_idl_txn_add_comment(txn, "ovs-vsctl: %s", args);
2713 ovs = ovsrec_open_vswitch_first(idl);
2715 /* XXX add verification that table is empty */
2716 ovs = ovsrec_open_vswitch_insert(txn);
2719 if (wait_for_reload) {
2720 struct json *where = where_uuid_equals(&ovs->header_.uuid);
2721 ovsdb_idl_txn_increment(txn, "Open_vSwitch", "next_cfg", where);
2722 json_destroy(where);
2725 symtab = ovsdb_symbol_table_create();
2726 for (c = commands; c < &commands[n_commands]; c++) {
2727 ds_init(&c->output);
2729 for (c = commands; c < &commands[n_commands]; c++) {
2730 struct vsctl_context ctx;
2732 vsctl_context_init(&ctx, c, idl, txn, ovs, symtab);
2733 (c->syntax->run)(&ctx);
2734 vsctl_context_done(&ctx, c);
2736 if (ctx.try_again) {
2741 status = ovsdb_idl_txn_commit_block(txn);
2742 if (wait_for_reload && status == TXN_SUCCESS) {
2743 next_cfg = ovsdb_idl_txn_get_increment_new_value(txn);
2745 if (status == TXN_UNCHANGED || status == TXN_SUCCESS) {
2746 for (c = commands; c < &commands[n_commands]; c++) {
2747 if (c->syntax->postprocess) {
2748 struct vsctl_context ctx;
2750 vsctl_context_init(&ctx, c, idl, txn, ovs, symtab);
2751 (c->syntax->postprocess)(&ctx);
2752 vsctl_context_done(&ctx, c);
2756 error = xstrdup(ovsdb_idl_txn_get_error(txn));
2757 ovsdb_idl_txn_destroy(txn);
2760 unused = ovsdb_symbol_table_find_unused(symtab);
2762 vsctl_fatal("row id \"%s\" is referenced but never created (e.g. "
2763 "with \"-- --id=%s create ...\")", unused, unused);
2767 case TXN_INCOMPLETE:
2771 /* Should not happen--we never call ovsdb_idl_txn_abort(). */
2772 vsctl_fatal("transaction aborted");
2782 vsctl_fatal("transaction error: %s", error);
2789 ovsdb_symbol_table_destroy(symtab);
2791 for (c = commands; c < &commands[n_commands]; c++) {
2792 struct ds *ds = &c->output;
2793 struct shash_node *node;
2799 for (j = 0; j < ds->length; j++) {
2800 int c = ds->string[j];
2803 fputs("\\n", stdout);
2807 fputs("\\\\", stdout);
2816 fputs(ds_cstr(ds), stdout);
2818 ds_destroy(&c->output);
2820 SHASH_FOR_EACH (node, &c->options) {
2823 shash_destroy(&c->options);
2827 if (wait_for_reload && status != TXN_UNCHANGED) {
2829 const struct ovsrec_open_vswitch *ovs;
2832 OVSREC_OPEN_VSWITCH_FOR_EACH (ovs, idl) {
2833 if (ovs->cur_cfg >= next_cfg) {
2837 ovsdb_idl_wait(idl);
2842 ovsdb_idl_destroy(idl);
2847 /* Our transaction needs to be rerun, or a prerequisite was not met. Free
2848 * resources and return so that the caller can try again. */
2849 ovsdb_idl_txn_abort(txn);
2850 ovsdb_idl_txn_destroy(txn);
2851 ovsdb_symbol_table_destroy(symtab);
2852 for (c = commands; c < &commands[n_commands]; c++) {
2853 ds_destroy(&c->output);
2858 static const struct vsctl_command_syntax all_commands[] = {
2859 /* Open vSwitch commands. */
2860 {"init", 0, 0, cmd_init, NULL, ""},
2862 /* Bridge commands. */
2863 {"add-br", 1, 3, cmd_add_br, NULL, "--may-exist"},
2864 {"del-br", 1, 1, cmd_del_br, NULL, "--if-exists"},
2865 {"list-br", 0, 0, cmd_list_br, NULL, ""},
2866 {"br-exists", 1, 1, cmd_br_exists, NULL, ""},
2867 {"br-to-vlan", 1, 1, cmd_br_to_vlan, NULL, ""},
2868 {"br-to-parent", 1, 1, cmd_br_to_parent, NULL, ""},
2869 {"br-set-external-id", 2, 3, cmd_br_set_external_id, NULL, ""},
2870 {"br-get-external-id", 1, 2, cmd_br_get_external_id, NULL, ""},
2872 /* Port commands. */
2873 {"list-ports", 1, 1, cmd_list_ports, NULL, ""},
2874 {"add-port", 2, INT_MAX, cmd_add_port, NULL, "--may-exist"},
2875 {"add-bond", 4, INT_MAX, cmd_add_bond, NULL, "--may-exist,--fake-iface"},
2876 {"del-port", 1, 2, cmd_del_port, NULL, "--if-exists,--with-iface"},
2877 {"port-to-br", 1, 1, cmd_port_to_br, NULL, ""},
2879 /* Interface commands. */
2880 {"list-ifaces", 1, 1, cmd_list_ifaces, NULL, ""},
2881 {"iface-to-br", 1, 1, cmd_iface_to_br, NULL, ""},
2883 /* Controller commands. */
2884 {"get-controller", 1, 1, cmd_get_controller, NULL, ""},
2885 {"del-controller", 1, 1, cmd_del_controller, NULL, ""},
2886 {"set-controller", 1, INT_MAX, cmd_set_controller, NULL, ""},
2887 {"get-fail-mode", 1, 1, cmd_get_fail_mode, NULL, ""},
2888 {"del-fail-mode", 1, 1, cmd_del_fail_mode, NULL, ""},
2889 {"set-fail-mode", 2, 2, cmd_set_fail_mode, NULL, ""},
2892 {"get-ssl", 0, 0, cmd_get_ssl, NULL, ""},
2893 {"del-ssl", 0, 0, cmd_del_ssl, NULL, ""},
2894 {"set-ssl", 3, 3, cmd_set_ssl, NULL, "--bootstrap"},
2896 /* Switch commands. */
2897 {"emer-reset", 0, 0, cmd_emer_reset, NULL, ""},
2899 /* Parameter commands. */
2900 {"get", 3, INT_MAX, cmd_get, NULL, "--if-exists"},
2901 {"list", 1, INT_MAX, cmd_list, NULL, ""},
2902 {"set", 3, INT_MAX, cmd_set, NULL, ""},
2903 {"add", 4, INT_MAX, cmd_add, NULL, ""},
2904 {"remove", 4, INT_MAX, cmd_remove, NULL, ""},
2905 {"clear", 3, INT_MAX, cmd_clear, NULL, ""},
2906 {"create", 2, INT_MAX, cmd_create, post_create, "--id="},
2907 {"destroy", 1, INT_MAX, cmd_destroy, NULL, "--if-exists"},
2908 {"wait-until", 2, INT_MAX, cmd_wait_until, NULL, ""},
2910 {NULL, 0, 0, NULL, NULL, NULL},