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