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