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"
36 #include "transaction.h"
40 VLOG_DEFINE_THIS_MODULE(ovsdb_jsonrpc_server);
42 struct ovsdb_jsonrpc_remote;
43 struct ovsdb_jsonrpc_session;
45 /* Message rate-limiting. */
46 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
49 static struct ovsdb_jsonrpc_session *ovsdb_jsonrpc_session_create(
50 struct ovsdb_jsonrpc_remote *, struct jsonrpc_session *);
51 static void ovsdb_jsonrpc_session_run_all(struct ovsdb_jsonrpc_remote *);
52 static void ovsdb_jsonrpc_session_wait_all(struct ovsdb_jsonrpc_remote *);
53 static void ovsdb_jsonrpc_session_close_all(struct ovsdb_jsonrpc_remote *);
54 static void ovsdb_jsonrpc_session_reconnect_all(struct ovsdb_jsonrpc_remote *);
55 static void ovsdb_jsonrpc_session_set_all_options(
56 struct ovsdb_jsonrpc_remote *, const struct ovsdb_jsonrpc_options *);
57 static bool ovsdb_jsonrpc_session_get_status(
58 const struct ovsdb_jsonrpc_remote *,
59 struct ovsdb_jsonrpc_remote_status *);
62 static void ovsdb_jsonrpc_trigger_create(struct ovsdb_jsonrpc_session *,
63 struct json *id, struct json *params);
64 static struct ovsdb_jsonrpc_trigger *ovsdb_jsonrpc_trigger_find(
65 struct ovsdb_jsonrpc_session *, const struct json *id, size_t hash);
66 static void ovsdb_jsonrpc_trigger_complete(struct ovsdb_jsonrpc_trigger *);
67 static void ovsdb_jsonrpc_trigger_complete_all(struct ovsdb_jsonrpc_session *);
68 static void ovsdb_jsonrpc_trigger_complete_done(
69 struct ovsdb_jsonrpc_session *);
72 static struct json *ovsdb_jsonrpc_monitor_create(
73 struct ovsdb_jsonrpc_session *, struct json *params);
74 static struct jsonrpc_msg *ovsdb_jsonrpc_monitor_cancel(
75 struct ovsdb_jsonrpc_session *,
76 struct json_array *params,
77 const struct json *request_id);
78 static void ovsdb_jsonrpc_monitor_remove_all(struct ovsdb_jsonrpc_session *);
80 /* JSON-RPC database server. */
82 struct ovsdb_jsonrpc_server {
84 unsigned int n_sessions, max_sessions;
85 struct shash remotes; /* Contains "struct ovsdb_jsonrpc_remote *"s. */
88 /* A configured remote. This is either a passive stream listener plus a list
89 * of the currently connected sessions, or a list of exactly one active
91 struct ovsdb_jsonrpc_remote {
92 struct ovsdb_jsonrpc_server *server;
93 struct pstream *listener; /* Listener, if passive. */
94 struct list sessions; /* List of "struct ovsdb_jsonrpc_session"s. */
97 static struct ovsdb_jsonrpc_remote *ovsdb_jsonrpc_server_add_remote(
98 struct ovsdb_jsonrpc_server *, const char *name);
99 static void ovsdb_jsonrpc_server_del_remote(struct shash_node *);
101 struct ovsdb_jsonrpc_server *
102 ovsdb_jsonrpc_server_create(struct ovsdb *db)
104 struct ovsdb_jsonrpc_server *server = xzalloc(sizeof *server);
106 server->max_sessions = 64;
107 shash_init(&server->remotes);
112 ovsdb_jsonrpc_server_destroy(struct ovsdb_jsonrpc_server *svr)
114 struct shash_node *node, *next;
116 SHASH_FOR_EACH_SAFE (node, next, &svr->remotes) {
117 ovsdb_jsonrpc_server_del_remote(node);
119 shash_destroy(&svr->remotes);
123 struct ovsdb_jsonrpc_options *
124 ovsdb_jsonrpc_default_options(void)
126 struct ovsdb_jsonrpc_options *options = xzalloc(sizeof *options);
127 options->probe_interval = RECONNECT_DEFAULT_PROBE_INTERVAL;
128 options->max_backoff = RECONNECT_DEFAULT_MAX_BACKOFF;
132 /* Sets 'svr''s current set of remotes to the names in 'new_remotes', with
133 * options in the struct ovsdb_jsonrpc_options supplied as the data values.
135 * A remote is an active or passive stream connection method, e.g. "pssl:" or
138 ovsdb_jsonrpc_server_set_remotes(struct ovsdb_jsonrpc_server *svr,
139 const struct shash *new_remotes)
141 struct shash_node *node, *next;
143 SHASH_FOR_EACH_SAFE (node, next, &svr->remotes) {
144 if (!shash_find(new_remotes, node->name)) {
145 VLOG_INFO("%s: remote deconfigured", node->name);
146 ovsdb_jsonrpc_server_del_remote(node);
149 SHASH_FOR_EACH (node, new_remotes) {
150 const struct ovsdb_jsonrpc_options *options = node->data;
151 struct ovsdb_jsonrpc_remote *remote;
153 remote = shash_find_data(&svr->remotes, node->name);
155 remote = ovsdb_jsonrpc_server_add_remote(svr, node->name);
161 ovsdb_jsonrpc_session_set_all_options(remote, options);
165 static struct ovsdb_jsonrpc_remote *
166 ovsdb_jsonrpc_server_add_remote(struct ovsdb_jsonrpc_server *svr,
169 struct ovsdb_jsonrpc_remote *remote;
170 struct pstream *listener;
173 error = jsonrpc_pstream_open(name, &listener);
174 if (error && error != EAFNOSUPPORT) {
175 VLOG_ERR_RL(&rl, "%s: listen failed: %s", name, strerror(error));
179 remote = xmalloc(sizeof *remote);
180 remote->server = svr;
181 remote->listener = listener;
182 list_init(&remote->sessions);
183 shash_add(&svr->remotes, name, remote);
186 ovsdb_jsonrpc_session_create(remote, jsonrpc_session_open(name));
192 ovsdb_jsonrpc_server_del_remote(struct shash_node *node)
194 struct ovsdb_jsonrpc_remote *remote = node->data;
196 ovsdb_jsonrpc_session_close_all(remote);
197 pstream_close(remote->listener);
198 shash_delete(&remote->server->remotes, node);
202 /* Stores status information for the remote named 'target', which should have
203 * been configured on 'svr' with a call to ovsdb_jsonrpc_server_set_remotes(),
204 * into '*status'. On success returns true, on failure (if 'svr' doesn't have
205 * a remote named 'target' or if that remote is an inbound remote that has no
206 * active connections) returns false. On failure, 'status' will be zeroed.
209 ovsdb_jsonrpc_server_get_remote_status(
210 const struct ovsdb_jsonrpc_server *svr, const char *target,
211 struct ovsdb_jsonrpc_remote_status *status)
213 const struct ovsdb_jsonrpc_remote *remote;
215 memset(status, 0, sizeof *status);
217 remote = shash_find_data(&svr->remotes, target);
218 return remote && ovsdb_jsonrpc_session_get_status(remote, status);
221 /* Forces all of the JSON-RPC sessions managed by 'svr' to disconnect and
224 ovsdb_jsonrpc_server_reconnect(struct ovsdb_jsonrpc_server *svr)
226 struct shash_node *node;
228 SHASH_FOR_EACH (node, &svr->remotes) {
229 struct ovsdb_jsonrpc_remote *remote = node->data;
231 ovsdb_jsonrpc_session_reconnect_all(remote);
236 ovsdb_jsonrpc_server_run(struct ovsdb_jsonrpc_server *svr)
238 struct shash_node *node;
240 SHASH_FOR_EACH (node, &svr->remotes) {
241 struct ovsdb_jsonrpc_remote *remote = node->data;
243 if (remote->listener && svr->n_sessions < svr->max_sessions) {
244 struct stream *stream;
247 error = pstream_accept(remote->listener, &stream);
249 struct jsonrpc_session *js;
250 js = jsonrpc_session_open_unreliably(jsonrpc_open(stream));
251 ovsdb_jsonrpc_session_create(remote, js);
252 } else if (error != EAGAIN) {
253 VLOG_WARN_RL(&rl, "%s: accept failed: %s",
254 pstream_get_name(remote->listener),
259 ovsdb_jsonrpc_session_run_all(remote);
264 ovsdb_jsonrpc_server_wait(struct ovsdb_jsonrpc_server *svr)
266 struct shash_node *node;
268 SHASH_FOR_EACH (node, &svr->remotes) {
269 struct ovsdb_jsonrpc_remote *remote = node->data;
271 if (remote->listener && svr->n_sessions < svr->max_sessions) {
272 pstream_wait(remote->listener);
275 ovsdb_jsonrpc_session_wait_all(remote);
279 /* JSON-RPC database server session. */
281 struct ovsdb_jsonrpc_session {
282 struct ovsdb_jsonrpc_remote *remote;
283 struct list node; /* Element in remote's sessions list. */
286 struct hmap triggers; /* Hmap of "struct ovsdb_jsonrpc_trigger"s. */
287 struct list completions; /* Completed triggers. */
290 struct hmap monitors; /* Hmap of "struct ovsdb_jsonrpc_monitor"s. */
292 /* Network connectivity. */
293 struct jsonrpc_session *js; /* JSON-RPC session. */
294 unsigned int js_seqno; /* Last jsonrpc_session_get_seqno() value. */
297 static void ovsdb_jsonrpc_session_close(struct ovsdb_jsonrpc_session *);
298 static int ovsdb_jsonrpc_session_run(struct ovsdb_jsonrpc_session *);
299 static void ovsdb_jsonrpc_session_wait(struct ovsdb_jsonrpc_session *);
300 static void ovsdb_jsonrpc_session_set_options(
301 struct ovsdb_jsonrpc_session *, const struct ovsdb_jsonrpc_options *);
302 static void ovsdb_jsonrpc_session_got_request(struct ovsdb_jsonrpc_session *,
303 struct jsonrpc_msg *);
304 static void ovsdb_jsonrpc_session_got_notify(struct ovsdb_jsonrpc_session *,
305 struct jsonrpc_msg *);
307 static struct ovsdb_jsonrpc_session *
308 ovsdb_jsonrpc_session_create(struct ovsdb_jsonrpc_remote *remote,
309 struct jsonrpc_session *js)
311 struct ovsdb_jsonrpc_session *s;
313 s = xzalloc(sizeof *s);
315 list_push_back(&remote->sessions, &s->node);
316 hmap_init(&s->triggers);
317 hmap_init(&s->monitors);
318 list_init(&s->completions);
320 s->js_seqno = jsonrpc_session_get_seqno(js);
322 remote->server->n_sessions++;
328 ovsdb_jsonrpc_session_close(struct ovsdb_jsonrpc_session *s)
330 ovsdb_jsonrpc_monitor_remove_all(s);
331 jsonrpc_session_close(s->js);
332 list_remove(&s->node);
333 s->remote->server->n_sessions--;
338 ovsdb_jsonrpc_session_run(struct ovsdb_jsonrpc_session *s)
340 jsonrpc_session_run(s->js);
341 if (s->js_seqno != jsonrpc_session_get_seqno(s->js)) {
342 s->js_seqno = jsonrpc_session_get_seqno(s->js);
343 ovsdb_jsonrpc_trigger_complete_all(s);
344 ovsdb_jsonrpc_monitor_remove_all(s);
347 ovsdb_jsonrpc_trigger_complete_done(s);
349 if (!jsonrpc_session_get_backlog(s->js)) {
350 struct jsonrpc_msg *msg = jsonrpc_session_recv(s->js);
352 if (msg->type == JSONRPC_REQUEST) {
353 ovsdb_jsonrpc_session_got_request(s, msg);
354 } else if (msg->type == JSONRPC_NOTIFY) {
355 ovsdb_jsonrpc_session_got_notify(s, msg);
357 VLOG_WARN("%s: received unexpected %s message",
358 jsonrpc_session_get_name(s->js),
359 jsonrpc_msg_type_to_string(msg->type));
360 jsonrpc_session_force_reconnect(s->js);
361 jsonrpc_msg_destroy(msg);
365 return jsonrpc_session_is_alive(s->js) ? 0 : ETIMEDOUT;
369 ovsdb_jsonrpc_session_set_options(struct ovsdb_jsonrpc_session *session,
370 const struct ovsdb_jsonrpc_options *options)
372 jsonrpc_session_set_max_backoff(session->js, options->max_backoff);
373 jsonrpc_session_set_probe_interval(session->js, options->probe_interval);
377 ovsdb_jsonrpc_session_run_all(struct ovsdb_jsonrpc_remote *remote)
379 struct ovsdb_jsonrpc_session *s, *next;
381 LIST_FOR_EACH_SAFE (s, next, node, &remote->sessions) {
382 int error = ovsdb_jsonrpc_session_run(s);
384 ovsdb_jsonrpc_session_close(s);
390 ovsdb_jsonrpc_session_wait(struct ovsdb_jsonrpc_session *s)
392 jsonrpc_session_wait(s->js);
393 if (!jsonrpc_session_get_backlog(s->js)) {
394 jsonrpc_session_recv_wait(s->js);
399 ovsdb_jsonrpc_session_wait_all(struct ovsdb_jsonrpc_remote *remote)
401 struct ovsdb_jsonrpc_session *s;
403 LIST_FOR_EACH (s, node, &remote->sessions) {
404 ovsdb_jsonrpc_session_wait(s);
409 ovsdb_jsonrpc_session_close_all(struct ovsdb_jsonrpc_remote *remote)
411 struct ovsdb_jsonrpc_session *s, *next;
413 LIST_FOR_EACH_SAFE (s, next, node, &remote->sessions) {
414 ovsdb_jsonrpc_session_close(s);
418 /* Forces all of the JSON-RPC sessions managed by 'remote' to disconnect and
421 ovsdb_jsonrpc_session_reconnect_all(struct ovsdb_jsonrpc_remote *remote)
423 struct ovsdb_jsonrpc_session *s, *next;
425 LIST_FOR_EACH_SAFE (s, next, node, &remote->sessions) {
426 jsonrpc_session_force_reconnect(s->js);
427 if (!jsonrpc_session_is_alive(s->js)) {
428 ovsdb_jsonrpc_session_close(s);
433 /* Sets the options for all of the JSON-RPC sessions managed by 'remote' to
436 ovsdb_jsonrpc_session_set_all_options(
437 struct ovsdb_jsonrpc_remote *remote,
438 const struct ovsdb_jsonrpc_options *options)
440 struct ovsdb_jsonrpc_session *s;
442 LIST_FOR_EACH (s, node, &remote->sessions) {
443 ovsdb_jsonrpc_session_set_options(s, options);
448 ovsdb_jsonrpc_session_get_status(const struct ovsdb_jsonrpc_remote *remote,
449 struct ovsdb_jsonrpc_remote_status *status)
451 const struct ovsdb_jsonrpc_session *s;
452 const struct jsonrpc_session *js;
453 struct reconnect_stats rstats;
455 if (list_is_empty(&remote->sessions)) {
458 s = CONTAINER_OF(remote->sessions.next, struct ovsdb_jsonrpc_session, node);
461 status->is_connected = jsonrpc_session_is_connected(js);
462 status->last_error = jsonrpc_session_get_status(js);
464 jsonrpc_session_get_reconnect_stats(js, &rstats);
465 status->state = rstats.state;
466 status->sec_since_connect = rstats.msec_since_connect == UINT_MAX
467 ? UINT_MAX : rstats.msec_since_connect / 1000;
468 status->sec_since_disconnect = rstats.msec_since_disconnect == UINT_MAX
469 ? UINT_MAX : rstats.msec_since_disconnect / 1000;
471 status->n_connections = list_size(&remote->sessions);
477 get_db_name(const struct ovsdb_jsonrpc_session *s)
479 return s->remote->server->db->schema->name;
482 static struct jsonrpc_msg *
483 ovsdb_jsonrpc_check_db_name(const struct ovsdb_jsonrpc_session *s,
484 const struct jsonrpc_msg *request)
486 struct json_array *params;
487 const char *want_db_name;
488 const char *have_db_name;
489 struct ovsdb_error *error;
490 struct jsonrpc_msg *reply;
492 params = json_array(request->params);
493 if (!params->n || params->elems[0]->type != JSON_STRING) {
494 error = ovsdb_syntax_error(
495 request->params, NULL,
496 "%s request params must begin with <db-name>", request->method);
500 want_db_name = params->elems[0]->u.string;
501 have_db_name = get_db_name(s);
502 if (strcmp(want_db_name, have_db_name)) {
503 error = ovsdb_syntax_error(
504 request->params, "unknown database",
505 "%s request specifies unknown database %s",
506 request->method, want_db_name);
513 reply = jsonrpc_create_reply(ovsdb_error_to_json(error), request->id);
514 ovsdb_error_destroy(error);
518 static struct jsonrpc_msg *
519 execute_transaction(struct ovsdb_jsonrpc_session *s,
520 struct jsonrpc_msg *request)
522 ovsdb_jsonrpc_trigger_create(s, request->id, request->params);
524 request->params = NULL;
525 jsonrpc_msg_destroy(request);
530 ovsdb_jsonrpc_session_got_request(struct ovsdb_jsonrpc_session *s,
531 struct jsonrpc_msg *request)
533 struct jsonrpc_msg *reply;
535 if (!strcmp(request->method, "transact")) {
536 reply = ovsdb_jsonrpc_check_db_name(s, request);
538 reply = execute_transaction(s, request);
540 } else if (!strcmp(request->method, "monitor")) {
541 reply = ovsdb_jsonrpc_check_db_name(s, request);
543 reply = jsonrpc_create_reply(
544 ovsdb_jsonrpc_monitor_create(s, request->params), request->id);
546 } else if (!strcmp(request->method, "monitor_cancel")) {
547 reply = ovsdb_jsonrpc_monitor_cancel(s, json_array(request->params),
549 } else if (!strcmp(request->method, "get_schema")) {
550 reply = ovsdb_jsonrpc_check_db_name(s, request);
552 reply = jsonrpc_create_reply(
553 ovsdb_schema_to_json(s->remote->server->db->schema),
556 } else if (!strcmp(request->method, "list_dbs")) {
557 reply = jsonrpc_create_reply(
558 json_array_create_1(json_string_create(get_db_name(s))),
560 } else if (!strcmp(request->method, "echo")) {
561 reply = jsonrpc_create_reply(json_clone(request->params), request->id);
563 reply = jsonrpc_create_error(json_string_create("unknown method"),
568 jsonrpc_msg_destroy(request);
569 jsonrpc_session_send(s->js, reply);
574 execute_cancel(struct ovsdb_jsonrpc_session *s, struct jsonrpc_msg *request)
576 if (json_array(request->params)->n == 1) {
577 struct ovsdb_jsonrpc_trigger *t;
580 id = request->params->u.array.elems[0];
581 t = ovsdb_jsonrpc_trigger_find(s, id, json_hash(id, 0));
583 ovsdb_jsonrpc_trigger_complete(t);
589 ovsdb_jsonrpc_session_got_notify(struct ovsdb_jsonrpc_session *s,
590 struct jsonrpc_msg *request)
592 if (!strcmp(request->method, "cancel")) {
593 execute_cancel(s, request);
595 jsonrpc_msg_destroy(request);
598 /* JSON-RPC database server triggers.
600 * (Every transaction is treated as a trigger even if it doesn't actually have
601 * any "wait" operations.) */
603 struct ovsdb_jsonrpc_trigger {
604 struct ovsdb_trigger trigger;
605 struct ovsdb_jsonrpc_session *session;
606 struct hmap_node hmap_node; /* In session's "triggers" hmap. */
611 ovsdb_jsonrpc_trigger_create(struct ovsdb_jsonrpc_session *s,
612 struct json *id, struct json *params)
614 struct ovsdb_jsonrpc_trigger *t;
617 /* Check for duplicate ID. */
618 hash = json_hash(id, 0);
619 t = ovsdb_jsonrpc_trigger_find(s, id, hash);
621 struct jsonrpc_msg *msg;
623 msg = jsonrpc_create_error(json_string_create("duplicate request ID"),
625 jsonrpc_session_send(s->js, msg);
627 json_destroy(params);
631 /* Insert into trigger table. */
632 t = xmalloc(sizeof *t);
633 ovsdb_trigger_init(s->remote->server->db,
634 &t->trigger, params, &s->completions,
638 hmap_insert(&s->triggers, &t->hmap_node, hash);
640 /* Complete early if possible. */
641 if (ovsdb_trigger_is_complete(&t->trigger)) {
642 ovsdb_jsonrpc_trigger_complete(t);
646 static struct ovsdb_jsonrpc_trigger *
647 ovsdb_jsonrpc_trigger_find(struct ovsdb_jsonrpc_session *s,
648 const struct json *id, size_t hash)
650 struct ovsdb_jsonrpc_trigger *t;
652 HMAP_FOR_EACH_WITH_HASH (t, hmap_node, hash, &s->triggers) {
653 if (json_equal(t->id, id)) {
662 ovsdb_jsonrpc_trigger_complete(struct ovsdb_jsonrpc_trigger *t)
664 struct ovsdb_jsonrpc_session *s = t->session;
666 if (jsonrpc_session_is_connected(s->js)) {
667 struct jsonrpc_msg *reply;
670 result = ovsdb_trigger_steal_result(&t->trigger);
672 reply = jsonrpc_create_reply(result, t->id);
674 reply = jsonrpc_create_error(json_string_create("canceled"),
677 jsonrpc_session_send(s->js, reply);
681 ovsdb_trigger_destroy(&t->trigger);
682 hmap_remove(&s->triggers, &t->hmap_node);
687 ovsdb_jsonrpc_trigger_complete_all(struct ovsdb_jsonrpc_session *s)
689 struct ovsdb_jsonrpc_trigger *t, *next;
690 HMAP_FOR_EACH_SAFE (t, next, hmap_node, &s->triggers) {
691 ovsdb_jsonrpc_trigger_complete(t);
696 ovsdb_jsonrpc_trigger_complete_done(struct ovsdb_jsonrpc_session *s)
698 while (!list_is_empty(&s->completions)) {
699 struct ovsdb_jsonrpc_trigger *t
700 = CONTAINER_OF(s->completions.next,
701 struct ovsdb_jsonrpc_trigger, trigger.node);
702 ovsdb_jsonrpc_trigger_complete(t);
706 /* JSON-RPC database table monitors. */
708 enum ovsdb_jsonrpc_monitor_selection {
709 OJMS_INITIAL = 1 << 0, /* All rows when monitor is created. */
710 OJMS_INSERT = 1 << 1, /* New rows. */
711 OJMS_DELETE = 1 << 2, /* Deleted rows. */
712 OJMS_MODIFY = 1 << 3 /* Modified rows. */
715 /* A particular column being monitored. */
716 struct ovsdb_jsonrpc_monitor_column {
717 const struct ovsdb_column *column;
718 enum ovsdb_jsonrpc_monitor_selection select;
721 /* A particular table being monitored. */
722 struct ovsdb_jsonrpc_monitor_table {
723 const struct ovsdb_table *table;
725 /* This is the union (bitwise-OR) of the 'select' values in all of the
726 * members of 'columns' below. */
727 enum ovsdb_jsonrpc_monitor_selection select;
729 /* Columns being monitored. */
730 struct ovsdb_jsonrpc_monitor_column *columns;
734 /* A collection of tables being monitored. */
735 struct ovsdb_jsonrpc_monitor {
736 struct ovsdb_replica replica;
737 struct ovsdb_jsonrpc_session *session;
738 struct hmap_node node; /* In ovsdb_jsonrpc_session's "monitors". */
740 struct json *monitor_id;
741 struct shash tables; /* Holds "struct ovsdb_jsonrpc_monitor_table"s. */
744 static const struct ovsdb_replica_class ovsdb_jsonrpc_replica_class;
746 struct ovsdb_jsonrpc_monitor *ovsdb_jsonrpc_monitor_find(
747 struct ovsdb_jsonrpc_session *, const struct json *monitor_id);
748 static void ovsdb_jsonrpc_monitor_destroy(struct ovsdb_replica *);
749 static struct json *ovsdb_jsonrpc_monitor_get_initial(
750 const struct ovsdb_jsonrpc_monitor *);
753 parse_bool(struct ovsdb_parser *parser, const char *name, bool default_value)
755 const struct json *json;
757 json = ovsdb_parser_member(parser, name, OP_BOOLEAN | OP_OPTIONAL);
758 return json ? json_boolean(json) : default_value;
761 struct ovsdb_jsonrpc_monitor *
762 ovsdb_jsonrpc_monitor_find(struct ovsdb_jsonrpc_session *s,
763 const struct json *monitor_id)
765 struct ovsdb_jsonrpc_monitor *m;
767 HMAP_FOR_EACH_WITH_HASH (m, node, json_hash(monitor_id, 0), &s->monitors) {
768 if (json_equal(m->monitor_id, monitor_id)) {
777 ovsdb_jsonrpc_add_monitor_column(struct ovsdb_jsonrpc_monitor_table *mt,
778 const struct ovsdb_column *column,
779 enum ovsdb_jsonrpc_monitor_selection select,
780 size_t *allocated_columns)
782 struct ovsdb_jsonrpc_monitor_column *c;
784 if (mt->n_columns >= *allocated_columns) {
785 mt->columns = x2nrealloc(mt->columns, allocated_columns,
786 sizeof *mt->columns);
789 c = &mt->columns[mt->n_columns++];
795 compare_ovsdb_jsonrpc_monitor_column(const void *a_, const void *b_)
797 const struct ovsdb_jsonrpc_monitor_column *a = a_;
798 const struct ovsdb_jsonrpc_monitor_column *b = b_;
800 return a->column < b->column ? -1 : a->column > b->column;
803 static struct ovsdb_error * WARN_UNUSED_RESULT
804 ovsdb_jsonrpc_parse_monitor_request(struct ovsdb_jsonrpc_monitor_table *mt,
805 const struct json *monitor_request,
806 size_t *allocated_columns)
808 const struct ovsdb_table_schema *ts = mt->table->schema;
809 enum ovsdb_jsonrpc_monitor_selection select;
810 const struct json *columns, *select_json;
811 struct ovsdb_parser parser;
812 struct ovsdb_error *error;
814 ovsdb_parser_init(&parser, monitor_request, "table %s", ts->name);
815 columns = ovsdb_parser_member(&parser, "columns", OP_ARRAY | OP_OPTIONAL);
816 select_json = ovsdb_parser_member(&parser, "select",
817 OP_OBJECT | OP_OPTIONAL);
818 error = ovsdb_parser_finish(&parser);
825 ovsdb_parser_init(&parser, select_json, "table %s select", ts->name);
826 if (parse_bool(&parser, "initial", true)) {
827 select |= OJMS_INITIAL;
829 if (parse_bool(&parser, "insert", true)) {
830 select |= OJMS_INSERT;
832 if (parse_bool(&parser, "delete", true)) {
833 select |= OJMS_DELETE;
835 if (parse_bool(&parser, "modify", true)) {
836 select |= OJMS_MODIFY;
838 error = ovsdb_parser_finish(&parser);
843 select = OJMS_INITIAL | OJMS_INSERT | OJMS_DELETE | OJMS_MODIFY;
845 mt->select |= select;
850 if (columns->type != JSON_ARRAY) {
851 return ovsdb_syntax_error(columns, NULL,
852 "array of column names expected");
855 for (i = 0; i < columns->u.array.n; i++) {
856 const struct ovsdb_column *column;
859 if (columns->u.array.elems[i]->type != JSON_STRING) {
860 return ovsdb_syntax_error(columns, NULL,
861 "array of column names expected");
864 s = columns->u.array.elems[i]->u.string;
865 column = shash_find_data(&mt->table->schema->columns, s);
867 return ovsdb_syntax_error(columns, NULL, "%s is not a valid "
870 ovsdb_jsonrpc_add_monitor_column(mt, column, select,
874 struct shash_node *node;
876 SHASH_FOR_EACH (node, &ts->columns) {
877 const struct ovsdb_column *column = node->data;
878 if (column->index != OVSDB_COL_UUID) {
879 ovsdb_jsonrpc_add_monitor_column(mt, column, select,
889 ovsdb_jsonrpc_monitor_create(struct ovsdb_jsonrpc_session *s,
892 struct ovsdb_jsonrpc_monitor *m = NULL;
893 struct json *monitor_id, *monitor_requests;
894 struct ovsdb_error *error = NULL;
895 struct shash_node *node;
898 if (json_array(params)->n != 3) {
899 error = ovsdb_syntax_error(params, NULL, "invalid parameters");
902 monitor_id = params->u.array.elems[1];
903 monitor_requests = params->u.array.elems[2];
904 if (monitor_requests->type != JSON_OBJECT) {
905 error = ovsdb_syntax_error(monitor_requests, NULL,
906 "monitor-requests must be object");
910 if (ovsdb_jsonrpc_monitor_find(s, monitor_id)) {
911 error = ovsdb_syntax_error(monitor_id, NULL, "duplicate monitor ID");
915 m = xzalloc(sizeof *m);
916 ovsdb_replica_init(&m->replica, &ovsdb_jsonrpc_replica_class);
917 ovsdb_add_replica(s->remote->server->db, &m->replica);
919 hmap_insert(&s->monitors, &m->node, json_hash(monitor_id, 0));
920 m->monitor_id = json_clone(monitor_id);
921 shash_init(&m->tables);
923 SHASH_FOR_EACH (node, json_object(monitor_requests)) {
924 const struct ovsdb_table *table;
925 struct ovsdb_jsonrpc_monitor_table *mt;
926 size_t allocated_columns;
927 const struct json *mr_value;
930 table = ovsdb_get_table(s->remote->server->db, node->name);
932 error = ovsdb_syntax_error(NULL, NULL,
933 "no table named %s", node->name);
937 mt = xzalloc(sizeof *mt);
939 shash_add(&m->tables, table->schema->name, mt);
942 mr_value = node->data;
943 allocated_columns = 0;
944 if (mr_value->type == JSON_ARRAY) {
945 const struct json_array *array = &mr_value->u.array;
947 for (i = 0; i < array->n; i++) {
948 error = ovsdb_jsonrpc_parse_monitor_request(
949 mt, array->elems[i], &allocated_columns);
955 error = ovsdb_jsonrpc_parse_monitor_request(
956 mt, mr_value, &allocated_columns);
962 /* Check for duplicate columns. */
963 qsort(mt->columns, mt->n_columns, sizeof *mt->columns,
964 compare_ovsdb_jsonrpc_monitor_column);
965 for (i = 1; i < mt->n_columns; i++) {
966 if (mt->columns[i].column == mt->columns[i - 1].column) {
967 error = ovsdb_syntax_error(mr_value, NULL, "column %s "
968 "mentioned more than once",
969 mt->columns[i].column->name);
975 return ovsdb_jsonrpc_monitor_get_initial(m);
979 ovsdb_remove_replica(s->remote->server->db, &m->replica);
982 json = ovsdb_error_to_json(error);
983 ovsdb_error_destroy(error);
987 static struct jsonrpc_msg *
988 ovsdb_jsonrpc_monitor_cancel(struct ovsdb_jsonrpc_session *s,
989 struct json_array *params,
990 const struct json *request_id)
992 if (params->n != 1) {
993 return jsonrpc_create_error(json_string_create("invalid parameters"),
996 struct ovsdb_jsonrpc_monitor *m;
998 m = ovsdb_jsonrpc_monitor_find(s, params->elems[0]);
1000 return jsonrpc_create_error(json_string_create("unknown monitor"),
1003 ovsdb_remove_replica(s->remote->server->db, &m->replica);
1004 return jsonrpc_create_reply(json_object_create(), request_id);
1010 ovsdb_jsonrpc_monitor_remove_all(struct ovsdb_jsonrpc_session *s)
1012 struct ovsdb_jsonrpc_monitor *m, *next;
1014 HMAP_FOR_EACH_SAFE (m, next, node, &s->monitors) {
1015 ovsdb_remove_replica(s->remote->server->db, &m->replica);
1019 static struct ovsdb_jsonrpc_monitor *
1020 ovsdb_jsonrpc_monitor_cast(struct ovsdb_replica *replica)
1022 assert(replica->class == &ovsdb_jsonrpc_replica_class);
1023 return CONTAINER_OF(replica, struct ovsdb_jsonrpc_monitor, replica);
1026 struct ovsdb_jsonrpc_monitor_aux {
1027 bool initial; /* Sending initial contents of table? */
1028 const struct ovsdb_jsonrpc_monitor *monitor;
1029 struct json *json; /* JSON for the whole transaction. */
1031 /* Current table. */
1032 struct ovsdb_jsonrpc_monitor_table *mt;
1033 struct json *table_json; /* JSON for table's transaction. */
1037 any_reportable_change(const struct ovsdb_jsonrpc_monitor_table *mt,
1038 const unsigned long int *changed)
1042 for (i = 0; i < mt->n_columns; i++) {
1043 const struct ovsdb_jsonrpc_monitor_column *c = &mt->columns[i];
1044 unsigned int idx = c->column->index;
1046 if (c->select & OJMS_MODIFY && bitmap_is_set(changed, idx)) {
1055 ovsdb_jsonrpc_monitor_change_cb(const struct ovsdb_row *old,
1056 const struct ovsdb_row *new,
1057 const unsigned long int *changed,
1060 struct ovsdb_jsonrpc_monitor_aux *aux = aux_;
1061 const struct ovsdb_jsonrpc_monitor *m = aux->monitor;
1062 struct ovsdb_table *table = new ? new->table : old->table;
1063 enum ovsdb_jsonrpc_monitor_selection type;
1064 struct json *old_json, *new_json;
1065 struct json *row_json;
1066 char uuid[UUID_LEN + 1];
1069 if (!aux->mt || table != aux->mt->table) {
1070 aux->mt = shash_find_data(&m->tables, table->schema->name);
1071 aux->table_json = NULL;
1073 /* We don't care about rows in this table at all. Tell the caller
1079 type = (aux->initial ? OJMS_INITIAL
1080 : !old ? OJMS_INSERT
1081 : !new ? OJMS_DELETE
1083 if (!(aux->mt->select & type)) {
1084 /* We don't care about this type of change (but do want to be called
1085 * back for changes to other rows in the same table). */
1089 if (type == OJMS_MODIFY && !any_reportable_change(aux->mt, changed)) {
1090 /* Nothing of interest changed. */
1094 old_json = new_json = NULL;
1095 if (type & (OJMS_DELETE | OJMS_MODIFY)) {
1096 old_json = json_object_create();
1098 if (type & (OJMS_INITIAL | OJMS_INSERT | OJMS_MODIFY)) {
1099 new_json = json_object_create();
1101 for (i = 0; i < aux->mt->n_columns; i++) {
1102 const struct ovsdb_jsonrpc_monitor_column *c = &aux->mt->columns[i];
1103 const struct ovsdb_column *column = c->column;
1104 unsigned int idx = c->column->index;
1106 if (!(type & c->select)) {
1107 /* We don't care about this type of change for this particular
1108 * column (but we will care about it for some other column). */
1112 if ((type == OJMS_MODIFY && bitmap_is_set(changed, idx))
1113 || type == OJMS_DELETE) {
1114 json_object_put(old_json, column->name,
1115 ovsdb_datum_to_json(&old->fields[idx],
1118 if (type & (OJMS_INITIAL | OJMS_INSERT | OJMS_MODIFY)) {
1119 json_object_put(new_json, column->name,
1120 ovsdb_datum_to_json(&new->fields[idx],
1125 /* Create JSON object for transaction overall. */
1127 aux->json = json_object_create();
1130 /* Create JSON object for transaction on this table. */
1131 if (!aux->table_json) {
1132 aux->table_json = json_object_create();
1133 json_object_put(aux->json, aux->mt->table->schema->name,
1137 /* Create JSON object for transaction on this row. */
1138 row_json = json_object_create();
1140 json_object_put(row_json, "old", old_json);
1143 json_object_put(row_json, "new", new_json);
1146 /* Add JSON row to JSON table. */
1147 snprintf(uuid, sizeof uuid,
1148 UUID_FMT, UUID_ARGS(ovsdb_row_get_uuid(new ? new : old)));
1149 json_object_put(aux->table_json, uuid, row_json);
1155 ovsdb_jsonrpc_monitor_init_aux(struct ovsdb_jsonrpc_monitor_aux *aux,
1156 const struct ovsdb_jsonrpc_monitor *m,
1159 aux->initial = initial;
1163 aux->table_json = NULL;
1166 static struct ovsdb_error *
1167 ovsdb_jsonrpc_monitor_commit(struct ovsdb_replica *replica,
1168 const struct ovsdb_txn *txn,
1169 bool durable OVS_UNUSED)
1171 struct ovsdb_jsonrpc_monitor *m = ovsdb_jsonrpc_monitor_cast(replica);
1172 struct ovsdb_jsonrpc_monitor_aux aux;
1174 ovsdb_jsonrpc_monitor_init_aux(&aux, m, false);
1175 ovsdb_txn_for_each_change(txn, ovsdb_jsonrpc_monitor_change_cb, &aux);
1177 struct jsonrpc_msg *msg;
1178 struct json *params;
1180 params = json_array_create_2(json_clone(aux.monitor->monitor_id),
1182 msg = jsonrpc_create_notify("update", params);
1183 jsonrpc_session_send(aux.monitor->session->js, msg);
1189 static struct json *
1190 ovsdb_jsonrpc_monitor_get_initial(const struct ovsdb_jsonrpc_monitor *m)
1192 struct ovsdb_jsonrpc_monitor_aux aux;
1193 struct shash_node *node;
1195 ovsdb_jsonrpc_monitor_init_aux(&aux, m, true);
1196 SHASH_FOR_EACH (node, &m->tables) {
1197 struct ovsdb_jsonrpc_monitor_table *mt = node->data;
1199 if (mt->select & OJMS_INITIAL) {
1200 struct ovsdb_row *row;
1202 HMAP_FOR_EACH (row, hmap_node, &mt->table->rows) {
1203 ovsdb_jsonrpc_monitor_change_cb(NULL, row, NULL, &aux);
1207 return aux.json ? aux.json : json_object_create();
1211 ovsdb_jsonrpc_monitor_destroy(struct ovsdb_replica *replica)
1213 struct ovsdb_jsonrpc_monitor *m = ovsdb_jsonrpc_monitor_cast(replica);
1214 struct shash_node *node;
1216 json_destroy(m->monitor_id);
1217 SHASH_FOR_EACH (node, &m->tables) {
1218 struct ovsdb_jsonrpc_monitor_table *mt = node->data;
1222 shash_destroy(&m->tables);
1223 hmap_remove(&m->session->monitors, &m->node);
1227 static const struct ovsdb_replica_class ovsdb_jsonrpc_replica_class = {
1228 ovsdb_jsonrpc_monitor_commit,
1229 ovsdb_jsonrpc_monitor_destroy