]> git.proxmox.com Git - mirror_ovs.git/blame - ovsdb/monitor.c
monitor: Fix crash when monitor condition adds new columns.
[mirror_ovs.git] / ovsdb / monitor.c
CommitLineData
2fa1df7b 1/*
c1efab9b 2 * Copyright (c) 2015, 2017 Nicira, Inc.
2fa1df7b
AZ
3 *
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:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17#include <config.h>
18
19#include <errno.h>
20
21#include "bitmap.h"
22#include "column.h"
3e8a2ad1 23#include "openvswitch/dynamic-string.h"
ee89ea7b 24#include "openvswitch/json.h"
2fa1df7b
AZ
25#include "jsonrpc.h"
26#include "ovsdb-error.h"
27#include "ovsdb-parser.h"
28#include "ovsdb.h"
29#include "row.h"
71cdf7cd 30#include "condition.h"
2fa1df7b 31#include "simap.h"
7e911055 32#include "hash.h"
2fa1df7b 33#include "table.h"
6e5a9216 34#include "hash.h"
2fa1df7b
AZ
35#include "timeval.h"
36#include "transaction.h"
37#include "jsonrpc-server.h"
38#include "monitor.h"
ee89ea7b 39#include "util.h"
2fa1df7b
AZ
40#include "openvswitch/vlog.h"
41
71cdf7cd 42VLOG_DEFINE_THIS_MODULE(ovsdb_monitor);
2fa1df7b 43
6e5a9216 44static struct hmap ovsdb_monitors = HMAP_INITIALIZER(&ovsdb_monitors);
2fa1df7b 45
71cdf7cd
LS
46/* Keep state of session's conditions */
47struct ovsdb_monitor_session_condition {
e4ef6881 48 bool conditional; /* True iff every table's condition is true. */
71cdf7cd
LS
49 struct shash tables; /* Contains
50 * "struct ovsdb_monitor_table_condition *"s. */
51};
52
53/* Monitored table session's conditions */
54struct 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;
59};
60
2fa1df7b
AZ
61/* Backend monitor.
62 *
63 * ovsdb_monitor keep track of the ovsdb changes.
64 */
65
66/* A collection of tables being monitored. */
67struct ovsdb_monitor {
009bf21f 68 struct ovs_list list_node; /* In struct ovsdb's "monitors" list. */
2fa1df7b 69 struct shash tables; /* Holds "struct ovsdb_monitor_table"s. */
d9412837 70 struct ovs_list jsonrpc_monitors; /* Contains "jsonrpc_monitor_node"s. */
2fa1df7b 71 struct ovsdb *db;
59c35e11 72 uint64_t n_transactions; /* Count number of committed transactions. */
6e5a9216 73 struct hmap_node hmap_node; /* Elements within ovsdb_monitors. */
4c280978
AZ
74 struct hmap json_cache; /* Contains "ovsdb_monitor_json_cache_node"s.*/
75};
76
77/* A json object of updates between 'from_txn' and 'dbmon->n_transactions'
78 * inclusive. */
79struct ovsdb_monitor_json_cache_node {
80 struct hmap_node hmap_node; /* Elements in json cache. */
a32b4433 81 enum ovsdb_monitor_version version;
4c280978
AZ
82 uint64_t from_txn;
83 struct json *json; /* Null, or a cloned of json */
2fa1df7b
AZ
84};
85
d9412837 86struct jsonrpc_monitor_node {
d9412837 87 struct ovs_list node;
ad376c93 88 struct ovsdb_jsonrpc_monitor *jsonrpc_monitor;
d9412837
AZ
89};
90
2fa1df7b
AZ
91/* A particular column being monitored. */
92struct ovsdb_monitor_column {
93 const struct ovsdb_column *column;
94 enum ovsdb_monitor_selection select;
6150a75f 95 bool monitored;
2fa1df7b
AZ
96};
97
98/* A row that has changed in a monitored table. */
99struct ovsdb_monitor_row {
100 struct hmap_node hmap_node; /* In ovsdb_jsonrpc_monitor_table.changes. */
101 struct uuid uuid; /* UUID of row that changed. */
102 struct ovsdb_datum *old; /* Old data, NULL for an inserted row. */
103 struct ovsdb_datum *new; /* New data, NULL for a deleted row. */
104};
105
1158f320
AZ
106/* Contains 'struct ovsdb_monitor_row's for rows that have been
107 * updated but not yet flushed to all the jsonrpc connection.
108 *
109 * 'n_refs' represent the number of jsonrpc connections that have
110 * not received updates. Generate the update for the last jsonprc
111 * connection will also destroy the whole "struct ovsdb_monitor_changes"
112 * object.
113 *
114 * 'transaction' stores the first update's transaction id.
115 * */
116struct ovsdb_monitor_changes {
ad376c93
JS
117 struct hmap_node hmap_node; /* Element in ovsdb_monitor_tables' changes
118 hmap. */
1158f320
AZ
119 struct ovsdb_monitor_table *mt;
120 struct hmap rows;
121 int n_refs;
122 uint64_t transaction;
e0f42d4a
HZ
123
124 /* Save the mt->n_columns that is used when creating the changes.
125 * It can be different from the current mt->n_columns because
126 * mt->n_columns can be increased when there are condition changes
127 * from any of the clients sharing the dbmon. */
128 size_t n_columns;
1158f320
AZ
129};
130
2fa1df7b
AZ
131/* A particular table being monitored. */
132struct ovsdb_monitor_table {
133 const struct ovsdb_table *table;
134
135 /* This is the union (bitwise-OR) of the 'select' values in all of the
136 * members of 'columns' below. */
137 enum ovsdb_monitor_selection select;
138
139 /* Columns being monitored. */
140 struct ovsdb_monitor_column *columns;
141 size_t n_columns;
6150a75f
LS
142 size_t n_monitored_columns;
143 size_t allocated_columns;
2fa1df7b 144
ec1eadce
LS
145 /* Columns in ovsdb_monitor_row have different indexes then in
146 * ovsdb_row. This field maps between column->index to the index in the
147 * ovsdb_monitor_row. It is used for condition evaluation. */
148 unsigned int *columns_index_map;
149
7e911055
AZ
150 /* Contains 'ovsdb_monitor_changes' indexed by 'transaction'. */
151 struct hmap changes;
2fa1df7b
AZ
152};
153
845a1187
LS
154enum ovsdb_monitor_row_type {
155 OVSDB_ROW,
156 OVSDB_MONITOR_ROW
157};
158
52553aea 159typedef struct json *
71cdf7cd
LS
160(*compose_row_update_cb_func)
161 (const struct ovsdb_monitor_table *mt,
162 const struct ovsdb_monitor_session_condition * condition,
845a1187
LS
163 enum ovsdb_monitor_row_type row_type,
164 const void *,
e0f42d4a
HZ
165 bool initial, unsigned long int *changed,
166 size_t n_columns);
52553aea 167
d9412837 168static void ovsdb_monitor_destroy(struct ovsdb_monitor *dbmon);
6e5a9216
AZ
169static struct ovsdb_monitor_changes * ovsdb_monitor_table_add_changes(
170 struct ovsdb_monitor_table *mt, uint64_t next_txn);
7e911055
AZ
171static struct ovsdb_monitor_changes *ovsdb_monitor_table_find_changes(
172 struct ovsdb_monitor_table *mt, uint64_t unflushed);
1158f320
AZ
173static void ovsdb_monitor_changes_destroy(
174 struct ovsdb_monitor_changes *changes);
175static void ovsdb_monitor_table_track_changes(struct ovsdb_monitor_table *mt,
7e911055 176 uint64_t unflushed);
d9412837 177
a32b4433
HZ
178static uint32_t
179json_cache_hash(enum ovsdb_monitor_version version, uint64_t from_txn)
180{
181 uint32_t hash;
182
183 hash = hash_uint64(version);
184 hash = hash_uint64_basis(from_txn, hash);
185
186 return hash;
187}
188
4c280978
AZ
189static struct ovsdb_monitor_json_cache_node *
190ovsdb_monitor_json_cache_search(const struct ovsdb_monitor *dbmon,
a32b4433 191 enum ovsdb_monitor_version version,
4c280978
AZ
192 uint64_t from_txn)
193{
194 struct ovsdb_monitor_json_cache_node *node;
a32b4433 195 uint32_t hash = json_cache_hash(version, from_txn);
4c280978
AZ
196
197 HMAP_FOR_EACH_WITH_HASH(node, hmap_node, hash, &dbmon->json_cache) {
a32b4433 198 if (node->from_txn == from_txn && node->version == version) {
4c280978
AZ
199 return node;
200 }
201 }
202
203 return NULL;
204}
205
206static void
207ovsdb_monitor_json_cache_insert(struct ovsdb_monitor *dbmon,
a32b4433 208 enum ovsdb_monitor_version version,
4c280978
AZ
209 uint64_t from_txn, struct json *json)
210{
211 struct ovsdb_monitor_json_cache_node *node;
a32b4433 212 uint32_t hash = json_cache_hash(version, from_txn);
4c280978
AZ
213
214 node = xmalloc(sizeof *node);
215
a32b4433 216 node->version = version;
4c280978
AZ
217 node->from_txn = from_txn;
218 node->json = json ? json_clone(json) : NULL;
219
220 hmap_insert(&dbmon->json_cache, &node->hmap_node, hash);
221}
222
223static void
224ovsdb_monitor_json_cache_flush(struct ovsdb_monitor *dbmon)
225{
4ec3d7c7 226 struct ovsdb_monitor_json_cache_node *node;
4c280978 227
4ec3d7c7 228 HMAP_FOR_EACH_POP(node, hmap_node, &dbmon->json_cache) {
4c280978
AZ
229 json_destroy(node->json);
230 free(node);
231 }
232}
233
2fa1df7b
AZ
234static int
235compare_ovsdb_monitor_column(const void *a_, const void *b_)
236{
237 const struct ovsdb_monitor_column *a = a_;
238 const struct ovsdb_monitor_column *b = b_;
239
6150a75f
LS
240 /* put all monitored columns at the begining */
241 if (a->monitored != b->monitored) {
242 return a->monitored ? -1 : 1;
243 }
244
2fa1df7b
AZ
245 return a->column < b->column ? -1 : a->column > b->column;
246}
247
1158f320 248/* Finds and returns the ovsdb_monitor_row in 'mt->changes->rows' for the
2fa1df7b
AZ
249 * given 'uuid', or NULL if there is no such row. */
250static struct ovsdb_monitor_row *
7e911055
AZ
251ovsdb_monitor_changes_row_find(const struct ovsdb_monitor_changes *changes,
252 const struct uuid *uuid)
2fa1df7b
AZ
253{
254 struct ovsdb_monitor_row *row;
255
1158f320 256 HMAP_FOR_EACH_WITH_HASH (row, hmap_node, uuid_hash(uuid),
7e911055 257 &changes->rows) {
2fa1df7b
AZ
258 if (uuid_equals(uuid, &row->uuid)) {
259 return row;
260 }
261 }
262 return NULL;
263}
264
e0f42d4a 265/* Allocates an array of 'n_columns' ovsdb_datums and initializes them as
2fa1df7b
AZ
266 * copies of the data in 'row' drawn from the columns represented by
267 * mt->columns[]. Returns the array.
268 *
269 * If 'row' is NULL, returns NULL. */
270static struct ovsdb_datum *
271clone_monitor_row_data(const struct ovsdb_monitor_table *mt,
e0f42d4a
HZ
272 const struct ovsdb_row *row,
273 size_t n_columns)
2fa1df7b
AZ
274{
275 struct ovsdb_datum *data;
276 size_t i;
277
278 if (!row) {
279 return NULL;
280 }
281
e0f42d4a
HZ
282 data = xmalloc(n_columns * sizeof *data);
283 for (i = 0; i < n_columns; i++) {
2fa1df7b
AZ
284 const struct ovsdb_column *c = mt->columns[i].column;
285 const struct ovsdb_datum *src = &row->fields[c->index];
286 struct ovsdb_datum *dst = &data[i];
287 const struct ovsdb_type *type = &c->type;
288
289 ovsdb_datum_clone(dst, src, type);
290 }
291 return data;
292}
293
e0f42d4a 294/* Replaces the n_columns ovsdb_datums in row[] by copies of the data from
2fa1df7b
AZ
295 * in 'row' drawn from the columns represented by mt->columns[]. */
296static void
297update_monitor_row_data(const struct ovsdb_monitor_table *mt,
298 const struct ovsdb_row *row,
e0f42d4a
HZ
299 struct ovsdb_datum *data,
300 size_t n_columns)
2fa1df7b
AZ
301{
302 size_t i;
303
e0f42d4a 304 for (i = 0; i < n_columns; i++) {
2fa1df7b
AZ
305 const struct ovsdb_column *c = mt->columns[i].column;
306 const struct ovsdb_datum *src = &row->fields[c->index];
307 struct ovsdb_datum *dst = &data[i];
308 const struct ovsdb_type *type = &c->type;
309
310 if (!ovsdb_datum_equals(src, dst, type)) {
311 ovsdb_datum_destroy(dst, type);
312 ovsdb_datum_clone(dst, src, type);
313 }
314 }
315}
316
e0f42d4a 317/* Frees all of the n_columns ovsdb_datums in data[], using the types taken
2fa1df7b
AZ
318 * from mt->columns[], plus 'data' itself. */
319static void
320free_monitor_row_data(const struct ovsdb_monitor_table *mt,
e0f42d4a
HZ
321 struct ovsdb_datum *data,
322 size_t n_columns)
2fa1df7b
AZ
323{
324 if (data) {
325 size_t i;
326
e0f42d4a 327 for (i = 0; i < n_columns; i++) {
2fa1df7b
AZ
328 const struct ovsdb_column *c = mt->columns[i].column;
329
330 ovsdb_datum_destroy(&data[i], &c->type);
331 }
332 free(data);
333 }
334}
335
336/* Frees 'row', which must have been created from 'mt'. */
337static void
338ovsdb_monitor_row_destroy(const struct ovsdb_monitor_table *mt,
e0f42d4a
HZ
339 struct ovsdb_monitor_row *row,
340 size_t n_columns)
2fa1df7b
AZ
341{
342 if (row) {
e0f42d4a
HZ
343 free_monitor_row_data(mt, row->old, n_columns);
344 free_monitor_row_data(mt, row->new, n_columns);
2fa1df7b
AZ
345 free(row);
346 }
347}
348
ec1eadce
LS
349static void
350ovsdb_monitor_columns_sort(struct ovsdb_monitor *dbmon)
351{
352 int i;
353 struct shash_node *node;
354
355 SHASH_FOR_EACH (node, &dbmon->tables) {
356 struct ovsdb_monitor_table *mt = node->data;
357
358 qsort(mt->columns, mt->n_columns, sizeof *mt->columns,
359 compare_ovsdb_monitor_column);
360 for (i = 0; i < mt->n_columns; i++) {
361 /* re-set index map due to sort */
362 mt->columns_index_map[mt->columns[i].column->index] = i;
363 }
364 }
365}
366
6e5a9216 367void
36247a75
AZ
368ovsdb_monitor_add_jsonrpc_monitor(struct ovsdb_monitor *dbmon,
369 struct ovsdb_jsonrpc_monitor *jsonrpc_monitor)
370{
371 struct jsonrpc_monitor_node *jm;
372
373 jm = xzalloc(sizeof *jm);
374 jm->jsonrpc_monitor = jsonrpc_monitor;
417e7e66 375 ovs_list_push_back(&dbmon->jsonrpc_monitors, &jm->node);
36247a75
AZ
376}
377
2fa1df7b
AZ
378struct ovsdb_monitor *
379ovsdb_monitor_create(struct ovsdb *db,
380 struct ovsdb_jsonrpc_monitor *jsonrpc_monitor)
381{
382 struct ovsdb_monitor *dbmon;
383
384 dbmon = xzalloc(sizeof *dbmon);
385
009bf21f 386 ovs_list_push_back(&db->monitors, &dbmon->list_node);
417e7e66 387 ovs_list_init(&dbmon->jsonrpc_monitors);
2fa1df7b 388 dbmon->db = db;
59c35e11 389 dbmon->n_transactions = 0;
2fa1df7b 390 shash_init(&dbmon->tables);
6e5a9216 391 hmap_node_nullify(&dbmon->hmap_node);
4c280978 392 hmap_init(&dbmon->json_cache);
2fa1df7b 393
36247a75 394 ovsdb_monitor_add_jsonrpc_monitor(dbmon, jsonrpc_monitor);
2fa1df7b
AZ
395 return dbmon;
396}
397
398void
399ovsdb_monitor_add_table(struct ovsdb_monitor *m,
400 const struct ovsdb_table *table)
401{
402 struct ovsdb_monitor_table *mt;
ec1eadce
LS
403 int i;
404 size_t n_columns = shash_count(&table->schema->columns);
2fa1df7b
AZ
405
406 mt = xzalloc(sizeof *mt);
407 mt->table = table;
2fa1df7b 408 shash_add(&m->tables, table->schema->name, mt);
7e911055 409 hmap_init(&mt->changes);
ec1eadce
LS
410 mt->columns_index_map =
411 xmalloc(sizeof *mt->columns_index_map * n_columns);
412 for (i = 0; i < n_columns; i++) {
413 mt->columns_index_map[i] = -1;
414 }
2fa1df7b
AZ
415}
416
ec1eadce 417const char *
2fa1df7b
AZ
418ovsdb_monitor_add_column(struct ovsdb_monitor *dbmon,
419 const struct ovsdb_table *table,
420 const struct ovsdb_column *column,
421 enum ovsdb_monitor_selection select,
6150a75f 422 bool monitored)
2fa1df7b
AZ
423{
424 struct ovsdb_monitor_table *mt;
425 struct ovsdb_monitor_column *c;
426
427 mt = shash_find_data(&dbmon->tables, table->schema->name);
428
ec1eadce
LS
429 /* Check for column duplication. Return duplicated column name. */
430 if (mt->columns_index_map[column->index] != -1) {
431 return column->name;
432 }
433
6150a75f
LS
434 if (mt->n_columns >= mt->allocated_columns) {
435 mt->columns = x2nrealloc(mt->columns, &mt->allocated_columns,
2fa1df7b
AZ
436 sizeof *mt->columns);
437 }
438
439 mt->select |= select;
ec1eadce 440 mt->columns_index_map[column->index] = mt->n_columns;
2fa1df7b
AZ
441 c = &mt->columns[mt->n_columns++];
442 c->column = column;
443 c->select = select;
6150a75f
LS
444 c->monitored = monitored;
445 if (monitored) {
446 mt->n_monitored_columns++;
447 }
2fa1df7b
AZ
448
449 return NULL;
450}
451
71cdf7cd
LS
452static void
453ovsdb_monitor_condition_add_columns(struct ovsdb_monitor *dbmon,
454 const struct ovsdb_table *table,
455 struct ovsdb_condition *condition)
456{
457 size_t n_columns;
458 int i;
459 const struct ovsdb_column **columns =
460 ovsdb_condition_get_columns(condition, &n_columns);
461
462 for (i = 0; i < n_columns; i++) {
463 ovsdb_monitor_add_column(dbmon, table, columns[i],
464 OJMS_NONE, false);
465 }
466
467 free(columns);
468}
469
470/* Bind this session's condition to ovsdb_monitor */
471void
472ovsdb_monitor_condition_bind(struct ovsdb_monitor *dbmon,
473 struct ovsdb_monitor_session_condition *cond)
474{
475 struct shash_node *node;
476
477 SHASH_FOR_EACH(node, &cond->tables) {
478 struct ovsdb_monitor_table_condition *mtc = node->data;
479 struct ovsdb_monitor_table *mt =
480 shash_find_data(&dbmon->tables, mtc->table->schema->name);
481
482 mtc->mt = mt;
483 ovsdb_monitor_condition_add_columns(dbmon, mtc->table,
484 &mtc->new_condition);
485 }
486}
487
845a1187
LS
488bool
489ovsdb_monitor_table_exists(struct ovsdb_monitor *m,
490 const struct ovsdb_table *table)
491{
492 return shash_find_data(&m->tables, table->schema->name);
493}
494
6e5a9216 495static struct ovsdb_monitor_changes *
1158f320
AZ
496ovsdb_monitor_table_add_changes(struct ovsdb_monitor_table *mt,
497 uint64_t next_txn)
498{
499 struct ovsdb_monitor_changes *changes;
500
501 changes = xzalloc(sizeof *changes);
502
503 changes->transaction = next_txn;
504 changes->mt = mt;
505 changes->n_refs = 1;
e0f42d4a 506 changes->n_columns = mt->n_columns;
1158f320 507 hmap_init(&changes->rows);
7e911055 508 hmap_insert(&mt->changes, &changes->hmap_node, hash_uint64(next_txn));
6e5a9216
AZ
509
510 return changes;
7e911055
AZ
511};
512
513static struct ovsdb_monitor_changes *
514ovsdb_monitor_table_find_changes(struct ovsdb_monitor_table *mt,
515 uint64_t transaction)
516{
517 struct ovsdb_monitor_changes *changes;
518 size_t hash = hash_uint64(transaction);
519
520 HMAP_FOR_EACH_WITH_HASH(changes, hmap_node, hash, &mt->changes) {
521 if (changes->transaction == transaction) {
522 return changes;
523 }
524 }
525
526 return NULL;
1158f320
AZ
527}
528
fcd48081 529/* Stop currently tracking changes to table 'mt' since 'transaction'. */
1158f320
AZ
530static void
531ovsdb_monitor_table_untrack_changes(struct ovsdb_monitor_table *mt,
532 uint64_t transaction)
533{
7e911055
AZ
534 struct ovsdb_monitor_changes *changes =
535 ovsdb_monitor_table_find_changes(mt, transaction);
1158f320 536 if (changes) {
1158f320 537 if (--changes->n_refs == 0) {
7e911055 538 hmap_remove(&mt->changes, &changes->hmap_node);
1158f320 539 ovsdb_monitor_changes_destroy(changes);
1158f320
AZ
540 }
541 }
542}
543
544/* Start tracking changes to table 'mt' begins from 'transaction' inclusive.
545 */
546static void
547ovsdb_monitor_table_track_changes(struct ovsdb_monitor_table *mt,
548 uint64_t transaction)
549{
7e911055
AZ
550 struct ovsdb_monitor_changes *changes;
551
552 changes = ovsdb_monitor_table_find_changes(mt, transaction);
553 if (changes) {
554 changes->n_refs++;
555 } else {
556 ovsdb_monitor_table_add_changes(mt, transaction);
557 }
1158f320
AZ
558}
559
560static void
561ovsdb_monitor_changes_destroy(struct ovsdb_monitor_changes *changes)
562{
563 struct ovsdb_monitor_row *row, *next;
564
565 HMAP_FOR_EACH_SAFE (row, next, hmap_node, &changes->rows) {
566 hmap_remove(&changes->rows, &row->hmap_node);
e0f42d4a 567 ovsdb_monitor_row_destroy(changes->mt, row, changes->n_columns);
1158f320
AZ
568 }
569 hmap_destroy(&changes->rows);
570 free(changes);
571}
572
23cbedb7
AZ
573static enum ovsdb_monitor_selection
574ovsdb_monitor_row_update_type(bool initial, const bool old, const bool new)
575{
576 return initial ? OJMS_INITIAL
577 : !old ? OJMS_INSERT
578 : !new ? OJMS_DELETE
579 : OJMS_MODIFY;
580}
71cdf7cd
LS
581
582/* Set conditional monitoring mode only if we have non-empty condition in one
583 * of the tables at least */
584static inline void
585ovsdb_monitor_session_condition_set_mode(
586 struct ovsdb_monitor_session_condition *cond)
587{
e4ef6881
BP
588 struct shash_node *node;
589
590 SHASH_FOR_EACH (node, &cond->tables) {
591 struct ovsdb_monitor_table_condition *mtc = node->data;
592
593 if (!ovsdb_condition_is_true(&mtc->new_condition)) {
594 cond->conditional = true;
595 return;
596 }
597 }
598 cond->conditional = false;
71cdf7cd
LS
599}
600
601/* Returnes an empty allocated session's condition state holder */
602struct ovsdb_monitor_session_condition *
603ovsdb_monitor_session_condition_create(void)
604{
605 struct ovsdb_monitor_session_condition *condition =
606 xzalloc(sizeof *condition);
607
608 condition->conditional = false;
609 shash_init(&condition->tables);
610 return condition;
611}
612
613void
614ovsdb_monitor_session_condition_destroy(
615 struct ovsdb_monitor_session_condition *condition)
616{
617 struct shash_node *node, *next;
618
619 if (!condition) {
620 return;
621 }
622
623 SHASH_FOR_EACH_SAFE (node, next, &condition->tables) {
624 struct ovsdb_monitor_table_condition *mtc = node->data;
625
626 ovsdb_condition_destroy(&mtc->new_condition);
627 ovsdb_condition_destroy(&mtc->old_condition);
628 shash_delete(&condition->tables, node);
629 free(mtc);
630 }
d3c8a7e8 631 shash_destroy(&condition->tables);
71cdf7cd
LS
632 free(condition);
633}
634
635struct ovsdb_error *
636ovsdb_monitor_table_condition_create(
637 struct ovsdb_monitor_session_condition *condition,
638 const struct ovsdb_table *table,
639 const struct json *json_cnd)
640{
641 struct ovsdb_monitor_table_condition *mtc;
642 struct ovsdb_error *error;
643
644 mtc = xzalloc(sizeof *mtc);
645 mtc->table = table;
646 ovsdb_condition_init(&mtc->old_condition);
647 ovsdb_condition_init(&mtc->new_condition);
648
649 if (json_cnd) {
650 error = ovsdb_condition_from_json(table->schema,
651 json_cnd,
652 NULL,
653 &mtc->old_condition);
654 if (error) {
655 free(mtc);
656 return error;
657 }
658 }
659
660 shash_add(&condition->tables, table->schema->name, mtc);
661 /* On session startup old == new condition */
662 ovsdb_condition_clone(&mtc->new_condition, &mtc->old_condition);
5707b87d 663 ovsdb_monitor_session_condition_set_mode(condition);
71cdf7cd
LS
664
665 return NULL;
666}
667
668static bool
669ovsdb_monitor_get_table_conditions(
670 const struct ovsdb_monitor_table *mt,
671 const struct ovsdb_monitor_session_condition *condition,
672 struct ovsdb_condition **old_condition,
673 struct ovsdb_condition **new_condition)
674{
675 if (!condition) {
676 return false;
677 }
678
679 struct ovsdb_monitor_table_condition *mtc =
680 shash_find_data(&condition->tables, mt->table->schema->name);
681
682 if (!mtc) {
683 return false;
684 }
685 *old_condition = &mtc->old_condition;
686 *new_condition = &mtc->new_condition;
687
688 return true;
689}
690
845a1187
LS
691struct ovsdb_error *
692ovsdb_monitor_table_condition_update(
693 struct ovsdb_monitor *dbmon,
694 struct ovsdb_monitor_session_condition *condition,
695 const struct ovsdb_table *table,
696 const struct json *cond_json)
697{
c1efab9b
BP
698 if (!condition) {
699 return NULL;
700 }
701
845a1187
LS
702 struct ovsdb_monitor_table_condition *mtc =
703 shash_find_data(&condition->tables, table->schema->name);
704 struct ovsdb_error *error;
f0d7ae19 705 struct ovsdb_condition cond = OVSDB_CONDITION_INITIALIZER(&cond);
845a1187 706
845a1187
LS
707 error = ovsdb_condition_from_json(table->schema, cond_json,
708 NULL, &cond);
709 if (error) {
710 return error;
711 }
712 ovsdb_condition_destroy(&mtc->new_condition);
713 ovsdb_condition_clone(&mtc->new_condition, &cond);
714 ovsdb_condition_destroy(&cond);
715 ovsdb_monitor_condition_add_columns(dbmon,
716 table,
717 &mtc->new_condition);
718
719 return NULL;
720}
721
722static void
723ovsdb_monitor_table_condition_updated(struct ovsdb_monitor_table *mt,
724 struct ovsdb_monitor_session_condition *condition)
725{
726 struct ovsdb_monitor_table_condition *mtc =
727 shash_find_data(&condition->tables, mt->table->schema->name);
728
729 if (mtc) {
730 /* If conditional monitoring - set old condition to new condition */
731 if (ovsdb_condition_cmp_3way(&mtc->old_condition,
732 &mtc->new_condition)) {
845a1187
LS
733 ovsdb_condition_destroy(&mtc->old_condition);
734 ovsdb_condition_clone(&mtc->old_condition, &mtc->new_condition);
735 ovsdb_monitor_session_condition_set_mode(condition);
736 }
737 }
738}
739
71cdf7cd
LS
740static enum ovsdb_monitor_selection
741ovsdb_monitor_row_update_type_condition(
742 const struct ovsdb_monitor_table *mt,
743 const struct ovsdb_monitor_session_condition *condition,
744 bool initial,
845a1187 745 enum ovsdb_monitor_row_type row_type,
71cdf7cd
LS
746 const struct ovsdb_datum *old,
747 const struct ovsdb_datum *new)
748{
749 struct ovsdb_condition *old_condition, *new_condition;
750 enum ovsdb_monitor_selection type =
751 ovsdb_monitor_row_update_type(initial, old, new);
752
753 if (ovsdb_monitor_get_table_conditions(mt,
754 condition,
755 &old_condition,
756 &new_condition)) {
757 bool old_cond = !old ? false
758 : ovsdb_condition_empty_or_match_any(old,
845a1187
LS
759 old_condition,
760 row_type == OVSDB_MONITOR_ROW ?
761 mt->columns_index_map :
762 NULL);
71cdf7cd
LS
763 bool new_cond = !new ? false
764 : ovsdb_condition_empty_or_match_any(new,
845a1187
LS
765 new_condition,
766 row_type == OVSDB_MONITOR_ROW ?
767 mt->columns_index_map :
768 NULL);
71cdf7cd
LS
769
770 if (!old_cond && !new_cond) {
771 type = OJMS_NONE;
772 }
773
774 switch (type) {
775 case OJMS_INITIAL:
776 case OJMS_INSERT:
777 if (!new_cond) {
778 type = OJMS_NONE;
779 }
780 break;
781 case OJMS_MODIFY:
782 type = !old_cond ? OJMS_INSERT : !new_cond
783 ? OJMS_DELETE : OJMS_MODIFY;
784 break;
785 case OJMS_DELETE:
786 if (!old_cond) {
787 type = OJMS_NONE;
788 }
789 break;
790 case OJMS_NONE:
791 break;
792 }
793 }
794 return type;
795}
796
52553aea
AZ
797static bool
798ovsdb_monitor_row_skip_update(const struct ovsdb_monitor_table *mt,
845a1187
LS
799 enum ovsdb_monitor_row_type row_type,
800 const struct ovsdb_datum *old,
801 const struct ovsdb_datum *new,
52553aea 802 enum ovsdb_monitor_selection type,
e0f42d4a
HZ
803 unsigned long int *changed,
804 size_t n_columns)
52553aea
AZ
805{
806 if (!(mt->select & type)) {
807 return true;
808 }
809
810 if (type == OJMS_MODIFY) {
811 size_t i, n_changes;
812
813 n_changes = 0;
e0f42d4a
HZ
814 memset(changed, 0, bitmap_n_bytes(n_columns));
815 for (i = 0; i < n_columns; i++) {
52553aea 816 const struct ovsdb_column *c = mt->columns[i].column;
845a1187
LS
817 size_t index = row_type == OVSDB_ROW ? c->index : i;
818 if (!ovsdb_datum_equals(&old[index], &new[index], &c->type)) {
52553aea
AZ
819 bitmap_set1(changed, i);
820 n_changes++;
821 }
822 }
823 if (!n_changes) {
824 /* No actual changes: presumably a row changed and then
825 * changed back later. */
826 return true;
827 }
828 }
829
830 return false;
831}
23cbedb7 832
2fa1df7b
AZ
833/* Returns JSON for a <row-update> (as described in RFC 7047) for 'row' within
834 * 'mt', or NULL if no row update should be sent.
835 *
836 * The caller should specify 'initial' as true if the returned JSON is going to
837 * be used as part of the initial reply to a "monitor" request, false if it is
838 * going to be used as part of an "update" notification.
839 *
840 * 'changed' must be a scratch buffer for internal use that is at least
e0f42d4a 841 * bitmap_n_bytes(n_columns) bytes long. */
2fa1df7b
AZ
842static struct json *
843ovsdb_monitor_compose_row_update(
844 const struct ovsdb_monitor_table *mt,
71cdf7cd 845 const struct ovsdb_monitor_session_condition *condition OVS_UNUSED,
845a1187
LS
846 enum ovsdb_monitor_row_type row_type OVS_UNUSED,
847 const void *_row,
e0f42d4a
HZ
848 bool initial, unsigned long int *changed,
849 size_t n_columns OVS_UNUSED)
2fa1df7b 850{
845a1187 851 const struct ovsdb_monitor_row *row = _row;
2fa1df7b
AZ
852 enum ovsdb_monitor_selection type;
853 struct json *old_json, *new_json;
854 struct json *row_json;
855 size_t i;
856
845a1187 857 ovs_assert(row_type == OVSDB_MONITOR_ROW);
23cbedb7 858 type = ovsdb_monitor_row_update_type(initial, row->old, row->new);
845a1187 859 if (ovsdb_monitor_row_skip_update(mt, row_type, row->old,
e0f42d4a
HZ
860 row->new, type, changed,
861 mt->n_columns)) {
2fa1df7b
AZ
862 return NULL;
863 }
864
2fa1df7b
AZ
865 row_json = json_object_create();
866 old_json = new_json = NULL;
867 if (type & (OJMS_DELETE | OJMS_MODIFY)) {
868 old_json = json_object_create();
869 json_object_put(row_json, "old", old_json);
870 }
871 if (type & (OJMS_INITIAL | OJMS_INSERT | OJMS_MODIFY)) {
872 new_json = json_object_create();
873 json_object_put(row_json, "new", new_json);
874 }
6150a75f 875 for (i = 0; i < mt->n_monitored_columns; i++) {
2fa1df7b
AZ
876 const struct ovsdb_monitor_column *c = &mt->columns[i];
877
6150a75f 878 if (!c->monitored || !(type & c->select)) {
2fa1df7b
AZ
879 /* We don't care about this type of change for this
880 * particular column (but we will care about it for some
881 * other column). */
882 continue;
883 }
884
885 if ((type == OJMS_MODIFY && bitmap_is_set(changed, i))
886 || type == OJMS_DELETE) {
887 json_object_put(old_json, c->column->name,
888 ovsdb_datum_to_json(&row->old[i],
889 &c->column->type));
890 }
891 if (type & (OJMS_INITIAL | OJMS_INSERT | OJMS_MODIFY)) {
892 json_object_put(new_json, c->column->name,
893 ovsdb_datum_to_json(&row->new[i],
894 &c->column->type));
895 }
896 }
897
898 return row_json;
899}
900
52553aea
AZ
901/* Returns JSON for a <row-update2> (as described in ovsdb-server(1) mapage)
902 * for 'row' within * 'mt', or NULL if no row update should be sent.
903 *
904 * The caller should specify 'initial' as true if the returned JSON is
845a1187 905 * going to be used as part of the initial reply to a "monitor_cond" request,
52553aea
AZ
906 * false if it is going to be used as part of an "update2" notification.
907 *
908 * 'changed' must be a scratch buffer for internal use that is at least
e0f42d4a 909 * bitmap_n_bytes(n_columns) bytes long. */
52553aea
AZ
910static struct json *
911ovsdb_monitor_compose_row_update2(
912 const struct ovsdb_monitor_table *mt,
71cdf7cd 913 const struct ovsdb_monitor_session_condition *condition,
845a1187
LS
914 enum ovsdb_monitor_row_type row_type,
915 const void *_row,
e0f42d4a
HZ
916 bool initial, unsigned long int *changed,
917 size_t n_columns)
52553aea
AZ
918{
919 enum ovsdb_monitor_selection type;
920 struct json *row_update2, *diff_json;
845a1187 921 const struct ovsdb_datum *old, *new;
52553aea
AZ
922 size_t i;
923
845a1187
LS
924 if (row_type == OVSDB_MONITOR_ROW) {
925 old = ((const struct ovsdb_monitor_row *)_row)->old;;
926 new = ((const struct ovsdb_monitor_row *)_row)->new;
927 } else {
928 old = new = ((const struct ovsdb_row *)_row)->fields;
929 }
930
71cdf7cd 931 type = ovsdb_monitor_row_update_type_condition(mt, condition, initial,
845a1187 932 row_type, old, new);
e0f42d4a
HZ
933 if (ovsdb_monitor_row_skip_update(mt, row_type, old, new, type, changed,
934 n_columns)) {
52553aea
AZ
935 return NULL;
936 }
937
938 row_update2 = json_object_create();
939 if (type == OJMS_DELETE) {
940 json_object_put(row_update2, "delete", json_null_create());
941 } else {
942 diff_json = json_object_create();
943 const char *op;
944
6150a75f 945 for (i = 0; i < mt->n_monitored_columns; i++) {
52553aea 946 const struct ovsdb_monitor_column *c = &mt->columns[i];
845a1187 947 size_t index = row_type == OVSDB_ROW ? c->column->index : i;
6150a75f 948 if (!c->monitored || !(type & c->select)) {
52553aea
AZ
949 /* We don't care about this type of change for this
950 * particular column (but we will care about it for some
951 * other column). */
952 continue;
953 }
954
955 if (type == OJMS_MODIFY) {
956 struct ovsdb_datum diff;
957
958 if (!bitmap_is_set(changed, i)) {
959 continue;
960 }
961
845a1187 962 ovsdb_datum_diff(&diff ,&old[index], &new[index],
52553aea
AZ
963 &c->column->type);
964 json_object_put(diff_json, c->column->name,
965 ovsdb_datum_to_json(&diff, &c->column->type));
966 ovsdb_datum_destroy(&diff, &c->column->type);
967 } else {
845a1187 968 if (!ovsdb_datum_is_default(&new[index], &c->column->type)) {
52553aea 969 json_object_put(diff_json, c->column->name,
845a1187 970 ovsdb_datum_to_json(&new[index],
52553aea
AZ
971 &c->column->type));
972 }
973 }
974 }
975
976 op = type == OJMS_INITIAL ? "initial"
977 : type == OJMS_MODIFY ? "modify" : "insert";
978 json_object_put(row_update2, op, diff_json);
979 }
980
981 return row_update2;
982}
983
6cef8e87
AZ
984static size_t
985ovsdb_monitor_max_columns(struct ovsdb_monitor *dbmon)
986{
987 struct shash_node *node;
988 size_t max_columns = 0;
989
990 SHASH_FOR_EACH (node, &dbmon->tables) {
991 struct ovsdb_monitor_table *mt = node->data;
992
993 max_columns = MAX(max_columns, mt->n_columns);
994 }
995
996 return max_columns;
997}
998
845a1187
LS
999static void
1000ovsdb_monitor_add_json_row(struct json **json, const char *table_name,
1001 struct json **table_json, struct json *row_json,
1002 const struct uuid *row_uuid)
1003{
1004 char uuid[UUID_LEN + 1];
1005
1006 /* Create JSON object for transaction overall. */
1007 if (!*json) {
1008 *json = json_object_create();
1009 }
1010
1011 /* Create JSON object for transaction on this table. */
1012 if (!*table_json) {
1013 *table_json = json_object_create();
1014 json_object_put(*json, table_name, *table_json);
1015 }
1016
1017 /* Add JSON row to JSON table. */
1018 snprintf(uuid, sizeof uuid, UUID_FMT, UUID_ARGS(row_uuid));
1019 json_object_put(*table_json, uuid, row_json);
1020}
1021
2fa1df7b 1022/* Constructs and returns JSON for a <table-updates> object (as described in
4c280978
AZ
1023 * RFC 7047) for all the outstanding changes within 'monitor', starting from
1024 * 'transaction'. */
1025static struct json*
71cdf7cd
LS
1026ovsdb_monitor_compose_update(
1027 struct ovsdb_monitor *dbmon,
1028 bool initial, uint64_t transaction,
1029 const struct ovsdb_monitor_session_condition *condition,
1030 compose_row_update_cb_func row_update)
2fa1df7b
AZ
1031{
1032 struct shash_node *node;
2fa1df7b 1033 struct json *json;
6cef8e87
AZ
1034 size_t max_columns = ovsdb_monitor_max_columns(dbmon);
1035 unsigned long int *changed = xmalloc(bitmap_n_bytes(max_columns));
2fa1df7b
AZ
1036
1037 json = NULL;
1038 SHASH_FOR_EACH (node, &dbmon->tables) {
1039 struct ovsdb_monitor_table *mt = node->data;
1040 struct ovsdb_monitor_row *row, *next;
7e911055 1041 struct ovsdb_monitor_changes *changes;
2fa1df7b
AZ
1042 struct json *table_json = NULL;
1043
4c280978 1044 changes = ovsdb_monitor_table_find_changes(mt, transaction);
7e911055 1045 if (!changes) {
1158f320
AZ
1046 continue;
1047 }
1048
70ec021a
AS
1049 HMAP_FOR_EACH_SAFE (row, next, hmap_node, &changes->rows) {
1050 struct json *row_json;
1051 row_json = (*row_update)(mt, condition, OVSDB_MONITOR_ROW, row,
e0f42d4a 1052 initial, changed, changes->n_columns);
70ec021a
AS
1053 if (row_json) {
1054 ovsdb_monitor_add_json_row(&json, mt->table->schema->name,
1055 &table_json, row_json,
1056 &row->uuid);
1057 }
1058 }
1059 }
845a1187 1060 free(changed);
2fa1df7b 1061
845a1187
LS
1062 return json;
1063}
2fa1df7b 1064
845a1187
LS
1065static struct json*
1066ovsdb_monitor_compose_cond_change_update(
1067 struct ovsdb_monitor *dbmon,
1068 struct ovsdb_monitor_session_condition *condition)
1069{
1070 struct shash_node *node;
1071 struct json *json = NULL;
1072 size_t max_columns = ovsdb_monitor_max_columns(dbmon);
1073 unsigned long int *changed = xmalloc(bitmap_n_bytes(max_columns));
2fa1df7b 1074
845a1187
LS
1075 SHASH_FOR_EACH (node, &dbmon->tables) {
1076 struct ovsdb_monitor_table *mt = node->data;
1077 struct ovsdb_row *row;
1078 struct json *table_json = NULL;
1079 struct ovsdb_condition *old_condition, *new_condition;
1080
1081 if (!ovsdb_monitor_get_table_conditions(mt,
1082 condition,
1083 &old_condition,
1084 &new_condition) ||
1085 !ovsdb_condition_cmp_3way(old_condition, new_condition)) {
1086 /* Nothing to update on this table */
1087 continue;
1088 }
2fa1df7b 1089
845a1187
LS
1090 /* Iterate over all rows in table */
1091 HMAP_FOR_EACH (row, hmap_node, &mt->table->rows) {
1092 struct json *row_json;
1093
1094 row_json = ovsdb_monitor_compose_row_update2(mt, condition,
1095 OVSDB_ROW, row,
e0f42d4a
HZ
1096 false, changed,
1097 mt->n_columns);
845a1187
LS
1098 if (row_json) {
1099 ovsdb_monitor_add_json_row(&json, mt->table->schema->name,
1100 &table_json, row_json,
1101 ovsdb_row_get_uuid(row));
2fa1df7b 1102 }
2fa1df7b 1103 }
845a1187 1104 ovsdb_monitor_table_condition_updated(mt, condition);
4c280978
AZ
1105 }
1106 free(changed);
1107
1108 return json;
1109}
1110
1111/* Returns JSON for a <table-updates> object (as described in RFC 7047)
1112 * for all the outstanding changes within 'monitor' that starts from
845a1187
LS
1113 * '*unflushed'.
1114 * If cond_updated is true all rows in the db that match conditions will be
1115 * sent.
4c280978
AZ
1116 *
1117 * The caller should specify 'initial' as true if the returned JSON is going to
1118 * be used as part of the initial reply to a "monitor" request, false if it is
1119 * going to be used as part of an "update" notification. */
1120struct json *
71cdf7cd
LS
1121ovsdb_monitor_get_update(
1122 struct ovsdb_monitor *dbmon,
845a1187
LS
1123 bool initial, bool cond_updated,
1124 uint64_t *unflushed_,
1125 struct ovsdb_monitor_session_condition *condition,
71cdf7cd 1126 enum ovsdb_monitor_version version)
4c280978 1127{
71cdf7cd 1128 struct ovsdb_monitor_json_cache_node *cache_node = NULL;
4c280978
AZ
1129 struct shash_node *node;
1130 struct json *json;
765b5a0e
AZ
1131 const uint64_t unflushed = *unflushed_;
1132 const uint64_t next_unflushed = dbmon->n_transactions + 1;
4c280978 1133
845a1187
LS
1134 ovs_assert(cond_updated ? unflushed == next_unflushed : true);
1135
4c280978
AZ
1136 /* Return a clone of cached json if one exists. Otherwise,
1137 * generate a new one and add it to the cache. */
845a1187 1138 if (!condition || (!condition->conditional && !cond_updated)) {
71cdf7cd
LS
1139 cache_node = ovsdb_monitor_json_cache_search(dbmon, version,
1140 unflushed);
1141 }
4c280978
AZ
1142 if (cache_node) {
1143 json = cache_node->json ? json_clone(cache_node->json) : NULL;
1144 } else {
52553aea 1145 if (version == OVSDB_MONITOR_V1) {
71cdf7cd
LS
1146 json =
1147 ovsdb_monitor_compose_update(dbmon, initial, unflushed,
1148 condition,
1149 ovsdb_monitor_compose_row_update);
52553aea
AZ
1150 } else {
1151 ovs_assert(version == OVSDB_MONITOR_V2);
845a1187 1152 if (!cond_updated) {
70ec021a
AS
1153 json = ovsdb_monitor_compose_update(dbmon, initial, unflushed,
1154 condition,
1155 ovsdb_monitor_compose_row_update2);
1156
1157 if (!condition || !condition->conditional) {
1158 ovsdb_monitor_json_cache_insert(dbmon, version, unflushed,
1159 json);
1160 }
1161 } else {
845a1187
LS
1162 /* Compose update on whole db due to condition update.
1163 Session must be flushed (change list is empty)*/
70ec021a
AS
1164 json =
1165 ovsdb_monitor_compose_cond_change_update(dbmon, condition);
1166 }
1167 }
4c280978
AZ
1168 }
1169
1170 /* Maintain transaction id of 'changes'. */
1171 SHASH_FOR_EACH (node, &dbmon->tables) {
1172 struct ovsdb_monitor_table *mt = node->data;
1158f320 1173
765b5a0e
AZ
1174 ovsdb_monitor_table_untrack_changes(mt, unflushed);
1175 ovsdb_monitor_table_track_changes(mt, next_unflushed);
2fa1df7b 1176 }
765b5a0e 1177 *unflushed_ = next_unflushed;
4c280978 1178
2fa1df7b
AZ
1179 return json;
1180}
1181
1182bool
59c35e11
AZ
1183ovsdb_monitor_needs_flush(struct ovsdb_monitor *dbmon,
1184 uint64_t next_transaction)
2fa1df7b 1185{
59c35e11
AZ
1186 ovs_assert(next_transaction <= dbmon->n_transactions + 1);
1187 return (next_transaction <= dbmon->n_transactions);
2fa1df7b
AZ
1188}
1189
1190void
1191ovsdb_monitor_table_add_select(struct ovsdb_monitor *dbmon,
1192 const struct ovsdb_table *table,
1193 enum ovsdb_monitor_selection select)
1194{
1195 struct ovsdb_monitor_table * mt;
1196
1197 mt = shash_find_data(&dbmon->tables, table->schema->name);
1198 mt->select |= select;
1199}
1200
58de87cc
AZ
1201 /*
1202 * If a row's change type (insert, delete or modify) matches that of
1203 * the monitor, they should be sent to the monitor's clients as updates.
1204 * Of cause, the monitor should also internally update with this change.
1205 *
1206 * When a change type does not require client side update, the monitor
1207 * may still need to keep track of certain changes in order to generate
1208 * correct future updates. For example, the monitor internal state should
1209 * be updated whenever a new row is inserted, in order to generate the
1210 * correct initial state, regardless if a insert change type is being
1211 * monitored.
1212 *
1213 * On the other hand, if a transaction only contains changes to columns
1214 * that are not monitored, this transaction can be safely ignored by the
1215 * monitor.
1216 *
1217 * Thus, the order of the declaration is important:
1218 * 'OVSDB_CHANGES_REQUIRE_EXTERNAL_UPDATE' always implies
1219 * 'OVSDB_CHANGES_REQUIRE_INTERNAL_UPDATE', but not vice versa. */
1220enum ovsdb_monitor_changes_efficacy {
1221 OVSDB_CHANGES_NO_EFFECT, /* Monitor does not care about this
1222 change. */
1223 OVSDB_CHANGES_REQUIRE_INTERNAL_UPDATE, /* Monitor internal updates. */
1224 OVSDB_CHANGES_REQUIRE_EXTERNAL_UPDATE, /* Client needs to be updated. */
1225};
1226
2fa1df7b
AZ
1227struct ovsdb_monitor_aux {
1228 const struct ovsdb_monitor *monitor;
1229 struct ovsdb_monitor_table *mt;
a0cba5c2 1230 enum ovsdb_monitor_changes_efficacy efficacy;
2fa1df7b
AZ
1231};
1232
1233static void
1234ovsdb_monitor_init_aux(struct ovsdb_monitor_aux *aux,
1235 const struct ovsdb_monitor *m)
1236{
1237 aux->monitor = m;
1238 aux->mt = NULL;
a0cba5c2 1239 aux->efficacy = OVSDB_CHANGES_NO_EFFECT;
2fa1df7b
AZ
1240}
1241
7e911055
AZ
1242static void
1243ovsdb_monitor_changes_update(const struct ovsdb_row *old,
1244 const struct ovsdb_row *new,
1245 const struct ovsdb_monitor_table *mt,
1246 struct ovsdb_monitor_changes *changes)
2fa1df7b 1247{
2fa1df7b
AZ
1248 const struct uuid *uuid = ovsdb_row_get_uuid(new ? new : old);
1249 struct ovsdb_monitor_row *change;
2fa1df7b 1250
7e911055 1251 change = ovsdb_monitor_changes_row_find(changes, uuid);
2fa1df7b 1252 if (!change) {
1158f320 1253 change = xzalloc(sizeof *change);
7e911055 1254 hmap_insert(&changes->rows, &change->hmap_node, uuid_hash(uuid));
2fa1df7b 1255 change->uuid = *uuid;
e0f42d4a
HZ
1256 change->old = clone_monitor_row_data(mt, old, changes->n_columns);
1257 change->new = clone_monitor_row_data(mt, new, changes->n_columns);
2fa1df7b
AZ
1258 } else {
1259 if (new) {
20853442
AZ
1260 if (!change->new) {
1261 /* Reinsert the row that was just deleted.
1262 *
1263 * This path won't be hit without replication. Whenever OVSDB
1264 * server inserts a new row, It always generates a new UUID
1265 * that is different from the row just deleted.
1266 *
1267 * With replication, this path can be hit in a corner
1268 * case when two OVSDB servers are set up to replicate
1269 * each other. Not that is a useful set up, but can
1270 * happen in practice.
1271 *
1272 * An example of how this path can be hit is documented below.
1273 * The details is not as important to the correctness of the
1274 * logic, but added here to convince ourselves that this path
1275 * can be hit.
1276 *
1277 * Imagine two OVSDB servers that replicates from each
1278 * other. For each replication session, there is a
1279 * corresponding monitor at the other end of the replication
1280 * JSONRPC connection.
1281 *
1282 * The events can lead to a back to back deletion and
1283 * insertion operation of the same row for the monitor of
1284 * the first server are:
1285 *
1286 * 1. A row is inserted in the first OVSDB server.
1287 * 2. The row is then replicated to the remote OVSDB server.
1288 * 3. The row is now deleted by the local OVSDB server. This
1289 * deletion operation is replicated to the local monitor
1290 * of the OVSDB server.
1291 * 4. The monitor now receives the same row, as an insertion,
1292 * from the replication server. Because of
1293 * replication, the row carries the same UUID as the row
1294 * just deleted.
1295 */
e0f42d4a
HZ
1296 change->new = clone_monitor_row_data(mt, new,
1297 changes->n_columns);
20853442 1298 } else {
e0f42d4a
HZ
1299 update_monitor_row_data(mt, new, change->new,
1300 changes->n_columns);
20853442 1301 }
2fa1df7b 1302 } else {
e0f42d4a 1303 free_monitor_row_data(mt, change->new, changes->n_columns);
2fa1df7b
AZ
1304 change->new = NULL;
1305
1306 if (!change->old) {
1307 /* This row was added then deleted. Forget about it. */
7e911055 1308 hmap_remove(&changes->rows, &change->hmap_node);
2fa1df7b
AZ
1309 free(change);
1310 }
1311 }
1312 }
7e911055
AZ
1313}
1314
58de87cc
AZ
1315static bool
1316ovsdb_monitor_columns_changed(const struct ovsdb_monitor_table *mt,
1317 const unsigned long int *changed)
1318{
1319 size_t i;
1320
1321 for (i = 0; i < mt->n_columns; i++) {
1322 size_t column_index = mt->columns[i].column->index;
1323
1324 if (bitmap_is_set(changed, column_index)) {
1325 return true;
1326 }
1327 }
1328
1329 return false;
1330}
1331
1332/* Return the efficacy of a row's change to a monitor table.
1333 *
1334 * Please see the block comment above 'ovsdb_monitor_changes_efficacy'
1335 * definition form more information. */
1336static enum ovsdb_monitor_changes_efficacy
1337ovsdb_monitor_changes_classify(enum ovsdb_monitor_selection type,
1338 const struct ovsdb_monitor_table *mt,
1339 const unsigned long int *changed)
1340{
1341 if (type == OJMS_MODIFY &&
1342 !ovsdb_monitor_columns_changed(mt, changed)) {
1343 return OVSDB_CHANGES_NO_EFFECT;
1344 }
1345
71cdf7cd
LS
1346 if (type == OJMS_MODIFY) {
1347 /* Condition might turn a modify operation to insert or delete */
1348 type |= OJMS_INSERT | OJMS_DELETE;
1349 }
1350
58de87cc
AZ
1351 return (mt->select & type)
1352 ? OVSDB_CHANGES_REQUIRE_EXTERNAL_UPDATE
1353 : OVSDB_CHANGES_REQUIRE_INTERNAL_UPDATE;
1354}
1355
7e911055
AZ
1356static bool
1357ovsdb_monitor_change_cb(const struct ovsdb_row *old,
1358 const struct ovsdb_row *new,
3a22387b 1359 const unsigned long int *changed,
7e911055
AZ
1360 void *aux_)
1361{
1362 struct ovsdb_monitor_aux *aux = aux_;
1363 const struct ovsdb_monitor *m = aux->monitor;
1364 struct ovsdb_table *table = new ? new->table : old->table;
1365 struct ovsdb_monitor_table *mt;
1366 struct ovsdb_monitor_changes *changes;
1367
1368 if (!aux->mt || table != aux->mt->table) {
1369 aux->mt = shash_find_data(&m->tables, table->schema->name);
1370 if (!aux->mt) {
1371 /* We don't care about rows in this table at all. Tell the caller
1372 * to skip it. */
1373 return false;
1374 }
1375 }
1376 mt = aux->mt;
1377
71cdf7cd
LS
1378 enum ovsdb_monitor_selection type =
1379 ovsdb_monitor_row_update_type(false, old, new);
1380 enum ovsdb_monitor_changes_efficacy efficacy =
1381 ovsdb_monitor_changes_classify(type, mt, changed);
58de87cc 1382
71cdf7cd 1383 HMAP_FOR_EACH(changes, hmap_node, &mt->changes) {
58de87cc
AZ
1384 if (efficacy > OVSDB_CHANGES_NO_EFFECT) {
1385 ovsdb_monitor_changes_update(old, new, mt, changes);
1386 }
71cdf7cd
LS
1387 }
1388 if (aux->efficacy < efficacy) {
1389 aux->efficacy = efficacy;
7e911055 1390 }
58de87cc 1391
2fa1df7b
AZ
1392 return true;
1393}
1394
61b63013 1395void
2fa1df7b
AZ
1396ovsdb_monitor_get_initial(const struct ovsdb_monitor *dbmon)
1397{
2fa1df7b 1398 struct shash_node *node;
2fa1df7b 1399
2fa1df7b
AZ
1400 SHASH_FOR_EACH (node, &dbmon->tables) {
1401 struct ovsdb_monitor_table *mt = node->data;
1402
1403 if (mt->select & OJMS_INITIAL) {
1404 struct ovsdb_row *row;
6e5a9216 1405 struct ovsdb_monitor_changes *changes;
2fa1df7b 1406
6e5a9216
AZ
1407 changes = ovsdb_monitor_table_find_changes(mt, 0);
1408 if (!changes) {
1409 changes = ovsdb_monitor_table_add_changes(mt, 0);
1410 HMAP_FOR_EACH (row, hmap_node, &mt->table->rows) {
1411 ovsdb_monitor_changes_update(NULL, row, mt, changes);
1412 }
1413 } else {
1414 changes->n_refs++;
2fa1df7b
AZ
1415 }
1416 }
1417 }
2fa1df7b
AZ
1418}
1419
1420void
d9412837 1421ovsdb_monitor_remove_jsonrpc_monitor(struct ovsdb_monitor *dbmon,
f76def25
AZ
1422 struct ovsdb_jsonrpc_monitor *jsonrpc_monitor,
1423 uint64_t unflushed)
d9412837
AZ
1424{
1425 struct jsonrpc_monitor_node *jm;
1426
417e7e66 1427 if (ovs_list_is_empty(&dbmon->jsonrpc_monitors)) {
6e5a9216
AZ
1428 ovsdb_monitor_destroy(dbmon);
1429 return;
1430 }
1431
d9412837
AZ
1432 /* Find and remove the jsonrpc monitor from the list. */
1433 LIST_FOR_EACH(jm, node, &dbmon->jsonrpc_monitors) {
1434 if (jm->jsonrpc_monitor == jsonrpc_monitor) {
f76def25
AZ
1435 /* Release the tracked changes. */
1436 struct shash_node *node;
1437 SHASH_FOR_EACH (node, &dbmon->tables) {
1438 struct ovsdb_monitor_table *mt = node->data;
1439 ovsdb_monitor_table_untrack_changes(mt, unflushed);
1440 }
417e7e66 1441 ovs_list_remove(&jm->node);
d9412837
AZ
1442 free(jm);
1443
1444 /* Destroy ovsdb monitor if this is the last user. */
417e7e66 1445 if (ovs_list_is_empty(&dbmon->jsonrpc_monitors)) {
d9412837
AZ
1446 ovsdb_monitor_destroy(dbmon);
1447 }
1448
1449 return;
1450 };
1451 }
1452
1453 /* Should never reach here. jsonrpc_monitor should be on the list. */
1454 OVS_NOT_REACHED();
1455}
1456
6e5a9216
AZ
1457static bool
1458ovsdb_monitor_table_equal(const struct ovsdb_monitor_table *a,
1459 const struct ovsdb_monitor_table *b)
1460{
1461 size_t i;
1462
6150a75f
LS
1463 ovs_assert(b->n_columns == b->n_monitored_columns);
1464
6e5a9216
AZ
1465 if ((a->table != b->table) ||
1466 (a->select != b->select) ||
6150a75f 1467 (a->n_monitored_columns != b->n_monitored_columns)) {
6e5a9216
AZ
1468 return false;
1469 }
1470
6150a75f
LS
1471 /* Compare only monitored columns that must be sorted already */
1472 for (i = 0; i < a->n_monitored_columns; i++) {
6e5a9216
AZ
1473 if ((a->columns[i].column != b->columns[i].column) ||
1474 (a->columns[i].select != b->columns[i].select)) {
1475 return false;
1476 }
1477 }
6e5a9216
AZ
1478 return true;
1479}
1480
1481static bool
1482ovsdb_monitor_equal(const struct ovsdb_monitor *a,
1483 const struct ovsdb_monitor *b)
1484{
1485 struct shash_node *node;
1486
1487 if (shash_count(&a->tables) != shash_count(&b->tables)) {
1488 return false;
1489 }
1490
1491 SHASH_FOR_EACH(node, &a->tables) {
1492 const struct ovsdb_monitor_table *mta = node->data;
1493 const struct ovsdb_monitor_table *mtb;
1494
1495 mtb = shash_find_data(&b->tables, node->name);
1496 if (!mtb) {
1497 return false;
1498 }
1499
1500 if (!ovsdb_monitor_table_equal(mta, mtb)) {
1501 return false;
1502 }
1503 }
1504
1505 return true;
1506}
1507
1508static size_t
1509ovsdb_monitor_hash(const struct ovsdb_monitor *dbmon, size_t basis)
1510{
1511 const struct shash_node **nodes;
1512 size_t i, j, n;
1513
1514 nodes = shash_sort(&dbmon->tables);
1515 n = shash_count(&dbmon->tables);
1516
1517 for (i = 0; i < n; i++) {
1518 struct ovsdb_monitor_table *mt = nodes[i]->data;
1519
1520 basis = hash_pointer(mt->table, basis);
1521 basis = hash_3words(mt->select, mt->n_columns, basis);
1522
1523 for (j = 0; j < mt->n_columns; j++) {
1524 basis = hash_pointer(mt->columns[j].column, basis);
1525 basis = hash_2words(mt->columns[j].select, basis);
1526 }
1527 }
1528 free(nodes);
1529
1530 return basis;
1531}
1532
1533struct ovsdb_monitor *
1534ovsdb_monitor_add(struct ovsdb_monitor *new_dbmon)
1535{
1536 struct ovsdb_monitor *dbmon;
1537 size_t hash;
1538
1539 /* New_dbmon should be associated with only one jsonrpc
1540 * connections. */
417e7e66 1541 ovs_assert(ovs_list_is_singleton(&new_dbmon->jsonrpc_monitors));
6e5a9216 1542
ec1eadce
LS
1543 ovsdb_monitor_columns_sort(new_dbmon);
1544
6e5a9216
AZ
1545 hash = ovsdb_monitor_hash(new_dbmon, 0);
1546 HMAP_FOR_EACH_WITH_HASH(dbmon, hmap_node, hash, &ovsdb_monitors) {
1547 if (ovsdb_monitor_equal(dbmon, new_dbmon)) {
1548 return dbmon;
1549 }
1550 }
1551
1552 hmap_insert(&ovsdb_monitors, &new_dbmon->hmap_node, hash);
1553 return new_dbmon;
1554}
1555
d9412837 1556static void
2fa1df7b
AZ
1557ovsdb_monitor_destroy(struct ovsdb_monitor *dbmon)
1558{
1559 struct shash_node *node;
1560
009bf21f 1561 ovs_list_remove(&dbmon->list_node);
2fa1df7b 1562
6e5a9216
AZ
1563 if (!hmap_node_is_null(&dbmon->hmap_node)) {
1564 hmap_remove(&ovsdb_monitors, &dbmon->hmap_node);
1565 }
1566
4c280978
AZ
1567 ovsdb_monitor_json_cache_flush(dbmon);
1568 hmap_destroy(&dbmon->json_cache);
1569
2fa1df7b
AZ
1570 SHASH_FOR_EACH (node, &dbmon->tables) {
1571 struct ovsdb_monitor_table *mt = node->data;
7e911055 1572 struct ovsdb_monitor_changes *changes, *next;
2fa1df7b 1573
7e911055
AZ
1574 HMAP_FOR_EACH_SAFE (changes, next, hmap_node, &mt->changes) {
1575 hmap_remove(&mt->changes, &changes->hmap_node);
1576 ovsdb_monitor_changes_destroy(changes);
1577 }
a7ca2803 1578 hmap_destroy(&mt->changes);
2fa1df7b 1579 free(mt->columns);
ec1eadce 1580 free(mt->columns_index_map);
2fa1df7b
AZ
1581 free(mt);
1582 }
1583 shash_destroy(&dbmon->tables);
1584 free(dbmon);
1585}
1586
120fb2ca 1587static void
009bf21f 1588ovsdb_monitor_commit(struct ovsdb_monitor *m, const struct ovsdb_txn *txn)
2fa1df7b 1589{
2fa1df7b
AZ
1590 struct ovsdb_monitor_aux aux;
1591
1592 ovsdb_monitor_init_aux(&aux, m);
5e229177
AZ
1593 /* Update ovsdb_monitor's transaction number for
1594 * each transaction, before calling ovsdb_monitor_change_cb(). */
1595 m->n_transactions++;
2fa1df7b 1596 ovsdb_txn_for_each_change(txn, ovsdb_monitor_change_cb, &aux);
a0cba5c2 1597
5e229177
AZ
1598 switch(aux.efficacy) {
1599 case OVSDB_CHANGES_NO_EFFECT:
1600 /* The transaction is ignored by the monitor.
1601 * Roll back the 'n_transactions' as if the transaction
1602 * has never happened. */
1603 m->n_transactions--;
1604 break;
1605 case OVSDB_CHANGES_REQUIRE_INTERNAL_UPDATE:
1606 /* Nothing. */
1607 break;
1608 case OVSDB_CHANGES_REQUIRE_EXTERNAL_UPDATE:
a0cba5c2 1609 ovsdb_monitor_json_cache_flush(m);
5e229177 1610 break;
a0cba5c2 1611 }
2fa1df7b
AZ
1612}
1613
009bf21f
BP
1614void
1615ovsdb_monitors_commit(struct ovsdb *db, const struct ovsdb_txn *txn)
2fa1df7b 1616{
009bf21f 1617 struct ovsdb_monitor *m;
2fa1df7b 1618
009bf21f
BP
1619 LIST_FOR_EACH (m, list_node, &db->monitors) {
1620 ovsdb_monitor_commit(m, txn);
1621 }
1622}
1623
1624void
1625ovsdb_monitors_remove(struct ovsdb *db)
1626{
1627 struct ovsdb_monitor *m, *next_m;
1628
1629 LIST_FOR_EACH_SAFE (m, next_m, list_node, &db->monitors) {
1630 struct jsonrpc_monitor_node *jm, *next_jm;
1631
1632 /* Delete all front-end monitors. Removing the last front-end monitor
1633 * will also destroy the corresponding ovsdb_monitor. */
1634 LIST_FOR_EACH_SAFE (jm, next_jm, node, &m->jsonrpc_monitors) {
53178986 1635 ovsdb_jsonrpc_monitor_destroy(jm->jsonrpc_monitor, false);
009bf21f 1636 }
d9412837 1637 }
2fa1df7b
AZ
1638}
1639
e0c73ed1
AZ
1640/* Add some memory usage statics for monitors into 'usage', for use with
1641 * memory_report(). */
1642void
1643ovsdb_monitor_get_memory_usage(struct simap *usage)
1644{
8302b6d1 1645 struct ovsdb_monitor *dbmon;
e0c73ed1 1646 simap_put(usage, "monitors", hmap_count(&ovsdb_monitors));
8302b6d1
AZ
1647
1648 HMAP_FOR_EACH(dbmon, hmap_node, &ovsdb_monitors) {
1649 simap_increase(usage, "json-caches", hmap_count(&dbmon->json_cache));
1650 }
e0c73ed1 1651}
53178986
BP
1652
1653void
1654ovsdb_monitor_prereplace_db(struct ovsdb *db)
1655{
1656 struct ovsdb_monitor *m, *next_m;
1657
1658 LIST_FOR_EACH_SAFE (m, next_m, list_node, &db->monitors) {
1659 struct jsonrpc_monitor_node *jm, *next_jm;
1660
1661 /* Delete all front-end monitors. Removing the last front-end monitor
1662 * will also destroy the corresponding ovsdb_monitor. */
1663 LIST_FOR_EACH_SAFE (jm, next_jm, node, &m->jsonrpc_monitors) {
1664 ovsdb_jsonrpc_monitor_destroy(jm->jsonrpc_monitor, true);
1665 }
1666 }
1667}