]> git.proxmox.com Git - mirror_ovs.git/blame - ovsdb/jsonrpc-server.c
ovsdb-client: Fix memory leaks
[mirror_ovs.git] / ovsdb / jsonrpc-server.c
CommitLineData
c2e3cbaf 1/* Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015 Nicira, Inc.
f85f8ebb
BP
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#include <config.h>
17
18#include "jsonrpc-server.h"
19
20#include <errno.h>
21
17d18afb 22#include "bitmap.h"
b93d3b6c 23#include "column.h"
3e8a2ad1 24#include "openvswitch/dynamic-string.h"
52553aea 25#include "monitor.h"
ee89ea7b 26#include "openvswitch/json.h"
f85f8ebb 27#include "jsonrpc.h"
a8425c53
BP
28#include "ovsdb-error.h"
29#include "ovsdb-parser.h"
f85f8ebb 30#include "ovsdb.h"
71cdf7cd 31#include "condition.h"
fd016ae3 32#include "openvswitch/poll-loop.h"
6c2882f9 33#include "reconnect.h"
a8425c53 34#include "row.h"
e317253b 35#include "server.h"
0d085684 36#include "simap.h"
f85f8ebb 37#include "stream.h"
a8425c53 38#include "table.h"
f85f8ebb 39#include "timeval.h"
a8425c53 40#include "transaction.h"
f85f8ebb 41#include "trigger.h"
ee89ea7b 42#include "util.h"
e6211adc 43#include "openvswitch/vlog.h"
f85f8ebb 44
d98e6007 45VLOG_DEFINE_THIS_MODULE(ovsdb_jsonrpc_server);
5136ce49 46
0b1fae1b 47struct ovsdb_jsonrpc_remote;
b93d3b6c 48struct ovsdb_jsonrpc_session;
f85f8ebb 49
c383f3bf
LS
50/* Set false to defeature monitor_cond, causing jsonrpc to respond to
51 * monitor_cond method with an error. */
52static bool monitor_cond_enable__ = true;
e47cb14c 53
0b1fae1b 54/* Message rate-limiting. */
d3d8f1f7 55static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
0b1fae1b 56
a8425c53 57/* Sessions. */
0b1fae1b 58static struct ovsdb_jsonrpc_session *ovsdb_jsonrpc_session_create(
e51879e9 59 struct ovsdb_jsonrpc_remote *, struct jsonrpc_session *, bool);
0b1fae1b
BP
60static void ovsdb_jsonrpc_session_run_all(struct ovsdb_jsonrpc_remote *);
61static void ovsdb_jsonrpc_session_wait_all(struct ovsdb_jsonrpc_remote *);
0d085684
BP
62static void ovsdb_jsonrpc_session_get_memory_usage_all(
63 const struct ovsdb_jsonrpc_remote *, struct simap *usage);
0b1fae1b 64static void ovsdb_jsonrpc_session_close_all(struct ovsdb_jsonrpc_remote *);
31d0b6c9 65static void ovsdb_jsonrpc_session_reconnect_all(struct ovsdb_jsonrpc_remote *);
94db5407
BP
66static void ovsdb_jsonrpc_session_set_all_options(
67 struct ovsdb_jsonrpc_remote *, const struct ovsdb_jsonrpc_options *);
600766e8 68static bool ovsdb_jsonrpc_active_session_get_status(
0b3e7a8b 69 const struct ovsdb_jsonrpc_remote *,
87fcbc60 70 struct ovsdb_jsonrpc_remote_status *);
600766e8
AZ
71static void ovsdb_jsonrpc_session_get_status(
72 const struct ovsdb_jsonrpc_session *,
73 struct ovsdb_jsonrpc_remote_status *);
da897f41
BP
74static void ovsdb_jsonrpc_session_unlock_all(struct ovsdb_jsonrpc_session *);
75static void ovsdb_jsonrpc_session_unlock__(struct ovsdb_lock_waiter *);
48f6e410
BP
76static void ovsdb_jsonrpc_session_send(struct ovsdb_jsonrpc_session *,
77 struct jsonrpc_msg *);
b93d3b6c 78
a8425c53 79/* Triggers. */
b93d3b6c 80static void ovsdb_jsonrpc_trigger_create(struct ovsdb_jsonrpc_session *,
b4e8d170 81 struct ovsdb *,
b93d3b6c
BP
82 struct json *id, struct json *params);
83static struct ovsdb_jsonrpc_trigger *ovsdb_jsonrpc_trigger_find(
84 struct ovsdb_jsonrpc_session *, const struct json *id, size_t hash);
85static void ovsdb_jsonrpc_trigger_complete(struct ovsdb_jsonrpc_trigger *);
86static void ovsdb_jsonrpc_trigger_complete_all(struct ovsdb_jsonrpc_session *);
87static void ovsdb_jsonrpc_trigger_complete_done(
88 struct ovsdb_jsonrpc_session *);
a8425c53
BP
89
90/* Monitors. */
508624b6
BP
91static struct jsonrpc_msg *ovsdb_jsonrpc_monitor_create(
92 struct ovsdb_jsonrpc_session *, struct ovsdb *, struct json *params,
92f8d65b 93 enum ovsdb_monitor_version, const struct json *request_id);
845a1187
LS
94static struct jsonrpc_msg *ovsdb_jsonrpc_monitor_cond_change(
95 struct ovsdb_jsonrpc_session *s,
96 struct json *params,
97 const struct json *request_id);
a8425c53
BP
98static struct jsonrpc_msg *ovsdb_jsonrpc_monitor_cancel(
99 struct ovsdb_jsonrpc_session *,
100 struct json_array *params,
101 const struct json *request_id);
102static void ovsdb_jsonrpc_monitor_remove_all(struct ovsdb_jsonrpc_session *);
48f6e410
BP
103static void ovsdb_jsonrpc_monitor_flush_all(struct ovsdb_jsonrpc_session *);
104static bool ovsdb_jsonrpc_monitor_needs_flush(struct ovsdb_jsonrpc_session *);
f1de87bb 105static struct json *ovsdb_jsonrpc_monitor_compose_update(
59c35e11 106 struct ovsdb_jsonrpc_monitor *monitor, bool initial);
845a1187
LS
107static struct jsonrpc_msg * ovsdb_jsonrpc_create_notify(
108 const struct ovsdb_jsonrpc_monitor *m,
109 struct json *params);
61b63013 110
b93d3b6c
BP
111\f
112/* JSON-RPC database server. */
f85f8ebb
BP
113
114struct ovsdb_jsonrpc_server {
e317253b 115 struct ovsdb_server up;
acdd0764 116 unsigned int n_sessions;
e51879e9
AZ
117 bool read_only; /* This server is does not accept any
118 transactions that can modify the database. */
0b1fae1b
BP
119 struct shash remotes; /* Contains "struct ovsdb_jsonrpc_remote *"s. */
120};
f85f8ebb 121
0b1fae1b
BP
122/* A configured remote. This is either a passive stream listener plus a list
123 * of the currently connected sessions, or a list of exactly one active
124 * session. */
125struct ovsdb_jsonrpc_remote {
126 struct ovsdb_jsonrpc_server *server;
127 struct pstream *listener; /* Listener, if passive. */
ca6ba700 128 struct ovs_list sessions; /* List of "struct ovsdb_jsonrpc_session"s. */
e879d33e 129 uint8_t dscp;
9c1a1182 130 bool read_only;
d6db7b3c 131 char *role;
f85f8ebb
BP
132};
133
94db5407 134static struct ovsdb_jsonrpc_remote *ovsdb_jsonrpc_server_add_remote(
f125905c
MM
135 struct ovsdb_jsonrpc_server *, const char *name,
136 const struct ovsdb_jsonrpc_options *options
137);
0b1fae1b
BP
138static void ovsdb_jsonrpc_server_del_remote(struct shash_node *);
139
b4e8d170
BP
140/* Creates and returns a new server to provide JSON-RPC access to an OVSDB.
141 *
142 * The caller must call ovsdb_jsonrpc_server_add_db() for each database to
143 * which 'server' should provide access. */
b93d3b6c 144struct ovsdb_jsonrpc_server *
e51879e9 145ovsdb_jsonrpc_server_create(bool read_only)
f85f8ebb 146{
b93d3b6c 147 struct ovsdb_jsonrpc_server *server = xzalloc(sizeof *server);
b4e8d170 148 ovsdb_server_init(&server->up);
0b1fae1b 149 shash_init(&server->remotes);
e51879e9 150 server->read_only = read_only;
b93d3b6c
BP
151 return server;
152}
f85f8ebb 153
b4e8d170
BP
154/* Adds 'db' to the set of databases served out by 'svr'. Returns true if
155 * successful, false if 'db''s name is the same as some database already in
156 * 'server'. */
157bool
158ovsdb_jsonrpc_server_add_db(struct ovsdb_jsonrpc_server *svr, struct ovsdb *db)
159{
0a3b723b
BP
160 /* The OVSDB protocol doesn't have a way to notify a client that a
161 * database has been added. If some client tried to use the database
162 * that we're adding and failed, then forcing it to reconnect seems like
163 * a reasonable way to make it try again.
164 *
165 * If this is too big of a hammer in practice, we could be more selective,
166 * e.g. disconnect only connections that actually tried to use a database
167 * with 'db''s name. */
e51879e9 168 ovsdb_jsonrpc_server_reconnect(svr, svr->read_only);
0a3b723b 169
b4e8d170
BP
170 return ovsdb_server_add_db(&svr->up, db);
171}
172
0a3b723b
BP
173/* Removes 'db' from the set of databases served out by 'svr'. Returns
174 * true if successful, false if there is no database associated with 'db'. */
175bool
176ovsdb_jsonrpc_server_remove_db(struct ovsdb_jsonrpc_server *svr,
177 struct ovsdb *db)
178{
179 /* There might be pointers to 'db' from 'svr', such as monitors or
180 * outstanding transactions. Disconnect all JSON-RPC connections to avoid
181 * accesses to freed memory.
182 *
183 * If this is too big of a hammer in practice, we could be more selective,
184 * e.g. disconnect only connections that actually reference 'db'. */
e51879e9 185 ovsdb_jsonrpc_server_reconnect(svr, svr->read_only);
0a3b723b
BP
186
187 return ovsdb_server_remove_db(&svr->up, db);
188}
189
23935e8b
BP
190void
191ovsdb_jsonrpc_server_destroy(struct ovsdb_jsonrpc_server *svr)
192{
193 struct shash_node *node, *next;
194
195 SHASH_FOR_EACH_SAFE (node, next, &svr->remotes) {
196 ovsdb_jsonrpc_server_del_remote(node);
197 }
198 shash_destroy(&svr->remotes);
e317253b 199 ovsdb_server_destroy(&svr->up);
23935e8b
BP
200 free(svr);
201}
202
94db5407 203struct ovsdb_jsonrpc_options *
f1936eb6 204ovsdb_jsonrpc_default_options(const char *target)
94db5407
BP
205{
206 struct ovsdb_jsonrpc_options *options = xzalloc(sizeof *options);
94db5407 207 options->max_backoff = RECONNECT_DEFAULT_MAX_BACKOFF;
f1936eb6
EJ
208 options->probe_interval = (stream_or_pstream_needs_probes(target)
209 ? RECONNECT_DEFAULT_PROBE_INTERVAL
210 : 0);
94db5407
BP
211 return options;
212}
213
214/* Sets 'svr''s current set of remotes to the names in 'new_remotes', with
215 * options in the struct ovsdb_jsonrpc_options supplied as the data values.
0b1fae1b
BP
216 *
217 * A remote is an active or passive stream connection method, e.g. "pssl:" or
218 * "tcp:1.2.3.4". */
6dea5eaf 219void
0b1fae1b
BP
220ovsdb_jsonrpc_server_set_remotes(struct ovsdb_jsonrpc_server *svr,
221 const struct shash *new_remotes)
b93d3b6c 222{
0b1fae1b
BP
223 struct shash_node *node, *next;
224
225 SHASH_FOR_EACH_SAFE (node, next, &svr->remotes) {
c2e3cbaf
BP
226 struct ovsdb_jsonrpc_remote *remote = node->data;
227 struct ovsdb_jsonrpc_options *options
228 = shash_find_data(new_remotes, node->name);
229
230 if (!options) {
f97ffebf 231 VLOG_INFO("%s: remote deconfigured", node->name);
0b1fae1b 232 ovsdb_jsonrpc_server_del_remote(node);
c2e3cbaf
BP
233 } else if (options->dscp != remote->dscp) {
234 ovsdb_jsonrpc_server_del_remote(node);
235 }
0b1fae1b
BP
236 }
237 SHASH_FOR_EACH (node, new_remotes) {
94db5407
BP
238 const struct ovsdb_jsonrpc_options *options = node->data;
239 struct ovsdb_jsonrpc_remote *remote;
240
241 remote = shash_find_data(&svr->remotes, node->name);
242 if (!remote) {
f125905c 243 remote = ovsdb_jsonrpc_server_add_remote(svr, node->name, options);
94db5407
BP
244 if (!remote) {
245 continue;
246 }
0b1fae1b 247 }
94db5407
BP
248
249 ovsdb_jsonrpc_session_set_all_options(remote, options);
f85f8ebb 250 }
b93d3b6c 251}
f85f8ebb 252
94db5407 253static struct ovsdb_jsonrpc_remote *
0b1fae1b 254ovsdb_jsonrpc_server_add_remote(struct ovsdb_jsonrpc_server *svr,
f125905c
MM
255 const char *name,
256 const struct ovsdb_jsonrpc_options *options)
b93d3b6c 257{
0b1fae1b
BP
258 struct ovsdb_jsonrpc_remote *remote;
259 struct pstream *listener;
260 int error;
261
f125905c 262 error = jsonrpc_pstream_open(name, &listener, options->dscp);
0b1fae1b 263 if (error && error != EAFNOSUPPORT) {
10a89ef0 264 VLOG_ERR_RL(&rl, "%s: listen failed: %s", name, ovs_strerror(error));
94db5407 265 return NULL;
0b1fae1b
BP
266 }
267
268 remote = xmalloc(sizeof *remote);
269 remote->server = svr;
270 remote->listener = listener;
417e7e66 271 ovs_list_init(&remote->sessions);
e879d33e 272 remote->dscp = options->dscp;
9c1a1182 273 remote->read_only = options->read_only;
d6db7b3c 274 remote->role = nullable_xstrdup(options->role);
0b1fae1b
BP
275 shash_add(&svr->remotes, name, remote);
276
277 if (!listener) {
e51879e9 278 ovsdb_jsonrpc_session_create(remote, jsonrpc_session_open(name, true),
9c1a1182 279 svr->read_only || remote->read_only);
0b1fae1b 280 }
94db5407 281 return remote;
0b1fae1b
BP
282}
283
284static void
285ovsdb_jsonrpc_server_del_remote(struct shash_node *node)
286{
287 struct ovsdb_jsonrpc_remote *remote = node->data;
288
289 ovsdb_jsonrpc_session_close_all(remote);
290 pstream_close(remote->listener);
291 shash_delete(&remote->server->remotes, node);
d6db7b3c 292 free(remote->role);
0b1fae1b 293 free(remote);
f85f8ebb
BP
294}
295
87fcbc60
BP
296/* Stores status information for the remote named 'target', which should have
297 * been configured on 'svr' with a call to ovsdb_jsonrpc_server_set_remotes(),
298 * into '*status'. On success returns true, on failure (if 'svr' doesn't have
600766e8 299 * a remote named 'target' or if that remote is an outbound remote that has no
87fcbc60
BP
300 * active connections) returns false. On failure, 'status' will be zeroed.
301 */
302bool
303ovsdb_jsonrpc_server_get_remote_status(
304 const struct ovsdb_jsonrpc_server *svr, const char *target,
305 struct ovsdb_jsonrpc_remote_status *status)
0b3e7a8b 306{
87fcbc60 307 const struct ovsdb_jsonrpc_remote *remote;
0b3e7a8b 308
87fcbc60 309 memset(status, 0, sizeof *status);
0b3e7a8b 310
87fcbc60 311 remote = shash_find_data(&svr->remotes, target);
600766e8
AZ
312
313 if (!remote) {
314 return false;
315 }
316
317 if (remote->listener) {
318 status->bound_port = pstream_get_bound_port(remote->listener);
417e7e66
BW
319 status->is_connected = !ovs_list_is_empty(&remote->sessions);
320 status->n_connections = ovs_list_size(&remote->sessions);
600766e8
AZ
321 return true;
322 }
323
324 return ovsdb_jsonrpc_active_session_get_status(remote, status);
0b3e7a8b
AE
325}
326
da897f41
BP
327void
328ovsdb_jsonrpc_server_free_remote_status(
329 struct ovsdb_jsonrpc_remote_status *status)
330{
331 free(status->locks_held);
332 free(status->locks_waiting);
333 free(status->locks_lost);
334}
335
31d0b6c9
BP
336/* Forces all of the JSON-RPC sessions managed by 'svr' to disconnect and
337 * reconnect. */
338void
e51879e9 339ovsdb_jsonrpc_server_reconnect(struct ovsdb_jsonrpc_server *svr, bool read_only)
31d0b6c9
BP
340{
341 struct shash_node *node;
342
e51879e9 343 svr->read_only = read_only;
31d0b6c9
BP
344 SHASH_FOR_EACH (node, &svr->remotes) {
345 struct ovsdb_jsonrpc_remote *remote = node->data;
346
347 ovsdb_jsonrpc_session_reconnect_all(remote);
348 }
349}
350
60e0cd04
AZ
351bool
352ovsdb_jsonrpc_server_is_read_only(struct ovsdb_jsonrpc_server *svr)
353{
354 return svr->read_only;
355}
356
f85f8ebb
BP
357void
358ovsdb_jsonrpc_server_run(struct ovsdb_jsonrpc_server *svr)
359{
0b1fae1b
BP
360 struct shash_node *node;
361
362 SHASH_FOR_EACH (node, &svr->remotes) {
363 struct ovsdb_jsonrpc_remote *remote = node->data;
364
04abfbe7 365 if (remote->listener) {
acdd0764
AZ
366 struct stream *stream;
367 int error;
368
369 error = pstream_accept(remote->listener, &stream);
370 if (!error) {
371 struct jsonrpc_session *js;
372 js = jsonrpc_session_open_unreliably(jsonrpc_open(stream),
373 remote->dscp);
9c1a1182
LR
374 ovsdb_jsonrpc_session_create(remote, js, svr->read_only ||
375 remote->read_only);
acdd0764
AZ
376 } else if (error != EAGAIN) {
377 VLOG_WARN_RL(&rl, "%s: accept failed: %s",
0b1fae1b 378 pstream_get_name(remote->listener),
acdd0764 379 ovs_strerror(error));
0b1fae1b 380 }
f85f8ebb 381 }
f85f8ebb 382
0b1fae1b
BP
383 ovsdb_jsonrpc_session_run_all(remote);
384 }
f85f8ebb
BP
385}
386
387void
388ovsdb_jsonrpc_server_wait(struct ovsdb_jsonrpc_server *svr)
389{
0b1fae1b
BP
390 struct shash_node *node;
391
392 SHASH_FOR_EACH (node, &svr->remotes) {
393 struct ovsdb_jsonrpc_remote *remote = node->data;
f85f8ebb 394
acdd0764 395 if (remote->listener) {
0b1fae1b 396 pstream_wait(remote->listener);
f85f8ebb 397 }
f85f8ebb 398
0b1fae1b
BP
399 ovsdb_jsonrpc_session_wait_all(remote);
400 }
f85f8ebb 401}
0d085684
BP
402
403/* Adds some memory usage statistics for 'svr' into 'usage', for use with
404 * memory_report(). */
405void
406ovsdb_jsonrpc_server_get_memory_usage(const struct ovsdb_jsonrpc_server *svr,
407 struct simap *usage)
408{
409 struct shash_node *node;
410
411 simap_increase(usage, "sessions", svr->n_sessions);
412 SHASH_FOR_EACH (node, &svr->remotes) {
413 struct ovsdb_jsonrpc_remote *remote = node->data;
414
415 ovsdb_jsonrpc_session_get_memory_usage_all(remote, usage);
416 }
417}
b93d3b6c
BP
418\f
419/* JSON-RPC database server session. */
f85f8ebb 420
b93d3b6c 421struct ovsdb_jsonrpc_session {
ca6ba700 422 struct ovs_list node; /* Element in remote's sessions list. */
e317253b 423 struct ovsdb_session up;
0b1fae1b 424 struct ovsdb_jsonrpc_remote *remote;
f85f8ebb 425
b93d3b6c
BP
426 /* Triggers. */
427 struct hmap triggers; /* Hmap of "struct ovsdb_jsonrpc_trigger"s. */
f85f8ebb 428
a8425c53
BP
429 /* Monitors. */
430 struct hmap monitors; /* Hmap of "struct ovsdb_jsonrpc_monitor"s. */
431
4931f33a
BP
432 /* Network connectivity. */
433 struct jsonrpc_session *js; /* JSON-RPC session. */
434 unsigned int js_seqno; /* Last jsonrpc_session_get_seqno() value. */
e51879e9
AZ
435
436 /* Read only. */
437 bool read_only; /* When true, not allow to modify the
438 database. */
b93d3b6c 439};
f85f8ebb 440
b93d3b6c 441static void ovsdb_jsonrpc_session_close(struct ovsdb_jsonrpc_session *);
b93d3b6c
BP
442static int ovsdb_jsonrpc_session_run(struct ovsdb_jsonrpc_session *);
443static void ovsdb_jsonrpc_session_wait(struct ovsdb_jsonrpc_session *);
0d085684
BP
444static void ovsdb_jsonrpc_session_get_memory_usage(
445 const struct ovsdb_jsonrpc_session *, struct simap *usage);
b93d3b6c 446static void ovsdb_jsonrpc_session_got_request(struct ovsdb_jsonrpc_session *,
845a1187 447 struct jsonrpc_msg *);
b93d3b6c
BP
448static void ovsdb_jsonrpc_session_got_notify(struct ovsdb_jsonrpc_session *,
449 struct jsonrpc_msg *);
f85f8ebb 450
6c2882f9 451static struct ovsdb_jsonrpc_session *
0b1fae1b 452ovsdb_jsonrpc_session_create(struct ovsdb_jsonrpc_remote *remote,
e51879e9 453 struct jsonrpc_session *js, bool read_only)
f85f8ebb
BP
454{
455 struct ovsdb_jsonrpc_session *s;
456
457 s = xzalloc(sizeof *s);
b4e8d170 458 ovsdb_session_init(&s->up, &remote->server->up);
0b1fae1b 459 s->remote = remote;
417e7e66 460 ovs_list_push_back(&remote->sessions, &s->node);
f85f8ebb 461 hmap_init(&s->triggers);
a8425c53 462 hmap_init(&s->monitors);
4931f33a
BP
463 s->js = js;
464 s->js_seqno = jsonrpc_session_get_seqno(js);
e51879e9 465 s->read_only = read_only;
6c2882f9 466
0b1fae1b 467 remote->server->n_sessions++;
6c2882f9
BP
468
469 return s;
f85f8ebb
BP
470}
471
6c2882f9
BP
472static void
473ovsdb_jsonrpc_session_close(struct ovsdb_jsonrpc_session *s)
474{
e084f690 475 ovsdb_jsonrpc_monitor_remove_all(s);
da897f41 476 ovsdb_jsonrpc_session_unlock_all(s);
aca16ce6
BP
477 ovsdb_jsonrpc_trigger_complete_all(s);
478
479 hmap_destroy(&s->monitors);
480 hmap_destroy(&s->triggers);
481
4931f33a 482 jsonrpc_session_close(s->js);
417e7e66 483 ovs_list_remove(&s->node);
0b1fae1b 484 s->remote->server->n_sessions--;
e317253b 485 ovsdb_session_destroy(&s->up);
e084f690 486 free(s);
f85f8ebb
BP
487}
488
4931f33a
BP
489static int
490ovsdb_jsonrpc_session_run(struct ovsdb_jsonrpc_session *s)
6c2882f9 491{
4931f33a
BP
492 jsonrpc_session_run(s->js);
493 if (s->js_seqno != jsonrpc_session_get_seqno(s->js)) {
494 s->js_seqno = jsonrpc_session_get_seqno(s->js);
b93d3b6c 495 ovsdb_jsonrpc_trigger_complete_all(s);
a8425c53 496 ovsdb_jsonrpc_monitor_remove_all(s);
da897f41 497 ovsdb_jsonrpc_session_unlock_all(s);
6c2882f9 498 }
6c2882f9 499
4931f33a 500 ovsdb_jsonrpc_trigger_complete_done(s);
6c2882f9 501
633f7247 502 if (!jsonrpc_session_get_backlog(s->js)) {
48f6e410
BP
503 struct jsonrpc_msg *msg;
504
505 ovsdb_jsonrpc_monitor_flush_all(s);
506
507 msg = jsonrpc_session_recv(s->js);
4931f33a 508 if (msg) {
6c2882f9
BP
509 if (msg->type == JSONRPC_REQUEST) {
510 ovsdb_jsonrpc_session_got_request(s, msg);
511 } else if (msg->type == JSONRPC_NOTIFY) {
512 ovsdb_jsonrpc_session_got_notify(s, msg);
513 } else {
514 VLOG_WARN("%s: received unexpected %s message",
4931f33a 515 jsonrpc_session_get_name(s->js),
6c2882f9 516 jsonrpc_msg_type_to_string(msg->type));
4931f33a 517 jsonrpc_session_force_reconnect(s->js);
6c2882f9
BP
518 jsonrpc_msg_destroy(msg);
519 }
520 }
6c2882f9 521 }
4931f33a 522 return jsonrpc_session_is_alive(s->js) ? 0 : ETIMEDOUT;
b93d3b6c 523}
6c2882f9 524
94db5407
BP
525static void
526ovsdb_jsonrpc_session_set_options(struct ovsdb_jsonrpc_session *session,
527 const struct ovsdb_jsonrpc_options *options)
528{
529 jsonrpc_session_set_max_backoff(session->js, options->max_backoff);
530 jsonrpc_session_set_probe_interval(session->js, options->probe_interval);
f125905c 531 jsonrpc_session_set_dscp(session->js, options->dscp);
94db5407
BP
532}
533
b93d3b6c 534static void
0b1fae1b 535ovsdb_jsonrpc_session_run_all(struct ovsdb_jsonrpc_remote *remote)
b93d3b6c
BP
536{
537 struct ovsdb_jsonrpc_session *s, *next;
538
4e8e4213 539 LIST_FOR_EACH_SAFE (s, next, node, &remote->sessions) {
b93d3b6c
BP
540 int error = ovsdb_jsonrpc_session_run(s);
541 if (error) {
542 ovsdb_jsonrpc_session_close(s);
543 }
544 }
6c2882f9
BP
545}
546
547static void
548ovsdb_jsonrpc_session_wait(struct ovsdb_jsonrpc_session *s)
549{
4931f33a
BP
550 jsonrpc_session_wait(s->js);
551 if (!jsonrpc_session_get_backlog(s->js)) {
48f6e410
BP
552 if (ovsdb_jsonrpc_monitor_needs_flush(s)) {
553 poll_immediate_wake();
554 } else {
555 jsonrpc_session_recv_wait(s->js);
556 }
6c2882f9 557 }
6c2882f9
BP
558}
559
b93d3b6c 560static void
0b1fae1b 561ovsdb_jsonrpc_session_wait_all(struct ovsdb_jsonrpc_remote *remote)
f85f8ebb 562{
b93d3b6c 563 struct ovsdb_jsonrpc_session *s;
f85f8ebb 564
4e8e4213 565 LIST_FOR_EACH (s, node, &remote->sessions) {
b93d3b6c 566 ovsdb_jsonrpc_session_wait(s);
f85f8ebb 567 }
b93d3b6c 568}
f85f8ebb 569
0d085684
BP
570static void
571ovsdb_jsonrpc_session_get_memory_usage(const struct ovsdb_jsonrpc_session *s,
572 struct simap *usage)
573{
574 simap_increase(usage, "triggers", hmap_count(&s->triggers));
0d085684
BP
575 simap_increase(usage, "backlog", jsonrpc_session_get_backlog(s->js));
576}
577
578static void
579ovsdb_jsonrpc_session_get_memory_usage_all(
580 const struct ovsdb_jsonrpc_remote *remote,
581 struct simap *usage)
582{
583 struct ovsdb_jsonrpc_session *s;
584
585 LIST_FOR_EACH (s, node, &remote->sessions) {
586 ovsdb_jsonrpc_session_get_memory_usage(s, usage);
587 }
588}
589
0b1fae1b
BP
590static void
591ovsdb_jsonrpc_session_close_all(struct ovsdb_jsonrpc_remote *remote)
592{
593 struct ovsdb_jsonrpc_session *s, *next;
594
4e8e4213 595 LIST_FOR_EACH_SAFE (s, next, node, &remote->sessions) {
0b1fae1b
BP
596 ovsdb_jsonrpc_session_close(s);
597 }
598}
599
31d0b6c9
BP
600/* Forces all of the JSON-RPC sessions managed by 'remote' to disconnect and
601 * reconnect. */
602static void
603ovsdb_jsonrpc_session_reconnect_all(struct ovsdb_jsonrpc_remote *remote)
604{
605 struct ovsdb_jsonrpc_session *s, *next;
606
4e8e4213 607 LIST_FOR_EACH_SAFE (s, next, node, &remote->sessions) {
31d0b6c9
BP
608 jsonrpc_session_force_reconnect(s->js);
609 if (!jsonrpc_session_is_alive(s->js)) {
610 ovsdb_jsonrpc_session_close(s);
611 }
612 }
613}
614
94db5407 615/* Sets the options for all of the JSON-RPC sessions managed by 'remote' to
c2e3cbaf
BP
616 * 'options'.
617 *
618 * (The dscp value can't be changed directly; the caller must instead close and
619 * re-open the session.) */
94db5407
BP
620static void
621ovsdb_jsonrpc_session_set_all_options(
622 struct ovsdb_jsonrpc_remote *remote,
623 const struct ovsdb_jsonrpc_options *options)
624{
625 struct ovsdb_jsonrpc_session *s;
626
627 LIST_FOR_EACH (s, node, &remote->sessions) {
628 ovsdb_jsonrpc_session_set_options(s, options);
629 }
630}
631
600766e8 632/* Sets the 'status' of for the 'remote' with an outgoing connection. */
87fcbc60 633static bool
600766e8
AZ
634ovsdb_jsonrpc_active_session_get_status(
635 const struct ovsdb_jsonrpc_remote *remote,
636 struct ovsdb_jsonrpc_remote_status *status)
0b3e7a8b 637{
600766e8 638 const struct ovs_list *sessions = &remote->sessions;
0b3e7a8b 639 const struct ovsdb_jsonrpc_session *s;
600766e8 640
417e7e66 641 if (ovs_list_is_empty(sessions)) {
600766e8
AZ
642 return false;
643 }
644
417e7e66
BW
645 ovs_assert(ovs_list_is_singleton(sessions));
646 s = CONTAINER_OF(ovs_list_front(sessions), struct ovsdb_jsonrpc_session, node);
600766e8
AZ
647 ovsdb_jsonrpc_session_get_status(s, status);
648 status->n_connections = 1;
649
650 return true;
651}
652
653static void
654ovsdb_jsonrpc_session_get_status(const struct ovsdb_jsonrpc_session *session,
655 struct ovsdb_jsonrpc_remote_status *status)
656{
657 const struct ovsdb_jsonrpc_session *s = session;
0b3e7a8b 658 const struct jsonrpc_session *js;
da897f41 659 struct ovsdb_lock_waiter *waiter;
0b3e7a8b 660 struct reconnect_stats rstats;
da897f41 661 struct ds locks_held, locks_waiting, locks_lost;
0b3e7a8b 662
0b3e7a8b 663 js = s->js;
0b3e7a8b
AE
664
665 status->is_connected = jsonrpc_session_is_connected(js);
666 status->last_error = jsonrpc_session_get_status(js);
667
668 jsonrpc_session_get_reconnect_stats(js, &rstats);
669 status->state = rstats.state;
5eda645e
AE
670 status->sec_since_connect = rstats.msec_since_connect == UINT_MAX
671 ? UINT_MAX : rstats.msec_since_connect / 1000;
672 status->sec_since_disconnect = rstats.msec_since_disconnect == UINT_MAX
673 ? UINT_MAX : rstats.msec_since_disconnect / 1000;
87fcbc60 674
da897f41
BP
675 ds_init(&locks_held);
676 ds_init(&locks_waiting);
677 ds_init(&locks_lost);
678 HMAP_FOR_EACH (waiter, session_node, &s->up.waiters) {
679 struct ds *string;
680
681 string = (ovsdb_lock_waiter_is_owner(waiter) ? &locks_held
682 : waiter->mode == OVSDB_LOCK_WAIT ? &locks_waiting
683 : &locks_lost);
684 if (string->length) {
685 ds_put_char(string, ' ');
686 }
687 ds_put_cstr(string, waiter->lock_name);
688 }
689 status->locks_held = ds_steal_cstr(&locks_held);
690 status->locks_waiting = ds_steal_cstr(&locks_waiting);
691 status->locks_lost = ds_steal_cstr(&locks_lost);
0b3e7a8b
AE
692}
693
b4e8d170
BP
694/* Examines 'request' to determine the database to which it relates, and then
695 * searches 's' to find that database:
696 *
697 * - If successful, returns the database and sets '*replyp' to NULL.
698 *
699 * - If no such database exists, returns NULL and sets '*replyp' to an
700 * appropriate JSON-RPC error reply, owned by the caller. */
701static struct ovsdb *
702ovsdb_jsonrpc_lookup_db(const struct ovsdb_jsonrpc_session *s,
703 const struct jsonrpc_msg *request,
704 struct jsonrpc_msg **replyp)
9cb53f26
BP
705{
706 struct json_array *params;
9cb53f26 707 struct ovsdb_error *error;
b4e8d170
BP
708 const char *db_name;
709 struct ovsdb *db;
9cb53f26
BP
710
711 params = json_array(request->params);
712 if (!params->n || params->elems[0]->type != JSON_STRING) {
713 error = ovsdb_syntax_error(
714 request->params, NULL,
715 "%s request params must begin with <db-name>", request->method);
716 goto error;
717 }
718
b4e8d170
BP
719 db_name = params->elems[0]->u.string;
720 db = shash_find_data(&s->up.server->dbs, db_name);
721 if (!db) {
9cb53f26
BP
722 error = ovsdb_syntax_error(
723 request->params, "unknown database",
724 "%s request specifies unknown database %s",
b4e8d170 725 request->method, db_name);
9cb53f26
BP
726 goto error;
727 }
728
b4e8d170
BP
729 *replyp = NULL;
730 return db;
9cb53f26
BP
731
732error:
201891c3
BP
733 *replyp = jsonrpc_create_error(ovsdb_error_to_json_free(error),
734 request->id);
b4e8d170 735 return NULL;
9cb53f26
BP
736}
737
da897f41
BP
738static struct ovsdb_error *
739ovsdb_jsonrpc_session_parse_lock_name(const struct jsonrpc_msg *request,
740 const char **lock_namep)
741{
742 const struct json_array *params;
743
744 params = json_array(request->params);
745 if (params->n != 1 || params->elems[0]->type != JSON_STRING ||
746 !ovsdb_parser_is_id(json_string(params->elems[0]))) {
747 *lock_namep = NULL;
748 return ovsdb_syntax_error(request->params, NULL,
749 "%s request params must be <id>",
750 request->method);
751 }
752
753 *lock_namep = json_string(params->elems[0]);
754 return NULL;
755}
756
757static void
758ovsdb_jsonrpc_session_notify(struct ovsdb_session *session,
759 const char *lock_name,
760 const char *method)
761{
762 struct ovsdb_jsonrpc_session *s;
763 struct json *params;
764
765 s = CONTAINER_OF(session, struct ovsdb_jsonrpc_session, up);
766 params = json_array_create_1(json_string_create(lock_name));
48f6e410 767 ovsdb_jsonrpc_session_send(s, jsonrpc_create_notify(method, params));
da897f41
BP
768}
769
e51879e9
AZ
770static struct jsonrpc_msg *
771jsonrpc_create_readonly_lock_error(const struct json *id)
772{
773 return jsonrpc_create_error(json_string_create(
774 "lock and unlock methods not allowed,"
775 " DB server is read only."), id);
776}
777
da897f41
BP
778static struct jsonrpc_msg *
779ovsdb_jsonrpc_session_lock(struct ovsdb_jsonrpc_session *s,
780 struct jsonrpc_msg *request,
781 enum ovsdb_lock_mode mode)
782{
783 struct ovsdb_lock_waiter *waiter;
da897f41
BP
784 struct ovsdb_error *error;
785 struct ovsdb_session *victim;
786 const char *lock_name;
787 struct json *result;
788
e51879e9
AZ
789 if (s->read_only) {
790 return jsonrpc_create_readonly_lock_error(request->id);
791 }
792
da897f41
BP
793 error = ovsdb_jsonrpc_session_parse_lock_name(request, &lock_name);
794 if (error) {
795 goto error;
796 }
797
798 /* Report error if this session has issued a "lock" or "steal" without a
799 * matching "unlock" for this lock. */
800 waiter = ovsdb_session_get_lock_waiter(&s->up, lock_name);
801 if (waiter) {
802 error = ovsdb_syntax_error(
803 request->params, NULL,
804 "must issue \"unlock\" before new \"%s\"", request->method);
805 goto error;
806 }
807
808 /* Get the lock, add us as a waiter. */
809 waiter = ovsdb_server_lock(&s->remote->server->up, &s->up, lock_name, mode,
810 &victim);
811 if (victim) {
812 ovsdb_jsonrpc_session_notify(victim, lock_name, "stolen");
813 }
814
815 result = json_object_create();
816 json_object_put(result, "locked",
817 json_boolean_create(ovsdb_lock_waiter_is_owner(waiter)));
818
819 return jsonrpc_create_reply(result, request->id);
820
821error:
201891c3 822 return jsonrpc_create_error(ovsdb_error_to_json_free(error), request->id);
da897f41
BP
823}
824
825static void
826ovsdb_jsonrpc_session_unlock_all(struct ovsdb_jsonrpc_session *s)
827{
828 struct ovsdb_lock_waiter *waiter, *next;
829
830 HMAP_FOR_EACH_SAFE (waiter, next, session_node, &s->up.waiters) {
831 ovsdb_jsonrpc_session_unlock__(waiter);
832 }
833}
834
835static void
836ovsdb_jsonrpc_session_unlock__(struct ovsdb_lock_waiter *waiter)
837{
838 struct ovsdb_lock *lock = waiter->lock;
839
840 if (lock) {
841 struct ovsdb_session *new_owner = ovsdb_lock_waiter_remove(waiter);
842 if (new_owner) {
843 ovsdb_jsonrpc_session_notify(new_owner, lock->name, "locked");
844 } else {
845 /* ovsdb_server_lock() might have freed 'lock'. */
846 }
847 }
848
849 ovsdb_lock_waiter_destroy(waiter);
850}
851
852static struct jsonrpc_msg *
853ovsdb_jsonrpc_session_unlock(struct ovsdb_jsonrpc_session *s,
854 struct jsonrpc_msg *request)
855{
856 struct ovsdb_lock_waiter *waiter;
da897f41
BP
857 struct ovsdb_error *error;
858 const char *lock_name;
859
e51879e9
AZ
860 if (s->read_only) {
861 return jsonrpc_create_readonly_lock_error(request->id);
862 }
863
da897f41
BP
864 error = ovsdb_jsonrpc_session_parse_lock_name(request, &lock_name);
865 if (error) {
866 goto error;
867 }
868
869 /* Report error if this session has not issued a "lock" or "steal" for this
870 * lock. */
871 waiter = ovsdb_session_get_lock_waiter(&s->up, lock_name);
872 if (!waiter) {
873 error = ovsdb_syntax_error(
874 request->params, NULL, "\"unlock\" without \"lock\" or \"steal\"");
875 goto error;
876 }
877
878 ovsdb_jsonrpc_session_unlock__(waiter);
879
880 return jsonrpc_create_reply(json_object_create(), request->id);
881
882error:
201891c3 883 return jsonrpc_create_error(ovsdb_error_to_json_free(error), request->id);
da897f41
BP
884}
885
b93d3b6c 886static struct jsonrpc_msg *
b4e8d170 887execute_transaction(struct ovsdb_jsonrpc_session *s, struct ovsdb *db,
b93d3b6c
BP
888 struct jsonrpc_msg *request)
889{
b4e8d170 890 ovsdb_jsonrpc_trigger_create(s, db, request->id, request->params);
f85f8ebb
BP
891 request->id = NULL;
892 request->params = NULL;
e084f690 893 jsonrpc_msg_destroy(request);
f85f8ebb
BP
894 return NULL;
895}
896
897static void
898ovsdb_jsonrpc_session_got_request(struct ovsdb_jsonrpc_session *s,
899 struct jsonrpc_msg *request)
900{
901 struct jsonrpc_msg *reply;
902
903 if (!strcmp(request->method, "transact")) {
b4e8d170 904 struct ovsdb *db = ovsdb_jsonrpc_lookup_db(s, request, &reply);
9cb53f26 905 if (!reply) {
b4e8d170 906 reply = execute_transaction(s, db, request);
9cb53f26 907 }
92f8d65b 908 } else if (!strcmp(request->method, "monitor") ||
c383f3bf
LS
909 (monitor_cond_enable__ && !strcmp(request->method,
910 "monitor_cond"))) {
b4e8d170 911 struct ovsdb *db = ovsdb_jsonrpc_lookup_db(s, request, &reply);
9cb53f26 912 if (!reply) {
92f8d65b
AZ
913 int l = strlen(request->method) - strlen("monitor");
914 enum ovsdb_monitor_version version = l ? OVSDB_MONITOR_V2
915 : OVSDB_MONITOR_V1;
508624b6 916 reply = ovsdb_jsonrpc_monitor_create(s, db, request->params,
92f8d65b 917 version, request->id);
9cb53f26 918 }
845a1187
LS
919 } else if (!strcmp(request->method, "monitor_cond_change")) {
920 reply = ovsdb_jsonrpc_monitor_cond_change(s, request->params,
921 request->id);
a8425c53
BP
922 } else if (!strcmp(request->method, "monitor_cancel")) {
923 reply = ovsdb_jsonrpc_monitor_cancel(s, json_array(request->params),
924 request->id);
f85f8ebb 925 } else if (!strcmp(request->method, "get_schema")) {
b4e8d170 926 struct ovsdb *db = ovsdb_jsonrpc_lookup_db(s, request, &reply);
9cb53f26 927 if (!reply) {
b4e8d170
BP
928 reply = jsonrpc_create_reply(ovsdb_schema_to_json(db->schema),
929 request->id);
9cb53f26
BP
930 }
931 } else if (!strcmp(request->method, "list_dbs")) {
b4e8d170
BP
932 size_t n_dbs = shash_count(&s->up.server->dbs);
933 struct shash_node *node;
934 struct json **dbs;
935 size_t i;
936
937 dbs = xmalloc(n_dbs * sizeof *dbs);
938 i = 0;
939 SHASH_FOR_EACH (node, &s->up.server->dbs) {
940 dbs[i++] = json_string_create(node->name);
941 }
942 reply = jsonrpc_create_reply(json_array_create(dbs, n_dbs),
943 request->id);
05ac209a
AZ
944 } else if (!strcmp(request->method, "get_server_id")) {
945 const struct uuid *uuid = &s->up.server->uuid;
946 struct json *result;
947
948 result = json_string_create_nocopy(xasprintf(UUID_FMT,
949 UUID_ARGS(uuid)));
950 reply = jsonrpc_create_reply(result, request->id);
da897f41
BP
951 } else if (!strcmp(request->method, "lock")) {
952 reply = ovsdb_jsonrpc_session_lock(s, request, OVSDB_LOCK_WAIT);
953 } else if (!strcmp(request->method, "steal")) {
954 reply = ovsdb_jsonrpc_session_lock(s, request, OVSDB_LOCK_STEAL);
955 } else if (!strcmp(request->method, "unlock")) {
956 reply = ovsdb_jsonrpc_session_unlock(s, request);
6c2882f9
BP
957 } else if (!strcmp(request->method, "echo")) {
958 reply = jsonrpc_create_reply(json_clone(request->params), request->id);
f85f8ebb
BP
959 } else {
960 reply = jsonrpc_create_error(json_string_create("unknown method"),
961 request->id);
962 }
963
964 if (reply) {
965 jsonrpc_msg_destroy(request);
48f6e410 966 ovsdb_jsonrpc_session_send(s, reply);
f85f8ebb
BP
967 }
968}
969
970static void
971execute_cancel(struct ovsdb_jsonrpc_session *s, struct jsonrpc_msg *request)
972{
6e79e210
BP
973 if (json_array(request->params)->n == 1) {
974 struct ovsdb_jsonrpc_trigger *t;
975 struct json *id;
f85f8ebb 976
6e79e210
BP
977 id = request->params->u.array.elems[0];
978 t = ovsdb_jsonrpc_trigger_find(s, id, json_hash(id, 0));
979 if (t) {
980 ovsdb_jsonrpc_trigger_complete(t);
981 }
f85f8ebb
BP
982 }
983}
984
985static void
986ovsdb_jsonrpc_session_got_notify(struct ovsdb_jsonrpc_session *s,
987 struct jsonrpc_msg *request)
988{
989 if (!strcmp(request->method, "cancel")) {
990 execute_cancel(s, request);
991 }
992 jsonrpc_msg_destroy(request);
993}
48f6e410
BP
994
995static void
996ovsdb_jsonrpc_session_send(struct ovsdb_jsonrpc_session *s,
997 struct jsonrpc_msg *msg)
998{
999 ovsdb_jsonrpc_monitor_flush_all(s);
1000 jsonrpc_session_send(s->js, msg);
1001}
b93d3b6c
BP
1002\f
1003/* JSON-RPC database server triggers.
1004 *
1005 * (Every transaction is treated as a trigger even if it doesn't actually have
1006 * any "wait" operations.) */
1007
1008struct ovsdb_jsonrpc_trigger {
1009 struct ovsdb_trigger trigger;
b93d3b6c
BP
1010 struct hmap_node hmap_node; /* In session's "triggers" hmap. */
1011 struct json *id;
1012};
1013
1014static void
b4e8d170 1015ovsdb_jsonrpc_trigger_create(struct ovsdb_jsonrpc_session *s, struct ovsdb *db,
b93d3b6c
BP
1016 struct json *id, struct json *params)
1017{
1018 struct ovsdb_jsonrpc_trigger *t;
1019 size_t hash;
1020
1021 /* Check for duplicate ID. */
1022 hash = json_hash(id, 0);
1023 t = ovsdb_jsonrpc_trigger_find(s, id, hash);
1024 if (t) {
4931f33a
BP
1025 struct jsonrpc_msg *msg;
1026
1027 msg = jsonrpc_create_error(json_string_create("duplicate request ID"),
1028 id);
48f6e410 1029 ovsdb_jsonrpc_session_send(s, msg);
b93d3b6c
BP
1030 json_destroy(id);
1031 json_destroy(params);
1032 return;
1033 }
1034
1035 /* Insert into trigger table. */
1036 t = xmalloc(sizeof *t);
e51879e9 1037 ovsdb_trigger_init(&s->up, db, &t->trigger, params, time_msec(),
d6db7b3c
LR
1038 s->read_only, s->remote->role,
1039 jsonrpc_session_get_id(s->js));
b93d3b6c
BP
1040 t->id = id;
1041 hmap_insert(&s->triggers, &t->hmap_node, hash);
1042
1043 /* Complete early if possible. */
1044 if (ovsdb_trigger_is_complete(&t->trigger)) {
1045 ovsdb_jsonrpc_trigger_complete(t);
1046 }
1047}
1048
1049static struct ovsdb_jsonrpc_trigger *
1050ovsdb_jsonrpc_trigger_find(struct ovsdb_jsonrpc_session *s,
1051 const struct json *id, size_t hash)
1052{
1053 struct ovsdb_jsonrpc_trigger *t;
1054
4e8e4213 1055 HMAP_FOR_EACH_WITH_HASH (t, hmap_node, hash, &s->triggers) {
b93d3b6c
BP
1056 if (json_equal(t->id, id)) {
1057 return t;
1058 }
1059 }
1060
1061 return NULL;
1062}
1063
1064static void
1065ovsdb_jsonrpc_trigger_complete(struct ovsdb_jsonrpc_trigger *t)
1066{
e317253b
BP
1067 struct ovsdb_jsonrpc_session *s;
1068
1069 s = CONTAINER_OF(t->trigger.session, struct ovsdb_jsonrpc_session, up);
b93d3b6c 1070
4931f33a 1071 if (jsonrpc_session_is_connected(s->js)) {
b93d3b6c
BP
1072 struct jsonrpc_msg *reply;
1073 struct json *result;
1074
1075 result = ovsdb_trigger_steal_result(&t->trigger);
1076 if (result) {
1077 reply = jsonrpc_create_reply(result, t->id);
1078 } else {
1079 reply = jsonrpc_create_error(json_string_create("canceled"),
1080 t->id);
1081 }
48f6e410 1082 ovsdb_jsonrpc_session_send(s, reply);
b93d3b6c
BP
1083 }
1084
1085 json_destroy(t->id);
1086 ovsdb_trigger_destroy(&t->trigger);
1087 hmap_remove(&s->triggers, &t->hmap_node);
1088 free(t);
1089}
1090
1091static void
1092ovsdb_jsonrpc_trigger_complete_all(struct ovsdb_jsonrpc_session *s)
1093{
1094 struct ovsdb_jsonrpc_trigger *t, *next;
4e8e4213 1095 HMAP_FOR_EACH_SAFE (t, next, hmap_node, &s->triggers) {
b93d3b6c
BP
1096 ovsdb_jsonrpc_trigger_complete(t);
1097 }
1098}
1099
1100static void
1101ovsdb_jsonrpc_trigger_complete_done(struct ovsdb_jsonrpc_session *s)
1102{
417e7e66 1103 while (!ovs_list_is_empty(&s->up.completions)) {
b93d3b6c 1104 struct ovsdb_jsonrpc_trigger *t
e317253b 1105 = CONTAINER_OF(s->up.completions.next,
b93d3b6c
BP
1106 struct ovsdb_jsonrpc_trigger, trigger.node);
1107 ovsdb_jsonrpc_trigger_complete(t);
1108 }
1109}
a8425c53 1110\f
897af587
AZ
1111/* Jsonrpc front end monitor. */
1112struct ovsdb_jsonrpc_monitor {
ad376c93 1113 struct hmap_node node; /* In ovsdb_jsonrpc_session's "monitors". */
a8425c53 1114 struct ovsdb_jsonrpc_session *session;
b4e8d170 1115 struct ovsdb *db;
a8425c53 1116 struct json *monitor_id;
897af587 1117 struct ovsdb_monitor *dbmon;
59c35e11
AZ
1118 uint64_t unflushed; /* The first transaction that has not been
1119 flushed to the jsonrpc remote client. */
92f8d65b 1120 enum ovsdb_monitor_version version;
71cdf7cd 1121 struct ovsdb_monitor_session_condition *condition;/* Session's condition */
a8425c53
BP
1122};
1123
2fa1df7b 1124static struct ovsdb_jsonrpc_monitor *
a8425c53
BP
1125ovsdb_jsonrpc_monitor_find(struct ovsdb_jsonrpc_session *s,
1126 const struct json *monitor_id)
1127{
1128 struct ovsdb_jsonrpc_monitor *m;
1129
4e8e4213 1130 HMAP_FOR_EACH_WITH_HASH (m, node, json_hash(monitor_id, 0), &s->monitors) {
a8425c53
BP
1131 if (json_equal(m->monitor_id, monitor_id)) {
1132 return m;
1133 }
1134 }
1135
1136 return NULL;
1137}
1138
2fa1df7b
AZ
1139static bool
1140parse_bool(struct ovsdb_parser *parser, const char *name, bool default_value)
20aa445d 1141{
2fa1df7b 1142 const struct json *json;
20aa445d 1143
2fa1df7b
AZ
1144 json = ovsdb_parser_member(parser, name, OP_BOOLEAN | OP_OPTIONAL);
1145 return json ? json_boolean(json) : default_value;
f3395ab3
AZ
1146}
1147
cab50449 1148static struct ovsdb_error * OVS_WARN_UNUSED_RESULT
71cdf7cd
LS
1149ovsdb_jsonrpc_parse_monitor_request(
1150 struct ovsdb_monitor *dbmon,
1151 const struct ovsdb_table *table,
1152 struct ovsdb_monitor_session_condition *cond,
1153 const struct json *monitor_request)
20aa445d 1154{
83d300f6 1155 const struct ovsdb_table_schema *ts = table->schema;
897af587 1156 enum ovsdb_monitor_selection select;
71cdf7cd 1157 const struct json *columns, *select_json, *where = NULL;
20aa445d
BP
1158 struct ovsdb_parser parser;
1159 struct ovsdb_error *error;
1160
1161 ovsdb_parser_init(&parser, monitor_request, "table %s", ts->name);
71cdf7cd
LS
1162 if (cond) {
1163 where = ovsdb_parser_member(&parser, "where", OP_ARRAY | OP_OPTIONAL);
1164 }
20aa445d 1165 columns = ovsdb_parser_member(&parser, "columns", OP_ARRAY | OP_OPTIONAL);
71cdf7cd 1166
20aa445d
BP
1167 select_json = ovsdb_parser_member(&parser, "select",
1168 OP_OBJECT | OP_OPTIONAL);
71cdf7cd 1169
20aa445d
BP
1170 error = ovsdb_parser_finish(&parser);
1171 if (error) {
1172 return error;
1173 }
1174
1175 if (select_json) {
1176 select = 0;
1177 ovsdb_parser_init(&parser, select_json, "table %s select", ts->name);
1178 if (parse_bool(&parser, "initial", true)) {
1179 select |= OJMS_INITIAL;
1180 }
1181 if (parse_bool(&parser, "insert", true)) {
1182 select |= OJMS_INSERT;
1183 }
1184 if (parse_bool(&parser, "delete", true)) {
1185 select |= OJMS_DELETE;
1186 }
1187 if (parse_bool(&parser, "modify", true)) {
1188 select |= OJMS_MODIFY;
1189 }
1190 error = ovsdb_parser_finish(&parser);
1191 if (error) {
1192 return error;
1193 }
1194 } else {
1195 select = OJMS_INITIAL | OJMS_INSERT | OJMS_DELETE | OJMS_MODIFY;
1196 }
20aa445d 1197
ea585a0e 1198 ovsdb_monitor_table_add_select(dbmon, table, select);
20aa445d
BP
1199 if (columns) {
1200 size_t i;
1201
1202 if (columns->type != JSON_ARRAY) {
1203 return ovsdb_syntax_error(columns, NULL,
1204 "array of column names expected");
1205 }
1206
1207 for (i = 0; i < columns->u.array.n; i++) {
1208 const struct ovsdb_column *column;
1209 const char *s;
1210
1211 if (columns->u.array.elems[i]->type != JSON_STRING) {
1212 return ovsdb_syntax_error(columns, NULL,
1213 "array of column names expected");
1214 }
1215
1216 s = columns->u.array.elems[i]->u.string;
ea585a0e 1217 column = shash_find_data(&table->schema->columns, s);
20aa445d
BP
1218 if (!column) {
1219 return ovsdb_syntax_error(columns, NULL, "%s is not a valid "
1220 "column name", s);
1221 }
6150a75f
LS
1222 if (ovsdb_monitor_add_column(dbmon, table, column,
1223 select, true)) {
ec1eadce
LS
1224 return ovsdb_syntax_error(columns, NULL, "column %s "
1225 "mentioned more than once",
1226 column->name);
1227 }
20aa445d
BP
1228 }
1229 } else {
1230 struct shash_node *node;
1231
1232 SHASH_FOR_EACH (node, &ts->columns) {
1233 const struct ovsdb_column *column = node->data;
1234 if (column->index != OVSDB_COL_UUID) {
6150a75f
LS
1235 if (ovsdb_monitor_add_column(dbmon, table, column,
1236 select, true)) {
ec1eadce
LS
1237 return ovsdb_syntax_error(columns, NULL, "column %s "
1238 "mentioned more than once",
1239 column->name);
1240 }
20aa445d
BP
1241 }
1242 }
1243 }
71cdf7cd
LS
1244 if (cond) {
1245 error = ovsdb_monitor_table_condition_create(cond, table, where);
1246 if (error) {
1247 return error;
1248 }
1249 }
20aa445d
BP
1250
1251 return NULL;
1252}
1253
508624b6 1254static struct jsonrpc_msg *
b4e8d170 1255ovsdb_jsonrpc_monitor_create(struct ovsdb_jsonrpc_session *s, struct ovsdb *db,
508624b6 1256 struct json *params,
92f8d65b 1257 enum ovsdb_monitor_version version,
508624b6 1258 const struct json *request_id)
a8425c53
BP
1259{
1260 struct ovsdb_jsonrpc_monitor *m = NULL;
6e5a9216 1261 struct ovsdb_monitor *dbmon = NULL;
a8425c53
BP
1262 struct json *monitor_id, *monitor_requests;
1263 struct ovsdb_error *error = NULL;
1264 struct shash_node *node;
1265 struct json *json;
1266
9cb53f26 1267 if (json_array(params)->n != 3) {
a8425c53
BP
1268 error = ovsdb_syntax_error(params, NULL, "invalid parameters");
1269 goto error;
1270 }
9cb53f26
BP
1271 monitor_id = params->u.array.elems[1];
1272 monitor_requests = params->u.array.elems[2];
a8425c53
BP
1273 if (monitor_requests->type != JSON_OBJECT) {
1274 error = ovsdb_syntax_error(monitor_requests, NULL,
1275 "monitor-requests must be object");
1276 goto error;
1277 }
1278
1279 if (ovsdb_jsonrpc_monitor_find(s, monitor_id)) {
1280 error = ovsdb_syntax_error(monitor_id, NULL, "duplicate monitor ID");
1281 goto error;
1282 }
1283
1284 m = xzalloc(sizeof *m);
a8425c53 1285 m->session = s;
b4e8d170 1286 m->db = db;
2fa1df7b 1287 m->dbmon = ovsdb_monitor_create(db, m);
71cdf7cd
LS
1288 if (version == OVSDB_MONITOR_V2) {
1289 m->condition = ovsdb_monitor_session_condition_create();
1290 }
59c35e11 1291 m->unflushed = 0;
92f8d65b 1292 m->version = version;
a8425c53
BP
1293 hmap_insert(&s->monitors, &m->node, json_hash(monitor_id, 0));
1294 m->monitor_id = json_clone(monitor_id);
a8425c53
BP
1295
1296 SHASH_FOR_EACH (node, json_object(monitor_requests)) {
1297 const struct ovsdb_table *table;
20aa445d
BP
1298 const struct json *mr_value;
1299 size_t i;
a8425c53 1300
b4e8d170 1301 table = ovsdb_get_table(m->db, node->name);
a8425c53
BP
1302 if (!table) {
1303 error = ovsdb_syntax_error(NULL, NULL,
1304 "no table named %s", node->name);
1305 goto error;
1306 }
1307
83d300f6 1308 ovsdb_monitor_add_table(m->dbmon, table);
a8425c53 1309
20aa445d
BP
1310 /* Parse columns. */
1311 mr_value = node->data;
20aa445d
BP
1312 if (mr_value->type == JSON_ARRAY) {
1313 const struct json_array *array = &mr_value->u.array;
1314
1315 for (i = 0; i < array->n; i++) {
6150a75f
LS
1316 error = ovsdb_jsonrpc_parse_monitor_request(m->dbmon,
1317 table,
71cdf7cd 1318 m->condition,
6150a75f 1319 array->elems[i]);
20aa445d
BP
1320 if (error) {
1321 goto error;
1322 }
a8425c53
BP
1323 }
1324 } else {
6150a75f
LS
1325 error = ovsdb_jsonrpc_parse_monitor_request(m->dbmon,
1326 table,
71cdf7cd 1327 m->condition,
6150a75f 1328 mr_value);
20aa445d
BP
1329 if (error) {
1330 goto error;
a8425c53
BP
1331 }
1332 }
a8425c53
BP
1333 }
1334
6e5a9216
AZ
1335 dbmon = ovsdb_monitor_add(m->dbmon);
1336 if (dbmon != m->dbmon) {
1337 /* Found an exisiting dbmon, reuse the current one. */
f76def25 1338 ovsdb_monitor_remove_jsonrpc_monitor(m->dbmon, m, m->unflushed);
6e5a9216
AZ
1339 ovsdb_monitor_add_jsonrpc_monitor(dbmon, m);
1340 m->dbmon = dbmon;
1341 }
1342
71cdf7cd
LS
1343 /* Only now we can bind session's condition to ovsdb_monitor */
1344 if (m->condition) {
1345 ovsdb_monitor_condition_bind(m->dbmon, m->condition);
1346 }
1347
61b63013 1348 ovsdb_monitor_get_initial(m->dbmon);
f1de87bb 1349 json = ovsdb_jsonrpc_monitor_compose_update(m, true);
61b63013
AZ
1350 json = json ? json : json_object_create();
1351 return jsonrpc_create_reply(json, request_id);
a8425c53
BP
1352
1353error:
23f37a97 1354 if (m) {
88b63308 1355 ovsdb_jsonrpc_monitor_destroy(m);
23f37a97 1356 }
a8425c53 1357
201891c3 1358 return jsonrpc_create_error(ovsdb_error_to_json_free(error), request_id);
a8425c53
BP
1359}
1360
845a1187
LS
1361static struct ovsdb_error *
1362ovsdb_jsonrpc_parse_monitor_cond_change_request(
1363 struct ovsdb_jsonrpc_monitor *m,
1364 const struct ovsdb_table *table,
1365 const struct json *cond_change_req)
1366{
1367 const struct ovsdb_table_schema *ts = table->schema;
1368 const struct json *condition, *columns;
1369 struct ovsdb_parser parser;
1370 struct ovsdb_error *error;
1371
1372 ovsdb_parser_init(&parser, cond_change_req, "table %s", ts->name);
1373 columns = ovsdb_parser_member(&parser, "columns", OP_ARRAY | OP_OPTIONAL);
1374 condition = ovsdb_parser_member(&parser, "where", OP_ARRAY | OP_OPTIONAL);
1375
1376 error = ovsdb_parser_finish(&parser);
1377 if (error) {
1378 return error;
1379 }
1380
1381 if (columns) {
1382 error = ovsdb_syntax_error(cond_change_req, NULL, "changing columns "
1383 "is unsupported");
1384 return error;
1385 }
1386 error = ovsdb_monitor_table_condition_update(m->dbmon, m->condition, table,
1387 condition);
1388
1389 return error;
1390}
1391
1392static struct jsonrpc_msg *
1393ovsdb_jsonrpc_monitor_cond_change(struct ovsdb_jsonrpc_session *s,
1394 struct json *params,
1395 const struct json *request_id)
1396{
1397 struct ovsdb_error *error;
1398 struct ovsdb_jsonrpc_monitor *m;
1399 struct json *monitor_cond_change_reqs;
1400 struct shash_node *node;
845a1187
LS
1401
1402 if (json_array(params)->n != 3) {
1403 error = ovsdb_syntax_error(params, NULL, "invalid parameters");
1404 goto error;
1405 }
1406
1407 m = ovsdb_jsonrpc_monitor_find(s, params->u.array.elems[0]);
1408 if (!m) {
a01ca4ee 1409 error = ovsdb_syntax_error(params->u.array.elems[0], NULL,
845a1187
LS
1410 "unknown monitor session");
1411 goto error;
1412 }
1413
404682c6
BP
1414 const struct json *new_monitor_id = params->u.array.elems[1];
1415 bool changing_id = !json_equal(m->monitor_id, new_monitor_id);
1416 if (changing_id && ovsdb_jsonrpc_monitor_find(s, new_monitor_id)) {
1417 error = ovsdb_syntax_error(new_monitor_id, NULL,
1418 "duplicate monitor ID");
1419 goto error;
1420 }
1421
845a1187
LS
1422 monitor_cond_change_reqs = params->u.array.elems[2];
1423 if (monitor_cond_change_reqs->type != JSON_OBJECT) {
1424 error =
1425 ovsdb_syntax_error(NULL, NULL,
1426 "monitor-cond-change-requests must be object");
1427 goto error;
1428 }
1429
1430 SHASH_FOR_EACH (node, json_object(monitor_cond_change_reqs)) {
1431 const struct ovsdb_table *table;
1432 const struct json *mr_value;
1433 size_t i;
1434
1435 table = ovsdb_get_table(m->db, node->name);
1436 if (!table) {
1437 error = ovsdb_syntax_error(NULL, NULL,
1438 "no table named %s", node->name);
1439 goto error;
1440 }
1441 if (!ovsdb_monitor_table_exists(m->dbmon, table)) {
1442 error = ovsdb_syntax_error(NULL, NULL,
1443 "no table named %s in monitor session",
1444 node->name);
1445 goto error;
1446 }
1447
1448 mr_value = node->data;
1449 if (mr_value->type == JSON_ARRAY) {
1450 const struct json_array *array = &mr_value->u.array;
1451
1452 for (i = 0; i < array->n; i++) {
1453 error = ovsdb_jsonrpc_parse_monitor_cond_change_request(
1454 m, table, array->elems[i]);
1455 if (error) {
1456 goto error;
1457 }
1458 }
1459 } else {
1460 error = ovsdb_syntax_error(
1461 NULL, NULL,
1462 "table %s no monitor-cond-change JSON array",
1463 node->name);
1464 goto error;
1465 }
1466 }
1467
404682c6
BP
1468 if (changing_id) {
1469 hmap_remove(&s->monitors, &m->node);
1470 json_destroy(m->monitor_id);
1471 m->monitor_id = json_clone(new_monitor_id);
1472 hmap_insert(&s->monitors, &m->node, json_hash(m->monitor_id, 0));
1473 }
845a1187
LS
1474
1475 /* Send the new update, if any, represents the difference from the old
1476 * condition and the new one. */
1477 struct json *update_json;
1478
1479 update_json = ovsdb_monitor_get_update(m->dbmon, false, true,
1480 &m->unflushed, m->condition, m->version);
1481 if (update_json) {
1482 struct jsonrpc_msg *msg;
71f21279 1483 struct json *p;
845a1187 1484
71f21279
BP
1485 p = json_array_create_2(json_clone(m->monitor_id), update_json);
1486 msg = ovsdb_jsonrpc_create_notify(m, p);
845a1187
LS
1487 jsonrpc_session_send(s->js, msg);
1488 }
1489
1490 return jsonrpc_create_reply(json_object_create(), request_id);
1491
1492error:
201891c3 1493 return jsonrpc_create_error(ovsdb_error_to_json_free(error), request_id);
845a1187
LS
1494}
1495
a8425c53
BP
1496static struct jsonrpc_msg *
1497ovsdb_jsonrpc_monitor_cancel(struct ovsdb_jsonrpc_session *s,
1498 struct json_array *params,
1499 const struct json *request_id)
1500{
1501 if (params->n != 1) {
1502 return jsonrpc_create_error(json_string_create("invalid parameters"),
1503 request_id);
1504 } else {
1505 struct ovsdb_jsonrpc_monitor *m;
1506
1507 m = ovsdb_jsonrpc_monitor_find(s, params->elems[0]);
1508 if (!m) {
1509 return jsonrpc_create_error(json_string_create("unknown monitor"),
1510 request_id);
1511 } else {
88b63308 1512 ovsdb_jsonrpc_monitor_destroy(m);
a8425c53
BP
1513 return jsonrpc_create_reply(json_object_create(), request_id);
1514 }
1515 }
1516}
1517
1518static void
1519ovsdb_jsonrpc_monitor_remove_all(struct ovsdb_jsonrpc_session *s)
1520{
1521 struct ovsdb_jsonrpc_monitor *m, *next;
1522
4e8e4213 1523 HMAP_FOR_EACH_SAFE (m, next, node, &s->monitors) {
88b63308 1524 ovsdb_jsonrpc_monitor_destroy(m);
a8425c53
BP
1525 }
1526}
1527
92d5d643 1528static struct json *
1158f320
AZ
1529ovsdb_jsonrpc_monitor_compose_update(struct ovsdb_jsonrpc_monitor *m,
1530 bool initial)
92d5d643 1531{
845a1187 1532
8cffdcd0
AZ
1533 if (!ovsdb_monitor_needs_flush(m->dbmon, m->unflushed)) {
1534 return NULL;
1535 }
1536
845a1187
LS
1537 return ovsdb_monitor_get_update(m->dbmon, initial, false,
1538 &m->unflushed, m->condition, m->version);
92d5d643
AZ
1539}
1540
48f6e410
BP
1541static bool
1542ovsdb_jsonrpc_monitor_needs_flush(struct ovsdb_jsonrpc_session *s)
1543{
1544 struct ovsdb_jsonrpc_monitor *m;
1545
1546 HMAP_FOR_EACH (m, node, &s->monitors) {
59c35e11 1547 if (ovsdb_monitor_needs_flush(m->dbmon, m->unflushed)) {
dbc1cfbb 1548 return true;
48f6e410
BP
1549 }
1550 }
1551
1552 return false;
1553}
1554
2fa1df7b
AZ
1555void
1556ovsdb_jsonrpc_monitor_destroy(struct ovsdb_jsonrpc_monitor *m)
1557{
1558 json_destroy(m->monitor_id);
1559 hmap_remove(&m->session->monitors, &m->node);
f76def25 1560 ovsdb_monitor_remove_jsonrpc_monitor(m->dbmon, m, m->unflushed);
71cdf7cd 1561 ovsdb_monitor_session_condition_destroy(m->condition);
2fa1df7b
AZ
1562 free(m);
1563}
1564
92f8d65b
AZ
1565static struct jsonrpc_msg *
1566ovsdb_jsonrpc_create_notify(const struct ovsdb_jsonrpc_monitor *m,
1567 struct json *params)
1568{
1569 const char *method;
1570
1571 switch(m->version) {
1572 case OVSDB_MONITOR_V1:
1573 method = "update";
1574 break;
1575 case OVSDB_MONITOR_V2:
1576 method = "update2";
1577 break;
1578 case OVSDB_MONITOR_VERSION_MAX:
1579 default:
1580 OVS_NOT_REACHED();
1581 }
1582
1583 return jsonrpc_create_notify(method, params);
1584}
1585
64fada26
AZ
1586const struct uuid *
1587ovsdb_jsonrpc_server_get_uuid(const struct ovsdb_jsonrpc_server *s)
1588{
1589 return &s->up.uuid;
1590}
1591
48f6e410
BP
1592static void
1593ovsdb_jsonrpc_monitor_flush_all(struct ovsdb_jsonrpc_session *s)
1594{
1595 struct ovsdb_jsonrpc_monitor *m;
1596
1597 HMAP_FOR_EACH (m, node, &s->monitors) {
1598 struct json *json;
1599
f1de87bb 1600 json = ovsdb_jsonrpc_monitor_compose_update(m, false);
48f6e410
BP
1601 if (json) {
1602 struct jsonrpc_msg *msg;
1603 struct json *params;
1604
1605 params = json_array_create_2(json_clone(m->monitor_id), json);
92f8d65b 1606 msg = ovsdb_jsonrpc_create_notify(m, params);
48f6e410
BP
1607 jsonrpc_session_send(s->js, msg);
1608 }
1609 }
1610}
e47cb14c
AZ
1611
1612void
c383f3bf 1613ovsdb_jsonrpc_disable_monitor_cond(void)
e47cb14c
AZ
1614{
1615 /* Once disabled, it is not possible to re-enable it. */
c383f3bf 1616 monitor_cond_enable__ = false;
e47cb14c 1617}