2 * Copyright (c) 2015, 2017 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"
24 #include "openvswitch/json.h"
26 #include "ovsdb-error.h"
27 #include "ovsdb-parser.h"
30 #include "condition.h"
36 #include "transaction.h"
37 #include "jsonrpc-server.h"
40 #include "openvswitch/vlog.h"
42 VLOG_DEFINE_THIS_MODULE(ovsdb_monitor
);
44 static const struct ovsdb_replica_class ovsdb_jsonrpc_replica_class
;
45 static struct hmap ovsdb_monitors
= HMAP_INITIALIZER(&ovsdb_monitors
);
47 /* Keep state of session's conditions */
48 struct ovsdb_monitor_session_condition
{
51 struct shash tables
; /* Contains
52 * "struct ovsdb_monitor_table_condition *"s. */
55 /* Monitored table session's conditions */
56 struct ovsdb_monitor_table_condition
{
57 const struct ovsdb_table
*table
;
58 struct ovsdb_monitor_table
*mt
;
59 struct ovsdb_condition old_condition
;
60 struct ovsdb_condition new_condition
;
65 * ovsdb_monitor keep track of the ovsdb changes.
68 /* A collection of tables being monitored. */
69 struct ovsdb_monitor
{
70 struct ovsdb_replica replica
;
71 struct shash tables
; /* Holds "struct ovsdb_monitor_table"s. */
72 struct ovs_list jsonrpc_monitors
; /* Contains "jsonrpc_monitor_node"s. */
74 uint64_t n_transactions
; /* Count number of committed transactions. */
75 struct hmap_node hmap_node
; /* Elements within ovsdb_monitors. */
76 struct hmap json_cache
; /* Contains "ovsdb_monitor_json_cache_node"s.*/
79 /* A json object of updates between 'from_txn' and 'dbmon->n_transactions'
81 struct ovsdb_monitor_json_cache_node
{
82 struct hmap_node hmap_node
; /* Elements in json cache. */
83 enum ovsdb_monitor_version version
;
85 struct json
*json
; /* Null, or a cloned of json */
88 struct jsonrpc_monitor_node
{
90 struct ovsdb_jsonrpc_monitor
*jsonrpc_monitor
;
93 /* A particular column being monitored. */
94 struct ovsdb_monitor_column
{
95 const struct ovsdb_column
*column
;
96 enum ovsdb_monitor_selection select
;
100 /* A row that has changed in a monitored table. */
101 struct ovsdb_monitor_row
{
102 struct hmap_node hmap_node
; /* In ovsdb_jsonrpc_monitor_table.changes. */
103 struct uuid uuid
; /* UUID of row that changed. */
104 struct ovsdb_datum
*old
; /* Old data, NULL for an inserted row. */
105 struct ovsdb_datum
*new; /* New data, NULL for a deleted row. */
108 /* Contains 'struct ovsdb_monitor_row's for rows that have been
109 * updated but not yet flushed to all the jsonrpc connection.
111 * 'n_refs' represent the number of jsonrpc connections that have
112 * not received updates. Generate the update for the last jsonprc
113 * connection will also destroy the whole "struct ovsdb_monitor_changes"
116 * 'transaction' stores the first update's transaction id.
118 struct ovsdb_monitor_changes
{
119 struct hmap_node hmap_node
; /* Element in ovsdb_monitor_tables' changes
121 struct ovsdb_monitor_table
*mt
;
124 uint64_t transaction
;
127 /* A particular table being monitored. */
128 struct ovsdb_monitor_table
{
129 const struct ovsdb_table
*table
;
131 /* This is the union (bitwise-OR) of the 'select' values in all of the
132 * members of 'columns' below. */
133 enum ovsdb_monitor_selection select
;
135 /* Columns being monitored. */
136 struct ovsdb_monitor_column
*columns
;
138 size_t n_monitored_columns
;
139 size_t allocated_columns
;
141 /* Columns in ovsdb_monitor_row have different indexes then in
142 * ovsdb_row. This field maps between column->index to the index in the
143 * ovsdb_monitor_row. It is used for condition evaluation. */
144 unsigned int *columns_index_map
;
146 /* Contains 'ovsdb_monitor_changes' indexed by 'transaction'. */
150 enum ovsdb_monitor_row_type
{
155 typedef struct json
*
156 (*compose_row_update_cb_func
)
157 (const struct ovsdb_monitor_table
*mt
,
158 const struct ovsdb_monitor_session_condition
* condition
,
159 enum ovsdb_monitor_row_type row_type
,
161 bool initial
, unsigned long int *changed
);
163 static void ovsdb_monitor_destroy(struct ovsdb_monitor
*dbmon
);
164 static struct ovsdb_monitor_changes
* ovsdb_monitor_table_add_changes(
165 struct ovsdb_monitor_table
*mt
, uint64_t next_txn
);
166 static struct ovsdb_monitor_changes
*ovsdb_monitor_table_find_changes(
167 struct ovsdb_monitor_table
*mt
, uint64_t unflushed
);
168 static void ovsdb_monitor_changes_destroy(
169 struct ovsdb_monitor_changes
*changes
);
170 static void ovsdb_monitor_table_track_changes(struct ovsdb_monitor_table
*mt
,
174 json_cache_hash(enum ovsdb_monitor_version version
, uint64_t from_txn
)
178 hash
= hash_uint64(version
);
179 hash
= hash_uint64_basis(from_txn
, hash
);
184 static struct ovsdb_monitor_json_cache_node
*
185 ovsdb_monitor_json_cache_search(const struct ovsdb_monitor
*dbmon
,
186 enum ovsdb_monitor_version version
,
189 struct ovsdb_monitor_json_cache_node
*node
;
190 uint32_t hash
= json_cache_hash(version
, from_txn
);
192 HMAP_FOR_EACH_WITH_HASH(node
, hmap_node
, hash
, &dbmon
->json_cache
) {
193 if (node
->from_txn
== from_txn
&& node
->version
== version
) {
202 ovsdb_monitor_json_cache_insert(struct ovsdb_monitor
*dbmon
,
203 enum ovsdb_monitor_version version
,
204 uint64_t from_txn
, struct json
*json
)
206 struct ovsdb_monitor_json_cache_node
*node
;
207 uint32_t hash
= json_cache_hash(version
, from_txn
);
209 node
= xmalloc(sizeof *node
);
211 node
->version
= version
;
212 node
->from_txn
= from_txn
;
213 node
->json
= json
? json_clone(json
) : NULL
;
215 hmap_insert(&dbmon
->json_cache
, &node
->hmap_node
, hash
);
219 ovsdb_monitor_json_cache_flush(struct ovsdb_monitor
*dbmon
)
221 struct ovsdb_monitor_json_cache_node
*node
;
223 HMAP_FOR_EACH_POP(node
, hmap_node
, &dbmon
->json_cache
) {
224 json_destroy(node
->json
);
230 compare_ovsdb_monitor_column(const void *a_
, const void *b_
)
232 const struct ovsdb_monitor_column
*a
= a_
;
233 const struct ovsdb_monitor_column
*b
= b_
;
235 /* put all monitored columns at the begining */
236 if (a
->monitored
!= b
->monitored
) {
237 return a
->monitored
? -1 : 1;
240 return a
->column
< b
->column
? -1 : a
->column
> b
->column
;
243 static struct ovsdb_monitor
*
244 ovsdb_monitor_cast(struct ovsdb_replica
*replica
)
246 ovs_assert(replica
->class == &ovsdb_jsonrpc_replica_class
);
247 return CONTAINER_OF(replica
, struct ovsdb_monitor
, replica
);
250 /* Finds and returns the ovsdb_monitor_row in 'mt->changes->rows' for the
251 * given 'uuid', or NULL if there is no such row. */
252 static struct ovsdb_monitor_row
*
253 ovsdb_monitor_changes_row_find(const struct ovsdb_monitor_changes
*changes
,
254 const struct uuid
*uuid
)
256 struct ovsdb_monitor_row
*row
;
258 HMAP_FOR_EACH_WITH_HASH (row
, hmap_node
, uuid_hash(uuid
),
260 if (uuid_equals(uuid
, &row
->uuid
)) {
267 /* Allocates an array of 'mt->n_columns' ovsdb_datums and initializes them as
268 * copies of the data in 'row' drawn from the columns represented by
269 * mt->columns[]. Returns the array.
271 * If 'row' is NULL, returns NULL. */
272 static struct ovsdb_datum
*
273 clone_monitor_row_data(const struct ovsdb_monitor_table
*mt
,
274 const struct ovsdb_row
*row
)
276 struct ovsdb_datum
*data
;
283 data
= xmalloc(mt
->n_columns
* sizeof *data
);
284 for (i
= 0; i
< mt
->n_columns
; i
++) {
285 const struct ovsdb_column
*c
= mt
->columns
[i
].column
;
286 const struct ovsdb_datum
*src
= &row
->fields
[c
->index
];
287 struct ovsdb_datum
*dst
= &data
[i
];
288 const struct ovsdb_type
*type
= &c
->type
;
290 ovsdb_datum_clone(dst
, src
, type
);
295 /* Replaces the mt->n_columns ovsdb_datums in row[] by copies of the data from
296 * in 'row' drawn from the columns represented by mt->columns[]. */
298 update_monitor_row_data(const struct ovsdb_monitor_table
*mt
,
299 const struct ovsdb_row
*row
,
300 struct ovsdb_datum
*data
)
304 for (i
= 0; i
< mt
->n_columns
; i
++) {
305 const struct ovsdb_column
*c
= mt
->columns
[i
].column
;
306 const struct ovsdb_datum
*src
= &row
->fields
[c
->index
];
307 struct ovsdb_datum
*dst
= &data
[i
];
308 const struct ovsdb_type
*type
= &c
->type
;
310 if (!ovsdb_datum_equals(src
, dst
, type
)) {
311 ovsdb_datum_destroy(dst
, type
);
312 ovsdb_datum_clone(dst
, src
, type
);
317 /* Frees all of the mt->n_columns ovsdb_datums in data[], using the types taken
318 * from mt->columns[], plus 'data' itself. */
320 free_monitor_row_data(const struct ovsdb_monitor_table
*mt
,
321 struct ovsdb_datum
*data
)
326 for (i
= 0; i
< mt
->n_columns
; i
++) {
327 const struct ovsdb_column
*c
= mt
->columns
[i
].column
;
329 ovsdb_datum_destroy(&data
[i
], &c
->type
);
335 /* Frees 'row', which must have been created from 'mt'. */
337 ovsdb_monitor_row_destroy(const struct ovsdb_monitor_table
*mt
,
338 struct ovsdb_monitor_row
*row
)
341 free_monitor_row_data(mt
, row
->old
);
342 free_monitor_row_data(mt
, row
->new);
348 ovsdb_monitor_columns_sort(struct ovsdb_monitor
*dbmon
)
351 struct shash_node
*node
;
353 SHASH_FOR_EACH (node
, &dbmon
->tables
) {
354 struct ovsdb_monitor_table
*mt
= node
->data
;
356 qsort(mt
->columns
, mt
->n_columns
, sizeof *mt
->columns
,
357 compare_ovsdb_monitor_column
);
358 for (i
= 0; i
< mt
->n_columns
; i
++) {
359 /* re-set index map due to sort */
360 mt
->columns_index_map
[mt
->columns
[i
].column
->index
] = i
;
366 ovsdb_monitor_add_jsonrpc_monitor(struct ovsdb_monitor
*dbmon
,
367 struct ovsdb_jsonrpc_monitor
*jsonrpc_monitor
)
369 struct jsonrpc_monitor_node
*jm
;
371 jm
= xzalloc(sizeof *jm
);
372 jm
->jsonrpc_monitor
= jsonrpc_monitor
;
373 ovs_list_push_back(&dbmon
->jsonrpc_monitors
, &jm
->node
);
376 struct ovsdb_monitor
*
377 ovsdb_monitor_create(struct ovsdb
*db
,
378 struct ovsdb_jsonrpc_monitor
*jsonrpc_monitor
)
380 struct ovsdb_monitor
*dbmon
;
382 dbmon
= xzalloc(sizeof *dbmon
);
384 ovsdb_replica_init(&dbmon
->replica
, &ovsdb_jsonrpc_replica_class
);
385 ovsdb_add_replica(db
, &dbmon
->replica
);
386 ovs_list_init(&dbmon
->jsonrpc_monitors
);
388 dbmon
->n_transactions
= 0;
389 shash_init(&dbmon
->tables
);
390 hmap_node_nullify(&dbmon
->hmap_node
);
391 hmap_init(&dbmon
->json_cache
);
393 ovsdb_monitor_add_jsonrpc_monitor(dbmon
, jsonrpc_monitor
);
398 ovsdb_monitor_add_table(struct ovsdb_monitor
*m
,
399 const struct ovsdb_table
*table
)
401 struct ovsdb_monitor_table
*mt
;
403 size_t n_columns
= shash_count(&table
->schema
->columns
);
405 mt
= xzalloc(sizeof *mt
);
407 shash_add(&m
->tables
, table
->schema
->name
, mt
);
408 hmap_init(&mt
->changes
);
409 mt
->columns_index_map
=
410 xmalloc(sizeof *mt
->columns_index_map
* n_columns
);
411 for (i
= 0; i
< n_columns
; i
++) {
412 mt
->columns_index_map
[i
] = -1;
417 ovsdb_monitor_add_column(struct ovsdb_monitor
*dbmon
,
418 const struct ovsdb_table
*table
,
419 const struct ovsdb_column
*column
,
420 enum ovsdb_monitor_selection select
,
423 struct ovsdb_monitor_table
*mt
;
424 struct ovsdb_monitor_column
*c
;
426 mt
= shash_find_data(&dbmon
->tables
, table
->schema
->name
);
428 /* Check for column duplication. Return duplicated column name. */
429 if (mt
->columns_index_map
[column
->index
] != -1) {
433 if (mt
->n_columns
>= mt
->allocated_columns
) {
434 mt
->columns
= x2nrealloc(mt
->columns
, &mt
->allocated_columns
,
435 sizeof *mt
->columns
);
438 mt
->select
|= select
;
439 mt
->columns_index_map
[column
->index
] = mt
->n_columns
;
440 c
= &mt
->columns
[mt
->n_columns
++];
443 c
->monitored
= monitored
;
445 mt
->n_monitored_columns
++;
452 ovsdb_monitor_condition_add_columns(struct ovsdb_monitor
*dbmon
,
453 const struct ovsdb_table
*table
,
454 struct ovsdb_condition
*condition
)
458 const struct ovsdb_column
**columns
=
459 ovsdb_condition_get_columns(condition
, &n_columns
);
461 for (i
= 0; i
< n_columns
; i
++) {
462 ovsdb_monitor_add_column(dbmon
, table
, columns
[i
],
469 /* Bind this session's condition to ovsdb_monitor */
471 ovsdb_monitor_condition_bind(struct ovsdb_monitor
*dbmon
,
472 struct ovsdb_monitor_session_condition
*cond
)
474 struct shash_node
*node
;
476 SHASH_FOR_EACH(node
, &cond
->tables
) {
477 struct ovsdb_monitor_table_condition
*mtc
= node
->data
;
478 struct ovsdb_monitor_table
*mt
=
479 shash_find_data(&dbmon
->tables
, mtc
->table
->schema
->name
);
482 ovsdb_monitor_condition_add_columns(dbmon
, mtc
->table
,
483 &mtc
->new_condition
);
488 ovsdb_monitor_table_exists(struct ovsdb_monitor
*m
,
489 const struct ovsdb_table
*table
)
491 return shash_find_data(&m
->tables
, table
->schema
->name
);
494 static struct ovsdb_monitor_changes
*
495 ovsdb_monitor_table_add_changes(struct ovsdb_monitor_table
*mt
,
498 struct ovsdb_monitor_changes
*changes
;
500 changes
= xzalloc(sizeof *changes
);
502 changes
->transaction
= next_txn
;
505 hmap_init(&changes
->rows
);
506 hmap_insert(&mt
->changes
, &changes
->hmap_node
, hash_uint64(next_txn
));
511 static struct ovsdb_monitor_changes
*
512 ovsdb_monitor_table_find_changes(struct ovsdb_monitor_table
*mt
,
513 uint64_t transaction
)
515 struct ovsdb_monitor_changes
*changes
;
516 size_t hash
= hash_uint64(transaction
);
518 HMAP_FOR_EACH_WITH_HASH(changes
, hmap_node
, hash
, &mt
->changes
) {
519 if (changes
->transaction
== transaction
) {
527 /* Stop currently tracking changes to table 'mt' since 'transaction'. */
529 ovsdb_monitor_table_untrack_changes(struct ovsdb_monitor_table
*mt
,
530 uint64_t transaction
)
532 struct ovsdb_monitor_changes
*changes
=
533 ovsdb_monitor_table_find_changes(mt
, transaction
);
535 if (--changes
->n_refs
== 0) {
536 hmap_remove(&mt
->changes
, &changes
->hmap_node
);
537 ovsdb_monitor_changes_destroy(changes
);
542 /* Start tracking changes to table 'mt' begins from 'transaction' inclusive.
545 ovsdb_monitor_table_track_changes(struct ovsdb_monitor_table
*mt
,
546 uint64_t transaction
)
548 struct ovsdb_monitor_changes
*changes
;
550 changes
= ovsdb_monitor_table_find_changes(mt
, transaction
);
554 ovsdb_monitor_table_add_changes(mt
, transaction
);
559 ovsdb_monitor_changes_destroy(struct ovsdb_monitor_changes
*changes
)
561 struct ovsdb_monitor_row
*row
, *next
;
563 HMAP_FOR_EACH_SAFE (row
, next
, hmap_node
, &changes
->rows
) {
564 hmap_remove(&changes
->rows
, &row
->hmap_node
);
565 ovsdb_monitor_row_destroy(changes
->mt
, row
);
567 hmap_destroy(&changes
->rows
);
571 static enum ovsdb_monitor_selection
572 ovsdb_monitor_row_update_type(bool initial
, const bool old
, const bool new)
574 return initial
? OJMS_INITIAL
580 /* Set conditional monitoring mode only if we have non-empty condition in one
581 * of the tables at least */
583 ovsdb_monitor_session_condition_set_mode(
584 struct ovsdb_monitor_session_condition
*cond
)
586 cond
->conditional
= shash_count(&cond
->tables
) !=
590 /* Returnes an empty allocated session's condition state holder */
591 struct ovsdb_monitor_session_condition
*
592 ovsdb_monitor_session_condition_create(void)
594 struct ovsdb_monitor_session_condition
*condition
=
595 xzalloc(sizeof *condition
);
597 condition
->conditional
= false;
598 shash_init(&condition
->tables
);
603 ovsdb_monitor_session_condition_destroy(
604 struct ovsdb_monitor_session_condition
*condition
)
606 struct shash_node
*node
, *next
;
612 SHASH_FOR_EACH_SAFE (node
, next
, &condition
->tables
) {
613 struct ovsdb_monitor_table_condition
*mtc
= node
->data
;
615 ovsdb_condition_destroy(&mtc
->new_condition
);
616 ovsdb_condition_destroy(&mtc
->old_condition
);
617 shash_delete(&condition
->tables
, node
);
620 shash_destroy(&condition
->tables
);
625 ovsdb_monitor_table_condition_create(
626 struct ovsdb_monitor_session_condition
*condition
,
627 const struct ovsdb_table
*table
,
628 const struct json
*json_cnd
)
630 struct ovsdb_monitor_table_condition
*mtc
;
631 struct ovsdb_error
*error
;
633 mtc
= xzalloc(sizeof *mtc
);
635 ovsdb_condition_init(&mtc
->old_condition
);
636 ovsdb_condition_init(&mtc
->new_condition
);
639 error
= ovsdb_condition_from_json(table
->schema
,
642 &mtc
->old_condition
);
649 shash_add(&condition
->tables
, table
->schema
->name
, mtc
);
650 /* On session startup old == new condition */
651 ovsdb_condition_clone(&mtc
->new_condition
, &mtc
->old_condition
);
652 if (ovsdb_condition_is_true(&mtc
->old_condition
)) {
653 condition
->n_true_cnd
++;
654 ovsdb_monitor_session_condition_set_mode(condition
);
661 ovsdb_monitor_get_table_conditions(
662 const struct ovsdb_monitor_table
*mt
,
663 const struct ovsdb_monitor_session_condition
*condition
,
664 struct ovsdb_condition
**old_condition
,
665 struct ovsdb_condition
**new_condition
)
671 struct ovsdb_monitor_table_condition
*mtc
=
672 shash_find_data(&condition
->tables
, mt
->table
->schema
->name
);
677 *old_condition
= &mtc
->old_condition
;
678 *new_condition
= &mtc
->new_condition
;
684 ovsdb_monitor_table_condition_update(
685 struct ovsdb_monitor
*dbmon
,
686 struct ovsdb_monitor_session_condition
*condition
,
687 const struct ovsdb_table
*table
,
688 const struct json
*cond_json
)
694 struct ovsdb_monitor_table_condition
*mtc
=
695 shash_find_data(&condition
->tables
, table
->schema
->name
);
696 struct ovsdb_error
*error
;
697 struct ovsdb_condition cond
= OVSDB_CONDITION_INITIALIZER(&cond
);
699 error
= ovsdb_condition_from_json(table
->schema
, cond_json
,
704 ovsdb_condition_destroy(&mtc
->new_condition
);
705 ovsdb_condition_clone(&mtc
->new_condition
, &cond
);
706 ovsdb_condition_destroy(&cond
);
707 ovsdb_monitor_condition_add_columns(dbmon
,
709 &mtc
->new_condition
);
715 ovsdb_monitor_table_condition_updated(struct ovsdb_monitor_table
*mt
,
716 struct ovsdb_monitor_session_condition
*condition
)
718 struct ovsdb_monitor_table_condition
*mtc
=
719 shash_find_data(&condition
->tables
, mt
->table
->schema
->name
);
722 /* If conditional monitoring - set old condition to new condition */
723 if (ovsdb_condition_cmp_3way(&mtc
->old_condition
,
724 &mtc
->new_condition
)) {
725 if (ovsdb_condition_is_true(&mtc
->new_condition
)) {
726 if (!ovsdb_condition_is_true(&mtc
->old_condition
)) {
727 condition
->n_true_cnd
++;
730 if (ovsdb_condition_is_true(&mtc
->old_condition
)) {
731 condition
->n_true_cnd
--;
734 ovsdb_condition_destroy(&mtc
->old_condition
);
735 ovsdb_condition_clone(&mtc
->old_condition
, &mtc
->new_condition
);
736 ovsdb_monitor_session_condition_set_mode(condition
);
741 static enum ovsdb_monitor_selection
742 ovsdb_monitor_row_update_type_condition(
743 const struct ovsdb_monitor_table
*mt
,
744 const struct ovsdb_monitor_session_condition
*condition
,
746 enum ovsdb_monitor_row_type row_type
,
747 const struct ovsdb_datum
*old
,
748 const struct ovsdb_datum
*new)
750 struct ovsdb_condition
*old_condition
, *new_condition
;
751 enum ovsdb_monitor_selection type
=
752 ovsdb_monitor_row_update_type(initial
, old
, new);
754 if (ovsdb_monitor_get_table_conditions(mt
,
758 bool old_cond
= !old
? false
759 : ovsdb_condition_empty_or_match_any(old
,
761 row_type
== OVSDB_MONITOR_ROW
?
762 mt
->columns_index_map
:
764 bool new_cond
= !new ? false
765 : ovsdb_condition_empty_or_match_any(new,
767 row_type
== OVSDB_MONITOR_ROW
?
768 mt
->columns_index_map
:
771 if (!old_cond
&& !new_cond
) {
783 type
= !old_cond
? OJMS_INSERT
: !new_cond
784 ? OJMS_DELETE
: OJMS_MODIFY
;
799 ovsdb_monitor_row_skip_update(const struct ovsdb_monitor_table
*mt
,
800 enum ovsdb_monitor_row_type row_type
,
801 const struct ovsdb_datum
*old
,
802 const struct ovsdb_datum
*new,
803 enum ovsdb_monitor_selection type
,
804 unsigned long int *changed
)
806 if (!(mt
->select
& type
)) {
810 if (type
== OJMS_MODIFY
) {
814 memset(changed
, 0, bitmap_n_bytes(mt
->n_columns
));
815 for (i
= 0; i
< mt
->n_columns
; i
++) {
816 const struct ovsdb_column
*c
= mt
->columns
[i
].column
;
817 size_t index
= row_type
== OVSDB_ROW
? c
->index
: i
;
818 if (!ovsdb_datum_equals(&old
[index
], &new[index
], &c
->type
)) {
819 bitmap_set1(changed
, i
);
824 /* No actual changes: presumably a row changed and then
825 * changed back later. */
833 /* Returns JSON for a <row-update> (as described in RFC 7047) for 'row' within
834 * 'mt', or NULL if no row update should be sent.
836 * The caller should specify 'initial' as true if the returned JSON is going to
837 * be used as part of the initial reply to a "monitor" request, false if it is
838 * going to be used as part of an "update" notification.
840 * 'changed' must be a scratch buffer for internal use that is at least
841 * bitmap_n_bytes(mt->n_columns) bytes long. */
843 ovsdb_monitor_compose_row_update(
844 const struct ovsdb_monitor_table
*mt
,
845 const struct ovsdb_monitor_session_condition
*condition OVS_UNUSED
,
846 enum ovsdb_monitor_row_type row_type OVS_UNUSED
,
848 bool initial
, unsigned long int *changed
)
850 const struct ovsdb_monitor_row
*row
= _row
;
851 enum ovsdb_monitor_selection type
;
852 struct json
*old_json
, *new_json
;
853 struct json
*row_json
;
856 ovs_assert(row_type
== OVSDB_MONITOR_ROW
);
857 type
= ovsdb_monitor_row_update_type(initial
, row
->old
, row
->new);
858 if (ovsdb_monitor_row_skip_update(mt
, row_type
, row
->old
,
859 row
->new, type
, changed
)) {
863 row_json
= json_object_create();
864 old_json
= new_json
= NULL
;
865 if (type
& (OJMS_DELETE
| OJMS_MODIFY
)) {
866 old_json
= json_object_create();
867 json_object_put(row_json
, "old", old_json
);
869 if (type
& (OJMS_INITIAL
| OJMS_INSERT
| OJMS_MODIFY
)) {
870 new_json
= json_object_create();
871 json_object_put(row_json
, "new", new_json
);
873 for (i
= 0; i
< mt
->n_monitored_columns
; i
++) {
874 const struct ovsdb_monitor_column
*c
= &mt
->columns
[i
];
876 if (!c
->monitored
|| !(type
& c
->select
)) {
877 /* We don't care about this type of change for this
878 * particular column (but we will care about it for some
883 if ((type
== OJMS_MODIFY
&& bitmap_is_set(changed
, i
))
884 || type
== OJMS_DELETE
) {
885 json_object_put(old_json
, c
->column
->name
,
886 ovsdb_datum_to_json(&row
->old
[i
],
889 if (type
& (OJMS_INITIAL
| OJMS_INSERT
| OJMS_MODIFY
)) {
890 json_object_put(new_json
, c
->column
->name
,
891 ovsdb_datum_to_json(&row
->new[i
],
899 /* Returns JSON for a <row-update2> (as described in ovsdb-server(1) mapage)
900 * for 'row' within * 'mt', or NULL if no row update should be sent.
902 * The caller should specify 'initial' as true if the returned JSON is
903 * going to be used as part of the initial reply to a "monitor_cond" request,
904 * false if it is going to be used as part of an "update2" notification.
906 * 'changed' must be a scratch buffer for internal use that is at least
907 * bitmap_n_bytes(mt->n_columns) bytes long. */
909 ovsdb_monitor_compose_row_update2(
910 const struct ovsdb_monitor_table
*mt
,
911 const struct ovsdb_monitor_session_condition
*condition
,
912 enum ovsdb_monitor_row_type row_type
,
914 bool initial
, unsigned long int *changed
)
916 enum ovsdb_monitor_selection type
;
917 struct json
*row_update2
, *diff_json
;
918 const struct ovsdb_datum
*old
, *new;
921 if (row_type
== OVSDB_MONITOR_ROW
) {
922 old
= ((const struct ovsdb_monitor_row
*)_row
)->old
;;
923 new = ((const struct ovsdb_monitor_row
*)_row
)->new;
925 old
= new = ((const struct ovsdb_row
*)_row
)->fields
;
928 type
= ovsdb_monitor_row_update_type_condition(mt
, condition
, initial
,
930 if (ovsdb_monitor_row_skip_update(mt
, row_type
, old
, new, type
, changed
)) {
934 row_update2
= json_object_create();
935 if (type
== OJMS_DELETE
) {
936 json_object_put(row_update2
, "delete", json_null_create());
938 diff_json
= json_object_create();
941 for (i
= 0; i
< mt
->n_monitored_columns
; i
++) {
942 const struct ovsdb_monitor_column
*c
= &mt
->columns
[i
];
943 size_t index
= row_type
== OVSDB_ROW
? c
->column
->index
: i
;
944 if (!c
->monitored
|| !(type
& c
->select
)) {
945 /* We don't care about this type of change for this
946 * particular column (but we will care about it for some
951 if (type
== OJMS_MODIFY
) {
952 struct ovsdb_datum diff
;
954 if (!bitmap_is_set(changed
, i
)) {
958 ovsdb_datum_diff(&diff
,&old
[index
], &new[index
],
960 json_object_put(diff_json
, c
->column
->name
,
961 ovsdb_datum_to_json(&diff
, &c
->column
->type
));
962 ovsdb_datum_destroy(&diff
, &c
->column
->type
);
964 if (!ovsdb_datum_is_default(&new[index
], &c
->column
->type
)) {
965 json_object_put(diff_json
, c
->column
->name
,
966 ovsdb_datum_to_json(&new[index
],
972 op
= type
== OJMS_INITIAL
? "initial"
973 : type
== OJMS_MODIFY
? "modify" : "insert";
974 json_object_put(row_update2
, op
, diff_json
);
981 ovsdb_monitor_max_columns(struct ovsdb_monitor
*dbmon
)
983 struct shash_node
*node
;
984 size_t max_columns
= 0;
986 SHASH_FOR_EACH (node
, &dbmon
->tables
) {
987 struct ovsdb_monitor_table
*mt
= node
->data
;
989 max_columns
= MAX(max_columns
, mt
->n_columns
);
996 ovsdb_monitor_add_json_row(struct json
**json
, const char *table_name
,
997 struct json
**table_json
, struct json
*row_json
,
998 const struct uuid
*row_uuid
)
1000 char uuid
[UUID_LEN
+ 1];
1002 /* Create JSON object for transaction overall. */
1004 *json
= json_object_create();
1007 /* Create JSON object for transaction on this table. */
1009 *table_json
= json_object_create();
1010 json_object_put(*json
, table_name
, *table_json
);
1013 /* Add JSON row to JSON table. */
1014 snprintf(uuid
, sizeof uuid
, UUID_FMT
, UUID_ARGS(row_uuid
));
1015 json_object_put(*table_json
, uuid
, row_json
);
1018 /* Constructs and returns JSON for a <table-updates> object (as described in
1019 * RFC 7047) for all the outstanding changes within 'monitor', starting from
1022 ovsdb_monitor_compose_update(
1023 struct ovsdb_monitor
*dbmon
,
1024 bool initial
, uint64_t transaction
,
1025 const struct ovsdb_monitor_session_condition
*condition
,
1026 compose_row_update_cb_func row_update
)
1028 struct shash_node
*node
;
1030 size_t max_columns
= ovsdb_monitor_max_columns(dbmon
);
1031 unsigned long int *changed
= xmalloc(bitmap_n_bytes(max_columns
));
1034 SHASH_FOR_EACH (node
, &dbmon
->tables
) {
1035 struct ovsdb_monitor_table
*mt
= node
->data
;
1036 struct ovsdb_monitor_row
*row
, *next
;
1037 struct ovsdb_monitor_changes
*changes
;
1038 struct json
*table_json
= NULL
;
1040 changes
= ovsdb_monitor_table_find_changes(mt
, transaction
);
1045 HMAP_FOR_EACH_SAFE (row
, next
, hmap_node
, &changes
->rows
) {
1046 struct json
*row_json
;
1047 row_json
= (*row_update
)(mt
, condition
, OVSDB_MONITOR_ROW
, row
,
1050 ovsdb_monitor_add_json_row(&json
, mt
->table
->schema
->name
,
1051 &table_json
, row_json
,
1062 ovsdb_monitor_compose_cond_change_update(
1063 struct ovsdb_monitor
*dbmon
,
1064 struct ovsdb_monitor_session_condition
*condition
)
1066 struct shash_node
*node
;
1067 struct json
*json
= NULL
;
1068 size_t max_columns
= ovsdb_monitor_max_columns(dbmon
);
1069 unsigned long int *changed
= xmalloc(bitmap_n_bytes(max_columns
));
1071 SHASH_FOR_EACH (node
, &dbmon
->tables
) {
1072 struct ovsdb_monitor_table
*mt
= node
->data
;
1073 struct ovsdb_row
*row
;
1074 struct json
*table_json
= NULL
;
1075 struct ovsdb_condition
*old_condition
, *new_condition
;
1077 if (!ovsdb_monitor_get_table_conditions(mt
,
1081 !ovsdb_condition_cmp_3way(old_condition
, new_condition
)) {
1082 /* Nothing to update on this table */
1086 /* Iterate over all rows in table */
1087 HMAP_FOR_EACH (row
, hmap_node
, &mt
->table
->rows
) {
1088 struct json
*row_json
;
1090 row_json
= ovsdb_monitor_compose_row_update2(mt
, condition
,
1094 ovsdb_monitor_add_json_row(&json
, mt
->table
->schema
->name
,
1095 &table_json
, row_json
,
1096 ovsdb_row_get_uuid(row
));
1099 ovsdb_monitor_table_condition_updated(mt
, condition
);
1106 /* Returns JSON for a <table-updates> object (as described in RFC 7047)
1107 * for all the outstanding changes within 'monitor' that starts from
1109 * If cond_updated is true all rows in the db that match conditions will be
1112 * The caller should specify 'initial' as true if the returned JSON is going to
1113 * be used as part of the initial reply to a "monitor" request, false if it is
1114 * going to be used as part of an "update" notification. */
1116 ovsdb_monitor_get_update(
1117 struct ovsdb_monitor
*dbmon
,
1118 bool initial
, bool cond_updated
,
1119 uint64_t *unflushed_
,
1120 struct ovsdb_monitor_session_condition
*condition
,
1121 enum ovsdb_monitor_version version
)
1123 struct ovsdb_monitor_json_cache_node
*cache_node
= NULL
;
1124 struct shash_node
*node
;
1126 const uint64_t unflushed
= *unflushed_
;
1127 const uint64_t next_unflushed
= dbmon
->n_transactions
+ 1;
1129 ovs_assert(cond_updated
? unflushed
== next_unflushed
: true);
1131 /* Return a clone of cached json if one exists. Otherwise,
1132 * generate a new one and add it to the cache. */
1133 if (!condition
|| (!condition
->conditional
&& !cond_updated
)) {
1134 cache_node
= ovsdb_monitor_json_cache_search(dbmon
, version
,
1138 json
= cache_node
->json
? json_clone(cache_node
->json
) : NULL
;
1140 if (version
== OVSDB_MONITOR_V1
) {
1142 ovsdb_monitor_compose_update(dbmon
, initial
, unflushed
,
1144 ovsdb_monitor_compose_row_update
);
1146 ovs_assert(version
== OVSDB_MONITOR_V2
);
1147 if (!cond_updated
) {
1148 json
= ovsdb_monitor_compose_update(dbmon
, initial
, unflushed
,
1150 ovsdb_monitor_compose_row_update2
);
1152 if (!condition
|| !condition
->conditional
) {
1153 ovsdb_monitor_json_cache_insert(dbmon
, version
, unflushed
,
1157 /* Compose update on whole db due to condition update.
1158 Session must be flushed (change list is empty)*/
1160 ovsdb_monitor_compose_cond_change_update(dbmon
, condition
);
1165 /* Maintain transaction id of 'changes'. */
1166 SHASH_FOR_EACH (node
, &dbmon
->tables
) {
1167 struct ovsdb_monitor_table
*mt
= node
->data
;
1169 ovsdb_monitor_table_untrack_changes(mt
, unflushed
);
1170 ovsdb_monitor_table_track_changes(mt
, next_unflushed
);
1172 *unflushed_
= next_unflushed
;
1178 ovsdb_monitor_needs_flush(struct ovsdb_monitor
*dbmon
,
1179 uint64_t next_transaction
)
1181 ovs_assert(next_transaction
<= dbmon
->n_transactions
+ 1);
1182 return (next_transaction
<= dbmon
->n_transactions
);
1186 ovsdb_monitor_table_add_select(struct ovsdb_monitor
*dbmon
,
1187 const struct ovsdb_table
*table
,
1188 enum ovsdb_monitor_selection select
)
1190 struct ovsdb_monitor_table
* mt
;
1192 mt
= shash_find_data(&dbmon
->tables
, table
->schema
->name
);
1193 mt
->select
|= select
;
1197 * If a row's change type (insert, delete or modify) matches that of
1198 * the monitor, they should be sent to the monitor's clients as updates.
1199 * Of cause, the monitor should also internally update with this change.
1201 * When a change type does not require client side update, the monitor
1202 * may still need to keep track of certain changes in order to generate
1203 * correct future updates. For example, the monitor internal state should
1204 * be updated whenever a new row is inserted, in order to generate the
1205 * correct initial state, regardless if a insert change type is being
1208 * On the other hand, if a transaction only contains changes to columns
1209 * that are not monitored, this transaction can be safely ignored by the
1212 * Thus, the order of the declaration is important:
1213 * 'OVSDB_CHANGES_REQUIRE_EXTERNAL_UPDATE' always implies
1214 * 'OVSDB_CHANGES_REQUIRE_INTERNAL_UPDATE', but not vice versa. */
1215 enum ovsdb_monitor_changes_efficacy
{
1216 OVSDB_CHANGES_NO_EFFECT
, /* Monitor does not care about this
1218 OVSDB_CHANGES_REQUIRE_INTERNAL_UPDATE
, /* Monitor internal updates. */
1219 OVSDB_CHANGES_REQUIRE_EXTERNAL_UPDATE
, /* Client needs to be updated. */
1222 struct ovsdb_monitor_aux
{
1223 const struct ovsdb_monitor
*monitor
;
1224 struct ovsdb_monitor_table
*mt
;
1225 enum ovsdb_monitor_changes_efficacy efficacy
;
1229 ovsdb_monitor_init_aux(struct ovsdb_monitor_aux
*aux
,
1230 const struct ovsdb_monitor
*m
)
1234 aux
->efficacy
= OVSDB_CHANGES_NO_EFFECT
;
1238 ovsdb_monitor_changes_update(const struct ovsdb_row
*old
,
1239 const struct ovsdb_row
*new,
1240 const struct ovsdb_monitor_table
*mt
,
1241 struct ovsdb_monitor_changes
*changes
)
1243 const struct uuid
*uuid
= ovsdb_row_get_uuid(new ? new : old
);
1244 struct ovsdb_monitor_row
*change
;
1246 change
= ovsdb_monitor_changes_row_find(changes
, uuid
);
1248 change
= xzalloc(sizeof *change
);
1249 hmap_insert(&changes
->rows
, &change
->hmap_node
, uuid_hash(uuid
));
1250 change
->uuid
= *uuid
;
1251 change
->old
= clone_monitor_row_data(mt
, old
);
1252 change
->new = clone_monitor_row_data(mt
, new);
1256 /* Reinsert the row that was just deleted.
1258 * This path won't be hit without replication. Whenever OVSDB
1259 * server inserts a new row, It always generates a new UUID
1260 * that is different from the row just deleted.
1262 * With replication, this path can be hit in a corner
1263 * case when two OVSDB servers are set up to replicate
1264 * each other. Not that is a useful set up, but can
1265 * happen in practice.
1267 * An example of how this path can be hit is documented below.
1268 * The details is not as important to the correctness of the
1269 * logic, but added here to convince ourselves that this path
1272 * Imagine two OVSDB servers that replicates from each
1273 * other. For each replication session, there is a
1274 * corresponding monitor at the other end of the replication
1275 * JSONRPC connection.
1277 * The events can lead to a back to back deletion and
1278 * insertion operation of the same row for the monitor of
1279 * the first server are:
1281 * 1. A row is inserted in the first OVSDB server.
1282 * 2. The row is then replicated to the remote OVSDB server.
1283 * 3. The row is now deleted by the local OVSDB server. This
1284 * deletion operation is replicated to the local monitor
1285 * of the OVSDB server.
1286 * 4. The monitor now receives the same row, as an insertion,
1287 * from the replication server. Because of
1288 * replication, the row carries the same UUID as the row
1291 change
->new = clone_monitor_row_data(mt
, new);
1293 update_monitor_row_data(mt
, new, change
->new);
1296 free_monitor_row_data(mt
, change
->new);
1300 /* This row was added then deleted. Forget about it. */
1301 hmap_remove(&changes
->rows
, &change
->hmap_node
);
1309 ovsdb_monitor_columns_changed(const struct ovsdb_monitor_table
*mt
,
1310 const unsigned long int *changed
)
1314 for (i
= 0; i
< mt
->n_columns
; i
++) {
1315 size_t column_index
= mt
->columns
[i
].column
->index
;
1317 if (bitmap_is_set(changed
, column_index
)) {
1325 /* Return the efficacy of a row's change to a monitor table.
1327 * Please see the block comment above 'ovsdb_monitor_changes_efficacy'
1328 * definition form more information. */
1329 static enum ovsdb_monitor_changes_efficacy
1330 ovsdb_monitor_changes_classify(enum ovsdb_monitor_selection type
,
1331 const struct ovsdb_monitor_table
*mt
,
1332 const unsigned long int *changed
)
1334 if (type
== OJMS_MODIFY
&&
1335 !ovsdb_monitor_columns_changed(mt
, changed
)) {
1336 return OVSDB_CHANGES_NO_EFFECT
;
1339 if (type
== OJMS_MODIFY
) {
1340 /* Condition might turn a modify operation to insert or delete */
1341 type
|= OJMS_INSERT
| OJMS_DELETE
;
1344 return (mt
->select
& type
)
1345 ? OVSDB_CHANGES_REQUIRE_EXTERNAL_UPDATE
1346 : OVSDB_CHANGES_REQUIRE_INTERNAL_UPDATE
;
1350 ovsdb_monitor_change_cb(const struct ovsdb_row
*old
,
1351 const struct ovsdb_row
*new,
1352 const unsigned long int *changed
,
1355 struct ovsdb_monitor_aux
*aux
= aux_
;
1356 const struct ovsdb_monitor
*m
= aux
->monitor
;
1357 struct ovsdb_table
*table
= new ? new->table
: old
->table
;
1358 struct ovsdb_monitor_table
*mt
;
1359 struct ovsdb_monitor_changes
*changes
;
1361 if (!aux
->mt
|| table
!= aux
->mt
->table
) {
1362 aux
->mt
= shash_find_data(&m
->tables
, table
->schema
->name
);
1364 /* We don't care about rows in this table at all. Tell the caller
1371 enum ovsdb_monitor_selection type
=
1372 ovsdb_monitor_row_update_type(false, old
, new);
1373 enum ovsdb_monitor_changes_efficacy efficacy
=
1374 ovsdb_monitor_changes_classify(type
, mt
, changed
);
1376 HMAP_FOR_EACH(changes
, hmap_node
, &mt
->changes
) {
1377 if (efficacy
> OVSDB_CHANGES_NO_EFFECT
) {
1378 ovsdb_monitor_changes_update(old
, new, mt
, changes
);
1381 if (aux
->efficacy
< efficacy
) {
1382 aux
->efficacy
= efficacy
;
1389 ovsdb_monitor_get_initial(const struct ovsdb_monitor
*dbmon
)
1391 struct shash_node
*node
;
1393 SHASH_FOR_EACH (node
, &dbmon
->tables
) {
1394 struct ovsdb_monitor_table
*mt
= node
->data
;
1396 if (mt
->select
& OJMS_INITIAL
) {
1397 struct ovsdb_row
*row
;
1398 struct ovsdb_monitor_changes
*changes
;
1400 changes
= ovsdb_monitor_table_find_changes(mt
, 0);
1402 changes
= ovsdb_monitor_table_add_changes(mt
, 0);
1403 HMAP_FOR_EACH (row
, hmap_node
, &mt
->table
->rows
) {
1404 ovsdb_monitor_changes_update(NULL
, row
, mt
, changes
);
1414 ovsdb_monitor_remove_jsonrpc_monitor(struct ovsdb_monitor
*dbmon
,
1415 struct ovsdb_jsonrpc_monitor
*jsonrpc_monitor
,
1418 struct jsonrpc_monitor_node
*jm
;
1420 if (ovs_list_is_empty(&dbmon
->jsonrpc_monitors
)) {
1421 ovsdb_monitor_destroy(dbmon
);
1425 /* Find and remove the jsonrpc monitor from the list. */
1426 LIST_FOR_EACH(jm
, node
, &dbmon
->jsonrpc_monitors
) {
1427 if (jm
->jsonrpc_monitor
== jsonrpc_monitor
) {
1428 /* Release the tracked changes. */
1429 struct shash_node
*node
;
1430 SHASH_FOR_EACH (node
, &dbmon
->tables
) {
1431 struct ovsdb_monitor_table
*mt
= node
->data
;
1432 ovsdb_monitor_table_untrack_changes(mt
, unflushed
);
1434 ovs_list_remove(&jm
->node
);
1437 /* Destroy ovsdb monitor if this is the last user. */
1438 if (ovs_list_is_empty(&dbmon
->jsonrpc_monitors
)) {
1439 ovsdb_monitor_destroy(dbmon
);
1446 /* Should never reach here. jsonrpc_monitor should be on the list. */
1451 ovsdb_monitor_table_equal(const struct ovsdb_monitor_table
*a
,
1452 const struct ovsdb_monitor_table
*b
)
1456 ovs_assert(b
->n_columns
== b
->n_monitored_columns
);
1458 if ((a
->table
!= b
->table
) ||
1459 (a
->select
!= b
->select
) ||
1460 (a
->n_monitored_columns
!= b
->n_monitored_columns
)) {
1464 /* Compare only monitored columns that must be sorted already */
1465 for (i
= 0; i
< a
->n_monitored_columns
; i
++) {
1466 if ((a
->columns
[i
].column
!= b
->columns
[i
].column
) ||
1467 (a
->columns
[i
].select
!= b
->columns
[i
].select
)) {
1475 ovsdb_monitor_equal(const struct ovsdb_monitor
*a
,
1476 const struct ovsdb_monitor
*b
)
1478 struct shash_node
*node
;
1480 if (shash_count(&a
->tables
) != shash_count(&b
->tables
)) {
1484 SHASH_FOR_EACH(node
, &a
->tables
) {
1485 const struct ovsdb_monitor_table
*mta
= node
->data
;
1486 const struct ovsdb_monitor_table
*mtb
;
1488 mtb
= shash_find_data(&b
->tables
, node
->name
);
1493 if (!ovsdb_monitor_table_equal(mta
, mtb
)) {
1502 ovsdb_monitor_hash(const struct ovsdb_monitor
*dbmon
, size_t basis
)
1504 const struct shash_node
**nodes
;
1507 nodes
= shash_sort(&dbmon
->tables
);
1508 n
= shash_count(&dbmon
->tables
);
1510 for (i
= 0; i
< n
; i
++) {
1511 struct ovsdb_monitor_table
*mt
= nodes
[i
]->data
;
1513 basis
= hash_pointer(mt
->table
, basis
);
1514 basis
= hash_3words(mt
->select
, mt
->n_columns
, basis
);
1516 for (j
= 0; j
< mt
->n_columns
; j
++) {
1517 basis
= hash_pointer(mt
->columns
[j
].column
, basis
);
1518 basis
= hash_2words(mt
->columns
[j
].select
, basis
);
1526 struct ovsdb_monitor
*
1527 ovsdb_monitor_add(struct ovsdb_monitor
*new_dbmon
)
1529 struct ovsdb_monitor
*dbmon
;
1532 /* New_dbmon should be associated with only one jsonrpc
1534 ovs_assert(ovs_list_is_singleton(&new_dbmon
->jsonrpc_monitors
));
1536 ovsdb_monitor_columns_sort(new_dbmon
);
1538 hash
= ovsdb_monitor_hash(new_dbmon
, 0);
1539 HMAP_FOR_EACH_WITH_HASH(dbmon
, hmap_node
, hash
, &ovsdb_monitors
) {
1540 if (ovsdb_monitor_equal(dbmon
, new_dbmon
)) {
1545 hmap_insert(&ovsdb_monitors
, &new_dbmon
->hmap_node
, hash
);
1550 ovsdb_monitor_destroy(struct ovsdb_monitor
*dbmon
)
1552 struct shash_node
*node
;
1554 ovs_list_remove(&dbmon
->replica
.node
);
1556 if (!hmap_node_is_null(&dbmon
->hmap_node
)) {
1557 hmap_remove(&ovsdb_monitors
, &dbmon
->hmap_node
);
1560 ovsdb_monitor_json_cache_flush(dbmon
);
1561 hmap_destroy(&dbmon
->json_cache
);
1563 SHASH_FOR_EACH (node
, &dbmon
->tables
) {
1564 struct ovsdb_monitor_table
*mt
= node
->data
;
1565 struct ovsdb_monitor_changes
*changes
, *next
;
1567 HMAP_FOR_EACH_SAFE (changes
, next
, hmap_node
, &mt
->changes
) {
1568 hmap_remove(&mt
->changes
, &changes
->hmap_node
);
1569 ovsdb_monitor_changes_destroy(changes
);
1571 hmap_destroy(&mt
->changes
);
1573 free(mt
->columns_index_map
);
1576 shash_destroy(&dbmon
->tables
);
1580 static struct ovsdb_error
*
1581 ovsdb_monitor_commit(struct ovsdb_replica
*replica
,
1582 const struct ovsdb_txn
*txn
,
1583 bool durable OVS_UNUSED
)
1585 struct ovsdb_monitor
*m
= ovsdb_monitor_cast(replica
);
1586 struct ovsdb_monitor_aux aux
;
1588 ovsdb_monitor_init_aux(&aux
, m
);
1589 /* Update ovsdb_monitor's transaction number for
1590 * each transaction, before calling ovsdb_monitor_change_cb(). */
1591 m
->n_transactions
++;
1592 ovsdb_txn_for_each_change(txn
, ovsdb_monitor_change_cb
, &aux
);
1594 switch(aux
.efficacy
) {
1595 case OVSDB_CHANGES_NO_EFFECT
:
1596 /* The transaction is ignored by the monitor.
1597 * Roll back the 'n_transactions' as if the transaction
1598 * has never happened. */
1599 m
->n_transactions
--;
1601 case OVSDB_CHANGES_REQUIRE_INTERNAL_UPDATE
:
1604 case OVSDB_CHANGES_REQUIRE_EXTERNAL_UPDATE
:
1605 ovsdb_monitor_json_cache_flush(m
);
1613 ovsdb_monitor_destroy_callback(struct ovsdb_replica
*replica
)
1615 struct ovsdb_monitor
*dbmon
= ovsdb_monitor_cast(replica
);
1616 struct jsonrpc_monitor_node
*jm
, *next
;
1618 /* Delete all front end monitors. Removing the last front
1619 * end monitor will also destroy the corresponding 'ovsdb_monitor'.
1620 * ovsdb monitor will also be destroied. */
1621 LIST_FOR_EACH_SAFE(jm
, next
, node
, &dbmon
->jsonrpc_monitors
) {
1622 ovsdb_jsonrpc_monitor_destroy(jm
->jsonrpc_monitor
);
1626 /* Add some memory usage statics for monitors into 'usage', for use with
1627 * memory_report(). */
1629 ovsdb_monitor_get_memory_usage(struct simap
*usage
)
1631 struct ovsdb_monitor
*dbmon
;
1632 simap_put(usage
, "monitors", hmap_count(&ovsdb_monitors
));
1634 HMAP_FOR_EACH(dbmon
, hmap_node
, &ovsdb_monitors
) {
1635 simap_increase(usage
, "json-caches", hmap_count(&dbmon
->json_cache
));
1639 static const struct ovsdb_replica_class ovsdb_jsonrpc_replica_class
= {
1640 ovsdb_monitor_commit
,
1641 ovsdb_monitor_destroy_callback
,