+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 db dbs[], size_t n_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(dbs, n_dbs, remote_name, &db, &table, &column);
+ if (retval) {
+ ovs_fatal(0, "%s", retval);
+ }
+
+ 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 db dbs[], size_t n_dbs)
+{
+ static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
+ const char *remote;
+ size_t i;
+
+ for (i = 0; i < n_dbs; i++) {
+ dbs[i].txn = ovsdb_txn_create(dbs[i].db);
+ }
+
+ /* Iterate over --remote arguments given on command line. */
+ SSET_FOR_EACH (remote, remotes) {
+ update_remote_rows(dbs, n_dbs, remote, jsonrpc);
+ }
+
+ for (i = 0; i < n_dbs; i++) {
+ struct ovsdb_error *error = ovsdb_txn_commit(dbs[i].txn, false);
+ if (error) {
+ VLOG_ERR_RL(&rl, "Failed to update remote status: %s",
+ ovsdb_error_to_string(error));
+ ovsdb_error_destroy(error);
+ }
+ }
+}
+