]>
Commit | Line | Data |
---|---|---|
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 AZ |
33 | #include "table.h" |
34 | #include "timeval.h" | |
35 | #include "transaction.h" | |
36 | #include "jsonrpc-server.h" | |
37 | #include "monitor.h" | |
ee89ea7b | 38 | #include "util.h" |
2fa1df7b AZ |
39 | #include "openvswitch/vlog.h" |
40 | ||
71cdf7cd | 41 | VLOG_DEFINE_THIS_MODULE(ovsdb_monitor); |
2fa1df7b | 42 | |
6e5a9216 | 43 | static struct hmap ovsdb_monitors = HMAP_INITIALIZER(&ovsdb_monitors); |
2fa1df7b | 44 | |
71cdf7cd LS |
45 | /* Keep state of session's conditions */ |
46 | struct ovsdb_monitor_session_condition { | |
e4ef6881 | 47 | bool conditional; /* True iff every table's condition is true. */ |
71cdf7cd LS |
48 | struct shash tables; /* Contains |
49 | * "struct ovsdb_monitor_table_condition *"s. */ | |
50 | }; | |
51 | ||
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; | |
58 | }; | |
59 | ||
2fa1df7b AZ |
60 | /* Backend monitor. |
61 | * | |
62 | * ovsdb_monitor keep track of the ovsdb changes. | |
63 | */ | |
64 | ||
65 | /* A collection of tables being monitored. */ | |
66 | struct ovsdb_monitor { | |
009bf21f | 67 | struct ovs_list list_node; /* In struct ovsdb's "monitors" list. */ |
2fa1df7b | 68 | struct shash tables; /* Holds "struct ovsdb_monitor_table"s. */ |
d9412837 | 69 | struct ovs_list jsonrpc_monitors; /* Contains "jsonrpc_monitor_node"s. */ |
2fa1df7b | 70 | struct ovsdb *db; |
222bed54 HZ |
71 | |
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; | |
78 | ||
79 | /* The new change set that is to be populated for future transactions. */ | |
80 | struct ovsdb_monitor_change_set *new_change_set; | |
81 | ||
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; | |
85 | ||
86 | struct hmap_node hmap_node; /* Elements within ovsdb_monitors. */ | |
87 | struct hmap json_cache; /* Contains "ovsdb_monitor_json_cache_node"s.*/ | |
4c280978 AZ |
88 | }; |
89 | ||
222bed54 HZ |
90 | /* A json object of updates for the ovsdb_monitor_change_set and the given |
91 | * monitor version. */ | |
4c280978 AZ |
92 | struct ovsdb_monitor_json_cache_node { |
93 | struct hmap_node hmap_node; /* Elements in json cache. */ | |
a32b4433 | 94 | enum ovsdb_monitor_version version; |
222bed54 | 95 | struct uuid change_set_uuid; |
4c280978 | 96 | struct json *json; /* Null, or a cloned of json */ |
2fa1df7b AZ |
97 | }; |
98 | ||
d9412837 | 99 | struct jsonrpc_monitor_node { |
d9412837 | 100 | struct ovs_list node; |
ad376c93 | 101 | struct ovsdb_jsonrpc_monitor *jsonrpc_monitor; |
d9412837 AZ |
102 | }; |
103 | ||
2fa1df7b AZ |
104 | /* A particular column being monitored. */ |
105 | struct ovsdb_monitor_column { | |
106 | const struct ovsdb_column *column; | |
107 | enum ovsdb_monitor_selection select; | |
6150a75f | 108 | bool monitored; |
2fa1df7b AZ |
109 | }; |
110 | ||
111 | /* A row that has changed in a monitored table. */ | |
112 | struct ovsdb_monitor_row { | |
222bed54 | 113 | struct hmap_node hmap_node; /* In ovsdb_monitor_change_set_for_table. */ |
2fa1df7b AZ |
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. */ | |
117 | }; | |
118 | ||
222bed54 HZ |
119 | /* Contains a set of changes that are not yet flushed to all the jsonrpc |
120 | * connections. | |
1158f320 | 121 | * |
222bed54 HZ |
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. | |
126 | */ | |
127 | struct ovsdb_monitor_change_set { | |
128 | /* Element in change_sets of ovsdb_monitor. */ | |
129 | struct ovs_list list_node; | |
130 | ||
131 | /* Internally generated uuid that identifies this data structure. */ | |
132 | struct uuid uuid; | |
133 | ||
134 | /* Contains struct ovsdb_monitor_change_set_for_table. */ | |
135 | struct ovs_list change_set_for_tables; | |
136 | ||
137 | int n_refs; | |
9167cb52 HZ |
138 | |
139 | /* The previous txn id before this change set's start point. */ | |
140 | struct uuid prev_txn; | |
222bed54 HZ |
141 | }; |
142 | ||
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; | |
149 | ||
150 | /* Element in ovsdb_monitor_change_sets' change_set_for_tables list. */ | |
151 | struct ovs_list list_in_change_set; | |
152 | ||
1158f320 | 153 | struct ovsdb_monitor_table *mt; |
222bed54 HZ |
154 | struct ovsdb_monitor_change_set *mcs; |
155 | ||
156 | /* Contains struct ovsdb_monitor_row. */ | |
1158f320 | 157 | struct hmap rows; |
e0f42d4a HZ |
158 | |
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. */ | |
163 | size_t n_columns; | |
1158f320 AZ |
164 | }; |
165 | ||
2fa1df7b AZ |
166 | /* A particular table being monitored. */ |
167 | struct ovsdb_monitor_table { | |
168 | const struct ovsdb_table *table; | |
169 | ||
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; | |
173 | ||
174 | /* Columns being monitored. */ | |
175 | struct ovsdb_monitor_column *columns; | |
176 | size_t n_columns; | |
6150a75f LS |
177 | size_t n_monitored_columns; |
178 | size_t allocated_columns; | |
2fa1df7b | 179 | |
ec1eadce LS |
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; | |
184 | ||
222bed54 HZ |
185 | /* Contains 'ovsdb_monitor_change_set_for_table'. */ |
186 | struct ovs_list change_sets; | |
2fa1df7b AZ |
187 | }; |
188 | ||
845a1187 LS |
189 | enum ovsdb_monitor_row_type { |
190 | OVSDB_ROW, | |
191 | OVSDB_MONITOR_ROW | |
192 | }; | |
193 | ||
52553aea | 194 | typedef struct json * |
71cdf7cd LS |
195 | (*compose_row_update_cb_func) |
196 | (const struct ovsdb_monitor_table *mt, | |
197 | const struct ovsdb_monitor_session_condition * condition, | |
845a1187 LS |
198 | enum ovsdb_monitor_row_type row_type, |
199 | const void *, | |
e0f42d4a HZ |
200 | bool initial, unsigned long int *changed, |
201 | size_t n_columns); | |
52553aea | 202 | |
be084595 | 203 | static void ovsdb_monitor_destroy(struct ovsdb_monitor *); |
222bed54 | 204 | static struct ovsdb_monitor_change_set * ovsdb_monitor_add_change_set( |
9167cb52 HZ |
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); | |
222bed54 HZ |
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 *); | |
d9412837 | 211 | |
a32b4433 | 212 | static uint32_t |
222bed54 HZ |
213 | json_cache_hash(enum ovsdb_monitor_version version, |
214 | struct ovsdb_monitor_change_set *change_set) | |
a32b4433 | 215 | { |
222bed54 | 216 | return hash_uint64_basis(version, uuid_hash(&change_set->uuid)); |
a32b4433 HZ |
217 | } |
218 | ||
4c280978 AZ |
219 | static struct ovsdb_monitor_json_cache_node * |
220 | ovsdb_monitor_json_cache_search(const struct ovsdb_monitor *dbmon, | |
a32b4433 | 221 | enum ovsdb_monitor_version version, |
222bed54 | 222 | struct ovsdb_monitor_change_set *change_set) |
4c280978 AZ |
223 | { |
224 | struct ovsdb_monitor_json_cache_node *node; | |
222bed54 | 225 | uint32_t hash = json_cache_hash(version, change_set); |
4c280978 AZ |
226 | |
227 | HMAP_FOR_EACH_WITH_HASH(node, hmap_node, hash, &dbmon->json_cache) { | |
222bed54 HZ |
228 | if (uuid_equals(&node->change_set_uuid, &change_set->uuid) && |
229 | node->version == version) { | |
4c280978 AZ |
230 | return node; |
231 | } | |
232 | } | |
233 | ||
234 | return NULL; | |
235 | } | |
236 | ||
237 | static void | |
238 | ovsdb_monitor_json_cache_insert(struct ovsdb_monitor *dbmon, | |
a32b4433 | 239 | enum ovsdb_monitor_version version, |
222bed54 HZ |
240 | struct ovsdb_monitor_change_set *change_set, |
241 | struct json *json) | |
4c280978 AZ |
242 | { |
243 | struct ovsdb_monitor_json_cache_node *node; | |
222bed54 | 244 | uint32_t hash = json_cache_hash(version, change_set); |
4c280978 AZ |
245 | |
246 | node = xmalloc(sizeof *node); | |
247 | ||
a32b4433 | 248 | node->version = version; |
222bed54 | 249 | node->change_set_uuid = change_set->uuid; |
4c280978 AZ |
250 | node->json = json ? json_clone(json) : NULL; |
251 | ||
252 | hmap_insert(&dbmon->json_cache, &node->hmap_node, hash); | |
253 | } | |
254 | ||
255 | static void | |
256 | ovsdb_monitor_json_cache_flush(struct ovsdb_monitor *dbmon) | |
257 | { | |
4ec3d7c7 | 258 | struct ovsdb_monitor_json_cache_node *node; |
4c280978 | 259 | |
4ec3d7c7 | 260 | HMAP_FOR_EACH_POP(node, hmap_node, &dbmon->json_cache) { |
4c280978 AZ |
261 | json_destroy(node->json); |
262 | free(node); | |
263 | } | |
264 | } | |
265 | ||
222bed54 HZ |
266 | /* Free all versions of json cache for a given change_set.*/ |
267 | static void | |
268 | ovsdb_monitor_json_cache_destroy(struct ovsdb_monitor *dbmon, | |
269 | struct ovsdb_monitor_change_set *change_set) | |
270 | { | |
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); | |
275 | if (node) { | |
276 | hmap_remove(&dbmon->json_cache, &node->hmap_node); | |
277 | json_destroy(node->json); | |
278 | free(node); | |
279 | } | |
280 | } | |
281 | } | |
282 | ||
2fa1df7b AZ |
283 | static int |
284 | compare_ovsdb_monitor_column(const void *a_, const void *b_) | |
285 | { | |
286 | const struct ovsdb_monitor_column *a = a_; | |
287 | const struct ovsdb_monitor_column *b = b_; | |
288 | ||
6150a75f LS |
289 | /* put all monitored columns at the begining */ |
290 | if (a->monitored != b->monitored) { | |
291 | return a->monitored ? -1 : 1; | |
292 | } | |
293 | ||
2fa1df7b AZ |
294 | return a->column < b->column ? -1 : a->column > b->column; |
295 | } | |
296 | ||
1158f320 | 297 | /* Finds and returns the ovsdb_monitor_row in 'mt->changes->rows' for the |
2fa1df7b AZ |
298 | * given 'uuid', or NULL if there is no such row. */ |
299 | static struct ovsdb_monitor_row * | |
222bed54 HZ |
300 | ovsdb_monitor_changes_row_find( |
301 | const struct ovsdb_monitor_change_set_for_table *changes, | |
302 | const struct uuid *uuid) | |
2fa1df7b AZ |
303 | { |
304 | struct ovsdb_monitor_row *row; | |
305 | ||
1158f320 | 306 | HMAP_FOR_EACH_WITH_HASH (row, hmap_node, uuid_hash(uuid), |
7e911055 | 307 | &changes->rows) { |
2fa1df7b AZ |
308 | if (uuid_equals(uuid, &row->uuid)) { |
309 | return row; | |
310 | } | |
311 | } | |
312 | return NULL; | |
313 | } | |
314 | ||
e0f42d4a | 315 | /* Allocates an array of 'n_columns' ovsdb_datums and initializes them as |
2fa1df7b AZ |
316 | * copies of the data in 'row' drawn from the columns represented by |
317 | * mt->columns[]. Returns the array. | |
318 | * | |
319 | * If 'row' is NULL, returns NULL. */ | |
320 | static struct ovsdb_datum * | |
321 | clone_monitor_row_data(const struct ovsdb_monitor_table *mt, | |
e0f42d4a HZ |
322 | const struct ovsdb_row *row, |
323 | size_t n_columns) | |
2fa1df7b AZ |
324 | { |
325 | struct ovsdb_datum *data; | |
326 | size_t i; | |
327 | ||
328 | if (!row) { | |
329 | return NULL; | |
330 | } | |
331 | ||
e0f42d4a HZ |
332 | data = xmalloc(n_columns * sizeof *data); |
333 | for (i = 0; i < n_columns; i++) { | |
2fa1df7b AZ |
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; | |
338 | ||
339 | ovsdb_datum_clone(dst, src, type); | |
340 | } | |
341 | return data; | |
342 | } | |
343 | ||
e0f42d4a | 344 | /* Replaces the n_columns ovsdb_datums in row[] by copies of the data from |
2fa1df7b AZ |
345 | * in 'row' drawn from the columns represented by mt->columns[]. */ |
346 | static void | |
347 | update_monitor_row_data(const struct ovsdb_monitor_table *mt, | |
348 | const struct ovsdb_row *row, | |
e0f42d4a HZ |
349 | struct ovsdb_datum *data, |
350 | size_t n_columns) | |
2fa1df7b AZ |
351 | { |
352 | size_t i; | |
353 | ||
e0f42d4a | 354 | for (i = 0; i < n_columns; i++) { |
2fa1df7b AZ |
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; | |
359 | ||
360 | if (!ovsdb_datum_equals(src, dst, type)) { | |
361 | ovsdb_datum_destroy(dst, type); | |
362 | ovsdb_datum_clone(dst, src, type); | |
363 | } | |
364 | } | |
365 | } | |
366 | ||
e0f42d4a | 367 | /* Frees all of the n_columns ovsdb_datums in data[], using the types taken |
2fa1df7b AZ |
368 | * from mt->columns[], plus 'data' itself. */ |
369 | static void | |
370 | free_monitor_row_data(const struct ovsdb_monitor_table *mt, | |
e0f42d4a HZ |
371 | struct ovsdb_datum *data, |
372 | size_t n_columns) | |
2fa1df7b AZ |
373 | { |
374 | if (data) { | |
375 | size_t i; | |
376 | ||
e0f42d4a | 377 | for (i = 0; i < n_columns; i++) { |
2fa1df7b AZ |
378 | const struct ovsdb_column *c = mt->columns[i].column; |
379 | ||
380 | ovsdb_datum_destroy(&data[i], &c->type); | |
381 | } | |
382 | free(data); | |
383 | } | |
384 | } | |
385 | ||
386 | /* Frees 'row', which must have been created from 'mt'. */ | |
387 | static void | |
388 | ovsdb_monitor_row_destroy(const struct ovsdb_monitor_table *mt, | |
e0f42d4a HZ |
389 | struct ovsdb_monitor_row *row, |
390 | size_t n_columns) | |
2fa1df7b AZ |
391 | { |
392 | if (row) { | |
e0f42d4a HZ |
393 | free_monitor_row_data(mt, row->old, n_columns); |
394 | free_monitor_row_data(mt, row->new, n_columns); | |
2fa1df7b AZ |
395 | free(row); |
396 | } | |
397 | } | |
398 | ||
ec1eadce LS |
399 | static void |
400 | ovsdb_monitor_columns_sort(struct ovsdb_monitor *dbmon) | |
401 | { | |
402 | int i; | |
403 | struct shash_node *node; | |
404 | ||
405 | SHASH_FOR_EACH (node, &dbmon->tables) { | |
406 | struct ovsdb_monitor_table *mt = node->data; | |
407 | ||
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; | |
413 | } | |
414 | } | |
415 | } | |
416 | ||
6e5a9216 | 417 | void |
36247a75 AZ |
418 | ovsdb_monitor_add_jsonrpc_monitor(struct ovsdb_monitor *dbmon, |
419 | struct ovsdb_jsonrpc_monitor *jsonrpc_monitor) | |
420 | { | |
421 | struct jsonrpc_monitor_node *jm; | |
422 | ||
423 | jm = xzalloc(sizeof *jm); | |
424 | jm->jsonrpc_monitor = jsonrpc_monitor; | |
417e7e66 | 425 | ovs_list_push_back(&dbmon->jsonrpc_monitors, &jm->node); |
36247a75 AZ |
426 | } |
427 | ||
2fa1df7b AZ |
428 | struct ovsdb_monitor * |
429 | ovsdb_monitor_create(struct ovsdb *db, | |
430 | struct ovsdb_jsonrpc_monitor *jsonrpc_monitor) | |
431 | { | |
432 | struct ovsdb_monitor *dbmon; | |
433 | ||
434 | dbmon = xzalloc(sizeof *dbmon); | |
435 | ||
009bf21f | 436 | ovs_list_push_back(&db->monitors, &dbmon->list_node); |
417e7e66 | 437 | ovs_list_init(&dbmon->jsonrpc_monitors); |
2fa1df7b | 438 | dbmon->db = db; |
222bed54 | 439 | ovs_list_init(&dbmon->change_sets); |
2fa1df7b | 440 | shash_init(&dbmon->tables); |
6e5a9216 | 441 | hmap_node_nullify(&dbmon->hmap_node); |
4c280978 | 442 | hmap_init(&dbmon->json_cache); |
2fa1df7b | 443 | |
36247a75 | 444 | ovsdb_monitor_add_jsonrpc_monitor(dbmon, jsonrpc_monitor); |
2fa1df7b AZ |
445 | return dbmon; |
446 | } | |
447 | ||
448 | void | |
449 | ovsdb_monitor_add_table(struct ovsdb_monitor *m, | |
450 | const struct ovsdb_table *table) | |
451 | { | |
452 | struct ovsdb_monitor_table *mt; | |
ec1eadce LS |
453 | int i; |
454 | size_t n_columns = shash_count(&table->schema->columns); | |
2fa1df7b AZ |
455 | |
456 | mt = xzalloc(sizeof *mt); | |
457 | mt->table = table; | |
2fa1df7b | 458 | shash_add(&m->tables, table->schema->name, mt); |
222bed54 | 459 | ovs_list_init(&mt->change_sets); |
ec1eadce LS |
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; | |
464 | } | |
2fa1df7b AZ |
465 | } |
466 | ||
ec1eadce | 467 | const char * |
2fa1df7b AZ |
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, | |
6150a75f | 472 | bool monitored) |
2fa1df7b AZ |
473 | { |
474 | struct ovsdb_monitor_table *mt; | |
475 | struct ovsdb_monitor_column *c; | |
476 | ||
477 | mt = shash_find_data(&dbmon->tables, table->schema->name); | |
478 | ||
ec1eadce LS |
479 | /* Check for column duplication. Return duplicated column name. */ |
480 | if (mt->columns_index_map[column->index] != -1) { | |
481 | return column->name; | |
482 | } | |
483 | ||
6150a75f LS |
484 | if (mt->n_columns >= mt->allocated_columns) { |
485 | mt->columns = x2nrealloc(mt->columns, &mt->allocated_columns, | |
2fa1df7b AZ |
486 | sizeof *mt->columns); |
487 | } | |
488 | ||
489 | mt->select |= select; | |
ec1eadce | 490 | mt->columns_index_map[column->index] = mt->n_columns; |
2fa1df7b AZ |
491 | c = &mt->columns[mt->n_columns++]; |
492 | c->column = column; | |
493 | c->select = select; | |
6150a75f LS |
494 | c->monitored = monitored; |
495 | if (monitored) { | |
496 | mt->n_monitored_columns++; | |
497 | } | |
2fa1df7b AZ |
498 | |
499 | return NULL; | |
500 | } | |
501 | ||
71cdf7cd LS |
502 | static void |
503 | ovsdb_monitor_condition_add_columns(struct ovsdb_monitor *dbmon, | |
504 | const struct ovsdb_table *table, | |
505 | struct ovsdb_condition *condition) | |
506 | { | |
507 | size_t n_columns; | |
508 | int i; | |
509 | const struct ovsdb_column **columns = | |
510 | ovsdb_condition_get_columns(condition, &n_columns); | |
511 | ||
512 | for (i = 0; i < n_columns; i++) { | |
513 | ovsdb_monitor_add_column(dbmon, table, columns[i], | |
514 | OJMS_NONE, false); | |
515 | } | |
516 | ||
517 | free(columns); | |
518 | } | |
519 | ||
520 | /* Bind this session's condition to ovsdb_monitor */ | |
521 | void | |
522 | ovsdb_monitor_condition_bind(struct ovsdb_monitor *dbmon, | |
523 | struct ovsdb_monitor_session_condition *cond) | |
524 | { | |
525 | struct shash_node *node; | |
526 | ||
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); | |
531 | ||
532 | mtc->mt = mt; | |
533 | ovsdb_monitor_condition_add_columns(dbmon, mtc->table, | |
534 | &mtc->new_condition); | |
535 | } | |
536 | } | |
537 | ||
845a1187 LS |
538 | bool |
539 | ovsdb_monitor_table_exists(struct ovsdb_monitor *m, | |
540 | const struct ovsdb_table *table) | |
541 | { | |
542 | return shash_find_data(&m->tables, table->schema->name); | |
543 | } | |
544 | ||
222bed54 HZ |
545 | static struct ovsdb_monitor_change_set * |
546 | ovsdb_monitor_add_change_set(struct ovsdb_monitor *dbmon, | |
9167cb52 | 547 | bool init_only, const struct uuid *prev_txn) |
1158f320 | 548 | { |
222bed54 HZ |
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; | |
9167cb52 | 554 | change_set->prev_txn = prev_txn ? *prev_txn : UUID_ZERO; |
6e5a9216 | 555 | |
222bed54 HZ |
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); | |
562 | mcst->mt = mt; | |
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); | |
7e911055 AZ |
569 | } |
570 | } | |
571 | ||
222bed54 HZ |
572 | return change_set; |
573 | }; | |
1158f320 | 574 | |
9167cb52 HZ |
575 | static struct ovsdb_monitor_change_set * |
576 | ovsdb_monitor_find_change_set(const struct ovsdb_monitor *dbmon, | |
577 | const struct uuid *prev_txn) | |
578 | { | |
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; | |
591 | break; | |
592 | } | |
593 | } | |
594 | if (n_col_is_equal) { | |
595 | return cs; | |
596 | } | |
597 | } | |
598 | } | |
599 | return NULL; | |
600 | } | |
601 | ||
1158f320 | 602 | static void |
222bed54 HZ |
603 | ovsdb_monitor_untrack_change_set(struct ovsdb_monitor *dbmon, |
604 | struct ovsdb_monitor_change_set *mcs) | |
1158f320 | 605 | { |
222bed54 HZ |
606 | ovs_assert(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; | |
1158f320 | 612 | } |
222bed54 HZ |
613 | ovsdb_monitor_json_cache_destroy(dbmon, mcs); |
614 | ovsdb_monitor_change_set_destroy(mcs); | |
1158f320 AZ |
615 | } |
616 | } | |
617 | ||
1158f320 | 618 | static void |
222bed54 | 619 | ovsdb_monitor_track_new_change_set(struct ovsdb_monitor *dbmon) |
1158f320 | 620 | { |
222bed54 | 621 | struct ovsdb_monitor_change_set *change_set = dbmon->new_change_set; |
7e911055 | 622 | |
222bed54 HZ |
623 | if (change_set) { |
624 | change_set->n_refs++; | |
7e911055 | 625 | } else { |
9167cb52 HZ |
626 | change_set = ovsdb_monitor_add_change_set(dbmon, false, |
627 | ovsdb_monitor_get_last_txnid(dbmon)); | |
222bed54 | 628 | dbmon->new_change_set = change_set; |
7e911055 | 629 | } |
1158f320 AZ |
630 | } |
631 | ||
632 | static void | |
222bed54 | 633 | ovsdb_monitor_change_set_destroy(struct ovsdb_monitor_change_set *mcs) |
1158f320 | 634 | { |
222bed54 | 635 | ovs_list_remove(&mcs->list_node); |
1158f320 | 636 | |
222bed54 HZ |
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); | |
642 | ||
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); | |
647 | } | |
648 | hmap_destroy(&mcst->rows); | |
649 | ||
650 | free(mcst); | |
1158f320 | 651 | } |
222bed54 | 652 | free(mcs); |
1158f320 AZ |
653 | } |
654 | ||
23cbedb7 AZ |
655 | static enum ovsdb_monitor_selection |
656 | ovsdb_monitor_row_update_type(bool initial, const bool old, const bool new) | |
657 | { | |
658 | return initial ? OJMS_INITIAL | |
659 | : !old ? OJMS_INSERT | |
660 | : !new ? OJMS_DELETE | |
661 | : OJMS_MODIFY; | |
662 | } | |
71cdf7cd LS |
663 | |
664 | /* Set conditional monitoring mode only if we have non-empty condition in one | |
665 | * of the tables at least */ | |
666 | static inline void | |
667 | ovsdb_monitor_session_condition_set_mode( | |
668 | struct ovsdb_monitor_session_condition *cond) | |
669 | { | |
e4ef6881 BP |
670 | struct shash_node *node; |
671 | ||
672 | SHASH_FOR_EACH (node, &cond->tables) { | |
673 | struct ovsdb_monitor_table_condition *mtc = node->data; | |
674 | ||
675 | if (!ovsdb_condition_is_true(&mtc->new_condition)) { | |
676 | cond->conditional = true; | |
677 | return; | |
678 | } | |
679 | } | |
680 | cond->conditional = false; | |
71cdf7cd LS |
681 | } |
682 | ||
683 | /* Returnes an empty allocated session's condition state holder */ | |
684 | struct ovsdb_monitor_session_condition * | |
685 | ovsdb_monitor_session_condition_create(void) | |
686 | { | |
687 | struct ovsdb_monitor_session_condition *condition = | |
688 | xzalloc(sizeof *condition); | |
689 | ||
690 | condition->conditional = false; | |
691 | shash_init(&condition->tables); | |
692 | return condition; | |
693 | } | |
694 | ||
695 | void | |
696 | ovsdb_monitor_session_condition_destroy( | |
697 | struct ovsdb_monitor_session_condition *condition) | |
698 | { | |
699 | struct shash_node *node, *next; | |
700 | ||
701 | if (!condition) { | |
702 | return; | |
703 | } | |
704 | ||
705 | SHASH_FOR_EACH_SAFE (node, next, &condition->tables) { | |
706 | struct ovsdb_monitor_table_condition *mtc = node->data; | |
707 | ||
708 | ovsdb_condition_destroy(&mtc->new_condition); | |
709 | ovsdb_condition_destroy(&mtc->old_condition); | |
710 | shash_delete(&condition->tables, node); | |
711 | free(mtc); | |
712 | } | |
d3c8a7e8 | 713 | shash_destroy(&condition->tables); |
71cdf7cd LS |
714 | free(condition); |
715 | } | |
716 | ||
717 | struct ovsdb_error * | |
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) | |
722 | { | |
723 | struct ovsdb_monitor_table_condition *mtc; | |
724 | struct ovsdb_error *error; | |
725 | ||
726 | mtc = xzalloc(sizeof *mtc); | |
727 | mtc->table = table; | |
728 | ovsdb_condition_init(&mtc->old_condition); | |
729 | ovsdb_condition_init(&mtc->new_condition); | |
730 | ||
731 | if (json_cnd) { | |
732 | error = ovsdb_condition_from_json(table->schema, | |
733 | json_cnd, | |
734 | NULL, | |
735 | &mtc->old_condition); | |
736 | if (error) { | |
737 | free(mtc); | |
738 | return error; | |
739 | } | |
740 | } | |
741 | ||
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); | |
5707b87d | 745 | ovsdb_monitor_session_condition_set_mode(condition); |
71cdf7cd LS |
746 | |
747 | return NULL; | |
748 | } | |
749 | ||
750 | static bool | |
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) | |
756 | { | |
757 | if (!condition) { | |
758 | return false; | |
759 | } | |
760 | ||
761 | struct ovsdb_monitor_table_condition *mtc = | |
762 | shash_find_data(&condition->tables, mt->table->schema->name); | |
763 | ||
764 | if (!mtc) { | |
765 | return false; | |
766 | } | |
767 | *old_condition = &mtc->old_condition; | |
768 | *new_condition = &mtc->new_condition; | |
769 | ||
770 | return true; | |
771 | } | |
772 | ||
845a1187 LS |
773 | struct ovsdb_error * |
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) | |
779 | { | |
c1efab9b BP |
780 | if (!condition) { |
781 | return NULL; | |
782 | } | |
783 | ||
845a1187 LS |
784 | struct ovsdb_monitor_table_condition *mtc = |
785 | shash_find_data(&condition->tables, table->schema->name); | |
786 | struct ovsdb_error *error; | |
f0d7ae19 | 787 | struct ovsdb_condition cond = OVSDB_CONDITION_INITIALIZER(&cond); |
845a1187 | 788 | |
845a1187 LS |
789 | error = ovsdb_condition_from_json(table->schema, cond_json, |
790 | NULL, &cond); | |
791 | if (error) { | |
792 | return error; | |
793 | } | |
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, | |
798 | table, | |
799 | &mtc->new_condition); | |
800 | ||
801 | return NULL; | |
802 | } | |
803 | ||
804 | static void | |
805 | ovsdb_monitor_table_condition_updated(struct ovsdb_monitor_table *mt, | |
806 | struct ovsdb_monitor_session_condition *condition) | |
807 | { | |
808 | struct ovsdb_monitor_table_condition *mtc = | |
809 | shash_find_data(&condition->tables, mt->table->schema->name); | |
810 | ||
811 | if (mtc) { | |
812 | /* If conditional monitoring - set old condition to new condition */ | |
813 | if (ovsdb_condition_cmp_3way(&mtc->old_condition, | |
814 | &mtc->new_condition)) { | |
845a1187 LS |
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); | |
818 | } | |
819 | } | |
820 | } | |
821 | ||
71cdf7cd LS |
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, | |
826 | bool initial, | |
845a1187 | 827 | enum ovsdb_monitor_row_type row_type, |
71cdf7cd LS |
828 | const struct ovsdb_datum *old, |
829 | const struct ovsdb_datum *new) | |
830 | { | |
831 | struct ovsdb_condition *old_condition, *new_condition; | |
832 | enum ovsdb_monitor_selection type = | |
833 | ovsdb_monitor_row_update_type(initial, old, new); | |
834 | ||
835 | if (ovsdb_monitor_get_table_conditions(mt, | |
836 | condition, | |
837 | &old_condition, | |
838 | &new_condition)) { | |
839 | bool old_cond = !old ? false | |
840 | : ovsdb_condition_empty_or_match_any(old, | |
845a1187 LS |
841 | old_condition, |
842 | row_type == OVSDB_MONITOR_ROW ? | |
843 | mt->columns_index_map : | |
844 | NULL); | |
71cdf7cd LS |
845 | bool new_cond = !new ? false |
846 | : ovsdb_condition_empty_or_match_any(new, | |
845a1187 LS |
847 | new_condition, |
848 | row_type == OVSDB_MONITOR_ROW ? | |
849 | mt->columns_index_map : | |
850 | NULL); | |
71cdf7cd LS |
851 | |
852 | if (!old_cond && !new_cond) { | |
853 | type = OJMS_NONE; | |
854 | } | |
855 | ||
856 | switch (type) { | |
857 | case OJMS_INITIAL: | |
858 | case OJMS_INSERT: | |
859 | if (!new_cond) { | |
860 | type = OJMS_NONE; | |
861 | } | |
862 | break; | |
863 | case OJMS_MODIFY: | |
864 | type = !old_cond ? OJMS_INSERT : !new_cond | |
865 | ? OJMS_DELETE : OJMS_MODIFY; | |
866 | break; | |
867 | case OJMS_DELETE: | |
868 | if (!old_cond) { | |
869 | type = OJMS_NONE; | |
870 | } | |
871 | break; | |
872 | case OJMS_NONE: | |
873 | break; | |
874 | } | |
875 | } | |
876 | return type; | |
877 | } | |
878 | ||
52553aea AZ |
879 | static bool |
880 | ovsdb_monitor_row_skip_update(const struct ovsdb_monitor_table *mt, | |
845a1187 LS |
881 | enum ovsdb_monitor_row_type row_type, |
882 | const struct ovsdb_datum *old, | |
883 | const struct ovsdb_datum *new, | |
52553aea | 884 | enum ovsdb_monitor_selection type, |
e0f42d4a HZ |
885 | unsigned long int *changed, |
886 | size_t n_columns) | |
52553aea AZ |
887 | { |
888 | if (!(mt->select & type)) { | |
889 | return true; | |
890 | } | |
891 | ||
892 | if (type == OJMS_MODIFY) { | |
893 | size_t i, n_changes; | |
894 | ||
895 | n_changes = 0; | |
e0f42d4a HZ |
896 | memset(changed, 0, bitmap_n_bytes(n_columns)); |
897 | for (i = 0; i < n_columns; i++) { | |
52553aea | 898 | const struct ovsdb_column *c = mt->columns[i].column; |
845a1187 LS |
899 | size_t index = row_type == OVSDB_ROW ? c->index : i; |
900 | if (!ovsdb_datum_equals(&old[index], &new[index], &c->type)) { | |
52553aea AZ |
901 | bitmap_set1(changed, i); |
902 | n_changes++; | |
903 | } | |
904 | } | |
905 | if (!n_changes) { | |
906 | /* No actual changes: presumably a row changed and then | |
907 | * changed back later. */ | |
908 | return true; | |
909 | } | |
910 | } | |
911 | ||
912 | return false; | |
913 | } | |
23cbedb7 | 914 | |
2fa1df7b AZ |
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. | |
917 | * | |
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. | |
921 | * | |
922 | * 'changed' must be a scratch buffer for internal use that is at least | |
e0f42d4a | 923 | * bitmap_n_bytes(n_columns) bytes long. */ |
2fa1df7b AZ |
924 | static struct json * |
925 | ovsdb_monitor_compose_row_update( | |
926 | const struct ovsdb_monitor_table *mt, | |
71cdf7cd | 927 | const struct ovsdb_monitor_session_condition *condition OVS_UNUSED, |
845a1187 LS |
928 | enum ovsdb_monitor_row_type row_type OVS_UNUSED, |
929 | const void *_row, | |
e0f42d4a HZ |
930 | bool initial, unsigned long int *changed, |
931 | size_t n_columns OVS_UNUSED) | |
2fa1df7b | 932 | { |
845a1187 | 933 | const struct ovsdb_monitor_row *row = _row; |
2fa1df7b AZ |
934 | enum ovsdb_monitor_selection type; |
935 | struct json *old_json, *new_json; | |
936 | struct json *row_json; | |
937 | size_t i; | |
938 | ||
845a1187 | 939 | ovs_assert(row_type == OVSDB_MONITOR_ROW); |
23cbedb7 | 940 | type = ovsdb_monitor_row_update_type(initial, row->old, row->new); |
845a1187 | 941 | if (ovsdb_monitor_row_skip_update(mt, row_type, row->old, |
e0f42d4a HZ |
942 | row->new, type, changed, |
943 | mt->n_columns)) { | |
2fa1df7b AZ |
944 | return NULL; |
945 | } | |
946 | ||
2fa1df7b AZ |
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); | |
952 | } | |
953 | if (type & (OJMS_INITIAL | OJMS_INSERT | OJMS_MODIFY)) { | |
954 | new_json = json_object_create(); | |
955 | json_object_put(row_json, "new", new_json); | |
956 | } | |
6150a75f | 957 | for (i = 0; i < mt->n_monitored_columns; i++) { |
2fa1df7b AZ |
958 | const struct ovsdb_monitor_column *c = &mt->columns[i]; |
959 | ||
6150a75f | 960 | if (!c->monitored || !(type & c->select)) { |
2fa1df7b AZ |
961 | /* We don't care about this type of change for this |
962 | * particular column (but we will care about it for some | |
963 | * other column). */ | |
964 | continue; | |
965 | } | |
966 | ||
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], | |
971 | &c->column->type)); | |
972 | } | |
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], | |
976 | &c->column->type)); | |
977 | } | |
978 | } | |
979 | ||
980 | return row_json; | |
981 | } | |
982 | ||
52553aea AZ |
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. | |
985 | * | |
986 | * The caller should specify 'initial' as true if the returned JSON is | |
845a1187 | 987 | * going to be used as part of the initial reply to a "monitor_cond" request, |
52553aea AZ |
988 | * false if it is going to be used as part of an "update2" notification. |
989 | * | |
990 | * 'changed' must be a scratch buffer for internal use that is at least | |
e0f42d4a | 991 | * bitmap_n_bytes(n_columns) bytes long. */ |
52553aea AZ |
992 | static struct json * |
993 | ovsdb_monitor_compose_row_update2( | |
994 | const struct ovsdb_monitor_table *mt, | |
71cdf7cd | 995 | const struct ovsdb_monitor_session_condition *condition, |
845a1187 LS |
996 | enum ovsdb_monitor_row_type row_type, |
997 | const void *_row, | |
e0f42d4a HZ |
998 | bool initial, unsigned long int *changed, |
999 | size_t n_columns) | |
52553aea AZ |
1000 | { |
1001 | enum ovsdb_monitor_selection type; | |
1002 | struct json *row_update2, *diff_json; | |
845a1187 | 1003 | const struct ovsdb_datum *old, *new; |
52553aea AZ |
1004 | size_t i; |
1005 | ||
845a1187 LS |
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; | |
1009 | } else { | |
1010 | old = new = ((const struct ovsdb_row *)_row)->fields; | |
1011 | } | |
1012 | ||
71cdf7cd | 1013 | type = ovsdb_monitor_row_update_type_condition(mt, condition, initial, |
845a1187 | 1014 | row_type, old, new); |
e0f42d4a HZ |
1015 | if (ovsdb_monitor_row_skip_update(mt, row_type, old, new, type, changed, |
1016 | n_columns)) { | |
52553aea AZ |
1017 | return NULL; |
1018 | } | |
1019 | ||
1020 | row_update2 = json_object_create(); | |
1021 | if (type == OJMS_DELETE) { | |
1022 | json_object_put(row_update2, "delete", json_null_create()); | |
1023 | } else { | |
1024 | diff_json = json_object_create(); | |
1025 | const char *op; | |
1026 | ||
6150a75f | 1027 | for (i = 0; i < mt->n_monitored_columns; i++) { |
52553aea | 1028 | const struct ovsdb_monitor_column *c = &mt->columns[i]; |
845a1187 | 1029 | size_t index = row_type == OVSDB_ROW ? c->column->index : i; |
6150a75f | 1030 | if (!c->monitored || !(type & c->select)) { |
52553aea AZ |
1031 | /* We don't care about this type of change for this |
1032 | * particular column (but we will care about it for some | |
1033 | * other column). */ | |
1034 | continue; | |
1035 | } | |
1036 | ||
1037 | if (type == OJMS_MODIFY) { | |
1038 | struct ovsdb_datum diff; | |
1039 | ||
1040 | if (!bitmap_is_set(changed, i)) { | |
1041 | continue; | |
1042 | } | |
1043 | ||
845a1187 | 1044 | ovsdb_datum_diff(&diff ,&old[index], &new[index], |
52553aea AZ |
1045 | &c->column->type); |
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); | |
1049 | } else { | |
845a1187 | 1050 | if (!ovsdb_datum_is_default(&new[index], &c->column->type)) { |
52553aea | 1051 | json_object_put(diff_json, c->column->name, |
845a1187 | 1052 | ovsdb_datum_to_json(&new[index], |
52553aea AZ |
1053 | &c->column->type)); |
1054 | } | |
1055 | } | |
1056 | } | |
1057 | ||
1058 | op = type == OJMS_INITIAL ? "initial" | |
1059 | : type == OJMS_MODIFY ? "modify" : "insert"; | |
1060 | json_object_put(row_update2, op, diff_json); | |
1061 | } | |
1062 | ||
1063 | return row_update2; | |
1064 | } | |
1065 | ||
6cef8e87 AZ |
1066 | static size_t |
1067 | ovsdb_monitor_max_columns(struct ovsdb_monitor *dbmon) | |
1068 | { | |
1069 | struct shash_node *node; | |
1070 | size_t max_columns = 0; | |
1071 | ||
1072 | SHASH_FOR_EACH (node, &dbmon->tables) { | |
1073 | struct ovsdb_monitor_table *mt = node->data; | |
1074 | ||
1075 | max_columns = MAX(max_columns, mt->n_columns); | |
1076 | } | |
1077 | ||
1078 | return max_columns; | |
1079 | } | |
1080 | ||
845a1187 LS |
1081 | static void |
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) | |
1085 | { | |
1086 | char uuid[UUID_LEN + 1]; | |
1087 | ||
1088 | /* Create JSON object for transaction overall. */ | |
1089 | if (!*json) { | |
1090 | *json = json_object_create(); | |
1091 | } | |
1092 | ||
1093 | /* Create JSON object for transaction on this table. */ | |
1094 | if (!*table_json) { | |
1095 | *table_json = json_object_create(); | |
1096 | json_object_put(*json, table_name, *table_json); | |
1097 | } | |
1098 | ||
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); | |
1102 | } | |
1103 | ||
2fa1df7b | 1104 | /* Constructs and returns JSON for a <table-updates> object (as described in |
4c280978 AZ |
1105 | * RFC 7047) for all the outstanding changes within 'monitor', starting from |
1106 | * 'transaction'. */ | |
1107 | static struct json* | |
71cdf7cd LS |
1108 | ovsdb_monitor_compose_update( |
1109 | struct ovsdb_monitor *dbmon, | |
222bed54 | 1110 | bool initial, struct ovsdb_monitor_change_set *mcs, |
71cdf7cd LS |
1111 | const struct ovsdb_monitor_session_condition *condition, |
1112 | compose_row_update_cb_func row_update) | |
2fa1df7b | 1113 | { |
2fa1df7b | 1114 | struct json *json; |
6cef8e87 AZ |
1115 | size_t max_columns = ovsdb_monitor_max_columns(dbmon); |
1116 | unsigned long int *changed = xmalloc(bitmap_n_bytes(max_columns)); | |
2fa1df7b AZ |
1117 | |
1118 | json = NULL; | |
222bed54 HZ |
1119 | struct ovsdb_monitor_change_set_for_table *mcst; |
1120 | LIST_FOR_EACH (mcst, list_in_change_set, &mcs->change_set_for_tables) { | |
2fa1df7b AZ |
1121 | struct ovsdb_monitor_row *row, *next; |
1122 | struct json *table_json = NULL; | |
222bed54 | 1123 | struct ovsdb_monitor_table *mt = mcst->mt; |
2fa1df7b | 1124 | |
222bed54 | 1125 | HMAP_FOR_EACH_SAFE (row, next, hmap_node, &mcst->rows) { |
70ec021a AS |
1126 | struct json *row_json; |
1127 | row_json = (*row_update)(mt, condition, OVSDB_MONITOR_ROW, row, | |
222bed54 | 1128 | initial, changed, mcst->n_columns); |
70ec021a AS |
1129 | if (row_json) { |
1130 | ovsdb_monitor_add_json_row(&json, mt->table->schema->name, | |
1131 | &table_json, row_json, | |
1132 | &row->uuid); | |
1133 | } | |
1134 | } | |
1135 | } | |
845a1187 | 1136 | free(changed); |
2fa1df7b | 1137 | |
845a1187 LS |
1138 | return json; |
1139 | } | |
2fa1df7b | 1140 | |
845a1187 LS |
1141 | static struct json* |
1142 | ovsdb_monitor_compose_cond_change_update( | |
1143 | struct ovsdb_monitor *dbmon, | |
1144 | struct ovsdb_monitor_session_condition *condition) | |
1145 | { | |
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)); | |
2fa1df7b | 1150 | |
845a1187 LS |
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; | |
1156 | ||
1157 | if (!ovsdb_monitor_get_table_conditions(mt, | |
1158 | condition, | |
1159 | &old_condition, | |
1160 | &new_condition) || | |
1161 | !ovsdb_condition_cmp_3way(old_condition, new_condition)) { | |
1162 | /* Nothing to update on this table */ | |
1163 | continue; | |
1164 | } | |
2fa1df7b | 1165 | |
845a1187 LS |
1166 | /* Iterate over all rows in table */ |
1167 | HMAP_FOR_EACH (row, hmap_node, &mt->table->rows) { | |
1168 | struct json *row_json; | |
1169 | ||
1170 | row_json = ovsdb_monitor_compose_row_update2(mt, condition, | |
1171 | OVSDB_ROW, row, | |
e0f42d4a HZ |
1172 | false, changed, |
1173 | mt->n_columns); | |
845a1187 LS |
1174 | if (row_json) { |
1175 | ovsdb_monitor_add_json_row(&json, mt->table->schema->name, | |
1176 | &table_json, row_json, | |
1177 | ovsdb_row_get_uuid(row)); | |
2fa1df7b | 1178 | } |
2fa1df7b | 1179 | } |
845a1187 | 1180 | ovsdb_monitor_table_condition_updated(mt, condition); |
4c280978 AZ |
1181 | } |
1182 | free(changed); | |
1183 | ||
1184 | return json; | |
1185 | } | |
1186 | ||
1187 | /* Returns JSON for a <table-updates> object (as described in RFC 7047) | |
222bed54 HZ |
1188 | * for all the outstanding changes in dbmon that are tracked by the change set |
1189 | * *p_mcs. | |
1190 | * | |
845a1187 LS |
1191 | * If cond_updated is true all rows in the db that match conditions will be |
1192 | * sent. | |
4c280978 AZ |
1193 | * |
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. */ | |
1197 | struct json * | |
71cdf7cd LS |
1198 | ovsdb_monitor_get_update( |
1199 | struct ovsdb_monitor *dbmon, | |
845a1187 | 1200 | bool initial, bool cond_updated, |
845a1187 | 1201 | struct ovsdb_monitor_session_condition *condition, |
be084595 | 1202 | enum ovsdb_monitor_version version, |
222bed54 | 1203 | struct ovsdb_monitor_change_set **p_mcs) |
4c280978 | 1204 | { |
71cdf7cd | 1205 | struct ovsdb_monitor_json_cache_node *cache_node = NULL; |
4c280978 | 1206 | struct json *json; |
222bed54 | 1207 | struct ovsdb_monitor_change_set *mcs = *p_mcs; |
4c280978 | 1208 | |
222bed54 | 1209 | ovs_assert(cond_updated ? mcs == dbmon->new_change_set : true); |
845a1187 | 1210 | |
4c280978 AZ |
1211 | /* Return a clone of cached json if one exists. Otherwise, |
1212 | * generate a new one and add it to the cache. */ | |
845a1187 | 1213 | if (!condition || (!condition->conditional && !cond_updated)) { |
71cdf7cd | 1214 | cache_node = ovsdb_monitor_json_cache_search(dbmon, version, |
222bed54 | 1215 | mcs); |
71cdf7cd | 1216 | } |
4c280978 AZ |
1217 | if (cache_node) { |
1218 | json = cache_node->json ? json_clone(cache_node->json) : NULL; | |
1219 | } else { | |
52553aea | 1220 | if (version == OVSDB_MONITOR_V1) { |
71cdf7cd | 1221 | json = |
222bed54 | 1222 | ovsdb_monitor_compose_update(dbmon, initial, mcs, |
71cdf7cd LS |
1223 | condition, |
1224 | ovsdb_monitor_compose_row_update); | |
52553aea | 1225 | } else { |
9167cb52 HZ |
1226 | ovs_assert(version == OVSDB_MONITOR_V2 || |
1227 | version == OVSDB_MONITOR_V3); | |
1228 | ||
845a1187 | 1229 | if (!cond_updated) { |
222bed54 | 1230 | json = ovsdb_monitor_compose_update(dbmon, initial, mcs, |
70ec021a AS |
1231 | condition, |
1232 | ovsdb_monitor_compose_row_update2); | |
70ec021a | 1233 | if (!condition || !condition->conditional) { |
222bed54 | 1234 | ovsdb_monitor_json_cache_insert(dbmon, version, mcs, |
70ec021a AS |
1235 | json); |
1236 | } | |
1237 | } else { | |
845a1187 LS |
1238 | /* Compose update on whole db due to condition update. |
1239 | Session must be flushed (change list is empty)*/ | |
70ec021a AS |
1240 | json = |
1241 | ovsdb_monitor_compose_cond_change_update(dbmon, condition); | |
1242 | } | |
1243 | } | |
4c280978 AZ |
1244 | } |
1245 | ||
222bed54 HZ |
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; | |
4c280978 | 1250 | |
2fa1df7b AZ |
1251 | return json; |
1252 | } | |
1253 | ||
1254 | bool | |
59c35e11 | 1255 | ovsdb_monitor_needs_flush(struct ovsdb_monitor *dbmon, |
222bed54 | 1256 | struct ovsdb_monitor_change_set *change_set) |
2fa1df7b | 1257 | { |
222bed54 HZ |
1258 | ovs_assert(change_set); |
1259 | return (change_set != dbmon->new_change_set); | |
2fa1df7b AZ |
1260 | } |
1261 | ||
1262 | void | |
1263 | ovsdb_monitor_table_add_select(struct ovsdb_monitor *dbmon, | |
1264 | const struct ovsdb_table *table, | |
1265 | enum ovsdb_monitor_selection select) | |
1266 | { | |
1267 | struct ovsdb_monitor_table * mt; | |
1268 | ||
1269 | mt = shash_find_data(&dbmon->tables, table->schema->name); | |
1270 | mt->select |= select; | |
1271 | } | |
1272 | ||
58de87cc AZ |
1273 | /* |
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. | |
1277 | * | |
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 | |
1283 | * monitored. | |
1284 | * | |
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 | |
1287 | * monitor. | |
1288 | * | |
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 | |
1294 | change. */ | |
1295 | OVSDB_CHANGES_REQUIRE_INTERNAL_UPDATE, /* Monitor internal updates. */ | |
1296 | OVSDB_CHANGES_REQUIRE_EXTERNAL_UPDATE, /* Client needs to be updated. */ | |
1297 | }; | |
1298 | ||
2fa1df7b AZ |
1299 | struct ovsdb_monitor_aux { |
1300 | const struct ovsdb_monitor *monitor; | |
1301 | struct ovsdb_monitor_table *mt; | |
a0cba5c2 | 1302 | enum ovsdb_monitor_changes_efficacy efficacy; |
2fa1df7b AZ |
1303 | }; |
1304 | ||
1305 | static void | |
1306 | ovsdb_monitor_init_aux(struct ovsdb_monitor_aux *aux, | |
1307 | const struct ovsdb_monitor *m) | |
1308 | { | |
1309 | aux->monitor = m; | |
1310 | aux->mt = NULL; | |
a0cba5c2 | 1311 | aux->efficacy = OVSDB_CHANGES_NO_EFFECT; |
2fa1df7b AZ |
1312 | } |
1313 | ||
7e911055 AZ |
1314 | static void |
1315 | ovsdb_monitor_changes_update(const struct ovsdb_row *old, | |
1316 | const struct ovsdb_row *new, | |
1317 | const struct ovsdb_monitor_table *mt, | |
222bed54 | 1318 | struct ovsdb_monitor_change_set_for_table *mcst) |
2fa1df7b | 1319 | { |
2fa1df7b AZ |
1320 | const struct uuid *uuid = ovsdb_row_get_uuid(new ? new : old); |
1321 | struct ovsdb_monitor_row *change; | |
2fa1df7b | 1322 | |
222bed54 | 1323 | change = ovsdb_monitor_changes_row_find(mcst, uuid); |
2fa1df7b | 1324 | if (!change) { |
1158f320 | 1325 | change = xzalloc(sizeof *change); |
222bed54 | 1326 | hmap_insert(&mcst->rows, &change->hmap_node, uuid_hash(uuid)); |
2fa1df7b | 1327 | change->uuid = *uuid; |
222bed54 HZ |
1328 | change->old = clone_monitor_row_data(mt, old, mcst->n_columns); |
1329 | change->new = clone_monitor_row_data(mt, new, mcst->n_columns); | |
2fa1df7b AZ |
1330 | } else { |
1331 | if (new) { | |
20853442 AZ |
1332 | if (!change->new) { |
1333 | /* Reinsert the row that was just deleted. | |
1334 | * | |
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. | |
1338 | * | |
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. | |
1343 | * | |
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 | |
1347 | * can be hit. | |
1348 | * | |
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. | |
1353 | * | |
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: | |
1357 | * | |
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 | |
1366 | * just deleted. | |
1367 | */ | |
222bed54 | 1368 | change->new = clone_monitor_row_data(mt, new, mcst->n_columns); |
20853442 | 1369 | } else { |
222bed54 | 1370 | update_monitor_row_data(mt, new, change->new, mcst->n_columns); |
20853442 | 1371 | } |
2fa1df7b | 1372 | } else { |
222bed54 | 1373 | free_monitor_row_data(mt, change->new, mcst->n_columns); |
2fa1df7b AZ |
1374 | change->new = NULL; |
1375 | ||
1376 | if (!change->old) { | |
1377 | /* This row was added then deleted. Forget about it. */ | |
222bed54 | 1378 | hmap_remove(&mcst->rows, &change->hmap_node); |
2fa1df7b AZ |
1379 | free(change); |
1380 | } | |
1381 | } | |
1382 | } | |
7e911055 AZ |
1383 | } |
1384 | ||
58de87cc AZ |
1385 | static bool |
1386 | ovsdb_monitor_columns_changed(const struct ovsdb_monitor_table *mt, | |
1387 | const unsigned long int *changed) | |
1388 | { | |
1389 | size_t i; | |
1390 | ||
1391 | for (i = 0; i < mt->n_columns; i++) { | |
1392 | size_t column_index = mt->columns[i].column->index; | |
1393 | ||
1394 | if (bitmap_is_set(changed, column_index)) { | |
1395 | return true; | |
1396 | } | |
1397 | } | |
1398 | ||
1399 | return false; | |
1400 | } | |
1401 | ||
1402 | /* Return the efficacy of a row's change to a monitor table. | |
1403 | * | |
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) | |
1410 | { | |
1411 | if (type == OJMS_MODIFY && | |
1412 | !ovsdb_monitor_columns_changed(mt, changed)) { | |
1413 | return OVSDB_CHANGES_NO_EFFECT; | |
1414 | } | |
1415 | ||
71cdf7cd LS |
1416 | if (type == OJMS_MODIFY) { |
1417 | /* Condition might turn a modify operation to insert or delete */ | |
1418 | type |= OJMS_INSERT | OJMS_DELETE; | |
1419 | } | |
1420 | ||
58de87cc AZ |
1421 | return (mt->select & type) |
1422 | ? OVSDB_CHANGES_REQUIRE_EXTERNAL_UPDATE | |
1423 | : OVSDB_CHANGES_REQUIRE_INTERNAL_UPDATE; | |
1424 | } | |
1425 | ||
7e911055 AZ |
1426 | static bool |
1427 | ovsdb_monitor_change_cb(const struct ovsdb_row *old, | |
1428 | const struct ovsdb_row *new, | |
3a22387b | 1429 | const unsigned long int *changed, |
7e911055 AZ |
1430 | void *aux_) |
1431 | { | |
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; | |
222bed54 | 1436 | struct ovsdb_monitor_change_set_for_table *mcst; |
7e911055 AZ |
1437 | |
1438 | if (!aux->mt || table != aux->mt->table) { | |
1439 | aux->mt = shash_find_data(&m->tables, table->schema->name); | |
1440 | if (!aux->mt) { | |
1441 | /* We don't care about rows in this table at all. Tell the caller | |
1442 | * to skip it. */ | |
1443 | return false; | |
1444 | } | |
1445 | } | |
1446 | mt = aux->mt; | |
1447 | ||
71cdf7cd LS |
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); | |
58de87cc | 1452 | |
222bed54 HZ |
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); | |
58de87cc | 1456 | } |
71cdf7cd LS |
1457 | } |
1458 | if (aux->efficacy < efficacy) { | |
1459 | aux->efficacy = efficacy; | |
7e911055 | 1460 | } |
58de87cc | 1461 | |
2fa1df7b AZ |
1462 | return true; |
1463 | } | |
1464 | ||
61b63013 | 1465 | void |
222bed54 HZ |
1466 | ovsdb_monitor_get_initial(struct ovsdb_monitor *dbmon, |
1467 | struct ovsdb_monitor_change_set **p_mcs) | |
2fa1df7b | 1468 | { |
222bed54 HZ |
1469 | if (!dbmon->init_change_set) { |
1470 | struct ovsdb_monitor_change_set *change_set = | |
9167cb52 | 1471 | ovsdb_monitor_add_change_set(dbmon, true, NULL); |
222bed54 HZ |
1472 | dbmon->init_change_set = change_set; |
1473 | ||
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); | |
6e5a9216 | 1481 | } |
2fa1df7b AZ |
1482 | } |
1483 | } | |
222bed54 HZ |
1484 | } else { |
1485 | dbmon->init_change_set->n_refs++; | |
2fa1df7b | 1486 | } |
222bed54 HZ |
1487 | |
1488 | *p_mcs = dbmon->init_change_set; | |
2fa1df7b AZ |
1489 | } |
1490 | ||
9167cb52 HZ |
1491 | static bool |
1492 | ovsdb_monitor_history_change_cb(const struct ovsdb_row *old, | |
1493 | const struct ovsdb_row *new, | |
1494 | const unsigned long int *changed, | |
1495 | void *aux) | |
1496 | { | |
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; | |
1500 | ||
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); | |
1510 | } | |
1511 | return true; | |
1512 | } | |
1513 | } | |
1514 | return false; | |
1515 | } | |
1516 | ||
1517 | void | |
1518 | ovsdb_monitor_get_changes_after(const struct uuid *txn_uuid, | |
1519 | struct ovsdb_monitor *dbmon, | |
1520 | struct ovsdb_monitor_change_set **p_mcs) | |
1521 | { | |
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); | |
1526 | if (change_set) { | |
1527 | change_set->n_refs++; | |
1528 | *p_mcs = change_set; | |
1529 | return; | |
1530 | } | |
1531 | ||
1532 | struct ovsdb_txn_history_node *h_node; | |
1533 | bool found = false; | |
1534 | LIST_FOR_EACH (h_node, node, &dbmon->db->txn_history) { | |
1535 | struct ovsdb_txn *txn = h_node->txn; | |
1536 | if (!found) { | |
1537 | /* find the txn with last_id in history */ | |
1538 | if (uuid_equals(ovsdb_txn_get_txnid(txn), txn_uuid)) { | |
1539 | found = true; | |
1540 | change_set = ovsdb_monitor_add_change_set(dbmon, false, | |
1541 | txn_uuid); | |
1542 | } | |
1543 | } else { | |
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, | |
1547 | change_set); | |
1548 | } | |
1549 | } | |
1550 | *p_mcs = change_set; | |
1551 | } | |
1552 | ||
2fa1df7b | 1553 | void |
d9412837 | 1554 | ovsdb_monitor_remove_jsonrpc_monitor(struct ovsdb_monitor *dbmon, |
f76def25 | 1555 | struct ovsdb_jsonrpc_monitor *jsonrpc_monitor, |
222bed54 | 1556 | struct ovsdb_monitor_change_set *change_set) |
d9412837 AZ |
1557 | { |
1558 | struct jsonrpc_monitor_node *jm; | |
1559 | ||
417e7e66 | 1560 | if (ovs_list_is_empty(&dbmon->jsonrpc_monitors)) { |
6e5a9216 AZ |
1561 | ovsdb_monitor_destroy(dbmon); |
1562 | return; | |
1563 | } | |
1564 | ||
d9412837 AZ |
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) { | |
f76def25 | 1568 | /* Release the tracked changes. */ |
222bed54 HZ |
1569 | if (change_set) { |
1570 | ovsdb_monitor_untrack_change_set(dbmon, change_set); | |
f76def25 | 1571 | } |
417e7e66 | 1572 | ovs_list_remove(&jm->node); |
d9412837 AZ |
1573 | free(jm); |
1574 | ||
1575 | /* Destroy ovsdb monitor if this is the last user. */ | |
417e7e66 | 1576 | if (ovs_list_is_empty(&dbmon->jsonrpc_monitors)) { |
d9412837 AZ |
1577 | ovsdb_monitor_destroy(dbmon); |
1578 | } | |
1579 | ||
1580 | return; | |
1581 | }; | |
1582 | } | |
1583 | ||
1584 | /* Should never reach here. jsonrpc_monitor should be on the list. */ | |
1585 | OVS_NOT_REACHED(); | |
1586 | } | |
1587 | ||
6e5a9216 AZ |
1588 | static bool |
1589 | ovsdb_monitor_table_equal(const struct ovsdb_monitor_table *a, | |
1590 | const struct ovsdb_monitor_table *b) | |
1591 | { | |
1592 | size_t i; | |
1593 | ||
6150a75f LS |
1594 | ovs_assert(b->n_columns == b->n_monitored_columns); |
1595 | ||
6e5a9216 AZ |
1596 | if ((a->table != b->table) || |
1597 | (a->select != b->select) || | |
6150a75f | 1598 | (a->n_monitored_columns != b->n_monitored_columns)) { |
6e5a9216 AZ |
1599 | return false; |
1600 | } | |
1601 | ||
6150a75f LS |
1602 | /* Compare only monitored columns that must be sorted already */ |
1603 | for (i = 0; i < a->n_monitored_columns; i++) { | |
6e5a9216 AZ |
1604 | if ((a->columns[i].column != b->columns[i].column) || |
1605 | (a->columns[i].select != b->columns[i].select)) { | |
1606 | return false; | |
1607 | } | |
1608 | } | |
6e5a9216 AZ |
1609 | return true; |
1610 | } | |
1611 | ||
1612 | static bool | |
1613 | ovsdb_monitor_equal(const struct ovsdb_monitor *a, | |
1614 | const struct ovsdb_monitor *b) | |
1615 | { | |
1616 | struct shash_node *node; | |
1617 | ||
1618 | if (shash_count(&a->tables) != shash_count(&b->tables)) { | |
1619 | return false; | |
1620 | } | |
1621 | ||
1622 | SHASH_FOR_EACH(node, &a->tables) { | |
1623 | const struct ovsdb_monitor_table *mta = node->data; | |
1624 | const struct ovsdb_monitor_table *mtb; | |
1625 | ||
1626 | mtb = shash_find_data(&b->tables, node->name); | |
1627 | if (!mtb) { | |
1628 | return false; | |
1629 | } | |
1630 | ||
1631 | if (!ovsdb_monitor_table_equal(mta, mtb)) { | |
1632 | return false; | |
1633 | } | |
1634 | } | |
1635 | ||
1636 | return true; | |
1637 | } | |
1638 | ||
1639 | static size_t | |
1640 | ovsdb_monitor_hash(const struct ovsdb_monitor *dbmon, size_t basis) | |
1641 | { | |
1642 | const struct shash_node **nodes; | |
1643 | size_t i, j, n; | |
1644 | ||
1645 | nodes = shash_sort(&dbmon->tables); | |
1646 | n = shash_count(&dbmon->tables); | |
1647 | ||
1648 | for (i = 0; i < n; i++) { | |
1649 | struct ovsdb_monitor_table *mt = nodes[i]->data; | |
1650 | ||
1651 | basis = hash_pointer(mt->table, basis); | |
1652 | basis = hash_3words(mt->select, mt->n_columns, basis); | |
1653 | ||
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); | |
1657 | } | |
1658 | } | |
1659 | free(nodes); | |
1660 | ||
1661 | return basis; | |
1662 | } | |
1663 | ||
1664 | struct ovsdb_monitor * | |
1665 | ovsdb_monitor_add(struct ovsdb_monitor *new_dbmon) | |
1666 | { | |
1667 | struct ovsdb_monitor *dbmon; | |
1668 | size_t hash; | |
1669 | ||
1670 | /* New_dbmon should be associated with only one jsonrpc | |
1671 | * connections. */ | |
417e7e66 | 1672 | ovs_assert(ovs_list_is_singleton(&new_dbmon->jsonrpc_monitors)); |
6e5a9216 | 1673 | |
ec1eadce LS |
1674 | ovsdb_monitor_columns_sort(new_dbmon); |
1675 | ||
6e5a9216 AZ |
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)) { | |
1679 | return dbmon; | |
1680 | } | |
1681 | } | |
1682 | ||
1683 | hmap_insert(&ovsdb_monitors, &new_dbmon->hmap_node, hash); | |
1684 | return new_dbmon; | |
1685 | } | |
1686 | ||
d9412837 | 1687 | static void |
2fa1df7b AZ |
1688 | ovsdb_monitor_destroy(struct ovsdb_monitor *dbmon) |
1689 | { | |
1690 | struct shash_node *node; | |
1691 | ||
009bf21f | 1692 | ovs_list_remove(&dbmon->list_node); |
2fa1df7b | 1693 | |
6e5a9216 AZ |
1694 | if (!hmap_node_is_null(&dbmon->hmap_node)) { |
1695 | hmap_remove(&ovsdb_monitors, &dbmon->hmap_node); | |
1696 | } | |
1697 | ||
4c280978 AZ |
1698 | ovsdb_monitor_json_cache_flush(dbmon); |
1699 | hmap_destroy(&dbmon->json_cache); | |
1700 | ||
222bed54 HZ |
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); | |
1704 | } | |
1705 | ||
2fa1df7b AZ |
1706 | SHASH_FOR_EACH (node, &dbmon->tables) { |
1707 | struct ovsdb_monitor_table *mt = node->data; | |
222bed54 | 1708 | ovs_assert(ovs_list_is_empty(&mt->change_sets)); |
2fa1df7b | 1709 | free(mt->columns); |
ec1eadce | 1710 | free(mt->columns_index_map); |
2fa1df7b AZ |
1711 | free(mt); |
1712 | } | |
1713 | shash_destroy(&dbmon->tables); | |
1714 | free(dbmon); | |
1715 | } | |
1716 | ||
120fb2ca | 1717 | static void |
009bf21f | 1718 | ovsdb_monitor_commit(struct ovsdb_monitor *m, const struct ovsdb_txn *txn) |
2fa1df7b | 1719 | { |
2fa1df7b AZ |
1720 | struct ovsdb_monitor_aux aux; |
1721 | ||
1722 | ovsdb_monitor_init_aux(&aux, m); | |
1723 | ovsdb_txn_for_each_change(txn, ovsdb_monitor_change_cb, &aux); | |
a0cba5c2 | 1724 | |
222bed54 HZ |
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; | |
1730 | ||
1731 | if (aux.efficacy == OVSDB_CHANGES_REQUIRE_EXTERNAL_UPDATE) { | |
1732 | ovsdb_monitor_json_cache_flush(m); | |
1733 | } | |
a0cba5c2 | 1734 | } |
2fa1df7b AZ |
1735 | } |
1736 | ||
009bf21f BP |
1737 | void |
1738 | ovsdb_monitors_commit(struct ovsdb *db, const struct ovsdb_txn *txn) | |
2fa1df7b | 1739 | { |
009bf21f | 1740 | struct ovsdb_monitor *m; |
2fa1df7b | 1741 | |
009bf21f BP |
1742 | LIST_FOR_EACH (m, list_node, &db->monitors) { |
1743 | ovsdb_monitor_commit(m, txn); | |
1744 | } | |
1745 | } | |
1746 | ||
1747 | void | |
1748 | ovsdb_monitors_remove(struct ovsdb *db) | |
1749 | { | |
1750 | struct ovsdb_monitor *m, *next_m; | |
1751 | ||
1752 | LIST_FOR_EACH_SAFE (m, next_m, list_node, &db->monitors) { | |
1753 | struct jsonrpc_monitor_node *jm, *next_jm; | |
1754 | ||
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) { | |
53178986 | 1758 | ovsdb_jsonrpc_monitor_destroy(jm->jsonrpc_monitor, false); |
009bf21f | 1759 | } |
d9412837 | 1760 | } |
2fa1df7b AZ |
1761 | } |
1762 | ||
e0c73ed1 AZ |
1763 | /* Add some memory usage statics for monitors into 'usage', for use with |
1764 | * memory_report(). */ | |
1765 | void | |
1766 | ovsdb_monitor_get_memory_usage(struct simap *usage) | |
1767 | { | |
8302b6d1 | 1768 | struct ovsdb_monitor *dbmon; |
e0c73ed1 | 1769 | simap_put(usage, "monitors", hmap_count(&ovsdb_monitors)); |
8302b6d1 AZ |
1770 | |
1771 | HMAP_FOR_EACH(dbmon, hmap_node, &ovsdb_monitors) { | |
1772 | simap_increase(usage, "json-caches", hmap_count(&dbmon->json_cache)); | |
1773 | } | |
e0c73ed1 | 1774 | } |
53178986 BP |
1775 | |
1776 | void | |
1777 | ovsdb_monitor_prereplace_db(struct ovsdb *db) | |
1778 | { | |
1779 | struct ovsdb_monitor *m, *next_m; | |
1780 | ||
1781 | LIST_FOR_EACH_SAFE (m, next_m, list_node, &db->monitors) { | |
1782 | struct jsonrpc_monitor_node *jm, *next_jm; | |
1783 | ||
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); | |
1788 | } | |
1789 | } | |
1790 | } | |
9167cb52 HZ |
1791 | |
1792 | const struct uuid * | |
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); | |
1800 | } | |
1801 | return &dummy; | |
1802 | } |