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 "openvswitch/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. */
64 enum ovsdb_monitor_version version;
66 struct json *json; /* Null, or a cloned of json */
69 struct jsonrpc_monitor_node {
70 struct ovsdb_jsonrpc_monitor *jsonrpc_monitor;
74 /* A particular column being monitored. */
75 struct ovsdb_monitor_column {
76 const struct ovsdb_column *column;
77 enum ovsdb_monitor_selection select;
80 /* A row that has changed in a monitored table. */
81 struct ovsdb_monitor_row {
82 struct hmap_node hmap_node; /* In ovsdb_jsonrpc_monitor_table.changes. */
83 struct uuid uuid; /* UUID of row that changed. */
84 struct ovsdb_datum *old; /* Old data, NULL for an inserted row. */
85 struct ovsdb_datum *new; /* New data, NULL for a deleted row. */
88 /* Contains 'struct ovsdb_monitor_row's for rows that have been
89 * updated but not yet flushed to all the jsonrpc connection.
91 * 'n_refs' represent the number of jsonrpc connections that have
92 * not received updates. Generate the update for the last jsonprc
93 * connection will also destroy the whole "struct ovsdb_monitor_changes"
96 * 'transaction' stores the first update's transaction id.
98 struct ovsdb_monitor_changes {
99 struct ovsdb_monitor_table *mt;
102 uint64_t transaction;
103 struct hmap_node hmap_node; /* Element in ovsdb_monitor_tables' changes
107 /* A particular table being monitored. */
108 struct ovsdb_monitor_table {
109 const struct ovsdb_table *table;
111 /* This is the union (bitwise-OR) of the 'select' values in all of the
112 * members of 'columns' below. */
113 enum ovsdb_monitor_selection select;
115 /* Columns being monitored. */
116 struct ovsdb_monitor_column *columns;
119 /* Columns in ovsdb_monitor_row have different indexes then in
120 * ovsdb_row. This field maps between column->index to the index in the
121 * ovsdb_monitor_row. It is used for condition evaluation. */
122 unsigned int *columns_index_map;
124 /* Contains 'ovsdb_monitor_changes' indexed by 'transaction'. */
128 typedef struct json *
129 (*compose_row_update_cb_func)(const struct ovsdb_monitor_table *mt,
130 const struct ovsdb_monitor_row *row,
131 bool initial, unsigned long int *changed);
133 static void ovsdb_monitor_destroy(struct ovsdb_monitor *dbmon);
134 static struct ovsdb_monitor_changes * ovsdb_monitor_table_add_changes(
135 struct ovsdb_monitor_table *mt, uint64_t next_txn);
136 static struct ovsdb_monitor_changes *ovsdb_monitor_table_find_changes(
137 struct ovsdb_monitor_table *mt, uint64_t unflushed);
138 static void ovsdb_monitor_changes_destroy(
139 struct ovsdb_monitor_changes *changes);
140 static void ovsdb_monitor_table_track_changes(struct ovsdb_monitor_table *mt,
144 json_cache_hash(enum ovsdb_monitor_version version, uint64_t from_txn)
148 hash = hash_uint64(version);
149 hash = hash_uint64_basis(from_txn, hash);
154 static struct ovsdb_monitor_json_cache_node *
155 ovsdb_monitor_json_cache_search(const struct ovsdb_monitor *dbmon,
156 enum ovsdb_monitor_version version,
159 struct ovsdb_monitor_json_cache_node *node;
160 uint32_t hash = json_cache_hash(version, from_txn);
162 HMAP_FOR_EACH_WITH_HASH(node, hmap_node, hash, &dbmon->json_cache) {
163 if (node->from_txn == from_txn && node->version == version) {
172 ovsdb_monitor_json_cache_insert(struct ovsdb_monitor *dbmon,
173 enum ovsdb_monitor_version version,
174 uint64_t from_txn, struct json *json)
176 struct ovsdb_monitor_json_cache_node *node;
177 uint32_t hash = json_cache_hash(version, from_txn);
179 node = xmalloc(sizeof *node);
181 node->version = version;
182 node->from_txn = from_txn;
183 node->json = json ? json_clone(json) : NULL;
185 hmap_insert(&dbmon->json_cache, &node->hmap_node, hash);
189 ovsdb_monitor_json_cache_flush(struct ovsdb_monitor *dbmon)
191 struct ovsdb_monitor_json_cache_node *node;
193 HMAP_FOR_EACH_POP(node, hmap_node, &dbmon->json_cache) {
194 json_destroy(node->json);
200 compare_ovsdb_monitor_column(const void *a_, const void *b_)
202 const struct ovsdb_monitor_column *a = a_;
203 const struct ovsdb_monitor_column *b = b_;
205 return a->column < b->column ? -1 : a->column > b->column;
208 static struct ovsdb_monitor *
209 ovsdb_monitor_cast(struct ovsdb_replica *replica)
211 ovs_assert(replica->class == &ovsdb_jsonrpc_replica_class);
212 return CONTAINER_OF(replica, struct ovsdb_monitor, replica);
215 /* Finds and returns the ovsdb_monitor_row in 'mt->changes->rows' for the
216 * given 'uuid', or NULL if there is no such row. */
217 static struct ovsdb_monitor_row *
218 ovsdb_monitor_changes_row_find(const struct ovsdb_monitor_changes *changes,
219 const struct uuid *uuid)
221 struct ovsdb_monitor_row *row;
223 HMAP_FOR_EACH_WITH_HASH (row, hmap_node, uuid_hash(uuid),
225 if (uuid_equals(uuid, &row->uuid)) {
232 /* Allocates an array of 'mt->n_columns' ovsdb_datums and initializes them as
233 * copies of the data in 'row' drawn from the columns represented by
234 * mt->columns[]. Returns the array.
236 * If 'row' is NULL, returns NULL. */
237 static struct ovsdb_datum *
238 clone_monitor_row_data(const struct ovsdb_monitor_table *mt,
239 const struct ovsdb_row *row)
241 struct ovsdb_datum *data;
248 data = xmalloc(mt->n_columns * sizeof *data);
249 for (i = 0; i < mt->n_columns; i++) {
250 const struct ovsdb_column *c = mt->columns[i].column;
251 const struct ovsdb_datum *src = &row->fields[c->index];
252 struct ovsdb_datum *dst = &data[i];
253 const struct ovsdb_type *type = &c->type;
255 ovsdb_datum_clone(dst, src, type);
260 /* Replaces the mt->n_columns ovsdb_datums in row[] by copies of the data from
261 * in 'row' drawn from the columns represented by mt->columns[]. */
263 update_monitor_row_data(const struct ovsdb_monitor_table *mt,
264 const struct ovsdb_row *row,
265 struct ovsdb_datum *data)
269 for (i = 0; i < mt->n_columns; i++) {
270 const struct ovsdb_column *c = mt->columns[i].column;
271 const struct ovsdb_datum *src = &row->fields[c->index];
272 struct ovsdb_datum *dst = &data[i];
273 const struct ovsdb_type *type = &c->type;
275 if (!ovsdb_datum_equals(src, dst, type)) {
276 ovsdb_datum_destroy(dst, type);
277 ovsdb_datum_clone(dst, src, type);
282 /* Frees all of the mt->n_columns ovsdb_datums in data[], using the types taken
283 * from mt->columns[], plus 'data' itself. */
285 free_monitor_row_data(const struct ovsdb_monitor_table *mt,
286 struct ovsdb_datum *data)
291 for (i = 0; i < mt->n_columns; i++) {
292 const struct ovsdb_column *c = mt->columns[i].column;
294 ovsdb_datum_destroy(&data[i], &c->type);
300 /* Frees 'row', which must have been created from 'mt'. */
302 ovsdb_monitor_row_destroy(const struct ovsdb_monitor_table *mt,
303 struct ovsdb_monitor_row *row)
306 free_monitor_row_data(mt, row->old);
307 free_monitor_row_data(mt, row->new);
313 ovsdb_monitor_columns_sort(struct ovsdb_monitor *dbmon)
316 struct shash_node *node;
318 SHASH_FOR_EACH (node, &dbmon->tables) {
319 struct ovsdb_monitor_table *mt = node->data;
321 qsort(mt->columns, mt->n_columns, sizeof *mt->columns,
322 compare_ovsdb_monitor_column);
323 for (i = 0; i < mt->n_columns; i++) {
324 /* re-set index map due to sort */
325 mt->columns_index_map[mt->columns[i].column->index] = i;
331 ovsdb_monitor_add_jsonrpc_monitor(struct ovsdb_monitor *dbmon,
332 struct ovsdb_jsonrpc_monitor *jsonrpc_monitor)
334 struct jsonrpc_monitor_node *jm;
336 jm = xzalloc(sizeof *jm);
337 jm->jsonrpc_monitor = jsonrpc_monitor;
338 ovs_list_push_back(&dbmon->jsonrpc_monitors, &jm->node);
341 struct ovsdb_monitor *
342 ovsdb_monitor_create(struct ovsdb *db,
343 struct ovsdb_jsonrpc_monitor *jsonrpc_monitor)
345 struct ovsdb_monitor *dbmon;
347 dbmon = xzalloc(sizeof *dbmon);
349 ovsdb_replica_init(&dbmon->replica, &ovsdb_jsonrpc_replica_class);
350 ovsdb_add_replica(db, &dbmon->replica);
351 ovs_list_init(&dbmon->jsonrpc_monitors);
353 dbmon->n_transactions = 0;
354 shash_init(&dbmon->tables);
355 hmap_node_nullify(&dbmon->hmap_node);
356 hmap_init(&dbmon->json_cache);
358 ovsdb_monitor_add_jsonrpc_monitor(dbmon, jsonrpc_monitor);
363 ovsdb_monitor_add_table(struct ovsdb_monitor *m,
364 const struct ovsdb_table *table)
366 struct ovsdb_monitor_table *mt;
368 size_t n_columns = shash_count(&table->schema->columns);
370 mt = xzalloc(sizeof *mt);
372 shash_add(&m->tables, table->schema->name, mt);
373 hmap_init(&mt->changes);
374 mt->columns_index_map =
375 xmalloc(sizeof *mt->columns_index_map * n_columns);
376 for (i = 0; i < n_columns; i++) {
377 mt->columns_index_map[i] = -1;
382 ovsdb_monitor_add_column(struct ovsdb_monitor *dbmon,
383 const struct ovsdb_table *table,
384 const struct ovsdb_column *column,
385 enum ovsdb_monitor_selection select,
386 size_t *allocated_columns)
388 struct ovsdb_monitor_table *mt;
389 struct ovsdb_monitor_column *c;
391 mt = shash_find_data(&dbmon->tables, table->schema->name);
393 /* Check for column duplication. Return duplicated column name. */
394 if (mt->columns_index_map[column->index] != -1) {
398 if (mt->n_columns >= *allocated_columns) {
399 mt->columns = x2nrealloc(mt->columns, allocated_columns,
400 sizeof *mt->columns);
403 mt->select |= select;
404 mt->columns_index_map[column->index] = mt->n_columns;
405 c = &mt->columns[mt->n_columns++];
412 static struct ovsdb_monitor_changes *
413 ovsdb_monitor_table_add_changes(struct ovsdb_monitor_table *mt,
416 struct ovsdb_monitor_changes *changes;
418 changes = xzalloc(sizeof *changes);
420 changes->transaction = next_txn;
423 hmap_init(&changes->rows);
424 hmap_insert(&mt->changes, &changes->hmap_node, hash_uint64(next_txn));
429 static struct ovsdb_monitor_changes *
430 ovsdb_monitor_table_find_changes(struct ovsdb_monitor_table *mt,
431 uint64_t transaction)
433 struct ovsdb_monitor_changes *changes;
434 size_t hash = hash_uint64(transaction);
436 HMAP_FOR_EACH_WITH_HASH(changes, hmap_node, hash, &mt->changes) {
437 if (changes->transaction == transaction) {
445 /* Stop currently tracking changes to table 'mt' since 'transaction'. */
447 ovsdb_monitor_table_untrack_changes(struct ovsdb_monitor_table *mt,
448 uint64_t transaction)
450 struct ovsdb_monitor_changes *changes =
451 ovsdb_monitor_table_find_changes(mt, transaction);
453 if (--changes->n_refs == 0) {
454 hmap_remove(&mt->changes, &changes->hmap_node);
455 ovsdb_monitor_changes_destroy(changes);
460 /* Start tracking changes to table 'mt' begins from 'transaction' inclusive.
463 ovsdb_monitor_table_track_changes(struct ovsdb_monitor_table *mt,
464 uint64_t transaction)
466 struct ovsdb_monitor_changes *changes;
468 changes = ovsdb_monitor_table_find_changes(mt, transaction);
472 ovsdb_monitor_table_add_changes(mt, transaction);
477 ovsdb_monitor_changes_destroy(struct ovsdb_monitor_changes *changes)
479 struct ovsdb_monitor_row *row, *next;
481 HMAP_FOR_EACH_SAFE (row, next, hmap_node, &changes->rows) {
482 hmap_remove(&changes->rows, &row->hmap_node);
483 ovsdb_monitor_row_destroy(changes->mt, row);
485 hmap_destroy(&changes->rows);
489 static enum ovsdb_monitor_selection
490 ovsdb_monitor_row_update_type(bool initial, const bool old, const bool new)
492 return initial ? OJMS_INITIAL
498 ovsdb_monitor_row_skip_update(const struct ovsdb_monitor_table *mt,
499 const struct ovsdb_monitor_row *row,
500 enum ovsdb_monitor_selection type,
501 unsigned long int *changed)
503 if (!(mt->select & type)) {
507 if (type == OJMS_MODIFY) {
511 memset(changed, 0, bitmap_n_bytes(mt->n_columns));
512 for (i = 0; i < mt->n_columns; i++) {
513 const struct ovsdb_column *c = mt->columns[i].column;
514 if (!ovsdb_datum_equals(&row->old[i], &row->new[i], &c->type)) {
515 bitmap_set1(changed, i);
520 /* No actual changes: presumably a row changed and then
521 * changed back later. */
529 /* Returns JSON for a <row-update> (as described in RFC 7047) for 'row' within
530 * 'mt', or NULL if no row update should be sent.
532 * The caller should specify 'initial' as true if the returned JSON is going to
533 * be used as part of the initial reply to a "monitor" request, false if it is
534 * going to be used as part of an "update" notification.
536 * 'changed' must be a scratch buffer for internal use that is at least
537 * bitmap_n_bytes(mt->n_columns) bytes long. */
539 ovsdb_monitor_compose_row_update(
540 const struct ovsdb_monitor_table *mt,
541 const struct ovsdb_monitor_row *row,
542 bool initial, unsigned long int *changed)
544 enum ovsdb_monitor_selection type;
545 struct json *old_json, *new_json;
546 struct json *row_json;
549 type = ovsdb_monitor_row_update_type(initial, row->old, row->new);
550 if (ovsdb_monitor_row_skip_update(mt, row, type, changed)) {
554 row_json = json_object_create();
555 old_json = new_json = NULL;
556 if (type & (OJMS_DELETE | OJMS_MODIFY)) {
557 old_json = json_object_create();
558 json_object_put(row_json, "old", old_json);
560 if (type & (OJMS_INITIAL | OJMS_INSERT | OJMS_MODIFY)) {
561 new_json = json_object_create();
562 json_object_put(row_json, "new", new_json);
564 for (i = 0; i < mt->n_columns; i++) {
565 const struct ovsdb_monitor_column *c = &mt->columns[i];
567 if (!(type & c->select)) {
568 /* We don't care about this type of change for this
569 * particular column (but we will care about it for some
574 if ((type == OJMS_MODIFY && bitmap_is_set(changed, i))
575 || type == OJMS_DELETE) {
576 json_object_put(old_json, c->column->name,
577 ovsdb_datum_to_json(&row->old[i],
580 if (type & (OJMS_INITIAL | OJMS_INSERT | OJMS_MODIFY)) {
581 json_object_put(new_json, c->column->name,
582 ovsdb_datum_to_json(&row->new[i],
590 /* Returns JSON for a <row-update2> (as described in ovsdb-server(1) mapage)
591 * for 'row' within * 'mt', or NULL if no row update should be sent.
593 * The caller should specify 'initial' as true if the returned JSON is
594 * going to be used as part of the initial reply to a "monitor2" request,
595 * false if it is going to be used as part of an "update2" notification.
597 * 'changed' must be a scratch buffer for internal use that is at least
598 * bitmap_n_bytes(mt->n_columns) bytes long. */
600 ovsdb_monitor_compose_row_update2(
601 const struct ovsdb_monitor_table *mt,
602 const struct ovsdb_monitor_row *row,
603 bool initial, unsigned long int *changed)
605 enum ovsdb_monitor_selection type;
606 struct json *row_update2, *diff_json;
609 type = ovsdb_monitor_row_update_type(initial, row->old, row->new);
610 if (ovsdb_monitor_row_skip_update(mt, row, type, changed)) {
614 row_update2 = json_object_create();
615 if (type == OJMS_DELETE) {
616 json_object_put(row_update2, "delete", json_null_create());
618 diff_json = json_object_create();
621 for (i = 0; i < mt->n_columns; i++) {
622 const struct ovsdb_monitor_column *c = &mt->columns[i];
624 if (!(type & c->select)) {
625 /* We don't care about this type of change for this
626 * particular column (but we will care about it for some
631 if (type == OJMS_MODIFY) {
632 struct ovsdb_datum diff;
634 if (!bitmap_is_set(changed, i)) {
638 ovsdb_datum_diff(&diff ,&row->old[i], &row->new[i],
640 json_object_put(diff_json, c->column->name,
641 ovsdb_datum_to_json(&diff, &c->column->type));
642 ovsdb_datum_destroy(&diff, &c->column->type);
644 if (!ovsdb_datum_is_default(&row->new[i], &c->column->type)) {
645 json_object_put(diff_json, c->column->name,
646 ovsdb_datum_to_json(&row->new[i],
652 op = type == OJMS_INITIAL ? "initial"
653 : type == OJMS_MODIFY ? "modify" : "insert";
654 json_object_put(row_update2, op, diff_json);
661 ovsdb_monitor_max_columns(struct ovsdb_monitor *dbmon)
663 struct shash_node *node;
664 size_t max_columns = 0;
666 SHASH_FOR_EACH (node, &dbmon->tables) {
667 struct ovsdb_monitor_table *mt = node->data;
669 max_columns = MAX(max_columns, mt->n_columns);
675 /* Constructs and returns JSON for a <table-updates> object (as described in
676 * RFC 7047) for all the outstanding changes within 'monitor', starting from
679 ovsdb_monitor_compose_update(struct ovsdb_monitor *dbmon,
680 bool initial, uint64_t transaction,
681 compose_row_update_cb_func row_update)
683 struct shash_node *node;
685 size_t max_columns = ovsdb_monitor_max_columns(dbmon);
686 unsigned long int *changed = xmalloc(bitmap_n_bytes(max_columns));
689 SHASH_FOR_EACH (node, &dbmon->tables) {
690 struct ovsdb_monitor_table *mt = node->data;
691 struct ovsdb_monitor_row *row, *next;
692 struct ovsdb_monitor_changes *changes;
693 struct json *table_json = NULL;
695 changes = ovsdb_monitor_table_find_changes(mt, transaction);
700 HMAP_FOR_EACH_SAFE (row, next, hmap_node, &changes->rows) {
701 struct json *row_json;
703 row_json = (*row_update)(mt, row, initial, changed);
705 char uuid[UUID_LEN + 1];
707 /* Create JSON object for transaction overall. */
709 json = json_object_create();
712 /* Create JSON object for transaction on this table. */
714 table_json = json_object_create();
715 json_object_put(json, mt->table->schema->name, table_json);
718 /* Add JSON row to JSON table. */
719 snprintf(uuid, sizeof uuid, UUID_FMT, UUID_ARGS(&row->uuid));
720 json_object_put(table_json, uuid, row_json);
729 /* Returns JSON for a <table-updates> object (as described in RFC 7047)
730 * for all the outstanding changes within 'monitor' that starts from
731 * '*unflushed' transaction id.
733 * The caller should specify 'initial' as true if the returned JSON is going to
734 * be used as part of the initial reply to a "monitor" request, false if it is
735 * going to be used as part of an "update" notification. */
737 ovsdb_monitor_get_update(struct ovsdb_monitor *dbmon,
738 bool initial, uint64_t *unflushed_,
739 enum ovsdb_monitor_version version)
741 struct ovsdb_monitor_json_cache_node *cache_node;
742 struct shash_node *node;
744 const uint64_t unflushed = *unflushed_;
745 const uint64_t next_unflushed = dbmon->n_transactions + 1;
747 /* Return a clone of cached json if one exists. Otherwise,
748 * generate a new one and add it to the cache. */
749 cache_node = ovsdb_monitor_json_cache_search(dbmon, version, unflushed);
751 json = cache_node->json ? json_clone(cache_node->json) : NULL;
753 if (version == OVSDB_MONITOR_V1) {
754 json = ovsdb_monitor_compose_update(dbmon, initial, unflushed,
755 ovsdb_monitor_compose_row_update);
757 ovs_assert(version == OVSDB_MONITOR_V2);
758 json = ovsdb_monitor_compose_update(dbmon, initial, unflushed,
759 ovsdb_monitor_compose_row_update2);
761 ovsdb_monitor_json_cache_insert(dbmon, version, unflushed, json);
764 /* Maintain transaction id of 'changes'. */
765 SHASH_FOR_EACH (node, &dbmon->tables) {
766 struct ovsdb_monitor_table *mt = node->data;
768 ovsdb_monitor_table_untrack_changes(mt, unflushed);
769 ovsdb_monitor_table_track_changes(mt, next_unflushed);
771 *unflushed_ = next_unflushed;
777 ovsdb_monitor_needs_flush(struct ovsdb_monitor *dbmon,
778 uint64_t next_transaction)
780 ovs_assert(next_transaction <= dbmon->n_transactions + 1);
781 return (next_transaction <= dbmon->n_transactions);
785 ovsdb_monitor_table_add_select(struct ovsdb_monitor *dbmon,
786 const struct ovsdb_table *table,
787 enum ovsdb_monitor_selection select)
789 struct ovsdb_monitor_table * mt;
791 mt = shash_find_data(&dbmon->tables, table->schema->name);
792 mt->select |= select;
796 * If a row's change type (insert, delete or modify) matches that of
797 * the monitor, they should be sent to the monitor's clients as updates.
798 * Of cause, the monitor should also internally update with this change.
800 * When a change type does not require client side update, the monitor
801 * may still need to keep track of certain changes in order to generate
802 * correct future updates. For example, the monitor internal state should
803 * be updated whenever a new row is inserted, in order to generate the
804 * correct initial state, regardless if a insert change type is being
807 * On the other hand, if a transaction only contains changes to columns
808 * that are not monitored, this transaction can be safely ignored by the
811 * Thus, the order of the declaration is important:
812 * 'OVSDB_CHANGES_REQUIRE_EXTERNAL_UPDATE' always implies
813 * 'OVSDB_CHANGES_REQUIRE_INTERNAL_UPDATE', but not vice versa. */
814 enum ovsdb_monitor_changes_efficacy {
815 OVSDB_CHANGES_NO_EFFECT, /* Monitor does not care about this
817 OVSDB_CHANGES_REQUIRE_INTERNAL_UPDATE, /* Monitor internal updates. */
818 OVSDB_CHANGES_REQUIRE_EXTERNAL_UPDATE, /* Client needs to be updated. */
821 struct ovsdb_monitor_aux {
822 const struct ovsdb_monitor *monitor;
823 struct ovsdb_monitor_table *mt;
824 enum ovsdb_monitor_changes_efficacy efficacy;
828 ovsdb_monitor_init_aux(struct ovsdb_monitor_aux *aux,
829 const struct ovsdb_monitor *m)
833 aux->efficacy = OVSDB_CHANGES_NO_EFFECT;
837 ovsdb_monitor_changes_update(const struct ovsdb_row *old,
838 const struct ovsdb_row *new,
839 const struct ovsdb_monitor_table *mt,
840 struct ovsdb_monitor_changes *changes)
842 const struct uuid *uuid = ovsdb_row_get_uuid(new ? new : old);
843 struct ovsdb_monitor_row *change;
845 change = ovsdb_monitor_changes_row_find(changes, uuid);
847 change = xzalloc(sizeof *change);
848 hmap_insert(&changes->rows, &change->hmap_node, uuid_hash(uuid));
849 change->uuid = *uuid;
850 change->old = clone_monitor_row_data(mt, old);
851 change->new = clone_monitor_row_data(mt, new);
854 update_monitor_row_data(mt, new, change->new);
856 free_monitor_row_data(mt, change->new);
860 /* This row was added then deleted. Forget about it. */
861 hmap_remove(&changes->rows, &change->hmap_node);
869 ovsdb_monitor_columns_changed(const struct ovsdb_monitor_table *mt,
870 const unsigned long int *changed)
874 for (i = 0; i < mt->n_columns; i++) {
875 size_t column_index = mt->columns[i].column->index;
877 if (bitmap_is_set(changed, column_index)) {
885 /* Return the efficacy of a row's change to a monitor table.
887 * Please see the block comment above 'ovsdb_monitor_changes_efficacy'
888 * definition form more information. */
889 static enum ovsdb_monitor_changes_efficacy
890 ovsdb_monitor_changes_classify(enum ovsdb_monitor_selection type,
891 const struct ovsdb_monitor_table *mt,
892 const unsigned long int *changed)
894 if (type == OJMS_MODIFY &&
895 !ovsdb_monitor_columns_changed(mt, changed)) {
896 return OVSDB_CHANGES_NO_EFFECT;
899 return (mt->select & type)
900 ? OVSDB_CHANGES_REQUIRE_EXTERNAL_UPDATE
901 : OVSDB_CHANGES_REQUIRE_INTERNAL_UPDATE;
905 ovsdb_monitor_change_cb(const struct ovsdb_row *old,
906 const struct ovsdb_row *new,
907 const unsigned long int *changed,
910 struct ovsdb_monitor_aux *aux = aux_;
911 const struct ovsdb_monitor *m = aux->monitor;
912 struct ovsdb_table *table = new ? new->table : old->table;
913 struct ovsdb_monitor_table *mt;
914 struct ovsdb_monitor_changes *changes;
916 if (!aux->mt || table != aux->mt->table) {
917 aux->mt = shash_find_data(&m->tables, table->schema->name);
919 /* We don't care about rows in this table at all. Tell the caller
926 HMAP_FOR_EACH(changes, hmap_node, &mt->changes) {
927 enum ovsdb_monitor_changes_efficacy efficacy;
928 enum ovsdb_monitor_selection type;
930 type = ovsdb_monitor_row_update_type(false, old, new);
931 efficacy = ovsdb_monitor_changes_classify(type, mt, changed);
932 if (efficacy > OVSDB_CHANGES_NO_EFFECT) {
933 ovsdb_monitor_changes_update(old, new, mt, changes);
936 if (aux->efficacy < efficacy) {
937 aux->efficacy = efficacy;
945 ovsdb_monitor_get_initial(const struct ovsdb_monitor *dbmon)
947 struct ovsdb_monitor_aux aux;
948 struct shash_node *node;
950 ovsdb_monitor_init_aux(&aux, dbmon);
951 SHASH_FOR_EACH (node, &dbmon->tables) {
952 struct ovsdb_monitor_table *mt = node->data;
954 if (mt->select & OJMS_INITIAL) {
955 struct ovsdb_row *row;
956 struct ovsdb_monitor_changes *changes;
958 changes = ovsdb_monitor_table_find_changes(mt, 0);
960 changes = ovsdb_monitor_table_add_changes(mt, 0);
961 HMAP_FOR_EACH (row, hmap_node, &mt->table->rows) {
962 ovsdb_monitor_changes_update(NULL, row, mt, changes);
972 ovsdb_monitor_remove_jsonrpc_monitor(struct ovsdb_monitor *dbmon,
973 struct ovsdb_jsonrpc_monitor *jsonrpc_monitor,
976 struct jsonrpc_monitor_node *jm;
978 if (ovs_list_is_empty(&dbmon->jsonrpc_monitors)) {
979 ovsdb_monitor_destroy(dbmon);
983 /* Find and remove the jsonrpc monitor from the list. */
984 LIST_FOR_EACH(jm, node, &dbmon->jsonrpc_monitors) {
985 if (jm->jsonrpc_monitor == jsonrpc_monitor) {
986 /* Release the tracked changes. */
987 struct shash_node *node;
988 SHASH_FOR_EACH (node, &dbmon->tables) {
989 struct ovsdb_monitor_table *mt = node->data;
990 ovsdb_monitor_table_untrack_changes(mt, unflushed);
992 ovs_list_remove(&jm->node);
995 /* Destroy ovsdb monitor if this is the last user. */
996 if (ovs_list_is_empty(&dbmon->jsonrpc_monitors)) {
997 ovsdb_monitor_destroy(dbmon);
1004 /* Should never reach here. jsonrpc_monitor should be on the list. */
1009 ovsdb_monitor_table_equal(const struct ovsdb_monitor_table *a,
1010 const struct ovsdb_monitor_table *b)
1014 if ((a->table != b->table) ||
1015 (a->select != b->select) ||
1016 (a->n_columns != b->n_columns)) {
1020 for (i = 0; i < a->n_columns; i++) {
1021 if ((a->columns[i].column != b->columns[i].column) ||
1022 (a->columns[i].select != b->columns[i].select)) {
1031 ovsdb_monitor_equal(const struct ovsdb_monitor *a,
1032 const struct ovsdb_monitor *b)
1034 struct shash_node *node;
1036 if (shash_count(&a->tables) != shash_count(&b->tables)) {
1040 SHASH_FOR_EACH(node, &a->tables) {
1041 const struct ovsdb_monitor_table *mta = node->data;
1042 const struct ovsdb_monitor_table *mtb;
1044 mtb = shash_find_data(&b->tables, node->name);
1049 if (!ovsdb_monitor_table_equal(mta, mtb)) {
1058 ovsdb_monitor_hash(const struct ovsdb_monitor *dbmon, size_t basis)
1060 const struct shash_node **nodes;
1063 nodes = shash_sort(&dbmon->tables);
1064 n = shash_count(&dbmon->tables);
1066 for (i = 0; i < n; i++) {
1067 struct ovsdb_monitor_table *mt = nodes[i]->data;
1069 basis = hash_pointer(mt->table, basis);
1070 basis = hash_3words(mt->select, mt->n_columns, basis);
1072 for (j = 0; j < mt->n_columns; j++) {
1073 basis = hash_pointer(mt->columns[j].column, basis);
1074 basis = hash_2words(mt->columns[j].select, basis);
1082 struct ovsdb_monitor *
1083 ovsdb_monitor_add(struct ovsdb_monitor *new_dbmon)
1085 struct ovsdb_monitor *dbmon;
1088 /* New_dbmon should be associated with only one jsonrpc
1090 ovs_assert(ovs_list_is_singleton(&new_dbmon->jsonrpc_monitors));
1092 ovsdb_monitor_columns_sort(new_dbmon);
1094 hash = ovsdb_monitor_hash(new_dbmon, 0);
1095 HMAP_FOR_EACH_WITH_HASH(dbmon, hmap_node, hash, &ovsdb_monitors) {
1096 if (ovsdb_monitor_equal(dbmon, new_dbmon)) {
1101 hmap_insert(&ovsdb_monitors, &new_dbmon->hmap_node, hash);
1106 ovsdb_monitor_destroy(struct ovsdb_monitor *dbmon)
1108 struct shash_node *node;
1110 ovs_list_remove(&dbmon->replica.node);
1112 if (!hmap_node_is_null(&dbmon->hmap_node)) {
1113 hmap_remove(&ovsdb_monitors, &dbmon->hmap_node);
1116 ovsdb_monitor_json_cache_flush(dbmon);
1117 hmap_destroy(&dbmon->json_cache);
1119 SHASH_FOR_EACH (node, &dbmon->tables) {
1120 struct ovsdb_monitor_table *mt = node->data;
1121 struct ovsdb_monitor_changes *changes, *next;
1123 HMAP_FOR_EACH_SAFE (changes, next, hmap_node, &mt->changes) {
1124 hmap_remove(&mt->changes, &changes->hmap_node);
1125 ovsdb_monitor_changes_destroy(changes);
1127 hmap_destroy(&mt->changes);
1129 free(mt->columns_index_map);
1132 shash_destroy(&dbmon->tables);
1136 static struct ovsdb_error *
1137 ovsdb_monitor_commit(struct ovsdb_replica *replica,
1138 const struct ovsdb_txn *txn,
1139 bool durable OVS_UNUSED)
1141 struct ovsdb_monitor *m = ovsdb_monitor_cast(replica);
1142 struct ovsdb_monitor_aux aux;
1144 ovsdb_monitor_init_aux(&aux, m);
1145 /* Update ovsdb_monitor's transaction number for
1146 * each transaction, before calling ovsdb_monitor_change_cb(). */
1147 m->n_transactions++;
1148 ovsdb_txn_for_each_change(txn, ovsdb_monitor_change_cb, &aux);
1150 switch(aux.efficacy) {
1151 case OVSDB_CHANGES_NO_EFFECT:
1152 /* The transaction is ignored by the monitor.
1153 * Roll back the 'n_transactions' as if the transaction
1154 * has never happened. */
1155 m->n_transactions--;
1157 case OVSDB_CHANGES_REQUIRE_INTERNAL_UPDATE:
1160 case OVSDB_CHANGES_REQUIRE_EXTERNAL_UPDATE:
1161 ovsdb_monitor_json_cache_flush(m);
1169 ovsdb_monitor_destroy_callback(struct ovsdb_replica *replica)
1171 struct ovsdb_monitor *dbmon = ovsdb_monitor_cast(replica);
1172 struct jsonrpc_monitor_node *jm, *next;
1174 /* Delete all front end monitors. Removing the last front
1175 * end monitor will also destroy the corresponding 'ovsdb_monitor'.
1176 * ovsdb monitor will also be destroied. */
1177 LIST_FOR_EACH_SAFE(jm, next, node, &dbmon->jsonrpc_monitors) {
1178 ovsdb_jsonrpc_monitor_destroy(jm->jsonrpc_monitor);
1182 /* Add some memory usage statics for monitors into 'usage', for use with
1183 * memory_report(). */
1185 ovsdb_monitor_get_memory_usage(struct simap *usage)
1187 struct ovsdb_monitor *dbmon;
1188 simap_put(usage, "monitors", hmap_count(&ovsdb_monitors));
1190 HMAP_FOR_EACH(dbmon, hmap_node, &ovsdb_monitors) {
1191 simap_increase(usage, "json-caches", hmap_count(&dbmon->json_cache));
1195 static const struct ovsdb_replica_class ovsdb_jsonrpc_replica_class = {
1196 ovsdb_monitor_commit,
1197 ovsdb_monitor_destroy_callback,