#include <assert.h>
#include <errno.h>
+#include "bitmap.h"
#include "column.h"
#include "json.h"
#include "jsonrpc.h"
return server;
}
+void
+ovsdb_jsonrpc_server_destroy(struct ovsdb_jsonrpc_server *svr)
+{
+ struct shash_node *node, *next;
+
+ SHASH_FOR_EACH_SAFE (node, next, &svr->remotes) {
+ ovsdb_jsonrpc_server_del_remote(node);
+ }
+ shash_destroy(&svr->remotes);
+ free(svr);
+}
+
/* Sets 'svr''s current set of remotes to the names in 'new_remotes'. The data
* values in 'new_remotes' are ignored.
*
struct pstream *listener;
int error;
- error = pstream_open(name, &listener);
+ error = jsonrpc_pstream_open(name, &listener);
if (error && error != EAFNOSUPPORT) {
VLOG_ERR_RL(&rl, "%s: listen failed: %s", name, strerror(error));
return;
}
}
+static const char *
+get_db_name(const struct ovsdb_jsonrpc_session *s)
+{
+ return s->remote->server->db->schema->name;
+}
+
+static struct jsonrpc_msg *
+ovsdb_jsonrpc_check_db_name(const struct ovsdb_jsonrpc_session *s,
+ const struct jsonrpc_msg *request)
+{
+ struct json_array *params;
+ const char *want_db_name;
+ const char *have_db_name;
+ struct ovsdb_error *error;
+ struct jsonrpc_msg *reply;
+
+ params = json_array(request->params);
+ if (!params->n || params->elems[0]->type != JSON_STRING) {
+ error = ovsdb_syntax_error(
+ request->params, NULL,
+ "%s request params must begin with <db-name>", request->method);
+ goto error;
+ }
+
+ want_db_name = params->elems[0]->u.string;
+ have_db_name = get_db_name(s);
+ if (strcmp(want_db_name, have_db_name)) {
+ error = ovsdb_syntax_error(
+ request->params, "unknown database",
+ "%s request specifies unknown database %s",
+ request->method, want_db_name);
+ goto error;
+ }
+
+ return NULL;
+
+error:
+ reply = jsonrpc_create_reply(ovsdb_error_to_json(error), request->id);
+ ovsdb_error_destroy(error);
+ return reply;
+}
+
static struct jsonrpc_msg *
execute_transaction(struct ovsdb_jsonrpc_session *s,
struct jsonrpc_msg *request)
struct jsonrpc_msg *reply;
if (!strcmp(request->method, "transact")) {
- reply = execute_transaction(s, request);
+ reply = ovsdb_jsonrpc_check_db_name(s, request);
+ if (!reply) {
+ reply = execute_transaction(s, request);
+ }
} else if (!strcmp(request->method, "monitor")) {
- reply = jsonrpc_create_reply(
- ovsdb_jsonrpc_monitor_create(s, request->params), request->id);
+ reply = ovsdb_jsonrpc_check_db_name(s, request);
+ if (!reply) {
+ reply = jsonrpc_create_reply(
+ ovsdb_jsonrpc_monitor_create(s, request->params), request->id);
+ }
} else if (!strcmp(request->method, "monitor_cancel")) {
reply = ovsdb_jsonrpc_monitor_cancel(s, json_array(request->params),
request->id);
} else if (!strcmp(request->method, "get_schema")) {
+ reply = ovsdb_jsonrpc_check_db_name(s, request);
+ if (!reply) {
+ reply = jsonrpc_create_reply(
+ ovsdb_schema_to_json(s->remote->server->db->schema),
+ request->id);
+ }
+ } else if (!strcmp(request->method, "list_dbs")) {
reply = jsonrpc_create_reply(
- ovsdb_schema_to_json(s->remote->server->db->schema), request->id);
+ json_array_create_1(json_string_create(get_db_name(s))),
+ request->id);
} else if (!strcmp(request->method, "echo")) {
reply = jsonrpc_create_reply(json_clone(request->params), request->id);
} else {
struct shash_node *node;
struct json *json;
- if (json_array(params)->n != 2) {
+ if (json_array(params)->n != 3) {
error = ovsdb_syntax_error(params, NULL, "invalid parameters");
goto error;
}
- monitor_id = params->u.array.elems[0];
- monitor_requests = params->u.array.elems[1];
+ 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");
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_;
for (i = 0; i < aux->mt->columns.n_columns; i++) {
const struct ovsdb_column *column = aux->mt->columns.columns[i];
unsigned int idx = column->index;
- bool changed = false;
+ bool column_changed = false;
if (type == OJMS_MODIFY) {
- changed = !ovsdb_datum_equals(&old->fields[idx],
- &new->fields[idx], &column->type);
- n_changed += changed;
+ column_changed = bitmap_is_set(changed, idx);
+ n_changed += column_changed;
}
- if (changed || type == OJMS_DELETE) {
+ if (column_changed || type == OJMS_DELETE) {
if (!old_json) {
old_json = json_object_create();
}
static struct ovsdb_error *
ovsdb_jsonrpc_monitor_commit(struct ovsdb_replica *replica,
- const struct ovsdb_txn *txn, bool durable UNUSED)
+ 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;
HMAP_FOR_EACH (row, struct ovsdb_row, hmap_node,
&mt->table->rows) {
- ovsdb_jsonrpc_monitor_change_cb(NULL, row, &aux);
+ ovsdb_jsonrpc_monitor_change_cb(NULL, row, NULL, &aux);
}
}
}