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, NULL, OPT_DB},
211 {"no-syslog", no_argument, NULL, OPT_NO_SYSLOG},
212 {"no-wait", no_argument, NULL, OPT_NO_WAIT},
213 {"dry-run", no_argument, NULL, OPT_DRY_RUN},
214 {"oneline", no_argument, NULL, OPT_ONELINE},
215 {"timeout", required_argument, NULL, 't'},
216 {"help", no_argument, NULL, 'h'},
217 {"version", no_argument, NULL, 'V'},
220 STREAM_SSL_LONG_OPTIONS,
221 {"peer-ca-cert", required_argument, NULL, OPT_PEER_CA_CERT},
224 char *tmp, *short_options;
226 tmp = long_options_to_short_options(long_options);
227 short_options = xasprintf("+%s", tmp);
230 table_style.format = TF_LIST;
235 c = getopt_long(argc, argv, short_options, long_options, NULL);
250 vlog_set_levels(&VLM_vsctl, VLF_SYSLOG, VLL_WARN);
254 wait_for_reload = false;
265 OVS_PRINT_VERSION(0, 0);
269 timeout = strtoul(optarg, NULL, 10);
271 vsctl_fatal("value %s on -t or --timeout is invalid",
277 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);
299 static struct vsctl_command *
300 parse_commands(int argc, char *argv[], size_t *n_commandsp)
302 struct vsctl_command *commands;
303 size_t n_commands, allocated_commands;
307 n_commands = allocated_commands = 0;
309 for (start = i = 0; i <= argc; i++) {
310 if (i == argc || !strcmp(argv[i], "--")) {
312 if (n_commands >= allocated_commands) {
313 struct vsctl_command *c;
315 commands = x2nrealloc(commands, &allocated_commands,
317 for (c = commands; c < &commands[n_commands]; c++) {
318 shash_moved(&c->options);
321 parse_command(i - start, &argv[start],
322 &commands[n_commands++]);
328 vsctl_fatal("missing command name (use --help for help)");
330 *n_commandsp = n_commands;
335 parse_command(int argc, char *argv[], struct vsctl_command *command)
337 const struct vsctl_command_syntax *p;
338 struct shash_node *node;
342 shash_init(&command->options);
343 for (i = 0; i < argc; i++) {
344 const char *option = argv[i];
348 if (option[0] != '-') {
352 equals = strchr(option, '=');
354 key = xmemdup0(option, equals - option);
355 value = xstrdup(equals + 1);
357 key = xstrdup(option);
361 if (shash_find(&command->options, key)) {
362 vsctl_fatal("'%s' option specified multiple times", argv[i]);
364 shash_add_nocopy(&command->options, key, value);
367 vsctl_fatal("missing command name");
370 p = find_command(argv[i]);
372 vsctl_fatal("unknown command '%s'; use --help for help", argv[i]);
375 SHASH_FOR_EACH (node, &command->options) {
376 const char *s = strstr(p->options, node->name);
377 int end = s ? s[strlen(node->name)] : EOF;
379 if (end != '=' && end != ',' && end != ' ' && end != '\0') {
380 vsctl_fatal("'%s' command has no '%s' option",
381 argv[i], node->name);
383 if ((end == '=') != (node->data != NULL)) {
385 vsctl_fatal("missing argument to '%s' option on '%s' "
386 "command", node->name, argv[i]);
388 vsctl_fatal("'%s' option on '%s' does not accept an "
389 "argument", node->name, argv[i]);
394 n_arg = argc - i - 1;
395 if (n_arg < p->min_args) {
396 vsctl_fatal("'%s' command requires at least %d arguments",
397 p->name, p->min_args);
398 } else if (n_arg > p->max_args) {
401 for (j = i + 1; j < argc; j++) {
402 if (argv[j][0] == '-') {
403 vsctl_fatal("'%s' command takes at most %d arguments "
404 "(note that options must precede command "
405 "names and follow a \"--\" argument)",
406 p->name, p->max_args);
410 vsctl_fatal("'%s' command takes at most %d arguments",
411 p->name, p->max_args);
415 command->argc = n_arg + 1;
416 command->argv = &argv[i];
419 /* Returns the "struct vsctl_command_syntax" for a given command 'name', or a
420 * null pointer if there is none. */
421 static const struct vsctl_command_syntax *
422 find_command(const char *name)
424 static struct shash commands = SHASH_INITIALIZER(&commands);
426 if (shash_is_empty(&commands)) {
427 const struct vsctl_command_syntax *p;
429 for (p = all_commands; p->name; p++) {
430 shash_add_assert(&commands, p->name, p);
434 return shash_find_data(&commands, name);
438 vsctl_fatal(const char *format, ...)
443 va_start(args, format);
444 message = xvasprintf(format, args);
447 vlog_set_levels(&VLM_vsctl, VLF_CONSOLE, VLL_EMER);
448 VLOG_ERR("%s", message);
449 ovs_error(0, "%s", message);
450 vsctl_exit(EXIT_FAILURE);
453 /* Frees the current transaction and the underlying IDL and then calls
456 * Freeing the transaction and the IDL is not strictly necessary, but it makes
457 * for a clean memory leak report from valgrind in the normal case. That makes
458 * it easier to notice real memory leaks. */
460 vsctl_exit(int status)
463 ovsdb_idl_txn_abort(the_idl_txn);
464 ovsdb_idl_txn_destroy(the_idl_txn);
466 ovsdb_idl_destroy(the_idl);
474 %s: ovs-vswitchd management utility\n\
475 usage: %s [OPTIONS] COMMAND [ARG...]\n\
477 Open vSwitch commands:\n\
478 init initialize database, if not yet initialized\n\
479 show print overview of database contents\n\
480 emer-reset reset configuration to clean state\n\
483 add-br BRIDGE create a new bridge named BRIDGE\n\
484 add-br BRIDGE PARENT VLAN create new fake BRIDGE in PARENT on VLAN\n\
485 del-br BRIDGE delete BRIDGE and all of its ports\n\
486 list-br print the names of all the bridges\n\
487 br-exists BRIDGE test whether BRIDGE exists\n\
488 br-to-vlan BRIDGE print the VLAN which BRIDGE is on\n\
489 br-to-parent BRIDGE print the parent of BRIDGE\n\
490 br-set-external-id BRIDGE KEY VALUE set KEY on BRIDGE to VALUE\n\
491 br-set-external-id BRIDGE KEY unset KEY on BRIDGE\n\
492 br-get-external-id BRIDGE KEY print value of KEY on BRIDGE\n\
493 br-get-external-id BRIDGE list key-value pairs on BRIDGE\n\
495 Port commands (a bond is considered to be a single port):\n\
496 list-ports BRIDGE print the names of all the ports on BRIDGE\n\
497 add-port BRIDGE PORT add network device PORT to BRIDGE\n\
498 add-bond BRIDGE PORT IFACE... add bonded port PORT in BRIDGE from IFACES\n\
499 del-port [BRIDGE] PORT delete PORT (which may be bonded) from BRIDGE\n\
500 port-to-br PORT print name of bridge that contains PORT\n\
502 Interface commands (a bond consists of multiple interfaces):\n\
503 list-ifaces BRIDGE print the names of all interfaces on BRIDGE\n\
504 iface-to-br IFACE print name of bridge that contains IFACE\n\
506 Controller commands:\n\
507 get-controller BRIDGE print the controller for BRIDGE\n\
508 del-controller BRIDGE delete the controller for BRIDGE\n\
509 set-controller BRIDGE TARGET set the controller for BRIDGE to TARGET\n\
510 get-fail-mode BRIDGE print the fail-mode for BRIDGE\n\
511 del-fail-mode BRIDGE delete the fail-mode for BRIDGE\n\
512 set-fail-mode BRIDGE MODE set the fail-mode for BRIDGE to MODE\n\
515 get-manager print all manager(s)\n\
516 del-manager delete all manager(s)\n\
517 set-manager TARGET... set the list of manager(s) to TARGET(s)\n\
520 get-ssl print the SSL configuration\n\
521 del-ssl delete the SSL configuration\n\
522 set-ssl PRIV-KEY CERT CA-CERT set the SSL configuration\n\
525 emer-reset reset switch to known good state\n\
527 Database commands:\n\
528 list TBL [REC] list RECord (or all records) in TBL\n\
529 find TBL CONDITION... list records satisfying CONDITION in TBL\n\
530 get TBL REC COL[:KEY] print values of COLumns in RECord in TBL\n\
531 set TBL REC COL[:KEY]=VALUE set COLumn values in RECord in TBL\n\
532 add TBL REC COL [KEY=]VALUE add (KEY=)VALUE to COLumn in RECord in TBL\n\
533 remove TBL REC COL [KEY=]VALUE remove (KEY=)VALUE from COLumn\n\
534 clear TBL REC COL clear values from COLumn in RECord in TBL\n\
535 create TBL COL[:KEY]=VALUE create and initialize new record\n\
536 destroy TBL REC delete RECord from TBL\n\
537 wait-until TBL REC [COL[:KEY]=VALUE] wait until condition is true\n\
538 Potentially unsafe database commands require --force option.\n\
541 --db=DATABASE connect to DATABASE\n\
543 --no-wait do not wait for ovs-vswitchd to reconfigure\n\
544 -t, --timeout=SECS wait at most SECS seconds for ovs-vswitchd\n\
545 --dry-run do not commit changes to database\n\
546 --oneline print exactly one line of output per command\n",
547 program_name, program_name, default_db());
550 --no-syslog equivalent to --verbose=vsctl:syslog:warn\n");
551 stream_usage("database", true, true, false);
554 -h, --help display this help message\n\
555 -V, --version display version information\n");
564 def = xasprintf("unix:%s/db.sock", ovs_rundir());
569 /* Returns true if it looks like this set of arguments might modify the
570 * database, otherwise false. (Not very smart, so it's prone to false
573 might_write_to_db(char **argv)
575 for (; *argv; argv++) {
576 const struct vsctl_command_syntax *p = find_command(*argv);
577 if (p && p->mode == RW) {
584 struct vsctl_context {
588 struct shash options;
590 /* Modifiable state. */
593 struct ovsdb_idl *idl;
594 struct ovsdb_idl_txn *txn;
595 struct ovsdb_symbol_table *symtab;
596 const struct ovsrec_open_vswitch *ovs;
599 /* A command may set this member to true if some prerequisite is not met
600 * and the caller should wait for something to change and then retry. */
604 struct vsctl_bridge {
605 struct ovsrec_bridge *br_cfg;
607 struct ovsrec_controller **ctrl;
610 struct vsctl_bridge *parent;
615 struct ovsrec_port *port_cfg;
616 struct vsctl_bridge *bridge;
620 struct ovsrec_interface *iface_cfg;
621 struct vsctl_port *port;
625 struct vsctl_context *ctx;
626 struct shash bridges; /* Maps from bridge name to struct vsctl_bridge. */
627 struct shash ports; /* Maps from port name to struct vsctl_port. */
628 struct shash ifaces; /* Maps from port name to struct vsctl_iface. */
632 vsctl_context_to_string(const struct vsctl_context *ctx)
634 const struct shash_node *node;
640 SHASH_FOR_EACH (node, &ctx->options) {
641 svec_add(&words, node->name);
643 for (i = 0; i < ctx->argc; i++) {
644 svec_add(&words, ctx->argv[i]);
646 svec_terminate(&words);
648 s = process_escape_args(words.names);
650 svec_destroy(&words);
656 verify_ports(struct vsctl_context *ctx)
658 if (!ctx->verified_ports) {
659 const struct ovsrec_bridge *bridge;
660 const struct ovsrec_port *port;
662 ovsrec_open_vswitch_verify_bridges(ctx->ovs);
663 OVSREC_BRIDGE_FOR_EACH (bridge, ctx->idl) {
664 ovsrec_bridge_verify_ports(bridge);
666 OVSREC_PORT_FOR_EACH (port, ctx->idl) {
667 ovsrec_port_verify_interfaces(port);
670 ctx->verified_ports = true;
674 static struct vsctl_bridge *
675 add_bridge(struct vsctl_info *b,
676 struct ovsrec_bridge *br_cfg, const char *name,
677 struct vsctl_bridge *parent, int vlan)
679 struct vsctl_bridge *br = xmalloc(sizeof *br);
681 br->name = xstrdup(name);
685 br->ctrl = parent->br_cfg->controller;
686 br->n_ctrl = parent->br_cfg->n_controller;
687 br->fail_mode = parent->br_cfg->fail_mode;
689 br->ctrl = br_cfg->controller;
690 br->n_ctrl = br_cfg->n_controller;
691 br->fail_mode = br_cfg->fail_mode;
693 shash_add(&b->bridges, br->name, br);
698 port_is_fake_bridge(const struct ovsrec_port *port_cfg)
700 return (port_cfg->fake_bridge
702 && *port_cfg->tag >= 1 && *port_cfg->tag <= 4095);
705 static struct vsctl_bridge *
706 find_vlan_bridge(struct vsctl_info *info,
707 struct vsctl_bridge *parent, int vlan)
709 struct shash_node *node;
711 SHASH_FOR_EACH (node, &info->bridges) {
712 struct vsctl_bridge *br = node->data;
713 if (br->parent == parent && br->vlan == vlan) {
722 free_info(struct vsctl_info *info)
724 struct shash_node *node;
726 SHASH_FOR_EACH (node, &info->bridges) {
727 struct vsctl_bridge *bridge = node->data;
731 shash_destroy(&info->bridges);
733 shash_destroy_free_data(&info->ports);
734 shash_destroy_free_data(&info->ifaces);
738 pre_get_info(struct vsctl_context *ctx)
740 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_bridges);
742 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_name);
743 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_controller);
744 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_fail_mode);
745 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_ports);
747 ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_name);
748 ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_fake_bridge);
749 ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_tag);
750 ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_interfaces);
752 ovsdb_idl_add_column(ctx->idl, &ovsrec_interface_col_name);
756 get_info(struct vsctl_context *ctx, struct vsctl_info *info)
758 const struct ovsrec_open_vswitch *ovs = ctx->ovs;
759 struct sset bridges, ports;
763 shash_init(&info->bridges);
764 shash_init(&info->ports);
765 shash_init(&info->ifaces);
769 for (i = 0; i < ovs->n_bridges; i++) {
770 struct ovsrec_bridge *br_cfg = ovs->bridges[i];
771 struct vsctl_bridge *br;
774 if (!sset_add(&bridges, br_cfg->name)) {
775 VLOG_WARN("%s: database contains duplicate bridge name",
779 br = add_bridge(info, br_cfg, br_cfg->name, NULL, 0);
784 for (j = 0; j < br_cfg->n_ports; j++) {
785 struct ovsrec_port *port_cfg = br_cfg->ports[j];
787 if (!sset_add(&ports, port_cfg->name)) {
788 /* Duplicate port name. (We will warn about that later.) */
792 if (port_is_fake_bridge(port_cfg)
793 && sset_add(&bridges, port_cfg->name)) {
794 add_bridge(info, NULL, port_cfg->name, br, *port_cfg->tag);
798 sset_destroy(&bridges);
799 sset_destroy(&ports);
802 for (i = 0; i < ovs->n_bridges; i++) {
803 struct ovsrec_bridge *br_cfg = ovs->bridges[i];
804 struct vsctl_bridge *br;
807 if (!sset_add(&bridges, br_cfg->name)) {
810 br = shash_find_data(&info->bridges, br_cfg->name);
811 for (j = 0; j < br_cfg->n_ports; j++) {
812 struct ovsrec_port *port_cfg = br_cfg->ports[j];
813 struct vsctl_port *port;
816 port = shash_find_data(&info->ports, port_cfg->name);
818 if (port_cfg == port->port_cfg) {
819 VLOG_WARN("%s: port is in multiple bridges (%s and %s)",
820 port_cfg->name, br->name, port->bridge->name);
822 /* Log as an error because this violates the database's
823 * uniqueness constraints, so the database server shouldn't
824 * have allowed it. */
825 VLOG_ERR("%s: database contains duplicate port name",
831 if (port_is_fake_bridge(port_cfg)
832 && !sset_add(&bridges, port_cfg->name)) {
836 port = xmalloc(sizeof *port);
837 port->port_cfg = port_cfg;
839 && *port_cfg->tag >= 1 && *port_cfg->tag <= 4095) {
840 port->bridge = find_vlan_bridge(info, br, *port_cfg->tag);
847 shash_add(&info->ports, port_cfg->name, port);
849 for (k = 0; k < port_cfg->n_interfaces; k++) {
850 struct ovsrec_interface *iface_cfg = port_cfg->interfaces[k];
851 struct vsctl_iface *iface;
853 iface = shash_find_data(&info->ifaces, iface_cfg->name);
855 if (iface_cfg == iface->iface_cfg) {
856 VLOG_WARN("%s: interface is in multiple ports "
859 iface->port->port_cfg->name,
860 port->port_cfg->name);
862 /* Log as an error because this violates the database's
863 * uniqueness constraints, so the database server
864 * shouldn't have allowed it. */
865 VLOG_ERR("%s: database contains duplicate interface "
866 "name", iface_cfg->name);
871 iface = xmalloc(sizeof *iface);
872 iface->iface_cfg = iface_cfg;
874 shash_add(&info->ifaces, iface_cfg->name, iface);
878 sset_destroy(&bridges);
882 check_conflicts(struct vsctl_info *info, const char *name,
885 struct vsctl_iface *iface;
886 struct vsctl_port *port;
888 verify_ports(info->ctx);
890 if (shash_find(&info->bridges, name)) {
891 vsctl_fatal("%s because a bridge named %s already exists",
895 port = shash_find_data(&info->ports, name);
897 vsctl_fatal("%s because a port named %s already exists on "
898 "bridge %s", msg, name, port->bridge->name);
901 iface = shash_find_data(&info->ifaces, name);
903 vsctl_fatal("%s because an interface named %s already exists "
904 "on bridge %s", msg, name, iface->port->bridge->name);
910 static struct vsctl_bridge *
911 find_bridge(struct vsctl_info *info, const char *name, bool must_exist)
913 struct vsctl_bridge *br = shash_find_data(&info->bridges, name);
914 if (must_exist && !br) {
915 vsctl_fatal("no bridge named %s", name);
917 ovsrec_open_vswitch_verify_bridges(info->ctx->ovs);
921 static struct vsctl_bridge *
922 find_real_bridge(struct vsctl_info *info, const char *name, bool must_exist)
924 struct vsctl_bridge *br = find_bridge(info, name, must_exist);
925 if (br && br->parent) {
926 vsctl_fatal("%s is a fake bridge", name);
931 static struct vsctl_port *
932 find_port(struct vsctl_info *info, const char *name, bool must_exist)
934 struct vsctl_port *port = shash_find_data(&info->ports, name);
935 if (port && !strcmp(name, port->bridge->name)) {
938 if (must_exist && !port) {
939 vsctl_fatal("no port named %s", name);
941 verify_ports(info->ctx);
945 static struct vsctl_iface *
946 find_iface(struct vsctl_info *info, const char *name, bool must_exist)
948 struct vsctl_iface *iface = shash_find_data(&info->ifaces, name);
949 if (iface && !strcmp(name, iface->port->bridge->name)) {
952 if (must_exist && !iface) {
953 vsctl_fatal("no interface named %s", name);
955 verify_ports(info->ctx);
960 bridge_insert_port(struct ovsrec_bridge *br, struct ovsrec_port *port)
962 struct ovsrec_port **ports;
965 ports = xmalloc(sizeof *br->ports * (br->n_ports + 1));
966 for (i = 0; i < br->n_ports; i++) {
967 ports[i] = br->ports[i];
969 ports[br->n_ports] = port;
970 ovsrec_bridge_set_ports(br, ports, br->n_ports + 1);
975 bridge_delete_port(struct ovsrec_bridge *br, struct ovsrec_port *port)
977 struct ovsrec_port **ports;
980 ports = xmalloc(sizeof *br->ports * br->n_ports);
981 for (i = n = 0; i < br->n_ports; i++) {
982 if (br->ports[i] != port) {
983 ports[n++] = br->ports[i];
986 ovsrec_bridge_set_ports(br, ports, n);
991 ovs_insert_bridge(const struct ovsrec_open_vswitch *ovs,
992 struct ovsrec_bridge *bridge)
994 struct ovsrec_bridge **bridges;
997 bridges = xmalloc(sizeof *ovs->bridges * (ovs->n_bridges + 1));
998 for (i = 0; i < ovs->n_bridges; i++) {
999 bridges[i] = ovs->bridges[i];
1001 bridges[ovs->n_bridges] = bridge;
1002 ovsrec_open_vswitch_set_bridges(ovs, bridges, ovs->n_bridges + 1);
1007 ovs_delete_bridge(const struct ovsrec_open_vswitch *ovs,
1008 struct ovsrec_bridge *bridge)
1010 struct ovsrec_bridge **bridges;
1013 bridges = xmalloc(sizeof *ovs->bridges * ovs->n_bridges);
1014 for (i = n = 0; i < ovs->n_bridges; i++) {
1015 if (ovs->bridges[i] != bridge) {
1016 bridges[n++] = ovs->bridges[i];
1019 ovsrec_open_vswitch_set_bridges(ovs, bridges, n);
1024 cmd_init(struct vsctl_context *ctx OVS_UNUSED)
1028 struct cmd_show_table {
1029 const struct ovsdb_idl_table_class *table;
1030 const struct ovsdb_idl_column *name_column;
1031 const struct ovsdb_idl_column *columns[3];
1035 static struct cmd_show_table cmd_show_tables[] = {
1036 {&ovsrec_table_open_vswitch,
1038 {&ovsrec_open_vswitch_col_manager_options,
1039 &ovsrec_open_vswitch_col_bridges,
1040 &ovsrec_open_vswitch_col_ovs_version},
1043 {&ovsrec_table_bridge,
1044 &ovsrec_bridge_col_name,
1045 {&ovsrec_bridge_col_controller,
1046 &ovsrec_bridge_col_fail_mode,
1047 &ovsrec_bridge_col_ports},
1050 {&ovsrec_table_port,
1051 &ovsrec_port_col_name,
1052 {&ovsrec_port_col_tag,
1053 &ovsrec_port_col_trunks,
1054 &ovsrec_port_col_interfaces},
1057 {&ovsrec_table_interface,
1058 &ovsrec_interface_col_name,
1059 {&ovsrec_interface_col_type,
1060 &ovsrec_interface_col_options,
1064 {&ovsrec_table_controller,
1065 &ovsrec_controller_col_target,
1066 {&ovsrec_controller_col_is_connected,
1071 {&ovsrec_table_manager,
1072 &ovsrec_manager_col_target,
1073 {&ovsrec_manager_col_is_connected,
1080 pre_cmd_show(struct vsctl_context *ctx)
1082 struct cmd_show_table *show;
1084 for (show = cmd_show_tables;
1085 show < &cmd_show_tables[ARRAY_SIZE(cmd_show_tables)];
1089 ovsdb_idl_add_table(ctx->idl, show->table);
1090 if (show->name_column) {
1091 ovsdb_idl_add_column(ctx->idl, show->name_column);
1093 for (i = 0; i < ARRAY_SIZE(show->columns); i++) {
1094 const struct ovsdb_idl_column *column = show->columns[i];
1096 ovsdb_idl_add_column(ctx->idl, column);
1102 static struct cmd_show_table *
1103 cmd_show_find_table_by_row(const struct ovsdb_idl_row *row)
1105 struct cmd_show_table *show;
1107 for (show = cmd_show_tables;
1108 show < &cmd_show_tables[ARRAY_SIZE(cmd_show_tables)];
1110 if (show->table == row->table->class) {
1117 static struct cmd_show_table *
1118 cmd_show_find_table_by_name(const char *name)
1120 struct cmd_show_table *show;
1122 for (show = cmd_show_tables;
1123 show < &cmd_show_tables[ARRAY_SIZE(cmd_show_tables)];
1125 if (!strcmp(show->table->name, name)) {
1133 cmd_show_row(struct vsctl_context *ctx, const struct ovsdb_idl_row *row,
1136 struct cmd_show_table *show = cmd_show_find_table_by_row(row);
1139 ds_put_char_multiple(&ctx->output, ' ', level * 4);
1140 if (show && show->name_column) {
1141 const struct ovsdb_datum *datum;
1143 ds_put_format(&ctx->output, "%s ", show->table->name);
1144 datum = ovsdb_idl_read(row, show->name_column);
1145 ovsdb_datum_to_string(datum, &show->name_column->type, &ctx->output);
1147 ds_put_format(&ctx->output, UUID_FMT, UUID_ARGS(&row->uuid));
1149 ds_put_char(&ctx->output, '\n');
1151 if (!show || show->recurse) {
1155 show->recurse = true;
1156 for (i = 0; i < ARRAY_SIZE(show->columns); i++) {
1157 const struct ovsdb_idl_column *column = show->columns[i];
1158 const struct ovsdb_datum *datum;
1164 datum = ovsdb_idl_read(row, column);
1165 if (column->type.key.type == OVSDB_TYPE_UUID &&
1166 column->type.key.u.uuid.refTableName) {
1167 struct cmd_show_table *ref_show;
1170 ref_show = cmd_show_find_table_by_name(
1171 column->type.key.u.uuid.refTableName);
1173 for (j = 0; j < datum->n; j++) {
1174 const struct ovsdb_idl_row *ref_row;
1176 ref_row = ovsdb_idl_get_row_for_uuid(ctx->idl,
1178 &datum->keys[j].uuid);
1180 cmd_show_row(ctx, ref_row, level + 1);
1187 if (!ovsdb_datum_is_default(datum, &column->type)) {
1188 ds_put_char_multiple(&ctx->output, ' ', (level + 1) * 4);
1189 ds_put_format(&ctx->output, "%s: ", column->name);
1190 ovsdb_datum_to_string(datum, &column->type, &ctx->output);
1191 ds_put_char(&ctx->output, '\n');
1194 show->recurse = false;
1198 cmd_show(struct vsctl_context *ctx)
1200 const struct ovsdb_idl_row *row;
1202 for (row = ovsdb_idl_first_row(ctx->idl, cmd_show_tables[0].table);
1203 row; row = ovsdb_idl_next_row(row)) {
1204 cmd_show_row(ctx, row, 0);
1209 pre_cmd_emer_reset(struct vsctl_context *ctx)
1211 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_manager_options);
1212 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_ssl);
1214 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_controller);
1215 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_fail_mode);
1216 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_mirrors);
1217 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_netflow);
1218 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_sflow);
1219 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_flood_vlans);
1220 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_other_config);
1222 ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_other_config);
1224 ovsdb_idl_add_column(ctx->idl,
1225 &ovsrec_interface_col_ingress_policing_rate);
1226 ovsdb_idl_add_column(ctx->idl,
1227 &ovsrec_interface_col_ingress_policing_burst);
1231 cmd_emer_reset(struct vsctl_context *ctx)
1233 const struct ovsdb_idl *idl = ctx->idl;
1234 const struct ovsrec_bridge *br;
1235 const struct ovsrec_port *port;
1236 const struct ovsrec_interface *iface;
1237 const struct ovsrec_mirror *mirror, *next_mirror;
1238 const struct ovsrec_controller *ctrl, *next_ctrl;
1239 const struct ovsrec_manager *mgr, *next_mgr;
1240 const struct ovsrec_netflow *nf, *next_nf;
1241 const struct ovsrec_ssl *ssl, *next_ssl;
1242 const struct ovsrec_sflow *sflow, *next_sflow;
1244 /* Reset the Open_vSwitch table. */
1245 ovsrec_open_vswitch_set_manager_options(ctx->ovs, NULL, 0);
1246 ovsrec_open_vswitch_set_ssl(ctx->ovs, NULL);
1248 OVSREC_BRIDGE_FOR_EACH (br, idl) {
1250 char *hw_key = "hwaddr";
1251 char *hw_val = NULL;
1253 ovsrec_bridge_set_controller(br, NULL, 0);
1254 ovsrec_bridge_set_fail_mode(br, NULL);
1255 ovsrec_bridge_set_mirrors(br, NULL, 0);
1256 ovsrec_bridge_set_netflow(br, NULL);
1257 ovsrec_bridge_set_sflow(br, NULL);
1258 ovsrec_bridge_set_flood_vlans(br, NULL, 0);
1260 /* We only want to save the "hwaddr" key from other_config. */
1261 for (i=0; i < br->n_other_config; i++) {
1262 if (!strcmp(br->key_other_config[i], hw_key)) {
1263 hw_val = br->value_other_config[i];
1268 char *val = xstrdup(hw_val);
1269 ovsrec_bridge_set_other_config(br, &hw_key, &val, 1);
1272 ovsrec_bridge_set_other_config(br, NULL, NULL, 0);
1276 OVSREC_PORT_FOR_EACH (port, idl) {
1277 ovsrec_port_set_other_config(port, NULL, NULL, 0);
1280 OVSREC_INTERFACE_FOR_EACH (iface, idl) {
1281 /* xxx What do we do about gre/patch devices created by mgr? */
1283 ovsrec_interface_set_ingress_policing_rate(iface, 0);
1284 ovsrec_interface_set_ingress_policing_burst(iface, 0);
1287 OVSREC_MIRROR_FOR_EACH_SAFE (mirror, next_mirror, idl) {
1288 ovsrec_mirror_delete(mirror);
1291 OVSREC_CONTROLLER_FOR_EACH_SAFE (ctrl, next_ctrl, idl) {
1292 ovsrec_controller_delete(ctrl);
1295 OVSREC_MANAGER_FOR_EACH_SAFE (mgr, next_mgr, idl) {
1296 ovsrec_manager_delete(mgr);
1299 OVSREC_NETFLOW_FOR_EACH_SAFE (nf, next_nf, idl) {
1300 ovsrec_netflow_delete(nf);
1303 OVSREC_SSL_FOR_EACH_SAFE (ssl, next_ssl, idl) {
1304 ovsrec_ssl_delete(ssl);
1307 OVSREC_SFLOW_FOR_EACH_SAFE (sflow, next_sflow, idl) {
1308 ovsrec_sflow_delete(sflow);
1313 cmd_add_br(struct vsctl_context *ctx)
1315 bool may_exist = shash_find(&ctx->options, "--may-exist") != NULL;
1316 const char *br_name, *parent_name;
1317 struct vsctl_info info;
1320 br_name = ctx->argv[1];
1321 if (ctx->argc == 2) {
1324 } else if (ctx->argc == 4) {
1325 parent_name = ctx->argv[2];
1326 vlan = atoi(ctx->argv[3]);
1327 if (vlan < 1 || vlan > 4095) {
1328 vsctl_fatal("%s: vlan must be between 1 and 4095", ctx->argv[0]);
1331 vsctl_fatal("'%s' command takes exactly 1 or 3 arguments",
1335 get_info(ctx, &info);
1337 struct vsctl_bridge *br;
1339 br = find_bridge(&info, br_name, false);
1343 vsctl_fatal("\"--may-exist add-br %s\" but %s is "
1344 "a VLAN bridge for VLAN %d",
1345 br_name, br_name, br->vlan);
1349 vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
1350 "is not a VLAN bridge",
1351 br_name, parent_name, vlan, br_name);
1352 } else if (strcmp(br->parent->name, parent_name)) {
1353 vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
1354 "has the wrong parent %s",
1355 br_name, parent_name, vlan,
1356 br_name, br->parent->name);
1357 } else if (br->vlan != vlan) {
1358 vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
1359 "is a VLAN bridge for the wrong VLAN %d",
1360 br_name, parent_name, vlan, br_name, br->vlan);
1366 check_conflicts(&info, br_name,
1367 xasprintf("cannot create a bridge named %s", br_name));
1370 struct ovsrec_port *port;
1371 struct ovsrec_interface *iface;
1372 struct ovsrec_bridge *br;
1374 iface = ovsrec_interface_insert(ctx->txn);
1375 ovsrec_interface_set_name(iface, br_name);
1376 ovsrec_interface_set_type(iface, "internal");
1378 port = ovsrec_port_insert(ctx->txn);
1379 ovsrec_port_set_name(port, br_name);
1380 ovsrec_port_set_interfaces(port, &iface, 1);
1382 br = ovsrec_bridge_insert(ctx->txn);
1383 ovsrec_bridge_set_name(br, br_name);
1384 ovsrec_bridge_set_ports(br, &port, 1);
1386 ovs_insert_bridge(ctx->ovs, br);
1388 struct vsctl_bridge *parent;
1389 struct ovsrec_port *port;
1390 struct ovsrec_interface *iface;
1391 struct ovsrec_bridge *br;
1394 parent = find_bridge(&info, parent_name, false);
1395 if (parent && parent->vlan) {
1396 vsctl_fatal("cannot create bridge with fake bridge as parent");
1399 vsctl_fatal("parent bridge %s does not exist", parent_name);
1401 br = parent->br_cfg;
1403 iface = ovsrec_interface_insert(ctx->txn);
1404 ovsrec_interface_set_name(iface, br_name);
1405 ovsrec_interface_set_type(iface, "internal");
1407 port = ovsrec_port_insert(ctx->txn);
1408 ovsrec_port_set_name(port, br_name);
1409 ovsrec_port_set_interfaces(port, &iface, 1);
1410 ovsrec_port_set_fake_bridge(port, true);
1411 ovsrec_port_set_tag(port, &tag, 1);
1413 bridge_insert_port(br, port);
1420 del_port(struct vsctl_info *info, struct vsctl_port *port)
1422 struct shash_node *node;
1424 SHASH_FOR_EACH (node, &info->ifaces) {
1425 struct vsctl_iface *iface = node->data;
1426 if (iface->port == port) {
1427 ovsrec_interface_delete(iface->iface_cfg);
1430 ovsrec_port_delete(port->port_cfg);
1432 bridge_delete_port((port->bridge->parent
1433 ? port->bridge->parent->br_cfg
1434 : port->bridge->br_cfg), port->port_cfg);
1438 cmd_del_br(struct vsctl_context *ctx)
1440 bool must_exist = !shash_find(&ctx->options, "--if-exists");
1441 struct vsctl_bridge *bridge;
1442 struct vsctl_info info;
1444 get_info(ctx, &info);
1445 bridge = find_bridge(&info, ctx->argv[1], must_exist);
1447 struct shash_node *node;
1449 SHASH_FOR_EACH (node, &info.ports) {
1450 struct vsctl_port *port = node->data;
1451 if (port->bridge == bridge || port->bridge->parent == bridge
1452 || !strcmp(port->port_cfg->name, bridge->name)) {
1453 del_port(&info, port);
1456 if (bridge->br_cfg) {
1457 ovsrec_bridge_delete(bridge->br_cfg);
1458 ovs_delete_bridge(ctx->ovs, bridge->br_cfg);
1465 output_sorted(struct svec *svec, struct ds *output)
1471 SVEC_FOR_EACH (i, name, svec) {
1472 ds_put_format(output, "%s\n", name);
1477 cmd_list_br(struct vsctl_context *ctx)
1479 struct shash_node *node;
1480 struct vsctl_info info;
1481 struct svec bridges;
1483 get_info(ctx, &info);
1485 svec_init(&bridges);
1486 SHASH_FOR_EACH (node, &info.bridges) {
1487 struct vsctl_bridge *br = node->data;
1488 svec_add(&bridges, br->name);
1490 output_sorted(&bridges, &ctx->output);
1491 svec_destroy(&bridges);
1497 cmd_br_exists(struct vsctl_context *ctx)
1499 struct vsctl_info info;
1501 get_info(ctx, &info);
1502 if (!find_bridge(&info, ctx->argv[1], false)) {
1508 /* Returns true if 'b_prefix' (of length 'b_prefix_len') concatenated with 'b'
1509 * equals 'a', false otherwise. */
1511 key_matches(const char *a,
1512 const char *b_prefix, size_t b_prefix_len, const char *b)
1514 return !strncmp(a, b_prefix, b_prefix_len) && !strcmp(a + b_prefix_len, b);
1518 set_external_id(char **old_keys, char **old_values, size_t old_n,
1519 char *key, char *value,
1520 char ***new_keysp, char ***new_valuesp, size_t *new_np)
1527 new_keys = xmalloc(sizeof *new_keys * (old_n + 1));
1528 new_values = xmalloc(sizeof *new_values * (old_n + 1));
1530 for (i = 0; i < old_n; i++) {
1531 if (strcmp(key, old_keys[i])) {
1532 new_keys[new_n] = old_keys[i];
1533 new_values[new_n] = old_values[i];
1538 new_keys[new_n] = key;
1539 new_values[new_n] = value;
1542 *new_keysp = new_keys;
1543 *new_valuesp = new_values;
1548 pre_cmd_br_set_external_id(struct vsctl_context *ctx)
1551 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_external_ids);
1552 ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_external_ids);
1556 cmd_br_set_external_id(struct vsctl_context *ctx)
1558 struct vsctl_info info;
1559 struct vsctl_bridge *bridge;
1560 char **keys, **values;
1563 get_info(ctx, &info);
1564 bridge = find_bridge(&info, ctx->argv[1], true);
1565 if (bridge->br_cfg) {
1566 set_external_id(bridge->br_cfg->key_external_ids,
1567 bridge->br_cfg->value_external_ids,
1568 bridge->br_cfg->n_external_ids,
1569 ctx->argv[2], ctx->argc >= 4 ? ctx->argv[3] : NULL,
1570 &keys, &values, &n);
1571 ovsrec_bridge_verify_external_ids(bridge->br_cfg);
1572 ovsrec_bridge_set_external_ids(bridge->br_cfg, keys, values, n);
1574 char *key = xasprintf("fake-bridge-%s", ctx->argv[2]);
1575 struct vsctl_port *port = shash_find_data(&info.ports, ctx->argv[1]);
1576 set_external_id(port->port_cfg->key_external_ids,
1577 port->port_cfg->value_external_ids,
1578 port->port_cfg->n_external_ids,
1579 key, ctx->argc >= 4 ? ctx->argv[3] : NULL,
1580 &keys, &values, &n);
1581 ovsrec_port_verify_external_ids(port->port_cfg);
1582 ovsrec_port_set_external_ids(port->port_cfg, keys, values, n);
1592 get_external_id(char **keys, char **values, size_t n,
1593 const char *prefix, const char *key,
1596 size_t prefix_len = strlen(prefix);
1601 for (i = 0; i < n; i++) {
1602 if (!key && !strncmp(keys[i], prefix, prefix_len)) {
1603 svec_add_nocopy(&svec, xasprintf("%s=%s",
1604 keys[i] + prefix_len, values[i]));
1605 } else if (key && key_matches(keys[i], prefix, prefix_len, key)) {
1606 svec_add(&svec, values[i]);
1610 output_sorted(&svec, output);
1611 svec_destroy(&svec);
1615 pre_cmd_br_get_external_id(struct vsctl_context *ctx)
1617 pre_cmd_br_set_external_id(ctx);
1621 cmd_br_get_external_id(struct vsctl_context *ctx)
1623 struct vsctl_info info;
1624 struct vsctl_bridge *bridge;
1626 get_info(ctx, &info);
1627 bridge = find_bridge(&info, ctx->argv[1], true);
1628 if (bridge->br_cfg) {
1629 ovsrec_bridge_verify_external_ids(bridge->br_cfg);
1630 get_external_id(bridge->br_cfg->key_external_ids,
1631 bridge->br_cfg->value_external_ids,
1632 bridge->br_cfg->n_external_ids,
1633 "", ctx->argc >= 3 ? ctx->argv[2] : NULL,
1636 struct vsctl_port *port = shash_find_data(&info.ports, ctx->argv[1]);
1637 ovsrec_port_verify_external_ids(port->port_cfg);
1638 get_external_id(port->port_cfg->key_external_ids,
1639 port->port_cfg->value_external_ids,
1640 port->port_cfg->n_external_ids,
1641 "fake-bridge-", ctx->argc >= 3 ? ctx->argv[2] : NULL, &ctx->output);
1648 cmd_list_ports(struct vsctl_context *ctx)
1650 struct vsctl_bridge *br;
1651 struct shash_node *node;
1652 struct vsctl_info info;
1655 get_info(ctx, &info);
1656 br = find_bridge(&info, ctx->argv[1], true);
1657 ovsrec_bridge_verify_ports(br->br_cfg ? br->br_cfg : br->parent->br_cfg);
1660 SHASH_FOR_EACH (node, &info.ports) {
1661 struct vsctl_port *port = node->data;
1663 if (strcmp(port->port_cfg->name, br->name) && br == port->bridge) {
1664 svec_add(&ports, port->port_cfg->name);
1667 output_sorted(&ports, &ctx->output);
1668 svec_destroy(&ports);
1674 add_port(struct vsctl_context *ctx,
1675 const char *br_name, const char *port_name,
1676 bool may_exist, bool fake_iface,
1677 char *iface_names[], int n_ifaces,
1678 char *settings[], int n_settings)
1680 struct vsctl_info info;
1681 struct vsctl_bridge *bridge;
1682 struct ovsrec_interface **ifaces;
1683 struct ovsrec_port *port;
1686 get_info(ctx, &info);
1688 struct vsctl_port *vsctl_port;
1690 vsctl_port = find_port(&info, port_name, false);
1692 struct svec want_names, have_names;
1694 svec_init(&want_names);
1695 for (i = 0; i < n_ifaces; i++) {
1696 svec_add(&want_names, iface_names[i]);
1698 svec_sort(&want_names);
1700 svec_init(&have_names);
1701 for (i = 0; i < vsctl_port->port_cfg->n_interfaces; i++) {
1702 svec_add(&have_names,
1703 vsctl_port->port_cfg->interfaces[i]->name);
1705 svec_sort(&have_names);
1707 if (strcmp(vsctl_port->bridge->name, br_name)) {
1708 char *command = vsctl_context_to_string(ctx);
1709 vsctl_fatal("\"%s\" but %s is actually attached to bridge %s",
1710 command, port_name, vsctl_port->bridge->name);
1713 if (!svec_equal(&want_names, &have_names)) {
1714 char *have_names_string = svec_join(&have_names, ", ", "");
1715 char *command = vsctl_context_to_string(ctx);
1717 vsctl_fatal("\"%s\" but %s actually has interface(s) %s",
1718 command, port_name, have_names_string);
1721 svec_destroy(&want_names);
1722 svec_destroy(&have_names);
1727 check_conflicts(&info, port_name,
1728 xasprintf("cannot create a port named %s", port_name));
1729 for (i = 0; i < n_ifaces; i++) {
1730 check_conflicts(&info, iface_names[i],
1731 xasprintf("cannot create an interface named %s",
1734 bridge = find_bridge(&info, br_name, true);
1736 ifaces = xmalloc(n_ifaces * sizeof *ifaces);
1737 for (i = 0; i < n_ifaces; i++) {
1738 ifaces[i] = ovsrec_interface_insert(ctx->txn);
1739 ovsrec_interface_set_name(ifaces[i], iface_names[i]);
1742 port = ovsrec_port_insert(ctx->txn);
1743 ovsrec_port_set_name(port, port_name);
1744 ovsrec_port_set_interfaces(port, ifaces, n_ifaces);
1745 ovsrec_port_set_bond_fake_iface(port, fake_iface);
1749 int64_t tag = bridge->vlan;
1750 ovsrec_port_set_tag(port, &tag, 1);
1753 for (i = 0; i < n_settings; i++) {
1754 set_column(get_table("Port"), &port->header_, settings[i],
1758 bridge_insert_port((bridge->parent ? bridge->parent->br_cfg
1759 : bridge->br_cfg), port);
1765 cmd_add_port(struct vsctl_context *ctx)
1767 bool may_exist = shash_find(&ctx->options, "--may-exist") != NULL;
1769 add_port(ctx, ctx->argv[1], ctx->argv[2], may_exist, false,
1770 &ctx->argv[2], 1, &ctx->argv[3], ctx->argc - 3);
1774 cmd_add_bond(struct vsctl_context *ctx)
1776 bool may_exist = shash_find(&ctx->options, "--may-exist") != NULL;
1777 bool fake_iface = shash_find(&ctx->options, "--fake-iface");
1781 n_ifaces = ctx->argc - 3;
1782 for (i = 3; i < ctx->argc; i++) {
1783 if (strchr(ctx->argv[i], '=')) {
1789 vsctl_fatal("add-bond requires at least 2 interfaces, but only "
1790 "%d were specified", n_ifaces);
1793 add_port(ctx, ctx->argv[1], ctx->argv[2], may_exist, fake_iface,
1794 &ctx->argv[3], n_ifaces,
1795 &ctx->argv[n_ifaces + 3], ctx->argc - 3 - n_ifaces);
1799 cmd_del_port(struct vsctl_context *ctx)
1801 bool must_exist = !shash_find(&ctx->options, "--if-exists");
1802 bool with_iface = shash_find(&ctx->options, "--with-iface") != NULL;
1803 struct vsctl_port *port;
1804 struct vsctl_info info;
1806 get_info(ctx, &info);
1808 port = find_port(&info, ctx->argv[ctx->argc - 1], must_exist);
1810 const char *target = ctx->argv[ctx->argc - 1];
1811 struct vsctl_iface *iface;
1813 port = find_port(&info, target, false);
1815 iface = find_iface(&info, target, false);
1820 if (must_exist && !port) {
1821 vsctl_fatal("no port or interface named %s", target);
1826 if (ctx->argc == 3) {
1827 struct vsctl_bridge *bridge;
1829 bridge = find_bridge(&info, ctx->argv[1], true);
1830 if (port->bridge != bridge) {
1831 if (port->bridge->parent == bridge) {
1832 vsctl_fatal("bridge %s does not have a port %s (although "
1833 "its parent bridge %s does)",
1834 ctx->argv[1], ctx->argv[2],
1835 bridge->parent->name);
1837 vsctl_fatal("bridge %s does not have a port %s",
1838 ctx->argv[1], ctx->argv[2]);
1843 del_port(&info, port);
1850 cmd_port_to_br(struct vsctl_context *ctx)
1852 struct vsctl_port *port;
1853 struct vsctl_info info;
1855 get_info(ctx, &info);
1856 port = find_port(&info, ctx->argv[1], true);
1857 ds_put_format(&ctx->output, "%s\n", port->bridge->name);
1862 cmd_br_to_vlan(struct vsctl_context *ctx)
1864 struct vsctl_bridge *bridge;
1865 struct vsctl_info info;
1867 get_info(ctx, &info);
1868 bridge = find_bridge(&info, ctx->argv[1], true);
1869 ds_put_format(&ctx->output, "%d\n", bridge->vlan);
1874 cmd_br_to_parent(struct vsctl_context *ctx)
1876 struct vsctl_bridge *bridge;
1877 struct vsctl_info info;
1879 get_info(ctx, &info);
1880 bridge = find_bridge(&info, ctx->argv[1], true);
1881 if (bridge->parent) {
1882 bridge = bridge->parent;
1884 ds_put_format(&ctx->output, "%s\n", bridge->name);
1889 cmd_list_ifaces(struct vsctl_context *ctx)
1891 struct vsctl_bridge *br;
1892 struct shash_node *node;
1893 struct vsctl_info info;
1896 get_info(ctx, &info);
1897 br = find_bridge(&info, ctx->argv[1], true);
1901 SHASH_FOR_EACH (node, &info.ifaces) {
1902 struct vsctl_iface *iface = node->data;
1904 if (strcmp(iface->iface_cfg->name, br->name)
1905 && br == iface->port->bridge) {
1906 svec_add(&ifaces, iface->iface_cfg->name);
1909 output_sorted(&ifaces, &ctx->output);
1910 svec_destroy(&ifaces);
1916 cmd_iface_to_br(struct vsctl_context *ctx)
1918 struct vsctl_iface *iface;
1919 struct vsctl_info info;
1921 get_info(ctx, &info);
1922 iface = find_iface(&info, ctx->argv[1], true);
1923 ds_put_format(&ctx->output, "%s\n", iface->port->bridge->name);
1928 verify_controllers(struct ovsrec_bridge *bridge)
1933 ovsrec_bridge_verify_controller(bridge);
1934 for (i = 0; i < bridge->n_controller; i++) {
1935 ovsrec_controller_verify_target(bridge->controller[i]);
1941 pre_controller(struct vsctl_context *ctx)
1945 ovsdb_idl_add_column(ctx->idl, &ovsrec_controller_col_target);
1949 cmd_get_controller(struct vsctl_context *ctx)
1951 struct vsctl_info info;
1952 struct vsctl_bridge *br;
1953 struct svec targets;
1956 get_info(ctx, &info);
1957 br = find_bridge(&info, ctx->argv[1], true);
1958 verify_controllers(br->br_cfg);
1960 /* Print the targets in sorted order for reproducibility. */
1961 svec_init(&targets);
1962 for (i = 0; i < br->n_ctrl; i++) {
1963 svec_add(&targets, br->ctrl[i]->target);
1966 svec_sort(&targets);
1967 for (i = 0; i < targets.n; i++) {
1968 ds_put_format(&ctx->output, "%s\n", targets.names[i]);
1970 svec_destroy(&targets);
1976 delete_controllers(struct ovsrec_controller **controllers,
1977 size_t n_controllers)
1981 for (i = 0; i < n_controllers; i++) {
1982 ovsrec_controller_delete(controllers[i]);
1987 cmd_del_controller(struct vsctl_context *ctx)
1989 struct vsctl_info info;
1990 struct vsctl_bridge *br;
1992 get_info(ctx, &info);
1994 br = find_real_bridge(&info, ctx->argv[1], true);
1995 verify_controllers(br->br_cfg);
1998 delete_controllers(br->ctrl, br->n_ctrl);
1999 ovsrec_bridge_set_controller(br->br_cfg, NULL, 0);
2005 static struct ovsrec_controller **
2006 insert_controllers(struct ovsdb_idl_txn *txn, char *targets[], size_t n)
2008 struct ovsrec_controller **controllers;
2011 controllers = xmalloc(n * sizeof *controllers);
2012 for (i = 0; i < n; i++) {
2013 controllers[i] = ovsrec_controller_insert(txn);
2014 ovsrec_controller_set_target(controllers[i], targets[i]);
2021 cmd_set_controller(struct vsctl_context *ctx)
2023 struct vsctl_info info;
2024 struct vsctl_bridge *br;
2025 struct ovsrec_controller **controllers;
2028 get_info(ctx, &info);
2029 br = find_real_bridge(&info, ctx->argv[1], true);
2030 verify_controllers(br->br_cfg);
2032 delete_controllers(br->ctrl, br->n_ctrl);
2035 controllers = insert_controllers(ctx->txn, &ctx->argv[2], n);
2036 ovsrec_bridge_set_controller(br->br_cfg, controllers, n);
2043 cmd_get_fail_mode(struct vsctl_context *ctx)
2045 struct vsctl_info info;
2046 struct vsctl_bridge *br;
2048 get_info(ctx, &info);
2049 br = find_bridge(&info, ctx->argv[1], true);
2052 ovsrec_bridge_verify_fail_mode(br->br_cfg);
2054 if (br->fail_mode && strlen(br->fail_mode)) {
2055 ds_put_format(&ctx->output, "%s\n", br->fail_mode);
2062 cmd_del_fail_mode(struct vsctl_context *ctx)
2064 struct vsctl_info info;
2065 struct vsctl_bridge *br;
2067 get_info(ctx, &info);
2068 br = find_real_bridge(&info, ctx->argv[1], true);
2070 ovsrec_bridge_set_fail_mode(br->br_cfg, NULL);
2076 cmd_set_fail_mode(struct vsctl_context *ctx)
2078 struct vsctl_info info;
2079 struct vsctl_bridge *br;
2080 const char *fail_mode = ctx->argv[2];
2082 get_info(ctx, &info);
2083 br = find_real_bridge(&info, ctx->argv[1], true);
2085 if (strcmp(fail_mode, "standalone") && strcmp(fail_mode, "secure")) {
2086 vsctl_fatal("fail-mode must be \"standalone\" or \"secure\"");
2089 ovsrec_bridge_set_fail_mode(br->br_cfg, fail_mode);
2095 verify_managers(const struct ovsrec_open_vswitch *ovs)
2099 ovsrec_open_vswitch_verify_manager_options(ovs);
2101 for (i = 0; i < ovs->n_manager_options; ++i) {
2102 const struct ovsrec_manager *mgr = ovs->manager_options[i];
2104 ovsrec_manager_verify_target(mgr);
2109 pre_manager(struct vsctl_context *ctx)
2111 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_manager_options);
2112 ovsdb_idl_add_column(ctx->idl, &ovsrec_manager_col_target);
2116 cmd_get_manager(struct vsctl_context *ctx)
2118 const struct ovsrec_open_vswitch *ovs = ctx->ovs;
2119 struct svec targets;
2122 verify_managers(ovs);
2124 /* Print the targets in sorted order for reproducibility. */
2125 svec_init(&targets);
2127 for (i = 0; i < ovs->n_manager_options; i++) {
2128 svec_add(&targets, ovs->manager_options[i]->target);
2131 svec_sort_unique(&targets);
2132 for (i = 0; i < targets.n; i++) {
2133 ds_put_format(&ctx->output, "%s\n", targets.names[i]);
2135 svec_destroy(&targets);
2139 delete_managers(const struct vsctl_context *ctx)
2141 const struct ovsrec_open_vswitch *ovs = ctx->ovs;
2144 /* Delete Manager rows pointed to by 'manager_options' column. */
2145 for (i = 0; i < ovs->n_manager_options; i++) {
2146 ovsrec_manager_delete(ovs->manager_options[i]);
2149 /* Delete 'Manager' row refs in 'manager_options' column. */
2150 ovsrec_open_vswitch_set_manager_options(ovs, NULL, 0);
2154 cmd_del_manager(struct vsctl_context *ctx)
2156 const struct ovsrec_open_vswitch *ovs = ctx->ovs;
2158 verify_managers(ovs);
2159 delete_managers(ctx);
2163 insert_managers(struct vsctl_context *ctx, char *targets[], size_t n)
2165 struct ovsrec_manager **managers;
2168 /* Insert each manager in a new row in Manager table. */
2169 managers = xmalloc(n * sizeof *managers);
2170 for (i = 0; i < n; i++) {
2171 managers[i] = ovsrec_manager_insert(ctx->txn);
2172 ovsrec_manager_set_target(managers[i], targets[i]);
2175 /* Store uuids of new Manager rows in 'manager_options' column. */
2176 ovsrec_open_vswitch_set_manager_options(ctx->ovs, managers, n);
2181 cmd_set_manager(struct vsctl_context *ctx)
2183 const size_t n = ctx->argc - 1;
2185 verify_managers(ctx->ovs);
2186 delete_managers(ctx);
2187 insert_managers(ctx, &ctx->argv[1], n);
2191 pre_cmd_get_ssl(struct vsctl_context *ctx)
2193 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_ssl);
2195 ovsdb_idl_add_column(ctx->idl, &ovsrec_ssl_col_private_key);
2196 ovsdb_idl_add_column(ctx->idl, &ovsrec_ssl_col_certificate);
2197 ovsdb_idl_add_column(ctx->idl, &ovsrec_ssl_col_ca_cert);
2198 ovsdb_idl_add_column(ctx->idl, &ovsrec_ssl_col_bootstrap_ca_cert);
2202 cmd_get_ssl(struct vsctl_context *ctx)
2204 struct ovsrec_ssl *ssl = ctx->ovs->ssl;
2206 ovsrec_open_vswitch_verify_ssl(ctx->ovs);
2208 ovsrec_ssl_verify_private_key(ssl);
2209 ovsrec_ssl_verify_certificate(ssl);
2210 ovsrec_ssl_verify_ca_cert(ssl);
2211 ovsrec_ssl_verify_bootstrap_ca_cert(ssl);
2213 ds_put_format(&ctx->output, "Private key: %s\n", ssl->private_key);
2214 ds_put_format(&ctx->output, "Certificate: %s\n", ssl->certificate);
2215 ds_put_format(&ctx->output, "CA Certificate: %s\n", ssl->ca_cert);
2216 ds_put_format(&ctx->output, "Bootstrap: %s\n",
2217 ssl->bootstrap_ca_cert ? "true" : "false");
2222 pre_cmd_del_ssl(struct vsctl_context *ctx)
2224 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_ssl);
2228 cmd_del_ssl(struct vsctl_context *ctx)
2230 struct ovsrec_ssl *ssl = ctx->ovs->ssl;
2233 ovsrec_open_vswitch_verify_ssl(ctx->ovs);
2234 ovsrec_ssl_delete(ssl);
2235 ovsrec_open_vswitch_set_ssl(ctx->ovs, NULL);
2240 pre_cmd_set_ssl(struct vsctl_context *ctx)
2242 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_ssl);
2246 cmd_set_ssl(struct vsctl_context *ctx)
2248 bool bootstrap = shash_find(&ctx->options, "--bootstrap");
2249 struct ovsrec_ssl *ssl = ctx->ovs->ssl;
2251 ovsrec_open_vswitch_verify_ssl(ctx->ovs);
2253 ovsrec_ssl_delete(ssl);
2255 ssl = ovsrec_ssl_insert(ctx->txn);
2257 ovsrec_ssl_set_private_key(ssl, ctx->argv[1]);
2258 ovsrec_ssl_set_certificate(ssl, ctx->argv[2]);
2259 ovsrec_ssl_set_ca_cert(ssl, ctx->argv[3]);
2261 ovsrec_ssl_set_bootstrap_ca_cert(ssl, bootstrap);
2263 ovsrec_open_vswitch_set_ssl(ctx->ovs, ssl);
2266 /* Parameter commands. */
2268 struct vsctl_row_id {
2269 const struct ovsdb_idl_table_class *table;
2270 const struct ovsdb_idl_column *name_column;
2271 const struct ovsdb_idl_column *uuid_column;
2274 struct vsctl_table_class {
2275 struct ovsdb_idl_table_class *class;
2276 struct vsctl_row_id row_ids[2];
2279 static const struct vsctl_table_class tables[] = {
2280 {&ovsrec_table_bridge,
2281 {{&ovsrec_table_bridge, &ovsrec_bridge_col_name, NULL},
2282 {NULL, NULL, NULL}}},
2284 {&ovsrec_table_controller,
2285 {{&ovsrec_table_bridge,
2286 &ovsrec_bridge_col_name,
2287 &ovsrec_bridge_col_controller}}},
2289 {&ovsrec_table_interface,
2290 {{&ovsrec_table_interface, &ovsrec_interface_col_name, NULL},
2291 {NULL, NULL, NULL}}},
2293 {&ovsrec_table_mirror,
2294 {{&ovsrec_table_mirror, &ovsrec_mirror_col_name, NULL},
2295 {NULL, NULL, NULL}}},
2297 {&ovsrec_table_manager,
2298 {{&ovsrec_table_manager, &ovsrec_manager_col_target, NULL},
2299 {NULL, NULL, NULL}}},
2301 {&ovsrec_table_netflow,
2302 {{&ovsrec_table_bridge,
2303 &ovsrec_bridge_col_name,
2304 &ovsrec_bridge_col_netflow},
2305 {NULL, NULL, NULL}}},
2307 {&ovsrec_table_open_vswitch,
2308 {{&ovsrec_table_open_vswitch, NULL, NULL},
2309 {NULL, NULL, NULL}}},
2311 {&ovsrec_table_port,
2312 {{&ovsrec_table_port, &ovsrec_port_col_name, NULL},
2313 {NULL, NULL, NULL}}},
2316 {{&ovsrec_table_port, &ovsrec_port_col_name, &ovsrec_port_col_qos},
2317 {NULL, NULL, NULL}}},
2319 {&ovsrec_table_queue,
2320 {{NULL, NULL, NULL},
2321 {NULL, NULL, NULL}}},
2324 {{&ovsrec_table_open_vswitch, NULL, &ovsrec_open_vswitch_col_ssl}}},
2326 {&ovsrec_table_sflow,
2327 {{&ovsrec_table_bridge,
2328 &ovsrec_bridge_col_name,
2329 &ovsrec_bridge_col_sflow},
2330 {NULL, NULL, NULL}}},
2332 {NULL, {{NULL, NULL, NULL}, {NULL, NULL, NULL}}}
2336 die_if_error(char *error)
2339 vsctl_fatal("%s", error);
2344 to_lower_and_underscores(unsigned c)
2346 return c == '-' ? '_' : tolower(c);
2350 score_partial_match(const char *name, const char *s)
2354 if (!strcmp(name, s)) {
2357 for (score = 0; ; score++, name++, s++) {
2358 if (to_lower_and_underscores(*name) != to_lower_and_underscores(*s)) {
2360 } else if (*name == '\0') {
2361 return UINT_MAX - 1;
2364 return *s == '\0' ? score : 0;
2367 static const struct vsctl_table_class *
2368 get_table(const char *table_name)
2370 const struct vsctl_table_class *table;
2371 const struct vsctl_table_class *best_match = NULL;
2372 unsigned int best_score = 0;
2374 for (table = tables; table->class; table++) {
2375 unsigned int score = score_partial_match(table->class->name,
2377 if (score > best_score) {
2380 } else if (score == best_score) {
2386 } else if (best_score) {
2387 vsctl_fatal("multiple table names match \"%s\"", table_name);
2389 vsctl_fatal("unknown table \"%s\"", table_name);
2393 static const struct vsctl_table_class *
2394 pre_get_table(struct vsctl_context *ctx, const char *table_name)
2396 const struct vsctl_table_class *table_class;
2399 table_class = get_table(table_name);
2400 ovsdb_idl_add_table(ctx->idl, table_class->class);
2402 for (i = 0; i < ARRAY_SIZE(table_class->row_ids); i++) {
2403 const struct vsctl_row_id *id = &table_class->row_ids[i];
2405 ovsdb_idl_add_table(ctx->idl, id->table);
2407 if (id->name_column) {
2408 ovsdb_idl_add_column(ctx->idl, id->name_column);
2410 if (id->uuid_column) {
2411 ovsdb_idl_add_column(ctx->idl, id->uuid_column);
2418 static const struct ovsdb_idl_row *
2419 get_row_by_id(struct vsctl_context *ctx, const struct vsctl_table_class *table,
2420 const struct vsctl_row_id *id, const char *record_id)
2422 const struct ovsdb_idl_row *referrer, *final;
2428 if (!id->name_column) {
2429 if (strcmp(record_id, ".")) {
2432 referrer = ovsdb_idl_first_row(ctx->idl, id->table);
2433 if (!referrer || ovsdb_idl_next_row(referrer)) {
2437 const struct ovsdb_idl_row *row;
2440 for (row = ovsdb_idl_first_row(ctx->idl, id->table);
2442 row = ovsdb_idl_next_row(row))
2444 const struct ovsdb_datum *name;
2446 name = ovsdb_idl_get(row, id->name_column,
2447 OVSDB_TYPE_STRING, OVSDB_TYPE_VOID);
2448 if (name->n == 1 && !strcmp(name->keys[0].string, record_id)) {
2450 vsctl_fatal("multiple rows in %s match \"%s\"",
2451 table->class->name, record_id);
2462 if (id->uuid_column) {
2463 const struct ovsdb_datum *uuid;
2465 ovsdb_idl_txn_verify(referrer, id->uuid_column);
2466 uuid = ovsdb_idl_get(referrer, id->uuid_column,
2467 OVSDB_TYPE_UUID, OVSDB_TYPE_VOID);
2469 final = ovsdb_idl_get_row_for_uuid(ctx->idl, table->class,
2470 &uuid->keys[0].uuid);
2479 static const struct ovsdb_idl_row *
2480 get_row (struct vsctl_context *ctx,
2481 const struct vsctl_table_class *table, const char *record_id)
2483 const struct ovsdb_idl_row *row;
2486 if (uuid_from_string(&uuid, record_id)) {
2487 row = ovsdb_idl_get_row_for_uuid(ctx->idl, table->class, &uuid);
2491 for (i = 0; i < ARRAY_SIZE(table->row_ids); i++) {
2492 row = get_row_by_id(ctx, table, &table->row_ids[i], record_id);
2501 static const struct ovsdb_idl_row *
2502 must_get_row(struct vsctl_context *ctx,
2503 const struct vsctl_table_class *table, const char *record_id)
2505 const struct ovsdb_idl_row *row = get_row(ctx, table, record_id);
2507 vsctl_fatal("no row \"%s\" in table %s",
2508 record_id, table->class->name);
2514 get_column(const struct vsctl_table_class *table, const char *column_name,
2515 const struct ovsdb_idl_column **columnp)
2517 const struct ovsdb_idl_column *best_match = NULL;
2518 unsigned int best_score = 0;
2521 for (i = 0; i < table->class->n_columns; i++) {
2522 const struct ovsdb_idl_column *column = &table->class->columns[i];
2523 unsigned int score = score_partial_match(column->name, column_name);
2524 if (score > best_score) {
2525 best_match = column;
2527 } else if (score == best_score) {
2532 *columnp = best_match;
2535 } else if (best_score) {
2536 return xasprintf("%s contains more than one column whose name "
2537 "matches \"%s\"", table->class->name, column_name);
2539 return xasprintf("%s does not contain a column whose name matches "
2540 "\"%s\"", table->class->name, column_name);
2544 static struct ovsdb_symbol *
2545 create_symbol(struct ovsdb_symbol_table *symtab, const char *id, bool *newp)
2547 struct ovsdb_symbol *symbol;
2550 vsctl_fatal("row id \"%s\" does not begin with \"@\"", id);
2554 *newp = ovsdb_symbol_table_get(symtab, id) == NULL;
2557 symbol = ovsdb_symbol_table_insert(symtab, id);
2558 if (symbol->created) {
2559 vsctl_fatal("row id \"%s\" may only be specified on one --id option",
2562 symbol->created = true;
2567 pre_get_column(struct vsctl_context *ctx,
2568 const struct vsctl_table_class *table, const char *column_name,
2569 const struct ovsdb_idl_column **columnp)
2571 die_if_error(get_column(table, column_name, columnp));
2572 ovsdb_idl_add_column(ctx->idl, *columnp);
2576 missing_operator_error(const char *arg, const char **allowed_operators,
2582 ds_put_format(&s, "%s: argument does not end in ", arg);
2583 ds_put_format(&s, "\"%s\"", allowed_operators[0]);
2584 if (n_allowed == 2) {
2585 ds_put_format(&s, " or \"%s\"", allowed_operators[1]);
2586 } else if (n_allowed > 2) {
2589 for (i = 1; i < n_allowed - 1; i++) {
2590 ds_put_format(&s, ", \"%s\"", allowed_operators[i]);
2592 ds_put_format(&s, ", or \"%s\"", allowed_operators[i]);
2594 ds_put_format(&s, " followed by a value.");
2596 return ds_steal_cstr(&s);
2599 /* Breaks 'arg' apart into a number of fields in the following order:
2601 * - The name of a column in 'table', stored into '*columnp'. The column
2602 * name may be abbreviated.
2604 * - Optionally ':' followed by a key string. The key is stored as a
2605 * malloc()'d string into '*keyp', or NULL if no key is present in
2608 * - If 'valuep' is nonnull, an operator followed by a value string. The
2609 * allowed operators are the 'n_allowed' string in 'allowed_operators',
2610 * or just "=" if 'n_allowed' is 0. If 'operatorp' is nonnull, then the
2611 * operator is stored into '*operatorp' (one of the pointers from
2612 * 'allowed_operators' is stored; nothing is malloc()'d). The value is
2613 * stored as a malloc()'d string into '*valuep', or NULL if no value is
2616 * On success, returns NULL. On failure, returned a malloc()'d string error
2617 * message and stores NULL into all of the nonnull output arguments. */
2618 static char * WARN_UNUSED_RESULT
2619 parse_column_key_value(const char *arg,
2620 const struct vsctl_table_class *table,
2621 const struct ovsdb_idl_column **columnp, char **keyp,
2622 const char **operatorp,
2623 const char **allowed_operators, size_t n_allowed,
2626 const char *p = arg;
2630 assert(!(operatorp && !valuep));
2636 /* Parse column name. */
2637 error = ovsdb_token_parse(&p, &column_name);
2641 if (column_name[0] == '\0') {
2643 error = xasprintf("%s: missing column name", arg);
2646 error = get_column(table, column_name, columnp);
2652 /* Parse key string. */
2655 error = ovsdb_token_parse(&p, keyp);
2661 /* Parse value string. */
2667 if (!allowed_operators) {
2668 static const char *equals = "=";
2669 allowed_operators = =
2675 for (i = 0; i < n_allowed; i++) {
2676 const char *op = allowed_operators[i];
2677 size_t op_len = strlen(op);
2679 if (op_len > best_len && !strncmp(op, p, op_len) && p[op_len]) {
2685 error = missing_operator_error(arg, allowed_operators, n_allowed);
2692 *valuep = xstrdup(p + best_len);
2695 error = xasprintf("%s: trailing garbage \"%s\" in argument",
2717 pre_parse_column_key_value(struct vsctl_context *ctx,
2719 const struct vsctl_table_class *table)
2721 const struct ovsdb_idl_column *column;
2726 die_if_error(ovsdb_token_parse(&p, &column_name));
2727 if (column_name[0] == '\0') {
2728 vsctl_fatal("%s: missing column name", arg);
2731 pre_get_column(ctx, table, column_name, &column);
2736 pre_cmd_get(struct vsctl_context *ctx)
2738 const char *id = shash_find_data(&ctx->options, "--id");
2739 const char *table_name = ctx->argv[1];
2740 const struct vsctl_table_class *table;
2743 /* Using "get" without --id or a column name could possibly make sense.
2744 * Maybe, for example, a ovs-vsctl run wants to assert that a row exists.
2745 * But it is unlikely that an interactive user would want to do that, so
2746 * issue a warning if we're running on a terminal. */
2747 if (!id && ctx->argc <= 3 && isatty(STDOUT_FILENO)) {
2748 VLOG_WARN("\"get\" command without row arguments or \"--id\" is "
2749 "possibly erroneous");
2752 table = pre_get_table(ctx, table_name);
2753 for (i = 3; i < ctx->argc; i++) {
2754 if (!strcasecmp(ctx->argv[i], "_uuid")
2755 || !strcasecmp(ctx->argv[i], "-uuid")) {
2759 pre_parse_column_key_value(ctx, ctx->argv[i], table);
2764 cmd_get(struct vsctl_context *ctx)
2766 const char *id = shash_find_data(&ctx->options, "--id");
2767 bool if_exists = shash_find(&ctx->options, "--if-exists");
2768 const char *table_name = ctx->argv[1];
2769 const char *record_id = ctx->argv[2];
2770 const struct vsctl_table_class *table;
2771 const struct ovsdb_idl_row *row;
2772 struct ds *out = &ctx->output;
2775 table = get_table(table_name);
2776 row = must_get_row(ctx, table, record_id);
2778 struct ovsdb_symbol *symbol;
2781 symbol = create_symbol(ctx->symtab, id, &new);
2783 vsctl_fatal("row id \"%s\" specified on \"get\" command was used "
2784 "before it was defined", id);
2786 symbol->uuid = row->uuid;
2788 /* This symbol refers to a row that already exists, so disable warnings
2789 * about it being unreferenced. */
2790 symbol->strong_ref = true;
2792 for (i = 3; i < ctx->argc; i++) {
2793 const struct ovsdb_idl_column *column;
2794 const struct ovsdb_datum *datum;
2797 /* Special case for obtaining the UUID of a row. We can't just do this
2798 * through parse_column_key_value() below since it returns a "struct
2799 * ovsdb_idl_column" and the UUID column doesn't have one. */
2800 if (!strcasecmp(ctx->argv[i], "_uuid")
2801 || !strcasecmp(ctx->argv[i], "-uuid")) {
2802 ds_put_format(out, UUID_FMT"\n", UUID_ARGS(&row->uuid));
2806 die_if_error(parse_column_key_value(ctx->argv[i], table,
2807 &column, &key_string,
2808 NULL, NULL, 0, NULL));
2810 ovsdb_idl_txn_verify(row, column);
2811 datum = ovsdb_idl_read(row, column);
2813 union ovsdb_atom key;
2816 if (column->type.value.type == OVSDB_TYPE_VOID) {
2817 vsctl_fatal("cannot specify key to get for non-map column %s",
2821 die_if_error(ovsdb_atom_from_string(&key,
2823 key_string, ctx->symtab));
2825 idx = ovsdb_datum_find_key(datum, &key,
2826 column->type.key.type);
2827 if (idx == UINT_MAX) {
2829 vsctl_fatal("no key \"%s\" in %s record \"%s\" column %s",
2830 key_string, table->class->name, record_id,
2834 ovsdb_atom_to_string(&datum->values[idx],
2835 column->type.value.type, out);
2837 ovsdb_atom_destroy(&key, column->type.key.type);
2839 ovsdb_datum_to_string(datum, &column->type, out);
2841 ds_put_char(out, '\n');
2848 parse_column_names(const char *column_names,
2849 const struct vsctl_table_class *table,
2850 const struct ovsdb_idl_column ***columnsp,
2853 const struct ovsdb_idl_column **columns;
2856 if (!column_names) {
2859 n_columns = table->class->n_columns + 1;
2860 columns = xmalloc(n_columns * sizeof *columns);
2862 for (i = 0; i < table->class->n_columns; i++) {
2863 columns[i + 1] = &table->class->columns[i];
2866 char *s = xstrdup(column_names);
2867 size_t allocated_columns;
2868 char *save_ptr = NULL;
2872 allocated_columns = n_columns = 0;
2873 for (column_name = strtok_r(s, ", ", &save_ptr); column_name;
2874 column_name = strtok_r(NULL, ", ", &save_ptr)) {
2875 const struct ovsdb_idl_column *column;
2877 if (!strcasecmp(column_name, "_uuid")) {
2880 die_if_error(get_column(table, column_name, &column));
2882 if (n_columns >= allocated_columns) {
2883 columns = x2nrealloc(columns, &allocated_columns,
2886 columns[n_columns++] = column;
2891 vsctl_fatal("must specify at least one column name");
2894 *columnsp = columns;
2895 *n_columnsp = n_columns;
2900 pre_list_columns(struct vsctl_context *ctx,
2901 const struct vsctl_table_class *table,
2902 const char *column_names)
2904 const struct ovsdb_idl_column **columns;
2908 parse_column_names(column_names, table, &columns, &n_columns);
2909 for (i = 0; i < n_columns; i++) {
2911 ovsdb_idl_add_column(ctx->idl, columns[i]);
2918 pre_cmd_list(struct vsctl_context *ctx)
2920 const char *column_names = shash_find_data(&ctx->options, "--columns");
2921 const char *table_name = ctx->argv[1];
2922 const struct vsctl_table_class *table;
2924 table = pre_get_table(ctx, table_name);
2925 pre_list_columns(ctx, table, column_names);
2928 static struct table *
2929 list_make_table(const struct ovsdb_idl_column **columns, size_t n_columns)
2934 out = xmalloc(sizeof *out);
2937 for (i = 0; i < n_columns; i++) {
2938 const struct ovsdb_idl_column *column = columns[i];
2939 const char *column_name = column ? column->name : "_uuid";
2941 table_add_column(out, "%s", column_name);
2948 list_record(const struct ovsdb_idl_row *row,
2949 const struct ovsdb_idl_column **columns, size_t n_columns,
2955 for (i = 0; i < n_columns; i++) {
2956 const struct ovsdb_idl_column *column = columns[i];
2957 struct cell *cell = table_add_cell(out);
2960 struct ovsdb_datum datum;
2961 union ovsdb_atom atom;
2963 atom.uuid = row->uuid;
2966 datum.values = NULL;
2969 cell->json = ovsdb_datum_to_json(&datum, &ovsdb_type_uuid);
2970 cell->type = &ovsdb_type_uuid;
2972 const struct ovsdb_datum *datum = ovsdb_idl_read(row, column);
2974 cell->json = ovsdb_datum_to_json(datum, &column->type);
2975 cell->type = &column->type;
2981 cmd_list(struct vsctl_context *ctx)
2983 const char *column_names = shash_find_data(&ctx->options, "--columns");
2984 const struct ovsdb_idl_column **columns;
2985 const char *table_name = ctx->argv[1];
2986 const struct vsctl_table_class *table;
2991 table = get_table(table_name);
2992 parse_column_names(column_names, table, &columns, &n_columns);
2993 out = ctx->table = list_make_table(columns, n_columns);
2994 if (ctx->argc > 2) {
2995 for (i = 2; i < ctx->argc; i++) {
2996 list_record(must_get_row(ctx, table, ctx->argv[i]),
2997 columns, n_columns, out);
3000 const struct ovsdb_idl_row *row;
3002 for (row = ovsdb_idl_first_row(ctx->idl, table->class); row != NULL;
3003 row = ovsdb_idl_next_row(row)) {
3004 list_record(row, columns, n_columns, out);
3011 pre_cmd_find(struct vsctl_context *ctx)
3013 const char *column_names = shash_find_data(&ctx->options, "--columns");
3014 const char *table_name = ctx->argv[1];
3015 const struct vsctl_table_class *table;
3018 table = pre_get_table(ctx, table_name);
3019 pre_list_columns(ctx, table, column_names);
3020 for (i = 2; i < ctx->argc; i++) {
3021 pre_parse_column_key_value(ctx, ctx->argv[i], table);
3026 cmd_find(struct vsctl_context *ctx)
3028 const char *column_names = shash_find_data(&ctx->options, "--columns");
3029 const struct ovsdb_idl_column **columns;
3030 const char *table_name = ctx->argv[1];
3031 const struct vsctl_table_class *table;
3032 const struct ovsdb_idl_row *row;
3036 table = get_table(table_name);
3037 parse_column_names(column_names, table, &columns, &n_columns);
3038 out = ctx->table = list_make_table(columns, n_columns);
3039 for (row = ovsdb_idl_first_row(ctx->idl, table->class); row;
3040 row = ovsdb_idl_next_row(row)) {
3043 for (i = 2; i < ctx->argc; i++) {
3044 if (!is_condition_satisfied(table, row, ctx->argv[i],
3049 list_record(row, columns, n_columns, out);
3057 pre_cmd_set(struct vsctl_context *ctx)
3059 const char *table_name = ctx->argv[1];
3060 const struct vsctl_table_class *table;
3063 table = pre_get_table(ctx, table_name);
3064 for (i = 3; i < ctx->argc; i++) {
3065 pre_parse_column_key_value(ctx, ctx->argv[i], table);
3070 set_column(const struct vsctl_table_class *table,
3071 const struct ovsdb_idl_row *row, const char *arg,
3072 struct ovsdb_symbol_table *symtab)
3074 const struct ovsdb_idl_column *column;
3075 char *key_string, *value_string;
3078 error = parse_column_key_value(arg, table, &column, &key_string,
3079 NULL, NULL, 0, &value_string);
3080 die_if_error(error);
3081 if (!value_string) {
3082 vsctl_fatal("%s: missing value", arg);
3086 union ovsdb_atom key, value;
3087 struct ovsdb_datum datum;
3089 if (column->type.value.type == OVSDB_TYPE_VOID) {
3090 vsctl_fatal("cannot specify key to set for non-map column %s",
3094 die_if_error(ovsdb_atom_from_string(&key, &column->type.key,
3095 key_string, symtab));
3096 die_if_error(ovsdb_atom_from_string(&value, &column->type.value,
3097 value_string, symtab));
3099 ovsdb_datum_init_empty(&datum);
3100 ovsdb_datum_add_unsafe(&datum, &key, &value, &column->type);
3102 ovsdb_atom_destroy(&key, column->type.key.type);
3103 ovsdb_atom_destroy(&value, column->type.value.type);
3105 ovsdb_datum_union(&datum, ovsdb_idl_read(row, column),
3106 &column->type, false);
3107 ovsdb_idl_txn_write(row, column, &datum);
3109 struct ovsdb_datum datum;
3111 die_if_error(ovsdb_datum_from_string(&datum, &column->type,
3112 value_string, symtab));
3113 ovsdb_idl_txn_write(row, column, &datum);
3121 cmd_set(struct vsctl_context *ctx)
3123 const char *table_name = ctx->argv[1];
3124 const char *record_id = ctx->argv[2];
3125 const struct vsctl_table_class *table;
3126 const struct ovsdb_idl_row *row;
3129 table = get_table(table_name);
3130 row = must_get_row(ctx, table, record_id);
3131 for (i = 3; i < ctx->argc; i++) {
3132 set_column(table, row, ctx->argv[i], ctx->symtab);
3137 pre_cmd_add(struct vsctl_context *ctx)
3139 const char *table_name = ctx->argv[1];
3140 const char *column_name = ctx->argv[3];
3141 const struct vsctl_table_class *table;
3142 const struct ovsdb_idl_column *column;
3144 table = pre_get_table(ctx, table_name);
3145 pre_get_column(ctx, table, column_name, &column);
3149 cmd_add(struct vsctl_context *ctx)
3151 const char *table_name = ctx->argv[1];
3152 const char *record_id = ctx->argv[2];
3153 const char *column_name = ctx->argv[3];
3154 const struct vsctl_table_class *table;
3155 const struct ovsdb_idl_column *column;
3156 const struct ovsdb_idl_row *row;
3157 const struct ovsdb_type *type;
3158 struct ovsdb_datum old;
3161 table = get_table(table_name);
3162 row = must_get_row(ctx, table, record_id);
3163 die_if_error(get_column(table, column_name, &column));
3165 type = &column->type;
3166 ovsdb_datum_clone(&old, ovsdb_idl_read(row, column), &column->type);
3167 for (i = 4; i < ctx->argc; i++) {
3168 struct ovsdb_type add_type;
3169 struct ovsdb_datum add;
3173 add_type.n_max = UINT_MAX;
3174 die_if_error(ovsdb_datum_from_string(&add, &add_type, ctx->argv[i],
3176 ovsdb_datum_union(&old, &add, type, false);
3177 ovsdb_datum_destroy(&add, type);
3179 if (old.n > type->n_max) {
3180 vsctl_fatal("\"add\" operation would put %u %s in column %s of "
3181 "table %s but the maximum number is %u",
3183 type->value.type == OVSDB_TYPE_VOID ? "values" : "pairs",
3184 column->name, table->class->name, type->n_max);
3186 ovsdb_idl_txn_verify(row, column);
3187 ovsdb_idl_txn_write(row, column, &old);
3191 pre_cmd_remove(struct vsctl_context *ctx)
3193 const char *table_name = ctx->argv[1];
3194 const char *column_name = ctx->argv[3];
3195 const struct vsctl_table_class *table;
3196 const struct ovsdb_idl_column *column;
3198 table = pre_get_table(ctx, table_name);
3199 pre_get_column(ctx, table, column_name, &column);
3203 cmd_remove(struct vsctl_context *ctx)
3205 const char *table_name = ctx->argv[1];
3206 const char *record_id = ctx->argv[2];
3207 const char *column_name = ctx->argv[3];
3208 const struct vsctl_table_class *table;
3209 const struct ovsdb_idl_column *column;
3210 const struct ovsdb_idl_row *row;
3211 const struct ovsdb_type *type;
3212 struct ovsdb_datum old;
3215 table = get_table(table_name);
3216 row = must_get_row(ctx, table, record_id);
3217 die_if_error(get_column(table, column_name, &column));
3219 type = &column->type;
3220 ovsdb_datum_clone(&old, ovsdb_idl_read(row, column), &column->type);
3221 for (i = 4; i < ctx->argc; i++) {
3222 struct ovsdb_type rm_type;
3223 struct ovsdb_datum rm;
3228 rm_type.n_max = UINT_MAX;
3229 error = ovsdb_datum_from_string(&rm, &rm_type,
3230 ctx->argv[i], ctx->symtab);
3231 if (error && ovsdb_type_is_map(&rm_type)) {
3233 rm_type.value.type = OVSDB_TYPE_VOID;
3234 die_if_error(ovsdb_datum_from_string(&rm, &rm_type,
3235 ctx->argv[i], ctx->symtab));
3237 ovsdb_datum_subtract(&old, type, &rm, &rm_type);
3238 ovsdb_datum_destroy(&rm, &rm_type);
3240 if (old.n < type->n_min) {
3241 vsctl_fatal("\"remove\" operation would put %u %s in column %s of "
3242 "table %s but the minimum number is %u",
3244 type->value.type == OVSDB_TYPE_VOID ? "values" : "pairs",
3245 column->name, table->class->name, type->n_min);
3247 ovsdb_idl_txn_verify(row, column);
3248 ovsdb_idl_txn_write(row, column, &old);
3252 pre_cmd_clear(struct vsctl_context *ctx)
3254 const char *table_name = ctx->argv[1];
3255 const struct vsctl_table_class *table;
3258 table = pre_get_table(ctx, table_name);
3259 for (i = 3; i < ctx->argc; i++) {
3260 const struct ovsdb_idl_column *column;
3262 pre_get_column(ctx, table, ctx->argv[i], &column);
3267 cmd_clear(struct vsctl_context *ctx)
3269 const char *table_name = ctx->argv[1];
3270 const char *record_id = ctx->argv[2];
3271 const struct vsctl_table_class *table;
3272 const struct ovsdb_idl_row *row;
3275 table = get_table(table_name);
3276 row = must_get_row(ctx, table, record_id);
3277 for (i = 3; i < ctx->argc; i++) {
3278 const struct ovsdb_idl_column *column;
3279 const struct ovsdb_type *type;
3280 struct ovsdb_datum datum;
3282 die_if_error(get_column(table, ctx->argv[i], &column));
3284 type = &column->type;
3285 if (type->n_min > 0) {
3286 vsctl_fatal("\"clear\" operation cannot be applied to column %s "
3287 "of table %s, which is not allowed to be empty",
3288 column->name, table->class->name);
3291 ovsdb_datum_init_empty(&datum);
3292 ovsdb_idl_txn_write(row, column, &datum);
3297 pre_create(struct vsctl_context *ctx)
3299 const char *id = shash_find_data(&ctx->options, "--id");
3300 const char *table_name = ctx->argv[1];
3301 const struct vsctl_table_class *table;
3303 table = get_table(table_name);
3304 if (!id && !table->class->is_root) {
3305 VLOG_WARN("applying \"create\" command to table %s without --id "
3306 "option will have no effect", table->class->name);
3311 cmd_create(struct vsctl_context *ctx)
3313 const char *id = shash_find_data(&ctx->options, "--id");
3314 const char *table_name = ctx->argv[1];
3315 const struct vsctl_table_class *table = get_table(table_name);
3316 const struct ovsdb_idl_row *row;
3317 const struct uuid *uuid;
3321 struct ovsdb_symbol *symbol = create_symbol(ctx->symtab, id, NULL);
3322 if (table->class->is_root) {
3323 /* This table is in the root set, meaning that rows created in it
3324 * won't disappear even if they are unreferenced, so disable
3325 * warnings about that by pretending that there is a reference. */
3326 symbol->strong_ref = true;
3328 uuid = &symbol->uuid;
3333 row = ovsdb_idl_txn_insert(ctx->txn, table->class, uuid);
3334 for (i = 2; i < ctx->argc; i++) {
3335 set_column(table, row, ctx->argv[i], ctx->symtab);
3337 ds_put_format(&ctx->output, UUID_FMT, UUID_ARGS(&row->uuid));
3340 /* This function may be used as the 'postprocess' function for commands that
3341 * insert new rows into the database. It expects that the command's 'run'
3342 * function prints the UUID reported by ovsdb_idl_txn_insert() as the command's
3343 * sole output. It replaces that output by the row's permanent UUID assigned
3344 * by the database server and appends a new-line.
3346 * Currently we use this only for "create", because the higher-level commands
3347 * are supposed to be independent of the actual structure of the vswitch
3350 post_create(struct vsctl_context *ctx)
3352 const struct uuid *real;
3355 if (!uuid_from_string(&dummy, ds_cstr(&ctx->output))) {
3358 real = ovsdb_idl_txn_get_insert_uuid(ctx->txn, &dummy);
3360 ds_clear(&ctx->output);
3361 ds_put_format(&ctx->output, UUID_FMT, UUID_ARGS(real));
3363 ds_put_char(&ctx->output, '\n');
3367 pre_cmd_destroy(struct vsctl_context *ctx)
3369 const char *table_name = ctx->argv[1];
3371 pre_get_table(ctx, table_name);
3375 cmd_destroy(struct vsctl_context *ctx)
3377 bool must_exist = !shash_find(&ctx->options, "--if-exists");
3378 const char *table_name = ctx->argv[1];
3379 const struct vsctl_table_class *table;
3382 table = get_table(table_name);
3383 for (i = 2; i < ctx->argc; i++) {
3384 const struct ovsdb_idl_row *row;
3386 row = (must_exist ? must_get_row : get_row)(ctx, table, ctx->argv[i]);
3388 ovsdb_idl_txn_delete(row);
3394 is_condition_satisfied(const struct vsctl_table_class *table,
3395 const struct ovsdb_idl_row *row, const char *arg,
3396 struct ovsdb_symbol_table *symtab)
3398 static const char *operators[] = {
3399 "=", "!=", "<", ">", "<=", ">="
3402 const struct ovsdb_idl_column *column;
3403 const struct ovsdb_datum *have_datum;
3404 char *key_string, *value_string;
3405 const char *operator;
3410 error = parse_column_key_value(arg, table, &column, &key_string,
3411 &operator, operators, ARRAY_SIZE(operators),
3413 die_if_error(error);
3414 if (!value_string) {
3415 vsctl_fatal("%s: missing value", arg);
3418 have_datum = ovsdb_idl_read(row, column);
3420 union ovsdb_atom want_key, want_value;
3422 if (column->type.value.type == OVSDB_TYPE_VOID) {
3423 vsctl_fatal("cannot specify key to check for non-map column %s",
3427 die_if_error(ovsdb_atom_from_string(&want_key, &column->type.key,
3428 key_string, symtab));
3429 die_if_error(ovsdb_atom_from_string(&want_value, &column->type.value,
3430 value_string, symtab));
3432 idx = ovsdb_datum_find_key(have_datum,
3433 &want_key, column->type.key.type);
3434 if (idx != UINT_MAX) {
3435 cmp = ovsdb_atom_compare_3way(&have_datum->values[idx],
3437 column->type.value.type);
3440 ovsdb_atom_destroy(&want_key, column->type.key.type);
3441 ovsdb_atom_destroy(&want_value, column->type.value.type);
3443 struct ovsdb_datum want_datum;
3445 die_if_error(ovsdb_datum_from_string(&want_datum, &column->type,
3446 value_string, symtab));
3448 cmp = ovsdb_datum_compare_3way(have_datum, &want_datum,
3450 ovsdb_datum_destroy(&want_datum, &column->type);
3456 return (idx == UINT_MAX ? false
3457 : !strcmp(operator, "=") ? cmp == 0
3458 : !strcmp(operator, "!=") ? cmp != 0
3459 : !strcmp(operator, "<") ? cmp < 0
3460 : !strcmp(operator, ">") ? cmp > 0
3461 : !strcmp(operator, "<=") ? cmp <= 0
3462 : !strcmp(operator, ">=") ? cmp >= 0
3467 pre_cmd_wait_until(struct vsctl_context *ctx)
3469 const char *table_name = ctx->argv[1];
3470 const struct vsctl_table_class *table;
3473 table = pre_get_table(ctx, table_name);
3475 for (i = 3; i < ctx->argc; i++) {
3476 pre_parse_column_key_value(ctx, ctx->argv[i], table);
3481 cmd_wait_until(struct vsctl_context *ctx)
3483 const char *table_name = ctx->argv[1];
3484 const char *record_id = ctx->argv[2];
3485 const struct vsctl_table_class *table;
3486 const struct ovsdb_idl_row *row;
3489 table = get_table(table_name);
3491 row = get_row(ctx, table, record_id);
3493 ctx->try_again = true;
3497 for (i = 3; i < ctx->argc; i++) {
3498 if (!is_condition_satisfied(table, row, ctx->argv[i], ctx->symtab)) {
3499 ctx->try_again = true;
3505 static struct json *
3506 where_uuid_equals(const struct uuid *uuid)
3509 json_array_create_1(
3510 json_array_create_3(
3511 json_string_create("_uuid"),
3512 json_string_create("=="),
3513 json_array_create_2(
3514 json_string_create("uuid"),
3515 json_string_create_nocopy(
3516 xasprintf(UUID_FMT, UUID_ARGS(uuid))))));
3520 vsctl_context_init(struct vsctl_context *ctx, struct vsctl_command *command,
3521 struct ovsdb_idl *idl, struct ovsdb_idl_txn *txn,
3522 const struct ovsrec_open_vswitch *ovs,
3523 struct ovsdb_symbol_table *symtab)
3525 ctx->argc = command->argc;
3526 ctx->argv = command->argv;
3527 ctx->options = command->options;
3529 ds_swap(&ctx->output, &command->output);
3530 ctx->table = command->table;
3534 ctx->symtab = symtab;
3535 ctx->verified_ports = false;
3537 ctx->try_again = false;
3541 vsctl_context_done(struct vsctl_context *ctx, struct vsctl_command *command)
3543 ds_swap(&ctx->output, &command->output);
3544 command->table = ctx->table;
3548 run_prerequisites(struct vsctl_command *commands, size_t n_commands,
3549 struct ovsdb_idl *idl)
3551 struct vsctl_command *c;
3553 ovsdb_idl_add_table(idl, &ovsrec_table_open_vswitch);
3554 if (wait_for_reload) {
3555 ovsdb_idl_add_column(idl, &ovsrec_open_vswitch_col_cur_cfg);
3557 for (c = commands; c < &commands[n_commands]; c++) {
3558 if (c->syntax->prerequisites) {
3559 struct vsctl_context ctx;
3561 ds_init(&c->output);
3564 vsctl_context_init(&ctx, c, idl, NULL, NULL, NULL);
3565 (c->syntax->prerequisites)(&ctx);
3566 vsctl_context_done(&ctx, c);
3568 assert(!c->output.string);
3575 do_vsctl(const char *args, struct vsctl_command *commands, size_t n_commands,
3576 struct ovsdb_idl *idl)
3578 struct ovsdb_idl_txn *txn;
3579 const struct ovsrec_open_vswitch *ovs;
3580 enum ovsdb_idl_txn_status status;
3581 struct ovsdb_symbol_table *symtab;
3582 struct vsctl_command *c;
3583 struct shash_node *node;
3584 int64_t next_cfg = 0;
3587 txn = the_idl_txn = ovsdb_idl_txn_create(idl);
3589 ovsdb_idl_txn_set_dry_run(txn);
3592 ovsdb_idl_txn_add_comment(txn, "ovs-vsctl: %s", args);
3594 ovs = ovsrec_open_vswitch_first(idl);
3596 /* XXX add verification that table is empty */
3597 ovs = ovsrec_open_vswitch_insert(txn);
3600 if (wait_for_reload) {
3601 struct json *where = where_uuid_equals(&ovs->header_.uuid);
3602 ovsdb_idl_txn_increment(txn, "Open_vSwitch", "next_cfg", where);
3603 json_destroy(where);
3606 symtab = ovsdb_symbol_table_create();
3607 for (c = commands; c < &commands[n_commands]; c++) {
3608 ds_init(&c->output);
3611 for (c = commands; c < &commands[n_commands]; c++) {
3612 struct vsctl_context ctx;
3614 vsctl_context_init(&ctx, c, idl, txn, ovs, symtab);
3615 if (c->syntax->run) {
3616 (c->syntax->run)(&ctx);
3618 vsctl_context_done(&ctx, c);
3620 if (ctx.try_again) {
3625 SHASH_FOR_EACH (node, &symtab->sh) {
3626 struct ovsdb_symbol *symbol = node->data;
3627 if (!symbol->created) {
3628 vsctl_fatal("row id \"%s\" is referenced but never created (e.g. "
3629 "with \"-- --id=%s create ...\")",
3630 node->name, node->name);
3632 if (!symbol->strong_ref) {
3633 if (!symbol->weak_ref) {
3634 VLOG_WARN("row id \"%s\" was created but no reference to it "
3635 "was inserted, so it will not actually appear in "
3636 "the database", node->name);
3638 VLOG_WARN("row id \"%s\" was created but only a weak "
3639 "reference to it was inserted, so it will not "
3640 "actually appear in the database", node->name);
3645 status = ovsdb_idl_txn_commit_block(txn);
3646 if (wait_for_reload && status == TXN_SUCCESS) {
3647 next_cfg = ovsdb_idl_txn_get_increment_new_value(txn);
3649 if (status == TXN_UNCHANGED || status == TXN_SUCCESS) {
3650 for (c = commands; c < &commands[n_commands]; c++) {
3651 if (c->syntax->postprocess) {
3652 struct vsctl_context ctx;
3654 vsctl_context_init(&ctx, c, idl, txn, ovs, symtab);
3655 (c->syntax->postprocess)(&ctx);
3656 vsctl_context_done(&ctx, c);
3660 error = xstrdup(ovsdb_idl_txn_get_error(txn));
3661 ovsdb_idl_txn_destroy(txn);
3662 txn = the_idl_txn = NULL;
3665 case TXN_UNCOMMITTED:
3666 case TXN_INCOMPLETE:
3670 /* Should not happen--we never call ovsdb_idl_txn_abort(). */
3671 vsctl_fatal("transaction aborted");
3681 vsctl_fatal("transaction error: %s", error);
3683 case TXN_NOT_LOCKED:
3684 /* Should not happen--we never call ovsdb_idl_set_lock(). */
3685 vsctl_fatal("database not locked");
3692 ovsdb_symbol_table_destroy(symtab);
3694 for (c = commands; c < &commands[n_commands]; c++) {
3695 struct ds *ds = &c->output;
3698 table_print(c->table, &table_style);
3699 } else if (oneline) {
3703 for (j = 0; j < ds->length; j++) {
3704 int ch = ds->string[j];
3707 fputs("\\n", stdout);
3711 fputs("\\\\", stdout);
3720 fputs(ds_cstr(ds), stdout);
3722 ds_destroy(&c->output);
3723 table_destroy(c->table);
3726 smap_destroy(&c->options);
3730 if (wait_for_reload && status != TXN_UNCHANGED) {
3733 OVSREC_OPEN_VSWITCH_FOR_EACH (ovs, idl) {
3734 if (ovs->cur_cfg >= next_cfg) {
3738 ovsdb_idl_wait(idl);
3743 ovsdb_idl_destroy(idl);
3748 /* Our transaction needs to be rerun, or a prerequisite was not met. Free
3749 * resources and return so that the caller can try again. */
3751 ovsdb_idl_txn_abort(txn);
3752 ovsdb_idl_txn_destroy(txn);
3754 ovsdb_symbol_table_destroy(symtab);
3755 for (c = commands; c < &commands[n_commands]; c++) {
3756 ds_destroy(&c->output);
3757 table_destroy(c->table);
3763 static const struct vsctl_command_syntax all_commands[] = {
3764 /* Open vSwitch commands. */
3765 {"init", 0, 0, NULL, cmd_init, NULL, "", RW},
3766 {"show", 0, 0, pre_cmd_show, cmd_show, NULL, "", RO},
3768 /* Bridge commands. */
3769 {"add-br", 1, 3, pre_get_info, cmd_add_br, NULL, "--may-exist", RW},
3770 {"del-br", 1, 1, pre_get_info, cmd_del_br, NULL, "--if-exists", RW},
3771 {"list-br", 0, 0, pre_get_info, cmd_list_br, NULL, "", RO},
3772 {"br-exists", 1, 1, pre_get_info, cmd_br_exists, NULL, "", RO},
3773 {"br-to-vlan", 1, 1, pre_get_info, cmd_br_to_vlan, NULL, "", RO},
3774 {"br-to-parent", 1, 1, pre_get_info, cmd_br_to_parent, NULL, "", RO},
3775 {"br-set-external-id", 2, 3, pre_cmd_br_set_external_id,
3776 cmd_br_set_external_id, NULL, "", RW},
3777 {"br-get-external-id", 1, 2, pre_cmd_br_get_external_id,
3778 cmd_br_get_external_id, NULL, "", RO},
3780 /* Port commands. */
3781 {"list-ports", 1, 1, pre_get_info, cmd_list_ports, NULL, "", RO},
3782 {"add-port", 2, INT_MAX, pre_get_info, cmd_add_port, NULL, "--may-exist",
3784 {"add-bond", 4, INT_MAX, pre_get_info, cmd_add_bond, NULL,
3785 "--may-exist,--fake-iface", RW},
3786 {"del-port", 1, 2, pre_get_info, cmd_del_port, NULL,
3787 "--if-exists,--with-iface", RW},
3788 {"port-to-br", 1, 1, pre_get_info, cmd_port_to_br, NULL, "", RO},
3790 /* Interface commands. */
3791 {"list-ifaces", 1, 1, pre_get_info, cmd_list_ifaces, NULL, "", RO},
3792 {"iface-to-br", 1, 1, pre_get_info, cmd_iface_to_br, NULL, "", RO},
3794 /* Controller commands. */
3795 {"get-controller", 1, 1, pre_controller, cmd_get_controller, NULL, "", RO},
3796 {"del-controller", 1, 1, pre_controller, cmd_del_controller, NULL, "", RW},
3797 {"set-controller", 1, INT_MAX, pre_controller, cmd_set_controller, NULL,
3799 {"get-fail-mode", 1, 1, pre_get_info, cmd_get_fail_mode, NULL, "", RO},
3800 {"del-fail-mode", 1, 1, pre_get_info, cmd_del_fail_mode, NULL, "", RW},
3801 {"set-fail-mode", 2, 2, pre_get_info, cmd_set_fail_mode, NULL, "", RW},
3803 /* Manager commands. */
3804 {"get-manager", 0, 0, pre_manager, cmd_get_manager, NULL, "", RO},
3805 {"del-manager", 0, INT_MAX, pre_manager, cmd_del_manager, NULL, "", RW},
3806 {"set-manager", 1, INT_MAX, pre_manager, cmd_set_manager, NULL, "", RW},
3809 {"get-ssl", 0, 0, pre_cmd_get_ssl, cmd_get_ssl, NULL, "", RO},
3810 {"del-ssl", 0, 0, pre_cmd_del_ssl, cmd_del_ssl, NULL, "", RW},
3811 {"set-ssl", 3, 3, pre_cmd_set_ssl, cmd_set_ssl, NULL, "--bootstrap", RW},
3813 /* Switch commands. */
3814 {"emer-reset", 0, 0, pre_cmd_emer_reset, cmd_emer_reset, NULL, "", RW},
3816 /* Database commands. */
3817 {"comment", 0, INT_MAX, NULL, NULL, NULL, "", RO},
3818 {"get", 2, INT_MAX, pre_cmd_get, cmd_get, NULL, "--if-exists,--id=", RO},
3819 {"list", 1, INT_MAX, pre_cmd_list, cmd_list, NULL, "--columns=", RO},
3820 {"find", 1, INT_MAX, pre_cmd_find, cmd_find, NULL, "--columns=", RO},
3821 {"set", 3, INT_MAX, pre_cmd_set, cmd_set, NULL, "", RW},
3822 {"add", 4, INT_MAX, pre_cmd_add, cmd_add, NULL, "", RW},
3823 {"remove", 4, INT_MAX, pre_cmd_remove, cmd_remove, NULL, "", RW},
3824 {"clear", 3, INT_MAX, pre_cmd_clear, cmd_clear, NULL, "", RW},
3825 {"create", 2, INT_MAX, pre_create, cmd_create, post_create, "--id=", RW},
3826 {"destroy", 1, INT_MAX, pre_cmd_destroy, cmd_destroy, NULL, "--if-exists",
3828 {"wait-until", 2, INT_MAX, pre_cmd_wait_until, cmd_wait_until, NULL, "",
3831 {NULL, 0, 0, NULL, NULL, NULL, NULL, RO},