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