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