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