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. */
52 uint64_t n_transactions; /* Count number of committed transactions. */
55 struct jsonrpc_monitor_node {
56 struct ovsdb_jsonrpc_monitor *jsonrpc_monitor;
60 /* A particular column being monitored. */
61 struct ovsdb_monitor_column {
62 const struct ovsdb_column *column;
63 enum ovsdb_monitor_selection select;
66 /* A row that has changed in a monitored table. */
67 struct ovsdb_monitor_row {
68 struct hmap_node hmap_node; /* In ovsdb_jsonrpc_monitor_table.changes. */
69 struct uuid uuid; /* UUID of row that changed. */
70 struct ovsdb_datum *old; /* Old data, NULL for an inserted row. */
71 struct ovsdb_datum *new; /* New data, NULL for a deleted row. */
74 /* A particular table being monitored. */
75 struct ovsdb_monitor_table {
76 const struct ovsdb_table *table;
78 /* This is the union (bitwise-OR) of the 'select' values in all of the
79 * members of 'columns' below. */
80 enum ovsdb_monitor_selection select;
82 /* Columns being monitored. */
83 struct ovsdb_monitor_column *columns;
86 /* Contains 'struct ovsdb_monitor_row's for rows that have been
87 * updated but not yet flushed to the jsonrpc connection. */
91 static void ovsdb_monitor_destroy(struct ovsdb_monitor *dbmon);
94 compare_ovsdb_monitor_column(const void *a_, const void *b_)
96 const struct ovsdb_monitor_column *a = a_;
97 const struct ovsdb_monitor_column *b = b_;
99 return a->column < b->column ? -1 : a->column > b->column;
102 static struct ovsdb_monitor *
103 ovsdb_monitor_cast(struct ovsdb_replica *replica)
105 ovs_assert(replica->class == &ovsdb_jsonrpc_replica_class);
106 return CONTAINER_OF(replica, struct ovsdb_monitor, replica);
109 /* Finds and returns the ovsdb_monitor_row in 'mt->changes' for the
110 * given 'uuid', or NULL if there is no such row. */
111 static struct ovsdb_monitor_row *
112 ovsdb_monitor_row_find(const struct ovsdb_monitor_table *mt,
113 const struct uuid *uuid)
115 struct ovsdb_monitor_row *row;
117 HMAP_FOR_EACH_WITH_HASH (row, hmap_node, uuid_hash(uuid), &mt->changes) {
118 if (uuid_equals(uuid, &row->uuid)) {
125 /* Allocates an array of 'mt->n_columns' ovsdb_datums and initializes them as
126 * copies of the data in 'row' drawn from the columns represented by
127 * mt->columns[]. Returns the array.
129 * If 'row' is NULL, returns NULL. */
130 static struct ovsdb_datum *
131 clone_monitor_row_data(const struct ovsdb_monitor_table *mt,
132 const struct ovsdb_row *row)
134 struct ovsdb_datum *data;
141 data = xmalloc(mt->n_columns * sizeof *data);
142 for (i = 0; i < mt->n_columns; i++) {
143 const struct ovsdb_column *c = mt->columns[i].column;
144 const struct ovsdb_datum *src = &row->fields[c->index];
145 struct ovsdb_datum *dst = &data[i];
146 const struct ovsdb_type *type = &c->type;
148 ovsdb_datum_clone(dst, src, type);
153 /* Replaces the mt->n_columns ovsdb_datums in row[] by copies of the data from
154 * in 'row' drawn from the columns represented by mt->columns[]. */
156 update_monitor_row_data(const struct ovsdb_monitor_table *mt,
157 const struct ovsdb_row *row,
158 struct ovsdb_datum *data)
162 for (i = 0; i < mt->n_columns; i++) {
163 const struct ovsdb_column *c = mt->columns[i].column;
164 const struct ovsdb_datum *src = &row->fields[c->index];
165 struct ovsdb_datum *dst = &data[i];
166 const struct ovsdb_type *type = &c->type;
168 if (!ovsdb_datum_equals(src, dst, type)) {
169 ovsdb_datum_destroy(dst, type);
170 ovsdb_datum_clone(dst, src, type);
175 /* Frees all of the mt->n_columns ovsdb_datums in data[], using the types taken
176 * from mt->columns[], plus 'data' itself. */
178 free_monitor_row_data(const struct ovsdb_monitor_table *mt,
179 struct ovsdb_datum *data)
184 for (i = 0; i < mt->n_columns; i++) {
185 const struct ovsdb_column *c = mt->columns[i].column;
187 ovsdb_datum_destroy(&data[i], &c->type);
193 /* Frees 'row', which must have been created from 'mt'. */
195 ovsdb_monitor_row_destroy(const struct ovsdb_monitor_table *mt,
196 struct ovsdb_monitor_row *row)
199 free_monitor_row_data(mt, row->old);
200 free_monitor_row_data(mt, row->new);
205 struct ovsdb_monitor *
206 ovsdb_monitor_create(struct ovsdb *db,
207 struct ovsdb_jsonrpc_monitor *jsonrpc_monitor)
209 struct ovsdb_monitor *dbmon;
210 struct jsonrpc_monitor_node *jm;
212 dbmon = xzalloc(sizeof *dbmon);
214 ovsdb_replica_init(&dbmon->replica, &ovsdb_jsonrpc_replica_class);
215 ovsdb_add_replica(db, &dbmon->replica);
216 list_init(&dbmon->jsonrpc_monitors);
218 dbmon->n_transactions = 0;
219 shash_init(&dbmon->tables);
221 jm = xzalloc(sizeof *jm);
222 jm->jsonrpc_monitor = jsonrpc_monitor;
223 list_push_back(&dbmon->jsonrpc_monitors, &jm->node);
229 ovsdb_monitor_add_table(struct ovsdb_monitor *m,
230 const struct ovsdb_table *table)
232 struct ovsdb_monitor_table *mt;
234 mt = xzalloc(sizeof *mt);
236 hmap_init(&mt->changes);
237 shash_add(&m->tables, table->schema->name, mt);
241 ovsdb_monitor_add_column(struct ovsdb_monitor *dbmon,
242 const struct ovsdb_table *table,
243 const struct ovsdb_column *column,
244 enum ovsdb_monitor_selection select,
245 size_t *allocated_columns)
247 struct ovsdb_monitor_table *mt;
248 struct ovsdb_monitor_column *c;
250 mt = shash_find_data(&dbmon->tables, table->schema->name);
252 if (mt->n_columns >= *allocated_columns) {
253 mt->columns = x2nrealloc(mt->columns, allocated_columns,
254 sizeof *mt->columns);
257 mt->select |= select;
258 c = &mt->columns[mt->n_columns++];
263 /* Check for duplicated column names. Return the first
264 * duplicated column's name if found. Otherwise return
266 const char * OVS_WARN_UNUSED_RESULT
267 ovsdb_monitor_table_check_duplicates(struct ovsdb_monitor *m,
268 const struct ovsdb_table *table)
270 struct ovsdb_monitor_table *mt;
273 mt = shash_find_data(&m->tables, table->schema->name);
276 /* Check for duplicate columns. */
277 qsort(mt->columns, mt->n_columns, sizeof *mt->columns,
278 compare_ovsdb_monitor_column);
279 for (i = 1; i < mt->n_columns; i++) {
280 if (mt->columns[i].column == mt->columns[i - 1].column) {
281 return mt->columns[i].column->name;
289 /* Returns JSON for a <row-update> (as described in RFC 7047) for 'row' within
290 * 'mt', or NULL if no row update should be sent.
292 * The caller should specify 'initial' as true if the returned JSON is going to
293 * be used as part of the initial reply to a "monitor" request, false if it is
294 * going to be used as part of an "update" notification.
296 * 'changed' must be a scratch buffer for internal use that is at least
297 * bitmap_n_bytes(mt->n_columns) bytes long. */
299 ovsdb_monitor_compose_row_update(
300 const struct ovsdb_monitor_table *mt,
301 const struct ovsdb_monitor_row *row,
302 bool initial, unsigned long int *changed)
304 enum ovsdb_monitor_selection type;
305 struct json *old_json, *new_json;
306 struct json *row_json;
309 type = (initial ? OJMS_INITIAL
310 : !row->old ? OJMS_INSERT
311 : !row->new ? OJMS_DELETE
313 if (!(mt->select & type)) {
317 if (type == OJMS_MODIFY) {
321 memset(changed, 0, bitmap_n_bytes(mt->n_columns));
322 for (i = 0; i < mt->n_columns; i++) {
323 const struct ovsdb_column *c = mt->columns[i].column;
324 if (!ovsdb_datum_equals(&row->old[i], &row->new[i], &c->type)) {
325 bitmap_set1(changed, i);
330 /* No actual changes: presumably a row changed and then
331 * changed back later. */
336 row_json = json_object_create();
337 old_json = new_json = NULL;
338 if (type & (OJMS_DELETE | OJMS_MODIFY)) {
339 old_json = json_object_create();
340 json_object_put(row_json, "old", old_json);
342 if (type & (OJMS_INITIAL | OJMS_INSERT | OJMS_MODIFY)) {
343 new_json = json_object_create();
344 json_object_put(row_json, "new", new_json);
346 for (i = 0; i < mt->n_columns; i++) {
347 const struct ovsdb_monitor_column *c = &mt->columns[i];
349 if (!(type & c->select)) {
350 /* We don't care about this type of change for this
351 * particular column (but we will care about it for some
356 if ((type == OJMS_MODIFY && bitmap_is_set(changed, i))
357 || type == OJMS_DELETE) {
358 json_object_put(old_json, c->column->name,
359 ovsdb_datum_to_json(&row->old[i],
362 if (type & (OJMS_INITIAL | OJMS_INSERT | OJMS_MODIFY)) {
363 json_object_put(new_json, c->column->name,
364 ovsdb_datum_to_json(&row->new[i],
372 /* Constructs and returns JSON for a <table-updates> object (as described in
373 * RFC 7047) for all the outstanding changes within 'monitor', and deletes all
374 * the outstanding changes from 'monitor'. Returns NULL if no update needs to
377 * The caller should specify 'initial' as true if the returned JSON is going to
378 * be used as part of the initial reply to a "monitor" request, false if it is
379 * going to be used as part of an "update" notification. */
381 ovsdb_monitor_compose_table_update(const struct ovsdb_monitor *dbmon,
382 bool initial, uint64_t *unflushed)
384 struct shash_node *node;
385 unsigned long int *changed;
389 *unflushed = dbmon->n_transactions + 1;
392 SHASH_FOR_EACH (node, &dbmon->tables) {
393 struct ovsdb_monitor_table *mt = node->data;
395 max_columns = MAX(max_columns, mt->n_columns);
397 changed = xmalloc(bitmap_n_bytes(max_columns));
400 SHASH_FOR_EACH (node, &dbmon->tables) {
401 struct ovsdb_monitor_table *mt = node->data;
402 struct ovsdb_monitor_row *row, *next;
403 struct json *table_json = NULL;
405 HMAP_FOR_EACH_SAFE (row, next, hmap_node, &mt->changes) {
406 struct json *row_json;
408 row_json = ovsdb_monitor_compose_row_update(
409 mt, row, initial, changed);
411 char uuid[UUID_LEN + 1];
413 /* Create JSON object for transaction overall. */
415 json = json_object_create();
418 /* Create JSON object for transaction on this table. */
420 table_json = json_object_create();
421 json_object_put(json, mt->table->schema->name, table_json);
424 /* Add JSON row to JSON table. */
425 snprintf(uuid, sizeof uuid, UUID_FMT, UUID_ARGS(&row->uuid));
426 json_object_put(table_json, uuid, row_json);
429 hmap_remove(&mt->changes, &row->hmap_node);
430 ovsdb_monitor_row_destroy(mt, row);
439 ovsdb_monitor_needs_flush(struct ovsdb_monitor *dbmon,
440 uint64_t next_transaction)
442 ovs_assert(next_transaction <= dbmon->n_transactions + 1);
443 return (next_transaction <= dbmon->n_transactions);
447 ovsdb_monitor_table_add_select(struct ovsdb_monitor *dbmon,
448 const struct ovsdb_table *table,
449 enum ovsdb_monitor_selection select)
451 struct ovsdb_monitor_table * mt;
453 mt = shash_find_data(&dbmon->tables, table->schema->name);
454 mt->select |= select;
457 struct ovsdb_monitor_aux {
458 const struct ovsdb_monitor *monitor;
459 struct ovsdb_monitor_table *mt;
463 ovsdb_monitor_init_aux(struct ovsdb_monitor_aux *aux,
464 const struct ovsdb_monitor *m)
471 ovsdb_monitor_change_cb(const struct ovsdb_row *old,
472 const struct ovsdb_row *new,
473 const unsigned long int *changed OVS_UNUSED,
476 struct ovsdb_monitor_aux *aux = aux_;
477 const struct ovsdb_monitor *m = aux->monitor;
478 struct ovsdb_table *table = new ? new->table : old->table;
479 const struct uuid *uuid = ovsdb_row_get_uuid(new ? new : old);
480 struct ovsdb_monitor_row *change;
481 struct ovsdb_monitor_table *mt;
483 if (!aux->mt || table != aux->mt->table) {
484 aux->mt = shash_find_data(&m->tables, table->schema->name);
486 /* We don't care about rows in this table at all. Tell the caller
493 change = ovsdb_monitor_row_find(mt, uuid);
495 change = xmalloc(sizeof *change);
496 hmap_insert(&mt->changes, &change->hmap_node, uuid_hash(uuid));
497 change->uuid = *uuid;
498 change->old = clone_monitor_row_data(mt, old);
499 change->new = clone_monitor_row_data(mt, new);
502 update_monitor_row_data(mt, new, change->new);
504 free_monitor_row_data(mt, change->new);
508 /* This row was added then deleted. Forget about it. */
509 hmap_remove(&mt->changes, &change->hmap_node);
518 ovsdb_monitor_get_initial(const struct ovsdb_monitor *dbmon)
520 struct ovsdb_monitor_aux aux;
521 struct shash_node *node;
523 ovsdb_monitor_init_aux(&aux, dbmon);
524 SHASH_FOR_EACH (node, &dbmon->tables) {
525 struct ovsdb_monitor_table *mt = node->data;
527 if (mt->select & OJMS_INITIAL) {
528 struct ovsdb_row *row;
530 HMAP_FOR_EACH (row, hmap_node, &mt->table->rows) {
531 ovsdb_monitor_change_cb(NULL, row, NULL, &aux);
538 ovsdb_monitor_remove_jsonrpc_monitor(struct ovsdb_monitor *dbmon,
539 struct ovsdb_jsonrpc_monitor *jsonrpc_monitor)
541 struct jsonrpc_monitor_node *jm;
543 /* Find and remove the jsonrpc monitor from the list. */
544 LIST_FOR_EACH(jm, node, &dbmon->jsonrpc_monitors) {
545 if (jm->jsonrpc_monitor == jsonrpc_monitor) {
546 list_remove(&jm->node);
549 /* Destroy ovsdb monitor if this is the last user. */
550 if (list_is_empty(&dbmon->jsonrpc_monitors)) {
551 ovsdb_monitor_destroy(dbmon);
558 /* Should never reach here. jsonrpc_monitor should be on the list. */
563 ovsdb_monitor_destroy(struct ovsdb_monitor *dbmon)
565 struct shash_node *node;
567 list_remove(&dbmon->replica.node);
569 SHASH_FOR_EACH (node, &dbmon->tables) {
570 struct ovsdb_monitor_table *mt = node->data;
571 struct ovsdb_monitor_row *row, *next;
573 HMAP_FOR_EACH_SAFE (row, next, hmap_node, &mt->changes) {
574 hmap_remove(&mt->changes, &row->hmap_node);
575 ovsdb_monitor_row_destroy(mt, row);
577 hmap_destroy(&mt->changes);
582 shash_destroy(&dbmon->tables);
586 static struct ovsdb_error *
587 ovsdb_monitor_commit(struct ovsdb_replica *replica,
588 const struct ovsdb_txn *txn,
589 bool durable OVS_UNUSED)
591 struct ovsdb_monitor *m = ovsdb_monitor_cast(replica);
592 struct ovsdb_monitor_aux aux;
594 ovsdb_monitor_init_aux(&aux, m);
595 ovsdb_txn_for_each_change(txn, ovsdb_monitor_change_cb, &aux);
602 ovsdb_monitor_destroy_callback(struct ovsdb_replica *replica)
604 struct ovsdb_monitor *dbmon = ovsdb_monitor_cast(replica);
605 struct jsonrpc_monitor_node *jm, *next;
607 /* Delete all front end monitors. Removing the last front
608 * end monitor will also destroy the corresponding 'ovsdb_monitor'.
609 * ovsdb monitor will also be destroied. */
610 LIST_FOR_EACH_SAFE(jm, next, node, &dbmon->jsonrpc_monitors) {
611 ovsdb_jsonrpc_monitor_destroy(jm->jsonrpc_monitor);
615 static const struct ovsdb_replica_class ovsdb_jsonrpc_replica_class = {
616 ovsdb_monitor_commit,
617 ovsdb_monitor_destroy_callback,