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 const char *br_name = ctx->argv[1];
811 struct vsctl_info info;
813 get_info(ctx->ovs, &info);
814 check_conflicts(&info, br_name,
815 xasprintf("cannot create a bridge named %s", br_name));
817 if (ctx->argc == 2) {
818 struct ovsrec_bridge *br;
819 struct ovsrec_port *port;
820 struct ovsrec_interface *iface;
822 iface = ovsrec_interface_insert(ctx->txn);
823 ovsrec_interface_set_name(iface, br_name);
825 port = ovsrec_port_insert(ctx->txn);
826 ovsrec_port_set_name(port, br_name);
827 ovsrec_port_set_interfaces(port, &iface, 1);
829 br = ovsrec_bridge_insert(ctx->txn);
830 ovsrec_bridge_set_name(br, br_name);
831 ovsrec_bridge_set_ports(br, &port, 1);
833 ovs_insert_bridge(ctx->ovs, br);
834 } else if (ctx->argc == 3) {
835 vsctl_fatal("'%s' command takes exactly 1 or 3 arguments",
837 } else if (ctx->argc == 4) {
838 const char *parent_name = ctx->argv[2];
839 int vlan = atoi(ctx->argv[3]);
840 struct ovsrec_bridge *br;
841 struct vsctl_bridge *parent;
842 struct ovsrec_port *port;
843 struct ovsrec_interface *iface;
846 if (vlan < 1 || vlan > 4095) {
847 vsctl_fatal("%s: vlan must be between 1 and 4095", ctx->argv[0]);
850 parent = find_bridge(&info, parent_name, false);
851 if (parent && parent->vlan) {
852 vsctl_fatal("cannot create bridge with fake bridge as parent");
855 vsctl_fatal("parent bridge %s does not exist", parent_name);
859 iface = ovsrec_interface_insert(ctx->txn);
860 ovsrec_interface_set_name(iface, br_name);
861 ovsrec_interface_set_type(iface, "internal");
863 port = ovsrec_port_insert(ctx->txn);
864 ovsrec_port_set_name(port, br_name);
865 ovsrec_port_set_interfaces(port, &iface, 1);
866 ovsrec_port_set_fake_bridge(port, true);
867 ovsrec_port_set_tag(port, &tag, 1);
869 bridge_insert_port(br, port);
878 del_port(struct vsctl_info *info, struct vsctl_port *port)
880 struct shash_node *node;
882 SHASH_FOR_EACH (node, &info->ifaces) {
883 struct vsctl_iface *iface = node->data;
884 if (iface->port == port) {
885 ovsrec_interface_delete(iface->iface_cfg);
888 ovsrec_port_delete(port->port_cfg);
890 bridge_delete_port((port->bridge->parent
891 ? port->bridge->parent->br_cfg
892 : port->bridge->br_cfg), port->port_cfg);
896 cmd_del_br(struct vsctl_context *ctx)
898 bool must_exist = !shash_find(&ctx->options, "--if-exists");
899 struct vsctl_bridge *bridge;
900 struct vsctl_info info;
902 get_info(ctx->ovs, &info);
903 bridge = find_bridge(&info, ctx->argv[1], must_exist);
905 struct shash_node *node;
907 SHASH_FOR_EACH (node, &info.ports) {
908 struct vsctl_port *port = node->data;
909 if (port->bridge == bridge || port->bridge->parent == bridge
910 || !strcmp(port->port_cfg->name, bridge->name)) {
911 del_port(&info, port);
914 if (bridge->br_cfg) {
915 ovsrec_bridge_delete(bridge->br_cfg);
916 ovs_delete_bridge(ctx->ovs, bridge->br_cfg);
923 output_sorted(struct svec *svec, struct ds *output)
929 SVEC_FOR_EACH (i, name, svec) {
930 ds_put_format(output, "%s\n", name);
935 cmd_list_br(struct vsctl_context *ctx)
937 struct shash_node *node;
938 struct vsctl_info info;
941 get_info(ctx->ovs, &info);
944 SHASH_FOR_EACH (node, &info.bridges) {
945 struct vsctl_bridge *br = node->data;
946 svec_add(&bridges, br->name);
948 output_sorted(&bridges, &ctx->output);
949 svec_destroy(&bridges);
955 cmd_br_exists(struct vsctl_context *ctx)
957 struct vsctl_info info;
959 get_info(ctx->ovs, &info);
960 if (!find_bridge(&info, ctx->argv[1], false)) {
966 /* Returns true if 'b_prefix' (of length 'b_prefix_len') concatenated with 'b'
967 * equals 'a', false otherwise. */
969 key_matches(const char *a,
970 const char *b_prefix, size_t b_prefix_len, const char *b)
972 return !strncmp(a, b_prefix, b_prefix_len) && !strcmp(a + b_prefix_len, b);
976 set_external_id(char **old_keys, char **old_values, size_t old_n,
977 char *key, char *value,
978 char ***new_keysp, char ***new_valuesp, size_t *new_np)
985 new_keys = xmalloc(sizeof *new_keys * (old_n + 1));
986 new_values = xmalloc(sizeof *new_values * (old_n + 1));
988 for (i = 0; i < old_n; i++) {
989 if (strcmp(key, old_keys[i])) {
990 new_keys[new_n] = old_keys[i];
991 new_values[new_n] = old_values[i];
996 new_keys[new_n] = key;
997 new_values[new_n] = value;
1000 *new_keysp = new_keys;
1001 *new_valuesp = new_values;
1006 cmd_br_set_external_id(struct vsctl_context *ctx)
1008 struct vsctl_info info;
1009 struct vsctl_bridge *bridge;
1010 char **keys, **values;
1013 get_info(ctx->ovs, &info);
1014 bridge = find_bridge(&info, ctx->argv[1], true);
1015 if (bridge->br_cfg) {
1016 set_external_id(bridge->br_cfg->key_external_ids,
1017 bridge->br_cfg->value_external_ids,
1018 bridge->br_cfg->n_external_ids,
1019 ctx->argv[2], ctx->argc >= 4 ? ctx->argv[3] : NULL,
1020 &keys, &values, &n);
1021 ovsrec_bridge_set_external_ids(bridge->br_cfg, keys, values, n);
1023 char *key = xasprintf("fake-bridge-%s", ctx->argv[2]);
1024 struct vsctl_port *port = shash_find_data(&info.ports, ctx->argv[1]);
1025 set_external_id(port->port_cfg->key_external_ids,
1026 port->port_cfg->value_external_ids,
1027 port->port_cfg->n_external_ids,
1028 key, ctx->argc >= 4 ? ctx->argv[3] : NULL,
1029 &keys, &values, &n);
1030 ovsrec_port_set_external_ids(port->port_cfg, keys, values, n);
1040 get_external_id(char **keys, char **values, size_t n,
1041 const char *prefix, const char *key,
1044 size_t prefix_len = strlen(prefix);
1049 for (i = 0; i < n; i++) {
1050 if (!key && !strncmp(keys[i], prefix, prefix_len)) {
1051 svec_add_nocopy(&svec, xasprintf("%s=%s",
1052 keys[i] + prefix_len, values[i]));
1053 } else if (key_matches(keys[i], prefix, prefix_len, key)) {
1054 svec_add(&svec, values[i]);
1058 output_sorted(&svec, output);
1059 svec_destroy(&svec);
1063 cmd_br_get_external_id(struct vsctl_context *ctx)
1065 struct vsctl_info info;
1066 struct vsctl_bridge *bridge;
1068 get_info(ctx->ovs, &info);
1069 bridge = find_bridge(&info, ctx->argv[1], true);
1070 if (bridge->br_cfg) {
1071 get_external_id(bridge->br_cfg->key_external_ids,
1072 bridge->br_cfg->value_external_ids,
1073 bridge->br_cfg->n_external_ids,
1074 "", ctx->argc >= 3 ? ctx->argv[2] : NULL,
1077 struct vsctl_port *port = shash_find_data(&info.ports, ctx->argv[1]);
1078 get_external_id(port->port_cfg->key_external_ids,
1079 port->port_cfg->value_external_ids,
1080 port->port_cfg->n_external_ids,
1081 "fake-bridge-", ctx->argc >= 3 ? ctx->argv[2] : NULL, &ctx->output);
1088 cmd_list_ports(struct vsctl_context *ctx)
1090 struct vsctl_bridge *br;
1091 struct shash_node *node;
1092 struct vsctl_info info;
1095 get_info(ctx->ovs, &info);
1096 br = find_bridge(&info, ctx->argv[1], true);
1099 SHASH_FOR_EACH (node, &info.ports) {
1100 struct vsctl_port *port = node->data;
1102 if (strcmp(port->port_cfg->name, br->name) && br == port->bridge) {
1103 svec_add(&ports, port->port_cfg->name);
1106 output_sorted(&ports, &ctx->output);
1107 svec_destroy(&ports);
1113 add_port(struct vsctl_context *ctx,
1114 const char *br_name, const char *port_name, bool fake_iface,
1115 char *iface_names[], int n_ifaces)
1117 struct vsctl_info info;
1118 struct vsctl_bridge *bridge;
1119 struct ovsrec_interface **ifaces;
1120 struct ovsrec_port *port;
1123 get_info(ctx->ovs, &info);
1124 check_conflicts(&info, port_name,
1125 xasprintf("cannot create a port named %s", port_name));
1126 /* XXX need to check for conflicts on interfaces too */
1127 bridge = find_bridge(&info, br_name, true);
1129 ifaces = xmalloc(n_ifaces * sizeof *ifaces);
1130 for (i = 0; i < n_ifaces; i++) {
1131 ifaces[i] = ovsrec_interface_insert(ctx->txn);
1132 ovsrec_interface_set_name(ifaces[i], iface_names[i]);
1135 port = ovsrec_port_insert(ctx->txn);
1136 ovsrec_port_set_name(port, port_name);
1137 ovsrec_port_set_interfaces(port, ifaces, n_ifaces);
1138 ovsrec_port_set_bond_fake_iface(port, fake_iface);
1142 int64_t tag = bridge->vlan;
1143 ovsrec_port_set_tag(port, &tag, 1);
1146 bridge_insert_port((bridge->parent ? bridge->parent->br_cfg
1147 : bridge->br_cfg), port);
1153 cmd_add_port(struct vsctl_context *ctx)
1155 add_port(ctx, ctx->argv[1], ctx->argv[2], false, &ctx->argv[2], 1);
1159 cmd_add_bond(struct vsctl_context *ctx)
1161 bool fake_iface = shash_find(&ctx->options, "--fake-iface");
1163 add_port(ctx, ctx->argv[1], ctx->argv[2], fake_iface,
1164 &ctx->argv[3], ctx->argc - 3);
1168 cmd_del_port(struct vsctl_context *ctx)
1170 bool must_exist = !shash_find(&ctx->options, "--if-exists");
1171 struct vsctl_info info;
1173 get_info(ctx->ovs, &info);
1174 if (ctx->argc == 2) {
1175 struct vsctl_port *port = find_port(&info, ctx->argv[1], must_exist);
1177 del_port(&info, port);
1179 } else if (ctx->argc == 3) {
1180 struct vsctl_bridge *bridge = find_bridge(&info, ctx->argv[1], true);
1181 struct vsctl_port *port = find_port(&info, ctx->argv[2], must_exist);
1184 if (port->bridge == bridge) {
1185 del_port(&info, port);
1186 } else if (port->bridge->parent == bridge) {
1187 vsctl_fatal("bridge %s does not have a port %s (although its "
1188 "parent bridge %s does)",
1189 ctx->argv[1], ctx->argv[2], bridge->parent->name);
1191 vsctl_fatal("bridge %s does not have a port %s",
1192 ctx->argv[1], ctx->argv[2]);
1200 cmd_port_to_br(struct vsctl_context *ctx)
1202 struct vsctl_port *port;
1203 struct vsctl_info info;
1205 get_info(ctx->ovs, &info);
1206 port = find_port(&info, ctx->argv[1], true);
1207 ds_put_format(&ctx->output, "%s\n", port->bridge->name);
1212 cmd_br_to_vlan(struct vsctl_context *ctx)
1214 struct vsctl_bridge *bridge;
1215 struct vsctl_info info;
1217 get_info(ctx->ovs, &info);
1218 bridge = find_bridge(&info, ctx->argv[1], true);
1219 ds_put_format(&ctx->output, "%d\n", bridge->vlan);
1224 cmd_br_to_parent(struct vsctl_context *ctx)
1226 struct vsctl_bridge *bridge;
1227 struct vsctl_info info;
1229 get_info(ctx->ovs, &info);
1230 bridge = find_bridge(&info, ctx->argv[1], true);
1231 if (bridge->parent) {
1232 bridge = bridge->parent;
1234 ds_put_format(&ctx->output, "%s\n", bridge->name);
1239 cmd_list_ifaces(struct vsctl_context *ctx)
1241 struct vsctl_bridge *br;
1242 struct shash_node *node;
1243 struct vsctl_info info;
1246 get_info(ctx->ovs, &info);
1247 br = find_bridge(&info, ctx->argv[1], true);
1250 SHASH_FOR_EACH (node, &info.ifaces) {
1251 struct vsctl_iface *iface = node->data;
1253 if (strcmp(iface->iface_cfg->name, br->name)
1254 && br == iface->port->bridge) {
1255 svec_add(&ifaces, iface->iface_cfg->name);
1258 output_sorted(&ifaces, &ctx->output);
1259 svec_destroy(&ifaces);
1265 cmd_iface_to_br(struct vsctl_context *ctx)
1267 struct vsctl_iface *iface;
1268 struct vsctl_info info;
1270 get_info(ctx->ovs, &info);
1271 iface = find_iface(&info, ctx->argv[1], true);
1272 ds_put_format(&ctx->output, "%s\n", iface->port->bridge->name);
1277 cmd_get_controller(struct vsctl_context *ctx)
1279 struct vsctl_info info;
1281 get_info(ctx->ovs, &info);
1283 if (ctx->argc == 1) {
1284 /* Return the controller from the "Open_vSwitch" table */
1286 ds_put_format(&ctx->output, "%s\n", info.ctrl->target);
1289 /* Return the controller for a particular bridge. */
1290 struct vsctl_bridge *br = find_bridge(&info, ctx->argv[1], true);
1292 /* If no controller is explicitly defined for the requested
1293 * bridge, fallback to the "Open_vSwitch" table's controller. */
1295 ds_put_format(&ctx->output, "%s\n", br->ctrl->target);
1296 } else if (info.ctrl) {
1297 ds_put_format(&ctx->output, "%s\n", info.ctrl->target);
1305 cmd_del_controller(struct vsctl_context *ctx)
1307 struct vsctl_info info;
1309 get_info(ctx->ovs, &info);
1311 if (ctx->argc == 1) {
1313 ovsrec_controller_delete(info.ctrl);
1314 ovsrec_open_vswitch_set_controller(ctx->ovs, NULL);
1317 struct vsctl_bridge *br = find_real_bridge(&info, ctx->argv[1], true);
1320 ovsrec_controller_delete(br->ctrl);
1321 ovsrec_bridge_set_controller(br->br_cfg, NULL);
1329 cmd_set_controller(struct vsctl_context *ctx)
1331 struct vsctl_info info;
1332 struct ovsrec_controller *ctrl;
1334 get_info(ctx->ovs, &info);
1336 if (ctx->argc == 2) {
1337 /* Set the controller in the "Open_vSwitch" table. */
1339 ovsrec_controller_delete(info.ctrl);
1341 ctrl = ovsrec_controller_insert(ctx->txn);
1342 ovsrec_controller_set_target(ctrl, ctx->argv[1]);
1343 ovsrec_open_vswitch_set_controller(ctx->ovs, ctrl);
1345 /* Set the controller for a particular bridge. */
1346 struct vsctl_bridge *br = find_real_bridge(&info, ctx->argv[1], true);
1349 ovsrec_controller_delete(br->ctrl);
1351 ctrl = ovsrec_controller_insert(ctx->txn);
1352 ovsrec_controller_set_target(ctrl, ctx->argv[2]);
1353 ovsrec_bridge_set_controller(br->br_cfg, ctrl);
1360 cmd_get_fail_mode(struct vsctl_context *ctx)
1362 struct vsctl_info info;
1363 const char *fail_mode = NULL;
1365 get_info(ctx->ovs, &info);
1367 if (ctx->argc == 1) {
1368 /* Return the fail-mode from the "Open_vSwitch" table */
1369 if (info.ctrl && info.ctrl->fail_mode) {
1370 fail_mode = info.ctrl->fail_mode;
1373 /* Return the fail-mode for a particular bridge. */
1374 struct vsctl_bridge *br = find_bridge(&info, ctx->argv[1], true);
1376 /* If no controller or fail-mode is explicitly defined for the
1377 * requested bridge, fallback to the "Open_vSwitch" table's
1379 if (br->ctrl && br->ctrl->fail_mode) {
1380 fail_mode = br->ctrl->fail_mode;
1381 } else if (info.ctrl && info.ctrl->fail_mode) {
1382 fail_mode = info.ctrl->fail_mode;
1386 if (fail_mode && strlen(fail_mode)) {
1387 ds_put_format(&ctx->output, "%s\n", fail_mode);
1394 cmd_del_fail_mode(struct vsctl_context *ctx)
1396 struct vsctl_info info;
1398 get_info(ctx->ovs, &info);
1400 if (ctx->argc == 1) {
1401 if (info.ctrl && info.ctrl->fail_mode) {
1402 ovsrec_controller_set_fail_mode(info.ctrl, NULL);
1405 struct vsctl_bridge *br = find_real_bridge(&info, ctx->argv[1], true);
1407 if (br->ctrl && br->ctrl->fail_mode) {
1408 ovsrec_controller_set_fail_mode(br->ctrl, NULL);
1416 cmd_set_fail_mode(struct vsctl_context *ctx)
1418 struct vsctl_info info;
1419 const char *fail_mode;
1421 get_info(ctx->ovs, &info);
1423 fail_mode = (ctx->argc == 2) ? ctx->argv[1] : ctx->argv[2];
1425 if (strcmp(fail_mode, "standalone") && strcmp(fail_mode, "secure")) {
1426 vsctl_fatal("fail-mode must be \"standalone\" or \"secure\"");
1429 if (ctx->argc == 2) {
1430 /* Set the fail-mode in the "Open_vSwitch" table. */
1432 vsctl_fatal("no controller declared");
1434 ovsrec_controller_set_fail_mode(info.ctrl, fail_mode);
1436 struct vsctl_bridge *br = find_real_bridge(&info, ctx->argv[1], true);
1439 vsctl_fatal("no controller declared for %s", br->name);
1441 ovsrec_controller_set_fail_mode(br->ctrl, fail_mode);
1448 cmd_get_ssl(struct vsctl_context *ctx)
1450 struct ovsrec_ssl *ssl = ctx->ovs->ssl;
1453 ds_put_format(&ctx->output, "Private key: %s\n", ssl->private_key);
1454 ds_put_format(&ctx->output, "Certificate: %s\n", ssl->certificate);
1455 ds_put_format(&ctx->output, "CA Certificate: %s\n", ssl->ca_cert);
1456 ds_put_format(&ctx->output, "Bootstrap: %s\n",
1457 ssl->bootstrap_ca_cert ? "true" : "false");
1462 cmd_del_ssl(struct vsctl_context *ctx)
1464 struct ovsrec_ssl *ssl = ctx->ovs->ssl;
1467 ovsrec_ssl_delete(ssl);
1468 ovsrec_open_vswitch_set_ssl(ctx->ovs, NULL);
1473 cmd_set_ssl(struct vsctl_context *ctx)
1475 bool bootstrap = shash_find(&ctx->options, "--bootstrap");
1476 struct ovsrec_ssl *ssl = ctx->ovs->ssl;
1479 ovsrec_ssl_delete(ssl);
1481 ssl = ovsrec_ssl_insert(ctx->txn);
1483 ovsrec_ssl_set_private_key(ssl, ctx->argv[1]);
1484 ovsrec_ssl_set_certificate(ssl, ctx->argv[2]);
1485 ovsrec_ssl_set_ca_cert(ssl, ctx->argv[3]);
1487 ovsrec_ssl_set_bootstrap_ca_cert(ssl, bootstrap);
1489 ovsrec_open_vswitch_set_ssl(ctx->ovs, ssl);
1492 /* Parameter commands. */
1494 struct vsctl_row_id {
1495 const struct ovsdb_idl_table_class *table;
1496 const struct ovsdb_idl_column *name_column;
1497 const struct ovsdb_idl_column *uuid_column;
1500 struct vsctl_table_class {
1501 struct ovsdb_idl_table_class *class;
1502 struct vsctl_row_id row_ids[2];
1505 static const struct vsctl_table_class tables[] = {
1506 {&ovsrec_table_bridge,
1507 {{&ovsrec_table_bridge, &ovsrec_bridge_col_name, NULL},
1508 {NULL, NULL, NULL}}},
1510 {&ovsrec_table_controller,
1511 {{&ovsrec_table_bridge,
1512 &ovsrec_bridge_col_name,
1513 &ovsrec_bridge_col_controller},
1514 {&ovsrec_table_open_vswitch,
1516 &ovsrec_open_vswitch_col_controller}}},
1518 {&ovsrec_table_interface,
1519 {{&ovsrec_table_interface, &ovsrec_interface_col_name, NULL},
1520 {NULL, NULL, NULL}}},
1522 {&ovsrec_table_mirror,
1523 {{&ovsrec_table_mirror, &ovsrec_mirror_col_name, NULL},
1524 {NULL, NULL, NULL}}},
1526 {&ovsrec_table_netflow,
1527 {{&ovsrec_table_bridge,
1528 &ovsrec_bridge_col_name,
1529 &ovsrec_bridge_col_netflow},
1530 {NULL, NULL, NULL}}},
1532 {&ovsrec_table_open_vswitch,
1533 {{&ovsrec_table_open_vswitch, NULL, NULL},
1534 {NULL, NULL, NULL}}},
1536 {&ovsrec_table_port,
1537 {{&ovsrec_table_port, &ovsrec_port_col_name, NULL},
1538 {NULL, NULL, NULL}}},
1541 {{&ovsrec_table_open_vswitch, NULL, &ovsrec_open_vswitch_col_ssl}}},
1543 {NULL, {{NULL, NULL, NULL}, {NULL, NULL, NULL}}}
1547 die_if_error(char *error)
1550 vsctl_fatal("%s", error);
1555 to_lower_and_underscores(unsigned c)
1557 return c == '-' ? '_' : tolower(c);
1561 score_partial_match(const char *name, const char *s)
1565 if (!strcmp(name, s)) {
1568 for (score = 0; ; score++, name++, s++) {
1569 if (to_lower_and_underscores(*name) != to_lower_and_underscores(*s)) {
1571 } else if (*name == '\0') {
1572 return UINT_MAX - 1;
1575 return *s == '\0' ? score : 0;
1578 static const struct vsctl_table_class *
1579 get_table(const char *table_name)
1581 const struct vsctl_table_class *table;
1582 const struct vsctl_table_class *best_match = NULL;
1583 unsigned int best_score = 0;
1585 for (table = tables; table->class; table++) {
1586 unsigned int score = score_partial_match(table->class->name,
1588 if (score > best_score) {
1591 } else if (score == best_score) {
1597 } else if (best_score) {
1598 vsctl_fatal("multiple table names match \"%s\"", table_name);
1600 vsctl_fatal("unknown table \"%s\"", table_name);
1604 static const struct ovsdb_idl_row *
1605 get_row_by_id(struct vsctl_context *ctx, const struct vsctl_table_class *table,
1606 const struct vsctl_row_id *id, const char *record_id)
1608 const struct ovsdb_idl_row *referrer, *final;
1614 if (!id->name_column) {
1615 if (strcmp(record_id, ".")) {
1618 referrer = ovsdb_idl_first_row(ctx->idl, id->table);
1619 if (!referrer || ovsdb_idl_next_row(referrer)) {
1623 const struct ovsdb_idl_row *row;
1624 unsigned int best_score = 0;
1626 /* It might make sense to relax this assertion. */
1627 assert(id->name_column->type.key.type == OVSDB_TYPE_STRING);
1630 for (row = ovsdb_idl_first_row(ctx->idl, id->table);
1631 row != NULL && best_score != UINT_MAX;
1632 row = ovsdb_idl_next_row(row))
1634 struct ovsdb_datum name;
1636 ovsdb_idl_txn_read(row, id->name_column, &name);
1638 unsigned int score = score_partial_match(name.keys[0].string,
1640 if (score > best_score) {
1643 } else if (score == best_score) {
1647 ovsdb_datum_destroy(&name, &id->name_column->type);
1649 if (best_score && !referrer) {
1650 vsctl_fatal("multiple rows in %s match \"%s\"",
1651 table->class->name, record_id);
1659 if (id->uuid_column) {
1660 struct ovsdb_datum uuid;
1662 assert(id->uuid_column->type.key.type == OVSDB_TYPE_UUID);
1663 assert(id->uuid_column->type.value.type == OVSDB_TYPE_VOID);
1665 ovsdb_idl_txn_read(referrer, id->uuid_column, &uuid);
1667 final = ovsdb_idl_get_row_for_uuid(ctx->idl, table->class,
1668 &uuid.keys[0].uuid);
1670 ovsdb_datum_destroy(&uuid, &id->uuid_column->type);
1678 static const struct ovsdb_idl_row *
1679 get_row(struct vsctl_context *ctx,
1680 const struct vsctl_table_class *table, const char *record_id)
1682 const struct ovsdb_idl_row *row;
1685 if (uuid_from_string(&uuid, record_id)) {
1686 row = ovsdb_idl_get_row_for_uuid(ctx->idl, table->class, &uuid);
1690 for (i = 0; i < ARRAY_SIZE(table->row_ids); i++) {
1691 row = get_row_by_id(ctx, table, &table->row_ids[i], record_id);
1700 static const struct ovsdb_idl_row *
1701 must_get_row(struct vsctl_context *ctx,
1702 const struct vsctl_table_class *table, const char *record_id)
1704 const struct ovsdb_idl_row *row = get_row(ctx, table, record_id);
1706 vsctl_fatal("no row \"%s\" in table %s",
1707 record_id, table->class->name);
1713 get_column(const struct vsctl_table_class *table, const char *column_name,
1714 const struct ovsdb_idl_column **columnp)
1716 const struct ovsdb_idl_column *best_match = NULL;
1717 unsigned int best_score = 0;
1720 for (i = 0; i < table->class->n_columns; i++) {
1721 const struct ovsdb_idl_column *column = &table->class->columns[i];
1722 unsigned int score = score_partial_match(column->name, column_name);
1723 if (score > best_score) {
1724 best_match = column;
1726 } else if (score == best_score) {
1731 *columnp = best_match;
1734 } else if (best_score) {
1735 return xasprintf("%s contains more than one column whose name "
1736 "matches \"%s\"", table->class->name, column_name);
1738 return xasprintf("%s does not contain a column whose name matches "
1739 "\"%s\"", table->class->name, column_name);
1743 static char * WARN_UNUSED_RESULT
1744 parse_column_key_value(const char *arg, const struct vsctl_table_class *table,
1745 const struct ovsdb_idl_column **columnp,
1746 char **keyp, char **valuep)
1748 const char *p = arg;
1751 assert(columnp || keyp);
1759 /* Parse column name. */
1763 error = ovsdb_token_parse(&p, &column_name);
1767 if (column_name[0] == '\0') {
1769 error = xasprintf("%s: missing column name", arg);
1772 error = get_column(table, column_name, columnp);
1779 /* Parse key string. */
1780 if (*p == ':' || !columnp) {
1784 error = xasprintf("%s: key not accepted here", arg);
1787 error = ovsdb_token_parse(&p, keyp);
1795 /* Parse value string. */
1798 error = xasprintf("%s: value not accepted here", arg);
1801 *valuep = xstrdup(p + 1);
1807 error = xasprintf("%s: trailing garbage \"%s\" in argument",
1830 cmd_get(struct vsctl_context *ctx)
1832 bool if_exists = shash_find(&ctx->options, "--if-exists");
1833 const char *table_name = ctx->argv[1];
1834 const char *record_id = ctx->argv[2];
1835 const struct vsctl_table_class *table;
1836 const struct ovsdb_idl_row *row;
1837 struct ds *out = &ctx->output;
1840 table = get_table(table_name);
1841 row = must_get_row(ctx, table, record_id);
1842 for (i = 3; i < ctx->argc; i++) {
1843 const struct ovsdb_idl_column *column;
1844 struct ovsdb_datum datum;
1847 die_if_error(parse_column_key_value(ctx->argv[i], table,
1848 &column, &key_string, NULL));
1850 ovsdb_idl_txn_read(row, column, &datum);
1852 union ovsdb_atom key;
1855 if (column->type.value.type == OVSDB_TYPE_VOID) {
1856 vsctl_fatal("cannot specify key to get for non-map column %s",
1860 die_if_error(ovsdb_atom_from_string(&key,
1864 idx = ovsdb_datum_find_key(&datum, &key,
1865 column->type.key.type);
1866 if (idx == UINT_MAX) {
1868 vsctl_fatal("no key \"%s\" in %s record \"%s\" column %s",
1869 key_string, table->class->name, record_id,
1873 ovsdb_atom_to_string(&datum.values[idx],
1874 column->type.value.type, out);
1876 ovsdb_atom_destroy(&key, column->type.key.type);
1878 ovsdb_datum_to_string(&datum, &column->type, out);
1880 ds_put_char(out, '\n');
1881 ovsdb_datum_destroy(&datum, &column->type);
1888 list_record(const struct vsctl_table_class *table,
1889 const struct ovsdb_idl_row *row, struct ds *out)
1893 ds_put_format(out, "%-20s: "UUID_FMT"\n", "_uuid",
1894 UUID_ARGS(&row->uuid));
1895 for (i = 0; i < table->class->n_columns; i++) {
1896 const struct ovsdb_idl_column *column = &table->class->columns[i];
1897 struct ovsdb_datum datum;
1899 ovsdb_idl_txn_read(row, column, &datum);
1901 ds_put_format(out, "%-20s: ", column->name);
1902 ovsdb_datum_to_string(&datum, &column->type, out);
1903 ds_put_char(out, '\n');
1905 ovsdb_datum_destroy(&datum, &column->type);
1910 cmd_list(struct vsctl_context *ctx)
1912 const char *table_name = ctx->argv[1];
1913 const struct vsctl_table_class *table;
1914 struct ds *out = &ctx->output;
1917 table = get_table(table_name);
1918 if (ctx->argc > 2) {
1919 for (i = 2; i < ctx->argc; i++) {
1921 ds_put_char(out, '\n');
1923 list_record(table, must_get_row(ctx, table, ctx->argv[i]), out);
1926 const struct ovsdb_idl_row *row;
1929 for (row = ovsdb_idl_first_row(ctx->idl, table->class), first = true;
1931 row = ovsdb_idl_next_row(row), first = false) {
1933 ds_put_char(out, '\n');
1935 list_record(table, row, out);
1941 set_column(const struct vsctl_table_class *table,
1942 const struct ovsdb_idl_row *row, const char *arg)
1944 const struct ovsdb_idl_column *column;
1945 char *key_string, *value_string;
1948 error = parse_column_key_value(arg, table, &column, &key_string,
1950 die_if_error(error);
1951 if (!value_string) {
1952 vsctl_fatal("%s: missing value", arg);
1956 union ovsdb_atom key, value;
1957 struct ovsdb_datum old, new;
1959 if (column->type.value.type == OVSDB_TYPE_VOID) {
1960 vsctl_fatal("cannot specify key to set for non-map column %s",
1964 die_if_error(ovsdb_atom_from_string(&key, &column->type.key,
1966 die_if_error(ovsdb_atom_from_string(&value, &column->type.value,
1969 ovsdb_datum_init_empty(&new);
1970 ovsdb_datum_add_unsafe(&new, &key, &value, &column->type);
1972 ovsdb_atom_destroy(&key, column->type.key.type);
1973 ovsdb_atom_destroy(&value, column->type.value.type);
1975 ovsdb_idl_txn_read(row, column, &old);
1976 ovsdb_datum_union(&old, &new, &column->type, true);
1977 ovsdb_idl_txn_write(row, column, &old);
1979 ovsdb_datum_destroy(&new, &column->type);
1981 struct ovsdb_datum datum;
1983 die_if_error(ovsdb_datum_from_string(&datum, &column->type,
1985 ovsdb_idl_txn_write(row, column, &datum);
1993 cmd_set(struct vsctl_context *ctx)
1995 const char *table_name = ctx->argv[1];
1996 const char *record_id = ctx->argv[2];
1997 const struct vsctl_table_class *table;
1998 const struct ovsdb_idl_row *row;
2001 table = get_table(table_name);
2002 row = must_get_row(ctx, table, record_id);
2003 for (i = 3; i < ctx->argc; i++) {
2004 set_column(table, row, ctx->argv[i]);
2009 cmd_add(struct vsctl_context *ctx)
2011 const char *table_name = ctx->argv[1];
2012 const char *record_id = ctx->argv[2];
2013 const char *column_name = ctx->argv[3];
2014 const struct vsctl_table_class *table;
2015 const struct ovsdb_idl_column *column;
2016 const struct ovsdb_idl_row *row;
2017 const struct ovsdb_type *type;
2018 struct ovsdb_datum old;
2021 table = get_table(table_name);
2022 row = must_get_row(ctx, table, record_id);
2023 die_if_error(get_column(table, column_name, &column));
2025 type = &column->type;
2026 ovsdb_idl_txn_read(row, column, &old);
2027 for (i = 4; i < ctx->argc; i++) {
2028 struct ovsdb_type add_type;
2029 struct ovsdb_datum add;
2033 add_type.n_max = UINT_MAX;
2034 die_if_error(ovsdb_datum_from_string(&add, &add_type, ctx->argv[i]));
2035 ovsdb_datum_union(&old, &add, type, false);
2036 ovsdb_datum_destroy(&add, type);
2038 if (old.n > type->n_max) {
2039 vsctl_fatal("\"add\" operation would put %u %s in column %s of "
2040 "table %s but the maximum number is %u",
2042 type->value.type == OVSDB_TYPE_VOID ? "values" : "pairs",
2043 column->name, table->class->name, type->n_max);
2045 ovsdb_idl_txn_write(row, column, &old);
2049 cmd_remove(struct vsctl_context *ctx)
2051 const char *table_name = ctx->argv[1];
2052 const char *record_id = ctx->argv[2];
2053 const char *column_name = ctx->argv[3];
2054 const struct vsctl_table_class *table;
2055 const struct ovsdb_idl_column *column;
2056 const struct ovsdb_idl_row *row;
2057 const struct ovsdb_type *type;
2058 struct ovsdb_datum old;
2061 table = get_table(table_name);
2062 row = must_get_row(ctx, table, record_id);
2063 die_if_error(get_column(table, column_name, &column));
2065 type = &column->type;
2066 ovsdb_idl_txn_read(row, column, &old);
2067 for (i = 4; i < ctx->argc; i++) {
2068 struct ovsdb_type rm_type;
2069 struct ovsdb_datum rm;
2074 rm_type.n_max = UINT_MAX;
2075 error = ovsdb_datum_from_string(&rm, &rm_type, ctx->argv[i]);
2076 if (error && ovsdb_type_is_map(&rm_type)) {
2078 rm_type.value.type = OVSDB_TYPE_VOID;
2079 die_if_error(ovsdb_datum_from_string(&rm, &rm_type, ctx->argv[i]));
2081 ovsdb_datum_subtract(&old, type, &rm, &rm_type);
2082 ovsdb_datum_destroy(&rm, &rm_type);
2084 if (old.n < type->n_min) {
2085 vsctl_fatal("\"remove\" operation would put %u %s in column %s of "
2086 "table %s but the minimun number is %u",
2088 type->value.type == OVSDB_TYPE_VOID ? "values" : "pairs",
2089 column->name, table->class->name, type->n_min);
2091 ovsdb_idl_txn_write(row, column, &old);
2095 cmd_clear(struct vsctl_context *ctx)
2097 const char *table_name = ctx->argv[1];
2098 const char *record_id = ctx->argv[2];
2099 const struct vsctl_table_class *table;
2100 const struct ovsdb_idl_row *row;
2103 table = get_table(table_name);
2104 row = must_get_row(ctx, table, record_id);
2105 for (i = 3; i < ctx->argc; i++) {
2106 const struct ovsdb_idl_column *column;
2107 const struct ovsdb_type *type;
2108 struct ovsdb_datum datum;
2110 die_if_error(get_column(table, ctx->argv[i], &column));
2112 type = &column->type;
2113 if (type->n_min > 0) {
2114 vsctl_fatal("\"clear\" operation cannot be applied to column %s "
2115 "of table %s, which is not allowed to be empty",
2116 column->name, table->class->name);
2119 ovsdb_datum_init_empty(&datum);
2120 ovsdb_idl_txn_write(row, column, &datum);
2125 cmd_create(struct vsctl_context *ctx)
2127 const char *table_name = ctx->argv[1];
2128 const struct vsctl_table_class *table;
2129 const struct ovsdb_idl_row *row;
2132 table = get_table(table_name);
2133 row = ovsdb_idl_txn_insert(ctx->txn, table->class);
2134 for (i = 2; i < ctx->argc; i++) {
2135 set_column(table, row, ctx->argv[i]);
2137 ds_put_format(&ctx->output, UUID_FMT, UUID_ARGS(&row->uuid));
2140 /* This function may be used as the 'postprocess' function for commands that
2141 * insert new rows into the database. It expects that the command's 'run'
2142 * function prints the UUID reported by ovsdb_idl_txn_insert() as the command's
2143 * sole output. It replaces that output by the row's permanent UUID assigned
2144 * by the database server and appends a new-line.
2146 * Currently we use this only for "create", because the higher-level commands
2147 * are supposed to be independent of the actual structure of the vswitch
2150 post_create(struct vsctl_context *ctx)
2152 const struct uuid *real;
2155 uuid_from_string(&dummy, ds_cstr(&ctx->output));
2156 real = ovsdb_idl_txn_get_insert_uuid(ctx->txn, &dummy);
2158 ds_clear(&ctx->output);
2159 ds_put_format(&ctx->output, UUID_FMT, UUID_ARGS(real));
2161 ds_put_char(&ctx->output, '\n');
2165 cmd_destroy(struct vsctl_context *ctx)
2167 bool must_exist = !shash_find(&ctx->options, "--if-exists");
2168 const char *table_name = ctx->argv[1];
2169 const struct vsctl_table_class *table;
2172 table = get_table(table_name);
2173 for (i = 2; i < ctx->argc; i++) {
2174 const struct ovsdb_idl_row *row;
2176 row = (must_exist ? must_get_row : get_row)(ctx, table, ctx->argv[i]);
2178 ovsdb_idl_txn_delete(row);
2183 static struct json *
2184 where_uuid_equals(const struct uuid *uuid)
2187 json_array_create_1(
2188 json_array_create_3(
2189 json_string_create("_uuid"),
2190 json_string_create("=="),
2191 json_array_create_2(
2192 json_string_create("uuid"),
2193 json_string_create_nocopy(
2194 xasprintf(UUID_FMT, UUID_ARGS(uuid))))));
2198 vsctl_context_init(struct vsctl_context *ctx, struct vsctl_command *command,
2199 struct ovsdb_idl *idl, struct ovsdb_idl_txn *txn,
2200 const struct ovsrec_open_vswitch *ovs)
2202 ctx->argc = command->argc;
2203 ctx->argv = command->argv;
2204 ctx->options = command->options;
2206 ds_swap(&ctx->output, &command->output);
2214 vsctl_context_done(struct vsctl_context *ctx, struct vsctl_command *command)
2216 ds_swap(&ctx->output, &command->output);
2220 do_vsctl(const char *args, struct vsctl_command *commands, size_t n_commands,
2221 struct ovsdb_idl *idl)
2223 struct ovsdb_idl_txn *txn;
2224 const struct ovsrec_open_vswitch *ovs;
2225 enum ovsdb_idl_txn_status status;
2226 struct vsctl_command *c;
2227 int64_t next_cfg = 0;
2231 txn = the_idl_txn = ovsdb_idl_txn_create(idl);
2233 ovsdb_idl_txn_set_dry_run(txn);
2236 comment = xasprintf("ovs-vsctl: %s", args);
2237 ovsdb_idl_txn_add_comment(txn, comment);
2240 ovs = ovsrec_open_vswitch_first(idl);
2242 /* XXX add verification that table is empty */
2243 ovs = ovsrec_open_vswitch_insert(txn);
2246 if (wait_for_reload) {
2247 struct json *where = where_uuid_equals(&ovs->header_.uuid);
2248 ovsdb_idl_txn_increment(txn, "Open_vSwitch", "next_cfg", where);
2249 json_destroy(where);
2252 for (c = commands; c < &commands[n_commands]; c++) {
2253 struct vsctl_context ctx;
2255 ds_init(&c->output);
2256 vsctl_context_init(&ctx, c, idl, txn, ovs);
2257 (c->syntax->run)(&ctx);
2258 vsctl_context_done(&ctx, c);
2261 while ((status = ovsdb_idl_txn_commit(txn)) == TXN_INCOMPLETE) {
2263 ovsdb_idl_wait(idl);
2264 ovsdb_idl_txn_wait(txn);
2267 if (wait_for_reload && status == TXN_SUCCESS) {
2268 next_cfg = ovsdb_idl_txn_get_increment_new_value(txn);
2270 for (c = commands; c < &commands[n_commands]; c++) {
2271 if (c->syntax->postprocess) {
2272 struct vsctl_context ctx;
2274 vsctl_context_init(&ctx, c, idl, txn, ovs);
2275 (c->syntax->postprocess)(&ctx);
2276 vsctl_context_done(&ctx, c);
2279 error = xstrdup(ovsdb_idl_txn_get_error(txn));
2280 ovsdb_idl_txn_destroy(txn);
2284 case TXN_INCOMPLETE:
2288 /* Should not happen--we never call ovsdb_idl_txn_abort(). */
2289 vsctl_fatal("transaction aborted");
2296 for (c = commands; c < &commands[n_commands]; c++) {
2297 ds_destroy(&c->output);
2303 vsctl_fatal("transaction error: %s", error);
2310 for (c = commands; c < &commands[n_commands]; c++) {
2311 struct ds *ds = &c->output;
2316 for (j = 0; j < ds->length; j++) {
2317 int c = ds->string[j];
2320 fputs("\\n", stdout);
2324 fputs("\\\\", stdout);
2333 fputs(ds_cstr(ds), stdout);
2335 ds_destroy(&c->output);
2336 shash_destroy(&c->options);
2340 if (wait_for_reload && status != TXN_UNCHANGED) {
2342 const struct ovsrec_open_vswitch *ovs;
2345 OVSREC_OPEN_VSWITCH_FOR_EACH (ovs, idl) {
2346 if (ovs->cur_cfg >= next_cfg) {
2350 ovsdb_idl_wait(idl);
2355 ovsdb_idl_destroy(idl);
2360 static const struct vsctl_command_syntax all_commands[] = {
2361 /* Open vSwitch commands. */
2362 {"init", 0, 0, cmd_init, NULL, ""},
2364 /* Bridge commands. */
2365 {"add-br", 1, 3, cmd_add_br, NULL, ""},
2366 {"del-br", 1, 1, cmd_del_br, NULL, "--if-exists"},
2367 {"list-br", 0, 0, cmd_list_br, NULL, ""},
2368 {"br-exists", 1, 1, cmd_br_exists, NULL, ""},
2369 {"br-to-vlan", 1, 1, cmd_br_to_vlan, NULL, ""},
2370 {"br-to-parent", 1, 1, cmd_br_to_parent, NULL, ""},
2371 {"br-set-external-id", 2, 3, cmd_br_set_external_id, NULL, ""},
2372 {"br-get-external-id", 1, 2, cmd_br_get_external_id, NULL, ""},
2374 /* Port commands. */
2375 {"list-ports", 1, 1, cmd_list_ports, NULL, ""},
2376 {"add-port", 2, 2, cmd_add_port, NULL, ""},
2377 {"add-bond", 4, INT_MAX, cmd_add_bond, NULL, "--fake-iface"},
2378 {"del-port", 1, 2, cmd_del_port, NULL, "--if-exists"},
2379 {"port-to-br", 1, 1, cmd_port_to_br, NULL, ""},
2381 /* Interface commands. */
2382 {"list-ifaces", 1, 1, cmd_list_ifaces, NULL, ""},
2383 {"iface-to-br", 1, 1, cmd_iface_to_br, NULL, ""},
2385 /* Controller commands. */
2386 {"get-controller", 0, 1, cmd_get_controller, NULL, ""},
2387 {"del-controller", 0, 1, cmd_del_controller, NULL, ""},
2388 {"set-controller", 1, 2, cmd_set_controller, NULL, ""},
2389 {"get-fail-mode", 0, 1, cmd_get_fail_mode, NULL, ""},
2390 {"del-fail-mode", 0, 1, cmd_del_fail_mode, NULL, ""},
2391 {"set-fail-mode", 1, 2, cmd_set_fail_mode, NULL, ""},
2394 {"get-ssl", 0, 0, cmd_get_ssl, NULL, ""},
2395 {"del-ssl", 0, 0, cmd_del_ssl, NULL, ""},
2396 {"set-ssl", 3, 3, cmd_set_ssl, NULL, "--bootstrap"},
2398 /* Parameter commands. */
2399 {"get", 3, INT_MAX, cmd_get, NULL, "--if-exists"},
2400 {"list", 1, INT_MAX, cmd_list, NULL, ""},
2401 {"set", 3, INT_MAX, cmd_set, NULL, ""},
2402 {"add", 4, INT_MAX, cmd_add, NULL, ""},
2403 {"remove", 4, INT_MAX, cmd_remove, NULL, ""},
2404 {"clear", 3, INT_MAX, cmd_clear, NULL, ""},
2405 {"create", 2, INT_MAX, cmd_create, post_create, ""},
2406 {"destroy", 1, INT_MAX, cmd_destroy, NULL, "--if-exists"},
2408 {NULL, 0, 0, NULL, NULL, NULL},