enum {
OPT_DB = UCHAR_MAX + 1,
OPT_ONELINE,
- OPT_NO_SYSLOG
+ OPT_NO_SYSLOG,
+ OPT_NO_WAIT
};
static struct option long_options[] = {
{"db", required_argument, 0, OPT_DB},
{"no-syslog", no_argument, 0, OPT_NO_SYSLOG},
+ {"no-wait", no_argument, 0, OPT_NO_WAIT},
{"oneline", no_argument, 0, OPT_ONELINE},
{"verbose", optional_argument, 0, 'v'},
{"help", no_argument, 0, 'h'},
vlog_set_levels(VLM_vsctl, VLF_SYSLOG, VLL_WARN);
break;
+ case OPT_NO_WAIT:
+ /* XXX not yet implemented */
+ break;
+
case 'h':
usage();
" br-to-vlan BRIDGE "
"print the VLAN which BRIDGE is on\n"
" br-to-parent BRIDGE "
- "print the parent of BRIDGE\n");
+ "print the parent of BRIDGE\n"
+ " br-set-external-id BRIDGE KEY VALUE"
+ " set KEY on BRIDGE to VALUE\n"
+ " br-set-external-id BRIDGE KEY"
+ " unset KEY on BRIDGE\n"
+ " br-get-external-id BRIDGE KEY"
+ " print value of KEY on BRIDGE\n"
+ " br-get-external-id BRIDGE"
+ " list key-value pairs on BRIDGE\n"
+ );
printf("\nPort commands:\n"
" list-ports BRIDGE "
"print the names of all the ports on BRIDGE\n"
"delete PORT (which may be bonded) from BRIDGE\n"
" port-to-br PORT "
"print name of bridge that contains PORT\n"
- "A bond is considered to be a single port.\n");
+ " port-set-external-id PORT KEY VALUE"
+ " set KEY on PORT to VALUE\n"
+ " port-set-external-id PORT KEY"
+ " unset KEY on PORT\n"
+ " port-get-external-id PORT KEY"
+ " print value of KEY on PORT\n"
+ " port-get-external-id PORT"
+ " list key-value pairs on PORT\n"
+ "A bond is considered to be a single port.\n"
+ );
printf("\nInterface commands (a bond consists of multiple interfaces):\n"
" list-ifaces BRIDGE "
"print the names of all the interfaces on BRIDGE\n"
" iface-to-br IFACE "
- "print name of bridge that contains IFACE\n");
+ "print name of bridge that contains IFACE\n"
+ " iface-set-external-id IFACE KEY VALUE"
+ " set KEY on IFACE to VALUE\n"
+ " iface-set-external-id IFACE KEY"
+ " unset KEY on IFACE\n"
+ " iface-get-external-id IFACE KEY"
+ " print value of KEY on IFACE\n"
+ " iface-get-external-id IFACE"
+ " list key-value pairs on IFACE\n"
+ );
printf("\nOptions:\n"
" --db=DATABASE "
"connect to DATABASE\n"
return def;
}
\f
+struct vsctl_context {
+ int argc;
+ char **argv;
+ const struct ovsrec_open_vswitch *ovs;
+ struct ds output;
+ struct shash options;
+};
+
struct vsctl_bridge {
struct ovsrec_bridge *br_cfg;
char *name;
}
static struct vsctl_bridge *
-find_bridge(struct vsctl_info *info, const char *name)
+find_bridge(struct vsctl_info *info, const char *name, bool must_exist)
{
struct vsctl_bridge *br = shash_find_data(&info->bridges, name);
- if (!br) {
+ if (must_exist && !br) {
ovs_fatal(0, "no bridge named %s", name);
}
return br;
}
static struct vsctl_port *
-find_port(struct vsctl_info *info, const char *name)
+find_port(struct vsctl_info *info, const char *name, bool must_exist)
{
struct vsctl_port *port = shash_find_data(&info->ports, name);
- if (!port || !strcmp(name, port->bridge->name)) {
+ if (port && !strcmp(name, port->bridge->name)) {
+ port = NULL;
+ }
+ if (must_exist && !port) {
ovs_fatal(0, "no port named %s", name);
}
return port;
}
static struct vsctl_iface *
-find_iface(struct vsctl_info *info, const char *name)
+find_iface(struct vsctl_info *info, const char *name, bool must_exist)
{
struct vsctl_iface *iface = shash_find_data(&info->ifaces, name);
- if (!iface || !strcmp(name, iface->port->bridge->name)) {
+ if (iface && !strcmp(name, iface->port->bridge->name)) {
+ iface = NULL;
+ }
+ if (must_exist && !iface) {
ovs_fatal(0, "no interface named %s", name);
}
return iface;
}
static void
-cmd_add_br(int argc, char *argv[], const struct ovsrec_open_vswitch *ovs,
- struct ds *output UNUSED)
+cmd_add_br(struct vsctl_context *ctx)
{
- const char *br_name = argv[1];
+ const char *br_name = ctx->argv[1];
struct vsctl_info info;
- get_info(ovs, &info);
+ get_info(ctx->ovs, &info);
check_conflicts(&info, br_name,
xasprintf("cannot create a bridge named %s", br_name));
- if (argc == 2) {
+ if (ctx->argc == 2) {
struct ovsrec_bridge *br;
struct ovsrec_port *port;
struct ovsrec_interface *iface;
- iface = ovsrec_interface_insert(txn_from_openvswitch(ovs));
+ iface = ovsrec_interface_insert(txn_from_openvswitch(ctx->ovs));
ovsrec_interface_set_name(iface, br_name);
- port = ovsrec_port_insert(txn_from_openvswitch(ovs));
+ port = ovsrec_port_insert(txn_from_openvswitch(ctx->ovs));
ovsrec_port_set_name(port, br_name);
ovsrec_port_set_interfaces(port, &iface, 1);
- br = ovsrec_bridge_insert(txn_from_openvswitch(ovs));
+ br = ovsrec_bridge_insert(txn_from_openvswitch(ctx->ovs));
ovsrec_bridge_set_name(br, br_name);
ovsrec_bridge_set_ports(br, &port, 1);
- ovs_insert_bridge(ovs, br);
- } else if (argc == 3) {
- ovs_fatal(0, "'%s' comamnd takes exactly 1 or 3 arguments", argv[0]);
- } else if (argc == 4) {
- const char *parent_name = argv[2];
- int vlan = atoi(argv[3]);
+ ovs_insert_bridge(ctx->ovs, br);
+ } else if (ctx->argc == 3) {
+ ovs_fatal(0, "'%s' comamnd takes exactly 1 or 3 arguments", ctx->argv[0]);
+ } else if (ctx->argc == 4) {
+ const char *parent_name = ctx->argv[2];
+ int vlan = atoi(ctx->argv[3]);
struct ovsrec_bridge *br;
struct vsctl_bridge *parent;
struct ovsrec_port *port;
int64_t tag = vlan;
if (vlan < 1 || vlan > 4095) {
- ovs_fatal(0, "%s: vlan must be between 1 and 4095", argv[0]);
+ ovs_fatal(0, "%s: vlan must be between 1 and 4095", ctx->argv[0]);
}
- parent = shash_find_data(&info.bridges, parent_name);
+ parent = find_bridge(&info, parent_name, false);
if (parent && parent->vlan) {
ovs_fatal(0, "cannot create brdige with fake bridge as parent");
}
}
br = parent->br_cfg;
- iface = ovsrec_interface_insert(txn_from_openvswitch(ovs));
+ iface = ovsrec_interface_insert(txn_from_openvswitch(ctx->ovs));
ovsrec_interface_set_name(iface, br_name);
ovsrec_interface_set_type(iface, "internal");
- port = ovsrec_port_insert(txn_from_openvswitch(ovs));
+ port = ovsrec_port_insert(txn_from_openvswitch(ctx->ovs));
ovsrec_port_set_name(port, br_name);
ovsrec_port_set_interfaces(port, &iface, 1);
ovsrec_port_set_fake_bridge(port, true);
}
static void
-cmd_del_br(int argc UNUSED, char *argv[],
- const struct ovsrec_open_vswitch *ovs, struct ds *output UNUSED)
+cmd_del_br(struct vsctl_context *ctx)
{
- struct shash_node *node;
- struct vsctl_info info;
+ bool must_exist = !shash_find(&ctx->options, "--if-exists");
struct vsctl_bridge *bridge;
+ struct vsctl_info info;
- get_info(ovs, &info);
- bridge = find_bridge(&info, argv[1]);
- SHASH_FOR_EACH (node, &info.ports) {
- struct vsctl_port *port = node->data;
- if (port->bridge == bridge
- || !strcmp(port->port_cfg->name, bridge->name)) {
- del_port(&info, port);
+ get_info(ctx->ovs, &info);
+ bridge = find_bridge(&info, ctx->argv[1], must_exist);
+ if (bridge) {
+ struct shash_node *node;
+
+ SHASH_FOR_EACH (node, &info.ports) {
+ struct vsctl_port *port = node->data;
+ if (port->bridge == bridge
+ || !strcmp(port->port_cfg->name, bridge->name)) {
+ del_port(&info, port);
+ }
+ }
+ if (bridge->br_cfg) {
+ ovsrec_bridge_delete(bridge->br_cfg);
+ ovs_delete_bridge(ctx->ovs, bridge->br_cfg);
}
- }
- if (bridge->br_cfg) {
- ovsrec_bridge_delete(bridge->br_cfg);
- ovs_delete_bridge(ovs, bridge->br_cfg);
}
free_info(&info);
}
}
static void
-cmd_list_br(int argc UNUSED, char *argv[] UNUSED,
- const struct ovsrec_open_vswitch *ovs, struct ds *output)
+cmd_list_br(struct vsctl_context *ctx)
{
struct shash_node *node;
struct vsctl_info info;
struct svec bridges;
- get_info(ovs, &info);
+ get_info(ctx->ovs, &info);
svec_init(&bridges);
SHASH_FOR_EACH (node, &info.bridges) {
struct vsctl_bridge *br = node->data;
svec_add(&bridges, br->name);
}
- output_sorted(&bridges, output);
+ output_sorted(&bridges, &ctx->output);
svec_destroy(&bridges);
free_info(&info);
}
static void
-cmd_br_exists(int argc UNUSED, char *argv[],
- const struct ovsrec_open_vswitch *ovs, struct ds *output UNUSED)
+cmd_br_exists(struct vsctl_context *ctx)
{
struct vsctl_info info;
- get_info(ovs, &info);
- if (!shash_find_data(&info.bridges, argv[1])) {
+ get_info(ctx->ovs, &info);
+ if (!find_bridge(&info, ctx->argv[1], false)) {
exit(2);
}
free_info(&info);
}
+/* Returns true if 'b_prefix' (of length 'b_prefix_len') concatenated with 'b'
+ * equals 'a', false otherwise. */
+static bool
+key_matches(const char *a,
+ const char *b_prefix, size_t b_prefix_len, const char *b)
+{
+ return !strncmp(a, b_prefix, b_prefix_len) && !strcmp(a + b_prefix_len, b);
+}
+
+static void
+set_external_id(char **old_keys, char **old_values, size_t old_n,
+ char *key, char *value,
+ char ***new_keysp, char ***new_valuesp, size_t *new_np)
+{
+ char **new_keys;
+ char **new_values;
+ size_t new_n;
+ size_t i;
+
+ new_keys = xmalloc(sizeof *new_keys * (old_n + 1));
+ new_values = xmalloc(sizeof *new_values * (old_n + 1));
+ new_n = 0;
+ for (i = 0; i < old_n; i++) {
+ if (strcmp(key, old_keys[i])) {
+ new_keys[new_n] = old_keys[i];
+ new_values[new_n] = old_values[i];
+ new_n++;
+ }
+ }
+ if (value) {
+ new_keys[new_n] = key;
+ new_values[new_n] = value;
+ new_n++;
+ }
+ *new_keysp = new_keys;
+ *new_valuesp = new_values;
+ *new_np = new_n;
+}
+
+static void
+cmd_br_set_external_id(struct vsctl_context *ctx)
+{
+ struct vsctl_info info;
+ struct vsctl_bridge *bridge;
+ char **keys, **values;
+ size_t n;
+
+ get_info(ctx->ovs, &info);
+ bridge = find_bridge(&info, ctx->argv[1], true);
+ if (bridge->br_cfg) {
+ set_external_id(bridge->br_cfg->key_external_ids,
+ bridge->br_cfg->value_external_ids,
+ bridge->br_cfg->n_external_ids,
+ ctx->argv[2], ctx->argc >= 4 ? ctx->argv[3] : NULL,
+ &keys, &values, &n);
+ ovsrec_bridge_set_external_ids(bridge->br_cfg, keys, values, n);
+ } else {
+ char *key = xasprintf("fake-bridge-%s", ctx->argv[2]);
+ struct vsctl_port *port = shash_find_data(&info.ports, ctx->argv[1]);
+ set_external_id(port->port_cfg->key_external_ids,
+ port->port_cfg->value_external_ids,
+ port->port_cfg->n_external_ids,
+ key, ctx->argc >= 4 ? ctx->argv[3] : NULL,
+ &keys, &values, &n);
+ ovsrec_port_set_external_ids(port->port_cfg, keys, values, n);
+ free(key);
+ }
+ free(keys);
+ free(values);
+
+ free_info(&info);
+}
+
static void
-cmd_list_ports(int argc UNUSED, char *argv[],
- const struct ovsrec_open_vswitch *ovs, struct ds *output)
+get_external_id(char **keys, char **values, size_t n,
+ const char *prefix, const char *key,
+ struct ds *output)
+{
+ size_t prefix_len = strlen(prefix);
+ struct svec svec;
+ size_t i;
+
+ svec_init(&svec);
+ for (i = 0; i < n; i++) {
+ if (!key && !strncmp(keys[i], prefix, prefix_len)) {
+ svec_add_nocopy(&svec, xasprintf("%s=%s",
+ keys[i] + prefix_len, values[i]));
+ } else if (key_matches(keys[i], prefix, prefix_len, key)) {
+ svec_add(&svec, values[i]);
+ break;
+ }
+ }
+ output_sorted(&svec, output);
+ svec_destroy(&svec);
+}
+
+static void
+cmd_br_get_external_id(struct vsctl_context *ctx)
+{
+ struct vsctl_info info;
+ struct vsctl_bridge *bridge;
+
+ get_info(ctx->ovs, &info);
+ bridge = find_bridge(&info, ctx->argv[1], true);
+ if (bridge->br_cfg) {
+ get_external_id(bridge->br_cfg->key_external_ids,
+ bridge->br_cfg->value_external_ids,
+ bridge->br_cfg->n_external_ids,
+ "", ctx->argc >= 3 ? ctx->argv[2] : NULL,
+ &ctx->output);
+ } else {
+ struct vsctl_port *port = shash_find_data(&info.ports, ctx->argv[1]);
+ get_external_id(port->port_cfg->key_external_ids,
+ port->port_cfg->value_external_ids,
+ port->port_cfg->n_external_ids,
+ "fake-bridge-", ctx->argc >= 3 ? ctx->argv[2] : NULL, &ctx->output);
+ }
+ free_info(&info);
+}
+
+
+static void
+cmd_list_ports(struct vsctl_context *ctx)
{
struct vsctl_bridge *br;
struct shash_node *node;
struct vsctl_info info;
struct svec ports;
- get_info(ovs, &info);
- br = find_bridge(&info, argv[1]);
+ get_info(ctx->ovs, &info);
+ br = find_bridge(&info, ctx->argv[1], true);
svec_init(&ports);
SHASH_FOR_EACH (node, &info.ports) {
svec_add(&ports, port->port_cfg->name);
}
}
- output_sorted(&ports, output);
+ output_sorted(&ports, &ctx->output);
svec_destroy(&ports);
free_info(&info);
check_conflicts(&info, port_name,
xasprintf("cannot create a port named %s", port_name));
/* XXX need to check for conflicts on interfaces too */
- bridge = find_bridge(&info, br_name);
+ bridge = find_bridge(&info, br_name, true);
ifaces = xmalloc(n_ifaces * sizeof *ifaces);
for (i = 0; i < n_ifaces; i++) {
port = ovsrec_port_insert(txn_from_openvswitch(ovs));
ovsrec_port_set_name(port, port_name);
ovsrec_port_set_interfaces(port, ifaces, n_ifaces);
+ free(ifaces);
+
if (bridge->vlan) {
int64_t tag = bridge->vlan;
ovsrec_port_set_tag(port, &tag, 1);
}
static void
-cmd_add_port(int argc UNUSED, char *argv[],
- const struct ovsrec_open_vswitch *ovs, struct ds *output UNUSED)
+cmd_add_port(struct vsctl_context *ctx)
{
- add_port(ovs, argv[1], argv[2], &argv[2], 1);
+ add_port(ctx->ovs, ctx->argv[1], ctx->argv[2], &ctx->argv[2], 1);
}
static void
-cmd_add_bond(int argc, char *argv[],
- const struct ovsrec_open_vswitch *ovs, struct ds *output UNUSED)
+cmd_add_bond(struct vsctl_context *ctx)
{
- add_port(ovs, argv[1], argv[2], &argv[3], argc - 3);
+ add_port(ctx->ovs, ctx->argv[1], ctx->argv[2], &ctx->argv[3], ctx->argc - 3);
}
static void
-cmd_del_port(int argc, char *argv[],
- const struct ovsrec_open_vswitch *ovs, struct ds *output UNUSED)
+cmd_del_port(struct vsctl_context *ctx)
{
+ bool must_exist = !shash_find(&ctx->options, "--if-exists");
struct vsctl_info info;
- get_info(ovs, &info);
- if (argc == 2) {
- struct vsctl_port *port = find_port(&info, argv[1]);
- del_port(&info, port);
- } else if (argc == 3) {
- struct vsctl_bridge *bridge = find_bridge(&info, argv[1]);
- struct vsctl_port *port = find_port(&info, argv[2]);
-
- if (port->bridge == bridge) {
+ get_info(ctx->ovs, &info);
+ if (ctx->argc == 2) {
+ struct vsctl_port *port = find_port(&info, ctx->argv[1], must_exist);
+ if (port) {
del_port(&info, port);
- } else if (port->bridge->parent == bridge) {
- ovs_fatal(0, "bridge %s does not have a port %s (although its "
- "parent bridge %s does)",
- argv[1], argv[2], bridge->parent->name);
- } else {
- ovs_fatal(0, "bridge %s does not have a port %s",
- argv[1], argv[2]);
+ }
+ } else if (ctx->argc == 3) {
+ struct vsctl_bridge *bridge = find_bridge(&info, ctx->argv[1], true);
+ struct vsctl_port *port = find_port(&info, ctx->argv[2], must_exist);
+
+ if (port) {
+ if (port->bridge == bridge) {
+ del_port(&info, port);
+ } else if (port->bridge->parent == bridge) {
+ ovs_fatal(0, "bridge %s does not have a port %s (although its "
+ "parent bridge %s does)",
+ ctx->argv[1], ctx->argv[2], bridge->parent->name);
+ } else {
+ ovs_fatal(0, "bridge %s does not have a port %s",
+ ctx->argv[1], ctx->argv[2]);
+ }
}
}
free_info(&info);
}
static void
-cmd_port_to_br(int argc UNUSED, char *argv[],
- const struct ovsrec_open_vswitch *ovs, struct ds *output)
+cmd_port_to_br(struct vsctl_context *ctx)
{
struct vsctl_port *port;
struct vsctl_info info;
- get_info(ovs, &info);
- port = find_port(&info, argv[1]);
- ds_put_format(output, "%s\n", port->bridge->name);
+ get_info(ctx->ovs, &info);
+ port = find_port(&info, ctx->argv[1], true);
+ ds_put_format(&ctx->output, "%s\n", port->bridge->name);
free_info(&info);
}
static void
-cmd_br_to_vlan(int argc UNUSED, char *argv[],
- const struct ovsrec_open_vswitch *ovs, struct ds *output)
+cmd_port_set_external_id(struct vsctl_context *ctx)
+{
+ struct vsctl_info info;
+ struct vsctl_port *port;
+ char **keys, **values;
+ size_t n;
+
+ get_info(ctx->ovs, &info);
+ port = find_port(&info, ctx->argv[1], true);
+ set_external_id(port->port_cfg->key_external_ids,
+ port->port_cfg->value_external_ids,
+ port->port_cfg->n_external_ids,
+ ctx->argv[2], ctx->argc >= 4 ? ctx->argv[3] : NULL,
+ &keys, &values, &n);
+ ovsrec_port_set_external_ids(port->port_cfg, keys, values, n);
+ free(keys);
+ free(values);
+
+ free_info(&info);
+}
+
+static void
+cmd_port_get_external_id(struct vsctl_context *ctx)
+{
+ struct vsctl_info info;
+ struct vsctl_port *port;
+
+ get_info(ctx->ovs, &info);
+ port = find_port(&info, ctx->argv[1], true);
+ get_external_id(port->port_cfg->key_external_ids,
+ port->port_cfg->value_external_ids,
+ port->port_cfg->n_external_ids,
+ "", ctx->argc >= 3 ? ctx->argv[2] : NULL, &ctx->output);
+ free_info(&info);
+}
+
+static void
+cmd_br_to_vlan(struct vsctl_context *ctx)
{
struct vsctl_bridge *bridge;
struct vsctl_info info;
- get_info(ovs, &info);
- bridge = find_bridge(&info, argv[1]);
- ds_put_format(output, "%d\n", bridge->vlan);
+ get_info(ctx->ovs, &info);
+ bridge = find_bridge(&info, ctx->argv[1], true);
+ ds_put_format(&ctx->output, "%d\n", bridge->vlan);
free_info(&info);
}
static void
-cmd_br_to_parent(int argc UNUSED, char *argv[],
- const struct ovsrec_open_vswitch *ovs, struct ds *output)
+cmd_br_to_parent(struct vsctl_context *ctx)
{
struct vsctl_bridge *bridge;
struct vsctl_info info;
- get_info(ovs, &info);
- bridge = find_bridge(&info, argv[1]);
+ get_info(ctx->ovs, &info);
+ bridge = find_bridge(&info, ctx->argv[1], true);
if (bridge->parent) {
bridge = bridge->parent;
}
- ds_put_format(output, "%s\n", bridge->name);
+ ds_put_format(&ctx->output, "%s\n", bridge->name);
free_info(&info);
}
static void
-cmd_list_ifaces(int argc UNUSED, char *argv[],
- const struct ovsrec_open_vswitch *ovs, struct ds *output)
+cmd_list_ifaces(struct vsctl_context *ctx)
{
struct vsctl_bridge *br;
struct shash_node *node;
struct vsctl_info info;
struct svec ifaces;
- get_info(ovs, &info);
- br = find_bridge(&info, argv[1]);
+ get_info(ctx->ovs, &info);
+ br = find_bridge(&info, ctx->argv[1], true);
svec_init(&ifaces);
SHASH_FOR_EACH (node, &info.ifaces) {
svec_add(&ifaces, iface->iface_cfg->name);
}
}
- output_sorted(&ifaces, output);
+ output_sorted(&ifaces, &ctx->output);
svec_destroy(&ifaces);
free_info(&info);
}
static void
-cmd_iface_to_br(int argc UNUSED, char *argv[],
- const struct ovsrec_open_vswitch *ovs, struct ds *output)
+cmd_iface_to_br(struct vsctl_context *ctx)
{
struct vsctl_iface *iface;
struct vsctl_info info;
- get_info(ovs, &info);
- iface = find_iface(&info, argv[1]);
- ds_put_format(output, "%s\n", iface->port->bridge->name);
+ get_info(ctx->ovs, &info);
+ iface = find_iface(&info, ctx->argv[1], true);
+ ds_put_format(&ctx->output, "%s\n", iface->port->bridge->name);
+ free_info(&info);
+}
+
+static void
+cmd_iface_set_external_id(struct vsctl_context *ctx)
+{
+ struct vsctl_info info;
+ struct vsctl_iface *iface;
+ char **keys, **values;
+ size_t n;
+
+ get_info(ctx->ovs, &info);
+ iface = find_iface(&info, ctx->argv[1], true);
+ set_external_id(iface->iface_cfg->key_external_ids,
+ iface->iface_cfg->value_external_ids,
+ iface->iface_cfg->n_external_ids,
+ ctx->argv[2], ctx->argc >= 4 ? ctx->argv[3] : NULL,
+ &keys, &values, &n);
+ ovsrec_interface_set_external_ids(iface->iface_cfg, keys, values, n);
+ free(keys);
+ free(values);
+
+ free_info(&info);
+}
+
+static void
+cmd_iface_get_external_id(struct vsctl_context *ctx)
+{
+ struct vsctl_info info;
+ struct vsctl_iface *iface;
+
+ get_info(ctx->ovs, &info);
+ iface = find_iface(&info, ctx->argv[1], true);
+ get_external_id(iface->iface_cfg->key_external_ids,
+ iface->iface_cfg->value_external_ids,
+ iface->iface_cfg->n_external_ids,
+ "", ctx->argc >= 3 ? ctx->argv[2] : NULL, &ctx->output);
free_info(&info);
}
\f
-typedef void vsctl_handler_func(int argc, char *argv[],
- const struct ovsrec_open_vswitch *,
- struct ds *output);
+typedef void vsctl_handler_func(struct vsctl_context *);
struct vsctl_command {
const char *name;
int min_args;
int max_args;
vsctl_handler_func *handler;
+ const char *options;
};
static void run_vsctl_command(int argc, char *argv[],
while ((status = ovsdb_idl_txn_commit(txn)) == TXN_INCOMPLETE) {
ovsdb_idl_run(idl);
ovsdb_idl_wait(idl);
+ ovsdb_idl_txn_wait(txn);
poll_block();
}
ovsdb_idl_txn_destroy(txn);
}
static vsctl_handler_func *
-get_vsctl_handler(int argc, char *argv[])
+get_vsctl_handler(int argc, char *argv[], struct vsctl_context *ctx)
{
static const struct vsctl_command all_commands[] = {
- {"add-br", 1, 3, cmd_add_br},
- {"del-br", 1, 1, cmd_del_br},
- {"list-br", 0, 0, cmd_list_br},
- {"br-exists", 1, 1, cmd_br_exists},
- {"list-ports", 1, 1, cmd_list_ports},
- {"add-port", 2, 2, cmd_add_port},
- {"add-bond", 4, INT_MAX, cmd_add_bond},
- {"del-port", 1, 2, cmd_del_port},
- {"port-to-br", 1, 1, cmd_port_to_br},
- {"br-to-vlan", 1, 1, cmd_br_to_vlan},
- {"br-to-parent", 1, 1, cmd_br_to_parent},
- {"list-ifaces", 1, 1, cmd_list_ifaces},
- {"iface-to-br", 1, 1, cmd_iface_to_br},
+ /* Bridge commands. */
+ {"add-br", 1, 3, cmd_add_br, ""},
+ {"del-br", 1, 1, cmd_del_br, "--if-exists"},
+ {"list-br", 0, 0, cmd_list_br, ""},
+ {"br-exists", 1, 1, cmd_br_exists, ""},
+ {"br-to-vlan", 1, 1, cmd_br_to_vlan, ""},
+ {"br-to-parent", 1, 1, cmd_br_to_parent, ""},
+ {"br-set-external-id", 2, 3, cmd_br_set_external_id, ""},
+ {"br-get-external-id", 1, 2, cmd_br_get_external_id, ""},
+
+ /* Port commands. */
+ {"list-ports", 1, 1, cmd_list_ports, ""},
+ {"add-port", 2, 2, cmd_add_port, ""},
+ {"add-bond", 4, INT_MAX, cmd_add_bond, ""},
+ {"del-port", 1, 2, cmd_del_port, "--if-exists"},
+ {"port-to-br", 1, 1, cmd_port_to_br, ""},
+ {"port-set-external-id", 2, 3, cmd_port_set_external_id, ""},
+ {"port-get-external-id", 1, 2, cmd_port_get_external_id, ""},
+
+ /* Interface commands. */
+ {"list-ifaces", 1, 1, cmd_list_ifaces, ""},
+ {"iface-to-br", 1, 1, cmd_iface_to_br, ""},
+ {"iface-set-external-id", 2, 3, cmd_iface_set_external_id, ""},
+ {"iface-get-external-id", 1, 2, cmd_iface_get_external_id, ""},
};
const struct vsctl_command *p;
+ int i;
+
+ shash_init(&ctx->options);
+ for (i = 0; i < argc; i++) {
+ if (argv[i][0] != '-') {
+ break;
+ }
+ if (!shash_add_once(&ctx->options, argv[i], NULL)) {
+ ovs_fatal(0, "'%s' option specified multiple times", argv[i]);
+ }
+ }
+ if (i == argc) {
+ ovs_fatal(0, "missing command name");
+ }
- assert(argc > 0);
for (p = all_commands; p < &all_commands[ARRAY_SIZE(all_commands)]; p++) {
- if (!strcmp(p->name, argv[0])) {
- int n_arg = argc - 1;
+ if (!strcmp(p->name, argv[i])) {
+ struct shash_node *node;
+ int n_arg;
+
+ SHASH_FOR_EACH (node, &ctx->options) {
+ const char *s = strstr(p->options, node->name);
+ int end = s ? s[strlen(node->name)] : EOF;
+ if (end != ',' && end != ' ' && end != '\0') {
+ ovs_fatal(0, "'%s' command has no '%s' option",
+ argv[i], node->name);
+ }
+ }
+
+ n_arg = argc - i - 1;
if (n_arg < p->min_args) {
ovs_fatal(0, "'%s' command requires at least %d arguments",
p->name, p->min_args);
ovs_fatal(0, "'%s' command takes at most %d arguments",
p->name, p->max_args);
} else {
+ ctx->argc = n_arg + 1;
+ ctx->argv = &argv[i];
return p->handler;
}
}
static void
check_vsctl_command(int argc, char *argv[])
{
- get_vsctl_handler(argc, argv);
+ struct vsctl_context ctx;
+
+ get_vsctl_handler(argc, argv, &ctx);
+ shash_destroy(&ctx.options);
}
static void
run_vsctl_command(int argc, char *argv[],
const struct ovsrec_open_vswitch *ovs, struct ds *output)
{
- get_vsctl_handler(argc, argv)(argc, argv, ovs, output);
+ vsctl_handler_func *function;
+ struct vsctl_context ctx;
+
+ function = get_vsctl_handler(argc, argv, &ctx);
+ ctx.ovs = ovs;
+ ds_init(&ctx.output);
+ function(&ctx);
+ *output = ctx.output;
+ shash_destroy(&ctx.options);
}