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"
45 #include "lib/vswitch-idl.h"
52 VLOG_DEFINE_THIS_MODULE(vsctl);
54 /* vsctl_fatal() also logs the error, so it is preferred in this file. */
55 #define ovs_fatal please_use_vsctl_fatal_instead_of_ovs_fatal
59 /* A command supported by ovs-vsctl. */
60 struct vsctl_command_syntax {
61 const char *name; /* e.g. "add-br" */
62 int min_args; /* Min number of arguments following name. */
63 int max_args; /* Max number of arguments following name. */
65 /* If nonnull, calls ovsdb_idl_add_column() or ovsdb_idl_add_table() for
66 * each column or table in ctx->idl that it uses. */
67 void (*prerequisites)(struct vsctl_context *ctx);
69 /* Does the actual work of the command and puts the command's output, if
70 * any, in ctx->output or ctx->table.
72 * Alternatively, if some prerequisite of the command is not met and the
73 * caller should wait for something to change and then retry, it may set
74 * ctx->try_again to true. (Only the "wait-until" command currently does
76 void (*run)(struct vsctl_context *ctx);
78 /* If nonnull, called after the transaction has been successfully
79 * committed. ctx->output is the output from the "run" function, which
80 * this function may modify and otherwise postprocess as needed. (Only the
81 * "create" command currently does any postprocessing.) */
82 void (*postprocess)(struct vsctl_context *ctx);
84 /* A comma-separated list of supported options, e.g. "--a,--b", or the
85 * empty string if the command does not support any options. */
87 enum { RO, RW } mode; /* Does this command modify the database? */
90 struct vsctl_command {
91 /* Data that remains constant after initialization. */
92 const struct vsctl_command_syntax *syntax;
97 /* Data modified by commands. */
102 /* --db: The database server to contact. */
103 static const char *db;
105 /* --oneline: Write each command's output as a single line? */
108 /* --dry-run: Do not commit any changes. */
111 /* --no-wait: Wait for ovs-vswitchd to reload its configuration? */
112 static bool wait_for_reload = true;
114 /* --timeout: Time to wait for a connection to 'db'. */
117 /* Format for table output. */
118 static struct table_style table_style = TABLE_STYLE_DEFAULT;
120 /* All supported commands. */
121 static const struct vsctl_command_syntax all_commands[];
123 /* The IDL we're using and the current transaction, if any.
124 * This is for use by vsctl_exit() only, to allow it to clean up.
125 * Other code should use its context arguments. */
126 static struct ovsdb_idl *the_idl;
127 static struct ovsdb_idl_txn *the_idl_txn;
129 static void vsctl_exit(int status) NO_RETURN;
130 static void vsctl_fatal(const char *, ...) PRINTF_FORMAT(1, 2) NO_RETURN;
131 static char *default_db(void);
132 static void usage(void) NO_RETURN;
133 static void parse_options(int argc, char *argv[]);
134 static bool might_write_to_db(char **argv);
136 static struct vsctl_command *parse_commands(int argc, char *argv[],
137 size_t *n_commandsp);
138 static void parse_command(int argc, char *argv[], struct vsctl_command *);
139 static const struct vsctl_command_syntax *find_command(const char *name);
140 static void run_prerequisites(struct vsctl_command[], size_t n_commands,
142 static void do_vsctl(const char *args, struct vsctl_command *, size_t n,
145 static const struct vsctl_table_class *get_table(const char *table_name);
146 static void set_column(const struct vsctl_table_class *,
147 const struct ovsdb_idl_row *, const char *arg,
148 struct ovsdb_symbol_table *);
150 static bool is_condition_satisfied(const struct vsctl_table_class *,
151 const struct ovsdb_idl_row *,
153 struct ovsdb_symbol_table *);
156 main(int argc, char *argv[])
158 extern struct vlog_module VLM_reconnect;
159 struct ovsdb_idl *idl;
160 struct vsctl_command *commands;
165 set_program_name(argv[0]);
166 signal(SIGPIPE, SIG_IGN);
167 vlog_set_levels(NULL, VLF_CONSOLE, VLL_WARN);
168 vlog_set_levels(&VLM_reconnect, VLF_ANY_FACILITY, VLL_WARN);
171 /* Log our arguments. This is often valuable for debugging systems. */
172 args = process_escape_args(argv);
173 VLOG(might_write_to_db(argv) ? VLL_INFO : VLL_DBG, "Called as %s", args);
175 /* Parse command line. */
176 parse_options(argc, argv);
177 commands = parse_commands(argc - optind, argv + optind, &n_commands);
183 /* Initialize IDL. */
184 idl = the_idl = ovsdb_idl_create(db, &ovsrec_idl_class, false);
185 run_prerequisites(commands, n_commands, idl);
187 /* Execute the commands.
189 * 'seqno' is the database sequence number for which we last tried to
190 * execute our transaction. There's no point in trying to commit more than
191 * once for any given sequence number, because if the transaction fails
192 * it's because the database changed and we need to obtain an up-to-date
193 * view of the database before we try the transaction again. */
194 seqno = ovsdb_idl_get_seqno(idl);
198 if (seqno != ovsdb_idl_get_seqno(idl)) {
199 seqno = ovsdb_idl_get_seqno(idl);
200 do_vsctl(args, commands, n_commands, idl);
203 if (seqno == ovsdb_idl_get_seqno(idl)) {
211 parse_options(int argc, char *argv[])
214 OPT_DB = UCHAR_MAX + 1,
223 static struct option long_options[] = {
224 {"db", required_argument, NULL, OPT_DB},
225 {"no-syslog", no_argument, NULL, OPT_NO_SYSLOG},
226 {"no-wait", no_argument, NULL, OPT_NO_WAIT},
227 {"dry-run", no_argument, NULL, OPT_DRY_RUN},
228 {"oneline", no_argument, NULL, OPT_ONELINE},
229 {"timeout", required_argument, NULL, 't'},
230 {"help", no_argument, NULL, 'h'},
231 {"version", no_argument, NULL, 'V'},
234 STREAM_SSL_LONG_OPTIONS,
235 {"peer-ca-cert", required_argument, NULL, OPT_PEER_CA_CERT},
238 char *tmp, *short_options;
240 tmp = long_options_to_short_options(long_options);
241 short_options = xasprintf("+%s", tmp);
244 table_style.format = TF_LIST;
249 c = getopt_long(argc, argv, short_options, long_options, NULL);
264 vlog_set_levels(&VLM_vsctl, VLF_SYSLOG, VLL_WARN);
268 wait_for_reload = false;
279 ovs_print_version(0, 0);
283 timeout = strtoul(optarg, NULL, 10);
285 vsctl_fatal("value %s on -t or --timeout is invalid",
291 TABLE_OPTION_HANDLERS(&table_style)
293 STREAM_SSL_OPTION_HANDLERS
295 case OPT_PEER_CA_CERT:
296 stream_ssl_set_peer_ca_cert_file(optarg);
313 static struct vsctl_command *
314 parse_commands(int argc, char *argv[], size_t *n_commandsp)
316 struct vsctl_command *commands;
317 size_t n_commands, allocated_commands;
321 n_commands = allocated_commands = 0;
323 for (start = i = 0; i <= argc; i++) {
324 if (i == argc || !strcmp(argv[i], "--")) {
326 if (n_commands >= allocated_commands) {
327 struct vsctl_command *c;
329 commands = x2nrealloc(commands, &allocated_commands,
331 for (c = commands; c < &commands[n_commands]; c++) {
332 shash_moved(&c->options);
335 parse_command(i - start, &argv[start],
336 &commands[n_commands++]);
342 vsctl_fatal("missing command name (use --help for help)");
344 *n_commandsp = n_commands;
349 parse_command(int argc, char *argv[], struct vsctl_command *command)
351 const struct vsctl_command_syntax *p;
352 struct shash_node *node;
356 shash_init(&command->options);
357 for (i = 0; i < argc; i++) {
358 const char *option = argv[i];
362 if (option[0] != '-') {
366 equals = strchr(option, '=');
368 key = xmemdup0(option, equals - option);
369 value = xstrdup(equals + 1);
371 key = xstrdup(option);
375 if (shash_find(&command->options, key)) {
376 vsctl_fatal("'%s' option specified multiple times", argv[i]);
378 shash_add_nocopy(&command->options, key, value);
381 vsctl_fatal("missing command name");
384 p = find_command(argv[i]);
386 vsctl_fatal("unknown command '%s'; use --help for help", argv[i]);
389 SHASH_FOR_EACH (node, &command->options) {
390 const char *s = strstr(p->options, node->name);
391 int end = s ? s[strlen(node->name)] : EOF;
393 if (end != '=' && end != ',' && end != ' ' && end != '\0') {
394 vsctl_fatal("'%s' command has no '%s' option",
395 argv[i], node->name);
397 if ((end == '=') != (node->data != NULL)) {
399 vsctl_fatal("missing argument to '%s' option on '%s' "
400 "command", node->name, argv[i]);
402 vsctl_fatal("'%s' option on '%s' does not accept an "
403 "argument", node->name, argv[i]);
408 n_arg = argc - i - 1;
409 if (n_arg < p->min_args) {
410 vsctl_fatal("'%s' command requires at least %d arguments",
411 p->name, p->min_args);
412 } else if (n_arg > p->max_args) {
415 for (j = i + 1; j < argc; j++) {
416 if (argv[j][0] == '-') {
417 vsctl_fatal("'%s' command takes at most %d arguments "
418 "(note that options must precede command "
419 "names and follow a \"--\" argument)",
420 p->name, p->max_args);
424 vsctl_fatal("'%s' command takes at most %d arguments",
425 p->name, p->max_args);
429 command->argc = n_arg + 1;
430 command->argv = &argv[i];
433 /* Returns the "struct vsctl_command_syntax" for a given command 'name', or a
434 * null pointer if there is none. */
435 static const struct vsctl_command_syntax *
436 find_command(const char *name)
438 static struct shash commands = SHASH_INITIALIZER(&commands);
440 if (shash_is_empty(&commands)) {
441 const struct vsctl_command_syntax *p;
443 for (p = all_commands; p->name; p++) {
444 shash_add_assert(&commands, p->name, p);
448 return shash_find_data(&commands, name);
452 vsctl_fatal(const char *format, ...)
457 va_start(args, format);
458 message = xvasprintf(format, args);
461 vlog_set_levels(&VLM_vsctl, VLF_CONSOLE, VLL_OFF);
462 VLOG_ERR("%s", message);
463 ovs_error(0, "%s", message);
464 vsctl_exit(EXIT_FAILURE);
467 /* Frees the current transaction and the underlying IDL and then calls
470 * Freeing the transaction and the IDL is not strictly necessary, but it makes
471 * for a clean memory leak report from valgrind in the normal case. That makes
472 * it easier to notice real memory leaks. */
474 vsctl_exit(int status)
477 ovsdb_idl_txn_abort(the_idl_txn);
478 ovsdb_idl_txn_destroy(the_idl_txn);
480 ovsdb_idl_destroy(the_idl);
488 %s: ovs-vswitchd management utility\n\
489 usage: %s [OPTIONS] COMMAND [ARG...]\n\
491 Open vSwitch commands:\n\
492 init initialize database, if not yet initialized\n\
493 show print overview of database contents\n\
494 emer-reset reset configuration to clean state\n\
497 add-br BRIDGE create a new bridge named BRIDGE\n\
498 add-br BRIDGE PARENT VLAN create new fake BRIDGE in PARENT on VLAN\n\
499 del-br BRIDGE delete BRIDGE and all of its ports\n\
500 list-br print the names of all the bridges\n\
501 br-exists BRIDGE exit 2 if BRIDGE does not exist\n\
502 br-to-vlan BRIDGE print the VLAN which BRIDGE is on\n\
503 br-to-parent BRIDGE print the parent of BRIDGE\n\
504 br-set-external-id BRIDGE KEY VALUE set KEY on BRIDGE to VALUE\n\
505 br-set-external-id BRIDGE KEY unset KEY on BRIDGE\n\
506 br-get-external-id BRIDGE KEY print value of KEY on BRIDGE\n\
507 br-get-external-id BRIDGE list key-value pairs on BRIDGE\n\
509 Port commands (a bond is considered to be a single port):\n\
510 list-ports BRIDGE print the names of all the ports on BRIDGE\n\
511 add-port BRIDGE PORT add network device PORT to BRIDGE\n\
512 add-bond BRIDGE PORT IFACE... add bonded port PORT in BRIDGE from IFACES\n\
513 del-port [BRIDGE] PORT delete PORT (which may be bonded) from BRIDGE\n\
514 port-to-br PORT print name of bridge that contains PORT\n\
516 Interface commands (a bond consists of multiple interfaces):\n\
517 list-ifaces BRIDGE print the names of all interfaces on BRIDGE\n\
518 iface-to-br IFACE print name of bridge that contains IFACE\n\
520 Controller commands:\n\
521 get-controller BRIDGE print the controllers for BRIDGE\n\
522 del-controller BRIDGE delete the controllers for BRIDGE\n\
523 set-controller BRIDGE TARGET... set the controllers for BRIDGE\n\
524 get-fail-mode BRIDGE print the fail-mode for BRIDGE\n\
525 del-fail-mode BRIDGE delete the fail-mode for BRIDGE\n\
526 set-fail-mode BRIDGE MODE set the fail-mode for BRIDGE to MODE\n\
529 get-manager print the managers\n\
530 del-manager delete the managers\n\
531 set-manager TARGET... set the list of managers to TARGET...\n\
534 get-ssl print the SSL configuration\n\
535 del-ssl delete the SSL configuration\n\
536 set-ssl PRIV-KEY CERT CA-CERT set the SSL configuration\n\
539 emer-reset reset switch to known good state\n\
541 Database commands:\n\
542 list TBL [REC] list RECord (or all records) in TBL\n\
543 find TBL CONDITION... list records satisfying CONDITION in TBL\n\
544 get TBL REC COL[:KEY] print values of COLumns in RECord in TBL\n\
545 set TBL REC COL[:KEY]=VALUE set COLumn values in RECord in TBL\n\
546 add TBL REC COL [KEY=]VALUE add (KEY=)VALUE to COLumn in RECord in TBL\n\
547 remove TBL REC COL [KEY=]VALUE remove (KEY=)VALUE from COLumn\n\
548 clear TBL REC COL clear values from COLumn in RECord in TBL\n\
549 create TBL COL[:KEY]=VALUE create and initialize new record\n\
550 destroy TBL REC delete RECord from TBL\n\
551 wait-until TBL REC [COL[:KEY]=VALUE] wait until condition is true\n\
552 Potentially unsafe database commands require --force option.\n\
555 --db=DATABASE connect to DATABASE\n\
557 --no-wait do not wait for ovs-vswitchd to reconfigure\n\
558 -t, --timeout=SECS wait at most SECS seconds for ovs-vswitchd\n\
559 --dry-run do not commit changes to database\n\
560 --oneline print exactly one line of output per command\n",
561 program_name, program_name, default_db());
564 --no-syslog equivalent to --verbose=vsctl:syslog:warn\n");
565 stream_usage("database", true, true, false);
568 -h, --help display this help message\n\
569 -V, --version display version information\n");
578 def = xasprintf("unix:%s/db.sock", ovs_rundir());
583 /* Returns true if it looks like this set of arguments might modify the
584 * database, otherwise false. (Not very smart, so it's prone to false
587 might_write_to_db(char **argv)
589 for (; *argv; argv++) {
590 const struct vsctl_command_syntax *p = find_command(*argv);
591 if (p && p->mode == RW) {
598 struct vsctl_context {
602 struct shash options;
604 /* Modifiable state. */
607 struct ovsdb_idl *idl;
608 struct ovsdb_idl_txn *txn;
609 struct ovsdb_symbol_table *symtab;
610 const struct ovsrec_open_vswitch *ovs;
613 /* A cache of the contents of the database.
615 * A command that needs to use any of this information must first call
616 * vsctl_context_populate_cache(). A command that changes anything that
617 * could invalidate the cache must either call
618 * vsctl_context_invalidate_cache() or manually update the cache to
619 * maintain its correctness. */
621 struct shash bridges; /* Maps from bridge name to struct vsctl_bridge. */
622 struct shash ports; /* Maps from port name to struct vsctl_port. */
623 struct shash ifaces; /* Maps from port name to struct vsctl_iface. */
625 /* A command may set this member to true if some prerequisite is not met
626 * and the caller should wait for something to change and then retry. */
630 struct vsctl_bridge {
631 struct ovsrec_bridge *br_cfg;
633 struct list ports; /* Contains "struct vsctl_port"s. */
635 /* VLAN ("fake") bridge support.
637 * Use 'parent != NULL' to detect a fake bridge, because 'vlan' can be 0
639 struct hmap children; /* VLAN bridges indexed by 'vlan'. */
640 struct hmap_node children_node; /* Node in parent's 'children' hmap. */
641 struct vsctl_bridge *parent; /* Real bridge, or NULL. */
642 int vlan; /* VLAN VID (0...4095), or 0. */
646 struct list ports_node; /* In struct vsctl_bridge's 'ports' list. */
647 struct list ifaces; /* Contains "struct vsctl_iface"s. */
648 struct ovsrec_port *port_cfg;
649 struct vsctl_bridge *bridge;
653 struct list ifaces_node; /* In struct vsctl_port's 'ifaces' list. */
654 struct ovsrec_interface *iface_cfg;
655 struct vsctl_port *port;
659 vsctl_context_to_string(const struct vsctl_context *ctx)
661 const struct shash_node *node;
667 SHASH_FOR_EACH (node, &ctx->options) {
668 svec_add(&words, node->name);
670 for (i = 0; i < ctx->argc; i++) {
671 svec_add(&words, ctx->argv[i]);
673 svec_terminate(&words);
675 s = process_escape_args(words.names);
677 svec_destroy(&words);
683 verify_ports(struct vsctl_context *ctx)
685 if (!ctx->verified_ports) {
686 const struct ovsrec_bridge *bridge;
687 const struct ovsrec_port *port;
689 ovsrec_open_vswitch_verify_bridges(ctx->ovs);
690 OVSREC_BRIDGE_FOR_EACH (bridge, ctx->idl) {
691 ovsrec_bridge_verify_ports(bridge);
693 OVSREC_PORT_FOR_EACH (port, ctx->idl) {
694 ovsrec_port_verify_interfaces(port);
697 ctx->verified_ports = true;
701 static struct vsctl_bridge *
702 add_bridge_to_cache(struct vsctl_context *ctx,
703 struct ovsrec_bridge *br_cfg, const char *name,
704 struct vsctl_bridge *parent, int vlan)
706 struct vsctl_bridge *br = xmalloc(sizeof *br);
708 br->name = xstrdup(name);
709 list_init(&br->ports);
712 hmap_init(&br->children);
714 hmap_insert(&parent->children, &br->children_node, hash_int(vlan, 0));
716 shash_add(&ctx->bridges, br->name, br);
721 ovs_delete_bridge(const struct ovsrec_open_vswitch *ovs,
722 struct ovsrec_bridge *bridge)
724 struct ovsrec_bridge **bridges;
727 bridges = xmalloc(sizeof *ovs->bridges * ovs->n_bridges);
728 for (i = n = 0; i < ovs->n_bridges; i++) {
729 if (ovs->bridges[i] != bridge) {
730 bridges[n++] = ovs->bridges[i];
733 ovsrec_open_vswitch_set_bridges(ovs, bridges, n);
738 del_cached_bridge(struct vsctl_context *ctx, struct vsctl_bridge *br)
740 assert(list_is_empty(&br->ports));
741 assert(hmap_is_empty(&br->children));
743 hmap_remove(&br->parent->children, &br->children_node);
746 ovsrec_bridge_delete(br->br_cfg);
747 ovs_delete_bridge(ctx->ovs, br->br_cfg);
749 shash_find_and_delete(&ctx->bridges, br->name);
750 hmap_destroy(&br->children);
756 port_is_fake_bridge(const struct ovsrec_port *port_cfg)
758 return (port_cfg->fake_bridge
760 && *port_cfg->tag >= 0 && *port_cfg->tag <= 4095);
763 static struct vsctl_bridge *
764 find_vlan_bridge(struct vsctl_bridge *parent, int vlan)
766 struct vsctl_bridge *child;
768 HMAP_FOR_EACH_IN_BUCKET (child, children_node, hash_int(vlan, 0),
770 if (child->vlan == vlan) {
778 static struct vsctl_port *
779 add_port_to_cache(struct vsctl_context *ctx, struct vsctl_bridge *parent,
780 struct ovsrec_port *port_cfg)
782 struct vsctl_port *port;
785 && *port_cfg->tag >= 0 && *port_cfg->tag <= 4095) {
786 struct vsctl_bridge *vlan_bridge;
788 vlan_bridge = find_vlan_bridge(parent, *port_cfg->tag);
790 parent = vlan_bridge;
794 port = xmalloc(sizeof *port);
795 list_push_back(&parent->ports, &port->ports_node);
796 list_init(&port->ifaces);
797 port->port_cfg = port_cfg;
798 port->bridge = parent;
799 shash_add(&ctx->ports, port_cfg->name, port);
805 del_cached_port(struct vsctl_context *ctx, struct vsctl_port *port)
807 assert(list_is_empty(&port->ifaces));
808 list_remove(&port->ports_node);
809 shash_find_and_delete(&ctx->ports, port->port_cfg->name);
810 ovsrec_port_delete(port->port_cfg);
814 static struct vsctl_iface *
815 add_iface_to_cache(struct vsctl_context *ctx, struct vsctl_port *parent,
816 struct ovsrec_interface *iface_cfg)
818 struct vsctl_iface *iface;
820 iface = xmalloc(sizeof *iface);
821 list_push_back(&parent->ifaces, &iface->ifaces_node);
822 iface->iface_cfg = iface_cfg;
823 iface->port = parent;
824 shash_add(&ctx->ifaces, iface_cfg->name, iface);
830 del_cached_iface(struct vsctl_context *ctx, struct vsctl_iface *iface)
832 list_remove(&iface->ifaces_node);
833 shash_find_and_delete(&ctx->ifaces, iface->iface_cfg->name);
834 ovsrec_interface_delete(iface->iface_cfg);
839 vsctl_context_invalidate_cache(struct vsctl_context *ctx)
841 struct shash_node *node;
843 if (!ctx->cache_valid) {
846 ctx->cache_valid = false;
848 SHASH_FOR_EACH (node, &ctx->bridges) {
849 struct vsctl_bridge *bridge = node->data;
850 hmap_destroy(&bridge->children);
854 shash_destroy(&ctx->bridges);
856 shash_destroy_free_data(&ctx->ports);
857 shash_destroy_free_data(&ctx->ifaces);
861 pre_get_info(struct vsctl_context *ctx)
863 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_bridges);
865 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_name);
866 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_controller);
867 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_fail_mode);
868 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_ports);
870 ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_name);
871 ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_fake_bridge);
872 ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_tag);
873 ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_interfaces);
875 ovsdb_idl_add_column(ctx->idl, &ovsrec_interface_col_name);
879 vsctl_context_populate_cache(struct vsctl_context *ctx)
881 const struct ovsrec_open_vswitch *ovs = ctx->ovs;
882 struct sset bridges, ports;
885 if (ctx->cache_valid) {
886 /* Cache is already populated. */
889 ctx->cache_valid = true;
890 shash_init(&ctx->bridges);
891 shash_init(&ctx->ports);
892 shash_init(&ctx->ifaces);
896 for (i = 0; i < ovs->n_bridges; i++) {
897 struct ovsrec_bridge *br_cfg = ovs->bridges[i];
898 struct vsctl_bridge *br;
901 if (!sset_add(&bridges, br_cfg->name)) {
902 VLOG_WARN("%s: database contains duplicate bridge name",
906 br = add_bridge_to_cache(ctx, br_cfg, br_cfg->name, NULL, 0);
911 for (j = 0; j < br_cfg->n_ports; j++) {
912 struct ovsrec_port *port_cfg = br_cfg->ports[j];
914 if (!sset_add(&ports, port_cfg->name)) {
915 /* Duplicate port name. (We will warn about that later.) */
919 if (port_is_fake_bridge(port_cfg)
920 && sset_add(&bridges, port_cfg->name)) {
921 add_bridge_to_cache(ctx, NULL, port_cfg->name, br,
926 sset_destroy(&bridges);
927 sset_destroy(&ports);
930 for (i = 0; i < ovs->n_bridges; i++) {
931 struct ovsrec_bridge *br_cfg = ovs->bridges[i];
932 struct vsctl_bridge *br;
935 if (!sset_add(&bridges, br_cfg->name)) {
938 br = shash_find_data(&ctx->bridges, br_cfg->name);
939 for (j = 0; j < br_cfg->n_ports; j++) {
940 struct ovsrec_port *port_cfg = br_cfg->ports[j];
941 struct vsctl_port *port;
944 port = shash_find_data(&ctx->ports, port_cfg->name);
946 if (port_cfg == port->port_cfg) {
947 VLOG_WARN("%s: port is in multiple bridges (%s and %s)",
948 port_cfg->name, br->name, port->bridge->name);
950 /* Log as an error because this violates the database's
951 * uniqueness constraints, so the database server shouldn't
952 * have allowed it. */
953 VLOG_ERR("%s: database contains duplicate port name",
959 if (port_is_fake_bridge(port_cfg)
960 && !sset_add(&bridges, port_cfg->name)) {
964 port = add_port_to_cache(ctx, br, port_cfg);
965 for (k = 0; k < port_cfg->n_interfaces; k++) {
966 struct ovsrec_interface *iface_cfg = port_cfg->interfaces[k];
967 struct vsctl_iface *iface;
969 iface = shash_find_data(&ctx->ifaces, iface_cfg->name);
971 if (iface_cfg == iface->iface_cfg) {
972 VLOG_WARN("%s: interface is in multiple ports "
975 iface->port->port_cfg->name,
976 port->port_cfg->name);
978 /* Log as an error because this violates the database's
979 * uniqueness constraints, so the database server
980 * shouldn't have allowed it. */
981 VLOG_ERR("%s: database contains duplicate interface "
982 "name", iface_cfg->name);
987 add_iface_to_cache(ctx, port, iface_cfg);
991 sset_destroy(&bridges);
995 check_conflicts(struct vsctl_context *ctx, const char *name,
998 struct vsctl_iface *iface;
999 struct vsctl_port *port;
1003 if (shash_find(&ctx->bridges, name)) {
1004 vsctl_fatal("%s because a bridge named %s already exists",
1008 port = shash_find_data(&ctx->ports, name);
1010 vsctl_fatal("%s because a port named %s already exists on "
1011 "bridge %s", msg, name, port->bridge->name);
1014 iface = shash_find_data(&ctx->ifaces, name);
1016 vsctl_fatal("%s because an interface named %s already exists "
1017 "on bridge %s", msg, name, iface->port->bridge->name);
1023 static struct vsctl_bridge *
1024 find_bridge(struct vsctl_context *ctx, const char *name, bool must_exist)
1026 struct vsctl_bridge *br;
1028 assert(ctx->cache_valid);
1030 br = shash_find_data(&ctx->bridges, name);
1031 if (must_exist && !br) {
1032 vsctl_fatal("no bridge named %s", name);
1034 ovsrec_open_vswitch_verify_bridges(ctx->ovs);
1038 static struct vsctl_bridge *
1039 find_real_bridge(struct vsctl_context *ctx, const char *name, bool must_exist)
1041 struct vsctl_bridge *br = find_bridge(ctx, name, must_exist);
1042 if (br && br->parent) {
1043 vsctl_fatal("%s is a fake bridge", name);
1048 static struct vsctl_port *
1049 find_port(struct vsctl_context *ctx, const char *name, bool must_exist)
1051 struct vsctl_port *port;
1053 assert(ctx->cache_valid);
1055 port = shash_find_data(&ctx->ports, name);
1056 if (port && !strcmp(name, port->bridge->name)) {
1059 if (must_exist && !port) {
1060 vsctl_fatal("no port named %s", name);
1066 static struct vsctl_iface *
1067 find_iface(struct vsctl_context *ctx, const char *name, bool must_exist)
1069 struct vsctl_iface *iface;
1071 assert(ctx->cache_valid);
1073 iface = shash_find_data(&ctx->ifaces, name);
1074 if (iface && !strcmp(name, iface->port->bridge->name)) {
1077 if (must_exist && !iface) {
1078 vsctl_fatal("no interface named %s", name);
1085 bridge_insert_port(struct ovsrec_bridge *br, struct ovsrec_port *port)
1087 struct ovsrec_port **ports;
1090 ports = xmalloc(sizeof *br->ports * (br->n_ports + 1));
1091 for (i = 0; i < br->n_ports; i++) {
1092 ports[i] = br->ports[i];
1094 ports[br->n_ports] = port;
1095 ovsrec_bridge_set_ports(br, ports, br->n_ports + 1);
1100 bridge_delete_port(struct ovsrec_bridge *br, struct ovsrec_port *port)
1102 struct ovsrec_port **ports;
1105 ports = xmalloc(sizeof *br->ports * br->n_ports);
1106 for (i = n = 0; i < br->n_ports; i++) {
1107 if (br->ports[i] != port) {
1108 ports[n++] = br->ports[i];
1111 ovsrec_bridge_set_ports(br, ports, n);
1116 ovs_insert_bridge(const struct ovsrec_open_vswitch *ovs,
1117 struct ovsrec_bridge *bridge)
1119 struct ovsrec_bridge **bridges;
1122 bridges = xmalloc(sizeof *ovs->bridges * (ovs->n_bridges + 1));
1123 for (i = 0; i < ovs->n_bridges; i++) {
1124 bridges[i] = ovs->bridges[i];
1126 bridges[ovs->n_bridges] = bridge;
1127 ovsrec_open_vswitch_set_bridges(ovs, bridges, ovs->n_bridges + 1);
1132 cmd_init(struct vsctl_context *ctx OVS_UNUSED)
1136 struct cmd_show_table {
1137 const struct ovsdb_idl_table_class *table;
1138 const struct ovsdb_idl_column *name_column;
1139 const struct ovsdb_idl_column *columns[3];
1143 static struct cmd_show_table cmd_show_tables[] = {
1144 {&ovsrec_table_open_vswitch,
1146 {&ovsrec_open_vswitch_col_manager_options,
1147 &ovsrec_open_vswitch_col_bridges,
1148 &ovsrec_open_vswitch_col_ovs_version},
1151 {&ovsrec_table_bridge,
1152 &ovsrec_bridge_col_name,
1153 {&ovsrec_bridge_col_controller,
1154 &ovsrec_bridge_col_fail_mode,
1155 &ovsrec_bridge_col_ports},
1158 {&ovsrec_table_port,
1159 &ovsrec_port_col_name,
1160 {&ovsrec_port_col_tag,
1161 &ovsrec_port_col_trunks,
1162 &ovsrec_port_col_interfaces},
1165 {&ovsrec_table_interface,
1166 &ovsrec_interface_col_name,
1167 {&ovsrec_interface_col_type,
1168 &ovsrec_interface_col_options,
1172 {&ovsrec_table_controller,
1173 &ovsrec_controller_col_target,
1174 {&ovsrec_controller_col_is_connected,
1179 {&ovsrec_table_manager,
1180 &ovsrec_manager_col_target,
1181 {&ovsrec_manager_col_is_connected,
1188 pre_cmd_show(struct vsctl_context *ctx)
1190 struct cmd_show_table *show;
1192 for (show = cmd_show_tables;
1193 show < &cmd_show_tables[ARRAY_SIZE(cmd_show_tables)];
1197 ovsdb_idl_add_table(ctx->idl, show->table);
1198 if (show->name_column) {
1199 ovsdb_idl_add_column(ctx->idl, show->name_column);
1201 for (i = 0; i < ARRAY_SIZE(show->columns); i++) {
1202 const struct ovsdb_idl_column *column = show->columns[i];
1204 ovsdb_idl_add_column(ctx->idl, column);
1210 static struct cmd_show_table *
1211 cmd_show_find_table_by_row(const struct ovsdb_idl_row *row)
1213 struct cmd_show_table *show;
1215 for (show = cmd_show_tables;
1216 show < &cmd_show_tables[ARRAY_SIZE(cmd_show_tables)];
1218 if (show->table == row->table->class) {
1225 static struct cmd_show_table *
1226 cmd_show_find_table_by_name(const char *name)
1228 struct cmd_show_table *show;
1230 for (show = cmd_show_tables;
1231 show < &cmd_show_tables[ARRAY_SIZE(cmd_show_tables)];
1233 if (!strcmp(show->table->name, name)) {
1241 cmd_show_row(struct vsctl_context *ctx, const struct ovsdb_idl_row *row,
1244 struct cmd_show_table *show = cmd_show_find_table_by_row(row);
1247 ds_put_char_multiple(&ctx->output, ' ', level * 4);
1248 if (show && show->name_column) {
1249 const struct ovsdb_datum *datum;
1251 ds_put_format(&ctx->output, "%s ", show->table->name);
1252 datum = ovsdb_idl_read(row, show->name_column);
1253 ovsdb_datum_to_string(datum, &show->name_column->type, &ctx->output);
1255 ds_put_format(&ctx->output, UUID_FMT, UUID_ARGS(&row->uuid));
1257 ds_put_char(&ctx->output, '\n');
1259 if (!show || show->recurse) {
1263 show->recurse = true;
1264 for (i = 0; i < ARRAY_SIZE(show->columns); i++) {
1265 const struct ovsdb_idl_column *column = show->columns[i];
1266 const struct ovsdb_datum *datum;
1272 datum = ovsdb_idl_read(row, column);
1273 if (column->type.key.type == OVSDB_TYPE_UUID &&
1274 column->type.key.u.uuid.refTableName) {
1275 struct cmd_show_table *ref_show;
1278 ref_show = cmd_show_find_table_by_name(
1279 column->type.key.u.uuid.refTableName);
1281 for (j = 0; j < datum->n; j++) {
1282 const struct ovsdb_idl_row *ref_row;
1284 ref_row = ovsdb_idl_get_row_for_uuid(ctx->idl,
1286 &datum->keys[j].uuid);
1288 cmd_show_row(ctx, ref_row, level + 1);
1295 if (!ovsdb_datum_is_default(datum, &column->type)) {
1296 ds_put_char_multiple(&ctx->output, ' ', (level + 1) * 4);
1297 ds_put_format(&ctx->output, "%s: ", column->name);
1298 ovsdb_datum_to_string(datum, &column->type, &ctx->output);
1299 ds_put_char(&ctx->output, '\n');
1302 show->recurse = false;
1306 cmd_show(struct vsctl_context *ctx)
1308 const struct ovsdb_idl_row *row;
1310 for (row = ovsdb_idl_first_row(ctx->idl, cmd_show_tables[0].table);
1311 row; row = ovsdb_idl_next_row(row)) {
1312 cmd_show_row(ctx, row, 0);
1317 pre_cmd_emer_reset(struct vsctl_context *ctx)
1319 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_manager_options);
1320 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_ssl);
1322 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_controller);
1323 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_fail_mode);
1324 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_mirrors);
1325 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_netflow);
1326 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_sflow);
1327 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_flood_vlans);
1328 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_other_config);
1330 ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_other_config);
1332 ovsdb_idl_add_column(ctx->idl,
1333 &ovsrec_interface_col_ingress_policing_rate);
1334 ovsdb_idl_add_column(ctx->idl,
1335 &ovsrec_interface_col_ingress_policing_burst);
1339 cmd_emer_reset(struct vsctl_context *ctx)
1341 const struct ovsdb_idl *idl = ctx->idl;
1342 const struct ovsrec_bridge *br;
1343 const struct ovsrec_port *port;
1344 const struct ovsrec_interface *iface;
1345 const struct ovsrec_mirror *mirror, *next_mirror;
1346 const struct ovsrec_controller *ctrl, *next_ctrl;
1347 const struct ovsrec_manager *mgr, *next_mgr;
1348 const struct ovsrec_netflow *nf, *next_nf;
1349 const struct ovsrec_ssl *ssl, *next_ssl;
1350 const struct ovsrec_sflow *sflow, *next_sflow;
1352 /* Reset the Open_vSwitch table. */
1353 ovsrec_open_vswitch_set_manager_options(ctx->ovs, NULL, 0);
1354 ovsrec_open_vswitch_set_ssl(ctx->ovs, NULL);
1356 OVSREC_BRIDGE_FOR_EACH (br, idl) {
1358 char *hw_key = "hwaddr";
1359 char *hw_val = NULL;
1361 ovsrec_bridge_set_controller(br, NULL, 0);
1362 ovsrec_bridge_set_fail_mode(br, NULL);
1363 ovsrec_bridge_set_mirrors(br, NULL, 0);
1364 ovsrec_bridge_set_netflow(br, NULL);
1365 ovsrec_bridge_set_sflow(br, NULL);
1366 ovsrec_bridge_set_flood_vlans(br, NULL, 0);
1368 /* We only want to save the "hwaddr" key from other_config. */
1369 for (i=0; i < br->n_other_config; i++) {
1370 if (!strcmp(br->key_other_config[i], hw_key)) {
1371 hw_val = br->value_other_config[i];
1376 char *val = xstrdup(hw_val);
1377 ovsrec_bridge_set_other_config(br, &hw_key, &val, 1);
1380 ovsrec_bridge_set_other_config(br, NULL, NULL, 0);
1384 OVSREC_PORT_FOR_EACH (port, idl) {
1385 ovsrec_port_set_other_config(port, NULL, NULL, 0);
1388 OVSREC_INTERFACE_FOR_EACH (iface, idl) {
1389 /* xxx What do we do about gre/patch devices created by mgr? */
1391 ovsrec_interface_set_ingress_policing_rate(iface, 0);
1392 ovsrec_interface_set_ingress_policing_burst(iface, 0);
1395 OVSREC_MIRROR_FOR_EACH_SAFE (mirror, next_mirror, idl) {
1396 ovsrec_mirror_delete(mirror);
1399 OVSREC_CONTROLLER_FOR_EACH_SAFE (ctrl, next_ctrl, idl) {
1400 ovsrec_controller_delete(ctrl);
1403 OVSREC_MANAGER_FOR_EACH_SAFE (mgr, next_mgr, idl) {
1404 ovsrec_manager_delete(mgr);
1407 OVSREC_NETFLOW_FOR_EACH_SAFE (nf, next_nf, idl) {
1408 ovsrec_netflow_delete(nf);
1411 OVSREC_SSL_FOR_EACH_SAFE (ssl, next_ssl, idl) {
1412 ovsrec_ssl_delete(ssl);
1415 OVSREC_SFLOW_FOR_EACH_SAFE (sflow, next_sflow, idl) {
1416 ovsrec_sflow_delete(sflow);
1419 vsctl_context_invalidate_cache(ctx);
1423 cmd_add_br(struct vsctl_context *ctx)
1425 bool may_exist = shash_find(&ctx->options, "--may-exist") != NULL;
1426 const char *br_name, *parent_name;
1429 br_name = ctx->argv[1];
1430 if (ctx->argc == 2) {
1433 } else if (ctx->argc == 4) {
1434 parent_name = ctx->argv[2];
1435 vlan = atoi(ctx->argv[3]);
1436 if (vlan < 0 || vlan > 4095) {
1437 vsctl_fatal("%s: vlan must be between 0 and 4095", ctx->argv[0]);
1440 vsctl_fatal("'%s' command takes exactly 1 or 3 arguments",
1444 vsctl_context_populate_cache(ctx);
1446 struct vsctl_bridge *br;
1448 br = find_bridge(ctx, br_name, false);
1452 vsctl_fatal("\"--may-exist add-br %s\" but %s is "
1453 "a VLAN bridge for VLAN %d",
1454 br_name, br_name, br->vlan);
1458 vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
1459 "is not a VLAN bridge",
1460 br_name, parent_name, vlan, br_name);
1461 } else if (strcmp(br->parent->name, parent_name)) {
1462 vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
1463 "has the wrong parent %s",
1464 br_name, parent_name, vlan,
1465 br_name, br->parent->name);
1466 } else if (br->vlan != vlan) {
1467 vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
1468 "is a VLAN bridge for the wrong VLAN %d",
1469 br_name, parent_name, vlan, br_name, br->vlan);
1475 check_conflicts(ctx, br_name,
1476 xasprintf("cannot create a bridge named %s", br_name));
1479 struct ovsrec_port *port;
1480 struct ovsrec_interface *iface;
1481 struct ovsrec_bridge *br;
1483 iface = ovsrec_interface_insert(ctx->txn);
1484 ovsrec_interface_set_name(iface, br_name);
1485 ovsrec_interface_set_type(iface, "internal");
1487 port = ovsrec_port_insert(ctx->txn);
1488 ovsrec_port_set_name(port, br_name);
1489 ovsrec_port_set_interfaces(port, &iface, 1);
1491 br = ovsrec_bridge_insert(ctx->txn);
1492 ovsrec_bridge_set_name(br, br_name);
1493 ovsrec_bridge_set_ports(br, &port, 1);
1495 ovs_insert_bridge(ctx->ovs, br);
1497 struct vsctl_bridge *parent;
1498 struct ovsrec_port *port;
1499 struct ovsrec_interface *iface;
1500 struct ovsrec_bridge *br;
1503 parent = find_bridge(ctx, parent_name, false);
1504 if (parent && parent->parent) {
1505 vsctl_fatal("cannot create bridge with fake bridge as parent");
1508 vsctl_fatal("parent bridge %s does not exist", parent_name);
1510 br = parent->br_cfg;
1512 iface = ovsrec_interface_insert(ctx->txn);
1513 ovsrec_interface_set_name(iface, br_name);
1514 ovsrec_interface_set_type(iface, "internal");
1516 port = ovsrec_port_insert(ctx->txn);
1517 ovsrec_port_set_name(port, br_name);
1518 ovsrec_port_set_interfaces(port, &iface, 1);
1519 ovsrec_port_set_fake_bridge(port, true);
1520 ovsrec_port_set_tag(port, &tag, 1);
1522 bridge_insert_port(br, port);
1525 vsctl_context_invalidate_cache(ctx);
1529 del_port(struct vsctl_context *ctx, struct vsctl_port *port)
1531 struct vsctl_iface *iface, *next_iface;
1533 bridge_delete_port((port->bridge->parent
1534 ? port->bridge->parent->br_cfg
1535 : port->bridge->br_cfg), port->port_cfg);
1537 LIST_FOR_EACH_SAFE (iface, next_iface, ifaces_node, &port->ifaces) {
1538 del_cached_iface(ctx, iface);
1540 del_cached_port(ctx, port);
1544 del_bridge(struct vsctl_context *ctx, struct vsctl_bridge *br)
1546 struct vsctl_bridge *child, *next_child;
1547 struct vsctl_port *port, *next_port;
1549 HMAP_FOR_EACH_SAFE (child, next_child, children_node, &br->children) {
1550 del_bridge(ctx, child);
1553 LIST_FOR_EACH_SAFE (port, next_port, ports_node, &br->ports) {
1554 del_port(ctx, port);
1557 del_cached_bridge(ctx, br);
1561 cmd_del_br(struct vsctl_context *ctx)
1563 bool must_exist = !shash_find(&ctx->options, "--if-exists");
1564 struct vsctl_bridge *bridge;
1566 vsctl_context_populate_cache(ctx);
1567 bridge = find_bridge(ctx, ctx->argv[1], must_exist);
1569 del_bridge(ctx, bridge);
1574 output_sorted(struct svec *svec, struct ds *output)
1580 SVEC_FOR_EACH (i, name, svec) {
1581 ds_put_format(output, "%s\n", name);
1586 cmd_list_br(struct vsctl_context *ctx)
1588 struct shash_node *node;
1589 struct svec bridges;
1591 vsctl_context_populate_cache(ctx);
1593 svec_init(&bridges);
1594 SHASH_FOR_EACH (node, &ctx->bridges) {
1595 struct vsctl_bridge *br = node->data;
1596 svec_add(&bridges, br->name);
1598 output_sorted(&bridges, &ctx->output);
1599 svec_destroy(&bridges);
1603 cmd_br_exists(struct vsctl_context *ctx)
1605 vsctl_context_populate_cache(ctx);
1606 if (!find_bridge(ctx, ctx->argv[1], false)) {
1611 /* Returns true if 'b_prefix' (of length 'b_prefix_len') concatenated with 'b'
1612 * equals 'a', false otherwise. */
1614 key_matches(const char *a,
1615 const char *b_prefix, size_t b_prefix_len, const char *b)
1617 return !strncmp(a, b_prefix, b_prefix_len) && !strcmp(a + b_prefix_len, b);
1621 set_external_id(char **old_keys, char **old_values, size_t old_n,
1622 char *key, char *value,
1623 char ***new_keysp, char ***new_valuesp, size_t *new_np)
1630 new_keys = xmalloc(sizeof *new_keys * (old_n + 1));
1631 new_values = xmalloc(sizeof *new_values * (old_n + 1));
1633 for (i = 0; i < old_n; i++) {
1634 if (strcmp(key, old_keys[i])) {
1635 new_keys[new_n] = old_keys[i];
1636 new_values[new_n] = old_values[i];
1641 new_keys[new_n] = key;
1642 new_values[new_n] = value;
1645 *new_keysp = new_keys;
1646 *new_valuesp = new_values;
1651 pre_cmd_br_set_external_id(struct vsctl_context *ctx)
1654 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_external_ids);
1655 ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_external_ids);
1659 cmd_br_set_external_id(struct vsctl_context *ctx)
1661 struct vsctl_bridge *bridge;
1662 char **keys, **values;
1665 vsctl_context_populate_cache(ctx);
1666 bridge = find_bridge(ctx, ctx->argv[1], true);
1667 if (bridge->br_cfg) {
1668 set_external_id(bridge->br_cfg->key_external_ids,
1669 bridge->br_cfg->value_external_ids,
1670 bridge->br_cfg->n_external_ids,
1671 ctx->argv[2], ctx->argc >= 4 ? ctx->argv[3] : NULL,
1672 &keys, &values, &n);
1673 ovsrec_bridge_verify_external_ids(bridge->br_cfg);
1674 ovsrec_bridge_set_external_ids(bridge->br_cfg, keys, values, n);
1676 char *key = xasprintf("fake-bridge-%s", ctx->argv[2]);
1677 struct vsctl_port *port = shash_find_data(&ctx->ports, ctx->argv[1]);
1678 set_external_id(port->port_cfg->key_external_ids,
1679 port->port_cfg->value_external_ids,
1680 port->port_cfg->n_external_ids,
1681 key, ctx->argc >= 4 ? ctx->argv[3] : NULL,
1682 &keys, &values, &n);
1683 ovsrec_port_verify_external_ids(port->port_cfg);
1684 ovsrec_port_set_external_ids(port->port_cfg, keys, values, n);
1692 get_external_id(char **keys, char **values, size_t n,
1693 const char *prefix, const char *key,
1696 size_t prefix_len = strlen(prefix);
1701 for (i = 0; i < n; i++) {
1702 if (!key && !strncmp(keys[i], prefix, prefix_len)) {
1703 svec_add_nocopy(&svec, xasprintf("%s=%s",
1704 keys[i] + prefix_len, values[i]));
1705 } else if (key && key_matches(keys[i], prefix, prefix_len, key)) {
1706 svec_add(&svec, values[i]);
1710 output_sorted(&svec, output);
1711 svec_destroy(&svec);
1715 pre_cmd_br_get_external_id(struct vsctl_context *ctx)
1717 pre_cmd_br_set_external_id(ctx);
1721 cmd_br_get_external_id(struct vsctl_context *ctx)
1723 struct vsctl_bridge *bridge;
1725 vsctl_context_populate_cache(ctx);
1727 bridge = find_bridge(ctx, ctx->argv[1], true);
1728 if (bridge->br_cfg) {
1729 ovsrec_bridge_verify_external_ids(bridge->br_cfg);
1730 get_external_id(bridge->br_cfg->key_external_ids,
1731 bridge->br_cfg->value_external_ids,
1732 bridge->br_cfg->n_external_ids,
1733 "", ctx->argc >= 3 ? ctx->argv[2] : NULL,
1736 struct vsctl_port *port = shash_find_data(&ctx->ports, ctx->argv[1]);
1737 ovsrec_port_verify_external_ids(port->port_cfg);
1738 get_external_id(port->port_cfg->key_external_ids,
1739 port->port_cfg->value_external_ids,
1740 port->port_cfg->n_external_ids,
1741 "fake-bridge-", ctx->argc >= 3 ? ctx->argv[2] : NULL, &ctx->output);
1746 cmd_list_ports(struct vsctl_context *ctx)
1748 struct vsctl_bridge *br;
1749 struct vsctl_port *port;
1752 vsctl_context_populate_cache(ctx);
1753 br = find_bridge(ctx, ctx->argv[1], true);
1754 ovsrec_bridge_verify_ports(br->br_cfg ? br->br_cfg : br->parent->br_cfg);
1757 LIST_FOR_EACH (port, ports_node, &br->ports) {
1758 if (strcmp(port->port_cfg->name, br->name)) {
1759 svec_add(&ports, port->port_cfg->name);
1762 output_sorted(&ports, &ctx->output);
1763 svec_destroy(&ports);
1767 add_port(struct vsctl_context *ctx,
1768 const char *br_name, const char *port_name,
1769 bool may_exist, bool fake_iface,
1770 char *iface_names[], int n_ifaces,
1771 char *settings[], int n_settings)
1773 struct vsctl_port *vsctl_port;
1774 struct vsctl_bridge *bridge;
1775 struct ovsrec_interface **ifaces;
1776 struct ovsrec_port *port;
1779 vsctl_context_populate_cache(ctx);
1781 struct vsctl_port *vsctl_port;
1783 vsctl_port = find_port(ctx, port_name, false);
1785 struct svec want_names, have_names;
1787 svec_init(&want_names);
1788 for (i = 0; i < n_ifaces; i++) {
1789 svec_add(&want_names, iface_names[i]);
1791 svec_sort(&want_names);
1793 svec_init(&have_names);
1794 for (i = 0; i < vsctl_port->port_cfg->n_interfaces; i++) {
1795 svec_add(&have_names,
1796 vsctl_port->port_cfg->interfaces[i]->name);
1798 svec_sort(&have_names);
1800 if (strcmp(vsctl_port->bridge->name, br_name)) {
1801 char *command = vsctl_context_to_string(ctx);
1802 vsctl_fatal("\"%s\" but %s is actually attached to bridge %s",
1803 command, port_name, vsctl_port->bridge->name);
1806 if (!svec_equal(&want_names, &have_names)) {
1807 char *have_names_string = svec_join(&have_names, ", ", "");
1808 char *command = vsctl_context_to_string(ctx);
1810 vsctl_fatal("\"%s\" but %s actually has interface(s) %s",
1811 command, port_name, have_names_string);
1814 svec_destroy(&want_names);
1815 svec_destroy(&have_names);
1820 check_conflicts(ctx, port_name,
1821 xasprintf("cannot create a port named %s", port_name));
1822 for (i = 0; i < n_ifaces; i++) {
1823 check_conflicts(ctx, iface_names[i],
1824 xasprintf("cannot create an interface named %s",
1827 bridge = find_bridge(ctx, br_name, true);
1829 ifaces = xmalloc(n_ifaces * sizeof *ifaces);
1830 for (i = 0; i < n_ifaces; i++) {
1831 ifaces[i] = ovsrec_interface_insert(ctx->txn);
1832 ovsrec_interface_set_name(ifaces[i], iface_names[i]);
1835 port = ovsrec_port_insert(ctx->txn);
1836 ovsrec_port_set_name(port, port_name);
1837 ovsrec_port_set_interfaces(port, ifaces, n_ifaces);
1838 ovsrec_port_set_bond_fake_iface(port, fake_iface);
1840 if (bridge->parent) {
1841 int64_t tag = bridge->vlan;
1842 ovsrec_port_set_tag(port, &tag, 1);
1845 for (i = 0; i < n_settings; i++) {
1846 set_column(get_table("Port"), &port->header_, settings[i],
1850 bridge_insert_port((bridge->parent ? bridge->parent->br_cfg
1851 : bridge->br_cfg), port);
1853 vsctl_port = add_port_to_cache(ctx, bridge, port);
1854 for (i = 0; i < n_ifaces; i++) {
1855 add_iface_to_cache(ctx, vsctl_port, ifaces[i]);
1861 cmd_add_port(struct vsctl_context *ctx)
1863 bool may_exist = shash_find(&ctx->options, "--may-exist") != NULL;
1865 add_port(ctx, ctx->argv[1], ctx->argv[2], may_exist, false,
1866 &ctx->argv[2], 1, &ctx->argv[3], ctx->argc - 3);
1870 cmd_add_bond(struct vsctl_context *ctx)
1872 bool may_exist = shash_find(&ctx->options, "--may-exist") != NULL;
1873 bool fake_iface = shash_find(&ctx->options, "--fake-iface");
1877 n_ifaces = ctx->argc - 3;
1878 for (i = 3; i < ctx->argc; i++) {
1879 if (strchr(ctx->argv[i], '=')) {
1885 vsctl_fatal("add-bond requires at least 2 interfaces, but only "
1886 "%d were specified", n_ifaces);
1889 add_port(ctx, ctx->argv[1], ctx->argv[2], may_exist, fake_iface,
1890 &ctx->argv[3], n_ifaces,
1891 &ctx->argv[n_ifaces + 3], ctx->argc - 3 - n_ifaces);
1895 cmd_del_port(struct vsctl_context *ctx)
1897 bool must_exist = !shash_find(&ctx->options, "--if-exists");
1898 bool with_iface = shash_find(&ctx->options, "--with-iface") != NULL;
1899 struct vsctl_port *port;
1901 vsctl_context_populate_cache(ctx);
1903 port = find_port(ctx, ctx->argv[ctx->argc - 1], must_exist);
1905 const char *target = ctx->argv[ctx->argc - 1];
1906 struct vsctl_iface *iface;
1908 port = find_port(ctx, target, false);
1910 iface = find_iface(ctx, target, false);
1915 if (must_exist && !port) {
1916 vsctl_fatal("no port or interface named %s", target);
1921 if (ctx->argc == 3) {
1922 struct vsctl_bridge *bridge;
1924 bridge = find_bridge(ctx, ctx->argv[1], true);
1925 if (port->bridge != bridge) {
1926 if (port->bridge->parent == bridge) {
1927 vsctl_fatal("bridge %s does not have a port %s (although "
1928 "its parent bridge %s does)",
1929 ctx->argv[1], ctx->argv[2],
1930 bridge->parent->name);
1932 vsctl_fatal("bridge %s does not have a port %s",
1933 ctx->argv[1], ctx->argv[2]);
1938 del_port(ctx, port);
1943 cmd_port_to_br(struct vsctl_context *ctx)
1945 struct vsctl_port *port;
1947 vsctl_context_populate_cache(ctx);
1949 port = find_port(ctx, ctx->argv[1], true);
1950 ds_put_format(&ctx->output, "%s\n", port->bridge->name);
1954 cmd_br_to_vlan(struct vsctl_context *ctx)
1956 struct vsctl_bridge *bridge;
1958 vsctl_context_populate_cache(ctx);
1960 bridge = find_bridge(ctx, ctx->argv[1], true);
1961 ds_put_format(&ctx->output, "%d\n", bridge->vlan);
1965 cmd_br_to_parent(struct vsctl_context *ctx)
1967 struct vsctl_bridge *bridge;
1969 vsctl_context_populate_cache(ctx);
1971 bridge = find_bridge(ctx, ctx->argv[1], true);
1972 if (bridge->parent) {
1973 bridge = bridge->parent;
1975 ds_put_format(&ctx->output, "%s\n", bridge->name);
1979 cmd_list_ifaces(struct vsctl_context *ctx)
1981 struct vsctl_bridge *br;
1982 struct vsctl_port *port;
1985 vsctl_context_populate_cache(ctx);
1987 br = find_bridge(ctx, ctx->argv[1], true);
1991 LIST_FOR_EACH (port, ports_node, &br->ports) {
1992 struct vsctl_iface *iface;
1994 LIST_FOR_EACH (iface, ifaces_node, &port->ifaces) {
1995 if (strcmp(iface->iface_cfg->name, br->name)) {
1996 svec_add(&ifaces, iface->iface_cfg->name);
2000 output_sorted(&ifaces, &ctx->output);
2001 svec_destroy(&ifaces);
2005 cmd_iface_to_br(struct vsctl_context *ctx)
2007 struct vsctl_iface *iface;
2009 vsctl_context_populate_cache(ctx);
2011 iface = find_iface(ctx, ctx->argv[1], true);
2012 ds_put_format(&ctx->output, "%s\n", iface->port->bridge->name);
2016 verify_controllers(struct ovsrec_bridge *bridge)
2020 ovsrec_bridge_verify_controller(bridge);
2021 for (i = 0; i < bridge->n_controller; i++) {
2022 ovsrec_controller_verify_target(bridge->controller[i]);
2027 pre_controller(struct vsctl_context *ctx)
2031 ovsdb_idl_add_column(ctx->idl, &ovsrec_controller_col_target);
2035 cmd_get_controller(struct vsctl_context *ctx)
2037 struct vsctl_bridge *br;
2038 struct svec targets;
2041 vsctl_context_populate_cache(ctx);
2043 br = find_bridge(ctx, ctx->argv[1], true);
2047 verify_controllers(br->br_cfg);
2049 /* Print the targets in sorted order for reproducibility. */
2050 svec_init(&targets);
2051 for (i = 0; i < br->br_cfg->n_controller; i++) {
2052 svec_add(&targets, br->br_cfg->controller[i]->target);
2055 svec_sort(&targets);
2056 for (i = 0; i < targets.n; i++) {
2057 ds_put_format(&ctx->output, "%s\n", targets.names[i]);
2059 svec_destroy(&targets);
2063 delete_controllers(struct ovsrec_controller **controllers,
2064 size_t n_controllers)
2068 for (i = 0; i < n_controllers; i++) {
2069 ovsrec_controller_delete(controllers[i]);
2074 cmd_del_controller(struct vsctl_context *ctx)
2076 struct ovsrec_bridge *br;
2078 vsctl_context_populate_cache(ctx);
2080 br = find_real_bridge(ctx, ctx->argv[1], true)->br_cfg;
2081 verify_controllers(br);
2083 if (br->controller) {
2084 delete_controllers(br->controller, br->n_controller);
2085 ovsrec_bridge_set_controller(br, NULL, 0);
2089 static struct ovsrec_controller **
2090 insert_controllers(struct ovsdb_idl_txn *txn, char *targets[], size_t n)
2092 struct ovsrec_controller **controllers;
2095 controllers = xmalloc(n * sizeof *controllers);
2096 for (i = 0; i < n; i++) {
2097 if (vconn_verify_name(targets[i]) && pvconn_verify_name(targets[i])) {
2098 VLOG_WARN("target type \"%s\" is possibly erroneous", targets[i]);
2100 controllers[i] = ovsrec_controller_insert(txn);
2101 ovsrec_controller_set_target(controllers[i], targets[i]);
2108 cmd_set_controller(struct vsctl_context *ctx)
2110 struct ovsrec_controller **controllers;
2111 struct ovsrec_bridge *br;
2114 vsctl_context_populate_cache(ctx);
2116 br = find_real_bridge(ctx, ctx->argv[1], true)->br_cfg;
2117 verify_controllers(br);
2119 delete_controllers(br->controller, br->n_controller);
2122 controllers = insert_controllers(ctx->txn, &ctx->argv[2], n);
2123 ovsrec_bridge_set_controller(br, controllers, n);
2128 cmd_get_fail_mode(struct vsctl_context *ctx)
2130 struct vsctl_bridge *br;
2131 const char *fail_mode;
2133 vsctl_context_populate_cache(ctx);
2134 br = find_bridge(ctx, ctx->argv[1], true);
2139 ovsrec_bridge_verify_fail_mode(br->br_cfg);
2141 fail_mode = br->br_cfg->fail_mode;
2142 if (fail_mode && strlen(fail_mode)) {
2143 ds_put_format(&ctx->output, "%s\n", fail_mode);
2148 cmd_del_fail_mode(struct vsctl_context *ctx)
2150 struct vsctl_bridge *br;
2152 vsctl_context_populate_cache(ctx);
2154 br = find_real_bridge(ctx, ctx->argv[1], true);
2156 ovsrec_bridge_set_fail_mode(br->br_cfg, NULL);
2160 cmd_set_fail_mode(struct vsctl_context *ctx)
2162 struct vsctl_bridge *br;
2163 const char *fail_mode = ctx->argv[2];
2165 vsctl_context_populate_cache(ctx);
2167 br = find_real_bridge(ctx, ctx->argv[1], true);
2169 if (strcmp(fail_mode, "standalone") && strcmp(fail_mode, "secure")) {
2170 vsctl_fatal("fail-mode must be \"standalone\" or \"secure\"");
2173 ovsrec_bridge_set_fail_mode(br->br_cfg, fail_mode);
2177 verify_managers(const struct ovsrec_open_vswitch *ovs)
2181 ovsrec_open_vswitch_verify_manager_options(ovs);
2183 for (i = 0; i < ovs->n_manager_options; ++i) {
2184 const struct ovsrec_manager *mgr = ovs->manager_options[i];
2186 ovsrec_manager_verify_target(mgr);
2191 pre_manager(struct vsctl_context *ctx)
2193 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_manager_options);
2194 ovsdb_idl_add_column(ctx->idl, &ovsrec_manager_col_target);
2198 cmd_get_manager(struct vsctl_context *ctx)
2200 const struct ovsrec_open_vswitch *ovs = ctx->ovs;
2201 struct svec targets;
2204 verify_managers(ovs);
2206 /* Print the targets in sorted order for reproducibility. */
2207 svec_init(&targets);
2209 for (i = 0; i < ovs->n_manager_options; i++) {
2210 svec_add(&targets, ovs->manager_options[i]->target);
2213 svec_sort_unique(&targets);
2214 for (i = 0; i < targets.n; i++) {
2215 ds_put_format(&ctx->output, "%s\n", targets.names[i]);
2217 svec_destroy(&targets);
2221 delete_managers(const struct vsctl_context *ctx)
2223 const struct ovsrec_open_vswitch *ovs = ctx->ovs;
2226 /* Delete Manager rows pointed to by 'manager_options' column. */
2227 for (i = 0; i < ovs->n_manager_options; i++) {
2228 ovsrec_manager_delete(ovs->manager_options[i]);
2231 /* Delete 'Manager' row refs in 'manager_options' column. */
2232 ovsrec_open_vswitch_set_manager_options(ovs, NULL, 0);
2236 cmd_del_manager(struct vsctl_context *ctx)
2238 const struct ovsrec_open_vswitch *ovs = ctx->ovs;
2240 verify_managers(ovs);
2241 delete_managers(ctx);
2245 insert_managers(struct vsctl_context *ctx, char *targets[], size_t n)
2247 struct ovsrec_manager **managers;
2250 /* Insert each manager in a new row in Manager table. */
2251 managers = xmalloc(n * sizeof *managers);
2252 for (i = 0; i < n; i++) {
2253 if (stream_verify_name(targets[i]) && pstream_verify_name(targets[i])) {
2254 VLOG_WARN("target type \"%s\" is possibly erroneous", targets[i]);
2256 managers[i] = ovsrec_manager_insert(ctx->txn);
2257 ovsrec_manager_set_target(managers[i], targets[i]);
2260 /* Store uuids of new Manager rows in 'manager_options' column. */
2261 ovsrec_open_vswitch_set_manager_options(ctx->ovs, managers, n);
2266 cmd_set_manager(struct vsctl_context *ctx)
2268 const size_t n = ctx->argc - 1;
2270 verify_managers(ctx->ovs);
2271 delete_managers(ctx);
2272 insert_managers(ctx, &ctx->argv[1], n);
2276 pre_cmd_get_ssl(struct vsctl_context *ctx)
2278 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_ssl);
2280 ovsdb_idl_add_column(ctx->idl, &ovsrec_ssl_col_private_key);
2281 ovsdb_idl_add_column(ctx->idl, &ovsrec_ssl_col_certificate);
2282 ovsdb_idl_add_column(ctx->idl, &ovsrec_ssl_col_ca_cert);
2283 ovsdb_idl_add_column(ctx->idl, &ovsrec_ssl_col_bootstrap_ca_cert);
2287 cmd_get_ssl(struct vsctl_context *ctx)
2289 struct ovsrec_ssl *ssl = ctx->ovs->ssl;
2291 ovsrec_open_vswitch_verify_ssl(ctx->ovs);
2293 ovsrec_ssl_verify_private_key(ssl);
2294 ovsrec_ssl_verify_certificate(ssl);
2295 ovsrec_ssl_verify_ca_cert(ssl);
2296 ovsrec_ssl_verify_bootstrap_ca_cert(ssl);
2298 ds_put_format(&ctx->output, "Private key: %s\n", ssl->private_key);
2299 ds_put_format(&ctx->output, "Certificate: %s\n", ssl->certificate);
2300 ds_put_format(&ctx->output, "CA Certificate: %s\n", ssl->ca_cert);
2301 ds_put_format(&ctx->output, "Bootstrap: %s\n",
2302 ssl->bootstrap_ca_cert ? "true" : "false");
2307 pre_cmd_del_ssl(struct vsctl_context *ctx)
2309 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_ssl);
2313 cmd_del_ssl(struct vsctl_context *ctx)
2315 struct ovsrec_ssl *ssl = ctx->ovs->ssl;
2318 ovsrec_open_vswitch_verify_ssl(ctx->ovs);
2319 ovsrec_ssl_delete(ssl);
2320 ovsrec_open_vswitch_set_ssl(ctx->ovs, NULL);
2325 pre_cmd_set_ssl(struct vsctl_context *ctx)
2327 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_ssl);
2331 cmd_set_ssl(struct vsctl_context *ctx)
2333 bool bootstrap = shash_find(&ctx->options, "--bootstrap");
2334 struct ovsrec_ssl *ssl = ctx->ovs->ssl;
2336 ovsrec_open_vswitch_verify_ssl(ctx->ovs);
2338 ovsrec_ssl_delete(ssl);
2340 ssl = ovsrec_ssl_insert(ctx->txn);
2342 ovsrec_ssl_set_private_key(ssl, ctx->argv[1]);
2343 ovsrec_ssl_set_certificate(ssl, ctx->argv[2]);
2344 ovsrec_ssl_set_ca_cert(ssl, ctx->argv[3]);
2346 ovsrec_ssl_set_bootstrap_ca_cert(ssl, bootstrap);
2348 ovsrec_open_vswitch_set_ssl(ctx->ovs, ssl);
2351 /* Parameter commands. */
2353 struct vsctl_row_id {
2354 const struct ovsdb_idl_table_class *table;
2355 const struct ovsdb_idl_column *name_column;
2356 const struct ovsdb_idl_column *uuid_column;
2359 struct vsctl_table_class {
2360 struct ovsdb_idl_table_class *class;
2361 struct vsctl_row_id row_ids[2];
2364 static const struct vsctl_table_class tables[] = {
2365 {&ovsrec_table_bridge,
2366 {{&ovsrec_table_bridge, &ovsrec_bridge_col_name, NULL},
2367 {NULL, NULL, NULL}}},
2369 {&ovsrec_table_controller,
2370 {{&ovsrec_table_bridge,
2371 &ovsrec_bridge_col_name,
2372 &ovsrec_bridge_col_controller}}},
2374 {&ovsrec_table_interface,
2375 {{&ovsrec_table_interface, &ovsrec_interface_col_name, NULL},
2376 {NULL, NULL, NULL}}},
2378 {&ovsrec_table_mirror,
2379 {{&ovsrec_table_mirror, &ovsrec_mirror_col_name, NULL},
2380 {NULL, NULL, NULL}}},
2382 {&ovsrec_table_manager,
2383 {{&ovsrec_table_manager, &ovsrec_manager_col_target, NULL},
2384 {NULL, NULL, NULL}}},
2386 {&ovsrec_table_netflow,
2387 {{&ovsrec_table_bridge,
2388 &ovsrec_bridge_col_name,
2389 &ovsrec_bridge_col_netflow},
2390 {NULL, NULL, NULL}}},
2392 {&ovsrec_table_open_vswitch,
2393 {{&ovsrec_table_open_vswitch, NULL, NULL},
2394 {NULL, NULL, NULL}}},
2396 {&ovsrec_table_port,
2397 {{&ovsrec_table_port, &ovsrec_port_col_name, NULL},
2398 {NULL, NULL, NULL}}},
2401 {{&ovsrec_table_port, &ovsrec_port_col_name, &ovsrec_port_col_qos},
2402 {NULL, NULL, NULL}}},
2404 {&ovsrec_table_queue,
2405 {{NULL, NULL, NULL},
2406 {NULL, NULL, NULL}}},
2409 {{&ovsrec_table_open_vswitch, NULL, &ovsrec_open_vswitch_col_ssl}}},
2411 {&ovsrec_table_sflow,
2412 {{&ovsrec_table_bridge,
2413 &ovsrec_bridge_col_name,
2414 &ovsrec_bridge_col_sflow},
2415 {NULL, NULL, NULL}}},
2417 {&ovsrec_table_flow_table,
2418 {{&ovsrec_table_flow_table, &ovsrec_flow_table_col_name, NULL},
2419 {NULL, NULL, NULL}}},
2421 {NULL, {{NULL, NULL, NULL}, {NULL, NULL, NULL}}}
2425 die_if_error(char *error)
2428 vsctl_fatal("%s", error);
2433 to_lower_and_underscores(unsigned c)
2435 return c == '-' ? '_' : tolower(c);
2439 score_partial_match(const char *name, const char *s)
2443 if (!strcmp(name, s)) {
2446 for (score = 0; ; score++, name++, s++) {
2447 if (to_lower_and_underscores(*name) != to_lower_and_underscores(*s)) {
2449 } else if (*name == '\0') {
2450 return UINT_MAX - 1;
2453 return *s == '\0' ? score : 0;
2456 static const struct vsctl_table_class *
2457 get_table(const char *table_name)
2459 const struct vsctl_table_class *table;
2460 const struct vsctl_table_class *best_match = NULL;
2461 unsigned int best_score = 0;
2463 for (table = tables; table->class; table++) {
2464 unsigned int score = score_partial_match(table->class->name,
2466 if (score > best_score) {
2469 } else if (score == best_score) {
2475 } else if (best_score) {
2476 vsctl_fatal("multiple table names match \"%s\"", table_name);
2478 vsctl_fatal("unknown table \"%s\"", table_name);
2482 static const struct vsctl_table_class *
2483 pre_get_table(struct vsctl_context *ctx, const char *table_name)
2485 const struct vsctl_table_class *table_class;
2488 table_class = get_table(table_name);
2489 ovsdb_idl_add_table(ctx->idl, table_class->class);
2491 for (i = 0; i < ARRAY_SIZE(table_class->row_ids); i++) {
2492 const struct vsctl_row_id *id = &table_class->row_ids[i];
2494 ovsdb_idl_add_table(ctx->idl, id->table);
2496 if (id->name_column) {
2497 ovsdb_idl_add_column(ctx->idl, id->name_column);
2499 if (id->uuid_column) {
2500 ovsdb_idl_add_column(ctx->idl, id->uuid_column);
2507 static const struct ovsdb_idl_row *
2508 get_row_by_id(struct vsctl_context *ctx, const struct vsctl_table_class *table,
2509 const struct vsctl_row_id *id, const char *record_id)
2511 const struct ovsdb_idl_row *referrer, *final;
2517 if (!id->name_column) {
2518 if (strcmp(record_id, ".")) {
2521 referrer = ovsdb_idl_first_row(ctx->idl, id->table);
2522 if (!referrer || ovsdb_idl_next_row(referrer)) {
2526 const struct ovsdb_idl_row *row;
2529 for (row = ovsdb_idl_first_row(ctx->idl, id->table);
2531 row = ovsdb_idl_next_row(row))
2533 const struct ovsdb_datum *name;
2535 name = ovsdb_idl_get(row, id->name_column,
2536 OVSDB_TYPE_STRING, OVSDB_TYPE_VOID);
2537 if (name->n == 1 && !strcmp(name->keys[0].string, record_id)) {
2539 vsctl_fatal("multiple rows in %s match \"%s\"",
2540 table->class->name, record_id);
2551 if (id->uuid_column) {
2552 const struct ovsdb_datum *uuid;
2554 ovsdb_idl_txn_verify(referrer, id->uuid_column);
2555 uuid = ovsdb_idl_get(referrer, id->uuid_column,
2556 OVSDB_TYPE_UUID, OVSDB_TYPE_VOID);
2558 final = ovsdb_idl_get_row_for_uuid(ctx->idl, table->class,
2559 &uuid->keys[0].uuid);
2568 static const struct ovsdb_idl_row *
2569 get_row (struct vsctl_context *ctx,
2570 const struct vsctl_table_class *table, const char *record_id)
2572 const struct ovsdb_idl_row *row;
2575 if (uuid_from_string(&uuid, record_id)) {
2576 row = ovsdb_idl_get_row_for_uuid(ctx->idl, table->class, &uuid);
2580 for (i = 0; i < ARRAY_SIZE(table->row_ids); i++) {
2581 row = get_row_by_id(ctx, table, &table->row_ids[i], record_id);
2590 static const struct ovsdb_idl_row *
2591 must_get_row(struct vsctl_context *ctx,
2592 const struct vsctl_table_class *table, const char *record_id)
2594 const struct ovsdb_idl_row *row = get_row(ctx, table, record_id);
2596 vsctl_fatal("no row \"%s\" in table %s",
2597 record_id, table->class->name);
2603 get_column(const struct vsctl_table_class *table, const char *column_name,
2604 const struct ovsdb_idl_column **columnp)
2606 const struct ovsdb_idl_column *best_match = NULL;
2607 unsigned int best_score = 0;
2610 for (i = 0; i < table->class->n_columns; i++) {
2611 const struct ovsdb_idl_column *column = &table->class->columns[i];
2612 unsigned int score = score_partial_match(column->name, column_name);
2613 if (score > best_score) {
2614 best_match = column;
2616 } else if (score == best_score) {
2621 *columnp = best_match;
2624 } else if (best_score) {
2625 return xasprintf("%s contains more than one column whose name "
2626 "matches \"%s\"", table->class->name, column_name);
2628 return xasprintf("%s does not contain a column whose name matches "
2629 "\"%s\"", table->class->name, column_name);
2633 static struct ovsdb_symbol *
2634 create_symbol(struct ovsdb_symbol_table *symtab, const char *id, bool *newp)
2636 struct ovsdb_symbol *symbol;
2639 vsctl_fatal("row id \"%s\" does not begin with \"@\"", id);
2643 *newp = ovsdb_symbol_table_get(symtab, id) == NULL;
2646 symbol = ovsdb_symbol_table_insert(symtab, id);
2647 if (symbol->created) {
2648 vsctl_fatal("row id \"%s\" may only be specified on one --id option",
2651 symbol->created = true;
2656 pre_get_column(struct vsctl_context *ctx,
2657 const struct vsctl_table_class *table, const char *column_name,
2658 const struct ovsdb_idl_column **columnp)
2660 die_if_error(get_column(table, column_name, columnp));
2661 ovsdb_idl_add_column(ctx->idl, *columnp);
2665 missing_operator_error(const char *arg, const char **allowed_operators,
2671 ds_put_format(&s, "%s: argument does not end in ", arg);
2672 ds_put_format(&s, "\"%s\"", allowed_operators[0]);
2673 if (n_allowed == 2) {
2674 ds_put_format(&s, " or \"%s\"", allowed_operators[1]);
2675 } else if (n_allowed > 2) {
2678 for (i = 1; i < n_allowed - 1; i++) {
2679 ds_put_format(&s, ", \"%s\"", allowed_operators[i]);
2681 ds_put_format(&s, ", or \"%s\"", allowed_operators[i]);
2683 ds_put_format(&s, " followed by a value.");
2685 return ds_steal_cstr(&s);
2688 /* Breaks 'arg' apart into a number of fields in the following order:
2690 * - The name of a column in 'table', stored into '*columnp'. The column
2691 * name may be abbreviated.
2693 * - Optionally ':' followed by a key string. The key is stored as a
2694 * malloc()'d string into '*keyp', or NULL if no key is present in
2697 * - If 'valuep' is nonnull, an operator followed by a value string. The
2698 * allowed operators are the 'n_allowed' string in 'allowed_operators',
2699 * or just "=" if 'n_allowed' is 0. If 'operatorp' is nonnull, then the
2700 * index of the operator within 'allowed_operators' is stored into
2701 * '*operatorp'. The value is stored as a malloc()'d string into
2702 * '*valuep', or NULL if no value is present in 'arg'.
2704 * On success, returns NULL. On failure, returned a malloc()'d string error
2705 * message and stores NULL into all of the nonnull output arguments. */
2706 static char * WARN_UNUSED_RESULT
2707 parse_column_key_value(const char *arg,
2708 const struct vsctl_table_class *table,
2709 const struct ovsdb_idl_column **columnp, char **keyp,
2711 const char **allowed_operators, size_t n_allowed,
2714 const char *p = arg;
2718 assert(!(operatorp && !valuep));
2724 /* Parse column name. */
2725 error = ovsdb_token_parse(&p, &column_name);
2729 if (column_name[0] == '\0') {
2731 error = xasprintf("%s: missing column name", arg);
2734 error = get_column(table, column_name, columnp);
2740 /* Parse key string. */
2743 error = ovsdb_token_parse(&p, keyp);
2749 /* Parse value string. */
2755 if (!allowed_operators) {
2756 static const char *equals = "=";
2757 allowed_operators = =
2763 for (i = 0; i < n_allowed; i++) {
2764 const char *op = allowed_operators[i];
2765 size_t op_len = strlen(op);
2767 if (op_len > best_len && !strncmp(op, p, op_len) && p[op_len]) {
2773 error = missing_operator_error(arg, allowed_operators, n_allowed);
2780 *valuep = xstrdup(p + best_len);
2783 error = xasprintf("%s: trailing garbage \"%s\" in argument",
2805 pre_parse_column_key_value(struct vsctl_context *ctx,
2807 const struct vsctl_table_class *table)
2809 const struct ovsdb_idl_column *column;
2814 die_if_error(ovsdb_token_parse(&p, &column_name));
2815 if (column_name[0] == '\0') {
2816 vsctl_fatal("%s: missing column name", arg);
2819 pre_get_column(ctx, table, column_name, &column);
2824 pre_cmd_get(struct vsctl_context *ctx)
2826 const char *id = shash_find_data(&ctx->options, "--id");
2827 const char *table_name = ctx->argv[1];
2828 const struct vsctl_table_class *table;
2831 /* Using "get" without --id or a column name could possibly make sense.
2832 * Maybe, for example, a ovs-vsctl run wants to assert that a row exists.
2833 * But it is unlikely that an interactive user would want to do that, so
2834 * issue a warning if we're running on a terminal. */
2835 if (!id && ctx->argc <= 3 && isatty(STDOUT_FILENO)) {
2836 VLOG_WARN("\"get\" command without row arguments or \"--id\" is "
2837 "possibly erroneous");
2840 table = pre_get_table(ctx, table_name);
2841 for (i = 3; i < ctx->argc; i++) {
2842 if (!strcasecmp(ctx->argv[i], "_uuid")
2843 || !strcasecmp(ctx->argv[i], "-uuid")) {
2847 pre_parse_column_key_value(ctx, ctx->argv[i], table);
2852 cmd_get(struct vsctl_context *ctx)
2854 const char *id = shash_find_data(&ctx->options, "--id");
2855 bool if_exists = shash_find(&ctx->options, "--if-exists");
2856 const char *table_name = ctx->argv[1];
2857 const char *record_id = ctx->argv[2];
2858 const struct vsctl_table_class *table;
2859 const struct ovsdb_idl_row *row;
2860 struct ds *out = &ctx->output;
2863 table = get_table(table_name);
2864 row = must_get_row(ctx, table, record_id);
2866 struct ovsdb_symbol *symbol;
2869 symbol = create_symbol(ctx->symtab, id, &new);
2871 vsctl_fatal("row id \"%s\" specified on \"get\" command was used "
2872 "before it was defined", id);
2874 symbol->uuid = row->uuid;
2876 /* This symbol refers to a row that already exists, so disable warnings
2877 * about it being unreferenced. */
2878 symbol->strong_ref = true;
2880 for (i = 3; i < ctx->argc; i++) {
2881 const struct ovsdb_idl_column *column;
2882 const struct ovsdb_datum *datum;
2885 /* Special case for obtaining the UUID of a row. We can't just do this
2886 * through parse_column_key_value() below since it returns a "struct
2887 * ovsdb_idl_column" and the UUID column doesn't have one. */
2888 if (!strcasecmp(ctx->argv[i], "_uuid")
2889 || !strcasecmp(ctx->argv[i], "-uuid")) {
2890 ds_put_format(out, UUID_FMT"\n", UUID_ARGS(&row->uuid));
2894 die_if_error(parse_column_key_value(ctx->argv[i], table,
2895 &column, &key_string,
2896 NULL, NULL, 0, NULL));
2898 ovsdb_idl_txn_verify(row, column);
2899 datum = ovsdb_idl_read(row, column);
2901 union ovsdb_atom key;
2904 if (column->type.value.type == OVSDB_TYPE_VOID) {
2905 vsctl_fatal("cannot specify key to get for non-map column %s",
2909 die_if_error(ovsdb_atom_from_string(&key,
2911 key_string, ctx->symtab));
2913 idx = ovsdb_datum_find_key(datum, &key,
2914 column->type.key.type);
2915 if (idx == UINT_MAX) {
2917 vsctl_fatal("no key \"%s\" in %s record \"%s\" column %s",
2918 key_string, table->class->name, record_id,
2922 ovsdb_atom_to_string(&datum->values[idx],
2923 column->type.value.type, out);
2925 ovsdb_atom_destroy(&key, column->type.key.type);
2927 ovsdb_datum_to_string(datum, &column->type, out);
2929 ds_put_char(out, '\n');
2936 parse_column_names(const char *column_names,
2937 const struct vsctl_table_class *table,
2938 const struct ovsdb_idl_column ***columnsp,
2941 const struct ovsdb_idl_column **columns;
2944 if (!column_names) {
2947 n_columns = table->class->n_columns + 1;
2948 columns = xmalloc(n_columns * sizeof *columns);
2950 for (i = 0; i < table->class->n_columns; i++) {
2951 columns[i + 1] = &table->class->columns[i];
2954 char *s = xstrdup(column_names);
2955 size_t allocated_columns;
2956 char *save_ptr = NULL;
2960 allocated_columns = n_columns = 0;
2961 for (column_name = strtok_r(s, ", ", &save_ptr); column_name;
2962 column_name = strtok_r(NULL, ", ", &save_ptr)) {
2963 const struct ovsdb_idl_column *column;
2965 if (!strcasecmp(column_name, "_uuid")) {
2968 die_if_error(get_column(table, column_name, &column));
2970 if (n_columns >= allocated_columns) {
2971 columns = x2nrealloc(columns, &allocated_columns,
2974 columns[n_columns++] = column;
2979 vsctl_fatal("must specify at least one column name");
2982 *columnsp = columns;
2983 *n_columnsp = n_columns;
2988 pre_list_columns(struct vsctl_context *ctx,
2989 const struct vsctl_table_class *table,
2990 const char *column_names)
2992 const struct ovsdb_idl_column **columns;
2996 parse_column_names(column_names, table, &columns, &n_columns);
2997 for (i = 0; i < n_columns; i++) {
2999 ovsdb_idl_add_column(ctx->idl, columns[i]);
3006 pre_cmd_list(struct vsctl_context *ctx)
3008 const char *column_names = shash_find_data(&ctx->options, "--columns");
3009 const char *table_name = ctx->argv[1];
3010 const struct vsctl_table_class *table;
3012 table = pre_get_table(ctx, table_name);
3013 pre_list_columns(ctx, table, column_names);
3016 static struct table *
3017 list_make_table(const struct ovsdb_idl_column **columns, size_t n_columns)
3022 out = xmalloc(sizeof *out);
3025 for (i = 0; i < n_columns; i++) {
3026 const struct ovsdb_idl_column *column = columns[i];
3027 const char *column_name = column ? column->name : "_uuid";
3029 table_add_column(out, "%s", column_name);
3036 list_record(const struct ovsdb_idl_row *row,
3037 const struct ovsdb_idl_column **columns, size_t n_columns,
3043 for (i = 0; i < n_columns; i++) {
3044 const struct ovsdb_idl_column *column = columns[i];
3045 struct cell *cell = table_add_cell(out);
3048 struct ovsdb_datum datum;
3049 union ovsdb_atom atom;
3051 atom.uuid = row->uuid;
3054 datum.values = NULL;
3057 cell->json = ovsdb_datum_to_json(&datum, &ovsdb_type_uuid);
3058 cell->type = &ovsdb_type_uuid;
3060 const struct ovsdb_datum *datum = ovsdb_idl_read(row, column);
3062 cell->json = ovsdb_datum_to_json(datum, &column->type);
3063 cell->type = &column->type;
3069 cmd_list(struct vsctl_context *ctx)
3071 const char *column_names = shash_find_data(&ctx->options, "--columns");
3072 const struct ovsdb_idl_column **columns;
3073 const char *table_name = ctx->argv[1];
3074 const struct vsctl_table_class *table;
3079 table = get_table(table_name);
3080 parse_column_names(column_names, table, &columns, &n_columns);
3081 out = ctx->table = list_make_table(columns, n_columns);
3082 if (ctx->argc > 2) {
3083 for (i = 2; i < ctx->argc; i++) {
3084 list_record(must_get_row(ctx, table, ctx->argv[i]),
3085 columns, n_columns, out);
3088 const struct ovsdb_idl_row *row;
3090 for (row = ovsdb_idl_first_row(ctx->idl, table->class); row != NULL;
3091 row = ovsdb_idl_next_row(row)) {
3092 list_record(row, columns, n_columns, out);
3099 pre_cmd_find(struct vsctl_context *ctx)
3101 const char *column_names = shash_find_data(&ctx->options, "--columns");
3102 const char *table_name = ctx->argv[1];
3103 const struct vsctl_table_class *table;
3106 table = pre_get_table(ctx, table_name);
3107 pre_list_columns(ctx, table, column_names);
3108 for (i = 2; i < ctx->argc; i++) {
3109 pre_parse_column_key_value(ctx, ctx->argv[i], table);
3114 cmd_find(struct vsctl_context *ctx)
3116 const char *column_names = shash_find_data(&ctx->options, "--columns");
3117 const struct ovsdb_idl_column **columns;
3118 const char *table_name = ctx->argv[1];
3119 const struct vsctl_table_class *table;
3120 const struct ovsdb_idl_row *row;
3124 table = get_table(table_name);
3125 parse_column_names(column_names, table, &columns, &n_columns);
3126 out = ctx->table = list_make_table(columns, n_columns);
3127 for (row = ovsdb_idl_first_row(ctx->idl, table->class); row;
3128 row = ovsdb_idl_next_row(row)) {
3131 for (i = 2; i < ctx->argc; i++) {
3132 if (!is_condition_satisfied(table, row, ctx->argv[i],
3137 list_record(row, columns, n_columns, out);
3145 pre_cmd_set(struct vsctl_context *ctx)
3147 const char *table_name = ctx->argv[1];
3148 const struct vsctl_table_class *table;
3151 table = pre_get_table(ctx, table_name);
3152 for (i = 3; i < ctx->argc; i++) {
3153 pre_parse_column_key_value(ctx, ctx->argv[i], table);
3158 set_column(const struct vsctl_table_class *table,
3159 const struct ovsdb_idl_row *row, const char *arg,
3160 struct ovsdb_symbol_table *symtab)
3162 const struct ovsdb_idl_column *column;
3163 char *key_string, *value_string;
3166 error = parse_column_key_value(arg, table, &column, &key_string,
3167 NULL, NULL, 0, &value_string);
3168 die_if_error(error);
3169 if (!value_string) {
3170 vsctl_fatal("%s: missing value", arg);
3174 union ovsdb_atom key, value;
3175 struct ovsdb_datum datum;
3177 if (column->type.value.type == OVSDB_TYPE_VOID) {
3178 vsctl_fatal("cannot specify key to set for non-map column %s",
3182 die_if_error(ovsdb_atom_from_string(&key, &column->type.key,
3183 key_string, symtab));
3184 die_if_error(ovsdb_atom_from_string(&value, &column->type.value,
3185 value_string, symtab));
3187 ovsdb_datum_init_empty(&datum);
3188 ovsdb_datum_add_unsafe(&datum, &key, &value, &column->type);
3190 ovsdb_atom_destroy(&key, column->type.key.type);
3191 ovsdb_atom_destroy(&value, column->type.value.type);
3193 ovsdb_datum_union(&datum, ovsdb_idl_read(row, column),
3194 &column->type, false);
3195 ovsdb_idl_txn_write(row, column, &datum);
3197 struct ovsdb_datum datum;
3199 die_if_error(ovsdb_datum_from_string(&datum, &column->type,
3200 value_string, symtab));
3201 ovsdb_idl_txn_write(row, column, &datum);
3209 cmd_set(struct vsctl_context *ctx)
3211 const char *table_name = ctx->argv[1];
3212 const char *record_id = ctx->argv[2];
3213 const struct vsctl_table_class *table;
3214 const struct ovsdb_idl_row *row;
3217 table = get_table(table_name);
3218 row = must_get_row(ctx, table, record_id);
3219 for (i = 3; i < ctx->argc; i++) {
3220 set_column(table, row, ctx->argv[i], ctx->symtab);
3223 vsctl_context_invalidate_cache(ctx);
3227 pre_cmd_add(struct vsctl_context *ctx)
3229 const char *table_name = ctx->argv[1];
3230 const char *column_name = ctx->argv[3];
3231 const struct vsctl_table_class *table;
3232 const struct ovsdb_idl_column *column;
3234 table = pre_get_table(ctx, table_name);
3235 pre_get_column(ctx, table, column_name, &column);
3239 cmd_add(struct vsctl_context *ctx)
3241 const char *table_name = ctx->argv[1];
3242 const char *record_id = ctx->argv[2];
3243 const char *column_name = ctx->argv[3];
3244 const struct vsctl_table_class *table;
3245 const struct ovsdb_idl_column *column;
3246 const struct ovsdb_idl_row *row;
3247 const struct ovsdb_type *type;
3248 struct ovsdb_datum old;
3251 table = get_table(table_name);
3252 row = must_get_row(ctx, table, record_id);
3253 die_if_error(get_column(table, column_name, &column));
3255 type = &column->type;
3256 ovsdb_datum_clone(&old, ovsdb_idl_read(row, column), &column->type);
3257 for (i = 4; i < ctx->argc; i++) {
3258 struct ovsdb_type add_type;
3259 struct ovsdb_datum add;
3263 add_type.n_max = UINT_MAX;
3264 die_if_error(ovsdb_datum_from_string(&add, &add_type, ctx->argv[i],
3266 ovsdb_datum_union(&old, &add, type, false);
3267 ovsdb_datum_destroy(&add, type);
3269 if (old.n > type->n_max) {
3270 vsctl_fatal("\"add\" operation would put %u %s in column %s of "
3271 "table %s but the maximum number is %u",
3273 type->value.type == OVSDB_TYPE_VOID ? "values" : "pairs",
3274 column->name, table->class->name, type->n_max);
3276 ovsdb_idl_txn_verify(row, column);
3277 ovsdb_idl_txn_write(row, column, &old);
3279 vsctl_context_invalidate_cache(ctx);
3283 pre_cmd_remove(struct vsctl_context *ctx)
3285 const char *table_name = ctx->argv[1];
3286 const char *column_name = ctx->argv[3];
3287 const struct vsctl_table_class *table;
3288 const struct ovsdb_idl_column *column;
3290 table = pre_get_table(ctx, table_name);
3291 pre_get_column(ctx, table, column_name, &column);
3295 cmd_remove(struct vsctl_context *ctx)
3297 const char *table_name = ctx->argv[1];
3298 const char *record_id = ctx->argv[2];
3299 const char *column_name = ctx->argv[3];
3300 const struct vsctl_table_class *table;
3301 const struct ovsdb_idl_column *column;
3302 const struct ovsdb_idl_row *row;
3303 const struct ovsdb_type *type;
3304 struct ovsdb_datum old;
3307 table = get_table(table_name);
3308 row = must_get_row(ctx, table, record_id);
3309 die_if_error(get_column(table, column_name, &column));
3311 type = &column->type;
3312 ovsdb_datum_clone(&old, ovsdb_idl_read(row, column), &column->type);
3313 for (i = 4; i < ctx->argc; i++) {
3314 struct ovsdb_type rm_type;
3315 struct ovsdb_datum rm;
3320 rm_type.n_max = UINT_MAX;
3321 error = ovsdb_datum_from_string(&rm, &rm_type,
3322 ctx->argv[i], ctx->symtab);
3323 if (error && ovsdb_type_is_map(&rm_type)) {
3325 rm_type.value.type = OVSDB_TYPE_VOID;
3326 die_if_error(ovsdb_datum_from_string(&rm, &rm_type,
3327 ctx->argv[i], ctx->symtab));
3329 ovsdb_datum_subtract(&old, type, &rm, &rm_type);
3330 ovsdb_datum_destroy(&rm, &rm_type);
3332 if (old.n < type->n_min) {
3333 vsctl_fatal("\"remove\" operation would put %u %s in column %s of "
3334 "table %s but the minimum number is %u",
3336 type->value.type == OVSDB_TYPE_VOID ? "values" : "pairs",
3337 column->name, table->class->name, type->n_min);
3339 ovsdb_idl_txn_verify(row, column);
3340 ovsdb_idl_txn_write(row, column, &old);
3342 vsctl_context_invalidate_cache(ctx);
3346 pre_cmd_clear(struct vsctl_context *ctx)
3348 const char *table_name = ctx->argv[1];
3349 const struct vsctl_table_class *table;
3352 table = pre_get_table(ctx, table_name);
3353 for (i = 3; i < ctx->argc; i++) {
3354 const struct ovsdb_idl_column *column;
3356 pre_get_column(ctx, table, ctx->argv[i], &column);
3361 cmd_clear(struct vsctl_context *ctx)
3363 const char *table_name = ctx->argv[1];
3364 const char *record_id = ctx->argv[2];
3365 const struct vsctl_table_class *table;
3366 const struct ovsdb_idl_row *row;
3369 table = get_table(table_name);
3370 row = must_get_row(ctx, table, record_id);
3371 for (i = 3; i < ctx->argc; i++) {
3372 const struct ovsdb_idl_column *column;
3373 const struct ovsdb_type *type;
3374 struct ovsdb_datum datum;
3376 die_if_error(get_column(table, ctx->argv[i], &column));
3378 type = &column->type;
3379 if (type->n_min > 0) {
3380 vsctl_fatal("\"clear\" operation cannot be applied to column %s "
3381 "of table %s, which is not allowed to be empty",
3382 column->name, table->class->name);
3385 ovsdb_datum_init_empty(&datum);
3386 ovsdb_idl_txn_write(row, column, &datum);
3389 vsctl_context_invalidate_cache(ctx);
3393 pre_create(struct vsctl_context *ctx)
3395 const char *id = shash_find_data(&ctx->options, "--id");
3396 const char *table_name = ctx->argv[1];
3397 const struct vsctl_table_class *table;
3399 table = get_table(table_name);
3400 if (!id && !table->class->is_root) {
3401 VLOG_WARN("applying \"create\" command to table %s without --id "
3402 "option will have no effect", table->class->name);
3407 cmd_create(struct vsctl_context *ctx)
3409 const char *id = shash_find_data(&ctx->options, "--id");
3410 const char *table_name = ctx->argv[1];
3411 const struct vsctl_table_class *table = get_table(table_name);
3412 const struct ovsdb_idl_row *row;
3413 const struct uuid *uuid;
3417 struct ovsdb_symbol *symbol = create_symbol(ctx->symtab, id, NULL);
3418 if (table->class->is_root) {
3419 /* This table is in the root set, meaning that rows created in it
3420 * won't disappear even if they are unreferenced, so disable
3421 * warnings about that by pretending that there is a reference. */
3422 symbol->strong_ref = true;
3424 uuid = &symbol->uuid;
3429 row = ovsdb_idl_txn_insert(ctx->txn, table->class, uuid);
3430 for (i = 2; i < ctx->argc; i++) {
3431 set_column(table, row, ctx->argv[i], ctx->symtab);
3433 ds_put_format(&ctx->output, UUID_FMT, UUID_ARGS(&row->uuid));
3436 /* This function may be used as the 'postprocess' function for commands that
3437 * insert new rows into the database. It expects that the command's 'run'
3438 * function prints the UUID reported by ovsdb_idl_txn_insert() as the command's
3439 * sole output. It replaces that output by the row's permanent UUID assigned
3440 * by the database server and appends a new-line.
3442 * Currently we use this only for "create", because the higher-level commands
3443 * are supposed to be independent of the actual structure of the vswitch
3446 post_create(struct vsctl_context *ctx)
3448 const struct uuid *real;
3451 if (!uuid_from_string(&dummy, ds_cstr(&ctx->output))) {
3454 real = ovsdb_idl_txn_get_insert_uuid(ctx->txn, &dummy);
3456 ds_clear(&ctx->output);
3457 ds_put_format(&ctx->output, UUID_FMT, UUID_ARGS(real));
3459 ds_put_char(&ctx->output, '\n');
3463 pre_cmd_destroy(struct vsctl_context *ctx)
3465 const char *table_name = ctx->argv[1];
3467 pre_get_table(ctx, table_name);
3471 cmd_destroy(struct vsctl_context *ctx)
3473 bool must_exist = !shash_find(&ctx->options, "--if-exists");
3474 bool delete_all = shash_find(&ctx->options, "--all");
3475 const char *table_name = ctx->argv[1];
3476 const struct vsctl_table_class *table;
3479 table = get_table(table_name);
3481 if (delete_all && ctx->argc > 2) {
3482 vsctl_fatal("--all and records argument should not be specified together");
3485 if (delete_all && !must_exist) {
3486 vsctl_fatal("--all and --if-exists should not be specified together");
3490 const struct ovsdb_idl_row *row;
3491 const struct ovsdb_idl_row *next_row;
3493 for (row = ovsdb_idl_first_row(ctx->idl, table->class);
3495 next_row = ovsdb_idl_next_row(row);
3496 ovsdb_idl_txn_delete(row);
3500 for (i = 2; i < ctx->argc; i++) {
3501 const struct ovsdb_idl_row *row;
3503 row = (must_exist ? must_get_row : get_row)(ctx, table, ctx->argv[i]);
3505 ovsdb_idl_txn_delete(row);
3509 vsctl_context_invalidate_cache(ctx);
3513 RELOP(RELOP_EQ, "=") \
3514 RELOP(RELOP_NE, "!=") \
3515 RELOP(RELOP_LT, "<") \
3516 RELOP(RELOP_GT, ">") \
3517 RELOP(RELOP_LE, "<=") \
3518 RELOP(RELOP_GE, ">=") \
3519 RELOP(RELOP_SET_EQ, "{=}") \
3520 RELOP(RELOP_SET_NE, "{!=}") \
3521 RELOP(RELOP_SET_LT, "{<}") \
3522 RELOP(RELOP_SET_GT, "{>}") \
3523 RELOP(RELOP_SET_LE, "{<=}") \
3524 RELOP(RELOP_SET_GE, "{>=}")
3527 #define RELOP(ENUM, STRING) ENUM,
3533 is_set_operator(enum relop op)
3535 return (op == RELOP_SET_EQ || op == RELOP_SET_NE ||
3536 op == RELOP_SET_LT || op == RELOP_SET_GT ||
3537 op == RELOP_SET_LE || op == RELOP_SET_GE);
3541 evaluate_relop(const struct ovsdb_datum *a, const struct ovsdb_datum *b,
3542 const struct ovsdb_type *type, enum relop op)
3547 return ovsdb_datum_compare_3way(a, b, type) == 0;
3550 return ovsdb_datum_compare_3way(a, b, type) != 0;
3552 return ovsdb_datum_compare_3way(a, b, type) < 0;
3554 return ovsdb_datum_compare_3way(a, b, type) > 0;
3556 return ovsdb_datum_compare_3way(a, b, type) <= 0;
3558 return ovsdb_datum_compare_3way(a, b, type) >= 0;
3561 return b->n > a->n && ovsdb_datum_includes_all(a, b, type);
3563 return a->n > b->n && ovsdb_datum_includes_all(b, a, type);
3565 return ovsdb_datum_includes_all(a, b, type);
3567 return ovsdb_datum_includes_all(b, a, type);
3575 is_condition_satisfied(const struct vsctl_table_class *table,
3576 const struct ovsdb_idl_row *row, const char *arg,
3577 struct ovsdb_symbol_table *symtab)
3579 static const char *operators[] = {
3580 #define RELOP(ENUM, STRING) STRING,
3585 const struct ovsdb_idl_column *column;
3586 const struct ovsdb_datum *have_datum;
3587 char *key_string, *value_string;
3588 struct ovsdb_type type;
3593 error = parse_column_key_value(arg, table, &column, &key_string,
3594 &operator, operators, ARRAY_SIZE(operators),
3596 die_if_error(error);
3597 if (!value_string) {
3598 vsctl_fatal("%s: missing value", arg);
3601 type = column->type;
3602 type.n_max = UINT_MAX;
3604 have_datum = ovsdb_idl_read(row, column);
3606 union ovsdb_atom want_key;
3607 struct ovsdb_datum b;
3610 if (column->type.value.type == OVSDB_TYPE_VOID) {
3611 vsctl_fatal("cannot specify key to check for non-map column %s",
3615 die_if_error(ovsdb_atom_from_string(&want_key, &column->type.key,
3616 key_string, symtab));
3618 type.key = type.value;
3619 type.value.type = OVSDB_TYPE_VOID;
3620 die_if_error(ovsdb_datum_from_string(&b, &type, value_string, symtab));
3622 idx = ovsdb_datum_find_key(have_datum,
3623 &want_key, column->type.key.type);
3624 if (idx == UINT_MAX && !is_set_operator(operator)) {
3627 struct ovsdb_datum a;
3629 if (idx != UINT_MAX) {
3631 a.keys = &have_datum->values[idx];
3639 retval = evaluate_relop(&a, &b, &type, operator);
3642 ovsdb_atom_destroy(&want_key, column->type.key.type);
3643 ovsdb_datum_destroy(&b, &type);
3645 struct ovsdb_datum want_datum;
3647 die_if_error(ovsdb_datum_from_string(&want_datum, &column->type,
3648 value_string, symtab));
3649 retval = evaluate_relop(have_datum, &want_datum, &type, operator);
3650 ovsdb_datum_destroy(&want_datum, &column->type);
3660 pre_cmd_wait_until(struct vsctl_context *ctx)
3662 const char *table_name = ctx->argv[1];
3663 const struct vsctl_table_class *table;
3666 table = pre_get_table(ctx, table_name);
3668 for (i = 3; i < ctx->argc; i++) {
3669 pre_parse_column_key_value(ctx, ctx->argv[i], table);
3674 cmd_wait_until(struct vsctl_context *ctx)
3676 const char *table_name = ctx->argv[1];
3677 const char *record_id = ctx->argv[2];
3678 const struct vsctl_table_class *table;
3679 const struct ovsdb_idl_row *row;
3682 table = get_table(table_name);
3684 row = get_row(ctx, table, record_id);
3686 ctx->try_again = true;
3690 for (i = 3; i < ctx->argc; i++) {
3691 if (!is_condition_satisfied(table, row, ctx->argv[i], ctx->symtab)) {
3692 ctx->try_again = true;
3698 /* Prepares 'ctx', which has already been initialized with
3699 * vsctl_context_init(), for processing 'command'. */
3701 vsctl_context_init_command(struct vsctl_context *ctx,
3702 struct vsctl_command *command)
3704 ctx->argc = command->argc;
3705 ctx->argv = command->argv;
3706 ctx->options = command->options;
3708 ds_swap(&ctx->output, &command->output);
3709 ctx->table = command->table;
3711 ctx->verified_ports = false;
3713 ctx->try_again = false;
3716 /* Prepares 'ctx' for processing commands, initializing its members with the
3717 * values passed in as arguments.
3719 * If 'command' is nonnull, calls vsctl_context_init_command() to prepare for
3720 * that particular command. */
3722 vsctl_context_init(struct vsctl_context *ctx, struct vsctl_command *command,
3723 struct ovsdb_idl *idl, struct ovsdb_idl_txn *txn,
3724 const struct ovsrec_open_vswitch *ovs,
3725 struct ovsdb_symbol_table *symtab)
3728 vsctl_context_init_command(ctx, command);
3733 ctx->symtab = symtab;
3734 ctx->cache_valid = false;
3737 /* Completes processing of 'command' within 'ctx'. */
3739 vsctl_context_done_command(struct vsctl_context *ctx,
3740 struct vsctl_command *command)
3742 ds_swap(&ctx->output, &command->output);
3743 command->table = ctx->table;
3746 /* Finishes up with 'ctx'.
3748 * If command is nonnull, first calls vsctl_context_done_command() to complete
3749 * processing that command within 'ctx'. */
3751 vsctl_context_done(struct vsctl_context *ctx, struct vsctl_command *command)
3754 vsctl_context_done_command(ctx, command);
3756 vsctl_context_invalidate_cache(ctx);
3760 run_prerequisites(struct vsctl_command *commands, size_t n_commands,
3761 struct ovsdb_idl *idl)
3763 struct vsctl_command *c;
3765 ovsdb_idl_add_table(idl, &ovsrec_table_open_vswitch);
3766 if (wait_for_reload) {
3767 ovsdb_idl_add_column(idl, &ovsrec_open_vswitch_col_cur_cfg);
3769 for (c = commands; c < &commands[n_commands]; c++) {
3770 if (c->syntax->prerequisites) {
3771 struct vsctl_context ctx;
3773 ds_init(&c->output);
3776 vsctl_context_init(&ctx, c, idl, NULL, NULL, NULL);
3777 (c->syntax->prerequisites)(&ctx);
3778 vsctl_context_done(&ctx, c);
3780 assert(!c->output.string);
3787 do_vsctl(const char *args, struct vsctl_command *commands, size_t n_commands,
3788 struct ovsdb_idl *idl)
3790 struct ovsdb_idl_txn *txn;
3791 const struct ovsrec_open_vswitch *ovs;
3792 enum ovsdb_idl_txn_status status;
3793 struct ovsdb_symbol_table *symtab;
3794 struct vsctl_context ctx;
3795 struct vsctl_command *c;
3796 struct shash_node *node;
3797 int64_t next_cfg = 0;
3800 txn = the_idl_txn = ovsdb_idl_txn_create(idl);
3802 ovsdb_idl_txn_set_dry_run(txn);
3805 ovsdb_idl_txn_add_comment(txn, "ovs-vsctl: %s", args);
3807 ovs = ovsrec_open_vswitch_first(idl);
3809 /* XXX add verification that table is empty */
3810 ovs = ovsrec_open_vswitch_insert(txn);
3813 if (wait_for_reload) {
3814 ovsdb_idl_txn_increment(txn, &ovs->header_,
3815 &ovsrec_open_vswitch_col_next_cfg);
3818 symtab = ovsdb_symbol_table_create();
3819 for (c = commands; c < &commands[n_commands]; c++) {
3820 ds_init(&c->output);
3823 vsctl_context_init(&ctx, NULL, idl, txn, ovs, symtab);
3824 for (c = commands; c < &commands[n_commands]; c++) {
3825 vsctl_context_init_command(&ctx, c);
3826 if (c->syntax->run) {
3827 (c->syntax->run)(&ctx);
3829 vsctl_context_done_command(&ctx, c);
3831 if (ctx.try_again) {
3832 vsctl_context_done(&ctx, NULL);
3836 vsctl_context_done(&ctx, NULL);
3838 SHASH_FOR_EACH (node, &symtab->sh) {
3839 struct ovsdb_symbol *symbol = node->data;
3840 if (!symbol->created) {
3841 vsctl_fatal("row id \"%s\" is referenced but never created (e.g. "
3842 "with \"-- --id=%s create ...\")",
3843 node->name, node->name);
3845 if (!symbol->strong_ref) {
3846 if (!symbol->weak_ref) {
3847 VLOG_WARN("row id \"%s\" was created but no reference to it "
3848 "was inserted, so it will not actually appear in "
3849 "the database", node->name);
3851 VLOG_WARN("row id \"%s\" was created but only a weak "
3852 "reference to it was inserted, so it will not "
3853 "actually appear in the database", node->name);
3858 status = ovsdb_idl_txn_commit_block(txn);
3859 if (wait_for_reload && status == TXN_SUCCESS) {
3860 next_cfg = ovsdb_idl_txn_get_increment_new_value(txn);
3862 if (status == TXN_UNCHANGED || status == TXN_SUCCESS) {
3863 for (c = commands; c < &commands[n_commands]; c++) {
3864 if (c->syntax->postprocess) {
3865 struct vsctl_context ctx;
3867 vsctl_context_init(&ctx, c, idl, txn, ovs, symtab);
3868 (c->syntax->postprocess)(&ctx);
3869 vsctl_context_done(&ctx, c);
3873 error = xstrdup(ovsdb_idl_txn_get_error(txn));
3874 ovsdb_idl_txn_destroy(txn);
3875 txn = the_idl_txn = NULL;
3878 case TXN_UNCOMMITTED:
3879 case TXN_INCOMPLETE:
3883 /* Should not happen--we never call ovsdb_idl_txn_abort(). */
3884 vsctl_fatal("transaction aborted");
3894 vsctl_fatal("transaction error: %s", error);
3896 case TXN_NOT_LOCKED:
3897 /* Should not happen--we never call ovsdb_idl_set_lock(). */
3898 vsctl_fatal("database not locked");
3905 ovsdb_symbol_table_destroy(symtab);
3907 for (c = commands; c < &commands[n_commands]; c++) {
3908 struct ds *ds = &c->output;
3911 table_print(c->table, &table_style);
3912 } else if (oneline) {
3916 for (j = 0; j < ds->length; j++) {
3917 int ch = ds->string[j];
3920 fputs("\\n", stdout);
3924 fputs("\\\\", stdout);
3933 fputs(ds_cstr(ds), stdout);
3935 ds_destroy(&c->output);
3936 table_destroy(c->table);
3939 smap_destroy(&c->options);
3943 if (wait_for_reload && status != TXN_UNCHANGED) {
3946 OVSREC_OPEN_VSWITCH_FOR_EACH (ovs, idl) {
3947 if (ovs->cur_cfg >= next_cfg) {
3951 ovsdb_idl_wait(idl);
3956 ovsdb_idl_destroy(idl);
3961 /* Our transaction needs to be rerun, or a prerequisite was not met. Free
3962 * resources and return so that the caller can try again. */
3964 ovsdb_idl_txn_abort(txn);
3965 ovsdb_idl_txn_destroy(txn);
3967 ovsdb_symbol_table_destroy(symtab);
3968 for (c = commands; c < &commands[n_commands]; c++) {
3969 ds_destroy(&c->output);
3970 table_destroy(c->table);
3976 static const struct vsctl_command_syntax all_commands[] = {
3977 /* Open vSwitch commands. */
3978 {"init", 0, 0, NULL, cmd_init, NULL, "", RW},
3979 {"show", 0, 0, pre_cmd_show, cmd_show, NULL, "", RO},
3981 /* Bridge commands. */
3982 {"add-br", 1, 3, pre_get_info, cmd_add_br, NULL, "--may-exist", RW},
3983 {"del-br", 1, 1, pre_get_info, cmd_del_br, NULL, "--if-exists", RW},
3984 {"list-br", 0, 0, pre_get_info, cmd_list_br, NULL, "", RO},
3985 {"br-exists", 1, 1, pre_get_info, cmd_br_exists, NULL, "", RO},
3986 {"br-to-vlan", 1, 1, pre_get_info, cmd_br_to_vlan, NULL, "", RO},
3987 {"br-to-parent", 1, 1, pre_get_info, cmd_br_to_parent, NULL, "", RO},
3988 {"br-set-external-id", 2, 3, pre_cmd_br_set_external_id,
3989 cmd_br_set_external_id, NULL, "", RW},
3990 {"br-get-external-id", 1, 2, pre_cmd_br_get_external_id,
3991 cmd_br_get_external_id, NULL, "", RO},
3993 /* Port commands. */
3994 {"list-ports", 1, 1, pre_get_info, cmd_list_ports, NULL, "", RO},
3995 {"add-port", 2, INT_MAX, pre_get_info, cmd_add_port, NULL, "--may-exist",
3997 {"add-bond", 4, INT_MAX, pre_get_info, cmd_add_bond, NULL,
3998 "--may-exist,--fake-iface", RW},
3999 {"del-port", 1, 2, pre_get_info, cmd_del_port, NULL,
4000 "--if-exists,--with-iface", RW},
4001 {"port-to-br", 1, 1, pre_get_info, cmd_port_to_br, NULL, "", RO},
4003 /* Interface commands. */
4004 {"list-ifaces", 1, 1, pre_get_info, cmd_list_ifaces, NULL, "", RO},
4005 {"iface-to-br", 1, 1, pre_get_info, cmd_iface_to_br, NULL, "", RO},
4007 /* Controller commands. */
4008 {"get-controller", 1, 1, pre_controller, cmd_get_controller, NULL, "", RO},
4009 {"del-controller", 1, 1, pre_controller, cmd_del_controller, NULL, "", RW},
4010 {"set-controller", 1, INT_MAX, pre_controller, cmd_set_controller, NULL,
4012 {"get-fail-mode", 1, 1, pre_get_info, cmd_get_fail_mode, NULL, "", RO},
4013 {"del-fail-mode", 1, 1, pre_get_info, cmd_del_fail_mode, NULL, "", RW},
4014 {"set-fail-mode", 2, 2, pre_get_info, cmd_set_fail_mode, NULL, "", RW},
4016 /* Manager commands. */
4017 {"get-manager", 0, 0, pre_manager, cmd_get_manager, NULL, "", RO},
4018 {"del-manager", 0, INT_MAX, pre_manager, cmd_del_manager, NULL, "", RW},
4019 {"set-manager", 1, INT_MAX, pre_manager, cmd_set_manager, NULL, "", RW},
4022 {"get-ssl", 0, 0, pre_cmd_get_ssl, cmd_get_ssl, NULL, "", RO},
4023 {"del-ssl", 0, 0, pre_cmd_del_ssl, cmd_del_ssl, NULL, "", RW},
4024 {"set-ssl", 3, 3, pre_cmd_set_ssl, cmd_set_ssl, NULL, "--bootstrap", RW},
4026 /* Switch commands. */
4027 {"emer-reset", 0, 0, pre_cmd_emer_reset, cmd_emer_reset, NULL, "", RW},
4029 /* Database commands. */
4030 {"comment", 0, INT_MAX, NULL, NULL, NULL, "", RO},
4031 {"get", 2, INT_MAX, pre_cmd_get, cmd_get, NULL, "--if-exists,--id=", RO},
4032 {"list", 1, INT_MAX, pre_cmd_list, cmd_list, NULL, "--columns=", RO},
4033 {"find", 1, INT_MAX, pre_cmd_find, cmd_find, NULL, "--columns=", RO},
4034 {"set", 3, INT_MAX, pre_cmd_set, cmd_set, NULL, "", RW},
4035 {"add", 4, INT_MAX, pre_cmd_add, cmd_add, NULL, "", RW},
4036 {"remove", 4, INT_MAX, pre_cmd_remove, cmd_remove, NULL, "", RW},
4037 {"clear", 3, INT_MAX, pre_cmd_clear, cmd_clear, NULL, "", RW},
4038 {"create", 2, INT_MAX, pre_create, cmd_create, post_create, "--id=", RW},
4039 {"destroy", 1, INT_MAX, pre_cmd_destroy, cmd_destroy, NULL,
4040 "--if-exists,--all", RW},
4041 {"wait-until", 2, INT_MAX, pre_cmd_wait_until, cmd_wait_until, NULL, "",
4044 {NULL, 0, 0, NULL, NULL, NULL, NULL, RO},