1 /* Copyright (c) 2009, 2010, 2011 Nicira Networks
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"
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_close_all(struct ovsdb_jsonrpc_remote *);
55 static void ovsdb_jsonrpc_session_reconnect_all(struct ovsdb_jsonrpc_remote *);
56 static void ovsdb_jsonrpc_session_set_all_options(
57 struct ovsdb_jsonrpc_remote *, const struct ovsdb_jsonrpc_options *);
58 static bool ovsdb_jsonrpc_session_get_status(
59 const struct ovsdb_jsonrpc_remote *,
60 struct ovsdb_jsonrpc_remote_status *);
61 static void ovsdb_jsonrpc_session_unlock_all(struct ovsdb_jsonrpc_session *);
62 static void ovsdb_jsonrpc_session_unlock__(struct ovsdb_lock_waiter *);
65 static void ovsdb_jsonrpc_trigger_create(struct ovsdb_jsonrpc_session *,
66 struct json *id, struct json *params);
67 static struct ovsdb_jsonrpc_trigger *ovsdb_jsonrpc_trigger_find(
68 struct ovsdb_jsonrpc_session *, const struct json *id, size_t hash);
69 static void ovsdb_jsonrpc_trigger_complete(struct ovsdb_jsonrpc_trigger *);
70 static void ovsdb_jsonrpc_trigger_complete_all(struct ovsdb_jsonrpc_session *);
71 static void ovsdb_jsonrpc_trigger_complete_done(
72 struct ovsdb_jsonrpc_session *);
75 static struct json *ovsdb_jsonrpc_monitor_create(
76 struct ovsdb_jsonrpc_session *, struct json *params);
77 static struct jsonrpc_msg *ovsdb_jsonrpc_monitor_cancel(
78 struct ovsdb_jsonrpc_session *,
79 struct json_array *params,
80 const struct json *request_id);
81 static void ovsdb_jsonrpc_monitor_remove_all(struct ovsdb_jsonrpc_session *);
83 /* JSON-RPC database server. */
85 struct ovsdb_jsonrpc_server {
86 struct ovsdb_server up;
87 unsigned int n_sessions, max_sessions;
88 struct shash remotes; /* Contains "struct ovsdb_jsonrpc_remote *"s. */
91 /* A configured remote. This is either a passive stream listener plus a list
92 * of the currently connected sessions, or a list of exactly one active
94 struct ovsdb_jsonrpc_remote {
95 struct ovsdb_jsonrpc_server *server;
96 struct pstream *listener; /* Listener, if passive. */
97 struct list sessions; /* List of "struct ovsdb_jsonrpc_session"s. */
100 static struct ovsdb_jsonrpc_remote *ovsdb_jsonrpc_server_add_remote(
101 struct ovsdb_jsonrpc_server *, const char *name);
102 static void ovsdb_jsonrpc_server_del_remote(struct shash_node *);
104 struct ovsdb_jsonrpc_server *
105 ovsdb_jsonrpc_server_create(struct ovsdb *db)
107 struct ovsdb_jsonrpc_server *server = xzalloc(sizeof *server);
108 ovsdb_server_init(&server->up, db);
109 server->max_sessions = 64;
110 shash_init(&server->remotes);
115 ovsdb_jsonrpc_server_destroy(struct ovsdb_jsonrpc_server *svr)
117 struct shash_node *node, *next;
119 SHASH_FOR_EACH_SAFE (node, next, &svr->remotes) {
120 ovsdb_jsonrpc_server_del_remote(node);
122 shash_destroy(&svr->remotes);
123 ovsdb_server_destroy(&svr->up);
127 struct ovsdb_jsonrpc_options *
128 ovsdb_jsonrpc_default_options(const char *target)
130 struct ovsdb_jsonrpc_options *options = xzalloc(sizeof *options);
131 options->max_backoff = RECONNECT_DEFAULT_MAX_BACKOFF;
132 options->probe_interval = (stream_or_pstream_needs_probes(target)
133 ? RECONNECT_DEFAULT_PROBE_INTERVAL
138 /* Sets 'svr''s current set of remotes to the names in 'new_remotes', with
139 * options in the struct ovsdb_jsonrpc_options supplied as the data values.
141 * A remote is an active or passive stream connection method, e.g. "pssl:" or
144 ovsdb_jsonrpc_server_set_remotes(struct ovsdb_jsonrpc_server *svr,
145 const struct shash *new_remotes)
147 struct shash_node *node, *next;
149 SHASH_FOR_EACH_SAFE (node, next, &svr->remotes) {
150 if (!shash_find(new_remotes, node->name)) {
151 VLOG_INFO("%s: remote deconfigured", node->name);
152 ovsdb_jsonrpc_server_del_remote(node);
155 SHASH_FOR_EACH (node, new_remotes) {
156 const struct ovsdb_jsonrpc_options *options = node->data;
157 struct ovsdb_jsonrpc_remote *remote;
159 remote = shash_find_data(&svr->remotes, node->name);
161 remote = ovsdb_jsonrpc_server_add_remote(svr, node->name);
167 ovsdb_jsonrpc_session_set_all_options(remote, options);
171 static struct ovsdb_jsonrpc_remote *
172 ovsdb_jsonrpc_server_add_remote(struct ovsdb_jsonrpc_server *svr,
175 struct ovsdb_jsonrpc_remote *remote;
176 struct pstream *listener;
179 error = jsonrpc_pstream_open(name, &listener);
180 if (error && error != EAFNOSUPPORT) {
181 VLOG_ERR_RL(&rl, "%s: listen failed: %s", name, strerror(error));
185 remote = xmalloc(sizeof *remote);
186 remote->server = svr;
187 remote->listener = listener;
188 list_init(&remote->sessions);
189 shash_add(&svr->remotes, name, remote);
192 ovsdb_jsonrpc_session_create(remote, jsonrpc_session_open(name));
198 ovsdb_jsonrpc_server_del_remote(struct shash_node *node)
200 struct ovsdb_jsonrpc_remote *remote = node->data;
202 ovsdb_jsonrpc_session_close_all(remote);
203 pstream_close(remote->listener);
204 shash_delete(&remote->server->remotes, node);
208 /* Stores status information for the remote named 'target', which should have
209 * been configured on 'svr' with a call to ovsdb_jsonrpc_server_set_remotes(),
210 * into '*status'. On success returns true, on failure (if 'svr' doesn't have
211 * a remote named 'target' or if that remote is an inbound remote that has no
212 * active connections) returns false. On failure, 'status' will be zeroed.
215 ovsdb_jsonrpc_server_get_remote_status(
216 const struct ovsdb_jsonrpc_server *svr, const char *target,
217 struct ovsdb_jsonrpc_remote_status *status)
219 const struct ovsdb_jsonrpc_remote *remote;
221 memset(status, 0, sizeof *status);
223 remote = shash_find_data(&svr->remotes, target);
224 return remote && ovsdb_jsonrpc_session_get_status(remote, status);
228 ovsdb_jsonrpc_server_free_remote_status(
229 struct ovsdb_jsonrpc_remote_status *status)
231 free(status->locks_held);
232 free(status->locks_waiting);
233 free(status->locks_lost);
236 /* Forces all of the JSON-RPC sessions managed by 'svr' to disconnect and
239 ovsdb_jsonrpc_server_reconnect(struct ovsdb_jsonrpc_server *svr)
241 struct shash_node *node;
243 SHASH_FOR_EACH (node, &svr->remotes) {
244 struct ovsdb_jsonrpc_remote *remote = node->data;
246 ovsdb_jsonrpc_session_reconnect_all(remote);
251 ovsdb_jsonrpc_server_run(struct ovsdb_jsonrpc_server *svr)
253 struct shash_node *node;
255 SHASH_FOR_EACH (node, &svr->remotes) {
256 struct ovsdb_jsonrpc_remote *remote = node->data;
258 if (remote->listener && svr->n_sessions < svr->max_sessions) {
259 struct stream *stream;
262 error = pstream_accept(remote->listener, &stream);
264 struct jsonrpc_session *js;
265 js = jsonrpc_session_open_unreliably(jsonrpc_open(stream));
266 ovsdb_jsonrpc_session_create(remote, js);
267 } else if (error != EAGAIN) {
268 VLOG_WARN_RL(&rl, "%s: accept failed: %s",
269 pstream_get_name(remote->listener),
274 ovsdb_jsonrpc_session_run_all(remote);
279 ovsdb_jsonrpc_server_wait(struct ovsdb_jsonrpc_server *svr)
281 struct shash_node *node;
283 SHASH_FOR_EACH (node, &svr->remotes) {
284 struct ovsdb_jsonrpc_remote *remote = node->data;
286 if (remote->listener && svr->n_sessions < svr->max_sessions) {
287 pstream_wait(remote->listener);
290 ovsdb_jsonrpc_session_wait_all(remote);
294 /* JSON-RPC database server session. */
296 struct ovsdb_jsonrpc_session {
297 struct ovsdb_session up;
298 struct ovsdb_jsonrpc_remote *remote;
299 struct list node; /* Element in remote's sessions list. */
302 struct hmap triggers; /* Hmap of "struct ovsdb_jsonrpc_trigger"s. */
305 struct hmap monitors; /* Hmap of "struct ovsdb_jsonrpc_monitor"s. */
307 /* Network connectivity. */
308 struct jsonrpc_session *js; /* JSON-RPC session. */
309 unsigned int js_seqno; /* Last jsonrpc_session_get_seqno() value. */
312 static void ovsdb_jsonrpc_session_close(struct ovsdb_jsonrpc_session *);
313 static int ovsdb_jsonrpc_session_run(struct ovsdb_jsonrpc_session *);
314 static void ovsdb_jsonrpc_session_wait(struct ovsdb_jsonrpc_session *);
315 static void ovsdb_jsonrpc_session_set_options(
316 struct ovsdb_jsonrpc_session *, const struct ovsdb_jsonrpc_options *);
317 static void ovsdb_jsonrpc_session_got_request(struct ovsdb_jsonrpc_session *,
318 struct jsonrpc_msg *);
319 static void ovsdb_jsonrpc_session_got_notify(struct ovsdb_jsonrpc_session *,
320 struct jsonrpc_msg *);
322 static struct ovsdb_jsonrpc_session *
323 ovsdb_jsonrpc_session_create(struct ovsdb_jsonrpc_remote *remote,
324 struct jsonrpc_session *js)
326 struct ovsdb_jsonrpc_session *s;
328 s = xzalloc(sizeof *s);
329 ovsdb_session_init(&s->up, remote->server->up.db);
331 list_push_back(&remote->sessions, &s->node);
332 hmap_init(&s->triggers);
333 hmap_init(&s->monitors);
335 s->js_seqno = jsonrpc_session_get_seqno(js);
337 remote->server->n_sessions++;
343 ovsdb_jsonrpc_session_close(struct ovsdb_jsonrpc_session *s)
345 ovsdb_jsonrpc_monitor_remove_all(s);
346 ovsdb_jsonrpc_session_unlock_all(s);
347 jsonrpc_session_close(s->js);
348 list_remove(&s->node);
349 ovsdb_session_destroy(&s->up);
350 s->remote->server->n_sessions--;
351 ovsdb_session_destroy(&s->up);
356 ovsdb_jsonrpc_session_run(struct ovsdb_jsonrpc_session *s)
358 jsonrpc_session_run(s->js);
359 if (s->js_seqno != jsonrpc_session_get_seqno(s->js)) {
360 s->js_seqno = jsonrpc_session_get_seqno(s->js);
361 ovsdb_jsonrpc_trigger_complete_all(s);
362 ovsdb_jsonrpc_monitor_remove_all(s);
363 ovsdb_jsonrpc_session_unlock_all(s);
366 ovsdb_jsonrpc_trigger_complete_done(s);
368 if (!jsonrpc_session_get_backlog(s->js)) {
369 struct jsonrpc_msg *msg = jsonrpc_session_recv(s->js);
371 if (msg->type == JSONRPC_REQUEST) {
372 ovsdb_jsonrpc_session_got_request(s, msg);
373 } else if (msg->type == JSONRPC_NOTIFY) {
374 ovsdb_jsonrpc_session_got_notify(s, msg);
376 VLOG_WARN("%s: received unexpected %s message",
377 jsonrpc_session_get_name(s->js),
378 jsonrpc_msg_type_to_string(msg->type));
379 jsonrpc_session_force_reconnect(s->js);
380 jsonrpc_msg_destroy(msg);
384 return jsonrpc_session_is_alive(s->js) ? 0 : ETIMEDOUT;
388 ovsdb_jsonrpc_session_set_options(struct ovsdb_jsonrpc_session *session,
389 const struct ovsdb_jsonrpc_options *options)
391 jsonrpc_session_set_max_backoff(session->js, options->max_backoff);
392 jsonrpc_session_set_probe_interval(session->js, options->probe_interval);
396 ovsdb_jsonrpc_session_run_all(struct ovsdb_jsonrpc_remote *remote)
398 struct ovsdb_jsonrpc_session *s, *next;
400 LIST_FOR_EACH_SAFE (s, next, node, &remote->sessions) {
401 int error = ovsdb_jsonrpc_session_run(s);
403 ovsdb_jsonrpc_session_close(s);
409 ovsdb_jsonrpc_session_wait(struct ovsdb_jsonrpc_session *s)
411 jsonrpc_session_wait(s->js);
412 if (!jsonrpc_session_get_backlog(s->js)) {
413 jsonrpc_session_recv_wait(s->js);
418 ovsdb_jsonrpc_session_wait_all(struct ovsdb_jsonrpc_remote *remote)
420 struct ovsdb_jsonrpc_session *s;
422 LIST_FOR_EACH (s, node, &remote->sessions) {
423 ovsdb_jsonrpc_session_wait(s);
428 ovsdb_jsonrpc_session_close_all(struct ovsdb_jsonrpc_remote *remote)
430 struct ovsdb_jsonrpc_session *s, *next;
432 LIST_FOR_EACH_SAFE (s, next, node, &remote->sessions) {
433 ovsdb_jsonrpc_session_close(s);
437 /* Forces all of the JSON-RPC sessions managed by 'remote' to disconnect and
440 ovsdb_jsonrpc_session_reconnect_all(struct ovsdb_jsonrpc_remote *remote)
442 struct ovsdb_jsonrpc_session *s, *next;
444 LIST_FOR_EACH_SAFE (s, next, node, &remote->sessions) {
445 jsonrpc_session_force_reconnect(s->js);
446 if (!jsonrpc_session_is_alive(s->js)) {
447 ovsdb_jsonrpc_session_close(s);
452 /* Sets the options for all of the JSON-RPC sessions managed by 'remote' to
455 ovsdb_jsonrpc_session_set_all_options(
456 struct ovsdb_jsonrpc_remote *remote,
457 const struct ovsdb_jsonrpc_options *options)
459 struct ovsdb_jsonrpc_session *s;
461 LIST_FOR_EACH (s, node, &remote->sessions) {
462 ovsdb_jsonrpc_session_set_options(s, options);
467 ovsdb_jsonrpc_session_get_status(const struct ovsdb_jsonrpc_remote *remote,
468 struct ovsdb_jsonrpc_remote_status *status)
470 const struct ovsdb_jsonrpc_session *s;
471 const struct jsonrpc_session *js;
472 struct ovsdb_lock_waiter *waiter;
473 struct reconnect_stats rstats;
474 struct ds locks_held, locks_waiting, locks_lost;
476 if (list_is_empty(&remote->sessions)) {
479 s = CONTAINER_OF(remote->sessions.next, struct ovsdb_jsonrpc_session, node);
482 status->is_connected = jsonrpc_session_is_connected(js);
483 status->last_error = jsonrpc_session_get_status(js);
485 jsonrpc_session_get_reconnect_stats(js, &rstats);
486 status->state = rstats.state;
487 status->sec_since_connect = rstats.msec_since_connect == UINT_MAX
488 ? UINT_MAX : rstats.msec_since_connect / 1000;
489 status->sec_since_disconnect = rstats.msec_since_disconnect == UINT_MAX
490 ? UINT_MAX : rstats.msec_since_disconnect / 1000;
492 ds_init(&locks_held);
493 ds_init(&locks_waiting);
494 ds_init(&locks_lost);
495 HMAP_FOR_EACH (waiter, session_node, &s->up.waiters) {
498 string = (ovsdb_lock_waiter_is_owner(waiter) ? &locks_held
499 : waiter->mode == OVSDB_LOCK_WAIT ? &locks_waiting
501 if (string->length) {
502 ds_put_char(string, ' ');
504 ds_put_cstr(string, waiter->lock_name);
506 status->locks_held = ds_steal_cstr(&locks_held);
507 status->locks_waiting = ds_steal_cstr(&locks_waiting);
508 status->locks_lost = ds_steal_cstr(&locks_lost);
510 status->n_connections = list_size(&remote->sessions);
516 get_db_name(const struct ovsdb_jsonrpc_session *s)
518 return s->remote->server->up.db->schema->name;
521 static struct jsonrpc_msg *
522 ovsdb_jsonrpc_check_db_name(const struct ovsdb_jsonrpc_session *s,
523 const struct jsonrpc_msg *request)
525 struct json_array *params;
526 const char *want_db_name;
527 const char *have_db_name;
528 struct ovsdb_error *error;
529 struct jsonrpc_msg *reply;
531 params = json_array(request->params);
532 if (!params->n || params->elems[0]->type != JSON_STRING) {
533 error = ovsdb_syntax_error(
534 request->params, NULL,
535 "%s request params must begin with <db-name>", request->method);
539 want_db_name = params->elems[0]->u.string;
540 have_db_name = get_db_name(s);
541 if (strcmp(want_db_name, have_db_name)) {
542 error = ovsdb_syntax_error(
543 request->params, "unknown database",
544 "%s request specifies unknown database %s",
545 request->method, want_db_name);
552 reply = jsonrpc_create_reply(ovsdb_error_to_json(error), request->id);
553 ovsdb_error_destroy(error);
557 static struct ovsdb_error *
558 ovsdb_jsonrpc_session_parse_lock_name(const struct jsonrpc_msg *request,
559 const char **lock_namep)
561 const struct json_array *params;
563 params = json_array(request->params);
564 if (params->n != 1 || params->elems[0]->type != JSON_STRING ||
565 !ovsdb_parser_is_id(json_string(params->elems[0]))) {
567 return ovsdb_syntax_error(request->params, NULL,
568 "%s request params must be <id>",
572 *lock_namep = json_string(params->elems[0]);
577 ovsdb_jsonrpc_session_notify(struct ovsdb_session *session,
578 const char *lock_name,
581 struct ovsdb_jsonrpc_session *s;
584 s = CONTAINER_OF(session, struct ovsdb_jsonrpc_session, up);
585 params = json_array_create_1(json_string_create(lock_name));
586 jsonrpc_session_send(s->js, jsonrpc_create_notify(method, params));
589 static struct jsonrpc_msg *
590 ovsdb_jsonrpc_session_lock(struct ovsdb_jsonrpc_session *s,
591 struct jsonrpc_msg *request,
592 enum ovsdb_lock_mode mode)
594 struct ovsdb_lock_waiter *waiter;
595 struct jsonrpc_msg *reply;
596 struct ovsdb_error *error;
597 struct ovsdb_session *victim;
598 const char *lock_name;
601 error = ovsdb_jsonrpc_session_parse_lock_name(request, &lock_name);
606 /* Report error if this session has issued a "lock" or "steal" without a
607 * matching "unlock" for this lock. */
608 waiter = ovsdb_session_get_lock_waiter(&s->up, lock_name);
610 error = ovsdb_syntax_error(
611 request->params, NULL,
612 "must issue \"unlock\" before new \"%s\"", request->method);
616 /* Get the lock, add us as a waiter. */
617 waiter = ovsdb_server_lock(&s->remote->server->up, &s->up, lock_name, mode,
620 ovsdb_jsonrpc_session_notify(victim, lock_name, "stolen");
623 result = json_object_create();
624 json_object_put(result, "locked",
625 json_boolean_create(ovsdb_lock_waiter_is_owner(waiter)));
627 return jsonrpc_create_reply(result, request->id);
630 reply = jsonrpc_create_reply(ovsdb_error_to_json(error), request->id);
631 ovsdb_error_destroy(error);
636 ovsdb_jsonrpc_session_unlock_all(struct ovsdb_jsonrpc_session *s)
638 struct ovsdb_lock_waiter *waiter, *next;
640 HMAP_FOR_EACH_SAFE (waiter, next, session_node, &s->up.waiters) {
641 ovsdb_jsonrpc_session_unlock__(waiter);
646 ovsdb_jsonrpc_session_unlock__(struct ovsdb_lock_waiter *waiter)
648 struct ovsdb_lock *lock = waiter->lock;
651 struct ovsdb_session *new_owner = ovsdb_lock_waiter_remove(waiter);
653 ovsdb_jsonrpc_session_notify(new_owner, lock->name, "locked");
655 /* ovsdb_server_lock() might have freed 'lock'. */
659 ovsdb_lock_waiter_destroy(waiter);
662 static struct jsonrpc_msg *
663 ovsdb_jsonrpc_session_unlock(struct ovsdb_jsonrpc_session *s,
664 struct jsonrpc_msg *request)
666 struct ovsdb_lock_waiter *waiter;
667 struct jsonrpc_msg *reply;
668 struct ovsdb_error *error;
669 const char *lock_name;
671 error = ovsdb_jsonrpc_session_parse_lock_name(request, &lock_name);
676 /* Report error if this session has not issued a "lock" or "steal" for this
678 waiter = ovsdb_session_get_lock_waiter(&s->up, lock_name);
680 error = ovsdb_syntax_error(
681 request->params, NULL, "\"unlock\" without \"lock\" or \"steal\"");
685 ovsdb_jsonrpc_session_unlock__(waiter);
687 return jsonrpc_create_reply(json_object_create(), request->id);
690 reply = jsonrpc_create_reply(ovsdb_error_to_json(error), request->id);
691 ovsdb_error_destroy(error);
695 static struct jsonrpc_msg *
696 execute_transaction(struct ovsdb_jsonrpc_session *s,
697 struct jsonrpc_msg *request)
699 ovsdb_jsonrpc_trigger_create(s, request->id, request->params);
701 request->params = NULL;
702 jsonrpc_msg_destroy(request);
707 ovsdb_jsonrpc_session_got_request(struct ovsdb_jsonrpc_session *s,
708 struct jsonrpc_msg *request)
710 struct jsonrpc_msg *reply;
712 if (!strcmp(request->method, "transact")) {
713 reply = ovsdb_jsonrpc_check_db_name(s, request);
715 reply = execute_transaction(s, request);
717 } else if (!strcmp(request->method, "monitor")) {
718 reply = ovsdb_jsonrpc_check_db_name(s, request);
720 reply = jsonrpc_create_reply(
721 ovsdb_jsonrpc_monitor_create(s, request->params), request->id);
723 } else if (!strcmp(request->method, "monitor_cancel")) {
724 reply = ovsdb_jsonrpc_monitor_cancel(s, json_array(request->params),
726 } else if (!strcmp(request->method, "get_schema")) {
727 reply = ovsdb_jsonrpc_check_db_name(s, request);
729 reply = jsonrpc_create_reply(
730 ovsdb_schema_to_json(s->remote->server->up.db->schema),
733 } else if (!strcmp(request->method, "list_dbs")) {
734 reply = jsonrpc_create_reply(
735 json_array_create_1(json_string_create(get_db_name(s))),
737 } else if (!strcmp(request->method, "lock")) {
738 reply = ovsdb_jsonrpc_session_lock(s, request, OVSDB_LOCK_WAIT);
739 } else if (!strcmp(request->method, "steal")) {
740 reply = ovsdb_jsonrpc_session_lock(s, request, OVSDB_LOCK_STEAL);
741 } else if (!strcmp(request->method, "unlock")) {
742 reply = ovsdb_jsonrpc_session_unlock(s, request);
743 } else if (!strcmp(request->method, "echo")) {
744 reply = jsonrpc_create_reply(json_clone(request->params), request->id);
746 reply = jsonrpc_create_error(json_string_create("unknown method"),
751 jsonrpc_msg_destroy(request);
752 jsonrpc_session_send(s->js, reply);
757 execute_cancel(struct ovsdb_jsonrpc_session *s, struct jsonrpc_msg *request)
759 if (json_array(request->params)->n == 1) {
760 struct ovsdb_jsonrpc_trigger *t;
763 id = request->params->u.array.elems[0];
764 t = ovsdb_jsonrpc_trigger_find(s, id, json_hash(id, 0));
766 ovsdb_jsonrpc_trigger_complete(t);
772 ovsdb_jsonrpc_session_got_notify(struct ovsdb_jsonrpc_session *s,
773 struct jsonrpc_msg *request)
775 if (!strcmp(request->method, "cancel")) {
776 execute_cancel(s, request);
778 jsonrpc_msg_destroy(request);
781 /* JSON-RPC database server triggers.
783 * (Every transaction is treated as a trigger even if it doesn't actually have
784 * any "wait" operations.) */
786 struct ovsdb_jsonrpc_trigger {
787 struct ovsdb_trigger trigger;
788 struct hmap_node hmap_node; /* In session's "triggers" hmap. */
793 ovsdb_jsonrpc_trigger_create(struct ovsdb_jsonrpc_session *s,
794 struct json *id, struct json *params)
796 struct ovsdb_jsonrpc_trigger *t;
799 /* Check for duplicate ID. */
800 hash = json_hash(id, 0);
801 t = ovsdb_jsonrpc_trigger_find(s, id, hash);
803 struct jsonrpc_msg *msg;
805 msg = jsonrpc_create_error(json_string_create("duplicate request ID"),
807 jsonrpc_session_send(s->js, msg);
809 json_destroy(params);
813 /* Insert into trigger table. */
814 t = xmalloc(sizeof *t);
815 ovsdb_trigger_init(&s->up, &t->trigger, params, time_msec());
817 hmap_insert(&s->triggers, &t->hmap_node, hash);
819 /* Complete early if possible. */
820 if (ovsdb_trigger_is_complete(&t->trigger)) {
821 ovsdb_jsonrpc_trigger_complete(t);
825 static struct ovsdb_jsonrpc_trigger *
826 ovsdb_jsonrpc_trigger_find(struct ovsdb_jsonrpc_session *s,
827 const struct json *id, size_t hash)
829 struct ovsdb_jsonrpc_trigger *t;
831 HMAP_FOR_EACH_WITH_HASH (t, hmap_node, hash, &s->triggers) {
832 if (json_equal(t->id, id)) {
841 ovsdb_jsonrpc_trigger_complete(struct ovsdb_jsonrpc_trigger *t)
843 struct ovsdb_jsonrpc_session *s;
845 s = CONTAINER_OF(t->trigger.session, struct ovsdb_jsonrpc_session, up);
847 if (jsonrpc_session_is_connected(s->js)) {
848 struct jsonrpc_msg *reply;
851 result = ovsdb_trigger_steal_result(&t->trigger);
853 reply = jsonrpc_create_reply(result, t->id);
855 reply = jsonrpc_create_error(json_string_create("canceled"),
858 jsonrpc_session_send(s->js, reply);
862 ovsdb_trigger_destroy(&t->trigger);
863 hmap_remove(&s->triggers, &t->hmap_node);
868 ovsdb_jsonrpc_trigger_complete_all(struct ovsdb_jsonrpc_session *s)
870 struct ovsdb_jsonrpc_trigger *t, *next;
871 HMAP_FOR_EACH_SAFE (t, next, hmap_node, &s->triggers) {
872 ovsdb_jsonrpc_trigger_complete(t);
877 ovsdb_jsonrpc_trigger_complete_done(struct ovsdb_jsonrpc_session *s)
879 while (!list_is_empty(&s->up.completions)) {
880 struct ovsdb_jsonrpc_trigger *t
881 = CONTAINER_OF(s->up.completions.next,
882 struct ovsdb_jsonrpc_trigger, trigger.node);
883 ovsdb_jsonrpc_trigger_complete(t);
887 /* JSON-RPC database table monitors. */
889 enum ovsdb_jsonrpc_monitor_selection {
890 OJMS_INITIAL = 1 << 0, /* All rows when monitor is created. */
891 OJMS_INSERT = 1 << 1, /* New rows. */
892 OJMS_DELETE = 1 << 2, /* Deleted rows. */
893 OJMS_MODIFY = 1 << 3 /* Modified rows. */
896 /* A particular column being monitored. */
897 struct ovsdb_jsonrpc_monitor_column {
898 const struct ovsdb_column *column;
899 enum ovsdb_jsonrpc_monitor_selection select;
902 /* A particular table being monitored. */
903 struct ovsdb_jsonrpc_monitor_table {
904 const struct ovsdb_table *table;
906 /* This is the union (bitwise-OR) of the 'select' values in all of the
907 * members of 'columns' below. */
908 enum ovsdb_jsonrpc_monitor_selection select;
910 /* Columns being monitored. */
911 struct ovsdb_jsonrpc_monitor_column *columns;
915 /* A collection of tables being monitored. */
916 struct ovsdb_jsonrpc_monitor {
917 struct ovsdb_replica replica;
918 struct ovsdb_jsonrpc_session *session;
919 struct hmap_node node; /* In ovsdb_jsonrpc_session's "monitors". */
921 struct json *monitor_id;
922 struct shash tables; /* Holds "struct ovsdb_jsonrpc_monitor_table"s. */
925 static const struct ovsdb_replica_class ovsdb_jsonrpc_replica_class;
927 struct ovsdb_jsonrpc_monitor *ovsdb_jsonrpc_monitor_find(
928 struct ovsdb_jsonrpc_session *, const struct json *monitor_id);
929 static void ovsdb_jsonrpc_monitor_destroy(struct ovsdb_replica *);
930 static struct json *ovsdb_jsonrpc_monitor_get_initial(
931 const struct ovsdb_jsonrpc_monitor *);
934 parse_bool(struct ovsdb_parser *parser, const char *name, bool default_value)
936 const struct json *json;
938 json = ovsdb_parser_member(parser, name, OP_BOOLEAN | OP_OPTIONAL);
939 return json ? json_boolean(json) : default_value;
942 struct ovsdb_jsonrpc_monitor *
943 ovsdb_jsonrpc_monitor_find(struct ovsdb_jsonrpc_session *s,
944 const struct json *monitor_id)
946 struct ovsdb_jsonrpc_monitor *m;
948 HMAP_FOR_EACH_WITH_HASH (m, node, json_hash(monitor_id, 0), &s->monitors) {
949 if (json_equal(m->monitor_id, monitor_id)) {
958 ovsdb_jsonrpc_add_monitor_column(struct ovsdb_jsonrpc_monitor_table *mt,
959 const struct ovsdb_column *column,
960 enum ovsdb_jsonrpc_monitor_selection select,
961 size_t *allocated_columns)
963 struct ovsdb_jsonrpc_monitor_column *c;
965 if (mt->n_columns >= *allocated_columns) {
966 mt->columns = x2nrealloc(mt->columns, allocated_columns,
967 sizeof *mt->columns);
970 c = &mt->columns[mt->n_columns++];
976 compare_ovsdb_jsonrpc_monitor_column(const void *a_, const void *b_)
978 const struct ovsdb_jsonrpc_monitor_column *a = a_;
979 const struct ovsdb_jsonrpc_monitor_column *b = b_;
981 return a->column < b->column ? -1 : a->column > b->column;
984 static struct ovsdb_error * WARN_UNUSED_RESULT
985 ovsdb_jsonrpc_parse_monitor_request(struct ovsdb_jsonrpc_monitor_table *mt,
986 const struct json *monitor_request,
987 size_t *allocated_columns)
989 const struct ovsdb_table_schema *ts = mt->table->schema;
990 enum ovsdb_jsonrpc_monitor_selection select;
991 const struct json *columns, *select_json;
992 struct ovsdb_parser parser;
993 struct ovsdb_error *error;
995 ovsdb_parser_init(&parser, monitor_request, "table %s", ts->name);
996 columns = ovsdb_parser_member(&parser, "columns", OP_ARRAY | OP_OPTIONAL);
997 select_json = ovsdb_parser_member(&parser, "select",
998 OP_OBJECT | OP_OPTIONAL);
999 error = ovsdb_parser_finish(&parser);
1006 ovsdb_parser_init(&parser, select_json, "table %s select", ts->name);
1007 if (parse_bool(&parser, "initial", true)) {
1008 select |= OJMS_INITIAL;
1010 if (parse_bool(&parser, "insert", true)) {
1011 select |= OJMS_INSERT;
1013 if (parse_bool(&parser, "delete", true)) {
1014 select |= OJMS_DELETE;
1016 if (parse_bool(&parser, "modify", true)) {
1017 select |= OJMS_MODIFY;
1019 error = ovsdb_parser_finish(&parser);
1024 select = OJMS_INITIAL | OJMS_INSERT | OJMS_DELETE | OJMS_MODIFY;
1026 mt->select |= select;
1031 if (columns->type != JSON_ARRAY) {
1032 return ovsdb_syntax_error(columns, NULL,
1033 "array of column names expected");
1036 for (i = 0; i < columns->u.array.n; i++) {
1037 const struct ovsdb_column *column;
1040 if (columns->u.array.elems[i]->type != JSON_STRING) {
1041 return ovsdb_syntax_error(columns, NULL,
1042 "array of column names expected");
1045 s = columns->u.array.elems[i]->u.string;
1046 column = shash_find_data(&mt->table->schema->columns, s);
1048 return ovsdb_syntax_error(columns, NULL, "%s is not a valid "
1051 ovsdb_jsonrpc_add_monitor_column(mt, column, select,
1055 struct shash_node *node;
1057 SHASH_FOR_EACH (node, &ts->columns) {
1058 const struct ovsdb_column *column = node->data;
1059 if (column->index != OVSDB_COL_UUID) {
1060 ovsdb_jsonrpc_add_monitor_column(mt, column, select,
1069 static struct json *
1070 ovsdb_jsonrpc_monitor_create(struct ovsdb_jsonrpc_session *s,
1071 struct json *params)
1073 struct ovsdb_jsonrpc_monitor *m = NULL;
1074 struct json *monitor_id, *monitor_requests;
1075 struct ovsdb_error *error = NULL;
1076 struct shash_node *node;
1079 if (json_array(params)->n != 3) {
1080 error = ovsdb_syntax_error(params, NULL, "invalid parameters");
1083 monitor_id = params->u.array.elems[1];
1084 monitor_requests = params->u.array.elems[2];
1085 if (monitor_requests->type != JSON_OBJECT) {
1086 error = ovsdb_syntax_error(monitor_requests, NULL,
1087 "monitor-requests must be object");
1091 if (ovsdb_jsonrpc_monitor_find(s, monitor_id)) {
1092 error = ovsdb_syntax_error(monitor_id, NULL, "duplicate monitor ID");
1096 m = xzalloc(sizeof *m);
1097 ovsdb_replica_init(&m->replica, &ovsdb_jsonrpc_replica_class);
1098 ovsdb_add_replica(s->remote->server->up.db, &m->replica);
1100 hmap_insert(&s->monitors, &m->node, json_hash(monitor_id, 0));
1101 m->monitor_id = json_clone(monitor_id);
1102 shash_init(&m->tables);
1104 SHASH_FOR_EACH (node, json_object(monitor_requests)) {
1105 const struct ovsdb_table *table;
1106 struct ovsdb_jsonrpc_monitor_table *mt;
1107 size_t allocated_columns;
1108 const struct json *mr_value;
1111 table = ovsdb_get_table(s->remote->server->up.db, node->name);
1113 error = ovsdb_syntax_error(NULL, NULL,
1114 "no table named %s", node->name);
1118 mt = xzalloc(sizeof *mt);
1120 shash_add(&m->tables, table->schema->name, mt);
1122 /* Parse columns. */
1123 mr_value = node->data;
1124 allocated_columns = 0;
1125 if (mr_value->type == JSON_ARRAY) {
1126 const struct json_array *array = &mr_value->u.array;
1128 for (i = 0; i < array->n; i++) {
1129 error = ovsdb_jsonrpc_parse_monitor_request(
1130 mt, array->elems[i], &allocated_columns);
1136 error = ovsdb_jsonrpc_parse_monitor_request(
1137 mt, mr_value, &allocated_columns);
1143 /* Check for duplicate columns. */
1144 qsort(mt->columns, mt->n_columns, sizeof *mt->columns,
1145 compare_ovsdb_jsonrpc_monitor_column);
1146 for (i = 1; i < mt->n_columns; i++) {
1147 if (mt->columns[i].column == mt->columns[i - 1].column) {
1148 error = ovsdb_syntax_error(mr_value, NULL, "column %s "
1149 "mentioned more than once",
1150 mt->columns[i].column->name);
1156 return ovsdb_jsonrpc_monitor_get_initial(m);
1160 ovsdb_remove_replica(s->remote->server->up.db, &m->replica);
1163 json = ovsdb_error_to_json(error);
1164 ovsdb_error_destroy(error);
1168 static struct jsonrpc_msg *
1169 ovsdb_jsonrpc_monitor_cancel(struct ovsdb_jsonrpc_session *s,
1170 struct json_array *params,
1171 const struct json *request_id)
1173 if (params->n != 1) {
1174 return jsonrpc_create_error(json_string_create("invalid parameters"),
1177 struct ovsdb_jsonrpc_monitor *m;
1179 m = ovsdb_jsonrpc_monitor_find(s, params->elems[0]);
1181 return jsonrpc_create_error(json_string_create("unknown monitor"),
1184 ovsdb_remove_replica(s->remote->server->up.db, &m->replica);
1185 return jsonrpc_create_reply(json_object_create(), request_id);
1191 ovsdb_jsonrpc_monitor_remove_all(struct ovsdb_jsonrpc_session *s)
1193 struct ovsdb_jsonrpc_monitor *m, *next;
1195 HMAP_FOR_EACH_SAFE (m, next, node, &s->monitors) {
1196 ovsdb_remove_replica(s->remote->server->up.db, &m->replica);
1200 static struct ovsdb_jsonrpc_monitor *
1201 ovsdb_jsonrpc_monitor_cast(struct ovsdb_replica *replica)
1203 assert(replica->class == &ovsdb_jsonrpc_replica_class);
1204 return CONTAINER_OF(replica, struct ovsdb_jsonrpc_monitor, replica);
1207 struct ovsdb_jsonrpc_monitor_aux {
1208 bool initial; /* Sending initial contents of table? */
1209 const struct ovsdb_jsonrpc_monitor *monitor;
1210 struct json *json; /* JSON for the whole transaction. */
1212 /* Current table. */
1213 struct ovsdb_jsonrpc_monitor_table *mt;
1214 struct json *table_json; /* JSON for table's transaction. */
1218 any_reportable_change(const struct ovsdb_jsonrpc_monitor_table *mt,
1219 const unsigned long int *changed)
1223 for (i = 0; i < mt->n_columns; i++) {
1224 const struct ovsdb_jsonrpc_monitor_column *c = &mt->columns[i];
1225 unsigned int idx = c->column->index;
1227 if (c->select & OJMS_MODIFY && bitmap_is_set(changed, idx)) {
1236 ovsdb_jsonrpc_monitor_change_cb(const struct ovsdb_row *old,
1237 const struct ovsdb_row *new,
1238 const unsigned long int *changed,
1241 struct ovsdb_jsonrpc_monitor_aux *aux = aux_;
1242 const struct ovsdb_jsonrpc_monitor *m = aux->monitor;
1243 struct ovsdb_table *table = new ? new->table : old->table;
1244 enum ovsdb_jsonrpc_monitor_selection type;
1245 struct json *old_json, *new_json;
1246 struct json *row_json;
1247 char uuid[UUID_LEN + 1];
1250 if (!aux->mt || table != aux->mt->table) {
1251 aux->mt = shash_find_data(&m->tables, table->schema->name);
1252 aux->table_json = NULL;
1254 /* We don't care about rows in this table at all. Tell the caller
1260 type = (aux->initial ? OJMS_INITIAL
1261 : !old ? OJMS_INSERT
1262 : !new ? OJMS_DELETE
1264 if (!(aux->mt->select & type)) {
1265 /* We don't care about this type of change (but do want to be called
1266 * back for changes to other rows in the same table). */
1270 if (type == OJMS_MODIFY && !any_reportable_change(aux->mt, changed)) {
1271 /* Nothing of interest changed. */
1275 old_json = new_json = NULL;
1276 if (type & (OJMS_DELETE | OJMS_MODIFY)) {
1277 old_json = json_object_create();
1279 if (type & (OJMS_INITIAL | OJMS_INSERT | OJMS_MODIFY)) {
1280 new_json = json_object_create();
1282 for (i = 0; i < aux->mt->n_columns; i++) {
1283 const struct ovsdb_jsonrpc_monitor_column *c = &aux->mt->columns[i];
1284 const struct ovsdb_column *column = c->column;
1285 unsigned int idx = c->column->index;
1287 if (!(type & c->select)) {
1288 /* We don't care about this type of change for this particular
1289 * column (but we will care about it for some other column). */
1293 if ((type == OJMS_MODIFY && bitmap_is_set(changed, idx))
1294 || type == OJMS_DELETE) {
1295 json_object_put(old_json, column->name,
1296 ovsdb_datum_to_json(&old->fields[idx],
1299 if (type & (OJMS_INITIAL | OJMS_INSERT | OJMS_MODIFY)) {
1300 json_object_put(new_json, column->name,
1301 ovsdb_datum_to_json(&new->fields[idx],
1306 /* Create JSON object for transaction overall. */
1308 aux->json = json_object_create();
1311 /* Create JSON object for transaction on this table. */
1312 if (!aux->table_json) {
1313 aux->table_json = json_object_create();
1314 json_object_put(aux->json, aux->mt->table->schema->name,
1318 /* Create JSON object for transaction on this row. */
1319 row_json = json_object_create();
1321 json_object_put(row_json, "old", old_json);
1324 json_object_put(row_json, "new", new_json);
1327 /* Add JSON row to JSON table. */
1328 snprintf(uuid, sizeof uuid,
1329 UUID_FMT, UUID_ARGS(ovsdb_row_get_uuid(new ? new : old)));
1330 json_object_put(aux->table_json, uuid, row_json);
1336 ovsdb_jsonrpc_monitor_init_aux(struct ovsdb_jsonrpc_monitor_aux *aux,
1337 const struct ovsdb_jsonrpc_monitor *m,
1340 aux->initial = initial;
1344 aux->table_json = NULL;
1347 static struct ovsdb_error *
1348 ovsdb_jsonrpc_monitor_commit(struct ovsdb_replica *replica,
1349 const struct ovsdb_txn *txn,
1350 bool durable OVS_UNUSED)
1352 struct ovsdb_jsonrpc_monitor *m = ovsdb_jsonrpc_monitor_cast(replica);
1353 struct ovsdb_jsonrpc_monitor_aux aux;
1355 ovsdb_jsonrpc_monitor_init_aux(&aux, m, false);
1356 ovsdb_txn_for_each_change(txn, ovsdb_jsonrpc_monitor_change_cb, &aux);
1358 struct jsonrpc_msg *msg;
1359 struct json *params;
1361 params = json_array_create_2(json_clone(aux.monitor->monitor_id),
1363 msg = jsonrpc_create_notify("update", params);
1364 jsonrpc_session_send(aux.monitor->session->js, msg);
1370 static struct json *
1371 ovsdb_jsonrpc_monitor_get_initial(const struct ovsdb_jsonrpc_monitor *m)
1373 struct ovsdb_jsonrpc_monitor_aux aux;
1374 struct shash_node *node;
1376 ovsdb_jsonrpc_monitor_init_aux(&aux, m, true);
1377 SHASH_FOR_EACH (node, &m->tables) {
1378 struct ovsdb_jsonrpc_monitor_table *mt = node->data;
1380 if (mt->select & OJMS_INITIAL) {
1381 struct ovsdb_row *row;
1383 HMAP_FOR_EACH (row, hmap_node, &mt->table->rows) {
1384 ovsdb_jsonrpc_monitor_change_cb(NULL, row, NULL, &aux);
1388 return aux.json ? aux.json : json_object_create();
1392 ovsdb_jsonrpc_monitor_destroy(struct ovsdb_replica *replica)
1394 struct ovsdb_jsonrpc_monitor *m = ovsdb_jsonrpc_monitor_cast(replica);
1395 struct shash_node *node;
1397 json_destroy(m->monitor_id);
1398 SHASH_FOR_EACH (node, &m->tables) {
1399 struct ovsdb_jsonrpc_monitor_table *mt = node->data;
1403 shash_destroy(&m->tables);
1404 hmap_remove(&m->session->monitors, &m->node);
1408 static const struct ovsdb_replica_class ovsdb_jsonrpc_replica_class = {
1409 ovsdb_jsonrpc_monitor_commit,
1410 ovsdb_jsonrpc_monitor_destroy