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