+\f
+/* JSON-RPC database table monitors. */
+
+enum ovsdb_jsonrpc_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_jsonrpc_monitor_column {
+ const struct ovsdb_column *column;
+ enum ovsdb_jsonrpc_monitor_selection select;
+};
+
+/* A particular table being monitored. */
+struct ovsdb_jsonrpc_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_jsonrpc_monitor_selection select;
+
+ /* Columns being monitored. */
+ struct ovsdb_jsonrpc_monitor_column *columns;
+ size_t n_columns;
+};
+
+/* A collection of tables being monitored. */
+struct ovsdb_jsonrpc_monitor {
+ struct ovsdb_replica replica;
+ struct ovsdb_jsonrpc_session *session;
+ struct hmap_node node; /* In ovsdb_jsonrpc_session's "monitors". */
+
+ struct json *monitor_id;
+ struct shash tables; /* Holds "struct ovsdb_jsonrpc_monitor_table"s. */
+};
+
+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_jsonrpc_monitor_destroy(struct ovsdb_replica *);
+static struct json *ovsdb_jsonrpc_monitor_get_initial(
+ const struct ovsdb_jsonrpc_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 *
+ovsdb_jsonrpc_monitor_find(struct ovsdb_jsonrpc_session *s,
+ const struct json *monitor_id)
+{
+ struct ovsdb_jsonrpc_monitor *m;
+
+ HMAP_FOR_EACH_WITH_HASH (m, node, json_hash(monitor_id, 0), &s->monitors) {
+ if (json_equal(m->monitor_id, monitor_id)) {
+ return m;
+ }
+ }
+
+ return NULL;
+}
+
+static void
+ovsdb_jsonrpc_add_monitor_column(struct ovsdb_jsonrpc_monitor_table *mt,
+ const struct ovsdb_column *column,
+ enum ovsdb_jsonrpc_monitor_selection select,
+ size_t *allocated_columns)
+{
+ struct ovsdb_jsonrpc_monitor_column *c;
+
+ 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_jsonrpc_monitor_column(const void *a_, const void *b_)
+{
+ const struct ovsdb_jsonrpc_monitor_column *a = a_;
+ const struct ovsdb_jsonrpc_monitor_column *b = b_;
+
+ return a->column < b->column ? -1 : a->column > b->column;
+}
+
+static struct ovsdb_error * WARN_UNUSED_RESULT
+ovsdb_jsonrpc_parse_monitor_request(struct ovsdb_jsonrpc_monitor_table *mt,
+ const struct json *monitor_request,
+ size_t *allocated_columns)
+{
+ const struct ovsdb_table_schema *ts = mt->table->schema;
+ enum ovsdb_jsonrpc_monitor_selection select;
+ const struct json *columns, *select_json;
+ struct ovsdb_parser parser;
+ struct ovsdb_error *error;
+
+ ovsdb_parser_init(&parser, monitor_request, "table %s", ts->name);
+ columns = ovsdb_parser_member(&parser, "columns", OP_ARRAY | OP_OPTIONAL);
+ select_json = ovsdb_parser_member(&parser, "select",
+ OP_OBJECT | OP_OPTIONAL);
+ error = ovsdb_parser_finish(&parser);
+ if (error) {
+ return error;
+ }
+
+ if (select_json) {
+ select = 0;
+ ovsdb_parser_init(&parser, select_json, "table %s select", ts->name);
+ if (parse_bool(&parser, "initial", true)) {
+ select |= OJMS_INITIAL;
+ }
+ if (parse_bool(&parser, "insert", true)) {
+ select |= OJMS_INSERT;
+ }
+ if (parse_bool(&parser, "delete", true)) {
+ select |= OJMS_DELETE;
+ }
+ if (parse_bool(&parser, "modify", true)) {
+ select |= OJMS_MODIFY;
+ }
+ error = ovsdb_parser_finish(&parser);
+ if (error) {
+ return error;
+ }
+ } else {
+ select = OJMS_INITIAL | OJMS_INSERT | OJMS_DELETE | OJMS_MODIFY;
+ }
+ mt->select |= select;
+
+ if (columns) {
+ size_t i;
+
+ if (columns->type != JSON_ARRAY) {
+ return ovsdb_syntax_error(columns, NULL,
+ "array of column names expected");
+ }
+
+ for (i = 0; i < columns->u.array.n; i++) {
+ const struct ovsdb_column *column;
+ const char *s;
+
+ if (columns->u.array.elems[i]->type != JSON_STRING) {
+ return ovsdb_syntax_error(columns, NULL,
+ "array of column names expected");
+ }
+
+ s = columns->u.array.elems[i]->u.string;
+ column = shash_find_data(&mt->table->schema->columns, s);
+ if (!column) {
+ return ovsdb_syntax_error(columns, NULL, "%s is not a valid "
+ "column name", s);
+ }
+ ovsdb_jsonrpc_add_monitor_column(mt, column, select,
+ allocated_columns);
+ }
+ } else {
+ struct shash_node *node;
+
+ SHASH_FOR_EACH (node, &ts->columns) {
+ const struct ovsdb_column *column = node->data;
+ if (column->index != OVSDB_COL_UUID) {
+ ovsdb_jsonrpc_add_monitor_column(mt, column, select,
+ allocated_columns);
+ }
+ }
+ }
+
+ return NULL;
+}
+
+static struct json *
+ovsdb_jsonrpc_monitor_create(struct ovsdb_jsonrpc_session *s,
+ struct json *params)
+{
+ struct ovsdb_jsonrpc_monitor *m = NULL;
+ struct json *monitor_id, *monitor_requests;
+ struct ovsdb_error *error = NULL;
+ struct shash_node *node;
+ struct json *json;
+
+ if (json_array(params)->n != 3) {
+ error = ovsdb_syntax_error(params, NULL, "invalid parameters");
+ goto error;
+ }
+ monitor_id = params->u.array.elems[1];
+ monitor_requests = params->u.array.elems[2];
+ if (monitor_requests->type != JSON_OBJECT) {
+ error = ovsdb_syntax_error(monitor_requests, NULL,
+ "monitor-requests must be object");
+ goto error;
+ }
+
+ if (ovsdb_jsonrpc_monitor_find(s, monitor_id)) {
+ error = ovsdb_syntax_error(monitor_id, NULL, "duplicate monitor ID");
+ goto error;
+ }
+
+ m = xzalloc(sizeof *m);
+ ovsdb_replica_init(&m->replica, &ovsdb_jsonrpc_replica_class);
+ ovsdb_add_replica(s->remote->server->up.db, &m->replica);
+ m->session = s;
+ hmap_insert(&s->monitors, &m->node, json_hash(monitor_id, 0));
+ m->monitor_id = json_clone(monitor_id);
+ shash_init(&m->tables);
+
+ SHASH_FOR_EACH (node, json_object(monitor_requests)) {
+ const struct ovsdb_table *table;
+ struct ovsdb_jsonrpc_monitor_table *mt;
+ size_t allocated_columns;
+ const struct json *mr_value;
+ size_t i;
+
+ table = ovsdb_get_table(s->remote->server->up.db, node->name);
+ if (!table) {
+ error = ovsdb_syntax_error(NULL, NULL,
+ "no table named %s", node->name);
+ goto error;
+ }
+
+ mt = xzalloc(sizeof *mt);
+ mt->table = table;
+ shash_add(&m->tables, table->schema->name, mt);
+
+ /* Parse columns. */
+ mr_value = node->data;
+ allocated_columns = 0;
+ if (mr_value->type == JSON_ARRAY) {
+ const struct json_array *array = &mr_value->u.array;
+
+ for (i = 0; i < array->n; i++) {
+ error = ovsdb_jsonrpc_parse_monitor_request(
+ mt, array->elems[i], &allocated_columns);
+ if (error) {
+ goto error;
+ }
+ }
+ } else {
+ error = ovsdb_jsonrpc_parse_monitor_request(
+ mt, mr_value, &allocated_columns);
+ if (error) {
+ goto error;
+ }
+ }
+
+ /* Check for duplicate columns. */
+ qsort(mt->columns, mt->n_columns, sizeof *mt->columns,
+ compare_ovsdb_jsonrpc_monitor_column);
+ for (i = 1; i < mt->n_columns; i++) {
+ if (mt->columns[i].column == mt->columns[i - 1].column) {
+ error = ovsdb_syntax_error(mr_value, NULL, "column %s "
+ "mentioned more than once",
+ mt->columns[i].column->name);
+ goto error;
+ }
+ }
+ }
+
+ return ovsdb_jsonrpc_monitor_get_initial(m);
+
+error:
+ if (m) {
+ ovsdb_remove_replica(s->remote->server->up.db, &m->replica);
+ }
+
+ json = ovsdb_error_to_json(error);
+ ovsdb_error_destroy(error);
+ return json;
+}
+
+static struct jsonrpc_msg *
+ovsdb_jsonrpc_monitor_cancel(struct ovsdb_jsonrpc_session *s,
+ struct json_array *params,
+ const struct json *request_id)
+{
+ if (params->n != 1) {
+ return jsonrpc_create_error(json_string_create("invalid parameters"),
+ request_id);
+ } else {
+ struct ovsdb_jsonrpc_monitor *m;
+
+ m = ovsdb_jsonrpc_monitor_find(s, params->elems[0]);
+ if (!m) {
+ return jsonrpc_create_error(json_string_create("unknown monitor"),
+ request_id);
+ } else {
+ ovsdb_remove_replica(s->remote->server->up.db, &m->replica);
+ return jsonrpc_create_reply(json_object_create(), request_id);
+ }
+ }
+}
+
+static void
+ovsdb_jsonrpc_monitor_remove_all(struct ovsdb_jsonrpc_session *s)
+{
+ struct ovsdb_jsonrpc_monitor *m, *next;
+
+ HMAP_FOR_EACH_SAFE (m, next, node, &s->monitors) {
+ ovsdb_remove_replica(s->remote->server->up.db, &m->replica);
+ }
+}
+
+static struct ovsdb_jsonrpc_monitor *
+ovsdb_jsonrpc_monitor_cast(struct ovsdb_replica *replica)
+{
+ assert(replica->class == &ovsdb_jsonrpc_replica_class);
+ return CONTAINER_OF(replica, struct ovsdb_jsonrpc_monitor, replica);
+}
+
+struct ovsdb_jsonrpc_monitor_aux {
+ bool initial; /* Sending initial contents of table? */
+ const struct ovsdb_jsonrpc_monitor *monitor;
+ struct json *json; /* JSON for the whole transaction. */
+
+ /* Current table. */
+ struct ovsdb_jsonrpc_monitor_table *mt;
+ struct json *table_json; /* JSON for table's transaction. */
+};
+
+static bool
+any_reportable_change(const struct ovsdb_jsonrpc_monitor_table *mt,
+ const unsigned long int *changed)
+{
+ size_t i;
+
+ for (i = 0; i < mt->n_columns; i++) {
+ const struct ovsdb_jsonrpc_monitor_column *c = &mt->columns[i];
+ unsigned int idx = c->column->index;
+
+ if (c->select & OJMS_MODIFY && bitmap_is_set(changed, idx)) {
+ return true;
+ }
+ }
+
+ return false;
+}
+
+static bool
+ovsdb_jsonrpc_monitor_change_cb(const struct ovsdb_row *old,
+ const struct ovsdb_row *new,
+ const unsigned long int *changed,
+ void *aux_)
+{
+ struct ovsdb_jsonrpc_monitor_aux *aux = aux_;
+ const struct ovsdb_jsonrpc_monitor *m = aux->monitor;
+ struct ovsdb_table *table = new ? new->table : old->table;
+ enum ovsdb_jsonrpc_monitor_selection type;
+ struct json *old_json, *new_json;
+ struct json *row_json;
+ char uuid[UUID_LEN + 1];
+ size_t i;
+
+ if (!aux->mt || table != aux->mt->table) {
+ aux->mt = shash_find_data(&m->tables, table->schema->name);
+ aux->table_json = NULL;
+ if (!aux->mt) {
+ /* We don't care about rows in this table at all. Tell the caller
+ * to skip it. */
+ return false;
+ }
+ }
+
+ type = (aux->initial ? OJMS_INITIAL
+ : !old ? OJMS_INSERT
+ : !new ? OJMS_DELETE
+ : OJMS_MODIFY);
+ if (!(aux->mt->select & type)) {
+ /* We don't care about this type of change (but do want to be called
+ * back for changes to other rows in the same table). */
+ return true;
+ }
+
+ if (type == OJMS_MODIFY && !any_reportable_change(aux->mt, changed)) {
+ /* Nothing of interest changed. */
+ return true;
+ }
+
+ old_json = new_json = NULL;
+ if (type & (OJMS_DELETE | OJMS_MODIFY)) {
+ old_json = json_object_create();
+ }
+ if (type & (OJMS_INITIAL | OJMS_INSERT | OJMS_MODIFY)) {
+ new_json = json_object_create();
+ }
+ for (i = 0; i < aux->mt->n_columns; i++) {
+ const struct ovsdb_jsonrpc_monitor_column *c = &aux->mt->columns[i];
+ const struct ovsdb_column *column = c->column;
+ unsigned int idx = c->column->index;
+
+ 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, idx))
+ || type == OJMS_DELETE) {
+ json_object_put(old_json, column->name,
+ ovsdb_datum_to_json(&old->fields[idx],
+ &column->type));
+ }
+ if (type & (OJMS_INITIAL | OJMS_INSERT | OJMS_MODIFY)) {
+ json_object_put(new_json, column->name,
+ ovsdb_datum_to_json(&new->fields[idx],
+ &column->type));
+ }
+ }
+
+ /* Create JSON object for transaction overall. */
+ if (!aux->json) {
+ aux->json = json_object_create();
+ }
+
+ /* Create JSON object for transaction on this table. */
+ if (!aux->table_json) {
+ aux->table_json = json_object_create();
+ json_object_put(aux->json, aux->mt->table->schema->name,
+ aux->table_json);
+ }
+
+ /* Create JSON object for transaction on this row. */
+ row_json = json_object_create();
+ if (old_json) {
+ json_object_put(row_json, "old", old_json);
+ }
+ if (new_json) {
+ json_object_put(row_json, "new", new_json);
+ }
+
+ /* Add JSON row to JSON table. */
+ snprintf(uuid, sizeof uuid,
+ UUID_FMT, UUID_ARGS(ovsdb_row_get_uuid(new ? new : old)));
+ json_object_put(aux->table_json, uuid, row_json);
+
+ return true;
+}
+
+static void
+ovsdb_jsonrpc_monitor_init_aux(struct ovsdb_jsonrpc_monitor_aux *aux,
+ const struct ovsdb_jsonrpc_monitor *m,
+ bool initial)
+{
+ aux->initial = initial;
+ aux->monitor = m;
+ aux->json = NULL;
+ aux->mt = NULL;
+ aux->table_json = NULL;
+}
+
+static struct ovsdb_error *
+ovsdb_jsonrpc_monitor_commit(struct ovsdb_replica *replica,
+ const struct ovsdb_txn *txn,
+ bool durable OVS_UNUSED)
+{
+ struct ovsdb_jsonrpc_monitor *m = ovsdb_jsonrpc_monitor_cast(replica);
+ struct ovsdb_jsonrpc_monitor_aux aux;
+
+ ovsdb_jsonrpc_monitor_init_aux(&aux, m, false);
+ ovsdb_txn_for_each_change(txn, ovsdb_jsonrpc_monitor_change_cb, &aux);
+ if (aux.json) {
+ struct jsonrpc_msg *msg;
+ struct json *params;
+
+ params = json_array_create_2(json_clone(aux.monitor->monitor_id),
+ aux.json);
+ msg = jsonrpc_create_notify("update", params);
+ jsonrpc_session_send(aux.monitor->session->js, msg);
+ }
+
+ return NULL;
+}
+
+static struct json *
+ovsdb_jsonrpc_monitor_get_initial(const struct ovsdb_jsonrpc_monitor *m)
+{
+ struct ovsdb_jsonrpc_monitor_aux aux;
+ struct shash_node *node;
+
+ ovsdb_jsonrpc_monitor_init_aux(&aux, m, true);
+ SHASH_FOR_EACH (node, &m->tables) {
+ struct ovsdb_jsonrpc_monitor_table *mt = node->data;
+
+ if (mt->select & OJMS_INITIAL) {
+ struct ovsdb_row *row;
+
+ HMAP_FOR_EACH (row, hmap_node, &mt->table->rows) {
+ ovsdb_jsonrpc_monitor_change_cb(NULL, row, NULL, &aux);
+ }
+ }
+ }
+ return aux.json ? aux.json : json_object_create();
+}
+
+static void
+ovsdb_jsonrpc_monitor_destroy(struct ovsdb_replica *replica)
+{
+ struct ovsdb_jsonrpc_monitor *m = ovsdb_jsonrpc_monitor_cast(replica);
+ struct shash_node *node;
+
+ json_destroy(m->monitor_id);
+ SHASH_FOR_EACH (node, &m->tables) {
+ struct ovsdb_jsonrpc_monitor_table *mt = node->data;
+ free(mt->columns);
+ free(mt);
+ }
+ shash_destroy(&m->tables);
+ hmap_remove(&m->session->monitors, &m->node);
+ free(m);
+}
+
+static const struct ovsdb_replica_class ovsdb_jsonrpc_replica_class = {
+ ovsdb_jsonrpc_monitor_commit,
+ ovsdb_jsonrpc_monitor_destroy
+};