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 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},
190 c = getopt_long(argc, argv, "+v::hVt:", long_options, NULL);
205 vlog_set_levels(&VLM_vsctl, VLF_SYSLOG, VLL_WARN);
209 wait_for_reload = false;
220 OVS_PRINT_VERSION(0, 0);
224 timeout = strtoul(optarg, NULL, 10);
226 vsctl_fatal("value %s on -t or --timeout is invalid",
234 STREAM_SSL_OPTION_HANDLERS
236 case OPT_PEER_CA_CERT:
237 stream_ssl_set_peer_ca_cert_file(optarg);
254 static struct vsctl_command *
255 parse_commands(int argc, char *argv[], size_t *n_commandsp)
257 struct vsctl_command *commands;
258 size_t n_commands, allocated_commands;
262 n_commands = allocated_commands = 0;
264 for (start = i = 0; i <= argc; i++) {
265 if (i == argc || !strcmp(argv[i], "--")) {
267 if (n_commands >= allocated_commands) {
268 struct vsctl_command *c;
270 commands = x2nrealloc(commands, &allocated_commands,
272 for (c = commands; c < &commands[n_commands]; c++) {
273 shash_moved(&c->options);
276 parse_command(i - start, &argv[start],
277 &commands[n_commands++]);
283 vsctl_fatal("missing command name (use --help for help)");
285 *n_commandsp = n_commands;
290 parse_command(int argc, char *argv[], struct vsctl_command *command)
292 const struct vsctl_command_syntax *p;
295 shash_init(&command->options);
296 for (i = 0; i < argc; i++) {
297 const char *option = argv[i];
301 if (option[0] != '-') {
305 equals = strchr(option, '=');
307 key = xmemdup0(option, equals - option);
308 value = xstrdup(equals + 1);
310 key = xstrdup(option);
314 if (shash_find(&command->options, key)) {
315 vsctl_fatal("'%s' option specified multiple times", argv[i]);
317 shash_add_nocopy(&command->options, key, value);
320 vsctl_fatal("missing command name");
323 for (p = all_commands; p->name; p++) {
324 if (!strcmp(p->name, argv[i])) {
325 struct shash_node *node;
328 SHASH_FOR_EACH (node, &command->options) {
329 const char *s = strstr(p->options, node->name);
330 int end = s ? s[strlen(node->name)] : EOF;
332 if (end != '=' && end != ',' && end != ' ' && end != '\0') {
333 vsctl_fatal("'%s' command has no '%s' option",
334 argv[i], node->name);
336 if ((end == '=') != (node->data != NULL)) {
338 vsctl_fatal("missing argument to '%s' option on '%s' "
339 "command", node->name, argv[i]);
341 vsctl_fatal("'%s' option on '%s' does not accept an "
342 "argument", node->name, argv[i]);
347 n_arg = argc - i - 1;
348 if (n_arg < p->min_args) {
349 vsctl_fatal("'%s' command requires at least %d arguments",
350 p->name, p->min_args);
351 } else if (n_arg > p->max_args) {
354 for (j = i + 1; j < argc; j++) {
355 if (argv[j][0] == '-') {
356 vsctl_fatal("'%s' command takes at most %d arguments "
357 "(note that options must precede command "
358 "names and follow a \"--\" argument)",
359 p->name, p->max_args);
363 vsctl_fatal("'%s' command takes at most %d arguments",
364 p->name, p->max_args);
367 command->argc = n_arg + 1;
368 command->argv = &argv[i];
374 vsctl_fatal("unknown command '%s'; use --help for help", argv[i]);
378 vsctl_fatal(const char *format, ...)
383 va_start(args, format);
384 message = xvasprintf(format, args);
387 vlog_set_levels(&VLM_vsctl, VLF_CONSOLE, VLL_EMER);
388 VLOG_ERR("%s", message);
389 ovs_error(0, "%s", message);
390 vsctl_exit(EXIT_FAILURE);
393 /* Frees the current transaction and the underlying IDL and then calls
396 * Freeing the transaction and the IDL is not strictly necessary, but it makes
397 * for a clean memory leak report from valgrind in the normal case. That makes
398 * it easier to notice real memory leaks. */
400 vsctl_exit(int status)
403 ovsdb_idl_txn_abort(the_idl_txn);
404 ovsdb_idl_txn_destroy(the_idl_txn);
406 ovsdb_idl_destroy(the_idl);
414 %s: ovs-vswitchd management utility\n\
415 usage: %s [OPTIONS] COMMAND [ARG...]\n\
418 add-br BRIDGE create a new bridge named BRIDGE\n\
419 add-br BRIDGE PARENT VLAN create new fake BRIDGE in PARENT on VLAN\n\
420 del-br BRIDGE delete BRIDGE and all of its ports\n\
421 list-br print the names of all the bridges\n\
422 br-exists BRIDGE test whether BRIDGE exists\n\
423 br-to-vlan BRIDGE print the VLAN which BRIDGE is on\n\
424 br-to-parent BRIDGE print the parent of BRIDGE\n\
425 br-set-external-id BRIDGE KEY VALUE set KEY on BRIDGE to VALUE\n\
426 br-set-external-id BRIDGE KEY unset KEY on BRIDGE\n\
427 br-get-external-id BRIDGE KEY print value of KEY on BRIDGE\n\
428 br-get-external-id BRIDGE list key-value pairs on BRIDGE\n\
431 list-ports BRIDGE print the names of all the ports on BRIDGE\n\
432 add-port BRIDGE PORT add network device PORT to BRIDGE\n\
433 add-bond BRIDGE PORT IFACE... add bonded port PORT in BRIDGE from IFACES\n\
434 del-port [BRIDGE] PORT delete PORT (which may be bonded) from BRIDGE\n\
435 port-to-br PORT print name of bridge that contains PORT\n\
436 A bond is considered to be a single port.\n\
438 Interface commands (a bond consists of multiple interfaces):\n\
439 list-ifaces BRIDGE print the names of all interfaces on BRIDGE\n\
440 iface-to-br IFACE print name of bridge that contains IFACE\n\
442 Controller commands:\n\
443 get-controller [BRIDGE] print the controller for BRIDGE\n\
444 del-controller [BRIDGE] delete the controller for BRIDGE\n\
445 set-controller [BRIDGE] TARGET set the controller for BRIDGE to TARGET\n\
446 get-fail-mode [BRIDGE] print the fail-mode for BRIDGE\n\
447 del-fail-mode [BRIDGE] delete the fail-mode for BRIDGE\n\
448 set-fail-mode [BRIDGE] MODE set the fail-mode for BRIDGE to MODE\n\
451 get-ssl print the SSL configuration\n\
452 del-ssl delete the SSL configuration\n\
453 set-ssl PRIV-KEY CERT CA-CERT set the SSL configuration\n\
456 emer-reset reset switch to known good state\n\
458 Database commands:\n\
459 list TBL [REC] list RECord (or all records) in TBL\n\
460 get TBL REC COL[:KEY] print values of COLumns in RECord in TBL\n\
461 set TBL REC COL[:KEY]=VALUE set COLumn values in RECord in TBL\n\
462 add TBL REC COL [KEY=]VALUE add (KEY=)VALUE to COLumn in RECord in TBL\n\
463 remove TBL REC COL [KEY=]VALUE remove (KEY=)VALUE from COLumn\n\
464 clear TBL REC COL clear values from COLumn in RECord in TBL\n\
465 create TBL COL[:KEY]=VALUE create and initialize new record\n\
466 destroy TBL REC delete RECord from TBL\n\
467 wait-until TBL REC [COL[:KEY]=VALUE] wait until condition is true\n\
468 Potentially unsafe database commands require --force option.\n\
471 --db=DATABASE connect to DATABASE\n\
473 --oneline print exactly one line of output per command\n",
474 program_name, program_name, default_db());
478 -h, --help display this help message\n\
479 -V, --version display version information\n");
488 def = xasprintf("unix:%s/db.sock", ovs_rundir);
493 struct vsctl_context {
497 struct shash options;
499 /* Modifiable state. */
501 struct ovsdb_idl *idl;
502 struct ovsdb_idl_txn *txn;
503 struct ovsdb_symbol_table *symtab;
504 const struct ovsrec_open_vswitch *ovs;
506 /* A command may set this member to true if some prerequisite is not met
507 * and the caller should wait for something to change and then retry. */
511 struct vsctl_bridge {
512 struct ovsrec_bridge *br_cfg;
514 struct ovsrec_controller **ctrl;
516 struct vsctl_bridge *parent;
521 struct ovsrec_port *port_cfg;
522 struct vsctl_bridge *bridge;
526 struct ovsrec_interface *iface_cfg;
527 struct vsctl_port *port;
531 struct shash bridges;
534 struct ovsrec_controller **ctrl;
539 vsctl_context_to_string(const struct vsctl_context *ctx)
541 const struct shash_node *node;
547 SHASH_FOR_EACH (node, &ctx->options) {
548 svec_add(&words, node->name);
550 for (i = 0; i < ctx->argc; i++) {
551 svec_add(&words, ctx->argv[i]);
553 svec_terminate(&words);
555 s = process_escape_args(words.names);
557 svec_destroy(&words);
562 static struct vsctl_bridge *
563 add_bridge(struct vsctl_info *b,
564 struct ovsrec_bridge *br_cfg, const char *name,
565 struct vsctl_bridge *parent, int vlan)
567 struct vsctl_bridge *br = xmalloc(sizeof *br);
569 br->name = xstrdup(name);
573 br->ctrl = parent->br_cfg->controller;
574 br->n_ctrl = parent->br_cfg->n_controller;
576 br->ctrl = br_cfg->controller;
577 br->n_ctrl = br_cfg->n_controller;
579 shash_add(&b->bridges, br->name, br);
584 port_is_fake_bridge(const struct ovsrec_port *port_cfg)
586 return (port_cfg->fake_bridge
588 && *port_cfg->tag >= 1 && *port_cfg->tag <= 4095);
591 static struct vsctl_bridge *
592 find_vlan_bridge(struct vsctl_info *info,
593 struct vsctl_bridge *parent, int vlan)
595 struct shash_node *node;
597 SHASH_FOR_EACH (node, &info->bridges) {
598 struct vsctl_bridge *br = node->data;
599 if (br->parent == parent && br->vlan == vlan) {
608 free_info(struct vsctl_info *info)
610 struct shash_node *node;
612 SHASH_FOR_EACH (node, &info->bridges) {
613 struct vsctl_bridge *bridge = node->data;
617 shash_destroy(&info->bridges);
619 shash_destroy_free_data(&info->ports);
620 shash_destroy_free_data(&info->ifaces);
624 get_info(const struct ovsrec_open_vswitch *ovs, struct vsctl_info *info)
626 struct shash bridges, ports;
629 shash_init(&info->bridges);
630 shash_init(&info->ports);
631 shash_init(&info->ifaces);
633 info->ctrl = ovs->controller;
634 info->n_ctrl = ovs->n_controller;
636 shash_init(&bridges);
638 for (i = 0; i < ovs->n_bridges; i++) {
639 struct ovsrec_bridge *br_cfg = ovs->bridges[i];
640 struct vsctl_bridge *br;
643 if (!shash_add_once(&bridges, br_cfg->name, NULL)) {
644 VLOG_WARN("%s: database contains duplicate bridge name",
648 br = add_bridge(info, br_cfg, br_cfg->name, NULL, 0);
653 for (j = 0; j < br_cfg->n_ports; j++) {
654 struct ovsrec_port *port_cfg = br_cfg->ports[j];
656 if (!shash_add_once(&ports, port_cfg->name, NULL)) {
657 VLOG_WARN("%s: database contains duplicate port name",
662 if (port_is_fake_bridge(port_cfg)
663 && shash_add_once(&bridges, port_cfg->name, NULL)) {
664 add_bridge(info, NULL, port_cfg->name, br, *port_cfg->tag);
668 shash_destroy(&bridges);
669 shash_destroy(&ports);
671 shash_init(&bridges);
673 for (i = 0; i < ovs->n_bridges; i++) {
674 struct ovsrec_bridge *br_cfg = ovs->bridges[i];
675 struct vsctl_bridge *br;
678 if (!shash_add_once(&bridges, br_cfg->name, NULL)) {
681 br = shash_find_data(&info->bridges, br_cfg->name);
682 for (j = 0; j < br_cfg->n_ports; j++) {
683 struct ovsrec_port *port_cfg = br_cfg->ports[j];
684 struct vsctl_port *port;
687 if (!shash_add_once(&ports, port_cfg->name, NULL)) {
691 if (port_is_fake_bridge(port_cfg)
692 && !shash_add_once(&bridges, port_cfg->name, NULL)) {
696 port = xmalloc(sizeof *port);
697 port->port_cfg = port_cfg;
699 && *port_cfg->tag >= 1 && *port_cfg->tag <= 4095) {
700 port->bridge = find_vlan_bridge(info, br, *port_cfg->tag);
707 shash_add(&info->ports, port_cfg->name, port);
709 for (k = 0; k < port_cfg->n_interfaces; k++) {
710 struct ovsrec_interface *iface_cfg = port_cfg->interfaces[k];
711 struct vsctl_iface *iface;
713 if (shash_find(&info->ifaces, iface_cfg->name)) {
714 VLOG_WARN("%s: database contains duplicate interface name",
719 iface = xmalloc(sizeof *iface);
720 iface->iface_cfg = iface_cfg;
722 shash_add(&info->ifaces, iface_cfg->name, iface);
726 shash_destroy(&bridges);
727 shash_destroy(&ports);
731 check_conflicts(struct vsctl_info *info, const char *name,
734 struct vsctl_iface *iface;
735 struct vsctl_port *port;
737 if (shash_find(&info->bridges, name)) {
738 vsctl_fatal("%s because a bridge named %s already exists",
742 port = shash_find_data(&info->ports, name);
744 vsctl_fatal("%s because a port named %s already exists on "
745 "bridge %s", msg, name, port->bridge->name);
748 iface = shash_find_data(&info->ifaces, name);
750 vsctl_fatal("%s because an interface named %s already exists "
751 "on bridge %s", msg, name, iface->port->bridge->name);
757 static struct vsctl_bridge *
758 find_bridge(struct vsctl_info *info, const char *name, bool must_exist)
760 struct vsctl_bridge *br = shash_find_data(&info->bridges, name);
761 if (must_exist && !br) {
762 vsctl_fatal("no bridge named %s", name);
767 static struct vsctl_bridge *
768 find_real_bridge(struct vsctl_info *info, const char *name, bool must_exist)
770 struct vsctl_bridge *br = find_bridge(info, name, must_exist);
771 if (br && br->parent) {
772 vsctl_fatal("%s is a fake bridge", name);
777 static struct vsctl_port *
778 find_port(struct vsctl_info *info, const char *name, bool must_exist)
780 struct vsctl_port *port = shash_find_data(&info->ports, name);
781 if (port && !strcmp(name, port->bridge->name)) {
784 if (must_exist && !port) {
785 vsctl_fatal("no port named %s", name);
790 static struct vsctl_iface *
791 find_iface(struct vsctl_info *info, const char *name, bool must_exist)
793 struct vsctl_iface *iface = shash_find_data(&info->ifaces, name);
794 if (iface && !strcmp(name, iface->port->bridge->name)) {
797 if (must_exist && !iface) {
798 vsctl_fatal("no interface named %s", name);
804 bridge_insert_port(struct ovsrec_bridge *br, struct ovsrec_port *port)
806 struct ovsrec_port **ports;
809 ports = xmalloc(sizeof *br->ports * (br->n_ports + 1));
810 for (i = 0; i < br->n_ports; i++) {
811 ports[i] = br->ports[i];
813 ports[br->n_ports] = port;
814 ovsrec_bridge_set_ports(br, ports, br->n_ports + 1);
819 bridge_delete_port(struct ovsrec_bridge *br, struct ovsrec_port *port)
821 struct ovsrec_port **ports;
824 ports = xmalloc(sizeof *br->ports * br->n_ports);
825 for (i = n = 0; i < br->n_ports; i++) {
826 if (br->ports[i] != port) {
827 ports[n++] = br->ports[i];
830 ovsrec_bridge_set_ports(br, ports, n);
835 ovs_insert_bridge(const struct ovsrec_open_vswitch *ovs,
836 struct ovsrec_bridge *bridge)
838 struct ovsrec_bridge **bridges;
841 bridges = xmalloc(sizeof *ovs->bridges * (ovs->n_bridges + 1));
842 for (i = 0; i < ovs->n_bridges; i++) {
843 bridges[i] = ovs->bridges[i];
845 bridges[ovs->n_bridges] = bridge;
846 ovsrec_open_vswitch_set_bridges(ovs, bridges, ovs->n_bridges + 1);
851 ovs_delete_bridge(const struct ovsrec_open_vswitch *ovs,
852 struct ovsrec_bridge *bridge)
854 struct ovsrec_bridge **bridges;
857 bridges = xmalloc(sizeof *ovs->bridges * ovs->n_bridges);
858 for (i = n = 0; i < ovs->n_bridges; i++) {
859 if (ovs->bridges[i] != bridge) {
860 bridges[n++] = ovs->bridges[i];
863 ovsrec_open_vswitch_set_bridges(ovs, bridges, n);
868 cmd_init(struct vsctl_context *ctx OVS_UNUSED)
873 cmd_emer_reset(struct vsctl_context *ctx)
875 const struct ovsdb_idl *idl = ctx->idl;
876 const struct ovsrec_bridge *br;
877 const struct ovsrec_port *port;
878 const struct ovsrec_interface *iface;
879 const struct ovsrec_mirror *mirror, *next_mirror;
880 const struct ovsrec_controller *ctrl, *next_ctrl;
881 const struct ovsrec_netflow *nf, *next_nf;
882 const struct ovsrec_ssl *ssl, *next_ssl;
883 const struct ovsrec_sflow *sflow, *next_sflow;
886 /* Reset the Open_vSwitch table. */
887 ovsrec_open_vswitch_set_managers(ctx->ovs, NULL, 0);
888 ovsrec_open_vswitch_set_controller(ctx->ovs, NULL, 0);
889 ovsrec_open_vswitch_set_ssl(ctx->ovs, NULL);
891 OVSREC_BRIDGE_FOR_EACH (br, idl) {
893 char *hw_key = "hwaddr";
896 ovsrec_bridge_set_controller(br, NULL, 0);
897 ovsrec_bridge_set_mirrors(br, NULL, 0);
898 ovsrec_bridge_set_netflow(br, NULL);
899 ovsrec_bridge_set_sflow(br, NULL);
900 ovsrec_bridge_set_flood_vlans(br, NULL, 0);
902 /* We only want to save the "hwaddr" key from other_config. */
903 for (i=0; i < br->n_other_config; i++) {
904 if (!strcmp(br->key_other_config[i], hw_key)) {
905 hw_val = br->value_other_config[i];
910 char *val = xstrdup(hw_val);
911 ovsrec_bridge_set_other_config(br, &hw_key, &val, 1);
914 ovsrec_bridge_set_other_config(br, NULL, NULL, 0);
918 OVSREC_PORT_FOR_EACH (port, idl) {
919 ovsrec_port_set_other_config(port, NULL, NULL, 0);
922 OVSREC_INTERFACE_FOR_EACH (iface, idl) {
923 /* xxx What do we do about gre/patch devices created by mgr? */
925 ovsrec_interface_set_ingress_policing_rate(iface, 0);
926 ovsrec_interface_set_ingress_policing_burst(iface, 0);
929 OVSREC_MIRROR_FOR_EACH_SAFE (mirror, next_mirror, idl) {
930 ovsrec_mirror_delete(mirror);
933 OVSREC_CONTROLLER_FOR_EACH_SAFE (ctrl, next_ctrl, idl) {
934 ovsrec_controller_delete(ctrl);
937 OVSREC_NETFLOW_FOR_EACH_SAFE (nf, next_nf, idl) {
938 ovsrec_netflow_delete(nf);
941 OVSREC_SSL_FOR_EACH_SAFE (ssl, next_ssl, idl) {
942 ovsrec_ssl_delete(ssl);
945 OVSREC_SFLOW_FOR_EACH_SAFE (sflow, next_sflow, idl) {
946 ovsrec_sflow_delete(sflow);
951 cmd_add_br(struct vsctl_context *ctx)
953 bool may_exist = shash_find(&ctx->options, "--may-exist") != 0;
954 const char *br_name, *parent_name;
955 struct vsctl_info info;
958 br_name = ctx->argv[1];
959 if (ctx->argc == 2) {
962 } else if (ctx->argc == 4) {
963 parent_name = ctx->argv[2];
964 vlan = atoi(ctx->argv[3]);
965 if (vlan < 1 || vlan > 4095) {
966 vsctl_fatal("%s: vlan must be between 1 and 4095", ctx->argv[0]);
969 vsctl_fatal("'%s' command takes exactly 1 or 3 arguments",
973 get_info(ctx->ovs, &info);
975 struct vsctl_bridge *br;
977 br = find_bridge(&info, br_name, false);
981 vsctl_fatal("\"--may-exist add-br %s\" but %s is "
982 "a VLAN bridge for VLAN %d",
983 br_name, br_name, br->vlan);
987 vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
988 "is not a VLAN bridge",
989 br_name, parent_name, vlan, br_name);
990 } else if (strcmp(br->parent->name, parent_name)) {
991 vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
992 "has the wrong parent %s",
993 br_name, parent_name, vlan,
994 br_name, br->parent->name);
995 } else if (br->vlan != vlan) {
996 vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
997 "is a VLAN bridge for the wrong VLAN %d",
998 br_name, parent_name, vlan, br_name, br->vlan);
1004 check_conflicts(&info, br_name,
1005 xasprintf("cannot create a bridge named %s", br_name));
1008 struct ovsrec_port *port;
1009 struct ovsrec_interface *iface;
1010 struct ovsrec_bridge *br;
1012 iface = ovsrec_interface_insert(ctx->txn);
1013 ovsrec_interface_set_name(iface, br_name);
1015 port = ovsrec_port_insert(ctx->txn);
1016 ovsrec_port_set_name(port, br_name);
1017 ovsrec_port_set_interfaces(port, &iface, 1);
1019 br = ovsrec_bridge_insert(ctx->txn);
1020 ovsrec_bridge_set_name(br, br_name);
1021 ovsrec_bridge_set_ports(br, &port, 1);
1023 ovs_insert_bridge(ctx->ovs, br);
1025 struct vsctl_bridge *parent;
1026 struct ovsrec_port *port;
1027 struct ovsrec_interface *iface;
1028 struct ovsrec_bridge *br;
1031 parent = find_bridge(&info, parent_name, false);
1032 if (parent && parent->vlan) {
1033 vsctl_fatal("cannot create bridge with fake bridge as parent");
1036 vsctl_fatal("parent bridge %s does not exist", parent_name);
1038 br = parent->br_cfg;
1040 iface = ovsrec_interface_insert(ctx->txn);
1041 ovsrec_interface_set_name(iface, br_name);
1042 ovsrec_interface_set_type(iface, "internal");
1044 port = ovsrec_port_insert(ctx->txn);
1045 ovsrec_port_set_name(port, br_name);
1046 ovsrec_port_set_interfaces(port, &iface, 1);
1047 ovsrec_port_set_fake_bridge(port, true);
1048 ovsrec_port_set_tag(port, &tag, 1);
1050 bridge_insert_port(br, port);
1057 del_port(struct vsctl_info *info, struct vsctl_port *port)
1059 struct shash_node *node;
1061 SHASH_FOR_EACH (node, &info->ifaces) {
1062 struct vsctl_iface *iface = node->data;
1063 if (iface->port == port) {
1064 ovsrec_interface_delete(iface->iface_cfg);
1067 ovsrec_port_delete(port->port_cfg);
1069 bridge_delete_port((port->bridge->parent
1070 ? port->bridge->parent->br_cfg
1071 : port->bridge->br_cfg), port->port_cfg);
1075 cmd_del_br(struct vsctl_context *ctx)
1077 bool must_exist = !shash_find(&ctx->options, "--if-exists");
1078 struct vsctl_bridge *bridge;
1079 struct vsctl_info info;
1081 get_info(ctx->ovs, &info);
1082 bridge = find_bridge(&info, ctx->argv[1], must_exist);
1084 struct shash_node *node;
1086 SHASH_FOR_EACH (node, &info.ports) {
1087 struct vsctl_port *port = node->data;
1088 if (port->bridge == bridge || port->bridge->parent == bridge
1089 || !strcmp(port->port_cfg->name, bridge->name)) {
1090 del_port(&info, port);
1093 if (bridge->br_cfg) {
1094 ovsrec_bridge_delete(bridge->br_cfg);
1095 ovs_delete_bridge(ctx->ovs, bridge->br_cfg);
1102 output_sorted(struct svec *svec, struct ds *output)
1108 SVEC_FOR_EACH (i, name, svec) {
1109 ds_put_format(output, "%s\n", name);
1114 cmd_list_br(struct vsctl_context *ctx)
1116 struct shash_node *node;
1117 struct vsctl_info info;
1118 struct svec bridges;
1120 get_info(ctx->ovs, &info);
1122 svec_init(&bridges);
1123 SHASH_FOR_EACH (node, &info.bridges) {
1124 struct vsctl_bridge *br = node->data;
1125 svec_add(&bridges, br->name);
1127 output_sorted(&bridges, &ctx->output);
1128 svec_destroy(&bridges);
1134 cmd_br_exists(struct vsctl_context *ctx)
1136 struct vsctl_info info;
1138 get_info(ctx->ovs, &info);
1139 if (!find_bridge(&info, ctx->argv[1], false)) {
1145 /* Returns true if 'b_prefix' (of length 'b_prefix_len') concatenated with 'b'
1146 * equals 'a', false otherwise. */
1148 key_matches(const char *a,
1149 const char *b_prefix, size_t b_prefix_len, const char *b)
1151 return !strncmp(a, b_prefix, b_prefix_len) && !strcmp(a + b_prefix_len, b);
1155 set_external_id(char **old_keys, char **old_values, size_t old_n,
1156 char *key, char *value,
1157 char ***new_keysp, char ***new_valuesp, size_t *new_np)
1164 new_keys = xmalloc(sizeof *new_keys * (old_n + 1));
1165 new_values = xmalloc(sizeof *new_values * (old_n + 1));
1167 for (i = 0; i < old_n; i++) {
1168 if (strcmp(key, old_keys[i])) {
1169 new_keys[new_n] = old_keys[i];
1170 new_values[new_n] = old_values[i];
1175 new_keys[new_n] = key;
1176 new_values[new_n] = value;
1179 *new_keysp = new_keys;
1180 *new_valuesp = new_values;
1185 cmd_br_set_external_id(struct vsctl_context *ctx)
1187 struct vsctl_info info;
1188 struct vsctl_bridge *bridge;
1189 char **keys, **values;
1192 get_info(ctx->ovs, &info);
1193 bridge = find_bridge(&info, ctx->argv[1], true);
1194 if (bridge->br_cfg) {
1195 set_external_id(bridge->br_cfg->key_external_ids,
1196 bridge->br_cfg->value_external_ids,
1197 bridge->br_cfg->n_external_ids,
1198 ctx->argv[2], ctx->argc >= 4 ? ctx->argv[3] : NULL,
1199 &keys, &values, &n);
1200 ovsrec_bridge_set_external_ids(bridge->br_cfg, keys, values, n);
1202 char *key = xasprintf("fake-bridge-%s", ctx->argv[2]);
1203 struct vsctl_port *port = shash_find_data(&info.ports, ctx->argv[1]);
1204 set_external_id(port->port_cfg->key_external_ids,
1205 port->port_cfg->value_external_ids,
1206 port->port_cfg->n_external_ids,
1207 key, ctx->argc >= 4 ? ctx->argv[3] : NULL,
1208 &keys, &values, &n);
1209 ovsrec_port_set_external_ids(port->port_cfg, keys, values, n);
1219 get_external_id(char **keys, char **values, size_t n,
1220 const char *prefix, const char *key,
1223 size_t prefix_len = strlen(prefix);
1228 for (i = 0; i < n; i++) {
1229 if (!key && !strncmp(keys[i], prefix, prefix_len)) {
1230 svec_add_nocopy(&svec, xasprintf("%s=%s",
1231 keys[i] + prefix_len, values[i]));
1232 } else if (key_matches(keys[i], prefix, prefix_len, key)) {
1233 svec_add(&svec, values[i]);
1237 output_sorted(&svec, output);
1238 svec_destroy(&svec);
1242 cmd_br_get_external_id(struct vsctl_context *ctx)
1244 struct vsctl_info info;
1245 struct vsctl_bridge *bridge;
1247 get_info(ctx->ovs, &info);
1248 bridge = find_bridge(&info, ctx->argv[1], true);
1249 if (bridge->br_cfg) {
1250 get_external_id(bridge->br_cfg->key_external_ids,
1251 bridge->br_cfg->value_external_ids,
1252 bridge->br_cfg->n_external_ids,
1253 "", ctx->argc >= 3 ? ctx->argv[2] : NULL,
1256 struct vsctl_port *port = shash_find_data(&info.ports, ctx->argv[1]);
1257 get_external_id(port->port_cfg->key_external_ids,
1258 port->port_cfg->value_external_ids,
1259 port->port_cfg->n_external_ids,
1260 "fake-bridge-", ctx->argc >= 3 ? ctx->argv[2] : NULL, &ctx->output);
1267 cmd_list_ports(struct vsctl_context *ctx)
1269 struct vsctl_bridge *br;
1270 struct shash_node *node;
1271 struct vsctl_info info;
1274 get_info(ctx->ovs, &info);
1275 br = find_bridge(&info, ctx->argv[1], true);
1278 SHASH_FOR_EACH (node, &info.ports) {
1279 struct vsctl_port *port = node->data;
1281 if (strcmp(port->port_cfg->name, br->name) && br == port->bridge) {
1282 svec_add(&ports, port->port_cfg->name);
1285 output_sorted(&ports, &ctx->output);
1286 svec_destroy(&ports);
1292 add_port(struct vsctl_context *ctx,
1293 const char *br_name, const char *port_name,
1294 bool may_exist, bool fake_iface,
1295 char *iface_names[], int n_ifaces,
1296 char *settings[], int n_settings)
1298 struct vsctl_info info;
1299 struct vsctl_bridge *bridge;
1300 struct ovsrec_interface **ifaces;
1301 struct ovsrec_port *port;
1304 get_info(ctx->ovs, &info);
1306 struct vsctl_port *port;
1308 port = find_port(&info, port_name, false);
1310 struct svec want_names, have_names;
1313 svec_init(&want_names);
1314 for (i = 0; i < n_ifaces; i++) {
1315 svec_add(&want_names, iface_names[i]);
1317 svec_sort(&want_names);
1319 svec_init(&have_names);
1320 for (i = 0; i < port->port_cfg->n_interfaces; i++) {
1321 svec_add(&have_names, port->port_cfg->interfaces[i]->name);
1323 svec_sort(&have_names);
1325 if (strcmp(port->bridge->name, br_name)) {
1326 char *command = vsctl_context_to_string(ctx);
1327 vsctl_fatal("\"%s\" but %s is actually attached to bridge %s",
1328 command, port_name, port->bridge->name);
1331 if (!svec_equal(&want_names, &have_names)) {
1332 char *have_names_string = svec_join(&have_names, ", ", "");
1333 char *command = vsctl_context_to_string(ctx);
1335 vsctl_fatal("\"%s\" but %s actually has interface(s) %s",
1336 command, port_name, have_names_string);
1339 svec_destroy(&want_names);
1340 svec_destroy(&have_names);
1345 check_conflicts(&info, port_name,
1346 xasprintf("cannot create a port named %s", port_name));
1347 for (i = 0; i < n_ifaces; i++) {
1348 check_conflicts(&info, iface_names[i],
1349 xasprintf("cannot create an interface named %s",
1352 bridge = find_bridge(&info, br_name, true);
1354 ifaces = xmalloc(n_ifaces * sizeof *ifaces);
1355 for (i = 0; i < n_ifaces; i++) {
1356 ifaces[i] = ovsrec_interface_insert(ctx->txn);
1357 ovsrec_interface_set_name(ifaces[i], iface_names[i]);
1360 port = ovsrec_port_insert(ctx->txn);
1361 ovsrec_port_set_name(port, port_name);
1362 ovsrec_port_set_interfaces(port, ifaces, n_ifaces);
1363 ovsrec_port_set_bond_fake_iface(port, fake_iface);
1367 int64_t tag = bridge->vlan;
1368 ovsrec_port_set_tag(port, &tag, 1);
1371 for (i = 0; i < n_settings; i++) {
1372 set_column(get_table("Port"), &port->header_, settings[i],
1376 bridge_insert_port((bridge->parent ? bridge->parent->br_cfg
1377 : bridge->br_cfg), port);
1383 cmd_add_port(struct vsctl_context *ctx)
1385 bool may_exist = shash_find(&ctx->options, "--may-exist") != 0;
1387 add_port(ctx, ctx->argv[1], ctx->argv[2], may_exist, false,
1388 &ctx->argv[2], 1, &ctx->argv[3], ctx->argc - 3);
1392 cmd_add_bond(struct vsctl_context *ctx)
1394 bool may_exist = shash_find(&ctx->options, "--may-exist") != 0;
1395 bool fake_iface = shash_find(&ctx->options, "--fake-iface");
1399 n_ifaces = ctx->argc - 3;
1400 for (i = 3; i < ctx->argc; i++) {
1401 if (strchr(ctx->argv[i], '=')) {
1407 vsctl_fatal("add-bond requires at least 2 interfaces, but only "
1408 "%d were specified", n_ifaces);
1411 add_port(ctx, ctx->argv[1], ctx->argv[2], may_exist, fake_iface,
1412 &ctx->argv[3], n_ifaces,
1413 &ctx->argv[n_ifaces + 3], ctx->argc - 3 - n_ifaces);
1417 cmd_del_port(struct vsctl_context *ctx)
1419 bool must_exist = !shash_find(&ctx->options, "--if-exists");
1420 bool with_iface = shash_find(&ctx->options, "--with-iface") != NULL;
1421 struct vsctl_port *port;
1422 struct vsctl_info info;
1424 get_info(ctx->ovs, &info);
1426 port = find_port(&info, ctx->argv[ctx->argc - 1], must_exist);
1428 const char *target = ctx->argv[ctx->argc - 1];
1429 struct vsctl_iface *iface;
1431 port = find_port(&info, target, false);
1433 iface = find_iface(&info, target, false);
1438 if (must_exist && !port) {
1439 vsctl_fatal("no port or interface named %s", target);
1444 if (ctx->argc == 3) {
1445 struct vsctl_bridge *bridge;
1447 bridge = find_bridge(&info, ctx->argv[1], true);
1448 if (port->bridge != bridge) {
1449 if (port->bridge->parent == bridge) {
1450 vsctl_fatal("bridge %s does not have a port %s (although "
1451 "its parent bridge %s does)",
1452 ctx->argv[1], ctx->argv[2],
1453 bridge->parent->name);
1455 vsctl_fatal("bridge %s does not have a port %s",
1456 ctx->argv[1], ctx->argv[2]);
1461 del_port(&info, port);
1468 cmd_port_to_br(struct vsctl_context *ctx)
1470 struct vsctl_port *port;
1471 struct vsctl_info info;
1473 get_info(ctx->ovs, &info);
1474 port = find_port(&info, ctx->argv[1], true);
1475 ds_put_format(&ctx->output, "%s\n", port->bridge->name);
1480 cmd_br_to_vlan(struct vsctl_context *ctx)
1482 struct vsctl_bridge *bridge;
1483 struct vsctl_info info;
1485 get_info(ctx->ovs, &info);
1486 bridge = find_bridge(&info, ctx->argv[1], true);
1487 ds_put_format(&ctx->output, "%d\n", bridge->vlan);
1492 cmd_br_to_parent(struct vsctl_context *ctx)
1494 struct vsctl_bridge *bridge;
1495 struct vsctl_info info;
1497 get_info(ctx->ovs, &info);
1498 bridge = find_bridge(&info, ctx->argv[1], true);
1499 if (bridge->parent) {
1500 bridge = bridge->parent;
1502 ds_put_format(&ctx->output, "%s\n", bridge->name);
1507 cmd_list_ifaces(struct vsctl_context *ctx)
1509 struct vsctl_bridge *br;
1510 struct shash_node *node;
1511 struct vsctl_info info;
1514 get_info(ctx->ovs, &info);
1515 br = find_bridge(&info, ctx->argv[1], true);
1518 SHASH_FOR_EACH (node, &info.ifaces) {
1519 struct vsctl_iface *iface = node->data;
1521 if (strcmp(iface->iface_cfg->name, br->name)
1522 && br == iface->port->bridge) {
1523 svec_add(&ifaces, iface->iface_cfg->name);
1526 output_sorted(&ifaces, &ctx->output);
1527 svec_destroy(&ifaces);
1533 cmd_iface_to_br(struct vsctl_context *ctx)
1535 struct vsctl_iface *iface;
1536 struct vsctl_info info;
1538 get_info(ctx->ovs, &info);
1539 iface = find_iface(&info, ctx->argv[1], true);
1540 ds_put_format(&ctx->output, "%s\n", iface->port->bridge->name);
1544 /* Print targets of the 'n_controllers' in 'controllers' on the output for
1547 print_controllers(struct vsctl_context *ctx,
1548 struct ovsrec_controller **controllers,
1549 size_t n_controllers)
1551 /* Print the targets in sorted order for reproducibility. */
1552 struct svec targets;
1555 svec_init(&targets);
1556 for (i = 0; i < n_controllers; i++) {
1557 svec_add(&targets, controllers[i]->target);
1560 svec_sort(&targets);
1561 for (i = 0; i < targets.n; i++) {
1562 ds_put_format(&ctx->output, "%s\n", targets.names[i]);
1564 svec_destroy(&targets);
1568 cmd_get_controller(struct vsctl_context *ctx)
1570 struct vsctl_info info;
1572 get_info(ctx->ovs, &info);
1574 if (ctx->argc == 1 || !strcmp(ctx->argv[1], "default")) {
1575 print_controllers(ctx, info.ctrl, info.n_ctrl);
1577 struct vsctl_bridge *br = find_bridge(&info, ctx->argv[1], true);
1579 print_controllers(ctx, br->ctrl, br->n_ctrl);
1581 print_controllers(ctx, info.ctrl, info.n_ctrl);
1589 delete_controllers(struct ovsrec_controller **controllers,
1590 size_t n_controllers)
1594 for (i = 0; i < n_controllers; i++) {
1595 ovsrec_controller_delete(controllers[i]);
1600 cmd_del_controller(struct vsctl_context *ctx)
1602 struct vsctl_info info;
1604 get_info(ctx->ovs, &info);
1606 if (ctx->argc == 1 || !strcmp(ctx->argv[1], "default")) {
1608 delete_controllers(info.ctrl, info.n_ctrl);
1609 ovsrec_open_vswitch_set_controller(ctx->ovs, NULL, 0);
1612 struct vsctl_bridge *br = find_real_bridge(&info, ctx->argv[1], true);
1614 delete_controllers(br->ctrl, br->n_ctrl);
1615 ovsrec_bridge_set_controller(br->br_cfg, NULL, 0);
1622 static struct ovsrec_controller **
1623 insert_controllers(struct ovsdb_idl_txn *txn, char *targets[], size_t n)
1625 struct ovsrec_controller **controllers;
1628 controllers = xmalloc(n * sizeof *controllers);
1629 for (i = 0; i < n; i++) {
1630 controllers[i] = ovsrec_controller_insert(txn);
1631 ovsrec_controller_set_target(controllers[i], targets[i]);
1638 set_default_controllers(struct vsctl_context *ctx, char *targets[], size_t n)
1640 struct ovsrec_controller **controllers;
1642 delete_controllers(ctx->ovs->controller, ctx->ovs->n_controller);
1644 controllers = insert_controllers(ctx->txn, targets, n);
1645 ovsrec_open_vswitch_set_controller(ctx->ovs, controllers, n);
1650 cmd_set_controller(struct vsctl_context *ctx)
1652 struct vsctl_info info;
1654 get_info(ctx->ovs, &info);
1656 if (ctx->argc == 2) {
1657 /* Set one controller in the "Open_vSwitch" table. */
1658 set_default_controllers(ctx, &ctx->argv[1], 1);
1659 } else if (!strcmp(ctx->argv[1], "default")) {
1660 /* Set one or more controllers in the "Open_vSwitch" table. */
1661 set_default_controllers(ctx, &ctx->argv[2], ctx->argc - 2);
1663 /* Set one or more controllers for a particular bridge. */
1664 struct vsctl_bridge *br = find_real_bridge(&info, ctx->argv[1], true);
1665 struct ovsrec_controller **controllers;
1668 delete_controllers(br->ctrl, br->n_ctrl);
1671 controllers = insert_controllers(ctx->txn, &ctx->argv[2], n);
1672 ovsrec_bridge_set_controller(br->br_cfg, controllers, n);
1680 get_fail_mode(struct ovsrec_controller **controllers, size_t n_controllers)
1682 const char *fail_mode;
1686 for (i = 0; i < n_controllers; i++) {
1687 const char *s = controllers[i]->fail_mode;
1689 if (!strcmp(s, "secure")) {
1701 cmd_get_fail_mode(struct vsctl_context *ctx)
1703 struct vsctl_info info;
1704 const char *fail_mode = NULL;
1706 get_info(ctx->ovs, &info);
1708 if (ctx->argc == 1 || !strcmp(ctx->argv[1], "default")) {
1709 /* Return the fail-mode from the "Open_vSwitch" table */
1710 fail_mode = get_fail_mode(info.ctrl, info.n_ctrl);
1712 /* Return the fail-mode for a particular bridge. */
1713 struct vsctl_bridge *br = find_bridge(&info, ctx->argv[1], true);
1715 /* If no controller is defined for the requested bridge, fallback to
1716 * the "Open_vSwitch" table's controller. */
1717 fail_mode = (br->n_ctrl
1718 ? get_fail_mode(br->ctrl, br->n_ctrl)
1719 : get_fail_mode(info.ctrl, info.n_ctrl));
1722 if (fail_mode && strlen(fail_mode)) {
1723 ds_put_format(&ctx->output, "%s\n", fail_mode);
1730 set_fail_mode(struct ovsrec_controller **controllers, size_t n_controllers,
1731 const char *fail_mode)
1735 for (i = 0; i < n_controllers; i++) {
1736 ovsrec_controller_set_fail_mode(controllers[i], fail_mode);
1741 cmd_del_fail_mode(struct vsctl_context *ctx)
1743 struct vsctl_info info;
1745 get_info(ctx->ovs, &info);
1747 if (ctx->argc == 1 || !strcmp(ctx->argv[1], "default")) {
1748 set_fail_mode(info.ctrl, info.n_ctrl, NULL);
1750 struct vsctl_bridge *br = find_real_bridge(&info, ctx->argv[1], true);
1752 set_fail_mode(br->ctrl, br->n_ctrl, NULL);
1759 cmd_set_fail_mode(struct vsctl_context *ctx)
1761 struct vsctl_info info;
1763 const char *fail_mode;
1765 get_info(ctx->ovs, &info);
1767 if (ctx->argc == 2) {
1769 fail_mode = ctx->argv[1];
1771 bridge = ctx->argv[1];
1772 fail_mode = ctx->argv[2];
1775 if (strcmp(fail_mode, "standalone") && strcmp(fail_mode, "secure")) {
1776 vsctl_fatal("fail-mode must be \"standalone\" or \"secure\"");
1779 if (!strcmp(bridge, "default")) {
1780 /* Set the fail-mode in the "Open_vSwitch" table. */
1782 vsctl_fatal("no controller declared");
1784 set_fail_mode(info.ctrl, info.n_ctrl, fail_mode);
1786 struct vsctl_bridge *br = find_real_bridge(&info, bridge, true);
1789 vsctl_fatal("no controller declared for %s", br->name);
1791 set_fail_mode(br->ctrl, br->n_ctrl, fail_mode);
1798 cmd_get_ssl(struct vsctl_context *ctx)
1800 struct ovsrec_ssl *ssl = ctx->ovs->ssl;
1803 ds_put_format(&ctx->output, "Private key: %s\n", ssl->private_key);
1804 ds_put_format(&ctx->output, "Certificate: %s\n", ssl->certificate);
1805 ds_put_format(&ctx->output, "CA Certificate: %s\n", ssl->ca_cert);
1806 ds_put_format(&ctx->output, "Bootstrap: %s\n",
1807 ssl->bootstrap_ca_cert ? "true" : "false");
1812 cmd_del_ssl(struct vsctl_context *ctx)
1814 struct ovsrec_ssl *ssl = ctx->ovs->ssl;
1817 ovsrec_ssl_delete(ssl);
1818 ovsrec_open_vswitch_set_ssl(ctx->ovs, NULL);
1823 cmd_set_ssl(struct vsctl_context *ctx)
1825 bool bootstrap = shash_find(&ctx->options, "--bootstrap");
1826 struct ovsrec_ssl *ssl = ctx->ovs->ssl;
1829 ovsrec_ssl_delete(ssl);
1831 ssl = ovsrec_ssl_insert(ctx->txn);
1833 ovsrec_ssl_set_private_key(ssl, ctx->argv[1]);
1834 ovsrec_ssl_set_certificate(ssl, ctx->argv[2]);
1835 ovsrec_ssl_set_ca_cert(ssl, ctx->argv[3]);
1837 ovsrec_ssl_set_bootstrap_ca_cert(ssl, bootstrap);
1839 ovsrec_open_vswitch_set_ssl(ctx->ovs, ssl);
1842 /* Parameter commands. */
1844 struct vsctl_row_id {
1845 const struct ovsdb_idl_table_class *table;
1846 const struct ovsdb_idl_column *name_column;
1847 const struct ovsdb_idl_column *uuid_column;
1850 struct vsctl_table_class {
1851 struct ovsdb_idl_table_class *class;
1852 struct vsctl_row_id row_ids[2];
1855 static const struct vsctl_table_class tables[] = {
1856 {&ovsrec_table_bridge,
1857 {{&ovsrec_table_bridge, &ovsrec_bridge_col_name, NULL},
1858 {NULL, NULL, NULL}}},
1860 {&ovsrec_table_controller,
1861 {{&ovsrec_table_bridge,
1862 &ovsrec_bridge_col_name,
1863 &ovsrec_bridge_col_controller},
1864 {&ovsrec_table_open_vswitch,
1866 &ovsrec_open_vswitch_col_controller}}},
1868 {&ovsrec_table_interface,
1869 {{&ovsrec_table_interface, &ovsrec_interface_col_name, NULL},
1870 {NULL, NULL, NULL}}},
1872 {&ovsrec_table_mirror,
1873 {{&ovsrec_table_mirror, &ovsrec_mirror_col_name, NULL},
1874 {NULL, NULL, NULL}}},
1876 {&ovsrec_table_netflow,
1877 {{&ovsrec_table_bridge,
1878 &ovsrec_bridge_col_name,
1879 &ovsrec_bridge_col_netflow},
1880 {NULL, NULL, NULL}}},
1882 {&ovsrec_table_open_vswitch,
1883 {{&ovsrec_table_open_vswitch, NULL, NULL},
1884 {NULL, NULL, NULL}}},
1886 {&ovsrec_table_port,
1887 {{&ovsrec_table_port, &ovsrec_port_col_name, NULL},
1888 {NULL, NULL, NULL}}},
1891 {{&ovsrec_table_port, &ovsrec_port_col_name, &ovsrec_port_col_qos},
1892 {NULL, NULL, NULL}}},
1894 {&ovsrec_table_queue,
1895 {{NULL, NULL, NULL},
1896 {NULL, NULL, NULL}}},
1899 {{&ovsrec_table_open_vswitch, NULL, &ovsrec_open_vswitch_col_ssl}}},
1901 {&ovsrec_table_sflow,
1902 {{&ovsrec_table_bridge,
1903 &ovsrec_bridge_col_name,
1904 &ovsrec_bridge_col_sflow},
1905 {NULL, NULL, NULL}}},
1907 {NULL, {{NULL, NULL, NULL}, {NULL, NULL, NULL}}}
1911 die_if_error(char *error)
1914 vsctl_fatal("%s", error);
1919 to_lower_and_underscores(unsigned c)
1921 return c == '-' ? '_' : tolower(c);
1925 score_partial_match(const char *name, const char *s)
1929 if (!strcmp(name, s)) {
1932 for (score = 0; ; score++, name++, s++) {
1933 if (to_lower_and_underscores(*name) != to_lower_and_underscores(*s)) {
1935 } else if (*name == '\0') {
1936 return UINT_MAX - 1;
1939 return *s == '\0' ? score : 0;
1942 static const struct vsctl_table_class *
1943 get_table(const char *table_name)
1945 const struct vsctl_table_class *table;
1946 const struct vsctl_table_class *best_match = NULL;
1947 unsigned int best_score = 0;
1949 for (table = tables; table->class; table++) {
1950 unsigned int score = score_partial_match(table->class->name,
1952 if (score > best_score) {
1955 } else if (score == best_score) {
1961 } else if (best_score) {
1962 vsctl_fatal("multiple table names match \"%s\"", table_name);
1964 vsctl_fatal("unknown table \"%s\"", table_name);
1968 static const struct ovsdb_idl_row *
1969 get_row_by_id(struct vsctl_context *ctx, const struct vsctl_table_class *table,
1970 const struct vsctl_row_id *id, const char *record_id)
1972 const struct ovsdb_idl_row *referrer, *final;
1978 if (!id->name_column) {
1979 if (strcmp(record_id, ".")) {
1982 referrer = ovsdb_idl_first_row(ctx->idl, id->table);
1983 if (!referrer || ovsdb_idl_next_row(referrer)) {
1987 const struct ovsdb_idl_row *row;
1990 for (row = ovsdb_idl_first_row(ctx->idl, id->table);
1992 row = ovsdb_idl_next_row(row))
1994 const struct ovsdb_datum *name;
1996 name = ovsdb_idl_get(row, id->name_column,
1997 OVSDB_TYPE_STRING, OVSDB_TYPE_VOID);
1998 if (name->n == 1 && !strcmp(name->keys[0].string, record_id)) {
2000 vsctl_fatal("multiple rows in %s match \"%s\"",
2001 table->class->name, record_id);
2012 if (id->uuid_column) {
2013 const struct ovsdb_datum *uuid;
2015 uuid = ovsdb_idl_get(referrer, id->uuid_column,
2016 OVSDB_TYPE_UUID, OVSDB_TYPE_VOID);
2018 final = ovsdb_idl_get_row_for_uuid(ctx->idl, table->class,
2019 &uuid->keys[0].uuid);
2028 static const struct ovsdb_idl_row *
2029 get_row (struct vsctl_context *ctx,
2030 const struct vsctl_table_class *table, const char *record_id)
2032 const struct ovsdb_idl_row *row;
2035 if (uuid_from_string(&uuid, record_id)) {
2036 row = ovsdb_idl_get_row_for_uuid(ctx->idl, table->class, &uuid);
2040 for (i = 0; i < ARRAY_SIZE(table->row_ids); i++) {
2041 row = get_row_by_id(ctx, table, &table->row_ids[i], record_id);
2050 static const struct ovsdb_idl_row *
2051 must_get_row(struct vsctl_context *ctx,
2052 const struct vsctl_table_class *table, const char *record_id)
2054 const struct ovsdb_idl_row *row = get_row(ctx, table, record_id);
2056 vsctl_fatal("no row \"%s\" in table %s",
2057 record_id, table->class->name);
2063 get_column(const struct vsctl_table_class *table, const char *column_name,
2064 const struct ovsdb_idl_column **columnp)
2066 const struct ovsdb_idl_column *best_match = NULL;
2067 unsigned int best_score = 0;
2070 for (i = 0; i < table->class->n_columns; i++) {
2071 const struct ovsdb_idl_column *column = &table->class->columns[i];
2072 unsigned int score = score_partial_match(column->name, column_name);
2073 if (score > best_score) {
2074 best_match = column;
2076 } else if (score == best_score) {
2081 *columnp = best_match;
2084 } else if (best_score) {
2085 return xasprintf("%s contains more than one column whose name "
2086 "matches \"%s\"", table->class->name, column_name);
2088 return xasprintf("%s does not contain a column whose name matches "
2089 "\"%s\"", table->class->name, column_name);
2094 missing_operator_error(const char *arg, const char **allowed_operators,
2100 ds_put_format(&s, "%s: argument does not end in ", arg);
2101 ds_put_format(&s, "\"%s\"", allowed_operators[0]);
2102 if (n_allowed == 2) {
2103 ds_put_format(&s, " or \"%s\"", allowed_operators[1]);
2104 } else if (n_allowed > 2) {
2107 for (i = 1; i < n_allowed - 1; i++) {
2108 ds_put_format(&s, ", \"%s\"", allowed_operators[i]);
2110 ds_put_format(&s, ", or \"%s\"", allowed_operators[i]);
2112 ds_put_format(&s, " followed by a value.");
2114 return ds_steal_cstr(&s);
2117 /* Breaks 'arg' apart into a number of fields in the following order:
2119 * - If 'columnp' is nonnull, the name of a column in 'table'. The column
2120 * is stored into '*columnp'. The column name may be abbreviated.
2122 * - If 'keyp' is nonnull, optionally a key string. (If both 'columnp'
2123 * and 'keyp' are nonnull, then the column and key names are expected to
2124 * be separated by ':'). The key is stored as a malloc()'d string into
2125 * '*keyp', or NULL if no key is present in 'arg'.
2127 * - If 'valuep' is nonnull, an operator followed by a value string. The
2128 * allowed operators are the 'n_allowed' string in 'allowed_operators',
2129 * or just "=" if 'n_allowed' is 0. If 'operatorp' is nonnull, then the
2130 * operator is stored into '*operatorp' (one of the pointers from
2131 * 'allowed_operators' is stored; nothing is malloc()'d). The value is
2132 * stored as a malloc()'d string into '*valuep', or NULL if no value is
2135 * At least 'columnp' or 'keyp' must be nonnull.
2137 * On success, returns NULL. On failure, returned a malloc()'d string error
2138 * message and stores NULL into all of the nonnull output arguments. */
2139 static char * WARN_UNUSED_RESULT
2140 parse_column_key_value(const char *arg,
2141 const struct vsctl_table_class *table,
2142 const struct ovsdb_idl_column **columnp, char **keyp,
2143 const char **operatorp,
2144 const char **allowed_operators, size_t n_allowed,
2147 const char *p = arg;
2150 assert(columnp || keyp);
2151 assert(!(operatorp && !valuep));
2159 /* Parse column name. */
2163 error = ovsdb_token_parse(&p, &column_name);
2167 if (column_name[0] == '\0') {
2169 error = xasprintf("%s: missing column name", arg);
2172 error = get_column(table, column_name, columnp);
2179 /* Parse key string. */
2180 if (*p == ':' || !columnp) {
2184 error = xasprintf("%s: key not accepted here", arg);
2187 error = ovsdb_token_parse(&p, keyp);
2195 /* Parse value string. */
2201 if (!allowed_operators) {
2202 static const char *equals = "=";
2203 allowed_operators = =
2209 for (i = 0; i < n_allowed; i++) {
2210 const char *op = allowed_operators[i];
2211 size_t op_len = strlen(op);
2213 if (op_len > best_len && !strncmp(op, p, op_len) && p[op_len]) {
2219 error = missing_operator_error(arg, allowed_operators, n_allowed);
2226 *valuep = xstrdup(p + best_len);
2232 error = xasprintf("%s: trailing garbage \"%s\" in argument",
2258 cmd_get(struct vsctl_context *ctx)
2260 bool if_exists = shash_find(&ctx->options, "--if-exists");
2261 const char *table_name = ctx->argv[1];
2262 const char *record_id = ctx->argv[2];
2263 const struct vsctl_table_class *table;
2264 const struct ovsdb_idl_row *row;
2265 struct ds *out = &ctx->output;
2268 table = get_table(table_name);
2269 row = must_get_row(ctx, table, record_id);
2270 for (i = 3; i < ctx->argc; i++) {
2271 const struct ovsdb_idl_column *column;
2272 const struct ovsdb_datum *datum;
2275 /* Special case for obtaining the UUID of a row. We can't just do this
2276 * through parse_column_key_value() below since it returns a "struct
2277 * ovsdb_idl_column" and the UUID column doesn't have one. */
2278 if (!strcasecmp(ctx->argv[i], "_uuid")
2279 || !strcasecmp(ctx->argv[i], "-uuid")) {
2280 ds_put_format(out, UUID_FMT"\n", UUID_ARGS(&row->uuid));
2284 die_if_error(parse_column_key_value(ctx->argv[i], table,
2285 &column, &key_string,
2286 NULL, NULL, 0, NULL));
2288 datum = ovsdb_idl_read(row, column);
2290 union ovsdb_atom key;
2293 if (column->type.value.type == OVSDB_TYPE_VOID) {
2294 vsctl_fatal("cannot specify key to get for non-map column %s",
2298 die_if_error(ovsdb_atom_from_string(&key,
2300 key_string, ctx->symtab));
2302 idx = ovsdb_datum_find_key(datum, &key,
2303 column->type.key.type);
2304 if (idx == UINT_MAX) {
2306 vsctl_fatal("no key \"%s\" in %s record \"%s\" column %s",
2307 key_string, table->class->name, record_id,
2311 ovsdb_atom_to_string(&datum->values[idx],
2312 column->type.value.type, out);
2314 ovsdb_atom_destroy(&key, column->type.key.type);
2316 ovsdb_datum_to_string(datum, &column->type, out);
2318 ds_put_char(out, '\n');
2325 list_record(const struct vsctl_table_class *table,
2326 const struct ovsdb_idl_row *row, struct ds *out)
2330 ds_put_format(out, "%-20s: "UUID_FMT"\n", "_uuid",
2331 UUID_ARGS(&row->uuid));
2332 for (i = 0; i < table->class->n_columns; i++) {
2333 const struct ovsdb_idl_column *column = &table->class->columns[i];
2334 const struct ovsdb_datum *datum;
2336 datum = ovsdb_idl_read(row, column);
2338 ds_put_format(out, "%-20s: ", column->name);
2339 ovsdb_datum_to_string(datum, &column->type, out);
2340 ds_put_char(out, '\n');
2345 cmd_list(struct vsctl_context *ctx)
2347 const char *table_name = ctx->argv[1];
2348 const struct vsctl_table_class *table;
2349 struct ds *out = &ctx->output;
2352 table = get_table(table_name);
2353 if (ctx->argc > 2) {
2354 for (i = 2; i < ctx->argc; i++) {
2356 ds_put_char(out, '\n');
2358 list_record(table, must_get_row(ctx, table, ctx->argv[i]), out);
2361 const struct ovsdb_idl_row *row;
2364 for (row = ovsdb_idl_first_row(ctx->idl, table->class), first = true;
2366 row = ovsdb_idl_next_row(row), first = false) {
2368 ds_put_char(out, '\n');
2370 list_record(table, row, out);
2376 set_column(const struct vsctl_table_class *table,
2377 const struct ovsdb_idl_row *row, const char *arg,
2378 struct ovsdb_symbol_table *symtab)
2380 const struct ovsdb_idl_column *column;
2381 char *key_string, *value_string;
2384 error = parse_column_key_value(arg, table, &column, &key_string,
2385 NULL, NULL, 0, &value_string);
2386 die_if_error(error);
2387 if (!value_string) {
2388 vsctl_fatal("%s: missing value", arg);
2392 union ovsdb_atom key, value;
2393 struct ovsdb_datum datum;
2395 if (column->type.value.type == OVSDB_TYPE_VOID) {
2396 vsctl_fatal("cannot specify key to set for non-map column %s",
2400 die_if_error(ovsdb_atom_from_string(&key, &column->type.key,
2401 key_string, symtab));
2402 die_if_error(ovsdb_atom_from_string(&value, &column->type.value,
2403 value_string, symtab));
2405 ovsdb_datum_init_empty(&datum);
2406 ovsdb_datum_add_unsafe(&datum, &key, &value, &column->type);
2408 ovsdb_atom_destroy(&key, column->type.key.type);
2409 ovsdb_atom_destroy(&value, column->type.value.type);
2411 ovsdb_datum_union(&datum, ovsdb_idl_read(row, column),
2412 &column->type, false);
2413 ovsdb_idl_txn_write(row, column, &datum);
2415 struct ovsdb_datum datum;
2417 die_if_error(ovsdb_datum_from_string(&datum, &column->type,
2418 value_string, symtab));
2419 ovsdb_idl_txn_write(row, column, &datum);
2427 cmd_set(struct vsctl_context *ctx)
2429 const char *table_name = ctx->argv[1];
2430 const char *record_id = ctx->argv[2];
2431 const struct vsctl_table_class *table;
2432 const struct ovsdb_idl_row *row;
2435 table = get_table(table_name);
2436 row = must_get_row(ctx, table, record_id);
2437 for (i = 3; i < ctx->argc; i++) {
2438 set_column(table, row, ctx->argv[i], ctx->symtab);
2443 cmd_add(struct vsctl_context *ctx)
2445 const char *table_name = ctx->argv[1];
2446 const char *record_id = ctx->argv[2];
2447 const char *column_name = ctx->argv[3];
2448 const struct vsctl_table_class *table;
2449 const struct ovsdb_idl_column *column;
2450 const struct ovsdb_idl_row *row;
2451 const struct ovsdb_type *type;
2452 struct ovsdb_datum old;
2455 table = get_table(table_name);
2456 row = must_get_row(ctx, table, record_id);
2457 die_if_error(get_column(table, column_name, &column));
2459 type = &column->type;
2460 ovsdb_datum_clone(&old, ovsdb_idl_read(row, column), &column->type);
2461 for (i = 4; i < ctx->argc; i++) {
2462 struct ovsdb_type add_type;
2463 struct ovsdb_datum add;
2467 add_type.n_max = UINT_MAX;
2468 die_if_error(ovsdb_datum_from_string(&add, &add_type, ctx->argv[i],
2470 ovsdb_datum_union(&old, &add, type, false);
2471 ovsdb_datum_destroy(&add, type);
2473 if (old.n > type->n_max) {
2474 vsctl_fatal("\"add\" operation would put %u %s in column %s of "
2475 "table %s but the maximum number is %u",
2477 type->value.type == OVSDB_TYPE_VOID ? "values" : "pairs",
2478 column->name, table->class->name, type->n_max);
2480 ovsdb_idl_txn_write(row, column, &old);
2484 cmd_remove(struct vsctl_context *ctx)
2486 const char *table_name = ctx->argv[1];
2487 const char *record_id = ctx->argv[2];
2488 const char *column_name = ctx->argv[3];
2489 const struct vsctl_table_class *table;
2490 const struct ovsdb_idl_column *column;
2491 const struct ovsdb_idl_row *row;
2492 const struct ovsdb_type *type;
2493 struct ovsdb_datum old;
2496 table = get_table(table_name);
2497 row = must_get_row(ctx, table, record_id);
2498 die_if_error(get_column(table, column_name, &column));
2500 type = &column->type;
2501 ovsdb_datum_clone(&old, ovsdb_idl_read(row, column), &column->type);
2502 for (i = 4; i < ctx->argc; i++) {
2503 struct ovsdb_type rm_type;
2504 struct ovsdb_datum rm;
2509 rm_type.n_max = UINT_MAX;
2510 error = ovsdb_datum_from_string(&rm, &rm_type,
2511 ctx->argv[i], ctx->symtab);
2512 if (error && ovsdb_type_is_map(&rm_type)) {
2514 rm_type.value.type = OVSDB_TYPE_VOID;
2515 die_if_error(ovsdb_datum_from_string(&rm, &rm_type,
2516 ctx->argv[i], ctx->symtab));
2518 ovsdb_datum_subtract(&old, type, &rm, &rm_type);
2519 ovsdb_datum_destroy(&rm, &rm_type);
2521 if (old.n < type->n_min) {
2522 vsctl_fatal("\"remove\" operation would put %u %s in column %s of "
2523 "table %s but the minimum number is %u",
2525 type->value.type == OVSDB_TYPE_VOID ? "values" : "pairs",
2526 column->name, table->class->name, type->n_min);
2528 ovsdb_idl_txn_write(row, column, &old);
2532 cmd_clear(struct vsctl_context *ctx)
2534 const char *table_name = ctx->argv[1];
2535 const char *record_id = ctx->argv[2];
2536 const struct vsctl_table_class *table;
2537 const struct ovsdb_idl_row *row;
2540 table = get_table(table_name);
2541 row = must_get_row(ctx, table, record_id);
2542 for (i = 3; i < ctx->argc; i++) {
2543 const struct ovsdb_idl_column *column;
2544 const struct ovsdb_type *type;
2545 struct ovsdb_datum datum;
2547 die_if_error(get_column(table, ctx->argv[i], &column));
2549 type = &column->type;
2550 if (type->n_min > 0) {
2551 vsctl_fatal("\"clear\" operation cannot be applied to column %s "
2552 "of table %s, which is not allowed to be empty",
2553 column->name, table->class->name);
2556 ovsdb_datum_init_empty(&datum);
2557 ovsdb_idl_txn_write(row, column, &datum);
2562 cmd_create(struct vsctl_context *ctx)
2564 const char *id = shash_find_data(&ctx->options, "--id");
2565 const char *table_name = ctx->argv[1];
2566 const struct vsctl_table_class *table;
2567 const struct ovsdb_idl_row *row;
2568 const struct uuid *uuid;
2572 struct ovsdb_symbol *symbol;
2575 vsctl_fatal("row id \"%s\" does not begin with \"@\"", id);
2578 symbol = ovsdb_symbol_table_insert(ctx->symtab, id);
2580 vsctl_fatal("row id \"%s\" may only be used to insert a single "
2583 symbol->used = true;
2585 uuid = &symbol->uuid;
2590 table = get_table(table_name);
2591 row = ovsdb_idl_txn_insert(ctx->txn, table->class, uuid);
2592 for (i = 2; i < ctx->argc; i++) {
2593 set_column(table, row, ctx->argv[i], ctx->symtab);
2595 ds_put_format(&ctx->output, UUID_FMT, UUID_ARGS(&row->uuid));
2598 /* This function may be used as the 'postprocess' function for commands that
2599 * insert new rows into the database. It expects that the command's 'run'
2600 * function prints the UUID reported by ovsdb_idl_txn_insert() as the command's
2601 * sole output. It replaces that output by the row's permanent UUID assigned
2602 * by the database server and appends a new-line.
2604 * Currently we use this only for "create", because the higher-level commands
2605 * are supposed to be independent of the actual structure of the vswitch
2608 post_create(struct vsctl_context *ctx)
2610 const struct uuid *real;
2613 uuid_from_string(&dummy, ds_cstr(&ctx->output));
2614 real = ovsdb_idl_txn_get_insert_uuid(ctx->txn, &dummy);
2616 ds_clear(&ctx->output);
2617 ds_put_format(&ctx->output, UUID_FMT, UUID_ARGS(real));
2619 ds_put_char(&ctx->output, '\n');
2623 cmd_destroy(struct vsctl_context *ctx)
2625 bool must_exist = !shash_find(&ctx->options, "--if-exists");
2626 const char *table_name = ctx->argv[1];
2627 const struct vsctl_table_class *table;
2630 table = get_table(table_name);
2631 for (i = 2; i < ctx->argc; i++) {
2632 const struct ovsdb_idl_row *row;
2634 row = (must_exist ? must_get_row : get_row)(ctx, table, ctx->argv[i]);
2636 ovsdb_idl_txn_delete(row);
2642 is_condition_satified(const struct vsctl_table_class *table,
2643 const struct ovsdb_idl_row *row, const char *arg,
2644 struct ovsdb_symbol_table *symtab)
2646 static const char *operators[] = {
2647 "=", "!=", "<", ">", "<=", ">="
2650 const struct ovsdb_idl_column *column;
2651 const struct ovsdb_datum *have_datum;
2652 char *key_string, *value_string;
2653 const char *operator;
2658 error = parse_column_key_value(arg, table, &column, &key_string,
2659 &operator, operators, ARRAY_SIZE(operators),
2661 die_if_error(error);
2662 if (!value_string) {
2663 vsctl_fatal("%s: missing value", arg);
2666 have_datum = ovsdb_idl_read(row, column);
2668 union ovsdb_atom want_key, want_value;
2670 if (column->type.value.type == OVSDB_TYPE_VOID) {
2671 vsctl_fatal("cannot specify key to check for non-map column %s",
2675 die_if_error(ovsdb_atom_from_string(&want_key, &column->type.key,
2676 key_string, symtab));
2677 die_if_error(ovsdb_atom_from_string(&want_value, &column->type.value,
2678 value_string, symtab));
2680 idx = ovsdb_datum_find_key(have_datum,
2681 &want_key, column->type.key.type);
2682 if (idx != UINT_MAX) {
2683 cmp = ovsdb_atom_compare_3way(&have_datum->values[idx],
2685 column->type.value.type);
2688 ovsdb_atom_destroy(&want_key, column->type.key.type);
2689 ovsdb_atom_destroy(&want_value, column->type.value.type);
2691 struct ovsdb_datum want_datum;
2693 die_if_error(ovsdb_datum_from_string(&want_datum, &column->type,
2694 value_string, symtab));
2696 cmp = ovsdb_datum_compare_3way(have_datum, &want_datum,
2698 ovsdb_datum_destroy(&want_datum, &column->type);
2704 return (idx == UINT_MAX ? false
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
2710 : !strcmp(operator, ">=") ? cmp >= 0
2715 cmd_wait_until(struct vsctl_context *ctx)
2717 const char *table_name = ctx->argv[1];
2718 const char *record_id = ctx->argv[2];
2719 const struct vsctl_table_class *table;
2720 const struct ovsdb_idl_row *row;
2723 table = get_table(table_name);
2725 row = get_row(ctx, table, record_id);
2727 ctx->try_again = true;
2731 for (i = 3; i < ctx->argc; i++) {
2732 if (!is_condition_satified(table, row, ctx->argv[i], ctx->symtab)) {
2733 ctx->try_again = true;
2739 static struct json *
2740 where_uuid_equals(const struct uuid *uuid)
2743 json_array_create_1(
2744 json_array_create_3(
2745 json_string_create("_uuid"),
2746 json_string_create("=="),
2747 json_array_create_2(
2748 json_string_create("uuid"),
2749 json_string_create_nocopy(
2750 xasprintf(UUID_FMT, UUID_ARGS(uuid))))));
2754 vsctl_context_init(struct vsctl_context *ctx, struct vsctl_command *command,
2755 struct ovsdb_idl *idl, struct ovsdb_idl_txn *txn,
2756 const struct ovsrec_open_vswitch *ovs,
2757 struct ovsdb_symbol_table *symtab)
2759 ctx->argc = command->argc;
2760 ctx->argv = command->argv;
2761 ctx->options = command->options;
2763 ds_swap(&ctx->output, &command->output);
2767 ctx->symtab = symtab;
2769 ctx->try_again = false;
2773 vsctl_context_done(struct vsctl_context *ctx, struct vsctl_command *command)
2775 ds_swap(&ctx->output, &command->output);
2779 do_vsctl(const char *args, struct vsctl_command *commands, size_t n_commands,
2780 struct ovsdb_idl *idl)
2782 struct ovsdb_idl_txn *txn;
2783 const struct ovsrec_open_vswitch *ovs;
2784 enum ovsdb_idl_txn_status status;
2785 struct ovsdb_symbol_table *symtab;
2787 struct vsctl_command *c;
2788 int64_t next_cfg = 0;
2791 txn = the_idl_txn = ovsdb_idl_txn_create(idl);
2793 ovsdb_idl_txn_set_dry_run(txn);
2796 ovsdb_idl_txn_add_comment(txn, "ovs-vsctl: %s", args);
2798 ovs = ovsrec_open_vswitch_first(idl);
2800 /* XXX add verification that table is empty */
2801 ovs = ovsrec_open_vswitch_insert(txn);
2804 if (wait_for_reload) {
2805 struct json *where = where_uuid_equals(&ovs->header_.uuid);
2806 ovsdb_idl_txn_increment(txn, "Open_vSwitch", "next_cfg", where);
2807 json_destroy(where);
2810 symtab = ovsdb_symbol_table_create();
2811 for (c = commands; c < &commands[n_commands]; c++) {
2812 ds_init(&c->output);
2814 for (c = commands; c < &commands[n_commands]; c++) {
2815 struct vsctl_context ctx;
2817 vsctl_context_init(&ctx, c, idl, txn, ovs, symtab);
2818 (c->syntax->run)(&ctx);
2819 vsctl_context_done(&ctx, c);
2821 if (ctx.try_again) {
2826 status = ovsdb_idl_txn_commit_block(txn);
2827 if (wait_for_reload && status == TXN_SUCCESS) {
2828 next_cfg = ovsdb_idl_txn_get_increment_new_value(txn);
2830 if (status == TXN_UNCHANGED || status == TXN_SUCCESS) {
2831 for (c = commands; c < &commands[n_commands]; c++) {
2832 if (c->syntax->postprocess) {
2833 struct vsctl_context ctx;
2835 vsctl_context_init(&ctx, c, idl, txn, ovs, symtab);
2836 (c->syntax->postprocess)(&ctx);
2837 vsctl_context_done(&ctx, c);
2841 error = xstrdup(ovsdb_idl_txn_get_error(txn));
2842 ovsdb_idl_txn_destroy(txn);
2845 unused = ovsdb_symbol_table_find_unused(symtab);
2847 vsctl_fatal("row id \"%s\" is referenced but never created (e.g. "
2848 "with \"-- --id=%s create ...\")", unused, unused);
2852 case TXN_INCOMPLETE:
2856 /* Should not happen--we never call ovsdb_idl_txn_abort(). */
2857 vsctl_fatal("transaction aborted");
2867 vsctl_fatal("transaction error: %s", error);
2874 ovsdb_symbol_table_destroy(symtab);
2876 for (c = commands; c < &commands[n_commands]; c++) {
2877 struct ds *ds = &c->output;
2878 struct shash_node *node;
2884 for (j = 0; j < ds->length; j++) {
2885 int c = ds->string[j];
2888 fputs("\\n", stdout);
2892 fputs("\\\\", stdout);
2901 fputs(ds_cstr(ds), stdout);
2903 ds_destroy(&c->output);
2905 SHASH_FOR_EACH (node, &c->options) {
2908 shash_destroy(&c->options);
2912 if (wait_for_reload && status != TXN_UNCHANGED) {
2914 const struct ovsrec_open_vswitch *ovs;
2917 OVSREC_OPEN_VSWITCH_FOR_EACH (ovs, idl) {
2918 if (ovs->cur_cfg >= next_cfg) {
2922 ovsdb_idl_wait(idl);
2927 ovsdb_idl_destroy(idl);
2932 /* Our transaction needs to be rerun, or a prerequisite was not met. Free
2933 * resources and return so that the caller can try again. */
2934 ovsdb_idl_txn_abort(txn);
2935 ovsdb_idl_txn_destroy(txn);
2936 ovsdb_symbol_table_destroy(symtab);
2937 for (c = commands; c < &commands[n_commands]; c++) {
2938 ds_destroy(&c->output);
2943 static const struct vsctl_command_syntax all_commands[] = {
2944 /* Open vSwitch commands. */
2945 {"init", 0, 0, cmd_init, NULL, ""},
2947 /* Bridge commands. */
2948 {"add-br", 1, 3, cmd_add_br, NULL, "--may-exist"},
2949 {"del-br", 1, 1, cmd_del_br, NULL, "--if-exists"},
2950 {"list-br", 0, 0, cmd_list_br, NULL, ""},
2951 {"br-exists", 1, 1, cmd_br_exists, NULL, ""},
2952 {"br-to-vlan", 1, 1, cmd_br_to_vlan, NULL, ""},
2953 {"br-to-parent", 1, 1, cmd_br_to_parent, NULL, ""},
2954 {"br-set-external-id", 2, 3, cmd_br_set_external_id, NULL, ""},
2955 {"br-get-external-id", 1, 2, cmd_br_get_external_id, NULL, ""},
2957 /* Port commands. */
2958 {"list-ports", 1, 1, cmd_list_ports, NULL, ""},
2959 {"add-port", 2, INT_MAX, cmd_add_port, NULL, "--may-exist"},
2960 {"add-bond", 4, INT_MAX, cmd_add_bond, NULL, "--may-exist,--fake-iface"},
2961 {"del-port", 1, 2, cmd_del_port, NULL, "--if-exists,--with-iface"},
2962 {"port-to-br", 1, 1, cmd_port_to_br, NULL, ""},
2964 /* Interface commands. */
2965 {"list-ifaces", 1, 1, cmd_list_ifaces, NULL, ""},
2966 {"iface-to-br", 1, 1, cmd_iface_to_br, NULL, ""},
2968 /* Controller commands. */
2969 {"get-controller", 0, 1, cmd_get_controller, NULL, ""},
2970 {"del-controller", 0, 1, cmd_del_controller, NULL, ""},
2971 {"set-controller", 1, INT_MAX, cmd_set_controller, NULL, ""},
2972 {"get-fail-mode", 0, 1, cmd_get_fail_mode, NULL, ""},
2973 {"del-fail-mode", 0, 1, cmd_del_fail_mode, NULL, ""},
2974 {"set-fail-mode", 1, 2, cmd_set_fail_mode, NULL, ""},
2977 {"get-ssl", 0, 0, cmd_get_ssl, NULL, ""},
2978 {"del-ssl", 0, 0, cmd_del_ssl, NULL, ""},
2979 {"set-ssl", 3, 3, cmd_set_ssl, NULL, "--bootstrap"},
2981 /* Switch commands. */
2982 {"emer-reset", 0, 0, cmd_emer_reset, NULL, ""},
2984 /* Parameter commands. */
2985 {"get", 3, INT_MAX, cmd_get, NULL, "--if-exists"},
2986 {"list", 1, INT_MAX, cmd_list, NULL, ""},
2987 {"set", 3, INT_MAX, cmd_set, NULL, ""},
2988 {"add", 4, INT_MAX, cmd_add, NULL, ""},
2989 {"remove", 4, INT_MAX, cmd_remove, NULL, ""},
2990 {"clear", 3, INT_MAX, cmd_clear, NULL, ""},
2991 {"create", 2, INT_MAX, cmd_create, post_create, "--id="},
2992 {"destroy", 1, INT_MAX, cmd_destroy, NULL, "--if-exists"},
2993 {"wait-until", 2, INT_MAX, cmd_wait_until, NULL, ""},
2995 {NULL, 0, 0, NULL, NULL, NULL},