2 * Copyright (c) 2015 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
23 #include "dynamic-string.h"
26 #include "ovsdb-error.h"
27 #include "ovsdb-parser.h"
35 #include "transaction.h"
36 #include "jsonrpc-server.h"
38 #include "openvswitch/vlog.h"
41 static const struct ovsdb_replica_class ovsdb_jsonrpc_replica_class
;
42 static struct hmap ovsdb_monitors
= HMAP_INITIALIZER(&ovsdb_monitors
);
46 * ovsdb_monitor keep track of the ovsdb changes.
49 /* A collection of tables being monitored. */
50 struct ovsdb_monitor
{
51 struct ovsdb_replica replica
;
52 struct shash tables
; /* Holds "struct ovsdb_monitor_table"s. */
53 struct ovs_list jsonrpc_monitors
; /* Contains "jsonrpc_monitor_node"s. */
55 uint64_t n_transactions
; /* Count number of committed transactions. */
56 struct hmap_node hmap_node
; /* Elements within ovsdb_monitors. */
57 struct hmap json_cache
; /* Contains "ovsdb_monitor_json_cache_node"s.*/
60 /* A json object of updates between 'from_txn' and 'dbmon->n_transactions'
62 struct ovsdb_monitor_json_cache_node
{
63 struct hmap_node hmap_node
; /* Elements in json cache. */
65 struct json
*json
; /* Null, or a cloned of json */
68 struct jsonrpc_monitor_node
{
69 struct ovsdb_jsonrpc_monitor
*jsonrpc_monitor
;
73 /* A particular column being monitored. */
74 struct ovsdb_monitor_column
{
75 const struct ovsdb_column
*column
;
76 enum ovsdb_monitor_selection select
;
79 /* A row that has changed in a monitored table. */
80 struct ovsdb_monitor_row
{
81 struct hmap_node hmap_node
; /* In ovsdb_jsonrpc_monitor_table.changes. */
82 struct uuid uuid
; /* UUID of row that changed. */
83 struct ovsdb_datum
*old
; /* Old data, NULL for an inserted row. */
84 struct ovsdb_datum
*new; /* New data, NULL for a deleted row. */
87 /* Contains 'struct ovsdb_monitor_row's for rows that have been
88 * updated but not yet flushed to all the jsonrpc connection.
90 * 'n_refs' represent the number of jsonrpc connections that have
91 * not received updates. Generate the update for the last jsonprc
92 * connection will also destroy the whole "struct ovsdb_monitor_changes"
95 * 'transaction' stores the first update's transaction id.
97 struct ovsdb_monitor_changes
{
98 struct ovsdb_monitor_table
*mt
;
101 uint64_t transaction
;
102 struct hmap_node hmap_node
; /* Element in ovsdb_monitor_tables' changes
106 /* A particular table being monitored. */
107 struct ovsdb_monitor_table
{
108 const struct ovsdb_table
*table
;
110 /* This is the union (bitwise-OR) of the 'select' values in all of the
111 * members of 'columns' below. */
112 enum ovsdb_monitor_selection select
;
114 /* Columns being monitored. */
115 struct ovsdb_monitor_column
*columns
;
118 /* Contains 'ovsdb_monitor_changes' indexed by 'transaction'. */
122 static void ovsdb_monitor_destroy(struct ovsdb_monitor
*dbmon
);
123 static struct ovsdb_monitor_changes
* ovsdb_monitor_table_add_changes(
124 struct ovsdb_monitor_table
*mt
, uint64_t next_txn
);
125 static struct ovsdb_monitor_changes
*ovsdb_monitor_table_find_changes(
126 struct ovsdb_monitor_table
*mt
, uint64_t unflushed
);
127 static void ovsdb_monitor_changes_destroy(
128 struct ovsdb_monitor_changes
*changes
);
129 static void ovsdb_monitor_table_track_changes(struct ovsdb_monitor_table
*mt
,
132 static struct ovsdb_monitor_json_cache_node
*
133 ovsdb_monitor_json_cache_search(const struct ovsdb_monitor
*dbmon
,
136 struct ovsdb_monitor_json_cache_node
*node
;
137 uint32_t hash
= hash_uint64(from_txn
);
139 HMAP_FOR_EACH_WITH_HASH(node
, hmap_node
, hash
, &dbmon
->json_cache
) {
140 if (node
->from_txn
== from_txn
) {
149 ovsdb_monitor_json_cache_insert(struct ovsdb_monitor
*dbmon
,
150 uint64_t from_txn
, struct json
*json
)
152 struct ovsdb_monitor_json_cache_node
*node
;
155 node
= xmalloc(sizeof *node
);
157 hash
= hash_uint64(from_txn
);
158 node
->from_txn
= from_txn
;
159 node
->json
= json
? json_clone(json
) : NULL
;
161 hmap_insert(&dbmon
->json_cache
, &node
->hmap_node
, hash
);
165 ovsdb_monitor_json_cache_flush(struct ovsdb_monitor
*dbmon
)
167 struct ovsdb_monitor_json_cache_node
*node
, *next
;
169 HMAP_FOR_EACH_SAFE(node
, next
, hmap_node
, &dbmon
->json_cache
) {
170 hmap_remove(&dbmon
->json_cache
, &node
->hmap_node
);
171 json_destroy(node
->json
);
177 compare_ovsdb_monitor_column(const void *a_
, const void *b_
)
179 const struct ovsdb_monitor_column
*a
= a_
;
180 const struct ovsdb_monitor_column
*b
= b_
;
182 return a
->column
< b
->column
? -1 : a
->column
> b
->column
;
185 static struct ovsdb_monitor
*
186 ovsdb_monitor_cast(struct ovsdb_replica
*replica
)
188 ovs_assert(replica
->class == &ovsdb_jsonrpc_replica_class
);
189 return CONTAINER_OF(replica
, struct ovsdb_monitor
, replica
);
192 /* Finds and returns the ovsdb_monitor_row in 'mt->changes->rows' for the
193 * given 'uuid', or NULL if there is no such row. */
194 static struct ovsdb_monitor_row
*
195 ovsdb_monitor_changes_row_find(const struct ovsdb_monitor_changes
*changes
,
196 const struct uuid
*uuid
)
198 struct ovsdb_monitor_row
*row
;
200 HMAP_FOR_EACH_WITH_HASH (row
, hmap_node
, uuid_hash(uuid
),
202 if (uuid_equals(uuid
, &row
->uuid
)) {
209 /* Allocates an array of 'mt->n_columns' ovsdb_datums and initializes them as
210 * copies of the data in 'row' drawn from the columns represented by
211 * mt->columns[]. Returns the array.
213 * If 'row' is NULL, returns NULL. */
214 static struct ovsdb_datum
*
215 clone_monitor_row_data(const struct ovsdb_monitor_table
*mt
,
216 const struct ovsdb_row
*row
)
218 struct ovsdb_datum
*data
;
225 data
= xmalloc(mt
->n_columns
* sizeof *data
);
226 for (i
= 0; i
< mt
->n_columns
; i
++) {
227 const struct ovsdb_column
*c
= mt
->columns
[i
].column
;
228 const struct ovsdb_datum
*src
= &row
->fields
[c
->index
];
229 struct ovsdb_datum
*dst
= &data
[i
];
230 const struct ovsdb_type
*type
= &c
->type
;
232 ovsdb_datum_clone(dst
, src
, type
);
237 /* Replaces the mt->n_columns ovsdb_datums in row[] by copies of the data from
238 * in 'row' drawn from the columns represented by mt->columns[]. */
240 update_monitor_row_data(const struct ovsdb_monitor_table
*mt
,
241 const struct ovsdb_row
*row
,
242 struct ovsdb_datum
*data
)
246 for (i
= 0; i
< mt
->n_columns
; i
++) {
247 const struct ovsdb_column
*c
= mt
->columns
[i
].column
;
248 const struct ovsdb_datum
*src
= &row
->fields
[c
->index
];
249 struct ovsdb_datum
*dst
= &data
[i
];
250 const struct ovsdb_type
*type
= &c
->type
;
252 if (!ovsdb_datum_equals(src
, dst
, type
)) {
253 ovsdb_datum_destroy(dst
, type
);
254 ovsdb_datum_clone(dst
, src
, type
);
259 /* Frees all of the mt->n_columns ovsdb_datums in data[], using the types taken
260 * from mt->columns[], plus 'data' itself. */
262 free_monitor_row_data(const struct ovsdb_monitor_table
*mt
,
263 struct ovsdb_datum
*data
)
268 for (i
= 0; i
< mt
->n_columns
; i
++) {
269 const struct ovsdb_column
*c
= mt
->columns
[i
].column
;
271 ovsdb_datum_destroy(&data
[i
], &c
->type
);
277 /* Frees 'row', which must have been created from 'mt'. */
279 ovsdb_monitor_row_destroy(const struct ovsdb_monitor_table
*mt
,
280 struct ovsdb_monitor_row
*row
)
283 free_monitor_row_data(mt
, row
->old
);
284 free_monitor_row_data(mt
, row
->new);
290 ovsdb_monitor_add_jsonrpc_monitor(struct ovsdb_monitor
*dbmon
,
291 struct ovsdb_jsonrpc_monitor
*jsonrpc_monitor
)
293 struct jsonrpc_monitor_node
*jm
;
295 jm
= xzalloc(sizeof *jm
);
296 jm
->jsonrpc_monitor
= jsonrpc_monitor
;
297 list_push_back(&dbmon
->jsonrpc_monitors
, &jm
->node
);
300 struct ovsdb_monitor
*
301 ovsdb_monitor_create(struct ovsdb
*db
,
302 struct ovsdb_jsonrpc_monitor
*jsonrpc_monitor
)
304 struct ovsdb_monitor
*dbmon
;
306 dbmon
= xzalloc(sizeof *dbmon
);
308 ovsdb_replica_init(&dbmon
->replica
, &ovsdb_jsonrpc_replica_class
);
309 ovsdb_add_replica(db
, &dbmon
->replica
);
310 list_init(&dbmon
->jsonrpc_monitors
);
312 dbmon
->n_transactions
= 0;
313 shash_init(&dbmon
->tables
);
314 hmap_node_nullify(&dbmon
->hmap_node
);
315 hmap_init(&dbmon
->json_cache
);
317 ovsdb_monitor_add_jsonrpc_monitor(dbmon
, jsonrpc_monitor
);
322 ovsdb_monitor_add_table(struct ovsdb_monitor
*m
,
323 const struct ovsdb_table
*table
)
325 struct ovsdb_monitor_table
*mt
;
327 mt
= xzalloc(sizeof *mt
);
329 shash_add(&m
->tables
, table
->schema
->name
, mt
);
330 hmap_init(&mt
->changes
);
334 ovsdb_monitor_add_column(struct ovsdb_monitor
*dbmon
,
335 const struct ovsdb_table
*table
,
336 const struct ovsdb_column
*column
,
337 enum ovsdb_monitor_selection select
,
338 size_t *allocated_columns
)
340 struct ovsdb_monitor_table
*mt
;
341 struct ovsdb_monitor_column
*c
;
343 mt
= shash_find_data(&dbmon
->tables
, table
->schema
->name
);
345 if (mt
->n_columns
>= *allocated_columns
) {
346 mt
->columns
= x2nrealloc(mt
->columns
, allocated_columns
,
347 sizeof *mt
->columns
);
350 mt
->select
|= select
;
351 c
= &mt
->columns
[mt
->n_columns
++];
356 /* Check for duplicated column names. Return the first
357 * duplicated column's name if found. Otherwise return
359 const char * OVS_WARN_UNUSED_RESULT
360 ovsdb_monitor_table_check_duplicates(struct ovsdb_monitor
*m
,
361 const struct ovsdb_table
*table
)
363 struct ovsdb_monitor_table
*mt
;
366 mt
= shash_find_data(&m
->tables
, table
->schema
->name
);
369 /* Check for duplicate columns. */
370 qsort(mt
->columns
, mt
->n_columns
, sizeof *mt
->columns
,
371 compare_ovsdb_monitor_column
);
372 for (i
= 1; i
< mt
->n_columns
; i
++) {
373 if (mt
->columns
[i
].column
== mt
->columns
[i
- 1].column
) {
374 return mt
->columns
[i
].column
->name
;
382 static struct ovsdb_monitor_changes
*
383 ovsdb_monitor_table_add_changes(struct ovsdb_monitor_table
*mt
,
386 struct ovsdb_monitor_changes
*changes
;
388 changes
= xzalloc(sizeof *changes
);
390 changes
->transaction
= next_txn
;
393 hmap_init(&changes
->rows
);
394 hmap_insert(&mt
->changes
, &changes
->hmap_node
, hash_uint64(next_txn
));
399 static struct ovsdb_monitor_changes
*
400 ovsdb_monitor_table_find_changes(struct ovsdb_monitor_table
*mt
,
401 uint64_t transaction
)
403 struct ovsdb_monitor_changes
*changes
;
404 size_t hash
= hash_uint64(transaction
);
406 HMAP_FOR_EACH_WITH_HASH(changes
, hmap_node
, hash
, &mt
->changes
) {
407 if (changes
->transaction
== transaction
) {
415 /* Stop currently tracking changes to table 'mt' since 'transaction'.
417 * Return 'true' if the 'transaction' is being tracked. 'false' otherwise. */
419 ovsdb_monitor_table_untrack_changes(struct ovsdb_monitor_table
*mt
,
420 uint64_t transaction
)
422 struct ovsdb_monitor_changes
*changes
=
423 ovsdb_monitor_table_find_changes(mt
, transaction
);
425 if (--changes
->n_refs
== 0) {
426 hmap_remove(&mt
->changes
, &changes
->hmap_node
);
427 ovsdb_monitor_changes_destroy(changes
);
432 /* Start tracking changes to table 'mt' begins from 'transaction' inclusive.
435 ovsdb_monitor_table_track_changes(struct ovsdb_monitor_table
*mt
,
436 uint64_t transaction
)
438 struct ovsdb_monitor_changes
*changes
;
440 changes
= ovsdb_monitor_table_find_changes(mt
, transaction
);
444 ovsdb_monitor_table_add_changes(mt
, transaction
);
449 ovsdb_monitor_changes_destroy(struct ovsdb_monitor_changes
*changes
)
451 struct ovsdb_monitor_row
*row
, *next
;
453 HMAP_FOR_EACH_SAFE (row
, next
, hmap_node
, &changes
->rows
) {
454 hmap_remove(&changes
->rows
, &row
->hmap_node
);
455 ovsdb_monitor_row_destroy(changes
->mt
, row
);
457 hmap_destroy(&changes
->rows
);
461 /* Returns JSON for a <row-update> (as described in RFC 7047) for 'row' within
462 * 'mt', or NULL if no row update should be sent.
464 * The caller should specify 'initial' as true if the returned JSON is going to
465 * be used as part of the initial reply to a "monitor" request, false if it is
466 * going to be used as part of an "update" notification.
468 * 'changed' must be a scratch buffer for internal use that is at least
469 * bitmap_n_bytes(mt->n_columns) bytes long. */
471 ovsdb_monitor_compose_row_update(
472 const struct ovsdb_monitor_table
*mt
,
473 const struct ovsdb_monitor_row
*row
,
474 bool initial
, unsigned long int *changed
)
476 enum ovsdb_monitor_selection type
;
477 struct json
*old_json
, *new_json
;
478 struct json
*row_json
;
481 type
= (initial
? OJMS_INITIAL
482 : !row
->old
? OJMS_INSERT
483 : !row
->new ? OJMS_DELETE
485 if (!(mt
->select
& type
)) {
489 if (type
== OJMS_MODIFY
) {
493 memset(changed
, 0, bitmap_n_bytes(mt
->n_columns
));
494 for (i
= 0; i
< mt
->n_columns
; i
++) {
495 const struct ovsdb_column
*c
= mt
->columns
[i
].column
;
496 if (!ovsdb_datum_equals(&row
->old
[i
], &row
->new[i
], &c
->type
)) {
497 bitmap_set1(changed
, i
);
502 /* No actual changes: presumably a row changed and then
503 * changed back later. */
508 row_json
= json_object_create();
509 old_json
= new_json
= NULL
;
510 if (type
& (OJMS_DELETE
| OJMS_MODIFY
)) {
511 old_json
= json_object_create();
512 json_object_put(row_json
, "old", old_json
);
514 if (type
& (OJMS_INITIAL
| OJMS_INSERT
| OJMS_MODIFY
)) {
515 new_json
= json_object_create();
516 json_object_put(row_json
, "new", new_json
);
518 for (i
= 0; i
< mt
->n_columns
; i
++) {
519 const struct ovsdb_monitor_column
*c
= &mt
->columns
[i
];
521 if (!(type
& c
->select
)) {
522 /* We don't care about this type of change for this
523 * particular column (but we will care about it for some
528 if ((type
== OJMS_MODIFY
&& bitmap_is_set(changed
, i
))
529 || type
== OJMS_DELETE
) {
530 json_object_put(old_json
, c
->column
->name
,
531 ovsdb_datum_to_json(&row
->old
[i
],
534 if (type
& (OJMS_INITIAL
| OJMS_INSERT
| OJMS_MODIFY
)) {
535 json_object_put(new_json
, c
->column
->name
,
536 ovsdb_datum_to_json(&row
->new[i
],
544 /* Constructs and returns JSON for a <table-updates> object (as described in
545 * RFC 7047) for all the outstanding changes within 'monitor', starting from
548 ovsdb_monitor_compose_update(struct ovsdb_monitor
*dbmon
,
549 bool initial
, uint64_t transaction
)
551 struct shash_node
*node
;
552 unsigned long int *changed
;
557 SHASH_FOR_EACH (node
, &dbmon
->tables
) {
558 struct ovsdb_monitor_table
*mt
= node
->data
;
560 max_columns
= MAX(max_columns
, mt
->n_columns
);
562 changed
= xmalloc(bitmap_n_bytes(max_columns
));
565 SHASH_FOR_EACH (node
, &dbmon
->tables
) {
566 struct ovsdb_monitor_table
*mt
= node
->data
;
567 struct ovsdb_monitor_row
*row
, *next
;
568 struct ovsdb_monitor_changes
*changes
;
569 struct json
*table_json
= NULL
;
571 changes
= ovsdb_monitor_table_find_changes(mt
, transaction
);
576 HMAP_FOR_EACH_SAFE (row
, next
, hmap_node
, &changes
->rows
) {
577 struct json
*row_json
;
579 row_json
= ovsdb_monitor_compose_row_update(
580 mt
, row
, initial
, changed
);
582 char uuid
[UUID_LEN
+ 1];
584 /* Create JSON object for transaction overall. */
586 json
= json_object_create();
589 /* Create JSON object for transaction on this table. */
591 table_json
= json_object_create();
592 json_object_put(json
, mt
->table
->schema
->name
, table_json
);
595 /* Add JSON row to JSON table. */
596 snprintf(uuid
, sizeof uuid
, UUID_FMT
, UUID_ARGS(&row
->uuid
));
597 json_object_put(table_json
, uuid
, row_json
);
606 /* Returns JSON for a <table-updates> object (as described in RFC 7047)
607 * for all the outstanding changes within 'monitor' that starts from
608 * '*unflushed' transaction id.
610 * The caller should specify 'initial' as true if the returned JSON is going to
611 * be used as part of the initial reply to a "monitor" request, false if it is
612 * going to be used as part of an "update" notification. */
614 ovsdb_monitor_get_update(struct ovsdb_monitor
*dbmon
,
615 bool initial
, uint64_t *unflushed
)
617 struct ovsdb_monitor_json_cache_node
*cache_node
;
618 struct shash_node
*node
;
620 uint64_t prev_txn
= *unflushed
;
621 uint64_t next_txn
= dbmon
->n_transactions
+ 1;
623 /* Return a clone of cached json if one exists. Otherwise,
624 * generate a new one and add it to the cache. */
625 cache_node
= ovsdb_monitor_json_cache_search(dbmon
, prev_txn
);
627 json
= cache_node
->json
? json_clone(cache_node
->json
) : NULL
;
629 json
= ovsdb_monitor_compose_update(dbmon
, initial
, prev_txn
);
630 ovsdb_monitor_json_cache_insert(dbmon
, prev_txn
, json
);
633 /* Maintain transaction id of 'changes'. */
634 SHASH_FOR_EACH (node
, &dbmon
->tables
) {
635 struct ovsdb_monitor_table
*mt
= node
->data
;
637 ovsdb_monitor_table_untrack_changes(mt
, prev_txn
);
638 ovsdb_monitor_table_track_changes(mt
, next_txn
);
640 *unflushed
= next_txn
;
646 ovsdb_monitor_needs_flush(struct ovsdb_monitor
*dbmon
,
647 uint64_t next_transaction
)
649 ovs_assert(next_transaction
<= dbmon
->n_transactions
+ 1);
650 return (next_transaction
<= dbmon
->n_transactions
);
654 ovsdb_monitor_table_add_select(struct ovsdb_monitor
*dbmon
,
655 const struct ovsdb_table
*table
,
656 enum ovsdb_monitor_selection select
)
658 struct ovsdb_monitor_table
* mt
;
660 mt
= shash_find_data(&dbmon
->tables
, table
->schema
->name
);
661 mt
->select
|= select
;
664 struct ovsdb_monitor_aux
{
665 const struct ovsdb_monitor
*monitor
;
666 struct ovsdb_monitor_table
*mt
;
670 ovsdb_monitor_init_aux(struct ovsdb_monitor_aux
*aux
,
671 const struct ovsdb_monitor
*m
)
678 ovsdb_monitor_changes_update(const struct ovsdb_row
*old
,
679 const struct ovsdb_row
*new,
680 const struct ovsdb_monitor_table
*mt
,
681 struct ovsdb_monitor_changes
*changes
)
683 const struct uuid
*uuid
= ovsdb_row_get_uuid(new ? new : old
);
684 struct ovsdb_monitor_row
*change
;
686 change
= ovsdb_monitor_changes_row_find(changes
, uuid
);
688 change
= xzalloc(sizeof *change
);
689 hmap_insert(&changes
->rows
, &change
->hmap_node
, uuid_hash(uuid
));
690 change
->uuid
= *uuid
;
691 change
->old
= clone_monitor_row_data(mt
, old
);
692 change
->new = clone_monitor_row_data(mt
, new);
695 update_monitor_row_data(mt
, new, change
->new);
697 free_monitor_row_data(mt
, change
->new);
701 /* This row was added then deleted. Forget about it. */
702 hmap_remove(&changes
->rows
, &change
->hmap_node
);
710 ovsdb_monitor_change_cb(const struct ovsdb_row
*old
,
711 const struct ovsdb_row
*new,
712 const unsigned long int *changed OVS_UNUSED
,
715 struct ovsdb_monitor_aux
*aux
= aux_
;
716 const struct ovsdb_monitor
*m
= aux
->monitor
;
717 struct ovsdb_table
*table
= new ? new->table
: old
->table
;
718 struct ovsdb_monitor_table
*mt
;
719 struct ovsdb_monitor_changes
*changes
;
721 if (!aux
->mt
|| table
!= aux
->mt
->table
) {
722 aux
->mt
= shash_find_data(&m
->tables
, table
->schema
->name
);
724 /* We don't care about rows in this table at all. Tell the caller
731 HMAP_FOR_EACH(changes
, hmap_node
, &mt
->changes
) {
732 ovsdb_monitor_changes_update(old
, new, mt
, changes
);
738 ovsdb_monitor_get_initial(const struct ovsdb_monitor
*dbmon
)
740 struct ovsdb_monitor_aux aux
;
741 struct shash_node
*node
;
743 ovsdb_monitor_init_aux(&aux
, dbmon
);
744 SHASH_FOR_EACH (node
, &dbmon
->tables
) {
745 struct ovsdb_monitor_table
*mt
= node
->data
;
747 if (mt
->select
& OJMS_INITIAL
) {
748 struct ovsdb_row
*row
;
749 struct ovsdb_monitor_changes
*changes
;
751 changes
= ovsdb_monitor_table_find_changes(mt
, 0);
753 changes
= ovsdb_monitor_table_add_changes(mt
, 0);
754 HMAP_FOR_EACH (row
, hmap_node
, &mt
->table
->rows
) {
755 ovsdb_monitor_changes_update(NULL
, row
, mt
, changes
);
765 ovsdb_monitor_remove_jsonrpc_monitor(struct ovsdb_monitor
*dbmon
,
766 struct ovsdb_jsonrpc_monitor
*jsonrpc_monitor
)
768 struct jsonrpc_monitor_node
*jm
;
770 if (list_is_empty(&dbmon
->jsonrpc_monitors
)) {
771 ovsdb_monitor_destroy(dbmon
);
775 /* Find and remove the jsonrpc monitor from the list. */
776 LIST_FOR_EACH(jm
, node
, &dbmon
->jsonrpc_monitors
) {
777 if (jm
->jsonrpc_monitor
== jsonrpc_monitor
) {
778 list_remove(&jm
->node
);
781 /* Destroy ovsdb monitor if this is the last user. */
782 if (list_is_empty(&dbmon
->jsonrpc_monitors
)) {
783 ovsdb_monitor_destroy(dbmon
);
790 /* Should never reach here. jsonrpc_monitor should be on the list. */
795 ovsdb_monitor_table_equal(const struct ovsdb_monitor_table
*a
,
796 const struct ovsdb_monitor_table
*b
)
800 if ((a
->table
!= b
->table
) ||
801 (a
->select
!= b
->select
) ||
802 (a
->n_columns
!= b
->n_columns
)) {
806 for (i
= 0; i
< a
->n_columns
; i
++) {
807 if ((a
->columns
[i
].column
!= b
->columns
[i
].column
) ||
808 (a
->columns
[i
].select
!= b
->columns
[i
].select
)) {
817 ovsdb_monitor_equal(const struct ovsdb_monitor
*a
,
818 const struct ovsdb_monitor
*b
)
820 struct shash_node
*node
;
822 if (shash_count(&a
->tables
) != shash_count(&b
->tables
)) {
826 SHASH_FOR_EACH(node
, &a
->tables
) {
827 const struct ovsdb_monitor_table
*mta
= node
->data
;
828 const struct ovsdb_monitor_table
*mtb
;
830 mtb
= shash_find_data(&b
->tables
, node
->name
);
835 if (!ovsdb_monitor_table_equal(mta
, mtb
)) {
844 ovsdb_monitor_hash(const struct ovsdb_monitor
*dbmon
, size_t basis
)
846 const struct shash_node
**nodes
;
849 nodes
= shash_sort(&dbmon
->tables
);
850 n
= shash_count(&dbmon
->tables
);
852 for (i
= 0; i
< n
; i
++) {
853 struct ovsdb_monitor_table
*mt
= nodes
[i
]->data
;
855 basis
= hash_pointer(mt
->table
, basis
);
856 basis
= hash_3words(mt
->select
, mt
->n_columns
, basis
);
858 for (j
= 0; j
< mt
->n_columns
; j
++) {
859 basis
= hash_pointer(mt
->columns
[j
].column
, basis
);
860 basis
= hash_2words(mt
->columns
[j
].select
, basis
);
868 struct ovsdb_monitor
*
869 ovsdb_monitor_add(struct ovsdb_monitor
*new_dbmon
)
871 struct ovsdb_monitor
*dbmon
;
874 /* New_dbmon should be associated with only one jsonrpc
876 ovs_assert(list_is_singleton(&new_dbmon
->jsonrpc_monitors
));
878 hash
= ovsdb_monitor_hash(new_dbmon
, 0);
879 HMAP_FOR_EACH_WITH_HASH(dbmon
, hmap_node
, hash
, &ovsdb_monitors
) {
880 if (ovsdb_monitor_equal(dbmon
, new_dbmon
)) {
885 hmap_insert(&ovsdb_monitors
, &new_dbmon
->hmap_node
, hash
);
890 ovsdb_monitor_destroy(struct ovsdb_monitor
*dbmon
)
892 struct shash_node
*node
;
894 list_remove(&dbmon
->replica
.node
);
896 if (!hmap_node_is_null(&dbmon
->hmap_node
)) {
897 hmap_remove(&ovsdb_monitors
, &dbmon
->hmap_node
);
900 ovsdb_monitor_json_cache_flush(dbmon
);
901 hmap_destroy(&dbmon
->json_cache
);
903 SHASH_FOR_EACH (node
, &dbmon
->tables
) {
904 struct ovsdb_monitor_table
*mt
= node
->data
;
905 struct ovsdb_monitor_changes
*changes
, *next
;
907 HMAP_FOR_EACH_SAFE (changes
, next
, hmap_node
, &mt
->changes
) {
908 hmap_remove(&mt
->changes
, &changes
->hmap_node
);
909 ovsdb_monitor_changes_destroy(changes
);
914 shash_destroy(&dbmon
->tables
);
918 static struct ovsdb_error
*
919 ovsdb_monitor_commit(struct ovsdb_replica
*replica
,
920 const struct ovsdb_txn
*txn
,
921 bool durable OVS_UNUSED
)
923 struct ovsdb_monitor
*m
= ovsdb_monitor_cast(replica
);
924 struct ovsdb_monitor_aux aux
;
926 ovsdb_monitor_json_cache_flush(m
);
927 ovsdb_monitor_init_aux(&aux
, m
);
928 ovsdb_txn_for_each_change(txn
, ovsdb_monitor_change_cb
, &aux
);
935 ovsdb_monitor_destroy_callback(struct ovsdb_replica
*replica
)
937 struct ovsdb_monitor
*dbmon
= ovsdb_monitor_cast(replica
);
938 struct jsonrpc_monitor_node
*jm
, *next
;
940 /* Delete all front end monitors. Removing the last front
941 * end monitor will also destroy the corresponding 'ovsdb_monitor'.
942 * ovsdb monitor will also be destroied. */
943 LIST_FOR_EACH_SAFE(jm
, next
, node
, &dbmon
->jsonrpc_monitors
) {
944 ovsdb_jsonrpc_monitor_destroy(jm
->jsonrpc_monitor
);
948 static const struct ovsdb_replica_class ovsdb_jsonrpc_replica_class
= {
949 ovsdb_monitor_commit
,
950 ovsdb_monitor_destroy_callback
,