2015-02-20 08:44:48 -08:00
|
|
|
|
/* Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015 Nicira, Inc.
|
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"
|
|
|
|
|
|
|
|
|
|
#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"
|
2011-07-13 16:15:22 -07:00
|
|
|
|
#include "dynamic-string.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"
|
2014-04-02 08:43:17 -07:00
|
|
|
|
#include "poll-loop.h"
|
2009-11-12 12:58:53 -08:00
|
|
|
|
#include "reconnect.h"
|
2009-11-16 10:38:14 -08:00
|
|
|
|
#include "row.h"
|
2011-07-26 10:17:36 -07:00
|
|
|
|
#include "server.h"
|
2012-05-08 15:44:21 -07:00
|
|
|
|
#include "simap.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"
|
2014-12-15 14:10:38 +01:00
|
|
|
|
#include "openvswitch/vlog.h"
|
2009-11-04 15:11:44 -08:00
|
|
|
|
|
2010-10-19 14:47:01 -07:00
|
|
|
|
VLOG_DEFINE_THIS_MODULE(ovsdb_jsonrpc_server);
|
2010-07-16 11:02:49 -07:00
|
|
|
|
|
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. */
|
2011-05-04 13:58:10 -07:00
|
|
|
|
static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
|
2010-01-04 10:05:51 -08:00
|
|
|
|
|
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 *);
|
2012-05-08 15:44:21 -07:00
|
|
|
|
static void ovsdb_jsonrpc_session_get_memory_usage_all(
|
|
|
|
|
const struct ovsdb_jsonrpc_remote *, struct simap *usage);
|
2010-01-04 10:05:51 -08:00
|
|
|
|
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 *);
|
2010-11-05 10:22:18 -07:00
|
|
|
|
static void ovsdb_jsonrpc_session_set_all_options(
|
|
|
|
|
struct ovsdb_jsonrpc_remote *, const struct ovsdb_jsonrpc_options *);
|
2011-07-13 16:15:22 -07:00
|
|
|
|
static bool ovsdb_jsonrpc_session_get_status(
|
2011-01-28 15:39:55 -08:00
|
|
|
|
const struct ovsdb_jsonrpc_remote *,
|
2011-07-13 16:15:22 -07:00
|
|
|
|
struct ovsdb_jsonrpc_remote_status *);
|
2011-07-26 10:24:17 -07:00
|
|
|
|
static void ovsdb_jsonrpc_session_unlock_all(struct ovsdb_jsonrpc_session *);
|
|
|
|
|
static void ovsdb_jsonrpc_session_unlock__(struct ovsdb_lock_waiter *);
|
2014-04-02 08:43:17 -07:00
|
|
|
|
static void ovsdb_jsonrpc_session_send(struct ovsdb_jsonrpc_session *,
|
|
|
|
|
struct jsonrpc_msg *);
|
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 *,
|
2012-09-07 10:07:03 -07:00
|
|
|
|
struct ovsdb *,
|
2009-11-13 14:47:25 -08:00
|
|
|
|
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. */
|
2015-03-19 21:00:46 -07:00
|
|
|
|
static struct jsonrpc_msg *ovsdb_jsonrpc_monitor_create(
|
|
|
|
|
struct ovsdb_jsonrpc_session *, struct ovsdb *, struct json *params,
|
|
|
|
|
const struct json *request_id);
|
2009-11-16 10:38:14 -08:00
|
|
|
|
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 *);
|
2014-04-02 08:43:17 -07:00
|
|
|
|
static void ovsdb_jsonrpc_monitor_flush_all(struct ovsdb_jsonrpc_session *);
|
|
|
|
|
static bool ovsdb_jsonrpc_monitor_needs_flush(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 {
|
2011-07-26 10:17:36 -07:00
|
|
|
|
struct ovsdb_server up;
|
2009-11-04 15:11:44 -08:00
|
|
|
|
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. */
|
2014-12-15 14:10:38 +01:00
|
|
|
|
struct ovs_list sessions; /* List of "struct ovsdb_jsonrpc_session"s. */
|
2012-10-04 12:33:05 -07:00
|
|
|
|
uint8_t dscp;
|
2009-11-04 15:11:44 -08:00
|
|
|
|
};
|
|
|
|
|
|
2010-11-05 10:22:18 -07:00
|
|
|
|
static struct ovsdb_jsonrpc_remote *ovsdb_jsonrpc_server_add_remote(
|
2012-03-10 15:58:10 -08:00
|
|
|
|
struct ovsdb_jsonrpc_server *, const char *name,
|
|
|
|
|
const struct ovsdb_jsonrpc_options *options
|
|
|
|
|
);
|
2010-01-04 10:05:51 -08:00
|
|
|
|
static void ovsdb_jsonrpc_server_del_remote(struct shash_node *);
|
|
|
|
|
|
2012-09-07 10:07:03 -07:00
|
|
|
|
/* Creates and returns a new server to provide JSON-RPC access to an OVSDB.
|
|
|
|
|
*
|
|
|
|
|
* The caller must call ovsdb_jsonrpc_server_add_db() for each database to
|
|
|
|
|
* which 'server' should provide access. */
|
2009-11-13 14:47:25 -08:00
|
|
|
|
struct ovsdb_jsonrpc_server *
|
2012-09-07 10:07:03 -07:00
|
|
|
|
ovsdb_jsonrpc_server_create(void)
|
2009-11-04 15:11:44 -08:00
|
|
|
|
{
|
2009-11-13 14:47:25 -08:00
|
|
|
|
struct ovsdb_jsonrpc_server *server = xzalloc(sizeof *server);
|
2012-09-07 10:07:03 -07:00
|
|
|
|
ovsdb_server_init(&server->up);
|
2015-02-23 23:20:17 -08:00
|
|
|
|
server->max_sessions = 330; /* Random limit. */
|
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
|
|
|
|
|
2012-09-07 10:07:03 -07:00
|
|
|
|
/* Adds 'db' to the set of databases served out by 'svr'. Returns true if
|
|
|
|
|
* successful, false if 'db''s name is the same as some database already in
|
|
|
|
|
* 'server'. */
|
|
|
|
|
bool
|
|
|
|
|
ovsdb_jsonrpc_server_add_db(struct ovsdb_jsonrpc_server *svr, struct ovsdb *db)
|
|
|
|
|
{
|
2013-06-27 10:27:57 -07:00
|
|
|
|
/* The OVSDB protocol doesn't have a way to notify a client that a
|
|
|
|
|
* database has been added. If some client tried to use the database
|
|
|
|
|
* that we're adding and failed, then forcing it to reconnect seems like
|
|
|
|
|
* a reasonable way to make it try again.
|
|
|
|
|
*
|
|
|
|
|
* If this is too big of a hammer in practice, we could be more selective,
|
|
|
|
|
* e.g. disconnect only connections that actually tried to use a database
|
|
|
|
|
* with 'db''s name. */
|
|
|
|
|
ovsdb_jsonrpc_server_reconnect(svr);
|
|
|
|
|
|
2012-09-07 10:07:03 -07:00
|
|
|
|
return ovsdb_server_add_db(&svr->up, db);
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-27 10:27:57 -07:00
|
|
|
|
/* Removes 'db' from the set of databases served out by 'svr'. Returns
|
|
|
|
|
* true if successful, false if there is no database associated with 'db'. */
|
|
|
|
|
bool
|
|
|
|
|
ovsdb_jsonrpc_server_remove_db(struct ovsdb_jsonrpc_server *svr,
|
|
|
|
|
struct ovsdb *db)
|
|
|
|
|
{
|
|
|
|
|
/* There might be pointers to 'db' from 'svr', such as monitors or
|
|
|
|
|
* outstanding transactions. Disconnect all JSON-RPC connections to avoid
|
|
|
|
|
* accesses to freed memory.
|
|
|
|
|
*
|
|
|
|
|
* If this is too big of a hammer in practice, we could be more selective,
|
|
|
|
|
* e.g. disconnect only connections that actually reference 'db'. */
|
|
|
|
|
ovsdb_jsonrpc_server_reconnect(svr);
|
|
|
|
|
|
|
|
|
|
return ovsdb_server_remove_db(&svr->up, db);
|
|
|
|
|
}
|
|
|
|
|
|
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);
|
2011-07-26 10:17:36 -07:00
|
|
|
|
ovsdb_server_destroy(&svr->up);
|
2010-02-02 14:41:00 -08:00
|
|
|
|
free(svr);
|
|
|
|
|
}
|
|
|
|
|
|
2010-11-05 10:22:18 -07:00
|
|
|
|
struct ovsdb_jsonrpc_options *
|
2012-04-11 20:18:34 -07:00
|
|
|
|
ovsdb_jsonrpc_default_options(const char *target)
|
2010-11-05 10:22:18 -07:00
|
|
|
|
{
|
|
|
|
|
struct ovsdb_jsonrpc_options *options = xzalloc(sizeof *options);
|
|
|
|
|
options->max_backoff = RECONNECT_DEFAULT_MAX_BACKOFF;
|
2012-04-11 20:18:34 -07:00
|
|
|
|
options->probe_interval = (stream_or_pstream_needs_probes(target)
|
|
|
|
|
? RECONNECT_DEFAULT_PROBE_INTERVAL
|
|
|
|
|
: 0);
|
2010-11-05 10:22:18 -07:00
|
|
|
|
return options;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Sets 'svr''s current set of remotes to the names in 'new_remotes', with
|
|
|
|
|
* options in the struct ovsdb_jsonrpc_options supplied as the data values.
|
2010-01-04 10:05:51 -08:00
|
|
|
|
*
|
|
|
|
|
* 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) {
|
2015-02-20 08:44:48 -08:00
|
|
|
|
struct ovsdb_jsonrpc_remote *remote = node->data;
|
|
|
|
|
struct ovsdb_jsonrpc_options *options
|
|
|
|
|
= shash_find_data(new_remotes, node->name);
|
|
|
|
|
|
|
|
|
|
if (!options) {
|
2011-07-15 11:25:14 -07:00
|
|
|
|
VLOG_INFO("%s: remote deconfigured", node->name);
|
2010-01-04 10:05:51 -08:00
|
|
|
|
ovsdb_jsonrpc_server_del_remote(node);
|
2015-02-20 08:44:48 -08:00
|
|
|
|
} else if (options->dscp != remote->dscp) {
|
|
|
|
|
ovsdb_jsonrpc_server_del_remote(node);
|
|
|
|
|
}
|
2010-01-04 10:05:51 -08:00
|
|
|
|
}
|
|
|
|
|
SHASH_FOR_EACH (node, new_remotes) {
|
2010-11-05 10:22:18 -07:00
|
|
|
|
const struct ovsdb_jsonrpc_options *options = node->data;
|
|
|
|
|
struct ovsdb_jsonrpc_remote *remote;
|
|
|
|
|
|
|
|
|
|
remote = shash_find_data(&svr->remotes, node->name);
|
|
|
|
|
if (!remote) {
|
2012-03-10 15:58:10 -08:00
|
|
|
|
remote = ovsdb_jsonrpc_server_add_remote(svr, node->name, options);
|
2010-11-05 10:22:18 -07:00
|
|
|
|
if (!remote) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2010-01-04 10:05:51 -08:00
|
|
|
|
}
|
2010-11-05 10:22:18 -07:00
|
|
|
|
|
|
|
|
|
ovsdb_jsonrpc_session_set_all_options(remote, options);
|
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-11-05 10:22:18 -07:00
|
|
|
|
static struct ovsdb_jsonrpc_remote *
|
2010-01-04 10:05:51 -08:00
|
|
|
|
ovsdb_jsonrpc_server_add_remote(struct ovsdb_jsonrpc_server *svr,
|
2012-03-10 15:58:10 -08:00
|
|
|
|
const char *name,
|
|
|
|
|
const struct ovsdb_jsonrpc_options *options)
|
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;
|
|
|
|
|
|
2012-03-10 15:58:10 -08:00
|
|
|
|
error = jsonrpc_pstream_open(name, &listener, options->dscp);
|
2010-01-04 10:05:51 -08:00
|
|
|
|
if (error && error != EAFNOSUPPORT) {
|
2013-06-24 10:54:49 -07:00
|
|
|
|
VLOG_ERR_RL(&rl, "%s: listen failed: %s", name, ovs_strerror(error));
|
2010-11-05 10:22:18 -07:00
|
|
|
|
return NULL;
|
2010-01-04 10:05:51 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
remote = xmalloc(sizeof *remote);
|
|
|
|
|
remote->server = svr;
|
|
|
|
|
remote->listener = listener;
|
|
|
|
|
list_init(&remote->sessions);
|
2012-10-04 12:33:05 -07:00
|
|
|
|
remote->dscp = options->dscp;
|
2010-01-04 10:05:51 -08:00
|
|
|
|
shash_add(&svr->remotes, name, remote);
|
|
|
|
|
|
|
|
|
|
if (!listener) {
|
2013-03-15 16:14:28 -07:00
|
|
|
|
ovsdb_jsonrpc_session_create(remote, jsonrpc_session_open(name, true));
|
2010-01-04 10:05:51 -08:00
|
|
|
|
}
|
2010-11-05 10:22:18 -07:00
|
|
|
|
return remote;
|
2010-01-04 10:05:51 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2011-07-13 16:15:22 -07:00
|
|
|
|
/* 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
|
|
|
|
|
* active connections) returns false. On failure, 'status' will be zeroed.
|
|
|
|
|
*/
|
|
|
|
|
bool
|
|
|
|
|
ovsdb_jsonrpc_server_get_remote_status(
|
|
|
|
|
const struct ovsdb_jsonrpc_server *svr, const char *target,
|
|
|
|
|
struct ovsdb_jsonrpc_remote_status *status)
|
2011-01-28 15:39:55 -08:00
|
|
|
|
{
|
2011-07-13 16:15:22 -07:00
|
|
|
|
const struct ovsdb_jsonrpc_remote *remote;
|
2011-01-28 15:39:55 -08:00
|
|
|
|
|
2011-07-13 16:15:22 -07:00
|
|
|
|
memset(status, 0, sizeof *status);
|
2011-01-28 15:39:55 -08:00
|
|
|
|
|
2011-07-13 16:15:22 -07:00
|
|
|
|
remote = shash_find_data(&svr->remotes, target);
|
|
|
|
|
return remote && ovsdb_jsonrpc_session_get_status(remote, status);
|
2011-01-28 15:39:55 -08:00
|
|
|
|
}
|
|
|
|
|
|
2011-07-26 10:24:17 -07:00
|
|
|
|
void
|
|
|
|
|
ovsdb_jsonrpc_server_free_remote_status(
|
|
|
|
|
struct ovsdb_jsonrpc_remote_status *status)
|
|
|
|
|
{
|
|
|
|
|
free(status->locks_held);
|
|
|
|
|
free(status->locks_waiting);
|
|
|
|
|
free(status->locks_lost);
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
|
|
|
2015-02-23 23:15:23 -08:00
|
|
|
|
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)",
|
2010-01-04 10:05:51 -08:00
|
|
|
|
pstream_get_name(remote->listener),
|
2015-02-23 23:15:23 -08:00
|
|
|
|
svr->max_sessions);
|
2010-01-04 10:05:51 -08:00
|
|
|
|
}
|
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
|
|
|
|
}
|
2012-05-08 15:44:21 -07:00
|
|
|
|
|
|
|
|
|
/* Adds some memory usage statistics for 'svr' into 'usage', for use with
|
|
|
|
|
* memory_report(). */
|
|
|
|
|
void
|
|
|
|
|
ovsdb_jsonrpc_server_get_memory_usage(const struct ovsdb_jsonrpc_server *svr,
|
|
|
|
|
struct simap *usage)
|
|
|
|
|
{
|
|
|
|
|
struct shash_node *node;
|
|
|
|
|
|
|
|
|
|
simap_increase(usage, "sessions", svr->n_sessions);
|
|
|
|
|
SHASH_FOR_EACH (node, &svr->remotes) {
|
|
|
|
|
struct ovsdb_jsonrpc_remote *remote = node->data;
|
|
|
|
|
|
|
|
|
|
ovsdb_jsonrpc_session_get_memory_usage_all(remote, usage);
|
|
|
|
|
}
|
|
|
|
|
}
|
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 {
|
2014-12-15 14:10:38 +01:00
|
|
|
|
struct ovs_list node; /* Element in remote's sessions list. */
|
2011-07-26 10:17:36 -07:00
|
|
|
|
struct ovsdb_session up;
|
2010-01-04 10:05:51 -08:00
|
|
|
|
struct ovsdb_jsonrpc_remote *remote;
|
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. */
|
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 *);
|
2012-05-08 15:44:21 -07:00
|
|
|
|
static void ovsdb_jsonrpc_session_get_memory_usage(
|
|
|
|
|
const struct ovsdb_jsonrpc_session *, struct simap *usage);
|
2009-11-13 14:47:25 -08:00
|
|
|
|
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);
|
2012-09-07 10:07:03 -07:00
|
|
|
|
ovsdb_session_init(&s->up, &remote->server->up);
|
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-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);
|
2011-07-26 10:24:17 -07:00
|
|
|
|
ovsdb_jsonrpc_session_unlock_all(s);
|
2013-01-24 11:33:35 -08:00
|
|
|
|
ovsdb_jsonrpc_trigger_complete_all(s);
|
|
|
|
|
|
|
|
|
|
hmap_destroy(&s->monitors);
|
|
|
|
|
hmap_destroy(&s->triggers);
|
|
|
|
|
|
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--;
|
2011-07-26 10:17:36 -07:00
|
|
|
|
ovsdb_session_destroy(&s->up);
|
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);
|
2011-07-26 10:24:17 -07:00
|
|
|
|
ovsdb_jsonrpc_session_unlock_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
|
|
|
|
|
2014-04-02 07:09:47 -07:00
|
|
|
|
if (!jsonrpc_session_get_backlog(s->js)) {
|
2014-04-02 08:43:17 -07:00
|
|
|
|
struct jsonrpc_msg *msg;
|
|
|
|
|
|
|
|
|
|
ovsdb_jsonrpc_monitor_flush_all(s);
|
|
|
|
|
|
|
|
|
|
msg = jsonrpc_session_recv(s->js);
|
2009-12-17 15:16:43 -08:00
|
|
|
|
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
|
|
|
|
|
2010-11-05 10:22:18 -07:00
|
|
|
|
static void
|
|
|
|
|
ovsdb_jsonrpc_session_set_options(struct ovsdb_jsonrpc_session *session,
|
|
|
|
|
const struct ovsdb_jsonrpc_options *options)
|
|
|
|
|
{
|
|
|
|
|
jsonrpc_session_set_max_backoff(session->js, options->max_backoff);
|
|
|
|
|
jsonrpc_session_set_probe_interval(session->js, options->probe_interval);
|
2012-03-10 15:58:10 -08:00
|
|
|
|
jsonrpc_session_set_dscp(session->js, options->dscp);
|
2010-11-05 10:22:18 -07: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;
|
|
|
|
|
|
2010-09-17 10:33:10 -07:00
|
|
|
|
LIST_FOR_EACH_SAFE (s, next, node, &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)) {
|
2014-04-02 08:43:17 -07:00
|
|
|
|
if (ovsdb_jsonrpc_monitor_needs_flush(s)) {
|
|
|
|
|
poll_immediate_wake();
|
|
|
|
|
} else {
|
|
|
|
|
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-09-17 10:33:10 -07:00
|
|
|
|
LIST_FOR_EACH (s, 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
|
|
|
|
|
2012-05-08 15:44:21 -07:00
|
|
|
|
static void
|
|
|
|
|
ovsdb_jsonrpc_session_get_memory_usage(const struct ovsdb_jsonrpc_session *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));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
ovsdb_jsonrpc_session_get_memory_usage_all(
|
|
|
|
|
const struct ovsdb_jsonrpc_remote *remote,
|
|
|
|
|
struct simap *usage)
|
|
|
|
|
{
|
|
|
|
|
struct ovsdb_jsonrpc_session *s;
|
|
|
|
|
|
|
|
|
|
LIST_FOR_EACH (s, node, &remote->sessions) {
|
|
|
|
|
ovsdb_jsonrpc_session_get_memory_usage(s, usage);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
|
|
|
2010-09-17 10:33:10 -07:00
|
|
|
|
LIST_FOR_EACH_SAFE (s, next, node, &remote->sessions) {
|
2010-01-04 10:05:51 -08:00
|
|
|
|
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;
|
|
|
|
|
|
2010-09-17 10:33:10 -07:00
|
|
|
|
LIST_FOR_EACH_SAFE (s, next, node, &remote->sessions) {
|
2010-06-24 12:56:30 -07:00
|
|
|
|
jsonrpc_session_force_reconnect(s->js);
|
|
|
|
|
if (!jsonrpc_session_is_alive(s->js)) {
|
|
|
|
|
ovsdb_jsonrpc_session_close(s);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-11-05 10:22:18 -07:00
|
|
|
|
/* Sets the options for all of the JSON-RPC sessions managed by 'remote' to
|
2015-02-20 08:44:48 -08:00
|
|
|
|
* 'options'.
|
|
|
|
|
*
|
|
|
|
|
* (The dscp value can't be changed directly; the caller must instead close and
|
|
|
|
|
* re-open the session.) */
|
2010-11-05 10:22:18 -07:00
|
|
|
|
static void
|
|
|
|
|
ovsdb_jsonrpc_session_set_all_options(
|
|
|
|
|
struct ovsdb_jsonrpc_remote *remote,
|
|
|
|
|
const struct ovsdb_jsonrpc_options *options)
|
|
|
|
|
{
|
|
|
|
|
struct ovsdb_jsonrpc_session *s;
|
|
|
|
|
|
|
|
|
|
LIST_FOR_EACH (s, node, &remote->sessions) {
|
|
|
|
|
ovsdb_jsonrpc_session_set_options(s, options);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-07-13 16:15:22 -07:00
|
|
|
|
static bool
|
2011-01-28 15:39:55 -08:00
|
|
|
|
ovsdb_jsonrpc_session_get_status(const struct ovsdb_jsonrpc_remote *remote,
|
2011-07-13 16:15:22 -07:00
|
|
|
|
struct ovsdb_jsonrpc_remote_status *status)
|
2011-01-28 15:39:55 -08:00
|
|
|
|
{
|
|
|
|
|
const struct ovsdb_jsonrpc_session *s;
|
|
|
|
|
const struct jsonrpc_session *js;
|
2011-07-26 10:24:17 -07:00
|
|
|
|
struct ovsdb_lock_waiter *waiter;
|
2011-01-28 15:39:55 -08:00
|
|
|
|
struct reconnect_stats rstats;
|
2011-07-26 10:24:17 -07:00
|
|
|
|
struct ds locks_held, locks_waiting, locks_lost;
|
2011-01-28 15:39:55 -08:00
|
|
|
|
|
2013-04-18 16:37:05 -07:00
|
|
|
|
status->bound_port = (remote->listener
|
|
|
|
|
? pstream_get_bound_port(remote->listener)
|
|
|
|
|
: htons(0));
|
|
|
|
|
|
2011-01-28 15:39:55 -08:00
|
|
|
|
if (list_is_empty(&remote->sessions)) {
|
2011-07-13 16:15:22 -07:00
|
|
|
|
return false;
|
2011-01-28 15:39:55 -08:00
|
|
|
|
}
|
|
|
|
|
s = CONTAINER_OF(remote->sessions.next, struct ovsdb_jsonrpc_session, node);
|
|
|
|
|
js = s->js;
|
|
|
|
|
|
|
|
|
|
status->is_connected = jsonrpc_session_is_connected(js);
|
|
|
|
|
status->last_error = jsonrpc_session_get_status(js);
|
|
|
|
|
|
|
|
|
|
jsonrpc_session_get_reconnect_stats(js, &rstats);
|
|
|
|
|
status->state = rstats.state;
|
2011-03-14 13:10:02 -07:00
|
|
|
|
status->sec_since_connect = rstats.msec_since_connect == UINT_MAX
|
|
|
|
|
? UINT_MAX : rstats.msec_since_connect / 1000;
|
|
|
|
|
status->sec_since_disconnect = rstats.msec_since_disconnect == UINT_MAX
|
|
|
|
|
? UINT_MAX : rstats.msec_since_disconnect / 1000;
|
2011-07-13 16:15:22 -07:00
|
|
|
|
|
2011-07-26 10:24:17 -07:00
|
|
|
|
ds_init(&locks_held);
|
|
|
|
|
ds_init(&locks_waiting);
|
|
|
|
|
ds_init(&locks_lost);
|
|
|
|
|
HMAP_FOR_EACH (waiter, session_node, &s->up.waiters) {
|
|
|
|
|
struct ds *string;
|
|
|
|
|
|
|
|
|
|
string = (ovsdb_lock_waiter_is_owner(waiter) ? &locks_held
|
|
|
|
|
: waiter->mode == OVSDB_LOCK_WAIT ? &locks_waiting
|
|
|
|
|
: &locks_lost);
|
|
|
|
|
if (string->length) {
|
|
|
|
|
ds_put_char(string, ' ');
|
|
|
|
|
}
|
|
|
|
|
ds_put_cstr(string, waiter->lock_name);
|
|
|
|
|
}
|
|
|
|
|
status->locks_held = ds_steal_cstr(&locks_held);
|
|
|
|
|
status->locks_waiting = ds_steal_cstr(&locks_waiting);
|
|
|
|
|
status->locks_lost = ds_steal_cstr(&locks_lost);
|
|
|
|
|
|
2011-07-13 16:08:37 -07:00
|
|
|
|
status->n_connections = list_size(&remote->sessions);
|
|
|
|
|
|
2011-07-13 16:15:22 -07:00
|
|
|
|
return true;
|
2011-01-28 15:39:55 -08:00
|
|
|
|
}
|
|
|
|
|
|
2012-09-07 10:07:03 -07:00
|
|
|
|
/* Examines 'request' to determine the database to which it relates, and then
|
|
|
|
|
* searches 's' to find that database:
|
|
|
|
|
*
|
|
|
|
|
* - If successful, returns the database and sets '*replyp' to NULL.
|
|
|
|
|
*
|
|
|
|
|
* - If no such database exists, returns NULL and sets '*replyp' to an
|
|
|
|
|
* appropriate JSON-RPC error reply, owned by the caller. */
|
|
|
|
|
static struct ovsdb *
|
|
|
|
|
ovsdb_jsonrpc_lookup_db(const struct ovsdb_jsonrpc_session *s,
|
|
|
|
|
const struct jsonrpc_msg *request,
|
|
|
|
|
struct jsonrpc_msg **replyp)
|
2010-02-09 10:17:58 -08:00
|
|
|
|
{
|
|
|
|
|
struct json_array *params;
|
|
|
|
|
struct ovsdb_error *error;
|
2012-09-07 10:07:03 -07:00
|
|
|
|
const char *db_name;
|
|
|
|
|
struct ovsdb *db;
|
2010-02-09 10:17:58 -08:00
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-07 10:07:03 -07:00
|
|
|
|
db_name = params->elems[0]->u.string;
|
|
|
|
|
db = shash_find_data(&s->up.server->dbs, db_name);
|
|
|
|
|
if (!db) {
|
2010-02-09 10:17:58 -08:00
|
|
|
|
error = ovsdb_syntax_error(
|
|
|
|
|
request->params, "unknown database",
|
|
|
|
|
"%s request specifies unknown database %s",
|
2012-09-07 10:07:03 -07:00
|
|
|
|
request->method, db_name);
|
2010-02-09 10:17:58 -08:00
|
|
|
|
goto error;
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-07 10:07:03 -07:00
|
|
|
|
*replyp = NULL;
|
|
|
|
|
return db;
|
2010-02-09 10:17:58 -08:00
|
|
|
|
|
|
|
|
|
error:
|
2015-03-19 21:00:46 -07:00
|
|
|
|
*replyp = jsonrpc_create_error(ovsdb_error_to_json(error), request->id);
|
2010-02-09 10:17:58 -08:00
|
|
|
|
ovsdb_error_destroy(error);
|
2012-09-07 10:07:03 -07:00
|
|
|
|
return NULL;
|
2010-02-09 10:17:58 -08:00
|
|
|
|
}
|
|
|
|
|
|
2011-07-26 10:24:17 -07:00
|
|
|
|
static struct ovsdb_error *
|
|
|
|
|
ovsdb_jsonrpc_session_parse_lock_name(const struct jsonrpc_msg *request,
|
|
|
|
|
const char **lock_namep)
|
|
|
|
|
{
|
|
|
|
|
const struct json_array *params;
|
|
|
|
|
|
|
|
|
|
params = json_array(request->params);
|
|
|
|
|
if (params->n != 1 || params->elems[0]->type != JSON_STRING ||
|
|
|
|
|
!ovsdb_parser_is_id(json_string(params->elems[0]))) {
|
|
|
|
|
*lock_namep = NULL;
|
|
|
|
|
return ovsdb_syntax_error(request->params, NULL,
|
|
|
|
|
"%s request params must be <id>",
|
|
|
|
|
request->method);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
*lock_namep = json_string(params->elems[0]);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
ovsdb_jsonrpc_session_notify(struct ovsdb_session *session,
|
|
|
|
|
const char *lock_name,
|
|
|
|
|
const char *method)
|
|
|
|
|
{
|
|
|
|
|
struct ovsdb_jsonrpc_session *s;
|
|
|
|
|
struct json *params;
|
|
|
|
|
|
|
|
|
|
s = CONTAINER_OF(session, struct ovsdb_jsonrpc_session, up);
|
|
|
|
|
params = json_array_create_1(json_string_create(lock_name));
|
2014-04-02 08:43:17 -07:00
|
|
|
|
ovsdb_jsonrpc_session_send(s, jsonrpc_create_notify(method, params));
|
2011-07-26 10:24:17 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static struct jsonrpc_msg *
|
|
|
|
|
ovsdb_jsonrpc_session_lock(struct ovsdb_jsonrpc_session *s,
|
|
|
|
|
struct jsonrpc_msg *request,
|
|
|
|
|
enum ovsdb_lock_mode mode)
|
|
|
|
|
{
|
|
|
|
|
struct ovsdb_lock_waiter *waiter;
|
|
|
|
|
struct jsonrpc_msg *reply;
|
|
|
|
|
struct ovsdb_error *error;
|
|
|
|
|
struct ovsdb_session *victim;
|
|
|
|
|
const char *lock_name;
|
|
|
|
|
struct json *result;
|
|
|
|
|
|
|
|
|
|
error = ovsdb_jsonrpc_session_parse_lock_name(request, &lock_name);
|
|
|
|
|
if (error) {
|
|
|
|
|
goto error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Report error if this session has issued a "lock" or "steal" without a
|
|
|
|
|
* matching "unlock" for this lock. */
|
|
|
|
|
waiter = ovsdb_session_get_lock_waiter(&s->up, lock_name);
|
|
|
|
|
if (waiter) {
|
|
|
|
|
error = ovsdb_syntax_error(
|
|
|
|
|
request->params, NULL,
|
|
|
|
|
"must issue \"unlock\" before new \"%s\"", request->method);
|
|
|
|
|
goto error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Get the lock, add us as a waiter. */
|
|
|
|
|
waiter = ovsdb_server_lock(&s->remote->server->up, &s->up, lock_name, mode,
|
|
|
|
|
&victim);
|
|
|
|
|
if (victim) {
|
|
|
|
|
ovsdb_jsonrpc_session_notify(victim, lock_name, "stolen");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result = json_object_create();
|
|
|
|
|
json_object_put(result, "locked",
|
|
|
|
|
json_boolean_create(ovsdb_lock_waiter_is_owner(waiter)));
|
|
|
|
|
|
|
|
|
|
return jsonrpc_create_reply(result, request->id);
|
|
|
|
|
|
|
|
|
|
error:
|
2015-03-19 21:00:46 -07:00
|
|
|
|
reply = jsonrpc_create_error(ovsdb_error_to_json(error), request->id);
|
2011-07-26 10:24:17 -07:00
|
|
|
|
ovsdb_error_destroy(error);
|
|
|
|
|
return reply;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
ovsdb_jsonrpc_session_unlock_all(struct ovsdb_jsonrpc_session *s)
|
|
|
|
|
{
|
|
|
|
|
struct ovsdb_lock_waiter *waiter, *next;
|
|
|
|
|
|
|
|
|
|
HMAP_FOR_EACH_SAFE (waiter, next, session_node, &s->up.waiters) {
|
|
|
|
|
ovsdb_jsonrpc_session_unlock__(waiter);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
ovsdb_jsonrpc_session_unlock__(struct ovsdb_lock_waiter *waiter)
|
|
|
|
|
{
|
|
|
|
|
struct ovsdb_lock *lock = waiter->lock;
|
|
|
|
|
|
|
|
|
|
if (lock) {
|
|
|
|
|
struct ovsdb_session *new_owner = ovsdb_lock_waiter_remove(waiter);
|
|
|
|
|
if (new_owner) {
|
|
|
|
|
ovsdb_jsonrpc_session_notify(new_owner, lock->name, "locked");
|
|
|
|
|
} else {
|
|
|
|
|
/* ovsdb_server_lock() might have freed 'lock'. */
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ovsdb_lock_waiter_destroy(waiter);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static struct jsonrpc_msg *
|
|
|
|
|
ovsdb_jsonrpc_session_unlock(struct ovsdb_jsonrpc_session *s,
|
|
|
|
|
struct jsonrpc_msg *request)
|
|
|
|
|
{
|
|
|
|
|
struct ovsdb_lock_waiter *waiter;
|
|
|
|
|
struct jsonrpc_msg *reply;
|
|
|
|
|
struct ovsdb_error *error;
|
|
|
|
|
const char *lock_name;
|
|
|
|
|
|
|
|
|
|
error = ovsdb_jsonrpc_session_parse_lock_name(request, &lock_name);
|
|
|
|
|
if (error) {
|
|
|
|
|
goto error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Report error if this session has not issued a "lock" or "steal" for this
|
|
|
|
|
* lock. */
|
|
|
|
|
waiter = ovsdb_session_get_lock_waiter(&s->up, lock_name);
|
|
|
|
|
if (!waiter) {
|
|
|
|
|
error = ovsdb_syntax_error(
|
|
|
|
|
request->params, NULL, "\"unlock\" without \"lock\" or \"steal\"");
|
|
|
|
|
goto error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ovsdb_jsonrpc_session_unlock__(waiter);
|
|
|
|
|
|
|
|
|
|
return jsonrpc_create_reply(json_object_create(), request->id);
|
|
|
|
|
|
|
|
|
|
error:
|
2015-03-19 21:00:46 -07:00
|
|
|
|
reply = jsonrpc_create_error(ovsdb_error_to_json(error), request->id);
|
2011-07-26 10:24:17 -07:00
|
|
|
|
ovsdb_error_destroy(error);
|
|
|
|
|
return reply;
|
|
|
|
|
}
|
|
|
|
|
|
2009-11-13 14:47:25 -08:00
|
|
|
|
static struct jsonrpc_msg *
|
2012-09-07 10:07:03 -07:00
|
|
|
|
execute_transaction(struct ovsdb_jsonrpc_session *s, struct ovsdb *db,
|
2009-11-13 14:47:25 -08:00
|
|
|
|
struct jsonrpc_msg *request)
|
|
|
|
|
{
|
2012-09-07 10:07:03 -07:00
|
|
|
|
ovsdb_jsonrpc_trigger_create(s, db, 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")) {
|
2012-09-07 10:07:03 -07:00
|
|
|
|
struct ovsdb *db = ovsdb_jsonrpc_lookup_db(s, request, &reply);
|
2010-02-09 10:17:58 -08:00
|
|
|
|
if (!reply) {
|
2012-09-07 10:07:03 -07:00
|
|
|
|
reply = execute_transaction(s, db, request);
|
2010-02-09 10:17:58 -08:00
|
|
|
|
}
|
2009-11-16 10:38:14 -08:00
|
|
|
|
} else if (!strcmp(request->method, "monitor")) {
|
2012-09-07 10:07:03 -07:00
|
|
|
|
struct ovsdb *db = ovsdb_jsonrpc_lookup_db(s, request, &reply);
|
2010-02-09 10:17:58 -08:00
|
|
|
|
if (!reply) {
|
2015-03-19 21:00:46 -07:00
|
|
|
|
reply = ovsdb_jsonrpc_monitor_create(s, db, request->params,
|
|
|
|
|
request->id);
|
2010-02-09 10:17:58 -08:00
|
|
|
|
}
|
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")) {
|
2012-09-07 10:07:03 -07:00
|
|
|
|
struct ovsdb *db = ovsdb_jsonrpc_lookup_db(s, request, &reply);
|
2010-02-09 10:17:58 -08:00
|
|
|
|
if (!reply) {
|
2012-09-07 10:07:03 -07:00
|
|
|
|
reply = jsonrpc_create_reply(ovsdb_schema_to_json(db->schema),
|
|
|
|
|
request->id);
|
2010-02-09 10:17:58 -08:00
|
|
|
|
}
|
|
|
|
|
} else if (!strcmp(request->method, "list_dbs")) {
|
2012-09-07 10:07:03 -07:00
|
|
|
|
size_t n_dbs = shash_count(&s->up.server->dbs);
|
|
|
|
|
struct shash_node *node;
|
|
|
|
|
struct json **dbs;
|
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
|
|
dbs = xmalloc(n_dbs * sizeof *dbs);
|
|
|
|
|
i = 0;
|
|
|
|
|
SHASH_FOR_EACH (node, &s->up.server->dbs) {
|
|
|
|
|
dbs[i++] = json_string_create(node->name);
|
|
|
|
|
}
|
|
|
|
|
reply = jsonrpc_create_reply(json_array_create(dbs, n_dbs),
|
|
|
|
|
request->id);
|
2011-07-26 10:24:17 -07:00
|
|
|
|
} else if (!strcmp(request->method, "lock")) {
|
|
|
|
|
reply = ovsdb_jsonrpc_session_lock(s, request, OVSDB_LOCK_WAIT);
|
|
|
|
|
} else if (!strcmp(request->method, "steal")) {
|
|
|
|
|
reply = ovsdb_jsonrpc_session_lock(s, request, OVSDB_LOCK_STEAL);
|
|
|
|
|
} else if (!strcmp(request->method, "unlock")) {
|
|
|
|
|
reply = ovsdb_jsonrpc_session_unlock(s, request);
|
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);
|
2014-04-02 08:43:17 -07:00
|
|
|
|
ovsdb_jsonrpc_session_send(s, 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);
|
|
|
|
|
}
|
2014-04-02 08:43:17 -07:00
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
ovsdb_jsonrpc_session_send(struct ovsdb_jsonrpc_session *s,
|
|
|
|
|
struct jsonrpc_msg *msg)
|
|
|
|
|
{
|
|
|
|
|
ovsdb_jsonrpc_monitor_flush_all(s);
|
|
|
|
|
jsonrpc_session_send(s->js, msg);
|
|
|
|
|
}
|
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 hmap_node hmap_node; /* In session's "triggers" hmap. */
|
|
|
|
|
struct json *id;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static void
|
2012-09-07 10:07:03 -07:00
|
|
|
|
ovsdb_jsonrpc_trigger_create(struct ovsdb_jsonrpc_session *s, struct ovsdb *db,
|
2009-11-13 14:47:25 -08:00
|
|
|
|
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);
|
2014-04-02 08:43:17 -07:00
|
|
|
|
ovsdb_jsonrpc_session_send(s, msg);
|
2009-11-13 14:47:25 -08:00
|
|
|
|
json_destroy(id);
|
|
|
|
|
json_destroy(params);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Insert into trigger table. */
|
|
|
|
|
t = xmalloc(sizeof *t);
|
2012-09-07 10:07:03 -07:00
|
|
|
|
ovsdb_trigger_init(&s->up, db, &t->trigger, params, time_msec());
|
2009-11-13 14:47:25 -08:00
|
|
|
|
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;
|
|
|
|
|
|
2010-09-17 10:33:10 -07:00
|
|
|
|
HMAP_FOR_EACH_WITH_HASH (t, hmap_node, hash, &s->triggers) {
|
2009-11-13 14:47:25 -08:00
|
|
|
|
if (json_equal(t->id, id)) {
|
|
|
|
|
return t;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
ovsdb_jsonrpc_trigger_complete(struct ovsdb_jsonrpc_trigger *t)
|
|
|
|
|
{
|
2011-07-26 10:17:36 -07:00
|
|
|
|
struct ovsdb_jsonrpc_session *s;
|
|
|
|
|
|
|
|
|
|
s = CONTAINER_OF(t->trigger.session, struct ovsdb_jsonrpc_session, up);
|
2009-11-13 14:47:25 -08:00
|
|
|
|
|
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);
|
|
|
|
|
}
|
2014-04-02 08:43:17 -07:00
|
|
|
|
ovsdb_jsonrpc_session_send(s, 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;
|
2010-09-17 10:33:10 -07:00
|
|
|
|
HMAP_FOR_EACH_SAFE (t, next, hmap_node, &s->triggers) {
|
2009-11-13 14:47:25 -08:00
|
|
|
|
ovsdb_jsonrpc_trigger_complete(t);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
ovsdb_jsonrpc_trigger_complete_done(struct ovsdb_jsonrpc_session *s)
|
|
|
|
|
{
|
2011-07-26 10:17:36 -07:00
|
|
|
|
while (!list_is_empty(&s->up.completions)) {
|
2009-11-13 14:47:25 -08:00
|
|
|
|
struct ovsdb_jsonrpc_trigger *t
|
2011-07-26 10:17:36 -07:00
|
|
|
|
= CONTAINER_OF(s->up.completions.next,
|
2009-11-13 14:47:25 -08:00
|
|
|
|
struct ovsdb_jsonrpc_trigger, trigger.node);
|
|
|
|
|
ovsdb_jsonrpc_trigger_complete(t);
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-11-16 10:38:14 -08:00
|
|
|
|
|
2015-03-11 16:50:41 -07:00
|
|
|
|
/* database table monitors. */
|
2009-11-16 10:38:14 -08:00
|
|
|
|
|
2015-03-11 16:50:41 -07:00
|
|
|
|
enum ovsdb_monitor_selection {
|
2009-11-16 10:38:14 -08:00
|
|
|
|
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. */
|
2015-03-11 16:50:41 -07:00
|
|
|
|
struct ovsdb_monitor_column {
|
2010-07-01 09:47:46 -07:00
|
|
|
|
const struct ovsdb_column *column;
|
2015-03-11 16:50:41 -07:00
|
|
|
|
enum ovsdb_monitor_selection select;
|
2010-07-01 09:47:46 -07:00
|
|
|
|
};
|
|
|
|
|
|
2014-04-01 16:26:47 -07:00
|
|
|
|
/* A row that has changed in a monitored table. */
|
2015-03-11 16:50:41 -07:00
|
|
|
|
struct ovsdb_monitor_row {
|
2014-04-01 16:26:47 -07:00
|
|
|
|
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. */
|
|
|
|
|
};
|
|
|
|
|
|
2010-07-01 09:47:46 -07:00
|
|
|
|
/* A particular table being monitored. */
|
2015-03-11 16:50:41 -07:00
|
|
|
|
struct ovsdb_monitor_table {
|
2009-11-16 10:38:14 -08:00
|
|
|
|
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. */
|
2015-03-11 16:50:41 -07:00
|
|
|
|
enum ovsdb_monitor_selection select;
|
2010-07-01 09:47:46 -07:00
|
|
|
|
|
|
|
|
|
/* Columns being monitored. */
|
2015-03-11 16:50:41 -07:00
|
|
|
|
struct ovsdb_monitor_column *columns;
|
2010-07-01 09:47:46 -07:00
|
|
|
|
size_t n_columns;
|
2014-04-01 16:26:47 -07:00
|
|
|
|
|
2015-03-11 16:50:41 -07:00
|
|
|
|
/* Contains 'struct ovsdb_monitor_row's for rows that have been
|
2014-04-01 16:26:47 -07:00
|
|
|
|
* updated but not yet flushed to the jsonrpc connection. */
|
|
|
|
|
struct hmap changes;
|
2009-11-16 10:38:14 -08:00
|
|
|
|
};
|
|
|
|
|
|
2015-03-11 16:50:41 -07:00
|
|
|
|
struct ovsdb_jsonrpc_monitor;
|
|
|
|
|
/* Backend monitor.
|
|
|
|
|
*
|
|
|
|
|
* ovsdb_monitor keep track of the ovsdb changes.
|
|
|
|
|
*/
|
2010-07-01 09:47:46 -07:00
|
|
|
|
/* A collection of tables being monitored. */
|
2015-03-11 16:50:41 -07:00
|
|
|
|
struct ovsdb_monitor {
|
2009-11-16 10:38:14 -08:00
|
|
|
|
struct ovsdb_replica replica;
|
2015-03-11 16:50:41 -07:00
|
|
|
|
struct shash tables; /* Holds "struct ovsdb_monitor_table"s. */
|
|
|
|
|
struct ovsdb_jsonrpc_monitor *jsonrpc_monitor;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/* Jsonrpc front end monitor. */
|
|
|
|
|
struct ovsdb_jsonrpc_monitor {
|
2009-11-16 10:38:14 -08:00
|
|
|
|
struct ovsdb_jsonrpc_session *session;
|
2012-09-07 10:07:03 -07:00
|
|
|
|
struct ovsdb *db;
|
2009-11-16 10:38:14 -08:00
|
|
|
|
struct hmap_node node; /* In ovsdb_jsonrpc_session's "monitors". */
|
|
|
|
|
|
|
|
|
|
struct json *monitor_id;
|
2015-03-11 16:50:41 -07:00
|
|
|
|
struct ovsdb_monitor *dbmon;
|
2009-11-16 10:38:14 -08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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);
|
2015-03-11 16:50:41 -07:00
|
|
|
|
static void ovsdb_monitor_destroy(struct ovsdb_replica *);
|
2009-11-16 10:38:14 -08:00
|
|
|
|
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;
|
|
|
|
|
|
2010-09-17 10:33:10 -07:00
|
|
|
|
HMAP_FOR_EACH_WITH_HASH (m, node, json_hash(monitor_id, 0), &s->monitors) {
|
2009-11-16 10:38:14 -08:00
|
|
|
|
if (json_equal(m->monitor_id, monitor_id)) {
|
|
|
|
|
return m;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-01 09:47:46 -07:00
|
|
|
|
static void
|
2015-03-11 19:05:49 -07:00
|
|
|
|
ovsdb_monitor_add_column(struct ovsdb_monitor *dbmon,
|
|
|
|
|
const struct ovsdb_table *table,
|
2015-03-11 16:50:41 -07:00
|
|
|
|
const struct ovsdb_column *column,
|
|
|
|
|
enum ovsdb_monitor_selection select,
|
|
|
|
|
size_t *allocated_columns)
|
2010-07-01 09:47:46 -07:00
|
|
|
|
{
|
2015-03-11 19:05:49 -07:00
|
|
|
|
struct ovsdb_monitor_table *mt;
|
2015-03-11 16:50:41 -07:00
|
|
|
|
struct ovsdb_monitor_column *c;
|
2010-07-01 09:47:46 -07:00
|
|
|
|
|
2015-03-11 19:05:49 -07:00
|
|
|
|
mt = shash_find_data(&dbmon->tables, table->schema->name);
|
|
|
|
|
|
2010-07-01 09:47:46 -07:00
|
|
|
|
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
|
2015-03-11 16:50:41 -07:00
|
|
|
|
compare_ovsdb_monitor_column(const void *a_, const void *b_)
|
2010-07-01 09:47:46 -07:00
|
|
|
|
{
|
2015-03-11 16:50:41 -07:00
|
|
|
|
const struct ovsdb_monitor_column *a = a_;
|
|
|
|
|
const struct ovsdb_monitor_column *b = b_;
|
2010-07-01 09:47:46 -07:00
|
|
|
|
|
|
|
|
|
return a->column < b->column ? -1 : a->column > b->column;
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-11 18:02:53 -07:00
|
|
|
|
static void
|
2015-03-11 19:05:49 -07:00
|
|
|
|
ovsdb_monitor_table_add_select(struct ovsdb_monitor *dbmon,
|
|
|
|
|
const struct ovsdb_table *table,
|
|
|
|
|
enum ovsdb_monitor_selection select)
|
2015-03-11 18:02:53 -07:00
|
|
|
|
{
|
2015-03-11 19:05:49 -07:00
|
|
|
|
struct ovsdb_monitor_table * mt;
|
|
|
|
|
mt = shash_find_data(&dbmon->tables, table->schema->name);
|
2015-03-11 18:02:53 -07:00
|
|
|
|
mt->select |= select;
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-15 14:10:38 +01:00
|
|
|
|
static struct ovsdb_error * OVS_WARN_UNUSED_RESULT
|
2015-03-11 18:57:55 -07:00
|
|
|
|
ovsdb_jsonrpc_parse_monitor_request(struct ovsdb_monitor *dbmon,
|
|
|
|
|
const struct ovsdb_table *table,
|
2010-07-01 09:47:46 -07:00
|
|
|
|
const struct json *monitor_request,
|
|
|
|
|
size_t *allocated_columns)
|
|
|
|
|
{
|
2015-03-11 18:57:55 -07:00
|
|
|
|
const struct ovsdb_table_schema *ts = table->schema;
|
2015-03-11 16:50:41 -07:00
|
|
|
|
enum ovsdb_monitor_selection select;
|
2010-07-01 09:47:46 -07:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-11 19:05:49 -07:00
|
|
|
|
ovsdb_monitor_table_add_select(dbmon, table, select);
|
2010-07-01 09:47:46 -07:00
|
|
|
|
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;
|
2015-03-11 19:05:49 -07:00
|
|
|
|
column = shash_find_data(&table->schema->columns, s);
|
2010-07-01 09:47:46 -07:00
|
|
|
|
if (!column) {
|
|
|
|
|
return ovsdb_syntax_error(columns, NULL, "%s is not a valid "
|
|
|
|
|
"column name", s);
|
|
|
|
|
}
|
2015-03-11 19:05:49 -07:00
|
|
|
|
ovsdb_monitor_add_column(dbmon, table, column, select,
|
|
|
|
|
allocated_columns);
|
2010-07-01 09:47:46 -07:00
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
struct shash_node *node;
|
|
|
|
|
|
|
|
|
|
SHASH_FOR_EACH (node, &ts->columns) {
|
|
|
|
|
const struct ovsdb_column *column = node->data;
|
|
|
|
|
if (column->index != OVSDB_COL_UUID) {
|
2015-03-11 19:05:49 -07:00
|
|
|
|
ovsdb_monitor_add_column(dbmon, table, column, select,
|
2015-03-11 16:50:41 -07:00
|
|
|
|
allocated_columns);
|
2010-07-01 09:47:46 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-11 16:50:41 -07:00
|
|
|
|
static struct ovsdb_monitor *
|
|
|
|
|
ovsdb_monitor_create(struct ovsdb *db,
|
|
|
|
|
struct ovsdb_jsonrpc_monitor *jsonrpc_monitor,
|
|
|
|
|
const struct ovsdb_replica_class *replica_class)
|
|
|
|
|
{
|
|
|
|
|
struct ovsdb_monitor *m;
|
|
|
|
|
|
|
|
|
|
m = xzalloc(sizeof *m);
|
|
|
|
|
|
|
|
|
|
ovsdb_replica_init(&m->replica, replica_class);
|
|
|
|
|
ovsdb_add_replica(db, &m->replica);
|
|
|
|
|
m->jsonrpc_monitor = jsonrpc_monitor;
|
|
|
|
|
shash_init(&m->tables);
|
|
|
|
|
|
|
|
|
|
return m;
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-11 18:57:55 -07:00
|
|
|
|
static void
|
|
|
|
|
ovsdb_monitor_add_table(struct ovsdb_monitor *m,
|
|
|
|
|
const struct ovsdb_table *table)
|
|
|
|
|
{
|
|
|
|
|
struct ovsdb_monitor_table *mt;
|
|
|
|
|
|
|
|
|
|
mt = xzalloc(sizeof *mt);
|
|
|
|
|
mt->table = table;
|
|
|
|
|
hmap_init(&mt->changes);
|
|
|
|
|
shash_add(&m->tables, table->schema->name, mt);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Check for duplicated column names. Return the first
|
|
|
|
|
* duplicated column's name if found. Otherwise return
|
|
|
|
|
* NULL. */
|
|
|
|
|
static const char * OVS_WARN_UNUSED_RESULT
|
|
|
|
|
ovsdb_monitor_table_check_duplicates(struct ovsdb_monitor *m,
|
|
|
|
|
const struct ovsdb_table *table)
|
|
|
|
|
{
|
|
|
|
|
struct ovsdb_monitor_table *mt;
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
mt = shash_find_data(&m->tables, table->schema->name);
|
|
|
|
|
|
|
|
|
|
if (mt) {
|
|
|
|
|
/* Check for duplicate columns. */
|
|
|
|
|
qsort(mt->columns, mt->n_columns, sizeof *mt->columns,
|
|
|
|
|
compare_ovsdb_monitor_column);
|
|
|
|
|
for (i = 1; i < mt->n_columns; i++) {
|
|
|
|
|
if (mt->columns[i].column == mt->columns[i - 1].column) {
|
|
|
|
|
return mt->columns[i].column->name;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-19 21:00:46 -07:00
|
|
|
|
static struct jsonrpc_msg *
|
2012-09-07 10:07:03 -07:00
|
|
|
|
ovsdb_jsonrpc_monitor_create(struct ovsdb_jsonrpc_session *s, struct ovsdb *db,
|
2015-03-19 21:00:46 -07:00
|
|
|
|
struct json *params,
|
|
|
|
|
const struct json *request_id)
|
2009-11-16 10:38:14 -08:00
|
|
|
|
{
|
|
|
|
|
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);
|
|
|
|
|
m->session = s;
|
2012-09-07 10:07:03 -07:00
|
|
|
|
m->db = db;
|
2015-03-11 16:50:41 -07:00
|
|
|
|
m->dbmon = ovsdb_monitor_create(db, m, &ovsdb_jsonrpc_replica_class);
|
2009-11-16 10:38:14 -08:00
|
|
|
|
hmap_insert(&s->monitors, &m->node, json_hash(monitor_id, 0));
|
|
|
|
|
m->monitor_id = json_clone(monitor_id);
|
|
|
|
|
|
|
|
|
|
SHASH_FOR_EACH (node, json_object(monitor_requests)) {
|
|
|
|
|
const struct ovsdb_table *table;
|
2015-03-11 18:57:55 -07:00
|
|
|
|
const char *column_name;
|
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
|
|
|
|
|
2012-09-07 10:07:03 -07:00
|
|
|
|
table = ovsdb_get_table(m->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;
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-11 18:57:55 -07:00
|
|
|
|
ovsdb_monitor_add_table(m->dbmon, table);
|
2009-11-16 10:38:14 -08:00
|
|
|
|
|
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(
|
2015-03-11 18:57:55 -07:00
|
|
|
|
m->dbmon, table, array->elems[i], &allocated_columns);
|
2010-07-01 09:47:46 -07:00
|
|
|
|
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(
|
2015-03-11 18:57:55 -07:00
|
|
|
|
m->dbmon, table, mr_value, &allocated_columns);
|
2010-07-01 09:47:46 -07:00
|
|
|
|
if (error) {
|
|
|
|
|
goto error;
|
2009-11-16 10:38:14 -08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-11 18:57:55 -07:00
|
|
|
|
column_name = ovsdb_monitor_table_check_duplicates(m->dbmon, table);
|
|
|
|
|
|
|
|
|
|
if (column_name) {
|
|
|
|
|
error = ovsdb_syntax_error(mr_value, NULL, "column %s "
|
|
|
|
|
"mentioned more than once",
|
|
|
|
|
column_name);
|
|
|
|
|
goto error;
|
2009-11-16 10:38:14 -08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-19 21:00:46 -07:00
|
|
|
|
return jsonrpc_create_reply(ovsdb_jsonrpc_monitor_get_initial(m),
|
|
|
|
|
request_id);
|
2009-11-16 10:38:14 -08:00
|
|
|
|
|
|
|
|
|
error:
|
2009-11-18 11:32:16 -08:00
|
|
|
|
if (m) {
|
2015-03-11 16:50:41 -07:00
|
|
|
|
ovsdb_remove_replica(m->db, &m->dbmon->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);
|
2015-03-19 21:00:46 -07:00
|
|
|
|
return jsonrpc_create_error(json, request_id);
|
2009-11-16 10:38:14 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 {
|
2015-03-11 16:50:41 -07:00
|
|
|
|
ovsdb_remove_replica(m->db, &m->dbmon->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;
|
|
|
|
|
|
2010-09-17 10:33:10 -07:00
|
|
|
|
HMAP_FOR_EACH_SAFE (m, next, node, &s->monitors) {
|
2015-03-11 16:50:41 -07:00
|
|
|
|
ovsdb_remove_replica(m->db, &m->dbmon->replica);
|
2009-11-16 10:38:14 -08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-11 16:50:41 -07:00
|
|
|
|
static struct ovsdb_monitor *
|
|
|
|
|
ovsdb_monitor_cast(struct ovsdb_replica *replica)
|
2009-11-16 10:38:14 -08:00
|
|
|
|
{
|
2012-11-06 13:14:55 -08:00
|
|
|
|
ovs_assert(replica->class == &ovsdb_jsonrpc_replica_class);
|
2015-03-11 16:50:41 -07:00
|
|
|
|
return CONTAINER_OF(replica, struct ovsdb_monitor, replica);
|
2009-11-16 10:38:14 -08:00
|
|
|
|
}
|
|
|
|
|
|
2015-03-11 16:50:41 -07:00
|
|
|
|
struct ovsdb_monitor_aux {
|
|
|
|
|
const struct ovsdb_monitor *monitor;
|
|
|
|
|
struct ovsdb_monitor_table *mt;
|
2009-11-16 10:38:14 -08:00
|
|
|
|
};
|
|
|
|
|
|
2015-03-11 16:50:41 -07:00
|
|
|
|
/* Finds and returns the ovsdb_monitor_row in 'mt->changes' for the
|
2014-04-01 16:26:47 -07:00
|
|
|
|
* given 'uuid', or NULL if there is no such row. */
|
2015-03-11 16:50:41 -07:00
|
|
|
|
static struct ovsdb_monitor_row *
|
|
|
|
|
ovsdb_monitor_row_find(const struct ovsdb_monitor_table *mt,
|
|
|
|
|
const struct uuid *uuid)
|
2014-04-01 16:26:47 -07:00
|
|
|
|
{
|
2015-03-11 16:50:41 -07:00
|
|
|
|
struct ovsdb_monitor_row *row;
|
2014-04-01 16:26:47 -07:00
|
|
|
|
|
|
|
|
|
HMAP_FOR_EACH_WITH_HASH (row, hmap_node, uuid_hash(uuid), &mt->changes) {
|
|
|
|
|
if (uuid_equals(uuid, &row->uuid)) {
|
|
|
|
|
return row;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Allocates an array of 'mt->n_columns' ovsdb_datums and initializes them as
|
|
|
|
|
* copies of the data in 'row' drawn from the columns represented by
|
|
|
|
|
* mt->columns[]. Returns the array.
|
|
|
|
|
*
|
|
|
|
|
* If 'row' is NULL, returns NULL. */
|
|
|
|
|
static struct ovsdb_datum *
|
2015-03-11 16:50:41 -07:00
|
|
|
|
clone_monitor_row_data(const struct ovsdb_monitor_table *mt,
|
2014-04-01 16:26:47 -07:00
|
|
|
|
const struct ovsdb_row *row)
|
2010-08-30 14:12:27 -07:00
|
|
|
|
{
|
2014-04-01 16:26:47 -07:00
|
|
|
|
struct ovsdb_datum *data;
|
2010-08-30 14:12:27 -07:00
|
|
|
|
size_t i;
|
|
|
|
|
|
2014-04-01 16:26:47 -07:00
|
|
|
|
if (!row) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
data = xmalloc(mt->n_columns * sizeof *data);
|
2010-08-30 14:12:27 -07:00
|
|
|
|
for (i = 0; i < mt->n_columns; i++) {
|
2014-04-01 16:26:47 -07:00
|
|
|
|
const struct ovsdb_column *c = mt->columns[i].column;
|
|
|
|
|
const struct ovsdb_datum *src = &row->fields[c->index];
|
|
|
|
|
struct ovsdb_datum *dst = &data[i];
|
|
|
|
|
const struct ovsdb_type *type = &c->type;
|
|
|
|
|
|
|
|
|
|
ovsdb_datum_clone(dst, src, type);
|
|
|
|
|
}
|
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Replaces the mt->n_columns ovsdb_datums in row[] by copies of the data from
|
|
|
|
|
* in 'row' drawn from the columns represented by mt->columns[]. */
|
|
|
|
|
static void
|
2015-03-11 16:50:41 -07:00
|
|
|
|
update_monitor_row_data(const struct ovsdb_monitor_table *mt,
|
2014-04-01 16:26:47 -07:00
|
|
|
|
const struct ovsdb_row *row,
|
|
|
|
|
struct ovsdb_datum *data)
|
|
|
|
|
{
|
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < mt->n_columns; i++) {
|
|
|
|
|
const struct ovsdb_column *c = mt->columns[i].column;
|
|
|
|
|
const struct ovsdb_datum *src = &row->fields[c->index];
|
|
|
|
|
struct ovsdb_datum *dst = &data[i];
|
|
|
|
|
const struct ovsdb_type *type = &c->type;
|
|
|
|
|
|
|
|
|
|
if (!ovsdb_datum_equals(src, dst, type)) {
|
|
|
|
|
ovsdb_datum_destroy(dst, type);
|
|
|
|
|
ovsdb_datum_clone(dst, src, type);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Frees all of the mt->n_columns ovsdb_datums in data[], using the types taken
|
|
|
|
|
* from mt->columns[], plus 'data' itself. */
|
|
|
|
|
static void
|
2015-03-11 16:50:41 -07:00
|
|
|
|
free_monitor_row_data(const struct ovsdb_monitor_table *mt,
|
2014-04-01 16:26:47 -07:00
|
|
|
|
struct ovsdb_datum *data)
|
|
|
|
|
{
|
|
|
|
|
if (data) {
|
|
|
|
|
size_t i;
|
2010-08-30 14:12:27 -07:00
|
|
|
|
|
2014-04-01 16:26:47 -07:00
|
|
|
|
for (i = 0; i < mt->n_columns; i++) {
|
|
|
|
|
const struct ovsdb_column *c = mt->columns[i].column;
|
|
|
|
|
|
|
|
|
|
ovsdb_datum_destroy(&data[i], &c->type);
|
2010-08-30 14:12:27 -07:00
|
|
|
|
}
|
2014-04-01 16:26:47 -07:00
|
|
|
|
free(data);
|
2010-08-30 14:12:27 -07:00
|
|
|
|
}
|
2014-04-01 16:26:47 -07:00
|
|
|
|
}
|
2010-08-30 14:12:27 -07:00
|
|
|
|
|
2014-04-01 16:26:47 -07:00
|
|
|
|
/* Frees 'row', which must have been created from 'mt'. */
|
|
|
|
|
static void
|
2015-03-11 16:50:41 -07:00
|
|
|
|
ovsdb_monitor_row_destroy(const struct ovsdb_monitor_table *mt,
|
|
|
|
|
struct ovsdb_monitor_row *row)
|
2014-04-01 16:26:47 -07:00
|
|
|
|
{
|
|
|
|
|
if (row) {
|
|
|
|
|
free_monitor_row_data(mt, row->old);
|
|
|
|
|
free_monitor_row_data(mt, row->new);
|
|
|
|
|
free(row);
|
|
|
|
|
}
|
2010-08-30 14:12:27 -07:00
|
|
|
|
}
|
|
|
|
|
|
2009-11-16 10:38:14 -08:00
|
|
|
|
static bool
|
2015-03-11 16:50:41 -07:00
|
|
|
|
ovsdb_monitor_change_cb(const struct ovsdb_row *old,
|
|
|
|
|
const struct ovsdb_row *new,
|
|
|
|
|
const unsigned long int *changed OVS_UNUSED,
|
|
|
|
|
void *aux_)
|
2009-11-16 10:38:14 -08:00
|
|
|
|
{
|
2015-03-11 16:50:41 -07:00
|
|
|
|
struct ovsdb_monitor_aux *aux = aux_;
|
|
|
|
|
const struct ovsdb_monitor *m = aux->monitor;
|
2009-11-16 10:38:14 -08:00
|
|
|
|
struct ovsdb_table *table = new ? new->table : old->table;
|
2014-04-01 16:26:47 -07:00
|
|
|
|
const struct uuid *uuid = ovsdb_row_get_uuid(new ? new : old);
|
2015-03-11 16:50:41 -07:00
|
|
|
|
struct ovsdb_monitor_row *change;
|
|
|
|
|
struct ovsdb_monitor_table *mt;
|
2009-11-16 10:38:14 -08:00
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-04-01 16:26:47 -07:00
|
|
|
|
mt = aux->mt;
|
|
|
|
|
|
2015-03-11 16:50:41 -07:00
|
|
|
|
change = ovsdb_monitor_row_find(mt, uuid);
|
2014-04-01 16:26:47 -07:00
|
|
|
|
if (!change) {
|
|
|
|
|
change = xmalloc(sizeof *change);
|
|
|
|
|
hmap_insert(&mt->changes, &change->hmap_node, uuid_hash(uuid));
|
|
|
|
|
change->uuid = *uuid;
|
|
|
|
|
change->old = clone_monitor_row_data(mt, old);
|
|
|
|
|
change->new = clone_monitor_row_data(mt, new);
|
|
|
|
|
} else {
|
|
|
|
|
if (new) {
|
|
|
|
|
update_monitor_row_data(mt, new, change->new);
|
|
|
|
|
} else {
|
|
|
|
|
free_monitor_row_data(mt, change->new);
|
|
|
|
|
change->new = NULL;
|
|
|
|
|
|
|
|
|
|
if (!change->old) {
|
|
|
|
|
/* This row was added then deleted. Forget about it. */
|
|
|
|
|
hmap_remove(&mt->changes, &change->hmap_node);
|
|
|
|
|
free(change);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2009-11-16 10:38:14 -08:00
|
|
|
|
|
2014-04-04 09:43:54 -07:00
|
|
|
|
/* Returns JSON for a <row-update> (as described in RFC 7047) for 'row' within
|
|
|
|
|
* 'mt', or NULL if no row update should be sent.
|
2014-04-01 16:26:47 -07:00
|
|
|
|
*
|
|
|
|
|
* 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 *
|
2015-03-11 16:50:41 -07:00
|
|
|
|
ovsdb_monitor_compose_row_update(
|
|
|
|
|
const struct ovsdb_monitor_table *mt,
|
|
|
|
|
const struct ovsdb_monitor_row *row,
|
2014-04-01 16:26:47 -07:00
|
|
|
|
bool initial, unsigned long int *changed)
|
|
|
|
|
{
|
2015-03-11 16:50:41 -07:00
|
|
|
|
enum ovsdb_monitor_selection type;
|
2014-04-01 16:26:47 -07:00
|
|
|
|
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
|
2009-11-16 10:38:14 -08:00
|
|
|
|
: OJMS_MODIFY);
|
2014-04-01 16:26:47 -07:00
|
|
|
|
if (!(mt->select & type)) {
|
|
|
|
|
return NULL;
|
2009-11-16 10:38:14 -08:00
|
|
|
|
}
|
|
|
|
|
|
2014-04-01 16:26:47 -07:00
|
|
|
|
if (type == OJMS_MODIFY) {
|
|
|
|
|
size_t n_changes;
|
|
|
|
|
|
|
|
|
|
n_changes = 0;
|
|
|
|
|
memset(changed, 0, bitmap_n_bytes(mt->n_columns));
|
|
|
|
|
for (i = 0; i < mt->n_columns; i++) {
|
|
|
|
|
const struct ovsdb_column *c = mt->columns[i].column;
|
|
|
|
|
if (!ovsdb_datum_equals(&row->old[i], &row->new[i], &c->type)) {
|
|
|
|
|
bitmap_set1(changed, i);
|
|
|
|
|
n_changes++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!n_changes) {
|
|
|
|
|
/* No actual changes: presumably a row changed and then
|
|
|
|
|
* changed back later. */
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2010-08-30 14:12:27 -07:00
|
|
|
|
}
|
|
|
|
|
|
2014-04-01 16:26:47 -07:00
|
|
|
|
row_json = json_object_create();
|
2009-11-16 10:38:14 -08:00
|
|
|
|
old_json = new_json = NULL;
|
2010-08-30 14:12:27 -07:00
|
|
|
|
if (type & (OJMS_DELETE | OJMS_MODIFY)) {
|
|
|
|
|
old_json = json_object_create();
|
2014-04-01 16:26:47 -07:00
|
|
|
|
json_object_put(row_json, "old", old_json);
|
2010-08-30 14:12:27 -07:00
|
|
|
|
}
|
|
|
|
|
if (type & (OJMS_INITIAL | OJMS_INSERT | OJMS_MODIFY)) {
|
|
|
|
|
new_json = json_object_create();
|
2014-04-01 16:26:47 -07:00
|
|
|
|
json_object_put(row_json, "new", new_json);
|
2010-08-30 14:12:27 -07:00
|
|
|
|
}
|
2014-04-01 16:26:47 -07:00
|
|
|
|
for (i = 0; i < mt->n_columns; i++) {
|
2015-03-11 16:50:41 -07:00
|
|
|
|
const struct ovsdb_monitor_column *c = &mt->columns[i];
|
2009-11-16 10:38:14 -08:00
|
|
|
|
|
2010-07-01 09:47:46 -07:00
|
|
|
|
if (!(type & c->select)) {
|
2014-04-01 16:26:47 -07:00
|
|
|
|
/* We don't care about this type of change for this
|
|
|
|
|
* particular column (but we will care about it for some
|
|
|
|
|
* other column). */
|
2010-07-01 09:47:46 -07:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-01 16:26:47 -07:00
|
|
|
|
if ((type == OJMS_MODIFY && bitmap_is_set(changed, i))
|
2010-08-30 14:12:27 -07:00
|
|
|
|
|| type == OJMS_DELETE) {
|
2014-04-01 16:26:47 -07:00
|
|
|
|
json_object_put(old_json, c->column->name,
|
|
|
|
|
ovsdb_datum_to_json(&row->old[i],
|
|
|
|
|
&c->column->type));
|
2009-11-16 10:38:14 -08:00
|
|
|
|
}
|
|
|
|
|
if (type & (OJMS_INITIAL | OJMS_INSERT | OJMS_MODIFY)) {
|
2014-04-01 16:26:47 -07:00
|
|
|
|
json_object_put(new_json, c->column->name,
|
|
|
|
|
ovsdb_datum_to_json(&row->new[i],
|
|
|
|
|
&c->column->type));
|
2009-11-16 10:38:14 -08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-01 16:26:47 -07:00
|
|
|
|
return row_json;
|
|
|
|
|
}
|
2009-11-16 10:38:14 -08:00
|
|
|
|
|
2014-04-01 16:26:47 -07:00
|
|
|
|
/* Constructs and returns JSON for a <table-updates> object (as described in
|
2014-04-04 09:43:54 -07:00
|
|
|
|
* 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.
|
2014-04-01 16:26:47 -07:00
|
|
|
|
*
|
|
|
|
|
* 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 *
|
2015-03-11 19:28:13 -07:00
|
|
|
|
ovsdb_monitor_compose_table_update(
|
|
|
|
|
const struct ovsdb_monitor *dbmon, bool initial)
|
2014-04-01 16:26:47 -07:00
|
|
|
|
{
|
|
|
|
|
struct shash_node *node;
|
|
|
|
|
unsigned long int *changed;
|
|
|
|
|
struct json *json;
|
|
|
|
|
size_t max_columns;
|
2009-11-16 10:38:14 -08:00
|
|
|
|
|
2014-04-01 16:26:47 -07:00
|
|
|
|
max_columns = 0;
|
2015-03-11 19:28:13 -07:00
|
|
|
|
SHASH_FOR_EACH (node, &dbmon->tables) {
|
2015-03-11 16:50:41 -07:00
|
|
|
|
struct ovsdb_monitor_table *mt = node->data;
|
2014-04-01 16:26:47 -07:00
|
|
|
|
|
|
|
|
|
max_columns = MAX(max_columns, mt->n_columns);
|
2009-11-16 10:38:14 -08:00
|
|
|
|
}
|
2014-04-01 16:26:47 -07:00
|
|
|
|
changed = xmalloc(bitmap_n_bytes(max_columns));
|
|
|
|
|
|
|
|
|
|
json = NULL;
|
2015-03-11 19:28:13 -07:00
|
|
|
|
SHASH_FOR_EACH (node, &dbmon->tables) {
|
2015-03-11 16:50:41 -07:00
|
|
|
|
struct ovsdb_monitor_table *mt = node->data;
|
|
|
|
|
struct ovsdb_monitor_row *row, *next;
|
2014-04-01 16:26:47 -07:00
|
|
|
|
struct json *table_json = NULL;
|
|
|
|
|
|
|
|
|
|
HMAP_FOR_EACH_SAFE (row, next, hmap_node, &mt->changes) {
|
|
|
|
|
struct json *row_json;
|
|
|
|
|
|
2015-03-11 16:50:41 -07:00
|
|
|
|
row_json = ovsdb_monitor_compose_row_update(
|
2014-04-01 16:26:47 -07:00
|
|
|
|
mt, row, initial, changed);
|
|
|
|
|
if (row_json) {
|
|
|
|
|
char uuid[UUID_LEN + 1];
|
|
|
|
|
|
|
|
|
|
/* Create JSON object for transaction overall. */
|
|
|
|
|
if (!json) {
|
|
|
|
|
json = json_object_create();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Create JSON object for transaction on this table. */
|
|
|
|
|
if (!table_json) {
|
|
|
|
|
table_json = json_object_create();
|
|
|
|
|
json_object_put(json, mt->table->schema->name, table_json);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Add JSON row to JSON table. */
|
|
|
|
|
snprintf(uuid, sizeof uuid, UUID_FMT, UUID_ARGS(&row->uuid));
|
|
|
|
|
json_object_put(table_json, uuid, row_json);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hmap_remove(&mt->changes, &row->hmap_node);
|
2015-03-11 16:50:41 -07:00
|
|
|
|
ovsdb_monitor_row_destroy(mt, row);
|
2014-04-01 16:26:47 -07:00
|
|
|
|
}
|
2009-11-16 10:38:14 -08:00
|
|
|
|
}
|
|
|
|
|
|
2014-04-01 16:26:47 -07:00
|
|
|
|
free(changed);
|
2009-11-16 10:38:14 -08:00
|
|
|
|
|
2014-04-01 16:26:47 -07:00
|
|
|
|
return json;
|
2009-11-16 10:38:14 -08:00
|
|
|
|
}
|
|
|
|
|
|
2015-03-11 19:28:13 -07:00
|
|
|
|
static struct json *
|
|
|
|
|
ovsdb_jsonrpc_monitor_compose_table_update(
|
|
|
|
|
const struct ovsdb_jsonrpc_monitor *monitor, bool initial)
|
|
|
|
|
{
|
|
|
|
|
return ovsdb_monitor_compose_table_update(monitor->dbmon, initial);
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-02 08:43:17 -07:00
|
|
|
|
static bool
|
|
|
|
|
ovsdb_jsonrpc_monitor_needs_flush(struct ovsdb_jsonrpc_session *s)
|
|
|
|
|
{
|
|
|
|
|
struct ovsdb_jsonrpc_monitor *m;
|
|
|
|
|
|
|
|
|
|
HMAP_FOR_EACH (m, node, &s->monitors) {
|
|
|
|
|
struct shash_node *node;
|
|
|
|
|
|
2015-03-11 16:50:41 -07:00
|
|
|
|
SHASH_FOR_EACH (node, &m->dbmon->tables) {
|
|
|
|
|
struct ovsdb_monitor_table *mt = node->data;
|
2014-04-02 08:43:17 -07:00
|
|
|
|
|
|
|
|
|
if (!hmap_is_empty(&mt->changes)) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
ovsdb_jsonrpc_monitor_flush_all(struct ovsdb_jsonrpc_session *s)
|
|
|
|
|
{
|
|
|
|
|
struct ovsdb_jsonrpc_monitor *m;
|
|
|
|
|
|
|
|
|
|
HMAP_FOR_EACH (m, node, &s->monitors) {
|
|
|
|
|
struct json *json;
|
|
|
|
|
|
|
|
|
|
json = ovsdb_jsonrpc_monitor_compose_table_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);
|
|
|
|
|
jsonrpc_session_send(s->js, msg);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-11-16 10:38:14 -08:00
|
|
|
|
static void
|
2015-03-11 16:50:41 -07:00
|
|
|
|
ovsdb_monitor_init_aux(struct ovsdb_monitor_aux *aux,
|
|
|
|
|
const struct ovsdb_monitor *m)
|
2009-11-16 10:38:14 -08:00
|
|
|
|
{
|
|
|
|
|
aux->monitor = m;
|
|
|
|
|
aux->mt = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static struct ovsdb_error *
|
2015-03-11 16:50:41 -07:00
|
|
|
|
ovsdb_monitor_commit(struct ovsdb_replica *replica,
|
|
|
|
|
const struct ovsdb_txn *txn,
|
|
|
|
|
bool durable OVS_UNUSED)
|
2009-11-16 10:38:14 -08:00
|
|
|
|
{
|
2015-03-11 16:50:41 -07:00
|
|
|
|
struct ovsdb_monitor *m = ovsdb_monitor_cast(replica);
|
|
|
|
|
struct ovsdb_monitor_aux aux;
|
2009-11-16 10:38:14 -08:00
|
|
|
|
|
2015-03-11 16:50:41 -07:00
|
|
|
|
ovsdb_monitor_init_aux(&aux, m);
|
|
|
|
|
ovsdb_txn_for_each_change(txn, ovsdb_monitor_change_cb, &aux);
|
2009-11-16 10:38:14 -08:00
|
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static struct json *
|
|
|
|
|
ovsdb_jsonrpc_monitor_get_initial(const struct ovsdb_jsonrpc_monitor *m)
|
|
|
|
|
{
|
2015-03-11 16:50:41 -07:00
|
|
|
|
struct ovsdb_monitor_aux aux;
|
2009-11-16 10:38:14 -08:00
|
|
|
|
struct shash_node *node;
|
2014-04-01 16:26:47 -07:00
|
|
|
|
struct json *json;
|
2009-11-16 10:38:14 -08:00
|
|
|
|
|
2015-03-11 16:50:41 -07:00
|
|
|
|
ovsdb_monitor_init_aux(&aux, m->dbmon);
|
|
|
|
|
SHASH_FOR_EACH (node, &m->dbmon->tables) {
|
|
|
|
|
struct ovsdb_monitor_table *mt = node->data;
|
2009-11-16 10:38:14 -08:00
|
|
|
|
|
|
|
|
|
if (mt->select & OJMS_INITIAL) {
|
|
|
|
|
struct ovsdb_row *row;
|
|
|
|
|
|
2010-09-17 10:33:10 -07:00
|
|
|
|
HMAP_FOR_EACH (row, hmap_node, &mt->table->rows) {
|
2015-03-11 16:50:41 -07:00
|
|
|
|
ovsdb_monitor_change_cb(NULL, row, NULL, &aux);
|
2009-11-16 10:38:14 -08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-04-01 16:26:47 -07:00
|
|
|
|
json = ovsdb_jsonrpc_monitor_compose_table_update(m, true);
|
|
|
|
|
return json ? json : json_object_create();
|
2009-11-16 10:38:14 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2015-03-11 16:50:41 -07:00
|
|
|
|
ovsdb_monitor_destroy(struct ovsdb_replica *replica)
|
2009-11-16 10:38:14 -08:00
|
|
|
|
{
|
2015-03-11 16:50:41 -07:00
|
|
|
|
struct ovsdb_monitor *m = ovsdb_monitor_cast(replica);
|
|
|
|
|
struct ovsdb_jsonrpc_monitor *jsonrpc_monitor = m->jsonrpc_monitor;
|
2009-11-16 10:38:14 -08:00
|
|
|
|
struct shash_node *node;
|
|
|
|
|
|
2015-03-11 16:50:41 -07:00
|
|
|
|
json_destroy(jsonrpc_monitor->monitor_id);
|
2009-11-16 10:38:14 -08:00
|
|
|
|
SHASH_FOR_EACH (node, &m->tables) {
|
2015-03-11 16:50:41 -07:00
|
|
|
|
struct ovsdb_monitor_table *mt = node->data;
|
|
|
|
|
struct ovsdb_monitor_row *row, *next;
|
2014-04-01 16:26:47 -07:00
|
|
|
|
|
|
|
|
|
HMAP_FOR_EACH_SAFE (row, next, hmap_node, &mt->changes) {
|
|
|
|
|
hmap_remove(&mt->changes, &row->hmap_node);
|
2015-03-11 16:50:41 -07:00
|
|
|
|
ovsdb_monitor_row_destroy(mt, row);
|
2014-04-01 16:26:47 -07:00
|
|
|
|
}
|
|
|
|
|
hmap_destroy(&mt->changes);
|
|
|
|
|
|
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);
|
2015-03-11 16:50:41 -07:00
|
|
|
|
hmap_remove(&jsonrpc_monitor->session->monitors, &jsonrpc_monitor->node);
|
|
|
|
|
free(jsonrpc_monitor);
|
2009-11-16 10:38:14 -08:00
|
|
|
|
free(m);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static const struct ovsdb_replica_class ovsdb_jsonrpc_replica_class = {
|
2015-03-11 16:50:41 -07:00
|
|
|
|
ovsdb_monitor_commit,
|
|
|
|
|
ovsdb_monitor_destroy
|
2009-11-16 10:38:14 -08:00
|
|
|
|
};
|