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 struct hmap ovsdb_monitors
= HMAP_INITIALIZER(&ovsdb_monitors
);
46 /* Keep state of session's conditions */
47 struct ovsdb_monitor_session_condition
{
48 bool conditional
; /* True iff every table's condition is true. */
49 struct shash tables
; /* Contains
50 * "struct ovsdb_monitor_table_condition *"s. */
53 /* Monitored table session's conditions */
54 struct ovsdb_monitor_table_condition
{
55 const struct ovsdb_table
*table
;
56 struct ovsdb_monitor_table
*mt
;
57 struct ovsdb_condition old_condition
;
58 struct ovsdb_condition new_condition
;
63 * ovsdb_monitor keep track of the ovsdb changes.
66 /* A collection of tables being monitored. */
67 struct ovsdb_monitor
{
68 struct ovs_list list_node
; /* In struct ovsdb's "monitors" list. */
69 struct shash tables
; /* Holds "struct ovsdb_monitor_table"s. */
70 struct ovs_list jsonrpc_monitors
; /* Contains "jsonrpc_monitor_node"s. */
72 uint64_t n_transactions
; /* Count number of committed transactions. */
73 struct hmap_node hmap_node
; /* Elements within ovsdb_monitors. */
74 struct hmap json_cache
; /* Contains "ovsdb_monitor_json_cache_node"s.*/
77 /* A json object of updates between 'from_txn' and 'dbmon->n_transactions'
79 struct ovsdb_monitor_json_cache_node
{
80 struct hmap_node hmap_node
; /* Elements in json cache. */
81 enum ovsdb_monitor_version version
;
83 struct json
*json
; /* Null, or a cloned of json */
86 struct jsonrpc_monitor_node
{
88 struct ovsdb_jsonrpc_monitor
*jsonrpc_monitor
;
91 /* A particular column being monitored. */
92 struct ovsdb_monitor_column
{
93 const struct ovsdb_column
*column
;
94 enum ovsdb_monitor_selection select
;
98 /* A row that has changed in a monitored table. */
99 struct ovsdb_monitor_row
{
100 struct hmap_node hmap_node
; /* In ovsdb_jsonrpc_monitor_table.changes. */
101 struct uuid uuid
; /* UUID of row that changed. */
102 struct ovsdb_datum
*old
; /* Old data, NULL for an inserted row. */
103 struct ovsdb_datum
*new; /* New data, NULL for a deleted row. */
106 /* Contains 'struct ovsdb_monitor_row's for rows that have been
107 * updated but not yet flushed to all the jsonrpc connection.
109 * 'n_refs' represent the number of jsonrpc connections that have
110 * not received updates. Generate the update for the last jsonprc
111 * connection will also destroy the whole "struct ovsdb_monitor_changes"
114 * 'transaction' stores the first update's transaction id.
116 struct ovsdb_monitor_changes
{
117 struct hmap_node hmap_node
; /* Element in ovsdb_monitor_tables' changes
119 struct ovsdb_monitor_table
*mt
;
122 uint64_t transaction
;
124 /* Save the mt->n_columns that is used when creating the changes.
125 * It can be different from the current mt->n_columns because
126 * mt->n_columns can be increased when there are condition changes
127 * from any of the clients sharing the dbmon. */
131 /* A particular table being monitored. */
132 struct ovsdb_monitor_table
{
133 const struct ovsdb_table
*table
;
135 /* This is the union (bitwise-OR) of the 'select' values in all of the
136 * members of 'columns' below. */
137 enum ovsdb_monitor_selection select
;
139 /* Columns being monitored. */
140 struct ovsdb_monitor_column
*columns
;
142 size_t n_monitored_columns
;
143 size_t allocated_columns
;
145 /* Columns in ovsdb_monitor_row have different indexes then in
146 * ovsdb_row. This field maps between column->index to the index in the
147 * ovsdb_monitor_row. It is used for condition evaluation. */
148 unsigned int *columns_index_map
;
150 /* Contains 'ovsdb_monitor_changes' indexed by 'transaction'. */
154 enum ovsdb_monitor_row_type
{
159 typedef struct json
*
160 (*compose_row_update_cb_func
)
161 (const struct ovsdb_monitor_table
*mt
,
162 const struct ovsdb_monitor_session_condition
* condition
,
163 enum ovsdb_monitor_row_type row_type
,
165 bool initial
, unsigned long int *changed
,
168 static void ovsdb_monitor_destroy(struct ovsdb_monitor
*dbmon
);
169 static struct ovsdb_monitor_changes
* ovsdb_monitor_table_add_changes(
170 struct ovsdb_monitor_table
*mt
, uint64_t next_txn
);
171 static struct ovsdb_monitor_changes
*ovsdb_monitor_table_find_changes(
172 struct ovsdb_monitor_table
*mt
, uint64_t unflushed
);
173 static void ovsdb_monitor_changes_destroy(
174 struct ovsdb_monitor_changes
*changes
);
175 static void ovsdb_monitor_table_track_changes(struct ovsdb_monitor_table
*mt
,
179 json_cache_hash(enum ovsdb_monitor_version version
, uint64_t from_txn
)
183 hash
= hash_uint64(version
);
184 hash
= hash_uint64_basis(from_txn
, hash
);
189 static struct ovsdb_monitor_json_cache_node
*
190 ovsdb_monitor_json_cache_search(const struct ovsdb_monitor
*dbmon
,
191 enum ovsdb_monitor_version version
,
194 struct ovsdb_monitor_json_cache_node
*node
;
195 uint32_t hash
= json_cache_hash(version
, from_txn
);
197 HMAP_FOR_EACH_WITH_HASH(node
, hmap_node
, hash
, &dbmon
->json_cache
) {
198 if (node
->from_txn
== from_txn
&& node
->version
== version
) {
207 ovsdb_monitor_json_cache_insert(struct ovsdb_monitor
*dbmon
,
208 enum ovsdb_monitor_version version
,
209 uint64_t from_txn
, struct json
*json
)
211 struct ovsdb_monitor_json_cache_node
*node
;
212 uint32_t hash
= json_cache_hash(version
, from_txn
);
214 node
= xmalloc(sizeof *node
);
216 node
->version
= version
;
217 node
->from_txn
= from_txn
;
218 node
->json
= json
? json_clone(json
) : NULL
;
220 hmap_insert(&dbmon
->json_cache
, &node
->hmap_node
, hash
);
224 ovsdb_monitor_json_cache_flush(struct ovsdb_monitor
*dbmon
)
226 struct ovsdb_monitor_json_cache_node
*node
;
228 HMAP_FOR_EACH_POP(node
, hmap_node
, &dbmon
->json_cache
) {
229 json_destroy(node
->json
);
235 compare_ovsdb_monitor_column(const void *a_
, const void *b_
)
237 const struct ovsdb_monitor_column
*a
= a_
;
238 const struct ovsdb_monitor_column
*b
= b_
;
240 /* put all monitored columns at the begining */
241 if (a
->monitored
!= b
->monitored
) {
242 return a
->monitored
? -1 : 1;
245 return a
->column
< b
->column
? -1 : a
->column
> b
->column
;
248 /* Finds and returns the ovsdb_monitor_row in 'mt->changes->rows' for the
249 * given 'uuid', or NULL if there is no such row. */
250 static struct ovsdb_monitor_row
*
251 ovsdb_monitor_changes_row_find(const struct ovsdb_monitor_changes
*changes
,
252 const struct uuid
*uuid
)
254 struct ovsdb_monitor_row
*row
;
256 HMAP_FOR_EACH_WITH_HASH (row
, hmap_node
, uuid_hash(uuid
),
258 if (uuid_equals(uuid
, &row
->uuid
)) {
265 /* Allocates an array of 'n_columns' ovsdb_datums and initializes them as
266 * copies of the data in 'row' drawn from the columns represented by
267 * mt->columns[]. Returns the array.
269 * If 'row' is NULL, returns NULL. */
270 static struct ovsdb_datum
*
271 clone_monitor_row_data(const struct ovsdb_monitor_table
*mt
,
272 const struct ovsdb_row
*row
,
275 struct ovsdb_datum
*data
;
282 data
= xmalloc(n_columns
* sizeof *data
);
283 for (i
= 0; i
< n_columns
; i
++) {
284 const struct ovsdb_column
*c
= mt
->columns
[i
].column
;
285 const struct ovsdb_datum
*src
= &row
->fields
[c
->index
];
286 struct ovsdb_datum
*dst
= &data
[i
];
287 const struct ovsdb_type
*type
= &c
->type
;
289 ovsdb_datum_clone(dst
, src
, type
);
294 /* Replaces the n_columns ovsdb_datums in row[] by copies of the data from
295 * in 'row' drawn from the columns represented by mt->columns[]. */
297 update_monitor_row_data(const struct ovsdb_monitor_table
*mt
,
298 const struct ovsdb_row
*row
,
299 struct ovsdb_datum
*data
,
304 for (i
= 0; i
< n_columns
; i
++) {
305 const struct ovsdb_column
*c
= mt
->columns
[i
].column
;
306 const struct ovsdb_datum
*src
= &row
->fields
[c
->index
];
307 struct ovsdb_datum
*dst
= &data
[i
];
308 const struct ovsdb_type
*type
= &c
->type
;
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 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
,
327 for (i
= 0; i
< n_columns
; i
++) {
328 const struct ovsdb_column
*c
= mt
->columns
[i
].column
;
330 ovsdb_datum_destroy(&data
[i
], &c
->type
);
336 /* Frees 'row', which must have been created from 'mt'. */
338 ovsdb_monitor_row_destroy(const struct ovsdb_monitor_table
*mt
,
339 struct ovsdb_monitor_row
*row
,
343 free_monitor_row_data(mt
, row
->old
, n_columns
);
344 free_monitor_row_data(mt
, row
->new, n_columns
);
350 ovsdb_monitor_columns_sort(struct ovsdb_monitor
*dbmon
)
353 struct shash_node
*node
;
355 SHASH_FOR_EACH (node
, &dbmon
->tables
) {
356 struct ovsdb_monitor_table
*mt
= node
->data
;
358 qsort(mt
->columns
, mt
->n_columns
, sizeof *mt
->columns
,
359 compare_ovsdb_monitor_column
);
360 for (i
= 0; i
< mt
->n_columns
; i
++) {
361 /* re-set index map due to sort */
362 mt
->columns_index_map
[mt
->columns
[i
].column
->index
] = i
;
368 ovsdb_monitor_add_jsonrpc_monitor(struct ovsdb_monitor
*dbmon
,
369 struct ovsdb_jsonrpc_monitor
*jsonrpc_monitor
)
371 struct jsonrpc_monitor_node
*jm
;
373 jm
= xzalloc(sizeof *jm
);
374 jm
->jsonrpc_monitor
= jsonrpc_monitor
;
375 ovs_list_push_back(&dbmon
->jsonrpc_monitors
, &jm
->node
);
378 struct ovsdb_monitor
*
379 ovsdb_monitor_create(struct ovsdb
*db
,
380 struct ovsdb_jsonrpc_monitor
*jsonrpc_monitor
)
382 struct ovsdb_monitor
*dbmon
;
384 dbmon
= xzalloc(sizeof *dbmon
);
386 ovs_list_push_back(&db
->monitors
, &dbmon
->list_node
);
387 ovs_list_init(&dbmon
->jsonrpc_monitors
);
389 dbmon
->n_transactions
= 0;
390 shash_init(&dbmon
->tables
);
391 hmap_node_nullify(&dbmon
->hmap_node
);
392 hmap_init(&dbmon
->json_cache
);
394 ovsdb_monitor_add_jsonrpc_monitor(dbmon
, jsonrpc_monitor
);
399 ovsdb_monitor_add_table(struct ovsdb_monitor
*m
,
400 const struct ovsdb_table
*table
)
402 struct ovsdb_monitor_table
*mt
;
404 size_t n_columns
= shash_count(&table
->schema
->columns
);
406 mt
= xzalloc(sizeof *mt
);
408 shash_add(&m
->tables
, table
->schema
->name
, mt
);
409 hmap_init(&mt
->changes
);
410 mt
->columns_index_map
=
411 xmalloc(sizeof *mt
->columns_index_map
* n_columns
);
412 for (i
= 0; i
< n_columns
; i
++) {
413 mt
->columns_index_map
[i
] = -1;
418 ovsdb_monitor_add_column(struct ovsdb_monitor
*dbmon
,
419 const struct ovsdb_table
*table
,
420 const struct ovsdb_column
*column
,
421 enum ovsdb_monitor_selection select
,
424 struct ovsdb_monitor_table
*mt
;
425 struct ovsdb_monitor_column
*c
;
427 mt
= shash_find_data(&dbmon
->tables
, table
->schema
->name
);
429 /* Check for column duplication. Return duplicated column name. */
430 if (mt
->columns_index_map
[column
->index
] != -1) {
434 if (mt
->n_columns
>= mt
->allocated_columns
) {
435 mt
->columns
= x2nrealloc(mt
->columns
, &mt
->allocated_columns
,
436 sizeof *mt
->columns
);
439 mt
->select
|= select
;
440 mt
->columns_index_map
[column
->index
] = mt
->n_columns
;
441 c
= &mt
->columns
[mt
->n_columns
++];
444 c
->monitored
= monitored
;
446 mt
->n_monitored_columns
++;
453 ovsdb_monitor_condition_add_columns(struct ovsdb_monitor
*dbmon
,
454 const struct ovsdb_table
*table
,
455 struct ovsdb_condition
*condition
)
459 const struct ovsdb_column
**columns
=
460 ovsdb_condition_get_columns(condition
, &n_columns
);
462 for (i
= 0; i
< n_columns
; i
++) {
463 ovsdb_monitor_add_column(dbmon
, table
, columns
[i
],
470 /* Bind this session's condition to ovsdb_monitor */
472 ovsdb_monitor_condition_bind(struct ovsdb_monitor
*dbmon
,
473 struct ovsdb_monitor_session_condition
*cond
)
475 struct shash_node
*node
;
477 SHASH_FOR_EACH(node
, &cond
->tables
) {
478 struct ovsdb_monitor_table_condition
*mtc
= node
->data
;
479 struct ovsdb_monitor_table
*mt
=
480 shash_find_data(&dbmon
->tables
, mtc
->table
->schema
->name
);
483 ovsdb_monitor_condition_add_columns(dbmon
, mtc
->table
,
484 &mtc
->new_condition
);
489 ovsdb_monitor_table_exists(struct ovsdb_monitor
*m
,
490 const struct ovsdb_table
*table
)
492 return shash_find_data(&m
->tables
, table
->schema
->name
);
495 static struct ovsdb_monitor_changes
*
496 ovsdb_monitor_table_add_changes(struct ovsdb_monitor_table
*mt
,
499 struct ovsdb_monitor_changes
*changes
;
501 changes
= xzalloc(sizeof *changes
);
503 changes
->transaction
= next_txn
;
506 changes
->n_columns
= mt
->n_columns
;
507 hmap_init(&changes
->rows
);
508 hmap_insert(&mt
->changes
, &changes
->hmap_node
, hash_uint64(next_txn
));
513 static struct ovsdb_monitor_changes
*
514 ovsdb_monitor_table_find_changes(struct ovsdb_monitor_table
*mt
,
515 uint64_t transaction
)
517 struct ovsdb_monitor_changes
*changes
;
518 size_t hash
= hash_uint64(transaction
);
520 HMAP_FOR_EACH_WITH_HASH(changes
, hmap_node
, hash
, &mt
->changes
) {
521 if (changes
->transaction
== transaction
) {
529 /* Stop currently tracking changes to table 'mt' since 'transaction'. */
531 ovsdb_monitor_table_untrack_changes(struct ovsdb_monitor_table
*mt
,
532 uint64_t transaction
)
534 struct ovsdb_monitor_changes
*changes
=
535 ovsdb_monitor_table_find_changes(mt
, transaction
);
537 if (--changes
->n_refs
== 0) {
538 hmap_remove(&mt
->changes
, &changes
->hmap_node
);
539 ovsdb_monitor_changes_destroy(changes
);
544 /* Start tracking changes to table 'mt' begins from 'transaction' inclusive.
547 ovsdb_monitor_table_track_changes(struct ovsdb_monitor_table
*mt
,
548 uint64_t transaction
)
550 struct ovsdb_monitor_changes
*changes
;
552 changes
= ovsdb_monitor_table_find_changes(mt
, transaction
);
556 ovsdb_monitor_table_add_changes(mt
, transaction
);
561 ovsdb_monitor_changes_destroy(struct ovsdb_monitor_changes
*changes
)
563 struct ovsdb_monitor_row
*row
, *next
;
565 HMAP_FOR_EACH_SAFE (row
, next
, hmap_node
, &changes
->rows
) {
566 hmap_remove(&changes
->rows
, &row
->hmap_node
);
567 ovsdb_monitor_row_destroy(changes
->mt
, row
, changes
->n_columns
);
569 hmap_destroy(&changes
->rows
);
573 static enum ovsdb_monitor_selection
574 ovsdb_monitor_row_update_type(bool initial
, const bool old
, const bool new)
576 return initial
? OJMS_INITIAL
582 /* Set conditional monitoring mode only if we have non-empty condition in one
583 * of the tables at least */
585 ovsdb_monitor_session_condition_set_mode(
586 struct ovsdb_monitor_session_condition
*cond
)
588 struct shash_node
*node
;
590 SHASH_FOR_EACH (node
, &cond
->tables
) {
591 struct ovsdb_monitor_table_condition
*mtc
= node
->data
;
593 if (!ovsdb_condition_is_true(&mtc
->new_condition
)) {
594 cond
->conditional
= true;
598 cond
->conditional
= false;
601 /* Returnes an empty allocated session's condition state holder */
602 struct ovsdb_monitor_session_condition
*
603 ovsdb_monitor_session_condition_create(void)
605 struct ovsdb_monitor_session_condition
*condition
=
606 xzalloc(sizeof *condition
);
608 condition
->conditional
= false;
609 shash_init(&condition
->tables
);
614 ovsdb_monitor_session_condition_destroy(
615 struct ovsdb_monitor_session_condition
*condition
)
617 struct shash_node
*node
, *next
;
623 SHASH_FOR_EACH_SAFE (node
, next
, &condition
->tables
) {
624 struct ovsdb_monitor_table_condition
*mtc
= node
->data
;
626 ovsdb_condition_destroy(&mtc
->new_condition
);
627 ovsdb_condition_destroy(&mtc
->old_condition
);
628 shash_delete(&condition
->tables
, node
);
631 shash_destroy(&condition
->tables
);
636 ovsdb_monitor_table_condition_create(
637 struct ovsdb_monitor_session_condition
*condition
,
638 const struct ovsdb_table
*table
,
639 const struct json
*json_cnd
)
641 struct ovsdb_monitor_table_condition
*mtc
;
642 struct ovsdb_error
*error
;
644 mtc
= xzalloc(sizeof *mtc
);
646 ovsdb_condition_init(&mtc
->old_condition
);
647 ovsdb_condition_init(&mtc
->new_condition
);
650 error
= ovsdb_condition_from_json(table
->schema
,
653 &mtc
->old_condition
);
660 shash_add(&condition
->tables
, table
->schema
->name
, mtc
);
661 /* On session startup old == new condition */
662 ovsdb_condition_clone(&mtc
->new_condition
, &mtc
->old_condition
);
663 ovsdb_monitor_session_condition_set_mode(condition
);
669 ovsdb_monitor_get_table_conditions(
670 const struct ovsdb_monitor_table
*mt
,
671 const struct ovsdb_monitor_session_condition
*condition
,
672 struct ovsdb_condition
**old_condition
,
673 struct ovsdb_condition
**new_condition
)
679 struct ovsdb_monitor_table_condition
*mtc
=
680 shash_find_data(&condition
->tables
, mt
->table
->schema
->name
);
685 *old_condition
= &mtc
->old_condition
;
686 *new_condition
= &mtc
->new_condition
;
692 ovsdb_monitor_table_condition_update(
693 struct ovsdb_monitor
*dbmon
,
694 struct ovsdb_monitor_session_condition
*condition
,
695 const struct ovsdb_table
*table
,
696 const struct json
*cond_json
)
702 struct ovsdb_monitor_table_condition
*mtc
=
703 shash_find_data(&condition
->tables
, table
->schema
->name
);
704 struct ovsdb_error
*error
;
705 struct ovsdb_condition cond
= OVSDB_CONDITION_INITIALIZER(&cond
);
707 error
= ovsdb_condition_from_json(table
->schema
, cond_json
,
712 ovsdb_condition_destroy(&mtc
->new_condition
);
713 ovsdb_condition_clone(&mtc
->new_condition
, &cond
);
714 ovsdb_condition_destroy(&cond
);
715 ovsdb_monitor_condition_add_columns(dbmon
,
717 &mtc
->new_condition
);
723 ovsdb_monitor_table_condition_updated(struct ovsdb_monitor_table
*mt
,
724 struct ovsdb_monitor_session_condition
*condition
)
726 struct ovsdb_monitor_table_condition
*mtc
=
727 shash_find_data(&condition
->tables
, mt
->table
->schema
->name
);
730 /* If conditional monitoring - set old condition to new condition */
731 if (ovsdb_condition_cmp_3way(&mtc
->old_condition
,
732 &mtc
->new_condition
)) {
733 ovsdb_condition_destroy(&mtc
->old_condition
);
734 ovsdb_condition_clone(&mtc
->old_condition
, &mtc
->new_condition
);
735 ovsdb_monitor_session_condition_set_mode(condition
);
740 static enum ovsdb_monitor_selection
741 ovsdb_monitor_row_update_type_condition(
742 const struct ovsdb_monitor_table
*mt
,
743 const struct ovsdb_monitor_session_condition
*condition
,
745 enum ovsdb_monitor_row_type row_type
,
746 const struct ovsdb_datum
*old
,
747 const struct ovsdb_datum
*new)
749 struct ovsdb_condition
*old_condition
, *new_condition
;
750 enum ovsdb_monitor_selection type
=
751 ovsdb_monitor_row_update_type(initial
, old
, new);
753 if (ovsdb_monitor_get_table_conditions(mt
,
757 bool old_cond
= !old
? false
758 : ovsdb_condition_empty_or_match_any(old
,
760 row_type
== OVSDB_MONITOR_ROW
?
761 mt
->columns_index_map
:
763 bool new_cond
= !new ? false
764 : ovsdb_condition_empty_or_match_any(new,
766 row_type
== OVSDB_MONITOR_ROW
?
767 mt
->columns_index_map
:
770 if (!old_cond
&& !new_cond
) {
782 type
= !old_cond
? OJMS_INSERT
: !new_cond
783 ? OJMS_DELETE
: OJMS_MODIFY
;
798 ovsdb_monitor_row_skip_update(const struct ovsdb_monitor_table
*mt
,
799 enum ovsdb_monitor_row_type row_type
,
800 const struct ovsdb_datum
*old
,
801 const struct ovsdb_datum
*new,
802 enum ovsdb_monitor_selection type
,
803 unsigned long int *changed
,
806 if (!(mt
->select
& type
)) {
810 if (type
== OJMS_MODIFY
) {
814 memset(changed
, 0, bitmap_n_bytes(n_columns
));
815 for (i
= 0; i
< n_columns
; i
++) {
816 const struct ovsdb_column
*c
= mt
->columns
[i
].column
;
817 size_t index
= row_type
== OVSDB_ROW
? c
->index
: i
;
818 if (!ovsdb_datum_equals(&old
[index
], &new[index
], &c
->type
)) {
819 bitmap_set1(changed
, i
);
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(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
,
849 size_t n_columns OVS_UNUSED
)
851 const struct ovsdb_monitor_row
*row
= _row
;
852 enum ovsdb_monitor_selection type
;
853 struct json
*old_json
, *new_json
;
854 struct json
*row_json
;
857 ovs_assert(row_type
== OVSDB_MONITOR_ROW
);
858 type
= ovsdb_monitor_row_update_type(initial
, row
->old
, row
->new);
859 if (ovsdb_monitor_row_skip_update(mt
, row_type
, row
->old
,
860 row
->new, type
, changed
,
865 row_json
= json_object_create();
866 old_json
= new_json
= NULL
;
867 if (type
& (OJMS_DELETE
| OJMS_MODIFY
)) {
868 old_json
= json_object_create();
869 json_object_put(row_json
, "old", old_json
);
871 if (type
& (OJMS_INITIAL
| OJMS_INSERT
| OJMS_MODIFY
)) {
872 new_json
= json_object_create();
873 json_object_put(row_json
, "new", new_json
);
875 for (i
= 0; i
< mt
->n_monitored_columns
; i
++) {
876 const struct ovsdb_monitor_column
*c
= &mt
->columns
[i
];
878 if (!c
->monitored
|| !(type
& c
->select
)) {
879 /* We don't care about this type of change for this
880 * particular column (but we will care about it for some
885 if ((type
== OJMS_MODIFY
&& bitmap_is_set(changed
, i
))
886 || type
== OJMS_DELETE
) {
887 json_object_put(old_json
, c
->column
->name
,
888 ovsdb_datum_to_json(&row
->old
[i
],
891 if (type
& (OJMS_INITIAL
| OJMS_INSERT
| OJMS_MODIFY
)) {
892 json_object_put(new_json
, c
->column
->name
,
893 ovsdb_datum_to_json(&row
->new[i
],
901 /* Returns JSON for a <row-update2> (as described in ovsdb-server(1) mapage)
902 * for 'row' within * 'mt', or NULL if no row update should be sent.
904 * The caller should specify 'initial' as true if the returned JSON is
905 * going to be used as part of the initial reply to a "monitor_cond" request,
906 * false if it is going to be used as part of an "update2" notification.
908 * 'changed' must be a scratch buffer for internal use that is at least
909 * bitmap_n_bytes(n_columns) bytes long. */
911 ovsdb_monitor_compose_row_update2(
912 const struct ovsdb_monitor_table
*mt
,
913 const struct ovsdb_monitor_session_condition
*condition
,
914 enum ovsdb_monitor_row_type row_type
,
916 bool initial
, unsigned long int *changed
,
919 enum ovsdb_monitor_selection type
;
920 struct json
*row_update2
, *diff_json
;
921 const struct ovsdb_datum
*old
, *new;
924 if (row_type
== OVSDB_MONITOR_ROW
) {
925 old
= ((const struct ovsdb_monitor_row
*)_row
)->old
;;
926 new = ((const struct ovsdb_monitor_row
*)_row
)->new;
928 old
= new = ((const struct ovsdb_row
*)_row
)->fields
;
931 type
= ovsdb_monitor_row_update_type_condition(mt
, condition
, initial
,
933 if (ovsdb_monitor_row_skip_update(mt
, row_type
, old
, new, type
, changed
,
938 row_update2
= json_object_create();
939 if (type
== OJMS_DELETE
) {
940 json_object_put(row_update2
, "delete", json_null_create());
942 diff_json
= json_object_create();
945 for (i
= 0; i
< mt
->n_monitored_columns
; i
++) {
946 const struct ovsdb_monitor_column
*c
= &mt
->columns
[i
];
947 size_t index
= row_type
== OVSDB_ROW
? c
->column
->index
: i
;
948 if (!c
->monitored
|| !(type
& c
->select
)) {
949 /* We don't care about this type of change for this
950 * particular column (but we will care about it for some
955 if (type
== OJMS_MODIFY
) {
956 struct ovsdb_datum diff
;
958 if (!bitmap_is_set(changed
, i
)) {
962 ovsdb_datum_diff(&diff
,&old
[index
], &new[index
],
964 json_object_put(diff_json
, c
->column
->name
,
965 ovsdb_datum_to_json(&diff
, &c
->column
->type
));
966 ovsdb_datum_destroy(&diff
, &c
->column
->type
);
968 if (!ovsdb_datum_is_default(&new[index
], &c
->column
->type
)) {
969 json_object_put(diff_json
, c
->column
->name
,
970 ovsdb_datum_to_json(&new[index
],
976 op
= type
== OJMS_INITIAL
? "initial"
977 : type
== OJMS_MODIFY
? "modify" : "insert";
978 json_object_put(row_update2
, op
, diff_json
);
985 ovsdb_monitor_max_columns(struct ovsdb_monitor
*dbmon
)
987 struct shash_node
*node
;
988 size_t max_columns
= 0;
990 SHASH_FOR_EACH (node
, &dbmon
->tables
) {
991 struct ovsdb_monitor_table
*mt
= node
->data
;
993 max_columns
= MAX(max_columns
, mt
->n_columns
);
1000 ovsdb_monitor_add_json_row(struct json
**json
, const char *table_name
,
1001 struct json
**table_json
, struct json
*row_json
,
1002 const struct uuid
*row_uuid
)
1004 char uuid
[UUID_LEN
+ 1];
1006 /* Create JSON object for transaction overall. */
1008 *json
= json_object_create();
1011 /* Create JSON object for transaction on this table. */
1013 *table_json
= json_object_create();
1014 json_object_put(*json
, table_name
, *table_json
);
1017 /* Add JSON row to JSON table. */
1018 snprintf(uuid
, sizeof uuid
, UUID_FMT
, UUID_ARGS(row_uuid
));
1019 json_object_put(*table_json
, uuid
, row_json
);
1022 /* Constructs and returns JSON for a <table-updates> object (as described in
1023 * RFC 7047) for all the outstanding changes within 'monitor', starting from
1026 ovsdb_monitor_compose_update(
1027 struct ovsdb_monitor
*dbmon
,
1028 bool initial
, uint64_t transaction
,
1029 const struct ovsdb_monitor_session_condition
*condition
,
1030 compose_row_update_cb_func row_update
)
1032 struct shash_node
*node
;
1034 size_t max_columns
= ovsdb_monitor_max_columns(dbmon
);
1035 unsigned long int *changed
= xmalloc(bitmap_n_bytes(max_columns
));
1038 SHASH_FOR_EACH (node
, &dbmon
->tables
) {
1039 struct ovsdb_monitor_table
*mt
= node
->data
;
1040 struct ovsdb_monitor_row
*row
, *next
;
1041 struct ovsdb_monitor_changes
*changes
;
1042 struct json
*table_json
= NULL
;
1044 changes
= ovsdb_monitor_table_find_changes(mt
, transaction
);
1049 HMAP_FOR_EACH_SAFE (row
, next
, hmap_node
, &changes
->rows
) {
1050 struct json
*row_json
;
1051 row_json
= (*row_update
)(mt
, condition
, OVSDB_MONITOR_ROW
, row
,
1052 initial
, changed
, changes
->n_columns
);
1054 ovsdb_monitor_add_json_row(&json
, mt
->table
->schema
->name
,
1055 &table_json
, row_json
,
1066 ovsdb_monitor_compose_cond_change_update(
1067 struct ovsdb_monitor
*dbmon
,
1068 struct ovsdb_monitor_session_condition
*condition
)
1070 struct shash_node
*node
;
1071 struct json
*json
= NULL
;
1072 size_t max_columns
= ovsdb_monitor_max_columns(dbmon
);
1073 unsigned long int *changed
= xmalloc(bitmap_n_bytes(max_columns
));
1075 SHASH_FOR_EACH (node
, &dbmon
->tables
) {
1076 struct ovsdb_monitor_table
*mt
= node
->data
;
1077 struct ovsdb_row
*row
;
1078 struct json
*table_json
= NULL
;
1079 struct ovsdb_condition
*old_condition
, *new_condition
;
1081 if (!ovsdb_monitor_get_table_conditions(mt
,
1085 !ovsdb_condition_cmp_3way(old_condition
, new_condition
)) {
1086 /* Nothing to update on this table */
1090 /* Iterate over all rows in table */
1091 HMAP_FOR_EACH (row
, hmap_node
, &mt
->table
->rows
) {
1092 struct json
*row_json
;
1094 row_json
= ovsdb_monitor_compose_row_update2(mt
, condition
,
1099 ovsdb_monitor_add_json_row(&json
, mt
->table
->schema
->name
,
1100 &table_json
, row_json
,
1101 ovsdb_row_get_uuid(row
));
1104 ovsdb_monitor_table_condition_updated(mt
, condition
);
1111 /* Returns JSON for a <table-updates> object (as described in RFC 7047)
1112 * for all the outstanding changes within 'monitor' that starts from
1114 * If cond_updated is true all rows in the db that match conditions will be
1117 * The caller should specify 'initial' as true if the returned JSON is going to
1118 * be used as part of the initial reply to a "monitor" request, false if it is
1119 * going to be used as part of an "update" notification. */
1121 ovsdb_monitor_get_update(
1122 struct ovsdb_monitor
*dbmon
,
1123 bool initial
, bool cond_updated
,
1124 uint64_t *unflushed_
,
1125 struct ovsdb_monitor_session_condition
*condition
,
1126 enum ovsdb_monitor_version version
)
1128 struct ovsdb_monitor_json_cache_node
*cache_node
= NULL
;
1129 struct shash_node
*node
;
1131 const uint64_t unflushed
= *unflushed_
;
1132 const uint64_t next_unflushed
= dbmon
->n_transactions
+ 1;
1134 ovs_assert(cond_updated
? unflushed
== next_unflushed
: true);
1136 /* Return a clone of cached json if one exists. Otherwise,
1137 * generate a new one and add it to the cache. */
1138 if (!condition
|| (!condition
->conditional
&& !cond_updated
)) {
1139 cache_node
= ovsdb_monitor_json_cache_search(dbmon
, version
,
1143 json
= cache_node
->json
? json_clone(cache_node
->json
) : NULL
;
1145 if (version
== OVSDB_MONITOR_V1
) {
1147 ovsdb_monitor_compose_update(dbmon
, initial
, unflushed
,
1149 ovsdb_monitor_compose_row_update
);
1151 ovs_assert(version
== OVSDB_MONITOR_V2
);
1152 if (!cond_updated
) {
1153 json
= ovsdb_monitor_compose_update(dbmon
, initial
, unflushed
,
1155 ovsdb_monitor_compose_row_update2
);
1157 if (!condition
|| !condition
->conditional
) {
1158 ovsdb_monitor_json_cache_insert(dbmon
, version
, unflushed
,
1162 /* Compose update on whole db due to condition update.
1163 Session must be flushed (change list is empty)*/
1165 ovsdb_monitor_compose_cond_change_update(dbmon
, condition
);
1170 /* Maintain transaction id of 'changes'. */
1171 SHASH_FOR_EACH (node
, &dbmon
->tables
) {
1172 struct ovsdb_monitor_table
*mt
= node
->data
;
1174 ovsdb_monitor_table_untrack_changes(mt
, unflushed
);
1175 ovsdb_monitor_table_track_changes(mt
, next_unflushed
);
1177 *unflushed_
= next_unflushed
;
1183 ovsdb_monitor_needs_flush(struct ovsdb_monitor
*dbmon
,
1184 uint64_t next_transaction
)
1186 ovs_assert(next_transaction
<= dbmon
->n_transactions
+ 1);
1187 return (next_transaction
<= dbmon
->n_transactions
);
1191 ovsdb_monitor_table_add_select(struct ovsdb_monitor
*dbmon
,
1192 const struct ovsdb_table
*table
,
1193 enum ovsdb_monitor_selection select
)
1195 struct ovsdb_monitor_table
* mt
;
1197 mt
= shash_find_data(&dbmon
->tables
, table
->schema
->name
);
1198 mt
->select
|= select
;
1202 * If a row's change type (insert, delete or modify) matches that of
1203 * the monitor, they should be sent to the monitor's clients as updates.
1204 * Of cause, the monitor should also internally update with this change.
1206 * When a change type does not require client side update, the monitor
1207 * may still need to keep track of certain changes in order to generate
1208 * correct future updates. For example, the monitor internal state should
1209 * be updated whenever a new row is inserted, in order to generate the
1210 * correct initial state, regardless if a insert change type is being
1213 * On the other hand, if a transaction only contains changes to columns
1214 * that are not monitored, this transaction can be safely ignored by the
1217 * Thus, the order of the declaration is important:
1218 * 'OVSDB_CHANGES_REQUIRE_EXTERNAL_UPDATE' always implies
1219 * 'OVSDB_CHANGES_REQUIRE_INTERNAL_UPDATE', but not vice versa. */
1220 enum ovsdb_monitor_changes_efficacy
{
1221 OVSDB_CHANGES_NO_EFFECT
, /* Monitor does not care about this
1223 OVSDB_CHANGES_REQUIRE_INTERNAL_UPDATE
, /* Monitor internal updates. */
1224 OVSDB_CHANGES_REQUIRE_EXTERNAL_UPDATE
, /* Client needs to be updated. */
1227 struct ovsdb_monitor_aux
{
1228 const struct ovsdb_monitor
*monitor
;
1229 struct ovsdb_monitor_table
*mt
;
1230 enum ovsdb_monitor_changes_efficacy efficacy
;
1234 ovsdb_monitor_init_aux(struct ovsdb_monitor_aux
*aux
,
1235 const struct ovsdb_monitor
*m
)
1239 aux
->efficacy
= OVSDB_CHANGES_NO_EFFECT
;
1243 ovsdb_monitor_changes_update(const struct ovsdb_row
*old
,
1244 const struct ovsdb_row
*new,
1245 const struct ovsdb_monitor_table
*mt
,
1246 struct ovsdb_monitor_changes
*changes
)
1248 const struct uuid
*uuid
= ovsdb_row_get_uuid(new ? new : old
);
1249 struct ovsdb_monitor_row
*change
;
1251 change
= ovsdb_monitor_changes_row_find(changes
, uuid
);
1253 change
= xzalloc(sizeof *change
);
1254 hmap_insert(&changes
->rows
, &change
->hmap_node
, uuid_hash(uuid
));
1255 change
->uuid
= *uuid
;
1256 change
->old
= clone_monitor_row_data(mt
, old
, changes
->n_columns
);
1257 change
->new = clone_monitor_row_data(mt
, new, changes
->n_columns
);
1261 /* Reinsert the row that was just deleted.
1263 * This path won't be hit without replication. Whenever OVSDB
1264 * server inserts a new row, It always generates a new UUID
1265 * that is different from the row just deleted.
1267 * With replication, this path can be hit in a corner
1268 * case when two OVSDB servers are set up to replicate
1269 * each other. Not that is a useful set up, but can
1270 * happen in practice.
1272 * An example of how this path can be hit is documented below.
1273 * The details is not as important to the correctness of the
1274 * logic, but added here to convince ourselves that this path
1277 * Imagine two OVSDB servers that replicates from each
1278 * other. For each replication session, there is a
1279 * corresponding monitor at the other end of the replication
1280 * JSONRPC connection.
1282 * The events can lead to a back to back deletion and
1283 * insertion operation of the same row for the monitor of
1284 * the first server are:
1286 * 1. A row is inserted in the first OVSDB server.
1287 * 2. The row is then replicated to the remote OVSDB server.
1288 * 3. The row is now deleted by the local OVSDB server. This
1289 * deletion operation is replicated to the local monitor
1290 * of the OVSDB server.
1291 * 4. The monitor now receives the same row, as an insertion,
1292 * from the replication server. Because of
1293 * replication, the row carries the same UUID as the row
1296 change
->new = clone_monitor_row_data(mt
, new,
1297 changes
->n_columns
);
1299 update_monitor_row_data(mt
, new, change
->new,
1300 changes
->n_columns
);
1303 free_monitor_row_data(mt
, change
->new, changes
->n_columns
);
1307 /* This row was added then deleted. Forget about it. */
1308 hmap_remove(&changes
->rows
, &change
->hmap_node
);
1316 ovsdb_monitor_columns_changed(const struct ovsdb_monitor_table
*mt
,
1317 const unsigned long int *changed
)
1321 for (i
= 0; i
< mt
->n_columns
; i
++) {
1322 size_t column_index
= mt
->columns
[i
].column
->index
;
1324 if (bitmap_is_set(changed
, column_index
)) {
1332 /* Return the efficacy of a row's change to a monitor table.
1334 * Please see the block comment above 'ovsdb_monitor_changes_efficacy'
1335 * definition form more information. */
1336 static enum ovsdb_monitor_changes_efficacy
1337 ovsdb_monitor_changes_classify(enum ovsdb_monitor_selection type
,
1338 const struct ovsdb_monitor_table
*mt
,
1339 const unsigned long int *changed
)
1341 if (type
== OJMS_MODIFY
&&
1342 !ovsdb_monitor_columns_changed(mt
, changed
)) {
1343 return OVSDB_CHANGES_NO_EFFECT
;
1346 if (type
== OJMS_MODIFY
) {
1347 /* Condition might turn a modify operation to insert or delete */
1348 type
|= OJMS_INSERT
| OJMS_DELETE
;
1351 return (mt
->select
& type
)
1352 ? OVSDB_CHANGES_REQUIRE_EXTERNAL_UPDATE
1353 : OVSDB_CHANGES_REQUIRE_INTERNAL_UPDATE
;
1357 ovsdb_monitor_change_cb(const struct ovsdb_row
*old
,
1358 const struct ovsdb_row
*new,
1359 const unsigned long int *changed
,
1362 struct ovsdb_monitor_aux
*aux
= aux_
;
1363 const struct ovsdb_monitor
*m
= aux
->monitor
;
1364 struct ovsdb_table
*table
= new ? new->table
: old
->table
;
1365 struct ovsdb_monitor_table
*mt
;
1366 struct ovsdb_monitor_changes
*changes
;
1368 if (!aux
->mt
|| table
!= aux
->mt
->table
) {
1369 aux
->mt
= shash_find_data(&m
->tables
, table
->schema
->name
);
1371 /* We don't care about rows in this table at all. Tell the caller
1378 enum ovsdb_monitor_selection type
=
1379 ovsdb_monitor_row_update_type(false, old
, new);
1380 enum ovsdb_monitor_changes_efficacy efficacy
=
1381 ovsdb_monitor_changes_classify(type
, mt
, changed
);
1383 HMAP_FOR_EACH(changes
, hmap_node
, &mt
->changes
) {
1384 if (efficacy
> OVSDB_CHANGES_NO_EFFECT
) {
1385 ovsdb_monitor_changes_update(old
, new, mt
, changes
);
1388 if (aux
->efficacy
< efficacy
) {
1389 aux
->efficacy
= efficacy
;
1396 ovsdb_monitor_get_initial(const struct ovsdb_monitor
*dbmon
)
1398 struct shash_node
*node
;
1400 SHASH_FOR_EACH (node
, &dbmon
->tables
) {
1401 struct ovsdb_monitor_table
*mt
= node
->data
;
1403 if (mt
->select
& OJMS_INITIAL
) {
1404 struct ovsdb_row
*row
;
1405 struct ovsdb_monitor_changes
*changes
;
1407 changes
= ovsdb_monitor_table_find_changes(mt
, 0);
1409 changes
= ovsdb_monitor_table_add_changes(mt
, 0);
1410 HMAP_FOR_EACH (row
, hmap_node
, &mt
->table
->rows
) {
1411 ovsdb_monitor_changes_update(NULL
, row
, mt
, changes
);
1421 ovsdb_monitor_remove_jsonrpc_monitor(struct ovsdb_monitor
*dbmon
,
1422 struct ovsdb_jsonrpc_monitor
*jsonrpc_monitor
,
1425 struct jsonrpc_monitor_node
*jm
;
1427 if (ovs_list_is_empty(&dbmon
->jsonrpc_monitors
)) {
1428 ovsdb_monitor_destroy(dbmon
);
1432 /* Find and remove the jsonrpc monitor from the list. */
1433 LIST_FOR_EACH(jm
, node
, &dbmon
->jsonrpc_monitors
) {
1434 if (jm
->jsonrpc_monitor
== jsonrpc_monitor
) {
1435 /* Release the tracked changes. */
1436 struct shash_node
*node
;
1437 SHASH_FOR_EACH (node
, &dbmon
->tables
) {
1438 struct ovsdb_monitor_table
*mt
= node
->data
;
1439 ovsdb_monitor_table_untrack_changes(mt
, unflushed
);
1441 ovs_list_remove(&jm
->node
);
1444 /* Destroy ovsdb monitor if this is the last user. */
1445 if (ovs_list_is_empty(&dbmon
->jsonrpc_monitors
)) {
1446 ovsdb_monitor_destroy(dbmon
);
1453 /* Should never reach here. jsonrpc_monitor should be on the list. */
1458 ovsdb_monitor_table_equal(const struct ovsdb_monitor_table
*a
,
1459 const struct ovsdb_monitor_table
*b
)
1463 ovs_assert(b
->n_columns
== b
->n_monitored_columns
);
1465 if ((a
->table
!= b
->table
) ||
1466 (a
->select
!= b
->select
) ||
1467 (a
->n_monitored_columns
!= b
->n_monitored_columns
)) {
1471 /* Compare only monitored columns that must be sorted already */
1472 for (i
= 0; i
< a
->n_monitored_columns
; i
++) {
1473 if ((a
->columns
[i
].column
!= b
->columns
[i
].column
) ||
1474 (a
->columns
[i
].select
!= b
->columns
[i
].select
)) {
1482 ovsdb_monitor_equal(const struct ovsdb_monitor
*a
,
1483 const struct ovsdb_monitor
*b
)
1485 struct shash_node
*node
;
1487 if (shash_count(&a
->tables
) != shash_count(&b
->tables
)) {
1491 SHASH_FOR_EACH(node
, &a
->tables
) {
1492 const struct ovsdb_monitor_table
*mta
= node
->data
;
1493 const struct ovsdb_monitor_table
*mtb
;
1495 mtb
= shash_find_data(&b
->tables
, node
->name
);
1500 if (!ovsdb_monitor_table_equal(mta
, mtb
)) {
1509 ovsdb_monitor_hash(const struct ovsdb_monitor
*dbmon
, size_t basis
)
1511 const struct shash_node
**nodes
;
1514 nodes
= shash_sort(&dbmon
->tables
);
1515 n
= shash_count(&dbmon
->tables
);
1517 for (i
= 0; i
< n
; i
++) {
1518 struct ovsdb_monitor_table
*mt
= nodes
[i
]->data
;
1520 basis
= hash_pointer(mt
->table
, basis
);
1521 basis
= hash_3words(mt
->select
, mt
->n_columns
, basis
);
1523 for (j
= 0; j
< mt
->n_columns
; j
++) {
1524 basis
= hash_pointer(mt
->columns
[j
].column
, basis
);
1525 basis
= hash_2words(mt
->columns
[j
].select
, basis
);
1533 struct ovsdb_monitor
*
1534 ovsdb_monitor_add(struct ovsdb_monitor
*new_dbmon
)
1536 struct ovsdb_monitor
*dbmon
;
1539 /* New_dbmon should be associated with only one jsonrpc
1541 ovs_assert(ovs_list_is_singleton(&new_dbmon
->jsonrpc_monitors
));
1543 ovsdb_monitor_columns_sort(new_dbmon
);
1545 hash
= ovsdb_monitor_hash(new_dbmon
, 0);
1546 HMAP_FOR_EACH_WITH_HASH(dbmon
, hmap_node
, hash
, &ovsdb_monitors
) {
1547 if (ovsdb_monitor_equal(dbmon
, new_dbmon
)) {
1552 hmap_insert(&ovsdb_monitors
, &new_dbmon
->hmap_node
, hash
);
1557 ovsdb_monitor_destroy(struct ovsdb_monitor
*dbmon
)
1559 struct shash_node
*node
;
1561 ovs_list_remove(&dbmon
->list_node
);
1563 if (!hmap_node_is_null(&dbmon
->hmap_node
)) {
1564 hmap_remove(&ovsdb_monitors
, &dbmon
->hmap_node
);
1567 ovsdb_monitor_json_cache_flush(dbmon
);
1568 hmap_destroy(&dbmon
->json_cache
);
1570 SHASH_FOR_EACH (node
, &dbmon
->tables
) {
1571 struct ovsdb_monitor_table
*mt
= node
->data
;
1572 struct ovsdb_monitor_changes
*changes
, *next
;
1574 HMAP_FOR_EACH_SAFE (changes
, next
, hmap_node
, &mt
->changes
) {
1575 hmap_remove(&mt
->changes
, &changes
->hmap_node
);
1576 ovsdb_monitor_changes_destroy(changes
);
1578 hmap_destroy(&mt
->changes
);
1580 free(mt
->columns_index_map
);
1583 shash_destroy(&dbmon
->tables
);
1588 ovsdb_monitor_commit(struct ovsdb_monitor
*m
, const struct ovsdb_txn
*txn
)
1590 struct ovsdb_monitor_aux aux
;
1592 ovsdb_monitor_init_aux(&aux
, m
);
1593 /* Update ovsdb_monitor's transaction number for
1594 * each transaction, before calling ovsdb_monitor_change_cb(). */
1595 m
->n_transactions
++;
1596 ovsdb_txn_for_each_change(txn
, ovsdb_monitor_change_cb
, &aux
);
1598 switch(aux
.efficacy
) {
1599 case OVSDB_CHANGES_NO_EFFECT
:
1600 /* The transaction is ignored by the monitor.
1601 * Roll back the 'n_transactions' as if the transaction
1602 * has never happened. */
1603 m
->n_transactions
--;
1605 case OVSDB_CHANGES_REQUIRE_INTERNAL_UPDATE
:
1608 case OVSDB_CHANGES_REQUIRE_EXTERNAL_UPDATE
:
1609 ovsdb_monitor_json_cache_flush(m
);
1615 ovsdb_monitors_commit(struct ovsdb
*db
, const struct ovsdb_txn
*txn
)
1617 struct ovsdb_monitor
*m
;
1619 LIST_FOR_EACH (m
, list_node
, &db
->monitors
) {
1620 ovsdb_monitor_commit(m
, txn
);
1625 ovsdb_monitors_remove(struct ovsdb
*db
)
1627 struct ovsdb_monitor
*m
, *next_m
;
1629 LIST_FOR_EACH_SAFE (m
, next_m
, list_node
, &db
->monitors
) {
1630 struct jsonrpc_monitor_node
*jm
, *next_jm
;
1632 /* Delete all front-end monitors. Removing the last front-end monitor
1633 * will also destroy the corresponding ovsdb_monitor. */
1634 LIST_FOR_EACH_SAFE (jm
, next_jm
, node
, &m
->jsonrpc_monitors
) {
1635 ovsdb_jsonrpc_monitor_destroy(jm
->jsonrpc_monitor
, false);
1640 /* Add some memory usage statics for monitors into 'usage', for use with
1641 * memory_report(). */
1643 ovsdb_monitor_get_memory_usage(struct simap
*usage
)
1645 struct ovsdb_monitor
*dbmon
;
1646 simap_put(usage
, "monitors", hmap_count(&ovsdb_monitors
));
1648 HMAP_FOR_EACH(dbmon
, hmap_node
, &ovsdb_monitors
) {
1649 simap_increase(usage
, "json-caches", hmap_count(&dbmon
->json_cache
));
1654 ovsdb_monitor_prereplace_db(struct ovsdb
*db
)
1656 struct ovsdb_monitor
*m
, *next_m
;
1658 LIST_FOR_EACH_SAFE (m
, next_m
, list_node
, &db
->monitors
) {
1659 struct jsonrpc_monitor_node
*jm
, *next_jm
;
1661 /* Delete all front-end monitors. Removing the last front-end monitor
1662 * will also destroy the corresponding ovsdb_monitor. */
1663 LIST_FOR_EACH_SAFE (jm
, next_jm
, node
, &m
->jsonrpc_monitors
) {
1664 ovsdb_jsonrpc_monitor_destroy(jm
->jsonrpc_monitor
, true);