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;
62 enum { RO, RW } mode; /* Does this command modify the database? */
65 struct vsctl_command {
66 /* Data that remains constant after initialization. */
67 const struct vsctl_command_syntax *syntax;
72 /* Data modified by commands. */
76 /* --db: The database server to contact. */
77 static const char *db;
79 /* --oneline: Write each command's output as a single line? */
82 /* --dry-run: Do not commit any changes. */
85 /* --no-wait: Wait for ovs-vswitchd to reload its configuration? */
86 static bool wait_for_reload = true;
88 /* --timeout: Time to wait for a connection to 'db'. */
91 /* All supported commands. */
92 static const struct vsctl_command_syntax all_commands[];
94 /* The IDL we're using and the current transaction, if any.
95 * This is for use by vsctl_exit() only, to allow it to clean up.
96 * Other code should use its context arguments. */
97 static struct ovsdb_idl *the_idl;
98 static struct ovsdb_idl_txn *the_idl_txn;
100 static void vsctl_exit(int status) NO_RETURN;
101 static void vsctl_fatal(const char *, ...) PRINTF_FORMAT(1, 2) NO_RETURN;
102 static char *default_db(void);
103 static void usage(void) NO_RETURN;
104 static void parse_options(int argc, char *argv[]);
105 static bool might_write_to_db(char **argv);
107 static struct vsctl_command *parse_commands(int argc, char *argv[],
108 size_t *n_commandsp);
109 static void parse_command(int argc, char *argv[], struct vsctl_command *);
110 static const struct vsctl_command_syntax *find_command(const char *name);
111 static void do_vsctl(const char *args,
112 struct vsctl_command *, size_t n_commands,
115 static const struct vsctl_table_class *get_table(const char *table_name);
116 static void set_column(const struct vsctl_table_class *,
117 const struct ovsdb_idl_row *, const char *arg,
118 struct ovsdb_symbol_table *);
121 main(int argc, char *argv[])
123 extern struct vlog_module VLM_reconnect;
124 struct ovsdb_idl *idl;
125 struct vsctl_command *commands;
129 set_program_name(argv[0]);
130 signal(SIGPIPE, SIG_IGN);
131 vlog_set_levels(NULL, VLF_CONSOLE, VLL_WARN);
132 vlog_set_levels(&VLM_reconnect, VLF_ANY_FACILITY, VLL_WARN);
135 /* Log our arguments. This is often valuable for debugging systems. */
136 args = process_escape_args(argv);
137 VLOG(might_write_to_db(argv) ? VLL_INFO : VLL_DBG, "Called as %s", args);
139 /* Parse command line. */
140 parse_options(argc, argv);
141 commands = parse_commands(argc - optind, argv + optind, &n_commands);
147 /* Now execute the commands. */
148 idl = the_idl = ovsdb_idl_create(db, &ovsrec_idl_class);
150 if (ovsdb_idl_run(idl)) {
151 do_vsctl(args, commands, n_commands, idl);
160 parse_options(int argc, char *argv[])
163 OPT_DB = UCHAR_MAX + 1,
171 static struct option long_options[] = {
172 {"db", required_argument, 0, OPT_DB},
173 {"no-syslog", no_argument, 0, OPT_NO_SYSLOG},
174 {"no-wait", no_argument, 0, OPT_NO_WAIT},
175 {"dry-run", no_argument, 0, OPT_DRY_RUN},
176 {"oneline", no_argument, 0, OPT_ONELINE},
177 {"timeout", required_argument, 0, 't'},
178 {"help", no_argument, 0, 'h'},
179 {"version", no_argument, 0, 'V'},
182 STREAM_SSL_LONG_OPTIONS
183 {"peer-ca-cert", required_argument, 0, OPT_PEER_CA_CERT},
187 char *tmp, *short_options;
189 tmp = long_options_to_short_options(long_options);
190 short_options = xasprintf("+%s", tmp);
196 c = getopt_long(argc, argv, short_options, long_options, NULL);
211 vlog_set_levels(&VLM_vsctl, VLF_SYSLOG, VLL_WARN);
215 wait_for_reload = false;
226 OVS_PRINT_VERSION(0, 0);
230 timeout = strtoul(optarg, NULL, 10);
232 vsctl_fatal("value %s on -t or --timeout is invalid",
240 STREAM_SSL_OPTION_HANDLERS
242 case OPT_PEER_CA_CERT:
243 stream_ssl_set_peer_ca_cert_file(optarg);
261 static struct vsctl_command *
262 parse_commands(int argc, char *argv[], size_t *n_commandsp)
264 struct vsctl_command *commands;
265 size_t n_commands, allocated_commands;
269 n_commands = allocated_commands = 0;
271 for (start = i = 0; i <= argc; i++) {
272 if (i == argc || !strcmp(argv[i], "--")) {
274 if (n_commands >= allocated_commands) {
275 struct vsctl_command *c;
277 commands = x2nrealloc(commands, &allocated_commands,
279 for (c = commands; c < &commands[n_commands]; c++) {
280 shash_moved(&c->options);
283 parse_command(i - start, &argv[start],
284 &commands[n_commands++]);
290 vsctl_fatal("missing command name (use --help for help)");
292 *n_commandsp = n_commands;
297 parse_command(int argc, char *argv[], struct vsctl_command *command)
299 const struct vsctl_command_syntax *p;
300 struct shash_node *node;
304 shash_init(&command->options);
305 for (i = 0; i < argc; i++) {
306 const char *option = argv[i];
310 if (option[0] != '-') {
314 equals = strchr(option, '=');
316 key = xmemdup0(option, equals - option);
317 value = xstrdup(equals + 1);
319 key = xstrdup(option);
323 if (shash_find(&command->options, key)) {
324 vsctl_fatal("'%s' option specified multiple times", argv[i]);
326 shash_add_nocopy(&command->options, key, value);
329 vsctl_fatal("missing command name");
332 p = find_command(argv[i]);
334 vsctl_fatal("unknown command '%s'; use --help for help", argv[i]);
337 SHASH_FOR_EACH (node, &command->options) {
338 const char *s = strstr(p->options, node->name);
339 int end = s ? s[strlen(node->name)] : EOF;
341 if (end != '=' && end != ',' && end != ' ' && end != '\0') {
342 vsctl_fatal("'%s' command has no '%s' option",
343 argv[i], node->name);
345 if ((end == '=') != (node->data != NULL)) {
347 vsctl_fatal("missing argument to '%s' option on '%s' "
348 "command", node->name, argv[i]);
350 vsctl_fatal("'%s' option on '%s' does not accept an "
351 "argument", node->name, argv[i]);
356 n_arg = argc - i - 1;
357 if (n_arg < p->min_args) {
358 vsctl_fatal("'%s' command requires at least %d arguments",
359 p->name, p->min_args);
360 } else if (n_arg > p->max_args) {
363 for (j = i + 1; j < argc; j++) {
364 if (argv[j][0] == '-') {
365 vsctl_fatal("'%s' command takes at most %d arguments "
366 "(note that options must precede command "
367 "names and follow a \"--\" argument)",
368 p->name, p->max_args);
372 vsctl_fatal("'%s' command takes at most %d arguments",
373 p->name, p->max_args);
377 command->argc = n_arg + 1;
378 command->argv = &argv[i];
381 /* Returns the "struct vsctl_command_syntax" for a given command 'name', or a
382 * null pointer if there is none. */
383 static const struct vsctl_command_syntax *
384 find_command(const char *name)
386 static struct shash commands = SHASH_INITIALIZER(&commands);
388 if (shash_is_empty(&commands)) {
389 const struct vsctl_command_syntax *p;
391 for (p = all_commands; p->name; p++) {
392 shash_add_assert(&commands, p->name, p);
396 return shash_find_data(&commands, name);
400 vsctl_fatal(const char *format, ...)
405 va_start(args, format);
406 message = xvasprintf(format, args);
409 vlog_set_levels(&VLM_vsctl, VLF_CONSOLE, VLL_EMER);
410 VLOG_ERR("%s", message);
411 ovs_error(0, "%s", message);
412 vsctl_exit(EXIT_FAILURE);
415 /* Frees the current transaction and the underlying IDL and then calls
418 * Freeing the transaction and the IDL is not strictly necessary, but it makes
419 * for a clean memory leak report from valgrind in the normal case. That makes
420 * it easier to notice real memory leaks. */
422 vsctl_exit(int status)
425 ovsdb_idl_txn_abort(the_idl_txn);
426 ovsdb_idl_txn_destroy(the_idl_txn);
428 ovsdb_idl_destroy(the_idl);
436 %s: ovs-vswitchd management utility\n\
437 usage: %s [OPTIONS] COMMAND [ARG...]\n\
440 add-br BRIDGE create a new bridge named BRIDGE\n\
441 add-br BRIDGE PARENT VLAN create new fake BRIDGE in PARENT on VLAN\n\
442 del-br BRIDGE delete BRIDGE and all of its ports\n\
443 list-br print the names of all the bridges\n\
444 br-exists BRIDGE test whether BRIDGE exists\n\
445 br-to-vlan BRIDGE print the VLAN which BRIDGE is on\n\
446 br-to-parent BRIDGE print the parent of BRIDGE\n\
447 br-set-external-id BRIDGE KEY VALUE set KEY on BRIDGE to VALUE\n\
448 br-set-external-id BRIDGE KEY unset KEY on BRIDGE\n\
449 br-get-external-id BRIDGE KEY print value of KEY on BRIDGE\n\
450 br-get-external-id BRIDGE list key-value pairs on BRIDGE\n\
453 list-ports BRIDGE print the names of all the ports on BRIDGE\n\
454 add-port BRIDGE PORT add network device PORT to BRIDGE\n\
455 add-bond BRIDGE PORT IFACE... add bonded port PORT in BRIDGE from IFACES\n\
456 del-port [BRIDGE] PORT delete PORT (which may be bonded) from BRIDGE\n\
457 port-to-br PORT print name of bridge that contains PORT\n\
458 A bond is considered to be a single port.\n\
460 Interface commands (a bond consists of multiple interfaces):\n\
461 list-ifaces BRIDGE print the names of all interfaces on BRIDGE\n\
462 iface-to-br IFACE print name of bridge that contains IFACE\n\
464 Controller commands:\n\
465 get-controller BRIDGE print the controller for BRIDGE\n\
466 del-controller BRIDGE delete the controller for BRIDGE\n\
467 set-controller BRIDGE TARGET set the controller for BRIDGE to TARGET\n\
468 get-fail-mode BRIDGE print the fail-mode for BRIDGE\n\
469 del-fail-mode BRIDGE delete the fail-mode for BRIDGE\n\
470 set-fail-mode BRIDGE MODE set the fail-mode for BRIDGE to MODE\n\
473 get-ssl print the SSL configuration\n\
474 del-ssl delete the SSL configuration\n\
475 set-ssl PRIV-KEY CERT CA-CERT set the SSL configuration\n\
478 emer-reset reset switch to known good state\n\
480 Database commands:\n\
481 list TBL [REC] list RECord (or all records) in TBL\n\
482 get TBL REC COL[:KEY] print values of COLumns in RECord in TBL\n\
483 set TBL REC COL[:KEY]=VALUE set COLumn values in RECord in TBL\n\
484 add TBL REC COL [KEY=]VALUE add (KEY=)VALUE to COLumn in RECord in TBL\n\
485 remove TBL REC COL [KEY=]VALUE remove (KEY=)VALUE from COLumn\n\
486 clear TBL REC COL clear values from COLumn in RECord in TBL\n\
487 create TBL COL[:KEY]=VALUE create and initialize new record\n\
488 destroy TBL REC delete RECord from TBL\n\
489 wait-until TBL REC [COL[:KEY]=VALUE] wait until condition is true\n\
490 Potentially unsafe database commands require --force option.\n\
493 --db=DATABASE connect to DATABASE\n\
495 --oneline print exactly one line of output per command\n",
496 program_name, program_name, default_db());
500 -h, --help display this help message\n\
501 -V, --version display version information\n");
510 def = xasprintf("unix:%s/db.sock", ovs_rundir);
515 /* Returns true if it looks like this set of arguments might modify the
516 * database, otherwise false. (Not very smart, so it's prone to false
519 might_write_to_db(char **argv)
521 for (; *argv; argv++) {
522 const struct vsctl_command_syntax *p = find_command(*argv);
523 if (p && p->mode == RW) {
530 struct vsctl_context {
534 struct shash options;
536 /* Modifiable state. */
538 struct ovsdb_idl *idl;
539 struct ovsdb_idl_txn *txn;
540 struct ovsdb_symbol_table *symtab;
541 const struct ovsrec_open_vswitch *ovs;
544 /* A command may set this member to true if some prerequisite is not met
545 * and the caller should wait for something to change and then retry. */
549 struct vsctl_bridge {
550 struct ovsrec_bridge *br_cfg;
552 struct ovsrec_controller **ctrl;
555 struct vsctl_bridge *parent;
560 struct ovsrec_port *port_cfg;
561 struct vsctl_bridge *bridge;
565 struct ovsrec_interface *iface_cfg;
566 struct vsctl_port *port;
570 struct vsctl_context *ctx;
571 struct shash bridges;
577 vsctl_context_to_string(const struct vsctl_context *ctx)
579 const struct shash_node *node;
585 SHASH_FOR_EACH (node, &ctx->options) {
586 svec_add(&words, node->name);
588 for (i = 0; i < ctx->argc; i++) {
589 svec_add(&words, ctx->argv[i]);
591 svec_terminate(&words);
593 s = process_escape_args(words.names);
595 svec_destroy(&words);
601 verify_ports(struct vsctl_context *ctx)
603 if (!ctx->verified_ports) {
604 const struct ovsrec_bridge *bridge;
605 const struct ovsrec_port *port;
607 ovsrec_open_vswitch_verify_bridges(ctx->ovs);
608 OVSREC_BRIDGE_FOR_EACH (bridge, ctx->idl) {
609 ovsrec_bridge_verify_ports(bridge);
611 OVSREC_PORT_FOR_EACH (port, ctx->idl) {
612 ovsrec_port_verify_interfaces(port);
615 ctx->verified_ports = true;
619 static struct vsctl_bridge *
620 add_bridge(struct vsctl_info *b,
621 struct ovsrec_bridge *br_cfg, const char *name,
622 struct vsctl_bridge *parent, int vlan)
624 struct vsctl_bridge *br = xmalloc(sizeof *br);
626 br->name = xstrdup(name);
630 br->ctrl = parent->br_cfg->controller;
631 br->n_ctrl = parent->br_cfg->n_controller;
632 br->fail_mode = parent->br_cfg->fail_mode;
634 br->ctrl = br_cfg->controller;
635 br->n_ctrl = br_cfg->n_controller;
636 br->fail_mode = br_cfg->fail_mode;
638 shash_add(&b->bridges, br->name, br);
643 port_is_fake_bridge(const struct ovsrec_port *port_cfg)
645 return (port_cfg->fake_bridge
647 && *port_cfg->tag >= 1 && *port_cfg->tag <= 4095);
650 static struct vsctl_bridge *
651 find_vlan_bridge(struct vsctl_info *info,
652 struct vsctl_bridge *parent, int vlan)
654 struct shash_node *node;
656 SHASH_FOR_EACH (node, &info->bridges) {
657 struct vsctl_bridge *br = node->data;
658 if (br->parent == parent && br->vlan == vlan) {
667 free_info(struct vsctl_info *info)
669 struct shash_node *node;
671 SHASH_FOR_EACH (node, &info->bridges) {
672 struct vsctl_bridge *bridge = node->data;
676 shash_destroy(&info->bridges);
678 shash_destroy_free_data(&info->ports);
679 shash_destroy_free_data(&info->ifaces);
683 get_info(struct vsctl_context *ctx, struct vsctl_info *info)
685 const struct ovsrec_open_vswitch *ovs = ctx->ovs;
686 struct shash bridges, ports;
690 shash_init(&info->bridges);
691 shash_init(&info->ports);
692 shash_init(&info->ifaces);
694 shash_init(&bridges);
696 for (i = 0; i < ovs->n_bridges; i++) {
697 struct ovsrec_bridge *br_cfg = ovs->bridges[i];
698 struct vsctl_bridge *br;
701 if (!shash_add_once(&bridges, br_cfg->name, NULL)) {
702 VLOG_WARN("%s: database contains duplicate bridge name",
706 br = add_bridge(info, br_cfg, br_cfg->name, NULL, 0);
711 for (j = 0; j < br_cfg->n_ports; j++) {
712 struct ovsrec_port *port_cfg = br_cfg->ports[j];
714 if (!shash_add_once(&ports, port_cfg->name, NULL)) {
715 VLOG_WARN("%s: database contains duplicate port name",
720 if (port_is_fake_bridge(port_cfg)
721 && shash_add_once(&bridges, port_cfg->name, NULL)) {
722 add_bridge(info, NULL, port_cfg->name, br, *port_cfg->tag);
726 shash_destroy(&bridges);
727 shash_destroy(&ports);
729 shash_init(&bridges);
731 for (i = 0; i < ovs->n_bridges; i++) {
732 struct ovsrec_bridge *br_cfg = ovs->bridges[i];
733 struct vsctl_bridge *br;
736 if (!shash_add_once(&bridges, br_cfg->name, NULL)) {
739 br = shash_find_data(&info->bridges, br_cfg->name);
740 for (j = 0; j < br_cfg->n_ports; j++) {
741 struct ovsrec_port *port_cfg = br_cfg->ports[j];
742 struct vsctl_port *port;
745 if (!shash_add_once(&ports, port_cfg->name, NULL)) {
749 if (port_is_fake_bridge(port_cfg)
750 && !shash_add_once(&bridges, port_cfg->name, NULL)) {
754 port = xmalloc(sizeof *port);
755 port->port_cfg = port_cfg;
757 && *port_cfg->tag >= 1 && *port_cfg->tag <= 4095) {
758 port->bridge = find_vlan_bridge(info, br, *port_cfg->tag);
765 shash_add(&info->ports, port_cfg->name, port);
767 for (k = 0; k < port_cfg->n_interfaces; k++) {
768 struct ovsrec_interface *iface_cfg = port_cfg->interfaces[k];
769 struct vsctl_iface *iface;
771 if (shash_find(&info->ifaces, iface_cfg->name)) {
772 VLOG_WARN("%s: database contains duplicate interface name",
777 iface = xmalloc(sizeof *iface);
778 iface->iface_cfg = iface_cfg;
780 shash_add(&info->ifaces, iface_cfg->name, iface);
784 shash_destroy(&bridges);
785 shash_destroy(&ports);
789 check_conflicts(struct vsctl_info *info, const char *name,
792 struct vsctl_iface *iface;
793 struct vsctl_port *port;
795 verify_ports(info->ctx);
797 if (shash_find(&info->bridges, name)) {
798 vsctl_fatal("%s because a bridge named %s already exists",
802 port = shash_find_data(&info->ports, name);
804 vsctl_fatal("%s because a port named %s already exists on "
805 "bridge %s", msg, name, port->bridge->name);
808 iface = shash_find_data(&info->ifaces, name);
810 vsctl_fatal("%s because an interface named %s already exists "
811 "on bridge %s", msg, name, iface->port->bridge->name);
817 static struct vsctl_bridge *
818 find_bridge(struct vsctl_info *info, const char *name, bool must_exist)
820 struct vsctl_bridge *br = shash_find_data(&info->bridges, name);
821 if (must_exist && !br) {
822 vsctl_fatal("no bridge named %s", name);
824 ovsrec_open_vswitch_verify_bridges(info->ctx->ovs);
828 static struct vsctl_bridge *
829 find_real_bridge(struct vsctl_info *info, const char *name, bool must_exist)
831 struct vsctl_bridge *br = find_bridge(info, name, must_exist);
832 if (br && br->parent) {
833 vsctl_fatal("%s is a fake bridge", name);
838 static struct vsctl_port *
839 find_port(struct vsctl_info *info, const char *name, bool must_exist)
841 struct vsctl_port *port = shash_find_data(&info->ports, name);
842 if (port && !strcmp(name, port->bridge->name)) {
845 if (must_exist && !port) {
846 vsctl_fatal("no port named %s", name);
848 verify_ports(info->ctx);
852 static struct vsctl_iface *
853 find_iface(struct vsctl_info *info, const char *name, bool must_exist)
855 struct vsctl_iface *iface = shash_find_data(&info->ifaces, name);
856 if (iface && !strcmp(name, iface->port->bridge->name)) {
859 if (must_exist && !iface) {
860 vsctl_fatal("no interface named %s", name);
862 verify_ports(info->ctx);
867 bridge_insert_port(struct ovsrec_bridge *br, struct ovsrec_port *port)
869 struct ovsrec_port **ports;
872 ports = xmalloc(sizeof *br->ports * (br->n_ports + 1));
873 for (i = 0; i < br->n_ports; i++) {
874 ports[i] = br->ports[i];
876 ports[br->n_ports] = port;
877 ovsrec_bridge_set_ports(br, ports, br->n_ports + 1);
882 bridge_delete_port(struct ovsrec_bridge *br, struct ovsrec_port *port)
884 struct ovsrec_port **ports;
887 ports = xmalloc(sizeof *br->ports * br->n_ports);
888 for (i = n = 0; i < br->n_ports; i++) {
889 if (br->ports[i] != port) {
890 ports[n++] = br->ports[i];
893 ovsrec_bridge_set_ports(br, ports, n);
898 ovs_insert_bridge(const struct ovsrec_open_vswitch *ovs,
899 struct ovsrec_bridge *bridge)
901 struct ovsrec_bridge **bridges;
904 bridges = xmalloc(sizeof *ovs->bridges * (ovs->n_bridges + 1));
905 for (i = 0; i < ovs->n_bridges; i++) {
906 bridges[i] = ovs->bridges[i];
908 bridges[ovs->n_bridges] = bridge;
909 ovsrec_open_vswitch_set_bridges(ovs, bridges, ovs->n_bridges + 1);
914 ovs_delete_bridge(const struct ovsrec_open_vswitch *ovs,
915 struct ovsrec_bridge *bridge)
917 struct ovsrec_bridge **bridges;
920 bridges = xmalloc(sizeof *ovs->bridges * ovs->n_bridges);
921 for (i = n = 0; i < ovs->n_bridges; i++) {
922 if (ovs->bridges[i] != bridge) {
923 bridges[n++] = ovs->bridges[i];
926 ovsrec_open_vswitch_set_bridges(ovs, bridges, n);
931 cmd_init(struct vsctl_context *ctx OVS_UNUSED)
936 cmd_emer_reset(struct vsctl_context *ctx)
938 const struct ovsdb_idl *idl = ctx->idl;
939 const struct ovsrec_bridge *br;
940 const struct ovsrec_port *port;
941 const struct ovsrec_interface *iface;
942 const struct ovsrec_mirror *mirror, *next_mirror;
943 const struct ovsrec_controller *ctrl, *next_ctrl;
944 const struct ovsrec_netflow *nf, *next_nf;
945 const struct ovsrec_ssl *ssl, *next_ssl;
946 const struct ovsrec_sflow *sflow, *next_sflow;
949 /* Reset the Open_vSwitch table. */
950 ovsrec_open_vswitch_set_managers(ctx->ovs, NULL, 0);
951 ovsrec_open_vswitch_set_ssl(ctx->ovs, NULL);
953 OVSREC_BRIDGE_FOR_EACH (br, idl) {
955 char *hw_key = "hwaddr";
958 ovsrec_bridge_set_controller(br, NULL, 0);
959 ovsrec_bridge_set_mirrors(br, NULL, 0);
960 ovsrec_bridge_set_netflow(br, NULL);
961 ovsrec_bridge_set_sflow(br, NULL);
962 ovsrec_bridge_set_flood_vlans(br, NULL, 0);
964 /* We only want to save the "hwaddr" key from other_config. */
965 for (i=0; i < br->n_other_config; i++) {
966 if (!strcmp(br->key_other_config[i], hw_key)) {
967 hw_val = br->value_other_config[i];
972 char *val = xstrdup(hw_val);
973 ovsrec_bridge_set_other_config(br, &hw_key, &val, 1);
976 ovsrec_bridge_set_other_config(br, NULL, NULL, 0);
980 OVSREC_PORT_FOR_EACH (port, idl) {
981 ovsrec_port_set_other_config(port, NULL, NULL, 0);
984 OVSREC_INTERFACE_FOR_EACH (iface, idl) {
985 /* xxx What do we do about gre/patch devices created by mgr? */
987 ovsrec_interface_set_ingress_policing_rate(iface, 0);
988 ovsrec_interface_set_ingress_policing_burst(iface, 0);
991 OVSREC_MIRROR_FOR_EACH_SAFE (mirror, next_mirror, idl) {
992 ovsrec_mirror_delete(mirror);
995 OVSREC_CONTROLLER_FOR_EACH_SAFE (ctrl, next_ctrl, idl) {
996 ovsrec_controller_delete(ctrl);
999 OVSREC_NETFLOW_FOR_EACH_SAFE (nf, next_nf, idl) {
1000 ovsrec_netflow_delete(nf);
1003 OVSREC_SSL_FOR_EACH_SAFE (ssl, next_ssl, idl) {
1004 ovsrec_ssl_delete(ssl);
1007 OVSREC_SFLOW_FOR_EACH_SAFE (sflow, next_sflow, idl) {
1008 ovsrec_sflow_delete(sflow);
1013 cmd_add_br(struct vsctl_context *ctx)
1015 bool may_exist = shash_find(&ctx->options, "--may-exist") != 0;
1016 const char *br_name, *parent_name;
1017 struct vsctl_info info;
1020 br_name = ctx->argv[1];
1021 if (ctx->argc == 2) {
1024 } else if (ctx->argc == 4) {
1025 parent_name = ctx->argv[2];
1026 vlan = atoi(ctx->argv[3]);
1027 if (vlan < 1 || vlan > 4095) {
1028 vsctl_fatal("%s: vlan must be between 1 and 4095", ctx->argv[0]);
1031 vsctl_fatal("'%s' command takes exactly 1 or 3 arguments",
1035 get_info(ctx, &info);
1037 struct vsctl_bridge *br;
1039 br = find_bridge(&info, br_name, false);
1043 vsctl_fatal("\"--may-exist add-br %s\" but %s is "
1044 "a VLAN bridge for VLAN %d",
1045 br_name, br_name, br->vlan);
1049 vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
1050 "is not a VLAN bridge",
1051 br_name, parent_name, vlan, br_name);
1052 } else if (strcmp(br->parent->name, parent_name)) {
1053 vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
1054 "has the wrong parent %s",
1055 br_name, parent_name, vlan,
1056 br_name, br->parent->name);
1057 } else if (br->vlan != vlan) {
1058 vsctl_fatal("\"--may-exist add-br %s %s %d\" but %s "
1059 "is a VLAN bridge for the wrong VLAN %d",
1060 br_name, parent_name, vlan, br_name, br->vlan);
1066 check_conflicts(&info, br_name,
1067 xasprintf("cannot create a bridge named %s", br_name));
1070 struct ovsrec_port *port;
1071 struct ovsrec_interface *iface;
1072 struct ovsrec_bridge *br;
1074 iface = ovsrec_interface_insert(ctx->txn);
1075 ovsrec_interface_set_name(iface, br_name);
1077 port = ovsrec_port_insert(ctx->txn);
1078 ovsrec_port_set_name(port, br_name);
1079 ovsrec_port_set_interfaces(port, &iface, 1);
1081 br = ovsrec_bridge_insert(ctx->txn);
1082 ovsrec_bridge_set_name(br, br_name);
1083 ovsrec_bridge_set_ports(br, &port, 1);
1085 ovs_insert_bridge(ctx->ovs, br);
1087 struct vsctl_bridge *parent;
1088 struct ovsrec_port *port;
1089 struct ovsrec_interface *iface;
1090 struct ovsrec_bridge *br;
1093 parent = find_bridge(&info, parent_name, false);
1094 if (parent && parent->vlan) {
1095 vsctl_fatal("cannot create bridge with fake bridge as parent");
1098 vsctl_fatal("parent bridge %s does not exist", parent_name);
1100 br = parent->br_cfg;
1102 iface = ovsrec_interface_insert(ctx->txn);
1103 ovsrec_interface_set_name(iface, br_name);
1104 ovsrec_interface_set_type(iface, "internal");
1106 port = ovsrec_port_insert(ctx->txn);
1107 ovsrec_port_set_name(port, br_name);
1108 ovsrec_port_set_interfaces(port, &iface, 1);
1109 ovsrec_port_set_fake_bridge(port, true);
1110 ovsrec_port_set_tag(port, &tag, 1);
1112 bridge_insert_port(br, port);
1119 del_port(struct vsctl_info *info, struct vsctl_port *port)
1121 struct shash_node *node;
1123 SHASH_FOR_EACH (node, &info->ifaces) {
1124 struct vsctl_iface *iface = node->data;
1125 if (iface->port == port) {
1126 ovsrec_interface_delete(iface->iface_cfg);
1129 ovsrec_port_delete(port->port_cfg);
1131 bridge_delete_port((port->bridge->parent
1132 ? port->bridge->parent->br_cfg
1133 : port->bridge->br_cfg), port->port_cfg);
1137 cmd_del_br(struct vsctl_context *ctx)
1139 bool must_exist = !shash_find(&ctx->options, "--if-exists");
1140 struct vsctl_bridge *bridge;
1141 struct vsctl_info info;
1143 get_info(ctx, &info);
1144 bridge = find_bridge(&info, ctx->argv[1], must_exist);
1146 struct shash_node *node;
1148 SHASH_FOR_EACH (node, &info.ports) {
1149 struct vsctl_port *port = node->data;
1150 if (port->bridge == bridge || port->bridge->parent == bridge
1151 || !strcmp(port->port_cfg->name, bridge->name)) {
1152 del_port(&info, port);
1155 if (bridge->br_cfg) {
1156 ovsrec_bridge_delete(bridge->br_cfg);
1157 ovs_delete_bridge(ctx->ovs, bridge->br_cfg);
1164 output_sorted(struct svec *svec, struct ds *output)
1170 SVEC_FOR_EACH (i, name, svec) {
1171 ds_put_format(output, "%s\n", name);
1176 cmd_list_br(struct vsctl_context *ctx)
1178 struct shash_node *node;
1179 struct vsctl_info info;
1180 struct svec bridges;
1182 get_info(ctx, &info);
1184 svec_init(&bridges);
1185 SHASH_FOR_EACH (node, &info.bridges) {
1186 struct vsctl_bridge *br = node->data;
1187 svec_add(&bridges, br->name);
1189 output_sorted(&bridges, &ctx->output);
1190 svec_destroy(&bridges);
1196 cmd_br_exists(struct vsctl_context *ctx)
1198 struct vsctl_info info;
1200 get_info(ctx, &info);
1201 if (!find_bridge(&info, ctx->argv[1], false)) {
1207 /* Returns true if 'b_prefix' (of length 'b_prefix_len') concatenated with 'b'
1208 * equals 'a', false otherwise. */
1210 key_matches(const char *a,
1211 const char *b_prefix, size_t b_prefix_len, const char *b)
1213 return !strncmp(a, b_prefix, b_prefix_len) && !strcmp(a + b_prefix_len, b);
1217 set_external_id(char **old_keys, char **old_values, size_t old_n,
1218 char *key, char *value,
1219 char ***new_keysp, char ***new_valuesp, size_t *new_np)
1226 new_keys = xmalloc(sizeof *new_keys * (old_n + 1));
1227 new_values = xmalloc(sizeof *new_values * (old_n + 1));
1229 for (i = 0; i < old_n; i++) {
1230 if (strcmp(key, old_keys[i])) {
1231 new_keys[new_n] = old_keys[i];
1232 new_values[new_n] = old_values[i];
1237 new_keys[new_n] = key;
1238 new_values[new_n] = value;
1241 *new_keysp = new_keys;
1242 *new_valuesp = new_values;
1247 cmd_br_set_external_id(struct vsctl_context *ctx)
1249 struct vsctl_info info;
1250 struct vsctl_bridge *bridge;
1251 char **keys, **values;
1254 get_info(ctx, &info);
1255 bridge = find_bridge(&info, ctx->argv[1], true);
1256 if (bridge->br_cfg) {
1257 set_external_id(bridge->br_cfg->key_external_ids,
1258 bridge->br_cfg->value_external_ids,
1259 bridge->br_cfg->n_external_ids,
1260 ctx->argv[2], ctx->argc >= 4 ? ctx->argv[3] : NULL,
1261 &keys, &values, &n);
1262 ovsrec_bridge_verify_external_ids(bridge->br_cfg);
1263 ovsrec_bridge_set_external_ids(bridge->br_cfg, keys, values, n);
1265 char *key = xasprintf("fake-bridge-%s", ctx->argv[2]);
1266 struct vsctl_port *port = shash_find_data(&info.ports, ctx->argv[1]);
1267 set_external_id(port->port_cfg->key_external_ids,
1268 port->port_cfg->value_external_ids,
1269 port->port_cfg->n_external_ids,
1270 key, ctx->argc >= 4 ? ctx->argv[3] : NULL,
1271 &keys, &values, &n);
1272 ovsrec_port_verify_external_ids(port->port_cfg);
1273 ovsrec_port_set_external_ids(port->port_cfg, keys, values, n);
1283 get_external_id(char **keys, char **values, size_t n,
1284 const char *prefix, const char *key,
1287 size_t prefix_len = strlen(prefix);
1292 for (i = 0; i < n; i++) {
1293 if (!key && !strncmp(keys[i], prefix, prefix_len)) {
1294 svec_add_nocopy(&svec, xasprintf("%s=%s",
1295 keys[i] + prefix_len, values[i]));
1296 } else if (key_matches(keys[i], prefix, prefix_len, key)) {
1297 svec_add(&svec, values[i]);
1301 output_sorted(&svec, output);
1302 svec_destroy(&svec);
1306 cmd_br_get_external_id(struct vsctl_context *ctx)
1308 struct vsctl_info info;
1309 struct vsctl_bridge *bridge;
1311 get_info(ctx, &info);
1312 bridge = find_bridge(&info, ctx->argv[1], true);
1313 if (bridge->br_cfg) {
1314 ovsrec_bridge_verify_external_ids(bridge->br_cfg);
1315 get_external_id(bridge->br_cfg->key_external_ids,
1316 bridge->br_cfg->value_external_ids,
1317 bridge->br_cfg->n_external_ids,
1318 "", ctx->argc >= 3 ? ctx->argv[2] : NULL,
1321 struct vsctl_port *port = shash_find_data(&info.ports, ctx->argv[1]);
1322 ovsrec_port_verify_external_ids(port->port_cfg);
1323 get_external_id(port->port_cfg->key_external_ids,
1324 port->port_cfg->value_external_ids,
1325 port->port_cfg->n_external_ids,
1326 "fake-bridge-", ctx->argc >= 3 ? ctx->argv[2] : NULL, &ctx->output);
1333 cmd_list_ports(struct vsctl_context *ctx)
1335 struct vsctl_bridge *br;
1336 struct shash_node *node;
1337 struct vsctl_info info;
1340 get_info(ctx, &info);
1341 br = find_bridge(&info, ctx->argv[1], true);
1342 ovsrec_bridge_verify_ports(br->br_cfg ? br->br_cfg : br->parent->br_cfg);
1345 SHASH_FOR_EACH (node, &info.ports) {
1346 struct vsctl_port *port = node->data;
1348 if (strcmp(port->port_cfg->name, br->name) && br == port->bridge) {
1349 svec_add(&ports, port->port_cfg->name);
1352 output_sorted(&ports, &ctx->output);
1353 svec_destroy(&ports);
1359 add_port(struct vsctl_context *ctx,
1360 const char *br_name, const char *port_name,
1361 bool may_exist, bool fake_iface,
1362 char *iface_names[], int n_ifaces,
1363 char *settings[], int n_settings)
1365 struct vsctl_info info;
1366 struct vsctl_bridge *bridge;
1367 struct ovsrec_interface **ifaces;
1368 struct ovsrec_port *port;
1371 get_info(ctx, &info);
1373 struct vsctl_port *vsctl_port;
1375 vsctl_port = find_port(&info, port_name, false);
1377 struct svec want_names, have_names;
1379 svec_init(&want_names);
1380 for (i = 0; i < n_ifaces; i++) {
1381 svec_add(&want_names, iface_names[i]);
1383 svec_sort(&want_names);
1385 svec_init(&have_names);
1386 for (i = 0; i < vsctl_port->port_cfg->n_interfaces; i++) {
1387 svec_add(&have_names,
1388 vsctl_port->port_cfg->interfaces[i]->name);
1390 svec_sort(&have_names);
1392 if (strcmp(vsctl_port->bridge->name, br_name)) {
1393 char *command = vsctl_context_to_string(ctx);
1394 vsctl_fatal("\"%s\" but %s is actually attached to bridge %s",
1395 command, port_name, vsctl_port->bridge->name);
1398 if (!svec_equal(&want_names, &have_names)) {
1399 char *have_names_string = svec_join(&have_names, ", ", "");
1400 char *command = vsctl_context_to_string(ctx);
1402 vsctl_fatal("\"%s\" but %s actually has interface(s) %s",
1403 command, port_name, have_names_string);
1406 svec_destroy(&want_names);
1407 svec_destroy(&have_names);
1412 check_conflicts(&info, port_name,
1413 xasprintf("cannot create a port named %s", port_name));
1414 for (i = 0; i < n_ifaces; i++) {
1415 check_conflicts(&info, iface_names[i],
1416 xasprintf("cannot create an interface named %s",
1419 bridge = find_bridge(&info, br_name, true);
1421 ifaces = xmalloc(n_ifaces * sizeof *ifaces);
1422 for (i = 0; i < n_ifaces; i++) {
1423 ifaces[i] = ovsrec_interface_insert(ctx->txn);
1424 ovsrec_interface_set_name(ifaces[i], iface_names[i]);
1427 port = ovsrec_port_insert(ctx->txn);
1428 ovsrec_port_set_name(port, port_name);
1429 ovsrec_port_set_interfaces(port, ifaces, n_ifaces);
1430 ovsrec_port_set_bond_fake_iface(port, fake_iface);
1434 int64_t tag = bridge->vlan;
1435 ovsrec_port_set_tag(port, &tag, 1);
1438 for (i = 0; i < n_settings; i++) {
1439 set_column(get_table("Port"), &port->header_, settings[i],
1443 bridge_insert_port((bridge->parent ? bridge->parent->br_cfg
1444 : bridge->br_cfg), port);
1450 cmd_add_port(struct vsctl_context *ctx)
1452 bool may_exist = shash_find(&ctx->options, "--may-exist") != 0;
1454 add_port(ctx, ctx->argv[1], ctx->argv[2], may_exist, false,
1455 &ctx->argv[2], 1, &ctx->argv[3], ctx->argc - 3);
1459 cmd_add_bond(struct vsctl_context *ctx)
1461 bool may_exist = shash_find(&ctx->options, "--may-exist") != 0;
1462 bool fake_iface = shash_find(&ctx->options, "--fake-iface");
1466 n_ifaces = ctx->argc - 3;
1467 for (i = 3; i < ctx->argc; i++) {
1468 if (strchr(ctx->argv[i], '=')) {
1474 vsctl_fatal("add-bond requires at least 2 interfaces, but only "
1475 "%d were specified", n_ifaces);
1478 add_port(ctx, ctx->argv[1], ctx->argv[2], may_exist, fake_iface,
1479 &ctx->argv[3], n_ifaces,
1480 &ctx->argv[n_ifaces + 3], ctx->argc - 3 - n_ifaces);
1484 cmd_del_port(struct vsctl_context *ctx)
1486 bool must_exist = !shash_find(&ctx->options, "--if-exists");
1487 bool with_iface = shash_find(&ctx->options, "--with-iface") != NULL;
1488 struct vsctl_port *port;
1489 struct vsctl_info info;
1491 get_info(ctx, &info);
1493 port = find_port(&info, ctx->argv[ctx->argc - 1], must_exist);
1495 const char *target = ctx->argv[ctx->argc - 1];
1496 struct vsctl_iface *iface;
1498 port = find_port(&info, target, false);
1500 iface = find_iface(&info, target, false);
1505 if (must_exist && !port) {
1506 vsctl_fatal("no port or interface named %s", target);
1511 if (ctx->argc == 3) {
1512 struct vsctl_bridge *bridge;
1514 bridge = find_bridge(&info, ctx->argv[1], true);
1515 if (port->bridge != bridge) {
1516 if (port->bridge->parent == bridge) {
1517 vsctl_fatal("bridge %s does not have a port %s (although "
1518 "its parent bridge %s does)",
1519 ctx->argv[1], ctx->argv[2],
1520 bridge->parent->name);
1522 vsctl_fatal("bridge %s does not have a port %s",
1523 ctx->argv[1], ctx->argv[2]);
1528 del_port(&info, port);
1535 cmd_port_to_br(struct vsctl_context *ctx)
1537 struct vsctl_port *port;
1538 struct vsctl_info info;
1540 get_info(ctx, &info);
1541 port = find_port(&info, ctx->argv[1], true);
1542 ds_put_format(&ctx->output, "%s\n", port->bridge->name);
1547 cmd_br_to_vlan(struct vsctl_context *ctx)
1549 struct vsctl_bridge *bridge;
1550 struct vsctl_info info;
1552 get_info(ctx, &info);
1553 bridge = find_bridge(&info, ctx->argv[1], true);
1554 ds_put_format(&ctx->output, "%d\n", bridge->vlan);
1559 cmd_br_to_parent(struct vsctl_context *ctx)
1561 struct vsctl_bridge *bridge;
1562 struct vsctl_info info;
1564 get_info(ctx, &info);
1565 bridge = find_bridge(&info, ctx->argv[1], true);
1566 if (bridge->parent) {
1567 bridge = bridge->parent;
1569 ds_put_format(&ctx->output, "%s\n", bridge->name);
1574 cmd_list_ifaces(struct vsctl_context *ctx)
1576 struct vsctl_bridge *br;
1577 struct shash_node *node;
1578 struct vsctl_info info;
1581 get_info(ctx, &info);
1582 br = find_bridge(&info, ctx->argv[1], true);
1586 SHASH_FOR_EACH (node, &info.ifaces) {
1587 struct vsctl_iface *iface = node->data;
1589 if (strcmp(iface->iface_cfg->name, br->name)
1590 && br == iface->port->bridge) {
1591 svec_add(&ifaces, iface->iface_cfg->name);
1594 output_sorted(&ifaces, &ctx->output);
1595 svec_destroy(&ifaces);
1601 cmd_iface_to_br(struct vsctl_context *ctx)
1603 struct vsctl_iface *iface;
1604 struct vsctl_info info;
1606 get_info(ctx, &info);
1607 iface = find_iface(&info, ctx->argv[1], true);
1608 ds_put_format(&ctx->output, "%s\n", iface->port->bridge->name);
1613 verify_controllers(struct ovsrec_bridge *bridge)
1618 ovsrec_bridge_verify_controller(bridge);
1619 for (i = 0; i < bridge->n_controller; i++) {
1620 ovsrec_controller_verify_target(bridge->controller[i]);
1626 cmd_get_controller(struct vsctl_context *ctx)
1628 struct vsctl_info info;
1629 struct vsctl_bridge *br;
1630 struct svec targets;
1633 get_info(ctx, &info);
1634 br = find_bridge(&info, ctx->argv[1], true);
1635 verify_controllers(br->br_cfg);
1637 /* Print the targets in sorted order for reproducibility. */
1638 svec_init(&targets);
1639 for (i = 0; i < br->n_ctrl; i++) {
1640 svec_add(&targets, br->ctrl[i]->target);
1643 svec_sort(&targets);
1644 for (i = 0; i < targets.n; i++) {
1645 ds_put_format(&ctx->output, "%s\n", targets.names[i]);
1647 svec_destroy(&targets);
1653 delete_controllers(struct ovsrec_controller **controllers,
1654 size_t n_controllers)
1658 for (i = 0; i < n_controllers; i++) {
1659 ovsrec_controller_delete(controllers[i]);
1664 cmd_del_controller(struct vsctl_context *ctx)
1666 struct vsctl_info info;
1667 struct vsctl_bridge *br;
1669 get_info(ctx, &info);
1670 br = find_real_bridge(&info, ctx->argv[1], true);
1671 verify_controllers(br->br_cfg);
1674 delete_controllers(br->ctrl, br->n_ctrl);
1675 ovsrec_bridge_set_controller(br->br_cfg, NULL, 0);
1681 static struct ovsrec_controller **
1682 insert_controllers(struct ovsdb_idl_txn *txn, char *targets[], size_t n)
1684 struct ovsrec_controller **controllers;
1687 controllers = xmalloc(n * sizeof *controllers);
1688 for (i = 0; i < n; i++) {
1689 controllers[i] = ovsrec_controller_insert(txn);
1690 ovsrec_controller_set_target(controllers[i], targets[i]);
1697 cmd_set_controller(struct vsctl_context *ctx)
1699 struct vsctl_info info;
1700 struct vsctl_bridge *br;
1701 struct ovsrec_controller **controllers;
1704 get_info(ctx, &info);
1705 br = find_real_bridge(&info, ctx->argv[1], true);
1706 verify_controllers(br->br_cfg);
1708 delete_controllers(br->ctrl, br->n_ctrl);
1711 controllers = insert_controllers(ctx->txn, &ctx->argv[2], n);
1712 ovsrec_bridge_set_controller(br->br_cfg, controllers, n);
1719 cmd_get_fail_mode(struct vsctl_context *ctx)
1721 struct vsctl_info info;
1722 struct vsctl_bridge *br;
1724 get_info(ctx, &info);
1725 br = find_bridge(&info, ctx->argv[1], true);
1728 ovsrec_bridge_verify_fail_mode(br->br_cfg);
1730 if (br->fail_mode && strlen(br->fail_mode)) {
1731 ds_put_format(&ctx->output, "%s\n", br->fail_mode);
1738 cmd_del_fail_mode(struct vsctl_context *ctx)
1740 struct vsctl_info info;
1741 struct vsctl_bridge *br;
1743 get_info(ctx, &info);
1744 br = find_real_bridge(&info, ctx->argv[1], true);
1746 ovsrec_bridge_set_fail_mode(br->br_cfg, NULL);
1752 cmd_set_fail_mode(struct vsctl_context *ctx)
1754 struct vsctl_info info;
1755 struct vsctl_bridge *br;
1756 const char *fail_mode = ctx->argv[2];
1758 get_info(ctx, &info);
1759 br = find_real_bridge(&info, ctx->argv[1], true);
1761 if (strcmp(fail_mode, "standalone") && strcmp(fail_mode, "secure")) {
1762 vsctl_fatal("fail-mode must be \"standalone\" or \"secure\"");
1765 ovsrec_bridge_set_fail_mode(br->br_cfg, fail_mode);
1771 cmd_get_ssl(struct vsctl_context *ctx)
1773 struct ovsrec_ssl *ssl = ctx->ovs->ssl;
1775 ovsrec_open_vswitch_verify_ssl(ctx->ovs);
1777 ovsrec_ssl_verify_private_key(ssl);
1778 ovsrec_ssl_verify_certificate(ssl);
1779 ovsrec_ssl_verify_ca_cert(ssl);
1780 ovsrec_ssl_verify_bootstrap_ca_cert(ssl);
1782 ds_put_format(&ctx->output, "Private key: %s\n", ssl->private_key);
1783 ds_put_format(&ctx->output, "Certificate: %s\n", ssl->certificate);
1784 ds_put_format(&ctx->output, "CA Certificate: %s\n", ssl->ca_cert);
1785 ds_put_format(&ctx->output, "Bootstrap: %s\n",
1786 ssl->bootstrap_ca_cert ? "true" : "false");
1791 cmd_del_ssl(struct vsctl_context *ctx)
1793 struct ovsrec_ssl *ssl = ctx->ovs->ssl;
1796 ovsrec_open_vswitch_verify_ssl(ctx->ovs);
1797 ovsrec_ssl_delete(ssl);
1798 ovsrec_open_vswitch_set_ssl(ctx->ovs, NULL);
1803 cmd_set_ssl(struct vsctl_context *ctx)
1805 bool bootstrap = shash_find(&ctx->options, "--bootstrap");
1806 struct ovsrec_ssl *ssl = ctx->ovs->ssl;
1808 ovsrec_open_vswitch_verify_ssl(ctx->ovs);
1810 ovsrec_ssl_delete(ssl);
1812 ssl = ovsrec_ssl_insert(ctx->txn);
1814 ovsrec_ssl_set_private_key(ssl, ctx->argv[1]);
1815 ovsrec_ssl_set_certificate(ssl, ctx->argv[2]);
1816 ovsrec_ssl_set_ca_cert(ssl, ctx->argv[3]);
1818 ovsrec_ssl_set_bootstrap_ca_cert(ssl, bootstrap);
1820 ovsrec_open_vswitch_set_ssl(ctx->ovs, ssl);
1823 /* Parameter commands. */
1825 struct vsctl_row_id {
1826 const struct ovsdb_idl_table_class *table;
1827 const struct ovsdb_idl_column *name_column;
1828 const struct ovsdb_idl_column *uuid_column;
1831 struct vsctl_table_class {
1832 struct ovsdb_idl_table_class *class;
1833 struct vsctl_row_id row_ids[2];
1836 static const struct vsctl_table_class tables[] = {
1837 {&ovsrec_table_bridge,
1838 {{&ovsrec_table_bridge, &ovsrec_bridge_col_name, NULL},
1839 {NULL, NULL, NULL}}},
1841 {&ovsrec_table_controller,
1842 {{&ovsrec_table_bridge,
1843 &ovsrec_bridge_col_name,
1844 &ovsrec_bridge_col_controller}}},
1846 {&ovsrec_table_interface,
1847 {{&ovsrec_table_interface, &ovsrec_interface_col_name, NULL},
1848 {NULL, NULL, NULL}}},
1850 {&ovsrec_table_mirror,
1851 {{&ovsrec_table_mirror, &ovsrec_mirror_col_name, NULL},
1852 {NULL, NULL, NULL}}},
1854 {&ovsrec_table_netflow,
1855 {{&ovsrec_table_bridge,
1856 &ovsrec_bridge_col_name,
1857 &ovsrec_bridge_col_netflow},
1858 {NULL, NULL, NULL}}},
1860 {&ovsrec_table_open_vswitch,
1861 {{&ovsrec_table_open_vswitch, NULL, NULL},
1862 {NULL, NULL, NULL}}},
1864 {&ovsrec_table_port,
1865 {{&ovsrec_table_port, &ovsrec_port_col_name, NULL},
1866 {NULL, NULL, NULL}}},
1869 {{&ovsrec_table_port, &ovsrec_port_col_name, &ovsrec_port_col_qos},
1870 {NULL, NULL, NULL}}},
1872 {&ovsrec_table_queue,
1873 {{NULL, NULL, NULL},
1874 {NULL, NULL, NULL}}},
1877 {{&ovsrec_table_open_vswitch, NULL, &ovsrec_open_vswitch_col_ssl}}},
1879 {&ovsrec_table_sflow,
1880 {{&ovsrec_table_bridge,
1881 &ovsrec_bridge_col_name,
1882 &ovsrec_bridge_col_sflow},
1883 {NULL, NULL, NULL}}},
1885 {NULL, {{NULL, NULL, NULL}, {NULL, NULL, NULL}}}
1889 die_if_error(char *error)
1892 vsctl_fatal("%s", error);
1897 to_lower_and_underscores(unsigned c)
1899 return c == '-' ? '_' : tolower(c);
1903 score_partial_match(const char *name, const char *s)
1907 if (!strcmp(name, s)) {
1910 for (score = 0; ; score++, name++, s++) {
1911 if (to_lower_and_underscores(*name) != to_lower_and_underscores(*s)) {
1913 } else if (*name == '\0') {
1914 return UINT_MAX - 1;
1917 return *s == '\0' ? score : 0;
1920 static const struct vsctl_table_class *
1921 get_table(const char *table_name)
1923 const struct vsctl_table_class *table;
1924 const struct vsctl_table_class *best_match = NULL;
1925 unsigned int best_score = 0;
1927 for (table = tables; table->class; table++) {
1928 unsigned int score = score_partial_match(table->class->name,
1930 if (score > best_score) {
1933 } else if (score == best_score) {
1939 } else if (best_score) {
1940 vsctl_fatal("multiple table names match \"%s\"", table_name);
1942 vsctl_fatal("unknown table \"%s\"", table_name);
1946 static const struct ovsdb_idl_row *
1947 get_row_by_id(struct vsctl_context *ctx, const struct vsctl_table_class *table,
1948 const struct vsctl_row_id *id, const char *record_id)
1950 const struct ovsdb_idl_row *referrer, *final;
1956 if (!id->name_column) {
1957 if (strcmp(record_id, ".")) {
1960 referrer = ovsdb_idl_first_row(ctx->idl, id->table);
1961 if (!referrer || ovsdb_idl_next_row(referrer)) {
1965 const struct ovsdb_idl_row *row;
1968 for (row = ovsdb_idl_first_row(ctx->idl, id->table);
1970 row = ovsdb_idl_next_row(row))
1972 const struct ovsdb_datum *name;
1974 name = ovsdb_idl_get(row, id->name_column,
1975 OVSDB_TYPE_STRING, OVSDB_TYPE_VOID);
1976 if (name->n == 1 && !strcmp(name->keys[0].string, record_id)) {
1978 vsctl_fatal("multiple rows in %s match \"%s\"",
1979 table->class->name, record_id);
1990 if (id->uuid_column) {
1991 const struct ovsdb_datum *uuid;
1993 ovsdb_idl_txn_verify(referrer, id->uuid_column);
1994 uuid = ovsdb_idl_get(referrer, id->uuid_column,
1995 OVSDB_TYPE_UUID, OVSDB_TYPE_VOID);
1997 final = ovsdb_idl_get_row_for_uuid(ctx->idl, table->class,
1998 &uuid->keys[0].uuid);
2007 static const struct ovsdb_idl_row *
2008 get_row (struct vsctl_context *ctx,
2009 const struct vsctl_table_class *table, const char *record_id)
2011 const struct ovsdb_idl_row *row;
2014 if (uuid_from_string(&uuid, record_id)) {
2015 row = ovsdb_idl_get_row_for_uuid(ctx->idl, table->class, &uuid);
2019 for (i = 0; i < ARRAY_SIZE(table->row_ids); i++) {
2020 row = get_row_by_id(ctx, table, &table->row_ids[i], record_id);
2029 static const struct ovsdb_idl_row *
2030 must_get_row(struct vsctl_context *ctx,
2031 const struct vsctl_table_class *table, const char *record_id)
2033 const struct ovsdb_idl_row *row = get_row(ctx, table, record_id);
2035 vsctl_fatal("no row \"%s\" in table %s",
2036 record_id, table->class->name);
2042 get_column(const struct vsctl_table_class *table, const char *column_name,
2043 const struct ovsdb_idl_column **columnp)
2045 const struct ovsdb_idl_column *best_match = NULL;
2046 unsigned int best_score = 0;
2049 for (i = 0; i < table->class->n_columns; i++) {
2050 const struct ovsdb_idl_column *column = &table->class->columns[i];
2051 unsigned int score = score_partial_match(column->name, column_name);
2052 if (score > best_score) {
2053 best_match = column;
2055 } else if (score == best_score) {
2060 *columnp = best_match;
2063 } else if (best_score) {
2064 return xasprintf("%s contains more than one column whose name "
2065 "matches \"%s\"", table->class->name, column_name);
2067 return xasprintf("%s does not contain a column whose name matches "
2068 "\"%s\"", table->class->name, column_name);
2072 static struct uuid *
2073 create_symbol(struct ovsdb_symbol_table *symtab, const char *id, bool *newp)
2075 struct ovsdb_symbol *symbol;
2078 vsctl_fatal("row id \"%s\" does not begin with \"@\"", id);
2082 *newp = ovsdb_symbol_table_get(symtab, id) == NULL;
2085 symbol = ovsdb_symbol_table_insert(symtab, id);
2087 vsctl_fatal("row id \"%s\" may only be specified on one --id option",
2090 symbol->used = true;
2091 return &symbol->uuid;
2095 missing_operator_error(const char *arg, const char **allowed_operators,
2101 ds_put_format(&s, "%s: argument does not end in ", arg);
2102 ds_put_format(&s, "\"%s\"", allowed_operators[0]);
2103 if (n_allowed == 2) {
2104 ds_put_format(&s, " or \"%s\"", allowed_operators[1]);
2105 } else if (n_allowed > 2) {
2108 for (i = 1; i < n_allowed - 1; i++) {
2109 ds_put_format(&s, ", \"%s\"", allowed_operators[i]);
2111 ds_put_format(&s, ", or \"%s\"", allowed_operators[i]);
2113 ds_put_format(&s, " followed by a value.");
2115 return ds_steal_cstr(&s);
2118 /* Breaks 'arg' apart into a number of fields in the following order:
2120 * - If 'columnp' is nonnull, the name of a column in 'table'. The column
2121 * is stored into '*columnp'. The column name may be abbreviated.
2123 * - If 'keyp' is nonnull, optionally a key string. (If both 'columnp'
2124 * and 'keyp' are nonnull, then the column and key names are expected to
2125 * be separated by ':'). The key is stored as a malloc()'d string into
2126 * '*keyp', or NULL if no key is present in 'arg'.
2128 * - If 'valuep' is nonnull, an operator followed by a value string. The
2129 * allowed operators are the 'n_allowed' string in 'allowed_operators',
2130 * or just "=" if 'n_allowed' is 0. If 'operatorp' is nonnull, then the
2131 * operator is stored into '*operatorp' (one of the pointers from
2132 * 'allowed_operators' is stored; nothing is malloc()'d). The value is
2133 * stored as a malloc()'d string into '*valuep', or NULL if no value is
2136 * At least 'columnp' or 'keyp' must be nonnull.
2138 * On success, returns NULL. On failure, returned a malloc()'d string error
2139 * message and stores NULL into all of the nonnull output arguments. */
2140 static char * WARN_UNUSED_RESULT
2141 parse_column_key_value(const char *arg,
2142 const struct vsctl_table_class *table,
2143 const struct ovsdb_idl_column **columnp, char **keyp,
2144 const char **operatorp,
2145 const char **allowed_operators, size_t n_allowed,
2148 const char *p = arg;
2151 assert(columnp || keyp);
2152 assert(!(operatorp && !valuep));
2160 /* Parse column name. */
2164 error = ovsdb_token_parse(&p, &column_name);
2168 if (column_name[0] == '\0') {
2170 error = xasprintf("%s: missing column name", arg);
2173 error = get_column(table, column_name, columnp);
2180 /* Parse key string. */
2181 if (*p == ':' || !columnp) {
2185 error = xasprintf("%s: key not accepted here", arg);
2188 error = ovsdb_token_parse(&p, keyp);
2196 /* Parse value string. */
2202 if (!allowed_operators) {
2203 static const char *equals = "=";
2204 allowed_operators = =
2210 for (i = 0; i < n_allowed; i++) {
2211 const char *op = allowed_operators[i];
2212 size_t op_len = strlen(op);
2214 if (op_len > best_len && !strncmp(op, p, op_len) && p[op_len]) {
2220 error = missing_operator_error(arg, allowed_operators, n_allowed);
2227 *valuep = xstrdup(p + best_len);
2233 error = xasprintf("%s: trailing garbage \"%s\" in argument",
2259 cmd_get(struct vsctl_context *ctx)
2261 const char *id = shash_find_data(&ctx->options, "--id");
2262 bool if_exists = shash_find(&ctx->options, "--if-exists");
2263 const char *table_name = ctx->argv[1];
2264 const char *record_id = ctx->argv[2];
2265 const struct vsctl_table_class *table;
2266 const struct ovsdb_idl_row *row;
2267 struct ds *out = &ctx->output;
2270 table = get_table(table_name);
2271 row = must_get_row(ctx, table, record_id);
2275 *create_symbol(ctx->symtab, id, &new) = row->uuid;
2277 vsctl_fatal("row id \"%s\" specified on \"get\" command was used "
2278 "before it was defined", id);
2281 for (i = 3; i < ctx->argc; i++) {
2282 const struct ovsdb_idl_column *column;
2283 const struct ovsdb_datum *datum;
2286 /* Special case for obtaining the UUID of a row. We can't just do this
2287 * through parse_column_key_value() below since it returns a "struct
2288 * ovsdb_idl_column" and the UUID column doesn't have one. */
2289 if (!strcasecmp(ctx->argv[i], "_uuid")
2290 || !strcasecmp(ctx->argv[i], "-uuid")) {
2291 ds_put_format(out, UUID_FMT"\n", UUID_ARGS(&row->uuid));
2295 die_if_error(parse_column_key_value(ctx->argv[i], table,
2296 &column, &key_string,
2297 NULL, NULL, 0, NULL));
2299 ovsdb_idl_txn_verify(row, column);
2300 datum = ovsdb_idl_read(row, column);
2302 union ovsdb_atom key;
2305 if (column->type.value.type == OVSDB_TYPE_VOID) {
2306 vsctl_fatal("cannot specify key to get for non-map column %s",
2310 die_if_error(ovsdb_atom_from_string(&key,
2312 key_string, ctx->symtab));
2314 idx = ovsdb_datum_find_key(datum, &key,
2315 column->type.key.type);
2316 if (idx == UINT_MAX) {
2318 vsctl_fatal("no key \"%s\" in %s record \"%s\" column %s",
2319 key_string, table->class->name, record_id,
2323 ovsdb_atom_to_string(&datum->values[idx],
2324 column->type.value.type, out);
2326 ovsdb_atom_destroy(&key, column->type.key.type);
2328 ovsdb_datum_to_string(datum, &column->type, out);
2330 ds_put_char(out, '\n');
2337 list_record(const struct vsctl_table_class *table,
2338 const struct ovsdb_idl_row *row, struct ds *out)
2342 ds_put_format(out, "%-20s: "UUID_FMT"\n", "_uuid",
2343 UUID_ARGS(&row->uuid));
2344 for (i = 0; i < table->class->n_columns; i++) {
2345 const struct ovsdb_idl_column *column = &table->class->columns[i];
2346 const struct ovsdb_datum *datum;
2348 datum = ovsdb_idl_read(row, column);
2350 ds_put_format(out, "%-20s: ", column->name);
2351 ovsdb_datum_to_string(datum, &column->type, out);
2352 ds_put_char(out, '\n');
2357 cmd_list(struct vsctl_context *ctx)
2359 const char *table_name = ctx->argv[1];
2360 const struct vsctl_table_class *table;
2361 struct ds *out = &ctx->output;
2364 table = get_table(table_name);
2365 if (ctx->argc > 2) {
2366 for (i = 2; i < ctx->argc; i++) {
2368 ds_put_char(out, '\n');
2370 list_record(table, must_get_row(ctx, table, ctx->argv[i]), out);
2373 const struct ovsdb_idl_row *row;
2376 for (row = ovsdb_idl_first_row(ctx->idl, table->class), first = true;
2378 row = ovsdb_idl_next_row(row), first = false) {
2380 ds_put_char(out, '\n');
2382 list_record(table, row, out);
2388 set_column(const struct vsctl_table_class *table,
2389 const struct ovsdb_idl_row *row, const char *arg,
2390 struct ovsdb_symbol_table *symtab)
2392 const struct ovsdb_idl_column *column;
2393 char *key_string, *value_string;
2396 error = parse_column_key_value(arg, table, &column, &key_string,
2397 NULL, NULL, 0, &value_string);
2398 die_if_error(error);
2399 if (!value_string) {
2400 vsctl_fatal("%s: missing value", arg);
2404 union ovsdb_atom key, value;
2405 struct ovsdb_datum datum;
2407 if (column->type.value.type == OVSDB_TYPE_VOID) {
2408 vsctl_fatal("cannot specify key to set for non-map column %s",
2412 die_if_error(ovsdb_atom_from_string(&key, &column->type.key,
2413 key_string, symtab));
2414 die_if_error(ovsdb_atom_from_string(&value, &column->type.value,
2415 value_string, symtab));
2417 ovsdb_datum_init_empty(&datum);
2418 ovsdb_datum_add_unsafe(&datum, &key, &value, &column->type);
2420 ovsdb_atom_destroy(&key, column->type.key.type);
2421 ovsdb_atom_destroy(&value, column->type.value.type);
2423 ovsdb_datum_union(&datum, ovsdb_idl_read(row, column),
2424 &column->type, false);
2425 ovsdb_idl_txn_write(row, column, &datum);
2427 struct ovsdb_datum datum;
2429 die_if_error(ovsdb_datum_from_string(&datum, &column->type,
2430 value_string, symtab));
2431 ovsdb_idl_txn_write(row, column, &datum);
2439 cmd_set(struct vsctl_context *ctx)
2441 const char *table_name = ctx->argv[1];
2442 const char *record_id = ctx->argv[2];
2443 const struct vsctl_table_class *table;
2444 const struct ovsdb_idl_row *row;
2447 table = get_table(table_name);
2448 row = must_get_row(ctx, table, record_id);
2449 for (i = 3; i < ctx->argc; i++) {
2450 set_column(table, row, ctx->argv[i], ctx->symtab);
2455 cmd_add(struct vsctl_context *ctx)
2457 const char *table_name = ctx->argv[1];
2458 const char *record_id = ctx->argv[2];
2459 const char *column_name = ctx->argv[3];
2460 const struct vsctl_table_class *table;
2461 const struct ovsdb_idl_column *column;
2462 const struct ovsdb_idl_row *row;
2463 const struct ovsdb_type *type;
2464 struct ovsdb_datum old;
2467 table = get_table(table_name);
2468 row = must_get_row(ctx, table, record_id);
2469 die_if_error(get_column(table, column_name, &column));
2471 type = &column->type;
2472 ovsdb_datum_clone(&old, ovsdb_idl_read(row, column), &column->type);
2473 for (i = 4; i < ctx->argc; i++) {
2474 struct ovsdb_type add_type;
2475 struct ovsdb_datum add;
2479 add_type.n_max = UINT_MAX;
2480 die_if_error(ovsdb_datum_from_string(&add, &add_type, ctx->argv[i],
2482 ovsdb_datum_union(&old, &add, type, false);
2483 ovsdb_datum_destroy(&add, type);
2485 if (old.n > type->n_max) {
2486 vsctl_fatal("\"add\" operation would put %u %s in column %s of "
2487 "table %s but the maximum number is %u",
2489 type->value.type == OVSDB_TYPE_VOID ? "values" : "pairs",
2490 column->name, table->class->name, type->n_max);
2492 ovsdb_idl_txn_verify(row, column);
2493 ovsdb_idl_txn_write(row, column, &old);
2497 cmd_remove(struct vsctl_context *ctx)
2499 const char *table_name = ctx->argv[1];
2500 const char *record_id = ctx->argv[2];
2501 const char *column_name = ctx->argv[3];
2502 const struct vsctl_table_class *table;
2503 const struct ovsdb_idl_column *column;
2504 const struct ovsdb_idl_row *row;
2505 const struct ovsdb_type *type;
2506 struct ovsdb_datum old;
2509 table = get_table(table_name);
2510 row = must_get_row(ctx, table, record_id);
2511 die_if_error(get_column(table, column_name, &column));
2513 type = &column->type;
2514 ovsdb_datum_clone(&old, ovsdb_idl_read(row, column), &column->type);
2515 for (i = 4; i < ctx->argc; i++) {
2516 struct ovsdb_type rm_type;
2517 struct ovsdb_datum rm;
2522 rm_type.n_max = UINT_MAX;
2523 error = ovsdb_datum_from_string(&rm, &rm_type,
2524 ctx->argv[i], ctx->symtab);
2525 if (error && ovsdb_type_is_map(&rm_type)) {
2527 rm_type.value.type = OVSDB_TYPE_VOID;
2528 die_if_error(ovsdb_datum_from_string(&rm, &rm_type,
2529 ctx->argv[i], ctx->symtab));
2531 ovsdb_datum_subtract(&old, type, &rm, &rm_type);
2532 ovsdb_datum_destroy(&rm, &rm_type);
2534 if (old.n < type->n_min) {
2535 vsctl_fatal("\"remove\" operation would put %u %s in column %s of "
2536 "table %s but the minimum number is %u",
2538 type->value.type == OVSDB_TYPE_VOID ? "values" : "pairs",
2539 column->name, table->class->name, type->n_min);
2541 ovsdb_idl_txn_verify(row, column);
2542 ovsdb_idl_txn_write(row, column, &old);
2546 cmd_clear(struct vsctl_context *ctx)
2548 const char *table_name = ctx->argv[1];
2549 const char *record_id = ctx->argv[2];
2550 const struct vsctl_table_class *table;
2551 const struct ovsdb_idl_row *row;
2554 table = get_table(table_name);
2555 row = must_get_row(ctx, table, record_id);
2556 for (i = 3; i < ctx->argc; i++) {
2557 const struct ovsdb_idl_column *column;
2558 const struct ovsdb_type *type;
2559 struct ovsdb_datum datum;
2561 die_if_error(get_column(table, ctx->argv[i], &column));
2563 type = &column->type;
2564 if (type->n_min > 0) {
2565 vsctl_fatal("\"clear\" operation cannot be applied to column %s "
2566 "of table %s, which is not allowed to be empty",
2567 column->name, table->class->name);
2570 ovsdb_datum_init_empty(&datum);
2571 ovsdb_idl_txn_write(row, column, &datum);
2576 cmd_create(struct vsctl_context *ctx)
2578 const char *id = shash_find_data(&ctx->options, "--id");
2579 const char *table_name = ctx->argv[1];
2580 const struct vsctl_table_class *table;
2581 const struct ovsdb_idl_row *row;
2582 const struct uuid *uuid;
2585 uuid = id ? create_symbol(ctx->symtab, id, NULL) : NULL;
2587 table = get_table(table_name);
2588 row = ovsdb_idl_txn_insert(ctx->txn, table->class, uuid);
2589 for (i = 2; i < ctx->argc; i++) {
2590 set_column(table, row, ctx->argv[i], ctx->symtab);
2592 ds_put_format(&ctx->output, UUID_FMT, UUID_ARGS(&row->uuid));
2595 /* This function may be used as the 'postprocess' function for commands that
2596 * insert new rows into the database. It expects that the command's 'run'
2597 * function prints the UUID reported by ovsdb_idl_txn_insert() as the command's
2598 * sole output. It replaces that output by the row's permanent UUID assigned
2599 * by the database server and appends a new-line.
2601 * Currently we use this only for "create", because the higher-level commands
2602 * are supposed to be independent of the actual structure of the vswitch
2605 post_create(struct vsctl_context *ctx)
2607 const struct uuid *real;
2610 uuid_from_string(&dummy, ds_cstr(&ctx->output));
2611 real = ovsdb_idl_txn_get_insert_uuid(ctx->txn, &dummy);
2613 ds_clear(&ctx->output);
2614 ds_put_format(&ctx->output, UUID_FMT, UUID_ARGS(real));
2616 ds_put_char(&ctx->output, '\n');
2620 cmd_destroy(struct vsctl_context *ctx)
2622 bool must_exist = !shash_find(&ctx->options, "--if-exists");
2623 const char *table_name = ctx->argv[1];
2624 const struct vsctl_table_class *table;
2627 table = get_table(table_name);
2628 for (i = 2; i < ctx->argc; i++) {
2629 const struct ovsdb_idl_row *row;
2631 row = (must_exist ? must_get_row : get_row)(ctx, table, ctx->argv[i]);
2633 ovsdb_idl_txn_delete(row);
2639 is_condition_satified(const struct vsctl_table_class *table,
2640 const struct ovsdb_idl_row *row, const char *arg,
2641 struct ovsdb_symbol_table *symtab)
2643 static const char *operators[] = {
2644 "=", "!=", "<", ">", "<=", ">="
2647 const struct ovsdb_idl_column *column;
2648 const struct ovsdb_datum *have_datum;
2649 char *key_string, *value_string;
2650 const char *operator;
2655 error = parse_column_key_value(arg, table, &column, &key_string,
2656 &operator, operators, ARRAY_SIZE(operators),
2658 die_if_error(error);
2659 if (!value_string) {
2660 vsctl_fatal("%s: missing value", arg);
2663 have_datum = ovsdb_idl_read(row, column);
2665 union ovsdb_atom want_key, want_value;
2667 if (column->type.value.type == OVSDB_TYPE_VOID) {
2668 vsctl_fatal("cannot specify key to check for non-map column %s",
2672 die_if_error(ovsdb_atom_from_string(&want_key, &column->type.key,
2673 key_string, symtab));
2674 die_if_error(ovsdb_atom_from_string(&want_value, &column->type.value,
2675 value_string, symtab));
2677 idx = ovsdb_datum_find_key(have_datum,
2678 &want_key, column->type.key.type);
2679 if (idx != UINT_MAX) {
2680 cmp = ovsdb_atom_compare_3way(&have_datum->values[idx],
2682 column->type.value.type);
2685 ovsdb_atom_destroy(&want_key, column->type.key.type);
2686 ovsdb_atom_destroy(&want_value, column->type.value.type);
2688 struct ovsdb_datum want_datum;
2690 die_if_error(ovsdb_datum_from_string(&want_datum, &column->type,
2691 value_string, symtab));
2693 cmp = ovsdb_datum_compare_3way(have_datum, &want_datum,
2695 ovsdb_datum_destroy(&want_datum, &column->type);
2701 return (idx == UINT_MAX ? false
2702 : !strcmp(operator, "=") ? cmp == 0
2703 : !strcmp(operator, "!=") ? cmp != 0
2704 : !strcmp(operator, "<") ? cmp < 0
2705 : !strcmp(operator, ">") ? cmp > 0
2706 : !strcmp(operator, "<=") ? cmp <= 0
2707 : !strcmp(operator, ">=") ? cmp >= 0
2712 cmd_wait_until(struct vsctl_context *ctx)
2714 const char *table_name = ctx->argv[1];
2715 const char *record_id = ctx->argv[2];
2716 const struct vsctl_table_class *table;
2717 const struct ovsdb_idl_row *row;
2720 table = get_table(table_name);
2722 row = get_row(ctx, table, record_id);
2724 ctx->try_again = true;
2728 for (i = 3; i < ctx->argc; i++) {
2729 if (!is_condition_satified(table, row, ctx->argv[i], ctx->symtab)) {
2730 ctx->try_again = true;
2736 static struct json *
2737 where_uuid_equals(const struct uuid *uuid)
2740 json_array_create_1(
2741 json_array_create_3(
2742 json_string_create("_uuid"),
2743 json_string_create("=="),
2744 json_array_create_2(
2745 json_string_create("uuid"),
2746 json_string_create_nocopy(
2747 xasprintf(UUID_FMT, UUID_ARGS(uuid))))));
2751 vsctl_context_init(struct vsctl_context *ctx, struct vsctl_command *command,
2752 struct ovsdb_idl *idl, struct ovsdb_idl_txn *txn,
2753 const struct ovsrec_open_vswitch *ovs,
2754 struct ovsdb_symbol_table *symtab)
2756 ctx->argc = command->argc;
2757 ctx->argv = command->argv;
2758 ctx->options = command->options;
2760 ds_swap(&ctx->output, &command->output);
2764 ctx->symtab = symtab;
2765 ctx->verified_ports = false;
2767 ctx->try_again = false;
2771 vsctl_context_done(struct vsctl_context *ctx, struct vsctl_command *command)
2773 ds_swap(&ctx->output, &command->output);
2777 do_vsctl(const char *args, struct vsctl_command *commands, size_t n_commands,
2778 struct ovsdb_idl *idl)
2780 struct ovsdb_idl_txn *txn;
2781 const struct ovsrec_open_vswitch *ovs;
2782 enum ovsdb_idl_txn_status status;
2783 struct ovsdb_symbol_table *symtab;
2785 struct vsctl_command *c;
2786 int64_t next_cfg = 0;
2789 txn = the_idl_txn = ovsdb_idl_txn_create(idl);
2791 ovsdb_idl_txn_set_dry_run(txn);
2794 ovsdb_idl_txn_add_comment(txn, "ovs-vsctl: %s", args);
2796 ovs = ovsrec_open_vswitch_first(idl);
2798 /* XXX add verification that table is empty */
2799 ovs = ovsrec_open_vswitch_insert(txn);
2802 if (wait_for_reload) {
2803 struct json *where = where_uuid_equals(&ovs->header_.uuid);
2804 ovsdb_idl_txn_increment(txn, "Open_vSwitch", "next_cfg", where);
2805 json_destroy(where);
2808 symtab = ovsdb_symbol_table_create();
2809 for (c = commands; c < &commands[n_commands]; c++) {
2810 ds_init(&c->output);
2812 for (c = commands; c < &commands[n_commands]; c++) {
2813 struct vsctl_context ctx;
2815 vsctl_context_init(&ctx, c, idl, txn, ovs, symtab);
2816 (c->syntax->run)(&ctx);
2817 vsctl_context_done(&ctx, c);
2819 if (ctx.try_again) {
2824 status = ovsdb_idl_txn_commit_block(txn);
2825 if (wait_for_reload && status == TXN_SUCCESS) {
2826 next_cfg = ovsdb_idl_txn_get_increment_new_value(txn);
2828 if (status == TXN_UNCHANGED || status == TXN_SUCCESS) {
2829 for (c = commands; c < &commands[n_commands]; c++) {
2830 if (c->syntax->postprocess) {
2831 struct vsctl_context ctx;
2833 vsctl_context_init(&ctx, c, idl, txn, ovs, symtab);
2834 (c->syntax->postprocess)(&ctx);
2835 vsctl_context_done(&ctx, c);
2839 error = xstrdup(ovsdb_idl_txn_get_error(txn));
2840 ovsdb_idl_txn_destroy(txn);
2841 txn = the_idl_txn = NULL;
2843 unused = ovsdb_symbol_table_find_unused(symtab);
2845 vsctl_fatal("row id \"%s\" is referenced but never created (e.g. "
2846 "with \"-- --id=%s create ...\")", unused, unused);
2850 case TXN_INCOMPLETE:
2854 /* Should not happen--we never call ovsdb_idl_txn_abort(). */
2855 vsctl_fatal("transaction aborted");
2865 vsctl_fatal("transaction error: %s", error);
2872 ovsdb_symbol_table_destroy(symtab);
2874 for (c = commands; c < &commands[n_commands]; c++) {
2875 struct ds *ds = &c->output;
2876 struct shash_node *node;
2882 for (j = 0; j < ds->length; j++) {
2883 int ch = ds->string[j];
2886 fputs("\\n", stdout);
2890 fputs("\\\\", stdout);
2899 fputs(ds_cstr(ds), stdout);
2901 ds_destroy(&c->output);
2903 SHASH_FOR_EACH (node, &c->options) {
2906 shash_destroy(&c->options);
2910 if (wait_for_reload && status != TXN_UNCHANGED) {
2913 OVSREC_OPEN_VSWITCH_FOR_EACH (ovs, idl) {
2914 if (ovs->cur_cfg >= next_cfg) {
2918 ovsdb_idl_wait(idl);
2923 ovsdb_idl_destroy(idl);
2928 /* Our transaction needs to be rerun, or a prerequisite was not met. Free
2929 * resources and return so that the caller can try again. */
2931 ovsdb_idl_txn_abort(txn);
2932 ovsdb_idl_txn_destroy(txn);
2934 ovsdb_symbol_table_destroy(symtab);
2935 for (c = commands; c < &commands[n_commands]; c++) {
2936 ds_destroy(&c->output);
2941 static const struct vsctl_command_syntax all_commands[] = {
2942 /* Open vSwitch commands. */
2943 {"init", 0, 0, cmd_init, NULL, "", RW},
2945 /* Bridge commands. */
2946 {"add-br", 1, 3, cmd_add_br, NULL, "--may-exist", RW},
2947 {"del-br", 1, 1, cmd_del_br, NULL, "--if-exists", RW},
2948 {"list-br", 0, 0, cmd_list_br, NULL, "", RO},
2949 {"br-exists", 1, 1, cmd_br_exists, NULL, "", RO},
2950 {"br-to-vlan", 1, 1, cmd_br_to_vlan, NULL, "", RO},
2951 {"br-to-parent", 1, 1, cmd_br_to_parent, NULL, "", RO},
2952 {"br-set-external-id", 2, 3, cmd_br_set_external_id, NULL, "", RW},
2953 {"br-get-external-id", 1, 2, cmd_br_get_external_id, NULL, "", RO},
2955 /* Port commands. */
2956 {"list-ports", 1, 1, cmd_list_ports, NULL, "", RO},
2957 {"add-port", 2, INT_MAX, cmd_add_port, NULL, "--may-exist", RW},
2958 {"add-bond", 4, INT_MAX, cmd_add_bond, NULL, "--may-exist,--fake-iface", RW},
2959 {"del-port", 1, 2, cmd_del_port, NULL, "--if-exists,--with-iface", RW},
2960 {"port-to-br", 1, 1, cmd_port_to_br, NULL, "", RO},
2962 /* Interface commands. */
2963 {"list-ifaces", 1, 1, cmd_list_ifaces, NULL, "", RO},
2964 {"iface-to-br", 1, 1, cmd_iface_to_br, NULL, "", RO},
2966 /* Controller commands. */
2967 {"get-controller", 1, 1, cmd_get_controller, NULL, "", RO},
2968 {"del-controller", 1, 1, cmd_del_controller, NULL, "", RW},
2969 {"set-controller", 1, INT_MAX, cmd_set_controller, NULL, "", RW},
2970 {"get-fail-mode", 1, 1, cmd_get_fail_mode, NULL, "", RO},
2971 {"del-fail-mode", 1, 1, cmd_del_fail_mode, NULL, "", RW},
2972 {"set-fail-mode", 2, 2, cmd_set_fail_mode, NULL, "", RW},
2975 {"get-ssl", 0, 0, cmd_get_ssl, NULL, "", RO},
2976 {"del-ssl", 0, 0, cmd_del_ssl, NULL, "", RW},
2977 {"set-ssl", 3, 3, cmd_set_ssl, NULL, "--bootstrap", RW},
2979 /* Switch commands. */
2980 {"emer-reset", 0, 0, cmd_emer_reset, NULL, "", RW},
2982 /* Parameter commands. */
2983 {"get", 2, INT_MAX, cmd_get, NULL, "--if-exists,--id=", RO},
2984 {"list", 1, INT_MAX, cmd_list, NULL, "", RO},
2985 {"set", 3, INT_MAX, cmd_set, NULL, "", RW},
2986 {"add", 4, INT_MAX, cmd_add, NULL, "", RW},
2987 {"remove", 4, INT_MAX, cmd_remove, NULL, "", RW},
2988 {"clear", 3, INT_MAX, cmd_clear, NULL, "", RW},
2989 {"create", 2, INT_MAX, cmd_create, post_create, "--id=", RW},
2990 {"destroy", 1, INT_MAX, cmd_destroy, NULL, "--if-exists", RW},
2991 {"wait-until", 2, INT_MAX, cmd_wait_until, NULL, "", RO},
2993 {NULL, 0, 0, NULL, NULL, NULL, RO},