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 *);
56 static void ovsdb_jsonrpc_trigger_create(struct ovsdb_jsonrpc_session *,
57 struct json *id, struct json *params);
58 static struct ovsdb_jsonrpc_trigger *ovsdb_jsonrpc_trigger_find(
59 struct ovsdb_jsonrpc_session *, const struct json *id, size_t hash);
60 static void ovsdb_jsonrpc_trigger_complete(struct ovsdb_jsonrpc_trigger *);
61 static void ovsdb_jsonrpc_trigger_complete_all(struct ovsdb_jsonrpc_session *);
62 static void ovsdb_jsonrpc_trigger_complete_done(
63 struct ovsdb_jsonrpc_session *);
66 static struct json *ovsdb_jsonrpc_monitor_create(
67 struct ovsdb_jsonrpc_session *, struct json *params);
68 static struct jsonrpc_msg *ovsdb_jsonrpc_monitor_cancel(
69 struct ovsdb_jsonrpc_session *,
70 struct json_array *params,
71 const struct json *request_id);
72 static void ovsdb_jsonrpc_monitor_remove_all(struct ovsdb_jsonrpc_session *);
74 /* JSON-RPC database server. */
76 struct ovsdb_jsonrpc_server {
78 unsigned int n_sessions, max_sessions;
79 struct shash remotes; /* Contains "struct ovsdb_jsonrpc_remote *"s. */
82 /* A configured remote. This is either a passive stream listener plus a list
83 * of the currently connected sessions, or a list of exactly one active
85 struct ovsdb_jsonrpc_remote {
86 struct ovsdb_jsonrpc_server *server;
87 struct pstream *listener; /* Listener, if passive. */
88 struct list sessions; /* List of "struct ovsdb_jsonrpc_session"s. */
91 static void ovsdb_jsonrpc_server_add_remote(struct ovsdb_jsonrpc_server *,
93 static void ovsdb_jsonrpc_server_del_remote(struct shash_node *);
95 struct ovsdb_jsonrpc_server *
96 ovsdb_jsonrpc_server_create(struct ovsdb *db)
98 struct ovsdb_jsonrpc_server *server = xzalloc(sizeof *server);
100 server->max_sessions = 64;
101 shash_init(&server->remotes);
106 ovsdb_jsonrpc_server_destroy(struct ovsdb_jsonrpc_server *svr)
108 struct shash_node *node, *next;
110 SHASH_FOR_EACH_SAFE (node, next, &svr->remotes) {
111 ovsdb_jsonrpc_server_del_remote(node);
113 shash_destroy(&svr->remotes);
117 /* Sets 'svr''s current set of remotes to the names in 'new_remotes'. The data
118 * values in 'new_remotes' are ignored.
120 * A remote is an active or passive stream connection method, e.g. "pssl:" or
123 ovsdb_jsonrpc_server_set_remotes(struct ovsdb_jsonrpc_server *svr,
124 const struct shash *new_remotes)
126 struct shash_node *node, *next;
128 SHASH_FOR_EACH_SAFE (node, next, &svr->remotes) {
129 if (!shash_find(new_remotes, node->name)) {
130 ovsdb_jsonrpc_server_del_remote(node);
133 SHASH_FOR_EACH (node, new_remotes) {
134 if (!shash_find(&svr->remotes, node->name)) {
135 ovsdb_jsonrpc_server_add_remote(svr, node->name);
141 ovsdb_jsonrpc_server_add_remote(struct ovsdb_jsonrpc_server *svr,
144 struct ovsdb_jsonrpc_remote *remote;
145 struct pstream *listener;
148 error = jsonrpc_pstream_open(name, &listener);
149 if (error && error != EAFNOSUPPORT) {
150 VLOG_ERR_RL(&rl, "%s: listen failed: %s", name, strerror(error));
154 remote = xmalloc(sizeof *remote);
155 remote->server = svr;
156 remote->listener = listener;
157 list_init(&remote->sessions);
158 shash_add(&svr->remotes, name, remote);
161 ovsdb_jsonrpc_session_create(remote, jsonrpc_session_open(name));
166 ovsdb_jsonrpc_server_del_remote(struct shash_node *node)
168 struct ovsdb_jsonrpc_remote *remote = node->data;
170 ovsdb_jsonrpc_session_close_all(remote);
171 pstream_close(remote->listener);
172 shash_delete(&remote->server->remotes, node);
176 /* Forces all of the JSON-RPC sessions managed by 'svr' to disconnect and
179 ovsdb_jsonrpc_server_reconnect(struct ovsdb_jsonrpc_server *svr)
181 struct shash_node *node;
183 SHASH_FOR_EACH (node, &svr->remotes) {
184 struct ovsdb_jsonrpc_remote *remote = node->data;
186 ovsdb_jsonrpc_session_reconnect_all(remote);
191 ovsdb_jsonrpc_server_run(struct ovsdb_jsonrpc_server *svr)
193 struct shash_node *node;
195 SHASH_FOR_EACH (node, &svr->remotes) {
196 struct ovsdb_jsonrpc_remote *remote = node->data;
198 if (remote->listener && svr->n_sessions < svr->max_sessions) {
199 struct stream *stream;
202 error = pstream_accept(remote->listener, &stream);
204 struct jsonrpc_session *js;
205 js = jsonrpc_session_open_unreliably(jsonrpc_open(stream));
206 ovsdb_jsonrpc_session_create(remote, js);
207 } else if (error != EAGAIN) {
208 VLOG_WARN_RL(&rl, "%s: accept failed: %s",
209 pstream_get_name(remote->listener),
214 ovsdb_jsonrpc_session_run_all(remote);
219 ovsdb_jsonrpc_server_wait(struct ovsdb_jsonrpc_server *svr)
221 struct shash_node *node;
223 SHASH_FOR_EACH (node, &svr->remotes) {
224 struct ovsdb_jsonrpc_remote *remote = node->data;
226 if (remote->listener && svr->n_sessions < svr->max_sessions) {
227 pstream_wait(remote->listener);
230 ovsdb_jsonrpc_session_wait_all(remote);
234 /* JSON-RPC database server session. */
236 struct ovsdb_jsonrpc_session {
237 struct ovsdb_jsonrpc_remote *remote;
238 struct list node; /* Element in remote's sessions list. */
241 struct hmap triggers; /* Hmap of "struct ovsdb_jsonrpc_trigger"s. */
242 struct list completions; /* Completed triggers. */
245 struct hmap monitors; /* Hmap of "struct ovsdb_jsonrpc_monitor"s. */
247 /* Network connectivity. */
248 struct jsonrpc_session *js; /* JSON-RPC session. */
249 unsigned int js_seqno; /* Last jsonrpc_session_get_seqno() value. */
252 static void ovsdb_jsonrpc_session_close(struct ovsdb_jsonrpc_session *);
253 static int ovsdb_jsonrpc_session_run(struct ovsdb_jsonrpc_session *);
254 static void ovsdb_jsonrpc_session_wait(struct ovsdb_jsonrpc_session *);
255 static void ovsdb_jsonrpc_session_got_request(struct ovsdb_jsonrpc_session *,
256 struct jsonrpc_msg *);
257 static void ovsdb_jsonrpc_session_got_notify(struct ovsdb_jsonrpc_session *,
258 struct jsonrpc_msg *);
260 static struct ovsdb_jsonrpc_session *
261 ovsdb_jsonrpc_session_create(struct ovsdb_jsonrpc_remote *remote,
262 struct jsonrpc_session *js)
264 struct ovsdb_jsonrpc_session *s;
266 s = xzalloc(sizeof *s);
268 list_push_back(&remote->sessions, &s->node);
269 hmap_init(&s->triggers);
270 hmap_init(&s->monitors);
271 list_init(&s->completions);
273 s->js_seqno = jsonrpc_session_get_seqno(js);
275 remote->server->n_sessions++;
281 ovsdb_jsonrpc_session_close(struct ovsdb_jsonrpc_session *s)
283 ovsdb_jsonrpc_monitor_remove_all(s);
284 jsonrpc_session_close(s->js);
285 list_remove(&s->node);
286 s->remote->server->n_sessions--;
291 ovsdb_jsonrpc_session_run(struct ovsdb_jsonrpc_session *s)
293 jsonrpc_session_run(s->js);
294 if (s->js_seqno != jsonrpc_session_get_seqno(s->js)) {
295 s->js_seqno = jsonrpc_session_get_seqno(s->js);
296 ovsdb_jsonrpc_trigger_complete_all(s);
297 ovsdb_jsonrpc_monitor_remove_all(s);
300 ovsdb_jsonrpc_trigger_complete_done(s);
302 if (!jsonrpc_session_get_backlog(s->js)) {
303 struct jsonrpc_msg *msg = jsonrpc_session_recv(s->js);
305 if (msg->type == JSONRPC_REQUEST) {
306 ovsdb_jsonrpc_session_got_request(s, msg);
307 } else if (msg->type == JSONRPC_NOTIFY) {
308 ovsdb_jsonrpc_session_got_notify(s, msg);
310 VLOG_WARN("%s: received unexpected %s message",
311 jsonrpc_session_get_name(s->js),
312 jsonrpc_msg_type_to_string(msg->type));
313 jsonrpc_session_force_reconnect(s->js);
314 jsonrpc_msg_destroy(msg);
318 return jsonrpc_session_is_alive(s->js) ? 0 : ETIMEDOUT;
322 ovsdb_jsonrpc_session_run_all(struct ovsdb_jsonrpc_remote *remote)
324 struct ovsdb_jsonrpc_session *s, *next;
326 LIST_FOR_EACH_SAFE (s, next, struct ovsdb_jsonrpc_session, node,
328 int error = ovsdb_jsonrpc_session_run(s);
330 ovsdb_jsonrpc_session_close(s);
336 ovsdb_jsonrpc_session_wait(struct ovsdb_jsonrpc_session *s)
338 jsonrpc_session_wait(s->js);
339 if (!jsonrpc_session_get_backlog(s->js)) {
340 jsonrpc_session_recv_wait(s->js);
345 ovsdb_jsonrpc_session_wait_all(struct ovsdb_jsonrpc_remote *remote)
347 struct ovsdb_jsonrpc_session *s;
349 LIST_FOR_EACH (s, struct ovsdb_jsonrpc_session, node, &remote->sessions) {
350 ovsdb_jsonrpc_session_wait(s);
355 ovsdb_jsonrpc_session_close_all(struct ovsdb_jsonrpc_remote *remote)
357 struct ovsdb_jsonrpc_session *s, *next;
359 LIST_FOR_EACH_SAFE (s, next, struct ovsdb_jsonrpc_session, node,
361 ovsdb_jsonrpc_session_close(s);
365 /* Forces all of the JSON-RPC sessions managed by 'remote' to disconnect and
368 ovsdb_jsonrpc_session_reconnect_all(struct ovsdb_jsonrpc_remote *remote)
370 struct ovsdb_jsonrpc_session *s, *next;
372 LIST_FOR_EACH_SAFE (s, next, struct ovsdb_jsonrpc_session, node,
374 jsonrpc_session_force_reconnect(s->js);
375 if (!jsonrpc_session_is_alive(s->js)) {
376 ovsdb_jsonrpc_session_close(s);
382 get_db_name(const struct ovsdb_jsonrpc_session *s)
384 return s->remote->server->db->schema->name;
387 static struct jsonrpc_msg *
388 ovsdb_jsonrpc_check_db_name(const struct ovsdb_jsonrpc_session *s,
389 const struct jsonrpc_msg *request)
391 struct json_array *params;
392 const char *want_db_name;
393 const char *have_db_name;
394 struct ovsdb_error *error;
395 struct jsonrpc_msg *reply;
397 params = json_array(request->params);
398 if (!params->n || params->elems[0]->type != JSON_STRING) {
399 error = ovsdb_syntax_error(
400 request->params, NULL,
401 "%s request params must begin with <db-name>", request->method);
405 want_db_name = params->elems[0]->u.string;
406 have_db_name = get_db_name(s);
407 if (strcmp(want_db_name, have_db_name)) {
408 error = ovsdb_syntax_error(
409 request->params, "unknown database",
410 "%s request specifies unknown database %s",
411 request->method, want_db_name);
418 reply = jsonrpc_create_reply(ovsdb_error_to_json(error), request->id);
419 ovsdb_error_destroy(error);
423 static struct jsonrpc_msg *
424 execute_transaction(struct ovsdb_jsonrpc_session *s,
425 struct jsonrpc_msg *request)
427 ovsdb_jsonrpc_trigger_create(s, request->id, request->params);
429 request->params = NULL;
430 jsonrpc_msg_destroy(request);
435 ovsdb_jsonrpc_session_got_request(struct ovsdb_jsonrpc_session *s,
436 struct jsonrpc_msg *request)
438 struct jsonrpc_msg *reply;
440 if (!strcmp(request->method, "transact")) {
441 reply = ovsdb_jsonrpc_check_db_name(s, request);
443 reply = execute_transaction(s, request);
445 } else if (!strcmp(request->method, "monitor")) {
446 reply = ovsdb_jsonrpc_check_db_name(s, request);
448 reply = jsonrpc_create_reply(
449 ovsdb_jsonrpc_monitor_create(s, request->params), request->id);
451 } else if (!strcmp(request->method, "monitor_cancel")) {
452 reply = ovsdb_jsonrpc_monitor_cancel(s, json_array(request->params),
454 } else if (!strcmp(request->method, "get_schema")) {
455 reply = ovsdb_jsonrpc_check_db_name(s, request);
457 reply = jsonrpc_create_reply(
458 ovsdb_schema_to_json(s->remote->server->db->schema),
461 } else if (!strcmp(request->method, "list_dbs")) {
462 reply = jsonrpc_create_reply(
463 json_array_create_1(json_string_create(get_db_name(s))),
465 } else if (!strcmp(request->method, "echo")) {
466 reply = jsonrpc_create_reply(json_clone(request->params), request->id);
468 reply = jsonrpc_create_error(json_string_create("unknown method"),
473 jsonrpc_msg_destroy(request);
474 jsonrpc_session_send(s->js, reply);
479 execute_cancel(struct ovsdb_jsonrpc_session *s, struct jsonrpc_msg *request)
481 if (json_array(request->params)->n == 1) {
482 struct ovsdb_jsonrpc_trigger *t;
485 id = request->params->u.array.elems[0];
486 t = ovsdb_jsonrpc_trigger_find(s, id, json_hash(id, 0));
488 ovsdb_jsonrpc_trigger_complete(t);
494 ovsdb_jsonrpc_session_got_notify(struct ovsdb_jsonrpc_session *s,
495 struct jsonrpc_msg *request)
497 if (!strcmp(request->method, "cancel")) {
498 execute_cancel(s, request);
500 jsonrpc_msg_destroy(request);
503 /* JSON-RPC database server triggers.
505 * (Every transaction is treated as a trigger even if it doesn't actually have
506 * any "wait" operations.) */
508 struct ovsdb_jsonrpc_trigger {
509 struct ovsdb_trigger trigger;
510 struct ovsdb_jsonrpc_session *session;
511 struct hmap_node hmap_node; /* In session's "triggers" hmap. */
516 ovsdb_jsonrpc_trigger_create(struct ovsdb_jsonrpc_session *s,
517 struct json *id, struct json *params)
519 struct ovsdb_jsonrpc_trigger *t;
522 /* Check for duplicate ID. */
523 hash = json_hash(id, 0);
524 t = ovsdb_jsonrpc_trigger_find(s, id, hash);
526 struct jsonrpc_msg *msg;
528 msg = jsonrpc_create_error(json_string_create("duplicate request ID"),
530 jsonrpc_session_send(s->js, msg);
532 json_destroy(params);
536 /* Insert into trigger table. */
537 t = xmalloc(sizeof *t);
538 ovsdb_trigger_init(s->remote->server->db,
539 &t->trigger, params, &s->completions,
543 hmap_insert(&s->triggers, &t->hmap_node, hash);
545 /* Complete early if possible. */
546 if (ovsdb_trigger_is_complete(&t->trigger)) {
547 ovsdb_jsonrpc_trigger_complete(t);
551 static struct ovsdb_jsonrpc_trigger *
552 ovsdb_jsonrpc_trigger_find(struct ovsdb_jsonrpc_session *s,
553 const struct json *id, size_t hash)
555 struct ovsdb_jsonrpc_trigger *t;
557 HMAP_FOR_EACH_WITH_HASH (t, struct ovsdb_jsonrpc_trigger, hmap_node, hash,
559 if (json_equal(t->id, id)) {
568 ovsdb_jsonrpc_trigger_complete(struct ovsdb_jsonrpc_trigger *t)
570 struct ovsdb_jsonrpc_session *s = t->session;
572 if (jsonrpc_session_is_connected(s->js)) {
573 struct jsonrpc_msg *reply;
576 result = ovsdb_trigger_steal_result(&t->trigger);
578 reply = jsonrpc_create_reply(result, t->id);
580 reply = jsonrpc_create_error(json_string_create("canceled"),
583 jsonrpc_session_send(s->js, reply);
587 ovsdb_trigger_destroy(&t->trigger);
588 hmap_remove(&s->triggers, &t->hmap_node);
593 ovsdb_jsonrpc_trigger_complete_all(struct ovsdb_jsonrpc_session *s)
595 struct ovsdb_jsonrpc_trigger *t, *next;
596 HMAP_FOR_EACH_SAFE (t, next, struct ovsdb_jsonrpc_trigger, hmap_node,
598 ovsdb_jsonrpc_trigger_complete(t);
603 ovsdb_jsonrpc_trigger_complete_done(struct ovsdb_jsonrpc_session *s)
605 while (!list_is_empty(&s->completions)) {
606 struct ovsdb_jsonrpc_trigger *t
607 = CONTAINER_OF(s->completions.next,
608 struct ovsdb_jsonrpc_trigger, trigger.node);
609 ovsdb_jsonrpc_trigger_complete(t);
613 /* JSON-RPC database table monitors. */
615 enum ovsdb_jsonrpc_monitor_selection {
616 OJMS_INITIAL = 1 << 0, /* All rows when monitor is created. */
617 OJMS_INSERT = 1 << 1, /* New rows. */
618 OJMS_DELETE = 1 << 2, /* Deleted rows. */
619 OJMS_MODIFY = 1 << 3 /* Modified rows. */
622 /* A particular column being monitored. */
623 struct ovsdb_jsonrpc_monitor_column {
624 const struct ovsdb_column *column;
625 enum ovsdb_jsonrpc_monitor_selection select;
628 /* A particular table being monitored. */
629 struct ovsdb_jsonrpc_monitor_table {
630 const struct ovsdb_table *table;
632 /* This is the union (bitwise-OR) of the 'select' values in all of the
633 * members of 'columns' below. */
634 enum ovsdb_jsonrpc_monitor_selection select;
636 /* Columns being monitored. */
637 struct ovsdb_jsonrpc_monitor_column *columns;
641 /* A collection of tables being monitored. */
642 struct ovsdb_jsonrpc_monitor {
643 struct ovsdb_replica replica;
644 struct ovsdb_jsonrpc_session *session;
645 struct hmap_node node; /* In ovsdb_jsonrpc_session's "monitors". */
647 struct json *monitor_id;
648 struct shash tables; /* Holds "struct ovsdb_jsonrpc_monitor_table"s. */
651 static const struct ovsdb_replica_class ovsdb_jsonrpc_replica_class;
653 struct ovsdb_jsonrpc_monitor *ovsdb_jsonrpc_monitor_find(
654 struct ovsdb_jsonrpc_session *, const struct json *monitor_id);
655 static void ovsdb_jsonrpc_monitor_destroy(struct ovsdb_replica *);
656 static struct json *ovsdb_jsonrpc_monitor_get_initial(
657 const struct ovsdb_jsonrpc_monitor *);
660 parse_bool(struct ovsdb_parser *parser, const char *name, bool default_value)
662 const struct json *json;
664 json = ovsdb_parser_member(parser, name, OP_BOOLEAN | OP_OPTIONAL);
665 return json ? json_boolean(json) : default_value;
668 struct ovsdb_jsonrpc_monitor *
669 ovsdb_jsonrpc_monitor_find(struct ovsdb_jsonrpc_session *s,
670 const struct json *monitor_id)
672 struct ovsdb_jsonrpc_monitor *m;
674 HMAP_FOR_EACH_WITH_HASH (m, struct ovsdb_jsonrpc_monitor, node,
675 json_hash(monitor_id, 0), &s->monitors) {
676 if (json_equal(m->monitor_id, monitor_id)) {
685 ovsdb_jsonrpc_add_monitor_column(struct ovsdb_jsonrpc_monitor_table *mt,
686 const struct ovsdb_column *column,
687 enum ovsdb_jsonrpc_monitor_selection select,
688 size_t *allocated_columns)
690 struct ovsdb_jsonrpc_monitor_column *c;
692 if (mt->n_columns >= *allocated_columns) {
693 mt->columns = x2nrealloc(mt->columns, allocated_columns,
694 sizeof *mt->columns);
697 c = &mt->columns[mt->n_columns++];
703 compare_ovsdb_jsonrpc_monitor_column(const void *a_, const void *b_)
705 const struct ovsdb_jsonrpc_monitor_column *a = a_;
706 const struct ovsdb_jsonrpc_monitor_column *b = b_;
708 return a->column < b->column ? -1 : a->column > b->column;
711 static struct ovsdb_error * WARN_UNUSED_RESULT
712 ovsdb_jsonrpc_parse_monitor_request(struct ovsdb_jsonrpc_monitor_table *mt,
713 const struct json *monitor_request,
714 size_t *allocated_columns)
716 const struct ovsdb_table_schema *ts = mt->table->schema;
717 enum ovsdb_jsonrpc_monitor_selection select;
718 const struct json *columns, *select_json;
719 struct ovsdb_parser parser;
720 struct ovsdb_error *error;
722 ovsdb_parser_init(&parser, monitor_request, "table %s", ts->name);
723 columns = ovsdb_parser_member(&parser, "columns", OP_ARRAY | OP_OPTIONAL);
724 select_json = ovsdb_parser_member(&parser, "select",
725 OP_OBJECT | OP_OPTIONAL);
726 error = ovsdb_parser_finish(&parser);
733 ovsdb_parser_init(&parser, select_json, "table %s select", ts->name);
734 if (parse_bool(&parser, "initial", true)) {
735 select |= OJMS_INITIAL;
737 if (parse_bool(&parser, "insert", true)) {
738 select |= OJMS_INSERT;
740 if (parse_bool(&parser, "delete", true)) {
741 select |= OJMS_DELETE;
743 if (parse_bool(&parser, "modify", true)) {
744 select |= OJMS_MODIFY;
746 error = ovsdb_parser_finish(&parser);
751 select = OJMS_INITIAL | OJMS_INSERT | OJMS_DELETE | OJMS_MODIFY;
753 mt->select |= select;
758 if (columns->type != JSON_ARRAY) {
759 return ovsdb_syntax_error(columns, NULL,
760 "array of column names expected");
763 for (i = 0; i < columns->u.array.n; i++) {
764 const struct ovsdb_column *column;
767 if (columns->u.array.elems[i]->type != JSON_STRING) {
768 return ovsdb_syntax_error(columns, NULL,
769 "array of column names expected");
772 s = columns->u.array.elems[i]->u.string;
773 column = shash_find_data(&mt->table->schema->columns, s);
775 return ovsdb_syntax_error(columns, NULL, "%s is not a valid "
778 ovsdb_jsonrpc_add_monitor_column(mt, column, select,
782 struct shash_node *node;
784 SHASH_FOR_EACH (node, &ts->columns) {
785 const struct ovsdb_column *column = node->data;
786 if (column->index != OVSDB_COL_UUID) {
787 ovsdb_jsonrpc_add_monitor_column(mt, column, select,
797 ovsdb_jsonrpc_monitor_create(struct ovsdb_jsonrpc_session *s,
800 struct ovsdb_jsonrpc_monitor *m = NULL;
801 struct json *monitor_id, *monitor_requests;
802 struct ovsdb_error *error = NULL;
803 struct shash_node *node;
806 if (json_array(params)->n != 3) {
807 error = ovsdb_syntax_error(params, NULL, "invalid parameters");
810 monitor_id = params->u.array.elems[1];
811 monitor_requests = params->u.array.elems[2];
812 if (monitor_requests->type != JSON_OBJECT) {
813 error = ovsdb_syntax_error(monitor_requests, NULL,
814 "monitor-requests must be object");
818 if (ovsdb_jsonrpc_monitor_find(s, monitor_id)) {
819 error = ovsdb_syntax_error(monitor_id, NULL, "duplicate monitor ID");
823 m = xzalloc(sizeof *m);
824 ovsdb_replica_init(&m->replica, &ovsdb_jsonrpc_replica_class);
825 ovsdb_add_replica(s->remote->server->db, &m->replica);
827 hmap_insert(&s->monitors, &m->node, json_hash(monitor_id, 0));
828 m->monitor_id = json_clone(monitor_id);
829 shash_init(&m->tables);
831 SHASH_FOR_EACH (node, json_object(monitor_requests)) {
832 const struct ovsdb_table *table;
833 struct ovsdb_jsonrpc_monitor_table *mt;
834 size_t allocated_columns;
835 const struct json *mr_value;
838 table = ovsdb_get_table(s->remote->server->db, node->name);
840 error = ovsdb_syntax_error(NULL, NULL,
841 "no table named %s", node->name);
845 mt = xzalloc(sizeof *mt);
847 shash_add(&m->tables, table->schema->name, mt);
850 mr_value = node->data;
851 allocated_columns = 0;
852 if (mr_value->type == JSON_ARRAY) {
853 const struct json_array *array = &mr_value->u.array;
855 for (i = 0; i < array->n; i++) {
856 error = ovsdb_jsonrpc_parse_monitor_request(
857 mt, array->elems[i], &allocated_columns);
863 error = ovsdb_jsonrpc_parse_monitor_request(
864 mt, mr_value, &allocated_columns);
870 /* Check for duplicate columns. */
871 qsort(mt->columns, mt->n_columns, sizeof *mt->columns,
872 compare_ovsdb_jsonrpc_monitor_column);
873 for (i = 1; i < mt->n_columns; i++) {
874 if (mt->columns[i].column == mt->columns[i - 1].column) {
875 error = ovsdb_syntax_error(mr_value, NULL, "column %s "
876 "mentioned more than once",
877 mt->columns[i].column->name);
883 return ovsdb_jsonrpc_monitor_get_initial(m);
887 ovsdb_remove_replica(s->remote->server->db, &m->replica);
890 json = ovsdb_error_to_json(error);
891 ovsdb_error_destroy(error);
895 static struct jsonrpc_msg *
896 ovsdb_jsonrpc_monitor_cancel(struct ovsdb_jsonrpc_session *s,
897 struct json_array *params,
898 const struct json *request_id)
900 if (params->n != 1) {
901 return jsonrpc_create_error(json_string_create("invalid parameters"),
904 struct ovsdb_jsonrpc_monitor *m;
906 m = ovsdb_jsonrpc_monitor_find(s, params->elems[0]);
908 return jsonrpc_create_error(json_string_create("unknown monitor"),
911 ovsdb_remove_replica(s->remote->server->db, &m->replica);
912 return jsonrpc_create_reply(json_object_create(), request_id);
918 ovsdb_jsonrpc_monitor_remove_all(struct ovsdb_jsonrpc_session *s)
920 struct ovsdb_jsonrpc_monitor *m, *next;
922 HMAP_FOR_EACH_SAFE (m, next,
923 struct ovsdb_jsonrpc_monitor, node, &s->monitors) {
924 ovsdb_remove_replica(s->remote->server->db, &m->replica);
928 static struct ovsdb_jsonrpc_monitor *
929 ovsdb_jsonrpc_monitor_cast(struct ovsdb_replica *replica)
931 assert(replica->class == &ovsdb_jsonrpc_replica_class);
932 return CONTAINER_OF(replica, struct ovsdb_jsonrpc_monitor, replica);
935 struct ovsdb_jsonrpc_monitor_aux {
936 bool initial; /* Sending initial contents of table? */
937 const struct ovsdb_jsonrpc_monitor *monitor;
938 struct json *json; /* JSON for the whole transaction. */
941 struct ovsdb_jsonrpc_monitor_table *mt;
942 struct json *table_json; /* JSON for table's transaction. */
946 ovsdb_jsonrpc_monitor_change_cb(const struct ovsdb_row *old,
947 const struct ovsdb_row *new,
948 const unsigned long int *changed,
951 struct ovsdb_jsonrpc_monitor_aux *aux = aux_;
952 const struct ovsdb_jsonrpc_monitor *m = aux->monitor;
953 struct ovsdb_table *table = new ? new->table : old->table;
954 enum ovsdb_jsonrpc_monitor_selection type;
955 struct json *old_json, *new_json;
956 struct json *row_json;
957 char uuid[UUID_LEN + 1];
961 if (!aux->mt || table != aux->mt->table) {
962 aux->mt = shash_find_data(&m->tables, table->schema->name);
963 aux->table_json = NULL;
965 /* We don't care about rows in this table at all. Tell the caller
971 type = (aux->initial ? OJMS_INITIAL
975 if (!(aux->mt->select & type)) {
976 /* We don't care about this type of change (but do want to be called
977 * back for changes to other rows in the same table). */
981 old_json = new_json = NULL;
983 for (i = 0; i < aux->mt->n_columns; i++) {
984 const struct ovsdb_jsonrpc_monitor_column *c = &aux->mt->columns[i];
985 const struct ovsdb_column *column = c->column;
986 unsigned int idx = c->column->index;
987 bool column_changed = false;
989 if (!(type & c->select)) {
990 /* We don't care about this type of change for this particular
991 * column (but we will care about it for some other column). */
995 if (type == OJMS_MODIFY) {
996 column_changed = bitmap_is_set(changed, idx);
997 n_changed += column_changed;
999 if (column_changed || type == OJMS_DELETE) {
1001 old_json = json_object_create();
1003 json_object_put(old_json, column->name,
1004 ovsdb_datum_to_json(&old->fields[idx],
1007 if (type & (OJMS_INITIAL | OJMS_INSERT | OJMS_MODIFY)) {
1009 new_json = json_object_create();
1011 json_object_put(new_json, column->name,
1012 ovsdb_datum_to_json(&new->fields[idx],
1016 if ((type == OJMS_MODIFY && !n_changed) || (!old_json && !new_json)) {
1017 /* No reportable changes. */
1018 json_destroy(old_json);
1019 json_destroy(new_json);
1023 /* Create JSON object for transaction overall. */
1025 aux->json = json_object_create();
1028 /* Create JSON object for transaction on this table. */
1029 if (!aux->table_json) {
1030 aux->table_json = json_object_create();
1031 json_object_put(aux->json, aux->mt->table->schema->name,
1035 /* Create JSON object for transaction on this row. */
1036 row_json = json_object_create();
1038 json_object_put(row_json, "old", old_json);
1041 json_object_put(row_json, "new", new_json);
1044 /* Add JSON row to JSON table. */
1045 snprintf(uuid, sizeof uuid,
1046 UUID_FMT, UUID_ARGS(ovsdb_row_get_uuid(new ? new : old)));
1047 json_object_put(aux->table_json, uuid, row_json);
1053 ovsdb_jsonrpc_monitor_init_aux(struct ovsdb_jsonrpc_monitor_aux *aux,
1054 const struct ovsdb_jsonrpc_monitor *m,
1057 aux->initial = initial;
1061 aux->table_json = NULL;
1064 static struct ovsdb_error *
1065 ovsdb_jsonrpc_monitor_commit(struct ovsdb_replica *replica,
1066 const struct ovsdb_txn *txn,
1067 bool durable OVS_UNUSED)
1069 struct ovsdb_jsonrpc_monitor *m = ovsdb_jsonrpc_monitor_cast(replica);
1070 struct ovsdb_jsonrpc_monitor_aux aux;
1072 ovsdb_jsonrpc_monitor_init_aux(&aux, m, false);
1073 ovsdb_txn_for_each_change(txn, ovsdb_jsonrpc_monitor_change_cb, &aux);
1075 struct jsonrpc_msg *msg;
1076 struct json *params;
1078 params = json_array_create_2(json_clone(aux.monitor->monitor_id),
1080 msg = jsonrpc_create_notify("update", params);
1081 jsonrpc_session_send(aux.monitor->session->js, msg);
1087 static struct json *
1088 ovsdb_jsonrpc_monitor_get_initial(const struct ovsdb_jsonrpc_monitor *m)
1090 struct ovsdb_jsonrpc_monitor_aux aux;
1091 struct shash_node *node;
1093 ovsdb_jsonrpc_monitor_init_aux(&aux, m, true);
1094 SHASH_FOR_EACH (node, &m->tables) {
1095 struct ovsdb_jsonrpc_monitor_table *mt = node->data;
1097 if (mt->select & OJMS_INITIAL) {
1098 struct ovsdb_row *row;
1100 HMAP_FOR_EACH (row, struct ovsdb_row, hmap_node,
1102 ovsdb_jsonrpc_monitor_change_cb(NULL, row, NULL, &aux);
1106 return aux.json ? aux.json : json_object_create();
1110 ovsdb_jsonrpc_monitor_destroy(struct ovsdb_replica *replica)
1112 struct ovsdb_jsonrpc_monitor *m = ovsdb_jsonrpc_monitor_cast(replica);
1113 struct shash_node *node;
1115 json_destroy(m->monitor_id);
1116 SHASH_FOR_EACH (node, &m->tables) {
1117 struct ovsdb_jsonrpc_monitor_table *mt = node->data;
1121 shash_destroy(&m->tables);
1122 hmap_remove(&m->session->monitors, &m->node);
1126 static const struct ovsdb_replica_class ovsdb_jsonrpc_replica_class = {
1127 ovsdb_jsonrpc_monitor_commit,
1128 ovsdb_jsonrpc_monitor_destroy