2 * Copyright (c) 2009, 2010, 2011, 2012 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
31 #include "command-line.h"
34 #include "dynamic-string.h"
37 #include "ovsdb-data.h"
38 #include "ovsdb-idl.h"
39 #include "poll-loop.h"
42 #include "stream-ssl.h"
46 #include "lib/vswitch-idl.h"
53 VLOG_DEFINE_THIS_MODULE(vsctl);
55 /* vsctl_fatal() also logs the error, so it is preferred in this file. */
56 #define ovs_fatal please_use_vsctl_fatal_instead_of_ovs_fatal
60 /* A command supported by ovs-vsctl. */
61 struct vsctl_command_syntax {
62 const char *name; /* e.g. "add-br" */
63 int min_args; /* Min number of arguments following name. */
64 int max_args; /* Max number of arguments following name. */
66 /* If nonnull, calls ovsdb_idl_add_column() or ovsdb_idl_add_table() for
67 * each column or table in ctx->idl that it uses. */
68 void (*prerequisites)(struct vsctl_context *ctx);
70 /* Does the actual work of the command and puts the command's output, if
71 * any, in ctx->output or ctx->table.
73 * Alternatively, if some prerequisite of the command is not met and the
74 * caller should wait for something to change and then retry, it may set
75 * ctx->try_again to true. (Only the "wait-until" command currently does
77 void (*run)(struct vsctl_context *ctx);
79 /* If nonnull, called after the transaction has been successfully
80 * committed. ctx->output is the output from the "run" function, which
81 * this function may modify and otherwise postprocess as needed. (Only the
82 * "create" command currently does any postprocessing.) */
83 void (*postprocess)(struct vsctl_context *ctx);
85 /* A comma-separated list of supported options, e.g. "--a,--b", or the
86 * empty string if the command does not support any options. */
88 enum { RO, RW } mode; /* Does this command modify the database? */
91 struct vsctl_command {
92 /* Data that remains constant after initialization. */
93 const struct vsctl_command_syntax *syntax;
98 /* Data modified by commands. */
103 /* --db: The database server to contact. */
104 static const char *db;
106 /* --oneline: Write each command's output as a single line? */
109 /* --dry-run: Do not commit any changes. */
112 /* --no-wait: Wait for ovs-vswitchd to reload its configuration? */
113 static bool wait_for_reload = true;
115 /* --timeout: Time to wait for a connection to 'db'. */
118 /* Format for table output. */
119 static struct table_style table_style = TABLE_STYLE_DEFAULT;
121 /* All supported commands. */
122 static const struct vsctl_command_syntax all_commands[];
124 /* The IDL we're using and the current transaction, if any.
125 * This is for use by vsctl_exit() only, to allow it to clean up.
126 * Other code should use its context arguments. */
127 static struct ovsdb_idl *the_idl;
128 static struct ovsdb_idl_txn *the_idl_txn;
130 static void vsctl_exit(int status) NO_RETURN;
131 static void vsctl_fatal(const char *, ...) PRINTF_FORMAT(1, 2) NO_RETURN;
132 static char *default_db(void);
133 static void usage(void) NO_RETURN;
134 static void parse_options(int argc, char *argv[]);
135 static bool might_write_to_db(char **argv);
137 static struct vsctl_command *parse_commands(int argc, char *argv[],
138 size_t *n_commandsp);
139 static void parse_command(int argc, char *argv[], struct vsctl_command *);
140 static const struct vsctl_command_syntax *find_command(const char *name);
141 static void run_prerequisites(struct vsctl_command[], size_t n_commands,
143 static void do_vsctl(const char *args, struct vsctl_command *, size_t n,
146 static const struct vsctl_table_class *get_table(const char *table_name);
147 static void set_column(const struct vsctl_table_class *,
148 const struct ovsdb_idl_row *, const char *arg,
149 struct ovsdb_symbol_table *);
151 static bool is_condition_satisfied(const struct vsctl_table_class *,
152 const struct ovsdb_idl_row *,
154 struct ovsdb_symbol_table *);
157 main(int argc, char *argv[])
159 extern struct vlog_module VLM_reconnect;
160 struct ovsdb_idl *idl;
161 struct vsctl_command *commands;
166 set_program_name(argv[0]);
167 signal(SIGPIPE, SIG_IGN);
168 vlog_set_levels(NULL, VLF_CONSOLE, VLL_WARN);
169 vlog_set_levels(&VLM_reconnect, VLF_ANY_FACILITY, VLL_WARN);
172 /* Log our arguments. This is often valuable for debugging systems. */
173 args = process_escape_args(argv);
174 VLOG(might_write_to_db(argv) ? VLL_INFO : VLL_DBG, "Called as %s", args);
176 /* Parse command line. */
177 parse_options(argc, argv);
178 commands = parse_commands(argc - optind, argv + optind, &n_commands);
184 /* Initialize IDL. */
185 idl = the_idl = ovsdb_idl_create(db, &ovsrec_idl_class, false);
186 run_prerequisites(commands, n_commands, idl);
188 /* Execute the commands.
190 * 'seqno' is the database sequence number for which we last tried to
191 * execute our transaction. There's no point in trying to commit more than
192 * once for any given sequence number, because if the transaction fails
193 * it's because the database changed and we need to obtain an up-to-date
194 * view of the database before we try the transaction again. */
195 seqno = ovsdb_idl_get_seqno(idl);
199 if (seqno != ovsdb_idl_get_seqno(idl)) {
200 seqno = ovsdb_idl_get_seqno(idl);
201 do_vsctl(args, commands, n_commands, idl);
204 if (seqno == ovsdb_idl_get_seqno(idl)) {
212 parse_options(int argc, char *argv[])
215 OPT_DB = UCHAR_MAX + 1,
224 static struct option long_options[] = {
225 {"db", required_argument, NULL, OPT_DB},
226 {"no-syslog", no_argument, NULL, OPT_NO_SYSLOG},
227 {"no-wait", no_argument, NULL, OPT_NO_WAIT},
228 {"dry-run", no_argument, NULL, OPT_DRY_RUN},
229 {"oneline", no_argument, NULL, OPT_ONELINE},
230 {"timeout", required_argument, NULL, 't'},
231 {"help", no_argument, NULL, 'h'},
232 {"version", no_argument, NULL, 'V'},
235 STREAM_SSL_LONG_OPTIONS,
236 {"peer-ca-cert", required_argument, NULL, OPT_PEER_CA_CERT},
239 char *tmp, *short_options;
241 tmp = long_options_to_short_options(long_options);
242 short_options = xasprintf("+%s", tmp);
245 table_style.format = TF_LIST;
250 c = getopt_long(argc, argv, short_options, long_options, NULL);
265 vlog_set_levels(&VLM_vsctl, VLF_SYSLOG, VLL_WARN);
269 wait_for_reload = false;
280 ovs_print_version(0, 0);
284 timeout = strtoul(optarg, NULL, 10);
286 vsctl_fatal("value %s on -t or --timeout is invalid",
292 TABLE_OPTION_HANDLERS(&table_style)
294 STREAM_SSL_OPTION_HANDLERS
296 case OPT_PEER_CA_CERT:
297 stream_ssl_set_peer_ca_cert_file(optarg);
314 static struct vsctl_command *
315 parse_commands(int argc, char *argv[], size_t *n_commandsp)
317 struct vsctl_command *commands;
318 size_t n_commands, allocated_commands;
322 n_commands = allocated_commands = 0;
324 for (start = i = 0; i <= argc; i++) {
325 if (i == argc || !strcmp(argv[i], "--")) {
327 if (n_commands >= allocated_commands) {
328 struct vsctl_command *c;
330 commands = x2nrealloc(commands, &allocated_commands,
332 for (c = commands; c < &commands[n_commands]; c++) {
333 shash_moved(&c->options);
336 parse_command(i - start, &argv[start],
337 &commands[n_commands++]);
343 vsctl_fatal("missing command name (use --help for help)");
345 *n_commandsp = n_commands;
350 parse_command(int argc, char *argv[], struct vsctl_command *command)
352 const struct vsctl_command_syntax *p;
353 struct shash_node *node;
357 shash_init(&command->options);
358 for (i = 0; i < argc; i++) {
359 const char *option = argv[i];
363 if (option[0] != '-') {
367 equals = strchr(option, '=');
369 key = xmemdup0(option, equals - option);
370 value = xstrdup(equals + 1);
372 key = xstrdup(option);
376 if (shash_find(&command->options, key)) {
377 vsctl_fatal("'%s' option specified multiple times", argv[i]);
379 shash_add_nocopy(&command->options, key, value);
382 vsctl_fatal("missing command name");
385 p = find_command(argv[i]);
387 vsctl_fatal("unknown command '%s'; use --help for help", argv[i]);
390 SHASH_FOR_EACH (node, &command->options) {
391 const char *s = strstr(p->options, node->name);
392 int end = s ? s[strlen(node->name)] : EOF;
394 if (end != '=' && end != ',' && end != ' ' && end != '\0') {
395 vsctl_fatal("'%s' command has no '%s' option",
396 argv[i], node->name);
398 if ((end == '=') != (node->data != NULL)) {
400 vsctl_fatal("missing argument to '%s' option on '%s' "
401 "command", node->name, argv[i]);
403 vsctl_fatal("'%s' option on '%s' does not accept an "
404 "argument", node->name, argv[i]);
409 n_arg = argc - i - 1;
410 if (n_arg < p->min_args) {
411 vsctl_fatal("'%s' command requires at least %d arguments",
412 p->name, p->min_args);
413 } else if (n_arg > p->max_args) {
416 for (j = i + 1; j < argc; j++) {
417 if (argv[j][0] == '-') {
418 vsctl_fatal("'%s' command takes at most %d arguments "
419 "(note that options must precede command "
420 "names and follow a \"--\" argument)",
421 p->name, p->max_args);
425 vsctl_fatal("'%s' command takes at most %d arguments",
426 p->name, p->max_args);
430 command->argc = n_arg + 1;
431 command->argv = &argv[i];
434 /* Returns the "struct vsctl_command_syntax" for a given command 'name', or a
435 * null pointer if there is none. */
436 static const struct vsctl_command_syntax *
437 find_command(const char *name)
439 static struct shash commands = SHASH_INITIALIZER(&commands);
441 if (shash_is_empty(&commands)) {
442 const struct vsctl_command_syntax *p;
444 for (p = all_commands; p->name; p++) {
445 shash_add_assert(&commands, p->name, p);
449 return shash_find_data(&commands, name);
453 vsctl_fatal(const char *format, ...)
458 va_start(args, format);
459 message = xvasprintf(format, args);
462 vlog_set_levels(&VLM_vsctl, VLF_CONSOLE, VLL_OFF);
463 VLOG_ERR("%s", message);
464 ovs_error(0, "%s", message);
465 vsctl_exit(EXIT_FAILURE);
468 /* Frees the current transaction and the underlying IDL and then calls
471 * Freeing the transaction and the IDL is not strictly necessary, but it makes
472 * for a clean memory leak report from valgrind in the normal case. That makes
473 * it easier to notice real memory leaks. */
475 vsctl_exit(int status)
478 ovsdb_idl_txn_abort(the_idl_txn);
479 ovsdb_idl_txn_destroy(the_idl_txn);
481 ovsdb_idl_destroy(the_idl);
489 %s: ovs-vswitchd management utility\n\
490 usage: %s [OPTIONS] COMMAND [ARG...]\n\
492 Open vSwitch commands:\n\
493 init initialize database, if not yet initialized\n\
494 show print overview of database contents\n\
495 emer-reset reset configuration to clean state\n\
498 add-br BRIDGE create a new bridge named BRIDGE\n\
499 add-br BRIDGE PARENT VLAN create new fake BRIDGE in PARENT on VLAN\n\
500 del-br BRIDGE delete BRIDGE and all of its ports\n\
501 list-br print the names of all the bridges\n\
502 br-exists BRIDGE exit 2 if BRIDGE does not exist\n\
503 br-to-vlan BRIDGE print the VLAN which BRIDGE is on\n\
504 br-to-parent BRIDGE print the parent of BRIDGE\n\
505 br-set-external-id BRIDGE KEY VALUE set KEY on BRIDGE to VALUE\n\
506 br-set-external-id BRIDGE KEY unset KEY on BRIDGE\n\
507 br-get-external-id BRIDGE KEY print value of KEY on BRIDGE\n\
508 br-get-external-id BRIDGE list key-value pairs on BRIDGE\n\
510 Port commands (a bond is considered to be a single port):\n\
511 list-ports BRIDGE print the names of all the ports on BRIDGE\n\
512 add-port BRIDGE PORT add network device PORT to BRIDGE\n\
513 add-bond BRIDGE PORT IFACE... add bonded port PORT in BRIDGE from IFACES\n\
514 del-port [BRIDGE] PORT delete PORT (which may be bonded) from BRIDGE\n\
515 port-to-br PORT print name of bridge that contains PORT\n\
517 Interface commands (a bond consists of multiple interfaces):\n\
518 list-ifaces BRIDGE print the names of all interfaces on BRIDGE\n\
519 iface-to-br IFACE print name of bridge that contains IFACE\n\
521 Controller commands:\n\
522 get-controller BRIDGE print the controllers for BRIDGE\n\
523 del-controller BRIDGE delete the controllers for BRIDGE\n\
524 set-controller BRIDGE TARGET... set the controllers for BRIDGE\n\
525 get-fail-mode BRIDGE print the fail-mode for BRIDGE\n\
526 del-fail-mode BRIDGE delete the fail-mode for BRIDGE\n\
527 set-fail-mode BRIDGE MODE set the fail-mode for BRIDGE to MODE\n\
530 get-manager print the managers\n\
531 del-manager delete the managers\n\
532 set-manager TARGET... set the list of managers to TARGET...\n\
535 get-ssl print the SSL configuration\n\
536 del-ssl delete the SSL configuration\n\
537 set-ssl PRIV-KEY CERT CA-CERT set the SSL configuration\n\
540 emer-reset reset switch to known good state\n\
542 Database commands:\n\
543 list TBL [REC] list RECord (or all records) in TBL\n\
544 find TBL CONDITION... list records satisfying CONDITION in TBL\n\
545 get TBL REC COL[:KEY] print values of COLumns in RECord in TBL\n\
546 set TBL REC COL[:KEY]=VALUE set COLumn values in RECord in TBL\n\
547 add TBL REC COL [KEY=]VALUE add (KEY=)VALUE to COLumn in RECord in TBL\n\
548 remove TBL REC COL [KEY=]VALUE remove (KEY=)VALUE from COLumn\n\
549 clear TBL REC COL clear values from COLumn in RECord in TBL\n\
550 create TBL COL[:KEY]=VALUE create and initialize new record\n\
551 destroy TBL REC delete RECord from TBL\n\
552 wait-until TBL REC [COL[:KEY]=VALUE] wait until condition is true\n\
553 Potentially unsafe database commands require --force option.\n\
556 --db=DATABASE connect to DATABASE\n\
558 --no-wait do not wait for ovs-vswitchd to reconfigure\n\
559 -t, --timeout=SECS wait at most SECS seconds for ovs-vswitchd\n\
560 --dry-run do not commit changes to database\n\
561 --oneline print exactly one line of output per command\n",
562 program_name, program_name, default_db());
565 --no-syslog equivalent to --verbose=vsctl:syslog:warn\n");
566 stream_usage("database", true, true, false);
569 -h, --help display this help message\n\
570 -V, --version display version information\n");
579 def = xasprintf("unix:%s/db.sock", ovs_rundir());
584 /* Returns true if it looks like this set of arguments might modify the
585 * database, otherwise false. (Not very smart, so it's prone to false
588 might_write_to_db(char **argv)
590 for (; *argv; argv++) {
591 const struct vsctl_command_syntax *p = find_command(*argv);
592 if (p && p->mode == RW) {
599 struct vsctl_context {
603 struct shash options;
605 /* Modifiable state. */
608 struct ovsdb_idl *idl;
609 struct ovsdb_idl_txn *txn;
610 struct ovsdb_symbol_table *symtab;
611 const struct ovsrec_open_vswitch *ovs;
614 /* A cache of the contents of the database.
616 * A command that needs to use any of this information must first call
617 * vsctl_context_populate_cache(). A command that changes anything that
618 * could invalidate the cache must either call
619 * vsctl_context_invalidate_cache() or manually update the cache to
620 * maintain its correctness. */
622 struct shash bridges; /* Maps from bridge name to struct vsctl_bridge. */
623 struct shash ports; /* Maps from port name to struct vsctl_port. */
624 struct shash ifaces; /* Maps from port name to struct vsctl_iface. */
626 /* A command may set this member to true if some prerequisite is not met
627 * and the caller should wait for something to change and then retry. */
631 struct vsctl_bridge {
632 struct ovsrec_bridge *br_cfg;
634 struct list ports; /* Contains "struct vsctl_port"s. */
636 /* VLAN ("fake") bridge support.
638 * Use 'parent != NULL' to detect a fake bridge, because 'vlan' can be 0
640 struct hmap children; /* VLAN bridges indexed by 'vlan'. */
641 struct hmap_node children_node; /* Node in parent's 'children' hmap. */
642 struct vsctl_bridge *parent; /* Real bridge, or NULL. */
643 int vlan; /* VLAN VID (0...4095), or 0. */
647 struct list ports_node; /* In struct vsctl_bridge's 'ports' list. */
648 struct list ifaces; /* Contains "struct vsctl_iface"s. */
649 struct ovsrec_port *port_cfg;
650 struct vsctl_bridge *bridge;
654 struct list ifaces_node; /* In struct vsctl_port's 'ifaces' list. */
655 struct ovsrec_interface *iface_cfg;
656 struct vsctl_port *port;
660 vsctl_context_to_string(const struct vsctl_context *ctx)
662 const struct shash_node *node;
668 SHASH_FOR_EACH (node, &ctx->options) {
669 svec_add(&words, node->name);
671 for (i = 0; i < ctx->argc; i++) {
672 svec_add(&words, ctx->argv[i]);
674 svec_terminate(&words);
676 s = process_escape_args(words.names);
678 svec_destroy(&words);
684 verify_ports(struct vsctl_context *ctx)
686 if (!ctx->verified_ports) {
687 const struct ovsrec_bridge *bridge;
688 const struct ovsrec_port *port;
690 ovsrec_open_vswitch_verify_bridges(ctx->ovs);
691 OVSREC_BRIDGE_FOR_EACH (bridge, ctx->idl) {
692 ovsrec_bridge_verify_ports(bridge);
694 OVSREC_PORT_FOR_EACH (port, ctx->idl) {
695 ovsrec_port_verify_interfaces(port);
698 ctx->verified_ports = true;
702 static struct vsctl_bridge *
703 add_bridge_to_cache(struct vsctl_context *ctx,
704 struct ovsrec_bridge *br_cfg, const char *name,
705 struct vsctl_bridge *parent, int vlan)
707 struct vsctl_bridge *br = xmalloc(sizeof *br);
709 br->name = xstrdup(name);
710 list_init(&br->ports);
713 hmap_init(&br->children);
715 hmap_insert(&parent->children, &br->children_node, hash_int(vlan, 0));
717 shash_add(&ctx->bridges, br->name, br);
722 ovs_delete_bridge(const struct ovsrec_open_vswitch *ovs,
723 struct ovsrec_bridge *bridge)
725 struct ovsrec_bridge **bridges;
728 bridges = xmalloc(sizeof *ovs->bridges * ovs->n_bridges);
729 for (i = n = 0; i < ovs->n_bridges; i++) {
730 if (ovs->bridges[i] != bridge) {
731 bridges[n++] = ovs->bridges[i];
734 ovsrec_open_vswitch_set_bridges(ovs, bridges, n);
739 del_cached_bridge(struct vsctl_context *ctx, struct vsctl_bridge *br)
741 assert(list_is_empty(&br->ports));
742 assert(hmap_is_empty(&br->children));
744 hmap_remove(&br->parent->children, &br->children_node);
747 ovsrec_bridge_delete(br->br_cfg);
748 ovs_delete_bridge(ctx->ovs, br->br_cfg);
750 shash_find_and_delete(&ctx->bridges, br->name);
751 hmap_destroy(&br->children);
757 port_is_fake_bridge(const struct ovsrec_port *port_cfg)
759 return (port_cfg->fake_bridge
761 && *port_cfg->tag >= 0 && *port_cfg->tag <= 4095);
764 static struct vsctl_bridge *
765 find_vlan_bridge(struct vsctl_bridge *parent, int vlan)
767 struct vsctl_bridge *child;
769 HMAP_FOR_EACH_IN_BUCKET (child, children_node, hash_int(vlan, 0),
771 if (child->vlan == vlan) {
779 static struct vsctl_port *
780 add_port_to_cache(struct vsctl_context *ctx, struct vsctl_bridge *parent,
781 struct ovsrec_port *port_cfg)
783 struct vsctl_port *port;
786 && *port_cfg->tag >= 0 && *port_cfg->tag <= 4095) {
787 struct vsctl_bridge *vlan_bridge;
789 vlan_bridge = find_vlan_bridge(parent, *port_cfg->tag);
791 parent = vlan_bridge;
795 port = xmalloc(sizeof *port);
796 list_push_back(&parent->ports, &port->ports_node);
797 list_init(&port->ifaces);
798 port->port_cfg = port_cfg;
799 port->bridge = parent;
800 shash_add(&ctx->ports, port_cfg->name, port);
806 del_cached_port(struct vsctl_context *ctx, struct vsctl_port *port)
808 assert(list_is_empty(&port->ifaces));
809 list_remove(&port->ports_node);
810 shash_find_and_delete(&ctx->ports, port->port_cfg->name);
811 ovsrec_port_delete(port->port_cfg);
815 static struct vsctl_iface *
816 add_iface_to_cache(struct vsctl_context *ctx, struct vsctl_port *parent,
817 struct ovsrec_interface *iface_cfg)
819 struct vsctl_iface *iface;
821 iface = xmalloc(sizeof *iface);
822 list_push_back(&parent->ifaces, &iface->ifaces_node);
823 iface->iface_cfg = iface_cfg;
824 iface->port = parent;
825 shash_add(&ctx->ifaces, iface_cfg->name, iface);
831 del_cached_iface(struct vsctl_context *ctx, struct vsctl_iface *iface)
833 list_remove(&iface->ifaces_node);
834 shash_find_and_delete(&ctx->ifaces, iface->iface_cfg->name);
835 ovsrec_interface_delete(iface->iface_cfg);
840 vsctl_context_invalidate_cache(struct vsctl_context *ctx)
842 struct shash_node *node;
844 if (!ctx->cache_valid) {
847 ctx->cache_valid = false;
849 SHASH_FOR_EACH (node, &ctx->bridges) {
850 struct vsctl_bridge *bridge = node->data;
851 hmap_destroy(&bridge->children);
855 shash_destroy(&ctx->bridges);
857 shash_destroy_free_data(&ctx->ports);
858 shash_destroy_free_data(&ctx->ifaces);
862 pre_get_info(struct vsctl_context *ctx)
864 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_bridges);
866 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_name);
867 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_controller);
868 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_fail_mode);
869 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_ports);
871 ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_name);
872 ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_fake_bridge);
873 ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_tag);
874 ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_interfaces);
876 ovsdb_idl_add_column(ctx->idl, &ovsrec_interface_col_name);
880 vsctl_context_populate_cache(struct vsctl_context *ctx)
882 const struct ovsrec_open_vswitch *ovs = ctx->ovs;
883 struct sset bridges, ports;
886 if (ctx->cache_valid) {
887 /* Cache is already populated. */
890 ctx->cache_valid = true;
891 shash_init(&ctx->bridges);
892 shash_init(&ctx->ports);
893 shash_init(&ctx->ifaces);
897 for (i = 0; i < ovs->n_bridges; i++) {
898 struct ovsrec_bridge *br_cfg = ovs->bridges[i];
899 struct vsctl_bridge *br;
902 if (!sset_add(&bridges, br_cfg->name)) {
903 VLOG_WARN("%s: database contains duplicate bridge name",
907 br = add_bridge_to_cache(ctx, br_cfg, br_cfg->name, NULL, 0);
912 for (j = 0; j < br_cfg->n_ports; j++) {
913 struct ovsrec_port *port_cfg = br_cfg->ports[j];
915 if (!sset_add(&ports, port_cfg->name)) {
916 /* Duplicate port name. (We will warn about that later.) */
920 if (port_is_fake_bridge(port_cfg)
921 && sset_add(&bridges, port_cfg->name)) {
922 add_bridge_to_cache(ctx, NULL, port_cfg->name, br,
927 sset_destroy(&bridges);
928 sset_destroy(&ports);
931 for (i = 0; i < ovs->n_bridges; i++) {
932 struct ovsrec_bridge *br_cfg = ovs->bridges[i];
933 struct vsctl_bridge *br;
936 if (!sset_add(&bridges, br_cfg->name)) {
939 br = shash_find_data(&ctx->bridges, br_cfg->name);
940 for (j = 0; j < br_cfg->n_ports; j++) {
941 struct ovsrec_port *port_cfg = br_cfg->ports[j];
942 struct vsctl_port *port;
945 port = shash_find_data(&ctx->ports, port_cfg->name);
947 if (port_cfg == port->port_cfg) {
948 VLOG_WARN("%s: port is in multiple bridges (%s and %s)",
949 port_cfg->name, br->name, port->bridge->name);
951 /* Log as an error because this violates the database's
952 * uniqueness constraints, so the database server shouldn't
953 * have allowed it. */
954 VLOG_ERR("%s: database contains duplicate port name",
960 if (port_is_fake_bridge(port_cfg)
961 && !sset_add(&bridges, port_cfg->name)) {
965 port = add_port_to_cache(ctx, br, port_cfg);
966 for (k = 0; k < port_cfg->n_interfaces; k++) {
967 struct ovsrec_interface *iface_cfg = port_cfg->interfaces[k];
968 struct vsctl_iface *iface;
970 iface = shash_find_data(&ctx->ifaces, iface_cfg->name);
972 if (iface_cfg == iface->iface_cfg) {
973 VLOG_WARN("%s: interface is in multiple ports "
976 iface->port->port_cfg->name,
977 port->port_cfg->name);
979 /* Log as an error because this violates the database's
980 * uniqueness constraints, so the database server
981 * shouldn't have allowed it. */
982 VLOG_ERR("%s: database contains duplicate interface "
983 "name", iface_cfg->name);
988 add_iface_to_cache(ctx, port, iface_cfg);
992 sset_destroy(&bridges);
996 check_conflicts(struct vsctl_context *ctx, const char *name,
999 struct vsctl_iface *iface;
1000 struct vsctl_port *port;
1004 if (shash_find(&ctx->bridges, name)) {
1005 vsctl_fatal("%s because a bridge named %s already exists",
1009 port = shash_find_data(&ctx->ports, name);
1011 vsctl_fatal("%s because a port named %s already exists on "
1012 "bridge %s", msg, name, port->bridge->name);
1015 iface = shash_find_data(&ctx->ifaces, name);
1017 vsctl_fatal("%s because an interface named %s already exists "
1018 "on bridge %s", msg, name, iface->port->bridge->name);
1024 static struct vsctl_bridge *
1025 find_bridge(struct vsctl_context *ctx, const char *name, bool must_exist)
1027 struct vsctl_bridge *br;
1029 assert(ctx->cache_valid);
1031 br = shash_find_data(&ctx->bridges, name);
1032 if (must_exist && !br) {
1033 vsctl_fatal("no bridge named %s", name);
1035 ovsrec_open_vswitch_verify_bridges(ctx->ovs);
1039 static struct vsctl_bridge *
1040 find_real_bridge(struct vsctl_context *ctx, const char *name, bool must_exist)
1042 struct vsctl_bridge *br = find_bridge(ctx, name, must_exist);
1043 if (br && br->parent) {
1044 vsctl_fatal("%s is a fake bridge", name);
1049 static struct vsctl_port *
1050 find_port(struct vsctl_context *ctx, const char *name, bool must_exist)
1052 struct vsctl_port *port;
1054 assert(ctx->cache_valid);
1056 port = shash_find_data(&ctx->ports, name);
1057 if (port && !strcmp(name, port->bridge->name)) {
1060 if (must_exist && !port) {
1061 vsctl_fatal("no port named %s", name);
1067 static struct vsctl_iface *
1068 find_iface(struct vsctl_context *ctx, const char *name, bool must_exist)
1070 struct vsctl_iface *iface;
1072 assert(ctx->cache_valid);
1074 iface = shash_find_data(&ctx->ifaces, name);
1075 if (iface && !strcmp(name, iface->port->bridge->name)) {
1078 if (must_exist && !iface) {
1079 vsctl_fatal("no interface named %s", name);
1086 bridge_insert_port(struct ovsrec_bridge *br, struct ovsrec_port *port)
1088 struct ovsrec_port **ports;
1091 ports = xmalloc(sizeof *br->ports * (br->n_ports + 1));
1092 for (i = 0; i < br->n_ports; i++) {
1093 ports[i] = br->ports[i];
1095 ports[br->n_ports] = port;
1096 ovsrec_bridge_set_ports(br, ports, br->n_ports + 1);
1101 bridge_delete_port(struct ovsrec_bridge *br, struct ovsrec_port *port)
1103 struct ovsrec_port **ports;
1106 ports = xmalloc(sizeof *br->ports * br->n_ports);
1107 for (i = n = 0; i < br->n_ports; i++) {
1108 if (br->ports[i] != port) {
1109 ports[n++] = br->ports[i];
1112 ovsrec_bridge_set_ports(br, ports, n);
1117 ovs_insert_bridge(const struct ovsrec_open_vswitch *ovs,
1118 struct ovsrec_bridge *bridge)
1120 struct ovsrec_bridge **bridges;
1123 bridges = xmalloc(sizeof *ovs->bridges * (ovs->n_bridges + 1));
1124 for (i = 0; i < ovs->n_bridges; i++) {
1125 bridges[i] = ovs->bridges[i];
1127 bridges[ovs->n_bridges] = bridge;
1128 ovsrec_open_vswitch_set_bridges(ovs, bridges, ovs->n_bridges + 1);
1133 cmd_init(struct vsctl_context *ctx OVS_UNUSED)
1137 struct cmd_show_table {
1138 const struct ovsdb_idl_table_class *table;
1139 const struct ovsdb_idl_column *name_column;
1140 const struct ovsdb_idl_column *columns[3];
1144 static struct cmd_show_table cmd_show_tables[] = {
1145 {&ovsrec_table_open_vswitch,
1147 {&ovsrec_open_vswitch_col_manager_options,
1148 &ovsrec_open_vswitch_col_bridges,
1149 &ovsrec_open_vswitch_col_ovs_version},
1152 {&ovsrec_table_bridge,
1153 &ovsrec_bridge_col_name,
1154 {&ovsrec_bridge_col_controller,
1155 &ovsrec_bridge_col_fail_mode,
1156 &ovsrec_bridge_col_ports},
1159 {&ovsrec_table_port,
1160 &ovsrec_port_col_name,
1161 {&ovsrec_port_col_tag,
1162 &ovsrec_port_col_trunks,
1163 &ovsrec_port_col_interfaces},
1166 {&ovsrec_table_interface,
1167 &ovsrec_interface_col_name,
1168 {&ovsrec_interface_col_type,
1169 &ovsrec_interface_col_options,
1173 {&ovsrec_table_controller,
1174 &ovsrec_controller_col_target,
1175 {&ovsrec_controller_col_is_connected,
1180 {&ovsrec_table_manager,
1181 &ovsrec_manager_col_target,
1182 {&ovsrec_manager_col_is_connected,
1189 pre_cmd_show(struct vsctl_context *ctx)
1191 struct cmd_show_table *show;
1193 for (show = cmd_show_tables;
1194 show < &cmd_show_tables[ARRAY_SIZE(cmd_show_tables)];
1198 ovsdb_idl_add_table(ctx->idl, show->table);
1199 if (show->name_column) {
1200 ovsdb_idl_add_column(ctx->idl, show->name_column);
1202 for (i = 0; i < ARRAY_SIZE(show->columns); i++) {
1203 const struct ovsdb_idl_column *column = show->columns[i];
1205 ovsdb_idl_add_column(ctx->idl, column);
1211 static struct cmd_show_table *
1212 cmd_show_find_table_by_row(const struct ovsdb_idl_row *row)
1214 struct cmd_show_table *show;
1216 for (show = cmd_show_tables;
1217 show < &cmd_show_tables[ARRAY_SIZE(cmd_show_tables)];
1219 if (show->table == row->table->class) {
1226 static struct cmd_show_table *
1227 cmd_show_find_table_by_name(const char *name)
1229 struct cmd_show_table *show;
1231 for (show = cmd_show_tables;
1232 show < &cmd_show_tables[ARRAY_SIZE(cmd_show_tables)];
1234 if (!strcmp(show->table->name, name)) {
1242 cmd_show_row(struct vsctl_context *ctx, const struct ovsdb_idl_row *row,
1245 struct cmd_show_table *show = cmd_show_find_table_by_row(row);
1248 ds_put_char_multiple(&ctx->output, ' ', level * 4);
1249 if (show && show->name_column) {
1250 const struct ovsdb_datum *datum;
1252 ds_put_format(&ctx->output, "%s ", show->table->name);
1253 datum = ovsdb_idl_read(row, show->name_column);
1254 ovsdb_datum_to_string(datum, &show->name_column->type, &ctx->output);
1256 ds_put_format(&ctx->output, UUID_FMT, UUID_ARGS(&row->uuid));
1258 ds_put_char(&ctx->output, '\n');
1260 if (!show || show->recurse) {
1264 show->recurse = true;
1265 for (i = 0; i < ARRAY_SIZE(show->columns); i++) {
1266 const struct ovsdb_idl_column *column = show->columns[i];
1267 const struct ovsdb_datum *datum;
1273 datum = ovsdb_idl_read(row, column);
1274 if (column->type.key.type == OVSDB_TYPE_UUID &&
1275 column->type.key.u.uuid.refTableName) {
1276 struct cmd_show_table *ref_show;
1279 ref_show = cmd_show_find_table_by_name(
1280 column->type.key.u.uuid.refTableName);
1282 for (j = 0; j < datum->n; j++) {
1283 const struct ovsdb_idl_row *ref_row;
1285 ref_row = ovsdb_idl_get_row_for_uuid(ctx->idl,
1287 &datum->keys[j].uuid);
1289 cmd_show_row(ctx, ref_row, level + 1);
1296 if (!ovsdb_datum_is_default(datum, &column->type)) {
1297 ds_put_char_multiple(&ctx->output, ' ', (level + 1) * 4);
1298 ds_put_format(&ctx->output, "%s: ", column->name);
1299 ovsdb_datum_to_string(datum, &column->type, &ctx->output);
1300 ds_put_char(&ctx->output, '\n');
1303 show->recurse = false;
1307 cmd_show(struct vsctl_context *ctx)
1309 const struct ovsdb_idl_row *row;
1311 for (row = ovsdb_idl_first_row(ctx->idl, cmd_show_tables[0].table);
1312 row; row = ovsdb_idl_next_row(row)) {
1313 cmd_show_row(ctx, row, 0);
1318 pre_cmd_emer_reset(struct vsctl_context *ctx)
1320 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_manager_options);
1321 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_ssl);
1323 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_controller);
1324 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_fail_mode);
1325 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_mirrors);
1326 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_netflow);
1327 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_sflow);
1328 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_flood_vlans);
1329 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_other_config);
1331 ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_other_config);
1333 ovsdb_idl_add_column(ctx->idl,
1334 &ovsrec_interface_col_ingress_policing_rate);
1335 ovsdb_idl_add_column(ctx->idl,
1336 &ovsrec_interface_col_ingress_policing_burst);
1340 cmd_emer_reset(struct vsctl_context *ctx)
1342 const struct ovsdb_idl *idl = ctx->idl;
1343 const struct ovsrec_bridge *br;
1344 const struct ovsrec_port *port;
1345 const struct ovsrec_interface *iface;
1346 const struct ovsrec_mirror *mirror, *next_mirror;
1347 const struct ovsrec_controller *ctrl, *next_ctrl;
1348 const struct ovsrec_manager *mgr, *next_mgr;
1349 const struct ovsrec_netflow *nf, *next_nf;
1350 const struct ovsrec_ssl *ssl, *next_ssl;
1351 const struct ovsrec_sflow *sflow, *next_sflow;
1353 /* Reset the Open_vSwitch table. */
1354 ovsrec_open_vswitch_set_manager_options(ctx->ovs, NULL, 0);
1355 ovsrec_open_vswitch_set_ssl(ctx->ovs, NULL);
1357 OVSREC_BRIDGE_FOR_EACH (br, idl) {
1360 ovsrec_bridge_set_controller(br, NULL, 0);
1361 ovsrec_bridge_set_fail_mode(br, NULL);
1362 ovsrec_bridge_set_mirrors(br, NULL, 0);
1363 ovsrec_bridge_set_netflow(br, NULL);
1364 ovsrec_bridge_set_sflow(br, NULL);
1365 ovsrec_bridge_set_flood_vlans(br, NULL, 0);
1367 /* We only want to save the "hwaddr" key from other_config. */
1368 hwaddr = smap_get(&br->other_config, "hwaddr");
1370 struct smap smap = SMAP_INITIALIZER(&smap);
1371 smap_add(&smap, "hwaddr", hwaddr);
1372 ovsrec_bridge_set_other_config(br, &smap);
1373 smap_destroy(&smap);
1375 ovsrec_bridge_set_other_config(br, NULL);
1379 OVSREC_PORT_FOR_EACH (port, idl) {
1380 ovsrec_port_set_other_config(port, NULL);
1383 OVSREC_INTERFACE_FOR_EACH (iface, idl) {
1384 /* xxx What do we do about gre/patch devices created by mgr? */
1386 ovsrec_interface_set_ingress_policing_rate(iface, 0);
1387 ovsrec_interface_set_ingress_policing_burst(iface, 0);
1390 OVSREC_MIRROR_FOR_EACH_SAFE (mirror, next_mirror, idl) {
1391 ovsrec_mirror_delete(mirror);
1394 OVSREC_CONTROLLER_FOR_EACH_SAFE (ctrl, next_ctrl, idl) {
1395 ovsrec_controller_delete(ctrl);
1398 OVSREC_MANAGER_FOR_EACH_SAFE (mgr, next_mgr, idl) {
1399 ovsrec_manager_delete(mgr);
1402 OVSREC_NETFLOW_FOR_EACH_SAFE (nf, next_nf, idl) {
1403 ovsrec_netflow_delete(nf);
1406 OVSREC_SSL_FOR_EACH_SAFE (ssl, next_ssl, idl) {
1407 ovsrec_ssl_delete(ssl);
1410 OVSREC_SFLOW_FOR_EACH_SAFE (sflow, next_sflow, idl) {
1411 ovsrec_sflow_delete(sflow);
1414 vsctl_context_invalidate_cache(ctx);
1418 cmd_add_br(struct vsctl_context *ctx)
1420 bool may_exist = shash_find(&ctx->options, "--may-exist") != NULL;
1421 const char *br_name, *parent_name;
1424 br_name = ctx->argv[1];
1425 if (ctx->argc == 2) {
1428 } else if (ctx->argc == 4) {
1429 parent_name = ctx->argv[2];
1430 vlan = atoi(ctx->argv[3]);
1431 if (vlan < 0 || vlan > 4095) {
1432 vsctl_fatal("%s: vlan must be between 0 and 4095", ctx->argv[0]);
1435 vsctl_fatal("'%s' command takes exactly 1 or 3 arguments",
1439 vsctl_context_populate_cache(ctx);
1441 struct vsctl_bridge *br;
1443 br = find_bridge(ctx, br_name, false);
1447 vsctl_fatal("\"--may-exist add-br %s\" but %s is "
1448 "a VLAN bridge for VLAN %d",
1449 br_name, br_name, br->vlan);
1453 vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
1454 "is not a VLAN bridge",
1455 br_name, parent_name, vlan, br_name);
1456 } else if (strcmp(br->parent->name, parent_name)) {
1457 vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
1458 "has the wrong parent %s",
1459 br_name, parent_name, vlan,
1460 br_name, br->parent->name);
1461 } else if (br->vlan != vlan) {
1462 vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
1463 "is a VLAN bridge for the wrong VLAN %d",
1464 br_name, parent_name, vlan, br_name, br->vlan);
1470 check_conflicts(ctx, br_name,
1471 xasprintf("cannot create a bridge named %s", br_name));
1474 struct ovsrec_port *port;
1475 struct ovsrec_interface *iface;
1476 struct ovsrec_bridge *br;
1478 iface = ovsrec_interface_insert(ctx->txn);
1479 ovsrec_interface_set_name(iface, br_name);
1480 ovsrec_interface_set_type(iface, "internal");
1482 port = ovsrec_port_insert(ctx->txn);
1483 ovsrec_port_set_name(port, br_name);
1484 ovsrec_port_set_interfaces(port, &iface, 1);
1486 br = ovsrec_bridge_insert(ctx->txn);
1487 ovsrec_bridge_set_name(br, br_name);
1488 ovsrec_bridge_set_ports(br, &port, 1);
1490 ovs_insert_bridge(ctx->ovs, br);
1492 struct vsctl_bridge *parent;
1493 struct ovsrec_port *port;
1494 struct ovsrec_interface *iface;
1495 struct ovsrec_bridge *br;
1498 parent = find_bridge(ctx, parent_name, false);
1499 if (parent && parent->parent) {
1500 vsctl_fatal("cannot create bridge with fake bridge as parent");
1503 vsctl_fatal("parent bridge %s does not exist", parent_name);
1505 br = parent->br_cfg;
1507 iface = ovsrec_interface_insert(ctx->txn);
1508 ovsrec_interface_set_name(iface, br_name);
1509 ovsrec_interface_set_type(iface, "internal");
1511 port = ovsrec_port_insert(ctx->txn);
1512 ovsrec_port_set_name(port, br_name);
1513 ovsrec_port_set_interfaces(port, &iface, 1);
1514 ovsrec_port_set_fake_bridge(port, true);
1515 ovsrec_port_set_tag(port, &tag, 1);
1517 bridge_insert_port(br, port);
1520 vsctl_context_invalidate_cache(ctx);
1524 del_port(struct vsctl_context *ctx, struct vsctl_port *port)
1526 struct vsctl_iface *iface, *next_iface;
1528 bridge_delete_port((port->bridge->parent
1529 ? port->bridge->parent->br_cfg
1530 : port->bridge->br_cfg), port->port_cfg);
1532 LIST_FOR_EACH_SAFE (iface, next_iface, ifaces_node, &port->ifaces) {
1533 del_cached_iface(ctx, iface);
1535 del_cached_port(ctx, port);
1539 del_bridge(struct vsctl_context *ctx, struct vsctl_bridge *br)
1541 struct vsctl_bridge *child, *next_child;
1542 struct vsctl_port *port, *next_port;
1544 HMAP_FOR_EACH_SAFE (child, next_child, children_node, &br->children) {
1545 del_bridge(ctx, child);
1548 LIST_FOR_EACH_SAFE (port, next_port, ports_node, &br->ports) {
1549 del_port(ctx, port);
1552 del_cached_bridge(ctx, br);
1556 cmd_del_br(struct vsctl_context *ctx)
1558 bool must_exist = !shash_find(&ctx->options, "--if-exists");
1559 struct vsctl_bridge *bridge;
1561 vsctl_context_populate_cache(ctx);
1562 bridge = find_bridge(ctx, ctx->argv[1], must_exist);
1564 del_bridge(ctx, bridge);
1569 output_sorted(struct svec *svec, struct ds *output)
1575 SVEC_FOR_EACH (i, name, svec) {
1576 ds_put_format(output, "%s\n", name);
1581 cmd_list_br(struct vsctl_context *ctx)
1583 struct shash_node *node;
1584 struct svec bridges;
1585 bool real = shash_find(&ctx->options, "--real");
1586 bool fake = shash_find(&ctx->options, "--fake");
1588 /* If neither fake nor real were requested, return both. */
1589 if (!real && !fake) {
1593 vsctl_context_populate_cache(ctx);
1595 svec_init(&bridges);
1596 SHASH_FOR_EACH (node, &ctx->bridges) {
1597 struct vsctl_bridge *br = node->data;
1599 if (br->parent ? fake : real) {
1600 svec_add(&bridges, br->name);
1603 output_sorted(&bridges, &ctx->output);
1604 svec_destroy(&bridges);
1608 cmd_br_exists(struct vsctl_context *ctx)
1610 vsctl_context_populate_cache(ctx);
1611 if (!find_bridge(ctx, ctx->argv[1], false)) {
1617 set_external_id(struct smap *old, struct smap *new,
1618 char *key, char *value)
1620 smap_clone(new, old);
1623 smap_replace(new, key, value);
1625 smap_remove(new, key);
1630 pre_cmd_br_set_external_id(struct vsctl_context *ctx)
1633 ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_external_ids);
1634 ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_external_ids);
1638 cmd_br_set_external_id(struct vsctl_context *ctx)
1640 struct vsctl_bridge *bridge;
1643 vsctl_context_populate_cache(ctx);
1644 bridge = find_bridge(ctx, ctx->argv[1], true);
1645 if (bridge->br_cfg) {
1647 set_external_id(&bridge->br_cfg->external_ids, &new, ctx->argv[2],
1648 ctx->argc >= 4 ? ctx->argv[3] : NULL);
1649 ovsrec_bridge_verify_external_ids(bridge->br_cfg);
1650 ovsrec_bridge_set_external_ids(bridge->br_cfg, &new);
1652 char *key = xasprintf("fake-bridge-%s", ctx->argv[2]);
1653 struct vsctl_port *port = shash_find_data(&ctx->ports, ctx->argv[1]);
1654 set_external_id(&port->port_cfg->external_ids, &new,
1655 key, ctx->argc >= 4 ? ctx->argv[3] : NULL);
1656 ovsrec_port_verify_external_ids(port->port_cfg);
1657 ovsrec_port_set_external_ids(port->port_cfg, &new);
1664 get_external_id(struct smap *smap, const char *prefix, const char *key,
1668 char *prefix_key = xasprintf("%s%s", prefix, key);
1669 const char *value = smap_get(smap, prefix_key);
1672 ds_put_format(output, "%s\n", value);
1676 const struct smap_node **sorted = smap_sort(smap);
1677 size_t prefix_len = strlen(prefix);
1680 for (i = 0; i < smap_count(smap); i++) {
1681 const struct smap_node *node = sorted[i];
1682 if (!strncmp(node->key, prefix, prefix_len)) {
1683 ds_put_format(output, "%s=%s\n", node->key + prefix_len,
1692 pre_cmd_br_get_external_id(struct vsctl_context *ctx)
1694 pre_cmd_br_set_external_id(ctx);
1698 cmd_br_get_external_id(struct vsctl_context *ctx)
1700 struct vsctl_bridge *bridge;
1702 vsctl_context_populate_cache(ctx);
1704 bridge = find_bridge(ctx, ctx->argv[1], true);
1705 if (bridge->br_cfg) {
1706 ovsrec_bridge_verify_external_ids(bridge->br_cfg);
1707 get_external_id(&bridge->br_cfg->external_ids, "",
1708 ctx->argc >= 3 ? ctx->argv[2] : NULL, &ctx->output);
1710 struct vsctl_port *port = shash_find_data(&ctx->ports, ctx->argv[1]);
1711 ovsrec_port_verify_external_ids(port->port_cfg);
1712 get_external_id(&port->port_cfg->external_ids, "fake-bridge-",
1713 ctx->argc >= 3 ? ctx->argv[2] : NULL, &ctx->output);
1718 cmd_list_ports(struct vsctl_context *ctx)
1720 struct vsctl_bridge *br;
1721 struct vsctl_port *port;
1724 vsctl_context_populate_cache(ctx);
1725 br = find_bridge(ctx, ctx->argv[1], true);
1726 ovsrec_bridge_verify_ports(br->br_cfg ? br->br_cfg : br->parent->br_cfg);
1729 LIST_FOR_EACH (port, ports_node, &br->ports) {
1730 if (strcmp(port->port_cfg->name, br->name)) {
1731 svec_add(&ports, port->port_cfg->name);
1734 output_sorted(&ports, &ctx->output);
1735 svec_destroy(&ports);
1739 add_port(struct vsctl_context *ctx,
1740 const char *br_name, const char *port_name,
1741 bool may_exist, bool fake_iface,
1742 char *iface_names[], int n_ifaces,
1743 char *settings[], int n_settings)
1745 struct vsctl_port *vsctl_port;
1746 struct vsctl_bridge *bridge;
1747 struct ovsrec_interface **ifaces;
1748 struct ovsrec_port *port;
1751 vsctl_context_populate_cache(ctx);
1753 struct vsctl_port *vsctl_port;
1755 vsctl_port = find_port(ctx, port_name, false);
1757 struct svec want_names, have_names;
1759 svec_init(&want_names);
1760 for (i = 0; i < n_ifaces; i++) {
1761 svec_add(&want_names, iface_names[i]);
1763 svec_sort(&want_names);
1765 svec_init(&have_names);
1766 for (i = 0; i < vsctl_port->port_cfg->n_interfaces; i++) {
1767 svec_add(&have_names,
1768 vsctl_port->port_cfg->interfaces[i]->name);
1770 svec_sort(&have_names);
1772 if (strcmp(vsctl_port->bridge->name, br_name)) {
1773 char *command = vsctl_context_to_string(ctx);
1774 vsctl_fatal("\"%s\" but %s is actually attached to bridge %s",
1775 command, port_name, vsctl_port->bridge->name);
1778 if (!svec_equal(&want_names, &have_names)) {
1779 char *have_names_string = svec_join(&have_names, ", ", "");
1780 char *command = vsctl_context_to_string(ctx);
1782 vsctl_fatal("\"%s\" but %s actually has interface(s) %s",
1783 command, port_name, have_names_string);
1786 svec_destroy(&want_names);
1787 svec_destroy(&have_names);
1792 check_conflicts(ctx, port_name,
1793 xasprintf("cannot create a port named %s", port_name));
1794 for (i = 0; i < n_ifaces; i++) {
1795 check_conflicts(ctx, iface_names[i],
1796 xasprintf("cannot create an interface named %s",
1799 bridge = find_bridge(ctx, br_name, true);
1801 ifaces = xmalloc(n_ifaces * sizeof *ifaces);
1802 for (i = 0; i < n_ifaces; i++) {
1803 ifaces[i] = ovsrec_interface_insert(ctx->txn);
1804 ovsrec_interface_set_name(ifaces[i], iface_names[i]);
1807 port = ovsrec_port_insert(ctx->txn);
1808 ovsrec_port_set_name(port, port_name);
1809 ovsrec_port_set_interfaces(port, ifaces, n_ifaces);
1810 ovsrec_port_set_bond_fake_iface(port, fake_iface);
1812 if (bridge->parent) {
1813 int64_t tag = bridge->vlan;
1814 ovsrec_port_set_tag(port, &tag, 1);
1817 for (i = 0; i < n_settings; i++) {
1818 set_column(get_table("Port"), &port->header_, settings[i],
1822 bridge_insert_port((bridge->parent ? bridge->parent->br_cfg
1823 : bridge->br_cfg), port);
1825 vsctl_port = add_port_to_cache(ctx, bridge, port);
1826 for (i = 0; i < n_ifaces; i++) {
1827 add_iface_to_cache(ctx, vsctl_port, ifaces[i]);
1833 cmd_add_port(struct vsctl_context *ctx)
1835 bool may_exist = shash_find(&ctx->options, "--may-exist") != NULL;
1837 add_port(ctx, ctx->argv[1], ctx->argv[2], may_exist, false,
1838 &ctx->argv[2], 1, &ctx->argv[3], ctx->argc - 3);
1842 cmd_add_bond(struct vsctl_context *ctx)
1844 bool may_exist = shash_find(&ctx->options, "--may-exist") != NULL;
1845 bool fake_iface = shash_find(&ctx->options, "--fake-iface");
1849 n_ifaces = ctx->argc - 3;
1850 for (i = 3; i < ctx->argc; i++) {
1851 if (strchr(ctx->argv[i], '=')) {
1857 vsctl_fatal("add-bond requires at least 2 interfaces, but only "
1858 "%d were specified", n_ifaces);
1861 add_port(ctx, ctx->argv[1], ctx->argv[2], may_exist, fake_iface,
1862 &ctx->argv[3], n_ifaces,
1863 &ctx->argv[n_ifaces + 3], ctx->argc - 3 - n_ifaces);
1867 cmd_del_port(struct vsctl_context *ctx)
1869 bool must_exist = !shash_find(&ctx->options, "--if-exists");
1870 bool with_iface = shash_find(&ctx->options, "--with-iface") != NULL;
1871 struct vsctl_port *port;
1873 vsctl_context_populate_cache(ctx);
1875 port = find_port(ctx, ctx->argv[ctx->argc - 1], must_exist);
1877 const char *target = ctx->argv[ctx->argc - 1];
1878 struct vsctl_iface *iface;
1880 port = find_port(ctx, target, false);
1882 iface = find_iface(ctx, target, false);
1887 if (must_exist && !port) {
1888 vsctl_fatal("no port or interface named %s", target);
1893 if (ctx->argc == 3) {
1894 struct vsctl_bridge *bridge;
1896 bridge = find_bridge(ctx, ctx->argv[1], true);
1897 if (port->bridge != bridge) {
1898 if (port->bridge->parent == bridge) {
1899 vsctl_fatal("bridge %s does not have a port %s (although "
1900 "its parent bridge %s does)",
1901 ctx->argv[1], ctx->argv[2],
1902 bridge->parent->name);
1904 vsctl_fatal("bridge %s does not have a port %s",
1905 ctx->argv[1], ctx->argv[2]);
1910 del_port(ctx, port);
1915 cmd_port_to_br(struct vsctl_context *ctx)
1917 struct vsctl_port *port;
1919 vsctl_context_populate_cache(ctx);
1921 port = find_port(ctx, ctx->argv[1], true);
1922 ds_put_format(&ctx->output, "%s\n", port->bridge->name);
1926 cmd_br_to_vlan(struct vsctl_context *ctx)
1928 struct vsctl_bridge *bridge;
1930 vsctl_context_populate_cache(ctx);
1932 bridge = find_bridge(ctx, ctx->argv[1], true);
1933 ds_put_format(&ctx->output, "%d\n", bridge->vlan);
1937 cmd_br_to_parent(struct vsctl_context *ctx)
1939 struct vsctl_bridge *bridge;
1941 vsctl_context_populate_cache(ctx);
1943 bridge = find_bridge(ctx, ctx->argv[1], true);
1944 if (bridge->parent) {
1945 bridge = bridge->parent;
1947 ds_put_format(&ctx->output, "%s\n", bridge->name);
1951 cmd_list_ifaces(struct vsctl_context *ctx)
1953 struct vsctl_bridge *br;
1954 struct vsctl_port *port;
1957 vsctl_context_populate_cache(ctx);
1959 br = find_bridge(ctx, ctx->argv[1], true);
1963 LIST_FOR_EACH (port, ports_node, &br->ports) {
1964 struct vsctl_iface *iface;
1966 LIST_FOR_EACH (iface, ifaces_node, &port->ifaces) {
1967 if (strcmp(iface->iface_cfg->name, br->name)) {
1968 svec_add(&ifaces, iface->iface_cfg->name);
1972 output_sorted(&ifaces, &ctx->output);
1973 svec_destroy(&ifaces);
1977 cmd_iface_to_br(struct vsctl_context *ctx)
1979 struct vsctl_iface *iface;
1981 vsctl_context_populate_cache(ctx);
1983 iface = find_iface(ctx, ctx->argv[1], true);
1984 ds_put_format(&ctx->output, "%s\n", iface->port->bridge->name);
1988 verify_controllers(struct ovsrec_bridge *bridge)
1992 ovsrec_bridge_verify_controller(bridge);
1993 for (i = 0; i < bridge->n_controller; i++) {
1994 ovsrec_controller_verify_target(bridge->controller[i]);
1999 pre_controller(struct vsctl_context *ctx)
2003 ovsdb_idl_add_column(ctx->idl, &ovsrec_controller_col_target);
2007 cmd_get_controller(struct vsctl_context *ctx)
2009 struct vsctl_bridge *br;
2010 struct svec targets;
2013 vsctl_context_populate_cache(ctx);
2015 br = find_bridge(ctx, ctx->argv[1], true);
2019 verify_controllers(br->br_cfg);
2021 /* Print the targets in sorted order for reproducibility. */
2022 svec_init(&targets);
2023 for (i = 0; i < br->br_cfg->n_controller; i++) {
2024 svec_add(&targets, br->br_cfg->controller[i]->target);
2027 svec_sort(&targets);
2028 for (i = 0; i < targets.n; i++) {
2029 ds_put_format(&ctx->output, "%s\n", targets.names[i]);
2031 svec_destroy(&targets);
2035 delete_controllers(struct ovsrec_controller **controllers,
2036 size_t n_controllers)
2040 for (i = 0; i < n_controllers; i++) {
2041 ovsrec_controller_delete(controllers[i]);
2046 cmd_del_controller(struct vsctl_context *ctx)
2048 struct ovsrec_bridge *br;
2050 vsctl_context_populate_cache(ctx);
2052 br = find_real_bridge(ctx, ctx->argv[1], true)->br_cfg;
2053 verify_controllers(br);
2055 if (br->controller) {
2056 delete_controllers(br->controller, br->n_controller);
2057 ovsrec_bridge_set_controller(br, NULL, 0);
2061 static struct ovsrec_controller **
2062 insert_controllers(struct ovsdb_idl_txn *txn, char *targets[], size_t n)
2064 struct ovsrec_controller **controllers;
2067 controllers = xmalloc(n * sizeof *controllers);
2068 for (i = 0; i < n; i++) {
2069 if (vconn_verify_name(targets[i]) && pvconn_verify_name(targets[i])) {
2070 VLOG_WARN("target type \"%s\" is possibly erroneous", targets[i]);
2072 controllers[i] = ovsrec_controller_insert(txn);
2073 ovsrec_controller_set_target(controllers[i], targets[i]);
2080 cmd_set_controller(struct vsctl_context *ctx)
2082 struct ovsrec_controller **controllers;
2083 struct ovsrec_bridge *br;
2086 vsctl_context_populate_cache(ctx);
2088 br = find_real_bridge(ctx, ctx->argv[1], true)->br_cfg;
2089 verify_controllers(br);
2091 delete_controllers(br->controller, br->n_controller);
2094 controllers = insert_controllers(ctx->txn, &ctx->argv[2], n);
2095 ovsrec_bridge_set_controller(br, controllers, n);
2100 cmd_get_fail_mode(struct vsctl_context *ctx)
2102 struct vsctl_bridge *br;
2103 const char *fail_mode;
2105 vsctl_context_populate_cache(ctx);
2106 br = find_bridge(ctx, ctx->argv[1], true);
2111 ovsrec_bridge_verify_fail_mode(br->br_cfg);
2113 fail_mode = br->br_cfg->fail_mode;
2114 if (fail_mode && strlen(fail_mode)) {
2115 ds_put_format(&ctx->output, "%s\n", fail_mode);
2120 cmd_del_fail_mode(struct vsctl_context *ctx)
2122 struct vsctl_bridge *br;
2124 vsctl_context_populate_cache(ctx);
2126 br = find_real_bridge(ctx, ctx->argv[1], true);
2128 ovsrec_bridge_set_fail_mode(br->br_cfg, NULL);
2132 cmd_set_fail_mode(struct vsctl_context *ctx)
2134 struct vsctl_bridge *br;
2135 const char *fail_mode = ctx->argv[2];
2137 vsctl_context_populate_cache(ctx);
2139 br = find_real_bridge(ctx, ctx->argv[1], true);
2141 if (strcmp(fail_mode, "standalone") && strcmp(fail_mode, "secure")) {
2142 vsctl_fatal("fail-mode must be \"standalone\" or \"secure\"");
2145 ovsrec_bridge_set_fail_mode(br->br_cfg, fail_mode);
2149 verify_managers(const struct ovsrec_open_vswitch *ovs)
2153 ovsrec_open_vswitch_verify_manager_options(ovs);
2155 for (i = 0; i < ovs->n_manager_options; ++i) {
2156 const struct ovsrec_manager *mgr = ovs->manager_options[i];
2158 ovsrec_manager_verify_target(mgr);
2163 pre_manager(struct vsctl_context *ctx)
2165 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_manager_options);
2166 ovsdb_idl_add_column(ctx->idl, &ovsrec_manager_col_target);
2170 cmd_get_manager(struct vsctl_context *ctx)
2172 const struct ovsrec_open_vswitch *ovs = ctx->ovs;
2173 struct svec targets;
2176 verify_managers(ovs);
2178 /* Print the targets in sorted order for reproducibility. */
2179 svec_init(&targets);
2181 for (i = 0; i < ovs->n_manager_options; i++) {
2182 svec_add(&targets, ovs->manager_options[i]->target);
2185 svec_sort_unique(&targets);
2186 for (i = 0; i < targets.n; i++) {
2187 ds_put_format(&ctx->output, "%s\n", targets.names[i]);
2189 svec_destroy(&targets);
2193 delete_managers(const struct vsctl_context *ctx)
2195 const struct ovsrec_open_vswitch *ovs = ctx->ovs;
2198 /* Delete Manager rows pointed to by 'manager_options' column. */
2199 for (i = 0; i < ovs->n_manager_options; i++) {
2200 ovsrec_manager_delete(ovs->manager_options[i]);
2203 /* Delete 'Manager' row refs in 'manager_options' column. */
2204 ovsrec_open_vswitch_set_manager_options(ovs, NULL, 0);
2208 cmd_del_manager(struct vsctl_context *ctx)
2210 const struct ovsrec_open_vswitch *ovs = ctx->ovs;
2212 verify_managers(ovs);
2213 delete_managers(ctx);
2217 insert_managers(struct vsctl_context *ctx, char *targets[], size_t n)
2219 struct ovsrec_manager **managers;
2222 /* Insert each manager in a new row in Manager table. */
2223 managers = xmalloc(n * sizeof *managers);
2224 for (i = 0; i < n; i++) {
2225 if (stream_verify_name(targets[i]) && pstream_verify_name(targets[i])) {
2226 VLOG_WARN("target type \"%s\" is possibly erroneous", targets[i]);
2228 managers[i] = ovsrec_manager_insert(ctx->txn);
2229 ovsrec_manager_set_target(managers[i], targets[i]);
2232 /* Store uuids of new Manager rows in 'manager_options' column. */
2233 ovsrec_open_vswitch_set_manager_options(ctx->ovs, managers, n);
2238 cmd_set_manager(struct vsctl_context *ctx)
2240 const size_t n = ctx->argc - 1;
2242 verify_managers(ctx->ovs);
2243 delete_managers(ctx);
2244 insert_managers(ctx, &ctx->argv[1], n);
2248 pre_cmd_get_ssl(struct vsctl_context *ctx)
2250 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_ssl);
2252 ovsdb_idl_add_column(ctx->idl, &ovsrec_ssl_col_private_key);
2253 ovsdb_idl_add_column(ctx->idl, &ovsrec_ssl_col_certificate);
2254 ovsdb_idl_add_column(ctx->idl, &ovsrec_ssl_col_ca_cert);
2255 ovsdb_idl_add_column(ctx->idl, &ovsrec_ssl_col_bootstrap_ca_cert);
2259 cmd_get_ssl(struct vsctl_context *ctx)
2261 struct ovsrec_ssl *ssl = ctx->ovs->ssl;
2263 ovsrec_open_vswitch_verify_ssl(ctx->ovs);
2265 ovsrec_ssl_verify_private_key(ssl);
2266 ovsrec_ssl_verify_certificate(ssl);
2267 ovsrec_ssl_verify_ca_cert(ssl);
2268 ovsrec_ssl_verify_bootstrap_ca_cert(ssl);
2270 ds_put_format(&ctx->output, "Private key: %s\n", ssl->private_key);
2271 ds_put_format(&ctx->output, "Certificate: %s\n", ssl->certificate);
2272 ds_put_format(&ctx->output, "CA Certificate: %s\n", ssl->ca_cert);
2273 ds_put_format(&ctx->output, "Bootstrap: %s\n",
2274 ssl->bootstrap_ca_cert ? "true" : "false");
2279 pre_cmd_del_ssl(struct vsctl_context *ctx)
2281 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_ssl);
2285 cmd_del_ssl(struct vsctl_context *ctx)
2287 struct ovsrec_ssl *ssl = ctx->ovs->ssl;
2290 ovsrec_open_vswitch_verify_ssl(ctx->ovs);
2291 ovsrec_ssl_delete(ssl);
2292 ovsrec_open_vswitch_set_ssl(ctx->ovs, NULL);
2297 pre_cmd_set_ssl(struct vsctl_context *ctx)
2299 ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_ssl);
2303 cmd_set_ssl(struct vsctl_context *ctx)
2305 bool bootstrap = shash_find(&ctx->options, "--bootstrap");
2306 struct ovsrec_ssl *ssl = ctx->ovs->ssl;
2308 ovsrec_open_vswitch_verify_ssl(ctx->ovs);
2310 ovsrec_ssl_delete(ssl);
2312 ssl = ovsrec_ssl_insert(ctx->txn);
2314 ovsrec_ssl_set_private_key(ssl, ctx->argv[1]);
2315 ovsrec_ssl_set_certificate(ssl, ctx->argv[2]);
2316 ovsrec_ssl_set_ca_cert(ssl, ctx->argv[3]);
2318 ovsrec_ssl_set_bootstrap_ca_cert(ssl, bootstrap);
2320 ovsrec_open_vswitch_set_ssl(ctx->ovs, ssl);
2323 /* Parameter commands. */
2325 struct vsctl_row_id {
2326 const struct ovsdb_idl_table_class *table;
2327 const struct ovsdb_idl_column *name_column;
2328 const struct ovsdb_idl_column *uuid_column;
2331 struct vsctl_table_class {
2332 struct ovsdb_idl_table_class *class;
2333 struct vsctl_row_id row_ids[2];
2336 static const struct vsctl_table_class tables[] = {
2337 {&ovsrec_table_bridge,
2338 {{&ovsrec_table_bridge, &ovsrec_bridge_col_name, NULL},
2339 {NULL, NULL, NULL}}},
2341 {&ovsrec_table_controller,
2342 {{&ovsrec_table_bridge,
2343 &ovsrec_bridge_col_name,
2344 &ovsrec_bridge_col_controller}}},
2346 {&ovsrec_table_interface,
2347 {{&ovsrec_table_interface, &ovsrec_interface_col_name, NULL},
2348 {NULL, NULL, NULL}}},
2350 {&ovsrec_table_mirror,
2351 {{&ovsrec_table_mirror, &ovsrec_mirror_col_name, NULL},
2352 {NULL, NULL, NULL}}},
2354 {&ovsrec_table_manager,
2355 {{&ovsrec_table_manager, &ovsrec_manager_col_target, NULL},
2356 {NULL, NULL, NULL}}},
2358 {&ovsrec_table_netflow,
2359 {{&ovsrec_table_bridge,
2360 &ovsrec_bridge_col_name,
2361 &ovsrec_bridge_col_netflow},
2362 {NULL, NULL, NULL}}},
2364 {&ovsrec_table_open_vswitch,
2365 {{&ovsrec_table_open_vswitch, NULL, NULL},
2366 {NULL, NULL, NULL}}},
2368 {&ovsrec_table_port,
2369 {{&ovsrec_table_port, &ovsrec_port_col_name, NULL},
2370 {NULL, NULL, NULL}}},
2373 {{&ovsrec_table_port, &ovsrec_port_col_name, &ovsrec_port_col_qos},
2374 {NULL, NULL, NULL}}},
2376 {&ovsrec_table_queue,
2377 {{NULL, NULL, NULL},
2378 {NULL, NULL, NULL}}},
2381 {{&ovsrec_table_open_vswitch, NULL, &ovsrec_open_vswitch_col_ssl}}},
2383 {&ovsrec_table_sflow,
2384 {{&ovsrec_table_bridge,
2385 &ovsrec_bridge_col_name,
2386 &ovsrec_bridge_col_sflow},
2387 {NULL, NULL, NULL}}},
2389 {&ovsrec_table_flow_table,
2390 {{&ovsrec_table_flow_table, &ovsrec_flow_table_col_name, NULL},
2391 {NULL, NULL, NULL}}},
2393 {NULL, {{NULL, NULL, NULL}, {NULL, NULL, NULL}}}
2397 die_if_error(char *error)
2400 vsctl_fatal("%s", error);
2405 to_lower_and_underscores(unsigned c)
2407 return c == '-' ? '_' : tolower(c);
2411 score_partial_match(const char *name, const char *s)
2415 if (!strcmp(name, s)) {
2418 for (score = 0; ; score++, name++, s++) {
2419 if (to_lower_and_underscores(*name) != to_lower_and_underscores(*s)) {
2421 } else if (*name == '\0') {
2422 return UINT_MAX - 1;
2425 return *s == '\0' ? score : 0;
2428 static const struct vsctl_table_class *
2429 get_table(const char *table_name)
2431 const struct vsctl_table_class *table;
2432 const struct vsctl_table_class *best_match = NULL;
2433 unsigned int best_score = 0;
2435 for (table = tables; table->class; table++) {
2436 unsigned int score = score_partial_match(table->class->name,
2438 if (score > best_score) {
2441 } else if (score == best_score) {
2447 } else if (best_score) {
2448 vsctl_fatal("multiple table names match \"%s\"", table_name);
2450 vsctl_fatal("unknown table \"%s\"", table_name);
2454 static const struct vsctl_table_class *
2455 pre_get_table(struct vsctl_context *ctx, const char *table_name)
2457 const struct vsctl_table_class *table_class;
2460 table_class = get_table(table_name);
2461 ovsdb_idl_add_table(ctx->idl, table_class->class);
2463 for (i = 0; i < ARRAY_SIZE(table_class->row_ids); i++) {
2464 const struct vsctl_row_id *id = &table_class->row_ids[i];
2466 ovsdb_idl_add_table(ctx->idl, id->table);
2468 if (id->name_column) {
2469 ovsdb_idl_add_column(ctx->idl, id->name_column);
2471 if (id->uuid_column) {
2472 ovsdb_idl_add_column(ctx->idl, id->uuid_column);
2479 static const struct ovsdb_idl_row *
2480 get_row_by_id(struct vsctl_context *ctx, const struct vsctl_table_class *table,
2481 const struct vsctl_row_id *id, const char *record_id)
2483 const struct ovsdb_idl_row *referrer, *final;
2489 if (!id->name_column) {
2490 if (strcmp(record_id, ".")) {
2493 referrer = ovsdb_idl_first_row(ctx->idl, id->table);
2494 if (!referrer || ovsdb_idl_next_row(referrer)) {
2498 const struct ovsdb_idl_row *row;
2501 for (row = ovsdb_idl_first_row(ctx->idl, id->table);
2503 row = ovsdb_idl_next_row(row))
2505 const struct ovsdb_datum *name;
2507 name = ovsdb_idl_get(row, id->name_column,
2508 OVSDB_TYPE_STRING, OVSDB_TYPE_VOID);
2509 if (name->n == 1 && !strcmp(name->keys[0].string, record_id)) {
2511 vsctl_fatal("multiple rows in %s match \"%s\"",
2512 table->class->name, record_id);
2523 if (id->uuid_column) {
2524 const struct ovsdb_datum *uuid;
2526 ovsdb_idl_txn_verify(referrer, id->uuid_column);
2527 uuid = ovsdb_idl_get(referrer, id->uuid_column,
2528 OVSDB_TYPE_UUID, OVSDB_TYPE_VOID);
2530 final = ovsdb_idl_get_row_for_uuid(ctx->idl, table->class,
2531 &uuid->keys[0].uuid);
2540 static const struct ovsdb_idl_row *
2541 get_row (struct vsctl_context *ctx,
2542 const struct vsctl_table_class *table, const char *record_id)
2544 const struct ovsdb_idl_row *row;
2547 if (uuid_from_string(&uuid, record_id)) {
2548 row = ovsdb_idl_get_row_for_uuid(ctx->idl, table->class, &uuid);
2552 for (i = 0; i < ARRAY_SIZE(table->row_ids); i++) {
2553 row = get_row_by_id(ctx, table, &table->row_ids[i], record_id);
2562 static const struct ovsdb_idl_row *
2563 must_get_row(struct vsctl_context *ctx,
2564 const struct vsctl_table_class *table, const char *record_id)
2566 const struct ovsdb_idl_row *row = get_row(ctx, table, record_id);
2568 vsctl_fatal("no row \"%s\" in table %s",
2569 record_id, table->class->name);
2575 get_column(const struct vsctl_table_class *table, const char *column_name,
2576 const struct ovsdb_idl_column **columnp)
2578 const struct ovsdb_idl_column *best_match = NULL;
2579 unsigned int best_score = 0;
2582 for (i = 0; i < table->class->n_columns; i++) {
2583 const struct ovsdb_idl_column *column = &table->class->columns[i];
2584 unsigned int score = score_partial_match(column->name, column_name);
2585 if (score > best_score) {
2586 best_match = column;
2588 } else if (score == best_score) {
2593 *columnp = best_match;
2596 } else if (best_score) {
2597 return xasprintf("%s contains more than one column whose name "
2598 "matches \"%s\"", table->class->name, column_name);
2600 return xasprintf("%s does not contain a column whose name matches "
2601 "\"%s\"", table->class->name, column_name);
2605 static struct ovsdb_symbol *
2606 create_symbol(struct ovsdb_symbol_table *symtab, const char *id, bool *newp)
2608 struct ovsdb_symbol *symbol;
2611 vsctl_fatal("row id \"%s\" does not begin with \"@\"", id);
2615 *newp = ovsdb_symbol_table_get(symtab, id) == NULL;
2618 symbol = ovsdb_symbol_table_insert(symtab, id);
2619 if (symbol->created) {
2620 vsctl_fatal("row id \"%s\" may only be specified on one --id option",
2623 symbol->created = true;
2628 pre_get_column(struct vsctl_context *ctx,
2629 const struct vsctl_table_class *table, const char *column_name,
2630 const struct ovsdb_idl_column **columnp)
2632 die_if_error(get_column(table, column_name, columnp));
2633 ovsdb_idl_add_column(ctx->idl, *columnp);
2637 missing_operator_error(const char *arg, const char **allowed_operators,
2643 ds_put_format(&s, "%s: argument does not end in ", arg);
2644 ds_put_format(&s, "\"%s\"", allowed_operators[0]);
2645 if (n_allowed == 2) {
2646 ds_put_format(&s, " or \"%s\"", allowed_operators[1]);
2647 } else if (n_allowed > 2) {
2650 for (i = 1; i < n_allowed - 1; i++) {
2651 ds_put_format(&s, ", \"%s\"", allowed_operators[i]);
2653 ds_put_format(&s, ", or \"%s\"", allowed_operators[i]);
2655 ds_put_format(&s, " followed by a value.");
2657 return ds_steal_cstr(&s);
2660 /* Breaks 'arg' apart into a number of fields in the following order:
2662 * - The name of a column in 'table', stored into '*columnp'. The column
2663 * name may be abbreviated.
2665 * - Optionally ':' followed by a key string. The key is stored as a
2666 * malloc()'d string into '*keyp', or NULL if no key is present in
2669 * - If 'valuep' is nonnull, an operator followed by a value string. The
2670 * allowed operators are the 'n_allowed' string in 'allowed_operators',
2671 * or just "=" if 'n_allowed' is 0. If 'operatorp' is nonnull, then the
2672 * index of the operator within 'allowed_operators' is stored into
2673 * '*operatorp'. The value is stored as a malloc()'d string into
2674 * '*valuep', or NULL if no value is present in 'arg'.
2676 * On success, returns NULL. On failure, returned a malloc()'d string error
2677 * message and stores NULL into all of the nonnull output arguments. */
2678 static char * WARN_UNUSED_RESULT
2679 parse_column_key_value(const char *arg,
2680 const struct vsctl_table_class *table,
2681 const struct ovsdb_idl_column **columnp, char **keyp,
2683 const char **allowed_operators, size_t n_allowed,
2686 const char *p = arg;
2690 assert(!(operatorp && !valuep));
2696 /* Parse column name. */
2697 error = ovsdb_token_parse(&p, &column_name);
2701 if (column_name[0] == '\0') {
2703 error = xasprintf("%s: missing column name", arg);
2706 error = get_column(table, column_name, columnp);
2712 /* Parse key string. */
2715 error = ovsdb_token_parse(&p, keyp);
2721 /* Parse value string. */
2727 if (!allowed_operators) {
2728 static const char *equals = "=";
2729 allowed_operators = =
2735 for (i = 0; i < n_allowed; i++) {
2736 const char *op = allowed_operators[i];
2737 size_t op_len = strlen(op);
2739 if (op_len > best_len && !strncmp(op, p, op_len) && p[op_len]) {
2745 error = missing_operator_error(arg, allowed_operators, n_allowed);
2752 *valuep = xstrdup(p + best_len);
2755 error = xasprintf("%s: trailing garbage \"%s\" in argument",
2776 static const struct ovsdb_idl_column *
2777 pre_parse_column_key_value(struct vsctl_context *ctx,
2779 const struct vsctl_table_class *table)
2781 const struct ovsdb_idl_column *column;
2786 die_if_error(ovsdb_token_parse(&p, &column_name));
2787 if (column_name[0] == '\0') {
2788 vsctl_fatal("%s: missing column name", arg);
2791 pre_get_column(ctx, table, column_name, &column);
2798 check_mutable(const struct vsctl_table_class *table,
2799 const struct ovsdb_idl_column *column)
2801 if (!column->mutable) {
2802 vsctl_fatal("cannot modify read-only column %s in table %s",
2803 column->name, table->class->name);
2808 pre_cmd_get(struct vsctl_context *ctx)
2810 const char *id = shash_find_data(&ctx->options, "--id");
2811 const char *table_name = ctx->argv[1];
2812 const struct vsctl_table_class *table;
2815 /* Using "get" without --id or a column name could possibly make sense.
2816 * Maybe, for example, a ovs-vsctl run wants to assert that a row exists.
2817 * But it is unlikely that an interactive user would want to do that, so
2818 * issue a warning if we're running on a terminal. */
2819 if (!id && ctx->argc <= 3 && isatty(STDOUT_FILENO)) {
2820 VLOG_WARN("\"get\" command without row arguments or \"--id\" is "
2821 "possibly erroneous");
2824 table = pre_get_table(ctx, table_name);
2825 for (i = 3; i < ctx->argc; i++) {
2826 if (!strcasecmp(ctx->argv[i], "_uuid")
2827 || !strcasecmp(ctx->argv[i], "-uuid")) {
2831 pre_parse_column_key_value(ctx, ctx->argv[i], table);
2836 cmd_get(struct vsctl_context *ctx)
2838 const char *id = shash_find_data(&ctx->options, "--id");
2839 bool if_exists = shash_find(&ctx->options, "--if-exists");
2840 const char *table_name = ctx->argv[1];
2841 const char *record_id = ctx->argv[2];
2842 const struct vsctl_table_class *table;
2843 const struct ovsdb_idl_row *row;
2844 struct ds *out = &ctx->output;
2847 table = get_table(table_name);
2848 row = must_get_row(ctx, table, record_id);
2850 struct ovsdb_symbol *symbol;
2853 symbol = create_symbol(ctx->symtab, id, &new);
2855 vsctl_fatal("row id \"%s\" specified on \"get\" command was used "
2856 "before it was defined", id);
2858 symbol->uuid = row->uuid;
2860 /* This symbol refers to a row that already exists, so disable warnings
2861 * about it being unreferenced. */
2862 symbol->strong_ref = true;
2864 for (i = 3; i < ctx->argc; i++) {
2865 const struct ovsdb_idl_column *column;
2866 const struct ovsdb_datum *datum;
2869 /* Special case for obtaining the UUID of a row. We can't just do this
2870 * through parse_column_key_value() below since it returns a "struct
2871 * ovsdb_idl_column" and the UUID column doesn't have one. */
2872 if (!strcasecmp(ctx->argv[i], "_uuid")
2873 || !strcasecmp(ctx->argv[i], "-uuid")) {
2874 ds_put_format(out, UUID_FMT"\n", UUID_ARGS(&row->uuid));
2878 die_if_error(parse_column_key_value(ctx->argv[i], table,
2879 &column, &key_string,
2880 NULL, NULL, 0, NULL));
2882 ovsdb_idl_txn_verify(row, column);
2883 datum = ovsdb_idl_read(row, column);
2885 union ovsdb_atom key;
2888 if (column->type.value.type == OVSDB_TYPE_VOID) {
2889 vsctl_fatal("cannot specify key to get for non-map column %s",
2893 die_if_error(ovsdb_atom_from_string(&key,
2895 key_string, ctx->symtab));
2897 idx = ovsdb_datum_find_key(datum, &key,
2898 column->type.key.type);
2899 if (idx == UINT_MAX) {
2901 vsctl_fatal("no key \"%s\" in %s record \"%s\" column %s",
2902 key_string, table->class->name, record_id,
2906 ovsdb_atom_to_string(&datum->values[idx],
2907 column->type.value.type, out);
2909 ovsdb_atom_destroy(&key, column->type.key.type);
2911 ovsdb_datum_to_string(datum, &column->type, out);
2913 ds_put_char(out, '\n');
2920 parse_column_names(const char *column_names,
2921 const struct vsctl_table_class *table,
2922 const struct ovsdb_idl_column ***columnsp,
2925 const struct ovsdb_idl_column **columns;
2928 if (!column_names) {
2931 n_columns = table->class->n_columns + 1;
2932 columns = xmalloc(n_columns * sizeof *columns);
2934 for (i = 0; i < table->class->n_columns; i++) {
2935 columns[i + 1] = &table->class->columns[i];
2938 char *s = xstrdup(column_names);
2939 size_t allocated_columns;
2940 char *save_ptr = NULL;
2944 allocated_columns = n_columns = 0;
2945 for (column_name = strtok_r(s, ", ", &save_ptr); column_name;
2946 column_name = strtok_r(NULL, ", ", &save_ptr)) {
2947 const struct ovsdb_idl_column *column;
2949 if (!strcasecmp(column_name, "_uuid")) {
2952 die_if_error(get_column(table, column_name, &column));
2954 if (n_columns >= allocated_columns) {
2955 columns = x2nrealloc(columns, &allocated_columns,
2958 columns[n_columns++] = column;
2963 vsctl_fatal("must specify at least one column name");
2966 *columnsp = columns;
2967 *n_columnsp = n_columns;
2972 pre_list_columns(struct vsctl_context *ctx,
2973 const struct vsctl_table_class *table,
2974 const char *column_names)
2976 const struct ovsdb_idl_column **columns;
2980 parse_column_names(column_names, table, &columns, &n_columns);
2981 for (i = 0; i < n_columns; i++) {
2983 ovsdb_idl_add_column(ctx->idl, columns[i]);
2990 pre_cmd_list(struct vsctl_context *ctx)
2992 const char *column_names = shash_find_data(&ctx->options, "--columns");
2993 const char *table_name = ctx->argv[1];
2994 const struct vsctl_table_class *table;
2996 table = pre_get_table(ctx, table_name);
2997 pre_list_columns(ctx, table, column_names);
3000 static struct table *
3001 list_make_table(const struct ovsdb_idl_column **columns, size_t n_columns)
3006 out = xmalloc(sizeof *out);
3009 for (i = 0; i < n_columns; i++) {
3010 const struct ovsdb_idl_column *column = columns[i];
3011 const char *column_name = column ? column->name : "_uuid";
3013 table_add_column(out, "%s", column_name);
3020 list_record(const struct ovsdb_idl_row *row,
3021 const struct ovsdb_idl_column **columns, size_t n_columns,
3027 for (i = 0; i < n_columns; i++) {
3028 const struct ovsdb_idl_column *column = columns[i];
3029 struct cell *cell = table_add_cell(out);
3032 struct ovsdb_datum datum;
3033 union ovsdb_atom atom;
3035 atom.uuid = row->uuid;
3038 datum.values = NULL;
3041 cell->json = ovsdb_datum_to_json(&datum, &ovsdb_type_uuid);
3042 cell->type = &ovsdb_type_uuid;
3044 const struct ovsdb_datum *datum = ovsdb_idl_read(row, column);
3046 cell->json = ovsdb_datum_to_json(datum, &column->type);
3047 cell->type = &column->type;
3053 cmd_list(struct vsctl_context *ctx)
3055 const char *column_names = shash_find_data(&ctx->options, "--columns");
3056 const struct ovsdb_idl_column **columns;
3057 const char *table_name = ctx->argv[1];
3058 const struct vsctl_table_class *table;
3063 table = get_table(table_name);
3064 parse_column_names(column_names, table, &columns, &n_columns);
3065 out = ctx->table = list_make_table(columns, n_columns);
3066 if (ctx->argc > 2) {
3067 for (i = 2; i < ctx->argc; i++) {
3068 list_record(must_get_row(ctx, table, ctx->argv[i]),
3069 columns, n_columns, out);
3072 const struct ovsdb_idl_row *row;
3074 for (row = ovsdb_idl_first_row(ctx->idl, table->class); row != NULL;
3075 row = ovsdb_idl_next_row(row)) {
3076 list_record(row, columns, n_columns, out);
3083 pre_cmd_find(struct vsctl_context *ctx)
3085 const char *column_names = shash_find_data(&ctx->options, "--columns");
3086 const char *table_name = ctx->argv[1];
3087 const struct vsctl_table_class *table;
3090 table = pre_get_table(ctx, table_name);
3091 pre_list_columns(ctx, table, column_names);
3092 for (i = 2; i < ctx->argc; i++) {
3093 pre_parse_column_key_value(ctx, ctx->argv[i], table);
3098 cmd_find(struct vsctl_context *ctx)
3100 const char *column_names = shash_find_data(&ctx->options, "--columns");
3101 const struct ovsdb_idl_column **columns;
3102 const char *table_name = ctx->argv[1];
3103 const struct vsctl_table_class *table;
3104 const struct ovsdb_idl_row *row;
3108 table = get_table(table_name);
3109 parse_column_names(column_names, table, &columns, &n_columns);
3110 out = ctx->table = list_make_table(columns, n_columns);
3111 for (row = ovsdb_idl_first_row(ctx->idl, table->class); row;
3112 row = ovsdb_idl_next_row(row)) {
3115 for (i = 2; i < ctx->argc; i++) {
3116 if (!is_condition_satisfied(table, row, ctx->argv[i],
3121 list_record(row, columns, n_columns, out);
3129 pre_cmd_set(struct vsctl_context *ctx)
3131 const char *table_name = ctx->argv[1];
3132 const struct vsctl_table_class *table;
3135 table = pre_get_table(ctx, table_name);
3136 for (i = 3; i < ctx->argc; i++) {
3137 const struct ovsdb_idl_column *column;
3139 column = pre_parse_column_key_value(ctx, ctx->argv[i], table);
3140 check_mutable(table, column);
3145 set_column(const struct vsctl_table_class *table,
3146 const struct ovsdb_idl_row *row, const char *arg,
3147 struct ovsdb_symbol_table *symtab)
3149 const struct ovsdb_idl_column *column;
3150 char *key_string, *value_string;
3153 error = parse_column_key_value(arg, table, &column, &key_string,
3154 NULL, NULL, 0, &value_string);
3155 die_if_error(error);
3156 if (!value_string) {
3157 vsctl_fatal("%s: missing value", arg);
3161 union ovsdb_atom key, value;
3162 struct ovsdb_datum datum;
3164 if (column->type.value.type == OVSDB_TYPE_VOID) {
3165 vsctl_fatal("cannot specify key to set for non-map column %s",
3169 die_if_error(ovsdb_atom_from_string(&key, &column->type.key,
3170 key_string, symtab));
3171 die_if_error(ovsdb_atom_from_string(&value, &column->type.value,
3172 value_string, symtab));
3174 ovsdb_datum_init_empty(&datum);
3175 ovsdb_datum_add_unsafe(&datum, &key, &value, &column->type);
3177 ovsdb_atom_destroy(&key, column->type.key.type);
3178 ovsdb_atom_destroy(&value, column->type.value.type);
3180 ovsdb_datum_union(&datum, ovsdb_idl_read(row, column),
3181 &column->type, false);
3182 ovsdb_idl_txn_write(row, column, &datum);
3184 struct ovsdb_datum datum;
3186 die_if_error(ovsdb_datum_from_string(&datum, &column->type,
3187 value_string, symtab));
3188 ovsdb_idl_txn_write(row, column, &datum);
3196 cmd_set(struct vsctl_context *ctx)
3198 const char *table_name = ctx->argv[1];
3199 const char *record_id = ctx->argv[2];
3200 const struct vsctl_table_class *table;
3201 const struct ovsdb_idl_row *row;
3204 table = get_table(table_name);
3205 row = must_get_row(ctx, table, record_id);
3206 for (i = 3; i < ctx->argc; i++) {
3207 set_column(table, row, ctx->argv[i], ctx->symtab);
3210 vsctl_context_invalidate_cache(ctx);
3214 pre_cmd_add(struct vsctl_context *ctx)
3216 const char *table_name = ctx->argv[1];
3217 const char *column_name = ctx->argv[3];
3218 const struct vsctl_table_class *table;
3219 const struct ovsdb_idl_column *column;
3221 table = pre_get_table(ctx, table_name);
3222 pre_get_column(ctx, table, column_name, &column);
3223 check_mutable(table, column);
3227 cmd_add(struct vsctl_context *ctx)
3229 const char *table_name = ctx->argv[1];
3230 const char *record_id = ctx->argv[2];
3231 const char *column_name = ctx->argv[3];
3232 const struct vsctl_table_class *table;
3233 const struct ovsdb_idl_column *column;
3234 const struct ovsdb_idl_row *row;
3235 const struct ovsdb_type *type;
3236 struct ovsdb_datum old;
3239 table = get_table(table_name);
3240 row = must_get_row(ctx, table, record_id);
3241 die_if_error(get_column(table, column_name, &column));
3243 type = &column->type;
3244 ovsdb_datum_clone(&old, ovsdb_idl_read(row, column), &column->type);
3245 for (i = 4; i < ctx->argc; i++) {
3246 struct ovsdb_type add_type;
3247 struct ovsdb_datum add;
3251 add_type.n_max = UINT_MAX;
3252 die_if_error(ovsdb_datum_from_string(&add, &add_type, ctx->argv[i],
3254 ovsdb_datum_union(&old, &add, type, false);
3255 ovsdb_datum_destroy(&add, type);
3257 if (old.n > type->n_max) {
3258 vsctl_fatal("\"add\" operation would put %u %s in column %s of "
3259 "table %s but the maximum number is %u",
3261 type->value.type == OVSDB_TYPE_VOID ? "values" : "pairs",
3262 column->name, table->class->name, type->n_max);
3264 ovsdb_idl_txn_verify(row, column);
3265 ovsdb_idl_txn_write(row, column, &old);
3267 vsctl_context_invalidate_cache(ctx);
3271 pre_cmd_remove(struct vsctl_context *ctx)
3273 const char *table_name = ctx->argv[1];
3274 const char *column_name = ctx->argv[3];
3275 const struct vsctl_table_class *table;
3276 const struct ovsdb_idl_column *column;
3278 table = pre_get_table(ctx, table_name);
3279 pre_get_column(ctx, table, column_name, &column);
3280 check_mutable(table, column);
3284 cmd_remove(struct vsctl_context *ctx)
3286 const char *table_name = ctx->argv[1];
3287 const char *record_id = ctx->argv[2];
3288 const char *column_name = ctx->argv[3];
3289 const struct vsctl_table_class *table;
3290 const struct ovsdb_idl_column *column;
3291 const struct ovsdb_idl_row *row;
3292 const struct ovsdb_type *type;
3293 struct ovsdb_datum old;
3296 table = get_table(table_name);
3297 row = must_get_row(ctx, table, record_id);
3298 die_if_error(get_column(table, column_name, &column));
3300 type = &column->type;
3301 ovsdb_datum_clone(&old, ovsdb_idl_read(row, column), &column->type);
3302 for (i = 4; i < ctx->argc; i++) {
3303 struct ovsdb_type rm_type;
3304 struct ovsdb_datum rm;
3309 rm_type.n_max = UINT_MAX;
3310 error = ovsdb_datum_from_string(&rm, &rm_type,
3311 ctx->argv[i], ctx->symtab);
3312 if (error && ovsdb_type_is_map(&rm_type)) {
3314 rm_type.value.type = OVSDB_TYPE_VOID;
3315 die_if_error(ovsdb_datum_from_string(&rm, &rm_type,
3316 ctx->argv[i], ctx->symtab));
3318 ovsdb_datum_subtract(&old, type, &rm, &rm_type);
3319 ovsdb_datum_destroy(&rm, &rm_type);
3321 if (old.n < type->n_min) {
3322 vsctl_fatal("\"remove\" operation would put %u %s in column %s of "
3323 "table %s but the minimum number is %u",
3325 type->value.type == OVSDB_TYPE_VOID ? "values" : "pairs",
3326 column->name, table->class->name, type->n_min);
3328 ovsdb_idl_txn_verify(row, column);
3329 ovsdb_idl_txn_write(row, column, &old);
3331 vsctl_context_invalidate_cache(ctx);
3335 pre_cmd_clear(struct vsctl_context *ctx)
3337 const char *table_name = ctx->argv[1];
3338 const struct vsctl_table_class *table;
3341 table = pre_get_table(ctx, table_name);
3342 for (i = 3; i < ctx->argc; i++) {
3343 const struct ovsdb_idl_column *column;
3345 pre_get_column(ctx, table, ctx->argv[i], &column);
3346 check_mutable(table, column);
3351 cmd_clear(struct vsctl_context *ctx)
3353 const char *table_name = ctx->argv[1];
3354 const char *record_id = ctx->argv[2];
3355 const struct vsctl_table_class *table;
3356 const struct ovsdb_idl_row *row;
3359 table = get_table(table_name);
3360 row = must_get_row(ctx, table, record_id);
3361 for (i = 3; i < ctx->argc; i++) {
3362 const struct ovsdb_idl_column *column;
3363 const struct ovsdb_type *type;
3364 struct ovsdb_datum datum;
3366 die_if_error(get_column(table, ctx->argv[i], &column));
3368 type = &column->type;
3369 if (type->n_min > 0) {
3370 vsctl_fatal("\"clear\" operation cannot be applied to column %s "
3371 "of table %s, which is not allowed to be empty",
3372 column->name, table->class->name);
3375 ovsdb_datum_init_empty(&datum);
3376 ovsdb_idl_txn_write(row, column, &datum);
3379 vsctl_context_invalidate_cache(ctx);
3383 pre_create(struct vsctl_context *ctx)
3385 const char *id = shash_find_data(&ctx->options, "--id");
3386 const char *table_name = ctx->argv[1];
3387 const struct vsctl_table_class *table;
3389 table = get_table(table_name);
3390 if (!id && !table->class->is_root) {
3391 VLOG_WARN("applying \"create\" command to table %s without --id "
3392 "option will have no effect", table->class->name);
3397 cmd_create(struct vsctl_context *ctx)
3399 const char *id = shash_find_data(&ctx->options, "--id");
3400 const char *table_name = ctx->argv[1];
3401 const struct vsctl_table_class *table = get_table(table_name);
3402 const struct ovsdb_idl_row *row;
3403 const struct uuid *uuid;
3407 struct ovsdb_symbol *symbol = create_symbol(ctx->symtab, id, NULL);
3408 if (table->class->is_root) {
3409 /* This table is in the root set, meaning that rows created in it
3410 * won't disappear even if they are unreferenced, so disable
3411 * warnings about that by pretending that there is a reference. */
3412 symbol->strong_ref = true;
3414 uuid = &symbol->uuid;
3419 row = ovsdb_idl_txn_insert(ctx->txn, table->class, uuid);
3420 for (i = 2; i < ctx->argc; i++) {
3421 set_column(table, row, ctx->argv[i], ctx->symtab);
3423 ds_put_format(&ctx->output, UUID_FMT, UUID_ARGS(&row->uuid));
3426 /* This function may be used as the 'postprocess' function for commands that
3427 * insert new rows into the database. It expects that the command's 'run'
3428 * function prints the UUID reported by ovsdb_idl_txn_insert() as the command's
3429 * sole output. It replaces that output by the row's permanent UUID assigned
3430 * by the database server and appends a new-line.
3432 * Currently we use this only for "create", because the higher-level commands
3433 * are supposed to be independent of the actual structure of the vswitch
3436 post_create(struct vsctl_context *ctx)
3438 const struct uuid *real;
3441 if (!uuid_from_string(&dummy, ds_cstr(&ctx->output))) {
3444 real = ovsdb_idl_txn_get_insert_uuid(ctx->txn, &dummy);
3446 ds_clear(&ctx->output);
3447 ds_put_format(&ctx->output, UUID_FMT, UUID_ARGS(real));
3449 ds_put_char(&ctx->output, '\n');
3453 pre_cmd_destroy(struct vsctl_context *ctx)
3455 const char *table_name = ctx->argv[1];
3457 pre_get_table(ctx, table_name);
3461 cmd_destroy(struct vsctl_context *ctx)
3463 bool must_exist = !shash_find(&ctx->options, "--if-exists");
3464 bool delete_all = shash_find(&ctx->options, "--all");
3465 const char *table_name = ctx->argv[1];
3466 const struct vsctl_table_class *table;
3469 table = get_table(table_name);
3471 if (delete_all && ctx->argc > 2) {
3472 vsctl_fatal("--all and records argument should not be specified together");
3475 if (delete_all && !must_exist) {
3476 vsctl_fatal("--all and --if-exists should not be specified together");
3480 const struct ovsdb_idl_row *row;
3481 const struct ovsdb_idl_row *next_row;
3483 for (row = ovsdb_idl_first_row(ctx->idl, table->class);
3485 next_row = ovsdb_idl_next_row(row);
3486 ovsdb_idl_txn_delete(row);
3490 for (i = 2; i < ctx->argc; i++) {
3491 const struct ovsdb_idl_row *row;
3493 row = (must_exist ? must_get_row : get_row)(ctx, table, ctx->argv[i]);
3495 ovsdb_idl_txn_delete(row);
3499 vsctl_context_invalidate_cache(ctx);
3503 RELOP(RELOP_EQ, "=") \
3504 RELOP(RELOP_NE, "!=") \
3505 RELOP(RELOP_LT, "<") \
3506 RELOP(RELOP_GT, ">") \
3507 RELOP(RELOP_LE, "<=") \
3508 RELOP(RELOP_GE, ">=") \
3509 RELOP(RELOP_SET_EQ, "{=}") \
3510 RELOP(RELOP_SET_NE, "{!=}") \
3511 RELOP(RELOP_SET_LT, "{<}") \
3512 RELOP(RELOP_SET_GT, "{>}") \
3513 RELOP(RELOP_SET_LE, "{<=}") \
3514 RELOP(RELOP_SET_GE, "{>=}")
3517 #define RELOP(ENUM, STRING) ENUM,
3523 is_set_operator(enum relop op)
3525 return (op == RELOP_SET_EQ || op == RELOP_SET_NE ||
3526 op == RELOP_SET_LT || op == RELOP_SET_GT ||
3527 op == RELOP_SET_LE || op == RELOP_SET_GE);
3531 evaluate_relop(const struct ovsdb_datum *a, const struct ovsdb_datum *b,
3532 const struct ovsdb_type *type, enum relop op)
3537 return ovsdb_datum_compare_3way(a, b, type) == 0;
3540 return ovsdb_datum_compare_3way(a, b, type) != 0;
3542 return ovsdb_datum_compare_3way(a, b, type) < 0;
3544 return ovsdb_datum_compare_3way(a, b, type) > 0;
3546 return ovsdb_datum_compare_3way(a, b, type) <= 0;
3548 return ovsdb_datum_compare_3way(a, b, type) >= 0;
3551 return b->n > a->n && ovsdb_datum_includes_all(a, b, type);
3553 return a->n > b->n && ovsdb_datum_includes_all(b, a, type);
3555 return ovsdb_datum_includes_all(a, b, type);
3557 return ovsdb_datum_includes_all(b, a, type);
3565 is_condition_satisfied(const struct vsctl_table_class *table,
3566 const struct ovsdb_idl_row *row, const char *arg,
3567 struct ovsdb_symbol_table *symtab)
3569 static const char *operators[] = {
3570 #define RELOP(ENUM, STRING) STRING,
3575 const struct ovsdb_idl_column *column;
3576 const struct ovsdb_datum *have_datum;
3577 char *key_string, *value_string;
3578 struct ovsdb_type type;
3583 error = parse_column_key_value(arg, table, &column, &key_string,
3584 &operator, operators, ARRAY_SIZE(operators),
3586 die_if_error(error);
3587 if (!value_string) {
3588 vsctl_fatal("%s: missing value", arg);
3591 type = column->type;
3592 type.n_max = UINT_MAX;
3594 have_datum = ovsdb_idl_read(row, column);
3596 union ovsdb_atom want_key;
3597 struct ovsdb_datum b;
3600 if (column->type.value.type == OVSDB_TYPE_VOID) {
3601 vsctl_fatal("cannot specify key to check for non-map column %s",
3605 die_if_error(ovsdb_atom_from_string(&want_key, &column->type.key,
3606 key_string, symtab));
3608 type.key = type.value;
3609 type.value.type = OVSDB_TYPE_VOID;
3610 die_if_error(ovsdb_datum_from_string(&b, &type, value_string, symtab));
3612 idx = ovsdb_datum_find_key(have_datum,
3613 &want_key, column->type.key.type);
3614 if (idx == UINT_MAX && !is_set_operator(operator)) {
3617 struct ovsdb_datum a;
3619 if (idx != UINT_MAX) {
3621 a.keys = &have_datum->values[idx];
3629 retval = evaluate_relop(&a, &b, &type, operator);
3632 ovsdb_atom_destroy(&want_key, column->type.key.type);
3633 ovsdb_datum_destroy(&b, &type);
3635 struct ovsdb_datum want_datum;
3637 die_if_error(ovsdb_datum_from_string(&want_datum, &column->type,
3638 value_string, symtab));
3639 retval = evaluate_relop(have_datum, &want_datum, &type, operator);
3640 ovsdb_datum_destroy(&want_datum, &column->type);
3650 pre_cmd_wait_until(struct vsctl_context *ctx)
3652 const char *table_name = ctx->argv[1];
3653 const struct vsctl_table_class *table;
3656 table = pre_get_table(ctx, table_name);
3658 for (i = 3; i < ctx->argc; i++) {
3659 pre_parse_column_key_value(ctx, ctx->argv[i], table);
3664 cmd_wait_until(struct vsctl_context *ctx)
3666 const char *table_name = ctx->argv[1];
3667 const char *record_id = ctx->argv[2];
3668 const struct vsctl_table_class *table;
3669 const struct ovsdb_idl_row *row;
3672 table = get_table(table_name);
3674 row = get_row(ctx, table, record_id);
3676 ctx->try_again = true;
3680 for (i = 3; i < ctx->argc; i++) {
3681 if (!is_condition_satisfied(table, row, ctx->argv[i], ctx->symtab)) {
3682 ctx->try_again = true;
3688 /* Prepares 'ctx', which has already been initialized with
3689 * vsctl_context_init(), for processing 'command'. */
3691 vsctl_context_init_command(struct vsctl_context *ctx,
3692 struct vsctl_command *command)
3694 ctx->argc = command->argc;
3695 ctx->argv = command->argv;
3696 ctx->options = command->options;
3698 ds_swap(&ctx->output, &command->output);
3699 ctx->table = command->table;
3701 ctx->verified_ports = false;
3703 ctx->try_again = false;
3706 /* Prepares 'ctx' for processing commands, initializing its members with the
3707 * values passed in as arguments.
3709 * If 'command' is nonnull, calls vsctl_context_init_command() to prepare for
3710 * that particular command. */
3712 vsctl_context_init(struct vsctl_context *ctx, struct vsctl_command *command,
3713 struct ovsdb_idl *idl, struct ovsdb_idl_txn *txn,
3714 const struct ovsrec_open_vswitch *ovs,
3715 struct ovsdb_symbol_table *symtab)
3718 vsctl_context_init_command(ctx, command);
3723 ctx->symtab = symtab;
3724 ctx->cache_valid = false;
3727 /* Completes processing of 'command' within 'ctx'. */
3729 vsctl_context_done_command(struct vsctl_context *ctx,
3730 struct vsctl_command *command)
3732 ds_swap(&ctx->output, &command->output);
3733 command->table = ctx->table;
3736 /* Finishes up with 'ctx'.
3738 * If command is nonnull, first calls vsctl_context_done_command() to complete
3739 * processing that command within 'ctx'. */
3741 vsctl_context_done(struct vsctl_context *ctx, struct vsctl_command *command)
3744 vsctl_context_done_command(ctx, command);
3746 vsctl_context_invalidate_cache(ctx);
3750 run_prerequisites(struct vsctl_command *commands, size_t n_commands,
3751 struct ovsdb_idl *idl)
3753 struct vsctl_command *c;
3755 ovsdb_idl_add_table(idl, &ovsrec_table_open_vswitch);
3756 if (wait_for_reload) {
3757 ovsdb_idl_add_column(idl, &ovsrec_open_vswitch_col_cur_cfg);
3759 for (c = commands; c < &commands[n_commands]; c++) {
3760 if (c->syntax->prerequisites) {
3761 struct vsctl_context ctx;
3763 ds_init(&c->output);
3766 vsctl_context_init(&ctx, c, idl, NULL, NULL, NULL);
3767 (c->syntax->prerequisites)(&ctx);
3768 vsctl_context_done(&ctx, c);
3770 assert(!c->output.string);
3777 do_vsctl(const char *args, struct vsctl_command *commands, size_t n_commands,
3778 struct ovsdb_idl *idl)
3780 struct ovsdb_idl_txn *txn;
3781 const struct ovsrec_open_vswitch *ovs;
3782 enum ovsdb_idl_txn_status status;
3783 struct ovsdb_symbol_table *symtab;
3784 struct vsctl_context ctx;
3785 struct vsctl_command *c;
3786 struct shash_node *node;
3787 int64_t next_cfg = 0;
3790 txn = the_idl_txn = ovsdb_idl_txn_create(idl);
3792 ovsdb_idl_txn_set_dry_run(txn);
3795 ovsdb_idl_txn_add_comment(txn, "ovs-vsctl: %s", args);
3797 ovs = ovsrec_open_vswitch_first(idl);
3799 /* XXX add verification that table is empty */
3800 ovs = ovsrec_open_vswitch_insert(txn);
3803 if (wait_for_reload) {
3804 ovsdb_idl_txn_increment(txn, &ovs->header_,
3805 &ovsrec_open_vswitch_col_next_cfg);
3808 symtab = ovsdb_symbol_table_create();
3809 for (c = commands; c < &commands[n_commands]; c++) {
3810 ds_init(&c->output);
3813 vsctl_context_init(&ctx, NULL, idl, txn, ovs, symtab);
3814 for (c = commands; c < &commands[n_commands]; c++) {
3815 vsctl_context_init_command(&ctx, c);
3816 if (c->syntax->run) {
3817 (c->syntax->run)(&ctx);
3819 vsctl_context_done_command(&ctx, c);
3821 if (ctx.try_again) {
3822 vsctl_context_done(&ctx, NULL);
3826 vsctl_context_done(&ctx, NULL);
3828 SHASH_FOR_EACH (node, &symtab->sh) {
3829 struct ovsdb_symbol *symbol = node->data;
3830 if (!symbol->created) {
3831 vsctl_fatal("row id \"%s\" is referenced but never created (e.g. "
3832 "with \"-- --id=%s create ...\")",
3833 node->name, node->name);
3835 if (!symbol->strong_ref) {
3836 if (!symbol->weak_ref) {
3837 VLOG_WARN("row id \"%s\" was created but no reference to it "
3838 "was inserted, so it will not actually appear in "
3839 "the database", node->name);
3841 VLOG_WARN("row id \"%s\" was created but only a weak "
3842 "reference to it was inserted, so it will not "
3843 "actually appear in the database", node->name);
3848 status = ovsdb_idl_txn_commit_block(txn);
3849 if (wait_for_reload && status == TXN_SUCCESS) {
3850 next_cfg = ovsdb_idl_txn_get_increment_new_value(txn);
3852 if (status == TXN_UNCHANGED || status == TXN_SUCCESS) {
3853 for (c = commands; c < &commands[n_commands]; c++) {
3854 if (c->syntax->postprocess) {
3855 struct vsctl_context ctx;
3857 vsctl_context_init(&ctx, c, idl, txn, ovs, symtab);
3858 (c->syntax->postprocess)(&ctx);
3859 vsctl_context_done(&ctx, c);
3863 error = xstrdup(ovsdb_idl_txn_get_error(txn));
3864 ovsdb_idl_txn_destroy(txn);
3865 txn = the_idl_txn = NULL;
3868 case TXN_UNCOMMITTED:
3869 case TXN_INCOMPLETE:
3873 /* Should not happen--we never call ovsdb_idl_txn_abort(). */
3874 vsctl_fatal("transaction aborted");
3884 vsctl_fatal("transaction error: %s", error);
3886 case TXN_NOT_LOCKED:
3887 /* Should not happen--we never call ovsdb_idl_set_lock(). */
3888 vsctl_fatal("database not locked");
3895 ovsdb_symbol_table_destroy(symtab);
3897 for (c = commands; c < &commands[n_commands]; c++) {
3898 struct ds *ds = &c->output;
3901 table_print(c->table, &table_style);
3902 } else if (oneline) {
3906 for (j = 0; j < ds->length; j++) {
3907 int ch = ds->string[j];
3910 fputs("\\n", stdout);
3914 fputs("\\\\", stdout);
3923 fputs(ds_cstr(ds), stdout);
3925 ds_destroy(&c->output);
3926 table_destroy(c->table);
3929 shash_destroy_free_data(&c->options);
3933 if (wait_for_reload && status != TXN_UNCHANGED) {
3936 OVSREC_OPEN_VSWITCH_FOR_EACH (ovs, idl) {
3937 if (ovs->cur_cfg >= next_cfg) {
3941 ovsdb_idl_wait(idl);
3946 ovsdb_idl_destroy(idl);
3951 /* Our transaction needs to be rerun, or a prerequisite was not met. Free
3952 * resources and return so that the caller can try again. */
3954 ovsdb_idl_txn_abort(txn);
3955 ovsdb_idl_txn_destroy(txn);
3957 ovsdb_symbol_table_destroy(symtab);
3958 for (c = commands; c < &commands[n_commands]; c++) {
3959 ds_destroy(&c->output);
3960 table_destroy(c->table);
3966 static const struct vsctl_command_syntax all_commands[] = {
3967 /* Open vSwitch commands. */
3968 {"init", 0, 0, NULL, cmd_init, NULL, "", RW},
3969 {"show", 0, 0, pre_cmd_show, cmd_show, NULL, "", RO},
3971 /* Bridge commands. */
3972 {"add-br", 1, 3, pre_get_info, cmd_add_br, NULL, "--may-exist", RW},
3973 {"del-br", 1, 1, pre_get_info, cmd_del_br, NULL, "--if-exists", RW},
3974 {"list-br", 0, 0, pre_get_info, cmd_list_br, NULL, "--real,--fake", RO},
3975 {"br-exists", 1, 1, pre_get_info, cmd_br_exists, NULL, "", RO},
3976 {"br-to-vlan", 1, 1, pre_get_info, cmd_br_to_vlan, NULL, "", RO},
3977 {"br-to-parent", 1, 1, pre_get_info, cmd_br_to_parent, NULL, "", RO},
3978 {"br-set-external-id", 2, 3, pre_cmd_br_set_external_id,
3979 cmd_br_set_external_id, NULL, "", RW},
3980 {"br-get-external-id", 1, 2, pre_cmd_br_get_external_id,
3981 cmd_br_get_external_id, NULL, "", RO},
3983 /* Port commands. */
3984 {"list-ports", 1, 1, pre_get_info, cmd_list_ports, NULL, "", RO},
3985 {"add-port", 2, INT_MAX, pre_get_info, cmd_add_port, NULL, "--may-exist",
3987 {"add-bond", 4, INT_MAX, pre_get_info, cmd_add_bond, NULL,
3988 "--may-exist,--fake-iface", RW},
3989 {"del-port", 1, 2, pre_get_info, cmd_del_port, NULL,
3990 "--if-exists,--with-iface", RW},
3991 {"port-to-br", 1, 1, pre_get_info, cmd_port_to_br, NULL, "", RO},
3993 /* Interface commands. */
3994 {"list-ifaces", 1, 1, pre_get_info, cmd_list_ifaces, NULL, "", RO},
3995 {"iface-to-br", 1, 1, pre_get_info, cmd_iface_to_br, NULL, "", RO},
3997 /* Controller commands. */
3998 {"get-controller", 1, 1, pre_controller, cmd_get_controller, NULL, "", RO},
3999 {"del-controller", 1, 1, pre_controller, cmd_del_controller, NULL, "", RW},
4000 {"set-controller", 1, INT_MAX, pre_controller, cmd_set_controller, NULL,
4002 {"get-fail-mode", 1, 1, pre_get_info, cmd_get_fail_mode, NULL, "", RO},
4003 {"del-fail-mode", 1, 1, pre_get_info, cmd_del_fail_mode, NULL, "", RW},
4004 {"set-fail-mode", 2, 2, pre_get_info, cmd_set_fail_mode, NULL, "", RW},
4006 /* Manager commands. */
4007 {"get-manager", 0, 0, pre_manager, cmd_get_manager, NULL, "", RO},
4008 {"del-manager", 0, 0, pre_manager, cmd_del_manager, NULL, "", RW},
4009 {"set-manager", 1, INT_MAX, pre_manager, cmd_set_manager, NULL, "", RW},
4012 {"get-ssl", 0, 0, pre_cmd_get_ssl, cmd_get_ssl, NULL, "", RO},
4013 {"del-ssl", 0, 0, pre_cmd_del_ssl, cmd_del_ssl, NULL, "", RW},
4014 {"set-ssl", 3, 3, pre_cmd_set_ssl, cmd_set_ssl, NULL, "--bootstrap", RW},
4016 /* Switch commands. */
4017 {"emer-reset", 0, 0, pre_cmd_emer_reset, cmd_emer_reset, NULL, "", RW},
4019 /* Database commands. */
4020 {"comment", 0, INT_MAX, NULL, NULL, NULL, "", RO},
4021 {"get", 2, INT_MAX, pre_cmd_get, cmd_get, NULL, "--if-exists,--id=", RO},
4022 {"list", 1, INT_MAX, pre_cmd_list, cmd_list, NULL, "--columns=", RO},
4023 {"find", 1, INT_MAX, pre_cmd_find, cmd_find, NULL, "--columns=", RO},
4024 {"set", 3, INT_MAX, pre_cmd_set, cmd_set, NULL, "", RW},
4025 {"add", 4, INT_MAX, pre_cmd_add, cmd_add, NULL, "", RW},
4026 {"remove", 4, INT_MAX, pre_cmd_remove, cmd_remove, NULL, "", RW},
4027 {"clear", 3, INT_MAX, pre_cmd_clear, cmd_clear, NULL, "", RW},
4028 {"create", 2, INT_MAX, pre_create, cmd_create, post_create, "--id=", RW},
4029 {"destroy", 1, INT_MAX, pre_cmd_destroy, cmd_destroy, NULL,
4030 "--if-exists,--all", RW},
4031 {"wait-until", 2, INT_MAX, pre_cmd_wait_until, cmd_wait_until, NULL, "",
4034 {NULL, 0, 0, NULL, NULL, NULL, NULL, RO},