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.
31 #include "command-line.h"
34 #include "dynamic-string.h"
36 #include "ovsdb-data.h"
37 #include "ovsdb-idl.h"
38 #include "poll-loop.h"
40 #include "vswitchd/vswitch-idl.h"
45 #define THIS_MODULE VLM_vsctl
47 /* --db: The database server to contact. */
48 static const char *db;
50 /* --oneline: Write each command's output as a single line? */
53 /* --dry-run: Do not commit any changes. */
56 /* --no-wait: Wait for ovs-vswitchd to reload its configuration? */
57 static bool wait_for_reload = true;
59 /* --timeout: Time to wait for a connection to 'db'. */
60 static int timeout = 5;
62 static void vsctl_fatal(const char *, ...) PRINTF_FORMAT(1, 2) NO_RETURN;
63 static char *default_db(void);
64 static void usage(void) NO_RETURN;
65 static void parse_options(int argc, char *argv[]);
67 static void check_vsctl_command(int argc, char *argv[]);
68 static void do_vsctl(int argc, char *argv[], struct ovsdb_idl *idl);
71 main(int argc, char *argv[])
73 struct ovsdb_idl *idl;
76 int start, n_commands;
80 set_program_name(argv[0]);
81 signal(SIGPIPE, SIG_IGN);
84 vlog_set_levels(VLM_ANY_MODULE, VLF_CONSOLE, VLL_WARN);
85 vlog_set_levels(VLM_reconnect, VLF_ANY_FACILITY, VLL_WARN);
86 parse_options(argc, argv);
92 /* Log our arguments. This is often valuable for debugging systems. */
94 for (i = 1; i < argc; i++) {
95 ds_put_format(&args, " %s", argv[i]);
97 VLOG_INFO("Called as%s", ds_cstr(&args));
100 /* Do basic command syntax checking. */
102 for (start = i = optind; i <= argc; i++) {
103 if (i == argc || !strcmp(argv[i], "--")) {
105 check_vsctl_command(i - start, &argv[start]);
112 vsctl_fatal("missing command name (use --help for help)");
115 /* Now execute the commands. */
116 idl = ovsdb_idl_create(db, &ovsrec_idl_class);
117 seqno = ovsdb_idl_get_seqno(idl);
120 unsigned int new_seqno;
123 new_seqno = ovsdb_idl_get_seqno(idl);
124 if (new_seqno != seqno) {
126 vsctl_fatal("too many database inconsistency failures");
128 do_vsctl(argc - optind, argv + optind, idl);
138 vsctl_fatal(const char *format, ...)
143 va_start(args, format);
144 message = xvasprintf(format, args);
147 vlog_set_levels(VLM_vsctl, VLF_CONSOLE, VLL_EMER);
148 VLOG_ERR("%s", message);
149 ovs_fatal(0, "%s", message);
153 parse_options(int argc, char *argv[])
156 OPT_DB = UCHAR_MAX + 1,
163 static struct option long_options[] = {
164 {"db", required_argument, 0, OPT_DB},
165 {"no-syslog", no_argument, 0, OPT_NO_SYSLOG},
166 {"no-wait", no_argument, 0, OPT_NO_WAIT},
167 {"dry-run", no_argument, 0, OPT_DRY_RUN},
168 {"oneline", no_argument, 0, OPT_ONELINE},
169 {"timeout", required_argument, 0, 't'},
170 {"help", no_argument, 0, 'h'},
171 {"version", no_argument, 0, 'V'},
180 c = getopt_long(argc, argv, "+v::hVt:", long_options, NULL);
195 vlog_set_levels(VLM_vsctl, VLF_SYSLOG, VLL_WARN);
199 wait_for_reload = false;
210 OVS_PRINT_VERSION(0, 0);
214 timeout = strtoul(optarg, NULL, 10);
216 ovs_fatal(0, "value %s on -t or --timeout is invalid",
240 %s: ovs-vswitchd management utility\n\
241 usage: %s [OPTIONS] COMMAND [ARG...]\n\
244 add-br BRIDGE create a new bridge named BRIDGE\n\
245 add-br BRIDGE PARENT VLAN create new fake BRIDGE in PARENT on VLAN\n\
246 del-br BRIDGE delete BRIDGE and all of its ports\n\
247 list-br print the names of all the bridges\n\
248 br-exists BRIDGE test whether BRIDGE exists\n\
249 br-to-vlan BRIDGE print the VLAN which BRIDGE is on\n\
250 br-to-parent BRIDGE print the parent of BRIDGE\n\
251 br-set-external-id BRIDGE KEY VALUE set KEY on BRIDGE to VALUE\n\
252 br-set-external-id BRIDGE KEY unset KEY on BRIDGE\n\
253 br-get-external-id BRIDGE KEY print value of KEY on BRIDGE\n\
254 br-get-external-id BRIDGE list key-value pairs on BRIDGE\n\
257 list-ports BRIDGE print the names of all the ports on BRIDGE\n\
258 add-port BRIDGE PORT add network device PORT to BRIDGE\n\
259 add-bond BRIDGE PORT IFACE... add bonded port PORT in BRIDGE from IFACES\n\
260 del-port [BRIDGE] PORT delete PORT (which may be bonded) from BRIDGE\n\
261 port-to-br PORT print name of bridge that contains PORT\n\
262 A bond is considered to be a single port.\n\
264 Interface commands (a bond consists of multiple interfaces):\n\
265 list-ifaces BRIDGE print the names of all interfaces on BRIDGE\n\
266 iface-to-br IFACE print name of bridge that contains IFACE\n\
268 Controller commands:\n\
269 get-controller [BRIDGE] print the controller for BRIDGE\n\
270 del-controller [BRIDGE] delete the controller for BRIDGE\n\
271 set-controller [BRIDGE] TARGET set the controller for BRIDGE to TARGET\n\
272 get-fail-mode [BRIDGE] print the fail-mode for BRIDGE\n\
273 del-fail-mode [BRIDGE] delete the fail-mode for BRIDGE\n\
274 set-fail-mode [BRIDGE] MODE set the fail-mode for BRIDGE to MODE\n\
277 get-ssl print the SSL configuration\n\
278 del-ssl delete the SSL configuration\n\
279 set-ssl PRIV-KEY CERT CA-CERT set the SSL configuration\n\
281 Database commands:\n\
282 list TBL [REC] list RECord (or all records) in TBL\n\
283 get TBL REC COL[:KEY] print values of COLumns in RECORD in TBL\n\
284 set TBL REC COL[:KEY]=VALUE set COLumn values in RECord in TBL\n\
285 add TBL REC COL [KEY=]VALUE add (KEY=)VALUE to COLumn in RECord in TBL\n\
286 remove TBL REC COL [KEY=]VALUE remove (KEY=)VALUE from COLumn\n\
287 clear TBL REC COL clear values from COLumn in RECord in TBL\n\
288 create TBL COL[:KEY]=VALUE create and initialize new record\n\
289 destroy TBL REC delete REC from TBL\n\
290 Potentially unsafe database commands require --force option.\n\
293 --db=DATABASE connect to DATABASE\n\
295 --oneline print exactly one line of output per command\n",
296 program_name, program_name, default_db());
300 -h, --help display this help message\n\
301 -V, --version display version information\n");
310 def = xasprintf("unix:%s/ovsdb-server", ovs_rundir);
315 struct vsctl_context {
318 struct ovsdb_idl *idl;
319 const struct ovsrec_open_vswitch *ovs;
321 struct shash options;
324 struct vsctl_bridge {
325 struct ovsrec_bridge *br_cfg;
327 struct ovsrec_controller *ctrl;
328 struct vsctl_bridge *parent;
333 struct ovsrec_port *port_cfg;
334 struct vsctl_bridge *bridge;
338 struct ovsrec_interface *iface_cfg;
339 struct vsctl_port *port;
343 struct shash bridges;
346 struct ovsrec_controller *ctrl;
349 static struct ovsdb_idl_txn *
350 txn_from_openvswitch(const struct ovsrec_open_vswitch *ovs)
352 return ovsdb_idl_txn_get(&ovs->header_);
355 static struct vsctl_bridge *
356 add_bridge(struct vsctl_info *b,
357 struct ovsrec_bridge *br_cfg, const char *name,
358 struct vsctl_bridge *parent, int vlan)
360 struct vsctl_bridge *br = xmalloc(sizeof *br);
362 br->name = xstrdup(name);
365 br->ctrl = parent ? parent->br_cfg->controller : br_cfg->controller;
366 shash_add(&b->bridges, br->name, br);
371 port_is_fake_bridge(const struct ovsrec_port *port_cfg)
373 return (port_cfg->fake_bridge
375 && *port_cfg->tag >= 1 && *port_cfg->tag <= 4095);
378 static struct vsctl_bridge *
379 find_vlan_bridge(struct vsctl_info *info,
380 struct vsctl_bridge *parent, int vlan)
382 struct shash_node *node;
384 SHASH_FOR_EACH (node, &info->bridges) {
385 struct vsctl_bridge *br = node->data;
386 if (br->parent == parent && br->vlan == vlan) {
395 free_info(struct vsctl_info *info)
397 struct shash_node *node;
399 SHASH_FOR_EACH (node, &info->bridges) {
400 struct vsctl_bridge *bridge = node->data;
404 shash_destroy(&info->bridges);
406 SHASH_FOR_EACH (node, &info->ports) {
407 struct vsctl_port *port = node->data;
410 shash_destroy(&info->ports);
412 SHASH_FOR_EACH (node, &info->ifaces) {
413 struct vsctl_iface *iface = node->data;
416 shash_destroy(&info->ifaces);
420 get_info(const struct ovsrec_open_vswitch *ovs, struct vsctl_info *info)
422 struct shash bridges, ports;
425 shash_init(&info->bridges);
426 shash_init(&info->ports);
427 shash_init(&info->ifaces);
429 info->ctrl = ovs->controller;
431 shash_init(&bridges);
433 for (i = 0; i < ovs->n_bridges; i++) {
434 struct ovsrec_bridge *br_cfg = ovs->bridges[i];
435 struct vsctl_bridge *br;
438 if (!shash_add_once(&bridges, br_cfg->name, NULL)) {
439 VLOG_WARN("%s: database contains duplicate bridge name",
443 br = add_bridge(info, br_cfg, br_cfg->name, NULL, 0);
448 for (j = 0; j < br_cfg->n_ports; j++) {
449 struct ovsrec_port *port_cfg = br_cfg->ports[j];
451 if (!shash_add_once(&ports, port_cfg->name, NULL)) {
452 VLOG_WARN("%s: database contains duplicate port name",
457 if (port_is_fake_bridge(port_cfg)
458 && shash_add_once(&bridges, port_cfg->name, NULL)) {
459 add_bridge(info, NULL, port_cfg->name, br, *port_cfg->tag);
463 shash_destroy(&bridges);
464 shash_destroy(&ports);
466 shash_init(&bridges);
468 for (i = 0; i < ovs->n_bridges; i++) {
469 struct ovsrec_bridge *br_cfg = ovs->bridges[i];
470 struct vsctl_bridge *br;
473 if (!shash_add_once(&bridges, br_cfg->name, NULL)) {
476 br = shash_find_data(&info->bridges, br_cfg->name);
477 for (j = 0; j < br_cfg->n_ports; j++) {
478 struct ovsrec_port *port_cfg = br_cfg->ports[j];
479 struct vsctl_port *port;
482 if (!shash_add_once(&ports, port_cfg->name, NULL)) {
486 if (port_is_fake_bridge(port_cfg)
487 && !shash_add_once(&bridges, port_cfg->name, NULL)) {
491 port = xmalloc(sizeof *port);
492 port->port_cfg = port_cfg;
494 && *port_cfg->tag >= 1 && *port_cfg->tag <= 4095) {
495 port->bridge = find_vlan_bridge(info, br, *port_cfg->tag);
502 shash_add(&info->ports, port_cfg->name, port);
504 for (k = 0; k < port_cfg->n_interfaces; k++) {
505 struct ovsrec_interface *iface_cfg = port_cfg->interfaces[k];
506 struct vsctl_iface *iface;
508 if (shash_find(&info->ifaces, iface_cfg->name)) {
509 VLOG_WARN("%s: database contains duplicate interface name",
514 iface = xmalloc(sizeof *iface);
515 iface->iface_cfg = iface_cfg;
517 shash_add(&info->ifaces, iface_cfg->name, iface);
521 shash_destroy(&bridges);
522 shash_destroy(&ports);
526 check_conflicts(struct vsctl_info *info, const char *name,
529 struct vsctl_iface *iface;
530 struct vsctl_port *port;
532 if (shash_find(&info->bridges, name)) {
533 vsctl_fatal("%s because a bridge named %s already exists",
537 port = shash_find_data(&info->ports, name);
539 vsctl_fatal("%s because a port named %s already exists on "
540 "bridge %s", msg, name, port->bridge->name);
543 iface = shash_find_data(&info->ifaces, name);
545 vsctl_fatal("%s because an interface named %s already exists "
546 "on bridge %s", msg, name, iface->port->bridge->name);
552 static struct vsctl_bridge *
553 find_bridge(struct vsctl_info *info, const char *name, bool must_exist)
555 struct vsctl_bridge *br = shash_find_data(&info->bridges, name);
556 if (must_exist && !br) {
557 vsctl_fatal("no bridge named %s", name);
562 static struct vsctl_bridge *
563 find_real_bridge(struct vsctl_info *info, const char *name, bool must_exist)
565 struct vsctl_bridge *br = find_bridge(info, name, must_exist);
566 if (br && br->parent) {
567 vsctl_fatal("%s is a fake bridge", name);
572 static struct vsctl_port *
573 find_port(struct vsctl_info *info, const char *name, bool must_exist)
575 struct vsctl_port *port = shash_find_data(&info->ports, name);
576 if (port && !strcmp(name, port->bridge->name)) {
579 if (must_exist && !port) {
580 vsctl_fatal("no port named %s", name);
585 static struct vsctl_iface *
586 find_iface(struct vsctl_info *info, const char *name, bool must_exist)
588 struct vsctl_iface *iface = shash_find_data(&info->ifaces, name);
589 if (iface && !strcmp(name, iface->port->bridge->name)) {
592 if (must_exist && !iface) {
593 vsctl_fatal("no interface named %s", name);
599 bridge_insert_port(struct ovsrec_bridge *br, struct ovsrec_port *port)
601 struct ovsrec_port **ports;
604 ports = xmalloc(sizeof *br->ports * (br->n_ports + 1));
605 for (i = 0; i < br->n_ports; i++) {
606 ports[i] = br->ports[i];
608 ports[br->n_ports] = port;
609 ovsrec_bridge_set_ports(br, ports, br->n_ports + 1);
614 bridge_delete_port(struct ovsrec_bridge *br, struct ovsrec_port *port)
616 struct ovsrec_port **ports;
619 ports = xmalloc(sizeof *br->ports * br->n_ports);
620 for (i = n = 0; i < br->n_ports; i++) {
621 if (br->ports[i] != port) {
622 ports[n++] = br->ports[i];
625 ovsrec_bridge_set_ports(br, ports, n);
630 ovs_insert_bridge(const struct ovsrec_open_vswitch *ovs,
631 struct ovsrec_bridge *bridge)
633 struct ovsrec_bridge **bridges;
636 bridges = xmalloc(sizeof *ovs->bridges * (ovs->n_bridges + 1));
637 for (i = 0; i < ovs->n_bridges; i++) {
638 bridges[i] = ovs->bridges[i];
640 bridges[ovs->n_bridges] = bridge;
641 ovsrec_open_vswitch_set_bridges(ovs, bridges, ovs->n_bridges + 1);
646 ovs_delete_bridge(const struct ovsrec_open_vswitch *ovs,
647 struct ovsrec_bridge *bridge)
649 struct ovsrec_bridge **bridges;
652 bridges = xmalloc(sizeof *ovs->bridges * ovs->n_bridges);
653 for (i = n = 0; i < ovs->n_bridges; i++) {
654 if (ovs->bridges[i] != bridge) {
655 bridges[n++] = ovs->bridges[i];
658 ovsrec_open_vswitch_set_bridges(ovs, bridges, n);
663 cmd_init(struct vsctl_context *ctx UNUSED)
668 cmd_add_br(struct vsctl_context *ctx)
670 const char *br_name = ctx->argv[1];
671 struct vsctl_info info;
673 get_info(ctx->ovs, &info);
674 check_conflicts(&info, br_name,
675 xasprintf("cannot create a bridge named %s", br_name));
677 if (ctx->argc == 2) {
678 struct ovsrec_bridge *br;
679 struct ovsrec_port *port;
680 struct ovsrec_interface *iface;
682 iface = ovsrec_interface_insert(txn_from_openvswitch(ctx->ovs));
683 ovsrec_interface_set_name(iface, br_name);
685 port = ovsrec_port_insert(txn_from_openvswitch(ctx->ovs));
686 ovsrec_port_set_name(port, br_name);
687 ovsrec_port_set_interfaces(port, &iface, 1);
689 br = ovsrec_bridge_insert(txn_from_openvswitch(ctx->ovs));
690 ovsrec_bridge_set_name(br, br_name);
691 ovsrec_bridge_set_ports(br, &port, 1);
693 ovs_insert_bridge(ctx->ovs, br);
694 } else if (ctx->argc == 3) {
695 vsctl_fatal("'%s' command takes exactly 1 or 3 arguments",
697 } else if (ctx->argc == 4) {
698 const char *parent_name = ctx->argv[2];
699 int vlan = atoi(ctx->argv[3]);
700 struct ovsrec_bridge *br;
701 struct vsctl_bridge *parent;
702 struct ovsrec_port *port;
703 struct ovsrec_interface *iface;
706 if (vlan < 1 || vlan > 4095) {
707 vsctl_fatal("%s: vlan must be between 1 and 4095", ctx->argv[0]);
710 parent = find_bridge(&info, parent_name, false);
711 if (parent && parent->vlan) {
712 vsctl_fatal("cannot create bridge with fake bridge as parent");
715 vsctl_fatal("parent bridge %s does not exist", parent_name);
719 iface = ovsrec_interface_insert(txn_from_openvswitch(ctx->ovs));
720 ovsrec_interface_set_name(iface, br_name);
721 ovsrec_interface_set_type(iface, "internal");
723 port = ovsrec_port_insert(txn_from_openvswitch(ctx->ovs));
724 ovsrec_port_set_name(port, br_name);
725 ovsrec_port_set_interfaces(port, &iface, 1);
726 ovsrec_port_set_fake_bridge(port, true);
727 ovsrec_port_set_tag(port, &tag, 1);
729 bridge_insert_port(br, port);
738 del_port(struct vsctl_info *info, struct vsctl_port *port)
740 struct shash_node *node;
742 SHASH_FOR_EACH (node, &info->ifaces) {
743 struct vsctl_iface *iface = node->data;
744 if (iface->port == port) {
745 ovsrec_interface_delete(iface->iface_cfg);
748 ovsrec_port_delete(port->port_cfg);
750 bridge_delete_port((port->bridge->parent
751 ? port->bridge->parent->br_cfg
752 : port->bridge->br_cfg), port->port_cfg);
756 cmd_del_br(struct vsctl_context *ctx)
758 bool must_exist = !shash_find(&ctx->options, "--if-exists");
759 struct vsctl_bridge *bridge;
760 struct vsctl_info info;
762 get_info(ctx->ovs, &info);
763 bridge = find_bridge(&info, ctx->argv[1], must_exist);
765 struct shash_node *node;
767 SHASH_FOR_EACH (node, &info.ports) {
768 struct vsctl_port *port = node->data;
769 if (port->bridge == bridge
770 || !strcmp(port->port_cfg->name, bridge->name)) {
771 del_port(&info, port);
774 if (bridge->br_cfg) {
775 ovsrec_bridge_delete(bridge->br_cfg);
776 ovs_delete_bridge(ctx->ovs, bridge->br_cfg);
783 output_sorted(struct svec *svec, struct ds *output)
789 SVEC_FOR_EACH (i, name, svec) {
790 ds_put_format(output, "%s\n", name);
795 cmd_list_br(struct vsctl_context *ctx)
797 struct shash_node *node;
798 struct vsctl_info info;
801 get_info(ctx->ovs, &info);
804 SHASH_FOR_EACH (node, &info.bridges) {
805 struct vsctl_bridge *br = node->data;
806 svec_add(&bridges, br->name);
808 output_sorted(&bridges, &ctx->output);
809 svec_destroy(&bridges);
815 cmd_br_exists(struct vsctl_context *ctx)
817 struct vsctl_info info;
819 get_info(ctx->ovs, &info);
820 if (!find_bridge(&info, ctx->argv[1], false)) {
826 /* Returns true if 'b_prefix' (of length 'b_prefix_len') concatenated with 'b'
827 * equals 'a', false otherwise. */
829 key_matches(const char *a,
830 const char *b_prefix, size_t b_prefix_len, const char *b)
832 return !strncmp(a, b_prefix, b_prefix_len) && !strcmp(a + b_prefix_len, b);
836 set_external_id(char **old_keys, char **old_values, size_t old_n,
837 char *key, char *value,
838 char ***new_keysp, char ***new_valuesp, size_t *new_np)
845 new_keys = xmalloc(sizeof *new_keys * (old_n + 1));
846 new_values = xmalloc(sizeof *new_values * (old_n + 1));
848 for (i = 0; i < old_n; i++) {
849 if (strcmp(key, old_keys[i])) {
850 new_keys[new_n] = old_keys[i];
851 new_values[new_n] = old_values[i];
856 new_keys[new_n] = key;
857 new_values[new_n] = value;
860 *new_keysp = new_keys;
861 *new_valuesp = new_values;
866 cmd_br_set_external_id(struct vsctl_context *ctx)
868 struct vsctl_info info;
869 struct vsctl_bridge *bridge;
870 char **keys, **values;
873 get_info(ctx->ovs, &info);
874 bridge = find_bridge(&info, ctx->argv[1], true);
875 if (bridge->br_cfg) {
876 set_external_id(bridge->br_cfg->key_external_ids,
877 bridge->br_cfg->value_external_ids,
878 bridge->br_cfg->n_external_ids,
879 ctx->argv[2], ctx->argc >= 4 ? ctx->argv[3] : NULL,
881 ovsrec_bridge_set_external_ids(bridge->br_cfg, keys, values, n);
883 char *key = xasprintf("fake-bridge-%s", ctx->argv[2]);
884 struct vsctl_port *port = shash_find_data(&info.ports, ctx->argv[1]);
885 set_external_id(port->port_cfg->key_external_ids,
886 port->port_cfg->value_external_ids,
887 port->port_cfg->n_external_ids,
888 key, ctx->argc >= 4 ? ctx->argv[3] : NULL,
890 ovsrec_port_set_external_ids(port->port_cfg, keys, values, n);
900 get_external_id(char **keys, char **values, size_t n,
901 const char *prefix, const char *key,
904 size_t prefix_len = strlen(prefix);
909 for (i = 0; i < n; i++) {
910 if (!key && !strncmp(keys[i], prefix, prefix_len)) {
911 svec_add_nocopy(&svec, xasprintf("%s=%s",
912 keys[i] + prefix_len, values[i]));
913 } else if (key_matches(keys[i], prefix, prefix_len, key)) {
914 svec_add(&svec, values[i]);
918 output_sorted(&svec, output);
923 cmd_br_get_external_id(struct vsctl_context *ctx)
925 struct vsctl_info info;
926 struct vsctl_bridge *bridge;
928 get_info(ctx->ovs, &info);
929 bridge = find_bridge(&info, ctx->argv[1], true);
930 if (bridge->br_cfg) {
931 get_external_id(bridge->br_cfg->key_external_ids,
932 bridge->br_cfg->value_external_ids,
933 bridge->br_cfg->n_external_ids,
934 "", ctx->argc >= 3 ? ctx->argv[2] : NULL,
937 struct vsctl_port *port = shash_find_data(&info.ports, ctx->argv[1]);
938 get_external_id(port->port_cfg->key_external_ids,
939 port->port_cfg->value_external_ids,
940 port->port_cfg->n_external_ids,
941 "fake-bridge-", ctx->argc >= 3 ? ctx->argv[2] : NULL, &ctx->output);
948 cmd_list_ports(struct vsctl_context *ctx)
950 struct vsctl_bridge *br;
951 struct shash_node *node;
952 struct vsctl_info info;
955 get_info(ctx->ovs, &info);
956 br = find_bridge(&info, ctx->argv[1], true);
959 SHASH_FOR_EACH (node, &info.ports) {
960 struct vsctl_port *port = node->data;
962 if (strcmp(port->port_cfg->name, br->name) && br == port->bridge) {
963 svec_add(&ports, port->port_cfg->name);
966 output_sorted(&ports, &ctx->output);
967 svec_destroy(&ports);
973 add_port(const struct ovsrec_open_vswitch *ovs,
974 const char *br_name, const char *port_name, bool fake_iface,
975 char *iface_names[], int n_ifaces)
977 struct vsctl_info info;
978 struct vsctl_bridge *bridge;
979 struct ovsrec_interface **ifaces;
980 struct ovsrec_port *port;
983 get_info(ovs, &info);
984 check_conflicts(&info, port_name,
985 xasprintf("cannot create a port named %s", port_name));
986 /* XXX need to check for conflicts on interfaces too */
987 bridge = find_bridge(&info, br_name, true);
989 ifaces = xmalloc(n_ifaces * sizeof *ifaces);
990 for (i = 0; i < n_ifaces; i++) {
991 ifaces[i] = ovsrec_interface_insert(txn_from_openvswitch(ovs));
992 ovsrec_interface_set_name(ifaces[i], iface_names[i]);
995 port = ovsrec_port_insert(txn_from_openvswitch(ovs));
996 ovsrec_port_set_name(port, port_name);
997 ovsrec_port_set_interfaces(port, ifaces, n_ifaces);
998 ovsrec_port_set_bond_fake_iface(port, fake_iface);
1002 int64_t tag = bridge->vlan;
1003 ovsrec_port_set_tag(port, &tag, 1);
1006 bridge_insert_port((bridge->parent ? bridge->parent->br_cfg
1007 : bridge->br_cfg), port);
1013 cmd_add_port(struct vsctl_context *ctx)
1015 add_port(ctx->ovs, ctx->argv[1], ctx->argv[2], false, &ctx->argv[2], 1);
1019 cmd_add_bond(struct vsctl_context *ctx)
1021 bool fake_iface = shash_find(&ctx->options, "--fake-iface");
1023 add_port(ctx->ovs, ctx->argv[1], ctx->argv[2], fake_iface,
1024 &ctx->argv[3], ctx->argc - 3);
1028 cmd_del_port(struct vsctl_context *ctx)
1030 bool must_exist = !shash_find(&ctx->options, "--if-exists");
1031 struct vsctl_info info;
1033 get_info(ctx->ovs, &info);
1034 if (ctx->argc == 2) {
1035 struct vsctl_port *port = find_port(&info, ctx->argv[1], must_exist);
1037 del_port(&info, port);
1039 } else if (ctx->argc == 3) {
1040 struct vsctl_bridge *bridge = find_bridge(&info, ctx->argv[1], true);
1041 struct vsctl_port *port = find_port(&info, ctx->argv[2], must_exist);
1044 if (port->bridge == bridge) {
1045 del_port(&info, port);
1046 } else if (port->bridge->parent == bridge) {
1047 vsctl_fatal("bridge %s does not have a port %s (although its "
1048 "parent bridge %s does)",
1049 ctx->argv[1], ctx->argv[2], bridge->parent->name);
1051 vsctl_fatal("bridge %s does not have a port %s",
1052 ctx->argv[1], ctx->argv[2]);
1060 cmd_port_to_br(struct vsctl_context *ctx)
1062 struct vsctl_port *port;
1063 struct vsctl_info info;
1065 get_info(ctx->ovs, &info);
1066 port = find_port(&info, ctx->argv[1], true);
1067 ds_put_format(&ctx->output, "%s\n", port->bridge->name);
1072 cmd_br_to_vlan(struct vsctl_context *ctx)
1074 struct vsctl_bridge *bridge;
1075 struct vsctl_info info;
1077 get_info(ctx->ovs, &info);
1078 bridge = find_bridge(&info, ctx->argv[1], true);
1079 ds_put_format(&ctx->output, "%d\n", bridge->vlan);
1084 cmd_br_to_parent(struct vsctl_context *ctx)
1086 struct vsctl_bridge *bridge;
1087 struct vsctl_info info;
1089 get_info(ctx->ovs, &info);
1090 bridge = find_bridge(&info, ctx->argv[1], true);
1091 if (bridge->parent) {
1092 bridge = bridge->parent;
1094 ds_put_format(&ctx->output, "%s\n", bridge->name);
1099 cmd_list_ifaces(struct vsctl_context *ctx)
1101 struct vsctl_bridge *br;
1102 struct shash_node *node;
1103 struct vsctl_info info;
1106 get_info(ctx->ovs, &info);
1107 br = find_bridge(&info, ctx->argv[1], true);
1110 SHASH_FOR_EACH (node, &info.ifaces) {
1111 struct vsctl_iface *iface = node->data;
1113 if (strcmp(iface->iface_cfg->name, br->name)
1114 && br == iface->port->bridge) {
1115 svec_add(&ifaces, iface->iface_cfg->name);
1118 output_sorted(&ifaces, &ctx->output);
1119 svec_destroy(&ifaces);
1125 cmd_iface_to_br(struct vsctl_context *ctx)
1127 struct vsctl_iface *iface;
1128 struct vsctl_info info;
1130 get_info(ctx->ovs, &info);
1131 iface = find_iface(&info, ctx->argv[1], true);
1132 ds_put_format(&ctx->output, "%s\n", iface->port->bridge->name);
1137 cmd_get_controller(struct vsctl_context *ctx)
1139 struct vsctl_info info;
1141 get_info(ctx->ovs, &info);
1143 if (ctx->argc == 1) {
1144 /* Return the controller from the "Open_vSwitch" table */
1146 ds_put_format(&ctx->output, "%s\n", info.ctrl->target);
1149 /* Return the controller for a particular bridge. */
1150 struct vsctl_bridge *br = find_bridge(&info, ctx->argv[1], true);
1152 /* If no controller is explicitly defined for the requested
1153 * bridge, fallback to the "Open_vSwitch" table's controller. */
1155 ds_put_format(&ctx->output, "%s\n", br->ctrl->target);
1156 } else if (info.ctrl) {
1157 ds_put_format(&ctx->output, "%s\n", info.ctrl->target);
1165 cmd_del_controller(struct vsctl_context *ctx)
1167 struct vsctl_info info;
1169 get_info(ctx->ovs, &info);
1171 if (ctx->argc == 1) {
1173 ovsrec_controller_delete(info.ctrl);
1174 ovsrec_open_vswitch_set_controller(ctx->ovs, NULL);
1177 struct vsctl_bridge *br = find_real_bridge(&info, ctx->argv[1], true);
1180 ovsrec_controller_delete(br->ctrl);
1181 ovsrec_bridge_set_controller(br->br_cfg, NULL);
1189 cmd_set_controller(struct vsctl_context *ctx)
1191 struct vsctl_info info;
1192 struct ovsrec_controller *ctrl;
1194 get_info(ctx->ovs, &info);
1196 if (ctx->argc == 2) {
1197 /* Set the controller in the "Open_vSwitch" table. */
1199 ovsrec_controller_delete(info.ctrl);
1201 ctrl = ovsrec_controller_insert(txn_from_openvswitch(ctx->ovs));
1202 ovsrec_controller_set_target(ctrl, ctx->argv[1]);
1203 ovsrec_open_vswitch_set_controller(ctx->ovs, ctrl);
1205 /* Set the controller for a particular bridge. */
1206 struct vsctl_bridge *br = find_real_bridge(&info, ctx->argv[1], true);
1209 ovsrec_controller_delete(br->ctrl);
1211 ctrl = ovsrec_controller_insert(txn_from_openvswitch(ctx->ovs));
1212 ovsrec_controller_set_target(ctrl, ctx->argv[2]);
1213 ovsrec_bridge_set_controller(br->br_cfg, ctrl);
1220 cmd_get_fail_mode(struct vsctl_context *ctx)
1222 struct vsctl_info info;
1223 const char *fail_mode = NULL;
1225 get_info(ctx->ovs, &info);
1227 if (ctx->argc == 1) {
1228 /* Return the fail-mode from the "Open_vSwitch" table */
1229 if (info.ctrl && info.ctrl->fail_mode) {
1230 fail_mode = info.ctrl->fail_mode;
1233 /* Return the fail-mode for a particular bridge. */
1234 struct vsctl_bridge *br = find_bridge(&info, ctx->argv[1], true);
1236 /* If no controller or fail-mode is explicitly defined for the
1237 * requested bridge, fallback to the "Open_vSwitch" table's
1239 if (br->ctrl && br->ctrl->fail_mode) {
1240 fail_mode = br->ctrl->fail_mode;
1241 } else if (info.ctrl && info.ctrl->fail_mode) {
1242 fail_mode = info.ctrl->fail_mode;
1246 if (fail_mode && strlen(fail_mode)) {
1247 ds_put_format(&ctx->output, "%s\n", fail_mode);
1254 cmd_del_fail_mode(struct vsctl_context *ctx)
1256 struct vsctl_info info;
1258 get_info(ctx->ovs, &info);
1260 if (ctx->argc == 1) {
1261 if (info.ctrl && info.ctrl->fail_mode) {
1262 ovsrec_controller_set_fail_mode(info.ctrl, NULL);
1265 struct vsctl_bridge *br = find_real_bridge(&info, ctx->argv[1], true);
1267 if (br->ctrl && br->ctrl->fail_mode) {
1268 ovsrec_controller_set_fail_mode(br->ctrl, NULL);
1276 cmd_set_fail_mode(struct vsctl_context *ctx)
1278 struct vsctl_info info;
1279 const char *fail_mode;
1281 get_info(ctx->ovs, &info);
1283 fail_mode = (ctx->argc == 2) ? ctx->argv[1] : ctx->argv[2];
1285 if (strcmp(fail_mode, "standalone") && strcmp(fail_mode, "secure")) {
1286 vsctl_fatal("fail-mode must be \"standalone\" or \"secure\"");
1289 if (ctx->argc == 2) {
1290 /* Set the fail-mode in the "Open_vSwitch" table. */
1292 vsctl_fatal("no controller declared");
1294 ovsrec_controller_set_fail_mode(info.ctrl, fail_mode);
1296 struct vsctl_bridge *br = find_real_bridge(&info, ctx->argv[1], true);
1299 vsctl_fatal("no controller declared for %s", br->name);
1301 ovsrec_controller_set_fail_mode(br->ctrl, fail_mode);
1308 cmd_get_ssl(struct vsctl_context *ctx)
1310 struct ovsrec_ssl *ssl = ctx->ovs->ssl;
1313 ds_put_format(&ctx->output, "Private key: %s\n", ssl->private_key);
1314 ds_put_format(&ctx->output, "Certificate: %s\n", ssl->certificate);
1315 ds_put_format(&ctx->output, "CA Certificate: %s\n", ssl->ca_cert);
1316 ds_put_format(&ctx->output, "Bootstrap: %s\n",
1317 ssl->bootstrap_ca_cert ? "true" : "false");
1322 cmd_del_ssl(struct vsctl_context *ctx)
1324 struct ovsrec_ssl *ssl = ctx->ovs->ssl;
1327 ovsrec_ssl_delete(ssl);
1328 ovsrec_open_vswitch_set_ssl(ctx->ovs, NULL);
1333 cmd_set_ssl(struct vsctl_context *ctx)
1335 bool bootstrap = shash_find(&ctx->options, "--bootstrap");
1336 struct ovsrec_ssl *ssl = ctx->ovs->ssl;
1339 ovsrec_ssl_delete(ssl);
1341 ssl = ovsrec_ssl_insert(txn_from_openvswitch(ctx->ovs));
1343 ovsrec_ssl_set_private_key(ssl, ctx->argv[1]);
1344 ovsrec_ssl_set_certificate(ssl, ctx->argv[2]);
1345 ovsrec_ssl_set_ca_cert(ssl, ctx->argv[3]);
1347 ovsrec_ssl_set_bootstrap_ca_cert(ssl, bootstrap);
1349 ovsrec_open_vswitch_set_ssl(ctx->ovs, ssl);
1352 /* Parameter commands. */
1354 /* POSIX extended regular expression for an 8-bit unsigned decimal integer. */
1355 #define OCTET_RE "([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])"
1357 /* POSIX extended regular expression for an IP address. */
1358 #define IP_RE "("OCTET_RE"\\."OCTET_RE"\\."OCTET_RE"\\."OCTET_RE")"
1360 /* POSIX extended regular expression for a netmask. */
1361 #define NETMASK_RE \
1362 "255.255.255."NETMASK_END_RE"|" \
1363 "255.255."NETMASK_END_RE".0|" \
1364 "255."NETMASK_END_RE".0.0|" \
1365 NETMASK_END_RE".0.0.0"
1366 #define NETMASK_END_RE "(255|254|252|248|240|224|192|128|0)"
1368 /* POSIX extended regular expression for an Ethernet address. */
1369 #define XX_RE "[0-9a-fA-F][0-9a-fA-F]"
1370 #define MAC_RE XX_RE":"XX_RE":"XX_RE":"XX_RE":"XX_RE":"XX_RE
1372 /* POSIX extended regular expression for a TCP or UDP port number. */
1376 "[1-9][0-9][0-9]|" \
1377 "[1-9][0-9][0-9][0-9]|" \
1378 "[1-5][0-9][0-9][0-9][0-9]|" \
1379 "6[1-4][0-9][0-9][0-9]|" \
1380 "65[1-4][0-9][0-9]|" \
1385 VSCF_READONLY = 1 << 0,
1386 VSCF_HIDDEN = 1 << 1
1389 struct vsctl_column {
1390 struct ovsdb_idl_column *idl;
1392 const char *constraint;
1395 static const struct vsctl_column bridge_columns[] = {
1396 {&ovsrec_bridge_col_datapath_id, VSCF_READONLY, NULL},
1397 {&ovsrec_bridge_col_datapath_type, VSCF_READONLY, NULL},
1398 {&ovsrec_bridge_col_name, VSCF_READONLY, NULL},
1399 {&ovsrec_bridge_col_mirrors, VSCF_READONLY, NULL},
1400 {&ovsrec_bridge_col_other_config, 0, NULL},
1401 {&ovsrec_bridge_col_flood_vlans, 0, "[1,4095]"},
1402 {&ovsrec_bridge_col_controller, VSCF_READONLY, NULL},
1403 {&ovsrec_bridge_col_netflow, VSCF_READONLY, NULL},
1404 {&ovsrec_bridge_col_external_ids, 0, NULL},
1405 {&ovsrec_bridge_col_ports, VSCF_READONLY, NULL},
1409 static const struct vsctl_column controller_columns[] = {
1410 {&ovsrec_controller_col_connection_mode, 0, "in-band|out-of-band"},
1411 {&ovsrec_controller_col_controller_burst_limit, 0, "[25,]"},
1412 {&ovsrec_controller_col_controller_rate_limit, 0, "[100,]"},
1413 {&ovsrec_controller_col_discover_accept_regex, 0, NULL},
1414 {&ovsrec_controller_col_discover_update_resolv_conf, 0, NULL},
1415 {&ovsrec_controller_col_fail_mode, 0, "standalone|secure"},
1416 {&ovsrec_controller_col_inactivity_probe, 0, "[5000,]"},
1417 {&ovsrec_controller_col_local_gateway, 0, IP_RE},
1418 {&ovsrec_controller_col_local_ip, 0, IP_RE},
1419 {&ovsrec_controller_col_local_netmask, 0, NETMASK_RE},
1420 {&ovsrec_controller_col_max_backoff, 0, "[1000,]"},
1421 {&ovsrec_controller_col_target, 0, NULL},
1425 static const struct vsctl_column interface_columns[] = {
1426 {&ovsrec_interface_col_external_ids, 0, NULL},
1427 {&ovsrec_interface_col_ingress_policing_burst, 0, "[10,]"},
1428 {&ovsrec_interface_col_ingress_policing_rate, 0, "[100,]"},
1429 {&ovsrec_interface_col_mac, 0, MAC_RE},
1430 {&ovsrec_interface_col_name, VSCF_READONLY, NULL},
1431 {&ovsrec_interface_col_ofport, VSCF_READONLY, NULL},
1432 {&ovsrec_interface_col_options, 0, NULL},
1433 {&ovsrec_interface_col_type, VSCF_READONLY, NULL},
1437 static const struct vsctl_column mirror_columns[] = {
1438 {&ovsrec_mirror_col_name, VSCF_READONLY, NULL},
1439 {&ovsrec_mirror_col_output_port, 0, "Port"},
1440 {&ovsrec_mirror_col_output_vlan, 0, "[1,4095]"},
1441 {&ovsrec_mirror_col_select_dst_port, 0, "Port"},
1442 {&ovsrec_mirror_col_select_src_port, 0, "Port"},
1443 {&ovsrec_mirror_col_select_vlan, 0, "[1,4095]"},
1447 static const struct vsctl_column netflow_columns[] = {
1448 {&ovsrec_netflow_col_active_timeout, 0, "[-1,]"},
1449 {&ovsrec_netflow_col_add_id_to_interface, 0, NULL},
1450 {&ovsrec_netflow_col_targets, 0, IP_RE":"PORT_RE},
1451 {&ovsrec_netflow_col_engine_type, 0, "[0,255]"},
1452 {&ovsrec_netflow_col_engine_id, 0, "[0,255]"},
1456 static const struct vsctl_column open_vswitch_columns[] = {
1457 {&ovsrec_open_vswitch_col_bridges, VSCF_READONLY, NULL},
1458 {&ovsrec_open_vswitch_col_controller, VSCF_READONLY, NULL},
1459 {&ovsrec_open_vswitch_col_cur_cfg, VSCF_HIDDEN, NULL},
1460 {&ovsrec_open_vswitch_col_management_id, 0, "[0-9a-fA-F]{12}"},
1461 {&ovsrec_open_vswitch_col_managers, 0, "p?(ssl|tcp|unix):.*"},
1462 {&ovsrec_open_vswitch_col_next_cfg, VSCF_HIDDEN, NULL},
1463 {&ovsrec_open_vswitch_col_ssl, VSCF_READONLY, NULL},
1467 static const struct vsctl_column port_columns[] = {
1468 {&ovsrec_port_col_bond_updelay, 0, "[0,]"},
1469 {&ovsrec_port_col_bond_downdelay, 0, "[0,]"},
1470 {&ovsrec_port_col_bond_fake_iface, VSCF_READONLY, NULL},
1471 {&ovsrec_port_col_external_ids, 0, NULL},
1472 {&ovsrec_port_col_fake_bridge, VSCF_READONLY, NULL},
1473 {&ovsrec_port_col_interfaces, VSCF_READONLY, NULL},
1474 {&ovsrec_port_col_mac, 0, MAC_RE},
1475 {&ovsrec_port_col_name, VSCF_READONLY, NULL},
1476 {&ovsrec_port_col_other_config, 0, NULL},
1477 {&ovsrec_port_col_tag, 0, "[0,4095]"},
1478 {&ovsrec_port_col_trunks, 0, "[0,4095]"},
1482 static const struct vsctl_column ssl_columns[] = {
1483 {&ovsrec_ssl_col_bootstrap_ca_cert, 0, NULL},
1484 {&ovsrec_ssl_col_ca_cert, 0, NULL},
1485 {&ovsrec_ssl_col_certificate, 0, NULL},
1486 {&ovsrec_ssl_col_private_key, 0, NULL},
1490 struct vsctl_row_id {
1491 const struct ovsdb_idl_table_class *table;
1492 const struct ovsdb_idl_column *name_column;
1493 const struct ovsdb_idl_column *uuid_column;
1496 struct vsctl_table_class {
1497 struct ovsdb_idl_table_class *class;
1498 const struct vsctl_column *columns;
1499 struct vsctl_row_id row_ids[2];
1502 static const struct vsctl_table_class tables[] = {
1503 {&ovsrec_table_bridge, bridge_columns,
1504 {{&ovsrec_table_bridge, &ovsrec_bridge_col_name, NULL},
1505 {NULL, NULL, NULL}}},
1507 {&ovsrec_table_controller, controller_columns,
1508 {{&ovsrec_table_bridge,
1509 &ovsrec_bridge_col_name,
1510 &ovsrec_bridge_col_controller},
1511 {&ovsrec_table_open_vswitch,
1513 &ovsrec_open_vswitch_col_controller}}},
1515 {&ovsrec_table_interface, interface_columns,
1516 {{&ovsrec_table_interface, &ovsrec_interface_col_name, NULL},
1517 {NULL, NULL, NULL}}},
1519 {&ovsrec_table_mirror, mirror_columns,
1520 {{&ovsrec_table_mirror, &ovsrec_mirror_col_name, NULL},
1521 {NULL, NULL, NULL}}},
1523 {&ovsrec_table_netflow, netflow_columns,
1524 {{&ovsrec_table_bridge,
1525 &ovsrec_bridge_col_name,
1526 &ovsrec_bridge_col_netflow},
1527 {NULL, NULL, NULL}}},
1529 {&ovsrec_table_open_vswitch, open_vswitch_columns,
1530 {{&ovsrec_table_open_vswitch, NULL, NULL},
1531 {NULL, NULL, NULL}}},
1533 {&ovsrec_table_port, port_columns,
1534 {{&ovsrec_table_port, &ovsrec_port_col_name, NULL},
1535 {NULL, NULL, NULL}}},
1537 {&ovsrec_table_ssl, ssl_columns,
1538 {{&ovsrec_table_open_vswitch, NULL, &ovsrec_open_vswitch_col_ssl}}},
1540 {NULL, NULL, {{NULL, NULL, NULL}, {NULL, NULL, NULL}}}
1544 die_if_error(char *error)
1547 ovs_fatal(0, "%s", error);
1552 to_lower_and_underscores(unsigned c)
1554 return c == '-' ? '_' : tolower(c);
1558 score_partial_match(const char *name, const char *s)
1562 if (!strcmp(name, s)) {
1565 for (score = 0; ; score++, name++, s++) {
1566 if (to_lower_and_underscores(*name) != to_lower_and_underscores(*s)) {
1568 } else if (*name == '\0') {
1569 return UINT_MAX - 1;
1572 return *s == '\0' ? score : 0;
1575 static const struct vsctl_table_class *
1576 get_table(const char *table_name)
1578 const struct vsctl_table_class *table;
1579 const struct vsctl_table_class *best_match = NULL;
1580 unsigned int best_score = 0;
1582 for (table = tables; table->class; table++) {
1583 unsigned int score = score_partial_match(table->class->name,
1585 if (score > best_score) {
1588 } else if (score == best_score) {
1594 } else if (best_score) {
1595 ovs_fatal(0, "multiple table names match \"%s\"", table_name);
1597 ovs_fatal(0, "unknown table \"%s\"", table_name);
1601 static const struct ovsdb_idl_row *
1602 get_row_by_id(struct vsctl_context *ctx, const struct vsctl_table_class *table,
1603 const struct vsctl_row_id *id, const char *record_id)
1605 const struct ovsdb_idl_row *referrer, *final;
1611 if (!id->name_column) {
1612 if (strcmp(record_id, ".")) {
1615 referrer = ovsdb_idl_first_row(ctx->idl, id->table);
1616 if (!referrer || ovsdb_idl_next_row(referrer)) {
1620 const struct ovsdb_idl_row *row;
1621 unsigned int best_score = 0;
1623 /* It might make sense to relax this assertion. */
1624 assert(id->name_column->type.key_type == OVSDB_TYPE_STRING);
1627 for (row = ovsdb_idl_first_row(ctx->idl, id->table);
1628 row != NULL && best_score != UINT_MAX;
1629 row = ovsdb_idl_next_row(row))
1631 struct ovsdb_datum name;
1633 ovsdb_idl_txn_read(row, id->name_column, &name);
1635 unsigned int score = score_partial_match(name.keys[0].string,
1637 if (score > best_score) {
1640 } else if (score == best_score) {
1644 ovsdb_datum_destroy(&name, &id->name_column->type);
1652 if (id->uuid_column) {
1653 struct ovsdb_datum uuid;
1655 assert(id->uuid_column->type.key_type == OVSDB_TYPE_UUID);
1656 assert(id->uuid_column->type.value_type == OVSDB_TYPE_VOID);
1658 ovsdb_idl_txn_read(referrer, id->uuid_column, &uuid);
1660 final = ovsdb_idl_get_row_for_uuid(ctx->idl, table->class,
1661 &uuid.keys[0].uuid);
1663 ovsdb_datum_destroy(&uuid, &id->uuid_column->type);
1671 static const struct ovsdb_idl_row *
1672 get_row(struct vsctl_context *ctx,
1673 const struct vsctl_table_class *table, const char *record_id)
1675 const struct ovsdb_idl_row *row;
1678 if (uuid_from_string(&uuid, record_id)) {
1679 row = ovsdb_idl_get_row_for_uuid(ctx->idl, table->class, &uuid);
1683 for (i = 0; i < ARRAY_SIZE(table->row_ids); i++) {
1684 row = get_row_by_id(ctx, table, &table->row_ids[i], record_id);
1693 static const struct ovsdb_idl_row *
1694 must_get_row(struct vsctl_context *ctx,
1695 const struct vsctl_table_class *table, const char *record_id)
1697 const struct ovsdb_idl_row *row = get_row(ctx, table, record_id);
1699 ovs_fatal(0, "no row \"%s\" in table %s",
1700 record_id, table->class->name);
1706 get_column(const struct vsctl_table_class *table, const char *column_name,
1707 const struct vsctl_column **columnp)
1709 const struct vsctl_column *column;
1710 const struct vsctl_column *best_match = NULL;
1711 unsigned int best_score = 0;
1713 for (column = table->columns; column->idl; column++) {
1714 if (!(column->flags & VSCF_HIDDEN)) {
1715 unsigned int score = score_partial_match(column->idl->name,
1717 if (score > best_score) {
1718 best_match = column;
1720 } else if (score == best_score) {
1726 *columnp = best_match;
1729 } else if (best_score) {
1730 return xasprintf("%s contains more than one column whose name "
1731 "matches \"%s\"", table->class->name, column_name);
1733 return xasprintf("%s does not contain a column whose name matches "
1734 "\"%s\"", table->class->name, column_name);
1738 static char * WARN_UNUSED_RESULT
1739 parse_column_key_value(const char *arg, const struct vsctl_table_class *table,
1740 const struct vsctl_column **columnp,
1741 char **keyp, char **valuep)
1743 const char *p = arg;
1746 assert(columnp || keyp);
1754 /* Parse column name. */
1758 error = ovsdb_token_parse(&p, &column_name);
1762 if (column_name[0] == '\0') {
1764 error = xasprintf("%s: missing column name", arg);
1767 error = get_column(table, column_name, columnp);
1774 /* Parse key string. */
1775 if (*p == ':' || !columnp) {
1779 error = xasprintf("%s: key not accepted here", arg);
1782 error = ovsdb_token_parse(&p, keyp);
1790 /* Parse value string. */
1793 error = xasprintf("%s: value not accepted here", arg);
1796 *valuep = xstrdup(p + 1);
1802 error = xasprintf("%s: trailing garbage in argument at offset %td",
1825 cmd_get(struct vsctl_context *ctx)
1827 const char *table_name = ctx->argv[1];
1828 const char *record_id = ctx->argv[2];
1829 const struct vsctl_table_class *table;
1830 const struct ovsdb_idl_row *row;
1831 struct ds *out = &ctx->output;
1834 table = get_table(table_name);
1835 row = must_get_row(ctx, table, record_id);
1836 for (i = 3; i < ctx->argc; i++) {
1837 const struct vsctl_column *column;
1838 struct ovsdb_datum datum;
1841 die_if_error(parse_column_key_value(ctx->argv[i], table,
1842 &column, &key_string, NULL));
1844 ovsdb_idl_txn_read(row, column->idl, &datum);
1846 union ovsdb_atom key;
1849 if (column->idl->type.value_type == OVSDB_TYPE_VOID) {
1850 ovs_fatal(0, "cannot specify key to get for non-map column %s",
1854 die_if_error(ovsdb_atom_from_string(&key,
1855 column->idl->type.key_type,
1858 idx = ovsdb_datum_find_key(&datum, &key,
1859 column->idl->type.key_type);
1860 if (idx == UINT_MAX) {
1861 ovs_fatal(0, "no key %s in %s record \"%s\" column %s",
1862 key_string, table_name, record_id,
1866 ovsdb_atom_to_string(&datum.values[idx],
1867 column->idl->type.value_type, out);
1869 ovsdb_atom_destroy(&key, column->idl->type.key_type);
1871 ovsdb_datum_to_string(&datum, &column->idl->type, out);
1873 ds_put_char(out, '\n');
1874 ovsdb_datum_destroy(&datum, &column->idl->type);
1881 list_record(const struct vsctl_table_class *table,
1882 const struct ovsdb_idl_row *row, struct ds *out)
1884 const struct vsctl_column *column;
1886 ds_put_format(out, "%-20s (RO): "UUID_FMT"\n", "_uuid",
1887 UUID_ARGS(&row->uuid));
1888 for (column = table->columns; column->idl; column++) {
1889 struct ovsdb_datum datum;
1891 if (column->flags & VSCF_HIDDEN) {
1895 ovsdb_idl_txn_read(row, column->idl, &datum);
1897 ds_put_format(out, "%-20s (%s): ", column->idl->name,
1898 column->flags & VSCF_READONLY ? "RO" : "RW");
1899 ovsdb_datum_to_string(&datum, &column->idl->type, out);
1900 ds_put_char(out, '\n');
1902 ovsdb_datum_destroy(&datum, &column->idl->type);
1907 cmd_list(struct vsctl_context *ctx)
1909 const char *table_name = ctx->argv[1];
1910 const struct vsctl_table_class *table;
1911 struct ds *out = &ctx->output;
1914 table = get_table(table_name);
1915 if (ctx->argc > 2) {
1916 for (i = 2; i < ctx->argc; i++) {
1918 ds_put_char(out, '\n');
1920 list_record(table, must_get_row(ctx, table, ctx->argv[i]), out);
1923 const struct ovsdb_idl_row *row;
1926 for (row = ovsdb_idl_first_row(ctx->idl, table->class), first = true;
1928 row = ovsdb_idl_next_row(row), first = false) {
1930 ds_put_char(out, '\n');
1932 list_record(table, row, out);
1938 check_string_constraint(const struct ovsdb_datum *datum,
1939 const char *constraint)
1946 regex = xasprintf("^%s$", constraint);
1947 retval = regcomp(&re, regex, REG_NOSUB | REG_EXTENDED);
1949 size_t length = regerror(retval, &re, NULL, 0);
1950 char *buffer = xmalloc(length);
1951 regerror(retval, &re, buffer, length);
1952 ovs_fatal(0, "internal error compiling regular expression %s: %s",
1956 for (i = 0; i < datum->n; i++) {
1957 const char *key = datum->keys[i].string;
1958 if (regexec(&re, key, 0, NULL, 0)) {
1959 ovs_fatal(0, "%s is not valid (it does not match %s)", key, regex);
1967 check_integer_constraint(const struct ovsdb_datum *datum,
1968 const char *constraint)
1974 sscanf(constraint, "[%"SCNd64",%"SCNd64"]%n", &min, &max, &n);
1976 sscanf(constraint, "[%"SCNd64",]%n", &min, &n);
1978 sscanf(constraint, "[,%"SCNd64"]%n", &max, &n);
1980 VLOG_DBG("internal error: bad integer contraint \"%s\"",
1991 for (i = 0; i < datum->n; i++) {
1992 int64_t value = datum->keys[i].integer;
1993 if (value < min || value > max) {
1994 if (max == INT64_MAX) {
1995 ovs_fatal(0, "%"PRId64" is less than the minimum "
1996 "allowed value %"PRId64, value, min);
1997 } else if (min == INT64_MIN) {
1998 ovs_fatal(0, "%"PRId64" is greater than the maximum "
1999 "allowed value %"PRId64, value, max);
2001 ovs_fatal(0, "%"PRId64" is outside the valid range %"PRId64" "
2002 "to %"PRId64" (inclusive)", value, min, max);
2009 check_constraint(const struct ovsdb_datum *datum,
2010 const struct ovsdb_type *type, const char *constraint)
2012 if (constraint && datum->n) {
2013 if (type->key_type == OVSDB_TYPE_STRING) {
2014 check_string_constraint(datum, constraint);
2015 } else if (type->key_type == OVSDB_TYPE_INTEGER) {
2016 check_integer_constraint(datum, constraint);
2022 set_column(const struct vsctl_table_class *table,
2023 const struct ovsdb_idl_row *row,
2024 const char *arg, bool force)
2026 const struct vsctl_column *column;
2027 char *key_string, *value_string;
2030 error = parse_column_key_value(arg, table, &column, &key_string,
2032 die_if_error(error);
2033 if (column->flags & VSCF_READONLY && !force) {
2034 ovs_fatal(0, "%s: cannot modify read-only column %s in table %s",
2035 arg, column->idl->name, table->class->name);
2037 if (!value_string) {
2038 ovs_fatal(0, "%s: missing value", arg);
2042 union ovsdb_atom key, value;
2043 struct ovsdb_datum old, new;
2045 if (column->idl->type.value_type == OVSDB_TYPE_VOID) {
2046 ovs_fatal(0, "cannot specify key to set for non-map column %s",
2050 die_if_error(ovsdb_atom_from_string(&key,
2051 column->idl->type.key_type,
2053 die_if_error(ovsdb_atom_from_string(&value,
2054 column->idl->type.value_type,
2057 ovsdb_datum_init_empty(&new);
2058 ovsdb_datum_add_unsafe(&new, &key, &value, &column->idl->type);
2060 ovsdb_idl_txn_read(row, column->idl, &old);
2061 ovsdb_datum_union(&old, &new, &column->idl->type, true);
2062 ovsdb_idl_txn_write(row, column->idl, &old);
2064 ovsdb_datum_destroy(&new, &column->idl->type);
2066 struct ovsdb_datum datum;
2068 die_if_error(ovsdb_datum_from_string(&datum, &column->idl->type,
2071 check_constraint(&datum, &column->idl->type,
2072 column->constraint);
2074 ovsdb_idl_txn_write(row, column->idl, &datum);
2081 cmd_set(struct vsctl_context *ctx)
2083 bool force = shash_find(&ctx->options, "--force");
2084 const char *table_name = ctx->argv[1];
2085 const char *record_id = ctx->argv[2];
2086 const struct vsctl_table_class *table;
2087 const struct ovsdb_idl_row *row;
2090 table = get_table(table_name);
2091 row = must_get_row(ctx, table, record_id);
2092 for (i = 3; i < ctx->argc; i++) {
2093 set_column(table, row, ctx->argv[i], force);
2098 cmd_add(struct vsctl_context *ctx)
2100 bool force = shash_find(&ctx->options, "--force");
2101 const char *table_name = ctx->argv[1];
2102 const char *record_id = ctx->argv[2];
2103 const char *column_name = ctx->argv[3];
2104 const struct vsctl_table_class *table;
2105 const struct vsctl_column *column;
2106 const struct ovsdb_idl_row *row;
2107 const struct ovsdb_type *type;
2108 struct ovsdb_datum old;
2111 table = get_table(table_name);
2112 row = must_get_row(ctx, table, record_id);
2113 die_if_error(get_column(table, column_name, &column));
2114 type = &column->idl->type;
2115 ovsdb_idl_txn_read(row, column->idl, &old);
2116 for (i = 4; i < ctx->argc; i++) {
2117 struct ovsdb_type add_type;
2118 struct ovsdb_datum add;
2120 if (column->flags & VSCF_READONLY && !force) {
2121 ovs_fatal(0, "%s: cannot modify read-only column %s in table %s",
2122 ctx->argv[i], column->idl->name, table_name);
2127 add_type.n_max = UINT_MAX;
2128 die_if_error(ovsdb_datum_from_string(&add, &add_type, ctx->argv[i]));
2129 ovsdb_datum_union(&old, &add, type, false);
2130 ovsdb_datum_destroy(&add, type);
2132 if (old.n > type->n_max) {
2133 ovs_fatal(0, "\"add\" operation would put %u %s in column %s of "
2134 "table %s but at most %u are allowed",
2136 type->value_type == OVSDB_TYPE_VOID ? "values" : "pairs",
2137 column->idl->name, table_name, type->n_max);
2139 ovsdb_idl_txn_write(row, column->idl, &old);
2143 cmd_remove(struct vsctl_context *ctx)
2145 bool force = shash_find(&ctx->options, "--force");
2146 const char *table_name = ctx->argv[1];
2147 const char *record_id = ctx->argv[2];
2148 const char *column_name = ctx->argv[3];
2149 const struct vsctl_table_class *table;
2150 const struct vsctl_column *column;
2151 const struct ovsdb_idl_row *row;
2152 const struct ovsdb_type *type;
2153 struct ovsdb_datum old;
2156 table = get_table(table_name);
2157 row = must_get_row(ctx, table, record_id);
2158 die_if_error(get_column(table, column_name, &column));
2159 type = &column->idl->type;
2160 ovsdb_idl_txn_read(row, column->idl, &old);
2161 for (i = 4; i < ctx->argc; i++) {
2162 struct ovsdb_type rm_type;
2163 struct ovsdb_datum rm;
2166 if (column->flags & VSCF_READONLY && !force) {
2167 ovs_fatal(0, "%s: cannot modify read-only column %s in table %s",
2168 ctx->argv[i], column->idl->name, table_name);
2173 rm_type.n_max = UINT_MAX;
2174 error = ovsdb_datum_from_string(&rm, &rm_type, ctx->argv[i]);
2175 if (error && ovsdb_type_is_map(&rm_type)) {
2177 rm_type.value_type = OVSDB_TYPE_VOID;
2178 die_if_error(ovsdb_datum_from_string(&rm, &rm_type, ctx->argv[i]));
2180 ovsdb_datum_subtract(&old, type, &rm, &rm_type);
2181 ovsdb_datum_destroy(&rm, &rm_type);
2183 if (old.n < type->n_min) {
2184 ovs_fatal(0, "\"remove\" operation would put %u %s in column %s of "
2185 "table %s but at least %u are required",
2187 type->value_type == OVSDB_TYPE_VOID ? "values" : "pairs",
2188 column->idl->name, table_name, type->n_min);
2190 ovsdb_idl_txn_write(row, column->idl, &old);
2194 cmd_clear(struct vsctl_context *ctx)
2196 bool force = shash_find(&ctx->options, "--force");
2197 const char *table_name = ctx->argv[1];
2198 const char *record_id = ctx->argv[2];
2199 const struct vsctl_table_class *table;
2200 const struct ovsdb_idl_row *row;
2203 table = get_table(table_name);
2204 row = must_get_row(ctx, table, record_id);
2205 for (i = 3; i < ctx->argc; i++) {
2206 const struct vsctl_column *column;
2207 const struct ovsdb_type *type;
2208 struct ovsdb_datum datum;
2210 die_if_error(get_column(table, ctx->argv[i], &column));
2212 type = &column->idl->type;
2213 if (column->flags & VSCF_READONLY && !force) {
2214 ovs_fatal(0, "%s: cannot modify read-only column %s in table %s",
2215 ctx->argv[i], column->idl->name, table_name);
2216 } else if (type->n_min > 0) {
2217 ovs_fatal(0, "\"clear\" operation cannot be applied to column %s "
2218 "of table %s, which is not allowed to be empty",
2219 column->idl->name, table_name);
2222 ovsdb_datum_init_empty(&datum);
2223 ovsdb_idl_txn_write(row, column->idl, &datum);
2228 cmd_create(struct vsctl_context *ctx)
2230 bool force = shash_find(&ctx->options, "--force");
2231 const char *table_name = ctx->argv[1];
2232 const struct vsctl_table_class *table;
2233 const struct ovsdb_idl_row *row;
2237 ovs_fatal(0, "\"create\" requires --force");
2240 table = get_table(table_name);
2241 row = ovsdb_idl_txn_insert(txn_from_openvswitch(ctx->ovs), table->class);
2242 for (i = 2; i < ctx->argc; i++) {
2243 set_column(table, row, ctx->argv[i], force);
2248 cmd_destroy(struct vsctl_context *ctx)
2250 bool force = shash_find(&ctx->options, "--force");
2251 bool must_exist = !shash_find(&ctx->options, "--if-exists");
2252 const char *table_name = ctx->argv[1];
2253 const struct vsctl_table_class *table;
2257 ovs_fatal(0, "\"destroy\" requires --force");
2260 table = get_table(table_name);
2261 for (i = 1; i < ctx->argc; i++) {
2262 const struct ovsdb_idl_row *row;
2264 row = (must_exist ? must_get_row : get_row)(ctx, table, ctx->argv[i]);
2266 ovsdb_idl_txn_delete(row);
2271 typedef void vsctl_handler_func(struct vsctl_context *);
2273 struct vsctl_command {
2277 vsctl_handler_func *handler;
2278 const char *options;
2281 static void run_vsctl_command(int argc, char *argv[],
2282 const struct ovsrec_open_vswitch *,
2283 struct ovsdb_idl *, struct ds *output);
2285 static struct json *
2286 where_uuid_equals(const struct uuid *uuid)
2289 json_array_create_1(
2290 json_array_create_3(
2291 json_string_create("_uuid"),
2292 json_string_create("=="),
2293 json_array_create_2(
2294 json_string_create("uuid"),
2295 json_string_create_nocopy(
2296 xasprintf(UUID_FMT, UUID_ARGS(uuid))))));
2300 do_vsctl(int argc, char *argv[], struct ovsdb_idl *idl)
2302 struct ovsdb_idl_txn *txn;
2303 const struct ovsrec_open_vswitch *ovs;
2304 enum ovsdb_idl_txn_status status;
2305 struct ds comment, *output;
2306 int64_t next_cfg = 0;
2310 txn = ovsdb_idl_txn_create(idl);
2312 ovsdb_idl_txn_set_dry_run(txn);
2316 ds_put_cstr(&comment, "ovs-vsctl:");
2317 for (i = 0; i < argc; i++) {
2318 ds_put_format(&comment, " %s", argv[i]);
2320 ovsdb_idl_txn_add_comment(txn, ds_cstr(&comment));
2321 ds_destroy(&comment);
2323 ovs = ovsrec_open_vswitch_first(idl);
2325 /* XXX add verification that table is empty */
2326 ovs = ovsrec_open_vswitch_insert(txn);
2329 if (wait_for_reload) {
2330 struct json *where = where_uuid_equals(&ovs->header_.uuid);
2331 ovsdb_idl_txn_increment(txn, "Open_vSwitch", "next_cfg", where);
2332 json_destroy(where);
2335 output = xmalloc(argc * sizeof *output);
2337 for (start = i = 0; i <= argc; i++) {
2338 if (i == argc || !strcmp(argv[i], "--")) {
2340 ds_init(&output[n_output]);
2341 run_vsctl_command(i - start, &argv[start], ovs, idl,
2342 &output[n_output++]);
2348 while ((status = ovsdb_idl_txn_commit(txn)) == TXN_INCOMPLETE) {
2350 ovsdb_idl_wait(idl);
2351 ovsdb_idl_txn_wait(txn);
2354 if (wait_for_reload && status == TXN_SUCCESS) {
2355 next_cfg = ovsdb_idl_txn_get_increment_new_value(txn);
2357 ovsdb_idl_txn_destroy(txn);
2360 case TXN_INCOMPLETE:
2364 /* Should not happen--we never call ovsdb_idl_txn_abort(). */
2365 vsctl_fatal("transaction aborted");
2372 for (i = 0; i < n_output; i++) {
2373 ds_destroy(&output[i]);
2378 vsctl_fatal("transaction error");
2384 for (i = 0; i < n_output; i++) {
2385 struct ds *ds = &output[i];
2390 for (j = 0; j < ds->length; j++) {
2391 int c = ds->string[j];
2394 fputs("\\n", stdout);
2398 fputs("\\\\", stdout);
2407 fputs(ds_cstr(ds), stdout);
2411 if (wait_for_reload && status != TXN_UNCHANGED) {
2413 const struct ovsrec_open_vswitch *ovs;
2416 OVSREC_OPEN_VSWITCH_FOR_EACH (ovs, idl) {
2417 if (ovs->cur_cfg >= next_cfg) {
2421 ovsdb_idl_wait(idl);
2430 static vsctl_handler_func *
2431 get_vsctl_handler(int argc, char *argv[], struct vsctl_context *ctx)
2433 static const struct vsctl_command all_commands[] = {
2434 /* Open vSwitch commands. */
2435 {"init", 0, 0, cmd_init, ""},
2437 /* Bridge commands. */
2438 {"add-br", 1, 3, cmd_add_br, ""},
2439 {"del-br", 1, 1, cmd_del_br, "--if-exists"},
2440 {"list-br", 0, 0, cmd_list_br, ""},
2441 {"br-exists", 1, 1, cmd_br_exists, ""},
2442 {"br-to-vlan", 1, 1, cmd_br_to_vlan, ""},
2443 {"br-to-parent", 1, 1, cmd_br_to_parent, ""},
2444 {"br-set-external-id", 2, 3, cmd_br_set_external_id, ""},
2445 {"br-get-external-id", 1, 2, cmd_br_get_external_id, ""},
2447 /* Port commands. */
2448 {"list-ports", 1, 1, cmd_list_ports, ""},
2449 {"add-port", 2, 2, cmd_add_port, ""},
2450 {"add-bond", 4, INT_MAX, cmd_add_bond, "--fake-iface"},
2451 {"del-port", 1, 2, cmd_del_port, "--if-exists"},
2452 {"port-to-br", 1, 1, cmd_port_to_br, ""},
2454 /* Interface commands. */
2455 {"list-ifaces", 1, 1, cmd_list_ifaces, ""},
2456 {"iface-to-br", 1, 1, cmd_iface_to_br, ""},
2458 /* Controller commands. */
2459 {"get-controller", 0, 1, cmd_get_controller, ""},
2460 {"del-controller", 0, 1, cmd_del_controller, ""},
2461 {"set-controller", 1, 2, cmd_set_controller, ""},
2462 {"get-fail-mode", 0, 1, cmd_get_fail_mode, ""},
2463 {"del-fail-mode", 0, 1, cmd_del_fail_mode, ""},
2464 {"set-fail-mode", 1, 2, cmd_set_fail_mode, ""},
2467 {"get-ssl", 0, 0, cmd_get_ssl, ""},
2468 {"del-ssl", 0, 0, cmd_del_ssl, ""},
2469 {"set-ssl", 3, 3, cmd_set_ssl, "--bootstrap"},
2471 /* Parameter commands. */
2472 {"get", 3, INT_MAX, cmd_get, ""},
2473 {"list", 1, INT_MAX, cmd_list, ""},
2474 {"set", 3, INT_MAX, cmd_set, "--force"},
2475 {"add", 4, INT_MAX, cmd_add, "--force"},
2476 {"remove", 4, INT_MAX, cmd_remove, "--force"},
2477 {"clear", 3, INT_MAX, cmd_clear, "--force"},
2478 {"create", 2, INT_MAX, cmd_create, "--force"},
2479 {"destroy", 1, INT_MAX, cmd_destroy, "--force,--if-exists"},
2482 const struct vsctl_command *p;
2485 shash_init(&ctx->options);
2486 for (i = 0; i < argc; i++) {
2487 if (argv[i][0] != '-') {
2490 if (!shash_add_once(&ctx->options, argv[i], NULL)) {
2491 vsctl_fatal("'%s' option specified multiple times", argv[i]);
2495 vsctl_fatal("missing command name");
2498 for (p = all_commands; p < &all_commands[ARRAY_SIZE(all_commands)]; p++) {
2499 if (!strcmp(p->name, argv[i])) {
2500 struct shash_node *node;
2503 SHASH_FOR_EACH (node, &ctx->options) {
2504 const char *s = strstr(p->options, node->name);
2505 int end = s ? s[strlen(node->name)] : EOF;
2506 if (end != ',' && end != ' ' && end != '\0') {
2507 vsctl_fatal("'%s' command has no '%s' option",
2508 argv[i], node->name);
2512 n_arg = argc - i - 1;
2513 if (n_arg < p->min_args) {
2514 vsctl_fatal("'%s' command requires at least %d arguments",
2515 p->name, p->min_args);
2516 } else if (n_arg > p->max_args) {
2517 vsctl_fatal("'%s' command takes at most %d arguments",
2518 p->name, p->max_args);
2520 ctx->argc = n_arg + 1;
2521 ctx->argv = &argv[i];
2527 vsctl_fatal("unknown command '%s'; use --help for help", argv[i]);
2531 check_vsctl_command(int argc, char *argv[])
2533 struct vsctl_context ctx;
2535 get_vsctl_handler(argc, argv, &ctx);
2536 shash_destroy(&ctx.options);
2540 run_vsctl_command(int argc, char *argv[],
2541 const struct ovsrec_open_vswitch *ovs,
2542 struct ovsdb_idl *idl, struct ds *output)
2544 vsctl_handler_func *function;
2545 struct vsctl_context ctx;
2547 function = get_vsctl_handler(argc, argv, &ctx);
2550 ds_init(&ctx.output);
2552 *output = ctx.output;
2553 shash_destroy(&ctx.options);