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 *);
58 static void ovsdb_jsonrpc_trigger_create(struct ovsdb_jsonrpc_session *,
59 struct json *id, struct json *params);
60 static struct ovsdb_jsonrpc_trigger *ovsdb_jsonrpc_trigger_find(
61 struct ovsdb_jsonrpc_session *, const struct json *id, size_t hash);
62 static void ovsdb_jsonrpc_trigger_complete(struct ovsdb_jsonrpc_trigger *);
63 static void ovsdb_jsonrpc_trigger_complete_all(struct ovsdb_jsonrpc_session *);
64 static void ovsdb_jsonrpc_trigger_complete_done(
65 struct ovsdb_jsonrpc_session *);
68 static struct json *ovsdb_jsonrpc_monitor_create(
69 struct ovsdb_jsonrpc_session *, struct json *params);
70 static struct jsonrpc_msg *ovsdb_jsonrpc_monitor_cancel(
71 struct ovsdb_jsonrpc_session *,
72 struct json_array *params,
73 const struct json *request_id);
74 static void ovsdb_jsonrpc_monitor_remove_all(struct ovsdb_jsonrpc_session *);
76 /* JSON-RPC database server. */
78 struct ovsdb_jsonrpc_server {
80 unsigned int n_sessions, max_sessions;
81 struct shash remotes; /* Contains "struct ovsdb_jsonrpc_remote *"s. */
84 /* A configured remote. This is either a passive stream listener plus a list
85 * of the currently connected sessions, or a list of exactly one active
87 struct ovsdb_jsonrpc_remote {
88 struct ovsdb_jsonrpc_server *server;
89 struct pstream *listener; /* Listener, if passive. */
90 struct list sessions; /* List of "struct ovsdb_jsonrpc_session"s. */
93 static struct ovsdb_jsonrpc_remote *ovsdb_jsonrpc_server_add_remote(
94 struct ovsdb_jsonrpc_server *, const char *name);
95 static void ovsdb_jsonrpc_server_del_remote(struct shash_node *);
97 struct ovsdb_jsonrpc_server *
98 ovsdb_jsonrpc_server_create(struct ovsdb *db)
100 struct ovsdb_jsonrpc_server *server = xzalloc(sizeof *server);
102 server->max_sessions = 64;
103 shash_init(&server->remotes);
108 ovsdb_jsonrpc_server_destroy(struct ovsdb_jsonrpc_server *svr)
110 struct shash_node *node, *next;
112 SHASH_FOR_EACH_SAFE (node, next, &svr->remotes) {
113 ovsdb_jsonrpc_server_del_remote(node);
115 shash_destroy(&svr->remotes);
119 struct ovsdb_jsonrpc_options *
120 ovsdb_jsonrpc_default_options(void)
122 struct ovsdb_jsonrpc_options *options = xzalloc(sizeof *options);
123 options->probe_interval = RECONNECT_DEFAULT_PROBE_INTERVAL;
124 options->max_backoff = RECONNECT_DEFAULT_MAX_BACKOFF;
128 /* Sets 'svr''s current set of remotes to the names in 'new_remotes', with
129 * options in the struct ovsdb_jsonrpc_options supplied as the data values.
131 * A remote is an active or passive stream connection method, e.g. "pssl:" or
134 ovsdb_jsonrpc_server_set_remotes(struct ovsdb_jsonrpc_server *svr,
135 const struct shash *new_remotes)
137 struct shash_node *node, *next;
139 SHASH_FOR_EACH_SAFE (node, next, &svr->remotes) {
140 if (!shash_find(new_remotes, node->name)) {
141 ovsdb_jsonrpc_server_del_remote(node);
144 SHASH_FOR_EACH (node, new_remotes) {
145 const struct ovsdb_jsonrpc_options *options = node->data;
146 struct ovsdb_jsonrpc_remote *remote;
148 remote = shash_find_data(&svr->remotes, node->name);
150 remote = ovsdb_jsonrpc_server_add_remote(svr, node->name);
156 ovsdb_jsonrpc_session_set_all_options(remote, options);
160 static struct ovsdb_jsonrpc_remote *
161 ovsdb_jsonrpc_server_add_remote(struct ovsdb_jsonrpc_server *svr,
164 struct ovsdb_jsonrpc_remote *remote;
165 struct pstream *listener;
168 error = jsonrpc_pstream_open(name, &listener);
169 if (error && error != EAFNOSUPPORT) {
170 VLOG_ERR_RL(&rl, "%s: listen failed: %s", name, strerror(error));
174 remote = xmalloc(sizeof *remote);
175 remote->server = svr;
176 remote->listener = listener;
177 list_init(&remote->sessions);
178 shash_add(&svr->remotes, name, remote);
181 ovsdb_jsonrpc_session_create(remote, jsonrpc_session_open(name));
187 ovsdb_jsonrpc_server_del_remote(struct shash_node *node)
189 struct ovsdb_jsonrpc_remote *remote = node->data;
191 ovsdb_jsonrpc_session_close_all(remote);
192 pstream_close(remote->listener);
193 shash_delete(&remote->server->remotes, node);
197 /* Forces all of the JSON-RPC sessions managed by 'svr' to disconnect and
200 ovsdb_jsonrpc_server_reconnect(struct ovsdb_jsonrpc_server *svr)
202 struct shash_node *node;
204 SHASH_FOR_EACH (node, &svr->remotes) {
205 struct ovsdb_jsonrpc_remote *remote = node->data;
207 ovsdb_jsonrpc_session_reconnect_all(remote);
212 ovsdb_jsonrpc_server_run(struct ovsdb_jsonrpc_server *svr)
214 struct shash_node *node;
216 SHASH_FOR_EACH (node, &svr->remotes) {
217 struct ovsdb_jsonrpc_remote *remote = node->data;
219 if (remote->listener && svr->n_sessions < svr->max_sessions) {
220 struct stream *stream;
223 error = pstream_accept(remote->listener, &stream);
225 struct jsonrpc_session *js;
226 js = jsonrpc_session_open_unreliably(jsonrpc_open(stream));
227 ovsdb_jsonrpc_session_create(remote, js);
228 } else if (error != EAGAIN) {
229 VLOG_WARN_RL(&rl, "%s: accept failed: %s",
230 pstream_get_name(remote->listener),
235 ovsdb_jsonrpc_session_run_all(remote);
240 ovsdb_jsonrpc_server_wait(struct ovsdb_jsonrpc_server *svr)
242 struct shash_node *node;
244 SHASH_FOR_EACH (node, &svr->remotes) {
245 struct ovsdb_jsonrpc_remote *remote = node->data;
247 if (remote->listener && svr->n_sessions < svr->max_sessions) {
248 pstream_wait(remote->listener);
251 ovsdb_jsonrpc_session_wait_all(remote);
255 /* JSON-RPC database server session. */
257 struct ovsdb_jsonrpc_session {
258 struct ovsdb_jsonrpc_remote *remote;
259 struct list node; /* Element in remote's sessions list. */
262 struct hmap triggers; /* Hmap of "struct ovsdb_jsonrpc_trigger"s. */
263 struct list completions; /* Completed triggers. */
266 struct hmap monitors; /* Hmap of "struct ovsdb_jsonrpc_monitor"s. */
268 /* Network connectivity. */
269 struct jsonrpc_session *js; /* JSON-RPC session. */
270 unsigned int js_seqno; /* Last jsonrpc_session_get_seqno() value. */
273 static void ovsdb_jsonrpc_session_close(struct ovsdb_jsonrpc_session *);
274 static int ovsdb_jsonrpc_session_run(struct ovsdb_jsonrpc_session *);
275 static void ovsdb_jsonrpc_session_wait(struct ovsdb_jsonrpc_session *);
276 static void ovsdb_jsonrpc_session_set_options(
277 struct ovsdb_jsonrpc_session *, const struct ovsdb_jsonrpc_options *);
278 static void ovsdb_jsonrpc_session_got_request(struct ovsdb_jsonrpc_session *,
279 struct jsonrpc_msg *);
280 static void ovsdb_jsonrpc_session_got_notify(struct ovsdb_jsonrpc_session *,
281 struct jsonrpc_msg *);
283 static struct ovsdb_jsonrpc_session *
284 ovsdb_jsonrpc_session_create(struct ovsdb_jsonrpc_remote *remote,
285 struct jsonrpc_session *js)
287 struct ovsdb_jsonrpc_session *s;
289 s = xzalloc(sizeof *s);
291 list_push_back(&remote->sessions, &s->node);
292 hmap_init(&s->triggers);
293 hmap_init(&s->monitors);
294 list_init(&s->completions);
296 s->js_seqno = jsonrpc_session_get_seqno(js);
298 remote->server->n_sessions++;
304 ovsdb_jsonrpc_session_close(struct ovsdb_jsonrpc_session *s)
306 ovsdb_jsonrpc_monitor_remove_all(s);
307 jsonrpc_session_close(s->js);
308 list_remove(&s->node);
309 s->remote->server->n_sessions--;
314 ovsdb_jsonrpc_session_run(struct ovsdb_jsonrpc_session *s)
316 jsonrpc_session_run(s->js);
317 if (s->js_seqno != jsonrpc_session_get_seqno(s->js)) {
318 s->js_seqno = jsonrpc_session_get_seqno(s->js);
319 ovsdb_jsonrpc_trigger_complete_all(s);
320 ovsdb_jsonrpc_monitor_remove_all(s);
323 ovsdb_jsonrpc_trigger_complete_done(s);
325 if (!jsonrpc_session_get_backlog(s->js)) {
326 struct jsonrpc_msg *msg = jsonrpc_session_recv(s->js);
328 if (msg->type == JSONRPC_REQUEST) {
329 ovsdb_jsonrpc_session_got_request(s, msg);
330 } else if (msg->type == JSONRPC_NOTIFY) {
331 ovsdb_jsonrpc_session_got_notify(s, msg);
333 VLOG_WARN("%s: received unexpected %s message",
334 jsonrpc_session_get_name(s->js),
335 jsonrpc_msg_type_to_string(msg->type));
336 jsonrpc_session_force_reconnect(s->js);
337 jsonrpc_msg_destroy(msg);
341 return jsonrpc_session_is_alive(s->js) ? 0 : ETIMEDOUT;
345 ovsdb_jsonrpc_session_set_options(struct ovsdb_jsonrpc_session *session,
346 const struct ovsdb_jsonrpc_options *options)
348 jsonrpc_session_set_max_backoff(session->js, options->max_backoff);
349 jsonrpc_session_set_probe_interval(session->js, options->probe_interval);
353 ovsdb_jsonrpc_session_run_all(struct ovsdb_jsonrpc_remote *remote)
355 struct ovsdb_jsonrpc_session *s, *next;
357 LIST_FOR_EACH_SAFE (s, next, node, &remote->sessions) {
358 int error = ovsdb_jsonrpc_session_run(s);
360 ovsdb_jsonrpc_session_close(s);
366 ovsdb_jsonrpc_session_wait(struct ovsdb_jsonrpc_session *s)
368 jsonrpc_session_wait(s->js);
369 if (!jsonrpc_session_get_backlog(s->js)) {
370 jsonrpc_session_recv_wait(s->js);
375 ovsdb_jsonrpc_session_wait_all(struct ovsdb_jsonrpc_remote *remote)
377 struct ovsdb_jsonrpc_session *s;
379 LIST_FOR_EACH (s, node, &remote->sessions) {
380 ovsdb_jsonrpc_session_wait(s);
385 ovsdb_jsonrpc_session_close_all(struct ovsdb_jsonrpc_remote *remote)
387 struct ovsdb_jsonrpc_session *s, *next;
389 LIST_FOR_EACH_SAFE (s, next, node, &remote->sessions) {
390 ovsdb_jsonrpc_session_close(s);
394 /* Forces all of the JSON-RPC sessions managed by 'remote' to disconnect and
397 ovsdb_jsonrpc_session_reconnect_all(struct ovsdb_jsonrpc_remote *remote)
399 struct ovsdb_jsonrpc_session *s, *next;
401 LIST_FOR_EACH_SAFE (s, next, node, &remote->sessions) {
402 jsonrpc_session_force_reconnect(s->js);
403 if (!jsonrpc_session_is_alive(s->js)) {
404 ovsdb_jsonrpc_session_close(s);
409 /* Sets the options for all of the JSON-RPC sessions managed by 'remote' to
412 ovsdb_jsonrpc_session_set_all_options(
413 struct ovsdb_jsonrpc_remote *remote,
414 const struct ovsdb_jsonrpc_options *options)
416 struct ovsdb_jsonrpc_session *s;
418 LIST_FOR_EACH (s, node, &remote->sessions) {
419 ovsdb_jsonrpc_session_set_options(s, options);
424 get_db_name(const struct ovsdb_jsonrpc_session *s)
426 return s->remote->server->db->schema->name;
429 static struct jsonrpc_msg *
430 ovsdb_jsonrpc_check_db_name(const struct ovsdb_jsonrpc_session *s,
431 const struct jsonrpc_msg *request)
433 struct json_array *params;
434 const char *want_db_name;
435 const char *have_db_name;
436 struct ovsdb_error *error;
437 struct jsonrpc_msg *reply;
439 params = json_array(request->params);
440 if (!params->n || params->elems[0]->type != JSON_STRING) {
441 error = ovsdb_syntax_error(
442 request->params, NULL,
443 "%s request params must begin with <db-name>", request->method);
447 want_db_name = params->elems[0]->u.string;
448 have_db_name = get_db_name(s);
449 if (strcmp(want_db_name, have_db_name)) {
450 error = ovsdb_syntax_error(
451 request->params, "unknown database",
452 "%s request specifies unknown database %s",
453 request->method, want_db_name);
460 reply = jsonrpc_create_reply(ovsdb_error_to_json(error), request->id);
461 ovsdb_error_destroy(error);
465 static struct jsonrpc_msg *
466 execute_transaction(struct ovsdb_jsonrpc_session *s,
467 struct jsonrpc_msg *request)
469 ovsdb_jsonrpc_trigger_create(s, request->id, request->params);
471 request->params = NULL;
472 jsonrpc_msg_destroy(request);
477 ovsdb_jsonrpc_session_got_request(struct ovsdb_jsonrpc_session *s,
478 struct jsonrpc_msg *request)
480 struct jsonrpc_msg *reply;
482 if (!strcmp(request->method, "transact")) {
483 reply = ovsdb_jsonrpc_check_db_name(s, request);
485 reply = execute_transaction(s, request);
487 } else if (!strcmp(request->method, "monitor")) {
488 reply = ovsdb_jsonrpc_check_db_name(s, request);
490 reply = jsonrpc_create_reply(
491 ovsdb_jsonrpc_monitor_create(s, request->params), request->id);
493 } else if (!strcmp(request->method, "monitor_cancel")) {
494 reply = ovsdb_jsonrpc_monitor_cancel(s, json_array(request->params),
496 } else if (!strcmp(request->method, "get_schema")) {
497 reply = ovsdb_jsonrpc_check_db_name(s, request);
499 reply = jsonrpc_create_reply(
500 ovsdb_schema_to_json(s->remote->server->db->schema),
503 } else if (!strcmp(request->method, "list_dbs")) {
504 reply = jsonrpc_create_reply(
505 json_array_create_1(json_string_create(get_db_name(s))),
507 } else if (!strcmp(request->method, "echo")) {
508 reply = jsonrpc_create_reply(json_clone(request->params), request->id);
510 reply = jsonrpc_create_error(json_string_create("unknown method"),
515 jsonrpc_msg_destroy(request);
516 jsonrpc_session_send(s->js, reply);
521 execute_cancel(struct ovsdb_jsonrpc_session *s, struct jsonrpc_msg *request)
523 if (json_array(request->params)->n == 1) {
524 struct ovsdb_jsonrpc_trigger *t;
527 id = request->params->u.array.elems[0];
528 t = ovsdb_jsonrpc_trigger_find(s, id, json_hash(id, 0));
530 ovsdb_jsonrpc_trigger_complete(t);
536 ovsdb_jsonrpc_session_got_notify(struct ovsdb_jsonrpc_session *s,
537 struct jsonrpc_msg *request)
539 if (!strcmp(request->method, "cancel")) {
540 execute_cancel(s, request);
542 jsonrpc_msg_destroy(request);
545 /* JSON-RPC database server triggers.
547 * (Every transaction is treated as a trigger even if it doesn't actually have
548 * any "wait" operations.) */
550 struct ovsdb_jsonrpc_trigger {
551 struct ovsdb_trigger trigger;
552 struct ovsdb_jsonrpc_session *session;
553 struct hmap_node hmap_node; /* In session's "triggers" hmap. */
558 ovsdb_jsonrpc_trigger_create(struct ovsdb_jsonrpc_session *s,
559 struct json *id, struct json *params)
561 struct ovsdb_jsonrpc_trigger *t;
564 /* Check for duplicate ID. */
565 hash = json_hash(id, 0);
566 t = ovsdb_jsonrpc_trigger_find(s, id, hash);
568 struct jsonrpc_msg *msg;
570 msg = jsonrpc_create_error(json_string_create("duplicate request ID"),
572 jsonrpc_session_send(s->js, msg);
574 json_destroy(params);
578 /* Insert into trigger table. */
579 t = xmalloc(sizeof *t);
580 ovsdb_trigger_init(s->remote->server->db,
581 &t->trigger, params, &s->completions,
585 hmap_insert(&s->triggers, &t->hmap_node, hash);
587 /* Complete early if possible. */
588 if (ovsdb_trigger_is_complete(&t->trigger)) {
589 ovsdb_jsonrpc_trigger_complete(t);
593 static struct ovsdb_jsonrpc_trigger *
594 ovsdb_jsonrpc_trigger_find(struct ovsdb_jsonrpc_session *s,
595 const struct json *id, size_t hash)
597 struct ovsdb_jsonrpc_trigger *t;
599 HMAP_FOR_EACH_WITH_HASH (t, hmap_node, hash, &s->triggers) {
600 if (json_equal(t->id, id)) {
609 ovsdb_jsonrpc_trigger_complete(struct ovsdb_jsonrpc_trigger *t)
611 struct ovsdb_jsonrpc_session *s = t->session;
613 if (jsonrpc_session_is_connected(s->js)) {
614 struct jsonrpc_msg *reply;
617 result = ovsdb_trigger_steal_result(&t->trigger);
619 reply = jsonrpc_create_reply(result, t->id);
621 reply = jsonrpc_create_error(json_string_create("canceled"),
624 jsonrpc_session_send(s->js, reply);
628 ovsdb_trigger_destroy(&t->trigger);
629 hmap_remove(&s->triggers, &t->hmap_node);
634 ovsdb_jsonrpc_trigger_complete_all(struct ovsdb_jsonrpc_session *s)
636 struct ovsdb_jsonrpc_trigger *t, *next;
637 HMAP_FOR_EACH_SAFE (t, next, hmap_node, &s->triggers) {
638 ovsdb_jsonrpc_trigger_complete(t);
643 ovsdb_jsonrpc_trigger_complete_done(struct ovsdb_jsonrpc_session *s)
645 while (!list_is_empty(&s->completions)) {
646 struct ovsdb_jsonrpc_trigger *t
647 = CONTAINER_OF(s->completions.next,
648 struct ovsdb_jsonrpc_trigger, trigger.node);
649 ovsdb_jsonrpc_trigger_complete(t);
653 /* JSON-RPC database table monitors. */
655 enum ovsdb_jsonrpc_monitor_selection {
656 OJMS_INITIAL = 1 << 0, /* All rows when monitor is created. */
657 OJMS_INSERT = 1 << 1, /* New rows. */
658 OJMS_DELETE = 1 << 2, /* Deleted rows. */
659 OJMS_MODIFY = 1 << 3 /* Modified rows. */
662 /* A particular column being monitored. */
663 struct ovsdb_jsonrpc_monitor_column {
664 const struct ovsdb_column *column;
665 enum ovsdb_jsonrpc_monitor_selection select;
668 /* A particular table being monitored. */
669 struct ovsdb_jsonrpc_monitor_table {
670 const struct ovsdb_table *table;
672 /* This is the union (bitwise-OR) of the 'select' values in all of the
673 * members of 'columns' below. */
674 enum ovsdb_jsonrpc_monitor_selection select;
676 /* Columns being monitored. */
677 struct ovsdb_jsonrpc_monitor_column *columns;
681 /* A collection of tables being monitored. */
682 struct ovsdb_jsonrpc_monitor {
683 struct ovsdb_replica replica;
684 struct ovsdb_jsonrpc_session *session;
685 struct hmap_node node; /* In ovsdb_jsonrpc_session's "monitors". */
687 struct json *monitor_id;
688 struct shash tables; /* Holds "struct ovsdb_jsonrpc_monitor_table"s. */
691 static const struct ovsdb_replica_class ovsdb_jsonrpc_replica_class;
693 struct ovsdb_jsonrpc_monitor *ovsdb_jsonrpc_monitor_find(
694 struct ovsdb_jsonrpc_session *, const struct json *monitor_id);
695 static void ovsdb_jsonrpc_monitor_destroy(struct ovsdb_replica *);
696 static struct json *ovsdb_jsonrpc_monitor_get_initial(
697 const struct ovsdb_jsonrpc_monitor *);
700 parse_bool(struct ovsdb_parser *parser, const char *name, bool default_value)
702 const struct json *json;
704 json = ovsdb_parser_member(parser, name, OP_BOOLEAN | OP_OPTIONAL);
705 return json ? json_boolean(json) : default_value;
708 struct ovsdb_jsonrpc_monitor *
709 ovsdb_jsonrpc_monitor_find(struct ovsdb_jsonrpc_session *s,
710 const struct json *monitor_id)
712 struct ovsdb_jsonrpc_monitor *m;
714 HMAP_FOR_EACH_WITH_HASH (m, node, json_hash(monitor_id, 0), &s->monitors) {
715 if (json_equal(m->monitor_id, monitor_id)) {
724 ovsdb_jsonrpc_add_monitor_column(struct ovsdb_jsonrpc_monitor_table *mt,
725 const struct ovsdb_column *column,
726 enum ovsdb_jsonrpc_monitor_selection select,
727 size_t *allocated_columns)
729 struct ovsdb_jsonrpc_monitor_column *c;
731 if (mt->n_columns >= *allocated_columns) {
732 mt->columns = x2nrealloc(mt->columns, allocated_columns,
733 sizeof *mt->columns);
736 c = &mt->columns[mt->n_columns++];
742 compare_ovsdb_jsonrpc_monitor_column(const void *a_, const void *b_)
744 const struct ovsdb_jsonrpc_monitor_column *a = a_;
745 const struct ovsdb_jsonrpc_monitor_column *b = b_;
747 return a->column < b->column ? -1 : a->column > b->column;
750 static struct ovsdb_error * WARN_UNUSED_RESULT
751 ovsdb_jsonrpc_parse_monitor_request(struct ovsdb_jsonrpc_monitor_table *mt,
752 const struct json *monitor_request,
753 size_t *allocated_columns)
755 const struct ovsdb_table_schema *ts = mt->table->schema;
756 enum ovsdb_jsonrpc_monitor_selection select;
757 const struct json *columns, *select_json;
758 struct ovsdb_parser parser;
759 struct ovsdb_error *error;
761 ovsdb_parser_init(&parser, monitor_request, "table %s", ts->name);
762 columns = ovsdb_parser_member(&parser, "columns", OP_ARRAY | OP_OPTIONAL);
763 select_json = ovsdb_parser_member(&parser, "select",
764 OP_OBJECT | OP_OPTIONAL);
765 error = ovsdb_parser_finish(&parser);
772 ovsdb_parser_init(&parser, select_json, "table %s select", ts->name);
773 if (parse_bool(&parser, "initial", true)) {
774 select |= OJMS_INITIAL;
776 if (parse_bool(&parser, "insert", true)) {
777 select |= OJMS_INSERT;
779 if (parse_bool(&parser, "delete", true)) {
780 select |= OJMS_DELETE;
782 if (parse_bool(&parser, "modify", true)) {
783 select |= OJMS_MODIFY;
785 error = ovsdb_parser_finish(&parser);
790 select = OJMS_INITIAL | OJMS_INSERT | OJMS_DELETE | OJMS_MODIFY;
792 mt->select |= select;
797 if (columns->type != JSON_ARRAY) {
798 return ovsdb_syntax_error(columns, NULL,
799 "array of column names expected");
802 for (i = 0; i < columns->u.array.n; i++) {
803 const struct ovsdb_column *column;
806 if (columns->u.array.elems[i]->type != JSON_STRING) {
807 return ovsdb_syntax_error(columns, NULL,
808 "array of column names expected");
811 s = columns->u.array.elems[i]->u.string;
812 column = shash_find_data(&mt->table->schema->columns, s);
814 return ovsdb_syntax_error(columns, NULL, "%s is not a valid "
817 ovsdb_jsonrpc_add_monitor_column(mt, column, select,
821 struct shash_node *node;
823 SHASH_FOR_EACH (node, &ts->columns) {
824 const struct ovsdb_column *column = node->data;
825 if (column->index != OVSDB_COL_UUID) {
826 ovsdb_jsonrpc_add_monitor_column(mt, column, select,
836 ovsdb_jsonrpc_monitor_create(struct ovsdb_jsonrpc_session *s,
839 struct ovsdb_jsonrpc_monitor *m = NULL;
840 struct json *monitor_id, *monitor_requests;
841 struct ovsdb_error *error = NULL;
842 struct shash_node *node;
845 if (json_array(params)->n != 3) {
846 error = ovsdb_syntax_error(params, NULL, "invalid parameters");
849 monitor_id = params->u.array.elems[1];
850 monitor_requests = params->u.array.elems[2];
851 if (monitor_requests->type != JSON_OBJECT) {
852 error = ovsdb_syntax_error(monitor_requests, NULL,
853 "monitor-requests must be object");
857 if (ovsdb_jsonrpc_monitor_find(s, monitor_id)) {
858 error = ovsdb_syntax_error(monitor_id, NULL, "duplicate monitor ID");
862 m = xzalloc(sizeof *m);
863 ovsdb_replica_init(&m->replica, &ovsdb_jsonrpc_replica_class);
864 ovsdb_add_replica(s->remote->server->db, &m->replica);
866 hmap_insert(&s->monitors, &m->node, json_hash(monitor_id, 0));
867 m->monitor_id = json_clone(monitor_id);
868 shash_init(&m->tables);
870 SHASH_FOR_EACH (node, json_object(monitor_requests)) {
871 const struct ovsdb_table *table;
872 struct ovsdb_jsonrpc_monitor_table *mt;
873 size_t allocated_columns;
874 const struct json *mr_value;
877 table = ovsdb_get_table(s->remote->server->db, node->name);
879 error = ovsdb_syntax_error(NULL, NULL,
880 "no table named %s", node->name);
884 mt = xzalloc(sizeof *mt);
886 shash_add(&m->tables, table->schema->name, mt);
889 mr_value = node->data;
890 allocated_columns = 0;
891 if (mr_value->type == JSON_ARRAY) {
892 const struct json_array *array = &mr_value->u.array;
894 for (i = 0; i < array->n; i++) {
895 error = ovsdb_jsonrpc_parse_monitor_request(
896 mt, array->elems[i], &allocated_columns);
902 error = ovsdb_jsonrpc_parse_monitor_request(
903 mt, mr_value, &allocated_columns);
909 /* Check for duplicate columns. */
910 qsort(mt->columns, mt->n_columns, sizeof *mt->columns,
911 compare_ovsdb_jsonrpc_monitor_column);
912 for (i = 1; i < mt->n_columns; i++) {
913 if (mt->columns[i].column == mt->columns[i - 1].column) {
914 error = ovsdb_syntax_error(mr_value, NULL, "column %s "
915 "mentioned more than once",
916 mt->columns[i].column->name);
922 return ovsdb_jsonrpc_monitor_get_initial(m);
926 ovsdb_remove_replica(s->remote->server->db, &m->replica);
929 json = ovsdb_error_to_json(error);
930 ovsdb_error_destroy(error);
934 static struct jsonrpc_msg *
935 ovsdb_jsonrpc_monitor_cancel(struct ovsdb_jsonrpc_session *s,
936 struct json_array *params,
937 const struct json *request_id)
939 if (params->n != 1) {
940 return jsonrpc_create_error(json_string_create("invalid parameters"),
943 struct ovsdb_jsonrpc_monitor *m;
945 m = ovsdb_jsonrpc_monitor_find(s, params->elems[0]);
947 return jsonrpc_create_error(json_string_create("unknown monitor"),
950 ovsdb_remove_replica(s->remote->server->db, &m->replica);
951 return jsonrpc_create_reply(json_object_create(), request_id);
957 ovsdb_jsonrpc_monitor_remove_all(struct ovsdb_jsonrpc_session *s)
959 struct ovsdb_jsonrpc_monitor *m, *next;
961 HMAP_FOR_EACH_SAFE (m, next, node, &s->monitors) {
962 ovsdb_remove_replica(s->remote->server->db, &m->replica);
966 static struct ovsdb_jsonrpc_monitor *
967 ovsdb_jsonrpc_monitor_cast(struct ovsdb_replica *replica)
969 assert(replica->class == &ovsdb_jsonrpc_replica_class);
970 return CONTAINER_OF(replica, struct ovsdb_jsonrpc_monitor, replica);
973 struct ovsdb_jsonrpc_monitor_aux {
974 bool initial; /* Sending initial contents of table? */
975 const struct ovsdb_jsonrpc_monitor *monitor;
976 struct json *json; /* JSON for the whole transaction. */
979 struct ovsdb_jsonrpc_monitor_table *mt;
980 struct json *table_json; /* JSON for table's transaction. */
984 any_reportable_change(const struct ovsdb_jsonrpc_monitor_table *mt,
985 const unsigned long int *changed)
989 for (i = 0; i < mt->n_columns; i++) {
990 const struct ovsdb_jsonrpc_monitor_column *c = &mt->columns[i];
991 unsigned int idx = c->column->index;
993 if (c->select & OJMS_MODIFY && bitmap_is_set(changed, idx)) {
1002 ovsdb_jsonrpc_monitor_change_cb(const struct ovsdb_row *old,
1003 const struct ovsdb_row *new,
1004 const unsigned long int *changed,
1007 struct ovsdb_jsonrpc_monitor_aux *aux = aux_;
1008 const struct ovsdb_jsonrpc_monitor *m = aux->monitor;
1009 struct ovsdb_table *table = new ? new->table : old->table;
1010 enum ovsdb_jsonrpc_monitor_selection type;
1011 struct json *old_json, *new_json;
1012 struct json *row_json;
1013 char uuid[UUID_LEN + 1];
1016 if (!aux->mt || table != aux->mt->table) {
1017 aux->mt = shash_find_data(&m->tables, table->schema->name);
1018 aux->table_json = NULL;
1020 /* We don't care about rows in this table at all. Tell the caller
1026 type = (aux->initial ? OJMS_INITIAL
1027 : !old ? OJMS_INSERT
1028 : !new ? OJMS_DELETE
1030 if (!(aux->mt->select & type)) {
1031 /* We don't care about this type of change (but do want to be called
1032 * back for changes to other rows in the same table). */
1036 if (type == OJMS_MODIFY && !any_reportable_change(aux->mt, changed)) {
1037 /* Nothing of interest changed. */
1041 old_json = new_json = NULL;
1042 if (type & (OJMS_DELETE | OJMS_MODIFY)) {
1043 old_json = json_object_create();
1045 if (type & (OJMS_INITIAL | OJMS_INSERT | OJMS_MODIFY)) {
1046 new_json = json_object_create();
1048 for (i = 0; i < aux->mt->n_columns; i++) {
1049 const struct ovsdb_jsonrpc_monitor_column *c = &aux->mt->columns[i];
1050 const struct ovsdb_column *column = c->column;
1051 unsigned int idx = c->column->index;
1053 if (!(type & c->select)) {
1054 /* We don't care about this type of change for this particular
1055 * column (but we will care about it for some other column). */
1059 if ((type == OJMS_MODIFY && bitmap_is_set(changed, idx))
1060 || type == OJMS_DELETE) {
1061 json_object_put(old_json, column->name,
1062 ovsdb_datum_to_json(&old->fields[idx],
1065 if (type & (OJMS_INITIAL | OJMS_INSERT | OJMS_MODIFY)) {
1066 json_object_put(new_json, column->name,
1067 ovsdb_datum_to_json(&new->fields[idx],
1072 /* Create JSON object for transaction overall. */
1074 aux->json = json_object_create();
1077 /* Create JSON object for transaction on this table. */
1078 if (!aux->table_json) {
1079 aux->table_json = json_object_create();
1080 json_object_put(aux->json, aux->mt->table->schema->name,
1084 /* Create JSON object for transaction on this row. */
1085 row_json = json_object_create();
1087 json_object_put(row_json, "old", old_json);
1090 json_object_put(row_json, "new", new_json);
1093 /* Add JSON row to JSON table. */
1094 snprintf(uuid, sizeof uuid,
1095 UUID_FMT, UUID_ARGS(ovsdb_row_get_uuid(new ? new : old)));
1096 json_object_put(aux->table_json, uuid, row_json);
1102 ovsdb_jsonrpc_monitor_init_aux(struct ovsdb_jsonrpc_monitor_aux *aux,
1103 const struct ovsdb_jsonrpc_monitor *m,
1106 aux->initial = initial;
1110 aux->table_json = NULL;
1113 static struct ovsdb_error *
1114 ovsdb_jsonrpc_monitor_commit(struct ovsdb_replica *replica,
1115 const struct ovsdb_txn *txn,
1116 bool durable OVS_UNUSED)
1118 struct ovsdb_jsonrpc_monitor *m = ovsdb_jsonrpc_monitor_cast(replica);
1119 struct ovsdb_jsonrpc_monitor_aux aux;
1121 ovsdb_jsonrpc_monitor_init_aux(&aux, m, false);
1122 ovsdb_txn_for_each_change(txn, ovsdb_jsonrpc_monitor_change_cb, &aux);
1124 struct jsonrpc_msg *msg;
1125 struct json *params;
1127 params = json_array_create_2(json_clone(aux.monitor->monitor_id),
1129 msg = jsonrpc_create_notify("update", params);
1130 jsonrpc_session_send(aux.monitor->session->js, msg);
1136 static struct json *
1137 ovsdb_jsonrpc_monitor_get_initial(const struct ovsdb_jsonrpc_monitor *m)
1139 struct ovsdb_jsonrpc_monitor_aux aux;
1140 struct shash_node *node;
1142 ovsdb_jsonrpc_monitor_init_aux(&aux, m, true);
1143 SHASH_FOR_EACH (node, &m->tables) {
1144 struct ovsdb_jsonrpc_monitor_table *mt = node->data;
1146 if (mt->select & OJMS_INITIAL) {
1147 struct ovsdb_row *row;
1149 HMAP_FOR_EACH (row, hmap_node, &mt->table->rows) {
1150 ovsdb_jsonrpc_monitor_change_cb(NULL, row, NULL, &aux);
1154 return aux.json ? aux.json : json_object_create();
1158 ovsdb_jsonrpc_monitor_destroy(struct ovsdb_replica *replica)
1160 struct ovsdb_jsonrpc_monitor *m = ovsdb_jsonrpc_monitor_cast(replica);
1161 struct shash_node *node;
1163 json_destroy(m->monitor_id);
1164 SHASH_FOR_EACH (node, &m->tables) {
1165 struct ovsdb_jsonrpc_monitor_table *mt = node->data;
1169 shash_destroy(&m->tables);
1170 hmap_remove(&m->session->monitors, &m->node);
1174 static const struct ovsdb_replica_class ovsdb_jsonrpc_replica_class = {
1175 ovsdb_jsonrpc_monitor_commit,
1176 ovsdb_jsonrpc_monitor_destroy