2 * Copyright (c) 2009, 2010, 2011, 2012 Nicira Networks.
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.
25 #include "dynamic-string.h"
26 #include "fatal-signal.h"
30 #include "poll-loop.h"
31 #include "reconnect.h"
36 VLOG_DEFINE_THIS_MODULE(jsonrpc);
39 struct stream *stream;
45 struct json_parser *parser;
46 struct jsonrpc_msg *received;
49 struct list output; /* Contains "struct ofpbuf"s. */
53 /* Rate limit for error messages. */
54 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 5);
56 static void jsonrpc_received(struct jsonrpc *);
57 static void jsonrpc_cleanup(struct jsonrpc *);
58 static void jsonrpc_error(struct jsonrpc *, int error);
60 /* This is just the same as stream_open() except that it uses the default
61 * JSONRPC ports if none is specified. */
63 jsonrpc_stream_open(const char *name, struct stream **streamp)
65 return stream_open_with_default_ports(name, JSONRPC_TCP_PORT,
66 JSONRPC_SSL_PORT, streamp);
69 /* This is just the same as pstream_open() except that it uses the default
70 * JSONRPC ports if none is specified. */
72 jsonrpc_pstream_open(const char *name, struct pstream **pstreamp)
74 return pstream_open_with_default_ports(name, JSONRPC_TCP_PORT,
75 JSONRPC_SSL_PORT, pstreamp);
78 /* Returns a new JSON-RPC stream that uses 'stream' for input and output. The
79 * new jsonrpc object takes ownership of 'stream'. */
81 jsonrpc_open(struct stream *stream)
85 assert(stream != NULL);
87 rpc = xzalloc(sizeof *rpc);
88 rpc->name = xstrdup(stream_get_name(stream));
90 byteq_init(&rpc->input);
91 list_init(&rpc->output);
96 /* Destroys 'rpc', closing the stream on which it is based, and frees its
99 jsonrpc_close(struct jsonrpc *rpc)
102 jsonrpc_cleanup(rpc);
108 /* Performs periodic maintenance on 'rpc', such as flushing output buffers. */
110 jsonrpc_run(struct jsonrpc *rpc)
116 stream_run(rpc->stream);
117 while (!list_is_empty(&rpc->output)) {
118 struct ofpbuf *buf = ofpbuf_from_list(rpc->output.next);
121 retval = stream_send(rpc->stream, buf->data, buf->size);
123 rpc->backlog -= retval;
124 ofpbuf_pull(buf, retval);
126 list_remove(&buf->list_node);
130 if (retval != -EAGAIN) {
131 VLOG_WARN_RL(&rl, "%s: send error: %s",
132 rpc->name, strerror(-retval));
133 jsonrpc_error(rpc, -retval);
140 /* Arranges for the poll loop to wake up when 'rpc' needs to perform
141 * maintenance activities. */
143 jsonrpc_wait(struct jsonrpc *rpc)
146 stream_run_wait(rpc->stream);
147 if (!list_is_empty(&rpc->output)) {
148 stream_send_wait(rpc->stream);
154 * Returns the current status of 'rpc'. The possible return values are:
157 * - EOF: end of file (remote end closed connection; not necessarily an error).
159 * When this functions nonzero, 'rpc' is effectively out of commission. 'rpc'
160 * will not receive any more messages and any further messages that one
161 * attempts to send with 'rpc' will be discarded. The caller can keep 'rpc'
162 * around as long as it wants, but it's not going to provide any more useful
166 jsonrpc_get_status(const struct jsonrpc *rpc)
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. */
175 jsonrpc_get_backlog(const struct jsonrpc *rpc)
177 return rpc->status ? 0 : rpc->backlog;
180 /* Returns 'rpc''s name, that is, the name returned by stream_get_name() for
181 * the stream underlying 'rpc' when 'rpc' was created. */
183 jsonrpc_get_name(const struct jsonrpc *rpc)
189 jsonrpc_log_msg(const struct jsonrpc *rpc, const char *title,
190 const struct jsonrpc_msg *msg)
192 if (VLOG_IS_DBG_ENABLED()) {
193 struct ds s = DS_EMPTY_INITIALIZER;
195 ds_put_format(&s, ", method=\"%s\"", msg->method);
198 ds_put_cstr(&s, ", params=");
199 json_to_ds(msg->params, 0, &s);
202 ds_put_cstr(&s, ", result=");
203 json_to_ds(msg->result, 0, &s);
206 ds_put_cstr(&s, ", error=");
207 json_to_ds(msg->error, 0, &s);
210 ds_put_cstr(&s, ", id=");
211 json_to_ds(msg->id, 0, &s);
213 VLOG_DBG("%s: %s %s%s", rpc->name, title,
214 jsonrpc_msg_type_to_string(msg->type), ds_cstr(&s));
219 /* Schedules 'msg' to be sent on 'rpc' and returns 'rpc''s status (as with
220 * jsonrpc_get_status()).
222 * If 'msg' cannot be sent immediately, it is appended to a buffer. The caller
223 * is responsible for ensuring that the amount of buffered data is somehow
224 * limited. (jsonrpc_get_backlog() returns the amount of data currently
225 * buffered in 'rpc'.)
227 * Always takes ownership of 'msg', regardless of success. */
229 jsonrpc_send(struct jsonrpc *rpc, struct jsonrpc_msg *msg)
237 jsonrpc_msg_destroy(msg);
241 jsonrpc_log_msg(rpc, "send", msg);
243 json = jsonrpc_msg_to_json(msg);
244 s = json_to_string(json, 0);
248 buf = xmalloc(sizeof *buf);
249 ofpbuf_use(buf, s, length);
251 list_push_back(&rpc->output, &buf->list_node);
252 rpc->backlog += length;
254 if (rpc->backlog == length) {
260 /* Attempts to receive a message from 'rpc'.
262 * If successful, stores the received message in '*msgp' and returns 0. The
263 * caller takes ownership of '*msgp' and must eventually destroy it with
264 * jsonrpc_msg_destroy().
266 * Otherwise, stores NULL in '*msgp' and returns one of the following:
268 * - EAGAIN: No message has been received.
270 * - EOF: The remote end closed the connection gracefully.
272 * - Otherwise an errno value that represents a JSON-RPC protocol violation
273 * or another error fatal to the connection. 'rpc' will not send or
274 * receive any more messages.
277 jsonrpc_recv(struct jsonrpc *rpc, struct jsonrpc_msg **msgp)
284 while (!rpc->received) {
285 if (byteq_is_empty(&rpc->input)) {
289 chunk = byteq_headroom(&rpc->input);
290 retval = stream_recv(rpc->stream, byteq_head(&rpc->input), chunk);
292 if (retval == -EAGAIN) {
295 VLOG_WARN_RL(&rl, "%s: receive error: %s",
296 rpc->name, strerror(-retval));
297 jsonrpc_error(rpc, -retval);
300 } else if (retval == 0) {
301 jsonrpc_error(rpc, EOF);
304 byteq_advance_head(&rpc->input, retval);
309 rpc->parser = json_parser_create(0);
311 n = byteq_tailroom(&rpc->input);
312 used = json_parser_feed(rpc->parser,
313 (char *) byteq_tail(&rpc->input), n);
314 byteq_advance_tail(&rpc->input, used);
315 if (json_parser_is_done(rpc->parser)) {
316 jsonrpc_received(rpc);
318 const struct byteq *q = &rpc->input;
319 if (q->head <= BYTEQ_SIZE) {
320 stream_report_content(q->buffer, q->head,
322 THIS_MODULE, rpc->name);
330 *msgp = rpc->received;
331 rpc->received = NULL;
335 /* Causes the poll loop to wake up when jsonrpc_recv() may return a value other
338 jsonrpc_recv_wait(struct jsonrpc *rpc)
340 if (rpc->status || rpc->received || !byteq_is_empty(&rpc->input)) {
341 (poll_immediate_wake)(rpc->name);
343 stream_recv_wait(rpc->stream);
347 /* Sends 'msg' on 'rpc' and waits for it to be successfully queued to the
348 * underlying stream. Returns 0 if 'msg' was sent successfully, otherwise a
349 * status value (see jsonrpc_get_status()).
351 * Always takes ownership of 'msg', regardless of success. */
353 jsonrpc_send_block(struct jsonrpc *rpc, struct jsonrpc_msg *msg)
359 error = jsonrpc_send(rpc, msg);
366 if (list_is_empty(&rpc->output) || rpc->status) {
374 /* Waits for a message to be received on 'rpc'. Same semantics as
375 * jsonrpc_recv() except that EAGAIN will never be returned. */
377 jsonrpc_recv_block(struct jsonrpc *rpc, struct jsonrpc_msg **msgp)
380 int error = jsonrpc_recv(rpc, msgp);
381 if (error != EAGAIN) {
388 jsonrpc_recv_wait(rpc);
393 /* Sends 'request' to 'rpc' then waits for a reply. The return value is 0 if
394 * successful, in which case '*replyp' is set to the reply, which the caller
395 * must eventually free with jsonrpc_msg_destroy(). Otherwise returns a status
396 * value (see jsonrpc_get_status()).
398 * Discards any message received on 'rpc' that is not a reply to 'request'
399 * (based on message id).
401 * Always takes ownership of 'request', regardless of success. */
403 jsonrpc_transact_block(struct jsonrpc *rpc, struct jsonrpc_msg *request,
404 struct jsonrpc_msg **replyp)
406 struct jsonrpc_msg *reply = NULL;
410 id = json_clone(request->id);
411 error = jsonrpc_send_block(rpc, request);
414 error = jsonrpc_recv_block(rpc, &reply);
416 || (reply->type == JSONRPC_REPLY
417 && json_equal(id, reply->id))) {
420 jsonrpc_msg_destroy(reply);
423 *replyp = error ? NULL : reply;
429 jsonrpc_received(struct jsonrpc *rpc)
431 struct jsonrpc_msg *msg;
435 json = json_parser_finish(rpc->parser);
437 if (json->type == JSON_STRING) {
438 VLOG_WARN_RL(&rl, "%s: error parsing stream: %s",
439 rpc->name, json_string(json));
440 jsonrpc_error(rpc, EPROTO);
445 error = jsonrpc_msg_from_json(json, &msg);
447 VLOG_WARN_RL(&rl, "%s: received bad JSON-RPC message: %s",
450 jsonrpc_error(rpc, EPROTO);
454 jsonrpc_log_msg(rpc, "received", msg);
459 jsonrpc_error(struct jsonrpc *rpc, int error)
464 jsonrpc_cleanup(rpc);
469 jsonrpc_cleanup(struct jsonrpc *rpc)
471 stream_close(rpc->stream);
474 json_parser_abort(rpc->parser);
477 jsonrpc_msg_destroy(rpc->received);
478 rpc->received = NULL;
480 ofpbuf_list_delete(&rpc->output);
484 static struct jsonrpc_msg *
485 jsonrpc_create(enum jsonrpc_msg_type type, const char *method,
486 struct json *params, struct json *result, struct json *error,
489 struct jsonrpc_msg *msg = xmalloc(sizeof *msg);
491 msg->method = method ? xstrdup(method) : NULL;
492 msg->params = params;
493 msg->result = result;
500 jsonrpc_create_id(void)
502 static unsigned int id;
503 return json_integer_create(id++);
507 jsonrpc_create_request(const char *method, struct json *params,
510 struct json *id = jsonrpc_create_id();
512 *idp = json_clone(id);
514 return jsonrpc_create(JSONRPC_REQUEST, method, params, NULL, NULL, id);
518 jsonrpc_create_notify(const char *method, struct json *params)
520 return jsonrpc_create(JSONRPC_NOTIFY, method, params, NULL, NULL, NULL);
524 jsonrpc_create_reply(struct json *result, const struct json *id)
526 return jsonrpc_create(JSONRPC_REPLY, NULL, NULL, result, NULL,
531 jsonrpc_create_error(struct json *error, const struct json *id)
533 return jsonrpc_create(JSONRPC_REPLY, NULL, NULL, NULL, error,
538 jsonrpc_msg_type_to_string(enum jsonrpc_msg_type type)
541 case JSONRPC_REQUEST:
545 return "notification";
557 jsonrpc_msg_is_valid(const struct jsonrpc_msg *m)
559 const char *type_name;
560 unsigned int pattern;
562 if (m->params && m->params->type != JSON_ARRAY) {
563 return xstrdup("\"params\" must be JSON array");
567 case JSONRPC_REQUEST:
584 return xasprintf("invalid JSON-RPC message type %d", m->type);
587 type_name = jsonrpc_msg_type_to_string(m->type);
588 if ((m->method != NULL) != ((pattern & 0x10000) != 0)) {
589 return xasprintf("%s must%s have \"method\"",
590 type_name, (pattern & 0x10000) ? "" : " not");
593 if ((m->params != NULL) != ((pattern & 0x1000) != 0)) {
594 return xasprintf("%s must%s have \"params\"",
595 type_name, (pattern & 0x1000) ? "" : " not");
598 if ((m->result != NULL) != ((pattern & 0x100) != 0)) {
599 return xasprintf("%s must%s have \"result\"",
600 type_name, (pattern & 0x100) ? "" : " not");
603 if ((m->error != NULL) != ((pattern & 0x10) != 0)) {
604 return xasprintf("%s must%s have \"error\"",
605 type_name, (pattern & 0x10) ? "" : " not");
608 if ((m->id != NULL) != ((pattern & 0x1) != 0)) {
609 return xasprintf("%s must%s have \"id\"",
610 type_name, (pattern & 0x1) ? "" : " not");
617 jsonrpc_msg_destroy(struct jsonrpc_msg *m)
621 json_destroy(m->params);
622 json_destroy(m->result);
623 json_destroy(m->error);
630 null_from_json_null(struct json *json)
632 if (json && json->type == JSON_NULL) {
640 jsonrpc_msg_from_json(struct json *json, struct jsonrpc_msg **msgp)
642 struct json *method = NULL;
643 struct jsonrpc_msg *msg = NULL;
644 struct shash *object;
647 if (json->type != JSON_OBJECT) {
648 error = xstrdup("message is not a JSON object");
651 object = json_object(json);
653 method = shash_find_and_delete(object, "method");
654 if (method && method->type != JSON_STRING) {
655 error = xstrdup("method is not a JSON string");
659 msg = xzalloc(sizeof *msg);
660 msg->method = method ? xstrdup(method->u.string) : NULL;
661 msg->params = null_from_json_null(shash_find_and_delete(object, "params"));
662 msg->result = null_from_json_null(shash_find_and_delete(object, "result"));
663 msg->error = null_from_json_null(shash_find_and_delete(object, "error"));
664 msg->id = null_from_json_null(shash_find_and_delete(object, "id"));
665 msg->type = (msg->result ? JSONRPC_REPLY
666 : msg->error ? JSONRPC_ERROR
667 : msg->id ? JSONRPC_REQUEST
669 if (!shash_is_empty(object)) {
670 error = xasprintf("message has unexpected member \"%s\"",
671 shash_first(object)->name);
674 error = jsonrpc_msg_is_valid(msg);
680 json_destroy(method);
683 jsonrpc_msg_destroy(msg);
691 jsonrpc_msg_to_json(struct jsonrpc_msg *m)
693 struct json *json = json_object_create();
696 json_object_put(json, "method", json_string_create_nocopy(m->method));
700 json_object_put(json, "params", m->params);
704 json_object_put(json, "result", m->result);
705 } else if (m->type == JSONRPC_ERROR) {
706 json_object_put(json, "result", json_null_create());
710 json_object_put(json, "error", m->error);
711 } else if (m->type == JSONRPC_REPLY) {
712 json_object_put(json, "error", json_null_create());
716 json_object_put(json, "id", m->id);
717 } else if (m->type == JSONRPC_NOTIFY) {
718 json_object_put(json, "id", json_null_create());
726 /* A JSON-RPC session with reconnection. */
728 struct jsonrpc_session {
729 struct reconnect *reconnect;
731 struct stream *stream;
732 struct pstream *pstream;
736 /* Creates and returns a jsonrpc_session to 'name', which should be a string
737 * acceptable to stream_open() or pstream_open().
739 * If 'name' is an active connection method, e.g. "tcp:127.1.2.3", the new
740 * jsonrpc_session connects and reconnects, with back-off, to 'name'.
742 * If 'name' is a passive connection method, e.g. "ptcp:", the new
743 * jsonrpc_session listens for connections to 'name'. It maintains at most one
744 * connection at any given time. Any new connection causes the previous one
745 * (if any) to be dropped. */
746 struct jsonrpc_session *
747 jsonrpc_session_open(const char *name)
749 struct jsonrpc_session *s;
751 s = xmalloc(sizeof *s);
752 s->reconnect = reconnect_create(time_msec());
753 reconnect_set_name(s->reconnect, name);
754 reconnect_enable(s->reconnect, time_msec());
760 if (!pstream_verify_name(name)) {
761 reconnect_set_passive(s->reconnect, true, time_msec());
767 /* Creates and returns a jsonrpc_session that is initially connected to
768 * 'jsonrpc'. If the connection is dropped, it will not be reconnected.
770 * On the assumption that such connections are likely to be short-lived
771 * (e.g. from ovs-vsctl), informational logging for them is suppressed. */
772 struct jsonrpc_session *
773 jsonrpc_session_open_unreliably(struct jsonrpc *jsonrpc)
775 struct jsonrpc_session *s;
777 s = xmalloc(sizeof *s);
778 s->reconnect = reconnect_create(time_msec());
779 reconnect_set_quiet(s->reconnect, true);
780 reconnect_set_name(s->reconnect, jsonrpc_get_name(jsonrpc));
781 reconnect_set_max_tries(s->reconnect, 0);
782 reconnect_connected(s->reconnect, time_msec());
792 jsonrpc_session_close(struct jsonrpc_session *s)
795 jsonrpc_close(s->rpc);
796 reconnect_destroy(s->reconnect);
797 stream_close(s->stream);
798 pstream_close(s->pstream);
804 jsonrpc_session_disconnect(struct jsonrpc_session *s)
807 jsonrpc_error(s->rpc, EOF);
808 jsonrpc_close(s->rpc);
811 } else if (s->stream) {
812 stream_close(s->stream);
819 jsonrpc_session_connect(struct jsonrpc_session *s)
821 const char *name = reconnect_get_name(s->reconnect);
824 jsonrpc_session_disconnect(s);
825 if (!reconnect_is_passive(s->reconnect)) {
826 error = jsonrpc_stream_open(name, &s->stream);
828 reconnect_connecting(s->reconnect, time_msec());
831 error = s->pstream ? 0 : jsonrpc_pstream_open(name, &s->pstream);
833 reconnect_listening(s->reconnect, time_msec());
838 reconnect_connect_failed(s->reconnect, time_msec(), error);
844 jsonrpc_session_run(struct jsonrpc_session *s)
847 struct stream *stream;
850 error = pstream_accept(s->pstream, &stream);
852 if (s->rpc || s->stream) {
854 "%s: new connection replacing active connection",
855 reconnect_get_name(s->reconnect));
856 jsonrpc_session_disconnect(s);
858 reconnect_connected(s->reconnect, time_msec());
859 s->rpc = jsonrpc_open(stream);
860 } else if (error != EAGAIN) {
861 reconnect_listen_error(s->reconnect, time_msec(), error);
862 pstream_close(s->pstream);
871 error = jsonrpc_get_status(s->rpc);
873 reconnect_disconnected(s->reconnect, time_msec(), error);
874 jsonrpc_session_disconnect(s);
876 } else if (s->stream) {
879 stream_run(s->stream);
880 error = stream_connect(s->stream);
882 reconnect_connected(s->reconnect, time_msec());
883 s->rpc = jsonrpc_open(s->stream);
885 } else if (error != EAGAIN) {
886 reconnect_connect_failed(s->reconnect, time_msec(), error);
887 stream_close(s->stream);
892 switch (reconnect_run(s->reconnect, time_msec())) {
893 case RECONNECT_CONNECT:
894 jsonrpc_session_connect(s);
897 case RECONNECT_DISCONNECT:
898 reconnect_disconnected(s->reconnect, time_msec(), 0);
899 jsonrpc_session_disconnect(s);
902 case RECONNECT_PROBE:
905 struct jsonrpc_msg *request;
907 params = json_array_create_empty();
908 request = jsonrpc_create_request("echo", params, NULL);
909 json_destroy(request->id);
910 request->id = json_string_create("echo");
911 jsonrpc_send(s->rpc, request);
918 jsonrpc_session_wait(struct jsonrpc_session *s)
921 jsonrpc_wait(s->rpc);
922 } else if (s->stream) {
923 stream_run_wait(s->stream);
924 stream_connect_wait(s->stream);
927 pstream_wait(s->pstream);
929 reconnect_wait(s->reconnect, time_msec());
933 jsonrpc_session_get_backlog(const struct jsonrpc_session *s)
935 return s->rpc ? jsonrpc_get_backlog(s->rpc) : 0;
938 /* Always returns a pointer to a valid C string, assuming 's' was initialized
941 jsonrpc_session_get_name(const struct jsonrpc_session *s)
943 return reconnect_get_name(s->reconnect);
946 /* Always takes ownership of 'msg', regardless of success. */
948 jsonrpc_session_send(struct jsonrpc_session *s, struct jsonrpc_msg *msg)
951 return jsonrpc_send(s->rpc, msg);
953 jsonrpc_msg_destroy(msg);
959 jsonrpc_session_recv(struct jsonrpc_session *s)
962 struct jsonrpc_msg *msg;
963 jsonrpc_recv(s->rpc, &msg);
965 reconnect_received(s->reconnect, time_msec());
966 if (msg->type == JSONRPC_REQUEST && !strcmp(msg->method, "echo")) {
967 /* Echo request. Send reply. */
968 struct jsonrpc_msg *reply;
970 reply = jsonrpc_create_reply(json_clone(msg->params), msg->id);
971 jsonrpc_session_send(s, reply);
972 } else if (msg->type == JSONRPC_REPLY
973 && msg->id && msg->id->type == JSON_STRING
974 && !strcmp(msg->id->u.string, "echo")) {
975 /* It's a reply to our echo request. Suppress it. */
979 jsonrpc_msg_destroy(msg);
986 jsonrpc_session_recv_wait(struct jsonrpc_session *s)
989 jsonrpc_recv_wait(s->rpc);
994 jsonrpc_session_is_alive(const struct jsonrpc_session *s)
996 return s->rpc || s->stream || reconnect_get_max_tries(s->reconnect);
1000 jsonrpc_session_is_connected(const struct jsonrpc_session *s)
1002 return s->rpc != NULL;
1006 jsonrpc_session_get_seqno(const struct jsonrpc_session *s)
1012 jsonrpc_session_get_status(const struct jsonrpc_session *s)
1014 return s && s->rpc ? jsonrpc_get_status(s->rpc) : 0;
1018 jsonrpc_session_get_reconnect_stats(const struct jsonrpc_session *s,
1019 struct reconnect_stats *stats)
1021 reconnect_get_stats(s->reconnect, time_msec(), stats);
1025 jsonrpc_session_force_reconnect(struct jsonrpc_session *s)
1027 reconnect_force_reconnect(s->reconnect, time_msec());
1031 jsonrpc_session_set_max_backoff(struct jsonrpc_session *s, int max_backoff)
1033 reconnect_set_backoff(s->reconnect, 0, max_backoff);
1037 jsonrpc_session_set_probe_interval(struct jsonrpc_session *s,
1040 reconnect_set_probe_interval(s->reconnect, probe_interval);