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"
24 #include "dynamic-string.h"
27 #include "ovsdb-error.h"
28 #include "ovsdb-parser.h"
30 #include "reconnect.h"
37 #include "transaction.h"
41 VLOG_DEFINE_THIS_MODULE(ovsdb_jsonrpc_server);
43 struct ovsdb_jsonrpc_remote;
44 struct ovsdb_jsonrpc_session;
46 /* Message rate-limiting. */
47 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
50 static struct ovsdb_jsonrpc_session *ovsdb_jsonrpc_session_create(
51 struct ovsdb_jsonrpc_remote *, struct jsonrpc_session *);
52 static void ovsdb_jsonrpc_session_run_all(struct ovsdb_jsonrpc_remote *);
53 static void ovsdb_jsonrpc_session_wait_all(struct ovsdb_jsonrpc_remote *);
54 static void ovsdb_jsonrpc_session_get_memory_usage_all(
55 const struct ovsdb_jsonrpc_remote *, struct simap *usage);
56 static void ovsdb_jsonrpc_session_close_all(struct ovsdb_jsonrpc_remote *);
57 static void ovsdb_jsonrpc_session_reconnect_all(struct ovsdb_jsonrpc_remote *);
58 static void ovsdb_jsonrpc_session_set_all_options(
59 struct ovsdb_jsonrpc_remote *, const struct ovsdb_jsonrpc_options *);
60 static bool ovsdb_jsonrpc_session_get_status(
61 const struct ovsdb_jsonrpc_remote *,
62 struct ovsdb_jsonrpc_remote_status *);
63 static void ovsdb_jsonrpc_session_unlock_all(struct ovsdb_jsonrpc_session *);
64 static void ovsdb_jsonrpc_session_unlock__(struct ovsdb_lock_waiter *);
67 static void ovsdb_jsonrpc_trigger_create(struct ovsdb_jsonrpc_session *,
69 struct json *id, struct json *params);
70 static struct ovsdb_jsonrpc_trigger *ovsdb_jsonrpc_trigger_find(
71 struct ovsdb_jsonrpc_session *, const struct json *id, size_t hash);
72 static void ovsdb_jsonrpc_trigger_complete(struct ovsdb_jsonrpc_trigger *);
73 static void ovsdb_jsonrpc_trigger_complete_all(struct ovsdb_jsonrpc_session *);
74 static void ovsdb_jsonrpc_trigger_complete_done(
75 struct ovsdb_jsonrpc_session *);
78 static struct json *ovsdb_jsonrpc_monitor_create(
79 struct ovsdb_jsonrpc_session *, struct ovsdb *, struct json *params);
80 static struct jsonrpc_msg *ovsdb_jsonrpc_monitor_cancel(
81 struct ovsdb_jsonrpc_session *,
82 struct json_array *params,
83 const struct json *request_id);
84 static void ovsdb_jsonrpc_monitor_remove_all(struct ovsdb_jsonrpc_session *);
85 static size_t ovsdb_jsonrpc_monitor_json_length_all(
86 struct ovsdb_jsonrpc_session *);
88 /* JSON-RPC database server. */
90 struct ovsdb_jsonrpc_server {
91 struct ovsdb_server up;
92 unsigned int n_sessions, max_sessions;
93 struct shash remotes; /* Contains "struct ovsdb_jsonrpc_remote *"s. */
96 /* A configured remote. This is either a passive stream listener plus a list
97 * of the currently connected sessions, or a list of exactly one active
99 struct ovsdb_jsonrpc_remote {
100 struct ovsdb_jsonrpc_server *server;
101 struct pstream *listener; /* Listener, if passive. */
102 struct list sessions; /* List of "struct ovsdb_jsonrpc_session"s. */
106 static struct ovsdb_jsonrpc_remote *ovsdb_jsonrpc_server_add_remote(
107 struct ovsdb_jsonrpc_server *, const char *name,
108 const struct ovsdb_jsonrpc_options *options
110 static void ovsdb_jsonrpc_server_del_remote(struct shash_node *);
112 /* Creates and returns a new server to provide JSON-RPC access to an OVSDB.
114 * The caller must call ovsdb_jsonrpc_server_add_db() for each database to
115 * which 'server' should provide access. */
116 struct ovsdb_jsonrpc_server *
117 ovsdb_jsonrpc_server_create(void)
119 struct ovsdb_jsonrpc_server *server = xzalloc(sizeof *server);
120 ovsdb_server_init(&server->up);
121 server->max_sessions = 64;
122 shash_init(&server->remotes);
126 /* Adds 'db' to the set of databases served out by 'svr'. Returns true if
127 * successful, false if 'db''s name is the same as some database already in
130 ovsdb_jsonrpc_server_add_db(struct ovsdb_jsonrpc_server *svr, struct ovsdb *db)
132 return ovsdb_server_add_db(&svr->up, db);
136 ovsdb_jsonrpc_server_destroy(struct ovsdb_jsonrpc_server *svr)
138 struct shash_node *node, *next;
140 SHASH_FOR_EACH_SAFE (node, next, &svr->remotes) {
141 ovsdb_jsonrpc_server_del_remote(node);
143 shash_destroy(&svr->remotes);
144 ovsdb_server_destroy(&svr->up);
148 struct ovsdb_jsonrpc_options *
149 ovsdb_jsonrpc_default_options(const char *target)
151 struct ovsdb_jsonrpc_options *options = xzalloc(sizeof *options);
152 options->max_backoff = RECONNECT_DEFAULT_MAX_BACKOFF;
153 options->probe_interval = (stream_or_pstream_needs_probes(target)
154 ? RECONNECT_DEFAULT_PROBE_INTERVAL
159 /* Sets 'svr''s current set of remotes to the names in 'new_remotes', with
160 * options in the struct ovsdb_jsonrpc_options supplied as the data values.
162 * A remote is an active or passive stream connection method, e.g. "pssl:" or
165 ovsdb_jsonrpc_server_set_remotes(struct ovsdb_jsonrpc_server *svr,
166 const struct shash *new_remotes)
168 struct shash_node *node, *next;
170 SHASH_FOR_EACH_SAFE (node, next, &svr->remotes) {
171 if (!shash_find(new_remotes, node->name)) {
172 VLOG_INFO("%s: remote deconfigured", node->name);
173 ovsdb_jsonrpc_server_del_remote(node);
176 SHASH_FOR_EACH (node, new_remotes) {
177 const struct ovsdb_jsonrpc_options *options = node->data;
178 struct ovsdb_jsonrpc_remote *remote;
180 remote = shash_find_data(&svr->remotes, node->name);
182 remote = ovsdb_jsonrpc_server_add_remote(svr, node->name, options);
188 ovsdb_jsonrpc_session_set_all_options(remote, options);
192 static struct ovsdb_jsonrpc_remote *
193 ovsdb_jsonrpc_server_add_remote(struct ovsdb_jsonrpc_server *svr,
195 const struct ovsdb_jsonrpc_options *options)
197 struct ovsdb_jsonrpc_remote *remote;
198 struct pstream *listener;
201 error = jsonrpc_pstream_open(name, &listener, options->dscp);
202 if (error && error != EAFNOSUPPORT) {
203 VLOG_ERR_RL(&rl, "%s: listen failed: %s", name, strerror(error));
207 remote = xmalloc(sizeof *remote);
208 remote->server = svr;
209 remote->listener = listener;
210 list_init(&remote->sessions);
211 remote->dscp = options->dscp;
212 shash_add(&svr->remotes, name, remote);
215 ovsdb_jsonrpc_session_create(remote, jsonrpc_session_open(name, true));
221 ovsdb_jsonrpc_server_del_remote(struct shash_node *node)
223 struct ovsdb_jsonrpc_remote *remote = node->data;
225 ovsdb_jsonrpc_session_close_all(remote);
226 pstream_close(remote->listener);
227 shash_delete(&remote->server->remotes, node);
231 /* Stores status information for the remote named 'target', which should have
232 * been configured on 'svr' with a call to ovsdb_jsonrpc_server_set_remotes(),
233 * into '*status'. On success returns true, on failure (if 'svr' doesn't have
234 * a remote named 'target' or if that remote is an inbound remote that has no
235 * active connections) returns false. On failure, 'status' will be zeroed.
238 ovsdb_jsonrpc_server_get_remote_status(
239 const struct ovsdb_jsonrpc_server *svr, const char *target,
240 struct ovsdb_jsonrpc_remote_status *status)
242 const struct ovsdb_jsonrpc_remote *remote;
244 memset(status, 0, sizeof *status);
246 remote = shash_find_data(&svr->remotes, target);
247 return remote && ovsdb_jsonrpc_session_get_status(remote, status);
251 ovsdb_jsonrpc_server_free_remote_status(
252 struct ovsdb_jsonrpc_remote_status *status)
254 free(status->locks_held);
255 free(status->locks_waiting);
256 free(status->locks_lost);
259 /* Forces all of the JSON-RPC sessions managed by 'svr' to disconnect and
262 ovsdb_jsonrpc_server_reconnect(struct ovsdb_jsonrpc_server *svr)
264 struct shash_node *node;
266 SHASH_FOR_EACH (node, &svr->remotes) {
267 struct ovsdb_jsonrpc_remote *remote = node->data;
269 ovsdb_jsonrpc_session_reconnect_all(remote);
274 ovsdb_jsonrpc_server_run(struct ovsdb_jsonrpc_server *svr)
276 struct shash_node *node;
278 SHASH_FOR_EACH (node, &svr->remotes) {
279 struct ovsdb_jsonrpc_remote *remote = node->data;
281 if (remote->listener && svr->n_sessions < svr->max_sessions) {
282 struct stream *stream;
285 error = pstream_accept(remote->listener, &stream);
287 struct jsonrpc_session *js;
288 js = jsonrpc_session_open_unreliably(jsonrpc_open(stream),
290 ovsdb_jsonrpc_session_create(remote, js);
291 } else if (error != EAGAIN) {
292 VLOG_WARN_RL(&rl, "%s: accept failed: %s",
293 pstream_get_name(remote->listener),
298 ovsdb_jsonrpc_session_run_all(remote);
303 ovsdb_jsonrpc_server_wait(struct ovsdb_jsonrpc_server *svr)
305 struct shash_node *node;
307 SHASH_FOR_EACH (node, &svr->remotes) {
308 struct ovsdb_jsonrpc_remote *remote = node->data;
310 if (remote->listener && svr->n_sessions < svr->max_sessions) {
311 pstream_wait(remote->listener);
314 ovsdb_jsonrpc_session_wait_all(remote);
318 /* Adds some memory usage statistics for 'svr' into 'usage', for use with
319 * memory_report(). */
321 ovsdb_jsonrpc_server_get_memory_usage(const struct ovsdb_jsonrpc_server *svr,
324 struct shash_node *node;
326 simap_increase(usage, "sessions", svr->n_sessions);
327 SHASH_FOR_EACH (node, &svr->remotes) {
328 struct ovsdb_jsonrpc_remote *remote = node->data;
330 ovsdb_jsonrpc_session_get_memory_usage_all(remote, usage);
334 /* JSON-RPC database server session. */
336 struct ovsdb_jsonrpc_session {
337 struct list node; /* Element in remote's sessions list. */
338 struct ovsdb_session up;
339 struct ovsdb_jsonrpc_remote *remote;
340 size_t backlog_threshold; /* See ovsdb_jsonrpc_session_run(). */
341 size_t reply_backlog;
344 struct hmap triggers; /* Hmap of "struct ovsdb_jsonrpc_trigger"s. */
347 struct hmap monitors; /* Hmap of "struct ovsdb_jsonrpc_monitor"s. */
349 /* Network connectivity. */
350 struct jsonrpc_session *js; /* JSON-RPC session. */
351 unsigned int js_seqno; /* Last jsonrpc_session_get_seqno() value. */
354 static void ovsdb_jsonrpc_session_close(struct ovsdb_jsonrpc_session *);
355 static int ovsdb_jsonrpc_session_run(struct ovsdb_jsonrpc_session *);
356 static void ovsdb_jsonrpc_session_wait(struct ovsdb_jsonrpc_session *);
357 static void ovsdb_jsonrpc_session_get_memory_usage(
358 const struct ovsdb_jsonrpc_session *, struct simap *usage);
359 static void ovsdb_jsonrpc_session_set_options(
360 struct ovsdb_jsonrpc_session *, const struct ovsdb_jsonrpc_options *);
361 static void ovsdb_jsonrpc_session_got_request(struct ovsdb_jsonrpc_session *,
362 struct jsonrpc_msg *);
363 static void ovsdb_jsonrpc_session_got_notify(struct ovsdb_jsonrpc_session *,
364 struct jsonrpc_msg *);
366 static struct ovsdb_jsonrpc_session *
367 ovsdb_jsonrpc_session_create(struct ovsdb_jsonrpc_remote *remote,
368 struct jsonrpc_session *js)
370 struct ovsdb_jsonrpc_session *s;
372 s = xzalloc(sizeof *s);
373 ovsdb_session_init(&s->up, &remote->server->up);
375 list_push_back(&remote->sessions, &s->node);
376 hmap_init(&s->triggers);
377 hmap_init(&s->monitors);
378 s->reply_backlog = 0;
379 s->backlog_threshold = 1024 * 1024;
381 s->js_seqno = jsonrpc_session_get_seqno(js);
383 remote->server->n_sessions++;
389 ovsdb_jsonrpc_session_close(struct ovsdb_jsonrpc_session *s)
391 ovsdb_jsonrpc_monitor_remove_all(s);
392 ovsdb_jsonrpc_session_unlock_all(s);
393 ovsdb_jsonrpc_trigger_complete_all(s);
395 hmap_destroy(&s->monitors);
396 hmap_destroy(&s->triggers);
398 jsonrpc_session_close(s->js);
399 list_remove(&s->node);
400 s->remote->server->n_sessions--;
401 ovsdb_session_destroy(&s->up);
406 ovsdb_jsonrpc_session_run(struct ovsdb_jsonrpc_session *s)
410 jsonrpc_session_run(s->js);
411 if (s->js_seqno != jsonrpc_session_get_seqno(s->js)) {
412 s->js_seqno = jsonrpc_session_get_seqno(s->js);
413 ovsdb_jsonrpc_trigger_complete_all(s);
414 ovsdb_jsonrpc_monitor_remove_all(s);
415 ovsdb_jsonrpc_session_unlock_all(s);
418 ovsdb_jsonrpc_trigger_complete_done(s);
420 backlog = jsonrpc_session_get_backlog(s->js);
422 struct jsonrpc_msg *msg = jsonrpc_session_recv(s->js);
424 if (msg->type == JSONRPC_REQUEST) {
425 ovsdb_jsonrpc_session_got_request(s, msg);
426 } else if (msg->type == JSONRPC_NOTIFY) {
427 ovsdb_jsonrpc_session_got_notify(s, msg);
429 VLOG_WARN("%s: received unexpected %s message",
430 jsonrpc_session_get_name(s->js),
431 jsonrpc_msg_type_to_string(msg->type));
432 jsonrpc_session_force_reconnect(s->js);
433 jsonrpc_msg_destroy(msg);
436 s->reply_backlog = jsonrpc_session_get_backlog(s->js);
437 } else if (backlog > s->reply_backlog + s->backlog_threshold) {
438 /* We have a lot of data queued to send to the client. The data is
439 * likely to be mostly monitor updates. It is unlikely that the
440 * monitor updates are due to transactions by 's', because we will not
441 * let 's' make any more transactions until it drains its backlog to 0
442 * (see previous 'if' case). So the monitor updates are probably due
443 * to transactions made by database clients other than 's'. We can't
444 * fix that by preventing 's' from executing more transactions. We
445 * could fix it by preventing every client from executing transactions,
446 * but then one slow or hung client could prevent other clients from
449 * Our solution is to cap the maximum backlog to O(1) in the amount of
450 * data in the database. If the backlog exceeds that amount, then we
451 * disconnect the client. When it reconnects, it can fetch the entire
452 * contents of the database using less data than was previously
454 size_t monitor_length;
456 monitor_length = ovsdb_jsonrpc_monitor_json_length_all(s);
457 if (backlog > s->reply_backlog + monitor_length * 2) {
458 VLOG_INFO("%s: %zu bytes backlogged but a complete replica "
459 "would only take %zu bytes, disconnecting",
460 jsonrpc_session_get_name(s->js),
461 backlog - s->reply_backlog, monitor_length);
462 jsonrpc_session_force_reconnect(s->js);
464 /* The backlog is not unreasonably big. Only check again after it
465 * becomes much bigger. */
466 s->backlog_threshold = 2 * MAX(s->backlog_threshold * 2,
470 return jsonrpc_session_is_alive(s->js) ? 0 : ETIMEDOUT;
474 ovsdb_jsonrpc_session_set_options(struct ovsdb_jsonrpc_session *session,
475 const struct ovsdb_jsonrpc_options *options)
477 jsonrpc_session_set_max_backoff(session->js, options->max_backoff);
478 jsonrpc_session_set_probe_interval(session->js, options->probe_interval);
479 jsonrpc_session_set_dscp(session->js, options->dscp);
483 ovsdb_jsonrpc_session_run_all(struct ovsdb_jsonrpc_remote *remote)
485 struct ovsdb_jsonrpc_session *s, *next;
487 LIST_FOR_EACH_SAFE (s, next, node, &remote->sessions) {
488 int error = ovsdb_jsonrpc_session_run(s);
490 ovsdb_jsonrpc_session_close(s);
496 ovsdb_jsonrpc_session_wait(struct ovsdb_jsonrpc_session *s)
498 jsonrpc_session_wait(s->js);
499 if (!jsonrpc_session_get_backlog(s->js)) {
500 jsonrpc_session_recv_wait(s->js);
505 ovsdb_jsonrpc_session_wait_all(struct ovsdb_jsonrpc_remote *remote)
507 struct ovsdb_jsonrpc_session *s;
509 LIST_FOR_EACH (s, node, &remote->sessions) {
510 ovsdb_jsonrpc_session_wait(s);
515 ovsdb_jsonrpc_session_get_memory_usage(const struct ovsdb_jsonrpc_session *s,
518 simap_increase(usage, "triggers", hmap_count(&s->triggers));
519 simap_increase(usage, "monitors", hmap_count(&s->monitors));
520 simap_increase(usage, "backlog", jsonrpc_session_get_backlog(s->js));
524 ovsdb_jsonrpc_session_get_memory_usage_all(
525 const struct ovsdb_jsonrpc_remote *remote,
528 struct ovsdb_jsonrpc_session *s;
530 LIST_FOR_EACH (s, node, &remote->sessions) {
531 ovsdb_jsonrpc_session_get_memory_usage(s, usage);
536 ovsdb_jsonrpc_session_close_all(struct ovsdb_jsonrpc_remote *remote)
538 struct ovsdb_jsonrpc_session *s, *next;
540 LIST_FOR_EACH_SAFE (s, next, node, &remote->sessions) {
541 ovsdb_jsonrpc_session_close(s);
545 /* Forces all of the JSON-RPC sessions managed by 'remote' to disconnect and
548 ovsdb_jsonrpc_session_reconnect_all(struct ovsdb_jsonrpc_remote *remote)
550 struct ovsdb_jsonrpc_session *s, *next;
552 LIST_FOR_EACH_SAFE (s, next, node, &remote->sessions) {
553 jsonrpc_session_force_reconnect(s->js);
554 if (!jsonrpc_session_is_alive(s->js)) {
555 ovsdb_jsonrpc_session_close(s);
560 /* Sets the options for all of the JSON-RPC sessions managed by 'remote' to
563 ovsdb_jsonrpc_session_set_all_options(
564 struct ovsdb_jsonrpc_remote *remote,
565 const struct ovsdb_jsonrpc_options *options)
567 struct ovsdb_jsonrpc_session *s;
569 if (remote->listener) {
572 error = pstream_set_dscp(remote->listener, options->dscp);
574 VLOG_ERR("%s: set_dscp failed %s",
575 pstream_get_name(remote->listener), strerror(error));
577 remote->dscp = options->dscp;
580 * XXX race window between setting dscp to listening socket
581 * and accepting socket. Accepted socket may have old dscp value.
582 * Ignore this race window for now.
585 LIST_FOR_EACH (s, node, &remote->sessions) {
586 ovsdb_jsonrpc_session_set_options(s, options);
591 ovsdb_jsonrpc_session_get_status(const struct ovsdb_jsonrpc_remote *remote,
592 struct ovsdb_jsonrpc_remote_status *status)
594 const struct ovsdb_jsonrpc_session *s;
595 const struct jsonrpc_session *js;
596 struct ovsdb_lock_waiter *waiter;
597 struct reconnect_stats rstats;
598 struct ds locks_held, locks_waiting, locks_lost;
600 if (list_is_empty(&remote->sessions)) {
603 s = CONTAINER_OF(remote->sessions.next, struct ovsdb_jsonrpc_session, node);
606 status->is_connected = jsonrpc_session_is_connected(js);
607 status->last_error = jsonrpc_session_get_status(js);
609 jsonrpc_session_get_reconnect_stats(js, &rstats);
610 status->state = rstats.state;
611 status->sec_since_connect = rstats.msec_since_connect == UINT_MAX
612 ? UINT_MAX : rstats.msec_since_connect / 1000;
613 status->sec_since_disconnect = rstats.msec_since_disconnect == UINT_MAX
614 ? UINT_MAX : rstats.msec_since_disconnect / 1000;
616 ds_init(&locks_held);
617 ds_init(&locks_waiting);
618 ds_init(&locks_lost);
619 HMAP_FOR_EACH (waiter, session_node, &s->up.waiters) {
622 string = (ovsdb_lock_waiter_is_owner(waiter) ? &locks_held
623 : waiter->mode == OVSDB_LOCK_WAIT ? &locks_waiting
625 if (string->length) {
626 ds_put_char(string, ' ');
628 ds_put_cstr(string, waiter->lock_name);
630 status->locks_held = ds_steal_cstr(&locks_held);
631 status->locks_waiting = ds_steal_cstr(&locks_waiting);
632 status->locks_lost = ds_steal_cstr(&locks_lost);
634 status->n_connections = list_size(&remote->sessions);
639 /* Examines 'request' to determine the database to which it relates, and then
640 * searches 's' to find that database:
642 * - If successful, returns the database and sets '*replyp' to NULL.
644 * - If no such database exists, returns NULL and sets '*replyp' to an
645 * appropriate JSON-RPC error reply, owned by the caller. */
646 static struct ovsdb *
647 ovsdb_jsonrpc_lookup_db(const struct ovsdb_jsonrpc_session *s,
648 const struct jsonrpc_msg *request,
649 struct jsonrpc_msg **replyp)
651 struct json_array *params;
652 struct ovsdb_error *error;
656 params = json_array(request->params);
657 if (!params->n || params->elems[0]->type != JSON_STRING) {
658 error = ovsdb_syntax_error(
659 request->params, NULL,
660 "%s request params must begin with <db-name>", request->method);
664 db_name = params->elems[0]->u.string;
665 db = shash_find_data(&s->up.server->dbs, db_name);
667 error = ovsdb_syntax_error(
668 request->params, "unknown database",
669 "%s request specifies unknown database %s",
670 request->method, db_name);
678 *replyp = jsonrpc_create_reply(ovsdb_error_to_json(error), request->id);
679 ovsdb_error_destroy(error);
683 static struct ovsdb_error *
684 ovsdb_jsonrpc_session_parse_lock_name(const struct jsonrpc_msg *request,
685 const char **lock_namep)
687 const struct json_array *params;
689 params = json_array(request->params);
690 if (params->n != 1 || params->elems[0]->type != JSON_STRING ||
691 !ovsdb_parser_is_id(json_string(params->elems[0]))) {
693 return ovsdb_syntax_error(request->params, NULL,
694 "%s request params must be <id>",
698 *lock_namep = json_string(params->elems[0]);
703 ovsdb_jsonrpc_session_notify(struct ovsdb_session *session,
704 const char *lock_name,
707 struct ovsdb_jsonrpc_session *s;
710 s = CONTAINER_OF(session, struct ovsdb_jsonrpc_session, up);
711 params = json_array_create_1(json_string_create(lock_name));
712 jsonrpc_session_send(s->js, jsonrpc_create_notify(method, params));
715 static struct jsonrpc_msg *
716 ovsdb_jsonrpc_session_lock(struct ovsdb_jsonrpc_session *s,
717 struct jsonrpc_msg *request,
718 enum ovsdb_lock_mode mode)
720 struct ovsdb_lock_waiter *waiter;
721 struct jsonrpc_msg *reply;
722 struct ovsdb_error *error;
723 struct ovsdb_session *victim;
724 const char *lock_name;
727 error = ovsdb_jsonrpc_session_parse_lock_name(request, &lock_name);
732 /* Report error if this session has issued a "lock" or "steal" without a
733 * matching "unlock" for this lock. */
734 waiter = ovsdb_session_get_lock_waiter(&s->up, lock_name);
736 error = ovsdb_syntax_error(
737 request->params, NULL,
738 "must issue \"unlock\" before new \"%s\"", request->method);
742 /* Get the lock, add us as a waiter. */
743 waiter = ovsdb_server_lock(&s->remote->server->up, &s->up, lock_name, mode,
746 ovsdb_jsonrpc_session_notify(victim, lock_name, "stolen");
749 result = json_object_create();
750 json_object_put(result, "locked",
751 json_boolean_create(ovsdb_lock_waiter_is_owner(waiter)));
753 return jsonrpc_create_reply(result, request->id);
756 reply = jsonrpc_create_reply(ovsdb_error_to_json(error), request->id);
757 ovsdb_error_destroy(error);
762 ovsdb_jsonrpc_session_unlock_all(struct ovsdb_jsonrpc_session *s)
764 struct ovsdb_lock_waiter *waiter, *next;
766 HMAP_FOR_EACH_SAFE (waiter, next, session_node, &s->up.waiters) {
767 ovsdb_jsonrpc_session_unlock__(waiter);
772 ovsdb_jsonrpc_session_unlock__(struct ovsdb_lock_waiter *waiter)
774 struct ovsdb_lock *lock = waiter->lock;
777 struct ovsdb_session *new_owner = ovsdb_lock_waiter_remove(waiter);
779 ovsdb_jsonrpc_session_notify(new_owner, lock->name, "locked");
781 /* ovsdb_server_lock() might have freed 'lock'. */
785 ovsdb_lock_waiter_destroy(waiter);
788 static struct jsonrpc_msg *
789 ovsdb_jsonrpc_session_unlock(struct ovsdb_jsonrpc_session *s,
790 struct jsonrpc_msg *request)
792 struct ovsdb_lock_waiter *waiter;
793 struct jsonrpc_msg *reply;
794 struct ovsdb_error *error;
795 const char *lock_name;
797 error = ovsdb_jsonrpc_session_parse_lock_name(request, &lock_name);
802 /* Report error if this session has not issued a "lock" or "steal" for this
804 waiter = ovsdb_session_get_lock_waiter(&s->up, lock_name);
806 error = ovsdb_syntax_error(
807 request->params, NULL, "\"unlock\" without \"lock\" or \"steal\"");
811 ovsdb_jsonrpc_session_unlock__(waiter);
813 return jsonrpc_create_reply(json_object_create(), request->id);
816 reply = jsonrpc_create_reply(ovsdb_error_to_json(error), request->id);
817 ovsdb_error_destroy(error);
821 static struct jsonrpc_msg *
822 execute_transaction(struct ovsdb_jsonrpc_session *s, struct ovsdb *db,
823 struct jsonrpc_msg *request)
825 ovsdb_jsonrpc_trigger_create(s, db, request->id, request->params);
827 request->params = NULL;
828 jsonrpc_msg_destroy(request);
833 ovsdb_jsonrpc_session_got_request(struct ovsdb_jsonrpc_session *s,
834 struct jsonrpc_msg *request)
836 struct jsonrpc_msg *reply;
838 if (!strcmp(request->method, "transact")) {
839 struct ovsdb *db = ovsdb_jsonrpc_lookup_db(s, request, &reply);
841 reply = execute_transaction(s, db, request);
843 } else if (!strcmp(request->method, "monitor")) {
844 struct ovsdb *db = ovsdb_jsonrpc_lookup_db(s, request, &reply);
846 reply = jsonrpc_create_reply(
847 ovsdb_jsonrpc_monitor_create(s, db, request->params),
850 } else if (!strcmp(request->method, "monitor_cancel")) {
851 reply = ovsdb_jsonrpc_monitor_cancel(s, json_array(request->params),
853 } else if (!strcmp(request->method, "get_schema")) {
854 struct ovsdb *db = ovsdb_jsonrpc_lookup_db(s, request, &reply);
856 reply = jsonrpc_create_reply(ovsdb_schema_to_json(db->schema),
859 } else if (!strcmp(request->method, "list_dbs")) {
860 size_t n_dbs = shash_count(&s->up.server->dbs);
861 struct shash_node *node;
865 dbs = xmalloc(n_dbs * sizeof *dbs);
867 SHASH_FOR_EACH (node, &s->up.server->dbs) {
868 dbs[i++] = json_string_create(node->name);
870 reply = jsonrpc_create_reply(json_array_create(dbs, n_dbs),
872 } else if (!strcmp(request->method, "lock")) {
873 reply = ovsdb_jsonrpc_session_lock(s, request, OVSDB_LOCK_WAIT);
874 } else if (!strcmp(request->method, "steal")) {
875 reply = ovsdb_jsonrpc_session_lock(s, request, OVSDB_LOCK_STEAL);
876 } else if (!strcmp(request->method, "unlock")) {
877 reply = ovsdb_jsonrpc_session_unlock(s, request);
878 } else if (!strcmp(request->method, "echo")) {
879 reply = jsonrpc_create_reply(json_clone(request->params), request->id);
881 reply = jsonrpc_create_error(json_string_create("unknown method"),
886 jsonrpc_msg_destroy(request);
887 jsonrpc_session_send(s->js, reply);
892 execute_cancel(struct ovsdb_jsonrpc_session *s, struct jsonrpc_msg *request)
894 if (json_array(request->params)->n == 1) {
895 struct ovsdb_jsonrpc_trigger *t;
898 id = request->params->u.array.elems[0];
899 t = ovsdb_jsonrpc_trigger_find(s, id, json_hash(id, 0));
901 ovsdb_jsonrpc_trigger_complete(t);
907 ovsdb_jsonrpc_session_got_notify(struct ovsdb_jsonrpc_session *s,
908 struct jsonrpc_msg *request)
910 if (!strcmp(request->method, "cancel")) {
911 execute_cancel(s, request);
913 jsonrpc_msg_destroy(request);
916 /* JSON-RPC database server triggers.
918 * (Every transaction is treated as a trigger even if it doesn't actually have
919 * any "wait" operations.) */
921 struct ovsdb_jsonrpc_trigger {
922 struct ovsdb_trigger trigger;
923 struct hmap_node hmap_node; /* In session's "triggers" hmap. */
928 ovsdb_jsonrpc_trigger_create(struct ovsdb_jsonrpc_session *s, struct ovsdb *db,
929 struct json *id, struct json *params)
931 struct ovsdb_jsonrpc_trigger *t;
934 /* Check for duplicate ID. */
935 hash = json_hash(id, 0);
936 t = ovsdb_jsonrpc_trigger_find(s, id, hash);
938 struct jsonrpc_msg *msg;
940 msg = jsonrpc_create_error(json_string_create("duplicate request ID"),
942 jsonrpc_session_send(s->js, msg);
944 json_destroy(params);
948 /* Insert into trigger table. */
949 t = xmalloc(sizeof *t);
950 ovsdb_trigger_init(&s->up, db, &t->trigger, params, time_msec());
952 hmap_insert(&s->triggers, &t->hmap_node, hash);
954 /* Complete early if possible. */
955 if (ovsdb_trigger_is_complete(&t->trigger)) {
956 ovsdb_jsonrpc_trigger_complete(t);
960 static struct ovsdb_jsonrpc_trigger *
961 ovsdb_jsonrpc_trigger_find(struct ovsdb_jsonrpc_session *s,
962 const struct json *id, size_t hash)
964 struct ovsdb_jsonrpc_trigger *t;
966 HMAP_FOR_EACH_WITH_HASH (t, hmap_node, hash, &s->triggers) {
967 if (json_equal(t->id, id)) {
976 ovsdb_jsonrpc_trigger_complete(struct ovsdb_jsonrpc_trigger *t)
978 struct ovsdb_jsonrpc_session *s;
980 s = CONTAINER_OF(t->trigger.session, struct ovsdb_jsonrpc_session, up);
982 if (jsonrpc_session_is_connected(s->js)) {
983 struct jsonrpc_msg *reply;
986 result = ovsdb_trigger_steal_result(&t->trigger);
988 reply = jsonrpc_create_reply(result, t->id);
990 reply = jsonrpc_create_error(json_string_create("canceled"),
993 jsonrpc_session_send(s->js, reply);
997 ovsdb_trigger_destroy(&t->trigger);
998 hmap_remove(&s->triggers, &t->hmap_node);
1003 ovsdb_jsonrpc_trigger_complete_all(struct ovsdb_jsonrpc_session *s)
1005 struct ovsdb_jsonrpc_trigger *t, *next;
1006 HMAP_FOR_EACH_SAFE (t, next, hmap_node, &s->triggers) {
1007 ovsdb_jsonrpc_trigger_complete(t);
1012 ovsdb_jsonrpc_trigger_complete_done(struct ovsdb_jsonrpc_session *s)
1014 while (!list_is_empty(&s->up.completions)) {
1015 struct ovsdb_jsonrpc_trigger *t
1016 = CONTAINER_OF(s->up.completions.next,
1017 struct ovsdb_jsonrpc_trigger, trigger.node);
1018 ovsdb_jsonrpc_trigger_complete(t);
1022 /* JSON-RPC database table monitors. */
1024 enum ovsdb_jsonrpc_monitor_selection {
1025 OJMS_INITIAL = 1 << 0, /* All rows when monitor is created. */
1026 OJMS_INSERT = 1 << 1, /* New rows. */
1027 OJMS_DELETE = 1 << 2, /* Deleted rows. */
1028 OJMS_MODIFY = 1 << 3 /* Modified rows. */
1031 /* A particular column being monitored. */
1032 struct ovsdb_jsonrpc_monitor_column {
1033 const struct ovsdb_column *column;
1034 enum ovsdb_jsonrpc_monitor_selection select;
1037 /* A particular table being monitored. */
1038 struct ovsdb_jsonrpc_monitor_table {
1039 const struct ovsdb_table *table;
1041 /* This is the union (bitwise-OR) of the 'select' values in all of the
1042 * members of 'columns' below. */
1043 enum ovsdb_jsonrpc_monitor_selection select;
1045 /* Columns being monitored. */
1046 struct ovsdb_jsonrpc_monitor_column *columns;
1050 /* A collection of tables being monitored. */
1051 struct ovsdb_jsonrpc_monitor {
1052 struct ovsdb_replica replica;
1053 struct ovsdb_jsonrpc_session *session;
1055 struct hmap_node node; /* In ovsdb_jsonrpc_session's "monitors". */
1057 struct json *monitor_id;
1058 struct shash tables; /* Holds "struct ovsdb_jsonrpc_monitor_table"s. */
1061 static const struct ovsdb_replica_class ovsdb_jsonrpc_replica_class;
1063 struct ovsdb_jsonrpc_monitor *ovsdb_jsonrpc_monitor_find(
1064 struct ovsdb_jsonrpc_session *, const struct json *monitor_id);
1065 static void ovsdb_jsonrpc_monitor_destroy(struct ovsdb_replica *);
1066 static struct json *ovsdb_jsonrpc_monitor_get_initial(
1067 const struct ovsdb_jsonrpc_monitor *);
1068 static size_t ovsdb_jsonrpc_monitor_json_length(
1069 const struct ovsdb_jsonrpc_monitor *);
1072 parse_bool(struct ovsdb_parser *parser, const char *name, bool default_value)
1074 const struct json *json;
1076 json = ovsdb_parser_member(parser, name, OP_BOOLEAN | OP_OPTIONAL);
1077 return json ? json_boolean(json) : default_value;
1080 struct ovsdb_jsonrpc_monitor *
1081 ovsdb_jsonrpc_monitor_find(struct ovsdb_jsonrpc_session *s,
1082 const struct json *monitor_id)
1084 struct ovsdb_jsonrpc_monitor *m;
1086 HMAP_FOR_EACH_WITH_HASH (m, node, json_hash(monitor_id, 0), &s->monitors) {
1087 if (json_equal(m->monitor_id, monitor_id)) {
1096 ovsdb_jsonrpc_add_monitor_column(struct ovsdb_jsonrpc_monitor_table *mt,
1097 const struct ovsdb_column *column,
1098 enum ovsdb_jsonrpc_monitor_selection select,
1099 size_t *allocated_columns)
1101 struct ovsdb_jsonrpc_monitor_column *c;
1103 if (mt->n_columns >= *allocated_columns) {
1104 mt->columns = x2nrealloc(mt->columns, allocated_columns,
1105 sizeof *mt->columns);
1108 c = &mt->columns[mt->n_columns++];
1114 compare_ovsdb_jsonrpc_monitor_column(const void *a_, const void *b_)
1116 const struct ovsdb_jsonrpc_monitor_column *a = a_;
1117 const struct ovsdb_jsonrpc_monitor_column *b = b_;
1119 return a->column < b->column ? -1 : a->column > b->column;
1122 static struct ovsdb_error * WARN_UNUSED_RESULT
1123 ovsdb_jsonrpc_parse_monitor_request(struct ovsdb_jsonrpc_monitor_table *mt,
1124 const struct json *monitor_request,
1125 size_t *allocated_columns)
1127 const struct ovsdb_table_schema *ts = mt->table->schema;
1128 enum ovsdb_jsonrpc_monitor_selection select;
1129 const struct json *columns, *select_json;
1130 struct ovsdb_parser parser;
1131 struct ovsdb_error *error;
1133 ovsdb_parser_init(&parser, monitor_request, "table %s", ts->name);
1134 columns = ovsdb_parser_member(&parser, "columns", OP_ARRAY | OP_OPTIONAL);
1135 select_json = ovsdb_parser_member(&parser, "select",
1136 OP_OBJECT | OP_OPTIONAL);
1137 error = ovsdb_parser_finish(&parser);
1144 ovsdb_parser_init(&parser, select_json, "table %s select", ts->name);
1145 if (parse_bool(&parser, "initial", true)) {
1146 select |= OJMS_INITIAL;
1148 if (parse_bool(&parser, "insert", true)) {
1149 select |= OJMS_INSERT;
1151 if (parse_bool(&parser, "delete", true)) {
1152 select |= OJMS_DELETE;
1154 if (parse_bool(&parser, "modify", true)) {
1155 select |= OJMS_MODIFY;
1157 error = ovsdb_parser_finish(&parser);
1162 select = OJMS_INITIAL | OJMS_INSERT | OJMS_DELETE | OJMS_MODIFY;
1164 mt->select |= select;
1169 if (columns->type != JSON_ARRAY) {
1170 return ovsdb_syntax_error(columns, NULL,
1171 "array of column names expected");
1174 for (i = 0; i < columns->u.array.n; i++) {
1175 const struct ovsdb_column *column;
1178 if (columns->u.array.elems[i]->type != JSON_STRING) {
1179 return ovsdb_syntax_error(columns, NULL,
1180 "array of column names expected");
1183 s = columns->u.array.elems[i]->u.string;
1184 column = shash_find_data(&mt->table->schema->columns, s);
1186 return ovsdb_syntax_error(columns, NULL, "%s is not a valid "
1189 ovsdb_jsonrpc_add_monitor_column(mt, column, select,
1193 struct shash_node *node;
1195 SHASH_FOR_EACH (node, &ts->columns) {
1196 const struct ovsdb_column *column = node->data;
1197 if (column->index != OVSDB_COL_UUID) {
1198 ovsdb_jsonrpc_add_monitor_column(mt, column, select,
1207 static struct json *
1208 ovsdb_jsonrpc_monitor_create(struct ovsdb_jsonrpc_session *s, struct ovsdb *db,
1209 struct json *params)
1211 struct ovsdb_jsonrpc_monitor *m = NULL;
1212 struct json *monitor_id, *monitor_requests;
1213 struct ovsdb_error *error = NULL;
1214 struct shash_node *node;
1217 if (json_array(params)->n != 3) {
1218 error = ovsdb_syntax_error(params, NULL, "invalid parameters");
1221 monitor_id = params->u.array.elems[1];
1222 monitor_requests = params->u.array.elems[2];
1223 if (monitor_requests->type != JSON_OBJECT) {
1224 error = ovsdb_syntax_error(monitor_requests, NULL,
1225 "monitor-requests must be object");
1229 if (ovsdb_jsonrpc_monitor_find(s, monitor_id)) {
1230 error = ovsdb_syntax_error(monitor_id, NULL, "duplicate monitor ID");
1234 m = xzalloc(sizeof *m);
1235 ovsdb_replica_init(&m->replica, &ovsdb_jsonrpc_replica_class);
1236 ovsdb_add_replica(db, &m->replica);
1239 hmap_insert(&s->monitors, &m->node, json_hash(monitor_id, 0));
1240 m->monitor_id = json_clone(monitor_id);
1241 shash_init(&m->tables);
1243 SHASH_FOR_EACH (node, json_object(monitor_requests)) {
1244 const struct ovsdb_table *table;
1245 struct ovsdb_jsonrpc_monitor_table *mt;
1246 size_t allocated_columns;
1247 const struct json *mr_value;
1250 table = ovsdb_get_table(m->db, node->name);
1252 error = ovsdb_syntax_error(NULL, NULL,
1253 "no table named %s", node->name);
1257 mt = xzalloc(sizeof *mt);
1259 shash_add(&m->tables, table->schema->name, mt);
1261 /* Parse columns. */
1262 mr_value = node->data;
1263 allocated_columns = 0;
1264 if (mr_value->type == JSON_ARRAY) {
1265 const struct json_array *array = &mr_value->u.array;
1267 for (i = 0; i < array->n; i++) {
1268 error = ovsdb_jsonrpc_parse_monitor_request(
1269 mt, array->elems[i], &allocated_columns);
1275 error = ovsdb_jsonrpc_parse_monitor_request(
1276 mt, mr_value, &allocated_columns);
1282 /* Check for duplicate columns. */
1283 qsort(mt->columns, mt->n_columns, sizeof *mt->columns,
1284 compare_ovsdb_jsonrpc_monitor_column);
1285 for (i = 1; i < mt->n_columns; i++) {
1286 if (mt->columns[i].column == mt->columns[i - 1].column) {
1287 error = ovsdb_syntax_error(mr_value, NULL, "column %s "
1288 "mentioned more than once",
1289 mt->columns[i].column->name);
1295 return ovsdb_jsonrpc_monitor_get_initial(m);
1299 ovsdb_remove_replica(m->db, &m->replica);
1302 json = ovsdb_error_to_json(error);
1303 ovsdb_error_destroy(error);
1307 static struct jsonrpc_msg *
1308 ovsdb_jsonrpc_monitor_cancel(struct ovsdb_jsonrpc_session *s,
1309 struct json_array *params,
1310 const struct json *request_id)
1312 if (params->n != 1) {
1313 return jsonrpc_create_error(json_string_create("invalid parameters"),
1316 struct ovsdb_jsonrpc_monitor *m;
1318 m = ovsdb_jsonrpc_monitor_find(s, params->elems[0]);
1320 return jsonrpc_create_error(json_string_create("unknown monitor"),
1323 ovsdb_remove_replica(m->db, &m->replica);
1324 return jsonrpc_create_reply(json_object_create(), request_id);
1330 ovsdb_jsonrpc_monitor_remove_all(struct ovsdb_jsonrpc_session *s)
1332 struct ovsdb_jsonrpc_monitor *m, *next;
1334 HMAP_FOR_EACH_SAFE (m, next, node, &s->monitors) {
1335 ovsdb_remove_replica(m->db, &m->replica);
1339 /* Returns an overestimate of the number of bytes of JSON data required to
1340 * report the current contents of the database over all the monitors currently
1341 * configured in 's'. */
1343 ovsdb_jsonrpc_monitor_json_length_all(struct ovsdb_jsonrpc_session *s)
1345 struct ovsdb_jsonrpc_monitor *m;
1349 HMAP_FOR_EACH (m, node, &s->monitors) {
1350 length += ovsdb_jsonrpc_monitor_json_length(m);
1355 static struct ovsdb_jsonrpc_monitor *
1356 ovsdb_jsonrpc_monitor_cast(struct ovsdb_replica *replica)
1358 ovs_assert(replica->class == &ovsdb_jsonrpc_replica_class);
1359 return CONTAINER_OF(replica, struct ovsdb_jsonrpc_monitor, replica);
1362 struct ovsdb_jsonrpc_monitor_aux {
1363 bool initial; /* Sending initial contents of table? */
1364 const struct ovsdb_jsonrpc_monitor *monitor;
1365 struct json *json; /* JSON for the whole transaction. */
1367 /* Current table. */
1368 struct ovsdb_jsonrpc_monitor_table *mt;
1369 struct json *table_json; /* JSON for table's transaction. */
1373 any_reportable_change(const struct ovsdb_jsonrpc_monitor_table *mt,
1374 const unsigned long int *changed)
1378 for (i = 0; i < mt->n_columns; i++) {
1379 const struct ovsdb_jsonrpc_monitor_column *c = &mt->columns[i];
1380 unsigned int idx = c->column->index;
1382 if (c->select & OJMS_MODIFY && bitmap_is_set(changed, idx)) {
1391 ovsdb_jsonrpc_monitor_change_cb(const struct ovsdb_row *old,
1392 const struct ovsdb_row *new,
1393 const unsigned long int *changed,
1396 struct ovsdb_jsonrpc_monitor_aux *aux = aux_;
1397 const struct ovsdb_jsonrpc_monitor *m = aux->monitor;
1398 struct ovsdb_table *table = new ? new->table : old->table;
1399 enum ovsdb_jsonrpc_monitor_selection type;
1400 struct json *old_json, *new_json;
1401 struct json *row_json;
1402 char uuid[UUID_LEN + 1];
1405 if (!aux->mt || table != aux->mt->table) {
1406 aux->mt = shash_find_data(&m->tables, table->schema->name);
1407 aux->table_json = NULL;
1409 /* We don't care about rows in this table at all. Tell the caller
1415 type = (aux->initial ? OJMS_INITIAL
1416 : !old ? OJMS_INSERT
1417 : !new ? OJMS_DELETE
1419 if (!(aux->mt->select & type)) {
1420 /* We don't care about this type of change (but do want to be called
1421 * back for changes to other rows in the same table). */
1425 if (type == OJMS_MODIFY && !any_reportable_change(aux->mt, changed)) {
1426 /* Nothing of interest changed. */
1430 old_json = new_json = NULL;
1431 if (type & (OJMS_DELETE | OJMS_MODIFY)) {
1432 old_json = json_object_create();
1434 if (type & (OJMS_INITIAL | OJMS_INSERT | OJMS_MODIFY)) {
1435 new_json = json_object_create();
1437 for (i = 0; i < aux->mt->n_columns; i++) {
1438 const struct ovsdb_jsonrpc_monitor_column *c = &aux->mt->columns[i];
1439 const struct ovsdb_column *column = c->column;
1440 unsigned int idx = c->column->index;
1442 if (!(type & c->select)) {
1443 /* We don't care about this type of change for this particular
1444 * column (but we will care about it for some other column). */
1448 if ((type == OJMS_MODIFY && bitmap_is_set(changed, idx))
1449 || type == OJMS_DELETE) {
1450 json_object_put(old_json, column->name,
1451 ovsdb_datum_to_json(&old->fields[idx],
1454 if (type & (OJMS_INITIAL | OJMS_INSERT | OJMS_MODIFY)) {
1455 json_object_put(new_json, column->name,
1456 ovsdb_datum_to_json(&new->fields[idx],
1461 /* Create JSON object for transaction overall. */
1463 aux->json = json_object_create();
1466 /* Create JSON object for transaction on this table. */
1467 if (!aux->table_json) {
1468 aux->table_json = json_object_create();
1469 json_object_put(aux->json, aux->mt->table->schema->name,
1473 /* Create JSON object for transaction on this row. */
1474 row_json = json_object_create();
1476 json_object_put(row_json, "old", old_json);
1479 json_object_put(row_json, "new", new_json);
1482 /* Add JSON row to JSON table. */
1483 snprintf(uuid, sizeof uuid,
1484 UUID_FMT, UUID_ARGS(ovsdb_row_get_uuid(new ? new : old)));
1485 json_object_put(aux->table_json, uuid, row_json);
1490 /* Returns an overestimate of the number of bytes of JSON data required to
1491 * report the current contents of the database over monitor 'm'. */
1493 ovsdb_jsonrpc_monitor_json_length(const struct ovsdb_jsonrpc_monitor *m)
1495 const struct shash_node *node;
1498 /* Top-level overhead of monitor JSON. */
1501 SHASH_FOR_EACH (node, &m->tables) {
1502 const struct ovsdb_jsonrpc_monitor_table *mt = node->data;
1503 const struct ovsdb_table *table = mt->table;
1504 const struct ovsdb_row *row;
1507 /* Per-table JSON overhead: "<table>":{...}. */
1508 length += strlen(table->schema->name) + 32;
1510 /* Per-row JSON overhead: ,"<uuid>":{"old":{...},"new":{...}} */
1511 length += hmap_count(&table->rows) * (UUID_LEN + 32);
1513 /* Per-row, per-column JSON overhead: ,"<column>": */
1514 for (i = 0; i < mt->n_columns; i++) {
1515 const struct ovsdb_jsonrpc_monitor_column *c = &mt->columns[i];
1516 const struct ovsdb_column *column = c->column;
1518 length += hmap_count(&table->rows) * (8 + strlen(column->name));
1522 HMAP_FOR_EACH (row, hmap_node, &table->rows) {
1523 for (i = 0; i < mt->n_columns; i++) {
1524 const struct ovsdb_jsonrpc_monitor_column *c = &mt->columns[i];
1525 const struct ovsdb_column *column = c->column;
1527 length += ovsdb_datum_json_length(&row->fields[column->index],
1537 ovsdb_jsonrpc_monitor_init_aux(struct ovsdb_jsonrpc_monitor_aux *aux,
1538 const struct ovsdb_jsonrpc_monitor *m,
1541 aux->initial = initial;
1545 aux->table_json = NULL;
1548 static struct ovsdb_error *
1549 ovsdb_jsonrpc_monitor_commit(struct ovsdb_replica *replica,
1550 const struct ovsdb_txn *txn,
1551 bool durable OVS_UNUSED)
1553 struct ovsdb_jsonrpc_monitor *m = ovsdb_jsonrpc_monitor_cast(replica);
1554 struct ovsdb_jsonrpc_monitor_aux aux;
1556 ovsdb_jsonrpc_monitor_init_aux(&aux, m, false);
1557 ovsdb_txn_for_each_change(txn, ovsdb_jsonrpc_monitor_change_cb, &aux);
1559 struct jsonrpc_msg *msg;
1560 struct json *params;
1562 params = json_array_create_2(json_clone(aux.monitor->monitor_id),
1564 msg = jsonrpc_create_notify("update", params);
1565 jsonrpc_session_send(aux.monitor->session->js, msg);
1571 static struct json *
1572 ovsdb_jsonrpc_monitor_get_initial(const struct ovsdb_jsonrpc_monitor *m)
1574 struct ovsdb_jsonrpc_monitor_aux aux;
1575 struct shash_node *node;
1577 ovsdb_jsonrpc_monitor_init_aux(&aux, m, true);
1578 SHASH_FOR_EACH (node, &m->tables) {
1579 struct ovsdb_jsonrpc_monitor_table *mt = node->data;
1581 if (mt->select & OJMS_INITIAL) {
1582 struct ovsdb_row *row;
1584 HMAP_FOR_EACH (row, hmap_node, &mt->table->rows) {
1585 ovsdb_jsonrpc_monitor_change_cb(NULL, row, NULL, &aux);
1589 return aux.json ? aux.json : json_object_create();
1593 ovsdb_jsonrpc_monitor_destroy(struct ovsdb_replica *replica)
1595 struct ovsdb_jsonrpc_monitor *m = ovsdb_jsonrpc_monitor_cast(replica);
1596 struct shash_node *node;
1598 json_destroy(m->monitor_id);
1599 SHASH_FOR_EACH (node, &m->tables) {
1600 struct ovsdb_jsonrpc_monitor_table *mt = node->data;
1604 shash_destroy(&m->tables);
1605 hmap_remove(&m->session->monitors, &m->node);
1609 static const struct ovsdb_replica_class ovsdb_jsonrpc_replica_class = {
1610 ovsdb_jsonrpc_monitor_commit,
1611 ovsdb_jsonrpc_monitor_destroy