#include "bitmap.h"
#include "column.h"
-#include "dynamic-string.h"
+#include "openvswitch/dynamic-string.h"
+#include "monitor.h"
#include "json.h"
#include "jsonrpc.h"
#include "ovsdb-error.h"
#include "ovsdb-parser.h"
#include "ovsdb.h"
+#include "condition.h"
#include "poll-loop.h"
#include "reconnect.h"
#include "row.h"
struct ovsdb_jsonrpc_remote;
struct ovsdb_jsonrpc_session;
+/* Set false to defeature monitor_cond, causing jsonrpc to respond to
+ * monitor_cond method with an error. */
+static bool monitor_cond_enable__ = true;
+
/* Message rate-limiting. */
static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
static void ovsdb_jsonrpc_session_reconnect_all(struct ovsdb_jsonrpc_remote *);
static void ovsdb_jsonrpc_session_set_all_options(
struct ovsdb_jsonrpc_remote *, const struct ovsdb_jsonrpc_options *);
-static bool ovsdb_jsonrpc_session_get_status(
+static bool ovsdb_jsonrpc_active_session_get_status(
const struct ovsdb_jsonrpc_remote *,
struct ovsdb_jsonrpc_remote_status *);
+static void ovsdb_jsonrpc_session_get_status(
+ const struct ovsdb_jsonrpc_session *,
+ struct ovsdb_jsonrpc_remote_status *);
static void ovsdb_jsonrpc_session_unlock_all(struct ovsdb_jsonrpc_session *);
static void ovsdb_jsonrpc_session_unlock__(struct ovsdb_lock_waiter *);
static void ovsdb_jsonrpc_session_send(struct ovsdb_jsonrpc_session *,
/* Monitors. */
static struct jsonrpc_msg *ovsdb_jsonrpc_monitor_create(
struct ovsdb_jsonrpc_session *, struct ovsdb *, struct json *params,
+ enum ovsdb_monitor_version, const struct json *request_id);
+static struct jsonrpc_msg *ovsdb_jsonrpc_monitor_cond_change(
+ struct ovsdb_jsonrpc_session *s,
+ struct json *params,
const struct json *request_id);
static struct jsonrpc_msg *ovsdb_jsonrpc_monitor_cancel(
struct ovsdb_jsonrpc_session *,
static void ovsdb_jsonrpc_monitor_remove_all(struct ovsdb_jsonrpc_session *);
static void ovsdb_jsonrpc_monitor_flush_all(struct ovsdb_jsonrpc_session *);
static bool ovsdb_jsonrpc_monitor_needs_flush(struct ovsdb_jsonrpc_session *);
+static struct json *ovsdb_jsonrpc_monitor_compose_update(
+ struct ovsdb_jsonrpc_monitor *monitor, bool initial);
+static struct jsonrpc_msg * ovsdb_jsonrpc_create_notify(
+ const struct ovsdb_jsonrpc_monitor *m,
+ struct json *params);
+
\f
/* JSON-RPC database server. */
struct ovsdb_jsonrpc_server {
struct ovsdb_server up;
- unsigned int n_sessions, max_sessions;
+ unsigned int n_sessions;
struct shash remotes; /* Contains "struct ovsdb_jsonrpc_remote *"s. */
};
{
struct ovsdb_jsonrpc_server *server = xzalloc(sizeof *server);
ovsdb_server_init(&server->up);
- server->max_sessions = 330; /* Random limit. */
shash_init(&server->remotes);
return server;
}
remote = xmalloc(sizeof *remote);
remote->server = svr;
remote->listener = listener;
- list_init(&remote->sessions);
+ ovs_list_init(&remote->sessions);
remote->dscp = options->dscp;
shash_add(&svr->remotes, name, remote);
/* Stores status information for the remote named 'target', which should have
* been configured on 'svr' with a call to ovsdb_jsonrpc_server_set_remotes(),
* into '*status'. On success returns true, on failure (if 'svr' doesn't have
- * a remote named 'target' or if that remote is an inbound remote that has no
+ * a remote named 'target' or if that remote is an outbound remote that has no
* active connections) returns false. On failure, 'status' will be zeroed.
*/
bool
memset(status, 0, sizeof *status);
remote = shash_find_data(&svr->remotes, target);
- return remote && ovsdb_jsonrpc_session_get_status(remote, status);
+
+ if (!remote) {
+ return false;
+ }
+
+ if (remote->listener) {
+ status->bound_port = pstream_get_bound_port(remote->listener);
+ status->is_connected = !ovs_list_is_empty(&remote->sessions);
+ status->n_connections = ovs_list_size(&remote->sessions);
+ return true;
+ }
+
+ return ovsdb_jsonrpc_active_session_get_status(remote, status);
}
void
struct ovsdb_jsonrpc_remote *remote = node->data;
if (remote->listener) {
- if (svr->n_sessions < svr->max_sessions) {
- struct stream *stream;
- int error;
-
- error = pstream_accept(remote->listener, &stream);
- if (!error) {
- struct jsonrpc_session *js;
- js = jsonrpc_session_open_unreliably(jsonrpc_open(stream),
- remote->dscp);
- ovsdb_jsonrpc_session_create(remote, js);
- } else if (error != EAGAIN) {
- VLOG_WARN_RL(&rl, "%s: accept failed: %s",
- pstream_get_name(remote->listener),
- ovs_strerror(error));
- }
- } else {
- VLOG_WARN_RL(&rl, "%s: connection exceeded maximum (%d)",
+ struct stream *stream;
+ int error;
+
+ error = pstream_accept(remote->listener, &stream);
+ if (!error) {
+ struct jsonrpc_session *js;
+ js = jsonrpc_session_open_unreliably(jsonrpc_open(stream),
+ remote->dscp);
+ ovsdb_jsonrpc_session_create(remote, js);
+ } else if (error != EAGAIN) {
+ VLOG_WARN_RL(&rl, "%s: accept failed: %s",
pstream_get_name(remote->listener),
- svr->max_sessions);
+ ovs_strerror(error));
}
}
SHASH_FOR_EACH (node, &svr->remotes) {
struct ovsdb_jsonrpc_remote *remote = node->data;
- if (remote->listener && svr->n_sessions < svr->max_sessions) {
+ if (remote->listener) {
pstream_wait(remote->listener);
}
static void ovsdb_jsonrpc_session_get_memory_usage(
const struct ovsdb_jsonrpc_session *, struct simap *usage);
static void ovsdb_jsonrpc_session_got_request(struct ovsdb_jsonrpc_session *,
- struct jsonrpc_msg *);
+ struct jsonrpc_msg *);
static void ovsdb_jsonrpc_session_got_notify(struct ovsdb_jsonrpc_session *,
struct jsonrpc_msg *);
s = xzalloc(sizeof *s);
ovsdb_session_init(&s->up, &remote->server->up);
s->remote = remote;
- list_push_back(&remote->sessions, &s->node);
+ ovs_list_push_back(&remote->sessions, &s->node);
hmap_init(&s->triggers);
hmap_init(&s->monitors);
s->js = js;
hmap_destroy(&s->triggers);
jsonrpc_session_close(s->js);
- list_remove(&s->node);
+ ovs_list_remove(&s->node);
s->remote->server->n_sessions--;
ovsdb_session_destroy(&s->up);
free(s);
struct simap *usage)
{
simap_increase(usage, "triggers", hmap_count(&s->triggers));
- simap_increase(usage, "monitors", hmap_count(&s->monitors));
simap_increase(usage, "backlog", jsonrpc_session_get_backlog(s->js));
}
}
}
+/* Sets the 'status' of for the 'remote' with an outgoing connection. */
static bool
-ovsdb_jsonrpc_session_get_status(const struct ovsdb_jsonrpc_remote *remote,
- struct ovsdb_jsonrpc_remote_status *status)
+ovsdb_jsonrpc_active_session_get_status(
+ const struct ovsdb_jsonrpc_remote *remote,
+ struct ovsdb_jsonrpc_remote_status *status)
{
+ const struct ovs_list *sessions = &remote->sessions;
const struct ovsdb_jsonrpc_session *s;
+
+ if (ovs_list_is_empty(sessions)) {
+ return false;
+ }
+
+ ovs_assert(ovs_list_is_singleton(sessions));
+ s = CONTAINER_OF(ovs_list_front(sessions), struct ovsdb_jsonrpc_session, node);
+ ovsdb_jsonrpc_session_get_status(s, status);
+ status->n_connections = 1;
+
+ return true;
+}
+
+static void
+ovsdb_jsonrpc_session_get_status(const struct ovsdb_jsonrpc_session *session,
+ struct ovsdb_jsonrpc_remote_status *status)
+{
+ const struct ovsdb_jsonrpc_session *s = session;
const struct jsonrpc_session *js;
struct ovsdb_lock_waiter *waiter;
struct reconnect_stats rstats;
struct ds locks_held, locks_waiting, locks_lost;
- status->bound_port = (remote->listener
- ? pstream_get_bound_port(remote->listener)
- : htons(0));
-
- if (list_is_empty(&remote->sessions)) {
- return false;
- }
- s = CONTAINER_OF(remote->sessions.next, struct ovsdb_jsonrpc_session, node);
js = s->js;
status->is_connected = jsonrpc_session_is_connected(js);
status->locks_held = ds_steal_cstr(&locks_held);
status->locks_waiting = ds_steal_cstr(&locks_waiting);
status->locks_lost = ds_steal_cstr(&locks_lost);
-
- status->n_connections = list_size(&remote->sessions);
-
- return true;
}
/* Examines 'request' to determine the database to which it relates, and then
if (!reply) {
reply = execute_transaction(s, db, request);
}
- } else if (!strcmp(request->method, "monitor")) {
+ } else if (!strcmp(request->method, "monitor") ||
+ (monitor_cond_enable__ && !strcmp(request->method,
+ "monitor_cond"))) {
struct ovsdb *db = ovsdb_jsonrpc_lookup_db(s, request, &reply);
if (!reply) {
+ int l = strlen(request->method) - strlen("monitor");
+ enum ovsdb_monitor_version version = l ? OVSDB_MONITOR_V2
+ : OVSDB_MONITOR_V1;
reply = ovsdb_jsonrpc_monitor_create(s, db, request->params,
- request->id);
+ version, request->id);
}
+ } else if (!strcmp(request->method, "monitor_cond_change")) {
+ reply = ovsdb_jsonrpc_monitor_cond_change(s, request->params,
+ request->id);
} else if (!strcmp(request->method, "monitor_cancel")) {
reply = ovsdb_jsonrpc_monitor_cancel(s, json_array(request->params),
request->id);
static void
ovsdb_jsonrpc_trigger_complete_done(struct ovsdb_jsonrpc_session *s)
{
- while (!list_is_empty(&s->up.completions)) {
+ while (!ovs_list_is_empty(&s->up.completions)) {
struct ovsdb_jsonrpc_trigger *t
= CONTAINER_OF(s->up.completions.next,
struct ovsdb_jsonrpc_trigger, trigger.node);
}
}
\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 row that has changed in a monitored table. */
-struct ovsdb_jsonrpc_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_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;
-
- /* Contains 'struct ovsdb_jsonrpc_monitor_row's for rows that have been
- * updated but not yet flushed to the jsonrpc connection. */
- struct hmap changes;
-};
-
-/* A collection of tables being monitored. */
+/* Jsonrpc front end monitor. */
struct ovsdb_jsonrpc_monitor {
- struct ovsdb_replica replica;
struct ovsdb_jsonrpc_session *session;
struct ovsdb *db;
struct hmap_node node; /* In ovsdb_jsonrpc_session's "monitors". */
-
struct json *monitor_id;
- struct shash tables; /* Holds "struct ovsdb_jsonrpc_monitor_table"s. */
+ struct ovsdb_monitor *dbmon;
+ uint64_t unflushed; /* The first transaction that has not been
+ flushed to the jsonrpc remote client. */
+ enum ovsdb_monitor_version version;
+ struct ovsdb_monitor_session_condition *condition;/* Session's condition */
};
-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 *
+static struct ovsdb_jsonrpc_monitor *
ovsdb_jsonrpc_monitor_find(struct ovsdb_jsonrpc_session *s,
const struct json *monitor_id)
{
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_)
+static bool
+parse_bool(struct ovsdb_parser *parser, const char *name, bool default_value)
{
- const struct ovsdb_jsonrpc_monitor_column *a = a_;
- const struct ovsdb_jsonrpc_monitor_column *b = b_;
+ const struct json *json;
- return a->column < b->column ? -1 : a->column > b->column;
+ 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
-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;
+ovsdb_jsonrpc_parse_monitor_request(
+ struct ovsdb_monitor *dbmon,
+ const struct ovsdb_table *table,
+ struct ovsdb_monitor_session_condition *cond,
+ const struct json *monitor_request)
+{
+ const struct ovsdb_table_schema *ts = table->schema;
+ enum ovsdb_monitor_selection select;
+ const struct json *columns, *select_json, *where = NULL;
struct ovsdb_parser parser;
struct ovsdb_error *error;
ovsdb_parser_init(&parser, monitor_request, "table %s", ts->name);
+ if (cond) {
+ where = ovsdb_parser_member(&parser, "where", OP_ARRAY | OP_OPTIONAL);
+ }
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;
} else {
select = OJMS_INITIAL | OJMS_INSERT | OJMS_DELETE | OJMS_MODIFY;
}
- mt->select |= select;
+ ovsdb_monitor_table_add_select(dbmon, table, select);
if (columns) {
size_t i;
}
s = columns->u.array.elems[i]->u.string;
- column = shash_find_data(&mt->table->schema->columns, s);
+ column = shash_find_data(&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);
+ if (ovsdb_monitor_add_column(dbmon, table, column,
+ select, true)) {
+ return ovsdb_syntax_error(columns, NULL, "column %s "
+ "mentioned more than once",
+ column->name);
+ }
}
} 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);
+ if (ovsdb_monitor_add_column(dbmon, table, column,
+ select, true)) {
+ return ovsdb_syntax_error(columns, NULL, "column %s "
+ "mentioned more than once",
+ column->name);
+ }
}
}
}
+ if (cond) {
+ error = ovsdb_monitor_table_condition_create(cond, table, where);
+ if (error) {
+ return error;
+ }
+ }
return NULL;
}
static struct jsonrpc_msg *
ovsdb_jsonrpc_monitor_create(struct ovsdb_jsonrpc_session *s, struct ovsdb *db,
struct json *params,
+ enum ovsdb_monitor_version version,
const struct json *request_id)
{
struct ovsdb_jsonrpc_monitor *m = NULL;
+ struct ovsdb_monitor *dbmon = NULL;
struct json *monitor_id, *monitor_requests;
struct ovsdb_error *error = NULL;
struct shash_node *node;
}
m = xzalloc(sizeof *m);
- ovsdb_replica_init(&m->replica, &ovsdb_jsonrpc_replica_class);
- ovsdb_add_replica(db, &m->replica);
m->session = s;
m->db = db;
+ m->dbmon = ovsdb_monitor_create(db, m);
+ if (version == OVSDB_MONITOR_V2) {
+ m->condition = ovsdb_monitor_session_condition_create();
+ }
+ m->unflushed = 0;
+ m->version = version;
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;
goto error;
}
- mt = xzalloc(sizeof *mt);
- mt->table = table;
- hmap_init(&mt->changes);
- shash_add(&m->tables, table->schema->name, mt);
+ ovsdb_monitor_add_table(m->dbmon, table);
/* 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);
+ error = ovsdb_jsonrpc_parse_monitor_request(m->dbmon,
+ table,
+ m->condition,
+ array->elems[i]);
if (error) {
goto error;
}
}
} else {
- error = ovsdb_jsonrpc_parse_monitor_request(
- mt, mr_value, &allocated_columns);
+ error = ovsdb_jsonrpc_parse_monitor_request(m->dbmon,
+ table,
+ m->condition,
+ mr_value);
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;
- }
- }
+ dbmon = ovsdb_monitor_add(m->dbmon);
+ if (dbmon != m->dbmon) {
+ /* Found an exisiting dbmon, reuse the current one. */
+ ovsdb_monitor_remove_jsonrpc_monitor(m->dbmon, m, m->unflushed);
+ ovsdb_monitor_add_jsonrpc_monitor(dbmon, m);
+ m->dbmon = dbmon;
}
- return jsonrpc_create_reply(ovsdb_jsonrpc_monitor_get_initial(m),
- request_id);
+ /* Only now we can bind session's condition to ovsdb_monitor */
+ if (m->condition) {
+ ovsdb_monitor_condition_bind(m->dbmon, m->condition);
+ }
+
+ ovsdb_monitor_get_initial(m->dbmon);
+ json = ovsdb_jsonrpc_monitor_compose_update(m, true);
+ json = json ? json : json_object_create();
+ return jsonrpc_create_reply(json, request_id);
error:
if (m) {
- ovsdb_remove_replica(m->db, &m->replica);
+ ovsdb_jsonrpc_monitor_destroy(m);
}
json = ovsdb_error_to_json(error);
return jsonrpc_create_error(json, request_id);
}
-static struct jsonrpc_msg *
-ovsdb_jsonrpc_monitor_cancel(struct ovsdb_jsonrpc_session *s,
- struct json_array *params,
- const struct json *request_id)
+static struct ovsdb_error *
+ovsdb_jsonrpc_parse_monitor_cond_change_request(
+ struct ovsdb_jsonrpc_monitor *m,
+ const struct ovsdb_table *table,
+ const struct json *cond_change_req)
{
- 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(m->db, &m->replica);
- return jsonrpc_create_reply(json_object_create(), request_id);
- }
- }
-}
+ const struct ovsdb_table_schema *ts = table->schema;
+ const struct json *condition, *columns;
+ struct ovsdb_parser parser;
+ struct ovsdb_error *error;
-static void
-ovsdb_jsonrpc_monitor_remove_all(struct ovsdb_jsonrpc_session *s)
-{
- struct ovsdb_jsonrpc_monitor *m, *next;
+ ovsdb_parser_init(&parser, cond_change_req, "table %s", ts->name);
+ columns = ovsdb_parser_member(&parser, "columns", OP_ARRAY | OP_OPTIONAL);
+ condition = ovsdb_parser_member(&parser, "where", OP_ARRAY | OP_OPTIONAL);
- HMAP_FOR_EACH_SAFE (m, next, node, &s->monitors) {
- ovsdb_remove_replica(m->db, &m->replica);
+ error = ovsdb_parser_finish(&parser);
+ if (error) {
+ return error;
}
-}
-
-static struct ovsdb_jsonrpc_monitor *
-ovsdb_jsonrpc_monitor_cast(struct ovsdb_replica *replica)
-{
- ovs_assert(replica->class == &ovsdb_jsonrpc_replica_class);
- return CONTAINER_OF(replica, struct ovsdb_jsonrpc_monitor, replica);
-}
-struct ovsdb_jsonrpc_monitor_aux {
- const struct ovsdb_jsonrpc_monitor *monitor;
- struct ovsdb_jsonrpc_monitor_table *mt;
-};
-
-/* Finds and returns the ovsdb_jsonrpc_monitor_row in 'mt->changes' for the
- * given 'uuid', or NULL if there is no such row. */
-static struct ovsdb_jsonrpc_monitor_row *
-ovsdb_jsonrpc_monitor_row_find(const struct ovsdb_jsonrpc_monitor_table *mt,
- const struct uuid *uuid)
-{
- struct ovsdb_jsonrpc_monitor_row *row;
-
- HMAP_FOR_EACH_WITH_HASH (row, hmap_node, uuid_hash(uuid), &mt->changes) {
- if (uuid_equals(uuid, &row->uuid)) {
- return row;
- }
+ if (columns) {
+ error = ovsdb_syntax_error(cond_change_req, NULL, "changing columns "
+ "is unsupported");
+ return error;
}
- return NULL;
+ error = ovsdb_monitor_table_condition_update(m->dbmon, m->condition, table,
+ condition);
+
+ return error;
}
-/* 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_jsonrpc_monitor_table *mt,
- const struct ovsdb_row *row)
+static struct jsonrpc_msg *
+ovsdb_jsonrpc_monitor_cond_change(struct ovsdb_jsonrpc_session *s,
+ struct json *params,
+ const struct json *request_id)
{
- struct ovsdb_datum *data;
- size_t i;
+ struct ovsdb_error *error;
+ struct ovsdb_jsonrpc_monitor *m;
+ struct json *monitor_cond_change_reqs;
+ struct shash_node *node;
+ struct json *json;
- if (!row) {
- return NULL;
+ if (json_array(params)->n != 3) {
+ error = ovsdb_syntax_error(params, NULL, "invalid parameters");
+ goto error;
}
- 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);
+ m = ovsdb_jsonrpc_monitor_find(s, params->u.array.elems[0]);
+ if (!m) {
+ error = ovsdb_syntax_error(request_id, NULL,
+ "unknown monitor session");
+ goto error;
}
- 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_jsonrpc_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);
- }
+ monitor_cond_change_reqs = params->u.array.elems[2];
+ if (monitor_cond_change_reqs->type != JSON_OBJECT) {
+ error =
+ ovsdb_syntax_error(NULL, NULL,
+ "monitor-cond-change-requests must be object");
+ goto error;
}
-}
-/* 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_jsonrpc_monitor_table *mt,
- struct ovsdb_datum *data)
-{
- if (data) {
+ SHASH_FOR_EACH (node, json_object(monitor_cond_change_reqs)) {
+ const struct ovsdb_table *table;
+ const struct json *mr_value;
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);
+ table = ovsdb_get_table(m->db, node->name);
+ if (!table) {
+ error = ovsdb_syntax_error(NULL, NULL,
+ "no table named %s", node->name);
+ goto error;
}
- free(data);
- }
-}
-
-/* Frees 'row', which must have been created from 'mt'. */
-static void
-ovsdb_jsonrpc_monitor_row_destroy(const struct ovsdb_jsonrpc_monitor_table *mt,
- struct ovsdb_jsonrpc_monitor_row *row)
-{
- if (row) {
- free_monitor_row_data(mt, row->old);
- free_monitor_row_data(mt, row->new);
- free(row);
- }
-}
-
-static bool
-ovsdb_jsonrpc_monitor_change_cb(const struct ovsdb_row *old,
- const struct ovsdb_row *new,
- const unsigned long int *changed OVS_UNUSED,
- 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;
- const struct uuid *uuid = ovsdb_row_get_uuid(new ? new : old);
- struct ovsdb_jsonrpc_monitor_row *change;
- struct ovsdb_jsonrpc_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;
+ if (!ovsdb_monitor_table_exists(m->dbmon, table)) {
+ error = ovsdb_syntax_error(NULL, NULL,
+ "no table named %s in monitor session",
+ node->name);
+ goto error;
}
- }
- mt = aux->mt;
- change = ovsdb_jsonrpc_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;
+ mr_value = node->data;
+ if (mr_value->type == JSON_ARRAY) {
+ const struct json_array *array = &mr_value->u.array;
- if (!change->old) {
- /* This row was added then deleted. Forget about it. */
- hmap_remove(&mt->changes, &change->hmap_node);
- free(change);
+ for (i = 0; i < array->n; i++) {
+ error = ovsdb_jsonrpc_parse_monitor_cond_change_request(
+ m, table, array->elems[i]);
+ if (error) {
+ goto error;
+ }
}
+ } else {
+ error = ovsdb_syntax_error(
+ NULL, NULL,
+ "table %s no monitor-cond-change JSON array",
+ node->name);
+ goto error;
}
}
- 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_jsonrpc_monitor_compose_row_update(
- const struct ovsdb_jsonrpc_monitor_table *mt,
- const struct ovsdb_jsonrpc_monitor_row *row,
- bool initial, unsigned long int *changed)
-{
- enum ovsdb_jsonrpc_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;
- }
+ /* Change monitor id */
+ hmap_remove(&s->monitors, &m->node);
+ json_destroy(m->monitor_id);
+ m->monitor_id = json_clone(params->u.array.elems[1]);
+ hmap_insert(&s->monitors, &m->node, json_hash(m->monitor_id, 0));
- if (type == OJMS_MODIFY) {
- size_t n_changes;
+ /* Send the new update, if any, represents the difference from the old
+ * condition and the new one. */
+ struct json *update_json;
- 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;
- }
- }
+ update_json = ovsdb_monitor_get_update(m->dbmon, false, true,
+ &m->unflushed, m->condition, m->version);
+ if (update_json) {
+ struct jsonrpc_msg *msg;
+ struct json *params;
- 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);
+ params = json_array_create_2(json_clone(m->monitor_id), update_json);
+ msg = ovsdb_jsonrpc_create_notify(m, params);
+ jsonrpc_session_send(s->js, msg);
}
- for (i = 0; i < mt->n_columns; i++) {
- const struct ovsdb_jsonrpc_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;
- }
+ return jsonrpc_create_reply(json_object_create(), request_id);
- 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));
- }
- }
+error:
- return row_json;
+ json = ovsdb_error_to_json(error);
+ ovsdb_error_destroy(error);
+ return jsonrpc_create_error(json, request_id);
}
-/* 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_jsonrpc_monitor_compose_table_update(
- const struct ovsdb_jsonrpc_monitor *monitor, bool initial)
+static struct jsonrpc_msg *
+ovsdb_jsonrpc_monitor_cancel(struct ovsdb_jsonrpc_session *s,
+ struct json_array *params,
+ const struct json *request_id)
{
- struct shash_node *node;
- unsigned long int *changed;
- struct json *json;
- size_t max_columns;
-
- max_columns = 0;
- SHASH_FOR_EACH (node, &monitor->tables) {
- struct ovsdb_jsonrpc_monitor_table *mt = node->data;
+ if (params->n != 1) {
+ return jsonrpc_create_error(json_string_create("invalid parameters"),
+ request_id);
+ } else {
+ struct ovsdb_jsonrpc_monitor *m;
- max_columns = MAX(max_columns, mt->n_columns);
+ m = ovsdb_jsonrpc_monitor_find(s, params->elems[0]);
+ if (!m) {
+ return jsonrpc_create_error(json_string_create("unknown monitor"),
+ request_id);
+ } else {
+ ovsdb_jsonrpc_monitor_destroy(m);
+ return jsonrpc_create_reply(json_object_create(), request_id);
+ }
}
- changed = xmalloc(bitmap_n_bytes(max_columns));
-
- json = NULL;
- SHASH_FOR_EACH (node, &monitor->tables) {
- struct ovsdb_jsonrpc_monitor_table *mt = node->data;
- struct ovsdb_jsonrpc_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_jsonrpc_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();
- }
+static void
+ovsdb_jsonrpc_monitor_remove_all(struct ovsdb_jsonrpc_session *s)
+{
+ struct ovsdb_jsonrpc_monitor *m, *next;
- /* 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);
- }
+ HMAP_FOR_EACH_SAFE (m, next, node, &s->monitors) {
+ ovsdb_jsonrpc_monitor_destroy(m);
+ }
+}
- /* Add JSON row to JSON table. */
- snprintf(uuid, sizeof uuid, UUID_FMT, UUID_ARGS(&row->uuid));
- json_object_put(table_json, uuid, row_json);
- }
+static struct json *
+ovsdb_jsonrpc_monitor_compose_update(struct ovsdb_jsonrpc_monitor *m,
+ bool initial)
+{
- hmap_remove(&mt->changes, &row->hmap_node);
- ovsdb_jsonrpc_monitor_row_destroy(mt, row);
- }
+ if (!ovsdb_monitor_needs_flush(m->dbmon, m->unflushed)) {
+ return NULL;
}
- free(changed);
-
- return json;
+ return ovsdb_monitor_get_update(m->dbmon, initial, false,
+ &m->unflushed, m->condition, m->version);
}
static bool
struct ovsdb_jsonrpc_monitor *m;
HMAP_FOR_EACH (m, node, &s->monitors) {
- struct shash_node *node;
-
- SHASH_FOR_EACH (node, &m->tables) {
- struct ovsdb_jsonrpc_monitor_table *mt = node->data;
-
- if (!hmap_is_empty(&mt->changes)) {
- return true;
- }
+ if (ovsdb_monitor_needs_flush(m->dbmon, m->unflushed)) {
+ return true;
}
}
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_remove_jsonrpc_monitor(m->dbmon, m, m->unflushed);
+ ovsdb_monitor_session_condition_destroy(m->condition);
+ free(m);
+}
+
+static struct jsonrpc_msg *
+ovsdb_jsonrpc_create_notify(const struct ovsdb_jsonrpc_monitor *m,
+ struct json *params)
+{
+ const char *method;
+
+ switch(m->version) {
+ case OVSDB_MONITOR_V1:
+ method = "update";
+ break;
+ case OVSDB_MONITOR_V2:
+ method = "update2";
+ break;
+ case OVSDB_MONITOR_VERSION_MAX:
+ default:
+ OVS_NOT_REACHED();
+ }
+
+ return jsonrpc_create_notify(method, params);
+}
+
static void
ovsdb_jsonrpc_monitor_flush_all(struct ovsdb_jsonrpc_session *s)
{
HMAP_FOR_EACH (m, node, &s->monitors) {
struct json *json;
- json = ovsdb_jsonrpc_monitor_compose_table_update(m, false);
+ json = ovsdb_jsonrpc_monitor_compose_update(m, false);
if (json) {
struct jsonrpc_msg *msg;
struct json *params;
params = json_array_create_2(json_clone(m->monitor_id), json);
- msg = jsonrpc_create_notify("update", params);
+ msg = ovsdb_jsonrpc_create_notify(m, params);
jsonrpc_session_send(s->js, msg);
}
}
}
-static void
-ovsdb_jsonrpc_monitor_init_aux(struct ovsdb_jsonrpc_monitor_aux *aux,
- const struct ovsdb_jsonrpc_monitor *m)
-{
- aux->monitor = m;
- aux->mt = 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);
- ovsdb_txn_for_each_change(txn, ovsdb_jsonrpc_monitor_change_cb, &aux);
-
- 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;
- struct json *json;
-
- ovsdb_jsonrpc_monitor_init_aux(&aux, m);
- 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);
- }
- }
- }
- json = ovsdb_jsonrpc_monitor_compose_table_update(m, true);
- return json ? json : json_object_create();
-}
-
-static void
-ovsdb_jsonrpc_monitor_destroy(struct ovsdb_replica *replica)
+void
+ovsdb_jsonrpc_disable_monitor_cond(void)
{
- 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;
- struct ovsdb_jsonrpc_monitor_row *row, *next;
-
- HMAP_FOR_EACH_SAFE (row, next, hmap_node, &mt->changes) {
- hmap_remove(&mt->changes, &row->hmap_node);
- ovsdb_jsonrpc_monitor_row_destroy(mt, row);
- }
- hmap_destroy(&mt->changes);
-
- free(mt->columns);
- free(mt);
- }
- shash_destroy(&m->tables);
- hmap_remove(&m->session->monitors, &m->node);
- free(m);
+ /* Once disabled, it is not possible to re-enable it. */
+ monitor_cond_enable__ = false;
}
-
-static const struct ovsdb_replica_class ovsdb_jsonrpc_replica_class = {
- ovsdb_jsonrpc_monitor_commit,
- ovsdb_jsonrpc_monitor_destroy
-};