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