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,
112 main(int argc, char *argv[])
114 struct ovsdb_idl *idl;
116 struct vsctl_command *commands;
121 set_program_name(argv[0]);
122 signal(SIGPIPE, SIG_IGN);
125 vlog_set_levels(VLM_ANY_MODULE, VLF_CONSOLE, VLL_WARN);
126 vlog_set_levels(VLM_reconnect, VLF_ANY_FACILITY, VLL_WARN);
129 /* Log our arguments. This is often valuable for debugging systems. */
130 args = process_escape_args(argv);
131 VLOG_INFO("Called as %s", args);
133 /* Parse command line. */
134 parse_options(argc, argv);
135 commands = parse_commands(argc - optind, argv + optind, &n_commands);
141 /* Now execute the commands. */
142 idl = the_idl = ovsdb_idl_create(db, &ovsrec_idl_class);
143 seqno = ovsdb_idl_get_seqno(idl);
146 unsigned int new_seqno;
149 new_seqno = ovsdb_idl_get_seqno(idl);
150 if (new_seqno != seqno) {
152 vsctl_fatal("too many database inconsistency failures");
154 do_vsctl(args, commands, n_commands, idl);
164 parse_options(int argc, char *argv[])
167 OPT_DB = UCHAR_MAX + 1,
174 static struct option long_options[] = {
175 {"db", required_argument, 0, OPT_DB},
176 {"no-syslog", no_argument, 0, OPT_NO_SYSLOG},
177 {"no-wait", no_argument, 0, OPT_NO_WAIT},
178 {"dry-run", no_argument, 0, OPT_DRY_RUN},
179 {"oneline", no_argument, 0, OPT_ONELINE},
180 {"timeout", required_argument, 0, 't'},
181 {"help", no_argument, 0, 'h'},
182 {"version", no_argument, 0, 'V'},
191 c = getopt_long(argc, argv, "+v::hVt:", long_options, NULL);
206 vlog_set_levels(VLM_vsctl, VLF_SYSLOG, VLL_WARN);
210 wait_for_reload = false;
221 OVS_PRINT_VERSION(0, 0);
225 timeout = strtoul(optarg, NULL, 10);
227 vsctl_fatal("value %s on -t or --timeout is invalid",
247 static struct vsctl_command *
248 parse_commands(int argc, char *argv[], size_t *n_commandsp)
250 struct vsctl_command *commands;
251 size_t n_commands, allocated_commands;
255 n_commands = allocated_commands = 0;
257 for (start = i = 0; i <= argc; i++) {
258 if (i == argc || !strcmp(argv[i], "--")) {
260 if (n_commands >= allocated_commands) {
261 struct vsctl_command *c;
263 commands = x2nrealloc(commands, &allocated_commands,
265 for (c = commands; c < &commands[n_commands]; c++) {
266 shash_moved(&c->options);
269 parse_command(i - start, &argv[start],
270 &commands[n_commands++]);
276 vsctl_fatal("missing command name (use --help for help)");
278 *n_commandsp = n_commands;
283 parse_command(int argc, char *argv[], struct vsctl_command *command)
285 const struct vsctl_command_syntax *p;
288 shash_init(&command->options);
289 for (i = 0; i < argc; i++) {
290 if (argv[i][0] != '-') {
293 if (!shash_add_once(&command->options, argv[i], NULL)) {
294 vsctl_fatal("'%s' option specified multiple times", argv[i]);
298 vsctl_fatal("missing command name");
301 for (p = all_commands; p->name; p++) {
302 if (!strcmp(p->name, argv[i])) {
303 struct shash_node *node;
306 SHASH_FOR_EACH (node, &command->options) {
307 const char *s = strstr(p->options, node->name);
308 int end = s ? s[strlen(node->name)] : EOF;
309 if (end != ',' && end != ' ' && end != '\0') {
310 vsctl_fatal("'%s' command has no '%s' option",
311 argv[i], node->name);
315 n_arg = argc - i - 1;
316 if (n_arg < p->min_args) {
317 vsctl_fatal("'%s' command requires at least %d arguments",
318 p->name, p->min_args);
319 } else if (n_arg > p->max_args) {
322 for (j = i + 1; j < argc; j++) {
323 if (argv[j][0] == '-') {
324 vsctl_fatal("'%s' command takes at most %d arguments "
325 "(note that options must precede command "
326 "names and follow a \"--\" argument)",
327 p->name, p->max_args);
331 vsctl_fatal("'%s' command takes at most %d arguments",
332 p->name, p->max_args);
335 command->argc = n_arg + 1;
336 command->argv = &argv[i];
342 vsctl_fatal("unknown command '%s'; use --help for help", argv[i]);
346 vsctl_fatal(const char *format, ...)
351 va_start(args, format);
352 message = xvasprintf(format, args);
355 vlog_set_levels(VLM_vsctl, VLF_CONSOLE, VLL_EMER);
356 VLOG_ERR("%s", message);
357 ovs_error(0, "%s", message);
358 vsctl_exit(EXIT_FAILURE);
361 /* Frees the current transaction and the underlying IDL and then calls
364 * Freeing the transaction and the IDL is not strictly necessary, but it makes
365 * for a clean memory leak report from valgrind in the normal case. That makes
366 * it easier to notice real memory leaks. */
368 vsctl_exit(int status)
371 ovsdb_idl_txn_abort(the_idl_txn);
372 ovsdb_idl_txn_destroy(the_idl_txn);
374 ovsdb_idl_destroy(the_idl);
382 %s: ovs-vswitchd management utility\n\
383 usage: %s [OPTIONS] COMMAND [ARG...]\n\
386 add-br BRIDGE create a new bridge named BRIDGE\n\
387 add-br BRIDGE PARENT VLAN create new fake BRIDGE in PARENT on VLAN\n\
388 del-br BRIDGE delete BRIDGE and all of its ports\n\
389 list-br print the names of all the bridges\n\
390 br-exists BRIDGE test whether BRIDGE exists\n\
391 br-to-vlan BRIDGE print the VLAN which BRIDGE is on\n\
392 br-to-parent BRIDGE print the parent of BRIDGE\n\
393 br-set-external-id BRIDGE KEY VALUE set KEY on BRIDGE to VALUE\n\
394 br-set-external-id BRIDGE KEY unset KEY on BRIDGE\n\
395 br-get-external-id BRIDGE KEY print value of KEY on BRIDGE\n\
396 br-get-external-id BRIDGE list key-value pairs on BRIDGE\n\
399 list-ports BRIDGE print the names of all the ports on BRIDGE\n\
400 add-port BRIDGE PORT add network device PORT to BRIDGE\n\
401 add-bond BRIDGE PORT IFACE... add bonded port PORT in BRIDGE from IFACES\n\
402 del-port [BRIDGE] PORT delete PORT (which may be bonded) from BRIDGE\n\
403 port-to-br PORT print name of bridge that contains PORT\n\
404 A bond is considered to be a single port.\n\
406 Interface commands (a bond consists of multiple interfaces):\n\
407 list-ifaces BRIDGE print the names of all interfaces on BRIDGE\n\
408 iface-to-br IFACE print name of bridge that contains IFACE\n\
410 Controller commands:\n\
411 get-controller [BRIDGE] print the controller for BRIDGE\n\
412 del-controller [BRIDGE] delete the controller for BRIDGE\n\
413 set-controller [BRIDGE] TARGET set the controller for BRIDGE to TARGET\n\
414 get-fail-mode [BRIDGE] print the fail-mode for BRIDGE\n\
415 del-fail-mode [BRIDGE] delete the fail-mode for BRIDGE\n\
416 set-fail-mode [BRIDGE] MODE set the fail-mode for BRIDGE to MODE\n\
419 get-ssl print the SSL configuration\n\
420 del-ssl delete the SSL configuration\n\
421 set-ssl PRIV-KEY CERT CA-CERT set the SSL configuration\n\
423 Database commands:\n\
424 list TBL [REC] list RECord (or all records) in TBL\n\
425 get TBL REC COL[:KEY] print values of COLumns in RECORD in TBL\n\
426 set TBL REC COL[:KEY]=VALUE set COLumn values in RECord in TBL\n\
427 add TBL REC COL [KEY=]VALUE add (KEY=)VALUE to COLumn in RECord in TBL\n\
428 remove TBL REC COL [KEY=]VALUE remove (KEY=)VALUE from COLumn\n\
429 clear TBL REC COL clear values from COLumn in RECord in TBL\n\
430 create TBL COL[:KEY]=VALUE create and initialize new record\n\
431 destroy TBL REC delete REC from TBL\n\
432 Potentially unsafe database commands require --force option.\n\
435 --db=DATABASE connect to DATABASE\n\
437 --oneline print exactly one line of output per command\n",
438 program_name, program_name, default_db());
442 -h, --help display this help message\n\
443 -V, --version display version information\n");
452 def = xasprintf("unix:%s/ovsdb-server", ovs_rundir);
457 struct vsctl_context {
461 struct shash options;
463 /* Modifiable state. */
465 struct ovsdb_idl *idl;
466 struct ovsdb_idl_txn *txn;
467 const struct ovsrec_open_vswitch *ovs;
470 struct vsctl_bridge {
471 struct ovsrec_bridge *br_cfg;
473 struct ovsrec_controller *ctrl;
474 struct vsctl_bridge *parent;
479 struct ovsrec_port *port_cfg;
480 struct vsctl_bridge *bridge;
484 struct ovsrec_interface *iface_cfg;
485 struct vsctl_port *port;
489 struct shash bridges;
492 struct ovsrec_controller *ctrl;
496 vsctl_context_to_string(const struct vsctl_context *ctx)
498 const struct shash_node *node;
504 SHASH_FOR_EACH (node, &ctx->options) {
505 svec_add(&words, node->name);
507 for (i = 0; i < ctx->argc; i++) {
508 svec_add(&words, ctx->argv[i]);
510 svec_terminate(&words);
512 s = process_escape_args(words.names);
514 svec_destroy(&words);
519 static struct vsctl_bridge *
520 add_bridge(struct vsctl_info *b,
521 struct ovsrec_bridge *br_cfg, const char *name,
522 struct vsctl_bridge *parent, int vlan)
524 struct vsctl_bridge *br = xmalloc(sizeof *br);
526 br->name = xstrdup(name);
529 br->ctrl = parent ? parent->br_cfg->controller : br_cfg->controller;
530 shash_add(&b->bridges, br->name, br);
535 port_is_fake_bridge(const struct ovsrec_port *port_cfg)
537 return (port_cfg->fake_bridge
539 && *port_cfg->tag >= 1 && *port_cfg->tag <= 4095);
542 static struct vsctl_bridge *
543 find_vlan_bridge(struct vsctl_info *info,
544 struct vsctl_bridge *parent, int vlan)
546 struct shash_node *node;
548 SHASH_FOR_EACH (node, &info->bridges) {
549 struct vsctl_bridge *br = node->data;
550 if (br->parent == parent && br->vlan == vlan) {
559 free_info(struct vsctl_info *info)
561 struct shash_node *node;
563 SHASH_FOR_EACH (node, &info->bridges) {
564 struct vsctl_bridge *bridge = node->data;
568 shash_destroy(&info->bridges);
570 SHASH_FOR_EACH (node, &info->ports) {
571 struct vsctl_port *port = node->data;
574 shash_destroy(&info->ports);
576 SHASH_FOR_EACH (node, &info->ifaces) {
577 struct vsctl_iface *iface = node->data;
580 shash_destroy(&info->ifaces);
584 get_info(const struct ovsrec_open_vswitch *ovs, struct vsctl_info *info)
586 struct shash bridges, ports;
589 shash_init(&info->bridges);
590 shash_init(&info->ports);
591 shash_init(&info->ifaces);
593 info->ctrl = ovs->controller;
595 shash_init(&bridges);
597 for (i = 0; i < ovs->n_bridges; i++) {
598 struct ovsrec_bridge *br_cfg = ovs->bridges[i];
599 struct vsctl_bridge *br;
602 if (!shash_add_once(&bridges, br_cfg->name, NULL)) {
603 VLOG_WARN("%s: database contains duplicate bridge name",
607 br = add_bridge(info, br_cfg, br_cfg->name, NULL, 0);
612 for (j = 0; j < br_cfg->n_ports; j++) {
613 struct ovsrec_port *port_cfg = br_cfg->ports[j];
615 if (!shash_add_once(&ports, port_cfg->name, NULL)) {
616 VLOG_WARN("%s: database contains duplicate port name",
621 if (port_is_fake_bridge(port_cfg)
622 && shash_add_once(&bridges, port_cfg->name, NULL)) {
623 add_bridge(info, NULL, port_cfg->name, br, *port_cfg->tag);
627 shash_destroy(&bridges);
628 shash_destroy(&ports);
630 shash_init(&bridges);
632 for (i = 0; i < ovs->n_bridges; i++) {
633 struct ovsrec_bridge *br_cfg = ovs->bridges[i];
634 struct vsctl_bridge *br;
637 if (!shash_add_once(&bridges, br_cfg->name, NULL)) {
640 br = shash_find_data(&info->bridges, br_cfg->name);
641 for (j = 0; j < br_cfg->n_ports; j++) {
642 struct ovsrec_port *port_cfg = br_cfg->ports[j];
643 struct vsctl_port *port;
646 if (!shash_add_once(&ports, port_cfg->name, NULL)) {
650 if (port_is_fake_bridge(port_cfg)
651 && !shash_add_once(&bridges, port_cfg->name, NULL)) {
655 port = xmalloc(sizeof *port);
656 port->port_cfg = port_cfg;
658 && *port_cfg->tag >= 1 && *port_cfg->tag <= 4095) {
659 port->bridge = find_vlan_bridge(info, br, *port_cfg->tag);
666 shash_add(&info->ports, port_cfg->name, port);
668 for (k = 0; k < port_cfg->n_interfaces; k++) {
669 struct ovsrec_interface *iface_cfg = port_cfg->interfaces[k];
670 struct vsctl_iface *iface;
672 if (shash_find(&info->ifaces, iface_cfg->name)) {
673 VLOG_WARN("%s: database contains duplicate interface name",
678 iface = xmalloc(sizeof *iface);
679 iface->iface_cfg = iface_cfg;
681 shash_add(&info->ifaces, iface_cfg->name, iface);
685 shash_destroy(&bridges);
686 shash_destroy(&ports);
690 check_conflicts(struct vsctl_info *info, const char *name,
693 struct vsctl_iface *iface;
694 struct vsctl_port *port;
696 if (shash_find(&info->bridges, name)) {
697 vsctl_fatal("%s because a bridge named %s already exists",
701 port = shash_find_data(&info->ports, name);
703 vsctl_fatal("%s because a port named %s already exists on "
704 "bridge %s", msg, name, port->bridge->name);
707 iface = shash_find_data(&info->ifaces, name);
709 vsctl_fatal("%s because an interface named %s already exists "
710 "on bridge %s", msg, name, iface->port->bridge->name);
716 static struct vsctl_bridge *
717 find_bridge(struct vsctl_info *info, const char *name, bool must_exist)
719 struct vsctl_bridge *br = shash_find_data(&info->bridges, name);
720 if (must_exist && !br) {
721 vsctl_fatal("no bridge named %s", name);
726 static struct vsctl_bridge *
727 find_real_bridge(struct vsctl_info *info, const char *name, bool must_exist)
729 struct vsctl_bridge *br = find_bridge(info, name, must_exist);
730 if (br && br->parent) {
731 vsctl_fatal("%s is a fake bridge", name);
736 static struct vsctl_port *
737 find_port(struct vsctl_info *info, const char *name, bool must_exist)
739 struct vsctl_port *port = shash_find_data(&info->ports, name);
740 if (port && !strcmp(name, port->bridge->name)) {
743 if (must_exist && !port) {
744 vsctl_fatal("no port named %s", name);
749 static struct vsctl_iface *
750 find_iface(struct vsctl_info *info, const char *name, bool must_exist)
752 struct vsctl_iface *iface = shash_find_data(&info->ifaces, name);
753 if (iface && !strcmp(name, iface->port->bridge->name)) {
756 if (must_exist && !iface) {
757 vsctl_fatal("no interface named %s", name);
763 bridge_insert_port(struct ovsrec_bridge *br, struct ovsrec_port *port)
765 struct ovsrec_port **ports;
768 ports = xmalloc(sizeof *br->ports * (br->n_ports + 1));
769 for (i = 0; i < br->n_ports; i++) {
770 ports[i] = br->ports[i];
772 ports[br->n_ports] = port;
773 ovsrec_bridge_set_ports(br, ports, br->n_ports + 1);
778 bridge_delete_port(struct ovsrec_bridge *br, struct ovsrec_port *port)
780 struct ovsrec_port **ports;
783 ports = xmalloc(sizeof *br->ports * br->n_ports);
784 for (i = n = 0; i < br->n_ports; i++) {
785 if (br->ports[i] != port) {
786 ports[n++] = br->ports[i];
789 ovsrec_bridge_set_ports(br, ports, n);
794 ovs_insert_bridge(const struct ovsrec_open_vswitch *ovs,
795 struct ovsrec_bridge *bridge)
797 struct ovsrec_bridge **bridges;
800 bridges = xmalloc(sizeof *ovs->bridges * (ovs->n_bridges + 1));
801 for (i = 0; i < ovs->n_bridges; i++) {
802 bridges[i] = ovs->bridges[i];
804 bridges[ovs->n_bridges] = bridge;
805 ovsrec_open_vswitch_set_bridges(ovs, bridges, ovs->n_bridges + 1);
810 ovs_delete_bridge(const struct ovsrec_open_vswitch *ovs,
811 struct ovsrec_bridge *bridge)
813 struct ovsrec_bridge **bridges;
816 bridges = xmalloc(sizeof *ovs->bridges * ovs->n_bridges);
817 for (i = n = 0; i < ovs->n_bridges; i++) {
818 if (ovs->bridges[i] != bridge) {
819 bridges[n++] = ovs->bridges[i];
822 ovsrec_open_vswitch_set_bridges(ovs, bridges, n);
827 cmd_init(struct vsctl_context *ctx OVS_UNUSED)
832 cmd_add_br(struct vsctl_context *ctx)
834 bool may_exist = shash_find(&ctx->options, "--may-exist") != 0;
835 const char *br_name, *parent_name;
836 struct vsctl_info info;
839 br_name = ctx->argv[1];
840 if (ctx->argc == 2) {
843 } else if (ctx->argc == 4) {
844 parent_name = ctx->argv[2];
845 vlan = atoi(ctx->argv[3]);
846 if (vlan < 1 || vlan > 4095) {
847 vsctl_fatal("%s: vlan must be between 1 and 4095", ctx->argv[0]);
850 vsctl_fatal("'%s' command takes exactly 1 or 3 arguments",
854 get_info(ctx->ovs, &info);
856 struct vsctl_bridge *br;
858 br = find_bridge(&info, br_name, false);
862 vsctl_fatal("\"--may-exist add-br %s\" but %s is "
863 "a VLAN bridge for VLAN %d",
864 br_name, br_name, br->vlan);
868 vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
869 "is not a VLAN bridge",
870 br_name, parent_name, vlan, br_name);
871 } else if (strcmp(br->parent->name, parent_name)) {
872 vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
873 "has the wrong parent %s",
874 br_name, parent_name, vlan,
875 br_name, br->parent->name);
876 } else if (br->vlan != vlan) {
877 vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
878 "is a VLAN bridge for the wrong VLAN %d",
879 br_name, parent_name, vlan, br_name, br->vlan);
885 check_conflicts(&info, br_name,
886 xasprintf("cannot create a bridge named %s", br_name));
889 struct ovsrec_port *port;
890 struct ovsrec_interface *iface;
891 struct ovsrec_bridge *br;
893 iface = ovsrec_interface_insert(ctx->txn);
894 ovsrec_interface_set_name(iface, br_name);
896 port = ovsrec_port_insert(ctx->txn);
897 ovsrec_port_set_name(port, br_name);
898 ovsrec_port_set_interfaces(port, &iface, 1);
900 br = ovsrec_bridge_insert(ctx->txn);
901 ovsrec_bridge_set_name(br, br_name);
902 ovsrec_bridge_set_ports(br, &port, 1);
904 ovs_insert_bridge(ctx->ovs, br);
906 struct vsctl_bridge *parent;
907 struct ovsrec_port *port;
908 struct ovsrec_interface *iface;
909 struct ovsrec_bridge *br;
912 parent = find_bridge(&info, parent_name, false);
913 if (parent && parent->vlan) {
914 vsctl_fatal("cannot create bridge with fake bridge as parent");
917 vsctl_fatal("parent bridge %s does not exist", parent_name);
921 iface = ovsrec_interface_insert(ctx->txn);
922 ovsrec_interface_set_name(iface, br_name);
923 ovsrec_interface_set_type(iface, "internal");
925 port = ovsrec_port_insert(ctx->txn);
926 ovsrec_port_set_name(port, br_name);
927 ovsrec_port_set_interfaces(port, &iface, 1);
928 ovsrec_port_set_fake_bridge(port, true);
929 ovsrec_port_set_tag(port, &tag, 1);
931 bridge_insert_port(br, port);
938 del_port(struct vsctl_info *info, struct vsctl_port *port)
940 struct shash_node *node;
942 SHASH_FOR_EACH (node, &info->ifaces) {
943 struct vsctl_iface *iface = node->data;
944 if (iface->port == port) {
945 ovsrec_interface_delete(iface->iface_cfg);
948 ovsrec_port_delete(port->port_cfg);
950 bridge_delete_port((port->bridge->parent
951 ? port->bridge->parent->br_cfg
952 : port->bridge->br_cfg), port->port_cfg);
956 cmd_del_br(struct vsctl_context *ctx)
958 bool must_exist = !shash_find(&ctx->options, "--if-exists");
959 struct vsctl_bridge *bridge;
960 struct vsctl_info info;
962 get_info(ctx->ovs, &info);
963 bridge = find_bridge(&info, ctx->argv[1], must_exist);
965 struct shash_node *node;
967 SHASH_FOR_EACH (node, &info.ports) {
968 struct vsctl_port *port = node->data;
969 if (port->bridge == bridge || port->bridge->parent == bridge
970 || !strcmp(port->port_cfg->name, bridge->name)) {
971 del_port(&info, port);
974 if (bridge->br_cfg) {
975 ovsrec_bridge_delete(bridge->br_cfg);
976 ovs_delete_bridge(ctx->ovs, bridge->br_cfg);
983 output_sorted(struct svec *svec, struct ds *output)
989 SVEC_FOR_EACH (i, name, svec) {
990 ds_put_format(output, "%s\n", name);
995 cmd_list_br(struct vsctl_context *ctx)
997 struct shash_node *node;
998 struct vsctl_info info;
1001 get_info(ctx->ovs, &info);
1003 svec_init(&bridges);
1004 SHASH_FOR_EACH (node, &info.bridges) {
1005 struct vsctl_bridge *br = node->data;
1006 svec_add(&bridges, br->name);
1008 output_sorted(&bridges, &ctx->output);
1009 svec_destroy(&bridges);
1015 cmd_br_exists(struct vsctl_context *ctx)
1017 struct vsctl_info info;
1019 get_info(ctx->ovs, &info);
1020 if (!find_bridge(&info, ctx->argv[1], false)) {
1026 /* Returns true if 'b_prefix' (of length 'b_prefix_len') concatenated with 'b'
1027 * equals 'a', false otherwise. */
1029 key_matches(const char *a,
1030 const char *b_prefix, size_t b_prefix_len, const char *b)
1032 return !strncmp(a, b_prefix, b_prefix_len) && !strcmp(a + b_prefix_len, b);
1036 set_external_id(char **old_keys, char **old_values, size_t old_n,
1037 char *key, char *value,
1038 char ***new_keysp, char ***new_valuesp, size_t *new_np)
1045 new_keys = xmalloc(sizeof *new_keys * (old_n + 1));
1046 new_values = xmalloc(sizeof *new_values * (old_n + 1));
1048 for (i = 0; i < old_n; i++) {
1049 if (strcmp(key, old_keys[i])) {
1050 new_keys[new_n] = old_keys[i];
1051 new_values[new_n] = old_values[i];
1056 new_keys[new_n] = key;
1057 new_values[new_n] = value;
1060 *new_keysp = new_keys;
1061 *new_valuesp = new_values;
1066 cmd_br_set_external_id(struct vsctl_context *ctx)
1068 struct vsctl_info info;
1069 struct vsctl_bridge *bridge;
1070 char **keys, **values;
1073 get_info(ctx->ovs, &info);
1074 bridge = find_bridge(&info, ctx->argv[1], true);
1075 if (bridge->br_cfg) {
1076 set_external_id(bridge->br_cfg->key_external_ids,
1077 bridge->br_cfg->value_external_ids,
1078 bridge->br_cfg->n_external_ids,
1079 ctx->argv[2], ctx->argc >= 4 ? ctx->argv[3] : NULL,
1080 &keys, &values, &n);
1081 ovsrec_bridge_set_external_ids(bridge->br_cfg, keys, values, n);
1083 char *key = xasprintf("fake-bridge-%s", ctx->argv[2]);
1084 struct vsctl_port *port = shash_find_data(&info.ports, ctx->argv[1]);
1085 set_external_id(port->port_cfg->key_external_ids,
1086 port->port_cfg->value_external_ids,
1087 port->port_cfg->n_external_ids,
1088 key, ctx->argc >= 4 ? ctx->argv[3] : NULL,
1089 &keys, &values, &n);
1090 ovsrec_port_set_external_ids(port->port_cfg, keys, values, n);
1100 get_external_id(char **keys, char **values, size_t n,
1101 const char *prefix, const char *key,
1104 size_t prefix_len = strlen(prefix);
1109 for (i = 0; i < n; i++) {
1110 if (!key && !strncmp(keys[i], prefix, prefix_len)) {
1111 svec_add_nocopy(&svec, xasprintf("%s=%s",
1112 keys[i] + prefix_len, values[i]));
1113 } else if (key_matches(keys[i], prefix, prefix_len, key)) {
1114 svec_add(&svec, values[i]);
1118 output_sorted(&svec, output);
1119 svec_destroy(&svec);
1123 cmd_br_get_external_id(struct vsctl_context *ctx)
1125 struct vsctl_info info;
1126 struct vsctl_bridge *bridge;
1128 get_info(ctx->ovs, &info);
1129 bridge = find_bridge(&info, ctx->argv[1], true);
1130 if (bridge->br_cfg) {
1131 get_external_id(bridge->br_cfg->key_external_ids,
1132 bridge->br_cfg->value_external_ids,
1133 bridge->br_cfg->n_external_ids,
1134 "", ctx->argc >= 3 ? ctx->argv[2] : NULL,
1137 struct vsctl_port *port = shash_find_data(&info.ports, ctx->argv[1]);
1138 get_external_id(port->port_cfg->key_external_ids,
1139 port->port_cfg->value_external_ids,
1140 port->port_cfg->n_external_ids,
1141 "fake-bridge-", ctx->argc >= 3 ? ctx->argv[2] : NULL, &ctx->output);
1148 cmd_list_ports(struct vsctl_context *ctx)
1150 struct vsctl_bridge *br;
1151 struct shash_node *node;
1152 struct vsctl_info info;
1155 get_info(ctx->ovs, &info);
1156 br = find_bridge(&info, ctx->argv[1], true);
1159 SHASH_FOR_EACH (node, &info.ports) {
1160 struct vsctl_port *port = node->data;
1162 if (strcmp(port->port_cfg->name, br->name) && br == port->bridge) {
1163 svec_add(&ports, port->port_cfg->name);
1166 output_sorted(&ports, &ctx->output);
1167 svec_destroy(&ports);
1173 add_port(struct vsctl_context *ctx,
1174 const char *br_name, const char *port_name,
1175 bool may_exist, bool fake_iface,
1176 char *iface_names[], int n_ifaces)
1178 struct vsctl_info info;
1179 struct vsctl_bridge *bridge;
1180 struct ovsrec_interface **ifaces;
1181 struct ovsrec_port *port;
1184 get_info(ctx->ovs, &info);
1186 struct vsctl_port *port;
1188 port = find_port(&info, port_name, false);
1190 struct svec want_names, have_names;
1193 svec_init(&want_names);
1194 for (i = 0; i < n_ifaces; i++) {
1195 svec_add(&want_names, iface_names[i]);
1197 svec_sort(&want_names);
1199 svec_init(&have_names);
1200 for (i = 0; i < port->port_cfg->n_interfaces; i++) {
1201 svec_add(&have_names, port->port_cfg->interfaces[i]->name);
1203 svec_sort(&have_names);
1205 if (strcmp(port->bridge->name, br_name)) {
1206 char *command = vsctl_context_to_string(ctx);
1207 vsctl_fatal("\"%s\" but %s is actually attached to bridge %s",
1208 command, port_name, port->bridge->name);
1211 if (!svec_equal(&want_names, &have_names)) {
1212 char *have_names_string = svec_join(&have_names, ", ", "");
1213 char *command = vsctl_context_to_string(ctx);
1215 vsctl_fatal("\"%s\" but %s actually has interface(s) %s",
1216 command, port_name, have_names_string);
1219 svec_destroy(&want_names);
1220 svec_destroy(&have_names);
1225 check_conflicts(&info, port_name,
1226 xasprintf("cannot create a port named %s", port_name));
1227 for (i = 0; i < n_ifaces; i++) {
1228 check_conflicts(&info, iface_names[i],
1229 xasprintf("cannot create an interface named %s",
1232 bridge = find_bridge(&info, br_name, true);
1234 ifaces = xmalloc(n_ifaces * sizeof *ifaces);
1235 for (i = 0; i < n_ifaces; i++) {
1236 ifaces[i] = ovsrec_interface_insert(ctx->txn);
1237 ovsrec_interface_set_name(ifaces[i], iface_names[i]);
1240 port = ovsrec_port_insert(ctx->txn);
1241 ovsrec_port_set_name(port, port_name);
1242 ovsrec_port_set_interfaces(port, ifaces, n_ifaces);
1243 ovsrec_port_set_bond_fake_iface(port, fake_iface);
1247 int64_t tag = bridge->vlan;
1248 ovsrec_port_set_tag(port, &tag, 1);
1251 bridge_insert_port((bridge->parent ? bridge->parent->br_cfg
1252 : bridge->br_cfg), port);
1258 cmd_add_port(struct vsctl_context *ctx)
1260 bool may_exist = shash_find(&ctx->options, "--may-exist") != 0;
1262 add_port(ctx, ctx->argv[1], ctx->argv[2], may_exist, false,
1267 cmd_add_bond(struct vsctl_context *ctx)
1269 bool may_exist = shash_find(&ctx->options, "--may-exist") != 0;
1270 bool fake_iface = shash_find(&ctx->options, "--fake-iface");
1272 add_port(ctx, ctx->argv[1], ctx->argv[2], may_exist, fake_iface,
1273 &ctx->argv[3], ctx->argc - 3);
1277 cmd_del_port(struct vsctl_context *ctx)
1279 bool must_exist = !shash_find(&ctx->options, "--if-exists");
1280 bool with_iface = shash_find(&ctx->options, "--with-iface") != NULL;
1281 struct vsctl_port *port;
1282 struct vsctl_info info;
1284 get_info(ctx->ovs, &info);
1286 port = find_port(&info, ctx->argv[ctx->argc - 1], must_exist);
1288 const char *target = ctx->argv[ctx->argc - 1];
1289 struct vsctl_iface *iface;
1291 port = find_port(&info, target, false);
1293 iface = find_iface(&info, target, false);
1298 if (must_exist && !port) {
1299 vsctl_fatal("no port or interface named %s", target);
1304 if (ctx->argc == 3) {
1305 struct vsctl_bridge *bridge;
1307 bridge = find_bridge(&info, ctx->argv[1], true);
1308 if (port->bridge != bridge) {
1309 if (port->bridge->parent == bridge) {
1310 vsctl_fatal("bridge %s does not have a port %s (although "
1311 "its parent bridge %s does)",
1312 ctx->argv[1], ctx->argv[2],
1313 bridge->parent->name);
1315 vsctl_fatal("bridge %s does not have a port %s",
1316 ctx->argv[1], ctx->argv[2]);
1321 del_port(&info, port);
1328 cmd_port_to_br(struct vsctl_context *ctx)
1330 struct vsctl_port *port;
1331 struct vsctl_info info;
1333 get_info(ctx->ovs, &info);
1334 port = find_port(&info, ctx->argv[1], true);
1335 ds_put_format(&ctx->output, "%s\n", port->bridge->name);
1340 cmd_br_to_vlan(struct vsctl_context *ctx)
1342 struct vsctl_bridge *bridge;
1343 struct vsctl_info info;
1345 get_info(ctx->ovs, &info);
1346 bridge = find_bridge(&info, ctx->argv[1], true);
1347 ds_put_format(&ctx->output, "%d\n", bridge->vlan);
1352 cmd_br_to_parent(struct vsctl_context *ctx)
1354 struct vsctl_bridge *bridge;
1355 struct vsctl_info info;
1357 get_info(ctx->ovs, &info);
1358 bridge = find_bridge(&info, ctx->argv[1], true);
1359 if (bridge->parent) {
1360 bridge = bridge->parent;
1362 ds_put_format(&ctx->output, "%s\n", bridge->name);
1367 cmd_list_ifaces(struct vsctl_context *ctx)
1369 struct vsctl_bridge *br;
1370 struct shash_node *node;
1371 struct vsctl_info info;
1374 get_info(ctx->ovs, &info);
1375 br = find_bridge(&info, ctx->argv[1], true);
1378 SHASH_FOR_EACH (node, &info.ifaces) {
1379 struct vsctl_iface *iface = node->data;
1381 if (strcmp(iface->iface_cfg->name, br->name)
1382 && br == iface->port->bridge) {
1383 svec_add(&ifaces, iface->iface_cfg->name);
1386 output_sorted(&ifaces, &ctx->output);
1387 svec_destroy(&ifaces);
1393 cmd_iface_to_br(struct vsctl_context *ctx)
1395 struct vsctl_iface *iface;
1396 struct vsctl_info info;
1398 get_info(ctx->ovs, &info);
1399 iface = find_iface(&info, ctx->argv[1], true);
1400 ds_put_format(&ctx->output, "%s\n", iface->port->bridge->name);
1405 cmd_get_controller(struct vsctl_context *ctx)
1407 struct vsctl_info info;
1409 get_info(ctx->ovs, &info);
1411 if (ctx->argc == 1) {
1412 /* Return the controller from the "Open_vSwitch" table */
1414 ds_put_format(&ctx->output, "%s\n", info.ctrl->target);
1417 /* Return the controller for a particular bridge. */
1418 struct vsctl_bridge *br = find_bridge(&info, ctx->argv[1], true);
1420 /* If no controller is explicitly defined for the requested
1421 * bridge, fallback to the "Open_vSwitch" table's controller. */
1423 ds_put_format(&ctx->output, "%s\n", br->ctrl->target);
1424 } else if (info.ctrl) {
1425 ds_put_format(&ctx->output, "%s\n", info.ctrl->target);
1433 cmd_del_controller(struct vsctl_context *ctx)
1435 struct vsctl_info info;
1437 get_info(ctx->ovs, &info);
1439 if (ctx->argc == 1) {
1441 ovsrec_controller_delete(info.ctrl);
1442 ovsrec_open_vswitch_set_controller(ctx->ovs, NULL);
1445 struct vsctl_bridge *br = find_real_bridge(&info, ctx->argv[1], true);
1448 ovsrec_controller_delete(br->ctrl);
1449 ovsrec_bridge_set_controller(br->br_cfg, NULL);
1457 cmd_set_controller(struct vsctl_context *ctx)
1459 struct vsctl_info info;
1460 struct ovsrec_controller *ctrl;
1462 get_info(ctx->ovs, &info);
1464 if (ctx->argc == 2) {
1465 /* Set the controller in the "Open_vSwitch" table. */
1467 ovsrec_controller_delete(info.ctrl);
1469 ctrl = ovsrec_controller_insert(ctx->txn);
1470 ovsrec_controller_set_target(ctrl, ctx->argv[1]);
1471 ovsrec_open_vswitch_set_controller(ctx->ovs, ctrl);
1473 /* Set the controller for a particular bridge. */
1474 struct vsctl_bridge *br = find_real_bridge(&info, ctx->argv[1], true);
1477 ovsrec_controller_delete(br->ctrl);
1479 ctrl = ovsrec_controller_insert(ctx->txn);
1480 ovsrec_controller_set_target(ctrl, ctx->argv[2]);
1481 ovsrec_bridge_set_controller(br->br_cfg, ctrl);
1488 cmd_get_fail_mode(struct vsctl_context *ctx)
1490 struct vsctl_info info;
1491 const char *fail_mode = NULL;
1493 get_info(ctx->ovs, &info);
1495 if (ctx->argc == 1) {
1496 /* Return the fail-mode from the "Open_vSwitch" table */
1497 if (info.ctrl && info.ctrl->fail_mode) {
1498 fail_mode = info.ctrl->fail_mode;
1501 /* Return the fail-mode for a particular bridge. */
1502 struct vsctl_bridge *br = find_bridge(&info, ctx->argv[1], true);
1504 /* If no controller or fail-mode is explicitly defined for the
1505 * requested bridge, fallback to the "Open_vSwitch" table's
1507 if (br->ctrl && br->ctrl->fail_mode) {
1508 fail_mode = br->ctrl->fail_mode;
1509 } else if (info.ctrl && info.ctrl->fail_mode) {
1510 fail_mode = info.ctrl->fail_mode;
1514 if (fail_mode && strlen(fail_mode)) {
1515 ds_put_format(&ctx->output, "%s\n", fail_mode);
1522 cmd_del_fail_mode(struct vsctl_context *ctx)
1524 struct vsctl_info info;
1526 get_info(ctx->ovs, &info);
1528 if (ctx->argc == 1) {
1529 if (info.ctrl && info.ctrl->fail_mode) {
1530 ovsrec_controller_set_fail_mode(info.ctrl, NULL);
1533 struct vsctl_bridge *br = find_real_bridge(&info, ctx->argv[1], true);
1535 if (br->ctrl && br->ctrl->fail_mode) {
1536 ovsrec_controller_set_fail_mode(br->ctrl, NULL);
1544 cmd_set_fail_mode(struct vsctl_context *ctx)
1546 struct vsctl_info info;
1547 const char *fail_mode;
1549 get_info(ctx->ovs, &info);
1551 fail_mode = (ctx->argc == 2) ? ctx->argv[1] : ctx->argv[2];
1553 if (strcmp(fail_mode, "standalone") && strcmp(fail_mode, "secure")) {
1554 vsctl_fatal("fail-mode must be \"standalone\" or \"secure\"");
1557 if (ctx->argc == 2) {
1558 /* Set the fail-mode in the "Open_vSwitch" table. */
1560 vsctl_fatal("no controller declared");
1562 ovsrec_controller_set_fail_mode(info.ctrl, fail_mode);
1564 struct vsctl_bridge *br = find_real_bridge(&info, ctx->argv[1], true);
1567 vsctl_fatal("no controller declared for %s", br->name);
1569 ovsrec_controller_set_fail_mode(br->ctrl, fail_mode);
1576 cmd_get_ssl(struct vsctl_context *ctx)
1578 struct ovsrec_ssl *ssl = ctx->ovs->ssl;
1581 ds_put_format(&ctx->output, "Private key: %s\n", ssl->private_key);
1582 ds_put_format(&ctx->output, "Certificate: %s\n", ssl->certificate);
1583 ds_put_format(&ctx->output, "CA Certificate: %s\n", ssl->ca_cert);
1584 ds_put_format(&ctx->output, "Bootstrap: %s\n",
1585 ssl->bootstrap_ca_cert ? "true" : "false");
1590 cmd_del_ssl(struct vsctl_context *ctx)
1592 struct ovsrec_ssl *ssl = ctx->ovs->ssl;
1595 ovsrec_ssl_delete(ssl);
1596 ovsrec_open_vswitch_set_ssl(ctx->ovs, NULL);
1601 cmd_set_ssl(struct vsctl_context *ctx)
1603 bool bootstrap = shash_find(&ctx->options, "--bootstrap");
1604 struct ovsrec_ssl *ssl = ctx->ovs->ssl;
1607 ovsrec_ssl_delete(ssl);
1609 ssl = ovsrec_ssl_insert(ctx->txn);
1611 ovsrec_ssl_set_private_key(ssl, ctx->argv[1]);
1612 ovsrec_ssl_set_certificate(ssl, ctx->argv[2]);
1613 ovsrec_ssl_set_ca_cert(ssl, ctx->argv[3]);
1615 ovsrec_ssl_set_bootstrap_ca_cert(ssl, bootstrap);
1617 ovsrec_open_vswitch_set_ssl(ctx->ovs, ssl);
1620 /* Parameter commands. */
1622 struct vsctl_row_id {
1623 const struct ovsdb_idl_table_class *table;
1624 const struct ovsdb_idl_column *name_column;
1625 const struct ovsdb_idl_column *uuid_column;
1628 struct vsctl_table_class {
1629 struct ovsdb_idl_table_class *class;
1630 struct vsctl_row_id row_ids[2];
1633 static const struct vsctl_table_class tables[] = {
1634 {&ovsrec_table_bridge,
1635 {{&ovsrec_table_bridge, &ovsrec_bridge_col_name, NULL},
1636 {NULL, NULL, NULL}}},
1638 {&ovsrec_table_controller,
1639 {{&ovsrec_table_bridge,
1640 &ovsrec_bridge_col_name,
1641 &ovsrec_bridge_col_controller},
1642 {&ovsrec_table_open_vswitch,
1644 &ovsrec_open_vswitch_col_controller}}},
1646 {&ovsrec_table_interface,
1647 {{&ovsrec_table_interface, &ovsrec_interface_col_name, NULL},
1648 {NULL, NULL, NULL}}},
1650 {&ovsrec_table_mirror,
1651 {{&ovsrec_table_mirror, &ovsrec_mirror_col_name, NULL},
1652 {NULL, NULL, NULL}}},
1654 {&ovsrec_table_netflow,
1655 {{&ovsrec_table_bridge,
1656 &ovsrec_bridge_col_name,
1657 &ovsrec_bridge_col_netflow},
1658 {NULL, NULL, NULL}}},
1660 {&ovsrec_table_open_vswitch,
1661 {{&ovsrec_table_open_vswitch, NULL, NULL},
1662 {NULL, NULL, NULL}}},
1664 {&ovsrec_table_port,
1665 {{&ovsrec_table_port, &ovsrec_port_col_name, NULL},
1666 {NULL, NULL, NULL}}},
1669 {{&ovsrec_table_open_vswitch, NULL, &ovsrec_open_vswitch_col_ssl}}},
1671 {NULL, {{NULL, NULL, NULL}, {NULL, NULL, NULL}}}
1675 die_if_error(char *error)
1678 vsctl_fatal("%s", error);
1683 to_lower_and_underscores(unsigned c)
1685 return c == '-' ? '_' : tolower(c);
1689 score_partial_match(const char *name, const char *s)
1693 if (!strcmp(name, s)) {
1696 for (score = 0; ; score++, name++, s++) {
1697 if (to_lower_and_underscores(*name) != to_lower_and_underscores(*s)) {
1699 } else if (*name == '\0') {
1700 return UINT_MAX - 1;
1703 return *s == '\0' ? score : 0;
1706 static const struct vsctl_table_class *
1707 get_table(const char *table_name)
1709 const struct vsctl_table_class *table;
1710 const struct vsctl_table_class *best_match = NULL;
1711 unsigned int best_score = 0;
1713 for (table = tables; table->class; table++) {
1714 unsigned int score = score_partial_match(table->class->name,
1716 if (score > best_score) {
1719 } else if (score == best_score) {
1725 } else if (best_score) {
1726 vsctl_fatal("multiple table names match \"%s\"", table_name);
1728 vsctl_fatal("unknown table \"%s\"", table_name);
1732 static const struct ovsdb_idl_row *
1733 get_row_by_id(struct vsctl_context *ctx, const struct vsctl_table_class *table,
1734 const struct vsctl_row_id *id, const char *record_id)
1736 const struct ovsdb_idl_row *referrer, *final;
1742 if (!id->name_column) {
1743 if (strcmp(record_id, ".")) {
1746 referrer = ovsdb_idl_first_row(ctx->idl, id->table);
1747 if (!referrer || ovsdb_idl_next_row(referrer)) {
1751 const struct ovsdb_idl_row *row;
1752 unsigned int best_score = 0;
1754 /* It might make sense to relax this assertion. */
1755 assert(id->name_column->type.key.type == OVSDB_TYPE_STRING);
1758 for (row = ovsdb_idl_first_row(ctx->idl, id->table);
1759 row != NULL && best_score != UINT_MAX;
1760 row = ovsdb_idl_next_row(row))
1762 struct ovsdb_datum name;
1764 ovsdb_idl_txn_read(row, id->name_column, &name);
1766 unsigned int score = score_partial_match(name.keys[0].string,
1768 if (score > best_score) {
1771 } else if (score == best_score) {
1775 ovsdb_datum_destroy(&name, &id->name_column->type);
1777 if (best_score && !referrer) {
1778 vsctl_fatal("multiple rows in %s match \"%s\"",
1779 table->class->name, record_id);
1787 if (id->uuid_column) {
1788 struct ovsdb_datum uuid;
1790 assert(id->uuid_column->type.key.type == OVSDB_TYPE_UUID);
1791 assert(id->uuid_column->type.value.type == OVSDB_TYPE_VOID);
1793 ovsdb_idl_txn_read(referrer, id->uuid_column, &uuid);
1795 final = ovsdb_idl_get_row_for_uuid(ctx->idl, table->class,
1796 &uuid.keys[0].uuid);
1798 ovsdb_datum_destroy(&uuid, &id->uuid_column->type);
1806 static const struct ovsdb_idl_row *
1807 get_row(struct vsctl_context *ctx,
1808 const struct vsctl_table_class *table, const char *record_id)
1810 const struct ovsdb_idl_row *row;
1813 if (uuid_from_string(&uuid, record_id)) {
1814 row = ovsdb_idl_get_row_for_uuid(ctx->idl, table->class, &uuid);
1818 for (i = 0; i < ARRAY_SIZE(table->row_ids); i++) {
1819 row = get_row_by_id(ctx, table, &table->row_ids[i], record_id);
1828 static const struct ovsdb_idl_row *
1829 must_get_row(struct vsctl_context *ctx,
1830 const struct vsctl_table_class *table, const char *record_id)
1832 const struct ovsdb_idl_row *row = get_row(ctx, table, record_id);
1834 vsctl_fatal("no row \"%s\" in table %s",
1835 record_id, table->class->name);
1841 get_column(const struct vsctl_table_class *table, const char *column_name,
1842 const struct ovsdb_idl_column **columnp)
1844 const struct ovsdb_idl_column *best_match = NULL;
1845 unsigned int best_score = 0;
1848 for (i = 0; i < table->class->n_columns; i++) {
1849 const struct ovsdb_idl_column *column = &table->class->columns[i];
1850 unsigned int score = score_partial_match(column->name, column_name);
1851 if (score > best_score) {
1852 best_match = column;
1854 } else if (score == best_score) {
1859 *columnp = best_match;
1862 } else if (best_score) {
1863 return xasprintf("%s contains more than one column whose name "
1864 "matches \"%s\"", table->class->name, column_name);
1866 return xasprintf("%s does not contain a column whose name matches "
1867 "\"%s\"", table->class->name, column_name);
1871 static char * WARN_UNUSED_RESULT
1872 parse_column_key_value(const char *arg, const struct vsctl_table_class *table,
1873 const struct ovsdb_idl_column **columnp,
1874 char **keyp, char **valuep)
1876 const char *p = arg;
1879 assert(columnp || keyp);
1887 /* Parse column name. */
1891 error = ovsdb_token_parse(&p, &column_name);
1895 if (column_name[0] == '\0') {
1897 error = xasprintf("%s: missing column name", arg);
1900 error = get_column(table, column_name, columnp);
1907 /* Parse key string. */
1908 if (*p == ':' || !columnp) {
1912 error = xasprintf("%s: key not accepted here", arg);
1915 error = ovsdb_token_parse(&p, keyp);
1923 /* Parse value string. */
1926 error = xasprintf("%s: value not accepted here", arg);
1929 *valuep = xstrdup(p + 1);
1935 error = xasprintf("%s: trailing garbage \"%s\" in argument",
1958 cmd_get(struct vsctl_context *ctx)
1960 bool if_exists = shash_find(&ctx->options, "--if-exists");
1961 const char *table_name = ctx->argv[1];
1962 const char *record_id = ctx->argv[2];
1963 const struct vsctl_table_class *table;
1964 const struct ovsdb_idl_row *row;
1965 struct ds *out = &ctx->output;
1968 table = get_table(table_name);
1969 row = must_get_row(ctx, table, record_id);
1970 for (i = 3; i < ctx->argc; i++) {
1971 const struct ovsdb_idl_column *column;
1972 struct ovsdb_datum datum;
1975 die_if_error(parse_column_key_value(ctx->argv[i], table,
1976 &column, &key_string, NULL));
1978 ovsdb_idl_txn_read(row, column, &datum);
1980 union ovsdb_atom key;
1983 if (column->type.value.type == OVSDB_TYPE_VOID) {
1984 vsctl_fatal("cannot specify key to get for non-map column %s",
1988 die_if_error(ovsdb_atom_from_string(&key,
1992 idx = ovsdb_datum_find_key(&datum, &key,
1993 column->type.key.type);
1994 if (idx == UINT_MAX) {
1996 vsctl_fatal("no key \"%s\" in %s record \"%s\" column %s",
1997 key_string, table->class->name, record_id,
2001 ovsdb_atom_to_string(&datum.values[idx],
2002 column->type.value.type, out);
2004 ovsdb_atom_destroy(&key, column->type.key.type);
2006 ovsdb_datum_to_string(&datum, &column->type, out);
2008 ds_put_char(out, '\n');
2009 ovsdb_datum_destroy(&datum, &column->type);
2016 list_record(const struct vsctl_table_class *table,
2017 const struct ovsdb_idl_row *row, struct ds *out)
2021 ds_put_format(out, "%-20s: "UUID_FMT"\n", "_uuid",
2022 UUID_ARGS(&row->uuid));
2023 for (i = 0; i < table->class->n_columns; i++) {
2024 const struct ovsdb_idl_column *column = &table->class->columns[i];
2025 struct ovsdb_datum datum;
2027 ovsdb_idl_txn_read(row, column, &datum);
2029 ds_put_format(out, "%-20s: ", column->name);
2030 ovsdb_datum_to_string(&datum, &column->type, out);
2031 ds_put_char(out, '\n');
2033 ovsdb_datum_destroy(&datum, &column->type);
2038 cmd_list(struct vsctl_context *ctx)
2040 const char *table_name = ctx->argv[1];
2041 const struct vsctl_table_class *table;
2042 struct ds *out = &ctx->output;
2045 table = get_table(table_name);
2046 if (ctx->argc > 2) {
2047 for (i = 2; i < ctx->argc; i++) {
2049 ds_put_char(out, '\n');
2051 list_record(table, must_get_row(ctx, table, ctx->argv[i]), out);
2054 const struct ovsdb_idl_row *row;
2057 for (row = ovsdb_idl_first_row(ctx->idl, table->class), first = true;
2059 row = ovsdb_idl_next_row(row), first = false) {
2061 ds_put_char(out, '\n');
2063 list_record(table, row, out);
2069 set_column(const struct vsctl_table_class *table,
2070 const struct ovsdb_idl_row *row, const char *arg)
2072 const struct ovsdb_idl_column *column;
2073 char *key_string, *value_string;
2076 error = parse_column_key_value(arg, table, &column, &key_string,
2078 die_if_error(error);
2079 if (!value_string) {
2080 vsctl_fatal("%s: missing value", arg);
2084 union ovsdb_atom key, value;
2085 struct ovsdb_datum old, new;
2087 if (column->type.value.type == OVSDB_TYPE_VOID) {
2088 vsctl_fatal("cannot specify key to set for non-map column %s",
2092 die_if_error(ovsdb_atom_from_string(&key, &column->type.key,
2094 die_if_error(ovsdb_atom_from_string(&value, &column->type.value,
2097 ovsdb_datum_init_empty(&new);
2098 ovsdb_datum_add_unsafe(&new, &key, &value, &column->type);
2100 ovsdb_atom_destroy(&key, column->type.key.type);
2101 ovsdb_atom_destroy(&value, column->type.value.type);
2103 ovsdb_idl_txn_read(row, column, &old);
2104 ovsdb_datum_union(&old, &new, &column->type, true);
2105 ovsdb_idl_txn_write(row, column, &old);
2107 ovsdb_datum_destroy(&new, &column->type);
2109 struct ovsdb_datum datum;
2111 die_if_error(ovsdb_datum_from_string(&datum, &column->type,
2113 ovsdb_idl_txn_write(row, column, &datum);
2121 cmd_set(struct vsctl_context *ctx)
2123 const char *table_name = ctx->argv[1];
2124 const char *record_id = ctx->argv[2];
2125 const struct vsctl_table_class *table;
2126 const struct ovsdb_idl_row *row;
2129 table = get_table(table_name);
2130 row = must_get_row(ctx, table, record_id);
2131 for (i = 3; i < ctx->argc; i++) {
2132 set_column(table, row, ctx->argv[i]);
2137 cmd_add(struct vsctl_context *ctx)
2139 const char *table_name = ctx->argv[1];
2140 const char *record_id = ctx->argv[2];
2141 const char *column_name = ctx->argv[3];
2142 const struct vsctl_table_class *table;
2143 const struct ovsdb_idl_column *column;
2144 const struct ovsdb_idl_row *row;
2145 const struct ovsdb_type *type;
2146 struct ovsdb_datum old;
2149 table = get_table(table_name);
2150 row = must_get_row(ctx, table, record_id);
2151 die_if_error(get_column(table, column_name, &column));
2153 type = &column->type;
2154 ovsdb_idl_txn_read(row, column, &old);
2155 for (i = 4; i < ctx->argc; i++) {
2156 struct ovsdb_type add_type;
2157 struct ovsdb_datum add;
2161 add_type.n_max = UINT_MAX;
2162 die_if_error(ovsdb_datum_from_string(&add, &add_type, ctx->argv[i]));
2163 ovsdb_datum_union(&old, &add, type, false);
2164 ovsdb_datum_destroy(&add, type);
2166 if (old.n > type->n_max) {
2167 vsctl_fatal("\"add\" operation would put %u %s in column %s of "
2168 "table %s but the maximum number is %u",
2170 type->value.type == OVSDB_TYPE_VOID ? "values" : "pairs",
2171 column->name, table->class->name, type->n_max);
2173 ovsdb_idl_txn_write(row, column, &old);
2177 cmd_remove(struct vsctl_context *ctx)
2179 const char *table_name = ctx->argv[1];
2180 const char *record_id = ctx->argv[2];
2181 const char *column_name = ctx->argv[3];
2182 const struct vsctl_table_class *table;
2183 const struct ovsdb_idl_column *column;
2184 const struct ovsdb_idl_row *row;
2185 const struct ovsdb_type *type;
2186 struct ovsdb_datum old;
2189 table = get_table(table_name);
2190 row = must_get_row(ctx, table, record_id);
2191 die_if_error(get_column(table, column_name, &column));
2193 type = &column->type;
2194 ovsdb_idl_txn_read(row, column, &old);
2195 for (i = 4; i < ctx->argc; i++) {
2196 struct ovsdb_type rm_type;
2197 struct ovsdb_datum rm;
2202 rm_type.n_max = UINT_MAX;
2203 error = ovsdb_datum_from_string(&rm, &rm_type, ctx->argv[i]);
2204 if (error && ovsdb_type_is_map(&rm_type)) {
2206 rm_type.value.type = OVSDB_TYPE_VOID;
2207 die_if_error(ovsdb_datum_from_string(&rm, &rm_type, ctx->argv[i]));
2209 ovsdb_datum_subtract(&old, type, &rm, &rm_type);
2210 ovsdb_datum_destroy(&rm, &rm_type);
2212 if (old.n < type->n_min) {
2213 vsctl_fatal("\"remove\" operation would put %u %s in column %s of "
2214 "table %s but the minimun number is %u",
2216 type->value.type == OVSDB_TYPE_VOID ? "values" : "pairs",
2217 column->name, table->class->name, type->n_min);
2219 ovsdb_idl_txn_write(row, column, &old);
2223 cmd_clear(struct vsctl_context *ctx)
2225 const char *table_name = ctx->argv[1];
2226 const char *record_id = ctx->argv[2];
2227 const struct vsctl_table_class *table;
2228 const struct ovsdb_idl_row *row;
2231 table = get_table(table_name);
2232 row = must_get_row(ctx, table, record_id);
2233 for (i = 3; i < ctx->argc; i++) {
2234 const struct ovsdb_idl_column *column;
2235 const struct ovsdb_type *type;
2236 struct ovsdb_datum datum;
2238 die_if_error(get_column(table, ctx->argv[i], &column));
2240 type = &column->type;
2241 if (type->n_min > 0) {
2242 vsctl_fatal("\"clear\" operation cannot be applied to column %s "
2243 "of table %s, which is not allowed to be empty",
2244 column->name, table->class->name);
2247 ovsdb_datum_init_empty(&datum);
2248 ovsdb_idl_txn_write(row, column, &datum);
2253 cmd_create(struct vsctl_context *ctx)
2255 const char *table_name = ctx->argv[1];
2256 const struct vsctl_table_class *table;
2257 const struct ovsdb_idl_row *row;
2260 table = get_table(table_name);
2261 row = ovsdb_idl_txn_insert(ctx->txn, table->class);
2262 for (i = 2; i < ctx->argc; i++) {
2263 set_column(table, row, ctx->argv[i]);
2265 ds_put_format(&ctx->output, UUID_FMT, UUID_ARGS(&row->uuid));
2268 /* This function may be used as the 'postprocess' function for commands that
2269 * insert new rows into the database. It expects that the command's 'run'
2270 * function prints the UUID reported by ovsdb_idl_txn_insert() as the command's
2271 * sole output. It replaces that output by the row's permanent UUID assigned
2272 * by the database server and appends a new-line.
2274 * Currently we use this only for "create", because the higher-level commands
2275 * are supposed to be independent of the actual structure of the vswitch
2278 post_create(struct vsctl_context *ctx)
2280 const struct uuid *real;
2283 uuid_from_string(&dummy, ds_cstr(&ctx->output));
2284 real = ovsdb_idl_txn_get_insert_uuid(ctx->txn, &dummy);
2286 ds_clear(&ctx->output);
2287 ds_put_format(&ctx->output, UUID_FMT, UUID_ARGS(real));
2289 ds_put_char(&ctx->output, '\n');
2293 cmd_destroy(struct vsctl_context *ctx)
2295 bool must_exist = !shash_find(&ctx->options, "--if-exists");
2296 const char *table_name = ctx->argv[1];
2297 const struct vsctl_table_class *table;
2300 table = get_table(table_name);
2301 for (i = 2; i < ctx->argc; i++) {
2302 const struct ovsdb_idl_row *row;
2304 row = (must_exist ? must_get_row : get_row)(ctx, table, ctx->argv[i]);
2306 ovsdb_idl_txn_delete(row);
2311 static struct json *
2312 where_uuid_equals(const struct uuid *uuid)
2315 json_array_create_1(
2316 json_array_create_3(
2317 json_string_create("_uuid"),
2318 json_string_create("=="),
2319 json_array_create_2(
2320 json_string_create("uuid"),
2321 json_string_create_nocopy(
2322 xasprintf(UUID_FMT, UUID_ARGS(uuid))))));
2326 vsctl_context_init(struct vsctl_context *ctx, struct vsctl_command *command,
2327 struct ovsdb_idl *idl, struct ovsdb_idl_txn *txn,
2328 const struct ovsrec_open_vswitch *ovs)
2330 ctx->argc = command->argc;
2331 ctx->argv = command->argv;
2332 ctx->options = command->options;
2334 ds_swap(&ctx->output, &command->output);
2342 vsctl_context_done(struct vsctl_context *ctx, struct vsctl_command *command)
2344 ds_swap(&ctx->output, &command->output);
2348 do_vsctl(const char *args, struct vsctl_command *commands, size_t n_commands,
2349 struct ovsdb_idl *idl)
2351 struct ovsdb_idl_txn *txn;
2352 const struct ovsrec_open_vswitch *ovs;
2353 enum ovsdb_idl_txn_status status;
2354 struct vsctl_command *c;
2355 int64_t next_cfg = 0;
2359 txn = the_idl_txn = ovsdb_idl_txn_create(idl);
2361 ovsdb_idl_txn_set_dry_run(txn);
2364 comment = xasprintf("ovs-vsctl: %s", args);
2365 ovsdb_idl_txn_add_comment(txn, comment);
2368 ovs = ovsrec_open_vswitch_first(idl);
2370 /* XXX add verification that table is empty */
2371 ovs = ovsrec_open_vswitch_insert(txn);
2374 if (wait_for_reload) {
2375 struct json *where = where_uuid_equals(&ovs->header_.uuid);
2376 ovsdb_idl_txn_increment(txn, "Open_vSwitch", "next_cfg", where);
2377 json_destroy(where);
2380 for (c = commands; c < &commands[n_commands]; c++) {
2381 struct vsctl_context ctx;
2383 ds_init(&c->output);
2384 vsctl_context_init(&ctx, c, idl, txn, ovs);
2385 (c->syntax->run)(&ctx);
2386 vsctl_context_done(&ctx, c);
2389 while ((status = ovsdb_idl_txn_commit(txn)) == TXN_INCOMPLETE) {
2391 ovsdb_idl_wait(idl);
2392 ovsdb_idl_txn_wait(txn);
2395 if (wait_for_reload && status == TXN_SUCCESS) {
2396 next_cfg = ovsdb_idl_txn_get_increment_new_value(txn);
2398 for (c = commands; c < &commands[n_commands]; c++) {
2399 if (c->syntax->postprocess) {
2400 struct vsctl_context ctx;
2402 vsctl_context_init(&ctx, c, idl, txn, ovs);
2403 (c->syntax->postprocess)(&ctx);
2404 vsctl_context_done(&ctx, c);
2407 error = xstrdup(ovsdb_idl_txn_get_error(txn));
2408 ovsdb_idl_txn_destroy(txn);
2412 case TXN_INCOMPLETE:
2416 /* Should not happen--we never call ovsdb_idl_txn_abort(). */
2417 vsctl_fatal("transaction aborted");
2424 for (c = commands; c < &commands[n_commands]; c++) {
2425 ds_destroy(&c->output);
2431 vsctl_fatal("transaction error: %s", error);
2438 for (c = commands; c < &commands[n_commands]; c++) {
2439 struct ds *ds = &c->output;
2444 for (j = 0; j < ds->length; j++) {
2445 int c = ds->string[j];
2448 fputs("\\n", stdout);
2452 fputs("\\\\", stdout);
2461 fputs(ds_cstr(ds), stdout);
2463 ds_destroy(&c->output);
2464 shash_destroy(&c->options);
2468 if (wait_for_reload && status != TXN_UNCHANGED) {
2470 const struct ovsrec_open_vswitch *ovs;
2473 OVSREC_OPEN_VSWITCH_FOR_EACH (ovs, idl) {
2474 if (ovs->cur_cfg >= next_cfg) {
2478 ovsdb_idl_wait(idl);
2483 ovsdb_idl_destroy(idl);
2488 static const struct vsctl_command_syntax all_commands[] = {
2489 /* Open vSwitch commands. */
2490 {"init", 0, 0, cmd_init, NULL, ""},
2492 /* Bridge commands. */
2493 {"add-br", 1, 3, cmd_add_br, NULL, "--may-exist"},
2494 {"del-br", 1, 1, cmd_del_br, NULL, "--if-exists"},
2495 {"list-br", 0, 0, cmd_list_br, NULL, ""},
2496 {"br-exists", 1, 1, cmd_br_exists, NULL, ""},
2497 {"br-to-vlan", 1, 1, cmd_br_to_vlan, NULL, ""},
2498 {"br-to-parent", 1, 1, cmd_br_to_parent, NULL, ""},
2499 {"br-set-external-id", 2, 3, cmd_br_set_external_id, NULL, ""},
2500 {"br-get-external-id", 1, 2, cmd_br_get_external_id, NULL, ""},
2502 /* Port commands. */
2503 {"list-ports", 1, 1, cmd_list_ports, NULL, ""},
2504 {"add-port", 2, 2, cmd_add_port, NULL, "--may-exist"},
2505 {"add-bond", 4, INT_MAX, cmd_add_bond, NULL, "--may-exist,--fake-iface"},
2506 {"del-port", 1, 2, cmd_del_port, NULL, "--if-exists,--with-iface"},
2507 {"port-to-br", 1, 1, cmd_port_to_br, NULL, ""},
2509 /* Interface commands. */
2510 {"list-ifaces", 1, 1, cmd_list_ifaces, NULL, ""},
2511 {"iface-to-br", 1, 1, cmd_iface_to_br, NULL, ""},
2513 /* Controller commands. */
2514 {"get-controller", 0, 1, cmd_get_controller, NULL, ""},
2515 {"del-controller", 0, 1, cmd_del_controller, NULL, ""},
2516 {"set-controller", 1, 2, cmd_set_controller, NULL, ""},
2517 {"get-fail-mode", 0, 1, cmd_get_fail_mode, NULL, ""},
2518 {"del-fail-mode", 0, 1, cmd_del_fail_mode, NULL, ""},
2519 {"set-fail-mode", 1, 2, cmd_set_fail_mode, NULL, ""},
2522 {"get-ssl", 0, 0, cmd_get_ssl, NULL, ""},
2523 {"del-ssl", 0, 0, cmd_del_ssl, NULL, ""},
2524 {"set-ssl", 3, 3, cmd_set_ssl, NULL, "--bootstrap"},
2526 /* Parameter commands. */
2527 {"get", 3, INT_MAX, cmd_get, NULL, "--if-exists"},
2528 {"list", 1, INT_MAX, cmd_list, NULL, ""},
2529 {"set", 3, INT_MAX, cmd_set, NULL, ""},
2530 {"add", 4, INT_MAX, cmd_add, NULL, ""},
2531 {"remove", 4, INT_MAX, cmd_remove, NULL, ""},
2532 {"clear", 3, INT_MAX, cmd_clear, NULL, ""},
2533 {"create", 2, INT_MAX, cmd_create, post_create, ""},
2534 {"destroy", 1, INT_MAX, cmd_destroy, NULL, "--if-exists"},
2536 {NULL, 0, 0, NULL, NULL, NULL},