+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_info info;
+ struct vsctl_bridge *bridge;
+ struct ovsrec_interface **ifaces;
+ struct ovsrec_port *port;
+ size_t i;
+
+ get_info(ctx->ovs, &info);
+ if (may_exist) {
+ struct vsctl_port *port;
+
+ port = find_port(&info, port_name, false);
+ if (port) {
+ struct svec want_names, have_names;
+ size_t i;
+
+ 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 < port->port_cfg->n_interfaces; i++) {
+ svec_add(&have_names, port->port_cfg->interfaces[i]->name);
+ }
+ svec_sort(&have_names);
+
+ if (strcmp(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, 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(&info, port_name,
+ xasprintf("cannot create a port named %s", port_name));
+ for (i = 0; i < n_ifaces; i++) {
+ check_conflicts(&info, iface_names[i],
+ xasprintf("cannot create an interface named %s",
+ iface_names[i]));
+ }
+ bridge = find_bridge(&info, 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);
+ free(ifaces);
+
+ if (bridge->vlan) {
+ 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);
+
+ 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->ovs, &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->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(struct vsctl_context *ctx)
+{
+ struct vsctl_bridge *bridge;
+ struct vsctl_info info;
+
+ 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(struct vsctl_context *ctx)
+{
+ struct vsctl_bridge *bridge;
+ struct vsctl_info info;
+
+ get_info(ctx->ovs, &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->ovs, &info);
+ br = find_bridge(&info, ctx->argv[1], true);
+
+ 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->ovs, &info);
+ iface = find_iface(&info, ctx->argv[1], true);
+ ds_put_format(&ctx->output, "%s\n", iface->port->bridge->name);
+ free_info(&info);
+}
+
+/* Print targets of the 'n_controllers' in 'controllers' on the output for
+ * 'ctx'. */
+static void
+print_controllers(struct vsctl_context *ctx,
+ struct ovsrec_controller **controllers,
+ size_t n_controllers)
+{
+ /* Print the targets in sorted order for reproducibility. */
+ struct svec targets;
+ size_t i;
+
+ svec_init(&targets);
+ for (i = 0; i < n_controllers; i++) {
+ svec_add(&targets, controllers[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
+cmd_get_controller(struct vsctl_context *ctx)
+{
+ struct vsctl_info info;
+
+ get_info(ctx->ovs, &info);
+
+ if (ctx->argc == 1 || !strcmp(ctx->argv[1], "default")) {
+ print_controllers(ctx, info.ctrl, info.n_ctrl);
+ } else {
+ struct vsctl_bridge *br = find_bridge(&info, ctx->argv[1], true);
+ if (br->n_ctrl) {
+ print_controllers(ctx, br->ctrl, br->n_ctrl);
+ } else {
+ print_controllers(ctx, info.ctrl, info.n_ctrl);
+ }
+ }
+
+ 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;
+
+ get_info(ctx->ovs, &info);
+
+ if (ctx->argc == 1 || !strcmp(ctx->argv[1], "default")) {
+ if (info.n_ctrl) {
+ delete_controllers(info.ctrl, info.n_ctrl);
+ ovsrec_open_vswitch_set_controller(ctx->ovs, NULL, 0);
+ }
+ } else {
+ struct vsctl_bridge *br = find_real_bridge(&info, ctx->argv[1], true);
+ 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
+set_default_controllers(struct vsctl_context *ctx, char *targets[], size_t n)
+{
+ struct ovsrec_controller **controllers;
+
+ delete_controllers(ctx->ovs->controller, ctx->ovs->n_controller);
+
+ controllers = insert_controllers(ctx->txn, targets, n);
+ ovsrec_open_vswitch_set_controller(ctx->ovs, controllers, n);
+ free(controllers);
+}
+
+static void
+cmd_set_controller(struct vsctl_context *ctx)
+{
+ struct vsctl_info info;
+
+ get_info(ctx->ovs, &info);
+
+ if (ctx->argc == 2) {
+ /* Set one controller in the "Open_vSwitch" table. */
+ set_default_controllers(ctx, &ctx->argv[1], 1);
+ } else if (!strcmp(ctx->argv[1], "default")) {
+ /* Set one or more controllers in the "Open_vSwitch" table. */
+ set_default_controllers(ctx, &ctx->argv[2], ctx->argc - 2);
+ } else {
+ /* Set one or more controllers for a particular bridge. */
+ struct vsctl_bridge *br = find_real_bridge(&info, ctx->argv[1], true);
+ struct ovsrec_controller **controllers;
+ size_t n;
+
+ 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 const char *
+get_fail_mode(struct ovsrec_controller **controllers, size_t n_controllers)
+{
+ const char *fail_mode;
+ size_t i;
+
+ fail_mode = NULL;
+ for (i = 0; i < n_controllers; i++) {
+ const char *s = controllers[i]->fail_mode;
+ if (s) {
+ if (!strcmp(s, "secure")) {
+ return s;
+ } else {
+ fail_mode = s;
+ }
+ }
+ }
+
+ return fail_mode;
+}
+
+static void
+cmd_get_fail_mode(struct vsctl_context *ctx)
+{
+ struct vsctl_info info;
+ const char *fail_mode = NULL;
+
+ get_info(ctx->ovs, &info);
+
+ if (ctx->argc == 1 || !strcmp(ctx->argv[1], "default")) {
+ /* Return the fail-mode from the "Open_vSwitch" table */
+ fail_mode = get_fail_mode(info.ctrl, info.n_ctrl);
+ } else {
+ /* Return the fail-mode for a particular bridge. */
+ struct vsctl_bridge *br = find_bridge(&info, ctx->argv[1], true);
+
+ /* If no controller is defined for the requested bridge, fallback to
+ * the "Open_vSwitch" table's controller. */
+ fail_mode = (br->n_ctrl
+ ? get_fail_mode(br->ctrl, br->n_ctrl)
+ : get_fail_mode(info.ctrl, info.n_ctrl));
+ }
+
+ if (fail_mode && strlen(fail_mode)) {
+ ds_put_format(&ctx->output, "%s\n", fail_mode);
+ }
+
+ free_info(&info);
+}
+
+static void
+set_fail_mode(struct ovsrec_controller **controllers, size_t n_controllers,
+ const char *fail_mode)
+{
+ size_t i;
+
+ for (i = 0; i < n_controllers; i++) {
+ ovsrec_controller_set_fail_mode(controllers[i], fail_mode);
+ }
+}
+
+static void
+cmd_del_fail_mode(struct vsctl_context *ctx)
+{
+ struct vsctl_info info;
+
+ get_info(ctx->ovs, &info);
+
+ if (ctx->argc == 1 || !strcmp(ctx->argv[1], "default")) {
+ set_fail_mode(info.ctrl, info.n_ctrl, NULL);
+ } else {
+ struct vsctl_bridge *br = find_real_bridge(&info, ctx->argv[1], true);
+
+ set_fail_mode(br->ctrl, br->n_ctrl, NULL);
+ }
+
+ free_info(&info);
+}
+
+static void
+cmd_set_fail_mode(struct vsctl_context *ctx)
+{
+ struct vsctl_info info;
+ const char *bridge;
+ const char *fail_mode;
+
+ get_info(ctx->ovs, &info);
+
+ if (ctx->argc == 2) {
+ bridge = "default";
+ fail_mode = ctx->argv[1];
+ } else {
+ bridge = ctx->argv[1];
+ fail_mode = ctx->argv[2];
+ }
+
+ if (strcmp(fail_mode, "standalone") && strcmp(fail_mode, "secure")) {
+ vsctl_fatal("fail-mode must be \"standalone\" or \"secure\"");
+ }
+
+ if (!strcmp(bridge, "default")) {
+ /* Set the fail-mode in the "Open_vSwitch" table. */
+ if (!info.ctrl) {
+ vsctl_fatal("no controller declared");
+ }
+ set_fail_mode(info.ctrl, info.n_ctrl, fail_mode);
+ } else {
+ struct vsctl_bridge *br = find_real_bridge(&info, bridge, true);
+
+ if (!br->ctrl) {
+ vsctl_fatal("no controller declared for %s", br->name);
+ }
+ set_fail_mode(br->ctrl, br->n_ctrl, fail_mode);
+ }
+
+ free_info(&info);
+}
+
+static void
+cmd_get_ssl(struct vsctl_context *ctx)
+{
+ struct ovsrec_ssl *ssl = ctx->ovs->ssl;
+
+ if (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
+cmd_del_ssl(struct vsctl_context *ctx)
+{
+ struct ovsrec_ssl *ssl = ctx->ovs->ssl;
+
+ if (ssl) {
+ ovsrec_ssl_delete(ssl);
+ ovsrec_open_vswitch_set_ssl(ctx->ovs, NULL);
+ }
+}
+
+static void
+cmd_set_ssl(struct vsctl_context *ctx)
+{
+ bool bootstrap = shash_find(&ctx->options, "--bootstrap");
+ struct ovsrec_ssl *ssl = ctx->ovs->ssl;
+
+ 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_open_vswitch,
+ NULL,
+ &ovsrec_open_vswitch_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_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}}},
+
+ {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 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;
+
+ 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)