]>
git.proxmox.com Git - mirror_ovs.git/blob - lib/jsonrpc.c
2 * Copyright (c) 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
24 #include "dynamic-string.h"
25 #include "fatal-signal.h"
29 #include "ovs-thread.h"
30 #include "poll-loop.h"
31 #include "reconnect.h"
36 VLOG_DEFINE_THIS_MODULE(jsonrpc
);
39 struct stream
*stream
;
45 uint8_t input_buffer
[512];
46 struct json_parser
*parser
;
47 struct jsonrpc_msg
*received
;
50 struct list output
; /* Contains "struct ofpbuf"s. */
54 /* Rate limit for error messages. */
55 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(5, 5);
57 static void jsonrpc_received(struct jsonrpc
*);
58 static void jsonrpc_cleanup(struct jsonrpc
*);
59 static void jsonrpc_error(struct jsonrpc
*, int error
);
61 /* This is just the same as stream_open() except that it uses the default
62 * JSONRPC ports if none is specified. */
64 jsonrpc_stream_open(const char *name
, struct stream
**streamp
, uint8_t dscp
)
66 return stream_open_with_default_ports(name
, JSONRPC_TCP_PORT
,
67 JSONRPC_SSL_PORT
, streamp
,
71 /* This is just the same as pstream_open() except that it uses the default
72 * JSONRPC ports if none is specified. */
74 jsonrpc_pstream_open(const char *name
, struct pstream
**pstreamp
, uint8_t dscp
)
76 return pstream_open_with_default_ports(name
, JSONRPC_TCP_PORT
,
77 JSONRPC_SSL_PORT
, pstreamp
, dscp
);
80 /* Returns a new JSON-RPC stream that uses 'stream' for input and output. The
81 * new jsonrpc object takes ownership of 'stream'. */
83 jsonrpc_open(struct stream
*stream
)
87 ovs_assert(stream
!= NULL
);
89 rpc
= xzalloc(sizeof *rpc
);
90 rpc
->name
= xstrdup(stream_get_name(stream
));
92 byteq_init(&rpc
->input
, rpc
->input_buffer
, sizeof rpc
->input_buffer
);
93 list_init(&rpc
->output
);
98 /* Destroys 'rpc', closing the stream on which it is based, and frees its
101 jsonrpc_close(struct jsonrpc
*rpc
)
104 jsonrpc_cleanup(rpc
);
110 /* Performs periodic maintenance on 'rpc', such as flushing output buffers. */
112 jsonrpc_run(struct jsonrpc
*rpc
)
118 stream_run(rpc
->stream
);
119 while (!list_is_empty(&rpc
->output
)) {
120 struct ofpbuf
*buf
= ofpbuf_from_list(rpc
->output
.next
);
123 retval
= stream_send(rpc
->stream
, buf
->data
, buf
->size
);
125 rpc
->backlog
-= retval
;
126 ofpbuf_pull(buf
, retval
);
128 list_remove(&buf
->list_node
);
132 if (retval
!= -EAGAIN
) {
133 VLOG_WARN_RL(&rl
, "%s: send error: %s",
134 rpc
->name
, ovs_strerror(-retval
));
135 jsonrpc_error(rpc
, -retval
);
142 /* Arranges for the poll loop to wake up when 'rpc' needs to perform
143 * maintenance activities. */
145 jsonrpc_wait(struct jsonrpc
*rpc
)
148 stream_run_wait(rpc
->stream
);
149 if (!list_is_empty(&rpc
->output
)) {
150 stream_send_wait(rpc
->stream
);
156 * Returns the current status of 'rpc'. The possible return values are:
159 * - EOF: end of file (remote end closed connection; not necessarily an error).
161 * When this functions nonzero, 'rpc' is effectively out of commission. 'rpc'
162 * will not receive any more messages and any further messages that one
163 * attempts to send with 'rpc' will be discarded. The caller can keep 'rpc'
164 * around as long as it wants, but it's not going to provide any more useful
168 jsonrpc_get_status(const struct jsonrpc
*rpc
)
173 /* Returns the number of bytes buffered by 'rpc' to be written to the
174 * underlying stream. Always returns 0 if 'rpc' has encountered an error or if
175 * the remote end closed the connection. */
177 jsonrpc_get_backlog(const struct jsonrpc
*rpc
)
179 return rpc
->status
? 0 : rpc
->backlog
;
182 /* Returns the number of bytes that have been received on 'rpc''s underlying
183 * stream. (The value wraps around if it exceeds UINT_MAX.) */
185 jsonrpc_get_received_bytes(const struct jsonrpc
*rpc
)
187 return rpc
->input
.head
;
190 /* Returns 'rpc''s name, that is, the name returned by stream_get_name() for
191 * the stream underlying 'rpc' when 'rpc' was created. */
193 jsonrpc_get_name(const struct jsonrpc
*rpc
)
199 jsonrpc_log_msg(const struct jsonrpc
*rpc
, const char *title
,
200 const struct jsonrpc_msg
*msg
)
202 if (VLOG_IS_DBG_ENABLED()) {
203 struct ds s
= DS_EMPTY_INITIALIZER
;
205 ds_put_format(&s
, ", method=\"%s\"", msg
->method
);
208 ds_put_cstr(&s
, ", params=");
209 json_to_ds(msg
->params
, 0, &s
);
212 ds_put_cstr(&s
, ", result=");
213 json_to_ds(msg
->result
, 0, &s
);
216 ds_put_cstr(&s
, ", error=");
217 json_to_ds(msg
->error
, 0, &s
);
220 ds_put_cstr(&s
, ", id=");
221 json_to_ds(msg
->id
, 0, &s
);
223 VLOG_DBG("%s: %s %s%s", rpc
->name
, title
,
224 jsonrpc_msg_type_to_string(msg
->type
), ds_cstr(&s
));
229 /* Schedules 'msg' to be sent on 'rpc' and returns 'rpc''s status (as with
230 * jsonrpc_get_status()).
232 * If 'msg' cannot be sent immediately, it is appended to a buffer. The caller
233 * is responsible for ensuring that the amount of buffered data is somehow
234 * limited. (jsonrpc_get_backlog() returns the amount of data currently
235 * buffered in 'rpc'.)
237 * Always takes ownership of 'msg', regardless of success. */
239 jsonrpc_send(struct jsonrpc
*rpc
, struct jsonrpc_msg
*msg
)
247 jsonrpc_msg_destroy(msg
);
251 jsonrpc_log_msg(rpc
, "send", msg
);
253 json
= jsonrpc_msg_to_json(msg
);
254 s
= json_to_string(json
, 0);
258 buf
= xmalloc(sizeof *buf
);
259 ofpbuf_use(buf
, s
, length
);
261 list_push_back(&rpc
->output
, &buf
->list_node
);
262 rpc
->backlog
+= length
;
264 if (rpc
->backlog
== length
) {
270 /* Attempts to receive a message from 'rpc'.
272 * If successful, stores the received message in '*msgp' and returns 0. The
273 * caller takes ownership of '*msgp' and must eventually destroy it with
274 * jsonrpc_msg_destroy().
276 * Otherwise, stores NULL in '*msgp' and returns one of the following:
278 * - EAGAIN: No message has been received.
280 * - EOF: The remote end closed the connection gracefully.
282 * - Otherwise an errno value that represents a JSON-RPC protocol violation
283 * or another error fatal to the connection. 'rpc' will not send or
284 * receive any more messages.
287 jsonrpc_recv(struct jsonrpc
*rpc
, struct jsonrpc_msg
**msgp
)
296 for (i
= 0; i
< 50; i
++) {
298 *msgp
= rpc
->received
;
299 rpc
->received
= NULL
;
301 } else if (byteq_is_empty(&rpc
->input
)) {
305 chunk
= byteq_headroom(&rpc
->input
);
306 retval
= stream_recv(rpc
->stream
, byteq_head(&rpc
->input
), chunk
);
308 if (retval
== -EAGAIN
) {
311 VLOG_WARN_RL(&rl
, "%s: receive error: %s",
312 rpc
->name
, ovs_strerror(-retval
));
313 jsonrpc_error(rpc
, -retval
);
316 } else if (retval
== 0) {
317 jsonrpc_error(rpc
, EOF
);
320 byteq_advance_head(&rpc
->input
, retval
);
325 rpc
->parser
= json_parser_create(0);
327 n
= byteq_tailroom(&rpc
->input
);
328 used
= json_parser_feed(rpc
->parser
,
329 (char *) byteq_tail(&rpc
->input
), n
);
330 byteq_advance_tail(&rpc
->input
, used
);
331 if (json_parser_is_done(rpc
->parser
)) {
332 jsonrpc_received(rpc
);
334 const struct byteq
*q
= &rpc
->input
;
335 if (q
->head
<= q
->size
) {
336 stream_report_content(q
->buffer
, q
->head
,
338 THIS_MODULE
, rpc
->name
);
349 /* Causes the poll loop to wake up when jsonrpc_recv() may return a value other
352 jsonrpc_recv_wait(struct jsonrpc
*rpc
)
354 if (rpc
->status
|| rpc
->received
|| !byteq_is_empty(&rpc
->input
)) {
355 poll_immediate_wake_at(rpc
->name
);
357 stream_recv_wait(rpc
->stream
);
361 /* Sends 'msg' on 'rpc' and waits for it to be successfully queued to the
362 * underlying stream. Returns 0 if 'msg' was sent successfully, otherwise a
363 * status value (see jsonrpc_get_status()).
365 * Always takes ownership of 'msg', regardless of success. */
367 jsonrpc_send_block(struct jsonrpc
*rpc
, struct jsonrpc_msg
*msg
)
373 error
= jsonrpc_send(rpc
, msg
);
380 if (list_is_empty(&rpc
->output
) || rpc
->status
) {
388 /* Waits for a message to be received on 'rpc'. Same semantics as
389 * jsonrpc_recv() except that EAGAIN will never be returned. */
391 jsonrpc_recv_block(struct jsonrpc
*rpc
, struct jsonrpc_msg
**msgp
)
394 int error
= jsonrpc_recv(rpc
, msgp
);
395 if (error
!= EAGAIN
) {
402 jsonrpc_recv_wait(rpc
);
407 /* Sends 'request' to 'rpc' then waits for a reply. The return value is 0 if
408 * successful, in which case '*replyp' is set to the reply, which the caller
409 * must eventually free with jsonrpc_msg_destroy(). Otherwise returns a status
410 * value (see jsonrpc_get_status()).
412 * Discards any message received on 'rpc' that is not a reply to 'request'
413 * (based on message id).
415 * Always takes ownership of 'request', regardless of success. */
417 jsonrpc_transact_block(struct jsonrpc
*rpc
, struct jsonrpc_msg
*request
,
418 struct jsonrpc_msg
**replyp
)
420 struct jsonrpc_msg
*reply
= NULL
;
424 id
= json_clone(request
->id
);
425 error
= jsonrpc_send_block(rpc
, request
);
428 error
= jsonrpc_recv_block(rpc
, &reply
);
432 if ((reply
->type
== JSONRPC_REPLY
|| reply
->type
== JSONRPC_ERROR
)
433 && json_equal(id
, reply
->id
)) {
436 jsonrpc_msg_destroy(reply
);
439 *replyp
= error
? NULL
: reply
;
445 jsonrpc_received(struct jsonrpc
*rpc
)
447 struct jsonrpc_msg
*msg
;
451 json
= json_parser_finish(rpc
->parser
);
453 if (json
->type
== JSON_STRING
) {
454 VLOG_WARN_RL(&rl
, "%s: error parsing stream: %s",
455 rpc
->name
, json_string(json
));
456 jsonrpc_error(rpc
, EPROTO
);
461 error
= jsonrpc_msg_from_json(json
, &msg
);
463 VLOG_WARN_RL(&rl
, "%s: received bad JSON-RPC message: %s",
466 jsonrpc_error(rpc
, EPROTO
);
470 jsonrpc_log_msg(rpc
, "received", msg
);
475 jsonrpc_error(struct jsonrpc
*rpc
, int error
)
480 jsonrpc_cleanup(rpc
);
485 jsonrpc_cleanup(struct jsonrpc
*rpc
)
487 stream_close(rpc
->stream
);
490 json_parser_abort(rpc
->parser
);
493 jsonrpc_msg_destroy(rpc
->received
);
494 rpc
->received
= NULL
;
496 ofpbuf_list_delete(&rpc
->output
);
500 static struct jsonrpc_msg
*
501 jsonrpc_create(enum jsonrpc_msg_type type
, const char *method
,
502 struct json
*params
, struct json
*result
, struct json
*error
,
505 struct jsonrpc_msg
*msg
= xmalloc(sizeof *msg
);
507 msg
->method
= method
? xstrdup(method
) : NULL
;
508 msg
->params
= params
;
509 msg
->result
= result
;
516 jsonrpc_create_id(void)
518 static atomic_uint next_id
= ATOMIC_VAR_INIT(0);
521 atomic_add(&next_id
, 1, &id
);
522 return json_integer_create(id
);
526 jsonrpc_create_request(const char *method
, struct json
*params
,
529 struct json
*id
= jsonrpc_create_id();
531 *idp
= json_clone(id
);
533 return jsonrpc_create(JSONRPC_REQUEST
, method
, params
, NULL
, NULL
, id
);
537 jsonrpc_create_notify(const char *method
, struct json
*params
)
539 return jsonrpc_create(JSONRPC_NOTIFY
, method
, params
, NULL
, NULL
, NULL
);
543 jsonrpc_create_reply(struct json
*result
, const struct json
*id
)
545 return jsonrpc_create(JSONRPC_REPLY
, NULL
, NULL
, result
, NULL
,
550 jsonrpc_create_error(struct json
*error
, const struct json
*id
)
552 return jsonrpc_create(JSONRPC_REPLY
, NULL
, NULL
, NULL
, error
,
557 jsonrpc_msg_type_to_string(enum jsonrpc_msg_type type
)
560 case JSONRPC_REQUEST
:
564 return "notification";
576 jsonrpc_msg_is_valid(const struct jsonrpc_msg
*m
)
578 const char *type_name
;
579 unsigned int pattern
;
581 if (m
->params
&& m
->params
->type
!= JSON_ARRAY
) {
582 return xstrdup("\"params\" must be JSON array");
586 case JSONRPC_REQUEST
:
603 return xasprintf("invalid JSON-RPC message type %d", m
->type
);
606 type_name
= jsonrpc_msg_type_to_string(m
->type
);
607 if ((m
->method
!= NULL
) != ((pattern
& 0x10000) != 0)) {
608 return xasprintf("%s must%s have \"method\"",
609 type_name
, (pattern
& 0x10000) ? "" : " not");
612 if ((m
->params
!= NULL
) != ((pattern
& 0x1000) != 0)) {
613 return xasprintf("%s must%s have \"params\"",
614 type_name
, (pattern
& 0x1000) ? "" : " not");
617 if ((m
->result
!= NULL
) != ((pattern
& 0x100) != 0)) {
618 return xasprintf("%s must%s have \"result\"",
619 type_name
, (pattern
& 0x100) ? "" : " not");
622 if ((m
->error
!= NULL
) != ((pattern
& 0x10) != 0)) {
623 return xasprintf("%s must%s have \"error\"",
624 type_name
, (pattern
& 0x10) ? "" : " not");
627 if ((m
->id
!= NULL
) != ((pattern
& 0x1) != 0)) {
628 return xasprintf("%s must%s have \"id\"",
629 type_name
, (pattern
& 0x1) ? "" : " not");
636 jsonrpc_msg_destroy(struct jsonrpc_msg
*m
)
640 json_destroy(m
->params
);
641 json_destroy(m
->result
);
642 json_destroy(m
->error
);
649 null_from_json_null(struct json
*json
)
651 if (json
&& json
->type
== JSON_NULL
) {
659 jsonrpc_msg_from_json(struct json
*json
, struct jsonrpc_msg
**msgp
)
661 struct json
*method
= NULL
;
662 struct jsonrpc_msg
*msg
= NULL
;
663 struct shash
*object
;
666 if (json
->type
!= JSON_OBJECT
) {
667 error
= xstrdup("message is not a JSON object");
670 object
= json_object(json
);
672 method
= shash_find_and_delete(object
, "method");
673 if (method
&& method
->type
!= JSON_STRING
) {
674 error
= xstrdup("method is not a JSON string");
678 msg
= xzalloc(sizeof *msg
);
679 msg
->method
= method
? xstrdup(method
->u
.string
) : NULL
;
680 msg
->params
= null_from_json_null(shash_find_and_delete(object
, "params"));
681 msg
->result
= null_from_json_null(shash_find_and_delete(object
, "result"));
682 msg
->error
= null_from_json_null(shash_find_and_delete(object
, "error"));
683 msg
->id
= null_from_json_null(shash_find_and_delete(object
, "id"));
684 msg
->type
= (msg
->result
? JSONRPC_REPLY
685 : msg
->error
? JSONRPC_ERROR
686 : msg
->id
? JSONRPC_REQUEST
688 if (!shash_is_empty(object
)) {
689 error
= xasprintf("message has unexpected member \"%s\"",
690 shash_first(object
)->name
);
693 error
= jsonrpc_msg_is_valid(msg
);
699 json_destroy(method
);
702 jsonrpc_msg_destroy(msg
);
710 jsonrpc_msg_to_json(struct jsonrpc_msg
*m
)
712 struct json
*json
= json_object_create();
715 json_object_put(json
, "method", json_string_create_nocopy(m
->method
));
719 json_object_put(json
, "params", m
->params
);
723 json_object_put(json
, "result", m
->result
);
724 } else if (m
->type
== JSONRPC_ERROR
) {
725 json_object_put(json
, "result", json_null_create());
729 json_object_put(json
, "error", m
->error
);
730 } else if (m
->type
== JSONRPC_REPLY
) {
731 json_object_put(json
, "error", json_null_create());
735 json_object_put(json
, "id", m
->id
);
736 } else if (m
->type
== JSONRPC_NOTIFY
) {
737 json_object_put(json
, "id", json_null_create());
745 /* A JSON-RPC session with reconnection. */
747 struct jsonrpc_session
{
748 struct reconnect
*reconnect
;
750 struct stream
*stream
;
751 struct pstream
*pstream
;
757 /* Creates and returns a jsonrpc_session to 'name', which should be a string
758 * acceptable to stream_open() or pstream_open().
760 * If 'name' is an active connection method, e.g. "tcp:127.1.2.3", the new
761 * jsonrpc_session connects to 'name'. If 'retry' is true, then the new
762 * session connects and reconnects to 'name', with backoff. If 'retry' is
763 * false, the new session will only try to connect once and after a connection
764 * failure or a disconnection jsonrpc_session_is_alive() will return false for
767 * If 'name' is a passive connection method, e.g. "ptcp:", the new
768 * jsonrpc_session listens for connections to 'name'. It maintains at most one
769 * connection at any given time. Any new connection causes the previous one
770 * (if any) to be dropped. */
771 struct jsonrpc_session
*
772 jsonrpc_session_open(const char *name
, bool retry
)
774 struct jsonrpc_session
*s
;
776 s
= xmalloc(sizeof *s
);
777 s
->reconnect
= reconnect_create(time_msec());
778 reconnect_set_name(s
->reconnect
, name
);
779 reconnect_enable(s
->reconnect
, time_msec());
787 if (!pstream_verify_name(name
)) {
788 reconnect_set_passive(s
->reconnect
, true, time_msec());
790 reconnect_set_max_tries(s
->reconnect
, 1);
791 reconnect_set_backoff(s
->reconnect
, INT_MAX
, INT_MAX
);
794 if (!stream_or_pstream_needs_probes(name
)) {
795 reconnect_set_probe_interval(s
->reconnect
, 0);
801 /* Creates and returns a jsonrpc_session that is initially connected to
802 * 'jsonrpc'. If the connection is dropped, it will not be reconnected.
804 * On the assumption that such connections are likely to be short-lived
805 * (e.g. from ovs-vsctl), informational logging for them is suppressed. */
806 struct jsonrpc_session
*
807 jsonrpc_session_open_unreliably(struct jsonrpc
*jsonrpc
, uint8_t dscp
)
809 struct jsonrpc_session
*s
;
811 s
= xmalloc(sizeof *s
);
812 s
->reconnect
= reconnect_create(time_msec());
813 reconnect_set_quiet(s
->reconnect
, true);
814 reconnect_set_name(s
->reconnect
, jsonrpc_get_name(jsonrpc
));
815 reconnect_set_max_tries(s
->reconnect
, 0);
816 reconnect_connected(s
->reconnect
, time_msec());
827 jsonrpc_session_close(struct jsonrpc_session
*s
)
830 jsonrpc_close(s
->rpc
);
831 reconnect_destroy(s
->reconnect
);
832 stream_close(s
->stream
);
833 pstream_close(s
->pstream
);
839 jsonrpc_session_disconnect(struct jsonrpc_session
*s
)
842 jsonrpc_error(s
->rpc
, EOF
);
843 jsonrpc_close(s
->rpc
);
846 } else if (s
->stream
) {
847 stream_close(s
->stream
);
854 jsonrpc_session_connect(struct jsonrpc_session
*s
)
856 const char *name
= reconnect_get_name(s
->reconnect
);
859 jsonrpc_session_disconnect(s
);
860 if (!reconnect_is_passive(s
->reconnect
)) {
861 error
= jsonrpc_stream_open(name
, &s
->stream
, s
->dscp
);
863 reconnect_connecting(s
->reconnect
, time_msec());
865 s
->last_error
= error
;
868 error
= s
->pstream
? 0 : jsonrpc_pstream_open(name
, &s
->pstream
,
871 reconnect_listening(s
->reconnect
, time_msec());
876 reconnect_connect_failed(s
->reconnect
, time_msec(), error
);
882 jsonrpc_session_run(struct jsonrpc_session
*s
)
885 struct stream
*stream
;
888 error
= pstream_accept(s
->pstream
, &stream
);
890 if (s
->rpc
|| s
->stream
) {
892 "%s: new connection replacing active connection",
893 reconnect_get_name(s
->reconnect
));
894 jsonrpc_session_disconnect(s
);
896 reconnect_connected(s
->reconnect
, time_msec());
897 s
->rpc
= jsonrpc_open(stream
);
898 } else if (error
!= EAGAIN
) {
899 reconnect_listen_error(s
->reconnect
, time_msec(), error
);
900 pstream_close(s
->pstream
);
909 backlog
= jsonrpc_get_backlog(s
->rpc
);
911 if (jsonrpc_get_backlog(s
->rpc
) < backlog
) {
912 /* Data previously caught in a queue was successfully sent (or
913 * there's an error, which we'll catch below.)
915 * We don't count data that is successfully sent immediately as
916 * activity, because there's a lot of queuing downstream from us,
917 * which means that we can push a lot of data into a connection
918 * that has stalled and won't ever recover.
920 reconnect_activity(s
->reconnect
, time_msec());
923 error
= jsonrpc_get_status(s
->rpc
);
925 reconnect_disconnected(s
->reconnect
, time_msec(), error
);
926 jsonrpc_session_disconnect(s
);
927 s
->last_error
= error
;
929 } else if (s
->stream
) {
932 stream_run(s
->stream
);
933 error
= stream_connect(s
->stream
);
935 reconnect_connected(s
->reconnect
, time_msec());
936 s
->rpc
= jsonrpc_open(s
->stream
);
938 } else if (error
!= EAGAIN
) {
939 reconnect_connect_failed(s
->reconnect
, time_msec(), error
);
940 stream_close(s
->stream
);
945 switch (reconnect_run(s
->reconnect
, time_msec())) {
946 case RECONNECT_CONNECT
:
947 jsonrpc_session_connect(s
);
950 case RECONNECT_DISCONNECT
:
951 reconnect_disconnected(s
->reconnect
, time_msec(), 0);
952 jsonrpc_session_disconnect(s
);
955 case RECONNECT_PROBE
:
958 struct jsonrpc_msg
*request
;
960 params
= json_array_create_empty();
961 request
= jsonrpc_create_request("echo", params
, NULL
);
962 json_destroy(request
->id
);
963 request
->id
= json_string_create("echo");
964 jsonrpc_send(s
->rpc
, request
);
971 jsonrpc_session_wait(struct jsonrpc_session
*s
)
974 jsonrpc_wait(s
->rpc
);
975 } else if (s
->stream
) {
976 stream_run_wait(s
->stream
);
977 stream_connect_wait(s
->stream
);
980 pstream_wait(s
->pstream
);
982 reconnect_wait(s
->reconnect
, time_msec());
986 jsonrpc_session_get_backlog(const struct jsonrpc_session
*s
)
988 return s
->rpc
? jsonrpc_get_backlog(s
->rpc
) : 0;
991 /* Always returns a pointer to a valid C string, assuming 's' was initialized
994 jsonrpc_session_get_name(const struct jsonrpc_session
*s
)
996 return reconnect_get_name(s
->reconnect
);
999 /* Always takes ownership of 'msg', regardless of success. */
1001 jsonrpc_session_send(struct jsonrpc_session
*s
, struct jsonrpc_msg
*msg
)
1004 return jsonrpc_send(s
->rpc
, msg
);
1006 jsonrpc_msg_destroy(msg
);
1011 struct jsonrpc_msg
*
1012 jsonrpc_session_recv(struct jsonrpc_session
*s
)
1015 unsigned int received_bytes
;
1016 struct jsonrpc_msg
*msg
;
1018 received_bytes
= jsonrpc_get_received_bytes(s
->rpc
);
1019 jsonrpc_recv(s
->rpc
, &msg
);
1020 if (received_bytes
!= jsonrpc_get_received_bytes(s
->rpc
)) {
1021 /* Data was successfully received.
1023 * Previously we only counted receiving a full message as activity,
1024 * but with large messages or a slow connection that policy could
1025 * time out the session mid-message. */
1026 reconnect_activity(s
->reconnect
, time_msec());
1030 if (msg
->type
== JSONRPC_REQUEST
&& !strcmp(msg
->method
, "echo")) {
1031 /* Echo request. Send reply. */
1032 struct jsonrpc_msg
*reply
;
1034 reply
= jsonrpc_create_reply(json_clone(msg
->params
), msg
->id
);
1035 jsonrpc_session_send(s
, reply
);
1036 } else if (msg
->type
== JSONRPC_REPLY
1037 && msg
->id
&& msg
->id
->type
== JSON_STRING
1038 && !strcmp(msg
->id
->u
.string
, "echo")) {
1039 /* It's a reply to our echo request. Suppress it. */
1043 jsonrpc_msg_destroy(msg
);
1050 jsonrpc_session_recv_wait(struct jsonrpc_session
*s
)
1053 jsonrpc_recv_wait(s
->rpc
);
1058 jsonrpc_session_is_alive(const struct jsonrpc_session
*s
)
1060 return s
->rpc
|| s
->stream
|| reconnect_get_max_tries(s
->reconnect
);
1064 jsonrpc_session_is_connected(const struct jsonrpc_session
*s
)
1066 return s
->rpc
!= NULL
;
1070 jsonrpc_session_get_seqno(const struct jsonrpc_session
*s
)
1076 jsonrpc_session_get_status(const struct jsonrpc_session
*s
)
1078 return s
&& s
->rpc
? jsonrpc_get_status(s
->rpc
) : 0;
1082 jsonrpc_session_get_last_error(const struct jsonrpc_session
*s
)
1084 return s
->last_error
;
1088 jsonrpc_session_get_reconnect_stats(const struct jsonrpc_session
*s
,
1089 struct reconnect_stats
*stats
)
1091 reconnect_get_stats(s
->reconnect
, time_msec(), stats
);
1095 jsonrpc_session_force_reconnect(struct jsonrpc_session
*s
)
1097 reconnect_force_reconnect(s
->reconnect
, time_msec());
1101 jsonrpc_session_set_max_backoff(struct jsonrpc_session
*s
, int max_backoff
)
1103 reconnect_set_backoff(s
->reconnect
, 0, max_backoff
);
1107 jsonrpc_session_set_probe_interval(struct jsonrpc_session
*s
,
1110 reconnect_set_probe_interval(s
->reconnect
, probe_interval
);
1114 jsonrpc_session_set_dscp(struct jsonrpc_session
*s
,
1117 if (s
->dscp
!= dscp
) {
1121 error
= pstream_set_dscp(s
->pstream
, dscp
);
1123 VLOG_ERR("%s: failed set_dscp %s",
1124 reconnect_get_name(s
->reconnect
),
1125 ovs_strerror(error
));
1128 * XXX race window between setting dscp to listening socket
1129 * and accepting socket. accepted socket may have old dscp value.
1130 * Ignore this race window for now.
1134 jsonrpc_session_force_reconnect(s
);