1 /* Copyright (c) 2009, 2010 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"
27 #include "ovsdb-error.h"
28 #include "ovsdb-parser.h"
30 #include "reconnect.h"
35 #include "transaction.h"
39 VLOG_DEFINE_THIS_MODULE(ovsdb_jsonrpc_server);
41 struct ovsdb_jsonrpc_remote;
42 struct ovsdb_jsonrpc_session;
44 /* Message rate-limiting. */
45 struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
48 static struct ovsdb_jsonrpc_session *ovsdb_jsonrpc_session_create(
49 struct ovsdb_jsonrpc_remote *, struct jsonrpc_session *);
50 static void ovsdb_jsonrpc_session_run_all(struct ovsdb_jsonrpc_remote *);
51 static void ovsdb_jsonrpc_session_wait_all(struct ovsdb_jsonrpc_remote *);
52 static void ovsdb_jsonrpc_session_close_all(struct ovsdb_jsonrpc_remote *);
53 static void ovsdb_jsonrpc_session_reconnect_all(struct ovsdb_jsonrpc_remote *);
54 static void ovsdb_jsonrpc_session_set_all_options(
55 struct ovsdb_jsonrpc_remote *, const struct ovsdb_jsonrpc_options *);
56 static void ovsdb_jsonrpc_session_get_status(
57 const struct ovsdb_jsonrpc_remote *,
61 static void ovsdb_jsonrpc_trigger_create(struct ovsdb_jsonrpc_session *,
62 struct json *id, struct json *params);
63 static struct ovsdb_jsonrpc_trigger *ovsdb_jsonrpc_trigger_find(
64 struct ovsdb_jsonrpc_session *, const struct json *id, size_t hash);
65 static void ovsdb_jsonrpc_trigger_complete(struct ovsdb_jsonrpc_trigger *);
66 static void ovsdb_jsonrpc_trigger_complete_all(struct ovsdb_jsonrpc_session *);
67 static void ovsdb_jsonrpc_trigger_complete_done(
68 struct ovsdb_jsonrpc_session *);
71 static struct json *ovsdb_jsonrpc_monitor_create(
72 struct ovsdb_jsonrpc_session *, struct json *params);
73 static struct jsonrpc_msg *ovsdb_jsonrpc_monitor_cancel(
74 struct ovsdb_jsonrpc_session *,
75 struct json_array *params,
76 const struct json *request_id);
77 static void ovsdb_jsonrpc_monitor_remove_all(struct ovsdb_jsonrpc_session *);
79 /* JSON-RPC database server. */
81 struct ovsdb_jsonrpc_server {
83 unsigned int n_sessions, max_sessions;
84 struct shash remotes; /* Contains "struct ovsdb_jsonrpc_remote *"s. */
87 /* A configured remote. This is either a passive stream listener plus a list
88 * of the currently connected sessions, or a list of exactly one active
90 struct ovsdb_jsonrpc_remote {
91 struct ovsdb_jsonrpc_server *server;
92 struct pstream *listener; /* Listener, if passive. */
93 struct list sessions; /* List of "struct ovsdb_jsonrpc_session"s. */
96 static struct ovsdb_jsonrpc_remote *ovsdb_jsonrpc_server_add_remote(
97 struct ovsdb_jsonrpc_server *, const char *name);
98 static void ovsdb_jsonrpc_server_del_remote(struct shash_node *);
100 struct ovsdb_jsonrpc_server *
101 ovsdb_jsonrpc_server_create(struct ovsdb *db)
103 struct ovsdb_jsonrpc_server *server = xzalloc(sizeof *server);
105 server->max_sessions = 64;
106 shash_init(&server->remotes);
111 ovsdb_jsonrpc_server_destroy(struct ovsdb_jsonrpc_server *svr)
113 struct shash_node *node, *next;
115 SHASH_FOR_EACH_SAFE (node, next, &svr->remotes) {
116 ovsdb_jsonrpc_server_del_remote(node);
118 shash_destroy(&svr->remotes);
122 struct ovsdb_jsonrpc_options *
123 ovsdb_jsonrpc_default_options(void)
125 struct ovsdb_jsonrpc_options *options = xzalloc(sizeof *options);
126 options->probe_interval = RECONNECT_DEFAULT_PROBE_INTERVAL;
127 options->max_backoff = RECONNECT_DEFAULT_MAX_BACKOFF;
131 /* Sets 'svr''s current set of remotes to the names in 'new_remotes', with
132 * options in the struct ovsdb_jsonrpc_options supplied as the data values.
134 * A remote is an active or passive stream connection method, e.g. "pssl:" or
137 ovsdb_jsonrpc_server_set_remotes(struct ovsdb_jsonrpc_server *svr,
138 const struct shash *new_remotes)
140 struct shash_node *node, *next;
142 SHASH_FOR_EACH_SAFE (node, next, &svr->remotes) {
143 if (!shash_find(new_remotes, node->name)) {
144 ovsdb_jsonrpc_server_del_remote(node);
147 SHASH_FOR_EACH (node, new_remotes) {
148 const struct ovsdb_jsonrpc_options *options = node->data;
149 struct ovsdb_jsonrpc_remote *remote;
151 remote = shash_find_data(&svr->remotes, node->name);
153 remote = ovsdb_jsonrpc_server_add_remote(svr, node->name);
159 ovsdb_jsonrpc_session_set_all_options(remote, options);
163 static struct ovsdb_jsonrpc_remote *
164 ovsdb_jsonrpc_server_add_remote(struct ovsdb_jsonrpc_server *svr,
167 struct ovsdb_jsonrpc_remote *remote;
168 struct pstream *listener;
171 error = jsonrpc_pstream_open(name, &listener);
172 if (error && error != EAFNOSUPPORT) {
173 VLOG_ERR_RL(&rl, "%s: listen failed: %s", name, strerror(error));
177 remote = xmalloc(sizeof *remote);
178 remote->server = svr;
179 remote->listener = listener;
180 list_init(&remote->sessions);
181 shash_add(&svr->remotes, name, remote);
184 ovsdb_jsonrpc_session_create(remote, jsonrpc_session_open(name));
190 ovsdb_jsonrpc_server_del_remote(struct shash_node *node)
192 struct ovsdb_jsonrpc_remote *remote = node->data;
194 ovsdb_jsonrpc_session_close_all(remote);
195 pstream_close(remote->listener);
196 shash_delete(&remote->server->remotes, node);
201 ovsdb_jsonrpc_server_get_remote_status(const struct ovsdb_jsonrpc_server *svr,
202 struct shash *statuses)
204 struct shash_node *node;
206 shash_init(statuses);
208 SHASH_FOR_EACH (node, &svr->remotes) {
209 const struct ovsdb_jsonrpc_remote *remote = node->data;
211 ovsdb_jsonrpc_session_get_status(remote, statuses);
215 /* Forces all of the JSON-RPC sessions managed by 'svr' to disconnect and
218 ovsdb_jsonrpc_server_reconnect(struct ovsdb_jsonrpc_server *svr)
220 struct shash_node *node;
222 SHASH_FOR_EACH (node, &svr->remotes) {
223 struct ovsdb_jsonrpc_remote *remote = node->data;
225 ovsdb_jsonrpc_session_reconnect_all(remote);
230 ovsdb_jsonrpc_server_run(struct ovsdb_jsonrpc_server *svr)
232 struct shash_node *node;
234 SHASH_FOR_EACH (node, &svr->remotes) {
235 struct ovsdb_jsonrpc_remote *remote = node->data;
237 if (remote->listener && svr->n_sessions < svr->max_sessions) {
238 struct stream *stream;
241 error = pstream_accept(remote->listener, &stream);
243 struct jsonrpc_session *js;
244 js = jsonrpc_session_open_unreliably(jsonrpc_open(stream));
245 ovsdb_jsonrpc_session_create(remote, js);
246 } else if (error != EAGAIN) {
247 VLOG_WARN_RL(&rl, "%s: accept failed: %s",
248 pstream_get_name(remote->listener),
253 ovsdb_jsonrpc_session_run_all(remote);
258 ovsdb_jsonrpc_server_wait(struct ovsdb_jsonrpc_server *svr)
260 struct shash_node *node;
262 SHASH_FOR_EACH (node, &svr->remotes) {
263 struct ovsdb_jsonrpc_remote *remote = node->data;
265 if (remote->listener && svr->n_sessions < svr->max_sessions) {
266 pstream_wait(remote->listener);
269 ovsdb_jsonrpc_session_wait_all(remote);
273 /* JSON-RPC database server session. */
275 struct ovsdb_jsonrpc_session {
276 struct ovsdb_jsonrpc_remote *remote;
277 struct list node; /* Element in remote's sessions list. */
280 struct hmap triggers; /* Hmap of "struct ovsdb_jsonrpc_trigger"s. */
281 struct list completions; /* Completed triggers. */
284 struct hmap monitors; /* Hmap of "struct ovsdb_jsonrpc_monitor"s. */
286 /* Network connectivity. */
287 struct jsonrpc_session *js; /* JSON-RPC session. */
288 unsigned int js_seqno; /* Last jsonrpc_session_get_seqno() value. */
291 static void ovsdb_jsonrpc_session_close(struct ovsdb_jsonrpc_session *);
292 static int ovsdb_jsonrpc_session_run(struct ovsdb_jsonrpc_session *);
293 static void ovsdb_jsonrpc_session_wait(struct ovsdb_jsonrpc_session *);
294 static void ovsdb_jsonrpc_session_set_options(
295 struct ovsdb_jsonrpc_session *, const struct ovsdb_jsonrpc_options *);
296 static void ovsdb_jsonrpc_session_got_request(struct ovsdb_jsonrpc_session *,
297 struct jsonrpc_msg *);
298 static void ovsdb_jsonrpc_session_got_notify(struct ovsdb_jsonrpc_session *,
299 struct jsonrpc_msg *);
301 static struct ovsdb_jsonrpc_session *
302 ovsdb_jsonrpc_session_create(struct ovsdb_jsonrpc_remote *remote,
303 struct jsonrpc_session *js)
305 struct ovsdb_jsonrpc_session *s;
307 s = xzalloc(sizeof *s);
309 list_push_back(&remote->sessions, &s->node);
310 hmap_init(&s->triggers);
311 hmap_init(&s->monitors);
312 list_init(&s->completions);
314 s->js_seqno = jsonrpc_session_get_seqno(js);
316 remote->server->n_sessions++;
322 ovsdb_jsonrpc_session_close(struct ovsdb_jsonrpc_session *s)
324 ovsdb_jsonrpc_monitor_remove_all(s);
325 jsonrpc_session_close(s->js);
326 list_remove(&s->node);
327 s->remote->server->n_sessions--;
332 ovsdb_jsonrpc_session_run(struct ovsdb_jsonrpc_session *s)
334 jsonrpc_session_run(s->js);
335 if (s->js_seqno != jsonrpc_session_get_seqno(s->js)) {
336 s->js_seqno = jsonrpc_session_get_seqno(s->js);
337 ovsdb_jsonrpc_trigger_complete_all(s);
338 ovsdb_jsonrpc_monitor_remove_all(s);
341 ovsdb_jsonrpc_trigger_complete_done(s);
343 if (!jsonrpc_session_get_backlog(s->js)) {
344 struct jsonrpc_msg *msg = jsonrpc_session_recv(s->js);
346 if (msg->type == JSONRPC_REQUEST) {
347 ovsdb_jsonrpc_session_got_request(s, msg);
348 } else if (msg->type == JSONRPC_NOTIFY) {
349 ovsdb_jsonrpc_session_got_notify(s, msg);
351 VLOG_WARN("%s: received unexpected %s message",
352 jsonrpc_session_get_name(s->js),
353 jsonrpc_msg_type_to_string(msg->type));
354 jsonrpc_session_force_reconnect(s->js);
355 jsonrpc_msg_destroy(msg);
359 return jsonrpc_session_is_alive(s->js) ? 0 : ETIMEDOUT;
363 ovsdb_jsonrpc_session_set_options(struct ovsdb_jsonrpc_session *session,
364 const struct ovsdb_jsonrpc_options *options)
366 jsonrpc_session_set_max_backoff(session->js, options->max_backoff);
367 jsonrpc_session_set_probe_interval(session->js, options->probe_interval);
371 ovsdb_jsonrpc_session_run_all(struct ovsdb_jsonrpc_remote *remote)
373 struct ovsdb_jsonrpc_session *s, *next;
375 LIST_FOR_EACH_SAFE (s, next, node, &remote->sessions) {
376 int error = ovsdb_jsonrpc_session_run(s);
378 ovsdb_jsonrpc_session_close(s);
384 ovsdb_jsonrpc_session_wait(struct ovsdb_jsonrpc_session *s)
386 jsonrpc_session_wait(s->js);
387 if (!jsonrpc_session_get_backlog(s->js)) {
388 jsonrpc_session_recv_wait(s->js);
393 ovsdb_jsonrpc_session_wait_all(struct ovsdb_jsonrpc_remote *remote)
395 struct ovsdb_jsonrpc_session *s;
397 LIST_FOR_EACH (s, node, &remote->sessions) {
398 ovsdb_jsonrpc_session_wait(s);
403 ovsdb_jsonrpc_session_close_all(struct ovsdb_jsonrpc_remote *remote)
405 struct ovsdb_jsonrpc_session *s, *next;
407 LIST_FOR_EACH_SAFE (s, next, node, &remote->sessions) {
408 ovsdb_jsonrpc_session_close(s);
412 /* Forces all of the JSON-RPC sessions managed by 'remote' to disconnect and
415 ovsdb_jsonrpc_session_reconnect_all(struct ovsdb_jsonrpc_remote *remote)
417 struct ovsdb_jsonrpc_session *s, *next;
419 LIST_FOR_EACH_SAFE (s, next, node, &remote->sessions) {
420 jsonrpc_session_force_reconnect(s->js);
421 if (!jsonrpc_session_is_alive(s->js)) {
422 ovsdb_jsonrpc_session_close(s);
427 /* Sets the options for all of the JSON-RPC sessions managed by 'remote' to
430 ovsdb_jsonrpc_session_set_all_options(
431 struct ovsdb_jsonrpc_remote *remote,
432 const struct ovsdb_jsonrpc_options *options)
434 struct ovsdb_jsonrpc_session *s;
436 LIST_FOR_EACH (s, node, &remote->sessions) {
437 ovsdb_jsonrpc_session_set_options(s, options);
442 ovsdb_jsonrpc_session_get_status(const struct ovsdb_jsonrpc_remote *remote,
445 const struct ovsdb_jsonrpc_session *s;
446 const struct jsonrpc_session *js;
448 struct ovsdb_jsonrpc_remote_status *status;
449 struct reconnect_stats rstats;
451 /* We only look at the first session in the list. There should be only one
452 * node in the list for outbound connections. We don't track status for
453 * each individual inbound connection if someone configures the DB that
454 * way. Since outbound connections are the norm, this is fine. */
455 if (list_is_empty(&remote->sessions)) {
458 s = CONTAINER_OF(remote->sessions.next, struct ovsdb_jsonrpc_session, node);
463 name = jsonrpc_session_get_name(js);
465 status = xzalloc(sizeof *status);
466 shash_add(shash, name, status);
468 status->is_connected = jsonrpc_session_is_connected(js);
469 status->last_error = jsonrpc_session_get_status(js);
471 jsonrpc_session_get_reconnect_stats(js, &rstats);
472 status->state = rstats.state;
473 status->conn_secs = (rstats.is_connected
474 ? rstats.current_connection_duration
475 : rstats.current_disconnect_duration
482 get_db_name(const struct ovsdb_jsonrpc_session *s)
484 return s->remote->server->db->schema->name;
487 static struct jsonrpc_msg *
488 ovsdb_jsonrpc_check_db_name(const struct ovsdb_jsonrpc_session *s,
489 const struct jsonrpc_msg *request)
491 struct json_array *params;
492 const char *want_db_name;
493 const char *have_db_name;
494 struct ovsdb_error *error;
495 struct jsonrpc_msg *reply;
497 params = json_array(request->params);
498 if (!params->n || params->elems[0]->type != JSON_STRING) {
499 error = ovsdb_syntax_error(
500 request->params, NULL,
501 "%s request params must begin with <db-name>", request->method);
505 want_db_name = params->elems[0]->u.string;
506 have_db_name = get_db_name(s);
507 if (strcmp(want_db_name, have_db_name)) {
508 error = ovsdb_syntax_error(
509 request->params, "unknown database",
510 "%s request specifies unknown database %s",
511 request->method, want_db_name);
518 reply = jsonrpc_create_reply(ovsdb_error_to_json(error), request->id);
519 ovsdb_error_destroy(error);
523 static struct jsonrpc_msg *
524 execute_transaction(struct ovsdb_jsonrpc_session *s,
525 struct jsonrpc_msg *request)
527 ovsdb_jsonrpc_trigger_create(s, request->id, request->params);
529 request->params = NULL;
530 jsonrpc_msg_destroy(request);
535 ovsdb_jsonrpc_session_got_request(struct ovsdb_jsonrpc_session *s,
536 struct jsonrpc_msg *request)
538 struct jsonrpc_msg *reply;
540 if (!strcmp(request->method, "transact")) {
541 reply = ovsdb_jsonrpc_check_db_name(s, request);
543 reply = execute_transaction(s, request);
545 } else if (!strcmp(request->method, "monitor")) {
546 reply = ovsdb_jsonrpc_check_db_name(s, request);
548 reply = jsonrpc_create_reply(
549 ovsdb_jsonrpc_monitor_create(s, request->params), request->id);
551 } else if (!strcmp(request->method, "monitor_cancel")) {
552 reply = ovsdb_jsonrpc_monitor_cancel(s, json_array(request->params),
554 } else if (!strcmp(request->method, "get_schema")) {
555 reply = ovsdb_jsonrpc_check_db_name(s, request);
557 reply = jsonrpc_create_reply(
558 ovsdb_schema_to_json(s->remote->server->db->schema),
561 } else if (!strcmp(request->method, "list_dbs")) {
562 reply = jsonrpc_create_reply(
563 json_array_create_1(json_string_create(get_db_name(s))),
565 } else if (!strcmp(request->method, "echo")) {
566 reply = jsonrpc_create_reply(json_clone(request->params), request->id);
568 reply = jsonrpc_create_error(json_string_create("unknown method"),
573 jsonrpc_msg_destroy(request);
574 jsonrpc_session_send(s->js, reply);
579 execute_cancel(struct ovsdb_jsonrpc_session *s, struct jsonrpc_msg *request)
581 if (json_array(request->params)->n == 1) {
582 struct ovsdb_jsonrpc_trigger *t;
585 id = request->params->u.array.elems[0];
586 t = ovsdb_jsonrpc_trigger_find(s, id, json_hash(id, 0));
588 ovsdb_jsonrpc_trigger_complete(t);
594 ovsdb_jsonrpc_session_got_notify(struct ovsdb_jsonrpc_session *s,
595 struct jsonrpc_msg *request)
597 if (!strcmp(request->method, "cancel")) {
598 execute_cancel(s, request);
600 jsonrpc_msg_destroy(request);
603 /* JSON-RPC database server triggers.
605 * (Every transaction is treated as a trigger even if it doesn't actually have
606 * any "wait" operations.) */
608 struct ovsdb_jsonrpc_trigger {
609 struct ovsdb_trigger trigger;
610 struct ovsdb_jsonrpc_session *session;
611 struct hmap_node hmap_node; /* In session's "triggers" hmap. */
616 ovsdb_jsonrpc_trigger_create(struct ovsdb_jsonrpc_session *s,
617 struct json *id, struct json *params)
619 struct ovsdb_jsonrpc_trigger *t;
622 /* Check for duplicate ID. */
623 hash = json_hash(id, 0);
624 t = ovsdb_jsonrpc_trigger_find(s, id, hash);
626 struct jsonrpc_msg *msg;
628 msg = jsonrpc_create_error(json_string_create("duplicate request ID"),
630 jsonrpc_session_send(s->js, msg);
632 json_destroy(params);
636 /* Insert into trigger table. */
637 t = xmalloc(sizeof *t);
638 ovsdb_trigger_init(s->remote->server->db,
639 &t->trigger, params, &s->completions,
643 hmap_insert(&s->triggers, &t->hmap_node, hash);
645 /* Complete early if possible. */
646 if (ovsdb_trigger_is_complete(&t->trigger)) {
647 ovsdb_jsonrpc_trigger_complete(t);
651 static struct ovsdb_jsonrpc_trigger *
652 ovsdb_jsonrpc_trigger_find(struct ovsdb_jsonrpc_session *s,
653 const struct json *id, size_t hash)
655 struct ovsdb_jsonrpc_trigger *t;
657 HMAP_FOR_EACH_WITH_HASH (t, hmap_node, hash, &s->triggers) {
658 if (json_equal(t->id, id)) {
667 ovsdb_jsonrpc_trigger_complete(struct ovsdb_jsonrpc_trigger *t)
669 struct ovsdb_jsonrpc_session *s = t->session;
671 if (jsonrpc_session_is_connected(s->js)) {
672 struct jsonrpc_msg *reply;
675 result = ovsdb_trigger_steal_result(&t->trigger);
677 reply = jsonrpc_create_reply(result, t->id);
679 reply = jsonrpc_create_error(json_string_create("canceled"),
682 jsonrpc_session_send(s->js, reply);
686 ovsdb_trigger_destroy(&t->trigger);
687 hmap_remove(&s->triggers, &t->hmap_node);
692 ovsdb_jsonrpc_trigger_complete_all(struct ovsdb_jsonrpc_session *s)
694 struct ovsdb_jsonrpc_trigger *t, *next;
695 HMAP_FOR_EACH_SAFE (t, next, hmap_node, &s->triggers) {
696 ovsdb_jsonrpc_trigger_complete(t);
701 ovsdb_jsonrpc_trigger_complete_done(struct ovsdb_jsonrpc_session *s)
703 while (!list_is_empty(&s->completions)) {
704 struct ovsdb_jsonrpc_trigger *t
705 = CONTAINER_OF(s->completions.next,
706 struct ovsdb_jsonrpc_trigger, trigger.node);
707 ovsdb_jsonrpc_trigger_complete(t);
711 /* JSON-RPC database table monitors. */
713 enum ovsdb_jsonrpc_monitor_selection {
714 OJMS_INITIAL = 1 << 0, /* All rows when monitor is created. */
715 OJMS_INSERT = 1 << 1, /* New rows. */
716 OJMS_DELETE = 1 << 2, /* Deleted rows. */
717 OJMS_MODIFY = 1 << 3 /* Modified rows. */
720 /* A particular column being monitored. */
721 struct ovsdb_jsonrpc_monitor_column {
722 const struct ovsdb_column *column;
723 enum ovsdb_jsonrpc_monitor_selection select;
726 /* A particular table being monitored. */
727 struct ovsdb_jsonrpc_monitor_table {
728 const struct ovsdb_table *table;
730 /* This is the union (bitwise-OR) of the 'select' values in all of the
731 * members of 'columns' below. */
732 enum ovsdb_jsonrpc_monitor_selection select;
734 /* Columns being monitored. */
735 struct ovsdb_jsonrpc_monitor_column *columns;
739 /* A collection of tables being monitored. */
740 struct ovsdb_jsonrpc_monitor {
741 struct ovsdb_replica replica;
742 struct ovsdb_jsonrpc_session *session;
743 struct hmap_node node; /* In ovsdb_jsonrpc_session's "monitors". */
745 struct json *monitor_id;
746 struct shash tables; /* Holds "struct ovsdb_jsonrpc_monitor_table"s. */
749 static const struct ovsdb_replica_class ovsdb_jsonrpc_replica_class;
751 struct ovsdb_jsonrpc_monitor *ovsdb_jsonrpc_monitor_find(
752 struct ovsdb_jsonrpc_session *, const struct json *monitor_id);
753 static void ovsdb_jsonrpc_monitor_destroy(struct ovsdb_replica *);
754 static struct json *ovsdb_jsonrpc_monitor_get_initial(
755 const struct ovsdb_jsonrpc_monitor *);
758 parse_bool(struct ovsdb_parser *parser, const char *name, bool default_value)
760 const struct json *json;
762 json = ovsdb_parser_member(parser, name, OP_BOOLEAN | OP_OPTIONAL);
763 return json ? json_boolean(json) : default_value;
766 struct ovsdb_jsonrpc_monitor *
767 ovsdb_jsonrpc_monitor_find(struct ovsdb_jsonrpc_session *s,
768 const struct json *monitor_id)
770 struct ovsdb_jsonrpc_monitor *m;
772 HMAP_FOR_EACH_WITH_HASH (m, node, json_hash(monitor_id, 0), &s->monitors) {
773 if (json_equal(m->monitor_id, monitor_id)) {
782 ovsdb_jsonrpc_add_monitor_column(struct ovsdb_jsonrpc_monitor_table *mt,
783 const struct ovsdb_column *column,
784 enum ovsdb_jsonrpc_monitor_selection select,
785 size_t *allocated_columns)
787 struct ovsdb_jsonrpc_monitor_column *c;
789 if (mt->n_columns >= *allocated_columns) {
790 mt->columns = x2nrealloc(mt->columns, allocated_columns,
791 sizeof *mt->columns);
794 c = &mt->columns[mt->n_columns++];
800 compare_ovsdb_jsonrpc_monitor_column(const void *a_, const void *b_)
802 const struct ovsdb_jsonrpc_monitor_column *a = a_;
803 const struct ovsdb_jsonrpc_monitor_column *b = b_;
805 return a->column < b->column ? -1 : a->column > b->column;
808 static struct ovsdb_error * WARN_UNUSED_RESULT
809 ovsdb_jsonrpc_parse_monitor_request(struct ovsdb_jsonrpc_monitor_table *mt,
810 const struct json *monitor_request,
811 size_t *allocated_columns)
813 const struct ovsdb_table_schema *ts = mt->table->schema;
814 enum ovsdb_jsonrpc_monitor_selection select;
815 const struct json *columns, *select_json;
816 struct ovsdb_parser parser;
817 struct ovsdb_error *error;
819 ovsdb_parser_init(&parser, monitor_request, "table %s", ts->name);
820 columns = ovsdb_parser_member(&parser, "columns", OP_ARRAY | OP_OPTIONAL);
821 select_json = ovsdb_parser_member(&parser, "select",
822 OP_OBJECT | OP_OPTIONAL);
823 error = ovsdb_parser_finish(&parser);
830 ovsdb_parser_init(&parser, select_json, "table %s select", ts->name);
831 if (parse_bool(&parser, "initial", true)) {
832 select |= OJMS_INITIAL;
834 if (parse_bool(&parser, "insert", true)) {
835 select |= OJMS_INSERT;
837 if (parse_bool(&parser, "delete", true)) {
838 select |= OJMS_DELETE;
840 if (parse_bool(&parser, "modify", true)) {
841 select |= OJMS_MODIFY;
843 error = ovsdb_parser_finish(&parser);
848 select = OJMS_INITIAL | OJMS_INSERT | OJMS_DELETE | OJMS_MODIFY;
850 mt->select |= select;
855 if (columns->type != JSON_ARRAY) {
856 return ovsdb_syntax_error(columns, NULL,
857 "array of column names expected");
860 for (i = 0; i < columns->u.array.n; i++) {
861 const struct ovsdb_column *column;
864 if (columns->u.array.elems[i]->type != JSON_STRING) {
865 return ovsdb_syntax_error(columns, NULL,
866 "array of column names expected");
869 s = columns->u.array.elems[i]->u.string;
870 column = shash_find_data(&mt->table->schema->columns, s);
872 return ovsdb_syntax_error(columns, NULL, "%s is not a valid "
875 ovsdb_jsonrpc_add_monitor_column(mt, column, select,
879 struct shash_node *node;
881 SHASH_FOR_EACH (node, &ts->columns) {
882 const struct ovsdb_column *column = node->data;
883 if (column->index != OVSDB_COL_UUID) {
884 ovsdb_jsonrpc_add_monitor_column(mt, column, select,
894 ovsdb_jsonrpc_monitor_create(struct ovsdb_jsonrpc_session *s,
897 struct ovsdb_jsonrpc_monitor *m = NULL;
898 struct json *monitor_id, *monitor_requests;
899 struct ovsdb_error *error = NULL;
900 struct shash_node *node;
903 if (json_array(params)->n != 3) {
904 error = ovsdb_syntax_error(params, NULL, "invalid parameters");
907 monitor_id = params->u.array.elems[1];
908 monitor_requests = params->u.array.elems[2];
909 if (monitor_requests->type != JSON_OBJECT) {
910 error = ovsdb_syntax_error(monitor_requests, NULL,
911 "monitor-requests must be object");
915 if (ovsdb_jsonrpc_monitor_find(s, monitor_id)) {
916 error = ovsdb_syntax_error(monitor_id, NULL, "duplicate monitor ID");
920 m = xzalloc(sizeof *m);
921 ovsdb_replica_init(&m->replica, &ovsdb_jsonrpc_replica_class);
922 ovsdb_add_replica(s->remote->server->db, &m->replica);
924 hmap_insert(&s->monitors, &m->node, json_hash(monitor_id, 0));
925 m->monitor_id = json_clone(monitor_id);
926 shash_init(&m->tables);
928 SHASH_FOR_EACH (node, json_object(monitor_requests)) {
929 const struct ovsdb_table *table;
930 struct ovsdb_jsonrpc_monitor_table *mt;
931 size_t allocated_columns;
932 const struct json *mr_value;
935 table = ovsdb_get_table(s->remote->server->db, node->name);
937 error = ovsdb_syntax_error(NULL, NULL,
938 "no table named %s", node->name);
942 mt = xzalloc(sizeof *mt);
944 shash_add(&m->tables, table->schema->name, mt);
947 mr_value = node->data;
948 allocated_columns = 0;
949 if (mr_value->type == JSON_ARRAY) {
950 const struct json_array *array = &mr_value->u.array;
952 for (i = 0; i < array->n; i++) {
953 error = ovsdb_jsonrpc_parse_monitor_request(
954 mt, array->elems[i], &allocated_columns);
960 error = ovsdb_jsonrpc_parse_monitor_request(
961 mt, mr_value, &allocated_columns);
967 /* Check for duplicate columns. */
968 qsort(mt->columns, mt->n_columns, sizeof *mt->columns,
969 compare_ovsdb_jsonrpc_monitor_column);
970 for (i = 1; i < mt->n_columns; i++) {
971 if (mt->columns[i].column == mt->columns[i - 1].column) {
972 error = ovsdb_syntax_error(mr_value, NULL, "column %s "
973 "mentioned more than once",
974 mt->columns[i].column->name);
980 return ovsdb_jsonrpc_monitor_get_initial(m);
984 ovsdb_remove_replica(s->remote->server->db, &m->replica);
987 json = ovsdb_error_to_json(error);
988 ovsdb_error_destroy(error);
992 static struct jsonrpc_msg *
993 ovsdb_jsonrpc_monitor_cancel(struct ovsdb_jsonrpc_session *s,
994 struct json_array *params,
995 const struct json *request_id)
997 if (params->n != 1) {
998 return jsonrpc_create_error(json_string_create("invalid parameters"),
1001 struct ovsdb_jsonrpc_monitor *m;
1003 m = ovsdb_jsonrpc_monitor_find(s, params->elems[0]);
1005 return jsonrpc_create_error(json_string_create("unknown monitor"),
1008 ovsdb_remove_replica(s->remote->server->db, &m->replica);
1009 return jsonrpc_create_reply(json_object_create(), request_id);
1015 ovsdb_jsonrpc_monitor_remove_all(struct ovsdb_jsonrpc_session *s)
1017 struct ovsdb_jsonrpc_monitor *m, *next;
1019 HMAP_FOR_EACH_SAFE (m, next, node, &s->monitors) {
1020 ovsdb_remove_replica(s->remote->server->db, &m->replica);
1024 static struct ovsdb_jsonrpc_monitor *
1025 ovsdb_jsonrpc_monitor_cast(struct ovsdb_replica *replica)
1027 assert(replica->class == &ovsdb_jsonrpc_replica_class);
1028 return CONTAINER_OF(replica, struct ovsdb_jsonrpc_monitor, replica);
1031 struct ovsdb_jsonrpc_monitor_aux {
1032 bool initial; /* Sending initial contents of table? */
1033 const struct ovsdb_jsonrpc_monitor *monitor;
1034 struct json *json; /* JSON for the whole transaction. */
1036 /* Current table. */
1037 struct ovsdb_jsonrpc_monitor_table *mt;
1038 struct json *table_json; /* JSON for table's transaction. */
1042 any_reportable_change(const struct ovsdb_jsonrpc_monitor_table *mt,
1043 const unsigned long int *changed)
1047 for (i = 0; i < mt->n_columns; i++) {
1048 const struct ovsdb_jsonrpc_monitor_column *c = &mt->columns[i];
1049 unsigned int idx = c->column->index;
1051 if (c->select & OJMS_MODIFY && bitmap_is_set(changed, idx)) {
1060 ovsdb_jsonrpc_monitor_change_cb(const struct ovsdb_row *old,
1061 const struct ovsdb_row *new,
1062 const unsigned long int *changed,
1065 struct ovsdb_jsonrpc_monitor_aux *aux = aux_;
1066 const struct ovsdb_jsonrpc_monitor *m = aux->monitor;
1067 struct ovsdb_table *table = new ? new->table : old->table;
1068 enum ovsdb_jsonrpc_monitor_selection type;
1069 struct json *old_json, *new_json;
1070 struct json *row_json;
1071 char uuid[UUID_LEN + 1];
1074 if (!aux->mt || table != aux->mt->table) {
1075 aux->mt = shash_find_data(&m->tables, table->schema->name);
1076 aux->table_json = NULL;
1078 /* We don't care about rows in this table at all. Tell the caller
1084 type = (aux->initial ? OJMS_INITIAL
1085 : !old ? OJMS_INSERT
1086 : !new ? OJMS_DELETE
1088 if (!(aux->mt->select & type)) {
1089 /* We don't care about this type of change (but do want to be called
1090 * back for changes to other rows in the same table). */
1094 if (type == OJMS_MODIFY && !any_reportable_change(aux->mt, changed)) {
1095 /* Nothing of interest changed. */
1099 old_json = new_json = NULL;
1100 if (type & (OJMS_DELETE | OJMS_MODIFY)) {
1101 old_json = json_object_create();
1103 if (type & (OJMS_INITIAL | OJMS_INSERT | OJMS_MODIFY)) {
1104 new_json = json_object_create();
1106 for (i = 0; i < aux->mt->n_columns; i++) {
1107 const struct ovsdb_jsonrpc_monitor_column *c = &aux->mt->columns[i];
1108 const struct ovsdb_column *column = c->column;
1109 unsigned int idx = c->column->index;
1111 if (!(type & c->select)) {
1112 /* We don't care about this type of change for this particular
1113 * column (but we will care about it for some other column). */
1117 if ((type == OJMS_MODIFY && bitmap_is_set(changed, idx))
1118 || type == OJMS_DELETE) {
1119 json_object_put(old_json, column->name,
1120 ovsdb_datum_to_json(&old->fields[idx],
1123 if (type & (OJMS_INITIAL | OJMS_INSERT | OJMS_MODIFY)) {
1124 json_object_put(new_json, column->name,
1125 ovsdb_datum_to_json(&new->fields[idx],
1130 /* Create JSON object for transaction overall. */
1132 aux->json = json_object_create();
1135 /* Create JSON object for transaction on this table. */
1136 if (!aux->table_json) {
1137 aux->table_json = json_object_create();
1138 json_object_put(aux->json, aux->mt->table->schema->name,
1142 /* Create JSON object for transaction on this row. */
1143 row_json = json_object_create();
1145 json_object_put(row_json, "old", old_json);
1148 json_object_put(row_json, "new", new_json);
1151 /* Add JSON row to JSON table. */
1152 snprintf(uuid, sizeof uuid,
1153 UUID_FMT, UUID_ARGS(ovsdb_row_get_uuid(new ? new : old)));
1154 json_object_put(aux->table_json, uuid, row_json);
1160 ovsdb_jsonrpc_monitor_init_aux(struct ovsdb_jsonrpc_monitor_aux *aux,
1161 const struct ovsdb_jsonrpc_monitor *m,
1164 aux->initial = initial;
1168 aux->table_json = NULL;
1171 static struct ovsdb_error *
1172 ovsdb_jsonrpc_monitor_commit(struct ovsdb_replica *replica,
1173 const struct ovsdb_txn *txn,
1174 bool durable OVS_UNUSED)
1176 struct ovsdb_jsonrpc_monitor *m = ovsdb_jsonrpc_monitor_cast(replica);
1177 struct ovsdb_jsonrpc_monitor_aux aux;
1179 ovsdb_jsonrpc_monitor_init_aux(&aux, m, false);
1180 ovsdb_txn_for_each_change(txn, ovsdb_jsonrpc_monitor_change_cb, &aux);
1182 struct jsonrpc_msg *msg;
1183 struct json *params;
1185 params = json_array_create_2(json_clone(aux.monitor->monitor_id),
1187 msg = jsonrpc_create_notify("update", params);
1188 jsonrpc_session_send(aux.monitor->session->js, msg);
1194 static struct json *
1195 ovsdb_jsonrpc_monitor_get_initial(const struct ovsdb_jsonrpc_monitor *m)
1197 struct ovsdb_jsonrpc_monitor_aux aux;
1198 struct shash_node *node;
1200 ovsdb_jsonrpc_monitor_init_aux(&aux, m, true);
1201 SHASH_FOR_EACH (node, &m->tables) {
1202 struct ovsdb_jsonrpc_monitor_table *mt = node->data;
1204 if (mt->select & OJMS_INITIAL) {
1205 struct ovsdb_row *row;
1207 HMAP_FOR_EACH (row, hmap_node, &mt->table->rows) {
1208 ovsdb_jsonrpc_monitor_change_cb(NULL, row, NULL, &aux);
1212 return aux.json ? aux.json : json_object_create();
1216 ovsdb_jsonrpc_monitor_destroy(struct ovsdb_replica *replica)
1218 struct ovsdb_jsonrpc_monitor *m = ovsdb_jsonrpc_monitor_cast(replica);
1219 struct shash_node *node;
1221 json_destroy(m->monitor_id);
1222 SHASH_FOR_EACH (node, &m->tables) {
1223 struct ovsdb_jsonrpc_monitor_table *mt = node->data;
1227 shash_destroy(&m->tables);
1228 hmap_remove(&m->session->monitors, &m->node);
1232 static const struct ovsdb_replica_class ovsdb_jsonrpc_replica_class = {
1233 ovsdb_jsonrpc_monitor_commit,
1234 ovsdb_jsonrpc_monitor_destroy