ovsdb-monitor: add transaction ids
[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 "table.h"
32 #include "timeval.h"
33 #include "transaction.h"
34 #include "jsonrpc-server.h"
35 #include "monitor.h"
36 #include "openvswitch/vlog.h"
37
38
39 static const struct ovsdb_replica_class ovsdb_jsonrpc_replica_class;
40
41 /*  Backend monitor.
42  *
43  *  ovsdb_monitor keep track of the ovsdb changes.
44  */
45
46 /* A collection of tables being monitored. */
47 struct ovsdb_monitor {
48     struct ovsdb_replica replica;
49     struct shash tables;     /* Holds "struct ovsdb_monitor_table"s. */
50     struct ovs_list jsonrpc_monitors;  /* Contains "jsonrpc_monitor_node"s. */
51     struct ovsdb *db;
52     uint64_t n_transactions;      /* Count number of committed transactions. */
53 };
54
55 struct jsonrpc_monitor_node {
56     struct ovsdb_jsonrpc_monitor *jsonrpc_monitor;
57     struct ovs_list node;
58 };
59
60 /* A particular column being monitored. */
61 struct ovsdb_monitor_column {
62     const struct ovsdb_column *column;
63     enum ovsdb_monitor_selection select;
64 };
65
66 /* A row that has changed in a monitored table. */
67 struct ovsdb_monitor_row {
68     struct hmap_node hmap_node; /* In ovsdb_jsonrpc_monitor_table.changes. */
69     struct uuid uuid;           /* UUID of row that changed. */
70     struct ovsdb_datum *old;    /* Old data, NULL for an inserted row. */
71     struct ovsdb_datum *new;    /* New data, NULL for a deleted row. */
72 };
73
74 /* A particular table being monitored. */
75 struct ovsdb_monitor_table {
76     const struct ovsdb_table *table;
77
78     /* This is the union (bitwise-OR) of the 'select' values in all of the
79      * members of 'columns' below. */
80     enum ovsdb_monitor_selection select;
81
82     /* Columns being monitored. */
83     struct ovsdb_monitor_column *columns;
84     size_t n_columns;
85
86     /* Contains 'struct ovsdb_monitor_row's for rows that have been
87      * updated but not yet flushed to the jsonrpc connection. */
88     struct hmap changes;
89 };
90
91 static void ovsdb_monitor_destroy(struct ovsdb_monitor *dbmon);
92
93 static int
94 compare_ovsdb_monitor_column(const void *a_, const void *b_)
95 {
96     const struct ovsdb_monitor_column *a = a_;
97     const struct ovsdb_monitor_column *b = b_;
98
99     return a->column < b->column ? -1 : a->column > b->column;
100 }
101
102 static struct ovsdb_monitor *
103 ovsdb_monitor_cast(struct ovsdb_replica *replica)
104 {
105     ovs_assert(replica->class == &ovsdb_jsonrpc_replica_class);
106     return CONTAINER_OF(replica, struct ovsdb_monitor, replica);
107 }
108
109 /* Finds and returns the ovsdb_monitor_row in 'mt->changes' for the
110  * given 'uuid', or NULL if there is no such row. */
111 static struct ovsdb_monitor_row *
112 ovsdb_monitor_row_find(const struct ovsdb_monitor_table *mt,
113                        const struct uuid *uuid)
114 {
115     struct ovsdb_monitor_row *row;
116
117     HMAP_FOR_EACH_WITH_HASH (row, hmap_node, uuid_hash(uuid), &mt->changes) {
118         if (uuid_equals(uuid, &row->uuid)) {
119             return row;
120         }
121     }
122     return NULL;
123 }
124
125 /* Allocates an array of 'mt->n_columns' ovsdb_datums and initializes them as
126  * copies of the data in 'row' drawn from the columns represented by
127  * mt->columns[].  Returns the array.
128  *
129  * If 'row' is NULL, returns NULL. */
130 static struct ovsdb_datum *
131 clone_monitor_row_data(const struct ovsdb_monitor_table *mt,
132                        const struct ovsdb_row *row)
133 {
134     struct ovsdb_datum *data;
135     size_t i;
136
137     if (!row) {
138         return NULL;
139     }
140
141     data = xmalloc(mt->n_columns * sizeof *data);
142     for (i = 0; i < mt->n_columns; i++) {
143         const struct ovsdb_column *c = mt->columns[i].column;
144         const struct ovsdb_datum *src = &row->fields[c->index];
145         struct ovsdb_datum *dst = &data[i];
146         const struct ovsdb_type *type = &c->type;
147
148         ovsdb_datum_clone(dst, src, type);
149     }
150     return data;
151 }
152
153 /* Replaces the mt->n_columns ovsdb_datums in row[] by copies of the data from
154  * in 'row' drawn from the columns represented by mt->columns[]. */
155 static void
156 update_monitor_row_data(const struct ovsdb_monitor_table *mt,
157                         const struct ovsdb_row *row,
158                         struct ovsdb_datum *data)
159 {
160     size_t i;
161
162     for (i = 0; i < mt->n_columns; i++) {
163         const struct ovsdb_column *c = mt->columns[i].column;
164         const struct ovsdb_datum *src = &row->fields[c->index];
165         struct ovsdb_datum *dst = &data[i];
166         const struct ovsdb_type *type = &c->type;
167
168         if (!ovsdb_datum_equals(src, dst, type)) {
169             ovsdb_datum_destroy(dst, type);
170             ovsdb_datum_clone(dst, src, type);
171         }
172     }
173 }
174
175 /* Frees all of the mt->n_columns ovsdb_datums in data[], using the types taken
176  * from mt->columns[], plus 'data' itself. */
177 static void
178 free_monitor_row_data(const struct ovsdb_monitor_table *mt,
179                       struct ovsdb_datum *data)
180 {
181     if (data) {
182         size_t i;
183
184         for (i = 0; i < mt->n_columns; i++) {
185             const struct ovsdb_column *c = mt->columns[i].column;
186
187             ovsdb_datum_destroy(&data[i], &c->type);
188         }
189         free(data);
190     }
191 }
192
193 /* Frees 'row', which must have been created from 'mt'. */
194 static void
195 ovsdb_monitor_row_destroy(const struct ovsdb_monitor_table *mt,
196                           struct ovsdb_monitor_row *row)
197 {
198     if (row) {
199         free_monitor_row_data(mt, row->old);
200         free_monitor_row_data(mt, row->new);
201         free(row);
202     }
203 }
204
205 struct ovsdb_monitor *
206 ovsdb_monitor_create(struct ovsdb *db,
207                      struct ovsdb_jsonrpc_monitor *jsonrpc_monitor)
208 {
209     struct ovsdb_monitor *dbmon;
210     struct jsonrpc_monitor_node *jm;
211
212     dbmon = xzalloc(sizeof *dbmon);
213
214     ovsdb_replica_init(&dbmon->replica, &ovsdb_jsonrpc_replica_class);
215     ovsdb_add_replica(db, &dbmon->replica);
216     list_init(&dbmon->jsonrpc_monitors);
217     dbmon->db = db;
218     dbmon->n_transactions = 0;
219     shash_init(&dbmon->tables);
220
221     jm = xzalloc(sizeof *jm);
222     jm->jsonrpc_monitor = jsonrpc_monitor;
223     list_push_back(&dbmon->jsonrpc_monitors, &jm->node);
224
225     return dbmon;
226 }
227
228 void
229 ovsdb_monitor_add_table(struct ovsdb_monitor *m,
230                         const struct ovsdb_table *table)
231 {
232     struct ovsdb_monitor_table *mt;
233
234     mt = xzalloc(sizeof *mt);
235     mt->table = table;
236     hmap_init(&mt->changes);
237     shash_add(&m->tables, table->schema->name, mt);
238 }
239
240 void
241 ovsdb_monitor_add_column(struct ovsdb_monitor *dbmon,
242                          const struct ovsdb_table *table,
243                          const struct ovsdb_column *column,
244                          enum ovsdb_monitor_selection select,
245                          size_t *allocated_columns)
246 {
247     struct ovsdb_monitor_table *mt;
248     struct ovsdb_monitor_column *c;
249
250     mt = shash_find_data(&dbmon->tables, table->schema->name);
251
252     if (mt->n_columns >= *allocated_columns) {
253         mt->columns = x2nrealloc(mt->columns, allocated_columns,
254                                  sizeof *mt->columns);
255     }
256
257     mt->select |= select;
258     c = &mt->columns[mt->n_columns++];
259     c->column = column;
260     c->select = select;
261 }
262
263 /* Check for duplicated column names. Return the first
264  * duplicated column's name if found. Otherwise return
265  * NULL.  */
266 const char * OVS_WARN_UNUSED_RESULT
267 ovsdb_monitor_table_check_duplicates(struct ovsdb_monitor *m,
268                                      const struct ovsdb_table *table)
269 {
270     struct ovsdb_monitor_table *mt;
271     int i;
272
273     mt = shash_find_data(&m->tables, table->schema->name);
274
275     if (mt) {
276         /* Check for duplicate columns. */
277         qsort(mt->columns, mt->n_columns, sizeof *mt->columns,
278               compare_ovsdb_monitor_column);
279         for (i = 1; i < mt->n_columns; i++) {
280             if (mt->columns[i].column == mt->columns[i - 1].column) {
281                 return mt->columns[i].column->name;
282             }
283         }
284     }
285
286     return NULL;
287 }
288
289 /* Returns JSON for a <row-update> (as described in RFC 7047) for 'row' within
290  * 'mt', or NULL if no row update should be sent.
291  *
292  * The caller should specify 'initial' as true if the returned JSON is going to
293  * be used as part of the initial reply to a "monitor" request, false if it is
294  * going to be used as part of an "update" notification.
295  *
296  * 'changed' must be a scratch buffer for internal use that is at least
297  * bitmap_n_bytes(mt->n_columns) bytes long. */
298 static struct json *
299 ovsdb_monitor_compose_row_update(
300     const struct ovsdb_monitor_table *mt,
301     const struct ovsdb_monitor_row *row,
302     bool initial, unsigned long int *changed)
303 {
304     enum ovsdb_monitor_selection type;
305     struct json *old_json, *new_json;
306     struct json *row_json;
307     size_t i;
308
309     type = (initial ? OJMS_INITIAL
310             : !row->old ? OJMS_INSERT
311             : !row->new ? OJMS_DELETE
312             : OJMS_MODIFY);
313     if (!(mt->select & type)) {
314         return NULL;
315     }
316
317     if (type == OJMS_MODIFY) {
318         size_t n_changes;
319
320         n_changes = 0;
321         memset(changed, 0, bitmap_n_bytes(mt->n_columns));
322         for (i = 0; i < mt->n_columns; i++) {
323             const struct ovsdb_column *c = mt->columns[i].column;
324             if (!ovsdb_datum_equals(&row->old[i], &row->new[i], &c->type)) {
325                 bitmap_set1(changed, i);
326                 n_changes++;
327             }
328         }
329         if (!n_changes) {
330             /* No actual changes: presumably a row changed and then
331              * changed back later. */
332             return NULL;
333         }
334     }
335
336     row_json = json_object_create();
337     old_json = new_json = NULL;
338     if (type & (OJMS_DELETE | OJMS_MODIFY)) {
339         old_json = json_object_create();
340         json_object_put(row_json, "old", old_json);
341     }
342     if (type & (OJMS_INITIAL | OJMS_INSERT | OJMS_MODIFY)) {
343         new_json = json_object_create();
344         json_object_put(row_json, "new", new_json);
345     }
346     for (i = 0; i < mt->n_columns; i++) {
347         const struct ovsdb_monitor_column *c = &mt->columns[i];
348
349         if (!(type & c->select)) {
350             /* We don't care about this type of change for this
351              * particular column (but we will care about it for some
352              * other column). */
353             continue;
354         }
355
356         if ((type == OJMS_MODIFY && bitmap_is_set(changed, i))
357             || type == OJMS_DELETE) {
358             json_object_put(old_json, c->column->name,
359                             ovsdb_datum_to_json(&row->old[i],
360                                                 &c->column->type));
361         }
362         if (type & (OJMS_INITIAL | OJMS_INSERT | OJMS_MODIFY)) {
363             json_object_put(new_json, c->column->name,
364                             ovsdb_datum_to_json(&row->new[i],
365                                                 &c->column->type));
366         }
367     }
368
369     return row_json;
370 }
371
372 /* Constructs and returns JSON for a <table-updates> object (as described in
373  * RFC 7047) for all the outstanding changes within 'monitor', and deletes all
374  * the outstanding changes from 'monitor'.  Returns NULL if no update needs to
375  * be sent.
376  *
377  * The caller should specify 'initial' as true if the returned JSON is going to
378  * be used as part of the initial reply to a "monitor" request, false if it is
379  * going to be used as part of an "update" notification. */
380 struct json *
381 ovsdb_monitor_compose_table_update(const struct ovsdb_monitor *dbmon,
382                                    bool initial, uint64_t *unflushed)
383 {
384     struct shash_node *node;
385     unsigned long int *changed;
386     struct json *json;
387     size_t max_columns;
388
389     *unflushed = dbmon->n_transactions + 1;
390
391     max_columns = 0;
392     SHASH_FOR_EACH (node, &dbmon->tables) {
393         struct ovsdb_monitor_table *mt = node->data;
394
395         max_columns = MAX(max_columns, mt->n_columns);
396     }
397     changed = xmalloc(bitmap_n_bytes(max_columns));
398
399     json = NULL;
400     SHASH_FOR_EACH (node, &dbmon->tables) {
401         struct ovsdb_monitor_table *mt = node->data;
402         struct ovsdb_monitor_row *row, *next;
403         struct json *table_json = NULL;
404
405         HMAP_FOR_EACH_SAFE (row, next, hmap_node, &mt->changes) {
406             struct json *row_json;
407
408             row_json = ovsdb_monitor_compose_row_update(
409                 mt, row, initial, changed);
410             if (row_json) {
411                 char uuid[UUID_LEN + 1];
412
413                 /* Create JSON object for transaction overall. */
414                 if (!json) {
415                     json = json_object_create();
416                 }
417
418                 /* Create JSON object for transaction on this table. */
419                 if (!table_json) {
420                     table_json = json_object_create();
421                     json_object_put(json, mt->table->schema->name, table_json);
422                 }
423
424                 /* Add JSON row to JSON table. */
425                 snprintf(uuid, sizeof uuid, UUID_FMT, UUID_ARGS(&row->uuid));
426                 json_object_put(table_json, uuid, row_json);
427             }
428
429             hmap_remove(&mt->changes, &row->hmap_node);
430             ovsdb_monitor_row_destroy(mt, row);
431         }
432     }
433
434     free(changed);
435     return json;
436 }
437
438 bool
439 ovsdb_monitor_needs_flush(struct ovsdb_monitor *dbmon,
440                           uint64_t next_transaction)
441 {
442     ovs_assert(next_transaction <= dbmon->n_transactions + 1);
443     return (next_transaction <= dbmon->n_transactions);
444 }
445
446 void
447 ovsdb_monitor_table_add_select(struct ovsdb_monitor *dbmon,
448                                const struct ovsdb_table *table,
449                                enum ovsdb_monitor_selection select)
450 {
451     struct ovsdb_monitor_table * mt;
452
453     mt = shash_find_data(&dbmon->tables, table->schema->name);
454     mt->select |= select;
455 }
456
457 struct ovsdb_monitor_aux {
458     const struct ovsdb_monitor *monitor;
459     struct ovsdb_monitor_table *mt;
460 };
461
462 static void
463 ovsdb_monitor_init_aux(struct ovsdb_monitor_aux *aux,
464                        const struct ovsdb_monitor *m)
465 {
466     aux->monitor = m;
467     aux->mt = NULL;
468 }
469
470 static bool
471 ovsdb_monitor_change_cb(const struct ovsdb_row *old,
472                         const struct ovsdb_row *new,
473                         const unsigned long int *changed OVS_UNUSED,
474                         void *aux_)
475 {
476     struct ovsdb_monitor_aux *aux = aux_;
477     const struct ovsdb_monitor *m = aux->monitor;
478     struct ovsdb_table *table = new ? new->table : old->table;
479     const struct uuid *uuid = ovsdb_row_get_uuid(new ? new : old);
480     struct ovsdb_monitor_row *change;
481     struct ovsdb_monitor_table *mt;
482
483     if (!aux->mt || table != aux->mt->table) {
484         aux->mt = shash_find_data(&m->tables, table->schema->name);
485         if (!aux->mt) {
486             /* We don't care about rows in this table at all.  Tell the caller
487              * to skip it.  */
488             return false;
489         }
490     }
491     mt = aux->mt;
492
493     change = ovsdb_monitor_row_find(mt, uuid);
494     if (!change) {
495         change = xmalloc(sizeof *change);
496         hmap_insert(&mt->changes, &change->hmap_node, uuid_hash(uuid));
497         change->uuid = *uuid;
498         change->old = clone_monitor_row_data(mt, old);
499         change->new = clone_monitor_row_data(mt, new);
500     } else {
501         if (new) {
502             update_monitor_row_data(mt, new, change->new);
503         } else {
504             free_monitor_row_data(mt, change->new);
505             change->new = NULL;
506
507             if (!change->old) {
508                 /* This row was added then deleted.  Forget about it. */
509                 hmap_remove(&mt->changes, &change->hmap_node);
510                 free(change);
511             }
512         }
513     }
514     return true;
515 }
516
517 void
518 ovsdb_monitor_get_initial(const struct ovsdb_monitor *dbmon)
519 {
520     struct ovsdb_monitor_aux aux;
521     struct shash_node *node;
522
523     ovsdb_monitor_init_aux(&aux, dbmon);
524     SHASH_FOR_EACH (node, &dbmon->tables) {
525         struct ovsdb_monitor_table *mt = node->data;
526
527         if (mt->select & OJMS_INITIAL) {
528             struct ovsdb_row *row;
529
530             HMAP_FOR_EACH (row, hmap_node, &mt->table->rows) {
531                 ovsdb_monitor_change_cb(NULL, row, NULL, &aux);
532             }
533         }
534     }
535 }
536
537 void
538 ovsdb_monitor_remove_jsonrpc_monitor(struct ovsdb_monitor *dbmon,
539                    struct ovsdb_jsonrpc_monitor *jsonrpc_monitor)
540 {
541     struct jsonrpc_monitor_node *jm;
542
543     /* Find and remove the jsonrpc monitor from the list.  */
544     LIST_FOR_EACH(jm, node, &dbmon->jsonrpc_monitors) {
545         if (jm->jsonrpc_monitor == jsonrpc_monitor) {
546             list_remove(&jm->node);
547             free(jm);
548
549             /* Destroy ovsdb monitor if this is the last user.  */
550             if (list_is_empty(&dbmon->jsonrpc_monitors)) {
551                 ovsdb_monitor_destroy(dbmon);
552             }
553
554             return;
555         };
556     }
557
558     /* Should never reach here. jsonrpc_monitor should be on the list.  */
559     OVS_NOT_REACHED();
560 }
561
562 static void
563 ovsdb_monitor_destroy(struct ovsdb_monitor *dbmon)
564 {
565     struct shash_node *node;
566
567     list_remove(&dbmon->replica.node);
568
569     SHASH_FOR_EACH (node, &dbmon->tables) {
570         struct ovsdb_monitor_table *mt = node->data;
571         struct ovsdb_monitor_row *row, *next;
572
573         HMAP_FOR_EACH_SAFE (row, next, hmap_node, &mt->changes) {
574             hmap_remove(&mt->changes, &row->hmap_node);
575             ovsdb_monitor_row_destroy(mt, row);
576         }
577         hmap_destroy(&mt->changes);
578
579         free(mt->columns);
580         free(mt);
581     }
582     shash_destroy(&dbmon->tables);
583     free(dbmon);
584 }
585
586 static struct ovsdb_error *
587 ovsdb_monitor_commit(struct ovsdb_replica *replica,
588                      const struct ovsdb_txn *txn,
589                      bool durable OVS_UNUSED)
590 {
591     struct ovsdb_monitor *m = ovsdb_monitor_cast(replica);
592     struct ovsdb_monitor_aux aux;
593
594     ovsdb_monitor_init_aux(&aux, m);
595     ovsdb_txn_for_each_change(txn, ovsdb_monitor_change_cb, &aux);
596     m->n_transactions++;
597
598     return NULL;
599 }
600
601 static void
602 ovsdb_monitor_destroy_callback(struct ovsdb_replica *replica)
603 {
604     struct ovsdb_monitor *dbmon = ovsdb_monitor_cast(replica);
605     struct jsonrpc_monitor_node *jm, *next;
606
607     /* Delete all front end monitors. Removing the last front
608      * end monitor will also destroy the corresponding 'ovsdb_monitor'.
609      * ovsdb monitor will also be destroied.  */
610     LIST_FOR_EACH_SAFE(jm, next, node, &dbmon->jsonrpc_monitors) {
611         ovsdb_jsonrpc_monitor_destroy(jm->jsonrpc_monitor);
612     }
613 }
614
615 static const struct ovsdb_replica_class ovsdb_jsonrpc_replica_class = {
616     ovsdb_monitor_commit,
617     ovsdb_monitor_destroy_callback,
618 };