2 * Copyright (c) 2009, 2010, 2011, 2012 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 "lib/vswitch-idl.h"
51 VLOG_DEFINE_THIS_MODULE(vsctl);
53 /* vsctl_fatal() also logs the error, so it is preferred in this file. */
54 #define ovs_fatal please_use_vsctl_fatal_instead_of_ovs_fatal
58 /* A command supported by ovs-vsctl. */
59 struct vsctl_command_syntax {
60 const char *name; /* e.g. "add-br" */
61 int min_args; /* Min number of arguments following name. */
62 int max_args; /* Max number of arguments following name. */
64 /* If nonnull, calls ovsdb_idl_add_column() or ovsdb_idl_add_table() for
65 * each column or table in ctx->idl that it uses. */
66 void (*prerequisites)(struct vsctl_context *ctx);
68 /* Does the actual work of the command and puts the command's output, if
69 * any, in ctx->output or ctx->table.
71 * Alternatively, if some prerequisite of the command is not met and the
72 * caller should wait for something to change and then retry, it may set
73 * ctx->try_again to true. (Only the "wait-until" command currently does
75 void (*run)(struct vsctl_context *ctx);
77 /* If nonnull, called after the transaction has been successfully
78 * committed. ctx->output is the output from the "run" function, which
79 * this function may modify and otherwise postprocess as needed. (Only the
80 * "create" command currently does any postprocessing.) */
81 void (*postprocess)(struct vsctl_context *ctx);
83 /* A comma-separated list of supported options, e.g. "--a,--b", or the
84 * empty string if the command does not support any options. */
86 enum { RO, RW } mode; /* Does this command modify the database? */
89 struct vsctl_command {
90 /* Data that remains constant after initialization. */
91 const struct vsctl_command_syntax *syntax;
96 /* Data modified by commands. */
101 /* --db: The database server to contact. */
102 static const char *db;
104 /* --oneline: Write each command's output as a single line? */
107 /* --dry-run: Do not commit any changes. */
110 /* --no-wait: Wait for ovs-vswitchd to reload its configuration? */
111 static bool wait_for_reload = true;
113 /* --timeout: Time to wait for a connection to 'db'. */
116 /* Format for table output. */
117 static struct table_style table_style = TABLE_STYLE_DEFAULT;
119 /* All supported commands. */
120 static const struct vsctl_command_syntax all_commands[];
122 /* The IDL we're using and the current transaction, if any.
123 * This is for use by vsctl_exit() only, to allow it to clean up.
124 * Other code should use its context arguments. */
125 static struct ovsdb_idl *the_idl;
126 static struct ovsdb_idl_txn *the_idl_txn;
128 static void vsctl_exit(int status) NO_RETURN;
129 static void vsctl_fatal(const char *, ...) PRINTF_FORMAT(1, 2) NO_RETURN;
130 static char *default_db(void);
131 static void usage(void) NO_RETURN;
132 static void parse_options(int argc, char *argv[]);
133 static bool might_write_to_db(char **argv);
135 static struct vsctl_command *parse_commands(int argc, char *argv[],
136 size_t *n_commandsp);
137 static void parse_command(int argc, char *argv[], struct vsctl_command *);
138 static const struct vsctl_command_syntax *find_command(const char *name);
139 static void run_prerequisites(struct vsctl_command[], size_t n_commands,
141 static enum ovsdb_idl_txn_status do_vsctl(const char *args,
142 struct vsctl_command *, size_t n,
145 static const struct vsctl_table_class *get_table(const char *table_name);
146 static void set_column(const struct vsctl_table_class *,
147 const struct ovsdb_idl_row *, const char *arg,
148 struct ovsdb_symbol_table *);
150 static bool is_condition_satisfied(const struct vsctl_table_class *,
151 const struct ovsdb_idl_row *,
153 struct ovsdb_symbol_table *);
156 main(int argc, char *argv[])
158 extern struct vlog_module VLM_reconnect;
159 enum ovsdb_idl_txn_status status;
160 struct ovsdb_idl *idl;
161 struct vsctl_command *commands;
165 set_program_name(argv[0]);
166 signal(SIGPIPE, SIG_IGN);
167 vlog_set_levels(NULL, VLF_CONSOLE, VLL_WARN);
168 vlog_set_levels(&VLM_reconnect, VLF_ANY_FACILITY, VLL_WARN);
171 /* Log our arguments. This is often valuable for debugging systems. */
172 args = process_escape_args(argv);
173 VLOG(might_write_to_db(argv) ? VLL_INFO : VLL_DBG, "Called as %s", args);
175 /* Parse command line. */
176 parse_options(argc, argv);
177 commands = parse_commands(argc - optind, argv + optind, &n_commands);
183 /* Initialize IDL. */
184 idl = the_idl = ovsdb_idl_create(db, &ovsrec_idl_class, false);
185 run_prerequisites(commands, n_commands, idl);
187 /* Now execute the commands. */
188 status = TXN_AGAIN_WAIT;
190 if (ovsdb_idl_run(idl) || status == TXN_AGAIN_NOW) {
191 status = do_vsctl(args, commands, n_commands, idl);
194 if (status != TXN_AGAIN_NOW) {
202 parse_options(int argc, char *argv[])
205 OPT_DB = UCHAR_MAX + 1,
214 static struct option long_options[] = {
215 {"db", required_argument, NULL, OPT_DB},
216 {"no-syslog", no_argument, NULL, OPT_NO_SYSLOG},
217 {"no-wait", no_argument, NULL, OPT_NO_WAIT},
218 {"dry-run", no_argument, NULL, OPT_DRY_RUN},
219 {"oneline", no_argument, NULL, OPT_ONELINE},
220 {"timeout", required_argument, NULL, 't'},
221 {"help", no_argument, NULL, 'h'},
222 {"version", no_argument, NULL, 'V'},
225 STREAM_SSL_LONG_OPTIONS,
226 {"peer-ca-cert", required_argument, NULL, OPT_PEER_CA_CERT},
229 char *tmp, *short_options;
231 tmp = long_options_to_short_options(long_options);
232 short_options = xasprintf("+%s", tmp);
235 table_style.format = TF_LIST;
240 c = getopt_long(argc, argv, short_options, long_options, NULL);
255 vlog_set_levels(&VLM_vsctl, VLF_SYSLOG, VLL_WARN);
259 wait_for_reload = false;
270 ovs_print_version(0, 0);
274 timeout = strtoul(optarg, NULL, 10);
276 vsctl_fatal("value %s on -t or --timeout is invalid",
282 TABLE_OPTION_HANDLERS(&table_style)
284 STREAM_SSL_OPTION_HANDLERS
286 case OPT_PEER_CA_CERT:
287 stream_ssl_set_peer_ca_cert_file(optarg);
304 static struct vsctl_command *
305 parse_commands(int argc, char *argv[], size_t *n_commandsp)
307 struct vsctl_command *commands;
308 size_t n_commands, allocated_commands;
312 n_commands = allocated_commands = 0;
314 for (start = i = 0; i <= argc; i++) {
315 if (i == argc || !strcmp(argv[i], "--")) {
317 if (n_commands >= allocated_commands) {
318 struct vsctl_command *c;
320 commands = x2nrealloc(commands, &allocated_commands,
322 for (c = commands; c < &commands[n_commands]; c++) {
323 shash_moved(&c->options);
326 parse_command(i - start, &argv[start],
327 &commands[n_commands++]);
333 vsctl_fatal("missing command name (use --help for help)");
335 *n_commandsp = n_commands;
340 parse_command(int argc, char *argv[], struct vsctl_command *command)
342 const struct vsctl_command_syntax *p;
343 struct shash_node *node;
347 shash_init(&command->options);
348 for (i = 0; i < argc; i++) {
349 const char *option = argv[i];
353 if (option[0] != '-') {
357 equals = strchr(option, '=');
359 key = xmemdup0(option, equals - option);
360 value = xstrdup(equals + 1);
362 key = xstrdup(option);
366 if (shash_find(&command->options, key)) {
367 vsctl_fatal("'%s' option specified multiple times", argv[i]);
369 shash_add_nocopy(&command->options, key, value);
372 vsctl_fatal("missing command name");
375 p = find_command(argv[i]);
377 vsctl_fatal("unknown command '%s'; use --help for help", argv[i]);
380 SHASH_FOR_EACH (node, &command->options) {
381 const char *s = strstr(p->options, node->name);
382 int end = s ? s[strlen(node->name)] : EOF;
384 if (end != '=' && end != ',' && end != ' ' && end != '\0') {
385 vsctl_fatal("'%s' command has no '%s' option",
386 argv[i], node->name);
388 if ((end == '=') != (node->data != NULL)) {
390 vsctl_fatal("missing argument to '%s' option on '%s' "
391 "command", node->name, argv[i]);
393 vsctl_fatal("'%s' option on '%s' does not accept an "
394 "argument", node->name, argv[i]);
399 n_arg = argc - i - 1;
400 if (n_arg < p->min_args) {
401 vsctl_fatal("'%s' command requires at least %d arguments",
402 p->name, p->min_args);
403 } else if (n_arg > p->max_args) {
406 for (j = i + 1; j < argc; j++) {
407 if (argv[j][0] == '-') {
408 vsctl_fatal("'%s' command takes at most %d arguments "
409 "(note that options must precede command "
410 "names and follow a \"--\" argument)",
411 p->name, p->max_args);
415 vsctl_fatal("'%s' command takes at most %d arguments",
416 p->name, p->max_args);
420 command->argc = n_arg + 1;
421 command->argv = &argv[i];
424 /* Returns the "struct vsctl_command_syntax" for a given command 'name', or a
425 * null pointer if there is none. */
426 static const struct vsctl_command_syntax *
427 find_command(const char *name)
429 static struct shash commands = SHASH_INITIALIZER(&commands);
431 if (shash_is_empty(&commands)) {
432 const struct vsctl_command_syntax *p;
434 for (p = all_commands; p->name; p++) {
435 shash_add_assert(&commands, p->name, p);
439 return shash_find_data(&commands, name);
443 vsctl_fatal(const char *format, ...)
448 va_start(args, format);
449 message = xvasprintf(format, args);
452 vlog_set_levels(&VLM_vsctl, VLF_CONSOLE, VLL_OFF);
453 VLOG_ERR("%s", message);
454 ovs_error(0, "%s", message);
455 vsctl_exit(EXIT_FAILURE);
458 /* Frees the current transaction and the underlying IDL and then calls
461 * Freeing the transaction and the IDL is not strictly necessary, but it makes
462 * for a clean memory leak report from valgrind in the normal case. That makes
463 * it easier to notice real memory leaks. */
465 vsctl_exit(int status)
468 ovsdb_idl_txn_abort(the_idl_txn);
469 ovsdb_idl_txn_destroy(the_idl_txn);
471 ovsdb_idl_destroy(the_idl);
479 %s: ovs-vswitchd management utility\n\
480 usage: %s [OPTIONS] COMMAND [ARG...]\n\
482 Open vSwitch commands:\n\
483 init initialize database, if not yet initialized\n\
484 show print overview of database contents\n\
485 emer-reset reset configuration to clean state\n\
488 add-br BRIDGE create a new bridge named BRIDGE\n\
489 add-br BRIDGE PARENT VLAN create new fake BRIDGE in PARENT on VLAN\n\
490 del-br BRIDGE delete BRIDGE and all of its ports\n\
491 list-br print the names of all the bridges\n\
492 br-exists BRIDGE exit 2 if BRIDGE does not exist\n\
493 br-to-vlan BRIDGE print the VLAN which BRIDGE is on\n\
494 br-to-parent BRIDGE print the parent of BRIDGE\n\
495 br-set-external-id BRIDGE KEY VALUE set KEY on BRIDGE to VALUE\n\
496 br-set-external-id BRIDGE KEY unset KEY on BRIDGE\n\
497 br-get-external-id BRIDGE KEY print value of KEY on BRIDGE\n\
498 br-get-external-id BRIDGE list key-value pairs on BRIDGE\n\
500 Port commands (a bond is considered to be a single port):\n\
501 list-ports BRIDGE print the names of all the ports on BRIDGE\n\
502 add-port BRIDGE PORT add network device PORT to BRIDGE\n\
503 add-bond BRIDGE PORT IFACE... add bonded port PORT in BRIDGE from IFACES\n\
504 del-port [BRIDGE] PORT delete PORT (which may be bonded) from BRIDGE\n\
505 port-to-br PORT print name of bridge that contains PORT\n\
507 Interface commands (a bond consists of multiple interfaces):\n\
508 list-ifaces BRIDGE print the names of all interfaces on BRIDGE\n\
509 iface-to-br IFACE print name of bridge that contains IFACE\n\
511 Controller commands:\n\
512 get-controller BRIDGE print the controllers for BRIDGE\n\
513 del-controller BRIDGE delete the controllers for BRIDGE\n\
514 set-controller BRIDGE TARGET... set the controllers for BRIDGE\n\
515 get-fail-mode BRIDGE print the fail-mode for BRIDGE\n\
516 del-fail-mode BRIDGE delete the fail-mode for BRIDGE\n\
517 set-fail-mode BRIDGE MODE set the fail-mode for BRIDGE to MODE\n\
520 get-manager print the managers\n\
521 del-manager delete the managers\n\
522 set-manager TARGET... set the list of managers to TARGET...\n\
525 get-ssl print the SSL configuration\n\
526 del-ssl delete the SSL configuration\n\
527 set-ssl PRIV-KEY CERT CA-CERT set the SSL configuration\n\
530 emer-reset reset switch to known good state\n\
532 Database commands:\n\
533 list TBL [REC] list RECord (or all records) in TBL\n\
534 find TBL CONDITION... list records satisfying CONDITION in TBL\n\
535 get TBL REC COL[:KEY] print values of COLumns in RECord in TBL\n\
536 set TBL REC COL[:KEY]=VALUE set COLumn values in RECord in TBL\n\
537 add TBL REC COL [KEY=]VALUE add (KEY=)VALUE to COLumn in RECord in TBL\n\
538 remove TBL REC COL [KEY=]VALUE remove (KEY=)VALUE from COLumn\n\
539 clear TBL REC COL clear values from COLumn in RECord in TBL\n\
540 create TBL COL[:KEY]=VALUE create and initialize new record\n\
541 destroy TBL REC delete RECord from TBL\n\
542 wait-until TBL REC [COL[:KEY]=VALUE] wait until condition is true\n\
543 Potentially unsafe database commands require --force option.\n\
546 --db=DATABASE connect to DATABASE\n\
548 --no-wait do not wait for ovs-vswitchd to reconfigure\n\
549 -t, --timeout=SECS wait at most SECS seconds for ovs-vswitchd\n\
550 --dry-run do not commit changes to database\n\
551 --oneline print exactly one line of output per command\n",
552 program_name, program_name, default_db());
555 --no-syslog equivalent to --verbose=vsctl:syslog:warn\n");
556 stream_usage("database", true, true, false);
559 -h, --help display this help message\n\
560 -V, --version display version information\n");
569 def = xasprintf("unix:%s/db.sock", ovs_rundir());
574 /* Returns true if it looks like this set of arguments might modify the
575 * database, otherwise false. (Not very smart, so it's prone to false
578 might_write_to_db(char **argv)
580 for (; *argv; argv++) {
581 const struct vsctl_command_syntax *p = find_command(*argv);
582 if (p && p->mode == RW) {
589 struct vsctl_context {
593 struct shash options;
595 /* Modifiable state. */
598 struct ovsdb_idl *idl;
599 struct ovsdb_idl_txn *txn;
600 struct ovsdb_symbol_table *symtab;
601 const struct ovsrec_open_vswitch *ovs;
604 /* A command may set this member to true if some prerequisite is not met
605 * and the caller should wait for something to change and then retry. */
609 struct vsctl_bridge {
610 struct ovsrec_bridge *br_cfg;
612 struct ovsrec_controller **ctrl;
616 /* VLAN ("fake") bridge support.
618 * Use 'parent != NULL' to detect a fake bridge, because 'vlan' can be 0
620 struct vsctl_bridge *parent; /* Real bridge, or NULL. */
621 int vlan; /* VLAN VID (0...4095), or 0. */
625 struct ovsrec_port *port_cfg;
626 struct vsctl_bridge *bridge;
630 struct ovsrec_interface *iface_cfg;
631 struct vsctl_port *port;
635 struct vsctl_context *ctx;
636 struct shash bridges; /* Maps from bridge name to struct vsctl_bridge. */
637 struct shash ports; /* Maps from port name to struct vsctl_port. */
638 struct shash ifaces; /* Maps from port name to struct vsctl_iface. */
642 vsctl_context_to_string(const struct vsctl_context *ctx)
644 const struct shash_node *node;
650 SHASH_FOR_EACH (node, &ctx->options) {
651 svec_add(&words, node->name);
653 for (i = 0; i < ctx->argc; i++) {
654 svec_add(&words, ctx->argv[i]);
656 svec_terminate(&words);
658 s = process_escape_args(words.names);
660 svec_destroy(&words);
666 verify_ports(struct vsctl_context *ctx)
668 if (!ctx->verified_ports) {
669 const struct ovsrec_bridge *bridge;
670 const struct ovsrec_port *port;
672 ovsrec_open_vswitch_verify_bridges(ctx->ovs);
673 OVSREC_BRIDGE_FOR_EACH (bridge, ctx->idl) {
674 ovsrec_bridge_verify_ports(bridge);
676 OVSREC_PORT_FOR_EACH (port, ctx->idl) {
677 ovsrec_port_verify_interfaces(port);
680 ctx->verified_ports = true;
684 static struct vsctl_bridge *
685 add_bridge(struct vsctl_info *b,
686 struct ovsrec_bridge *br_cfg, const char *name,
687 struct vsctl_bridge *parent, int vlan)
689 struct vsctl_bridge *br = xmalloc(sizeof *br);
691 br->name = xstrdup(name);
695 br->ctrl = parent->br_cfg->controller;
696 br->n_ctrl = parent->br_cfg->n_controller;
697 br->fail_mode = parent->br_cfg->fail_mode;
699 br->ctrl = br_cfg->controller;
700 br->n_ctrl = br_cfg->n_controller;
701 br->fail_mode = br_cfg->fail_mode;
703 shash_add(&b->bridges, br->name, br);
708 port_is_fake_bridge(const struct ovsrec_port *port_cfg)
710 return (port_cfg->fake_bridge
712 && *port_cfg->tag >= 0 && *port_cfg->tag <= 4095);
715 static struct vsctl_bridge *
716 find_vlan_bridge(struct vsctl_info *info,
717 struct vsctl_bridge *parent, int vlan)
719 struct shash_node *node;
721 SHASH_FOR_EACH (node, &info->bridges) {
722 struct vsctl_bridge *br = node->data;
723 if (br->parent == parent && br->vlan == vlan) {
732 free_info(struct vsctl_info *info)
734 struct shash_node *node;
736 SHASH_FOR_EACH (node, &info->bridges) {
737 struct vsctl_bridge *bridge = node->data;
741 shash_destroy(&info->bridges);
743 shash_destroy_free_data(&info->ports);
744 shash_destroy_free_data(&info->ifaces);
748 pre_get_info(struct vsctl_context *ctx)
750 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_bridges);
752 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_name);
753 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_controller);
754 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_fail_mode);
755 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_ports);
757 ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_name);
758 ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_fake_bridge);
759 ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_tag);
760 ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_interfaces);
762 ovsdb_idl_add_column(ctx->idl, &ovsrec_interface_col_name);
766 get_info(struct vsctl_context *ctx, struct vsctl_info *info)
768 const struct ovsrec_open_vswitch *ovs = ctx->ovs;
769 struct sset bridges, ports;
773 shash_init(&info->bridges);
774 shash_init(&info->ports);
775 shash_init(&info->ifaces);
779 for (i = 0; i < ovs->n_bridges; i++) {
780 struct ovsrec_bridge *br_cfg = ovs->bridges[i];
781 struct vsctl_bridge *br;
784 if (!sset_add(&bridges, br_cfg->name)) {
785 VLOG_WARN("%s: database contains duplicate bridge name",
789 br = add_bridge(info, br_cfg, br_cfg->name, NULL, 0);
794 for (j = 0; j < br_cfg->n_ports; j++) {
795 struct ovsrec_port *port_cfg = br_cfg->ports[j];
797 if (!sset_add(&ports, port_cfg->name)) {
798 /* Duplicate port name. (We will warn about that later.) */
802 if (port_is_fake_bridge(port_cfg)
803 && sset_add(&bridges, port_cfg->name)) {
804 add_bridge(info, NULL, port_cfg->name, br, *port_cfg->tag);
808 sset_destroy(&bridges);
809 sset_destroy(&ports);
812 for (i = 0; i < ovs->n_bridges; i++) {
813 struct ovsrec_bridge *br_cfg = ovs->bridges[i];
814 struct vsctl_bridge *br;
817 if (!sset_add(&bridges, br_cfg->name)) {
820 br = shash_find_data(&info->bridges, br_cfg->name);
821 for (j = 0; j < br_cfg->n_ports; j++) {
822 struct ovsrec_port *port_cfg = br_cfg->ports[j];
823 struct vsctl_port *port;
826 port = shash_find_data(&info->ports, port_cfg->name);
828 if (port_cfg == port->port_cfg) {
829 VLOG_WARN("%s: port is in multiple bridges (%s and %s)",
830 port_cfg->name, br->name, port->bridge->name);
832 /* Log as an error because this violates the database's
833 * uniqueness constraints, so the database server shouldn't
834 * have allowed it. */
835 VLOG_ERR("%s: database contains duplicate port name",
841 if (port_is_fake_bridge(port_cfg)
842 && !sset_add(&bridges, port_cfg->name)) {
846 port = xmalloc(sizeof *port);
847 port->port_cfg = port_cfg;
849 && *port_cfg->tag >= 0 && *port_cfg->tag <= 4095) {
850 port->bridge = find_vlan_bridge(info, br, *port_cfg->tag);
857 shash_add(&info->ports, port_cfg->name, port);
859 for (k = 0; k < port_cfg->n_interfaces; k++) {
860 struct ovsrec_interface *iface_cfg = port_cfg->interfaces[k];
861 struct vsctl_iface *iface;
863 iface = shash_find_data(&info->ifaces, iface_cfg->name);
865 if (iface_cfg == iface->iface_cfg) {
866 VLOG_WARN("%s: interface is in multiple ports "
869 iface->port->port_cfg->name,
870 port->port_cfg->name);
872 /* Log as an error because this violates the database's
873 * uniqueness constraints, so the database server
874 * shouldn't have allowed it. */
875 VLOG_ERR("%s: database contains duplicate interface "
876 "name", iface_cfg->name);
881 iface = xmalloc(sizeof *iface);
882 iface->iface_cfg = iface_cfg;
884 shash_add(&info->ifaces, iface_cfg->name, iface);
888 sset_destroy(&bridges);
892 check_conflicts(struct vsctl_info *info, const char *name,
895 struct vsctl_iface *iface;
896 struct vsctl_port *port;
898 verify_ports(info->ctx);
900 if (shash_find(&info->bridges, name)) {
901 vsctl_fatal("%s because a bridge named %s already exists",
905 port = shash_find_data(&info->ports, name);
907 vsctl_fatal("%s because a port named %s already exists on "
908 "bridge %s", msg, name, port->bridge->name);
911 iface = shash_find_data(&info->ifaces, name);
913 vsctl_fatal("%s because an interface named %s already exists "
914 "on bridge %s", msg, name, iface->port->bridge->name);
920 static struct vsctl_bridge *
921 find_bridge(struct vsctl_info *info, const char *name, bool must_exist)
923 struct vsctl_bridge *br = shash_find_data(&info->bridges, name);
924 if (must_exist && !br) {
925 vsctl_fatal("no bridge named %s", name);
927 ovsrec_open_vswitch_verify_bridges(info->ctx->ovs);
931 static struct vsctl_bridge *
932 find_real_bridge(struct vsctl_info *info, const char *name, bool must_exist)
934 struct vsctl_bridge *br = find_bridge(info, name, must_exist);
935 if (br && br->parent) {
936 vsctl_fatal("%s is a fake bridge", name);
941 static struct vsctl_port *
942 find_port(struct vsctl_info *info, const char *name, bool must_exist)
944 struct vsctl_port *port = shash_find_data(&info->ports, name);
945 if (port && !strcmp(name, port->bridge->name)) {
948 if (must_exist && !port) {
949 vsctl_fatal("no port named %s", name);
951 verify_ports(info->ctx);
955 static struct vsctl_iface *
956 find_iface(struct vsctl_info *info, const char *name, bool must_exist)
958 struct vsctl_iface *iface = shash_find_data(&info->ifaces, name);
959 if (iface && !strcmp(name, iface->port->bridge->name)) {
962 if (must_exist && !iface) {
963 vsctl_fatal("no interface named %s", name);
965 verify_ports(info->ctx);
970 bridge_insert_port(struct ovsrec_bridge *br, struct ovsrec_port *port)
972 struct ovsrec_port **ports;
975 ports = xmalloc(sizeof *br->ports * (br->n_ports + 1));
976 for (i = 0; i < br->n_ports; i++) {
977 ports[i] = br->ports[i];
979 ports[br->n_ports] = port;
980 ovsrec_bridge_set_ports(br, ports, br->n_ports + 1);
985 bridge_delete_port(struct ovsrec_bridge *br, struct ovsrec_port *port)
987 struct ovsrec_port **ports;
990 ports = xmalloc(sizeof *br->ports * br->n_ports);
991 for (i = n = 0; i < br->n_ports; i++) {
992 if (br->ports[i] != port) {
993 ports[n++] = br->ports[i];
996 ovsrec_bridge_set_ports(br, ports, n);
1001 ovs_insert_bridge(const struct ovsrec_open_vswitch *ovs,
1002 struct ovsrec_bridge *bridge)
1004 struct ovsrec_bridge **bridges;
1007 bridges = xmalloc(sizeof *ovs->bridges * (ovs->n_bridges + 1));
1008 for (i = 0; i < ovs->n_bridges; i++) {
1009 bridges[i] = ovs->bridges[i];
1011 bridges[ovs->n_bridges] = bridge;
1012 ovsrec_open_vswitch_set_bridges(ovs, bridges, ovs->n_bridges + 1);
1017 ovs_delete_bridge(const struct ovsrec_open_vswitch *ovs,
1018 struct ovsrec_bridge *bridge)
1020 struct ovsrec_bridge **bridges;
1023 bridges = xmalloc(sizeof *ovs->bridges * ovs->n_bridges);
1024 for (i = n = 0; i < ovs->n_bridges; i++) {
1025 if (ovs->bridges[i] != bridge) {
1026 bridges[n++] = ovs->bridges[i];
1029 ovsrec_open_vswitch_set_bridges(ovs, bridges, n);
1034 cmd_init(struct vsctl_context *ctx OVS_UNUSED)
1038 struct cmd_show_table {
1039 const struct ovsdb_idl_table_class *table;
1040 const struct ovsdb_idl_column *name_column;
1041 const struct ovsdb_idl_column *columns[3];
1045 static struct cmd_show_table cmd_show_tables[] = {
1046 {&ovsrec_table_open_vswitch,
1048 {&ovsrec_open_vswitch_col_manager_options,
1049 &ovsrec_open_vswitch_col_bridges,
1050 &ovsrec_open_vswitch_col_ovs_version},
1053 {&ovsrec_table_bridge,
1054 &ovsrec_bridge_col_name,
1055 {&ovsrec_bridge_col_controller,
1056 &ovsrec_bridge_col_fail_mode,
1057 &ovsrec_bridge_col_ports},
1060 {&ovsrec_table_port,
1061 &ovsrec_port_col_name,
1062 {&ovsrec_port_col_tag,
1063 &ovsrec_port_col_trunks,
1064 &ovsrec_port_col_interfaces},
1067 {&ovsrec_table_interface,
1068 &ovsrec_interface_col_name,
1069 {&ovsrec_interface_col_type,
1070 &ovsrec_interface_col_options,
1074 {&ovsrec_table_controller,
1075 &ovsrec_controller_col_target,
1076 {&ovsrec_controller_col_is_connected,
1081 {&ovsrec_table_manager,
1082 &ovsrec_manager_col_target,
1083 {&ovsrec_manager_col_is_connected,
1090 pre_cmd_show(struct vsctl_context *ctx)
1092 struct cmd_show_table *show;
1094 for (show = cmd_show_tables;
1095 show < &cmd_show_tables[ARRAY_SIZE(cmd_show_tables)];
1099 ovsdb_idl_add_table(ctx->idl, show->table);
1100 if (show->name_column) {
1101 ovsdb_idl_add_column(ctx->idl, show->name_column);
1103 for (i = 0; i < ARRAY_SIZE(show->columns); i++) {
1104 const struct ovsdb_idl_column *column = show->columns[i];
1106 ovsdb_idl_add_column(ctx->idl, column);
1112 static struct cmd_show_table *
1113 cmd_show_find_table_by_row(const struct ovsdb_idl_row *row)
1115 struct cmd_show_table *show;
1117 for (show = cmd_show_tables;
1118 show < &cmd_show_tables[ARRAY_SIZE(cmd_show_tables)];
1120 if (show->table == row->table->class) {
1127 static struct cmd_show_table *
1128 cmd_show_find_table_by_name(const char *name)
1130 struct cmd_show_table *show;
1132 for (show = cmd_show_tables;
1133 show < &cmd_show_tables[ARRAY_SIZE(cmd_show_tables)];
1135 if (!strcmp(show->table->name, name)) {
1143 cmd_show_row(struct vsctl_context *ctx, const struct ovsdb_idl_row *row,
1146 struct cmd_show_table *show = cmd_show_find_table_by_row(row);
1149 ds_put_char_multiple(&ctx->output, ' ', level * 4);
1150 if (show && show->name_column) {
1151 const struct ovsdb_datum *datum;
1153 ds_put_format(&ctx->output, "%s ", show->table->name);
1154 datum = ovsdb_idl_read(row, show->name_column);
1155 ovsdb_datum_to_string(datum, &show->name_column->type, &ctx->output);
1157 ds_put_format(&ctx->output, UUID_FMT, UUID_ARGS(&row->uuid));
1159 ds_put_char(&ctx->output, '\n');
1161 if (!show || show->recurse) {
1165 show->recurse = true;
1166 for (i = 0; i < ARRAY_SIZE(show->columns); i++) {
1167 const struct ovsdb_idl_column *column = show->columns[i];
1168 const struct ovsdb_datum *datum;
1174 datum = ovsdb_idl_read(row, column);
1175 if (column->type.key.type == OVSDB_TYPE_UUID &&
1176 column->type.key.u.uuid.refTableName) {
1177 struct cmd_show_table *ref_show;
1180 ref_show = cmd_show_find_table_by_name(
1181 column->type.key.u.uuid.refTableName);
1183 for (j = 0; j < datum->n; j++) {
1184 const struct ovsdb_idl_row *ref_row;
1186 ref_row = ovsdb_idl_get_row_for_uuid(ctx->idl,
1188 &datum->keys[j].uuid);
1190 cmd_show_row(ctx, ref_row, level + 1);
1197 if (!ovsdb_datum_is_default(datum, &column->type)) {
1198 ds_put_char_multiple(&ctx->output, ' ', (level + 1) * 4);
1199 ds_put_format(&ctx->output, "%s: ", column->name);
1200 ovsdb_datum_to_string(datum, &column->type, &ctx->output);
1201 ds_put_char(&ctx->output, '\n');
1204 show->recurse = false;
1208 cmd_show(struct vsctl_context *ctx)
1210 const struct ovsdb_idl_row *row;
1212 for (row = ovsdb_idl_first_row(ctx->idl, cmd_show_tables[0].table);
1213 row; row = ovsdb_idl_next_row(row)) {
1214 cmd_show_row(ctx, row, 0);
1219 pre_cmd_emer_reset(struct vsctl_context *ctx)
1221 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_manager_options);
1222 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_ssl);
1224 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_controller);
1225 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_fail_mode);
1226 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_mirrors);
1227 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_netflow);
1228 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_sflow);
1229 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_flood_vlans);
1230 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_other_config);
1232 ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_other_config);
1234 ovsdb_idl_add_column(ctx->idl,
1235 &ovsrec_interface_col_ingress_policing_rate);
1236 ovsdb_idl_add_column(ctx->idl,
1237 &ovsrec_interface_col_ingress_policing_burst);
1241 cmd_emer_reset(struct vsctl_context *ctx)
1243 const struct ovsdb_idl *idl = ctx->idl;
1244 const struct ovsrec_bridge *br;
1245 const struct ovsrec_port *port;
1246 const struct ovsrec_interface *iface;
1247 const struct ovsrec_mirror *mirror, *next_mirror;
1248 const struct ovsrec_controller *ctrl, *next_ctrl;
1249 const struct ovsrec_manager *mgr, *next_mgr;
1250 const struct ovsrec_netflow *nf, *next_nf;
1251 const struct ovsrec_ssl *ssl, *next_ssl;
1252 const struct ovsrec_sflow *sflow, *next_sflow;
1254 /* Reset the Open_vSwitch table. */
1255 ovsrec_open_vswitch_set_manager_options(ctx->ovs, NULL, 0);
1256 ovsrec_open_vswitch_set_ssl(ctx->ovs, NULL);
1258 OVSREC_BRIDGE_FOR_EACH (br, idl) {
1260 char *hw_key = "hwaddr";
1261 char *hw_val = NULL;
1263 ovsrec_bridge_set_controller(br, NULL, 0);
1264 ovsrec_bridge_set_fail_mode(br, NULL);
1265 ovsrec_bridge_set_mirrors(br, NULL, 0);
1266 ovsrec_bridge_set_netflow(br, NULL);
1267 ovsrec_bridge_set_sflow(br, NULL);
1268 ovsrec_bridge_set_flood_vlans(br, NULL, 0);
1270 /* We only want to save the "hwaddr" key from other_config. */
1271 for (i=0; i < br->n_other_config; i++) {
1272 if (!strcmp(br->key_other_config[i], hw_key)) {
1273 hw_val = br->value_other_config[i];
1278 char *val = xstrdup(hw_val);
1279 ovsrec_bridge_set_other_config(br, &hw_key, &val, 1);
1282 ovsrec_bridge_set_other_config(br, NULL, NULL, 0);
1286 OVSREC_PORT_FOR_EACH (port, idl) {
1287 ovsrec_port_set_other_config(port, NULL, NULL, 0);
1290 OVSREC_INTERFACE_FOR_EACH (iface, idl) {
1291 /* xxx What do we do about gre/patch devices created by mgr? */
1293 ovsrec_interface_set_ingress_policing_rate(iface, 0);
1294 ovsrec_interface_set_ingress_policing_burst(iface, 0);
1297 OVSREC_MIRROR_FOR_EACH_SAFE (mirror, next_mirror, idl) {
1298 ovsrec_mirror_delete(mirror);
1301 OVSREC_CONTROLLER_FOR_EACH_SAFE (ctrl, next_ctrl, idl) {
1302 ovsrec_controller_delete(ctrl);
1305 OVSREC_MANAGER_FOR_EACH_SAFE (mgr, next_mgr, idl) {
1306 ovsrec_manager_delete(mgr);
1309 OVSREC_NETFLOW_FOR_EACH_SAFE (nf, next_nf, idl) {
1310 ovsrec_netflow_delete(nf);
1313 OVSREC_SSL_FOR_EACH_SAFE (ssl, next_ssl, idl) {
1314 ovsrec_ssl_delete(ssl);
1317 OVSREC_SFLOW_FOR_EACH_SAFE (sflow, next_sflow, idl) {
1318 ovsrec_sflow_delete(sflow);
1323 cmd_add_br(struct vsctl_context *ctx)
1325 bool may_exist = shash_find(&ctx->options, "--may-exist") != NULL;
1326 const char *br_name, *parent_name;
1327 struct vsctl_info info;
1330 br_name = ctx->argv[1];
1331 if (ctx->argc == 2) {
1334 } else if (ctx->argc == 4) {
1335 parent_name = ctx->argv[2];
1336 vlan = atoi(ctx->argv[3]);
1337 if (vlan < 0 || vlan > 4095) {
1338 vsctl_fatal("%s: vlan must be between 0 and 4095", ctx->argv[0]);
1341 vsctl_fatal("'%s' command takes exactly 1 or 3 arguments",
1345 get_info(ctx, &info);
1347 struct vsctl_bridge *br;
1349 br = find_bridge(&info, br_name, false);
1353 vsctl_fatal("\"--may-exist add-br %s\" but %s is "
1354 "a VLAN bridge for VLAN %d",
1355 br_name, br_name, br->vlan);
1359 vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
1360 "is not a VLAN bridge",
1361 br_name, parent_name, vlan, br_name);
1362 } else if (strcmp(br->parent->name, parent_name)) {
1363 vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
1364 "has the wrong parent %s",
1365 br_name, parent_name, vlan,
1366 br_name, br->parent->name);
1367 } else if (br->vlan != vlan) {
1368 vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
1369 "is a VLAN bridge for the wrong VLAN %d",
1370 br_name, parent_name, vlan, br_name, br->vlan);
1377 check_conflicts(&info, br_name,
1378 xasprintf("cannot create a bridge named %s", br_name));
1381 struct ovsrec_port *port;
1382 struct ovsrec_interface *iface;
1383 struct ovsrec_bridge *br;
1385 iface = ovsrec_interface_insert(ctx->txn);
1386 ovsrec_interface_set_name(iface, br_name);
1387 ovsrec_interface_set_type(iface, "internal");
1389 port = ovsrec_port_insert(ctx->txn);
1390 ovsrec_port_set_name(port, br_name);
1391 ovsrec_port_set_interfaces(port, &iface, 1);
1393 br = ovsrec_bridge_insert(ctx->txn);
1394 ovsrec_bridge_set_name(br, br_name);
1395 ovsrec_bridge_set_ports(br, &port, 1);
1397 ovs_insert_bridge(ctx->ovs, br);
1399 struct vsctl_bridge *parent;
1400 struct ovsrec_port *port;
1401 struct ovsrec_interface *iface;
1402 struct ovsrec_bridge *br;
1405 parent = find_bridge(&info, parent_name, false);
1406 if (parent && parent->parent) {
1407 vsctl_fatal("cannot create bridge with fake bridge as parent");
1410 vsctl_fatal("parent bridge %s does not exist", parent_name);
1412 br = parent->br_cfg;
1414 iface = ovsrec_interface_insert(ctx->txn);
1415 ovsrec_interface_set_name(iface, br_name);
1416 ovsrec_interface_set_type(iface, "internal");
1418 port = ovsrec_port_insert(ctx->txn);
1419 ovsrec_port_set_name(port, br_name);
1420 ovsrec_port_set_interfaces(port, &iface, 1);
1421 ovsrec_port_set_fake_bridge(port, true);
1422 ovsrec_port_set_tag(port, &tag, 1);
1424 bridge_insert_port(br, port);
1431 del_port(struct vsctl_info *info, struct vsctl_port *port)
1433 struct shash_node *node;
1435 SHASH_FOR_EACH (node, &info->ifaces) {
1436 struct vsctl_iface *iface = node->data;
1437 if (iface->port == port) {
1438 ovsrec_interface_delete(iface->iface_cfg);
1441 ovsrec_port_delete(port->port_cfg);
1443 bridge_delete_port((port->bridge->parent
1444 ? port->bridge->parent->br_cfg
1445 : port->bridge->br_cfg), port->port_cfg);
1449 cmd_del_br(struct vsctl_context *ctx)
1451 bool must_exist = !shash_find(&ctx->options, "--if-exists");
1452 struct vsctl_bridge *bridge;
1453 struct vsctl_info info;
1455 get_info(ctx, &info);
1456 bridge = find_bridge(&info, ctx->argv[1], must_exist);
1458 struct shash_node *node;
1460 SHASH_FOR_EACH (node, &info.ports) {
1461 struct vsctl_port *port = node->data;
1462 if (port->bridge == bridge || port->bridge->parent == bridge
1463 || !strcmp(port->port_cfg->name, bridge->name)) {
1464 del_port(&info, port);
1467 if (bridge->br_cfg) {
1468 ovsrec_bridge_delete(bridge->br_cfg);
1469 ovs_delete_bridge(ctx->ovs, bridge->br_cfg);
1476 output_sorted(struct svec *svec, struct ds *output)
1482 SVEC_FOR_EACH (i, name, svec) {
1483 ds_put_format(output, "%s\n", name);
1488 cmd_list_br(struct vsctl_context *ctx)
1490 struct shash_node *node;
1491 struct vsctl_info info;
1492 struct svec bridges;
1494 get_info(ctx, &info);
1496 svec_init(&bridges);
1497 SHASH_FOR_EACH (node, &info.bridges) {
1498 struct vsctl_bridge *br = node->data;
1499 svec_add(&bridges, br->name);
1501 output_sorted(&bridges, &ctx->output);
1502 svec_destroy(&bridges);
1508 cmd_br_exists(struct vsctl_context *ctx)
1510 struct vsctl_info info;
1512 get_info(ctx, &info);
1513 if (!find_bridge(&info, ctx->argv[1], false)) {
1519 /* Returns true if 'b_prefix' (of length 'b_prefix_len') concatenated with 'b'
1520 * equals 'a', false otherwise. */
1522 key_matches(const char *a,
1523 const char *b_prefix, size_t b_prefix_len, const char *b)
1525 return !strncmp(a, b_prefix, b_prefix_len) && !strcmp(a + b_prefix_len, b);
1529 set_external_id(char **old_keys, char **old_values, size_t old_n,
1530 char *key, char *value,
1531 char ***new_keysp, char ***new_valuesp, size_t *new_np)
1538 new_keys = xmalloc(sizeof *new_keys * (old_n + 1));
1539 new_values = xmalloc(sizeof *new_values * (old_n + 1));
1541 for (i = 0; i < old_n; i++) {
1542 if (strcmp(key, old_keys[i])) {
1543 new_keys[new_n] = old_keys[i];
1544 new_values[new_n] = old_values[i];
1549 new_keys[new_n] = key;
1550 new_values[new_n] = value;
1553 *new_keysp = new_keys;
1554 *new_valuesp = new_values;
1559 pre_cmd_br_set_external_id(struct vsctl_context *ctx)
1562 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_external_ids);
1563 ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_external_ids);
1567 cmd_br_set_external_id(struct vsctl_context *ctx)
1569 struct vsctl_info info;
1570 struct vsctl_bridge *bridge;
1571 char **keys, **values;
1574 get_info(ctx, &info);
1575 bridge = find_bridge(&info, ctx->argv[1], true);
1576 if (bridge->br_cfg) {
1577 set_external_id(bridge->br_cfg->key_external_ids,
1578 bridge->br_cfg->value_external_ids,
1579 bridge->br_cfg->n_external_ids,
1580 ctx->argv[2], ctx->argc >= 4 ? ctx->argv[3] : NULL,
1581 &keys, &values, &n);
1582 ovsrec_bridge_verify_external_ids(bridge->br_cfg);
1583 ovsrec_bridge_set_external_ids(bridge->br_cfg, keys, values, n);
1585 char *key = xasprintf("fake-bridge-%s", ctx->argv[2]);
1586 struct vsctl_port *port = shash_find_data(&info.ports, ctx->argv[1]);
1587 set_external_id(port->port_cfg->key_external_ids,
1588 port->port_cfg->value_external_ids,
1589 port->port_cfg->n_external_ids,
1590 key, ctx->argc >= 4 ? ctx->argv[3] : NULL,
1591 &keys, &values, &n);
1592 ovsrec_port_verify_external_ids(port->port_cfg);
1593 ovsrec_port_set_external_ids(port->port_cfg, keys, values, n);
1603 get_external_id(char **keys, char **values, size_t n,
1604 const char *prefix, const char *key,
1607 size_t prefix_len = strlen(prefix);
1612 for (i = 0; i < n; i++) {
1613 if (!key && !strncmp(keys[i], prefix, prefix_len)) {
1614 svec_add_nocopy(&svec, xasprintf("%s=%s",
1615 keys[i] + prefix_len, values[i]));
1616 } else if (key && key_matches(keys[i], prefix, prefix_len, key)) {
1617 svec_add(&svec, values[i]);
1621 output_sorted(&svec, output);
1622 svec_destroy(&svec);
1626 pre_cmd_br_get_external_id(struct vsctl_context *ctx)
1628 pre_cmd_br_set_external_id(ctx);
1632 cmd_br_get_external_id(struct vsctl_context *ctx)
1634 struct vsctl_info info;
1635 struct vsctl_bridge *bridge;
1637 get_info(ctx, &info);
1638 bridge = find_bridge(&info, ctx->argv[1], true);
1639 if (bridge->br_cfg) {
1640 ovsrec_bridge_verify_external_ids(bridge->br_cfg);
1641 get_external_id(bridge->br_cfg->key_external_ids,
1642 bridge->br_cfg->value_external_ids,
1643 bridge->br_cfg->n_external_ids,
1644 "", ctx->argc >= 3 ? ctx->argv[2] : NULL,
1647 struct vsctl_port *port = shash_find_data(&info.ports, ctx->argv[1]);
1648 ovsrec_port_verify_external_ids(port->port_cfg);
1649 get_external_id(port->port_cfg->key_external_ids,
1650 port->port_cfg->value_external_ids,
1651 port->port_cfg->n_external_ids,
1652 "fake-bridge-", ctx->argc >= 3 ? ctx->argv[2] : NULL, &ctx->output);
1659 cmd_list_ports(struct vsctl_context *ctx)
1661 struct vsctl_bridge *br;
1662 struct shash_node *node;
1663 struct vsctl_info info;
1666 get_info(ctx, &info);
1667 br = find_bridge(&info, ctx->argv[1], true);
1668 ovsrec_bridge_verify_ports(br->br_cfg ? br->br_cfg : br->parent->br_cfg);
1671 SHASH_FOR_EACH (node, &info.ports) {
1672 struct vsctl_port *port = node->data;
1674 if (strcmp(port->port_cfg->name, br->name) && br == port->bridge) {
1675 svec_add(&ports, port->port_cfg->name);
1678 output_sorted(&ports, &ctx->output);
1679 svec_destroy(&ports);
1685 add_port(struct vsctl_context *ctx,
1686 const char *br_name, const char *port_name,
1687 bool may_exist, bool fake_iface,
1688 char *iface_names[], int n_ifaces,
1689 char *settings[], int n_settings)
1691 struct vsctl_info info;
1692 struct vsctl_bridge *bridge;
1693 struct ovsrec_interface **ifaces;
1694 struct ovsrec_port *port;
1697 get_info(ctx, &info);
1699 struct vsctl_port *vsctl_port;
1701 vsctl_port = find_port(&info, port_name, false);
1703 struct svec want_names, have_names;
1705 svec_init(&want_names);
1706 for (i = 0; i < n_ifaces; i++) {
1707 svec_add(&want_names, iface_names[i]);
1709 svec_sort(&want_names);
1711 svec_init(&have_names);
1712 for (i = 0; i < vsctl_port->port_cfg->n_interfaces; i++) {
1713 svec_add(&have_names,
1714 vsctl_port->port_cfg->interfaces[i]->name);
1716 svec_sort(&have_names);
1718 if (strcmp(vsctl_port->bridge->name, br_name)) {
1719 char *command = vsctl_context_to_string(ctx);
1720 vsctl_fatal("\"%s\" but %s is actually attached to bridge %s",
1721 command, port_name, vsctl_port->bridge->name);
1724 if (!svec_equal(&want_names, &have_names)) {
1725 char *have_names_string = svec_join(&have_names, ", ", "");
1726 char *command = vsctl_context_to_string(ctx);
1728 vsctl_fatal("\"%s\" but %s actually has interface(s) %s",
1729 command, port_name, have_names_string);
1732 svec_destroy(&want_names);
1733 svec_destroy(&have_names);
1739 check_conflicts(&info, port_name,
1740 xasprintf("cannot create a port named %s", port_name));
1741 for (i = 0; i < n_ifaces; i++) {
1742 check_conflicts(&info, iface_names[i],
1743 xasprintf("cannot create an interface named %s",
1746 bridge = find_bridge(&info, br_name, true);
1748 ifaces = xmalloc(n_ifaces * sizeof *ifaces);
1749 for (i = 0; i < n_ifaces; i++) {
1750 ifaces[i] = ovsrec_interface_insert(ctx->txn);
1751 ovsrec_interface_set_name(ifaces[i], iface_names[i]);
1754 port = ovsrec_port_insert(ctx->txn);
1755 ovsrec_port_set_name(port, port_name);
1756 ovsrec_port_set_interfaces(port, ifaces, n_ifaces);
1757 ovsrec_port_set_bond_fake_iface(port, fake_iface);
1760 if (bridge->parent) {
1761 int64_t tag = bridge->vlan;
1762 ovsrec_port_set_tag(port, &tag, 1);
1765 for (i = 0; i < n_settings; i++) {
1766 set_column(get_table("Port"), &port->header_, settings[i],
1770 bridge_insert_port((bridge->parent ? bridge->parent->br_cfg
1771 : bridge->br_cfg), port);
1777 cmd_add_port(struct vsctl_context *ctx)
1779 bool may_exist = shash_find(&ctx->options, "--may-exist") != NULL;
1781 add_port(ctx, ctx->argv[1], ctx->argv[2], may_exist, false,
1782 &ctx->argv[2], 1, &ctx->argv[3], ctx->argc - 3);
1786 cmd_add_bond(struct vsctl_context *ctx)
1788 bool may_exist = shash_find(&ctx->options, "--may-exist") != NULL;
1789 bool fake_iface = shash_find(&ctx->options, "--fake-iface");
1793 n_ifaces = ctx->argc - 3;
1794 for (i = 3; i < ctx->argc; i++) {
1795 if (strchr(ctx->argv[i], '=')) {
1801 vsctl_fatal("add-bond requires at least 2 interfaces, but only "
1802 "%d were specified", n_ifaces);
1805 add_port(ctx, ctx->argv[1], ctx->argv[2], may_exist, fake_iface,
1806 &ctx->argv[3], n_ifaces,
1807 &ctx->argv[n_ifaces + 3], ctx->argc - 3 - n_ifaces);
1811 cmd_del_port(struct vsctl_context *ctx)
1813 bool must_exist = !shash_find(&ctx->options, "--if-exists");
1814 bool with_iface = shash_find(&ctx->options, "--with-iface") != NULL;
1815 struct vsctl_port *port;
1816 struct vsctl_info info;
1818 get_info(ctx, &info);
1820 port = find_port(&info, ctx->argv[ctx->argc - 1], must_exist);
1822 const char *target = ctx->argv[ctx->argc - 1];
1823 struct vsctl_iface *iface;
1825 port = find_port(&info, target, false);
1827 iface = find_iface(&info, target, false);
1832 if (must_exist && !port) {
1833 vsctl_fatal("no port or interface named %s", target);
1838 if (ctx->argc == 3) {
1839 struct vsctl_bridge *bridge;
1841 bridge = find_bridge(&info, ctx->argv[1], true);
1842 if (port->bridge != bridge) {
1843 if (port->bridge->parent == bridge) {
1844 vsctl_fatal("bridge %s does not have a port %s (although "
1845 "its parent bridge %s does)",
1846 ctx->argv[1], ctx->argv[2],
1847 bridge->parent->name);
1849 vsctl_fatal("bridge %s does not have a port %s",
1850 ctx->argv[1], ctx->argv[2]);
1855 del_port(&info, port);
1862 cmd_port_to_br(struct vsctl_context *ctx)
1864 struct vsctl_port *port;
1865 struct vsctl_info info;
1867 get_info(ctx, &info);
1868 port = find_port(&info, ctx->argv[1], true);
1869 ds_put_format(&ctx->output, "%s\n", port->bridge->name);
1874 cmd_br_to_vlan(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 ds_put_format(&ctx->output, "%d\n", bridge->vlan);
1886 cmd_br_to_parent(struct vsctl_context *ctx)
1888 struct vsctl_bridge *bridge;
1889 struct vsctl_info info;
1891 get_info(ctx, &info);
1892 bridge = find_bridge(&info, ctx->argv[1], true);
1893 if (bridge->parent) {
1894 bridge = bridge->parent;
1896 ds_put_format(&ctx->output, "%s\n", bridge->name);
1901 cmd_list_ifaces(struct vsctl_context *ctx)
1903 struct vsctl_bridge *br;
1904 struct shash_node *node;
1905 struct vsctl_info info;
1908 get_info(ctx, &info);
1909 br = find_bridge(&info, ctx->argv[1], true);
1913 SHASH_FOR_EACH (node, &info.ifaces) {
1914 struct vsctl_iface *iface = node->data;
1916 if (strcmp(iface->iface_cfg->name, br->name)
1917 && br == iface->port->bridge) {
1918 svec_add(&ifaces, iface->iface_cfg->name);
1921 output_sorted(&ifaces, &ctx->output);
1922 svec_destroy(&ifaces);
1928 cmd_iface_to_br(struct vsctl_context *ctx)
1930 struct vsctl_iface *iface;
1931 struct vsctl_info info;
1933 get_info(ctx, &info);
1934 iface = find_iface(&info, ctx->argv[1], true);
1935 ds_put_format(&ctx->output, "%s\n", iface->port->bridge->name);
1940 verify_controllers(struct ovsrec_bridge *bridge)
1945 ovsrec_bridge_verify_controller(bridge);
1946 for (i = 0; i < bridge->n_controller; i++) {
1947 ovsrec_controller_verify_target(bridge->controller[i]);
1953 pre_controller(struct vsctl_context *ctx)
1957 ovsdb_idl_add_column(ctx->idl, &ovsrec_controller_col_target);
1961 cmd_get_controller(struct vsctl_context *ctx)
1963 struct vsctl_info info;
1964 struct vsctl_bridge *br;
1965 struct svec targets;
1968 get_info(ctx, &info);
1969 br = find_bridge(&info, ctx->argv[1], true);
1970 verify_controllers(br->br_cfg);
1972 /* Print the targets in sorted order for reproducibility. */
1973 svec_init(&targets);
1974 for (i = 0; i < br->n_ctrl; i++) {
1975 svec_add(&targets, br->ctrl[i]->target);
1978 svec_sort(&targets);
1979 for (i = 0; i < targets.n; i++) {
1980 ds_put_format(&ctx->output, "%s\n", targets.names[i]);
1982 svec_destroy(&targets);
1988 delete_controllers(struct ovsrec_controller **controllers,
1989 size_t n_controllers)
1993 for (i = 0; i < n_controllers; i++) {
1994 ovsrec_controller_delete(controllers[i]);
1999 cmd_del_controller(struct vsctl_context *ctx)
2001 struct vsctl_info info;
2002 struct vsctl_bridge *br;
2004 get_info(ctx, &info);
2006 br = find_real_bridge(&info, ctx->argv[1], true);
2007 verify_controllers(br->br_cfg);
2010 delete_controllers(br->ctrl, br->n_ctrl);
2011 ovsrec_bridge_set_controller(br->br_cfg, NULL, 0);
2017 static struct ovsrec_controller **
2018 insert_controllers(struct ovsdb_idl_txn *txn, char *targets[], size_t n)
2020 struct ovsrec_controller **controllers;
2023 controllers = xmalloc(n * sizeof *controllers);
2024 for (i = 0; i < n; i++) {
2025 if (vconn_verify_name(targets[i]) && pvconn_verify_name(targets[i])) {
2026 VLOG_WARN("target type \"%s\" is possibly erroneous", targets[i]);
2028 controllers[i] = ovsrec_controller_insert(txn);
2029 ovsrec_controller_set_target(controllers[i], targets[i]);
2036 cmd_set_controller(struct vsctl_context *ctx)
2038 struct vsctl_info info;
2039 struct vsctl_bridge *br;
2040 struct ovsrec_controller **controllers;
2043 get_info(ctx, &info);
2044 br = find_real_bridge(&info, ctx->argv[1], true);
2045 verify_controllers(br->br_cfg);
2047 delete_controllers(br->ctrl, br->n_ctrl);
2050 controllers = insert_controllers(ctx->txn, &ctx->argv[2], n);
2051 ovsrec_bridge_set_controller(br->br_cfg, controllers, n);
2058 cmd_get_fail_mode(struct vsctl_context *ctx)
2060 struct vsctl_info info;
2061 struct vsctl_bridge *br;
2063 get_info(ctx, &info);
2064 br = find_bridge(&info, ctx->argv[1], true);
2067 ovsrec_bridge_verify_fail_mode(br->br_cfg);
2069 if (br->fail_mode && strlen(br->fail_mode)) {
2070 ds_put_format(&ctx->output, "%s\n", br->fail_mode);
2077 cmd_del_fail_mode(struct vsctl_context *ctx)
2079 struct vsctl_info info;
2080 struct vsctl_bridge *br;
2082 get_info(ctx, &info);
2083 br = find_real_bridge(&info, ctx->argv[1], true);
2085 ovsrec_bridge_set_fail_mode(br->br_cfg, NULL);
2091 cmd_set_fail_mode(struct vsctl_context *ctx)
2093 struct vsctl_info info;
2094 struct vsctl_bridge *br;
2095 const char *fail_mode = ctx->argv[2];
2097 get_info(ctx, &info);
2098 br = find_real_bridge(&info, ctx->argv[1], true);
2100 if (strcmp(fail_mode, "standalone") && strcmp(fail_mode, "secure")) {
2101 vsctl_fatal("fail-mode must be \"standalone\" or \"secure\"");
2104 ovsrec_bridge_set_fail_mode(br->br_cfg, fail_mode);
2110 verify_managers(const struct ovsrec_open_vswitch *ovs)
2114 ovsrec_open_vswitch_verify_manager_options(ovs);
2116 for (i = 0; i < ovs->n_manager_options; ++i) {
2117 const struct ovsrec_manager *mgr = ovs->manager_options[i];
2119 ovsrec_manager_verify_target(mgr);
2124 pre_manager(struct vsctl_context *ctx)
2126 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_manager_options);
2127 ovsdb_idl_add_column(ctx->idl, &ovsrec_manager_col_target);
2131 cmd_get_manager(struct vsctl_context *ctx)
2133 const struct ovsrec_open_vswitch *ovs = ctx->ovs;
2134 struct svec targets;
2137 verify_managers(ovs);
2139 /* Print the targets in sorted order for reproducibility. */
2140 svec_init(&targets);
2142 for (i = 0; i < ovs->n_manager_options; i++) {
2143 svec_add(&targets, ovs->manager_options[i]->target);
2146 svec_sort_unique(&targets);
2147 for (i = 0; i < targets.n; i++) {
2148 ds_put_format(&ctx->output, "%s\n", targets.names[i]);
2150 svec_destroy(&targets);
2154 delete_managers(const struct vsctl_context *ctx)
2156 const struct ovsrec_open_vswitch *ovs = ctx->ovs;
2159 /* Delete Manager rows pointed to by 'manager_options' column. */
2160 for (i = 0; i < ovs->n_manager_options; i++) {
2161 ovsrec_manager_delete(ovs->manager_options[i]);
2164 /* Delete 'Manager' row refs in 'manager_options' column. */
2165 ovsrec_open_vswitch_set_manager_options(ovs, NULL, 0);
2169 cmd_del_manager(struct vsctl_context *ctx)
2171 const struct ovsrec_open_vswitch *ovs = ctx->ovs;
2173 verify_managers(ovs);
2174 delete_managers(ctx);
2178 insert_managers(struct vsctl_context *ctx, char *targets[], size_t n)
2180 struct ovsrec_manager **managers;
2183 /* Insert each manager in a new row in Manager table. */
2184 managers = xmalloc(n * sizeof *managers);
2185 for (i = 0; i < n; i++) {
2186 if (stream_verify_name(targets[i]) && pstream_verify_name(targets[i])) {
2187 VLOG_WARN("target type \"%s\" is possibly erroneous", targets[i]);
2189 managers[i] = ovsrec_manager_insert(ctx->txn);
2190 ovsrec_manager_set_target(managers[i], targets[i]);
2193 /* Store uuids of new Manager rows in 'manager_options' column. */
2194 ovsrec_open_vswitch_set_manager_options(ctx->ovs, managers, n);
2199 cmd_set_manager(struct vsctl_context *ctx)
2201 const size_t n = ctx->argc - 1;
2203 verify_managers(ctx->ovs);
2204 delete_managers(ctx);
2205 insert_managers(ctx, &ctx->argv[1], n);
2209 pre_cmd_get_ssl(struct vsctl_context *ctx)
2211 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_ssl);
2213 ovsdb_idl_add_column(ctx->idl, &ovsrec_ssl_col_private_key);
2214 ovsdb_idl_add_column(ctx->idl, &ovsrec_ssl_col_certificate);
2215 ovsdb_idl_add_column(ctx->idl, &ovsrec_ssl_col_ca_cert);
2216 ovsdb_idl_add_column(ctx->idl, &ovsrec_ssl_col_bootstrap_ca_cert);
2220 cmd_get_ssl(struct vsctl_context *ctx)
2222 struct ovsrec_ssl *ssl = ctx->ovs->ssl;
2224 ovsrec_open_vswitch_verify_ssl(ctx->ovs);
2226 ovsrec_ssl_verify_private_key(ssl);
2227 ovsrec_ssl_verify_certificate(ssl);
2228 ovsrec_ssl_verify_ca_cert(ssl);
2229 ovsrec_ssl_verify_bootstrap_ca_cert(ssl);
2231 ds_put_format(&ctx->output, "Private key: %s\n", ssl->private_key);
2232 ds_put_format(&ctx->output, "Certificate: %s\n", ssl->certificate);
2233 ds_put_format(&ctx->output, "CA Certificate: %s\n", ssl->ca_cert);
2234 ds_put_format(&ctx->output, "Bootstrap: %s\n",
2235 ssl->bootstrap_ca_cert ? "true" : "false");
2240 pre_cmd_del_ssl(struct vsctl_context *ctx)
2242 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_ssl);
2246 cmd_del_ssl(struct vsctl_context *ctx)
2248 struct ovsrec_ssl *ssl = ctx->ovs->ssl;
2251 ovsrec_open_vswitch_verify_ssl(ctx->ovs);
2252 ovsrec_ssl_delete(ssl);
2253 ovsrec_open_vswitch_set_ssl(ctx->ovs, NULL);
2258 pre_cmd_set_ssl(struct vsctl_context *ctx)
2260 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_ssl);
2264 cmd_set_ssl(struct vsctl_context *ctx)
2266 bool bootstrap = shash_find(&ctx->options, "--bootstrap");
2267 struct ovsrec_ssl *ssl = ctx->ovs->ssl;
2269 ovsrec_open_vswitch_verify_ssl(ctx->ovs);
2271 ovsrec_ssl_delete(ssl);
2273 ssl = ovsrec_ssl_insert(ctx->txn);
2275 ovsrec_ssl_set_private_key(ssl, ctx->argv[1]);
2276 ovsrec_ssl_set_certificate(ssl, ctx->argv[2]);
2277 ovsrec_ssl_set_ca_cert(ssl, ctx->argv[3]);
2279 ovsrec_ssl_set_bootstrap_ca_cert(ssl, bootstrap);
2281 ovsrec_open_vswitch_set_ssl(ctx->ovs, ssl);
2284 /* Parameter commands. */
2286 struct vsctl_row_id {
2287 const struct ovsdb_idl_table_class *table;
2288 const struct ovsdb_idl_column *name_column;
2289 const struct ovsdb_idl_column *uuid_column;
2292 struct vsctl_table_class {
2293 struct ovsdb_idl_table_class *class;
2294 struct vsctl_row_id row_ids[2];
2297 static const struct vsctl_table_class tables[] = {
2298 {&ovsrec_table_bridge,
2299 {{&ovsrec_table_bridge, &ovsrec_bridge_col_name, NULL},
2300 {NULL, NULL, NULL}}},
2302 {&ovsrec_table_controller,
2303 {{&ovsrec_table_bridge,
2304 &ovsrec_bridge_col_name,
2305 &ovsrec_bridge_col_controller}}},
2307 {&ovsrec_table_interface,
2308 {{&ovsrec_table_interface, &ovsrec_interface_col_name, NULL},
2309 {NULL, NULL, NULL}}},
2311 {&ovsrec_table_mirror,
2312 {{&ovsrec_table_mirror, &ovsrec_mirror_col_name, NULL},
2313 {NULL, NULL, NULL}}},
2315 {&ovsrec_table_manager,
2316 {{&ovsrec_table_manager, &ovsrec_manager_col_target, NULL},
2317 {NULL, NULL, NULL}}},
2319 {&ovsrec_table_netflow,
2320 {{&ovsrec_table_bridge,
2321 &ovsrec_bridge_col_name,
2322 &ovsrec_bridge_col_netflow},
2323 {NULL, NULL, NULL}}},
2325 {&ovsrec_table_open_vswitch,
2326 {{&ovsrec_table_open_vswitch, NULL, NULL},
2327 {NULL, NULL, NULL}}},
2329 {&ovsrec_table_port,
2330 {{&ovsrec_table_port, &ovsrec_port_col_name, NULL},
2331 {NULL, NULL, NULL}}},
2334 {{&ovsrec_table_port, &ovsrec_port_col_name, &ovsrec_port_col_qos},
2335 {NULL, NULL, NULL}}},
2337 {&ovsrec_table_queue,
2338 {{NULL, NULL, NULL},
2339 {NULL, NULL, NULL}}},
2342 {{&ovsrec_table_open_vswitch, NULL, &ovsrec_open_vswitch_col_ssl}}},
2344 {&ovsrec_table_sflow,
2345 {{&ovsrec_table_bridge,
2346 &ovsrec_bridge_col_name,
2347 &ovsrec_bridge_col_sflow},
2348 {NULL, NULL, NULL}}},
2350 {&ovsrec_table_flow_table,
2351 {{&ovsrec_table_flow_table, &ovsrec_flow_table_col_name, NULL},
2352 {NULL, NULL, NULL}}},
2354 {NULL, {{NULL, NULL, NULL}, {NULL, NULL, NULL}}}
2358 die_if_error(char *error)
2361 vsctl_fatal("%s", error);
2366 to_lower_and_underscores(unsigned c)
2368 return c == '-' ? '_' : tolower(c);
2372 score_partial_match(const char *name, const char *s)
2376 if (!strcmp(name, s)) {
2379 for (score = 0; ; score++, name++, s++) {
2380 if (to_lower_and_underscores(*name) != to_lower_and_underscores(*s)) {
2382 } else if (*name == '\0') {
2383 return UINT_MAX - 1;
2386 return *s == '\0' ? score : 0;
2389 static const struct vsctl_table_class *
2390 get_table(const char *table_name)
2392 const struct vsctl_table_class *table;
2393 const struct vsctl_table_class *best_match = NULL;
2394 unsigned int best_score = 0;
2396 for (table = tables; table->class; table++) {
2397 unsigned int score = score_partial_match(table->class->name,
2399 if (score > best_score) {
2402 } else if (score == best_score) {
2408 } else if (best_score) {
2409 vsctl_fatal("multiple table names match \"%s\"", table_name);
2411 vsctl_fatal("unknown table \"%s\"", table_name);
2415 static const struct vsctl_table_class *
2416 pre_get_table(struct vsctl_context *ctx, const char *table_name)
2418 const struct vsctl_table_class *table_class;
2421 table_class = get_table(table_name);
2422 ovsdb_idl_add_table(ctx->idl, table_class->class);
2424 for (i = 0; i < ARRAY_SIZE(table_class->row_ids); i++) {
2425 const struct vsctl_row_id *id = &table_class->row_ids[i];
2427 ovsdb_idl_add_table(ctx->idl, id->table);
2429 if (id->name_column) {
2430 ovsdb_idl_add_column(ctx->idl, id->name_column);
2432 if (id->uuid_column) {
2433 ovsdb_idl_add_column(ctx->idl, id->uuid_column);
2440 static const struct ovsdb_idl_row *
2441 get_row_by_id(struct vsctl_context *ctx, const struct vsctl_table_class *table,
2442 const struct vsctl_row_id *id, const char *record_id)
2444 const struct ovsdb_idl_row *referrer, *final;
2450 if (!id->name_column) {
2451 if (strcmp(record_id, ".")) {
2454 referrer = ovsdb_idl_first_row(ctx->idl, id->table);
2455 if (!referrer || ovsdb_idl_next_row(referrer)) {
2459 const struct ovsdb_idl_row *row;
2462 for (row = ovsdb_idl_first_row(ctx->idl, id->table);
2464 row = ovsdb_idl_next_row(row))
2466 const struct ovsdb_datum *name;
2468 name = ovsdb_idl_get(row, id->name_column,
2469 OVSDB_TYPE_STRING, OVSDB_TYPE_VOID);
2470 if (name->n == 1 && !strcmp(name->keys[0].string, record_id)) {
2472 vsctl_fatal("multiple rows in %s match \"%s\"",
2473 table->class->name, record_id);
2484 if (id->uuid_column) {
2485 const struct ovsdb_datum *uuid;
2487 ovsdb_idl_txn_verify(referrer, id->uuid_column);
2488 uuid = ovsdb_idl_get(referrer, id->uuid_column,
2489 OVSDB_TYPE_UUID, OVSDB_TYPE_VOID);
2491 final = ovsdb_idl_get_row_for_uuid(ctx->idl, table->class,
2492 &uuid->keys[0].uuid);
2501 static const struct ovsdb_idl_row *
2502 get_row (struct vsctl_context *ctx,
2503 const struct vsctl_table_class *table, const char *record_id)
2505 const struct ovsdb_idl_row *row;
2508 if (uuid_from_string(&uuid, record_id)) {
2509 row = ovsdb_idl_get_row_for_uuid(ctx->idl, table->class, &uuid);
2513 for (i = 0; i < ARRAY_SIZE(table->row_ids); i++) {
2514 row = get_row_by_id(ctx, table, &table->row_ids[i], record_id);
2523 static const struct ovsdb_idl_row *
2524 must_get_row(struct vsctl_context *ctx,
2525 const struct vsctl_table_class *table, const char *record_id)
2527 const struct ovsdb_idl_row *row = get_row(ctx, table, record_id);
2529 vsctl_fatal("no row \"%s\" in table %s",
2530 record_id, table->class->name);
2536 get_column(const struct vsctl_table_class *table, const char *column_name,
2537 const struct ovsdb_idl_column **columnp)
2539 const struct ovsdb_idl_column *best_match = NULL;
2540 unsigned int best_score = 0;
2543 for (i = 0; i < table->class->n_columns; i++) {
2544 const struct ovsdb_idl_column *column = &table->class->columns[i];
2545 unsigned int score = score_partial_match(column->name, column_name);
2546 if (score > best_score) {
2547 best_match = column;
2549 } else if (score == best_score) {
2554 *columnp = best_match;
2557 } else if (best_score) {
2558 return xasprintf("%s contains more than one column whose name "
2559 "matches \"%s\"", table->class->name, column_name);
2561 return xasprintf("%s does not contain a column whose name matches "
2562 "\"%s\"", table->class->name, column_name);
2566 static struct ovsdb_symbol *
2567 create_symbol(struct ovsdb_symbol_table *symtab, const char *id, bool *newp)
2569 struct ovsdb_symbol *symbol;
2572 vsctl_fatal("row id \"%s\" does not begin with \"@\"", id);
2576 *newp = ovsdb_symbol_table_get(symtab, id) == NULL;
2579 symbol = ovsdb_symbol_table_insert(symtab, id);
2580 if (symbol->created) {
2581 vsctl_fatal("row id \"%s\" may only be specified on one --id option",
2584 symbol->created = true;
2589 pre_get_column(struct vsctl_context *ctx,
2590 const struct vsctl_table_class *table, const char *column_name,
2591 const struct ovsdb_idl_column **columnp)
2593 die_if_error(get_column(table, column_name, columnp));
2594 ovsdb_idl_add_column(ctx->idl, *columnp);
2598 missing_operator_error(const char *arg, const char **allowed_operators,
2604 ds_put_format(&s, "%s: argument does not end in ", arg);
2605 ds_put_format(&s, "\"%s\"", allowed_operators[0]);
2606 if (n_allowed == 2) {
2607 ds_put_format(&s, " or \"%s\"", allowed_operators[1]);
2608 } else if (n_allowed > 2) {
2611 for (i = 1; i < n_allowed - 1; i++) {
2612 ds_put_format(&s, ", \"%s\"", allowed_operators[i]);
2614 ds_put_format(&s, ", or \"%s\"", allowed_operators[i]);
2616 ds_put_format(&s, " followed by a value.");
2618 return ds_steal_cstr(&s);
2621 /* Breaks 'arg' apart into a number of fields in the following order:
2623 * - The name of a column in 'table', stored into '*columnp'. The column
2624 * name may be abbreviated.
2626 * - Optionally ':' followed by a key string. The key is stored as a
2627 * malloc()'d string into '*keyp', or NULL if no key is present in
2630 * - If 'valuep' is nonnull, an operator followed by a value string. The
2631 * allowed operators are the 'n_allowed' string in 'allowed_operators',
2632 * or just "=" if 'n_allowed' is 0. If 'operatorp' is nonnull, then the
2633 * index of the operator within 'allowed_operators' is stored into
2634 * '*operatorp'. The value is stored as a malloc()'d string into
2635 * '*valuep', or NULL if no value is present in 'arg'.
2637 * On success, returns NULL. On failure, returned a malloc()'d string error
2638 * message and stores NULL into all of the nonnull output arguments. */
2639 static char * WARN_UNUSED_RESULT
2640 parse_column_key_value(const char *arg,
2641 const struct vsctl_table_class *table,
2642 const struct ovsdb_idl_column **columnp, char **keyp,
2644 const char **allowed_operators, size_t n_allowed,
2647 const char *p = arg;
2651 assert(!(operatorp && !valuep));
2657 /* Parse column name. */
2658 error = ovsdb_token_parse(&p, &column_name);
2662 if (column_name[0] == '\0') {
2664 error = xasprintf("%s: missing column name", arg);
2667 error = get_column(table, column_name, columnp);
2673 /* Parse key string. */
2676 error = ovsdb_token_parse(&p, keyp);
2682 /* Parse value string. */
2688 if (!allowed_operators) {
2689 static const char *equals = "=";
2690 allowed_operators = =
2696 for (i = 0; i < n_allowed; i++) {
2697 const char *op = allowed_operators[i];
2698 size_t op_len = strlen(op);
2700 if (op_len > best_len && !strncmp(op, p, op_len) && p[op_len]) {
2706 error = missing_operator_error(arg, allowed_operators, n_allowed);
2713 *valuep = xstrdup(p + best_len);
2716 error = xasprintf("%s: trailing garbage \"%s\" in argument",
2738 pre_parse_column_key_value(struct vsctl_context *ctx,
2740 const struct vsctl_table_class *table)
2742 const struct ovsdb_idl_column *column;
2747 die_if_error(ovsdb_token_parse(&p, &column_name));
2748 if (column_name[0] == '\0') {
2749 vsctl_fatal("%s: missing column name", arg);
2752 pre_get_column(ctx, table, column_name, &column);
2757 pre_cmd_get(struct vsctl_context *ctx)
2759 const char *id = shash_find_data(&ctx->options, "--id");
2760 const char *table_name = ctx->argv[1];
2761 const struct vsctl_table_class *table;
2764 /* Using "get" without --id or a column name could possibly make sense.
2765 * Maybe, for example, a ovs-vsctl run wants to assert that a row exists.
2766 * But it is unlikely that an interactive user would want to do that, so
2767 * issue a warning if we're running on a terminal. */
2768 if (!id && ctx->argc <= 3 && isatty(STDOUT_FILENO)) {
2769 VLOG_WARN("\"get\" command without row arguments or \"--id\" is "
2770 "possibly erroneous");
2773 table = pre_get_table(ctx, table_name);
2774 for (i = 3; i < ctx->argc; i++) {
2775 if (!strcasecmp(ctx->argv[i], "_uuid")
2776 || !strcasecmp(ctx->argv[i], "-uuid")) {
2780 pre_parse_column_key_value(ctx, ctx->argv[i], table);
2785 cmd_get(struct vsctl_context *ctx)
2787 const char *id = shash_find_data(&ctx->options, "--id");
2788 bool if_exists = shash_find(&ctx->options, "--if-exists");
2789 const char *table_name = ctx->argv[1];
2790 const char *record_id = ctx->argv[2];
2791 const struct vsctl_table_class *table;
2792 const struct ovsdb_idl_row *row;
2793 struct ds *out = &ctx->output;
2796 table = get_table(table_name);
2797 row = must_get_row(ctx, table, record_id);
2799 struct ovsdb_symbol *symbol;
2802 symbol = create_symbol(ctx->symtab, id, &new);
2804 vsctl_fatal("row id \"%s\" specified on \"get\" command was used "
2805 "before it was defined", id);
2807 symbol->uuid = row->uuid;
2809 /* This symbol refers to a row that already exists, so disable warnings
2810 * about it being unreferenced. */
2811 symbol->strong_ref = true;
2813 for (i = 3; i < ctx->argc; i++) {
2814 const struct ovsdb_idl_column *column;
2815 const struct ovsdb_datum *datum;
2818 /* Special case for obtaining the UUID of a row. We can't just do this
2819 * through parse_column_key_value() below since it returns a "struct
2820 * ovsdb_idl_column" and the UUID column doesn't have one. */
2821 if (!strcasecmp(ctx->argv[i], "_uuid")
2822 || !strcasecmp(ctx->argv[i], "-uuid")) {
2823 ds_put_format(out, UUID_FMT"\n", UUID_ARGS(&row->uuid));
2827 die_if_error(parse_column_key_value(ctx->argv[i], table,
2828 &column, &key_string,
2829 NULL, NULL, 0, NULL));
2831 ovsdb_idl_txn_verify(row, column);
2832 datum = ovsdb_idl_read(row, column);
2834 union ovsdb_atom key;
2837 if (column->type.value.type == OVSDB_TYPE_VOID) {
2838 vsctl_fatal("cannot specify key to get for non-map column %s",
2842 die_if_error(ovsdb_atom_from_string(&key,
2844 key_string, ctx->symtab));
2846 idx = ovsdb_datum_find_key(datum, &key,
2847 column->type.key.type);
2848 if (idx == UINT_MAX) {
2850 vsctl_fatal("no key \"%s\" in %s record \"%s\" column %s",
2851 key_string, table->class->name, record_id,
2855 ovsdb_atom_to_string(&datum->values[idx],
2856 column->type.value.type, out);
2858 ovsdb_atom_destroy(&key, column->type.key.type);
2860 ovsdb_datum_to_string(datum, &column->type, out);
2862 ds_put_char(out, '\n');
2869 parse_column_names(const char *column_names,
2870 const struct vsctl_table_class *table,
2871 const struct ovsdb_idl_column ***columnsp,
2874 const struct ovsdb_idl_column **columns;
2877 if (!column_names) {
2880 n_columns = table->class->n_columns + 1;
2881 columns = xmalloc(n_columns * sizeof *columns);
2883 for (i = 0; i < table->class->n_columns; i++) {
2884 columns[i + 1] = &table->class->columns[i];
2887 char *s = xstrdup(column_names);
2888 size_t allocated_columns;
2889 char *save_ptr = NULL;
2893 allocated_columns = n_columns = 0;
2894 for (column_name = strtok_r(s, ", ", &save_ptr); column_name;
2895 column_name = strtok_r(NULL, ", ", &save_ptr)) {
2896 const struct ovsdb_idl_column *column;
2898 if (!strcasecmp(column_name, "_uuid")) {
2901 die_if_error(get_column(table, column_name, &column));
2903 if (n_columns >= allocated_columns) {
2904 columns = x2nrealloc(columns, &allocated_columns,
2907 columns[n_columns++] = column;
2912 vsctl_fatal("must specify at least one column name");
2915 *columnsp = columns;
2916 *n_columnsp = n_columns;
2921 pre_list_columns(struct vsctl_context *ctx,
2922 const struct vsctl_table_class *table,
2923 const char *column_names)
2925 const struct ovsdb_idl_column **columns;
2929 parse_column_names(column_names, table, &columns, &n_columns);
2930 for (i = 0; i < n_columns; i++) {
2932 ovsdb_idl_add_column(ctx->idl, columns[i]);
2939 pre_cmd_list(struct vsctl_context *ctx)
2941 const char *column_names = shash_find_data(&ctx->options, "--columns");
2942 const char *table_name = ctx->argv[1];
2943 const struct vsctl_table_class *table;
2945 table = pre_get_table(ctx, table_name);
2946 pre_list_columns(ctx, table, column_names);
2949 static struct table *
2950 list_make_table(const struct ovsdb_idl_column **columns, size_t n_columns)
2955 out = xmalloc(sizeof *out);
2958 for (i = 0; i < n_columns; i++) {
2959 const struct ovsdb_idl_column *column = columns[i];
2960 const char *column_name = column ? column->name : "_uuid";
2962 table_add_column(out, "%s", column_name);
2969 list_record(const struct ovsdb_idl_row *row,
2970 const struct ovsdb_idl_column **columns, size_t n_columns,
2976 for (i = 0; i < n_columns; i++) {
2977 const struct ovsdb_idl_column *column = columns[i];
2978 struct cell *cell = table_add_cell(out);
2981 struct ovsdb_datum datum;
2982 union ovsdb_atom atom;
2984 atom.uuid = row->uuid;
2987 datum.values = NULL;
2990 cell->json = ovsdb_datum_to_json(&datum, &ovsdb_type_uuid);
2991 cell->type = &ovsdb_type_uuid;
2993 const struct ovsdb_datum *datum = ovsdb_idl_read(row, column);
2995 cell->json = ovsdb_datum_to_json(datum, &column->type);
2996 cell->type = &column->type;
3002 cmd_list(struct vsctl_context *ctx)
3004 const char *column_names = shash_find_data(&ctx->options, "--columns");
3005 const struct ovsdb_idl_column **columns;
3006 const char *table_name = ctx->argv[1];
3007 const struct vsctl_table_class *table;
3012 table = get_table(table_name);
3013 parse_column_names(column_names, table, &columns, &n_columns);
3014 out = ctx->table = list_make_table(columns, n_columns);
3015 if (ctx->argc > 2) {
3016 for (i = 2; i < ctx->argc; i++) {
3017 list_record(must_get_row(ctx, table, ctx->argv[i]),
3018 columns, n_columns, out);
3021 const struct ovsdb_idl_row *row;
3023 for (row = ovsdb_idl_first_row(ctx->idl, table->class); row != NULL;
3024 row = ovsdb_idl_next_row(row)) {
3025 list_record(row, columns, n_columns, out);
3032 pre_cmd_find(struct vsctl_context *ctx)
3034 const char *column_names = shash_find_data(&ctx->options, "--columns");
3035 const char *table_name = ctx->argv[1];
3036 const struct vsctl_table_class *table;
3039 table = pre_get_table(ctx, table_name);
3040 pre_list_columns(ctx, table, column_names);
3041 for (i = 2; i < ctx->argc; i++) {
3042 pre_parse_column_key_value(ctx, ctx->argv[i], table);
3047 cmd_find(struct vsctl_context *ctx)
3049 const char *column_names = shash_find_data(&ctx->options, "--columns");
3050 const struct ovsdb_idl_column **columns;
3051 const char *table_name = ctx->argv[1];
3052 const struct vsctl_table_class *table;
3053 const struct ovsdb_idl_row *row;
3057 table = get_table(table_name);
3058 parse_column_names(column_names, table, &columns, &n_columns);
3059 out = ctx->table = list_make_table(columns, n_columns);
3060 for (row = ovsdb_idl_first_row(ctx->idl, table->class); row;
3061 row = ovsdb_idl_next_row(row)) {
3064 for (i = 2; i < ctx->argc; i++) {
3065 if (!is_condition_satisfied(table, row, ctx->argv[i],
3070 list_record(row, columns, n_columns, out);
3078 pre_cmd_set(struct vsctl_context *ctx)
3080 const char *table_name = ctx->argv[1];
3081 const struct vsctl_table_class *table;
3084 table = pre_get_table(ctx, table_name);
3085 for (i = 3; i < ctx->argc; i++) {
3086 pre_parse_column_key_value(ctx, ctx->argv[i], table);
3091 set_column(const struct vsctl_table_class *table,
3092 const struct ovsdb_idl_row *row, const char *arg,
3093 struct ovsdb_symbol_table *symtab)
3095 const struct ovsdb_idl_column *column;
3096 char *key_string, *value_string;
3099 error = parse_column_key_value(arg, table, &column, &key_string,
3100 NULL, NULL, 0, &value_string);
3101 die_if_error(error);
3102 if (!value_string) {
3103 vsctl_fatal("%s: missing value", arg);
3107 union ovsdb_atom key, value;
3108 struct ovsdb_datum datum;
3110 if (column->type.value.type == OVSDB_TYPE_VOID) {
3111 vsctl_fatal("cannot specify key to set for non-map column %s",
3115 die_if_error(ovsdb_atom_from_string(&key, &column->type.key,
3116 key_string, symtab));
3117 die_if_error(ovsdb_atom_from_string(&value, &column->type.value,
3118 value_string, symtab));
3120 ovsdb_datum_init_empty(&datum);
3121 ovsdb_datum_add_unsafe(&datum, &key, &value, &column->type);
3123 ovsdb_atom_destroy(&key, column->type.key.type);
3124 ovsdb_atom_destroy(&value, column->type.value.type);
3126 ovsdb_datum_union(&datum, ovsdb_idl_read(row, column),
3127 &column->type, false);
3128 ovsdb_idl_txn_write(row, column, &datum);
3130 struct ovsdb_datum datum;
3132 die_if_error(ovsdb_datum_from_string(&datum, &column->type,
3133 value_string, symtab));
3134 ovsdb_idl_txn_write(row, column, &datum);
3142 cmd_set(struct vsctl_context *ctx)
3144 const char *table_name = ctx->argv[1];
3145 const char *record_id = ctx->argv[2];
3146 const struct vsctl_table_class *table;
3147 const struct ovsdb_idl_row *row;
3150 table = get_table(table_name);
3151 row = must_get_row(ctx, table, record_id);
3152 for (i = 3; i < ctx->argc; i++) {
3153 set_column(table, row, ctx->argv[i], ctx->symtab);
3158 pre_cmd_add(struct vsctl_context *ctx)
3160 const char *table_name = ctx->argv[1];
3161 const char *column_name = ctx->argv[3];
3162 const struct vsctl_table_class *table;
3163 const struct ovsdb_idl_column *column;
3165 table = pre_get_table(ctx, table_name);
3166 pre_get_column(ctx, table, column_name, &column);
3170 cmd_add(struct vsctl_context *ctx)
3172 const char *table_name = ctx->argv[1];
3173 const char *record_id = ctx->argv[2];
3174 const char *column_name = ctx->argv[3];
3175 const struct vsctl_table_class *table;
3176 const struct ovsdb_idl_column *column;
3177 const struct ovsdb_idl_row *row;
3178 const struct ovsdb_type *type;
3179 struct ovsdb_datum old;
3182 table = get_table(table_name);
3183 row = must_get_row(ctx, table, record_id);
3184 die_if_error(get_column(table, column_name, &column));
3186 type = &column->type;
3187 ovsdb_datum_clone(&old, ovsdb_idl_read(row, column), &column->type);
3188 for (i = 4; i < ctx->argc; i++) {
3189 struct ovsdb_type add_type;
3190 struct ovsdb_datum add;
3194 add_type.n_max = UINT_MAX;
3195 die_if_error(ovsdb_datum_from_string(&add, &add_type, ctx->argv[i],
3197 ovsdb_datum_union(&old, &add, type, false);
3198 ovsdb_datum_destroy(&add, type);
3200 if (old.n > type->n_max) {
3201 vsctl_fatal("\"add\" operation would put %u %s in column %s of "
3202 "table %s but the maximum number is %u",
3204 type->value.type == OVSDB_TYPE_VOID ? "values" : "pairs",
3205 column->name, table->class->name, type->n_max);
3207 ovsdb_idl_txn_verify(row, column);
3208 ovsdb_idl_txn_write(row, column, &old);
3212 pre_cmd_remove(struct vsctl_context *ctx)
3214 const char *table_name = ctx->argv[1];
3215 const char *column_name = ctx->argv[3];
3216 const struct vsctl_table_class *table;
3217 const struct ovsdb_idl_column *column;
3219 table = pre_get_table(ctx, table_name);
3220 pre_get_column(ctx, table, column_name, &column);
3224 cmd_remove(struct vsctl_context *ctx)
3226 const char *table_name = ctx->argv[1];
3227 const char *record_id = ctx->argv[2];
3228 const char *column_name = ctx->argv[3];
3229 const struct vsctl_table_class *table;
3230 const struct ovsdb_idl_column *column;
3231 const struct ovsdb_idl_row *row;
3232 const struct ovsdb_type *type;
3233 struct ovsdb_datum old;
3236 table = get_table(table_name);
3237 row = must_get_row(ctx, table, record_id);
3238 die_if_error(get_column(table, column_name, &column));
3240 type = &column->type;
3241 ovsdb_datum_clone(&old, ovsdb_idl_read(row, column), &column->type);
3242 for (i = 4; i < ctx->argc; i++) {
3243 struct ovsdb_type rm_type;
3244 struct ovsdb_datum rm;
3249 rm_type.n_max = UINT_MAX;
3250 error = ovsdb_datum_from_string(&rm, &rm_type,
3251 ctx->argv[i], ctx->symtab);
3252 if (error && ovsdb_type_is_map(&rm_type)) {
3254 rm_type.value.type = OVSDB_TYPE_VOID;
3255 die_if_error(ovsdb_datum_from_string(&rm, &rm_type,
3256 ctx->argv[i], ctx->symtab));
3258 ovsdb_datum_subtract(&old, type, &rm, &rm_type);
3259 ovsdb_datum_destroy(&rm, &rm_type);
3261 if (old.n < type->n_min) {
3262 vsctl_fatal("\"remove\" operation would put %u %s in column %s of "
3263 "table %s but the minimum number is %u",
3265 type->value.type == OVSDB_TYPE_VOID ? "values" : "pairs",
3266 column->name, table->class->name, type->n_min);
3268 ovsdb_idl_txn_verify(row, column);
3269 ovsdb_idl_txn_write(row, column, &old);
3273 pre_cmd_clear(struct vsctl_context *ctx)
3275 const char *table_name = ctx->argv[1];
3276 const struct vsctl_table_class *table;
3279 table = pre_get_table(ctx, table_name);
3280 for (i = 3; i < ctx->argc; i++) {
3281 const struct ovsdb_idl_column *column;
3283 pre_get_column(ctx, table, ctx->argv[i], &column);
3288 cmd_clear(struct vsctl_context *ctx)
3290 const char *table_name = ctx->argv[1];
3291 const char *record_id = ctx->argv[2];
3292 const struct vsctl_table_class *table;
3293 const struct ovsdb_idl_row *row;
3296 table = get_table(table_name);
3297 row = must_get_row(ctx, table, record_id);
3298 for (i = 3; i < ctx->argc; i++) {
3299 const struct ovsdb_idl_column *column;
3300 const struct ovsdb_type *type;
3301 struct ovsdb_datum datum;
3303 die_if_error(get_column(table, ctx->argv[i], &column));
3305 type = &column->type;
3306 if (type->n_min > 0) {
3307 vsctl_fatal("\"clear\" operation cannot be applied to column %s "
3308 "of table %s, which is not allowed to be empty",
3309 column->name, table->class->name);
3312 ovsdb_datum_init_empty(&datum);
3313 ovsdb_idl_txn_write(row, column, &datum);
3318 pre_create(struct vsctl_context *ctx)
3320 const char *id = shash_find_data(&ctx->options, "--id");
3321 const char *table_name = ctx->argv[1];
3322 const struct vsctl_table_class *table;
3324 table = get_table(table_name);
3325 if (!id && !table->class->is_root) {
3326 VLOG_WARN("applying \"create\" command to table %s without --id "
3327 "option will have no effect", table->class->name);
3332 cmd_create(struct vsctl_context *ctx)
3334 const char *id = shash_find_data(&ctx->options, "--id");
3335 const char *table_name = ctx->argv[1];
3336 const struct vsctl_table_class *table = get_table(table_name);
3337 const struct ovsdb_idl_row *row;
3338 const struct uuid *uuid;
3342 struct ovsdb_symbol *symbol = create_symbol(ctx->symtab, id, NULL);
3343 if (table->class->is_root) {
3344 /* This table is in the root set, meaning that rows created in it
3345 * won't disappear even if they are unreferenced, so disable
3346 * warnings about that by pretending that there is a reference. */
3347 symbol->strong_ref = true;
3349 uuid = &symbol->uuid;
3354 row = ovsdb_idl_txn_insert(ctx->txn, table->class, uuid);
3355 for (i = 2; i < ctx->argc; i++) {
3356 set_column(table, row, ctx->argv[i], ctx->symtab);
3358 ds_put_format(&ctx->output, UUID_FMT, UUID_ARGS(&row->uuid));
3361 /* This function may be used as the 'postprocess' function for commands that
3362 * insert new rows into the database. It expects that the command's 'run'
3363 * function prints the UUID reported by ovsdb_idl_txn_insert() as the command's
3364 * sole output. It replaces that output by the row's permanent UUID assigned
3365 * by the database server and appends a new-line.
3367 * Currently we use this only for "create", because the higher-level commands
3368 * are supposed to be independent of the actual structure of the vswitch
3371 post_create(struct vsctl_context *ctx)
3373 const struct uuid *real;
3376 if (!uuid_from_string(&dummy, ds_cstr(&ctx->output))) {
3379 real = ovsdb_idl_txn_get_insert_uuid(ctx->txn, &dummy);
3381 ds_clear(&ctx->output);
3382 ds_put_format(&ctx->output, UUID_FMT, UUID_ARGS(real));
3384 ds_put_char(&ctx->output, '\n');
3388 pre_cmd_destroy(struct vsctl_context *ctx)
3390 const char *table_name = ctx->argv[1];
3392 pre_get_table(ctx, table_name);
3396 cmd_destroy(struct vsctl_context *ctx)
3398 bool must_exist = !shash_find(&ctx->options, "--if-exists");
3399 const char *table_name = ctx->argv[1];
3400 const struct vsctl_table_class *table;
3403 table = get_table(table_name);
3404 for (i = 2; i < ctx->argc; i++) {
3405 const struct ovsdb_idl_row *row;
3407 row = (must_exist ? must_get_row : get_row)(ctx, table, ctx->argv[i]);
3409 ovsdb_idl_txn_delete(row);
3415 RELOP(RELOP_EQ, "=") \
3416 RELOP(RELOP_NE, "!=") \
3417 RELOP(RELOP_LT, "<") \
3418 RELOP(RELOP_GT, ">") \
3419 RELOP(RELOP_LE, "<=") \
3420 RELOP(RELOP_GE, ">=") \
3421 RELOP(RELOP_SET_EQ, "{=}") \
3422 RELOP(RELOP_SET_NE, "{!=}") \
3423 RELOP(RELOP_SET_LT, "{<}") \
3424 RELOP(RELOP_SET_GT, "{>}") \
3425 RELOP(RELOP_SET_LE, "{<=}") \
3426 RELOP(RELOP_SET_GE, "{>=}")
3429 #define RELOP(ENUM, STRING) ENUM,
3435 is_set_operator(enum relop op)
3437 return (op == RELOP_SET_EQ || op == RELOP_SET_NE ||
3438 op == RELOP_SET_LT || op == RELOP_SET_GT ||
3439 op == RELOP_SET_LE || op == RELOP_SET_GE);
3443 evaluate_relop(const struct ovsdb_datum *a, const struct ovsdb_datum *b,
3444 const struct ovsdb_type *type, enum relop op)
3449 return ovsdb_datum_compare_3way(a, b, type) == 0;
3452 return ovsdb_datum_compare_3way(a, b, type) != 0;
3454 return ovsdb_datum_compare_3way(a, b, type) < 0;
3456 return ovsdb_datum_compare_3way(a, b, type) > 0;
3458 return ovsdb_datum_compare_3way(a, b, type) <= 0;
3460 return ovsdb_datum_compare_3way(a, b, type) >= 0;
3463 return b->n > a->n && ovsdb_datum_includes_all(a, b, type);
3465 return a->n > b->n && ovsdb_datum_includes_all(b, a, type);
3467 return ovsdb_datum_includes_all(a, b, type);
3469 return ovsdb_datum_includes_all(b, a, type);
3477 is_condition_satisfied(const struct vsctl_table_class *table,
3478 const struct ovsdb_idl_row *row, const char *arg,
3479 struct ovsdb_symbol_table *symtab)
3481 static const char *operators[] = {
3482 #define RELOP(ENUM, STRING) STRING,
3487 const struct ovsdb_idl_column *column;
3488 const struct ovsdb_datum *have_datum;
3489 char *key_string, *value_string;
3490 struct ovsdb_type type;
3495 error = parse_column_key_value(arg, table, &column, &key_string,
3496 &operator, operators, ARRAY_SIZE(operators),
3498 die_if_error(error);
3499 if (!value_string) {
3500 vsctl_fatal("%s: missing value", arg);
3503 type = column->type;
3504 type.n_max = UINT_MAX;
3506 have_datum = ovsdb_idl_read(row, column);
3508 union ovsdb_atom want_key;
3509 struct ovsdb_datum b;
3512 if (column->type.value.type == OVSDB_TYPE_VOID) {
3513 vsctl_fatal("cannot specify key to check for non-map column %s",
3517 die_if_error(ovsdb_atom_from_string(&want_key, &column->type.key,
3518 key_string, symtab));
3520 type.key = type.value;
3521 type.value.type = OVSDB_TYPE_VOID;
3522 die_if_error(ovsdb_datum_from_string(&b, &type, value_string, symtab));
3524 idx = ovsdb_datum_find_key(have_datum,
3525 &want_key, column->type.key.type);
3526 if (idx == UINT_MAX && !is_set_operator(operator)) {
3529 struct ovsdb_datum a;
3531 if (idx != UINT_MAX) {
3533 a.keys = &have_datum->values[idx];
3541 retval = evaluate_relop(&a, &b, &type, operator);
3544 ovsdb_atom_destroy(&want_key, column->type.key.type);
3545 ovsdb_datum_destroy(&b, &type);
3547 struct ovsdb_datum want_datum;
3549 die_if_error(ovsdb_datum_from_string(&want_datum, &column->type,
3550 value_string, symtab));
3551 retval = evaluate_relop(have_datum, &want_datum, &type, operator);
3552 ovsdb_datum_destroy(&want_datum, &column->type);
3562 pre_cmd_wait_until(struct vsctl_context *ctx)
3564 const char *table_name = ctx->argv[1];
3565 const struct vsctl_table_class *table;
3568 table = pre_get_table(ctx, table_name);
3570 for (i = 3; i < ctx->argc; i++) {
3571 pre_parse_column_key_value(ctx, ctx->argv[i], table);
3576 cmd_wait_until(struct vsctl_context *ctx)
3578 const char *table_name = ctx->argv[1];
3579 const char *record_id = ctx->argv[2];
3580 const struct vsctl_table_class *table;
3581 const struct ovsdb_idl_row *row;
3584 table = get_table(table_name);
3586 row = get_row(ctx, table, record_id);
3588 ctx->try_again = true;
3592 for (i = 3; i < ctx->argc; i++) {
3593 if (!is_condition_satisfied(table, row, ctx->argv[i], ctx->symtab)) {
3594 ctx->try_again = true;
3600 static struct json *
3601 where_uuid_equals(const struct uuid *uuid)
3604 json_array_create_1(
3605 json_array_create_3(
3606 json_string_create("_uuid"),
3607 json_string_create("=="),
3608 json_array_create_2(
3609 json_string_create("uuid"),
3610 json_string_create_nocopy(
3611 xasprintf(UUID_FMT, UUID_ARGS(uuid))))));
3615 vsctl_context_init(struct vsctl_context *ctx, struct vsctl_command *command,
3616 struct ovsdb_idl *idl, struct ovsdb_idl_txn *txn,
3617 const struct ovsrec_open_vswitch *ovs,
3618 struct ovsdb_symbol_table *symtab)
3620 ctx->argc = command->argc;
3621 ctx->argv = command->argv;
3622 ctx->options = command->options;
3624 ds_swap(&ctx->output, &command->output);
3625 ctx->table = command->table;
3629 ctx->symtab = symtab;
3630 ctx->verified_ports = false;
3632 ctx->try_again = false;
3636 vsctl_context_done(struct vsctl_context *ctx, struct vsctl_command *command)
3638 ds_swap(&ctx->output, &command->output);
3639 command->table = ctx->table;
3643 run_prerequisites(struct vsctl_command *commands, size_t n_commands,
3644 struct ovsdb_idl *idl)
3646 struct vsctl_command *c;
3648 ovsdb_idl_add_table(idl, &ovsrec_table_open_vswitch);
3649 if (wait_for_reload) {
3650 ovsdb_idl_add_column(idl, &ovsrec_open_vswitch_col_cur_cfg);
3652 for (c = commands; c < &commands[n_commands]; c++) {
3653 if (c->syntax->prerequisites) {
3654 struct vsctl_context ctx;
3656 ds_init(&c->output);
3659 vsctl_context_init(&ctx, c, idl, NULL, NULL, NULL);
3660 (c->syntax->prerequisites)(&ctx);
3661 vsctl_context_done(&ctx, c);
3663 assert(!c->output.string);
3669 static enum ovsdb_idl_txn_status
3670 do_vsctl(const char *args, struct vsctl_command *commands, size_t n_commands,
3671 struct ovsdb_idl *idl)
3673 struct ovsdb_idl_txn *txn;
3674 const struct ovsrec_open_vswitch *ovs;
3675 enum ovsdb_idl_txn_status status;
3676 struct ovsdb_symbol_table *symtab;
3677 struct vsctl_command *c;
3678 struct shash_node *node;
3679 int64_t next_cfg = 0;
3682 txn = the_idl_txn = ovsdb_idl_txn_create(idl);
3684 ovsdb_idl_txn_set_dry_run(txn);
3687 ovsdb_idl_txn_add_comment(txn, "ovs-vsctl: %s", args);
3689 ovs = ovsrec_open_vswitch_first(idl);
3691 /* XXX add verification that table is empty */
3692 ovs = ovsrec_open_vswitch_insert(txn);
3695 if (wait_for_reload) {
3696 struct json *where = where_uuid_equals(&ovs->header_.uuid);
3697 ovsdb_idl_txn_increment(txn, "Open_vSwitch", "next_cfg", where);
3698 json_destroy(where);
3701 symtab = ovsdb_symbol_table_create();
3702 for (c = commands; c < &commands[n_commands]; c++) {
3703 ds_init(&c->output);
3706 for (c = commands; c < &commands[n_commands]; c++) {
3707 struct vsctl_context ctx;
3709 vsctl_context_init(&ctx, c, idl, txn, ovs, symtab);
3710 if (c->syntax->run) {
3711 (c->syntax->run)(&ctx);
3713 vsctl_context_done(&ctx, c);
3715 if (ctx.try_again) {
3716 status = TXN_AGAIN_WAIT;
3721 SHASH_FOR_EACH (node, &symtab->sh) {
3722 struct ovsdb_symbol *symbol = node->data;
3723 if (!symbol->created) {
3724 vsctl_fatal("row id \"%s\" is referenced but never created (e.g. "
3725 "with \"-- --id=%s create ...\")",
3726 node->name, node->name);
3728 if (!symbol->strong_ref) {
3729 if (!symbol->weak_ref) {
3730 VLOG_WARN("row id \"%s\" was created but no reference to it "
3731 "was inserted, so it will not actually appear in "
3732 "the database", node->name);
3734 VLOG_WARN("row id \"%s\" was created but only a weak "
3735 "reference to it was inserted, so it will not "
3736 "actually appear in the database", node->name);
3741 status = ovsdb_idl_txn_commit_block(txn);
3742 if (wait_for_reload && status == TXN_SUCCESS) {
3743 next_cfg = ovsdb_idl_txn_get_increment_new_value(txn);
3745 if (status == TXN_UNCHANGED || status == TXN_SUCCESS) {
3746 for (c = commands; c < &commands[n_commands]; c++) {
3747 if (c->syntax->postprocess) {
3748 struct vsctl_context ctx;
3750 vsctl_context_init(&ctx, c, idl, txn, ovs, symtab);
3751 (c->syntax->postprocess)(&ctx);
3752 vsctl_context_done(&ctx, c);
3756 error = xstrdup(ovsdb_idl_txn_get_error(txn));
3757 ovsdb_idl_txn_destroy(txn);
3758 txn = the_idl_txn = NULL;
3761 case TXN_UNCOMMITTED:
3762 case TXN_INCOMPLETE:
3766 /* Should not happen--we never call ovsdb_idl_txn_abort(). */
3767 vsctl_fatal("transaction aborted");
3773 case TXN_AGAIN_WAIT:
3778 vsctl_fatal("transaction error: %s", error);
3780 case TXN_NOT_LOCKED:
3781 /* Should not happen--we never call ovsdb_idl_set_lock(). */
3782 vsctl_fatal("database not locked");
3789 ovsdb_symbol_table_destroy(symtab);
3791 for (c = commands; c < &commands[n_commands]; c++) {
3792 struct ds *ds = &c->output;
3795 table_print(c->table, &table_style);
3796 } else if (oneline) {
3800 for (j = 0; j < ds->length; j++) {
3801 int ch = ds->string[j];
3804 fputs("\\n", stdout);
3808 fputs("\\\\", stdout);
3817 fputs(ds_cstr(ds), stdout);
3819 ds_destroy(&c->output);
3820 table_destroy(c->table);
3823 smap_destroy(&c->options);
3827 if (wait_for_reload && status != TXN_UNCHANGED) {
3830 OVSREC_OPEN_VSWITCH_FOR_EACH (ovs, idl) {
3831 if (ovs->cur_cfg >= next_cfg) {
3835 ovsdb_idl_wait(idl);
3840 ovsdb_idl_destroy(idl);
3845 /* Our transaction needs to be rerun, or a prerequisite was not met. Free
3846 * resources and return so that the caller can try again. */
3848 ovsdb_idl_txn_abort(txn);
3849 ovsdb_idl_txn_destroy(txn);
3851 ovsdb_symbol_table_destroy(symtab);
3852 for (c = commands; c < &commands[n_commands]; c++) {
3853 ds_destroy(&c->output);
3854 table_destroy(c->table);
3862 static const struct vsctl_command_syntax all_commands[] = {
3863 /* Open vSwitch commands. */
3864 {"init", 0, 0, NULL, cmd_init, NULL, "", RW},
3865 {"show", 0, 0, pre_cmd_show, cmd_show, NULL, "", RO},
3867 /* Bridge commands. */
3868 {"add-br", 1, 3, pre_get_info, cmd_add_br, NULL, "--may-exist", RW},
3869 {"del-br", 1, 1, pre_get_info, cmd_del_br, NULL, "--if-exists", RW},
3870 {"list-br", 0, 0, pre_get_info, cmd_list_br, NULL, "", RO},
3871 {"br-exists", 1, 1, pre_get_info, cmd_br_exists, NULL, "", RO},
3872 {"br-to-vlan", 1, 1, pre_get_info, cmd_br_to_vlan, NULL, "", RO},
3873 {"br-to-parent", 1, 1, pre_get_info, cmd_br_to_parent, NULL, "", RO},
3874 {"br-set-external-id", 2, 3, pre_cmd_br_set_external_id,
3875 cmd_br_set_external_id, NULL, "", RW},
3876 {"br-get-external-id", 1, 2, pre_cmd_br_get_external_id,
3877 cmd_br_get_external_id, NULL, "", RO},
3879 /* Port commands. */
3880 {"list-ports", 1, 1, pre_get_info, cmd_list_ports, NULL, "", RO},
3881 {"add-port", 2, INT_MAX, pre_get_info, cmd_add_port, NULL, "--may-exist",
3883 {"add-bond", 4, INT_MAX, pre_get_info, cmd_add_bond, NULL,
3884 "--may-exist,--fake-iface", RW},
3885 {"del-port", 1, 2, pre_get_info, cmd_del_port, NULL,
3886 "--if-exists,--with-iface", RW},
3887 {"port-to-br", 1, 1, pre_get_info, cmd_port_to_br, NULL, "", RO},
3889 /* Interface commands. */
3890 {"list-ifaces", 1, 1, pre_get_info, cmd_list_ifaces, NULL, "", RO},
3891 {"iface-to-br", 1, 1, pre_get_info, cmd_iface_to_br, NULL, "", RO},
3893 /* Controller commands. */
3894 {"get-controller", 1, 1, pre_controller, cmd_get_controller, NULL, "", RO},
3895 {"del-controller", 1, 1, pre_controller, cmd_del_controller, NULL, "", RW},
3896 {"set-controller", 1, INT_MAX, pre_controller, cmd_set_controller, NULL,
3898 {"get-fail-mode", 1, 1, pre_get_info, cmd_get_fail_mode, NULL, "", RO},
3899 {"del-fail-mode", 1, 1, pre_get_info, cmd_del_fail_mode, NULL, "", RW},
3900 {"set-fail-mode", 2, 2, pre_get_info, cmd_set_fail_mode, NULL, "", RW},
3902 /* Manager commands. */
3903 {"get-manager", 0, 0, pre_manager, cmd_get_manager, NULL, "", RO},
3904 {"del-manager", 0, INT_MAX, pre_manager, cmd_del_manager, NULL, "", RW},
3905 {"set-manager", 1, INT_MAX, pre_manager, cmd_set_manager, NULL, "", RW},
3908 {"get-ssl", 0, 0, pre_cmd_get_ssl, cmd_get_ssl, NULL, "", RO},
3909 {"del-ssl", 0, 0, pre_cmd_del_ssl, cmd_del_ssl, NULL, "", RW},
3910 {"set-ssl", 3, 3, pre_cmd_set_ssl, cmd_set_ssl, NULL, "--bootstrap", RW},
3912 /* Switch commands. */
3913 {"emer-reset", 0, 0, pre_cmd_emer_reset, cmd_emer_reset, NULL, "", RW},
3915 /* Database commands. */
3916 {"comment", 0, INT_MAX, NULL, NULL, NULL, "", RO},
3917 {"get", 2, INT_MAX, pre_cmd_get, cmd_get, NULL, "--if-exists,--id=", RO},
3918 {"list", 1, INT_MAX, pre_cmd_list, cmd_list, NULL, "--columns=", RO},
3919 {"find", 1, INT_MAX, pre_cmd_find, cmd_find, NULL, "--columns=", RO},
3920 {"set", 3, INT_MAX, pre_cmd_set, cmd_set, NULL, "", RW},
3921 {"add", 4, INT_MAX, pre_cmd_add, cmd_add, NULL, "", RW},
3922 {"remove", 4, INT_MAX, pre_cmd_remove, cmd_remove, NULL, "", RW},
3923 {"clear", 3, INT_MAX, pre_cmd_clear, cmd_clear, NULL, "", RW},
3924 {"create", 2, INT_MAX, pre_create, cmd_create, post_create, "--id=", RW},
3925 {"destroy", 1, INT_MAX, pre_cmd_destroy, cmd_destroy, NULL, "--if-exists",
3927 {"wait-until", 2, INT_MAX, pre_cmd_wait_until, cmd_wait_until, NULL, "",
3930 {NULL, 0, 0, NULL, NULL, NULL, NULL, RO},