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