+/* Forces all of the JSON-RPC sessions managed by 'remote' to disconnect and
+ * reconnect. */
+static void
+ovsdb_jsonrpc_session_reconnect_all(struct ovsdb_jsonrpc_remote *remote)
+{
+ struct ovsdb_jsonrpc_session *s, *next;
+
+ LIST_FOR_EACH_SAFE (s, next, node, &remote->sessions) {
+ jsonrpc_session_force_reconnect(s->js);
+ if (!jsonrpc_session_is_alive(s->js)) {
+ ovsdb_jsonrpc_session_close(s);
+ }
+ }
+}
+
+/* Sets the options for all of the JSON-RPC sessions managed by 'remote' to
+ * 'options'. */
+static void
+ovsdb_jsonrpc_session_set_all_options(
+ struct ovsdb_jsonrpc_remote *remote,
+ const struct ovsdb_jsonrpc_options *options)
+{
+ struct ovsdb_jsonrpc_session *s;
+
+ if (remote->listener) {
+ int error;
+
+ error = pstream_set_dscp(remote->listener, options->dscp);
+ if (error) {
+ VLOG_ERR("%s: set_dscp failed %s",
+ pstream_get_name(remote->listener), ovs_strerror(error));
+ } else {
+ remote->dscp = options->dscp;
+ }
+ /*
+ * XXX race window between setting dscp to listening socket
+ * and accepting socket. Accepted socket may have old dscp value.
+ * Ignore this race window for now.
+ */
+ }
+ LIST_FOR_EACH (s, node, &remote->sessions) {
+ ovsdb_jsonrpc_session_set_options(s, options);
+ }
+}
+
+static bool
+ovsdb_jsonrpc_session_get_status(const struct ovsdb_jsonrpc_remote *remote,
+ struct ovsdb_jsonrpc_remote_status *status)
+{
+ const struct ovsdb_jsonrpc_session *s;
+ const struct jsonrpc_session *js;
+ struct ovsdb_lock_waiter *waiter;
+ struct reconnect_stats rstats;
+ struct ds locks_held, locks_waiting, locks_lost;
+
+ status->bound_port = (remote->listener
+ ? pstream_get_bound_port(remote->listener)
+ : htons(0));
+
+ if (list_is_empty(&remote->sessions)) {
+ return false;
+ }
+ s = CONTAINER_OF(remote->sessions.next, struct ovsdb_jsonrpc_session, node);
+ js = s->js;
+
+ status->is_connected = jsonrpc_session_is_connected(js);
+ status->last_error = jsonrpc_session_get_status(js);
+
+ jsonrpc_session_get_reconnect_stats(js, &rstats);
+ status->state = rstats.state;
+ status->sec_since_connect = rstats.msec_since_connect == UINT_MAX
+ ? UINT_MAX : rstats.msec_since_connect / 1000;
+ status->sec_since_disconnect = rstats.msec_since_disconnect == UINT_MAX
+ ? UINT_MAX : rstats.msec_since_disconnect / 1000;
+
+ ds_init(&locks_held);
+ ds_init(&locks_waiting);
+ ds_init(&locks_lost);
+ HMAP_FOR_EACH (waiter, session_node, &s->up.waiters) {
+ struct ds *string;
+
+ string = (ovsdb_lock_waiter_is_owner(waiter) ? &locks_held
+ : waiter->mode == OVSDB_LOCK_WAIT ? &locks_waiting
+ : &locks_lost);
+ if (string->length) {
+ ds_put_char(string, ' ');
+ }
+ ds_put_cstr(string, waiter->lock_name);
+ }
+ status->locks_held = ds_steal_cstr(&locks_held);
+ status->locks_waiting = ds_steal_cstr(&locks_waiting);
+ status->locks_lost = ds_steal_cstr(&locks_lost);
+
+ status->n_connections = list_size(&remote->sessions);
+
+ return true;
+}
+
+/* Examines 'request' to determine the database to which it relates, and then
+ * searches 's' to find that database:
+ *
+ * - If successful, returns the database and sets '*replyp' to NULL.
+ *
+ * - If no such database exists, returns NULL and sets '*replyp' to an
+ * appropriate JSON-RPC error reply, owned by the caller. */
+static struct ovsdb *
+ovsdb_jsonrpc_lookup_db(const struct ovsdb_jsonrpc_session *s,
+ const struct jsonrpc_msg *request,
+ struct jsonrpc_msg **replyp)
+{
+ struct json_array *params;
+ struct ovsdb_error *error;
+ const char *db_name;
+ struct ovsdb *db;
+
+ params = json_array(request->params);
+ if (!params->n || params->elems[0]->type != JSON_STRING) {
+ error = ovsdb_syntax_error(
+ request->params, NULL,
+ "%s request params must begin with <db-name>", request->method);
+ goto error;
+ }
+
+ db_name = params->elems[0]->u.string;
+ db = shash_find_data(&s->up.server->dbs, db_name);
+ if (!db) {
+ error = ovsdb_syntax_error(
+ request->params, "unknown database",
+ "%s request specifies unknown database %s",
+ request->method, db_name);
+ goto error;
+ }
+
+ *replyp = NULL;
+ return db;
+
+error:
+ *replyp = jsonrpc_create_reply(ovsdb_error_to_json(error), request->id);
+ ovsdb_error_destroy(error);
+ return NULL;
+}
+
+static struct ovsdb_error *
+ovsdb_jsonrpc_session_parse_lock_name(const struct jsonrpc_msg *request,
+ const char **lock_namep)
+{
+ const struct json_array *params;
+
+ params = json_array(request->params);
+ if (params->n != 1 || params->elems[0]->type != JSON_STRING ||
+ !ovsdb_parser_is_id(json_string(params->elems[0]))) {
+ *lock_namep = NULL;
+ return ovsdb_syntax_error(request->params, NULL,
+ "%s request params must be <id>",
+ request->method);
+ }
+
+ *lock_namep = json_string(params->elems[0]);
+ return NULL;
+}
+
+static void
+ovsdb_jsonrpc_session_notify(struct ovsdb_session *session,
+ const char *lock_name,
+ const char *method)
+{
+ struct ovsdb_jsonrpc_session *s;
+ struct json *params;
+
+ s = CONTAINER_OF(session, struct ovsdb_jsonrpc_session, up);
+ params = json_array_create_1(json_string_create(lock_name));
+ ovsdb_jsonrpc_session_send(s, jsonrpc_create_notify(method, params));
+}
+
+static struct jsonrpc_msg *
+ovsdb_jsonrpc_session_lock(struct ovsdb_jsonrpc_session *s,
+ struct jsonrpc_msg *request,
+ enum ovsdb_lock_mode mode)
+{
+ struct ovsdb_lock_waiter *waiter;
+ struct jsonrpc_msg *reply;
+ struct ovsdb_error *error;
+ struct ovsdb_session *victim;
+ const char *lock_name;
+ struct json *result;
+
+ error = ovsdb_jsonrpc_session_parse_lock_name(request, &lock_name);
+ if (error) {
+ goto error;
+ }
+
+ /* Report error if this session has issued a "lock" or "steal" without a
+ * matching "unlock" for this lock. */
+ waiter = ovsdb_session_get_lock_waiter(&s->up, lock_name);
+ if (waiter) {
+ error = ovsdb_syntax_error(
+ request->params, NULL,
+ "must issue \"unlock\" before new \"%s\"", request->method);
+ goto error;
+ }
+
+ /* Get the lock, add us as a waiter. */
+ waiter = ovsdb_server_lock(&s->remote->server->up, &s->up, lock_name, mode,
+ &victim);
+ if (victim) {
+ ovsdb_jsonrpc_session_notify(victim, lock_name, "stolen");
+ }
+
+ result = json_object_create();
+ json_object_put(result, "locked",
+ json_boolean_create(ovsdb_lock_waiter_is_owner(waiter)));
+
+ return jsonrpc_create_reply(result, request->id);
+
+error:
+ reply = jsonrpc_create_reply(ovsdb_error_to_json(error), request->id);
+ ovsdb_error_destroy(error);
+ return reply;
+}
+
+static void
+ovsdb_jsonrpc_session_unlock_all(struct ovsdb_jsonrpc_session *s)
+{
+ struct ovsdb_lock_waiter *waiter, *next;
+
+ HMAP_FOR_EACH_SAFE (waiter, next, session_node, &s->up.waiters) {
+ ovsdb_jsonrpc_session_unlock__(waiter);
+ }
+}
+
+static void
+ovsdb_jsonrpc_session_unlock__(struct ovsdb_lock_waiter *waiter)
+{
+ struct ovsdb_lock *lock = waiter->lock;
+
+ if (lock) {
+ struct ovsdb_session *new_owner = ovsdb_lock_waiter_remove(waiter);
+ if (new_owner) {
+ ovsdb_jsonrpc_session_notify(new_owner, lock->name, "locked");
+ } else {
+ /* ovsdb_server_lock() might have freed 'lock'. */
+ }
+ }
+
+ ovsdb_lock_waiter_destroy(waiter);
+}
+
+static struct jsonrpc_msg *
+ovsdb_jsonrpc_session_unlock(struct ovsdb_jsonrpc_session *s,
+ struct jsonrpc_msg *request)
+{
+ struct ovsdb_lock_waiter *waiter;
+ struct jsonrpc_msg *reply;
+ struct ovsdb_error *error;
+ const char *lock_name;
+
+ error = ovsdb_jsonrpc_session_parse_lock_name(request, &lock_name);
+ if (error) {
+ goto error;
+ }
+
+ /* Report error if this session has not issued a "lock" or "steal" for this
+ * lock. */
+ waiter = ovsdb_session_get_lock_waiter(&s->up, lock_name);
+ if (!waiter) {
+ error = ovsdb_syntax_error(
+ request->params, NULL, "\"unlock\" without \"lock\" or \"steal\"");
+ goto error;
+ }
+
+ ovsdb_jsonrpc_session_unlock__(waiter);
+
+ return jsonrpc_create_reply(json_object_create(), request->id);
+
+error:
+ reply = jsonrpc_create_reply(ovsdb_error_to_json(error), request->id);
+ ovsdb_error_destroy(error);
+ return reply;
+}
+