2 * Copyright (c) 2009, 2010, 2011, 2012 Nicira, Inc.
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"
37 #include "ovsdb-data.h"
38 #include "ovsdb-idl.h"
39 #include "poll-loop.h"
42 #include "stream-ssl.h"
46 #include "lib/vswitch-idl.h"
53 VLOG_DEFINE_THIS_MODULE(vsctl);
55 /* vsctl_fatal() also logs the error, so it is preferred in this file. */
56 #define ovs_fatal please_use_vsctl_fatal_instead_of_ovs_fatal
60 /* A command supported by ovs-vsctl. */
61 struct vsctl_command_syntax {
62 const char *name; /* e.g. "add-br" */
63 int min_args; /* Min number of arguments following name. */
64 int max_args; /* Max number of arguments following name. */
66 /* If nonnull, calls ovsdb_idl_add_column() or ovsdb_idl_add_table() for
67 * each column or table in ctx->idl that it uses. */
68 void (*prerequisites)(struct vsctl_context *ctx);
70 /* Does the actual work of the command and puts the command's output, if
71 * any, in ctx->output or ctx->table.
73 * Alternatively, if some prerequisite of the command is not met and the
74 * caller should wait for something to change and then retry, it may set
75 * ctx->try_again to true. (Only the "wait-until" command currently does
77 void (*run)(struct vsctl_context *ctx);
79 /* If nonnull, called after the transaction has been successfully
80 * committed. ctx->output is the output from the "run" function, which
81 * this function may modify and otherwise postprocess as needed. (Only the
82 * "create" command currently does any postprocessing.) */
83 void (*postprocess)(struct vsctl_context *ctx);
85 /* A comma-separated list of supported options, e.g. "--a,--b", or the
86 * empty string if the command does not support any options. */
88 enum { RO, RW } mode; /* Does this command modify the database? */
91 struct vsctl_command {
92 /* Data that remains constant after initialization. */
93 const struct vsctl_command_syntax *syntax;
98 /* Data modified by commands. */
103 /* --db: The database server to contact. */
104 static const char *db;
106 /* --oneline: Write each command's output as a single line? */
109 /* --dry-run: Do not commit any changes. */
112 /* --no-wait: Wait for ovs-vswitchd to reload its configuration? */
113 static bool wait_for_reload = true;
115 /* --timeout: Time to wait for a connection to 'db'. */
118 /* Format for table output. */
119 static struct table_style table_style = TABLE_STYLE_DEFAULT;
121 /* All supported commands. */
122 static const struct vsctl_command_syntax all_commands[];
124 /* The IDL we're using and the current transaction, if any.
125 * This is for use by vsctl_exit() only, to allow it to clean up.
126 * Other code should use its context arguments. */
127 static struct ovsdb_idl *the_idl;
128 static struct ovsdb_idl_txn *the_idl_txn;
130 static void vsctl_exit(int status) NO_RETURN;
131 static void vsctl_fatal(const char *, ...) PRINTF_FORMAT(1, 2) NO_RETURN;
132 static char *default_db(void);
133 static void usage(void) NO_RETURN;
134 static void parse_options(int argc, char *argv[], struct shash *local_options);
135 static bool might_write_to_db(char **argv);
137 static struct vsctl_command *parse_commands(int argc, char *argv[],
138 struct shash *local_options,
139 size_t *n_commandsp);
140 static void parse_command(int argc, char *argv[], struct shash *local_options,
141 struct vsctl_command *);
142 static const struct vsctl_command_syntax *find_command(const char *name);
143 static void run_prerequisites(struct vsctl_command[], size_t n_commands,
145 static void do_vsctl(const char *args, struct vsctl_command *, size_t n,
148 static const struct vsctl_table_class *get_table(const char *table_name);
149 static void set_column(const struct vsctl_table_class *,
150 const struct ovsdb_idl_row *, const char *arg,
151 struct ovsdb_symbol_table *);
153 static bool is_condition_satisfied(const struct vsctl_table_class *,
154 const struct ovsdb_idl_row *,
156 struct ovsdb_symbol_table *);
159 main(int argc, char *argv[])
161 extern struct vlog_module VLM_reconnect;
162 struct ovsdb_idl *idl;
163 struct vsctl_command *commands;
164 struct shash local_options;
169 set_program_name(argv[0]);
170 signal(SIGPIPE, SIG_IGN);
171 vlog_set_levels(NULL, VLF_CONSOLE, VLL_WARN);
172 vlog_set_levels(&VLM_reconnect, VLF_ANY_FACILITY, VLL_WARN);
175 /* Log our arguments. This is often valuable for debugging systems. */
176 args = process_escape_args(argv);
177 VLOG(might_write_to_db(argv) ? VLL_INFO : VLL_DBG, "Called as %s", args);
179 /* Parse command line. */
180 shash_init(&local_options);
181 parse_options(argc, argv, &local_options);
182 commands = parse_commands(argc - optind, argv + optind, &local_options,
189 /* Initialize IDL. */
190 idl = the_idl = ovsdb_idl_create(db, &ovsrec_idl_class, false);
191 run_prerequisites(commands, n_commands, idl);
193 /* Execute the commands.
195 * 'seqno' is the database sequence number for which we last tried to
196 * execute our transaction. There's no point in trying to commit more than
197 * once for any given sequence number, because if the transaction fails
198 * it's because the database changed and we need to obtain an up-to-date
199 * view of the database before we try the transaction again. */
200 seqno = ovsdb_idl_get_seqno(idl);
204 if (seqno != ovsdb_idl_get_seqno(idl)) {
205 seqno = ovsdb_idl_get_seqno(idl);
206 do_vsctl(args, commands, n_commands, idl);
209 if (seqno == ovsdb_idl_get_seqno(idl)) {
216 static struct option *
217 find_option(const char *name, struct option *options, size_t n_options)
221 for (i = 0; i < n_options; i++) {
222 if (!strcmp(options[i].name, name)) {
229 static struct option *
230 add_option(struct option **optionsp, size_t *n_optionsp,
231 size_t *allocated_optionsp)
233 if (*n_optionsp >= *allocated_optionsp) {
234 *optionsp = x2nrealloc(*optionsp, allocated_optionsp,
237 return &(*optionsp)[(*n_optionsp)++];
241 parse_options(int argc, char *argv[], struct shash *local_options)
244 OPT_DB = UCHAR_MAX + 1,
254 static const struct option global_long_options[] = {
255 {"db", required_argument, NULL, OPT_DB},
256 {"no-syslog", no_argument, NULL, OPT_NO_SYSLOG},
257 {"no-wait", no_argument, NULL, OPT_NO_WAIT},
258 {"dry-run", no_argument, NULL, OPT_DRY_RUN},
259 {"oneline", no_argument, NULL, OPT_ONELINE},
260 {"timeout", required_argument, NULL, 't'},
261 {"help", no_argument, NULL, 'h'},
262 {"version", no_argument, NULL, 'V'},
265 STREAM_SSL_LONG_OPTIONS,
266 {"peer-ca-cert", required_argument, NULL, OPT_PEER_CA_CERT},
269 const int n_global_long_options = ARRAY_SIZE(global_long_options) - 1;
270 char *tmp, *short_options;
272 const struct vsctl_command_syntax *p;
273 struct option *options, *o;
274 size_t allocated_options;
278 tmp = long_options_to_short_options(global_long_options);
279 short_options = xasprintf("+%s", tmp);
282 /* We want to parse both global and command-specific options here, but
283 * getopt_long() isn't too convenient for the job. We copy our global
284 * options into a dynamic array, then append all of the command-specific
286 options = xmemdup(global_long_options, sizeof global_long_options);
287 allocated_options = ARRAY_SIZE(global_long_options);
288 n_options = n_global_long_options;
289 for (p = all_commands; p->name; p++) {
291 char *save_ptr = NULL;
295 s = xstrdup(p->options);
296 for (name = strtok_r(s, ",", &save_ptr); name != NULL;
297 name = strtok_r(NULL, ",", &save_ptr)) {
301 assert(name[0] == '-' && name[1] == '-' && name[2]);
304 equals = strchr(name, '=');
306 has_arg = required_argument;
309 has_arg = no_argument;
312 o = find_option(name, options, n_options);
314 assert(o - options >= n_global_long_options);
315 assert(o->has_arg == has_arg);
317 o = add_option(&options, &n_options, &allocated_options);
318 o->name = xstrdup(name);
319 o->has_arg = has_arg;
328 o = add_option(&options, &n_options, &allocated_options);
329 memset(o, 0, sizeof *o);
331 table_style.format = TF_LIST;
337 c = getopt_long(argc, argv, short_options, options, &idx);
352 vlog_set_levels(&VLM_vsctl, VLF_SYSLOG, VLL_WARN);
356 wait_for_reload = false;
364 if (shash_find(local_options, options[idx].name)) {
365 vsctl_fatal("'%s' option specified multiple times",
368 shash_add_nocopy(local_options,
369 xasprintf("--%s", options[idx].name),
370 optarg ? xstrdup(optarg) : NULL);
377 ovs_print_version(0, 0);
381 timeout = strtoul(optarg, NULL, 10);
383 vsctl_fatal("value %s on -t or --timeout is invalid",
389 TABLE_OPTION_HANDLERS(&table_style)
391 STREAM_SSL_OPTION_HANDLERS
393 case OPT_PEER_CA_CERT:
394 stream_ssl_set_peer_ca_cert_file(optarg);
410 for (i = n_global_long_options; options[i].name; i++) {
411 free(CONST_CAST(char *, options[i].name));
416 static struct vsctl_command *
417 parse_commands(int argc, char *argv[], struct shash *local_options,
420 struct vsctl_command *commands;
421 size_t n_commands, allocated_commands;
425 n_commands = allocated_commands = 0;
427 for (start = i = 0; i <= argc; i++) {
428 if (i == argc || !strcmp(argv[i], "--")) {
430 if (n_commands >= allocated_commands) {
431 struct vsctl_command *c;
433 commands = x2nrealloc(commands, &allocated_commands,
435 for (c = commands; c < &commands[n_commands]; c++) {
436 shash_moved(&c->options);
439 parse_command(i - start, &argv[start], local_options,
440 &commands[n_commands++]);
441 } else if (!shash_is_empty(local_options)) {
442 vsctl_fatal("missing command name (use --help for help)");
448 vsctl_fatal("missing command name (use --help for help)");
450 *n_commandsp = n_commands;
455 parse_command(int argc, char *argv[], struct shash *local_options,
456 struct vsctl_command *command)
458 const struct vsctl_command_syntax *p;
459 struct shash_node *node;
463 shash_init(&command->options);
464 shash_swap(local_options, &command->options);
465 for (i = 0; i < argc; i++) {
466 const char *option = argv[i];
470 if (option[0] != '-') {
474 equals = strchr(option, '=');
476 key = xmemdup0(option, equals - option);
477 value = xstrdup(equals + 1);
479 key = xstrdup(option);
483 if (shash_find(&command->options, key)) {
484 vsctl_fatal("'%s' option specified multiple times", argv[i]);
486 shash_add_nocopy(&command->options, key, value);
489 vsctl_fatal("missing command name (use --help for help)");
492 p = find_command(argv[i]);
494 vsctl_fatal("unknown command '%s'; use --help for help", argv[i]);
497 SHASH_FOR_EACH (node, &command->options) {
498 const char *s = strstr(p->options, node->name);
499 int end = s ? s[strlen(node->name)] : EOF;
501 if (end != '=' && end != ',' && end != ' ' && end != '\0') {
502 vsctl_fatal("'%s' command has no '%s' option",
503 argv[i], node->name);
505 if ((end == '=') != (node->data != NULL)) {
507 vsctl_fatal("missing argument to '%s' option on '%s' "
508 "command", node->name, argv[i]);
510 vsctl_fatal("'%s' option on '%s' does not accept an "
511 "argument", node->name, argv[i]);
516 n_arg = argc - i - 1;
517 if (n_arg < p->min_args) {
518 vsctl_fatal("'%s' command requires at least %d arguments",
519 p->name, p->min_args);
520 } else if (n_arg > p->max_args) {
523 for (j = i + 1; j < argc; j++) {
524 if (argv[j][0] == '-') {
525 vsctl_fatal("'%s' command takes at most %d arguments "
526 "(note that options must precede command "
527 "names and follow a \"--\" argument)",
528 p->name, p->max_args);
532 vsctl_fatal("'%s' command takes at most %d arguments",
533 p->name, p->max_args);
537 command->argc = n_arg + 1;
538 command->argv = &argv[i];
541 /* Returns the "struct vsctl_command_syntax" for a given command 'name', or a
542 * null pointer if there is none. */
543 static const struct vsctl_command_syntax *
544 find_command(const char *name)
546 static struct shash commands = SHASH_INITIALIZER(&commands);
548 if (shash_is_empty(&commands)) {
549 const struct vsctl_command_syntax *p;
551 for (p = all_commands; p->name; p++) {
552 shash_add_assert(&commands, p->name, p);
556 return shash_find_data(&commands, name);
560 vsctl_fatal(const char *format, ...)
565 va_start(args, format);
566 message = xvasprintf(format, args);
569 vlog_set_levels(&VLM_vsctl, VLF_CONSOLE, VLL_OFF);
570 VLOG_ERR("%s", message);
571 ovs_error(0, "%s", message);
572 vsctl_exit(EXIT_FAILURE);
575 /* Frees the current transaction and the underlying IDL and then calls
578 * Freeing the transaction and the IDL is not strictly necessary, but it makes
579 * for a clean memory leak report from valgrind in the normal case. That makes
580 * it easier to notice real memory leaks. */
582 vsctl_exit(int status)
585 ovsdb_idl_txn_abort(the_idl_txn);
586 ovsdb_idl_txn_destroy(the_idl_txn);
588 ovsdb_idl_destroy(the_idl);
596 %s: ovs-vswitchd management utility\n\
597 usage: %s [OPTIONS] COMMAND [ARG...]\n\
599 Open vSwitch commands:\n\
600 init initialize database, if not yet initialized\n\
601 show print overview of database contents\n\
602 emer-reset reset configuration to clean state\n\
605 add-br BRIDGE create a new bridge named BRIDGE\n\
606 add-br BRIDGE PARENT VLAN create new fake BRIDGE in PARENT on VLAN\n\
607 del-br BRIDGE delete BRIDGE and all of its ports\n\
608 list-br print the names of all the bridges\n\
609 br-exists BRIDGE exit 2 if BRIDGE does not exist\n\
610 br-to-vlan BRIDGE print the VLAN which BRIDGE is on\n\
611 br-to-parent BRIDGE print the parent of BRIDGE\n\
612 br-set-external-id BRIDGE KEY VALUE set KEY on BRIDGE to VALUE\n\
613 br-set-external-id BRIDGE KEY unset KEY on BRIDGE\n\
614 br-get-external-id BRIDGE KEY print value of KEY on BRIDGE\n\
615 br-get-external-id BRIDGE list key-value pairs on BRIDGE\n\
617 Port commands (a bond is considered to be a single port):\n\
618 list-ports BRIDGE print the names of all the ports on BRIDGE\n\
619 add-port BRIDGE PORT add network device PORT to BRIDGE\n\
620 add-bond BRIDGE PORT IFACE... add bonded port PORT in BRIDGE from IFACES\n\
621 del-port [BRIDGE] PORT delete PORT (which may be bonded) from BRIDGE\n\
622 port-to-br PORT print name of bridge that contains PORT\n\
624 Interface commands (a bond consists of multiple interfaces):\n\
625 list-ifaces BRIDGE print the names of all interfaces on BRIDGE\n\
626 iface-to-br IFACE print name of bridge that contains IFACE\n\
628 Controller commands:\n\
629 get-controller BRIDGE print the controllers for BRIDGE\n\
630 del-controller BRIDGE delete the controllers for BRIDGE\n\
631 set-controller BRIDGE TARGET... set the controllers for BRIDGE\n\
632 get-fail-mode BRIDGE print the fail-mode for BRIDGE\n\
633 del-fail-mode BRIDGE delete the fail-mode for BRIDGE\n\
634 set-fail-mode BRIDGE MODE set the fail-mode for BRIDGE to MODE\n\
637 get-manager print the managers\n\
638 del-manager delete the managers\n\
639 set-manager TARGET... set the list of managers to TARGET...\n\
642 get-ssl print the SSL configuration\n\
643 del-ssl delete the SSL configuration\n\
644 set-ssl PRIV-KEY CERT CA-CERT set the SSL configuration\n\
647 emer-reset reset switch to known good state\n\
649 Database commands:\n\
650 list TBL [REC] list RECord (or all records) in TBL\n\
651 find TBL CONDITION... list records satisfying CONDITION in TBL\n\
652 get TBL REC COL[:KEY] print values of COLumns in RECord in TBL\n\
653 set TBL REC COL[:KEY]=VALUE set COLumn values in RECord in TBL\n\
654 add TBL REC COL [KEY=]VALUE add (KEY=)VALUE to COLumn in RECord in TBL\n\
655 remove TBL REC COL [KEY=]VALUE remove (KEY=)VALUE from COLumn\n\
656 clear TBL REC COL clear values from COLumn in RECord in TBL\n\
657 create TBL COL[:KEY]=VALUE create and initialize new record\n\
658 destroy TBL REC delete RECord from TBL\n\
659 wait-until TBL REC [COL[:KEY]=VALUE] wait until condition is true\n\
660 Potentially unsafe database commands require --force option.\n\
663 --db=DATABASE connect to DATABASE\n\
665 --no-wait do not wait for ovs-vswitchd to reconfigure\n\
666 -t, --timeout=SECS wait at most SECS seconds for ovs-vswitchd\n\
667 --dry-run do not commit changes to database\n\
668 --oneline print exactly one line of output per command\n",
669 program_name, program_name, default_db());
672 --no-syslog equivalent to --verbose=vsctl:syslog:warn\n");
673 stream_usage("database", true, true, false);
676 -h, --help display this help message\n\
677 -V, --version display version information\n");
686 def = xasprintf("unix:%s/db.sock", ovs_rundir());
691 /* Returns true if it looks like this set of arguments might modify the
692 * database, otherwise false. (Not very smart, so it's prone to false
695 might_write_to_db(char **argv)
697 for (; *argv; argv++) {
698 const struct vsctl_command_syntax *p = find_command(*argv);
699 if (p && p->mode == RW) {
706 struct vsctl_context {
710 struct shash options;
712 /* Modifiable state. */
715 struct ovsdb_idl *idl;
716 struct ovsdb_idl_txn *txn;
717 struct ovsdb_symbol_table *symtab;
718 const struct ovsrec_open_vswitch *ovs;
721 /* A cache of the contents of the database.
723 * A command that needs to use any of this information must first call
724 * vsctl_context_populate_cache(). A command that changes anything that
725 * could invalidate the cache must either call
726 * vsctl_context_invalidate_cache() or manually update the cache to
727 * maintain its correctness. */
729 struct shash bridges; /* Maps from bridge name to struct vsctl_bridge. */
730 struct shash ports; /* Maps from port name to struct vsctl_port. */
731 struct shash ifaces; /* Maps from port name to struct vsctl_iface. */
733 /* A command may set this member to true if some prerequisite is not met
734 * and the caller should wait for something to change and then retry. */
738 struct vsctl_bridge {
739 struct ovsrec_bridge *br_cfg;
741 struct list ports; /* Contains "struct vsctl_port"s. */
743 /* VLAN ("fake") bridge support.
745 * Use 'parent != NULL' to detect a fake bridge, because 'vlan' can be 0
747 struct hmap children; /* VLAN bridges indexed by 'vlan'. */
748 struct hmap_node children_node; /* Node in parent's 'children' hmap. */
749 struct vsctl_bridge *parent; /* Real bridge, or NULL. */
750 int vlan; /* VLAN VID (0...4095), or 0. */
754 struct list ports_node; /* In struct vsctl_bridge's 'ports' list. */
755 struct list ifaces; /* Contains "struct vsctl_iface"s. */
756 struct ovsrec_port *port_cfg;
757 struct vsctl_bridge *bridge;
761 struct list ifaces_node; /* In struct vsctl_port's 'ifaces' list. */
762 struct ovsrec_interface *iface_cfg;
763 struct vsctl_port *port;
767 vsctl_context_to_string(const struct vsctl_context *ctx)
769 const struct shash_node *node;
775 SHASH_FOR_EACH (node, &ctx->options) {
776 svec_add(&words, node->name);
778 for (i = 0; i < ctx->argc; i++) {
779 svec_add(&words, ctx->argv[i]);
781 svec_terminate(&words);
783 s = process_escape_args(words.names);
785 svec_destroy(&words);
791 verify_ports(struct vsctl_context *ctx)
793 if (!ctx->verified_ports) {
794 const struct ovsrec_bridge *bridge;
795 const struct ovsrec_port *port;
797 ovsrec_open_vswitch_verify_bridges(ctx->ovs);
798 OVSREC_BRIDGE_FOR_EACH (bridge, ctx->idl) {
799 ovsrec_bridge_verify_ports(bridge);
801 OVSREC_PORT_FOR_EACH (port, ctx->idl) {
802 ovsrec_port_verify_interfaces(port);
805 ctx->verified_ports = true;
809 static struct vsctl_bridge *
810 add_bridge_to_cache(struct vsctl_context *ctx,
811 struct ovsrec_bridge *br_cfg, const char *name,
812 struct vsctl_bridge *parent, int vlan)
814 struct vsctl_bridge *br = xmalloc(sizeof *br);
816 br->name = xstrdup(name);
817 list_init(&br->ports);
820 hmap_init(&br->children);
822 hmap_insert(&parent->children, &br->children_node, hash_int(vlan, 0));
824 shash_add(&ctx->bridges, br->name, br);
829 ovs_delete_bridge(const struct ovsrec_open_vswitch *ovs,
830 struct ovsrec_bridge *bridge)
832 struct ovsrec_bridge **bridges;
835 bridges = xmalloc(sizeof *ovs->bridges * ovs->n_bridges);
836 for (i = n = 0; i < ovs->n_bridges; i++) {
837 if (ovs->bridges[i] != bridge) {
838 bridges[n++] = ovs->bridges[i];
841 ovsrec_open_vswitch_set_bridges(ovs, bridges, n);
846 del_cached_bridge(struct vsctl_context *ctx, struct vsctl_bridge *br)
848 assert(list_is_empty(&br->ports));
849 assert(hmap_is_empty(&br->children));
851 hmap_remove(&br->parent->children, &br->children_node);
854 ovsrec_bridge_delete(br->br_cfg);
855 ovs_delete_bridge(ctx->ovs, br->br_cfg);
857 shash_find_and_delete(&ctx->bridges, br->name);
858 hmap_destroy(&br->children);
864 port_is_fake_bridge(const struct ovsrec_port *port_cfg)
866 return (port_cfg->fake_bridge
868 && *port_cfg->tag >= 0 && *port_cfg->tag <= 4095);
871 static struct vsctl_bridge *
872 find_vlan_bridge(struct vsctl_bridge *parent, int vlan)
874 struct vsctl_bridge *child;
876 HMAP_FOR_EACH_IN_BUCKET (child, children_node, hash_int(vlan, 0),
878 if (child->vlan == vlan) {
886 static struct vsctl_port *
887 add_port_to_cache(struct vsctl_context *ctx, struct vsctl_bridge *parent,
888 struct ovsrec_port *port_cfg)
890 struct vsctl_port *port;
893 && *port_cfg->tag >= 0 && *port_cfg->tag <= 4095) {
894 struct vsctl_bridge *vlan_bridge;
896 vlan_bridge = find_vlan_bridge(parent, *port_cfg->tag);
898 parent = vlan_bridge;
902 port = xmalloc(sizeof *port);
903 list_push_back(&parent->ports, &port->ports_node);
904 list_init(&port->ifaces);
905 port->port_cfg = port_cfg;
906 port->bridge = parent;
907 shash_add(&ctx->ports, port_cfg->name, port);
913 del_cached_port(struct vsctl_context *ctx, struct vsctl_port *port)
915 assert(list_is_empty(&port->ifaces));
916 list_remove(&port->ports_node);
917 shash_find_and_delete(&ctx->ports, port->port_cfg->name);
918 ovsrec_port_delete(port->port_cfg);
922 static struct vsctl_iface *
923 add_iface_to_cache(struct vsctl_context *ctx, struct vsctl_port *parent,
924 struct ovsrec_interface *iface_cfg)
926 struct vsctl_iface *iface;
928 iface = xmalloc(sizeof *iface);
929 list_push_back(&parent->ifaces, &iface->ifaces_node);
930 iface->iface_cfg = iface_cfg;
931 iface->port = parent;
932 shash_add(&ctx->ifaces, iface_cfg->name, iface);
938 del_cached_iface(struct vsctl_context *ctx, struct vsctl_iface *iface)
940 list_remove(&iface->ifaces_node);
941 shash_find_and_delete(&ctx->ifaces, iface->iface_cfg->name);
942 ovsrec_interface_delete(iface->iface_cfg);
947 vsctl_context_invalidate_cache(struct vsctl_context *ctx)
949 struct shash_node *node;
951 if (!ctx->cache_valid) {
954 ctx->cache_valid = false;
956 SHASH_FOR_EACH (node, &ctx->bridges) {
957 struct vsctl_bridge *bridge = node->data;
958 hmap_destroy(&bridge->children);
962 shash_destroy(&ctx->bridges);
964 shash_destroy_free_data(&ctx->ports);
965 shash_destroy_free_data(&ctx->ifaces);
969 pre_get_info(struct vsctl_context *ctx)
971 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_bridges);
973 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_name);
974 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_controller);
975 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_fail_mode);
976 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_ports);
978 ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_name);
979 ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_fake_bridge);
980 ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_tag);
981 ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_interfaces);
983 ovsdb_idl_add_column(ctx->idl, &ovsrec_interface_col_name);
987 vsctl_context_populate_cache(struct vsctl_context *ctx)
989 const struct ovsrec_open_vswitch *ovs = ctx->ovs;
990 struct sset bridges, ports;
993 if (ctx->cache_valid) {
994 /* Cache is already populated. */
997 ctx->cache_valid = true;
998 shash_init(&ctx->bridges);
999 shash_init(&ctx->ports);
1000 shash_init(&ctx->ifaces);
1002 sset_init(&bridges);
1004 for (i = 0; i < ovs->n_bridges; i++) {
1005 struct ovsrec_bridge *br_cfg = ovs->bridges[i];
1006 struct vsctl_bridge *br;
1009 if (!sset_add(&bridges, br_cfg->name)) {
1010 VLOG_WARN("%s: database contains duplicate bridge name",
1014 br = add_bridge_to_cache(ctx, br_cfg, br_cfg->name, NULL, 0);
1019 for (j = 0; j < br_cfg->n_ports; j++) {
1020 struct ovsrec_port *port_cfg = br_cfg->ports[j];
1022 if (!sset_add(&ports, port_cfg->name)) {
1023 /* Duplicate port name. (We will warn about that later.) */
1027 if (port_is_fake_bridge(port_cfg)
1028 && sset_add(&bridges, port_cfg->name)) {
1029 add_bridge_to_cache(ctx, NULL, port_cfg->name, br,
1034 sset_destroy(&bridges);
1035 sset_destroy(&ports);
1037 sset_init(&bridges);
1038 for (i = 0; i < ovs->n_bridges; i++) {
1039 struct ovsrec_bridge *br_cfg = ovs->bridges[i];
1040 struct vsctl_bridge *br;
1043 if (!sset_add(&bridges, br_cfg->name)) {
1046 br = shash_find_data(&ctx->bridges, br_cfg->name);
1047 for (j = 0; j < br_cfg->n_ports; j++) {
1048 struct ovsrec_port *port_cfg = br_cfg->ports[j];
1049 struct vsctl_port *port;
1052 port = shash_find_data(&ctx->ports, port_cfg->name);
1054 if (port_cfg == port->port_cfg) {
1055 VLOG_WARN("%s: port is in multiple bridges (%s and %s)",
1056 port_cfg->name, br->name, port->bridge->name);
1058 /* Log as an error because this violates the database's
1059 * uniqueness constraints, so the database server shouldn't
1060 * have allowed it. */
1061 VLOG_ERR("%s: database contains duplicate port name",
1067 if (port_is_fake_bridge(port_cfg)
1068 && !sset_add(&bridges, port_cfg->name)) {
1072 port = add_port_to_cache(ctx, br, port_cfg);
1073 for (k = 0; k < port_cfg->n_interfaces; k++) {
1074 struct ovsrec_interface *iface_cfg = port_cfg->interfaces[k];
1075 struct vsctl_iface *iface;
1077 iface = shash_find_data(&ctx->ifaces, iface_cfg->name);
1079 if (iface_cfg == iface->iface_cfg) {
1080 VLOG_WARN("%s: interface is in multiple ports "
1083 iface->port->port_cfg->name,
1084 port->port_cfg->name);
1086 /* Log as an error because this violates the database's
1087 * uniqueness constraints, so the database server
1088 * shouldn't have allowed it. */
1089 VLOG_ERR("%s: database contains duplicate interface "
1090 "name", iface_cfg->name);
1095 add_iface_to_cache(ctx, port, iface_cfg);
1099 sset_destroy(&bridges);
1103 check_conflicts(struct vsctl_context *ctx, const char *name,
1106 struct vsctl_iface *iface;
1107 struct vsctl_port *port;
1111 if (shash_find(&ctx->bridges, name)) {
1112 vsctl_fatal("%s because a bridge named %s already exists",
1116 port = shash_find_data(&ctx->ports, name);
1118 vsctl_fatal("%s because a port named %s already exists on "
1119 "bridge %s", msg, name, port->bridge->name);
1122 iface = shash_find_data(&ctx->ifaces, name);
1124 vsctl_fatal("%s because an interface named %s already exists "
1125 "on bridge %s", msg, name, iface->port->bridge->name);
1131 static struct vsctl_bridge *
1132 find_bridge(struct vsctl_context *ctx, const char *name, bool must_exist)
1134 struct vsctl_bridge *br;
1136 assert(ctx->cache_valid);
1138 br = shash_find_data(&ctx->bridges, name);
1139 if (must_exist && !br) {
1140 vsctl_fatal("no bridge named %s", name);
1142 ovsrec_open_vswitch_verify_bridges(ctx->ovs);
1146 static struct vsctl_bridge *
1147 find_real_bridge(struct vsctl_context *ctx, const char *name, bool must_exist)
1149 struct vsctl_bridge *br = find_bridge(ctx, name, must_exist);
1150 if (br && br->parent) {
1151 vsctl_fatal("%s is a fake bridge", name);
1156 static struct vsctl_port *
1157 find_port(struct vsctl_context *ctx, const char *name, bool must_exist)
1159 struct vsctl_port *port;
1161 assert(ctx->cache_valid);
1163 port = shash_find_data(&ctx->ports, name);
1164 if (port && !strcmp(name, port->bridge->name)) {
1167 if (must_exist && !port) {
1168 vsctl_fatal("no port named %s", name);
1174 static struct vsctl_iface *
1175 find_iface(struct vsctl_context *ctx, const char *name, bool must_exist)
1177 struct vsctl_iface *iface;
1179 assert(ctx->cache_valid);
1181 iface = shash_find_data(&ctx->ifaces, name);
1182 if (iface && !strcmp(name, iface->port->bridge->name)) {
1185 if (must_exist && !iface) {
1186 vsctl_fatal("no interface named %s", name);
1193 bridge_insert_port(struct ovsrec_bridge *br, struct ovsrec_port *port)
1195 struct ovsrec_port **ports;
1198 ports = xmalloc(sizeof *br->ports * (br->n_ports + 1));
1199 for (i = 0; i < br->n_ports; i++) {
1200 ports[i] = br->ports[i];
1202 ports[br->n_ports] = port;
1203 ovsrec_bridge_set_ports(br, ports, br->n_ports + 1);
1208 bridge_delete_port(struct ovsrec_bridge *br, struct ovsrec_port *port)
1210 struct ovsrec_port **ports;
1213 ports = xmalloc(sizeof *br->ports * br->n_ports);
1214 for (i = n = 0; i < br->n_ports; i++) {
1215 if (br->ports[i] != port) {
1216 ports[n++] = br->ports[i];
1219 ovsrec_bridge_set_ports(br, ports, n);
1224 ovs_insert_bridge(const struct ovsrec_open_vswitch *ovs,
1225 struct ovsrec_bridge *bridge)
1227 struct ovsrec_bridge **bridges;
1230 bridges = xmalloc(sizeof *ovs->bridges * (ovs->n_bridges + 1));
1231 for (i = 0; i < ovs->n_bridges; i++) {
1232 bridges[i] = ovs->bridges[i];
1234 bridges[ovs->n_bridges] = bridge;
1235 ovsrec_open_vswitch_set_bridges(ovs, bridges, ovs->n_bridges + 1);
1240 cmd_init(struct vsctl_context *ctx OVS_UNUSED)
1244 struct cmd_show_table {
1245 const struct ovsdb_idl_table_class *table;
1246 const struct ovsdb_idl_column *name_column;
1247 const struct ovsdb_idl_column *columns[3];
1251 static struct cmd_show_table cmd_show_tables[] = {
1252 {&ovsrec_table_open_vswitch,
1254 {&ovsrec_open_vswitch_col_manager_options,
1255 &ovsrec_open_vswitch_col_bridges,
1256 &ovsrec_open_vswitch_col_ovs_version},
1259 {&ovsrec_table_bridge,
1260 &ovsrec_bridge_col_name,
1261 {&ovsrec_bridge_col_controller,
1262 &ovsrec_bridge_col_fail_mode,
1263 &ovsrec_bridge_col_ports},
1266 {&ovsrec_table_port,
1267 &ovsrec_port_col_name,
1268 {&ovsrec_port_col_tag,
1269 &ovsrec_port_col_trunks,
1270 &ovsrec_port_col_interfaces},
1273 {&ovsrec_table_interface,
1274 &ovsrec_interface_col_name,
1275 {&ovsrec_interface_col_type,
1276 &ovsrec_interface_col_options,
1280 {&ovsrec_table_controller,
1281 &ovsrec_controller_col_target,
1282 {&ovsrec_controller_col_is_connected,
1287 {&ovsrec_table_manager,
1288 &ovsrec_manager_col_target,
1289 {&ovsrec_manager_col_is_connected,
1296 pre_cmd_show(struct vsctl_context *ctx)
1298 struct cmd_show_table *show;
1300 for (show = cmd_show_tables;
1301 show < &cmd_show_tables[ARRAY_SIZE(cmd_show_tables)];
1305 ovsdb_idl_add_table(ctx->idl, show->table);
1306 if (show->name_column) {
1307 ovsdb_idl_add_column(ctx->idl, show->name_column);
1309 for (i = 0; i < ARRAY_SIZE(show->columns); i++) {
1310 const struct ovsdb_idl_column *column = show->columns[i];
1312 ovsdb_idl_add_column(ctx->idl, column);
1318 static struct cmd_show_table *
1319 cmd_show_find_table_by_row(const struct ovsdb_idl_row *row)
1321 struct cmd_show_table *show;
1323 for (show = cmd_show_tables;
1324 show < &cmd_show_tables[ARRAY_SIZE(cmd_show_tables)];
1326 if (show->table == row->table->class) {
1333 static struct cmd_show_table *
1334 cmd_show_find_table_by_name(const char *name)
1336 struct cmd_show_table *show;
1338 for (show = cmd_show_tables;
1339 show < &cmd_show_tables[ARRAY_SIZE(cmd_show_tables)];
1341 if (!strcmp(show->table->name, name)) {
1349 cmd_show_row(struct vsctl_context *ctx, const struct ovsdb_idl_row *row,
1352 struct cmd_show_table *show = cmd_show_find_table_by_row(row);
1355 ds_put_char_multiple(&ctx->output, ' ', level * 4);
1356 if (show && show->name_column) {
1357 const struct ovsdb_datum *datum;
1359 ds_put_format(&ctx->output, "%s ", show->table->name);
1360 datum = ovsdb_idl_read(row, show->name_column);
1361 ovsdb_datum_to_string(datum, &show->name_column->type, &ctx->output);
1363 ds_put_format(&ctx->output, UUID_FMT, UUID_ARGS(&row->uuid));
1365 ds_put_char(&ctx->output, '\n');
1367 if (!show || show->recurse) {
1371 show->recurse = true;
1372 for (i = 0; i < ARRAY_SIZE(show->columns); i++) {
1373 const struct ovsdb_idl_column *column = show->columns[i];
1374 const struct ovsdb_datum *datum;
1380 datum = ovsdb_idl_read(row, column);
1381 if (column->type.key.type == OVSDB_TYPE_UUID &&
1382 column->type.key.u.uuid.refTableName) {
1383 struct cmd_show_table *ref_show;
1386 ref_show = cmd_show_find_table_by_name(
1387 column->type.key.u.uuid.refTableName);
1389 for (j = 0; j < datum->n; j++) {
1390 const struct ovsdb_idl_row *ref_row;
1392 ref_row = ovsdb_idl_get_row_for_uuid(ctx->idl,
1394 &datum->keys[j].uuid);
1396 cmd_show_row(ctx, ref_row, level + 1);
1403 if (!ovsdb_datum_is_default(datum, &column->type)) {
1404 ds_put_char_multiple(&ctx->output, ' ', (level + 1) * 4);
1405 ds_put_format(&ctx->output, "%s: ", column->name);
1406 ovsdb_datum_to_string(datum, &column->type, &ctx->output);
1407 ds_put_char(&ctx->output, '\n');
1410 show->recurse = false;
1414 cmd_show(struct vsctl_context *ctx)
1416 const struct ovsdb_idl_row *row;
1418 for (row = ovsdb_idl_first_row(ctx->idl, cmd_show_tables[0].table);
1419 row; row = ovsdb_idl_next_row(row)) {
1420 cmd_show_row(ctx, row, 0);
1425 pre_cmd_emer_reset(struct vsctl_context *ctx)
1427 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_manager_options);
1428 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_ssl);
1430 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_controller);
1431 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_fail_mode);
1432 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_mirrors);
1433 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_netflow);
1434 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_sflow);
1435 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_flood_vlans);
1436 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_other_config);
1438 ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_other_config);
1440 ovsdb_idl_add_column(ctx->idl,
1441 &ovsrec_interface_col_ingress_policing_rate);
1442 ovsdb_idl_add_column(ctx->idl,
1443 &ovsrec_interface_col_ingress_policing_burst);
1447 cmd_emer_reset(struct vsctl_context *ctx)
1449 const struct ovsdb_idl *idl = ctx->idl;
1450 const struct ovsrec_bridge *br;
1451 const struct ovsrec_port *port;
1452 const struct ovsrec_interface *iface;
1453 const struct ovsrec_mirror *mirror, *next_mirror;
1454 const struct ovsrec_controller *ctrl, *next_ctrl;
1455 const struct ovsrec_manager *mgr, *next_mgr;
1456 const struct ovsrec_netflow *nf, *next_nf;
1457 const struct ovsrec_ssl *ssl, *next_ssl;
1458 const struct ovsrec_sflow *sflow, *next_sflow;
1460 /* Reset the Open_vSwitch table. */
1461 ovsrec_open_vswitch_set_manager_options(ctx->ovs, NULL, 0);
1462 ovsrec_open_vswitch_set_ssl(ctx->ovs, NULL);
1464 OVSREC_BRIDGE_FOR_EACH (br, idl) {
1467 ovsrec_bridge_set_controller(br, NULL, 0);
1468 ovsrec_bridge_set_fail_mode(br, NULL);
1469 ovsrec_bridge_set_mirrors(br, NULL, 0);
1470 ovsrec_bridge_set_netflow(br, NULL);
1471 ovsrec_bridge_set_sflow(br, NULL);
1472 ovsrec_bridge_set_flood_vlans(br, NULL, 0);
1474 /* We only want to save the "hwaddr" key from other_config. */
1475 hwaddr = smap_get(&br->other_config, "hwaddr");
1477 struct smap smap = SMAP_INITIALIZER(&smap);
1478 smap_add(&smap, "hwaddr", hwaddr);
1479 ovsrec_bridge_set_other_config(br, &smap);
1480 smap_destroy(&smap);
1482 ovsrec_bridge_set_other_config(br, NULL);
1486 OVSREC_PORT_FOR_EACH (port, idl) {
1487 ovsrec_port_set_other_config(port, NULL);
1490 OVSREC_INTERFACE_FOR_EACH (iface, idl) {
1491 /* xxx What do we do about gre/patch devices created by mgr? */
1493 ovsrec_interface_set_ingress_policing_rate(iface, 0);
1494 ovsrec_interface_set_ingress_policing_burst(iface, 0);
1497 OVSREC_MIRROR_FOR_EACH_SAFE (mirror, next_mirror, idl) {
1498 ovsrec_mirror_delete(mirror);
1501 OVSREC_CONTROLLER_FOR_EACH_SAFE (ctrl, next_ctrl, idl) {
1502 ovsrec_controller_delete(ctrl);
1505 OVSREC_MANAGER_FOR_EACH_SAFE (mgr, next_mgr, idl) {
1506 ovsrec_manager_delete(mgr);
1509 OVSREC_NETFLOW_FOR_EACH_SAFE (nf, next_nf, idl) {
1510 ovsrec_netflow_delete(nf);
1513 OVSREC_SSL_FOR_EACH_SAFE (ssl, next_ssl, idl) {
1514 ovsrec_ssl_delete(ssl);
1517 OVSREC_SFLOW_FOR_EACH_SAFE (sflow, next_sflow, idl) {
1518 ovsrec_sflow_delete(sflow);
1521 vsctl_context_invalidate_cache(ctx);
1525 cmd_add_br(struct vsctl_context *ctx)
1527 bool may_exist = shash_find(&ctx->options, "--may-exist") != NULL;
1528 const char *br_name, *parent_name;
1531 br_name = ctx->argv[1];
1532 if (ctx->argc == 2) {
1535 } else if (ctx->argc == 4) {
1536 parent_name = ctx->argv[2];
1537 vlan = atoi(ctx->argv[3]);
1538 if (vlan < 0 || vlan > 4095) {
1539 vsctl_fatal("%s: vlan must be between 0 and 4095", ctx->argv[0]);
1542 vsctl_fatal("'%s' command takes exactly 1 or 3 arguments",
1546 vsctl_context_populate_cache(ctx);
1548 struct vsctl_bridge *br;
1550 br = find_bridge(ctx, br_name, false);
1554 vsctl_fatal("\"--may-exist add-br %s\" but %s is "
1555 "a VLAN bridge for VLAN %d",
1556 br_name, br_name, br->vlan);
1560 vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
1561 "is not a VLAN bridge",
1562 br_name, parent_name, vlan, br_name);
1563 } else if (strcmp(br->parent->name, parent_name)) {
1564 vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
1565 "has the wrong parent %s",
1566 br_name, parent_name, vlan,
1567 br_name, br->parent->name);
1568 } else if (br->vlan != vlan) {
1569 vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
1570 "is a VLAN bridge for the wrong VLAN %d",
1571 br_name, parent_name, vlan, br_name, br->vlan);
1577 check_conflicts(ctx, br_name,
1578 xasprintf("cannot create a bridge named %s", br_name));
1581 struct ovsrec_port *port;
1582 struct ovsrec_interface *iface;
1583 struct ovsrec_bridge *br;
1585 iface = ovsrec_interface_insert(ctx->txn);
1586 ovsrec_interface_set_name(iface, br_name);
1587 ovsrec_interface_set_type(iface, "internal");
1589 port = ovsrec_port_insert(ctx->txn);
1590 ovsrec_port_set_name(port, br_name);
1591 ovsrec_port_set_interfaces(port, &iface, 1);
1593 br = ovsrec_bridge_insert(ctx->txn);
1594 ovsrec_bridge_set_name(br, br_name);
1595 ovsrec_bridge_set_ports(br, &port, 1);
1597 ovs_insert_bridge(ctx->ovs, br);
1599 struct vsctl_bridge *parent;
1600 struct ovsrec_port *port;
1601 struct ovsrec_interface *iface;
1602 struct ovsrec_bridge *br;
1605 parent = find_bridge(ctx, parent_name, false);
1606 if (parent && parent->parent) {
1607 vsctl_fatal("cannot create bridge with fake bridge as parent");
1610 vsctl_fatal("parent bridge %s does not exist", parent_name);
1612 br = parent->br_cfg;
1614 iface = ovsrec_interface_insert(ctx->txn);
1615 ovsrec_interface_set_name(iface, br_name);
1616 ovsrec_interface_set_type(iface, "internal");
1618 port = ovsrec_port_insert(ctx->txn);
1619 ovsrec_port_set_name(port, br_name);
1620 ovsrec_port_set_interfaces(port, &iface, 1);
1621 ovsrec_port_set_fake_bridge(port, true);
1622 ovsrec_port_set_tag(port, &tag, 1);
1624 bridge_insert_port(br, port);
1627 vsctl_context_invalidate_cache(ctx);
1631 del_port(struct vsctl_context *ctx, struct vsctl_port *port)
1633 struct vsctl_iface *iface, *next_iface;
1635 bridge_delete_port((port->bridge->parent
1636 ? port->bridge->parent->br_cfg
1637 : port->bridge->br_cfg), port->port_cfg);
1639 LIST_FOR_EACH_SAFE (iface, next_iface, ifaces_node, &port->ifaces) {
1640 del_cached_iface(ctx, iface);
1642 del_cached_port(ctx, port);
1646 del_bridge(struct vsctl_context *ctx, struct vsctl_bridge *br)
1648 struct vsctl_bridge *child, *next_child;
1649 struct vsctl_port *port, *next_port;
1651 HMAP_FOR_EACH_SAFE (child, next_child, children_node, &br->children) {
1652 del_bridge(ctx, child);
1655 LIST_FOR_EACH_SAFE (port, next_port, ports_node, &br->ports) {
1656 del_port(ctx, port);
1659 del_cached_bridge(ctx, br);
1663 cmd_del_br(struct vsctl_context *ctx)
1665 bool must_exist = !shash_find(&ctx->options, "--if-exists");
1666 struct vsctl_bridge *bridge;
1668 vsctl_context_populate_cache(ctx);
1669 bridge = find_bridge(ctx, ctx->argv[1], must_exist);
1671 del_bridge(ctx, bridge);
1676 output_sorted(struct svec *svec, struct ds *output)
1682 SVEC_FOR_EACH (i, name, svec) {
1683 ds_put_format(output, "%s\n", name);
1688 cmd_list_br(struct vsctl_context *ctx)
1690 struct shash_node *node;
1691 struct svec bridges;
1692 bool real = shash_find(&ctx->options, "--real");
1693 bool fake = shash_find(&ctx->options, "--fake");
1695 /* If neither fake nor real were requested, return both. */
1696 if (!real && !fake) {
1700 vsctl_context_populate_cache(ctx);
1702 svec_init(&bridges);
1703 SHASH_FOR_EACH (node, &ctx->bridges) {
1704 struct vsctl_bridge *br = node->data;
1706 if (br->parent ? fake : real) {
1707 svec_add(&bridges, br->name);
1710 output_sorted(&bridges, &ctx->output);
1711 svec_destroy(&bridges);
1715 cmd_br_exists(struct vsctl_context *ctx)
1717 vsctl_context_populate_cache(ctx);
1718 if (!find_bridge(ctx, ctx->argv[1], false)) {
1724 set_external_id(struct smap *old, struct smap *new,
1725 char *key, char *value)
1727 smap_clone(new, old);
1730 smap_replace(new, key, value);
1732 smap_remove(new, key);
1737 pre_cmd_br_set_external_id(struct vsctl_context *ctx)
1740 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_external_ids);
1741 ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_external_ids);
1745 cmd_br_set_external_id(struct vsctl_context *ctx)
1747 struct vsctl_bridge *bridge;
1750 vsctl_context_populate_cache(ctx);
1751 bridge = find_bridge(ctx, ctx->argv[1], true);
1752 if (bridge->br_cfg) {
1754 set_external_id(&bridge->br_cfg->external_ids, &new, ctx->argv[2],
1755 ctx->argc >= 4 ? ctx->argv[3] : NULL);
1756 ovsrec_bridge_verify_external_ids(bridge->br_cfg);
1757 ovsrec_bridge_set_external_ids(bridge->br_cfg, &new);
1759 char *key = xasprintf("fake-bridge-%s", ctx->argv[2]);
1760 struct vsctl_port *port = shash_find_data(&ctx->ports, ctx->argv[1]);
1761 set_external_id(&port->port_cfg->external_ids, &new,
1762 key, ctx->argc >= 4 ? ctx->argv[3] : NULL);
1763 ovsrec_port_verify_external_ids(port->port_cfg);
1764 ovsrec_port_set_external_ids(port->port_cfg, &new);
1771 get_external_id(struct smap *smap, const char *prefix, const char *key,
1775 char *prefix_key = xasprintf("%s%s", prefix, key);
1776 const char *value = smap_get(smap, prefix_key);
1779 ds_put_format(output, "%s\n", value);
1783 const struct smap_node **sorted = smap_sort(smap);
1784 size_t prefix_len = strlen(prefix);
1787 for (i = 0; i < smap_count(smap); i++) {
1788 const struct smap_node *node = sorted[i];
1789 if (!strncmp(node->key, prefix, prefix_len)) {
1790 ds_put_format(output, "%s=%s\n", node->key + prefix_len,
1799 pre_cmd_br_get_external_id(struct vsctl_context *ctx)
1801 pre_cmd_br_set_external_id(ctx);
1805 cmd_br_get_external_id(struct vsctl_context *ctx)
1807 struct vsctl_bridge *bridge;
1809 vsctl_context_populate_cache(ctx);
1811 bridge = find_bridge(ctx, ctx->argv[1], true);
1812 if (bridge->br_cfg) {
1813 ovsrec_bridge_verify_external_ids(bridge->br_cfg);
1814 get_external_id(&bridge->br_cfg->external_ids, "",
1815 ctx->argc >= 3 ? ctx->argv[2] : NULL, &ctx->output);
1817 struct vsctl_port *port = shash_find_data(&ctx->ports, ctx->argv[1]);
1818 ovsrec_port_verify_external_ids(port->port_cfg);
1819 get_external_id(&port->port_cfg->external_ids, "fake-bridge-",
1820 ctx->argc >= 3 ? ctx->argv[2] : NULL, &ctx->output);
1825 cmd_list_ports(struct vsctl_context *ctx)
1827 struct vsctl_bridge *br;
1828 struct vsctl_port *port;
1831 vsctl_context_populate_cache(ctx);
1832 br = find_bridge(ctx, ctx->argv[1], true);
1833 ovsrec_bridge_verify_ports(br->br_cfg ? br->br_cfg : br->parent->br_cfg);
1836 LIST_FOR_EACH (port, ports_node, &br->ports) {
1837 if (strcmp(port->port_cfg->name, br->name)) {
1838 svec_add(&ports, port->port_cfg->name);
1841 output_sorted(&ports, &ctx->output);
1842 svec_destroy(&ports);
1846 add_port(struct vsctl_context *ctx,
1847 const char *br_name, const char *port_name,
1848 bool may_exist, bool fake_iface,
1849 char *iface_names[], int n_ifaces,
1850 char *settings[], int n_settings)
1852 struct vsctl_port *vsctl_port;
1853 struct vsctl_bridge *bridge;
1854 struct ovsrec_interface **ifaces;
1855 struct ovsrec_port *port;
1858 vsctl_context_populate_cache(ctx);
1860 struct vsctl_port *vsctl_port;
1862 vsctl_port = find_port(ctx, port_name, false);
1864 struct svec want_names, have_names;
1866 svec_init(&want_names);
1867 for (i = 0; i < n_ifaces; i++) {
1868 svec_add(&want_names, iface_names[i]);
1870 svec_sort(&want_names);
1872 svec_init(&have_names);
1873 for (i = 0; i < vsctl_port->port_cfg->n_interfaces; i++) {
1874 svec_add(&have_names,
1875 vsctl_port->port_cfg->interfaces[i]->name);
1877 svec_sort(&have_names);
1879 if (strcmp(vsctl_port->bridge->name, br_name)) {
1880 char *command = vsctl_context_to_string(ctx);
1881 vsctl_fatal("\"%s\" but %s is actually attached to bridge %s",
1882 command, port_name, vsctl_port->bridge->name);
1885 if (!svec_equal(&want_names, &have_names)) {
1886 char *have_names_string = svec_join(&have_names, ", ", "");
1887 char *command = vsctl_context_to_string(ctx);
1889 vsctl_fatal("\"%s\" but %s actually has interface(s) %s",
1890 command, port_name, have_names_string);
1893 svec_destroy(&want_names);
1894 svec_destroy(&have_names);
1899 check_conflicts(ctx, port_name,
1900 xasprintf("cannot create a port named %s", port_name));
1901 for (i = 0; i < n_ifaces; i++) {
1902 check_conflicts(ctx, iface_names[i],
1903 xasprintf("cannot create an interface named %s",
1906 bridge = find_bridge(ctx, br_name, true);
1908 ifaces = xmalloc(n_ifaces * sizeof *ifaces);
1909 for (i = 0; i < n_ifaces; i++) {
1910 ifaces[i] = ovsrec_interface_insert(ctx->txn);
1911 ovsrec_interface_set_name(ifaces[i], iface_names[i]);
1914 port = ovsrec_port_insert(ctx->txn);
1915 ovsrec_port_set_name(port, port_name);
1916 ovsrec_port_set_interfaces(port, ifaces, n_ifaces);
1917 ovsrec_port_set_bond_fake_iface(port, fake_iface);
1919 if (bridge->parent) {
1920 int64_t tag = bridge->vlan;
1921 ovsrec_port_set_tag(port, &tag, 1);
1924 for (i = 0; i < n_settings; i++) {
1925 set_column(get_table("Port"), &port->header_, settings[i],
1929 bridge_insert_port((bridge->parent ? bridge->parent->br_cfg
1930 : bridge->br_cfg), port);
1932 vsctl_port = add_port_to_cache(ctx, bridge, port);
1933 for (i = 0; i < n_ifaces; i++) {
1934 add_iface_to_cache(ctx, vsctl_port, ifaces[i]);
1940 cmd_add_port(struct vsctl_context *ctx)
1942 bool may_exist = shash_find(&ctx->options, "--may-exist") != NULL;
1944 add_port(ctx, ctx->argv[1], ctx->argv[2], may_exist, false,
1945 &ctx->argv[2], 1, &ctx->argv[3], ctx->argc - 3);
1949 cmd_add_bond(struct vsctl_context *ctx)
1951 bool may_exist = shash_find(&ctx->options, "--may-exist") != NULL;
1952 bool fake_iface = shash_find(&ctx->options, "--fake-iface");
1956 n_ifaces = ctx->argc - 3;
1957 for (i = 3; i < ctx->argc; i++) {
1958 if (strchr(ctx->argv[i], '=')) {
1964 vsctl_fatal("add-bond requires at least 2 interfaces, but only "
1965 "%d were specified", n_ifaces);
1968 add_port(ctx, ctx->argv[1], ctx->argv[2], may_exist, fake_iface,
1969 &ctx->argv[3], n_ifaces,
1970 &ctx->argv[n_ifaces + 3], ctx->argc - 3 - n_ifaces);
1974 cmd_del_port(struct vsctl_context *ctx)
1976 bool must_exist = !shash_find(&ctx->options, "--if-exists");
1977 bool with_iface = shash_find(&ctx->options, "--with-iface") != NULL;
1978 struct vsctl_port *port;
1980 vsctl_context_populate_cache(ctx);
1982 port = find_port(ctx, ctx->argv[ctx->argc - 1], must_exist);
1984 const char *target = ctx->argv[ctx->argc - 1];
1985 struct vsctl_iface *iface;
1987 port = find_port(ctx, target, false);
1989 iface = find_iface(ctx, target, false);
1994 if (must_exist && !port) {
1995 vsctl_fatal("no port or interface named %s", target);
2000 if (ctx->argc == 3) {
2001 struct vsctl_bridge *bridge;
2003 bridge = find_bridge(ctx, ctx->argv[1], true);
2004 if (port->bridge != bridge) {
2005 if (port->bridge->parent == bridge) {
2006 vsctl_fatal("bridge %s does not have a port %s (although "
2007 "its parent bridge %s does)",
2008 ctx->argv[1], ctx->argv[2],
2009 bridge->parent->name);
2011 vsctl_fatal("bridge %s does not have a port %s",
2012 ctx->argv[1], ctx->argv[2]);
2017 del_port(ctx, port);
2022 cmd_port_to_br(struct vsctl_context *ctx)
2024 struct vsctl_port *port;
2026 vsctl_context_populate_cache(ctx);
2028 port = find_port(ctx, ctx->argv[1], true);
2029 ds_put_format(&ctx->output, "%s\n", port->bridge->name);
2033 cmd_br_to_vlan(struct vsctl_context *ctx)
2035 struct vsctl_bridge *bridge;
2037 vsctl_context_populate_cache(ctx);
2039 bridge = find_bridge(ctx, ctx->argv[1], true);
2040 ds_put_format(&ctx->output, "%d\n", bridge->vlan);
2044 cmd_br_to_parent(struct vsctl_context *ctx)
2046 struct vsctl_bridge *bridge;
2048 vsctl_context_populate_cache(ctx);
2050 bridge = find_bridge(ctx, ctx->argv[1], true);
2051 if (bridge->parent) {
2052 bridge = bridge->parent;
2054 ds_put_format(&ctx->output, "%s\n", bridge->name);
2058 cmd_list_ifaces(struct vsctl_context *ctx)
2060 struct vsctl_bridge *br;
2061 struct vsctl_port *port;
2064 vsctl_context_populate_cache(ctx);
2066 br = find_bridge(ctx, ctx->argv[1], true);
2070 LIST_FOR_EACH (port, ports_node, &br->ports) {
2071 struct vsctl_iface *iface;
2073 LIST_FOR_EACH (iface, ifaces_node, &port->ifaces) {
2074 if (strcmp(iface->iface_cfg->name, br->name)) {
2075 svec_add(&ifaces, iface->iface_cfg->name);
2079 output_sorted(&ifaces, &ctx->output);
2080 svec_destroy(&ifaces);
2084 cmd_iface_to_br(struct vsctl_context *ctx)
2086 struct vsctl_iface *iface;
2088 vsctl_context_populate_cache(ctx);
2090 iface = find_iface(ctx, ctx->argv[1], true);
2091 ds_put_format(&ctx->output, "%s\n", iface->port->bridge->name);
2095 verify_controllers(struct ovsrec_bridge *bridge)
2099 ovsrec_bridge_verify_controller(bridge);
2100 for (i = 0; i < bridge->n_controller; i++) {
2101 ovsrec_controller_verify_target(bridge->controller[i]);
2106 pre_controller(struct vsctl_context *ctx)
2110 ovsdb_idl_add_column(ctx->idl, &ovsrec_controller_col_target);
2114 cmd_get_controller(struct vsctl_context *ctx)
2116 struct vsctl_bridge *br;
2117 struct svec targets;
2120 vsctl_context_populate_cache(ctx);
2122 br = find_bridge(ctx, ctx->argv[1], true);
2126 verify_controllers(br->br_cfg);
2128 /* Print the targets in sorted order for reproducibility. */
2129 svec_init(&targets);
2130 for (i = 0; i < br->br_cfg->n_controller; i++) {
2131 svec_add(&targets, br->br_cfg->controller[i]->target);
2134 svec_sort(&targets);
2135 for (i = 0; i < targets.n; i++) {
2136 ds_put_format(&ctx->output, "%s\n", targets.names[i]);
2138 svec_destroy(&targets);
2142 delete_controllers(struct ovsrec_controller **controllers,
2143 size_t n_controllers)
2147 for (i = 0; i < n_controllers; i++) {
2148 ovsrec_controller_delete(controllers[i]);
2153 cmd_del_controller(struct vsctl_context *ctx)
2155 struct ovsrec_bridge *br;
2157 vsctl_context_populate_cache(ctx);
2159 br = find_real_bridge(ctx, ctx->argv[1], true)->br_cfg;
2160 verify_controllers(br);
2162 if (br->controller) {
2163 delete_controllers(br->controller, br->n_controller);
2164 ovsrec_bridge_set_controller(br, NULL, 0);
2168 static struct ovsrec_controller **
2169 insert_controllers(struct ovsdb_idl_txn *txn, char *targets[], size_t n)
2171 struct ovsrec_controller **controllers;
2174 controllers = xmalloc(n * sizeof *controllers);
2175 for (i = 0; i < n; i++) {
2176 if (vconn_verify_name(targets[i]) && pvconn_verify_name(targets[i])) {
2177 VLOG_WARN("target type \"%s\" is possibly erroneous", targets[i]);
2179 controllers[i] = ovsrec_controller_insert(txn);
2180 ovsrec_controller_set_target(controllers[i], targets[i]);
2187 cmd_set_controller(struct vsctl_context *ctx)
2189 struct ovsrec_controller **controllers;
2190 struct ovsrec_bridge *br;
2193 vsctl_context_populate_cache(ctx);
2195 br = find_real_bridge(ctx, ctx->argv[1], true)->br_cfg;
2196 verify_controllers(br);
2198 delete_controllers(br->controller, br->n_controller);
2201 controllers = insert_controllers(ctx->txn, &ctx->argv[2], n);
2202 ovsrec_bridge_set_controller(br, controllers, n);
2207 cmd_get_fail_mode(struct vsctl_context *ctx)
2209 struct vsctl_bridge *br;
2210 const char *fail_mode;
2212 vsctl_context_populate_cache(ctx);
2213 br = find_bridge(ctx, ctx->argv[1], true);
2218 ovsrec_bridge_verify_fail_mode(br->br_cfg);
2220 fail_mode = br->br_cfg->fail_mode;
2221 if (fail_mode && strlen(fail_mode)) {
2222 ds_put_format(&ctx->output, "%s\n", fail_mode);
2227 cmd_del_fail_mode(struct vsctl_context *ctx)
2229 struct vsctl_bridge *br;
2231 vsctl_context_populate_cache(ctx);
2233 br = find_real_bridge(ctx, ctx->argv[1], true);
2235 ovsrec_bridge_set_fail_mode(br->br_cfg, NULL);
2239 cmd_set_fail_mode(struct vsctl_context *ctx)
2241 struct vsctl_bridge *br;
2242 const char *fail_mode = ctx->argv[2];
2244 vsctl_context_populate_cache(ctx);
2246 br = find_real_bridge(ctx, ctx->argv[1], true);
2248 if (strcmp(fail_mode, "standalone") && strcmp(fail_mode, "secure")) {
2249 vsctl_fatal("fail-mode must be \"standalone\" or \"secure\"");
2252 ovsrec_bridge_set_fail_mode(br->br_cfg, fail_mode);
2256 verify_managers(const struct ovsrec_open_vswitch *ovs)
2260 ovsrec_open_vswitch_verify_manager_options(ovs);
2262 for (i = 0; i < ovs->n_manager_options; ++i) {
2263 const struct ovsrec_manager *mgr = ovs->manager_options[i];
2265 ovsrec_manager_verify_target(mgr);
2270 pre_manager(struct vsctl_context *ctx)
2272 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_manager_options);
2273 ovsdb_idl_add_column(ctx->idl, &ovsrec_manager_col_target);
2277 cmd_get_manager(struct vsctl_context *ctx)
2279 const struct ovsrec_open_vswitch *ovs = ctx->ovs;
2280 struct svec targets;
2283 verify_managers(ovs);
2285 /* Print the targets in sorted order for reproducibility. */
2286 svec_init(&targets);
2288 for (i = 0; i < ovs->n_manager_options; i++) {
2289 svec_add(&targets, ovs->manager_options[i]->target);
2292 svec_sort_unique(&targets);
2293 for (i = 0; i < targets.n; i++) {
2294 ds_put_format(&ctx->output, "%s\n", targets.names[i]);
2296 svec_destroy(&targets);
2300 delete_managers(const struct vsctl_context *ctx)
2302 const struct ovsrec_open_vswitch *ovs = ctx->ovs;
2305 /* Delete Manager rows pointed to by 'manager_options' column. */
2306 for (i = 0; i < ovs->n_manager_options; i++) {
2307 ovsrec_manager_delete(ovs->manager_options[i]);
2310 /* Delete 'Manager' row refs in 'manager_options' column. */
2311 ovsrec_open_vswitch_set_manager_options(ovs, NULL, 0);
2315 cmd_del_manager(struct vsctl_context *ctx)
2317 const struct ovsrec_open_vswitch *ovs = ctx->ovs;
2319 verify_managers(ovs);
2320 delete_managers(ctx);
2324 insert_managers(struct vsctl_context *ctx, char *targets[], size_t n)
2326 struct ovsrec_manager **managers;
2329 /* Insert each manager in a new row in Manager table. */
2330 managers = xmalloc(n * sizeof *managers);
2331 for (i = 0; i < n; i++) {
2332 if (stream_verify_name(targets[i]) && pstream_verify_name(targets[i])) {
2333 VLOG_WARN("target type \"%s\" is possibly erroneous", targets[i]);
2335 managers[i] = ovsrec_manager_insert(ctx->txn);
2336 ovsrec_manager_set_target(managers[i], targets[i]);
2339 /* Store uuids of new Manager rows in 'manager_options' column. */
2340 ovsrec_open_vswitch_set_manager_options(ctx->ovs, managers, n);
2345 cmd_set_manager(struct vsctl_context *ctx)
2347 const size_t n = ctx->argc - 1;
2349 verify_managers(ctx->ovs);
2350 delete_managers(ctx);
2351 insert_managers(ctx, &ctx->argv[1], n);
2355 pre_cmd_get_ssl(struct vsctl_context *ctx)
2357 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_ssl);
2359 ovsdb_idl_add_column(ctx->idl, &ovsrec_ssl_col_private_key);
2360 ovsdb_idl_add_column(ctx->idl, &ovsrec_ssl_col_certificate);
2361 ovsdb_idl_add_column(ctx->idl, &ovsrec_ssl_col_ca_cert);
2362 ovsdb_idl_add_column(ctx->idl, &ovsrec_ssl_col_bootstrap_ca_cert);
2366 cmd_get_ssl(struct vsctl_context *ctx)
2368 struct ovsrec_ssl *ssl = ctx->ovs->ssl;
2370 ovsrec_open_vswitch_verify_ssl(ctx->ovs);
2372 ovsrec_ssl_verify_private_key(ssl);
2373 ovsrec_ssl_verify_certificate(ssl);
2374 ovsrec_ssl_verify_ca_cert(ssl);
2375 ovsrec_ssl_verify_bootstrap_ca_cert(ssl);
2377 ds_put_format(&ctx->output, "Private key: %s\n", ssl->private_key);
2378 ds_put_format(&ctx->output, "Certificate: %s\n", ssl->certificate);
2379 ds_put_format(&ctx->output, "CA Certificate: %s\n", ssl->ca_cert);
2380 ds_put_format(&ctx->output, "Bootstrap: %s\n",
2381 ssl->bootstrap_ca_cert ? "true" : "false");
2386 pre_cmd_del_ssl(struct vsctl_context *ctx)
2388 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_ssl);
2392 cmd_del_ssl(struct vsctl_context *ctx)
2394 struct ovsrec_ssl *ssl = ctx->ovs->ssl;
2397 ovsrec_open_vswitch_verify_ssl(ctx->ovs);
2398 ovsrec_ssl_delete(ssl);
2399 ovsrec_open_vswitch_set_ssl(ctx->ovs, NULL);
2404 pre_cmd_set_ssl(struct vsctl_context *ctx)
2406 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_ssl);
2410 cmd_set_ssl(struct vsctl_context *ctx)
2412 bool bootstrap = shash_find(&ctx->options, "--bootstrap");
2413 struct ovsrec_ssl *ssl = ctx->ovs->ssl;
2415 ovsrec_open_vswitch_verify_ssl(ctx->ovs);
2417 ovsrec_ssl_delete(ssl);
2419 ssl = ovsrec_ssl_insert(ctx->txn);
2421 ovsrec_ssl_set_private_key(ssl, ctx->argv[1]);
2422 ovsrec_ssl_set_certificate(ssl, ctx->argv[2]);
2423 ovsrec_ssl_set_ca_cert(ssl, ctx->argv[3]);
2425 ovsrec_ssl_set_bootstrap_ca_cert(ssl, bootstrap);
2427 ovsrec_open_vswitch_set_ssl(ctx->ovs, ssl);
2430 /* Parameter commands. */
2432 struct vsctl_row_id {
2433 const struct ovsdb_idl_table_class *table;
2434 const struct ovsdb_idl_column *name_column;
2435 const struct ovsdb_idl_column *uuid_column;
2438 struct vsctl_table_class {
2439 struct ovsdb_idl_table_class *class;
2440 struct vsctl_row_id row_ids[2];
2443 static const struct vsctl_table_class tables[] = {
2444 {&ovsrec_table_bridge,
2445 {{&ovsrec_table_bridge, &ovsrec_bridge_col_name, NULL},
2446 {NULL, NULL, NULL}}},
2448 {&ovsrec_table_controller,
2449 {{&ovsrec_table_bridge,
2450 &ovsrec_bridge_col_name,
2451 &ovsrec_bridge_col_controller}}},
2453 {&ovsrec_table_interface,
2454 {{&ovsrec_table_interface, &ovsrec_interface_col_name, NULL},
2455 {NULL, NULL, NULL}}},
2457 {&ovsrec_table_mirror,
2458 {{&ovsrec_table_mirror, &ovsrec_mirror_col_name, NULL},
2459 {NULL, NULL, NULL}}},
2461 {&ovsrec_table_manager,
2462 {{&ovsrec_table_manager, &ovsrec_manager_col_target, NULL},
2463 {NULL, NULL, NULL}}},
2465 {&ovsrec_table_netflow,
2466 {{&ovsrec_table_bridge,
2467 &ovsrec_bridge_col_name,
2468 &ovsrec_bridge_col_netflow},
2469 {NULL, NULL, NULL}}},
2471 {&ovsrec_table_open_vswitch,
2472 {{&ovsrec_table_open_vswitch, NULL, NULL},
2473 {NULL, NULL, NULL}}},
2475 {&ovsrec_table_port,
2476 {{&ovsrec_table_port, &ovsrec_port_col_name, NULL},
2477 {NULL, NULL, NULL}}},
2480 {{&ovsrec_table_port, &ovsrec_port_col_name, &ovsrec_port_col_qos},
2481 {NULL, NULL, NULL}}},
2483 {&ovsrec_table_queue,
2484 {{NULL, NULL, NULL},
2485 {NULL, NULL, NULL}}},
2488 {{&ovsrec_table_open_vswitch, NULL, &ovsrec_open_vswitch_col_ssl}}},
2490 {&ovsrec_table_sflow,
2491 {{&ovsrec_table_bridge,
2492 &ovsrec_bridge_col_name,
2493 &ovsrec_bridge_col_sflow},
2494 {NULL, NULL, NULL}}},
2496 {&ovsrec_table_flow_table,
2497 {{&ovsrec_table_flow_table, &ovsrec_flow_table_col_name, NULL},
2498 {NULL, NULL, NULL}}},
2500 {NULL, {{NULL, NULL, NULL}, {NULL, NULL, NULL}}}
2504 die_if_error(char *error)
2507 vsctl_fatal("%s", error);
2512 to_lower_and_underscores(unsigned c)
2514 return c == '-' ? '_' : tolower(c);
2518 score_partial_match(const char *name, const char *s)
2522 if (!strcmp(name, s)) {
2525 for (score = 0; ; score++, name++, s++) {
2526 if (to_lower_and_underscores(*name) != to_lower_and_underscores(*s)) {
2528 } else if (*name == '\0') {
2529 return UINT_MAX - 1;
2532 return *s == '\0' ? score : 0;
2535 static const struct vsctl_table_class *
2536 get_table(const char *table_name)
2538 const struct vsctl_table_class *table;
2539 const struct vsctl_table_class *best_match = NULL;
2540 unsigned int best_score = 0;
2542 for (table = tables; table->class; table++) {
2543 unsigned int score = score_partial_match(table->class->name,
2545 if (score > best_score) {
2548 } else if (score == best_score) {
2554 } else if (best_score) {
2555 vsctl_fatal("multiple table names match \"%s\"", table_name);
2557 vsctl_fatal("unknown table \"%s\"", table_name);
2561 static const struct vsctl_table_class *
2562 pre_get_table(struct vsctl_context *ctx, const char *table_name)
2564 const struct vsctl_table_class *table_class;
2567 table_class = get_table(table_name);
2568 ovsdb_idl_add_table(ctx->idl, table_class->class);
2570 for (i = 0; i < ARRAY_SIZE(table_class->row_ids); i++) {
2571 const struct vsctl_row_id *id = &table_class->row_ids[i];
2573 ovsdb_idl_add_table(ctx->idl, id->table);
2575 if (id->name_column) {
2576 ovsdb_idl_add_column(ctx->idl, id->name_column);
2578 if (id->uuid_column) {
2579 ovsdb_idl_add_column(ctx->idl, id->uuid_column);
2586 static const struct ovsdb_idl_row *
2587 get_row_by_id(struct vsctl_context *ctx, const struct vsctl_table_class *table,
2588 const struct vsctl_row_id *id, const char *record_id)
2590 const struct ovsdb_idl_row *referrer, *final;
2596 if (!id->name_column) {
2597 if (strcmp(record_id, ".")) {
2600 referrer = ovsdb_idl_first_row(ctx->idl, id->table);
2601 if (!referrer || ovsdb_idl_next_row(referrer)) {
2605 const struct ovsdb_idl_row *row;
2608 for (row = ovsdb_idl_first_row(ctx->idl, id->table);
2610 row = ovsdb_idl_next_row(row))
2612 const struct ovsdb_datum *name;
2614 name = ovsdb_idl_get(row, id->name_column,
2615 OVSDB_TYPE_STRING, OVSDB_TYPE_VOID);
2616 if (name->n == 1 && !strcmp(name->keys[0].string, record_id)) {
2618 vsctl_fatal("multiple rows in %s match \"%s\"",
2619 table->class->name, record_id);
2630 if (id->uuid_column) {
2631 const struct ovsdb_datum *uuid;
2633 ovsdb_idl_txn_verify(referrer, id->uuid_column);
2634 uuid = ovsdb_idl_get(referrer, id->uuid_column,
2635 OVSDB_TYPE_UUID, OVSDB_TYPE_VOID);
2637 final = ovsdb_idl_get_row_for_uuid(ctx->idl, table->class,
2638 &uuid->keys[0].uuid);
2647 static const struct ovsdb_idl_row *
2648 get_row (struct vsctl_context *ctx,
2649 const struct vsctl_table_class *table, const char *record_id)
2651 const struct ovsdb_idl_row *row;
2654 if (uuid_from_string(&uuid, record_id)) {
2655 row = ovsdb_idl_get_row_for_uuid(ctx->idl, table->class, &uuid);
2659 for (i = 0; i < ARRAY_SIZE(table->row_ids); i++) {
2660 row = get_row_by_id(ctx, table, &table->row_ids[i], record_id);
2669 static const struct ovsdb_idl_row *
2670 must_get_row(struct vsctl_context *ctx,
2671 const struct vsctl_table_class *table, const char *record_id)
2673 const struct ovsdb_idl_row *row = get_row(ctx, table, record_id);
2675 vsctl_fatal("no row \"%s\" in table %s",
2676 record_id, table->class->name);
2682 get_column(const struct vsctl_table_class *table, const char *column_name,
2683 const struct ovsdb_idl_column **columnp)
2685 const struct ovsdb_idl_column *best_match = NULL;
2686 unsigned int best_score = 0;
2689 for (i = 0; i < table->class->n_columns; i++) {
2690 const struct ovsdb_idl_column *column = &table->class->columns[i];
2691 unsigned int score = score_partial_match(column->name, column_name);
2692 if (score > best_score) {
2693 best_match = column;
2695 } else if (score == best_score) {
2700 *columnp = best_match;
2703 } else if (best_score) {
2704 return xasprintf("%s contains more than one column whose name "
2705 "matches \"%s\"", table->class->name, column_name);
2707 return xasprintf("%s does not contain a column whose name matches "
2708 "\"%s\"", table->class->name, column_name);
2712 static struct ovsdb_symbol *
2713 create_symbol(struct ovsdb_symbol_table *symtab, const char *id, bool *newp)
2715 struct ovsdb_symbol *symbol;
2718 vsctl_fatal("row id \"%s\" does not begin with \"@\"", id);
2722 *newp = ovsdb_symbol_table_get(symtab, id) == NULL;
2725 symbol = ovsdb_symbol_table_insert(symtab, id);
2726 if (symbol->created) {
2727 vsctl_fatal("row id \"%s\" may only be specified on one --id option",
2730 symbol->created = true;
2735 pre_get_column(struct vsctl_context *ctx,
2736 const struct vsctl_table_class *table, const char *column_name,
2737 const struct ovsdb_idl_column **columnp)
2739 die_if_error(get_column(table, column_name, columnp));
2740 ovsdb_idl_add_column(ctx->idl, *columnp);
2744 missing_operator_error(const char *arg, const char **allowed_operators,
2750 ds_put_format(&s, "%s: argument does not end in ", arg);
2751 ds_put_format(&s, "\"%s\"", allowed_operators[0]);
2752 if (n_allowed == 2) {
2753 ds_put_format(&s, " or \"%s\"", allowed_operators[1]);
2754 } else if (n_allowed > 2) {
2757 for (i = 1; i < n_allowed - 1; i++) {
2758 ds_put_format(&s, ", \"%s\"", allowed_operators[i]);
2760 ds_put_format(&s, ", or \"%s\"", allowed_operators[i]);
2762 ds_put_format(&s, " followed by a value.");
2764 return ds_steal_cstr(&s);
2767 /* Breaks 'arg' apart into a number of fields in the following order:
2769 * - The name of a column in 'table', stored into '*columnp'. The column
2770 * name may be abbreviated.
2772 * - Optionally ':' followed by a key string. The key is stored as a
2773 * malloc()'d string into '*keyp', or NULL if no key is present in
2776 * - If 'valuep' is nonnull, an operator followed by a value string. The
2777 * allowed operators are the 'n_allowed' string in 'allowed_operators',
2778 * or just "=" if 'n_allowed' is 0. If 'operatorp' is nonnull, then the
2779 * index of the operator within 'allowed_operators' is stored into
2780 * '*operatorp'. The value is stored as a malloc()'d string into
2781 * '*valuep', or NULL if no value is present in 'arg'.
2783 * On success, returns NULL. On failure, returned a malloc()'d string error
2784 * message and stores NULL into all of the nonnull output arguments. */
2785 static char * WARN_UNUSED_RESULT
2786 parse_column_key_value(const char *arg,
2787 const struct vsctl_table_class *table,
2788 const struct ovsdb_idl_column **columnp, char **keyp,
2790 const char **allowed_operators, size_t n_allowed,
2793 const char *p = arg;
2797 assert(!(operatorp && !valuep));
2803 /* Parse column name. */
2804 error = ovsdb_token_parse(&p, &column_name);
2808 if (column_name[0] == '\0') {
2810 error = xasprintf("%s: missing column name", arg);
2813 error = get_column(table, column_name, columnp);
2819 /* Parse key string. */
2822 error = ovsdb_token_parse(&p, keyp);
2828 /* Parse value string. */
2834 if (!allowed_operators) {
2835 static const char *equals = "=";
2836 allowed_operators = =
2842 for (i = 0; i < n_allowed; i++) {
2843 const char *op = allowed_operators[i];
2844 size_t op_len = strlen(op);
2846 if (op_len > best_len && !strncmp(op, p, op_len) && p[op_len]) {
2852 error = missing_operator_error(arg, allowed_operators, n_allowed);
2859 *valuep = xstrdup(p + best_len);
2862 error = xasprintf("%s: trailing garbage \"%s\" in argument",
2883 static const struct ovsdb_idl_column *
2884 pre_parse_column_key_value(struct vsctl_context *ctx,
2886 const struct vsctl_table_class *table)
2888 const struct ovsdb_idl_column *column;
2893 die_if_error(ovsdb_token_parse(&p, &column_name));
2894 if (column_name[0] == '\0') {
2895 vsctl_fatal("%s: missing column name", arg);
2898 pre_get_column(ctx, table, column_name, &column);
2905 check_mutable(const struct vsctl_table_class *table,
2906 const struct ovsdb_idl_column *column)
2908 if (!column->mutable) {
2909 vsctl_fatal("cannot modify read-only column %s in table %s",
2910 column->name, table->class->name);
2915 pre_cmd_get(struct vsctl_context *ctx)
2917 const char *id = shash_find_data(&ctx->options, "--id");
2918 const char *table_name = ctx->argv[1];
2919 const struct vsctl_table_class *table;
2922 /* Using "get" without --id or a column name could possibly make sense.
2923 * Maybe, for example, a ovs-vsctl run wants to assert that a row exists.
2924 * But it is unlikely that an interactive user would want to do that, so
2925 * issue a warning if we're running on a terminal. */
2926 if (!id && ctx->argc <= 3 && isatty(STDOUT_FILENO)) {
2927 VLOG_WARN("\"get\" command without row arguments or \"--id\" is "
2928 "possibly erroneous");
2931 table = pre_get_table(ctx, table_name);
2932 for (i = 3; i < ctx->argc; i++) {
2933 if (!strcasecmp(ctx->argv[i], "_uuid")
2934 || !strcasecmp(ctx->argv[i], "-uuid")) {
2938 pre_parse_column_key_value(ctx, ctx->argv[i], table);
2943 cmd_get(struct vsctl_context *ctx)
2945 const char *id = shash_find_data(&ctx->options, "--id");
2946 bool if_exists = shash_find(&ctx->options, "--if-exists");
2947 const char *table_name = ctx->argv[1];
2948 const char *record_id = ctx->argv[2];
2949 const struct vsctl_table_class *table;
2950 const struct ovsdb_idl_row *row;
2951 struct ds *out = &ctx->output;
2954 table = get_table(table_name);
2955 row = must_get_row(ctx, table, record_id);
2957 struct ovsdb_symbol *symbol;
2960 symbol = create_symbol(ctx->symtab, id, &new);
2962 vsctl_fatal("row id \"%s\" specified on \"get\" command was used "
2963 "before it was defined", id);
2965 symbol->uuid = row->uuid;
2967 /* This symbol refers to a row that already exists, so disable warnings
2968 * about it being unreferenced. */
2969 symbol->strong_ref = true;
2971 for (i = 3; i < ctx->argc; i++) {
2972 const struct ovsdb_idl_column *column;
2973 const struct ovsdb_datum *datum;
2976 /* Special case for obtaining the UUID of a row. We can't just do this
2977 * through parse_column_key_value() below since it returns a "struct
2978 * ovsdb_idl_column" and the UUID column doesn't have one. */
2979 if (!strcasecmp(ctx->argv[i], "_uuid")
2980 || !strcasecmp(ctx->argv[i], "-uuid")) {
2981 ds_put_format(out, UUID_FMT"\n", UUID_ARGS(&row->uuid));
2985 die_if_error(parse_column_key_value(ctx->argv[i], table,
2986 &column, &key_string,
2987 NULL, NULL, 0, NULL));
2989 ovsdb_idl_txn_verify(row, column);
2990 datum = ovsdb_idl_read(row, column);
2992 union ovsdb_atom key;
2995 if (column->type.value.type == OVSDB_TYPE_VOID) {
2996 vsctl_fatal("cannot specify key to get for non-map column %s",
3000 die_if_error(ovsdb_atom_from_string(&key,
3002 key_string, ctx->symtab));
3004 idx = ovsdb_datum_find_key(datum, &key,
3005 column->type.key.type);
3006 if (idx == UINT_MAX) {
3008 vsctl_fatal("no key \"%s\" in %s record \"%s\" column %s",
3009 key_string, table->class->name, record_id,
3013 ovsdb_atom_to_string(&datum->values[idx],
3014 column->type.value.type, out);
3016 ovsdb_atom_destroy(&key, column->type.key.type);
3018 ovsdb_datum_to_string(datum, &column->type, out);
3020 ds_put_char(out, '\n');
3027 parse_column_names(const char *column_names,
3028 const struct vsctl_table_class *table,
3029 const struct ovsdb_idl_column ***columnsp,
3032 const struct ovsdb_idl_column **columns;
3035 if (!column_names) {
3038 n_columns = table->class->n_columns + 1;
3039 columns = xmalloc(n_columns * sizeof *columns);
3041 for (i = 0; i < table->class->n_columns; i++) {
3042 columns[i + 1] = &table->class->columns[i];
3045 char *s = xstrdup(column_names);
3046 size_t allocated_columns;
3047 char *save_ptr = NULL;
3051 allocated_columns = n_columns = 0;
3052 for (column_name = strtok_r(s, ", ", &save_ptr); column_name;
3053 column_name = strtok_r(NULL, ", ", &save_ptr)) {
3054 const struct ovsdb_idl_column *column;
3056 if (!strcasecmp(column_name, "_uuid")) {
3059 die_if_error(get_column(table, column_name, &column));
3061 if (n_columns >= allocated_columns) {
3062 columns = x2nrealloc(columns, &allocated_columns,
3065 columns[n_columns++] = column;
3070 vsctl_fatal("must specify at least one column name");
3073 *columnsp = columns;
3074 *n_columnsp = n_columns;
3079 pre_list_columns(struct vsctl_context *ctx,
3080 const struct vsctl_table_class *table,
3081 const char *column_names)
3083 const struct ovsdb_idl_column **columns;
3087 parse_column_names(column_names, table, &columns, &n_columns);
3088 for (i = 0; i < n_columns; i++) {
3090 ovsdb_idl_add_column(ctx->idl, columns[i]);
3097 pre_cmd_list(struct vsctl_context *ctx)
3099 const char *column_names = shash_find_data(&ctx->options, "--columns");
3100 const char *table_name = ctx->argv[1];
3101 const struct vsctl_table_class *table;
3103 table = pre_get_table(ctx, table_name);
3104 pre_list_columns(ctx, table, column_names);
3107 static struct table *
3108 list_make_table(const struct ovsdb_idl_column **columns, size_t n_columns)
3113 out = xmalloc(sizeof *out);
3116 for (i = 0; i < n_columns; i++) {
3117 const struct ovsdb_idl_column *column = columns[i];
3118 const char *column_name = column ? column->name : "_uuid";
3120 table_add_column(out, "%s", column_name);
3127 list_record(const struct ovsdb_idl_row *row,
3128 const struct ovsdb_idl_column **columns, size_t n_columns,
3134 for (i = 0; i < n_columns; i++) {
3135 const struct ovsdb_idl_column *column = columns[i];
3136 struct cell *cell = table_add_cell(out);
3139 struct ovsdb_datum datum;
3140 union ovsdb_atom atom;
3142 atom.uuid = row->uuid;
3145 datum.values = NULL;
3148 cell->json = ovsdb_datum_to_json(&datum, &ovsdb_type_uuid);
3149 cell->type = &ovsdb_type_uuid;
3151 const struct ovsdb_datum *datum = ovsdb_idl_read(row, column);
3153 cell->json = ovsdb_datum_to_json(datum, &column->type);
3154 cell->type = &column->type;
3160 cmd_list(struct vsctl_context *ctx)
3162 const char *column_names = shash_find_data(&ctx->options, "--columns");
3163 const struct ovsdb_idl_column **columns;
3164 const char *table_name = ctx->argv[1];
3165 const struct vsctl_table_class *table;
3170 table = get_table(table_name);
3171 parse_column_names(column_names, table, &columns, &n_columns);
3172 out = ctx->table = list_make_table(columns, n_columns);
3173 if (ctx->argc > 2) {
3174 for (i = 2; i < ctx->argc; i++) {
3175 list_record(must_get_row(ctx, table, ctx->argv[i]),
3176 columns, n_columns, out);
3179 const struct ovsdb_idl_row *row;
3181 for (row = ovsdb_idl_first_row(ctx->idl, table->class); row != NULL;
3182 row = ovsdb_idl_next_row(row)) {
3183 list_record(row, columns, n_columns, out);
3190 pre_cmd_find(struct vsctl_context *ctx)
3192 const char *column_names = shash_find_data(&ctx->options, "--columns");
3193 const char *table_name = ctx->argv[1];
3194 const struct vsctl_table_class *table;
3197 table = pre_get_table(ctx, table_name);
3198 pre_list_columns(ctx, table, column_names);
3199 for (i = 2; i < ctx->argc; i++) {
3200 pre_parse_column_key_value(ctx, ctx->argv[i], table);
3205 cmd_find(struct vsctl_context *ctx)
3207 const char *column_names = shash_find_data(&ctx->options, "--columns");
3208 const struct ovsdb_idl_column **columns;
3209 const char *table_name = ctx->argv[1];
3210 const struct vsctl_table_class *table;
3211 const struct ovsdb_idl_row *row;
3215 table = get_table(table_name);
3216 parse_column_names(column_names, table, &columns, &n_columns);
3217 out = ctx->table = list_make_table(columns, n_columns);
3218 for (row = ovsdb_idl_first_row(ctx->idl, table->class); row;
3219 row = ovsdb_idl_next_row(row)) {
3222 for (i = 2; i < ctx->argc; i++) {
3223 if (!is_condition_satisfied(table, row, ctx->argv[i],
3228 list_record(row, columns, n_columns, out);
3236 pre_cmd_set(struct vsctl_context *ctx)
3238 const char *table_name = ctx->argv[1];
3239 const struct vsctl_table_class *table;
3242 table = pre_get_table(ctx, table_name);
3243 for (i = 3; i < ctx->argc; i++) {
3244 const struct ovsdb_idl_column *column;
3246 column = pre_parse_column_key_value(ctx, ctx->argv[i], table);
3247 check_mutable(table, column);
3252 set_column(const struct vsctl_table_class *table,
3253 const struct ovsdb_idl_row *row, const char *arg,
3254 struct ovsdb_symbol_table *symtab)
3256 const struct ovsdb_idl_column *column;
3257 char *key_string, *value_string;
3260 error = parse_column_key_value(arg, table, &column, &key_string,
3261 NULL, NULL, 0, &value_string);
3262 die_if_error(error);
3263 if (!value_string) {
3264 vsctl_fatal("%s: missing value", arg);
3268 union ovsdb_atom key, value;
3269 struct ovsdb_datum datum;
3271 if (column->type.value.type == OVSDB_TYPE_VOID) {
3272 vsctl_fatal("cannot specify key to set for non-map column %s",
3276 die_if_error(ovsdb_atom_from_string(&key, &column->type.key,
3277 key_string, symtab));
3278 die_if_error(ovsdb_atom_from_string(&value, &column->type.value,
3279 value_string, symtab));
3281 ovsdb_datum_init_empty(&datum);
3282 ovsdb_datum_add_unsafe(&datum, &key, &value, &column->type);
3284 ovsdb_atom_destroy(&key, column->type.key.type);
3285 ovsdb_atom_destroy(&value, column->type.value.type);
3287 ovsdb_datum_union(&datum, ovsdb_idl_read(row, column),
3288 &column->type, false);
3289 ovsdb_idl_txn_write(row, column, &datum);
3291 struct ovsdb_datum datum;
3293 die_if_error(ovsdb_datum_from_string(&datum, &column->type,
3294 value_string, symtab));
3295 ovsdb_idl_txn_write(row, column, &datum);
3303 cmd_set(struct vsctl_context *ctx)
3305 const char *table_name = ctx->argv[1];
3306 const char *record_id = ctx->argv[2];
3307 const struct vsctl_table_class *table;
3308 const struct ovsdb_idl_row *row;
3311 table = get_table(table_name);
3312 row = must_get_row(ctx, table, record_id);
3313 for (i = 3; i < ctx->argc; i++) {
3314 set_column(table, row, ctx->argv[i], ctx->symtab);
3317 vsctl_context_invalidate_cache(ctx);
3321 pre_cmd_add(struct vsctl_context *ctx)
3323 const char *table_name = ctx->argv[1];
3324 const char *column_name = ctx->argv[3];
3325 const struct vsctl_table_class *table;
3326 const struct ovsdb_idl_column *column;
3328 table = pre_get_table(ctx, table_name);
3329 pre_get_column(ctx, table, column_name, &column);
3330 check_mutable(table, column);
3334 cmd_add(struct vsctl_context *ctx)
3336 const char *table_name = ctx->argv[1];
3337 const char *record_id = ctx->argv[2];
3338 const char *column_name = ctx->argv[3];
3339 const struct vsctl_table_class *table;
3340 const struct ovsdb_idl_column *column;
3341 const struct ovsdb_idl_row *row;
3342 const struct ovsdb_type *type;
3343 struct ovsdb_datum old;
3346 table = get_table(table_name);
3347 row = must_get_row(ctx, table, record_id);
3348 die_if_error(get_column(table, column_name, &column));
3350 type = &column->type;
3351 ovsdb_datum_clone(&old, ovsdb_idl_read(row, column), &column->type);
3352 for (i = 4; i < ctx->argc; i++) {
3353 struct ovsdb_type add_type;
3354 struct ovsdb_datum add;
3358 add_type.n_max = UINT_MAX;
3359 die_if_error(ovsdb_datum_from_string(&add, &add_type, ctx->argv[i],
3361 ovsdb_datum_union(&old, &add, type, false);
3362 ovsdb_datum_destroy(&add, type);
3364 if (old.n > type->n_max) {
3365 vsctl_fatal("\"add\" operation would put %u %s in column %s of "
3366 "table %s but the maximum number is %u",
3368 type->value.type == OVSDB_TYPE_VOID ? "values" : "pairs",
3369 column->name, table->class->name, type->n_max);
3371 ovsdb_idl_txn_verify(row, column);
3372 ovsdb_idl_txn_write(row, column, &old);
3374 vsctl_context_invalidate_cache(ctx);
3378 pre_cmd_remove(struct vsctl_context *ctx)
3380 const char *table_name = ctx->argv[1];
3381 const char *column_name = ctx->argv[3];
3382 const struct vsctl_table_class *table;
3383 const struct ovsdb_idl_column *column;
3385 table = pre_get_table(ctx, table_name);
3386 pre_get_column(ctx, table, column_name, &column);
3387 check_mutable(table, column);
3391 cmd_remove(struct vsctl_context *ctx)
3393 const char *table_name = ctx->argv[1];
3394 const char *record_id = ctx->argv[2];
3395 const char *column_name = ctx->argv[3];
3396 const struct vsctl_table_class *table;
3397 const struct ovsdb_idl_column *column;
3398 const struct ovsdb_idl_row *row;
3399 const struct ovsdb_type *type;
3400 struct ovsdb_datum old;
3403 table = get_table(table_name);
3404 row = must_get_row(ctx, table, record_id);
3405 die_if_error(get_column(table, column_name, &column));
3407 type = &column->type;
3408 ovsdb_datum_clone(&old, ovsdb_idl_read(row, column), &column->type);
3409 for (i = 4; i < ctx->argc; i++) {
3410 struct ovsdb_type rm_type;
3411 struct ovsdb_datum rm;
3416 rm_type.n_max = UINT_MAX;
3417 error = ovsdb_datum_from_string(&rm, &rm_type,
3418 ctx->argv[i], ctx->symtab);
3419 if (error && ovsdb_type_is_map(&rm_type)) {
3421 rm_type.value.type = OVSDB_TYPE_VOID;
3422 die_if_error(ovsdb_datum_from_string(&rm, &rm_type,
3423 ctx->argv[i], ctx->symtab));
3425 ovsdb_datum_subtract(&old, type, &rm, &rm_type);
3426 ovsdb_datum_destroy(&rm, &rm_type);
3428 if (old.n < type->n_min) {
3429 vsctl_fatal("\"remove\" operation would put %u %s in column %s of "
3430 "table %s but the minimum number is %u",
3432 type->value.type == OVSDB_TYPE_VOID ? "values" : "pairs",
3433 column->name, table->class->name, type->n_min);
3435 ovsdb_idl_txn_verify(row, column);
3436 ovsdb_idl_txn_write(row, column, &old);
3438 vsctl_context_invalidate_cache(ctx);
3442 pre_cmd_clear(struct vsctl_context *ctx)
3444 const char *table_name = ctx->argv[1];
3445 const struct vsctl_table_class *table;
3448 table = pre_get_table(ctx, table_name);
3449 for (i = 3; i < ctx->argc; i++) {
3450 const struct ovsdb_idl_column *column;
3452 pre_get_column(ctx, table, ctx->argv[i], &column);
3453 check_mutable(table, column);
3458 cmd_clear(struct vsctl_context *ctx)
3460 const char *table_name = ctx->argv[1];
3461 const char *record_id = ctx->argv[2];
3462 const struct vsctl_table_class *table;
3463 const struct ovsdb_idl_row *row;
3466 table = get_table(table_name);
3467 row = must_get_row(ctx, table, record_id);
3468 for (i = 3; i < ctx->argc; i++) {
3469 const struct ovsdb_idl_column *column;
3470 const struct ovsdb_type *type;
3471 struct ovsdb_datum datum;
3473 die_if_error(get_column(table, ctx->argv[i], &column));
3475 type = &column->type;
3476 if (type->n_min > 0) {
3477 vsctl_fatal("\"clear\" operation cannot be applied to column %s "
3478 "of table %s, which is not allowed to be empty",
3479 column->name, table->class->name);
3482 ovsdb_datum_init_empty(&datum);
3483 ovsdb_idl_txn_write(row, column, &datum);
3486 vsctl_context_invalidate_cache(ctx);
3490 pre_create(struct vsctl_context *ctx)
3492 const char *id = shash_find_data(&ctx->options, "--id");
3493 const char *table_name = ctx->argv[1];
3494 const struct vsctl_table_class *table;
3496 table = get_table(table_name);
3497 if (!id && !table->class->is_root) {
3498 VLOG_WARN("applying \"create\" command to table %s without --id "
3499 "option will have no effect", table->class->name);
3504 cmd_create(struct vsctl_context *ctx)
3506 const char *id = shash_find_data(&ctx->options, "--id");
3507 const char *table_name = ctx->argv[1];
3508 const struct vsctl_table_class *table = get_table(table_name);
3509 const struct ovsdb_idl_row *row;
3510 const struct uuid *uuid;
3514 struct ovsdb_symbol *symbol = create_symbol(ctx->symtab, id, NULL);
3515 if (table->class->is_root) {
3516 /* This table is in the root set, meaning that rows created in it
3517 * won't disappear even if they are unreferenced, so disable
3518 * warnings about that by pretending that there is a reference. */
3519 symbol->strong_ref = true;
3521 uuid = &symbol->uuid;
3526 row = ovsdb_idl_txn_insert(ctx->txn, table->class, uuid);
3527 for (i = 2; i < ctx->argc; i++) {
3528 set_column(table, row, ctx->argv[i], ctx->symtab);
3530 ds_put_format(&ctx->output, UUID_FMT, UUID_ARGS(&row->uuid));
3533 /* This function may be used as the 'postprocess' function for commands that
3534 * insert new rows into the database. It expects that the command's 'run'
3535 * function prints the UUID reported by ovsdb_idl_txn_insert() as the command's
3536 * sole output. It replaces that output by the row's permanent UUID assigned
3537 * by the database server and appends a new-line.
3539 * Currently we use this only for "create", because the higher-level commands
3540 * are supposed to be independent of the actual structure of the vswitch
3543 post_create(struct vsctl_context *ctx)
3545 const struct uuid *real;
3548 if (!uuid_from_string(&dummy, ds_cstr(&ctx->output))) {
3551 real = ovsdb_idl_txn_get_insert_uuid(ctx->txn, &dummy);
3553 ds_clear(&ctx->output);
3554 ds_put_format(&ctx->output, UUID_FMT, UUID_ARGS(real));
3556 ds_put_char(&ctx->output, '\n');
3560 pre_cmd_destroy(struct vsctl_context *ctx)
3562 const char *table_name = ctx->argv[1];
3564 pre_get_table(ctx, table_name);
3568 cmd_destroy(struct vsctl_context *ctx)
3570 bool must_exist = !shash_find(&ctx->options, "--if-exists");
3571 bool delete_all = shash_find(&ctx->options, "--all");
3572 const char *table_name = ctx->argv[1];
3573 const struct vsctl_table_class *table;
3576 table = get_table(table_name);
3578 if (delete_all && ctx->argc > 2) {
3579 vsctl_fatal("--all and records argument should not be specified together");
3582 if (delete_all && !must_exist) {
3583 vsctl_fatal("--all and --if-exists should not be specified together");
3587 const struct ovsdb_idl_row *row;
3588 const struct ovsdb_idl_row *next_row;
3590 for (row = ovsdb_idl_first_row(ctx->idl, table->class);
3592 next_row = ovsdb_idl_next_row(row);
3593 ovsdb_idl_txn_delete(row);
3597 for (i = 2; i < ctx->argc; i++) {
3598 const struct ovsdb_idl_row *row;
3600 row = (must_exist ? must_get_row : get_row)(ctx, table, ctx->argv[i]);
3602 ovsdb_idl_txn_delete(row);
3606 vsctl_context_invalidate_cache(ctx);
3610 RELOP(RELOP_EQ, "=") \
3611 RELOP(RELOP_NE, "!=") \
3612 RELOP(RELOP_LT, "<") \
3613 RELOP(RELOP_GT, ">") \
3614 RELOP(RELOP_LE, "<=") \
3615 RELOP(RELOP_GE, ">=") \
3616 RELOP(RELOP_SET_EQ, "{=}") \
3617 RELOP(RELOP_SET_NE, "{!=}") \
3618 RELOP(RELOP_SET_LT, "{<}") \
3619 RELOP(RELOP_SET_GT, "{>}") \
3620 RELOP(RELOP_SET_LE, "{<=}") \
3621 RELOP(RELOP_SET_GE, "{>=}")
3624 #define RELOP(ENUM, STRING) ENUM,
3630 is_set_operator(enum relop op)
3632 return (op == RELOP_SET_EQ || op == RELOP_SET_NE ||
3633 op == RELOP_SET_LT || op == RELOP_SET_GT ||
3634 op == RELOP_SET_LE || op == RELOP_SET_GE);
3638 evaluate_relop(const struct ovsdb_datum *a, const struct ovsdb_datum *b,
3639 const struct ovsdb_type *type, enum relop op)
3644 return ovsdb_datum_compare_3way(a, b, type) == 0;
3647 return ovsdb_datum_compare_3way(a, b, type) != 0;
3649 return ovsdb_datum_compare_3way(a, b, type) < 0;
3651 return ovsdb_datum_compare_3way(a, b, type) > 0;
3653 return ovsdb_datum_compare_3way(a, b, type) <= 0;
3655 return ovsdb_datum_compare_3way(a, b, type) >= 0;
3658 return b->n > a->n && ovsdb_datum_includes_all(a, b, type);
3660 return a->n > b->n && ovsdb_datum_includes_all(b, a, type);
3662 return ovsdb_datum_includes_all(a, b, type);
3664 return ovsdb_datum_includes_all(b, a, type);
3672 is_condition_satisfied(const struct vsctl_table_class *table,
3673 const struct ovsdb_idl_row *row, const char *arg,
3674 struct ovsdb_symbol_table *symtab)
3676 static const char *operators[] = {
3677 #define RELOP(ENUM, STRING) STRING,
3682 const struct ovsdb_idl_column *column;
3683 const struct ovsdb_datum *have_datum;
3684 char *key_string, *value_string;
3685 struct ovsdb_type type;
3690 error = parse_column_key_value(arg, table, &column, &key_string,
3691 &operator, operators, ARRAY_SIZE(operators),
3693 die_if_error(error);
3694 if (!value_string) {
3695 vsctl_fatal("%s: missing value", arg);
3698 type = column->type;
3699 type.n_max = UINT_MAX;
3701 have_datum = ovsdb_idl_read(row, column);
3703 union ovsdb_atom want_key;
3704 struct ovsdb_datum b;
3707 if (column->type.value.type == OVSDB_TYPE_VOID) {
3708 vsctl_fatal("cannot specify key to check for non-map column %s",
3712 die_if_error(ovsdb_atom_from_string(&want_key, &column->type.key,
3713 key_string, symtab));
3715 type.key = type.value;
3716 type.value.type = OVSDB_TYPE_VOID;
3717 die_if_error(ovsdb_datum_from_string(&b, &type, value_string, symtab));
3719 idx = ovsdb_datum_find_key(have_datum,
3720 &want_key, column->type.key.type);
3721 if (idx == UINT_MAX && !is_set_operator(operator)) {
3724 struct ovsdb_datum a;
3726 if (idx != UINT_MAX) {
3728 a.keys = &have_datum->values[idx];
3736 retval = evaluate_relop(&a, &b, &type, operator);
3739 ovsdb_atom_destroy(&want_key, column->type.key.type);
3740 ovsdb_datum_destroy(&b, &type);
3742 struct ovsdb_datum want_datum;
3744 die_if_error(ovsdb_datum_from_string(&want_datum, &column->type,
3745 value_string, symtab));
3746 retval = evaluate_relop(have_datum, &want_datum, &type, operator);
3747 ovsdb_datum_destroy(&want_datum, &column->type);
3757 pre_cmd_wait_until(struct vsctl_context *ctx)
3759 const char *table_name = ctx->argv[1];
3760 const struct vsctl_table_class *table;
3763 table = pre_get_table(ctx, table_name);
3765 for (i = 3; i < ctx->argc; i++) {
3766 pre_parse_column_key_value(ctx, ctx->argv[i], table);
3771 cmd_wait_until(struct vsctl_context *ctx)
3773 const char *table_name = ctx->argv[1];
3774 const char *record_id = ctx->argv[2];
3775 const struct vsctl_table_class *table;
3776 const struct ovsdb_idl_row *row;
3779 table = get_table(table_name);
3781 row = get_row(ctx, table, record_id);
3783 ctx->try_again = true;
3787 for (i = 3; i < ctx->argc; i++) {
3788 if (!is_condition_satisfied(table, row, ctx->argv[i], ctx->symtab)) {
3789 ctx->try_again = true;
3795 /* Prepares 'ctx', which has already been initialized with
3796 * vsctl_context_init(), for processing 'command'. */
3798 vsctl_context_init_command(struct vsctl_context *ctx,
3799 struct vsctl_command *command)
3801 ctx->argc = command->argc;
3802 ctx->argv = command->argv;
3803 ctx->options = command->options;
3805 ds_swap(&ctx->output, &command->output);
3806 ctx->table = command->table;
3808 ctx->verified_ports = false;
3810 ctx->try_again = false;
3813 /* Prepares 'ctx' for processing commands, initializing its members with the
3814 * values passed in as arguments.
3816 * If 'command' is nonnull, calls vsctl_context_init_command() to prepare for
3817 * that particular command. */
3819 vsctl_context_init(struct vsctl_context *ctx, struct vsctl_command *command,
3820 struct ovsdb_idl *idl, struct ovsdb_idl_txn *txn,
3821 const struct ovsrec_open_vswitch *ovs,
3822 struct ovsdb_symbol_table *symtab)
3825 vsctl_context_init_command(ctx, command);
3830 ctx->symtab = symtab;
3831 ctx->cache_valid = false;
3834 /* Completes processing of 'command' within 'ctx'. */
3836 vsctl_context_done_command(struct vsctl_context *ctx,
3837 struct vsctl_command *command)
3839 ds_swap(&ctx->output, &command->output);
3840 command->table = ctx->table;
3843 /* Finishes up with 'ctx'.
3845 * If command is nonnull, first calls vsctl_context_done_command() to complete
3846 * processing that command within 'ctx'. */
3848 vsctl_context_done(struct vsctl_context *ctx, struct vsctl_command *command)
3851 vsctl_context_done_command(ctx, command);
3853 vsctl_context_invalidate_cache(ctx);
3857 run_prerequisites(struct vsctl_command *commands, size_t n_commands,
3858 struct ovsdb_idl *idl)
3860 struct vsctl_command *c;
3862 ovsdb_idl_add_table(idl, &ovsrec_table_open_vswitch);
3863 if (wait_for_reload) {
3864 ovsdb_idl_add_column(idl, &ovsrec_open_vswitch_col_cur_cfg);
3866 for (c = commands; c < &commands[n_commands]; c++) {
3867 if (c->syntax->prerequisites) {
3868 struct vsctl_context ctx;
3870 ds_init(&c->output);
3873 vsctl_context_init(&ctx, c, idl, NULL, NULL, NULL);
3874 (c->syntax->prerequisites)(&ctx);
3875 vsctl_context_done(&ctx, c);
3877 assert(!c->output.string);
3884 do_vsctl(const char *args, struct vsctl_command *commands, size_t n_commands,
3885 struct ovsdb_idl *idl)
3887 struct ovsdb_idl_txn *txn;
3888 const struct ovsrec_open_vswitch *ovs;
3889 enum ovsdb_idl_txn_status status;
3890 struct ovsdb_symbol_table *symtab;
3891 struct vsctl_context ctx;
3892 struct vsctl_command *c;
3893 struct shash_node *node;
3894 int64_t next_cfg = 0;
3897 txn = the_idl_txn = ovsdb_idl_txn_create(idl);
3899 ovsdb_idl_txn_set_dry_run(txn);
3902 ovsdb_idl_txn_add_comment(txn, "ovs-vsctl: %s", args);
3904 ovs = ovsrec_open_vswitch_first(idl);
3906 /* XXX add verification that table is empty */
3907 ovs = ovsrec_open_vswitch_insert(txn);
3910 if (wait_for_reload) {
3911 ovsdb_idl_txn_increment(txn, &ovs->header_,
3912 &ovsrec_open_vswitch_col_next_cfg);
3915 symtab = ovsdb_symbol_table_create();
3916 for (c = commands; c < &commands[n_commands]; c++) {
3917 ds_init(&c->output);
3920 vsctl_context_init(&ctx, NULL, idl, txn, ovs, symtab);
3921 for (c = commands; c < &commands[n_commands]; c++) {
3922 vsctl_context_init_command(&ctx, c);
3923 if (c->syntax->run) {
3924 (c->syntax->run)(&ctx);
3926 vsctl_context_done_command(&ctx, c);
3928 if (ctx.try_again) {
3929 vsctl_context_done(&ctx, NULL);
3933 vsctl_context_done(&ctx, NULL);
3935 SHASH_FOR_EACH (node, &symtab->sh) {
3936 struct ovsdb_symbol *symbol = node->data;
3937 if (!symbol->created) {
3938 vsctl_fatal("row id \"%s\" is referenced but never created (e.g. "
3939 "with \"-- --id=%s create ...\")",
3940 node->name, node->name);
3942 if (!symbol->strong_ref) {
3943 if (!symbol->weak_ref) {
3944 VLOG_WARN("row id \"%s\" was created but no reference to it "
3945 "was inserted, so it will not actually appear in "
3946 "the database", node->name);
3948 VLOG_WARN("row id \"%s\" was created but only a weak "
3949 "reference to it was inserted, so it will not "
3950 "actually appear in the database", node->name);
3955 status = ovsdb_idl_txn_commit_block(txn);
3956 if (wait_for_reload && status == TXN_SUCCESS) {
3957 next_cfg = ovsdb_idl_txn_get_increment_new_value(txn);
3959 if (status == TXN_UNCHANGED || status == TXN_SUCCESS) {
3960 for (c = commands; c < &commands[n_commands]; c++) {
3961 if (c->syntax->postprocess) {
3962 struct vsctl_context ctx;
3964 vsctl_context_init(&ctx, c, idl, txn, ovs, symtab);
3965 (c->syntax->postprocess)(&ctx);
3966 vsctl_context_done(&ctx, c);
3970 error = xstrdup(ovsdb_idl_txn_get_error(txn));
3971 ovsdb_idl_txn_destroy(txn);
3972 txn = the_idl_txn = NULL;
3975 case TXN_UNCOMMITTED:
3976 case TXN_INCOMPLETE:
3980 /* Should not happen--we never call ovsdb_idl_txn_abort(). */
3981 vsctl_fatal("transaction aborted");
3991 vsctl_fatal("transaction error: %s", error);
3993 case TXN_NOT_LOCKED:
3994 /* Should not happen--we never call ovsdb_idl_set_lock(). */
3995 vsctl_fatal("database not locked");
4002 ovsdb_symbol_table_destroy(symtab);
4004 for (c = commands; c < &commands[n_commands]; c++) {
4005 struct ds *ds = &c->output;
4008 table_print(c->table, &table_style);
4009 } else if (oneline) {
4013 for (j = 0; j < ds->length; j++) {
4014 int ch = ds->string[j];
4017 fputs("\\n", stdout);
4021 fputs("\\\\", stdout);
4030 fputs(ds_cstr(ds), stdout);
4032 ds_destroy(&c->output);
4033 table_destroy(c->table);
4036 shash_destroy_free_data(&c->options);
4040 if (wait_for_reload && status != TXN_UNCHANGED) {
4043 OVSREC_OPEN_VSWITCH_FOR_EACH (ovs, idl) {
4044 if (ovs->cur_cfg >= next_cfg) {
4048 ovsdb_idl_wait(idl);
4053 ovsdb_idl_destroy(idl);
4058 /* Our transaction needs to be rerun, or a prerequisite was not met. Free
4059 * resources and return so that the caller can try again. */
4061 ovsdb_idl_txn_abort(txn);
4062 ovsdb_idl_txn_destroy(txn);
4064 ovsdb_symbol_table_destroy(symtab);
4065 for (c = commands; c < &commands[n_commands]; c++) {
4066 ds_destroy(&c->output);
4067 table_destroy(c->table);
4073 static const struct vsctl_command_syntax all_commands[] = {
4074 /* Open vSwitch commands. */
4075 {"init", 0, 0, NULL, cmd_init, NULL, "", RW},
4076 {"show", 0, 0, pre_cmd_show, cmd_show, NULL, "", RO},
4078 /* Bridge commands. */
4079 {"add-br", 1, 3, pre_get_info, cmd_add_br, NULL, "--may-exist", RW},
4080 {"del-br", 1, 1, pre_get_info, cmd_del_br, NULL, "--if-exists", RW},
4081 {"list-br", 0, 0, pre_get_info, cmd_list_br, NULL, "--real,--fake", RO},
4082 {"br-exists", 1, 1, pre_get_info, cmd_br_exists, NULL, "", RO},
4083 {"br-to-vlan", 1, 1, pre_get_info, cmd_br_to_vlan, NULL, "", RO},
4084 {"br-to-parent", 1, 1, pre_get_info, cmd_br_to_parent, NULL, "", RO},
4085 {"br-set-external-id", 2, 3, pre_cmd_br_set_external_id,
4086 cmd_br_set_external_id, NULL, "", RW},
4087 {"br-get-external-id", 1, 2, pre_cmd_br_get_external_id,
4088 cmd_br_get_external_id, NULL, "", RO},
4090 /* Port commands. */
4091 {"list-ports", 1, 1, pre_get_info, cmd_list_ports, NULL, "", RO},
4092 {"add-port", 2, INT_MAX, pre_get_info, cmd_add_port, NULL, "--may-exist",
4094 {"add-bond", 4, INT_MAX, pre_get_info, cmd_add_bond, NULL,
4095 "--may-exist,--fake-iface", RW},
4096 {"del-port", 1, 2, pre_get_info, cmd_del_port, NULL,
4097 "--if-exists,--with-iface", RW},
4098 {"port-to-br", 1, 1, pre_get_info, cmd_port_to_br, NULL, "", RO},
4100 /* Interface commands. */
4101 {"list-ifaces", 1, 1, pre_get_info, cmd_list_ifaces, NULL, "", RO},
4102 {"iface-to-br", 1, 1, pre_get_info, cmd_iface_to_br, NULL, "", RO},
4104 /* Controller commands. */
4105 {"get-controller", 1, 1, pre_controller, cmd_get_controller, NULL, "", RO},
4106 {"del-controller", 1, 1, pre_controller, cmd_del_controller, NULL, "", RW},
4107 {"set-controller", 1, INT_MAX, pre_controller, cmd_set_controller, NULL,
4109 {"get-fail-mode", 1, 1, pre_get_info, cmd_get_fail_mode, NULL, "", RO},
4110 {"del-fail-mode", 1, 1, pre_get_info, cmd_del_fail_mode, NULL, "", RW},
4111 {"set-fail-mode", 2, 2, pre_get_info, cmd_set_fail_mode, NULL, "", RW},
4113 /* Manager commands. */
4114 {"get-manager", 0, 0, pre_manager, cmd_get_manager, NULL, "", RO},
4115 {"del-manager", 0, 0, pre_manager, cmd_del_manager, NULL, "", RW},
4116 {"set-manager", 1, INT_MAX, pre_manager, cmd_set_manager, NULL, "", RW},
4119 {"get-ssl", 0, 0, pre_cmd_get_ssl, cmd_get_ssl, NULL, "", RO},
4120 {"del-ssl", 0, 0, pre_cmd_del_ssl, cmd_del_ssl, NULL, "", RW},
4121 {"set-ssl", 3, 3, pre_cmd_set_ssl, cmd_set_ssl, NULL, "--bootstrap", RW},
4123 /* Switch commands. */
4124 {"emer-reset", 0, 0, pre_cmd_emer_reset, cmd_emer_reset, NULL, "", RW},
4126 /* Database commands. */
4127 {"comment", 0, INT_MAX, NULL, NULL, NULL, "", RO},
4128 {"get", 2, INT_MAX, pre_cmd_get, cmd_get, NULL, "--if-exists,--id=", RO},
4129 {"list", 1, INT_MAX, pre_cmd_list, cmd_list, NULL, "--columns=", RO},
4130 {"find", 1, INT_MAX, pre_cmd_find, cmd_find, NULL, "--columns=", RO},
4131 {"set", 3, INT_MAX, pre_cmd_set, cmd_set, NULL, "", RW},
4132 {"add", 4, INT_MAX, pre_cmd_add, cmd_add, NULL, "", RW},
4133 {"remove", 4, INT_MAX, pre_cmd_remove, cmd_remove, NULL, "", RW},
4134 {"clear", 3, INT_MAX, pre_cmd_clear, cmd_clear, NULL, "", RW},
4135 {"create", 2, INT_MAX, pre_create, cmd_create, post_create, "--id=", RW},
4136 {"destroy", 1, INT_MAX, pre_cmd_destroy, cmd_destroy, NULL,
4137 "--if-exists,--all", RW},
4138 {"wait-until", 2, INT_MAX, pre_cmd_wait_until, cmd_wait_until, NULL, "",
4141 {NULL, 0, 0, NULL, NULL, NULL, NULL, RO},