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;
495 static struct vsctl_bridge *
496 add_bridge(struct vsctl_info *b,
497 struct ovsrec_bridge *br_cfg, const char *name,
498 struct vsctl_bridge *parent, int vlan)
500 struct vsctl_bridge *br = xmalloc(sizeof *br);
502 br->name = xstrdup(name);
505 br->ctrl = parent ? parent->br_cfg->controller : br_cfg->controller;
506 shash_add(&b->bridges, br->name, br);
511 port_is_fake_bridge(const struct ovsrec_port *port_cfg)
513 return (port_cfg->fake_bridge
515 && *port_cfg->tag >= 1 && *port_cfg->tag <= 4095);
518 static struct vsctl_bridge *
519 find_vlan_bridge(struct vsctl_info *info,
520 struct vsctl_bridge *parent, int vlan)
522 struct shash_node *node;
524 SHASH_FOR_EACH (node, &info->bridges) {
525 struct vsctl_bridge *br = node->data;
526 if (br->parent == parent && br->vlan == vlan) {
535 free_info(struct vsctl_info *info)
537 struct shash_node *node;
539 SHASH_FOR_EACH (node, &info->bridges) {
540 struct vsctl_bridge *bridge = node->data;
544 shash_destroy(&info->bridges);
546 SHASH_FOR_EACH (node, &info->ports) {
547 struct vsctl_port *port = node->data;
550 shash_destroy(&info->ports);
552 SHASH_FOR_EACH (node, &info->ifaces) {
553 struct vsctl_iface *iface = node->data;
556 shash_destroy(&info->ifaces);
560 get_info(const struct ovsrec_open_vswitch *ovs, struct vsctl_info *info)
562 struct shash bridges, ports;
565 shash_init(&info->bridges);
566 shash_init(&info->ports);
567 shash_init(&info->ifaces);
569 info->ctrl = ovs->controller;
571 shash_init(&bridges);
573 for (i = 0; i < ovs->n_bridges; i++) {
574 struct ovsrec_bridge *br_cfg = ovs->bridges[i];
575 struct vsctl_bridge *br;
578 if (!shash_add_once(&bridges, br_cfg->name, NULL)) {
579 VLOG_WARN("%s: database contains duplicate bridge name",
583 br = add_bridge(info, br_cfg, br_cfg->name, NULL, 0);
588 for (j = 0; j < br_cfg->n_ports; j++) {
589 struct ovsrec_port *port_cfg = br_cfg->ports[j];
591 if (!shash_add_once(&ports, port_cfg->name, NULL)) {
592 VLOG_WARN("%s: database contains duplicate port name",
597 if (port_is_fake_bridge(port_cfg)
598 && shash_add_once(&bridges, port_cfg->name, NULL)) {
599 add_bridge(info, NULL, port_cfg->name, br, *port_cfg->tag);
603 shash_destroy(&bridges);
604 shash_destroy(&ports);
606 shash_init(&bridges);
608 for (i = 0; i < ovs->n_bridges; i++) {
609 struct ovsrec_bridge *br_cfg = ovs->bridges[i];
610 struct vsctl_bridge *br;
613 if (!shash_add_once(&bridges, br_cfg->name, NULL)) {
616 br = shash_find_data(&info->bridges, br_cfg->name);
617 for (j = 0; j < br_cfg->n_ports; j++) {
618 struct ovsrec_port *port_cfg = br_cfg->ports[j];
619 struct vsctl_port *port;
622 if (!shash_add_once(&ports, port_cfg->name, NULL)) {
626 if (port_is_fake_bridge(port_cfg)
627 && !shash_add_once(&bridges, port_cfg->name, NULL)) {
631 port = xmalloc(sizeof *port);
632 port->port_cfg = port_cfg;
634 && *port_cfg->tag >= 1 && *port_cfg->tag <= 4095) {
635 port->bridge = find_vlan_bridge(info, br, *port_cfg->tag);
642 shash_add(&info->ports, port_cfg->name, port);
644 for (k = 0; k < port_cfg->n_interfaces; k++) {
645 struct ovsrec_interface *iface_cfg = port_cfg->interfaces[k];
646 struct vsctl_iface *iface;
648 if (shash_find(&info->ifaces, iface_cfg->name)) {
649 VLOG_WARN("%s: database contains duplicate interface name",
654 iface = xmalloc(sizeof *iface);
655 iface->iface_cfg = iface_cfg;
657 shash_add(&info->ifaces, iface_cfg->name, iface);
661 shash_destroy(&bridges);
662 shash_destroy(&ports);
666 check_conflicts(struct vsctl_info *info, const char *name,
669 struct vsctl_iface *iface;
670 struct vsctl_port *port;
672 if (shash_find(&info->bridges, name)) {
673 vsctl_fatal("%s because a bridge named %s already exists",
677 port = shash_find_data(&info->ports, name);
679 vsctl_fatal("%s because a port named %s already exists on "
680 "bridge %s", msg, name, port->bridge->name);
683 iface = shash_find_data(&info->ifaces, name);
685 vsctl_fatal("%s because an interface named %s already exists "
686 "on bridge %s", msg, name, iface->port->bridge->name);
692 static struct vsctl_bridge *
693 find_bridge(struct vsctl_info *info, const char *name, bool must_exist)
695 struct vsctl_bridge *br = shash_find_data(&info->bridges, name);
696 if (must_exist && !br) {
697 vsctl_fatal("no bridge named %s", name);
702 static struct vsctl_bridge *
703 find_real_bridge(struct vsctl_info *info, const char *name, bool must_exist)
705 struct vsctl_bridge *br = find_bridge(info, name, must_exist);
706 if (br && br->parent) {
707 vsctl_fatal("%s is a fake bridge", name);
712 static struct vsctl_port *
713 find_port(struct vsctl_info *info, const char *name, bool must_exist)
715 struct vsctl_port *port = shash_find_data(&info->ports, name);
716 if (port && !strcmp(name, port->bridge->name)) {
719 if (must_exist && !port) {
720 vsctl_fatal("no port named %s", name);
725 static struct vsctl_iface *
726 find_iface(struct vsctl_info *info, const char *name, bool must_exist)
728 struct vsctl_iface *iface = shash_find_data(&info->ifaces, name);
729 if (iface && !strcmp(name, iface->port->bridge->name)) {
732 if (must_exist && !iface) {
733 vsctl_fatal("no interface named %s", name);
739 bridge_insert_port(struct ovsrec_bridge *br, struct ovsrec_port *port)
741 struct ovsrec_port **ports;
744 ports = xmalloc(sizeof *br->ports * (br->n_ports + 1));
745 for (i = 0; i < br->n_ports; i++) {
746 ports[i] = br->ports[i];
748 ports[br->n_ports] = port;
749 ovsrec_bridge_set_ports(br, ports, br->n_ports + 1);
754 bridge_delete_port(struct ovsrec_bridge *br, struct ovsrec_port *port)
756 struct ovsrec_port **ports;
759 ports = xmalloc(sizeof *br->ports * br->n_ports);
760 for (i = n = 0; i < br->n_ports; i++) {
761 if (br->ports[i] != port) {
762 ports[n++] = br->ports[i];
765 ovsrec_bridge_set_ports(br, ports, n);
770 ovs_insert_bridge(const struct ovsrec_open_vswitch *ovs,
771 struct ovsrec_bridge *bridge)
773 struct ovsrec_bridge **bridges;
776 bridges = xmalloc(sizeof *ovs->bridges * (ovs->n_bridges + 1));
777 for (i = 0; i < ovs->n_bridges; i++) {
778 bridges[i] = ovs->bridges[i];
780 bridges[ovs->n_bridges] = bridge;
781 ovsrec_open_vswitch_set_bridges(ovs, bridges, ovs->n_bridges + 1);
786 ovs_delete_bridge(const struct ovsrec_open_vswitch *ovs,
787 struct ovsrec_bridge *bridge)
789 struct ovsrec_bridge **bridges;
792 bridges = xmalloc(sizeof *ovs->bridges * ovs->n_bridges);
793 for (i = n = 0; i < ovs->n_bridges; i++) {
794 if (ovs->bridges[i] != bridge) {
795 bridges[n++] = ovs->bridges[i];
798 ovsrec_open_vswitch_set_bridges(ovs, bridges, n);
803 cmd_init(struct vsctl_context *ctx UNUSED)
808 cmd_add_br(struct vsctl_context *ctx)
810 bool may_exist = shash_find(&ctx->options, "--may-exist") != 0;
811 const char *br_name = ctx->argv[1];
812 const char *parent_name = ctx->argc > 2 ? ctx->argv[2] : NULL;
813 int vlan = ctx->argc > 3 ? atoi(ctx->argv[3]) : 0;
814 struct vsctl_info info;
816 br_name = ctx->argv[1];
817 if (ctx->argc == 2) {
820 } else if (ctx->argc == 4) {
821 parent_name = ctx->argv[2];
822 vlan = atoi(ctx->argv[3]);
823 if (vlan < 1 || vlan > 4095) {
824 vsctl_fatal("%s: vlan must be between 1 and 4095", ctx->argv[0]);
827 vsctl_fatal("'%s' command takes exactly 1 or 3 arguments",
831 get_info(ctx->ovs, &info);
833 struct vsctl_bridge *br;
835 br = find_bridge(&info, br_name, false);
839 vsctl_fatal("\"--may-exist add-br %s\" but %s is "
840 "a VLAN bridge for VLAN %d",
841 br_name, br_name, br->vlan);
845 vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
846 "is not a VLAN bridge",
847 br_name, parent_name, vlan, br_name);
848 } else if (strcmp(br->parent->name, parent_name)) {
849 vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
850 "has the wrong parent %s",
851 br_name, parent_name, vlan,
852 br_name, br->parent->name);
853 } else if (br->vlan != vlan) {
854 vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
855 "is a VLAN bridge for the wrong VLAN %d",
856 br_name, parent_name, vlan, br_name, br->vlan);
862 check_conflicts(&info, br_name,
863 xasprintf("cannot create a bridge named %s", br_name));
866 struct ovsrec_port *port;
867 struct ovsrec_interface *iface;
868 struct ovsrec_bridge *br;
870 iface = ovsrec_interface_insert(ctx->txn);
871 ovsrec_interface_set_name(iface, br_name);
873 port = ovsrec_port_insert(ctx->txn);
874 ovsrec_port_set_name(port, br_name);
875 ovsrec_port_set_interfaces(port, &iface, 1);
877 br = ovsrec_bridge_insert(ctx->txn);
878 ovsrec_bridge_set_name(br, br_name);
879 ovsrec_bridge_set_ports(br, &port, 1);
881 ovs_insert_bridge(ctx->ovs, br);
883 struct vsctl_bridge *parent;
884 struct ovsrec_port *port;
885 struct ovsrec_interface *iface;
886 struct ovsrec_bridge *br;
889 parent = find_bridge(&info, parent_name, false);
890 if (parent && parent->vlan) {
891 vsctl_fatal("cannot create bridge with fake bridge as parent");
894 vsctl_fatal("parent bridge %s does not exist", parent_name);
898 iface = ovsrec_interface_insert(ctx->txn);
899 ovsrec_interface_set_name(iface, br_name);
900 ovsrec_interface_set_type(iface, "internal");
902 port = ovsrec_port_insert(ctx->txn);
903 ovsrec_port_set_name(port, br_name);
904 ovsrec_port_set_interfaces(port, &iface, 1);
905 ovsrec_port_set_fake_bridge(port, true);
906 ovsrec_port_set_tag(port, &tag, 1);
908 bridge_insert_port(br, port);
915 del_port(struct vsctl_info *info, struct vsctl_port *port)
917 struct shash_node *node;
919 SHASH_FOR_EACH (node, &info->ifaces) {
920 struct vsctl_iface *iface = node->data;
921 if (iface->port == port) {
922 ovsrec_interface_delete(iface->iface_cfg);
925 ovsrec_port_delete(port->port_cfg);
927 bridge_delete_port((port->bridge->parent
928 ? port->bridge->parent->br_cfg
929 : port->bridge->br_cfg), port->port_cfg);
933 cmd_del_br(struct vsctl_context *ctx)
935 bool must_exist = !shash_find(&ctx->options, "--if-exists");
936 struct vsctl_bridge *bridge;
937 struct vsctl_info info;
939 get_info(ctx->ovs, &info);
940 bridge = find_bridge(&info, ctx->argv[1], must_exist);
942 struct shash_node *node;
944 SHASH_FOR_EACH (node, &info.ports) {
945 struct vsctl_port *port = node->data;
946 if (port->bridge == bridge || port->bridge->parent == bridge
947 || !strcmp(port->port_cfg->name, bridge->name)) {
948 del_port(&info, port);
951 if (bridge->br_cfg) {
952 ovsrec_bridge_delete(bridge->br_cfg);
953 ovs_delete_bridge(ctx->ovs, bridge->br_cfg);
960 output_sorted(struct svec *svec, struct ds *output)
966 SVEC_FOR_EACH (i, name, svec) {
967 ds_put_format(output, "%s\n", name);
972 cmd_list_br(struct vsctl_context *ctx)
974 struct shash_node *node;
975 struct vsctl_info info;
978 get_info(ctx->ovs, &info);
981 SHASH_FOR_EACH (node, &info.bridges) {
982 struct vsctl_bridge *br = node->data;
983 svec_add(&bridges, br->name);
985 output_sorted(&bridges, &ctx->output);
986 svec_destroy(&bridges);
992 cmd_br_exists(struct vsctl_context *ctx)
994 struct vsctl_info info;
996 get_info(ctx->ovs, &info);
997 if (!find_bridge(&info, ctx->argv[1], false)) {
1003 /* Returns true if 'b_prefix' (of length 'b_prefix_len') concatenated with 'b'
1004 * equals 'a', false otherwise. */
1006 key_matches(const char *a,
1007 const char *b_prefix, size_t b_prefix_len, const char *b)
1009 return !strncmp(a, b_prefix, b_prefix_len) && !strcmp(a + b_prefix_len, b);
1013 set_external_id(char **old_keys, char **old_values, size_t old_n,
1014 char *key, char *value,
1015 char ***new_keysp, char ***new_valuesp, size_t *new_np)
1022 new_keys = xmalloc(sizeof *new_keys * (old_n + 1));
1023 new_values = xmalloc(sizeof *new_values * (old_n + 1));
1025 for (i = 0; i < old_n; i++) {
1026 if (strcmp(key, old_keys[i])) {
1027 new_keys[new_n] = old_keys[i];
1028 new_values[new_n] = old_values[i];
1033 new_keys[new_n] = key;
1034 new_values[new_n] = value;
1037 *new_keysp = new_keys;
1038 *new_valuesp = new_values;
1043 cmd_br_set_external_id(struct vsctl_context *ctx)
1045 struct vsctl_info info;
1046 struct vsctl_bridge *bridge;
1047 char **keys, **values;
1050 get_info(ctx->ovs, &info);
1051 bridge = find_bridge(&info, ctx->argv[1], true);
1052 if (bridge->br_cfg) {
1053 set_external_id(bridge->br_cfg->key_external_ids,
1054 bridge->br_cfg->value_external_ids,
1055 bridge->br_cfg->n_external_ids,
1056 ctx->argv[2], ctx->argc >= 4 ? ctx->argv[3] : NULL,
1057 &keys, &values, &n);
1058 ovsrec_bridge_set_external_ids(bridge->br_cfg, keys, values, n);
1060 char *key = xasprintf("fake-bridge-%s", ctx->argv[2]);
1061 struct vsctl_port *port = shash_find_data(&info.ports, ctx->argv[1]);
1062 set_external_id(port->port_cfg->key_external_ids,
1063 port->port_cfg->value_external_ids,
1064 port->port_cfg->n_external_ids,
1065 key, ctx->argc >= 4 ? ctx->argv[3] : NULL,
1066 &keys, &values, &n);
1067 ovsrec_port_set_external_ids(port->port_cfg, keys, values, n);
1077 get_external_id(char **keys, char **values, size_t n,
1078 const char *prefix, const char *key,
1081 size_t prefix_len = strlen(prefix);
1086 for (i = 0; i < n; i++) {
1087 if (!key && !strncmp(keys[i], prefix, prefix_len)) {
1088 svec_add_nocopy(&svec, xasprintf("%s=%s",
1089 keys[i] + prefix_len, values[i]));
1090 } else if (key_matches(keys[i], prefix, prefix_len, key)) {
1091 svec_add(&svec, values[i]);
1095 output_sorted(&svec, output);
1096 svec_destroy(&svec);
1100 cmd_br_get_external_id(struct vsctl_context *ctx)
1102 struct vsctl_info info;
1103 struct vsctl_bridge *bridge;
1105 get_info(ctx->ovs, &info);
1106 bridge = find_bridge(&info, ctx->argv[1], true);
1107 if (bridge->br_cfg) {
1108 get_external_id(bridge->br_cfg->key_external_ids,
1109 bridge->br_cfg->value_external_ids,
1110 bridge->br_cfg->n_external_ids,
1111 "", ctx->argc >= 3 ? ctx->argv[2] : NULL,
1114 struct vsctl_port *port = shash_find_data(&info.ports, ctx->argv[1]);
1115 get_external_id(port->port_cfg->key_external_ids,
1116 port->port_cfg->value_external_ids,
1117 port->port_cfg->n_external_ids,
1118 "fake-bridge-", ctx->argc >= 3 ? ctx->argv[2] : NULL, &ctx->output);
1125 cmd_list_ports(struct vsctl_context *ctx)
1127 struct vsctl_bridge *br;
1128 struct shash_node *node;
1129 struct vsctl_info info;
1132 get_info(ctx->ovs, &info);
1133 br = find_bridge(&info, ctx->argv[1], true);
1136 SHASH_FOR_EACH (node, &info.ports) {
1137 struct vsctl_port *port = node->data;
1139 if (strcmp(port->port_cfg->name, br->name) && br == port->bridge) {
1140 svec_add(&ports, port->port_cfg->name);
1143 output_sorted(&ports, &ctx->output);
1144 svec_destroy(&ports);
1150 add_port(struct vsctl_context *ctx,
1151 const char *br_name, const char *port_name, bool fake_iface,
1152 char *iface_names[], int n_ifaces)
1154 struct vsctl_info info;
1155 struct vsctl_bridge *bridge;
1156 struct ovsrec_interface **ifaces;
1157 struct ovsrec_port *port;
1160 get_info(ctx->ovs, &info);
1161 check_conflicts(&info, port_name,
1162 xasprintf("cannot create a port named %s", port_name));
1163 /* XXX need to check for conflicts on interfaces too */
1164 bridge = find_bridge(&info, br_name, true);
1166 ifaces = xmalloc(n_ifaces * sizeof *ifaces);
1167 for (i = 0; i < n_ifaces; i++) {
1168 ifaces[i] = ovsrec_interface_insert(ctx->txn);
1169 ovsrec_interface_set_name(ifaces[i], iface_names[i]);
1172 port = ovsrec_port_insert(ctx->txn);
1173 ovsrec_port_set_name(port, port_name);
1174 ovsrec_port_set_interfaces(port, ifaces, n_ifaces);
1175 ovsrec_port_set_bond_fake_iface(port, fake_iface);
1179 int64_t tag = bridge->vlan;
1180 ovsrec_port_set_tag(port, &tag, 1);
1183 bridge_insert_port((bridge->parent ? bridge->parent->br_cfg
1184 : bridge->br_cfg), port);
1190 cmd_add_port(struct vsctl_context *ctx)
1192 add_port(ctx, ctx->argv[1], ctx->argv[2], false, &ctx->argv[2], 1);
1196 cmd_add_bond(struct vsctl_context *ctx)
1198 bool fake_iface = shash_find(&ctx->options, "--fake-iface");
1200 add_port(ctx, ctx->argv[1], ctx->argv[2], fake_iface,
1201 &ctx->argv[3], ctx->argc - 3);
1205 cmd_del_port(struct vsctl_context *ctx)
1207 bool must_exist = !shash_find(&ctx->options, "--if-exists");
1208 struct vsctl_info info;
1210 get_info(ctx->ovs, &info);
1211 if (ctx->argc == 2) {
1212 struct vsctl_port *port = find_port(&info, ctx->argv[1], must_exist);
1214 del_port(&info, port);
1216 } else if (ctx->argc == 3) {
1217 struct vsctl_bridge *bridge = find_bridge(&info, ctx->argv[1], true);
1218 struct vsctl_port *port = find_port(&info, ctx->argv[2], must_exist);
1221 if (port->bridge == bridge) {
1222 del_port(&info, port);
1223 } else if (port->bridge->parent == bridge) {
1224 vsctl_fatal("bridge %s does not have a port %s (although its "
1225 "parent bridge %s does)",
1226 ctx->argv[1], ctx->argv[2], bridge->parent->name);
1228 vsctl_fatal("bridge %s does not have a port %s",
1229 ctx->argv[1], ctx->argv[2]);
1237 cmd_port_to_br(struct vsctl_context *ctx)
1239 struct vsctl_port *port;
1240 struct vsctl_info info;
1242 get_info(ctx->ovs, &info);
1243 port = find_port(&info, ctx->argv[1], true);
1244 ds_put_format(&ctx->output, "%s\n", port->bridge->name);
1249 cmd_br_to_vlan(struct vsctl_context *ctx)
1251 struct vsctl_bridge *bridge;
1252 struct vsctl_info info;
1254 get_info(ctx->ovs, &info);
1255 bridge = find_bridge(&info, ctx->argv[1], true);
1256 ds_put_format(&ctx->output, "%d\n", bridge->vlan);
1261 cmd_br_to_parent(struct vsctl_context *ctx)
1263 struct vsctl_bridge *bridge;
1264 struct vsctl_info info;
1266 get_info(ctx->ovs, &info);
1267 bridge = find_bridge(&info, ctx->argv[1], true);
1268 if (bridge->parent) {
1269 bridge = bridge->parent;
1271 ds_put_format(&ctx->output, "%s\n", bridge->name);
1276 cmd_list_ifaces(struct vsctl_context *ctx)
1278 struct vsctl_bridge *br;
1279 struct shash_node *node;
1280 struct vsctl_info info;
1283 get_info(ctx->ovs, &info);
1284 br = find_bridge(&info, ctx->argv[1], true);
1287 SHASH_FOR_EACH (node, &info.ifaces) {
1288 struct vsctl_iface *iface = node->data;
1290 if (strcmp(iface->iface_cfg->name, br->name)
1291 && br == iface->port->bridge) {
1292 svec_add(&ifaces, iface->iface_cfg->name);
1295 output_sorted(&ifaces, &ctx->output);
1296 svec_destroy(&ifaces);
1302 cmd_iface_to_br(struct vsctl_context *ctx)
1304 struct vsctl_iface *iface;
1305 struct vsctl_info info;
1307 get_info(ctx->ovs, &info);
1308 iface = find_iface(&info, ctx->argv[1], true);
1309 ds_put_format(&ctx->output, "%s\n", iface->port->bridge->name);
1314 cmd_get_controller(struct vsctl_context *ctx)
1316 struct vsctl_info info;
1318 get_info(ctx->ovs, &info);
1320 if (ctx->argc == 1) {
1321 /* Return the controller from the "Open_vSwitch" table */
1323 ds_put_format(&ctx->output, "%s\n", info.ctrl->target);
1326 /* Return the controller for a particular bridge. */
1327 struct vsctl_bridge *br = find_bridge(&info, ctx->argv[1], true);
1329 /* If no controller is explicitly defined for the requested
1330 * bridge, fallback to the "Open_vSwitch" table's controller. */
1332 ds_put_format(&ctx->output, "%s\n", br->ctrl->target);
1333 } else if (info.ctrl) {
1334 ds_put_format(&ctx->output, "%s\n", info.ctrl->target);
1342 cmd_del_controller(struct vsctl_context *ctx)
1344 struct vsctl_info info;
1346 get_info(ctx->ovs, &info);
1348 if (ctx->argc == 1) {
1350 ovsrec_controller_delete(info.ctrl);
1351 ovsrec_open_vswitch_set_controller(ctx->ovs, NULL);
1354 struct vsctl_bridge *br = find_real_bridge(&info, ctx->argv[1], true);
1357 ovsrec_controller_delete(br->ctrl);
1358 ovsrec_bridge_set_controller(br->br_cfg, NULL);
1366 cmd_set_controller(struct vsctl_context *ctx)
1368 struct vsctl_info info;
1369 struct ovsrec_controller *ctrl;
1371 get_info(ctx->ovs, &info);
1373 if (ctx->argc == 2) {
1374 /* Set the controller in the "Open_vSwitch" table. */
1376 ovsrec_controller_delete(info.ctrl);
1378 ctrl = ovsrec_controller_insert(ctx->txn);
1379 ovsrec_controller_set_target(ctrl, ctx->argv[1]);
1380 ovsrec_open_vswitch_set_controller(ctx->ovs, ctrl);
1382 /* Set the controller for a particular bridge. */
1383 struct vsctl_bridge *br = find_real_bridge(&info, ctx->argv[1], true);
1386 ovsrec_controller_delete(br->ctrl);
1388 ctrl = ovsrec_controller_insert(ctx->txn);
1389 ovsrec_controller_set_target(ctrl, ctx->argv[2]);
1390 ovsrec_bridge_set_controller(br->br_cfg, ctrl);
1397 cmd_get_fail_mode(struct vsctl_context *ctx)
1399 struct vsctl_info info;
1400 const char *fail_mode = NULL;
1402 get_info(ctx->ovs, &info);
1404 if (ctx->argc == 1) {
1405 /* Return the fail-mode from the "Open_vSwitch" table */
1406 if (info.ctrl && info.ctrl->fail_mode) {
1407 fail_mode = info.ctrl->fail_mode;
1410 /* Return the fail-mode for a particular bridge. */
1411 struct vsctl_bridge *br = find_bridge(&info, ctx->argv[1], true);
1413 /* If no controller or fail-mode is explicitly defined for the
1414 * requested bridge, fallback to the "Open_vSwitch" table's
1416 if (br->ctrl && br->ctrl->fail_mode) {
1417 fail_mode = br->ctrl->fail_mode;
1418 } else if (info.ctrl && info.ctrl->fail_mode) {
1419 fail_mode = info.ctrl->fail_mode;
1423 if (fail_mode && strlen(fail_mode)) {
1424 ds_put_format(&ctx->output, "%s\n", fail_mode);
1431 cmd_del_fail_mode(struct vsctl_context *ctx)
1433 struct vsctl_info info;
1435 get_info(ctx->ovs, &info);
1437 if (ctx->argc == 1) {
1438 if (info.ctrl && info.ctrl->fail_mode) {
1439 ovsrec_controller_set_fail_mode(info.ctrl, NULL);
1442 struct vsctl_bridge *br = find_real_bridge(&info, ctx->argv[1], true);
1444 if (br->ctrl && br->ctrl->fail_mode) {
1445 ovsrec_controller_set_fail_mode(br->ctrl, NULL);
1453 cmd_set_fail_mode(struct vsctl_context *ctx)
1455 struct vsctl_info info;
1456 const char *fail_mode;
1458 get_info(ctx->ovs, &info);
1460 fail_mode = (ctx->argc == 2) ? ctx->argv[1] : ctx->argv[2];
1462 if (strcmp(fail_mode, "standalone") && strcmp(fail_mode, "secure")) {
1463 vsctl_fatal("fail-mode must be \"standalone\" or \"secure\"");
1466 if (ctx->argc == 2) {
1467 /* Set the fail-mode in the "Open_vSwitch" table. */
1469 vsctl_fatal("no controller declared");
1471 ovsrec_controller_set_fail_mode(info.ctrl, fail_mode);
1473 struct vsctl_bridge *br = find_real_bridge(&info, ctx->argv[1], true);
1476 vsctl_fatal("no controller declared for %s", br->name);
1478 ovsrec_controller_set_fail_mode(br->ctrl, fail_mode);
1485 cmd_get_ssl(struct vsctl_context *ctx)
1487 struct ovsrec_ssl *ssl = ctx->ovs->ssl;
1490 ds_put_format(&ctx->output, "Private key: %s\n", ssl->private_key);
1491 ds_put_format(&ctx->output, "Certificate: %s\n", ssl->certificate);
1492 ds_put_format(&ctx->output, "CA Certificate: %s\n", ssl->ca_cert);
1493 ds_put_format(&ctx->output, "Bootstrap: %s\n",
1494 ssl->bootstrap_ca_cert ? "true" : "false");
1499 cmd_del_ssl(struct vsctl_context *ctx)
1501 struct ovsrec_ssl *ssl = ctx->ovs->ssl;
1504 ovsrec_ssl_delete(ssl);
1505 ovsrec_open_vswitch_set_ssl(ctx->ovs, NULL);
1510 cmd_set_ssl(struct vsctl_context *ctx)
1512 bool bootstrap = shash_find(&ctx->options, "--bootstrap");
1513 struct ovsrec_ssl *ssl = ctx->ovs->ssl;
1516 ovsrec_ssl_delete(ssl);
1518 ssl = ovsrec_ssl_insert(ctx->txn);
1520 ovsrec_ssl_set_private_key(ssl, ctx->argv[1]);
1521 ovsrec_ssl_set_certificate(ssl, ctx->argv[2]);
1522 ovsrec_ssl_set_ca_cert(ssl, ctx->argv[3]);
1524 ovsrec_ssl_set_bootstrap_ca_cert(ssl, bootstrap);
1526 ovsrec_open_vswitch_set_ssl(ctx->ovs, ssl);
1529 /* Parameter commands. */
1531 struct vsctl_row_id {
1532 const struct ovsdb_idl_table_class *table;
1533 const struct ovsdb_idl_column *name_column;
1534 const struct ovsdb_idl_column *uuid_column;
1537 struct vsctl_table_class {
1538 struct ovsdb_idl_table_class *class;
1539 struct vsctl_row_id row_ids[2];
1542 static const struct vsctl_table_class tables[] = {
1543 {&ovsrec_table_bridge,
1544 {{&ovsrec_table_bridge, &ovsrec_bridge_col_name, NULL},
1545 {NULL, NULL, NULL}}},
1547 {&ovsrec_table_controller,
1548 {{&ovsrec_table_bridge,
1549 &ovsrec_bridge_col_name,
1550 &ovsrec_bridge_col_controller},
1551 {&ovsrec_table_open_vswitch,
1553 &ovsrec_open_vswitch_col_controller}}},
1555 {&ovsrec_table_interface,
1556 {{&ovsrec_table_interface, &ovsrec_interface_col_name, NULL},
1557 {NULL, NULL, NULL}}},
1559 {&ovsrec_table_mirror,
1560 {{&ovsrec_table_mirror, &ovsrec_mirror_col_name, NULL},
1561 {NULL, NULL, NULL}}},
1563 {&ovsrec_table_netflow,
1564 {{&ovsrec_table_bridge,
1565 &ovsrec_bridge_col_name,
1566 &ovsrec_bridge_col_netflow},
1567 {NULL, NULL, NULL}}},
1569 {&ovsrec_table_open_vswitch,
1570 {{&ovsrec_table_open_vswitch, NULL, NULL},
1571 {NULL, NULL, NULL}}},
1573 {&ovsrec_table_port,
1574 {{&ovsrec_table_port, &ovsrec_port_col_name, NULL},
1575 {NULL, NULL, NULL}}},
1578 {{&ovsrec_table_open_vswitch, NULL, &ovsrec_open_vswitch_col_ssl}}},
1580 {NULL, {{NULL, NULL, NULL}, {NULL, NULL, NULL}}}
1584 die_if_error(char *error)
1587 vsctl_fatal("%s", error);
1592 to_lower_and_underscores(unsigned c)
1594 return c == '-' ? '_' : tolower(c);
1598 score_partial_match(const char *name, const char *s)
1602 if (!strcmp(name, s)) {
1605 for (score = 0; ; score++, name++, s++) {
1606 if (to_lower_and_underscores(*name) != to_lower_and_underscores(*s)) {
1608 } else if (*name == '\0') {
1609 return UINT_MAX - 1;
1612 return *s == '\0' ? score : 0;
1615 static const struct vsctl_table_class *
1616 get_table(const char *table_name)
1618 const struct vsctl_table_class *table;
1619 const struct vsctl_table_class *best_match = NULL;
1620 unsigned int best_score = 0;
1622 for (table = tables; table->class; table++) {
1623 unsigned int score = score_partial_match(table->class->name,
1625 if (score > best_score) {
1628 } else if (score == best_score) {
1634 } else if (best_score) {
1635 vsctl_fatal("multiple table names match \"%s\"", table_name);
1637 vsctl_fatal("unknown table \"%s\"", table_name);
1641 static const struct ovsdb_idl_row *
1642 get_row_by_id(struct vsctl_context *ctx, const struct vsctl_table_class *table,
1643 const struct vsctl_row_id *id, const char *record_id)
1645 const struct ovsdb_idl_row *referrer, *final;
1651 if (!id->name_column) {
1652 if (strcmp(record_id, ".")) {
1655 referrer = ovsdb_idl_first_row(ctx->idl, id->table);
1656 if (!referrer || ovsdb_idl_next_row(referrer)) {
1660 const struct ovsdb_idl_row *row;
1661 unsigned int best_score = 0;
1663 /* It might make sense to relax this assertion. */
1664 assert(id->name_column->type.key.type == OVSDB_TYPE_STRING);
1667 for (row = ovsdb_idl_first_row(ctx->idl, id->table);
1668 row != NULL && best_score != UINT_MAX;
1669 row = ovsdb_idl_next_row(row))
1671 struct ovsdb_datum name;
1673 ovsdb_idl_txn_read(row, id->name_column, &name);
1675 unsigned int score = score_partial_match(name.keys[0].string,
1677 if (score > best_score) {
1680 } else if (score == best_score) {
1684 ovsdb_datum_destroy(&name, &id->name_column->type);
1686 if (best_score && !referrer) {
1687 vsctl_fatal("multiple rows in %s match \"%s\"",
1688 table->class->name, record_id);
1696 if (id->uuid_column) {
1697 struct ovsdb_datum uuid;
1699 assert(id->uuid_column->type.key.type == OVSDB_TYPE_UUID);
1700 assert(id->uuid_column->type.value.type == OVSDB_TYPE_VOID);
1702 ovsdb_idl_txn_read(referrer, id->uuid_column, &uuid);
1704 final = ovsdb_idl_get_row_for_uuid(ctx->idl, table->class,
1705 &uuid.keys[0].uuid);
1707 ovsdb_datum_destroy(&uuid, &id->uuid_column->type);
1715 static const struct ovsdb_idl_row *
1716 get_row(struct vsctl_context *ctx,
1717 const struct vsctl_table_class *table, const char *record_id)
1719 const struct ovsdb_idl_row *row;
1722 if (uuid_from_string(&uuid, record_id)) {
1723 row = ovsdb_idl_get_row_for_uuid(ctx->idl, table->class, &uuid);
1727 for (i = 0; i < ARRAY_SIZE(table->row_ids); i++) {
1728 row = get_row_by_id(ctx, table, &table->row_ids[i], record_id);
1737 static const struct ovsdb_idl_row *
1738 must_get_row(struct vsctl_context *ctx,
1739 const struct vsctl_table_class *table, const char *record_id)
1741 const struct ovsdb_idl_row *row = get_row(ctx, table, record_id);
1743 vsctl_fatal("no row \"%s\" in table %s",
1744 record_id, table->class->name);
1750 get_column(const struct vsctl_table_class *table, const char *column_name,
1751 const struct ovsdb_idl_column **columnp)
1753 const struct ovsdb_idl_column *best_match = NULL;
1754 unsigned int best_score = 0;
1757 for (i = 0; i < table->class->n_columns; i++) {
1758 const struct ovsdb_idl_column *column = &table->class->columns[i];
1759 unsigned int score = score_partial_match(column->name, column_name);
1760 if (score > best_score) {
1761 best_match = column;
1763 } else if (score == best_score) {
1768 *columnp = best_match;
1771 } else if (best_score) {
1772 return xasprintf("%s contains more than one column whose name "
1773 "matches \"%s\"", table->class->name, column_name);
1775 return xasprintf("%s does not contain a column whose name matches "
1776 "\"%s\"", table->class->name, column_name);
1780 static char * WARN_UNUSED_RESULT
1781 parse_column_key_value(const char *arg, const struct vsctl_table_class *table,
1782 const struct ovsdb_idl_column **columnp,
1783 char **keyp, char **valuep)
1785 const char *p = arg;
1788 assert(columnp || keyp);
1796 /* Parse column name. */
1800 error = ovsdb_token_parse(&p, &column_name);
1804 if (column_name[0] == '\0') {
1806 error = xasprintf("%s: missing column name", arg);
1809 error = get_column(table, column_name, columnp);
1816 /* Parse key string. */
1817 if (*p == ':' || !columnp) {
1821 error = xasprintf("%s: key not accepted here", arg);
1824 error = ovsdb_token_parse(&p, keyp);
1832 /* Parse value string. */
1835 error = xasprintf("%s: value not accepted here", arg);
1838 *valuep = xstrdup(p + 1);
1844 error = xasprintf("%s: trailing garbage \"%s\" in argument",
1867 cmd_get(struct vsctl_context *ctx)
1869 bool if_exists = shash_find(&ctx->options, "--if-exists");
1870 const char *table_name = ctx->argv[1];
1871 const char *record_id = ctx->argv[2];
1872 const struct vsctl_table_class *table;
1873 const struct ovsdb_idl_row *row;
1874 struct ds *out = &ctx->output;
1877 table = get_table(table_name);
1878 row = must_get_row(ctx, table, record_id);
1879 for (i = 3; i < ctx->argc; i++) {
1880 const struct ovsdb_idl_column *column;
1881 struct ovsdb_datum datum;
1884 die_if_error(parse_column_key_value(ctx->argv[i], table,
1885 &column, &key_string, NULL));
1887 ovsdb_idl_txn_read(row, column, &datum);
1889 union ovsdb_atom key;
1892 if (column->type.value.type == OVSDB_TYPE_VOID) {
1893 vsctl_fatal("cannot specify key to get for non-map column %s",
1897 die_if_error(ovsdb_atom_from_string(&key,
1901 idx = ovsdb_datum_find_key(&datum, &key,
1902 column->type.key.type);
1903 if (idx == UINT_MAX) {
1905 vsctl_fatal("no key \"%s\" in %s record \"%s\" column %s",
1906 key_string, table->class->name, record_id,
1910 ovsdb_atom_to_string(&datum.values[idx],
1911 column->type.value.type, out);
1913 ovsdb_atom_destroy(&key, column->type.key.type);
1915 ovsdb_datum_to_string(&datum, &column->type, out);
1917 ds_put_char(out, '\n');
1918 ovsdb_datum_destroy(&datum, &column->type);
1925 list_record(const struct vsctl_table_class *table,
1926 const struct ovsdb_idl_row *row, struct ds *out)
1930 ds_put_format(out, "%-20s: "UUID_FMT"\n", "_uuid",
1931 UUID_ARGS(&row->uuid));
1932 for (i = 0; i < table->class->n_columns; i++) {
1933 const struct ovsdb_idl_column *column = &table->class->columns[i];
1934 struct ovsdb_datum datum;
1936 ovsdb_idl_txn_read(row, column, &datum);
1938 ds_put_format(out, "%-20s: ", column->name);
1939 ovsdb_datum_to_string(&datum, &column->type, out);
1940 ds_put_char(out, '\n');
1942 ovsdb_datum_destroy(&datum, &column->type);
1947 cmd_list(struct vsctl_context *ctx)
1949 const char *table_name = ctx->argv[1];
1950 const struct vsctl_table_class *table;
1951 struct ds *out = &ctx->output;
1954 table = get_table(table_name);
1955 if (ctx->argc > 2) {
1956 for (i = 2; i < ctx->argc; i++) {
1958 ds_put_char(out, '\n');
1960 list_record(table, must_get_row(ctx, table, ctx->argv[i]), out);
1963 const struct ovsdb_idl_row *row;
1966 for (row = ovsdb_idl_first_row(ctx->idl, table->class), first = true;
1968 row = ovsdb_idl_next_row(row), first = false) {
1970 ds_put_char(out, '\n');
1972 list_record(table, row, out);
1978 set_column(const struct vsctl_table_class *table,
1979 const struct ovsdb_idl_row *row, const char *arg)
1981 const struct ovsdb_idl_column *column;
1982 char *key_string, *value_string;
1985 error = parse_column_key_value(arg, table, &column, &key_string,
1987 die_if_error(error);
1988 if (!value_string) {
1989 vsctl_fatal("%s: missing value", arg);
1993 union ovsdb_atom key, value;
1994 struct ovsdb_datum old, new;
1996 if (column->type.value.type == OVSDB_TYPE_VOID) {
1997 vsctl_fatal("cannot specify key to set for non-map column %s",
2001 die_if_error(ovsdb_atom_from_string(&key, &column->type.key,
2003 die_if_error(ovsdb_atom_from_string(&value, &column->type.value,
2006 ovsdb_datum_init_empty(&new);
2007 ovsdb_datum_add_unsafe(&new, &key, &value, &column->type);
2009 ovsdb_atom_destroy(&key, column->type.key.type);
2010 ovsdb_atom_destroy(&value, column->type.value.type);
2012 ovsdb_idl_txn_read(row, column, &old);
2013 ovsdb_datum_union(&old, &new, &column->type, true);
2014 ovsdb_idl_txn_write(row, column, &old);
2016 ovsdb_datum_destroy(&new, &column->type);
2018 struct ovsdb_datum datum;
2020 die_if_error(ovsdb_datum_from_string(&datum, &column->type,
2022 ovsdb_idl_txn_write(row, column, &datum);
2030 cmd_set(struct vsctl_context *ctx)
2032 const char *table_name = ctx->argv[1];
2033 const char *record_id = ctx->argv[2];
2034 const struct vsctl_table_class *table;
2035 const struct ovsdb_idl_row *row;
2038 table = get_table(table_name);
2039 row = must_get_row(ctx, table, record_id);
2040 for (i = 3; i < ctx->argc; i++) {
2041 set_column(table, row, ctx->argv[i]);
2046 cmd_add(struct vsctl_context *ctx)
2048 const char *table_name = ctx->argv[1];
2049 const char *record_id = ctx->argv[2];
2050 const char *column_name = ctx->argv[3];
2051 const struct vsctl_table_class *table;
2052 const struct ovsdb_idl_column *column;
2053 const struct ovsdb_idl_row *row;
2054 const struct ovsdb_type *type;
2055 struct ovsdb_datum old;
2058 table = get_table(table_name);
2059 row = must_get_row(ctx, table, record_id);
2060 die_if_error(get_column(table, column_name, &column));
2062 type = &column->type;
2063 ovsdb_idl_txn_read(row, column, &old);
2064 for (i = 4; i < ctx->argc; i++) {
2065 struct ovsdb_type add_type;
2066 struct ovsdb_datum add;
2070 add_type.n_max = UINT_MAX;
2071 die_if_error(ovsdb_datum_from_string(&add, &add_type, ctx->argv[i]));
2072 ovsdb_datum_union(&old, &add, type, false);
2073 ovsdb_datum_destroy(&add, type);
2075 if (old.n > type->n_max) {
2076 vsctl_fatal("\"add\" operation would put %u %s in column %s of "
2077 "table %s but the maximum number is %u",
2079 type->value.type == OVSDB_TYPE_VOID ? "values" : "pairs",
2080 column->name, table->class->name, type->n_max);
2082 ovsdb_idl_txn_write(row, column, &old);
2086 cmd_remove(struct vsctl_context *ctx)
2088 const char *table_name = ctx->argv[1];
2089 const char *record_id = ctx->argv[2];
2090 const char *column_name = ctx->argv[3];
2091 const struct vsctl_table_class *table;
2092 const struct ovsdb_idl_column *column;
2093 const struct ovsdb_idl_row *row;
2094 const struct ovsdb_type *type;
2095 struct ovsdb_datum old;
2098 table = get_table(table_name);
2099 row = must_get_row(ctx, table, record_id);
2100 die_if_error(get_column(table, column_name, &column));
2102 type = &column->type;
2103 ovsdb_idl_txn_read(row, column, &old);
2104 for (i = 4; i < ctx->argc; i++) {
2105 struct ovsdb_type rm_type;
2106 struct ovsdb_datum rm;
2111 rm_type.n_max = UINT_MAX;
2112 error = ovsdb_datum_from_string(&rm, &rm_type, ctx->argv[i]);
2113 if (error && ovsdb_type_is_map(&rm_type)) {
2115 rm_type.value.type = OVSDB_TYPE_VOID;
2116 die_if_error(ovsdb_datum_from_string(&rm, &rm_type, ctx->argv[i]));
2118 ovsdb_datum_subtract(&old, type, &rm, &rm_type);
2119 ovsdb_datum_destroy(&rm, &rm_type);
2121 if (old.n < type->n_min) {
2122 vsctl_fatal("\"remove\" operation would put %u %s in column %s of "
2123 "table %s but the minimun number is %u",
2125 type->value.type == OVSDB_TYPE_VOID ? "values" : "pairs",
2126 column->name, table->class->name, type->n_min);
2128 ovsdb_idl_txn_write(row, column, &old);
2132 cmd_clear(struct vsctl_context *ctx)
2134 const char *table_name = ctx->argv[1];
2135 const char *record_id = ctx->argv[2];
2136 const struct vsctl_table_class *table;
2137 const struct ovsdb_idl_row *row;
2140 table = get_table(table_name);
2141 row = must_get_row(ctx, table, record_id);
2142 for (i = 3; i < ctx->argc; i++) {
2143 const struct ovsdb_idl_column *column;
2144 const struct ovsdb_type *type;
2145 struct ovsdb_datum datum;
2147 die_if_error(get_column(table, ctx->argv[i], &column));
2149 type = &column->type;
2150 if (type->n_min > 0) {
2151 vsctl_fatal("\"clear\" operation cannot be applied to column %s "
2152 "of table %s, which is not allowed to be empty",
2153 column->name, table->class->name);
2156 ovsdb_datum_init_empty(&datum);
2157 ovsdb_idl_txn_write(row, column, &datum);
2162 cmd_create(struct vsctl_context *ctx)
2164 const char *table_name = ctx->argv[1];
2165 const struct vsctl_table_class *table;
2166 const struct ovsdb_idl_row *row;
2169 table = get_table(table_name);
2170 row = ovsdb_idl_txn_insert(ctx->txn, table->class);
2171 for (i = 2; i < ctx->argc; i++) {
2172 set_column(table, row, ctx->argv[i]);
2174 ds_put_format(&ctx->output, UUID_FMT, UUID_ARGS(&row->uuid));
2177 /* This function may be used as the 'postprocess' function for commands that
2178 * insert new rows into the database. It expects that the command's 'run'
2179 * function prints the UUID reported by ovsdb_idl_txn_insert() as the command's
2180 * sole output. It replaces that output by the row's permanent UUID assigned
2181 * by the database server and appends a new-line.
2183 * Currently we use this only for "create", because the higher-level commands
2184 * are supposed to be independent of the actual structure of the vswitch
2187 post_create(struct vsctl_context *ctx)
2189 const struct uuid *real;
2192 uuid_from_string(&dummy, ds_cstr(&ctx->output));
2193 real = ovsdb_idl_txn_get_insert_uuid(ctx->txn, &dummy);
2195 ds_clear(&ctx->output);
2196 ds_put_format(&ctx->output, UUID_FMT, UUID_ARGS(real));
2198 ds_put_char(&ctx->output, '\n');
2202 cmd_destroy(struct vsctl_context *ctx)
2204 bool must_exist = !shash_find(&ctx->options, "--if-exists");
2205 const char *table_name = ctx->argv[1];
2206 const struct vsctl_table_class *table;
2209 table = get_table(table_name);
2210 for (i = 2; i < ctx->argc; i++) {
2211 const struct ovsdb_idl_row *row;
2213 row = (must_exist ? must_get_row : get_row)(ctx, table, ctx->argv[i]);
2215 ovsdb_idl_txn_delete(row);
2220 static struct json *
2221 where_uuid_equals(const struct uuid *uuid)
2224 json_array_create_1(
2225 json_array_create_3(
2226 json_string_create("_uuid"),
2227 json_string_create("=="),
2228 json_array_create_2(
2229 json_string_create("uuid"),
2230 json_string_create_nocopy(
2231 xasprintf(UUID_FMT, UUID_ARGS(uuid))))));
2235 vsctl_context_init(struct vsctl_context *ctx, struct vsctl_command *command,
2236 struct ovsdb_idl *idl, struct ovsdb_idl_txn *txn,
2237 const struct ovsrec_open_vswitch *ovs)
2239 ctx->argc = command->argc;
2240 ctx->argv = command->argv;
2241 ctx->options = command->options;
2243 ds_swap(&ctx->output, &command->output);
2251 vsctl_context_done(struct vsctl_context *ctx, struct vsctl_command *command)
2253 ds_swap(&ctx->output, &command->output);
2257 do_vsctl(const char *args, struct vsctl_command *commands, size_t n_commands,
2258 struct ovsdb_idl *idl)
2260 struct ovsdb_idl_txn *txn;
2261 const struct ovsrec_open_vswitch *ovs;
2262 enum ovsdb_idl_txn_status status;
2263 struct vsctl_command *c;
2264 int64_t next_cfg = 0;
2268 txn = the_idl_txn = ovsdb_idl_txn_create(idl);
2270 ovsdb_idl_txn_set_dry_run(txn);
2273 comment = xasprintf("ovs-vsctl: %s", args);
2274 ovsdb_idl_txn_add_comment(txn, comment);
2277 ovs = ovsrec_open_vswitch_first(idl);
2279 /* XXX add verification that table is empty */
2280 ovs = ovsrec_open_vswitch_insert(txn);
2283 if (wait_for_reload) {
2284 struct json *where = where_uuid_equals(&ovs->header_.uuid);
2285 ovsdb_idl_txn_increment(txn, "Open_vSwitch", "next_cfg", where);
2286 json_destroy(where);
2289 for (c = commands; c < &commands[n_commands]; c++) {
2290 struct vsctl_context ctx;
2292 ds_init(&c->output);
2293 vsctl_context_init(&ctx, c, idl, txn, ovs);
2294 (c->syntax->run)(&ctx);
2295 vsctl_context_done(&ctx, c);
2298 while ((status = ovsdb_idl_txn_commit(txn)) == TXN_INCOMPLETE) {
2300 ovsdb_idl_wait(idl);
2301 ovsdb_idl_txn_wait(txn);
2304 if (wait_for_reload && status == TXN_SUCCESS) {
2305 next_cfg = ovsdb_idl_txn_get_increment_new_value(txn);
2307 for (c = commands; c < &commands[n_commands]; c++) {
2308 if (c->syntax->postprocess) {
2309 struct vsctl_context ctx;
2311 vsctl_context_init(&ctx, c, idl, txn, ovs);
2312 (c->syntax->postprocess)(&ctx);
2313 vsctl_context_done(&ctx, c);
2316 error = xstrdup(ovsdb_idl_txn_get_error(txn));
2317 ovsdb_idl_txn_destroy(txn);
2321 case TXN_INCOMPLETE:
2325 /* Should not happen--we never call ovsdb_idl_txn_abort(). */
2326 vsctl_fatal("transaction aborted");
2333 for (c = commands; c < &commands[n_commands]; c++) {
2334 ds_destroy(&c->output);
2340 vsctl_fatal("transaction error: %s", error);
2347 for (c = commands; c < &commands[n_commands]; c++) {
2348 struct ds *ds = &c->output;
2353 for (j = 0; j < ds->length; j++) {
2354 int c = ds->string[j];
2357 fputs("\\n", stdout);
2361 fputs("\\\\", stdout);
2370 fputs(ds_cstr(ds), stdout);
2372 ds_destroy(&c->output);
2373 shash_destroy(&c->options);
2377 if (wait_for_reload && status != TXN_UNCHANGED) {
2379 const struct ovsrec_open_vswitch *ovs;
2382 OVSREC_OPEN_VSWITCH_FOR_EACH (ovs, idl) {
2383 if (ovs->cur_cfg >= next_cfg) {
2387 ovsdb_idl_wait(idl);
2392 ovsdb_idl_destroy(idl);
2397 static const struct vsctl_command_syntax all_commands[] = {
2398 /* Open vSwitch commands. */
2399 {"init", 0, 0, cmd_init, NULL, ""},
2401 /* Bridge commands. */
2402 {"add-br", 1, 3, cmd_add_br, NULL, "--may-exist"},
2403 {"del-br", 1, 1, cmd_del_br, NULL, "--if-exists"},
2404 {"list-br", 0, 0, cmd_list_br, NULL, ""},
2405 {"br-exists", 1, 1, cmd_br_exists, NULL, ""},
2406 {"br-to-vlan", 1, 1, cmd_br_to_vlan, NULL, ""},
2407 {"br-to-parent", 1, 1, cmd_br_to_parent, NULL, ""},
2408 {"br-set-external-id", 2, 3, cmd_br_set_external_id, NULL, ""},
2409 {"br-get-external-id", 1, 2, cmd_br_get_external_id, NULL, ""},
2411 /* Port commands. */
2412 {"list-ports", 1, 1, cmd_list_ports, NULL, ""},
2413 {"add-port", 2, 2, cmd_add_port, NULL, ""},
2414 {"add-bond", 4, INT_MAX, cmd_add_bond, NULL, "--fake-iface"},
2415 {"del-port", 1, 2, cmd_del_port, NULL, "--if-exists"},
2416 {"port-to-br", 1, 1, cmd_port_to_br, NULL, ""},
2418 /* Interface commands. */
2419 {"list-ifaces", 1, 1, cmd_list_ifaces, NULL, ""},
2420 {"iface-to-br", 1, 1, cmd_iface_to_br, NULL, ""},
2422 /* Controller commands. */
2423 {"get-controller", 0, 1, cmd_get_controller, NULL, ""},
2424 {"del-controller", 0, 1, cmd_del_controller, NULL, ""},
2425 {"set-controller", 1, 2, cmd_set_controller, NULL, ""},
2426 {"get-fail-mode", 0, 1, cmd_get_fail_mode, NULL, ""},
2427 {"del-fail-mode", 0, 1, cmd_del_fail_mode, NULL, ""},
2428 {"set-fail-mode", 1, 2, cmd_set_fail_mode, NULL, ""},
2431 {"get-ssl", 0, 0, cmd_get_ssl, NULL, ""},
2432 {"del-ssl", 0, 0, cmd_del_ssl, NULL, ""},
2433 {"set-ssl", 3, 3, cmd_set_ssl, NULL, "--bootstrap"},
2435 /* Parameter commands. */
2436 {"get", 3, INT_MAX, cmd_get, NULL, "--if-exists"},
2437 {"list", 1, INT_MAX, cmd_list, NULL, ""},
2438 {"set", 3, INT_MAX, cmd_set, NULL, ""},
2439 {"add", 4, INT_MAX, cmd_add, NULL, ""},
2440 {"remove", 4, INT_MAX, cmd_remove, NULL, ""},
2441 {"clear", 3, INT_MAX, cmd_clear, NULL, ""},
2442 {"create", 2, INT_MAX, cmd_create, post_create, ""},
2443 {"destroy", 1, INT_MAX, cmd_destroy, NULL, "--if-exists"},
2445 {NULL, 0, 0, NULL, NULL, NULL},