1 /* Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014 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"
24 #include "dynamic-string.h"
27 #include "ovsdb-error.h"
28 #include "ovsdb-parser.h"
30 #include "poll-loop.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 *);
66 static void ovsdb_jsonrpc_session_send(struct ovsdb_jsonrpc_session *,
67 struct jsonrpc_msg *);
70 static void ovsdb_jsonrpc_trigger_create(struct ovsdb_jsonrpc_session *,
72 struct json *id, struct json *params);
73 static struct ovsdb_jsonrpc_trigger *ovsdb_jsonrpc_trigger_find(
74 struct ovsdb_jsonrpc_session *, const struct json *id, size_t hash);
75 static void ovsdb_jsonrpc_trigger_complete(struct ovsdb_jsonrpc_trigger *);
76 static void ovsdb_jsonrpc_trigger_complete_all(struct ovsdb_jsonrpc_session *);
77 static void ovsdb_jsonrpc_trigger_complete_done(
78 struct ovsdb_jsonrpc_session *);
81 static struct json *ovsdb_jsonrpc_monitor_create(
82 struct ovsdb_jsonrpc_session *, struct ovsdb *, struct json *params);
83 static struct jsonrpc_msg *ovsdb_jsonrpc_monitor_cancel(
84 struct ovsdb_jsonrpc_session *,
85 struct json_array *params,
86 const struct json *request_id);
87 static void ovsdb_jsonrpc_monitor_remove_all(struct ovsdb_jsonrpc_session *);
88 static void ovsdb_jsonrpc_monitor_flush_all(struct ovsdb_jsonrpc_session *);
89 static bool ovsdb_jsonrpc_monitor_needs_flush(struct ovsdb_jsonrpc_session *);
91 /* JSON-RPC database server. */
93 struct ovsdb_jsonrpc_server {
94 struct ovsdb_server up;
95 unsigned int n_sessions, max_sessions;
96 struct shash remotes; /* Contains "struct ovsdb_jsonrpc_remote *"s. */
99 /* A configured remote. This is either a passive stream listener plus a list
100 * of the currently connected sessions, or a list of exactly one active
102 struct ovsdb_jsonrpc_remote {
103 struct ovsdb_jsonrpc_server *server;
104 struct pstream *listener; /* Listener, if passive. */
105 struct list sessions; /* List of "struct ovsdb_jsonrpc_session"s. */
109 static struct ovsdb_jsonrpc_remote *ovsdb_jsonrpc_server_add_remote(
110 struct ovsdb_jsonrpc_server *, const char *name,
111 const struct ovsdb_jsonrpc_options *options
113 static void ovsdb_jsonrpc_server_del_remote(struct shash_node *);
115 /* Creates and returns a new server to provide JSON-RPC access to an OVSDB.
117 * The caller must call ovsdb_jsonrpc_server_add_db() for each database to
118 * which 'server' should provide access. */
119 struct ovsdb_jsonrpc_server *
120 ovsdb_jsonrpc_server_create(void)
122 struct ovsdb_jsonrpc_server *server = xzalloc(sizeof *server);
123 ovsdb_server_init(&server->up);
124 server->max_sessions = 64;
125 shash_init(&server->remotes);
129 /* Adds 'db' to the set of databases served out by 'svr'. Returns true if
130 * successful, false if 'db''s name is the same as some database already in
133 ovsdb_jsonrpc_server_add_db(struct ovsdb_jsonrpc_server *svr, struct ovsdb *db)
135 /* The OVSDB protocol doesn't have a way to notify a client that a
136 * database has been added. If some client tried to use the database
137 * that we're adding and failed, then forcing it to reconnect seems like
138 * a reasonable way to make it try again.
140 * If this is too big of a hammer in practice, we could be more selective,
141 * e.g. disconnect only connections that actually tried to use a database
142 * with 'db''s name. */
143 ovsdb_jsonrpc_server_reconnect(svr);
145 return ovsdb_server_add_db(&svr->up, db);
148 /* Removes 'db' from the set of databases served out by 'svr'. Returns
149 * true if successful, false if there is no database associated with 'db'. */
151 ovsdb_jsonrpc_server_remove_db(struct ovsdb_jsonrpc_server *svr,
154 /* There might be pointers to 'db' from 'svr', such as monitors or
155 * outstanding transactions. Disconnect all JSON-RPC connections to avoid
156 * accesses to freed memory.
158 * If this is too big of a hammer in practice, we could be more selective,
159 * e.g. disconnect only connections that actually reference 'db'. */
160 ovsdb_jsonrpc_server_reconnect(svr);
162 return ovsdb_server_remove_db(&svr->up, db);
166 ovsdb_jsonrpc_server_destroy(struct ovsdb_jsonrpc_server *svr)
168 struct shash_node *node, *next;
170 SHASH_FOR_EACH_SAFE (node, next, &svr->remotes) {
171 ovsdb_jsonrpc_server_del_remote(node);
173 shash_destroy(&svr->remotes);
174 ovsdb_server_destroy(&svr->up);
178 struct ovsdb_jsonrpc_options *
179 ovsdb_jsonrpc_default_options(const char *target)
181 struct ovsdb_jsonrpc_options *options = xzalloc(sizeof *options);
182 options->max_backoff = RECONNECT_DEFAULT_MAX_BACKOFF;
183 options->probe_interval = (stream_or_pstream_needs_probes(target)
184 ? RECONNECT_DEFAULT_PROBE_INTERVAL
189 /* Sets 'svr''s current set of remotes to the names in 'new_remotes', with
190 * options in the struct ovsdb_jsonrpc_options supplied as the data values.
192 * A remote is an active or passive stream connection method, e.g. "pssl:" or
195 ovsdb_jsonrpc_server_set_remotes(struct ovsdb_jsonrpc_server *svr,
196 const struct shash *new_remotes)
198 struct shash_node *node, *next;
200 SHASH_FOR_EACH_SAFE (node, next, &svr->remotes) {
201 if (!shash_find(new_remotes, node->name)) {
202 VLOG_INFO("%s: remote deconfigured", node->name);
203 ovsdb_jsonrpc_server_del_remote(node);
206 SHASH_FOR_EACH (node, new_remotes) {
207 const struct ovsdb_jsonrpc_options *options = node->data;
208 struct ovsdb_jsonrpc_remote *remote;
210 remote = shash_find_data(&svr->remotes, node->name);
212 remote = ovsdb_jsonrpc_server_add_remote(svr, node->name, options);
218 ovsdb_jsonrpc_session_set_all_options(remote, options);
222 static struct ovsdb_jsonrpc_remote *
223 ovsdb_jsonrpc_server_add_remote(struct ovsdb_jsonrpc_server *svr,
225 const struct ovsdb_jsonrpc_options *options)
227 struct ovsdb_jsonrpc_remote *remote;
228 struct pstream *listener;
231 error = jsonrpc_pstream_open(name, &listener, options->dscp);
232 if (error && error != EAFNOSUPPORT) {
233 VLOG_ERR_RL(&rl, "%s: listen failed: %s", name, ovs_strerror(error));
237 remote = xmalloc(sizeof *remote);
238 remote->server = svr;
239 remote->listener = listener;
240 list_init(&remote->sessions);
241 remote->dscp = options->dscp;
242 shash_add(&svr->remotes, name, remote);
245 ovsdb_jsonrpc_session_create(remote, jsonrpc_session_open(name, true));
251 ovsdb_jsonrpc_server_del_remote(struct shash_node *node)
253 struct ovsdb_jsonrpc_remote *remote = node->data;
255 ovsdb_jsonrpc_session_close_all(remote);
256 pstream_close(remote->listener);
257 shash_delete(&remote->server->remotes, node);
261 /* Stores status information for the remote named 'target', which should have
262 * been configured on 'svr' with a call to ovsdb_jsonrpc_server_set_remotes(),
263 * into '*status'. On success returns true, on failure (if 'svr' doesn't have
264 * a remote named 'target' or if that remote is an inbound remote that has no
265 * active connections) returns false. On failure, 'status' will be zeroed.
268 ovsdb_jsonrpc_server_get_remote_status(
269 const struct ovsdb_jsonrpc_server *svr, const char *target,
270 struct ovsdb_jsonrpc_remote_status *status)
272 const struct ovsdb_jsonrpc_remote *remote;
274 memset(status, 0, sizeof *status);
276 remote = shash_find_data(&svr->remotes, target);
277 return remote && ovsdb_jsonrpc_session_get_status(remote, status);
281 ovsdb_jsonrpc_server_free_remote_status(
282 struct ovsdb_jsonrpc_remote_status *status)
284 free(status->locks_held);
285 free(status->locks_waiting);
286 free(status->locks_lost);
289 /* Forces all of the JSON-RPC sessions managed by 'svr' to disconnect and
292 ovsdb_jsonrpc_server_reconnect(struct ovsdb_jsonrpc_server *svr)
294 struct shash_node *node;
296 SHASH_FOR_EACH (node, &svr->remotes) {
297 struct ovsdb_jsonrpc_remote *remote = node->data;
299 ovsdb_jsonrpc_session_reconnect_all(remote);
304 ovsdb_jsonrpc_server_run(struct ovsdb_jsonrpc_server *svr)
306 struct shash_node *node;
308 SHASH_FOR_EACH (node, &svr->remotes) {
309 struct ovsdb_jsonrpc_remote *remote = node->data;
311 if (remote->listener && svr->n_sessions < svr->max_sessions) {
312 struct stream *stream;
315 error = pstream_accept(remote->listener, &stream);
317 struct jsonrpc_session *js;
318 js = jsonrpc_session_open_unreliably(jsonrpc_open(stream),
320 ovsdb_jsonrpc_session_create(remote, js);
321 } else if (error != EAGAIN) {
322 VLOG_WARN_RL(&rl, "%s: accept failed: %s",
323 pstream_get_name(remote->listener),
324 ovs_strerror(error));
328 ovsdb_jsonrpc_session_run_all(remote);
333 ovsdb_jsonrpc_server_wait(struct ovsdb_jsonrpc_server *svr)
335 struct shash_node *node;
337 SHASH_FOR_EACH (node, &svr->remotes) {
338 struct ovsdb_jsonrpc_remote *remote = node->data;
340 if (remote->listener && svr->n_sessions < svr->max_sessions) {
341 pstream_wait(remote->listener);
344 ovsdb_jsonrpc_session_wait_all(remote);
348 /* Adds some memory usage statistics for 'svr' into 'usage', for use with
349 * memory_report(). */
351 ovsdb_jsonrpc_server_get_memory_usage(const struct ovsdb_jsonrpc_server *svr,
354 struct shash_node *node;
356 simap_increase(usage, "sessions", svr->n_sessions);
357 SHASH_FOR_EACH (node, &svr->remotes) {
358 struct ovsdb_jsonrpc_remote *remote = node->data;
360 ovsdb_jsonrpc_session_get_memory_usage_all(remote, usage);
364 /* JSON-RPC database server session. */
366 struct ovsdb_jsonrpc_session {
367 struct list node; /* Element in remote's sessions list. */
368 struct ovsdb_session up;
369 struct ovsdb_jsonrpc_remote *remote;
372 struct hmap triggers; /* Hmap of "struct ovsdb_jsonrpc_trigger"s. */
375 struct hmap monitors; /* Hmap of "struct ovsdb_jsonrpc_monitor"s. */
377 /* Network connectivity. */
378 struct jsonrpc_session *js; /* JSON-RPC session. */
379 unsigned int js_seqno; /* Last jsonrpc_session_get_seqno() value. */
382 static void ovsdb_jsonrpc_session_close(struct ovsdb_jsonrpc_session *);
383 static int ovsdb_jsonrpc_session_run(struct ovsdb_jsonrpc_session *);
384 static void ovsdb_jsonrpc_session_wait(struct ovsdb_jsonrpc_session *);
385 static void ovsdb_jsonrpc_session_get_memory_usage(
386 const struct ovsdb_jsonrpc_session *, struct simap *usage);
387 static void ovsdb_jsonrpc_session_set_options(
388 struct ovsdb_jsonrpc_session *, const struct ovsdb_jsonrpc_options *);
389 static void ovsdb_jsonrpc_session_got_request(struct ovsdb_jsonrpc_session *,
390 struct jsonrpc_msg *);
391 static void ovsdb_jsonrpc_session_got_notify(struct ovsdb_jsonrpc_session *,
392 struct jsonrpc_msg *);
394 static struct ovsdb_jsonrpc_session *
395 ovsdb_jsonrpc_session_create(struct ovsdb_jsonrpc_remote *remote,
396 struct jsonrpc_session *js)
398 struct ovsdb_jsonrpc_session *s;
400 s = xzalloc(sizeof *s);
401 ovsdb_session_init(&s->up, &remote->server->up);
403 list_push_back(&remote->sessions, &s->node);
404 hmap_init(&s->triggers);
405 hmap_init(&s->monitors);
407 s->js_seqno = jsonrpc_session_get_seqno(js);
409 remote->server->n_sessions++;
415 ovsdb_jsonrpc_session_close(struct ovsdb_jsonrpc_session *s)
417 ovsdb_jsonrpc_monitor_remove_all(s);
418 ovsdb_jsonrpc_session_unlock_all(s);
419 ovsdb_jsonrpc_trigger_complete_all(s);
421 hmap_destroy(&s->monitors);
422 hmap_destroy(&s->triggers);
424 jsonrpc_session_close(s->js);
425 list_remove(&s->node);
426 s->remote->server->n_sessions--;
427 ovsdb_session_destroy(&s->up);
432 ovsdb_jsonrpc_session_run(struct ovsdb_jsonrpc_session *s)
434 jsonrpc_session_run(s->js);
435 if (s->js_seqno != jsonrpc_session_get_seqno(s->js)) {
436 s->js_seqno = jsonrpc_session_get_seqno(s->js);
437 ovsdb_jsonrpc_trigger_complete_all(s);
438 ovsdb_jsonrpc_monitor_remove_all(s);
439 ovsdb_jsonrpc_session_unlock_all(s);
442 ovsdb_jsonrpc_trigger_complete_done(s);
444 if (!jsonrpc_session_get_backlog(s->js)) {
445 struct jsonrpc_msg *msg;
447 ovsdb_jsonrpc_monitor_flush_all(s);
449 msg = jsonrpc_session_recv(s->js);
451 if (msg->type == JSONRPC_REQUEST) {
452 ovsdb_jsonrpc_session_got_request(s, msg);
453 } else if (msg->type == JSONRPC_NOTIFY) {
454 ovsdb_jsonrpc_session_got_notify(s, msg);
456 VLOG_WARN("%s: received unexpected %s message",
457 jsonrpc_session_get_name(s->js),
458 jsonrpc_msg_type_to_string(msg->type));
459 jsonrpc_session_force_reconnect(s->js);
460 jsonrpc_msg_destroy(msg);
464 return jsonrpc_session_is_alive(s->js) ? 0 : ETIMEDOUT;
468 ovsdb_jsonrpc_session_set_options(struct ovsdb_jsonrpc_session *session,
469 const struct ovsdb_jsonrpc_options *options)
471 jsonrpc_session_set_max_backoff(session->js, options->max_backoff);
472 jsonrpc_session_set_probe_interval(session->js, options->probe_interval);
473 jsonrpc_session_set_dscp(session->js, options->dscp);
477 ovsdb_jsonrpc_session_run_all(struct ovsdb_jsonrpc_remote *remote)
479 struct ovsdb_jsonrpc_session *s, *next;
481 LIST_FOR_EACH_SAFE (s, next, node, &remote->sessions) {
482 int error = ovsdb_jsonrpc_session_run(s);
484 ovsdb_jsonrpc_session_close(s);
490 ovsdb_jsonrpc_session_wait(struct ovsdb_jsonrpc_session *s)
492 jsonrpc_session_wait(s->js);
493 if (!jsonrpc_session_get_backlog(s->js)) {
494 if (ovsdb_jsonrpc_monitor_needs_flush(s)) {
495 poll_immediate_wake();
497 jsonrpc_session_recv_wait(s->js);
503 ovsdb_jsonrpc_session_wait_all(struct ovsdb_jsonrpc_remote *remote)
505 struct ovsdb_jsonrpc_session *s;
507 LIST_FOR_EACH (s, node, &remote->sessions) {
508 ovsdb_jsonrpc_session_wait(s);
513 ovsdb_jsonrpc_session_get_memory_usage(const struct ovsdb_jsonrpc_session *s,
516 simap_increase(usage, "triggers", hmap_count(&s->triggers));
517 simap_increase(usage, "monitors", hmap_count(&s->monitors));
518 simap_increase(usage, "backlog", jsonrpc_session_get_backlog(s->js));
522 ovsdb_jsonrpc_session_get_memory_usage_all(
523 const struct ovsdb_jsonrpc_remote *remote,
526 struct ovsdb_jsonrpc_session *s;
528 LIST_FOR_EACH (s, node, &remote->sessions) {
529 ovsdb_jsonrpc_session_get_memory_usage(s, usage);
534 ovsdb_jsonrpc_session_close_all(struct ovsdb_jsonrpc_remote *remote)
536 struct ovsdb_jsonrpc_session *s, *next;
538 LIST_FOR_EACH_SAFE (s, next, node, &remote->sessions) {
539 ovsdb_jsonrpc_session_close(s);
543 /* Forces all of the JSON-RPC sessions managed by 'remote' to disconnect and
546 ovsdb_jsonrpc_session_reconnect_all(struct ovsdb_jsonrpc_remote *remote)
548 struct ovsdb_jsonrpc_session *s, *next;
550 LIST_FOR_EACH_SAFE (s, next, node, &remote->sessions) {
551 jsonrpc_session_force_reconnect(s->js);
552 if (!jsonrpc_session_is_alive(s->js)) {
553 ovsdb_jsonrpc_session_close(s);
558 /* Sets the options for all of the JSON-RPC sessions managed by 'remote' to
561 ovsdb_jsonrpc_session_set_all_options(
562 struct ovsdb_jsonrpc_remote *remote,
563 const struct ovsdb_jsonrpc_options *options)
565 struct ovsdb_jsonrpc_session *s;
567 if (remote->listener) {
570 error = pstream_set_dscp(remote->listener, options->dscp);
572 VLOG_ERR("%s: set_dscp failed %s",
573 pstream_get_name(remote->listener), ovs_strerror(error));
575 remote->dscp = options->dscp;
578 * XXX race window between setting dscp to listening socket
579 * and accepting socket. Accepted socket may have old dscp value.
580 * Ignore this race window for now.
583 LIST_FOR_EACH (s, node, &remote->sessions) {
584 ovsdb_jsonrpc_session_set_options(s, options);
589 ovsdb_jsonrpc_session_get_status(const struct ovsdb_jsonrpc_remote *remote,
590 struct ovsdb_jsonrpc_remote_status *status)
592 const struct ovsdb_jsonrpc_session *s;
593 const struct jsonrpc_session *js;
594 struct ovsdb_lock_waiter *waiter;
595 struct reconnect_stats rstats;
596 struct ds locks_held, locks_waiting, locks_lost;
598 status->bound_port = (remote->listener
599 ? pstream_get_bound_port(remote->listener)
602 if (list_is_empty(&remote->sessions)) {
605 s = CONTAINER_OF(remote->sessions.next, struct ovsdb_jsonrpc_session, node);
608 status->is_connected = jsonrpc_session_is_connected(js);
609 status->last_error = jsonrpc_session_get_status(js);
611 jsonrpc_session_get_reconnect_stats(js, &rstats);
612 status->state = rstats.state;
613 status->sec_since_connect = rstats.msec_since_connect == UINT_MAX
614 ? UINT_MAX : rstats.msec_since_connect / 1000;
615 status->sec_since_disconnect = rstats.msec_since_disconnect == UINT_MAX
616 ? UINT_MAX : rstats.msec_since_disconnect / 1000;
618 ds_init(&locks_held);
619 ds_init(&locks_waiting);
620 ds_init(&locks_lost);
621 HMAP_FOR_EACH (waiter, session_node, &s->up.waiters) {
624 string = (ovsdb_lock_waiter_is_owner(waiter) ? &locks_held
625 : waiter->mode == OVSDB_LOCK_WAIT ? &locks_waiting
627 if (string->length) {
628 ds_put_char(string, ' ');
630 ds_put_cstr(string, waiter->lock_name);
632 status->locks_held = ds_steal_cstr(&locks_held);
633 status->locks_waiting = ds_steal_cstr(&locks_waiting);
634 status->locks_lost = ds_steal_cstr(&locks_lost);
636 status->n_connections = list_size(&remote->sessions);
641 /* Examines 'request' to determine the database to which it relates, and then
642 * searches 's' to find that database:
644 * - If successful, returns the database and sets '*replyp' to NULL.
646 * - If no such database exists, returns NULL and sets '*replyp' to an
647 * appropriate JSON-RPC error reply, owned by the caller. */
648 static struct ovsdb *
649 ovsdb_jsonrpc_lookup_db(const struct ovsdb_jsonrpc_session *s,
650 const struct jsonrpc_msg *request,
651 struct jsonrpc_msg **replyp)
653 struct json_array *params;
654 struct ovsdb_error *error;
658 params = json_array(request->params);
659 if (!params->n || params->elems[0]->type != JSON_STRING) {
660 error = ovsdb_syntax_error(
661 request->params, NULL,
662 "%s request params must begin with <db-name>", request->method);
666 db_name = params->elems[0]->u.string;
667 db = shash_find_data(&s->up.server->dbs, db_name);
669 error = ovsdb_syntax_error(
670 request->params, "unknown database",
671 "%s request specifies unknown database %s",
672 request->method, db_name);
680 *replyp = jsonrpc_create_reply(ovsdb_error_to_json(error), request->id);
681 ovsdb_error_destroy(error);
685 static struct ovsdb_error *
686 ovsdb_jsonrpc_session_parse_lock_name(const struct jsonrpc_msg *request,
687 const char **lock_namep)
689 const struct json_array *params;
691 params = json_array(request->params);
692 if (params->n != 1 || params->elems[0]->type != JSON_STRING ||
693 !ovsdb_parser_is_id(json_string(params->elems[0]))) {
695 return ovsdb_syntax_error(request->params, NULL,
696 "%s request params must be <id>",
700 *lock_namep = json_string(params->elems[0]);
705 ovsdb_jsonrpc_session_notify(struct ovsdb_session *session,
706 const char *lock_name,
709 struct ovsdb_jsonrpc_session *s;
712 s = CONTAINER_OF(session, struct ovsdb_jsonrpc_session, up);
713 params = json_array_create_1(json_string_create(lock_name));
714 ovsdb_jsonrpc_session_send(s, jsonrpc_create_notify(method, params));
717 static struct jsonrpc_msg *
718 ovsdb_jsonrpc_session_lock(struct ovsdb_jsonrpc_session *s,
719 struct jsonrpc_msg *request,
720 enum ovsdb_lock_mode mode)
722 struct ovsdb_lock_waiter *waiter;
723 struct jsonrpc_msg *reply;
724 struct ovsdb_error *error;
725 struct ovsdb_session *victim;
726 const char *lock_name;
729 error = ovsdb_jsonrpc_session_parse_lock_name(request, &lock_name);
734 /* Report error if this session has issued a "lock" or "steal" without a
735 * matching "unlock" for this lock. */
736 waiter = ovsdb_session_get_lock_waiter(&s->up, lock_name);
738 error = ovsdb_syntax_error(
739 request->params, NULL,
740 "must issue \"unlock\" before new \"%s\"", request->method);
744 /* Get the lock, add us as a waiter. */
745 waiter = ovsdb_server_lock(&s->remote->server->up, &s->up, lock_name, mode,
748 ovsdb_jsonrpc_session_notify(victim, lock_name, "stolen");
751 result = json_object_create();
752 json_object_put(result, "locked",
753 json_boolean_create(ovsdb_lock_waiter_is_owner(waiter)));
755 return jsonrpc_create_reply(result, request->id);
758 reply = jsonrpc_create_reply(ovsdb_error_to_json(error), request->id);
759 ovsdb_error_destroy(error);
764 ovsdb_jsonrpc_session_unlock_all(struct ovsdb_jsonrpc_session *s)
766 struct ovsdb_lock_waiter *waiter, *next;
768 HMAP_FOR_EACH_SAFE (waiter, next, session_node, &s->up.waiters) {
769 ovsdb_jsonrpc_session_unlock__(waiter);
774 ovsdb_jsonrpc_session_unlock__(struct ovsdb_lock_waiter *waiter)
776 struct ovsdb_lock *lock = waiter->lock;
779 struct ovsdb_session *new_owner = ovsdb_lock_waiter_remove(waiter);
781 ovsdb_jsonrpc_session_notify(new_owner, lock->name, "locked");
783 /* ovsdb_server_lock() might have freed 'lock'. */
787 ovsdb_lock_waiter_destroy(waiter);
790 static struct jsonrpc_msg *
791 ovsdb_jsonrpc_session_unlock(struct ovsdb_jsonrpc_session *s,
792 struct jsonrpc_msg *request)
794 struct ovsdb_lock_waiter *waiter;
795 struct jsonrpc_msg *reply;
796 struct ovsdb_error *error;
797 const char *lock_name;
799 error = ovsdb_jsonrpc_session_parse_lock_name(request, &lock_name);
804 /* Report error if this session has not issued a "lock" or "steal" for this
806 waiter = ovsdb_session_get_lock_waiter(&s->up, lock_name);
808 error = ovsdb_syntax_error(
809 request->params, NULL, "\"unlock\" without \"lock\" or \"steal\"");
813 ovsdb_jsonrpc_session_unlock__(waiter);
815 return jsonrpc_create_reply(json_object_create(), request->id);
818 reply = jsonrpc_create_reply(ovsdb_error_to_json(error), request->id);
819 ovsdb_error_destroy(error);
823 static struct jsonrpc_msg *
824 execute_transaction(struct ovsdb_jsonrpc_session *s, struct ovsdb *db,
825 struct jsonrpc_msg *request)
827 ovsdb_jsonrpc_trigger_create(s, db, request->id, request->params);
829 request->params = NULL;
830 jsonrpc_msg_destroy(request);
835 ovsdb_jsonrpc_session_got_request(struct ovsdb_jsonrpc_session *s,
836 struct jsonrpc_msg *request)
838 struct jsonrpc_msg *reply;
840 if (!strcmp(request->method, "transact")) {
841 struct ovsdb *db = ovsdb_jsonrpc_lookup_db(s, request, &reply);
843 reply = execute_transaction(s, db, request);
845 } else if (!strcmp(request->method, "monitor")) {
846 struct ovsdb *db = ovsdb_jsonrpc_lookup_db(s, request, &reply);
848 reply = jsonrpc_create_reply(
849 ovsdb_jsonrpc_monitor_create(s, db, request->params),
852 } else if (!strcmp(request->method, "monitor_cancel")) {
853 reply = ovsdb_jsonrpc_monitor_cancel(s, json_array(request->params),
855 } else if (!strcmp(request->method, "get_schema")) {
856 struct ovsdb *db = ovsdb_jsonrpc_lookup_db(s, request, &reply);
858 reply = jsonrpc_create_reply(ovsdb_schema_to_json(db->schema),
861 } else if (!strcmp(request->method, "list_dbs")) {
862 size_t n_dbs = shash_count(&s->up.server->dbs);
863 struct shash_node *node;
867 dbs = xmalloc(n_dbs * sizeof *dbs);
869 SHASH_FOR_EACH (node, &s->up.server->dbs) {
870 dbs[i++] = json_string_create(node->name);
872 reply = jsonrpc_create_reply(json_array_create(dbs, n_dbs),
874 } else if (!strcmp(request->method, "lock")) {
875 reply = ovsdb_jsonrpc_session_lock(s, request, OVSDB_LOCK_WAIT);
876 } else if (!strcmp(request->method, "steal")) {
877 reply = ovsdb_jsonrpc_session_lock(s, request, OVSDB_LOCK_STEAL);
878 } else if (!strcmp(request->method, "unlock")) {
879 reply = ovsdb_jsonrpc_session_unlock(s, request);
880 } else if (!strcmp(request->method, "echo")) {
881 reply = jsonrpc_create_reply(json_clone(request->params), request->id);
883 reply = jsonrpc_create_error(json_string_create("unknown method"),
888 jsonrpc_msg_destroy(request);
889 ovsdb_jsonrpc_session_send(s, reply);
894 execute_cancel(struct ovsdb_jsonrpc_session *s, struct jsonrpc_msg *request)
896 if (json_array(request->params)->n == 1) {
897 struct ovsdb_jsonrpc_trigger *t;
900 id = request->params->u.array.elems[0];
901 t = ovsdb_jsonrpc_trigger_find(s, id, json_hash(id, 0));
903 ovsdb_jsonrpc_trigger_complete(t);
909 ovsdb_jsonrpc_session_got_notify(struct ovsdb_jsonrpc_session *s,
910 struct jsonrpc_msg *request)
912 if (!strcmp(request->method, "cancel")) {
913 execute_cancel(s, request);
915 jsonrpc_msg_destroy(request);
919 ovsdb_jsonrpc_session_send(struct ovsdb_jsonrpc_session *s,
920 struct jsonrpc_msg *msg)
922 ovsdb_jsonrpc_monitor_flush_all(s);
923 jsonrpc_session_send(s->js, msg);
926 /* JSON-RPC database server triggers.
928 * (Every transaction is treated as a trigger even if it doesn't actually have
929 * any "wait" operations.) */
931 struct ovsdb_jsonrpc_trigger {
932 struct ovsdb_trigger trigger;
933 struct hmap_node hmap_node; /* In session's "triggers" hmap. */
938 ovsdb_jsonrpc_trigger_create(struct ovsdb_jsonrpc_session *s, struct ovsdb *db,
939 struct json *id, struct json *params)
941 struct ovsdb_jsonrpc_trigger *t;
944 /* Check for duplicate ID. */
945 hash = json_hash(id, 0);
946 t = ovsdb_jsonrpc_trigger_find(s, id, hash);
948 struct jsonrpc_msg *msg;
950 msg = jsonrpc_create_error(json_string_create("duplicate request ID"),
952 ovsdb_jsonrpc_session_send(s, msg);
954 json_destroy(params);
958 /* Insert into trigger table. */
959 t = xmalloc(sizeof *t);
960 ovsdb_trigger_init(&s->up, db, &t->trigger, params, time_msec());
962 hmap_insert(&s->triggers, &t->hmap_node, hash);
964 /* Complete early if possible. */
965 if (ovsdb_trigger_is_complete(&t->trigger)) {
966 ovsdb_jsonrpc_trigger_complete(t);
970 static struct ovsdb_jsonrpc_trigger *
971 ovsdb_jsonrpc_trigger_find(struct ovsdb_jsonrpc_session *s,
972 const struct json *id, size_t hash)
974 struct ovsdb_jsonrpc_trigger *t;
976 HMAP_FOR_EACH_WITH_HASH (t, hmap_node, hash, &s->triggers) {
977 if (json_equal(t->id, id)) {
986 ovsdb_jsonrpc_trigger_complete(struct ovsdb_jsonrpc_trigger *t)
988 struct ovsdb_jsonrpc_session *s;
990 s = CONTAINER_OF(t->trigger.session, struct ovsdb_jsonrpc_session, up);
992 if (jsonrpc_session_is_connected(s->js)) {
993 struct jsonrpc_msg *reply;
996 result = ovsdb_trigger_steal_result(&t->trigger);
998 reply = jsonrpc_create_reply(result, t->id);
1000 reply = jsonrpc_create_error(json_string_create("canceled"),
1003 ovsdb_jsonrpc_session_send(s, reply);
1006 json_destroy(t->id);
1007 ovsdb_trigger_destroy(&t->trigger);
1008 hmap_remove(&s->triggers, &t->hmap_node);
1013 ovsdb_jsonrpc_trigger_complete_all(struct ovsdb_jsonrpc_session *s)
1015 struct ovsdb_jsonrpc_trigger *t, *next;
1016 HMAP_FOR_EACH_SAFE (t, next, hmap_node, &s->triggers) {
1017 ovsdb_jsonrpc_trigger_complete(t);
1022 ovsdb_jsonrpc_trigger_complete_done(struct ovsdb_jsonrpc_session *s)
1024 while (!list_is_empty(&s->up.completions)) {
1025 struct ovsdb_jsonrpc_trigger *t
1026 = CONTAINER_OF(s->up.completions.next,
1027 struct ovsdb_jsonrpc_trigger, trigger.node);
1028 ovsdb_jsonrpc_trigger_complete(t);
1032 /* JSON-RPC database table monitors. */
1034 enum ovsdb_jsonrpc_monitor_selection {
1035 OJMS_INITIAL = 1 << 0, /* All rows when monitor is created. */
1036 OJMS_INSERT = 1 << 1, /* New rows. */
1037 OJMS_DELETE = 1 << 2, /* Deleted rows. */
1038 OJMS_MODIFY = 1 << 3 /* Modified rows. */
1041 /* A particular column being monitored. */
1042 struct ovsdb_jsonrpc_monitor_column {
1043 const struct ovsdb_column *column;
1044 enum ovsdb_jsonrpc_monitor_selection select;
1047 /* A row that has changed in a monitored table. */
1048 struct ovsdb_jsonrpc_monitor_row {
1049 struct hmap_node hmap_node; /* In ovsdb_jsonrpc_monitor_table.changes. */
1050 struct uuid uuid; /* UUID of row that changed. */
1051 struct ovsdb_datum *old; /* Old data, NULL for an inserted row. */
1052 struct ovsdb_datum *new; /* New data, NULL for a deleted row. */
1055 /* A particular table being monitored. */
1056 struct ovsdb_jsonrpc_monitor_table {
1057 const struct ovsdb_table *table;
1059 /* This is the union (bitwise-OR) of the 'select' values in all of the
1060 * members of 'columns' below. */
1061 enum ovsdb_jsonrpc_monitor_selection select;
1063 /* Columns being monitored. */
1064 struct ovsdb_jsonrpc_monitor_column *columns;
1067 /* Contains 'struct ovsdb_jsonrpc_monitor_row's for rows that have been
1068 * updated but not yet flushed to the jsonrpc connection. */
1069 struct hmap changes;
1072 /* A collection of tables being monitored. */
1073 struct ovsdb_jsonrpc_monitor {
1074 struct ovsdb_replica replica;
1075 struct ovsdb_jsonrpc_session *session;
1077 struct hmap_node node; /* In ovsdb_jsonrpc_session's "monitors". */
1079 struct json *monitor_id;
1080 struct shash tables; /* Holds "struct ovsdb_jsonrpc_monitor_table"s. */
1083 static const struct ovsdb_replica_class ovsdb_jsonrpc_replica_class;
1085 struct ovsdb_jsonrpc_monitor *ovsdb_jsonrpc_monitor_find(
1086 struct ovsdb_jsonrpc_session *, const struct json *monitor_id);
1087 static void ovsdb_jsonrpc_monitor_destroy(struct ovsdb_replica *);
1088 static struct json *ovsdb_jsonrpc_monitor_get_initial(
1089 const struct ovsdb_jsonrpc_monitor *);
1092 parse_bool(struct ovsdb_parser *parser, const char *name, bool default_value)
1094 const struct json *json;
1096 json = ovsdb_parser_member(parser, name, OP_BOOLEAN | OP_OPTIONAL);
1097 return json ? json_boolean(json) : default_value;
1100 struct ovsdb_jsonrpc_monitor *
1101 ovsdb_jsonrpc_monitor_find(struct ovsdb_jsonrpc_session *s,
1102 const struct json *monitor_id)
1104 struct ovsdb_jsonrpc_monitor *m;
1106 HMAP_FOR_EACH_WITH_HASH (m, node, json_hash(monitor_id, 0), &s->monitors) {
1107 if (json_equal(m->monitor_id, monitor_id)) {
1116 ovsdb_jsonrpc_add_monitor_column(struct ovsdb_jsonrpc_monitor_table *mt,
1117 const struct ovsdb_column *column,
1118 enum ovsdb_jsonrpc_monitor_selection select,
1119 size_t *allocated_columns)
1121 struct ovsdb_jsonrpc_monitor_column *c;
1123 if (mt->n_columns >= *allocated_columns) {
1124 mt->columns = x2nrealloc(mt->columns, allocated_columns,
1125 sizeof *mt->columns);
1128 c = &mt->columns[mt->n_columns++];
1134 compare_ovsdb_jsonrpc_monitor_column(const void *a_, const void *b_)
1136 const struct ovsdb_jsonrpc_monitor_column *a = a_;
1137 const struct ovsdb_jsonrpc_monitor_column *b = b_;
1139 return a->column < b->column ? -1 : a->column > b->column;
1142 static struct ovsdb_error * WARN_UNUSED_RESULT
1143 ovsdb_jsonrpc_parse_monitor_request(struct ovsdb_jsonrpc_monitor_table *mt,
1144 const struct json *monitor_request,
1145 size_t *allocated_columns)
1147 const struct ovsdb_table_schema *ts = mt->table->schema;
1148 enum ovsdb_jsonrpc_monitor_selection select;
1149 const struct json *columns, *select_json;
1150 struct ovsdb_parser parser;
1151 struct ovsdb_error *error;
1153 ovsdb_parser_init(&parser, monitor_request, "table %s", ts->name);
1154 columns = ovsdb_parser_member(&parser, "columns", OP_ARRAY | OP_OPTIONAL);
1155 select_json = ovsdb_parser_member(&parser, "select",
1156 OP_OBJECT | OP_OPTIONAL);
1157 error = ovsdb_parser_finish(&parser);
1164 ovsdb_parser_init(&parser, select_json, "table %s select", ts->name);
1165 if (parse_bool(&parser, "initial", true)) {
1166 select |= OJMS_INITIAL;
1168 if (parse_bool(&parser, "insert", true)) {
1169 select |= OJMS_INSERT;
1171 if (parse_bool(&parser, "delete", true)) {
1172 select |= OJMS_DELETE;
1174 if (parse_bool(&parser, "modify", true)) {
1175 select |= OJMS_MODIFY;
1177 error = ovsdb_parser_finish(&parser);
1182 select = OJMS_INITIAL | OJMS_INSERT | OJMS_DELETE | OJMS_MODIFY;
1184 mt->select |= select;
1189 if (columns->type != JSON_ARRAY) {
1190 return ovsdb_syntax_error(columns, NULL,
1191 "array of column names expected");
1194 for (i = 0; i < columns->u.array.n; i++) {
1195 const struct ovsdb_column *column;
1198 if (columns->u.array.elems[i]->type != JSON_STRING) {
1199 return ovsdb_syntax_error(columns, NULL,
1200 "array of column names expected");
1203 s = columns->u.array.elems[i]->u.string;
1204 column = shash_find_data(&mt->table->schema->columns, s);
1206 return ovsdb_syntax_error(columns, NULL, "%s is not a valid "
1209 ovsdb_jsonrpc_add_monitor_column(mt, column, select,
1213 struct shash_node *node;
1215 SHASH_FOR_EACH (node, &ts->columns) {
1216 const struct ovsdb_column *column = node->data;
1217 if (column->index != OVSDB_COL_UUID) {
1218 ovsdb_jsonrpc_add_monitor_column(mt, column, select,
1227 static struct json *
1228 ovsdb_jsonrpc_monitor_create(struct ovsdb_jsonrpc_session *s, struct ovsdb *db,
1229 struct json *params)
1231 struct ovsdb_jsonrpc_monitor *m = NULL;
1232 struct json *monitor_id, *monitor_requests;
1233 struct ovsdb_error *error = NULL;
1234 struct shash_node *node;
1237 if (json_array(params)->n != 3) {
1238 error = ovsdb_syntax_error(params, NULL, "invalid parameters");
1241 monitor_id = params->u.array.elems[1];
1242 monitor_requests = params->u.array.elems[2];
1243 if (monitor_requests->type != JSON_OBJECT) {
1244 error = ovsdb_syntax_error(monitor_requests, NULL,
1245 "monitor-requests must be object");
1249 if (ovsdb_jsonrpc_monitor_find(s, monitor_id)) {
1250 error = ovsdb_syntax_error(monitor_id, NULL, "duplicate monitor ID");
1254 m = xzalloc(sizeof *m);
1255 ovsdb_replica_init(&m->replica, &ovsdb_jsonrpc_replica_class);
1256 ovsdb_add_replica(db, &m->replica);
1259 hmap_insert(&s->monitors, &m->node, json_hash(monitor_id, 0));
1260 m->monitor_id = json_clone(monitor_id);
1261 shash_init(&m->tables);
1263 SHASH_FOR_EACH (node, json_object(monitor_requests)) {
1264 const struct ovsdb_table *table;
1265 struct ovsdb_jsonrpc_monitor_table *mt;
1266 size_t allocated_columns;
1267 const struct json *mr_value;
1270 table = ovsdb_get_table(m->db, node->name);
1272 error = ovsdb_syntax_error(NULL, NULL,
1273 "no table named %s", node->name);
1277 mt = xzalloc(sizeof *mt);
1279 hmap_init(&mt->changes);
1280 shash_add(&m->tables, table->schema->name, mt);
1282 /* Parse columns. */
1283 mr_value = node->data;
1284 allocated_columns = 0;
1285 if (mr_value->type == JSON_ARRAY) {
1286 const struct json_array *array = &mr_value->u.array;
1288 for (i = 0; i < array->n; i++) {
1289 error = ovsdb_jsonrpc_parse_monitor_request(
1290 mt, array->elems[i], &allocated_columns);
1296 error = ovsdb_jsonrpc_parse_monitor_request(
1297 mt, mr_value, &allocated_columns);
1303 /* Check for duplicate columns. */
1304 qsort(mt->columns, mt->n_columns, sizeof *mt->columns,
1305 compare_ovsdb_jsonrpc_monitor_column);
1306 for (i = 1; i < mt->n_columns; i++) {
1307 if (mt->columns[i].column == mt->columns[i - 1].column) {
1308 error = ovsdb_syntax_error(mr_value, NULL, "column %s "
1309 "mentioned more than once",
1310 mt->columns[i].column->name);
1316 return ovsdb_jsonrpc_monitor_get_initial(m);
1320 ovsdb_remove_replica(m->db, &m->replica);
1323 json = ovsdb_error_to_json(error);
1324 ovsdb_error_destroy(error);
1328 static struct jsonrpc_msg *
1329 ovsdb_jsonrpc_monitor_cancel(struct ovsdb_jsonrpc_session *s,
1330 struct json_array *params,
1331 const struct json *request_id)
1333 if (params->n != 1) {
1334 return jsonrpc_create_error(json_string_create("invalid parameters"),
1337 struct ovsdb_jsonrpc_monitor *m;
1339 m = ovsdb_jsonrpc_monitor_find(s, params->elems[0]);
1341 return jsonrpc_create_error(json_string_create("unknown monitor"),
1344 ovsdb_remove_replica(m->db, &m->replica);
1345 return jsonrpc_create_reply(json_object_create(), request_id);
1351 ovsdb_jsonrpc_monitor_remove_all(struct ovsdb_jsonrpc_session *s)
1353 struct ovsdb_jsonrpc_monitor *m, *next;
1355 HMAP_FOR_EACH_SAFE (m, next, node, &s->monitors) {
1356 ovsdb_remove_replica(m->db, &m->replica);
1360 static struct ovsdb_jsonrpc_monitor *
1361 ovsdb_jsonrpc_monitor_cast(struct ovsdb_replica *replica)
1363 ovs_assert(replica->class == &ovsdb_jsonrpc_replica_class);
1364 return CONTAINER_OF(replica, struct ovsdb_jsonrpc_monitor, replica);
1367 struct ovsdb_jsonrpc_monitor_aux {
1368 const struct ovsdb_jsonrpc_monitor *monitor;
1369 struct ovsdb_jsonrpc_monitor_table *mt;
1372 /* Finds and returns the ovsdb_jsonrpc_monitor_row in 'mt->changes' for the
1373 * given 'uuid', or NULL if there is no such row. */
1374 static struct ovsdb_jsonrpc_monitor_row *
1375 ovsdb_jsonrpc_monitor_row_find(const struct ovsdb_jsonrpc_monitor_table *mt,
1376 const struct uuid *uuid)
1378 struct ovsdb_jsonrpc_monitor_row *row;
1380 HMAP_FOR_EACH_WITH_HASH (row, hmap_node, uuid_hash(uuid), &mt->changes) {
1381 if (uuid_equals(uuid, &row->uuid)) {
1388 /* Allocates an array of 'mt->n_columns' ovsdb_datums and initializes them as
1389 * copies of the data in 'row' drawn from the columns represented by
1390 * mt->columns[]. Returns the array.
1392 * If 'row' is NULL, returns NULL. */
1393 static struct ovsdb_datum *
1394 clone_monitor_row_data(const struct ovsdb_jsonrpc_monitor_table *mt,
1395 const struct ovsdb_row *row)
1397 struct ovsdb_datum *data;
1404 data = xmalloc(mt->n_columns * sizeof *data);
1405 for (i = 0; i < mt->n_columns; i++) {
1406 const struct ovsdb_column *c = mt->columns[i].column;
1407 const struct ovsdb_datum *src = &row->fields[c->index];
1408 struct ovsdb_datum *dst = &data[i];
1409 const struct ovsdb_type *type = &c->type;
1411 ovsdb_datum_clone(dst, src, type);
1416 /* Replaces the mt->n_columns ovsdb_datums in row[] by copies of the data from
1417 * in 'row' drawn from the columns represented by mt->columns[]. */
1419 update_monitor_row_data(const struct ovsdb_jsonrpc_monitor_table *mt,
1420 const struct ovsdb_row *row,
1421 struct ovsdb_datum *data)
1425 for (i = 0; i < mt->n_columns; i++) {
1426 const struct ovsdb_column *c = mt->columns[i].column;
1427 const struct ovsdb_datum *src = &row->fields[c->index];
1428 struct ovsdb_datum *dst = &data[i];
1429 const struct ovsdb_type *type = &c->type;
1431 if (!ovsdb_datum_equals(src, dst, type)) {
1432 ovsdb_datum_destroy(dst, type);
1433 ovsdb_datum_clone(dst, src, type);
1438 /* Frees all of the mt->n_columns ovsdb_datums in data[], using the types taken
1439 * from mt->columns[], plus 'data' itself. */
1441 free_monitor_row_data(const struct ovsdb_jsonrpc_monitor_table *mt,
1442 struct ovsdb_datum *data)
1447 for (i = 0; i < mt->n_columns; i++) {
1448 const struct ovsdb_column *c = mt->columns[i].column;
1450 ovsdb_datum_destroy(&data[i], &c->type);
1456 /* Frees 'row', which must have been created from 'mt'. */
1458 ovsdb_jsonrpc_monitor_row_destroy(const struct ovsdb_jsonrpc_monitor_table *mt,
1459 struct ovsdb_jsonrpc_monitor_row *row)
1462 free_monitor_row_data(mt, row->old);
1463 free_monitor_row_data(mt, row->new);
1469 ovsdb_jsonrpc_monitor_change_cb(const struct ovsdb_row *old,
1470 const struct ovsdb_row *new,
1471 const unsigned long int *changed OVS_UNUSED,
1474 struct ovsdb_jsonrpc_monitor_aux *aux = aux_;
1475 const struct ovsdb_jsonrpc_monitor *m = aux->monitor;
1476 struct ovsdb_table *table = new ? new->table : old->table;
1477 const struct uuid *uuid = ovsdb_row_get_uuid(new ? new : old);
1478 struct ovsdb_jsonrpc_monitor_row *change;
1479 struct ovsdb_jsonrpc_monitor_table *mt;
1481 if (!aux->mt || table != aux->mt->table) {
1482 aux->mt = shash_find_data(&m->tables, table->schema->name);
1484 /* We don't care about rows in this table at all. Tell the caller
1491 change = ovsdb_jsonrpc_monitor_row_find(mt, uuid);
1493 change = xmalloc(sizeof *change);
1494 hmap_insert(&mt->changes, &change->hmap_node, uuid_hash(uuid));
1495 change->uuid = *uuid;
1496 change->old = clone_monitor_row_data(mt, old);
1497 change->new = clone_monitor_row_data(mt, new);
1500 update_monitor_row_data(mt, new, change->new);
1502 free_monitor_row_data(mt, change->new);
1506 /* This row was added then deleted. Forget about it. */
1507 hmap_remove(&mt->changes, &change->hmap_node);
1515 /* Returns JSON for a <row-update> (as described in RFC 7047) for 'row' within
1516 * 'mt', or NULL if no row update should be sent.
1518 * The caller should specify 'initial' as true if the returned JSON is going to
1519 * be used as part of the initial reply to a "monitor" request, false if it is
1520 * going to be used as part of an "update" notification.
1522 * 'changed' must be a scratch buffer for internal use that is at least
1523 * bitmap_n_bytes(mt->n_columns) bytes long. */
1524 static struct json *
1525 ovsdb_jsonrpc_monitor_compose_row_update(
1526 const struct ovsdb_jsonrpc_monitor_table *mt,
1527 const struct ovsdb_jsonrpc_monitor_row *row,
1528 bool initial, unsigned long int *changed)
1530 enum ovsdb_jsonrpc_monitor_selection type;
1531 struct json *old_json, *new_json;
1532 struct json *row_json;
1535 type = (initial ? OJMS_INITIAL
1536 : !row->old ? OJMS_INSERT
1537 : !row->new ? OJMS_DELETE
1539 if (!(mt->select & type)) {
1543 if (type == OJMS_MODIFY) {
1547 memset(changed, 0, bitmap_n_bytes(mt->n_columns));
1548 for (i = 0; i < mt->n_columns; i++) {
1549 const struct ovsdb_column *c = mt->columns[i].column;
1550 if (!ovsdb_datum_equals(&row->old[i], &row->new[i], &c->type)) {
1551 bitmap_set1(changed, i);
1556 /* No actual changes: presumably a row changed and then
1557 * changed back later. */
1562 row_json = json_object_create();
1563 old_json = new_json = NULL;
1564 if (type & (OJMS_DELETE | OJMS_MODIFY)) {
1565 old_json = json_object_create();
1566 json_object_put(row_json, "old", old_json);
1568 if (type & (OJMS_INITIAL | OJMS_INSERT | OJMS_MODIFY)) {
1569 new_json = json_object_create();
1570 json_object_put(row_json, "new", new_json);
1572 for (i = 0; i < mt->n_columns; i++) {
1573 const struct ovsdb_jsonrpc_monitor_column *c = &mt->columns[i];
1575 if (!(type & c->select)) {
1576 /* We don't care about this type of change for this
1577 * particular column (but we will care about it for some
1582 if ((type == OJMS_MODIFY && bitmap_is_set(changed, i))
1583 || type == OJMS_DELETE) {
1584 json_object_put(old_json, c->column->name,
1585 ovsdb_datum_to_json(&row->old[i],
1588 if (type & (OJMS_INITIAL | OJMS_INSERT | OJMS_MODIFY)) {
1589 json_object_put(new_json, c->column->name,
1590 ovsdb_datum_to_json(&row->new[i],
1598 /* Constructs and returns JSON for a <table-updates> object (as described in
1599 * RFC 7047) for all the outstanding changes within 'monitor', and deletes all
1600 * the outstanding changes from 'monitor'. Returns NULL if no update needs to
1603 * The caller should specify 'initial' as true if the returned JSON is going to
1604 * be used as part of the initial reply to a "monitor" request, false if it is
1605 * going to be used as part of an "update" notification. */
1606 static struct json *
1607 ovsdb_jsonrpc_monitor_compose_table_update(
1608 const struct ovsdb_jsonrpc_monitor *monitor, bool initial)
1610 struct shash_node *node;
1611 unsigned long int *changed;
1616 SHASH_FOR_EACH (node, &monitor->tables) {
1617 struct ovsdb_jsonrpc_monitor_table *mt = node->data;
1619 max_columns = MAX(max_columns, mt->n_columns);
1621 changed = xmalloc(bitmap_n_bytes(max_columns));
1624 SHASH_FOR_EACH (node, &monitor->tables) {
1625 struct ovsdb_jsonrpc_monitor_table *mt = node->data;
1626 struct ovsdb_jsonrpc_monitor_row *row, *next;
1627 struct json *table_json = NULL;
1629 HMAP_FOR_EACH_SAFE (row, next, hmap_node, &mt->changes) {
1630 struct json *row_json;
1632 row_json = ovsdb_jsonrpc_monitor_compose_row_update(
1633 mt, row, initial, changed);
1635 char uuid[UUID_LEN + 1];
1637 /* Create JSON object for transaction overall. */
1639 json = json_object_create();
1642 /* Create JSON object for transaction on this table. */
1644 table_json = json_object_create();
1645 json_object_put(json, mt->table->schema->name, table_json);
1648 /* Add JSON row to JSON table. */
1649 snprintf(uuid, sizeof uuid, UUID_FMT, UUID_ARGS(&row->uuid));
1650 json_object_put(table_json, uuid, row_json);
1653 hmap_remove(&mt->changes, &row->hmap_node);
1654 ovsdb_jsonrpc_monitor_row_destroy(mt, row);
1664 ovsdb_jsonrpc_monitor_needs_flush(struct ovsdb_jsonrpc_session *s)
1666 struct ovsdb_jsonrpc_monitor *m;
1668 HMAP_FOR_EACH (m, node, &s->monitors) {
1669 struct shash_node *node;
1671 SHASH_FOR_EACH (node, &m->tables) {
1672 struct ovsdb_jsonrpc_monitor_table *mt = node->data;
1674 if (!hmap_is_empty(&mt->changes)) {
1684 ovsdb_jsonrpc_monitor_flush_all(struct ovsdb_jsonrpc_session *s)
1686 struct ovsdb_jsonrpc_monitor *m;
1688 HMAP_FOR_EACH (m, node, &s->monitors) {
1691 json = ovsdb_jsonrpc_monitor_compose_table_update(m, false);
1693 struct jsonrpc_msg *msg;
1694 struct json *params;
1696 params = json_array_create_2(json_clone(m->monitor_id), json);
1697 msg = jsonrpc_create_notify("update", params);
1698 jsonrpc_session_send(s->js, msg);
1704 ovsdb_jsonrpc_monitor_init_aux(struct ovsdb_jsonrpc_monitor_aux *aux,
1705 const struct ovsdb_jsonrpc_monitor *m)
1711 static struct ovsdb_error *
1712 ovsdb_jsonrpc_monitor_commit(struct ovsdb_replica *replica,
1713 const struct ovsdb_txn *txn,
1714 bool durable OVS_UNUSED)
1716 struct ovsdb_jsonrpc_monitor *m = ovsdb_jsonrpc_monitor_cast(replica);
1717 struct ovsdb_jsonrpc_monitor_aux aux;
1719 ovsdb_jsonrpc_monitor_init_aux(&aux, m);
1720 ovsdb_txn_for_each_change(txn, ovsdb_jsonrpc_monitor_change_cb, &aux);
1725 static struct json *
1726 ovsdb_jsonrpc_monitor_get_initial(const struct ovsdb_jsonrpc_monitor *m)
1728 struct ovsdb_jsonrpc_monitor_aux aux;
1729 struct shash_node *node;
1732 ovsdb_jsonrpc_monitor_init_aux(&aux, m);
1733 SHASH_FOR_EACH (node, &m->tables) {
1734 struct ovsdb_jsonrpc_monitor_table *mt = node->data;
1736 if (mt->select & OJMS_INITIAL) {
1737 struct ovsdb_row *row;
1739 HMAP_FOR_EACH (row, hmap_node, &mt->table->rows) {
1740 ovsdb_jsonrpc_monitor_change_cb(NULL, row, NULL, &aux);
1744 json = ovsdb_jsonrpc_monitor_compose_table_update(m, true);
1745 return json ? json : json_object_create();
1749 ovsdb_jsonrpc_monitor_destroy(struct ovsdb_replica *replica)
1751 struct ovsdb_jsonrpc_monitor *m = ovsdb_jsonrpc_monitor_cast(replica);
1752 struct shash_node *node;
1754 json_destroy(m->monitor_id);
1755 SHASH_FOR_EACH (node, &m->tables) {
1756 struct ovsdb_jsonrpc_monitor_table *mt = node->data;
1757 struct ovsdb_jsonrpc_monitor_row *row, *next;
1759 HMAP_FOR_EACH_SAFE (row, next, hmap_node, &mt->changes) {
1760 hmap_remove(&mt->changes, &row->hmap_node);
1761 ovsdb_jsonrpc_monitor_row_destroy(mt, row);
1763 hmap_destroy(&mt->changes);
1768 shash_destroy(&m->tables);
1769 hmap_remove(&m->session->monitors, &m->node);
1773 static const struct ovsdb_replica_class ovsdb_jsonrpc_replica_class = {
1774 ovsdb_jsonrpc_monitor_commit,
1775 ovsdb_jsonrpc_monitor_destroy