]>
Commit | Line | Data |
---|---|---|
f2129093 | 1 | /* |
8cf6bbb1 | 2 | * Copyright (c) 2009-2017 Nicira, Inc. |
f2129093 BP |
3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | * you may not use this file except in compliance with the License. | |
6 | * You may obtain a copy of the License at: | |
7 | * | |
8 | * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | * | |
10 | * Unless required by applicable law or agreed to in writing, software | |
11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | * See the License for the specific language governing permissions and | |
14 | * limitations under the License. | |
15 | */ | |
16 | ||
17 | #include <config.h> | |
18 | ||
19 | #include "jsonrpc.h" | |
20 | ||
21 | #include <errno.h> | |
22 | ||
23 | #include "byteq.h" | |
3e8a2ad1 | 24 | #include "openvswitch/dynamic-string.h" |
b302749b | 25 | #include "fatal-signal.h" |
ee89ea7b | 26 | #include "openvswitch/json.h" |
b19bab5b | 27 | #include "openvswitch/list.h" |
64c96779 | 28 | #include "openvswitch/ofpbuf.h" |
d7eea710 | 29 | #include "ovs-thread.h" |
fd016ae3 | 30 | #include "openvswitch/poll-loop.h" |
dcbb691b | 31 | #include "reconnect.h" |
f2129093 | 32 | #include "stream.h" |
8cf6bbb1 | 33 | #include "svec.h" |
dcbb691b | 34 | #include "timeval.h" |
e6211adc | 35 | #include "openvswitch/vlog.h" |
5136ce49 | 36 | |
d98e6007 | 37 | VLOG_DEFINE_THIS_MODULE(jsonrpc); |
f2129093 BP |
38 | \f |
39 | struct jsonrpc { | |
40 | struct stream *stream; | |
41 | char *name; | |
42 | int status; | |
43 | ||
44 | /* Input. */ | |
45 | struct byteq input; | |
ea5c1ba0 | 46 | uint8_t input_buffer[4096]; |
f2129093 | 47 | struct json_parser *parser; |
f2129093 BP |
48 | |
49 | /* Output. */ | |
ca6ba700 | 50 | struct ovs_list output; /* Contains "struct ofpbuf"s. */ |
65b81af3 | 51 | size_t output_count; /* Number of elements in "output". */ |
f2129093 BP |
52 | size_t backlog; |
53 | }; | |
54 | ||
55 | /* Rate limit for error messages. */ | |
56 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 5); | |
57 | ||
79bda825 | 58 | static struct jsonrpc_msg *jsonrpc_parse_received_message(struct jsonrpc *); |
f2129093 | 59 | static void jsonrpc_cleanup(struct jsonrpc *); |
c1ce8fbf | 60 | static void jsonrpc_error(struct jsonrpc *, int error); |
f2129093 | 61 | |
0d11f523 | 62 | /* This is just the same as stream_open() except that it uses the default |
ca843648 | 63 | * JSONRPC port if none is specified. */ |
0d11f523 | 64 | int |
f125905c | 65 | jsonrpc_stream_open(const char *name, struct stream **streamp, uint8_t dscp) |
0d11f523 | 66 | { |
d4763d1d | 67 | return stream_open_with_default_port(name, OVSDB_PORT, streamp, dscp); |
0d11f523 BP |
68 | } |
69 | ||
70 | /* This is just the same as pstream_open() except that it uses the default | |
ca843648 | 71 | * JSONRPC port if none is specified. */ |
0d11f523 | 72 | int |
f125905c | 73 | jsonrpc_pstream_open(const char *name, struct pstream **pstreamp, uint8_t dscp) |
0d11f523 | 74 | { |
d4763d1d | 75 | return pstream_open_with_default_port(name, OVSDB_PORT, pstreamp, dscp); |
0d11f523 BP |
76 | } |
77 | ||
78fdd76d BP |
78 | /* Returns a new JSON-RPC stream that uses 'stream' for input and output. The |
79 | * new jsonrpc object takes ownership of 'stream'. */ | |
f2129093 BP |
80 | struct jsonrpc * |
81 | jsonrpc_open(struct stream *stream) | |
82 | { | |
83 | struct jsonrpc *rpc; | |
84 | ||
cb22974d | 85 | ovs_assert(stream != NULL); |
f2129093 BP |
86 | |
87 | rpc = xzalloc(sizeof *rpc); | |
88 | rpc->name = xstrdup(stream_get_name(stream)); | |
89 | rpc->stream = stream; | |
00ecc5ce | 90 | byteq_init(&rpc->input, rpc->input_buffer, sizeof rpc->input_buffer); |
417e7e66 | 91 | ovs_list_init(&rpc->output); |
f2129093 BP |
92 | |
93 | return rpc; | |
94 | } | |
95 | ||
78fdd76d BP |
96 | /* Destroys 'rpc', closing the stream on which it is based, and frees its |
97 | * memory. */ | |
f2129093 BP |
98 | void |
99 | jsonrpc_close(struct jsonrpc *rpc) | |
100 | { | |
101 | if (rpc) { | |
102 | jsonrpc_cleanup(rpc); | |
103 | free(rpc->name); | |
104 | free(rpc); | |
105 | } | |
106 | } | |
107 | ||
78fdd76d | 108 | /* Performs periodic maintenance on 'rpc', such as flushing output buffers. */ |
f2129093 BP |
109 | void |
110 | jsonrpc_run(struct jsonrpc *rpc) | |
111 | { | |
112 | if (rpc->status) { | |
113 | return; | |
114 | } | |
115 | ||
539e96f6 | 116 | stream_run(rpc->stream); |
417e7e66 | 117 | while (!ovs_list_is_empty(&rpc->output)) { |
b3907fbc | 118 | struct ofpbuf *buf = ofpbuf_from_list(rpc->output.next); |
f2129093 BP |
119 | int retval; |
120 | ||
6fd6ed71 | 121 | retval = stream_send(rpc->stream, buf->data, buf->size); |
f2129093 BP |
122 | if (retval >= 0) { |
123 | rpc->backlog -= retval; | |
124 | ofpbuf_pull(buf, retval); | |
6fd6ed71 | 125 | if (!buf->size) { |
417e7e66 | 126 | ovs_list_remove(&buf->list_node); |
65b81af3 | 127 | rpc->output_count--; |
b3907fbc | 128 | ofpbuf_delete(buf); |
f2129093 BP |
129 | } |
130 | } else { | |
131 | if (retval != -EAGAIN) { | |
132 | VLOG_WARN_RL(&rl, "%s: send error: %s", | |
10a89ef0 | 133 | rpc->name, ovs_strerror(-retval)); |
f2129093 BP |
134 | jsonrpc_error(rpc, -retval); |
135 | } | |
136 | break; | |
137 | } | |
138 | } | |
139 | } | |
140 | ||
78fdd76d BP |
141 | /* Arranges for the poll loop to wake up when 'rpc' needs to perform |
142 | * maintenance activities. */ | |
f2129093 BP |
143 | void |
144 | jsonrpc_wait(struct jsonrpc *rpc) | |
145 | { | |
539e96f6 BP |
146 | if (!rpc->status) { |
147 | stream_run_wait(rpc->stream); | |
417e7e66 | 148 | if (!ovs_list_is_empty(&rpc->output)) { |
539e96f6 BP |
149 | stream_send_wait(rpc->stream); |
150 | } | |
f2129093 BP |
151 | } |
152 | } | |
153 | ||
c18ea70d | 154 | /* |
78fdd76d | 155 | * Returns the current status of 'rpc'. The possible return values are: |
c18ea70d AE |
156 | * - 0: no error yet |
157 | * - >0: errno value | |
78fdd76d BP |
158 | * - EOF: end of file (remote end closed connection; not necessarily an error). |
159 | * | |
2a8e0b7c JP |
160 | * When this function returns nonzero, 'rpc' is effectively out of |
161 | * commission. 'rpc' will not receive any more messages and any further | |
162 | * messages that one attempts to send with 'rpc' will be discarded. The | |
163 | * caller can keep 'rpc' around as long as it wants, but it's not going | |
164 | * to provide any more useful services. | |
c18ea70d | 165 | */ |
f2129093 BP |
166 | int |
167 | jsonrpc_get_status(const struct jsonrpc *rpc) | |
168 | { | |
169 | return rpc->status; | |
170 | } | |
171 | ||
78fdd76d BP |
172 | /* Returns the number of bytes buffered by 'rpc' to be written to the |
173 | * underlying stream. Always returns 0 if 'rpc' has encountered an error or if | |
174 | * the remote end closed the connection. */ | |
f2129093 BP |
175 | size_t |
176 | jsonrpc_get_backlog(const struct jsonrpc *rpc) | |
177 | { | |
178 | return rpc->status ? 0 : rpc->backlog; | |
179 | } | |
180 | ||
3a8d38c8 BP |
181 | /* Returns the number of bytes that have been received on 'rpc''s underlying |
182 | * stream. (The value wraps around if it exceeds UINT_MAX.) */ | |
183 | unsigned int | |
184 | jsonrpc_get_received_bytes(const struct jsonrpc *rpc) | |
185 | { | |
186 | return rpc->input.head; | |
187 | } | |
188 | ||
78fdd76d BP |
189 | /* Returns 'rpc''s name, that is, the name returned by stream_get_name() for |
190 | * the stream underlying 'rpc' when 'rpc' was created. */ | |
f2129093 BP |
191 | const char * |
192 | jsonrpc_get_name(const struct jsonrpc *rpc) | |
193 | { | |
194 | return rpc->name; | |
195 | } | |
196 | ||
1fd13cde BP |
197 | static void |
198 | jsonrpc_log_msg(const struct jsonrpc *rpc, const char *title, | |
199 | const struct jsonrpc_msg *msg) | |
200 | { | |
201 | if (VLOG_IS_DBG_ENABLED()) { | |
202 | struct ds s = DS_EMPTY_INITIALIZER; | |
203 | if (msg->method) { | |
204 | ds_put_format(&s, ", method=\"%s\"", msg->method); | |
205 | } | |
206 | if (msg->params) { | |
207 | ds_put_cstr(&s, ", params="); | |
36d802ae | 208 | json_to_ds(msg->params, 0, &s); |
1fd13cde BP |
209 | } |
210 | if (msg->result) { | |
211 | ds_put_cstr(&s, ", result="); | |
36d802ae | 212 | json_to_ds(msg->result, 0, &s); |
1fd13cde BP |
213 | } |
214 | if (msg->error) { | |
215 | ds_put_cstr(&s, ", error="); | |
36d802ae | 216 | json_to_ds(msg->error, 0, &s); |
1fd13cde BP |
217 | } |
218 | if (msg->id) { | |
219 | ds_put_cstr(&s, ", id="); | |
36d802ae | 220 | json_to_ds(msg->id, 0, &s); |
1fd13cde BP |
221 | } |
222 | VLOG_DBG("%s: %s %s%s", rpc->name, title, | |
223 | jsonrpc_msg_type_to_string(msg->type), ds_cstr(&s)); | |
224 | ds_destroy(&s); | |
225 | } | |
226 | } | |
227 | ||
78fdd76d BP |
228 | /* Schedules 'msg' to be sent on 'rpc' and returns 'rpc''s status (as with |
229 | * jsonrpc_get_status()). | |
230 | * | |
231 | * If 'msg' cannot be sent immediately, it is appended to a buffer. The caller | |
232 | * is responsible for ensuring that the amount of buffered data is somehow | |
233 | * limited. (jsonrpc_get_backlog() returns the amount of data currently | |
234 | * buffered in 'rpc'.) | |
235 | * | |
236 | * Always takes ownership of 'msg', regardless of success. */ | |
f2129093 BP |
237 | int |
238 | jsonrpc_send(struct jsonrpc *rpc, struct jsonrpc_msg *msg) | |
239 | { | |
240 | struct ofpbuf *buf; | |
241 | struct json *json; | |
71cc59f9 | 242 | struct ds ds = DS_EMPTY_INITIALIZER; |
f2129093 | 243 | size_t length; |
f2129093 BP |
244 | |
245 | if (rpc->status) { | |
246 | jsonrpc_msg_destroy(msg); | |
247 | return rpc->status; | |
248 | } | |
249 | ||
1fd13cde BP |
250 | jsonrpc_log_msg(rpc, "send", msg); |
251 | ||
f2129093 | 252 | json = jsonrpc_msg_to_json(msg); |
71cc59f9 AZ |
253 | json_to_ds(json, 0, &ds); |
254 | length = ds.length; | |
f2129093 BP |
255 | json_destroy(json); |
256 | ||
257 | buf = xmalloc(sizeof *buf); | |
2db02185 | 258 | ofpbuf_use_ds(buf, &ds); |
417e7e66 | 259 | ovs_list_push_back(&rpc->output, &buf->list_node); |
65b81af3 | 260 | rpc->output_count++; |
f2129093 BP |
261 | rpc->backlog += length; |
262 | ||
65b81af3 AW |
263 | if (rpc->output_count >= 50) { |
264 | VLOG_INFO_RL(&rl, "excessive sending backlog, jsonrpc: %s, num of" | |
265 | " msgs: %"PRIuSIZE", backlog: %"PRIuSIZE".", rpc->name, | |
266 | rpc->output_count, rpc->backlog); | |
267 | } | |
268 | ||
b3907fbc | 269 | if (rpc->backlog == length) { |
f2129093 BP |
270 | jsonrpc_run(rpc); |
271 | } | |
272 | return rpc->status; | |
273 | } | |
274 | ||
78fdd76d BP |
275 | /* Attempts to receive a message from 'rpc'. |
276 | * | |
277 | * If successful, stores the received message in '*msgp' and returns 0. The | |
278 | * caller takes ownership of '*msgp' and must eventually destroy it with | |
279 | * jsonrpc_msg_destroy(). | |
280 | * | |
281 | * Otherwise, stores NULL in '*msgp' and returns one of the following: | |
282 | * | |
283 | * - EAGAIN: No message has been received. | |
284 | * | |
285 | * - EOF: The remote end closed the connection gracefully. | |
286 | * | |
287 | * - Otherwise an errno value that represents a JSON-RPC protocol violation | |
288 | * or another error fatal to the connection. 'rpc' will not send or | |
289 | * receive any more messages. | |
290 | */ | |
f2129093 BP |
291 | int |
292 | jsonrpc_recv(struct jsonrpc *rpc, struct jsonrpc_msg **msgp) | |
293 | { | |
3a4548cf BP |
294 | int i; |
295 | ||
f2129093 BP |
296 | *msgp = NULL; |
297 | if (rpc->status) { | |
298 | return rpc->status; | |
299 | } | |
300 | ||
3a4548cf | 301 | for (i = 0; i < 50; i++) { |
79bda825 BP |
302 | size_t n, used; |
303 | ||
304 | /* Fill our input buffer if it's empty. */ | |
305 | if (byteq_is_empty(&rpc->input)) { | |
f2129093 BP |
306 | size_t chunk; |
307 | int retval; | |
308 | ||
309 | chunk = byteq_headroom(&rpc->input); | |
310 | retval = stream_recv(rpc->stream, byteq_head(&rpc->input), chunk); | |
311 | if (retval < 0) { | |
312 | if (retval == -EAGAIN) { | |
313 | return EAGAIN; | |
314 | } else { | |
315 | VLOG_WARN_RL(&rl, "%s: receive error: %s", | |
10a89ef0 | 316 | rpc->name, ovs_strerror(-retval)); |
f2129093 BP |
317 | jsonrpc_error(rpc, -retval); |
318 | return rpc->status; | |
319 | } | |
320 | } else if (retval == 0) { | |
f2129093 BP |
321 | jsonrpc_error(rpc, EOF); |
322 | return EOF; | |
323 | } | |
324 | byteq_advance_head(&rpc->input, retval); | |
79bda825 | 325 | } |
f2129093 | 326 | |
79bda825 BP |
327 | /* We have some input. Feed it into the JSON parser. */ |
328 | if (!rpc->parser) { | |
329 | rpc->parser = json_parser_create(0); | |
330 | } | |
331 | n = byteq_tailroom(&rpc->input); | |
332 | used = json_parser_feed(rpc->parser, | |
333 | (char *) byteq_tail(&rpc->input), n); | |
334 | byteq_advance_tail(&rpc->input, used); | |
335 | ||
336 | /* If we have complete JSON, attempt to parse it as JSON-RPC. */ | |
337 | if (json_parser_is_done(rpc->parser)) { | |
338 | *msgp = jsonrpc_parse_received_message(rpc); | |
339 | if (*msgp) { | |
340 | return 0; | |
f2129093 | 341 | } |
79bda825 BP |
342 | |
343 | if (rpc->status) { | |
344 | const struct byteq *q = &rpc->input; | |
345 | if (q->head <= q->size) { | |
346 | stream_report_content(q->buffer, q->head, STREAM_JSONRPC, | |
922fed06 | 347 | &this_module, rpc->name); |
f2129093 | 348 | } |
79bda825 | 349 | return rpc->status; |
f2129093 BP |
350 | } |
351 | } | |
352 | } | |
353 | ||
3a4548cf | 354 | return EAGAIN; |
f2129093 BP |
355 | } |
356 | ||
78fdd76d BP |
357 | /* Causes the poll loop to wake up when jsonrpc_recv() may return a value other |
358 | * than EAGAIN. */ | |
f2129093 BP |
359 | void |
360 | jsonrpc_recv_wait(struct jsonrpc *rpc) | |
361 | { | |
79bda825 | 362 | if (rpc->status || !byteq_is_empty(&rpc->input)) { |
5453ae20 | 363 | poll_immediate_wake_at(rpc->name); |
f2129093 BP |
364 | } else { |
365 | stream_recv_wait(rpc->stream); | |
366 | } | |
367 | } | |
368 | ||
78fdd76d BP |
369 | /* Sends 'msg' on 'rpc' and waits for it to be successfully queued to the |
370 | * underlying stream. Returns 0 if 'msg' was sent successfully, otherwise a | |
371 | * status value (see jsonrpc_get_status()). | |
372 | * | |
373 | * Always takes ownership of 'msg', regardless of success. */ | |
f2129093 BP |
374 | int |
375 | jsonrpc_send_block(struct jsonrpc *rpc, struct jsonrpc_msg *msg) | |
376 | { | |
377 | int error; | |
378 | ||
b302749b BP |
379 | fatal_signal_run(); |
380 | ||
f2129093 BP |
381 | error = jsonrpc_send(rpc, msg); |
382 | if (error) { | |
383 | return error; | |
384 | } | |
385 | ||
f3d00a23 | 386 | for (;;) { |
f2129093 | 387 | jsonrpc_run(rpc); |
417e7e66 | 388 | if (ovs_list_is_empty(&rpc->output) || rpc->status) { |
f3d00a23 BP |
389 | return rpc->status; |
390 | } | |
f2129093 BP |
391 | jsonrpc_wait(rpc); |
392 | poll_block(); | |
393 | } | |
f2129093 BP |
394 | } |
395 | ||
78fdd76d BP |
396 | /* Waits for a message to be received on 'rpc'. Same semantics as |
397 | * jsonrpc_recv() except that EAGAIN will never be returned. */ | |
f2129093 BP |
398 | int |
399 | jsonrpc_recv_block(struct jsonrpc *rpc, struct jsonrpc_msg **msgp) | |
400 | { | |
401 | for (;;) { | |
402 | int error = jsonrpc_recv(rpc, msgp); | |
403 | if (error != EAGAIN) { | |
b302749b | 404 | fatal_signal_run(); |
f2129093 BP |
405 | return error; |
406 | } | |
407 | ||
408 | jsonrpc_run(rpc); | |
409 | jsonrpc_wait(rpc); | |
410 | jsonrpc_recv_wait(rpc); | |
411 | poll_block(); | |
412 | } | |
413 | } | |
414 | ||
78fdd76d BP |
415 | /* Sends 'request' to 'rpc' then waits for a reply. The return value is 0 if |
416 | * successful, in which case '*replyp' is set to the reply, which the caller | |
417 | * must eventually free with jsonrpc_msg_destroy(). Otherwise returns a status | |
418 | * value (see jsonrpc_get_status()). | |
419 | * | |
420 | * Discards any message received on 'rpc' that is not a reply to 'request' | |
421 | * (based on message id). | |
422 | * | |
423 | * Always takes ownership of 'request', regardless of success. */ | |
d0632593 BP |
424 | int |
425 | jsonrpc_transact_block(struct jsonrpc *rpc, struct jsonrpc_msg *request, | |
426 | struct jsonrpc_msg **replyp) | |
427 | { | |
428 | struct jsonrpc_msg *reply = NULL; | |
429 | struct json *id; | |
430 | int error; | |
431 | ||
432 | id = json_clone(request->id); | |
433 | error = jsonrpc_send_block(rpc, request); | |
434 | if (!error) { | |
435 | for (;;) { | |
436 | error = jsonrpc_recv_block(rpc, &reply); | |
d35f8e72 EJ |
437 | if (error) { |
438 | break; | |
439 | } | |
440 | if ((reply->type == JSONRPC_REPLY || reply->type == JSONRPC_ERROR) | |
441 | && json_equal(id, reply->id)) { | |
d0632593 BP |
442 | break; |
443 | } | |
444 | jsonrpc_msg_destroy(reply); | |
445 | } | |
446 | } | |
447 | *replyp = error ? NULL : reply; | |
448 | json_destroy(id); | |
449 | return error; | |
450 | } | |
451 | ||
79bda825 BP |
452 | /* Attempts to parse the content of 'rpc->parser' (which is complete JSON) as a |
453 | * JSON-RPC message. If successful, returns the JSON-RPC message. On failure, | |
454 | * signals an error on 'rpc' with jsonrpc_error() and returns NULL. */ | |
455 | static struct jsonrpc_msg * | |
456 | jsonrpc_parse_received_message(struct jsonrpc *rpc) | |
f2129093 BP |
457 | { |
458 | struct jsonrpc_msg *msg; | |
459 | struct json *json; | |
460 | char *error; | |
461 | ||
462 | json = json_parser_finish(rpc->parser); | |
463 | rpc->parser = NULL; | |
464 | if (json->type == JSON_STRING) { | |
465 | VLOG_WARN_RL(&rl, "%s: error parsing stream: %s", | |
466 | rpc->name, json_string(json)); | |
467 | jsonrpc_error(rpc, EPROTO); | |
468 | json_destroy(json); | |
79bda825 | 469 | return NULL; |
f2129093 BP |
470 | } |
471 | ||
472 | error = jsonrpc_msg_from_json(json, &msg); | |
473 | if (error) { | |
474 | VLOG_WARN_RL(&rl, "%s: received bad JSON-RPC message: %s", | |
475 | rpc->name, error); | |
476 | free(error); | |
477 | jsonrpc_error(rpc, EPROTO); | |
79bda825 | 478 | return NULL; |
f2129093 BP |
479 | } |
480 | ||
1fd13cde | 481 | jsonrpc_log_msg(rpc, "received", msg); |
79bda825 | 482 | return msg; |
f2129093 BP |
483 | } |
484 | ||
c1ce8fbf | 485 | static void |
f2129093 BP |
486 | jsonrpc_error(struct jsonrpc *rpc, int error) |
487 | { | |
cb22974d | 488 | ovs_assert(error); |
f2129093 BP |
489 | if (!rpc->status) { |
490 | rpc->status = error; | |
491 | jsonrpc_cleanup(rpc); | |
492 | } | |
493 | } | |
494 | ||
495 | static void | |
496 | jsonrpc_cleanup(struct jsonrpc *rpc) | |
497 | { | |
498 | stream_close(rpc->stream); | |
499 | rpc->stream = NULL; | |
500 | ||
501 | json_parser_abort(rpc->parser); | |
502 | rpc->parser = NULL; | |
503 | ||
b3907fbc | 504 | ofpbuf_list_delete(&rpc->output); |
f2129093 | 505 | rpc->backlog = 0; |
65b81af3 | 506 | rpc->output_count = 0; |
f2129093 BP |
507 | } |
508 | \f | |
509 | static struct jsonrpc_msg * | |
510 | jsonrpc_create(enum jsonrpc_msg_type type, const char *method, | |
511 | struct json *params, struct json *result, struct json *error, | |
512 | struct json *id) | |
513 | { | |
514 | struct jsonrpc_msg *msg = xmalloc(sizeof *msg); | |
515 | msg->type = type; | |
2225c0b9 | 516 | msg->method = nullable_xstrdup(method); |
f2129093 BP |
517 | msg->params = params; |
518 | msg->result = result; | |
519 | msg->error = error; | |
520 | msg->id = id; | |
521 | return msg; | |
522 | } | |
523 | ||
524 | static struct json * | |
525 | jsonrpc_create_id(void) | |
526 | { | |
ca4fbdfe | 527 | static atomic_count next_id = ATOMIC_COUNT_INIT(0); |
d7eea710 BP |
528 | unsigned int id; |
529 | ||
ca4fbdfe | 530 | id = atomic_count_inc(&next_id); |
d7eea710 | 531 | return json_integer_create(id); |
f2129093 BP |
532 | } |
533 | ||
534 | struct jsonrpc_msg * | |
20bed8be BP |
535 | jsonrpc_create_request(const char *method, struct json *params, |
536 | struct json **idp) | |
f2129093 | 537 | { |
20bed8be BP |
538 | struct json *id = jsonrpc_create_id(); |
539 | if (idp) { | |
540 | *idp = json_clone(id); | |
541 | } | |
542 | return jsonrpc_create(JSONRPC_REQUEST, method, params, NULL, NULL, id); | |
f2129093 BP |
543 | } |
544 | ||
545 | struct jsonrpc_msg * | |
546 | jsonrpc_create_notify(const char *method, struct json *params) | |
547 | { | |
548 | return jsonrpc_create(JSONRPC_NOTIFY, method, params, NULL, NULL, NULL); | |
549 | } | |
550 | ||
551 | struct jsonrpc_msg * | |
552 | jsonrpc_create_reply(struct json *result, const struct json *id) | |
553 | { | |
554 | return jsonrpc_create(JSONRPC_REPLY, NULL, NULL, result, NULL, | |
555 | json_clone(id)); | |
556 | } | |
557 | ||
558 | struct jsonrpc_msg * | |
559 | jsonrpc_create_error(struct json *error, const struct json *id) | |
560 | { | |
561 | return jsonrpc_create(JSONRPC_REPLY, NULL, NULL, NULL, error, | |
562 | json_clone(id)); | |
563 | } | |
564 | ||
1b1d2e6d BP |
565 | struct jsonrpc_msg * |
566 | jsonrpc_msg_clone(const struct jsonrpc_msg *old) | |
567 | { | |
568 | return jsonrpc_create(old->type, old->method, | |
569 | json_nullable_clone(old->params), | |
570 | json_nullable_clone(old->result), | |
571 | json_nullable_clone(old->error), | |
572 | json_nullable_clone(old->id)); | |
573 | } | |
574 | ||
f2129093 BP |
575 | const char * |
576 | jsonrpc_msg_type_to_string(enum jsonrpc_msg_type type) | |
577 | { | |
578 | switch (type) { | |
579 | case JSONRPC_REQUEST: | |
580 | return "request"; | |
581 | ||
582 | case JSONRPC_NOTIFY: | |
583 | return "notification"; | |
584 | ||
585 | case JSONRPC_REPLY: | |
586 | return "reply"; | |
587 | ||
588 | case JSONRPC_ERROR: | |
589 | return "error"; | |
590 | } | |
591 | return "(null)"; | |
592 | } | |
593 | ||
594 | char * | |
595 | jsonrpc_msg_is_valid(const struct jsonrpc_msg *m) | |
596 | { | |
597 | const char *type_name; | |
598 | unsigned int pattern; | |
599 | ||
600 | if (m->params && m->params->type != JSON_ARRAY) { | |
601 | return xstrdup("\"params\" must be JSON array"); | |
602 | } | |
603 | ||
604 | switch (m->type) { | |
605 | case JSONRPC_REQUEST: | |
606 | pattern = 0x11001; | |
607 | break; | |
608 | ||
609 | case JSONRPC_NOTIFY: | |
610 | pattern = 0x11000; | |
611 | break; | |
612 | ||
613 | case JSONRPC_REPLY: | |
614 | pattern = 0x00101; | |
615 | break; | |
616 | ||
617 | case JSONRPC_ERROR: | |
618 | pattern = 0x00011; | |
619 | break; | |
620 | ||
621 | default: | |
622 | return xasprintf("invalid JSON-RPC message type %d", m->type); | |
623 | } | |
624 | ||
625 | type_name = jsonrpc_msg_type_to_string(m->type); | |
626 | if ((m->method != NULL) != ((pattern & 0x10000) != 0)) { | |
627 | return xasprintf("%s must%s have \"method\"", | |
628 | type_name, (pattern & 0x10000) ? "" : " not"); | |
629 | ||
630 | } | |
631 | if ((m->params != NULL) != ((pattern & 0x1000) != 0)) { | |
632 | return xasprintf("%s must%s have \"params\"", | |
633 | type_name, (pattern & 0x1000) ? "" : " not"); | |
634 | ||
635 | } | |
636 | if ((m->result != NULL) != ((pattern & 0x100) != 0)) { | |
637 | return xasprintf("%s must%s have \"result\"", | |
638 | type_name, (pattern & 0x100) ? "" : " not"); | |
639 | ||
640 | } | |
641 | if ((m->error != NULL) != ((pattern & 0x10) != 0)) { | |
642 | return xasprintf("%s must%s have \"error\"", | |
643 | type_name, (pattern & 0x10) ? "" : " not"); | |
644 | ||
645 | } | |
646 | if ((m->id != NULL) != ((pattern & 0x1) != 0)) { | |
647 | return xasprintf("%s must%s have \"id\"", | |
648 | type_name, (pattern & 0x1) ? "" : " not"); | |
649 | ||
650 | } | |
651 | return NULL; | |
652 | } | |
653 | ||
654 | void | |
655 | jsonrpc_msg_destroy(struct jsonrpc_msg *m) | |
656 | { | |
657 | if (m) { | |
658 | free(m->method); | |
659 | json_destroy(m->params); | |
660 | json_destroy(m->result); | |
661 | json_destroy(m->error); | |
662 | json_destroy(m->id); | |
663 | free(m); | |
664 | } | |
665 | } | |
666 | ||
667 | static struct json * | |
668 | null_from_json_null(struct json *json) | |
669 | { | |
670 | if (json && json->type == JSON_NULL) { | |
671 | json_destroy(json); | |
672 | return NULL; | |
673 | } | |
674 | return json; | |
675 | } | |
676 | ||
677 | char * | |
678 | jsonrpc_msg_from_json(struct json *json, struct jsonrpc_msg **msgp) | |
679 | { | |
680 | struct json *method = NULL; | |
681 | struct jsonrpc_msg *msg = NULL; | |
682 | struct shash *object; | |
683 | char *error; | |
684 | ||
685 | if (json->type != JSON_OBJECT) { | |
686 | error = xstrdup("message is not a JSON object"); | |
687 | goto exit; | |
688 | } | |
689 | object = json_object(json); | |
690 | ||
691 | method = shash_find_and_delete(object, "method"); | |
692 | if (method && method->type != JSON_STRING) { | |
693 | error = xstrdup("method is not a JSON string"); | |
694 | goto exit; | |
695 | } | |
696 | ||
697 | msg = xzalloc(sizeof *msg); | |
fa37affa | 698 | msg->method = method ? xstrdup(method->string) : NULL; |
f2129093 BP |
699 | msg->params = null_from_json_null(shash_find_and_delete(object, "params")); |
700 | msg->result = null_from_json_null(shash_find_and_delete(object, "result")); | |
701 | msg->error = null_from_json_null(shash_find_and_delete(object, "error")); | |
702 | msg->id = null_from_json_null(shash_find_and_delete(object, "id")); | |
703 | msg->type = (msg->result ? JSONRPC_REPLY | |
704 | : msg->error ? JSONRPC_ERROR | |
705 | : msg->id ? JSONRPC_REQUEST | |
706 | : JSONRPC_NOTIFY); | |
707 | if (!shash_is_empty(object)) { | |
708 | error = xasprintf("message has unexpected member \"%s\"", | |
709 | shash_first(object)->name); | |
710 | goto exit; | |
711 | } | |
712 | error = jsonrpc_msg_is_valid(msg); | |
713 | if (error) { | |
714 | goto exit; | |
715 | } | |
716 | ||
717 | exit: | |
718 | json_destroy(method); | |
719 | json_destroy(json); | |
720 | if (error) { | |
721 | jsonrpc_msg_destroy(msg); | |
722 | msg = NULL; | |
723 | } | |
724 | *msgp = msg; | |
725 | return error; | |
726 | } | |
727 | ||
303566c5 BP |
728 | /* Returns 'm' converted to JSON suitable for sending as a JSON-RPC message. |
729 | * | |
730 | * Consumes and destroys 'm'. */ | |
f2129093 BP |
731 | struct json * |
732 | jsonrpc_msg_to_json(struct jsonrpc_msg *m) | |
733 | { | |
734 | struct json *json = json_object_create(); | |
735 | ||
736 | if (m->method) { | |
737 | json_object_put(json, "method", json_string_create_nocopy(m->method)); | |
738 | } | |
739 | ||
740 | if (m->params) { | |
741 | json_object_put(json, "params", m->params); | |
742 | } | |
743 | ||
744 | if (m->result) { | |
745 | json_object_put(json, "result", m->result); | |
746 | } else if (m->type == JSONRPC_ERROR) { | |
747 | json_object_put(json, "result", json_null_create()); | |
748 | } | |
749 | ||
750 | if (m->error) { | |
751 | json_object_put(json, "error", m->error); | |
752 | } else if (m->type == JSONRPC_REPLY) { | |
753 | json_object_put(json, "error", json_null_create()); | |
754 | } | |
755 | ||
756 | if (m->id) { | |
757 | json_object_put(json, "id", m->id); | |
758 | } else if (m->type == JSONRPC_NOTIFY) { | |
759 | json_object_put(json, "id", json_null_create()); | |
760 | } | |
761 | ||
762 | free(m); | |
763 | ||
764 | return json; | |
765 | } | |
1b1d2e6d BP |
766 | |
767 | char * | |
768 | jsonrpc_msg_to_string(const struct jsonrpc_msg *m) | |
769 | { | |
770 | struct jsonrpc_msg *copy = jsonrpc_msg_clone(m); | |
771 | struct json *json = jsonrpc_msg_to_json(copy); | |
772 | char *s = json_to_string(json, JSSF_SORT); | |
773 | json_destroy(json); | |
774 | return s; | |
775 | } | |
dcbb691b BP |
776 | \f |
777 | /* A JSON-RPC session with reconnection. */ | |
778 | ||
779 | struct jsonrpc_session { | |
8cf6bbb1 BP |
780 | struct svec remotes; |
781 | size_t next_remote; | |
782 | ||
dcbb691b BP |
783 | struct reconnect *reconnect; |
784 | struct jsonrpc *rpc; | |
785 | struct stream *stream; | |
c9f3f37a | 786 | struct pstream *pstream; |
fba6bd1d | 787 | int last_error; |
dcbb691b | 788 | unsigned int seqno; |
317f6420 | 789 | uint8_t dscp; |
dcbb691b BP |
790 | }; |
791 | ||
8cf6bbb1 BP |
792 | static void |
793 | jsonrpc_session_pick_remote(struct jsonrpc_session *s) | |
794 | { | |
795 | reconnect_set_name(s->reconnect, | |
796 | s->remotes.names[s->next_remote++ % s->remotes.n]); | |
797 | } | |
798 | ||
c9f3f37a BP |
799 | /* Creates and returns a jsonrpc_session to 'name', which should be a string |
800 | * acceptable to stream_open() or pstream_open(). | |
801 | * | |
802 | * If 'name' is an active connection method, e.g. "tcp:127.1.2.3", the new | |
fba6bd1d BP |
803 | * jsonrpc_session connects to 'name'. If 'retry' is true, then the new |
804 | * session connects and reconnects to 'name', with backoff. If 'retry' is | |
805 | * false, the new session will only try to connect once and after a connection | |
806 | * failure or a disconnection jsonrpc_session_is_alive() will return false for | |
807 | * the new session. | |
c9f3f37a BP |
808 | * |
809 | * If 'name' is a passive connection method, e.g. "ptcp:", the new | |
810 | * jsonrpc_session listens for connections to 'name'. It maintains at most one | |
811 | * connection at any given time. Any new connection causes the previous one | |
812 | * (if any) to be dropped. */ | |
dcbb691b | 813 | struct jsonrpc_session * |
fba6bd1d | 814 | jsonrpc_session_open(const char *name, bool retry) |
8cf6bbb1 BP |
815 | { |
816 | const struct svec remotes = { .names = (char **) &name, .n = 1 }; | |
817 | return jsonrpc_session_open_multiple(&remotes, retry); | |
818 | } | |
819 | ||
820 | struct jsonrpc_session * | |
821 | jsonrpc_session_open_multiple(const struct svec *remotes, bool retry) | |
dcbb691b BP |
822 | { |
823 | struct jsonrpc_session *s; | |
824 | ||
825 | s = xmalloc(sizeof *s); | |
8cf6bbb1 | 826 | |
d5905055 | 827 | /* Set 'n' remotes from 'names'. */ |
8cf6bbb1 BP |
828 | ovs_assert(remotes->n > 0); |
829 | svec_clone(&s->remotes, remotes); | |
8cf6bbb1 BP |
830 | s->next_remote = 0; |
831 | ||
dcbb691b | 832 | s->reconnect = reconnect_create(time_msec()); |
8cf6bbb1 | 833 | jsonrpc_session_pick_remote(s); |
dcbb691b | 834 | reconnect_enable(s->reconnect, time_msec()); |
8cf6bbb1 | 835 | reconnect_set_backoff_free_tries(s->reconnect, remotes->n); |
dcbb691b BP |
836 | s->rpc = NULL; |
837 | s->stream = NULL; | |
c9f3f37a | 838 | s->pstream = NULL; |
dcbb691b | 839 | s->seqno = 0; |
317f6420 | 840 | s->dscp = 0; |
fba6bd1d | 841 | s->last_error = 0; |
dcbb691b | 842 | |
8cf6bbb1 | 843 | const char *name = reconnect_get_name(s->reconnect); |
c9f3f37a BP |
844 | if (!pstream_verify_name(name)) { |
845 | reconnect_set_passive(s->reconnect, true, time_msec()); | |
fba6bd1d | 846 | } else if (!retry) { |
8cf6bbb1 | 847 | reconnect_set_max_tries(s->reconnect, remotes->n); |
fba6bd1d | 848 | reconnect_set_backoff(s->reconnect, INT_MAX, INT_MAX); |
c9f3f37a BP |
849 | } |
850 | ||
f1936eb6 EJ |
851 | if (!stream_or_pstream_needs_probes(name)) { |
852 | reconnect_set_probe_interval(s->reconnect, 0); | |
853 | } | |
854 | ||
dcbb691b BP |
855 | return s; |
856 | } | |
857 | ||
4931f33a | 858 | /* Creates and returns a jsonrpc_session that is initially connected to |
36a7b32d BP |
859 | * 'jsonrpc'. If the connection is dropped, it will not be reconnected. |
860 | * | |
861 | * On the assumption that such connections are likely to be short-lived | |
862 | * (e.g. from ovs-vsctl), informational logging for them is suppressed. */ | |
4931f33a | 863 | struct jsonrpc_session * |
e879d33e | 864 | jsonrpc_session_open_unreliably(struct jsonrpc *jsonrpc, uint8_t dscp) |
4931f33a BP |
865 | { |
866 | struct jsonrpc_session *s; | |
867 | ||
868 | s = xmalloc(sizeof *s); | |
8cf6bbb1 BP |
869 | svec_init(&s->remotes); |
870 | svec_add(&s->remotes, jsonrpc_get_name(jsonrpc)); | |
871 | s->next_remote = 0; | |
4931f33a | 872 | s->reconnect = reconnect_create(time_msec()); |
36a7b32d | 873 | reconnect_set_quiet(s->reconnect, true); |
4931f33a BP |
874 | reconnect_set_name(s->reconnect, jsonrpc_get_name(jsonrpc)); |
875 | reconnect_set_max_tries(s->reconnect, 0); | |
876 | reconnect_connected(s->reconnect, time_msec()); | |
e879d33e | 877 | s->dscp = dscp; |
4931f33a BP |
878 | s->rpc = jsonrpc; |
879 | s->stream = NULL; | |
c9f3f37a | 880 | s->pstream = NULL; |
238f0052 | 881 | s->seqno = 1; |
4931f33a BP |
882 | |
883 | return s; | |
884 | } | |
885 | ||
dcbb691b BP |
886 | void |
887 | jsonrpc_session_close(struct jsonrpc_session *s) | |
888 | { | |
889 | if (s) { | |
890 | jsonrpc_close(s->rpc); | |
891 | reconnect_destroy(s->reconnect); | |
7c88a5dc | 892 | stream_close(s->stream); |
c9f3f37a | 893 | pstream_close(s->pstream); |
8cf6bbb1 | 894 | svec_destroy(&s->remotes); |
dcbb691b BP |
895 | free(s); |
896 | } | |
897 | } | |
898 | ||
1b1d2e6d BP |
899 | struct jsonrpc * |
900 | jsonrpc_session_steal(struct jsonrpc_session *s) | |
901 | { | |
902 | struct jsonrpc *rpc = s->rpc; | |
903 | s->rpc = NULL; | |
904 | jsonrpc_session_close(s); | |
905 | return rpc; | |
906 | } | |
907 | ||
dcbb691b BP |
908 | static void |
909 | jsonrpc_session_disconnect(struct jsonrpc_session *s) | |
910 | { | |
dcbb691b BP |
911 | if (s->rpc) { |
912 | jsonrpc_error(s->rpc, EOF); | |
913 | jsonrpc_close(s->rpc); | |
914 | s->rpc = NULL; | |
dcbb691b BP |
915 | } else if (s->stream) { |
916 | stream_close(s->stream); | |
917 | s->stream = NULL; | |
8cf6bbb1 BP |
918 | } else { |
919 | return; | |
dcbb691b | 920 | } |
8cf6bbb1 BP |
921 | |
922 | s->seqno++; | |
923 | jsonrpc_session_pick_remote(s); | |
dcbb691b BP |
924 | } |
925 | ||
926 | static void | |
927 | jsonrpc_session_connect(struct jsonrpc_session *s) | |
928 | { | |
c9f3f37a | 929 | const char *name = reconnect_get_name(s->reconnect); |
dcbb691b BP |
930 | int error; |
931 | ||
932 | jsonrpc_session_disconnect(s); | |
c9f3f37a | 933 | if (!reconnect_is_passive(s->reconnect)) { |
317f6420 | 934 | error = jsonrpc_stream_open(name, &s->stream, s->dscp); |
c9f3f37a BP |
935 | if (!error) { |
936 | reconnect_connecting(s->reconnect, time_msec()); | |
fba6bd1d BP |
937 | } else { |
938 | s->last_error = error; | |
c9f3f37a BP |
939 | } |
940 | } else { | |
f125905c | 941 | error = s->pstream ? 0 : jsonrpc_pstream_open(name, &s->pstream, |
317f6420 | 942 | s->dscp); |
c9f3f37a BP |
943 | if (!error) { |
944 | reconnect_listening(s->reconnect, time_msec()); | |
945 | } | |
946 | } | |
947 | ||
dcbb691b BP |
948 | if (error) { |
949 | reconnect_connect_failed(s->reconnect, time_msec(), error); | |
8cf6bbb1 | 950 | jsonrpc_session_pick_remote(s); |
dcbb691b | 951 | } |
dcbb691b BP |
952 | } |
953 | ||
954 | void | |
955 | jsonrpc_session_run(struct jsonrpc_session *s) | |
956 | { | |
c9f3f37a BP |
957 | if (s->pstream) { |
958 | struct stream *stream; | |
959 | int error; | |
960 | ||
961 | error = pstream_accept(s->pstream, &stream); | |
962 | if (!error) { | |
963 | if (s->rpc || s->stream) { | |
964 | VLOG_INFO_RL(&rl, | |
965 | "%s: new connection replacing active connection", | |
966 | reconnect_get_name(s->reconnect)); | |
967 | jsonrpc_session_disconnect(s); | |
968 | } | |
969 | reconnect_connected(s->reconnect, time_msec()); | |
970 | s->rpc = jsonrpc_open(stream); | |
238f0052 | 971 | s->seqno++; |
c9f3f37a BP |
972 | } else if (error != EAGAIN) { |
973 | reconnect_listen_error(s->reconnect, time_msec(), error); | |
974 | pstream_close(s->pstream); | |
975 | s->pstream = NULL; | |
976 | } | |
977 | } | |
978 | ||
dcbb691b | 979 | if (s->rpc) { |
f97cae29 | 980 | size_t backlog; |
dcbb691b BP |
981 | int error; |
982 | ||
f97cae29 | 983 | backlog = jsonrpc_get_backlog(s->rpc); |
dcbb691b | 984 | jsonrpc_run(s->rpc); |
f97cae29 BP |
985 | if (jsonrpc_get_backlog(s->rpc) < backlog) { |
986 | /* Data previously caught in a queue was successfully sent (or | |
987 | * there's an error, which we'll catch below.) | |
988 | * | |
989 | * We don't count data that is successfully sent immediately as | |
990 | * activity, because there's a lot of queuing downstream from us, | |
991 | * which means that we can push a lot of data into a connection | |
992 | * that has stalled and won't ever recover. | |
993 | */ | |
994 | reconnect_activity(s->reconnect, time_msec()); | |
995 | } | |
996 | ||
dcbb691b BP |
997 | error = jsonrpc_get_status(s->rpc); |
998 | if (error) { | |
41630cfb | 999 | reconnect_disconnected(s->reconnect, time_msec(), error); |
dcbb691b | 1000 | jsonrpc_session_disconnect(s); |
fba6bd1d | 1001 | s->last_error = error; |
dcbb691b BP |
1002 | } |
1003 | } else if (s->stream) { | |
539e96f6 BP |
1004 | int error; |
1005 | ||
1006 | stream_run(s->stream); | |
1007 | error = stream_connect(s->stream); | |
dcbb691b BP |
1008 | if (!error) { |
1009 | reconnect_connected(s->reconnect, time_msec()); | |
1010 | s->rpc = jsonrpc_open(s->stream); | |
1011 | s->stream = NULL; | |
238f0052 | 1012 | s->seqno++; |
dcbb691b BP |
1013 | } else if (error != EAGAIN) { |
1014 | reconnect_connect_failed(s->reconnect, time_msec(), error); | |
8cf6bbb1 | 1015 | jsonrpc_session_pick_remote(s); |
dcbb691b BP |
1016 | stream_close(s->stream); |
1017 | s->stream = NULL; | |
5bf6cbd6 | 1018 | s->last_error = error; |
dcbb691b BP |
1019 | } |
1020 | } | |
1021 | ||
1022 | switch (reconnect_run(s->reconnect, time_msec())) { | |
1023 | case RECONNECT_CONNECT: | |
1024 | jsonrpc_session_connect(s); | |
1025 | break; | |
1026 | ||
1027 | case RECONNECT_DISCONNECT: | |
a1ae9a43 | 1028 | reconnect_disconnected(s->reconnect, time_msec(), 0); |
dcbb691b BP |
1029 | jsonrpc_session_disconnect(s); |
1030 | break; | |
1031 | ||
1032 | case RECONNECT_PROBE: | |
1033 | if (s->rpc) { | |
1034 | struct json *params; | |
1035 | struct jsonrpc_msg *request; | |
1036 | ||
1037 | params = json_array_create_empty(); | |
20bed8be | 1038 | request = jsonrpc_create_request("echo", params, NULL); |
dcbb691b BP |
1039 | json_destroy(request->id); |
1040 | request->id = json_string_create("echo"); | |
1041 | jsonrpc_send(s->rpc, request); | |
1042 | } | |
1043 | break; | |
1044 | } | |
1045 | } | |
1046 | ||
1047 | void | |
1048 | jsonrpc_session_wait(struct jsonrpc_session *s) | |
1049 | { | |
1050 | if (s->rpc) { | |
1051 | jsonrpc_wait(s->rpc); | |
1052 | } else if (s->stream) { | |
539e96f6 | 1053 | stream_run_wait(s->stream); |
dcbb691b BP |
1054 | stream_connect_wait(s->stream); |
1055 | } | |
c9f3f37a BP |
1056 | if (s->pstream) { |
1057 | pstream_wait(s->pstream); | |
1058 | } | |
dcbb691b BP |
1059 | reconnect_wait(s->reconnect, time_msec()); |
1060 | } | |
1061 | ||
1062 | size_t | |
1063 | jsonrpc_session_get_backlog(const struct jsonrpc_session *s) | |
1064 | { | |
1065 | return s->rpc ? jsonrpc_get_backlog(s->rpc) : 0; | |
1066 | } | |
1067 | ||
0b3e7a8b AE |
1068 | /* Always returns a pointer to a valid C string, assuming 's' was initialized |
1069 | * correctly. */ | |
dcbb691b BP |
1070 | const char * |
1071 | jsonrpc_session_get_name(const struct jsonrpc_session *s) | |
1072 | { | |
1073 | return reconnect_get_name(s->reconnect); | |
1074 | } | |
1075 | ||
d6db7b3c LR |
1076 | const char * |
1077 | jsonrpc_session_get_id(const struct jsonrpc_session *s) | |
1078 | { | |
1079 | if (s->rpc && s->rpc->stream) { | |
1080 | return stream_get_peer_id(s->rpc->stream); | |
1081 | } else { | |
1082 | return NULL; | |
1083 | } | |
1084 | } | |
1085 | ||
8cf6bbb1 BP |
1086 | size_t |
1087 | jsonrpc_session_get_n_remotes(const struct jsonrpc_session *s) | |
1088 | { | |
1089 | return s->remotes.n; | |
1090 | } | |
1091 | ||
7b8dbc8d | 1092 | /* Always takes ownership of 'msg', regardless of success. */ |
dcbb691b BP |
1093 | int |
1094 | jsonrpc_session_send(struct jsonrpc_session *s, struct jsonrpc_msg *msg) | |
1095 | { | |
7b8dbc8d BP |
1096 | if (s->rpc) { |
1097 | return jsonrpc_send(s->rpc, msg); | |
1098 | } else { | |
1099 | jsonrpc_msg_destroy(msg); | |
1100 | return ENOTCONN; | |
1101 | } | |
dcbb691b BP |
1102 | } |
1103 | ||
1104 | struct jsonrpc_msg * | |
1105 | jsonrpc_session_recv(struct jsonrpc_session *s) | |
1106 | { | |
dcbb691b | 1107 | if (s->rpc) { |
3a8d38c8 | 1108 | unsigned int received_bytes; |
4931f33a | 1109 | struct jsonrpc_msg *msg; |
3a8d38c8 BP |
1110 | |
1111 | received_bytes = jsonrpc_get_received_bytes(s->rpc); | |
dcbb691b | 1112 | jsonrpc_recv(s->rpc, &msg); |
3a8d38c8 BP |
1113 | if (received_bytes != jsonrpc_get_received_bytes(s->rpc)) { |
1114 | /* Data was successfully received. | |
1115 | * | |
1116 | * Previously we only counted receiving a full message as activity, | |
1117 | * but with large messages or a slow connection that policy could | |
1118 | * time out the session mid-message. */ | |
a6f639f8 | 1119 | reconnect_activity(s->reconnect, time_msec()); |
3a8d38c8 BP |
1120 | } |
1121 | ||
1122 | if (msg) { | |
4931f33a BP |
1123 | if (msg->type == JSONRPC_REQUEST && !strcmp(msg->method, "echo")) { |
1124 | /* Echo request. Send reply. */ | |
1125 | struct jsonrpc_msg *reply; | |
1126 | ||
1127 | reply = jsonrpc_create_reply(json_clone(msg->params), msg->id); | |
1128 | jsonrpc_session_send(s, reply); | |
1129 | } else if (msg->type == JSONRPC_REPLY | |
33ff0dce | 1130 | && msg->id && msg->id->type == JSON_STRING |
fa37affa | 1131 | && !strcmp(msg->id->string, "echo")) { |
4931f33a BP |
1132 | /* It's a reply to our echo request. Suppress it. */ |
1133 | } else { | |
1134 | return msg; | |
1135 | } | |
1136 | jsonrpc_msg_destroy(msg); | |
dcbb691b BP |
1137 | } |
1138 | } | |
4931f33a | 1139 | return NULL; |
dcbb691b BP |
1140 | } |
1141 | ||
1142 | void | |
1143 | jsonrpc_session_recv_wait(struct jsonrpc_session *s) | |
1144 | { | |
1145 | if (s->rpc) { | |
1146 | jsonrpc_recv_wait(s->rpc); | |
1147 | } | |
1148 | } | |
1149 | ||
2e2f242e | 1150 | /* Returns true if 's' is currently connected or trying to connect. */ |
4931f33a BP |
1151 | bool |
1152 | jsonrpc_session_is_alive(const struct jsonrpc_session *s) | |
1153 | { | |
1154 | return s->rpc || s->stream || reconnect_get_max_tries(s->reconnect); | |
1155 | } | |
1156 | ||
2e2f242e | 1157 | /* Returns true if 's' is currently connected. */ |
dcbb691b BP |
1158 | bool |
1159 | jsonrpc_session_is_connected(const struct jsonrpc_session *s) | |
1160 | { | |
1161 | return s->rpc != NULL; | |
1162 | } | |
1163 | ||
2e2f242e BP |
1164 | /* Returns a sequence number for 's'. The sequence number increments every |
1165 | * time 's' connects or disconnects. Thus, a caller can use the change (or | |
1166 | * lack of change) in the sequence number to figure out whether the underlying | |
1167 | * connection is the same as before. */ | |
dcbb691b BP |
1168 | unsigned int |
1169 | jsonrpc_session_get_seqno(const struct jsonrpc_session *s) | |
1170 | { | |
1171 | return s->seqno; | |
1172 | } | |
1173 | ||
2e2f242e BP |
1174 | /* Returns the current status of 's'. If 's' is NULL or is disconnected, this |
1175 | * is 0, otherwise it is the status of the connection, as reported by | |
1176 | * jsonrpc_get_status(). */ | |
0b3e7a8b AE |
1177 | int |
1178 | jsonrpc_session_get_status(const struct jsonrpc_session *s) | |
1179 | { | |
1180 | return s && s->rpc ? jsonrpc_get_status(s->rpc) : 0; | |
1181 | } | |
1182 | ||
2e2f242e BP |
1183 | /* Returns the last error reported on a connection by 's'. The return value is |
1184 | * 0 only if no connection made by 's' has ever encountered an error. See | |
1185 | * jsonrpc_get_status() for return value interpretation. */ | |
fba6bd1d BP |
1186 | int |
1187 | jsonrpc_session_get_last_error(const struct jsonrpc_session *s) | |
1188 | { | |
1189 | return s->last_error; | |
1190 | } | |
1191 | ||
2e2f242e | 1192 | /* Populates 'stats' with statistics from 's'. */ |
0b3e7a8b AE |
1193 | void |
1194 | jsonrpc_session_get_reconnect_stats(const struct jsonrpc_session *s, | |
1195 | struct reconnect_stats *stats) | |
1196 | { | |
1197 | reconnect_get_stats(s->reconnect, time_msec(), stats); | |
1198 | } | |
1199 | ||
2e2f242e | 1200 | /* Enables 's' to reconnect to the peer if the connection drops. */ |
705d7a39 AA |
1201 | void |
1202 | jsonrpc_session_enable_reconnect(struct jsonrpc_session *s) | |
1203 | { | |
1204 | reconnect_set_max_tries(s->reconnect, UINT_MAX); | |
1205 | reconnect_set_backoff(s->reconnect, RECONNECT_DEFAULT_MIN_BACKOFF, | |
1206 | RECONNECT_DEFAULT_MAX_BACKOFF); | |
1207 | } | |
1208 | ||
2e2f242e | 1209 | /* Forces 's' to drop its connection (if any) and reconnect. */ |
dcbb691b BP |
1210 | void |
1211 | jsonrpc_session_force_reconnect(struct jsonrpc_session *s) | |
1212 | { | |
1213 | reconnect_force_reconnect(s->reconnect, time_msec()); | |
1214 | } | |
94db5407 | 1215 | |
2e2f242e BP |
1216 | /* Sets 'max_backoff' as the maximum time, in milliseconds, to wait after a |
1217 | * connection attempt fails before attempting to connect again. */ | |
94db5407 BP |
1218 | void |
1219 | jsonrpc_session_set_max_backoff(struct jsonrpc_session *s, int max_backoff) | |
1220 | { | |
1221 | reconnect_set_backoff(s->reconnect, 0, max_backoff); | |
1222 | } | |
1223 | ||
2e2f242e BP |
1224 | /* Sets the "probe interval" for 's' to 'probe_interval', in milliseconds. If |
1225 | * this is zero, it disables the connection keepalive feature. Otherwise, if | |
1226 | * 's' is idle for 'probe_interval' milliseconds then 's' will send an echo | |
1227 | * request and, if no reply is received within an additional 'probe_interval' | |
1228 | * milliseconds, close the connection (then reconnect, if that feature is | |
1229 | * enabled). */ | |
94db5407 BP |
1230 | void |
1231 | jsonrpc_session_set_probe_interval(struct jsonrpc_session *s, | |
1232 | int probe_interval) | |
1233 | { | |
1234 | reconnect_set_probe_interval(s->reconnect, probe_interval); | |
1235 | } | |
f125905c | 1236 | |
2e2f242e BP |
1237 | /* Sets the DSCP value used for 's''s connection to 'dscp'. If this is |
1238 | * different from the DSCP value currently in use then the connection is closed | |
1239 | * and reconnected. */ | |
f125905c | 1240 | void |
2e2f242e | 1241 | jsonrpc_session_set_dscp(struct jsonrpc_session *s, uint8_t dscp) |
f125905c | 1242 | { |
0442efd9 | 1243 | if (s->dscp != dscp) { |
c2e3cbaf BP |
1244 | pstream_close(s->pstream); |
1245 | s->pstream = NULL; | |
e879d33e | 1246 | |
0442efd9 MM |
1247 | s->dscp = dscp; |
1248 | jsonrpc_session_force_reconnect(s); | |
1249 | } | |
f125905c | 1250 | } |