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