1 /* Copyright (c) 2009, 2010, 2011, 2012 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 *,
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 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 *);
86 /* JSON-RPC database server. */
88 struct ovsdb_jsonrpc_server {
89 struct ovsdb_server up;
90 unsigned int n_sessions, max_sessions;
91 struct shash remotes; /* Contains "struct ovsdb_jsonrpc_remote *"s. */
94 /* A configured remote. This is either a passive stream listener plus a list
95 * of the currently connected sessions, or a list of exactly one active
97 struct ovsdb_jsonrpc_remote {
98 struct ovsdb_jsonrpc_server *server;
99 struct pstream *listener; /* Listener, if passive. */
100 struct list sessions; /* List of "struct ovsdb_jsonrpc_session"s. */
103 static struct ovsdb_jsonrpc_remote *ovsdb_jsonrpc_server_add_remote(
104 struct ovsdb_jsonrpc_server *, const char *name,
105 const struct ovsdb_jsonrpc_options *options
107 static void ovsdb_jsonrpc_server_del_remote(struct shash_node *);
109 struct ovsdb_jsonrpc_server *
110 ovsdb_jsonrpc_server_create(struct ovsdb *db)
112 struct ovsdb_jsonrpc_server *server = xzalloc(sizeof *server);
113 ovsdb_server_init(&server->up, db);
114 server->max_sessions = 64;
115 shash_init(&server->remotes);
120 ovsdb_jsonrpc_server_destroy(struct ovsdb_jsonrpc_server *svr)
122 struct shash_node *node, *next;
124 SHASH_FOR_EACH_SAFE (node, next, &svr->remotes) {
125 ovsdb_jsonrpc_server_del_remote(node);
127 shash_destroy(&svr->remotes);
128 ovsdb_server_destroy(&svr->up);
132 struct ovsdb_jsonrpc_options *
133 ovsdb_jsonrpc_default_options(const char *target)
135 struct ovsdb_jsonrpc_options *options = xzalloc(sizeof *options);
136 options->max_backoff = RECONNECT_DEFAULT_MAX_BACKOFF;
137 options->probe_interval = (stream_or_pstream_needs_probes(target)
138 ? RECONNECT_DEFAULT_PROBE_INTERVAL
143 /* Sets 'svr''s current set of remotes to the names in 'new_remotes', with
144 * options in the struct ovsdb_jsonrpc_options supplied as the data values.
146 * A remote is an active or passive stream connection method, e.g. "pssl:" or
149 ovsdb_jsonrpc_server_set_remotes(struct ovsdb_jsonrpc_server *svr,
150 const struct shash *new_remotes)
152 struct shash_node *node, *next;
154 SHASH_FOR_EACH_SAFE (node, next, &svr->remotes) {
155 if (!shash_find(new_remotes, node->name)) {
156 VLOG_INFO("%s: remote deconfigured", node->name);
157 ovsdb_jsonrpc_server_del_remote(node);
160 SHASH_FOR_EACH (node, new_remotes) {
161 const struct ovsdb_jsonrpc_options *options = node->data;
162 struct ovsdb_jsonrpc_remote *remote;
164 remote = shash_find_data(&svr->remotes, node->name);
166 remote = ovsdb_jsonrpc_server_add_remote(svr, node->name, options);
172 ovsdb_jsonrpc_session_set_all_options(remote, options);
176 static struct ovsdb_jsonrpc_remote *
177 ovsdb_jsonrpc_server_add_remote(struct ovsdb_jsonrpc_server *svr,
179 const struct ovsdb_jsonrpc_options *options)
181 struct ovsdb_jsonrpc_remote *remote;
182 struct pstream *listener;
185 error = jsonrpc_pstream_open(name, &listener, options->dscp);
186 if (error && error != EAFNOSUPPORT) {
187 VLOG_ERR_RL(&rl, "%s: listen failed: %s", name, strerror(error));
191 remote = xmalloc(sizeof *remote);
192 remote->server = svr;
193 remote->listener = listener;
194 list_init(&remote->sessions);
195 shash_add(&svr->remotes, name, remote);
198 ovsdb_jsonrpc_session_create(remote, jsonrpc_session_open(name));
204 ovsdb_jsonrpc_server_del_remote(struct shash_node *node)
206 struct ovsdb_jsonrpc_remote *remote = node->data;
208 ovsdb_jsonrpc_session_close_all(remote);
209 pstream_close(remote->listener);
210 shash_delete(&remote->server->remotes, node);
214 /* Stores status information for the remote named 'target', which should have
215 * been configured on 'svr' with a call to ovsdb_jsonrpc_server_set_remotes(),
216 * into '*status'. On success returns true, on failure (if 'svr' doesn't have
217 * a remote named 'target' or if that remote is an inbound remote that has no
218 * active connections) returns false. On failure, 'status' will be zeroed.
221 ovsdb_jsonrpc_server_get_remote_status(
222 const struct ovsdb_jsonrpc_server *svr, const char *target,
223 struct ovsdb_jsonrpc_remote_status *status)
225 const struct ovsdb_jsonrpc_remote *remote;
227 memset(status, 0, sizeof *status);
229 remote = shash_find_data(&svr->remotes, target);
230 return remote && ovsdb_jsonrpc_session_get_status(remote, status);
234 ovsdb_jsonrpc_server_free_remote_status(
235 struct ovsdb_jsonrpc_remote_status *status)
237 free(status->locks_held);
238 free(status->locks_waiting);
239 free(status->locks_lost);
242 /* Forces all of the JSON-RPC sessions managed by 'svr' to disconnect and
245 ovsdb_jsonrpc_server_reconnect(struct ovsdb_jsonrpc_server *svr)
247 struct shash_node *node;
249 SHASH_FOR_EACH (node, &svr->remotes) {
250 struct ovsdb_jsonrpc_remote *remote = node->data;
252 ovsdb_jsonrpc_session_reconnect_all(remote);
257 ovsdb_jsonrpc_server_run(struct ovsdb_jsonrpc_server *svr)
259 struct shash_node *node;
261 SHASH_FOR_EACH (node, &svr->remotes) {
262 struct ovsdb_jsonrpc_remote *remote = node->data;
264 if (remote->listener && svr->n_sessions < svr->max_sessions) {
265 struct stream *stream;
268 error = pstream_accept(remote->listener, &stream);
270 struct jsonrpc_session *js;
271 js = jsonrpc_session_open_unreliably(jsonrpc_open(stream));
272 ovsdb_jsonrpc_session_create(remote, js);
273 } else if (error != EAGAIN) {
274 VLOG_WARN_RL(&rl, "%s: accept failed: %s",
275 pstream_get_name(remote->listener),
280 ovsdb_jsonrpc_session_run_all(remote);
285 ovsdb_jsonrpc_server_wait(struct ovsdb_jsonrpc_server *svr)
287 struct shash_node *node;
289 SHASH_FOR_EACH (node, &svr->remotes) {
290 struct ovsdb_jsonrpc_remote *remote = node->data;
292 if (remote->listener && svr->n_sessions < svr->max_sessions) {
293 pstream_wait(remote->listener);
296 ovsdb_jsonrpc_session_wait_all(remote);
300 /* Adds some memory usage statistics for 'svr' into 'usage', for use with
301 * memory_report(). */
303 ovsdb_jsonrpc_server_get_memory_usage(const struct ovsdb_jsonrpc_server *svr,
306 struct shash_node *node;
308 simap_increase(usage, "sessions", svr->n_sessions);
309 SHASH_FOR_EACH (node, &svr->remotes) {
310 struct ovsdb_jsonrpc_remote *remote = node->data;
312 ovsdb_jsonrpc_session_get_memory_usage_all(remote, usage);
316 /* JSON-RPC database server session. */
318 struct ovsdb_jsonrpc_session {
319 struct list node; /* Element in remote's sessions list. */
320 struct ovsdb_session up;
321 struct ovsdb_jsonrpc_remote *remote;
324 struct hmap triggers; /* Hmap of "struct ovsdb_jsonrpc_trigger"s. */
327 struct hmap monitors; /* Hmap of "struct ovsdb_jsonrpc_monitor"s. */
329 /* Network connectivity. */
330 struct jsonrpc_session *js; /* JSON-RPC session. */
331 unsigned int js_seqno; /* Last jsonrpc_session_get_seqno() value. */
334 static void ovsdb_jsonrpc_session_close(struct ovsdb_jsonrpc_session *);
335 static int ovsdb_jsonrpc_session_run(struct ovsdb_jsonrpc_session *);
336 static void ovsdb_jsonrpc_session_wait(struct ovsdb_jsonrpc_session *);
337 static void ovsdb_jsonrpc_session_get_memory_usage(
338 const struct ovsdb_jsonrpc_session *, struct simap *usage);
339 static void ovsdb_jsonrpc_session_set_options(
340 struct ovsdb_jsonrpc_session *, const struct ovsdb_jsonrpc_options *);
341 static void ovsdb_jsonrpc_session_got_request(struct ovsdb_jsonrpc_session *,
342 struct jsonrpc_msg *);
343 static void ovsdb_jsonrpc_session_got_notify(struct ovsdb_jsonrpc_session *,
344 struct jsonrpc_msg *);
346 static struct ovsdb_jsonrpc_session *
347 ovsdb_jsonrpc_session_create(struct ovsdb_jsonrpc_remote *remote,
348 struct jsonrpc_session *js)
350 struct ovsdb_jsonrpc_session *s;
352 s = xzalloc(sizeof *s);
353 ovsdb_session_init(&s->up, remote->server->up.db);
355 list_push_back(&remote->sessions, &s->node);
356 hmap_init(&s->triggers);
357 hmap_init(&s->monitors);
359 s->js_seqno = jsonrpc_session_get_seqno(js);
361 remote->server->n_sessions++;
367 ovsdb_jsonrpc_session_close(struct ovsdb_jsonrpc_session *s)
369 ovsdb_jsonrpc_monitor_remove_all(s);
370 ovsdb_jsonrpc_session_unlock_all(s);
371 jsonrpc_session_close(s->js);
372 list_remove(&s->node);
373 ovsdb_session_destroy(&s->up);
374 s->remote->server->n_sessions--;
375 ovsdb_session_destroy(&s->up);
380 ovsdb_jsonrpc_session_run(struct ovsdb_jsonrpc_session *s)
382 jsonrpc_session_run(s->js);
383 if (s->js_seqno != jsonrpc_session_get_seqno(s->js)) {
384 s->js_seqno = jsonrpc_session_get_seqno(s->js);
385 ovsdb_jsonrpc_trigger_complete_all(s);
386 ovsdb_jsonrpc_monitor_remove_all(s);
387 ovsdb_jsonrpc_session_unlock_all(s);
390 ovsdb_jsonrpc_trigger_complete_done(s);
392 if (!jsonrpc_session_get_backlog(s->js)) {
393 struct jsonrpc_msg *msg = jsonrpc_session_recv(s->js);
395 if (msg->type == JSONRPC_REQUEST) {
396 ovsdb_jsonrpc_session_got_request(s, msg);
397 } else if (msg->type == JSONRPC_NOTIFY) {
398 ovsdb_jsonrpc_session_got_notify(s, msg);
400 VLOG_WARN("%s: received unexpected %s message",
401 jsonrpc_session_get_name(s->js),
402 jsonrpc_msg_type_to_string(msg->type));
403 jsonrpc_session_force_reconnect(s->js);
404 jsonrpc_msg_destroy(msg);
408 return jsonrpc_session_is_alive(s->js) ? 0 : ETIMEDOUT;
412 ovsdb_jsonrpc_session_set_options(struct ovsdb_jsonrpc_session *session,
413 const struct ovsdb_jsonrpc_options *options)
415 jsonrpc_session_set_max_backoff(session->js, options->max_backoff);
416 jsonrpc_session_set_probe_interval(session->js, options->probe_interval);
417 jsonrpc_session_set_dscp(session->js, options->dscp);
421 ovsdb_jsonrpc_session_run_all(struct ovsdb_jsonrpc_remote *remote)
423 struct ovsdb_jsonrpc_session *s, *next;
425 LIST_FOR_EACH_SAFE (s, next, node, &remote->sessions) {
426 int error = ovsdb_jsonrpc_session_run(s);
428 ovsdb_jsonrpc_session_close(s);
434 ovsdb_jsonrpc_session_wait(struct ovsdb_jsonrpc_session *s)
436 jsonrpc_session_wait(s->js);
437 if (!jsonrpc_session_get_backlog(s->js)) {
438 jsonrpc_session_recv_wait(s->js);
443 ovsdb_jsonrpc_session_wait_all(struct ovsdb_jsonrpc_remote *remote)
445 struct ovsdb_jsonrpc_session *s;
447 LIST_FOR_EACH (s, node, &remote->sessions) {
448 ovsdb_jsonrpc_session_wait(s);
453 ovsdb_jsonrpc_session_get_memory_usage(const struct ovsdb_jsonrpc_session *s,
456 simap_increase(usage, "triggers", hmap_count(&s->triggers));
457 simap_increase(usage, "monitors", hmap_count(&s->monitors));
458 simap_increase(usage, "backlog", jsonrpc_session_get_backlog(s->js));
462 ovsdb_jsonrpc_session_get_memory_usage_all(
463 const struct ovsdb_jsonrpc_remote *remote,
466 struct ovsdb_jsonrpc_session *s;
468 LIST_FOR_EACH (s, node, &remote->sessions) {
469 ovsdb_jsonrpc_session_get_memory_usage(s, usage);
474 ovsdb_jsonrpc_session_close_all(struct ovsdb_jsonrpc_remote *remote)
476 struct ovsdb_jsonrpc_session *s, *next;
478 LIST_FOR_EACH_SAFE (s, next, node, &remote->sessions) {
479 ovsdb_jsonrpc_session_close(s);
483 /* Forces all of the JSON-RPC sessions managed by 'remote' to disconnect and
486 ovsdb_jsonrpc_session_reconnect_all(struct ovsdb_jsonrpc_remote *remote)
488 struct ovsdb_jsonrpc_session *s, *next;
490 LIST_FOR_EACH_SAFE (s, next, node, &remote->sessions) {
491 jsonrpc_session_force_reconnect(s->js);
492 if (!jsonrpc_session_is_alive(s->js)) {
493 ovsdb_jsonrpc_session_close(s);
498 /* Sets the options for all of the JSON-RPC sessions managed by 'remote' to
501 ovsdb_jsonrpc_session_set_all_options(
502 struct ovsdb_jsonrpc_remote *remote,
503 const struct ovsdb_jsonrpc_options *options)
505 struct ovsdb_jsonrpc_session *s;
507 LIST_FOR_EACH (s, node, &remote->sessions) {
508 ovsdb_jsonrpc_session_set_options(s, options);
513 ovsdb_jsonrpc_session_get_status(const struct ovsdb_jsonrpc_remote *remote,
514 struct ovsdb_jsonrpc_remote_status *status)
516 const struct ovsdb_jsonrpc_session *s;
517 const struct jsonrpc_session *js;
518 struct ovsdb_lock_waiter *waiter;
519 struct reconnect_stats rstats;
520 struct ds locks_held, locks_waiting, locks_lost;
522 if (list_is_empty(&remote->sessions)) {
525 s = CONTAINER_OF(remote->sessions.next, struct ovsdb_jsonrpc_session, node);
528 status->is_connected = jsonrpc_session_is_connected(js);
529 status->last_error = jsonrpc_session_get_status(js);
531 jsonrpc_session_get_reconnect_stats(js, &rstats);
532 status->state = rstats.state;
533 status->sec_since_connect = rstats.msec_since_connect == UINT_MAX
534 ? UINT_MAX : rstats.msec_since_connect / 1000;
535 status->sec_since_disconnect = rstats.msec_since_disconnect == UINT_MAX
536 ? UINT_MAX : rstats.msec_since_disconnect / 1000;
538 ds_init(&locks_held);
539 ds_init(&locks_waiting);
540 ds_init(&locks_lost);
541 HMAP_FOR_EACH (waiter, session_node, &s->up.waiters) {
544 string = (ovsdb_lock_waiter_is_owner(waiter) ? &locks_held
545 : waiter->mode == OVSDB_LOCK_WAIT ? &locks_waiting
547 if (string->length) {
548 ds_put_char(string, ' ');
550 ds_put_cstr(string, waiter->lock_name);
552 status->locks_held = ds_steal_cstr(&locks_held);
553 status->locks_waiting = ds_steal_cstr(&locks_waiting);
554 status->locks_lost = ds_steal_cstr(&locks_lost);
556 status->n_connections = list_size(&remote->sessions);
562 get_db_name(const struct ovsdb_jsonrpc_session *s)
564 return s->remote->server->up.db->schema->name;
567 static struct jsonrpc_msg *
568 ovsdb_jsonrpc_check_db_name(const struct ovsdb_jsonrpc_session *s,
569 const struct jsonrpc_msg *request)
571 struct json_array *params;
572 const char *want_db_name;
573 const char *have_db_name;
574 struct ovsdb_error *error;
575 struct jsonrpc_msg *reply;
577 params = json_array(request->params);
578 if (!params->n || params->elems[0]->type != JSON_STRING) {
579 error = ovsdb_syntax_error(
580 request->params, NULL,
581 "%s request params must begin with <db-name>", request->method);
585 want_db_name = params->elems[0]->u.string;
586 have_db_name = get_db_name(s);
587 if (strcmp(want_db_name, have_db_name)) {
588 error = ovsdb_syntax_error(
589 request->params, "unknown database",
590 "%s request specifies unknown database %s",
591 request->method, want_db_name);
598 reply = jsonrpc_create_reply(ovsdb_error_to_json(error), request->id);
599 ovsdb_error_destroy(error);
603 static struct ovsdb_error *
604 ovsdb_jsonrpc_session_parse_lock_name(const struct jsonrpc_msg *request,
605 const char **lock_namep)
607 const struct json_array *params;
609 params = json_array(request->params);
610 if (params->n != 1 || params->elems[0]->type != JSON_STRING ||
611 !ovsdb_parser_is_id(json_string(params->elems[0]))) {
613 return ovsdb_syntax_error(request->params, NULL,
614 "%s request params must be <id>",
618 *lock_namep = json_string(params->elems[0]);
623 ovsdb_jsonrpc_session_notify(struct ovsdb_session *session,
624 const char *lock_name,
627 struct ovsdb_jsonrpc_session *s;
630 s = CONTAINER_OF(session, struct ovsdb_jsonrpc_session, up);
631 params = json_array_create_1(json_string_create(lock_name));
632 jsonrpc_session_send(s->js, jsonrpc_create_notify(method, params));
635 static struct jsonrpc_msg *
636 ovsdb_jsonrpc_session_lock(struct ovsdb_jsonrpc_session *s,
637 struct jsonrpc_msg *request,
638 enum ovsdb_lock_mode mode)
640 struct ovsdb_lock_waiter *waiter;
641 struct jsonrpc_msg *reply;
642 struct ovsdb_error *error;
643 struct ovsdb_session *victim;
644 const char *lock_name;
647 error = ovsdb_jsonrpc_session_parse_lock_name(request, &lock_name);
652 /* Report error if this session has issued a "lock" or "steal" without a
653 * matching "unlock" for this lock. */
654 waiter = ovsdb_session_get_lock_waiter(&s->up, lock_name);
656 error = ovsdb_syntax_error(
657 request->params, NULL,
658 "must issue \"unlock\" before new \"%s\"", request->method);
662 /* Get the lock, add us as a waiter. */
663 waiter = ovsdb_server_lock(&s->remote->server->up, &s->up, lock_name, mode,
666 ovsdb_jsonrpc_session_notify(victim, lock_name, "stolen");
669 result = json_object_create();
670 json_object_put(result, "locked",
671 json_boolean_create(ovsdb_lock_waiter_is_owner(waiter)));
673 return jsonrpc_create_reply(result, request->id);
676 reply = jsonrpc_create_reply(ovsdb_error_to_json(error), request->id);
677 ovsdb_error_destroy(error);
682 ovsdb_jsonrpc_session_unlock_all(struct ovsdb_jsonrpc_session *s)
684 struct ovsdb_lock_waiter *waiter, *next;
686 HMAP_FOR_EACH_SAFE (waiter, next, session_node, &s->up.waiters) {
687 ovsdb_jsonrpc_session_unlock__(waiter);
692 ovsdb_jsonrpc_session_unlock__(struct ovsdb_lock_waiter *waiter)
694 struct ovsdb_lock *lock = waiter->lock;
697 struct ovsdb_session *new_owner = ovsdb_lock_waiter_remove(waiter);
699 ovsdb_jsonrpc_session_notify(new_owner, lock->name, "locked");
701 /* ovsdb_server_lock() might have freed 'lock'. */
705 ovsdb_lock_waiter_destroy(waiter);
708 static struct jsonrpc_msg *
709 ovsdb_jsonrpc_session_unlock(struct ovsdb_jsonrpc_session *s,
710 struct jsonrpc_msg *request)
712 struct ovsdb_lock_waiter *waiter;
713 struct jsonrpc_msg *reply;
714 struct ovsdb_error *error;
715 const char *lock_name;
717 error = ovsdb_jsonrpc_session_parse_lock_name(request, &lock_name);
722 /* Report error if this session has not issued a "lock" or "steal" for this
724 waiter = ovsdb_session_get_lock_waiter(&s->up, lock_name);
726 error = ovsdb_syntax_error(
727 request->params, NULL, "\"unlock\" without \"lock\" or \"steal\"");
731 ovsdb_jsonrpc_session_unlock__(waiter);
733 return jsonrpc_create_reply(json_object_create(), request->id);
736 reply = jsonrpc_create_reply(ovsdb_error_to_json(error), request->id);
737 ovsdb_error_destroy(error);
741 static struct jsonrpc_msg *
742 execute_transaction(struct ovsdb_jsonrpc_session *s,
743 struct jsonrpc_msg *request)
745 ovsdb_jsonrpc_trigger_create(s, request->id, request->params);
747 request->params = NULL;
748 jsonrpc_msg_destroy(request);
753 ovsdb_jsonrpc_session_got_request(struct ovsdb_jsonrpc_session *s,
754 struct jsonrpc_msg *request)
756 struct jsonrpc_msg *reply;
758 if (!strcmp(request->method, "transact")) {
759 reply = ovsdb_jsonrpc_check_db_name(s, request);
761 reply = execute_transaction(s, request);
763 } else if (!strcmp(request->method, "monitor")) {
764 reply = ovsdb_jsonrpc_check_db_name(s, request);
766 reply = jsonrpc_create_reply(
767 ovsdb_jsonrpc_monitor_create(s, request->params), request->id);
769 } else if (!strcmp(request->method, "monitor_cancel")) {
770 reply = ovsdb_jsonrpc_monitor_cancel(s, json_array(request->params),
772 } else if (!strcmp(request->method, "get_schema")) {
773 reply = ovsdb_jsonrpc_check_db_name(s, request);
775 reply = jsonrpc_create_reply(
776 ovsdb_schema_to_json(s->remote->server->up.db->schema),
779 } else if (!strcmp(request->method, "list_dbs")) {
780 reply = jsonrpc_create_reply(
781 json_array_create_1(json_string_create(get_db_name(s))),
783 } else if (!strcmp(request->method, "lock")) {
784 reply = ovsdb_jsonrpc_session_lock(s, request, OVSDB_LOCK_WAIT);
785 } else if (!strcmp(request->method, "steal")) {
786 reply = ovsdb_jsonrpc_session_lock(s, request, OVSDB_LOCK_STEAL);
787 } else if (!strcmp(request->method, "unlock")) {
788 reply = ovsdb_jsonrpc_session_unlock(s, request);
789 } else if (!strcmp(request->method, "echo")) {
790 reply = jsonrpc_create_reply(json_clone(request->params), request->id);
792 reply = jsonrpc_create_error(json_string_create("unknown method"),
797 jsonrpc_msg_destroy(request);
798 jsonrpc_session_send(s->js, reply);
803 execute_cancel(struct ovsdb_jsonrpc_session *s, struct jsonrpc_msg *request)
805 if (json_array(request->params)->n == 1) {
806 struct ovsdb_jsonrpc_trigger *t;
809 id = request->params->u.array.elems[0];
810 t = ovsdb_jsonrpc_trigger_find(s, id, json_hash(id, 0));
812 ovsdb_jsonrpc_trigger_complete(t);
818 ovsdb_jsonrpc_session_got_notify(struct ovsdb_jsonrpc_session *s,
819 struct jsonrpc_msg *request)
821 if (!strcmp(request->method, "cancel")) {
822 execute_cancel(s, request);
824 jsonrpc_msg_destroy(request);
827 /* JSON-RPC database server triggers.
829 * (Every transaction is treated as a trigger even if it doesn't actually have
830 * any "wait" operations.) */
832 struct ovsdb_jsonrpc_trigger {
833 struct ovsdb_trigger trigger;
834 struct hmap_node hmap_node; /* In session's "triggers" hmap. */
839 ovsdb_jsonrpc_trigger_create(struct ovsdb_jsonrpc_session *s,
840 struct json *id, struct json *params)
842 struct ovsdb_jsonrpc_trigger *t;
845 /* Check for duplicate ID. */
846 hash = json_hash(id, 0);
847 t = ovsdb_jsonrpc_trigger_find(s, id, hash);
849 struct jsonrpc_msg *msg;
851 msg = jsonrpc_create_error(json_string_create("duplicate request ID"),
853 jsonrpc_session_send(s->js, msg);
855 json_destroy(params);
859 /* Insert into trigger table. */
860 t = xmalloc(sizeof *t);
861 ovsdb_trigger_init(&s->up, &t->trigger, params, time_msec());
863 hmap_insert(&s->triggers, &t->hmap_node, hash);
865 /* Complete early if possible. */
866 if (ovsdb_trigger_is_complete(&t->trigger)) {
867 ovsdb_jsonrpc_trigger_complete(t);
871 static struct ovsdb_jsonrpc_trigger *
872 ovsdb_jsonrpc_trigger_find(struct ovsdb_jsonrpc_session *s,
873 const struct json *id, size_t hash)
875 struct ovsdb_jsonrpc_trigger *t;
877 HMAP_FOR_EACH_WITH_HASH (t, hmap_node, hash, &s->triggers) {
878 if (json_equal(t->id, id)) {
887 ovsdb_jsonrpc_trigger_complete(struct ovsdb_jsonrpc_trigger *t)
889 struct ovsdb_jsonrpc_session *s;
891 s = CONTAINER_OF(t->trigger.session, struct ovsdb_jsonrpc_session, up);
893 if (jsonrpc_session_is_connected(s->js)) {
894 struct jsonrpc_msg *reply;
897 result = ovsdb_trigger_steal_result(&t->trigger);
899 reply = jsonrpc_create_reply(result, t->id);
901 reply = jsonrpc_create_error(json_string_create("canceled"),
904 jsonrpc_session_send(s->js, reply);
908 ovsdb_trigger_destroy(&t->trigger);
909 hmap_remove(&s->triggers, &t->hmap_node);
914 ovsdb_jsonrpc_trigger_complete_all(struct ovsdb_jsonrpc_session *s)
916 struct ovsdb_jsonrpc_trigger *t, *next;
917 HMAP_FOR_EACH_SAFE (t, next, hmap_node, &s->triggers) {
918 ovsdb_jsonrpc_trigger_complete(t);
923 ovsdb_jsonrpc_trigger_complete_done(struct ovsdb_jsonrpc_session *s)
925 while (!list_is_empty(&s->up.completions)) {
926 struct ovsdb_jsonrpc_trigger *t
927 = CONTAINER_OF(s->up.completions.next,
928 struct ovsdb_jsonrpc_trigger, trigger.node);
929 ovsdb_jsonrpc_trigger_complete(t);
933 /* JSON-RPC database table monitors. */
935 enum ovsdb_jsonrpc_monitor_selection {
936 OJMS_INITIAL = 1 << 0, /* All rows when monitor is created. */
937 OJMS_INSERT = 1 << 1, /* New rows. */
938 OJMS_DELETE = 1 << 2, /* Deleted rows. */
939 OJMS_MODIFY = 1 << 3 /* Modified rows. */
942 /* A particular column being monitored. */
943 struct ovsdb_jsonrpc_monitor_column {
944 const struct ovsdb_column *column;
945 enum ovsdb_jsonrpc_monitor_selection select;
948 /* A particular table being monitored. */
949 struct ovsdb_jsonrpc_monitor_table {
950 const struct ovsdb_table *table;
952 /* This is the union (bitwise-OR) of the 'select' values in all of the
953 * members of 'columns' below. */
954 enum ovsdb_jsonrpc_monitor_selection select;
956 /* Columns being monitored. */
957 struct ovsdb_jsonrpc_monitor_column *columns;
961 /* A collection of tables being monitored. */
962 struct ovsdb_jsonrpc_monitor {
963 struct ovsdb_replica replica;
964 struct ovsdb_jsonrpc_session *session;
965 struct hmap_node node; /* In ovsdb_jsonrpc_session's "monitors". */
967 struct json *monitor_id;
968 struct shash tables; /* Holds "struct ovsdb_jsonrpc_monitor_table"s. */
971 static const struct ovsdb_replica_class ovsdb_jsonrpc_replica_class;
973 struct ovsdb_jsonrpc_monitor *ovsdb_jsonrpc_monitor_find(
974 struct ovsdb_jsonrpc_session *, const struct json *monitor_id);
975 static void ovsdb_jsonrpc_monitor_destroy(struct ovsdb_replica *);
976 static struct json *ovsdb_jsonrpc_monitor_get_initial(
977 const struct ovsdb_jsonrpc_monitor *);
980 parse_bool(struct ovsdb_parser *parser, const char *name, bool default_value)
982 const struct json *json;
984 json = ovsdb_parser_member(parser, name, OP_BOOLEAN | OP_OPTIONAL);
985 return json ? json_boolean(json) : default_value;
988 struct ovsdb_jsonrpc_monitor *
989 ovsdb_jsonrpc_monitor_find(struct ovsdb_jsonrpc_session *s,
990 const struct json *monitor_id)
992 struct ovsdb_jsonrpc_monitor *m;
994 HMAP_FOR_EACH_WITH_HASH (m, node, json_hash(monitor_id, 0), &s->monitors) {
995 if (json_equal(m->monitor_id, monitor_id)) {
1004 ovsdb_jsonrpc_add_monitor_column(struct ovsdb_jsonrpc_monitor_table *mt,
1005 const struct ovsdb_column *column,
1006 enum ovsdb_jsonrpc_monitor_selection select,
1007 size_t *allocated_columns)
1009 struct ovsdb_jsonrpc_monitor_column *c;
1011 if (mt->n_columns >= *allocated_columns) {
1012 mt->columns = x2nrealloc(mt->columns, allocated_columns,
1013 sizeof *mt->columns);
1016 c = &mt->columns[mt->n_columns++];
1022 compare_ovsdb_jsonrpc_monitor_column(const void *a_, const void *b_)
1024 const struct ovsdb_jsonrpc_monitor_column *a = a_;
1025 const struct ovsdb_jsonrpc_monitor_column *b = b_;
1027 return a->column < b->column ? -1 : a->column > b->column;
1030 static struct ovsdb_error * WARN_UNUSED_RESULT
1031 ovsdb_jsonrpc_parse_monitor_request(struct ovsdb_jsonrpc_monitor_table *mt,
1032 const struct json *monitor_request,
1033 size_t *allocated_columns)
1035 const struct ovsdb_table_schema *ts = mt->table->schema;
1036 enum ovsdb_jsonrpc_monitor_selection select;
1037 const struct json *columns, *select_json;
1038 struct ovsdb_parser parser;
1039 struct ovsdb_error *error;
1041 ovsdb_parser_init(&parser, monitor_request, "table %s", ts->name);
1042 columns = ovsdb_parser_member(&parser, "columns", OP_ARRAY | OP_OPTIONAL);
1043 select_json = ovsdb_parser_member(&parser, "select",
1044 OP_OBJECT | OP_OPTIONAL);
1045 error = ovsdb_parser_finish(&parser);
1052 ovsdb_parser_init(&parser, select_json, "table %s select", ts->name);
1053 if (parse_bool(&parser, "initial", true)) {
1054 select |= OJMS_INITIAL;
1056 if (parse_bool(&parser, "insert", true)) {
1057 select |= OJMS_INSERT;
1059 if (parse_bool(&parser, "delete", true)) {
1060 select |= OJMS_DELETE;
1062 if (parse_bool(&parser, "modify", true)) {
1063 select |= OJMS_MODIFY;
1065 error = ovsdb_parser_finish(&parser);
1070 select = OJMS_INITIAL | OJMS_INSERT | OJMS_DELETE | OJMS_MODIFY;
1072 mt->select |= select;
1077 if (columns->type != JSON_ARRAY) {
1078 return ovsdb_syntax_error(columns, NULL,
1079 "array of column names expected");
1082 for (i = 0; i < columns->u.array.n; i++) {
1083 const struct ovsdb_column *column;
1086 if (columns->u.array.elems[i]->type != JSON_STRING) {
1087 return ovsdb_syntax_error(columns, NULL,
1088 "array of column names expected");
1091 s = columns->u.array.elems[i]->u.string;
1092 column = shash_find_data(&mt->table->schema->columns, s);
1094 return ovsdb_syntax_error(columns, NULL, "%s is not a valid "
1097 ovsdb_jsonrpc_add_monitor_column(mt, column, select,
1101 struct shash_node *node;
1103 SHASH_FOR_EACH (node, &ts->columns) {
1104 const struct ovsdb_column *column = node->data;
1105 if (column->index != OVSDB_COL_UUID) {
1106 ovsdb_jsonrpc_add_monitor_column(mt, column, select,
1115 static struct json *
1116 ovsdb_jsonrpc_monitor_create(struct ovsdb_jsonrpc_session *s,
1117 struct json *params)
1119 struct ovsdb_jsonrpc_monitor *m = NULL;
1120 struct json *monitor_id, *monitor_requests;
1121 struct ovsdb_error *error = NULL;
1122 struct shash_node *node;
1125 if (json_array(params)->n != 3) {
1126 error = ovsdb_syntax_error(params, NULL, "invalid parameters");
1129 monitor_id = params->u.array.elems[1];
1130 monitor_requests = params->u.array.elems[2];
1131 if (monitor_requests->type != JSON_OBJECT) {
1132 error = ovsdb_syntax_error(monitor_requests, NULL,
1133 "monitor-requests must be object");
1137 if (ovsdb_jsonrpc_monitor_find(s, monitor_id)) {
1138 error = ovsdb_syntax_error(monitor_id, NULL, "duplicate monitor ID");
1142 m = xzalloc(sizeof *m);
1143 ovsdb_replica_init(&m->replica, &ovsdb_jsonrpc_replica_class);
1144 ovsdb_add_replica(s->remote->server->up.db, &m->replica);
1146 hmap_insert(&s->monitors, &m->node, json_hash(monitor_id, 0));
1147 m->monitor_id = json_clone(monitor_id);
1148 shash_init(&m->tables);
1150 SHASH_FOR_EACH (node, json_object(monitor_requests)) {
1151 const struct ovsdb_table *table;
1152 struct ovsdb_jsonrpc_monitor_table *mt;
1153 size_t allocated_columns;
1154 const struct json *mr_value;
1157 table = ovsdb_get_table(s->remote->server->up.db, node->name);
1159 error = ovsdb_syntax_error(NULL, NULL,
1160 "no table named %s", node->name);
1164 mt = xzalloc(sizeof *mt);
1166 shash_add(&m->tables, table->schema->name, mt);
1168 /* Parse columns. */
1169 mr_value = node->data;
1170 allocated_columns = 0;
1171 if (mr_value->type == JSON_ARRAY) {
1172 const struct json_array *array = &mr_value->u.array;
1174 for (i = 0; i < array->n; i++) {
1175 error = ovsdb_jsonrpc_parse_monitor_request(
1176 mt, array->elems[i], &allocated_columns);
1182 error = ovsdb_jsonrpc_parse_monitor_request(
1183 mt, mr_value, &allocated_columns);
1189 /* Check for duplicate columns. */
1190 qsort(mt->columns, mt->n_columns, sizeof *mt->columns,
1191 compare_ovsdb_jsonrpc_monitor_column);
1192 for (i = 1; i < mt->n_columns; i++) {
1193 if (mt->columns[i].column == mt->columns[i - 1].column) {
1194 error = ovsdb_syntax_error(mr_value, NULL, "column %s "
1195 "mentioned more than once",
1196 mt->columns[i].column->name);
1202 return ovsdb_jsonrpc_monitor_get_initial(m);
1206 ovsdb_remove_replica(s->remote->server->up.db, &m->replica);
1209 json = ovsdb_error_to_json(error);
1210 ovsdb_error_destroy(error);
1214 static struct jsonrpc_msg *
1215 ovsdb_jsonrpc_monitor_cancel(struct ovsdb_jsonrpc_session *s,
1216 struct json_array *params,
1217 const struct json *request_id)
1219 if (params->n != 1) {
1220 return jsonrpc_create_error(json_string_create("invalid parameters"),
1223 struct ovsdb_jsonrpc_monitor *m;
1225 m = ovsdb_jsonrpc_monitor_find(s, params->elems[0]);
1227 return jsonrpc_create_error(json_string_create("unknown monitor"),
1230 ovsdb_remove_replica(s->remote->server->up.db, &m->replica);
1231 return jsonrpc_create_reply(json_object_create(), request_id);
1237 ovsdb_jsonrpc_monitor_remove_all(struct ovsdb_jsonrpc_session *s)
1239 struct ovsdb_jsonrpc_monitor *m, *next;
1241 HMAP_FOR_EACH_SAFE (m, next, node, &s->monitors) {
1242 ovsdb_remove_replica(s->remote->server->up.db, &m->replica);
1246 static struct ovsdb_jsonrpc_monitor *
1247 ovsdb_jsonrpc_monitor_cast(struct ovsdb_replica *replica)
1249 assert(replica->class == &ovsdb_jsonrpc_replica_class);
1250 return CONTAINER_OF(replica, struct ovsdb_jsonrpc_monitor, replica);
1253 struct ovsdb_jsonrpc_monitor_aux {
1254 bool initial; /* Sending initial contents of table? */
1255 const struct ovsdb_jsonrpc_monitor *monitor;
1256 struct json *json; /* JSON for the whole transaction. */
1258 /* Current table. */
1259 struct ovsdb_jsonrpc_monitor_table *mt;
1260 struct json *table_json; /* JSON for table's transaction. */
1264 any_reportable_change(const struct ovsdb_jsonrpc_monitor_table *mt,
1265 const unsigned long int *changed)
1269 for (i = 0; i < mt->n_columns; i++) {
1270 const struct ovsdb_jsonrpc_monitor_column *c = &mt->columns[i];
1271 unsigned int idx = c->column->index;
1273 if (c->select & OJMS_MODIFY && bitmap_is_set(changed, idx)) {
1282 ovsdb_jsonrpc_monitor_change_cb(const struct ovsdb_row *old,
1283 const struct ovsdb_row *new,
1284 const unsigned long int *changed,
1287 struct ovsdb_jsonrpc_monitor_aux *aux = aux_;
1288 const struct ovsdb_jsonrpc_monitor *m = aux->monitor;
1289 struct ovsdb_table *table = new ? new->table : old->table;
1290 enum ovsdb_jsonrpc_monitor_selection type;
1291 struct json *old_json, *new_json;
1292 struct json *row_json;
1293 char uuid[UUID_LEN + 1];
1296 if (!aux->mt || table != aux->mt->table) {
1297 aux->mt = shash_find_data(&m->tables, table->schema->name);
1298 aux->table_json = NULL;
1300 /* We don't care about rows in this table at all. Tell the caller
1306 type = (aux->initial ? OJMS_INITIAL
1307 : !old ? OJMS_INSERT
1308 : !new ? OJMS_DELETE
1310 if (!(aux->mt->select & type)) {
1311 /* We don't care about this type of change (but do want to be called
1312 * back for changes to other rows in the same table). */
1316 if (type == OJMS_MODIFY && !any_reportable_change(aux->mt, changed)) {
1317 /* Nothing of interest changed. */
1321 old_json = new_json = NULL;
1322 if (type & (OJMS_DELETE | OJMS_MODIFY)) {
1323 old_json = json_object_create();
1325 if (type & (OJMS_INITIAL | OJMS_INSERT | OJMS_MODIFY)) {
1326 new_json = json_object_create();
1328 for (i = 0; i < aux->mt->n_columns; i++) {
1329 const struct ovsdb_jsonrpc_monitor_column *c = &aux->mt->columns[i];
1330 const struct ovsdb_column *column = c->column;
1331 unsigned int idx = c->column->index;
1333 if (!(type & c->select)) {
1334 /* We don't care about this type of change for this particular
1335 * column (but we will care about it for some other column). */
1339 if ((type == OJMS_MODIFY && bitmap_is_set(changed, idx))
1340 || type == OJMS_DELETE) {
1341 json_object_put(old_json, column->name,
1342 ovsdb_datum_to_json(&old->fields[idx],
1345 if (type & (OJMS_INITIAL | OJMS_INSERT | OJMS_MODIFY)) {
1346 json_object_put(new_json, column->name,
1347 ovsdb_datum_to_json(&new->fields[idx],
1352 /* Create JSON object for transaction overall. */
1354 aux->json = json_object_create();
1357 /* Create JSON object for transaction on this table. */
1358 if (!aux->table_json) {
1359 aux->table_json = json_object_create();
1360 json_object_put(aux->json, aux->mt->table->schema->name,
1364 /* Create JSON object for transaction on this row. */
1365 row_json = json_object_create();
1367 json_object_put(row_json, "old", old_json);
1370 json_object_put(row_json, "new", new_json);
1373 /* Add JSON row to JSON table. */
1374 snprintf(uuid, sizeof uuid,
1375 UUID_FMT, UUID_ARGS(ovsdb_row_get_uuid(new ? new : old)));
1376 json_object_put(aux->table_json, uuid, row_json);
1382 ovsdb_jsonrpc_monitor_init_aux(struct ovsdb_jsonrpc_monitor_aux *aux,
1383 const struct ovsdb_jsonrpc_monitor *m,
1386 aux->initial = initial;
1390 aux->table_json = NULL;
1393 static struct ovsdb_error *
1394 ovsdb_jsonrpc_monitor_commit(struct ovsdb_replica *replica,
1395 const struct ovsdb_txn *txn,
1396 bool durable OVS_UNUSED)
1398 struct ovsdb_jsonrpc_monitor *m = ovsdb_jsonrpc_monitor_cast(replica);
1399 struct ovsdb_jsonrpc_monitor_aux aux;
1401 ovsdb_jsonrpc_monitor_init_aux(&aux, m, false);
1402 ovsdb_txn_for_each_change(txn, ovsdb_jsonrpc_monitor_change_cb, &aux);
1404 struct jsonrpc_msg *msg;
1405 struct json *params;
1407 params = json_array_create_2(json_clone(aux.monitor->monitor_id),
1409 msg = jsonrpc_create_notify("update", params);
1410 jsonrpc_session_send(aux.monitor->session->js, msg);
1416 static struct json *
1417 ovsdb_jsonrpc_monitor_get_initial(const struct ovsdb_jsonrpc_monitor *m)
1419 struct ovsdb_jsonrpc_monitor_aux aux;
1420 struct shash_node *node;
1422 ovsdb_jsonrpc_monitor_init_aux(&aux, m, true);
1423 SHASH_FOR_EACH (node, &m->tables) {
1424 struct ovsdb_jsonrpc_monitor_table *mt = node->data;
1426 if (mt->select & OJMS_INITIAL) {
1427 struct ovsdb_row *row;
1429 HMAP_FOR_EACH (row, hmap_node, &mt->table->rows) {
1430 ovsdb_jsonrpc_monitor_change_cb(NULL, row, NULL, &aux);
1434 return aux.json ? aux.json : json_object_create();
1438 ovsdb_jsonrpc_monitor_destroy(struct ovsdb_replica *replica)
1440 struct ovsdb_jsonrpc_monitor *m = ovsdb_jsonrpc_monitor_cast(replica);
1441 struct shash_node *node;
1443 json_destroy(m->monitor_id);
1444 SHASH_FOR_EACH (node, &m->tables) {
1445 struct ovsdb_jsonrpc_monitor_table *mt = node->data;
1449 shash_destroy(&m->tables);
1450 hmap_remove(&m->session->monitors, &m->node);
1454 static const struct ovsdb_replica_class ovsdb_jsonrpc_replica_class = {
1455 ovsdb_jsonrpc_monitor_commit,
1456 ovsdb_jsonrpc_monitor_destroy