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("\nOptions:\n"
315 "connect to DATABASE\n"
319 "print exactly one line of output per command\n",
322 printf("\nOther options:\n"
324 "display this help message\n"
326 "display version information\n");
335 def = xasprintf("unix:%s/ovsdb-server", ovs_rundir);
340 struct vsctl_context {
343 const struct ovsrec_open_vswitch *ovs;
345 struct shash options;
348 struct vsctl_bridge {
349 struct ovsrec_bridge *br_cfg;
351 struct ovsrec_controller *ctrl;
352 struct vsctl_bridge *parent;
357 struct ovsrec_port *port_cfg;
358 struct vsctl_bridge *bridge;
362 struct ovsrec_interface *iface_cfg;
363 struct vsctl_port *port;
367 struct shash bridges;
370 struct ovsrec_controller *ctrl;
373 static struct ovsdb_idl_txn *
374 txn_from_openvswitch(const struct ovsrec_open_vswitch *ovs)
376 return ovsdb_idl_txn_get(&ovs->header_);
379 static struct vsctl_bridge *
380 add_bridge(struct vsctl_info *b,
381 struct ovsrec_bridge *br_cfg, const char *name,
382 struct vsctl_bridge *parent, int vlan)
384 struct vsctl_bridge *br = xmalloc(sizeof *br);
386 br->name = xstrdup(name);
389 br->ctrl = br_cfg->controller;
390 shash_add(&b->bridges, br->name, br);
395 port_is_fake_bridge(const struct ovsrec_port *port_cfg)
397 return (port_cfg->fake_bridge
399 && *port_cfg->tag >= 1 && *port_cfg->tag <= 4095);
402 static struct vsctl_bridge *
403 find_vlan_bridge(struct vsctl_info *info,
404 struct vsctl_bridge *parent, int vlan)
406 struct shash_node *node;
408 SHASH_FOR_EACH (node, &info->bridges) {
409 struct vsctl_bridge *br = node->data;
410 if (br->parent == parent && br->vlan == vlan) {
419 free_info(struct vsctl_info *info)
421 struct shash_node *node;
423 SHASH_FOR_EACH (node, &info->bridges) {
424 struct vsctl_bridge *bridge = node->data;
428 shash_destroy(&info->bridges);
430 SHASH_FOR_EACH (node, &info->ports) {
431 struct vsctl_port *port = node->data;
434 shash_destroy(&info->ports);
436 SHASH_FOR_EACH (node, &info->ifaces) {
437 struct vsctl_iface *iface = node->data;
440 shash_destroy(&info->ifaces);
444 get_info(const struct ovsrec_open_vswitch *ovs, struct vsctl_info *info)
446 struct shash bridges, ports;
449 shash_init(&info->bridges);
450 shash_init(&info->ports);
451 shash_init(&info->ifaces);
453 info->ctrl = ovs->controller;
455 shash_init(&bridges);
457 for (i = 0; i < ovs->n_bridges; i++) {
458 struct ovsrec_bridge *br_cfg = ovs->bridges[i];
459 struct vsctl_bridge *br;
462 if (!shash_add_once(&bridges, br_cfg->name, NULL)) {
463 VLOG_WARN("%s: database contains duplicate bridge name",
467 br = add_bridge(info, br_cfg, br_cfg->name, NULL, 0);
472 for (j = 0; j < br_cfg->n_ports; j++) {
473 struct ovsrec_port *port_cfg = br_cfg->ports[j];
475 if (!shash_add_once(&ports, port_cfg->name, NULL)) {
476 VLOG_WARN("%s: database contains duplicate port name",
481 if (port_is_fake_bridge(port_cfg)
482 && shash_add_once(&bridges, port_cfg->name, NULL)) {
483 add_bridge(info, NULL, port_cfg->name, br, *port_cfg->tag);
487 shash_destroy(&bridges);
488 shash_destroy(&ports);
490 shash_init(&bridges);
492 for (i = 0; i < ovs->n_bridges; i++) {
493 struct ovsrec_bridge *br_cfg = ovs->bridges[i];
494 struct vsctl_bridge *br;
497 if (!shash_add_once(&bridges, br_cfg->name, NULL)) {
500 br = shash_find_data(&info->bridges, br_cfg->name);
501 for (j = 0; j < br_cfg->n_ports; j++) {
502 struct ovsrec_port *port_cfg = br_cfg->ports[j];
503 struct vsctl_port *port;
506 if (!shash_add_once(&ports, port_cfg->name, NULL)) {
510 if (port_is_fake_bridge(port_cfg)
511 && !shash_add_once(&bridges, port_cfg->name, NULL)) {
515 port = xmalloc(sizeof *port);
516 port->port_cfg = port_cfg;
518 && *port_cfg->tag >= 1 && *port_cfg->tag <= 4095) {
519 port->bridge = find_vlan_bridge(info, br, *port_cfg->tag);
526 shash_add(&info->ports, port_cfg->name, port);
528 for (k = 0; k < port_cfg->n_interfaces; k++) {
529 struct ovsrec_interface *iface_cfg = port_cfg->interfaces[k];
530 struct vsctl_iface *iface;
532 if (shash_find(&info->ifaces, iface_cfg->name)) {
533 VLOG_WARN("%s: database contains duplicate interface name",
538 iface = xmalloc(sizeof *iface);
539 iface->iface_cfg = iface_cfg;
541 shash_add(&info->ifaces, iface_cfg->name, iface);
545 shash_destroy(&bridges);
546 shash_destroy(&ports);
550 check_conflicts(struct vsctl_info *info, const char *name,
553 struct vsctl_iface *iface;
554 struct vsctl_port *port;
556 if (shash_find(&info->bridges, name)) {
557 vsctl_fatal("%s because a bridge named %s already exists",
561 port = shash_find_data(&info->ports, name);
563 vsctl_fatal("%s because a port named %s already exists on "
564 "bridge %s", msg, name, port->bridge->name);
567 iface = shash_find_data(&info->ifaces, name);
569 vsctl_fatal("%s because an interface named %s already exists "
570 "on bridge %s", msg, name, iface->port->bridge->name);
576 static struct vsctl_bridge *
577 find_bridge(struct vsctl_info *info, const char *name, bool must_exist)
579 struct vsctl_bridge *br = shash_find_data(&info->bridges, name);
580 if (must_exist && !br) {
581 vsctl_fatal("no bridge named %s", name);
586 static struct vsctl_port *
587 find_port(struct vsctl_info *info, const char *name, bool must_exist)
589 struct vsctl_port *port = shash_find_data(&info->ports, name);
590 if (port && !strcmp(name, port->bridge->name)) {
593 if (must_exist && !port) {
594 vsctl_fatal("no port named %s", name);
599 static struct vsctl_iface *
600 find_iface(struct vsctl_info *info, const char *name, bool must_exist)
602 struct vsctl_iface *iface = shash_find_data(&info->ifaces, name);
603 if (iface && !strcmp(name, iface->port->bridge->name)) {
606 if (must_exist && !iface) {
607 vsctl_fatal("no interface named %s", name);
613 bridge_insert_port(struct ovsrec_bridge *br, struct ovsrec_port *port)
615 struct ovsrec_port **ports;
618 ports = xmalloc(sizeof *br->ports * (br->n_ports + 1));
619 for (i = 0; i < br->n_ports; i++) {
620 ports[i] = br->ports[i];
622 ports[br->n_ports] = port;
623 ovsrec_bridge_set_ports(br, ports, br->n_ports + 1);
628 bridge_delete_port(struct ovsrec_bridge *br, struct ovsrec_port *port)
630 struct ovsrec_port **ports;
633 ports = xmalloc(sizeof *br->ports * br->n_ports);
634 for (i = n = 0; i < br->n_ports; i++) {
635 if (br->ports[i] != port) {
636 ports[n++] = br->ports[i];
639 ovsrec_bridge_set_ports(br, ports, n);
644 ovs_insert_bridge(const struct ovsrec_open_vswitch *ovs,
645 struct ovsrec_bridge *bridge)
647 struct ovsrec_bridge **bridges;
650 bridges = xmalloc(sizeof *ovs->bridges * (ovs->n_bridges + 1));
651 for (i = 0; i < ovs->n_bridges; i++) {
652 bridges[i] = ovs->bridges[i];
654 bridges[ovs->n_bridges] = bridge;
655 ovsrec_open_vswitch_set_bridges(ovs, bridges, ovs->n_bridges + 1);
660 ovs_delete_bridge(const struct ovsrec_open_vswitch *ovs,
661 struct ovsrec_bridge *bridge)
663 struct ovsrec_bridge **bridges;
666 bridges = xmalloc(sizeof *ovs->bridges * ovs->n_bridges);
667 for (i = n = 0; i < ovs->n_bridges; i++) {
668 if (ovs->bridges[i] != bridge) {
669 bridges[n++] = ovs->bridges[i];
672 ovsrec_open_vswitch_set_bridges(ovs, bridges, n);
677 cmd_init(struct vsctl_context *ctx UNUSED)
682 cmd_add_br(struct vsctl_context *ctx)
684 const char *br_name = ctx->argv[1];
685 struct vsctl_info info;
687 get_info(ctx->ovs, &info);
688 check_conflicts(&info, br_name,
689 xasprintf("cannot create a bridge named %s", br_name));
691 if (ctx->argc == 2) {
692 struct ovsrec_bridge *br;
693 struct ovsrec_port *port;
694 struct ovsrec_interface *iface;
696 iface = ovsrec_interface_insert(txn_from_openvswitch(ctx->ovs));
697 ovsrec_interface_set_name(iface, br_name);
699 port = ovsrec_port_insert(txn_from_openvswitch(ctx->ovs));
700 ovsrec_port_set_name(port, br_name);
701 ovsrec_port_set_interfaces(port, &iface, 1);
703 br = ovsrec_bridge_insert(txn_from_openvswitch(ctx->ovs));
704 ovsrec_bridge_set_name(br, br_name);
705 ovsrec_bridge_set_ports(br, &port, 1);
707 ovs_insert_bridge(ctx->ovs, br);
708 } else if (ctx->argc == 3) {
709 vsctl_fatal("'%s' command takes exactly 1 or 3 arguments",
711 } else if (ctx->argc == 4) {
712 const char *parent_name = ctx->argv[2];
713 int vlan = atoi(ctx->argv[3]);
714 struct ovsrec_bridge *br;
715 struct vsctl_bridge *parent;
716 struct ovsrec_port *port;
717 struct ovsrec_interface *iface;
720 if (vlan < 1 || vlan > 4095) {
721 vsctl_fatal("%s: vlan must be between 1 and 4095", ctx->argv[0]);
724 parent = find_bridge(&info, parent_name, false);
725 if (parent && parent->vlan) {
726 vsctl_fatal("cannot create brdige with fake bridge as parent");
729 vsctl_fatal("parent bridge %s does not exist", parent_name);
733 iface = ovsrec_interface_insert(txn_from_openvswitch(ctx->ovs));
734 ovsrec_interface_set_name(iface, br_name);
735 ovsrec_interface_set_type(iface, "internal");
737 port = ovsrec_port_insert(txn_from_openvswitch(ctx->ovs));
738 ovsrec_port_set_name(port, br_name);
739 ovsrec_port_set_interfaces(port, &iface, 1);
740 ovsrec_port_set_fake_bridge(port, true);
741 ovsrec_port_set_tag(port, &tag, 1);
743 bridge_insert_port(br, port);
752 del_port(struct vsctl_info *info, struct vsctl_port *port)
754 struct shash_node *node;
756 SHASH_FOR_EACH (node, &info->ifaces) {
757 struct vsctl_iface *iface = node->data;
758 if (iface->port == port) {
759 ovsrec_interface_delete(iface->iface_cfg);
762 ovsrec_port_delete(port->port_cfg);
764 bridge_delete_port((port->bridge->parent
765 ? port->bridge->parent->br_cfg
766 : port->bridge->br_cfg), port->port_cfg);
770 cmd_del_br(struct vsctl_context *ctx)
772 bool must_exist = !shash_find(&ctx->options, "--if-exists");
773 struct vsctl_bridge *bridge;
774 struct vsctl_info info;
776 get_info(ctx->ovs, &info);
777 bridge = find_bridge(&info, ctx->argv[1], must_exist);
779 struct shash_node *node;
781 SHASH_FOR_EACH (node, &info.ports) {
782 struct vsctl_port *port = node->data;
783 if (port->bridge == bridge
784 || !strcmp(port->port_cfg->name, bridge->name)) {
785 del_port(&info, port);
788 if (bridge->br_cfg) {
789 ovsrec_bridge_delete(bridge->br_cfg);
790 ovs_delete_bridge(ctx->ovs, bridge->br_cfg);
797 output_sorted(struct svec *svec, struct ds *output)
803 SVEC_FOR_EACH (i, name, svec) {
804 ds_put_format(output, "%s\n", name);
809 cmd_list_br(struct vsctl_context *ctx)
811 struct shash_node *node;
812 struct vsctl_info info;
815 get_info(ctx->ovs, &info);
818 SHASH_FOR_EACH (node, &info.bridges) {
819 struct vsctl_bridge *br = node->data;
820 svec_add(&bridges, br->name);
822 output_sorted(&bridges, &ctx->output);
823 svec_destroy(&bridges);
829 cmd_br_exists(struct vsctl_context *ctx)
831 struct vsctl_info info;
833 get_info(ctx->ovs, &info);
834 if (!find_bridge(&info, ctx->argv[1], false)) {
840 /* Returns true if 'b_prefix' (of length 'b_prefix_len') concatenated with 'b'
841 * equals 'a', false otherwise. */
843 key_matches(const char *a,
844 const char *b_prefix, size_t b_prefix_len, const char *b)
846 return !strncmp(a, b_prefix, b_prefix_len) && !strcmp(a + b_prefix_len, b);
850 set_external_id(char **old_keys, char **old_values, size_t old_n,
851 char *key, char *value,
852 char ***new_keysp, char ***new_valuesp, size_t *new_np)
859 new_keys = xmalloc(sizeof *new_keys * (old_n + 1));
860 new_values = xmalloc(sizeof *new_values * (old_n + 1));
862 for (i = 0; i < old_n; i++) {
863 if (strcmp(key, old_keys[i])) {
864 new_keys[new_n] = old_keys[i];
865 new_values[new_n] = old_values[i];
870 new_keys[new_n] = key;
871 new_values[new_n] = value;
874 *new_keysp = new_keys;
875 *new_valuesp = new_values;
880 cmd_br_set_external_id(struct vsctl_context *ctx)
882 struct vsctl_info info;
883 struct vsctl_bridge *bridge;
884 char **keys, **values;
887 get_info(ctx->ovs, &info);
888 bridge = find_bridge(&info, ctx->argv[1], true);
889 if (bridge->br_cfg) {
890 set_external_id(bridge->br_cfg->key_external_ids,
891 bridge->br_cfg->value_external_ids,
892 bridge->br_cfg->n_external_ids,
893 ctx->argv[2], ctx->argc >= 4 ? ctx->argv[3] : NULL,
895 ovsrec_bridge_set_external_ids(bridge->br_cfg, keys, values, n);
897 char *key = xasprintf("fake-bridge-%s", ctx->argv[2]);
898 struct vsctl_port *port = shash_find_data(&info.ports, ctx->argv[1]);
899 set_external_id(port->port_cfg->key_external_ids,
900 port->port_cfg->value_external_ids,
901 port->port_cfg->n_external_ids,
902 key, ctx->argc >= 4 ? ctx->argv[3] : NULL,
904 ovsrec_port_set_external_ids(port->port_cfg, keys, values, n);
914 get_external_id(char **keys, char **values, size_t n,
915 const char *prefix, const char *key,
918 size_t prefix_len = strlen(prefix);
923 for (i = 0; i < n; i++) {
924 if (!key && !strncmp(keys[i], prefix, prefix_len)) {
925 svec_add_nocopy(&svec, xasprintf("%s=%s",
926 keys[i] + prefix_len, values[i]));
927 } else if (key_matches(keys[i], prefix, prefix_len, key)) {
928 svec_add(&svec, values[i]);
932 output_sorted(&svec, output);
937 cmd_br_get_external_id(struct vsctl_context *ctx)
939 struct vsctl_info info;
940 struct vsctl_bridge *bridge;
942 get_info(ctx->ovs, &info);
943 bridge = find_bridge(&info, ctx->argv[1], true);
944 if (bridge->br_cfg) {
945 get_external_id(bridge->br_cfg->key_external_ids,
946 bridge->br_cfg->value_external_ids,
947 bridge->br_cfg->n_external_ids,
948 "", ctx->argc >= 3 ? ctx->argv[2] : NULL,
951 struct vsctl_port *port = shash_find_data(&info.ports, ctx->argv[1]);
952 get_external_id(port->port_cfg->key_external_ids,
953 port->port_cfg->value_external_ids,
954 port->port_cfg->n_external_ids,
955 "fake-bridge-", ctx->argc >= 3 ? ctx->argv[2] : NULL, &ctx->output);
962 cmd_list_ports(struct vsctl_context *ctx)
964 struct vsctl_bridge *br;
965 struct shash_node *node;
966 struct vsctl_info info;
969 get_info(ctx->ovs, &info);
970 br = find_bridge(&info, ctx->argv[1], true);
973 SHASH_FOR_EACH (node, &info.ports) {
974 struct vsctl_port *port = node->data;
976 if (strcmp(port->port_cfg->name, br->name) && br == port->bridge) {
977 svec_add(&ports, port->port_cfg->name);
980 output_sorted(&ports, &ctx->output);
981 svec_destroy(&ports);
987 add_port(const struct ovsrec_open_vswitch *ovs,
988 const char *br_name, const char *port_name,
989 char *iface_names[], int n_ifaces)
991 struct vsctl_info info;
992 struct vsctl_bridge *bridge;
993 struct ovsrec_interface **ifaces;
994 struct ovsrec_port *port;
997 get_info(ovs, &info);
998 check_conflicts(&info, port_name,
999 xasprintf("cannot create a port named %s", port_name));
1000 /* XXX need to check for conflicts on interfaces too */
1001 bridge = find_bridge(&info, br_name, true);
1003 ifaces = xmalloc(n_ifaces * sizeof *ifaces);
1004 for (i = 0; i < n_ifaces; i++) {
1005 ifaces[i] = ovsrec_interface_insert(txn_from_openvswitch(ovs));
1006 ovsrec_interface_set_name(ifaces[i], iface_names[i]);
1009 port = ovsrec_port_insert(txn_from_openvswitch(ovs));
1010 ovsrec_port_set_name(port, port_name);
1011 ovsrec_port_set_interfaces(port, ifaces, n_ifaces);
1015 int64_t tag = bridge->vlan;
1016 ovsrec_port_set_tag(port, &tag, 1);
1019 bridge_insert_port((bridge->parent ? bridge->parent->br_cfg
1020 : bridge->br_cfg), port);
1026 cmd_add_port(struct vsctl_context *ctx)
1028 add_port(ctx->ovs, ctx->argv[1], ctx->argv[2], &ctx->argv[2], 1);
1032 cmd_add_bond(struct vsctl_context *ctx)
1034 add_port(ctx->ovs, ctx->argv[1], ctx->argv[2], &ctx->argv[3], ctx->argc - 3);
1038 cmd_del_port(struct vsctl_context *ctx)
1040 bool must_exist = !shash_find(&ctx->options, "--if-exists");
1041 struct vsctl_info info;
1043 get_info(ctx->ovs, &info);
1044 if (ctx->argc == 2) {
1045 struct vsctl_port *port = find_port(&info, ctx->argv[1], must_exist);
1047 del_port(&info, port);
1049 } else if (ctx->argc == 3) {
1050 struct vsctl_bridge *bridge = find_bridge(&info, ctx->argv[1], true);
1051 struct vsctl_port *port = find_port(&info, ctx->argv[2], must_exist);
1054 if (port->bridge == bridge) {
1055 del_port(&info, port);
1056 } else if (port->bridge->parent == bridge) {
1057 vsctl_fatal("bridge %s does not have a port %s (although its "
1058 "parent bridge %s does)",
1059 ctx->argv[1], ctx->argv[2], bridge->parent->name);
1061 vsctl_fatal("bridge %s does not have a port %s",
1062 ctx->argv[1], ctx->argv[2]);
1070 cmd_port_to_br(struct vsctl_context *ctx)
1072 struct vsctl_port *port;
1073 struct vsctl_info info;
1075 get_info(ctx->ovs, &info);
1076 port = find_port(&info, ctx->argv[1], true);
1077 ds_put_format(&ctx->output, "%s\n", port->bridge->name);
1082 cmd_port_set_external_id(struct vsctl_context *ctx)
1084 struct vsctl_info info;
1085 struct vsctl_port *port;
1086 char **keys, **values;
1089 get_info(ctx->ovs, &info);
1090 port = find_port(&info, ctx->argv[1], true);
1091 set_external_id(port->port_cfg->key_external_ids,
1092 port->port_cfg->value_external_ids,
1093 port->port_cfg->n_external_ids,
1094 ctx->argv[2], ctx->argc >= 4 ? ctx->argv[3] : NULL,
1095 &keys, &values, &n);
1096 ovsrec_port_set_external_ids(port->port_cfg, keys, values, n);
1104 cmd_port_get_external_id(struct vsctl_context *ctx)
1106 struct vsctl_info info;
1107 struct vsctl_port *port;
1109 get_info(ctx->ovs, &info);
1110 port = find_port(&info, ctx->argv[1], true);
1111 get_external_id(port->port_cfg->key_external_ids,
1112 port->port_cfg->value_external_ids,
1113 port->port_cfg->n_external_ids,
1114 "", ctx->argc >= 3 ? ctx->argv[2] : NULL, &ctx->output);
1119 cmd_br_to_vlan(struct vsctl_context *ctx)
1121 struct vsctl_bridge *bridge;
1122 struct vsctl_info info;
1124 get_info(ctx->ovs, &info);
1125 bridge = find_bridge(&info, ctx->argv[1], true);
1126 ds_put_format(&ctx->output, "%d\n", bridge->vlan);
1131 cmd_br_to_parent(struct vsctl_context *ctx)
1133 struct vsctl_bridge *bridge;
1134 struct vsctl_info info;
1136 get_info(ctx->ovs, &info);
1137 bridge = find_bridge(&info, ctx->argv[1], true);
1138 if (bridge->parent) {
1139 bridge = bridge->parent;
1141 ds_put_format(&ctx->output, "%s\n", bridge->name);
1146 cmd_list_ifaces(struct vsctl_context *ctx)
1148 struct vsctl_bridge *br;
1149 struct shash_node *node;
1150 struct vsctl_info info;
1153 get_info(ctx->ovs, &info);
1154 br = find_bridge(&info, ctx->argv[1], true);
1157 SHASH_FOR_EACH (node, &info.ifaces) {
1158 struct vsctl_iface *iface = node->data;
1160 if (strcmp(iface->iface_cfg->name, br->name)
1161 && br == iface->port->bridge) {
1162 svec_add(&ifaces, iface->iface_cfg->name);
1165 output_sorted(&ifaces, &ctx->output);
1166 svec_destroy(&ifaces);
1172 cmd_iface_to_br(struct vsctl_context *ctx)
1174 struct vsctl_iface *iface;
1175 struct vsctl_info info;
1177 get_info(ctx->ovs, &info);
1178 iface = find_iface(&info, ctx->argv[1], true);
1179 ds_put_format(&ctx->output, "%s\n", iface->port->bridge->name);
1184 cmd_iface_set_external_id(struct vsctl_context *ctx)
1186 struct vsctl_info info;
1187 struct vsctl_iface *iface;
1188 char **keys, **values;
1191 get_info(ctx->ovs, &info);
1192 iface = find_iface(&info, ctx->argv[1], true);
1193 set_external_id(iface->iface_cfg->key_external_ids,
1194 iface->iface_cfg->value_external_ids,
1195 iface->iface_cfg->n_external_ids,
1196 ctx->argv[2], ctx->argc >= 4 ? ctx->argv[3] : NULL,
1197 &keys, &values, &n);
1198 ovsrec_interface_set_external_ids(iface->iface_cfg, keys, values, n);
1206 cmd_iface_get_external_id(struct vsctl_context *ctx)
1208 struct vsctl_info info;
1209 struct vsctl_iface *iface;
1211 get_info(ctx->ovs, &info);
1212 iface = find_iface(&info, ctx->argv[1], true);
1213 get_external_id(iface->iface_cfg->key_external_ids,
1214 iface->iface_cfg->value_external_ids,
1215 iface->iface_cfg->n_external_ids,
1216 "", ctx->argc >= 3 ? ctx->argv[2] : NULL, &ctx->output);
1221 cmd_get_controller(struct vsctl_context *ctx)
1223 struct vsctl_info info;
1225 get_info(ctx->ovs, &info);
1227 if (ctx->argc == 1) {
1228 /* Return the controller from the "Open_vSwitch" table */
1230 ds_put_format(&ctx->output, "%s\n", info.ctrl->target);
1233 /* Return the controller for a particular bridge. */
1234 struct vsctl_bridge *br = find_bridge(&info, ctx->argv[1], true);
1236 /* If no controller is explicitly defined for the requested
1237 * bridge, fallback to the "Open_vSwitch" table's controller. */
1239 ds_put_format(&ctx->output, "%s\n", br->ctrl->target);
1240 } else if (info.ctrl) {
1241 ds_put_format(&ctx->output, "%s\n", info.ctrl->target);
1249 cmd_del_controller(struct vsctl_context *ctx)
1251 struct vsctl_info info;
1253 get_info(ctx->ovs, &info);
1255 if (ctx->argc == 1) {
1257 ovsrec_controller_delete(info.ctrl);
1258 ovsrec_open_vswitch_set_controller(ctx->ovs, NULL);
1261 struct vsctl_bridge *br = find_bridge(&info, ctx->argv[1], true);
1264 ovsrec_controller_delete(br->ctrl);
1265 ovsrec_bridge_set_controller(br->br_cfg, NULL);
1273 cmd_set_controller(struct vsctl_context *ctx)
1275 struct vsctl_info info;
1276 struct ovsrec_controller *ctrl;
1278 get_info(ctx->ovs, &info);
1280 if (ctx->argc == 2) {
1281 /* Set the controller in the "Open_vSwitch" table. */
1283 ovsrec_controller_delete(info.ctrl);
1285 ctrl = ovsrec_controller_insert(txn_from_openvswitch(ctx->ovs));
1286 ovsrec_controller_set_target(ctrl, ctx->argv[1]);
1287 ovsrec_open_vswitch_set_controller(ctx->ovs, ctrl);
1289 /* Set the controller for a particular bridge. */
1290 struct vsctl_bridge *br = find_bridge(&info, ctx->argv[1], true);
1293 ovsrec_controller_delete(br->ctrl);
1295 ctrl = ovsrec_controller_insert(txn_from_openvswitch(ctx->ovs));
1296 ovsrec_controller_set_target(ctrl, ctx->argv[2]);
1297 ovsrec_bridge_set_controller(br->br_cfg, ctrl);
1304 cmd_get_fail_mode(struct vsctl_context *ctx)
1306 struct vsctl_info info;
1307 const char *fail_mode = NULL;
1309 get_info(ctx->ovs, &info);
1311 if (ctx->argc == 1) {
1312 /* Return the fail-mode from the "Open_vSwitch" table */
1313 if (info.ctrl && info.ctrl->fail_mode) {
1314 fail_mode = info.ctrl->fail_mode;
1317 /* Return the fail-mode for a particular bridge. */
1318 struct vsctl_bridge *br = find_bridge(&info, ctx->argv[1], true);
1320 /* If no controller or fail-mode is explicitly defined for the
1321 * requested bridge, fallback to the "Open_vSwitch" table's
1323 if (br->ctrl && br->ctrl->fail_mode) {
1324 fail_mode = br->ctrl->fail_mode;
1325 } else if (info.ctrl && info.ctrl->fail_mode) {
1326 fail_mode = info.ctrl->fail_mode;
1330 if (fail_mode && strlen(fail_mode)) {
1331 ds_put_format(&ctx->output, "%s\n", info.ctrl->fail_mode);
1338 cmd_del_fail_mode(struct vsctl_context *ctx)
1340 struct vsctl_info info;
1342 get_info(ctx->ovs, &info);
1344 if (ctx->argc == 1) {
1345 if (info.ctrl && info.ctrl->fail_mode) {
1346 ovsrec_controller_set_fail_mode(info.ctrl, NULL);
1349 struct vsctl_bridge *br = find_bridge(&info, ctx->argv[1], true);
1351 if (br->ctrl && br->ctrl->fail_mode) {
1352 ovsrec_controller_set_fail_mode(br->ctrl, NULL);
1360 cmd_set_fail_mode(struct vsctl_context *ctx)
1362 struct vsctl_info info;
1363 const char *fail_mode;
1365 get_info(ctx->ovs, &info);
1367 fail_mode = (ctx->argc == 2) ? ctx->argv[1] : ctx->argv[2];
1369 if (strcmp(fail_mode, "standalone") && strcmp(fail_mode, "secure")) {
1370 vsctl_fatal("fail-mode must be \"standalone\" or \"secure\"");
1373 if (ctx->argc == 2) {
1374 /* Set the fail-mode in the "Open_vSwitch" table. */
1376 vsctl_fatal("no controller declared");
1378 ovsrec_controller_set_fail_mode(info.ctrl, fail_mode);
1380 struct vsctl_bridge *br = find_bridge(&info, ctx->argv[1], true);
1383 vsctl_fatal("no controller declared for %s", br->name);
1385 ovsrec_controller_set_fail_mode(br->ctrl, fail_mode);
1391 typedef void vsctl_handler_func(struct vsctl_context *);
1393 struct vsctl_command {
1397 vsctl_handler_func *handler;
1398 const char *options;
1401 static void run_vsctl_command(int argc, char *argv[],
1402 const struct ovsrec_open_vswitch *ovs,
1405 static struct json *
1406 where_uuid_equals(const struct uuid *uuid)
1409 json_array_create_1(
1410 json_array_create_3(
1411 json_string_create("_uuid"),
1412 json_string_create("=="),
1413 json_array_create_2(
1414 json_string_create("uuid"),
1415 json_string_create_nocopy(
1416 xasprintf(UUID_FMT, UUID_ARGS(uuid))))));
1420 do_vsctl(int argc, char *argv[], struct ovsdb_idl *idl)
1422 struct ovsdb_idl_txn *txn;
1423 const struct ovsrec_open_vswitch *ovs;
1424 enum ovsdb_idl_txn_status status;
1425 struct ds comment, *output;
1430 txn = ovsdb_idl_txn_create(idl);
1432 ovsdb_idl_txn_set_dry_run(txn);
1436 ds_put_cstr(&comment, "ovs-vsctl:");
1437 for (i = 0; i < argc; i++) {
1438 ds_put_format(&comment, " %s", argv[i]);
1440 ovsdb_idl_txn_add_comment(txn, ds_cstr(&comment));
1441 ds_destroy(&comment);
1443 ovs = ovsrec_open_vswitch_first(idl);
1445 /* XXX add verification that table is empty */
1446 ovs = ovsrec_open_vswitch_insert(txn);
1449 if (wait_for_reload) {
1450 struct json *where = where_uuid_equals(&ovs->header_.uuid);
1451 ovsdb_idl_txn_increment(txn, "Open_vSwitch", "next_cfg",
1453 json_destroy(where);
1456 output = xmalloc(argc * sizeof *output);
1458 for (start = i = 0; i <= argc; i++) {
1459 if (i == argc || !strcmp(argv[i], "--")) {
1461 ds_init(&output[n_output]);
1462 run_vsctl_command(i - start, &argv[start], ovs,
1463 &output[n_output++]);
1469 while ((status = ovsdb_idl_txn_commit(txn)) == TXN_INCOMPLETE) {
1471 ovsdb_idl_wait(idl);
1472 ovsdb_idl_txn_wait(txn);
1475 if (wait_for_reload && status == TXN_SUCCESS) {
1476 next_cfg = ovsdb_idl_txn_get_increment_new_value(txn);
1478 ovsdb_idl_txn_destroy(txn);
1481 case TXN_INCOMPLETE:
1485 /* Should not happen--we never call ovsdb_idl_txn_abort(). */
1486 vsctl_fatal("transaction aborted");
1493 for (i = 0; i < n_output; i++) {
1494 ds_destroy(&output[i]);
1499 vsctl_fatal("transaction error");
1505 for (i = 0; i < n_output; i++) {
1506 struct ds *ds = &output[i];
1511 for (j = 0; j < ds->length; j++) {
1512 int c = ds->string[j];
1515 fputs("\\n", stdout);
1519 fputs("\\\\", stdout);
1528 fputs(ds_cstr(ds), stdout);
1532 if (wait_for_reload && status != TXN_UNCHANGED) {
1534 const struct ovsrec_open_vswitch *ovs;
1537 OVSREC_OPEN_VSWITCH_FOR_EACH (ovs, idl) {
1538 if (ovs->cur_cfg >= next_cfg) {
1542 ovsdb_idl_wait(idl);
1551 static vsctl_handler_func *
1552 get_vsctl_handler(int argc, char *argv[], struct vsctl_context *ctx)
1554 static const struct vsctl_command all_commands[] = {
1555 /* Open vSwitch commands. */
1556 {"init", 0, 0, cmd_init, ""},
1558 /* Bridge commands. */
1559 {"add-br", 1, 3, cmd_add_br, ""},
1560 {"del-br", 1, 1, cmd_del_br, "--if-exists"},
1561 {"list-br", 0, 0, cmd_list_br, ""},
1562 {"br-exists", 1, 1, cmd_br_exists, ""},
1563 {"br-to-vlan", 1, 1, cmd_br_to_vlan, ""},
1564 {"br-to-parent", 1, 1, cmd_br_to_parent, ""},
1565 {"br-set-external-id", 2, 3, cmd_br_set_external_id, ""},
1566 {"br-get-external-id", 1, 2, cmd_br_get_external_id, ""},
1568 /* Port commands. */
1569 {"list-ports", 1, 1, cmd_list_ports, ""},
1570 {"add-port", 2, 2, cmd_add_port, ""},
1571 {"add-bond", 4, INT_MAX, cmd_add_bond, ""},
1572 {"del-port", 1, 2, cmd_del_port, "--if-exists"},
1573 {"port-to-br", 1, 1, cmd_port_to_br, ""},
1574 {"port-set-external-id", 2, 3, cmd_port_set_external_id, ""},
1575 {"port-get-external-id", 1, 2, cmd_port_get_external_id, ""},
1577 /* Interface commands. */
1578 {"list-ifaces", 1, 1, cmd_list_ifaces, ""},
1579 {"iface-to-br", 1, 1, cmd_iface_to_br, ""},
1580 {"iface-set-external-id", 2, 3, cmd_iface_set_external_id, ""},
1581 {"iface-get-external-id", 1, 2, cmd_iface_get_external_id, ""},
1583 /* Controller commands. */
1584 {"get-controller", 0, 1, cmd_get_controller, ""},
1585 {"del-controller", 0, 1, cmd_del_controller, ""},
1586 {"set-controller", 1, 2, cmd_set_controller, ""},
1587 {"get-fail-mode", 0, 1, cmd_get_fail_mode, ""},
1588 {"del-fail-mode", 0, 1, cmd_del_fail_mode, ""},
1589 {"set-fail-mode", 1, 2, cmd_set_fail_mode, ""},
1592 const struct vsctl_command *p;
1595 shash_init(&ctx->options);
1596 for (i = 0; i < argc; i++) {
1597 if (argv[i][0] != '-') {
1600 if (!shash_add_once(&ctx->options, argv[i], NULL)) {
1601 vsctl_fatal("'%s' option specified multiple times", argv[i]);
1605 vsctl_fatal("missing command name");
1608 for (p = all_commands; p < &all_commands[ARRAY_SIZE(all_commands)]; p++) {
1609 if (!strcmp(p->name, argv[i])) {
1610 struct shash_node *node;
1613 SHASH_FOR_EACH (node, &ctx->options) {
1614 const char *s = strstr(p->options, node->name);
1615 int end = s ? s[strlen(node->name)] : EOF;
1616 if (end != ',' && end != ' ' && end != '\0') {
1617 vsctl_fatal("'%s' command has no '%s' option",
1618 argv[i], node->name);
1622 n_arg = argc - i - 1;
1623 if (n_arg < p->min_args) {
1624 vsctl_fatal("'%s' command requires at least %d arguments",
1625 p->name, p->min_args);
1626 } else if (n_arg > p->max_args) {
1627 vsctl_fatal("'%s' command takes at most %d arguments",
1628 p->name, p->max_args);
1630 ctx->argc = n_arg + 1;
1631 ctx->argv = &argv[i];
1637 vsctl_fatal("unknown command '%s'; use --help for help", argv[i]);
1641 check_vsctl_command(int argc, char *argv[])
1643 struct vsctl_context ctx;
1645 get_vsctl_handler(argc, argv, &ctx);
1646 shash_destroy(&ctx.options);
1650 run_vsctl_command(int argc, char *argv[],
1651 const struct ovsrec_open_vswitch *ovs, struct ds *output)
1653 vsctl_handler_func *function;
1654 struct vsctl_context ctx;
1656 function = get_vsctl_handler(argc, argv, &ctx);
1658 ds_init(&ctx.output);
1660 *output = ctx.output;
1661 shash_destroy(&ctx.options);