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"
39 #include "stream-ssl.h"
41 #include "vswitchd/vswitch-idl.h"
46 VLOG_DEFINE_THIS_MODULE(vsctl)
48 /* vsctl_fatal() also logs the error, so it is preferred in this file. */
49 #define ovs_fatal please_use_vsctl_fatal_instead_of_ovs_fatal
53 typedef void vsctl_handler_func(struct vsctl_context *);
55 struct vsctl_command_syntax {
59 vsctl_handler_func *run;
60 vsctl_handler_func *postprocess;
64 struct vsctl_command {
65 /* Data that remains constant after initialization. */
66 const struct vsctl_command_syntax *syntax;
71 /* Data modified by commands. */
75 /* --db: The database server to contact. */
76 static const char *db;
78 /* --oneline: Write each command's output as a single line? */
81 /* --dry-run: Do not commit any changes. */
84 /* --no-wait: Wait for ovs-vswitchd to reload its configuration? */
85 static bool wait_for_reload = true;
87 /* --timeout: Time to wait for a connection to 'db'. */
90 /* All supported commands. */
91 static const struct vsctl_command_syntax all_commands[];
93 /* The IDL we're using and the current transaction, if any.
94 * This is for use by vsctl_exit() only, to allow it to clean up.
95 * Other code should use its context arguments. */
96 static struct ovsdb_idl *the_idl;
97 static struct ovsdb_idl_txn *the_idl_txn;
99 static void vsctl_exit(int status) NO_RETURN;
100 static void vsctl_fatal(const char *, ...) PRINTF_FORMAT(1, 2) NO_RETURN;
101 static char *default_db(void);
102 static void usage(void) NO_RETURN;
103 static void parse_options(int argc, char *argv[]);
105 static struct vsctl_command *parse_commands(int argc, char *argv[],
106 size_t *n_commandsp);
107 static void parse_command(int argc, char *argv[], struct vsctl_command *);
108 static void do_vsctl(const char *args,
109 struct vsctl_command *, size_t n_commands,
112 static const struct vsctl_table_class *get_table(const char *table_name);
113 static void set_column(const struct vsctl_table_class *,
114 const struct ovsdb_idl_row *, const char *arg,
115 struct ovsdb_symbol_table *);
119 main(int argc, char *argv[])
121 extern struct vlog_module VLM_reconnect;
122 struct ovsdb_idl *idl;
123 struct vsctl_command *commands;
127 set_program_name(argv[0]);
128 signal(SIGPIPE, SIG_IGN);
129 vlog_set_levels(NULL, VLF_CONSOLE, VLL_WARN);
130 vlog_set_levels(&VLM_reconnect, VLF_ANY_FACILITY, VLL_WARN);
133 /* Log our arguments. This is often valuable for debugging systems. */
134 args = process_escape_args(argv);
135 VLOG_INFO("Called as %s", args);
137 /* Parse command line. */
138 parse_options(argc, argv);
139 commands = parse_commands(argc - optind, argv + optind, &n_commands);
145 /* Now execute the commands. */
146 idl = the_idl = ovsdb_idl_create(db, &ovsrec_idl_class);
148 if (ovsdb_idl_run(idl)) {
149 do_vsctl(args, commands, n_commands, idl);
158 parse_options(int argc, char *argv[])
161 OPT_DB = UCHAR_MAX + 1,
169 static struct option long_options[] = {
170 {"db", required_argument, 0, OPT_DB},
171 {"no-syslog", no_argument, 0, OPT_NO_SYSLOG},
172 {"no-wait", no_argument, 0, OPT_NO_WAIT},
173 {"dry-run", no_argument, 0, OPT_DRY_RUN},
174 {"oneline", no_argument, 0, OPT_ONELINE},
175 {"timeout", required_argument, 0, 't'},
176 {"help", no_argument, 0, 'h'},
177 {"version", no_argument, 0, 'V'},
180 STREAM_SSL_LONG_OPTIONS
181 {"peer-ca-cert", required_argument, 0, OPT_PEER_CA_CERT},
185 char *tmp, *short_options;
187 tmp = long_options_to_short_options(long_options);
188 short_options = xasprintf("+%s", tmp);
194 c = getopt_long(argc, argv, short_options, long_options, NULL);
209 vlog_set_levels(&VLM_vsctl, VLF_SYSLOG, VLL_WARN);
213 wait_for_reload = false;
224 OVS_PRINT_VERSION(0, 0);
228 timeout = strtoul(optarg, NULL, 10);
230 vsctl_fatal("value %s on -t or --timeout is invalid",
238 STREAM_SSL_OPTION_HANDLERS
240 case OPT_PEER_CA_CERT:
241 stream_ssl_set_peer_ca_cert_file(optarg);
259 static struct vsctl_command *
260 parse_commands(int argc, char *argv[], size_t *n_commandsp)
262 struct vsctl_command *commands;
263 size_t n_commands, allocated_commands;
267 n_commands = allocated_commands = 0;
269 for (start = i = 0; i <= argc; i++) {
270 if (i == argc || !strcmp(argv[i], "--")) {
272 if (n_commands >= allocated_commands) {
273 struct vsctl_command *c;
275 commands = x2nrealloc(commands, &allocated_commands,
277 for (c = commands; c < &commands[n_commands]; c++) {
278 shash_moved(&c->options);
281 parse_command(i - start, &argv[start],
282 &commands[n_commands++]);
288 vsctl_fatal("missing command name (use --help for help)");
290 *n_commandsp = n_commands;
295 parse_command(int argc, char *argv[], struct vsctl_command *command)
297 const struct vsctl_command_syntax *p;
300 shash_init(&command->options);
301 for (i = 0; i < argc; i++) {
302 const char *option = argv[i];
306 if (option[0] != '-') {
310 equals = strchr(option, '=');
312 key = xmemdup0(option, equals - option);
313 value = xstrdup(equals + 1);
315 key = xstrdup(option);
319 if (shash_find(&command->options, key)) {
320 vsctl_fatal("'%s' option specified multiple times", argv[i]);
322 shash_add_nocopy(&command->options, key, value);
325 vsctl_fatal("missing command name");
328 for (p = all_commands; p->name; p++) {
329 if (!strcmp(p->name, argv[i])) {
330 struct shash_node *node;
333 SHASH_FOR_EACH (node, &command->options) {
334 const char *s = strstr(p->options, node->name);
335 int end = s ? s[strlen(node->name)] : EOF;
337 if (end != '=' && end != ',' && end != ' ' && end != '\0') {
338 vsctl_fatal("'%s' command has no '%s' option",
339 argv[i], node->name);
341 if ((end == '=') != (node->data != NULL)) {
343 vsctl_fatal("missing argument to '%s' option on '%s' "
344 "command", node->name, argv[i]);
346 vsctl_fatal("'%s' option on '%s' does not accept an "
347 "argument", node->name, argv[i]);
352 n_arg = argc - i - 1;
353 if (n_arg < p->min_args) {
354 vsctl_fatal("'%s' command requires at least %d arguments",
355 p->name, p->min_args);
356 } else if (n_arg > p->max_args) {
359 for (j = i + 1; j < argc; j++) {
360 if (argv[j][0] == '-') {
361 vsctl_fatal("'%s' command takes at most %d arguments "
362 "(note that options must precede command "
363 "names and follow a \"--\" argument)",
364 p->name, p->max_args);
368 vsctl_fatal("'%s' command takes at most %d arguments",
369 p->name, p->max_args);
372 command->argc = n_arg + 1;
373 command->argv = &argv[i];
379 vsctl_fatal("unknown command '%s'; use --help for help", argv[i]);
383 vsctl_fatal(const char *format, ...)
388 va_start(args, format);
389 message = xvasprintf(format, args);
392 vlog_set_levels(&VLM_vsctl, VLF_CONSOLE, VLL_EMER);
393 VLOG_ERR("%s", message);
394 ovs_error(0, "%s", message);
395 vsctl_exit(EXIT_FAILURE);
398 /* Frees the current transaction and the underlying IDL and then calls
401 * Freeing the transaction and the IDL is not strictly necessary, but it makes
402 * for a clean memory leak report from valgrind in the normal case. That makes
403 * it easier to notice real memory leaks. */
405 vsctl_exit(int status)
408 ovsdb_idl_txn_abort(the_idl_txn);
409 ovsdb_idl_txn_destroy(the_idl_txn);
411 ovsdb_idl_destroy(the_idl);
419 %s: ovs-vswitchd management utility\n\
420 usage: %s [OPTIONS] COMMAND [ARG...]\n\
423 add-br BRIDGE create a new bridge named BRIDGE\n\
424 add-br BRIDGE PARENT VLAN create new fake BRIDGE in PARENT on VLAN\n\
425 del-br BRIDGE delete BRIDGE and all of its ports\n\
426 list-br print the names of all the bridges\n\
427 br-exists BRIDGE test whether BRIDGE exists\n\
428 br-to-vlan BRIDGE print the VLAN which BRIDGE is on\n\
429 br-to-parent BRIDGE print the parent of BRIDGE\n\
430 br-set-external-id BRIDGE KEY VALUE set KEY on BRIDGE to VALUE\n\
431 br-set-external-id BRIDGE KEY unset KEY on BRIDGE\n\
432 br-get-external-id BRIDGE KEY print value of KEY on BRIDGE\n\
433 br-get-external-id BRIDGE list key-value pairs on BRIDGE\n\
436 list-ports BRIDGE print the names of all the ports on BRIDGE\n\
437 add-port BRIDGE PORT add network device PORT to BRIDGE\n\
438 add-bond BRIDGE PORT IFACE... add bonded port PORT in BRIDGE from IFACES\n\
439 del-port [BRIDGE] PORT delete PORT (which may be bonded) from BRIDGE\n\
440 port-to-br PORT print name of bridge that contains PORT\n\
441 A bond is considered to be a single port.\n\
443 Interface commands (a bond consists of multiple interfaces):\n\
444 list-ifaces BRIDGE print the names of all interfaces on BRIDGE\n\
445 iface-to-br IFACE print name of bridge that contains IFACE\n\
447 Controller commands:\n\
448 get-controller BRIDGE print the controller for BRIDGE\n\
449 del-controller BRIDGE delete the controller for BRIDGE\n\
450 set-controller BRIDGE TARGET set the controller for BRIDGE to TARGET\n\
451 get-fail-mode BRIDGE print the fail-mode for BRIDGE\n\
452 del-fail-mode BRIDGE delete the fail-mode for BRIDGE\n\
453 set-fail-mode BRIDGE MODE set the fail-mode for BRIDGE to MODE\n\
456 get-ssl print the SSL configuration\n\
457 del-ssl delete the SSL configuration\n\
458 set-ssl PRIV-KEY CERT CA-CERT set the SSL configuration\n\
461 emer-reset reset switch to known good state\n\
463 Database commands:\n\
464 list TBL [REC] list RECord (or all records) in TBL\n\
465 get TBL REC COL[:KEY] print values of COLumns in RECord in TBL\n\
466 set TBL REC COL[:KEY]=VALUE set COLumn values in RECord in TBL\n\
467 add TBL REC COL [KEY=]VALUE add (KEY=)VALUE to COLumn in RECord in TBL\n\
468 remove TBL REC COL [KEY=]VALUE remove (KEY=)VALUE from COLumn\n\
469 clear TBL REC COL clear values from COLumn in RECord in TBL\n\
470 create TBL COL[:KEY]=VALUE create and initialize new record\n\
471 destroy TBL REC delete RECord from TBL\n\
472 wait-until TBL REC [COL[:KEY]=VALUE] wait until condition is true\n\
473 Potentially unsafe database commands require --force option.\n\
476 --db=DATABASE connect to DATABASE\n\
478 --oneline print exactly one line of output per command\n",
479 program_name, program_name, default_db());
483 -h, --help display this help message\n\
484 -V, --version display version information\n");
493 def = xasprintf("unix:%s/db.sock", ovs_rundir);
498 struct vsctl_context {
502 struct shash options;
504 /* Modifiable state. */
506 struct ovsdb_idl *idl;
507 struct ovsdb_idl_txn *txn;
508 struct ovsdb_symbol_table *symtab;
509 const struct ovsrec_open_vswitch *ovs;
512 /* A command may set this member to true if some prerequisite is not met
513 * and the caller should wait for something to change and then retry. */
517 struct vsctl_bridge {
518 struct ovsrec_bridge *br_cfg;
520 struct ovsrec_controller **ctrl;
523 struct vsctl_bridge *parent;
528 struct ovsrec_port *port_cfg;
529 struct vsctl_bridge *bridge;
533 struct ovsrec_interface *iface_cfg;
534 struct vsctl_port *port;
538 struct vsctl_context *ctx;
539 struct shash bridges;
545 vsctl_context_to_string(const struct vsctl_context *ctx)
547 const struct shash_node *node;
553 SHASH_FOR_EACH (node, &ctx->options) {
554 svec_add(&words, node->name);
556 for (i = 0; i < ctx->argc; i++) {
557 svec_add(&words, ctx->argv[i]);
559 svec_terminate(&words);
561 s = process_escape_args(words.names);
563 svec_destroy(&words);
569 verify_ports(struct vsctl_context *ctx)
571 if (!ctx->verified_ports) {
572 const struct ovsrec_bridge *bridge;
573 const struct ovsrec_port *port;
575 ovsrec_open_vswitch_verify_bridges(ctx->ovs);
576 OVSREC_BRIDGE_FOR_EACH (bridge, ctx->idl) {
577 ovsrec_bridge_verify_ports(bridge);
579 OVSREC_PORT_FOR_EACH (port, ctx->idl) {
580 ovsrec_port_verify_interfaces(port);
583 ctx->verified_ports = true;
587 static struct vsctl_bridge *
588 add_bridge(struct vsctl_info *b,
589 struct ovsrec_bridge *br_cfg, const char *name,
590 struct vsctl_bridge *parent, int vlan)
592 struct vsctl_bridge *br = xmalloc(sizeof *br);
594 br->name = xstrdup(name);
598 br->ctrl = parent->br_cfg->controller;
599 br->n_ctrl = parent->br_cfg->n_controller;
600 br->fail_mode = parent->br_cfg->fail_mode;
602 br->ctrl = br_cfg->controller;
603 br->n_ctrl = br_cfg->n_controller;
604 br->fail_mode = br_cfg->fail_mode;
606 shash_add(&b->bridges, br->name, br);
611 port_is_fake_bridge(const struct ovsrec_port *port_cfg)
613 return (port_cfg->fake_bridge
615 && *port_cfg->tag >= 1 && *port_cfg->tag <= 4095);
618 static struct vsctl_bridge *
619 find_vlan_bridge(struct vsctl_info *info,
620 struct vsctl_bridge *parent, int vlan)
622 struct shash_node *node;
624 SHASH_FOR_EACH (node, &info->bridges) {
625 struct vsctl_bridge *br = node->data;
626 if (br->parent == parent && br->vlan == vlan) {
635 free_info(struct vsctl_info *info)
637 struct shash_node *node;
639 SHASH_FOR_EACH (node, &info->bridges) {
640 struct vsctl_bridge *bridge = node->data;
644 shash_destroy(&info->bridges);
646 shash_destroy_free_data(&info->ports);
647 shash_destroy_free_data(&info->ifaces);
651 get_info(struct vsctl_context *ctx, struct vsctl_info *info)
653 const struct ovsrec_open_vswitch *ovs = ctx->ovs;
654 struct shash bridges, ports;
658 shash_init(&info->bridges);
659 shash_init(&info->ports);
660 shash_init(&info->ifaces);
662 shash_init(&bridges);
664 for (i = 0; i < ovs->n_bridges; i++) {
665 struct ovsrec_bridge *br_cfg = ovs->bridges[i];
666 struct vsctl_bridge *br;
669 if (!shash_add_once(&bridges, br_cfg->name, NULL)) {
670 VLOG_WARN("%s: database contains duplicate bridge name",
674 br = add_bridge(info, br_cfg, br_cfg->name, NULL, 0);
679 for (j = 0; j < br_cfg->n_ports; j++) {
680 struct ovsrec_port *port_cfg = br_cfg->ports[j];
682 if (!shash_add_once(&ports, port_cfg->name, NULL)) {
683 VLOG_WARN("%s: database contains duplicate port name",
688 if (port_is_fake_bridge(port_cfg)
689 && shash_add_once(&bridges, port_cfg->name, NULL)) {
690 add_bridge(info, NULL, port_cfg->name, br, *port_cfg->tag);
694 shash_destroy(&bridges);
695 shash_destroy(&ports);
697 shash_init(&bridges);
699 for (i = 0; i < ovs->n_bridges; i++) {
700 struct ovsrec_bridge *br_cfg = ovs->bridges[i];
701 struct vsctl_bridge *br;
704 if (!shash_add_once(&bridges, br_cfg->name, NULL)) {
707 br = shash_find_data(&info->bridges, br_cfg->name);
708 for (j = 0; j < br_cfg->n_ports; j++) {
709 struct ovsrec_port *port_cfg = br_cfg->ports[j];
710 struct vsctl_port *port;
713 if (!shash_add_once(&ports, port_cfg->name, NULL)) {
717 if (port_is_fake_bridge(port_cfg)
718 && !shash_add_once(&bridges, port_cfg->name, NULL)) {
722 port = xmalloc(sizeof *port);
723 port->port_cfg = port_cfg;
725 && *port_cfg->tag >= 1 && *port_cfg->tag <= 4095) {
726 port->bridge = find_vlan_bridge(info, br, *port_cfg->tag);
733 shash_add(&info->ports, port_cfg->name, port);
735 for (k = 0; k < port_cfg->n_interfaces; k++) {
736 struct ovsrec_interface *iface_cfg = port_cfg->interfaces[k];
737 struct vsctl_iface *iface;
739 if (shash_find(&info->ifaces, iface_cfg->name)) {
740 VLOG_WARN("%s: database contains duplicate interface name",
745 iface = xmalloc(sizeof *iface);
746 iface->iface_cfg = iface_cfg;
748 shash_add(&info->ifaces, iface_cfg->name, iface);
752 shash_destroy(&bridges);
753 shash_destroy(&ports);
757 check_conflicts(struct vsctl_info *info, const char *name,
760 struct vsctl_iface *iface;
761 struct vsctl_port *port;
763 verify_ports(info->ctx);
765 if (shash_find(&info->bridges, name)) {
766 vsctl_fatal("%s because a bridge named %s already exists",
770 port = shash_find_data(&info->ports, name);
772 vsctl_fatal("%s because a port named %s already exists on "
773 "bridge %s", msg, name, port->bridge->name);
776 iface = shash_find_data(&info->ifaces, name);
778 vsctl_fatal("%s because an interface named %s already exists "
779 "on bridge %s", msg, name, iface->port->bridge->name);
785 static struct vsctl_bridge *
786 find_bridge(struct vsctl_info *info, const char *name, bool must_exist)
788 struct vsctl_bridge *br = shash_find_data(&info->bridges, name);
789 if (must_exist && !br) {
790 vsctl_fatal("no bridge named %s", name);
792 ovsrec_open_vswitch_verify_bridges(info->ctx->ovs);
796 static struct vsctl_bridge *
797 find_real_bridge(struct vsctl_info *info, const char *name, bool must_exist)
799 struct vsctl_bridge *br = find_bridge(info, name, must_exist);
800 if (br && br->parent) {
801 vsctl_fatal("%s is a fake bridge", name);
806 static struct vsctl_port *
807 find_port(struct vsctl_info *info, const char *name, bool must_exist)
809 struct vsctl_port *port = shash_find_data(&info->ports, name);
810 if (port && !strcmp(name, port->bridge->name)) {
813 if (must_exist && !port) {
814 vsctl_fatal("no port named %s", name);
816 verify_ports(info->ctx);
820 static struct vsctl_iface *
821 find_iface(struct vsctl_info *info, const char *name, bool must_exist)
823 struct vsctl_iface *iface = shash_find_data(&info->ifaces, name);
824 if (iface && !strcmp(name, iface->port->bridge->name)) {
827 if (must_exist && !iface) {
828 vsctl_fatal("no interface named %s", name);
830 verify_ports(info->ctx);
835 bridge_insert_port(struct ovsrec_bridge *br, struct ovsrec_port *port)
837 struct ovsrec_port **ports;
840 ports = xmalloc(sizeof *br->ports * (br->n_ports + 1));
841 for (i = 0; i < br->n_ports; i++) {
842 ports[i] = br->ports[i];
844 ports[br->n_ports] = port;
845 ovsrec_bridge_set_ports(br, ports, br->n_ports + 1);
850 bridge_delete_port(struct ovsrec_bridge *br, struct ovsrec_port *port)
852 struct ovsrec_port **ports;
855 ports = xmalloc(sizeof *br->ports * br->n_ports);
856 for (i = n = 0; i < br->n_ports; i++) {
857 if (br->ports[i] != port) {
858 ports[n++] = br->ports[i];
861 ovsrec_bridge_set_ports(br, ports, n);
866 ovs_insert_bridge(const struct ovsrec_open_vswitch *ovs,
867 struct ovsrec_bridge *bridge)
869 struct ovsrec_bridge **bridges;
872 bridges = xmalloc(sizeof *ovs->bridges * (ovs->n_bridges + 1));
873 for (i = 0; i < ovs->n_bridges; i++) {
874 bridges[i] = ovs->bridges[i];
876 bridges[ovs->n_bridges] = bridge;
877 ovsrec_open_vswitch_set_bridges(ovs, bridges, ovs->n_bridges + 1);
882 ovs_delete_bridge(const struct ovsrec_open_vswitch *ovs,
883 struct ovsrec_bridge *bridge)
885 struct ovsrec_bridge **bridges;
888 bridges = xmalloc(sizeof *ovs->bridges * ovs->n_bridges);
889 for (i = n = 0; i < ovs->n_bridges; i++) {
890 if (ovs->bridges[i] != bridge) {
891 bridges[n++] = ovs->bridges[i];
894 ovsrec_open_vswitch_set_bridges(ovs, bridges, n);
899 cmd_init(struct vsctl_context *ctx OVS_UNUSED)
904 cmd_emer_reset(struct vsctl_context *ctx)
906 const struct ovsdb_idl *idl = ctx->idl;
907 const struct ovsrec_bridge *br;
908 const struct ovsrec_port *port;
909 const struct ovsrec_interface *iface;
910 const struct ovsrec_mirror *mirror, *next_mirror;
911 const struct ovsrec_controller *ctrl, *next_ctrl;
912 const struct ovsrec_netflow *nf, *next_nf;
913 const struct ovsrec_ssl *ssl, *next_ssl;
914 const struct ovsrec_sflow *sflow, *next_sflow;
917 /* Reset the Open_vSwitch table. */
918 ovsrec_open_vswitch_set_managers(ctx->ovs, NULL, 0);
919 ovsrec_open_vswitch_set_ssl(ctx->ovs, NULL);
921 OVSREC_BRIDGE_FOR_EACH (br, idl) {
923 char *hw_key = "hwaddr";
926 ovsrec_bridge_set_controller(br, NULL, 0);
927 ovsrec_bridge_set_mirrors(br, NULL, 0);
928 ovsrec_bridge_set_netflow(br, NULL);
929 ovsrec_bridge_set_sflow(br, NULL);
930 ovsrec_bridge_set_flood_vlans(br, NULL, 0);
932 /* We only want to save the "hwaddr" key from other_config. */
933 for (i=0; i < br->n_other_config; i++) {
934 if (!strcmp(br->key_other_config[i], hw_key)) {
935 hw_val = br->value_other_config[i];
940 char *val = xstrdup(hw_val);
941 ovsrec_bridge_set_other_config(br, &hw_key, &val, 1);
944 ovsrec_bridge_set_other_config(br, NULL, NULL, 0);
948 OVSREC_PORT_FOR_EACH (port, idl) {
949 ovsrec_port_set_other_config(port, NULL, NULL, 0);
952 OVSREC_INTERFACE_FOR_EACH (iface, idl) {
953 /* xxx What do we do about gre/patch devices created by mgr? */
955 ovsrec_interface_set_ingress_policing_rate(iface, 0);
956 ovsrec_interface_set_ingress_policing_burst(iface, 0);
959 OVSREC_MIRROR_FOR_EACH_SAFE (mirror, next_mirror, idl) {
960 ovsrec_mirror_delete(mirror);
963 OVSREC_CONTROLLER_FOR_EACH_SAFE (ctrl, next_ctrl, idl) {
964 ovsrec_controller_delete(ctrl);
967 OVSREC_NETFLOW_FOR_EACH_SAFE (nf, next_nf, idl) {
968 ovsrec_netflow_delete(nf);
971 OVSREC_SSL_FOR_EACH_SAFE (ssl, next_ssl, idl) {
972 ovsrec_ssl_delete(ssl);
975 OVSREC_SFLOW_FOR_EACH_SAFE (sflow, next_sflow, idl) {
976 ovsrec_sflow_delete(sflow);
981 cmd_add_br(struct vsctl_context *ctx)
983 bool may_exist = shash_find(&ctx->options, "--may-exist") != 0;
984 const char *br_name, *parent_name;
985 struct vsctl_info info;
988 br_name = ctx->argv[1];
989 if (ctx->argc == 2) {
992 } else if (ctx->argc == 4) {
993 parent_name = ctx->argv[2];
994 vlan = atoi(ctx->argv[3]);
995 if (vlan < 1 || vlan > 4095) {
996 vsctl_fatal("%s: vlan must be between 1 and 4095", ctx->argv[0]);
999 vsctl_fatal("'%s' command takes exactly 1 or 3 arguments",
1003 get_info(ctx, &info);
1005 struct vsctl_bridge *br;
1007 br = find_bridge(&info, br_name, false);
1011 vsctl_fatal("\"--may-exist add-br %s\" but %s is "
1012 "a VLAN bridge for VLAN %d",
1013 br_name, br_name, br->vlan);
1017 vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
1018 "is not a VLAN bridge",
1019 br_name, parent_name, vlan, br_name);
1020 } else if (strcmp(br->parent->name, parent_name)) {
1021 vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
1022 "has the wrong parent %s",
1023 br_name, parent_name, vlan,
1024 br_name, br->parent->name);
1025 } else if (br->vlan != vlan) {
1026 vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
1027 "is a VLAN bridge for the wrong VLAN %d",
1028 br_name, parent_name, vlan, br_name, br->vlan);
1034 check_conflicts(&info, br_name,
1035 xasprintf("cannot create a bridge named %s", br_name));
1038 struct ovsrec_port *port;
1039 struct ovsrec_interface *iface;
1040 struct ovsrec_bridge *br;
1042 iface = ovsrec_interface_insert(ctx->txn);
1043 ovsrec_interface_set_name(iface, br_name);
1045 port = ovsrec_port_insert(ctx->txn);
1046 ovsrec_port_set_name(port, br_name);
1047 ovsrec_port_set_interfaces(port, &iface, 1);
1049 br = ovsrec_bridge_insert(ctx->txn);
1050 ovsrec_bridge_set_name(br, br_name);
1051 ovsrec_bridge_set_ports(br, &port, 1);
1053 ovs_insert_bridge(ctx->ovs, br);
1055 struct vsctl_bridge *parent;
1056 struct ovsrec_port *port;
1057 struct ovsrec_interface *iface;
1058 struct ovsrec_bridge *br;
1061 parent = find_bridge(&info, parent_name, false);
1062 if (parent && parent->vlan) {
1063 vsctl_fatal("cannot create bridge with fake bridge as parent");
1066 vsctl_fatal("parent bridge %s does not exist", parent_name);
1068 br = parent->br_cfg;
1070 iface = ovsrec_interface_insert(ctx->txn);
1071 ovsrec_interface_set_name(iface, br_name);
1072 ovsrec_interface_set_type(iface, "internal");
1074 port = ovsrec_port_insert(ctx->txn);
1075 ovsrec_port_set_name(port, br_name);
1076 ovsrec_port_set_interfaces(port, &iface, 1);
1077 ovsrec_port_set_fake_bridge(port, true);
1078 ovsrec_port_set_tag(port, &tag, 1);
1080 bridge_insert_port(br, port);
1087 del_port(struct vsctl_info *info, struct vsctl_port *port)
1089 struct shash_node *node;
1091 SHASH_FOR_EACH (node, &info->ifaces) {
1092 struct vsctl_iface *iface = node->data;
1093 if (iface->port == port) {
1094 ovsrec_interface_delete(iface->iface_cfg);
1097 ovsrec_port_delete(port->port_cfg);
1099 bridge_delete_port((port->bridge->parent
1100 ? port->bridge->parent->br_cfg
1101 : port->bridge->br_cfg), port->port_cfg);
1105 cmd_del_br(struct vsctl_context *ctx)
1107 bool must_exist = !shash_find(&ctx->options, "--if-exists");
1108 struct vsctl_bridge *bridge;
1109 struct vsctl_info info;
1111 get_info(ctx, &info);
1112 bridge = find_bridge(&info, ctx->argv[1], must_exist);
1114 struct shash_node *node;
1116 SHASH_FOR_EACH (node, &info.ports) {
1117 struct vsctl_port *port = node->data;
1118 if (port->bridge == bridge || port->bridge->parent == bridge
1119 || !strcmp(port->port_cfg->name, bridge->name)) {
1120 del_port(&info, port);
1123 if (bridge->br_cfg) {
1124 ovsrec_bridge_delete(bridge->br_cfg);
1125 ovs_delete_bridge(ctx->ovs, bridge->br_cfg);
1132 output_sorted(struct svec *svec, struct ds *output)
1138 SVEC_FOR_EACH (i, name, svec) {
1139 ds_put_format(output, "%s\n", name);
1144 cmd_list_br(struct vsctl_context *ctx)
1146 struct shash_node *node;
1147 struct vsctl_info info;
1148 struct svec bridges;
1150 get_info(ctx, &info);
1152 svec_init(&bridges);
1153 SHASH_FOR_EACH (node, &info.bridges) {
1154 struct vsctl_bridge *br = node->data;
1155 svec_add(&bridges, br->name);
1157 output_sorted(&bridges, &ctx->output);
1158 svec_destroy(&bridges);
1164 cmd_br_exists(struct vsctl_context *ctx)
1166 struct vsctl_info info;
1168 get_info(ctx, &info);
1169 if (!find_bridge(&info, ctx->argv[1], false)) {
1175 /* Returns true if 'b_prefix' (of length 'b_prefix_len') concatenated with 'b'
1176 * equals 'a', false otherwise. */
1178 key_matches(const char *a,
1179 const char *b_prefix, size_t b_prefix_len, const char *b)
1181 return !strncmp(a, b_prefix, b_prefix_len) && !strcmp(a + b_prefix_len, b);
1185 set_external_id(char **old_keys, char **old_values, size_t old_n,
1186 char *key, char *value,
1187 char ***new_keysp, char ***new_valuesp, size_t *new_np)
1194 new_keys = xmalloc(sizeof *new_keys * (old_n + 1));
1195 new_values = xmalloc(sizeof *new_values * (old_n + 1));
1197 for (i = 0; i < old_n; i++) {
1198 if (strcmp(key, old_keys[i])) {
1199 new_keys[new_n] = old_keys[i];
1200 new_values[new_n] = old_values[i];
1205 new_keys[new_n] = key;
1206 new_values[new_n] = value;
1209 *new_keysp = new_keys;
1210 *new_valuesp = new_values;
1215 cmd_br_set_external_id(struct vsctl_context *ctx)
1217 struct vsctl_info info;
1218 struct vsctl_bridge *bridge;
1219 char **keys, **values;
1222 get_info(ctx, &info);
1223 bridge = find_bridge(&info, ctx->argv[1], true);
1224 if (bridge->br_cfg) {
1225 set_external_id(bridge->br_cfg->key_external_ids,
1226 bridge->br_cfg->value_external_ids,
1227 bridge->br_cfg->n_external_ids,
1228 ctx->argv[2], ctx->argc >= 4 ? ctx->argv[3] : NULL,
1229 &keys, &values, &n);
1230 ovsrec_bridge_verify_external_ids(bridge->br_cfg);
1231 ovsrec_bridge_set_external_ids(bridge->br_cfg, keys, values, n);
1233 char *key = xasprintf("fake-bridge-%s", ctx->argv[2]);
1234 struct vsctl_port *port = shash_find_data(&info.ports, ctx->argv[1]);
1235 set_external_id(port->port_cfg->key_external_ids,
1236 port->port_cfg->value_external_ids,
1237 port->port_cfg->n_external_ids,
1238 key, ctx->argc >= 4 ? ctx->argv[3] : NULL,
1239 &keys, &values, &n);
1240 ovsrec_port_verify_external_ids(port->port_cfg);
1241 ovsrec_port_set_external_ids(port->port_cfg, keys, values, n);
1251 get_external_id(char **keys, char **values, size_t n,
1252 const char *prefix, const char *key,
1255 size_t prefix_len = strlen(prefix);
1260 for (i = 0; i < n; i++) {
1261 if (!key && !strncmp(keys[i], prefix, prefix_len)) {
1262 svec_add_nocopy(&svec, xasprintf("%s=%s",
1263 keys[i] + prefix_len, values[i]));
1264 } else if (key_matches(keys[i], prefix, prefix_len, key)) {
1265 svec_add(&svec, values[i]);
1269 output_sorted(&svec, output);
1270 svec_destroy(&svec);
1274 cmd_br_get_external_id(struct vsctl_context *ctx)
1276 struct vsctl_info info;
1277 struct vsctl_bridge *bridge;
1279 get_info(ctx, &info);
1280 bridge = find_bridge(&info, ctx->argv[1], true);
1281 if (bridge->br_cfg) {
1282 ovsrec_bridge_verify_external_ids(bridge->br_cfg);
1283 get_external_id(bridge->br_cfg->key_external_ids,
1284 bridge->br_cfg->value_external_ids,
1285 bridge->br_cfg->n_external_ids,
1286 "", ctx->argc >= 3 ? ctx->argv[2] : NULL,
1289 struct vsctl_port *port = shash_find_data(&info.ports, ctx->argv[1]);
1290 ovsrec_port_verify_external_ids(port->port_cfg);
1291 get_external_id(port->port_cfg->key_external_ids,
1292 port->port_cfg->value_external_ids,
1293 port->port_cfg->n_external_ids,
1294 "fake-bridge-", ctx->argc >= 3 ? ctx->argv[2] : NULL, &ctx->output);
1301 cmd_list_ports(struct vsctl_context *ctx)
1303 struct vsctl_bridge *br;
1304 struct shash_node *node;
1305 struct vsctl_info info;
1308 get_info(ctx, &info);
1309 br = find_bridge(&info, ctx->argv[1], true);
1310 ovsrec_bridge_verify_ports(br->br_cfg ? br->br_cfg : br->parent->br_cfg);
1313 SHASH_FOR_EACH (node, &info.ports) {
1314 struct vsctl_port *port = node->data;
1316 if (strcmp(port->port_cfg->name, br->name) && br == port->bridge) {
1317 svec_add(&ports, port->port_cfg->name);
1320 output_sorted(&ports, &ctx->output);
1321 svec_destroy(&ports);
1327 add_port(struct vsctl_context *ctx,
1328 const char *br_name, const char *port_name,
1329 bool may_exist, bool fake_iface,
1330 char *iface_names[], int n_ifaces,
1331 char *settings[], int n_settings)
1333 struct vsctl_info info;
1334 struct vsctl_bridge *bridge;
1335 struct ovsrec_interface **ifaces;
1336 struct ovsrec_port *port;
1339 get_info(ctx, &info);
1341 struct vsctl_port *port;
1343 port = find_port(&info, port_name, false);
1345 struct svec want_names, have_names;
1348 svec_init(&want_names);
1349 for (i = 0; i < n_ifaces; i++) {
1350 svec_add(&want_names, iface_names[i]);
1352 svec_sort(&want_names);
1354 svec_init(&have_names);
1355 for (i = 0; i < port->port_cfg->n_interfaces; i++) {
1356 svec_add(&have_names, port->port_cfg->interfaces[i]->name);
1358 svec_sort(&have_names);
1360 if (strcmp(port->bridge->name, br_name)) {
1361 char *command = vsctl_context_to_string(ctx);
1362 vsctl_fatal("\"%s\" but %s is actually attached to bridge %s",
1363 command, port_name, port->bridge->name);
1366 if (!svec_equal(&want_names, &have_names)) {
1367 char *have_names_string = svec_join(&have_names, ", ", "");
1368 char *command = vsctl_context_to_string(ctx);
1370 vsctl_fatal("\"%s\" but %s actually has interface(s) %s",
1371 command, port_name, have_names_string);
1374 svec_destroy(&want_names);
1375 svec_destroy(&have_names);
1380 check_conflicts(&info, port_name,
1381 xasprintf("cannot create a port named %s", port_name));
1382 for (i = 0; i < n_ifaces; i++) {
1383 check_conflicts(&info, iface_names[i],
1384 xasprintf("cannot create an interface named %s",
1387 bridge = find_bridge(&info, br_name, true);
1389 ifaces = xmalloc(n_ifaces * sizeof *ifaces);
1390 for (i = 0; i < n_ifaces; i++) {
1391 ifaces[i] = ovsrec_interface_insert(ctx->txn);
1392 ovsrec_interface_set_name(ifaces[i], iface_names[i]);
1395 port = ovsrec_port_insert(ctx->txn);
1396 ovsrec_port_set_name(port, port_name);
1397 ovsrec_port_set_interfaces(port, ifaces, n_ifaces);
1398 ovsrec_port_set_bond_fake_iface(port, fake_iface);
1402 int64_t tag = bridge->vlan;
1403 ovsrec_port_set_tag(port, &tag, 1);
1406 for (i = 0; i < n_settings; i++) {
1407 set_column(get_table("Port"), &port->header_, settings[i],
1411 bridge_insert_port((bridge->parent ? bridge->parent->br_cfg
1412 : bridge->br_cfg), port);
1418 cmd_add_port(struct vsctl_context *ctx)
1420 bool may_exist = shash_find(&ctx->options, "--may-exist") != 0;
1422 add_port(ctx, ctx->argv[1], ctx->argv[2], may_exist, false,
1423 &ctx->argv[2], 1, &ctx->argv[3], ctx->argc - 3);
1427 cmd_add_bond(struct vsctl_context *ctx)
1429 bool may_exist = shash_find(&ctx->options, "--may-exist") != 0;
1430 bool fake_iface = shash_find(&ctx->options, "--fake-iface");
1434 n_ifaces = ctx->argc - 3;
1435 for (i = 3; i < ctx->argc; i++) {
1436 if (strchr(ctx->argv[i], '=')) {
1442 vsctl_fatal("add-bond requires at least 2 interfaces, but only "
1443 "%d were specified", n_ifaces);
1446 add_port(ctx, ctx->argv[1], ctx->argv[2], may_exist, fake_iface,
1447 &ctx->argv[3], n_ifaces,
1448 &ctx->argv[n_ifaces + 3], ctx->argc - 3 - n_ifaces);
1452 cmd_del_port(struct vsctl_context *ctx)
1454 bool must_exist = !shash_find(&ctx->options, "--if-exists");
1455 bool with_iface = shash_find(&ctx->options, "--with-iface") != NULL;
1456 struct vsctl_port *port;
1457 struct vsctl_info info;
1459 get_info(ctx, &info);
1461 port = find_port(&info, ctx->argv[ctx->argc - 1], must_exist);
1463 const char *target = ctx->argv[ctx->argc - 1];
1464 struct vsctl_iface *iface;
1466 port = find_port(&info, target, false);
1468 iface = find_iface(&info, target, false);
1473 if (must_exist && !port) {
1474 vsctl_fatal("no port or interface named %s", target);
1479 if (ctx->argc == 3) {
1480 struct vsctl_bridge *bridge;
1482 bridge = find_bridge(&info, ctx->argv[1], true);
1483 if (port->bridge != bridge) {
1484 if (port->bridge->parent == bridge) {
1485 vsctl_fatal("bridge %s does not have a port %s (although "
1486 "its parent bridge %s does)",
1487 ctx->argv[1], ctx->argv[2],
1488 bridge->parent->name);
1490 vsctl_fatal("bridge %s does not have a port %s",
1491 ctx->argv[1], ctx->argv[2]);
1496 del_port(&info, port);
1503 cmd_port_to_br(struct vsctl_context *ctx)
1505 struct vsctl_port *port;
1506 struct vsctl_info info;
1508 get_info(ctx, &info);
1509 port = find_port(&info, ctx->argv[1], true);
1510 ds_put_format(&ctx->output, "%s\n", port->bridge->name);
1515 cmd_br_to_vlan(struct vsctl_context *ctx)
1517 struct vsctl_bridge *bridge;
1518 struct vsctl_info info;
1520 get_info(ctx, &info);
1521 bridge = find_bridge(&info, ctx->argv[1], true);
1522 ds_put_format(&ctx->output, "%d\n", bridge->vlan);
1527 cmd_br_to_parent(struct vsctl_context *ctx)
1529 struct vsctl_bridge *bridge;
1530 struct vsctl_info info;
1532 get_info(ctx, &info);
1533 bridge = find_bridge(&info, ctx->argv[1], true);
1534 if (bridge->parent) {
1535 bridge = bridge->parent;
1537 ds_put_format(&ctx->output, "%s\n", bridge->name);
1542 cmd_list_ifaces(struct vsctl_context *ctx)
1544 struct vsctl_bridge *br;
1545 struct shash_node *node;
1546 struct vsctl_info info;
1549 get_info(ctx, &info);
1550 br = find_bridge(&info, ctx->argv[1], true);
1554 SHASH_FOR_EACH (node, &info.ifaces) {
1555 struct vsctl_iface *iface = node->data;
1557 if (strcmp(iface->iface_cfg->name, br->name)
1558 && br == iface->port->bridge) {
1559 svec_add(&ifaces, iface->iface_cfg->name);
1562 output_sorted(&ifaces, &ctx->output);
1563 svec_destroy(&ifaces);
1569 cmd_iface_to_br(struct vsctl_context *ctx)
1571 struct vsctl_iface *iface;
1572 struct vsctl_info info;
1574 get_info(ctx, &info);
1575 iface = find_iface(&info, ctx->argv[1], true);
1576 ds_put_format(&ctx->output, "%s\n", iface->port->bridge->name);
1581 verify_controllers(struct ovsrec_bridge *bridge)
1586 ovsrec_bridge_verify_controller(bridge);
1587 for (i = 0; i < bridge->n_controller; i++) {
1588 ovsrec_controller_verify_target(bridge->controller[i]);
1594 cmd_get_controller(struct vsctl_context *ctx)
1596 struct vsctl_info info;
1597 struct vsctl_bridge *br;
1598 struct svec targets;
1601 get_info(ctx, &info);
1602 br = find_bridge(&info, ctx->argv[1], true);
1603 verify_controllers(br->br_cfg);
1605 /* Print the targets in sorted order for reproducibility. */
1606 svec_init(&targets);
1607 for (i = 0; i < br->n_ctrl; i++) {
1608 svec_add(&targets, br->ctrl[i]->target);
1611 svec_sort(&targets);
1612 for (i = 0; i < targets.n; i++) {
1613 ds_put_format(&ctx->output, "%s\n", targets.names[i]);
1615 svec_destroy(&targets);
1621 delete_controllers(struct ovsrec_controller **controllers,
1622 size_t n_controllers)
1626 for (i = 0; i < n_controllers; i++) {
1627 ovsrec_controller_delete(controllers[i]);
1632 cmd_del_controller(struct vsctl_context *ctx)
1634 struct vsctl_info info;
1635 struct vsctl_bridge *br;
1637 get_info(ctx, &info);
1638 br = find_real_bridge(&info, ctx->argv[1], true);
1639 verify_controllers(br->br_cfg);
1642 delete_controllers(br->ctrl, br->n_ctrl);
1643 ovsrec_bridge_set_controller(br->br_cfg, NULL, 0);
1649 static struct ovsrec_controller **
1650 insert_controllers(struct ovsdb_idl_txn *txn, char *targets[], size_t n)
1652 struct ovsrec_controller **controllers;
1655 controllers = xmalloc(n * sizeof *controllers);
1656 for (i = 0; i < n; i++) {
1657 controllers[i] = ovsrec_controller_insert(txn);
1658 ovsrec_controller_set_target(controllers[i], targets[i]);
1665 cmd_set_controller(struct vsctl_context *ctx)
1667 struct vsctl_info info;
1668 struct vsctl_bridge *br;
1669 struct ovsrec_controller **controllers;
1672 get_info(ctx, &info);
1673 br = find_real_bridge(&info, ctx->argv[1], true);
1674 verify_controllers(br->br_cfg);
1676 delete_controllers(br->ctrl, br->n_ctrl);
1679 controllers = insert_controllers(ctx->txn, &ctx->argv[2], n);
1680 ovsrec_bridge_set_controller(br->br_cfg, controllers, n);
1687 cmd_get_fail_mode(struct vsctl_context *ctx)
1689 struct vsctl_info info;
1690 struct vsctl_bridge *br;
1692 get_info(ctx, &info);
1693 br = find_bridge(&info, ctx->argv[1], true);
1696 ovsrec_bridge_verify_fail_mode(br->br_cfg);
1698 if (br->fail_mode && strlen(br->fail_mode)) {
1699 ds_put_format(&ctx->output, "%s\n", br->fail_mode);
1706 cmd_del_fail_mode(struct vsctl_context *ctx)
1708 struct vsctl_info info;
1709 struct vsctl_bridge *br;
1711 get_info(ctx, &info);
1712 br = find_real_bridge(&info, ctx->argv[1], true);
1714 ovsrec_bridge_set_fail_mode(br->br_cfg, NULL);
1720 cmd_set_fail_mode(struct vsctl_context *ctx)
1722 struct vsctl_info info;
1723 struct vsctl_bridge *br;
1724 const char *fail_mode = ctx->argv[2];
1726 get_info(ctx, &info);
1727 br = find_real_bridge(&info, ctx->argv[1], true);
1729 if (strcmp(fail_mode, "standalone") && strcmp(fail_mode, "secure")) {
1730 vsctl_fatal("fail-mode must be \"standalone\" or \"secure\"");
1733 ovsrec_bridge_set_fail_mode(br->br_cfg, fail_mode);
1739 cmd_get_ssl(struct vsctl_context *ctx)
1741 struct ovsrec_ssl *ssl = ctx->ovs->ssl;
1743 ovsrec_open_vswitch_verify_ssl(ctx->ovs);
1745 ovsrec_ssl_verify_private_key(ssl);
1746 ovsrec_ssl_verify_certificate(ssl);
1747 ovsrec_ssl_verify_ca_cert(ssl);
1748 ovsrec_ssl_verify_bootstrap_ca_cert(ssl);
1750 ds_put_format(&ctx->output, "Private key: %s\n", ssl->private_key);
1751 ds_put_format(&ctx->output, "Certificate: %s\n", ssl->certificate);
1752 ds_put_format(&ctx->output, "CA Certificate: %s\n", ssl->ca_cert);
1753 ds_put_format(&ctx->output, "Bootstrap: %s\n",
1754 ssl->bootstrap_ca_cert ? "true" : "false");
1759 cmd_del_ssl(struct vsctl_context *ctx)
1761 struct ovsrec_ssl *ssl = ctx->ovs->ssl;
1764 ovsrec_open_vswitch_verify_ssl(ctx->ovs);
1765 ovsrec_ssl_delete(ssl);
1766 ovsrec_open_vswitch_set_ssl(ctx->ovs, NULL);
1771 cmd_set_ssl(struct vsctl_context *ctx)
1773 bool bootstrap = shash_find(&ctx->options, "--bootstrap");
1774 struct ovsrec_ssl *ssl = ctx->ovs->ssl;
1776 ovsrec_open_vswitch_verify_ssl(ctx->ovs);
1778 ovsrec_ssl_delete(ssl);
1780 ssl = ovsrec_ssl_insert(ctx->txn);
1782 ovsrec_ssl_set_private_key(ssl, ctx->argv[1]);
1783 ovsrec_ssl_set_certificate(ssl, ctx->argv[2]);
1784 ovsrec_ssl_set_ca_cert(ssl, ctx->argv[3]);
1786 ovsrec_ssl_set_bootstrap_ca_cert(ssl, bootstrap);
1788 ovsrec_open_vswitch_set_ssl(ctx->ovs, ssl);
1791 /* Parameter commands. */
1793 struct vsctl_row_id {
1794 const struct ovsdb_idl_table_class *table;
1795 const struct ovsdb_idl_column *name_column;
1796 const struct ovsdb_idl_column *uuid_column;
1799 struct vsctl_table_class {
1800 struct ovsdb_idl_table_class *class;
1801 struct vsctl_row_id row_ids[2];
1804 static const struct vsctl_table_class tables[] = {
1805 {&ovsrec_table_bridge,
1806 {{&ovsrec_table_bridge, &ovsrec_bridge_col_name, NULL},
1807 {NULL, NULL, NULL}}},
1809 {&ovsrec_table_controller,
1810 {{&ovsrec_table_bridge,
1811 &ovsrec_bridge_col_name,
1812 &ovsrec_bridge_col_controller}}},
1814 {&ovsrec_table_interface,
1815 {{&ovsrec_table_interface, &ovsrec_interface_col_name, NULL},
1816 {NULL, NULL, NULL}}},
1818 {&ovsrec_table_mirror,
1819 {{&ovsrec_table_mirror, &ovsrec_mirror_col_name, NULL},
1820 {NULL, NULL, NULL}}},
1822 {&ovsrec_table_netflow,
1823 {{&ovsrec_table_bridge,
1824 &ovsrec_bridge_col_name,
1825 &ovsrec_bridge_col_netflow},
1826 {NULL, NULL, NULL}}},
1828 {&ovsrec_table_open_vswitch,
1829 {{&ovsrec_table_open_vswitch, NULL, NULL},
1830 {NULL, NULL, NULL}}},
1832 {&ovsrec_table_port,
1833 {{&ovsrec_table_port, &ovsrec_port_col_name, NULL},
1834 {NULL, NULL, NULL}}},
1837 {{&ovsrec_table_port, &ovsrec_port_col_name, &ovsrec_port_col_qos},
1838 {NULL, NULL, NULL}}},
1840 {&ovsrec_table_queue,
1841 {{NULL, NULL, NULL},
1842 {NULL, NULL, NULL}}},
1845 {{&ovsrec_table_open_vswitch, NULL, &ovsrec_open_vswitch_col_ssl}}},
1847 {&ovsrec_table_sflow,
1848 {{&ovsrec_table_bridge,
1849 &ovsrec_bridge_col_name,
1850 &ovsrec_bridge_col_sflow},
1851 {NULL, NULL, NULL}}},
1853 {NULL, {{NULL, NULL, NULL}, {NULL, NULL, NULL}}}
1857 die_if_error(char *error)
1860 vsctl_fatal("%s", error);
1865 to_lower_and_underscores(unsigned c)
1867 return c == '-' ? '_' : tolower(c);
1871 score_partial_match(const char *name, const char *s)
1875 if (!strcmp(name, s)) {
1878 for (score = 0; ; score++, name++, s++) {
1879 if (to_lower_and_underscores(*name) != to_lower_and_underscores(*s)) {
1881 } else if (*name == '\0') {
1882 return UINT_MAX - 1;
1885 return *s == '\0' ? score : 0;
1888 static const struct vsctl_table_class *
1889 get_table(const char *table_name)
1891 const struct vsctl_table_class *table;
1892 const struct vsctl_table_class *best_match = NULL;
1893 unsigned int best_score = 0;
1895 for (table = tables; table->class; table++) {
1896 unsigned int score = score_partial_match(table->class->name,
1898 if (score > best_score) {
1901 } else if (score == best_score) {
1907 } else if (best_score) {
1908 vsctl_fatal("multiple table names match \"%s\"", table_name);
1910 vsctl_fatal("unknown table \"%s\"", table_name);
1914 static const struct ovsdb_idl_row *
1915 get_row_by_id(struct vsctl_context *ctx, const struct vsctl_table_class *table,
1916 const struct vsctl_row_id *id, const char *record_id)
1918 const struct ovsdb_idl_row *referrer, *final;
1924 if (!id->name_column) {
1925 if (strcmp(record_id, ".")) {
1928 referrer = ovsdb_idl_first_row(ctx->idl, id->table);
1929 if (!referrer || ovsdb_idl_next_row(referrer)) {
1933 const struct ovsdb_idl_row *row;
1936 for (row = ovsdb_idl_first_row(ctx->idl, id->table);
1938 row = ovsdb_idl_next_row(row))
1940 const struct ovsdb_datum *name;
1942 name = ovsdb_idl_get(row, id->name_column,
1943 OVSDB_TYPE_STRING, OVSDB_TYPE_VOID);
1944 if (name->n == 1 && !strcmp(name->keys[0].string, record_id)) {
1946 vsctl_fatal("multiple rows in %s match \"%s\"",
1947 table->class->name, record_id);
1958 if (id->uuid_column) {
1959 const struct ovsdb_datum *uuid;
1961 ovsdb_idl_txn_verify(referrer, id->uuid_column);
1962 uuid = ovsdb_idl_get(referrer, id->uuid_column,
1963 OVSDB_TYPE_UUID, OVSDB_TYPE_VOID);
1965 final = ovsdb_idl_get_row_for_uuid(ctx->idl, table->class,
1966 &uuid->keys[0].uuid);
1975 static const struct ovsdb_idl_row *
1976 get_row (struct vsctl_context *ctx,
1977 const struct vsctl_table_class *table, const char *record_id)
1979 const struct ovsdb_idl_row *row;
1982 if (uuid_from_string(&uuid, record_id)) {
1983 row = ovsdb_idl_get_row_for_uuid(ctx->idl, table->class, &uuid);
1987 for (i = 0; i < ARRAY_SIZE(table->row_ids); i++) {
1988 row = get_row_by_id(ctx, table, &table->row_ids[i], record_id);
1997 static const struct ovsdb_idl_row *
1998 must_get_row(struct vsctl_context *ctx,
1999 const struct vsctl_table_class *table, const char *record_id)
2001 const struct ovsdb_idl_row *row = get_row(ctx, table, record_id);
2003 vsctl_fatal("no row \"%s\" in table %s",
2004 record_id, table->class->name);
2010 get_column(const struct vsctl_table_class *table, const char *column_name,
2011 const struct ovsdb_idl_column **columnp)
2013 const struct ovsdb_idl_column *best_match = NULL;
2014 unsigned int best_score = 0;
2017 for (i = 0; i < table->class->n_columns; i++) {
2018 const struct ovsdb_idl_column *column = &table->class->columns[i];
2019 unsigned int score = score_partial_match(column->name, column_name);
2020 if (score > best_score) {
2021 best_match = column;
2023 } else if (score == best_score) {
2028 *columnp = best_match;
2031 } else if (best_score) {
2032 return xasprintf("%s contains more than one column whose name "
2033 "matches \"%s\"", table->class->name, column_name);
2035 return xasprintf("%s does not contain a column whose name matches "
2036 "\"%s\"", table->class->name, column_name);
2041 missing_operator_error(const char *arg, const char **allowed_operators,
2047 ds_put_format(&s, "%s: argument does not end in ", arg);
2048 ds_put_format(&s, "\"%s\"", allowed_operators[0]);
2049 if (n_allowed == 2) {
2050 ds_put_format(&s, " or \"%s\"", allowed_operators[1]);
2051 } else if (n_allowed > 2) {
2054 for (i = 1; i < n_allowed - 1; i++) {
2055 ds_put_format(&s, ", \"%s\"", allowed_operators[i]);
2057 ds_put_format(&s, ", or \"%s\"", allowed_operators[i]);
2059 ds_put_format(&s, " followed by a value.");
2061 return ds_steal_cstr(&s);
2064 /* Breaks 'arg' apart into a number of fields in the following order:
2066 * - If 'columnp' is nonnull, the name of a column in 'table'. The column
2067 * is stored into '*columnp'. The column name may be abbreviated.
2069 * - If 'keyp' is nonnull, optionally a key string. (If both 'columnp'
2070 * and 'keyp' are nonnull, then the column and key names are expected to
2071 * be separated by ':'). The key is stored as a malloc()'d string into
2072 * '*keyp', or NULL if no key is present in 'arg'.
2074 * - If 'valuep' is nonnull, an operator followed by a value string. The
2075 * allowed operators are the 'n_allowed' string in 'allowed_operators',
2076 * or just "=" if 'n_allowed' is 0. If 'operatorp' is nonnull, then the
2077 * operator is stored into '*operatorp' (one of the pointers from
2078 * 'allowed_operators' is stored; nothing is malloc()'d). The value is
2079 * stored as a malloc()'d string into '*valuep', or NULL if no value is
2082 * At least 'columnp' or 'keyp' must be nonnull.
2084 * On success, returns NULL. On failure, returned a malloc()'d string error
2085 * message and stores NULL into all of the nonnull output arguments. */
2086 static char * WARN_UNUSED_RESULT
2087 parse_column_key_value(const char *arg,
2088 const struct vsctl_table_class *table,
2089 const struct ovsdb_idl_column **columnp, char **keyp,
2090 const char **operatorp,
2091 const char **allowed_operators, size_t n_allowed,
2094 const char *p = arg;
2097 assert(columnp || keyp);
2098 assert(!(operatorp && !valuep));
2106 /* Parse column name. */
2110 error = ovsdb_token_parse(&p, &column_name);
2114 if (column_name[0] == '\0') {
2116 error = xasprintf("%s: missing column name", arg);
2119 error = get_column(table, column_name, columnp);
2126 /* Parse key string. */
2127 if (*p == ':' || !columnp) {
2131 error = xasprintf("%s: key not accepted here", arg);
2134 error = ovsdb_token_parse(&p, keyp);
2142 /* Parse value string. */
2148 if (!allowed_operators) {
2149 static const char *equals = "=";
2150 allowed_operators = =
2156 for (i = 0; i < n_allowed; i++) {
2157 const char *op = allowed_operators[i];
2158 size_t op_len = strlen(op);
2160 if (op_len > best_len && !strncmp(op, p, op_len) && p[op_len]) {
2166 error = missing_operator_error(arg, allowed_operators, n_allowed);
2173 *valuep = xstrdup(p + best_len);
2179 error = xasprintf("%s: trailing garbage \"%s\" in argument",
2205 cmd_get(struct vsctl_context *ctx)
2207 bool if_exists = shash_find(&ctx->options, "--if-exists");
2208 const char *table_name = ctx->argv[1];
2209 const char *record_id = ctx->argv[2];
2210 const struct vsctl_table_class *table;
2211 const struct ovsdb_idl_row *row;
2212 struct ds *out = &ctx->output;
2215 table = get_table(table_name);
2216 row = must_get_row(ctx, table, record_id);
2217 for (i = 3; i < ctx->argc; i++) {
2218 const struct ovsdb_idl_column *column;
2219 const struct ovsdb_datum *datum;
2222 /* Special case for obtaining the UUID of a row. We can't just do this
2223 * through parse_column_key_value() below since it returns a "struct
2224 * ovsdb_idl_column" and the UUID column doesn't have one. */
2225 if (!strcasecmp(ctx->argv[i], "_uuid")
2226 || !strcasecmp(ctx->argv[i], "-uuid")) {
2227 ds_put_format(out, UUID_FMT"\n", UUID_ARGS(&row->uuid));
2231 die_if_error(parse_column_key_value(ctx->argv[i], table,
2232 &column, &key_string,
2233 NULL, NULL, 0, NULL));
2235 ovsdb_idl_txn_verify(row, column);
2236 datum = ovsdb_idl_read(row, column);
2238 union ovsdb_atom key;
2241 if (column->type.value.type == OVSDB_TYPE_VOID) {
2242 vsctl_fatal("cannot specify key to get for non-map column %s",
2246 die_if_error(ovsdb_atom_from_string(&key,
2248 key_string, ctx->symtab));
2250 idx = ovsdb_datum_find_key(datum, &key,
2251 column->type.key.type);
2252 if (idx == UINT_MAX) {
2254 vsctl_fatal("no key \"%s\" in %s record \"%s\" column %s",
2255 key_string, table->class->name, record_id,
2259 ovsdb_atom_to_string(&datum->values[idx],
2260 column->type.value.type, out);
2262 ovsdb_atom_destroy(&key, column->type.key.type);
2264 ovsdb_datum_to_string(datum, &column->type, out);
2266 ds_put_char(out, '\n');
2273 list_record(const struct vsctl_table_class *table,
2274 const struct ovsdb_idl_row *row, struct ds *out)
2278 ds_put_format(out, "%-20s: "UUID_FMT"\n", "_uuid",
2279 UUID_ARGS(&row->uuid));
2280 for (i = 0; i < table->class->n_columns; i++) {
2281 const struct ovsdb_idl_column *column = &table->class->columns[i];
2282 const struct ovsdb_datum *datum;
2284 datum = ovsdb_idl_read(row, column);
2286 ds_put_format(out, "%-20s: ", column->name);
2287 ovsdb_datum_to_string(datum, &column->type, out);
2288 ds_put_char(out, '\n');
2293 cmd_list(struct vsctl_context *ctx)
2295 const char *table_name = ctx->argv[1];
2296 const struct vsctl_table_class *table;
2297 struct ds *out = &ctx->output;
2300 table = get_table(table_name);
2301 if (ctx->argc > 2) {
2302 for (i = 2; i < ctx->argc; i++) {
2304 ds_put_char(out, '\n');
2306 list_record(table, must_get_row(ctx, table, ctx->argv[i]), out);
2309 const struct ovsdb_idl_row *row;
2312 for (row = ovsdb_idl_first_row(ctx->idl, table->class), first = true;
2314 row = ovsdb_idl_next_row(row), first = false) {
2316 ds_put_char(out, '\n');
2318 list_record(table, row, out);
2324 set_column(const struct vsctl_table_class *table,
2325 const struct ovsdb_idl_row *row, const char *arg,
2326 struct ovsdb_symbol_table *symtab)
2328 const struct ovsdb_idl_column *column;
2329 char *key_string, *value_string;
2332 error = parse_column_key_value(arg, table, &column, &key_string,
2333 NULL, NULL, 0, &value_string);
2334 die_if_error(error);
2335 if (!value_string) {
2336 vsctl_fatal("%s: missing value", arg);
2340 union ovsdb_atom key, value;
2341 struct ovsdb_datum datum;
2343 if (column->type.value.type == OVSDB_TYPE_VOID) {
2344 vsctl_fatal("cannot specify key to set for non-map column %s",
2348 die_if_error(ovsdb_atom_from_string(&key, &column->type.key,
2349 key_string, symtab));
2350 die_if_error(ovsdb_atom_from_string(&value, &column->type.value,
2351 value_string, symtab));
2353 ovsdb_datum_init_empty(&datum);
2354 ovsdb_datum_add_unsafe(&datum, &key, &value, &column->type);
2356 ovsdb_atom_destroy(&key, column->type.key.type);
2357 ovsdb_atom_destroy(&value, column->type.value.type);
2359 ovsdb_datum_union(&datum, ovsdb_idl_read(row, column),
2360 &column->type, false);
2361 ovsdb_idl_txn_write(row, column, &datum);
2363 struct ovsdb_datum datum;
2365 die_if_error(ovsdb_datum_from_string(&datum, &column->type,
2366 value_string, symtab));
2367 ovsdb_idl_txn_write(row, column, &datum);
2375 cmd_set(struct vsctl_context *ctx)
2377 const char *table_name = ctx->argv[1];
2378 const char *record_id = ctx->argv[2];
2379 const struct vsctl_table_class *table;
2380 const struct ovsdb_idl_row *row;
2383 table = get_table(table_name);
2384 row = must_get_row(ctx, table, record_id);
2385 for (i = 3; i < ctx->argc; i++) {
2386 set_column(table, row, ctx->argv[i], ctx->symtab);
2391 cmd_add(struct vsctl_context *ctx)
2393 const char *table_name = ctx->argv[1];
2394 const char *record_id = ctx->argv[2];
2395 const char *column_name = ctx->argv[3];
2396 const struct vsctl_table_class *table;
2397 const struct ovsdb_idl_column *column;
2398 const struct ovsdb_idl_row *row;
2399 const struct ovsdb_type *type;
2400 struct ovsdb_datum old;
2403 table = get_table(table_name);
2404 row = must_get_row(ctx, table, record_id);
2405 die_if_error(get_column(table, column_name, &column));
2407 type = &column->type;
2408 ovsdb_datum_clone(&old, ovsdb_idl_read(row, column), &column->type);
2409 for (i = 4; i < ctx->argc; i++) {
2410 struct ovsdb_type add_type;
2411 struct ovsdb_datum add;
2415 add_type.n_max = UINT_MAX;
2416 die_if_error(ovsdb_datum_from_string(&add, &add_type, ctx->argv[i],
2418 ovsdb_datum_union(&old, &add, type, false);
2419 ovsdb_datum_destroy(&add, type);
2421 if (old.n > type->n_max) {
2422 vsctl_fatal("\"add\" operation would put %u %s in column %s of "
2423 "table %s but the maximum number is %u",
2425 type->value.type == OVSDB_TYPE_VOID ? "values" : "pairs",
2426 column->name, table->class->name, type->n_max);
2428 ovsdb_idl_txn_verify(row, column);
2429 ovsdb_idl_txn_write(row, column, &old);
2433 cmd_remove(struct vsctl_context *ctx)
2435 const char *table_name = ctx->argv[1];
2436 const char *record_id = ctx->argv[2];
2437 const char *column_name = ctx->argv[3];
2438 const struct vsctl_table_class *table;
2439 const struct ovsdb_idl_column *column;
2440 const struct ovsdb_idl_row *row;
2441 const struct ovsdb_type *type;
2442 struct ovsdb_datum old;
2445 table = get_table(table_name);
2446 row = must_get_row(ctx, table, record_id);
2447 die_if_error(get_column(table, column_name, &column));
2449 type = &column->type;
2450 ovsdb_datum_clone(&old, ovsdb_idl_read(row, column), &column->type);
2451 for (i = 4; i < ctx->argc; i++) {
2452 struct ovsdb_type rm_type;
2453 struct ovsdb_datum rm;
2458 rm_type.n_max = UINT_MAX;
2459 error = ovsdb_datum_from_string(&rm, &rm_type,
2460 ctx->argv[i], ctx->symtab);
2461 if (error && ovsdb_type_is_map(&rm_type)) {
2463 rm_type.value.type = OVSDB_TYPE_VOID;
2464 die_if_error(ovsdb_datum_from_string(&rm, &rm_type,
2465 ctx->argv[i], ctx->symtab));
2467 ovsdb_datum_subtract(&old, type, &rm, &rm_type);
2468 ovsdb_datum_destroy(&rm, &rm_type);
2470 if (old.n < type->n_min) {
2471 vsctl_fatal("\"remove\" operation would put %u %s in column %s of "
2472 "table %s but the minimum number is %u",
2474 type->value.type == OVSDB_TYPE_VOID ? "values" : "pairs",
2475 column->name, table->class->name, type->n_min);
2477 ovsdb_idl_txn_verify(row, column);
2478 ovsdb_idl_txn_write(row, column, &old);
2482 cmd_clear(struct vsctl_context *ctx)
2484 const char *table_name = ctx->argv[1];
2485 const char *record_id = ctx->argv[2];
2486 const struct vsctl_table_class *table;
2487 const struct ovsdb_idl_row *row;
2490 table = get_table(table_name);
2491 row = must_get_row(ctx, table, record_id);
2492 for (i = 3; i < ctx->argc; i++) {
2493 const struct ovsdb_idl_column *column;
2494 const struct ovsdb_type *type;
2495 struct ovsdb_datum datum;
2497 die_if_error(get_column(table, ctx->argv[i], &column));
2499 type = &column->type;
2500 if (type->n_min > 0) {
2501 vsctl_fatal("\"clear\" operation cannot be applied to column %s "
2502 "of table %s, which is not allowed to be empty",
2503 column->name, table->class->name);
2506 ovsdb_datum_init_empty(&datum);
2507 ovsdb_idl_txn_write(row, column, &datum);
2512 cmd_create(struct vsctl_context *ctx)
2514 const char *id = shash_find_data(&ctx->options, "--id");
2515 const char *table_name = ctx->argv[1];
2516 const struct vsctl_table_class *table;
2517 const struct ovsdb_idl_row *row;
2518 const struct uuid *uuid;
2522 struct ovsdb_symbol *symbol;
2525 vsctl_fatal("row id \"%s\" does not begin with \"@\"", id);
2528 symbol = ovsdb_symbol_table_insert(ctx->symtab, id);
2530 vsctl_fatal("row id \"%s\" may only be used to insert a single "
2533 symbol->used = true;
2535 uuid = &symbol->uuid;
2540 table = get_table(table_name);
2541 row = ovsdb_idl_txn_insert(ctx->txn, table->class, uuid);
2542 for (i = 2; i < ctx->argc; i++) {
2543 set_column(table, row, ctx->argv[i], ctx->symtab);
2545 ds_put_format(&ctx->output, UUID_FMT, UUID_ARGS(&row->uuid));
2548 /* This function may be used as the 'postprocess' function for commands that
2549 * insert new rows into the database. It expects that the command's 'run'
2550 * function prints the UUID reported by ovsdb_idl_txn_insert() as the command's
2551 * sole output. It replaces that output by the row's permanent UUID assigned
2552 * by the database server and appends a new-line.
2554 * Currently we use this only for "create", because the higher-level commands
2555 * are supposed to be independent of the actual structure of the vswitch
2558 post_create(struct vsctl_context *ctx)
2560 const struct uuid *real;
2563 uuid_from_string(&dummy, ds_cstr(&ctx->output));
2564 real = ovsdb_idl_txn_get_insert_uuid(ctx->txn, &dummy);
2566 ds_clear(&ctx->output);
2567 ds_put_format(&ctx->output, UUID_FMT, UUID_ARGS(real));
2569 ds_put_char(&ctx->output, '\n');
2573 cmd_destroy(struct vsctl_context *ctx)
2575 bool must_exist = !shash_find(&ctx->options, "--if-exists");
2576 const char *table_name = ctx->argv[1];
2577 const struct vsctl_table_class *table;
2580 table = get_table(table_name);
2581 for (i = 2; i < ctx->argc; i++) {
2582 const struct ovsdb_idl_row *row;
2584 row = (must_exist ? must_get_row : get_row)(ctx, table, ctx->argv[i]);
2586 ovsdb_idl_txn_delete(row);
2592 is_condition_satified(const struct vsctl_table_class *table,
2593 const struct ovsdb_idl_row *row, const char *arg,
2594 struct ovsdb_symbol_table *symtab)
2596 static const char *operators[] = {
2597 "=", "!=", "<", ">", "<=", ">="
2600 const struct ovsdb_idl_column *column;
2601 const struct ovsdb_datum *have_datum;
2602 char *key_string, *value_string;
2603 const char *operator;
2608 error = parse_column_key_value(arg, table, &column, &key_string,
2609 &operator, operators, ARRAY_SIZE(operators),
2611 die_if_error(error);
2612 if (!value_string) {
2613 vsctl_fatal("%s: missing value", arg);
2616 have_datum = ovsdb_idl_read(row, column);
2618 union ovsdb_atom want_key, want_value;
2620 if (column->type.value.type == OVSDB_TYPE_VOID) {
2621 vsctl_fatal("cannot specify key to check for non-map column %s",
2625 die_if_error(ovsdb_atom_from_string(&want_key, &column->type.key,
2626 key_string, symtab));
2627 die_if_error(ovsdb_atom_from_string(&want_value, &column->type.value,
2628 value_string, symtab));
2630 idx = ovsdb_datum_find_key(have_datum,
2631 &want_key, column->type.key.type);
2632 if (idx != UINT_MAX) {
2633 cmp = ovsdb_atom_compare_3way(&have_datum->values[idx],
2635 column->type.value.type);
2638 ovsdb_atom_destroy(&want_key, column->type.key.type);
2639 ovsdb_atom_destroy(&want_value, column->type.value.type);
2641 struct ovsdb_datum want_datum;
2643 die_if_error(ovsdb_datum_from_string(&want_datum, &column->type,
2644 value_string, symtab));
2646 cmp = ovsdb_datum_compare_3way(have_datum, &want_datum,
2648 ovsdb_datum_destroy(&want_datum, &column->type);
2654 return (idx == UINT_MAX ? false
2655 : !strcmp(operator, "=") ? cmp == 0
2656 : !strcmp(operator, "!=") ? cmp != 0
2657 : !strcmp(operator, "<") ? cmp < 0
2658 : !strcmp(operator, ">") ? cmp > 0
2659 : !strcmp(operator, "<=") ? cmp <= 0
2660 : !strcmp(operator, ">=") ? cmp >= 0
2665 cmd_wait_until(struct vsctl_context *ctx)
2667 const char *table_name = ctx->argv[1];
2668 const char *record_id = ctx->argv[2];
2669 const struct vsctl_table_class *table;
2670 const struct ovsdb_idl_row *row;
2673 table = get_table(table_name);
2675 row = get_row(ctx, table, record_id);
2677 ctx->try_again = true;
2681 for (i = 3; i < ctx->argc; i++) {
2682 if (!is_condition_satified(table, row, ctx->argv[i], ctx->symtab)) {
2683 ctx->try_again = true;
2689 static struct json *
2690 where_uuid_equals(const struct uuid *uuid)
2693 json_array_create_1(
2694 json_array_create_3(
2695 json_string_create("_uuid"),
2696 json_string_create("=="),
2697 json_array_create_2(
2698 json_string_create("uuid"),
2699 json_string_create_nocopy(
2700 xasprintf(UUID_FMT, UUID_ARGS(uuid))))));
2704 vsctl_context_init(struct vsctl_context *ctx, struct vsctl_command *command,
2705 struct ovsdb_idl *idl, struct ovsdb_idl_txn *txn,
2706 const struct ovsrec_open_vswitch *ovs,
2707 struct ovsdb_symbol_table *symtab)
2709 ctx->argc = command->argc;
2710 ctx->argv = command->argv;
2711 ctx->options = command->options;
2713 ds_swap(&ctx->output, &command->output);
2717 ctx->symtab = symtab;
2718 ctx->verified_ports = false;
2720 ctx->try_again = false;
2724 vsctl_context_done(struct vsctl_context *ctx, struct vsctl_command *command)
2726 ds_swap(&ctx->output, &command->output);
2730 do_vsctl(const char *args, struct vsctl_command *commands, size_t n_commands,
2731 struct ovsdb_idl *idl)
2733 struct ovsdb_idl_txn *txn;
2734 const struct ovsrec_open_vswitch *ovs;
2735 enum ovsdb_idl_txn_status status;
2736 struct ovsdb_symbol_table *symtab;
2738 struct vsctl_command *c;
2739 int64_t next_cfg = 0;
2742 txn = the_idl_txn = ovsdb_idl_txn_create(idl);
2744 ovsdb_idl_txn_set_dry_run(txn);
2747 ovsdb_idl_txn_add_comment(txn, "ovs-vsctl: %s", args);
2749 ovs = ovsrec_open_vswitch_first(idl);
2751 /* XXX add verification that table is empty */
2752 ovs = ovsrec_open_vswitch_insert(txn);
2755 if (wait_for_reload) {
2756 struct json *where = where_uuid_equals(&ovs->header_.uuid);
2757 ovsdb_idl_txn_increment(txn, "Open_vSwitch", "next_cfg", where);
2758 json_destroy(where);
2761 symtab = ovsdb_symbol_table_create();
2762 for (c = commands; c < &commands[n_commands]; c++) {
2763 ds_init(&c->output);
2765 for (c = commands; c < &commands[n_commands]; c++) {
2766 struct vsctl_context ctx;
2768 vsctl_context_init(&ctx, c, idl, txn, ovs, symtab);
2769 (c->syntax->run)(&ctx);
2770 vsctl_context_done(&ctx, c);
2772 if (ctx.try_again) {
2777 status = ovsdb_idl_txn_commit_block(txn);
2778 if (wait_for_reload && status == TXN_SUCCESS) {
2779 next_cfg = ovsdb_idl_txn_get_increment_new_value(txn);
2781 if (status == TXN_UNCHANGED || status == TXN_SUCCESS) {
2782 for (c = commands; c < &commands[n_commands]; c++) {
2783 if (c->syntax->postprocess) {
2784 struct vsctl_context ctx;
2786 vsctl_context_init(&ctx, c, idl, txn, ovs, symtab);
2787 (c->syntax->postprocess)(&ctx);
2788 vsctl_context_done(&ctx, c);
2792 error = xstrdup(ovsdb_idl_txn_get_error(txn));
2793 ovsdb_idl_txn_destroy(txn);
2794 txn = the_idl_txn = NULL;
2796 unused = ovsdb_symbol_table_find_unused(symtab);
2798 vsctl_fatal("row id \"%s\" is referenced but never created (e.g. "
2799 "with \"-- --id=%s create ...\")", unused, unused);
2803 case TXN_INCOMPLETE:
2807 /* Should not happen--we never call ovsdb_idl_txn_abort(). */
2808 vsctl_fatal("transaction aborted");
2818 vsctl_fatal("transaction error: %s", error);
2825 ovsdb_symbol_table_destroy(symtab);
2827 for (c = commands; c < &commands[n_commands]; c++) {
2828 struct ds *ds = &c->output;
2829 struct shash_node *node;
2835 for (j = 0; j < ds->length; j++) {
2836 int c = ds->string[j];
2839 fputs("\\n", stdout);
2843 fputs("\\\\", stdout);
2852 fputs(ds_cstr(ds), stdout);
2854 ds_destroy(&c->output);
2856 SHASH_FOR_EACH (node, &c->options) {
2859 shash_destroy(&c->options);
2863 if (wait_for_reload && status != TXN_UNCHANGED) {
2865 const struct ovsrec_open_vswitch *ovs;
2868 OVSREC_OPEN_VSWITCH_FOR_EACH (ovs, idl) {
2869 if (ovs->cur_cfg >= next_cfg) {
2873 ovsdb_idl_wait(idl);
2878 ovsdb_idl_destroy(idl);
2883 /* Our transaction needs to be rerun, or a prerequisite was not met. Free
2884 * resources and return so that the caller can try again. */
2886 ovsdb_idl_txn_abort(txn);
2887 ovsdb_idl_txn_destroy(txn);
2889 ovsdb_symbol_table_destroy(symtab);
2890 for (c = commands; c < &commands[n_commands]; c++) {
2891 ds_destroy(&c->output);
2896 static const struct vsctl_command_syntax all_commands[] = {
2897 /* Open vSwitch commands. */
2898 {"init", 0, 0, cmd_init, NULL, ""},
2900 /* Bridge commands. */
2901 {"add-br", 1, 3, cmd_add_br, NULL, "--may-exist"},
2902 {"del-br", 1, 1, cmd_del_br, NULL, "--if-exists"},
2903 {"list-br", 0, 0, cmd_list_br, NULL, ""},
2904 {"br-exists", 1, 1, cmd_br_exists, NULL, ""},
2905 {"br-to-vlan", 1, 1, cmd_br_to_vlan, NULL, ""},
2906 {"br-to-parent", 1, 1, cmd_br_to_parent, NULL, ""},
2907 {"br-set-external-id", 2, 3, cmd_br_set_external_id, NULL, ""},
2908 {"br-get-external-id", 1, 2, cmd_br_get_external_id, NULL, ""},
2910 /* Port commands. */
2911 {"list-ports", 1, 1, cmd_list_ports, NULL, ""},
2912 {"add-port", 2, INT_MAX, cmd_add_port, NULL, "--may-exist"},
2913 {"add-bond", 4, INT_MAX, cmd_add_bond, NULL, "--may-exist,--fake-iface"},
2914 {"del-port", 1, 2, cmd_del_port, NULL, "--if-exists,--with-iface"},
2915 {"port-to-br", 1, 1, cmd_port_to_br, NULL, ""},
2917 /* Interface commands. */
2918 {"list-ifaces", 1, 1, cmd_list_ifaces, NULL, ""},
2919 {"iface-to-br", 1, 1, cmd_iface_to_br, NULL, ""},
2921 /* Controller commands. */
2922 {"get-controller", 1, 1, cmd_get_controller, NULL, ""},
2923 {"del-controller", 1, 1, cmd_del_controller, NULL, ""},
2924 {"set-controller", 1, INT_MAX, cmd_set_controller, NULL, ""},
2925 {"get-fail-mode", 1, 1, cmd_get_fail_mode, NULL, ""},
2926 {"del-fail-mode", 1, 1, cmd_del_fail_mode, NULL, ""},
2927 {"set-fail-mode", 2, 2, cmd_set_fail_mode, NULL, ""},
2930 {"get-ssl", 0, 0, cmd_get_ssl, NULL, ""},
2931 {"del-ssl", 0, 0, cmd_del_ssl, NULL, ""},
2932 {"set-ssl", 3, 3, cmd_set_ssl, NULL, "--bootstrap"},
2934 /* Switch commands. */
2935 {"emer-reset", 0, 0, cmd_emer_reset, NULL, ""},
2937 /* Parameter commands. */
2938 {"get", 3, INT_MAX, cmd_get, NULL, "--if-exists"},
2939 {"list", 1, INT_MAX, cmd_list, NULL, ""},
2940 {"set", 3, INT_MAX, cmd_set, NULL, ""},
2941 {"add", 4, INT_MAX, cmd_add, NULL, ""},
2942 {"remove", 4, INT_MAX, cmd_remove, NULL, ""},
2943 {"clear", 3, INT_MAX, cmd_clear, NULL, ""},
2944 {"create", 2, INT_MAX, cmd_create, post_create, "--id="},
2945 {"destroy", 1, INT_MAX, cmd_destroy, NULL, "--if-exists"},
2946 {"wait-until", 2, INT_MAX, cmd_wait_until, NULL, ""},
2948 {NULL, 0, 0, NULL, NULL, NULL},