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"
35 #include "transaction.h"
36 #include "jsonrpc-server.h"
39 #include "openvswitch/vlog.h"
41 VLOG_DEFINE_THIS_MODULE(ovsdb_monitor
);
43 static struct hmap ovsdb_monitors
= HMAP_INITIALIZER(&ovsdb_monitors
);
45 /* Keep state of session's conditions */
46 struct ovsdb_monitor_session_condition
{
47 bool conditional
; /* True iff every table's condition is true. */
48 struct shash tables
; /* Contains
49 * "struct ovsdb_monitor_table_condition *"s. */
52 /* Monitored table session's conditions */
53 struct ovsdb_monitor_table_condition
{
54 const struct ovsdb_table
*table
;
55 struct ovsdb_monitor_table
*mt
;
56 struct ovsdb_condition old_condition
;
57 struct ovsdb_condition new_condition
;
62 * ovsdb_monitor keep track of the ovsdb changes.
65 /* A collection of tables being monitored. */
66 struct ovsdb_monitor
{
67 struct ovs_list list_node
; /* In struct ovsdb's "monitors" list. */
68 struct shash tables
; /* Holds "struct ovsdb_monitor_table"s. */
69 struct ovs_list jsonrpc_monitors
; /* Contains "jsonrpc_monitor_node"s. */
72 /* Contains "ovsdb_monitor_change_set". Each change set contains changes
73 * from some start point up to the latest committed transaction. There can
74 * be different change sets for the same struct ovsdb_monitor because there
75 * are different clients pending on changes starting from different points.
76 * The different change sets are maintained as a list. */
77 struct ovs_list change_sets
;
79 /* The new change set that is to be populated for future transactions. */
80 struct ovsdb_monitor_change_set
*new_change_set
;
82 /* The change set that starts from the first transaction of the DB, which
83 * is used for populating the initial data for new clients. */
84 struct ovsdb_monitor_change_set
*init_change_set
;
86 struct hmap_node hmap_node
; /* Elements within ovsdb_monitors. */
87 struct hmap json_cache
; /* Contains "ovsdb_monitor_json_cache_node"s.*/
90 /* A json object of updates for the ovsdb_monitor_change_set and the given
92 struct ovsdb_monitor_json_cache_node
{
93 struct hmap_node hmap_node
; /* Elements in json cache. */
94 enum ovsdb_monitor_version version
;
95 struct uuid change_set_uuid
;
96 struct json
*json
; /* Null, or a cloned of json */
99 struct jsonrpc_monitor_node
{
100 struct ovs_list node
;
101 struct ovsdb_jsonrpc_monitor
*jsonrpc_monitor
;
104 /* A particular column being monitored. */
105 struct ovsdb_monitor_column
{
106 const struct ovsdb_column
*column
;
107 enum ovsdb_monitor_selection select
;
111 /* A row that has changed in a monitored table. */
112 struct ovsdb_monitor_row
{
113 struct hmap_node hmap_node
; /* In ovsdb_monitor_change_set_for_table. */
114 struct uuid uuid
; /* UUID of row that changed. */
115 struct ovsdb_datum
*old
; /* Old data, NULL for an inserted row. */
116 struct ovsdb_datum
*new; /* New data, NULL for a deleted row. */
119 /* Contains a set of changes that are not yet flushed to all the jsonrpc
122 * 'n_refs' represent the number of jsonrpc connections that depend on this
123 * change set (have not received updates). Generate the update for the last
124 * jsonprc connection will also destroy the whole "struct
125 * ovsdb_monitor_change_set" object.
127 struct ovsdb_monitor_change_set
{
128 /* Element in change_sets of ovsdb_monitor. */
129 struct ovs_list list_node
;
131 /* Internally generated uuid that identifies this data structure. */
134 /* Contains struct ovsdb_monitor_change_set_for_table. */
135 struct ovs_list change_set_for_tables
;
139 /* The previous txn id before this change set's start point. */
140 struct uuid prev_txn
;
143 /* Contains 'struct ovsdb_monitor_row's for rows in a specific table
144 * of struct ovsdb_monitor_change_set. It can also be searched from
145 * member 'change_sets' of struct ovsdb_monitor_table. */
146 struct ovsdb_monitor_change_set_for_table
{
147 /* Element in ovsdb_monitor_tables' change_sets list. */
148 struct ovs_list list_in_mt
;
150 /* Element in ovsdb_monitor_change_sets' change_set_for_tables list. */
151 struct ovs_list list_in_change_set
;
153 struct ovsdb_monitor_table
*mt
;
154 struct ovsdb_monitor_change_set
*mcs
;
156 /* Contains struct ovsdb_monitor_row. */
159 /* Save the mt->n_columns that is used when creating the changes.
160 * It can be different from the current mt->n_columns because
161 * mt->n_columns can be increased when there are condition changes
162 * from any of the clients sharing the dbmon. */
166 /* A particular table being monitored. */
167 struct ovsdb_monitor_table
{
168 const struct ovsdb_table
*table
;
170 /* This is the union (bitwise-OR) of the 'select' values in all of the
171 * members of 'columns' below. */
172 enum ovsdb_monitor_selection select
;
174 /* Columns being monitored. */
175 struct ovsdb_monitor_column
*columns
;
177 size_t n_monitored_columns
;
178 size_t allocated_columns
;
180 /* Columns in ovsdb_monitor_row have different indexes then in
181 * ovsdb_row. This field maps between column->index to the index in the
182 * ovsdb_monitor_row. It is used for condition evaluation. */
183 unsigned int *columns_index_map
;
185 /* Contains 'ovsdb_monitor_change_set_for_table'. */
186 struct ovs_list change_sets
;
189 enum ovsdb_monitor_row_type
{
194 typedef struct json
*
195 (*compose_row_update_cb_func
)
196 (const struct ovsdb_monitor_table
*mt
,
197 const struct ovsdb_monitor_session_condition
* condition
,
198 enum ovsdb_monitor_row_type row_type
,
200 bool initial
, unsigned long int *changed
,
203 static void ovsdb_monitor_destroy(struct ovsdb_monitor
*);
204 static struct ovsdb_monitor_change_set
* ovsdb_monitor_add_change_set(
205 struct ovsdb_monitor
*, bool init_only
, const struct uuid
*prev_txn
);
206 static struct ovsdb_monitor_change_set
* ovsdb_monitor_find_change_set(
207 const struct ovsdb_monitor
*, const struct uuid
*prev_txn
);
208 static void ovsdb_monitor_change_set_destroy(
209 struct ovsdb_monitor_change_set
*);
210 static void ovsdb_monitor_track_new_change_set(struct ovsdb_monitor
*);
213 json_cache_hash(enum ovsdb_monitor_version version
,
214 struct ovsdb_monitor_change_set
*change_set
)
216 return hash_uint64_basis(version
, uuid_hash(&change_set
->uuid
));
219 static struct ovsdb_monitor_json_cache_node
*
220 ovsdb_monitor_json_cache_search(const struct ovsdb_monitor
*dbmon
,
221 enum ovsdb_monitor_version version
,
222 struct ovsdb_monitor_change_set
*change_set
)
224 struct ovsdb_monitor_json_cache_node
*node
;
225 uint32_t hash
= json_cache_hash(version
, change_set
);
227 HMAP_FOR_EACH_WITH_HASH(node
, hmap_node
, hash
, &dbmon
->json_cache
) {
228 if (uuid_equals(&node
->change_set_uuid
, &change_set
->uuid
) &&
229 node
->version
== version
) {
238 ovsdb_monitor_json_cache_insert(struct ovsdb_monitor
*dbmon
,
239 enum ovsdb_monitor_version version
,
240 struct ovsdb_monitor_change_set
*change_set
,
243 struct ovsdb_monitor_json_cache_node
*node
;
244 uint32_t hash
= json_cache_hash(version
, change_set
);
246 node
= xmalloc(sizeof *node
);
248 node
->version
= version
;
249 node
->change_set_uuid
= change_set
->uuid
;
250 node
->json
= json
? json_clone(json
) : NULL
;
252 hmap_insert(&dbmon
->json_cache
, &node
->hmap_node
, hash
);
256 ovsdb_monitor_json_cache_flush(struct ovsdb_monitor
*dbmon
)
258 struct ovsdb_monitor_json_cache_node
*node
;
260 HMAP_FOR_EACH_POP(node
, hmap_node
, &dbmon
->json_cache
) {
261 json_destroy(node
->json
);
266 /* Free all versions of json cache for a given change_set.*/
268 ovsdb_monitor_json_cache_destroy(struct ovsdb_monitor
*dbmon
,
269 struct ovsdb_monitor_change_set
*change_set
)
271 enum ovsdb_monitor_version v
;
272 for (v
= OVSDB_MONITOR_V1
; v
< OVSDB_MONITOR_VERSION_MAX
; v
++) {
273 struct ovsdb_monitor_json_cache_node
*node
274 = ovsdb_monitor_json_cache_search(dbmon
, v
, change_set
);
276 hmap_remove(&dbmon
->json_cache
, &node
->hmap_node
);
277 json_destroy(node
->json
);
284 compare_ovsdb_monitor_column(const void *a_
, const void *b_
)
286 const struct ovsdb_monitor_column
*a
= a_
;
287 const struct ovsdb_monitor_column
*b
= b_
;
289 /* put all monitored columns at the begining */
290 if (a
->monitored
!= b
->monitored
) {
291 return a
->monitored
? -1 : 1;
294 return a
->column
< b
->column
? -1 : a
->column
> b
->column
;
297 /* Finds and returns the ovsdb_monitor_row in 'mt->changes->rows' for the
298 * given 'uuid', or NULL if there is no such row. */
299 static struct ovsdb_monitor_row
*
300 ovsdb_monitor_changes_row_find(
301 const struct ovsdb_monitor_change_set_for_table
*changes
,
302 const struct uuid
*uuid
)
304 struct ovsdb_monitor_row
*row
;
306 HMAP_FOR_EACH_WITH_HASH (row
, hmap_node
, uuid_hash(uuid
),
308 if (uuid_equals(uuid
, &row
->uuid
)) {
315 /* Allocates an array of 'n_columns' ovsdb_datums and initializes them as
316 * copies of the data in 'row' drawn from the columns represented by
317 * mt->columns[]. Returns the array.
319 * If 'row' is NULL, returns NULL. */
320 static struct ovsdb_datum
*
321 clone_monitor_row_data(const struct ovsdb_monitor_table
*mt
,
322 const struct ovsdb_row
*row
,
325 struct ovsdb_datum
*data
;
332 data
= xmalloc(n_columns
* sizeof *data
);
333 for (i
= 0; i
< n_columns
; i
++) {
334 const struct ovsdb_column
*c
= mt
->columns
[i
].column
;
335 const struct ovsdb_datum
*src
= &row
->fields
[c
->index
];
336 struct ovsdb_datum
*dst
= &data
[i
];
337 const struct ovsdb_type
*type
= &c
->type
;
339 ovsdb_datum_clone(dst
, src
, type
);
344 /* Replaces the n_columns ovsdb_datums in row[] by copies of the data from
345 * in 'row' drawn from the columns represented by mt->columns[]. */
347 update_monitor_row_data(const struct ovsdb_monitor_table
*mt
,
348 const struct ovsdb_row
*row
,
349 struct ovsdb_datum
*data
,
354 for (i
= 0; i
< n_columns
; i
++) {
355 const struct ovsdb_column
*c
= mt
->columns
[i
].column
;
356 const struct ovsdb_datum
*src
= &row
->fields
[c
->index
];
357 struct ovsdb_datum
*dst
= &data
[i
];
358 const struct ovsdb_type
*type
= &c
->type
;
360 if (!ovsdb_datum_equals(src
, dst
, type
)) {
361 ovsdb_datum_destroy(dst
, type
);
362 ovsdb_datum_clone(dst
, src
, type
);
367 /* Frees all of the n_columns ovsdb_datums in data[], using the types taken
368 * from mt->columns[], plus 'data' itself. */
370 free_monitor_row_data(const struct ovsdb_monitor_table
*mt
,
371 struct ovsdb_datum
*data
,
377 for (i
= 0; i
< n_columns
; i
++) {
378 const struct ovsdb_column
*c
= mt
->columns
[i
].column
;
380 ovsdb_datum_destroy(&data
[i
], &c
->type
);
386 /* Frees 'row', which must have been created from 'mt'. */
388 ovsdb_monitor_row_destroy(const struct ovsdb_monitor_table
*mt
,
389 struct ovsdb_monitor_row
*row
,
393 free_monitor_row_data(mt
, row
->old
, n_columns
);
394 free_monitor_row_data(mt
, row
->new, n_columns
);
400 ovsdb_monitor_columns_sort(struct ovsdb_monitor
*dbmon
)
403 struct shash_node
*node
;
405 SHASH_FOR_EACH (node
, &dbmon
->tables
) {
406 struct ovsdb_monitor_table
*mt
= node
->data
;
408 qsort(mt
->columns
, mt
->n_columns
, sizeof *mt
->columns
,
409 compare_ovsdb_monitor_column
);
410 for (i
= 0; i
< mt
->n_columns
; i
++) {
411 /* re-set index map due to sort */
412 mt
->columns_index_map
[mt
->columns
[i
].column
->index
] = i
;
418 ovsdb_monitor_add_jsonrpc_monitor(struct ovsdb_monitor
*dbmon
,
419 struct ovsdb_jsonrpc_monitor
*jsonrpc_monitor
)
421 struct jsonrpc_monitor_node
*jm
;
423 jm
= xzalloc(sizeof *jm
);
424 jm
->jsonrpc_monitor
= jsonrpc_monitor
;
425 ovs_list_push_back(&dbmon
->jsonrpc_monitors
, &jm
->node
);
428 struct ovsdb_monitor
*
429 ovsdb_monitor_create(struct ovsdb
*db
,
430 struct ovsdb_jsonrpc_monitor
*jsonrpc_monitor
)
432 struct ovsdb_monitor
*dbmon
;
434 dbmon
= xzalloc(sizeof *dbmon
);
436 ovs_list_push_back(&db
->monitors
, &dbmon
->list_node
);
437 ovs_list_init(&dbmon
->jsonrpc_monitors
);
439 ovs_list_init(&dbmon
->change_sets
);
440 shash_init(&dbmon
->tables
);
441 hmap_node_nullify(&dbmon
->hmap_node
);
442 hmap_init(&dbmon
->json_cache
);
444 ovsdb_monitor_add_jsonrpc_monitor(dbmon
, jsonrpc_monitor
);
449 ovsdb_monitor_add_table(struct ovsdb_monitor
*m
,
450 const struct ovsdb_table
*table
)
452 struct ovsdb_monitor_table
*mt
;
454 size_t n_columns
= shash_count(&table
->schema
->columns
);
456 mt
= xzalloc(sizeof *mt
);
458 shash_add(&m
->tables
, table
->schema
->name
, mt
);
459 ovs_list_init(&mt
->change_sets
);
460 mt
->columns_index_map
=
461 xmalloc(sizeof *mt
->columns_index_map
* n_columns
);
462 for (i
= 0; i
< n_columns
; i
++) {
463 mt
->columns_index_map
[i
] = -1;
468 ovsdb_monitor_add_column(struct ovsdb_monitor
*dbmon
,
469 const struct ovsdb_table
*table
,
470 const struct ovsdb_column
*column
,
471 enum ovsdb_monitor_selection select
,
474 struct ovsdb_monitor_table
*mt
;
475 struct ovsdb_monitor_column
*c
;
477 mt
= shash_find_data(&dbmon
->tables
, table
->schema
->name
);
479 /* Check for column duplication. Return duplicated column name. */
480 if (mt
->columns_index_map
[column
->index
] != -1) {
484 if (mt
->n_columns
>= mt
->allocated_columns
) {
485 mt
->columns
= x2nrealloc(mt
->columns
, &mt
->allocated_columns
,
486 sizeof *mt
->columns
);
489 mt
->select
|= select
;
490 mt
->columns_index_map
[column
->index
] = mt
->n_columns
;
491 c
= &mt
->columns
[mt
->n_columns
++];
494 c
->monitored
= monitored
;
496 mt
->n_monitored_columns
++;
503 ovsdb_monitor_condition_add_columns(struct ovsdb_monitor
*dbmon
,
504 const struct ovsdb_table
*table
,
505 struct ovsdb_condition
*condition
)
509 const struct ovsdb_column
**columns
=
510 ovsdb_condition_get_columns(condition
, &n_columns
);
512 for (i
= 0; i
< n_columns
; i
++) {
513 ovsdb_monitor_add_column(dbmon
, table
, columns
[i
],
520 /* Bind this session's condition to ovsdb_monitor */
522 ovsdb_monitor_condition_bind(struct ovsdb_monitor
*dbmon
,
523 struct ovsdb_monitor_session_condition
*cond
)
525 struct shash_node
*node
;
527 SHASH_FOR_EACH(node
, &cond
->tables
) {
528 struct ovsdb_monitor_table_condition
*mtc
= node
->data
;
529 struct ovsdb_monitor_table
*mt
=
530 shash_find_data(&dbmon
->tables
, mtc
->table
->schema
->name
);
533 ovsdb_monitor_condition_add_columns(dbmon
, mtc
->table
,
534 &mtc
->new_condition
);
539 ovsdb_monitor_table_exists(struct ovsdb_monitor
*m
,
540 const struct ovsdb_table
*table
)
542 return shash_find_data(&m
->tables
, table
->schema
->name
);
545 static struct ovsdb_monitor_change_set
*
546 ovsdb_monitor_add_change_set(struct ovsdb_monitor
*dbmon
,
547 bool init_only
, const struct uuid
*prev_txn
)
549 struct ovsdb_monitor_change_set
*change_set
= xzalloc(sizeof *change_set
);
550 change_set
->uuid
= uuid_random();
551 ovs_list_push_back(&(dbmon
->change_sets
), &change_set
->list_node
);
552 ovs_list_init(&change_set
->change_set_for_tables
);
553 change_set
->n_refs
= 1;
554 change_set
->prev_txn
= prev_txn
? *prev_txn
: UUID_ZERO
;
556 struct shash_node
*node
;
557 SHASH_FOR_EACH (node
, &dbmon
->tables
) {
558 struct ovsdb_monitor_table
*mt
= node
->data
;
559 if (!init_only
|| (mt
->select
& OJMS_INITIAL
)) {
560 struct ovsdb_monitor_change_set_for_table
*mcst
=
561 xzalloc(sizeof *mcst
);
563 mcst
->n_columns
= mt
->n_columns
;
564 mcst
->mcs
= change_set
;
565 hmap_init(&mcst
->rows
);
566 ovs_list_push_back(&mt
->change_sets
, &mcst
->list_in_mt
);
567 ovs_list_push_back(&change_set
->change_set_for_tables
,
568 &mcst
->list_in_change_set
);
575 static struct ovsdb_monitor_change_set
*
576 ovsdb_monitor_find_change_set(const struct ovsdb_monitor
*dbmon
,
577 const struct uuid
*prev_txn
)
579 struct ovsdb_monitor_change_set
*cs
;
580 LIST_FOR_EACH (cs
, list_node
, &dbmon
->change_sets
) {
581 if (uuid_equals(&cs
->prev_txn
, prev_txn
)) {
582 /* Check n_columns for each table in dbmon, in case it is changed
583 * after the change set is populated. */
584 bool n_col_is_equal
= true;
585 struct ovsdb_monitor_change_set_for_table
*mcst
;
586 LIST_FOR_EACH (mcst
, list_in_change_set
,
587 &cs
->change_set_for_tables
) {
588 struct ovsdb_monitor_table
*mt
= mcst
->mt
;
589 if (mt
->n_columns
!= mcst
->n_columns
) {
590 n_col_is_equal
= false;
594 if (n_col_is_equal
) {
603 ovsdb_monitor_untrack_change_set(struct ovsdb_monitor
*dbmon
,
604 struct ovsdb_monitor_change_set
*mcs
)
607 if (--mcs
->n_refs
== 0) {
608 if (mcs
== dbmon
->init_change_set
) {
609 dbmon
->init_change_set
= NULL
;
610 } else if (mcs
== dbmon
->new_change_set
) {
611 dbmon
->new_change_set
= NULL
;
613 ovsdb_monitor_json_cache_destroy(dbmon
, mcs
);
614 ovsdb_monitor_change_set_destroy(mcs
);
619 ovsdb_monitor_track_new_change_set(struct ovsdb_monitor
*dbmon
)
621 struct ovsdb_monitor_change_set
*change_set
= dbmon
->new_change_set
;
624 change_set
->n_refs
++;
626 change_set
= ovsdb_monitor_add_change_set(dbmon
, false,
627 ovsdb_monitor_get_last_txnid(dbmon
));
628 dbmon
->new_change_set
= change_set
;
633 ovsdb_monitor_change_set_destroy(struct ovsdb_monitor_change_set
*mcs
)
635 ovs_list_remove(&mcs
->list_node
);
637 struct ovsdb_monitor_change_set_for_table
*mcst
, *next_mcst
;
638 LIST_FOR_EACH_SAFE (mcst
, next_mcst
, list_in_change_set
,
639 &mcs
->change_set_for_tables
) {
640 ovs_list_remove(&mcst
->list_in_change_set
);
641 ovs_list_remove(&mcst
->list_in_mt
);
643 struct ovsdb_monitor_row
*row
, *next
;
644 HMAP_FOR_EACH_SAFE (row
, next
, hmap_node
, &mcst
->rows
) {
645 hmap_remove(&mcst
->rows
, &row
->hmap_node
);
646 ovsdb_monitor_row_destroy(mcst
->mt
, row
, mcst
->n_columns
);
648 hmap_destroy(&mcst
->rows
);
655 static enum ovsdb_monitor_selection
656 ovsdb_monitor_row_update_type(bool initial
, const bool old
, const bool new)
658 return initial
? OJMS_INITIAL
664 /* Set conditional monitoring mode only if we have non-empty condition in one
665 * of the tables at least */
667 ovsdb_monitor_session_condition_set_mode(
668 struct ovsdb_monitor_session_condition
*cond
)
670 struct shash_node
*node
;
672 SHASH_FOR_EACH (node
, &cond
->tables
) {
673 struct ovsdb_monitor_table_condition
*mtc
= node
->data
;
675 if (!ovsdb_condition_is_true(&mtc
->new_condition
)) {
676 cond
->conditional
= true;
680 cond
->conditional
= false;
683 /* Returnes an empty allocated session's condition state holder */
684 struct ovsdb_monitor_session_condition
*
685 ovsdb_monitor_session_condition_create(void)
687 struct ovsdb_monitor_session_condition
*condition
=
688 xzalloc(sizeof *condition
);
690 condition
->conditional
= false;
691 shash_init(&condition
->tables
);
696 ovsdb_monitor_session_condition_destroy(
697 struct ovsdb_monitor_session_condition
*condition
)
699 struct shash_node
*node
, *next
;
705 SHASH_FOR_EACH_SAFE (node
, next
, &condition
->tables
) {
706 struct ovsdb_monitor_table_condition
*mtc
= node
->data
;
708 ovsdb_condition_destroy(&mtc
->new_condition
);
709 ovsdb_condition_destroy(&mtc
->old_condition
);
710 shash_delete(&condition
->tables
, node
);
713 shash_destroy(&condition
->tables
);
718 ovsdb_monitor_table_condition_create(
719 struct ovsdb_monitor_session_condition
*condition
,
720 const struct ovsdb_table
*table
,
721 const struct json
*json_cnd
)
723 struct ovsdb_monitor_table_condition
*mtc
;
724 struct ovsdb_error
*error
;
726 mtc
= xzalloc(sizeof *mtc
);
728 ovsdb_condition_init(&mtc
->old_condition
);
729 ovsdb_condition_init(&mtc
->new_condition
);
732 error
= ovsdb_condition_from_json(table
->schema
,
735 &mtc
->old_condition
);
742 shash_add(&condition
->tables
, table
->schema
->name
, mtc
);
743 /* On session startup old == new condition */
744 ovsdb_condition_clone(&mtc
->new_condition
, &mtc
->old_condition
);
745 ovsdb_monitor_session_condition_set_mode(condition
);
751 ovsdb_monitor_get_table_conditions(
752 const struct ovsdb_monitor_table
*mt
,
753 const struct ovsdb_monitor_session_condition
*condition
,
754 struct ovsdb_condition
**old_condition
,
755 struct ovsdb_condition
**new_condition
)
761 struct ovsdb_monitor_table_condition
*mtc
=
762 shash_find_data(&condition
->tables
, mt
->table
->schema
->name
);
767 *old_condition
= &mtc
->old_condition
;
768 *new_condition
= &mtc
->new_condition
;
774 ovsdb_monitor_table_condition_update(
775 struct ovsdb_monitor
*dbmon
,
776 struct ovsdb_monitor_session_condition
*condition
,
777 const struct ovsdb_table
*table
,
778 const struct json
*cond_json
)
784 struct ovsdb_monitor_table_condition
*mtc
=
785 shash_find_data(&condition
->tables
, table
->schema
->name
);
786 struct ovsdb_error
*error
;
787 struct ovsdb_condition cond
= OVSDB_CONDITION_INITIALIZER(&cond
);
789 error
= ovsdb_condition_from_json(table
->schema
, cond_json
,
794 ovsdb_condition_destroy(&mtc
->new_condition
);
795 ovsdb_condition_clone(&mtc
->new_condition
, &cond
);
796 ovsdb_condition_destroy(&cond
);
797 ovsdb_monitor_condition_add_columns(dbmon
,
799 &mtc
->new_condition
);
805 ovsdb_monitor_table_condition_updated(struct ovsdb_monitor_table
*mt
,
806 struct ovsdb_monitor_session_condition
*condition
)
808 struct ovsdb_monitor_table_condition
*mtc
=
809 shash_find_data(&condition
->tables
, mt
->table
->schema
->name
);
812 /* If conditional monitoring - set old condition to new condition */
813 if (ovsdb_condition_cmp_3way(&mtc
->old_condition
,
814 &mtc
->new_condition
)) {
815 ovsdb_condition_destroy(&mtc
->old_condition
);
816 ovsdb_condition_clone(&mtc
->old_condition
, &mtc
->new_condition
);
817 ovsdb_monitor_session_condition_set_mode(condition
);
822 static enum ovsdb_monitor_selection
823 ovsdb_monitor_row_update_type_condition(
824 const struct ovsdb_monitor_table
*mt
,
825 const struct ovsdb_monitor_session_condition
*condition
,
827 enum ovsdb_monitor_row_type row_type
,
828 const struct ovsdb_datum
*old
,
829 const struct ovsdb_datum
*new)
831 struct ovsdb_condition
*old_condition
, *new_condition
;
832 enum ovsdb_monitor_selection type
=
833 ovsdb_monitor_row_update_type(initial
, old
, new);
835 if (ovsdb_monitor_get_table_conditions(mt
,
839 bool old_cond
= !old
? false
840 : ovsdb_condition_empty_or_match_any(old
,
842 row_type
== OVSDB_MONITOR_ROW
?
843 mt
->columns_index_map
:
845 bool new_cond
= !new ? false
846 : ovsdb_condition_empty_or_match_any(new,
848 row_type
== OVSDB_MONITOR_ROW
?
849 mt
->columns_index_map
:
852 if (!old_cond
&& !new_cond
) {
864 type
= !old_cond
? OJMS_INSERT
: !new_cond
865 ? OJMS_DELETE
: OJMS_MODIFY
;
880 ovsdb_monitor_row_skip_update(const struct ovsdb_monitor_table
*mt
,
881 enum ovsdb_monitor_row_type row_type
,
882 const struct ovsdb_datum
*old
,
883 const struct ovsdb_datum
*new,
884 enum ovsdb_monitor_selection type
,
885 unsigned long int *changed
,
888 if (!(mt
->select
& type
)) {
892 if (type
== OJMS_MODIFY
) {
896 memset(changed
, 0, bitmap_n_bytes(n_columns
));
897 for (i
= 0; i
< n_columns
; i
++) {
898 const struct ovsdb_column
*c
= mt
->columns
[i
].column
;
899 size_t index
= row_type
== OVSDB_ROW
? c
->index
: i
;
900 if (!ovsdb_datum_equals(&old
[index
], &new[index
], &c
->type
)) {
901 bitmap_set1(changed
, i
);
906 /* No actual changes: presumably a row changed and then
907 * changed back later. */
915 /* Returns JSON for a <row-update> (as described in RFC 7047) for 'row' within
916 * 'mt', or NULL if no row update should be sent.
918 * The caller should specify 'initial' as true if the returned JSON is going to
919 * be used as part of the initial reply to a "monitor" request, false if it is
920 * going to be used as part of an "update" notification.
922 * 'changed' must be a scratch buffer for internal use that is at least
923 * bitmap_n_bytes(n_columns) bytes long. */
925 ovsdb_monitor_compose_row_update(
926 const struct ovsdb_monitor_table
*mt
,
927 const struct ovsdb_monitor_session_condition
*condition OVS_UNUSED
,
928 enum ovsdb_monitor_row_type row_type OVS_UNUSED
,
930 bool initial
, unsigned long int *changed
,
931 size_t n_columns OVS_UNUSED
)
933 const struct ovsdb_monitor_row
*row
= _row
;
934 enum ovsdb_monitor_selection type
;
935 struct json
*old_json
, *new_json
;
936 struct json
*row_json
;
939 ovs_assert(row_type
== OVSDB_MONITOR_ROW
);
940 type
= ovsdb_monitor_row_update_type(initial
, row
->old
, row
->new);
941 if (ovsdb_monitor_row_skip_update(mt
, row_type
, row
->old
,
942 row
->new, type
, changed
,
947 row_json
= json_object_create();
948 old_json
= new_json
= NULL
;
949 if (type
& (OJMS_DELETE
| OJMS_MODIFY
)) {
950 old_json
= json_object_create();
951 json_object_put(row_json
, "old", old_json
);
953 if (type
& (OJMS_INITIAL
| OJMS_INSERT
| OJMS_MODIFY
)) {
954 new_json
= json_object_create();
955 json_object_put(row_json
, "new", new_json
);
957 for (i
= 0; i
< mt
->n_monitored_columns
; i
++) {
958 const struct ovsdb_monitor_column
*c
= &mt
->columns
[i
];
960 if (!c
->monitored
|| !(type
& c
->select
)) {
961 /* We don't care about this type of change for this
962 * particular column (but we will care about it for some
967 if ((type
== OJMS_MODIFY
&& bitmap_is_set(changed
, i
))
968 || type
== OJMS_DELETE
) {
969 json_object_put(old_json
, c
->column
->name
,
970 ovsdb_datum_to_json(&row
->old
[i
],
973 if (type
& (OJMS_INITIAL
| OJMS_INSERT
| OJMS_MODIFY
)) {
974 json_object_put(new_json
, c
->column
->name
,
975 ovsdb_datum_to_json(&row
->new[i
],
983 /* Returns JSON for a <row-update2> (as described in ovsdb-server(1) mapage)
984 * for 'row' within * 'mt', or NULL if no row update should be sent.
986 * The caller should specify 'initial' as true if the returned JSON is
987 * going to be used as part of the initial reply to a "monitor_cond" request,
988 * false if it is going to be used as part of an "update2" notification.
990 * 'changed' must be a scratch buffer for internal use that is at least
991 * bitmap_n_bytes(n_columns) bytes long. */
993 ovsdb_monitor_compose_row_update2(
994 const struct ovsdb_monitor_table
*mt
,
995 const struct ovsdb_monitor_session_condition
*condition
,
996 enum ovsdb_monitor_row_type row_type
,
998 bool initial
, unsigned long int *changed
,
1001 enum ovsdb_monitor_selection type
;
1002 struct json
*row_update2
, *diff_json
;
1003 const struct ovsdb_datum
*old
, *new;
1006 if (row_type
== OVSDB_MONITOR_ROW
) {
1007 old
= ((const struct ovsdb_monitor_row
*)_row
)->old
;;
1008 new = ((const struct ovsdb_monitor_row
*)_row
)->new;
1010 old
= new = ((const struct ovsdb_row
*)_row
)->fields
;
1013 type
= ovsdb_monitor_row_update_type_condition(mt
, condition
, initial
,
1014 row_type
, old
, new);
1015 if (ovsdb_monitor_row_skip_update(mt
, row_type
, old
, new, type
, changed
,
1020 row_update2
= json_object_create();
1021 if (type
== OJMS_DELETE
) {
1022 json_object_put(row_update2
, "delete", json_null_create());
1024 diff_json
= json_object_create();
1027 for (i
= 0; i
< mt
->n_monitored_columns
; i
++) {
1028 const struct ovsdb_monitor_column
*c
= &mt
->columns
[i
];
1029 size_t index
= row_type
== OVSDB_ROW
? c
->column
->index
: i
;
1030 if (!c
->monitored
|| !(type
& c
->select
)) {
1031 /* We don't care about this type of change for this
1032 * particular column (but we will care about it for some
1037 if (type
== OJMS_MODIFY
) {
1038 struct ovsdb_datum diff
;
1040 if (!bitmap_is_set(changed
, i
)) {
1044 ovsdb_datum_diff(&diff
,&old
[index
], &new[index
],
1046 json_object_put(diff_json
, c
->column
->name
,
1047 ovsdb_datum_to_json(&diff
, &c
->column
->type
));
1048 ovsdb_datum_destroy(&diff
, &c
->column
->type
);
1050 if (!ovsdb_datum_is_default(&new[index
], &c
->column
->type
)) {
1051 json_object_put(diff_json
, c
->column
->name
,
1052 ovsdb_datum_to_json(&new[index
],
1058 op
= type
== OJMS_INITIAL
? "initial"
1059 : type
== OJMS_MODIFY
? "modify" : "insert";
1060 json_object_put(row_update2
, op
, diff_json
);
1067 ovsdb_monitor_max_columns(struct ovsdb_monitor
*dbmon
)
1069 struct shash_node
*node
;
1070 size_t max_columns
= 0;
1072 SHASH_FOR_EACH (node
, &dbmon
->tables
) {
1073 struct ovsdb_monitor_table
*mt
= node
->data
;
1075 max_columns
= MAX(max_columns
, mt
->n_columns
);
1082 ovsdb_monitor_add_json_row(struct json
**json
, const char *table_name
,
1083 struct json
**table_json
, struct json
*row_json
,
1084 const struct uuid
*row_uuid
)
1086 char uuid
[UUID_LEN
+ 1];
1088 /* Create JSON object for transaction overall. */
1090 *json
= json_object_create();
1093 /* Create JSON object for transaction on this table. */
1095 *table_json
= json_object_create();
1096 json_object_put(*json
, table_name
, *table_json
);
1099 /* Add JSON row to JSON table. */
1100 snprintf(uuid
, sizeof uuid
, UUID_FMT
, UUID_ARGS(row_uuid
));
1101 json_object_put(*table_json
, uuid
, row_json
);
1104 /* Constructs and returns JSON for a <table-updates> object (as described in
1105 * RFC 7047) for all the outstanding changes within 'monitor', starting from
1108 ovsdb_monitor_compose_update(
1109 struct ovsdb_monitor
*dbmon
,
1110 bool initial
, struct ovsdb_monitor_change_set
*mcs
,
1111 const struct ovsdb_monitor_session_condition
*condition
,
1112 compose_row_update_cb_func row_update
)
1115 size_t max_columns
= ovsdb_monitor_max_columns(dbmon
);
1116 unsigned long int *changed
= xmalloc(bitmap_n_bytes(max_columns
));
1119 struct ovsdb_monitor_change_set_for_table
*mcst
;
1120 LIST_FOR_EACH (mcst
, list_in_change_set
, &mcs
->change_set_for_tables
) {
1121 struct ovsdb_monitor_row
*row
, *next
;
1122 struct json
*table_json
= NULL
;
1123 struct ovsdb_monitor_table
*mt
= mcst
->mt
;
1125 HMAP_FOR_EACH_SAFE (row
, next
, hmap_node
, &mcst
->rows
) {
1126 struct json
*row_json
;
1127 row_json
= (*row_update
)(mt
, condition
, OVSDB_MONITOR_ROW
, row
,
1128 initial
, changed
, mcst
->n_columns
);
1130 ovsdb_monitor_add_json_row(&json
, mt
->table
->schema
->name
,
1131 &table_json
, row_json
,
1142 ovsdb_monitor_compose_cond_change_update(
1143 struct ovsdb_monitor
*dbmon
,
1144 struct ovsdb_monitor_session_condition
*condition
)
1146 struct shash_node
*node
;
1147 struct json
*json
= NULL
;
1148 size_t max_columns
= ovsdb_monitor_max_columns(dbmon
);
1149 unsigned long int *changed
= xmalloc(bitmap_n_bytes(max_columns
));
1151 SHASH_FOR_EACH (node
, &dbmon
->tables
) {
1152 struct ovsdb_monitor_table
*mt
= node
->data
;
1153 struct ovsdb_row
*row
;
1154 struct json
*table_json
= NULL
;
1155 struct ovsdb_condition
*old_condition
, *new_condition
;
1157 if (!ovsdb_monitor_get_table_conditions(mt
,
1161 !ovsdb_condition_cmp_3way(old_condition
, new_condition
)) {
1162 /* Nothing to update on this table */
1166 /* Iterate over all rows in table */
1167 HMAP_FOR_EACH (row
, hmap_node
, &mt
->table
->rows
) {
1168 struct json
*row_json
;
1170 row_json
= ovsdb_monitor_compose_row_update2(mt
, condition
,
1175 ovsdb_monitor_add_json_row(&json
, mt
->table
->schema
->name
,
1176 &table_json
, row_json
,
1177 ovsdb_row_get_uuid(row
));
1180 ovsdb_monitor_table_condition_updated(mt
, condition
);
1187 /* Returns JSON for a <table-updates> object (as described in RFC 7047)
1188 * for all the outstanding changes in dbmon that are tracked by the change set
1191 * If cond_updated is true all rows in the db that match conditions will be
1194 * The caller should specify 'initial' as true if the returned JSON is going to
1195 * be used as part of the initial reply to a "monitor" request, false if it is
1196 * going to be used as part of an "update" notification. */
1198 ovsdb_monitor_get_update(
1199 struct ovsdb_monitor
*dbmon
,
1200 bool initial
, bool cond_updated
,
1201 struct ovsdb_monitor_session_condition
*condition
,
1202 enum ovsdb_monitor_version version
,
1203 struct ovsdb_monitor_change_set
**p_mcs
)
1205 struct ovsdb_monitor_json_cache_node
*cache_node
= NULL
;
1207 struct ovsdb_monitor_change_set
*mcs
= *p_mcs
;
1209 ovs_assert(cond_updated
? mcs
== dbmon
->new_change_set
: true);
1211 /* Return a clone of cached json if one exists. Otherwise,
1212 * generate a new one and add it to the cache. */
1213 if (!condition
|| (!condition
->conditional
&& !cond_updated
)) {
1214 cache_node
= ovsdb_monitor_json_cache_search(dbmon
, version
,
1218 json
= cache_node
->json
? json_clone(cache_node
->json
) : NULL
;
1220 if (version
== OVSDB_MONITOR_V1
) {
1222 ovsdb_monitor_compose_update(dbmon
, initial
, mcs
,
1224 ovsdb_monitor_compose_row_update
);
1226 ovs_assert(version
== OVSDB_MONITOR_V2
||
1227 version
== OVSDB_MONITOR_V3
);
1229 if (!cond_updated
) {
1230 json
= ovsdb_monitor_compose_update(dbmon
, initial
, mcs
,
1232 ovsdb_monitor_compose_row_update2
);
1233 if (!condition
|| !condition
->conditional
) {
1234 ovsdb_monitor_json_cache_insert(dbmon
, version
, mcs
,
1238 /* Compose update on whole db due to condition update.
1239 Session must be flushed (change list is empty)*/
1241 ovsdb_monitor_compose_cond_change_update(dbmon
, condition
);
1246 /* Maintain tracking change set. */
1247 ovsdb_monitor_untrack_change_set(dbmon
, mcs
);
1248 ovsdb_monitor_track_new_change_set(dbmon
);
1249 *p_mcs
= dbmon
->new_change_set
;
1255 ovsdb_monitor_needs_flush(struct ovsdb_monitor
*dbmon
,
1256 struct ovsdb_monitor_change_set
*change_set
)
1258 ovs_assert(change_set
);
1259 return (change_set
!= dbmon
->new_change_set
);
1263 ovsdb_monitor_table_add_select(struct ovsdb_monitor
*dbmon
,
1264 const struct ovsdb_table
*table
,
1265 enum ovsdb_monitor_selection select
)
1267 struct ovsdb_monitor_table
* mt
;
1269 mt
= shash_find_data(&dbmon
->tables
, table
->schema
->name
);
1270 mt
->select
|= select
;
1274 * If a row's change type (insert, delete or modify) matches that of
1275 * the monitor, they should be sent to the monitor's clients as updates.
1276 * Of cause, the monitor should also internally update with this change.
1278 * When a change type does not require client side update, the monitor
1279 * may still need to keep track of certain changes in order to generate
1280 * correct future updates. For example, the monitor internal state should
1281 * be updated whenever a new row is inserted, in order to generate the
1282 * correct initial state, regardless if a insert change type is being
1285 * On the other hand, if a transaction only contains changes to columns
1286 * that are not monitored, this transaction can be safely ignored by the
1289 * Thus, the order of the declaration is important:
1290 * 'OVSDB_CHANGES_REQUIRE_EXTERNAL_UPDATE' always implies
1291 * 'OVSDB_CHANGES_REQUIRE_INTERNAL_UPDATE', but not vice versa. */
1292 enum ovsdb_monitor_changes_efficacy
{
1293 OVSDB_CHANGES_NO_EFFECT
, /* Monitor does not care about this
1295 OVSDB_CHANGES_REQUIRE_INTERNAL_UPDATE
, /* Monitor internal updates. */
1296 OVSDB_CHANGES_REQUIRE_EXTERNAL_UPDATE
, /* Client needs to be updated. */
1299 struct ovsdb_monitor_aux
{
1300 const struct ovsdb_monitor
*monitor
;
1301 struct ovsdb_monitor_table
*mt
;
1302 enum ovsdb_monitor_changes_efficacy efficacy
;
1306 ovsdb_monitor_init_aux(struct ovsdb_monitor_aux
*aux
,
1307 const struct ovsdb_monitor
*m
)
1311 aux
->efficacy
= OVSDB_CHANGES_NO_EFFECT
;
1315 ovsdb_monitor_changes_update(const struct ovsdb_row
*old
,
1316 const struct ovsdb_row
*new,
1317 const struct ovsdb_monitor_table
*mt
,
1318 struct ovsdb_monitor_change_set_for_table
*mcst
)
1320 const struct uuid
*uuid
= ovsdb_row_get_uuid(new ? new : old
);
1321 struct ovsdb_monitor_row
*change
;
1323 change
= ovsdb_monitor_changes_row_find(mcst
, uuid
);
1325 change
= xzalloc(sizeof *change
);
1326 hmap_insert(&mcst
->rows
, &change
->hmap_node
, uuid_hash(uuid
));
1327 change
->uuid
= *uuid
;
1328 change
->old
= clone_monitor_row_data(mt
, old
, mcst
->n_columns
);
1329 change
->new = clone_monitor_row_data(mt
, new, mcst
->n_columns
);
1333 /* Reinsert the row that was just deleted.
1335 * This path won't be hit without replication. Whenever OVSDB
1336 * server inserts a new row, It always generates a new UUID
1337 * that is different from the row just deleted.
1339 * With replication, this path can be hit in a corner
1340 * case when two OVSDB servers are set up to replicate
1341 * each other. Not that is a useful set up, but can
1342 * happen in practice.
1344 * An example of how this path can be hit is documented below.
1345 * The details is not as important to the correctness of the
1346 * logic, but added here to convince ourselves that this path
1349 * Imagine two OVSDB servers that replicates from each
1350 * other. For each replication session, there is a
1351 * corresponding monitor at the other end of the replication
1352 * JSONRPC connection.
1354 * The events can lead to a back to back deletion and
1355 * insertion operation of the same row for the monitor of
1356 * the first server are:
1358 * 1. A row is inserted in the first OVSDB server.
1359 * 2. The row is then replicated to the remote OVSDB server.
1360 * 3. The row is now deleted by the local OVSDB server. This
1361 * deletion operation is replicated to the local monitor
1362 * of the OVSDB server.
1363 * 4. The monitor now receives the same row, as an insertion,
1364 * from the replication server. Because of
1365 * replication, the row carries the same UUID as the row
1368 change
->new = clone_monitor_row_data(mt
, new, mcst
->n_columns
);
1370 update_monitor_row_data(mt
, new, change
->new, mcst
->n_columns
);
1373 free_monitor_row_data(mt
, change
->new, mcst
->n_columns
);
1377 /* This row was added then deleted. Forget about it. */
1378 hmap_remove(&mcst
->rows
, &change
->hmap_node
);
1386 ovsdb_monitor_columns_changed(const struct ovsdb_monitor_table
*mt
,
1387 const unsigned long int *changed
)
1391 for (i
= 0; i
< mt
->n_columns
; i
++) {
1392 size_t column_index
= mt
->columns
[i
].column
->index
;
1394 if (bitmap_is_set(changed
, column_index
)) {
1402 /* Return the efficacy of a row's change to a monitor table.
1404 * Please see the block comment above 'ovsdb_monitor_changes_efficacy'
1405 * definition form more information. */
1406 static enum ovsdb_monitor_changes_efficacy
1407 ovsdb_monitor_changes_classify(enum ovsdb_monitor_selection type
,
1408 const struct ovsdb_monitor_table
*mt
,
1409 const unsigned long int *changed
)
1411 if (type
== OJMS_MODIFY
&&
1412 !ovsdb_monitor_columns_changed(mt
, changed
)) {
1413 return OVSDB_CHANGES_NO_EFFECT
;
1416 if (type
== OJMS_MODIFY
) {
1417 /* Condition might turn a modify operation to insert or delete */
1418 type
|= OJMS_INSERT
| OJMS_DELETE
;
1421 return (mt
->select
& type
)
1422 ? OVSDB_CHANGES_REQUIRE_EXTERNAL_UPDATE
1423 : OVSDB_CHANGES_REQUIRE_INTERNAL_UPDATE
;
1427 ovsdb_monitor_change_cb(const struct ovsdb_row
*old
,
1428 const struct ovsdb_row
*new,
1429 const unsigned long int *changed
,
1432 struct ovsdb_monitor_aux
*aux
= aux_
;
1433 const struct ovsdb_monitor
*m
= aux
->monitor
;
1434 struct ovsdb_table
*table
= new ? new->table
: old
->table
;
1435 struct ovsdb_monitor_table
*mt
;
1436 struct ovsdb_monitor_change_set_for_table
*mcst
;
1438 if (!aux
->mt
|| table
!= aux
->mt
->table
) {
1439 aux
->mt
= shash_find_data(&m
->tables
, table
->schema
->name
);
1441 /* We don't care about rows in this table at all. Tell the caller
1448 enum ovsdb_monitor_selection type
=
1449 ovsdb_monitor_row_update_type(false, old
, new);
1450 enum ovsdb_monitor_changes_efficacy efficacy
=
1451 ovsdb_monitor_changes_classify(type
, mt
, changed
);
1453 if (efficacy
> OVSDB_CHANGES_NO_EFFECT
) {
1454 LIST_FOR_EACH (mcst
, list_in_mt
, &mt
->change_sets
) {
1455 ovsdb_monitor_changes_update(old
, new, mt
, mcst
);
1458 if (aux
->efficacy
< efficacy
) {
1459 aux
->efficacy
= efficacy
;
1466 ovsdb_monitor_get_initial(struct ovsdb_monitor
*dbmon
,
1467 struct ovsdb_monitor_change_set
**p_mcs
)
1469 if (!dbmon
->init_change_set
) {
1470 struct ovsdb_monitor_change_set
*change_set
=
1471 ovsdb_monitor_add_change_set(dbmon
, true, NULL
);
1472 dbmon
->init_change_set
= change_set
;
1474 struct ovsdb_monitor_change_set_for_table
*mcst
;
1475 LIST_FOR_EACH (mcst
, list_in_change_set
,
1476 &change_set
->change_set_for_tables
) {
1477 if (mcst
->mt
->select
& OJMS_INITIAL
) {
1478 struct ovsdb_row
*row
;
1479 HMAP_FOR_EACH (row
, hmap_node
, &mcst
->mt
->table
->rows
) {
1480 ovsdb_monitor_changes_update(NULL
, row
, mcst
->mt
, mcst
);
1485 dbmon
->init_change_set
->n_refs
++;
1488 *p_mcs
= dbmon
->init_change_set
;
1492 ovsdb_monitor_history_change_cb(const struct ovsdb_row
*old
,
1493 const struct ovsdb_row
*new,
1494 const unsigned long int *changed
,
1497 struct ovsdb_monitor_change_set
*change_set
= aux
;
1498 struct ovsdb_table
*table
= new ? new->table
: old
->table
;
1499 struct ovsdb_monitor_change_set_for_table
*mcst
;
1501 enum ovsdb_monitor_selection type
=
1502 ovsdb_monitor_row_update_type(false, old
, new);
1503 LIST_FOR_EACH (mcst
, list_in_change_set
,
1504 &change_set
->change_set_for_tables
) {
1505 if (mcst
->mt
->table
== table
) {
1506 enum ovsdb_monitor_changes_efficacy efficacy
=
1507 ovsdb_monitor_changes_classify(type
, mcst
->mt
, changed
);
1508 if (efficacy
> OVSDB_CHANGES_NO_EFFECT
) {
1509 ovsdb_monitor_changes_update(old
, new, mcst
->mt
, mcst
);
1518 ovsdb_monitor_get_changes_after(const struct uuid
*txn_uuid
,
1519 struct ovsdb_monitor
*dbmon
,
1520 struct ovsdb_monitor_change_set
**p_mcs
)
1522 ovs_assert(*p_mcs
== NULL
);
1523 ovs_assert(!uuid_is_zero(txn_uuid
));
1524 struct ovsdb_monitor_change_set
*change_set
=
1525 ovsdb_monitor_find_change_set(dbmon
, txn_uuid
);
1527 change_set
->n_refs
++;
1528 *p_mcs
= change_set
;
1532 struct ovsdb_txn_history_node
*h_node
;
1534 LIST_FOR_EACH (h_node
, node
, &dbmon
->db
->txn_history
) {
1535 struct ovsdb_txn
*txn
= h_node
->txn
;
1537 /* find the txn with last_id in history */
1538 if (uuid_equals(ovsdb_txn_get_txnid(txn
), txn_uuid
)) {
1540 change_set
= ovsdb_monitor_add_change_set(dbmon
, false,
1544 /* Already found. Add changes in each follow up transaction to
1545 * the new change_set. */
1546 ovsdb_txn_for_each_change(txn
, ovsdb_monitor_history_change_cb
,
1550 *p_mcs
= change_set
;
1554 ovsdb_monitor_remove_jsonrpc_monitor(struct ovsdb_monitor
*dbmon
,
1555 struct ovsdb_jsonrpc_monitor
*jsonrpc_monitor
,
1556 struct ovsdb_monitor_change_set
*change_set
)
1558 struct jsonrpc_monitor_node
*jm
;
1560 if (ovs_list_is_empty(&dbmon
->jsonrpc_monitors
)) {
1561 ovsdb_monitor_destroy(dbmon
);
1565 /* Find and remove the jsonrpc monitor from the list. */
1566 LIST_FOR_EACH(jm
, node
, &dbmon
->jsonrpc_monitors
) {
1567 if (jm
->jsonrpc_monitor
== jsonrpc_monitor
) {
1568 /* Release the tracked changes. */
1570 ovsdb_monitor_untrack_change_set(dbmon
, change_set
);
1572 ovs_list_remove(&jm
->node
);
1575 /* Destroy ovsdb monitor if this is the last user. */
1576 if (ovs_list_is_empty(&dbmon
->jsonrpc_monitors
)) {
1577 ovsdb_monitor_destroy(dbmon
);
1584 /* Should never reach here. jsonrpc_monitor should be on the list. */
1589 ovsdb_monitor_table_equal(const struct ovsdb_monitor_table
*a
,
1590 const struct ovsdb_monitor_table
*b
)
1594 ovs_assert(b
->n_columns
== b
->n_monitored_columns
);
1596 if ((a
->table
!= b
->table
) ||
1597 (a
->select
!= b
->select
) ||
1598 (a
->n_monitored_columns
!= b
->n_monitored_columns
)) {
1602 /* Compare only monitored columns that must be sorted already */
1603 for (i
= 0; i
< a
->n_monitored_columns
; i
++) {
1604 if ((a
->columns
[i
].column
!= b
->columns
[i
].column
) ||
1605 (a
->columns
[i
].select
!= b
->columns
[i
].select
)) {
1613 ovsdb_monitor_equal(const struct ovsdb_monitor
*a
,
1614 const struct ovsdb_monitor
*b
)
1616 struct shash_node
*node
;
1618 if (shash_count(&a
->tables
) != shash_count(&b
->tables
)) {
1622 SHASH_FOR_EACH(node
, &a
->tables
) {
1623 const struct ovsdb_monitor_table
*mta
= node
->data
;
1624 const struct ovsdb_monitor_table
*mtb
;
1626 mtb
= shash_find_data(&b
->tables
, node
->name
);
1631 if (!ovsdb_monitor_table_equal(mta
, mtb
)) {
1640 ovsdb_monitor_hash(const struct ovsdb_monitor
*dbmon
, size_t basis
)
1642 const struct shash_node
**nodes
;
1645 nodes
= shash_sort(&dbmon
->tables
);
1646 n
= shash_count(&dbmon
->tables
);
1648 for (i
= 0; i
< n
; i
++) {
1649 struct ovsdb_monitor_table
*mt
= nodes
[i
]->data
;
1651 basis
= hash_pointer(mt
->table
, basis
);
1652 basis
= hash_3words(mt
->select
, mt
->n_columns
, basis
);
1654 for (j
= 0; j
< mt
->n_columns
; j
++) {
1655 basis
= hash_pointer(mt
->columns
[j
].column
, basis
);
1656 basis
= hash_2words(mt
->columns
[j
].select
, basis
);
1664 struct ovsdb_monitor
*
1665 ovsdb_monitor_add(struct ovsdb_monitor
*new_dbmon
)
1667 struct ovsdb_monitor
*dbmon
;
1670 /* New_dbmon should be associated with only one jsonrpc
1672 ovs_assert(ovs_list_is_singleton(&new_dbmon
->jsonrpc_monitors
));
1674 ovsdb_monitor_columns_sort(new_dbmon
);
1676 hash
= ovsdb_monitor_hash(new_dbmon
, 0);
1677 HMAP_FOR_EACH_WITH_HASH(dbmon
, hmap_node
, hash
, &ovsdb_monitors
) {
1678 if (ovsdb_monitor_equal(dbmon
, new_dbmon
)) {
1683 hmap_insert(&ovsdb_monitors
, &new_dbmon
->hmap_node
, hash
);
1688 ovsdb_monitor_destroy(struct ovsdb_monitor
*dbmon
)
1690 struct shash_node
*node
;
1692 ovs_list_remove(&dbmon
->list_node
);
1694 if (!hmap_node_is_null(&dbmon
->hmap_node
)) {
1695 hmap_remove(&ovsdb_monitors
, &dbmon
->hmap_node
);
1698 ovsdb_monitor_json_cache_flush(dbmon
);
1699 hmap_destroy(&dbmon
->json_cache
);
1701 struct ovsdb_monitor_change_set
*cs
, *cs_next
;
1702 LIST_FOR_EACH_SAFE (cs
, cs_next
, list_node
, &dbmon
->change_sets
) {
1703 ovsdb_monitor_change_set_destroy(cs
);
1706 SHASH_FOR_EACH (node
, &dbmon
->tables
) {
1707 struct ovsdb_monitor_table
*mt
= node
->data
;
1708 ovs_assert(ovs_list_is_empty(&mt
->change_sets
));
1710 free(mt
->columns_index_map
);
1713 shash_destroy(&dbmon
->tables
);
1718 ovsdb_monitor_commit(struct ovsdb_monitor
*m
, const struct ovsdb_txn
*txn
)
1720 struct ovsdb_monitor_aux aux
;
1722 ovsdb_monitor_init_aux(&aux
, m
);
1723 ovsdb_txn_for_each_change(txn
, ovsdb_monitor_change_cb
, &aux
);
1725 if (aux
.efficacy
> OVSDB_CHANGES_NO_EFFECT
) {
1726 /* The transaction is has impact to the monitor.
1727 * Reset new_change_set, so that a new change set will be
1728 * created for future trackings. */
1729 m
->new_change_set
= NULL
;
1731 if (aux
.efficacy
== OVSDB_CHANGES_REQUIRE_EXTERNAL_UPDATE
) {
1732 ovsdb_monitor_json_cache_flush(m
);
1738 ovsdb_monitors_commit(struct ovsdb
*db
, const struct ovsdb_txn
*txn
)
1740 struct ovsdb_monitor
*m
;
1742 LIST_FOR_EACH (m
, list_node
, &db
->monitors
) {
1743 ovsdb_monitor_commit(m
, txn
);
1748 ovsdb_monitors_remove(struct ovsdb
*db
)
1750 struct ovsdb_monitor
*m
, *next_m
;
1752 LIST_FOR_EACH_SAFE (m
, next_m
, list_node
, &db
->monitors
) {
1753 struct jsonrpc_monitor_node
*jm
, *next_jm
;
1755 /* Delete all front-end monitors. Removing the last front-end monitor
1756 * will also destroy the corresponding ovsdb_monitor. */
1757 LIST_FOR_EACH_SAFE (jm
, next_jm
, node
, &m
->jsonrpc_monitors
) {
1758 ovsdb_jsonrpc_monitor_destroy(jm
->jsonrpc_monitor
, false);
1763 /* Add some memory usage statics for monitors into 'usage', for use with
1764 * memory_report(). */
1766 ovsdb_monitor_get_memory_usage(struct simap
*usage
)
1768 struct ovsdb_monitor
*dbmon
;
1769 simap_put(usage
, "monitors", hmap_count(&ovsdb_monitors
));
1771 HMAP_FOR_EACH(dbmon
, hmap_node
, &ovsdb_monitors
) {
1772 simap_increase(usage
, "json-caches", hmap_count(&dbmon
->json_cache
));
1777 ovsdb_monitor_prereplace_db(struct ovsdb
*db
)
1779 struct ovsdb_monitor
*m
, *next_m
;
1781 LIST_FOR_EACH_SAFE (m
, next_m
, list_node
, &db
->monitors
) {
1782 struct jsonrpc_monitor_node
*jm
, *next_jm
;
1784 /* Delete all front-end monitors. Removing the last front-end monitor
1785 * will also destroy the corresponding ovsdb_monitor. */
1786 LIST_FOR_EACH_SAFE (jm
, next_jm
, node
, &m
->jsonrpc_monitors
) {
1787 ovsdb_jsonrpc_monitor_destroy(jm
->jsonrpc_monitor
, true);
1793 ovsdb_monitor_get_last_txnid(struct ovsdb_monitor
*dbmon
) {
1794 static struct uuid dummy
= { .parts
= { 0, 0, 0, 0 } };
1795 if (dbmon
->db
->n_txn_history
) {
1796 struct ovsdb_txn_history_node
*thn
= CONTAINER_OF(
1797 ovs_list_back(&dbmon
->db
->txn_history
),
1798 struct ovsdb_txn_history_node
, node
);
1799 return ovsdb_txn_get_txnid(thn
->txn
);