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. */
73 /* Contains "ovsdb_monitor_change_set". Each change set contains changes
74 * from some start point up to the latest committed transaction. There can
75 * be different change sets for the same struct ovsdb_monitor because there
76 * are different clients pending on changes starting from different points.
77 * The different change sets are maintained as a list. */
78 struct ovs_list change_sets
;
80 /* The new change set that is to be populated for future transactions. */
81 struct ovsdb_monitor_change_set
*new_change_set
;
83 /* The change set that starts from the first transaction of the DB, which
84 * is used for populating the initial data for new clients. */
85 struct ovsdb_monitor_change_set
*init_change_set
;
87 struct hmap_node hmap_node
; /* Elements within ovsdb_monitors. */
88 struct hmap json_cache
; /* Contains "ovsdb_monitor_json_cache_node"s.*/
91 /* A json object of updates for the ovsdb_monitor_change_set and the given
93 struct ovsdb_monitor_json_cache_node
{
94 struct hmap_node hmap_node
; /* Elements in json cache. */
95 enum ovsdb_monitor_version version
;
96 struct uuid change_set_uuid
;
97 struct json
*json
; /* Null, or a cloned of json */
100 struct jsonrpc_monitor_node
{
101 struct ovs_list node
;
102 struct ovsdb_jsonrpc_monitor
*jsonrpc_monitor
;
105 /* A particular column being monitored. */
106 struct ovsdb_monitor_column
{
107 const struct ovsdb_column
*column
;
108 enum ovsdb_monitor_selection select
;
112 /* A row that has changed in a monitored table. */
113 struct ovsdb_monitor_row
{
114 struct hmap_node hmap_node
; /* In ovsdb_monitor_change_set_for_table. */
115 struct uuid uuid
; /* UUID of row that changed. */
116 struct ovsdb_datum
*old
; /* Old data, NULL for an inserted row. */
117 struct ovsdb_datum
*new; /* New data, NULL for a deleted row. */
120 /* Contains a set of changes that are not yet flushed to all the jsonrpc
123 * 'n_refs' represent the number of jsonrpc connections that depend on this
124 * change set (have not received updates). Generate the update for the last
125 * jsonprc connection will also destroy the whole "struct
126 * ovsdb_monitor_change_set" object.
128 struct ovsdb_monitor_change_set
{
129 /* Element in change_sets of ovsdb_monitor. */
130 struct ovs_list list_node
;
132 /* Internally generated uuid that identifies this data structure. */
135 /* Contains struct ovsdb_monitor_change_set_for_table. */
136 struct ovs_list change_set_for_tables
;
140 /* The previous txn id before this change set's start point. */
141 struct uuid prev_txn
;
144 /* Contains 'struct ovsdb_monitor_row's for rows in a specific table
145 * of struct ovsdb_monitor_change_set. It can also be searched from
146 * member 'change_sets' of struct ovsdb_monitor_table. */
147 struct ovsdb_monitor_change_set_for_table
{
148 /* Element in ovsdb_monitor_tables' change_sets list. */
149 struct ovs_list list_in_mt
;
151 /* Element in ovsdb_monitor_change_sets' change_set_for_tables list. */
152 struct ovs_list list_in_change_set
;
154 struct ovsdb_monitor_table
*mt
;
155 struct ovsdb_monitor_change_set
*mcs
;
157 /* Contains struct ovsdb_monitor_row. */
160 /* Save the mt->n_columns that is used when creating the changes.
161 * It can be different from the current mt->n_columns because
162 * mt->n_columns can be increased when there are condition changes
163 * from any of the clients sharing the dbmon. */
167 /* A particular table being monitored. */
168 struct ovsdb_monitor_table
{
169 const struct ovsdb_table
*table
;
171 /* This is the union (bitwise-OR) of the 'select' values in all of the
172 * members of 'columns' below. */
173 enum ovsdb_monitor_selection select
;
175 /* Columns being monitored. */
176 struct ovsdb_monitor_column
*columns
;
178 size_t n_monitored_columns
;
179 size_t allocated_columns
;
181 /* Columns in ovsdb_monitor_row have different indexes then in
182 * ovsdb_row. This field maps between column->index to the index in the
183 * ovsdb_monitor_row. It is used for condition evaluation. */
184 unsigned int *columns_index_map
;
186 /* Contains 'ovsdb_monitor_change_set_for_table'. */
187 struct ovs_list change_sets
;
190 enum ovsdb_monitor_row_type
{
195 typedef struct json
*
196 (*compose_row_update_cb_func
)
197 (const struct ovsdb_monitor_table
*mt
,
198 const struct ovsdb_monitor_session_condition
* condition
,
199 enum ovsdb_monitor_row_type row_type
,
201 bool initial
, unsigned long int *changed
,
204 static void ovsdb_monitor_destroy(struct ovsdb_monitor
*);
205 static struct ovsdb_monitor_change_set
* ovsdb_monitor_add_change_set(
206 struct ovsdb_monitor
*, bool init_only
, const struct uuid
*prev_txn
);
207 static struct ovsdb_monitor_change_set
* ovsdb_monitor_find_change_set(
208 const struct ovsdb_monitor
*, const struct uuid
*prev_txn
);
209 static void ovsdb_monitor_change_set_destroy(
210 struct ovsdb_monitor_change_set
*);
211 static void ovsdb_monitor_track_new_change_set(struct ovsdb_monitor
*);
214 json_cache_hash(enum ovsdb_monitor_version version
,
215 struct ovsdb_monitor_change_set
*change_set
)
217 return hash_uint64_basis(version
, uuid_hash(&change_set
->uuid
));
220 static struct ovsdb_monitor_json_cache_node
*
221 ovsdb_monitor_json_cache_search(const struct ovsdb_monitor
*dbmon
,
222 enum ovsdb_monitor_version version
,
223 struct ovsdb_monitor_change_set
*change_set
)
225 struct ovsdb_monitor_json_cache_node
*node
;
226 uint32_t hash
= json_cache_hash(version
, change_set
);
228 HMAP_FOR_EACH_WITH_HASH(node
, hmap_node
, hash
, &dbmon
->json_cache
) {
229 if (uuid_equals(&node
->change_set_uuid
, &change_set
->uuid
) &&
230 node
->version
== version
) {
239 ovsdb_monitor_json_cache_insert(struct ovsdb_monitor
*dbmon
,
240 enum ovsdb_monitor_version version
,
241 struct ovsdb_monitor_change_set
*change_set
,
244 struct ovsdb_monitor_json_cache_node
*node
;
245 uint32_t hash
= json_cache_hash(version
, change_set
);
247 node
= xmalloc(sizeof *node
);
249 node
->version
= version
;
250 node
->change_set_uuid
= change_set
->uuid
;
251 node
->json
= json
? json_clone(json
) : NULL
;
253 hmap_insert(&dbmon
->json_cache
, &node
->hmap_node
, hash
);
257 ovsdb_monitor_json_cache_flush(struct ovsdb_monitor
*dbmon
)
259 struct ovsdb_monitor_json_cache_node
*node
;
261 HMAP_FOR_EACH_POP(node
, hmap_node
, &dbmon
->json_cache
) {
262 json_destroy(node
->json
);
267 /* Free all versions of json cache for a given change_set.*/
269 ovsdb_monitor_json_cache_destroy(struct ovsdb_monitor
*dbmon
,
270 struct ovsdb_monitor_change_set
*change_set
)
272 enum ovsdb_monitor_version v
;
273 for (v
= OVSDB_MONITOR_V1
; v
< OVSDB_MONITOR_VERSION_MAX
; v
++) {
274 struct ovsdb_monitor_json_cache_node
*node
275 = ovsdb_monitor_json_cache_search(dbmon
, v
, change_set
);
277 hmap_remove(&dbmon
->json_cache
, &node
->hmap_node
);
278 json_destroy(node
->json
);
285 compare_ovsdb_monitor_column(const void *a_
, const void *b_
)
287 const struct ovsdb_monitor_column
*a
= a_
;
288 const struct ovsdb_monitor_column
*b
= b_
;
290 /* put all monitored columns at the begining */
291 if (a
->monitored
!= b
->monitored
) {
292 return a
->monitored
? -1 : 1;
295 return a
->column
< b
->column
? -1 : a
->column
> b
->column
;
298 /* Finds and returns the ovsdb_monitor_row in 'mt->changes->rows' for the
299 * given 'uuid', or NULL if there is no such row. */
300 static struct ovsdb_monitor_row
*
301 ovsdb_monitor_changes_row_find(
302 const struct ovsdb_monitor_change_set_for_table
*changes
,
303 const struct uuid
*uuid
)
305 struct ovsdb_monitor_row
*row
;
307 HMAP_FOR_EACH_WITH_HASH (row
, hmap_node
, uuid_hash(uuid
),
309 if (uuid_equals(uuid
, &row
->uuid
)) {
316 /* Allocates an array of 'n_columns' ovsdb_datums and initializes them as
317 * copies of the data in 'row' drawn from the columns represented by
318 * mt->columns[]. Returns the array.
320 * If 'row' is NULL, returns NULL. */
321 static struct ovsdb_datum
*
322 clone_monitor_row_data(const struct ovsdb_monitor_table
*mt
,
323 const struct ovsdb_row
*row
,
326 struct ovsdb_datum
*data
;
333 data
= xmalloc(n_columns
* sizeof *data
);
334 for (i
= 0; i
< n_columns
; i
++) {
335 const struct ovsdb_column
*c
= mt
->columns
[i
].column
;
336 const struct ovsdb_datum
*src
= &row
->fields
[c
->index
];
337 struct ovsdb_datum
*dst
= &data
[i
];
338 const struct ovsdb_type
*type
= &c
->type
;
340 ovsdb_datum_clone(dst
, src
, type
);
345 /* Replaces the n_columns ovsdb_datums in row[] by copies of the data from
346 * in 'row' drawn from the columns represented by mt->columns[]. */
348 update_monitor_row_data(const struct ovsdb_monitor_table
*mt
,
349 const struct ovsdb_row
*row
,
350 struct ovsdb_datum
*data
,
355 for (i
= 0; i
< n_columns
; i
++) {
356 const struct ovsdb_column
*c
= mt
->columns
[i
].column
;
357 const struct ovsdb_datum
*src
= &row
->fields
[c
->index
];
358 struct ovsdb_datum
*dst
= &data
[i
];
359 const struct ovsdb_type
*type
= &c
->type
;
361 if (!ovsdb_datum_equals(src
, dst
, type
)) {
362 ovsdb_datum_destroy(dst
, type
);
363 ovsdb_datum_clone(dst
, src
, type
);
368 /* Frees all of the n_columns ovsdb_datums in data[], using the types taken
369 * from mt->columns[], plus 'data' itself. */
371 free_monitor_row_data(const struct ovsdb_monitor_table
*mt
,
372 struct ovsdb_datum
*data
,
378 for (i
= 0; i
< n_columns
; i
++) {
379 const struct ovsdb_column
*c
= mt
->columns
[i
].column
;
381 ovsdb_datum_destroy(&data
[i
], &c
->type
);
387 /* Frees 'row', which must have been created from 'mt'. */
389 ovsdb_monitor_row_destroy(const struct ovsdb_monitor_table
*mt
,
390 struct ovsdb_monitor_row
*row
,
394 free_monitor_row_data(mt
, row
->old
, n_columns
);
395 free_monitor_row_data(mt
, row
->new, n_columns
);
401 ovsdb_monitor_columns_sort(struct ovsdb_monitor
*dbmon
)
404 struct shash_node
*node
;
406 SHASH_FOR_EACH (node
, &dbmon
->tables
) {
407 struct ovsdb_monitor_table
*mt
= node
->data
;
409 qsort(mt
->columns
, mt
->n_columns
, sizeof *mt
->columns
,
410 compare_ovsdb_monitor_column
);
411 for (i
= 0; i
< mt
->n_columns
; i
++) {
412 /* re-set index map due to sort */
413 mt
->columns_index_map
[mt
->columns
[i
].column
->index
] = i
;
419 ovsdb_monitor_add_jsonrpc_monitor(struct ovsdb_monitor
*dbmon
,
420 struct ovsdb_jsonrpc_monitor
*jsonrpc_monitor
)
422 struct jsonrpc_monitor_node
*jm
;
424 jm
= xzalloc(sizeof *jm
);
425 jm
->jsonrpc_monitor
= jsonrpc_monitor
;
426 ovs_list_push_back(&dbmon
->jsonrpc_monitors
, &jm
->node
);
429 struct ovsdb_monitor
*
430 ovsdb_monitor_create(struct ovsdb
*db
,
431 struct ovsdb_jsonrpc_monitor
*jsonrpc_monitor
)
433 struct ovsdb_monitor
*dbmon
;
435 dbmon
= xzalloc(sizeof *dbmon
);
437 ovs_list_push_back(&db
->monitors
, &dbmon
->list_node
);
438 ovs_list_init(&dbmon
->jsonrpc_monitors
);
440 ovs_list_init(&dbmon
->change_sets
);
441 shash_init(&dbmon
->tables
);
442 hmap_node_nullify(&dbmon
->hmap_node
);
443 hmap_init(&dbmon
->json_cache
);
445 ovsdb_monitor_add_jsonrpc_monitor(dbmon
, jsonrpc_monitor
);
450 ovsdb_monitor_add_table(struct ovsdb_monitor
*m
,
451 const struct ovsdb_table
*table
)
453 struct ovsdb_monitor_table
*mt
;
455 size_t n_columns
= shash_count(&table
->schema
->columns
);
457 mt
= xzalloc(sizeof *mt
);
459 shash_add(&m
->tables
, table
->schema
->name
, mt
);
460 ovs_list_init(&mt
->change_sets
);
461 mt
->columns_index_map
=
462 xmalloc(sizeof *mt
->columns_index_map
* n_columns
);
463 for (i
= 0; i
< n_columns
; i
++) {
464 mt
->columns_index_map
[i
] = -1;
469 ovsdb_monitor_add_column(struct ovsdb_monitor
*dbmon
,
470 const struct ovsdb_table
*table
,
471 const struct ovsdb_column
*column
,
472 enum ovsdb_monitor_selection select
,
475 struct ovsdb_monitor_table
*mt
;
476 struct ovsdb_monitor_column
*c
;
478 mt
= shash_find_data(&dbmon
->tables
, table
->schema
->name
);
480 /* Check for column duplication. Return duplicated column name. */
481 if (mt
->columns_index_map
[column
->index
] != -1) {
485 if (mt
->n_columns
>= mt
->allocated_columns
) {
486 mt
->columns
= x2nrealloc(mt
->columns
, &mt
->allocated_columns
,
487 sizeof *mt
->columns
);
490 mt
->select
|= select
;
491 mt
->columns_index_map
[column
->index
] = mt
->n_columns
;
492 c
= &mt
->columns
[mt
->n_columns
++];
495 c
->monitored
= monitored
;
497 mt
->n_monitored_columns
++;
504 ovsdb_monitor_condition_add_columns(struct ovsdb_monitor
*dbmon
,
505 const struct ovsdb_table
*table
,
506 struct ovsdb_condition
*condition
)
510 const struct ovsdb_column
**columns
=
511 ovsdb_condition_get_columns(condition
, &n_columns
);
513 for (i
= 0; i
< n_columns
; i
++) {
514 ovsdb_monitor_add_column(dbmon
, table
, columns
[i
],
521 /* Bind this session's condition to ovsdb_monitor */
523 ovsdb_monitor_condition_bind(struct ovsdb_monitor
*dbmon
,
524 struct ovsdb_monitor_session_condition
*cond
)
526 struct shash_node
*node
;
528 SHASH_FOR_EACH(node
, &cond
->tables
) {
529 struct ovsdb_monitor_table_condition
*mtc
= node
->data
;
530 struct ovsdb_monitor_table
*mt
=
531 shash_find_data(&dbmon
->tables
, mtc
->table
->schema
->name
);
534 ovsdb_monitor_condition_add_columns(dbmon
, mtc
->table
,
535 &mtc
->new_condition
);
540 ovsdb_monitor_table_exists(struct ovsdb_monitor
*m
,
541 const struct ovsdb_table
*table
)
543 return shash_find_data(&m
->tables
, table
->schema
->name
);
546 static struct ovsdb_monitor_change_set
*
547 ovsdb_monitor_add_change_set(struct ovsdb_monitor
*dbmon
,
548 bool init_only
, const struct uuid
*prev_txn
)
550 struct ovsdb_monitor_change_set
*change_set
= xzalloc(sizeof *change_set
);
551 change_set
->uuid
= uuid_random();
552 ovs_list_push_back(&(dbmon
->change_sets
), &change_set
->list_node
);
553 ovs_list_init(&change_set
->change_set_for_tables
);
554 change_set
->n_refs
= 1;
555 change_set
->prev_txn
= prev_txn
? *prev_txn
: UUID_ZERO
;
557 struct shash_node
*node
;
558 SHASH_FOR_EACH (node
, &dbmon
->tables
) {
559 struct ovsdb_monitor_table
*mt
= node
->data
;
560 if (!init_only
|| (mt
->select
& OJMS_INITIAL
)) {
561 struct ovsdb_monitor_change_set_for_table
*mcst
=
562 xzalloc(sizeof *mcst
);
564 mcst
->n_columns
= mt
->n_columns
;
565 mcst
->mcs
= change_set
;
566 hmap_init(&mcst
->rows
);
567 ovs_list_push_back(&mt
->change_sets
, &mcst
->list_in_mt
);
568 ovs_list_push_back(&change_set
->change_set_for_tables
,
569 &mcst
->list_in_change_set
);
576 static struct ovsdb_monitor_change_set
*
577 ovsdb_monitor_find_change_set(const struct ovsdb_monitor
*dbmon
,
578 const struct uuid
*prev_txn
)
580 struct ovsdb_monitor_change_set
*cs
;
581 LIST_FOR_EACH (cs
, list_node
, &dbmon
->change_sets
) {
582 if (uuid_equals(&cs
->prev_txn
, prev_txn
)) {
583 /* Check n_columns for each table in dbmon, in case it is changed
584 * after the change set is populated. */
585 bool n_col_is_equal
= true;
586 struct ovsdb_monitor_change_set_for_table
*mcst
;
587 LIST_FOR_EACH (mcst
, list_in_change_set
,
588 &cs
->change_set_for_tables
) {
589 struct ovsdb_monitor_table
*mt
= mcst
->mt
;
590 if (mt
->n_columns
!= mcst
->n_columns
) {
591 n_col_is_equal
= false;
595 if (n_col_is_equal
) {
604 ovsdb_monitor_untrack_change_set(struct ovsdb_monitor
*dbmon
,
605 struct ovsdb_monitor_change_set
*mcs
)
608 if (--mcs
->n_refs
== 0) {
609 if (mcs
== dbmon
->init_change_set
) {
610 dbmon
->init_change_set
= NULL
;
611 } else if (mcs
== dbmon
->new_change_set
) {
612 dbmon
->new_change_set
= NULL
;
614 ovsdb_monitor_json_cache_destroy(dbmon
, mcs
);
615 ovsdb_monitor_change_set_destroy(mcs
);
620 ovsdb_monitor_track_new_change_set(struct ovsdb_monitor
*dbmon
)
622 struct ovsdb_monitor_change_set
*change_set
= dbmon
->new_change_set
;
625 change_set
->n_refs
++;
627 change_set
= ovsdb_monitor_add_change_set(dbmon
, false,
628 ovsdb_monitor_get_last_txnid(dbmon
));
629 dbmon
->new_change_set
= change_set
;
634 ovsdb_monitor_change_set_destroy(struct ovsdb_monitor_change_set
*mcs
)
636 ovs_list_remove(&mcs
->list_node
);
638 struct ovsdb_monitor_change_set_for_table
*mcst
, *next_mcst
;
639 LIST_FOR_EACH_SAFE (mcst
, next_mcst
, list_in_change_set
,
640 &mcs
->change_set_for_tables
) {
641 ovs_list_remove(&mcst
->list_in_change_set
);
642 ovs_list_remove(&mcst
->list_in_mt
);
644 struct ovsdb_monitor_row
*row
, *next
;
645 HMAP_FOR_EACH_SAFE (row
, next
, hmap_node
, &mcst
->rows
) {
646 hmap_remove(&mcst
->rows
, &row
->hmap_node
);
647 ovsdb_monitor_row_destroy(mcst
->mt
, row
, mcst
->n_columns
);
649 hmap_destroy(&mcst
->rows
);
656 static enum ovsdb_monitor_selection
657 ovsdb_monitor_row_update_type(bool initial
, const bool old
, const bool new)
659 return initial
? OJMS_INITIAL
665 /* Set conditional monitoring mode only if we have non-empty condition in one
666 * of the tables at least */
668 ovsdb_monitor_session_condition_set_mode(
669 struct ovsdb_monitor_session_condition
*cond
)
671 struct shash_node
*node
;
673 SHASH_FOR_EACH (node
, &cond
->tables
) {
674 struct ovsdb_monitor_table_condition
*mtc
= node
->data
;
676 if (!ovsdb_condition_is_true(&mtc
->new_condition
)) {
677 cond
->conditional
= true;
681 cond
->conditional
= false;
684 /* Returnes an empty allocated session's condition state holder */
685 struct ovsdb_monitor_session_condition
*
686 ovsdb_monitor_session_condition_create(void)
688 struct ovsdb_monitor_session_condition
*condition
=
689 xzalloc(sizeof *condition
);
691 condition
->conditional
= false;
692 shash_init(&condition
->tables
);
697 ovsdb_monitor_session_condition_destroy(
698 struct ovsdb_monitor_session_condition
*condition
)
700 struct shash_node
*node
, *next
;
706 SHASH_FOR_EACH_SAFE (node
, next
, &condition
->tables
) {
707 struct ovsdb_monitor_table_condition
*mtc
= node
->data
;
709 ovsdb_condition_destroy(&mtc
->new_condition
);
710 ovsdb_condition_destroy(&mtc
->old_condition
);
711 shash_delete(&condition
->tables
, node
);
714 shash_destroy(&condition
->tables
);
719 ovsdb_monitor_table_condition_create(
720 struct ovsdb_monitor_session_condition
*condition
,
721 const struct ovsdb_table
*table
,
722 const struct json
*json_cnd
)
724 struct ovsdb_monitor_table_condition
*mtc
;
725 struct ovsdb_error
*error
;
727 mtc
= xzalloc(sizeof *mtc
);
729 ovsdb_condition_init(&mtc
->old_condition
);
730 ovsdb_condition_init(&mtc
->new_condition
);
733 error
= ovsdb_condition_from_json(table
->schema
,
736 &mtc
->old_condition
);
743 shash_add(&condition
->tables
, table
->schema
->name
, mtc
);
744 /* On session startup old == new condition */
745 ovsdb_condition_clone(&mtc
->new_condition
, &mtc
->old_condition
);
746 ovsdb_monitor_session_condition_set_mode(condition
);
752 ovsdb_monitor_get_table_conditions(
753 const struct ovsdb_monitor_table
*mt
,
754 const struct ovsdb_monitor_session_condition
*condition
,
755 struct ovsdb_condition
**old_condition
,
756 struct ovsdb_condition
**new_condition
)
762 struct ovsdb_monitor_table_condition
*mtc
=
763 shash_find_data(&condition
->tables
, mt
->table
->schema
->name
);
768 *old_condition
= &mtc
->old_condition
;
769 *new_condition
= &mtc
->new_condition
;
775 ovsdb_monitor_table_condition_update(
776 struct ovsdb_monitor
*dbmon
,
777 struct ovsdb_monitor_session_condition
*condition
,
778 const struct ovsdb_table
*table
,
779 const struct json
*cond_json
)
785 struct ovsdb_monitor_table_condition
*mtc
=
786 shash_find_data(&condition
->tables
, table
->schema
->name
);
787 struct ovsdb_error
*error
;
788 struct ovsdb_condition cond
= OVSDB_CONDITION_INITIALIZER(&cond
);
790 error
= ovsdb_condition_from_json(table
->schema
, cond_json
,
795 ovsdb_condition_destroy(&mtc
->new_condition
);
796 ovsdb_condition_clone(&mtc
->new_condition
, &cond
);
797 ovsdb_condition_destroy(&cond
);
798 ovsdb_monitor_condition_add_columns(dbmon
,
800 &mtc
->new_condition
);
806 ovsdb_monitor_table_condition_updated(struct ovsdb_monitor_table
*mt
,
807 struct ovsdb_monitor_session_condition
*condition
)
809 struct ovsdb_monitor_table_condition
*mtc
=
810 shash_find_data(&condition
->tables
, mt
->table
->schema
->name
);
813 /* If conditional monitoring - set old condition to new condition */
814 if (ovsdb_condition_cmp_3way(&mtc
->old_condition
,
815 &mtc
->new_condition
)) {
816 ovsdb_condition_destroy(&mtc
->old_condition
);
817 ovsdb_condition_clone(&mtc
->old_condition
, &mtc
->new_condition
);
818 ovsdb_monitor_session_condition_set_mode(condition
);
823 static enum ovsdb_monitor_selection
824 ovsdb_monitor_row_update_type_condition(
825 const struct ovsdb_monitor_table
*mt
,
826 const struct ovsdb_monitor_session_condition
*condition
,
828 enum ovsdb_monitor_row_type row_type
,
829 const struct ovsdb_datum
*old
,
830 const struct ovsdb_datum
*new)
832 struct ovsdb_condition
*old_condition
, *new_condition
;
833 enum ovsdb_monitor_selection type
=
834 ovsdb_monitor_row_update_type(initial
, old
, new);
836 if (ovsdb_monitor_get_table_conditions(mt
,
840 bool old_cond
= !old
? false
841 : ovsdb_condition_empty_or_match_any(old
,
843 row_type
== OVSDB_MONITOR_ROW
?
844 mt
->columns_index_map
:
846 bool new_cond
= !new ? false
847 : ovsdb_condition_empty_or_match_any(new,
849 row_type
== OVSDB_MONITOR_ROW
?
850 mt
->columns_index_map
:
853 if (!old_cond
&& !new_cond
) {
865 type
= !old_cond
? OJMS_INSERT
: !new_cond
866 ? OJMS_DELETE
: OJMS_MODIFY
;
881 ovsdb_monitor_row_skip_update(const struct ovsdb_monitor_table
*mt
,
882 enum ovsdb_monitor_row_type row_type
,
883 const struct ovsdb_datum
*old
,
884 const struct ovsdb_datum
*new,
885 enum ovsdb_monitor_selection type
,
886 unsigned long int *changed
,
889 if (!(mt
->select
& type
)) {
893 if (type
== OJMS_MODIFY
) {
897 memset(changed
, 0, bitmap_n_bytes(n_columns
));
898 for (i
= 0; i
< n_columns
; i
++) {
899 const struct ovsdb_column
*c
= mt
->columns
[i
].column
;
900 size_t index
= row_type
== OVSDB_ROW
? c
->index
: i
;
901 if (!ovsdb_datum_equals(&old
[index
], &new[index
], &c
->type
)) {
902 bitmap_set1(changed
, i
);
907 /* No actual changes: presumably a row changed and then
908 * changed back later. */
916 /* Returns JSON for a <row-update> (as described in RFC 7047) for 'row' within
917 * 'mt', or NULL if no row update should be sent.
919 * The caller should specify 'initial' as true if the returned JSON is going to
920 * be used as part of the initial reply to a "monitor" request, false if it is
921 * going to be used as part of an "update" notification.
923 * 'changed' must be a scratch buffer for internal use that is at least
924 * bitmap_n_bytes(n_columns) bytes long. */
926 ovsdb_monitor_compose_row_update(
927 const struct ovsdb_monitor_table
*mt
,
928 const struct ovsdb_monitor_session_condition
*condition OVS_UNUSED
,
929 enum ovsdb_monitor_row_type row_type OVS_UNUSED
,
931 bool initial
, unsigned long int *changed
,
932 size_t n_columns OVS_UNUSED
)
934 const struct ovsdb_monitor_row
*row
= _row
;
935 enum ovsdb_monitor_selection type
;
936 struct json
*old_json
, *new_json
;
937 struct json
*row_json
;
940 ovs_assert(row_type
== OVSDB_MONITOR_ROW
);
941 type
= ovsdb_monitor_row_update_type(initial
, row
->old
, row
->new);
942 if (ovsdb_monitor_row_skip_update(mt
, row_type
, row
->old
,
943 row
->new, type
, changed
,
948 row_json
= json_object_create();
949 old_json
= new_json
= NULL
;
950 if (type
& (OJMS_DELETE
| OJMS_MODIFY
)) {
951 old_json
= json_object_create();
952 json_object_put(row_json
, "old", old_json
);
954 if (type
& (OJMS_INITIAL
| OJMS_INSERT
| OJMS_MODIFY
)) {
955 new_json
= json_object_create();
956 json_object_put(row_json
, "new", new_json
);
958 for (i
= 0; i
< mt
->n_monitored_columns
; i
++) {
959 const struct ovsdb_monitor_column
*c
= &mt
->columns
[i
];
961 if (!c
->monitored
|| !(type
& c
->select
)) {
962 /* We don't care about this type of change for this
963 * particular column (but we will care about it for some
968 if ((type
== OJMS_MODIFY
&& bitmap_is_set(changed
, i
))
969 || type
== OJMS_DELETE
) {
970 json_object_put(old_json
, c
->column
->name
,
971 ovsdb_datum_to_json(&row
->old
[i
],
974 if (type
& (OJMS_INITIAL
| OJMS_INSERT
| OJMS_MODIFY
)) {
975 json_object_put(new_json
, c
->column
->name
,
976 ovsdb_datum_to_json(&row
->new[i
],
984 /* Returns JSON for a <row-update2> (as described in ovsdb-server(1) mapage)
985 * for 'row' within * 'mt', or NULL if no row update should be sent.
987 * The caller should specify 'initial' as true if the returned JSON is
988 * going to be used as part of the initial reply to a "monitor_cond" request,
989 * false if it is going to be used as part of an "update2" notification.
991 * 'changed' must be a scratch buffer for internal use that is at least
992 * bitmap_n_bytes(n_columns) bytes long. */
994 ovsdb_monitor_compose_row_update2(
995 const struct ovsdb_monitor_table
*mt
,
996 const struct ovsdb_monitor_session_condition
*condition
,
997 enum ovsdb_monitor_row_type row_type
,
999 bool initial
, unsigned long int *changed
,
1002 enum ovsdb_monitor_selection type
;
1003 struct json
*row_update2
, *diff_json
;
1004 const struct ovsdb_datum
*old
, *new;
1007 if (row_type
== OVSDB_MONITOR_ROW
) {
1008 old
= ((const struct ovsdb_monitor_row
*)_row
)->old
;;
1009 new = ((const struct ovsdb_monitor_row
*)_row
)->new;
1011 old
= new = ((const struct ovsdb_row
*)_row
)->fields
;
1014 type
= ovsdb_monitor_row_update_type_condition(mt
, condition
, initial
,
1015 row_type
, old
, new);
1016 if (ovsdb_monitor_row_skip_update(mt
, row_type
, old
, new, type
, changed
,
1021 row_update2
= json_object_create();
1022 if (type
== OJMS_DELETE
) {
1023 json_object_put(row_update2
, "delete", json_null_create());
1025 diff_json
= json_object_create();
1028 for (i
= 0; i
< mt
->n_monitored_columns
; i
++) {
1029 const struct ovsdb_monitor_column
*c
= &mt
->columns
[i
];
1030 size_t index
= row_type
== OVSDB_ROW
? c
->column
->index
: i
;
1031 if (!c
->monitored
|| !(type
& c
->select
)) {
1032 /* We don't care about this type of change for this
1033 * particular column (but we will care about it for some
1038 if (type
== OJMS_MODIFY
) {
1039 struct ovsdb_datum diff
;
1041 if (!bitmap_is_set(changed
, i
)) {
1045 ovsdb_datum_diff(&diff
,&old
[index
], &new[index
],
1047 json_object_put(diff_json
, c
->column
->name
,
1048 ovsdb_datum_to_json(&diff
, &c
->column
->type
));
1049 ovsdb_datum_destroy(&diff
, &c
->column
->type
);
1051 if (!ovsdb_datum_is_default(&new[index
], &c
->column
->type
)) {
1052 json_object_put(diff_json
, c
->column
->name
,
1053 ovsdb_datum_to_json(&new[index
],
1059 op
= type
== OJMS_INITIAL
? "initial"
1060 : type
== OJMS_MODIFY
? "modify" : "insert";
1061 json_object_put(row_update2
, op
, diff_json
);
1068 ovsdb_monitor_max_columns(struct ovsdb_monitor
*dbmon
)
1070 struct shash_node
*node
;
1071 size_t max_columns
= 0;
1073 SHASH_FOR_EACH (node
, &dbmon
->tables
) {
1074 struct ovsdb_monitor_table
*mt
= node
->data
;
1076 max_columns
= MAX(max_columns
, mt
->n_columns
);
1083 ovsdb_monitor_add_json_row(struct json
**json
, const char *table_name
,
1084 struct json
**table_json
, struct json
*row_json
,
1085 const struct uuid
*row_uuid
)
1087 char uuid
[UUID_LEN
+ 1];
1089 /* Create JSON object for transaction overall. */
1091 *json
= json_object_create();
1094 /* Create JSON object for transaction on this table. */
1096 *table_json
= json_object_create();
1097 json_object_put(*json
, table_name
, *table_json
);
1100 /* Add JSON row to JSON table. */
1101 snprintf(uuid
, sizeof uuid
, UUID_FMT
, UUID_ARGS(row_uuid
));
1102 json_object_put(*table_json
, uuid
, row_json
);
1105 /* Constructs and returns JSON for a <table-updates> object (as described in
1106 * RFC 7047) for all the outstanding changes within 'monitor', starting from
1109 ovsdb_monitor_compose_update(
1110 struct ovsdb_monitor
*dbmon
,
1111 bool initial
, struct ovsdb_monitor_change_set
*mcs
,
1112 const struct ovsdb_monitor_session_condition
*condition
,
1113 compose_row_update_cb_func row_update
)
1116 size_t max_columns
= ovsdb_monitor_max_columns(dbmon
);
1117 unsigned long int *changed
= xmalloc(bitmap_n_bytes(max_columns
));
1120 struct ovsdb_monitor_change_set_for_table
*mcst
;
1121 LIST_FOR_EACH (mcst
, list_in_change_set
, &mcs
->change_set_for_tables
) {
1122 struct ovsdb_monitor_row
*row
, *next
;
1123 struct json
*table_json
= NULL
;
1124 struct ovsdb_monitor_table
*mt
= mcst
->mt
;
1126 HMAP_FOR_EACH_SAFE (row
, next
, hmap_node
, &mcst
->rows
) {
1127 struct json
*row_json
;
1128 row_json
= (*row_update
)(mt
, condition
, OVSDB_MONITOR_ROW
, row
,
1129 initial
, changed
, mcst
->n_columns
);
1131 ovsdb_monitor_add_json_row(&json
, mt
->table
->schema
->name
,
1132 &table_json
, row_json
,
1143 ovsdb_monitor_compose_cond_change_update(
1144 struct ovsdb_monitor
*dbmon
,
1145 struct ovsdb_monitor_session_condition
*condition
)
1147 struct shash_node
*node
;
1148 struct json
*json
= NULL
;
1149 size_t max_columns
= ovsdb_monitor_max_columns(dbmon
);
1150 unsigned long int *changed
= xmalloc(bitmap_n_bytes(max_columns
));
1152 SHASH_FOR_EACH (node
, &dbmon
->tables
) {
1153 struct ovsdb_monitor_table
*mt
= node
->data
;
1154 struct ovsdb_row
*row
;
1155 struct json
*table_json
= NULL
;
1156 struct ovsdb_condition
*old_condition
, *new_condition
;
1158 if (!ovsdb_monitor_get_table_conditions(mt
,
1162 !ovsdb_condition_cmp_3way(old_condition
, new_condition
)) {
1163 /* Nothing to update on this table */
1167 /* Iterate over all rows in table */
1168 HMAP_FOR_EACH (row
, hmap_node
, &mt
->table
->rows
) {
1169 struct json
*row_json
;
1171 row_json
= ovsdb_monitor_compose_row_update2(mt
, condition
,
1176 ovsdb_monitor_add_json_row(&json
, mt
->table
->schema
->name
,
1177 &table_json
, row_json
,
1178 ovsdb_row_get_uuid(row
));
1181 ovsdb_monitor_table_condition_updated(mt
, condition
);
1188 /* Returns JSON for a <table-updates> object (as described in RFC 7047)
1189 * for all the outstanding changes in dbmon that are tracked by the change set
1192 * If cond_updated is true all rows in the db that match conditions will be
1195 * The caller should specify 'initial' as true if the returned JSON is going to
1196 * be used as part of the initial reply to a "monitor" request, false if it is
1197 * going to be used as part of an "update" notification. */
1199 ovsdb_monitor_get_update(
1200 struct ovsdb_monitor
*dbmon
,
1201 bool initial
, bool cond_updated
,
1202 struct ovsdb_monitor_session_condition
*condition
,
1203 enum ovsdb_monitor_version version
,
1204 struct ovsdb_monitor_change_set
**p_mcs
)
1206 struct ovsdb_monitor_json_cache_node
*cache_node
= NULL
;
1208 struct ovsdb_monitor_change_set
*mcs
= *p_mcs
;
1210 ovs_assert(cond_updated
? mcs
== dbmon
->new_change_set
: true);
1212 /* Return a clone of cached json if one exists. Otherwise,
1213 * generate a new one and add it to the cache. */
1214 if (!condition
|| (!condition
->conditional
&& !cond_updated
)) {
1215 cache_node
= ovsdb_monitor_json_cache_search(dbmon
, version
,
1219 json
= cache_node
->json
? json_clone(cache_node
->json
) : NULL
;
1221 if (version
== OVSDB_MONITOR_V1
) {
1223 ovsdb_monitor_compose_update(dbmon
, initial
, mcs
,
1225 ovsdb_monitor_compose_row_update
);
1227 ovs_assert(version
== OVSDB_MONITOR_V2
||
1228 version
== OVSDB_MONITOR_V3
);
1230 if (!cond_updated
) {
1231 json
= ovsdb_monitor_compose_update(dbmon
, initial
, mcs
,
1233 ovsdb_monitor_compose_row_update2
);
1234 if (!condition
|| !condition
->conditional
) {
1235 ovsdb_monitor_json_cache_insert(dbmon
, version
, mcs
,
1239 /* Compose update on whole db due to condition update.
1240 Session must be flushed (change list is empty)*/
1242 ovsdb_monitor_compose_cond_change_update(dbmon
, condition
);
1247 /* Maintain tracking change set. */
1248 ovsdb_monitor_untrack_change_set(dbmon
, mcs
);
1249 ovsdb_monitor_track_new_change_set(dbmon
);
1250 *p_mcs
= dbmon
->new_change_set
;
1256 ovsdb_monitor_needs_flush(struct ovsdb_monitor
*dbmon
,
1257 struct ovsdb_monitor_change_set
*change_set
)
1259 ovs_assert(change_set
);
1260 return (change_set
!= dbmon
->new_change_set
);
1264 ovsdb_monitor_table_add_select(struct ovsdb_monitor
*dbmon
,
1265 const struct ovsdb_table
*table
,
1266 enum ovsdb_monitor_selection select
)
1268 struct ovsdb_monitor_table
* mt
;
1270 mt
= shash_find_data(&dbmon
->tables
, table
->schema
->name
);
1271 mt
->select
|= select
;
1275 * If a row's change type (insert, delete or modify) matches that of
1276 * the monitor, they should be sent to the monitor's clients as updates.
1277 * Of cause, the monitor should also internally update with this change.
1279 * When a change type does not require client side update, the monitor
1280 * may still need to keep track of certain changes in order to generate
1281 * correct future updates. For example, the monitor internal state should
1282 * be updated whenever a new row is inserted, in order to generate the
1283 * correct initial state, regardless if a insert change type is being
1286 * On the other hand, if a transaction only contains changes to columns
1287 * that are not monitored, this transaction can be safely ignored by the
1290 * Thus, the order of the declaration is important:
1291 * 'OVSDB_CHANGES_REQUIRE_EXTERNAL_UPDATE' always implies
1292 * 'OVSDB_CHANGES_REQUIRE_INTERNAL_UPDATE', but not vice versa. */
1293 enum ovsdb_monitor_changes_efficacy
{
1294 OVSDB_CHANGES_NO_EFFECT
, /* Monitor does not care about this
1296 OVSDB_CHANGES_REQUIRE_INTERNAL_UPDATE
, /* Monitor internal updates. */
1297 OVSDB_CHANGES_REQUIRE_EXTERNAL_UPDATE
, /* Client needs to be updated. */
1300 struct ovsdb_monitor_aux
{
1301 const struct ovsdb_monitor
*monitor
;
1302 struct ovsdb_monitor_table
*mt
;
1303 enum ovsdb_monitor_changes_efficacy efficacy
;
1307 ovsdb_monitor_init_aux(struct ovsdb_monitor_aux
*aux
,
1308 const struct ovsdb_monitor
*m
)
1312 aux
->efficacy
= OVSDB_CHANGES_NO_EFFECT
;
1316 ovsdb_monitor_changes_update(const struct ovsdb_row
*old
,
1317 const struct ovsdb_row
*new,
1318 const struct ovsdb_monitor_table
*mt
,
1319 struct ovsdb_monitor_change_set_for_table
*mcst
)
1321 const struct uuid
*uuid
= ovsdb_row_get_uuid(new ? new : old
);
1322 struct ovsdb_monitor_row
*change
;
1324 change
= ovsdb_monitor_changes_row_find(mcst
, uuid
);
1326 change
= xzalloc(sizeof *change
);
1327 hmap_insert(&mcst
->rows
, &change
->hmap_node
, uuid_hash(uuid
));
1328 change
->uuid
= *uuid
;
1329 change
->old
= clone_monitor_row_data(mt
, old
, mcst
->n_columns
);
1330 change
->new = clone_monitor_row_data(mt
, new, mcst
->n_columns
);
1334 /* Reinsert the row that was just deleted.
1336 * This path won't be hit without replication. Whenever OVSDB
1337 * server inserts a new row, It always generates a new UUID
1338 * that is different from the row just deleted.
1340 * With replication, this path can be hit in a corner
1341 * case when two OVSDB servers are set up to replicate
1342 * each other. Not that is a useful set up, but can
1343 * happen in practice.
1345 * An example of how this path can be hit is documented below.
1346 * The details is not as important to the correctness of the
1347 * logic, but added here to convince ourselves that this path
1350 * Imagine two OVSDB servers that replicates from each
1351 * other. For each replication session, there is a
1352 * corresponding monitor at the other end of the replication
1353 * JSONRPC connection.
1355 * The events can lead to a back to back deletion and
1356 * insertion operation of the same row for the monitor of
1357 * the first server are:
1359 * 1. A row is inserted in the first OVSDB server.
1360 * 2. The row is then replicated to the remote OVSDB server.
1361 * 3. The row is now deleted by the local OVSDB server. This
1362 * deletion operation is replicated to the local monitor
1363 * of the OVSDB server.
1364 * 4. The monitor now receives the same row, as an insertion,
1365 * from the replication server. Because of
1366 * replication, the row carries the same UUID as the row
1369 change
->new = clone_monitor_row_data(mt
, new, mcst
->n_columns
);
1371 update_monitor_row_data(mt
, new, change
->new, mcst
->n_columns
);
1374 free_monitor_row_data(mt
, change
->new, mcst
->n_columns
);
1378 /* This row was added then deleted. Forget about it. */
1379 hmap_remove(&mcst
->rows
, &change
->hmap_node
);
1387 ovsdb_monitor_columns_changed(const struct ovsdb_monitor_table
*mt
,
1388 const unsigned long int *changed
)
1392 for (i
= 0; i
< mt
->n_columns
; i
++) {
1393 size_t column_index
= mt
->columns
[i
].column
->index
;
1395 if (bitmap_is_set(changed
, column_index
)) {
1403 /* Return the efficacy of a row's change to a monitor table.
1405 * Please see the block comment above 'ovsdb_monitor_changes_efficacy'
1406 * definition form more information. */
1407 static enum ovsdb_monitor_changes_efficacy
1408 ovsdb_monitor_changes_classify(enum ovsdb_monitor_selection type
,
1409 const struct ovsdb_monitor_table
*mt
,
1410 const unsigned long int *changed
)
1412 if (type
== OJMS_MODIFY
&&
1413 !ovsdb_monitor_columns_changed(mt
, changed
)) {
1414 return OVSDB_CHANGES_NO_EFFECT
;
1417 if (type
== OJMS_MODIFY
) {
1418 /* Condition might turn a modify operation to insert or delete */
1419 type
|= OJMS_INSERT
| OJMS_DELETE
;
1422 return (mt
->select
& type
)
1423 ? OVSDB_CHANGES_REQUIRE_EXTERNAL_UPDATE
1424 : OVSDB_CHANGES_REQUIRE_INTERNAL_UPDATE
;
1428 ovsdb_monitor_change_cb(const struct ovsdb_row
*old
,
1429 const struct ovsdb_row
*new,
1430 const unsigned long int *changed
,
1433 struct ovsdb_monitor_aux
*aux
= aux_
;
1434 const struct ovsdb_monitor
*m
= aux
->monitor
;
1435 struct ovsdb_table
*table
= new ? new->table
: old
->table
;
1436 struct ovsdb_monitor_table
*mt
;
1437 struct ovsdb_monitor_change_set_for_table
*mcst
;
1439 if (!aux
->mt
|| table
!= aux
->mt
->table
) {
1440 aux
->mt
= shash_find_data(&m
->tables
, table
->schema
->name
);
1442 /* We don't care about rows in this table at all. Tell the caller
1449 enum ovsdb_monitor_selection type
=
1450 ovsdb_monitor_row_update_type(false, old
, new);
1451 enum ovsdb_monitor_changes_efficacy efficacy
=
1452 ovsdb_monitor_changes_classify(type
, mt
, changed
);
1454 if (efficacy
> OVSDB_CHANGES_NO_EFFECT
) {
1455 LIST_FOR_EACH (mcst
, list_in_mt
, &mt
->change_sets
) {
1456 ovsdb_monitor_changes_update(old
, new, mt
, mcst
);
1459 if (aux
->efficacy
< efficacy
) {
1460 aux
->efficacy
= efficacy
;
1467 ovsdb_monitor_get_initial(struct ovsdb_monitor
*dbmon
,
1468 struct ovsdb_monitor_change_set
**p_mcs
)
1470 if (!dbmon
->init_change_set
) {
1471 struct ovsdb_monitor_change_set
*change_set
=
1472 ovsdb_monitor_add_change_set(dbmon
, true, NULL
);
1473 dbmon
->init_change_set
= change_set
;
1475 struct ovsdb_monitor_change_set_for_table
*mcst
;
1476 LIST_FOR_EACH (mcst
, list_in_change_set
,
1477 &change_set
->change_set_for_tables
) {
1478 if (mcst
->mt
->select
& OJMS_INITIAL
) {
1479 struct ovsdb_row
*row
;
1480 HMAP_FOR_EACH (row
, hmap_node
, &mcst
->mt
->table
->rows
) {
1481 ovsdb_monitor_changes_update(NULL
, row
, mcst
->mt
, mcst
);
1486 dbmon
->init_change_set
->n_refs
++;
1489 *p_mcs
= dbmon
->init_change_set
;
1493 ovsdb_monitor_history_change_cb(const struct ovsdb_row
*old
,
1494 const struct ovsdb_row
*new,
1495 const unsigned long int *changed
,
1498 struct ovsdb_monitor_change_set
*change_set
= aux
;
1499 struct ovsdb_table
*table
= new ? new->table
: old
->table
;
1500 struct ovsdb_monitor_change_set_for_table
*mcst
;
1502 enum ovsdb_monitor_selection type
=
1503 ovsdb_monitor_row_update_type(false, old
, new);
1504 LIST_FOR_EACH (mcst
, list_in_change_set
,
1505 &change_set
->change_set_for_tables
) {
1506 if (mcst
->mt
->table
== table
) {
1507 enum ovsdb_monitor_changes_efficacy efficacy
=
1508 ovsdb_monitor_changes_classify(type
, mcst
->mt
, changed
);
1509 if (efficacy
> OVSDB_CHANGES_NO_EFFECT
) {
1510 ovsdb_monitor_changes_update(old
, new, mcst
->mt
, mcst
);
1519 ovsdb_monitor_get_changes_after(const struct uuid
*txn_uuid
,
1520 struct ovsdb_monitor
*dbmon
,
1521 struct ovsdb_monitor_change_set
**p_mcs
)
1523 ovs_assert(*p_mcs
== NULL
);
1524 ovs_assert(!uuid_is_zero(txn_uuid
));
1525 struct ovsdb_monitor_change_set
*change_set
=
1526 ovsdb_monitor_find_change_set(dbmon
, txn_uuid
);
1528 change_set
->n_refs
++;
1529 *p_mcs
= change_set
;
1533 struct ovsdb_txn_history_node
*h_node
;
1535 LIST_FOR_EACH (h_node
, node
, &dbmon
->db
->txn_history
) {
1536 struct ovsdb_txn
*txn
= h_node
->txn
;
1538 /* find the txn with last_id in history */
1539 if (uuid_equals(ovsdb_txn_get_txnid(txn
), txn_uuid
)) {
1541 change_set
= ovsdb_monitor_add_change_set(dbmon
, false,
1545 /* Already found. Add changes in each follow up transaction to
1546 * the new change_set. */
1547 ovsdb_txn_for_each_change(txn
, ovsdb_monitor_history_change_cb
,
1551 *p_mcs
= change_set
;
1555 ovsdb_monitor_remove_jsonrpc_monitor(struct ovsdb_monitor
*dbmon
,
1556 struct ovsdb_jsonrpc_monitor
*jsonrpc_monitor
,
1557 struct ovsdb_monitor_change_set
*change_set
)
1559 struct jsonrpc_monitor_node
*jm
;
1561 if (ovs_list_is_empty(&dbmon
->jsonrpc_monitors
)) {
1562 ovsdb_monitor_destroy(dbmon
);
1566 /* Find and remove the jsonrpc monitor from the list. */
1567 LIST_FOR_EACH(jm
, node
, &dbmon
->jsonrpc_monitors
) {
1568 if (jm
->jsonrpc_monitor
== jsonrpc_monitor
) {
1569 /* Release the tracked changes. */
1571 ovsdb_monitor_untrack_change_set(dbmon
, change_set
);
1573 ovs_list_remove(&jm
->node
);
1576 /* Destroy ovsdb monitor if this is the last user. */
1577 if (ovs_list_is_empty(&dbmon
->jsonrpc_monitors
)) {
1578 ovsdb_monitor_destroy(dbmon
);
1585 /* Should never reach here. jsonrpc_monitor should be on the list. */
1590 ovsdb_monitor_table_equal(const struct ovsdb_monitor_table
*a
,
1591 const struct ovsdb_monitor_table
*b
)
1595 ovs_assert(b
->n_columns
== b
->n_monitored_columns
);
1597 if ((a
->table
!= b
->table
) ||
1598 (a
->select
!= b
->select
) ||
1599 (a
->n_monitored_columns
!= b
->n_monitored_columns
)) {
1603 /* Compare only monitored columns that must be sorted already */
1604 for (i
= 0; i
< a
->n_monitored_columns
; i
++) {
1605 if ((a
->columns
[i
].column
!= b
->columns
[i
].column
) ||
1606 (a
->columns
[i
].select
!= b
->columns
[i
].select
)) {
1614 ovsdb_monitor_equal(const struct ovsdb_monitor
*a
,
1615 const struct ovsdb_monitor
*b
)
1617 struct shash_node
*node
;
1619 if (shash_count(&a
->tables
) != shash_count(&b
->tables
)) {
1623 SHASH_FOR_EACH(node
, &a
->tables
) {
1624 const struct ovsdb_monitor_table
*mta
= node
->data
;
1625 const struct ovsdb_monitor_table
*mtb
;
1627 mtb
= shash_find_data(&b
->tables
, node
->name
);
1632 if (!ovsdb_monitor_table_equal(mta
, mtb
)) {
1641 ovsdb_monitor_hash(const struct ovsdb_monitor
*dbmon
, size_t basis
)
1643 const struct shash_node
**nodes
;
1646 nodes
= shash_sort(&dbmon
->tables
);
1647 n
= shash_count(&dbmon
->tables
);
1649 for (i
= 0; i
< n
; i
++) {
1650 struct ovsdb_monitor_table
*mt
= nodes
[i
]->data
;
1652 basis
= hash_pointer(mt
->table
, basis
);
1653 basis
= hash_3words(mt
->select
, mt
->n_columns
, basis
);
1655 for (j
= 0; j
< mt
->n_columns
; j
++) {
1656 basis
= hash_pointer(mt
->columns
[j
].column
, basis
);
1657 basis
= hash_2words(mt
->columns
[j
].select
, basis
);
1665 struct ovsdb_monitor
*
1666 ovsdb_monitor_add(struct ovsdb_monitor
*new_dbmon
)
1668 struct ovsdb_monitor
*dbmon
;
1671 /* New_dbmon should be associated with only one jsonrpc
1673 ovs_assert(ovs_list_is_singleton(&new_dbmon
->jsonrpc_monitors
));
1675 ovsdb_monitor_columns_sort(new_dbmon
);
1677 hash
= ovsdb_monitor_hash(new_dbmon
, 0);
1678 HMAP_FOR_EACH_WITH_HASH(dbmon
, hmap_node
, hash
, &ovsdb_monitors
) {
1679 if (ovsdb_monitor_equal(dbmon
, new_dbmon
)) {
1684 hmap_insert(&ovsdb_monitors
, &new_dbmon
->hmap_node
, hash
);
1689 ovsdb_monitor_destroy(struct ovsdb_monitor
*dbmon
)
1691 struct shash_node
*node
;
1693 ovs_list_remove(&dbmon
->list_node
);
1695 if (!hmap_node_is_null(&dbmon
->hmap_node
)) {
1696 hmap_remove(&ovsdb_monitors
, &dbmon
->hmap_node
);
1699 ovsdb_monitor_json_cache_flush(dbmon
);
1700 hmap_destroy(&dbmon
->json_cache
);
1702 struct ovsdb_monitor_change_set
*cs
, *cs_next
;
1703 LIST_FOR_EACH_SAFE (cs
, cs_next
, list_node
, &dbmon
->change_sets
) {
1704 ovsdb_monitor_change_set_destroy(cs
);
1707 SHASH_FOR_EACH (node
, &dbmon
->tables
) {
1708 struct ovsdb_monitor_table
*mt
= node
->data
;
1709 ovs_assert(ovs_list_is_empty(&mt
->change_sets
));
1711 free(mt
->columns_index_map
);
1714 shash_destroy(&dbmon
->tables
);
1719 ovsdb_monitor_commit(struct ovsdb_monitor
*m
, const struct ovsdb_txn
*txn
)
1721 struct ovsdb_monitor_aux aux
;
1723 ovsdb_monitor_init_aux(&aux
, m
);
1724 ovsdb_txn_for_each_change(txn
, ovsdb_monitor_change_cb
, &aux
);
1726 if (aux
.efficacy
> OVSDB_CHANGES_NO_EFFECT
) {
1727 /* The transaction is has impact to the monitor.
1728 * Reset new_change_set, so that a new change set will be
1729 * created for future trackings. */
1730 m
->new_change_set
= NULL
;
1732 if (aux
.efficacy
== OVSDB_CHANGES_REQUIRE_EXTERNAL_UPDATE
) {
1733 ovsdb_monitor_json_cache_flush(m
);
1739 ovsdb_monitors_commit(struct ovsdb
*db
, const struct ovsdb_txn
*txn
)
1741 struct ovsdb_monitor
*m
;
1743 LIST_FOR_EACH (m
, list_node
, &db
->monitors
) {
1744 ovsdb_monitor_commit(m
, txn
);
1749 ovsdb_monitors_remove(struct ovsdb
*db
)
1751 struct ovsdb_monitor
*m
, *next_m
;
1753 LIST_FOR_EACH_SAFE (m
, next_m
, list_node
, &db
->monitors
) {
1754 struct jsonrpc_monitor_node
*jm
, *next_jm
;
1756 /* Delete all front-end monitors. Removing the last front-end monitor
1757 * will also destroy the corresponding ovsdb_monitor. */
1758 LIST_FOR_EACH_SAFE (jm
, next_jm
, node
, &m
->jsonrpc_monitors
) {
1759 ovsdb_jsonrpc_monitor_destroy(jm
->jsonrpc_monitor
, false);
1764 /* Add some memory usage statics for monitors into 'usage', for use with
1765 * memory_report(). */
1767 ovsdb_monitor_get_memory_usage(struct simap
*usage
)
1769 struct ovsdb_monitor
*dbmon
;
1770 simap_put(usage
, "monitors", hmap_count(&ovsdb_monitors
));
1772 HMAP_FOR_EACH(dbmon
, hmap_node
, &ovsdb_monitors
) {
1773 simap_increase(usage
, "json-caches", hmap_count(&dbmon
->json_cache
));
1778 ovsdb_monitor_prereplace_db(struct ovsdb
*db
)
1780 struct ovsdb_monitor
*m
, *next_m
;
1782 LIST_FOR_EACH_SAFE (m
, next_m
, list_node
, &db
->monitors
) {
1783 struct jsonrpc_monitor_node
*jm
, *next_jm
;
1785 /* Delete all front-end monitors. Removing the last front-end monitor
1786 * will also destroy the corresponding ovsdb_monitor. */
1787 LIST_FOR_EACH_SAFE (jm
, next_jm
, node
, &m
->jsonrpc_monitors
) {
1788 ovsdb_jsonrpc_monitor_destroy(jm
->jsonrpc_monitor
, true);
1794 ovsdb_monitor_get_last_txnid(struct ovsdb_monitor
*dbmon
) {
1795 static struct uuid dummy
= { .parts
= { 0, 0, 0, 0 } };
1796 if (dbmon
->db
->n_txn_history
) {
1797 struct ovsdb_txn_history_node
*thn
= CONTAINER_OF(
1798 ovs_list_back(&dbmon
->db
->txn_history
),
1799 struct ovsdb_txn_history_node
, node
);
1800 return ovsdb_txn_get_txnid(thn
->txn
);