2 * Copyright (c) 2015 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
23 #include "dynamic-string.h"
26 #include "ovsdb-error.h"
27 #include "ovsdb-parser.h"
33 #include "transaction.h"
34 #include "jsonrpc-server.h"
36 #include "openvswitch/vlog.h"
39 static const struct ovsdb_replica_class ovsdb_jsonrpc_replica_class;
43 * ovsdb_monitor keep track of the ovsdb changes.
46 /* A collection of tables being monitored. */
47 struct ovsdb_monitor {
48 struct ovsdb_replica replica;
49 struct shash tables; /* Holds "struct ovsdb_monitor_table"s. */
50 struct ovs_list jsonrpc_monitors; /* Contains "jsonrpc_monitor_node"s. */
54 struct jsonrpc_monitor_node {
55 struct ovsdb_jsonrpc_monitor *jsonrpc_monitor;
59 /* A particular column being monitored. */
60 struct ovsdb_monitor_column {
61 const struct ovsdb_column *column;
62 enum ovsdb_monitor_selection select;
65 /* A row that has changed in a monitored table. */
66 struct ovsdb_monitor_row {
67 struct hmap_node hmap_node; /* In ovsdb_jsonrpc_monitor_table.changes. */
68 struct uuid uuid; /* UUID of row that changed. */
69 struct ovsdb_datum *old; /* Old data, NULL for an inserted row. */
70 struct ovsdb_datum *new; /* New data, NULL for a deleted row. */
73 /* A particular table being monitored. */
74 struct ovsdb_monitor_table {
75 const struct ovsdb_table *table;
77 /* This is the union (bitwise-OR) of the 'select' values in all of the
78 * members of 'columns' below. */
79 enum ovsdb_monitor_selection select;
81 /* Columns being monitored. */
82 struct ovsdb_monitor_column *columns;
85 /* Contains 'struct ovsdb_monitor_row's for rows that have been
86 * updated but not yet flushed to the jsonrpc connection. */
90 static void ovsdb_monitor_destroy(struct ovsdb_monitor *dbmon);
93 compare_ovsdb_monitor_column(const void *a_, const void *b_)
95 const struct ovsdb_monitor_column *a = a_;
96 const struct ovsdb_monitor_column *b = b_;
98 return a->column < b->column ? -1 : a->column > b->column;
101 static struct ovsdb_monitor *
102 ovsdb_monitor_cast(struct ovsdb_replica *replica)
104 ovs_assert(replica->class == &ovsdb_jsonrpc_replica_class);
105 return CONTAINER_OF(replica, struct ovsdb_monitor, replica);
108 /* Finds and returns the ovsdb_monitor_row in 'mt->changes' for the
109 * given 'uuid', or NULL if there is no such row. */
110 static struct ovsdb_monitor_row *
111 ovsdb_monitor_row_find(const struct ovsdb_monitor_table *mt,
112 const struct uuid *uuid)
114 struct ovsdb_monitor_row *row;
116 HMAP_FOR_EACH_WITH_HASH (row, hmap_node, uuid_hash(uuid), &mt->changes) {
117 if (uuid_equals(uuid, &row->uuid)) {
124 /* Allocates an array of 'mt->n_columns' ovsdb_datums and initializes them as
125 * copies of the data in 'row' drawn from the columns represented by
126 * mt->columns[]. Returns the array.
128 * If 'row' is NULL, returns NULL. */
129 static struct ovsdb_datum *
130 clone_monitor_row_data(const struct ovsdb_monitor_table *mt,
131 const struct ovsdb_row *row)
133 struct ovsdb_datum *data;
140 data = xmalloc(mt->n_columns * sizeof *data);
141 for (i = 0; i < mt->n_columns; i++) {
142 const struct ovsdb_column *c = mt->columns[i].column;
143 const struct ovsdb_datum *src = &row->fields[c->index];
144 struct ovsdb_datum *dst = &data[i];
145 const struct ovsdb_type *type = &c->type;
147 ovsdb_datum_clone(dst, src, type);
152 /* Replaces the mt->n_columns ovsdb_datums in row[] by copies of the data from
153 * in 'row' drawn from the columns represented by mt->columns[]. */
155 update_monitor_row_data(const struct ovsdb_monitor_table *mt,
156 const struct ovsdb_row *row,
157 struct ovsdb_datum *data)
161 for (i = 0; i < mt->n_columns; i++) {
162 const struct ovsdb_column *c = mt->columns[i].column;
163 const struct ovsdb_datum *src = &row->fields[c->index];
164 struct ovsdb_datum *dst = &data[i];
165 const struct ovsdb_type *type = &c->type;
167 if (!ovsdb_datum_equals(src, dst, type)) {
168 ovsdb_datum_destroy(dst, type);
169 ovsdb_datum_clone(dst, src, type);
174 /* Frees all of the mt->n_columns ovsdb_datums in data[], using the types taken
175 * from mt->columns[], plus 'data' itself. */
177 free_monitor_row_data(const struct ovsdb_monitor_table *mt,
178 struct ovsdb_datum *data)
183 for (i = 0; i < mt->n_columns; i++) {
184 const struct ovsdb_column *c = mt->columns[i].column;
186 ovsdb_datum_destroy(&data[i], &c->type);
192 /* Frees 'row', which must have been created from 'mt'. */
194 ovsdb_monitor_row_destroy(const struct ovsdb_monitor_table *mt,
195 struct ovsdb_monitor_row *row)
198 free_monitor_row_data(mt, row->old);
199 free_monitor_row_data(mt, row->new);
204 struct ovsdb_monitor *
205 ovsdb_monitor_create(struct ovsdb *db,
206 struct ovsdb_jsonrpc_monitor *jsonrpc_monitor)
208 struct ovsdb_monitor *dbmon;
209 struct jsonrpc_monitor_node *jm;
211 dbmon = xzalloc(sizeof *dbmon);
213 ovsdb_replica_init(&dbmon->replica, &ovsdb_jsonrpc_replica_class);
214 ovsdb_add_replica(db, &dbmon->replica);
215 list_init(&dbmon->jsonrpc_monitors);
217 shash_init(&dbmon->tables);
219 jm = xzalloc(sizeof *jm);
220 jm->jsonrpc_monitor = jsonrpc_monitor;
221 list_push_back(&dbmon->jsonrpc_monitors, &jm->node);
227 ovsdb_monitor_add_table(struct ovsdb_monitor *m,
228 const struct ovsdb_table *table)
230 struct ovsdb_monitor_table *mt;
232 mt = xzalloc(sizeof *mt);
234 hmap_init(&mt->changes);
235 shash_add(&m->tables, table->schema->name, mt);
239 ovsdb_monitor_add_column(struct ovsdb_monitor *dbmon,
240 const struct ovsdb_table *table,
241 const struct ovsdb_column *column,
242 enum ovsdb_monitor_selection select,
243 size_t *allocated_columns)
245 struct ovsdb_monitor_table *mt;
246 struct ovsdb_monitor_column *c;
248 mt = shash_find_data(&dbmon->tables, table->schema->name);
250 if (mt->n_columns >= *allocated_columns) {
251 mt->columns = x2nrealloc(mt->columns, allocated_columns,
252 sizeof *mt->columns);
255 mt->select |= select;
256 c = &mt->columns[mt->n_columns++];
261 /* Check for duplicated column names. Return the first
262 * duplicated column's name if found. Otherwise return
264 const char * OVS_WARN_UNUSED_RESULT
265 ovsdb_monitor_table_check_duplicates(struct ovsdb_monitor *m,
266 const struct ovsdb_table *table)
268 struct ovsdb_monitor_table *mt;
271 mt = shash_find_data(&m->tables, table->schema->name);
274 /* Check for duplicate columns. */
275 qsort(mt->columns, mt->n_columns, sizeof *mt->columns,
276 compare_ovsdb_monitor_column);
277 for (i = 1; i < mt->n_columns; i++) {
278 if (mt->columns[i].column == mt->columns[i - 1].column) {
279 return mt->columns[i].column->name;
287 /* Returns JSON for a <row-update> (as described in RFC 7047) for 'row' within
288 * 'mt', or NULL if no row update should be sent.
290 * The caller should specify 'initial' as true if the returned JSON is going to
291 * be used as part of the initial reply to a "monitor" request, false if it is
292 * going to be used as part of an "update" notification.
294 * 'changed' must be a scratch buffer for internal use that is at least
295 * bitmap_n_bytes(mt->n_columns) bytes long. */
297 ovsdb_monitor_compose_row_update(
298 const struct ovsdb_monitor_table *mt,
299 const struct ovsdb_monitor_row *row,
300 bool initial, unsigned long int *changed)
302 enum ovsdb_monitor_selection type;
303 struct json *old_json, *new_json;
304 struct json *row_json;
307 type = (initial ? OJMS_INITIAL
308 : !row->old ? OJMS_INSERT
309 : !row->new ? OJMS_DELETE
311 if (!(mt->select & type)) {
315 if (type == OJMS_MODIFY) {
319 memset(changed, 0, bitmap_n_bytes(mt->n_columns));
320 for (i = 0; i < mt->n_columns; i++) {
321 const struct ovsdb_column *c = mt->columns[i].column;
322 if (!ovsdb_datum_equals(&row->old[i], &row->new[i], &c->type)) {
323 bitmap_set1(changed, i);
328 /* No actual changes: presumably a row changed and then
329 * changed back later. */
334 row_json = json_object_create();
335 old_json = new_json = NULL;
336 if (type & (OJMS_DELETE | OJMS_MODIFY)) {
337 old_json = json_object_create();
338 json_object_put(row_json, "old", old_json);
340 if (type & (OJMS_INITIAL | OJMS_INSERT | OJMS_MODIFY)) {
341 new_json = json_object_create();
342 json_object_put(row_json, "new", new_json);
344 for (i = 0; i < mt->n_columns; i++) {
345 const struct ovsdb_monitor_column *c = &mt->columns[i];
347 if (!(type & c->select)) {
348 /* We don't care about this type of change for this
349 * particular column (but we will care about it for some
354 if ((type == OJMS_MODIFY && bitmap_is_set(changed, i))
355 || type == OJMS_DELETE) {
356 json_object_put(old_json, c->column->name,
357 ovsdb_datum_to_json(&row->old[i],
360 if (type & (OJMS_INITIAL | OJMS_INSERT | OJMS_MODIFY)) {
361 json_object_put(new_json, c->column->name,
362 ovsdb_datum_to_json(&row->new[i],
370 /* Constructs and returns JSON for a <table-updates> object (as described in
371 * RFC 7047) for all the outstanding changes within 'monitor', and deletes all
372 * the outstanding changes from 'monitor'. Returns NULL if no update needs to
375 * The caller should specify 'initial' as true if the returned JSON is going to
376 * be used as part of the initial reply to a "monitor" request, false if it is
377 * going to be used as part of an "update" notification. */
379 ovsdb_monitor_compose_table_update(
380 const struct ovsdb_monitor *dbmon, bool initial)
382 struct shash_node *node;
383 unsigned long int *changed;
388 SHASH_FOR_EACH (node, &dbmon->tables) {
389 struct ovsdb_monitor_table *mt = node->data;
391 max_columns = MAX(max_columns, mt->n_columns);
393 changed = xmalloc(bitmap_n_bytes(max_columns));
396 SHASH_FOR_EACH (node, &dbmon->tables) {
397 struct ovsdb_monitor_table *mt = node->data;
398 struct ovsdb_monitor_row *row, *next;
399 struct json *table_json = NULL;
401 HMAP_FOR_EACH_SAFE (row, next, hmap_node, &mt->changes) {
402 struct json *row_json;
404 row_json = ovsdb_monitor_compose_row_update(
405 mt, row, initial, changed);
407 char uuid[UUID_LEN + 1];
409 /* Create JSON object for transaction overall. */
411 json = json_object_create();
414 /* Create JSON object for transaction on this table. */
416 table_json = json_object_create();
417 json_object_put(json, mt->table->schema->name, table_json);
420 /* Add JSON row to JSON table. */
421 snprintf(uuid, sizeof uuid, UUID_FMT, UUID_ARGS(&row->uuid));
422 json_object_put(table_json, uuid, row_json);
425 hmap_remove(&mt->changes, &row->hmap_node);
426 ovsdb_monitor_row_destroy(mt, row);
435 ovsdb_monitor_needs_flush(struct ovsdb_monitor *dbmon)
437 struct shash_node *node;
439 SHASH_FOR_EACH (node, &dbmon->tables) {
440 struct ovsdb_monitor_table *mt = node->data;
442 if (!hmap_is_empty(&mt->changes)) {
450 ovsdb_monitor_table_add_select(struct ovsdb_monitor *dbmon,
451 const struct ovsdb_table *table,
452 enum ovsdb_monitor_selection select)
454 struct ovsdb_monitor_table * mt;
456 mt = shash_find_data(&dbmon->tables, table->schema->name);
457 mt->select |= select;
460 struct ovsdb_monitor_aux {
461 const struct ovsdb_monitor *monitor;
462 struct ovsdb_monitor_table *mt;
466 ovsdb_monitor_init_aux(struct ovsdb_monitor_aux *aux,
467 const struct ovsdb_monitor *m)
474 ovsdb_monitor_change_cb(const struct ovsdb_row *old,
475 const struct ovsdb_row *new,
476 const unsigned long int *changed OVS_UNUSED,
479 struct ovsdb_monitor_aux *aux = aux_;
480 const struct ovsdb_monitor *m = aux->monitor;
481 struct ovsdb_table *table = new ? new->table : old->table;
482 const struct uuid *uuid = ovsdb_row_get_uuid(new ? new : old);
483 struct ovsdb_monitor_row *change;
484 struct ovsdb_monitor_table *mt;
486 if (!aux->mt || table != aux->mt->table) {
487 aux->mt = shash_find_data(&m->tables, table->schema->name);
489 /* We don't care about rows in this table at all. Tell the caller
496 change = ovsdb_monitor_row_find(mt, uuid);
498 change = xmalloc(sizeof *change);
499 hmap_insert(&mt->changes, &change->hmap_node, uuid_hash(uuid));
500 change->uuid = *uuid;
501 change->old = clone_monitor_row_data(mt, old);
502 change->new = clone_monitor_row_data(mt, new);
505 update_monitor_row_data(mt, new, change->new);
507 free_monitor_row_data(mt, change->new);
511 /* This row was added then deleted. Forget about it. */
512 hmap_remove(&mt->changes, &change->hmap_node);
521 ovsdb_monitor_get_initial(const struct ovsdb_monitor *dbmon)
523 struct ovsdb_monitor_aux aux;
524 struct shash_node *node;
526 ovsdb_monitor_init_aux(&aux, dbmon);
527 SHASH_FOR_EACH (node, &dbmon->tables) {
528 struct ovsdb_monitor_table *mt = node->data;
530 if (mt->select & OJMS_INITIAL) {
531 struct ovsdb_row *row;
533 HMAP_FOR_EACH (row, hmap_node, &mt->table->rows) {
534 ovsdb_monitor_change_cb(NULL, row, NULL, &aux);
541 ovsdb_monitor_remove_jsonrpc_monitor(struct ovsdb_monitor *dbmon,
542 struct ovsdb_jsonrpc_monitor *jsonrpc_monitor)
544 struct jsonrpc_monitor_node *jm;
546 /* Find and remove the jsonrpc monitor from the list. */
547 LIST_FOR_EACH(jm, node, &dbmon->jsonrpc_monitors) {
548 if (jm->jsonrpc_monitor == jsonrpc_monitor) {
549 list_remove(&jm->node);
552 /* Destroy ovsdb monitor if this is the last user. */
553 if (list_is_empty(&dbmon->jsonrpc_monitors)) {
554 ovsdb_monitor_destroy(dbmon);
561 /* Should never reach here. jsonrpc_monitor should be on the list. */
566 ovsdb_monitor_destroy(struct ovsdb_monitor *dbmon)
568 struct shash_node *node;
570 list_remove(&dbmon->replica.node);
572 SHASH_FOR_EACH (node, &dbmon->tables) {
573 struct ovsdb_monitor_table *mt = node->data;
574 struct ovsdb_monitor_row *row, *next;
576 HMAP_FOR_EACH_SAFE (row, next, hmap_node, &mt->changes) {
577 hmap_remove(&mt->changes, &row->hmap_node);
578 ovsdb_monitor_row_destroy(mt, row);
580 hmap_destroy(&mt->changes);
585 shash_destroy(&dbmon->tables);
589 static struct ovsdb_error *
590 ovsdb_monitor_commit(struct ovsdb_replica *replica,
591 const struct ovsdb_txn *txn,
592 bool durable OVS_UNUSED)
594 struct ovsdb_monitor *m = ovsdb_monitor_cast(replica);
595 struct ovsdb_monitor_aux aux;
597 ovsdb_monitor_init_aux(&aux, m);
598 ovsdb_txn_for_each_change(txn, ovsdb_monitor_change_cb, &aux);
604 ovsdb_monitor_destroy_callback(struct ovsdb_replica *replica)
606 struct ovsdb_monitor *dbmon = ovsdb_monitor_cast(replica);
607 struct jsonrpc_monitor_node *jm, *next;
609 /* Delete all front end monitors. Removing the last front
610 * end monitor will also destroy the corresponding 'ovsdb_monitor'.
611 * ovsdb monitor will also be destroied. */
612 LIST_FOR_EACH_SAFE(jm, next, node, &dbmon->jsonrpc_monitors) {
613 ovsdb_jsonrpc_monitor_destroy(jm->jsonrpc_monitor);
617 static const struct ovsdb_replica_class ovsdb_jsonrpc_replica_class = {
618 ovsdb_monitor_commit,
619 ovsdb_monitor_destroy_callback,