ovsdb-monitor: allow multiple jsonrpc monitors to share a single ovsdb
[cascardo/ovs.git] / ovsdb / monitor.c
1 /*
2  * Copyright (c) 2015 Nicira, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <config.h>
18
19 #include <errno.h>
20
21 #include "bitmap.h"
22 #include "column.h"
23 #include "dynamic-string.h"
24 #include "json.h"
25 #include "jsonrpc.h"
26 #include "ovsdb-error.h"
27 #include "ovsdb-parser.h"
28 #include "ovsdb.h"
29 #include "row.h"
30 #include "simap.h"
31 #include "hash.h"
32 #include "table.h"
33 #include "hash.h"
34 #include "timeval.h"
35 #include "transaction.h"
36 #include "jsonrpc-server.h"
37 #include "monitor.h"
38 #include "openvswitch/vlog.h"
39
40
41 static const struct ovsdb_replica_class ovsdb_jsonrpc_replica_class;
42 static struct hmap ovsdb_monitors = HMAP_INITIALIZER(&ovsdb_monitors);
43
44 /*  Backend monitor.
45  *
46  *  ovsdb_monitor keep track of the ovsdb changes.
47  */
48
49 /* A collection of tables being monitored. */
50 struct ovsdb_monitor {
51     struct ovsdb_replica replica;
52     struct shash tables;     /* Holds "struct ovsdb_monitor_table"s. */
53     struct ovs_list jsonrpc_monitors;  /* Contains "jsonrpc_monitor_node"s. */
54     struct ovsdb *db;
55     uint64_t n_transactions;      /* Count number of committed transactions. */
56     struct hmap_node hmap_node;   /* Elements within ovsdb_monitors.  */
57 };
58
59 struct jsonrpc_monitor_node {
60     struct ovsdb_jsonrpc_monitor *jsonrpc_monitor;
61     struct ovs_list node;
62 };
63
64 /* A particular column being monitored. */
65 struct ovsdb_monitor_column {
66     const struct ovsdb_column *column;
67     enum ovsdb_monitor_selection select;
68 };
69
70 /* A row that has changed in a monitored table. */
71 struct ovsdb_monitor_row {
72     struct hmap_node hmap_node; /* In ovsdb_jsonrpc_monitor_table.changes. */
73     struct uuid uuid;           /* UUID of row that changed. */
74     struct ovsdb_datum *old;    /* Old data, NULL for an inserted row. */
75     struct ovsdb_datum *new;    /* New data, NULL for a deleted row. */
76 };
77
78 /* Contains 'struct ovsdb_monitor_row's for rows that have been
79  * updated but not yet flushed to all the jsonrpc connection.
80  *
81  * 'n_refs' represent the number of jsonrpc connections that have
82  * not received updates. Generate the update for the last jsonprc
83  * connection will also destroy the whole "struct ovsdb_monitor_changes"
84  * object.
85  *
86  * 'transaction' stores the first update's transaction id.
87  * */
88 struct ovsdb_monitor_changes {
89     struct ovsdb_monitor_table *mt;
90     struct hmap rows;
91     int n_refs;
92     uint64_t transaction;
93     struct hmap_node hmap_node;  /* Element in ovsdb_monitor_tables' changes
94                                     hmap.  */
95 };
96
97 /* A particular table being monitored. */
98 struct ovsdb_monitor_table {
99     const struct ovsdb_table *table;
100
101     /* This is the union (bitwise-OR) of the 'select' values in all of the
102      * members of 'columns' below. */
103     enum ovsdb_monitor_selection select;
104
105     /* Columns being monitored. */
106     struct ovsdb_monitor_column *columns;
107     size_t n_columns;
108
109     /* Contains 'ovsdb_monitor_changes' indexed by 'transaction'. */
110     struct hmap changes;
111 };
112
113 static void ovsdb_monitor_destroy(struct ovsdb_monitor *dbmon);
114 static struct ovsdb_monitor_changes * ovsdb_monitor_table_add_changes(
115     struct ovsdb_monitor_table *mt, uint64_t next_txn);
116 static struct ovsdb_monitor_changes *ovsdb_monitor_table_find_changes(
117     struct ovsdb_monitor_table *mt, uint64_t unflushed);
118 static void ovsdb_monitor_changes_destroy(
119                                   struct ovsdb_monitor_changes *changes);
120 static void ovsdb_monitor_table_track_changes(struct ovsdb_monitor_table *mt,
121                                   uint64_t unflushed);
122
123 static int
124 compare_ovsdb_monitor_column(const void *a_, const void *b_)
125 {
126     const struct ovsdb_monitor_column *a = a_;
127     const struct ovsdb_monitor_column *b = b_;
128
129     return a->column < b->column ? -1 : a->column > b->column;
130 }
131
132 static struct ovsdb_monitor *
133 ovsdb_monitor_cast(struct ovsdb_replica *replica)
134 {
135     ovs_assert(replica->class == &ovsdb_jsonrpc_replica_class);
136     return CONTAINER_OF(replica, struct ovsdb_monitor, replica);
137 }
138
139 /* Finds and returns the ovsdb_monitor_row in 'mt->changes->rows' for the
140  * given 'uuid', or NULL if there is no such row. */
141 static struct ovsdb_monitor_row *
142 ovsdb_monitor_changes_row_find(const struct ovsdb_monitor_changes *changes,
143                                const struct uuid *uuid)
144 {
145     struct ovsdb_monitor_row *row;
146
147     HMAP_FOR_EACH_WITH_HASH (row, hmap_node, uuid_hash(uuid),
148                              &changes->rows) {
149         if (uuid_equals(uuid, &row->uuid)) {
150             return row;
151         }
152     }
153     return NULL;
154 }
155
156 /* Allocates an array of 'mt->n_columns' ovsdb_datums and initializes them as
157  * copies of the data in 'row' drawn from the columns represented by
158  * mt->columns[].  Returns the array.
159  *
160  * If 'row' is NULL, returns NULL. */
161 static struct ovsdb_datum *
162 clone_monitor_row_data(const struct ovsdb_monitor_table *mt,
163                        const struct ovsdb_row *row)
164 {
165     struct ovsdb_datum *data;
166     size_t i;
167
168     if (!row) {
169         return NULL;
170     }
171
172     data = xmalloc(mt->n_columns * sizeof *data);
173     for (i = 0; i < mt->n_columns; i++) {
174         const struct ovsdb_column *c = mt->columns[i].column;
175         const struct ovsdb_datum *src = &row->fields[c->index];
176         struct ovsdb_datum *dst = &data[i];
177         const struct ovsdb_type *type = &c->type;
178
179         ovsdb_datum_clone(dst, src, type);
180     }
181     return data;
182 }
183
184 /* Replaces the mt->n_columns ovsdb_datums in row[] by copies of the data from
185  * in 'row' drawn from the columns represented by mt->columns[]. */
186 static void
187 update_monitor_row_data(const struct ovsdb_monitor_table *mt,
188                         const struct ovsdb_row *row,
189                         struct ovsdb_datum *data)
190 {
191     size_t i;
192
193     for (i = 0; i < mt->n_columns; i++) {
194         const struct ovsdb_column *c = mt->columns[i].column;
195         const struct ovsdb_datum *src = &row->fields[c->index];
196         struct ovsdb_datum *dst = &data[i];
197         const struct ovsdb_type *type = &c->type;
198
199         if (!ovsdb_datum_equals(src, dst, type)) {
200             ovsdb_datum_destroy(dst, type);
201             ovsdb_datum_clone(dst, src, type);
202         }
203     }
204 }
205
206 /* Frees all of the mt->n_columns ovsdb_datums in data[], using the types taken
207  * from mt->columns[], plus 'data' itself. */
208 static void
209 free_monitor_row_data(const struct ovsdb_monitor_table *mt,
210                       struct ovsdb_datum *data)
211 {
212     if (data) {
213         size_t i;
214
215         for (i = 0; i < mt->n_columns; i++) {
216             const struct ovsdb_column *c = mt->columns[i].column;
217
218             ovsdb_datum_destroy(&data[i], &c->type);
219         }
220         free(data);
221     }
222 }
223
224 /* Frees 'row', which must have been created from 'mt'. */
225 static void
226 ovsdb_monitor_row_destroy(const struct ovsdb_monitor_table *mt,
227                           struct ovsdb_monitor_row *row)
228 {
229     if (row) {
230         free_monitor_row_data(mt, row->old);
231         free_monitor_row_data(mt, row->new);
232         free(row);
233     }
234 }
235
236 void
237 ovsdb_monitor_add_jsonrpc_monitor(struct ovsdb_monitor *dbmon,
238                                   struct ovsdb_jsonrpc_monitor *jsonrpc_monitor)
239 {
240     struct jsonrpc_monitor_node *jm;
241
242     jm = xzalloc(sizeof *jm);
243     jm->jsonrpc_monitor = jsonrpc_monitor;
244     list_push_back(&dbmon->jsonrpc_monitors, &jm->node);
245 }
246
247 struct ovsdb_monitor *
248 ovsdb_monitor_create(struct ovsdb *db,
249                      struct ovsdb_jsonrpc_monitor *jsonrpc_monitor)
250 {
251     struct ovsdb_monitor *dbmon;
252
253     dbmon = xzalloc(sizeof *dbmon);
254
255     ovsdb_replica_init(&dbmon->replica, &ovsdb_jsonrpc_replica_class);
256     ovsdb_add_replica(db, &dbmon->replica);
257     list_init(&dbmon->jsonrpc_monitors);
258     dbmon->db = db;
259     dbmon->n_transactions = 0;
260     shash_init(&dbmon->tables);
261     hmap_node_nullify(&dbmon->hmap_node);
262
263     ovsdb_monitor_add_jsonrpc_monitor(dbmon, jsonrpc_monitor);
264     return dbmon;
265 }
266
267 void
268 ovsdb_monitor_add_table(struct ovsdb_monitor *m,
269                         const struct ovsdb_table *table)
270 {
271     struct ovsdb_monitor_table *mt;
272
273     mt = xzalloc(sizeof *mt);
274     mt->table = table;
275     shash_add(&m->tables, table->schema->name, mt);
276     hmap_init(&mt->changes);
277 }
278
279 void
280 ovsdb_monitor_add_column(struct ovsdb_monitor *dbmon,
281                          const struct ovsdb_table *table,
282                          const struct ovsdb_column *column,
283                          enum ovsdb_monitor_selection select,
284                          size_t *allocated_columns)
285 {
286     struct ovsdb_monitor_table *mt;
287     struct ovsdb_monitor_column *c;
288
289     mt = shash_find_data(&dbmon->tables, table->schema->name);
290
291     if (mt->n_columns >= *allocated_columns) {
292         mt->columns = x2nrealloc(mt->columns, allocated_columns,
293                                  sizeof *mt->columns);
294     }
295
296     mt->select |= select;
297     c = &mt->columns[mt->n_columns++];
298     c->column = column;
299     c->select = select;
300 }
301
302 /* Check for duplicated column names. Return the first
303  * duplicated column's name if found. Otherwise return
304  * NULL.  */
305 const char * OVS_WARN_UNUSED_RESULT
306 ovsdb_monitor_table_check_duplicates(struct ovsdb_monitor *m,
307                                      const struct ovsdb_table *table)
308 {
309     struct ovsdb_monitor_table *mt;
310     int i;
311
312     mt = shash_find_data(&m->tables, table->schema->name);
313
314     if (mt) {
315         /* Check for duplicate columns. */
316         qsort(mt->columns, mt->n_columns, sizeof *mt->columns,
317               compare_ovsdb_monitor_column);
318         for (i = 1; i < mt->n_columns; i++) {
319             if (mt->columns[i].column == mt->columns[i - 1].column) {
320                 return mt->columns[i].column->name;
321             }
322         }
323     }
324
325     return NULL;
326 }
327
328 static struct ovsdb_monitor_changes *
329 ovsdb_monitor_table_add_changes(struct ovsdb_monitor_table *mt,
330                                 uint64_t next_txn)
331 {
332     struct ovsdb_monitor_changes *changes;
333
334     changes = xzalloc(sizeof *changes);
335
336     changes->transaction = next_txn;
337     changes->mt = mt;
338     changes->n_refs = 1;
339     hmap_init(&changes->rows);
340     hmap_insert(&mt->changes, &changes->hmap_node, hash_uint64(next_txn));
341
342     return changes;
343 };
344
345 static struct ovsdb_monitor_changes *
346 ovsdb_monitor_table_find_changes(struct ovsdb_monitor_table *mt,
347                                  uint64_t transaction)
348 {
349     struct ovsdb_monitor_changes *changes;
350     size_t hash = hash_uint64(transaction);
351
352     HMAP_FOR_EACH_WITH_HASH(changes, hmap_node, hash, &mt->changes) {
353         if (changes->transaction == transaction) {
354             return changes;
355         }
356     }
357
358     return NULL;
359 }
360
361 /* Stop currently tracking changes to table 'mt' since 'transaction'.
362  *
363  * Return 'true' if the 'transaction' is being tracked. 'false' otherwise. */
364 static void
365 ovsdb_monitor_table_untrack_changes(struct ovsdb_monitor_table *mt,
366                                     uint64_t transaction)
367 {
368     struct ovsdb_monitor_changes *changes =
369                 ovsdb_monitor_table_find_changes(mt, transaction);
370     if (changes) {
371         if (--changes->n_refs == 0) {
372             hmap_remove(&mt->changes, &changes->hmap_node);
373             ovsdb_monitor_changes_destroy(changes);
374         }
375     }
376 }
377
378 /* Start tracking changes to table 'mt' begins from 'transaction' inclusive.
379  */
380 static void
381 ovsdb_monitor_table_track_changes(struct ovsdb_monitor_table *mt,
382                                   uint64_t transaction)
383 {
384     struct ovsdb_monitor_changes *changes;
385
386     changes = ovsdb_monitor_table_find_changes(mt, transaction);
387     if (changes) {
388         changes->n_refs++;
389     } else {
390         ovsdb_monitor_table_add_changes(mt, transaction);
391     }
392 }
393
394 static void
395 ovsdb_monitor_changes_destroy(struct ovsdb_monitor_changes *changes)
396 {
397     struct ovsdb_monitor_row *row, *next;
398
399     HMAP_FOR_EACH_SAFE (row, next, hmap_node, &changes->rows) {
400         hmap_remove(&changes->rows, &row->hmap_node);
401         ovsdb_monitor_row_destroy(changes->mt, row);
402     }
403     hmap_destroy(&changes->rows);
404     free(changes);
405 }
406
407 /* Returns JSON for a <row-update> (as described in RFC 7047) for 'row' within
408  * 'mt', or NULL if no row update should be sent.
409  *
410  * The caller should specify 'initial' as true if the returned JSON is going to
411  * be used as part of the initial reply to a "monitor" request, false if it is
412  * going to be used as part of an "update" notification.
413  *
414  * 'changed' must be a scratch buffer for internal use that is at least
415  * bitmap_n_bytes(mt->n_columns) bytes long. */
416 static struct json *
417 ovsdb_monitor_compose_row_update(
418     const struct ovsdb_monitor_table *mt,
419     const struct ovsdb_monitor_row *row,
420     bool initial, unsigned long int *changed)
421 {
422     enum ovsdb_monitor_selection type;
423     struct json *old_json, *new_json;
424     struct json *row_json;
425     size_t i;
426
427     type = (initial ? OJMS_INITIAL
428             : !row->old ? OJMS_INSERT
429             : !row->new ? OJMS_DELETE
430             : OJMS_MODIFY);
431     if (!(mt->select & type)) {
432         return NULL;
433     }
434
435     if (type == OJMS_MODIFY) {
436         size_t n_changes;
437
438         n_changes = 0;
439         memset(changed, 0, bitmap_n_bytes(mt->n_columns));
440         for (i = 0; i < mt->n_columns; i++) {
441             const struct ovsdb_column *c = mt->columns[i].column;
442             if (!ovsdb_datum_equals(&row->old[i], &row->new[i], &c->type)) {
443                 bitmap_set1(changed, i);
444                 n_changes++;
445             }
446         }
447         if (!n_changes) {
448             /* No actual changes: presumably a row changed and then
449              * changed back later. */
450             return NULL;
451         }
452     }
453
454     row_json = json_object_create();
455     old_json = new_json = NULL;
456     if (type & (OJMS_DELETE | OJMS_MODIFY)) {
457         old_json = json_object_create();
458         json_object_put(row_json, "old", old_json);
459     }
460     if (type & (OJMS_INITIAL | OJMS_INSERT | OJMS_MODIFY)) {
461         new_json = json_object_create();
462         json_object_put(row_json, "new", new_json);
463     }
464     for (i = 0; i < mt->n_columns; i++) {
465         const struct ovsdb_monitor_column *c = &mt->columns[i];
466
467         if (!(type & c->select)) {
468             /* We don't care about this type of change for this
469              * particular column (but we will care about it for some
470              * other column). */
471             continue;
472         }
473
474         if ((type == OJMS_MODIFY && bitmap_is_set(changed, i))
475             || type == OJMS_DELETE) {
476             json_object_put(old_json, c->column->name,
477                             ovsdb_datum_to_json(&row->old[i],
478                                                 &c->column->type));
479         }
480         if (type & (OJMS_INITIAL | OJMS_INSERT | OJMS_MODIFY)) {
481             json_object_put(new_json, c->column->name,
482                             ovsdb_datum_to_json(&row->new[i],
483                                                 &c->column->type));
484         }
485     }
486
487     return row_json;
488 }
489
490 /* Constructs and returns JSON for a <table-updates> object (as described in
491  * RFC 7047) for all the outstanding changes within 'monitor', and deletes all
492  * the outstanding changes from 'monitor'.  Returns NULL if no update needs to
493  * be sent.
494  *
495  * The caller should specify 'initial' as true if the returned JSON is going to
496  * be used as part of the initial reply to a "monitor" request, false if it is
497  * going to be used as part of an "update" notification.
498  *
499  * 'unflushed' should point to value that is the transaction ID that did
500  * was not updated. The update contains changes between
501  * ['unflushed, ovsdb->n_transcations]. Before the function returns, this
502  * value will be updated to ovsdb->n_transactions + 1, ready for the next
503  * update.  */
504 struct json *
505 ovsdb_monitor_compose_update(const struct ovsdb_monitor *dbmon,
506                              bool initial, uint64_t *unflushed)
507 {
508     struct shash_node *node;
509     unsigned long int *changed;
510     struct json *json;
511     size_t max_columns;
512     uint64_t prev_txn = *unflushed;
513     uint64_t next_txn = dbmon->n_transactions + 1;
514
515     max_columns = 0;
516     SHASH_FOR_EACH (node, &dbmon->tables) {
517         struct ovsdb_monitor_table *mt = node->data;
518
519         max_columns = MAX(max_columns, mt->n_columns);
520     }
521     changed = xmalloc(bitmap_n_bytes(max_columns));
522
523     json = NULL;
524     SHASH_FOR_EACH (node, &dbmon->tables) {
525         struct ovsdb_monitor_table *mt = node->data;
526         struct ovsdb_monitor_row *row, *next;
527         struct ovsdb_monitor_changes *changes;
528         struct json *table_json = NULL;
529
530         changes = ovsdb_monitor_table_find_changes(mt, prev_txn);
531         if (!changes) {
532             ovsdb_monitor_table_track_changes(mt, next_txn);
533             continue;
534         }
535
536         HMAP_FOR_EACH_SAFE (row, next, hmap_node, &changes->rows) {
537             struct json *row_json;
538
539             row_json = ovsdb_monitor_compose_row_update(
540                 mt, row, initial, changed);
541             if (row_json) {
542                 char uuid[UUID_LEN + 1];
543
544                 /* Create JSON object for transaction overall. */
545                 if (!json) {
546                     json = json_object_create();
547                 }
548
549                 /* Create JSON object for transaction on this table. */
550                 if (!table_json) {
551                     table_json = json_object_create();
552                     json_object_put(json, mt->table->schema->name, table_json);
553                 }
554
555                 /* Add JSON row to JSON table. */
556                 snprintf(uuid, sizeof uuid, UUID_FMT, UUID_ARGS(&row->uuid));
557                 json_object_put(table_json, uuid, row_json);
558             }
559         }
560
561         ovsdb_monitor_table_untrack_changes(mt, prev_txn);
562         ovsdb_monitor_table_track_changes(mt, next_txn);
563     }
564
565     *unflushed = next_txn;
566     free(changed);
567     return json;
568 }
569
570 bool
571 ovsdb_monitor_needs_flush(struct ovsdb_monitor *dbmon,
572                           uint64_t next_transaction)
573 {
574     ovs_assert(next_transaction <= dbmon->n_transactions + 1);
575     return (next_transaction <= dbmon->n_transactions);
576 }
577
578 void
579 ovsdb_monitor_table_add_select(struct ovsdb_monitor *dbmon,
580                                const struct ovsdb_table *table,
581                                enum ovsdb_monitor_selection select)
582 {
583     struct ovsdb_monitor_table * mt;
584
585     mt = shash_find_data(&dbmon->tables, table->schema->name);
586     mt->select |= select;
587 }
588
589 struct ovsdb_monitor_aux {
590     const struct ovsdb_monitor *monitor;
591     struct ovsdb_monitor_table *mt;
592 };
593
594 static void
595 ovsdb_monitor_init_aux(struct ovsdb_monitor_aux *aux,
596                        const struct ovsdb_monitor *m)
597 {
598     aux->monitor = m;
599     aux->mt = NULL;
600 }
601
602 static void
603 ovsdb_monitor_changes_update(const struct ovsdb_row *old,
604                              const struct ovsdb_row *new,
605                              const struct ovsdb_monitor_table *mt,
606                              struct ovsdb_monitor_changes *changes)
607 {
608     const struct uuid *uuid = ovsdb_row_get_uuid(new ? new : old);
609     struct ovsdb_monitor_row *change;
610
611     change = ovsdb_monitor_changes_row_find(changes, uuid);
612     if (!change) {
613         change = xzalloc(sizeof *change);
614         hmap_insert(&changes->rows, &change->hmap_node, uuid_hash(uuid));
615         change->uuid = *uuid;
616         change->old = clone_monitor_row_data(mt, old);
617         change->new = clone_monitor_row_data(mt, new);
618     } else {
619         if (new) {
620             update_monitor_row_data(mt, new, change->new);
621         } else {
622             free_monitor_row_data(mt, change->new);
623             change->new = NULL;
624
625             if (!change->old) {
626                 /* This row was added then deleted.  Forget about it. */
627                 hmap_remove(&changes->rows, &change->hmap_node);
628                 free(change);
629             }
630         }
631     }
632 }
633
634 static bool
635 ovsdb_monitor_change_cb(const struct ovsdb_row *old,
636                         const struct ovsdb_row *new,
637                         const unsigned long int *changed OVS_UNUSED,
638                         void *aux_)
639 {
640     struct ovsdb_monitor_aux *aux = aux_;
641     const struct ovsdb_monitor *m = aux->monitor;
642     struct ovsdb_table *table = new ? new->table : old->table;
643     struct ovsdb_monitor_table *mt;
644     struct ovsdb_monitor_changes *changes;
645
646     if (!aux->mt || table != aux->mt->table) {
647         aux->mt = shash_find_data(&m->tables, table->schema->name);
648         if (!aux->mt) {
649             /* We don't care about rows in this table at all.  Tell the caller
650              * to skip it.  */
651             return false;
652         }
653     }
654     mt = aux->mt;
655
656     HMAP_FOR_EACH(changes, hmap_node, &mt->changes) {
657         ovsdb_monitor_changes_update(old, new, mt, changes);
658     }
659     return true;
660 }
661
662 void
663 ovsdb_monitor_get_initial(const struct ovsdb_monitor *dbmon)
664 {
665     struct ovsdb_monitor_aux aux;
666     struct shash_node *node;
667
668     ovsdb_monitor_init_aux(&aux, dbmon);
669     SHASH_FOR_EACH (node, &dbmon->tables) {
670         struct ovsdb_monitor_table *mt = node->data;
671
672         if (mt->select & OJMS_INITIAL) {
673             struct ovsdb_row *row;
674             struct ovsdb_monitor_changes *changes;
675
676             changes = ovsdb_monitor_table_find_changes(mt, 0);
677             if (!changes) {
678                 changes = ovsdb_monitor_table_add_changes(mt, 0);
679                 HMAP_FOR_EACH (row, hmap_node, &mt->table->rows) {
680                     ovsdb_monitor_changes_update(NULL, row, mt, changes);
681                 }
682             } else {
683                 changes->n_refs++;
684             }
685         }
686     }
687 }
688
689 void
690 ovsdb_monitor_remove_jsonrpc_monitor(struct ovsdb_monitor *dbmon,
691                    struct ovsdb_jsonrpc_monitor *jsonrpc_monitor)
692 {
693     struct jsonrpc_monitor_node *jm;
694
695     if (list_is_empty(&dbmon->jsonrpc_monitors)) {
696         ovsdb_monitor_destroy(dbmon);
697         return;
698     }
699
700     /* Find and remove the jsonrpc monitor from the list.  */
701     LIST_FOR_EACH(jm, node, &dbmon->jsonrpc_monitors) {
702         if (jm->jsonrpc_monitor == jsonrpc_monitor) {
703             list_remove(&jm->node);
704             free(jm);
705
706             /* Destroy ovsdb monitor if this is the last user.  */
707             if (list_is_empty(&dbmon->jsonrpc_monitors)) {
708                 ovsdb_monitor_destroy(dbmon);
709             }
710
711             return;
712         };
713     }
714
715     /* Should never reach here. jsonrpc_monitor should be on the list.  */
716     OVS_NOT_REACHED();
717 }
718
719 static bool
720 ovsdb_monitor_table_equal(const struct ovsdb_monitor_table *a,
721                           const struct ovsdb_monitor_table *b)
722 {
723     size_t i;
724
725     if ((a->table != b->table) ||
726         (a->select != b->select) ||
727         (a->n_columns != b->n_columns)) {
728         return false;
729     }
730
731     for (i = 0; i < a->n_columns; i++) {
732         if ((a->columns[i].column != b->columns[i].column) ||
733             (a->columns[i].select != b->columns[i].select)) {
734             return false;
735         }
736     }
737
738     return true;
739 }
740
741 static bool
742 ovsdb_monitor_equal(const struct ovsdb_monitor *a,
743                     const struct ovsdb_monitor *b)
744 {
745     struct shash_node *node;
746
747     if (shash_count(&a->tables) != shash_count(&b->tables)) {
748         return false;
749     }
750
751     SHASH_FOR_EACH(node, &a->tables) {
752         const struct ovsdb_monitor_table *mta = node->data;
753         const struct ovsdb_monitor_table *mtb;
754
755         mtb = shash_find_data(&b->tables, node->name);
756         if (!mtb) {
757             return false;
758         }
759
760         if (!ovsdb_monitor_table_equal(mta, mtb)) {
761             return false;
762         }
763     }
764
765     return true;
766 }
767
768 static size_t
769 ovsdb_monitor_hash(const struct ovsdb_monitor *dbmon, size_t basis)
770 {
771     const struct shash_node **nodes;
772     size_t i, j, n;
773
774     nodes = shash_sort(&dbmon->tables);
775     n = shash_count(&dbmon->tables);
776
777     for (i = 0; i < n; i++) {
778         struct ovsdb_monitor_table *mt = nodes[i]->data;
779
780         basis = hash_pointer(mt->table, basis);
781         basis = hash_3words(mt->select, mt->n_columns, basis);
782
783         for (j = 0; j < mt->n_columns; j++) {
784             basis = hash_pointer(mt->columns[j].column, basis);
785             basis = hash_2words(mt->columns[j].select, basis);
786         }
787     }
788     free(nodes);
789
790     return basis;
791 }
792
793 struct ovsdb_monitor *
794 ovsdb_monitor_add(struct ovsdb_monitor *new_dbmon)
795 {
796     struct ovsdb_monitor *dbmon;
797     size_t hash;
798
799     /* New_dbmon should be associated with only one jsonrpc
800      * connections.  */
801     ovs_assert(list_is_singleton(&new_dbmon->jsonrpc_monitors));
802
803     hash = ovsdb_monitor_hash(new_dbmon, 0);
804     HMAP_FOR_EACH_WITH_HASH(dbmon, hmap_node, hash, &ovsdb_monitors) {
805         if (ovsdb_monitor_equal(dbmon,  new_dbmon)) {
806             return dbmon;
807         }
808     }
809
810     hmap_insert(&ovsdb_monitors, &new_dbmon->hmap_node, hash);
811     return new_dbmon;
812 }
813
814 static void
815 ovsdb_monitor_destroy(struct ovsdb_monitor *dbmon)
816 {
817     struct shash_node *node;
818
819     list_remove(&dbmon->replica.node);
820
821     if (!hmap_node_is_null(&dbmon->hmap_node)) {
822         hmap_remove(&ovsdb_monitors, &dbmon->hmap_node);
823     }
824
825     SHASH_FOR_EACH (node, &dbmon->tables) {
826         struct ovsdb_monitor_table *mt = node->data;
827         struct ovsdb_monitor_changes *changes, *next;
828
829         HMAP_FOR_EACH_SAFE (changes, next, hmap_node, &mt->changes) {
830             hmap_remove(&mt->changes, &changes->hmap_node);
831             ovsdb_monitor_changes_destroy(changes);
832         }
833         free(mt->columns);
834         free(mt);
835     }
836     shash_destroy(&dbmon->tables);
837     free(dbmon);
838 }
839
840 static struct ovsdb_error *
841 ovsdb_monitor_commit(struct ovsdb_replica *replica,
842                      const struct ovsdb_txn *txn,
843                      bool durable OVS_UNUSED)
844 {
845     struct ovsdb_monitor *m = ovsdb_monitor_cast(replica);
846     struct ovsdb_monitor_aux aux;
847
848     ovsdb_monitor_init_aux(&aux, m);
849     ovsdb_txn_for_each_change(txn, ovsdb_monitor_change_cb, &aux);
850     m->n_transactions++;
851
852     return NULL;
853 }
854
855 static void
856 ovsdb_monitor_destroy_callback(struct ovsdb_replica *replica)
857 {
858     struct ovsdb_monitor *dbmon = ovsdb_monitor_cast(replica);
859     struct jsonrpc_monitor_node *jm, *next;
860
861     /* Delete all front end monitors. Removing the last front
862      * end monitor will also destroy the corresponding 'ovsdb_monitor'.
863      * ovsdb monitor will also be destroied.  */
864     LIST_FOR_EACH_SAFE(jm, next, node, &dbmon->jsonrpc_monitors) {
865         ovsdb_jsonrpc_monitor_destroy(jm->jsonrpc_monitor);
866     }
867 }
868
869 static const struct ovsdb_replica_class ovsdb_jsonrpc_replica_class = {
870     ovsdb_monitor_commit,
871     ovsdb_monitor_destroy_callback,
872 };