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