2 * Copyright (c) 2009, 2010, 2011 Nicira Networks.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
30 #include "command-line.h"
33 #include "dynamic-string.h"
35 #include "ovsdb-data.h"
36 #include "ovsdb-idl.h"
37 #include "poll-loop.h"
39 #include "stream-ssl.h"
41 #include "vswitchd/vswitch-idl.h"
47 VLOG_DEFINE_THIS_MODULE(vsctl);
49 /* vsctl_fatal() also logs the error, so it is preferred in this file. */
50 #define ovs_fatal please_use_vsctl_fatal_instead_of_ovs_fatal
54 /* A command supported by ovs-vsctl. */
55 struct vsctl_command_syntax {
56 const char *name; /* e.g. "add-br" */
57 int min_args; /* Min number of arguments following name. */
58 int max_args; /* Max number of arguments following name. */
60 /* If nonnull, calls ovsdb_idl_add_column() or ovsdb_idl_add_table() for
61 * each column or table in ctx->idl that it uses. */
62 void (*prerequisites)(struct vsctl_context *ctx);
64 /* Does the actual work of the command and puts the command's output, if
65 * any, in ctx->output or ctx->table.
67 * Alternatively, if some prerequisite of the command is not met and the
68 * caller should wait for something to change and then retry, it may set
69 * ctx->try_again to true. (Only the "wait-until" command currently does
71 void (*run)(struct vsctl_context *ctx);
73 /* If nonnull, called after the transaction has been successfully
74 * committed. ctx->output is the output from the "run" function, which
75 * this function may modify and otherwise postprocess as needed. (Only the
76 * "create" command currently does any postprocessing.) */
77 void (*postprocess)(struct vsctl_context *ctx);
79 /* A comma-separated list of supported options, e.g. "--a,--b", or the
80 * empty string if the command does not support any options. */
82 enum { RO, RW } mode; /* Does this command modify the database? */
85 struct vsctl_command {
86 /* Data that remains constant after initialization. */
87 const struct vsctl_command_syntax *syntax;
92 /* Data modified by commands. */
97 /* --db: The database server to contact. */
98 static const char *db;
100 /* --oneline: Write each command's output as a single line? */
103 /* --dry-run: Do not commit any changes. */
106 /* --no-wait: Wait for ovs-vswitchd to reload its configuration? */
107 static bool wait_for_reload = true;
109 /* --timeout: Time to wait for a connection to 'db'. */
112 /* Format for table output. */
113 static struct table_style table_style = TABLE_STYLE_DEFAULT;
115 /* All supported commands. */
116 static const struct vsctl_command_syntax all_commands[];
118 /* The IDL we're using and the current transaction, if any.
119 * This is for use by vsctl_exit() only, to allow it to clean up.
120 * Other code should use its context arguments. */
121 static struct ovsdb_idl *the_idl;
122 static struct ovsdb_idl_txn *the_idl_txn;
124 static void vsctl_exit(int status) NO_RETURN;
125 static void vsctl_fatal(const char *, ...) PRINTF_FORMAT(1, 2) NO_RETURN;
126 static char *default_db(void);
127 static void usage(void) NO_RETURN;
128 static void parse_options(int argc, char *argv[]);
129 static bool might_write_to_db(char **argv);
131 static struct vsctl_command *parse_commands(int argc, char *argv[],
132 size_t *n_commandsp);
133 static void parse_command(int argc, char *argv[], struct vsctl_command *);
134 static const struct vsctl_command_syntax *find_command(const char *name);
135 static void run_prerequisites(struct vsctl_command[], size_t n_commands,
137 static void do_vsctl(const char *args,
138 struct vsctl_command *, size_t n_commands,
141 static const struct vsctl_table_class *get_table(const char *table_name);
142 static void set_column(const struct vsctl_table_class *,
143 const struct ovsdb_idl_row *, const char *arg,
144 struct ovsdb_symbol_table *);
146 static bool is_condition_satisfied(const struct vsctl_table_class *,
147 const struct ovsdb_idl_row *,
149 struct ovsdb_symbol_table *);
152 main(int argc, char *argv[])
154 extern struct vlog_module VLM_reconnect;
155 struct ovsdb_idl *idl;
156 struct vsctl_command *commands;
160 set_program_name(argv[0]);
161 signal(SIGPIPE, SIG_IGN);
162 vlog_set_levels(NULL, VLF_CONSOLE, VLL_WARN);
163 vlog_set_levels(&VLM_reconnect, VLF_ANY_FACILITY, VLL_WARN);
166 /* Log our arguments. This is often valuable for debugging systems. */
167 args = process_escape_args(argv);
168 VLOG(might_write_to_db(argv) ? VLL_INFO : VLL_DBG, "Called as %s", args);
170 /* Parse command line. */
171 parse_options(argc, argv);
172 commands = parse_commands(argc - optind, argv + optind, &n_commands);
178 /* Initialize IDL. */
179 idl = the_idl = ovsdb_idl_create(db, &ovsrec_idl_class, false);
180 run_prerequisites(commands, n_commands, idl);
182 /* Now execute the commands. */
184 if (ovsdb_idl_run(idl)) {
185 do_vsctl(args, commands, n_commands, idl);
194 parse_options(int argc, char *argv[])
197 OPT_DB = UCHAR_MAX + 1,
206 static struct option long_options[] = {
207 {"db", required_argument, 0, OPT_DB},
208 {"no-syslog", no_argument, 0, OPT_NO_SYSLOG},
209 {"no-wait", no_argument, 0, OPT_NO_WAIT},
210 {"dry-run", no_argument, 0, OPT_DRY_RUN},
211 {"oneline", no_argument, 0, OPT_ONELINE},
212 {"timeout", required_argument, 0, 't'},
213 {"help", no_argument, 0, 'h'},
214 {"version", no_argument, 0, 'V'},
218 STREAM_SSL_LONG_OPTIONS
219 {"peer-ca-cert", required_argument, 0, OPT_PEER_CA_CERT},
223 char *tmp, *short_options;
225 tmp = long_options_to_short_options(long_options);
226 short_options = xasprintf("+%s", tmp);
229 table_style.format = TF_LIST;
234 c = getopt_long(argc, argv, short_options, long_options, NULL);
249 vlog_set_levels(&VLM_vsctl, VLF_SYSLOG, VLL_WARN);
253 wait_for_reload = false;
264 OVS_PRINT_VERSION(0, 0);
268 timeout = strtoul(optarg, NULL, 10);
270 vsctl_fatal("value %s on -t or --timeout is invalid",
276 TABLE_OPTION_HANDLERS(&table_style)
279 STREAM_SSL_OPTION_HANDLERS
281 case OPT_PEER_CA_CERT:
282 stream_ssl_set_peer_ca_cert_file(optarg);
300 static struct vsctl_command *
301 parse_commands(int argc, char *argv[], size_t *n_commandsp)
303 struct vsctl_command *commands;
304 size_t n_commands, allocated_commands;
308 n_commands = allocated_commands = 0;
310 for (start = i = 0; i <= argc; i++) {
311 if (i == argc || !strcmp(argv[i], "--")) {
313 if (n_commands >= allocated_commands) {
314 struct vsctl_command *c;
316 commands = x2nrealloc(commands, &allocated_commands,
318 for (c = commands; c < &commands[n_commands]; c++) {
319 shash_moved(&c->options);
322 parse_command(i - start, &argv[start],
323 &commands[n_commands++]);
329 vsctl_fatal("missing command name (use --help for help)");
331 *n_commandsp = n_commands;
336 parse_command(int argc, char *argv[], struct vsctl_command *command)
338 const struct vsctl_command_syntax *p;
339 struct shash_node *node;
343 shash_init(&command->options);
344 for (i = 0; i < argc; i++) {
345 const char *option = argv[i];
349 if (option[0] != '-') {
353 equals = strchr(option, '=');
355 key = xmemdup0(option, equals - option);
356 value = xstrdup(equals + 1);
358 key = xstrdup(option);
362 if (shash_find(&command->options, key)) {
363 vsctl_fatal("'%s' option specified multiple times", argv[i]);
365 shash_add_nocopy(&command->options, key, value);
368 vsctl_fatal("missing command name");
371 p = find_command(argv[i]);
373 vsctl_fatal("unknown command '%s'; use --help for help", argv[i]);
376 SHASH_FOR_EACH (node, &command->options) {
377 const char *s = strstr(p->options, node->name);
378 int end = s ? s[strlen(node->name)] : EOF;
380 if (end != '=' && end != ',' && end != ' ' && end != '\0') {
381 vsctl_fatal("'%s' command has no '%s' option",
382 argv[i], node->name);
384 if ((end == '=') != (node->data != NULL)) {
386 vsctl_fatal("missing argument to '%s' option on '%s' "
387 "command", node->name, argv[i]);
389 vsctl_fatal("'%s' option on '%s' does not accept an "
390 "argument", node->name, argv[i]);
395 n_arg = argc - i - 1;
396 if (n_arg < p->min_args) {
397 vsctl_fatal("'%s' command requires at least %d arguments",
398 p->name, p->min_args);
399 } else if (n_arg > p->max_args) {
402 for (j = i + 1; j < argc; j++) {
403 if (argv[j][0] == '-') {
404 vsctl_fatal("'%s' command takes at most %d arguments "
405 "(note that options must precede command "
406 "names and follow a \"--\" argument)",
407 p->name, p->max_args);
411 vsctl_fatal("'%s' command takes at most %d arguments",
412 p->name, p->max_args);
416 command->argc = n_arg + 1;
417 command->argv = &argv[i];
420 /* Returns the "struct vsctl_command_syntax" for a given command 'name', or a
421 * null pointer if there is none. */
422 static const struct vsctl_command_syntax *
423 find_command(const char *name)
425 static struct shash commands = SHASH_INITIALIZER(&commands);
427 if (shash_is_empty(&commands)) {
428 const struct vsctl_command_syntax *p;
430 for (p = all_commands; p->name; p++) {
431 shash_add_assert(&commands, p->name, p);
435 return shash_find_data(&commands, name);
439 vsctl_fatal(const char *format, ...)
444 va_start(args, format);
445 message = xvasprintf(format, args);
448 vlog_set_levels(&VLM_vsctl, VLF_CONSOLE, VLL_EMER);
449 VLOG_ERR("%s", message);
450 ovs_error(0, "%s", message);
451 vsctl_exit(EXIT_FAILURE);
454 /* Frees the current transaction and the underlying IDL and then calls
457 * Freeing the transaction and the IDL is not strictly necessary, but it makes
458 * for a clean memory leak report from valgrind in the normal case. That makes
459 * it easier to notice real memory leaks. */
461 vsctl_exit(int status)
464 ovsdb_idl_txn_abort(the_idl_txn);
465 ovsdb_idl_txn_destroy(the_idl_txn);
467 ovsdb_idl_destroy(the_idl);
475 %s: ovs-vswitchd management utility\n\
476 usage: %s [OPTIONS] COMMAND [ARG...]\n\
479 add-br BRIDGE create a new bridge named BRIDGE\n\
480 add-br BRIDGE PARENT VLAN create new fake BRIDGE in PARENT on VLAN\n\
481 del-br BRIDGE delete BRIDGE and all of its ports\n\
482 list-br print the names of all the bridges\n\
483 br-exists BRIDGE test whether BRIDGE exists\n\
484 br-to-vlan BRIDGE print the VLAN which BRIDGE is on\n\
485 br-to-parent BRIDGE print the parent of BRIDGE\n\
486 br-set-external-id BRIDGE KEY VALUE set KEY on BRIDGE to VALUE\n\
487 br-set-external-id BRIDGE KEY unset KEY on BRIDGE\n\
488 br-get-external-id BRIDGE KEY print value of KEY on BRIDGE\n\
489 br-get-external-id BRIDGE list key-value pairs on BRIDGE\n\
492 list-ports BRIDGE print the names of all the ports on BRIDGE\n\
493 add-port BRIDGE PORT add network device PORT to BRIDGE\n\
494 add-bond BRIDGE PORT IFACE... add bonded port PORT in BRIDGE from IFACES\n\
495 del-port [BRIDGE] PORT delete PORT (which may be bonded) from BRIDGE\n\
496 port-to-br PORT print name of bridge that contains PORT\n\
497 A bond is considered to be a single port.\n\
499 Interface commands (a bond consists of multiple interfaces):\n\
500 list-ifaces BRIDGE print the names of all interfaces on BRIDGE\n\
501 iface-to-br IFACE print name of bridge that contains IFACE\n\
503 Controller commands:\n\
504 get-controller BRIDGE print the controller for BRIDGE\n\
505 del-controller BRIDGE delete the controller for BRIDGE\n\
506 set-controller BRIDGE TARGET set the controller for BRIDGE to TARGET\n\
507 get-fail-mode BRIDGE print the fail-mode for BRIDGE\n\
508 del-fail-mode BRIDGE delete the fail-mode for BRIDGE\n\
509 set-fail-mode BRIDGE MODE set the fail-mode for BRIDGE to MODE\n\
512 get-manager print all manager(s)\n\
513 del-manager delete all manager(s)\n\
514 set-manager TARGET... set the list of manager(s) to TARGET(s)\n\
517 get-ssl print the SSL configuration\n\
518 del-ssl delete the SSL configuration\n\
519 set-ssl PRIV-KEY CERT CA-CERT set the SSL configuration\n\
522 emer-reset reset switch to known good state\n\
524 Database commands:\n\
525 list TBL [REC] list RECord (or all records) in TBL\n\
526 find TBL CONDITION... list records satisfying CONDITION in TBL\n\
527 get TBL REC COL[:KEY] print values of COLumns in RECord in TBL\n\
528 set TBL REC COL[:KEY]=VALUE set COLumn values in RECord in TBL\n\
529 add TBL REC COL [KEY=]VALUE add (KEY=)VALUE to COLumn in RECord in TBL\n\
530 remove TBL REC COL [KEY=]VALUE remove (KEY=)VALUE from COLumn\n\
531 clear TBL REC COL clear values from COLumn in RECord in TBL\n\
532 create TBL COL[:KEY]=VALUE create and initialize new record\n\
533 destroy TBL REC delete RECord from TBL\n\
534 wait-until TBL REC [COL[:KEY]=VALUE] wait until condition is true\n\
535 Potentially unsafe database commands require --force option.\n\
538 --db=DATABASE connect to DATABASE\n\
540 --oneline print exactly one line of output per command\n",
541 program_name, program_name, default_db());
545 -h, --help display this help message\n\
546 -V, --version display version information\n");
555 def = xasprintf("unix:%s/db.sock", ovs_rundir());
560 /* Returns true if it looks like this set of arguments might modify the
561 * database, otherwise false. (Not very smart, so it's prone to false
564 might_write_to_db(char **argv)
566 for (; *argv; argv++) {
567 const struct vsctl_command_syntax *p = find_command(*argv);
568 if (p && p->mode == RW) {
575 struct vsctl_context {
579 struct shash options;
581 /* Modifiable state. */
584 struct ovsdb_idl *idl;
585 struct ovsdb_idl_txn *txn;
586 struct ovsdb_symbol_table *symtab;
587 const struct ovsrec_open_vswitch *ovs;
590 /* A command may set this member to true if some prerequisite is not met
591 * and the caller should wait for something to change and then retry. */
595 struct vsctl_bridge {
596 struct ovsrec_bridge *br_cfg;
598 struct ovsrec_controller **ctrl;
601 struct vsctl_bridge *parent;
606 struct ovsrec_port *port_cfg;
607 struct vsctl_bridge *bridge;
611 struct ovsrec_interface *iface_cfg;
612 struct vsctl_port *port;
616 struct vsctl_context *ctx;
617 struct shash bridges; /* Maps from bridge name to struct vsctl_bridge. */
618 struct shash ports; /* Maps from port name to struct vsctl_port. */
619 struct shash ifaces; /* Maps from port name to struct vsctl_iface. */
623 vsctl_context_to_string(const struct vsctl_context *ctx)
625 const struct shash_node *node;
631 SHASH_FOR_EACH (node, &ctx->options) {
632 svec_add(&words, node->name);
634 for (i = 0; i < ctx->argc; i++) {
635 svec_add(&words, ctx->argv[i]);
637 svec_terminate(&words);
639 s = process_escape_args(words.names);
641 svec_destroy(&words);
647 verify_ports(struct vsctl_context *ctx)
649 if (!ctx->verified_ports) {
650 const struct ovsrec_bridge *bridge;
651 const struct ovsrec_port *port;
653 ovsrec_open_vswitch_verify_bridges(ctx->ovs);
654 OVSREC_BRIDGE_FOR_EACH (bridge, ctx->idl) {
655 ovsrec_bridge_verify_ports(bridge);
657 OVSREC_PORT_FOR_EACH (port, ctx->idl) {
658 ovsrec_port_verify_interfaces(port);
661 ctx->verified_ports = true;
665 static struct vsctl_bridge *
666 add_bridge(struct vsctl_info *b,
667 struct ovsrec_bridge *br_cfg, const char *name,
668 struct vsctl_bridge *parent, int vlan)
670 struct vsctl_bridge *br = xmalloc(sizeof *br);
672 br->name = xstrdup(name);
676 br->ctrl = parent->br_cfg->controller;
677 br->n_ctrl = parent->br_cfg->n_controller;
678 br->fail_mode = parent->br_cfg->fail_mode;
680 br->ctrl = br_cfg->controller;
681 br->n_ctrl = br_cfg->n_controller;
682 br->fail_mode = br_cfg->fail_mode;
684 shash_add(&b->bridges, br->name, br);
689 port_is_fake_bridge(const struct ovsrec_port *port_cfg)
691 return (port_cfg->fake_bridge
693 && *port_cfg->tag >= 1 && *port_cfg->tag <= 4095);
696 static struct vsctl_bridge *
697 find_vlan_bridge(struct vsctl_info *info,
698 struct vsctl_bridge *parent, int vlan)
700 struct shash_node *node;
702 SHASH_FOR_EACH (node, &info->bridges) {
703 struct vsctl_bridge *br = node->data;
704 if (br->parent == parent && br->vlan == vlan) {
713 free_info(struct vsctl_info *info)
715 struct shash_node *node;
717 SHASH_FOR_EACH (node, &info->bridges) {
718 struct vsctl_bridge *bridge = node->data;
722 shash_destroy(&info->bridges);
724 shash_destroy_free_data(&info->ports);
725 shash_destroy_free_data(&info->ifaces);
729 pre_get_info(struct vsctl_context *ctx)
731 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_bridges);
733 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_name);
734 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_controller);
735 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_fail_mode);
736 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_ports);
738 ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_name);
739 ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_fake_bridge);
740 ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_tag);
741 ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_interfaces);
743 ovsdb_idl_add_column(ctx->idl, &ovsrec_interface_col_name);
747 get_info(struct vsctl_context *ctx, struct vsctl_info *info)
749 const struct ovsrec_open_vswitch *ovs = ctx->ovs;
750 struct shash bridges, ports;
754 shash_init(&info->bridges);
755 shash_init(&info->ports);
756 shash_init(&info->ifaces);
758 shash_init(&bridges);
760 for (i = 0; i < ovs->n_bridges; i++) {
761 struct ovsrec_bridge *br_cfg = ovs->bridges[i];
762 struct vsctl_bridge *br;
765 if (!shash_add_once(&bridges, br_cfg->name, NULL)) {
766 VLOG_WARN("%s: database contains duplicate bridge name",
770 br = add_bridge(info, br_cfg, br_cfg->name, NULL, 0);
775 for (j = 0; j < br_cfg->n_ports; j++) {
776 struct ovsrec_port *port_cfg = br_cfg->ports[j];
778 if (!shash_add_once(&ports, port_cfg->name, NULL)) {
779 VLOG_WARN("%s: database contains duplicate port name",
784 if (port_is_fake_bridge(port_cfg)
785 && shash_add_once(&bridges, port_cfg->name, NULL)) {
786 add_bridge(info, NULL, port_cfg->name, br, *port_cfg->tag);
790 shash_destroy(&bridges);
791 shash_destroy(&ports);
793 shash_init(&bridges);
795 for (i = 0; i < ovs->n_bridges; i++) {
796 struct ovsrec_bridge *br_cfg = ovs->bridges[i];
797 struct vsctl_bridge *br;
800 if (!shash_add_once(&bridges, br_cfg->name, NULL)) {
803 br = shash_find_data(&info->bridges, br_cfg->name);
804 for (j = 0; j < br_cfg->n_ports; j++) {
805 struct ovsrec_port *port_cfg = br_cfg->ports[j];
806 struct vsctl_port *port;
809 if (!shash_add_once(&ports, port_cfg->name, NULL)) {
813 if (port_is_fake_bridge(port_cfg)
814 && !shash_add_once(&bridges, port_cfg->name, NULL)) {
818 port = xmalloc(sizeof *port);
819 port->port_cfg = port_cfg;
821 && *port_cfg->tag >= 1 && *port_cfg->tag <= 4095) {
822 port->bridge = find_vlan_bridge(info, br, *port_cfg->tag);
829 shash_add(&info->ports, port_cfg->name, port);
831 for (k = 0; k < port_cfg->n_interfaces; k++) {
832 struct ovsrec_interface *iface_cfg = port_cfg->interfaces[k];
833 struct vsctl_iface *iface;
835 if (shash_find(&info->ifaces, iface_cfg->name)) {
836 VLOG_WARN("%s: database contains duplicate interface name",
841 iface = xmalloc(sizeof *iface);
842 iface->iface_cfg = iface_cfg;
844 shash_add(&info->ifaces, iface_cfg->name, iface);
848 shash_destroy(&bridges);
849 shash_destroy(&ports);
853 check_conflicts(struct vsctl_info *info, const char *name,
856 struct vsctl_iface *iface;
857 struct vsctl_port *port;
859 verify_ports(info->ctx);
861 if (shash_find(&info->bridges, name)) {
862 vsctl_fatal("%s because a bridge named %s already exists",
866 port = shash_find_data(&info->ports, name);
868 vsctl_fatal("%s because a port named %s already exists on "
869 "bridge %s", msg, name, port->bridge->name);
872 iface = shash_find_data(&info->ifaces, name);
874 vsctl_fatal("%s because an interface named %s already exists "
875 "on bridge %s", msg, name, iface->port->bridge->name);
881 static struct vsctl_bridge *
882 find_bridge(struct vsctl_info *info, const char *name, bool must_exist)
884 struct vsctl_bridge *br = shash_find_data(&info->bridges, name);
885 if (must_exist && !br) {
886 vsctl_fatal("no bridge named %s", name);
888 ovsrec_open_vswitch_verify_bridges(info->ctx->ovs);
892 static struct vsctl_bridge *
893 find_real_bridge(struct vsctl_info *info, const char *name, bool must_exist)
895 struct vsctl_bridge *br = find_bridge(info, name, must_exist);
896 if (br && br->parent) {
897 vsctl_fatal("%s is a fake bridge", name);
902 static struct vsctl_port *
903 find_port(struct vsctl_info *info, const char *name, bool must_exist)
905 struct vsctl_port *port = shash_find_data(&info->ports, name);
906 if (port && !strcmp(name, port->bridge->name)) {
909 if (must_exist && !port) {
910 vsctl_fatal("no port named %s", name);
912 verify_ports(info->ctx);
916 static struct vsctl_iface *
917 find_iface(struct vsctl_info *info, const char *name, bool must_exist)
919 struct vsctl_iface *iface = shash_find_data(&info->ifaces, name);
920 if (iface && !strcmp(name, iface->port->bridge->name)) {
923 if (must_exist && !iface) {
924 vsctl_fatal("no interface named %s", name);
926 verify_ports(info->ctx);
931 bridge_insert_port(struct ovsrec_bridge *br, struct ovsrec_port *port)
933 struct ovsrec_port **ports;
936 ports = xmalloc(sizeof *br->ports * (br->n_ports + 1));
937 for (i = 0; i < br->n_ports; i++) {
938 ports[i] = br->ports[i];
940 ports[br->n_ports] = port;
941 ovsrec_bridge_set_ports(br, ports, br->n_ports + 1);
946 bridge_delete_port(struct ovsrec_bridge *br, struct ovsrec_port *port)
948 struct ovsrec_port **ports;
951 ports = xmalloc(sizeof *br->ports * br->n_ports);
952 for (i = n = 0; i < br->n_ports; i++) {
953 if (br->ports[i] != port) {
954 ports[n++] = br->ports[i];
957 ovsrec_bridge_set_ports(br, ports, n);
962 ovs_insert_bridge(const struct ovsrec_open_vswitch *ovs,
963 struct ovsrec_bridge *bridge)
965 struct ovsrec_bridge **bridges;
968 bridges = xmalloc(sizeof *ovs->bridges * (ovs->n_bridges + 1));
969 for (i = 0; i < ovs->n_bridges; i++) {
970 bridges[i] = ovs->bridges[i];
972 bridges[ovs->n_bridges] = bridge;
973 ovsrec_open_vswitch_set_bridges(ovs, bridges, ovs->n_bridges + 1);
978 ovs_delete_bridge(const struct ovsrec_open_vswitch *ovs,
979 struct ovsrec_bridge *bridge)
981 struct ovsrec_bridge **bridges;
984 bridges = xmalloc(sizeof *ovs->bridges * ovs->n_bridges);
985 for (i = n = 0; i < ovs->n_bridges; i++) {
986 if (ovs->bridges[i] != bridge) {
987 bridges[n++] = ovs->bridges[i];
990 ovsrec_open_vswitch_set_bridges(ovs, bridges, n);
995 cmd_init(struct vsctl_context *ctx OVS_UNUSED)
1000 pre_cmd_emer_reset(struct vsctl_context *ctx)
1002 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_managers);
1003 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_manager_options);
1004 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_ssl);
1006 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_controller);
1007 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_fail_mode);
1008 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_mirrors);
1009 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_netflow);
1010 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_sflow);
1011 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_flood_vlans);
1012 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_other_config);
1014 ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_other_config);
1016 ovsdb_idl_add_column(ctx->idl,
1017 &ovsrec_interface_col_ingress_policing_rate);
1018 ovsdb_idl_add_column(ctx->idl,
1019 &ovsrec_interface_col_ingress_policing_burst);
1023 cmd_emer_reset(struct vsctl_context *ctx)
1025 const struct ovsdb_idl *idl = ctx->idl;
1026 const struct ovsrec_bridge *br;
1027 const struct ovsrec_port *port;
1028 const struct ovsrec_interface *iface;
1029 const struct ovsrec_mirror *mirror, *next_mirror;
1030 const struct ovsrec_controller *ctrl, *next_ctrl;
1031 const struct ovsrec_manager *mgr, *next_mgr;
1032 const struct ovsrec_netflow *nf, *next_nf;
1033 const struct ovsrec_ssl *ssl, *next_ssl;
1034 const struct ovsrec_sflow *sflow, *next_sflow;
1036 /* Reset the Open_vSwitch table. */
1037 ovsrec_open_vswitch_set_managers(ctx->ovs, NULL, 0);
1038 ovsrec_open_vswitch_set_manager_options(ctx->ovs, NULL, 0);
1039 ovsrec_open_vswitch_set_ssl(ctx->ovs, NULL);
1041 OVSREC_BRIDGE_FOR_EACH (br, idl) {
1043 char *hw_key = "hwaddr";
1044 char *hw_val = NULL;
1046 ovsrec_bridge_set_controller(br, NULL, 0);
1047 ovsrec_bridge_set_fail_mode(br, NULL);
1048 ovsrec_bridge_set_mirrors(br, NULL, 0);
1049 ovsrec_bridge_set_netflow(br, NULL);
1050 ovsrec_bridge_set_sflow(br, NULL);
1051 ovsrec_bridge_set_flood_vlans(br, NULL, 0);
1053 /* We only want to save the "hwaddr" key from other_config. */
1054 for (i=0; i < br->n_other_config; i++) {
1055 if (!strcmp(br->key_other_config[i], hw_key)) {
1056 hw_val = br->value_other_config[i];
1061 char *val = xstrdup(hw_val);
1062 ovsrec_bridge_set_other_config(br, &hw_key, &val, 1);
1065 ovsrec_bridge_set_other_config(br, NULL, NULL, 0);
1069 OVSREC_PORT_FOR_EACH (port, idl) {
1070 ovsrec_port_set_other_config(port, NULL, NULL, 0);
1073 OVSREC_INTERFACE_FOR_EACH (iface, idl) {
1074 /* xxx What do we do about gre/patch devices created by mgr? */
1076 ovsrec_interface_set_ingress_policing_rate(iface, 0);
1077 ovsrec_interface_set_ingress_policing_burst(iface, 0);
1080 OVSREC_MIRROR_FOR_EACH_SAFE (mirror, next_mirror, idl) {
1081 ovsrec_mirror_delete(mirror);
1084 OVSREC_CONTROLLER_FOR_EACH_SAFE (ctrl, next_ctrl, idl) {
1085 ovsrec_controller_delete(ctrl);
1088 OVSREC_MANAGER_FOR_EACH_SAFE (mgr, next_mgr, idl) {
1089 ovsrec_manager_delete(mgr);
1092 OVSREC_NETFLOW_FOR_EACH_SAFE (nf, next_nf, idl) {
1093 ovsrec_netflow_delete(nf);
1096 OVSREC_SSL_FOR_EACH_SAFE (ssl, next_ssl, idl) {
1097 ovsrec_ssl_delete(ssl);
1100 OVSREC_SFLOW_FOR_EACH_SAFE (sflow, next_sflow, idl) {
1101 ovsrec_sflow_delete(sflow);
1106 cmd_add_br(struct vsctl_context *ctx)
1108 bool may_exist = shash_find(&ctx->options, "--may-exist") != 0;
1109 const char *br_name, *parent_name;
1110 struct vsctl_info info;
1113 br_name = ctx->argv[1];
1114 if (ctx->argc == 2) {
1117 } else if (ctx->argc == 4) {
1118 parent_name = ctx->argv[2];
1119 vlan = atoi(ctx->argv[3]);
1120 if (vlan < 1 || vlan > 4095) {
1121 vsctl_fatal("%s: vlan must be between 1 and 4095", ctx->argv[0]);
1124 vsctl_fatal("'%s' command takes exactly 1 or 3 arguments",
1128 get_info(ctx, &info);
1130 struct vsctl_bridge *br;
1132 br = find_bridge(&info, br_name, false);
1136 vsctl_fatal("\"--may-exist add-br %s\" but %s is "
1137 "a VLAN bridge for VLAN %d",
1138 br_name, br_name, br->vlan);
1142 vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
1143 "is not a VLAN bridge",
1144 br_name, parent_name, vlan, br_name);
1145 } else if (strcmp(br->parent->name, parent_name)) {
1146 vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
1147 "has the wrong parent %s",
1148 br_name, parent_name, vlan,
1149 br_name, br->parent->name);
1150 } else if (br->vlan != vlan) {
1151 vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
1152 "is a VLAN bridge for the wrong VLAN %d",
1153 br_name, parent_name, vlan, br_name, br->vlan);
1159 check_conflicts(&info, br_name,
1160 xasprintf("cannot create a bridge named %s", br_name));
1163 struct ovsrec_port *port;
1164 struct ovsrec_interface *iface;
1165 struct ovsrec_bridge *br;
1167 iface = ovsrec_interface_insert(ctx->txn);
1168 ovsrec_interface_set_name(iface, br_name);
1169 ovsrec_interface_set_type(iface, "internal");
1171 port = ovsrec_port_insert(ctx->txn);
1172 ovsrec_port_set_name(port, br_name);
1173 ovsrec_port_set_interfaces(port, &iface, 1);
1175 br = ovsrec_bridge_insert(ctx->txn);
1176 ovsrec_bridge_set_name(br, br_name);
1177 ovsrec_bridge_set_ports(br, &port, 1);
1179 ovs_insert_bridge(ctx->ovs, br);
1181 struct vsctl_bridge *parent;
1182 struct ovsrec_port *port;
1183 struct ovsrec_interface *iface;
1184 struct ovsrec_bridge *br;
1187 parent = find_bridge(&info, parent_name, false);
1188 if (parent && parent->vlan) {
1189 vsctl_fatal("cannot create bridge with fake bridge as parent");
1192 vsctl_fatal("parent bridge %s does not exist", parent_name);
1194 br = parent->br_cfg;
1196 iface = ovsrec_interface_insert(ctx->txn);
1197 ovsrec_interface_set_name(iface, br_name);
1198 ovsrec_interface_set_type(iface, "internal");
1200 port = ovsrec_port_insert(ctx->txn);
1201 ovsrec_port_set_name(port, br_name);
1202 ovsrec_port_set_interfaces(port, &iface, 1);
1203 ovsrec_port_set_fake_bridge(port, true);
1204 ovsrec_port_set_tag(port, &tag, 1);
1206 bridge_insert_port(br, port);
1213 del_port(struct vsctl_info *info, struct vsctl_port *port)
1215 struct shash_node *node;
1217 SHASH_FOR_EACH (node, &info->ifaces) {
1218 struct vsctl_iface *iface = node->data;
1219 if (iface->port == port) {
1220 ovsrec_interface_delete(iface->iface_cfg);
1223 ovsrec_port_delete(port->port_cfg);
1225 bridge_delete_port((port->bridge->parent
1226 ? port->bridge->parent->br_cfg
1227 : port->bridge->br_cfg), port->port_cfg);
1231 cmd_del_br(struct vsctl_context *ctx)
1233 bool must_exist = !shash_find(&ctx->options, "--if-exists");
1234 struct vsctl_bridge *bridge;
1235 struct vsctl_info info;
1237 get_info(ctx, &info);
1238 bridge = find_bridge(&info, ctx->argv[1], must_exist);
1240 struct shash_node *node;
1242 SHASH_FOR_EACH (node, &info.ports) {
1243 struct vsctl_port *port = node->data;
1244 if (port->bridge == bridge || port->bridge->parent == bridge
1245 || !strcmp(port->port_cfg->name, bridge->name)) {
1246 del_port(&info, port);
1249 if (bridge->br_cfg) {
1250 ovsrec_bridge_delete(bridge->br_cfg);
1251 ovs_delete_bridge(ctx->ovs, bridge->br_cfg);
1258 output_sorted(struct svec *svec, struct ds *output)
1264 SVEC_FOR_EACH (i, name, svec) {
1265 ds_put_format(output, "%s\n", name);
1270 cmd_list_br(struct vsctl_context *ctx)
1272 struct shash_node *node;
1273 struct vsctl_info info;
1274 struct svec bridges;
1276 get_info(ctx, &info);
1278 svec_init(&bridges);
1279 SHASH_FOR_EACH (node, &info.bridges) {
1280 struct vsctl_bridge *br = node->data;
1281 svec_add(&bridges, br->name);
1283 output_sorted(&bridges, &ctx->output);
1284 svec_destroy(&bridges);
1290 cmd_br_exists(struct vsctl_context *ctx)
1292 struct vsctl_info info;
1294 get_info(ctx, &info);
1295 if (!find_bridge(&info, ctx->argv[1], false)) {
1301 /* Returns true if 'b_prefix' (of length 'b_prefix_len') concatenated with 'b'
1302 * equals 'a', false otherwise. */
1304 key_matches(const char *a,
1305 const char *b_prefix, size_t b_prefix_len, const char *b)
1307 return !strncmp(a, b_prefix, b_prefix_len) && !strcmp(a + b_prefix_len, b);
1311 set_external_id(char **old_keys, char **old_values, size_t old_n,
1312 char *key, char *value,
1313 char ***new_keysp, char ***new_valuesp, size_t *new_np)
1320 new_keys = xmalloc(sizeof *new_keys * (old_n + 1));
1321 new_values = xmalloc(sizeof *new_values * (old_n + 1));
1323 for (i = 0; i < old_n; i++) {
1324 if (strcmp(key, old_keys[i])) {
1325 new_keys[new_n] = old_keys[i];
1326 new_values[new_n] = old_values[i];
1331 new_keys[new_n] = key;
1332 new_values[new_n] = value;
1335 *new_keysp = new_keys;
1336 *new_valuesp = new_values;
1341 pre_cmd_br_set_external_id(struct vsctl_context *ctx)
1344 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_external_ids);
1345 ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_external_ids);
1349 cmd_br_set_external_id(struct vsctl_context *ctx)
1351 struct vsctl_info info;
1352 struct vsctl_bridge *bridge;
1353 char **keys, **values;
1356 get_info(ctx, &info);
1357 bridge = find_bridge(&info, ctx->argv[1], true);
1358 if (bridge->br_cfg) {
1359 set_external_id(bridge->br_cfg->key_external_ids,
1360 bridge->br_cfg->value_external_ids,
1361 bridge->br_cfg->n_external_ids,
1362 ctx->argv[2], ctx->argc >= 4 ? ctx->argv[3] : NULL,
1363 &keys, &values, &n);
1364 ovsrec_bridge_verify_external_ids(bridge->br_cfg);
1365 ovsrec_bridge_set_external_ids(bridge->br_cfg, keys, values, n);
1367 char *key = xasprintf("fake-bridge-%s", ctx->argv[2]);
1368 struct vsctl_port *port = shash_find_data(&info.ports, ctx->argv[1]);
1369 set_external_id(port->port_cfg->key_external_ids,
1370 port->port_cfg->value_external_ids,
1371 port->port_cfg->n_external_ids,
1372 key, ctx->argc >= 4 ? ctx->argv[3] : NULL,
1373 &keys, &values, &n);
1374 ovsrec_port_verify_external_ids(port->port_cfg);
1375 ovsrec_port_set_external_ids(port->port_cfg, keys, values, n);
1385 get_external_id(char **keys, char **values, size_t n,
1386 const char *prefix, const char *key,
1389 size_t prefix_len = strlen(prefix);
1394 for (i = 0; i < n; i++) {
1395 if (!key && !strncmp(keys[i], prefix, prefix_len)) {
1396 svec_add_nocopy(&svec, xasprintf("%s=%s",
1397 keys[i] + prefix_len, values[i]));
1398 } else if (key_matches(keys[i], prefix, prefix_len, key)) {
1399 svec_add(&svec, values[i]);
1403 output_sorted(&svec, output);
1404 svec_destroy(&svec);
1408 pre_cmd_br_get_external_id(struct vsctl_context *ctx)
1410 pre_cmd_br_set_external_id(ctx);
1414 cmd_br_get_external_id(struct vsctl_context *ctx)
1416 struct vsctl_info info;
1417 struct vsctl_bridge *bridge;
1419 get_info(ctx, &info);
1420 bridge = find_bridge(&info, ctx->argv[1], true);
1421 if (bridge->br_cfg) {
1422 ovsrec_bridge_verify_external_ids(bridge->br_cfg);
1423 get_external_id(bridge->br_cfg->key_external_ids,
1424 bridge->br_cfg->value_external_ids,
1425 bridge->br_cfg->n_external_ids,
1426 "", ctx->argc >= 3 ? ctx->argv[2] : NULL,
1429 struct vsctl_port *port = shash_find_data(&info.ports, ctx->argv[1]);
1430 ovsrec_port_verify_external_ids(port->port_cfg);
1431 get_external_id(port->port_cfg->key_external_ids,
1432 port->port_cfg->value_external_ids,
1433 port->port_cfg->n_external_ids,
1434 "fake-bridge-", ctx->argc >= 3 ? ctx->argv[2] : NULL, &ctx->output);
1441 cmd_list_ports(struct vsctl_context *ctx)
1443 struct vsctl_bridge *br;
1444 struct shash_node *node;
1445 struct vsctl_info info;
1448 get_info(ctx, &info);
1449 br = find_bridge(&info, ctx->argv[1], true);
1450 ovsrec_bridge_verify_ports(br->br_cfg ? br->br_cfg : br->parent->br_cfg);
1453 SHASH_FOR_EACH (node, &info.ports) {
1454 struct vsctl_port *port = node->data;
1456 if (strcmp(port->port_cfg->name, br->name) && br == port->bridge) {
1457 svec_add(&ports, port->port_cfg->name);
1460 output_sorted(&ports, &ctx->output);
1461 svec_destroy(&ports);
1467 add_port(struct vsctl_context *ctx,
1468 const char *br_name, const char *port_name,
1469 bool may_exist, bool fake_iface,
1470 char *iface_names[], int n_ifaces,
1471 char *settings[], int n_settings)
1473 struct vsctl_info info;
1474 struct vsctl_bridge *bridge;
1475 struct ovsrec_interface **ifaces;
1476 struct ovsrec_port *port;
1479 get_info(ctx, &info);
1481 struct vsctl_port *vsctl_port;
1483 vsctl_port = find_port(&info, port_name, false);
1485 struct svec want_names, have_names;
1487 svec_init(&want_names);
1488 for (i = 0; i < n_ifaces; i++) {
1489 svec_add(&want_names, iface_names[i]);
1491 svec_sort(&want_names);
1493 svec_init(&have_names);
1494 for (i = 0; i < vsctl_port->port_cfg->n_interfaces; i++) {
1495 svec_add(&have_names,
1496 vsctl_port->port_cfg->interfaces[i]->name);
1498 svec_sort(&have_names);
1500 if (strcmp(vsctl_port->bridge->name, br_name)) {
1501 char *command = vsctl_context_to_string(ctx);
1502 vsctl_fatal("\"%s\" but %s is actually attached to bridge %s",
1503 command, port_name, vsctl_port->bridge->name);
1506 if (!svec_equal(&want_names, &have_names)) {
1507 char *have_names_string = svec_join(&have_names, ", ", "");
1508 char *command = vsctl_context_to_string(ctx);
1510 vsctl_fatal("\"%s\" but %s actually has interface(s) %s",
1511 command, port_name, have_names_string);
1514 svec_destroy(&want_names);
1515 svec_destroy(&have_names);
1520 check_conflicts(&info, port_name,
1521 xasprintf("cannot create a port named %s", port_name));
1522 for (i = 0; i < n_ifaces; i++) {
1523 check_conflicts(&info, iface_names[i],
1524 xasprintf("cannot create an interface named %s",
1527 bridge = find_bridge(&info, br_name, true);
1529 ifaces = xmalloc(n_ifaces * sizeof *ifaces);
1530 for (i = 0; i < n_ifaces; i++) {
1531 ifaces[i] = ovsrec_interface_insert(ctx->txn);
1532 ovsrec_interface_set_name(ifaces[i], iface_names[i]);
1535 port = ovsrec_port_insert(ctx->txn);
1536 ovsrec_port_set_name(port, port_name);
1537 ovsrec_port_set_interfaces(port, ifaces, n_ifaces);
1538 ovsrec_port_set_bond_fake_iface(port, fake_iface);
1542 int64_t tag = bridge->vlan;
1543 ovsrec_port_set_tag(port, &tag, 1);
1546 for (i = 0; i < n_settings; i++) {
1547 set_column(get_table("Port"), &port->header_, settings[i],
1551 bridge_insert_port((bridge->parent ? bridge->parent->br_cfg
1552 : bridge->br_cfg), port);
1558 cmd_add_port(struct vsctl_context *ctx)
1560 bool may_exist = shash_find(&ctx->options, "--may-exist") != 0;
1562 add_port(ctx, ctx->argv[1], ctx->argv[2], may_exist, false,
1563 &ctx->argv[2], 1, &ctx->argv[3], ctx->argc - 3);
1567 cmd_add_bond(struct vsctl_context *ctx)
1569 bool may_exist = shash_find(&ctx->options, "--may-exist") != 0;
1570 bool fake_iface = shash_find(&ctx->options, "--fake-iface");
1574 n_ifaces = ctx->argc - 3;
1575 for (i = 3; i < ctx->argc; i++) {
1576 if (strchr(ctx->argv[i], '=')) {
1582 vsctl_fatal("add-bond requires at least 2 interfaces, but only "
1583 "%d were specified", n_ifaces);
1586 add_port(ctx, ctx->argv[1], ctx->argv[2], may_exist, fake_iface,
1587 &ctx->argv[3], n_ifaces,
1588 &ctx->argv[n_ifaces + 3], ctx->argc - 3 - n_ifaces);
1592 cmd_del_port(struct vsctl_context *ctx)
1594 bool must_exist = !shash_find(&ctx->options, "--if-exists");
1595 bool with_iface = shash_find(&ctx->options, "--with-iface") != NULL;
1596 struct vsctl_port *port;
1597 struct vsctl_info info;
1599 get_info(ctx, &info);
1601 port = find_port(&info, ctx->argv[ctx->argc - 1], must_exist);
1603 const char *target = ctx->argv[ctx->argc - 1];
1604 struct vsctl_iface *iface;
1606 port = find_port(&info, target, false);
1608 iface = find_iface(&info, target, false);
1613 if (must_exist && !port) {
1614 vsctl_fatal("no port or interface named %s", target);
1619 if (ctx->argc == 3) {
1620 struct vsctl_bridge *bridge;
1622 bridge = find_bridge(&info, ctx->argv[1], true);
1623 if (port->bridge != bridge) {
1624 if (port->bridge->parent == bridge) {
1625 vsctl_fatal("bridge %s does not have a port %s (although "
1626 "its parent bridge %s does)",
1627 ctx->argv[1], ctx->argv[2],
1628 bridge->parent->name);
1630 vsctl_fatal("bridge %s does not have a port %s",
1631 ctx->argv[1], ctx->argv[2]);
1636 del_port(&info, port);
1643 cmd_port_to_br(struct vsctl_context *ctx)
1645 struct vsctl_port *port;
1646 struct vsctl_info info;
1648 get_info(ctx, &info);
1649 port = find_port(&info, ctx->argv[1], true);
1650 ds_put_format(&ctx->output, "%s\n", port->bridge->name);
1655 cmd_br_to_vlan(struct vsctl_context *ctx)
1657 struct vsctl_bridge *bridge;
1658 struct vsctl_info info;
1660 get_info(ctx, &info);
1661 bridge = find_bridge(&info, ctx->argv[1], true);
1662 ds_put_format(&ctx->output, "%d\n", bridge->vlan);
1667 cmd_br_to_parent(struct vsctl_context *ctx)
1669 struct vsctl_bridge *bridge;
1670 struct vsctl_info info;
1672 get_info(ctx, &info);
1673 bridge = find_bridge(&info, ctx->argv[1], true);
1674 if (bridge->parent) {
1675 bridge = bridge->parent;
1677 ds_put_format(&ctx->output, "%s\n", bridge->name);
1682 cmd_list_ifaces(struct vsctl_context *ctx)
1684 struct vsctl_bridge *br;
1685 struct shash_node *node;
1686 struct vsctl_info info;
1689 get_info(ctx, &info);
1690 br = find_bridge(&info, ctx->argv[1], true);
1694 SHASH_FOR_EACH (node, &info.ifaces) {
1695 struct vsctl_iface *iface = node->data;
1697 if (strcmp(iface->iface_cfg->name, br->name)
1698 && br == iface->port->bridge) {
1699 svec_add(&ifaces, iface->iface_cfg->name);
1702 output_sorted(&ifaces, &ctx->output);
1703 svec_destroy(&ifaces);
1709 cmd_iface_to_br(struct vsctl_context *ctx)
1711 struct vsctl_iface *iface;
1712 struct vsctl_info info;
1714 get_info(ctx, &info);
1715 iface = find_iface(&info, ctx->argv[1], true);
1716 ds_put_format(&ctx->output, "%s\n", iface->port->bridge->name);
1721 verify_controllers(struct ovsrec_bridge *bridge)
1726 ovsrec_bridge_verify_controller(bridge);
1727 for (i = 0; i < bridge->n_controller; i++) {
1728 ovsrec_controller_verify_target(bridge->controller[i]);
1734 pre_controller(struct vsctl_context *ctx)
1738 ovsdb_idl_add_column(ctx->idl, &ovsrec_controller_col_target);
1742 cmd_get_controller(struct vsctl_context *ctx)
1744 struct vsctl_info info;
1745 struct vsctl_bridge *br;
1746 struct svec targets;
1749 get_info(ctx, &info);
1750 br = find_bridge(&info, ctx->argv[1], true);
1751 verify_controllers(br->br_cfg);
1753 /* Print the targets in sorted order for reproducibility. */
1754 svec_init(&targets);
1755 for (i = 0; i < br->n_ctrl; i++) {
1756 svec_add(&targets, br->ctrl[i]->target);
1759 svec_sort(&targets);
1760 for (i = 0; i < targets.n; i++) {
1761 ds_put_format(&ctx->output, "%s\n", targets.names[i]);
1763 svec_destroy(&targets);
1769 delete_controllers(struct ovsrec_controller **controllers,
1770 size_t n_controllers)
1774 for (i = 0; i < n_controllers; i++) {
1775 ovsrec_controller_delete(controllers[i]);
1780 cmd_del_controller(struct vsctl_context *ctx)
1782 struct vsctl_info info;
1783 struct vsctl_bridge *br;
1785 get_info(ctx, &info);
1786 br = find_real_bridge(&info, ctx->argv[1], true);
1787 verify_controllers(br->br_cfg);
1790 delete_controllers(br->ctrl, br->n_ctrl);
1791 ovsrec_bridge_set_controller(br->br_cfg, NULL, 0);
1797 static struct ovsrec_controller **
1798 insert_controllers(struct ovsdb_idl_txn *txn, char *targets[], size_t n)
1800 struct ovsrec_controller **controllers;
1803 controllers = xmalloc(n * sizeof *controllers);
1804 for (i = 0; i < n; i++) {
1805 controllers[i] = ovsrec_controller_insert(txn);
1806 ovsrec_controller_set_target(controllers[i], targets[i]);
1813 cmd_set_controller(struct vsctl_context *ctx)
1815 struct vsctl_info info;
1816 struct vsctl_bridge *br;
1817 struct ovsrec_controller **controllers;
1820 get_info(ctx, &info);
1821 br = find_real_bridge(&info, ctx->argv[1], true);
1822 verify_controllers(br->br_cfg);
1824 delete_controllers(br->ctrl, br->n_ctrl);
1827 controllers = insert_controllers(ctx->txn, &ctx->argv[2], n);
1828 ovsrec_bridge_set_controller(br->br_cfg, controllers, n);
1835 cmd_get_fail_mode(struct vsctl_context *ctx)
1837 struct vsctl_info info;
1838 struct vsctl_bridge *br;
1840 get_info(ctx, &info);
1841 br = find_bridge(&info, ctx->argv[1], true);
1844 ovsrec_bridge_verify_fail_mode(br->br_cfg);
1846 if (br->fail_mode && strlen(br->fail_mode)) {
1847 ds_put_format(&ctx->output, "%s\n", br->fail_mode);
1854 cmd_del_fail_mode(struct vsctl_context *ctx)
1856 struct vsctl_info info;
1857 struct vsctl_bridge *br;
1859 get_info(ctx, &info);
1860 br = find_real_bridge(&info, ctx->argv[1], true);
1862 ovsrec_bridge_set_fail_mode(br->br_cfg, NULL);
1868 cmd_set_fail_mode(struct vsctl_context *ctx)
1870 struct vsctl_info info;
1871 struct vsctl_bridge *br;
1872 const char *fail_mode = ctx->argv[2];
1874 get_info(ctx, &info);
1875 br = find_real_bridge(&info, ctx->argv[1], true);
1877 if (strcmp(fail_mode, "standalone") && strcmp(fail_mode, "secure")) {
1878 vsctl_fatal("fail-mode must be \"standalone\" or \"secure\"");
1881 ovsrec_bridge_set_fail_mode(br->br_cfg, fail_mode);
1887 verify_managers(const struct ovsrec_open_vswitch *ovs)
1891 ovsrec_open_vswitch_verify_managers(ovs);
1892 ovsrec_open_vswitch_verify_manager_options(ovs);
1894 for (i = 0; i < ovs->n_manager_options; ++i) {
1895 const struct ovsrec_manager *mgr = ovs->manager_options[i];
1897 ovsrec_manager_verify_target(mgr);
1902 pre_manager(struct vsctl_context *ctx)
1904 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_managers);
1905 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_manager_options);
1906 ovsdb_idl_add_column(ctx->idl, &ovsrec_manager_col_target);
1910 cmd_get_manager(struct vsctl_context *ctx)
1912 const struct ovsrec_open_vswitch *ovs = ctx->ovs;
1913 struct svec targets;
1916 verify_managers(ovs);
1918 /* Print the targets in sorted order for reproducibility. */
1919 svec_init(&targets);
1921 /* First, add all targets found in deprecated 'managers' column. */
1922 for (i = 0; i < ovs->n_managers; i++) {
1923 svec_add(&targets, ovs->managers[i]);
1926 /* Second, add all targets pointed to by 'manager_options' column. */
1927 for (i = 0; i < ovs->n_manager_options; i++) {
1928 svec_add(&targets, ovs->manager_options[i]->target);
1931 svec_sort_unique(&targets);
1932 for (i = 0; i < targets.n; i++) {
1933 ds_put_format(&ctx->output, "%s\n", targets.names[i]);
1935 svec_destroy(&targets);
1939 delete_managers(const struct vsctl_context *ctx)
1941 const struct ovsrec_open_vswitch *ovs = ctx->ovs;
1944 /* Delete manager targets in deprecated 'managers' column. */
1945 ovsrec_open_vswitch_set_managers(ovs, NULL, 0);
1947 /* Delete Manager rows pointed to by 'manager_options' column. */
1948 for (i = 0; i < ovs->n_manager_options; i++) {
1949 ovsrec_manager_delete(ovs->manager_options[i]);
1952 /* Delete 'Manager' row refs in 'manager_options' column. */
1953 ovsrec_open_vswitch_set_manager_options(ovs, NULL, 0);
1957 cmd_del_manager(struct vsctl_context *ctx)
1959 const struct ovsrec_open_vswitch *ovs = ctx->ovs;
1961 verify_managers(ovs);
1962 delete_managers(ctx);
1966 insert_managers(struct vsctl_context *ctx, char *targets[], size_t n)
1968 struct ovsrec_manager **managers;
1971 /* Store in deprecated 'manager' column. */
1972 ovsrec_open_vswitch_set_managers(ctx->ovs, targets, n);
1974 /* Insert each manager in a new row in Manager table. */
1975 managers = xmalloc(n * sizeof *managers);
1976 for (i = 0; i < n; i++) {
1977 managers[i] = ovsrec_manager_insert(ctx->txn);
1978 ovsrec_manager_set_target(managers[i], targets[i]);
1981 /* Store uuids of new Manager rows in 'manager_options' column. */
1982 ovsrec_open_vswitch_set_manager_options(ctx->ovs, managers, n);
1987 cmd_set_manager(struct vsctl_context *ctx)
1989 const size_t n = ctx->argc - 1;
1991 verify_managers(ctx->ovs);
1992 delete_managers(ctx);
1993 insert_managers(ctx, &ctx->argv[1], n);
1997 pre_cmd_get_ssl(struct vsctl_context *ctx)
1999 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_ssl);
2001 ovsdb_idl_add_column(ctx->idl, &ovsrec_ssl_col_private_key);
2002 ovsdb_idl_add_column(ctx->idl, &ovsrec_ssl_col_certificate);
2003 ovsdb_idl_add_column(ctx->idl, &ovsrec_ssl_col_ca_cert);
2004 ovsdb_idl_add_column(ctx->idl, &ovsrec_ssl_col_bootstrap_ca_cert);
2008 cmd_get_ssl(struct vsctl_context *ctx)
2010 struct ovsrec_ssl *ssl = ctx->ovs->ssl;
2012 ovsrec_open_vswitch_verify_ssl(ctx->ovs);
2014 ovsrec_ssl_verify_private_key(ssl);
2015 ovsrec_ssl_verify_certificate(ssl);
2016 ovsrec_ssl_verify_ca_cert(ssl);
2017 ovsrec_ssl_verify_bootstrap_ca_cert(ssl);
2019 ds_put_format(&ctx->output, "Private key: %s\n", ssl->private_key);
2020 ds_put_format(&ctx->output, "Certificate: %s\n", ssl->certificate);
2021 ds_put_format(&ctx->output, "CA Certificate: %s\n", ssl->ca_cert);
2022 ds_put_format(&ctx->output, "Bootstrap: %s\n",
2023 ssl->bootstrap_ca_cert ? "true" : "false");
2028 pre_cmd_del_ssl(struct vsctl_context *ctx)
2030 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_ssl);
2034 cmd_del_ssl(struct vsctl_context *ctx)
2036 struct ovsrec_ssl *ssl = ctx->ovs->ssl;
2039 ovsrec_open_vswitch_verify_ssl(ctx->ovs);
2040 ovsrec_ssl_delete(ssl);
2041 ovsrec_open_vswitch_set_ssl(ctx->ovs, NULL);
2046 pre_cmd_set_ssl(struct vsctl_context *ctx)
2048 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_ssl);
2052 cmd_set_ssl(struct vsctl_context *ctx)
2054 bool bootstrap = shash_find(&ctx->options, "--bootstrap");
2055 struct ovsrec_ssl *ssl = ctx->ovs->ssl;
2057 ovsrec_open_vswitch_verify_ssl(ctx->ovs);
2059 ovsrec_ssl_delete(ssl);
2061 ssl = ovsrec_ssl_insert(ctx->txn);
2063 ovsrec_ssl_set_private_key(ssl, ctx->argv[1]);
2064 ovsrec_ssl_set_certificate(ssl, ctx->argv[2]);
2065 ovsrec_ssl_set_ca_cert(ssl, ctx->argv[3]);
2067 ovsrec_ssl_set_bootstrap_ca_cert(ssl, bootstrap);
2069 ovsrec_open_vswitch_set_ssl(ctx->ovs, ssl);
2072 /* Parameter commands. */
2074 struct vsctl_row_id {
2075 const struct ovsdb_idl_table_class *table;
2076 const struct ovsdb_idl_column *name_column;
2077 const struct ovsdb_idl_column *uuid_column;
2080 struct vsctl_table_class {
2081 struct ovsdb_idl_table_class *class;
2082 struct vsctl_row_id row_ids[2];
2085 static const struct vsctl_table_class tables[] = {
2086 {&ovsrec_table_bridge,
2087 {{&ovsrec_table_bridge, &ovsrec_bridge_col_name, NULL},
2088 {NULL, NULL, NULL}}},
2090 {&ovsrec_table_controller,
2091 {{&ovsrec_table_bridge,
2092 &ovsrec_bridge_col_name,
2093 &ovsrec_bridge_col_controller}}},
2095 {&ovsrec_table_interface,
2096 {{&ovsrec_table_interface, &ovsrec_interface_col_name, NULL},
2097 {NULL, NULL, NULL}}},
2099 {&ovsrec_table_mirror,
2100 {{&ovsrec_table_mirror, &ovsrec_mirror_col_name, NULL},
2101 {NULL, NULL, NULL}}},
2103 {&ovsrec_table_manager,
2104 {{&ovsrec_table_manager, &ovsrec_manager_col_target, NULL},
2105 {NULL, NULL, NULL}}},
2107 {&ovsrec_table_netflow,
2108 {{&ovsrec_table_bridge,
2109 &ovsrec_bridge_col_name,
2110 &ovsrec_bridge_col_netflow},
2111 {NULL, NULL, NULL}}},
2113 {&ovsrec_table_open_vswitch,
2114 {{&ovsrec_table_open_vswitch, NULL, NULL},
2115 {NULL, NULL, NULL}}},
2117 {&ovsrec_table_port,
2118 {{&ovsrec_table_port, &ovsrec_port_col_name, NULL},
2119 {NULL, NULL, NULL}}},
2122 {{&ovsrec_table_port, &ovsrec_port_col_name, &ovsrec_port_col_qos},
2123 {NULL, NULL, NULL}}},
2125 {&ovsrec_table_monitor,
2126 {{&ovsrec_table_interface,
2127 &ovsrec_interface_col_name,
2128 &ovsrec_interface_col_monitor},
2129 {NULL, NULL, NULL}}},
2131 {&ovsrec_table_maintenance_point,
2132 {{NULL, NULL, NULL},
2133 {NULL, NULL, NULL}}},
2135 {&ovsrec_table_queue,
2136 {{NULL, NULL, NULL},
2137 {NULL, NULL, NULL}}},
2140 {{&ovsrec_table_open_vswitch, NULL, &ovsrec_open_vswitch_col_ssl}}},
2142 {&ovsrec_table_sflow,
2143 {{&ovsrec_table_bridge,
2144 &ovsrec_bridge_col_name,
2145 &ovsrec_bridge_col_sflow},
2146 {NULL, NULL, NULL}}},
2148 {NULL, {{NULL, NULL, NULL}, {NULL, NULL, NULL}}}
2152 die_if_error(char *error)
2155 vsctl_fatal("%s", error);
2160 to_lower_and_underscores(unsigned c)
2162 return c == '-' ? '_' : tolower(c);
2166 score_partial_match(const char *name, const char *s)
2170 if (!strcmp(name, s)) {
2173 for (score = 0; ; score++, name++, s++) {
2174 if (to_lower_and_underscores(*name) != to_lower_and_underscores(*s)) {
2176 } else if (*name == '\0') {
2177 return UINT_MAX - 1;
2180 return *s == '\0' ? score : 0;
2183 static const struct vsctl_table_class *
2184 get_table(const char *table_name)
2186 const struct vsctl_table_class *table;
2187 const struct vsctl_table_class *best_match = NULL;
2188 unsigned int best_score = 0;
2190 for (table = tables; table->class; table++) {
2191 unsigned int score = score_partial_match(table->class->name,
2193 if (score > best_score) {
2196 } else if (score == best_score) {
2202 } else if (best_score) {
2203 vsctl_fatal("multiple table names match \"%s\"", table_name);
2205 vsctl_fatal("unknown table \"%s\"", table_name);
2209 static const struct vsctl_table_class *
2210 pre_get_table(struct vsctl_context *ctx, const char *table_name)
2212 const struct vsctl_table_class *table_class;
2215 table_class = get_table(table_name);
2216 ovsdb_idl_add_table(ctx->idl, table_class->class);
2218 for (i = 0; i < ARRAY_SIZE(table_class->row_ids); i++) {
2219 const struct vsctl_row_id *id = &table_class->row_ids[i];
2221 ovsdb_idl_add_table(ctx->idl, id->table);
2223 if (id->name_column) {
2224 ovsdb_idl_add_column(ctx->idl, id->name_column);
2226 if (id->uuid_column) {
2227 ovsdb_idl_add_column(ctx->idl, id->uuid_column);
2234 static const struct ovsdb_idl_row *
2235 get_row_by_id(struct vsctl_context *ctx, const struct vsctl_table_class *table,
2236 const struct vsctl_row_id *id, const char *record_id)
2238 const struct ovsdb_idl_row *referrer, *final;
2244 if (!id->name_column) {
2245 if (strcmp(record_id, ".")) {
2248 referrer = ovsdb_idl_first_row(ctx->idl, id->table);
2249 if (!referrer || ovsdb_idl_next_row(referrer)) {
2253 const struct ovsdb_idl_row *row;
2256 for (row = ovsdb_idl_first_row(ctx->idl, id->table);
2258 row = ovsdb_idl_next_row(row))
2260 const struct ovsdb_datum *name;
2262 name = ovsdb_idl_get(row, id->name_column,
2263 OVSDB_TYPE_STRING, OVSDB_TYPE_VOID);
2264 if (name->n == 1 && !strcmp(name->keys[0].string, record_id)) {
2266 vsctl_fatal("multiple rows in %s match \"%s\"",
2267 table->class->name, record_id);
2278 if (id->uuid_column) {
2279 const struct ovsdb_datum *uuid;
2281 ovsdb_idl_txn_verify(referrer, id->uuid_column);
2282 uuid = ovsdb_idl_get(referrer, id->uuid_column,
2283 OVSDB_TYPE_UUID, OVSDB_TYPE_VOID);
2285 final = ovsdb_idl_get_row_for_uuid(ctx->idl, table->class,
2286 &uuid->keys[0].uuid);
2295 static const struct ovsdb_idl_row *
2296 get_row (struct vsctl_context *ctx,
2297 const struct vsctl_table_class *table, const char *record_id)
2299 const struct ovsdb_idl_row *row;
2302 if (uuid_from_string(&uuid, record_id)) {
2303 row = ovsdb_idl_get_row_for_uuid(ctx->idl, table->class, &uuid);
2307 for (i = 0; i < ARRAY_SIZE(table->row_ids); i++) {
2308 row = get_row_by_id(ctx, table, &table->row_ids[i], record_id);
2317 static const struct ovsdb_idl_row *
2318 must_get_row(struct vsctl_context *ctx,
2319 const struct vsctl_table_class *table, const char *record_id)
2321 const struct ovsdb_idl_row *row = get_row(ctx, table, record_id);
2323 vsctl_fatal("no row \"%s\" in table %s",
2324 record_id, table->class->name);
2330 get_column(const struct vsctl_table_class *table, const char *column_name,
2331 const struct ovsdb_idl_column **columnp)
2333 const struct ovsdb_idl_column *best_match = NULL;
2334 unsigned int best_score = 0;
2337 for (i = 0; i < table->class->n_columns; i++) {
2338 const struct ovsdb_idl_column *column = &table->class->columns[i];
2339 unsigned int score = score_partial_match(column->name, column_name);
2340 if (score > best_score) {
2341 best_match = column;
2343 } else if (score == best_score) {
2348 *columnp = best_match;
2351 } else if (best_score) {
2352 return xasprintf("%s contains more than one column whose name "
2353 "matches \"%s\"", table->class->name, column_name);
2355 return xasprintf("%s does not contain a column whose name matches "
2356 "\"%s\"", table->class->name, column_name);
2360 static struct uuid *
2361 create_symbol(struct ovsdb_symbol_table *symtab, const char *id, bool *newp)
2363 struct ovsdb_symbol *symbol;
2366 vsctl_fatal("row id \"%s\" does not begin with \"@\"", id);
2370 *newp = ovsdb_symbol_table_get(symtab, id) == NULL;
2373 symbol = ovsdb_symbol_table_insert(symtab, id);
2375 vsctl_fatal("row id \"%s\" may only be specified on one --id option",
2378 symbol->used = true;
2379 return &symbol->uuid;
2383 pre_get_column(struct vsctl_context *ctx,
2384 const struct vsctl_table_class *table, const char *column_name,
2385 const struct ovsdb_idl_column **columnp)
2387 die_if_error(get_column(table, column_name, columnp));
2388 ovsdb_idl_add_column(ctx->idl, *columnp);
2392 missing_operator_error(const char *arg, const char **allowed_operators,
2398 ds_put_format(&s, "%s: argument does not end in ", arg);
2399 ds_put_format(&s, "\"%s\"", allowed_operators[0]);
2400 if (n_allowed == 2) {
2401 ds_put_format(&s, " or \"%s\"", allowed_operators[1]);
2402 } else if (n_allowed > 2) {
2405 for (i = 1; i < n_allowed - 1; i++) {
2406 ds_put_format(&s, ", \"%s\"", allowed_operators[i]);
2408 ds_put_format(&s, ", or \"%s\"", allowed_operators[i]);
2410 ds_put_format(&s, " followed by a value.");
2412 return ds_steal_cstr(&s);
2415 /* Breaks 'arg' apart into a number of fields in the following order:
2417 * - If 'columnp' is nonnull, the name of a column in 'table'. The column
2418 * is stored into '*columnp'. The column name may be abbreviated.
2420 * - If 'keyp' is nonnull, optionally a key string. (If both 'columnp'
2421 * and 'keyp' are nonnull, then the column and key names are expected to
2422 * be separated by ':'). The key is stored as a malloc()'d string into
2423 * '*keyp', or NULL if no key is present in 'arg'.
2425 * - If 'valuep' is nonnull, an operator followed by a value string. The
2426 * allowed operators are the 'n_allowed' string in 'allowed_operators',
2427 * or just "=" if 'n_allowed' is 0. If 'operatorp' is nonnull, then the
2428 * operator is stored into '*operatorp' (one of the pointers from
2429 * 'allowed_operators' is stored; nothing is malloc()'d). The value is
2430 * stored as a malloc()'d string into '*valuep', or NULL if no value is
2433 * At least 'columnp' or 'keyp' must be nonnull.
2435 * On success, returns NULL. On failure, returned a malloc()'d string error
2436 * message and stores NULL into all of the nonnull output arguments. */
2437 static char * WARN_UNUSED_RESULT
2438 parse_column_key_value(const char *arg,
2439 const struct vsctl_table_class *table,
2440 const struct ovsdb_idl_column **columnp, char **keyp,
2441 const char **operatorp,
2442 const char **allowed_operators, size_t n_allowed,
2445 const char *p = arg;
2448 assert(columnp || keyp);
2449 assert(!(operatorp && !valuep));
2457 /* Parse column name. */
2461 error = ovsdb_token_parse(&p, &column_name);
2465 if (column_name[0] == '\0') {
2467 error = xasprintf("%s: missing column name", arg);
2470 error = get_column(table, column_name, columnp);
2477 /* Parse key string. */
2478 if (*p == ':' || !columnp) {
2482 error = xasprintf("%s: key not accepted here", arg);
2485 error = ovsdb_token_parse(&p, keyp);
2493 /* Parse value string. */
2499 if (!allowed_operators) {
2500 static const char *equals = "=";
2501 allowed_operators = =
2507 for (i = 0; i < n_allowed; i++) {
2508 const char *op = allowed_operators[i];
2509 size_t op_len = strlen(op);
2511 if (op_len > best_len && !strncmp(op, p, op_len) && p[op_len]) {
2517 error = missing_operator_error(arg, allowed_operators, n_allowed);
2524 *valuep = xstrdup(p + best_len);
2530 error = xasprintf("%s: trailing garbage \"%s\" in argument",
2556 pre_parse_column_key_value(struct vsctl_context *ctx,
2558 const struct vsctl_table_class *table)
2560 const struct ovsdb_idl_column *column;
2565 die_if_error(ovsdb_token_parse(&p, &column_name));
2566 if (column_name[0] == '\0') {
2567 vsctl_fatal("%s: missing column name", arg);
2570 pre_get_column(ctx, table, column_name, &column);
2575 pre_cmd_get(struct vsctl_context *ctx)
2577 const char *table_name = ctx->argv[1];
2578 const struct vsctl_table_class *table;
2581 table = pre_get_table(ctx, table_name);
2582 for (i = 3; i < ctx->argc; i++) {
2583 if (!strcasecmp(ctx->argv[i], "_uuid")
2584 || !strcasecmp(ctx->argv[i], "-uuid")) {
2588 pre_parse_column_key_value(ctx, ctx->argv[i], table);
2593 cmd_get(struct vsctl_context *ctx)
2595 const char *id = shash_find_data(&ctx->options, "--id");
2596 bool if_exists = shash_find(&ctx->options, "--if-exists");
2597 const char *table_name = ctx->argv[1];
2598 const char *record_id = ctx->argv[2];
2599 const struct vsctl_table_class *table;
2600 const struct ovsdb_idl_row *row;
2601 struct ds *out = &ctx->output;
2604 table = get_table(table_name);
2605 row = must_get_row(ctx, table, record_id);
2609 *create_symbol(ctx->symtab, id, &new) = row->uuid;
2611 vsctl_fatal("row id \"%s\" specified on \"get\" command was used "
2612 "before it was defined", id);
2615 for (i = 3; i < ctx->argc; i++) {
2616 const struct ovsdb_idl_column *column;
2617 const struct ovsdb_datum *datum;
2620 /* Special case for obtaining the UUID of a row. We can't just do this
2621 * through parse_column_key_value() below since it returns a "struct
2622 * ovsdb_idl_column" and the UUID column doesn't have one. */
2623 if (!strcasecmp(ctx->argv[i], "_uuid")
2624 || !strcasecmp(ctx->argv[i], "-uuid")) {
2625 ds_put_format(out, UUID_FMT"\n", UUID_ARGS(&row->uuid));
2629 die_if_error(parse_column_key_value(ctx->argv[i], table,
2630 &column, &key_string,
2631 NULL, NULL, 0, NULL));
2633 ovsdb_idl_txn_verify(row, column);
2634 datum = ovsdb_idl_read(row, column);
2636 union ovsdb_atom key;
2639 if (column->type.value.type == OVSDB_TYPE_VOID) {
2640 vsctl_fatal("cannot specify key to get for non-map column %s",
2644 die_if_error(ovsdb_atom_from_string(&key,
2646 key_string, ctx->symtab));
2648 idx = ovsdb_datum_find_key(datum, &key,
2649 column->type.key.type);
2650 if (idx == UINT_MAX) {
2652 vsctl_fatal("no key \"%s\" in %s record \"%s\" column %s",
2653 key_string, table->class->name, record_id,
2657 ovsdb_atom_to_string(&datum->values[idx],
2658 column->type.value.type, out);
2660 ovsdb_atom_destroy(&key, column->type.key.type);
2662 ovsdb_datum_to_string(datum, &column->type, out);
2664 ds_put_char(out, '\n');
2671 parse_column_names(const char *column_names,
2672 const struct vsctl_table_class *table,
2673 const struct ovsdb_idl_column ***columnsp,
2676 const struct ovsdb_idl_column **columns;
2679 if (!column_names) {
2682 n_columns = table->class->n_columns + 1;
2683 columns = xmalloc(n_columns * sizeof *columns);
2685 for (i = 0; i < table->class->n_columns; i++) {
2686 columns[i + 1] = &table->class->columns[i];
2689 char *s = xstrdup(column_names);
2690 size_t allocated_columns;
2691 char *save_ptr = NULL;
2695 allocated_columns = n_columns = 0;
2696 for (column_name = strtok_r(s, ", ", &save_ptr); column_name;
2697 column_name = strtok_r(NULL, ", ", &save_ptr)) {
2698 const struct ovsdb_idl_column *column;
2700 if (!strcasecmp(column_name, "_uuid")) {
2703 die_if_error(get_column(table, column_name, &column));
2705 if (n_columns >= allocated_columns) {
2706 columns = x2nrealloc(columns, &allocated_columns,
2709 columns[n_columns++] = column;
2714 vsctl_fatal("must specify at least one column name");
2717 *columnsp = columns;
2718 *n_columnsp = n_columns;
2723 pre_list_columns(struct vsctl_context *ctx,
2724 const struct vsctl_table_class *table,
2725 const char *column_names)
2727 const struct ovsdb_idl_column **columns;
2731 parse_column_names(column_names, table, &columns, &n_columns);
2732 for (i = 0; i < n_columns; i++) {
2734 ovsdb_idl_add_column(ctx->idl, columns[i]);
2741 pre_cmd_list(struct vsctl_context *ctx)
2743 const char *column_names = shash_find_data(&ctx->options, "--columns");
2744 const char *table_name = ctx->argv[1];
2745 const struct vsctl_table_class *table;
2747 table = pre_get_table(ctx, table_name);
2748 pre_list_columns(ctx, table, column_names);
2751 static struct table *
2752 list_make_table(const struct ovsdb_idl_column **columns, size_t n_columns)
2757 out = xmalloc(sizeof *out);
2760 for (i = 0; i < n_columns; i++) {
2761 const struct ovsdb_idl_column *column = columns[i];
2762 const char *column_name = column ? column->name : "_uuid";
2764 table_add_column(out, "%s", column_name);
2771 list_record(const struct ovsdb_idl_row *row,
2772 const struct ovsdb_idl_column **columns, size_t n_columns,
2778 for (i = 0; i < n_columns; i++) {
2779 const struct ovsdb_idl_column *column = columns[i];
2780 struct cell *cell = table_add_cell(out);
2783 struct ovsdb_datum datum;
2784 union ovsdb_atom atom;
2786 atom.uuid = row->uuid;
2789 datum.values = NULL;
2792 cell->json = ovsdb_datum_to_json(&datum, &ovsdb_type_uuid);
2793 cell->type = &ovsdb_type_uuid;
2795 const struct ovsdb_datum *datum = ovsdb_idl_read(row, column);
2797 cell->json = ovsdb_datum_to_json(datum, &column->type);
2798 cell->type = &column->type;
2804 cmd_list(struct vsctl_context *ctx)
2806 const char *column_names = shash_find_data(&ctx->options, "--columns");
2807 const struct ovsdb_idl_column **columns;
2808 const char *table_name = ctx->argv[1];
2809 const struct vsctl_table_class *table;
2814 table = get_table(table_name);
2815 parse_column_names(column_names, table, &columns, &n_columns);
2816 out = ctx->table = list_make_table(columns, n_columns);
2817 if (ctx->argc > 2) {
2818 for (i = 2; i < ctx->argc; i++) {
2819 list_record(must_get_row(ctx, table, ctx->argv[i]),
2820 columns, n_columns, out);
2823 const struct ovsdb_idl_row *row;
2826 for (row = ovsdb_idl_first_row(ctx->idl, table->class), first = true;
2828 row = ovsdb_idl_next_row(row), first = false) {
2829 list_record(row, columns, n_columns, out);
2836 pre_cmd_find(struct vsctl_context *ctx)
2838 const char *column_names = shash_find_data(&ctx->options, "--columns");
2839 const char *table_name = ctx->argv[1];
2840 const struct vsctl_table_class *table;
2843 table = pre_get_table(ctx, table_name);
2844 pre_list_columns(ctx, table, column_names);
2845 for (i = 2; i < ctx->argc; i++) {
2846 pre_parse_column_key_value(ctx, ctx->argv[i], table);
2851 cmd_find(struct vsctl_context *ctx)
2853 const char *column_names = shash_find_data(&ctx->options, "--columns");
2854 const struct ovsdb_idl_column **columns;
2855 const char *table_name = ctx->argv[1];
2856 const struct vsctl_table_class *table;
2857 const struct ovsdb_idl_row *row;
2861 table = get_table(table_name);
2862 parse_column_names(column_names, table, &columns, &n_columns);
2863 out = ctx->table = list_make_table(columns, n_columns);
2864 for (row = ovsdb_idl_first_row(ctx->idl, table->class); row;
2865 row = ovsdb_idl_next_row(row)) {
2868 for (i = 2; i < ctx->argc; i++) {
2869 if (!is_condition_satisfied(table, row, ctx->argv[i],
2874 list_record(row, columns, n_columns, out);
2881 pre_cmd_set(struct vsctl_context *ctx)
2883 const char *table_name = ctx->argv[1];
2884 const struct vsctl_table_class *table;
2887 table = pre_get_table(ctx, table_name);
2888 for (i = 3; i < ctx->argc; i++) {
2889 pre_parse_column_key_value(ctx, ctx->argv[i], table);
2894 set_column(const struct vsctl_table_class *table,
2895 const struct ovsdb_idl_row *row, const char *arg,
2896 struct ovsdb_symbol_table *symtab)
2898 const struct ovsdb_idl_column *column;
2899 char *key_string, *value_string;
2902 error = parse_column_key_value(arg, table, &column, &key_string,
2903 NULL, NULL, 0, &value_string);
2904 die_if_error(error);
2905 if (!value_string) {
2906 vsctl_fatal("%s: missing value", arg);
2910 union ovsdb_atom key, value;
2911 struct ovsdb_datum datum;
2913 if (column->type.value.type == OVSDB_TYPE_VOID) {
2914 vsctl_fatal("cannot specify key to set for non-map column %s",
2918 die_if_error(ovsdb_atom_from_string(&key, &column->type.key,
2919 key_string, symtab));
2920 die_if_error(ovsdb_atom_from_string(&value, &column->type.value,
2921 value_string, symtab));
2923 ovsdb_datum_init_empty(&datum);
2924 ovsdb_datum_add_unsafe(&datum, &key, &value, &column->type);
2926 ovsdb_atom_destroy(&key, column->type.key.type);
2927 ovsdb_atom_destroy(&value, column->type.value.type);
2929 ovsdb_datum_union(&datum, ovsdb_idl_read(row, column),
2930 &column->type, false);
2931 ovsdb_idl_txn_write(row, column, &datum);
2933 struct ovsdb_datum datum;
2935 die_if_error(ovsdb_datum_from_string(&datum, &column->type,
2936 value_string, symtab));
2937 ovsdb_idl_txn_write(row, column, &datum);
2945 cmd_set(struct vsctl_context *ctx)
2947 const char *table_name = ctx->argv[1];
2948 const char *record_id = ctx->argv[2];
2949 const struct vsctl_table_class *table;
2950 const struct ovsdb_idl_row *row;
2953 table = get_table(table_name);
2954 row = must_get_row(ctx, table, record_id);
2955 for (i = 3; i < ctx->argc; i++) {
2956 set_column(table, row, ctx->argv[i], ctx->symtab);
2961 pre_cmd_add(struct vsctl_context *ctx)
2963 const char *table_name = ctx->argv[1];
2964 const char *column_name = ctx->argv[3];
2965 const struct vsctl_table_class *table;
2966 const struct ovsdb_idl_column *column;
2968 table = pre_get_table(ctx, table_name);
2969 pre_get_column(ctx, table, column_name, &column);
2973 cmd_add(struct vsctl_context *ctx)
2975 const char *table_name = ctx->argv[1];
2976 const char *record_id = ctx->argv[2];
2977 const char *column_name = ctx->argv[3];
2978 const struct vsctl_table_class *table;
2979 const struct ovsdb_idl_column *column;
2980 const struct ovsdb_idl_row *row;
2981 const struct ovsdb_type *type;
2982 struct ovsdb_datum old;
2985 table = get_table(table_name);
2986 row = must_get_row(ctx, table, record_id);
2987 die_if_error(get_column(table, column_name, &column));
2989 type = &column->type;
2990 ovsdb_datum_clone(&old, ovsdb_idl_read(row, column), &column->type);
2991 for (i = 4; i < ctx->argc; i++) {
2992 struct ovsdb_type add_type;
2993 struct ovsdb_datum add;
2997 add_type.n_max = UINT_MAX;
2998 die_if_error(ovsdb_datum_from_string(&add, &add_type, ctx->argv[i],
3000 ovsdb_datum_union(&old, &add, type, false);
3001 ovsdb_datum_destroy(&add, type);
3003 if (old.n > type->n_max) {
3004 vsctl_fatal("\"add\" operation would put %u %s in column %s of "
3005 "table %s but the maximum number is %u",
3007 type->value.type == OVSDB_TYPE_VOID ? "values" : "pairs",
3008 column->name, table->class->name, type->n_max);
3010 ovsdb_idl_txn_verify(row, column);
3011 ovsdb_idl_txn_write(row, column, &old);
3015 pre_cmd_remove(struct vsctl_context *ctx)
3017 const char *table_name = ctx->argv[1];
3018 const char *column_name = ctx->argv[3];
3019 const struct vsctl_table_class *table;
3020 const struct ovsdb_idl_column *column;
3022 table = pre_get_table(ctx, table_name);
3023 pre_get_column(ctx, table, column_name, &column);
3027 cmd_remove(struct vsctl_context *ctx)
3029 const char *table_name = ctx->argv[1];
3030 const char *record_id = ctx->argv[2];
3031 const char *column_name = ctx->argv[3];
3032 const struct vsctl_table_class *table;
3033 const struct ovsdb_idl_column *column;
3034 const struct ovsdb_idl_row *row;
3035 const struct ovsdb_type *type;
3036 struct ovsdb_datum old;
3039 table = get_table(table_name);
3040 row = must_get_row(ctx, table, record_id);
3041 die_if_error(get_column(table, column_name, &column));
3043 type = &column->type;
3044 ovsdb_datum_clone(&old, ovsdb_idl_read(row, column), &column->type);
3045 for (i = 4; i < ctx->argc; i++) {
3046 struct ovsdb_type rm_type;
3047 struct ovsdb_datum rm;
3052 rm_type.n_max = UINT_MAX;
3053 error = ovsdb_datum_from_string(&rm, &rm_type,
3054 ctx->argv[i], ctx->symtab);
3055 if (error && ovsdb_type_is_map(&rm_type)) {
3057 rm_type.value.type = OVSDB_TYPE_VOID;
3058 die_if_error(ovsdb_datum_from_string(&rm, &rm_type,
3059 ctx->argv[i], ctx->symtab));
3061 ovsdb_datum_subtract(&old, type, &rm, &rm_type);
3062 ovsdb_datum_destroy(&rm, &rm_type);
3064 if (old.n < type->n_min) {
3065 vsctl_fatal("\"remove\" operation would put %u %s in column %s of "
3066 "table %s but the minimum number is %u",
3068 type->value.type == OVSDB_TYPE_VOID ? "values" : "pairs",
3069 column->name, table->class->name, type->n_min);
3071 ovsdb_idl_txn_verify(row, column);
3072 ovsdb_idl_txn_write(row, column, &old);
3076 pre_cmd_clear(struct vsctl_context *ctx)
3078 const char *table_name = ctx->argv[1];
3079 const struct vsctl_table_class *table;
3082 table = pre_get_table(ctx, table_name);
3083 for (i = 3; i < ctx->argc; i++) {
3084 const struct ovsdb_idl_column *column;
3086 pre_get_column(ctx, table, ctx->argv[i], &column);
3091 cmd_clear(struct vsctl_context *ctx)
3093 const char *table_name = ctx->argv[1];
3094 const char *record_id = ctx->argv[2];
3095 const struct vsctl_table_class *table;
3096 const struct ovsdb_idl_row *row;
3099 table = get_table(table_name);
3100 row = must_get_row(ctx, table, record_id);
3101 for (i = 3; i < ctx->argc; i++) {
3102 const struct ovsdb_idl_column *column;
3103 const struct ovsdb_type *type;
3104 struct ovsdb_datum datum;
3106 die_if_error(get_column(table, ctx->argv[i], &column));
3108 type = &column->type;
3109 if (type->n_min > 0) {
3110 vsctl_fatal("\"clear\" operation cannot be applied to column %s "
3111 "of table %s, which is not allowed to be empty",
3112 column->name, table->class->name);
3115 ovsdb_datum_init_empty(&datum);
3116 ovsdb_idl_txn_write(row, column, &datum);
3121 cmd_create(struct vsctl_context *ctx)
3123 const char *id = shash_find_data(&ctx->options, "--id");
3124 const char *table_name = ctx->argv[1];
3125 const struct vsctl_table_class *table;
3126 const struct ovsdb_idl_row *row;
3127 const struct uuid *uuid;
3130 uuid = id ? create_symbol(ctx->symtab, id, NULL) : NULL;
3132 table = get_table(table_name);
3133 row = ovsdb_idl_txn_insert(ctx->txn, table->class, uuid);
3134 for (i = 2; i < ctx->argc; i++) {
3135 set_column(table, row, ctx->argv[i], ctx->symtab);
3137 ds_put_format(&ctx->output, UUID_FMT, UUID_ARGS(&row->uuid));
3140 /* This function may be used as the 'postprocess' function for commands that
3141 * insert new rows into the database. It expects that the command's 'run'
3142 * function prints the UUID reported by ovsdb_idl_txn_insert() as the command's
3143 * sole output. It replaces that output by the row's permanent UUID assigned
3144 * by the database server and appends a new-line.
3146 * Currently we use this only for "create", because the higher-level commands
3147 * are supposed to be independent of the actual structure of the vswitch
3150 post_create(struct vsctl_context *ctx)
3152 const struct uuid *real;
3155 uuid_from_string(&dummy, ds_cstr(&ctx->output));
3156 real = ovsdb_idl_txn_get_insert_uuid(ctx->txn, &dummy);
3158 ds_clear(&ctx->output);
3159 ds_put_format(&ctx->output, UUID_FMT, UUID_ARGS(real));
3161 ds_put_char(&ctx->output, '\n');
3165 pre_cmd_destroy(struct vsctl_context *ctx)
3167 const char *table_name = ctx->argv[1];
3169 pre_get_table(ctx, table_name);
3173 cmd_destroy(struct vsctl_context *ctx)
3175 bool must_exist = !shash_find(&ctx->options, "--if-exists");
3176 const char *table_name = ctx->argv[1];
3177 const struct vsctl_table_class *table;
3180 table = get_table(table_name);
3181 for (i = 2; i < ctx->argc; i++) {
3182 const struct ovsdb_idl_row *row;
3184 row = (must_exist ? must_get_row : get_row)(ctx, table, ctx->argv[i]);
3186 ovsdb_idl_txn_delete(row);
3192 is_condition_satisfied(const struct vsctl_table_class *table,
3193 const struct ovsdb_idl_row *row, const char *arg,
3194 struct ovsdb_symbol_table *symtab)
3196 static const char *operators[] = {
3197 "=", "!=", "<", ">", "<=", ">="
3200 const struct ovsdb_idl_column *column;
3201 const struct ovsdb_datum *have_datum;
3202 char *key_string, *value_string;
3203 const char *operator;
3208 error = parse_column_key_value(arg, table, &column, &key_string,
3209 &operator, operators, ARRAY_SIZE(operators),
3211 die_if_error(error);
3212 if (!value_string) {
3213 vsctl_fatal("%s: missing value", arg);
3216 have_datum = ovsdb_idl_read(row, column);
3218 union ovsdb_atom want_key, want_value;
3220 if (column->type.value.type == OVSDB_TYPE_VOID) {
3221 vsctl_fatal("cannot specify key to check for non-map column %s",
3225 die_if_error(ovsdb_atom_from_string(&want_key, &column->type.key,
3226 key_string, symtab));
3227 die_if_error(ovsdb_atom_from_string(&want_value, &column->type.value,
3228 value_string, symtab));
3230 idx = ovsdb_datum_find_key(have_datum,
3231 &want_key, column->type.key.type);
3232 if (idx != UINT_MAX) {
3233 cmp = ovsdb_atom_compare_3way(&have_datum->values[idx],
3235 column->type.value.type);
3238 ovsdb_atom_destroy(&want_key, column->type.key.type);
3239 ovsdb_atom_destroy(&want_value, column->type.value.type);
3241 struct ovsdb_datum want_datum;
3243 die_if_error(ovsdb_datum_from_string(&want_datum, &column->type,
3244 value_string, symtab));
3246 cmp = ovsdb_datum_compare_3way(have_datum, &want_datum,
3248 ovsdb_datum_destroy(&want_datum, &column->type);
3254 return (idx == UINT_MAX ? false
3255 : !strcmp(operator, "=") ? cmp == 0
3256 : !strcmp(operator, "!=") ? cmp != 0
3257 : !strcmp(operator, "<") ? cmp < 0
3258 : !strcmp(operator, ">") ? cmp > 0
3259 : !strcmp(operator, "<=") ? cmp <= 0
3260 : !strcmp(operator, ">=") ? cmp >= 0
3265 pre_cmd_wait_until(struct vsctl_context *ctx)
3267 const char *table_name = ctx->argv[1];
3268 const struct vsctl_table_class *table;
3271 table = pre_get_table(ctx, table_name);
3273 for (i = 3; i < ctx->argc; i++) {
3274 pre_parse_column_key_value(ctx, ctx->argv[i], table);
3279 cmd_wait_until(struct vsctl_context *ctx)
3281 const char *table_name = ctx->argv[1];
3282 const char *record_id = ctx->argv[2];
3283 const struct vsctl_table_class *table;
3284 const struct ovsdb_idl_row *row;
3287 table = get_table(table_name);
3289 row = get_row(ctx, table, record_id);
3291 ctx->try_again = true;
3295 for (i = 3; i < ctx->argc; i++) {
3296 if (!is_condition_satisfied(table, row, ctx->argv[i], ctx->symtab)) {
3297 ctx->try_again = true;
3303 static struct json *
3304 where_uuid_equals(const struct uuid *uuid)
3307 json_array_create_1(
3308 json_array_create_3(
3309 json_string_create("_uuid"),
3310 json_string_create("=="),
3311 json_array_create_2(
3312 json_string_create("uuid"),
3313 json_string_create_nocopy(
3314 xasprintf(UUID_FMT, UUID_ARGS(uuid))))));
3318 vsctl_context_init(struct vsctl_context *ctx, struct vsctl_command *command,
3319 struct ovsdb_idl *idl, struct ovsdb_idl_txn *txn,
3320 const struct ovsrec_open_vswitch *ovs,
3321 struct ovsdb_symbol_table *symtab)
3323 ctx->argc = command->argc;
3324 ctx->argv = command->argv;
3325 ctx->options = command->options;
3327 ds_swap(&ctx->output, &command->output);
3328 ctx->table = command->table;
3332 ctx->symtab = symtab;
3333 ctx->verified_ports = false;
3335 ctx->try_again = false;
3339 vsctl_context_done(struct vsctl_context *ctx, struct vsctl_command *command)
3341 ds_swap(&ctx->output, &command->output);
3342 command->table = ctx->table;
3346 run_prerequisites(struct vsctl_command *commands, size_t n_commands,
3347 struct ovsdb_idl *idl)
3349 struct vsctl_command *c;
3351 ovsdb_idl_add_table(idl, &ovsrec_table_open_vswitch);
3352 if (wait_for_reload) {
3353 ovsdb_idl_add_column(idl, &ovsrec_open_vswitch_col_cur_cfg);
3355 for (c = commands; c < &commands[n_commands]; c++) {
3356 if (c->syntax->prerequisites) {
3357 struct vsctl_context ctx;
3359 ds_init(&c->output);
3362 vsctl_context_init(&ctx, c, idl, NULL, NULL, NULL);
3363 (c->syntax->prerequisites)(&ctx);
3364 vsctl_context_done(&ctx, c);
3366 assert(!c->output.string);
3373 do_vsctl(const char *args, struct vsctl_command *commands, size_t n_commands,
3374 struct ovsdb_idl *idl)
3376 struct ovsdb_idl_txn *txn;
3377 const struct ovsrec_open_vswitch *ovs;
3378 enum ovsdb_idl_txn_status status;
3379 struct ovsdb_symbol_table *symtab;
3381 struct vsctl_command *c;
3382 int64_t next_cfg = 0;
3385 txn = the_idl_txn = ovsdb_idl_txn_create(idl);
3387 ovsdb_idl_txn_set_dry_run(txn);
3390 ovsdb_idl_txn_add_comment(txn, "ovs-vsctl: %s", args);
3392 ovs = ovsrec_open_vswitch_first(idl);
3394 /* XXX add verification that table is empty */
3395 ovs = ovsrec_open_vswitch_insert(txn);
3398 if (wait_for_reload) {
3399 struct json *where = where_uuid_equals(&ovs->header_.uuid);
3400 ovsdb_idl_txn_increment(txn, "Open_vSwitch", "next_cfg", where);
3401 json_destroy(where);
3404 symtab = ovsdb_symbol_table_create();
3405 for (c = commands; c < &commands[n_commands]; c++) {
3406 ds_init(&c->output);
3409 for (c = commands; c < &commands[n_commands]; c++) {
3410 struct vsctl_context ctx;
3412 vsctl_context_init(&ctx, c, idl, txn, ovs, symtab);
3413 (c->syntax->run)(&ctx);
3414 vsctl_context_done(&ctx, c);
3416 if (ctx.try_again) {
3421 status = ovsdb_idl_txn_commit_block(txn);
3422 if (wait_for_reload && status == TXN_SUCCESS) {
3423 next_cfg = ovsdb_idl_txn_get_increment_new_value(txn);
3425 if (status == TXN_UNCHANGED || status == TXN_SUCCESS) {
3426 for (c = commands; c < &commands[n_commands]; c++) {
3427 if (c->syntax->postprocess) {
3428 struct vsctl_context ctx;
3430 vsctl_context_init(&ctx, c, idl, txn, ovs, symtab);
3431 (c->syntax->postprocess)(&ctx);
3432 vsctl_context_done(&ctx, c);
3436 error = xstrdup(ovsdb_idl_txn_get_error(txn));
3437 ovsdb_idl_txn_destroy(txn);
3438 txn = the_idl_txn = NULL;
3440 unused = ovsdb_symbol_table_find_unused(symtab);
3442 vsctl_fatal("row id \"%s\" is referenced but never created (e.g. "
3443 "with \"-- --id=%s create ...\")", unused, unused);
3447 case TXN_INCOMPLETE:
3451 /* Should not happen--we never call ovsdb_idl_txn_abort(). */
3452 vsctl_fatal("transaction aborted");
3462 vsctl_fatal("transaction error: %s", error);
3469 ovsdb_symbol_table_destroy(symtab);
3471 for (c = commands; c < &commands[n_commands]; c++) {
3472 struct ds *ds = &c->output;
3475 table_print(c->table, &table_style);
3476 } else if (oneline) {
3480 for (j = 0; j < ds->length; j++) {
3481 int ch = ds->string[j];
3484 fputs("\\n", stdout);
3488 fputs("\\\\", stdout);
3497 fputs(ds_cstr(ds), stdout);
3499 ds_destroy(&c->output);
3500 table_destroy(c->table);
3503 smap_destroy(&c->options);
3507 if (wait_for_reload && status != TXN_UNCHANGED) {
3510 OVSREC_OPEN_VSWITCH_FOR_EACH (ovs, idl) {
3511 if (ovs->cur_cfg >= next_cfg) {
3515 ovsdb_idl_wait(idl);
3520 ovsdb_idl_destroy(idl);
3525 /* Our transaction needs to be rerun, or a prerequisite was not met. Free
3526 * resources and return so that the caller can try again. */
3528 ovsdb_idl_txn_abort(txn);
3529 ovsdb_idl_txn_destroy(txn);
3531 ovsdb_symbol_table_destroy(symtab);
3532 for (c = commands; c < &commands[n_commands]; c++) {
3533 ds_destroy(&c->output);
3534 table_destroy(c->table);
3540 static const struct vsctl_command_syntax all_commands[] = {
3541 /* Open vSwitch commands. */
3542 {"init", 0, 0, NULL, cmd_init, NULL, "", RW},
3544 /* Bridge commands. */
3545 {"add-br", 1, 3, pre_get_info, cmd_add_br, NULL, "--may-exist", RW},
3546 {"del-br", 1, 1, pre_get_info, cmd_del_br, NULL, "--if-exists", RW},
3547 {"list-br", 0, 0, pre_get_info, cmd_list_br, NULL, "", RO},
3548 {"br-exists", 1, 1, pre_get_info, cmd_br_exists, NULL, "", RO},
3549 {"br-to-vlan", 1, 1, pre_get_info, cmd_br_to_vlan, NULL, "", RO},
3550 {"br-to-parent", 1, 1, pre_get_info, cmd_br_to_parent, NULL, "", RO},
3551 {"br-set-external-id", 2, 3, pre_cmd_br_set_external_id,
3552 cmd_br_set_external_id, NULL, "", RW},
3553 {"br-get-external-id", 1, 2, pre_cmd_br_get_external_id,
3554 cmd_br_get_external_id, NULL, "", RO},
3556 /* Port commands. */
3557 {"list-ports", 1, 1, pre_get_info, cmd_list_ports, NULL, "", RO},
3558 {"add-port", 2, INT_MAX, pre_get_info, cmd_add_port, NULL, "--may-exist",
3560 {"add-bond", 4, INT_MAX, pre_get_info, cmd_add_bond, NULL,
3561 "--may-exist,--fake-iface", RW},
3562 {"del-port", 1, 2, pre_get_info, cmd_del_port, NULL,
3563 "--if-exists,--with-iface", RW},
3564 {"port-to-br", 1, 1, pre_get_info, cmd_port_to_br, NULL, "", RO},
3566 /* Interface commands. */
3567 {"list-ifaces", 1, 1, pre_get_info, cmd_list_ifaces, NULL, "", RO},
3568 {"iface-to-br", 1, 1, pre_get_info, cmd_iface_to_br, NULL, "", RO},
3570 /* Controller commands. */
3571 {"get-controller", 1, 1, pre_controller, cmd_get_controller, NULL, "", RO},
3572 {"del-controller", 1, 1, pre_controller, cmd_del_controller, NULL, "", RW},
3573 {"set-controller", 1, INT_MAX, pre_controller, cmd_set_controller, NULL,
3575 {"get-fail-mode", 1, 1, pre_get_info, cmd_get_fail_mode, NULL, "", RO},
3576 {"del-fail-mode", 1, 1, pre_get_info, cmd_del_fail_mode, NULL, "", RW},
3577 {"set-fail-mode", 2, 2, pre_get_info, cmd_set_fail_mode, NULL, "", RW},
3579 /* Manager commands. */
3580 {"get-manager", 0, 0, pre_manager, cmd_get_manager, NULL, "", RO},
3581 {"del-manager", 0, INT_MAX, pre_manager, cmd_del_manager, NULL, "", RW},
3582 {"set-manager", 1, INT_MAX, pre_manager, cmd_set_manager, NULL, "", RW},
3585 {"get-ssl", 0, 0, pre_cmd_get_ssl, cmd_get_ssl, NULL, "", RO},
3586 {"del-ssl", 0, 0, pre_cmd_del_ssl, cmd_del_ssl, NULL, "", RW},
3587 {"set-ssl", 3, 3, pre_cmd_set_ssl, cmd_set_ssl, NULL, "--bootstrap", RW},
3589 /* Switch commands. */
3590 {"emer-reset", 0, 0, pre_cmd_emer_reset, cmd_emer_reset, NULL, "", RW},
3592 /* Parameter commands. */
3593 {"get", 2, INT_MAX, pre_cmd_get, cmd_get, NULL, "--if-exists,--id=", RO},
3594 {"list", 1, INT_MAX, pre_cmd_list, cmd_list, NULL, "--columns=", RO},
3595 {"find", 1, INT_MAX, pre_cmd_find, cmd_find, NULL, "--columns=", RO},
3596 {"set", 3, INT_MAX, pre_cmd_set, cmd_set, NULL, "", RW},
3597 {"add", 4, INT_MAX, pre_cmd_add, cmd_add, NULL, "", RW},
3598 {"remove", 4, INT_MAX, pre_cmd_remove, cmd_remove, NULL, "", RW},
3599 {"clear", 3, INT_MAX, pre_cmd_clear, cmd_clear, NULL, "", RW},
3600 {"create", 2, INT_MAX, NULL, cmd_create, post_create, "--id=", RW},
3601 {"destroy", 1, INT_MAX, pre_cmd_destroy, cmd_destroy, NULL, "--if-exists",
3603 {"wait-until", 2, INT_MAX, pre_cmd_wait_until, cmd_wait_until, NULL, "",
3606 {NULL, 0, 0, NULL, NULL, NULL, NULL, RO},