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.
31 #include "command-line.h"
34 #include "dynamic-string.h"
36 #include "ovsdb-data.h"
37 #include "ovsdb-idl.h"
38 #include "poll-loop.h"
41 #include "stream-ssl.h"
44 #include "vswitchd/vswitch-idl.h"
50 VLOG_DEFINE_THIS_MODULE(vsctl);
52 /* vsctl_fatal() also logs the error, so it is preferred in this file. */
53 #define ovs_fatal please_use_vsctl_fatal_instead_of_ovs_fatal
57 /* A command supported by ovs-vsctl. */
58 struct vsctl_command_syntax {
59 const char *name; /* e.g. "add-br" */
60 int min_args; /* Min number of arguments following name. */
61 int max_args; /* Max number of arguments following name. */
63 /* If nonnull, calls ovsdb_idl_add_column() or ovsdb_idl_add_table() for
64 * each column or table in ctx->idl that it uses. */
65 void (*prerequisites)(struct vsctl_context *ctx);
67 /* Does the actual work of the command and puts the command's output, if
68 * any, in ctx->output or ctx->table.
70 * Alternatively, if some prerequisite of the command is not met and the
71 * caller should wait for something to change and then retry, it may set
72 * ctx->try_again to true. (Only the "wait-until" command currently does
74 void (*run)(struct vsctl_context *ctx);
76 /* If nonnull, called after the transaction has been successfully
77 * committed. ctx->output is the output from the "run" function, which
78 * this function may modify and otherwise postprocess as needed. (Only the
79 * "create" command currently does any postprocessing.) */
80 void (*postprocess)(struct vsctl_context *ctx);
82 /* A comma-separated list of supported options, e.g. "--a,--b", or the
83 * empty string if the command does not support any options. */
85 enum { RO, RW } mode; /* Does this command modify the database? */
88 struct vsctl_command {
89 /* Data that remains constant after initialization. */
90 const struct vsctl_command_syntax *syntax;
95 /* Data modified by commands. */
100 /* --db: The database server to contact. */
101 static const char *db;
103 /* --oneline: Write each command's output as a single line? */
106 /* --dry-run: Do not commit any changes. */
109 /* --no-wait: Wait for ovs-vswitchd to reload its configuration? */
110 static bool wait_for_reload = true;
112 /* --timeout: Time to wait for a connection to 'db'. */
115 /* Format for table output. */
116 static struct table_style table_style = TABLE_STYLE_DEFAULT;
118 /* All supported commands. */
119 static const struct vsctl_command_syntax all_commands[];
121 /* The IDL we're using and the current transaction, if any.
122 * This is for use by vsctl_exit() only, to allow it to clean up.
123 * Other code should use its context arguments. */
124 static struct ovsdb_idl *the_idl;
125 static struct ovsdb_idl_txn *the_idl_txn;
127 static void vsctl_exit(int status) NO_RETURN;
128 static void vsctl_fatal(const char *, ...) PRINTF_FORMAT(1, 2) NO_RETURN;
129 static char *default_db(void);
130 static void usage(void) NO_RETURN;
131 static void parse_options(int argc, char *argv[]);
132 static bool might_write_to_db(char **argv);
134 static struct vsctl_command *parse_commands(int argc, char *argv[],
135 size_t *n_commandsp);
136 static void parse_command(int argc, char *argv[], struct vsctl_command *);
137 static const struct vsctl_command_syntax *find_command(const char *name);
138 static void run_prerequisites(struct vsctl_command[], size_t n_commands,
140 static void do_vsctl(const char *args,
141 struct vsctl_command *, size_t n_commands,
144 static const struct vsctl_table_class *get_table(const char *table_name);
145 static void set_column(const struct vsctl_table_class *,
146 const struct ovsdb_idl_row *, const char *arg,
147 struct ovsdb_symbol_table *);
149 static bool is_condition_satisfied(const struct vsctl_table_class *,
150 const struct ovsdb_idl_row *,
152 struct ovsdb_symbol_table *);
155 main(int argc, char *argv[])
157 extern struct vlog_module VLM_reconnect;
158 struct ovsdb_idl *idl;
159 struct vsctl_command *commands;
163 set_program_name(argv[0]);
164 signal(SIGPIPE, SIG_IGN);
165 vlog_set_levels(NULL, VLF_CONSOLE, VLL_WARN);
166 vlog_set_levels(&VLM_reconnect, VLF_ANY_FACILITY, VLL_WARN);
169 /* Log our arguments. This is often valuable for debugging systems. */
170 args = process_escape_args(argv);
171 VLOG(might_write_to_db(argv) ? VLL_INFO : VLL_DBG, "Called as %s", args);
173 /* Parse command line. */
174 parse_options(argc, argv);
175 commands = parse_commands(argc - optind, argv + optind, &n_commands);
181 /* Initialize IDL. */
182 idl = the_idl = ovsdb_idl_create(db, &ovsrec_idl_class, false);
183 run_prerequisites(commands, n_commands, idl);
185 /* Now execute the commands. */
187 if (ovsdb_idl_run(idl)) {
188 do_vsctl(args, commands, n_commands, idl);
197 parse_options(int argc, char *argv[])
200 OPT_DB = UCHAR_MAX + 1,
209 static struct option long_options[] = {
210 {"db", required_argument, 0, OPT_DB},
211 {"no-syslog", no_argument, 0, OPT_NO_SYSLOG},
212 {"no-wait", no_argument, 0, OPT_NO_WAIT},
213 {"dry-run", no_argument, 0, OPT_DRY_RUN},
214 {"oneline", no_argument, 0, OPT_ONELINE},
215 {"timeout", required_argument, 0, 't'},
216 {"help", no_argument, 0, 'h'},
217 {"version", no_argument, 0, 'V'},
221 STREAM_SSL_LONG_OPTIONS
222 {"peer-ca-cert", required_argument, 0, OPT_PEER_CA_CERT},
226 char *tmp, *short_options;
228 tmp = long_options_to_short_options(long_options);
229 short_options = xasprintf("+%s", tmp);
232 table_style.format = TF_LIST;
237 c = getopt_long(argc, argv, short_options, long_options, NULL);
252 vlog_set_levels(&VLM_vsctl, VLF_SYSLOG, VLL_WARN);
256 wait_for_reload = false;
267 OVS_PRINT_VERSION(0, 0);
271 timeout = strtoul(optarg, NULL, 10);
273 vsctl_fatal("value %s on -t or --timeout is invalid",
279 TABLE_OPTION_HANDLERS(&table_style)
282 STREAM_SSL_OPTION_HANDLERS
284 case OPT_PEER_CA_CERT:
285 stream_ssl_set_peer_ca_cert_file(optarg);
303 static struct vsctl_command *
304 parse_commands(int argc, char *argv[], size_t *n_commandsp)
306 struct vsctl_command *commands;
307 size_t n_commands, allocated_commands;
311 n_commands = allocated_commands = 0;
313 for (start = i = 0; i <= argc; i++) {
314 if (i == argc || !strcmp(argv[i], "--")) {
316 if (n_commands >= allocated_commands) {
317 struct vsctl_command *c;
319 commands = x2nrealloc(commands, &allocated_commands,
321 for (c = commands; c < &commands[n_commands]; c++) {
322 shash_moved(&c->options);
325 parse_command(i - start, &argv[start],
326 &commands[n_commands++]);
332 vsctl_fatal("missing command name (use --help for help)");
334 *n_commandsp = n_commands;
339 parse_command(int argc, char *argv[], struct vsctl_command *command)
341 const struct vsctl_command_syntax *p;
342 struct shash_node *node;
346 shash_init(&command->options);
347 for (i = 0; i < argc; i++) {
348 const char *option = argv[i];
352 if (option[0] != '-') {
356 equals = strchr(option, '=');
358 key = xmemdup0(option, equals - option);
359 value = xstrdup(equals + 1);
361 key = xstrdup(option);
365 if (shash_find(&command->options, key)) {
366 vsctl_fatal("'%s' option specified multiple times", argv[i]);
368 shash_add_nocopy(&command->options, key, value);
371 vsctl_fatal("missing command name");
374 p = find_command(argv[i]);
376 vsctl_fatal("unknown command '%s'; use --help for help", argv[i]);
379 SHASH_FOR_EACH (node, &command->options) {
380 const char *s = strstr(p->options, node->name);
381 int end = s ? s[strlen(node->name)] : EOF;
383 if (end != '=' && end != ',' && end != ' ' && end != '\0') {
384 vsctl_fatal("'%s' command has no '%s' option",
385 argv[i], node->name);
387 if ((end == '=') != (node->data != NULL)) {
389 vsctl_fatal("missing argument to '%s' option on '%s' "
390 "command", node->name, argv[i]);
392 vsctl_fatal("'%s' option on '%s' does not accept an "
393 "argument", node->name, argv[i]);
398 n_arg = argc - i - 1;
399 if (n_arg < p->min_args) {
400 vsctl_fatal("'%s' command requires at least %d arguments",
401 p->name, p->min_args);
402 } else if (n_arg > p->max_args) {
405 for (j = i + 1; j < argc; j++) {
406 if (argv[j][0] == '-') {
407 vsctl_fatal("'%s' command takes at most %d arguments "
408 "(note that options must precede command "
409 "names and follow a \"--\" argument)",
410 p->name, p->max_args);
414 vsctl_fatal("'%s' command takes at most %d arguments",
415 p->name, p->max_args);
419 command->argc = n_arg + 1;
420 command->argv = &argv[i];
423 /* Returns the "struct vsctl_command_syntax" for a given command 'name', or a
424 * null pointer if there is none. */
425 static const struct vsctl_command_syntax *
426 find_command(const char *name)
428 static struct shash commands = SHASH_INITIALIZER(&commands);
430 if (shash_is_empty(&commands)) {
431 const struct vsctl_command_syntax *p;
433 for (p = all_commands; p->name; p++) {
434 shash_add_assert(&commands, p->name, p);
438 return shash_find_data(&commands, name);
442 vsctl_fatal(const char *format, ...)
447 va_start(args, format);
448 message = xvasprintf(format, args);
451 vlog_set_levels(&VLM_vsctl, VLF_CONSOLE, VLL_EMER);
452 VLOG_ERR("%s", message);
453 ovs_error(0, "%s", message);
454 vsctl_exit(EXIT_FAILURE);
457 /* Frees the current transaction and the underlying IDL and then calls
460 * Freeing the transaction and the IDL is not strictly necessary, but it makes
461 * for a clean memory leak report from valgrind in the normal case. That makes
462 * it easier to notice real memory leaks. */
464 vsctl_exit(int status)
467 ovsdb_idl_txn_abort(the_idl_txn);
468 ovsdb_idl_txn_destroy(the_idl_txn);
470 ovsdb_idl_destroy(the_idl);
478 %s: ovs-vswitchd management utility\n\
479 usage: %s [OPTIONS] COMMAND [ARG...]\n\
481 Open vSwitch commands:\n\
482 init initialize database, if not yet initialized\n\
483 emer-reset reset configuration to clean state\n\
486 add-br BRIDGE create a new bridge named BRIDGE\n\
487 add-br BRIDGE PARENT VLAN create new fake BRIDGE in PARENT on VLAN\n\
488 del-br BRIDGE delete BRIDGE and all of its ports\n\
489 list-br print the names of all the bridges\n\
490 br-exists BRIDGE test whether BRIDGE exists\n\
491 br-to-vlan BRIDGE print the VLAN which BRIDGE is on\n\
492 br-to-parent BRIDGE print the parent of BRIDGE\n\
493 br-set-external-id BRIDGE KEY VALUE set KEY on BRIDGE to VALUE\n\
494 br-set-external-id BRIDGE KEY unset KEY on BRIDGE\n\
495 br-get-external-id BRIDGE KEY print value of KEY on BRIDGE\n\
496 br-get-external-id BRIDGE list key-value pairs on BRIDGE\n\
498 Port commands (a bond is considered to be a single port):\n\
499 list-ports BRIDGE print the names of all the ports on BRIDGE\n\
500 add-port BRIDGE PORT add network device PORT to BRIDGE\n\
501 add-bond BRIDGE PORT IFACE... add bonded port PORT in BRIDGE from IFACES\n\
502 del-port [BRIDGE] PORT delete PORT (which may be bonded) from BRIDGE\n\
503 port-to-br PORT print name of bridge that contains PORT\n\
505 Interface commands (a bond consists of multiple interfaces):\n\
506 list-ifaces BRIDGE print the names of all interfaces on BRIDGE\n\
507 iface-to-br IFACE print name of bridge that contains IFACE\n\
509 Controller commands:\n\
510 get-controller BRIDGE print the controller for BRIDGE\n\
511 del-controller BRIDGE delete the controller for BRIDGE\n\
512 set-controller BRIDGE TARGET set the controller for BRIDGE to TARGET\n\
513 get-fail-mode BRIDGE print the fail-mode for BRIDGE\n\
514 del-fail-mode BRIDGE delete the fail-mode for BRIDGE\n\
515 set-fail-mode BRIDGE MODE set the fail-mode for BRIDGE to MODE\n\
518 get-manager print all manager(s)\n\
519 del-manager delete all manager(s)\n\
520 set-manager TARGET... set the list of manager(s) to TARGET(s)\n\
523 get-ssl print the SSL configuration\n\
524 del-ssl delete the SSL configuration\n\
525 set-ssl PRIV-KEY CERT CA-CERT set the SSL configuration\n\
528 emer-reset reset switch to known good state\n\
530 Database commands:\n\
531 list TBL [REC] list RECord (or all records) in TBL\n\
532 find TBL CONDITION... list records satisfying CONDITION in TBL\n\
533 get TBL REC COL[:KEY] print values of COLumns in RECord in TBL\n\
534 set TBL REC COL[:KEY]=VALUE set COLumn values in RECord in TBL\n\
535 add TBL REC COL [KEY=]VALUE add (KEY=)VALUE to COLumn in RECord in TBL\n\
536 remove TBL REC COL [KEY=]VALUE remove (KEY=)VALUE from COLumn\n\
537 clear TBL REC COL clear values from COLumn in RECord in TBL\n\
538 create TBL COL[:KEY]=VALUE create and initialize new record\n\
539 destroy TBL REC delete RECord from TBL\n\
540 wait-until TBL REC [COL[:KEY]=VALUE] wait until condition is true\n\
541 Potentially unsafe database commands require --force option.\n\
544 --db=DATABASE connect to DATABASE\n\
546 --no-wait do not wait for ovs-vswitchd to reconfigure\n\
547 -t, --timeout=SECS wait at most SECS seconds for ovs-vswitchd\n\
548 --dry-run do not commit changes to database\n\
549 --oneline print exactly one line of output per command\n",
550 program_name, program_name, default_db());
553 --no-syslog equivalent to --verbose=vsctl:syslog:warn\n");
554 stream_usage("database", true, true, false);
557 -h, --help display this help message\n\
558 -V, --version display version information\n");
567 def = xasprintf("unix:%s/db.sock", ovs_rundir());
572 /* Returns true if it looks like this set of arguments might modify the
573 * database, otherwise false. (Not very smart, so it's prone to false
576 might_write_to_db(char **argv)
578 for (; *argv; argv++) {
579 const struct vsctl_command_syntax *p = find_command(*argv);
580 if (p && p->mode == RW) {
587 struct vsctl_context {
591 struct shash options;
593 /* Modifiable state. */
596 struct ovsdb_idl *idl;
597 struct ovsdb_idl_txn *txn;
598 struct ovsdb_symbol_table *symtab;
599 const struct ovsrec_open_vswitch *ovs;
602 /* A command may set this member to true if some prerequisite is not met
603 * and the caller should wait for something to change and then retry. */
607 struct vsctl_bridge {
608 struct ovsrec_bridge *br_cfg;
610 struct ovsrec_controller **ctrl;
613 struct vsctl_bridge *parent;
618 struct ovsrec_port *port_cfg;
619 struct vsctl_bridge *bridge;
623 struct ovsrec_interface *iface_cfg;
624 struct vsctl_port *port;
628 struct vsctl_context *ctx;
629 struct shash bridges; /* Maps from bridge name to struct vsctl_bridge. */
630 struct shash ports; /* Maps from port name to struct vsctl_port. */
631 struct shash ifaces; /* Maps from port name to struct vsctl_iface. */
635 vsctl_context_to_string(const struct vsctl_context *ctx)
637 const struct shash_node *node;
643 SHASH_FOR_EACH (node, &ctx->options) {
644 svec_add(&words, node->name);
646 for (i = 0; i < ctx->argc; i++) {
647 svec_add(&words, ctx->argv[i]);
649 svec_terminate(&words);
651 s = process_escape_args(words.names);
653 svec_destroy(&words);
659 verify_ports(struct vsctl_context *ctx)
661 if (!ctx->verified_ports) {
662 const struct ovsrec_bridge *bridge;
663 const struct ovsrec_port *port;
665 ovsrec_open_vswitch_verify_bridges(ctx->ovs);
666 OVSREC_BRIDGE_FOR_EACH (bridge, ctx->idl) {
667 ovsrec_bridge_verify_ports(bridge);
669 OVSREC_PORT_FOR_EACH (port, ctx->idl) {
670 ovsrec_port_verify_interfaces(port);
673 ctx->verified_ports = true;
677 static struct vsctl_bridge *
678 add_bridge(struct vsctl_info *b,
679 struct ovsrec_bridge *br_cfg, const char *name,
680 struct vsctl_bridge *parent, int vlan)
682 struct vsctl_bridge *br = xmalloc(sizeof *br);
684 br->name = xstrdup(name);
688 br->ctrl = parent->br_cfg->controller;
689 br->n_ctrl = parent->br_cfg->n_controller;
690 br->fail_mode = parent->br_cfg->fail_mode;
692 br->ctrl = br_cfg->controller;
693 br->n_ctrl = br_cfg->n_controller;
694 br->fail_mode = br_cfg->fail_mode;
696 shash_add(&b->bridges, br->name, br);
701 port_is_fake_bridge(const struct ovsrec_port *port_cfg)
703 return (port_cfg->fake_bridge
705 && *port_cfg->tag >= 1 && *port_cfg->tag <= 4095);
708 static struct vsctl_bridge *
709 find_vlan_bridge(struct vsctl_info *info,
710 struct vsctl_bridge *parent, int vlan)
712 struct shash_node *node;
714 SHASH_FOR_EACH (node, &info->bridges) {
715 struct vsctl_bridge *br = node->data;
716 if (br->parent == parent && br->vlan == vlan) {
725 free_info(struct vsctl_info *info)
727 struct shash_node *node;
729 SHASH_FOR_EACH (node, &info->bridges) {
730 struct vsctl_bridge *bridge = node->data;
734 shash_destroy(&info->bridges);
736 shash_destroy_free_data(&info->ports);
737 shash_destroy_free_data(&info->ifaces);
741 pre_get_info(struct vsctl_context *ctx)
743 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_bridges);
745 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_name);
746 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_controller);
747 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_fail_mode);
748 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_ports);
750 ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_name);
751 ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_fake_bridge);
752 ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_tag);
753 ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_interfaces);
755 ovsdb_idl_add_column(ctx->idl, &ovsrec_interface_col_name);
759 get_info(struct vsctl_context *ctx, struct vsctl_info *info)
761 const struct ovsrec_open_vswitch *ovs = ctx->ovs;
762 struct sset bridges, ports;
766 shash_init(&info->bridges);
767 shash_init(&info->ports);
768 shash_init(&info->ifaces);
772 for (i = 0; i < ovs->n_bridges; i++) {
773 struct ovsrec_bridge *br_cfg = ovs->bridges[i];
774 struct vsctl_bridge *br;
777 if (!sset_add(&bridges, br_cfg->name)) {
778 VLOG_WARN("%s: database contains duplicate bridge name",
782 br = add_bridge(info, br_cfg, br_cfg->name, NULL, 0);
787 for (j = 0; j < br_cfg->n_ports; j++) {
788 struct ovsrec_port *port_cfg = br_cfg->ports[j];
790 if (!sset_add(&ports, port_cfg->name)) {
791 VLOG_WARN("%s: database contains duplicate port name",
796 if (port_is_fake_bridge(port_cfg)
797 && sset_add(&bridges, port_cfg->name)) {
798 add_bridge(info, NULL, port_cfg->name, br, *port_cfg->tag);
802 sset_destroy(&bridges);
803 sset_destroy(&ports);
807 for (i = 0; i < ovs->n_bridges; i++) {
808 struct ovsrec_bridge *br_cfg = ovs->bridges[i];
809 struct vsctl_bridge *br;
812 if (!sset_add(&bridges, br_cfg->name)) {
815 br = shash_find_data(&info->bridges, br_cfg->name);
816 for (j = 0; j < br_cfg->n_ports; j++) {
817 struct ovsrec_port *port_cfg = br_cfg->ports[j];
818 struct vsctl_port *port;
821 if (!sset_add(&ports, port_cfg->name)) {
825 if (port_is_fake_bridge(port_cfg)
826 && !sset_add(&bridges, port_cfg->name)) {
830 port = xmalloc(sizeof *port);
831 port->port_cfg = port_cfg;
833 && *port_cfg->tag >= 1 && *port_cfg->tag <= 4095) {
834 port->bridge = find_vlan_bridge(info, br, *port_cfg->tag);
841 shash_add(&info->ports, port_cfg->name, port);
843 for (k = 0; k < port_cfg->n_interfaces; k++) {
844 struct ovsrec_interface *iface_cfg = port_cfg->interfaces[k];
845 struct vsctl_iface *iface;
847 if (shash_find(&info->ifaces, iface_cfg->name)) {
848 VLOG_WARN("%s: database contains duplicate interface name",
853 iface = xmalloc(sizeof *iface);
854 iface->iface_cfg = iface_cfg;
856 shash_add(&info->ifaces, iface_cfg->name, iface);
860 sset_destroy(&bridges);
861 sset_destroy(&ports);
865 check_conflicts(struct vsctl_info *info, const char *name,
868 struct vsctl_iface *iface;
869 struct vsctl_port *port;
871 verify_ports(info->ctx);
873 if (shash_find(&info->bridges, name)) {
874 vsctl_fatal("%s because a bridge named %s already exists",
878 port = shash_find_data(&info->ports, name);
880 vsctl_fatal("%s because a port named %s already exists on "
881 "bridge %s", msg, name, port->bridge->name);
884 iface = shash_find_data(&info->ifaces, name);
886 vsctl_fatal("%s because an interface named %s already exists "
887 "on bridge %s", msg, name, iface->port->bridge->name);
893 static struct vsctl_bridge *
894 find_bridge(struct vsctl_info *info, const char *name, bool must_exist)
896 struct vsctl_bridge *br = shash_find_data(&info->bridges, name);
897 if (must_exist && !br) {
898 vsctl_fatal("no bridge named %s", name);
900 ovsrec_open_vswitch_verify_bridges(info->ctx->ovs);
904 static struct vsctl_bridge *
905 find_real_bridge(struct vsctl_info *info, const char *name, bool must_exist)
907 struct vsctl_bridge *br = find_bridge(info, name, must_exist);
908 if (br && br->parent) {
909 vsctl_fatal("%s is a fake bridge", name);
914 static struct vsctl_port *
915 find_port(struct vsctl_info *info, const char *name, bool must_exist)
917 struct vsctl_port *port = shash_find_data(&info->ports, name);
918 if (port && !strcmp(name, port->bridge->name)) {
921 if (must_exist && !port) {
922 vsctl_fatal("no port named %s", name);
924 verify_ports(info->ctx);
928 static struct vsctl_iface *
929 find_iface(struct vsctl_info *info, const char *name, bool must_exist)
931 struct vsctl_iface *iface = shash_find_data(&info->ifaces, name);
932 if (iface && !strcmp(name, iface->port->bridge->name)) {
935 if (must_exist && !iface) {
936 vsctl_fatal("no interface named %s", name);
938 verify_ports(info->ctx);
943 bridge_insert_port(struct ovsrec_bridge *br, struct ovsrec_port *port)
945 struct ovsrec_port **ports;
948 ports = xmalloc(sizeof *br->ports * (br->n_ports + 1));
949 for (i = 0; i < br->n_ports; i++) {
950 ports[i] = br->ports[i];
952 ports[br->n_ports] = port;
953 ovsrec_bridge_set_ports(br, ports, br->n_ports + 1);
958 bridge_delete_port(struct ovsrec_bridge *br, struct ovsrec_port *port)
960 struct ovsrec_port **ports;
963 ports = xmalloc(sizeof *br->ports * br->n_ports);
964 for (i = n = 0; i < br->n_ports; i++) {
965 if (br->ports[i] != port) {
966 ports[n++] = br->ports[i];
969 ovsrec_bridge_set_ports(br, ports, n);
974 ovs_insert_bridge(const struct ovsrec_open_vswitch *ovs,
975 struct ovsrec_bridge *bridge)
977 struct ovsrec_bridge **bridges;
980 bridges = xmalloc(sizeof *ovs->bridges * (ovs->n_bridges + 1));
981 for (i = 0; i < ovs->n_bridges; i++) {
982 bridges[i] = ovs->bridges[i];
984 bridges[ovs->n_bridges] = bridge;
985 ovsrec_open_vswitch_set_bridges(ovs, bridges, ovs->n_bridges + 1);
990 ovs_delete_bridge(const struct ovsrec_open_vswitch *ovs,
991 struct ovsrec_bridge *bridge)
993 struct ovsrec_bridge **bridges;
996 bridges = xmalloc(sizeof *ovs->bridges * ovs->n_bridges);
997 for (i = n = 0; i < ovs->n_bridges; i++) {
998 if (ovs->bridges[i] != bridge) {
999 bridges[n++] = ovs->bridges[i];
1002 ovsrec_open_vswitch_set_bridges(ovs, bridges, n);
1007 cmd_init(struct vsctl_context *ctx OVS_UNUSED)
1012 pre_cmd_emer_reset(struct vsctl_context *ctx)
1014 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_manager_options);
1015 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_ssl);
1017 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_controller);
1018 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_fail_mode);
1019 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_mirrors);
1020 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_netflow);
1021 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_sflow);
1022 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_flood_vlans);
1023 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_other_config);
1025 ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_other_config);
1027 ovsdb_idl_add_column(ctx->idl,
1028 &ovsrec_interface_col_ingress_policing_rate);
1029 ovsdb_idl_add_column(ctx->idl,
1030 &ovsrec_interface_col_ingress_policing_burst);
1034 cmd_emer_reset(struct vsctl_context *ctx)
1036 const struct ovsdb_idl *idl = ctx->idl;
1037 const struct ovsrec_bridge *br;
1038 const struct ovsrec_port *port;
1039 const struct ovsrec_interface *iface;
1040 const struct ovsrec_mirror *mirror, *next_mirror;
1041 const struct ovsrec_controller *ctrl, *next_ctrl;
1042 const struct ovsrec_manager *mgr, *next_mgr;
1043 const struct ovsrec_netflow *nf, *next_nf;
1044 const struct ovsrec_ssl *ssl, *next_ssl;
1045 const struct ovsrec_sflow *sflow, *next_sflow;
1047 /* Reset the Open_vSwitch table. */
1048 ovsrec_open_vswitch_set_manager_options(ctx->ovs, NULL, 0);
1049 ovsrec_open_vswitch_set_ssl(ctx->ovs, NULL);
1051 OVSREC_BRIDGE_FOR_EACH (br, idl) {
1053 char *hw_key = "hwaddr";
1054 char *hw_val = NULL;
1056 ovsrec_bridge_set_controller(br, NULL, 0);
1057 ovsrec_bridge_set_fail_mode(br, NULL);
1058 ovsrec_bridge_set_mirrors(br, NULL, 0);
1059 ovsrec_bridge_set_netflow(br, NULL);
1060 ovsrec_bridge_set_sflow(br, NULL);
1061 ovsrec_bridge_set_flood_vlans(br, NULL, 0);
1063 /* We only want to save the "hwaddr" key from other_config. */
1064 for (i=0; i < br->n_other_config; i++) {
1065 if (!strcmp(br->key_other_config[i], hw_key)) {
1066 hw_val = br->value_other_config[i];
1071 char *val = xstrdup(hw_val);
1072 ovsrec_bridge_set_other_config(br, &hw_key, &val, 1);
1075 ovsrec_bridge_set_other_config(br, NULL, NULL, 0);
1079 OVSREC_PORT_FOR_EACH (port, idl) {
1080 ovsrec_port_set_other_config(port, NULL, NULL, 0);
1083 OVSREC_INTERFACE_FOR_EACH (iface, idl) {
1084 /* xxx What do we do about gre/patch devices created by mgr? */
1086 ovsrec_interface_set_ingress_policing_rate(iface, 0);
1087 ovsrec_interface_set_ingress_policing_burst(iface, 0);
1090 OVSREC_MIRROR_FOR_EACH_SAFE (mirror, next_mirror, idl) {
1091 ovsrec_mirror_delete(mirror);
1094 OVSREC_CONTROLLER_FOR_EACH_SAFE (ctrl, next_ctrl, idl) {
1095 ovsrec_controller_delete(ctrl);
1098 OVSREC_MANAGER_FOR_EACH_SAFE (mgr, next_mgr, idl) {
1099 ovsrec_manager_delete(mgr);
1102 OVSREC_NETFLOW_FOR_EACH_SAFE (nf, next_nf, idl) {
1103 ovsrec_netflow_delete(nf);
1106 OVSREC_SSL_FOR_EACH_SAFE (ssl, next_ssl, idl) {
1107 ovsrec_ssl_delete(ssl);
1110 OVSREC_SFLOW_FOR_EACH_SAFE (sflow, next_sflow, idl) {
1111 ovsrec_sflow_delete(sflow);
1116 cmd_add_br(struct vsctl_context *ctx)
1118 bool may_exist = shash_find(&ctx->options, "--may-exist") != 0;
1119 const char *br_name, *parent_name;
1120 struct vsctl_info info;
1123 br_name = ctx->argv[1];
1124 if (ctx->argc == 2) {
1127 } else if (ctx->argc == 4) {
1128 parent_name = ctx->argv[2];
1129 vlan = atoi(ctx->argv[3]);
1130 if (vlan < 1 || vlan > 4095) {
1131 vsctl_fatal("%s: vlan must be between 1 and 4095", ctx->argv[0]);
1134 vsctl_fatal("'%s' command takes exactly 1 or 3 arguments",
1138 get_info(ctx, &info);
1140 struct vsctl_bridge *br;
1142 br = find_bridge(&info, br_name, false);
1146 vsctl_fatal("\"--may-exist add-br %s\" but %s is "
1147 "a VLAN bridge for VLAN %d",
1148 br_name, br_name, br->vlan);
1152 vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
1153 "is not a VLAN bridge",
1154 br_name, parent_name, vlan, br_name);
1155 } else if (strcmp(br->parent->name, parent_name)) {
1156 vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
1157 "has the wrong parent %s",
1158 br_name, parent_name, vlan,
1159 br_name, br->parent->name);
1160 } else if (br->vlan != vlan) {
1161 vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
1162 "is a VLAN bridge for the wrong VLAN %d",
1163 br_name, parent_name, vlan, br_name, br->vlan);
1169 check_conflicts(&info, br_name,
1170 xasprintf("cannot create a bridge named %s", br_name));
1173 struct ovsrec_port *port;
1174 struct ovsrec_interface *iface;
1175 struct ovsrec_bridge *br;
1177 iface = ovsrec_interface_insert(ctx->txn);
1178 ovsrec_interface_set_name(iface, br_name);
1179 ovsrec_interface_set_type(iface, "internal");
1181 port = ovsrec_port_insert(ctx->txn);
1182 ovsrec_port_set_name(port, br_name);
1183 ovsrec_port_set_interfaces(port, &iface, 1);
1185 br = ovsrec_bridge_insert(ctx->txn);
1186 ovsrec_bridge_set_name(br, br_name);
1187 ovsrec_bridge_set_ports(br, &port, 1);
1189 ovs_insert_bridge(ctx->ovs, br);
1191 struct vsctl_bridge *parent;
1192 struct ovsrec_port *port;
1193 struct ovsrec_interface *iface;
1194 struct ovsrec_bridge *br;
1197 parent = find_bridge(&info, parent_name, false);
1198 if (parent && parent->vlan) {
1199 vsctl_fatal("cannot create bridge with fake bridge as parent");
1202 vsctl_fatal("parent bridge %s does not exist", parent_name);
1204 br = parent->br_cfg;
1206 iface = ovsrec_interface_insert(ctx->txn);
1207 ovsrec_interface_set_name(iface, br_name);
1208 ovsrec_interface_set_type(iface, "internal");
1210 port = ovsrec_port_insert(ctx->txn);
1211 ovsrec_port_set_name(port, br_name);
1212 ovsrec_port_set_interfaces(port, &iface, 1);
1213 ovsrec_port_set_fake_bridge(port, true);
1214 ovsrec_port_set_tag(port, &tag, 1);
1216 bridge_insert_port(br, port);
1223 del_port(struct vsctl_info *info, struct vsctl_port *port)
1225 struct shash_node *node;
1227 SHASH_FOR_EACH (node, &info->ifaces) {
1228 struct vsctl_iface *iface = node->data;
1229 if (iface->port == port) {
1230 ovsrec_interface_delete(iface->iface_cfg);
1233 ovsrec_port_delete(port->port_cfg);
1235 bridge_delete_port((port->bridge->parent
1236 ? port->bridge->parent->br_cfg
1237 : port->bridge->br_cfg), port->port_cfg);
1241 cmd_del_br(struct vsctl_context *ctx)
1243 bool must_exist = !shash_find(&ctx->options, "--if-exists");
1244 struct vsctl_bridge *bridge;
1245 struct vsctl_info info;
1247 get_info(ctx, &info);
1248 bridge = find_bridge(&info, ctx->argv[1], must_exist);
1250 struct shash_node *node;
1252 SHASH_FOR_EACH (node, &info.ports) {
1253 struct vsctl_port *port = node->data;
1254 if (port->bridge == bridge || port->bridge->parent == bridge
1255 || !strcmp(port->port_cfg->name, bridge->name)) {
1256 del_port(&info, port);
1259 if (bridge->br_cfg) {
1260 ovsrec_bridge_delete(bridge->br_cfg);
1261 ovs_delete_bridge(ctx->ovs, bridge->br_cfg);
1268 output_sorted(struct svec *svec, struct ds *output)
1274 SVEC_FOR_EACH (i, name, svec) {
1275 ds_put_format(output, "%s\n", name);
1280 cmd_list_br(struct vsctl_context *ctx)
1282 struct shash_node *node;
1283 struct vsctl_info info;
1284 struct svec bridges;
1286 get_info(ctx, &info);
1288 svec_init(&bridges);
1289 SHASH_FOR_EACH (node, &info.bridges) {
1290 struct vsctl_bridge *br = node->data;
1291 svec_add(&bridges, br->name);
1293 output_sorted(&bridges, &ctx->output);
1294 svec_destroy(&bridges);
1300 cmd_br_exists(struct vsctl_context *ctx)
1302 struct vsctl_info info;
1304 get_info(ctx, &info);
1305 if (!find_bridge(&info, ctx->argv[1], false)) {
1311 /* Returns true if 'b_prefix' (of length 'b_prefix_len') concatenated with 'b'
1312 * equals 'a', false otherwise. */
1314 key_matches(const char *a,
1315 const char *b_prefix, size_t b_prefix_len, const char *b)
1317 return !strncmp(a, b_prefix, b_prefix_len) && !strcmp(a + b_prefix_len, b);
1321 set_external_id(char **old_keys, char **old_values, size_t old_n,
1322 char *key, char *value,
1323 char ***new_keysp, char ***new_valuesp, size_t *new_np)
1330 new_keys = xmalloc(sizeof *new_keys * (old_n + 1));
1331 new_values = xmalloc(sizeof *new_values * (old_n + 1));
1333 for (i = 0; i < old_n; i++) {
1334 if (strcmp(key, old_keys[i])) {
1335 new_keys[new_n] = old_keys[i];
1336 new_values[new_n] = old_values[i];
1341 new_keys[new_n] = key;
1342 new_values[new_n] = value;
1345 *new_keysp = new_keys;
1346 *new_valuesp = new_values;
1351 pre_cmd_br_set_external_id(struct vsctl_context *ctx)
1354 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_external_ids);
1355 ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_external_ids);
1359 cmd_br_set_external_id(struct vsctl_context *ctx)
1361 struct vsctl_info info;
1362 struct vsctl_bridge *bridge;
1363 char **keys, **values;
1366 get_info(ctx, &info);
1367 bridge = find_bridge(&info, ctx->argv[1], true);
1368 if (bridge->br_cfg) {
1369 set_external_id(bridge->br_cfg->key_external_ids,
1370 bridge->br_cfg->value_external_ids,
1371 bridge->br_cfg->n_external_ids,
1372 ctx->argv[2], ctx->argc >= 4 ? ctx->argv[3] : NULL,
1373 &keys, &values, &n);
1374 ovsrec_bridge_verify_external_ids(bridge->br_cfg);
1375 ovsrec_bridge_set_external_ids(bridge->br_cfg, keys, values, n);
1377 char *key = xasprintf("fake-bridge-%s", ctx->argv[2]);
1378 struct vsctl_port *port = shash_find_data(&info.ports, ctx->argv[1]);
1379 set_external_id(port->port_cfg->key_external_ids,
1380 port->port_cfg->value_external_ids,
1381 port->port_cfg->n_external_ids,
1382 key, ctx->argc >= 4 ? ctx->argv[3] : NULL,
1383 &keys, &values, &n);
1384 ovsrec_port_verify_external_ids(port->port_cfg);
1385 ovsrec_port_set_external_ids(port->port_cfg, keys, values, n);
1395 get_external_id(char **keys, char **values, size_t n,
1396 const char *prefix, const char *key,
1399 size_t prefix_len = strlen(prefix);
1404 for (i = 0; i < n; i++) {
1405 if (!key && !strncmp(keys[i], prefix, prefix_len)) {
1406 svec_add_nocopy(&svec, xasprintf("%s=%s",
1407 keys[i] + prefix_len, values[i]));
1408 } else if (key && key_matches(keys[i], prefix, prefix_len, key)) {
1409 svec_add(&svec, values[i]);
1413 output_sorted(&svec, output);
1414 svec_destroy(&svec);
1418 pre_cmd_br_get_external_id(struct vsctl_context *ctx)
1420 pre_cmd_br_set_external_id(ctx);
1424 cmd_br_get_external_id(struct vsctl_context *ctx)
1426 struct vsctl_info info;
1427 struct vsctl_bridge *bridge;
1429 get_info(ctx, &info);
1430 bridge = find_bridge(&info, ctx->argv[1], true);
1431 if (bridge->br_cfg) {
1432 ovsrec_bridge_verify_external_ids(bridge->br_cfg);
1433 get_external_id(bridge->br_cfg->key_external_ids,
1434 bridge->br_cfg->value_external_ids,
1435 bridge->br_cfg->n_external_ids,
1436 "", ctx->argc >= 3 ? ctx->argv[2] : NULL,
1439 struct vsctl_port *port = shash_find_data(&info.ports, ctx->argv[1]);
1440 ovsrec_port_verify_external_ids(port->port_cfg);
1441 get_external_id(port->port_cfg->key_external_ids,
1442 port->port_cfg->value_external_ids,
1443 port->port_cfg->n_external_ids,
1444 "fake-bridge-", ctx->argc >= 3 ? ctx->argv[2] : NULL, &ctx->output);
1451 cmd_list_ports(struct vsctl_context *ctx)
1453 struct vsctl_bridge *br;
1454 struct shash_node *node;
1455 struct vsctl_info info;
1458 get_info(ctx, &info);
1459 br = find_bridge(&info, ctx->argv[1], true);
1460 ovsrec_bridge_verify_ports(br->br_cfg ? br->br_cfg : br->parent->br_cfg);
1463 SHASH_FOR_EACH (node, &info.ports) {
1464 struct vsctl_port *port = node->data;
1466 if (strcmp(port->port_cfg->name, br->name) && br == port->bridge) {
1467 svec_add(&ports, port->port_cfg->name);
1470 output_sorted(&ports, &ctx->output);
1471 svec_destroy(&ports);
1477 add_port(struct vsctl_context *ctx,
1478 const char *br_name, const char *port_name,
1479 bool may_exist, bool fake_iface,
1480 char *iface_names[], int n_ifaces,
1481 char *settings[], int n_settings)
1483 struct vsctl_info info;
1484 struct vsctl_bridge *bridge;
1485 struct ovsrec_interface **ifaces;
1486 struct ovsrec_port *port;
1489 get_info(ctx, &info);
1491 struct vsctl_port *vsctl_port;
1493 vsctl_port = find_port(&info, port_name, false);
1495 struct svec want_names, have_names;
1497 svec_init(&want_names);
1498 for (i = 0; i < n_ifaces; i++) {
1499 svec_add(&want_names, iface_names[i]);
1501 svec_sort(&want_names);
1503 svec_init(&have_names);
1504 for (i = 0; i < vsctl_port->port_cfg->n_interfaces; i++) {
1505 svec_add(&have_names,
1506 vsctl_port->port_cfg->interfaces[i]->name);
1508 svec_sort(&have_names);
1510 if (strcmp(vsctl_port->bridge->name, br_name)) {
1511 char *command = vsctl_context_to_string(ctx);
1512 vsctl_fatal("\"%s\" but %s is actually attached to bridge %s",
1513 command, port_name, vsctl_port->bridge->name);
1516 if (!svec_equal(&want_names, &have_names)) {
1517 char *have_names_string = svec_join(&have_names, ", ", "");
1518 char *command = vsctl_context_to_string(ctx);
1520 vsctl_fatal("\"%s\" but %s actually has interface(s) %s",
1521 command, port_name, have_names_string);
1524 svec_destroy(&want_names);
1525 svec_destroy(&have_names);
1530 check_conflicts(&info, port_name,
1531 xasprintf("cannot create a port named %s", port_name));
1532 for (i = 0; i < n_ifaces; i++) {
1533 check_conflicts(&info, iface_names[i],
1534 xasprintf("cannot create an interface named %s",
1537 bridge = find_bridge(&info, br_name, true);
1539 ifaces = xmalloc(n_ifaces * sizeof *ifaces);
1540 for (i = 0; i < n_ifaces; i++) {
1541 ifaces[i] = ovsrec_interface_insert(ctx->txn);
1542 ovsrec_interface_set_name(ifaces[i], iface_names[i]);
1545 port = ovsrec_port_insert(ctx->txn);
1546 ovsrec_port_set_name(port, port_name);
1547 ovsrec_port_set_interfaces(port, ifaces, n_ifaces);
1548 ovsrec_port_set_bond_fake_iface(port, fake_iface);
1552 int64_t tag = bridge->vlan;
1553 ovsrec_port_set_tag(port, &tag, 1);
1556 for (i = 0; i < n_settings; i++) {
1557 set_column(get_table("Port"), &port->header_, settings[i],
1561 bridge_insert_port((bridge->parent ? bridge->parent->br_cfg
1562 : bridge->br_cfg), port);
1568 cmd_add_port(struct vsctl_context *ctx)
1570 bool may_exist = shash_find(&ctx->options, "--may-exist") != 0;
1572 add_port(ctx, ctx->argv[1], ctx->argv[2], may_exist, false,
1573 &ctx->argv[2], 1, &ctx->argv[3], ctx->argc - 3);
1577 cmd_add_bond(struct vsctl_context *ctx)
1579 bool may_exist = shash_find(&ctx->options, "--may-exist") != 0;
1580 bool fake_iface = shash_find(&ctx->options, "--fake-iface");
1584 n_ifaces = ctx->argc - 3;
1585 for (i = 3; i < ctx->argc; i++) {
1586 if (strchr(ctx->argv[i], '=')) {
1592 vsctl_fatal("add-bond requires at least 2 interfaces, but only "
1593 "%d were specified", n_ifaces);
1596 add_port(ctx, ctx->argv[1], ctx->argv[2], may_exist, fake_iface,
1597 &ctx->argv[3], n_ifaces,
1598 &ctx->argv[n_ifaces + 3], ctx->argc - 3 - n_ifaces);
1602 cmd_del_port(struct vsctl_context *ctx)
1604 bool must_exist = !shash_find(&ctx->options, "--if-exists");
1605 bool with_iface = shash_find(&ctx->options, "--with-iface") != NULL;
1606 struct vsctl_port *port;
1607 struct vsctl_info info;
1609 get_info(ctx, &info);
1611 port = find_port(&info, ctx->argv[ctx->argc - 1], must_exist);
1613 const char *target = ctx->argv[ctx->argc - 1];
1614 struct vsctl_iface *iface;
1616 port = find_port(&info, target, false);
1618 iface = find_iface(&info, target, false);
1623 if (must_exist && !port) {
1624 vsctl_fatal("no port or interface named %s", target);
1629 if (ctx->argc == 3) {
1630 struct vsctl_bridge *bridge;
1632 bridge = find_bridge(&info, ctx->argv[1], true);
1633 if (port->bridge != bridge) {
1634 if (port->bridge->parent == bridge) {
1635 vsctl_fatal("bridge %s does not have a port %s (although "
1636 "its parent bridge %s does)",
1637 ctx->argv[1], ctx->argv[2],
1638 bridge->parent->name);
1640 vsctl_fatal("bridge %s does not have a port %s",
1641 ctx->argv[1], ctx->argv[2]);
1646 del_port(&info, port);
1653 cmd_port_to_br(struct vsctl_context *ctx)
1655 struct vsctl_port *port;
1656 struct vsctl_info info;
1658 get_info(ctx, &info);
1659 port = find_port(&info, ctx->argv[1], true);
1660 ds_put_format(&ctx->output, "%s\n", port->bridge->name);
1665 cmd_br_to_vlan(struct vsctl_context *ctx)
1667 struct vsctl_bridge *bridge;
1668 struct vsctl_info info;
1670 get_info(ctx, &info);
1671 bridge = find_bridge(&info, ctx->argv[1], true);
1672 ds_put_format(&ctx->output, "%d\n", bridge->vlan);
1677 cmd_br_to_parent(struct vsctl_context *ctx)
1679 struct vsctl_bridge *bridge;
1680 struct vsctl_info info;
1682 get_info(ctx, &info);
1683 bridge = find_bridge(&info, ctx->argv[1], true);
1684 if (bridge->parent) {
1685 bridge = bridge->parent;
1687 ds_put_format(&ctx->output, "%s\n", bridge->name);
1692 cmd_list_ifaces(struct vsctl_context *ctx)
1694 struct vsctl_bridge *br;
1695 struct shash_node *node;
1696 struct vsctl_info info;
1699 get_info(ctx, &info);
1700 br = find_bridge(&info, ctx->argv[1], true);
1704 SHASH_FOR_EACH (node, &info.ifaces) {
1705 struct vsctl_iface *iface = node->data;
1707 if (strcmp(iface->iface_cfg->name, br->name)
1708 && br == iface->port->bridge) {
1709 svec_add(&ifaces, iface->iface_cfg->name);
1712 output_sorted(&ifaces, &ctx->output);
1713 svec_destroy(&ifaces);
1719 cmd_iface_to_br(struct vsctl_context *ctx)
1721 struct vsctl_iface *iface;
1722 struct vsctl_info info;
1724 get_info(ctx, &info);
1725 iface = find_iface(&info, ctx->argv[1], true);
1726 ds_put_format(&ctx->output, "%s\n", iface->port->bridge->name);
1731 verify_controllers(struct ovsrec_bridge *bridge)
1736 ovsrec_bridge_verify_controller(bridge);
1737 for (i = 0; i < bridge->n_controller; i++) {
1738 ovsrec_controller_verify_target(bridge->controller[i]);
1744 pre_controller(struct vsctl_context *ctx)
1748 ovsdb_idl_add_column(ctx->idl, &ovsrec_controller_col_target);
1752 cmd_get_controller(struct vsctl_context *ctx)
1754 struct vsctl_info info;
1755 struct vsctl_bridge *br;
1756 struct svec targets;
1759 get_info(ctx, &info);
1760 br = find_bridge(&info, ctx->argv[1], true);
1761 verify_controllers(br->br_cfg);
1763 /* Print the targets in sorted order for reproducibility. */
1764 svec_init(&targets);
1765 for (i = 0; i < br->n_ctrl; i++) {
1766 svec_add(&targets, br->ctrl[i]->target);
1769 svec_sort(&targets);
1770 for (i = 0; i < targets.n; i++) {
1771 ds_put_format(&ctx->output, "%s\n", targets.names[i]);
1773 svec_destroy(&targets);
1779 delete_controllers(struct ovsrec_controller **controllers,
1780 size_t n_controllers)
1784 for (i = 0; i < n_controllers; i++) {
1785 ovsrec_controller_delete(controllers[i]);
1790 cmd_del_controller(struct vsctl_context *ctx)
1792 struct vsctl_info info;
1793 struct vsctl_bridge *br;
1795 get_info(ctx, &info);
1797 br = find_real_bridge(&info, ctx->argv[1], true);
1798 verify_controllers(br->br_cfg);
1801 delete_controllers(br->ctrl, br->n_ctrl);
1802 ovsrec_bridge_set_controller(br->br_cfg, NULL, 0);
1808 static struct ovsrec_controller **
1809 insert_controllers(struct ovsdb_idl_txn *txn, char *targets[], size_t n)
1811 struct ovsrec_controller **controllers;
1814 controllers = xmalloc(n * sizeof *controllers);
1815 for (i = 0; i < n; i++) {
1816 controllers[i] = ovsrec_controller_insert(txn);
1817 ovsrec_controller_set_target(controllers[i], targets[i]);
1824 cmd_set_controller(struct vsctl_context *ctx)
1826 struct vsctl_info info;
1827 struct vsctl_bridge *br;
1828 struct ovsrec_controller **controllers;
1831 get_info(ctx, &info);
1832 br = find_real_bridge(&info, ctx->argv[1], true);
1833 verify_controllers(br->br_cfg);
1835 delete_controllers(br->ctrl, br->n_ctrl);
1838 controllers = insert_controllers(ctx->txn, &ctx->argv[2], n);
1839 ovsrec_bridge_set_controller(br->br_cfg, controllers, n);
1846 cmd_get_fail_mode(struct vsctl_context *ctx)
1848 struct vsctl_info info;
1849 struct vsctl_bridge *br;
1851 get_info(ctx, &info);
1852 br = find_bridge(&info, ctx->argv[1], true);
1855 ovsrec_bridge_verify_fail_mode(br->br_cfg);
1857 if (br->fail_mode && strlen(br->fail_mode)) {
1858 ds_put_format(&ctx->output, "%s\n", br->fail_mode);
1865 cmd_del_fail_mode(struct vsctl_context *ctx)
1867 struct vsctl_info info;
1868 struct vsctl_bridge *br;
1870 get_info(ctx, &info);
1871 br = find_real_bridge(&info, ctx->argv[1], true);
1873 ovsrec_bridge_set_fail_mode(br->br_cfg, NULL);
1879 cmd_set_fail_mode(struct vsctl_context *ctx)
1881 struct vsctl_info info;
1882 struct vsctl_bridge *br;
1883 const char *fail_mode = ctx->argv[2];
1885 get_info(ctx, &info);
1886 br = find_real_bridge(&info, ctx->argv[1], true);
1888 if (strcmp(fail_mode, "standalone") && strcmp(fail_mode, "secure")) {
1889 vsctl_fatal("fail-mode must be \"standalone\" or \"secure\"");
1892 ovsrec_bridge_set_fail_mode(br->br_cfg, fail_mode);
1898 verify_managers(const struct ovsrec_open_vswitch *ovs)
1902 ovsrec_open_vswitch_verify_manager_options(ovs);
1904 for (i = 0; i < ovs->n_manager_options; ++i) {
1905 const struct ovsrec_manager *mgr = ovs->manager_options[i];
1907 ovsrec_manager_verify_target(mgr);
1912 pre_manager(struct vsctl_context *ctx)
1914 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_manager_options);
1915 ovsdb_idl_add_column(ctx->idl, &ovsrec_manager_col_target);
1919 cmd_get_manager(struct vsctl_context *ctx)
1921 const struct ovsrec_open_vswitch *ovs = ctx->ovs;
1922 struct svec targets;
1925 verify_managers(ovs);
1927 /* Print the targets in sorted order for reproducibility. */
1928 svec_init(&targets);
1930 for (i = 0; i < ovs->n_manager_options; i++) {
1931 svec_add(&targets, ovs->manager_options[i]->target);
1934 svec_sort_unique(&targets);
1935 for (i = 0; i < targets.n; i++) {
1936 ds_put_format(&ctx->output, "%s\n", targets.names[i]);
1938 svec_destroy(&targets);
1942 delete_managers(const struct vsctl_context *ctx)
1944 const struct ovsrec_open_vswitch *ovs = ctx->ovs;
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 /* Insert each manager in a new row in Manager table. */
1972 managers = xmalloc(n * sizeof *managers);
1973 for (i = 0; i < n; i++) {
1974 managers[i] = ovsrec_manager_insert(ctx->txn);
1975 ovsrec_manager_set_target(managers[i], targets[i]);
1978 /* Store uuids of new Manager rows in 'manager_options' column. */
1979 ovsrec_open_vswitch_set_manager_options(ctx->ovs, managers, n);
1984 cmd_set_manager(struct vsctl_context *ctx)
1986 const size_t n = ctx->argc - 1;
1988 verify_managers(ctx->ovs);
1989 delete_managers(ctx);
1990 insert_managers(ctx, &ctx->argv[1], n);
1994 pre_cmd_get_ssl(struct vsctl_context *ctx)
1996 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_ssl);
1998 ovsdb_idl_add_column(ctx->idl, &ovsrec_ssl_col_private_key);
1999 ovsdb_idl_add_column(ctx->idl, &ovsrec_ssl_col_certificate);
2000 ovsdb_idl_add_column(ctx->idl, &ovsrec_ssl_col_ca_cert);
2001 ovsdb_idl_add_column(ctx->idl, &ovsrec_ssl_col_bootstrap_ca_cert);
2005 cmd_get_ssl(struct vsctl_context *ctx)
2007 struct ovsrec_ssl *ssl = ctx->ovs->ssl;
2009 ovsrec_open_vswitch_verify_ssl(ctx->ovs);
2011 ovsrec_ssl_verify_private_key(ssl);
2012 ovsrec_ssl_verify_certificate(ssl);
2013 ovsrec_ssl_verify_ca_cert(ssl);
2014 ovsrec_ssl_verify_bootstrap_ca_cert(ssl);
2016 ds_put_format(&ctx->output, "Private key: %s\n", ssl->private_key);
2017 ds_put_format(&ctx->output, "Certificate: %s\n", ssl->certificate);
2018 ds_put_format(&ctx->output, "CA Certificate: %s\n", ssl->ca_cert);
2019 ds_put_format(&ctx->output, "Bootstrap: %s\n",
2020 ssl->bootstrap_ca_cert ? "true" : "false");
2025 pre_cmd_del_ssl(struct vsctl_context *ctx)
2027 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_ssl);
2031 cmd_del_ssl(struct vsctl_context *ctx)
2033 struct ovsrec_ssl *ssl = ctx->ovs->ssl;
2036 ovsrec_open_vswitch_verify_ssl(ctx->ovs);
2037 ovsrec_ssl_delete(ssl);
2038 ovsrec_open_vswitch_set_ssl(ctx->ovs, NULL);
2043 pre_cmd_set_ssl(struct vsctl_context *ctx)
2045 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_ssl);
2049 cmd_set_ssl(struct vsctl_context *ctx)
2051 bool bootstrap = shash_find(&ctx->options, "--bootstrap");
2052 struct ovsrec_ssl *ssl = ctx->ovs->ssl;
2054 ovsrec_open_vswitch_verify_ssl(ctx->ovs);
2056 ovsrec_ssl_delete(ssl);
2058 ssl = ovsrec_ssl_insert(ctx->txn);
2060 ovsrec_ssl_set_private_key(ssl, ctx->argv[1]);
2061 ovsrec_ssl_set_certificate(ssl, ctx->argv[2]);
2062 ovsrec_ssl_set_ca_cert(ssl, ctx->argv[3]);
2064 ovsrec_ssl_set_bootstrap_ca_cert(ssl, bootstrap);
2066 ovsrec_open_vswitch_set_ssl(ctx->ovs, ssl);
2069 /* Parameter commands. */
2071 struct vsctl_row_id {
2072 const struct ovsdb_idl_table_class *table;
2073 const struct ovsdb_idl_column *name_column;
2074 const struct ovsdb_idl_column *uuid_column;
2077 struct vsctl_table_class {
2078 struct ovsdb_idl_table_class *class;
2079 struct vsctl_row_id row_ids[2];
2082 static const struct vsctl_table_class tables[] = {
2083 {&ovsrec_table_bridge,
2084 {{&ovsrec_table_bridge, &ovsrec_bridge_col_name, NULL},
2085 {NULL, NULL, NULL}}},
2087 {&ovsrec_table_controller,
2088 {{&ovsrec_table_bridge,
2089 &ovsrec_bridge_col_name,
2090 &ovsrec_bridge_col_controller}}},
2092 {&ovsrec_table_interface,
2093 {{&ovsrec_table_interface, &ovsrec_interface_col_name, NULL},
2094 {NULL, NULL, NULL}}},
2096 {&ovsrec_table_mirror,
2097 {{&ovsrec_table_mirror, &ovsrec_mirror_col_name, NULL},
2098 {NULL, NULL, NULL}}},
2100 {&ovsrec_table_manager,
2101 {{&ovsrec_table_manager, &ovsrec_manager_col_target, NULL},
2102 {NULL, NULL, NULL}}},
2104 {&ovsrec_table_netflow,
2105 {{&ovsrec_table_bridge,
2106 &ovsrec_bridge_col_name,
2107 &ovsrec_bridge_col_netflow},
2108 {NULL, NULL, NULL}}},
2110 {&ovsrec_table_open_vswitch,
2111 {{&ovsrec_table_open_vswitch, NULL, NULL},
2112 {NULL, NULL, NULL}}},
2114 {&ovsrec_table_port,
2115 {{&ovsrec_table_port, &ovsrec_port_col_name, NULL},
2116 {NULL, NULL, NULL}}},
2119 {{&ovsrec_table_port, &ovsrec_port_col_name, &ovsrec_port_col_qos},
2120 {NULL, NULL, NULL}}},
2122 {&ovsrec_table_monitor,
2123 {{&ovsrec_table_interface,
2124 &ovsrec_interface_col_name,
2125 &ovsrec_interface_col_monitor},
2126 {NULL, NULL, NULL}}},
2128 {&ovsrec_table_maintenance_point,
2129 {{NULL, NULL, NULL},
2130 {NULL, NULL, NULL}}},
2132 {&ovsrec_table_queue,
2133 {{NULL, NULL, NULL},
2134 {NULL, NULL, NULL}}},
2137 {{&ovsrec_table_open_vswitch, NULL, &ovsrec_open_vswitch_col_ssl}}},
2139 {&ovsrec_table_sflow,
2140 {{&ovsrec_table_bridge,
2141 &ovsrec_bridge_col_name,
2142 &ovsrec_bridge_col_sflow},
2143 {NULL, NULL, NULL}}},
2145 {NULL, {{NULL, NULL, NULL}, {NULL, NULL, NULL}}}
2149 die_if_error(char *error)
2152 vsctl_fatal("%s", error);
2157 to_lower_and_underscores(unsigned c)
2159 return c == '-' ? '_' : tolower(c);
2163 score_partial_match(const char *name, const char *s)
2167 if (!strcmp(name, s)) {
2170 for (score = 0; ; score++, name++, s++) {
2171 if (to_lower_and_underscores(*name) != to_lower_and_underscores(*s)) {
2173 } else if (*name == '\0') {
2174 return UINT_MAX - 1;
2177 return *s == '\0' ? score : 0;
2180 static const struct vsctl_table_class *
2181 get_table(const char *table_name)
2183 const struct vsctl_table_class *table;
2184 const struct vsctl_table_class *best_match = NULL;
2185 unsigned int best_score = 0;
2187 for (table = tables; table->class; table++) {
2188 unsigned int score = score_partial_match(table->class->name,
2190 if (score > best_score) {
2193 } else if (score == best_score) {
2199 } else if (best_score) {
2200 vsctl_fatal("multiple table names match \"%s\"", table_name);
2202 vsctl_fatal("unknown table \"%s\"", table_name);
2206 static const struct vsctl_table_class *
2207 pre_get_table(struct vsctl_context *ctx, const char *table_name)
2209 const struct vsctl_table_class *table_class;
2212 table_class = get_table(table_name);
2213 ovsdb_idl_add_table(ctx->idl, table_class->class);
2215 for (i = 0; i < ARRAY_SIZE(table_class->row_ids); i++) {
2216 const struct vsctl_row_id *id = &table_class->row_ids[i];
2218 ovsdb_idl_add_table(ctx->idl, id->table);
2220 if (id->name_column) {
2221 ovsdb_idl_add_column(ctx->idl, id->name_column);
2223 if (id->uuid_column) {
2224 ovsdb_idl_add_column(ctx->idl, id->uuid_column);
2231 static const struct ovsdb_idl_row *
2232 get_row_by_id(struct vsctl_context *ctx, const struct vsctl_table_class *table,
2233 const struct vsctl_row_id *id, const char *record_id)
2235 const struct ovsdb_idl_row *referrer, *final;
2241 if (!id->name_column) {
2242 if (strcmp(record_id, ".")) {
2245 referrer = ovsdb_idl_first_row(ctx->idl, id->table);
2246 if (!referrer || ovsdb_idl_next_row(referrer)) {
2250 const struct ovsdb_idl_row *row;
2253 for (row = ovsdb_idl_first_row(ctx->idl, id->table);
2255 row = ovsdb_idl_next_row(row))
2257 const struct ovsdb_datum *name;
2259 name = ovsdb_idl_get(row, id->name_column,
2260 OVSDB_TYPE_STRING, OVSDB_TYPE_VOID);
2261 if (name->n == 1 && !strcmp(name->keys[0].string, record_id)) {
2263 vsctl_fatal("multiple rows in %s match \"%s\"",
2264 table->class->name, record_id);
2275 if (id->uuid_column) {
2276 const struct ovsdb_datum *uuid;
2278 ovsdb_idl_txn_verify(referrer, id->uuid_column);
2279 uuid = ovsdb_idl_get(referrer, id->uuid_column,
2280 OVSDB_TYPE_UUID, OVSDB_TYPE_VOID);
2282 final = ovsdb_idl_get_row_for_uuid(ctx->idl, table->class,
2283 &uuid->keys[0].uuid);
2292 static const struct ovsdb_idl_row *
2293 get_row (struct vsctl_context *ctx,
2294 const struct vsctl_table_class *table, const char *record_id)
2296 const struct ovsdb_idl_row *row;
2299 if (uuid_from_string(&uuid, record_id)) {
2300 row = ovsdb_idl_get_row_for_uuid(ctx->idl, table->class, &uuid);
2304 for (i = 0; i < ARRAY_SIZE(table->row_ids); i++) {
2305 row = get_row_by_id(ctx, table, &table->row_ids[i], record_id);
2314 static const struct ovsdb_idl_row *
2315 must_get_row(struct vsctl_context *ctx,
2316 const struct vsctl_table_class *table, const char *record_id)
2318 const struct ovsdb_idl_row *row = get_row(ctx, table, record_id);
2320 vsctl_fatal("no row \"%s\" in table %s",
2321 record_id, table->class->name);
2327 get_column(const struct vsctl_table_class *table, const char *column_name,
2328 const struct ovsdb_idl_column **columnp)
2330 const struct ovsdb_idl_column *best_match = NULL;
2331 unsigned int best_score = 0;
2334 for (i = 0; i < table->class->n_columns; i++) {
2335 const struct ovsdb_idl_column *column = &table->class->columns[i];
2336 unsigned int score = score_partial_match(column->name, column_name);
2337 if (score > best_score) {
2338 best_match = column;
2340 } else if (score == best_score) {
2345 *columnp = best_match;
2348 } else if (best_score) {
2349 return xasprintf("%s contains more than one column whose name "
2350 "matches \"%s\"", table->class->name, column_name);
2352 return xasprintf("%s does not contain a column whose name matches "
2353 "\"%s\"", table->class->name, column_name);
2357 static struct ovsdb_symbol *
2358 create_symbol(struct ovsdb_symbol_table *symtab, const char *id, bool *newp)
2360 struct ovsdb_symbol *symbol;
2363 vsctl_fatal("row id \"%s\" does not begin with \"@\"", id);
2367 *newp = ovsdb_symbol_table_get(symtab, id) == NULL;
2370 symbol = ovsdb_symbol_table_insert(symtab, id);
2371 if (symbol->created) {
2372 vsctl_fatal("row id \"%s\" may only be specified on one --id option",
2375 symbol->created = true;
2380 pre_get_column(struct vsctl_context *ctx,
2381 const struct vsctl_table_class *table, const char *column_name,
2382 const struct ovsdb_idl_column **columnp)
2384 die_if_error(get_column(table, column_name, columnp));
2385 ovsdb_idl_add_column(ctx->idl, *columnp);
2389 missing_operator_error(const char *arg, const char **allowed_operators,
2395 ds_put_format(&s, "%s: argument does not end in ", arg);
2396 ds_put_format(&s, "\"%s\"", allowed_operators[0]);
2397 if (n_allowed == 2) {
2398 ds_put_format(&s, " or \"%s\"", allowed_operators[1]);
2399 } else if (n_allowed > 2) {
2402 for (i = 1; i < n_allowed - 1; i++) {
2403 ds_put_format(&s, ", \"%s\"", allowed_operators[i]);
2405 ds_put_format(&s, ", or \"%s\"", allowed_operators[i]);
2407 ds_put_format(&s, " followed by a value.");
2409 return ds_steal_cstr(&s);
2412 /* Breaks 'arg' apart into a number of fields in the following order:
2414 * - The name of a column in 'table', stored into '*columnp'. The column
2415 * name may be abbreviated.
2417 * - Optionally ':' followed by a key string. The key is stored as a
2418 * malloc()'d string into '*keyp', or NULL if no key is present in
2421 * - If 'valuep' is nonnull, an operator followed by a value string. The
2422 * allowed operators are the 'n_allowed' string in 'allowed_operators',
2423 * or just "=" if 'n_allowed' is 0. If 'operatorp' is nonnull, then the
2424 * operator is stored into '*operatorp' (one of the pointers from
2425 * 'allowed_operators' is stored; nothing is malloc()'d). The value is
2426 * stored as a malloc()'d string into '*valuep', or NULL if no value is
2429 * On success, returns NULL. On failure, returned a malloc()'d string error
2430 * message and stores NULL into all of the nonnull output arguments. */
2431 static char * WARN_UNUSED_RESULT
2432 parse_column_key_value(const char *arg,
2433 const struct vsctl_table_class *table,
2434 const struct ovsdb_idl_column **columnp, char **keyp,
2435 const char **operatorp,
2436 const char **allowed_operators, size_t n_allowed,
2439 const char *p = arg;
2443 assert(!(operatorp && !valuep));
2449 /* Parse column name. */
2450 error = ovsdb_token_parse(&p, &column_name);
2454 if (column_name[0] == '\0') {
2456 error = xasprintf("%s: missing column name", arg);
2459 error = get_column(table, column_name, columnp);
2465 /* Parse key string. */
2468 error = ovsdb_token_parse(&p, keyp);
2474 /* Parse value string. */
2480 if (!allowed_operators) {
2481 static const char *equals = "=";
2482 allowed_operators = =
2488 for (i = 0; i < n_allowed; i++) {
2489 const char *op = allowed_operators[i];
2490 size_t op_len = strlen(op);
2492 if (op_len > best_len && !strncmp(op, p, op_len) && p[op_len]) {
2498 error = missing_operator_error(arg, allowed_operators, n_allowed);
2505 *valuep = xstrdup(p + best_len);
2508 error = xasprintf("%s: trailing garbage \"%s\" in argument",
2530 pre_parse_column_key_value(struct vsctl_context *ctx,
2532 const struct vsctl_table_class *table)
2534 const struct ovsdb_idl_column *column;
2539 die_if_error(ovsdb_token_parse(&p, &column_name));
2540 if (column_name[0] == '\0') {
2541 vsctl_fatal("%s: missing column name", arg);
2544 pre_get_column(ctx, table, column_name, &column);
2549 pre_cmd_get(struct vsctl_context *ctx)
2551 const char *id = shash_find_data(&ctx->options, "--id");
2552 const char *table_name = ctx->argv[1];
2553 const struct vsctl_table_class *table;
2556 /* Using "get" without --id or a column name could possibly make sense.
2557 * Maybe, for example, a ovs-vsctl run wants to assert that a row exists.
2558 * But it is unlikely that an interactive user would want to do that, so
2559 * issue a warning if we're running on a terminal. */
2560 if (!id && ctx->argc <= 3 && isatty(STDOUT_FILENO)) {
2561 VLOG_WARN("\"get\" command without row arguments or \"--id\" is "
2562 "possibly erroneous");
2565 table = pre_get_table(ctx, table_name);
2566 for (i = 3; i < ctx->argc; i++) {
2567 if (!strcasecmp(ctx->argv[i], "_uuid")
2568 || !strcasecmp(ctx->argv[i], "-uuid")) {
2572 pre_parse_column_key_value(ctx, ctx->argv[i], table);
2577 cmd_get(struct vsctl_context *ctx)
2579 const char *id = shash_find_data(&ctx->options, "--id");
2580 bool if_exists = shash_find(&ctx->options, "--if-exists");
2581 const char *table_name = ctx->argv[1];
2582 const char *record_id = ctx->argv[2];
2583 const struct vsctl_table_class *table;
2584 const struct ovsdb_idl_row *row;
2585 struct ds *out = &ctx->output;
2588 table = get_table(table_name);
2589 row = must_get_row(ctx, table, record_id);
2591 struct ovsdb_symbol *symbol;
2594 symbol = create_symbol(ctx->symtab, id, &new);
2596 vsctl_fatal("row id \"%s\" specified on \"get\" command was used "
2597 "before it was defined", id);
2599 symbol->uuid = row->uuid;
2601 /* This symbol refers to a row that already exists, so disable warnings
2602 * about it being unreferenced. */
2603 symbol->strong_ref = true;
2605 for (i = 3; i < ctx->argc; i++) {
2606 const struct ovsdb_idl_column *column;
2607 const struct ovsdb_datum *datum;
2610 /* Special case for obtaining the UUID of a row. We can't just do this
2611 * through parse_column_key_value() below since it returns a "struct
2612 * ovsdb_idl_column" and the UUID column doesn't have one. */
2613 if (!strcasecmp(ctx->argv[i], "_uuid")
2614 || !strcasecmp(ctx->argv[i], "-uuid")) {
2615 ds_put_format(out, UUID_FMT"\n", UUID_ARGS(&row->uuid));
2619 die_if_error(parse_column_key_value(ctx->argv[i], table,
2620 &column, &key_string,
2621 NULL, NULL, 0, NULL));
2623 ovsdb_idl_txn_verify(row, column);
2624 datum = ovsdb_idl_read(row, column);
2626 union ovsdb_atom key;
2629 if (column->type.value.type == OVSDB_TYPE_VOID) {
2630 vsctl_fatal("cannot specify key to get for non-map column %s",
2634 die_if_error(ovsdb_atom_from_string(&key,
2636 key_string, ctx->symtab));
2638 idx = ovsdb_datum_find_key(datum, &key,
2639 column->type.key.type);
2640 if (idx == UINT_MAX) {
2642 vsctl_fatal("no key \"%s\" in %s record \"%s\" column %s",
2643 key_string, table->class->name, record_id,
2647 ovsdb_atom_to_string(&datum->values[idx],
2648 column->type.value.type, out);
2650 ovsdb_atom_destroy(&key, column->type.key.type);
2652 ovsdb_datum_to_string(datum, &column->type, out);
2654 ds_put_char(out, '\n');
2661 parse_column_names(const char *column_names,
2662 const struct vsctl_table_class *table,
2663 const struct ovsdb_idl_column ***columnsp,
2666 const struct ovsdb_idl_column **columns;
2669 if (!column_names) {
2672 n_columns = table->class->n_columns + 1;
2673 columns = xmalloc(n_columns * sizeof *columns);
2675 for (i = 0; i < table->class->n_columns; i++) {
2676 columns[i + 1] = &table->class->columns[i];
2679 char *s = xstrdup(column_names);
2680 size_t allocated_columns;
2681 char *save_ptr = NULL;
2685 allocated_columns = n_columns = 0;
2686 for (column_name = strtok_r(s, ", ", &save_ptr); column_name;
2687 column_name = strtok_r(NULL, ", ", &save_ptr)) {
2688 const struct ovsdb_idl_column *column;
2690 if (!strcasecmp(column_name, "_uuid")) {
2693 die_if_error(get_column(table, column_name, &column));
2695 if (n_columns >= allocated_columns) {
2696 columns = x2nrealloc(columns, &allocated_columns,
2699 columns[n_columns++] = column;
2704 vsctl_fatal("must specify at least one column name");
2707 *columnsp = columns;
2708 *n_columnsp = n_columns;
2713 pre_list_columns(struct vsctl_context *ctx,
2714 const struct vsctl_table_class *table,
2715 const char *column_names)
2717 const struct ovsdb_idl_column **columns;
2721 parse_column_names(column_names, table, &columns, &n_columns);
2722 for (i = 0; i < n_columns; i++) {
2724 ovsdb_idl_add_column(ctx->idl, columns[i]);
2731 pre_cmd_list(struct vsctl_context *ctx)
2733 const char *column_names = shash_find_data(&ctx->options, "--columns");
2734 const char *table_name = ctx->argv[1];
2735 const struct vsctl_table_class *table;
2737 table = pre_get_table(ctx, table_name);
2738 pre_list_columns(ctx, table, column_names);
2741 static struct table *
2742 list_make_table(const struct ovsdb_idl_column **columns, size_t n_columns)
2747 out = xmalloc(sizeof *out);
2750 for (i = 0; i < n_columns; i++) {
2751 const struct ovsdb_idl_column *column = columns[i];
2752 const char *column_name = column ? column->name : "_uuid";
2754 table_add_column(out, "%s", column_name);
2761 list_record(const struct ovsdb_idl_row *row,
2762 const struct ovsdb_idl_column **columns, size_t n_columns,
2768 for (i = 0; i < n_columns; i++) {
2769 const struct ovsdb_idl_column *column = columns[i];
2770 struct cell *cell = table_add_cell(out);
2773 struct ovsdb_datum datum;
2774 union ovsdb_atom atom;
2776 atom.uuid = row->uuid;
2779 datum.values = NULL;
2782 cell->json = ovsdb_datum_to_json(&datum, &ovsdb_type_uuid);
2783 cell->type = &ovsdb_type_uuid;
2785 const struct ovsdb_datum *datum = ovsdb_idl_read(row, column);
2787 cell->json = ovsdb_datum_to_json(datum, &column->type);
2788 cell->type = &column->type;
2794 cmd_list(struct vsctl_context *ctx)
2796 const char *column_names = shash_find_data(&ctx->options, "--columns");
2797 const struct ovsdb_idl_column **columns;
2798 const char *table_name = ctx->argv[1];
2799 const struct vsctl_table_class *table;
2804 table = get_table(table_name);
2805 parse_column_names(column_names, table, &columns, &n_columns);
2806 out = ctx->table = list_make_table(columns, n_columns);
2807 if (ctx->argc > 2) {
2808 for (i = 2; i < ctx->argc; i++) {
2809 list_record(must_get_row(ctx, table, ctx->argv[i]),
2810 columns, n_columns, out);
2813 const struct ovsdb_idl_row *row;
2815 for (row = ovsdb_idl_first_row(ctx->idl, table->class); row != NULL;
2816 row = ovsdb_idl_next_row(row)) {
2817 list_record(row, columns, n_columns, out);
2824 pre_cmd_find(struct vsctl_context *ctx)
2826 const char *column_names = shash_find_data(&ctx->options, "--columns");
2827 const char *table_name = ctx->argv[1];
2828 const struct vsctl_table_class *table;
2831 table = pre_get_table(ctx, table_name);
2832 pre_list_columns(ctx, table, column_names);
2833 for (i = 2; i < ctx->argc; i++) {
2834 pre_parse_column_key_value(ctx, ctx->argv[i], table);
2839 cmd_find(struct vsctl_context *ctx)
2841 const char *column_names = shash_find_data(&ctx->options, "--columns");
2842 const struct ovsdb_idl_column **columns;
2843 const char *table_name = ctx->argv[1];
2844 const struct vsctl_table_class *table;
2845 const struct ovsdb_idl_row *row;
2849 table = get_table(table_name);
2850 parse_column_names(column_names, table, &columns, &n_columns);
2851 out = ctx->table = list_make_table(columns, n_columns);
2852 for (row = ovsdb_idl_first_row(ctx->idl, table->class); row;
2853 row = ovsdb_idl_next_row(row)) {
2856 for (i = 2; i < ctx->argc; i++) {
2857 if (!is_condition_satisfied(table, row, ctx->argv[i],
2862 list_record(row, columns, n_columns, out);
2870 pre_cmd_set(struct vsctl_context *ctx)
2872 const char *table_name = ctx->argv[1];
2873 const struct vsctl_table_class *table;
2876 table = pre_get_table(ctx, table_name);
2877 for (i = 3; i < ctx->argc; i++) {
2878 pre_parse_column_key_value(ctx, ctx->argv[i], table);
2883 set_column(const struct vsctl_table_class *table,
2884 const struct ovsdb_idl_row *row, const char *arg,
2885 struct ovsdb_symbol_table *symtab)
2887 const struct ovsdb_idl_column *column;
2888 char *key_string, *value_string;
2891 error = parse_column_key_value(arg, table, &column, &key_string,
2892 NULL, NULL, 0, &value_string);
2893 die_if_error(error);
2894 if (!value_string) {
2895 vsctl_fatal("%s: missing value", arg);
2899 union ovsdb_atom key, value;
2900 struct ovsdb_datum datum;
2902 if (column->type.value.type == OVSDB_TYPE_VOID) {
2903 vsctl_fatal("cannot specify key to set for non-map column %s",
2907 die_if_error(ovsdb_atom_from_string(&key, &column->type.key,
2908 key_string, symtab));
2909 die_if_error(ovsdb_atom_from_string(&value, &column->type.value,
2910 value_string, symtab));
2912 ovsdb_datum_init_empty(&datum);
2913 ovsdb_datum_add_unsafe(&datum, &key, &value, &column->type);
2915 ovsdb_atom_destroy(&key, column->type.key.type);
2916 ovsdb_atom_destroy(&value, column->type.value.type);
2918 ovsdb_datum_union(&datum, ovsdb_idl_read(row, column),
2919 &column->type, false);
2920 ovsdb_idl_txn_write(row, column, &datum);
2922 struct ovsdb_datum datum;
2924 die_if_error(ovsdb_datum_from_string(&datum, &column->type,
2925 value_string, symtab));
2926 ovsdb_idl_txn_write(row, column, &datum);
2934 cmd_set(struct vsctl_context *ctx)
2936 const char *table_name = ctx->argv[1];
2937 const char *record_id = ctx->argv[2];
2938 const struct vsctl_table_class *table;
2939 const struct ovsdb_idl_row *row;
2942 table = get_table(table_name);
2943 row = must_get_row(ctx, table, record_id);
2944 for (i = 3; i < ctx->argc; i++) {
2945 set_column(table, row, ctx->argv[i], ctx->symtab);
2950 pre_cmd_add(struct vsctl_context *ctx)
2952 const char *table_name = ctx->argv[1];
2953 const char *column_name = ctx->argv[3];
2954 const struct vsctl_table_class *table;
2955 const struct ovsdb_idl_column *column;
2957 table = pre_get_table(ctx, table_name);
2958 pre_get_column(ctx, table, column_name, &column);
2962 cmd_add(struct vsctl_context *ctx)
2964 const char *table_name = ctx->argv[1];
2965 const char *record_id = ctx->argv[2];
2966 const char *column_name = ctx->argv[3];
2967 const struct vsctl_table_class *table;
2968 const struct ovsdb_idl_column *column;
2969 const struct ovsdb_idl_row *row;
2970 const struct ovsdb_type *type;
2971 struct ovsdb_datum old;
2974 table = get_table(table_name);
2975 row = must_get_row(ctx, table, record_id);
2976 die_if_error(get_column(table, column_name, &column));
2978 type = &column->type;
2979 ovsdb_datum_clone(&old, ovsdb_idl_read(row, column), &column->type);
2980 for (i = 4; i < ctx->argc; i++) {
2981 struct ovsdb_type add_type;
2982 struct ovsdb_datum add;
2986 add_type.n_max = UINT_MAX;
2987 die_if_error(ovsdb_datum_from_string(&add, &add_type, ctx->argv[i],
2989 ovsdb_datum_union(&old, &add, type, false);
2990 ovsdb_datum_destroy(&add, type);
2992 if (old.n > type->n_max) {
2993 vsctl_fatal("\"add\" operation would put %u %s in column %s of "
2994 "table %s but the maximum number is %u",
2996 type->value.type == OVSDB_TYPE_VOID ? "values" : "pairs",
2997 column->name, table->class->name, type->n_max);
2999 ovsdb_idl_txn_verify(row, column);
3000 ovsdb_idl_txn_write(row, column, &old);
3004 pre_cmd_remove(struct vsctl_context *ctx)
3006 const char *table_name = ctx->argv[1];
3007 const char *column_name = ctx->argv[3];
3008 const struct vsctl_table_class *table;
3009 const struct ovsdb_idl_column *column;
3011 table = pre_get_table(ctx, table_name);
3012 pre_get_column(ctx, table, column_name, &column);
3016 cmd_remove(struct vsctl_context *ctx)
3018 const char *table_name = ctx->argv[1];
3019 const char *record_id = ctx->argv[2];
3020 const char *column_name = ctx->argv[3];
3021 const struct vsctl_table_class *table;
3022 const struct ovsdb_idl_column *column;
3023 const struct ovsdb_idl_row *row;
3024 const struct ovsdb_type *type;
3025 struct ovsdb_datum old;
3028 table = get_table(table_name);
3029 row = must_get_row(ctx, table, record_id);
3030 die_if_error(get_column(table, column_name, &column));
3032 type = &column->type;
3033 ovsdb_datum_clone(&old, ovsdb_idl_read(row, column), &column->type);
3034 for (i = 4; i < ctx->argc; i++) {
3035 struct ovsdb_type rm_type;
3036 struct ovsdb_datum rm;
3041 rm_type.n_max = UINT_MAX;
3042 error = ovsdb_datum_from_string(&rm, &rm_type,
3043 ctx->argv[i], ctx->symtab);
3044 if (error && ovsdb_type_is_map(&rm_type)) {
3046 rm_type.value.type = OVSDB_TYPE_VOID;
3047 die_if_error(ovsdb_datum_from_string(&rm, &rm_type,
3048 ctx->argv[i], ctx->symtab));
3050 ovsdb_datum_subtract(&old, type, &rm, &rm_type);
3051 ovsdb_datum_destroy(&rm, &rm_type);
3053 if (old.n < type->n_min) {
3054 vsctl_fatal("\"remove\" operation would put %u %s in column %s of "
3055 "table %s but the minimum number is %u",
3057 type->value.type == OVSDB_TYPE_VOID ? "values" : "pairs",
3058 column->name, table->class->name, type->n_min);
3060 ovsdb_idl_txn_verify(row, column);
3061 ovsdb_idl_txn_write(row, column, &old);
3065 pre_cmd_clear(struct vsctl_context *ctx)
3067 const char *table_name = ctx->argv[1];
3068 const struct vsctl_table_class *table;
3071 table = pre_get_table(ctx, table_name);
3072 for (i = 3; i < ctx->argc; i++) {
3073 const struct ovsdb_idl_column *column;
3075 pre_get_column(ctx, table, ctx->argv[i], &column);
3080 cmd_clear(struct vsctl_context *ctx)
3082 const char *table_name = ctx->argv[1];
3083 const char *record_id = ctx->argv[2];
3084 const struct vsctl_table_class *table;
3085 const struct ovsdb_idl_row *row;
3088 table = get_table(table_name);
3089 row = must_get_row(ctx, table, record_id);
3090 for (i = 3; i < ctx->argc; i++) {
3091 const struct ovsdb_idl_column *column;
3092 const struct ovsdb_type *type;
3093 struct ovsdb_datum datum;
3095 die_if_error(get_column(table, ctx->argv[i], &column));
3097 type = &column->type;
3098 if (type->n_min > 0) {
3099 vsctl_fatal("\"clear\" operation cannot be applied to column %s "
3100 "of table %s, which is not allowed to be empty",
3101 column->name, table->class->name);
3104 ovsdb_datum_init_empty(&datum);
3105 ovsdb_idl_txn_write(row, column, &datum);
3110 pre_create(struct vsctl_context *ctx)
3112 const char *id = shash_find_data(&ctx->options, "--id");
3113 const char *table_name = ctx->argv[1];
3114 const struct vsctl_table_class *table;
3116 table = get_table(table_name);
3117 if (!id && !table->class->is_root) {
3118 VLOG_WARN("applying \"create\" command to table %s without --id "
3119 "option will have no effect", table->class->name);
3124 cmd_create(struct vsctl_context *ctx)
3126 const char *id = shash_find_data(&ctx->options, "--id");
3127 const char *table_name = ctx->argv[1];
3128 const struct vsctl_table_class *table = get_table(table_name);
3129 const struct ovsdb_idl_row *row;
3130 const struct uuid *uuid;
3134 struct ovsdb_symbol *symbol = create_symbol(ctx->symtab, id, NULL);
3135 if (table->class->is_root) {
3136 /* This table is in the root set, meaning that rows created in it
3137 * won't disappear even if they are unreferenced, so disable
3138 * warnings about that by pretending that there is a reference. */
3139 symbol->strong_ref = true;
3141 uuid = &symbol->uuid;
3146 row = ovsdb_idl_txn_insert(ctx->txn, table->class, uuid);
3147 for (i = 2; i < ctx->argc; i++) {
3148 set_column(table, row, ctx->argv[i], ctx->symtab);
3150 ds_put_format(&ctx->output, UUID_FMT, UUID_ARGS(&row->uuid));
3153 /* This function may be used as the 'postprocess' function for commands that
3154 * insert new rows into the database. It expects that the command's 'run'
3155 * function prints the UUID reported by ovsdb_idl_txn_insert() as the command's
3156 * sole output. It replaces that output by the row's permanent UUID assigned
3157 * by the database server and appends a new-line.
3159 * Currently we use this only for "create", because the higher-level commands
3160 * are supposed to be independent of the actual structure of the vswitch
3163 post_create(struct vsctl_context *ctx)
3165 const struct uuid *real;
3168 if (!uuid_from_string(&dummy, ds_cstr(&ctx->output))) {
3171 real = ovsdb_idl_txn_get_insert_uuid(ctx->txn, &dummy);
3173 ds_clear(&ctx->output);
3174 ds_put_format(&ctx->output, UUID_FMT, UUID_ARGS(real));
3176 ds_put_char(&ctx->output, '\n');
3180 pre_cmd_destroy(struct vsctl_context *ctx)
3182 const char *table_name = ctx->argv[1];
3184 pre_get_table(ctx, table_name);
3188 cmd_destroy(struct vsctl_context *ctx)
3190 bool must_exist = !shash_find(&ctx->options, "--if-exists");
3191 const char *table_name = ctx->argv[1];
3192 const struct vsctl_table_class *table;
3195 table = get_table(table_name);
3196 for (i = 2; i < ctx->argc; i++) {
3197 const struct ovsdb_idl_row *row;
3199 row = (must_exist ? must_get_row : get_row)(ctx, table, ctx->argv[i]);
3201 ovsdb_idl_txn_delete(row);
3207 is_condition_satisfied(const struct vsctl_table_class *table,
3208 const struct ovsdb_idl_row *row, const char *arg,
3209 struct ovsdb_symbol_table *symtab)
3211 static const char *operators[] = {
3212 "=", "!=", "<", ">", "<=", ">="
3215 const struct ovsdb_idl_column *column;
3216 const struct ovsdb_datum *have_datum;
3217 char *key_string, *value_string;
3218 const char *operator;
3223 error = parse_column_key_value(arg, table, &column, &key_string,
3224 &operator, operators, ARRAY_SIZE(operators),
3226 die_if_error(error);
3227 if (!value_string) {
3228 vsctl_fatal("%s: missing value", arg);
3231 have_datum = ovsdb_idl_read(row, column);
3233 union ovsdb_atom want_key, want_value;
3235 if (column->type.value.type == OVSDB_TYPE_VOID) {
3236 vsctl_fatal("cannot specify key to check for non-map column %s",
3240 die_if_error(ovsdb_atom_from_string(&want_key, &column->type.key,
3241 key_string, symtab));
3242 die_if_error(ovsdb_atom_from_string(&want_value, &column->type.value,
3243 value_string, symtab));
3245 idx = ovsdb_datum_find_key(have_datum,
3246 &want_key, column->type.key.type);
3247 if (idx != UINT_MAX) {
3248 cmp = ovsdb_atom_compare_3way(&have_datum->values[idx],
3250 column->type.value.type);
3253 ovsdb_atom_destroy(&want_key, column->type.key.type);
3254 ovsdb_atom_destroy(&want_value, column->type.value.type);
3256 struct ovsdb_datum want_datum;
3258 die_if_error(ovsdb_datum_from_string(&want_datum, &column->type,
3259 value_string, symtab));
3261 cmp = ovsdb_datum_compare_3way(have_datum, &want_datum,
3263 ovsdb_datum_destroy(&want_datum, &column->type);
3269 return (idx == UINT_MAX ? false
3270 : !strcmp(operator, "=") ? cmp == 0
3271 : !strcmp(operator, "!=") ? cmp != 0
3272 : !strcmp(operator, "<") ? cmp < 0
3273 : !strcmp(operator, ">") ? cmp > 0
3274 : !strcmp(operator, "<=") ? cmp <= 0
3275 : !strcmp(operator, ">=") ? cmp >= 0
3280 pre_cmd_wait_until(struct vsctl_context *ctx)
3282 const char *table_name = ctx->argv[1];
3283 const struct vsctl_table_class *table;
3286 table = pre_get_table(ctx, table_name);
3288 for (i = 3; i < ctx->argc; i++) {
3289 pre_parse_column_key_value(ctx, ctx->argv[i], table);
3294 cmd_wait_until(struct vsctl_context *ctx)
3296 const char *table_name = ctx->argv[1];
3297 const char *record_id = ctx->argv[2];
3298 const struct vsctl_table_class *table;
3299 const struct ovsdb_idl_row *row;
3302 table = get_table(table_name);
3304 row = get_row(ctx, table, record_id);
3306 ctx->try_again = true;
3310 for (i = 3; i < ctx->argc; i++) {
3311 if (!is_condition_satisfied(table, row, ctx->argv[i], ctx->symtab)) {
3312 ctx->try_again = true;
3318 static struct json *
3319 where_uuid_equals(const struct uuid *uuid)
3322 json_array_create_1(
3323 json_array_create_3(
3324 json_string_create("_uuid"),
3325 json_string_create("=="),
3326 json_array_create_2(
3327 json_string_create("uuid"),
3328 json_string_create_nocopy(
3329 xasprintf(UUID_FMT, UUID_ARGS(uuid))))));
3333 vsctl_context_init(struct vsctl_context *ctx, struct vsctl_command *command,
3334 struct ovsdb_idl *idl, struct ovsdb_idl_txn *txn,
3335 const struct ovsrec_open_vswitch *ovs,
3336 struct ovsdb_symbol_table *symtab)
3338 ctx->argc = command->argc;
3339 ctx->argv = command->argv;
3340 ctx->options = command->options;
3342 ds_swap(&ctx->output, &command->output);
3343 ctx->table = command->table;
3347 ctx->symtab = symtab;
3348 ctx->verified_ports = false;
3350 ctx->try_again = false;
3354 vsctl_context_done(struct vsctl_context *ctx, struct vsctl_command *command)
3356 ds_swap(&ctx->output, &command->output);
3357 command->table = ctx->table;
3361 run_prerequisites(struct vsctl_command *commands, size_t n_commands,
3362 struct ovsdb_idl *idl)
3364 struct vsctl_command *c;
3366 ovsdb_idl_add_table(idl, &ovsrec_table_open_vswitch);
3367 if (wait_for_reload) {
3368 ovsdb_idl_add_column(idl, &ovsrec_open_vswitch_col_cur_cfg);
3370 for (c = commands; c < &commands[n_commands]; c++) {
3371 if (c->syntax->prerequisites) {
3372 struct vsctl_context ctx;
3374 ds_init(&c->output);
3377 vsctl_context_init(&ctx, c, idl, NULL, NULL, NULL);
3378 (c->syntax->prerequisites)(&ctx);
3379 vsctl_context_done(&ctx, c);
3381 assert(!c->output.string);
3388 do_vsctl(const char *args, struct vsctl_command *commands, size_t n_commands,
3389 struct ovsdb_idl *idl)
3391 struct ovsdb_idl_txn *txn;
3392 const struct ovsrec_open_vswitch *ovs;
3393 enum ovsdb_idl_txn_status status;
3394 struct ovsdb_symbol_table *symtab;
3395 struct vsctl_command *c;
3396 struct shash_node *node;
3397 int64_t next_cfg = 0;
3400 txn = the_idl_txn = ovsdb_idl_txn_create(idl);
3402 ovsdb_idl_txn_set_dry_run(txn);
3405 ovsdb_idl_txn_add_comment(txn, "ovs-vsctl: %s", args);
3407 ovs = ovsrec_open_vswitch_first(idl);
3409 /* XXX add verification that table is empty */
3410 ovs = ovsrec_open_vswitch_insert(txn);
3413 if (wait_for_reload) {
3414 struct json *where = where_uuid_equals(&ovs->header_.uuid);
3415 ovsdb_idl_txn_increment(txn, "Open_vSwitch", "next_cfg", where);
3416 json_destroy(where);
3419 symtab = ovsdb_symbol_table_create();
3420 for (c = commands; c < &commands[n_commands]; c++) {
3421 ds_init(&c->output);
3424 for (c = commands; c < &commands[n_commands]; c++) {
3425 struct vsctl_context ctx;
3427 vsctl_context_init(&ctx, c, idl, txn, ovs, symtab);
3428 (c->syntax->run)(&ctx);
3429 vsctl_context_done(&ctx, c);
3431 if (ctx.try_again) {
3436 SHASH_FOR_EACH (node, &symtab->sh) {
3437 struct ovsdb_symbol *symbol = node->data;
3438 if (!symbol->created) {
3439 vsctl_fatal("row id \"%s\" is referenced but never created (e.g. "
3440 "with \"-- --id=%s create ...\")",
3441 node->name, node->name);
3443 if (!symbol->strong_ref) {
3444 if (!symbol->weak_ref) {
3445 VLOG_WARN("row id \"%s\" was created but no reference to it "
3446 "was inserted, so it will not actually appear in "
3447 "the database", node->name);
3449 VLOG_WARN("row id \"%s\" was created but only a weak "
3450 "reference to it was inserted, so it will not "
3451 "actually appear in the database", node->name);
3456 status = ovsdb_idl_txn_commit_block(txn);
3457 if (wait_for_reload && status == TXN_SUCCESS) {
3458 next_cfg = ovsdb_idl_txn_get_increment_new_value(txn);
3460 if (status == TXN_UNCHANGED || status == TXN_SUCCESS) {
3461 for (c = commands; c < &commands[n_commands]; c++) {
3462 if (c->syntax->postprocess) {
3463 struct vsctl_context ctx;
3465 vsctl_context_init(&ctx, c, idl, txn, ovs, symtab);
3466 (c->syntax->postprocess)(&ctx);
3467 vsctl_context_done(&ctx, c);
3471 error = xstrdup(ovsdb_idl_txn_get_error(txn));
3472 ovsdb_idl_txn_destroy(txn);
3473 txn = the_idl_txn = NULL;
3476 case TXN_INCOMPLETE:
3480 /* Should not happen--we never call ovsdb_idl_txn_abort(). */
3481 vsctl_fatal("transaction aborted");
3491 vsctl_fatal("transaction error: %s", error);
3498 ovsdb_symbol_table_destroy(symtab);
3500 for (c = commands; c < &commands[n_commands]; c++) {
3501 struct ds *ds = &c->output;
3504 table_print(c->table, &table_style);
3505 } else if (oneline) {
3509 for (j = 0; j < ds->length; j++) {
3510 int ch = ds->string[j];
3513 fputs("\\n", stdout);
3517 fputs("\\\\", stdout);
3526 fputs(ds_cstr(ds), stdout);
3528 ds_destroy(&c->output);
3529 table_destroy(c->table);
3532 smap_destroy(&c->options);
3536 if (wait_for_reload && status != TXN_UNCHANGED) {
3539 OVSREC_OPEN_VSWITCH_FOR_EACH (ovs, idl) {
3540 if (ovs->cur_cfg >= next_cfg) {
3544 ovsdb_idl_wait(idl);
3549 ovsdb_idl_destroy(idl);
3554 /* Our transaction needs to be rerun, or a prerequisite was not met. Free
3555 * resources and return so that the caller can try again. */
3557 ovsdb_idl_txn_abort(txn);
3558 ovsdb_idl_txn_destroy(txn);
3560 ovsdb_symbol_table_destroy(symtab);
3561 for (c = commands; c < &commands[n_commands]; c++) {
3562 ds_destroy(&c->output);
3563 table_destroy(c->table);
3569 static const struct vsctl_command_syntax all_commands[] = {
3570 /* Open vSwitch commands. */
3571 {"init", 0, 0, NULL, cmd_init, NULL, "", RW},
3573 /* Bridge commands. */
3574 {"add-br", 1, 3, pre_get_info, cmd_add_br, NULL, "--may-exist", RW},
3575 {"del-br", 1, 1, pre_get_info, cmd_del_br, NULL, "--if-exists", RW},
3576 {"list-br", 0, 0, pre_get_info, cmd_list_br, NULL, "", RO},
3577 {"br-exists", 1, 1, pre_get_info, cmd_br_exists, NULL, "", RO},
3578 {"br-to-vlan", 1, 1, pre_get_info, cmd_br_to_vlan, NULL, "", RO},
3579 {"br-to-parent", 1, 1, pre_get_info, cmd_br_to_parent, NULL, "", RO},
3580 {"br-set-external-id", 2, 3, pre_cmd_br_set_external_id,
3581 cmd_br_set_external_id, NULL, "", RW},
3582 {"br-get-external-id", 1, 2, pre_cmd_br_get_external_id,
3583 cmd_br_get_external_id, NULL, "", RO},
3585 /* Port commands. */
3586 {"list-ports", 1, 1, pre_get_info, cmd_list_ports, NULL, "", RO},
3587 {"add-port", 2, INT_MAX, pre_get_info, cmd_add_port, NULL, "--may-exist",
3589 {"add-bond", 4, INT_MAX, pre_get_info, cmd_add_bond, NULL,
3590 "--may-exist,--fake-iface", RW},
3591 {"del-port", 1, 2, pre_get_info, cmd_del_port, NULL,
3592 "--if-exists,--with-iface", RW},
3593 {"port-to-br", 1, 1, pre_get_info, cmd_port_to_br, NULL, "", RO},
3595 /* Interface commands. */
3596 {"list-ifaces", 1, 1, pre_get_info, cmd_list_ifaces, NULL, "", RO},
3597 {"iface-to-br", 1, 1, pre_get_info, cmd_iface_to_br, NULL, "", RO},
3599 /* Controller commands. */
3600 {"get-controller", 1, 1, pre_controller, cmd_get_controller, NULL, "", RO},
3601 {"del-controller", 1, 1, pre_controller, cmd_del_controller, NULL, "", RW},
3602 {"set-controller", 1, INT_MAX, pre_controller, cmd_set_controller, NULL,
3604 {"get-fail-mode", 1, 1, pre_get_info, cmd_get_fail_mode, NULL, "", RO},
3605 {"del-fail-mode", 1, 1, pre_get_info, cmd_del_fail_mode, NULL, "", RW},
3606 {"set-fail-mode", 2, 2, pre_get_info, cmd_set_fail_mode, NULL, "", RW},
3608 /* Manager commands. */
3609 {"get-manager", 0, 0, pre_manager, cmd_get_manager, NULL, "", RO},
3610 {"del-manager", 0, INT_MAX, pre_manager, cmd_del_manager, NULL, "", RW},
3611 {"set-manager", 1, INT_MAX, pre_manager, cmd_set_manager, NULL, "", RW},
3614 {"get-ssl", 0, 0, pre_cmd_get_ssl, cmd_get_ssl, NULL, "", RO},
3615 {"del-ssl", 0, 0, pre_cmd_del_ssl, cmd_del_ssl, NULL, "", RW},
3616 {"set-ssl", 3, 3, pre_cmd_set_ssl, cmd_set_ssl, NULL, "--bootstrap", RW},
3618 /* Switch commands. */
3619 {"emer-reset", 0, 0, pre_cmd_emer_reset, cmd_emer_reset, NULL, "", RW},
3621 /* Parameter commands. */
3622 {"get", 2, INT_MAX, pre_cmd_get, cmd_get, NULL, "--if-exists,--id=", RO},
3623 {"list", 1, INT_MAX, pre_cmd_list, cmd_list, NULL, "--columns=", RO},
3624 {"find", 1, INT_MAX, pre_cmd_find, cmd_find, NULL, "--columns=", RO},
3625 {"set", 3, INT_MAX, pre_cmd_set, cmd_set, NULL, "", RW},
3626 {"add", 4, INT_MAX, pre_cmd_add, cmd_add, NULL, "", RW},
3627 {"remove", 4, INT_MAX, pre_cmd_remove, cmd_remove, NULL, "", RW},
3628 {"clear", 3, INT_MAX, pre_cmd_clear, cmd_clear, NULL, "", RW},
3629 {"create", 2, INT_MAX, pre_create, cmd_create, post_create, "--id=", RW},
3630 {"destroy", 1, INT_MAX, pre_cmd_destroy, cmd_destroy, NULL, "--if-exists",
3632 {"wait-until", 2, INT_MAX, pre_cmd_wait_until, cmd_wait_until, NULL, "",
3635 {NULL, 0, 0, NULL, NULL, NULL, NULL, RO},