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) {
320 vsctl_fatal("'%s' command takes at most %d arguments",
321 p->name, p->max_args);
324 command->argc = n_arg + 1;
325 command->argv = &argv[i];
331 vsctl_fatal("unknown command '%s'; use --help for help", argv[i]);
335 vsctl_fatal(const char *format, ...)
340 va_start(args, format);
341 message = xvasprintf(format, args);
344 vlog_set_levels(VLM_vsctl, VLF_CONSOLE, VLL_EMER);
345 VLOG_ERR("%s", message);
346 ovs_error(0, "%s", message);
347 vsctl_exit(EXIT_FAILURE);
350 /* Frees the current transaction and the underlying IDL and then calls
353 * Freeing the transaction and the IDL is not strictly necessary, but it makes
354 * for a clean memory leak report from valgrind in the normal case. That makes
355 * it easier to notice real memory leaks. */
357 vsctl_exit(int status)
360 ovsdb_idl_txn_abort(the_idl_txn);
361 ovsdb_idl_txn_destroy(the_idl_txn);
363 ovsdb_idl_destroy(the_idl);
371 %s: ovs-vswitchd management utility\n\
372 usage: %s [OPTIONS] COMMAND [ARG...]\n\
375 add-br BRIDGE create a new bridge named BRIDGE\n\
376 add-br BRIDGE PARENT VLAN create new fake BRIDGE in PARENT on VLAN\n\
377 del-br BRIDGE delete BRIDGE and all of its ports\n\
378 list-br print the names of all the bridges\n\
379 br-exists BRIDGE test whether BRIDGE exists\n\
380 br-to-vlan BRIDGE print the VLAN which BRIDGE is on\n\
381 br-to-parent BRIDGE print the parent of BRIDGE\n\
382 br-set-external-id BRIDGE KEY VALUE set KEY on BRIDGE to VALUE\n\
383 br-set-external-id BRIDGE KEY unset KEY on BRIDGE\n\
384 br-get-external-id BRIDGE KEY print value of KEY on BRIDGE\n\
385 br-get-external-id BRIDGE list key-value pairs on BRIDGE\n\
388 list-ports BRIDGE print the names of all the ports on BRIDGE\n\
389 add-port BRIDGE PORT add network device PORT to BRIDGE\n\
390 add-bond BRIDGE PORT IFACE... add bonded port PORT in BRIDGE from IFACES\n\
391 del-port [BRIDGE] PORT delete PORT (which may be bonded) from BRIDGE\n\
392 port-to-br PORT print name of bridge that contains PORT\n\
393 A bond is considered to be a single port.\n\
395 Interface commands (a bond consists of multiple interfaces):\n\
396 list-ifaces BRIDGE print the names of all interfaces on BRIDGE\n\
397 iface-to-br IFACE print name of bridge that contains IFACE\n\
399 Controller commands:\n\
400 get-controller [BRIDGE] print the controller for BRIDGE\n\
401 del-controller [BRIDGE] delete the controller for BRIDGE\n\
402 set-controller [BRIDGE] TARGET set the controller for BRIDGE to TARGET\n\
403 get-fail-mode [BRIDGE] print the fail-mode for BRIDGE\n\
404 del-fail-mode [BRIDGE] delete the fail-mode for BRIDGE\n\
405 set-fail-mode [BRIDGE] MODE set the fail-mode for BRIDGE to MODE\n\
408 get-ssl print the SSL configuration\n\
409 del-ssl delete the SSL configuration\n\
410 set-ssl PRIV-KEY CERT CA-CERT set the SSL configuration\n\
412 Database commands:\n\
413 list TBL [REC] list RECord (or all records) in TBL\n\
414 get TBL REC COL[:KEY] print values of COLumns in RECORD in TBL\n\
415 set TBL REC COL[:KEY]=VALUE set COLumn values in RECord in TBL\n\
416 add TBL REC COL [KEY=]VALUE add (KEY=)VALUE to COLumn in RECord in TBL\n\
417 remove TBL REC COL [KEY=]VALUE remove (KEY=)VALUE from COLumn\n\
418 clear TBL REC COL clear values from COLumn in RECord in TBL\n\
419 create TBL COL[:KEY]=VALUE create and initialize new record\n\
420 destroy TBL REC delete REC from TBL\n\
421 Potentially unsafe database commands require --force option.\n\
424 --db=DATABASE connect to DATABASE\n\
426 --oneline print exactly one line of output per command\n",
427 program_name, program_name, default_db());
431 -h, --help display this help message\n\
432 -V, --version display version information\n");
441 def = xasprintf("unix:%s/ovsdb-server", ovs_rundir);
446 struct vsctl_context {
450 struct shash options;
452 /* Modifiable state. */
454 struct ovsdb_idl *idl;
455 struct ovsdb_idl_txn *txn;
456 const struct ovsrec_open_vswitch *ovs;
459 struct vsctl_bridge {
460 struct ovsrec_bridge *br_cfg;
462 struct ovsrec_controller *ctrl;
463 struct vsctl_bridge *parent;
468 struct ovsrec_port *port_cfg;
469 struct vsctl_bridge *bridge;
473 struct ovsrec_interface *iface_cfg;
474 struct vsctl_port *port;
478 struct shash bridges;
481 struct ovsrec_controller *ctrl;
484 static struct vsctl_bridge *
485 add_bridge(struct vsctl_info *b,
486 struct ovsrec_bridge *br_cfg, const char *name,
487 struct vsctl_bridge *parent, int vlan)
489 struct vsctl_bridge *br = xmalloc(sizeof *br);
491 br->name = xstrdup(name);
494 br->ctrl = parent ? parent->br_cfg->controller : br_cfg->controller;
495 shash_add(&b->bridges, br->name, br);
500 port_is_fake_bridge(const struct ovsrec_port *port_cfg)
502 return (port_cfg->fake_bridge
504 && *port_cfg->tag >= 1 && *port_cfg->tag <= 4095);
507 static struct vsctl_bridge *
508 find_vlan_bridge(struct vsctl_info *info,
509 struct vsctl_bridge *parent, int vlan)
511 struct shash_node *node;
513 SHASH_FOR_EACH (node, &info->bridges) {
514 struct vsctl_bridge *br = node->data;
515 if (br->parent == parent && br->vlan == vlan) {
524 free_info(struct vsctl_info *info)
526 struct shash_node *node;
528 SHASH_FOR_EACH (node, &info->bridges) {
529 struct vsctl_bridge *bridge = node->data;
533 shash_destroy(&info->bridges);
535 SHASH_FOR_EACH (node, &info->ports) {
536 struct vsctl_port *port = node->data;
539 shash_destroy(&info->ports);
541 SHASH_FOR_EACH (node, &info->ifaces) {
542 struct vsctl_iface *iface = node->data;
545 shash_destroy(&info->ifaces);
549 get_info(const struct ovsrec_open_vswitch *ovs, struct vsctl_info *info)
551 struct shash bridges, ports;
554 shash_init(&info->bridges);
555 shash_init(&info->ports);
556 shash_init(&info->ifaces);
558 info->ctrl = ovs->controller;
560 shash_init(&bridges);
562 for (i = 0; i < ovs->n_bridges; i++) {
563 struct ovsrec_bridge *br_cfg = ovs->bridges[i];
564 struct vsctl_bridge *br;
567 if (!shash_add_once(&bridges, br_cfg->name, NULL)) {
568 VLOG_WARN("%s: database contains duplicate bridge name",
572 br = add_bridge(info, br_cfg, br_cfg->name, NULL, 0);
577 for (j = 0; j < br_cfg->n_ports; j++) {
578 struct ovsrec_port *port_cfg = br_cfg->ports[j];
580 if (!shash_add_once(&ports, port_cfg->name, NULL)) {
581 VLOG_WARN("%s: database contains duplicate port name",
586 if (port_is_fake_bridge(port_cfg)
587 && shash_add_once(&bridges, port_cfg->name, NULL)) {
588 add_bridge(info, NULL, port_cfg->name, br, *port_cfg->tag);
592 shash_destroy(&bridges);
593 shash_destroy(&ports);
595 shash_init(&bridges);
597 for (i = 0; i < ovs->n_bridges; i++) {
598 struct ovsrec_bridge *br_cfg = ovs->bridges[i];
599 struct vsctl_bridge *br;
602 if (!shash_add_once(&bridges, br_cfg->name, NULL)) {
605 br = shash_find_data(&info->bridges, br_cfg->name);
606 for (j = 0; j < br_cfg->n_ports; j++) {
607 struct ovsrec_port *port_cfg = br_cfg->ports[j];
608 struct vsctl_port *port;
611 if (!shash_add_once(&ports, port_cfg->name, NULL)) {
615 if (port_is_fake_bridge(port_cfg)
616 && !shash_add_once(&bridges, port_cfg->name, NULL)) {
620 port = xmalloc(sizeof *port);
621 port->port_cfg = port_cfg;
623 && *port_cfg->tag >= 1 && *port_cfg->tag <= 4095) {
624 port->bridge = find_vlan_bridge(info, br, *port_cfg->tag);
631 shash_add(&info->ports, port_cfg->name, port);
633 for (k = 0; k < port_cfg->n_interfaces; k++) {
634 struct ovsrec_interface *iface_cfg = port_cfg->interfaces[k];
635 struct vsctl_iface *iface;
637 if (shash_find(&info->ifaces, iface_cfg->name)) {
638 VLOG_WARN("%s: database contains duplicate interface name",
643 iface = xmalloc(sizeof *iface);
644 iface->iface_cfg = iface_cfg;
646 shash_add(&info->ifaces, iface_cfg->name, iface);
650 shash_destroy(&bridges);
651 shash_destroy(&ports);
655 check_conflicts(struct vsctl_info *info, const char *name,
658 struct vsctl_iface *iface;
659 struct vsctl_port *port;
661 if (shash_find(&info->bridges, name)) {
662 vsctl_fatal("%s because a bridge named %s already exists",
666 port = shash_find_data(&info->ports, name);
668 vsctl_fatal("%s because a port named %s already exists on "
669 "bridge %s", msg, name, port->bridge->name);
672 iface = shash_find_data(&info->ifaces, name);
674 vsctl_fatal("%s because an interface named %s already exists "
675 "on bridge %s", msg, name, iface->port->bridge->name);
681 static struct vsctl_bridge *
682 find_bridge(struct vsctl_info *info, const char *name, bool must_exist)
684 struct vsctl_bridge *br = shash_find_data(&info->bridges, name);
685 if (must_exist && !br) {
686 vsctl_fatal("no bridge named %s", name);
691 static struct vsctl_bridge *
692 find_real_bridge(struct vsctl_info *info, const char *name, bool must_exist)
694 struct vsctl_bridge *br = find_bridge(info, name, must_exist);
695 if (br && br->parent) {
696 vsctl_fatal("%s is a fake bridge", name);
701 static struct vsctl_port *
702 find_port(struct vsctl_info *info, const char *name, bool must_exist)
704 struct vsctl_port *port = shash_find_data(&info->ports, name);
705 if (port && !strcmp(name, port->bridge->name)) {
708 if (must_exist && !port) {
709 vsctl_fatal("no port named %s", name);
714 static struct vsctl_iface *
715 find_iface(struct vsctl_info *info, const char *name, bool must_exist)
717 struct vsctl_iface *iface = shash_find_data(&info->ifaces, name);
718 if (iface && !strcmp(name, iface->port->bridge->name)) {
721 if (must_exist && !iface) {
722 vsctl_fatal("no interface named %s", name);
728 bridge_insert_port(struct ovsrec_bridge *br, struct ovsrec_port *port)
730 struct ovsrec_port **ports;
733 ports = xmalloc(sizeof *br->ports * (br->n_ports + 1));
734 for (i = 0; i < br->n_ports; i++) {
735 ports[i] = br->ports[i];
737 ports[br->n_ports] = port;
738 ovsrec_bridge_set_ports(br, ports, br->n_ports + 1);
743 bridge_delete_port(struct ovsrec_bridge *br, struct ovsrec_port *port)
745 struct ovsrec_port **ports;
748 ports = xmalloc(sizeof *br->ports * br->n_ports);
749 for (i = n = 0; i < br->n_ports; i++) {
750 if (br->ports[i] != port) {
751 ports[n++] = br->ports[i];
754 ovsrec_bridge_set_ports(br, ports, n);
759 ovs_insert_bridge(const struct ovsrec_open_vswitch *ovs,
760 struct ovsrec_bridge *bridge)
762 struct ovsrec_bridge **bridges;
765 bridges = xmalloc(sizeof *ovs->bridges * (ovs->n_bridges + 1));
766 for (i = 0; i < ovs->n_bridges; i++) {
767 bridges[i] = ovs->bridges[i];
769 bridges[ovs->n_bridges] = bridge;
770 ovsrec_open_vswitch_set_bridges(ovs, bridges, ovs->n_bridges + 1);
775 ovs_delete_bridge(const struct ovsrec_open_vswitch *ovs,
776 struct ovsrec_bridge *bridge)
778 struct ovsrec_bridge **bridges;
781 bridges = xmalloc(sizeof *ovs->bridges * ovs->n_bridges);
782 for (i = n = 0; i < ovs->n_bridges; i++) {
783 if (ovs->bridges[i] != bridge) {
784 bridges[n++] = ovs->bridges[i];
787 ovsrec_open_vswitch_set_bridges(ovs, bridges, n);
792 cmd_init(struct vsctl_context *ctx UNUSED)
797 cmd_add_br(struct vsctl_context *ctx)
799 const char *br_name = ctx->argv[1];
800 struct vsctl_info info;
802 get_info(ctx->ovs, &info);
803 check_conflicts(&info, br_name,
804 xasprintf("cannot create a bridge named %s", br_name));
806 if (ctx->argc == 2) {
807 struct ovsrec_bridge *br;
808 struct ovsrec_port *port;
809 struct ovsrec_interface *iface;
811 iface = ovsrec_interface_insert(ctx->txn);
812 ovsrec_interface_set_name(iface, br_name);
814 port = ovsrec_port_insert(ctx->txn);
815 ovsrec_port_set_name(port, br_name);
816 ovsrec_port_set_interfaces(port, &iface, 1);
818 br = ovsrec_bridge_insert(ctx->txn);
819 ovsrec_bridge_set_name(br, br_name);
820 ovsrec_bridge_set_ports(br, &port, 1);
822 ovs_insert_bridge(ctx->ovs, br);
823 } else if (ctx->argc == 3) {
824 vsctl_fatal("'%s' command takes exactly 1 or 3 arguments",
826 } else if (ctx->argc == 4) {
827 const char *parent_name = ctx->argv[2];
828 int vlan = atoi(ctx->argv[3]);
829 struct ovsrec_bridge *br;
830 struct vsctl_bridge *parent;
831 struct ovsrec_port *port;
832 struct ovsrec_interface *iface;
835 if (vlan < 1 || vlan > 4095) {
836 vsctl_fatal("%s: vlan must be between 1 and 4095", ctx->argv[0]);
839 parent = find_bridge(&info, parent_name, false);
840 if (parent && parent->vlan) {
841 vsctl_fatal("cannot create bridge with fake bridge as parent");
844 vsctl_fatal("parent bridge %s does not exist", parent_name);
848 iface = ovsrec_interface_insert(ctx->txn);
849 ovsrec_interface_set_name(iface, br_name);
850 ovsrec_interface_set_type(iface, "internal");
852 port = ovsrec_port_insert(ctx->txn);
853 ovsrec_port_set_name(port, br_name);
854 ovsrec_port_set_interfaces(port, &iface, 1);
855 ovsrec_port_set_fake_bridge(port, true);
856 ovsrec_port_set_tag(port, &tag, 1);
858 bridge_insert_port(br, port);
867 del_port(struct vsctl_info *info, struct vsctl_port *port)
869 struct shash_node *node;
871 SHASH_FOR_EACH (node, &info->ifaces) {
872 struct vsctl_iface *iface = node->data;
873 if (iface->port == port) {
874 ovsrec_interface_delete(iface->iface_cfg);
877 ovsrec_port_delete(port->port_cfg);
879 bridge_delete_port((port->bridge->parent
880 ? port->bridge->parent->br_cfg
881 : port->bridge->br_cfg), port->port_cfg);
885 cmd_del_br(struct vsctl_context *ctx)
887 bool must_exist = !shash_find(&ctx->options, "--if-exists");
888 struct vsctl_bridge *bridge;
889 struct vsctl_info info;
891 get_info(ctx->ovs, &info);
892 bridge = find_bridge(&info, ctx->argv[1], must_exist);
894 struct shash_node *node;
896 SHASH_FOR_EACH (node, &info.ports) {
897 struct vsctl_port *port = node->data;
898 if (port->bridge == bridge || port->bridge->parent == bridge
899 || !strcmp(port->port_cfg->name, bridge->name)) {
900 del_port(&info, port);
903 if (bridge->br_cfg) {
904 ovsrec_bridge_delete(bridge->br_cfg);
905 ovs_delete_bridge(ctx->ovs, bridge->br_cfg);
912 output_sorted(struct svec *svec, struct ds *output)
918 SVEC_FOR_EACH (i, name, svec) {
919 ds_put_format(output, "%s\n", name);
924 cmd_list_br(struct vsctl_context *ctx)
926 struct shash_node *node;
927 struct vsctl_info info;
930 get_info(ctx->ovs, &info);
933 SHASH_FOR_EACH (node, &info.bridges) {
934 struct vsctl_bridge *br = node->data;
935 svec_add(&bridges, br->name);
937 output_sorted(&bridges, &ctx->output);
938 svec_destroy(&bridges);
944 cmd_br_exists(struct vsctl_context *ctx)
946 struct vsctl_info info;
948 get_info(ctx->ovs, &info);
949 if (!find_bridge(&info, ctx->argv[1], false)) {
955 /* Returns true if 'b_prefix' (of length 'b_prefix_len') concatenated with 'b'
956 * equals 'a', false otherwise. */
958 key_matches(const char *a,
959 const char *b_prefix, size_t b_prefix_len, const char *b)
961 return !strncmp(a, b_prefix, b_prefix_len) && !strcmp(a + b_prefix_len, b);
965 set_external_id(char **old_keys, char **old_values, size_t old_n,
966 char *key, char *value,
967 char ***new_keysp, char ***new_valuesp, size_t *new_np)
974 new_keys = xmalloc(sizeof *new_keys * (old_n + 1));
975 new_values = xmalloc(sizeof *new_values * (old_n + 1));
977 for (i = 0; i < old_n; i++) {
978 if (strcmp(key, old_keys[i])) {
979 new_keys[new_n] = old_keys[i];
980 new_values[new_n] = old_values[i];
985 new_keys[new_n] = key;
986 new_values[new_n] = value;
989 *new_keysp = new_keys;
990 *new_valuesp = new_values;
995 cmd_br_set_external_id(struct vsctl_context *ctx)
997 struct vsctl_info info;
998 struct vsctl_bridge *bridge;
999 char **keys, **values;
1002 get_info(ctx->ovs, &info);
1003 bridge = find_bridge(&info, ctx->argv[1], true);
1004 if (bridge->br_cfg) {
1005 set_external_id(bridge->br_cfg->key_external_ids,
1006 bridge->br_cfg->value_external_ids,
1007 bridge->br_cfg->n_external_ids,
1008 ctx->argv[2], ctx->argc >= 4 ? ctx->argv[3] : NULL,
1009 &keys, &values, &n);
1010 ovsrec_bridge_set_external_ids(bridge->br_cfg, keys, values, n);
1012 char *key = xasprintf("fake-bridge-%s", ctx->argv[2]);
1013 struct vsctl_port *port = shash_find_data(&info.ports, ctx->argv[1]);
1014 set_external_id(port->port_cfg->key_external_ids,
1015 port->port_cfg->value_external_ids,
1016 port->port_cfg->n_external_ids,
1017 key, ctx->argc >= 4 ? ctx->argv[3] : NULL,
1018 &keys, &values, &n);
1019 ovsrec_port_set_external_ids(port->port_cfg, keys, values, n);
1029 get_external_id(char **keys, char **values, size_t n,
1030 const char *prefix, const char *key,
1033 size_t prefix_len = strlen(prefix);
1038 for (i = 0; i < n; i++) {
1039 if (!key && !strncmp(keys[i], prefix, prefix_len)) {
1040 svec_add_nocopy(&svec, xasprintf("%s=%s",
1041 keys[i] + prefix_len, values[i]));
1042 } else if (key_matches(keys[i], prefix, prefix_len, key)) {
1043 svec_add(&svec, values[i]);
1047 output_sorted(&svec, output);
1048 svec_destroy(&svec);
1052 cmd_br_get_external_id(struct vsctl_context *ctx)
1054 struct vsctl_info info;
1055 struct vsctl_bridge *bridge;
1057 get_info(ctx->ovs, &info);
1058 bridge = find_bridge(&info, ctx->argv[1], true);
1059 if (bridge->br_cfg) {
1060 get_external_id(bridge->br_cfg->key_external_ids,
1061 bridge->br_cfg->value_external_ids,
1062 bridge->br_cfg->n_external_ids,
1063 "", ctx->argc >= 3 ? ctx->argv[2] : NULL,
1066 struct vsctl_port *port = shash_find_data(&info.ports, ctx->argv[1]);
1067 get_external_id(port->port_cfg->key_external_ids,
1068 port->port_cfg->value_external_ids,
1069 port->port_cfg->n_external_ids,
1070 "fake-bridge-", ctx->argc >= 3 ? ctx->argv[2] : NULL, &ctx->output);
1077 cmd_list_ports(struct vsctl_context *ctx)
1079 struct vsctl_bridge *br;
1080 struct shash_node *node;
1081 struct vsctl_info info;
1084 get_info(ctx->ovs, &info);
1085 br = find_bridge(&info, ctx->argv[1], true);
1088 SHASH_FOR_EACH (node, &info.ports) {
1089 struct vsctl_port *port = node->data;
1091 if (strcmp(port->port_cfg->name, br->name) && br == port->bridge) {
1092 svec_add(&ports, port->port_cfg->name);
1095 output_sorted(&ports, &ctx->output);
1096 svec_destroy(&ports);
1102 add_port(struct vsctl_context *ctx,
1103 const char *br_name, const char *port_name, bool fake_iface,
1104 char *iface_names[], int n_ifaces)
1106 struct vsctl_info info;
1107 struct vsctl_bridge *bridge;
1108 struct ovsrec_interface **ifaces;
1109 struct ovsrec_port *port;
1112 get_info(ctx->ovs, &info);
1113 check_conflicts(&info, port_name,
1114 xasprintf("cannot create a port named %s", port_name));
1115 /* XXX need to check for conflicts on interfaces too */
1116 bridge = find_bridge(&info, br_name, true);
1118 ifaces = xmalloc(n_ifaces * sizeof *ifaces);
1119 for (i = 0; i < n_ifaces; i++) {
1120 ifaces[i] = ovsrec_interface_insert(ctx->txn);
1121 ovsrec_interface_set_name(ifaces[i], iface_names[i]);
1124 port = ovsrec_port_insert(ctx->txn);
1125 ovsrec_port_set_name(port, port_name);
1126 ovsrec_port_set_interfaces(port, ifaces, n_ifaces);
1127 ovsrec_port_set_bond_fake_iface(port, fake_iface);
1131 int64_t tag = bridge->vlan;
1132 ovsrec_port_set_tag(port, &tag, 1);
1135 bridge_insert_port((bridge->parent ? bridge->parent->br_cfg
1136 : bridge->br_cfg), port);
1142 cmd_add_port(struct vsctl_context *ctx)
1144 add_port(ctx, ctx->argv[1], ctx->argv[2], false, &ctx->argv[2], 1);
1148 cmd_add_bond(struct vsctl_context *ctx)
1150 bool fake_iface = shash_find(&ctx->options, "--fake-iface");
1152 add_port(ctx, ctx->argv[1], ctx->argv[2], fake_iface,
1153 &ctx->argv[3], ctx->argc - 3);
1157 cmd_del_port(struct vsctl_context *ctx)
1159 bool must_exist = !shash_find(&ctx->options, "--if-exists");
1160 struct vsctl_info info;
1162 get_info(ctx->ovs, &info);
1163 if (ctx->argc == 2) {
1164 struct vsctl_port *port = find_port(&info, ctx->argv[1], must_exist);
1166 del_port(&info, port);
1168 } else if (ctx->argc == 3) {
1169 struct vsctl_bridge *bridge = find_bridge(&info, ctx->argv[1], true);
1170 struct vsctl_port *port = find_port(&info, ctx->argv[2], must_exist);
1173 if (port->bridge == bridge) {
1174 del_port(&info, port);
1175 } else if (port->bridge->parent == bridge) {
1176 vsctl_fatal("bridge %s does not have a port %s (although its "
1177 "parent bridge %s does)",
1178 ctx->argv[1], ctx->argv[2], bridge->parent->name);
1180 vsctl_fatal("bridge %s does not have a port %s",
1181 ctx->argv[1], ctx->argv[2]);
1189 cmd_port_to_br(struct vsctl_context *ctx)
1191 struct vsctl_port *port;
1192 struct vsctl_info info;
1194 get_info(ctx->ovs, &info);
1195 port = find_port(&info, ctx->argv[1], true);
1196 ds_put_format(&ctx->output, "%s\n", port->bridge->name);
1201 cmd_br_to_vlan(struct vsctl_context *ctx)
1203 struct vsctl_bridge *bridge;
1204 struct vsctl_info info;
1206 get_info(ctx->ovs, &info);
1207 bridge = find_bridge(&info, ctx->argv[1], true);
1208 ds_put_format(&ctx->output, "%d\n", bridge->vlan);
1213 cmd_br_to_parent(struct vsctl_context *ctx)
1215 struct vsctl_bridge *bridge;
1216 struct vsctl_info info;
1218 get_info(ctx->ovs, &info);
1219 bridge = find_bridge(&info, ctx->argv[1], true);
1220 if (bridge->parent) {
1221 bridge = bridge->parent;
1223 ds_put_format(&ctx->output, "%s\n", bridge->name);
1228 cmd_list_ifaces(struct vsctl_context *ctx)
1230 struct vsctl_bridge *br;
1231 struct shash_node *node;
1232 struct vsctl_info info;
1235 get_info(ctx->ovs, &info);
1236 br = find_bridge(&info, ctx->argv[1], true);
1239 SHASH_FOR_EACH (node, &info.ifaces) {
1240 struct vsctl_iface *iface = node->data;
1242 if (strcmp(iface->iface_cfg->name, br->name)
1243 && br == iface->port->bridge) {
1244 svec_add(&ifaces, iface->iface_cfg->name);
1247 output_sorted(&ifaces, &ctx->output);
1248 svec_destroy(&ifaces);
1254 cmd_iface_to_br(struct vsctl_context *ctx)
1256 struct vsctl_iface *iface;
1257 struct vsctl_info info;
1259 get_info(ctx->ovs, &info);
1260 iface = find_iface(&info, ctx->argv[1], true);
1261 ds_put_format(&ctx->output, "%s\n", iface->port->bridge->name);
1266 cmd_get_controller(struct vsctl_context *ctx)
1268 struct vsctl_info info;
1270 get_info(ctx->ovs, &info);
1272 if (ctx->argc == 1) {
1273 /* Return the controller from the "Open_vSwitch" table */
1275 ds_put_format(&ctx->output, "%s\n", info.ctrl->target);
1278 /* Return the controller for a particular bridge. */
1279 struct vsctl_bridge *br = find_bridge(&info, ctx->argv[1], true);
1281 /* If no controller is explicitly defined for the requested
1282 * bridge, fallback to the "Open_vSwitch" table's controller. */
1284 ds_put_format(&ctx->output, "%s\n", br->ctrl->target);
1285 } else if (info.ctrl) {
1286 ds_put_format(&ctx->output, "%s\n", info.ctrl->target);
1294 cmd_del_controller(struct vsctl_context *ctx)
1296 struct vsctl_info info;
1298 get_info(ctx->ovs, &info);
1300 if (ctx->argc == 1) {
1302 ovsrec_controller_delete(info.ctrl);
1303 ovsrec_open_vswitch_set_controller(ctx->ovs, NULL);
1306 struct vsctl_bridge *br = find_real_bridge(&info, ctx->argv[1], true);
1309 ovsrec_controller_delete(br->ctrl);
1310 ovsrec_bridge_set_controller(br->br_cfg, NULL);
1318 cmd_set_controller(struct vsctl_context *ctx)
1320 struct vsctl_info info;
1321 struct ovsrec_controller *ctrl;
1323 get_info(ctx->ovs, &info);
1325 if (ctx->argc == 2) {
1326 /* Set the controller in the "Open_vSwitch" table. */
1328 ovsrec_controller_delete(info.ctrl);
1330 ctrl = ovsrec_controller_insert(ctx->txn);
1331 ovsrec_controller_set_target(ctrl, ctx->argv[1]);
1332 ovsrec_open_vswitch_set_controller(ctx->ovs, ctrl);
1334 /* Set the controller for a particular bridge. */
1335 struct vsctl_bridge *br = find_real_bridge(&info, ctx->argv[1], true);
1338 ovsrec_controller_delete(br->ctrl);
1340 ctrl = ovsrec_controller_insert(ctx->txn);
1341 ovsrec_controller_set_target(ctrl, ctx->argv[2]);
1342 ovsrec_bridge_set_controller(br->br_cfg, ctrl);
1349 cmd_get_fail_mode(struct vsctl_context *ctx)
1351 struct vsctl_info info;
1352 const char *fail_mode = NULL;
1354 get_info(ctx->ovs, &info);
1356 if (ctx->argc == 1) {
1357 /* Return the fail-mode from the "Open_vSwitch" table */
1358 if (info.ctrl && info.ctrl->fail_mode) {
1359 fail_mode = info.ctrl->fail_mode;
1362 /* Return the fail-mode for a particular bridge. */
1363 struct vsctl_bridge *br = find_bridge(&info, ctx->argv[1], true);
1365 /* If no controller or fail-mode is explicitly defined for the
1366 * requested bridge, fallback to the "Open_vSwitch" table's
1368 if (br->ctrl && br->ctrl->fail_mode) {
1369 fail_mode = br->ctrl->fail_mode;
1370 } else if (info.ctrl && info.ctrl->fail_mode) {
1371 fail_mode = info.ctrl->fail_mode;
1375 if (fail_mode && strlen(fail_mode)) {
1376 ds_put_format(&ctx->output, "%s\n", fail_mode);
1383 cmd_del_fail_mode(struct vsctl_context *ctx)
1385 struct vsctl_info info;
1387 get_info(ctx->ovs, &info);
1389 if (ctx->argc == 1) {
1390 if (info.ctrl && info.ctrl->fail_mode) {
1391 ovsrec_controller_set_fail_mode(info.ctrl, NULL);
1394 struct vsctl_bridge *br = find_real_bridge(&info, ctx->argv[1], true);
1396 if (br->ctrl && br->ctrl->fail_mode) {
1397 ovsrec_controller_set_fail_mode(br->ctrl, NULL);
1405 cmd_set_fail_mode(struct vsctl_context *ctx)
1407 struct vsctl_info info;
1408 const char *fail_mode;
1410 get_info(ctx->ovs, &info);
1412 fail_mode = (ctx->argc == 2) ? ctx->argv[1] : ctx->argv[2];
1414 if (strcmp(fail_mode, "standalone") && strcmp(fail_mode, "secure")) {
1415 vsctl_fatal("fail-mode must be \"standalone\" or \"secure\"");
1418 if (ctx->argc == 2) {
1419 /* Set the fail-mode in the "Open_vSwitch" table. */
1421 vsctl_fatal("no controller declared");
1423 ovsrec_controller_set_fail_mode(info.ctrl, fail_mode);
1425 struct vsctl_bridge *br = find_real_bridge(&info, ctx->argv[1], true);
1428 vsctl_fatal("no controller declared for %s", br->name);
1430 ovsrec_controller_set_fail_mode(br->ctrl, fail_mode);
1437 cmd_get_ssl(struct vsctl_context *ctx)
1439 struct ovsrec_ssl *ssl = ctx->ovs->ssl;
1442 ds_put_format(&ctx->output, "Private key: %s\n", ssl->private_key);
1443 ds_put_format(&ctx->output, "Certificate: %s\n", ssl->certificate);
1444 ds_put_format(&ctx->output, "CA Certificate: %s\n", ssl->ca_cert);
1445 ds_put_format(&ctx->output, "Bootstrap: %s\n",
1446 ssl->bootstrap_ca_cert ? "true" : "false");
1451 cmd_del_ssl(struct vsctl_context *ctx)
1453 struct ovsrec_ssl *ssl = ctx->ovs->ssl;
1456 ovsrec_ssl_delete(ssl);
1457 ovsrec_open_vswitch_set_ssl(ctx->ovs, NULL);
1462 cmd_set_ssl(struct vsctl_context *ctx)
1464 bool bootstrap = shash_find(&ctx->options, "--bootstrap");
1465 struct ovsrec_ssl *ssl = ctx->ovs->ssl;
1468 ovsrec_ssl_delete(ssl);
1470 ssl = ovsrec_ssl_insert(ctx->txn);
1472 ovsrec_ssl_set_private_key(ssl, ctx->argv[1]);
1473 ovsrec_ssl_set_certificate(ssl, ctx->argv[2]);
1474 ovsrec_ssl_set_ca_cert(ssl, ctx->argv[3]);
1476 ovsrec_ssl_set_bootstrap_ca_cert(ssl, bootstrap);
1478 ovsrec_open_vswitch_set_ssl(ctx->ovs, ssl);
1481 /* Parameter commands. */
1483 struct vsctl_row_id {
1484 const struct ovsdb_idl_table_class *table;
1485 const struct ovsdb_idl_column *name_column;
1486 const struct ovsdb_idl_column *uuid_column;
1489 struct vsctl_table_class {
1490 struct ovsdb_idl_table_class *class;
1491 struct vsctl_row_id row_ids[2];
1494 static const struct vsctl_table_class tables[] = {
1495 {&ovsrec_table_bridge,
1496 {{&ovsrec_table_bridge, &ovsrec_bridge_col_name, NULL},
1497 {NULL, NULL, NULL}}},
1499 {&ovsrec_table_controller,
1500 {{&ovsrec_table_bridge,
1501 &ovsrec_bridge_col_name,
1502 &ovsrec_bridge_col_controller},
1503 {&ovsrec_table_open_vswitch,
1505 &ovsrec_open_vswitch_col_controller}}},
1507 {&ovsrec_table_interface,
1508 {{&ovsrec_table_interface, &ovsrec_interface_col_name, NULL},
1509 {NULL, NULL, NULL}}},
1511 {&ovsrec_table_mirror,
1512 {{&ovsrec_table_mirror, &ovsrec_mirror_col_name, NULL},
1513 {NULL, NULL, NULL}}},
1515 {&ovsrec_table_netflow,
1516 {{&ovsrec_table_bridge,
1517 &ovsrec_bridge_col_name,
1518 &ovsrec_bridge_col_netflow},
1519 {NULL, NULL, NULL}}},
1521 {&ovsrec_table_open_vswitch,
1522 {{&ovsrec_table_open_vswitch, NULL, NULL},
1523 {NULL, NULL, NULL}}},
1525 {&ovsrec_table_port,
1526 {{&ovsrec_table_port, &ovsrec_port_col_name, NULL},
1527 {NULL, NULL, NULL}}},
1530 {{&ovsrec_table_open_vswitch, NULL, &ovsrec_open_vswitch_col_ssl}}},
1532 {NULL, {{NULL, NULL, NULL}, {NULL, NULL, NULL}}}
1536 die_if_error(char *error)
1539 vsctl_fatal("%s", error);
1544 to_lower_and_underscores(unsigned c)
1546 return c == '-' ? '_' : tolower(c);
1550 score_partial_match(const char *name, const char *s)
1554 if (!strcmp(name, s)) {
1557 for (score = 0; ; score++, name++, s++) {
1558 if (to_lower_and_underscores(*name) != to_lower_and_underscores(*s)) {
1560 } else if (*name == '\0') {
1561 return UINT_MAX - 1;
1564 return *s == '\0' ? score : 0;
1567 static const struct vsctl_table_class *
1568 get_table(const char *table_name)
1570 const struct vsctl_table_class *table;
1571 const struct vsctl_table_class *best_match = NULL;
1572 unsigned int best_score = 0;
1574 for (table = tables; table->class; table++) {
1575 unsigned int score = score_partial_match(table->class->name,
1577 if (score > best_score) {
1580 } else if (score == best_score) {
1586 } else if (best_score) {
1587 vsctl_fatal("multiple table names match \"%s\"", table_name);
1589 vsctl_fatal("unknown table \"%s\"", table_name);
1593 static const struct ovsdb_idl_row *
1594 get_row_by_id(struct vsctl_context *ctx, const struct vsctl_table_class *table,
1595 const struct vsctl_row_id *id, const char *record_id)
1597 const struct ovsdb_idl_row *referrer, *final;
1603 if (!id->name_column) {
1604 if (strcmp(record_id, ".")) {
1607 referrer = ovsdb_idl_first_row(ctx->idl, id->table);
1608 if (!referrer || ovsdb_idl_next_row(referrer)) {
1612 const struct ovsdb_idl_row *row;
1613 unsigned int best_score = 0;
1615 /* It might make sense to relax this assertion. */
1616 assert(id->name_column->type.key.type == OVSDB_TYPE_STRING);
1619 for (row = ovsdb_idl_first_row(ctx->idl, id->table);
1620 row != NULL && best_score != UINT_MAX;
1621 row = ovsdb_idl_next_row(row))
1623 struct ovsdb_datum name;
1625 ovsdb_idl_txn_read(row, id->name_column, &name);
1627 unsigned int score = score_partial_match(name.keys[0].string,
1629 if (score > best_score) {
1632 } else if (score == best_score) {
1636 ovsdb_datum_destroy(&name, &id->name_column->type);
1638 if (best_score && !referrer) {
1639 vsctl_fatal("multiple rows in %s match \"%s\"",
1640 table->class->name, record_id);
1648 if (id->uuid_column) {
1649 struct ovsdb_datum uuid;
1651 assert(id->uuid_column->type.key.type == OVSDB_TYPE_UUID);
1652 assert(id->uuid_column->type.value.type == OVSDB_TYPE_VOID);
1654 ovsdb_idl_txn_read(referrer, id->uuid_column, &uuid);
1656 final = ovsdb_idl_get_row_for_uuid(ctx->idl, table->class,
1657 &uuid.keys[0].uuid);
1659 ovsdb_datum_destroy(&uuid, &id->uuid_column->type);
1667 static const struct ovsdb_idl_row *
1668 get_row(struct vsctl_context *ctx,
1669 const struct vsctl_table_class *table, const char *record_id)
1671 const struct ovsdb_idl_row *row;
1674 if (uuid_from_string(&uuid, record_id)) {
1675 row = ovsdb_idl_get_row_for_uuid(ctx->idl, table->class, &uuid);
1679 for (i = 0; i < ARRAY_SIZE(table->row_ids); i++) {
1680 row = get_row_by_id(ctx, table, &table->row_ids[i], record_id);
1689 static const struct ovsdb_idl_row *
1690 must_get_row(struct vsctl_context *ctx,
1691 const struct vsctl_table_class *table, const char *record_id)
1693 const struct ovsdb_idl_row *row = get_row(ctx, table, record_id);
1695 vsctl_fatal("no row \"%s\" in table %s",
1696 record_id, table->class->name);
1702 get_column(const struct vsctl_table_class *table, const char *column_name,
1703 const struct ovsdb_idl_column **columnp)
1705 const struct ovsdb_idl_column *best_match = NULL;
1706 unsigned int best_score = 0;
1709 for (i = 0; i < table->class->n_columns; i++) {
1710 const struct ovsdb_idl_column *column = &table->class->columns[i];
1711 unsigned int score = score_partial_match(column->name, column_name);
1712 if (score > best_score) {
1713 best_match = column;
1715 } else if (score == best_score) {
1720 *columnp = best_match;
1723 } else if (best_score) {
1724 return xasprintf("%s contains more than one column whose name "
1725 "matches \"%s\"", table->class->name, column_name);
1727 return xasprintf("%s does not contain a column whose name matches "
1728 "\"%s\"", table->class->name, column_name);
1732 static char * WARN_UNUSED_RESULT
1733 parse_column_key_value(const char *arg, const struct vsctl_table_class *table,
1734 const struct ovsdb_idl_column **columnp,
1735 char **keyp, char **valuep)
1737 const char *p = arg;
1740 assert(columnp || keyp);
1748 /* Parse column name. */
1752 error = ovsdb_token_parse(&p, &column_name);
1756 if (column_name[0] == '\0') {
1758 error = xasprintf("%s: missing column name", arg);
1761 error = get_column(table, column_name, columnp);
1768 /* Parse key string. */
1769 if (*p == ':' || !columnp) {
1773 error = xasprintf("%s: key not accepted here", arg);
1776 error = ovsdb_token_parse(&p, keyp);
1784 /* Parse value string. */
1787 error = xasprintf("%s: value not accepted here", arg);
1790 *valuep = xstrdup(p + 1);
1796 error = xasprintf("%s: trailing garbage \"%s\" in argument",
1819 cmd_get(struct vsctl_context *ctx)
1821 bool if_exists = shash_find(&ctx->options, "--if-exists");
1822 const char *table_name = ctx->argv[1];
1823 const char *record_id = ctx->argv[2];
1824 const struct vsctl_table_class *table;
1825 const struct ovsdb_idl_row *row;
1826 struct ds *out = &ctx->output;
1829 table = get_table(table_name);
1830 row = must_get_row(ctx, table, record_id);
1831 for (i = 3; i < ctx->argc; i++) {
1832 const struct ovsdb_idl_column *column;
1833 struct ovsdb_datum datum;
1836 die_if_error(parse_column_key_value(ctx->argv[i], table,
1837 &column, &key_string, NULL));
1839 ovsdb_idl_txn_read(row, column, &datum);
1841 union ovsdb_atom key;
1844 if (column->type.value.type == OVSDB_TYPE_VOID) {
1845 vsctl_fatal("cannot specify key to get for non-map column %s",
1849 die_if_error(ovsdb_atom_from_string(&key,
1853 idx = ovsdb_datum_find_key(&datum, &key,
1854 column->type.key.type);
1855 if (idx == UINT_MAX) {
1857 vsctl_fatal("no key \"%s\" in %s record \"%s\" column %s",
1858 key_string, table->class->name, record_id,
1862 ovsdb_atom_to_string(&datum.values[idx],
1863 column->type.value.type, out);
1865 ovsdb_atom_destroy(&key, column->type.key.type);
1867 ovsdb_datum_to_string(&datum, &column->type, out);
1869 ds_put_char(out, '\n');
1870 ovsdb_datum_destroy(&datum, &column->type);
1877 list_record(const struct vsctl_table_class *table,
1878 const struct ovsdb_idl_row *row, struct ds *out)
1882 ds_put_format(out, "%-20s: "UUID_FMT"\n", "_uuid",
1883 UUID_ARGS(&row->uuid));
1884 for (i = 0; i < table->class->n_columns; i++) {
1885 const struct ovsdb_idl_column *column = &table->class->columns[i];
1886 struct ovsdb_datum datum;
1888 ovsdb_idl_txn_read(row, column, &datum);
1890 ds_put_format(out, "%-20s: ", column->name);
1891 ovsdb_datum_to_string(&datum, &column->type, out);
1892 ds_put_char(out, '\n');
1894 ovsdb_datum_destroy(&datum, &column->type);
1899 cmd_list(struct vsctl_context *ctx)
1901 const char *table_name = ctx->argv[1];
1902 const struct vsctl_table_class *table;
1903 struct ds *out = &ctx->output;
1906 table = get_table(table_name);
1907 if (ctx->argc > 2) {
1908 for (i = 2; i < ctx->argc; i++) {
1910 ds_put_char(out, '\n');
1912 list_record(table, must_get_row(ctx, table, ctx->argv[i]), out);
1915 const struct ovsdb_idl_row *row;
1918 for (row = ovsdb_idl_first_row(ctx->idl, table->class), first = true;
1920 row = ovsdb_idl_next_row(row), first = false) {
1922 ds_put_char(out, '\n');
1924 list_record(table, row, out);
1930 set_column(const struct vsctl_table_class *table,
1931 const struct ovsdb_idl_row *row, const char *arg)
1933 const struct ovsdb_idl_column *column;
1934 char *key_string, *value_string;
1937 error = parse_column_key_value(arg, table, &column, &key_string,
1939 die_if_error(error);
1940 if (!value_string) {
1941 vsctl_fatal("%s: missing value", arg);
1945 union ovsdb_atom key, value;
1946 struct ovsdb_datum old, new;
1948 if (column->type.value.type == OVSDB_TYPE_VOID) {
1949 vsctl_fatal("cannot specify key to set for non-map column %s",
1953 die_if_error(ovsdb_atom_from_string(&key, &column->type.key,
1955 die_if_error(ovsdb_atom_from_string(&value, &column->type.value,
1958 ovsdb_datum_init_empty(&new);
1959 ovsdb_datum_add_unsafe(&new, &key, &value, &column->type);
1961 ovsdb_atom_destroy(&key, column->type.key.type);
1962 ovsdb_atom_destroy(&value, column->type.value.type);
1964 ovsdb_idl_txn_read(row, column, &old);
1965 ovsdb_datum_union(&old, &new, &column->type, true);
1966 ovsdb_idl_txn_write(row, column, &old);
1968 ovsdb_datum_destroy(&new, &column->type);
1970 struct ovsdb_datum datum;
1972 die_if_error(ovsdb_datum_from_string(&datum, &column->type,
1974 ovsdb_idl_txn_write(row, column, &datum);
1982 cmd_set(struct vsctl_context *ctx)
1984 const char *table_name = ctx->argv[1];
1985 const char *record_id = ctx->argv[2];
1986 const struct vsctl_table_class *table;
1987 const struct ovsdb_idl_row *row;
1990 table = get_table(table_name);
1991 row = must_get_row(ctx, table, record_id);
1992 for (i = 3; i < ctx->argc; i++) {
1993 set_column(table, row, ctx->argv[i]);
1998 cmd_add(struct vsctl_context *ctx)
2000 const char *table_name = ctx->argv[1];
2001 const char *record_id = ctx->argv[2];
2002 const char *column_name = ctx->argv[3];
2003 const struct vsctl_table_class *table;
2004 const struct ovsdb_idl_column *column;
2005 const struct ovsdb_idl_row *row;
2006 const struct ovsdb_type *type;
2007 struct ovsdb_datum old;
2010 table = get_table(table_name);
2011 row = must_get_row(ctx, table, record_id);
2012 die_if_error(get_column(table, column_name, &column));
2014 type = &column->type;
2015 ovsdb_idl_txn_read(row, column, &old);
2016 for (i = 4; i < ctx->argc; i++) {
2017 struct ovsdb_type add_type;
2018 struct ovsdb_datum add;
2022 add_type.n_max = UINT_MAX;
2023 die_if_error(ovsdb_datum_from_string(&add, &add_type, ctx->argv[i]));
2024 ovsdb_datum_union(&old, &add, type, false);
2025 ovsdb_datum_destroy(&add, type);
2027 if (old.n > type->n_max) {
2028 vsctl_fatal("\"add\" operation would put %u %s in column %s of "
2029 "table %s but the maximum number is %u",
2031 type->value.type == OVSDB_TYPE_VOID ? "values" : "pairs",
2032 column->name, table->class->name, type->n_max);
2034 ovsdb_idl_txn_write(row, column, &old);
2038 cmd_remove(struct vsctl_context *ctx)
2040 const char *table_name = ctx->argv[1];
2041 const char *record_id = ctx->argv[2];
2042 const char *column_name = ctx->argv[3];
2043 const struct vsctl_table_class *table;
2044 const struct ovsdb_idl_column *column;
2045 const struct ovsdb_idl_row *row;
2046 const struct ovsdb_type *type;
2047 struct ovsdb_datum old;
2050 table = get_table(table_name);
2051 row = must_get_row(ctx, table, record_id);
2052 die_if_error(get_column(table, column_name, &column));
2054 type = &column->type;
2055 ovsdb_idl_txn_read(row, column, &old);
2056 for (i = 4; i < ctx->argc; i++) {
2057 struct ovsdb_type rm_type;
2058 struct ovsdb_datum rm;
2063 rm_type.n_max = UINT_MAX;
2064 error = ovsdb_datum_from_string(&rm, &rm_type, ctx->argv[i]);
2065 if (error && ovsdb_type_is_map(&rm_type)) {
2067 rm_type.value.type = OVSDB_TYPE_VOID;
2068 die_if_error(ovsdb_datum_from_string(&rm, &rm_type, ctx->argv[i]));
2070 ovsdb_datum_subtract(&old, type, &rm, &rm_type);
2071 ovsdb_datum_destroy(&rm, &rm_type);
2073 if (old.n < type->n_min) {
2074 vsctl_fatal("\"remove\" operation would put %u %s in column %s of "
2075 "table %s but the minimun number is %u",
2077 type->value.type == OVSDB_TYPE_VOID ? "values" : "pairs",
2078 column->name, table->class->name, type->n_min);
2080 ovsdb_idl_txn_write(row, column, &old);
2084 cmd_clear(struct vsctl_context *ctx)
2086 const char *table_name = ctx->argv[1];
2087 const char *record_id = ctx->argv[2];
2088 const struct vsctl_table_class *table;
2089 const struct ovsdb_idl_row *row;
2092 table = get_table(table_name);
2093 row = must_get_row(ctx, table, record_id);
2094 for (i = 3; i < ctx->argc; i++) {
2095 const struct ovsdb_idl_column *column;
2096 const struct ovsdb_type *type;
2097 struct ovsdb_datum datum;
2099 die_if_error(get_column(table, ctx->argv[i], &column));
2101 type = &column->type;
2102 if (type->n_min > 0) {
2103 vsctl_fatal("\"clear\" operation cannot be applied to column %s "
2104 "of table %s, which is not allowed to be empty",
2105 column->name, table->class->name);
2108 ovsdb_datum_init_empty(&datum);
2109 ovsdb_idl_txn_write(row, column, &datum);
2114 cmd_create(struct vsctl_context *ctx)
2116 bool force = shash_find(&ctx->options, "--force");
2117 const char *table_name = ctx->argv[1];
2118 const struct vsctl_table_class *table;
2119 const struct ovsdb_idl_row *row;
2123 vsctl_fatal("\"create\" requires --force");
2126 table = get_table(table_name);
2127 row = ovsdb_idl_txn_insert(ctx->txn, table->class);
2128 for (i = 2; i < ctx->argc; i++) {
2129 set_column(table, row, ctx->argv[i]);
2131 ds_put_format(&ctx->output, UUID_FMT, UUID_ARGS(&row->uuid));
2134 /* This function may be used as the 'postprocess' function for commands that
2135 * insert new rows into the database. It expects that the command's 'run'
2136 * function prints the UUID reported by ovsdb_idl_txn_insert() as the command's
2137 * sole output. It replaces that output by the row's permanent UUID assigned
2138 * by the database server and appends a new-line.
2140 * Currently we use this only for "create", because the higher-level commands
2141 * are supposed to be independent of the actual structure of the vswitch
2144 post_create(struct vsctl_context *ctx)
2146 const struct uuid *real;
2149 uuid_from_string(&dummy, ds_cstr(&ctx->output));
2150 real = ovsdb_idl_txn_get_insert_uuid(ctx->txn, &dummy);
2152 ds_clear(&ctx->output);
2153 ds_put_format(&ctx->output, UUID_FMT, UUID_ARGS(real));
2155 ds_put_char(&ctx->output, '\n');
2159 cmd_destroy(struct vsctl_context *ctx)
2161 bool force = shash_find(&ctx->options, "--force");
2162 bool must_exist = !shash_find(&ctx->options, "--if-exists");
2163 const char *table_name = ctx->argv[1];
2164 const struct vsctl_table_class *table;
2168 vsctl_fatal("\"destroy\" requires --force");
2171 table = get_table(table_name);
2172 for (i = 2; i < ctx->argc; i++) {
2173 const struct ovsdb_idl_row *row;
2175 row = (must_exist ? must_get_row : get_row)(ctx, table, ctx->argv[i]);
2177 ovsdb_idl_txn_delete(row);
2182 static struct json *
2183 where_uuid_equals(const struct uuid *uuid)
2186 json_array_create_1(
2187 json_array_create_3(
2188 json_string_create("_uuid"),
2189 json_string_create("=="),
2190 json_array_create_2(
2191 json_string_create("uuid"),
2192 json_string_create_nocopy(
2193 xasprintf(UUID_FMT, UUID_ARGS(uuid))))));
2197 vsctl_context_init(struct vsctl_context *ctx, struct vsctl_command *command,
2198 struct ovsdb_idl *idl, struct ovsdb_idl_txn *txn,
2199 const struct ovsrec_open_vswitch *ovs)
2201 ctx->argc = command->argc;
2202 ctx->argv = command->argv;
2203 ctx->options = command->options;
2205 ds_swap(&ctx->output, &command->output);
2213 vsctl_context_done(struct vsctl_context *ctx, struct vsctl_command *command)
2215 ds_swap(&ctx->output, &command->output);
2219 do_vsctl(const char *args, struct vsctl_command *commands, size_t n_commands,
2220 struct ovsdb_idl *idl)
2222 struct ovsdb_idl_txn *txn;
2223 const struct ovsrec_open_vswitch *ovs;
2224 enum ovsdb_idl_txn_status status;
2225 struct vsctl_command *c;
2226 int64_t next_cfg = 0;
2230 txn = the_idl_txn = ovsdb_idl_txn_create(idl);
2232 ovsdb_idl_txn_set_dry_run(txn);
2235 comment = xasprintf("ovs-vsctl: %s", args);
2236 ovsdb_idl_txn_add_comment(txn, comment);
2239 ovs = ovsrec_open_vswitch_first(idl);
2241 /* XXX add verification that table is empty */
2242 ovs = ovsrec_open_vswitch_insert(txn);
2245 if (wait_for_reload) {
2246 struct json *where = where_uuid_equals(&ovs->header_.uuid);
2247 ovsdb_idl_txn_increment(txn, "Open_vSwitch", "next_cfg", where);
2248 json_destroy(where);
2251 for (c = commands; c < &commands[n_commands]; c++) {
2252 struct vsctl_context ctx;
2254 ds_init(&c->output);
2255 vsctl_context_init(&ctx, c, idl, txn, ovs);
2256 (c->syntax->run)(&ctx);
2257 vsctl_context_done(&ctx, c);
2260 while ((status = ovsdb_idl_txn_commit(txn)) == TXN_INCOMPLETE) {
2262 ovsdb_idl_wait(idl);
2263 ovsdb_idl_txn_wait(txn);
2266 if (wait_for_reload && status == TXN_SUCCESS) {
2267 next_cfg = ovsdb_idl_txn_get_increment_new_value(txn);
2269 for (c = commands; c < &commands[n_commands]; c++) {
2270 if (c->syntax->postprocess) {
2271 struct vsctl_context ctx;
2273 vsctl_context_init(&ctx, c, idl, txn, ovs);
2274 (c->syntax->postprocess)(&ctx);
2275 vsctl_context_done(&ctx, c);
2278 error = xstrdup(ovsdb_idl_txn_get_error(txn));
2279 ovsdb_idl_txn_destroy(txn);
2283 case TXN_INCOMPLETE:
2287 /* Should not happen--we never call ovsdb_idl_txn_abort(). */
2288 vsctl_fatal("transaction aborted");
2295 for (c = commands; c < &commands[n_commands]; c++) {
2296 ds_destroy(&c->output);
2302 vsctl_fatal("transaction error: %s", error);
2309 for (c = commands; c < &commands[n_commands]; c++) {
2310 struct ds *ds = &c->output;
2315 for (j = 0; j < ds->length; j++) {
2316 int c = ds->string[j];
2319 fputs("\\n", stdout);
2323 fputs("\\\\", stdout);
2332 fputs(ds_cstr(ds), stdout);
2334 ds_destroy(&c->output);
2335 shash_destroy(&c->options);
2339 if (wait_for_reload && status != TXN_UNCHANGED) {
2341 const struct ovsrec_open_vswitch *ovs;
2344 OVSREC_OPEN_VSWITCH_FOR_EACH (ovs, idl) {
2345 if (ovs->cur_cfg >= next_cfg) {
2349 ovsdb_idl_wait(idl);
2354 ovsdb_idl_destroy(idl);
2359 static const struct vsctl_command_syntax all_commands[] = {
2360 /* Open vSwitch commands. */
2361 {"init", 0, 0, cmd_init, NULL, ""},
2363 /* Bridge commands. */
2364 {"add-br", 1, 3, cmd_add_br, NULL, ""},
2365 {"del-br", 1, 1, cmd_del_br, NULL, "--if-exists"},
2366 {"list-br", 0, 0, cmd_list_br, NULL, ""},
2367 {"br-exists", 1, 1, cmd_br_exists, NULL, ""},
2368 {"br-to-vlan", 1, 1, cmd_br_to_vlan, NULL, ""},
2369 {"br-to-parent", 1, 1, cmd_br_to_parent, NULL, ""},
2370 {"br-set-external-id", 2, 3, cmd_br_set_external_id, NULL, ""},
2371 {"br-get-external-id", 1, 2, cmd_br_get_external_id, NULL, ""},
2373 /* Port commands. */
2374 {"list-ports", 1, 1, cmd_list_ports, NULL, ""},
2375 {"add-port", 2, 2, cmd_add_port, NULL, ""},
2376 {"add-bond", 4, INT_MAX, cmd_add_bond, NULL, "--fake-iface"},
2377 {"del-port", 1, 2, cmd_del_port, NULL, "--if-exists"},
2378 {"port-to-br", 1, 1, cmd_port_to_br, NULL, ""},
2380 /* Interface commands. */
2381 {"list-ifaces", 1, 1, cmd_list_ifaces, NULL, ""},
2382 {"iface-to-br", 1, 1, cmd_iface_to_br, NULL, ""},
2384 /* Controller commands. */
2385 {"get-controller", 0, 1, cmd_get_controller, NULL, ""},
2386 {"del-controller", 0, 1, cmd_del_controller, NULL, ""},
2387 {"set-controller", 1, 2, cmd_set_controller, NULL, ""},
2388 {"get-fail-mode", 0, 1, cmd_get_fail_mode, NULL, ""},
2389 {"del-fail-mode", 0, 1, cmd_del_fail_mode, NULL, ""},
2390 {"set-fail-mode", 1, 2, cmd_set_fail_mode, NULL, ""},
2393 {"get-ssl", 0, 0, cmd_get_ssl, NULL, ""},
2394 {"del-ssl", 0, 0, cmd_del_ssl, NULL, ""},
2395 {"set-ssl", 3, 3, cmd_set_ssl, NULL, "--bootstrap"},
2397 /* Parameter commands. */
2398 {"get", 3, INT_MAX, cmd_get, NULL, "--if-exists"},
2399 {"list", 1, INT_MAX, cmd_list, NULL, ""},
2400 {"create", 2, INT_MAX, cmd_create, post_create, "--force"},
2401 {"destroy", 1, INT_MAX, cmd_destroy, NULL, "--force,--if-exists"},
2402 {"set", 3, INT_MAX, cmd_set, NULL, ""},
2403 {"add", 4, INT_MAX, cmd_add, NULL, ""},
2404 {"remove", 4, INT_MAX, cmd_remove, NULL, ""},
2405 {"clear", 3, INT_MAX, cmd_clear, NULL, ""},
2407 {NULL, 0, 0, NULL, NULL, NULL},