2 * Copyright (c) 2009, 2010 Nicira Networks.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
30 #include "command-line.h"
33 #include "dynamic-string.h"
35 #include "ovsdb-data.h"
36 #include "ovsdb-idl.h"
37 #include "poll-loop.h"
40 #include "vswitchd/vswitch-idl.h"
45 #define THIS_MODULE VLM_vsctl
47 /* vsctl_fatal() also logs the error, so it is preferred in this file. */
48 #define ovs_fatal please_use_vsctl_fatal_instead_of_ovs_fatal
52 typedef void vsctl_handler_func(struct vsctl_context *);
54 struct vsctl_command_syntax {
58 vsctl_handler_func *run;
59 vsctl_handler_func *postprocess;
63 struct vsctl_command {
64 /* Data that remains constant after initialization. */
65 const struct vsctl_command_syntax *syntax;
70 /* Data modified by commands. */
74 /* --db: The database server to contact. */
75 static const char *db;
77 /* --oneline: Write each command's output as a single line? */
80 /* --dry-run: Do not commit any changes. */
83 /* --no-wait: Wait for ovs-vswitchd to reload its configuration? */
84 static bool wait_for_reload = true;
86 /* --timeout: Time to wait for a connection to 'db'. */
87 static int timeout = 5;
89 /* All supported commands. */
90 static const struct vsctl_command_syntax all_commands[];
92 /* The IDL we're using and the current transaction, if any.
93 * This is for use by vsctl_exit() only, to allow it to clean up.
94 * Other code should use its context arguments. */
95 static struct ovsdb_idl *the_idl;
96 static struct ovsdb_idl_txn *the_idl_txn;
98 static void vsctl_exit(int status) NO_RETURN;
99 static void vsctl_fatal(const char *, ...) PRINTF_FORMAT(1, 2) NO_RETURN;
100 static char *default_db(void);
101 static void usage(void) NO_RETURN;
102 static void parse_options(int argc, char *argv[]);
104 static struct vsctl_command *parse_commands(int argc, char *argv[],
105 size_t *n_commandsp);
106 static void parse_command(int argc, char *argv[], struct vsctl_command *);
107 static void do_vsctl(const char *args,
108 struct vsctl_command *, size_t n_commands,
111 static const struct vsctl_table_class *get_table(const char *table_name);
112 static void set_column(const struct vsctl_table_class *,
113 const struct ovsdb_idl_row *, const char *arg);
117 main(int argc, char *argv[])
119 struct ovsdb_idl *idl;
121 struct vsctl_command *commands;
126 set_program_name(argv[0]);
127 signal(SIGPIPE, SIG_IGN);
130 vlog_set_levels(VLM_ANY_MODULE, VLF_CONSOLE, VLL_WARN);
131 vlog_set_levels(VLM_reconnect, VLF_ANY_FACILITY, VLL_WARN);
134 /* Log our arguments. This is often valuable for debugging systems. */
135 args = process_escape_args(argv);
136 VLOG_INFO("Called as %s", args);
138 /* Parse command line. */
139 parse_options(argc, argv);
140 commands = parse_commands(argc - optind, argv + optind, &n_commands);
146 /* Now execute the commands. */
147 idl = the_idl = ovsdb_idl_create(db, &ovsrec_idl_class);
148 seqno = ovsdb_idl_get_seqno(idl);
151 unsigned int new_seqno;
154 new_seqno = ovsdb_idl_get_seqno(idl);
155 if (new_seqno != seqno) {
157 vsctl_fatal("too many database inconsistency failures");
159 do_vsctl(args, commands, n_commands, idl);
169 parse_options(int argc, char *argv[])
172 OPT_DB = UCHAR_MAX + 1,
179 static struct option long_options[] = {
180 {"db", required_argument, 0, OPT_DB},
181 {"no-syslog", no_argument, 0, OPT_NO_SYSLOG},
182 {"no-wait", no_argument, 0, OPT_NO_WAIT},
183 {"dry-run", no_argument, 0, OPT_DRY_RUN},
184 {"oneline", no_argument, 0, OPT_ONELINE},
185 {"timeout", required_argument, 0, 't'},
186 {"help", no_argument, 0, 'h'},
187 {"version", no_argument, 0, 'V'},
196 c = getopt_long(argc, argv, "+v::hVt:", long_options, NULL);
211 vlog_set_levels(VLM_vsctl, VLF_SYSLOG, VLL_WARN);
215 wait_for_reload = false;
226 OVS_PRINT_VERSION(0, 0);
230 timeout = strtoul(optarg, NULL, 10);
232 vsctl_fatal("value %s on -t or --timeout is invalid",
252 static struct vsctl_command *
253 parse_commands(int argc, char *argv[], size_t *n_commandsp)
255 struct vsctl_command *commands;
256 size_t n_commands, allocated_commands;
260 n_commands = allocated_commands = 0;
262 for (start = i = 0; i <= argc; i++) {
263 if (i == argc || !strcmp(argv[i], "--")) {
265 if (n_commands >= allocated_commands) {
266 struct vsctl_command *c;
268 commands = x2nrealloc(commands, &allocated_commands,
270 for (c = commands; c < &commands[n_commands]; c++) {
271 shash_moved(&c->options);
274 parse_command(i - start, &argv[start],
275 &commands[n_commands++]);
281 vsctl_fatal("missing command name (use --help for help)");
283 *n_commandsp = n_commands;
288 parse_command(int argc, char *argv[], struct vsctl_command *command)
290 const struct vsctl_command_syntax *p;
293 shash_init(&command->options);
294 for (i = 0; i < argc; i++) {
295 if (argv[i][0] != '-') {
298 if (!shash_add_once(&command->options, argv[i], NULL)) {
299 vsctl_fatal("'%s' option specified multiple times", argv[i]);
303 vsctl_fatal("missing command name");
306 for (p = all_commands; p->name; p++) {
307 if (!strcmp(p->name, argv[i])) {
308 struct shash_node *node;
311 SHASH_FOR_EACH (node, &command->options) {
312 const char *s = strstr(p->options, node->name);
313 int end = s ? s[strlen(node->name)] : EOF;
314 if (end != ',' && end != ' ' && end != '\0') {
315 vsctl_fatal("'%s' command has no '%s' option",
316 argv[i], node->name);
320 n_arg = argc - i - 1;
321 if (n_arg < p->min_args) {
322 vsctl_fatal("'%s' command requires at least %d arguments",
323 p->name, p->min_args);
324 } else if (n_arg > p->max_args) {
327 for (j = i + 1; j < argc; j++) {
328 if (argv[j][0] == '-') {
329 vsctl_fatal("'%s' command takes at most %d arguments "
330 "(note that options must precede command "
331 "names and follow a \"--\" argument)",
332 p->name, p->max_args);
336 vsctl_fatal("'%s' command takes at most %d arguments",
337 p->name, p->max_args);
340 command->argc = n_arg + 1;
341 command->argv = &argv[i];
347 vsctl_fatal("unknown command '%s'; use --help for help", argv[i]);
351 vsctl_fatal(const char *format, ...)
356 va_start(args, format);
357 message = xvasprintf(format, args);
360 vlog_set_levels(VLM_vsctl, VLF_CONSOLE, VLL_EMER);
361 VLOG_ERR("%s", message);
362 ovs_error(0, "%s", message);
363 vsctl_exit(EXIT_FAILURE);
366 /* Frees the current transaction and the underlying IDL and then calls
369 * Freeing the transaction and the IDL is not strictly necessary, but it makes
370 * for a clean memory leak report from valgrind in the normal case. That makes
371 * it easier to notice real memory leaks. */
373 vsctl_exit(int status)
376 ovsdb_idl_txn_abort(the_idl_txn);
377 ovsdb_idl_txn_destroy(the_idl_txn);
379 ovsdb_idl_destroy(the_idl);
387 %s: ovs-vswitchd management utility\n\
388 usage: %s [OPTIONS] COMMAND [ARG...]\n\
391 add-br BRIDGE create a new bridge named BRIDGE\n\
392 add-br BRIDGE PARENT VLAN create new fake BRIDGE in PARENT on VLAN\n\
393 del-br BRIDGE delete BRIDGE and all of its ports\n\
394 list-br print the names of all the bridges\n\
395 br-exists BRIDGE test whether BRIDGE exists\n\
396 br-to-vlan BRIDGE print the VLAN which BRIDGE is on\n\
397 br-to-parent BRIDGE print the parent of BRIDGE\n\
398 br-set-external-id BRIDGE KEY VALUE set KEY on BRIDGE to VALUE\n\
399 br-set-external-id BRIDGE KEY unset KEY on BRIDGE\n\
400 br-get-external-id BRIDGE KEY print value of KEY on BRIDGE\n\
401 br-get-external-id BRIDGE list key-value pairs on BRIDGE\n\
404 list-ports BRIDGE print the names of all the ports on BRIDGE\n\
405 add-port BRIDGE PORT add network device PORT to BRIDGE\n\
406 add-bond BRIDGE PORT IFACE... add bonded port PORT in BRIDGE from IFACES\n\
407 del-port [BRIDGE] PORT delete PORT (which may be bonded) from BRIDGE\n\
408 port-to-br PORT print name of bridge that contains PORT\n\
409 A bond is considered to be a single port.\n\
411 Interface commands (a bond consists of multiple interfaces):\n\
412 list-ifaces BRIDGE print the names of all interfaces on BRIDGE\n\
413 iface-to-br IFACE print name of bridge that contains IFACE\n\
415 Controller commands:\n\
416 get-controller [BRIDGE] print the controller for BRIDGE\n\
417 del-controller [BRIDGE] delete the controller for BRIDGE\n\
418 set-controller [BRIDGE] TARGET set the controller for BRIDGE to TARGET\n\
419 get-fail-mode [BRIDGE] print the fail-mode for BRIDGE\n\
420 del-fail-mode [BRIDGE] delete the fail-mode for BRIDGE\n\
421 set-fail-mode [BRIDGE] MODE set the fail-mode for BRIDGE to MODE\n\
424 get-ssl print the SSL configuration\n\
425 del-ssl delete the SSL configuration\n\
426 set-ssl PRIV-KEY CERT CA-CERT set the SSL configuration\n\
428 Database commands:\n\
429 list TBL [REC] list RECord (or all records) in TBL\n\
430 get TBL REC COL[:KEY] print values of COLumns in RECORD in TBL\n\
431 set TBL REC COL[:KEY]=VALUE set COLumn values in RECord in TBL\n\
432 add TBL REC COL [KEY=]VALUE add (KEY=)VALUE to COLumn in RECord in TBL\n\
433 remove TBL REC COL [KEY=]VALUE remove (KEY=)VALUE from COLumn\n\
434 clear TBL REC COL clear values from COLumn in RECord in TBL\n\
435 create TBL COL[:KEY]=VALUE create and initialize new record\n\
436 destroy TBL REC delete REC from TBL\n\
437 Potentially unsafe database commands require --force option.\n\
440 --db=DATABASE connect to DATABASE\n\
442 --oneline print exactly one line of output per command\n",
443 program_name, program_name, default_db());
447 -h, --help display this help message\n\
448 -V, --version display version information\n");
457 def = xasprintf("unix:%s/db.sock", ovs_rundir);
462 struct vsctl_context {
466 struct shash options;
468 /* Modifiable state. */
470 struct ovsdb_idl *idl;
471 struct ovsdb_idl_txn *txn;
472 const struct ovsrec_open_vswitch *ovs;
475 struct vsctl_bridge {
476 struct ovsrec_bridge *br_cfg;
478 struct ovsrec_controller *ctrl;
479 struct vsctl_bridge *parent;
484 struct ovsrec_port *port_cfg;
485 struct vsctl_bridge *bridge;
489 struct ovsrec_interface *iface_cfg;
490 struct vsctl_port *port;
494 struct shash bridges;
497 struct ovsrec_controller *ctrl;
501 vsctl_context_to_string(const struct vsctl_context *ctx)
503 const struct shash_node *node;
509 SHASH_FOR_EACH (node, &ctx->options) {
510 svec_add(&words, node->name);
512 for (i = 0; i < ctx->argc; i++) {
513 svec_add(&words, ctx->argv[i]);
515 svec_terminate(&words);
517 s = process_escape_args(words.names);
519 svec_destroy(&words);
524 static struct vsctl_bridge *
525 add_bridge(struct vsctl_info *b,
526 struct ovsrec_bridge *br_cfg, const char *name,
527 struct vsctl_bridge *parent, int vlan)
529 struct vsctl_bridge *br = xmalloc(sizeof *br);
531 br->name = xstrdup(name);
534 br->ctrl = parent ? parent->br_cfg->controller : br_cfg->controller;
535 shash_add(&b->bridges, br->name, br);
540 port_is_fake_bridge(const struct ovsrec_port *port_cfg)
542 return (port_cfg->fake_bridge
544 && *port_cfg->tag >= 1 && *port_cfg->tag <= 4095);
547 static struct vsctl_bridge *
548 find_vlan_bridge(struct vsctl_info *info,
549 struct vsctl_bridge *parent, int vlan)
551 struct shash_node *node;
553 SHASH_FOR_EACH (node, &info->bridges) {
554 struct vsctl_bridge *br = node->data;
555 if (br->parent == parent && br->vlan == vlan) {
564 free_info(struct vsctl_info *info)
566 struct shash_node *node;
568 SHASH_FOR_EACH (node, &info->bridges) {
569 struct vsctl_bridge *bridge = node->data;
573 shash_destroy(&info->bridges);
575 SHASH_FOR_EACH (node, &info->ports) {
576 struct vsctl_port *port = node->data;
579 shash_destroy(&info->ports);
581 SHASH_FOR_EACH (node, &info->ifaces) {
582 struct vsctl_iface *iface = node->data;
585 shash_destroy(&info->ifaces);
589 get_info(const struct ovsrec_open_vswitch *ovs, struct vsctl_info *info)
591 struct shash bridges, ports;
594 shash_init(&info->bridges);
595 shash_init(&info->ports);
596 shash_init(&info->ifaces);
598 info->ctrl = ovs->controller;
600 shash_init(&bridges);
602 for (i = 0; i < ovs->n_bridges; i++) {
603 struct ovsrec_bridge *br_cfg = ovs->bridges[i];
604 struct vsctl_bridge *br;
607 if (!shash_add_once(&bridges, br_cfg->name, NULL)) {
608 VLOG_WARN("%s: database contains duplicate bridge name",
612 br = add_bridge(info, br_cfg, br_cfg->name, NULL, 0);
617 for (j = 0; j < br_cfg->n_ports; j++) {
618 struct ovsrec_port *port_cfg = br_cfg->ports[j];
620 if (!shash_add_once(&ports, port_cfg->name, NULL)) {
621 VLOG_WARN("%s: database contains duplicate port name",
626 if (port_is_fake_bridge(port_cfg)
627 && shash_add_once(&bridges, port_cfg->name, NULL)) {
628 add_bridge(info, NULL, port_cfg->name, br, *port_cfg->tag);
632 shash_destroy(&bridges);
633 shash_destroy(&ports);
635 shash_init(&bridges);
637 for (i = 0; i < ovs->n_bridges; i++) {
638 struct ovsrec_bridge *br_cfg = ovs->bridges[i];
639 struct vsctl_bridge *br;
642 if (!shash_add_once(&bridges, br_cfg->name, NULL)) {
645 br = shash_find_data(&info->bridges, br_cfg->name);
646 for (j = 0; j < br_cfg->n_ports; j++) {
647 struct ovsrec_port *port_cfg = br_cfg->ports[j];
648 struct vsctl_port *port;
651 if (!shash_add_once(&ports, port_cfg->name, NULL)) {
655 if (port_is_fake_bridge(port_cfg)
656 && !shash_add_once(&bridges, port_cfg->name, NULL)) {
660 port = xmalloc(sizeof *port);
661 port->port_cfg = port_cfg;
663 && *port_cfg->tag >= 1 && *port_cfg->tag <= 4095) {
664 port->bridge = find_vlan_bridge(info, br, *port_cfg->tag);
671 shash_add(&info->ports, port_cfg->name, port);
673 for (k = 0; k < port_cfg->n_interfaces; k++) {
674 struct ovsrec_interface *iface_cfg = port_cfg->interfaces[k];
675 struct vsctl_iface *iface;
677 if (shash_find(&info->ifaces, iface_cfg->name)) {
678 VLOG_WARN("%s: database contains duplicate interface name",
683 iface = xmalloc(sizeof *iface);
684 iface->iface_cfg = iface_cfg;
686 shash_add(&info->ifaces, iface_cfg->name, iface);
690 shash_destroy(&bridges);
691 shash_destroy(&ports);
695 check_conflicts(struct vsctl_info *info, const char *name,
698 struct vsctl_iface *iface;
699 struct vsctl_port *port;
701 if (shash_find(&info->bridges, name)) {
702 vsctl_fatal("%s because a bridge named %s already exists",
706 port = shash_find_data(&info->ports, name);
708 vsctl_fatal("%s because a port named %s already exists on "
709 "bridge %s", msg, name, port->bridge->name);
712 iface = shash_find_data(&info->ifaces, name);
714 vsctl_fatal("%s because an interface named %s already exists "
715 "on bridge %s", msg, name, iface->port->bridge->name);
721 static struct vsctl_bridge *
722 find_bridge(struct vsctl_info *info, const char *name, bool must_exist)
724 struct vsctl_bridge *br = shash_find_data(&info->bridges, name);
725 if (must_exist && !br) {
726 vsctl_fatal("no bridge named %s", name);
731 static struct vsctl_bridge *
732 find_real_bridge(struct vsctl_info *info, const char *name, bool must_exist)
734 struct vsctl_bridge *br = find_bridge(info, name, must_exist);
735 if (br && br->parent) {
736 vsctl_fatal("%s is a fake bridge", name);
741 static struct vsctl_port *
742 find_port(struct vsctl_info *info, const char *name, bool must_exist)
744 struct vsctl_port *port = shash_find_data(&info->ports, name);
745 if (port && !strcmp(name, port->bridge->name)) {
748 if (must_exist && !port) {
749 vsctl_fatal("no port named %s", name);
754 static struct vsctl_iface *
755 find_iface(struct vsctl_info *info, const char *name, bool must_exist)
757 struct vsctl_iface *iface = shash_find_data(&info->ifaces, name);
758 if (iface && !strcmp(name, iface->port->bridge->name)) {
761 if (must_exist && !iface) {
762 vsctl_fatal("no interface named %s", name);
768 bridge_insert_port(struct ovsrec_bridge *br, struct ovsrec_port *port)
770 struct ovsrec_port **ports;
773 ports = xmalloc(sizeof *br->ports * (br->n_ports + 1));
774 for (i = 0; i < br->n_ports; i++) {
775 ports[i] = br->ports[i];
777 ports[br->n_ports] = port;
778 ovsrec_bridge_set_ports(br, ports, br->n_ports + 1);
783 bridge_delete_port(struct ovsrec_bridge *br, struct ovsrec_port *port)
785 struct ovsrec_port **ports;
788 ports = xmalloc(sizeof *br->ports * br->n_ports);
789 for (i = n = 0; i < br->n_ports; i++) {
790 if (br->ports[i] != port) {
791 ports[n++] = br->ports[i];
794 ovsrec_bridge_set_ports(br, ports, n);
799 ovs_insert_bridge(const struct ovsrec_open_vswitch *ovs,
800 struct ovsrec_bridge *bridge)
802 struct ovsrec_bridge **bridges;
805 bridges = xmalloc(sizeof *ovs->bridges * (ovs->n_bridges + 1));
806 for (i = 0; i < ovs->n_bridges; i++) {
807 bridges[i] = ovs->bridges[i];
809 bridges[ovs->n_bridges] = bridge;
810 ovsrec_open_vswitch_set_bridges(ovs, bridges, ovs->n_bridges + 1);
815 ovs_delete_bridge(const struct ovsrec_open_vswitch *ovs,
816 struct ovsrec_bridge *bridge)
818 struct ovsrec_bridge **bridges;
821 bridges = xmalloc(sizeof *ovs->bridges * ovs->n_bridges);
822 for (i = n = 0; i < ovs->n_bridges; i++) {
823 if (ovs->bridges[i] != bridge) {
824 bridges[n++] = ovs->bridges[i];
827 ovsrec_open_vswitch_set_bridges(ovs, bridges, n);
832 cmd_init(struct vsctl_context *ctx OVS_UNUSED)
837 cmd_add_br(struct vsctl_context *ctx)
839 bool may_exist = shash_find(&ctx->options, "--may-exist") != 0;
840 const char *br_name, *parent_name;
841 struct vsctl_info info;
844 br_name = ctx->argv[1];
845 if (ctx->argc == 2) {
848 } else if (ctx->argc == 4) {
849 parent_name = ctx->argv[2];
850 vlan = atoi(ctx->argv[3]);
851 if (vlan < 1 || vlan > 4095) {
852 vsctl_fatal("%s: vlan must be between 1 and 4095", ctx->argv[0]);
855 vsctl_fatal("'%s' command takes exactly 1 or 3 arguments",
859 get_info(ctx->ovs, &info);
861 struct vsctl_bridge *br;
863 br = find_bridge(&info, br_name, false);
867 vsctl_fatal("\"--may-exist add-br %s\" but %s is "
868 "a VLAN bridge for VLAN %d",
869 br_name, br_name, br->vlan);
873 vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
874 "is not a VLAN bridge",
875 br_name, parent_name, vlan, br_name);
876 } else if (strcmp(br->parent->name, parent_name)) {
877 vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
878 "has the wrong parent %s",
879 br_name, parent_name, vlan,
880 br_name, br->parent->name);
881 } else if (br->vlan != vlan) {
882 vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
883 "is a VLAN bridge for the wrong VLAN %d",
884 br_name, parent_name, vlan, br_name, br->vlan);
890 check_conflicts(&info, br_name,
891 xasprintf("cannot create a bridge named %s", br_name));
894 struct ovsrec_port *port;
895 struct ovsrec_interface *iface;
896 struct ovsrec_bridge *br;
898 iface = ovsrec_interface_insert(ctx->txn);
899 ovsrec_interface_set_name(iface, br_name);
901 port = ovsrec_port_insert(ctx->txn);
902 ovsrec_port_set_name(port, br_name);
903 ovsrec_port_set_interfaces(port, &iface, 1);
905 br = ovsrec_bridge_insert(ctx->txn);
906 ovsrec_bridge_set_name(br, br_name);
907 ovsrec_bridge_set_ports(br, &port, 1);
909 ovs_insert_bridge(ctx->ovs, br);
911 struct vsctl_bridge *parent;
912 struct ovsrec_port *port;
913 struct ovsrec_interface *iface;
914 struct ovsrec_bridge *br;
917 parent = find_bridge(&info, parent_name, false);
918 if (parent && parent->vlan) {
919 vsctl_fatal("cannot create bridge with fake bridge as parent");
922 vsctl_fatal("parent bridge %s does not exist", parent_name);
926 iface = ovsrec_interface_insert(ctx->txn);
927 ovsrec_interface_set_name(iface, br_name);
928 ovsrec_interface_set_type(iface, "internal");
930 port = ovsrec_port_insert(ctx->txn);
931 ovsrec_port_set_name(port, br_name);
932 ovsrec_port_set_interfaces(port, &iface, 1);
933 ovsrec_port_set_fake_bridge(port, true);
934 ovsrec_port_set_tag(port, &tag, 1);
936 bridge_insert_port(br, port);
943 del_port(struct vsctl_info *info, struct vsctl_port *port)
945 struct shash_node *node;
947 SHASH_FOR_EACH (node, &info->ifaces) {
948 struct vsctl_iface *iface = node->data;
949 if (iface->port == port) {
950 ovsrec_interface_delete(iface->iface_cfg);
953 ovsrec_port_delete(port->port_cfg);
955 bridge_delete_port((port->bridge->parent
956 ? port->bridge->parent->br_cfg
957 : port->bridge->br_cfg), port->port_cfg);
961 cmd_del_br(struct vsctl_context *ctx)
963 bool must_exist = !shash_find(&ctx->options, "--if-exists");
964 struct vsctl_bridge *bridge;
965 struct vsctl_info info;
967 get_info(ctx->ovs, &info);
968 bridge = find_bridge(&info, ctx->argv[1], must_exist);
970 struct shash_node *node;
972 SHASH_FOR_EACH (node, &info.ports) {
973 struct vsctl_port *port = node->data;
974 if (port->bridge == bridge || port->bridge->parent == bridge
975 || !strcmp(port->port_cfg->name, bridge->name)) {
976 del_port(&info, port);
979 if (bridge->br_cfg) {
980 ovsrec_bridge_delete(bridge->br_cfg);
981 ovs_delete_bridge(ctx->ovs, bridge->br_cfg);
988 output_sorted(struct svec *svec, struct ds *output)
994 SVEC_FOR_EACH (i, name, svec) {
995 ds_put_format(output, "%s\n", name);
1000 cmd_list_br(struct vsctl_context *ctx)
1002 struct shash_node *node;
1003 struct vsctl_info info;
1004 struct svec bridges;
1006 get_info(ctx->ovs, &info);
1008 svec_init(&bridges);
1009 SHASH_FOR_EACH (node, &info.bridges) {
1010 struct vsctl_bridge *br = node->data;
1011 svec_add(&bridges, br->name);
1013 output_sorted(&bridges, &ctx->output);
1014 svec_destroy(&bridges);
1020 cmd_br_exists(struct vsctl_context *ctx)
1022 struct vsctl_info info;
1024 get_info(ctx->ovs, &info);
1025 if (!find_bridge(&info, ctx->argv[1], false)) {
1031 /* Returns true if 'b_prefix' (of length 'b_prefix_len') concatenated with 'b'
1032 * equals 'a', false otherwise. */
1034 key_matches(const char *a,
1035 const char *b_prefix, size_t b_prefix_len, const char *b)
1037 return !strncmp(a, b_prefix, b_prefix_len) && !strcmp(a + b_prefix_len, b);
1041 set_external_id(char **old_keys, char **old_values, size_t old_n,
1042 char *key, char *value,
1043 char ***new_keysp, char ***new_valuesp, size_t *new_np)
1050 new_keys = xmalloc(sizeof *new_keys * (old_n + 1));
1051 new_values = xmalloc(sizeof *new_values * (old_n + 1));
1053 for (i = 0; i < old_n; i++) {
1054 if (strcmp(key, old_keys[i])) {
1055 new_keys[new_n] = old_keys[i];
1056 new_values[new_n] = old_values[i];
1061 new_keys[new_n] = key;
1062 new_values[new_n] = value;
1065 *new_keysp = new_keys;
1066 *new_valuesp = new_values;
1071 cmd_br_set_external_id(struct vsctl_context *ctx)
1073 struct vsctl_info info;
1074 struct vsctl_bridge *bridge;
1075 char **keys, **values;
1078 get_info(ctx->ovs, &info);
1079 bridge = find_bridge(&info, ctx->argv[1], true);
1080 if (bridge->br_cfg) {
1081 set_external_id(bridge->br_cfg->key_external_ids,
1082 bridge->br_cfg->value_external_ids,
1083 bridge->br_cfg->n_external_ids,
1084 ctx->argv[2], ctx->argc >= 4 ? ctx->argv[3] : NULL,
1085 &keys, &values, &n);
1086 ovsrec_bridge_set_external_ids(bridge->br_cfg, keys, values, n);
1088 char *key = xasprintf("fake-bridge-%s", ctx->argv[2]);
1089 struct vsctl_port *port = shash_find_data(&info.ports, ctx->argv[1]);
1090 set_external_id(port->port_cfg->key_external_ids,
1091 port->port_cfg->value_external_ids,
1092 port->port_cfg->n_external_ids,
1093 key, ctx->argc >= 4 ? ctx->argv[3] : NULL,
1094 &keys, &values, &n);
1095 ovsrec_port_set_external_ids(port->port_cfg, keys, values, n);
1105 get_external_id(char **keys, char **values, size_t n,
1106 const char *prefix, const char *key,
1109 size_t prefix_len = strlen(prefix);
1114 for (i = 0; i < n; i++) {
1115 if (!key && !strncmp(keys[i], prefix, prefix_len)) {
1116 svec_add_nocopy(&svec, xasprintf("%s=%s",
1117 keys[i] + prefix_len, values[i]));
1118 } else if (key_matches(keys[i], prefix, prefix_len, key)) {
1119 svec_add(&svec, values[i]);
1123 output_sorted(&svec, output);
1124 svec_destroy(&svec);
1128 cmd_br_get_external_id(struct vsctl_context *ctx)
1130 struct vsctl_info info;
1131 struct vsctl_bridge *bridge;
1133 get_info(ctx->ovs, &info);
1134 bridge = find_bridge(&info, ctx->argv[1], true);
1135 if (bridge->br_cfg) {
1136 get_external_id(bridge->br_cfg->key_external_ids,
1137 bridge->br_cfg->value_external_ids,
1138 bridge->br_cfg->n_external_ids,
1139 "", ctx->argc >= 3 ? ctx->argv[2] : NULL,
1142 struct vsctl_port *port = shash_find_data(&info.ports, ctx->argv[1]);
1143 get_external_id(port->port_cfg->key_external_ids,
1144 port->port_cfg->value_external_ids,
1145 port->port_cfg->n_external_ids,
1146 "fake-bridge-", ctx->argc >= 3 ? ctx->argv[2] : NULL, &ctx->output);
1153 cmd_list_ports(struct vsctl_context *ctx)
1155 struct vsctl_bridge *br;
1156 struct shash_node *node;
1157 struct vsctl_info info;
1160 get_info(ctx->ovs, &info);
1161 br = find_bridge(&info, ctx->argv[1], true);
1164 SHASH_FOR_EACH (node, &info.ports) {
1165 struct vsctl_port *port = node->data;
1167 if (strcmp(port->port_cfg->name, br->name) && br == port->bridge) {
1168 svec_add(&ports, port->port_cfg->name);
1171 output_sorted(&ports, &ctx->output);
1172 svec_destroy(&ports);
1178 add_port(struct vsctl_context *ctx,
1179 const char *br_name, const char *port_name,
1180 bool may_exist, bool fake_iface,
1181 char *iface_names[], int n_ifaces,
1182 char *settings[], int n_settings)
1184 struct vsctl_info info;
1185 struct vsctl_bridge *bridge;
1186 struct ovsrec_interface **ifaces;
1187 struct ovsrec_port *port;
1190 get_info(ctx->ovs, &info);
1192 struct vsctl_port *port;
1194 port = find_port(&info, port_name, false);
1196 struct svec want_names, have_names;
1199 svec_init(&want_names);
1200 for (i = 0; i < n_ifaces; i++) {
1201 svec_add(&want_names, iface_names[i]);
1203 svec_sort(&want_names);
1205 svec_init(&have_names);
1206 for (i = 0; i < port->port_cfg->n_interfaces; i++) {
1207 svec_add(&have_names, port->port_cfg->interfaces[i]->name);
1209 svec_sort(&have_names);
1211 if (strcmp(port->bridge->name, br_name)) {
1212 char *command = vsctl_context_to_string(ctx);
1213 vsctl_fatal("\"%s\" but %s is actually attached to bridge %s",
1214 command, port_name, port->bridge->name);
1217 if (!svec_equal(&want_names, &have_names)) {
1218 char *have_names_string = svec_join(&have_names, ", ", "");
1219 char *command = vsctl_context_to_string(ctx);
1221 vsctl_fatal("\"%s\" but %s actually has interface(s) %s",
1222 command, port_name, have_names_string);
1225 svec_destroy(&want_names);
1226 svec_destroy(&have_names);
1231 check_conflicts(&info, port_name,
1232 xasprintf("cannot create a port named %s", port_name));
1233 for (i = 0; i < n_ifaces; i++) {
1234 check_conflicts(&info, iface_names[i],
1235 xasprintf("cannot create an interface named %s",
1238 bridge = find_bridge(&info, br_name, true);
1240 ifaces = xmalloc(n_ifaces * sizeof *ifaces);
1241 for (i = 0; i < n_ifaces; i++) {
1242 ifaces[i] = ovsrec_interface_insert(ctx->txn);
1243 ovsrec_interface_set_name(ifaces[i], iface_names[i]);
1246 port = ovsrec_port_insert(ctx->txn);
1247 ovsrec_port_set_name(port, port_name);
1248 ovsrec_port_set_interfaces(port, ifaces, n_ifaces);
1249 ovsrec_port_set_bond_fake_iface(port, fake_iface);
1253 int64_t tag = bridge->vlan;
1254 ovsrec_port_set_tag(port, &tag, 1);
1257 for (i = 0; i < n_settings; i++) {
1258 set_column(get_table("Port"), &port->header_, settings[i]);
1261 bridge_insert_port((bridge->parent ? bridge->parent->br_cfg
1262 : bridge->br_cfg), port);
1268 cmd_add_port(struct vsctl_context *ctx)
1270 bool may_exist = shash_find(&ctx->options, "--may-exist") != 0;
1272 add_port(ctx, ctx->argv[1], ctx->argv[2], may_exist, false,
1273 &ctx->argv[2], 1, &ctx->argv[3], ctx->argc - 3);
1277 cmd_add_bond(struct vsctl_context *ctx)
1279 bool may_exist = shash_find(&ctx->options, "--may-exist") != 0;
1280 bool fake_iface = shash_find(&ctx->options, "--fake-iface");
1284 n_ifaces = ctx->argc - 3;
1285 for (i = 3; i < ctx->argc; i++) {
1286 if (strchr(ctx->argv[i], '=')) {
1292 vsctl_fatal("add-bond requires at least 2 interfaces, but only "
1293 "%d were specified", n_ifaces);
1296 add_port(ctx, ctx->argv[1], ctx->argv[2], may_exist, fake_iface,
1297 &ctx->argv[3], n_ifaces,
1298 &ctx->argv[n_ifaces + 3], ctx->argc - 3 - n_ifaces);
1302 cmd_del_port(struct vsctl_context *ctx)
1304 bool must_exist = !shash_find(&ctx->options, "--if-exists");
1305 bool with_iface = shash_find(&ctx->options, "--with-iface") != NULL;
1306 struct vsctl_port *port;
1307 struct vsctl_info info;
1309 get_info(ctx->ovs, &info);
1311 port = find_port(&info, ctx->argv[ctx->argc - 1], must_exist);
1313 const char *target = ctx->argv[ctx->argc - 1];
1314 struct vsctl_iface *iface;
1316 port = find_port(&info, target, false);
1318 iface = find_iface(&info, target, false);
1323 if (must_exist && !port) {
1324 vsctl_fatal("no port or interface named %s", target);
1329 if (ctx->argc == 3) {
1330 struct vsctl_bridge *bridge;
1332 bridge = find_bridge(&info, ctx->argv[1], true);
1333 if (port->bridge != bridge) {
1334 if (port->bridge->parent == bridge) {
1335 vsctl_fatal("bridge %s does not have a port %s (although "
1336 "its parent bridge %s does)",
1337 ctx->argv[1], ctx->argv[2],
1338 bridge->parent->name);
1340 vsctl_fatal("bridge %s does not have a port %s",
1341 ctx->argv[1], ctx->argv[2]);
1346 del_port(&info, port);
1353 cmd_port_to_br(struct vsctl_context *ctx)
1355 struct vsctl_port *port;
1356 struct vsctl_info info;
1358 get_info(ctx->ovs, &info);
1359 port = find_port(&info, ctx->argv[1], true);
1360 ds_put_format(&ctx->output, "%s\n", port->bridge->name);
1365 cmd_br_to_vlan(struct vsctl_context *ctx)
1367 struct vsctl_bridge *bridge;
1368 struct vsctl_info info;
1370 get_info(ctx->ovs, &info);
1371 bridge = find_bridge(&info, ctx->argv[1], true);
1372 ds_put_format(&ctx->output, "%d\n", bridge->vlan);
1377 cmd_br_to_parent(struct vsctl_context *ctx)
1379 struct vsctl_bridge *bridge;
1380 struct vsctl_info info;
1382 get_info(ctx->ovs, &info);
1383 bridge = find_bridge(&info, ctx->argv[1], true);
1384 if (bridge->parent) {
1385 bridge = bridge->parent;
1387 ds_put_format(&ctx->output, "%s\n", bridge->name);
1392 cmd_list_ifaces(struct vsctl_context *ctx)
1394 struct vsctl_bridge *br;
1395 struct shash_node *node;
1396 struct vsctl_info info;
1399 get_info(ctx->ovs, &info);
1400 br = find_bridge(&info, ctx->argv[1], true);
1403 SHASH_FOR_EACH (node, &info.ifaces) {
1404 struct vsctl_iface *iface = node->data;
1406 if (strcmp(iface->iface_cfg->name, br->name)
1407 && br == iface->port->bridge) {
1408 svec_add(&ifaces, iface->iface_cfg->name);
1411 output_sorted(&ifaces, &ctx->output);
1412 svec_destroy(&ifaces);
1418 cmd_iface_to_br(struct vsctl_context *ctx)
1420 struct vsctl_iface *iface;
1421 struct vsctl_info info;
1423 get_info(ctx->ovs, &info);
1424 iface = find_iface(&info, ctx->argv[1], true);
1425 ds_put_format(&ctx->output, "%s\n", iface->port->bridge->name);
1430 cmd_get_controller(struct vsctl_context *ctx)
1432 struct vsctl_info info;
1434 get_info(ctx->ovs, &info);
1436 if (ctx->argc == 1) {
1437 /* Return the controller from the "Open_vSwitch" table */
1439 ds_put_format(&ctx->output, "%s\n", info.ctrl->target);
1442 /* Return the controller for a particular bridge. */
1443 struct vsctl_bridge *br = find_bridge(&info, ctx->argv[1], true);
1445 /* If no controller is explicitly defined for the requested
1446 * bridge, fallback to the "Open_vSwitch" table's controller. */
1448 ds_put_format(&ctx->output, "%s\n", br->ctrl->target);
1449 } else if (info.ctrl) {
1450 ds_put_format(&ctx->output, "%s\n", info.ctrl->target);
1458 cmd_del_controller(struct vsctl_context *ctx)
1460 struct vsctl_info info;
1462 get_info(ctx->ovs, &info);
1464 if (ctx->argc == 1) {
1466 ovsrec_controller_delete(info.ctrl);
1467 ovsrec_open_vswitch_set_controller(ctx->ovs, NULL);
1470 struct vsctl_bridge *br = find_real_bridge(&info, ctx->argv[1], true);
1473 ovsrec_controller_delete(br->ctrl);
1474 ovsrec_bridge_set_controller(br->br_cfg, NULL);
1482 cmd_set_controller(struct vsctl_context *ctx)
1484 struct vsctl_info info;
1485 struct ovsrec_controller *ctrl;
1487 get_info(ctx->ovs, &info);
1489 if (ctx->argc == 2) {
1490 /* Set the controller in the "Open_vSwitch" table. */
1492 ovsrec_controller_delete(info.ctrl);
1494 ctrl = ovsrec_controller_insert(ctx->txn);
1495 ovsrec_controller_set_target(ctrl, ctx->argv[1]);
1496 ovsrec_open_vswitch_set_controller(ctx->ovs, ctrl);
1498 /* Set the controller for a particular bridge. */
1499 struct vsctl_bridge *br = find_real_bridge(&info, ctx->argv[1], true);
1502 ovsrec_controller_delete(br->ctrl);
1504 ctrl = ovsrec_controller_insert(ctx->txn);
1505 ovsrec_controller_set_target(ctrl, ctx->argv[2]);
1506 ovsrec_bridge_set_controller(br->br_cfg, ctrl);
1513 cmd_get_fail_mode(struct vsctl_context *ctx)
1515 struct vsctl_info info;
1516 const char *fail_mode = NULL;
1518 get_info(ctx->ovs, &info);
1520 if (ctx->argc == 1) {
1521 /* Return the fail-mode from the "Open_vSwitch" table */
1522 if (info.ctrl && info.ctrl->fail_mode) {
1523 fail_mode = info.ctrl->fail_mode;
1526 /* Return the fail-mode for a particular bridge. */
1527 struct vsctl_bridge *br = find_bridge(&info, ctx->argv[1], true);
1529 /* If no controller or fail-mode is explicitly defined for the
1530 * requested bridge, fallback to the "Open_vSwitch" table's
1532 if (br->ctrl && br->ctrl->fail_mode) {
1533 fail_mode = br->ctrl->fail_mode;
1534 } else if (info.ctrl && info.ctrl->fail_mode) {
1535 fail_mode = info.ctrl->fail_mode;
1539 if (fail_mode && strlen(fail_mode)) {
1540 ds_put_format(&ctx->output, "%s\n", fail_mode);
1547 cmd_del_fail_mode(struct vsctl_context *ctx)
1549 struct vsctl_info info;
1551 get_info(ctx->ovs, &info);
1553 if (ctx->argc == 1) {
1554 if (info.ctrl && info.ctrl->fail_mode) {
1555 ovsrec_controller_set_fail_mode(info.ctrl, NULL);
1558 struct vsctl_bridge *br = find_real_bridge(&info, ctx->argv[1], true);
1560 if (br->ctrl && br->ctrl->fail_mode) {
1561 ovsrec_controller_set_fail_mode(br->ctrl, NULL);
1569 cmd_set_fail_mode(struct vsctl_context *ctx)
1571 struct vsctl_info info;
1572 const char *fail_mode;
1574 get_info(ctx->ovs, &info);
1576 fail_mode = (ctx->argc == 2) ? ctx->argv[1] : ctx->argv[2];
1578 if (strcmp(fail_mode, "standalone") && strcmp(fail_mode, "secure")) {
1579 vsctl_fatal("fail-mode must be \"standalone\" or \"secure\"");
1582 if (ctx->argc == 2) {
1583 /* Set the fail-mode in the "Open_vSwitch" table. */
1585 vsctl_fatal("no controller declared");
1587 ovsrec_controller_set_fail_mode(info.ctrl, fail_mode);
1589 struct vsctl_bridge *br = find_real_bridge(&info, ctx->argv[1], true);
1592 vsctl_fatal("no controller declared for %s", br->name);
1594 ovsrec_controller_set_fail_mode(br->ctrl, fail_mode);
1601 cmd_get_ssl(struct vsctl_context *ctx)
1603 struct ovsrec_ssl *ssl = ctx->ovs->ssl;
1606 ds_put_format(&ctx->output, "Private key: %s\n", ssl->private_key);
1607 ds_put_format(&ctx->output, "Certificate: %s\n", ssl->certificate);
1608 ds_put_format(&ctx->output, "CA Certificate: %s\n", ssl->ca_cert);
1609 ds_put_format(&ctx->output, "Bootstrap: %s\n",
1610 ssl->bootstrap_ca_cert ? "true" : "false");
1615 cmd_del_ssl(struct vsctl_context *ctx)
1617 struct ovsrec_ssl *ssl = ctx->ovs->ssl;
1620 ovsrec_ssl_delete(ssl);
1621 ovsrec_open_vswitch_set_ssl(ctx->ovs, NULL);
1626 cmd_set_ssl(struct vsctl_context *ctx)
1628 bool bootstrap = shash_find(&ctx->options, "--bootstrap");
1629 struct ovsrec_ssl *ssl = ctx->ovs->ssl;
1632 ovsrec_ssl_delete(ssl);
1634 ssl = ovsrec_ssl_insert(ctx->txn);
1636 ovsrec_ssl_set_private_key(ssl, ctx->argv[1]);
1637 ovsrec_ssl_set_certificate(ssl, ctx->argv[2]);
1638 ovsrec_ssl_set_ca_cert(ssl, ctx->argv[3]);
1640 ovsrec_ssl_set_bootstrap_ca_cert(ssl, bootstrap);
1642 ovsrec_open_vswitch_set_ssl(ctx->ovs, ssl);
1645 /* Parameter commands. */
1647 struct vsctl_row_id {
1648 const struct ovsdb_idl_table_class *table;
1649 const struct ovsdb_idl_column *name_column;
1650 const struct ovsdb_idl_column *uuid_column;
1653 struct vsctl_table_class {
1654 struct ovsdb_idl_table_class *class;
1655 struct vsctl_row_id row_ids[2];
1658 static const struct vsctl_table_class tables[] = {
1659 {&ovsrec_table_bridge,
1660 {{&ovsrec_table_bridge, &ovsrec_bridge_col_name, NULL},
1661 {NULL, NULL, NULL}}},
1663 {&ovsrec_table_controller,
1664 {{&ovsrec_table_bridge,
1665 &ovsrec_bridge_col_name,
1666 &ovsrec_bridge_col_controller},
1667 {&ovsrec_table_open_vswitch,
1669 &ovsrec_open_vswitch_col_controller}}},
1671 {&ovsrec_table_interface,
1672 {{&ovsrec_table_interface, &ovsrec_interface_col_name, NULL},
1673 {NULL, NULL, NULL}}},
1675 {&ovsrec_table_mirror,
1676 {{&ovsrec_table_mirror, &ovsrec_mirror_col_name, NULL},
1677 {NULL, NULL, NULL}}},
1679 {&ovsrec_table_netflow,
1680 {{&ovsrec_table_bridge,
1681 &ovsrec_bridge_col_name,
1682 &ovsrec_bridge_col_netflow},
1683 {NULL, NULL, NULL}}},
1685 {&ovsrec_table_open_vswitch,
1686 {{&ovsrec_table_open_vswitch, NULL, NULL},
1687 {NULL, NULL, NULL}}},
1689 {&ovsrec_table_port,
1690 {{&ovsrec_table_port, &ovsrec_port_col_name, NULL},
1691 {NULL, NULL, NULL}}},
1694 {{&ovsrec_table_open_vswitch, NULL, &ovsrec_open_vswitch_col_ssl}}},
1696 {NULL, {{NULL, NULL, NULL}, {NULL, NULL, NULL}}}
1700 die_if_error(char *error)
1703 vsctl_fatal("%s", error);
1708 to_lower_and_underscores(unsigned c)
1710 return c == '-' ? '_' : tolower(c);
1714 score_partial_match(const char *name, const char *s)
1718 if (!strcmp(name, s)) {
1721 for (score = 0; ; score++, name++, s++) {
1722 if (to_lower_and_underscores(*name) != to_lower_and_underscores(*s)) {
1724 } else if (*name == '\0') {
1725 return UINT_MAX - 1;
1728 return *s == '\0' ? score : 0;
1731 static const struct vsctl_table_class *
1732 get_table(const char *table_name)
1734 const struct vsctl_table_class *table;
1735 const struct vsctl_table_class *best_match = NULL;
1736 unsigned int best_score = 0;
1738 for (table = tables; table->class; table++) {
1739 unsigned int score = score_partial_match(table->class->name,
1741 if (score > best_score) {
1744 } else if (score == best_score) {
1750 } else if (best_score) {
1751 vsctl_fatal("multiple table names match \"%s\"", table_name);
1753 vsctl_fatal("unknown table \"%s\"", table_name);
1757 static const struct ovsdb_idl_row *
1758 get_row_by_id(struct vsctl_context *ctx, const struct vsctl_table_class *table,
1759 const struct vsctl_row_id *id, const char *record_id)
1761 const struct ovsdb_idl_row *referrer, *final;
1767 if (!id->name_column) {
1768 if (strcmp(record_id, ".")) {
1771 referrer = ovsdb_idl_first_row(ctx->idl, id->table);
1772 if (!referrer || ovsdb_idl_next_row(referrer)) {
1776 const struct ovsdb_idl_row *row;
1777 unsigned int best_score = 0;
1779 /* It might make sense to relax this assertion. */
1780 assert(id->name_column->type.key.type == OVSDB_TYPE_STRING);
1783 for (row = ovsdb_idl_first_row(ctx->idl, id->table);
1784 row != NULL && best_score != UINT_MAX;
1785 row = ovsdb_idl_next_row(row))
1787 struct ovsdb_datum name;
1789 ovsdb_idl_txn_read(row, id->name_column, &name);
1791 unsigned int score = score_partial_match(name.keys[0].string,
1793 if (score > best_score) {
1796 } else if (score == best_score) {
1800 ovsdb_datum_destroy(&name, &id->name_column->type);
1802 if (best_score && !referrer) {
1803 vsctl_fatal("multiple rows in %s match \"%s\"",
1804 table->class->name, record_id);
1812 if (id->uuid_column) {
1813 struct ovsdb_datum uuid;
1815 assert(id->uuid_column->type.key.type == OVSDB_TYPE_UUID);
1816 assert(id->uuid_column->type.value.type == OVSDB_TYPE_VOID);
1818 ovsdb_idl_txn_read(referrer, id->uuid_column, &uuid);
1820 final = ovsdb_idl_get_row_for_uuid(ctx->idl, table->class,
1821 &uuid.keys[0].uuid);
1823 ovsdb_datum_destroy(&uuid, &id->uuid_column->type);
1831 static const struct ovsdb_idl_row *
1832 get_row(struct vsctl_context *ctx,
1833 const struct vsctl_table_class *table, const char *record_id)
1835 const struct ovsdb_idl_row *row;
1838 if (uuid_from_string(&uuid, record_id)) {
1839 row = ovsdb_idl_get_row_for_uuid(ctx->idl, table->class, &uuid);
1843 for (i = 0; i < ARRAY_SIZE(table->row_ids); i++) {
1844 row = get_row_by_id(ctx, table, &table->row_ids[i], record_id);
1853 static const struct ovsdb_idl_row *
1854 must_get_row(struct vsctl_context *ctx,
1855 const struct vsctl_table_class *table, const char *record_id)
1857 const struct ovsdb_idl_row *row = get_row(ctx, table, record_id);
1859 vsctl_fatal("no row \"%s\" in table %s",
1860 record_id, table->class->name);
1866 get_column(const struct vsctl_table_class *table, const char *column_name,
1867 const struct ovsdb_idl_column **columnp)
1869 const struct ovsdb_idl_column *best_match = NULL;
1870 unsigned int best_score = 0;
1873 for (i = 0; i < table->class->n_columns; i++) {
1874 const struct ovsdb_idl_column *column = &table->class->columns[i];
1875 unsigned int score = score_partial_match(column->name, column_name);
1876 if (score > best_score) {
1877 best_match = column;
1879 } else if (score == best_score) {
1884 *columnp = best_match;
1887 } else if (best_score) {
1888 return xasprintf("%s contains more than one column whose name "
1889 "matches \"%s\"", table->class->name, column_name);
1891 return xasprintf("%s does not contain a column whose name matches "
1892 "\"%s\"", table->class->name, column_name);
1896 static char * WARN_UNUSED_RESULT
1897 parse_column_key_value(const char *arg, const struct vsctl_table_class *table,
1898 const struct ovsdb_idl_column **columnp,
1899 char **keyp, char **valuep)
1901 const char *p = arg;
1904 assert(columnp || keyp);
1912 /* Parse column name. */
1916 error = ovsdb_token_parse(&p, &column_name);
1920 if (column_name[0] == '\0') {
1922 error = xasprintf("%s: missing column name", arg);
1925 error = get_column(table, column_name, columnp);
1932 /* Parse key string. */
1933 if (*p == ':' || !columnp) {
1937 error = xasprintf("%s: key not accepted here", arg);
1940 error = ovsdb_token_parse(&p, keyp);
1948 /* Parse value string. */
1951 error = xasprintf("%s: value not accepted here", arg);
1954 *valuep = xstrdup(p + 1);
1960 error = xasprintf("%s: trailing garbage \"%s\" in argument",
1983 cmd_get(struct vsctl_context *ctx)
1985 bool if_exists = shash_find(&ctx->options, "--if-exists");
1986 const char *table_name = ctx->argv[1];
1987 const char *record_id = ctx->argv[2];
1988 const struct vsctl_table_class *table;
1989 const struct ovsdb_idl_row *row;
1990 struct ds *out = &ctx->output;
1993 table = get_table(table_name);
1994 row = must_get_row(ctx, table, record_id);
1995 for (i = 3; i < ctx->argc; i++) {
1996 const struct ovsdb_idl_column *column;
1997 struct ovsdb_datum datum;
2000 die_if_error(parse_column_key_value(ctx->argv[i], table,
2001 &column, &key_string, NULL));
2003 ovsdb_idl_txn_read(row, column, &datum);
2005 union ovsdb_atom key;
2008 if (column->type.value.type == OVSDB_TYPE_VOID) {
2009 vsctl_fatal("cannot specify key to get for non-map column %s",
2013 die_if_error(ovsdb_atom_from_string(&key,
2017 idx = ovsdb_datum_find_key(&datum, &key,
2018 column->type.key.type);
2019 if (idx == UINT_MAX) {
2021 vsctl_fatal("no key \"%s\" in %s record \"%s\" column %s",
2022 key_string, table->class->name, record_id,
2026 ovsdb_atom_to_string(&datum.values[idx],
2027 column->type.value.type, out);
2029 ovsdb_atom_destroy(&key, column->type.key.type);
2031 ovsdb_datum_to_string(&datum, &column->type, out);
2033 ds_put_char(out, '\n');
2034 ovsdb_datum_destroy(&datum, &column->type);
2041 list_record(const struct vsctl_table_class *table,
2042 const struct ovsdb_idl_row *row, struct ds *out)
2046 ds_put_format(out, "%-20s: "UUID_FMT"\n", "_uuid",
2047 UUID_ARGS(&row->uuid));
2048 for (i = 0; i < table->class->n_columns; i++) {
2049 const struct ovsdb_idl_column *column = &table->class->columns[i];
2050 struct ovsdb_datum datum;
2052 ovsdb_idl_txn_read(row, column, &datum);
2054 ds_put_format(out, "%-20s: ", column->name);
2055 ovsdb_datum_to_string(&datum, &column->type, out);
2056 ds_put_char(out, '\n');
2058 ovsdb_datum_destroy(&datum, &column->type);
2063 cmd_list(struct vsctl_context *ctx)
2065 const char *table_name = ctx->argv[1];
2066 const struct vsctl_table_class *table;
2067 struct ds *out = &ctx->output;
2070 table = get_table(table_name);
2071 if (ctx->argc > 2) {
2072 for (i = 2; i < ctx->argc; i++) {
2074 ds_put_char(out, '\n');
2076 list_record(table, must_get_row(ctx, table, ctx->argv[i]), out);
2079 const struct ovsdb_idl_row *row;
2082 for (row = ovsdb_idl_first_row(ctx->idl, table->class), first = true;
2084 row = ovsdb_idl_next_row(row), first = false) {
2086 ds_put_char(out, '\n');
2088 list_record(table, row, out);
2094 set_column(const struct vsctl_table_class *table,
2095 const struct ovsdb_idl_row *row, const char *arg)
2097 const struct ovsdb_idl_column *column;
2098 char *key_string, *value_string;
2101 error = parse_column_key_value(arg, table, &column, &key_string,
2103 die_if_error(error);
2104 if (!value_string) {
2105 vsctl_fatal("%s: missing value", arg);
2109 union ovsdb_atom key, value;
2110 struct ovsdb_datum old, new;
2112 if (column->type.value.type == OVSDB_TYPE_VOID) {
2113 vsctl_fatal("cannot specify key to set for non-map column %s",
2117 die_if_error(ovsdb_atom_from_string(&key, &column->type.key,
2119 die_if_error(ovsdb_atom_from_string(&value, &column->type.value,
2122 ovsdb_datum_init_empty(&new);
2123 ovsdb_datum_add_unsafe(&new, &key, &value, &column->type);
2125 ovsdb_atom_destroy(&key, column->type.key.type);
2126 ovsdb_atom_destroy(&value, column->type.value.type);
2128 ovsdb_idl_txn_read(row, column, &old);
2129 ovsdb_datum_union(&old, &new, &column->type, true);
2130 ovsdb_idl_txn_write(row, column, &old);
2132 ovsdb_datum_destroy(&new, &column->type);
2134 struct ovsdb_datum datum;
2136 die_if_error(ovsdb_datum_from_string(&datum, &column->type,
2138 ovsdb_idl_txn_write(row, column, &datum);
2146 cmd_set(struct vsctl_context *ctx)
2148 const char *table_name = ctx->argv[1];
2149 const char *record_id = ctx->argv[2];
2150 const struct vsctl_table_class *table;
2151 const struct ovsdb_idl_row *row;
2154 table = get_table(table_name);
2155 row = must_get_row(ctx, table, record_id);
2156 for (i = 3; i < ctx->argc; i++) {
2157 set_column(table, row, ctx->argv[i]);
2162 cmd_add(struct vsctl_context *ctx)
2164 const char *table_name = ctx->argv[1];
2165 const char *record_id = ctx->argv[2];
2166 const char *column_name = ctx->argv[3];
2167 const struct vsctl_table_class *table;
2168 const struct ovsdb_idl_column *column;
2169 const struct ovsdb_idl_row *row;
2170 const struct ovsdb_type *type;
2171 struct ovsdb_datum old;
2174 table = get_table(table_name);
2175 row = must_get_row(ctx, table, record_id);
2176 die_if_error(get_column(table, column_name, &column));
2178 type = &column->type;
2179 ovsdb_idl_txn_read(row, column, &old);
2180 for (i = 4; i < ctx->argc; i++) {
2181 struct ovsdb_type add_type;
2182 struct ovsdb_datum add;
2186 add_type.n_max = UINT_MAX;
2187 die_if_error(ovsdb_datum_from_string(&add, &add_type, ctx->argv[i]));
2188 ovsdb_datum_union(&old, &add, type, false);
2189 ovsdb_datum_destroy(&add, type);
2191 if (old.n > type->n_max) {
2192 vsctl_fatal("\"add\" operation would put %u %s in column %s of "
2193 "table %s but the maximum number is %u",
2195 type->value.type == OVSDB_TYPE_VOID ? "values" : "pairs",
2196 column->name, table->class->name, type->n_max);
2198 ovsdb_idl_txn_write(row, column, &old);
2202 cmd_remove(struct vsctl_context *ctx)
2204 const char *table_name = ctx->argv[1];
2205 const char *record_id = ctx->argv[2];
2206 const char *column_name = ctx->argv[3];
2207 const struct vsctl_table_class *table;
2208 const struct ovsdb_idl_column *column;
2209 const struct ovsdb_idl_row *row;
2210 const struct ovsdb_type *type;
2211 struct ovsdb_datum old;
2214 table = get_table(table_name);
2215 row = must_get_row(ctx, table, record_id);
2216 die_if_error(get_column(table, column_name, &column));
2218 type = &column->type;
2219 ovsdb_idl_txn_read(row, column, &old);
2220 for (i = 4; i < ctx->argc; i++) {
2221 struct ovsdb_type rm_type;
2222 struct ovsdb_datum rm;
2227 rm_type.n_max = UINT_MAX;
2228 error = ovsdb_datum_from_string(&rm, &rm_type, ctx->argv[i]);
2229 if (error && ovsdb_type_is_map(&rm_type)) {
2231 rm_type.value.type = OVSDB_TYPE_VOID;
2232 die_if_error(ovsdb_datum_from_string(&rm, &rm_type, ctx->argv[i]));
2234 ovsdb_datum_subtract(&old, type, &rm, &rm_type);
2235 ovsdb_datum_destroy(&rm, &rm_type);
2237 if (old.n < type->n_min) {
2238 vsctl_fatal("\"remove\" operation would put %u %s in column %s of "
2239 "table %s but the minimun number is %u",
2241 type->value.type == OVSDB_TYPE_VOID ? "values" : "pairs",
2242 column->name, table->class->name, type->n_min);
2244 ovsdb_idl_txn_write(row, column, &old);
2248 cmd_clear(struct vsctl_context *ctx)
2250 const char *table_name = ctx->argv[1];
2251 const char *record_id = ctx->argv[2];
2252 const struct vsctl_table_class *table;
2253 const struct ovsdb_idl_row *row;
2256 table = get_table(table_name);
2257 row = must_get_row(ctx, table, record_id);
2258 for (i = 3; i < ctx->argc; i++) {
2259 const struct ovsdb_idl_column *column;
2260 const struct ovsdb_type *type;
2261 struct ovsdb_datum datum;
2263 die_if_error(get_column(table, ctx->argv[i], &column));
2265 type = &column->type;
2266 if (type->n_min > 0) {
2267 vsctl_fatal("\"clear\" operation cannot be applied to column %s "
2268 "of table %s, which is not allowed to be empty",
2269 column->name, table->class->name);
2272 ovsdb_datum_init_empty(&datum);
2273 ovsdb_idl_txn_write(row, column, &datum);
2278 cmd_create(struct vsctl_context *ctx)
2280 const char *table_name = ctx->argv[1];
2281 const struct vsctl_table_class *table;
2282 const struct ovsdb_idl_row *row;
2285 table = get_table(table_name);
2286 row = ovsdb_idl_txn_insert(ctx->txn, table->class);
2287 for (i = 2; i < ctx->argc; i++) {
2288 set_column(table, row, ctx->argv[i]);
2290 ds_put_format(&ctx->output, UUID_FMT, UUID_ARGS(&row->uuid));
2293 /* This function may be used as the 'postprocess' function for commands that
2294 * insert new rows into the database. It expects that the command's 'run'
2295 * function prints the UUID reported by ovsdb_idl_txn_insert() as the command's
2296 * sole output. It replaces that output by the row's permanent UUID assigned
2297 * by the database server and appends a new-line.
2299 * Currently we use this only for "create", because the higher-level commands
2300 * are supposed to be independent of the actual structure of the vswitch
2303 post_create(struct vsctl_context *ctx)
2305 const struct uuid *real;
2308 uuid_from_string(&dummy, ds_cstr(&ctx->output));
2309 real = ovsdb_idl_txn_get_insert_uuid(ctx->txn, &dummy);
2311 ds_clear(&ctx->output);
2312 ds_put_format(&ctx->output, UUID_FMT, UUID_ARGS(real));
2314 ds_put_char(&ctx->output, '\n');
2318 cmd_destroy(struct vsctl_context *ctx)
2320 bool must_exist = !shash_find(&ctx->options, "--if-exists");
2321 const char *table_name = ctx->argv[1];
2322 const struct vsctl_table_class *table;
2325 table = get_table(table_name);
2326 for (i = 2; i < ctx->argc; i++) {
2327 const struct ovsdb_idl_row *row;
2329 row = (must_exist ? must_get_row : get_row)(ctx, table, ctx->argv[i]);
2331 ovsdb_idl_txn_delete(row);
2336 static struct json *
2337 where_uuid_equals(const struct uuid *uuid)
2340 json_array_create_1(
2341 json_array_create_3(
2342 json_string_create("_uuid"),
2343 json_string_create("=="),
2344 json_array_create_2(
2345 json_string_create("uuid"),
2346 json_string_create_nocopy(
2347 xasprintf(UUID_FMT, UUID_ARGS(uuid))))));
2351 vsctl_context_init(struct vsctl_context *ctx, struct vsctl_command *command,
2352 struct ovsdb_idl *idl, struct ovsdb_idl_txn *txn,
2353 const struct ovsrec_open_vswitch *ovs)
2355 ctx->argc = command->argc;
2356 ctx->argv = command->argv;
2357 ctx->options = command->options;
2359 ds_swap(&ctx->output, &command->output);
2367 vsctl_context_done(struct vsctl_context *ctx, struct vsctl_command *command)
2369 ds_swap(&ctx->output, &command->output);
2373 do_vsctl(const char *args, struct vsctl_command *commands, size_t n_commands,
2374 struct ovsdb_idl *idl)
2376 struct ovsdb_idl_txn *txn;
2377 const struct ovsrec_open_vswitch *ovs;
2378 enum ovsdb_idl_txn_status status;
2379 struct vsctl_command *c;
2380 int64_t next_cfg = 0;
2383 txn = the_idl_txn = ovsdb_idl_txn_create(idl);
2385 ovsdb_idl_txn_set_dry_run(txn);
2388 ovsdb_idl_txn_add_comment(txn, "ovs-vsctl: %s", args);
2390 ovs = ovsrec_open_vswitch_first(idl);
2392 /* XXX add verification that table is empty */
2393 ovs = ovsrec_open_vswitch_insert(txn);
2396 if (wait_for_reload) {
2397 struct json *where = where_uuid_equals(&ovs->header_.uuid);
2398 ovsdb_idl_txn_increment(txn, "Open_vSwitch", "next_cfg", where);
2399 json_destroy(where);
2402 for (c = commands; c < &commands[n_commands]; c++) {
2403 struct vsctl_context ctx;
2405 ds_init(&c->output);
2406 vsctl_context_init(&ctx, c, idl, txn, ovs);
2407 (c->syntax->run)(&ctx);
2408 vsctl_context_done(&ctx, c);
2411 status = ovsdb_idl_txn_commit_block(txn);
2412 if (wait_for_reload && status == TXN_SUCCESS) {
2413 next_cfg = ovsdb_idl_txn_get_increment_new_value(txn);
2415 for (c = commands; c < &commands[n_commands]; c++) {
2416 if (c->syntax->postprocess) {
2417 struct vsctl_context ctx;
2419 vsctl_context_init(&ctx, c, idl, txn, ovs);
2420 (c->syntax->postprocess)(&ctx);
2421 vsctl_context_done(&ctx, c);
2424 error = xstrdup(ovsdb_idl_txn_get_error(txn));
2425 ovsdb_idl_txn_destroy(txn);
2429 case TXN_INCOMPLETE:
2433 /* Should not happen--we never call ovsdb_idl_txn_abort(). */
2434 vsctl_fatal("transaction aborted");
2441 for (c = commands; c < &commands[n_commands]; c++) {
2442 ds_destroy(&c->output);
2448 vsctl_fatal("transaction error: %s", error);
2455 for (c = commands; c < &commands[n_commands]; c++) {
2456 struct ds *ds = &c->output;
2461 for (j = 0; j < ds->length; j++) {
2462 int c = ds->string[j];
2465 fputs("\\n", stdout);
2469 fputs("\\\\", stdout);
2478 fputs(ds_cstr(ds), stdout);
2480 ds_destroy(&c->output);
2481 shash_destroy(&c->options);
2485 if (wait_for_reload && status != TXN_UNCHANGED) {
2487 const struct ovsrec_open_vswitch *ovs;
2490 OVSREC_OPEN_VSWITCH_FOR_EACH (ovs, idl) {
2491 if (ovs->cur_cfg >= next_cfg) {
2495 ovsdb_idl_wait(idl);
2500 ovsdb_idl_destroy(idl);
2505 static const struct vsctl_command_syntax all_commands[] = {
2506 /* Open vSwitch commands. */
2507 {"init", 0, 0, cmd_init, NULL, ""},
2509 /* Bridge commands. */
2510 {"add-br", 1, 3, cmd_add_br, NULL, "--may-exist"},
2511 {"del-br", 1, 1, cmd_del_br, NULL, "--if-exists"},
2512 {"list-br", 0, 0, cmd_list_br, NULL, ""},
2513 {"br-exists", 1, 1, cmd_br_exists, NULL, ""},
2514 {"br-to-vlan", 1, 1, cmd_br_to_vlan, NULL, ""},
2515 {"br-to-parent", 1, 1, cmd_br_to_parent, NULL, ""},
2516 {"br-set-external-id", 2, 3, cmd_br_set_external_id, NULL, ""},
2517 {"br-get-external-id", 1, 2, cmd_br_get_external_id, NULL, ""},
2519 /* Port commands. */
2520 {"list-ports", 1, 1, cmd_list_ports, NULL, ""},
2521 {"add-port", 2, INT_MAX, cmd_add_port, NULL, "--may-exist"},
2522 {"add-bond", 4, INT_MAX, cmd_add_bond, NULL, "--may-exist,--fake-iface"},
2523 {"del-port", 1, 2, cmd_del_port, NULL, "--if-exists,--with-iface"},
2524 {"port-to-br", 1, 1, cmd_port_to_br, NULL, ""},
2526 /* Interface commands. */
2527 {"list-ifaces", 1, 1, cmd_list_ifaces, NULL, ""},
2528 {"iface-to-br", 1, 1, cmd_iface_to_br, NULL, ""},
2530 /* Controller commands. */
2531 {"get-controller", 0, 1, cmd_get_controller, NULL, ""},
2532 {"del-controller", 0, 1, cmd_del_controller, NULL, ""},
2533 {"set-controller", 1, 2, cmd_set_controller, NULL, ""},
2534 {"get-fail-mode", 0, 1, cmd_get_fail_mode, NULL, ""},
2535 {"del-fail-mode", 0, 1, cmd_del_fail_mode, NULL, ""},
2536 {"set-fail-mode", 1, 2, cmd_set_fail_mode, NULL, ""},
2539 {"get-ssl", 0, 0, cmd_get_ssl, NULL, ""},
2540 {"del-ssl", 0, 0, cmd_del_ssl, NULL, ""},
2541 {"set-ssl", 3, 3, cmd_set_ssl, NULL, "--bootstrap"},
2543 /* Parameter commands. */
2544 {"get", 3, INT_MAX, cmd_get, NULL, "--if-exists"},
2545 {"list", 1, INT_MAX, cmd_list, NULL, ""},
2546 {"set", 3, INT_MAX, cmd_set, NULL, ""},
2547 {"add", 4, INT_MAX, cmd_add, NULL, ""},
2548 {"remove", 4, INT_MAX, cmd_remove, NULL, ""},
2549 {"clear", 3, INT_MAX, cmd_clear, NULL, ""},
2550 {"create", 2, INT_MAX, cmd_create, post_create, ""},
2551 {"destroy", 1, INT_MAX, cmd_destroy, NULL, "--if-exists"},
2553 {NULL, 0, 0, NULL, NULL, NULL},