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"
39 #include "stream-ssl.h"
41 #include "vswitchd/vswitch-idl.h"
46 #define THIS_MODULE VLM_vsctl
48 /* vsctl_fatal() also logs the error, so it is preferred in this file. */
49 #define ovs_fatal please_use_vsctl_fatal_instead_of_ovs_fatal
53 typedef void vsctl_handler_func(struct vsctl_context *);
55 struct vsctl_command_syntax {
59 vsctl_handler_func *run;
60 vsctl_handler_func *postprocess;
64 struct vsctl_command {
65 /* Data that remains constant after initialization. */
66 const struct vsctl_command_syntax *syntax;
71 /* Data modified by commands. */
75 /* --db: The database server to contact. */
76 static const char *db;
78 /* --oneline: Write each command's output as a single line? */
81 /* --dry-run: Do not commit any changes. */
84 /* --no-wait: Wait for ovs-vswitchd to reload its configuration? */
85 static bool wait_for_reload = true;
87 /* --timeout: Time to wait for a connection to 'db'. */
88 static int timeout = 5;
90 /* All supported commands. */
91 static const struct vsctl_command_syntax all_commands[];
93 /* The IDL we're using and the current transaction, if any.
94 * This is for use by vsctl_exit() only, to allow it to clean up.
95 * Other code should use its context arguments. */
96 static struct ovsdb_idl *the_idl;
97 static struct ovsdb_idl_txn *the_idl_txn;
99 static void vsctl_exit(int status) NO_RETURN;
100 static void vsctl_fatal(const char *, ...) PRINTF_FORMAT(1, 2) NO_RETURN;
101 static char *default_db(void);
102 static void usage(void) NO_RETURN;
103 static void parse_options(int argc, char *argv[]);
105 static struct vsctl_command *parse_commands(int argc, char *argv[],
106 size_t *n_commandsp);
107 static void parse_command(int argc, char *argv[], struct vsctl_command *);
108 static void do_vsctl(const char *args,
109 struct vsctl_command *, size_t n_commands,
112 static const struct vsctl_table_class *get_table(const char *table_name);
113 static void set_column(const struct vsctl_table_class *,
114 const struct ovsdb_idl_row *, const char *arg);
118 main(int argc, char *argv[])
120 struct ovsdb_idl *idl;
122 struct vsctl_command *commands;
127 set_program_name(argv[0]);
128 signal(SIGPIPE, SIG_IGN);
131 vlog_set_levels(VLM_ANY_MODULE, VLF_CONSOLE, VLL_WARN);
132 vlog_set_levels(VLM_reconnect, VLF_ANY_FACILITY, VLL_WARN);
135 /* Log our arguments. This is often valuable for debugging systems. */
136 args = process_escape_args(argv);
137 VLOG_INFO("Called as %s", args);
139 /* Parse command line. */
140 parse_options(argc, argv);
141 commands = parse_commands(argc - optind, argv + optind, &n_commands);
147 /* Now execute the commands. */
148 idl = the_idl = ovsdb_idl_create(db, &ovsrec_idl_class);
149 seqno = ovsdb_idl_get_seqno(idl);
152 unsigned int new_seqno;
155 new_seqno = ovsdb_idl_get_seqno(idl);
156 if (new_seqno != seqno) {
158 vsctl_fatal("too many database inconsistency failures");
160 do_vsctl(args, commands, n_commands, idl);
170 parse_options(int argc, char *argv[])
173 OPT_DB = UCHAR_MAX + 1,
181 static struct option long_options[] = {
182 {"db", required_argument, 0, OPT_DB},
183 {"no-syslog", no_argument, 0, OPT_NO_SYSLOG},
184 {"no-wait", no_argument, 0, OPT_NO_WAIT},
185 {"dry-run", no_argument, 0, OPT_DRY_RUN},
186 {"oneline", no_argument, 0, OPT_ONELINE},
187 {"timeout", required_argument, 0, 't'},
188 {"help", no_argument, 0, 'h'},
189 {"version", no_argument, 0, 'V'},
192 STREAM_SSL_LONG_OPTIONS
193 {"peer-ca-cert", required_argument, 0, OPT_PEER_CA_CERT},
202 c = getopt_long(argc, argv, "+v::hVt:", long_options, NULL);
217 vlog_set_levels(VLM_vsctl, VLF_SYSLOG, VLL_WARN);
221 wait_for_reload = false;
232 OVS_PRINT_VERSION(0, 0);
236 timeout = strtoul(optarg, NULL, 10);
238 vsctl_fatal("value %s on -t or --timeout is invalid",
246 STREAM_SSL_OPTION_HANDLERS
248 case OPT_PEER_CA_CERT:
249 stream_ssl_set_peer_ca_cert_file(optarg);
266 static struct vsctl_command *
267 parse_commands(int argc, char *argv[], size_t *n_commandsp)
269 struct vsctl_command *commands;
270 size_t n_commands, allocated_commands;
274 n_commands = allocated_commands = 0;
276 for (start = i = 0; i <= argc; i++) {
277 if (i == argc || !strcmp(argv[i], "--")) {
279 if (n_commands >= allocated_commands) {
280 struct vsctl_command *c;
282 commands = x2nrealloc(commands, &allocated_commands,
284 for (c = commands; c < &commands[n_commands]; c++) {
285 shash_moved(&c->options);
288 parse_command(i - start, &argv[start],
289 &commands[n_commands++]);
295 vsctl_fatal("missing command name (use --help for help)");
297 *n_commandsp = n_commands;
302 parse_command(int argc, char *argv[], struct vsctl_command *command)
304 const struct vsctl_command_syntax *p;
307 shash_init(&command->options);
308 for (i = 0; i < argc; i++) {
309 if (argv[i][0] != '-') {
312 if (!shash_add_once(&command->options, argv[i], NULL)) {
313 vsctl_fatal("'%s' option specified multiple times", argv[i]);
317 vsctl_fatal("missing command name");
320 for (p = all_commands; p->name; p++) {
321 if (!strcmp(p->name, argv[i])) {
322 struct shash_node *node;
325 SHASH_FOR_EACH (node, &command->options) {
326 const char *s = strstr(p->options, node->name);
327 int end = s ? s[strlen(node->name)] : EOF;
328 if (end != ',' && end != ' ' && end != '\0') {
329 vsctl_fatal("'%s' command has no '%s' option",
330 argv[i], node->name);
334 n_arg = argc - i - 1;
335 if (n_arg < p->min_args) {
336 vsctl_fatal("'%s' command requires at least %d arguments",
337 p->name, p->min_args);
338 } else if (n_arg > p->max_args) {
341 for (j = i + 1; j < argc; j++) {
342 if (argv[j][0] == '-') {
343 vsctl_fatal("'%s' command takes at most %d arguments "
344 "(note that options must precede command "
345 "names and follow a \"--\" argument)",
346 p->name, p->max_args);
350 vsctl_fatal("'%s' command takes at most %d arguments",
351 p->name, p->max_args);
354 command->argc = n_arg + 1;
355 command->argv = &argv[i];
361 vsctl_fatal("unknown command '%s'; use --help for help", argv[i]);
365 vsctl_fatal(const char *format, ...)
370 va_start(args, format);
371 message = xvasprintf(format, args);
374 vlog_set_levels(VLM_vsctl, VLF_CONSOLE, VLL_EMER);
375 VLOG_ERR("%s", message);
376 ovs_error(0, "%s", message);
377 vsctl_exit(EXIT_FAILURE);
380 /* Frees the current transaction and the underlying IDL and then calls
383 * Freeing the transaction and the IDL is not strictly necessary, but it makes
384 * for a clean memory leak report from valgrind in the normal case. That makes
385 * it easier to notice real memory leaks. */
387 vsctl_exit(int status)
390 ovsdb_idl_txn_abort(the_idl_txn);
391 ovsdb_idl_txn_destroy(the_idl_txn);
393 ovsdb_idl_destroy(the_idl);
401 %s: ovs-vswitchd management utility\n\
402 usage: %s [OPTIONS] COMMAND [ARG...]\n\
405 add-br BRIDGE create a new bridge named BRIDGE\n\
406 add-br BRIDGE PARENT VLAN create new fake BRIDGE in PARENT on VLAN\n\
407 del-br BRIDGE delete BRIDGE and all of its ports\n\
408 list-br print the names of all the bridges\n\
409 br-exists BRIDGE test whether BRIDGE exists\n\
410 br-to-vlan BRIDGE print the VLAN which BRIDGE is on\n\
411 br-to-parent BRIDGE print the parent of BRIDGE\n\
412 br-set-external-id BRIDGE KEY VALUE set KEY on BRIDGE to VALUE\n\
413 br-set-external-id BRIDGE KEY unset KEY on BRIDGE\n\
414 br-get-external-id BRIDGE KEY print value of KEY on BRIDGE\n\
415 br-get-external-id BRIDGE list key-value pairs on BRIDGE\n\
418 list-ports BRIDGE print the names of all the ports on BRIDGE\n\
419 add-port BRIDGE PORT add network device PORT to BRIDGE\n\
420 add-bond BRIDGE PORT IFACE... add bonded port PORT in BRIDGE from IFACES\n\
421 del-port [BRIDGE] PORT delete PORT (which may be bonded) from BRIDGE\n\
422 port-to-br PORT print name of bridge that contains PORT\n\
423 A bond is considered to be a single port.\n\
425 Interface commands (a bond consists of multiple interfaces):\n\
426 list-ifaces BRIDGE print the names of all interfaces on BRIDGE\n\
427 iface-to-br IFACE print name of bridge that contains IFACE\n\
429 Controller commands:\n\
430 get-controller [BRIDGE] print the controller for BRIDGE\n\
431 del-controller [BRIDGE] delete the controller for BRIDGE\n\
432 set-controller [BRIDGE] TARGET set the controller for BRIDGE to TARGET\n\
433 get-fail-mode [BRIDGE] print the fail-mode for BRIDGE\n\
434 del-fail-mode [BRIDGE] delete the fail-mode for BRIDGE\n\
435 set-fail-mode [BRIDGE] MODE set the fail-mode for BRIDGE to MODE\n\
438 get-ssl print the SSL configuration\n\
439 del-ssl delete the SSL configuration\n\
440 set-ssl PRIV-KEY CERT CA-CERT set the SSL configuration\n\
442 Database commands:\n\
443 list TBL [REC] list RECord (or all records) in TBL\n\
444 get TBL REC COL[:KEY] print values of COLumns in RECORD in TBL\n\
445 set TBL REC COL[:KEY]=VALUE set COLumn values in RECord in TBL\n\
446 add TBL REC COL [KEY=]VALUE add (KEY=)VALUE to COLumn in RECord in TBL\n\
447 remove TBL REC COL [KEY=]VALUE remove (KEY=)VALUE from COLumn\n\
448 clear TBL REC COL clear values from COLumn in RECord in TBL\n\
449 create TBL COL[:KEY]=VALUE create and initialize new record\n\
450 destroy TBL REC delete REC from TBL\n\
451 Potentially unsafe database commands require --force option.\n\
454 --db=DATABASE connect to DATABASE\n\
456 --oneline print exactly one line of output per command\n",
457 program_name, program_name, default_db());
461 -h, --help display this help message\n\
462 -V, --version display version information\n");
471 def = xasprintf("unix:%s/db.sock", ovs_rundir);
476 struct vsctl_context {
480 struct shash options;
482 /* Modifiable state. */
484 struct ovsdb_idl *idl;
485 struct ovsdb_idl_txn *txn;
486 const struct ovsrec_open_vswitch *ovs;
489 struct vsctl_bridge {
490 struct ovsrec_bridge *br_cfg;
492 struct ovsrec_controller *ctrl;
493 struct vsctl_bridge *parent;
498 struct ovsrec_port *port_cfg;
499 struct vsctl_bridge *bridge;
503 struct ovsrec_interface *iface_cfg;
504 struct vsctl_port *port;
508 struct shash bridges;
511 struct ovsrec_controller *ctrl;
515 vsctl_context_to_string(const struct vsctl_context *ctx)
517 const struct shash_node *node;
523 SHASH_FOR_EACH (node, &ctx->options) {
524 svec_add(&words, node->name);
526 for (i = 0; i < ctx->argc; i++) {
527 svec_add(&words, ctx->argv[i]);
529 svec_terminate(&words);
531 s = process_escape_args(words.names);
533 svec_destroy(&words);
538 static struct vsctl_bridge *
539 add_bridge(struct vsctl_info *b,
540 struct ovsrec_bridge *br_cfg, const char *name,
541 struct vsctl_bridge *parent, int vlan)
543 struct vsctl_bridge *br = xmalloc(sizeof *br);
545 br->name = xstrdup(name);
548 br->ctrl = parent ? parent->br_cfg->controller : br_cfg->controller;
549 shash_add(&b->bridges, br->name, br);
554 port_is_fake_bridge(const struct ovsrec_port *port_cfg)
556 return (port_cfg->fake_bridge
558 && *port_cfg->tag >= 1 && *port_cfg->tag <= 4095);
561 static struct vsctl_bridge *
562 find_vlan_bridge(struct vsctl_info *info,
563 struct vsctl_bridge *parent, int vlan)
565 struct shash_node *node;
567 SHASH_FOR_EACH (node, &info->bridges) {
568 struct vsctl_bridge *br = node->data;
569 if (br->parent == parent && br->vlan == vlan) {
578 free_info(struct vsctl_info *info)
580 struct shash_node *node;
582 SHASH_FOR_EACH (node, &info->bridges) {
583 struct vsctl_bridge *bridge = node->data;
587 shash_destroy(&info->bridges);
589 SHASH_FOR_EACH (node, &info->ports) {
590 struct vsctl_port *port = node->data;
593 shash_destroy(&info->ports);
595 SHASH_FOR_EACH (node, &info->ifaces) {
596 struct vsctl_iface *iface = node->data;
599 shash_destroy(&info->ifaces);
603 get_info(const struct ovsrec_open_vswitch *ovs, struct vsctl_info *info)
605 struct shash bridges, ports;
608 shash_init(&info->bridges);
609 shash_init(&info->ports);
610 shash_init(&info->ifaces);
612 info->ctrl = ovs->controller;
614 shash_init(&bridges);
616 for (i = 0; i < ovs->n_bridges; i++) {
617 struct ovsrec_bridge *br_cfg = ovs->bridges[i];
618 struct vsctl_bridge *br;
621 if (!shash_add_once(&bridges, br_cfg->name, NULL)) {
622 VLOG_WARN("%s: database contains duplicate bridge name",
626 br = add_bridge(info, br_cfg, br_cfg->name, NULL, 0);
631 for (j = 0; j < br_cfg->n_ports; j++) {
632 struct ovsrec_port *port_cfg = br_cfg->ports[j];
634 if (!shash_add_once(&ports, port_cfg->name, NULL)) {
635 VLOG_WARN("%s: database contains duplicate port name",
640 if (port_is_fake_bridge(port_cfg)
641 && shash_add_once(&bridges, port_cfg->name, NULL)) {
642 add_bridge(info, NULL, port_cfg->name, br, *port_cfg->tag);
646 shash_destroy(&bridges);
647 shash_destroy(&ports);
649 shash_init(&bridges);
651 for (i = 0; i < ovs->n_bridges; i++) {
652 struct ovsrec_bridge *br_cfg = ovs->bridges[i];
653 struct vsctl_bridge *br;
656 if (!shash_add_once(&bridges, br_cfg->name, NULL)) {
659 br = shash_find_data(&info->bridges, br_cfg->name);
660 for (j = 0; j < br_cfg->n_ports; j++) {
661 struct ovsrec_port *port_cfg = br_cfg->ports[j];
662 struct vsctl_port *port;
665 if (!shash_add_once(&ports, port_cfg->name, NULL)) {
669 if (port_is_fake_bridge(port_cfg)
670 && !shash_add_once(&bridges, port_cfg->name, NULL)) {
674 port = xmalloc(sizeof *port);
675 port->port_cfg = port_cfg;
677 && *port_cfg->tag >= 1 && *port_cfg->tag <= 4095) {
678 port->bridge = find_vlan_bridge(info, br, *port_cfg->tag);
685 shash_add(&info->ports, port_cfg->name, port);
687 for (k = 0; k < port_cfg->n_interfaces; k++) {
688 struct ovsrec_interface *iface_cfg = port_cfg->interfaces[k];
689 struct vsctl_iface *iface;
691 if (shash_find(&info->ifaces, iface_cfg->name)) {
692 VLOG_WARN("%s: database contains duplicate interface name",
697 iface = xmalloc(sizeof *iface);
698 iface->iface_cfg = iface_cfg;
700 shash_add(&info->ifaces, iface_cfg->name, iface);
704 shash_destroy(&bridges);
705 shash_destroy(&ports);
709 check_conflicts(struct vsctl_info *info, const char *name,
712 struct vsctl_iface *iface;
713 struct vsctl_port *port;
715 if (shash_find(&info->bridges, name)) {
716 vsctl_fatal("%s because a bridge named %s already exists",
720 port = shash_find_data(&info->ports, name);
722 vsctl_fatal("%s because a port named %s already exists on "
723 "bridge %s", msg, name, port->bridge->name);
726 iface = shash_find_data(&info->ifaces, name);
728 vsctl_fatal("%s because an interface named %s already exists "
729 "on bridge %s", msg, name, iface->port->bridge->name);
735 static struct vsctl_bridge *
736 find_bridge(struct vsctl_info *info, const char *name, bool must_exist)
738 struct vsctl_bridge *br = shash_find_data(&info->bridges, name);
739 if (must_exist && !br) {
740 vsctl_fatal("no bridge named %s", name);
745 static struct vsctl_bridge *
746 find_real_bridge(struct vsctl_info *info, const char *name, bool must_exist)
748 struct vsctl_bridge *br = find_bridge(info, name, must_exist);
749 if (br && br->parent) {
750 vsctl_fatal("%s is a fake bridge", name);
755 static struct vsctl_port *
756 find_port(struct vsctl_info *info, const char *name, bool must_exist)
758 struct vsctl_port *port = shash_find_data(&info->ports, name);
759 if (port && !strcmp(name, port->bridge->name)) {
762 if (must_exist && !port) {
763 vsctl_fatal("no port named %s", name);
768 static struct vsctl_iface *
769 find_iface(struct vsctl_info *info, const char *name, bool must_exist)
771 struct vsctl_iface *iface = shash_find_data(&info->ifaces, name);
772 if (iface && !strcmp(name, iface->port->bridge->name)) {
775 if (must_exist && !iface) {
776 vsctl_fatal("no interface named %s", name);
782 bridge_insert_port(struct ovsrec_bridge *br, struct ovsrec_port *port)
784 struct ovsrec_port **ports;
787 ports = xmalloc(sizeof *br->ports * (br->n_ports + 1));
788 for (i = 0; i < br->n_ports; i++) {
789 ports[i] = br->ports[i];
791 ports[br->n_ports] = port;
792 ovsrec_bridge_set_ports(br, ports, br->n_ports + 1);
797 bridge_delete_port(struct ovsrec_bridge *br, struct ovsrec_port *port)
799 struct ovsrec_port **ports;
802 ports = xmalloc(sizeof *br->ports * br->n_ports);
803 for (i = n = 0; i < br->n_ports; i++) {
804 if (br->ports[i] != port) {
805 ports[n++] = br->ports[i];
808 ovsrec_bridge_set_ports(br, ports, n);
813 ovs_insert_bridge(const struct ovsrec_open_vswitch *ovs,
814 struct ovsrec_bridge *bridge)
816 struct ovsrec_bridge **bridges;
819 bridges = xmalloc(sizeof *ovs->bridges * (ovs->n_bridges + 1));
820 for (i = 0; i < ovs->n_bridges; i++) {
821 bridges[i] = ovs->bridges[i];
823 bridges[ovs->n_bridges] = bridge;
824 ovsrec_open_vswitch_set_bridges(ovs, bridges, ovs->n_bridges + 1);
829 ovs_delete_bridge(const struct ovsrec_open_vswitch *ovs,
830 struct ovsrec_bridge *bridge)
832 struct ovsrec_bridge **bridges;
835 bridges = xmalloc(sizeof *ovs->bridges * ovs->n_bridges);
836 for (i = n = 0; i < ovs->n_bridges; i++) {
837 if (ovs->bridges[i] != bridge) {
838 bridges[n++] = ovs->bridges[i];
841 ovsrec_open_vswitch_set_bridges(ovs, bridges, n);
846 cmd_init(struct vsctl_context *ctx OVS_UNUSED)
851 cmd_add_br(struct vsctl_context *ctx)
853 bool may_exist = shash_find(&ctx->options, "--may-exist") != 0;
854 const char *br_name, *parent_name;
855 struct vsctl_info info;
858 br_name = ctx->argv[1];
859 if (ctx->argc == 2) {
862 } else if (ctx->argc == 4) {
863 parent_name = ctx->argv[2];
864 vlan = atoi(ctx->argv[3]);
865 if (vlan < 1 || vlan > 4095) {
866 vsctl_fatal("%s: vlan must be between 1 and 4095", ctx->argv[0]);
869 vsctl_fatal("'%s' command takes exactly 1 or 3 arguments",
873 get_info(ctx->ovs, &info);
875 struct vsctl_bridge *br;
877 br = find_bridge(&info, br_name, false);
881 vsctl_fatal("\"--may-exist add-br %s\" but %s is "
882 "a VLAN bridge for VLAN %d",
883 br_name, br_name, br->vlan);
887 vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
888 "is not a VLAN bridge",
889 br_name, parent_name, vlan, br_name);
890 } else if (strcmp(br->parent->name, parent_name)) {
891 vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
892 "has the wrong parent %s",
893 br_name, parent_name, vlan,
894 br_name, br->parent->name);
895 } else if (br->vlan != vlan) {
896 vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
897 "is a VLAN bridge for the wrong VLAN %d",
898 br_name, parent_name, vlan, br_name, br->vlan);
904 check_conflicts(&info, br_name,
905 xasprintf("cannot create a bridge named %s", br_name));
908 struct ovsrec_port *port;
909 struct ovsrec_interface *iface;
910 struct ovsrec_bridge *br;
912 iface = ovsrec_interface_insert(ctx->txn);
913 ovsrec_interface_set_name(iface, br_name);
915 port = ovsrec_port_insert(ctx->txn);
916 ovsrec_port_set_name(port, br_name);
917 ovsrec_port_set_interfaces(port, &iface, 1);
919 br = ovsrec_bridge_insert(ctx->txn);
920 ovsrec_bridge_set_name(br, br_name);
921 ovsrec_bridge_set_ports(br, &port, 1);
923 ovs_insert_bridge(ctx->ovs, br);
925 struct vsctl_bridge *parent;
926 struct ovsrec_port *port;
927 struct ovsrec_interface *iface;
928 struct ovsrec_bridge *br;
931 parent = find_bridge(&info, parent_name, false);
932 if (parent && parent->vlan) {
933 vsctl_fatal("cannot create bridge with fake bridge as parent");
936 vsctl_fatal("parent bridge %s does not exist", parent_name);
940 iface = ovsrec_interface_insert(ctx->txn);
941 ovsrec_interface_set_name(iface, br_name);
942 ovsrec_interface_set_type(iface, "internal");
944 port = ovsrec_port_insert(ctx->txn);
945 ovsrec_port_set_name(port, br_name);
946 ovsrec_port_set_interfaces(port, &iface, 1);
947 ovsrec_port_set_fake_bridge(port, true);
948 ovsrec_port_set_tag(port, &tag, 1);
950 bridge_insert_port(br, port);
957 del_port(struct vsctl_info *info, struct vsctl_port *port)
959 struct shash_node *node;
961 SHASH_FOR_EACH (node, &info->ifaces) {
962 struct vsctl_iface *iface = node->data;
963 if (iface->port == port) {
964 ovsrec_interface_delete(iface->iface_cfg);
967 ovsrec_port_delete(port->port_cfg);
969 bridge_delete_port((port->bridge->parent
970 ? port->bridge->parent->br_cfg
971 : port->bridge->br_cfg), port->port_cfg);
975 cmd_del_br(struct vsctl_context *ctx)
977 bool must_exist = !shash_find(&ctx->options, "--if-exists");
978 struct vsctl_bridge *bridge;
979 struct vsctl_info info;
981 get_info(ctx->ovs, &info);
982 bridge = find_bridge(&info, ctx->argv[1], must_exist);
984 struct shash_node *node;
986 SHASH_FOR_EACH (node, &info.ports) {
987 struct vsctl_port *port = node->data;
988 if (port->bridge == bridge || port->bridge->parent == bridge
989 || !strcmp(port->port_cfg->name, bridge->name)) {
990 del_port(&info, port);
993 if (bridge->br_cfg) {
994 ovsrec_bridge_delete(bridge->br_cfg);
995 ovs_delete_bridge(ctx->ovs, bridge->br_cfg);
1002 output_sorted(struct svec *svec, struct ds *output)
1008 SVEC_FOR_EACH (i, name, svec) {
1009 ds_put_format(output, "%s\n", name);
1014 cmd_list_br(struct vsctl_context *ctx)
1016 struct shash_node *node;
1017 struct vsctl_info info;
1018 struct svec bridges;
1020 get_info(ctx->ovs, &info);
1022 svec_init(&bridges);
1023 SHASH_FOR_EACH (node, &info.bridges) {
1024 struct vsctl_bridge *br = node->data;
1025 svec_add(&bridges, br->name);
1027 output_sorted(&bridges, &ctx->output);
1028 svec_destroy(&bridges);
1034 cmd_br_exists(struct vsctl_context *ctx)
1036 struct vsctl_info info;
1038 get_info(ctx->ovs, &info);
1039 if (!find_bridge(&info, ctx->argv[1], false)) {
1045 /* Returns true if 'b_prefix' (of length 'b_prefix_len') concatenated with 'b'
1046 * equals 'a', false otherwise. */
1048 key_matches(const char *a,
1049 const char *b_prefix, size_t b_prefix_len, const char *b)
1051 return !strncmp(a, b_prefix, b_prefix_len) && !strcmp(a + b_prefix_len, b);
1055 set_external_id(char **old_keys, char **old_values, size_t old_n,
1056 char *key, char *value,
1057 char ***new_keysp, char ***new_valuesp, size_t *new_np)
1064 new_keys = xmalloc(sizeof *new_keys * (old_n + 1));
1065 new_values = xmalloc(sizeof *new_values * (old_n + 1));
1067 for (i = 0; i < old_n; i++) {
1068 if (strcmp(key, old_keys[i])) {
1069 new_keys[new_n] = old_keys[i];
1070 new_values[new_n] = old_values[i];
1075 new_keys[new_n] = key;
1076 new_values[new_n] = value;
1079 *new_keysp = new_keys;
1080 *new_valuesp = new_values;
1085 cmd_br_set_external_id(struct vsctl_context *ctx)
1087 struct vsctl_info info;
1088 struct vsctl_bridge *bridge;
1089 char **keys, **values;
1092 get_info(ctx->ovs, &info);
1093 bridge = find_bridge(&info, ctx->argv[1], true);
1094 if (bridge->br_cfg) {
1095 set_external_id(bridge->br_cfg->key_external_ids,
1096 bridge->br_cfg->value_external_ids,
1097 bridge->br_cfg->n_external_ids,
1098 ctx->argv[2], ctx->argc >= 4 ? ctx->argv[3] : NULL,
1099 &keys, &values, &n);
1100 ovsrec_bridge_set_external_ids(bridge->br_cfg, keys, values, n);
1102 char *key = xasprintf("fake-bridge-%s", ctx->argv[2]);
1103 struct vsctl_port *port = shash_find_data(&info.ports, ctx->argv[1]);
1104 set_external_id(port->port_cfg->key_external_ids,
1105 port->port_cfg->value_external_ids,
1106 port->port_cfg->n_external_ids,
1107 key, ctx->argc >= 4 ? ctx->argv[3] : NULL,
1108 &keys, &values, &n);
1109 ovsrec_port_set_external_ids(port->port_cfg, keys, values, n);
1119 get_external_id(char **keys, char **values, size_t n,
1120 const char *prefix, const char *key,
1123 size_t prefix_len = strlen(prefix);
1128 for (i = 0; i < n; i++) {
1129 if (!key && !strncmp(keys[i], prefix, prefix_len)) {
1130 svec_add_nocopy(&svec, xasprintf("%s=%s",
1131 keys[i] + prefix_len, values[i]));
1132 } else if (key_matches(keys[i], prefix, prefix_len, key)) {
1133 svec_add(&svec, values[i]);
1137 output_sorted(&svec, output);
1138 svec_destroy(&svec);
1142 cmd_br_get_external_id(struct vsctl_context *ctx)
1144 struct vsctl_info info;
1145 struct vsctl_bridge *bridge;
1147 get_info(ctx->ovs, &info);
1148 bridge = find_bridge(&info, ctx->argv[1], true);
1149 if (bridge->br_cfg) {
1150 get_external_id(bridge->br_cfg->key_external_ids,
1151 bridge->br_cfg->value_external_ids,
1152 bridge->br_cfg->n_external_ids,
1153 "", ctx->argc >= 3 ? ctx->argv[2] : NULL,
1156 struct vsctl_port *port = shash_find_data(&info.ports, ctx->argv[1]);
1157 get_external_id(port->port_cfg->key_external_ids,
1158 port->port_cfg->value_external_ids,
1159 port->port_cfg->n_external_ids,
1160 "fake-bridge-", ctx->argc >= 3 ? ctx->argv[2] : NULL, &ctx->output);
1167 cmd_list_ports(struct vsctl_context *ctx)
1169 struct vsctl_bridge *br;
1170 struct shash_node *node;
1171 struct vsctl_info info;
1174 get_info(ctx->ovs, &info);
1175 br = find_bridge(&info, ctx->argv[1], true);
1178 SHASH_FOR_EACH (node, &info.ports) {
1179 struct vsctl_port *port = node->data;
1181 if (strcmp(port->port_cfg->name, br->name) && br == port->bridge) {
1182 svec_add(&ports, port->port_cfg->name);
1185 output_sorted(&ports, &ctx->output);
1186 svec_destroy(&ports);
1192 add_port(struct vsctl_context *ctx,
1193 const char *br_name, const char *port_name,
1194 bool may_exist, bool fake_iface,
1195 char *iface_names[], int n_ifaces,
1196 char *settings[], int n_settings)
1198 struct vsctl_info info;
1199 struct vsctl_bridge *bridge;
1200 struct ovsrec_interface **ifaces;
1201 struct ovsrec_port *port;
1204 get_info(ctx->ovs, &info);
1206 struct vsctl_port *port;
1208 port = find_port(&info, port_name, false);
1210 struct svec want_names, have_names;
1213 svec_init(&want_names);
1214 for (i = 0; i < n_ifaces; i++) {
1215 svec_add(&want_names, iface_names[i]);
1217 svec_sort(&want_names);
1219 svec_init(&have_names);
1220 for (i = 0; i < port->port_cfg->n_interfaces; i++) {
1221 svec_add(&have_names, port->port_cfg->interfaces[i]->name);
1223 svec_sort(&have_names);
1225 if (strcmp(port->bridge->name, br_name)) {
1226 char *command = vsctl_context_to_string(ctx);
1227 vsctl_fatal("\"%s\" but %s is actually attached to bridge %s",
1228 command, port_name, port->bridge->name);
1231 if (!svec_equal(&want_names, &have_names)) {
1232 char *have_names_string = svec_join(&have_names, ", ", "");
1233 char *command = vsctl_context_to_string(ctx);
1235 vsctl_fatal("\"%s\" but %s actually has interface(s) %s",
1236 command, port_name, have_names_string);
1239 svec_destroy(&want_names);
1240 svec_destroy(&have_names);
1245 check_conflicts(&info, port_name,
1246 xasprintf("cannot create a port named %s", port_name));
1247 for (i = 0; i < n_ifaces; i++) {
1248 check_conflicts(&info, iface_names[i],
1249 xasprintf("cannot create an interface named %s",
1252 bridge = find_bridge(&info, br_name, true);
1254 ifaces = xmalloc(n_ifaces * sizeof *ifaces);
1255 for (i = 0; i < n_ifaces; i++) {
1256 ifaces[i] = ovsrec_interface_insert(ctx->txn);
1257 ovsrec_interface_set_name(ifaces[i], iface_names[i]);
1260 port = ovsrec_port_insert(ctx->txn);
1261 ovsrec_port_set_name(port, port_name);
1262 ovsrec_port_set_interfaces(port, ifaces, n_ifaces);
1263 ovsrec_port_set_bond_fake_iface(port, fake_iface);
1267 int64_t tag = bridge->vlan;
1268 ovsrec_port_set_tag(port, &tag, 1);
1271 for (i = 0; i < n_settings; i++) {
1272 set_column(get_table("Port"), &port->header_, settings[i]);
1275 bridge_insert_port((bridge->parent ? bridge->parent->br_cfg
1276 : bridge->br_cfg), port);
1282 cmd_add_port(struct vsctl_context *ctx)
1284 bool may_exist = shash_find(&ctx->options, "--may-exist") != 0;
1286 add_port(ctx, ctx->argv[1], ctx->argv[2], may_exist, false,
1287 &ctx->argv[2], 1, &ctx->argv[3], ctx->argc - 3);
1291 cmd_add_bond(struct vsctl_context *ctx)
1293 bool may_exist = shash_find(&ctx->options, "--may-exist") != 0;
1294 bool fake_iface = shash_find(&ctx->options, "--fake-iface");
1298 n_ifaces = ctx->argc - 3;
1299 for (i = 3; i < ctx->argc; i++) {
1300 if (strchr(ctx->argv[i], '=')) {
1306 vsctl_fatal("add-bond requires at least 2 interfaces, but only "
1307 "%d were specified", n_ifaces);
1310 add_port(ctx, ctx->argv[1], ctx->argv[2], may_exist, fake_iface,
1311 &ctx->argv[3], n_ifaces,
1312 &ctx->argv[n_ifaces + 3], ctx->argc - 3 - n_ifaces);
1316 cmd_del_port(struct vsctl_context *ctx)
1318 bool must_exist = !shash_find(&ctx->options, "--if-exists");
1319 bool with_iface = shash_find(&ctx->options, "--with-iface") != NULL;
1320 struct vsctl_port *port;
1321 struct vsctl_info info;
1323 get_info(ctx->ovs, &info);
1325 port = find_port(&info, ctx->argv[ctx->argc - 1], must_exist);
1327 const char *target = ctx->argv[ctx->argc - 1];
1328 struct vsctl_iface *iface;
1330 port = find_port(&info, target, false);
1332 iface = find_iface(&info, target, false);
1337 if (must_exist && !port) {
1338 vsctl_fatal("no port or interface named %s", target);
1343 if (ctx->argc == 3) {
1344 struct vsctl_bridge *bridge;
1346 bridge = find_bridge(&info, ctx->argv[1], true);
1347 if (port->bridge != bridge) {
1348 if (port->bridge->parent == bridge) {
1349 vsctl_fatal("bridge %s does not have a port %s (although "
1350 "its parent bridge %s does)",
1351 ctx->argv[1], ctx->argv[2],
1352 bridge->parent->name);
1354 vsctl_fatal("bridge %s does not have a port %s",
1355 ctx->argv[1], ctx->argv[2]);
1360 del_port(&info, port);
1367 cmd_port_to_br(struct vsctl_context *ctx)
1369 struct vsctl_port *port;
1370 struct vsctl_info info;
1372 get_info(ctx->ovs, &info);
1373 port = find_port(&info, ctx->argv[1], true);
1374 ds_put_format(&ctx->output, "%s\n", port->bridge->name);
1379 cmd_br_to_vlan(struct vsctl_context *ctx)
1381 struct vsctl_bridge *bridge;
1382 struct vsctl_info info;
1384 get_info(ctx->ovs, &info);
1385 bridge = find_bridge(&info, ctx->argv[1], true);
1386 ds_put_format(&ctx->output, "%d\n", bridge->vlan);
1391 cmd_br_to_parent(struct vsctl_context *ctx)
1393 struct vsctl_bridge *bridge;
1394 struct vsctl_info info;
1396 get_info(ctx->ovs, &info);
1397 bridge = find_bridge(&info, ctx->argv[1], true);
1398 if (bridge->parent) {
1399 bridge = bridge->parent;
1401 ds_put_format(&ctx->output, "%s\n", bridge->name);
1406 cmd_list_ifaces(struct vsctl_context *ctx)
1408 struct vsctl_bridge *br;
1409 struct shash_node *node;
1410 struct vsctl_info info;
1413 get_info(ctx->ovs, &info);
1414 br = find_bridge(&info, ctx->argv[1], true);
1417 SHASH_FOR_EACH (node, &info.ifaces) {
1418 struct vsctl_iface *iface = node->data;
1420 if (strcmp(iface->iface_cfg->name, br->name)
1421 && br == iface->port->bridge) {
1422 svec_add(&ifaces, iface->iface_cfg->name);
1425 output_sorted(&ifaces, &ctx->output);
1426 svec_destroy(&ifaces);
1432 cmd_iface_to_br(struct vsctl_context *ctx)
1434 struct vsctl_iface *iface;
1435 struct vsctl_info info;
1437 get_info(ctx->ovs, &info);
1438 iface = find_iface(&info, ctx->argv[1], true);
1439 ds_put_format(&ctx->output, "%s\n", iface->port->bridge->name);
1444 cmd_get_controller(struct vsctl_context *ctx)
1446 struct vsctl_info info;
1448 get_info(ctx->ovs, &info);
1450 if (ctx->argc == 1) {
1451 /* Return the controller from the "Open_vSwitch" table */
1453 ds_put_format(&ctx->output, "%s\n", info.ctrl->target);
1456 /* Return the controller for a particular bridge. */
1457 struct vsctl_bridge *br = find_bridge(&info, ctx->argv[1], true);
1459 /* If no controller is explicitly defined for the requested
1460 * bridge, fallback to the "Open_vSwitch" table's controller. */
1462 ds_put_format(&ctx->output, "%s\n", br->ctrl->target);
1463 } else if (info.ctrl) {
1464 ds_put_format(&ctx->output, "%s\n", info.ctrl->target);
1472 cmd_del_controller(struct vsctl_context *ctx)
1474 struct vsctl_info info;
1476 get_info(ctx->ovs, &info);
1478 if (ctx->argc == 1) {
1480 ovsrec_controller_delete(info.ctrl);
1481 ovsrec_open_vswitch_set_controller(ctx->ovs, NULL);
1484 struct vsctl_bridge *br = find_real_bridge(&info, ctx->argv[1], true);
1487 ovsrec_controller_delete(br->ctrl);
1488 ovsrec_bridge_set_controller(br->br_cfg, NULL);
1496 cmd_set_controller(struct vsctl_context *ctx)
1498 struct vsctl_info info;
1499 struct ovsrec_controller *ctrl;
1501 get_info(ctx->ovs, &info);
1503 if (ctx->argc == 2) {
1504 /* Set the controller in the "Open_vSwitch" table. */
1506 ovsrec_controller_delete(info.ctrl);
1508 ctrl = ovsrec_controller_insert(ctx->txn);
1509 ovsrec_controller_set_target(ctrl, ctx->argv[1]);
1510 ovsrec_open_vswitch_set_controller(ctx->ovs, ctrl);
1512 /* Set the controller for a particular bridge. */
1513 struct vsctl_bridge *br = find_real_bridge(&info, ctx->argv[1], true);
1516 ovsrec_controller_delete(br->ctrl);
1518 ctrl = ovsrec_controller_insert(ctx->txn);
1519 ovsrec_controller_set_target(ctrl, ctx->argv[2]);
1520 ovsrec_bridge_set_controller(br->br_cfg, ctrl);
1527 cmd_get_fail_mode(struct vsctl_context *ctx)
1529 struct vsctl_info info;
1530 const char *fail_mode = NULL;
1532 get_info(ctx->ovs, &info);
1534 if (ctx->argc == 1) {
1535 /* Return the fail-mode from the "Open_vSwitch" table */
1536 if (info.ctrl && info.ctrl->fail_mode) {
1537 fail_mode = info.ctrl->fail_mode;
1540 /* Return the fail-mode for a particular bridge. */
1541 struct vsctl_bridge *br = find_bridge(&info, ctx->argv[1], true);
1543 /* If no controller or fail-mode is explicitly defined for the
1544 * requested bridge, fallback to the "Open_vSwitch" table's
1546 if (br->ctrl && br->ctrl->fail_mode) {
1547 fail_mode = br->ctrl->fail_mode;
1548 } else if (info.ctrl && info.ctrl->fail_mode) {
1549 fail_mode = info.ctrl->fail_mode;
1553 if (fail_mode && strlen(fail_mode)) {
1554 ds_put_format(&ctx->output, "%s\n", fail_mode);
1561 cmd_del_fail_mode(struct vsctl_context *ctx)
1563 struct vsctl_info info;
1565 get_info(ctx->ovs, &info);
1567 if (ctx->argc == 1) {
1568 if (info.ctrl && info.ctrl->fail_mode) {
1569 ovsrec_controller_set_fail_mode(info.ctrl, NULL);
1572 struct vsctl_bridge *br = find_real_bridge(&info, ctx->argv[1], true);
1574 if (br->ctrl && br->ctrl->fail_mode) {
1575 ovsrec_controller_set_fail_mode(br->ctrl, NULL);
1583 cmd_set_fail_mode(struct vsctl_context *ctx)
1585 struct vsctl_info info;
1586 const char *fail_mode;
1588 get_info(ctx->ovs, &info);
1590 fail_mode = (ctx->argc == 2) ? ctx->argv[1] : ctx->argv[2];
1592 if (strcmp(fail_mode, "standalone") && strcmp(fail_mode, "secure")) {
1593 vsctl_fatal("fail-mode must be \"standalone\" or \"secure\"");
1596 if (ctx->argc == 2) {
1597 /* Set the fail-mode in the "Open_vSwitch" table. */
1599 vsctl_fatal("no controller declared");
1601 ovsrec_controller_set_fail_mode(info.ctrl, fail_mode);
1603 struct vsctl_bridge *br = find_real_bridge(&info, ctx->argv[1], true);
1606 vsctl_fatal("no controller declared for %s", br->name);
1608 ovsrec_controller_set_fail_mode(br->ctrl, fail_mode);
1615 cmd_get_ssl(struct vsctl_context *ctx)
1617 struct ovsrec_ssl *ssl = ctx->ovs->ssl;
1620 ds_put_format(&ctx->output, "Private key: %s\n", ssl->private_key);
1621 ds_put_format(&ctx->output, "Certificate: %s\n", ssl->certificate);
1622 ds_put_format(&ctx->output, "CA Certificate: %s\n", ssl->ca_cert);
1623 ds_put_format(&ctx->output, "Bootstrap: %s\n",
1624 ssl->bootstrap_ca_cert ? "true" : "false");
1629 cmd_del_ssl(struct vsctl_context *ctx)
1631 struct ovsrec_ssl *ssl = ctx->ovs->ssl;
1634 ovsrec_ssl_delete(ssl);
1635 ovsrec_open_vswitch_set_ssl(ctx->ovs, NULL);
1640 cmd_set_ssl(struct vsctl_context *ctx)
1642 bool bootstrap = shash_find(&ctx->options, "--bootstrap");
1643 struct ovsrec_ssl *ssl = ctx->ovs->ssl;
1646 ovsrec_ssl_delete(ssl);
1648 ssl = ovsrec_ssl_insert(ctx->txn);
1650 ovsrec_ssl_set_private_key(ssl, ctx->argv[1]);
1651 ovsrec_ssl_set_certificate(ssl, ctx->argv[2]);
1652 ovsrec_ssl_set_ca_cert(ssl, ctx->argv[3]);
1654 ovsrec_ssl_set_bootstrap_ca_cert(ssl, bootstrap);
1656 ovsrec_open_vswitch_set_ssl(ctx->ovs, ssl);
1659 /* Parameter commands. */
1661 struct vsctl_row_id {
1662 const struct ovsdb_idl_table_class *table;
1663 const struct ovsdb_idl_column *name_column;
1664 const struct ovsdb_idl_column *uuid_column;
1667 struct vsctl_table_class {
1668 struct ovsdb_idl_table_class *class;
1669 struct vsctl_row_id row_ids[2];
1672 static const struct vsctl_table_class tables[] = {
1673 {&ovsrec_table_bridge,
1674 {{&ovsrec_table_bridge, &ovsrec_bridge_col_name, NULL},
1675 {NULL, NULL, NULL}}},
1677 {&ovsrec_table_controller,
1678 {{&ovsrec_table_bridge,
1679 &ovsrec_bridge_col_name,
1680 &ovsrec_bridge_col_controller},
1681 {&ovsrec_table_open_vswitch,
1683 &ovsrec_open_vswitch_col_controller}}},
1685 {&ovsrec_table_interface,
1686 {{&ovsrec_table_interface, &ovsrec_interface_col_name, NULL},
1687 {NULL, NULL, NULL}}},
1689 {&ovsrec_table_mirror,
1690 {{&ovsrec_table_mirror, &ovsrec_mirror_col_name, NULL},
1691 {NULL, NULL, NULL}}},
1693 {&ovsrec_table_netflow,
1694 {{&ovsrec_table_bridge,
1695 &ovsrec_bridge_col_name,
1696 &ovsrec_bridge_col_netflow},
1697 {NULL, NULL, NULL}}},
1699 {&ovsrec_table_open_vswitch,
1700 {{&ovsrec_table_open_vswitch, NULL, NULL},
1701 {NULL, NULL, NULL}}},
1703 {&ovsrec_table_port,
1704 {{&ovsrec_table_port, &ovsrec_port_col_name, NULL},
1705 {NULL, NULL, NULL}}},
1708 {{&ovsrec_table_open_vswitch, NULL, &ovsrec_open_vswitch_col_ssl}}},
1710 {NULL, {{NULL, NULL, NULL}, {NULL, NULL, NULL}}}
1714 die_if_error(char *error)
1717 vsctl_fatal("%s", error);
1722 to_lower_and_underscores(unsigned c)
1724 return c == '-' ? '_' : tolower(c);
1728 score_partial_match(const char *name, const char *s)
1732 if (!strcmp(name, s)) {
1735 for (score = 0; ; score++, name++, s++) {
1736 if (to_lower_and_underscores(*name) != to_lower_and_underscores(*s)) {
1738 } else if (*name == '\0') {
1739 return UINT_MAX - 1;
1742 return *s == '\0' ? score : 0;
1745 static const struct vsctl_table_class *
1746 get_table(const char *table_name)
1748 const struct vsctl_table_class *table;
1749 const struct vsctl_table_class *best_match = NULL;
1750 unsigned int best_score = 0;
1752 for (table = tables; table->class; table++) {
1753 unsigned int score = score_partial_match(table->class->name,
1755 if (score > best_score) {
1758 } else if (score == best_score) {
1764 } else if (best_score) {
1765 vsctl_fatal("multiple table names match \"%s\"", table_name);
1767 vsctl_fatal("unknown table \"%s\"", table_name);
1771 static const struct ovsdb_idl_row *
1772 get_row_by_id(struct vsctl_context *ctx, const struct vsctl_table_class *table,
1773 const struct vsctl_row_id *id, const char *record_id)
1775 const struct ovsdb_idl_row *referrer, *final;
1781 if (!id->name_column) {
1782 if (strcmp(record_id, ".")) {
1785 referrer = ovsdb_idl_first_row(ctx->idl, id->table);
1786 if (!referrer || ovsdb_idl_next_row(referrer)) {
1790 const struct ovsdb_idl_row *row;
1791 unsigned int best_score = 0;
1793 /* It might make sense to relax this assertion. */
1794 assert(id->name_column->type.key.type == OVSDB_TYPE_STRING);
1797 for (row = ovsdb_idl_first_row(ctx->idl, id->table);
1798 row != NULL && best_score != UINT_MAX;
1799 row = ovsdb_idl_next_row(row))
1801 struct ovsdb_datum name;
1803 ovsdb_idl_txn_read(row, id->name_column, &name);
1805 unsigned int score = score_partial_match(name.keys[0].string,
1807 if (score > best_score) {
1810 } else if (score == best_score) {
1814 ovsdb_datum_destroy(&name, &id->name_column->type);
1816 if (best_score && !referrer) {
1817 vsctl_fatal("multiple rows in %s match \"%s\"",
1818 table->class->name, record_id);
1826 if (id->uuid_column) {
1827 struct ovsdb_datum uuid;
1829 assert(id->uuid_column->type.key.type == OVSDB_TYPE_UUID);
1830 assert(id->uuid_column->type.value.type == OVSDB_TYPE_VOID);
1832 ovsdb_idl_txn_read(referrer, id->uuid_column, &uuid);
1834 final = ovsdb_idl_get_row_for_uuid(ctx->idl, table->class,
1835 &uuid.keys[0].uuid);
1837 ovsdb_datum_destroy(&uuid, &id->uuid_column->type);
1845 static const struct ovsdb_idl_row *
1846 get_row(struct vsctl_context *ctx,
1847 const struct vsctl_table_class *table, const char *record_id)
1849 const struct ovsdb_idl_row *row;
1852 if (uuid_from_string(&uuid, record_id)) {
1853 row = ovsdb_idl_get_row_for_uuid(ctx->idl, table->class, &uuid);
1857 for (i = 0; i < ARRAY_SIZE(table->row_ids); i++) {
1858 row = get_row_by_id(ctx, table, &table->row_ids[i], record_id);
1867 static const struct ovsdb_idl_row *
1868 must_get_row(struct vsctl_context *ctx,
1869 const struct vsctl_table_class *table, const char *record_id)
1871 const struct ovsdb_idl_row *row = get_row(ctx, table, record_id);
1873 vsctl_fatal("no row \"%s\" in table %s",
1874 record_id, table->class->name);
1880 get_column(const struct vsctl_table_class *table, const char *column_name,
1881 const struct ovsdb_idl_column **columnp)
1883 const struct ovsdb_idl_column *best_match = NULL;
1884 unsigned int best_score = 0;
1887 for (i = 0; i < table->class->n_columns; i++) {
1888 const struct ovsdb_idl_column *column = &table->class->columns[i];
1889 unsigned int score = score_partial_match(column->name, column_name);
1890 if (score > best_score) {
1891 best_match = column;
1893 } else if (score == best_score) {
1898 *columnp = best_match;
1901 } else if (best_score) {
1902 return xasprintf("%s contains more than one column whose name "
1903 "matches \"%s\"", table->class->name, column_name);
1905 return xasprintf("%s does not contain a column whose name matches "
1906 "\"%s\"", table->class->name, column_name);
1910 static char * WARN_UNUSED_RESULT
1911 parse_column_key_value(const char *arg, const struct vsctl_table_class *table,
1912 const struct ovsdb_idl_column **columnp,
1913 char **keyp, char **valuep)
1915 const char *p = arg;
1918 assert(columnp || keyp);
1926 /* Parse column name. */
1930 error = ovsdb_token_parse(&p, &column_name);
1934 if (column_name[0] == '\0') {
1936 error = xasprintf("%s: missing column name", arg);
1939 error = get_column(table, column_name, columnp);
1946 /* Parse key string. */
1947 if (*p == ':' || !columnp) {
1951 error = xasprintf("%s: key not accepted here", arg);
1954 error = ovsdb_token_parse(&p, keyp);
1962 /* Parse value string. */
1965 error = xasprintf("%s: value not accepted here", arg);
1968 *valuep = xstrdup(p + 1);
1974 error = xasprintf("%s: trailing garbage \"%s\" in argument",
1997 cmd_get(struct vsctl_context *ctx)
1999 bool if_exists = shash_find(&ctx->options, "--if-exists");
2000 const char *table_name = ctx->argv[1];
2001 const char *record_id = ctx->argv[2];
2002 const struct vsctl_table_class *table;
2003 const struct ovsdb_idl_row *row;
2004 struct ds *out = &ctx->output;
2007 table = get_table(table_name);
2008 row = must_get_row(ctx, table, record_id);
2009 for (i = 3; i < ctx->argc; i++) {
2010 const struct ovsdb_idl_column *column;
2011 struct ovsdb_datum datum;
2014 die_if_error(parse_column_key_value(ctx->argv[i], table,
2015 &column, &key_string, NULL));
2017 ovsdb_idl_txn_read(row, column, &datum);
2019 union ovsdb_atom key;
2022 if (column->type.value.type == OVSDB_TYPE_VOID) {
2023 vsctl_fatal("cannot specify key to get for non-map column %s",
2027 die_if_error(ovsdb_atom_from_string(&key,
2031 idx = ovsdb_datum_find_key(&datum, &key,
2032 column->type.key.type);
2033 if (idx == UINT_MAX) {
2035 vsctl_fatal("no key \"%s\" in %s record \"%s\" column %s",
2036 key_string, table->class->name, record_id,
2040 ovsdb_atom_to_string(&datum.values[idx],
2041 column->type.value.type, out);
2043 ovsdb_atom_destroy(&key, column->type.key.type);
2045 ovsdb_datum_to_string(&datum, &column->type, out);
2047 ds_put_char(out, '\n');
2048 ovsdb_datum_destroy(&datum, &column->type);
2055 list_record(const struct vsctl_table_class *table,
2056 const struct ovsdb_idl_row *row, struct ds *out)
2060 ds_put_format(out, "%-20s: "UUID_FMT"\n", "_uuid",
2061 UUID_ARGS(&row->uuid));
2062 for (i = 0; i < table->class->n_columns; i++) {
2063 const struct ovsdb_idl_column *column = &table->class->columns[i];
2064 struct ovsdb_datum datum;
2066 ovsdb_idl_txn_read(row, column, &datum);
2068 ds_put_format(out, "%-20s: ", column->name);
2069 ovsdb_datum_to_string(&datum, &column->type, out);
2070 ds_put_char(out, '\n');
2072 ovsdb_datum_destroy(&datum, &column->type);
2077 cmd_list(struct vsctl_context *ctx)
2079 const char *table_name = ctx->argv[1];
2080 const struct vsctl_table_class *table;
2081 struct ds *out = &ctx->output;
2084 table = get_table(table_name);
2085 if (ctx->argc > 2) {
2086 for (i = 2; i < ctx->argc; i++) {
2088 ds_put_char(out, '\n');
2090 list_record(table, must_get_row(ctx, table, ctx->argv[i]), out);
2093 const struct ovsdb_idl_row *row;
2096 for (row = ovsdb_idl_first_row(ctx->idl, table->class), first = true;
2098 row = ovsdb_idl_next_row(row), first = false) {
2100 ds_put_char(out, '\n');
2102 list_record(table, row, out);
2108 set_column(const struct vsctl_table_class *table,
2109 const struct ovsdb_idl_row *row, const char *arg)
2111 const struct ovsdb_idl_column *column;
2112 char *key_string, *value_string;
2115 error = parse_column_key_value(arg, table, &column, &key_string,
2117 die_if_error(error);
2118 if (!value_string) {
2119 vsctl_fatal("%s: missing value", arg);
2123 union ovsdb_atom key, value;
2124 struct ovsdb_datum old, new;
2126 if (column->type.value.type == OVSDB_TYPE_VOID) {
2127 vsctl_fatal("cannot specify key to set for non-map column %s",
2131 die_if_error(ovsdb_atom_from_string(&key, &column->type.key,
2133 die_if_error(ovsdb_atom_from_string(&value, &column->type.value,
2136 ovsdb_datum_init_empty(&new);
2137 ovsdb_datum_add_unsafe(&new, &key, &value, &column->type);
2139 ovsdb_atom_destroy(&key, column->type.key.type);
2140 ovsdb_atom_destroy(&value, column->type.value.type);
2142 ovsdb_idl_txn_read(row, column, &old);
2143 ovsdb_datum_union(&old, &new, &column->type, true);
2144 ovsdb_idl_txn_write(row, column, &old);
2146 ovsdb_datum_destroy(&new, &column->type);
2148 struct ovsdb_datum datum;
2150 die_if_error(ovsdb_datum_from_string(&datum, &column->type,
2152 ovsdb_idl_txn_write(row, column, &datum);
2160 cmd_set(struct vsctl_context *ctx)
2162 const char *table_name = ctx->argv[1];
2163 const char *record_id = ctx->argv[2];
2164 const struct vsctl_table_class *table;
2165 const struct ovsdb_idl_row *row;
2168 table = get_table(table_name);
2169 row = must_get_row(ctx, table, record_id);
2170 for (i = 3; i < ctx->argc; i++) {
2171 set_column(table, row, ctx->argv[i]);
2176 cmd_add(struct vsctl_context *ctx)
2178 const char *table_name = ctx->argv[1];
2179 const char *record_id = ctx->argv[2];
2180 const char *column_name = ctx->argv[3];
2181 const struct vsctl_table_class *table;
2182 const struct ovsdb_idl_column *column;
2183 const struct ovsdb_idl_row *row;
2184 const struct ovsdb_type *type;
2185 struct ovsdb_datum old;
2188 table = get_table(table_name);
2189 row = must_get_row(ctx, table, record_id);
2190 die_if_error(get_column(table, column_name, &column));
2192 type = &column->type;
2193 ovsdb_idl_txn_read(row, column, &old);
2194 for (i = 4; i < ctx->argc; i++) {
2195 struct ovsdb_type add_type;
2196 struct ovsdb_datum add;
2200 add_type.n_max = UINT_MAX;
2201 die_if_error(ovsdb_datum_from_string(&add, &add_type, ctx->argv[i]));
2202 ovsdb_datum_union(&old, &add, type, false);
2203 ovsdb_datum_destroy(&add, type);
2205 if (old.n > type->n_max) {
2206 vsctl_fatal("\"add\" operation would put %u %s in column %s of "
2207 "table %s but the maximum number is %u",
2209 type->value.type == OVSDB_TYPE_VOID ? "values" : "pairs",
2210 column->name, table->class->name, type->n_max);
2212 ovsdb_idl_txn_write(row, column, &old);
2216 cmd_remove(struct vsctl_context *ctx)
2218 const char *table_name = ctx->argv[1];
2219 const char *record_id = ctx->argv[2];
2220 const char *column_name = ctx->argv[3];
2221 const struct vsctl_table_class *table;
2222 const struct ovsdb_idl_column *column;
2223 const struct ovsdb_idl_row *row;
2224 const struct ovsdb_type *type;
2225 struct ovsdb_datum old;
2228 table = get_table(table_name);
2229 row = must_get_row(ctx, table, record_id);
2230 die_if_error(get_column(table, column_name, &column));
2232 type = &column->type;
2233 ovsdb_idl_txn_read(row, column, &old);
2234 for (i = 4; i < ctx->argc; i++) {
2235 struct ovsdb_type rm_type;
2236 struct ovsdb_datum rm;
2241 rm_type.n_max = UINT_MAX;
2242 error = ovsdb_datum_from_string(&rm, &rm_type, ctx->argv[i]);
2243 if (error && ovsdb_type_is_map(&rm_type)) {
2245 rm_type.value.type = OVSDB_TYPE_VOID;
2246 die_if_error(ovsdb_datum_from_string(&rm, &rm_type, ctx->argv[i]));
2248 ovsdb_datum_subtract(&old, type, &rm, &rm_type);
2249 ovsdb_datum_destroy(&rm, &rm_type);
2251 if (old.n < type->n_min) {
2252 vsctl_fatal("\"remove\" operation would put %u %s in column %s of "
2253 "table %s but the minimun number is %u",
2255 type->value.type == OVSDB_TYPE_VOID ? "values" : "pairs",
2256 column->name, table->class->name, type->n_min);
2258 ovsdb_idl_txn_write(row, column, &old);
2262 cmd_clear(struct vsctl_context *ctx)
2264 const char *table_name = ctx->argv[1];
2265 const char *record_id = ctx->argv[2];
2266 const struct vsctl_table_class *table;
2267 const struct ovsdb_idl_row *row;
2270 table = get_table(table_name);
2271 row = must_get_row(ctx, table, record_id);
2272 for (i = 3; i < ctx->argc; i++) {
2273 const struct ovsdb_idl_column *column;
2274 const struct ovsdb_type *type;
2275 struct ovsdb_datum datum;
2277 die_if_error(get_column(table, ctx->argv[i], &column));
2279 type = &column->type;
2280 if (type->n_min > 0) {
2281 vsctl_fatal("\"clear\" operation cannot be applied to column %s "
2282 "of table %s, which is not allowed to be empty",
2283 column->name, table->class->name);
2286 ovsdb_datum_init_empty(&datum);
2287 ovsdb_idl_txn_write(row, column, &datum);
2292 cmd_create(struct vsctl_context *ctx)
2294 const char *table_name = ctx->argv[1];
2295 const struct vsctl_table_class *table;
2296 const struct ovsdb_idl_row *row;
2299 table = get_table(table_name);
2300 row = ovsdb_idl_txn_insert(ctx->txn, table->class);
2301 for (i = 2; i < ctx->argc; i++) {
2302 set_column(table, row, ctx->argv[i]);
2304 ds_put_format(&ctx->output, UUID_FMT, UUID_ARGS(&row->uuid));
2307 /* This function may be used as the 'postprocess' function for commands that
2308 * insert new rows into the database. It expects that the command's 'run'
2309 * function prints the UUID reported by ovsdb_idl_txn_insert() as the command's
2310 * sole output. It replaces that output by the row's permanent UUID assigned
2311 * by the database server and appends a new-line.
2313 * Currently we use this only for "create", because the higher-level commands
2314 * are supposed to be independent of the actual structure of the vswitch
2317 post_create(struct vsctl_context *ctx)
2319 const struct uuid *real;
2322 uuid_from_string(&dummy, ds_cstr(&ctx->output));
2323 real = ovsdb_idl_txn_get_insert_uuid(ctx->txn, &dummy);
2325 ds_clear(&ctx->output);
2326 ds_put_format(&ctx->output, UUID_FMT, UUID_ARGS(real));
2328 ds_put_char(&ctx->output, '\n');
2332 cmd_destroy(struct vsctl_context *ctx)
2334 bool must_exist = !shash_find(&ctx->options, "--if-exists");
2335 const char *table_name = ctx->argv[1];
2336 const struct vsctl_table_class *table;
2339 table = get_table(table_name);
2340 for (i = 2; i < ctx->argc; i++) {
2341 const struct ovsdb_idl_row *row;
2343 row = (must_exist ? must_get_row : get_row)(ctx, table, ctx->argv[i]);
2345 ovsdb_idl_txn_delete(row);
2350 static struct json *
2351 where_uuid_equals(const struct uuid *uuid)
2354 json_array_create_1(
2355 json_array_create_3(
2356 json_string_create("_uuid"),
2357 json_string_create("=="),
2358 json_array_create_2(
2359 json_string_create("uuid"),
2360 json_string_create_nocopy(
2361 xasprintf(UUID_FMT, UUID_ARGS(uuid))))));
2365 vsctl_context_init(struct vsctl_context *ctx, struct vsctl_command *command,
2366 struct ovsdb_idl *idl, struct ovsdb_idl_txn *txn,
2367 const struct ovsrec_open_vswitch *ovs)
2369 ctx->argc = command->argc;
2370 ctx->argv = command->argv;
2371 ctx->options = command->options;
2373 ds_swap(&ctx->output, &command->output);
2381 vsctl_context_done(struct vsctl_context *ctx, struct vsctl_command *command)
2383 ds_swap(&ctx->output, &command->output);
2387 do_vsctl(const char *args, struct vsctl_command *commands, size_t n_commands,
2388 struct ovsdb_idl *idl)
2390 struct ovsdb_idl_txn *txn;
2391 const struct ovsrec_open_vswitch *ovs;
2392 enum ovsdb_idl_txn_status status;
2393 struct vsctl_command *c;
2394 int64_t next_cfg = 0;
2397 txn = the_idl_txn = ovsdb_idl_txn_create(idl);
2399 ovsdb_idl_txn_set_dry_run(txn);
2402 ovsdb_idl_txn_add_comment(txn, "ovs-vsctl: %s", args);
2404 ovs = ovsrec_open_vswitch_first(idl);
2406 /* XXX add verification that table is empty */
2407 ovs = ovsrec_open_vswitch_insert(txn);
2410 if (wait_for_reload) {
2411 struct json *where = where_uuid_equals(&ovs->header_.uuid);
2412 ovsdb_idl_txn_increment(txn, "Open_vSwitch", "next_cfg", where);
2413 json_destroy(where);
2416 for (c = commands; c < &commands[n_commands]; c++) {
2417 struct vsctl_context ctx;
2419 ds_init(&c->output);
2420 vsctl_context_init(&ctx, c, idl, txn, ovs);
2421 (c->syntax->run)(&ctx);
2422 vsctl_context_done(&ctx, c);
2425 status = ovsdb_idl_txn_commit_block(txn);
2426 if (wait_for_reload && status == TXN_SUCCESS) {
2427 next_cfg = ovsdb_idl_txn_get_increment_new_value(txn);
2429 for (c = commands; c < &commands[n_commands]; c++) {
2430 if (c->syntax->postprocess) {
2431 struct vsctl_context ctx;
2433 vsctl_context_init(&ctx, c, idl, txn, ovs);
2434 (c->syntax->postprocess)(&ctx);
2435 vsctl_context_done(&ctx, c);
2438 error = xstrdup(ovsdb_idl_txn_get_error(txn));
2439 ovsdb_idl_txn_destroy(txn);
2443 case TXN_INCOMPLETE:
2447 /* Should not happen--we never call ovsdb_idl_txn_abort(). */
2448 vsctl_fatal("transaction aborted");
2455 for (c = commands; c < &commands[n_commands]; c++) {
2456 ds_destroy(&c->output);
2462 vsctl_fatal("transaction error: %s", error);
2469 for (c = commands; c < &commands[n_commands]; c++) {
2470 struct ds *ds = &c->output;
2475 for (j = 0; j < ds->length; j++) {
2476 int c = ds->string[j];
2479 fputs("\\n", stdout);
2483 fputs("\\\\", stdout);
2492 fputs(ds_cstr(ds), stdout);
2494 ds_destroy(&c->output);
2495 shash_destroy(&c->options);
2499 if (wait_for_reload && status != TXN_UNCHANGED) {
2501 const struct ovsrec_open_vswitch *ovs;
2504 OVSREC_OPEN_VSWITCH_FOR_EACH (ovs, idl) {
2505 if (ovs->cur_cfg >= next_cfg) {
2509 ovsdb_idl_wait(idl);
2514 ovsdb_idl_destroy(idl);
2519 static const struct vsctl_command_syntax all_commands[] = {
2520 /* Open vSwitch commands. */
2521 {"init", 0, 0, cmd_init, NULL, ""},
2523 /* Bridge commands. */
2524 {"add-br", 1, 3, cmd_add_br, NULL, "--may-exist"},
2525 {"del-br", 1, 1, cmd_del_br, NULL, "--if-exists"},
2526 {"list-br", 0, 0, cmd_list_br, NULL, ""},
2527 {"br-exists", 1, 1, cmd_br_exists, NULL, ""},
2528 {"br-to-vlan", 1, 1, cmd_br_to_vlan, NULL, ""},
2529 {"br-to-parent", 1, 1, cmd_br_to_parent, NULL, ""},
2530 {"br-set-external-id", 2, 3, cmd_br_set_external_id, NULL, ""},
2531 {"br-get-external-id", 1, 2, cmd_br_get_external_id, NULL, ""},
2533 /* Port commands. */
2534 {"list-ports", 1, 1, cmd_list_ports, NULL, ""},
2535 {"add-port", 2, INT_MAX, cmd_add_port, NULL, "--may-exist"},
2536 {"add-bond", 4, INT_MAX, cmd_add_bond, NULL, "--may-exist,--fake-iface"},
2537 {"del-port", 1, 2, cmd_del_port, NULL, "--if-exists,--with-iface"},
2538 {"port-to-br", 1, 1, cmd_port_to_br, NULL, ""},
2540 /* Interface commands. */
2541 {"list-ifaces", 1, 1, cmd_list_ifaces, NULL, ""},
2542 {"iface-to-br", 1, 1, cmd_iface_to_br, NULL, ""},
2544 /* Controller commands. */
2545 {"get-controller", 0, 1, cmd_get_controller, NULL, ""},
2546 {"del-controller", 0, 1, cmd_del_controller, NULL, ""},
2547 {"set-controller", 1, 2, cmd_set_controller, NULL, ""},
2548 {"get-fail-mode", 0, 1, cmd_get_fail_mode, NULL, ""},
2549 {"del-fail-mode", 0, 1, cmd_del_fail_mode, NULL, ""},
2550 {"set-fail-mode", 1, 2, cmd_set_fail_mode, NULL, ""},
2553 {"get-ssl", 0, 0, cmd_get_ssl, NULL, ""},
2554 {"del-ssl", 0, 0, cmd_del_ssl, NULL, ""},
2555 {"set-ssl", 3, 3, cmd_set_ssl, NULL, "--bootstrap"},
2557 /* Parameter commands. */
2558 {"get", 3, INT_MAX, cmd_get, NULL, "--if-exists"},
2559 {"list", 1, INT_MAX, cmd_list, NULL, ""},
2560 {"set", 3, INT_MAX, cmd_set, NULL, ""},
2561 {"add", 4, INT_MAX, cmd_add, NULL, ""},
2562 {"remove", 4, INT_MAX, cmd_remove, NULL, ""},
2563 {"clear", 3, INT_MAX, cmd_clear, NULL, ""},
2564 {"create", 2, INT_MAX, cmd_create, post_create, ""},
2565 {"destroy", 1, INT_MAX, cmd_destroy, NULL, "--if-exists"},
2567 {NULL, 0, 0, NULL, NULL, NULL},