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 ovsdb_jsonrpc_monitor *jsonrpc_monitor;
54 /* A particular column being monitored. */
55 struct ovsdb_monitor_column {
56 const struct ovsdb_column *column;
57 enum ovsdb_monitor_selection select;
60 /* A row that has changed in a monitored table. */
61 struct ovsdb_monitor_row {
62 struct hmap_node hmap_node; /* In ovsdb_jsonrpc_monitor_table.changes. */
63 struct uuid uuid; /* UUID of row that changed. */
64 struct ovsdb_datum *old; /* Old data, NULL for an inserted row. */
65 struct ovsdb_datum *new; /* New data, NULL for a deleted row. */
68 /* A particular table being monitored. */
69 struct ovsdb_monitor_table {
70 const struct ovsdb_table *table;
72 /* This is the union (bitwise-OR) of the 'select' values in all of the
73 * members of 'columns' below. */
74 enum ovsdb_monitor_selection select;
76 /* Columns being monitored. */
77 struct ovsdb_monitor_column *columns;
80 /* Contains 'struct ovsdb_monitor_row's for rows that have been
81 * updated but not yet flushed to the jsonrpc connection. */
86 compare_ovsdb_monitor_column(const void *a_, const void *b_)
88 const struct ovsdb_monitor_column *a = a_;
89 const struct ovsdb_monitor_column *b = b_;
91 return a->column < b->column ? -1 : a->column > b->column;
94 static struct ovsdb_monitor *
95 ovsdb_monitor_cast(struct ovsdb_replica *replica)
97 ovs_assert(replica->class == &ovsdb_jsonrpc_replica_class);
98 return CONTAINER_OF(replica, struct ovsdb_monitor, replica);
101 /* Finds and returns the ovsdb_monitor_row in 'mt->changes' for the
102 * given 'uuid', or NULL if there is no such row. */
103 static struct ovsdb_monitor_row *
104 ovsdb_monitor_row_find(const struct ovsdb_monitor_table *mt,
105 const struct uuid *uuid)
107 struct ovsdb_monitor_row *row;
109 HMAP_FOR_EACH_WITH_HASH (row, hmap_node, uuid_hash(uuid), &mt->changes) {
110 if (uuid_equals(uuid, &row->uuid)) {
117 /* Allocates an array of 'mt->n_columns' ovsdb_datums and initializes them as
118 * copies of the data in 'row' drawn from the columns represented by
119 * mt->columns[]. Returns the array.
121 * If 'row' is NULL, returns NULL. */
122 static struct ovsdb_datum *
123 clone_monitor_row_data(const struct ovsdb_monitor_table *mt,
124 const struct ovsdb_row *row)
126 struct ovsdb_datum *data;
133 data = xmalloc(mt->n_columns * sizeof *data);
134 for (i = 0; i < mt->n_columns; i++) {
135 const struct ovsdb_column *c = mt->columns[i].column;
136 const struct ovsdb_datum *src = &row->fields[c->index];
137 struct ovsdb_datum *dst = &data[i];
138 const struct ovsdb_type *type = &c->type;
140 ovsdb_datum_clone(dst, src, type);
145 /* Replaces the mt->n_columns ovsdb_datums in row[] by copies of the data from
146 * in 'row' drawn from the columns represented by mt->columns[]. */
148 update_monitor_row_data(const struct ovsdb_monitor_table *mt,
149 const struct ovsdb_row *row,
150 struct ovsdb_datum *data)
154 for (i = 0; i < mt->n_columns; i++) {
155 const struct ovsdb_column *c = mt->columns[i].column;
156 const struct ovsdb_datum *src = &row->fields[c->index];
157 struct ovsdb_datum *dst = &data[i];
158 const struct ovsdb_type *type = &c->type;
160 if (!ovsdb_datum_equals(src, dst, type)) {
161 ovsdb_datum_destroy(dst, type);
162 ovsdb_datum_clone(dst, src, type);
167 /* Frees all of the mt->n_columns ovsdb_datums in data[], using the types taken
168 * from mt->columns[], plus 'data' itself. */
170 free_monitor_row_data(const struct ovsdb_monitor_table *mt,
171 struct ovsdb_datum *data)
176 for (i = 0; i < mt->n_columns; i++) {
177 const struct ovsdb_column *c = mt->columns[i].column;
179 ovsdb_datum_destroy(&data[i], &c->type);
185 /* Frees 'row', which must have been created from 'mt'. */
187 ovsdb_monitor_row_destroy(const struct ovsdb_monitor_table *mt,
188 struct ovsdb_monitor_row *row)
191 free_monitor_row_data(mt, row->old);
192 free_monitor_row_data(mt, row->new);
197 struct ovsdb_monitor *
198 ovsdb_monitor_create(struct ovsdb *db,
199 struct ovsdb_jsonrpc_monitor *jsonrpc_monitor)
201 struct ovsdb_monitor *dbmon;
203 dbmon = xzalloc(sizeof *dbmon);
205 ovsdb_replica_init(&dbmon->replica, &ovsdb_jsonrpc_replica_class);
206 ovsdb_add_replica(db, &dbmon->replica);
207 dbmon->jsonrpc_monitor = jsonrpc_monitor;
209 shash_init(&dbmon->tables);
215 ovsdb_monitor_add_table(struct ovsdb_monitor *m,
216 const struct ovsdb_table *table)
218 struct ovsdb_monitor_table *mt;
220 mt = xzalloc(sizeof *mt);
222 hmap_init(&mt->changes);
223 shash_add(&m->tables, table->schema->name, mt);
227 ovsdb_monitor_add_column(struct ovsdb_monitor *dbmon,
228 const struct ovsdb_table *table,
229 const struct ovsdb_column *column,
230 enum ovsdb_monitor_selection select,
231 size_t *allocated_columns)
233 struct ovsdb_monitor_table *mt;
234 struct ovsdb_monitor_column *c;
236 mt = shash_find_data(&dbmon->tables, table->schema->name);
238 if (mt->n_columns >= *allocated_columns) {
239 mt->columns = x2nrealloc(mt->columns, allocated_columns,
240 sizeof *mt->columns);
243 mt->select |= select;
244 c = &mt->columns[mt->n_columns++];
249 /* Check for duplicated column names. Return the first
250 * duplicated column's name if found. Otherwise return
252 const char * OVS_WARN_UNUSED_RESULT
253 ovsdb_monitor_table_check_duplicates(struct ovsdb_monitor *m,
254 const struct ovsdb_table *table)
256 struct ovsdb_monitor_table *mt;
259 mt = shash_find_data(&m->tables, table->schema->name);
262 /* Check for duplicate columns. */
263 qsort(mt->columns, mt->n_columns, sizeof *mt->columns,
264 compare_ovsdb_monitor_column);
265 for (i = 1; i < mt->n_columns; i++) {
266 if (mt->columns[i].column == mt->columns[i - 1].column) {
267 return mt->columns[i].column->name;
275 /* Returns JSON for a <row-update> (as described in RFC 7047) for 'row' within
276 * 'mt', or NULL if no row update should be sent.
278 * The caller should specify 'initial' as true if the returned JSON is going to
279 * be used as part of the initial reply to a "monitor" request, false if it is
280 * going to be used as part of an "update" notification.
282 * 'changed' must be a scratch buffer for internal use that is at least
283 * bitmap_n_bytes(mt->n_columns) bytes long. */
285 ovsdb_monitor_compose_row_update(
286 const struct ovsdb_monitor_table *mt,
287 const struct ovsdb_monitor_row *row,
288 bool initial, unsigned long int *changed)
290 enum ovsdb_monitor_selection type;
291 struct json *old_json, *new_json;
292 struct json *row_json;
295 type = (initial ? OJMS_INITIAL
296 : !row->old ? OJMS_INSERT
297 : !row->new ? OJMS_DELETE
299 if (!(mt->select & type)) {
303 if (type == OJMS_MODIFY) {
307 memset(changed, 0, bitmap_n_bytes(mt->n_columns));
308 for (i = 0; i < mt->n_columns; i++) {
309 const struct ovsdb_column *c = mt->columns[i].column;
310 if (!ovsdb_datum_equals(&row->old[i], &row->new[i], &c->type)) {
311 bitmap_set1(changed, i);
316 /* No actual changes: presumably a row changed and then
317 * changed back later. */
322 row_json = json_object_create();
323 old_json = new_json = NULL;
324 if (type & (OJMS_DELETE | OJMS_MODIFY)) {
325 old_json = json_object_create();
326 json_object_put(row_json, "old", old_json);
328 if (type & (OJMS_INITIAL | OJMS_INSERT | OJMS_MODIFY)) {
329 new_json = json_object_create();
330 json_object_put(row_json, "new", new_json);
332 for (i = 0; i < mt->n_columns; i++) {
333 const struct ovsdb_monitor_column *c = &mt->columns[i];
335 if (!(type & c->select)) {
336 /* We don't care about this type of change for this
337 * particular column (but we will care about it for some
342 if ((type == OJMS_MODIFY && bitmap_is_set(changed, i))
343 || type == OJMS_DELETE) {
344 json_object_put(old_json, c->column->name,
345 ovsdb_datum_to_json(&row->old[i],
348 if (type & (OJMS_INITIAL | OJMS_INSERT | OJMS_MODIFY)) {
349 json_object_put(new_json, c->column->name,
350 ovsdb_datum_to_json(&row->new[i],
358 /* Constructs and returns JSON for a <table-updates> object (as described in
359 * RFC 7047) for all the outstanding changes within 'monitor', and deletes all
360 * the outstanding changes from 'monitor'. Returns NULL if no update needs to
363 * The caller should specify 'initial' as true if the returned JSON is going to
364 * be used as part of the initial reply to a "monitor" request, false if it is
365 * going to be used as part of an "update" notification. */
367 ovsdb_monitor_compose_table_update(
368 const struct ovsdb_monitor *dbmon, bool initial)
370 struct shash_node *node;
371 unsigned long int *changed;
376 SHASH_FOR_EACH (node, &dbmon->tables) {
377 struct ovsdb_monitor_table *mt = node->data;
379 max_columns = MAX(max_columns, mt->n_columns);
381 changed = xmalloc(bitmap_n_bytes(max_columns));
384 SHASH_FOR_EACH (node, &dbmon->tables) {
385 struct ovsdb_monitor_table *mt = node->data;
386 struct ovsdb_monitor_row *row, *next;
387 struct json *table_json = NULL;
389 HMAP_FOR_EACH_SAFE (row, next, hmap_node, &mt->changes) {
390 struct json *row_json;
392 row_json = ovsdb_monitor_compose_row_update(
393 mt, row, initial, changed);
395 char uuid[UUID_LEN + 1];
397 /* Create JSON object for transaction overall. */
399 json = json_object_create();
402 /* Create JSON object for transaction on this table. */
404 table_json = json_object_create();
405 json_object_put(json, mt->table->schema->name, table_json);
408 /* Add JSON row to JSON table. */
409 snprintf(uuid, sizeof uuid, UUID_FMT, UUID_ARGS(&row->uuid));
410 json_object_put(table_json, uuid, row_json);
413 hmap_remove(&mt->changes, &row->hmap_node);
414 ovsdb_monitor_row_destroy(mt, row);
423 ovsdb_monitor_needs_flush(struct ovsdb_monitor *dbmon)
425 struct shash_node *node;
427 SHASH_FOR_EACH (node, &dbmon->tables) {
428 struct ovsdb_monitor_table *mt = node->data;
430 if (!hmap_is_empty(&mt->changes)) {
438 ovsdb_monitor_table_add_select(struct ovsdb_monitor *dbmon,
439 const struct ovsdb_table *table,
440 enum ovsdb_monitor_selection select)
442 struct ovsdb_monitor_table * mt;
444 mt = shash_find_data(&dbmon->tables, table->schema->name);
445 mt->select |= select;
448 struct ovsdb_monitor_aux {
449 const struct ovsdb_monitor *monitor;
450 struct ovsdb_monitor_table *mt;
454 ovsdb_monitor_init_aux(struct ovsdb_monitor_aux *aux,
455 const struct ovsdb_monitor *m)
462 ovsdb_monitor_change_cb(const struct ovsdb_row *old,
463 const struct ovsdb_row *new,
464 const unsigned long int *changed OVS_UNUSED,
467 struct ovsdb_monitor_aux *aux = aux_;
468 const struct ovsdb_monitor *m = aux->monitor;
469 struct ovsdb_table *table = new ? new->table : old->table;
470 const struct uuid *uuid = ovsdb_row_get_uuid(new ? new : old);
471 struct ovsdb_monitor_row *change;
472 struct ovsdb_monitor_table *mt;
474 if (!aux->mt || table != aux->mt->table) {
475 aux->mt = shash_find_data(&m->tables, table->schema->name);
477 /* We don't care about rows in this table at all. Tell the caller
484 change = ovsdb_monitor_row_find(mt, uuid);
486 change = xmalloc(sizeof *change);
487 hmap_insert(&mt->changes, &change->hmap_node, uuid_hash(uuid));
488 change->uuid = *uuid;
489 change->old = clone_monitor_row_data(mt, old);
490 change->new = clone_monitor_row_data(mt, new);
493 update_monitor_row_data(mt, new, change->new);
495 free_monitor_row_data(mt, change->new);
499 /* This row was added then deleted. Forget about it. */
500 hmap_remove(&mt->changes, &change->hmap_node);
509 ovsdb_monitor_get_initial(const struct ovsdb_monitor *dbmon)
511 struct ovsdb_monitor_aux aux;
512 struct shash_node *node;
515 ovsdb_monitor_init_aux(&aux, dbmon);
516 SHASH_FOR_EACH (node, &dbmon->tables) {
517 struct ovsdb_monitor_table *mt = node->data;
519 if (mt->select & OJMS_INITIAL) {
520 struct ovsdb_row *row;
522 HMAP_FOR_EACH (row, hmap_node, &mt->table->rows) {
523 ovsdb_monitor_change_cb(NULL, row, NULL, &aux);
527 json = ovsdb_monitor_compose_table_update(dbmon, true);
528 return json ? json : json_object_create();
532 ovsdb_monitor_destroy(struct ovsdb_monitor *dbmon)
534 struct shash_node *node;
536 list_remove(&dbmon->replica.node);
538 SHASH_FOR_EACH (node, &dbmon->tables) {
539 struct ovsdb_monitor_table *mt = node->data;
540 struct ovsdb_monitor_row *row, *next;
542 HMAP_FOR_EACH_SAFE (row, next, hmap_node, &mt->changes) {
543 hmap_remove(&mt->changes, &row->hmap_node);
544 ovsdb_monitor_row_destroy(mt, row);
546 hmap_destroy(&mt->changes);
551 shash_destroy(&dbmon->tables);
555 static struct ovsdb_error *
556 ovsdb_monitor_commit(struct ovsdb_replica *replica,
557 const struct ovsdb_txn *txn,
558 bool durable OVS_UNUSED)
560 struct ovsdb_monitor *m = ovsdb_monitor_cast(replica);
561 struct ovsdb_monitor_aux aux;
563 ovsdb_monitor_init_aux(&aux, m);
564 ovsdb_txn_for_each_change(txn, ovsdb_monitor_change_cb, &aux);
570 ovsdb_monitor_destroy_callback(struct ovsdb_replica *replica)
572 struct ovsdb_monitor *dbmon = ovsdb_monitor_cast(replica);
573 struct ovsdb_jsonrpc_monitor *m = dbmon->jsonrpc_monitor;
575 ovsdb_jsonrpc_monitor_destroy(m);
578 static const struct ovsdb_replica_class ovsdb_jsonrpc_replica_class = {
579 ovsdb_monitor_commit,
580 ovsdb_monitor_destroy_callback,