+ 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);
+
+ free_info(&info);
+}
+
+static void
+cmd_add_port(struct vsctl_context *ctx)
+{
+ bool may_exist = shash_find(&ctx->options, "--may-exist") != 0;
+
+ 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") != 0;
+ 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;
+ struct vsctl_port *port;
+ struct vsctl_info info;
+
+ get_info(ctx, &info);
+ if (!with_iface) {
+ port = find_port(&info, ctx->argv[ctx->argc - 1], must_exist);
+ } else {
+ const char *target = ctx->argv[ctx->argc - 1];
+ struct vsctl_iface *iface;
+
+ port = find_port(&info, target, false);
+ if (!port) {
+ iface = find_iface(&info, 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(&info, 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(&info, port);
+ }
+
+ free_info(&info);
+}
+
+static void
+cmd_port_to_br(struct vsctl_context *ctx)
+{
+ struct vsctl_port *port;
+ struct vsctl_info info;
+
+ get_info(ctx, &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(struct vsctl_context *ctx)
+{
+ struct vsctl_bridge *bridge;
+ struct vsctl_info info;
+
+ get_info(ctx, &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(struct vsctl_context *ctx)
+{
+ struct vsctl_bridge *bridge;
+ struct vsctl_info info;
+
+ get_info(ctx, &info);
+ bridge = find_bridge(&info, ctx->argv[1], true);
+ if (bridge->parent) {
+ bridge = bridge->parent;
+ }
+ ds_put_format(&ctx->output, "%s\n", bridge->name);
+ free_info(&info);
+}
+
+static void
+cmd_list_ifaces(struct vsctl_context *ctx)
+{
+ struct vsctl_bridge *br;
+ struct shash_node *node;
+ struct vsctl_info info;
+ struct svec ifaces;
+
+ get_info(ctx, &info);
+ br = find_bridge(&info, ctx->argv[1], true);
+ verify_ports(ctx);
+
+ svec_init(&ifaces);
+ SHASH_FOR_EACH (node, &info.ifaces) {
+ struct vsctl_iface *iface = node->data;
+
+ if (strcmp(iface->iface_cfg->name, br->name)
+ && br == iface->port->bridge) {
+ svec_add(&ifaces, iface->iface_cfg->name);
+ }
+ }
+ output_sorted(&ifaces, &ctx->output);
+ svec_destroy(&ifaces);
+
+ free_info(&info);
+}
+
+static void
+cmd_iface_to_br(struct vsctl_context *ctx)
+{
+ struct vsctl_iface *iface;
+ struct vsctl_info info;
+
+ get_info(ctx, &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
+verify_controllers(struct ovsrec_bridge *bridge)
+{
+ if (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_info info;
+ struct vsctl_bridge *br;
+ struct svec targets;
+ size_t i;
+
+ get_info(ctx, &info);
+ br = find_bridge(&info, ctx->argv[1], true);
+ verify_controllers(br->br_cfg);
+
+ /* Print the targets in sorted order for reproducibility. */
+ svec_init(&targets);
+ for (i = 0; i < br->n_ctrl; i++) {
+ svec_add(&targets, br->ctrl[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);
+
+ free_info(&info);
+}
+
+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 vsctl_info info;
+ struct vsctl_bridge *br;
+
+ get_info(ctx, &info);
+
+ br = find_real_bridge(&info, ctx->argv[1], true);
+ verify_controllers(br->br_cfg);
+
+ if (br->ctrl) {
+ delete_controllers(br->ctrl, br->n_ctrl);
+ ovsrec_bridge_set_controller(br->br_cfg, NULL, 0);
+ }
+
+ free_info(&info);
+}
+
+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++) {
+ 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 vsctl_info info;
+ struct vsctl_bridge *br;
+ struct ovsrec_controller **controllers;
+ size_t n;
+
+ get_info(ctx, &info);
+ br = find_real_bridge(&info, ctx->argv[1], true);
+ verify_controllers(br->br_cfg);
+
+ delete_controllers(br->ctrl, br->n_ctrl);
+
+ n = ctx->argc - 2;
+ controllers = insert_controllers(ctx->txn, &ctx->argv[2], n);
+ ovsrec_bridge_set_controller(br->br_cfg, controllers, n);
+ free(controllers);
+
+ free_info(&info);
+}
+
+static void
+cmd_get_fail_mode(struct vsctl_context *ctx)
+{
+ struct vsctl_info info;
+ struct vsctl_bridge *br;
+
+ get_info(ctx, &info);
+ br = find_bridge(&info, ctx->argv[1], true);
+
+ if (br->br_cfg) {
+ ovsrec_bridge_verify_fail_mode(br->br_cfg);
+ }
+ if (br->fail_mode && strlen(br->fail_mode)) {
+ ds_put_format(&ctx->output, "%s\n", br->fail_mode);
+ }
+
+ free_info(&info);
+}
+
+static void
+cmd_del_fail_mode(struct vsctl_context *ctx)
+{
+ struct vsctl_info info;
+ struct vsctl_bridge *br;
+
+ get_info(ctx, &info);
+ br = find_real_bridge(&info, ctx->argv[1], true);
+
+ ovsrec_bridge_set_fail_mode(br->br_cfg, NULL);
+
+ free_info(&info);
+}
+
+static void
+cmd_set_fail_mode(struct vsctl_context *ctx)
+{
+ struct vsctl_info info;
+ struct vsctl_bridge *br;
+ const char *fail_mode = ctx->argv[2];
+
+ get_info(ctx, &info);
+ br = find_real_bridge(&info, 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);
+
+ free_info(&info);
+}
+
+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++) {
+ 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},
+ {NULL, NULL, NULL}}},
+
+ {&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_monitor,
+ {{&ovsrec_table_interface,
+ &ovsrec_interface_col_name,
+ &ovsrec_interface_col_monitor},
+ {NULL, NULL, NULL}}},
+
+ {&ovsrec_table_maintenance_point,
+ {{NULL, NULL, NULL},
+ {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}}},
+
+ {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)
+{
+ 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;
+ }
+ }
+ }
+ return row;
+}
+
+static const struct ovsdb_idl_row *
+must_get_row(struct vsctl_context *ctx,
+ const struct vsctl_table_class *table, const char *record_id)
+{
+ const struct ovsdb_idl_row *row = get_row(ctx, table, record_id);
+ if (!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
+ * operator is stored into '*operatorp' (one of the pointers from
+ * 'allowed_operators' is stored; nothing is malloc()'d). 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,
+ const char **operatorp,
+ const char **allowed_operators, size_t n_allowed,
+ char **valuep)
+{
+ const char *p = arg;
+ char *column_name;
+ char *error;
+
+ 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) {
+ const char *best;
+ size_t best_len;
+ size_t i;
+
+ if (!allowed_operators) {
+ static const char *equals = "=";
+ allowed_operators = =
+ n_allowed = 1;
+ }
+
+ best = NULL;
+ 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 = op;
+ }
+ }
+ if (!best) {
+ 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 = NULL;
+ }
+ }
+ return error;
+}
+
+static void
+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);
+}
+
+static void
+pre_cmd_get(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++) {
+ 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 if_exists = 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;
+
+ table = get_table(table_name);
+ row = must_get_row(ctx, table, record_id);
+ 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 (!if_exists) {
+ 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;