1 /* Copyright (c) 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
18 #include "jsonrpc-server.h"
25 #include "dynamic-string.h"
28 #include "ovsdb-error.h"
29 #include "ovsdb-parser.h"
31 #include "reconnect.h"
38 #include "transaction.h"
42 VLOG_DEFINE_THIS_MODULE(ovsdb_jsonrpc_server);
44 struct ovsdb_jsonrpc_remote;
45 struct ovsdb_jsonrpc_session;
47 /* Message rate-limiting. */
48 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
51 static struct ovsdb_jsonrpc_session *ovsdb_jsonrpc_session_create(
52 struct ovsdb_jsonrpc_remote *, struct jsonrpc_session *);
53 static void ovsdb_jsonrpc_session_run_all(struct ovsdb_jsonrpc_remote *);
54 static void ovsdb_jsonrpc_session_wait_all(struct ovsdb_jsonrpc_remote *);
55 static void ovsdb_jsonrpc_session_get_memory_usage_all(
56 const struct ovsdb_jsonrpc_remote *, struct simap *usage);
57 static void ovsdb_jsonrpc_session_close_all(struct ovsdb_jsonrpc_remote *);
58 static void ovsdb_jsonrpc_session_reconnect_all(struct ovsdb_jsonrpc_remote *);
59 static void ovsdb_jsonrpc_session_set_all_options(
60 struct ovsdb_jsonrpc_remote *, const struct ovsdb_jsonrpc_options *);
61 static bool ovsdb_jsonrpc_session_get_status(
62 const struct ovsdb_jsonrpc_remote *,
63 struct ovsdb_jsonrpc_remote_status *);
64 static void ovsdb_jsonrpc_session_unlock_all(struct ovsdb_jsonrpc_session *);
65 static void ovsdb_jsonrpc_session_unlock__(struct ovsdb_lock_waiter *);
68 static void ovsdb_jsonrpc_trigger_create(struct ovsdb_jsonrpc_session *,
70 struct json *id, struct json *params);
71 static struct ovsdb_jsonrpc_trigger *ovsdb_jsonrpc_trigger_find(
72 struct ovsdb_jsonrpc_session *, const struct json *id, size_t hash);
73 static void ovsdb_jsonrpc_trigger_complete(struct ovsdb_jsonrpc_trigger *);
74 static void ovsdb_jsonrpc_trigger_complete_all(struct ovsdb_jsonrpc_session *);
75 static void ovsdb_jsonrpc_trigger_complete_done(
76 struct ovsdb_jsonrpc_session *);
79 static struct json *ovsdb_jsonrpc_monitor_create(
80 struct ovsdb_jsonrpc_session *, struct ovsdb *, struct json *params);
81 static struct jsonrpc_msg *ovsdb_jsonrpc_monitor_cancel(
82 struct ovsdb_jsonrpc_session *,
83 struct json_array *params,
84 const struct json *request_id);
85 static void ovsdb_jsonrpc_monitor_remove_all(struct ovsdb_jsonrpc_session *);
87 /* JSON-RPC database server. */
89 struct ovsdb_jsonrpc_server {
90 struct ovsdb_server up;
91 unsigned int n_sessions, max_sessions;
92 struct shash remotes; /* Contains "struct ovsdb_jsonrpc_remote *"s. */
95 /* A configured remote. This is either a passive stream listener plus a list
96 * of the currently connected sessions, or a list of exactly one active
98 struct ovsdb_jsonrpc_remote {
99 struct ovsdb_jsonrpc_server *server;
100 struct pstream *listener; /* Listener, if passive. */
101 struct list sessions; /* List of "struct ovsdb_jsonrpc_session"s. */
105 static struct ovsdb_jsonrpc_remote *ovsdb_jsonrpc_server_add_remote(
106 struct ovsdb_jsonrpc_server *, const char *name,
107 const struct ovsdb_jsonrpc_options *options
109 static void ovsdb_jsonrpc_server_del_remote(struct shash_node *);
111 /* Creates and returns a new server to provide JSON-RPC access to an OVSDB.
113 * The caller must call ovsdb_jsonrpc_server_add_db() for each database to
114 * which 'server' should provide access. */
115 struct ovsdb_jsonrpc_server *
116 ovsdb_jsonrpc_server_create(void)
118 struct ovsdb_jsonrpc_server *server = xzalloc(sizeof *server);
119 ovsdb_server_init(&server->up);
120 server->max_sessions = 64;
121 shash_init(&server->remotes);
125 /* Adds 'db' to the set of databases served out by 'svr'. Returns true if
126 * successful, false if 'db''s name is the same as some database already in
129 ovsdb_jsonrpc_server_add_db(struct ovsdb_jsonrpc_server *svr, struct ovsdb *db)
131 return ovsdb_server_add_db(&svr->up, db);
135 ovsdb_jsonrpc_server_destroy(struct ovsdb_jsonrpc_server *svr)
137 struct shash_node *node, *next;
139 SHASH_FOR_EACH_SAFE (node, next, &svr->remotes) {
140 ovsdb_jsonrpc_server_del_remote(node);
142 shash_destroy(&svr->remotes);
143 ovsdb_server_destroy(&svr->up);
147 struct ovsdb_jsonrpc_options *
148 ovsdb_jsonrpc_default_options(const char *target)
150 struct ovsdb_jsonrpc_options *options = xzalloc(sizeof *options);
151 options->max_backoff = RECONNECT_DEFAULT_MAX_BACKOFF;
152 options->probe_interval = (stream_or_pstream_needs_probes(target)
153 ? RECONNECT_DEFAULT_PROBE_INTERVAL
158 /* Sets 'svr''s current set of remotes to the names in 'new_remotes', with
159 * options in the struct ovsdb_jsonrpc_options supplied as the data values.
161 * A remote is an active or passive stream connection method, e.g. "pssl:" or
164 ovsdb_jsonrpc_server_set_remotes(struct ovsdb_jsonrpc_server *svr,
165 const struct shash *new_remotes)
167 struct shash_node *node, *next;
169 SHASH_FOR_EACH_SAFE (node, next, &svr->remotes) {
170 if (!shash_find(new_remotes, node->name)) {
171 VLOG_INFO("%s: remote deconfigured", node->name);
172 ovsdb_jsonrpc_server_del_remote(node);
175 SHASH_FOR_EACH (node, new_remotes) {
176 const struct ovsdb_jsonrpc_options *options = node->data;
177 struct ovsdb_jsonrpc_remote *remote;
179 remote = shash_find_data(&svr->remotes, node->name);
181 remote = ovsdb_jsonrpc_server_add_remote(svr, node->name, options);
187 ovsdb_jsonrpc_session_set_all_options(remote, options);
191 static struct ovsdb_jsonrpc_remote *
192 ovsdb_jsonrpc_server_add_remote(struct ovsdb_jsonrpc_server *svr,
194 const struct ovsdb_jsonrpc_options *options)
196 struct ovsdb_jsonrpc_remote *remote;
197 struct pstream *listener;
200 error = jsonrpc_pstream_open(name, &listener, options->dscp);
201 if (error && error != EAFNOSUPPORT) {
202 VLOG_ERR_RL(&rl, "%s: listen failed: %s", name, strerror(error));
206 remote = xmalloc(sizeof *remote);
207 remote->server = svr;
208 remote->listener = listener;
209 list_init(&remote->sessions);
210 remote->dscp = options->dscp;
211 shash_add(&svr->remotes, name, remote);
214 ovsdb_jsonrpc_session_create(remote, jsonrpc_session_open(name));
220 ovsdb_jsonrpc_server_del_remote(struct shash_node *node)
222 struct ovsdb_jsonrpc_remote *remote = node->data;
224 ovsdb_jsonrpc_session_close_all(remote);
225 pstream_close(remote->listener);
226 shash_delete(&remote->server->remotes, node);
230 /* Stores status information for the remote named 'target', which should have
231 * been configured on 'svr' with a call to ovsdb_jsonrpc_server_set_remotes(),
232 * into '*status'. On success returns true, on failure (if 'svr' doesn't have
233 * a remote named 'target' or if that remote is an inbound remote that has no
234 * active connections) returns false. On failure, 'status' will be zeroed.
237 ovsdb_jsonrpc_server_get_remote_status(
238 const struct ovsdb_jsonrpc_server *svr, const char *target,
239 struct ovsdb_jsonrpc_remote_status *status)
241 const struct ovsdb_jsonrpc_remote *remote;
243 memset(status, 0, sizeof *status);
245 remote = shash_find_data(&svr->remotes, target);
246 return remote && ovsdb_jsonrpc_session_get_status(remote, status);
250 ovsdb_jsonrpc_server_free_remote_status(
251 struct ovsdb_jsonrpc_remote_status *status)
253 free(status->locks_held);
254 free(status->locks_waiting);
255 free(status->locks_lost);
258 /* Forces all of the JSON-RPC sessions managed by 'svr' to disconnect and
261 ovsdb_jsonrpc_server_reconnect(struct ovsdb_jsonrpc_server *svr)
263 struct shash_node *node;
265 SHASH_FOR_EACH (node, &svr->remotes) {
266 struct ovsdb_jsonrpc_remote *remote = node->data;
268 ovsdb_jsonrpc_session_reconnect_all(remote);
273 ovsdb_jsonrpc_server_run(struct ovsdb_jsonrpc_server *svr)
275 struct shash_node *node;
277 SHASH_FOR_EACH (node, &svr->remotes) {
278 struct ovsdb_jsonrpc_remote *remote = node->data;
280 if (remote->listener && svr->n_sessions < svr->max_sessions) {
281 struct stream *stream;
284 error = pstream_accept(remote->listener, &stream);
286 struct jsonrpc_session *js;
287 js = jsonrpc_session_open_unreliably(jsonrpc_open(stream),
289 ovsdb_jsonrpc_session_create(remote, js);
290 } else if (error != EAGAIN) {
291 VLOG_WARN_RL(&rl, "%s: accept failed: %s",
292 pstream_get_name(remote->listener),
297 ovsdb_jsonrpc_session_run_all(remote);
302 ovsdb_jsonrpc_server_wait(struct ovsdb_jsonrpc_server *svr)
304 struct shash_node *node;
306 SHASH_FOR_EACH (node, &svr->remotes) {
307 struct ovsdb_jsonrpc_remote *remote = node->data;
309 if (remote->listener && svr->n_sessions < svr->max_sessions) {
310 pstream_wait(remote->listener);
313 ovsdb_jsonrpc_session_wait_all(remote);
317 /* Adds some memory usage statistics for 'svr' into 'usage', for use with
318 * memory_report(). */
320 ovsdb_jsonrpc_server_get_memory_usage(const struct ovsdb_jsonrpc_server *svr,
323 struct shash_node *node;
325 simap_increase(usage, "sessions", svr->n_sessions);
326 SHASH_FOR_EACH (node, &svr->remotes) {
327 struct ovsdb_jsonrpc_remote *remote = node->data;
329 ovsdb_jsonrpc_session_get_memory_usage_all(remote, usage);
333 /* JSON-RPC database server session. */
335 struct ovsdb_jsonrpc_session {
336 struct list node; /* Element in remote's sessions list. */
337 struct ovsdb_session up;
338 struct ovsdb_jsonrpc_remote *remote;
341 struct hmap triggers; /* Hmap of "struct ovsdb_jsonrpc_trigger"s. */
344 struct hmap monitors; /* Hmap of "struct ovsdb_jsonrpc_monitor"s. */
346 /* Network connectivity. */
347 struct jsonrpc_session *js; /* JSON-RPC session. */
348 unsigned int js_seqno; /* Last jsonrpc_session_get_seqno() value. */
351 static void ovsdb_jsonrpc_session_close(struct ovsdb_jsonrpc_session *);
352 static int ovsdb_jsonrpc_session_run(struct ovsdb_jsonrpc_session *);
353 static void ovsdb_jsonrpc_session_wait(struct ovsdb_jsonrpc_session *);
354 static void ovsdb_jsonrpc_session_get_memory_usage(
355 const struct ovsdb_jsonrpc_session *, struct simap *usage);
356 static void ovsdb_jsonrpc_session_set_options(
357 struct ovsdb_jsonrpc_session *, const struct ovsdb_jsonrpc_options *);
358 static void ovsdb_jsonrpc_session_got_request(struct ovsdb_jsonrpc_session *,
359 struct jsonrpc_msg *);
360 static void ovsdb_jsonrpc_session_got_notify(struct ovsdb_jsonrpc_session *,
361 struct jsonrpc_msg *);
363 static struct ovsdb_jsonrpc_session *
364 ovsdb_jsonrpc_session_create(struct ovsdb_jsonrpc_remote *remote,
365 struct jsonrpc_session *js)
367 struct ovsdb_jsonrpc_session *s;
369 s = xzalloc(sizeof *s);
370 ovsdb_session_init(&s->up, &remote->server->up);
372 list_push_back(&remote->sessions, &s->node);
373 hmap_init(&s->triggers);
374 hmap_init(&s->monitors);
376 s->js_seqno = jsonrpc_session_get_seqno(js);
378 remote->server->n_sessions++;
384 ovsdb_jsonrpc_session_close(struct ovsdb_jsonrpc_session *s)
386 ovsdb_jsonrpc_monitor_remove_all(s);
387 ovsdb_jsonrpc_session_unlock_all(s);
388 ovsdb_jsonrpc_trigger_complete_all(s);
390 hmap_destroy(&s->monitors);
391 hmap_destroy(&s->triggers);
393 jsonrpc_session_close(s->js);
394 list_remove(&s->node);
395 s->remote->server->n_sessions--;
396 ovsdb_session_destroy(&s->up);
401 ovsdb_jsonrpc_session_run(struct ovsdb_jsonrpc_session *s)
403 jsonrpc_session_run(s->js);
404 if (s->js_seqno != jsonrpc_session_get_seqno(s->js)) {
405 s->js_seqno = jsonrpc_session_get_seqno(s->js);
406 ovsdb_jsonrpc_trigger_complete_all(s);
407 ovsdb_jsonrpc_monitor_remove_all(s);
408 ovsdb_jsonrpc_session_unlock_all(s);
411 ovsdb_jsonrpc_trigger_complete_done(s);
413 if (!jsonrpc_session_get_backlog(s->js)) {
414 struct jsonrpc_msg *msg = jsonrpc_session_recv(s->js);
416 if (msg->type == JSONRPC_REQUEST) {
417 ovsdb_jsonrpc_session_got_request(s, msg);
418 } else if (msg->type == JSONRPC_NOTIFY) {
419 ovsdb_jsonrpc_session_got_notify(s, msg);
421 VLOG_WARN("%s: received unexpected %s message",
422 jsonrpc_session_get_name(s->js),
423 jsonrpc_msg_type_to_string(msg->type));
424 jsonrpc_session_force_reconnect(s->js);
425 jsonrpc_msg_destroy(msg);
429 return jsonrpc_session_is_alive(s->js) ? 0 : ETIMEDOUT;
433 ovsdb_jsonrpc_session_set_options(struct ovsdb_jsonrpc_session *session,
434 const struct ovsdb_jsonrpc_options *options)
436 jsonrpc_session_set_max_backoff(session->js, options->max_backoff);
437 jsonrpc_session_set_probe_interval(session->js, options->probe_interval);
438 jsonrpc_session_set_dscp(session->js, options->dscp);
442 ovsdb_jsonrpc_session_run_all(struct ovsdb_jsonrpc_remote *remote)
444 struct ovsdb_jsonrpc_session *s, *next;
446 LIST_FOR_EACH_SAFE (s, next, node, &remote->sessions) {
447 int error = ovsdb_jsonrpc_session_run(s);
449 ovsdb_jsonrpc_session_close(s);
455 ovsdb_jsonrpc_session_wait(struct ovsdb_jsonrpc_session *s)
457 jsonrpc_session_wait(s->js);
458 if (!jsonrpc_session_get_backlog(s->js)) {
459 jsonrpc_session_recv_wait(s->js);
464 ovsdb_jsonrpc_session_wait_all(struct ovsdb_jsonrpc_remote *remote)
466 struct ovsdb_jsonrpc_session *s;
468 LIST_FOR_EACH (s, node, &remote->sessions) {
469 ovsdb_jsonrpc_session_wait(s);
474 ovsdb_jsonrpc_session_get_memory_usage(const struct ovsdb_jsonrpc_session *s,
477 simap_increase(usage, "triggers", hmap_count(&s->triggers));
478 simap_increase(usage, "monitors", hmap_count(&s->monitors));
479 simap_increase(usage, "backlog", jsonrpc_session_get_backlog(s->js));
483 ovsdb_jsonrpc_session_get_memory_usage_all(
484 const struct ovsdb_jsonrpc_remote *remote,
487 struct ovsdb_jsonrpc_session *s;
489 LIST_FOR_EACH (s, node, &remote->sessions) {
490 ovsdb_jsonrpc_session_get_memory_usage(s, usage);
495 ovsdb_jsonrpc_session_close_all(struct ovsdb_jsonrpc_remote *remote)
497 struct ovsdb_jsonrpc_session *s, *next;
499 LIST_FOR_EACH_SAFE (s, next, node, &remote->sessions) {
500 ovsdb_jsonrpc_session_close(s);
504 /* Forces all of the JSON-RPC sessions managed by 'remote' to disconnect and
507 ovsdb_jsonrpc_session_reconnect_all(struct ovsdb_jsonrpc_remote *remote)
509 struct ovsdb_jsonrpc_session *s, *next;
511 LIST_FOR_EACH_SAFE (s, next, node, &remote->sessions) {
512 jsonrpc_session_force_reconnect(s->js);
513 if (!jsonrpc_session_is_alive(s->js)) {
514 ovsdb_jsonrpc_session_close(s);
519 /* Sets the options for all of the JSON-RPC sessions managed by 'remote' to
522 ovsdb_jsonrpc_session_set_all_options(
523 struct ovsdb_jsonrpc_remote *remote,
524 const struct ovsdb_jsonrpc_options *options)
526 struct ovsdb_jsonrpc_session *s;
528 if (remote->listener) {
531 error = pstream_set_dscp(remote->listener, options->dscp);
533 VLOG_ERR("%s: set_dscp failed %s",
534 pstream_get_name(remote->listener), strerror(error));
536 remote->dscp = options->dscp;
539 * TODO:XXX race window between setting dscp to listening socket
540 * and accepting socket. Accepted socket may have old dscp value.
541 * Ignore this race window for now.
544 LIST_FOR_EACH (s, node, &remote->sessions) {
545 ovsdb_jsonrpc_session_set_options(s, options);
550 ovsdb_jsonrpc_session_get_status(const struct ovsdb_jsonrpc_remote *remote,
551 struct ovsdb_jsonrpc_remote_status *status)
553 const struct ovsdb_jsonrpc_session *s;
554 const struct jsonrpc_session *js;
555 struct ovsdb_lock_waiter *waiter;
556 struct reconnect_stats rstats;
557 struct ds locks_held, locks_waiting, locks_lost;
559 if (list_is_empty(&remote->sessions)) {
562 s = CONTAINER_OF(remote->sessions.next, struct ovsdb_jsonrpc_session, node);
565 status->is_connected = jsonrpc_session_is_connected(js);
566 status->last_error = jsonrpc_session_get_status(js);
568 jsonrpc_session_get_reconnect_stats(js, &rstats);
569 status->state = rstats.state;
570 status->sec_since_connect = rstats.msec_since_connect == UINT_MAX
571 ? UINT_MAX : rstats.msec_since_connect / 1000;
572 status->sec_since_disconnect = rstats.msec_since_disconnect == UINT_MAX
573 ? UINT_MAX : rstats.msec_since_disconnect / 1000;
575 ds_init(&locks_held);
576 ds_init(&locks_waiting);
577 ds_init(&locks_lost);
578 HMAP_FOR_EACH (waiter, session_node, &s->up.waiters) {
581 string = (ovsdb_lock_waiter_is_owner(waiter) ? &locks_held
582 : waiter->mode == OVSDB_LOCK_WAIT ? &locks_waiting
584 if (string->length) {
585 ds_put_char(string, ' ');
587 ds_put_cstr(string, waiter->lock_name);
589 status->locks_held = ds_steal_cstr(&locks_held);
590 status->locks_waiting = ds_steal_cstr(&locks_waiting);
591 status->locks_lost = ds_steal_cstr(&locks_lost);
593 status->n_connections = list_size(&remote->sessions);
598 /* Examines 'request' to determine the database to which it relates, and then
599 * searches 's' to find that database:
601 * - If successful, returns the database and sets '*replyp' to NULL.
603 * - If no such database exists, returns NULL and sets '*replyp' to an
604 * appropriate JSON-RPC error reply, owned by the caller. */
605 static struct ovsdb *
606 ovsdb_jsonrpc_lookup_db(const struct ovsdb_jsonrpc_session *s,
607 const struct jsonrpc_msg *request,
608 struct jsonrpc_msg **replyp)
610 struct json_array *params;
611 struct ovsdb_error *error;
615 params = json_array(request->params);
616 if (!params->n || params->elems[0]->type != JSON_STRING) {
617 error = ovsdb_syntax_error(
618 request->params, NULL,
619 "%s request params must begin with <db-name>", request->method);
623 db_name = params->elems[0]->u.string;
624 db = shash_find_data(&s->up.server->dbs, db_name);
626 error = ovsdb_syntax_error(
627 request->params, "unknown database",
628 "%s request specifies unknown database %s",
629 request->method, db_name);
637 *replyp = jsonrpc_create_reply(ovsdb_error_to_json(error), request->id);
638 ovsdb_error_destroy(error);
642 static struct ovsdb_error *
643 ovsdb_jsonrpc_session_parse_lock_name(const struct jsonrpc_msg *request,
644 const char **lock_namep)
646 const struct json_array *params;
648 params = json_array(request->params);
649 if (params->n != 1 || params->elems[0]->type != JSON_STRING ||
650 !ovsdb_parser_is_id(json_string(params->elems[0]))) {
652 return ovsdb_syntax_error(request->params, NULL,
653 "%s request params must be <id>",
657 *lock_namep = json_string(params->elems[0]);
662 ovsdb_jsonrpc_session_notify(struct ovsdb_session *session,
663 const char *lock_name,
666 struct ovsdb_jsonrpc_session *s;
669 s = CONTAINER_OF(session, struct ovsdb_jsonrpc_session, up);
670 params = json_array_create_1(json_string_create(lock_name));
671 jsonrpc_session_send(s->js, jsonrpc_create_notify(method, params));
674 static struct jsonrpc_msg *
675 ovsdb_jsonrpc_session_lock(struct ovsdb_jsonrpc_session *s,
676 struct jsonrpc_msg *request,
677 enum ovsdb_lock_mode mode)
679 struct ovsdb_lock_waiter *waiter;
680 struct jsonrpc_msg *reply;
681 struct ovsdb_error *error;
682 struct ovsdb_session *victim;
683 const char *lock_name;
686 error = ovsdb_jsonrpc_session_parse_lock_name(request, &lock_name);
691 /* Report error if this session has issued a "lock" or "steal" without a
692 * matching "unlock" for this lock. */
693 waiter = ovsdb_session_get_lock_waiter(&s->up, lock_name);
695 error = ovsdb_syntax_error(
696 request->params, NULL,
697 "must issue \"unlock\" before new \"%s\"", request->method);
701 /* Get the lock, add us as a waiter. */
702 waiter = ovsdb_server_lock(&s->remote->server->up, &s->up, lock_name, mode,
705 ovsdb_jsonrpc_session_notify(victim, lock_name, "stolen");
708 result = json_object_create();
709 json_object_put(result, "locked",
710 json_boolean_create(ovsdb_lock_waiter_is_owner(waiter)));
712 return jsonrpc_create_reply(result, request->id);
715 reply = jsonrpc_create_reply(ovsdb_error_to_json(error), request->id);
716 ovsdb_error_destroy(error);
721 ovsdb_jsonrpc_session_unlock_all(struct ovsdb_jsonrpc_session *s)
723 struct ovsdb_lock_waiter *waiter, *next;
725 HMAP_FOR_EACH_SAFE (waiter, next, session_node, &s->up.waiters) {
726 ovsdb_jsonrpc_session_unlock__(waiter);
731 ovsdb_jsonrpc_session_unlock__(struct ovsdb_lock_waiter *waiter)
733 struct ovsdb_lock *lock = waiter->lock;
736 struct ovsdb_session *new_owner = ovsdb_lock_waiter_remove(waiter);
738 ovsdb_jsonrpc_session_notify(new_owner, lock->name, "locked");
740 /* ovsdb_server_lock() might have freed 'lock'. */
744 ovsdb_lock_waiter_destroy(waiter);
747 static struct jsonrpc_msg *
748 ovsdb_jsonrpc_session_unlock(struct ovsdb_jsonrpc_session *s,
749 struct jsonrpc_msg *request)
751 struct ovsdb_lock_waiter *waiter;
752 struct jsonrpc_msg *reply;
753 struct ovsdb_error *error;
754 const char *lock_name;
756 error = ovsdb_jsonrpc_session_parse_lock_name(request, &lock_name);
761 /* Report error if this session has not issued a "lock" or "steal" for this
763 waiter = ovsdb_session_get_lock_waiter(&s->up, lock_name);
765 error = ovsdb_syntax_error(
766 request->params, NULL, "\"unlock\" without \"lock\" or \"steal\"");
770 ovsdb_jsonrpc_session_unlock__(waiter);
772 return jsonrpc_create_reply(json_object_create(), request->id);
775 reply = jsonrpc_create_reply(ovsdb_error_to_json(error), request->id);
776 ovsdb_error_destroy(error);
780 static struct jsonrpc_msg *
781 execute_transaction(struct ovsdb_jsonrpc_session *s, struct ovsdb *db,
782 struct jsonrpc_msg *request)
784 ovsdb_jsonrpc_trigger_create(s, db, request->id, request->params);
786 request->params = NULL;
787 jsonrpc_msg_destroy(request);
792 ovsdb_jsonrpc_session_got_request(struct ovsdb_jsonrpc_session *s,
793 struct jsonrpc_msg *request)
795 struct jsonrpc_msg *reply;
797 if (!strcmp(request->method, "transact")) {
798 struct ovsdb *db = ovsdb_jsonrpc_lookup_db(s, request, &reply);
800 reply = execute_transaction(s, db, request);
802 } else if (!strcmp(request->method, "monitor")) {
803 struct ovsdb *db = ovsdb_jsonrpc_lookup_db(s, request, &reply);
805 reply = jsonrpc_create_reply(
806 ovsdb_jsonrpc_monitor_create(s, db, request->params),
809 } else if (!strcmp(request->method, "monitor_cancel")) {
810 reply = ovsdb_jsonrpc_monitor_cancel(s, json_array(request->params),
812 } else if (!strcmp(request->method, "get_schema")) {
813 struct ovsdb *db = ovsdb_jsonrpc_lookup_db(s, request, &reply);
815 reply = jsonrpc_create_reply(ovsdb_schema_to_json(db->schema),
818 } else if (!strcmp(request->method, "list_dbs")) {
819 size_t n_dbs = shash_count(&s->up.server->dbs);
820 struct shash_node *node;
824 dbs = xmalloc(n_dbs * sizeof *dbs);
826 SHASH_FOR_EACH (node, &s->up.server->dbs) {
827 dbs[i++] = json_string_create(node->name);
829 reply = jsonrpc_create_reply(json_array_create(dbs, n_dbs),
831 } else if (!strcmp(request->method, "lock")) {
832 reply = ovsdb_jsonrpc_session_lock(s, request, OVSDB_LOCK_WAIT);
833 } else if (!strcmp(request->method, "steal")) {
834 reply = ovsdb_jsonrpc_session_lock(s, request, OVSDB_LOCK_STEAL);
835 } else if (!strcmp(request->method, "unlock")) {
836 reply = ovsdb_jsonrpc_session_unlock(s, request);
837 } else if (!strcmp(request->method, "echo")) {
838 reply = jsonrpc_create_reply(json_clone(request->params), request->id);
840 reply = jsonrpc_create_error(json_string_create("unknown method"),
845 jsonrpc_msg_destroy(request);
846 jsonrpc_session_send(s->js, reply);
851 execute_cancel(struct ovsdb_jsonrpc_session *s, struct jsonrpc_msg *request)
853 if (json_array(request->params)->n == 1) {
854 struct ovsdb_jsonrpc_trigger *t;
857 id = request->params->u.array.elems[0];
858 t = ovsdb_jsonrpc_trigger_find(s, id, json_hash(id, 0));
860 ovsdb_jsonrpc_trigger_complete(t);
866 ovsdb_jsonrpc_session_got_notify(struct ovsdb_jsonrpc_session *s,
867 struct jsonrpc_msg *request)
869 if (!strcmp(request->method, "cancel")) {
870 execute_cancel(s, request);
872 jsonrpc_msg_destroy(request);
875 /* JSON-RPC database server triggers.
877 * (Every transaction is treated as a trigger even if it doesn't actually have
878 * any "wait" operations.) */
880 struct ovsdb_jsonrpc_trigger {
881 struct ovsdb_trigger trigger;
882 struct hmap_node hmap_node; /* In session's "triggers" hmap. */
887 ovsdb_jsonrpc_trigger_create(struct ovsdb_jsonrpc_session *s, struct ovsdb *db,
888 struct json *id, struct json *params)
890 struct ovsdb_jsonrpc_trigger *t;
893 /* Check for duplicate ID. */
894 hash = json_hash(id, 0);
895 t = ovsdb_jsonrpc_trigger_find(s, id, hash);
897 struct jsonrpc_msg *msg;
899 msg = jsonrpc_create_error(json_string_create("duplicate request ID"),
901 jsonrpc_session_send(s->js, msg);
903 json_destroy(params);
907 /* Insert into trigger table. */
908 t = xmalloc(sizeof *t);
909 ovsdb_trigger_init(&s->up, db, &t->trigger, params, time_msec());
911 hmap_insert(&s->triggers, &t->hmap_node, hash);
913 /* Complete early if possible. */
914 if (ovsdb_trigger_is_complete(&t->trigger)) {
915 ovsdb_jsonrpc_trigger_complete(t);
919 static struct ovsdb_jsonrpc_trigger *
920 ovsdb_jsonrpc_trigger_find(struct ovsdb_jsonrpc_session *s,
921 const struct json *id, size_t hash)
923 struct ovsdb_jsonrpc_trigger *t;
925 HMAP_FOR_EACH_WITH_HASH (t, hmap_node, hash, &s->triggers) {
926 if (json_equal(t->id, id)) {
935 ovsdb_jsonrpc_trigger_complete(struct ovsdb_jsonrpc_trigger *t)
937 struct ovsdb_jsonrpc_session *s;
939 s = CONTAINER_OF(t->trigger.session, struct ovsdb_jsonrpc_session, up);
941 if (jsonrpc_session_is_connected(s->js)) {
942 struct jsonrpc_msg *reply;
945 result = ovsdb_trigger_steal_result(&t->trigger);
947 reply = jsonrpc_create_reply(result, t->id);
949 reply = jsonrpc_create_error(json_string_create("canceled"),
952 jsonrpc_session_send(s->js, reply);
956 ovsdb_trigger_destroy(&t->trigger);
957 hmap_remove(&s->triggers, &t->hmap_node);
962 ovsdb_jsonrpc_trigger_complete_all(struct ovsdb_jsonrpc_session *s)
964 struct ovsdb_jsonrpc_trigger *t, *next;
965 HMAP_FOR_EACH_SAFE (t, next, hmap_node, &s->triggers) {
966 ovsdb_jsonrpc_trigger_complete(t);
971 ovsdb_jsonrpc_trigger_complete_done(struct ovsdb_jsonrpc_session *s)
973 while (!list_is_empty(&s->up.completions)) {
974 struct ovsdb_jsonrpc_trigger *t
975 = CONTAINER_OF(s->up.completions.next,
976 struct ovsdb_jsonrpc_trigger, trigger.node);
977 ovsdb_jsonrpc_trigger_complete(t);
981 /* JSON-RPC database table monitors. */
983 enum ovsdb_jsonrpc_monitor_selection {
984 OJMS_INITIAL = 1 << 0, /* All rows when monitor is created. */
985 OJMS_INSERT = 1 << 1, /* New rows. */
986 OJMS_DELETE = 1 << 2, /* Deleted rows. */
987 OJMS_MODIFY = 1 << 3 /* Modified rows. */
990 /* A particular column being monitored. */
991 struct ovsdb_jsonrpc_monitor_column {
992 const struct ovsdb_column *column;
993 enum ovsdb_jsonrpc_monitor_selection select;
996 /* A particular table being monitored. */
997 struct ovsdb_jsonrpc_monitor_table {
998 const struct ovsdb_table *table;
1000 /* This is the union (bitwise-OR) of the 'select' values in all of the
1001 * members of 'columns' below. */
1002 enum ovsdb_jsonrpc_monitor_selection select;
1004 /* Columns being monitored. */
1005 struct ovsdb_jsonrpc_monitor_column *columns;
1009 /* A collection of tables being monitored. */
1010 struct ovsdb_jsonrpc_monitor {
1011 struct ovsdb_replica replica;
1012 struct ovsdb_jsonrpc_session *session;
1014 struct hmap_node node; /* In ovsdb_jsonrpc_session's "monitors". */
1016 struct json *monitor_id;
1017 struct shash tables; /* Holds "struct ovsdb_jsonrpc_monitor_table"s. */
1020 static const struct ovsdb_replica_class ovsdb_jsonrpc_replica_class;
1022 struct ovsdb_jsonrpc_monitor *ovsdb_jsonrpc_monitor_find(
1023 struct ovsdb_jsonrpc_session *, const struct json *monitor_id);
1024 static void ovsdb_jsonrpc_monitor_destroy(struct ovsdb_replica *);
1025 static struct json *ovsdb_jsonrpc_monitor_get_initial(
1026 const struct ovsdb_jsonrpc_monitor *);
1029 parse_bool(struct ovsdb_parser *parser, const char *name, bool default_value)
1031 const struct json *json;
1033 json = ovsdb_parser_member(parser, name, OP_BOOLEAN | OP_OPTIONAL);
1034 return json ? json_boolean(json) : default_value;
1037 struct ovsdb_jsonrpc_monitor *
1038 ovsdb_jsonrpc_monitor_find(struct ovsdb_jsonrpc_session *s,
1039 const struct json *monitor_id)
1041 struct ovsdb_jsonrpc_monitor *m;
1043 HMAP_FOR_EACH_WITH_HASH (m, node, json_hash(monitor_id, 0), &s->monitors) {
1044 if (json_equal(m->monitor_id, monitor_id)) {
1053 ovsdb_jsonrpc_add_monitor_column(struct ovsdb_jsonrpc_monitor_table *mt,
1054 const struct ovsdb_column *column,
1055 enum ovsdb_jsonrpc_monitor_selection select,
1056 size_t *allocated_columns)
1058 struct ovsdb_jsonrpc_monitor_column *c;
1060 if (mt->n_columns >= *allocated_columns) {
1061 mt->columns = x2nrealloc(mt->columns, allocated_columns,
1062 sizeof *mt->columns);
1065 c = &mt->columns[mt->n_columns++];
1071 compare_ovsdb_jsonrpc_monitor_column(const void *a_, const void *b_)
1073 const struct ovsdb_jsonrpc_monitor_column *a = a_;
1074 const struct ovsdb_jsonrpc_monitor_column *b = b_;
1076 return a->column < b->column ? -1 : a->column > b->column;
1079 static struct ovsdb_error * WARN_UNUSED_RESULT
1080 ovsdb_jsonrpc_parse_monitor_request(struct ovsdb_jsonrpc_monitor_table *mt,
1081 const struct json *monitor_request,
1082 size_t *allocated_columns)
1084 const struct ovsdb_table_schema *ts = mt->table->schema;
1085 enum ovsdb_jsonrpc_monitor_selection select;
1086 const struct json *columns, *select_json;
1087 struct ovsdb_parser parser;
1088 struct ovsdb_error *error;
1090 ovsdb_parser_init(&parser, monitor_request, "table %s", ts->name);
1091 columns = ovsdb_parser_member(&parser, "columns", OP_ARRAY | OP_OPTIONAL);
1092 select_json = ovsdb_parser_member(&parser, "select",
1093 OP_OBJECT | OP_OPTIONAL);
1094 error = ovsdb_parser_finish(&parser);
1101 ovsdb_parser_init(&parser, select_json, "table %s select", ts->name);
1102 if (parse_bool(&parser, "initial", true)) {
1103 select |= OJMS_INITIAL;
1105 if (parse_bool(&parser, "insert", true)) {
1106 select |= OJMS_INSERT;
1108 if (parse_bool(&parser, "delete", true)) {
1109 select |= OJMS_DELETE;
1111 if (parse_bool(&parser, "modify", true)) {
1112 select |= OJMS_MODIFY;
1114 error = ovsdb_parser_finish(&parser);
1119 select = OJMS_INITIAL | OJMS_INSERT | OJMS_DELETE | OJMS_MODIFY;
1121 mt->select |= select;
1126 if (columns->type != JSON_ARRAY) {
1127 return ovsdb_syntax_error(columns, NULL,
1128 "array of column names expected");
1131 for (i = 0; i < columns->u.array.n; i++) {
1132 const struct ovsdb_column *column;
1135 if (columns->u.array.elems[i]->type != JSON_STRING) {
1136 return ovsdb_syntax_error(columns, NULL,
1137 "array of column names expected");
1140 s = columns->u.array.elems[i]->u.string;
1141 column = shash_find_data(&mt->table->schema->columns, s);
1143 return ovsdb_syntax_error(columns, NULL, "%s is not a valid "
1146 ovsdb_jsonrpc_add_monitor_column(mt, column, select,
1150 struct shash_node *node;
1152 SHASH_FOR_EACH (node, &ts->columns) {
1153 const struct ovsdb_column *column = node->data;
1154 if (column->index != OVSDB_COL_UUID) {
1155 ovsdb_jsonrpc_add_monitor_column(mt, column, select,
1164 static struct json *
1165 ovsdb_jsonrpc_monitor_create(struct ovsdb_jsonrpc_session *s, struct ovsdb *db,
1166 struct json *params)
1168 struct ovsdb_jsonrpc_monitor *m = NULL;
1169 struct json *monitor_id, *monitor_requests;
1170 struct ovsdb_error *error = NULL;
1171 struct shash_node *node;
1174 if (json_array(params)->n != 3) {
1175 error = ovsdb_syntax_error(params, NULL, "invalid parameters");
1178 monitor_id = params->u.array.elems[1];
1179 monitor_requests = params->u.array.elems[2];
1180 if (monitor_requests->type != JSON_OBJECT) {
1181 error = ovsdb_syntax_error(monitor_requests, NULL,
1182 "monitor-requests must be object");
1186 if (ovsdb_jsonrpc_monitor_find(s, monitor_id)) {
1187 error = ovsdb_syntax_error(monitor_id, NULL, "duplicate monitor ID");
1191 m = xzalloc(sizeof *m);
1192 ovsdb_replica_init(&m->replica, &ovsdb_jsonrpc_replica_class);
1193 ovsdb_add_replica(db, &m->replica);
1196 hmap_insert(&s->monitors, &m->node, json_hash(monitor_id, 0));
1197 m->monitor_id = json_clone(monitor_id);
1198 shash_init(&m->tables);
1200 SHASH_FOR_EACH (node, json_object(monitor_requests)) {
1201 const struct ovsdb_table *table;
1202 struct ovsdb_jsonrpc_monitor_table *mt;
1203 size_t allocated_columns;
1204 const struct json *mr_value;
1207 table = ovsdb_get_table(m->db, node->name);
1209 error = ovsdb_syntax_error(NULL, NULL,
1210 "no table named %s", node->name);
1214 mt = xzalloc(sizeof *mt);
1216 shash_add(&m->tables, table->schema->name, mt);
1218 /* Parse columns. */
1219 mr_value = node->data;
1220 allocated_columns = 0;
1221 if (mr_value->type == JSON_ARRAY) {
1222 const struct json_array *array = &mr_value->u.array;
1224 for (i = 0; i < array->n; i++) {
1225 error = ovsdb_jsonrpc_parse_monitor_request(
1226 mt, array->elems[i], &allocated_columns);
1232 error = ovsdb_jsonrpc_parse_monitor_request(
1233 mt, mr_value, &allocated_columns);
1239 /* Check for duplicate columns. */
1240 qsort(mt->columns, mt->n_columns, sizeof *mt->columns,
1241 compare_ovsdb_jsonrpc_monitor_column);
1242 for (i = 1; i < mt->n_columns; i++) {
1243 if (mt->columns[i].column == mt->columns[i - 1].column) {
1244 error = ovsdb_syntax_error(mr_value, NULL, "column %s "
1245 "mentioned more than once",
1246 mt->columns[i].column->name);
1252 return ovsdb_jsonrpc_monitor_get_initial(m);
1256 ovsdb_remove_replica(m->db, &m->replica);
1259 json = ovsdb_error_to_json(error);
1260 ovsdb_error_destroy(error);
1264 static struct jsonrpc_msg *
1265 ovsdb_jsonrpc_monitor_cancel(struct ovsdb_jsonrpc_session *s,
1266 struct json_array *params,
1267 const struct json *request_id)
1269 if (params->n != 1) {
1270 return jsonrpc_create_error(json_string_create("invalid parameters"),
1273 struct ovsdb_jsonrpc_monitor *m;
1275 m = ovsdb_jsonrpc_monitor_find(s, params->elems[0]);
1277 return jsonrpc_create_error(json_string_create("unknown monitor"),
1280 ovsdb_remove_replica(m->db, &m->replica);
1281 return jsonrpc_create_reply(json_object_create(), request_id);
1287 ovsdb_jsonrpc_monitor_remove_all(struct ovsdb_jsonrpc_session *s)
1289 struct ovsdb_jsonrpc_monitor *m, *next;
1291 HMAP_FOR_EACH_SAFE (m, next, node, &s->monitors) {
1292 ovsdb_remove_replica(m->db, &m->replica);
1296 static struct ovsdb_jsonrpc_monitor *
1297 ovsdb_jsonrpc_monitor_cast(struct ovsdb_replica *replica)
1299 assert(replica->class == &ovsdb_jsonrpc_replica_class);
1300 return CONTAINER_OF(replica, struct ovsdb_jsonrpc_monitor, replica);
1303 struct ovsdb_jsonrpc_monitor_aux {
1304 bool initial; /* Sending initial contents of table? */
1305 const struct ovsdb_jsonrpc_monitor *monitor;
1306 struct json *json; /* JSON for the whole transaction. */
1308 /* Current table. */
1309 struct ovsdb_jsonrpc_monitor_table *mt;
1310 struct json *table_json; /* JSON for table's transaction. */
1314 any_reportable_change(const struct ovsdb_jsonrpc_monitor_table *mt,
1315 const unsigned long int *changed)
1319 for (i = 0; i < mt->n_columns; i++) {
1320 const struct ovsdb_jsonrpc_monitor_column *c = &mt->columns[i];
1321 unsigned int idx = c->column->index;
1323 if (c->select & OJMS_MODIFY && bitmap_is_set(changed, idx)) {
1332 ovsdb_jsonrpc_monitor_change_cb(const struct ovsdb_row *old,
1333 const struct ovsdb_row *new,
1334 const unsigned long int *changed,
1337 struct ovsdb_jsonrpc_monitor_aux *aux = aux_;
1338 const struct ovsdb_jsonrpc_monitor *m = aux->monitor;
1339 struct ovsdb_table *table = new ? new->table : old->table;
1340 enum ovsdb_jsonrpc_monitor_selection type;
1341 struct json *old_json, *new_json;
1342 struct json *row_json;
1343 char uuid[UUID_LEN + 1];
1346 if (!aux->mt || table != aux->mt->table) {
1347 aux->mt = shash_find_data(&m->tables, table->schema->name);
1348 aux->table_json = NULL;
1350 /* We don't care about rows in this table at all. Tell the caller
1356 type = (aux->initial ? OJMS_INITIAL
1357 : !old ? OJMS_INSERT
1358 : !new ? OJMS_DELETE
1360 if (!(aux->mt->select & type)) {
1361 /* We don't care about this type of change (but do want to be called
1362 * back for changes to other rows in the same table). */
1366 if (type == OJMS_MODIFY && !any_reportable_change(aux->mt, changed)) {
1367 /* Nothing of interest changed. */
1371 old_json = new_json = NULL;
1372 if (type & (OJMS_DELETE | OJMS_MODIFY)) {
1373 old_json = json_object_create();
1375 if (type & (OJMS_INITIAL | OJMS_INSERT | OJMS_MODIFY)) {
1376 new_json = json_object_create();
1378 for (i = 0; i < aux->mt->n_columns; i++) {
1379 const struct ovsdb_jsonrpc_monitor_column *c = &aux->mt->columns[i];
1380 const struct ovsdb_column *column = c->column;
1381 unsigned int idx = c->column->index;
1383 if (!(type & c->select)) {
1384 /* We don't care about this type of change for this particular
1385 * column (but we will care about it for some other column). */
1389 if ((type == OJMS_MODIFY && bitmap_is_set(changed, idx))
1390 || type == OJMS_DELETE) {
1391 json_object_put(old_json, column->name,
1392 ovsdb_datum_to_json(&old->fields[idx],
1395 if (type & (OJMS_INITIAL | OJMS_INSERT | OJMS_MODIFY)) {
1396 json_object_put(new_json, column->name,
1397 ovsdb_datum_to_json(&new->fields[idx],
1402 /* Create JSON object for transaction overall. */
1404 aux->json = json_object_create();
1407 /* Create JSON object for transaction on this table. */
1408 if (!aux->table_json) {
1409 aux->table_json = json_object_create();
1410 json_object_put(aux->json, aux->mt->table->schema->name,
1414 /* Create JSON object for transaction on this row. */
1415 row_json = json_object_create();
1417 json_object_put(row_json, "old", old_json);
1420 json_object_put(row_json, "new", new_json);
1423 /* Add JSON row to JSON table. */
1424 snprintf(uuid, sizeof uuid,
1425 UUID_FMT, UUID_ARGS(ovsdb_row_get_uuid(new ? new : old)));
1426 json_object_put(aux->table_json, uuid, row_json);
1432 ovsdb_jsonrpc_monitor_init_aux(struct ovsdb_jsonrpc_monitor_aux *aux,
1433 const struct ovsdb_jsonrpc_monitor *m,
1436 aux->initial = initial;
1440 aux->table_json = NULL;
1443 static struct ovsdb_error *
1444 ovsdb_jsonrpc_monitor_commit(struct ovsdb_replica *replica,
1445 const struct ovsdb_txn *txn,
1446 bool durable OVS_UNUSED)
1448 struct ovsdb_jsonrpc_monitor *m = ovsdb_jsonrpc_monitor_cast(replica);
1449 struct ovsdb_jsonrpc_monitor_aux aux;
1451 ovsdb_jsonrpc_monitor_init_aux(&aux, m, false);
1452 ovsdb_txn_for_each_change(txn, ovsdb_jsonrpc_monitor_change_cb, &aux);
1454 struct jsonrpc_msg *msg;
1455 struct json *params;
1457 params = json_array_create_2(json_clone(aux.monitor->monitor_id),
1459 msg = jsonrpc_create_notify("update", params);
1460 jsonrpc_session_send(aux.monitor->session->js, msg);
1466 static struct json *
1467 ovsdb_jsonrpc_monitor_get_initial(const struct ovsdb_jsonrpc_monitor *m)
1469 struct ovsdb_jsonrpc_monitor_aux aux;
1470 struct shash_node *node;
1472 ovsdb_jsonrpc_monitor_init_aux(&aux, m, true);
1473 SHASH_FOR_EACH (node, &m->tables) {
1474 struct ovsdb_jsonrpc_monitor_table *mt = node->data;
1476 if (mt->select & OJMS_INITIAL) {
1477 struct ovsdb_row *row;
1479 HMAP_FOR_EACH (row, hmap_node, &mt->table->rows) {
1480 ovsdb_jsonrpc_monitor_change_cb(NULL, row, NULL, &aux);
1484 return aux.json ? aux.json : json_object_create();
1488 ovsdb_jsonrpc_monitor_destroy(struct ovsdb_replica *replica)
1490 struct ovsdb_jsonrpc_monitor *m = ovsdb_jsonrpc_monitor_cast(replica);
1491 struct shash_node *node;
1493 json_destroy(m->monitor_id);
1494 SHASH_FOR_EACH (node, &m->tables) {
1495 struct ovsdb_jsonrpc_monitor_table *mt = node->data;
1499 shash_destroy(&m->tables);
1500 hmap_remove(&m->session->monitors, &m->node);
1504 static const struct ovsdb_replica_class ovsdb_jsonrpc_replica_class = {
1505 ovsdb_jsonrpc_monitor_commit,
1506 ovsdb_jsonrpc_monitor_destroy