ovsdb: enable jasonrpc-server to service monitor2 request
[cascardo/ovs.git] / ovsdb / jsonrpc-server.c
1 /* Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015 Nicira, Inc.
2  *
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:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15
16 #include <config.h>
17
18 #include "jsonrpc-server.h"
19
20 #include <errno.h>
21
22 #include "bitmap.h"
23 #include "column.h"
24 #include "dynamic-string.h"
25 #include "monitor.h"
26 #include "json.h"
27 #include "jsonrpc.h"
28 #include "ovsdb-error.h"
29 #include "ovsdb-parser.h"
30 #include "ovsdb.h"
31 #include "poll-loop.h"
32 #include "reconnect.h"
33 #include "row.h"
34 #include "server.h"
35 #include "simap.h"
36 #include "stream.h"
37 #include "table.h"
38 #include "timeval.h"
39 #include "transaction.h"
40 #include "trigger.h"
41 #include "openvswitch/vlog.h"
42
43 VLOG_DEFINE_THIS_MODULE(ovsdb_jsonrpc_server);
44
45 struct ovsdb_jsonrpc_remote;
46 struct ovsdb_jsonrpc_session;
47
48 /* Message rate-limiting. */
49 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
50
51 /* Sessions. */
52 static struct ovsdb_jsonrpc_session *ovsdb_jsonrpc_session_create(
53     struct ovsdb_jsonrpc_remote *, struct jsonrpc_session *);
54 static void ovsdb_jsonrpc_session_run_all(struct ovsdb_jsonrpc_remote *);
55 static void ovsdb_jsonrpc_session_wait_all(struct ovsdb_jsonrpc_remote *);
56 static void ovsdb_jsonrpc_session_get_memory_usage_all(
57     const struct ovsdb_jsonrpc_remote *, struct simap *usage);
58 static void ovsdb_jsonrpc_session_close_all(struct ovsdb_jsonrpc_remote *);
59 static void ovsdb_jsonrpc_session_reconnect_all(struct ovsdb_jsonrpc_remote *);
60 static void ovsdb_jsonrpc_session_set_all_options(
61     struct ovsdb_jsonrpc_remote *, const struct ovsdb_jsonrpc_options *);
62 static bool ovsdb_jsonrpc_session_get_status(
63     const struct ovsdb_jsonrpc_remote *,
64     struct ovsdb_jsonrpc_remote_status *);
65 static void ovsdb_jsonrpc_session_unlock_all(struct ovsdb_jsonrpc_session *);
66 static void ovsdb_jsonrpc_session_unlock__(struct ovsdb_lock_waiter *);
67 static void ovsdb_jsonrpc_session_send(struct ovsdb_jsonrpc_session *,
68                                        struct jsonrpc_msg *);
69
70 /* Triggers. */
71 static void ovsdb_jsonrpc_trigger_create(struct ovsdb_jsonrpc_session *,
72                                          struct ovsdb *,
73                                          struct json *id, struct json *params);
74 static struct ovsdb_jsonrpc_trigger *ovsdb_jsonrpc_trigger_find(
75     struct ovsdb_jsonrpc_session *, const struct json *id, size_t hash);
76 static void ovsdb_jsonrpc_trigger_complete(struct ovsdb_jsonrpc_trigger *);
77 static void ovsdb_jsonrpc_trigger_complete_all(struct ovsdb_jsonrpc_session *);
78 static void ovsdb_jsonrpc_trigger_complete_done(
79     struct ovsdb_jsonrpc_session *);
80
81 /* Monitors. */
82 static struct jsonrpc_msg *ovsdb_jsonrpc_monitor_create(
83     struct ovsdb_jsonrpc_session *, struct ovsdb *, struct json *params,
84     enum ovsdb_monitor_version, const struct json *request_id);
85 static struct jsonrpc_msg *ovsdb_jsonrpc_monitor_cancel(
86     struct ovsdb_jsonrpc_session *,
87     struct json_array *params,
88     const struct json *request_id);
89 static void ovsdb_jsonrpc_monitor_remove_all(struct ovsdb_jsonrpc_session *);
90 static void ovsdb_jsonrpc_monitor_flush_all(struct ovsdb_jsonrpc_session *);
91 static bool ovsdb_jsonrpc_monitor_needs_flush(struct ovsdb_jsonrpc_session *);
92 static struct json *ovsdb_jsonrpc_monitor_compose_update(
93     struct ovsdb_jsonrpc_monitor *monitor, bool initial);
94
95 \f
96 /* JSON-RPC database server. */
97
98 struct ovsdb_jsonrpc_server {
99     struct ovsdb_server up;
100     unsigned int n_sessions, max_sessions;
101     struct shash remotes;      /* Contains "struct ovsdb_jsonrpc_remote *"s. */
102 };
103
104 /* A configured remote.  This is either a passive stream listener plus a list
105  * of the currently connected sessions, or a list of exactly one active
106  * session. */
107 struct ovsdb_jsonrpc_remote {
108     struct ovsdb_jsonrpc_server *server;
109     struct pstream *listener;   /* Listener, if passive. */
110     struct ovs_list sessions;   /* List of "struct ovsdb_jsonrpc_session"s. */
111     uint8_t dscp;
112 };
113
114 static struct ovsdb_jsonrpc_remote *ovsdb_jsonrpc_server_add_remote(
115     struct ovsdb_jsonrpc_server *, const char *name,
116     const struct ovsdb_jsonrpc_options *options
117 );
118 static void ovsdb_jsonrpc_server_del_remote(struct shash_node *);
119
120 /* Creates and returns a new server to provide JSON-RPC access to an OVSDB.
121  *
122  * The caller must call ovsdb_jsonrpc_server_add_db() for each database to
123  * which 'server' should provide access. */
124 struct ovsdb_jsonrpc_server *
125 ovsdb_jsonrpc_server_create(void)
126 {
127     struct ovsdb_jsonrpc_server *server = xzalloc(sizeof *server);
128     ovsdb_server_init(&server->up);
129     server->max_sessions = 330;   /* Random limit. */
130     shash_init(&server->remotes);
131     return server;
132 }
133
134 /* Adds 'db' to the set of databases served out by 'svr'.  Returns true if
135  * successful, false if 'db''s name is the same as some database already in
136  * 'server'. */
137 bool
138 ovsdb_jsonrpc_server_add_db(struct ovsdb_jsonrpc_server *svr, struct ovsdb *db)
139 {
140     /* The OVSDB protocol doesn't have a way to notify a client that a
141      * database has been added.  If some client tried to use the database
142      * that we're adding and failed, then forcing it to reconnect seems like
143      * a reasonable way to make it try again.
144      *
145      * If this is too big of a hammer in practice, we could be more selective,
146      * e.g. disconnect only connections that actually tried to use a database
147      * with 'db''s name. */
148     ovsdb_jsonrpc_server_reconnect(svr);
149
150     return ovsdb_server_add_db(&svr->up, db);
151 }
152
153 /* Removes 'db' from the set of databases served out by 'svr'.  Returns
154  * true if successful, false if there is no database associated with 'db'. */
155 bool
156 ovsdb_jsonrpc_server_remove_db(struct ovsdb_jsonrpc_server *svr,
157                                struct ovsdb *db)
158 {
159     /* There might be pointers to 'db' from 'svr', such as monitors or
160      * outstanding transactions.  Disconnect all JSON-RPC connections to avoid
161      * accesses to freed memory.
162      *
163      * If this is too big of a hammer in practice, we could be more selective,
164      * e.g. disconnect only connections that actually reference 'db'. */
165     ovsdb_jsonrpc_server_reconnect(svr);
166
167     return ovsdb_server_remove_db(&svr->up, db);
168 }
169
170 void
171 ovsdb_jsonrpc_server_destroy(struct ovsdb_jsonrpc_server *svr)
172 {
173     struct shash_node *node, *next;
174
175     SHASH_FOR_EACH_SAFE (node, next, &svr->remotes) {
176         ovsdb_jsonrpc_server_del_remote(node);
177     }
178     shash_destroy(&svr->remotes);
179     ovsdb_server_destroy(&svr->up);
180     free(svr);
181 }
182
183 struct ovsdb_jsonrpc_options *
184 ovsdb_jsonrpc_default_options(const char *target)
185 {
186     struct ovsdb_jsonrpc_options *options = xzalloc(sizeof *options);
187     options->max_backoff = RECONNECT_DEFAULT_MAX_BACKOFF;
188     options->probe_interval = (stream_or_pstream_needs_probes(target)
189                                ? RECONNECT_DEFAULT_PROBE_INTERVAL
190                                : 0);
191     return options;
192 }
193
194 /* Sets 'svr''s current set of remotes to the names in 'new_remotes', with
195  * options in the struct ovsdb_jsonrpc_options supplied as the data values.
196  *
197  * A remote is an active or passive stream connection method, e.g. "pssl:" or
198  * "tcp:1.2.3.4". */
199 void
200 ovsdb_jsonrpc_server_set_remotes(struct ovsdb_jsonrpc_server *svr,
201                                  const struct shash *new_remotes)
202 {
203     struct shash_node *node, *next;
204
205     SHASH_FOR_EACH_SAFE (node, next, &svr->remotes) {
206         struct ovsdb_jsonrpc_remote *remote = node->data;
207         struct ovsdb_jsonrpc_options *options
208             = shash_find_data(new_remotes, node->name);
209
210         if (!options) {
211             VLOG_INFO("%s: remote deconfigured", node->name);
212             ovsdb_jsonrpc_server_del_remote(node);
213         } else if (options->dscp != remote->dscp) {
214             ovsdb_jsonrpc_server_del_remote(node);
215          }
216     }
217     SHASH_FOR_EACH (node, new_remotes) {
218         const struct ovsdb_jsonrpc_options *options = node->data;
219         struct ovsdb_jsonrpc_remote *remote;
220
221         remote = shash_find_data(&svr->remotes, node->name);
222         if (!remote) {
223             remote = ovsdb_jsonrpc_server_add_remote(svr, node->name, options);
224             if (!remote) {
225                 continue;
226             }
227         }
228
229         ovsdb_jsonrpc_session_set_all_options(remote, options);
230     }
231 }
232
233 static struct ovsdb_jsonrpc_remote *
234 ovsdb_jsonrpc_server_add_remote(struct ovsdb_jsonrpc_server *svr,
235                                 const char *name,
236                                 const struct ovsdb_jsonrpc_options *options)
237 {
238     struct ovsdb_jsonrpc_remote *remote;
239     struct pstream *listener;
240     int error;
241
242     error = jsonrpc_pstream_open(name, &listener, options->dscp);
243     if (error && error != EAFNOSUPPORT) {
244         VLOG_ERR_RL(&rl, "%s: listen failed: %s", name, ovs_strerror(error));
245         return NULL;
246     }
247
248     remote = xmalloc(sizeof *remote);
249     remote->server = svr;
250     remote->listener = listener;
251     list_init(&remote->sessions);
252     remote->dscp = options->dscp;
253     shash_add(&svr->remotes, name, remote);
254
255     if (!listener) {
256         ovsdb_jsonrpc_session_create(remote, jsonrpc_session_open(name, true));
257     }
258     return remote;
259 }
260
261 static void
262 ovsdb_jsonrpc_server_del_remote(struct shash_node *node)
263 {
264     struct ovsdb_jsonrpc_remote *remote = node->data;
265
266     ovsdb_jsonrpc_session_close_all(remote);
267     pstream_close(remote->listener);
268     shash_delete(&remote->server->remotes, node);
269     free(remote);
270 }
271
272 /* Stores status information for the remote named 'target', which should have
273  * been configured on 'svr' with a call to ovsdb_jsonrpc_server_set_remotes(),
274  * into '*status'.  On success returns true, on failure (if 'svr' doesn't have
275  * a remote named 'target' or if that remote is an inbound remote that has no
276  * active connections) returns false.  On failure, 'status' will be zeroed.
277  */
278 bool
279 ovsdb_jsonrpc_server_get_remote_status(
280     const struct ovsdb_jsonrpc_server *svr, const char *target,
281     struct ovsdb_jsonrpc_remote_status *status)
282 {
283     const struct ovsdb_jsonrpc_remote *remote;
284
285     memset(status, 0, sizeof *status);
286
287     remote = shash_find_data(&svr->remotes, target);
288     return remote && ovsdb_jsonrpc_session_get_status(remote, status);
289 }
290
291 void
292 ovsdb_jsonrpc_server_free_remote_status(
293     struct ovsdb_jsonrpc_remote_status *status)
294 {
295     free(status->locks_held);
296     free(status->locks_waiting);
297     free(status->locks_lost);
298 }
299
300 /* Forces all of the JSON-RPC sessions managed by 'svr' to disconnect and
301  * reconnect. */
302 void
303 ovsdb_jsonrpc_server_reconnect(struct ovsdb_jsonrpc_server *svr)
304 {
305     struct shash_node *node;
306
307     SHASH_FOR_EACH (node, &svr->remotes) {
308         struct ovsdb_jsonrpc_remote *remote = node->data;
309
310         ovsdb_jsonrpc_session_reconnect_all(remote);
311     }
312 }
313
314 void
315 ovsdb_jsonrpc_server_run(struct ovsdb_jsonrpc_server *svr)
316 {
317     struct shash_node *node;
318
319     SHASH_FOR_EACH (node, &svr->remotes) {
320         struct ovsdb_jsonrpc_remote *remote = node->data;
321
322         if (remote->listener) {
323             if (svr->n_sessions < svr->max_sessions) {
324                 struct stream *stream;
325                 int error;
326
327                 error = pstream_accept(remote->listener, &stream);
328                 if (!error) {
329                     struct jsonrpc_session *js;
330                     js = jsonrpc_session_open_unreliably(jsonrpc_open(stream),
331                                                          remote->dscp);
332                     ovsdb_jsonrpc_session_create(remote, js);
333                 } else if (error != EAGAIN) {
334                     VLOG_WARN_RL(&rl, "%s: accept failed: %s",
335                                  pstream_get_name(remote->listener),
336                                  ovs_strerror(error));
337                 }
338             } else {
339                 VLOG_WARN_RL(&rl, "%s: connection exceeded maximum (%d)",
340                              pstream_get_name(remote->listener),
341                              svr->max_sessions);
342             }
343         }
344
345         ovsdb_jsonrpc_session_run_all(remote);
346     }
347 }
348
349 void
350 ovsdb_jsonrpc_server_wait(struct ovsdb_jsonrpc_server *svr)
351 {
352     struct shash_node *node;
353
354     SHASH_FOR_EACH (node, &svr->remotes) {
355         struct ovsdb_jsonrpc_remote *remote = node->data;
356
357         if (remote->listener && svr->n_sessions < svr->max_sessions) {
358             pstream_wait(remote->listener);
359         }
360
361         ovsdb_jsonrpc_session_wait_all(remote);
362     }
363 }
364
365 /* Adds some memory usage statistics for 'svr' into 'usage', for use with
366  * memory_report(). */
367 void
368 ovsdb_jsonrpc_server_get_memory_usage(const struct ovsdb_jsonrpc_server *svr,
369                                       struct simap *usage)
370 {
371     struct shash_node *node;
372
373     simap_increase(usage, "sessions", svr->n_sessions);
374     SHASH_FOR_EACH (node, &svr->remotes) {
375         struct ovsdb_jsonrpc_remote *remote = node->data;
376
377         ovsdb_jsonrpc_session_get_memory_usage_all(remote, usage);
378     }
379 }
380 \f
381 /* JSON-RPC database server session. */
382
383 struct ovsdb_jsonrpc_session {
384     struct ovs_list node;       /* Element in remote's sessions list. */
385     struct ovsdb_session up;
386     struct ovsdb_jsonrpc_remote *remote;
387
388     /* Triggers. */
389     struct hmap triggers;       /* Hmap of "struct ovsdb_jsonrpc_trigger"s. */
390
391     /* Monitors. */
392     struct hmap monitors;       /* Hmap of "struct ovsdb_jsonrpc_monitor"s. */
393
394     /* Network connectivity. */
395     struct jsonrpc_session *js;  /* JSON-RPC session. */
396     unsigned int js_seqno;       /* Last jsonrpc_session_get_seqno() value. */
397 };
398
399 static void ovsdb_jsonrpc_session_close(struct ovsdb_jsonrpc_session *);
400 static int ovsdb_jsonrpc_session_run(struct ovsdb_jsonrpc_session *);
401 static void ovsdb_jsonrpc_session_wait(struct ovsdb_jsonrpc_session *);
402 static void ovsdb_jsonrpc_session_get_memory_usage(
403     const struct ovsdb_jsonrpc_session *, struct simap *usage);
404 static void ovsdb_jsonrpc_session_got_request(struct ovsdb_jsonrpc_session *,
405                                              struct jsonrpc_msg *);
406 static void ovsdb_jsonrpc_session_got_notify(struct ovsdb_jsonrpc_session *,
407                                              struct jsonrpc_msg *);
408
409 static struct ovsdb_jsonrpc_session *
410 ovsdb_jsonrpc_session_create(struct ovsdb_jsonrpc_remote *remote,
411                              struct jsonrpc_session *js)
412 {
413     struct ovsdb_jsonrpc_session *s;
414
415     s = xzalloc(sizeof *s);
416     ovsdb_session_init(&s->up, &remote->server->up);
417     s->remote = remote;
418     list_push_back(&remote->sessions, &s->node);
419     hmap_init(&s->triggers);
420     hmap_init(&s->monitors);
421     s->js = js;
422     s->js_seqno = jsonrpc_session_get_seqno(js);
423
424     remote->server->n_sessions++;
425
426     return s;
427 }
428
429 static void
430 ovsdb_jsonrpc_session_close(struct ovsdb_jsonrpc_session *s)
431 {
432     ovsdb_jsonrpc_monitor_remove_all(s);
433     ovsdb_jsonrpc_session_unlock_all(s);
434     ovsdb_jsonrpc_trigger_complete_all(s);
435
436     hmap_destroy(&s->monitors);
437     hmap_destroy(&s->triggers);
438
439     jsonrpc_session_close(s->js);
440     list_remove(&s->node);
441     s->remote->server->n_sessions--;
442     ovsdb_session_destroy(&s->up);
443     free(s);
444 }
445
446 static int
447 ovsdb_jsonrpc_session_run(struct ovsdb_jsonrpc_session *s)
448 {
449     jsonrpc_session_run(s->js);
450     if (s->js_seqno != jsonrpc_session_get_seqno(s->js)) {
451         s->js_seqno = jsonrpc_session_get_seqno(s->js);
452         ovsdb_jsonrpc_trigger_complete_all(s);
453         ovsdb_jsonrpc_monitor_remove_all(s);
454         ovsdb_jsonrpc_session_unlock_all(s);
455     }
456
457     ovsdb_jsonrpc_trigger_complete_done(s);
458
459     if (!jsonrpc_session_get_backlog(s->js)) {
460         struct jsonrpc_msg *msg;
461
462         ovsdb_jsonrpc_monitor_flush_all(s);
463
464         msg = jsonrpc_session_recv(s->js);
465         if (msg) {
466             if (msg->type == JSONRPC_REQUEST) {
467                 ovsdb_jsonrpc_session_got_request(s, msg);
468             } else if (msg->type == JSONRPC_NOTIFY) {
469                 ovsdb_jsonrpc_session_got_notify(s, msg);
470             } else {
471                 VLOG_WARN("%s: received unexpected %s message",
472                           jsonrpc_session_get_name(s->js),
473                           jsonrpc_msg_type_to_string(msg->type));
474                 jsonrpc_session_force_reconnect(s->js);
475                 jsonrpc_msg_destroy(msg);
476             }
477         }
478     }
479     return jsonrpc_session_is_alive(s->js) ? 0 : ETIMEDOUT;
480 }
481
482 static void
483 ovsdb_jsonrpc_session_set_options(struct ovsdb_jsonrpc_session *session,
484                                   const struct ovsdb_jsonrpc_options *options)
485 {
486     jsonrpc_session_set_max_backoff(session->js, options->max_backoff);
487     jsonrpc_session_set_probe_interval(session->js, options->probe_interval);
488     jsonrpc_session_set_dscp(session->js, options->dscp);
489 }
490
491 static void
492 ovsdb_jsonrpc_session_run_all(struct ovsdb_jsonrpc_remote *remote)
493 {
494     struct ovsdb_jsonrpc_session *s, *next;
495
496     LIST_FOR_EACH_SAFE (s, next, node, &remote->sessions) {
497         int error = ovsdb_jsonrpc_session_run(s);
498         if (error) {
499             ovsdb_jsonrpc_session_close(s);
500         }
501     }
502 }
503
504 static void
505 ovsdb_jsonrpc_session_wait(struct ovsdb_jsonrpc_session *s)
506 {
507     jsonrpc_session_wait(s->js);
508     if (!jsonrpc_session_get_backlog(s->js)) {
509         if (ovsdb_jsonrpc_monitor_needs_flush(s)) {
510             poll_immediate_wake();
511         } else {
512             jsonrpc_session_recv_wait(s->js);
513         }
514     }
515 }
516
517 static void
518 ovsdb_jsonrpc_session_wait_all(struct ovsdb_jsonrpc_remote *remote)
519 {
520     struct ovsdb_jsonrpc_session *s;
521
522     LIST_FOR_EACH (s, node, &remote->sessions) {
523         ovsdb_jsonrpc_session_wait(s);
524     }
525 }
526
527 static void
528 ovsdb_jsonrpc_session_get_memory_usage(const struct ovsdb_jsonrpc_session *s,
529                                        struct simap *usage)
530 {
531     simap_increase(usage, "triggers", hmap_count(&s->triggers));
532     simap_increase(usage, "monitors", hmap_count(&s->monitors));
533     simap_increase(usage, "backlog", jsonrpc_session_get_backlog(s->js));
534 }
535
536 static void
537 ovsdb_jsonrpc_session_get_memory_usage_all(
538     const struct ovsdb_jsonrpc_remote *remote,
539     struct simap *usage)
540 {
541     struct ovsdb_jsonrpc_session *s;
542
543     LIST_FOR_EACH (s, node, &remote->sessions) {
544         ovsdb_jsonrpc_session_get_memory_usage(s, usage);
545     }
546 }
547
548 static void
549 ovsdb_jsonrpc_session_close_all(struct ovsdb_jsonrpc_remote *remote)
550 {
551     struct ovsdb_jsonrpc_session *s, *next;
552
553     LIST_FOR_EACH_SAFE (s, next, node, &remote->sessions) {
554         ovsdb_jsonrpc_session_close(s);
555     }
556 }
557
558 /* Forces all of the JSON-RPC sessions managed by 'remote' to disconnect and
559  * reconnect. */
560 static void
561 ovsdb_jsonrpc_session_reconnect_all(struct ovsdb_jsonrpc_remote *remote)
562 {
563     struct ovsdb_jsonrpc_session *s, *next;
564
565     LIST_FOR_EACH_SAFE (s, next, node, &remote->sessions) {
566         jsonrpc_session_force_reconnect(s->js);
567         if (!jsonrpc_session_is_alive(s->js)) {
568             ovsdb_jsonrpc_session_close(s);
569         }
570     }
571 }
572
573 /* Sets the options for all of the JSON-RPC sessions managed by 'remote' to
574  * 'options'.
575  *
576  * (The dscp value can't be changed directly; the caller must instead close and
577  * re-open the session.) */
578 static void
579 ovsdb_jsonrpc_session_set_all_options(
580     struct ovsdb_jsonrpc_remote *remote,
581     const struct ovsdb_jsonrpc_options *options)
582 {
583     struct ovsdb_jsonrpc_session *s;
584
585     LIST_FOR_EACH (s, node, &remote->sessions) {
586         ovsdb_jsonrpc_session_set_options(s, options);
587     }
588 }
589
590 static bool
591 ovsdb_jsonrpc_session_get_status(const struct ovsdb_jsonrpc_remote *remote,
592                                  struct ovsdb_jsonrpc_remote_status *status)
593 {
594     const struct ovsdb_jsonrpc_session *s;
595     const struct jsonrpc_session *js;
596     struct ovsdb_lock_waiter *waiter;
597     struct reconnect_stats rstats;
598     struct ds locks_held, locks_waiting, locks_lost;
599
600     status->bound_port = (remote->listener
601                           ? pstream_get_bound_port(remote->listener)
602                           : htons(0));
603
604     if (list_is_empty(&remote->sessions)) {
605         return false;
606     }
607     s = CONTAINER_OF(remote->sessions.next, struct ovsdb_jsonrpc_session, node);
608     js = s->js;
609
610     status->is_connected = jsonrpc_session_is_connected(js);
611     status->last_error = jsonrpc_session_get_status(js);
612
613     jsonrpc_session_get_reconnect_stats(js, &rstats);
614     status->state = rstats.state;
615     status->sec_since_connect = rstats.msec_since_connect == UINT_MAX
616         ? UINT_MAX : rstats.msec_since_connect / 1000;
617     status->sec_since_disconnect = rstats.msec_since_disconnect == UINT_MAX
618         ? UINT_MAX : rstats.msec_since_disconnect / 1000;
619
620     ds_init(&locks_held);
621     ds_init(&locks_waiting);
622     ds_init(&locks_lost);
623     HMAP_FOR_EACH (waiter, session_node, &s->up.waiters) {
624         struct ds *string;
625
626         string = (ovsdb_lock_waiter_is_owner(waiter) ? &locks_held
627                   : waiter->mode == OVSDB_LOCK_WAIT ? &locks_waiting
628                   : &locks_lost);
629         if (string->length) {
630             ds_put_char(string, ' ');
631         }
632         ds_put_cstr(string, waiter->lock_name);
633     }
634     status->locks_held = ds_steal_cstr(&locks_held);
635     status->locks_waiting = ds_steal_cstr(&locks_waiting);
636     status->locks_lost = ds_steal_cstr(&locks_lost);
637
638     status->n_connections = list_size(&remote->sessions);
639
640     return true;
641 }
642
643 /* Examines 'request' to determine the database to which it relates, and then
644  * searches 's' to find that database:
645  *
646  *    - If successful, returns the database and sets '*replyp' to NULL.
647  *
648  *    - If no such database exists, returns NULL and sets '*replyp' to an
649  *      appropriate JSON-RPC error reply, owned by the caller. */
650 static struct ovsdb *
651 ovsdb_jsonrpc_lookup_db(const struct ovsdb_jsonrpc_session *s,
652                         const struct jsonrpc_msg *request,
653                         struct jsonrpc_msg **replyp)
654 {
655     struct json_array *params;
656     struct ovsdb_error *error;
657     const char *db_name;
658     struct ovsdb *db;
659
660     params = json_array(request->params);
661     if (!params->n || params->elems[0]->type != JSON_STRING) {
662         error = ovsdb_syntax_error(
663             request->params, NULL,
664             "%s request params must begin with <db-name>", request->method);
665         goto error;
666     }
667
668     db_name = params->elems[0]->u.string;
669     db = shash_find_data(&s->up.server->dbs, db_name);
670     if (!db) {
671         error = ovsdb_syntax_error(
672             request->params, "unknown database",
673             "%s request specifies unknown database %s",
674             request->method, db_name);
675         goto error;
676     }
677
678     *replyp = NULL;
679     return db;
680
681 error:
682     *replyp = jsonrpc_create_error(ovsdb_error_to_json(error), request->id);
683     ovsdb_error_destroy(error);
684     return NULL;
685 }
686
687 static struct ovsdb_error *
688 ovsdb_jsonrpc_session_parse_lock_name(const struct jsonrpc_msg *request,
689                                       const char **lock_namep)
690 {
691     const struct json_array *params;
692
693     params = json_array(request->params);
694     if (params->n != 1 || params->elems[0]->type != JSON_STRING ||
695         !ovsdb_parser_is_id(json_string(params->elems[0]))) {
696         *lock_namep = NULL;
697         return ovsdb_syntax_error(request->params, NULL,
698                                   "%s request params must be <id>",
699                                   request->method);
700     }
701
702     *lock_namep = json_string(params->elems[0]);
703     return NULL;
704 }
705
706 static void
707 ovsdb_jsonrpc_session_notify(struct ovsdb_session *session,
708                              const char *lock_name,
709                              const char *method)
710 {
711     struct ovsdb_jsonrpc_session *s;
712     struct json *params;
713
714     s = CONTAINER_OF(session, struct ovsdb_jsonrpc_session, up);
715     params = json_array_create_1(json_string_create(lock_name));
716     ovsdb_jsonrpc_session_send(s, jsonrpc_create_notify(method, params));
717 }
718
719 static struct jsonrpc_msg *
720 ovsdb_jsonrpc_session_lock(struct ovsdb_jsonrpc_session *s,
721                            struct jsonrpc_msg *request,
722                            enum ovsdb_lock_mode mode)
723 {
724     struct ovsdb_lock_waiter *waiter;
725     struct jsonrpc_msg *reply;
726     struct ovsdb_error *error;
727     struct ovsdb_session *victim;
728     const char *lock_name;
729     struct json *result;
730
731     error = ovsdb_jsonrpc_session_parse_lock_name(request, &lock_name);
732     if (error) {
733         goto error;
734     }
735
736     /* Report error if this session has issued a "lock" or "steal" without a
737      * matching "unlock" for this lock. */
738     waiter = ovsdb_session_get_lock_waiter(&s->up, lock_name);
739     if (waiter) {
740         error = ovsdb_syntax_error(
741             request->params, NULL,
742             "must issue \"unlock\" before new \"%s\"", request->method);
743         goto error;
744     }
745
746     /* Get the lock, add us as a waiter. */
747     waiter = ovsdb_server_lock(&s->remote->server->up, &s->up, lock_name, mode,
748                                &victim);
749     if (victim) {
750         ovsdb_jsonrpc_session_notify(victim, lock_name, "stolen");
751     }
752
753     result = json_object_create();
754     json_object_put(result, "locked",
755                     json_boolean_create(ovsdb_lock_waiter_is_owner(waiter)));
756
757     return jsonrpc_create_reply(result, request->id);
758
759 error:
760     reply = jsonrpc_create_error(ovsdb_error_to_json(error), request->id);
761     ovsdb_error_destroy(error);
762     return reply;
763 }
764
765 static void
766 ovsdb_jsonrpc_session_unlock_all(struct ovsdb_jsonrpc_session *s)
767 {
768     struct ovsdb_lock_waiter *waiter, *next;
769
770     HMAP_FOR_EACH_SAFE (waiter, next, session_node, &s->up.waiters) {
771         ovsdb_jsonrpc_session_unlock__(waiter);
772     }
773 }
774
775 static void
776 ovsdb_jsonrpc_session_unlock__(struct ovsdb_lock_waiter *waiter)
777 {
778     struct ovsdb_lock *lock = waiter->lock;
779
780     if (lock) {
781         struct ovsdb_session *new_owner = ovsdb_lock_waiter_remove(waiter);
782         if (new_owner) {
783             ovsdb_jsonrpc_session_notify(new_owner, lock->name, "locked");
784         } else {
785             /* ovsdb_server_lock() might have freed 'lock'. */
786         }
787     }
788
789     ovsdb_lock_waiter_destroy(waiter);
790 }
791
792 static struct jsonrpc_msg *
793 ovsdb_jsonrpc_session_unlock(struct ovsdb_jsonrpc_session *s,
794                              struct jsonrpc_msg *request)
795 {
796     struct ovsdb_lock_waiter *waiter;
797     struct jsonrpc_msg *reply;
798     struct ovsdb_error *error;
799     const char *lock_name;
800
801     error = ovsdb_jsonrpc_session_parse_lock_name(request, &lock_name);
802     if (error) {
803         goto error;
804     }
805
806     /* Report error if this session has not issued a "lock" or "steal" for this
807      * lock. */
808     waiter = ovsdb_session_get_lock_waiter(&s->up, lock_name);
809     if (!waiter) {
810         error = ovsdb_syntax_error(
811             request->params, NULL, "\"unlock\" without \"lock\" or \"steal\"");
812         goto error;
813     }
814
815     ovsdb_jsonrpc_session_unlock__(waiter);
816
817     return jsonrpc_create_reply(json_object_create(), request->id);
818
819 error:
820     reply = jsonrpc_create_error(ovsdb_error_to_json(error), request->id);
821     ovsdb_error_destroy(error);
822     return reply;
823 }
824
825 static struct jsonrpc_msg *
826 execute_transaction(struct ovsdb_jsonrpc_session *s, struct ovsdb *db,
827                     struct jsonrpc_msg *request)
828 {
829     ovsdb_jsonrpc_trigger_create(s, db, request->id, request->params);
830     request->id = NULL;
831     request->params = NULL;
832     jsonrpc_msg_destroy(request);
833     return NULL;
834 }
835
836 static void
837 ovsdb_jsonrpc_session_got_request(struct ovsdb_jsonrpc_session *s,
838                                   struct jsonrpc_msg *request)
839 {
840     struct jsonrpc_msg *reply;
841
842     if (!strcmp(request->method, "transact")) {
843         struct ovsdb *db = ovsdb_jsonrpc_lookup_db(s, request, &reply);
844         if (!reply) {
845             reply = execute_transaction(s, db, request);
846         }
847     } else if (!strcmp(request->method, "monitor") ||
848                !strcmp(request->method, "monitor2")) {
849         struct ovsdb *db = ovsdb_jsonrpc_lookup_db(s, request, &reply);
850         if (!reply) {
851             int l = strlen(request->method) - strlen("monitor");
852             enum ovsdb_monitor_version version = l ? OVSDB_MONITOR_V2
853                                                    : OVSDB_MONITOR_V1;
854             reply = ovsdb_jsonrpc_monitor_create(s, db, request->params,
855                                                  version, request->id);
856         }
857     } else if (!strcmp(request->method, "monitor_cancel")) {
858         reply = ovsdb_jsonrpc_monitor_cancel(s, json_array(request->params),
859                                              request->id);
860     } else if (!strcmp(request->method, "get_schema")) {
861         struct ovsdb *db = ovsdb_jsonrpc_lookup_db(s, request, &reply);
862         if (!reply) {
863             reply = jsonrpc_create_reply(ovsdb_schema_to_json(db->schema),
864                                          request->id);
865         }
866     } else if (!strcmp(request->method, "list_dbs")) {
867         size_t n_dbs = shash_count(&s->up.server->dbs);
868         struct shash_node *node;
869         struct json **dbs;
870         size_t i;
871
872         dbs = xmalloc(n_dbs * sizeof *dbs);
873         i = 0;
874         SHASH_FOR_EACH (node, &s->up.server->dbs) {
875             dbs[i++] = json_string_create(node->name);
876         }
877         reply = jsonrpc_create_reply(json_array_create(dbs, n_dbs),
878                                      request->id);
879     } else if (!strcmp(request->method, "lock")) {
880         reply = ovsdb_jsonrpc_session_lock(s, request, OVSDB_LOCK_WAIT);
881     } else if (!strcmp(request->method, "steal")) {
882         reply = ovsdb_jsonrpc_session_lock(s, request, OVSDB_LOCK_STEAL);
883     } else if (!strcmp(request->method, "unlock")) {
884         reply = ovsdb_jsonrpc_session_unlock(s, request);
885     } else if (!strcmp(request->method, "echo")) {
886         reply = jsonrpc_create_reply(json_clone(request->params), request->id);
887     } else {
888         reply = jsonrpc_create_error(json_string_create("unknown method"),
889                                      request->id);
890     }
891
892     if (reply) {
893         jsonrpc_msg_destroy(request);
894         ovsdb_jsonrpc_session_send(s, reply);
895     }
896 }
897
898 static void
899 execute_cancel(struct ovsdb_jsonrpc_session *s, struct jsonrpc_msg *request)
900 {
901     if (json_array(request->params)->n == 1) {
902         struct ovsdb_jsonrpc_trigger *t;
903         struct json *id;
904
905         id = request->params->u.array.elems[0];
906         t = ovsdb_jsonrpc_trigger_find(s, id, json_hash(id, 0));
907         if (t) {
908             ovsdb_jsonrpc_trigger_complete(t);
909         }
910     }
911 }
912
913 static void
914 ovsdb_jsonrpc_session_got_notify(struct ovsdb_jsonrpc_session *s,
915                                  struct jsonrpc_msg *request)
916 {
917     if (!strcmp(request->method, "cancel")) {
918         execute_cancel(s, request);
919     }
920     jsonrpc_msg_destroy(request);
921 }
922
923 static void
924 ovsdb_jsonrpc_session_send(struct ovsdb_jsonrpc_session *s,
925                            struct jsonrpc_msg *msg)
926 {
927     ovsdb_jsonrpc_monitor_flush_all(s);
928     jsonrpc_session_send(s->js, msg);
929 }
930 \f
931 /* JSON-RPC database server triggers.
932  *
933  * (Every transaction is treated as a trigger even if it doesn't actually have
934  * any "wait" operations.) */
935
936 struct ovsdb_jsonrpc_trigger {
937     struct ovsdb_trigger trigger;
938     struct hmap_node hmap_node; /* In session's "triggers" hmap. */
939     struct json *id;
940 };
941
942 static void
943 ovsdb_jsonrpc_trigger_create(struct ovsdb_jsonrpc_session *s, struct ovsdb *db,
944                              struct json *id, struct json *params)
945 {
946     struct ovsdb_jsonrpc_trigger *t;
947     size_t hash;
948
949     /* Check for duplicate ID. */
950     hash = json_hash(id, 0);
951     t = ovsdb_jsonrpc_trigger_find(s, id, hash);
952     if (t) {
953         struct jsonrpc_msg *msg;
954
955         msg = jsonrpc_create_error(json_string_create("duplicate request ID"),
956                                    id);
957         ovsdb_jsonrpc_session_send(s, msg);
958         json_destroy(id);
959         json_destroy(params);
960         return;
961     }
962
963     /* Insert into trigger table. */
964     t = xmalloc(sizeof *t);
965     ovsdb_trigger_init(&s->up, db, &t->trigger, params, time_msec());
966     t->id = id;
967     hmap_insert(&s->triggers, &t->hmap_node, hash);
968
969     /* Complete early if possible. */
970     if (ovsdb_trigger_is_complete(&t->trigger)) {
971         ovsdb_jsonrpc_trigger_complete(t);
972     }
973 }
974
975 static struct ovsdb_jsonrpc_trigger *
976 ovsdb_jsonrpc_trigger_find(struct ovsdb_jsonrpc_session *s,
977                            const struct json *id, size_t hash)
978 {
979     struct ovsdb_jsonrpc_trigger *t;
980
981     HMAP_FOR_EACH_WITH_HASH (t, hmap_node, hash, &s->triggers) {
982         if (json_equal(t->id, id)) {
983             return t;
984         }
985     }
986
987     return NULL;
988 }
989
990 static void
991 ovsdb_jsonrpc_trigger_complete(struct ovsdb_jsonrpc_trigger *t)
992 {
993     struct ovsdb_jsonrpc_session *s;
994
995     s = CONTAINER_OF(t->trigger.session, struct ovsdb_jsonrpc_session, up);
996
997     if (jsonrpc_session_is_connected(s->js)) {
998         struct jsonrpc_msg *reply;
999         struct json *result;
1000
1001         result = ovsdb_trigger_steal_result(&t->trigger);
1002         if (result) {
1003             reply = jsonrpc_create_reply(result, t->id);
1004         } else {
1005             reply = jsonrpc_create_error(json_string_create("canceled"),
1006                                          t->id);
1007         }
1008         ovsdb_jsonrpc_session_send(s, reply);
1009     }
1010
1011     json_destroy(t->id);
1012     ovsdb_trigger_destroy(&t->trigger);
1013     hmap_remove(&s->triggers, &t->hmap_node);
1014     free(t);
1015 }
1016
1017 static void
1018 ovsdb_jsonrpc_trigger_complete_all(struct ovsdb_jsonrpc_session *s)
1019 {
1020     struct ovsdb_jsonrpc_trigger *t, *next;
1021     HMAP_FOR_EACH_SAFE (t, next, hmap_node, &s->triggers) {
1022         ovsdb_jsonrpc_trigger_complete(t);
1023     }
1024 }
1025
1026 static void
1027 ovsdb_jsonrpc_trigger_complete_done(struct ovsdb_jsonrpc_session *s)
1028 {
1029     while (!list_is_empty(&s->up.completions)) {
1030         struct ovsdb_jsonrpc_trigger *t
1031             = CONTAINER_OF(s->up.completions.next,
1032                            struct ovsdb_jsonrpc_trigger, trigger.node);
1033         ovsdb_jsonrpc_trigger_complete(t);
1034     }
1035 }
1036 \f
1037 /* Jsonrpc front end monitor. */
1038 struct ovsdb_jsonrpc_monitor {
1039     struct ovsdb_jsonrpc_session *session;
1040     struct ovsdb *db;
1041     struct hmap_node node;      /* In ovsdb_jsonrpc_session's "monitors". */
1042     struct json *monitor_id;
1043     struct ovsdb_monitor *dbmon;
1044     uint64_t unflushed;         /* The first transaction that has not been
1045                                        flushed to the jsonrpc remote client. */
1046     enum ovsdb_monitor_version version;
1047 };
1048
1049 static struct ovsdb_jsonrpc_monitor *
1050 ovsdb_jsonrpc_monitor_find(struct ovsdb_jsonrpc_session *s,
1051                            const struct json *monitor_id)
1052 {
1053     struct ovsdb_jsonrpc_monitor *m;
1054
1055     HMAP_FOR_EACH_WITH_HASH (m, node, json_hash(monitor_id, 0), &s->monitors) {
1056         if (json_equal(m->monitor_id, monitor_id)) {
1057             return m;
1058         }
1059     }
1060
1061     return NULL;
1062 }
1063
1064 static bool
1065 parse_bool(struct ovsdb_parser *parser, const char *name, bool default_value)
1066 {
1067     const struct json *json;
1068
1069     json = ovsdb_parser_member(parser, name, OP_BOOLEAN | OP_OPTIONAL);
1070     return json ? json_boolean(json) : default_value;
1071 }
1072
1073 static struct ovsdb_error * OVS_WARN_UNUSED_RESULT
1074 ovsdb_jsonrpc_parse_monitor_request(struct ovsdb_monitor *dbmon,
1075                                     const struct ovsdb_table *table,
1076                                     const struct json *monitor_request,
1077                                     size_t *allocated_columns)
1078 {
1079     const struct ovsdb_table_schema *ts = table->schema;
1080     enum ovsdb_monitor_selection select;
1081     const struct json *columns, *select_json;
1082     struct ovsdb_parser parser;
1083     struct ovsdb_error *error;
1084
1085     ovsdb_parser_init(&parser, monitor_request, "table %s", ts->name);
1086     columns = ovsdb_parser_member(&parser, "columns", OP_ARRAY | OP_OPTIONAL);
1087     select_json = ovsdb_parser_member(&parser, "select",
1088                                       OP_OBJECT | OP_OPTIONAL);
1089     error = ovsdb_parser_finish(&parser);
1090     if (error) {
1091         return error;
1092     }
1093
1094     if (select_json) {
1095         select = 0;
1096         ovsdb_parser_init(&parser, select_json, "table %s select", ts->name);
1097         if (parse_bool(&parser, "initial", true)) {
1098             select |= OJMS_INITIAL;
1099         }
1100         if (parse_bool(&parser, "insert", true)) {
1101             select |= OJMS_INSERT;
1102         }
1103         if (parse_bool(&parser, "delete", true)) {
1104             select |= OJMS_DELETE;
1105         }
1106         if (parse_bool(&parser, "modify", true)) {
1107             select |= OJMS_MODIFY;
1108         }
1109         error = ovsdb_parser_finish(&parser);
1110         if (error) {
1111             return error;
1112         }
1113     } else {
1114         select = OJMS_INITIAL | OJMS_INSERT | OJMS_DELETE | OJMS_MODIFY;
1115     }
1116
1117     ovsdb_monitor_table_add_select(dbmon, table, select);
1118     if (columns) {
1119         size_t i;
1120
1121         if (columns->type != JSON_ARRAY) {
1122             return ovsdb_syntax_error(columns, NULL,
1123                                       "array of column names expected");
1124         }
1125
1126         for (i = 0; i < columns->u.array.n; i++) {
1127             const struct ovsdb_column *column;
1128             const char *s;
1129
1130             if (columns->u.array.elems[i]->type != JSON_STRING) {
1131                 return ovsdb_syntax_error(columns, NULL,
1132                                           "array of column names expected");
1133             }
1134
1135             s = columns->u.array.elems[i]->u.string;
1136             column = shash_find_data(&table->schema->columns, s);
1137             if (!column) {
1138                 return ovsdb_syntax_error(columns, NULL, "%s is not a valid "
1139                                           "column name", s);
1140             }
1141             ovsdb_monitor_add_column(dbmon, table, column, select,
1142                                      allocated_columns);
1143         }
1144     } else {
1145         struct shash_node *node;
1146
1147         SHASH_FOR_EACH (node, &ts->columns) {
1148             const struct ovsdb_column *column = node->data;
1149             if (column->index != OVSDB_COL_UUID) {
1150                 ovsdb_monitor_add_column(dbmon, table, column, select,
1151                                          allocated_columns);
1152             }
1153         }
1154     }
1155
1156     return NULL;
1157 }
1158
1159 static struct jsonrpc_msg *
1160 ovsdb_jsonrpc_monitor_create(struct ovsdb_jsonrpc_session *s, struct ovsdb *db,
1161                              struct json *params,
1162                              enum ovsdb_monitor_version version,
1163                              const struct json *request_id)
1164 {
1165     struct ovsdb_jsonrpc_monitor *m = NULL;
1166     struct ovsdb_monitor *dbmon = NULL;
1167     struct json *monitor_id, *monitor_requests;
1168     struct ovsdb_error *error = NULL;
1169     struct shash_node *node;
1170     struct json *json;
1171
1172     if (json_array(params)->n != 3) {
1173         error = ovsdb_syntax_error(params, NULL, "invalid parameters");
1174         goto error;
1175     }
1176     monitor_id = params->u.array.elems[1];
1177     monitor_requests = params->u.array.elems[2];
1178     if (monitor_requests->type != JSON_OBJECT) {
1179         error = ovsdb_syntax_error(monitor_requests, NULL,
1180                                    "monitor-requests must be object");
1181         goto error;
1182     }
1183
1184     if (ovsdb_jsonrpc_monitor_find(s, monitor_id)) {
1185         error = ovsdb_syntax_error(monitor_id, NULL, "duplicate monitor ID");
1186         goto error;
1187     }
1188
1189     m = xzalloc(sizeof *m);
1190     m->session = s;
1191     m->db = db;
1192     m->dbmon = ovsdb_monitor_create(db, m);
1193     m->unflushed = 0;
1194     m->version = version;
1195     hmap_insert(&s->monitors, &m->node, json_hash(monitor_id, 0));
1196     m->monitor_id = json_clone(monitor_id);
1197
1198     SHASH_FOR_EACH (node, json_object(monitor_requests)) {
1199         const struct ovsdb_table *table;
1200         const char *column_name;
1201         size_t allocated_columns;
1202         const struct json *mr_value;
1203         size_t i;
1204
1205         table = ovsdb_get_table(m->db, node->name);
1206         if (!table) {
1207             error = ovsdb_syntax_error(NULL, NULL,
1208                                        "no table named %s", node->name);
1209             goto error;
1210         }
1211
1212         ovsdb_monitor_add_table(m->dbmon, table);
1213
1214         /* Parse columns. */
1215         mr_value = node->data;
1216         allocated_columns = 0;
1217         if (mr_value->type == JSON_ARRAY) {
1218             const struct json_array *array = &mr_value->u.array;
1219
1220             for (i = 0; i < array->n; i++) {
1221                 error = ovsdb_jsonrpc_parse_monitor_request(
1222                     m->dbmon, table, array->elems[i], &allocated_columns);
1223                 if (error) {
1224                     goto error;
1225                 }
1226             }
1227         } else {
1228             error = ovsdb_jsonrpc_parse_monitor_request(
1229                 m->dbmon, table, mr_value, &allocated_columns);
1230             if (error) {
1231                 goto error;
1232             }
1233         }
1234
1235         column_name = ovsdb_monitor_table_check_duplicates(m->dbmon, table);
1236
1237         if (column_name) {
1238             error = ovsdb_syntax_error(mr_value, NULL, "column %s "
1239                                        "mentioned more than once",
1240                                         column_name);
1241             goto error;
1242         }
1243     }
1244
1245     dbmon = ovsdb_monitor_add(m->dbmon);
1246     if (dbmon != m->dbmon) {
1247         /* Found an exisiting dbmon, reuse the current one. */
1248         ovsdb_monitor_remove_jsonrpc_monitor(m->dbmon, m);
1249         ovsdb_monitor_add_jsonrpc_monitor(dbmon, m);
1250         m->dbmon = dbmon;
1251     }
1252
1253     ovsdb_monitor_get_initial(m->dbmon);
1254     json = ovsdb_jsonrpc_monitor_compose_update(m, true);
1255     json = json ? json : json_object_create();
1256     return jsonrpc_create_reply(json, request_id);
1257
1258 error:
1259     if (m) {
1260         ovsdb_jsonrpc_monitor_destroy(m);
1261     }
1262
1263     json = ovsdb_error_to_json(error);
1264     ovsdb_error_destroy(error);
1265     return jsonrpc_create_error(json, request_id);
1266 }
1267
1268 static struct jsonrpc_msg *
1269 ovsdb_jsonrpc_monitor_cancel(struct ovsdb_jsonrpc_session *s,
1270                              struct json_array *params,
1271                              const struct json *request_id)
1272 {
1273     if (params->n != 1) {
1274         return jsonrpc_create_error(json_string_create("invalid parameters"),
1275                                     request_id);
1276     } else {
1277         struct ovsdb_jsonrpc_monitor *m;
1278
1279         m = ovsdb_jsonrpc_monitor_find(s, params->elems[0]);
1280         if (!m) {
1281             return jsonrpc_create_error(json_string_create("unknown monitor"),
1282                                         request_id);
1283         } else {
1284             ovsdb_jsonrpc_monitor_destroy(m);
1285             return jsonrpc_create_reply(json_object_create(), request_id);
1286         }
1287     }
1288 }
1289
1290 static void
1291 ovsdb_jsonrpc_monitor_remove_all(struct ovsdb_jsonrpc_session *s)
1292 {
1293     struct ovsdb_jsonrpc_monitor *m, *next;
1294
1295     HMAP_FOR_EACH_SAFE (m, next, node, &s->monitors) {
1296         ovsdb_jsonrpc_monitor_destroy(m);
1297     }
1298 }
1299
1300 static struct json *
1301 ovsdb_jsonrpc_monitor_compose_update(struct ovsdb_jsonrpc_monitor *m,
1302                                      bool initial)
1303 {
1304     return ovsdb_monitor_get_update(m->dbmon, initial, &m->unflushed,
1305                                     m->version);
1306 }
1307
1308 static bool
1309 ovsdb_jsonrpc_monitor_needs_flush(struct ovsdb_jsonrpc_session *s)
1310 {
1311     struct ovsdb_jsonrpc_monitor *m;
1312
1313     HMAP_FOR_EACH (m, node, &s->monitors) {
1314         if (ovsdb_monitor_needs_flush(m->dbmon, m->unflushed)) {
1315             return true;
1316         }
1317     }
1318
1319     return false;
1320 }
1321
1322 void
1323 ovsdb_jsonrpc_monitor_destroy(struct ovsdb_jsonrpc_monitor *m)
1324 {
1325     json_destroy(m->monitor_id);
1326     hmap_remove(&m->session->monitors, &m->node);
1327     ovsdb_monitor_remove_jsonrpc_monitor(m->dbmon, m);
1328     free(m);
1329 }
1330
1331 static struct jsonrpc_msg *
1332 ovsdb_jsonrpc_create_notify(const struct ovsdb_jsonrpc_monitor *m,
1333                             struct json *params)
1334 {
1335     const char *method;
1336
1337     switch(m->version) {
1338     case OVSDB_MONITOR_V1:
1339         method = "update";
1340         break;
1341     case OVSDB_MONITOR_V2:
1342         method = "update2";
1343         break;
1344     case OVSDB_MONITOR_VERSION_MAX:
1345     default:
1346         OVS_NOT_REACHED();
1347     }
1348
1349     return jsonrpc_create_notify(method, params);
1350 }
1351
1352 static void
1353 ovsdb_jsonrpc_monitor_flush_all(struct ovsdb_jsonrpc_session *s)
1354 {
1355     struct ovsdb_jsonrpc_monitor *m;
1356
1357     HMAP_FOR_EACH (m, node, &s->monitors) {
1358         struct json *json;
1359
1360         json = ovsdb_jsonrpc_monitor_compose_update(m, false);
1361         if (json) {
1362             struct jsonrpc_msg *msg;
1363             struct json *params;
1364
1365             params = json_array_create_2(json_clone(m->monitor_id), json);
1366             msg = ovsdb_jsonrpc_create_notify(m, params);
1367             jsonrpc_session_send(s->js, msg);
1368         }
1369     }
1370 }