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(void)
130 struct ovsdb_jsonrpc_options *options = xzalloc(sizeof *options);
131 options->probe_interval = RECONNECT_DEFAULT_PROBE_INTERVAL;
132 options->max_backoff = RECONNECT_DEFAULT_MAX_BACKOFF;
136 /* Sets 'svr''s current set of remotes to the names in 'new_remotes', with
137 * options in the struct ovsdb_jsonrpc_options supplied as the data values.
139 * A remote is an active or passive stream connection method, e.g. "pssl:" or
142 ovsdb_jsonrpc_server_set_remotes(struct ovsdb_jsonrpc_server *svr,
143 const struct shash *new_remotes)
145 struct shash_node *node, *next;
147 SHASH_FOR_EACH_SAFE (node, next, &svr->remotes) {
148 if (!shash_find(new_remotes, node->name)) {
149 VLOG_INFO("%s: remote deconfigured", node->name);
150 ovsdb_jsonrpc_server_del_remote(node);
153 SHASH_FOR_EACH (node, new_remotes) {
154 const struct ovsdb_jsonrpc_options *options = node->data;
155 struct ovsdb_jsonrpc_remote *remote;
157 remote = shash_find_data(&svr->remotes, node->name);
159 remote = ovsdb_jsonrpc_server_add_remote(svr, node->name);
165 ovsdb_jsonrpc_session_set_all_options(remote, options);
169 static struct ovsdb_jsonrpc_remote *
170 ovsdb_jsonrpc_server_add_remote(struct ovsdb_jsonrpc_server *svr,
173 struct ovsdb_jsonrpc_remote *remote;
174 struct pstream *listener;
177 error = jsonrpc_pstream_open(name, &listener);
178 if (error && error != EAFNOSUPPORT) {
179 VLOG_ERR_RL(&rl, "%s: listen failed: %s", name, strerror(error));
183 remote = xmalloc(sizeof *remote);
184 remote->server = svr;
185 remote->listener = listener;
186 list_init(&remote->sessions);
187 shash_add(&svr->remotes, name, remote);
190 ovsdb_jsonrpc_session_create(remote, jsonrpc_session_open(name));
196 ovsdb_jsonrpc_server_del_remote(struct shash_node *node)
198 struct ovsdb_jsonrpc_remote *remote = node->data;
200 ovsdb_jsonrpc_session_close_all(remote);
201 pstream_close(remote->listener);
202 shash_delete(&remote->server->remotes, node);
206 /* Stores status information for the remote named 'target', which should have
207 * been configured on 'svr' with a call to ovsdb_jsonrpc_server_set_remotes(),
208 * into '*status'. On success returns true, on failure (if 'svr' doesn't have
209 * a remote named 'target' or if that remote is an inbound remote that has no
210 * active connections) returns false. On failure, 'status' will be zeroed.
213 ovsdb_jsonrpc_server_get_remote_status(
214 const struct ovsdb_jsonrpc_server *svr, const char *target,
215 struct ovsdb_jsonrpc_remote_status *status)
217 const struct ovsdb_jsonrpc_remote *remote;
219 memset(status, 0, sizeof *status);
221 remote = shash_find_data(&svr->remotes, target);
222 return remote && ovsdb_jsonrpc_session_get_status(remote, status);
226 ovsdb_jsonrpc_server_free_remote_status(
227 struct ovsdb_jsonrpc_remote_status *status)
229 free(status->locks_held);
230 free(status->locks_waiting);
231 free(status->locks_lost);
234 /* Forces all of the JSON-RPC sessions managed by 'svr' to disconnect and
237 ovsdb_jsonrpc_server_reconnect(struct ovsdb_jsonrpc_server *svr)
239 struct shash_node *node;
241 SHASH_FOR_EACH (node, &svr->remotes) {
242 struct ovsdb_jsonrpc_remote *remote = node->data;
244 ovsdb_jsonrpc_session_reconnect_all(remote);
249 ovsdb_jsonrpc_server_run(struct ovsdb_jsonrpc_server *svr)
251 struct shash_node *node;
253 SHASH_FOR_EACH (node, &svr->remotes) {
254 struct ovsdb_jsonrpc_remote *remote = node->data;
256 if (remote->listener && svr->n_sessions < svr->max_sessions) {
257 struct stream *stream;
260 error = pstream_accept(remote->listener, &stream);
262 struct jsonrpc_session *js;
263 js = jsonrpc_session_open_unreliably(jsonrpc_open(stream));
264 ovsdb_jsonrpc_session_create(remote, js);
265 } else if (error != EAGAIN) {
266 VLOG_WARN_RL(&rl, "%s: accept failed: %s",
267 pstream_get_name(remote->listener),
272 ovsdb_jsonrpc_session_run_all(remote);
277 ovsdb_jsonrpc_server_wait(struct ovsdb_jsonrpc_server *svr)
279 struct shash_node *node;
281 SHASH_FOR_EACH (node, &svr->remotes) {
282 struct ovsdb_jsonrpc_remote *remote = node->data;
284 if (remote->listener && svr->n_sessions < svr->max_sessions) {
285 pstream_wait(remote->listener);
288 ovsdb_jsonrpc_session_wait_all(remote);
292 /* JSON-RPC database server session. */
294 struct ovsdb_jsonrpc_session {
295 struct ovsdb_session up;
296 struct ovsdb_jsonrpc_remote *remote;
297 struct list node; /* Element in remote's sessions list. */
300 struct hmap triggers; /* Hmap of "struct ovsdb_jsonrpc_trigger"s. */
303 struct hmap monitors; /* Hmap of "struct ovsdb_jsonrpc_monitor"s. */
305 /* Network connectivity. */
306 struct jsonrpc_session *js; /* JSON-RPC session. */
307 unsigned int js_seqno; /* Last jsonrpc_session_get_seqno() value. */
310 static void ovsdb_jsonrpc_session_close(struct ovsdb_jsonrpc_session *);
311 static int ovsdb_jsonrpc_session_run(struct ovsdb_jsonrpc_session *);
312 static void ovsdb_jsonrpc_session_wait(struct ovsdb_jsonrpc_session *);
313 static void ovsdb_jsonrpc_session_set_options(
314 struct ovsdb_jsonrpc_session *, const struct ovsdb_jsonrpc_options *);
315 static void ovsdb_jsonrpc_session_got_request(struct ovsdb_jsonrpc_session *,
316 struct jsonrpc_msg *);
317 static void ovsdb_jsonrpc_session_got_notify(struct ovsdb_jsonrpc_session *,
318 struct jsonrpc_msg *);
320 static struct ovsdb_jsonrpc_session *
321 ovsdb_jsonrpc_session_create(struct ovsdb_jsonrpc_remote *remote,
322 struct jsonrpc_session *js)
324 struct ovsdb_jsonrpc_session *s;
326 s = xzalloc(sizeof *s);
327 ovsdb_session_init(&s->up, remote->server->up.db);
329 list_push_back(&remote->sessions, &s->node);
330 hmap_init(&s->triggers);
331 hmap_init(&s->monitors);
333 s->js_seqno = jsonrpc_session_get_seqno(js);
335 remote->server->n_sessions++;
341 ovsdb_jsonrpc_session_close(struct ovsdb_jsonrpc_session *s)
343 ovsdb_jsonrpc_monitor_remove_all(s);
344 ovsdb_jsonrpc_session_unlock_all(s);
345 jsonrpc_session_close(s->js);
346 list_remove(&s->node);
347 ovsdb_session_destroy(&s->up);
348 s->remote->server->n_sessions--;
349 ovsdb_session_destroy(&s->up);
354 ovsdb_jsonrpc_session_run(struct ovsdb_jsonrpc_session *s)
356 jsonrpc_session_run(s->js);
357 if (s->js_seqno != jsonrpc_session_get_seqno(s->js)) {
358 s->js_seqno = jsonrpc_session_get_seqno(s->js);
359 ovsdb_jsonrpc_trigger_complete_all(s);
360 ovsdb_jsonrpc_monitor_remove_all(s);
361 ovsdb_jsonrpc_session_unlock_all(s);
364 ovsdb_jsonrpc_trigger_complete_done(s);
366 if (!jsonrpc_session_get_backlog(s->js)) {
367 struct jsonrpc_msg *msg = jsonrpc_session_recv(s->js);
369 if (msg->type == JSONRPC_REQUEST) {
370 ovsdb_jsonrpc_session_got_request(s, msg);
371 } else if (msg->type == JSONRPC_NOTIFY) {
372 ovsdb_jsonrpc_session_got_notify(s, msg);
374 VLOG_WARN("%s: received unexpected %s message",
375 jsonrpc_session_get_name(s->js),
376 jsonrpc_msg_type_to_string(msg->type));
377 jsonrpc_session_force_reconnect(s->js);
378 jsonrpc_msg_destroy(msg);
382 return jsonrpc_session_is_alive(s->js) ? 0 : ETIMEDOUT;
386 ovsdb_jsonrpc_session_set_options(struct ovsdb_jsonrpc_session *session,
387 const struct ovsdb_jsonrpc_options *options)
389 jsonrpc_session_set_max_backoff(session->js, options->max_backoff);
390 jsonrpc_session_set_probe_interval(session->js, options->probe_interval);
394 ovsdb_jsonrpc_session_run_all(struct ovsdb_jsonrpc_remote *remote)
396 struct ovsdb_jsonrpc_session *s, *next;
398 LIST_FOR_EACH_SAFE (s, next, node, &remote->sessions) {
399 int error = ovsdb_jsonrpc_session_run(s);
401 ovsdb_jsonrpc_session_close(s);
407 ovsdb_jsonrpc_session_wait(struct ovsdb_jsonrpc_session *s)
409 jsonrpc_session_wait(s->js);
410 if (!jsonrpc_session_get_backlog(s->js)) {
411 jsonrpc_session_recv_wait(s->js);
416 ovsdb_jsonrpc_session_wait_all(struct ovsdb_jsonrpc_remote *remote)
418 struct ovsdb_jsonrpc_session *s;
420 LIST_FOR_EACH (s, node, &remote->sessions) {
421 ovsdb_jsonrpc_session_wait(s);
426 ovsdb_jsonrpc_session_close_all(struct ovsdb_jsonrpc_remote *remote)
428 struct ovsdb_jsonrpc_session *s, *next;
430 LIST_FOR_EACH_SAFE (s, next, node, &remote->sessions) {
431 ovsdb_jsonrpc_session_close(s);
435 /* Forces all of the JSON-RPC sessions managed by 'remote' to disconnect and
438 ovsdb_jsonrpc_session_reconnect_all(struct ovsdb_jsonrpc_remote *remote)
440 struct ovsdb_jsonrpc_session *s, *next;
442 LIST_FOR_EACH_SAFE (s, next, node, &remote->sessions) {
443 jsonrpc_session_force_reconnect(s->js);
444 if (!jsonrpc_session_is_alive(s->js)) {
445 ovsdb_jsonrpc_session_close(s);
450 /* Sets the options for all of the JSON-RPC sessions managed by 'remote' to
453 ovsdb_jsonrpc_session_set_all_options(
454 struct ovsdb_jsonrpc_remote *remote,
455 const struct ovsdb_jsonrpc_options *options)
457 struct ovsdb_jsonrpc_session *s;
459 LIST_FOR_EACH (s, node, &remote->sessions) {
460 ovsdb_jsonrpc_session_set_options(s, options);
465 ovsdb_jsonrpc_session_get_status(const struct ovsdb_jsonrpc_remote *remote,
466 struct ovsdb_jsonrpc_remote_status *status)
468 const struct ovsdb_jsonrpc_session *s;
469 const struct jsonrpc_session *js;
470 struct ovsdb_lock_waiter *waiter;
471 struct reconnect_stats rstats;
472 struct ds locks_held, locks_waiting, locks_lost;
474 if (list_is_empty(&remote->sessions)) {
477 s = CONTAINER_OF(remote->sessions.next, struct ovsdb_jsonrpc_session, node);
480 status->is_connected = jsonrpc_session_is_connected(js);
481 status->last_error = jsonrpc_session_get_status(js);
483 jsonrpc_session_get_reconnect_stats(js, &rstats);
484 status->state = rstats.state;
485 status->sec_since_connect = rstats.msec_since_connect == UINT_MAX
486 ? UINT_MAX : rstats.msec_since_connect / 1000;
487 status->sec_since_disconnect = rstats.msec_since_disconnect == UINT_MAX
488 ? UINT_MAX : rstats.msec_since_disconnect / 1000;
490 ds_init(&locks_held);
491 ds_init(&locks_waiting);
492 ds_init(&locks_lost);
493 HMAP_FOR_EACH (waiter, session_node, &s->up.waiters) {
496 string = (ovsdb_lock_waiter_is_owner(waiter) ? &locks_held
497 : waiter->mode == OVSDB_LOCK_WAIT ? &locks_waiting
499 if (string->length) {
500 ds_put_char(string, ' ');
502 ds_put_cstr(string, waiter->lock_name);
504 status->locks_held = ds_steal_cstr(&locks_held);
505 status->locks_waiting = ds_steal_cstr(&locks_waiting);
506 status->locks_lost = ds_steal_cstr(&locks_lost);
508 status->n_connections = list_size(&remote->sessions);
514 get_db_name(const struct ovsdb_jsonrpc_session *s)
516 return s->remote->server->up.db->schema->name;
519 static struct jsonrpc_msg *
520 ovsdb_jsonrpc_check_db_name(const struct ovsdb_jsonrpc_session *s,
521 const struct jsonrpc_msg *request)
523 struct json_array *params;
524 const char *want_db_name;
525 const char *have_db_name;
526 struct ovsdb_error *error;
527 struct jsonrpc_msg *reply;
529 params = json_array(request->params);
530 if (!params->n || params->elems[0]->type != JSON_STRING) {
531 error = ovsdb_syntax_error(
532 request->params, NULL,
533 "%s request params must begin with <db-name>", request->method);
537 want_db_name = params->elems[0]->u.string;
538 have_db_name = get_db_name(s);
539 if (strcmp(want_db_name, have_db_name)) {
540 error = ovsdb_syntax_error(
541 request->params, "unknown database",
542 "%s request specifies unknown database %s",
543 request->method, want_db_name);
550 reply = jsonrpc_create_reply(ovsdb_error_to_json(error), request->id);
551 ovsdb_error_destroy(error);
555 static struct ovsdb_error *
556 ovsdb_jsonrpc_session_parse_lock_name(const struct jsonrpc_msg *request,
557 const char **lock_namep)
559 const struct json_array *params;
561 params = json_array(request->params);
562 if (params->n != 1 || params->elems[0]->type != JSON_STRING ||
563 !ovsdb_parser_is_id(json_string(params->elems[0]))) {
565 return ovsdb_syntax_error(request->params, NULL,
566 "%s request params must be <id>",
570 *lock_namep = json_string(params->elems[0]);
575 ovsdb_jsonrpc_session_notify(struct ovsdb_session *session,
576 const char *lock_name,
579 struct ovsdb_jsonrpc_session *s;
582 s = CONTAINER_OF(session, struct ovsdb_jsonrpc_session, up);
583 params = json_array_create_1(json_string_create(lock_name));
584 jsonrpc_session_send(s->js, jsonrpc_create_notify(method, params));
587 static struct jsonrpc_msg *
588 ovsdb_jsonrpc_session_lock(struct ovsdb_jsonrpc_session *s,
589 struct jsonrpc_msg *request,
590 enum ovsdb_lock_mode mode)
592 struct ovsdb_lock_waiter *waiter;
593 struct jsonrpc_msg *reply;
594 struct ovsdb_error *error;
595 struct ovsdb_session *victim;
596 const char *lock_name;
599 error = ovsdb_jsonrpc_session_parse_lock_name(request, &lock_name);
604 /* Report error if this session has issued a "lock" or "steal" without a
605 * matching "unlock" for this lock. */
606 waiter = ovsdb_session_get_lock_waiter(&s->up, lock_name);
608 error = ovsdb_syntax_error(
609 request->params, NULL,
610 "must issue \"unlock\" before new \"%s\"", request->method);
614 /* Get the lock, add us as a waiter. */
615 waiter = ovsdb_server_lock(&s->remote->server->up, &s->up, lock_name, mode,
618 ovsdb_jsonrpc_session_notify(victim, lock_name, "stolen");
621 result = json_object_create();
622 json_object_put(result, "locked",
623 json_boolean_create(ovsdb_lock_waiter_is_owner(waiter)));
625 return jsonrpc_create_reply(result, request->id);
628 reply = jsonrpc_create_reply(ovsdb_error_to_json(error), request->id);
629 ovsdb_error_destroy(error);
634 ovsdb_jsonrpc_session_unlock_all(struct ovsdb_jsonrpc_session *s)
636 struct ovsdb_lock_waiter *waiter, *next;
638 HMAP_FOR_EACH_SAFE (waiter, next, session_node, &s->up.waiters) {
639 ovsdb_jsonrpc_session_unlock__(waiter);
644 ovsdb_jsonrpc_session_unlock__(struct ovsdb_lock_waiter *waiter)
646 struct ovsdb_lock *lock = waiter->lock;
649 struct ovsdb_session *new_owner = ovsdb_lock_waiter_remove(waiter);
651 ovsdb_jsonrpc_session_notify(new_owner, lock->name, "locked");
653 /* ovsdb_server_lock() might have freed 'lock'. */
657 ovsdb_lock_waiter_destroy(waiter);
660 static struct jsonrpc_msg *
661 ovsdb_jsonrpc_session_unlock(struct ovsdb_jsonrpc_session *s,
662 struct jsonrpc_msg *request)
664 struct ovsdb_lock_waiter *waiter;
665 struct jsonrpc_msg *reply;
666 struct ovsdb_error *error;
667 const char *lock_name;
669 error = ovsdb_jsonrpc_session_parse_lock_name(request, &lock_name);
674 /* Report error if this session has not issued a "lock" or "steal" for this
676 waiter = ovsdb_session_get_lock_waiter(&s->up, lock_name);
678 error = ovsdb_syntax_error(
679 request->params, NULL, "\"unlock\" without \"lock\" or \"steal\"");
683 ovsdb_jsonrpc_session_unlock__(waiter);
685 return jsonrpc_create_reply(json_object_create(), request->id);
688 reply = jsonrpc_create_reply(ovsdb_error_to_json(error), request->id);
689 ovsdb_error_destroy(error);
693 static struct jsonrpc_msg *
694 execute_transaction(struct ovsdb_jsonrpc_session *s,
695 struct jsonrpc_msg *request)
697 ovsdb_jsonrpc_trigger_create(s, request->id, request->params);
699 request->params = NULL;
700 jsonrpc_msg_destroy(request);
705 ovsdb_jsonrpc_session_got_request(struct ovsdb_jsonrpc_session *s,
706 struct jsonrpc_msg *request)
708 struct jsonrpc_msg *reply;
710 if (!strcmp(request->method, "transact")) {
711 reply = ovsdb_jsonrpc_check_db_name(s, request);
713 reply = execute_transaction(s, request);
715 } else if (!strcmp(request->method, "monitor")) {
716 reply = ovsdb_jsonrpc_check_db_name(s, request);
718 reply = jsonrpc_create_reply(
719 ovsdb_jsonrpc_monitor_create(s, request->params), request->id);
721 } else if (!strcmp(request->method, "monitor_cancel")) {
722 reply = ovsdb_jsonrpc_monitor_cancel(s, json_array(request->params),
724 } else if (!strcmp(request->method, "get_schema")) {
725 reply = ovsdb_jsonrpc_check_db_name(s, request);
727 reply = jsonrpc_create_reply(
728 ovsdb_schema_to_json(s->remote->server->up.db->schema),
731 } else if (!strcmp(request->method, "list_dbs")) {
732 reply = jsonrpc_create_reply(
733 json_array_create_1(json_string_create(get_db_name(s))),
735 } else if (!strcmp(request->method, "lock")) {
736 reply = ovsdb_jsonrpc_session_lock(s, request, OVSDB_LOCK_WAIT);
737 } else if (!strcmp(request->method, "steal")) {
738 reply = ovsdb_jsonrpc_session_lock(s, request, OVSDB_LOCK_STEAL);
739 } else if (!strcmp(request->method, "unlock")) {
740 reply = ovsdb_jsonrpc_session_unlock(s, request);
741 } else if (!strcmp(request->method, "echo")) {
742 reply = jsonrpc_create_reply(json_clone(request->params), request->id);
744 reply = jsonrpc_create_error(json_string_create("unknown method"),
749 jsonrpc_msg_destroy(request);
750 jsonrpc_session_send(s->js, reply);
755 execute_cancel(struct ovsdb_jsonrpc_session *s, struct jsonrpc_msg *request)
757 if (json_array(request->params)->n == 1) {
758 struct ovsdb_jsonrpc_trigger *t;
761 id = request->params->u.array.elems[0];
762 t = ovsdb_jsonrpc_trigger_find(s, id, json_hash(id, 0));
764 ovsdb_jsonrpc_trigger_complete(t);
770 ovsdb_jsonrpc_session_got_notify(struct ovsdb_jsonrpc_session *s,
771 struct jsonrpc_msg *request)
773 if (!strcmp(request->method, "cancel")) {
774 execute_cancel(s, request);
776 jsonrpc_msg_destroy(request);
779 /* JSON-RPC database server triggers.
781 * (Every transaction is treated as a trigger even if it doesn't actually have
782 * any "wait" operations.) */
784 struct ovsdb_jsonrpc_trigger {
785 struct ovsdb_trigger trigger;
786 struct hmap_node hmap_node; /* In session's "triggers" hmap. */
791 ovsdb_jsonrpc_trigger_create(struct ovsdb_jsonrpc_session *s,
792 struct json *id, struct json *params)
794 struct ovsdb_jsonrpc_trigger *t;
797 /* Check for duplicate ID. */
798 hash = json_hash(id, 0);
799 t = ovsdb_jsonrpc_trigger_find(s, id, hash);
801 struct jsonrpc_msg *msg;
803 msg = jsonrpc_create_error(json_string_create("duplicate request ID"),
805 jsonrpc_session_send(s->js, msg);
807 json_destroy(params);
811 /* Insert into trigger table. */
812 t = xmalloc(sizeof *t);
813 ovsdb_trigger_init(&s->up, &t->trigger, params, time_msec());
815 hmap_insert(&s->triggers, &t->hmap_node, hash);
817 /* Complete early if possible. */
818 if (ovsdb_trigger_is_complete(&t->trigger)) {
819 ovsdb_jsonrpc_trigger_complete(t);
823 static struct ovsdb_jsonrpc_trigger *
824 ovsdb_jsonrpc_trigger_find(struct ovsdb_jsonrpc_session *s,
825 const struct json *id, size_t hash)
827 struct ovsdb_jsonrpc_trigger *t;
829 HMAP_FOR_EACH_WITH_HASH (t, hmap_node, hash, &s->triggers) {
830 if (json_equal(t->id, id)) {
839 ovsdb_jsonrpc_trigger_complete(struct ovsdb_jsonrpc_trigger *t)
841 struct ovsdb_jsonrpc_session *s;
843 s = CONTAINER_OF(t->trigger.session, struct ovsdb_jsonrpc_session, up);
845 if (jsonrpc_session_is_connected(s->js)) {
846 struct jsonrpc_msg *reply;
849 result = ovsdb_trigger_steal_result(&t->trigger);
851 reply = jsonrpc_create_reply(result, t->id);
853 reply = jsonrpc_create_error(json_string_create("canceled"),
856 jsonrpc_session_send(s->js, reply);
860 ovsdb_trigger_destroy(&t->trigger);
861 hmap_remove(&s->triggers, &t->hmap_node);
866 ovsdb_jsonrpc_trigger_complete_all(struct ovsdb_jsonrpc_session *s)
868 struct ovsdb_jsonrpc_trigger *t, *next;
869 HMAP_FOR_EACH_SAFE (t, next, hmap_node, &s->triggers) {
870 ovsdb_jsonrpc_trigger_complete(t);
875 ovsdb_jsonrpc_trigger_complete_done(struct ovsdb_jsonrpc_session *s)
877 while (!list_is_empty(&s->up.completions)) {
878 struct ovsdb_jsonrpc_trigger *t
879 = CONTAINER_OF(s->up.completions.next,
880 struct ovsdb_jsonrpc_trigger, trigger.node);
881 ovsdb_jsonrpc_trigger_complete(t);
885 /* JSON-RPC database table monitors. */
887 enum ovsdb_jsonrpc_monitor_selection {
888 OJMS_INITIAL = 1 << 0, /* All rows when monitor is created. */
889 OJMS_INSERT = 1 << 1, /* New rows. */
890 OJMS_DELETE = 1 << 2, /* Deleted rows. */
891 OJMS_MODIFY = 1 << 3 /* Modified rows. */
894 /* A particular column being monitored. */
895 struct ovsdb_jsonrpc_monitor_column {
896 const struct ovsdb_column *column;
897 enum ovsdb_jsonrpc_monitor_selection select;
900 /* A particular table being monitored. */
901 struct ovsdb_jsonrpc_monitor_table {
902 const struct ovsdb_table *table;
904 /* This is the union (bitwise-OR) of the 'select' values in all of the
905 * members of 'columns' below. */
906 enum ovsdb_jsonrpc_monitor_selection select;
908 /* Columns being monitored. */
909 struct ovsdb_jsonrpc_monitor_column *columns;
913 /* A collection of tables being monitored. */
914 struct ovsdb_jsonrpc_monitor {
915 struct ovsdb_replica replica;
916 struct ovsdb_jsonrpc_session *session;
917 struct hmap_node node; /* In ovsdb_jsonrpc_session's "monitors". */
919 struct json *monitor_id;
920 struct shash tables; /* Holds "struct ovsdb_jsonrpc_monitor_table"s. */
923 static const struct ovsdb_replica_class ovsdb_jsonrpc_replica_class;
925 struct ovsdb_jsonrpc_monitor *ovsdb_jsonrpc_monitor_find(
926 struct ovsdb_jsonrpc_session *, const struct json *monitor_id);
927 static void ovsdb_jsonrpc_monitor_destroy(struct ovsdb_replica *);
928 static struct json *ovsdb_jsonrpc_monitor_get_initial(
929 const struct ovsdb_jsonrpc_monitor *);
932 parse_bool(struct ovsdb_parser *parser, const char *name, bool default_value)
934 const struct json *json;
936 json = ovsdb_parser_member(parser, name, OP_BOOLEAN | OP_OPTIONAL);
937 return json ? json_boolean(json) : default_value;
940 struct ovsdb_jsonrpc_monitor *
941 ovsdb_jsonrpc_monitor_find(struct ovsdb_jsonrpc_session *s,
942 const struct json *monitor_id)
944 struct ovsdb_jsonrpc_monitor *m;
946 HMAP_FOR_EACH_WITH_HASH (m, node, json_hash(monitor_id, 0), &s->monitors) {
947 if (json_equal(m->monitor_id, monitor_id)) {
956 ovsdb_jsonrpc_add_monitor_column(struct ovsdb_jsonrpc_monitor_table *mt,
957 const struct ovsdb_column *column,
958 enum ovsdb_jsonrpc_monitor_selection select,
959 size_t *allocated_columns)
961 struct ovsdb_jsonrpc_monitor_column *c;
963 if (mt->n_columns >= *allocated_columns) {
964 mt->columns = x2nrealloc(mt->columns, allocated_columns,
965 sizeof *mt->columns);
968 c = &mt->columns[mt->n_columns++];
974 compare_ovsdb_jsonrpc_monitor_column(const void *a_, const void *b_)
976 const struct ovsdb_jsonrpc_monitor_column *a = a_;
977 const struct ovsdb_jsonrpc_monitor_column *b = b_;
979 return a->column < b->column ? -1 : a->column > b->column;
982 static struct ovsdb_error * WARN_UNUSED_RESULT
983 ovsdb_jsonrpc_parse_monitor_request(struct ovsdb_jsonrpc_monitor_table *mt,
984 const struct json *monitor_request,
985 size_t *allocated_columns)
987 const struct ovsdb_table_schema *ts = mt->table->schema;
988 enum ovsdb_jsonrpc_monitor_selection select;
989 const struct json *columns, *select_json;
990 struct ovsdb_parser parser;
991 struct ovsdb_error *error;
993 ovsdb_parser_init(&parser, monitor_request, "table %s", ts->name);
994 columns = ovsdb_parser_member(&parser, "columns", OP_ARRAY | OP_OPTIONAL);
995 select_json = ovsdb_parser_member(&parser, "select",
996 OP_OBJECT | OP_OPTIONAL);
997 error = ovsdb_parser_finish(&parser);
1004 ovsdb_parser_init(&parser, select_json, "table %s select", ts->name);
1005 if (parse_bool(&parser, "initial", true)) {
1006 select |= OJMS_INITIAL;
1008 if (parse_bool(&parser, "insert", true)) {
1009 select |= OJMS_INSERT;
1011 if (parse_bool(&parser, "delete", true)) {
1012 select |= OJMS_DELETE;
1014 if (parse_bool(&parser, "modify", true)) {
1015 select |= OJMS_MODIFY;
1017 error = ovsdb_parser_finish(&parser);
1022 select = OJMS_INITIAL | OJMS_INSERT | OJMS_DELETE | OJMS_MODIFY;
1024 mt->select |= select;
1029 if (columns->type != JSON_ARRAY) {
1030 return ovsdb_syntax_error(columns, NULL,
1031 "array of column names expected");
1034 for (i = 0; i < columns->u.array.n; i++) {
1035 const struct ovsdb_column *column;
1038 if (columns->u.array.elems[i]->type != JSON_STRING) {
1039 return ovsdb_syntax_error(columns, NULL,
1040 "array of column names expected");
1043 s = columns->u.array.elems[i]->u.string;
1044 column = shash_find_data(&mt->table->schema->columns, s);
1046 return ovsdb_syntax_error(columns, NULL, "%s is not a valid "
1049 ovsdb_jsonrpc_add_monitor_column(mt, column, select,
1053 struct shash_node *node;
1055 SHASH_FOR_EACH (node, &ts->columns) {
1056 const struct ovsdb_column *column = node->data;
1057 if (column->index != OVSDB_COL_UUID) {
1058 ovsdb_jsonrpc_add_monitor_column(mt, column, select,
1067 static struct json *
1068 ovsdb_jsonrpc_monitor_create(struct ovsdb_jsonrpc_session *s,
1069 struct json *params)
1071 struct ovsdb_jsonrpc_monitor *m = NULL;
1072 struct json *monitor_id, *monitor_requests;
1073 struct ovsdb_error *error = NULL;
1074 struct shash_node *node;
1077 if (json_array(params)->n != 3) {
1078 error = ovsdb_syntax_error(params, NULL, "invalid parameters");
1081 monitor_id = params->u.array.elems[1];
1082 monitor_requests = params->u.array.elems[2];
1083 if (monitor_requests->type != JSON_OBJECT) {
1084 error = ovsdb_syntax_error(monitor_requests, NULL,
1085 "monitor-requests must be object");
1089 if (ovsdb_jsonrpc_monitor_find(s, monitor_id)) {
1090 error = ovsdb_syntax_error(monitor_id, NULL, "duplicate monitor ID");
1094 m = xzalloc(sizeof *m);
1095 ovsdb_replica_init(&m->replica, &ovsdb_jsonrpc_replica_class);
1096 ovsdb_add_replica(s->remote->server->up.db, &m->replica);
1098 hmap_insert(&s->monitors, &m->node, json_hash(monitor_id, 0));
1099 m->monitor_id = json_clone(monitor_id);
1100 shash_init(&m->tables);
1102 SHASH_FOR_EACH (node, json_object(monitor_requests)) {
1103 const struct ovsdb_table *table;
1104 struct ovsdb_jsonrpc_monitor_table *mt;
1105 size_t allocated_columns;
1106 const struct json *mr_value;
1109 table = ovsdb_get_table(s->remote->server->up.db, node->name);
1111 error = ovsdb_syntax_error(NULL, NULL,
1112 "no table named %s", node->name);
1116 mt = xzalloc(sizeof *mt);
1118 shash_add(&m->tables, table->schema->name, mt);
1120 /* Parse columns. */
1121 mr_value = node->data;
1122 allocated_columns = 0;
1123 if (mr_value->type == JSON_ARRAY) {
1124 const struct json_array *array = &mr_value->u.array;
1126 for (i = 0; i < array->n; i++) {
1127 error = ovsdb_jsonrpc_parse_monitor_request(
1128 mt, array->elems[i], &allocated_columns);
1134 error = ovsdb_jsonrpc_parse_monitor_request(
1135 mt, mr_value, &allocated_columns);
1141 /* Check for duplicate columns. */
1142 qsort(mt->columns, mt->n_columns, sizeof *mt->columns,
1143 compare_ovsdb_jsonrpc_monitor_column);
1144 for (i = 1; i < mt->n_columns; i++) {
1145 if (mt->columns[i].column == mt->columns[i - 1].column) {
1146 error = ovsdb_syntax_error(mr_value, NULL, "column %s "
1147 "mentioned more than once",
1148 mt->columns[i].column->name);
1154 return ovsdb_jsonrpc_monitor_get_initial(m);
1158 ovsdb_remove_replica(s->remote->server->up.db, &m->replica);
1161 json = ovsdb_error_to_json(error);
1162 ovsdb_error_destroy(error);
1166 static struct jsonrpc_msg *
1167 ovsdb_jsonrpc_monitor_cancel(struct ovsdb_jsonrpc_session *s,
1168 struct json_array *params,
1169 const struct json *request_id)
1171 if (params->n != 1) {
1172 return jsonrpc_create_error(json_string_create("invalid parameters"),
1175 struct ovsdb_jsonrpc_monitor *m;
1177 m = ovsdb_jsonrpc_monitor_find(s, params->elems[0]);
1179 return jsonrpc_create_error(json_string_create("unknown monitor"),
1182 ovsdb_remove_replica(s->remote->server->up.db, &m->replica);
1183 return jsonrpc_create_reply(json_object_create(), request_id);
1189 ovsdb_jsonrpc_monitor_remove_all(struct ovsdb_jsonrpc_session *s)
1191 struct ovsdb_jsonrpc_monitor *m, *next;
1193 HMAP_FOR_EACH_SAFE (m, next, node, &s->monitors) {
1194 ovsdb_remove_replica(s->remote->server->up.db, &m->replica);
1198 static struct ovsdb_jsonrpc_monitor *
1199 ovsdb_jsonrpc_monitor_cast(struct ovsdb_replica *replica)
1201 assert(replica->class == &ovsdb_jsonrpc_replica_class);
1202 return CONTAINER_OF(replica, struct ovsdb_jsonrpc_monitor, replica);
1205 struct ovsdb_jsonrpc_monitor_aux {
1206 bool initial; /* Sending initial contents of table? */
1207 const struct ovsdb_jsonrpc_monitor *monitor;
1208 struct json *json; /* JSON for the whole transaction. */
1210 /* Current table. */
1211 struct ovsdb_jsonrpc_monitor_table *mt;
1212 struct json *table_json; /* JSON for table's transaction. */
1216 any_reportable_change(const struct ovsdb_jsonrpc_monitor_table *mt,
1217 const unsigned long int *changed)
1221 for (i = 0; i < mt->n_columns; i++) {
1222 const struct ovsdb_jsonrpc_monitor_column *c = &mt->columns[i];
1223 unsigned int idx = c->column->index;
1225 if (c->select & OJMS_MODIFY && bitmap_is_set(changed, idx)) {
1234 ovsdb_jsonrpc_monitor_change_cb(const struct ovsdb_row *old,
1235 const struct ovsdb_row *new,
1236 const unsigned long int *changed,
1239 struct ovsdb_jsonrpc_monitor_aux *aux = aux_;
1240 const struct ovsdb_jsonrpc_monitor *m = aux->monitor;
1241 struct ovsdb_table *table = new ? new->table : old->table;
1242 enum ovsdb_jsonrpc_monitor_selection type;
1243 struct json *old_json, *new_json;
1244 struct json *row_json;
1245 char uuid[UUID_LEN + 1];
1248 if (!aux->mt || table != aux->mt->table) {
1249 aux->mt = shash_find_data(&m->tables, table->schema->name);
1250 aux->table_json = NULL;
1252 /* We don't care about rows in this table at all. Tell the caller
1258 type = (aux->initial ? OJMS_INITIAL
1259 : !old ? OJMS_INSERT
1260 : !new ? OJMS_DELETE
1262 if (!(aux->mt->select & type)) {
1263 /* We don't care about this type of change (but do want to be called
1264 * back for changes to other rows in the same table). */
1268 if (type == OJMS_MODIFY && !any_reportable_change(aux->mt, changed)) {
1269 /* Nothing of interest changed. */
1273 old_json = new_json = NULL;
1274 if (type & (OJMS_DELETE | OJMS_MODIFY)) {
1275 old_json = json_object_create();
1277 if (type & (OJMS_INITIAL | OJMS_INSERT | OJMS_MODIFY)) {
1278 new_json = json_object_create();
1280 for (i = 0; i < aux->mt->n_columns; i++) {
1281 const struct ovsdb_jsonrpc_monitor_column *c = &aux->mt->columns[i];
1282 const struct ovsdb_column *column = c->column;
1283 unsigned int idx = c->column->index;
1285 if (!(type & c->select)) {
1286 /* We don't care about this type of change for this particular
1287 * column (but we will care about it for some other column). */
1291 if ((type == OJMS_MODIFY && bitmap_is_set(changed, idx))
1292 || type == OJMS_DELETE) {
1293 json_object_put(old_json, column->name,
1294 ovsdb_datum_to_json(&old->fields[idx],
1297 if (type & (OJMS_INITIAL | OJMS_INSERT | OJMS_MODIFY)) {
1298 json_object_put(new_json, column->name,
1299 ovsdb_datum_to_json(&new->fields[idx],
1304 /* Create JSON object for transaction overall. */
1306 aux->json = json_object_create();
1309 /* Create JSON object for transaction on this table. */
1310 if (!aux->table_json) {
1311 aux->table_json = json_object_create();
1312 json_object_put(aux->json, aux->mt->table->schema->name,
1316 /* Create JSON object for transaction on this row. */
1317 row_json = json_object_create();
1319 json_object_put(row_json, "old", old_json);
1322 json_object_put(row_json, "new", new_json);
1325 /* Add JSON row to JSON table. */
1326 snprintf(uuid, sizeof uuid,
1327 UUID_FMT, UUID_ARGS(ovsdb_row_get_uuid(new ? new : old)));
1328 json_object_put(aux->table_json, uuid, row_json);
1334 ovsdb_jsonrpc_monitor_init_aux(struct ovsdb_jsonrpc_monitor_aux *aux,
1335 const struct ovsdb_jsonrpc_monitor *m,
1338 aux->initial = initial;
1342 aux->table_json = NULL;
1345 static struct ovsdb_error *
1346 ovsdb_jsonrpc_monitor_commit(struct ovsdb_replica *replica,
1347 const struct ovsdb_txn *txn,
1348 bool durable OVS_UNUSED)
1350 struct ovsdb_jsonrpc_monitor *m = ovsdb_jsonrpc_monitor_cast(replica);
1351 struct ovsdb_jsonrpc_monitor_aux aux;
1353 ovsdb_jsonrpc_monitor_init_aux(&aux, m, false);
1354 ovsdb_txn_for_each_change(txn, ovsdb_jsonrpc_monitor_change_cb, &aux);
1356 struct jsonrpc_msg *msg;
1357 struct json *params;
1359 params = json_array_create_2(json_clone(aux.monitor->monitor_id),
1361 msg = jsonrpc_create_notify("update", params);
1362 jsonrpc_session_send(aux.monitor->session->js, msg);
1368 static struct json *
1369 ovsdb_jsonrpc_monitor_get_initial(const struct ovsdb_jsonrpc_monitor *m)
1371 struct ovsdb_jsonrpc_monitor_aux aux;
1372 struct shash_node *node;
1374 ovsdb_jsonrpc_monitor_init_aux(&aux, m, true);
1375 SHASH_FOR_EACH (node, &m->tables) {
1376 struct ovsdb_jsonrpc_monitor_table *mt = node->data;
1378 if (mt->select & OJMS_INITIAL) {
1379 struct ovsdb_row *row;
1381 HMAP_FOR_EACH (row, hmap_node, &mt->table->rows) {
1382 ovsdb_jsonrpc_monitor_change_cb(NULL, row, NULL, &aux);
1386 return aux.json ? aux.json : json_object_create();
1390 ovsdb_jsonrpc_monitor_destroy(struct ovsdb_replica *replica)
1392 struct ovsdb_jsonrpc_monitor *m = ovsdb_jsonrpc_monitor_cast(replica);
1393 struct shash_node *node;
1395 json_destroy(m->monitor_id);
1396 SHASH_FOR_EACH (node, &m->tables) {
1397 struct ovsdb_jsonrpc_monitor_table *mt = node->data;
1401 shash_destroy(&m->tables);
1402 hmap_remove(&m->session->monitors, &m->node);
1406 static const struct ovsdb_replica_class ovsdb_jsonrpc_replica_class = {
1407 ovsdb_jsonrpc_monitor_commit,
1408 ovsdb_jsonrpc_monitor_destroy