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