+ vsctl_context_populate_cache(ctx);
+ if (!find_bridge(ctx, ctx->argv[1], false)) {
+ vsctl_exit(2);
+ }
+}
+
+static void
+set_external_id(struct smap *old, struct smap *new,
+ char *key, char *value)
+{
+ smap_clone(new, old);
+
+ if (value) {
+ smap_replace(new, key, value);
+ } else {
+ smap_remove(new, key);
+ }
+}
+
+static void
+pre_cmd_br_set_external_id(struct vsctl_context *ctx)
+{
+ pre_get_info(ctx);
+ ovsdb_idl_add_column(ctx->idl, &ovsrec_bridge_col_external_ids);
+ ovsdb_idl_add_column(ctx->idl, &ovsrec_port_col_external_ids);
+}
+
+static void
+cmd_br_set_external_id(struct vsctl_context *ctx)
+{
+ struct vsctl_bridge *bridge;
+ struct smap new;
+
+ vsctl_context_populate_cache(ctx);
+ bridge = find_bridge(ctx, ctx->argv[1], true);
+ if (bridge->br_cfg) {
+
+ set_external_id(&bridge->br_cfg->external_ids, &new, ctx->argv[2],
+ ctx->argc >= 4 ? ctx->argv[3] : NULL);
+ ovsrec_bridge_verify_external_ids(bridge->br_cfg);
+ ovsrec_bridge_set_external_ids(bridge->br_cfg, &new);
+ } else {
+ char *key = xasprintf("fake-bridge-%s", ctx->argv[2]);
+ struct vsctl_port *port = shash_find_data(&ctx->ports, ctx->argv[1]);
+ set_external_id(&port->port_cfg->external_ids, &new,
+ key, ctx->argc >= 4 ? ctx->argv[3] : NULL);
+ ovsrec_port_verify_external_ids(port->port_cfg);
+ ovsrec_port_set_external_ids(port->port_cfg, &new);
+ free(key);
+ }
+ smap_destroy(&new);
+}
+
+static void
+get_external_id(struct smap *smap, const char *prefix, const char *key,
+ struct ds *output)
+{
+ if (key) {
+ char *prefix_key = xasprintf("%s%s", prefix, key);
+ const char *value = smap_get(smap, prefix_key);
+
+ if (value) {
+ ds_put_format(output, "%s\n", value);
+ }
+ free(prefix_key);
+ } else {
+ const struct smap_node **sorted = smap_sort(smap);
+ size_t prefix_len = strlen(prefix);
+ size_t i;
+
+ for (i = 0; i < smap_count(smap); i++) {
+ const struct smap_node *node = sorted[i];
+ if (!strncmp(node->key, prefix, prefix_len)) {
+ ds_put_format(output, "%s=%s\n", node->key + prefix_len,
+ node->value);
+ }
+ }
+ free(sorted);
+ }
+}
+
+static void
+pre_cmd_br_get_external_id(struct vsctl_context *ctx)
+{
+ pre_cmd_br_set_external_id(ctx);
+}
+
+static void
+cmd_br_get_external_id(struct vsctl_context *ctx)
+{
+ struct vsctl_bridge *bridge;
+
+ vsctl_context_populate_cache(ctx);
+
+ bridge = find_bridge(ctx, ctx->argv[1], true);
+ if (bridge->br_cfg) {
+ ovsrec_bridge_verify_external_ids(bridge->br_cfg);
+ get_external_id(&bridge->br_cfg->external_ids, "",
+ ctx->argc >= 3 ? ctx->argv[2] : NULL, &ctx->output);
+ } else {
+ struct vsctl_port *port = shash_find_data(&ctx->ports, ctx->argv[1]);
+ ovsrec_port_verify_external_ids(port->port_cfg);
+ get_external_id(&port->port_cfg->external_ids, "fake-bridge-",
+ ctx->argc >= 3 ? ctx->argv[2] : NULL, &ctx->output);
+ }
+}
+
+static void
+cmd_list_ports(struct vsctl_context *ctx)
+{
+ struct vsctl_bridge *br;
+ struct vsctl_port *port;
+ struct svec ports;
+
+ vsctl_context_populate_cache(ctx);
+ br = find_bridge(ctx, ctx->argv[1], true);
+ ovsrec_bridge_verify_ports(br->br_cfg ? br->br_cfg : br->parent->br_cfg);
+
+ svec_init(&ports);
+ LIST_FOR_EACH (port, ports_node, &br->ports) {
+ if (strcmp(port->port_cfg->name, br->name)) {
+ svec_add(&ports, port->port_cfg->name);
+ }
+ }
+ output_sorted(&ports, &ctx->output);
+ svec_destroy(&ports);
+}
+
+static void
+add_port(struct vsctl_context *ctx,
+ const char *br_name, const char *port_name,
+ bool may_exist, bool fake_iface,
+ char *iface_names[], int n_ifaces,
+ char *settings[], int n_settings)
+{
+ struct vsctl_port *vsctl_port;
+ struct vsctl_bridge *bridge;
+ struct ovsrec_interface **ifaces;
+ struct ovsrec_port *port;
+ size_t i;
+
+ vsctl_context_populate_cache(ctx);
+ if (may_exist) {
+ struct vsctl_port *vsctl_port;
+
+ vsctl_port = find_port(ctx, port_name, false);
+ if (vsctl_port) {
+ struct svec want_names, have_names;
+
+ svec_init(&want_names);
+ for (i = 0; i < n_ifaces; i++) {
+ svec_add(&want_names, iface_names[i]);
+ }
+ svec_sort(&want_names);
+
+ svec_init(&have_names);
+ for (i = 0; i < vsctl_port->port_cfg->n_interfaces; i++) {
+ svec_add(&have_names,
+ vsctl_port->port_cfg->interfaces[i]->name);
+ }
+ svec_sort(&have_names);
+
+ if (strcmp(vsctl_port->bridge->name, br_name)) {
+ char *command = vsctl_context_to_string(ctx);
+ vsctl_fatal("\"%s\" but %s is actually attached to bridge %s",
+ command, port_name, vsctl_port->bridge->name);
+ }
+
+ if (!svec_equal(&want_names, &have_names)) {
+ char *have_names_string = svec_join(&have_names, ", ", "");
+ char *command = vsctl_context_to_string(ctx);
+
+ vsctl_fatal("\"%s\" but %s actually has interface(s) %s",
+ command, port_name, have_names_string);
+ }
+
+ svec_destroy(&want_names);
+ svec_destroy(&have_names);
+
+ return;
+ }
+ }
+ check_conflicts(ctx, port_name,
+ xasprintf("cannot create a port named %s", port_name));
+ for (i = 0; i < n_ifaces; i++) {
+ check_conflicts(ctx, iface_names[i],
+ xasprintf("cannot create an interface named %s",
+ iface_names[i]));
+ }
+ bridge = find_bridge(ctx, br_name, true);
+
+ ifaces = xmalloc(n_ifaces * sizeof *ifaces);
+ for (i = 0; i < n_ifaces; i++) {
+ ifaces[i] = ovsrec_interface_insert(ctx->txn);
+ ovsrec_interface_set_name(ifaces[i], iface_names[i]);
+ }
+
+ port = ovsrec_port_insert(ctx->txn);
+ ovsrec_port_set_name(port, port_name);
+ ovsrec_port_set_interfaces(port, ifaces, n_ifaces);
+ ovsrec_port_set_bond_fake_iface(port, fake_iface);
+
+ if (bridge->parent) {
+ int64_t tag = bridge->vlan;
+ ovsrec_port_set_tag(port, &tag, 1);
+ }
+
+ for (i = 0; i < n_settings; i++) {
+ set_column(get_table("Port"), &port->header_, settings[i],
+ ctx->symtab);
+ }
+
+ bridge_insert_port((bridge->parent ? bridge->parent->br_cfg
+ : bridge->br_cfg), port);
+
+ vsctl_port = add_port_to_cache(ctx, bridge, port);
+ for (i = 0; i < n_ifaces; i++) {
+ add_iface_to_cache(ctx, vsctl_port, ifaces[i]);
+ }
+ free(ifaces);
+}
+
+static void
+cmd_add_port(struct vsctl_context *ctx)
+{
+ bool may_exist = shash_find(&ctx->options, "--may-exist") != NULL;
+
+ add_port(ctx, ctx->argv[1], ctx->argv[2], may_exist, false,
+ &ctx->argv[2], 1, &ctx->argv[3], ctx->argc - 3);
+}
+
+static void
+cmd_add_bond(struct vsctl_context *ctx)
+{
+ bool may_exist = shash_find(&ctx->options, "--may-exist") != NULL;
+ bool fake_iface = shash_find(&ctx->options, "--fake-iface");
+ int n_ifaces;
+ int i;
+
+ n_ifaces = ctx->argc - 3;
+ for (i = 3; i < ctx->argc; i++) {
+ if (strchr(ctx->argv[i], '=')) {
+ n_ifaces = i - 3;
+ break;
+ }
+ }
+ if (n_ifaces < 2) {
+ vsctl_fatal("add-bond requires at least 2 interfaces, but only "
+ "%d were specified", n_ifaces);
+ }
+
+ add_port(ctx, ctx->argv[1], ctx->argv[2], may_exist, fake_iface,
+ &ctx->argv[3], n_ifaces,
+ &ctx->argv[n_ifaces + 3], ctx->argc - 3 - n_ifaces);
+}
+
+static void
+cmd_del_port(struct vsctl_context *ctx)
+{
+ bool must_exist = !shash_find(&ctx->options, "--if-exists");
+ bool with_iface = shash_find(&ctx->options, "--with-iface") != NULL;
+ const char *target = ctx->argv[ctx->argc - 1];
+ struct vsctl_port *port;
+
+ vsctl_context_populate_cache(ctx);
+ if (find_bridge(ctx, target, false)) {
+ if (must_exist) {
+ vsctl_fatal("cannot delete port %s because it is the local port "
+ "for bridge %s (deleting this port requires deleting "
+ "the entire bridge)", target, target);
+ }
+ port = NULL;
+ } else if (!with_iface) {
+ port = find_port(ctx, target, must_exist);
+ } else {
+ struct vsctl_iface *iface;
+
+ port = find_port(ctx, target, false);
+ if (!port) {
+ iface = find_iface(ctx, target, false);
+ if (iface) {
+ port = iface->port;
+ }
+ }
+ if (must_exist && !port) {
+ vsctl_fatal("no port or interface named %s", target);
+ }
+ }
+
+ if (port) {
+ if (ctx->argc == 3) {
+ struct vsctl_bridge *bridge;
+
+ bridge = find_bridge(ctx, ctx->argv[1], true);
+ if (port->bridge != bridge) {
+ if (port->bridge->parent == bridge) {
+ vsctl_fatal("bridge %s does not have a port %s (although "
+ "its parent bridge %s does)",
+ ctx->argv[1], ctx->argv[2],
+ bridge->parent->name);
+ } else {
+ vsctl_fatal("bridge %s does not have a port %s",
+ ctx->argv[1], ctx->argv[2]);
+ }
+ }
+ }
+
+ del_port(ctx, port);
+ }
+}
+
+static void
+cmd_port_to_br(struct vsctl_context *ctx)
+{
+ struct vsctl_port *port;
+
+ vsctl_context_populate_cache(ctx);
+
+ port = find_port(ctx, ctx->argv[1], true);
+ ds_put_format(&ctx->output, "%s\n", port->bridge->name);
+}
+
+static void
+cmd_br_to_vlan(struct vsctl_context *ctx)
+{
+ struct vsctl_bridge *bridge;
+
+ vsctl_context_populate_cache(ctx);
+
+ bridge = find_bridge(ctx, ctx->argv[1], true);
+ ds_put_format(&ctx->output, "%d\n", bridge->vlan);
+}
+
+static void
+cmd_br_to_parent(struct vsctl_context *ctx)
+{
+ struct vsctl_bridge *bridge;
+
+ vsctl_context_populate_cache(ctx);
+
+ bridge = find_bridge(ctx, ctx->argv[1], true);
+ if (bridge->parent) {
+ bridge = bridge->parent;
+ }
+ ds_put_format(&ctx->output, "%s\n", bridge->name);
+}
+
+static void
+cmd_list_ifaces(struct vsctl_context *ctx)
+{
+ struct vsctl_bridge *br;
+ struct vsctl_port *port;
+ struct svec ifaces;
+
+ vsctl_context_populate_cache(ctx);
+
+ br = find_bridge(ctx, ctx->argv[1], true);
+ verify_ports(ctx);
+
+ svec_init(&ifaces);
+ LIST_FOR_EACH (port, ports_node, &br->ports) {
+ struct vsctl_iface *iface;
+
+ LIST_FOR_EACH (iface, ifaces_node, &port->ifaces) {
+ if (strcmp(iface->iface_cfg->name, br->name)) {
+ svec_add(&ifaces, iface->iface_cfg->name);
+ }
+ }
+ }
+ output_sorted(&ifaces, &ctx->output);
+ svec_destroy(&ifaces);
+}
+
+static void
+cmd_iface_to_br(struct vsctl_context *ctx)
+{
+ struct vsctl_iface *iface;
+
+ vsctl_context_populate_cache(ctx);
+
+ iface = find_iface(ctx, ctx->argv[1], true);
+ ds_put_format(&ctx->output, "%s\n", iface->port->bridge->name);
+}
+
+static void
+verify_controllers(struct ovsrec_bridge *bridge)
+{
+ size_t i;
+
+ ovsrec_bridge_verify_controller(bridge);
+ for (i = 0; i < bridge->n_controller; i++) {
+ ovsrec_controller_verify_target(bridge->controller[i]);
+ }
+}
+
+static void
+pre_controller(struct vsctl_context *ctx)
+{
+ pre_get_info(ctx);
+
+ ovsdb_idl_add_column(ctx->idl, &ovsrec_controller_col_target);
+}
+
+static void
+cmd_get_controller(struct vsctl_context *ctx)
+{
+ struct vsctl_bridge *br;
+ struct svec targets;
+ size_t i;
+
+ vsctl_context_populate_cache(ctx);
+
+ br = find_bridge(ctx, ctx->argv[1], true);
+ if (br->parent) {
+ br = br->parent;
+ }
+ verify_controllers(br->br_cfg);
+
+ /* Print the targets in sorted order for reproducibility. */
+ svec_init(&targets);
+ for (i = 0; i < br->br_cfg->n_controller; i++) {
+ svec_add(&targets, br->br_cfg->controller[i]->target);
+ }
+
+ svec_sort(&targets);
+ for (i = 0; i < targets.n; i++) {
+ ds_put_format(&ctx->output, "%s\n", targets.names[i]);
+ }
+ svec_destroy(&targets);
+}
+
+static void
+delete_controllers(struct ovsrec_controller **controllers,
+ size_t n_controllers)
+{
+ size_t i;
+
+ for (i = 0; i < n_controllers; i++) {
+ ovsrec_controller_delete(controllers[i]);
+ }
+}
+
+static void
+cmd_del_controller(struct vsctl_context *ctx)
+{
+ struct ovsrec_bridge *br;
+
+ vsctl_context_populate_cache(ctx);
+
+ br = find_real_bridge(ctx, ctx->argv[1], true)->br_cfg;
+ verify_controllers(br);
+
+ if (br->controller) {
+ delete_controllers(br->controller, br->n_controller);
+ ovsrec_bridge_set_controller(br, NULL, 0);
+ }
+}
+
+static struct ovsrec_controller **
+insert_controllers(struct ovsdb_idl_txn *txn, char *targets[], size_t n)
+{
+ struct ovsrec_controller **controllers;
+ size_t i;
+
+ controllers = xmalloc(n * sizeof *controllers);
+ for (i = 0; i < n; i++) {
+ if (vconn_verify_name(targets[i]) && pvconn_verify_name(targets[i])) {
+ VLOG_WARN("target type \"%s\" is possibly erroneous", targets[i]);
+ }
+ controllers[i] = ovsrec_controller_insert(txn);
+ ovsrec_controller_set_target(controllers[i], targets[i]);
+ }
+
+ return controllers;
+}
+
+static void
+cmd_set_controller(struct vsctl_context *ctx)
+{
+ struct ovsrec_controller **controllers;
+ struct ovsrec_bridge *br;
+ size_t n;
+
+ vsctl_context_populate_cache(ctx);
+
+ br = find_real_bridge(ctx, ctx->argv[1], true)->br_cfg;
+ verify_controllers(br);
+
+ delete_controllers(br->controller, br->n_controller);
+
+ n = ctx->argc - 2;
+ controllers = insert_controllers(ctx->txn, &ctx->argv[2], n);
+ ovsrec_bridge_set_controller(br, controllers, n);
+ free(controllers);
+}
+
+static void
+cmd_get_fail_mode(struct vsctl_context *ctx)
+{
+ struct vsctl_bridge *br;
+ const char *fail_mode;
+
+ vsctl_context_populate_cache(ctx);
+ br = find_bridge(ctx, ctx->argv[1], true);
+
+ if (br->parent) {
+ br = br->parent;
+ }
+ ovsrec_bridge_verify_fail_mode(br->br_cfg);
+
+ fail_mode = br->br_cfg->fail_mode;
+ if (fail_mode && strlen(fail_mode)) {
+ ds_put_format(&ctx->output, "%s\n", fail_mode);
+ }
+}
+
+static void
+cmd_del_fail_mode(struct vsctl_context *ctx)
+{
+ struct vsctl_bridge *br;
+
+ vsctl_context_populate_cache(ctx);
+
+ br = find_real_bridge(ctx, ctx->argv[1], true);
+
+ ovsrec_bridge_set_fail_mode(br->br_cfg, NULL);
+}
+
+static void
+cmd_set_fail_mode(struct vsctl_context *ctx)
+{
+ struct vsctl_bridge *br;
+ const char *fail_mode = ctx->argv[2];
+
+ vsctl_context_populate_cache(ctx);
+
+ br = find_real_bridge(ctx, ctx->argv[1], true);
+
+ if (strcmp(fail_mode, "standalone") && strcmp(fail_mode, "secure")) {
+ vsctl_fatal("fail-mode must be \"standalone\" or \"secure\"");
+ }
+
+ ovsrec_bridge_set_fail_mode(br->br_cfg, fail_mode);
+}
+
+static void
+verify_managers(const struct ovsrec_open_vswitch *ovs)
+{
+ size_t i;
+
+ ovsrec_open_vswitch_verify_manager_options(ovs);
+
+ for (i = 0; i < ovs->n_manager_options; ++i) {
+ const struct ovsrec_manager *mgr = ovs->manager_options[i];
+
+ ovsrec_manager_verify_target(mgr);
+ }
+}
+
+static void
+pre_manager(struct vsctl_context *ctx)
+{
+ ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_manager_options);
+ ovsdb_idl_add_column(ctx->idl, &ovsrec_manager_col_target);
+}
+
+static void
+cmd_get_manager(struct vsctl_context *ctx)
+{
+ const struct ovsrec_open_vswitch *ovs = ctx->ovs;
+ struct svec targets;
+ size_t i;
+
+ verify_managers(ovs);
+
+ /* Print the targets in sorted order for reproducibility. */
+ svec_init(&targets);
+
+ for (i = 0; i < ovs->n_manager_options; i++) {
+ svec_add(&targets, ovs->manager_options[i]->target);
+ }
+
+ svec_sort_unique(&targets);
+ for (i = 0; i < targets.n; i++) {
+ ds_put_format(&ctx->output, "%s\n", targets.names[i]);
+ }
+ svec_destroy(&targets);
+}
+
+static void
+delete_managers(const struct vsctl_context *ctx)
+{
+ const struct ovsrec_open_vswitch *ovs = ctx->ovs;
+ size_t i;
+
+ /* Delete Manager rows pointed to by 'manager_options' column. */
+ for (i = 0; i < ovs->n_manager_options; i++) {
+ ovsrec_manager_delete(ovs->manager_options[i]);
+ }
+
+ /* Delete 'Manager' row refs in 'manager_options' column. */
+ ovsrec_open_vswitch_set_manager_options(ovs, NULL, 0);
+}
+
+static void
+cmd_del_manager(struct vsctl_context *ctx)
+{
+ const struct ovsrec_open_vswitch *ovs = ctx->ovs;
+
+ verify_managers(ovs);
+ delete_managers(ctx);
+}
+
+static void
+insert_managers(struct vsctl_context *ctx, char *targets[], size_t n)
+{
+ struct ovsrec_manager **managers;
+ size_t i;
+
+ /* Insert each manager in a new row in Manager table. */
+ managers = xmalloc(n * sizeof *managers);
+ for (i = 0; i < n; i++) {
+ if (stream_verify_name(targets[i]) && pstream_verify_name(targets[i])) {
+ VLOG_WARN("target type \"%s\" is possibly erroneous", targets[i]);
+ }
+ managers[i] = ovsrec_manager_insert(ctx->txn);
+ ovsrec_manager_set_target(managers[i], targets[i]);
+ }
+
+ /* Store uuids of new Manager rows in 'manager_options' column. */
+ ovsrec_open_vswitch_set_manager_options(ctx->ovs, managers, n);
+ free(managers);
+}
+
+static void
+cmd_set_manager(struct vsctl_context *ctx)
+{
+ const size_t n = ctx->argc - 1;
+
+ verify_managers(ctx->ovs);
+ delete_managers(ctx);
+ insert_managers(ctx, &ctx->argv[1], n);
+}
+
+static void
+pre_cmd_get_ssl(struct vsctl_context *ctx)
+{
+ ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_ssl);
+
+ ovsdb_idl_add_column(ctx->idl, &ovsrec_ssl_col_private_key);
+ ovsdb_idl_add_column(ctx->idl, &ovsrec_ssl_col_certificate);
+ ovsdb_idl_add_column(ctx->idl, &ovsrec_ssl_col_ca_cert);
+ ovsdb_idl_add_column(ctx->idl, &ovsrec_ssl_col_bootstrap_ca_cert);
+}
+
+static void
+cmd_get_ssl(struct vsctl_context *ctx)
+{
+ struct ovsrec_ssl *ssl = ctx->ovs->ssl;
+
+ ovsrec_open_vswitch_verify_ssl(ctx->ovs);
+ if (ssl) {
+ ovsrec_ssl_verify_private_key(ssl);
+ ovsrec_ssl_verify_certificate(ssl);
+ ovsrec_ssl_verify_ca_cert(ssl);
+ ovsrec_ssl_verify_bootstrap_ca_cert(ssl);
+
+ ds_put_format(&ctx->output, "Private key: %s\n", ssl->private_key);
+ ds_put_format(&ctx->output, "Certificate: %s\n", ssl->certificate);
+ ds_put_format(&ctx->output, "CA Certificate: %s\n", ssl->ca_cert);
+ ds_put_format(&ctx->output, "Bootstrap: %s\n",
+ ssl->bootstrap_ca_cert ? "true" : "false");
+ }
+}
+
+static void
+pre_cmd_del_ssl(struct vsctl_context *ctx)
+{
+ ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_ssl);
+}
+
+static void
+cmd_del_ssl(struct vsctl_context *ctx)
+{
+ struct ovsrec_ssl *ssl = ctx->ovs->ssl;
+
+ if (ssl) {
+ ovsrec_open_vswitch_verify_ssl(ctx->ovs);
+ ovsrec_ssl_delete(ssl);
+ ovsrec_open_vswitch_set_ssl(ctx->ovs, NULL);
+ }
+}
+
+static void
+pre_cmd_set_ssl(struct vsctl_context *ctx)
+{
+ ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_ssl);
+}
+
+static void
+cmd_set_ssl(struct vsctl_context *ctx)
+{
+ bool bootstrap = shash_find(&ctx->options, "--bootstrap");
+ struct ovsrec_ssl *ssl = ctx->ovs->ssl;
+
+ ovsrec_open_vswitch_verify_ssl(ctx->ovs);
+ if (ssl) {
+ ovsrec_ssl_delete(ssl);
+ }
+ ssl = ovsrec_ssl_insert(ctx->txn);
+
+ ovsrec_ssl_set_private_key(ssl, ctx->argv[1]);
+ ovsrec_ssl_set_certificate(ssl, ctx->argv[2]);
+ ovsrec_ssl_set_ca_cert(ssl, ctx->argv[3]);
+
+ ovsrec_ssl_set_bootstrap_ca_cert(ssl, bootstrap);
+
+ ovsrec_open_vswitch_set_ssl(ctx->ovs, ssl);
+}
+\f
+/* Parameter commands. */
+
+struct vsctl_row_id {
+ const struct ovsdb_idl_table_class *table;
+ const struct ovsdb_idl_column *name_column;
+ const struct ovsdb_idl_column *uuid_column;
+};
+
+struct vsctl_table_class {
+ struct ovsdb_idl_table_class *class;
+ struct vsctl_row_id row_ids[2];
+};
+
+static const struct vsctl_table_class tables[] = {
+ {&ovsrec_table_bridge,
+ {{&ovsrec_table_bridge, &ovsrec_bridge_col_name, NULL},
+ {&ovsrec_table_flow_sample_collector_set, NULL,
+ &ovsrec_flow_sample_collector_set_col_bridge}}},
+
+ {&ovsrec_table_controller,
+ {{&ovsrec_table_bridge,
+ &ovsrec_bridge_col_name,
+ &ovsrec_bridge_col_controller}}},
+
+ {&ovsrec_table_interface,
+ {{&ovsrec_table_interface, &ovsrec_interface_col_name, NULL},
+ {NULL, NULL, NULL}}},
+
+ {&ovsrec_table_mirror,
+ {{&ovsrec_table_mirror, &ovsrec_mirror_col_name, NULL},
+ {NULL, NULL, NULL}}},
+
+ {&ovsrec_table_manager,
+ {{&ovsrec_table_manager, &ovsrec_manager_col_target, NULL},
+ {NULL, NULL, NULL}}},
+
+ {&ovsrec_table_netflow,
+ {{&ovsrec_table_bridge,
+ &ovsrec_bridge_col_name,
+ &ovsrec_bridge_col_netflow},
+ {NULL, NULL, NULL}}},
+
+ {&ovsrec_table_open_vswitch,
+ {{&ovsrec_table_open_vswitch, NULL, NULL},
+ {NULL, NULL, NULL}}},
+
+ {&ovsrec_table_port,
+ {{&ovsrec_table_port, &ovsrec_port_col_name, NULL},
+ {NULL, NULL, NULL}}},
+
+ {&ovsrec_table_qos,
+ {{&ovsrec_table_port, &ovsrec_port_col_name, &ovsrec_port_col_qos},
+ {NULL, NULL, NULL}}},
+
+ {&ovsrec_table_queue,
+ {{NULL, NULL, NULL},
+ {NULL, NULL, NULL}}},
+
+ {&ovsrec_table_ssl,
+ {{&ovsrec_table_open_vswitch, NULL, &ovsrec_open_vswitch_col_ssl}}},
+
+ {&ovsrec_table_sflow,
+ {{&ovsrec_table_bridge,
+ &ovsrec_bridge_col_name,
+ &ovsrec_bridge_col_sflow},
+ {NULL, NULL, NULL}}},
+
+ {&ovsrec_table_flow_table,
+ {{&ovsrec_table_flow_table, &ovsrec_flow_table_col_name, NULL},
+ {NULL, NULL, NULL}}},
+
+ {&ovsrec_table_ipfix,
+ {{&ovsrec_table_bridge,
+ &ovsrec_bridge_col_name,
+ &ovsrec_bridge_col_ipfix},
+ {&ovsrec_table_flow_sample_collector_set, NULL,
+ &ovsrec_flow_sample_collector_set_col_ipfix}}},
+
+ {&ovsrec_table_flow_sample_collector_set,
+ {{NULL, NULL, NULL},
+ {NULL, NULL, NULL}}},
+
+ {NULL, {{NULL, NULL, NULL}, {NULL, NULL, NULL}}}
+};
+
+static void
+die_if_error(char *error)
+{
+ if (error) {
+ vsctl_fatal("%s", error);
+ }
+}
+
+static int
+to_lower_and_underscores(unsigned c)
+{
+ return c == '-' ? '_' : tolower(c);
+}
+
+static unsigned int
+score_partial_match(const char *name, const char *s)
+{
+ int score;
+
+ if (!strcmp(name, s)) {
+ return UINT_MAX;
+ }
+ for (score = 0; ; score++, name++, s++) {
+ if (to_lower_and_underscores(*name) != to_lower_and_underscores(*s)) {
+ break;
+ } else if (*name == '\0') {
+ return UINT_MAX - 1;
+ }
+ }
+ return *s == '\0' ? score : 0;
+}
+
+static const struct vsctl_table_class *
+get_table(const char *table_name)
+{
+ const struct vsctl_table_class *table;
+ const struct vsctl_table_class *best_match = NULL;
+ unsigned int best_score = 0;
+
+ for (table = tables; table->class; table++) {
+ unsigned int score = score_partial_match(table->class->name,
+ table_name);
+ if (score > best_score) {
+ best_match = table;
+ best_score = score;
+ } else if (score == best_score) {
+ best_match = NULL;
+ }
+ }
+ if (best_match) {
+ return best_match;
+ } else if (best_score) {
+ vsctl_fatal("multiple table names match \"%s\"", table_name);
+ } else {
+ vsctl_fatal("unknown table \"%s\"", table_name);
+ }
+}
+
+static const struct vsctl_table_class *
+pre_get_table(struct vsctl_context *ctx, const char *table_name)
+{
+ const struct vsctl_table_class *table_class;
+ int i;
+
+ table_class = get_table(table_name);
+ ovsdb_idl_add_table(ctx->idl, table_class->class);
+
+ for (i = 0; i < ARRAY_SIZE(table_class->row_ids); i++) {
+ const struct vsctl_row_id *id = &table_class->row_ids[i];
+ if (id->table) {
+ ovsdb_idl_add_table(ctx->idl, id->table);
+ }
+ if (id->name_column) {
+ ovsdb_idl_add_column(ctx->idl, id->name_column);
+ }
+ if (id->uuid_column) {
+ ovsdb_idl_add_column(ctx->idl, id->uuid_column);
+ }
+ }
+
+ return table_class;
+}
+
+static const struct ovsdb_idl_row *
+get_row_by_id(struct vsctl_context *ctx, const struct vsctl_table_class *table,
+ const struct vsctl_row_id *id, const char *record_id)
+{
+ const struct ovsdb_idl_row *referrer, *final;
+
+ if (!id->table) {
+ return NULL;
+ }
+
+ if (!id->name_column) {
+ if (strcmp(record_id, ".")) {
+ return NULL;
+ }
+ referrer = ovsdb_idl_first_row(ctx->idl, id->table);
+ if (!referrer || ovsdb_idl_next_row(referrer)) {
+ return NULL;
+ }
+ } else {
+ const struct ovsdb_idl_row *row;
+
+ referrer = NULL;
+ for (row = ovsdb_idl_first_row(ctx->idl, id->table);
+ row != NULL;
+ row = ovsdb_idl_next_row(row))
+ {
+ const struct ovsdb_datum *name;
+
+ name = ovsdb_idl_get(row, id->name_column,
+ OVSDB_TYPE_STRING, OVSDB_TYPE_VOID);
+ if (name->n == 1 && !strcmp(name->keys[0].string, record_id)) {
+ if (referrer) {
+ vsctl_fatal("multiple rows in %s match \"%s\"",
+ table->class->name, record_id);
+ }
+ referrer = row;
+ }
+ }
+ }
+ if (!referrer) {
+ return NULL;
+ }
+
+ final = NULL;
+ if (id->uuid_column) {
+ const struct ovsdb_datum *uuid;
+
+ ovsdb_idl_txn_verify(referrer, id->uuid_column);
+ uuid = ovsdb_idl_get(referrer, id->uuid_column,
+ OVSDB_TYPE_UUID, OVSDB_TYPE_VOID);
+ if (uuid->n == 1) {
+ final = ovsdb_idl_get_row_for_uuid(ctx->idl, table->class,
+ &uuid->keys[0].uuid);
+ }
+ } else {
+ final = referrer;
+ }
+
+ return final;
+}
+
+static const struct ovsdb_idl_row *
+get_row (struct vsctl_context *ctx,
+ const struct vsctl_table_class *table, const char *record_id,
+ bool must_exist)
+{
+ const struct ovsdb_idl_row *row;
+ struct uuid uuid;
+
+ if (uuid_from_string(&uuid, record_id)) {
+ row = ovsdb_idl_get_row_for_uuid(ctx->idl, table->class, &uuid);
+ } else {
+ int i;
+
+ for (i = 0; i < ARRAY_SIZE(table->row_ids); i++) {
+ row = get_row_by_id(ctx, table, &table->row_ids[i], record_id);
+ if (row) {
+ break;
+ }
+ }
+ }
+ if (must_exist && !row) {
+ vsctl_fatal("no row \"%s\" in table %s",
+ record_id, table->class->name);
+ }
+ return row;
+}
+
+static char *
+get_column(const struct vsctl_table_class *table, const char *column_name,
+ const struct ovsdb_idl_column **columnp)
+{
+ const struct ovsdb_idl_column *best_match = NULL;
+ unsigned int best_score = 0;
+ size_t i;
+
+ for (i = 0; i < table->class->n_columns; i++) {
+ const struct ovsdb_idl_column *column = &table->class->columns[i];
+ unsigned int score = score_partial_match(column->name, column_name);
+ if (score > best_score) {
+ best_match = column;
+ best_score = score;
+ } else if (score == best_score) {
+ best_match = NULL;
+ }
+ }
+
+ *columnp = best_match;
+ if (best_match) {
+ return NULL;
+ } else if (best_score) {
+ return xasprintf("%s contains more than one column whose name "
+ "matches \"%s\"", table->class->name, column_name);
+ } else {
+ return xasprintf("%s does not contain a column whose name matches "
+ "\"%s\"", table->class->name, column_name);
+ }
+}
+
+static struct ovsdb_symbol *
+create_symbol(struct ovsdb_symbol_table *symtab, const char *id, bool *newp)
+{
+ struct ovsdb_symbol *symbol;
+
+ if (id[0] != '@') {
+ vsctl_fatal("row id \"%s\" does not begin with \"@\"", id);
+ }
+
+ if (newp) {
+ *newp = ovsdb_symbol_table_get(symtab, id) == NULL;
+ }
+
+ symbol = ovsdb_symbol_table_insert(symtab, id);
+ if (symbol->created) {
+ vsctl_fatal("row id \"%s\" may only be specified on one --id option",
+ id);
+ }
+ symbol->created = true;
+ return symbol;
+}
+
+static void
+pre_get_column(struct vsctl_context *ctx,
+ const struct vsctl_table_class *table, const char *column_name,
+ const struct ovsdb_idl_column **columnp)
+{
+ die_if_error(get_column(table, column_name, columnp));
+ ovsdb_idl_add_column(ctx->idl, *columnp);
+}
+
+static char *
+missing_operator_error(const char *arg, const char **allowed_operators,
+ size_t n_allowed)
+{
+ struct ds s;
+
+ ds_init(&s);
+ ds_put_format(&s, "%s: argument does not end in ", arg);
+ ds_put_format(&s, "\"%s\"", allowed_operators[0]);
+ if (n_allowed == 2) {
+ ds_put_format(&s, " or \"%s\"", allowed_operators[1]);
+ } else if (n_allowed > 2) {
+ size_t i;
+
+ for (i = 1; i < n_allowed - 1; i++) {
+ ds_put_format(&s, ", \"%s\"", allowed_operators[i]);
+ }
+ ds_put_format(&s, ", or \"%s\"", allowed_operators[i]);
+ }
+ ds_put_format(&s, " followed by a value.");
+
+ return ds_steal_cstr(&s);
+}
+
+/* Breaks 'arg' apart into a number of fields in the following order:
+ *
+ * - The name of a column in 'table', stored into '*columnp'. The column
+ * name may be abbreviated.
+ *
+ * - Optionally ':' followed by a key string. The key is stored as a
+ * malloc()'d string into '*keyp', or NULL if no key is present in
+ * 'arg'.
+ *
+ * - If 'valuep' is nonnull, an operator followed by a value string. The
+ * allowed operators are the 'n_allowed' string in 'allowed_operators',
+ * or just "=" if 'n_allowed' is 0. If 'operatorp' is nonnull, then the
+ * index of the operator within 'allowed_operators' is stored into
+ * '*operatorp'. The value is stored as a malloc()'d string into
+ * '*valuep', or NULL if no value is present in 'arg'.
+ *
+ * On success, returns NULL. On failure, returned a malloc()'d string error
+ * message and stores NULL into all of the nonnull output arguments. */
+static char * WARN_UNUSED_RESULT
+parse_column_key_value(const char *arg,
+ const struct vsctl_table_class *table,
+ const struct ovsdb_idl_column **columnp, char **keyp,
+ int *operatorp,
+ const char **allowed_operators, size_t n_allowed,
+ char **valuep)
+{
+ const char *p = arg;
+ char *column_name;
+ char *error;
+
+ ovs_assert(!(operatorp && !valuep));
+ *keyp = NULL;
+ if (valuep) {
+ *valuep = NULL;
+ }
+
+ /* Parse column name. */
+ error = ovsdb_token_parse(&p, &column_name);
+ if (error) {
+ goto error;
+ }
+ if (column_name[0] == '\0') {
+ free(column_name);
+ error = xasprintf("%s: missing column name", arg);
+ goto error;
+ }
+ error = get_column(table, column_name, columnp);
+ free(column_name);
+ if (error) {
+ goto error;
+ }
+
+ /* Parse key string. */
+ if (*p == ':') {
+ p++;
+ error = ovsdb_token_parse(&p, keyp);
+ if (error) {
+ goto error;
+ }
+ }
+
+ /* Parse value string. */
+ if (valuep) {
+ size_t best_len;
+ size_t i;
+ int best;
+
+ if (!allowed_operators) {
+ static const char *equals = "=";
+ allowed_operators = =
+ n_allowed = 1;
+ }
+
+ best = -1;
+ best_len = 0;
+ for (i = 0; i < n_allowed; i++) {
+ const char *op = allowed_operators[i];
+ size_t op_len = strlen(op);
+
+ if (op_len > best_len && !strncmp(op, p, op_len) && p[op_len]) {
+ best_len = op_len;
+ best = i;
+ }
+ }
+ if (best < 0) {
+ error = missing_operator_error(arg, allowed_operators, n_allowed);
+ goto error;
+ }
+
+ if (operatorp) {
+ *operatorp = best;
+ }
+ *valuep = xstrdup(p + best_len);
+ } else {
+ if (*p != '\0') {
+ error = xasprintf("%s: trailing garbage \"%s\" in argument",
+ arg, p);
+ goto error;
+ }
+ }
+ return NULL;
+
+error:
+ *columnp = NULL;
+ free(*keyp);
+ *keyp = NULL;
+ if (valuep) {
+ free(*valuep);
+ *valuep = NULL;
+ if (operatorp) {
+ *operatorp = -1;
+ }
+ }
+ return error;
+}
+
+static const struct ovsdb_idl_column *
+pre_parse_column_key_value(struct vsctl_context *ctx,
+ const char *arg,
+ const struct vsctl_table_class *table)
+{
+ const struct ovsdb_idl_column *column;
+ const char *p;
+ char *column_name;
+
+ p = arg;
+ die_if_error(ovsdb_token_parse(&p, &column_name));
+ if (column_name[0] == '\0') {
+ vsctl_fatal("%s: missing column name", arg);
+ }
+
+ pre_get_column(ctx, table, column_name, &column);
+ free(column_name);
+
+ return column;
+}
+
+static void
+check_mutable(const struct vsctl_table_class *table,
+ const struct ovsdb_idl_column *column)
+{
+ if (!column->mutable) {
+ vsctl_fatal("cannot modify read-only column %s in table %s",
+ column->name, table->class->name);
+ }
+}
+
+static void
+pre_cmd_get(struct vsctl_context *ctx)
+{
+ const char *id = shash_find_data(&ctx->options, "--id");
+ const char *table_name = ctx->argv[1];
+ const struct vsctl_table_class *table;
+ int i;
+
+ /* Using "get" without --id or a column name could possibly make sense.
+ * Maybe, for example, a ovs-vsctl run wants to assert that a row exists.
+ * But it is unlikely that an interactive user would want to do that, so
+ * issue a warning if we're running on a terminal. */
+ if (!id && ctx->argc <= 3 && isatty(STDOUT_FILENO)) {
+ VLOG_WARN("\"get\" command without row arguments or \"--id\" is "
+ "possibly erroneous");
+ }
+
+ table = pre_get_table(ctx, table_name);
+ for (i = 3; i < ctx->argc; i++) {
+ if (!strcasecmp(ctx->argv[i], "_uuid")
+ || !strcasecmp(ctx->argv[i], "-uuid")) {
+ continue;
+ }
+
+ pre_parse_column_key_value(ctx, ctx->argv[i], table);
+ }
+}
+
+static void
+cmd_get(struct vsctl_context *ctx)
+{
+ const char *id = shash_find_data(&ctx->options, "--id");
+ bool must_exist = !shash_find(&ctx->options, "--if-exists");
+ const char *table_name = ctx->argv[1];
+ const char *record_id = ctx->argv[2];
+ const struct vsctl_table_class *table;
+ const struct ovsdb_idl_row *row;
+ struct ds *out = &ctx->output;
+ int i;
+
+ if (id && !must_exist) {
+ vsctl_fatal("--if-exists and --id may not be specified together");
+ }
+
+ table = get_table(table_name);
+ row = get_row(ctx, table, record_id, must_exist);
+ if (!row) {
+ return;
+ }
+
+ if (id) {
+ struct ovsdb_symbol *symbol;
+ bool new;
+
+ symbol = create_symbol(ctx->symtab, id, &new);
+ if (!new) {
+ vsctl_fatal("row id \"%s\" specified on \"get\" command was used "
+ "before it was defined", id);
+ }
+ symbol->uuid = row->uuid;
+
+ /* This symbol refers to a row that already exists, so disable warnings
+ * about it being unreferenced. */
+ symbol->strong_ref = true;
+ }
+ for (i = 3; i < ctx->argc; i++) {
+ const struct ovsdb_idl_column *column;
+ const struct ovsdb_datum *datum;
+ char *key_string;
+
+ /* Special case for obtaining the UUID of a row. We can't just do this
+ * through parse_column_key_value() below since it returns a "struct
+ * ovsdb_idl_column" and the UUID column doesn't have one. */
+ if (!strcasecmp(ctx->argv[i], "_uuid")
+ || !strcasecmp(ctx->argv[i], "-uuid")) {
+ ds_put_format(out, UUID_FMT"\n", UUID_ARGS(&row->uuid));
+ continue;
+ }
+
+ die_if_error(parse_column_key_value(ctx->argv[i], table,
+ &column, &key_string,
+ NULL, NULL, 0, NULL));
+
+ ovsdb_idl_txn_verify(row, column);
+ datum = ovsdb_idl_read(row, column);
+ if (key_string) {
+ union ovsdb_atom key;
+ unsigned int idx;
+
+ if (column->type.value.type == OVSDB_TYPE_VOID) {
+ vsctl_fatal("cannot specify key to get for non-map column %s",
+ column->name);
+ }
+
+ die_if_error(ovsdb_atom_from_string(&key,
+ &column->type.key,
+ key_string, ctx->symtab));
+
+ idx = ovsdb_datum_find_key(datum, &key,
+ column->type.key.type);
+ if (idx == UINT_MAX) {
+ if (must_exist) {
+ vsctl_fatal("no key \"%s\" in %s record \"%s\" column %s",
+ key_string, table->class->name, record_id,
+ column->name);
+ }
+ } else {
+ ovsdb_atom_to_string(&datum->values[idx],
+ column->type.value.type, out);
+ }
+ ovsdb_atom_destroy(&key, column->type.key.type);
+ } else {
+ ovsdb_datum_to_string(datum, &column->type, out);
+ }
+ ds_put_char(out, '\n');
+
+ free(key_string);
+ }
+}
+
+static void
+parse_column_names(const char *column_names,
+ const struct vsctl_table_class *table,
+ const struct ovsdb_idl_column ***columnsp,
+ size_t *n_columnsp)
+{
+ const struct ovsdb_idl_column **columns;
+ size_t n_columns;
+
+ if (!column_names) {
+ size_t i;
+
+ n_columns = table->class->n_columns + 1;
+ columns = xmalloc(n_columns * sizeof *columns);
+ columns[0] = NULL;
+ for (i = 0; i < table->class->n_columns; i++) {
+ columns[i + 1] = &table->class->columns[i];
+ }
+ } else {
+ char *s = xstrdup(column_names);
+ size_t allocated_columns;
+ char *save_ptr = NULL;
+ char *column_name;
+
+ columns = NULL;
+ allocated_columns = n_columns = 0;
+ for (column_name = strtok_r(s, ", ", &save_ptr); column_name;
+ column_name = strtok_r(NULL, ", ", &save_ptr)) {
+ const struct ovsdb_idl_column *column;
+
+ if (!strcasecmp(column_name, "_uuid")) {
+ column = NULL;
+ } else {
+ die_if_error(get_column(table, column_name, &column));
+ }
+ if (n_columns >= allocated_columns) {
+ columns = x2nrealloc(columns, &allocated_columns,
+ sizeof *columns);
+ }
+ columns[n_columns++] = column;
+ }
+ free(s);
+
+ if (!n_columns) {
+ vsctl_fatal("must specify at least one column name");
+ }
+ }
+ *columnsp = columns;
+ *n_columnsp = n_columns;
+}
+
+
+static void
+pre_list_columns(struct vsctl_context *ctx,
+ const struct vsctl_table_class *table,
+ const char *column_names)
+{
+ const struct ovsdb_idl_column **columns;
+ size_t n_columns;
+ size_t i;
+
+ parse_column_names(column_names, table, &columns, &n_columns);
+ for (i = 0; i < n_columns; i++) {
+ if (columns[i]) {
+ ovsdb_idl_add_column(ctx->idl, columns[i]);
+ }
+ }
+ free(columns);
+}
+
+static void
+pre_cmd_list(struct vsctl_context *ctx)
+{
+ const char *column_names = shash_find_data(&ctx->options, "--columns");
+ const char *table_name = ctx->argv[1];
+ const struct vsctl_table_class *table;
+
+ table = pre_get_table(ctx, table_name);
+ pre_list_columns(ctx, table, column_names);
+}
+
+static struct table *
+list_make_table(const struct ovsdb_idl_column **columns, size_t n_columns)
+{
+ struct table *out;
+ size_t i;
+
+ out = xmalloc(sizeof *out);
+ table_init(out);
+
+ for (i = 0; i < n_columns; i++) {
+ const struct ovsdb_idl_column *column = columns[i];
+ const char *column_name = column ? column->name : "_uuid";
+
+ table_add_column(out, "%s", column_name);
+ }
+
+ return out;
+}
+
+static void
+list_record(const struct ovsdb_idl_row *row,
+ const struct ovsdb_idl_column **columns, size_t n_columns,
+ struct table *out)
+{
+ size_t i;
+
+ if (!row) {
+ return;
+ }
+
+ table_add_row(out);
+ for (i = 0; i < n_columns; i++) {
+ const struct ovsdb_idl_column *column = columns[i];
+ struct cell *cell = table_add_cell(out);
+
+ if (!column) {
+ struct ovsdb_datum datum;
+ union ovsdb_atom atom;
+
+ atom.uuid = row->uuid;
+
+ datum.keys = &atom;
+ datum.values = NULL;
+ datum.n = 1;
+
+ cell->json = ovsdb_datum_to_json(&datum, &ovsdb_type_uuid);
+ cell->type = &ovsdb_type_uuid;
+ } else {
+ const struct ovsdb_datum *datum = ovsdb_idl_read(row, column);
+
+ cell->json = ovsdb_datum_to_json(datum, &column->type);
+ cell->type = &column->type;
+ }
+ }
+}
+
+static void
+cmd_list(struct vsctl_context *ctx)
+{
+ const char *column_names = shash_find_data(&ctx->options, "--columns");
+ bool must_exist = !shash_find(&ctx->options, "--if-exists");
+ const struct ovsdb_idl_column **columns;
+ const char *table_name = ctx->argv[1];
+ const struct vsctl_table_class *table;
+ struct table *out;
+ size_t n_columns;
+ int i;
+
+ table = get_table(table_name);
+ parse_column_names(column_names, table, &columns, &n_columns);
+ out = ctx->table = list_make_table(columns, n_columns);
+ if (ctx->argc > 2) {
+ for (i = 2; i < ctx->argc; i++) {
+ list_record(get_row(ctx, table, ctx->argv[i], must_exist),
+ columns, n_columns, out);
+ }
+ } else {
+ const struct ovsdb_idl_row *row;
+
+ for (row = ovsdb_idl_first_row(ctx->idl, table->class); row != NULL;
+ row = ovsdb_idl_next_row(row)) {
+ list_record(row, columns, n_columns, out);
+ }
+ }
+ free(columns);
+}
+
+static void
+pre_cmd_find(struct vsctl_context *ctx)
+{
+ const char *column_names = shash_find_data(&ctx->options, "--columns");
+ const char *table_name = ctx->argv[1];
+ const struct vsctl_table_class *table;
+ int i;
+
+ table = pre_get_table(ctx, table_name);
+ pre_list_columns(ctx, table, column_names);
+ for (i = 2; i < ctx->argc; i++) {
+ pre_parse_column_key_value(ctx, ctx->argv[i], table);
+ }
+}
+
+static void
+cmd_find(struct vsctl_context *ctx)
+{
+ const char *column_names = shash_find_data(&ctx->options, "--columns");
+ const struct ovsdb_idl_column **columns;
+ const char *table_name = ctx->argv[1];
+ const struct vsctl_table_class *table;
+ const struct ovsdb_idl_row *row;
+ struct table *out;
+ size_t n_columns;
+
+ table = get_table(table_name);
+ parse_column_names(column_names, table, &columns, &n_columns);
+ out = ctx->table = list_make_table(columns, n_columns);
+ for (row = ovsdb_idl_first_row(ctx->idl, table->class); row;
+ row = ovsdb_idl_next_row(row)) {
+ int i;
+
+ for (i = 2; i < ctx->argc; i++) {
+ if (!is_condition_satisfied(table, row, ctx->argv[i],
+ ctx->symtab)) {
+ goto next_row;
+ }
+ }
+ list_record(row, columns, n_columns, out);
+
+ next_row: ;
+ }
+ free(columns);
+}
+
+static void
+pre_cmd_set(struct vsctl_context *ctx)
+{
+ const char *table_name = ctx->argv[1];
+ const struct vsctl_table_class *table;
+ int i;
+
+ table = pre_get_table(ctx, table_name);
+ for (i = 3; i < ctx->argc; i++) {
+ const struct ovsdb_idl_column *column;
+
+ column = pre_parse_column_key_value(ctx, ctx->argv[i], table);
+ check_mutable(table, column);
+ }
+}
+
+static void
+set_column(const struct vsctl_table_class *table,
+ const struct ovsdb_idl_row *row, const char *arg,
+ struct ovsdb_symbol_table *symtab)
+{
+ const struct ovsdb_idl_column *column;
+ char *key_string, *value_string;
+ char *error;
+
+ error = parse_column_key_value(arg, table, &column, &key_string,
+ NULL, NULL, 0, &value_string);
+ die_if_error(error);
+ if (!value_string) {
+ vsctl_fatal("%s: missing value", arg);
+ }
+
+ if (key_string) {
+ union ovsdb_atom key, value;
+ struct ovsdb_datum datum;
+
+ if (column->type.value.type == OVSDB_TYPE_VOID) {
+ vsctl_fatal("cannot specify key to set for non-map column %s",
+ column->name);
+ }
+
+ die_if_error(ovsdb_atom_from_string(&key, &column->type.key,
+ key_string, symtab));
+ die_if_error(ovsdb_atom_from_string(&value, &column->type.value,
+ value_string, symtab));
+
+ ovsdb_datum_init_empty(&datum);
+ ovsdb_datum_add_unsafe(&datum, &key, &value, &column->type);
+
+ ovsdb_atom_destroy(&key, column->type.key.type);
+ ovsdb_atom_destroy(&value, column->type.value.type);
+
+ ovsdb_datum_union(&datum, ovsdb_idl_read(row, column),
+ &column->type, false);
+ ovsdb_idl_txn_write(row, column, &datum);
+ } else {
+ struct ovsdb_datum datum;
+
+ die_if_error(ovsdb_datum_from_string(&datum, &column->type,
+ value_string, symtab));
+ ovsdb_idl_txn_write(row, column, &datum);
+ }
+
+ free(key_string);
+ free(value_string);
+}
+
+static void
+cmd_set(struct vsctl_context *ctx)
+{
+ bool must_exist = !shash_find(&ctx->options, "--if-exists");
+ const char *table_name = ctx->argv[1];
+ const char *record_id = ctx->argv[2];
+ const struct vsctl_table_class *table;
+ const struct ovsdb_idl_row *row;
+ int i;
+
+ table = get_table(table_name);
+ row = get_row(ctx, table, record_id, must_exist);
+ if (!row) {
+ return;
+ }