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 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 = ctx->argv[1];
836 const char *parent_name = ctx->argc > 2 ? ctx->argv[2] : NULL;
837 int vlan = ctx->argc > 3 ? atoi(ctx->argv[3]) : 0;
838 struct vsctl_info info;
840 br_name = ctx->argv[1];
841 if (ctx->argc == 2) {
844 } else if (ctx->argc == 4) {
845 parent_name = ctx->argv[2];
846 vlan = atoi(ctx->argv[3]);
847 if (vlan < 1 || vlan > 4095) {
848 vsctl_fatal("%s: vlan must be between 1 and 4095", ctx->argv[0]);
851 vsctl_fatal("'%s' command takes exactly 1 or 3 arguments",
855 get_info(ctx->ovs, &info);
857 struct vsctl_bridge *br;
859 br = find_bridge(&info, br_name, false);
863 vsctl_fatal("\"--may-exist add-br %s\" but %s is "
864 "a VLAN bridge for VLAN %d",
865 br_name, br_name, br->vlan);
869 vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
870 "is not a VLAN bridge",
871 br_name, parent_name, vlan, br_name);
872 } else if (strcmp(br->parent->name, parent_name)) {
873 vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
874 "has the wrong parent %s",
875 br_name, parent_name, vlan,
876 br_name, br->parent->name);
877 } else if (br->vlan != vlan) {
878 vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
879 "is a VLAN bridge for the wrong VLAN %d",
880 br_name, parent_name, vlan, br_name, br->vlan);
886 check_conflicts(&info, br_name,
887 xasprintf("cannot create a bridge named %s", br_name));
890 struct ovsrec_port *port;
891 struct ovsrec_interface *iface;
892 struct ovsrec_bridge *br;
894 iface = ovsrec_interface_insert(ctx->txn);
895 ovsrec_interface_set_name(iface, br_name);
897 port = ovsrec_port_insert(ctx->txn);
898 ovsrec_port_set_name(port, br_name);
899 ovsrec_port_set_interfaces(port, &iface, 1);
901 br = ovsrec_bridge_insert(ctx->txn);
902 ovsrec_bridge_set_name(br, br_name);
903 ovsrec_bridge_set_ports(br, &port, 1);
905 ovs_insert_bridge(ctx->ovs, br);
907 struct vsctl_bridge *parent;
908 struct ovsrec_port *port;
909 struct ovsrec_interface *iface;
910 struct ovsrec_bridge *br;
913 parent = find_bridge(&info, parent_name, false);
914 if (parent && parent->vlan) {
915 vsctl_fatal("cannot create bridge with fake bridge as parent");
918 vsctl_fatal("parent bridge %s does not exist", parent_name);
922 iface = ovsrec_interface_insert(ctx->txn);
923 ovsrec_interface_set_name(iface, br_name);
924 ovsrec_interface_set_type(iface, "internal");
926 port = ovsrec_port_insert(ctx->txn);
927 ovsrec_port_set_name(port, br_name);
928 ovsrec_port_set_interfaces(port, &iface, 1);
929 ovsrec_port_set_fake_bridge(port, true);
930 ovsrec_port_set_tag(port, &tag, 1);
932 bridge_insert_port(br, port);
939 del_port(struct vsctl_info *info, struct vsctl_port *port)
941 struct shash_node *node;
943 SHASH_FOR_EACH (node, &info->ifaces) {
944 struct vsctl_iface *iface = node->data;
945 if (iface->port == port) {
946 ovsrec_interface_delete(iface->iface_cfg);
949 ovsrec_port_delete(port->port_cfg);
951 bridge_delete_port((port->bridge->parent
952 ? port->bridge->parent->br_cfg
953 : port->bridge->br_cfg), port->port_cfg);
957 cmd_del_br(struct vsctl_context *ctx)
959 bool must_exist = !shash_find(&ctx->options, "--if-exists");
960 struct vsctl_bridge *bridge;
961 struct vsctl_info info;
963 get_info(ctx->ovs, &info);
964 bridge = find_bridge(&info, ctx->argv[1], must_exist);
966 struct shash_node *node;
968 SHASH_FOR_EACH (node, &info.ports) {
969 struct vsctl_port *port = node->data;
970 if (port->bridge == bridge || port->bridge->parent == bridge
971 || !strcmp(port->port_cfg->name, bridge->name)) {
972 del_port(&info, port);
975 if (bridge->br_cfg) {
976 ovsrec_bridge_delete(bridge->br_cfg);
977 ovs_delete_bridge(ctx->ovs, bridge->br_cfg);
984 output_sorted(struct svec *svec, struct ds *output)
990 SVEC_FOR_EACH (i, name, svec) {
991 ds_put_format(output, "%s\n", name);
996 cmd_list_br(struct vsctl_context *ctx)
998 struct shash_node *node;
999 struct vsctl_info info;
1000 struct svec bridges;
1002 get_info(ctx->ovs, &info);
1004 svec_init(&bridges);
1005 SHASH_FOR_EACH (node, &info.bridges) {
1006 struct vsctl_bridge *br = node->data;
1007 svec_add(&bridges, br->name);
1009 output_sorted(&bridges, &ctx->output);
1010 svec_destroy(&bridges);
1016 cmd_br_exists(struct vsctl_context *ctx)
1018 struct vsctl_info info;
1020 get_info(ctx->ovs, &info);
1021 if (!find_bridge(&info, ctx->argv[1], false)) {
1027 /* Returns true if 'b_prefix' (of length 'b_prefix_len') concatenated with 'b'
1028 * equals 'a', false otherwise. */
1030 key_matches(const char *a,
1031 const char *b_prefix, size_t b_prefix_len, const char *b)
1033 return !strncmp(a, b_prefix, b_prefix_len) && !strcmp(a + b_prefix_len, b);
1037 set_external_id(char **old_keys, char **old_values, size_t old_n,
1038 char *key, char *value,
1039 char ***new_keysp, char ***new_valuesp, size_t *new_np)
1046 new_keys = xmalloc(sizeof *new_keys * (old_n + 1));
1047 new_values = xmalloc(sizeof *new_values * (old_n + 1));
1049 for (i = 0; i < old_n; i++) {
1050 if (strcmp(key, old_keys[i])) {
1051 new_keys[new_n] = old_keys[i];
1052 new_values[new_n] = old_values[i];
1057 new_keys[new_n] = key;
1058 new_values[new_n] = value;
1061 *new_keysp = new_keys;
1062 *new_valuesp = new_values;
1067 cmd_br_set_external_id(struct vsctl_context *ctx)
1069 struct vsctl_info info;
1070 struct vsctl_bridge *bridge;
1071 char **keys, **values;
1074 get_info(ctx->ovs, &info);
1075 bridge = find_bridge(&info, ctx->argv[1], true);
1076 if (bridge->br_cfg) {
1077 set_external_id(bridge->br_cfg->key_external_ids,
1078 bridge->br_cfg->value_external_ids,
1079 bridge->br_cfg->n_external_ids,
1080 ctx->argv[2], ctx->argc >= 4 ? ctx->argv[3] : NULL,
1081 &keys, &values, &n);
1082 ovsrec_bridge_set_external_ids(bridge->br_cfg, keys, values, n);
1084 char *key = xasprintf("fake-bridge-%s", ctx->argv[2]);
1085 struct vsctl_port *port = shash_find_data(&info.ports, ctx->argv[1]);
1086 set_external_id(port->port_cfg->key_external_ids,
1087 port->port_cfg->value_external_ids,
1088 port->port_cfg->n_external_ids,
1089 key, ctx->argc >= 4 ? ctx->argv[3] : NULL,
1090 &keys, &values, &n);
1091 ovsrec_port_set_external_ids(port->port_cfg, keys, values, n);
1101 get_external_id(char **keys, char **values, size_t n,
1102 const char *prefix, const char *key,
1105 size_t prefix_len = strlen(prefix);
1110 for (i = 0; i < n; i++) {
1111 if (!key && !strncmp(keys[i], prefix, prefix_len)) {
1112 svec_add_nocopy(&svec, xasprintf("%s=%s",
1113 keys[i] + prefix_len, values[i]));
1114 } else if (key_matches(keys[i], prefix, prefix_len, key)) {
1115 svec_add(&svec, values[i]);
1119 output_sorted(&svec, output);
1120 svec_destroy(&svec);
1124 cmd_br_get_external_id(struct vsctl_context *ctx)
1126 struct vsctl_info info;
1127 struct vsctl_bridge *bridge;
1129 get_info(ctx->ovs, &info);
1130 bridge = find_bridge(&info, ctx->argv[1], true);
1131 if (bridge->br_cfg) {
1132 get_external_id(bridge->br_cfg->key_external_ids,
1133 bridge->br_cfg->value_external_ids,
1134 bridge->br_cfg->n_external_ids,
1135 "", ctx->argc >= 3 ? ctx->argv[2] : NULL,
1138 struct vsctl_port *port = shash_find_data(&info.ports, ctx->argv[1]);
1139 get_external_id(port->port_cfg->key_external_ids,
1140 port->port_cfg->value_external_ids,
1141 port->port_cfg->n_external_ids,
1142 "fake-bridge-", ctx->argc >= 3 ? ctx->argv[2] : NULL, &ctx->output);
1149 cmd_list_ports(struct vsctl_context *ctx)
1151 struct vsctl_bridge *br;
1152 struct shash_node *node;
1153 struct vsctl_info info;
1156 get_info(ctx->ovs, &info);
1157 br = find_bridge(&info, ctx->argv[1], true);
1160 SHASH_FOR_EACH (node, &info.ports) {
1161 struct vsctl_port *port = node->data;
1163 if (strcmp(port->port_cfg->name, br->name) && br == port->bridge) {
1164 svec_add(&ports, port->port_cfg->name);
1167 output_sorted(&ports, &ctx->output);
1168 svec_destroy(&ports);
1174 add_port(struct vsctl_context *ctx,
1175 const char *br_name, const char *port_name,
1176 bool may_exist, bool fake_iface,
1177 char *iface_names[], int n_ifaces)
1179 struct vsctl_info info;
1180 struct vsctl_bridge *bridge;
1181 struct ovsrec_interface **ifaces;
1182 struct ovsrec_port *port;
1185 get_info(ctx->ovs, &info);
1187 struct vsctl_port *port;
1189 port = find_port(&info, port_name, false);
1191 struct svec want_names, have_names;
1194 svec_init(&want_names);
1195 for (i = 0; i < n_ifaces; i++) {
1196 svec_add(&want_names, iface_names[i]);
1198 svec_sort(&want_names);
1200 svec_init(&have_names);
1201 for (i = 0; i < port->port_cfg->n_interfaces; i++) {
1202 svec_add(&have_names, port->port_cfg->interfaces[i]->name);
1204 svec_sort(&have_names);
1206 if (strcmp(port->bridge->name, br_name)) {
1207 char *command = vsctl_context_to_string(ctx);
1208 vsctl_fatal("\"%s\" but %s is actually attached to bridge %s",
1209 command, port_name, port->bridge->name);
1212 if (!svec_equal(&want_names, &have_names)) {
1213 char *have_names_string = svec_join(&have_names, ", ", "");
1214 char *command = vsctl_context_to_string(ctx);
1216 vsctl_fatal("\"%s\" but %s actually has interface(s) %s",
1217 command, port_name, have_names_string);
1220 svec_destroy(&want_names);
1221 svec_destroy(&have_names);
1226 check_conflicts(&info, port_name,
1227 xasprintf("cannot create a port named %s", port_name));
1228 for (i = 0; i < n_ifaces; i++) {
1229 check_conflicts(&info, iface_names[i],
1230 xasprintf("cannot create an interface named %s",
1233 bridge = find_bridge(&info, br_name, true);
1235 ifaces = xmalloc(n_ifaces * sizeof *ifaces);
1236 for (i = 0; i < n_ifaces; i++) {
1237 ifaces[i] = ovsrec_interface_insert(ctx->txn);
1238 ovsrec_interface_set_name(ifaces[i], iface_names[i]);
1241 port = ovsrec_port_insert(ctx->txn);
1242 ovsrec_port_set_name(port, port_name);
1243 ovsrec_port_set_interfaces(port, ifaces, n_ifaces);
1244 ovsrec_port_set_bond_fake_iface(port, fake_iface);
1248 int64_t tag = bridge->vlan;
1249 ovsrec_port_set_tag(port, &tag, 1);
1252 bridge_insert_port((bridge->parent ? bridge->parent->br_cfg
1253 : bridge->br_cfg), port);
1259 cmd_add_port(struct vsctl_context *ctx)
1261 bool may_exist = shash_find(&ctx->options, "--may-exist") != 0;
1263 add_port(ctx, ctx->argv[1], ctx->argv[2], may_exist, false,
1268 cmd_add_bond(struct vsctl_context *ctx)
1270 bool may_exist = shash_find(&ctx->options, "--may-exist") != 0;
1271 bool fake_iface = shash_find(&ctx->options, "--fake-iface");
1273 add_port(ctx, ctx->argv[1], ctx->argv[2], may_exist, fake_iface,
1274 &ctx->argv[3], ctx->argc - 3);
1278 cmd_del_port(struct vsctl_context *ctx)
1280 bool must_exist = !shash_find(&ctx->options, "--if-exists");
1281 struct vsctl_info info;
1283 get_info(ctx->ovs, &info);
1284 if (ctx->argc == 2) {
1285 struct vsctl_port *port = find_port(&info, ctx->argv[1], must_exist);
1287 del_port(&info, port);
1289 } else if (ctx->argc == 3) {
1290 struct vsctl_bridge *bridge = find_bridge(&info, ctx->argv[1], true);
1291 struct vsctl_port *port = find_port(&info, ctx->argv[2], must_exist);
1294 if (port->bridge == bridge) {
1295 del_port(&info, port);
1296 } else if (port->bridge->parent == bridge) {
1297 vsctl_fatal("bridge %s does not have a port %s (although its "
1298 "parent bridge %s does)",
1299 ctx->argv[1], ctx->argv[2], bridge->parent->name);
1301 vsctl_fatal("bridge %s does not have a port %s",
1302 ctx->argv[1], ctx->argv[2]);
1310 cmd_port_to_br(struct vsctl_context *ctx)
1312 struct vsctl_port *port;
1313 struct vsctl_info info;
1315 get_info(ctx->ovs, &info);
1316 port = find_port(&info, ctx->argv[1], true);
1317 ds_put_format(&ctx->output, "%s\n", port->bridge->name);
1322 cmd_br_to_vlan(struct vsctl_context *ctx)
1324 struct vsctl_bridge *bridge;
1325 struct vsctl_info info;
1327 get_info(ctx->ovs, &info);
1328 bridge = find_bridge(&info, ctx->argv[1], true);
1329 ds_put_format(&ctx->output, "%d\n", bridge->vlan);
1334 cmd_br_to_parent(struct vsctl_context *ctx)
1336 struct vsctl_bridge *bridge;
1337 struct vsctl_info info;
1339 get_info(ctx->ovs, &info);
1340 bridge = find_bridge(&info, ctx->argv[1], true);
1341 if (bridge->parent) {
1342 bridge = bridge->parent;
1344 ds_put_format(&ctx->output, "%s\n", bridge->name);
1349 cmd_list_ifaces(struct vsctl_context *ctx)
1351 struct vsctl_bridge *br;
1352 struct shash_node *node;
1353 struct vsctl_info info;
1356 get_info(ctx->ovs, &info);
1357 br = find_bridge(&info, ctx->argv[1], true);
1360 SHASH_FOR_EACH (node, &info.ifaces) {
1361 struct vsctl_iface *iface = node->data;
1363 if (strcmp(iface->iface_cfg->name, br->name)
1364 && br == iface->port->bridge) {
1365 svec_add(&ifaces, iface->iface_cfg->name);
1368 output_sorted(&ifaces, &ctx->output);
1369 svec_destroy(&ifaces);
1375 cmd_iface_to_br(struct vsctl_context *ctx)
1377 struct vsctl_iface *iface;
1378 struct vsctl_info info;
1380 get_info(ctx->ovs, &info);
1381 iface = find_iface(&info, ctx->argv[1], true);
1382 ds_put_format(&ctx->output, "%s\n", iface->port->bridge->name);
1387 cmd_get_controller(struct vsctl_context *ctx)
1389 struct vsctl_info info;
1391 get_info(ctx->ovs, &info);
1393 if (ctx->argc == 1) {
1394 /* Return the controller from the "Open_vSwitch" table */
1396 ds_put_format(&ctx->output, "%s\n", info.ctrl->target);
1399 /* Return the controller for a particular bridge. */
1400 struct vsctl_bridge *br = find_bridge(&info, ctx->argv[1], true);
1402 /* If no controller is explicitly defined for the requested
1403 * bridge, fallback to the "Open_vSwitch" table's controller. */
1405 ds_put_format(&ctx->output, "%s\n", br->ctrl->target);
1406 } else if (info.ctrl) {
1407 ds_put_format(&ctx->output, "%s\n", info.ctrl->target);
1415 cmd_del_controller(struct vsctl_context *ctx)
1417 struct vsctl_info info;
1419 get_info(ctx->ovs, &info);
1421 if (ctx->argc == 1) {
1423 ovsrec_controller_delete(info.ctrl);
1424 ovsrec_open_vswitch_set_controller(ctx->ovs, NULL);
1427 struct vsctl_bridge *br = find_real_bridge(&info, ctx->argv[1], true);
1430 ovsrec_controller_delete(br->ctrl);
1431 ovsrec_bridge_set_controller(br->br_cfg, NULL);
1439 cmd_set_controller(struct vsctl_context *ctx)
1441 struct vsctl_info info;
1442 struct ovsrec_controller *ctrl;
1444 get_info(ctx->ovs, &info);
1446 if (ctx->argc == 2) {
1447 /* Set the controller in the "Open_vSwitch" table. */
1449 ovsrec_controller_delete(info.ctrl);
1451 ctrl = ovsrec_controller_insert(ctx->txn);
1452 ovsrec_controller_set_target(ctrl, ctx->argv[1]);
1453 ovsrec_open_vswitch_set_controller(ctx->ovs, ctrl);
1455 /* Set the controller for a particular bridge. */
1456 struct vsctl_bridge *br = find_real_bridge(&info, ctx->argv[1], true);
1459 ovsrec_controller_delete(br->ctrl);
1461 ctrl = ovsrec_controller_insert(ctx->txn);
1462 ovsrec_controller_set_target(ctrl, ctx->argv[2]);
1463 ovsrec_bridge_set_controller(br->br_cfg, ctrl);
1470 cmd_get_fail_mode(struct vsctl_context *ctx)
1472 struct vsctl_info info;
1473 const char *fail_mode = NULL;
1475 get_info(ctx->ovs, &info);
1477 if (ctx->argc == 1) {
1478 /* Return the fail-mode from the "Open_vSwitch" table */
1479 if (info.ctrl && info.ctrl->fail_mode) {
1480 fail_mode = info.ctrl->fail_mode;
1483 /* Return the fail-mode for a particular bridge. */
1484 struct vsctl_bridge *br = find_bridge(&info, ctx->argv[1], true);
1486 /* If no controller or fail-mode is explicitly defined for the
1487 * requested bridge, fallback to the "Open_vSwitch" table's
1489 if (br->ctrl && br->ctrl->fail_mode) {
1490 fail_mode = br->ctrl->fail_mode;
1491 } else if (info.ctrl && info.ctrl->fail_mode) {
1492 fail_mode = info.ctrl->fail_mode;
1496 if (fail_mode && strlen(fail_mode)) {
1497 ds_put_format(&ctx->output, "%s\n", fail_mode);
1504 cmd_del_fail_mode(struct vsctl_context *ctx)
1506 struct vsctl_info info;
1508 get_info(ctx->ovs, &info);
1510 if (ctx->argc == 1) {
1511 if (info.ctrl && info.ctrl->fail_mode) {
1512 ovsrec_controller_set_fail_mode(info.ctrl, NULL);
1515 struct vsctl_bridge *br = find_real_bridge(&info, ctx->argv[1], true);
1517 if (br->ctrl && br->ctrl->fail_mode) {
1518 ovsrec_controller_set_fail_mode(br->ctrl, NULL);
1526 cmd_set_fail_mode(struct vsctl_context *ctx)
1528 struct vsctl_info info;
1529 const char *fail_mode;
1531 get_info(ctx->ovs, &info);
1533 fail_mode = (ctx->argc == 2) ? ctx->argv[1] : ctx->argv[2];
1535 if (strcmp(fail_mode, "standalone") && strcmp(fail_mode, "secure")) {
1536 vsctl_fatal("fail-mode must be \"standalone\" or \"secure\"");
1539 if (ctx->argc == 2) {
1540 /* Set the fail-mode in the "Open_vSwitch" table. */
1542 vsctl_fatal("no controller declared");
1544 ovsrec_controller_set_fail_mode(info.ctrl, fail_mode);
1546 struct vsctl_bridge *br = find_real_bridge(&info, ctx->argv[1], true);
1549 vsctl_fatal("no controller declared for %s", br->name);
1551 ovsrec_controller_set_fail_mode(br->ctrl, fail_mode);
1558 cmd_get_ssl(struct vsctl_context *ctx)
1560 struct ovsrec_ssl *ssl = ctx->ovs->ssl;
1563 ds_put_format(&ctx->output, "Private key: %s\n", ssl->private_key);
1564 ds_put_format(&ctx->output, "Certificate: %s\n", ssl->certificate);
1565 ds_put_format(&ctx->output, "CA Certificate: %s\n", ssl->ca_cert);
1566 ds_put_format(&ctx->output, "Bootstrap: %s\n",
1567 ssl->bootstrap_ca_cert ? "true" : "false");
1572 cmd_del_ssl(struct vsctl_context *ctx)
1574 struct ovsrec_ssl *ssl = ctx->ovs->ssl;
1577 ovsrec_ssl_delete(ssl);
1578 ovsrec_open_vswitch_set_ssl(ctx->ovs, NULL);
1583 cmd_set_ssl(struct vsctl_context *ctx)
1585 bool bootstrap = shash_find(&ctx->options, "--bootstrap");
1586 struct ovsrec_ssl *ssl = ctx->ovs->ssl;
1589 ovsrec_ssl_delete(ssl);
1591 ssl = ovsrec_ssl_insert(ctx->txn);
1593 ovsrec_ssl_set_private_key(ssl, ctx->argv[1]);
1594 ovsrec_ssl_set_certificate(ssl, ctx->argv[2]);
1595 ovsrec_ssl_set_ca_cert(ssl, ctx->argv[3]);
1597 ovsrec_ssl_set_bootstrap_ca_cert(ssl, bootstrap);
1599 ovsrec_open_vswitch_set_ssl(ctx->ovs, ssl);
1602 /* Parameter commands. */
1604 struct vsctl_row_id {
1605 const struct ovsdb_idl_table_class *table;
1606 const struct ovsdb_idl_column *name_column;
1607 const struct ovsdb_idl_column *uuid_column;
1610 struct vsctl_table_class {
1611 struct ovsdb_idl_table_class *class;
1612 struct vsctl_row_id row_ids[2];
1615 static const struct vsctl_table_class tables[] = {
1616 {&ovsrec_table_bridge,
1617 {{&ovsrec_table_bridge, &ovsrec_bridge_col_name, NULL},
1618 {NULL, NULL, NULL}}},
1620 {&ovsrec_table_controller,
1621 {{&ovsrec_table_bridge,
1622 &ovsrec_bridge_col_name,
1623 &ovsrec_bridge_col_controller},
1624 {&ovsrec_table_open_vswitch,
1626 &ovsrec_open_vswitch_col_controller}}},
1628 {&ovsrec_table_interface,
1629 {{&ovsrec_table_interface, &ovsrec_interface_col_name, NULL},
1630 {NULL, NULL, NULL}}},
1632 {&ovsrec_table_mirror,
1633 {{&ovsrec_table_mirror, &ovsrec_mirror_col_name, NULL},
1634 {NULL, NULL, NULL}}},
1636 {&ovsrec_table_netflow,
1637 {{&ovsrec_table_bridge,
1638 &ovsrec_bridge_col_name,
1639 &ovsrec_bridge_col_netflow},
1640 {NULL, NULL, NULL}}},
1642 {&ovsrec_table_open_vswitch,
1643 {{&ovsrec_table_open_vswitch, NULL, NULL},
1644 {NULL, NULL, NULL}}},
1646 {&ovsrec_table_port,
1647 {{&ovsrec_table_port, &ovsrec_port_col_name, NULL},
1648 {NULL, NULL, NULL}}},
1651 {{&ovsrec_table_open_vswitch, NULL, &ovsrec_open_vswitch_col_ssl}}},
1653 {NULL, {{NULL, NULL, NULL}, {NULL, NULL, NULL}}}
1657 die_if_error(char *error)
1660 vsctl_fatal("%s", error);
1665 to_lower_and_underscores(unsigned c)
1667 return c == '-' ? '_' : tolower(c);
1671 score_partial_match(const char *name, const char *s)
1675 if (!strcmp(name, s)) {
1678 for (score = 0; ; score++, name++, s++) {
1679 if (to_lower_and_underscores(*name) != to_lower_and_underscores(*s)) {
1681 } else if (*name == '\0') {
1682 return UINT_MAX - 1;
1685 return *s == '\0' ? score : 0;
1688 static const struct vsctl_table_class *
1689 get_table(const char *table_name)
1691 const struct vsctl_table_class *table;
1692 const struct vsctl_table_class *best_match = NULL;
1693 unsigned int best_score = 0;
1695 for (table = tables; table->class; table++) {
1696 unsigned int score = score_partial_match(table->class->name,
1698 if (score > best_score) {
1701 } else if (score == best_score) {
1707 } else if (best_score) {
1708 vsctl_fatal("multiple table names match \"%s\"", table_name);
1710 vsctl_fatal("unknown table \"%s\"", table_name);
1714 static const struct ovsdb_idl_row *
1715 get_row_by_id(struct vsctl_context *ctx, const struct vsctl_table_class *table,
1716 const struct vsctl_row_id *id, const char *record_id)
1718 const struct ovsdb_idl_row *referrer, *final;
1724 if (!id->name_column) {
1725 if (strcmp(record_id, ".")) {
1728 referrer = ovsdb_idl_first_row(ctx->idl, id->table);
1729 if (!referrer || ovsdb_idl_next_row(referrer)) {
1733 const struct ovsdb_idl_row *row;
1734 unsigned int best_score = 0;
1736 /* It might make sense to relax this assertion. */
1737 assert(id->name_column->type.key.type == OVSDB_TYPE_STRING);
1740 for (row = ovsdb_idl_first_row(ctx->idl, id->table);
1741 row != NULL && best_score != UINT_MAX;
1742 row = ovsdb_idl_next_row(row))
1744 struct ovsdb_datum name;
1746 ovsdb_idl_txn_read(row, id->name_column, &name);
1748 unsigned int score = score_partial_match(name.keys[0].string,
1750 if (score > best_score) {
1753 } else if (score == best_score) {
1757 ovsdb_datum_destroy(&name, &id->name_column->type);
1759 if (best_score && !referrer) {
1760 vsctl_fatal("multiple rows in %s match \"%s\"",
1761 table->class->name, record_id);
1769 if (id->uuid_column) {
1770 struct ovsdb_datum uuid;
1772 assert(id->uuid_column->type.key.type == OVSDB_TYPE_UUID);
1773 assert(id->uuid_column->type.value.type == OVSDB_TYPE_VOID);
1775 ovsdb_idl_txn_read(referrer, id->uuid_column, &uuid);
1777 final = ovsdb_idl_get_row_for_uuid(ctx->idl, table->class,
1778 &uuid.keys[0].uuid);
1780 ovsdb_datum_destroy(&uuid, &id->uuid_column->type);
1788 static const struct ovsdb_idl_row *
1789 get_row(struct vsctl_context *ctx,
1790 const struct vsctl_table_class *table, const char *record_id)
1792 const struct ovsdb_idl_row *row;
1795 if (uuid_from_string(&uuid, record_id)) {
1796 row = ovsdb_idl_get_row_for_uuid(ctx->idl, table->class, &uuid);
1800 for (i = 0; i < ARRAY_SIZE(table->row_ids); i++) {
1801 row = get_row_by_id(ctx, table, &table->row_ids[i], record_id);
1810 static const struct ovsdb_idl_row *
1811 must_get_row(struct vsctl_context *ctx,
1812 const struct vsctl_table_class *table, const char *record_id)
1814 const struct ovsdb_idl_row *row = get_row(ctx, table, record_id);
1816 vsctl_fatal("no row \"%s\" in table %s",
1817 record_id, table->class->name);
1823 get_column(const struct vsctl_table_class *table, const char *column_name,
1824 const struct ovsdb_idl_column **columnp)
1826 const struct ovsdb_idl_column *best_match = NULL;
1827 unsigned int best_score = 0;
1830 for (i = 0; i < table->class->n_columns; i++) {
1831 const struct ovsdb_idl_column *column = &table->class->columns[i];
1832 unsigned int score = score_partial_match(column->name, column_name);
1833 if (score > best_score) {
1834 best_match = column;
1836 } else if (score == best_score) {
1841 *columnp = best_match;
1844 } else if (best_score) {
1845 return xasprintf("%s contains more than one column whose name "
1846 "matches \"%s\"", table->class->name, column_name);
1848 return xasprintf("%s does not contain a column whose name matches "
1849 "\"%s\"", table->class->name, column_name);
1853 static char * WARN_UNUSED_RESULT
1854 parse_column_key_value(const char *arg, const struct vsctl_table_class *table,
1855 const struct ovsdb_idl_column **columnp,
1856 char **keyp, char **valuep)
1858 const char *p = arg;
1861 assert(columnp || keyp);
1869 /* Parse column name. */
1873 error = ovsdb_token_parse(&p, &column_name);
1877 if (column_name[0] == '\0') {
1879 error = xasprintf("%s: missing column name", arg);
1882 error = get_column(table, column_name, columnp);
1889 /* Parse key string. */
1890 if (*p == ':' || !columnp) {
1894 error = xasprintf("%s: key not accepted here", arg);
1897 error = ovsdb_token_parse(&p, keyp);
1905 /* Parse value string. */
1908 error = xasprintf("%s: value not accepted here", arg);
1911 *valuep = xstrdup(p + 1);
1917 error = xasprintf("%s: trailing garbage \"%s\" in argument",
1940 cmd_get(struct vsctl_context *ctx)
1942 bool if_exists = shash_find(&ctx->options, "--if-exists");
1943 const char *table_name = ctx->argv[1];
1944 const char *record_id = ctx->argv[2];
1945 const struct vsctl_table_class *table;
1946 const struct ovsdb_idl_row *row;
1947 struct ds *out = &ctx->output;
1950 table = get_table(table_name);
1951 row = must_get_row(ctx, table, record_id);
1952 for (i = 3; i < ctx->argc; i++) {
1953 const struct ovsdb_idl_column *column;
1954 struct ovsdb_datum datum;
1957 die_if_error(parse_column_key_value(ctx->argv[i], table,
1958 &column, &key_string, NULL));
1960 ovsdb_idl_txn_read(row, column, &datum);
1962 union ovsdb_atom key;
1965 if (column->type.value.type == OVSDB_TYPE_VOID) {
1966 vsctl_fatal("cannot specify key to get for non-map column %s",
1970 die_if_error(ovsdb_atom_from_string(&key,
1974 idx = ovsdb_datum_find_key(&datum, &key,
1975 column->type.key.type);
1976 if (idx == UINT_MAX) {
1978 vsctl_fatal("no key \"%s\" in %s record \"%s\" column %s",
1979 key_string, table->class->name, record_id,
1983 ovsdb_atom_to_string(&datum.values[idx],
1984 column->type.value.type, out);
1986 ovsdb_atom_destroy(&key, column->type.key.type);
1988 ovsdb_datum_to_string(&datum, &column->type, out);
1990 ds_put_char(out, '\n');
1991 ovsdb_datum_destroy(&datum, &column->type);
1998 list_record(const struct vsctl_table_class *table,
1999 const struct ovsdb_idl_row *row, struct ds *out)
2003 ds_put_format(out, "%-20s: "UUID_FMT"\n", "_uuid",
2004 UUID_ARGS(&row->uuid));
2005 for (i = 0; i < table->class->n_columns; i++) {
2006 const struct ovsdb_idl_column *column = &table->class->columns[i];
2007 struct ovsdb_datum datum;
2009 ovsdb_idl_txn_read(row, column, &datum);
2011 ds_put_format(out, "%-20s: ", column->name);
2012 ovsdb_datum_to_string(&datum, &column->type, out);
2013 ds_put_char(out, '\n');
2015 ovsdb_datum_destroy(&datum, &column->type);
2020 cmd_list(struct vsctl_context *ctx)
2022 const char *table_name = ctx->argv[1];
2023 const struct vsctl_table_class *table;
2024 struct ds *out = &ctx->output;
2027 table = get_table(table_name);
2028 if (ctx->argc > 2) {
2029 for (i = 2; i < ctx->argc; i++) {
2031 ds_put_char(out, '\n');
2033 list_record(table, must_get_row(ctx, table, ctx->argv[i]), out);
2036 const struct ovsdb_idl_row *row;
2039 for (row = ovsdb_idl_first_row(ctx->idl, table->class), first = true;
2041 row = ovsdb_idl_next_row(row), first = false) {
2043 ds_put_char(out, '\n');
2045 list_record(table, row, out);
2051 set_column(const struct vsctl_table_class *table,
2052 const struct ovsdb_idl_row *row, const char *arg)
2054 const struct ovsdb_idl_column *column;
2055 char *key_string, *value_string;
2058 error = parse_column_key_value(arg, table, &column, &key_string,
2060 die_if_error(error);
2061 if (!value_string) {
2062 vsctl_fatal("%s: missing value", arg);
2066 union ovsdb_atom key, value;
2067 struct ovsdb_datum old, new;
2069 if (column->type.value.type == OVSDB_TYPE_VOID) {
2070 vsctl_fatal("cannot specify key to set for non-map column %s",
2074 die_if_error(ovsdb_atom_from_string(&key, &column->type.key,
2076 die_if_error(ovsdb_atom_from_string(&value, &column->type.value,
2079 ovsdb_datum_init_empty(&new);
2080 ovsdb_datum_add_unsafe(&new, &key, &value, &column->type);
2082 ovsdb_atom_destroy(&key, column->type.key.type);
2083 ovsdb_atom_destroy(&value, column->type.value.type);
2085 ovsdb_idl_txn_read(row, column, &old);
2086 ovsdb_datum_union(&old, &new, &column->type, true);
2087 ovsdb_idl_txn_write(row, column, &old);
2089 ovsdb_datum_destroy(&new, &column->type);
2091 struct ovsdb_datum datum;
2093 die_if_error(ovsdb_datum_from_string(&datum, &column->type,
2095 ovsdb_idl_txn_write(row, column, &datum);
2103 cmd_set(struct vsctl_context *ctx)
2105 const char *table_name = ctx->argv[1];
2106 const char *record_id = ctx->argv[2];
2107 const struct vsctl_table_class *table;
2108 const struct ovsdb_idl_row *row;
2111 table = get_table(table_name);
2112 row = must_get_row(ctx, table, record_id);
2113 for (i = 3; i < ctx->argc; i++) {
2114 set_column(table, row, ctx->argv[i]);
2119 cmd_add(struct vsctl_context *ctx)
2121 const char *table_name = ctx->argv[1];
2122 const char *record_id = ctx->argv[2];
2123 const char *column_name = ctx->argv[3];
2124 const struct vsctl_table_class *table;
2125 const struct ovsdb_idl_column *column;
2126 const struct ovsdb_idl_row *row;
2127 const struct ovsdb_type *type;
2128 struct ovsdb_datum old;
2131 table = get_table(table_name);
2132 row = must_get_row(ctx, table, record_id);
2133 die_if_error(get_column(table, column_name, &column));
2135 type = &column->type;
2136 ovsdb_idl_txn_read(row, column, &old);
2137 for (i = 4; i < ctx->argc; i++) {
2138 struct ovsdb_type add_type;
2139 struct ovsdb_datum add;
2143 add_type.n_max = UINT_MAX;
2144 die_if_error(ovsdb_datum_from_string(&add, &add_type, ctx->argv[i]));
2145 ovsdb_datum_union(&old, &add, type, false);
2146 ovsdb_datum_destroy(&add, type);
2148 if (old.n > type->n_max) {
2149 vsctl_fatal("\"add\" operation would put %u %s in column %s of "
2150 "table %s but the maximum number is %u",
2152 type->value.type == OVSDB_TYPE_VOID ? "values" : "pairs",
2153 column->name, table->class->name, type->n_max);
2155 ovsdb_idl_txn_write(row, column, &old);
2159 cmd_remove(struct vsctl_context *ctx)
2161 const char *table_name = ctx->argv[1];
2162 const char *record_id = ctx->argv[2];
2163 const char *column_name = ctx->argv[3];
2164 const struct vsctl_table_class *table;
2165 const struct ovsdb_idl_column *column;
2166 const struct ovsdb_idl_row *row;
2167 const struct ovsdb_type *type;
2168 struct ovsdb_datum old;
2171 table = get_table(table_name);
2172 row = must_get_row(ctx, table, record_id);
2173 die_if_error(get_column(table, column_name, &column));
2175 type = &column->type;
2176 ovsdb_idl_txn_read(row, column, &old);
2177 for (i = 4; i < ctx->argc; i++) {
2178 struct ovsdb_type rm_type;
2179 struct ovsdb_datum rm;
2184 rm_type.n_max = UINT_MAX;
2185 error = ovsdb_datum_from_string(&rm, &rm_type, ctx->argv[i]);
2186 if (error && ovsdb_type_is_map(&rm_type)) {
2188 rm_type.value.type = OVSDB_TYPE_VOID;
2189 die_if_error(ovsdb_datum_from_string(&rm, &rm_type, ctx->argv[i]));
2191 ovsdb_datum_subtract(&old, type, &rm, &rm_type);
2192 ovsdb_datum_destroy(&rm, &rm_type);
2194 if (old.n < type->n_min) {
2195 vsctl_fatal("\"remove\" operation would put %u %s in column %s of "
2196 "table %s but the minimun number is %u",
2198 type->value.type == OVSDB_TYPE_VOID ? "values" : "pairs",
2199 column->name, table->class->name, type->n_min);
2201 ovsdb_idl_txn_write(row, column, &old);
2205 cmd_clear(struct vsctl_context *ctx)
2207 const char *table_name = ctx->argv[1];
2208 const char *record_id = ctx->argv[2];
2209 const struct vsctl_table_class *table;
2210 const struct ovsdb_idl_row *row;
2213 table = get_table(table_name);
2214 row = must_get_row(ctx, table, record_id);
2215 for (i = 3; i < ctx->argc; i++) {
2216 const struct ovsdb_idl_column *column;
2217 const struct ovsdb_type *type;
2218 struct ovsdb_datum datum;
2220 die_if_error(get_column(table, ctx->argv[i], &column));
2222 type = &column->type;
2223 if (type->n_min > 0) {
2224 vsctl_fatal("\"clear\" operation cannot be applied to column %s "
2225 "of table %s, which is not allowed to be empty",
2226 column->name, table->class->name);
2229 ovsdb_datum_init_empty(&datum);
2230 ovsdb_idl_txn_write(row, column, &datum);
2235 cmd_create(struct vsctl_context *ctx)
2237 const char *table_name = ctx->argv[1];
2238 const struct vsctl_table_class *table;
2239 const struct ovsdb_idl_row *row;
2242 table = get_table(table_name);
2243 row = ovsdb_idl_txn_insert(ctx->txn, table->class);
2244 for (i = 2; i < ctx->argc; i++) {
2245 set_column(table, row, ctx->argv[i]);
2247 ds_put_format(&ctx->output, UUID_FMT, UUID_ARGS(&row->uuid));
2250 /* This function may be used as the 'postprocess' function for commands that
2251 * insert new rows into the database. It expects that the command's 'run'
2252 * function prints the UUID reported by ovsdb_idl_txn_insert() as the command's
2253 * sole output. It replaces that output by the row's permanent UUID assigned
2254 * by the database server and appends a new-line.
2256 * Currently we use this only for "create", because the higher-level commands
2257 * are supposed to be independent of the actual structure of the vswitch
2260 post_create(struct vsctl_context *ctx)
2262 const struct uuid *real;
2265 uuid_from_string(&dummy, ds_cstr(&ctx->output));
2266 real = ovsdb_idl_txn_get_insert_uuid(ctx->txn, &dummy);
2268 ds_clear(&ctx->output);
2269 ds_put_format(&ctx->output, UUID_FMT, UUID_ARGS(real));
2271 ds_put_char(&ctx->output, '\n');
2275 cmd_destroy(struct vsctl_context *ctx)
2277 bool must_exist = !shash_find(&ctx->options, "--if-exists");
2278 const char *table_name = ctx->argv[1];
2279 const struct vsctl_table_class *table;
2282 table = get_table(table_name);
2283 for (i = 2; i < ctx->argc; i++) {
2284 const struct ovsdb_idl_row *row;
2286 row = (must_exist ? must_get_row : get_row)(ctx, table, ctx->argv[i]);
2288 ovsdb_idl_txn_delete(row);
2293 static struct json *
2294 where_uuid_equals(const struct uuid *uuid)
2297 json_array_create_1(
2298 json_array_create_3(
2299 json_string_create("_uuid"),
2300 json_string_create("=="),
2301 json_array_create_2(
2302 json_string_create("uuid"),
2303 json_string_create_nocopy(
2304 xasprintf(UUID_FMT, UUID_ARGS(uuid))))));
2308 vsctl_context_init(struct vsctl_context *ctx, struct vsctl_command *command,
2309 struct ovsdb_idl *idl, struct ovsdb_idl_txn *txn,
2310 const struct ovsrec_open_vswitch *ovs)
2312 ctx->argc = command->argc;
2313 ctx->argv = command->argv;
2314 ctx->options = command->options;
2316 ds_swap(&ctx->output, &command->output);
2324 vsctl_context_done(struct vsctl_context *ctx, struct vsctl_command *command)
2326 ds_swap(&ctx->output, &command->output);
2330 do_vsctl(const char *args, struct vsctl_command *commands, size_t n_commands,
2331 struct ovsdb_idl *idl)
2333 struct ovsdb_idl_txn *txn;
2334 const struct ovsrec_open_vswitch *ovs;
2335 enum ovsdb_idl_txn_status status;
2336 struct vsctl_command *c;
2337 int64_t next_cfg = 0;
2341 txn = the_idl_txn = ovsdb_idl_txn_create(idl);
2343 ovsdb_idl_txn_set_dry_run(txn);
2346 comment = xasprintf("ovs-vsctl: %s", args);
2347 ovsdb_idl_txn_add_comment(txn, comment);
2350 ovs = ovsrec_open_vswitch_first(idl);
2352 /* XXX add verification that table is empty */
2353 ovs = ovsrec_open_vswitch_insert(txn);
2356 if (wait_for_reload) {
2357 struct json *where = where_uuid_equals(&ovs->header_.uuid);
2358 ovsdb_idl_txn_increment(txn, "Open_vSwitch", "next_cfg", where);
2359 json_destroy(where);
2362 for (c = commands; c < &commands[n_commands]; c++) {
2363 struct vsctl_context ctx;
2365 ds_init(&c->output);
2366 vsctl_context_init(&ctx, c, idl, txn, ovs);
2367 (c->syntax->run)(&ctx);
2368 vsctl_context_done(&ctx, c);
2371 while ((status = ovsdb_idl_txn_commit(txn)) == TXN_INCOMPLETE) {
2373 ovsdb_idl_wait(idl);
2374 ovsdb_idl_txn_wait(txn);
2377 if (wait_for_reload && status == TXN_SUCCESS) {
2378 next_cfg = ovsdb_idl_txn_get_increment_new_value(txn);
2380 for (c = commands; c < &commands[n_commands]; c++) {
2381 if (c->syntax->postprocess) {
2382 struct vsctl_context ctx;
2384 vsctl_context_init(&ctx, c, idl, txn, ovs);
2385 (c->syntax->postprocess)(&ctx);
2386 vsctl_context_done(&ctx, c);
2389 error = xstrdup(ovsdb_idl_txn_get_error(txn));
2390 ovsdb_idl_txn_destroy(txn);
2394 case TXN_INCOMPLETE:
2398 /* Should not happen--we never call ovsdb_idl_txn_abort(). */
2399 vsctl_fatal("transaction aborted");
2406 for (c = commands; c < &commands[n_commands]; c++) {
2407 ds_destroy(&c->output);
2413 vsctl_fatal("transaction error: %s", error);
2420 for (c = commands; c < &commands[n_commands]; c++) {
2421 struct ds *ds = &c->output;
2426 for (j = 0; j < ds->length; j++) {
2427 int c = ds->string[j];
2430 fputs("\\n", stdout);
2434 fputs("\\\\", stdout);
2443 fputs(ds_cstr(ds), stdout);
2445 ds_destroy(&c->output);
2446 shash_destroy(&c->options);
2450 if (wait_for_reload && status != TXN_UNCHANGED) {
2452 const struct ovsrec_open_vswitch *ovs;
2455 OVSREC_OPEN_VSWITCH_FOR_EACH (ovs, idl) {
2456 if (ovs->cur_cfg >= next_cfg) {
2460 ovsdb_idl_wait(idl);
2465 ovsdb_idl_destroy(idl);
2470 static const struct vsctl_command_syntax all_commands[] = {
2471 /* Open vSwitch commands. */
2472 {"init", 0, 0, cmd_init, NULL, ""},
2474 /* Bridge commands. */
2475 {"add-br", 1, 3, cmd_add_br, NULL, "--may-exist"},
2476 {"del-br", 1, 1, cmd_del_br, NULL, "--if-exists"},
2477 {"list-br", 0, 0, cmd_list_br, NULL, ""},
2478 {"br-exists", 1, 1, cmd_br_exists, NULL, ""},
2479 {"br-to-vlan", 1, 1, cmd_br_to_vlan, NULL, ""},
2480 {"br-to-parent", 1, 1, cmd_br_to_parent, NULL, ""},
2481 {"br-set-external-id", 2, 3, cmd_br_set_external_id, NULL, ""},
2482 {"br-get-external-id", 1, 2, cmd_br_get_external_id, NULL, ""},
2484 /* Port commands. */
2485 {"list-ports", 1, 1, cmd_list_ports, NULL, ""},
2486 {"add-port", 2, 2, cmd_add_port, NULL, "--may-exist"},
2487 {"add-bond", 4, INT_MAX, cmd_add_bond, NULL, "--may-exist,--fake-iface"},
2488 {"del-port", 1, 2, cmd_del_port, NULL, "--if-exists"},
2489 {"port-to-br", 1, 1, cmd_port_to_br, NULL, ""},
2491 /* Interface commands. */
2492 {"list-ifaces", 1, 1, cmd_list_ifaces, NULL, ""},
2493 {"iface-to-br", 1, 1, cmd_iface_to_br, NULL, ""},
2495 /* Controller commands. */
2496 {"get-controller", 0, 1, cmd_get_controller, NULL, ""},
2497 {"del-controller", 0, 1, cmd_del_controller, NULL, ""},
2498 {"set-controller", 1, 2, cmd_set_controller, NULL, ""},
2499 {"get-fail-mode", 0, 1, cmd_get_fail_mode, NULL, ""},
2500 {"del-fail-mode", 0, 1, cmd_del_fail_mode, NULL, ""},
2501 {"set-fail-mode", 1, 2, cmd_set_fail_mode, NULL, ""},
2504 {"get-ssl", 0, 0, cmd_get_ssl, NULL, ""},
2505 {"del-ssl", 0, 0, cmd_del_ssl, NULL, ""},
2506 {"set-ssl", 3, 3, cmd_set_ssl, NULL, "--bootstrap"},
2508 /* Parameter commands. */
2509 {"get", 3, INT_MAX, cmd_get, NULL, "--if-exists"},
2510 {"list", 1, INT_MAX, cmd_list, NULL, ""},
2511 {"set", 3, INT_MAX, cmd_set, NULL, ""},
2512 {"add", 4, INT_MAX, cmd_add, NULL, ""},
2513 {"remove", 4, INT_MAX, cmd_remove, NULL, ""},
2514 {"clear", 3, INT_MAX, cmd_clear, NULL, ""},
2515 {"create", 2, INT_MAX, cmd_create, post_create, ""},
2516 {"destroy", 1, INT_MAX, cmd_destroy, NULL, "--if-exists"},
2518 {NULL, 0, 0, NULL, NULL, NULL},