+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[3], *values[3];
+ 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);
+ keys[n] = xstrdup("time_in_state");
+ values[n++] = xasprintf("%u", status->state_elapsed);
+ 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) {
+ 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, txn, statuses);
+ }
+ }
+ }
+}
+
+static void
+update_remote_status(const struct ovsdb_jsonrpc_server *jsonrpc,
+ const struct shash *remotes, struct ovsdb *db)
+{
+ static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
+ struct shash_node *remote;
+ struct shash statuses;
+ struct ovsdb_txn *txn;
+ const bool durable_txn = false;
+ struct ovsdb_error *error;
+
+ /* Get status of current connections. */
+ ovsdb_jsonrpc_server_get_remote_status(jsonrpc, &statuses);
+
+ txn = ovsdb_txn_create(db);
+
+ /* Iterate over --remote arguments given on command line. */
+ SHASH_FOR_EACH (remote, remotes) {
+ update_remote_rows(db, txn, remote->name, &statuses);
+ }
+
+ error = ovsdb_txn_commit(txn, durable_txn);
+ if (error) {
+ VLOG_ERR_RL(&rl, "Failed to update remote status: %s",
+ ovsdb_error_to_string(error));
+ }
+
+ shash_destroy_free_data(&statuses);
+}
+