+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) {
+ 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;
+
+ 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;
+ }
+}
+
+static void
+query_db_remotes(const char *name, const struct ovsdb *db,
+ struct shash *remotes)
+{
+ const struct ovsdb_column *column;
+ const struct ovsdb_table *table;
+ const struct ovsdb_row *row;
+
+ parse_db_column(db, name, &table, &column);
+
+ 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 shash *statuses)
+{
+ struct ovsdb_row *rw_row;
+ const char *target;
+ const struct ovsdb_jsonrpc_remote_status *status;
+ char *keys[4], *values[4];
+ 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;
+ }
+
+ /* Prepare to modify this row. */
+ rw_row = ovsdb_txn_row_modify(txn, row);
+
+ /* Find status information for this target. */
+ status = shash_find_data(statuses, target);
+ if (!status) {
+ /* Should never happen, but just in case... */
+ return;
+ }
+
+ /* Update status information columns. */
+
+ write_bool_column(rw_row, "is_connected",
+ status->is_connected);
+
+ 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));
+ }
+ write_string_string_column(rw_row, "status", keys, values, n);
+}
+
+static void
+update_remote_rows(const struct ovsdb *db, struct ovsdb_txn *txn,
+ const char *remote_name, const struct shash *statuses)
+{
+ const struct ovsdb_table *table, *ref_table;
+ const struct ovsdb_column *column;
+ const struct ovsdb_row *row;
+
+ if (strncmp("db:", remote_name, 3)) {
+ return;
+ }
+
+ parse_db_column(db, remote_name, &table, &column);
+
+ 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) {