2 * Copyright (c) 2009 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.
28 #include "command-line.h"
31 #include "dynamic-string.h"
33 #include "ovsdb-idl.h"
34 #include "poll-loop.h"
36 #include "vswitchd/vswitch-idl.h"
41 #define THIS_MODULE VLM_vsctl
43 /* --db: The database server to contact. */
44 static const char *db;
46 /* --oneline: Write each command's output as a single line? */
49 /* --dry-run: Do not commit any changes. */
52 /* --no-wait: Wait for ovs-vswitchd to reload its configuration? */
53 static bool wait_for_reload = true;
55 /* --timeout: Time to wait for a connection to 'db'. */
56 static int timeout = 5;
58 static void vsctl_fatal(const char *, ...) PRINTF_FORMAT(1, 2) NO_RETURN;
59 static char *default_db(void);
60 static void usage(void) NO_RETURN;
61 static void parse_options(int argc, char *argv[]);
63 static void check_vsctl_command(int argc, char *argv[]);
64 static void do_vsctl(int argc, char *argv[], struct ovsdb_idl *idl);
67 main(int argc, char *argv[])
69 struct ovsdb_idl *idl;
72 int start, n_commands;
76 set_program_name(argv[0]);
77 signal(SIGPIPE, SIG_IGN);
80 vlog_set_levels(VLM_ANY_MODULE, VLF_CONSOLE, VLL_WARN);
81 vlog_set_levels(VLM_reconnect, VLF_ANY_FACILITY, VLL_WARN);
82 parse_options(argc, argv);
88 /* Log our arguments. This is often valuable for debugging systems. */
90 for (i = 1; i < argc; i++) {
91 ds_put_format(&args, " %s", argv[i]);
93 VLOG_INFO("Called as%s", ds_cstr(&args));
96 /* Do basic command syntax checking. */
98 for (start = i = optind; i <= argc; i++) {
99 if (i == argc || !strcmp(argv[i], "--")) {
101 check_vsctl_command(i - start, &argv[start]);
108 vsctl_fatal("missing command name (use --help for help)");
111 /* Now execute the commands. */
112 idl = ovsdb_idl_create(db, &ovsrec_idl_class);
113 seqno = ovsdb_idl_get_seqno(idl);
116 unsigned int new_seqno;
119 new_seqno = ovsdb_idl_get_seqno(idl);
120 if (new_seqno != seqno) {
122 vsctl_fatal("too many database inconsistency failures");
124 do_vsctl(argc - optind, argv + optind, idl);
134 vsctl_fatal(const char *format, ...)
139 va_start(args, format);
140 message = xvasprintf(format, args);
143 vlog_set_levels(VLM_vsctl, VLF_CONSOLE, VLL_EMER);
144 VLOG_ERR("%s", message);
145 ovs_fatal(0, "%s", message);
149 parse_options(int argc, char *argv[])
152 OPT_DB = UCHAR_MAX + 1,
158 static struct option long_options[] = {
159 {"db", required_argument, 0, OPT_DB},
160 {"no-syslog", no_argument, 0, OPT_NO_SYSLOG},
161 {"no-wait", no_argument, 0, OPT_NO_WAIT},
162 {"dry-run", no_argument, 0, OPT_DRY_RUN},
163 {"oneline", no_argument, 0, OPT_ONELINE},
164 {"timeout", required_argument, 0, 't'},
165 {"verbose", optional_argument, 0, 'v'},
166 {"help", no_argument, 0, 'h'},
167 {"version", no_argument, 0, 'V'},
173 unsigned long int timeout;
176 c = getopt_long(argc, argv, "+v::hVt:", long_options, NULL);
191 vlog_set_levels(VLM_vsctl, VLF_SYSLOG, VLL_WARN);
195 wait_for_reload = false;
206 OVS_PRINT_VERSION(0, 0);
210 timeout = strtoul(optarg, NULL, 10);
212 ovs_fatal(0, "value %s on -t or --timeout is invalid",
218 vlog_set_verbosity(optarg);
237 printf("%s: ovs-vswitchd management utility\n"
238 "usage: %s [OPTIONS] COMMAND [ARG...]\n",
239 program_name, program_name);
240 printf("\nBridge commands:\n"
242 "create a new bridge named BRIDGE\n"
243 " add-br BRIDGE PARENT VLAN "
244 "create new fake bridge BRIDGE in PARENT on VLAN\n"
246 "delete BRIDGE and all of its ports\n"
248 "print the names of all the bridges\n"
250 "test whether BRIDGE exists\n"
251 " br-to-vlan BRIDGE "
252 "print the VLAN which BRIDGE is on\n"
253 " br-to-parent BRIDGE "
254 "print the parent of BRIDGE\n"
255 " br-set-external-id BRIDGE KEY VALUE"
256 " set KEY on BRIDGE to VALUE\n"
257 " br-set-external-id BRIDGE KEY"
258 " unset KEY on BRIDGE\n"
259 " br-get-external-id BRIDGE KEY"
260 " print value of KEY on BRIDGE\n"
261 " br-get-external-id BRIDGE"
262 " list key-value pairs on BRIDGE\n"
264 printf("\nPort commands:\n"
265 " list-ports BRIDGE "
266 "print the names of all the ports on BRIDGE\n"
267 " add-port BRIDGE PORT "
268 "add network device PORT to BRIDGE\n"
269 " add-bond BRIDGE PORT IFACE... "
270 "add new bonded port PORT in BRIDGE from IFACES\n"
271 " del-port [BRIDGE] PORT "
272 "delete PORT (which may be bonded) from BRIDGE\n"
274 "print name of bridge that contains PORT\n"
275 " port-set-external-id PORT KEY VALUE"
276 " set KEY on PORT to VALUE\n"
277 " port-set-external-id PORT KEY"
278 " unset KEY on PORT\n"
279 " port-get-external-id PORT KEY"
280 " print value of KEY on PORT\n"
281 " port-get-external-id PORT"
282 " list key-value pairs on PORT\n"
283 "A bond is considered to be a single port.\n"
285 printf("\nInterface commands (a bond consists of multiple interfaces):\n"
286 " list-ifaces BRIDGE "
287 "print the names of all the interfaces on BRIDGE\n"
288 " iface-to-br IFACE "
289 "print name of bridge that contains IFACE\n"
290 " iface-set-external-id IFACE KEY VALUE"
291 " set KEY on IFACE to VALUE\n"
292 " iface-set-external-id IFACE KEY"
293 " unset KEY on IFACE\n"
294 " iface-get-external-id IFACE KEY"
295 " print value of KEY on IFACE\n"
296 " iface-get-external-id IFACE"
297 " list key-value pairs on IFACE\n"
299 printf("\nController commands:\n"
300 " get-controller [BRIDGE] "
301 "print the controller for BRIDGE\n"
302 " del-controller [BRIDGE] "
303 "delete the controller for BRIDGE\n"
304 " set-controller [BRIDGE] TARGET "
305 "set the controller for BRIDGE to TARGET\n"
306 " get-fail-mode [BRIDGE] "
307 "print the fail-mode for BRIDGE\n"
308 " del-fail-mode [BRIDGE] "
309 "delete the fail-mode for BRIDGE\n"
310 " set-fail-mode [BRIDGE] MODE "
311 "set the fail-mode for BRIDGE to MODE\n"
313 printf("\nSSL commands:\n"
315 "print the SSL configuration\n"
317 "delete the SSL configuration\n"
318 " set-ssl PRIV-KEY CERT CA-CERT "
319 "set the SSL configuration\n"
321 printf("\nOptions:\n"
323 "connect to DATABASE\n"
327 "print exactly one line of output per command\n",
330 printf("\nOther options:\n"
332 "display this help message\n"
334 "display version information\n");
343 def = xasprintf("unix:%s/ovsdb-server", ovs_rundir);
348 struct vsctl_context {
351 const struct ovsrec_open_vswitch *ovs;
353 struct shash options;
356 struct vsctl_bridge {
357 struct ovsrec_bridge *br_cfg;
359 struct ovsrec_controller *ctrl;
360 struct vsctl_bridge *parent;
365 struct ovsrec_port *port_cfg;
366 struct vsctl_bridge *bridge;
370 struct ovsrec_interface *iface_cfg;
371 struct vsctl_port *port;
375 struct shash bridges;
378 struct ovsrec_controller *ctrl;
381 static struct ovsdb_idl_txn *
382 txn_from_openvswitch(const struct ovsrec_open_vswitch *ovs)
384 return ovsdb_idl_txn_get(&ovs->header_);
387 static struct vsctl_bridge *
388 add_bridge(struct vsctl_info *b,
389 struct ovsrec_bridge *br_cfg, const char *name,
390 struct vsctl_bridge *parent, int vlan)
392 struct vsctl_bridge *br = xmalloc(sizeof *br);
394 br->name = xstrdup(name);
397 br->ctrl = parent ? parent->br_cfg->controller : br_cfg->controller;
398 shash_add(&b->bridges, br->name, br);
403 port_is_fake_bridge(const struct ovsrec_port *port_cfg)
405 return (port_cfg->fake_bridge
407 && *port_cfg->tag >= 1 && *port_cfg->tag <= 4095);
410 static struct vsctl_bridge *
411 find_vlan_bridge(struct vsctl_info *info,
412 struct vsctl_bridge *parent, int vlan)
414 struct shash_node *node;
416 SHASH_FOR_EACH (node, &info->bridges) {
417 struct vsctl_bridge *br = node->data;
418 if (br->parent == parent && br->vlan == vlan) {
427 free_info(struct vsctl_info *info)
429 struct shash_node *node;
431 SHASH_FOR_EACH (node, &info->bridges) {
432 struct vsctl_bridge *bridge = node->data;
436 shash_destroy(&info->bridges);
438 SHASH_FOR_EACH (node, &info->ports) {
439 struct vsctl_port *port = node->data;
442 shash_destroy(&info->ports);
444 SHASH_FOR_EACH (node, &info->ifaces) {
445 struct vsctl_iface *iface = node->data;
448 shash_destroy(&info->ifaces);
452 get_info(const struct ovsrec_open_vswitch *ovs, struct vsctl_info *info)
454 struct shash bridges, ports;
457 shash_init(&info->bridges);
458 shash_init(&info->ports);
459 shash_init(&info->ifaces);
461 info->ctrl = ovs->controller;
463 shash_init(&bridges);
465 for (i = 0; i < ovs->n_bridges; i++) {
466 struct ovsrec_bridge *br_cfg = ovs->bridges[i];
467 struct vsctl_bridge *br;
470 if (!shash_add_once(&bridges, br_cfg->name, NULL)) {
471 VLOG_WARN("%s: database contains duplicate bridge name",
475 br = add_bridge(info, br_cfg, br_cfg->name, NULL, 0);
480 for (j = 0; j < br_cfg->n_ports; j++) {
481 struct ovsrec_port *port_cfg = br_cfg->ports[j];
483 if (!shash_add_once(&ports, port_cfg->name, NULL)) {
484 VLOG_WARN("%s: database contains duplicate port name",
489 if (port_is_fake_bridge(port_cfg)
490 && shash_add_once(&bridges, port_cfg->name, NULL)) {
491 add_bridge(info, NULL, port_cfg->name, br, *port_cfg->tag);
495 shash_destroy(&bridges);
496 shash_destroy(&ports);
498 shash_init(&bridges);
500 for (i = 0; i < ovs->n_bridges; i++) {
501 struct ovsrec_bridge *br_cfg = ovs->bridges[i];
502 struct vsctl_bridge *br;
505 if (!shash_add_once(&bridges, br_cfg->name, NULL)) {
508 br = shash_find_data(&info->bridges, br_cfg->name);
509 for (j = 0; j < br_cfg->n_ports; j++) {
510 struct ovsrec_port *port_cfg = br_cfg->ports[j];
511 struct vsctl_port *port;
514 if (!shash_add_once(&ports, port_cfg->name, NULL)) {
518 if (port_is_fake_bridge(port_cfg)
519 && !shash_add_once(&bridges, port_cfg->name, NULL)) {
523 port = xmalloc(sizeof *port);
524 port->port_cfg = port_cfg;
526 && *port_cfg->tag >= 1 && *port_cfg->tag <= 4095) {
527 port->bridge = find_vlan_bridge(info, br, *port_cfg->tag);
534 shash_add(&info->ports, port_cfg->name, port);
536 for (k = 0; k < port_cfg->n_interfaces; k++) {
537 struct ovsrec_interface *iface_cfg = port_cfg->interfaces[k];
538 struct vsctl_iface *iface;
540 if (shash_find(&info->ifaces, iface_cfg->name)) {
541 VLOG_WARN("%s: database contains duplicate interface name",
546 iface = xmalloc(sizeof *iface);
547 iface->iface_cfg = iface_cfg;
549 shash_add(&info->ifaces, iface_cfg->name, iface);
553 shash_destroy(&bridges);
554 shash_destroy(&ports);
558 check_conflicts(struct vsctl_info *info, const char *name,
561 struct vsctl_iface *iface;
562 struct vsctl_port *port;
564 if (shash_find(&info->bridges, name)) {
565 vsctl_fatal("%s because a bridge named %s already exists",
569 port = shash_find_data(&info->ports, name);
571 vsctl_fatal("%s because a port named %s already exists on "
572 "bridge %s", msg, name, port->bridge->name);
575 iface = shash_find_data(&info->ifaces, name);
577 vsctl_fatal("%s because an interface named %s already exists "
578 "on bridge %s", msg, name, iface->port->bridge->name);
584 static struct vsctl_bridge *
585 find_bridge(struct vsctl_info *info, const char *name, bool must_exist)
587 struct vsctl_bridge *br = shash_find_data(&info->bridges, name);
588 if (must_exist && !br) {
589 vsctl_fatal("no bridge named %s", name);
594 static struct vsctl_bridge *
595 find_real_bridge(struct vsctl_info *info, const char *name, bool must_exist)
597 struct vsctl_bridge *br = find_bridge(info, name, must_exist);
598 if (br && br->parent) {
599 vsctl_fatal("%s is a fake bridge", name);
604 static struct vsctl_port *
605 find_port(struct vsctl_info *info, const char *name, bool must_exist)
607 struct vsctl_port *port = shash_find_data(&info->ports, name);
608 if (port && !strcmp(name, port->bridge->name)) {
611 if (must_exist && !port) {
612 vsctl_fatal("no port named %s", name);
617 static struct vsctl_iface *
618 find_iface(struct vsctl_info *info, const char *name, bool must_exist)
620 struct vsctl_iface *iface = shash_find_data(&info->ifaces, name);
621 if (iface && !strcmp(name, iface->port->bridge->name)) {
624 if (must_exist && !iface) {
625 vsctl_fatal("no interface named %s", name);
631 bridge_insert_port(struct ovsrec_bridge *br, struct ovsrec_port *port)
633 struct ovsrec_port **ports;
636 ports = xmalloc(sizeof *br->ports * (br->n_ports + 1));
637 for (i = 0; i < br->n_ports; i++) {
638 ports[i] = br->ports[i];
640 ports[br->n_ports] = port;
641 ovsrec_bridge_set_ports(br, ports, br->n_ports + 1);
646 bridge_delete_port(struct ovsrec_bridge *br, struct ovsrec_port *port)
648 struct ovsrec_port **ports;
651 ports = xmalloc(sizeof *br->ports * br->n_ports);
652 for (i = n = 0; i < br->n_ports; i++) {
653 if (br->ports[i] != port) {
654 ports[n++] = br->ports[i];
657 ovsrec_bridge_set_ports(br, ports, n);
662 ovs_insert_bridge(const struct ovsrec_open_vswitch *ovs,
663 struct ovsrec_bridge *bridge)
665 struct ovsrec_bridge **bridges;
668 bridges = xmalloc(sizeof *ovs->bridges * (ovs->n_bridges + 1));
669 for (i = 0; i < ovs->n_bridges; i++) {
670 bridges[i] = ovs->bridges[i];
672 bridges[ovs->n_bridges] = bridge;
673 ovsrec_open_vswitch_set_bridges(ovs, bridges, ovs->n_bridges + 1);
678 ovs_delete_bridge(const struct ovsrec_open_vswitch *ovs,
679 struct ovsrec_bridge *bridge)
681 struct ovsrec_bridge **bridges;
684 bridges = xmalloc(sizeof *ovs->bridges * ovs->n_bridges);
685 for (i = n = 0; i < ovs->n_bridges; i++) {
686 if (ovs->bridges[i] != bridge) {
687 bridges[n++] = ovs->bridges[i];
690 ovsrec_open_vswitch_set_bridges(ovs, bridges, n);
695 cmd_init(struct vsctl_context *ctx UNUSED)
700 cmd_add_br(struct vsctl_context *ctx)
702 const char *br_name = ctx->argv[1];
703 struct vsctl_info info;
705 get_info(ctx->ovs, &info);
706 check_conflicts(&info, br_name,
707 xasprintf("cannot create a bridge named %s", br_name));
709 if (ctx->argc == 2) {
710 struct ovsrec_bridge *br;
711 struct ovsrec_port *port;
712 struct ovsrec_interface *iface;
714 iface = ovsrec_interface_insert(txn_from_openvswitch(ctx->ovs));
715 ovsrec_interface_set_name(iface, br_name);
717 port = ovsrec_port_insert(txn_from_openvswitch(ctx->ovs));
718 ovsrec_port_set_name(port, br_name);
719 ovsrec_port_set_interfaces(port, &iface, 1);
721 br = ovsrec_bridge_insert(txn_from_openvswitch(ctx->ovs));
722 ovsrec_bridge_set_name(br, br_name);
723 ovsrec_bridge_set_ports(br, &port, 1);
725 ovs_insert_bridge(ctx->ovs, br);
726 } else if (ctx->argc == 3) {
727 vsctl_fatal("'%s' command takes exactly 1 or 3 arguments",
729 } else if (ctx->argc == 4) {
730 const char *parent_name = ctx->argv[2];
731 int vlan = atoi(ctx->argv[3]);
732 struct ovsrec_bridge *br;
733 struct vsctl_bridge *parent;
734 struct ovsrec_port *port;
735 struct ovsrec_interface *iface;
738 if (vlan < 1 || vlan > 4095) {
739 vsctl_fatal("%s: vlan must be between 1 and 4095", ctx->argv[0]);
742 parent = find_bridge(&info, parent_name, false);
743 if (parent && parent->vlan) {
744 vsctl_fatal("cannot create bridge with fake bridge as parent");
747 vsctl_fatal("parent bridge %s does not exist", parent_name);
751 iface = ovsrec_interface_insert(txn_from_openvswitch(ctx->ovs));
752 ovsrec_interface_set_name(iface, br_name);
753 ovsrec_interface_set_type(iface, "internal");
755 port = ovsrec_port_insert(txn_from_openvswitch(ctx->ovs));
756 ovsrec_port_set_name(port, br_name);
757 ovsrec_port_set_interfaces(port, &iface, 1);
758 ovsrec_port_set_fake_bridge(port, true);
759 ovsrec_port_set_tag(port, &tag, 1);
761 bridge_insert_port(br, port);
770 del_port(struct vsctl_info *info, struct vsctl_port *port)
772 struct shash_node *node;
774 SHASH_FOR_EACH (node, &info->ifaces) {
775 struct vsctl_iface *iface = node->data;
776 if (iface->port == port) {
777 ovsrec_interface_delete(iface->iface_cfg);
780 ovsrec_port_delete(port->port_cfg);
782 bridge_delete_port((port->bridge->parent
783 ? port->bridge->parent->br_cfg
784 : port->bridge->br_cfg), port->port_cfg);
788 cmd_del_br(struct vsctl_context *ctx)
790 bool must_exist = !shash_find(&ctx->options, "--if-exists");
791 struct vsctl_bridge *bridge;
792 struct vsctl_info info;
794 get_info(ctx->ovs, &info);
795 bridge = find_bridge(&info, ctx->argv[1], must_exist);
797 struct shash_node *node;
799 SHASH_FOR_EACH (node, &info.ports) {
800 struct vsctl_port *port = node->data;
801 if (port->bridge == bridge
802 || !strcmp(port->port_cfg->name, bridge->name)) {
803 del_port(&info, port);
806 if (bridge->br_cfg) {
807 ovsrec_bridge_delete(bridge->br_cfg);
808 ovs_delete_bridge(ctx->ovs, bridge->br_cfg);
815 output_sorted(struct svec *svec, struct ds *output)
821 SVEC_FOR_EACH (i, name, svec) {
822 ds_put_format(output, "%s\n", name);
827 cmd_list_br(struct vsctl_context *ctx)
829 struct shash_node *node;
830 struct vsctl_info info;
833 get_info(ctx->ovs, &info);
836 SHASH_FOR_EACH (node, &info.bridges) {
837 struct vsctl_bridge *br = node->data;
838 svec_add(&bridges, br->name);
840 output_sorted(&bridges, &ctx->output);
841 svec_destroy(&bridges);
847 cmd_br_exists(struct vsctl_context *ctx)
849 struct vsctl_info info;
851 get_info(ctx->ovs, &info);
852 if (!find_bridge(&info, ctx->argv[1], false)) {
858 /* Returns true if 'b_prefix' (of length 'b_prefix_len') concatenated with 'b'
859 * equals 'a', false otherwise. */
861 key_matches(const char *a,
862 const char *b_prefix, size_t b_prefix_len, const char *b)
864 return !strncmp(a, b_prefix, b_prefix_len) && !strcmp(a + b_prefix_len, b);
868 set_external_id(char **old_keys, char **old_values, size_t old_n,
869 char *key, char *value,
870 char ***new_keysp, char ***new_valuesp, size_t *new_np)
877 new_keys = xmalloc(sizeof *new_keys * (old_n + 1));
878 new_values = xmalloc(sizeof *new_values * (old_n + 1));
880 for (i = 0; i < old_n; i++) {
881 if (strcmp(key, old_keys[i])) {
882 new_keys[new_n] = old_keys[i];
883 new_values[new_n] = old_values[i];
888 new_keys[new_n] = key;
889 new_values[new_n] = value;
892 *new_keysp = new_keys;
893 *new_valuesp = new_values;
898 cmd_br_set_external_id(struct vsctl_context *ctx)
900 struct vsctl_info info;
901 struct vsctl_bridge *bridge;
902 char **keys, **values;
905 get_info(ctx->ovs, &info);
906 bridge = find_bridge(&info, ctx->argv[1], true);
907 if (bridge->br_cfg) {
908 set_external_id(bridge->br_cfg->key_external_ids,
909 bridge->br_cfg->value_external_ids,
910 bridge->br_cfg->n_external_ids,
911 ctx->argv[2], ctx->argc >= 4 ? ctx->argv[3] : NULL,
913 ovsrec_bridge_set_external_ids(bridge->br_cfg, keys, values, n);
915 char *key = xasprintf("fake-bridge-%s", ctx->argv[2]);
916 struct vsctl_port *port = shash_find_data(&info.ports, ctx->argv[1]);
917 set_external_id(port->port_cfg->key_external_ids,
918 port->port_cfg->value_external_ids,
919 port->port_cfg->n_external_ids,
920 key, ctx->argc >= 4 ? ctx->argv[3] : NULL,
922 ovsrec_port_set_external_ids(port->port_cfg, keys, values, n);
932 get_external_id(char **keys, char **values, size_t n,
933 const char *prefix, const char *key,
936 size_t prefix_len = strlen(prefix);
941 for (i = 0; i < n; i++) {
942 if (!key && !strncmp(keys[i], prefix, prefix_len)) {
943 svec_add_nocopy(&svec, xasprintf("%s=%s",
944 keys[i] + prefix_len, values[i]));
945 } else if (key_matches(keys[i], prefix, prefix_len, key)) {
946 svec_add(&svec, values[i]);
950 output_sorted(&svec, output);
955 cmd_br_get_external_id(struct vsctl_context *ctx)
957 struct vsctl_info info;
958 struct vsctl_bridge *bridge;
960 get_info(ctx->ovs, &info);
961 bridge = find_bridge(&info, ctx->argv[1], true);
962 if (bridge->br_cfg) {
963 get_external_id(bridge->br_cfg->key_external_ids,
964 bridge->br_cfg->value_external_ids,
965 bridge->br_cfg->n_external_ids,
966 "", ctx->argc >= 3 ? ctx->argv[2] : NULL,
969 struct vsctl_port *port = shash_find_data(&info.ports, ctx->argv[1]);
970 get_external_id(port->port_cfg->key_external_ids,
971 port->port_cfg->value_external_ids,
972 port->port_cfg->n_external_ids,
973 "fake-bridge-", ctx->argc >= 3 ? ctx->argv[2] : NULL, &ctx->output);
980 cmd_list_ports(struct vsctl_context *ctx)
982 struct vsctl_bridge *br;
983 struct shash_node *node;
984 struct vsctl_info info;
987 get_info(ctx->ovs, &info);
988 br = find_bridge(&info, ctx->argv[1], true);
991 SHASH_FOR_EACH (node, &info.ports) {
992 struct vsctl_port *port = node->data;
994 if (strcmp(port->port_cfg->name, br->name) && br == port->bridge) {
995 svec_add(&ports, port->port_cfg->name);
998 output_sorted(&ports, &ctx->output);
999 svec_destroy(&ports);
1005 add_port(const struct ovsrec_open_vswitch *ovs,
1006 const char *br_name, const char *port_name,
1007 char *iface_names[], int n_ifaces)
1009 struct vsctl_info info;
1010 struct vsctl_bridge *bridge;
1011 struct ovsrec_interface **ifaces;
1012 struct ovsrec_port *port;
1015 get_info(ovs, &info);
1016 check_conflicts(&info, port_name,
1017 xasprintf("cannot create a port named %s", port_name));
1018 /* XXX need to check for conflicts on interfaces too */
1019 bridge = find_bridge(&info, br_name, true);
1021 ifaces = xmalloc(n_ifaces * sizeof *ifaces);
1022 for (i = 0; i < n_ifaces; i++) {
1023 ifaces[i] = ovsrec_interface_insert(txn_from_openvswitch(ovs));
1024 ovsrec_interface_set_name(ifaces[i], iface_names[i]);
1027 port = ovsrec_port_insert(txn_from_openvswitch(ovs));
1028 ovsrec_port_set_name(port, port_name);
1029 ovsrec_port_set_interfaces(port, ifaces, n_ifaces);
1033 int64_t tag = bridge->vlan;
1034 ovsrec_port_set_tag(port, &tag, 1);
1037 bridge_insert_port((bridge->parent ? bridge->parent->br_cfg
1038 : bridge->br_cfg), port);
1044 cmd_add_port(struct vsctl_context *ctx)
1046 add_port(ctx->ovs, ctx->argv[1], ctx->argv[2], &ctx->argv[2], 1);
1050 cmd_add_bond(struct vsctl_context *ctx)
1052 add_port(ctx->ovs, ctx->argv[1], ctx->argv[2], &ctx->argv[3], ctx->argc - 3);
1056 cmd_del_port(struct vsctl_context *ctx)
1058 bool must_exist = !shash_find(&ctx->options, "--if-exists");
1059 struct vsctl_info info;
1061 get_info(ctx->ovs, &info);
1062 if (ctx->argc == 2) {
1063 struct vsctl_port *port = find_port(&info, ctx->argv[1], must_exist);
1065 del_port(&info, port);
1067 } else if (ctx->argc == 3) {
1068 struct vsctl_bridge *bridge = find_bridge(&info, ctx->argv[1], true);
1069 struct vsctl_port *port = find_port(&info, ctx->argv[2], must_exist);
1072 if (port->bridge == bridge) {
1073 del_port(&info, port);
1074 } else if (port->bridge->parent == bridge) {
1075 vsctl_fatal("bridge %s does not have a port %s (although its "
1076 "parent bridge %s does)",
1077 ctx->argv[1], ctx->argv[2], bridge->parent->name);
1079 vsctl_fatal("bridge %s does not have a port %s",
1080 ctx->argv[1], ctx->argv[2]);
1088 cmd_port_to_br(struct vsctl_context *ctx)
1090 struct vsctl_port *port;
1091 struct vsctl_info info;
1093 get_info(ctx->ovs, &info);
1094 port = find_port(&info, ctx->argv[1], true);
1095 ds_put_format(&ctx->output, "%s\n", port->bridge->name);
1100 cmd_port_set_external_id(struct vsctl_context *ctx)
1102 struct vsctl_info info;
1103 struct vsctl_port *port;
1104 char **keys, **values;
1107 get_info(ctx->ovs, &info);
1108 port = find_port(&info, ctx->argv[1], true);
1109 set_external_id(port->port_cfg->key_external_ids,
1110 port->port_cfg->value_external_ids,
1111 port->port_cfg->n_external_ids,
1112 ctx->argv[2], ctx->argc >= 4 ? ctx->argv[3] : NULL,
1113 &keys, &values, &n);
1114 ovsrec_port_set_external_ids(port->port_cfg, keys, values, n);
1122 cmd_port_get_external_id(struct vsctl_context *ctx)
1124 struct vsctl_info info;
1125 struct vsctl_port *port;
1127 get_info(ctx->ovs, &info);
1128 port = find_port(&info, ctx->argv[1], true);
1129 get_external_id(port->port_cfg->key_external_ids,
1130 port->port_cfg->value_external_ids,
1131 port->port_cfg->n_external_ids,
1132 "", ctx->argc >= 3 ? ctx->argv[2] : NULL, &ctx->output);
1137 cmd_br_to_vlan(struct vsctl_context *ctx)
1139 struct vsctl_bridge *bridge;
1140 struct vsctl_info info;
1142 get_info(ctx->ovs, &info);
1143 bridge = find_bridge(&info, ctx->argv[1], true);
1144 ds_put_format(&ctx->output, "%d\n", bridge->vlan);
1149 cmd_br_to_parent(struct vsctl_context *ctx)
1151 struct vsctl_bridge *bridge;
1152 struct vsctl_info info;
1154 get_info(ctx->ovs, &info);
1155 bridge = find_bridge(&info, ctx->argv[1], true);
1156 if (bridge->parent) {
1157 bridge = bridge->parent;
1159 ds_put_format(&ctx->output, "%s\n", bridge->name);
1164 cmd_list_ifaces(struct vsctl_context *ctx)
1166 struct vsctl_bridge *br;
1167 struct shash_node *node;
1168 struct vsctl_info info;
1171 get_info(ctx->ovs, &info);
1172 br = find_bridge(&info, ctx->argv[1], true);
1175 SHASH_FOR_EACH (node, &info.ifaces) {
1176 struct vsctl_iface *iface = node->data;
1178 if (strcmp(iface->iface_cfg->name, br->name)
1179 && br == iface->port->bridge) {
1180 svec_add(&ifaces, iface->iface_cfg->name);
1183 output_sorted(&ifaces, &ctx->output);
1184 svec_destroy(&ifaces);
1190 cmd_iface_to_br(struct vsctl_context *ctx)
1192 struct vsctl_iface *iface;
1193 struct vsctl_info info;
1195 get_info(ctx->ovs, &info);
1196 iface = find_iface(&info, ctx->argv[1], true);
1197 ds_put_format(&ctx->output, "%s\n", iface->port->bridge->name);
1202 cmd_iface_set_external_id(struct vsctl_context *ctx)
1204 struct vsctl_info info;
1205 struct vsctl_iface *iface;
1206 char **keys, **values;
1209 get_info(ctx->ovs, &info);
1210 iface = find_iface(&info, ctx->argv[1], true);
1211 set_external_id(iface->iface_cfg->key_external_ids,
1212 iface->iface_cfg->value_external_ids,
1213 iface->iface_cfg->n_external_ids,
1214 ctx->argv[2], ctx->argc >= 4 ? ctx->argv[3] : NULL,
1215 &keys, &values, &n);
1216 ovsrec_interface_set_external_ids(iface->iface_cfg, keys, values, n);
1224 cmd_iface_get_external_id(struct vsctl_context *ctx)
1226 struct vsctl_info info;
1227 struct vsctl_iface *iface;
1229 get_info(ctx->ovs, &info);
1230 iface = find_iface(&info, ctx->argv[1], true);
1231 get_external_id(iface->iface_cfg->key_external_ids,
1232 iface->iface_cfg->value_external_ids,
1233 iface->iface_cfg->n_external_ids,
1234 "", ctx->argc >= 3 ? ctx->argv[2] : NULL, &ctx->output);
1239 cmd_get_controller(struct vsctl_context *ctx)
1241 struct vsctl_info info;
1243 get_info(ctx->ovs, &info);
1245 if (ctx->argc == 1) {
1246 /* Return the controller from the "Open_vSwitch" table */
1248 ds_put_format(&ctx->output, "%s\n", info.ctrl->target);
1251 /* Return the controller for a particular bridge. */
1252 struct vsctl_bridge *br = find_bridge(&info, ctx->argv[1], true);
1254 /* If no controller is explicitly defined for the requested
1255 * bridge, fallback to the "Open_vSwitch" table's controller. */
1257 ds_put_format(&ctx->output, "%s\n", br->ctrl->target);
1258 } else if (info.ctrl) {
1259 ds_put_format(&ctx->output, "%s\n", info.ctrl->target);
1267 cmd_del_controller(struct vsctl_context *ctx)
1269 struct vsctl_info info;
1271 get_info(ctx->ovs, &info);
1273 if (ctx->argc == 1) {
1275 ovsrec_controller_delete(info.ctrl);
1276 ovsrec_open_vswitch_set_controller(ctx->ovs, NULL);
1279 struct vsctl_bridge *br = find_real_bridge(&info, ctx->argv[1], true);
1282 ovsrec_controller_delete(br->ctrl);
1283 ovsrec_bridge_set_controller(br->br_cfg, NULL);
1291 cmd_set_controller(struct vsctl_context *ctx)
1293 struct vsctl_info info;
1294 struct ovsrec_controller *ctrl;
1296 get_info(ctx->ovs, &info);
1298 if (ctx->argc == 2) {
1299 /* Set the controller in the "Open_vSwitch" table. */
1301 ovsrec_controller_delete(info.ctrl);
1303 ctrl = ovsrec_controller_insert(txn_from_openvswitch(ctx->ovs));
1304 ovsrec_controller_set_target(ctrl, ctx->argv[1]);
1305 ovsrec_open_vswitch_set_controller(ctx->ovs, ctrl);
1307 /* Set the controller for a particular bridge. */
1308 struct vsctl_bridge *br = find_real_bridge(&info, ctx->argv[1], true);
1311 ovsrec_controller_delete(br->ctrl);
1313 ctrl = ovsrec_controller_insert(txn_from_openvswitch(ctx->ovs));
1314 ovsrec_controller_set_target(ctrl, ctx->argv[2]);
1315 ovsrec_bridge_set_controller(br->br_cfg, ctrl);
1322 cmd_get_fail_mode(struct vsctl_context *ctx)
1324 struct vsctl_info info;
1325 const char *fail_mode = NULL;
1327 get_info(ctx->ovs, &info);
1329 if (ctx->argc == 1) {
1330 /* Return the fail-mode from the "Open_vSwitch" table */
1331 if (info.ctrl && info.ctrl->fail_mode) {
1332 fail_mode = info.ctrl->fail_mode;
1335 /* Return the fail-mode for a particular bridge. */
1336 struct vsctl_bridge *br = find_bridge(&info, ctx->argv[1], true);
1338 /* If no controller or fail-mode is explicitly defined for the
1339 * requested bridge, fallback to the "Open_vSwitch" table's
1341 if (br->ctrl && br->ctrl->fail_mode) {
1342 fail_mode = br->ctrl->fail_mode;
1343 } else if (info.ctrl && info.ctrl->fail_mode) {
1344 fail_mode = info.ctrl->fail_mode;
1348 if (fail_mode && strlen(fail_mode)) {
1349 ds_put_format(&ctx->output, "%s\n", info.ctrl->fail_mode);
1356 cmd_del_fail_mode(struct vsctl_context *ctx)
1358 struct vsctl_info info;
1360 get_info(ctx->ovs, &info);
1362 if (ctx->argc == 1) {
1363 if (info.ctrl && info.ctrl->fail_mode) {
1364 ovsrec_controller_set_fail_mode(info.ctrl, NULL);
1367 struct vsctl_bridge *br = find_real_bridge(&info, ctx->argv[1], true);
1369 if (br->ctrl && br->ctrl->fail_mode) {
1370 ovsrec_controller_set_fail_mode(br->ctrl, NULL);
1378 cmd_set_fail_mode(struct vsctl_context *ctx)
1380 struct vsctl_info info;
1381 const char *fail_mode;
1383 get_info(ctx->ovs, &info);
1385 fail_mode = (ctx->argc == 2) ? ctx->argv[1] : ctx->argv[2];
1387 if (strcmp(fail_mode, "standalone") && strcmp(fail_mode, "secure")) {
1388 vsctl_fatal("fail-mode must be \"standalone\" or \"secure\"");
1391 if (ctx->argc == 2) {
1392 /* Set the fail-mode in the "Open_vSwitch" table. */
1394 vsctl_fatal("no controller declared");
1396 ovsrec_controller_set_fail_mode(info.ctrl, fail_mode);
1398 struct vsctl_bridge *br = find_real_bridge(&info, ctx->argv[1], true);
1401 vsctl_fatal("no controller declared for %s", br->name);
1403 ovsrec_controller_set_fail_mode(br->ctrl, fail_mode);
1410 cmd_get_ssl(struct vsctl_context *ctx)
1412 struct ovsrec_ssl *ssl = ctx->ovs->ssl;
1415 ds_put_format(&ctx->output, "Private key: %s\n", ssl->private_key);
1416 ds_put_format(&ctx->output, "Certificate: %s\n", ssl->certificate);
1417 ds_put_format(&ctx->output, "CA Certificate: %s\n", ssl->ca_cert);
1418 ds_put_format(&ctx->output, "Bootstrap: %s\n",
1419 ssl->bootstrap_ca_cert ? "true" : "false");
1424 cmd_del_ssl(struct vsctl_context *ctx)
1426 struct ovsrec_ssl *ssl = ctx->ovs->ssl;
1429 ovsrec_ssl_delete(ssl);
1430 ovsrec_open_vswitch_set_ssl(ctx->ovs, NULL);
1435 cmd_set_ssl(struct vsctl_context *ctx)
1437 bool bootstrap = shash_find(&ctx->options, "--bootstrap");
1438 struct ovsrec_ssl *ssl = ctx->ovs->ssl;
1441 ovsrec_ssl_delete(ssl);
1443 ssl = ovsrec_ssl_insert(txn_from_openvswitch(ctx->ovs));
1445 ovsrec_ssl_set_private_key(ssl, ctx->argv[1]);
1446 ovsrec_ssl_set_certificate(ssl, ctx->argv[2]);
1447 ovsrec_ssl_set_ca_cert(ssl, ctx->argv[3]);
1449 ovsrec_ssl_set_bootstrap_ca_cert(ssl, bootstrap);
1451 ovsrec_open_vswitch_set_ssl(ctx->ovs, ssl);
1454 typedef void vsctl_handler_func(struct vsctl_context *);
1456 struct vsctl_command {
1460 vsctl_handler_func *handler;
1461 const char *options;
1464 static void run_vsctl_command(int argc, char *argv[],
1465 const struct ovsrec_open_vswitch *ovs,
1468 static struct json *
1469 where_uuid_equals(const struct uuid *uuid)
1472 json_array_create_1(
1473 json_array_create_3(
1474 json_string_create("_uuid"),
1475 json_string_create("=="),
1476 json_array_create_2(
1477 json_string_create("uuid"),
1478 json_string_create_nocopy(
1479 xasprintf(UUID_FMT, UUID_ARGS(uuid))))));
1483 do_vsctl(int argc, char *argv[], struct ovsdb_idl *idl)
1485 struct ovsdb_idl_txn *txn;
1486 const struct ovsrec_open_vswitch *ovs;
1487 enum ovsdb_idl_txn_status status;
1488 struct ds comment, *output;
1493 txn = ovsdb_idl_txn_create(idl);
1495 ovsdb_idl_txn_set_dry_run(txn);
1499 ds_put_cstr(&comment, "ovs-vsctl:");
1500 for (i = 0; i < argc; i++) {
1501 ds_put_format(&comment, " %s", argv[i]);
1503 ovsdb_idl_txn_add_comment(txn, ds_cstr(&comment));
1504 ds_destroy(&comment);
1506 ovs = ovsrec_open_vswitch_first(idl);
1508 /* XXX add verification that table is empty */
1509 ovs = ovsrec_open_vswitch_insert(txn);
1512 if (wait_for_reload) {
1513 struct json *where = where_uuid_equals(&ovs->header_.uuid);
1514 ovsdb_idl_txn_increment(txn, "Open_vSwitch", "next_cfg",
1516 json_destroy(where);
1519 output = xmalloc(argc * sizeof *output);
1521 for (start = i = 0; i <= argc; i++) {
1522 if (i == argc || !strcmp(argv[i], "--")) {
1524 ds_init(&output[n_output]);
1525 run_vsctl_command(i - start, &argv[start], ovs,
1526 &output[n_output++]);
1532 while ((status = ovsdb_idl_txn_commit(txn)) == TXN_INCOMPLETE) {
1534 ovsdb_idl_wait(idl);
1535 ovsdb_idl_txn_wait(txn);
1538 if (wait_for_reload && status == TXN_SUCCESS) {
1539 next_cfg = ovsdb_idl_txn_get_increment_new_value(txn);
1541 ovsdb_idl_txn_destroy(txn);
1544 case TXN_INCOMPLETE:
1548 /* Should not happen--we never call ovsdb_idl_txn_abort(). */
1549 vsctl_fatal("transaction aborted");
1556 for (i = 0; i < n_output; i++) {
1557 ds_destroy(&output[i]);
1562 vsctl_fatal("transaction error");
1568 for (i = 0; i < n_output; i++) {
1569 struct ds *ds = &output[i];
1574 for (j = 0; j < ds->length; j++) {
1575 int c = ds->string[j];
1578 fputs("\\n", stdout);
1582 fputs("\\\\", stdout);
1591 fputs(ds_cstr(ds), stdout);
1595 if (wait_for_reload && status != TXN_UNCHANGED) {
1597 const struct ovsrec_open_vswitch *ovs;
1600 OVSREC_OPEN_VSWITCH_FOR_EACH (ovs, idl) {
1601 if (ovs->cur_cfg >= next_cfg) {
1605 ovsdb_idl_wait(idl);
1614 static vsctl_handler_func *
1615 get_vsctl_handler(int argc, char *argv[], struct vsctl_context *ctx)
1617 static const struct vsctl_command all_commands[] = {
1618 /* Open vSwitch commands. */
1619 {"init", 0, 0, cmd_init, ""},
1621 /* Bridge commands. */
1622 {"add-br", 1, 3, cmd_add_br, ""},
1623 {"del-br", 1, 1, cmd_del_br, "--if-exists"},
1624 {"list-br", 0, 0, cmd_list_br, ""},
1625 {"br-exists", 1, 1, cmd_br_exists, ""},
1626 {"br-to-vlan", 1, 1, cmd_br_to_vlan, ""},
1627 {"br-to-parent", 1, 1, cmd_br_to_parent, ""},
1628 {"br-set-external-id", 2, 3, cmd_br_set_external_id, ""},
1629 {"br-get-external-id", 1, 2, cmd_br_get_external_id, ""},
1631 /* Port commands. */
1632 {"list-ports", 1, 1, cmd_list_ports, ""},
1633 {"add-port", 2, 2, cmd_add_port, ""},
1634 {"add-bond", 4, INT_MAX, cmd_add_bond, ""},
1635 {"del-port", 1, 2, cmd_del_port, "--if-exists"},
1636 {"port-to-br", 1, 1, cmd_port_to_br, ""},
1637 {"port-set-external-id", 2, 3, cmd_port_set_external_id, ""},
1638 {"port-get-external-id", 1, 2, cmd_port_get_external_id, ""},
1640 /* Interface commands. */
1641 {"list-ifaces", 1, 1, cmd_list_ifaces, ""},
1642 {"iface-to-br", 1, 1, cmd_iface_to_br, ""},
1643 {"iface-set-external-id", 2, 3, cmd_iface_set_external_id, ""},
1644 {"iface-get-external-id", 1, 2, cmd_iface_get_external_id, ""},
1646 /* Controller commands. */
1647 {"get-controller", 0, 1, cmd_get_controller, ""},
1648 {"del-controller", 0, 1, cmd_del_controller, ""},
1649 {"set-controller", 1, 2, cmd_set_controller, ""},
1650 {"get-fail-mode", 0, 1, cmd_get_fail_mode, ""},
1651 {"del-fail-mode", 0, 1, cmd_del_fail_mode, ""},
1652 {"set-fail-mode", 1, 2, cmd_set_fail_mode, ""},
1655 {"get-ssl", 0, 0, cmd_get_ssl, ""},
1656 {"del-ssl", 0, 0, cmd_del_ssl, ""},
1657 {"set-ssl", 3, 3, cmd_set_ssl, "--bootstrap"},
1660 const struct vsctl_command *p;
1663 shash_init(&ctx->options);
1664 for (i = 0; i < argc; i++) {
1665 if (argv[i][0] != '-') {
1668 if (!shash_add_once(&ctx->options, argv[i], NULL)) {
1669 vsctl_fatal("'%s' option specified multiple times", argv[i]);
1673 vsctl_fatal("missing command name");
1676 for (p = all_commands; p < &all_commands[ARRAY_SIZE(all_commands)]; p++) {
1677 if (!strcmp(p->name, argv[i])) {
1678 struct shash_node *node;
1681 SHASH_FOR_EACH (node, &ctx->options) {
1682 const char *s = strstr(p->options, node->name);
1683 int end = s ? s[strlen(node->name)] : EOF;
1684 if (end != ',' && end != ' ' && end != '\0') {
1685 vsctl_fatal("'%s' command has no '%s' option",
1686 argv[i], node->name);
1690 n_arg = argc - i - 1;
1691 if (n_arg < p->min_args) {
1692 vsctl_fatal("'%s' command requires at least %d arguments",
1693 p->name, p->min_args);
1694 } else if (n_arg > p->max_args) {
1695 vsctl_fatal("'%s' command takes at most %d arguments",
1696 p->name, p->max_args);
1698 ctx->argc = n_arg + 1;
1699 ctx->argv = &argv[i];
1705 vsctl_fatal("unknown command '%s'; use --help for help", argv[i]);
1709 check_vsctl_command(int argc, char *argv[])
1711 struct vsctl_context ctx;
1713 get_vsctl_handler(argc, argv, &ctx);
1714 shash_destroy(&ctx.options);
1718 run_vsctl_command(int argc, char *argv[],
1719 const struct ovsrec_open_vswitch *ovs, struct ds *output)
1721 vsctl_handler_func *function;
1722 struct vsctl_context ctx;
1724 function = get_vsctl_handler(argc, argv, &ctx);
1726 ds_init(&ctx.output);
1728 *output = ctx.output;
1729 shash_destroy(&ctx.options);