+write_bool_column(struct ovsdb_row *row, const char *column_name, bool value)
+{
+ const struct ovsdb_column *column;
+ struct ovsdb_datum *datum;
+
+ column = ovsdb_table_schema_get_column(row->table->schema, column_name);
+ datum = get_datum(row, column_name, OVSDB_TYPE_BOOLEAN,
+ OVSDB_TYPE_VOID, 1);
+ if (!datum) {
+ return;
+ }
+
+ if (datum->n != 1) {
+ ovsdb_datum_destroy(datum, &column->type);
+
+ datum->n = 1;
+ datum->keys = xmalloc(sizeof *datum->keys);
+ datum->values = NULL;
+ }
+
+ datum->keys[0].boolean = value;
+}
+
+static void
+write_string_string_column(struct ovsdb_row *row, const char *column_name,
+ char **keys, char **values, size_t n)
+{
+ const struct ovsdb_column *column;
+ struct ovsdb_datum *datum;
+ size_t i;
+
+ column = ovsdb_table_schema_get_column(row->table->schema, column_name);
+ datum = get_datum(row, column_name, OVSDB_TYPE_STRING, OVSDB_TYPE_STRING,
+ UINT_MAX);
+ if (!datum) {
+ for (i = 0; i < n; i++) {
+ free(keys[i]);
+ free(values[i]);
+ }
+ return;
+ }
+
+ /* Free existing data. */
+ ovsdb_datum_destroy(datum, &column->type);
+
+ /* Allocate space for new values. */
+ datum->n = n;
+ datum->keys = xmalloc(n * sizeof *datum->keys);
+ datum->values = xmalloc(n * sizeof *datum->values);
+
+ for (i = 0; i < n; ++i) {
+ datum->keys[i].string = keys[i];
+ datum->values[i].string = values[i];
+ }
+
+ /* Sort and check constraints. */
+ ovsdb_datum_sort_assert(datum, column->type.key.type);
+}
+
+/* Adds a remote and options to 'remotes', based on the Manager table row in
+ * 'row'. */
+static void
+add_manager_options(struct shash *remotes, const struct ovsdb_row *row)
+{
+ static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
+ struct ovsdb_jsonrpc_options *options;
+ long long int max_backoff, probe_interval;
+ const char *target, *dscp_string;
+
+ if (!read_string_column(row, "target", &target) || !target) {
+ VLOG_INFO_RL(&rl, "Table `%s' has missing or invalid `target' column",
+ row->table->schema->name);
+ return;
+ }
+
+ options = add_remote(remotes, target);
+ if (read_integer_column(row, "max_backoff", &max_backoff)) {
+ options->max_backoff = max_backoff;
+ }
+ if (read_integer_column(row, "inactivity_probe", &probe_interval)) {
+ options->probe_interval = probe_interval;
+ }
+
+ options->dscp = DSCP_DEFAULT;
+ dscp_string = read_map_string_column(row, "other_config", "dscp");
+ if (dscp_string) {
+ int dscp = atoi(dscp_string);
+ if (dscp >= 0 && dscp <= 63) {
+ options->dscp = dscp;
+ }
+ }
+}
+
+static void
+query_db_remotes(const char *name, const struct shash *all_dbs,
+ struct shash *remotes, struct ds *errors)
+{
+ const struct ovsdb_column *column;
+ const struct ovsdb_table *table;
+ const struct ovsdb_row *row;
+ const struct db *db;
+ char *retval;
+
+ retval = parse_db_column(all_dbs, name, &db, &table, &column);
+ if (retval) {
+ ds_put_format(errors, "%s\n", retval);
+ free(retval);
+ return;
+ }
+
+ if (column->type.key.type == OVSDB_TYPE_STRING
+ && column->type.value.type == OVSDB_TYPE_VOID) {
+ HMAP_FOR_EACH (row, hmap_node, &table->rows) {
+ const struct ovsdb_datum *datum;
+ size_t i;
+
+ datum = &row->fields[column->index];
+ for (i = 0; i < datum->n; i++) {
+ add_remote(remotes, datum->keys[i].string);
+ }
+ }
+ } else if (column->type.key.type == OVSDB_TYPE_UUID
+ && column->type.key.u.uuid.refTable
+ && column->type.value.type == OVSDB_TYPE_VOID) {
+ const struct ovsdb_table *ref_table = column->type.key.u.uuid.refTable;
+ HMAP_FOR_EACH (row, hmap_node, &table->rows) {
+ const struct ovsdb_datum *datum;
+ size_t i;
+
+ datum = &row->fields[column->index];
+ for (i = 0; i < datum->n; i++) {
+ const struct ovsdb_row *ref_row;
+
+ ref_row = ovsdb_table_get_row(ref_table, &datum->keys[i].uuid);
+ if (ref_row) {
+ add_manager_options(remotes, ref_row);
+ }
+ }
+ }
+ }
+}
+
+static void
+update_remote_row(const struct ovsdb_row *row, struct ovsdb_txn *txn,
+ const struct ovsdb_jsonrpc_server *jsonrpc)
+{
+ struct ovsdb_jsonrpc_remote_status status;
+ struct ovsdb_row *rw_row;
+ const char *target;
+ char *keys[9], *values[9];
+ size_t n = 0;
+
+ /* Get the "target" (protocol/host/port) spec. */
+ if (!read_string_column(row, "target", &target)) {
+ /* Bad remote spec or incorrect schema. */
+ return;
+ }
+ rw_row = ovsdb_txn_row_modify(txn, row);
+ ovsdb_jsonrpc_server_get_remote_status(jsonrpc, target, &status);
+
+ /* Update status information columns. */
+ write_bool_column(rw_row, "is_connected", status.is_connected);
+
+ if (status.state) {
+ keys[n] = xstrdup("state");
+ values[n++] = xstrdup(status.state);
+ }
+ if (status.sec_since_connect != UINT_MAX) {
+ keys[n] = xstrdup("sec_since_connect");
+ values[n++] = xasprintf("%u", status.sec_since_connect);
+ }
+ if (status.sec_since_disconnect != UINT_MAX) {
+ keys[n] = xstrdup("sec_since_disconnect");
+ values[n++] = xasprintf("%u", status.sec_since_disconnect);
+ }
+ if (status.last_error) {
+ keys[n] = xstrdup("last_error");
+ values[n++] =
+ xstrdup(ovs_retval_to_string(status.last_error));
+ }
+ if (status.locks_held && status.locks_held[0]) {
+ keys[n] = xstrdup("locks_held");
+ values[n++] = xstrdup(status.locks_held);
+ }
+ if (status.locks_waiting && status.locks_waiting[0]) {
+ keys[n] = xstrdup("locks_waiting");
+ values[n++] = xstrdup(status.locks_waiting);
+ }
+ if (status.locks_lost && status.locks_lost[0]) {
+ keys[n] = xstrdup("locks_lost");
+ values[n++] = xstrdup(status.locks_lost);
+ }
+ if (status.n_connections > 1) {
+ keys[n] = xstrdup("n_connections");
+ values[n++] = xasprintf("%d", status.n_connections);
+ }
+ if (status.bound_port != htons(0)) {
+ keys[n] = xstrdup("bound_port");
+ values[n++] = xasprintf("%"PRIu16, ntohs(status.bound_port));
+ }
+ write_string_string_column(rw_row, "status", keys, values, n);
+
+ ovsdb_jsonrpc_server_free_remote_status(&status);
+}
+
+static void
+update_remote_rows(const struct shash *all_dbs,
+ const char *remote_name,
+ const struct ovsdb_jsonrpc_server *jsonrpc)
+{
+ const struct ovsdb_table *table, *ref_table;
+ const struct ovsdb_column *column;
+ const struct ovsdb_row *row;
+ const struct db *db;
+ char *retval;
+
+ if (strncmp("db:", remote_name, 3)) {
+ return;
+ }
+
+ retval = parse_db_column(all_dbs, remote_name, &db, &table, &column);
+ if (retval) {
+ free(retval);
+ return;
+ }
+
+ if (column->type.key.type != OVSDB_TYPE_UUID
+ || !column->type.key.u.uuid.refTable
+ || column->type.value.type != OVSDB_TYPE_VOID) {
+ return;
+ }
+
+ ref_table = column->type.key.u.uuid.refTable;
+
+ HMAP_FOR_EACH (row, hmap_node, &table->rows) {
+ const struct ovsdb_datum *datum;
+ size_t i;
+
+ datum = &row->fields[column->index];
+ for (i = 0; i < datum->n; i++) {
+ const struct ovsdb_row *ref_row;
+
+ ref_row = ovsdb_table_get_row(ref_table, &datum->keys[i].uuid);
+ if (ref_row) {
+ update_remote_row(ref_row, db->txn, jsonrpc);
+ }
+ }
+ }
+}
+
+static void
+update_remote_status(const struct ovsdb_jsonrpc_server *jsonrpc,
+ const struct sset *remotes,
+ struct shash *all_dbs)
+{
+ static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
+ const char *remote;
+ struct db *db;
+ struct shash_node *node;
+
+ SHASH_FOR_EACH(node, all_dbs) {
+ db = node->data;
+ db->txn = ovsdb_txn_create(db->db);
+ }
+
+ /* Iterate over --remote arguments given on command line. */
+ SSET_FOR_EACH (remote, remotes) {
+ update_remote_rows(all_dbs, remote, jsonrpc);
+ }
+
+ SHASH_FOR_EACH(node, all_dbs) {
+ struct ovsdb_error *error;
+ db = node->data;
+ error = ovsdb_txn_commit(db->txn, false);
+ if (error) {
+ VLOG_ERR_RL(&rl, "Failed to update remote status: %s",
+ ovsdb_error_to_string(error));
+ ovsdb_error_destroy(error);
+ }
+ }
+}
+
+/* Reconfigures ovsdb-server's remotes based on information in the database. */
+static char *
+reconfigure_remotes(struct ovsdb_jsonrpc_server *jsonrpc,
+ const struct shash *all_dbs, struct sset *remotes)
+{
+ struct ds errors = DS_EMPTY_INITIALIZER;
+ struct shash resolved_remotes;
+ const char *name;
+
+ /* Configure remotes. */
+ shash_init(&resolved_remotes);
+ SSET_FOR_EACH (name, remotes) {
+ if (!strncmp(name, "db:", 3)) {
+ query_db_remotes(name, all_dbs, &resolved_remotes, &errors);
+ } else {
+ add_remote(&resolved_remotes, name);
+ }
+ }
+ ovsdb_jsonrpc_server_set_remotes(jsonrpc, &resolved_remotes);
+ shash_destroy_free_data(&resolved_remotes);
+
+ return errors.string;
+}
+
+static char *
+reconfigure_ssl(const struct shash *all_dbs)
+{
+ struct ds errors = DS_EMPTY_INITIALIZER;
+ const char *resolved_private_key;
+ const char *resolved_certificate;
+ const char *resolved_ca_cert;
+
+ resolved_private_key = query_db_string(all_dbs, private_key_file, &errors);
+ resolved_certificate = query_db_string(all_dbs, certificate_file, &errors);
+ resolved_ca_cert = query_db_string(all_dbs, ca_cert_file, &errors);
+
+ stream_ssl_set_key_and_cert(resolved_private_key, resolved_certificate);
+ stream_ssl_set_ca_cert_file(resolved_ca_cert, bootstrap_ca_cert);
+
+ return errors.string;
+}
+
+static void
+report_error_if_changed(char *error, char **last_errorp)
+{
+ if (error) {
+ if (!*last_errorp || strcmp(error, *last_errorp)) {
+ VLOG_WARN("%s", error);
+ free(*last_errorp);
+ *last_errorp = error;
+ return;
+ }
+ free(error);
+ } else {
+ free(*last_errorp);
+ *last_errorp = NULL;
+ }
+}
+
+static void
+ovsdb_server_exit(struct unixctl_conn *conn, int argc OVS_UNUSED,
+ const char *argv[] OVS_UNUSED,
+ void *exiting_)
+{
+ bool *exiting = exiting_;
+ *exiting = true;
+ unixctl_command_reply(conn, NULL);
+}
+
+static void
+ovsdb_server_compact(struct unixctl_conn *conn, int argc,
+ const char *argv[], void *dbs_)
+{
+ struct shash *all_dbs = dbs_;
+ struct ds reply;
+ struct db *db;
+ struct shash_node *node;
+ int n = 0;
+
+ ds_init(&reply);
+ SHASH_FOR_EACH(node, all_dbs) {
+ const char *name;
+
+ db = node->data;
+ name = db->db->schema->name;
+
+ if (argc < 2 || !strcmp(argv[1], name)) {
+ struct ovsdb_error *error;
+
+ VLOG_INFO("compacting %s database by user request", name);
+
+ error = ovsdb_file_compact(db->file);
+ if (error) {
+ char *s = ovsdb_error_to_string(error);
+ ds_put_format(&reply, "%s\n", s);
+ free(s);
+ ovsdb_error_destroy(error);
+ }
+
+ n++;
+ }
+ }
+
+ if (!n) {
+ unixctl_command_reply_error(conn, "no database by that name");
+ } else if (reply.length) {
+ unixctl_command_reply_error(conn, ds_cstr(&reply));
+ } else {
+ unixctl_command_reply(conn, NULL);
+ }
+ ds_destroy(&reply);
+}
+
+/* "ovsdb-server/reconnect": makes ovsdb-server drop all of its JSON-RPC
+ * connections and reconnect. */
+static void
+ovsdb_server_reconnect(struct unixctl_conn *conn, int argc OVS_UNUSED,
+ const char *argv[] OVS_UNUSED, void *jsonrpc_)
+{
+ struct ovsdb_jsonrpc_server *jsonrpc = jsonrpc_;
+
+ ovsdb_jsonrpc_server_reconnect(jsonrpc);
+ unixctl_command_reply(conn, NULL);
+}
+
+/* "ovsdb-server/add-remote REMOTE": adds REMOTE to the set of remotes that
+ * ovsdb-server services. */
+static void
+ovsdb_server_add_remote(struct unixctl_conn *conn, int argc OVS_UNUSED,
+ const char *argv[], void *config_)
+{
+ struct server_config *config = config_;
+ const char *remote = argv[1];
+
+ const struct ovsdb_column *column;
+ const struct ovsdb_table *table;
+ const struct db *db;
+ char *retval;
+
+ retval = (strncmp("db:", remote, 3)
+ ? NULL
+ : parse_db_column(config->all_dbs, remote,
+ &db, &table, &column));
+ if (!retval) {
+ if (sset_add(config->remotes, remote)) {
+ save_config(config);
+ }
+ unixctl_command_reply(conn, NULL);
+ } else {
+ unixctl_command_reply_error(conn, retval);
+ free(retval);
+ }
+}
+
+/* "ovsdb-server/remove-remote REMOTE": removes REMOTE frmo the set of remotes
+ * that ovsdb-server services. */
+static void
+ovsdb_server_remove_remote(struct unixctl_conn *conn, int argc OVS_UNUSED,
+ const char *argv[], void *config_)
+{
+ struct server_config *config = config_;
+ struct sset_node *node;
+
+ node = sset_find(config->remotes, argv[1]);
+ if (node) {
+ sset_delete(config->remotes, node);
+ save_config(config);
+ unixctl_command_reply(conn, NULL);
+ } else {
+ unixctl_command_reply_error(conn, "no such remote");
+ }
+}
+
+/* "ovsdb-server/list-remotes": outputs a list of configured rmeotes. */
+static void
+ovsdb_server_list_remotes(struct unixctl_conn *conn, int argc OVS_UNUSED,
+ const char *argv[] OVS_UNUSED, void *remotes_)
+{
+ struct sset *remotes = remotes_;
+ const char **list, **p;
+ struct ds s;
+
+ ds_init(&s);
+
+ list = sset_sort(remotes);
+ for (p = list; *p; p++) {
+ ds_put_format(&s, "%s\n", *p);
+ }
+ free(list);
+
+ unixctl_command_reply(conn, ds_cstr(&s));
+ ds_destroy(&s);
+}
+
+
+/* "ovsdb-server/add-db DB": adds the DB to ovsdb-server. */
+static void
+ovsdb_server_add_database(struct unixctl_conn *conn, int argc OVS_UNUSED,
+ const char *argv[], void *config_)
+{
+ struct server_config *config = config_;
+ const char *filename = argv[1];
+ char *error;
+
+ error = open_db(config, filename);
+ if (!error) {
+ save_config(config);
+ unixctl_command_reply(conn, NULL);
+ } else {
+ unixctl_command_reply_error(conn, error);
+ free(error);
+ }
+}
+
+static void
+ovsdb_server_remove_database(struct unixctl_conn *conn, int argc OVS_UNUSED,
+ const char *argv[], void *config_)
+{
+ struct server_config *config = config_;
+ struct shash_node *node;
+ struct db *db;
+ bool ok;
+
+ node = shash_find(config->all_dbs, argv[1]);
+ if (!node) {
+ unixctl_command_reply_error(conn, "Failed to find the database.");
+ return;
+ }
+ db = node->data;
+
+ ok = ovsdb_jsonrpc_server_remove_db(config->jsonrpc, db->db);
+ ovs_assert(ok);
+
+ ovsdb_destroy(db->db);
+ shash_delete(config->all_dbs, node);
+ free(db->filename);
+ free(db);
+
+ save_config(config);
+ unixctl_command_reply(conn, NULL);
+}
+
+static void
+ovsdb_server_list_databases(struct unixctl_conn *conn, int argc OVS_UNUSED,
+ const char *argv[] OVS_UNUSED, void *all_dbs_)
+{
+ struct shash *all_dbs = all_dbs_;
+ const struct shash_node **nodes;
+ struct ds s;
+ size_t i;
+
+ ds_init(&s);
+
+ nodes = shash_sort(all_dbs);
+ for (i = 0; i < shash_count(all_dbs); i++) {
+ struct db *db = nodes[i]->data;
+ ds_put_format(&s, "%s\n", db->db->schema->name);
+ }
+ free(nodes);
+
+ unixctl_command_reply(conn, ds_cstr(&s));
+ ds_destroy(&s);
+}
+
+static void
+parse_options(int *argcp, char **argvp[],
+ struct sset *remotes, char **unixctl_pathp, char **run_command)