2 * Copyright (c) 2015 Nicira, Inc.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 #include "dynamic-string.h"
26 #include "ovsdb-error.h"
27 #include "ovsdb-parser.h"
35 #include "transaction.h"
36 #include "jsonrpc-server.h"
38 #include "openvswitch/vlog.h"
41 static const struct ovsdb_replica_class ovsdb_jsonrpc_replica_class;
42 static struct hmap ovsdb_monitors = HMAP_INITIALIZER(&ovsdb_monitors);
46 * ovsdb_monitor keep track of the ovsdb changes.
49 /* A collection of tables being monitored. */
50 struct ovsdb_monitor {
51 struct ovsdb_replica replica;
52 struct shash tables; /* Holds "struct ovsdb_monitor_table"s. */
53 struct ovs_list jsonrpc_monitors; /* Contains "jsonrpc_monitor_node"s. */
55 uint64_t n_transactions; /* Count number of committed transactions. */
56 struct hmap_node hmap_node; /* Elements within ovsdb_monitors. */
57 struct hmap json_cache; /* Contains "ovsdb_monitor_json_cache_node"s.*/
60 /* A json object of updates between 'from_txn' and 'dbmon->n_transactions'
62 struct ovsdb_monitor_json_cache_node {
63 struct hmap_node hmap_node; /* Elements in json cache. */
65 struct json *json; /* Null, or a cloned of json */
68 struct jsonrpc_monitor_node {
69 struct ovsdb_jsonrpc_monitor *jsonrpc_monitor;
73 /* A particular column being monitored. */
74 struct ovsdb_monitor_column {
75 const struct ovsdb_column *column;
76 enum ovsdb_monitor_selection select;
79 /* A row that has changed in a monitored table. */
80 struct ovsdb_monitor_row {
81 struct hmap_node hmap_node; /* In ovsdb_jsonrpc_monitor_table.changes. */
82 struct uuid uuid; /* UUID of row that changed. */
83 struct ovsdb_datum *old; /* Old data, NULL for an inserted row. */
84 struct ovsdb_datum *new; /* New data, NULL for a deleted row. */
87 /* Contains 'struct ovsdb_monitor_row's for rows that have been
88 * updated but not yet flushed to all the jsonrpc connection.
90 * 'n_refs' represent the number of jsonrpc connections that have
91 * not received updates. Generate the update for the last jsonprc
92 * connection will also destroy the whole "struct ovsdb_monitor_changes"
95 * 'transaction' stores the first update's transaction id.
97 struct ovsdb_monitor_changes {
98 struct ovsdb_monitor_table *mt;
101 uint64_t transaction;
102 struct hmap_node hmap_node; /* Element in ovsdb_monitor_tables' changes
106 /* A particular table being monitored. */
107 struct ovsdb_monitor_table {
108 const struct ovsdb_table *table;
110 /* This is the union (bitwise-OR) of the 'select' values in all of the
111 * members of 'columns' below. */
112 enum ovsdb_monitor_selection select;
114 /* Columns being monitored. */
115 struct ovsdb_monitor_column *columns;
118 /* Contains 'ovsdb_monitor_changes' indexed by 'transaction'. */
122 typedef struct json *
123 (*compose_row_update_cb_func)(const struct ovsdb_monitor_table *mt,
124 const struct ovsdb_monitor_row *row,
125 bool initial, unsigned long int *changed);
127 static void ovsdb_monitor_destroy(struct ovsdb_monitor *dbmon);
128 static struct ovsdb_monitor_changes * ovsdb_monitor_table_add_changes(
129 struct ovsdb_monitor_table *mt, uint64_t next_txn);
130 static struct ovsdb_monitor_changes *ovsdb_monitor_table_find_changes(
131 struct ovsdb_monitor_table *mt, uint64_t unflushed);
132 static void ovsdb_monitor_changes_destroy(
133 struct ovsdb_monitor_changes *changes);
134 static void ovsdb_monitor_table_track_changes(struct ovsdb_monitor_table *mt,
137 static struct ovsdb_monitor_json_cache_node *
138 ovsdb_monitor_json_cache_search(const struct ovsdb_monitor *dbmon,
141 struct ovsdb_monitor_json_cache_node *node;
142 uint32_t hash = hash_uint64(from_txn);
144 HMAP_FOR_EACH_WITH_HASH(node, hmap_node, hash, &dbmon->json_cache) {
145 if (node->from_txn == from_txn) {
154 ovsdb_monitor_json_cache_insert(struct ovsdb_monitor *dbmon,
155 uint64_t from_txn, struct json *json)
157 struct ovsdb_monitor_json_cache_node *node;
160 node = xmalloc(sizeof *node);
162 hash = hash_uint64(from_txn);
163 node->from_txn = from_txn;
164 node->json = json ? json_clone(json) : NULL;
166 hmap_insert(&dbmon->json_cache, &node->hmap_node, hash);
170 ovsdb_monitor_json_cache_flush(struct ovsdb_monitor *dbmon)
172 struct ovsdb_monitor_json_cache_node *node, *next;
174 HMAP_FOR_EACH_SAFE(node, next, hmap_node, &dbmon->json_cache) {
175 hmap_remove(&dbmon->json_cache, &node->hmap_node);
176 json_destroy(node->json);
182 compare_ovsdb_monitor_column(const void *a_, const void *b_)
184 const struct ovsdb_monitor_column *a = a_;
185 const struct ovsdb_monitor_column *b = b_;
187 return a->column < b->column ? -1 : a->column > b->column;
190 static struct ovsdb_monitor *
191 ovsdb_monitor_cast(struct ovsdb_replica *replica)
193 ovs_assert(replica->class == &ovsdb_jsonrpc_replica_class);
194 return CONTAINER_OF(replica, struct ovsdb_monitor, replica);
197 /* Finds and returns the ovsdb_monitor_row in 'mt->changes->rows' for the
198 * given 'uuid', or NULL if there is no such row. */
199 static struct ovsdb_monitor_row *
200 ovsdb_monitor_changes_row_find(const struct ovsdb_monitor_changes *changes,
201 const struct uuid *uuid)
203 struct ovsdb_monitor_row *row;
205 HMAP_FOR_EACH_WITH_HASH (row, hmap_node, uuid_hash(uuid),
207 if (uuid_equals(uuid, &row->uuid)) {
214 /* Allocates an array of 'mt->n_columns' ovsdb_datums and initializes them as
215 * copies of the data in 'row' drawn from the columns represented by
216 * mt->columns[]. Returns the array.
218 * If 'row' is NULL, returns NULL. */
219 static struct ovsdb_datum *
220 clone_monitor_row_data(const struct ovsdb_monitor_table *mt,
221 const struct ovsdb_row *row)
223 struct ovsdb_datum *data;
230 data = xmalloc(mt->n_columns * sizeof *data);
231 for (i = 0; i < mt->n_columns; i++) {
232 const struct ovsdb_column *c = mt->columns[i].column;
233 const struct ovsdb_datum *src = &row->fields[c->index];
234 struct ovsdb_datum *dst = &data[i];
235 const struct ovsdb_type *type = &c->type;
237 ovsdb_datum_clone(dst, src, type);
242 /* Replaces the mt->n_columns ovsdb_datums in row[] by copies of the data from
243 * in 'row' drawn from the columns represented by mt->columns[]. */
245 update_monitor_row_data(const struct ovsdb_monitor_table *mt,
246 const struct ovsdb_row *row,
247 struct ovsdb_datum *data)
251 for (i = 0; i < mt->n_columns; i++) {
252 const struct ovsdb_column *c = mt->columns[i].column;
253 const struct ovsdb_datum *src = &row->fields[c->index];
254 struct ovsdb_datum *dst = &data[i];
255 const struct ovsdb_type *type = &c->type;
257 if (!ovsdb_datum_equals(src, dst, type)) {
258 ovsdb_datum_destroy(dst, type);
259 ovsdb_datum_clone(dst, src, type);
264 /* Frees all of the mt->n_columns ovsdb_datums in data[], using the types taken
265 * from mt->columns[], plus 'data' itself. */
267 free_monitor_row_data(const struct ovsdb_monitor_table *mt,
268 struct ovsdb_datum *data)
273 for (i = 0; i < mt->n_columns; i++) {
274 const struct ovsdb_column *c = mt->columns[i].column;
276 ovsdb_datum_destroy(&data[i], &c->type);
282 /* Frees 'row', which must have been created from 'mt'. */
284 ovsdb_monitor_row_destroy(const struct ovsdb_monitor_table *mt,
285 struct ovsdb_monitor_row *row)
288 free_monitor_row_data(mt, row->old);
289 free_monitor_row_data(mt, row->new);
295 ovsdb_monitor_add_jsonrpc_monitor(struct ovsdb_monitor *dbmon,
296 struct ovsdb_jsonrpc_monitor *jsonrpc_monitor)
298 struct jsonrpc_monitor_node *jm;
300 jm = xzalloc(sizeof *jm);
301 jm->jsonrpc_monitor = jsonrpc_monitor;
302 list_push_back(&dbmon->jsonrpc_monitors, &jm->node);
305 struct ovsdb_monitor *
306 ovsdb_monitor_create(struct ovsdb *db,
307 struct ovsdb_jsonrpc_monitor *jsonrpc_monitor)
309 struct ovsdb_monitor *dbmon;
311 dbmon = xzalloc(sizeof *dbmon);
313 ovsdb_replica_init(&dbmon->replica, &ovsdb_jsonrpc_replica_class);
314 ovsdb_add_replica(db, &dbmon->replica);
315 list_init(&dbmon->jsonrpc_monitors);
317 dbmon->n_transactions = 0;
318 shash_init(&dbmon->tables);
319 hmap_node_nullify(&dbmon->hmap_node);
320 hmap_init(&dbmon->json_cache);
322 ovsdb_monitor_add_jsonrpc_monitor(dbmon, jsonrpc_monitor);
327 ovsdb_monitor_add_table(struct ovsdb_monitor *m,
328 const struct ovsdb_table *table)
330 struct ovsdb_monitor_table *mt;
332 mt = xzalloc(sizeof *mt);
334 shash_add(&m->tables, table->schema->name, mt);
335 hmap_init(&mt->changes);
339 ovsdb_monitor_add_column(struct ovsdb_monitor *dbmon,
340 const struct ovsdb_table *table,
341 const struct ovsdb_column *column,
342 enum ovsdb_monitor_selection select,
343 size_t *allocated_columns)
345 struct ovsdb_monitor_table *mt;
346 struct ovsdb_monitor_column *c;
348 mt = shash_find_data(&dbmon->tables, table->schema->name);
350 if (mt->n_columns >= *allocated_columns) {
351 mt->columns = x2nrealloc(mt->columns, allocated_columns,
352 sizeof *mt->columns);
355 mt->select |= select;
356 c = &mt->columns[mt->n_columns++];
361 /* Check for duplicated column names. Return the first
362 * duplicated column's name if found. Otherwise return
364 const char * OVS_WARN_UNUSED_RESULT
365 ovsdb_monitor_table_check_duplicates(struct ovsdb_monitor *m,
366 const struct ovsdb_table *table)
368 struct ovsdb_monitor_table *mt;
371 mt = shash_find_data(&m->tables, table->schema->name);
374 /* Check for duplicate columns. */
375 qsort(mt->columns, mt->n_columns, sizeof *mt->columns,
376 compare_ovsdb_monitor_column);
377 for (i = 1; i < mt->n_columns; i++) {
378 if (mt->columns[i].column == mt->columns[i - 1].column) {
379 return mt->columns[i].column->name;
387 static struct ovsdb_monitor_changes *
388 ovsdb_monitor_table_add_changes(struct ovsdb_monitor_table *mt,
391 struct ovsdb_monitor_changes *changes;
393 changes = xzalloc(sizeof *changes);
395 changes->transaction = next_txn;
398 hmap_init(&changes->rows);
399 hmap_insert(&mt->changes, &changes->hmap_node, hash_uint64(next_txn));
404 static struct ovsdb_monitor_changes *
405 ovsdb_monitor_table_find_changes(struct ovsdb_monitor_table *mt,
406 uint64_t transaction)
408 struct ovsdb_monitor_changes *changes;
409 size_t hash = hash_uint64(transaction);
411 HMAP_FOR_EACH_WITH_HASH(changes, hmap_node, hash, &mt->changes) {
412 if (changes->transaction == transaction) {
420 /* Stop currently tracking changes to table 'mt' since 'transaction'. */
422 ovsdb_monitor_table_untrack_changes(struct ovsdb_monitor_table *mt,
423 uint64_t transaction)
425 struct ovsdb_monitor_changes *changes =
426 ovsdb_monitor_table_find_changes(mt, transaction);
428 if (--changes->n_refs == 0) {
429 hmap_remove(&mt->changes, &changes->hmap_node);
430 ovsdb_monitor_changes_destroy(changes);
435 /* Start tracking changes to table 'mt' begins from 'transaction' inclusive.
438 ovsdb_monitor_table_track_changes(struct ovsdb_monitor_table *mt,
439 uint64_t transaction)
441 struct ovsdb_monitor_changes *changes;
443 changes = ovsdb_monitor_table_find_changes(mt, transaction);
447 ovsdb_monitor_table_add_changes(mt, transaction);
452 ovsdb_monitor_changes_destroy(struct ovsdb_monitor_changes *changes)
454 struct ovsdb_monitor_row *row, *next;
456 HMAP_FOR_EACH_SAFE (row, next, hmap_node, &changes->rows) {
457 hmap_remove(&changes->rows, &row->hmap_node);
458 ovsdb_monitor_row_destroy(changes->mt, row);
460 hmap_destroy(&changes->rows);
464 static enum ovsdb_monitor_selection
465 ovsdb_monitor_row_update_type(bool initial, const bool old, const bool new)
467 return initial ? OJMS_INITIAL
473 ovsdb_monitor_row_skip_update(const struct ovsdb_monitor_table *mt,
474 const struct ovsdb_monitor_row *row,
475 enum ovsdb_monitor_selection type,
476 unsigned long int *changed)
478 if (!(mt->select & type)) {
482 if (type == OJMS_MODIFY) {
486 memset(changed, 0, bitmap_n_bytes(mt->n_columns));
487 for (i = 0; i < mt->n_columns; i++) {
488 const struct ovsdb_column *c = mt->columns[i].column;
489 if (!ovsdb_datum_equals(&row->old[i], &row->new[i], &c->type)) {
490 bitmap_set1(changed, i);
495 /* No actual changes: presumably a row changed and then
496 * changed back later. */
504 /* Returns JSON for a <row-update> (as described in RFC 7047) for 'row' within
505 * 'mt', or NULL if no row update should be sent.
507 * The caller should specify 'initial' as true if the returned JSON is going to
508 * be used as part of the initial reply to a "monitor" request, false if it is
509 * going to be used as part of an "update" notification.
511 * 'changed' must be a scratch buffer for internal use that is at least
512 * bitmap_n_bytes(mt->n_columns) bytes long. */
514 ovsdb_monitor_compose_row_update(
515 const struct ovsdb_monitor_table *mt,
516 const struct ovsdb_monitor_row *row,
517 bool initial, unsigned long int *changed)
519 enum ovsdb_monitor_selection type;
520 struct json *old_json, *new_json;
521 struct json *row_json;
524 type = ovsdb_monitor_row_update_type(initial, row->old, row->new);
525 if (ovsdb_monitor_row_skip_update(mt, row, type, changed)) {
529 row_json = json_object_create();
530 old_json = new_json = NULL;
531 if (type & (OJMS_DELETE | OJMS_MODIFY)) {
532 old_json = json_object_create();
533 json_object_put(row_json, "old", old_json);
535 if (type & (OJMS_INITIAL | OJMS_INSERT | OJMS_MODIFY)) {
536 new_json = json_object_create();
537 json_object_put(row_json, "new", new_json);
539 for (i = 0; i < mt->n_columns; i++) {
540 const struct ovsdb_monitor_column *c = &mt->columns[i];
542 if (!(type & c->select)) {
543 /* We don't care about this type of change for this
544 * particular column (but we will care about it for some
549 if ((type == OJMS_MODIFY && bitmap_is_set(changed, i))
550 || type == OJMS_DELETE) {
551 json_object_put(old_json, c->column->name,
552 ovsdb_datum_to_json(&row->old[i],
555 if (type & (OJMS_INITIAL | OJMS_INSERT | OJMS_MODIFY)) {
556 json_object_put(new_json, c->column->name,
557 ovsdb_datum_to_json(&row->new[i],
565 /* Returns JSON for a <row-update2> (as described in ovsdb-server(1) mapage)
566 * for 'row' within * 'mt', or NULL if no row update should be sent.
568 * The caller should specify 'initial' as true if the returned JSON is
569 * going to be used as part of the initial reply to a "monitor2" request,
570 * false if it is going to be used as part of an "update2" notification.
572 * 'changed' must be a scratch buffer for internal use that is at least
573 * bitmap_n_bytes(mt->n_columns) bytes long. */
575 ovsdb_monitor_compose_row_update2(
576 const struct ovsdb_monitor_table *mt,
577 const struct ovsdb_monitor_row *row,
578 bool initial, unsigned long int *changed)
580 enum ovsdb_monitor_selection type;
581 struct json *row_update2, *diff_json;
584 type = ovsdb_monitor_row_update_type(initial, row->old, row->new);
585 if (ovsdb_monitor_row_skip_update(mt, row, type, changed)) {
589 row_update2 = json_object_create();
590 if (type == OJMS_DELETE) {
591 json_object_put(row_update2, "delete", json_null_create());
593 diff_json = json_object_create();
596 for (i = 0; i < mt->n_columns; i++) {
597 const struct ovsdb_monitor_column *c = &mt->columns[i];
599 if (!(type & c->select)) {
600 /* We don't care about this type of change for this
601 * particular column (but we will care about it for some
606 if (type == OJMS_MODIFY) {
607 struct ovsdb_datum diff;
609 if (!bitmap_is_set(changed, i)) {
613 ovsdb_datum_diff(&diff ,&row->old[i], &row->new[i],
615 json_object_put(diff_json, c->column->name,
616 ovsdb_datum_to_json(&diff, &c->column->type));
617 ovsdb_datum_destroy(&diff, &c->column->type);
619 if (!ovsdb_datum_is_default(&row->new[i], &c->column->type)) {
620 json_object_put(diff_json, c->column->name,
621 ovsdb_datum_to_json(&row->new[i],
627 op = type == OJMS_INITIAL ? "initial"
628 : type == OJMS_MODIFY ? "modify" : "insert";
629 json_object_put(row_update2, op, diff_json);
636 ovsdb_monitor_max_columns(struct ovsdb_monitor *dbmon)
638 struct shash_node *node;
639 size_t max_columns = 0;
641 SHASH_FOR_EACH (node, &dbmon->tables) {
642 struct ovsdb_monitor_table *mt = node->data;
644 max_columns = MAX(max_columns, mt->n_columns);
650 /* Constructs and returns JSON for a <table-updates> object (as described in
651 * RFC 7047) for all the outstanding changes within 'monitor', starting from
654 ovsdb_monitor_compose_update(struct ovsdb_monitor *dbmon,
655 bool initial, uint64_t transaction,
656 compose_row_update_cb_func row_update)
658 struct shash_node *node;
660 size_t max_columns = ovsdb_monitor_max_columns(dbmon);
661 unsigned long int *changed = xmalloc(bitmap_n_bytes(max_columns));
664 SHASH_FOR_EACH (node, &dbmon->tables) {
665 struct ovsdb_monitor_table *mt = node->data;
666 struct ovsdb_monitor_row *row, *next;
667 struct ovsdb_monitor_changes *changes;
668 struct json *table_json = NULL;
670 changes = ovsdb_monitor_table_find_changes(mt, transaction);
675 HMAP_FOR_EACH_SAFE (row, next, hmap_node, &changes->rows) {
676 struct json *row_json;
678 row_json = (*row_update)(mt, row, initial, changed);
680 char uuid[UUID_LEN + 1];
682 /* Create JSON object for transaction overall. */
684 json = json_object_create();
687 /* Create JSON object for transaction on this table. */
689 table_json = json_object_create();
690 json_object_put(json, mt->table->schema->name, table_json);
693 /* Add JSON row to JSON table. */
694 snprintf(uuid, sizeof uuid, UUID_FMT, UUID_ARGS(&row->uuid));
695 json_object_put(table_json, uuid, row_json);
704 /* Returns JSON for a <table-updates> object (as described in RFC 7047)
705 * for all the outstanding changes within 'monitor' that starts from
706 * '*unflushed' transaction id.
708 * The caller should specify 'initial' as true if the returned JSON is going to
709 * be used as part of the initial reply to a "monitor" request, false if it is
710 * going to be used as part of an "update" notification. */
712 ovsdb_monitor_get_update(struct ovsdb_monitor *dbmon,
713 bool initial, uint64_t *unflushed,
714 enum ovsdb_monitor_version version)
716 struct ovsdb_monitor_json_cache_node *cache_node;
717 struct shash_node *node;
719 uint64_t prev_txn = *unflushed;
720 uint64_t next_txn = dbmon->n_transactions + 1;
722 /* Return a clone of cached json if one exists. Otherwise,
723 * generate a new one and add it to the cache. */
724 cache_node = ovsdb_monitor_json_cache_search(dbmon, prev_txn);
726 json = cache_node->json ? json_clone(cache_node->json) : NULL;
728 if (version == OVSDB_MONITOR_V1) {
729 json = ovsdb_monitor_compose_update(dbmon, initial, prev_txn,
730 ovsdb_monitor_compose_row_update);
732 ovs_assert(version == OVSDB_MONITOR_V2);
733 json = ovsdb_monitor_compose_update(dbmon, initial, prev_txn,
734 ovsdb_monitor_compose_row_update2);
736 ovsdb_monitor_json_cache_insert(dbmon, prev_txn, json);
739 /* Maintain transaction id of 'changes'. */
740 SHASH_FOR_EACH (node, &dbmon->tables) {
741 struct ovsdb_monitor_table *mt = node->data;
743 ovsdb_monitor_table_untrack_changes(mt, prev_txn);
744 ovsdb_monitor_table_track_changes(mt, next_txn);
746 *unflushed = next_txn;
752 ovsdb_monitor_needs_flush(struct ovsdb_monitor *dbmon,
753 uint64_t next_transaction)
755 ovs_assert(next_transaction <= dbmon->n_transactions + 1);
756 return (next_transaction <= dbmon->n_transactions);
760 ovsdb_monitor_table_add_select(struct ovsdb_monitor *dbmon,
761 const struct ovsdb_table *table,
762 enum ovsdb_monitor_selection select)
764 struct ovsdb_monitor_table * mt;
766 mt = shash_find_data(&dbmon->tables, table->schema->name);
767 mt->select |= select;
771 * If a row's change type (insert, delete or modify) matches that of
772 * the monitor, they should be sent to the monitor's clients as updates.
773 * Of cause, the monitor should also internally update with this change.
775 * When a change type does not require client side update, the monitor
776 * may still need to keep track of certain changes in order to generate
777 * correct future updates. For example, the monitor internal state should
778 * be updated whenever a new row is inserted, in order to generate the
779 * correct initial state, regardless if a insert change type is being
782 * On the other hand, if a transaction only contains changes to columns
783 * that are not monitored, this transaction can be safely ignored by the
786 * Thus, the order of the declaration is important:
787 * 'OVSDB_CHANGES_REQUIRE_EXTERNAL_UPDATE' always implies
788 * 'OVSDB_CHANGES_REQUIRE_INTERNAL_UPDATE', but not vice versa. */
789 enum ovsdb_monitor_changes_efficacy {
790 OVSDB_CHANGES_NO_EFFECT, /* Monitor does not care about this
792 OVSDB_CHANGES_REQUIRE_INTERNAL_UPDATE, /* Monitor internal updates. */
793 OVSDB_CHANGES_REQUIRE_EXTERNAL_UPDATE, /* Client needs to be updated. */
796 struct ovsdb_monitor_aux {
797 const struct ovsdb_monitor *monitor;
798 struct ovsdb_monitor_table *mt;
799 enum ovsdb_monitor_changes_efficacy efficacy;
803 ovsdb_monitor_init_aux(struct ovsdb_monitor_aux *aux,
804 const struct ovsdb_monitor *m)
808 aux->efficacy = OVSDB_CHANGES_NO_EFFECT;
812 ovsdb_monitor_changes_update(const struct ovsdb_row *old,
813 const struct ovsdb_row *new,
814 const struct ovsdb_monitor_table *mt,
815 struct ovsdb_monitor_changes *changes)
817 const struct uuid *uuid = ovsdb_row_get_uuid(new ? new : old);
818 struct ovsdb_monitor_row *change;
820 change = ovsdb_monitor_changes_row_find(changes, uuid);
822 change = xzalloc(sizeof *change);
823 hmap_insert(&changes->rows, &change->hmap_node, uuid_hash(uuid));
824 change->uuid = *uuid;
825 change->old = clone_monitor_row_data(mt, old);
826 change->new = clone_monitor_row_data(mt, new);
829 update_monitor_row_data(mt, new, change->new);
831 free_monitor_row_data(mt, change->new);
835 /* This row was added then deleted. Forget about it. */
836 hmap_remove(&changes->rows, &change->hmap_node);
844 ovsdb_monitor_columns_changed(const struct ovsdb_monitor_table *mt,
845 const unsigned long int *changed)
849 for (i = 0; i < mt->n_columns; i++) {
850 size_t column_index = mt->columns[i].column->index;
852 if (bitmap_is_set(changed, column_index)) {
860 /* Return the efficacy of a row's change to a monitor table.
862 * Please see the block comment above 'ovsdb_monitor_changes_efficacy'
863 * definition form more information. */
864 static enum ovsdb_monitor_changes_efficacy
865 ovsdb_monitor_changes_classify(enum ovsdb_monitor_selection type,
866 const struct ovsdb_monitor_table *mt,
867 const unsigned long int *changed)
869 if (type == OJMS_MODIFY &&
870 !ovsdb_monitor_columns_changed(mt, changed)) {
871 return OVSDB_CHANGES_NO_EFFECT;
874 return (mt->select & type)
875 ? OVSDB_CHANGES_REQUIRE_EXTERNAL_UPDATE
876 : OVSDB_CHANGES_REQUIRE_INTERNAL_UPDATE;
880 ovsdb_monitor_change_cb(const struct ovsdb_row *old,
881 const struct ovsdb_row *new,
882 const unsigned long int *changed,
885 struct ovsdb_monitor_aux *aux = aux_;
886 const struct ovsdb_monitor *m = aux->monitor;
887 struct ovsdb_table *table = new ? new->table : old->table;
888 struct ovsdb_monitor_table *mt;
889 struct ovsdb_monitor_changes *changes;
891 if (!aux->mt || table != aux->mt->table) {
892 aux->mt = shash_find_data(&m->tables, table->schema->name);
894 /* We don't care about rows in this table at all. Tell the caller
901 HMAP_FOR_EACH(changes, hmap_node, &mt->changes) {
902 enum ovsdb_monitor_changes_efficacy efficacy;
903 enum ovsdb_monitor_selection type;
905 type = ovsdb_monitor_row_update_type(false, old, new);
906 efficacy = ovsdb_monitor_changes_classify(type, mt, changed);
907 if (efficacy > OVSDB_CHANGES_NO_EFFECT) {
908 ovsdb_monitor_changes_update(old, new, mt, changes);
911 if (aux->efficacy < efficacy) {
912 aux->efficacy = efficacy;
920 ovsdb_monitor_get_initial(const struct ovsdb_monitor *dbmon)
922 struct ovsdb_monitor_aux aux;
923 struct shash_node *node;
925 ovsdb_monitor_init_aux(&aux, dbmon);
926 SHASH_FOR_EACH (node, &dbmon->tables) {
927 struct ovsdb_monitor_table *mt = node->data;
929 if (mt->select & OJMS_INITIAL) {
930 struct ovsdb_row *row;
931 struct ovsdb_monitor_changes *changes;
933 changes = ovsdb_monitor_table_find_changes(mt, 0);
935 changes = ovsdb_monitor_table_add_changes(mt, 0);
936 HMAP_FOR_EACH (row, hmap_node, &mt->table->rows) {
937 ovsdb_monitor_changes_update(NULL, row, mt, changes);
947 ovsdb_monitor_remove_jsonrpc_monitor(struct ovsdb_monitor *dbmon,
948 struct ovsdb_jsonrpc_monitor *jsonrpc_monitor)
950 struct jsonrpc_monitor_node *jm;
952 if (list_is_empty(&dbmon->jsonrpc_monitors)) {
953 ovsdb_monitor_destroy(dbmon);
957 /* Find and remove the jsonrpc monitor from the list. */
958 LIST_FOR_EACH(jm, node, &dbmon->jsonrpc_monitors) {
959 if (jm->jsonrpc_monitor == jsonrpc_monitor) {
960 list_remove(&jm->node);
963 /* Destroy ovsdb monitor if this is the last user. */
964 if (list_is_empty(&dbmon->jsonrpc_monitors)) {
965 ovsdb_monitor_destroy(dbmon);
972 /* Should never reach here. jsonrpc_monitor should be on the list. */
977 ovsdb_monitor_table_equal(const struct ovsdb_monitor_table *a,
978 const struct ovsdb_monitor_table *b)
982 if ((a->table != b->table) ||
983 (a->select != b->select) ||
984 (a->n_columns != b->n_columns)) {
988 for (i = 0; i < a->n_columns; i++) {
989 if ((a->columns[i].column != b->columns[i].column) ||
990 (a->columns[i].select != b->columns[i].select)) {
999 ovsdb_monitor_equal(const struct ovsdb_monitor *a,
1000 const struct ovsdb_monitor *b)
1002 struct shash_node *node;
1004 if (shash_count(&a->tables) != shash_count(&b->tables)) {
1008 SHASH_FOR_EACH(node, &a->tables) {
1009 const struct ovsdb_monitor_table *mta = node->data;
1010 const struct ovsdb_monitor_table *mtb;
1012 mtb = shash_find_data(&b->tables, node->name);
1017 if (!ovsdb_monitor_table_equal(mta, mtb)) {
1026 ovsdb_monitor_hash(const struct ovsdb_monitor *dbmon, size_t basis)
1028 const struct shash_node **nodes;
1031 nodes = shash_sort(&dbmon->tables);
1032 n = shash_count(&dbmon->tables);
1034 for (i = 0; i < n; i++) {
1035 struct ovsdb_monitor_table *mt = nodes[i]->data;
1037 basis = hash_pointer(mt->table, basis);
1038 basis = hash_3words(mt->select, mt->n_columns, basis);
1040 for (j = 0; j < mt->n_columns; j++) {
1041 basis = hash_pointer(mt->columns[j].column, basis);
1042 basis = hash_2words(mt->columns[j].select, basis);
1050 struct ovsdb_monitor *
1051 ovsdb_monitor_add(struct ovsdb_monitor *new_dbmon)
1053 struct ovsdb_monitor *dbmon;
1056 /* New_dbmon should be associated with only one jsonrpc
1058 ovs_assert(list_is_singleton(&new_dbmon->jsonrpc_monitors));
1060 hash = ovsdb_monitor_hash(new_dbmon, 0);
1061 HMAP_FOR_EACH_WITH_HASH(dbmon, hmap_node, hash, &ovsdb_monitors) {
1062 if (ovsdb_monitor_equal(dbmon, new_dbmon)) {
1067 hmap_insert(&ovsdb_monitors, &new_dbmon->hmap_node, hash);
1072 ovsdb_monitor_destroy(struct ovsdb_monitor *dbmon)
1074 struct shash_node *node;
1076 list_remove(&dbmon->replica.node);
1078 if (!hmap_node_is_null(&dbmon->hmap_node)) {
1079 hmap_remove(&ovsdb_monitors, &dbmon->hmap_node);
1082 ovsdb_monitor_json_cache_flush(dbmon);
1083 hmap_destroy(&dbmon->json_cache);
1085 SHASH_FOR_EACH (node, &dbmon->tables) {
1086 struct ovsdb_monitor_table *mt = node->data;
1087 struct ovsdb_monitor_changes *changes, *next;
1089 HMAP_FOR_EACH_SAFE (changes, next, hmap_node, &mt->changes) {
1090 hmap_remove(&mt->changes, &changes->hmap_node);
1091 ovsdb_monitor_changes_destroy(changes);
1093 hmap_destroy(&mt->changes);
1097 shash_destroy(&dbmon->tables);
1101 static struct ovsdb_error *
1102 ovsdb_monitor_commit(struct ovsdb_replica *replica,
1103 const struct ovsdb_txn *txn,
1104 bool durable OVS_UNUSED)
1106 struct ovsdb_monitor *m = ovsdb_monitor_cast(replica);
1107 struct ovsdb_monitor_aux aux;
1109 ovsdb_monitor_init_aux(&aux, m);
1110 ovsdb_txn_for_each_change(txn, ovsdb_monitor_change_cb, &aux);
1112 if (aux.efficacy == OVSDB_CHANGES_REQUIRE_EXTERNAL_UPDATE) {
1113 ovsdb_monitor_json_cache_flush(m);
1114 m->n_transactions++;
1121 ovsdb_monitor_destroy_callback(struct ovsdb_replica *replica)
1123 struct ovsdb_monitor *dbmon = ovsdb_monitor_cast(replica);
1124 struct jsonrpc_monitor_node *jm, *next;
1126 /* Delete all front end monitors. Removing the last front
1127 * end monitor will also destroy the corresponding 'ovsdb_monitor'.
1128 * ovsdb monitor will also be destroied. */
1129 LIST_FOR_EACH_SAFE(jm, next, node, &dbmon->jsonrpc_monitors) {
1130 ovsdb_jsonrpc_monitor_destroy(jm->jsonrpc_monitor);
1134 static const struct ovsdb_replica_class ovsdb_jsonrpc_replica_class = {
1135 ovsdb_monitor_commit,
1136 ovsdb_monitor_destroy_callback,