summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndy Zhou <azhou@nicira.com>2015-03-11 21:20:44 -0700
committerAndy Zhou <azhou@nicira.com>2015-05-29 17:39:50 -0700
commit2fa1df7b88611f8858882147b5db122c5644799c (patch)
treef42dce9dc3ffd14e863b7f3e11045f7b82f012b4
parent88b633082c0de646bebf1966bd06299692e4f9ba (diff)
downloadopenvswitch-2fa1df7b88611f8858882147b5db122c5644799c.tar.gz
jsonrpc-server: Split out monitor backend functions to monitor.c/h
Added new files monitor.[ch] for monitor backend functions. There is no functional changes. Signed-off-by: Andy Zhou <azhou@nicira.com> Acked-by: Ben Pfaff <blp@nicira.com>
-rw-r--r--ovsdb/automake.mk2
-rw-r--r--ovsdb/jsonrpc-server.c593
-rw-r--r--ovsdb/jsonrpc-server.h3
-rw-r--r--ovsdb/monitor.c581
-rw-r--r--ovsdb/monitor.h58
5 files changed, 661 insertions, 576 deletions
diff --git a/ovsdb/automake.mk b/ovsdb/automake.mk
index a66974a57..cc3e656dc 100644
--- a/ovsdb/automake.mk
+++ b/ovsdb/automake.mk
@@ -20,6 +20,8 @@ ovsdb_libovsdb_la_SOURCES = \
ovsdb/mutation.h \
ovsdb/ovsdb.c \
ovsdb/ovsdb.h \
+ ovsdb/monitor.c \
+ ovsdb/monitor.h \
ovsdb/query.c \
ovsdb/query.h \
ovsdb/row.c \
diff --git a/ovsdb/jsonrpc-server.c b/ovsdb/jsonrpc-server.c
index 6bbda5296..55148971a 100644
--- a/ovsdb/jsonrpc-server.c
+++ b/ovsdb/jsonrpc-server.c
@@ -37,13 +37,13 @@
#include "timeval.h"
#include "transaction.h"
#include "trigger.h"
+#include "monitor.h"
#include "openvswitch/vlog.h"
VLOG_DEFINE_THIS_MODULE(ovsdb_jsonrpc_server);
struct ovsdb_jsonrpc_remote;
struct ovsdb_jsonrpc_session;
-struct ovsdb_jsonrpc_monitor;
/* Message rate-limiting. */
static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
@@ -82,7 +82,6 @@ static void ovsdb_jsonrpc_trigger_complete_done(
static struct jsonrpc_msg *ovsdb_jsonrpc_monitor_create(
struct ovsdb_jsonrpc_session *, struct ovsdb *, struct json *params,
const struct json *request_id);
-static void ovsdb_jsonrpc_monitor_destroy(struct ovsdb_jsonrpc_monitor *);
static struct jsonrpc_msg *ovsdb_jsonrpc_monitor_cancel(
struct ovsdb_jsonrpc_session *,
struct json_array *params,
@@ -1028,86 +1027,16 @@ ovsdb_jsonrpc_trigger_complete_done(struct ovsdb_jsonrpc_session *s)
}
}
-/* database table monitors. */
-
-enum ovsdb_monitor_selection {
- OJMS_INITIAL = 1 << 0, /* All rows when monitor is created. */
- OJMS_INSERT = 1 << 1, /* New rows. */
- OJMS_DELETE = 1 << 2, /* Deleted rows. */
- OJMS_MODIFY = 1 << 3 /* Modified rows. */
-};
-
-/* A particular column being monitored. */
-struct ovsdb_monitor_column {
- const struct ovsdb_column *column;
- enum ovsdb_monitor_selection select;
-};
-
-/* A row that has changed in a monitored table. */
-struct ovsdb_monitor_row {
- struct hmap_node hmap_node; /* In ovsdb_jsonrpc_monitor_table.changes. */
- struct uuid uuid; /* UUID of row that changed. */
- struct ovsdb_datum *old; /* Old data, NULL for an inserted row. */
- struct ovsdb_datum *new; /* New data, NULL for a deleted row. */
-};
-
-/* A particular table being monitored. */
-struct ovsdb_monitor_table {
- const struct ovsdb_table *table;
-
- /* This is the union (bitwise-OR) of the 'select' values in all of the
- * members of 'columns' below. */
- enum ovsdb_monitor_selection select;
-
- /* Columns being monitored. */
- struct ovsdb_monitor_column *columns;
- size_t n_columns;
-
- /* Contains 'struct ovsdb_monitor_row's for rows that have been
- * updated but not yet flushed to the jsonrpc connection. */
- struct hmap changes;
-};
-
-struct ovsdb_jsonrpc_monitor;
-/* Backend monitor.
- *
- * ovsdb_monitor keep track of the ovsdb changes.
- */
-/* A collection of tables being monitored. */
-struct ovsdb_monitor {
- struct ovsdb_replica replica;
- struct shash tables; /* Holds "struct ovsdb_monitor_table"s. */
- struct ovsdb_jsonrpc_monitor *jsonrpc_monitor;
-};
-
/* Jsonrpc front end monitor. */
struct ovsdb_jsonrpc_monitor {
struct ovsdb_jsonrpc_session *session;
struct ovsdb *db;
struct hmap_node node; /* In ovsdb_jsonrpc_session's "monitors". */
-
struct json *monitor_id;
struct ovsdb_monitor *dbmon;
};
-static const struct ovsdb_replica_class ovsdb_jsonrpc_replica_class;
-
-struct ovsdb_jsonrpc_monitor *ovsdb_jsonrpc_monitor_find(
- struct ovsdb_jsonrpc_session *, const struct json *monitor_id);
-static void ovsdb_monitor_destroy(struct ovsdb_monitor *);
-static struct json *ovsdb_monitor_get_initial(
- const struct ovsdb_monitor *);
-
-static bool
-parse_bool(struct ovsdb_parser *parser, const char *name, bool default_value)
-{
- const struct json *json;
-
- json = ovsdb_parser_member(parser, name, OP_BOOLEAN | OP_OPTIONAL);
- return json ? json_boolean(json) : default_value;
-}
-
-struct ovsdb_jsonrpc_monitor *
+static struct ovsdb_jsonrpc_monitor *
ovsdb_jsonrpc_monitor_find(struct ovsdb_jsonrpc_session *s,
const struct json *monitor_id)
{
@@ -1122,45 +1051,13 @@ ovsdb_jsonrpc_monitor_find(struct ovsdb_jsonrpc_session *s,
return NULL;
}
-static void
-ovsdb_monitor_add_column(struct ovsdb_monitor *dbmon,
- const struct ovsdb_table *table,
- const struct ovsdb_column *column,
- enum ovsdb_monitor_selection select,
- size_t *allocated_columns)
-{
- struct ovsdb_monitor_table *mt;
- struct ovsdb_monitor_column *c;
-
- mt = shash_find_data(&dbmon->tables, table->schema->name);
-
- if (mt->n_columns >= *allocated_columns) {
- mt->columns = x2nrealloc(mt->columns, allocated_columns,
- sizeof *mt->columns);
- }
-
- c = &mt->columns[mt->n_columns++];
- c->column = column;
- c->select = select;
-}
-
-static int
-compare_ovsdb_monitor_column(const void *a_, const void *b_)
+static bool
+parse_bool(struct ovsdb_parser *parser, const char *name, bool default_value)
{
- const struct ovsdb_monitor_column *a = a_;
- const struct ovsdb_monitor_column *b = b_;
-
- return a->column < b->column ? -1 : a->column > b->column;
-}
+ const struct json *json;
-static void
-ovsdb_monitor_table_add_select(struct ovsdb_monitor *dbmon,
- const struct ovsdb_table *table,
- enum ovsdb_monitor_selection select)
-{
- struct ovsdb_monitor_table * mt;
- mt = shash_find_data(&dbmon->tables, table->schema->name);
- mt->select |= select;
+ json = ovsdb_parser_member(parser, name, OP_BOOLEAN | OP_OPTIONAL);
+ return json ? json_boolean(json) : default_value;
}
static struct ovsdb_error * OVS_WARN_UNUSED_RESULT
@@ -1249,61 +1146,6 @@ ovsdb_jsonrpc_parse_monitor_request(struct ovsdb_monitor *dbmon,
return NULL;
}
-static struct ovsdb_monitor *
-ovsdb_monitor_create(struct ovsdb *db,
- struct ovsdb_jsonrpc_monitor *jsonrpc_monitor,
- const struct ovsdb_replica_class *replica_class)
-{
- struct ovsdb_monitor *dbmon;
-
- dbmon = xzalloc(sizeof *dbmon);
-
- ovsdb_replica_init(&dbmon->replica, replica_class);
- ovsdb_add_replica(db, &dbmon->replica);
- dbmon->jsonrpc_monitor = jsonrpc_monitor;
- shash_init(&dbmon->tables);
-
- return dbmon;
-}
-
-static void
-ovsdb_monitor_add_table(struct ovsdb_monitor *m,
- const struct ovsdb_table *table)
-{
- struct ovsdb_monitor_table *mt;
-
- mt = xzalloc(sizeof *mt);
- mt->table = table;
- hmap_init(&mt->changes);
- shash_add(&m->tables, table->schema->name, mt);
-}
-
-/* Check for duplicated column names. Return the first
- * duplicated column's name if found. Otherwise return
- * NULL. */
-static const char * OVS_WARN_UNUSED_RESULT
-ovsdb_monitor_table_check_duplicates(struct ovsdb_monitor *m,
- const struct ovsdb_table *table)
-{
- struct ovsdb_monitor_table *mt;
- int i;
-
- mt = shash_find_data(&m->tables, table->schema->name);
-
- if (mt) {
- /* Check for duplicate columns. */
- qsort(mt->columns, mt->n_columns, sizeof *mt->columns,
- compare_ovsdb_monitor_column);
- for (i = 1; i < mt->n_columns; i++) {
- if (mt->columns[i].column == mt->columns[i - 1].column) {
- return mt->columns[i].column->name;
- }
- }
- }
-
- return NULL;
-}
-
static struct jsonrpc_msg *
ovsdb_jsonrpc_monitor_create(struct ovsdb_jsonrpc_session *s, struct ovsdb *db,
struct json *params,
@@ -1335,7 +1177,7 @@ ovsdb_jsonrpc_monitor_create(struct ovsdb_jsonrpc_session *s, struct ovsdb *db,
m = xzalloc(sizeof *m);
m->session = s;
m->db = db;
- m->dbmon = ovsdb_monitor_create(db, m, &ovsdb_jsonrpc_replica_class);
+ m->dbmon = ovsdb_monitor_create(db, m);
hmap_insert(&s->monitors, &m->node, json_hash(monitor_id, 0));
m->monitor_id = json_clone(monitor_id);
@@ -1431,309 +1273,6 @@ ovsdb_jsonrpc_monitor_remove_all(struct ovsdb_jsonrpc_session *s)
}
}
-static struct ovsdb_monitor *
-ovsdb_monitor_cast(struct ovsdb_replica *replica)
-{
- ovs_assert(replica->class == &ovsdb_jsonrpc_replica_class);
- return CONTAINER_OF(replica, struct ovsdb_monitor, replica);
-}
-
-struct ovsdb_monitor_aux {
- const struct ovsdb_monitor *monitor;
- struct ovsdb_monitor_table *mt;
-};
-
-/* Finds and returns the ovsdb_monitor_row in 'mt->changes' for the
- * given 'uuid', or NULL if there is no such row. */
-static struct ovsdb_monitor_row *
-ovsdb_monitor_row_find(const struct ovsdb_monitor_table *mt,
- const struct uuid *uuid)
-{
- struct ovsdb_monitor_row *row;
-
- HMAP_FOR_EACH_WITH_HASH (row, hmap_node, uuid_hash(uuid), &mt->changes) {
- if (uuid_equals(uuid, &row->uuid)) {
- return row;
- }
- }
- return NULL;
-}
-
-/* Allocates an array of 'mt->n_columns' ovsdb_datums and initializes them as
- * copies of the data in 'row' drawn from the columns represented by
- * mt->columns[]. Returns the array.
- *
- * If 'row' is NULL, returns NULL. */
-static struct ovsdb_datum *
-clone_monitor_row_data(const struct ovsdb_monitor_table *mt,
- const struct ovsdb_row *row)
-{
- struct ovsdb_datum *data;
- size_t i;
-
- if (!row) {
- return NULL;
- }
-
- data = xmalloc(mt->n_columns * sizeof *data);
- for (i = 0; i < mt->n_columns; i++) {
- const struct ovsdb_column *c = mt->columns[i].column;
- const struct ovsdb_datum *src = &row->fields[c->index];
- struct ovsdb_datum *dst = &data[i];
- const struct ovsdb_type *type = &c->type;
-
- ovsdb_datum_clone(dst, src, type);
- }
- return data;
-}
-
-/* Replaces the mt->n_columns ovsdb_datums in row[] by copies of the data from
- * in 'row' drawn from the columns represented by mt->columns[]. */
-static void
-update_monitor_row_data(const struct ovsdb_monitor_table *mt,
- const struct ovsdb_row *row,
- struct ovsdb_datum *data)
-{
- size_t i;
-
- for (i = 0; i < mt->n_columns; i++) {
- const struct ovsdb_column *c = mt->columns[i].column;
- const struct ovsdb_datum *src = &row->fields[c->index];
- struct ovsdb_datum *dst = &data[i];
- const struct ovsdb_type *type = &c->type;
-
- if (!ovsdb_datum_equals(src, dst, type)) {
- ovsdb_datum_destroy(dst, type);
- ovsdb_datum_clone(dst, src, type);
- }
- }
-}
-
-/* Frees all of the mt->n_columns ovsdb_datums in data[], using the types taken
- * from mt->columns[], plus 'data' itself. */
-static void
-free_monitor_row_data(const struct ovsdb_monitor_table *mt,
- struct ovsdb_datum *data)
-{
- if (data) {
- size_t i;
-
- for (i = 0; i < mt->n_columns; i++) {
- const struct ovsdb_column *c = mt->columns[i].column;
-
- ovsdb_datum_destroy(&data[i], &c->type);
- }
- free(data);
- }
-}
-
-/* Frees 'row', which must have been created from 'mt'. */
-static void
-ovsdb_monitor_row_destroy(const struct ovsdb_monitor_table *mt,
- struct ovsdb_monitor_row *row)
-{
- if (row) {
- free_monitor_row_data(mt, row->old);
- free_monitor_row_data(mt, row->new);
- free(row);
- }
-}
-
-static bool
-ovsdb_monitor_change_cb(const struct ovsdb_row *old,
- const struct ovsdb_row *new,
- const unsigned long int *changed OVS_UNUSED,
- void *aux_)
-{
- struct ovsdb_monitor_aux *aux = aux_;
- const struct ovsdb_monitor *m = aux->monitor;
- struct ovsdb_table *table = new ? new->table : old->table;
- const struct uuid *uuid = ovsdb_row_get_uuid(new ? new : old);
- struct ovsdb_monitor_row *change;
- struct ovsdb_monitor_table *mt;
-
- if (!aux->mt || table != aux->mt->table) {
- aux->mt = shash_find_data(&m->tables, table->schema->name);
- if (!aux->mt) {
- /* We don't care about rows in this table at all. Tell the caller
- * to skip it. */
- return false;
- }
- }
- mt = aux->mt;
-
- change = ovsdb_monitor_row_find(mt, uuid);
- if (!change) {
- change = xmalloc(sizeof *change);
- hmap_insert(&mt->changes, &change->hmap_node, uuid_hash(uuid));
- change->uuid = *uuid;
- change->old = clone_monitor_row_data(mt, old);
- change->new = clone_monitor_row_data(mt, new);
- } else {
- if (new) {
- update_monitor_row_data(mt, new, change->new);
- } else {
- free_monitor_row_data(mt, change->new);
- change->new = NULL;
-
- if (!change->old) {
- /* This row was added then deleted. Forget about it. */
- hmap_remove(&mt->changes, &change->hmap_node);
- free(change);
- }
- }
- }
- return true;
-}
-
-/* Returns JSON for a <row-update> (as described in RFC 7047) for 'row' within
- * 'mt', or NULL if no row update should be sent.
- *
- * The caller should specify 'initial' as true if the returned JSON is going to
- * be used as part of the initial reply to a "monitor" request, false if it is
- * going to be used as part of an "update" notification.
- *
- * 'changed' must be a scratch buffer for internal use that is at least
- * bitmap_n_bytes(mt->n_columns) bytes long. */
-static struct json *
-ovsdb_monitor_compose_row_update(
- const struct ovsdb_monitor_table *mt,
- const struct ovsdb_monitor_row *row,
- bool initial, unsigned long int *changed)
-{
- enum ovsdb_monitor_selection type;
- struct json *old_json, *new_json;
- struct json *row_json;
- size_t i;
-
- type = (initial ? OJMS_INITIAL
- : !row->old ? OJMS_INSERT
- : !row->new ? OJMS_DELETE
- : OJMS_MODIFY);
- if (!(mt->select & type)) {
- return NULL;
- }
-
- if (type == OJMS_MODIFY) {
- size_t n_changes;
-
- n_changes = 0;
- memset(changed, 0, bitmap_n_bytes(mt->n_columns));
- for (i = 0; i < mt->n_columns; i++) {
- const struct ovsdb_column *c = mt->columns[i].column;
- if (!ovsdb_datum_equals(&row->old[i], &row->new[i], &c->type)) {
- bitmap_set1(changed, i);
- n_changes++;
- }
- }
- if (!n_changes) {
- /* No actual changes: presumably a row changed and then
- * changed back later. */
- return NULL;
- }
- }
-
- row_json = json_object_create();
- old_json = new_json = NULL;
- if (type & (OJMS_DELETE | OJMS_MODIFY)) {
- old_json = json_object_create();
- json_object_put(row_json, "old", old_json);
- }
- if (type & (OJMS_INITIAL | OJMS_INSERT | OJMS_MODIFY)) {
- new_json = json_object_create();
- json_object_put(row_json, "new", new_json);
- }
- for (i = 0; i < mt->n_columns; i++) {
- const struct ovsdb_monitor_column *c = &mt->columns[i];
-
- if (!(type & c->select)) {
- /* We don't care about this type of change for this
- * particular column (but we will care about it for some
- * other column). */
- continue;
- }
-
- if ((type == OJMS_MODIFY && bitmap_is_set(changed, i))
- || type == OJMS_DELETE) {
- json_object_put(old_json, c->column->name,
- ovsdb_datum_to_json(&row->old[i],
- &c->column->type));
- }
- if (type & (OJMS_INITIAL | OJMS_INSERT | OJMS_MODIFY)) {
- json_object_put(new_json, c->column->name,
- ovsdb_datum_to_json(&row->new[i],
- &c->column->type));
- }
- }
-
- return row_json;
-}
-
-/* Constructs and returns JSON for a <table-updates> object (as described in
- * RFC 7047) for all the outstanding changes within 'monitor', and deletes all
- * the outstanding changes from 'monitor'. Returns NULL if no update needs to
- * be sent.
- *
- * The caller should specify 'initial' as true if the returned JSON is going to
- * be used as part of the initial reply to a "monitor" request, false if it is
- * going to be used as part of an "update" notification. */
-static struct json *
-ovsdb_monitor_compose_table_update(
- const struct ovsdb_monitor *dbmon, bool initial)
-{
- struct shash_node *node;
- unsigned long int *changed;
- struct json *json;
- size_t max_columns;
-
- max_columns = 0;
- SHASH_FOR_EACH (node, &dbmon->tables) {
- struct ovsdb_monitor_table *mt = node->data;
-
- max_columns = MAX(max_columns, mt->n_columns);
- }
- changed = xmalloc(bitmap_n_bytes(max_columns));
-
- json = NULL;
- SHASH_FOR_EACH (node, &dbmon->tables) {
- struct ovsdb_monitor_table *mt = node->data;
- struct ovsdb_monitor_row *row, *next;
- struct json *table_json = NULL;
-
- HMAP_FOR_EACH_SAFE (row, next, hmap_node, &mt->changes) {
- struct json *row_json;
-
- row_json = ovsdb_monitor_compose_row_update(
- mt, row, initial, changed);
- if (row_json) {
- char uuid[UUID_LEN + 1];
-
- /* Create JSON object for transaction overall. */
- if (!json) {
- json = json_object_create();
- }
-
- /* Create JSON object for transaction on this table. */
- if (!table_json) {
- table_json = json_object_create();
- json_object_put(json, mt->table->schema->name, table_json);
- }
-
- /* Add JSON row to JSON table. */
- snprintf(uuid, sizeof uuid, UUID_FMT, UUID_ARGS(&row->uuid));
- json_object_put(table_json, uuid, row_json);
- }
-
- hmap_remove(&mt->changes, &row->hmap_node);
- ovsdb_monitor_row_destroy(mt, row);
- }
- }
-
- free(changed);
-
- return json;
-}
-
static struct json *
ovsdb_jsonrpc_monitor_compose_table_update(
const struct ovsdb_jsonrpc_monitor *monitor, bool initial)
@@ -1742,21 +1281,6 @@ ovsdb_jsonrpc_monitor_compose_table_update(
}
static bool
-ovsdb_monitor_needs_flush(struct ovsdb_monitor *dbmon)
-{
- struct shash_node *node;
-
- SHASH_FOR_EACH (node, &dbmon->tables) {
- struct ovsdb_monitor_table *mt = node->data;
-
- if (!hmap_is_empty(&mt->changes)) {
- return true;
- }
- }
- return false;
-}
-
-static bool
ovsdb_jsonrpc_monitor_needs_flush(struct ovsdb_jsonrpc_session *s)
{
struct ovsdb_jsonrpc_monitor *m;
@@ -1770,6 +1294,15 @@ ovsdb_jsonrpc_monitor_needs_flush(struct ovsdb_jsonrpc_session *s)
return false;
}
+void
+ovsdb_jsonrpc_monitor_destroy(struct ovsdb_jsonrpc_monitor *m)
+{
+ json_destroy(m->monitor_id);
+ hmap_remove(&m->session->monitors, &m->node);
+ ovsdb_monitor_destroy(m->dbmon);
+ free(m);
+}
+
static void
ovsdb_jsonrpc_monitor_flush_all(struct ovsdb_jsonrpc_session *s)
{
@@ -1789,95 +1322,3 @@ ovsdb_jsonrpc_monitor_flush_all(struct ovsdb_jsonrpc_session *s)
}
}
}
-
-static void
-ovsdb_monitor_init_aux(struct ovsdb_monitor_aux *aux,
- const struct ovsdb_monitor *m)
-{
- aux->monitor = m;
- aux->mt = NULL;
-}
-
-static struct ovsdb_error *
-ovsdb_monitor_commit(struct ovsdb_replica *replica,
- const struct ovsdb_txn *txn,
- bool durable OVS_UNUSED)
-{
- struct ovsdb_monitor *m = ovsdb_monitor_cast(replica);
- struct ovsdb_monitor_aux aux;
-
- ovsdb_monitor_init_aux(&aux, m);
- ovsdb_txn_for_each_change(txn, ovsdb_monitor_change_cb, &aux);
-
- return NULL;
-}
-
-static struct json *
-ovsdb_monitor_get_initial(const struct ovsdb_monitor *dbmon)
-{
- struct ovsdb_monitor_aux aux;
- struct shash_node *node;
- struct json *json;
-
- ovsdb_monitor_init_aux(&aux, dbmon);
- SHASH_FOR_EACH (node, &dbmon->tables) {
- struct ovsdb_monitor_table *mt = node->data;
-
- if (mt->select & OJMS_INITIAL) {
- struct ovsdb_row *row;
-
- HMAP_FOR_EACH (row, hmap_node, &mt->table->rows) {
- ovsdb_monitor_change_cb(NULL, row, NULL, &aux);
- }
- }
- }
- json = ovsdb_monitor_compose_table_update(dbmon, true);
- return json ? json : json_object_create();
-}
-
-static void
-ovsdb_jsonrpc_monitor_destroy(struct ovsdb_jsonrpc_monitor *m)
-{
- json_destroy(m->monitor_id);
- hmap_remove(&m->session->monitors, &m->node);
- ovsdb_monitor_destroy(m->dbmon);
- free(m);
-}
-
-static void
-ovsdb_monitor_destroy(struct ovsdb_monitor *dbmon)
-{
- struct shash_node *node;
-
- list_remove(&dbmon->replica.node);
-
- SHASH_FOR_EACH (node, &dbmon->tables) {
- struct ovsdb_monitor_table *mt = node->data;
- struct ovsdb_monitor_row *row, *next;
-
- HMAP_FOR_EACH_SAFE (row, next, hmap_node, &mt->changes) {
- hmap_remove(&mt->changes, &row->hmap_node);
- ovsdb_monitor_row_destroy(mt, row);
- }
- hmap_destroy(&mt->changes);
-
- free(mt->columns);
- free(mt);
- }
- shash_destroy(&dbmon->tables);
- free(dbmon);
-}
-
-static void
-ovsdb_monitor_destroy_callback(struct ovsdb_replica *replica)
-{
- struct ovsdb_monitor *dbmon = ovsdb_monitor_cast(replica);
- struct ovsdb_jsonrpc_monitor *m = dbmon->jsonrpc_monitor;
-
- ovsdb_jsonrpc_monitor_destroy(m);
-}
-
-static const struct ovsdb_replica_class ovsdb_jsonrpc_replica_class = {
- ovsdb_monitor_commit,
- ovsdb_monitor_destroy_callback,
-};
diff --git a/ovsdb/jsonrpc-server.h b/ovsdb/jsonrpc-server.h
index e6a1642a5..fce8b7bd0 100644
--- a/ovsdb/jsonrpc-server.h
+++ b/ovsdb/jsonrpc-server.h
@@ -69,4 +69,7 @@ void ovsdb_jsonrpc_server_wait(struct ovsdb_jsonrpc_server *);
void ovsdb_jsonrpc_server_get_memory_usage(const struct ovsdb_jsonrpc_server *,
struct simap *usage);
+struct ovsdb_jsonrpc_monitor;
+void ovsdb_jsonrpc_monitor_destroy(struct ovsdb_jsonrpc_monitor *);
+
#endif /* ovsdb/jsonrpc-server.h */
diff --git a/ovsdb/monitor.c b/ovsdb/monitor.c
new file mode 100644
index 000000000..a1aeb5f51
--- /dev/null
+++ b/ovsdb/monitor.c
@@ -0,0 +1,581 @@
+/*
+ * Copyright (c) 2015 Nicira, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <config.h>
+
+#include <errno.h>
+
+#include "bitmap.h"
+#include "column.h"
+#include "dynamic-string.h"
+#include "json.h"
+#include "jsonrpc.h"
+#include "ovsdb-error.h"
+#include "ovsdb-parser.h"
+#include "ovsdb.h"
+#include "row.h"
+#include "simap.h"
+#include "table.h"
+#include "timeval.h"
+#include "transaction.h"
+#include "jsonrpc-server.h"
+#include "monitor.h"
+#include "openvswitch/vlog.h"
+
+
+static const struct ovsdb_replica_class ovsdb_jsonrpc_replica_class;
+
+/* Backend monitor.
+ *
+ * ovsdb_monitor keep track of the ovsdb changes.
+ */
+
+/* A collection of tables being monitored. */
+struct ovsdb_monitor {
+ struct ovsdb_replica replica;
+ struct shash tables; /* Holds "struct ovsdb_monitor_table"s. */
+ struct ovsdb_jsonrpc_monitor *jsonrpc_monitor;
+ struct ovsdb *db;
+};
+
+/* A particular column being monitored. */
+struct ovsdb_monitor_column {
+ const struct ovsdb_column *column;
+ enum ovsdb_monitor_selection select;
+};
+
+/* A row that has changed in a monitored table. */
+struct ovsdb_monitor_row {
+ struct hmap_node hmap_node; /* In ovsdb_jsonrpc_monitor_table.changes. */
+ struct uuid uuid; /* UUID of row that changed. */
+ struct ovsdb_datum *old; /* Old data, NULL for an inserted row. */
+ struct ovsdb_datum *new; /* New data, NULL for a deleted row. */
+};
+
+/* A particular table being monitored. */
+struct ovsdb_monitor_table {
+ const struct ovsdb_table *table;
+
+ /* This is the union (bitwise-OR) of the 'select' values in all of the
+ * members of 'columns' below. */
+ enum ovsdb_monitor_selection select;
+
+ /* Columns being monitored. */
+ struct ovsdb_monitor_column *columns;
+ size_t n_columns;
+
+ /* Contains 'struct ovsdb_monitor_row's for rows that have been
+ * updated but not yet flushed to the jsonrpc connection. */
+ struct hmap changes;
+};
+
+static int
+compare_ovsdb_monitor_column(const void *a_, const void *b_)
+{
+ const struct ovsdb_monitor_column *a = a_;
+ const struct ovsdb_monitor_column *b = b_;
+
+ return a->column < b->column ? -1 : a->column > b->column;
+}
+
+static struct ovsdb_monitor *
+ovsdb_monitor_cast(struct ovsdb_replica *replica)
+{
+ ovs_assert(replica->class == &ovsdb_jsonrpc_replica_class);
+ return CONTAINER_OF(replica, struct ovsdb_monitor, replica);
+}
+
+/* Finds and returns the ovsdb_monitor_row in 'mt->changes' for the
+ * given 'uuid', or NULL if there is no such row. */
+static struct ovsdb_monitor_row *
+ovsdb_monitor_row_find(const struct ovsdb_monitor_table *mt,
+ const struct uuid *uuid)
+{
+ struct ovsdb_monitor_row *row;
+
+ HMAP_FOR_EACH_WITH_HASH (row, hmap_node, uuid_hash(uuid), &mt->changes) {
+ if (uuid_equals(uuid, &row->uuid)) {
+ return row;
+ }
+ }
+ return NULL;
+}
+
+/* Allocates an array of 'mt->n_columns' ovsdb_datums and initializes them as
+ * copies of the data in 'row' drawn from the columns represented by
+ * mt->columns[]. Returns the array.
+ *
+ * If 'row' is NULL, returns NULL. */
+static struct ovsdb_datum *
+clone_monitor_row_data(const struct ovsdb_monitor_table *mt,
+ const struct ovsdb_row *row)
+{
+ struct ovsdb_datum *data;
+ size_t i;
+
+ if (!row) {
+ return NULL;
+ }
+
+ data = xmalloc(mt->n_columns * sizeof *data);
+ for (i = 0; i < mt->n_columns; i++) {
+ const struct ovsdb_column *c = mt->columns[i].column;
+ const struct ovsdb_datum *src = &row->fields[c->index];
+ struct ovsdb_datum *dst = &data[i];
+ const struct ovsdb_type *type = &c->type;
+
+ ovsdb_datum_clone(dst, src, type);
+ }
+ return data;
+}
+
+/* Replaces the mt->n_columns ovsdb_datums in row[] by copies of the data from
+ * in 'row' drawn from the columns represented by mt->columns[]. */
+static void
+update_monitor_row_data(const struct ovsdb_monitor_table *mt,
+ const struct ovsdb_row *row,
+ struct ovsdb_datum *data)
+{
+ size_t i;
+
+ for (i = 0; i < mt->n_columns; i++) {
+ const struct ovsdb_column *c = mt->columns[i].column;
+ const struct ovsdb_datum *src = &row->fields[c->index];
+ struct ovsdb_datum *dst = &data[i];
+ const struct ovsdb_type *type = &c->type;
+
+ if (!ovsdb_datum_equals(src, dst, type)) {
+ ovsdb_datum_destroy(dst, type);
+ ovsdb_datum_clone(dst, src, type);
+ }
+ }
+}
+
+/* Frees all of the mt->n_columns ovsdb_datums in data[], using the types taken
+ * from mt->columns[], plus 'data' itself. */
+static void
+free_monitor_row_data(const struct ovsdb_monitor_table *mt,
+ struct ovsdb_datum *data)
+{
+ if (data) {
+ size_t i;
+
+ for (i = 0; i < mt->n_columns; i++) {
+ const struct ovsdb_column *c = mt->columns[i].column;
+
+ ovsdb_datum_destroy(&data[i], &c->type);
+ }
+ free(data);
+ }
+}
+
+/* Frees 'row', which must have been created from 'mt'. */
+static void
+ovsdb_monitor_row_destroy(const struct ovsdb_monitor_table *mt,
+ struct ovsdb_monitor_row *row)
+{
+ if (row) {
+ free_monitor_row_data(mt, row->old);
+ free_monitor_row_data(mt, row->new);
+ free(row);
+ }
+}
+
+struct ovsdb_monitor *
+ovsdb_monitor_create(struct ovsdb *db,
+ struct ovsdb_jsonrpc_monitor *jsonrpc_monitor)
+{
+ struct ovsdb_monitor *dbmon;
+
+ dbmon = xzalloc(sizeof *dbmon);
+
+ ovsdb_replica_init(&dbmon->replica, &ovsdb_jsonrpc_replica_class);
+ ovsdb_add_replica(db, &dbmon->replica);
+ dbmon->jsonrpc_monitor = jsonrpc_monitor;
+ dbmon->db = db;
+ shash_init(&dbmon->tables);
+
+ return dbmon;
+}
+
+void
+ovsdb_monitor_add_table(struct ovsdb_monitor *m,
+ const struct ovsdb_table *table)
+{
+ struct ovsdb_monitor_table *mt;
+
+ mt = xzalloc(sizeof *mt);
+ mt->table = table;
+ hmap_init(&mt->changes);
+ shash_add(&m->tables, table->schema->name, mt);
+}
+
+void
+ovsdb_monitor_add_column(struct ovsdb_monitor *dbmon,
+ const struct ovsdb_table *table,
+ const struct ovsdb_column *column,
+ enum ovsdb_monitor_selection select,
+ size_t *allocated_columns)
+{
+ struct ovsdb_monitor_table *mt;
+ struct ovsdb_monitor_column *c;
+
+ mt = shash_find_data(&dbmon->tables, table->schema->name);
+
+ if (mt->n_columns >= *allocated_columns) {
+ mt->columns = x2nrealloc(mt->columns, allocated_columns,
+ sizeof *mt->columns);
+ }
+
+ mt->select |= select;
+ c = &mt->columns[mt->n_columns++];
+ c->column = column;
+ c->select = select;
+}
+
+/* Check for duplicated column names. Return the first
+ * duplicated column's name if found. Otherwise return
+ * NULL. */
+const char * OVS_WARN_UNUSED_RESULT
+ovsdb_monitor_table_check_duplicates(struct ovsdb_monitor *m,
+ const struct ovsdb_table *table)
+{
+ struct ovsdb_monitor_table *mt;
+ int i;
+
+ mt = shash_find_data(&m->tables, table->schema->name);
+
+ if (mt) {
+ /* Check for duplicate columns. */
+ qsort(mt->columns, mt->n_columns, sizeof *mt->columns,
+ compare_ovsdb_monitor_column);
+ for (i = 1; i < mt->n_columns; i++) {
+ if (mt->columns[i].column == mt->columns[i - 1].column) {
+ return mt->columns[i].column->name;
+ }
+ }
+ }
+
+ return NULL;
+}
+
+/* Returns JSON for a <row-update> (as described in RFC 7047) for 'row' within
+ * 'mt', or NULL if no row update should be sent.
+ *
+ * The caller should specify 'initial' as true if the returned JSON is going to
+ * be used as part of the initial reply to a "monitor" request, false if it is
+ * going to be used as part of an "update" notification.
+ *
+ * 'changed' must be a scratch buffer for internal use that is at least
+ * bitmap_n_bytes(mt->n_columns) bytes long. */
+static struct json *
+ovsdb_monitor_compose_row_update(
+ const struct ovsdb_monitor_table *mt,
+ const struct ovsdb_monitor_row *row,
+ bool initial, unsigned long int *changed)
+{
+ enum ovsdb_monitor_selection type;
+ struct json *old_json, *new_json;
+ struct json *row_json;
+ size_t i;
+
+ type = (initial ? OJMS_INITIAL
+ : !row->old ? OJMS_INSERT
+ : !row->new ? OJMS_DELETE
+ : OJMS_MODIFY);
+ if (!(mt->select & type)) {
+ return NULL;
+ }
+
+ if (type == OJMS_MODIFY) {
+ size_t n_changes;
+
+ n_changes = 0;
+ memset(changed, 0, bitmap_n_bytes(mt->n_columns));
+ for (i = 0; i < mt->n_columns; i++) {
+ const struct ovsdb_column *c = mt->columns[i].column;
+ if (!ovsdb_datum_equals(&row->old[i], &row->new[i], &c->type)) {
+ bitmap_set1(changed, i);
+ n_changes++;
+ }
+ }
+ if (!n_changes) {
+ /* No actual changes: presumably a row changed and then
+ * changed back later. */
+ return NULL;
+ }
+ }
+
+ row_json = json_object_create();
+ old_json = new_json = NULL;
+ if (type & (OJMS_DELETE | OJMS_MODIFY)) {
+ old_json = json_object_create();
+ json_object_put(row_json, "old", old_json);
+ }
+ if (type & (OJMS_INITIAL | OJMS_INSERT | OJMS_MODIFY)) {
+ new_json = json_object_create();
+ json_object_put(row_json, "new", new_json);
+ }
+ for (i = 0; i < mt->n_columns; i++) {
+ const struct ovsdb_monitor_column *c = &mt->columns[i];
+
+ if (!(type & c->select)) {
+ /* We don't care about this type of change for this
+ * particular column (but we will care about it for some
+ * other column). */
+ continue;
+ }
+
+ if ((type == OJMS_MODIFY && bitmap_is_set(changed, i))
+ || type == OJMS_DELETE) {
+ json_object_put(old_json, c->column->name,
+ ovsdb_datum_to_json(&row->old[i],
+ &c->column->type));
+ }
+ if (type & (OJMS_INITIAL | OJMS_INSERT | OJMS_MODIFY)) {
+ json_object_put(new_json, c->column->name,
+ ovsdb_datum_to_json(&row->new[i],
+ &c->column->type));
+ }
+ }
+
+ return row_json;
+}
+
+/* Constructs and returns JSON for a <table-updates> object (as described in
+ * RFC 7047) for all the outstanding changes within 'monitor', and deletes all
+ * the outstanding changes from 'monitor'. Returns NULL if no update needs to
+ * be sent.
+ *
+ * The caller should specify 'initial' as true if the returned JSON is going to
+ * be used as part of the initial reply to a "monitor" request, false if it is
+ * going to be used as part of an "update" notification. */
+struct json *
+ovsdb_monitor_compose_table_update(
+ const struct ovsdb_monitor *dbmon, bool initial)
+{
+ struct shash_node *node;
+ unsigned long int *changed;
+ struct json *json;
+ size_t max_columns;
+
+ max_columns = 0;
+ SHASH_FOR_EACH (node, &dbmon->tables) {
+ struct ovsdb_monitor_table *mt = node->data;
+
+ max_columns = MAX(max_columns, mt->n_columns);
+ }
+ changed = xmalloc(bitmap_n_bytes(max_columns));
+
+ json = NULL;
+ SHASH_FOR_EACH (node, &dbmon->tables) {
+ struct ovsdb_monitor_table *mt = node->data;
+ struct ovsdb_monitor_row *row, *next;
+ struct json *table_json = NULL;
+
+ HMAP_FOR_EACH_SAFE (row, next, hmap_node, &mt->changes) {
+ struct json *row_json;
+
+ row_json = ovsdb_monitor_compose_row_update(
+ mt, row, initial, changed);
+ if (row_json) {
+ char uuid[UUID_LEN + 1];
+
+ /* Create JSON object for transaction overall. */
+ if (!json) {
+ json = json_object_create();
+ }
+
+ /* Create JSON object for transaction on this table. */
+ if (!table_json) {
+ table_json = json_object_create();
+ json_object_put(json, mt->table->schema->name, table_json);
+ }
+
+ /* Add JSON row to JSON table. */
+ snprintf(uuid, sizeof uuid, UUID_FMT, UUID_ARGS(&row->uuid));
+ json_object_put(table_json, uuid, row_json);
+ }
+
+ hmap_remove(&mt->changes, &row->hmap_node);
+ ovsdb_monitor_row_destroy(mt, row);
+ }
+ }
+
+ free(changed);
+ return json;
+}
+
+bool
+ovsdb_monitor_needs_flush(struct ovsdb_monitor *dbmon)
+{
+ struct shash_node *node;
+
+ SHASH_FOR_EACH (node, &dbmon->tables) {
+ struct ovsdb_monitor_table *mt = node->data;
+
+ if (!hmap_is_empty(&mt->changes)) {
+ return true;
+ }
+ }
+ return false;
+}
+
+void
+ovsdb_monitor_table_add_select(struct ovsdb_monitor *dbmon,
+ const struct ovsdb_table *table,
+ enum ovsdb_monitor_selection select)
+{
+ struct ovsdb_monitor_table * mt;
+
+ mt = shash_find_data(&dbmon->tables, table->schema->name);
+ mt->select |= select;
+}
+
+struct ovsdb_monitor_aux {
+ const struct ovsdb_monitor *monitor;
+ struct ovsdb_monitor_table *mt;
+};
+
+static void
+ovsdb_monitor_init_aux(struct ovsdb_monitor_aux *aux,
+ const struct ovsdb_monitor *m)
+{
+ aux->monitor = m;
+ aux->mt = NULL;
+}
+
+static bool
+ovsdb_monitor_change_cb(const struct ovsdb_row *old,
+ const struct ovsdb_row *new,
+ const unsigned long int *changed OVS_UNUSED,
+ void *aux_)
+{
+ struct ovsdb_monitor_aux *aux = aux_;
+ const struct ovsdb_monitor *m = aux->monitor;
+ struct ovsdb_table *table = new ? new->table : old->table;
+ const struct uuid *uuid = ovsdb_row_get_uuid(new ? new : old);
+ struct ovsdb_monitor_row *change;
+ struct ovsdb_monitor_table *mt;
+
+ if (!aux->mt || table != aux->mt->table) {
+ aux->mt = shash_find_data(&m->tables, table->schema->name);
+ if (!aux->mt) {
+ /* We don't care about rows in this table at all. Tell the caller
+ * to skip it. */
+ return false;
+ }
+ }
+ mt = aux->mt;
+
+ change = ovsdb_monitor_row_find(mt, uuid);
+ if (!change) {
+ change = xmalloc(sizeof *change);
+ hmap_insert(&mt->changes, &change->hmap_node, uuid_hash(uuid));
+ change->uuid = *uuid;
+ change->old = clone_monitor_row_data(mt, old);
+ change->new = clone_monitor_row_data(mt, new);
+ } else {
+ if (new) {
+ update_monitor_row_data(mt, new, change->new);
+ } else {
+ free_monitor_row_data(mt, change->new);
+ change->new = NULL;
+
+ if (!change->old) {
+ /* This row was added then deleted. Forget about it. */
+ hmap_remove(&mt->changes, &change->hmap_node);
+ free(change);
+ }
+ }
+ }
+ return true;
+}
+
+struct json *
+ovsdb_monitor_get_initial(const struct ovsdb_monitor *dbmon)
+{
+ struct ovsdb_monitor_aux aux;
+ struct shash_node *node;
+ struct json *json;
+
+ ovsdb_monitor_init_aux(&aux, dbmon);
+ SHASH_FOR_EACH (node, &dbmon->tables) {
+ struct ovsdb_monitor_table *mt = node->data;
+
+ if (mt->select & OJMS_INITIAL) {
+ struct ovsdb_row *row;
+
+ HMAP_FOR_EACH (row, hmap_node, &mt->table->rows) {
+ ovsdb_monitor_change_cb(NULL, row, NULL, &aux);
+ }
+ }
+ }
+ json = ovsdb_monitor_compose_table_update(dbmon, true);
+ return json ? json : json_object_create();
+}
+
+void
+ovsdb_monitor_destroy(struct ovsdb_monitor *dbmon)
+{
+ struct shash_node *node;
+
+ list_remove(&dbmon->replica.node);
+
+ SHASH_FOR_EACH (node, &dbmon->tables) {
+ struct ovsdb_monitor_table *mt = node->data;
+ struct ovsdb_monitor_row *row, *next;
+
+ HMAP_FOR_EACH_SAFE (row, next, hmap_node, &mt->changes) {
+ hmap_remove(&mt->changes, &row->hmap_node);
+ ovsdb_monitor_row_destroy(mt, row);
+ }
+ hmap_destroy(&mt->changes);
+
+ free(mt->columns);
+ free(mt);
+ }
+ shash_destroy(&dbmon->tables);
+ free(dbmon);
+}
+
+static struct ovsdb_error *
+ovsdb_monitor_commit(struct ovsdb_replica *replica,
+ const struct ovsdb_txn *txn,
+ bool durable OVS_UNUSED)
+{
+ struct ovsdb_monitor *m = ovsdb_monitor_cast(replica);
+ struct ovsdb_monitor_aux aux;
+
+ ovsdb_monitor_init_aux(&aux, m);
+ ovsdb_txn_for_each_change(txn, ovsdb_monitor_change_cb, &aux);
+
+ return NULL;
+}
+
+static void
+ovsdb_monitor_destroy_callback(struct ovsdb_replica *replica)
+{
+ struct ovsdb_monitor *dbmon = ovsdb_monitor_cast(replica);
+ struct ovsdb_jsonrpc_monitor *m = dbmon->jsonrpc_monitor;
+
+ ovsdb_jsonrpc_monitor_destroy(m);
+}
+
+static const struct ovsdb_replica_class ovsdb_jsonrpc_replica_class = {
+ ovsdb_monitor_commit,
+ ovsdb_monitor_destroy_callback,
+};
diff --git a/ovsdb/monitor.h b/ovsdb/monitor.h
new file mode 100644
index 000000000..82c9a578b
--- /dev/null
+++ b/ovsdb/monitor.h
@@ -0,0 +1,58 @@
+/*
+ * Copyright (c) 2015 Nicira, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef OVSDB_MONITOR_H
+#define OVSDB_MONITOR_H
+
+struct ovsdb_monitor;
+
+enum ovsdb_monitor_selection {
+ OJMS_INITIAL = 1 << 0, /* All rows when monitor is created. */
+ OJMS_INSERT = 1 << 1, /* New rows. */
+ OJMS_DELETE = 1 << 2, /* Deleted rows. */
+ OJMS_MODIFY = 1 << 3 /* Modified rows. */
+};
+
+
+struct ovsdb_monitor *ovsdb_monitor_create(struct ovsdb *db,
+ struct ovsdb_jsonrpc_monitor *jsonrpc_monitor);
+
+void ovsdb_monitor_add_table(struct ovsdb_monitor *m,
+ const struct ovsdb_table *table);
+
+void ovsdb_monitor_add_column(struct ovsdb_monitor *dbmon,
+ const struct ovsdb_table *table,
+ const struct ovsdb_column *column,
+ enum ovsdb_monitor_selection select,
+ size_t *allocated_columns);
+
+const char * OVS_WARN_UNUSED_RESULT
+ovsdb_monitor_table_check_duplicates(struct ovsdb_monitor *,
+ const struct ovsdb_table *);
+
+struct json *ovsdb_monitor_compose_table_update(
+ const struct ovsdb_monitor *dbmon, bool initial);
+
+void ovsdb_monitor_table_add_select(struct ovsdb_monitor *dbmon,
+ const struct ovsdb_table *table,
+ enum ovsdb_monitor_selection select);
+
+bool ovsdb_monitor_needs_flush(struct ovsdb_monitor *dbmon);
+
+struct json *ovsdb_monitor_get_initial(const struct ovsdb_monitor *dbmon);
+
+void ovsdb_monitor_destroy(struct ovsdb_monitor *dbmon);
+#endif