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'. */
88 static int timeout = 5;
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 struct ovsdb_idl *idl;
122 struct vsctl_command *commands;
126 set_program_name(argv[0]);
127 signal(SIGPIPE, SIG_IGN);
128 vlog_set_levels(VLM_ANY_MODULE, VLF_CONSOLE, VLL_WARN);
129 vlog_set_levels(VLM_reconnect, VLF_ANY_FACILITY, VLL_WARN);
132 /* Log our arguments. This is often valuable for debugging systems. */
133 args = process_escape_args(argv);
134 VLOG_INFO("Called as %s", args);
136 /* Parse command line. */
137 parse_options(argc, argv);
138 commands = parse_commands(argc - optind, argv + optind, &n_commands);
144 /* Now execute the commands. */
145 idl = the_idl = ovsdb_idl_create(db, &ovsrec_idl_class);
147 if (ovsdb_idl_run(idl)) {
148 do_vsctl(args, commands, n_commands, idl);
157 parse_options(int argc, char *argv[])
160 OPT_DB = UCHAR_MAX + 1,
168 static struct option long_options[] = {
169 {"db", required_argument, 0, OPT_DB},
170 {"no-syslog", no_argument, 0, OPT_NO_SYSLOG},
171 {"no-wait", no_argument, 0, OPT_NO_WAIT},
172 {"dry-run", no_argument, 0, OPT_DRY_RUN},
173 {"oneline", no_argument, 0, OPT_ONELINE},
174 {"timeout", required_argument, 0, 't'},
175 {"help", no_argument, 0, 'h'},
176 {"version", no_argument, 0, 'V'},
179 STREAM_SSL_LONG_OPTIONS
180 {"peer-ca-cert", required_argument, 0, OPT_PEER_CA_CERT},
189 c = getopt_long(argc, argv, "+v::hVt:", long_options, NULL);
204 vlog_set_levels(VLM_vsctl, VLF_SYSLOG, VLL_WARN);
208 wait_for_reload = false;
219 OVS_PRINT_VERSION(0, 0);
223 timeout = strtoul(optarg, NULL, 10);
225 vsctl_fatal("value %s on -t or --timeout is invalid",
233 STREAM_SSL_OPTION_HANDLERS
235 case OPT_PEER_CA_CERT:
236 stream_ssl_set_peer_ca_cert_file(optarg);
253 static struct vsctl_command *
254 parse_commands(int argc, char *argv[], size_t *n_commandsp)
256 struct vsctl_command *commands;
257 size_t n_commands, allocated_commands;
261 n_commands = allocated_commands = 0;
263 for (start = i = 0; i <= argc; i++) {
264 if (i == argc || !strcmp(argv[i], "--")) {
266 if (n_commands >= allocated_commands) {
267 struct vsctl_command *c;
269 commands = x2nrealloc(commands, &allocated_commands,
271 for (c = commands; c < &commands[n_commands]; c++) {
272 shash_moved(&c->options);
275 parse_command(i - start, &argv[start],
276 &commands[n_commands++]);
282 vsctl_fatal("missing command name (use --help for help)");
284 *n_commandsp = n_commands;
289 parse_command(int argc, char *argv[], struct vsctl_command *command)
291 const struct vsctl_command_syntax *p;
294 shash_init(&command->options);
295 for (i = 0; i < argc; i++) {
296 const char *option = argv[i];
300 if (option[0] != '-') {
304 equals = strchr(option, '=');
306 key = xmemdup0(option, equals - option);
307 value = xstrdup(equals + 1);
309 key = xstrdup(option);
313 if (shash_find(&command->options, key)) {
314 vsctl_fatal("'%s' option specified multiple times", argv[i]);
316 shash_add_nocopy(&command->options, key, value);
319 vsctl_fatal("missing command name");
322 for (p = all_commands; p->name; p++) {
323 if (!strcmp(p->name, argv[i])) {
324 struct shash_node *node;
327 SHASH_FOR_EACH (node, &command->options) {
328 const char *s = strstr(p->options, node->name);
329 int end = s ? s[strlen(node->name)] : EOF;
331 if (end != '=' && end != ',' && end != ' ' && end != '\0') {
332 vsctl_fatal("'%s' command has no '%s' option",
333 argv[i], node->name);
335 if ((end == '=') != (node->data != NULL)) {
337 vsctl_fatal("missing argument to '%s' option on '%s' "
338 "command", node->name, argv[i]);
340 vsctl_fatal("'%s' option on '%s' does not accept an "
341 "argument", node->name, argv[i]);
346 n_arg = argc - i - 1;
347 if (n_arg < p->min_args) {
348 vsctl_fatal("'%s' command requires at least %d arguments",
349 p->name, p->min_args);
350 } else if (n_arg > p->max_args) {
353 for (j = i + 1; j < argc; j++) {
354 if (argv[j][0] == '-') {
355 vsctl_fatal("'%s' command takes at most %d arguments "
356 "(note that options must precede command "
357 "names and follow a \"--\" argument)",
358 p->name, p->max_args);
362 vsctl_fatal("'%s' command takes at most %d arguments",
363 p->name, p->max_args);
366 command->argc = n_arg + 1;
367 command->argv = &argv[i];
373 vsctl_fatal("unknown command '%s'; use --help for help", argv[i]);
377 vsctl_fatal(const char *format, ...)
382 va_start(args, format);
383 message = xvasprintf(format, args);
386 vlog_set_levels(VLM_vsctl, VLF_CONSOLE, VLL_EMER);
387 VLOG_ERR("%s", message);
388 ovs_error(0, "%s", message);
389 vsctl_exit(EXIT_FAILURE);
392 /* Frees the current transaction and the underlying IDL and then calls
395 * Freeing the transaction and the IDL is not strictly necessary, but it makes
396 * for a clean memory leak report from valgrind in the normal case. That makes
397 * it easier to notice real memory leaks. */
399 vsctl_exit(int status)
402 ovsdb_idl_txn_abort(the_idl_txn);
403 ovsdb_idl_txn_destroy(the_idl_txn);
405 ovsdb_idl_destroy(the_idl);
413 %s: ovs-vswitchd management utility\n\
414 usage: %s [OPTIONS] COMMAND [ARG...]\n\
417 add-br BRIDGE create a new bridge named BRIDGE\n\
418 add-br BRIDGE PARENT VLAN create new fake BRIDGE in PARENT on VLAN\n\
419 del-br BRIDGE delete BRIDGE and all of its ports\n\
420 list-br print the names of all the bridges\n\
421 br-exists BRIDGE test whether BRIDGE exists\n\
422 br-to-vlan BRIDGE print the VLAN which BRIDGE is on\n\
423 br-to-parent BRIDGE print the parent of BRIDGE\n\
424 br-set-external-id BRIDGE KEY VALUE set KEY on BRIDGE to VALUE\n\
425 br-set-external-id BRIDGE KEY unset KEY on BRIDGE\n\
426 br-get-external-id BRIDGE KEY print value of KEY on BRIDGE\n\
427 br-get-external-id BRIDGE list key-value pairs on BRIDGE\n\
430 list-ports BRIDGE print the names of all the ports on BRIDGE\n\
431 add-port BRIDGE PORT add network device PORT to BRIDGE\n\
432 add-bond BRIDGE PORT IFACE... add bonded port PORT in BRIDGE from IFACES\n\
433 del-port [BRIDGE] PORT delete PORT (which may be bonded) from BRIDGE\n\
434 port-to-br PORT print name of bridge that contains PORT\n\
435 A bond is considered to be a single port.\n\
437 Interface commands (a bond consists of multiple interfaces):\n\
438 list-ifaces BRIDGE print the names of all interfaces on BRIDGE\n\
439 iface-to-br IFACE print name of bridge that contains IFACE\n\
441 Controller commands:\n\
442 get-controller [BRIDGE] print the controller for BRIDGE\n\
443 del-controller [BRIDGE] delete the controller for BRIDGE\n\
444 set-controller [BRIDGE] TARGET set the controller for BRIDGE to TARGET\n\
445 get-fail-mode [BRIDGE] print the fail-mode for BRIDGE\n\
446 del-fail-mode [BRIDGE] delete the fail-mode for BRIDGE\n\
447 set-fail-mode [BRIDGE] MODE set the fail-mode for BRIDGE to MODE\n\
450 get-ssl print the SSL configuration\n\
451 del-ssl delete the SSL configuration\n\
452 set-ssl PRIV-KEY CERT CA-CERT set the SSL configuration\n\
455 emer-reset reset switch to known good state\n\
457 Database commands:\n\
458 list TBL [REC] list RECord (or all records) in TBL\n\
459 get TBL REC COL[:KEY] print values of COLumns in RECord in TBL\n\
460 set TBL REC COL[:KEY]=VALUE set COLumn values in RECord in TBL\n\
461 add TBL REC COL [KEY=]VALUE add (KEY=)VALUE to COLumn in RECord in TBL\n\
462 remove TBL REC COL [KEY=]VALUE remove (KEY=)VALUE from COLumn\n\
463 clear TBL REC COL clear values from COLumn in RECord in TBL\n\
464 create TBL COL[:KEY]=VALUE create and initialize new record\n\
465 destroy TBL REC delete RECord from TBL\n\
466 wait-until TBL REC [COL[:KEY]=VALUE] wait until condition is true\n\
467 Potentially unsafe database commands require --force option.\n\
470 --db=DATABASE connect to DATABASE\n\
472 --oneline print exactly one line of output per command\n",
473 program_name, program_name, default_db());
477 -h, --help display this help message\n\
478 -V, --version display version information\n");
487 def = xasprintf("unix:%s/db.sock", ovs_rundir);
492 struct vsctl_context {
496 struct shash options;
498 /* Modifiable state. */
500 struct ovsdb_idl *idl;
501 struct ovsdb_idl_txn *txn;
502 struct ovsdb_symbol_table *symtab;
503 const struct ovsrec_open_vswitch *ovs;
505 /* A command may set this member to true if some prerequisite is not met
506 * and the caller should wait for something to change and then retry. */
510 struct vsctl_bridge {
511 struct ovsrec_bridge *br_cfg;
513 struct ovsrec_controller **ctrl;
515 struct vsctl_bridge *parent;
520 struct ovsrec_port *port_cfg;
521 struct vsctl_bridge *bridge;
525 struct ovsrec_interface *iface_cfg;
526 struct vsctl_port *port;
530 struct shash bridges;
533 struct ovsrec_controller **ctrl;
538 vsctl_context_to_string(const struct vsctl_context *ctx)
540 const struct shash_node *node;
546 SHASH_FOR_EACH (node, &ctx->options) {
547 svec_add(&words, node->name);
549 for (i = 0; i < ctx->argc; i++) {
550 svec_add(&words, ctx->argv[i]);
552 svec_terminate(&words);
554 s = process_escape_args(words.names);
556 svec_destroy(&words);
561 static struct vsctl_bridge *
562 add_bridge(struct vsctl_info *b,
563 struct ovsrec_bridge *br_cfg, const char *name,
564 struct vsctl_bridge *parent, int vlan)
566 struct vsctl_bridge *br = xmalloc(sizeof *br);
568 br->name = xstrdup(name);
572 br->ctrl = parent->br_cfg->controller;
573 br->n_ctrl = parent->br_cfg->n_controller;
575 br->ctrl = br_cfg->controller;
576 br->n_ctrl = br_cfg->n_controller;
578 shash_add(&b->bridges, br->name, br);
583 port_is_fake_bridge(const struct ovsrec_port *port_cfg)
585 return (port_cfg->fake_bridge
587 && *port_cfg->tag >= 1 && *port_cfg->tag <= 4095);
590 static struct vsctl_bridge *
591 find_vlan_bridge(struct vsctl_info *info,
592 struct vsctl_bridge *parent, int vlan)
594 struct shash_node *node;
596 SHASH_FOR_EACH (node, &info->bridges) {
597 struct vsctl_bridge *br = node->data;
598 if (br->parent == parent && br->vlan == vlan) {
607 free_info(struct vsctl_info *info)
609 struct shash_node *node;
611 SHASH_FOR_EACH (node, &info->bridges) {
612 struct vsctl_bridge *bridge = node->data;
616 shash_destroy(&info->bridges);
618 shash_destroy_free_data(&info->ports);
619 shash_destroy_free_data(&info->ifaces);
623 get_info(const struct ovsrec_open_vswitch *ovs, struct vsctl_info *info)
625 struct shash bridges, ports;
628 shash_init(&info->bridges);
629 shash_init(&info->ports);
630 shash_init(&info->ifaces);
632 info->ctrl = ovs->controller;
633 info->n_ctrl = ovs->n_controller;
635 shash_init(&bridges);
637 for (i = 0; i < ovs->n_bridges; i++) {
638 struct ovsrec_bridge *br_cfg = ovs->bridges[i];
639 struct vsctl_bridge *br;
642 if (!shash_add_once(&bridges, br_cfg->name, NULL)) {
643 VLOG_WARN("%s: database contains duplicate bridge name",
647 br = add_bridge(info, br_cfg, br_cfg->name, NULL, 0);
652 for (j = 0; j < br_cfg->n_ports; j++) {
653 struct ovsrec_port *port_cfg = br_cfg->ports[j];
655 if (!shash_add_once(&ports, port_cfg->name, NULL)) {
656 VLOG_WARN("%s: database contains duplicate port name",
661 if (port_is_fake_bridge(port_cfg)
662 && shash_add_once(&bridges, port_cfg->name, NULL)) {
663 add_bridge(info, NULL, port_cfg->name, br, *port_cfg->tag);
667 shash_destroy(&bridges);
668 shash_destroy(&ports);
670 shash_init(&bridges);
672 for (i = 0; i < ovs->n_bridges; i++) {
673 struct ovsrec_bridge *br_cfg = ovs->bridges[i];
674 struct vsctl_bridge *br;
677 if (!shash_add_once(&bridges, br_cfg->name, NULL)) {
680 br = shash_find_data(&info->bridges, br_cfg->name);
681 for (j = 0; j < br_cfg->n_ports; j++) {
682 struct ovsrec_port *port_cfg = br_cfg->ports[j];
683 struct vsctl_port *port;
686 if (!shash_add_once(&ports, port_cfg->name, NULL)) {
690 if (port_is_fake_bridge(port_cfg)
691 && !shash_add_once(&bridges, port_cfg->name, NULL)) {
695 port = xmalloc(sizeof *port);
696 port->port_cfg = port_cfg;
698 && *port_cfg->tag >= 1 && *port_cfg->tag <= 4095) {
699 port->bridge = find_vlan_bridge(info, br, *port_cfg->tag);
706 shash_add(&info->ports, port_cfg->name, port);
708 for (k = 0; k < port_cfg->n_interfaces; k++) {
709 struct ovsrec_interface *iface_cfg = port_cfg->interfaces[k];
710 struct vsctl_iface *iface;
712 if (shash_find(&info->ifaces, iface_cfg->name)) {
713 VLOG_WARN("%s: database contains duplicate interface name",
718 iface = xmalloc(sizeof *iface);
719 iface->iface_cfg = iface_cfg;
721 shash_add(&info->ifaces, iface_cfg->name, iface);
725 shash_destroy(&bridges);
726 shash_destroy(&ports);
730 check_conflicts(struct vsctl_info *info, const char *name,
733 struct vsctl_iface *iface;
734 struct vsctl_port *port;
736 if (shash_find(&info->bridges, name)) {
737 vsctl_fatal("%s because a bridge named %s already exists",
741 port = shash_find_data(&info->ports, name);
743 vsctl_fatal("%s because a port named %s already exists on "
744 "bridge %s", msg, name, port->bridge->name);
747 iface = shash_find_data(&info->ifaces, name);
749 vsctl_fatal("%s because an interface named %s already exists "
750 "on bridge %s", msg, name, iface->port->bridge->name);
756 static struct vsctl_bridge *
757 find_bridge(struct vsctl_info *info, const char *name, bool must_exist)
759 struct vsctl_bridge *br = shash_find_data(&info->bridges, name);
760 if (must_exist && !br) {
761 vsctl_fatal("no bridge named %s", name);
766 static struct vsctl_bridge *
767 find_real_bridge(struct vsctl_info *info, const char *name, bool must_exist)
769 struct vsctl_bridge *br = find_bridge(info, name, must_exist);
770 if (br && br->parent) {
771 vsctl_fatal("%s is a fake bridge", name);
776 static struct vsctl_port *
777 find_port(struct vsctl_info *info, const char *name, bool must_exist)
779 struct vsctl_port *port = shash_find_data(&info->ports, name);
780 if (port && !strcmp(name, port->bridge->name)) {
783 if (must_exist && !port) {
784 vsctl_fatal("no port named %s", name);
789 static struct vsctl_iface *
790 find_iface(struct vsctl_info *info, const char *name, bool must_exist)
792 struct vsctl_iface *iface = shash_find_data(&info->ifaces, name);
793 if (iface && !strcmp(name, iface->port->bridge->name)) {
796 if (must_exist && !iface) {
797 vsctl_fatal("no interface named %s", name);
803 bridge_insert_port(struct ovsrec_bridge *br, struct ovsrec_port *port)
805 struct ovsrec_port **ports;
808 ports = xmalloc(sizeof *br->ports * (br->n_ports + 1));
809 for (i = 0; i < br->n_ports; i++) {
810 ports[i] = br->ports[i];
812 ports[br->n_ports] = port;
813 ovsrec_bridge_set_ports(br, ports, br->n_ports + 1);
818 bridge_delete_port(struct ovsrec_bridge *br, struct ovsrec_port *port)
820 struct ovsrec_port **ports;
823 ports = xmalloc(sizeof *br->ports * br->n_ports);
824 for (i = n = 0; i < br->n_ports; i++) {
825 if (br->ports[i] != port) {
826 ports[n++] = br->ports[i];
829 ovsrec_bridge_set_ports(br, ports, n);
834 ovs_insert_bridge(const struct ovsrec_open_vswitch *ovs,
835 struct ovsrec_bridge *bridge)
837 struct ovsrec_bridge **bridges;
840 bridges = xmalloc(sizeof *ovs->bridges * (ovs->n_bridges + 1));
841 for (i = 0; i < ovs->n_bridges; i++) {
842 bridges[i] = ovs->bridges[i];
844 bridges[ovs->n_bridges] = bridge;
845 ovsrec_open_vswitch_set_bridges(ovs, bridges, ovs->n_bridges + 1);
850 ovs_delete_bridge(const struct ovsrec_open_vswitch *ovs,
851 struct ovsrec_bridge *bridge)
853 struct ovsrec_bridge **bridges;
856 bridges = xmalloc(sizeof *ovs->bridges * ovs->n_bridges);
857 for (i = n = 0; i < ovs->n_bridges; i++) {
858 if (ovs->bridges[i] != bridge) {
859 bridges[n++] = ovs->bridges[i];
862 ovsrec_open_vswitch_set_bridges(ovs, bridges, n);
867 cmd_init(struct vsctl_context *ctx OVS_UNUSED)
872 cmd_emer_reset(struct vsctl_context *ctx)
874 const struct ovsdb_idl *idl = ctx->idl;
875 const struct ovsrec_bridge *br;
876 const struct ovsrec_port *port;
877 const struct ovsrec_interface *iface;
878 const struct ovsrec_mirror *mirror, *next_mirror;
879 const struct ovsrec_controller *ctrl, *next_ctrl;
880 const struct ovsrec_netflow *nf, *next_nf;
881 const struct ovsrec_ssl *ssl, *next_ssl;
882 const struct ovsrec_sflow *sflow, *next_sflow;
885 /* Reset the Open_vSwitch table. */
886 ovsrec_open_vswitch_set_managers(ctx->ovs, NULL, 0);
887 ovsrec_open_vswitch_set_controller(ctx->ovs, NULL, 0);
888 ovsrec_open_vswitch_set_ssl(ctx->ovs, NULL);
890 OVSREC_BRIDGE_FOR_EACH (br, idl) {
892 char *hw_key = "hwaddr";
895 ovsrec_bridge_set_controller(br, NULL, 0);
896 ovsrec_bridge_set_mirrors(br, NULL, 0);
897 ovsrec_bridge_set_netflow(br, NULL);
898 ovsrec_bridge_set_sflow(br, NULL);
899 ovsrec_bridge_set_flood_vlans(br, NULL, 0);
901 /* We only want to save the "hwaddr" key from other_config. */
902 for (i=0; i < br->n_other_config; i++) {
903 if (!strcmp(br->key_other_config[i], hw_key)) {
904 hw_val = br->value_other_config[i];
909 char *val = xstrdup(hw_val);
910 ovsrec_bridge_set_other_config(br, &hw_key, &val, 1);
913 ovsrec_bridge_set_other_config(br, NULL, NULL, 0);
917 OVSREC_PORT_FOR_EACH (port, idl) {
918 ovsrec_port_set_other_config(port, NULL, NULL, 0);
921 OVSREC_INTERFACE_FOR_EACH (iface, idl) {
922 /* xxx What do we do about gre/patch devices created by mgr? */
924 ovsrec_interface_set_ingress_policing_rate(iface, 0);
925 ovsrec_interface_set_ingress_policing_burst(iface, 0);
928 OVSREC_MIRROR_FOR_EACH_SAFE (mirror, next_mirror, idl) {
929 ovsrec_mirror_delete(mirror);
932 OVSREC_CONTROLLER_FOR_EACH_SAFE (ctrl, next_ctrl, idl) {
933 ovsrec_controller_delete(ctrl);
936 OVSREC_NETFLOW_FOR_EACH_SAFE (nf, next_nf, idl) {
937 ovsrec_netflow_delete(nf);
940 OVSREC_SSL_FOR_EACH_SAFE (ssl, next_ssl, idl) {
941 ovsrec_ssl_delete(ssl);
944 OVSREC_SFLOW_FOR_EACH_SAFE (sflow, next_sflow, idl) {
945 ovsrec_sflow_delete(sflow);
950 cmd_add_br(struct vsctl_context *ctx)
952 bool may_exist = shash_find(&ctx->options, "--may-exist") != 0;
953 const char *br_name, *parent_name;
954 struct vsctl_info info;
957 br_name = ctx->argv[1];
958 if (ctx->argc == 2) {
961 } else if (ctx->argc == 4) {
962 parent_name = ctx->argv[2];
963 vlan = atoi(ctx->argv[3]);
964 if (vlan < 1 || vlan > 4095) {
965 vsctl_fatal("%s: vlan must be between 1 and 4095", ctx->argv[0]);
968 vsctl_fatal("'%s' command takes exactly 1 or 3 arguments",
972 get_info(ctx->ovs, &info);
974 struct vsctl_bridge *br;
976 br = find_bridge(&info, br_name, false);
980 vsctl_fatal("\"--may-exist add-br %s\" but %s is "
981 "a VLAN bridge for VLAN %d",
982 br_name, br_name, br->vlan);
986 vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
987 "is not a VLAN bridge",
988 br_name, parent_name, vlan, br_name);
989 } else if (strcmp(br->parent->name, parent_name)) {
990 vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
991 "has the wrong parent %s",
992 br_name, parent_name, vlan,
993 br_name, br->parent->name);
994 } else if (br->vlan != vlan) {
995 vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
996 "is a VLAN bridge for the wrong VLAN %d",
997 br_name, parent_name, vlan, br_name, br->vlan);
1003 check_conflicts(&info, br_name,
1004 xasprintf("cannot create a bridge named %s", br_name));
1007 struct ovsrec_port *port;
1008 struct ovsrec_interface *iface;
1009 struct ovsrec_bridge *br;
1011 iface = ovsrec_interface_insert(ctx->txn);
1012 ovsrec_interface_set_name(iface, br_name);
1014 port = ovsrec_port_insert(ctx->txn);
1015 ovsrec_port_set_name(port, br_name);
1016 ovsrec_port_set_interfaces(port, &iface, 1);
1018 br = ovsrec_bridge_insert(ctx->txn);
1019 ovsrec_bridge_set_name(br, br_name);
1020 ovsrec_bridge_set_ports(br, &port, 1);
1022 ovs_insert_bridge(ctx->ovs, br);
1024 struct vsctl_bridge *parent;
1025 struct ovsrec_port *port;
1026 struct ovsrec_interface *iface;
1027 struct ovsrec_bridge *br;
1030 parent = find_bridge(&info, parent_name, false);
1031 if (parent && parent->vlan) {
1032 vsctl_fatal("cannot create bridge with fake bridge as parent");
1035 vsctl_fatal("parent bridge %s does not exist", parent_name);
1037 br = parent->br_cfg;
1039 iface = ovsrec_interface_insert(ctx->txn);
1040 ovsrec_interface_set_name(iface, br_name);
1041 ovsrec_interface_set_type(iface, "internal");
1043 port = ovsrec_port_insert(ctx->txn);
1044 ovsrec_port_set_name(port, br_name);
1045 ovsrec_port_set_interfaces(port, &iface, 1);
1046 ovsrec_port_set_fake_bridge(port, true);
1047 ovsrec_port_set_tag(port, &tag, 1);
1049 bridge_insert_port(br, port);
1056 del_port(struct vsctl_info *info, struct vsctl_port *port)
1058 struct shash_node *node;
1060 SHASH_FOR_EACH (node, &info->ifaces) {
1061 struct vsctl_iface *iface = node->data;
1062 if (iface->port == port) {
1063 ovsrec_interface_delete(iface->iface_cfg);
1066 ovsrec_port_delete(port->port_cfg);
1068 bridge_delete_port((port->bridge->parent
1069 ? port->bridge->parent->br_cfg
1070 : port->bridge->br_cfg), port->port_cfg);
1074 cmd_del_br(struct vsctl_context *ctx)
1076 bool must_exist = !shash_find(&ctx->options, "--if-exists");
1077 struct vsctl_bridge *bridge;
1078 struct vsctl_info info;
1080 get_info(ctx->ovs, &info);
1081 bridge = find_bridge(&info, ctx->argv[1], must_exist);
1083 struct shash_node *node;
1085 SHASH_FOR_EACH (node, &info.ports) {
1086 struct vsctl_port *port = node->data;
1087 if (port->bridge == bridge || port->bridge->parent == bridge
1088 || !strcmp(port->port_cfg->name, bridge->name)) {
1089 del_port(&info, port);
1092 if (bridge->br_cfg) {
1093 ovsrec_bridge_delete(bridge->br_cfg);
1094 ovs_delete_bridge(ctx->ovs, bridge->br_cfg);
1101 output_sorted(struct svec *svec, struct ds *output)
1107 SVEC_FOR_EACH (i, name, svec) {
1108 ds_put_format(output, "%s\n", name);
1113 cmd_list_br(struct vsctl_context *ctx)
1115 struct shash_node *node;
1116 struct vsctl_info info;
1117 struct svec bridges;
1119 get_info(ctx->ovs, &info);
1121 svec_init(&bridges);
1122 SHASH_FOR_EACH (node, &info.bridges) {
1123 struct vsctl_bridge *br = node->data;
1124 svec_add(&bridges, br->name);
1126 output_sorted(&bridges, &ctx->output);
1127 svec_destroy(&bridges);
1133 cmd_br_exists(struct vsctl_context *ctx)
1135 struct vsctl_info info;
1137 get_info(ctx->ovs, &info);
1138 if (!find_bridge(&info, ctx->argv[1], false)) {
1144 /* Returns true if 'b_prefix' (of length 'b_prefix_len') concatenated with 'b'
1145 * equals 'a', false otherwise. */
1147 key_matches(const char *a,
1148 const char *b_prefix, size_t b_prefix_len, const char *b)
1150 return !strncmp(a, b_prefix, b_prefix_len) && !strcmp(a + b_prefix_len, b);
1154 set_external_id(char **old_keys, char **old_values, size_t old_n,
1155 char *key, char *value,
1156 char ***new_keysp, char ***new_valuesp, size_t *new_np)
1163 new_keys = xmalloc(sizeof *new_keys * (old_n + 1));
1164 new_values = xmalloc(sizeof *new_values * (old_n + 1));
1166 for (i = 0; i < old_n; i++) {
1167 if (strcmp(key, old_keys[i])) {
1168 new_keys[new_n] = old_keys[i];
1169 new_values[new_n] = old_values[i];
1174 new_keys[new_n] = key;
1175 new_values[new_n] = value;
1178 *new_keysp = new_keys;
1179 *new_valuesp = new_values;
1184 cmd_br_set_external_id(struct vsctl_context *ctx)
1186 struct vsctl_info info;
1187 struct vsctl_bridge *bridge;
1188 char **keys, **values;
1191 get_info(ctx->ovs, &info);
1192 bridge = find_bridge(&info, ctx->argv[1], true);
1193 if (bridge->br_cfg) {
1194 set_external_id(bridge->br_cfg->key_external_ids,
1195 bridge->br_cfg->value_external_ids,
1196 bridge->br_cfg->n_external_ids,
1197 ctx->argv[2], ctx->argc >= 4 ? ctx->argv[3] : NULL,
1198 &keys, &values, &n);
1199 ovsrec_bridge_set_external_ids(bridge->br_cfg, keys, values, n);
1201 char *key = xasprintf("fake-bridge-%s", ctx->argv[2]);
1202 struct vsctl_port *port = shash_find_data(&info.ports, ctx->argv[1]);
1203 set_external_id(port->port_cfg->key_external_ids,
1204 port->port_cfg->value_external_ids,
1205 port->port_cfg->n_external_ids,
1206 key, ctx->argc >= 4 ? ctx->argv[3] : NULL,
1207 &keys, &values, &n);
1208 ovsrec_port_set_external_ids(port->port_cfg, keys, values, n);
1218 get_external_id(char **keys, char **values, size_t n,
1219 const char *prefix, const char *key,
1222 size_t prefix_len = strlen(prefix);
1227 for (i = 0; i < n; i++) {
1228 if (!key && !strncmp(keys[i], prefix, prefix_len)) {
1229 svec_add_nocopy(&svec, xasprintf("%s=%s",
1230 keys[i] + prefix_len, values[i]));
1231 } else if (key_matches(keys[i], prefix, prefix_len, key)) {
1232 svec_add(&svec, values[i]);
1236 output_sorted(&svec, output);
1237 svec_destroy(&svec);
1241 cmd_br_get_external_id(struct vsctl_context *ctx)
1243 struct vsctl_info info;
1244 struct vsctl_bridge *bridge;
1246 get_info(ctx->ovs, &info);
1247 bridge = find_bridge(&info, ctx->argv[1], true);
1248 if (bridge->br_cfg) {
1249 get_external_id(bridge->br_cfg->key_external_ids,
1250 bridge->br_cfg->value_external_ids,
1251 bridge->br_cfg->n_external_ids,
1252 "", ctx->argc >= 3 ? ctx->argv[2] : NULL,
1255 struct vsctl_port *port = shash_find_data(&info.ports, ctx->argv[1]);
1256 get_external_id(port->port_cfg->key_external_ids,
1257 port->port_cfg->value_external_ids,
1258 port->port_cfg->n_external_ids,
1259 "fake-bridge-", ctx->argc >= 3 ? ctx->argv[2] : NULL, &ctx->output);
1266 cmd_list_ports(struct vsctl_context *ctx)
1268 struct vsctl_bridge *br;
1269 struct shash_node *node;
1270 struct vsctl_info info;
1273 get_info(ctx->ovs, &info);
1274 br = find_bridge(&info, ctx->argv[1], true);
1277 SHASH_FOR_EACH (node, &info.ports) {
1278 struct vsctl_port *port = node->data;
1280 if (strcmp(port->port_cfg->name, br->name) && br == port->bridge) {
1281 svec_add(&ports, port->port_cfg->name);
1284 output_sorted(&ports, &ctx->output);
1285 svec_destroy(&ports);
1291 add_port(struct vsctl_context *ctx,
1292 const char *br_name, const char *port_name,
1293 bool may_exist, bool fake_iface,
1294 char *iface_names[], int n_ifaces,
1295 char *settings[], int n_settings)
1297 struct vsctl_info info;
1298 struct vsctl_bridge *bridge;
1299 struct ovsrec_interface **ifaces;
1300 struct ovsrec_port *port;
1303 get_info(ctx->ovs, &info);
1305 struct vsctl_port *port;
1307 port = find_port(&info, port_name, false);
1309 struct svec want_names, have_names;
1312 svec_init(&want_names);
1313 for (i = 0; i < n_ifaces; i++) {
1314 svec_add(&want_names, iface_names[i]);
1316 svec_sort(&want_names);
1318 svec_init(&have_names);
1319 for (i = 0; i < port->port_cfg->n_interfaces; i++) {
1320 svec_add(&have_names, port->port_cfg->interfaces[i]->name);
1322 svec_sort(&have_names);
1324 if (strcmp(port->bridge->name, br_name)) {
1325 char *command = vsctl_context_to_string(ctx);
1326 vsctl_fatal("\"%s\" but %s is actually attached to bridge %s",
1327 command, port_name, port->bridge->name);
1330 if (!svec_equal(&want_names, &have_names)) {
1331 char *have_names_string = svec_join(&have_names, ", ", "");
1332 char *command = vsctl_context_to_string(ctx);
1334 vsctl_fatal("\"%s\" but %s actually has interface(s) %s",
1335 command, port_name, have_names_string);
1338 svec_destroy(&want_names);
1339 svec_destroy(&have_names);
1344 check_conflicts(&info, port_name,
1345 xasprintf("cannot create a port named %s", port_name));
1346 for (i = 0; i < n_ifaces; i++) {
1347 check_conflicts(&info, iface_names[i],
1348 xasprintf("cannot create an interface named %s",
1351 bridge = find_bridge(&info, br_name, true);
1353 ifaces = xmalloc(n_ifaces * sizeof *ifaces);
1354 for (i = 0; i < n_ifaces; i++) {
1355 ifaces[i] = ovsrec_interface_insert(ctx->txn);
1356 ovsrec_interface_set_name(ifaces[i], iface_names[i]);
1359 port = ovsrec_port_insert(ctx->txn);
1360 ovsrec_port_set_name(port, port_name);
1361 ovsrec_port_set_interfaces(port, ifaces, n_ifaces);
1362 ovsrec_port_set_bond_fake_iface(port, fake_iface);
1366 int64_t tag = bridge->vlan;
1367 ovsrec_port_set_tag(port, &tag, 1);
1370 for (i = 0; i < n_settings; i++) {
1371 set_column(get_table("Port"), &port->header_, settings[i],
1375 bridge_insert_port((bridge->parent ? bridge->parent->br_cfg
1376 : bridge->br_cfg), port);
1382 cmd_add_port(struct vsctl_context *ctx)
1384 bool may_exist = shash_find(&ctx->options, "--may-exist") != 0;
1386 add_port(ctx, ctx->argv[1], ctx->argv[2], may_exist, false,
1387 &ctx->argv[2], 1, &ctx->argv[3], ctx->argc - 3);
1391 cmd_add_bond(struct vsctl_context *ctx)
1393 bool may_exist = shash_find(&ctx->options, "--may-exist") != 0;
1394 bool fake_iface = shash_find(&ctx->options, "--fake-iface");
1398 n_ifaces = ctx->argc - 3;
1399 for (i = 3; i < ctx->argc; i++) {
1400 if (strchr(ctx->argv[i], '=')) {
1406 vsctl_fatal("add-bond requires at least 2 interfaces, but only "
1407 "%d were specified", n_ifaces);
1410 add_port(ctx, ctx->argv[1], ctx->argv[2], may_exist, fake_iface,
1411 &ctx->argv[3], n_ifaces,
1412 &ctx->argv[n_ifaces + 3], ctx->argc - 3 - n_ifaces);
1416 cmd_del_port(struct vsctl_context *ctx)
1418 bool must_exist = !shash_find(&ctx->options, "--if-exists");
1419 bool with_iface = shash_find(&ctx->options, "--with-iface") != NULL;
1420 struct vsctl_port *port;
1421 struct vsctl_info info;
1423 get_info(ctx->ovs, &info);
1425 port = find_port(&info, ctx->argv[ctx->argc - 1], must_exist);
1427 const char *target = ctx->argv[ctx->argc - 1];
1428 struct vsctl_iface *iface;
1430 port = find_port(&info, target, false);
1432 iface = find_iface(&info, target, false);
1437 if (must_exist && !port) {
1438 vsctl_fatal("no port or interface named %s", target);
1443 if (ctx->argc == 3) {
1444 struct vsctl_bridge *bridge;
1446 bridge = find_bridge(&info, ctx->argv[1], true);
1447 if (port->bridge != bridge) {
1448 if (port->bridge->parent == bridge) {
1449 vsctl_fatal("bridge %s does not have a port %s (although "
1450 "its parent bridge %s does)",
1451 ctx->argv[1], ctx->argv[2],
1452 bridge->parent->name);
1454 vsctl_fatal("bridge %s does not have a port %s",
1455 ctx->argv[1], ctx->argv[2]);
1460 del_port(&info, port);
1467 cmd_port_to_br(struct vsctl_context *ctx)
1469 struct vsctl_port *port;
1470 struct vsctl_info info;
1472 get_info(ctx->ovs, &info);
1473 port = find_port(&info, ctx->argv[1], true);
1474 ds_put_format(&ctx->output, "%s\n", port->bridge->name);
1479 cmd_br_to_vlan(struct vsctl_context *ctx)
1481 struct vsctl_bridge *bridge;
1482 struct vsctl_info info;
1484 get_info(ctx->ovs, &info);
1485 bridge = find_bridge(&info, ctx->argv[1], true);
1486 ds_put_format(&ctx->output, "%d\n", bridge->vlan);
1491 cmd_br_to_parent(struct vsctl_context *ctx)
1493 struct vsctl_bridge *bridge;
1494 struct vsctl_info info;
1496 get_info(ctx->ovs, &info);
1497 bridge = find_bridge(&info, ctx->argv[1], true);
1498 if (bridge->parent) {
1499 bridge = bridge->parent;
1501 ds_put_format(&ctx->output, "%s\n", bridge->name);
1506 cmd_list_ifaces(struct vsctl_context *ctx)
1508 struct vsctl_bridge *br;
1509 struct shash_node *node;
1510 struct vsctl_info info;
1513 get_info(ctx->ovs, &info);
1514 br = find_bridge(&info, ctx->argv[1], true);
1517 SHASH_FOR_EACH (node, &info.ifaces) {
1518 struct vsctl_iface *iface = node->data;
1520 if (strcmp(iface->iface_cfg->name, br->name)
1521 && br == iface->port->bridge) {
1522 svec_add(&ifaces, iface->iface_cfg->name);
1525 output_sorted(&ifaces, &ctx->output);
1526 svec_destroy(&ifaces);
1532 cmd_iface_to_br(struct vsctl_context *ctx)
1534 struct vsctl_iface *iface;
1535 struct vsctl_info info;
1537 get_info(ctx->ovs, &info);
1538 iface = find_iface(&info, ctx->argv[1], true);
1539 ds_put_format(&ctx->output, "%s\n", iface->port->bridge->name);
1543 /* Print targets of the 'n_controllers' in 'controllers' on the output for
1546 print_controllers(struct vsctl_context *ctx,
1547 struct ovsrec_controller **controllers,
1548 size_t n_controllers)
1550 /* Print the targets in sorted order for reproducibility. */
1551 struct svec targets;
1554 svec_init(&targets);
1555 for (i = 0; i < n_controllers; i++) {
1556 svec_add(&targets, controllers[i]->target);
1559 svec_sort(&targets);
1560 for (i = 0; i < targets.n; i++) {
1561 ds_put_format(&ctx->output, "%s\n", targets.names[i]);
1563 svec_destroy(&targets);
1567 cmd_get_controller(struct vsctl_context *ctx)
1569 struct vsctl_info info;
1571 get_info(ctx->ovs, &info);
1573 if (ctx->argc == 1 || !strcmp(ctx->argv[1], "default")) {
1574 print_controllers(ctx, info.ctrl, info.n_ctrl);
1576 struct vsctl_bridge *br = find_bridge(&info, ctx->argv[1], true);
1578 print_controllers(ctx, br->ctrl, br->n_ctrl);
1580 print_controllers(ctx, info.ctrl, info.n_ctrl);
1588 delete_controllers(struct ovsrec_controller **controllers,
1589 size_t n_controllers)
1593 for (i = 0; i < n_controllers; i++) {
1594 ovsrec_controller_delete(controllers[i]);
1599 cmd_del_controller(struct vsctl_context *ctx)
1601 struct vsctl_info info;
1603 get_info(ctx->ovs, &info);
1605 if (ctx->argc == 1 || !strcmp(ctx->argv[1], "default")) {
1607 delete_controllers(info.ctrl, info.n_ctrl);
1608 ovsrec_open_vswitch_set_controller(ctx->ovs, NULL, 0);
1611 struct vsctl_bridge *br = find_real_bridge(&info, ctx->argv[1], true);
1613 delete_controllers(br->ctrl, br->n_ctrl);
1614 ovsrec_bridge_set_controller(br->br_cfg, NULL, 0);
1621 static struct ovsrec_controller **
1622 insert_controllers(struct ovsdb_idl_txn *txn, char *targets[], size_t n)
1624 struct ovsrec_controller **controllers;
1627 controllers = xmalloc(n * sizeof *controllers);
1628 for (i = 0; i < n; i++) {
1629 controllers[i] = ovsrec_controller_insert(txn);
1630 ovsrec_controller_set_target(controllers[i], targets[i]);
1637 set_default_controllers(struct vsctl_context *ctx, char *targets[], size_t n)
1639 struct ovsrec_controller **controllers;
1641 delete_controllers(ctx->ovs->controller, ctx->ovs->n_controller);
1643 controllers = insert_controllers(ctx->txn, targets, n);
1644 ovsrec_open_vswitch_set_controller(ctx->ovs, controllers, n);
1649 cmd_set_controller(struct vsctl_context *ctx)
1651 struct vsctl_info info;
1653 get_info(ctx->ovs, &info);
1655 if (ctx->argc == 2) {
1656 /* Set one controller in the "Open_vSwitch" table. */
1657 set_default_controllers(ctx, &ctx->argv[1], 1);
1658 } else if (!strcmp(ctx->argv[1], "default")) {
1659 /* Set one or more controllers in the "Open_vSwitch" table. */
1660 set_default_controllers(ctx, &ctx->argv[2], ctx->argc - 2);
1662 /* Set one or more controllers for a particular bridge. */
1663 struct vsctl_bridge *br = find_real_bridge(&info, ctx->argv[1], true);
1664 struct ovsrec_controller **controllers;
1667 delete_controllers(br->ctrl, br->n_ctrl);
1670 controllers = insert_controllers(ctx->txn, &ctx->argv[2], n);
1671 ovsrec_bridge_set_controller(br->br_cfg, controllers, n);
1679 get_fail_mode(struct ovsrec_controller **controllers, size_t n_controllers)
1681 const char *fail_mode;
1685 for (i = 0; i < n_controllers; i++) {
1686 const char *s = controllers[i]->fail_mode;
1688 if (!strcmp(s, "secure")) {
1700 cmd_get_fail_mode(struct vsctl_context *ctx)
1702 struct vsctl_info info;
1703 const char *fail_mode = NULL;
1705 get_info(ctx->ovs, &info);
1707 if (ctx->argc == 1 || !strcmp(ctx->argv[1], "default")) {
1708 /* Return the fail-mode from the "Open_vSwitch" table */
1709 fail_mode = get_fail_mode(info.ctrl, info.n_ctrl);
1711 /* Return the fail-mode for a particular bridge. */
1712 struct vsctl_bridge *br = find_bridge(&info, ctx->argv[1], true);
1714 /* If no controller is defined for the requested bridge, fallback to
1715 * the "Open_vSwitch" table's controller. */
1716 fail_mode = (br->n_ctrl
1717 ? get_fail_mode(br->ctrl, br->n_ctrl)
1718 : get_fail_mode(info.ctrl, info.n_ctrl));
1721 if (fail_mode && strlen(fail_mode)) {
1722 ds_put_format(&ctx->output, "%s\n", fail_mode);
1729 set_fail_mode(struct ovsrec_controller **controllers, size_t n_controllers,
1730 const char *fail_mode)
1734 for (i = 0; i < n_controllers; i++) {
1735 ovsrec_controller_set_fail_mode(controllers[i], fail_mode);
1740 cmd_del_fail_mode(struct vsctl_context *ctx)
1742 struct vsctl_info info;
1744 get_info(ctx->ovs, &info);
1746 if (ctx->argc == 1 || !strcmp(ctx->argv[1], "default")) {
1747 set_fail_mode(info.ctrl, info.n_ctrl, NULL);
1749 struct vsctl_bridge *br = find_real_bridge(&info, ctx->argv[1], true);
1751 set_fail_mode(br->ctrl, br->n_ctrl, NULL);
1758 cmd_set_fail_mode(struct vsctl_context *ctx)
1760 struct vsctl_info info;
1762 const char *fail_mode;
1764 get_info(ctx->ovs, &info);
1766 if (ctx->argc == 2) {
1768 fail_mode = ctx->argv[1];
1770 bridge = ctx->argv[1];
1771 fail_mode = ctx->argv[2];
1774 if (strcmp(fail_mode, "standalone") && strcmp(fail_mode, "secure")) {
1775 vsctl_fatal("fail-mode must be \"standalone\" or \"secure\"");
1778 if (!strcmp(bridge, "default")) {
1779 /* Set the fail-mode in the "Open_vSwitch" table. */
1781 vsctl_fatal("no controller declared");
1783 set_fail_mode(info.ctrl, info.n_ctrl, fail_mode);
1785 struct vsctl_bridge *br = find_real_bridge(&info, bridge, true);
1788 vsctl_fatal("no controller declared for %s", br->name);
1790 set_fail_mode(br->ctrl, br->n_ctrl, fail_mode);
1797 cmd_get_ssl(struct vsctl_context *ctx)
1799 struct ovsrec_ssl *ssl = ctx->ovs->ssl;
1802 ds_put_format(&ctx->output, "Private key: %s\n", ssl->private_key);
1803 ds_put_format(&ctx->output, "Certificate: %s\n", ssl->certificate);
1804 ds_put_format(&ctx->output, "CA Certificate: %s\n", ssl->ca_cert);
1805 ds_put_format(&ctx->output, "Bootstrap: %s\n",
1806 ssl->bootstrap_ca_cert ? "true" : "false");
1811 cmd_del_ssl(struct vsctl_context *ctx)
1813 struct ovsrec_ssl *ssl = ctx->ovs->ssl;
1816 ovsrec_ssl_delete(ssl);
1817 ovsrec_open_vswitch_set_ssl(ctx->ovs, NULL);
1822 cmd_set_ssl(struct vsctl_context *ctx)
1824 bool bootstrap = shash_find(&ctx->options, "--bootstrap");
1825 struct ovsrec_ssl *ssl = ctx->ovs->ssl;
1828 ovsrec_ssl_delete(ssl);
1830 ssl = ovsrec_ssl_insert(ctx->txn);
1832 ovsrec_ssl_set_private_key(ssl, ctx->argv[1]);
1833 ovsrec_ssl_set_certificate(ssl, ctx->argv[2]);
1834 ovsrec_ssl_set_ca_cert(ssl, ctx->argv[3]);
1836 ovsrec_ssl_set_bootstrap_ca_cert(ssl, bootstrap);
1838 ovsrec_open_vswitch_set_ssl(ctx->ovs, ssl);
1841 /* Parameter commands. */
1843 struct vsctl_row_id {
1844 const struct ovsdb_idl_table_class *table;
1845 const struct ovsdb_idl_column *name_column;
1846 const struct ovsdb_idl_column *uuid_column;
1849 struct vsctl_table_class {
1850 struct ovsdb_idl_table_class *class;
1851 struct vsctl_row_id row_ids[2];
1854 static const struct vsctl_table_class tables[] = {
1855 {&ovsrec_table_bridge,
1856 {{&ovsrec_table_bridge, &ovsrec_bridge_col_name, NULL},
1857 {NULL, NULL, NULL}}},
1859 {&ovsrec_table_controller,
1860 {{&ovsrec_table_bridge,
1861 &ovsrec_bridge_col_name,
1862 &ovsrec_bridge_col_controller},
1863 {&ovsrec_table_open_vswitch,
1865 &ovsrec_open_vswitch_col_controller}}},
1867 {&ovsrec_table_interface,
1868 {{&ovsrec_table_interface, &ovsrec_interface_col_name, NULL},
1869 {NULL, NULL, NULL}}},
1871 {&ovsrec_table_mirror,
1872 {{&ovsrec_table_mirror, &ovsrec_mirror_col_name, NULL},
1873 {NULL, NULL, NULL}}},
1875 {&ovsrec_table_netflow,
1876 {{&ovsrec_table_bridge,
1877 &ovsrec_bridge_col_name,
1878 &ovsrec_bridge_col_netflow},
1879 {NULL, NULL, NULL}}},
1881 {&ovsrec_table_open_vswitch,
1882 {{&ovsrec_table_open_vswitch, NULL, NULL},
1883 {NULL, NULL, NULL}}},
1885 {&ovsrec_table_port,
1886 {{&ovsrec_table_port, &ovsrec_port_col_name, NULL},
1887 {NULL, NULL, NULL}}},
1890 {{&ovsrec_table_port, &ovsrec_port_col_name, &ovsrec_port_col_qos},
1891 {NULL, NULL, NULL}}},
1893 {&ovsrec_table_queue,
1894 {{NULL, NULL, NULL},
1895 {NULL, NULL, NULL}}},
1898 {{&ovsrec_table_open_vswitch, NULL, &ovsrec_open_vswitch_col_ssl}}},
1900 {&ovsrec_table_sflow,
1901 {{&ovsrec_table_bridge,
1902 &ovsrec_bridge_col_name,
1903 &ovsrec_bridge_col_sflow},
1904 {NULL, NULL, NULL}}},
1906 {NULL, {{NULL, NULL, NULL}, {NULL, NULL, NULL}}}
1910 die_if_error(char *error)
1913 vsctl_fatal("%s", error);
1918 to_lower_and_underscores(unsigned c)
1920 return c == '-' ? '_' : tolower(c);
1924 score_partial_match(const char *name, const char *s)
1928 if (!strcmp(name, s)) {
1931 for (score = 0; ; score++, name++, s++) {
1932 if (to_lower_and_underscores(*name) != to_lower_and_underscores(*s)) {
1934 } else if (*name == '\0') {
1935 return UINT_MAX - 1;
1938 return *s == '\0' ? score : 0;
1941 static const struct vsctl_table_class *
1942 get_table(const char *table_name)
1944 const struct vsctl_table_class *table;
1945 const struct vsctl_table_class *best_match = NULL;
1946 unsigned int best_score = 0;
1948 for (table = tables; table->class; table++) {
1949 unsigned int score = score_partial_match(table->class->name,
1951 if (score > best_score) {
1954 } else if (score == best_score) {
1960 } else if (best_score) {
1961 vsctl_fatal("multiple table names match \"%s\"", table_name);
1963 vsctl_fatal("unknown table \"%s\"", table_name);
1967 static const struct ovsdb_idl_row *
1968 get_row_by_id(struct vsctl_context *ctx, const struct vsctl_table_class *table,
1969 const struct vsctl_row_id *id, const char *record_id)
1971 const struct ovsdb_idl_row *referrer, *final;
1977 if (!id->name_column) {
1978 if (strcmp(record_id, ".")) {
1981 referrer = ovsdb_idl_first_row(ctx->idl, id->table);
1982 if (!referrer || ovsdb_idl_next_row(referrer)) {
1986 const struct ovsdb_idl_row *row;
1989 for (row = ovsdb_idl_first_row(ctx->idl, id->table);
1991 row = ovsdb_idl_next_row(row))
1993 const struct ovsdb_datum *name;
1995 name = ovsdb_idl_get(row, id->name_column,
1996 OVSDB_TYPE_STRING, OVSDB_TYPE_VOID);
1997 if (name->n == 1 && !strcmp(name->keys[0].string, record_id)) {
1999 vsctl_fatal("multiple rows in %s match \"%s\"",
2000 table->class->name, record_id);
2011 if (id->uuid_column) {
2012 const struct ovsdb_datum *uuid;
2014 uuid = ovsdb_idl_get(referrer, id->uuid_column,
2015 OVSDB_TYPE_UUID, OVSDB_TYPE_VOID);
2017 final = ovsdb_idl_get_row_for_uuid(ctx->idl, table->class,
2018 &uuid->keys[0].uuid);
2027 static const struct ovsdb_idl_row *
2028 get_row (struct vsctl_context *ctx,
2029 const struct vsctl_table_class *table, const char *record_id)
2031 const struct ovsdb_idl_row *row;
2034 if (uuid_from_string(&uuid, record_id)) {
2035 row = ovsdb_idl_get_row_for_uuid(ctx->idl, table->class, &uuid);
2039 for (i = 0; i < ARRAY_SIZE(table->row_ids); i++) {
2040 row = get_row_by_id(ctx, table, &table->row_ids[i], record_id);
2049 static const struct ovsdb_idl_row *
2050 must_get_row(struct vsctl_context *ctx,
2051 const struct vsctl_table_class *table, const char *record_id)
2053 const struct ovsdb_idl_row *row = get_row(ctx, table, record_id);
2055 vsctl_fatal("no row \"%s\" in table %s",
2056 record_id, table->class->name);
2062 get_column(const struct vsctl_table_class *table, const char *column_name,
2063 const struct ovsdb_idl_column **columnp)
2065 const struct ovsdb_idl_column *best_match = NULL;
2066 unsigned int best_score = 0;
2069 for (i = 0; i < table->class->n_columns; i++) {
2070 const struct ovsdb_idl_column *column = &table->class->columns[i];
2071 unsigned int score = score_partial_match(column->name, column_name);
2072 if (score > best_score) {
2073 best_match = column;
2075 } else if (score == best_score) {
2080 *columnp = best_match;
2083 } else if (best_score) {
2084 return xasprintf("%s contains more than one column whose name "
2085 "matches \"%s\"", table->class->name, column_name);
2087 return xasprintf("%s does not contain a column whose name matches "
2088 "\"%s\"", table->class->name, column_name);
2093 missing_operator_error(const char *arg, const char **allowed_operators,
2099 ds_put_format(&s, "%s: argument does not end in ", arg);
2100 ds_put_format(&s, "\"%s\"", allowed_operators[0]);
2101 if (n_allowed == 2) {
2102 ds_put_format(&s, " or \"%s\"", allowed_operators[1]);
2103 } else if (n_allowed > 2) {
2106 for (i = 1; i < n_allowed - 1; i++) {
2107 ds_put_format(&s, ", \"%s\"", allowed_operators[i]);
2109 ds_put_format(&s, ", or \"%s\"", allowed_operators[i]);
2111 ds_put_format(&s, " followed by a value.");
2113 return ds_steal_cstr(&s);
2116 /* Breaks 'arg' apart into a number of fields in the following order:
2118 * - If 'columnp' is nonnull, the name of a column in 'table'. The column
2119 * is stored into '*columnp'. The column name may be abbreviated.
2121 * - If 'keyp' is nonnull, optionally a key string. (If both 'columnp'
2122 * and 'keyp' are nonnull, then the column and key names are expected to
2123 * be separated by ':'). The key is stored as a malloc()'d string into
2124 * '*keyp', or NULL if no key is present in 'arg'.
2126 * - If 'valuep' is nonnull, an operator followed by a value string. The
2127 * allowed operators are the 'n_allowed' string in 'allowed_operators',
2128 * or just "=" if 'n_allowed' is 0. If 'operatorp' is nonnull, then the
2129 * operator is stored into '*operatorp' (one of the pointers from
2130 * 'allowed_operators' is stored; nothing is malloc()'d). The value is
2131 * stored as a malloc()'d string into '*valuep', or NULL if no value is
2134 * At least 'columnp' or 'keyp' must be nonnull.
2136 * On success, returns NULL. On failure, returned a malloc()'d string error
2137 * message and stores NULL into all of the nonnull output arguments. */
2138 static char * WARN_UNUSED_RESULT
2139 parse_column_key_value(const char *arg,
2140 const struct vsctl_table_class *table,
2141 const struct ovsdb_idl_column **columnp, char **keyp,
2142 const char **operatorp,
2143 const char **allowed_operators, size_t n_allowed,
2146 const char *p = arg;
2149 assert(columnp || keyp);
2150 assert(!(operatorp && !valuep));
2158 /* Parse column name. */
2162 error = ovsdb_token_parse(&p, &column_name);
2166 if (column_name[0] == '\0') {
2168 error = xasprintf("%s: missing column name", arg);
2171 error = get_column(table, column_name, columnp);
2178 /* Parse key string. */
2179 if (*p == ':' || !columnp) {
2183 error = xasprintf("%s: key not accepted here", arg);
2186 error = ovsdb_token_parse(&p, keyp);
2194 /* Parse value string. */
2200 if (!allowed_operators) {
2201 static const char *equals = "=";
2202 allowed_operators = =
2208 for (i = 0; i < n_allowed; i++) {
2209 const char *op = allowed_operators[i];
2210 size_t op_len = strlen(op);
2212 if (op_len > best_len && !strncmp(op, p, op_len) && p[op_len]) {
2218 error = missing_operator_error(arg, allowed_operators, n_allowed);
2225 *valuep = xstrdup(p + best_len);
2231 error = xasprintf("%s: trailing garbage \"%s\" in argument",
2257 cmd_get(struct vsctl_context *ctx)
2259 bool if_exists = shash_find(&ctx->options, "--if-exists");
2260 const char *table_name = ctx->argv[1];
2261 const char *record_id = ctx->argv[2];
2262 const struct vsctl_table_class *table;
2263 const struct ovsdb_idl_row *row;
2264 struct ds *out = &ctx->output;
2267 table = get_table(table_name);
2268 row = must_get_row(ctx, table, record_id);
2269 for (i = 3; i < ctx->argc; i++) {
2270 const struct ovsdb_idl_column *column;
2271 const struct ovsdb_datum *datum;
2274 /* Special case for obtaining the UUID of a row. We can't just do this
2275 * through parse_column_key_value() below since it returns a "struct
2276 * ovsdb_idl_column" and the UUID column doesn't have one. */
2277 if (!strcasecmp(ctx->argv[i], "_uuid")
2278 || !strcasecmp(ctx->argv[i], "-uuid")) {
2279 ds_put_format(out, UUID_FMT"\n", UUID_ARGS(&row->uuid));
2283 die_if_error(parse_column_key_value(ctx->argv[i], table,
2284 &column, &key_string,
2285 NULL, NULL, 0, NULL));
2287 datum = ovsdb_idl_read(row, column);
2289 union ovsdb_atom key;
2292 if (column->type.value.type == OVSDB_TYPE_VOID) {
2293 vsctl_fatal("cannot specify key to get for non-map column %s",
2297 die_if_error(ovsdb_atom_from_string(&key,
2299 key_string, ctx->symtab));
2301 idx = ovsdb_datum_find_key(datum, &key,
2302 column->type.key.type);
2303 if (idx == UINT_MAX) {
2305 vsctl_fatal("no key \"%s\" in %s record \"%s\" column %s",
2306 key_string, table->class->name, record_id,
2310 ovsdb_atom_to_string(&datum->values[idx],
2311 column->type.value.type, out);
2313 ovsdb_atom_destroy(&key, column->type.key.type);
2315 ovsdb_datum_to_string(datum, &column->type, out);
2317 ds_put_char(out, '\n');
2324 list_record(const struct vsctl_table_class *table,
2325 const struct ovsdb_idl_row *row, struct ds *out)
2329 ds_put_format(out, "%-20s: "UUID_FMT"\n", "_uuid",
2330 UUID_ARGS(&row->uuid));
2331 for (i = 0; i < table->class->n_columns; i++) {
2332 const struct ovsdb_idl_column *column = &table->class->columns[i];
2333 const struct ovsdb_datum *datum;
2335 datum = ovsdb_idl_read(row, column);
2337 ds_put_format(out, "%-20s: ", column->name);
2338 ovsdb_datum_to_string(datum, &column->type, out);
2339 ds_put_char(out, '\n');
2344 cmd_list(struct vsctl_context *ctx)
2346 const char *table_name = ctx->argv[1];
2347 const struct vsctl_table_class *table;
2348 struct ds *out = &ctx->output;
2351 table = get_table(table_name);
2352 if (ctx->argc > 2) {
2353 for (i = 2; i < ctx->argc; i++) {
2355 ds_put_char(out, '\n');
2357 list_record(table, must_get_row(ctx, table, ctx->argv[i]), out);
2360 const struct ovsdb_idl_row *row;
2363 for (row = ovsdb_idl_first_row(ctx->idl, table->class), first = true;
2365 row = ovsdb_idl_next_row(row), first = false) {
2367 ds_put_char(out, '\n');
2369 list_record(table, row, out);
2375 set_column(const struct vsctl_table_class *table,
2376 const struct ovsdb_idl_row *row, const char *arg,
2377 struct ovsdb_symbol_table *symtab)
2379 const struct ovsdb_idl_column *column;
2380 char *key_string, *value_string;
2383 error = parse_column_key_value(arg, table, &column, &key_string,
2384 NULL, NULL, 0, &value_string);
2385 die_if_error(error);
2386 if (!value_string) {
2387 vsctl_fatal("%s: missing value", arg);
2391 union ovsdb_atom key, value;
2392 struct ovsdb_datum datum;
2394 if (column->type.value.type == OVSDB_TYPE_VOID) {
2395 vsctl_fatal("cannot specify key to set for non-map column %s",
2399 die_if_error(ovsdb_atom_from_string(&key, &column->type.key,
2400 key_string, symtab));
2401 die_if_error(ovsdb_atom_from_string(&value, &column->type.value,
2402 value_string, symtab));
2404 ovsdb_datum_init_empty(&datum);
2405 ovsdb_datum_add_unsafe(&datum, &key, &value, &column->type);
2407 ovsdb_atom_destroy(&key, column->type.key.type);
2408 ovsdb_atom_destroy(&value, column->type.value.type);
2410 ovsdb_datum_union(&datum, ovsdb_idl_read(row, column),
2411 &column->type, false);
2412 ovsdb_idl_txn_write(row, column, &datum);
2414 struct ovsdb_datum datum;
2416 die_if_error(ovsdb_datum_from_string(&datum, &column->type,
2417 value_string, symtab));
2418 ovsdb_idl_txn_write(row, column, &datum);
2426 cmd_set(struct vsctl_context *ctx)
2428 const char *table_name = ctx->argv[1];
2429 const char *record_id = ctx->argv[2];
2430 const struct vsctl_table_class *table;
2431 const struct ovsdb_idl_row *row;
2434 table = get_table(table_name);
2435 row = must_get_row(ctx, table, record_id);
2436 for (i = 3; i < ctx->argc; i++) {
2437 set_column(table, row, ctx->argv[i], ctx->symtab);
2442 cmd_add(struct vsctl_context *ctx)
2444 const char *table_name = ctx->argv[1];
2445 const char *record_id = ctx->argv[2];
2446 const char *column_name = ctx->argv[3];
2447 const struct vsctl_table_class *table;
2448 const struct ovsdb_idl_column *column;
2449 const struct ovsdb_idl_row *row;
2450 const struct ovsdb_type *type;
2451 struct ovsdb_datum old;
2454 table = get_table(table_name);
2455 row = must_get_row(ctx, table, record_id);
2456 die_if_error(get_column(table, column_name, &column));
2458 type = &column->type;
2459 ovsdb_datum_clone(&old, ovsdb_idl_read(row, column), &column->type);
2460 for (i = 4; i < ctx->argc; i++) {
2461 struct ovsdb_type add_type;
2462 struct ovsdb_datum add;
2466 add_type.n_max = UINT_MAX;
2467 die_if_error(ovsdb_datum_from_string(&add, &add_type, ctx->argv[i],
2469 ovsdb_datum_union(&old, &add, type, false);
2470 ovsdb_datum_destroy(&add, type);
2472 if (old.n > type->n_max) {
2473 vsctl_fatal("\"add\" operation would put %u %s in column %s of "
2474 "table %s but the maximum number is %u",
2476 type->value.type == OVSDB_TYPE_VOID ? "values" : "pairs",
2477 column->name, table->class->name, type->n_max);
2479 ovsdb_idl_txn_write(row, column, &old);
2483 cmd_remove(struct vsctl_context *ctx)
2485 const char *table_name = ctx->argv[1];
2486 const char *record_id = ctx->argv[2];
2487 const char *column_name = ctx->argv[3];
2488 const struct vsctl_table_class *table;
2489 const struct ovsdb_idl_column *column;
2490 const struct ovsdb_idl_row *row;
2491 const struct ovsdb_type *type;
2492 struct ovsdb_datum old;
2495 table = get_table(table_name);
2496 row = must_get_row(ctx, table, record_id);
2497 die_if_error(get_column(table, column_name, &column));
2499 type = &column->type;
2500 ovsdb_datum_clone(&old, ovsdb_idl_read(row, column), &column->type);
2501 for (i = 4; i < ctx->argc; i++) {
2502 struct ovsdb_type rm_type;
2503 struct ovsdb_datum rm;
2508 rm_type.n_max = UINT_MAX;
2509 error = ovsdb_datum_from_string(&rm, &rm_type,
2510 ctx->argv[i], ctx->symtab);
2511 if (error && ovsdb_type_is_map(&rm_type)) {
2513 rm_type.value.type = OVSDB_TYPE_VOID;
2514 die_if_error(ovsdb_datum_from_string(&rm, &rm_type,
2515 ctx->argv[i], ctx->symtab));
2517 ovsdb_datum_subtract(&old, type, &rm, &rm_type);
2518 ovsdb_datum_destroy(&rm, &rm_type);
2520 if (old.n < type->n_min) {
2521 vsctl_fatal("\"remove\" operation would put %u %s in column %s of "
2522 "table %s but the minimum number is %u",
2524 type->value.type == OVSDB_TYPE_VOID ? "values" : "pairs",
2525 column->name, table->class->name, type->n_min);
2527 ovsdb_idl_txn_write(row, column, &old);
2531 cmd_clear(struct vsctl_context *ctx)
2533 const char *table_name = ctx->argv[1];
2534 const char *record_id = ctx->argv[2];
2535 const struct vsctl_table_class *table;
2536 const struct ovsdb_idl_row *row;
2539 table = get_table(table_name);
2540 row = must_get_row(ctx, table, record_id);
2541 for (i = 3; i < ctx->argc; i++) {
2542 const struct ovsdb_idl_column *column;
2543 const struct ovsdb_type *type;
2544 struct ovsdb_datum datum;
2546 die_if_error(get_column(table, ctx->argv[i], &column));
2548 type = &column->type;
2549 if (type->n_min > 0) {
2550 vsctl_fatal("\"clear\" operation cannot be applied to column %s "
2551 "of table %s, which is not allowed to be empty",
2552 column->name, table->class->name);
2555 ovsdb_datum_init_empty(&datum);
2556 ovsdb_idl_txn_write(row, column, &datum);
2561 cmd_create(struct vsctl_context *ctx)
2563 const char *id = shash_find_data(&ctx->options, "--id");
2564 const char *table_name = ctx->argv[1];
2565 const struct vsctl_table_class *table;
2566 const struct ovsdb_idl_row *row;
2567 const struct uuid *uuid;
2571 struct ovsdb_symbol *symbol;
2574 vsctl_fatal("row id \"%s\" does not begin with \"@\"", id);
2577 symbol = ovsdb_symbol_table_insert(ctx->symtab, id);
2579 vsctl_fatal("row id \"%s\" may only be used to insert a single "
2582 symbol->used = true;
2584 uuid = &symbol->uuid;
2589 table = get_table(table_name);
2590 row = ovsdb_idl_txn_insert(ctx->txn, table->class, uuid);
2591 for (i = 2; i < ctx->argc; i++) {
2592 set_column(table, row, ctx->argv[i], ctx->symtab);
2594 ds_put_format(&ctx->output, UUID_FMT, UUID_ARGS(&row->uuid));
2597 /* This function may be used as the 'postprocess' function for commands that
2598 * insert new rows into the database. It expects that the command's 'run'
2599 * function prints the UUID reported by ovsdb_idl_txn_insert() as the command's
2600 * sole output. It replaces that output by the row's permanent UUID assigned
2601 * by the database server and appends a new-line.
2603 * Currently we use this only for "create", because the higher-level commands
2604 * are supposed to be independent of the actual structure of the vswitch
2607 post_create(struct vsctl_context *ctx)
2609 const struct uuid *real;
2612 uuid_from_string(&dummy, ds_cstr(&ctx->output));
2613 real = ovsdb_idl_txn_get_insert_uuid(ctx->txn, &dummy);
2615 ds_clear(&ctx->output);
2616 ds_put_format(&ctx->output, UUID_FMT, UUID_ARGS(real));
2618 ds_put_char(&ctx->output, '\n');
2622 cmd_destroy(struct vsctl_context *ctx)
2624 bool must_exist = !shash_find(&ctx->options, "--if-exists");
2625 const char *table_name = ctx->argv[1];
2626 const struct vsctl_table_class *table;
2629 table = get_table(table_name);
2630 for (i = 2; i < ctx->argc; i++) {
2631 const struct ovsdb_idl_row *row;
2633 row = (must_exist ? must_get_row : get_row)(ctx, table, ctx->argv[i]);
2635 ovsdb_idl_txn_delete(row);
2641 is_condition_satified(const struct vsctl_table_class *table,
2642 const struct ovsdb_idl_row *row, const char *arg,
2643 struct ovsdb_symbol_table *symtab)
2645 static const char *operators[] = {
2646 "=", "!=", "<", ">", "<=", ">="
2649 const struct ovsdb_idl_column *column;
2650 const struct ovsdb_datum *have_datum;
2651 char *key_string, *value_string;
2652 const char *operator;
2657 error = parse_column_key_value(arg, table, &column, &key_string,
2658 &operator, operators, ARRAY_SIZE(operators),
2660 die_if_error(error);
2661 if (!value_string) {
2662 vsctl_fatal("%s: missing value", arg);
2665 have_datum = ovsdb_idl_read(row, column);
2667 union ovsdb_atom want_key, want_value;
2669 if (column->type.value.type == OVSDB_TYPE_VOID) {
2670 vsctl_fatal("cannot specify key to check for non-map column %s",
2674 die_if_error(ovsdb_atom_from_string(&want_key, &column->type.key,
2675 key_string, symtab));
2676 die_if_error(ovsdb_atom_from_string(&want_value, &column->type.value,
2677 value_string, symtab));
2679 idx = ovsdb_datum_find_key(have_datum,
2680 &want_key, column->type.key.type);
2681 if (idx != UINT_MAX) {
2682 cmp = ovsdb_atom_compare_3way(&have_datum->values[idx],
2684 column->type.value.type);
2687 ovsdb_atom_destroy(&want_key, column->type.key.type);
2688 ovsdb_atom_destroy(&want_value, column->type.value.type);
2690 struct ovsdb_datum want_datum;
2692 die_if_error(ovsdb_datum_from_string(&want_datum, &column->type,
2693 value_string, symtab));
2695 cmp = ovsdb_datum_compare_3way(have_datum, &want_datum,
2697 ovsdb_datum_destroy(&want_datum, &column->type);
2703 return (idx == UINT_MAX ? false
2704 : !strcmp(operator, "=") ? cmp == 0
2705 : !strcmp(operator, "!=") ? cmp != 0
2706 : !strcmp(operator, "<") ? cmp < 0
2707 : !strcmp(operator, ">") ? cmp > 0
2708 : !strcmp(operator, "<=") ? cmp <= 0
2709 : !strcmp(operator, ">=") ? cmp >= 0
2714 cmd_wait_until(struct vsctl_context *ctx)
2716 const char *table_name = ctx->argv[1];
2717 const char *record_id = ctx->argv[2];
2718 const struct vsctl_table_class *table;
2719 const struct ovsdb_idl_row *row;
2722 table = get_table(table_name);
2724 row = get_row(ctx, table, record_id);
2726 ctx->try_again = true;
2730 for (i = 3; i < ctx->argc; i++) {
2731 if (!is_condition_satified(table, row, ctx->argv[i], ctx->symtab)) {
2732 ctx->try_again = true;
2738 static struct json *
2739 where_uuid_equals(const struct uuid *uuid)
2742 json_array_create_1(
2743 json_array_create_3(
2744 json_string_create("_uuid"),
2745 json_string_create("=="),
2746 json_array_create_2(
2747 json_string_create("uuid"),
2748 json_string_create_nocopy(
2749 xasprintf(UUID_FMT, UUID_ARGS(uuid))))));
2753 vsctl_context_init(struct vsctl_context *ctx, struct vsctl_command *command,
2754 struct ovsdb_idl *idl, struct ovsdb_idl_txn *txn,
2755 const struct ovsrec_open_vswitch *ovs,
2756 struct ovsdb_symbol_table *symtab)
2758 ctx->argc = command->argc;
2759 ctx->argv = command->argv;
2760 ctx->options = command->options;
2762 ds_swap(&ctx->output, &command->output);
2766 ctx->symtab = symtab;
2768 ctx->try_again = false;
2772 vsctl_context_done(struct vsctl_context *ctx, struct vsctl_command *command)
2774 ds_swap(&ctx->output, &command->output);
2778 do_vsctl(const char *args, struct vsctl_command *commands, size_t n_commands,
2779 struct ovsdb_idl *idl)
2781 struct ovsdb_idl_txn *txn;
2782 const struct ovsrec_open_vswitch *ovs;
2783 enum ovsdb_idl_txn_status status;
2784 struct ovsdb_symbol_table *symtab;
2786 struct vsctl_command *c;
2787 int64_t next_cfg = 0;
2790 txn = the_idl_txn = ovsdb_idl_txn_create(idl);
2792 ovsdb_idl_txn_set_dry_run(txn);
2795 ovsdb_idl_txn_add_comment(txn, "ovs-vsctl: %s", args);
2797 ovs = ovsrec_open_vswitch_first(idl);
2799 /* XXX add verification that table is empty */
2800 ovs = ovsrec_open_vswitch_insert(txn);
2803 if (wait_for_reload) {
2804 struct json *where = where_uuid_equals(&ovs->header_.uuid);
2805 ovsdb_idl_txn_increment(txn, "Open_vSwitch", "next_cfg", where);
2806 json_destroy(where);
2809 symtab = ovsdb_symbol_table_create();
2810 for (c = commands; c < &commands[n_commands]; c++) {
2811 ds_init(&c->output);
2813 for (c = commands; c < &commands[n_commands]; c++) {
2814 struct vsctl_context ctx;
2816 vsctl_context_init(&ctx, c, idl, txn, ovs, symtab);
2817 (c->syntax->run)(&ctx);
2818 vsctl_context_done(&ctx, c);
2820 if (ctx.try_again) {
2825 status = ovsdb_idl_txn_commit_block(txn);
2826 if (wait_for_reload && status == TXN_SUCCESS) {
2827 next_cfg = ovsdb_idl_txn_get_increment_new_value(txn);
2829 if (status == TXN_UNCHANGED || status == TXN_SUCCESS) {
2830 for (c = commands; c < &commands[n_commands]; c++) {
2831 if (c->syntax->postprocess) {
2832 struct vsctl_context ctx;
2834 vsctl_context_init(&ctx, c, idl, txn, ovs, symtab);
2835 (c->syntax->postprocess)(&ctx);
2836 vsctl_context_done(&ctx, c);
2840 error = xstrdup(ovsdb_idl_txn_get_error(txn));
2841 ovsdb_idl_txn_destroy(txn);
2844 unused = ovsdb_symbol_table_find_unused(symtab);
2846 vsctl_fatal("row id \"%s\" is referenced but never created (e.g. "
2847 "with \"-- --id=%s create ...\")", unused, unused);
2851 case TXN_INCOMPLETE:
2855 /* Should not happen--we never call ovsdb_idl_txn_abort(). */
2856 vsctl_fatal("transaction aborted");
2866 vsctl_fatal("transaction error: %s", error);
2873 ovsdb_symbol_table_destroy(symtab);
2875 for (c = commands; c < &commands[n_commands]; c++) {
2876 struct ds *ds = &c->output;
2877 struct shash_node *node;
2883 for (j = 0; j < ds->length; j++) {
2884 int c = ds->string[j];
2887 fputs("\\n", stdout);
2891 fputs("\\\\", stdout);
2900 fputs(ds_cstr(ds), stdout);
2902 ds_destroy(&c->output);
2904 SHASH_FOR_EACH (node, &c->options) {
2907 shash_destroy(&c->options);
2911 if (wait_for_reload && status != TXN_UNCHANGED) {
2913 const struct ovsrec_open_vswitch *ovs;
2916 OVSREC_OPEN_VSWITCH_FOR_EACH (ovs, idl) {
2917 if (ovs->cur_cfg >= next_cfg) {
2921 ovsdb_idl_wait(idl);
2926 ovsdb_idl_destroy(idl);
2931 /* Our transaction needs to be rerun, or a prerequisite was not met. Free
2932 * resources and return so that the caller can try again. */
2933 ovsdb_idl_txn_abort(txn);
2934 ovsdb_idl_txn_destroy(txn);
2935 ovsdb_symbol_table_destroy(symtab);
2936 for (c = commands; c < &commands[n_commands]; c++) {
2937 ds_destroy(&c->output);
2942 static const struct vsctl_command_syntax all_commands[] = {
2943 /* Open vSwitch commands. */
2944 {"init", 0, 0, cmd_init, NULL, ""},
2946 /* Bridge commands. */
2947 {"add-br", 1, 3, cmd_add_br, NULL, "--may-exist"},
2948 {"del-br", 1, 1, cmd_del_br, NULL, "--if-exists"},
2949 {"list-br", 0, 0, cmd_list_br, NULL, ""},
2950 {"br-exists", 1, 1, cmd_br_exists, NULL, ""},
2951 {"br-to-vlan", 1, 1, cmd_br_to_vlan, NULL, ""},
2952 {"br-to-parent", 1, 1, cmd_br_to_parent, NULL, ""},
2953 {"br-set-external-id", 2, 3, cmd_br_set_external_id, NULL, ""},
2954 {"br-get-external-id", 1, 2, cmd_br_get_external_id, NULL, ""},
2956 /* Port commands. */
2957 {"list-ports", 1, 1, cmd_list_ports, NULL, ""},
2958 {"add-port", 2, INT_MAX, cmd_add_port, NULL, "--may-exist"},
2959 {"add-bond", 4, INT_MAX, cmd_add_bond, NULL, "--may-exist,--fake-iface"},
2960 {"del-port", 1, 2, cmd_del_port, NULL, "--if-exists,--with-iface"},
2961 {"port-to-br", 1, 1, cmd_port_to_br, NULL, ""},
2963 /* Interface commands. */
2964 {"list-ifaces", 1, 1, cmd_list_ifaces, NULL, ""},
2965 {"iface-to-br", 1, 1, cmd_iface_to_br, NULL, ""},
2967 /* Controller commands. */
2968 {"get-controller", 0, 1, cmd_get_controller, NULL, ""},
2969 {"del-controller", 0, 1, cmd_del_controller, NULL, ""},
2970 {"set-controller", 1, INT_MAX, cmd_set_controller, NULL, ""},
2971 {"get-fail-mode", 0, 1, cmd_get_fail_mode, NULL, ""},
2972 {"del-fail-mode", 0, 1, cmd_del_fail_mode, NULL, ""},
2973 {"set-fail-mode", 1, 2, cmd_set_fail_mode, NULL, ""},
2976 {"get-ssl", 0, 0, cmd_get_ssl, NULL, ""},
2977 {"del-ssl", 0, 0, cmd_del_ssl, NULL, ""},
2978 {"set-ssl", 3, 3, cmd_set_ssl, NULL, "--bootstrap"},
2980 /* Switch commands. */
2981 {"emer-reset", 0, 0, cmd_emer_reset, NULL, ""},
2983 /* Parameter commands. */
2984 {"get", 3, INT_MAX, cmd_get, NULL, "--if-exists"},
2985 {"list", 1, INT_MAX, cmd_list, NULL, ""},
2986 {"set", 3, INT_MAX, cmd_set, NULL, ""},
2987 {"add", 4, INT_MAX, cmd_add, NULL, ""},
2988 {"remove", 4, INT_MAX, cmd_remove, NULL, ""},
2989 {"clear", 3, INT_MAX, cmd_clear, NULL, ""},
2990 {"create", 2, INT_MAX, cmd_create, post_create, "--id="},
2991 {"destroy", 1, INT_MAX, cmd_destroy, NULL, "--if-exists"},
2992 {"wait-until", 2, INT_MAX, cmd_wait_until, NULL, ""},
2994 {NULL, 0, 0, NULL, NULL, NULL},