]> git.proxmox.com Git - mirror_ovs.git/blob - ovsdb/monitor.c
trigger: Free leaked ovsdb_schema
[mirror_ovs.git] / ovsdb / monitor.c
1 /*
2 * Copyright (c) 2015, 2017 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 "openvswitch/dynamic-string.h"
24 #include "openvswitch/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 "condition.h"
31 #include "simap.h"
32 #include "hash.h"
33 #include "table.h"
34 #include "hash.h"
35 #include "timeval.h"
36 #include "transaction.h"
37 #include "jsonrpc-server.h"
38 #include "monitor.h"
39 #include "util.h"
40 #include "openvswitch/vlog.h"
41
42 VLOG_DEFINE_THIS_MODULE(ovsdb_monitor);
43
44 static struct hmap ovsdb_monitors = HMAP_INITIALIZER(&ovsdb_monitors);
45
46 /* Keep state of session's conditions */
47 struct ovsdb_monitor_session_condition {
48 bool conditional; /* True iff every table's condition is true. */
49 struct shash tables; /* Contains
50 * "struct ovsdb_monitor_table_condition *"s. */
51 };
52
53 /* Monitored table session's conditions */
54 struct ovsdb_monitor_table_condition {
55 const struct ovsdb_table *table;
56 struct ovsdb_monitor_table *mt;
57 struct ovsdb_condition old_condition;
58 struct ovsdb_condition new_condition;
59 };
60
61 /* Backend monitor.
62 *
63 * ovsdb_monitor keep track of the ovsdb changes.
64 */
65
66 /* A collection of tables being monitored. */
67 struct ovsdb_monitor {
68 struct ovs_list list_node; /* In struct ovsdb's "monitors" list. */
69 struct shash tables; /* Holds "struct ovsdb_monitor_table"s. */
70 struct ovs_list jsonrpc_monitors; /* Contains "jsonrpc_monitor_node"s. */
71 struct ovsdb *db;
72
73 /* Contains "ovsdb_monitor_change_set". Each change set contains changes
74 * from some start point up to the latest committed transaction. There can
75 * be different change sets for the same struct ovsdb_monitor because there
76 * are different clients pending on changes starting from different points.
77 * The different change sets are maintained as a list. */
78 struct ovs_list change_sets;
79
80 /* The new change set that is to be populated for future transactions. */
81 struct ovsdb_monitor_change_set *new_change_set;
82
83 /* The change set that starts from the first transaction of the DB, which
84 * is used for populating the initial data for new clients. */
85 struct ovsdb_monitor_change_set *init_change_set;
86
87 struct hmap_node hmap_node; /* Elements within ovsdb_monitors. */
88 struct hmap json_cache; /* Contains "ovsdb_monitor_json_cache_node"s.*/
89 };
90
91 /* A json object of updates for the ovsdb_monitor_change_set and the given
92 * monitor version. */
93 struct ovsdb_monitor_json_cache_node {
94 struct hmap_node hmap_node; /* Elements in json cache. */
95 enum ovsdb_monitor_version version;
96 struct uuid change_set_uuid;
97 struct json *json; /* Null, or a cloned of json */
98 };
99
100 struct jsonrpc_monitor_node {
101 struct ovs_list node;
102 struct ovsdb_jsonrpc_monitor *jsonrpc_monitor;
103 };
104
105 /* A particular column being monitored. */
106 struct ovsdb_monitor_column {
107 const struct ovsdb_column *column;
108 enum ovsdb_monitor_selection select;
109 bool monitored;
110 };
111
112 /* A row that has changed in a monitored table. */
113 struct ovsdb_monitor_row {
114 struct hmap_node hmap_node; /* In ovsdb_monitor_change_set_for_table. */
115 struct uuid uuid; /* UUID of row that changed. */
116 struct ovsdb_datum *old; /* Old data, NULL for an inserted row. */
117 struct ovsdb_datum *new; /* New data, NULL for a deleted row. */
118 };
119
120 /* Contains a set of changes that are not yet flushed to all the jsonrpc
121 * connections.
122 *
123 * 'n_refs' represent the number of jsonrpc connections that depend on this
124 * change set (have not received updates). Generate the update for the last
125 * jsonprc connection will also destroy the whole "struct
126 * ovsdb_monitor_change_set" object.
127 */
128 struct ovsdb_monitor_change_set {
129 /* Element in change_sets of ovsdb_monitor. */
130 struct ovs_list list_node;
131
132 /* Internally generated uuid that identifies this data structure. */
133 struct uuid uuid;
134
135 /* Contains struct ovsdb_monitor_change_set_for_table. */
136 struct ovs_list change_set_for_tables;
137
138 int n_refs;
139
140 /* The previous txn id before this change set's start point. */
141 struct uuid prev_txn;
142 };
143
144 /* Contains 'struct ovsdb_monitor_row's for rows in a specific table
145 * of struct ovsdb_monitor_change_set. It can also be searched from
146 * member 'change_sets' of struct ovsdb_monitor_table. */
147 struct ovsdb_monitor_change_set_for_table {
148 /* Element in ovsdb_monitor_tables' change_sets list. */
149 struct ovs_list list_in_mt;
150
151 /* Element in ovsdb_monitor_change_sets' change_set_for_tables list. */
152 struct ovs_list list_in_change_set;
153
154 struct ovsdb_monitor_table *mt;
155 struct ovsdb_monitor_change_set *mcs;
156
157 /* Contains struct ovsdb_monitor_row. */
158 struct hmap rows;
159
160 /* Save the mt->n_columns that is used when creating the changes.
161 * It can be different from the current mt->n_columns because
162 * mt->n_columns can be increased when there are condition changes
163 * from any of the clients sharing the dbmon. */
164 size_t n_columns;
165 };
166
167 /* A particular table being monitored. */
168 struct ovsdb_monitor_table {
169 const struct ovsdb_table *table;
170
171 /* This is the union (bitwise-OR) of the 'select' values in all of the
172 * members of 'columns' below. */
173 enum ovsdb_monitor_selection select;
174
175 /* Columns being monitored. */
176 struct ovsdb_monitor_column *columns;
177 size_t n_columns;
178 size_t n_monitored_columns;
179 size_t allocated_columns;
180
181 /* Columns in ovsdb_monitor_row have different indexes then in
182 * ovsdb_row. This field maps between column->index to the index in the
183 * ovsdb_monitor_row. It is used for condition evaluation. */
184 unsigned int *columns_index_map;
185
186 /* Contains 'ovsdb_monitor_change_set_for_table'. */
187 struct ovs_list change_sets;
188 };
189
190 enum ovsdb_monitor_row_type {
191 OVSDB_ROW,
192 OVSDB_MONITOR_ROW
193 };
194
195 typedef struct json *
196 (*compose_row_update_cb_func)
197 (const struct ovsdb_monitor_table *mt,
198 const struct ovsdb_monitor_session_condition * condition,
199 enum ovsdb_monitor_row_type row_type,
200 const void *,
201 bool initial, unsigned long int *changed,
202 size_t n_columns);
203
204 static void ovsdb_monitor_destroy(struct ovsdb_monitor *);
205 static struct ovsdb_monitor_change_set * ovsdb_monitor_add_change_set(
206 struct ovsdb_monitor *, bool init_only, const struct uuid *prev_txn);
207 static struct ovsdb_monitor_change_set * ovsdb_monitor_find_change_set(
208 const struct ovsdb_monitor *, const struct uuid *prev_txn);
209 static void ovsdb_monitor_change_set_destroy(
210 struct ovsdb_monitor_change_set *);
211 static void ovsdb_monitor_track_new_change_set(struct ovsdb_monitor *);
212
213 static uint32_t
214 json_cache_hash(enum ovsdb_monitor_version version,
215 struct ovsdb_monitor_change_set *change_set)
216 {
217 return hash_uint64_basis(version, uuid_hash(&change_set->uuid));
218 }
219
220 static struct ovsdb_monitor_json_cache_node *
221 ovsdb_monitor_json_cache_search(const struct ovsdb_monitor *dbmon,
222 enum ovsdb_monitor_version version,
223 struct ovsdb_monitor_change_set *change_set)
224 {
225 struct ovsdb_monitor_json_cache_node *node;
226 uint32_t hash = json_cache_hash(version, change_set);
227
228 HMAP_FOR_EACH_WITH_HASH(node, hmap_node, hash, &dbmon->json_cache) {
229 if (uuid_equals(&node->change_set_uuid, &change_set->uuid) &&
230 node->version == version) {
231 return node;
232 }
233 }
234
235 return NULL;
236 }
237
238 static void
239 ovsdb_monitor_json_cache_insert(struct ovsdb_monitor *dbmon,
240 enum ovsdb_monitor_version version,
241 struct ovsdb_monitor_change_set *change_set,
242 struct json *json)
243 {
244 struct ovsdb_monitor_json_cache_node *node;
245 uint32_t hash = json_cache_hash(version, change_set);
246
247 node = xmalloc(sizeof *node);
248
249 node->version = version;
250 node->change_set_uuid = change_set->uuid;
251 node->json = json ? json_clone(json) : NULL;
252
253 hmap_insert(&dbmon->json_cache, &node->hmap_node, hash);
254 }
255
256 static void
257 ovsdb_monitor_json_cache_flush(struct ovsdb_monitor *dbmon)
258 {
259 struct ovsdb_monitor_json_cache_node *node;
260
261 HMAP_FOR_EACH_POP(node, hmap_node, &dbmon->json_cache) {
262 json_destroy(node->json);
263 free(node);
264 }
265 }
266
267 /* Free all versions of json cache for a given change_set.*/
268 static void
269 ovsdb_monitor_json_cache_destroy(struct ovsdb_monitor *dbmon,
270 struct ovsdb_monitor_change_set *change_set)
271 {
272 enum ovsdb_monitor_version v;
273 for (v = OVSDB_MONITOR_V1; v < OVSDB_MONITOR_VERSION_MAX; v++) {
274 struct ovsdb_monitor_json_cache_node *node
275 = ovsdb_monitor_json_cache_search(dbmon, v, change_set);
276 if (node) {
277 hmap_remove(&dbmon->json_cache, &node->hmap_node);
278 json_destroy(node->json);
279 free(node);
280 }
281 }
282 }
283
284 static int
285 compare_ovsdb_monitor_column(const void *a_, const void *b_)
286 {
287 const struct ovsdb_monitor_column *a = a_;
288 const struct ovsdb_monitor_column *b = b_;
289
290 /* put all monitored columns at the begining */
291 if (a->monitored != b->monitored) {
292 return a->monitored ? -1 : 1;
293 }
294
295 return a->column < b->column ? -1 : a->column > b->column;
296 }
297
298 /* Finds and returns the ovsdb_monitor_row in 'mt->changes->rows' for the
299 * given 'uuid', or NULL if there is no such row. */
300 static struct ovsdb_monitor_row *
301 ovsdb_monitor_changes_row_find(
302 const struct ovsdb_monitor_change_set_for_table *changes,
303 const struct uuid *uuid)
304 {
305 struct ovsdb_monitor_row *row;
306
307 HMAP_FOR_EACH_WITH_HASH (row, hmap_node, uuid_hash(uuid),
308 &changes->rows) {
309 if (uuid_equals(uuid, &row->uuid)) {
310 return row;
311 }
312 }
313 return NULL;
314 }
315
316 /* Allocates an array of 'n_columns' ovsdb_datums and initializes them as
317 * copies of the data in 'row' drawn from the columns represented by
318 * mt->columns[]. Returns the array.
319 *
320 * If 'row' is NULL, returns NULL. */
321 static struct ovsdb_datum *
322 clone_monitor_row_data(const struct ovsdb_monitor_table *mt,
323 const struct ovsdb_row *row,
324 size_t n_columns)
325 {
326 struct ovsdb_datum *data;
327 size_t i;
328
329 if (!row) {
330 return NULL;
331 }
332
333 data = xmalloc(n_columns * sizeof *data);
334 for (i = 0; i < n_columns; i++) {
335 const struct ovsdb_column *c = mt->columns[i].column;
336 const struct ovsdb_datum *src = &row->fields[c->index];
337 struct ovsdb_datum *dst = &data[i];
338 const struct ovsdb_type *type = &c->type;
339
340 ovsdb_datum_clone(dst, src, type);
341 }
342 return data;
343 }
344
345 /* Replaces the n_columns ovsdb_datums in row[] by copies of the data from
346 * in 'row' drawn from the columns represented by mt->columns[]. */
347 static void
348 update_monitor_row_data(const struct ovsdb_monitor_table *mt,
349 const struct ovsdb_row *row,
350 struct ovsdb_datum *data,
351 size_t n_columns)
352 {
353 size_t i;
354
355 for (i = 0; i < n_columns; i++) {
356 const struct ovsdb_column *c = mt->columns[i].column;
357 const struct ovsdb_datum *src = &row->fields[c->index];
358 struct ovsdb_datum *dst = &data[i];
359 const struct ovsdb_type *type = &c->type;
360
361 if (!ovsdb_datum_equals(src, dst, type)) {
362 ovsdb_datum_destroy(dst, type);
363 ovsdb_datum_clone(dst, src, type);
364 }
365 }
366 }
367
368 /* Frees all of the n_columns ovsdb_datums in data[], using the types taken
369 * from mt->columns[], plus 'data' itself. */
370 static void
371 free_monitor_row_data(const struct ovsdb_monitor_table *mt,
372 struct ovsdb_datum *data,
373 size_t n_columns)
374 {
375 if (data) {
376 size_t i;
377
378 for (i = 0; i < n_columns; i++) {
379 const struct ovsdb_column *c = mt->columns[i].column;
380
381 ovsdb_datum_destroy(&data[i], &c->type);
382 }
383 free(data);
384 }
385 }
386
387 /* Frees 'row', which must have been created from 'mt'. */
388 static void
389 ovsdb_monitor_row_destroy(const struct ovsdb_monitor_table *mt,
390 struct ovsdb_monitor_row *row,
391 size_t n_columns)
392 {
393 if (row) {
394 free_monitor_row_data(mt, row->old, n_columns);
395 free_monitor_row_data(mt, row->new, n_columns);
396 free(row);
397 }
398 }
399
400 static void
401 ovsdb_monitor_columns_sort(struct ovsdb_monitor *dbmon)
402 {
403 int i;
404 struct shash_node *node;
405
406 SHASH_FOR_EACH (node, &dbmon->tables) {
407 struct ovsdb_monitor_table *mt = node->data;
408
409 qsort(mt->columns, mt->n_columns, sizeof *mt->columns,
410 compare_ovsdb_monitor_column);
411 for (i = 0; i < mt->n_columns; i++) {
412 /* re-set index map due to sort */
413 mt->columns_index_map[mt->columns[i].column->index] = i;
414 }
415 }
416 }
417
418 void
419 ovsdb_monitor_add_jsonrpc_monitor(struct ovsdb_monitor *dbmon,
420 struct ovsdb_jsonrpc_monitor *jsonrpc_monitor)
421 {
422 struct jsonrpc_monitor_node *jm;
423
424 jm = xzalloc(sizeof *jm);
425 jm->jsonrpc_monitor = jsonrpc_monitor;
426 ovs_list_push_back(&dbmon->jsonrpc_monitors, &jm->node);
427 }
428
429 struct ovsdb_monitor *
430 ovsdb_monitor_create(struct ovsdb *db,
431 struct ovsdb_jsonrpc_monitor *jsonrpc_monitor)
432 {
433 struct ovsdb_monitor *dbmon;
434
435 dbmon = xzalloc(sizeof *dbmon);
436
437 ovs_list_push_back(&db->monitors, &dbmon->list_node);
438 ovs_list_init(&dbmon->jsonrpc_monitors);
439 dbmon->db = db;
440 ovs_list_init(&dbmon->change_sets);
441 shash_init(&dbmon->tables);
442 hmap_node_nullify(&dbmon->hmap_node);
443 hmap_init(&dbmon->json_cache);
444
445 ovsdb_monitor_add_jsonrpc_monitor(dbmon, jsonrpc_monitor);
446 return dbmon;
447 }
448
449 void
450 ovsdb_monitor_add_table(struct ovsdb_monitor *m,
451 const struct ovsdb_table *table)
452 {
453 struct ovsdb_monitor_table *mt;
454 int i;
455 size_t n_columns = shash_count(&table->schema->columns);
456
457 mt = xzalloc(sizeof *mt);
458 mt->table = table;
459 shash_add(&m->tables, table->schema->name, mt);
460 ovs_list_init(&mt->change_sets);
461 mt->columns_index_map =
462 xmalloc(sizeof *mt->columns_index_map * n_columns);
463 for (i = 0; i < n_columns; i++) {
464 mt->columns_index_map[i] = -1;
465 }
466 }
467
468 const char *
469 ovsdb_monitor_add_column(struct ovsdb_monitor *dbmon,
470 const struct ovsdb_table *table,
471 const struct ovsdb_column *column,
472 enum ovsdb_monitor_selection select,
473 bool monitored)
474 {
475 struct ovsdb_monitor_table *mt;
476 struct ovsdb_monitor_column *c;
477
478 mt = shash_find_data(&dbmon->tables, table->schema->name);
479
480 /* Check for column duplication. Return duplicated column name. */
481 if (mt->columns_index_map[column->index] != -1) {
482 return column->name;
483 }
484
485 if (mt->n_columns >= mt->allocated_columns) {
486 mt->columns = x2nrealloc(mt->columns, &mt->allocated_columns,
487 sizeof *mt->columns);
488 }
489
490 mt->select |= select;
491 mt->columns_index_map[column->index] = mt->n_columns;
492 c = &mt->columns[mt->n_columns++];
493 c->column = column;
494 c->select = select;
495 c->monitored = monitored;
496 if (monitored) {
497 mt->n_monitored_columns++;
498 }
499
500 return NULL;
501 }
502
503 static void
504 ovsdb_monitor_condition_add_columns(struct ovsdb_monitor *dbmon,
505 const struct ovsdb_table *table,
506 struct ovsdb_condition *condition)
507 {
508 size_t n_columns;
509 int i;
510 const struct ovsdb_column **columns =
511 ovsdb_condition_get_columns(condition, &n_columns);
512
513 for (i = 0; i < n_columns; i++) {
514 ovsdb_monitor_add_column(dbmon, table, columns[i],
515 OJMS_NONE, false);
516 }
517
518 free(columns);
519 }
520
521 /* Bind this session's condition to ovsdb_monitor */
522 void
523 ovsdb_monitor_condition_bind(struct ovsdb_monitor *dbmon,
524 struct ovsdb_monitor_session_condition *cond)
525 {
526 struct shash_node *node;
527
528 SHASH_FOR_EACH(node, &cond->tables) {
529 struct ovsdb_monitor_table_condition *mtc = node->data;
530 struct ovsdb_monitor_table *mt =
531 shash_find_data(&dbmon->tables, mtc->table->schema->name);
532
533 mtc->mt = mt;
534 ovsdb_monitor_condition_add_columns(dbmon, mtc->table,
535 &mtc->new_condition);
536 }
537 }
538
539 bool
540 ovsdb_monitor_table_exists(struct ovsdb_monitor *m,
541 const struct ovsdb_table *table)
542 {
543 return shash_find_data(&m->tables, table->schema->name);
544 }
545
546 static struct ovsdb_monitor_change_set *
547 ovsdb_monitor_add_change_set(struct ovsdb_monitor *dbmon,
548 bool init_only, const struct uuid *prev_txn)
549 {
550 struct ovsdb_monitor_change_set *change_set = xzalloc(sizeof *change_set);
551 change_set->uuid = uuid_random();
552 ovs_list_push_back(&(dbmon->change_sets), &change_set->list_node);
553 ovs_list_init(&change_set->change_set_for_tables);
554 change_set->n_refs = 1;
555 change_set->prev_txn = prev_txn ? *prev_txn : UUID_ZERO;
556
557 struct shash_node *node;
558 SHASH_FOR_EACH (node, &dbmon->tables) {
559 struct ovsdb_monitor_table *mt = node->data;
560 if (!init_only || (mt->select & OJMS_INITIAL)) {
561 struct ovsdb_monitor_change_set_for_table *mcst =
562 xzalloc(sizeof *mcst);
563 mcst->mt = mt;
564 mcst->n_columns = mt->n_columns;
565 mcst->mcs = change_set;
566 hmap_init(&mcst->rows);
567 ovs_list_push_back(&mt->change_sets, &mcst->list_in_mt);
568 ovs_list_push_back(&change_set->change_set_for_tables,
569 &mcst->list_in_change_set);
570 }
571 }
572
573 return change_set;
574 };
575
576 static struct ovsdb_monitor_change_set *
577 ovsdb_monitor_find_change_set(const struct ovsdb_monitor *dbmon,
578 const struct uuid *prev_txn)
579 {
580 struct ovsdb_monitor_change_set *cs;
581 LIST_FOR_EACH (cs, list_node, &dbmon->change_sets) {
582 if (uuid_equals(&cs->prev_txn, prev_txn)) {
583 /* Check n_columns for each table in dbmon, in case it is changed
584 * after the change set is populated. */
585 bool n_col_is_equal = true;
586 struct ovsdb_monitor_change_set_for_table *mcst;
587 LIST_FOR_EACH (mcst, list_in_change_set,
588 &cs->change_set_for_tables) {
589 struct ovsdb_monitor_table *mt = mcst->mt;
590 if (mt->n_columns != mcst->n_columns) {
591 n_col_is_equal = false;
592 break;
593 }
594 }
595 if (n_col_is_equal) {
596 return cs;
597 }
598 }
599 }
600 return NULL;
601 }
602
603 static void
604 ovsdb_monitor_untrack_change_set(struct ovsdb_monitor *dbmon,
605 struct ovsdb_monitor_change_set *mcs)
606 {
607 ovs_assert(mcs);
608 if (--mcs->n_refs == 0) {
609 if (mcs == dbmon->init_change_set) {
610 dbmon->init_change_set = NULL;
611 } else if (mcs == dbmon->new_change_set) {
612 dbmon->new_change_set = NULL;
613 }
614 ovsdb_monitor_json_cache_destroy(dbmon, mcs);
615 ovsdb_monitor_change_set_destroy(mcs);
616 }
617 }
618
619 static void
620 ovsdb_monitor_track_new_change_set(struct ovsdb_monitor *dbmon)
621 {
622 struct ovsdb_monitor_change_set *change_set = dbmon->new_change_set;
623
624 if (change_set) {
625 change_set->n_refs++;
626 } else {
627 change_set = ovsdb_monitor_add_change_set(dbmon, false,
628 ovsdb_monitor_get_last_txnid(dbmon));
629 dbmon->new_change_set = change_set;
630 }
631 }
632
633 static void
634 ovsdb_monitor_change_set_destroy(struct ovsdb_monitor_change_set *mcs)
635 {
636 ovs_list_remove(&mcs->list_node);
637
638 struct ovsdb_monitor_change_set_for_table *mcst, *next_mcst;
639 LIST_FOR_EACH_SAFE (mcst, next_mcst, list_in_change_set,
640 &mcs->change_set_for_tables) {
641 ovs_list_remove(&mcst->list_in_change_set);
642 ovs_list_remove(&mcst->list_in_mt);
643
644 struct ovsdb_monitor_row *row, *next;
645 HMAP_FOR_EACH_SAFE (row, next, hmap_node, &mcst->rows) {
646 hmap_remove(&mcst->rows, &row->hmap_node);
647 ovsdb_monitor_row_destroy(mcst->mt, row, mcst->n_columns);
648 }
649 hmap_destroy(&mcst->rows);
650
651 free(mcst);
652 }
653 free(mcs);
654 }
655
656 static enum ovsdb_monitor_selection
657 ovsdb_monitor_row_update_type(bool initial, const bool old, const bool new)
658 {
659 return initial ? OJMS_INITIAL
660 : !old ? OJMS_INSERT
661 : !new ? OJMS_DELETE
662 : OJMS_MODIFY;
663 }
664
665 /* Set conditional monitoring mode only if we have non-empty condition in one
666 * of the tables at least */
667 static inline void
668 ovsdb_monitor_session_condition_set_mode(
669 struct ovsdb_monitor_session_condition *cond)
670 {
671 struct shash_node *node;
672
673 SHASH_FOR_EACH (node, &cond->tables) {
674 struct ovsdb_monitor_table_condition *mtc = node->data;
675
676 if (!ovsdb_condition_is_true(&mtc->new_condition)) {
677 cond->conditional = true;
678 return;
679 }
680 }
681 cond->conditional = false;
682 }
683
684 /* Returnes an empty allocated session's condition state holder */
685 struct ovsdb_monitor_session_condition *
686 ovsdb_monitor_session_condition_create(void)
687 {
688 struct ovsdb_monitor_session_condition *condition =
689 xzalloc(sizeof *condition);
690
691 condition->conditional = false;
692 shash_init(&condition->tables);
693 return condition;
694 }
695
696 void
697 ovsdb_monitor_session_condition_destroy(
698 struct ovsdb_monitor_session_condition *condition)
699 {
700 struct shash_node *node, *next;
701
702 if (!condition) {
703 return;
704 }
705
706 SHASH_FOR_EACH_SAFE (node, next, &condition->tables) {
707 struct ovsdb_monitor_table_condition *mtc = node->data;
708
709 ovsdb_condition_destroy(&mtc->new_condition);
710 ovsdb_condition_destroy(&mtc->old_condition);
711 shash_delete(&condition->tables, node);
712 free(mtc);
713 }
714 shash_destroy(&condition->tables);
715 free(condition);
716 }
717
718 struct ovsdb_error *
719 ovsdb_monitor_table_condition_create(
720 struct ovsdb_monitor_session_condition *condition,
721 const struct ovsdb_table *table,
722 const struct json *json_cnd)
723 {
724 struct ovsdb_monitor_table_condition *mtc;
725 struct ovsdb_error *error;
726
727 mtc = xzalloc(sizeof *mtc);
728 mtc->table = table;
729 ovsdb_condition_init(&mtc->old_condition);
730 ovsdb_condition_init(&mtc->new_condition);
731
732 if (json_cnd) {
733 error = ovsdb_condition_from_json(table->schema,
734 json_cnd,
735 NULL,
736 &mtc->old_condition);
737 if (error) {
738 free(mtc);
739 return error;
740 }
741 }
742
743 shash_add(&condition->tables, table->schema->name, mtc);
744 /* On session startup old == new condition */
745 ovsdb_condition_clone(&mtc->new_condition, &mtc->old_condition);
746 ovsdb_monitor_session_condition_set_mode(condition);
747
748 return NULL;
749 }
750
751 static bool
752 ovsdb_monitor_get_table_conditions(
753 const struct ovsdb_monitor_table *mt,
754 const struct ovsdb_monitor_session_condition *condition,
755 struct ovsdb_condition **old_condition,
756 struct ovsdb_condition **new_condition)
757 {
758 if (!condition) {
759 return false;
760 }
761
762 struct ovsdb_monitor_table_condition *mtc =
763 shash_find_data(&condition->tables, mt->table->schema->name);
764
765 if (!mtc) {
766 return false;
767 }
768 *old_condition = &mtc->old_condition;
769 *new_condition = &mtc->new_condition;
770
771 return true;
772 }
773
774 struct ovsdb_error *
775 ovsdb_monitor_table_condition_update(
776 struct ovsdb_monitor *dbmon,
777 struct ovsdb_monitor_session_condition *condition,
778 const struct ovsdb_table *table,
779 const struct json *cond_json)
780 {
781 if (!condition) {
782 return NULL;
783 }
784
785 struct ovsdb_monitor_table_condition *mtc =
786 shash_find_data(&condition->tables, table->schema->name);
787 struct ovsdb_error *error;
788 struct ovsdb_condition cond = OVSDB_CONDITION_INITIALIZER(&cond);
789
790 error = ovsdb_condition_from_json(table->schema, cond_json,
791 NULL, &cond);
792 if (error) {
793 return error;
794 }
795 ovsdb_condition_destroy(&mtc->new_condition);
796 ovsdb_condition_clone(&mtc->new_condition, &cond);
797 ovsdb_condition_destroy(&cond);
798 ovsdb_monitor_condition_add_columns(dbmon,
799 table,
800 &mtc->new_condition);
801
802 return NULL;
803 }
804
805 static void
806 ovsdb_monitor_table_condition_updated(struct ovsdb_monitor_table *mt,
807 struct ovsdb_monitor_session_condition *condition)
808 {
809 struct ovsdb_monitor_table_condition *mtc =
810 shash_find_data(&condition->tables, mt->table->schema->name);
811
812 if (mtc) {
813 /* If conditional monitoring - set old condition to new condition */
814 if (ovsdb_condition_cmp_3way(&mtc->old_condition,
815 &mtc->new_condition)) {
816 ovsdb_condition_destroy(&mtc->old_condition);
817 ovsdb_condition_clone(&mtc->old_condition, &mtc->new_condition);
818 ovsdb_monitor_session_condition_set_mode(condition);
819 }
820 }
821 }
822
823 static enum ovsdb_monitor_selection
824 ovsdb_monitor_row_update_type_condition(
825 const struct ovsdb_monitor_table *mt,
826 const struct ovsdb_monitor_session_condition *condition,
827 bool initial,
828 enum ovsdb_monitor_row_type row_type,
829 const struct ovsdb_datum *old,
830 const struct ovsdb_datum *new)
831 {
832 struct ovsdb_condition *old_condition, *new_condition;
833 enum ovsdb_monitor_selection type =
834 ovsdb_monitor_row_update_type(initial, old, new);
835
836 if (ovsdb_monitor_get_table_conditions(mt,
837 condition,
838 &old_condition,
839 &new_condition)) {
840 bool old_cond = !old ? false
841 : ovsdb_condition_empty_or_match_any(old,
842 old_condition,
843 row_type == OVSDB_MONITOR_ROW ?
844 mt->columns_index_map :
845 NULL);
846 bool new_cond = !new ? false
847 : ovsdb_condition_empty_or_match_any(new,
848 new_condition,
849 row_type == OVSDB_MONITOR_ROW ?
850 mt->columns_index_map :
851 NULL);
852
853 if (!old_cond && !new_cond) {
854 type = OJMS_NONE;
855 }
856
857 switch (type) {
858 case OJMS_INITIAL:
859 case OJMS_INSERT:
860 if (!new_cond) {
861 type = OJMS_NONE;
862 }
863 break;
864 case OJMS_MODIFY:
865 type = !old_cond ? OJMS_INSERT : !new_cond
866 ? OJMS_DELETE : OJMS_MODIFY;
867 break;
868 case OJMS_DELETE:
869 if (!old_cond) {
870 type = OJMS_NONE;
871 }
872 break;
873 case OJMS_NONE:
874 break;
875 }
876 }
877 return type;
878 }
879
880 static bool
881 ovsdb_monitor_row_skip_update(const struct ovsdb_monitor_table *mt,
882 enum ovsdb_monitor_row_type row_type,
883 const struct ovsdb_datum *old,
884 const struct ovsdb_datum *new,
885 enum ovsdb_monitor_selection type,
886 unsigned long int *changed,
887 size_t n_columns)
888 {
889 if (!(mt->select & type)) {
890 return true;
891 }
892
893 if (type == OJMS_MODIFY) {
894 size_t i, n_changes;
895
896 n_changes = 0;
897 memset(changed, 0, bitmap_n_bytes(n_columns));
898 for (i = 0; i < n_columns; i++) {
899 const struct ovsdb_column *c = mt->columns[i].column;
900 size_t index = row_type == OVSDB_ROW ? c->index : i;
901 if (!ovsdb_datum_equals(&old[index], &new[index], &c->type)) {
902 bitmap_set1(changed, i);
903 n_changes++;
904 }
905 }
906 if (!n_changes) {
907 /* No actual changes: presumably a row changed and then
908 * changed back later. */
909 return true;
910 }
911 }
912
913 return false;
914 }
915
916 /* Returns JSON for a <row-update> (as described in RFC 7047) for 'row' within
917 * 'mt', or NULL if no row update should be sent.
918 *
919 * The caller should specify 'initial' as true if the returned JSON is going to
920 * be used as part of the initial reply to a "monitor" request, false if it is
921 * going to be used as part of an "update" notification.
922 *
923 * 'changed' must be a scratch buffer for internal use that is at least
924 * bitmap_n_bytes(n_columns) bytes long. */
925 static struct json *
926 ovsdb_monitor_compose_row_update(
927 const struct ovsdb_monitor_table *mt,
928 const struct ovsdb_monitor_session_condition *condition OVS_UNUSED,
929 enum ovsdb_monitor_row_type row_type OVS_UNUSED,
930 const void *_row,
931 bool initial, unsigned long int *changed,
932 size_t n_columns OVS_UNUSED)
933 {
934 const struct ovsdb_monitor_row *row = _row;
935 enum ovsdb_monitor_selection type;
936 struct json *old_json, *new_json;
937 struct json *row_json;
938 size_t i;
939
940 ovs_assert(row_type == OVSDB_MONITOR_ROW);
941 type = ovsdb_monitor_row_update_type(initial, row->old, row->new);
942 if (ovsdb_monitor_row_skip_update(mt, row_type, row->old,
943 row->new, type, changed,
944 mt->n_columns)) {
945 return NULL;
946 }
947
948 row_json = json_object_create();
949 old_json = new_json = NULL;
950 if (type & (OJMS_DELETE | OJMS_MODIFY)) {
951 old_json = json_object_create();
952 json_object_put(row_json, "old", old_json);
953 }
954 if (type & (OJMS_INITIAL | OJMS_INSERT | OJMS_MODIFY)) {
955 new_json = json_object_create();
956 json_object_put(row_json, "new", new_json);
957 }
958 for (i = 0; i < mt->n_monitored_columns; i++) {
959 const struct ovsdb_monitor_column *c = &mt->columns[i];
960
961 if (!c->monitored || !(type & c->select)) {
962 /* We don't care about this type of change for this
963 * particular column (but we will care about it for some
964 * other column). */
965 continue;
966 }
967
968 if ((type == OJMS_MODIFY && bitmap_is_set(changed, i))
969 || type == OJMS_DELETE) {
970 json_object_put(old_json, c->column->name,
971 ovsdb_datum_to_json(&row->old[i],
972 &c->column->type));
973 }
974 if (type & (OJMS_INITIAL | OJMS_INSERT | OJMS_MODIFY)) {
975 json_object_put(new_json, c->column->name,
976 ovsdb_datum_to_json(&row->new[i],
977 &c->column->type));
978 }
979 }
980
981 return row_json;
982 }
983
984 /* Returns JSON for a <row-update2> (as described in ovsdb-server(1) mapage)
985 * for 'row' within * 'mt', or NULL if no row update should be sent.
986 *
987 * The caller should specify 'initial' as true if the returned JSON is
988 * going to be used as part of the initial reply to a "monitor_cond" request,
989 * false if it is going to be used as part of an "update2" notification.
990 *
991 * 'changed' must be a scratch buffer for internal use that is at least
992 * bitmap_n_bytes(n_columns) bytes long. */
993 static struct json *
994 ovsdb_monitor_compose_row_update2(
995 const struct ovsdb_monitor_table *mt,
996 const struct ovsdb_monitor_session_condition *condition,
997 enum ovsdb_monitor_row_type row_type,
998 const void *_row,
999 bool initial, unsigned long int *changed,
1000 size_t n_columns)
1001 {
1002 enum ovsdb_monitor_selection type;
1003 struct json *row_update2, *diff_json;
1004 const struct ovsdb_datum *old, *new;
1005 size_t i;
1006
1007 if (row_type == OVSDB_MONITOR_ROW) {
1008 old = ((const struct ovsdb_monitor_row *)_row)->old;;
1009 new = ((const struct ovsdb_monitor_row *)_row)->new;
1010 } else {
1011 old = new = ((const struct ovsdb_row *)_row)->fields;
1012 }
1013
1014 type = ovsdb_monitor_row_update_type_condition(mt, condition, initial,
1015 row_type, old, new);
1016 if (ovsdb_monitor_row_skip_update(mt, row_type, old, new, type, changed,
1017 n_columns)) {
1018 return NULL;
1019 }
1020
1021 row_update2 = json_object_create();
1022 if (type == OJMS_DELETE) {
1023 json_object_put(row_update2, "delete", json_null_create());
1024 } else {
1025 diff_json = json_object_create();
1026 const char *op;
1027
1028 for (i = 0; i < mt->n_monitored_columns; i++) {
1029 const struct ovsdb_monitor_column *c = &mt->columns[i];
1030 size_t index = row_type == OVSDB_ROW ? c->column->index : i;
1031 if (!c->monitored || !(type & c->select)) {
1032 /* We don't care about this type of change for this
1033 * particular column (but we will care about it for some
1034 * other column). */
1035 continue;
1036 }
1037
1038 if (type == OJMS_MODIFY) {
1039 struct ovsdb_datum diff;
1040
1041 if (!bitmap_is_set(changed, i)) {
1042 continue;
1043 }
1044
1045 ovsdb_datum_diff(&diff ,&old[index], &new[index],
1046 &c->column->type);
1047 json_object_put(diff_json, c->column->name,
1048 ovsdb_datum_to_json(&diff, &c->column->type));
1049 ovsdb_datum_destroy(&diff, &c->column->type);
1050 } else {
1051 if (!ovsdb_datum_is_default(&new[index], &c->column->type)) {
1052 json_object_put(diff_json, c->column->name,
1053 ovsdb_datum_to_json(&new[index],
1054 &c->column->type));
1055 }
1056 }
1057 }
1058
1059 op = type == OJMS_INITIAL ? "initial"
1060 : type == OJMS_MODIFY ? "modify" : "insert";
1061 json_object_put(row_update2, op, diff_json);
1062 }
1063
1064 return row_update2;
1065 }
1066
1067 static size_t
1068 ovsdb_monitor_max_columns(struct ovsdb_monitor *dbmon)
1069 {
1070 struct shash_node *node;
1071 size_t max_columns = 0;
1072
1073 SHASH_FOR_EACH (node, &dbmon->tables) {
1074 struct ovsdb_monitor_table *mt = node->data;
1075
1076 max_columns = MAX(max_columns, mt->n_columns);
1077 }
1078
1079 return max_columns;
1080 }
1081
1082 static void
1083 ovsdb_monitor_add_json_row(struct json **json, const char *table_name,
1084 struct json **table_json, struct json *row_json,
1085 const struct uuid *row_uuid)
1086 {
1087 char uuid[UUID_LEN + 1];
1088
1089 /* Create JSON object for transaction overall. */
1090 if (!*json) {
1091 *json = json_object_create();
1092 }
1093
1094 /* Create JSON object for transaction on this table. */
1095 if (!*table_json) {
1096 *table_json = json_object_create();
1097 json_object_put(*json, table_name, *table_json);
1098 }
1099
1100 /* Add JSON row to JSON table. */
1101 snprintf(uuid, sizeof uuid, UUID_FMT, UUID_ARGS(row_uuid));
1102 json_object_put(*table_json, uuid, row_json);
1103 }
1104
1105 /* Constructs and returns JSON for a <table-updates> object (as described in
1106 * RFC 7047) for all the outstanding changes within 'monitor', starting from
1107 * 'transaction'. */
1108 static struct json*
1109 ovsdb_monitor_compose_update(
1110 struct ovsdb_monitor *dbmon,
1111 bool initial, struct ovsdb_monitor_change_set *mcs,
1112 const struct ovsdb_monitor_session_condition *condition,
1113 compose_row_update_cb_func row_update)
1114 {
1115 struct json *json;
1116 size_t max_columns = ovsdb_monitor_max_columns(dbmon);
1117 unsigned long int *changed = xmalloc(bitmap_n_bytes(max_columns));
1118
1119 json = NULL;
1120 struct ovsdb_monitor_change_set_for_table *mcst;
1121 LIST_FOR_EACH (mcst, list_in_change_set, &mcs->change_set_for_tables) {
1122 struct ovsdb_monitor_row *row, *next;
1123 struct json *table_json = NULL;
1124 struct ovsdb_monitor_table *mt = mcst->mt;
1125
1126 HMAP_FOR_EACH_SAFE (row, next, hmap_node, &mcst->rows) {
1127 struct json *row_json;
1128 row_json = (*row_update)(mt, condition, OVSDB_MONITOR_ROW, row,
1129 initial, changed, mcst->n_columns);
1130 if (row_json) {
1131 ovsdb_monitor_add_json_row(&json, mt->table->schema->name,
1132 &table_json, row_json,
1133 &row->uuid);
1134 }
1135 }
1136 }
1137 free(changed);
1138
1139 return json;
1140 }
1141
1142 static struct json*
1143 ovsdb_monitor_compose_cond_change_update(
1144 struct ovsdb_monitor *dbmon,
1145 struct ovsdb_monitor_session_condition *condition)
1146 {
1147 struct shash_node *node;
1148 struct json *json = NULL;
1149 size_t max_columns = ovsdb_monitor_max_columns(dbmon);
1150 unsigned long int *changed = xmalloc(bitmap_n_bytes(max_columns));
1151
1152 SHASH_FOR_EACH (node, &dbmon->tables) {
1153 struct ovsdb_monitor_table *mt = node->data;
1154 struct ovsdb_row *row;
1155 struct json *table_json = NULL;
1156 struct ovsdb_condition *old_condition, *new_condition;
1157
1158 if (!ovsdb_monitor_get_table_conditions(mt,
1159 condition,
1160 &old_condition,
1161 &new_condition) ||
1162 !ovsdb_condition_cmp_3way(old_condition, new_condition)) {
1163 /* Nothing to update on this table */
1164 continue;
1165 }
1166
1167 /* Iterate over all rows in table */
1168 HMAP_FOR_EACH (row, hmap_node, &mt->table->rows) {
1169 struct json *row_json;
1170
1171 row_json = ovsdb_monitor_compose_row_update2(mt, condition,
1172 OVSDB_ROW, row,
1173 false, changed,
1174 mt->n_columns);
1175 if (row_json) {
1176 ovsdb_monitor_add_json_row(&json, mt->table->schema->name,
1177 &table_json, row_json,
1178 ovsdb_row_get_uuid(row));
1179 }
1180 }
1181 ovsdb_monitor_table_condition_updated(mt, condition);
1182 }
1183 free(changed);
1184
1185 return json;
1186 }
1187
1188 /* Returns JSON for a <table-updates> object (as described in RFC 7047)
1189 * for all the outstanding changes in dbmon that are tracked by the change set
1190 * *p_mcs.
1191 *
1192 * If cond_updated is true all rows in the db that match conditions will be
1193 * sent.
1194 *
1195 * The caller should specify 'initial' as true if the returned JSON is going to
1196 * be used as part of the initial reply to a "monitor" request, false if it is
1197 * going to be used as part of an "update" notification. */
1198 struct json *
1199 ovsdb_monitor_get_update(
1200 struct ovsdb_monitor *dbmon,
1201 bool initial, bool cond_updated,
1202 struct ovsdb_monitor_session_condition *condition,
1203 enum ovsdb_monitor_version version,
1204 struct ovsdb_monitor_change_set **p_mcs)
1205 {
1206 struct ovsdb_monitor_json_cache_node *cache_node = NULL;
1207 struct json *json;
1208 struct ovsdb_monitor_change_set *mcs = *p_mcs;
1209
1210 ovs_assert(cond_updated ? mcs == dbmon->new_change_set : true);
1211
1212 /* Return a clone of cached json if one exists. Otherwise,
1213 * generate a new one and add it to the cache. */
1214 if (!condition || (!condition->conditional && !cond_updated)) {
1215 cache_node = ovsdb_monitor_json_cache_search(dbmon, version,
1216 mcs);
1217 }
1218 if (cache_node) {
1219 json = cache_node->json ? json_clone(cache_node->json) : NULL;
1220 } else {
1221 if (version == OVSDB_MONITOR_V1) {
1222 json =
1223 ovsdb_monitor_compose_update(dbmon, initial, mcs,
1224 condition,
1225 ovsdb_monitor_compose_row_update);
1226 } else {
1227 ovs_assert(version == OVSDB_MONITOR_V2 ||
1228 version == OVSDB_MONITOR_V3);
1229
1230 if (!cond_updated) {
1231 json = ovsdb_monitor_compose_update(dbmon, initial, mcs,
1232 condition,
1233 ovsdb_monitor_compose_row_update2);
1234 if (!condition || !condition->conditional) {
1235 ovsdb_monitor_json_cache_insert(dbmon, version, mcs,
1236 json);
1237 }
1238 } else {
1239 /* Compose update on whole db due to condition update.
1240 Session must be flushed (change list is empty)*/
1241 json =
1242 ovsdb_monitor_compose_cond_change_update(dbmon, condition);
1243 }
1244 }
1245 }
1246
1247 /* Maintain tracking change set. */
1248 ovsdb_monitor_untrack_change_set(dbmon, mcs);
1249 ovsdb_monitor_track_new_change_set(dbmon);
1250 *p_mcs = dbmon->new_change_set;
1251
1252 return json;
1253 }
1254
1255 bool
1256 ovsdb_monitor_needs_flush(struct ovsdb_monitor *dbmon,
1257 struct ovsdb_monitor_change_set *change_set)
1258 {
1259 ovs_assert(change_set);
1260 return (change_set != dbmon->new_change_set);
1261 }
1262
1263 void
1264 ovsdb_monitor_table_add_select(struct ovsdb_monitor *dbmon,
1265 const struct ovsdb_table *table,
1266 enum ovsdb_monitor_selection select)
1267 {
1268 struct ovsdb_monitor_table * mt;
1269
1270 mt = shash_find_data(&dbmon->tables, table->schema->name);
1271 mt->select |= select;
1272 }
1273
1274 /*
1275 * If a row's change type (insert, delete or modify) matches that of
1276 * the monitor, they should be sent to the monitor's clients as updates.
1277 * Of cause, the monitor should also internally update with this change.
1278 *
1279 * When a change type does not require client side update, the monitor
1280 * may still need to keep track of certain changes in order to generate
1281 * correct future updates. For example, the monitor internal state should
1282 * be updated whenever a new row is inserted, in order to generate the
1283 * correct initial state, regardless if a insert change type is being
1284 * monitored.
1285 *
1286 * On the other hand, if a transaction only contains changes to columns
1287 * that are not monitored, this transaction can be safely ignored by the
1288 * monitor.
1289 *
1290 * Thus, the order of the declaration is important:
1291 * 'OVSDB_CHANGES_REQUIRE_EXTERNAL_UPDATE' always implies
1292 * 'OVSDB_CHANGES_REQUIRE_INTERNAL_UPDATE', but not vice versa. */
1293 enum ovsdb_monitor_changes_efficacy {
1294 OVSDB_CHANGES_NO_EFFECT, /* Monitor does not care about this
1295 change. */
1296 OVSDB_CHANGES_REQUIRE_INTERNAL_UPDATE, /* Monitor internal updates. */
1297 OVSDB_CHANGES_REQUIRE_EXTERNAL_UPDATE, /* Client needs to be updated. */
1298 };
1299
1300 struct ovsdb_monitor_aux {
1301 const struct ovsdb_monitor *monitor;
1302 struct ovsdb_monitor_table *mt;
1303 enum ovsdb_monitor_changes_efficacy efficacy;
1304 };
1305
1306 static void
1307 ovsdb_monitor_init_aux(struct ovsdb_monitor_aux *aux,
1308 const struct ovsdb_monitor *m)
1309 {
1310 aux->monitor = m;
1311 aux->mt = NULL;
1312 aux->efficacy = OVSDB_CHANGES_NO_EFFECT;
1313 }
1314
1315 static void
1316 ovsdb_monitor_changes_update(const struct ovsdb_row *old,
1317 const struct ovsdb_row *new,
1318 const struct ovsdb_monitor_table *mt,
1319 struct ovsdb_monitor_change_set_for_table *mcst)
1320 {
1321 const struct uuid *uuid = ovsdb_row_get_uuid(new ? new : old);
1322 struct ovsdb_monitor_row *change;
1323
1324 change = ovsdb_monitor_changes_row_find(mcst, uuid);
1325 if (!change) {
1326 change = xzalloc(sizeof *change);
1327 hmap_insert(&mcst->rows, &change->hmap_node, uuid_hash(uuid));
1328 change->uuid = *uuid;
1329 change->old = clone_monitor_row_data(mt, old, mcst->n_columns);
1330 change->new = clone_monitor_row_data(mt, new, mcst->n_columns);
1331 } else {
1332 if (new) {
1333 if (!change->new) {
1334 /* Reinsert the row that was just deleted.
1335 *
1336 * This path won't be hit without replication. Whenever OVSDB
1337 * server inserts a new row, It always generates a new UUID
1338 * that is different from the row just deleted.
1339 *
1340 * With replication, this path can be hit in a corner
1341 * case when two OVSDB servers are set up to replicate
1342 * each other. Not that is a useful set up, but can
1343 * happen in practice.
1344 *
1345 * An example of how this path can be hit is documented below.
1346 * The details is not as important to the correctness of the
1347 * logic, but added here to convince ourselves that this path
1348 * can be hit.
1349 *
1350 * Imagine two OVSDB servers that replicates from each
1351 * other. For each replication session, there is a
1352 * corresponding monitor at the other end of the replication
1353 * JSONRPC connection.
1354 *
1355 * The events can lead to a back to back deletion and
1356 * insertion operation of the same row for the monitor of
1357 * the first server are:
1358 *
1359 * 1. A row is inserted in the first OVSDB server.
1360 * 2. The row is then replicated to the remote OVSDB server.
1361 * 3. The row is now deleted by the local OVSDB server. This
1362 * deletion operation is replicated to the local monitor
1363 * of the OVSDB server.
1364 * 4. The monitor now receives the same row, as an insertion,
1365 * from the replication server. Because of
1366 * replication, the row carries the same UUID as the row
1367 * just deleted.
1368 */
1369 change->new = clone_monitor_row_data(mt, new, mcst->n_columns);
1370 } else {
1371 update_monitor_row_data(mt, new, change->new, mcst->n_columns);
1372 }
1373 } else {
1374 free_monitor_row_data(mt, change->new, mcst->n_columns);
1375 change->new = NULL;
1376
1377 if (!change->old) {
1378 /* This row was added then deleted. Forget about it. */
1379 hmap_remove(&mcst->rows, &change->hmap_node);
1380 free(change);
1381 }
1382 }
1383 }
1384 }
1385
1386 static bool
1387 ovsdb_monitor_columns_changed(const struct ovsdb_monitor_table *mt,
1388 const unsigned long int *changed)
1389 {
1390 size_t i;
1391
1392 for (i = 0; i < mt->n_columns; i++) {
1393 size_t column_index = mt->columns[i].column->index;
1394
1395 if (bitmap_is_set(changed, column_index)) {
1396 return true;
1397 }
1398 }
1399
1400 return false;
1401 }
1402
1403 /* Return the efficacy of a row's change to a monitor table.
1404 *
1405 * Please see the block comment above 'ovsdb_monitor_changes_efficacy'
1406 * definition form more information. */
1407 static enum ovsdb_monitor_changes_efficacy
1408 ovsdb_monitor_changes_classify(enum ovsdb_monitor_selection type,
1409 const struct ovsdb_monitor_table *mt,
1410 const unsigned long int *changed)
1411 {
1412 if (type == OJMS_MODIFY &&
1413 !ovsdb_monitor_columns_changed(mt, changed)) {
1414 return OVSDB_CHANGES_NO_EFFECT;
1415 }
1416
1417 if (type == OJMS_MODIFY) {
1418 /* Condition might turn a modify operation to insert or delete */
1419 type |= OJMS_INSERT | OJMS_DELETE;
1420 }
1421
1422 return (mt->select & type)
1423 ? OVSDB_CHANGES_REQUIRE_EXTERNAL_UPDATE
1424 : OVSDB_CHANGES_REQUIRE_INTERNAL_UPDATE;
1425 }
1426
1427 static bool
1428 ovsdb_monitor_change_cb(const struct ovsdb_row *old,
1429 const struct ovsdb_row *new,
1430 const unsigned long int *changed,
1431 void *aux_)
1432 {
1433 struct ovsdb_monitor_aux *aux = aux_;
1434 const struct ovsdb_monitor *m = aux->monitor;
1435 struct ovsdb_table *table = new ? new->table : old->table;
1436 struct ovsdb_monitor_table *mt;
1437 struct ovsdb_monitor_change_set_for_table *mcst;
1438
1439 if (!aux->mt || table != aux->mt->table) {
1440 aux->mt = shash_find_data(&m->tables, table->schema->name);
1441 if (!aux->mt) {
1442 /* We don't care about rows in this table at all. Tell the caller
1443 * to skip it. */
1444 return false;
1445 }
1446 }
1447 mt = aux->mt;
1448
1449 enum ovsdb_monitor_selection type =
1450 ovsdb_monitor_row_update_type(false, old, new);
1451 enum ovsdb_monitor_changes_efficacy efficacy =
1452 ovsdb_monitor_changes_classify(type, mt, changed);
1453
1454 if (efficacy > OVSDB_CHANGES_NO_EFFECT) {
1455 LIST_FOR_EACH (mcst, list_in_mt, &mt->change_sets) {
1456 ovsdb_monitor_changes_update(old, new, mt, mcst);
1457 }
1458 }
1459 if (aux->efficacy < efficacy) {
1460 aux->efficacy = efficacy;
1461 }
1462
1463 return true;
1464 }
1465
1466 void
1467 ovsdb_monitor_get_initial(struct ovsdb_monitor *dbmon,
1468 struct ovsdb_monitor_change_set **p_mcs)
1469 {
1470 if (!dbmon->init_change_set) {
1471 struct ovsdb_monitor_change_set *change_set =
1472 ovsdb_monitor_add_change_set(dbmon, true, NULL);
1473 dbmon->init_change_set = change_set;
1474
1475 struct ovsdb_monitor_change_set_for_table *mcst;
1476 LIST_FOR_EACH (mcst, list_in_change_set,
1477 &change_set->change_set_for_tables) {
1478 if (mcst->mt->select & OJMS_INITIAL) {
1479 struct ovsdb_row *row;
1480 HMAP_FOR_EACH (row, hmap_node, &mcst->mt->table->rows) {
1481 ovsdb_monitor_changes_update(NULL, row, mcst->mt, mcst);
1482 }
1483 }
1484 }
1485 } else {
1486 dbmon->init_change_set->n_refs++;
1487 }
1488
1489 *p_mcs = dbmon->init_change_set;
1490 }
1491
1492 static bool
1493 ovsdb_monitor_history_change_cb(const struct ovsdb_row *old,
1494 const struct ovsdb_row *new,
1495 const unsigned long int *changed,
1496 void *aux)
1497 {
1498 struct ovsdb_monitor_change_set *change_set = aux;
1499 struct ovsdb_table *table = new ? new->table : old->table;
1500 struct ovsdb_monitor_change_set_for_table *mcst;
1501
1502 enum ovsdb_monitor_selection type =
1503 ovsdb_monitor_row_update_type(false, old, new);
1504 LIST_FOR_EACH (mcst, list_in_change_set,
1505 &change_set->change_set_for_tables) {
1506 if (mcst->mt->table == table) {
1507 enum ovsdb_monitor_changes_efficacy efficacy =
1508 ovsdb_monitor_changes_classify(type, mcst->mt, changed);
1509 if (efficacy > OVSDB_CHANGES_NO_EFFECT) {
1510 ovsdb_monitor_changes_update(old, new, mcst->mt, mcst);
1511 }
1512 return true;
1513 }
1514 }
1515 return false;
1516 }
1517
1518 void
1519 ovsdb_monitor_get_changes_after(const struct uuid *txn_uuid,
1520 struct ovsdb_monitor *dbmon,
1521 struct ovsdb_monitor_change_set **p_mcs)
1522 {
1523 ovs_assert(*p_mcs == NULL);
1524 ovs_assert(!uuid_is_zero(txn_uuid));
1525 struct ovsdb_monitor_change_set *change_set =
1526 ovsdb_monitor_find_change_set(dbmon, txn_uuid);
1527 if (change_set) {
1528 change_set->n_refs++;
1529 *p_mcs = change_set;
1530 return;
1531 }
1532
1533 struct ovsdb_txn_history_node *h_node;
1534 bool found = false;
1535 LIST_FOR_EACH (h_node, node, &dbmon->db->txn_history) {
1536 struct ovsdb_txn *txn = h_node->txn;
1537 if (!found) {
1538 /* find the txn with last_id in history */
1539 if (uuid_equals(ovsdb_txn_get_txnid(txn), txn_uuid)) {
1540 found = true;
1541 change_set = ovsdb_monitor_add_change_set(dbmon, false,
1542 txn_uuid);
1543 }
1544 } else {
1545 /* Already found. Add changes in each follow up transaction to
1546 * the new change_set. */
1547 ovsdb_txn_for_each_change(txn, ovsdb_monitor_history_change_cb,
1548 change_set);
1549 }
1550 }
1551 *p_mcs = change_set;
1552 }
1553
1554 void
1555 ovsdb_monitor_remove_jsonrpc_monitor(struct ovsdb_monitor *dbmon,
1556 struct ovsdb_jsonrpc_monitor *jsonrpc_monitor,
1557 struct ovsdb_monitor_change_set *change_set)
1558 {
1559 struct jsonrpc_monitor_node *jm;
1560
1561 if (ovs_list_is_empty(&dbmon->jsonrpc_monitors)) {
1562 ovsdb_monitor_destroy(dbmon);
1563 return;
1564 }
1565
1566 /* Find and remove the jsonrpc monitor from the list. */
1567 LIST_FOR_EACH(jm, node, &dbmon->jsonrpc_monitors) {
1568 if (jm->jsonrpc_monitor == jsonrpc_monitor) {
1569 /* Release the tracked changes. */
1570 if (change_set) {
1571 ovsdb_monitor_untrack_change_set(dbmon, change_set);
1572 }
1573 ovs_list_remove(&jm->node);
1574 free(jm);
1575
1576 /* Destroy ovsdb monitor if this is the last user. */
1577 if (ovs_list_is_empty(&dbmon->jsonrpc_monitors)) {
1578 ovsdb_monitor_destroy(dbmon);
1579 }
1580
1581 return;
1582 };
1583 }
1584
1585 /* Should never reach here. jsonrpc_monitor should be on the list. */
1586 OVS_NOT_REACHED();
1587 }
1588
1589 static bool
1590 ovsdb_monitor_table_equal(const struct ovsdb_monitor_table *a,
1591 const struct ovsdb_monitor_table *b)
1592 {
1593 size_t i;
1594
1595 ovs_assert(b->n_columns == b->n_monitored_columns);
1596
1597 if ((a->table != b->table) ||
1598 (a->select != b->select) ||
1599 (a->n_monitored_columns != b->n_monitored_columns)) {
1600 return false;
1601 }
1602
1603 /* Compare only monitored columns that must be sorted already */
1604 for (i = 0; i < a->n_monitored_columns; i++) {
1605 if ((a->columns[i].column != b->columns[i].column) ||
1606 (a->columns[i].select != b->columns[i].select)) {
1607 return false;
1608 }
1609 }
1610 return true;
1611 }
1612
1613 static bool
1614 ovsdb_monitor_equal(const struct ovsdb_monitor *a,
1615 const struct ovsdb_monitor *b)
1616 {
1617 struct shash_node *node;
1618
1619 if (shash_count(&a->tables) != shash_count(&b->tables)) {
1620 return false;
1621 }
1622
1623 SHASH_FOR_EACH(node, &a->tables) {
1624 const struct ovsdb_monitor_table *mta = node->data;
1625 const struct ovsdb_monitor_table *mtb;
1626
1627 mtb = shash_find_data(&b->tables, node->name);
1628 if (!mtb) {
1629 return false;
1630 }
1631
1632 if (!ovsdb_monitor_table_equal(mta, mtb)) {
1633 return false;
1634 }
1635 }
1636
1637 return true;
1638 }
1639
1640 static size_t
1641 ovsdb_monitor_hash(const struct ovsdb_monitor *dbmon, size_t basis)
1642 {
1643 const struct shash_node **nodes;
1644 size_t i, j, n;
1645
1646 nodes = shash_sort(&dbmon->tables);
1647 n = shash_count(&dbmon->tables);
1648
1649 for (i = 0; i < n; i++) {
1650 struct ovsdb_monitor_table *mt = nodes[i]->data;
1651
1652 basis = hash_pointer(mt->table, basis);
1653 basis = hash_3words(mt->select, mt->n_columns, basis);
1654
1655 for (j = 0; j < mt->n_columns; j++) {
1656 basis = hash_pointer(mt->columns[j].column, basis);
1657 basis = hash_2words(mt->columns[j].select, basis);
1658 }
1659 }
1660 free(nodes);
1661
1662 return basis;
1663 }
1664
1665 struct ovsdb_monitor *
1666 ovsdb_monitor_add(struct ovsdb_monitor *new_dbmon)
1667 {
1668 struct ovsdb_monitor *dbmon;
1669 size_t hash;
1670
1671 /* New_dbmon should be associated with only one jsonrpc
1672 * connections. */
1673 ovs_assert(ovs_list_is_singleton(&new_dbmon->jsonrpc_monitors));
1674
1675 ovsdb_monitor_columns_sort(new_dbmon);
1676
1677 hash = ovsdb_monitor_hash(new_dbmon, 0);
1678 HMAP_FOR_EACH_WITH_HASH(dbmon, hmap_node, hash, &ovsdb_monitors) {
1679 if (ovsdb_monitor_equal(dbmon, new_dbmon)) {
1680 return dbmon;
1681 }
1682 }
1683
1684 hmap_insert(&ovsdb_monitors, &new_dbmon->hmap_node, hash);
1685 return new_dbmon;
1686 }
1687
1688 static void
1689 ovsdb_monitor_destroy(struct ovsdb_monitor *dbmon)
1690 {
1691 struct shash_node *node;
1692
1693 ovs_list_remove(&dbmon->list_node);
1694
1695 if (!hmap_node_is_null(&dbmon->hmap_node)) {
1696 hmap_remove(&ovsdb_monitors, &dbmon->hmap_node);
1697 }
1698
1699 ovsdb_monitor_json_cache_flush(dbmon);
1700 hmap_destroy(&dbmon->json_cache);
1701
1702 struct ovsdb_monitor_change_set *cs, *cs_next;
1703 LIST_FOR_EACH_SAFE (cs, cs_next, list_node, &dbmon->change_sets) {
1704 ovsdb_monitor_change_set_destroy(cs);
1705 }
1706
1707 SHASH_FOR_EACH (node, &dbmon->tables) {
1708 struct ovsdb_monitor_table *mt = node->data;
1709 ovs_assert(ovs_list_is_empty(&mt->change_sets));
1710 free(mt->columns);
1711 free(mt->columns_index_map);
1712 free(mt);
1713 }
1714 shash_destroy(&dbmon->tables);
1715 free(dbmon);
1716 }
1717
1718 static void
1719 ovsdb_monitor_commit(struct ovsdb_monitor *m, const struct ovsdb_txn *txn)
1720 {
1721 struct ovsdb_monitor_aux aux;
1722
1723 ovsdb_monitor_init_aux(&aux, m);
1724 ovsdb_txn_for_each_change(txn, ovsdb_monitor_change_cb, &aux);
1725
1726 if (aux.efficacy > OVSDB_CHANGES_NO_EFFECT) {
1727 /* The transaction is has impact to the monitor.
1728 * Reset new_change_set, so that a new change set will be
1729 * created for future trackings. */
1730 m->new_change_set = NULL;
1731
1732 if (aux.efficacy == OVSDB_CHANGES_REQUIRE_EXTERNAL_UPDATE) {
1733 ovsdb_monitor_json_cache_flush(m);
1734 }
1735 }
1736 }
1737
1738 void
1739 ovsdb_monitors_commit(struct ovsdb *db, const struct ovsdb_txn *txn)
1740 {
1741 struct ovsdb_monitor *m;
1742
1743 LIST_FOR_EACH (m, list_node, &db->monitors) {
1744 ovsdb_monitor_commit(m, txn);
1745 }
1746 }
1747
1748 void
1749 ovsdb_monitors_remove(struct ovsdb *db)
1750 {
1751 struct ovsdb_monitor *m, *next_m;
1752
1753 LIST_FOR_EACH_SAFE (m, next_m, list_node, &db->monitors) {
1754 struct jsonrpc_monitor_node *jm, *next_jm;
1755
1756 /* Delete all front-end monitors. Removing the last front-end monitor
1757 * will also destroy the corresponding ovsdb_monitor. */
1758 LIST_FOR_EACH_SAFE (jm, next_jm, node, &m->jsonrpc_monitors) {
1759 ovsdb_jsonrpc_monitor_destroy(jm->jsonrpc_monitor, false);
1760 }
1761 }
1762 }
1763
1764 /* Add some memory usage statics for monitors into 'usage', for use with
1765 * memory_report(). */
1766 void
1767 ovsdb_monitor_get_memory_usage(struct simap *usage)
1768 {
1769 struct ovsdb_monitor *dbmon;
1770 simap_put(usage, "monitors", hmap_count(&ovsdb_monitors));
1771
1772 HMAP_FOR_EACH(dbmon, hmap_node, &ovsdb_monitors) {
1773 simap_increase(usage, "json-caches", hmap_count(&dbmon->json_cache));
1774 }
1775 }
1776
1777 void
1778 ovsdb_monitor_prereplace_db(struct ovsdb *db)
1779 {
1780 struct ovsdb_monitor *m, *next_m;
1781
1782 LIST_FOR_EACH_SAFE (m, next_m, list_node, &db->monitors) {
1783 struct jsonrpc_monitor_node *jm, *next_jm;
1784
1785 /* Delete all front-end monitors. Removing the last front-end monitor
1786 * will also destroy the corresponding ovsdb_monitor. */
1787 LIST_FOR_EACH_SAFE (jm, next_jm, node, &m->jsonrpc_monitors) {
1788 ovsdb_jsonrpc_monitor_destroy(jm->jsonrpc_monitor, true);
1789 }
1790 }
1791 }
1792
1793 const struct uuid *
1794 ovsdb_monitor_get_last_txnid(struct ovsdb_monitor *dbmon) {
1795 static struct uuid dummy = { .parts = { 0, 0, 0, 0 } };
1796 if (dbmon->db->n_txn_history) {
1797 struct ovsdb_txn_history_node *thn = CONTAINER_OF(
1798 ovs_list_back(&dbmon->db->txn_history),
1799 struct ovsdb_txn_history_node, node);
1800 return ovsdb_txn_get_txnid(thn->txn);
1801 }
1802 return &dummy;
1803 }