2010-01-04 10:05:51 -08:00
|
|
|
|
/* Copyright (c) 2009, 2010 Nicira Networks
|
2009-11-04 15:11:44 -08:00
|
|
|
|
*
|
|
|
|
|
* 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 "jsonrpc-server.h"
|
|
|
|
|
|
2009-12-21 10:38:26 -08:00
|
|
|
|
#include <assert.h>
|
2009-11-04 15:11:44 -08:00
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
2010-03-11 17:14:31 -08:00
|
|
|
|
#include "bitmap.h"
|
2009-11-13 14:47:25 -08:00
|
|
|
|
#include "column.h"
|
2009-11-04 15:11:44 -08:00
|
|
|
|
#include "json.h"
|
|
|
|
|
#include "jsonrpc.h"
|
2009-11-16 10:38:14 -08:00
|
|
|
|
#include "ovsdb-error.h"
|
|
|
|
|
#include "ovsdb-parser.h"
|
2009-11-04 15:11:44 -08:00
|
|
|
|
#include "ovsdb.h"
|
2009-11-12 12:58:53 -08:00
|
|
|
|
#include "reconnect.h"
|
2009-11-16 10:38:14 -08:00
|
|
|
|
#include "row.h"
|
2009-11-04 15:11:44 -08:00
|
|
|
|
#include "stream.h"
|
2009-11-16 10:38:14 -08:00
|
|
|
|
#include "table.h"
|
2009-11-04 15:11:44 -08:00
|
|
|
|
#include "timeval.h"
|
2009-11-16 10:38:14 -08:00
|
|
|
|
#include "transaction.h"
|
2009-11-04 15:11:44 -08:00
|
|
|
|
#include "trigger.h"
|
|
|
|
|
#include "vlog.h"
|
|
|
|
|
|
2010-07-16 11:02:49 -07:00
|
|
|
|
VLOG_DEFINE_THIS_MODULE(ovsdb_jsonrpc_server)
|
|
|
|
|
|
2010-01-04 10:05:51 -08:00
|
|
|
|
struct ovsdb_jsonrpc_remote;
|
2009-11-13 14:47:25 -08:00
|
|
|
|
struct ovsdb_jsonrpc_session;
|
2009-11-04 15:11:44 -08:00
|
|
|
|
|
2010-01-04 10:05:51 -08:00
|
|
|
|
/* Message rate-limiting. */
|
|
|
|
|
struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
|
|
|
|
|
|
2009-11-16 10:38:14 -08:00
|
|
|
|
/* Sessions. */
|
2010-01-04 10:05:51 -08:00
|
|
|
|
static struct ovsdb_jsonrpc_session *ovsdb_jsonrpc_session_create(
|
|
|
|
|
struct ovsdb_jsonrpc_remote *, struct jsonrpc_session *);
|
|
|
|
|
static void ovsdb_jsonrpc_session_run_all(struct ovsdb_jsonrpc_remote *);
|
|
|
|
|
static void ovsdb_jsonrpc_session_wait_all(struct ovsdb_jsonrpc_remote *);
|
|
|
|
|
static void ovsdb_jsonrpc_session_close_all(struct ovsdb_jsonrpc_remote *);
|
2010-06-24 12:56:30 -07:00
|
|
|
|
static void ovsdb_jsonrpc_session_reconnect_all(struct ovsdb_jsonrpc_remote *);
|
2009-11-13 14:47:25 -08:00
|
|
|
|
|
2009-11-16 10:38:14 -08:00
|
|
|
|
/* Triggers. */
|
2009-11-13 14:47:25 -08:00
|
|
|
|
static void ovsdb_jsonrpc_trigger_create(struct ovsdb_jsonrpc_session *,
|
|
|
|
|
struct json *id, struct json *params);
|
|
|
|
|
static struct ovsdb_jsonrpc_trigger *ovsdb_jsonrpc_trigger_find(
|
|
|
|
|
struct ovsdb_jsonrpc_session *, const struct json *id, size_t hash);
|
|
|
|
|
static void ovsdb_jsonrpc_trigger_complete(struct ovsdb_jsonrpc_trigger *);
|
|
|
|
|
static void ovsdb_jsonrpc_trigger_complete_all(struct ovsdb_jsonrpc_session *);
|
|
|
|
|
static void ovsdb_jsonrpc_trigger_complete_done(
|
|
|
|
|
struct ovsdb_jsonrpc_session *);
|
2009-11-16 10:38:14 -08:00
|
|
|
|
|
|
|
|
|
/* Monitors. */
|
|
|
|
|
static struct json *ovsdb_jsonrpc_monitor_create(
|
|
|
|
|
struct ovsdb_jsonrpc_session *, struct json *params);
|
|
|
|
|
static struct jsonrpc_msg *ovsdb_jsonrpc_monitor_cancel(
|
|
|
|
|
struct ovsdb_jsonrpc_session *,
|
|
|
|
|
struct json_array *params,
|
|
|
|
|
const struct json *request_id);
|
|
|
|
|
static void ovsdb_jsonrpc_monitor_remove_all(struct ovsdb_jsonrpc_session *);
|
2009-11-13 14:47:25 -08:00
|
|
|
|
|
|
|
|
|
/* JSON-RPC database server. */
|
2009-11-04 15:11:44 -08:00
|
|
|
|
|
|
|
|
|
struct ovsdb_jsonrpc_server {
|
|
|
|
|
struct ovsdb *db;
|
|
|
|
|
unsigned int n_sessions, max_sessions;
|
2010-01-04 10:05:51 -08:00
|
|
|
|
struct shash remotes; /* Contains "struct ovsdb_jsonrpc_remote *"s. */
|
|
|
|
|
};
|
2009-11-04 15:11:44 -08:00
|
|
|
|
|
2010-01-04 10:05:51 -08:00
|
|
|
|
/* A configured remote. This is either a passive stream listener plus a list
|
|
|
|
|
* of the currently connected sessions, or a list of exactly one active
|
|
|
|
|
* session. */
|
|
|
|
|
struct ovsdb_jsonrpc_remote {
|
|
|
|
|
struct ovsdb_jsonrpc_server *server;
|
|
|
|
|
struct pstream *listener; /* Listener, if passive. */
|
|
|
|
|
struct list sessions; /* List of "struct ovsdb_jsonrpc_session"s. */
|
2009-11-04 15:11:44 -08:00
|
|
|
|
};
|
|
|
|
|
|
2010-01-04 10:05:51 -08:00
|
|
|
|
static void ovsdb_jsonrpc_server_add_remote(struct ovsdb_jsonrpc_server *,
|
|
|
|
|
const char *name);
|
|
|
|
|
static void ovsdb_jsonrpc_server_del_remote(struct shash_node *);
|
|
|
|
|
|
2009-11-13 14:47:25 -08:00
|
|
|
|
struct ovsdb_jsonrpc_server *
|
|
|
|
|
ovsdb_jsonrpc_server_create(struct ovsdb *db)
|
2009-11-04 15:11:44 -08:00
|
|
|
|
{
|
2009-11-13 14:47:25 -08:00
|
|
|
|
struct ovsdb_jsonrpc_server *server = xzalloc(sizeof *server);
|
2009-11-04 15:11:44 -08:00
|
|
|
|
server->db = db;
|
|
|
|
|
server->max_sessions = 64;
|
2010-01-04 10:05:51 -08:00
|
|
|
|
shash_init(&server->remotes);
|
2009-11-13 14:47:25 -08:00
|
|
|
|
return server;
|
|
|
|
|
}
|
2009-11-04 15:11:44 -08:00
|
|
|
|
|
2010-02-02 14:41:00 -08:00
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-04 10:05:51 -08:00
|
|
|
|
/* Sets 'svr''s current set of remotes to the names in 'new_remotes'. The data
|
|
|
|
|
* values in 'new_remotes' are ignored.
|
|
|
|
|
*
|
|
|
|
|
* A remote is an active or passive stream connection method, e.g. "pssl:" or
|
|
|
|
|
* "tcp:1.2.3.4". */
|
2009-12-03 14:20:33 -08:00
|
|
|
|
void
|
2010-01-04 10:05:51 -08:00
|
|
|
|
ovsdb_jsonrpc_server_set_remotes(struct ovsdb_jsonrpc_server *svr,
|
|
|
|
|
const struct shash *new_remotes)
|
2009-11-13 14:47:25 -08:00
|
|
|
|
{
|
2010-01-04 10:05:51 -08:00
|
|
|
|
struct shash_node *node, *next;
|
|
|
|
|
|
|
|
|
|
SHASH_FOR_EACH_SAFE (node, next, &svr->remotes) {
|
|
|
|
|
if (!shash_find(new_remotes, node->name)) {
|
|
|
|
|
ovsdb_jsonrpc_server_del_remote(node);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
SHASH_FOR_EACH (node, new_remotes) {
|
|
|
|
|
if (!shash_find(&svr->remotes, node->name)) {
|
|
|
|
|
ovsdb_jsonrpc_server_add_remote(svr, node->name);
|
|
|
|
|
}
|
2009-11-04 15:11:44 -08:00
|
|
|
|
}
|
2009-11-13 14:47:25 -08:00
|
|
|
|
}
|
2009-11-04 15:11:44 -08:00
|
|
|
|
|
2010-01-04 10:05:51 -08:00
|
|
|
|
static void
|
|
|
|
|
ovsdb_jsonrpc_server_add_remote(struct ovsdb_jsonrpc_server *svr,
|
|
|
|
|
const char *name)
|
2009-11-13 14:47:25 -08:00
|
|
|
|
{
|
2010-01-04 10:05:51 -08:00
|
|
|
|
struct ovsdb_jsonrpc_remote *remote;
|
|
|
|
|
struct pstream *listener;
|
|
|
|
|
int error;
|
|
|
|
|
|
2010-03-18 12:59:34 -07:00
|
|
|
|
error = jsonrpc_pstream_open(name, &listener);
|
2010-01-04 10:05:51 -08:00
|
|
|
|
if (error && error != EAFNOSUPPORT) {
|
|
|
|
|
VLOG_ERR_RL(&rl, "%s: listen failed: %s", name, strerror(error));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
remote = xmalloc(sizeof *remote);
|
|
|
|
|
remote->server = svr;
|
|
|
|
|
remote->listener = listener;
|
|
|
|
|
list_init(&remote->sessions);
|
|
|
|
|
shash_add(&svr->remotes, name, remote);
|
|
|
|
|
|
|
|
|
|
if (!listener) {
|
|
|
|
|
ovsdb_jsonrpc_session_create(remote, jsonrpc_session_open(name));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
ovsdb_jsonrpc_server_del_remote(struct shash_node *node)
|
|
|
|
|
{
|
|
|
|
|
struct ovsdb_jsonrpc_remote *remote = node->data;
|
|
|
|
|
|
|
|
|
|
ovsdb_jsonrpc_session_close_all(remote);
|
|
|
|
|
pstream_close(remote->listener);
|
|
|
|
|
shash_delete(&remote->server->remotes, node);
|
|
|
|
|
free(remote);
|
2009-11-04 15:11:44 -08:00
|
|
|
|
}
|
|
|
|
|
|
2010-06-24 12:56:30 -07:00
|
|
|
|
/* Forces all of the JSON-RPC sessions managed by 'svr' to disconnect and
|
|
|
|
|
* reconnect. */
|
|
|
|
|
void
|
|
|
|
|
ovsdb_jsonrpc_server_reconnect(struct ovsdb_jsonrpc_server *svr)
|
|
|
|
|
{
|
|
|
|
|
struct shash_node *node;
|
|
|
|
|
|
|
|
|
|
SHASH_FOR_EACH (node, &svr->remotes) {
|
|
|
|
|
struct ovsdb_jsonrpc_remote *remote = node->data;
|
|
|
|
|
|
|
|
|
|
ovsdb_jsonrpc_session_reconnect_all(remote);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-11-04 15:11:44 -08:00
|
|
|
|
void
|
|
|
|
|
ovsdb_jsonrpc_server_run(struct ovsdb_jsonrpc_server *svr)
|
|
|
|
|
{
|
2010-01-04 10:05:51 -08:00
|
|
|
|
struct shash_node *node;
|
|
|
|
|
|
|
|
|
|
SHASH_FOR_EACH (node, &svr->remotes) {
|
|
|
|
|
struct ovsdb_jsonrpc_remote *remote = node->data;
|
|
|
|
|
|
|
|
|
|
if (remote->listener && 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));
|
|
|
|
|
ovsdb_jsonrpc_session_create(remote, js);
|
|
|
|
|
} else if (error != EAGAIN) {
|
|
|
|
|
VLOG_WARN_RL(&rl, "%s: accept failed: %s",
|
|
|
|
|
pstream_get_name(remote->listener),
|
|
|
|
|
strerror(error));
|
|
|
|
|
}
|
2009-11-04 15:11:44 -08:00
|
|
|
|
}
|
|
|
|
|
|
2010-01-04 10:05:51 -08:00
|
|
|
|
ovsdb_jsonrpc_session_run_all(remote);
|
|
|
|
|
}
|
2009-11-04 15:11:44 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
ovsdb_jsonrpc_server_wait(struct ovsdb_jsonrpc_server *svr)
|
|
|
|
|
{
|
2010-01-04 10:05:51 -08:00
|
|
|
|
struct shash_node *node;
|
|
|
|
|
|
|
|
|
|
SHASH_FOR_EACH (node, &svr->remotes) {
|
|
|
|
|
struct ovsdb_jsonrpc_remote *remote = node->data;
|
2009-11-04 15:11:44 -08:00
|
|
|
|
|
2010-01-04 10:05:51 -08:00
|
|
|
|
if (remote->listener && svr->n_sessions < svr->max_sessions) {
|
|
|
|
|
pstream_wait(remote->listener);
|
2009-11-04 15:11:44 -08:00
|
|
|
|
}
|
|
|
|
|
|
2010-01-04 10:05:51 -08:00
|
|
|
|
ovsdb_jsonrpc_session_wait_all(remote);
|
|
|
|
|
}
|
2009-11-04 15:11:44 -08:00
|
|
|
|
}
|
2009-11-13 14:47:25 -08:00
|
|
|
|
|
|
|
|
|
/* JSON-RPC database server session. */
|
2009-11-04 15:11:44 -08:00
|
|
|
|
|
2009-11-13 14:47:25 -08:00
|
|
|
|
struct ovsdb_jsonrpc_session {
|
2010-01-04 10:05:51 -08:00
|
|
|
|
struct ovsdb_jsonrpc_remote *remote;
|
|
|
|
|
struct list node; /* Element in remote's sessions list. */
|
2009-11-04 15:11:44 -08:00
|
|
|
|
|
2009-11-13 14:47:25 -08:00
|
|
|
|
/* Triggers. */
|
|
|
|
|
struct hmap triggers; /* Hmap of "struct ovsdb_jsonrpc_trigger"s. */
|
|
|
|
|
struct list completions; /* Completed triggers. */
|
2009-11-04 15:11:44 -08:00
|
|
|
|
|
2009-11-16 10:38:14 -08:00
|
|
|
|
/* Monitors. */
|
|
|
|
|
struct hmap monitors; /* Hmap of "struct ovsdb_jsonrpc_monitor"s. */
|
|
|
|
|
|
2009-12-17 15:16:43 -08:00
|
|
|
|
/* Network connectivity. */
|
|
|
|
|
struct jsonrpc_session *js; /* JSON-RPC session. */
|
|
|
|
|
unsigned int js_seqno; /* Last jsonrpc_session_get_seqno() value. */
|
2009-11-13 14:47:25 -08:00
|
|
|
|
};
|
2009-11-04 15:11:44 -08:00
|
|
|
|
|
2009-11-13 14:47:25 -08:00
|
|
|
|
static void ovsdb_jsonrpc_session_close(struct ovsdb_jsonrpc_session *);
|
|
|
|
|
static int ovsdb_jsonrpc_session_run(struct ovsdb_jsonrpc_session *);
|
|
|
|
|
static void ovsdb_jsonrpc_session_wait(struct ovsdb_jsonrpc_session *);
|
|
|
|
|
static void ovsdb_jsonrpc_session_got_request(struct ovsdb_jsonrpc_session *,
|
|
|
|
|
struct jsonrpc_msg *);
|
|
|
|
|
static void ovsdb_jsonrpc_session_got_notify(struct ovsdb_jsonrpc_session *,
|
|
|
|
|
struct jsonrpc_msg *);
|
2009-11-04 15:11:44 -08:00
|
|
|
|
|
2009-11-12 12:58:53 -08:00
|
|
|
|
static struct ovsdb_jsonrpc_session *
|
2010-01-04 10:05:51 -08:00
|
|
|
|
ovsdb_jsonrpc_session_create(struct ovsdb_jsonrpc_remote *remote,
|
2009-12-17 15:16:43 -08:00
|
|
|
|
struct jsonrpc_session *js)
|
2009-11-04 15:11:44 -08:00
|
|
|
|
{
|
|
|
|
|
struct ovsdb_jsonrpc_session *s;
|
|
|
|
|
|
|
|
|
|
s = xzalloc(sizeof *s);
|
2010-01-04 10:05:51 -08:00
|
|
|
|
s->remote = remote;
|
|
|
|
|
list_push_back(&remote->sessions, &s->node);
|
2009-11-04 15:11:44 -08:00
|
|
|
|
hmap_init(&s->triggers);
|
2009-11-16 10:38:14 -08:00
|
|
|
|
hmap_init(&s->monitors);
|
2009-11-04 15:11:44 -08:00
|
|
|
|
list_init(&s->completions);
|
2009-12-17 15:16:43 -08:00
|
|
|
|
s->js = js;
|
|
|
|
|
s->js_seqno = jsonrpc_session_get_seqno(js);
|
2009-11-12 12:58:53 -08:00
|
|
|
|
|
2010-01-04 10:05:51 -08:00
|
|
|
|
remote->server->n_sessions++;
|
2009-11-12 12:58:53 -08:00
|
|
|
|
|
|
|
|
|
return s;
|
2009-11-04 15:11:44 -08:00
|
|
|
|
}
|
|
|
|
|
|
2009-11-12 12:58:53 -08:00
|
|
|
|
static void
|
|
|
|
|
ovsdb_jsonrpc_session_close(struct ovsdb_jsonrpc_session *s)
|
|
|
|
|
{
|
2010-02-02 14:40:25 -08:00
|
|
|
|
ovsdb_jsonrpc_monitor_remove_all(s);
|
2009-12-17 15:16:43 -08:00
|
|
|
|
jsonrpc_session_close(s->js);
|
2009-11-04 15:11:44 -08:00
|
|
|
|
list_remove(&s->node);
|
2010-01-04 10:05:51 -08:00
|
|
|
|
s->remote->server->n_sessions--;
|
2010-02-02 14:40:25 -08:00
|
|
|
|
free(s);
|
2009-11-04 15:11:44 -08:00
|
|
|
|
}
|
|
|
|
|
|
2009-12-17 15:16:43 -08:00
|
|
|
|
static int
|
|
|
|
|
ovsdb_jsonrpc_session_run(struct ovsdb_jsonrpc_session *s)
|
2009-11-12 12:58:53 -08:00
|
|
|
|
{
|
2009-12-17 15:16:43 -08:00
|
|
|
|
jsonrpc_session_run(s->js);
|
|
|
|
|
if (s->js_seqno != jsonrpc_session_get_seqno(s->js)) {
|
|
|
|
|
s->js_seqno = jsonrpc_session_get_seqno(s->js);
|
2009-11-13 14:47:25 -08:00
|
|
|
|
ovsdb_jsonrpc_trigger_complete_all(s);
|
2009-11-16 10:38:14 -08:00
|
|
|
|
ovsdb_jsonrpc_monitor_remove_all(s);
|
2009-11-12 12:58:53 -08:00
|
|
|
|
}
|
|
|
|
|
|
2009-12-17 15:16:43 -08:00
|
|
|
|
ovsdb_jsonrpc_trigger_complete_done(s);
|
2009-11-12 12:58:53 -08:00
|
|
|
|
|
2009-12-17 15:16:43 -08:00
|
|
|
|
if (!jsonrpc_session_get_backlog(s->js)) {
|
|
|
|
|
struct jsonrpc_msg *msg = jsonrpc_session_recv(s->js);
|
|
|
|
|
if (msg) {
|
2009-11-12 12:58:53 -08:00
|
|
|
|
if (msg->type == JSONRPC_REQUEST) {
|
|
|
|
|
ovsdb_jsonrpc_session_got_request(s, msg);
|
|
|
|
|
} else if (msg->type == JSONRPC_NOTIFY) {
|
|
|
|
|
ovsdb_jsonrpc_session_got_notify(s, msg);
|
|
|
|
|
} else {
|
|
|
|
|
VLOG_WARN("%s: received unexpected %s message",
|
2009-12-17 15:16:43 -08:00
|
|
|
|
jsonrpc_session_get_name(s->js),
|
2009-11-12 12:58:53 -08:00
|
|
|
|
jsonrpc_msg_type_to_string(msg->type));
|
2009-12-17 15:16:43 -08:00
|
|
|
|
jsonrpc_session_force_reconnect(s->js);
|
2009-11-12 12:58:53 -08:00
|
|
|
|
jsonrpc_msg_destroy(msg);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-12-17 15:16:43 -08:00
|
|
|
|
return jsonrpc_session_is_alive(s->js) ? 0 : ETIMEDOUT;
|
2009-11-13 14:47:25 -08:00
|
|
|
|
}
|
2009-11-12 12:58:53 -08:00
|
|
|
|
|
2009-11-13 14:47:25 -08:00
|
|
|
|
static void
|
2010-01-04 10:05:51 -08:00
|
|
|
|
ovsdb_jsonrpc_session_run_all(struct ovsdb_jsonrpc_remote *remote)
|
2009-11-13 14:47:25 -08:00
|
|
|
|
{
|
|
|
|
|
struct ovsdb_jsonrpc_session *s, *next;
|
|
|
|
|
|
|
|
|
|
LIST_FOR_EACH_SAFE (s, next, struct ovsdb_jsonrpc_session, node,
|
2010-01-04 10:05:51 -08:00
|
|
|
|
&remote->sessions) {
|
2009-11-13 14:47:25 -08:00
|
|
|
|
int error = ovsdb_jsonrpc_session_run(s);
|
|
|
|
|
if (error) {
|
|
|
|
|
ovsdb_jsonrpc_session_close(s);
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-11-12 12:58:53 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
ovsdb_jsonrpc_session_wait(struct ovsdb_jsonrpc_session *s)
|
|
|
|
|
{
|
2009-12-17 15:16:43 -08:00
|
|
|
|
jsonrpc_session_wait(s->js);
|
|
|
|
|
if (!jsonrpc_session_get_backlog(s->js)) {
|
|
|
|
|
jsonrpc_session_recv_wait(s->js);
|
2009-11-12 12:58:53 -08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-11-13 14:47:25 -08:00
|
|
|
|
static void
|
2010-01-04 10:05:51 -08:00
|
|
|
|
ovsdb_jsonrpc_session_wait_all(struct ovsdb_jsonrpc_remote *remote)
|
2009-11-04 15:11:44 -08:00
|
|
|
|
{
|
2009-11-13 14:47:25 -08:00
|
|
|
|
struct ovsdb_jsonrpc_session *s;
|
2009-11-04 15:11:44 -08:00
|
|
|
|
|
2010-01-04 10:05:51 -08:00
|
|
|
|
LIST_FOR_EACH (s, struct ovsdb_jsonrpc_session, node, &remote->sessions) {
|
2009-11-13 14:47:25 -08:00
|
|
|
|
ovsdb_jsonrpc_session_wait(s);
|
2009-11-04 15:11:44 -08:00
|
|
|
|
}
|
2009-11-13 14:47:25 -08:00
|
|
|
|
}
|
2009-11-04 15:11:44 -08:00
|
|
|
|
|
2010-01-04 10:05:51 -08:00
|
|
|
|
static void
|
|
|
|
|
ovsdb_jsonrpc_session_close_all(struct ovsdb_jsonrpc_remote *remote)
|
|
|
|
|
{
|
|
|
|
|
struct ovsdb_jsonrpc_session *s, *next;
|
|
|
|
|
|
|
|
|
|
LIST_FOR_EACH_SAFE (s, next, struct ovsdb_jsonrpc_session, node,
|
|
|
|
|
&remote->sessions) {
|
|
|
|
|
ovsdb_jsonrpc_session_close(s);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-06-24 12:56:30 -07:00
|
|
|
|
/* Forces all of the JSON-RPC sessions managed by 'remote' to disconnect and
|
|
|
|
|
* reconnect. */
|
|
|
|
|
static void
|
|
|
|
|
ovsdb_jsonrpc_session_reconnect_all(struct ovsdb_jsonrpc_remote *remote)
|
|
|
|
|
{
|
|
|
|
|
struct ovsdb_jsonrpc_session *s, *next;
|
|
|
|
|
|
|
|
|
|
LIST_FOR_EACH_SAFE (s, next, struct ovsdb_jsonrpc_session, node,
|
|
|
|
|
&remote->sessions) {
|
|
|
|
|
jsonrpc_session_force_reconnect(s->js);
|
|
|
|
|
if (!jsonrpc_session_is_alive(s->js)) {
|
|
|
|
|
ovsdb_jsonrpc_session_close(s);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-02-09 10:17:58 -08:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2009-11-13 14:47:25 -08:00
|
|
|
|
static struct jsonrpc_msg *
|
|
|
|
|
execute_transaction(struct ovsdb_jsonrpc_session *s,
|
|
|
|
|
struct jsonrpc_msg *request)
|
|
|
|
|
{
|
|
|
|
|
ovsdb_jsonrpc_trigger_create(s, request->id, request->params);
|
2009-11-04 15:11:44 -08:00
|
|
|
|
request->id = NULL;
|
|
|
|
|
request->params = NULL;
|
2010-02-02 14:40:25 -08:00
|
|
|
|
jsonrpc_msg_destroy(request);
|
2009-11-04 15:11:44 -08:00
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
ovsdb_jsonrpc_session_got_request(struct ovsdb_jsonrpc_session *s,
|
|
|
|
|
struct jsonrpc_msg *request)
|
|
|
|
|
{
|
|
|
|
|
struct jsonrpc_msg *reply;
|
|
|
|
|
|
|
|
|
|
if (!strcmp(request->method, "transact")) {
|
2010-02-09 10:17:58 -08:00
|
|
|
|
reply = ovsdb_jsonrpc_check_db_name(s, request);
|
|
|
|
|
if (!reply) {
|
|
|
|
|
reply = execute_transaction(s, request);
|
|
|
|
|
}
|
2009-11-16 10:38:14 -08:00
|
|
|
|
} else if (!strcmp(request->method, "monitor")) {
|
2010-02-09 10:17:58 -08:00
|
|
|
|
reply = ovsdb_jsonrpc_check_db_name(s, request);
|
|
|
|
|
if (!reply) {
|
|
|
|
|
reply = jsonrpc_create_reply(
|
|
|
|
|
ovsdb_jsonrpc_monitor_create(s, request->params), request->id);
|
|
|
|
|
}
|
2009-11-16 10:38:14 -08:00
|
|
|
|
} else if (!strcmp(request->method, "monitor_cancel")) {
|
|
|
|
|
reply = ovsdb_jsonrpc_monitor_cancel(s, json_array(request->params),
|
|
|
|
|
request->id);
|
2009-11-04 15:11:44 -08:00
|
|
|
|
} else if (!strcmp(request->method, "get_schema")) {
|
2010-02-09 10:17:58 -08:00
|
|
|
|
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")) {
|
2009-11-04 15:11:44 -08:00
|
|
|
|
reply = jsonrpc_create_reply(
|
2010-02-09 10:17:58 -08:00
|
|
|
|
json_array_create_1(json_string_create(get_db_name(s))),
|
|
|
|
|
request->id);
|
2009-11-12 12:58:53 -08:00
|
|
|
|
} else if (!strcmp(request->method, "echo")) {
|
|
|
|
|
reply = jsonrpc_create_reply(json_clone(request->params), request->id);
|
2009-11-04 15:11:44 -08:00
|
|
|
|
} else {
|
|
|
|
|
reply = jsonrpc_create_error(json_string_create("unknown method"),
|
|
|
|
|
request->id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (reply) {
|
|
|
|
|
jsonrpc_msg_destroy(request);
|
2009-12-17 15:16:43 -08:00
|
|
|
|
jsonrpc_session_send(s->js, reply);
|
2009-11-04 15:11:44 -08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
execute_cancel(struct ovsdb_jsonrpc_session *s, struct jsonrpc_msg *request)
|
|
|
|
|
{
|
2009-11-13 15:05:19 -08:00
|
|
|
|
if (json_array(request->params)->n == 1) {
|
|
|
|
|
struct ovsdb_jsonrpc_trigger *t;
|
|
|
|
|
struct json *id;
|
2009-11-04 15:11:44 -08:00
|
|
|
|
|
2009-11-13 15:05:19 -08:00
|
|
|
|
id = request->params->u.array.elems[0];
|
|
|
|
|
t = ovsdb_jsonrpc_trigger_find(s, id, json_hash(id, 0));
|
|
|
|
|
if (t) {
|
|
|
|
|
ovsdb_jsonrpc_trigger_complete(t);
|
|
|
|
|
}
|
2009-11-04 15:11:44 -08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
ovsdb_jsonrpc_session_got_notify(struct ovsdb_jsonrpc_session *s,
|
|
|
|
|
struct jsonrpc_msg *request)
|
|
|
|
|
{
|
|
|
|
|
if (!strcmp(request->method, "cancel")) {
|
|
|
|
|
execute_cancel(s, request);
|
|
|
|
|
}
|
|
|
|
|
jsonrpc_msg_destroy(request);
|
|
|
|
|
}
|
2009-11-13 14:47:25 -08:00
|
|
|
|
|
|
|
|
|
/* JSON-RPC database server triggers.
|
|
|
|
|
*
|
|
|
|
|
* (Every transaction is treated as a trigger even if it doesn't actually have
|
|
|
|
|
* any "wait" operations.) */
|
|
|
|
|
|
|
|
|
|
struct ovsdb_jsonrpc_trigger {
|
|
|
|
|
struct ovsdb_trigger trigger;
|
|
|
|
|
struct ovsdb_jsonrpc_session *session;
|
|
|
|
|
struct hmap_node hmap_node; /* In session's "triggers" hmap. */
|
|
|
|
|
struct json *id;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
ovsdb_jsonrpc_trigger_create(struct ovsdb_jsonrpc_session *s,
|
|
|
|
|
struct json *id, struct json *params)
|
|
|
|
|
{
|
|
|
|
|
struct ovsdb_jsonrpc_trigger *t;
|
|
|
|
|
size_t hash;
|
|
|
|
|
|
|
|
|
|
/* Check for duplicate ID. */
|
|
|
|
|
hash = json_hash(id, 0);
|
|
|
|
|
t = ovsdb_jsonrpc_trigger_find(s, id, hash);
|
|
|
|
|
if (t) {
|
2009-12-17 15:16:43 -08:00
|
|
|
|
struct jsonrpc_msg *msg;
|
|
|
|
|
|
|
|
|
|
msg = jsonrpc_create_error(json_string_create("duplicate request ID"),
|
|
|
|
|
id);
|
|
|
|
|
jsonrpc_session_send(s->js, msg);
|
2009-11-13 14:47:25 -08:00
|
|
|
|
json_destroy(id);
|
|
|
|
|
json_destroy(params);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Insert into trigger table. */
|
|
|
|
|
t = xmalloc(sizeof *t);
|
2010-01-04 10:05:51 -08:00
|
|
|
|
ovsdb_trigger_init(s->remote->server->db,
|
2009-11-13 14:47:25 -08:00
|
|
|
|
&t->trigger, params, &s->completions,
|
|
|
|
|
time_msec());
|
|
|
|
|
t->session = s;
|
|
|
|
|
t->id = id;
|
|
|
|
|
hmap_insert(&s->triggers, &t->hmap_node, hash);
|
|
|
|
|
|
|
|
|
|
/* Complete early if possible. */
|
|
|
|
|
if (ovsdb_trigger_is_complete(&t->trigger)) {
|
|
|
|
|
ovsdb_jsonrpc_trigger_complete(t);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static struct ovsdb_jsonrpc_trigger *
|
|
|
|
|
ovsdb_jsonrpc_trigger_find(struct ovsdb_jsonrpc_session *s,
|
|
|
|
|
const struct json *id, size_t hash)
|
|
|
|
|
{
|
|
|
|
|
struct ovsdb_jsonrpc_trigger *t;
|
|
|
|
|
|
|
|
|
|
HMAP_FOR_EACH_WITH_HASH (t, struct ovsdb_jsonrpc_trigger, hmap_node, hash,
|
|
|
|
|
&s->triggers) {
|
|
|
|
|
if (json_equal(t->id, id)) {
|
|
|
|
|
return t;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
ovsdb_jsonrpc_trigger_complete(struct ovsdb_jsonrpc_trigger *t)
|
|
|
|
|
{
|
|
|
|
|
struct ovsdb_jsonrpc_session *s = t->session;
|
|
|
|
|
|
2009-12-17 15:16:43 -08:00
|
|
|
|
if (jsonrpc_session_is_connected(s->js)) {
|
2009-11-13 14:47:25 -08:00
|
|
|
|
struct jsonrpc_msg *reply;
|
|
|
|
|
struct json *result;
|
|
|
|
|
|
|
|
|
|
result = ovsdb_trigger_steal_result(&t->trigger);
|
|
|
|
|
if (result) {
|
|
|
|
|
reply = jsonrpc_create_reply(result, t->id);
|
|
|
|
|
} else {
|
|
|
|
|
reply = jsonrpc_create_error(json_string_create("canceled"),
|
|
|
|
|
t->id);
|
|
|
|
|
}
|
2009-12-17 15:16:43 -08:00
|
|
|
|
jsonrpc_session_send(s->js, reply);
|
2009-11-13 14:47:25 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
json_destroy(t->id);
|
|
|
|
|
ovsdb_trigger_destroy(&t->trigger);
|
|
|
|
|
hmap_remove(&s->triggers, &t->hmap_node);
|
|
|
|
|
free(t);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
ovsdb_jsonrpc_trigger_complete_all(struct ovsdb_jsonrpc_session *s)
|
|
|
|
|
{
|
|
|
|
|
struct ovsdb_jsonrpc_trigger *t, *next;
|
|
|
|
|
HMAP_FOR_EACH_SAFE (t, next, struct ovsdb_jsonrpc_trigger, hmap_node,
|
|
|
|
|
&s->triggers) {
|
|
|
|
|
ovsdb_jsonrpc_trigger_complete(t);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
ovsdb_jsonrpc_trigger_complete_done(struct ovsdb_jsonrpc_session *s)
|
|
|
|
|
{
|
|
|
|
|
while (!list_is_empty(&s->completions)) {
|
|
|
|
|
struct ovsdb_jsonrpc_trigger *t
|
|
|
|
|
= CONTAINER_OF(s->completions.next,
|
|
|
|
|
struct ovsdb_jsonrpc_trigger, trigger.node);
|
|
|
|
|
ovsdb_jsonrpc_trigger_complete(t);
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-11-16 10:38:14 -08:00
|
|
|
|
|
|
|
|
|
/* 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. */
|
|
|
|
|
};
|
|
|
|
|
|
2010-07-01 09:47:46 -07:00
|
|
|
|
/* 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. */
|
2009-11-16 10:38:14 -08:00
|
|
|
|
struct ovsdb_jsonrpc_monitor_table {
|
|
|
|
|
const struct ovsdb_table *table;
|
2010-07-01 09:47:46 -07:00
|
|
|
|
|
|
|
|
|
/* This is the union (bitwise-OR) of the 'select' values in all of the
|
|
|
|
|
* members of 'columns' below. */
|
2009-11-16 10:38:14 -08:00
|
|
|
|
enum ovsdb_jsonrpc_monitor_selection select;
|
2010-07-01 09:47:46 -07:00
|
|
|
|
|
|
|
|
|
/* Columns being monitored. */
|
|
|
|
|
struct ovsdb_jsonrpc_monitor_column *columns;
|
|
|
|
|
size_t n_columns;
|
2009-11-16 10:38:14 -08:00
|
|
|
|
};
|
|
|
|
|
|
2010-07-01 09:47:46 -07:00
|
|
|
|
/* A collection of tables being monitored. */
|
2009-11-16 10:38:14 -08:00
|
|
|
|
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, struct ovsdb_jsonrpc_monitor, node,
|
|
|
|
|
json_hash(monitor_id, 0), &s->monitors) {
|
|
|
|
|
if (json_equal(m->monitor_id, monitor_id)) {
|
|
|
|
|
return m;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-01 09:47:46 -07:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2009-11-16 10:38:14 -08:00
|
|
|
|
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;
|
|
|
|
|
|
2010-02-09 10:17:58 -08:00
|
|
|
|
if (json_array(params)->n != 3) {
|
2009-11-16 10:38:14 -08:00
|
|
|
|
error = ovsdb_syntax_error(params, NULL, "invalid parameters");
|
|
|
|
|
goto error;
|
|
|
|
|
}
|
2010-02-09 10:17:58 -08:00
|
|
|
|
monitor_id = params->u.array.elems[1];
|
|
|
|
|
monitor_requests = params->u.array.elems[2];
|
2009-11-16 10:38:14 -08:00
|
|
|
|
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);
|
2010-01-04 10:05:51 -08:00
|
|
|
|
ovsdb_add_replica(s->remote->server->db, &m->replica);
|
2009-11-16 10:38:14 -08:00
|
|
|
|
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;
|
2010-07-01 09:47:46 -07:00
|
|
|
|
size_t allocated_columns;
|
|
|
|
|
const struct json *mr_value;
|
|
|
|
|
size_t i;
|
2009-11-16 10:38:14 -08:00
|
|
|
|
|
2010-01-04 10:05:51 -08:00
|
|
|
|
table = ovsdb_get_table(s->remote->server->db, node->name);
|
2009-11-16 10:38:14 -08:00
|
|
|
|
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);
|
|
|
|
|
|
2010-07-01 09:47:46 -07:00
|
|
|
|
/* 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;
|
|
|
|
|
}
|
2009-11-16 10:38:14 -08:00
|
|
|
|
}
|
|
|
|
|
} else {
|
2010-07-01 09:47:46 -07:00
|
|
|
|
error = ovsdb_jsonrpc_parse_monitor_request(
|
|
|
|
|
mt, mr_value, &allocated_columns);
|
|
|
|
|
if (error) {
|
|
|
|
|
goto error;
|
2009-11-16 10:38:14 -08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-01 09:47:46 -07:00
|
|
|
|
/* 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);
|
2009-11-16 10:38:14 -08:00
|
|
|
|
goto error;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ovsdb_jsonrpc_monitor_get_initial(m);
|
|
|
|
|
|
|
|
|
|
error:
|
2009-11-18 11:32:16 -08:00
|
|
|
|
if (m) {
|
2010-01-04 10:05:51 -08:00
|
|
|
|
ovsdb_remove_replica(s->remote->server->db, &m->replica);
|
2009-11-18 11:32:16 -08:00
|
|
|
|
}
|
2009-11-16 10:38:14 -08:00
|
|
|
|
|
|
|
|
|
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 {
|
2010-01-04 10:05:51 -08:00
|
|
|
|
ovsdb_remove_replica(s->remote->server->db, &m->replica);
|
2009-11-16 10:38:14 -08:00
|
|
|
|
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,
|
|
|
|
|
struct ovsdb_jsonrpc_monitor, node, &s->monitors) {
|
2010-01-04 10:05:51 -08:00
|
|
|
|
ovsdb_remove_replica(s->remote->server->db, &m->replica);
|
2009-11-16 10:38:14 -08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
ovsdb_jsonrpc_monitor_change_cb(const struct ovsdb_row *old,
|
|
|
|
|
const struct ovsdb_row *new,
|
2010-03-11 17:14:31 -08:00
|
|
|
|
const unsigned long int *changed,
|
2009-11-16 10:38:14 -08:00
|
|
|
|
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];
|
|
|
|
|
int n_changed;
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
old_json = new_json = NULL;
|
|
|
|
|
n_changed = 0;
|
2010-07-01 09:47:46 -07:00
|
|
|
|
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;
|
2010-03-11 17:14:31 -08:00
|
|
|
|
bool column_changed = false;
|
2009-11-16 10:38:14 -08:00
|
|
|
|
|
2010-07-01 09:47:46 -07:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2009-11-16 10:38:14 -08:00
|
|
|
|
if (type == OJMS_MODIFY) {
|
2010-03-11 17:14:31 -08:00
|
|
|
|
column_changed = bitmap_is_set(changed, idx);
|
|
|
|
|
n_changed += column_changed;
|
2009-11-16 10:38:14 -08:00
|
|
|
|
}
|
2010-03-11 17:14:31 -08:00
|
|
|
|
if (column_changed || type == OJMS_DELETE) {
|
2009-11-16 10:38:14 -08:00
|
|
|
|
if (!old_json) {
|
|
|
|
|
old_json = json_object_create();
|
|
|
|
|
}
|
|
|
|
|
json_object_put(old_json, column->name,
|
|
|
|
|
ovsdb_datum_to_json(&old->fields[idx],
|
|
|
|
|
&column->type));
|
|
|
|
|
}
|
|
|
|
|
if (type & (OJMS_INITIAL | OJMS_INSERT | OJMS_MODIFY)) {
|
|
|
|
|
if (!new_json) {
|
|
|
|
|
new_json = json_object_create();
|
|
|
|
|
}
|
|
|
|
|
json_object_put(new_json, column->name,
|
|
|
|
|
ovsdb_datum_to_json(&new->fields[idx],
|
|
|
|
|
&column->type));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if ((type == OJMS_MODIFY && !n_changed) || (!old_json && !new_json)) {
|
|
|
|
|
/* No reportable changes. */
|
|
|
|
|
json_destroy(old_json);
|
|
|
|
|
json_destroy(new_json);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 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,
|
2010-02-11 11:11:23 -08:00
|
|
|
|
const struct ovsdb_txn *txn,
|
|
|
|
|
bool durable OVS_UNUSED)
|
2009-11-16 10:38:14 -08:00
|
|
|
|
{
|
|
|
|
|
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);
|
2009-12-17 15:16:43 -08:00
|
|
|
|
jsonrpc_session_send(aux.monitor->session->js, msg);
|
2009-11-16 10:38:14 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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, struct ovsdb_row, hmap_node,
|
|
|
|
|
&mt->table->rows) {
|
2010-03-11 17:14:31 -08:00
|
|
|
|
ovsdb_jsonrpc_monitor_change_cb(NULL, row, NULL, &aux);
|
2009-11-16 10:38:14 -08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
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;
|
2010-07-01 09:47:46 -07:00
|
|
|
|
free(mt->columns);
|
2009-11-16 10:38:14 -08:00
|
|
|
|
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
|
|
|
|
|
};
|