xref: /spdk/lib/jsonrpc/jsonrpc_server.c (revision 552e21cce6cccbf833ed9109827e08337377d7ce)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright (c) Intel Corporation.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include "jsonrpc_internal.h"
35 
36 #include "spdk/util.h"
37 
38 struct jsonrpc_request {
39 	const struct spdk_json_val *version;
40 	const struct spdk_json_val *method;
41 	const struct spdk_json_val *params;
42 	const struct spdk_json_val *id;
43 };
44 
45 static int
46 capture_val(const struct spdk_json_val *val, void *out)
47 {
48 	const struct spdk_json_val **vptr = out;
49 
50 	*vptr = val;
51 	return 0;
52 }
53 
54 static const struct spdk_json_object_decoder jsonrpc_request_decoders[] = {
55 	{"jsonrpc", offsetof(struct jsonrpc_request, version), capture_val, true},
56 	{"method", offsetof(struct jsonrpc_request, method), capture_val},
57 	{"params", offsetof(struct jsonrpc_request, params), capture_val, true},
58 	{"id", offsetof(struct jsonrpc_request, id), capture_val, true},
59 };
60 
61 static void
62 parse_single_request(struct spdk_jsonrpc_request *request, struct spdk_json_val *values)
63 {
64 	struct jsonrpc_request req = {};
65 
66 	if (spdk_json_decode_object(values, jsonrpc_request_decoders,
67 				    SPDK_COUNTOF(jsonrpc_request_decoders),
68 				    &req)) {
69 		goto invalid;
70 	}
71 
72 	if (req.version && (req.version->type != SPDK_JSON_VAL_STRING ||
73 			    !spdk_json_strequal(req.version, "2.0"))) {
74 		goto invalid;
75 	}
76 
77 	if (!req.method || req.method->type != SPDK_JSON_VAL_STRING) {
78 		goto invalid;
79 	}
80 
81 	if (req.id) {
82 		if (req.id->type == SPDK_JSON_VAL_STRING ||
83 		    req.id->type == SPDK_JSON_VAL_NUMBER ||
84 		    req.id->type == SPDK_JSON_VAL_NULL) {
85 			request->id = req.id;
86 		} else  {
87 			goto invalid;
88 		}
89 	}
90 
91 	if (req.params) {
92 		if (req.params->type != SPDK_JSON_VAL_ARRAY_BEGIN &&
93 		    req.params->type != SPDK_JSON_VAL_OBJECT_BEGIN) {
94 			goto invalid;
95 		}
96 	}
97 
98 	spdk_jsonrpc_server_handle_request(request, req.method, req.params);
99 	return;
100 
101 invalid:
102 	spdk_jsonrpc_server_handle_error(request, SPDK_JSONRPC_ERROR_INVALID_REQUEST);
103 }
104 
105 int
106 spdk_jsonrpc_parse_request(struct spdk_jsonrpc_server_conn *conn, const void *json, size_t size)
107 {
108 	struct spdk_jsonrpc_request *request;
109 	ssize_t rc;
110 	size_t len;
111 	void *end = NULL;
112 
113 	/* Check to see if we have received a full JSON value. It is safe to cast away const
114 	 * as we don't decode in place. */
115 	rc = spdk_json_parse((void *)json, size, NULL, 0, &end, 0);
116 	if (rc == SPDK_JSON_PARSE_INCOMPLETE) {
117 		return 0;
118 	}
119 
120 	request = calloc(1, sizeof(*request));
121 	if (request == NULL) {
122 		SPDK_DEBUGLOG(SPDK_LOG_RPC, "Out of memory allocating request\n");
123 		return -1;
124 	}
125 
126 	conn->outstanding_requests++;
127 
128 	request->conn = conn;
129 
130 	len = end - json;
131 	request->recv_buffer = malloc(len + 1);
132 	if (request->recv_buffer == NULL) {
133 		SPDK_ERRLOG("Failed to allocate buffer to copy request (%zu bytes)\n", len + 1);
134 		spdk_jsonrpc_free_request(request);
135 		return -1;
136 	}
137 
138 	memcpy(request->recv_buffer, json, len);
139 	request->recv_buffer[len] = '\0';
140 
141 	if (rc > 0 && rc <= SPDK_JSONRPC_MAX_VALUES) {
142 		request->values_cnt = rc;
143 		request->values = malloc(request->values_cnt * sizeof(request->values[0]));
144 		if (request->values == NULL) {
145 			SPDK_ERRLOG("Failed to allocate buffer for JSON values (%zu bytes)\n",
146 				    request->values_cnt * sizeof(request->values[0]));
147 			spdk_jsonrpc_free_request(request);
148 			return -1;
149 		}
150 	}
151 
152 	request->send_offset = 0;
153 	request->send_len = 0;
154 	request->send_buf_size = SPDK_JSONRPC_SEND_BUF_SIZE_INIT;
155 	request->send_buf = malloc(request->send_buf_size);
156 	if (request->send_buf == NULL) {
157 		SPDK_ERRLOG("Failed to allocate send_buf (%zu bytes)\n", request->send_buf_size);
158 		spdk_jsonrpc_free_request(request);
159 		return -1;
160 	}
161 
162 	if (rc <= 0 || rc > SPDK_JSONRPC_MAX_VALUES) {
163 		SPDK_DEBUGLOG(SPDK_LOG_RPC, "JSON parse error\n");
164 		spdk_jsonrpc_server_handle_error(request, SPDK_JSONRPC_ERROR_PARSE_ERROR);
165 
166 		/*
167 		 * Can't recover from parse error (no guaranteed resync point in streaming JSON).
168 		 * Return an error to indicate that the connection should be closed.
169 		 */
170 		return -1;
171 	}
172 
173 	/* Decode a second time now that there is a full JSON value available. */
174 	rc = spdk_json_parse(request->recv_buffer, size, request->values, request->values_cnt, &end,
175 			     SPDK_JSON_PARSE_FLAG_DECODE_IN_PLACE);
176 	if (rc < 0 || rc > SPDK_JSONRPC_MAX_VALUES) {
177 		SPDK_DEBUGLOG(SPDK_LOG_RPC, "JSON parse error on second pass\n");
178 		spdk_jsonrpc_server_handle_error(request, SPDK_JSONRPC_ERROR_PARSE_ERROR);
179 		return -1;
180 	}
181 
182 	assert(end != NULL);
183 
184 	if (request->values[0].type == SPDK_JSON_VAL_OBJECT_BEGIN) {
185 		parse_single_request(request, request->values);
186 	} else if (request->values[0].type == SPDK_JSON_VAL_ARRAY_BEGIN) {
187 		SPDK_DEBUGLOG(SPDK_LOG_RPC, "Got batch array (not currently supported)\n");
188 		spdk_jsonrpc_server_handle_error(request, SPDK_JSONRPC_ERROR_INVALID_REQUEST);
189 	} else {
190 		SPDK_DEBUGLOG(SPDK_LOG_RPC, "top-level JSON value was not array or object\n");
191 		spdk_jsonrpc_server_handle_error(request, SPDK_JSONRPC_ERROR_INVALID_REQUEST);
192 	}
193 
194 	return len;
195 }
196 
197 struct spdk_jsonrpc_server_conn *
198 spdk_jsonrpc_get_conn(struct spdk_jsonrpc_request *request)
199 {
200 	return request->conn;
201 }
202 
203 static int
204 spdk_jsonrpc_server_write_cb(void *cb_ctx, const void *data, size_t size)
205 {
206 	struct spdk_jsonrpc_request *request = cb_ctx;
207 	size_t new_size = request->send_buf_size;
208 
209 	while (new_size - request->send_len < size) {
210 		if (new_size >= SPDK_JSONRPC_SEND_BUF_SIZE_MAX) {
211 			SPDK_ERRLOG("Send buf exceeded maximum size (%zu)\n",
212 				    (size_t)SPDK_JSONRPC_SEND_BUF_SIZE_MAX);
213 			return -1;
214 		}
215 
216 		new_size *= 2;
217 	}
218 
219 	if (new_size != request->send_buf_size) {
220 		uint8_t *new_buf;
221 
222 		new_buf = realloc(request->send_buf, new_size);
223 		if (new_buf == NULL) {
224 			SPDK_ERRLOG("Resizing send_buf failed (current size %zu, new size %zu)\n",
225 				    request->send_buf_size, new_size);
226 			return -1;
227 		}
228 
229 		request->send_buf = new_buf;
230 		request->send_buf_size = new_size;
231 	}
232 
233 	memcpy(request->send_buf + request->send_len, data, size);
234 	request->send_len += size;
235 
236 	return 0;
237 }
238 
239 static struct spdk_json_write_ctx *
240 begin_response(struct spdk_jsonrpc_request *request)
241 {
242 	struct spdk_json_write_ctx *w;
243 
244 	w = spdk_json_write_begin(spdk_jsonrpc_server_write_cb, request, 0);
245 	if (w == NULL) {
246 		return NULL;
247 	}
248 
249 	spdk_json_write_object_begin(w);
250 	spdk_json_write_name(w, "jsonrpc");
251 	spdk_json_write_string(w, "2.0");
252 
253 	spdk_json_write_name(w, "id");
254 	if (request->id) {
255 		spdk_json_write_val(w, request->id);
256 	} else {
257 		spdk_json_write_null(w);
258 	}
259 
260 	return w;
261 }
262 
263 static void
264 skip_response(struct spdk_jsonrpc_request *request)
265 {
266 	request->send_len = 0;
267 	spdk_jsonrpc_server_send_response(request);
268 }
269 
270 static void
271 end_response(struct spdk_jsonrpc_request *request, struct spdk_json_write_ctx *w)
272 {
273 	spdk_json_write_object_end(w);
274 	spdk_json_write_end(w);
275 	spdk_jsonrpc_server_write_cb(request, "\n", 1);
276 	spdk_jsonrpc_server_send_response(request);
277 }
278 
279 void
280 spdk_jsonrpc_free_request(struct spdk_jsonrpc_request *request)
281 {
282 	if (!request) {
283 		return;
284 	}
285 
286 	request->conn->outstanding_requests--;
287 	free(request->recv_buffer);
288 	free(request->values);
289 	free(request->send_buf);
290 	free(request);
291 }
292 
293 struct spdk_json_write_ctx *
294 spdk_jsonrpc_begin_result(struct spdk_jsonrpc_request *request)
295 {
296 	struct spdk_json_write_ctx *w;
297 
298 	if (request->id == NULL || request->id->type == SPDK_JSON_VAL_NULL) {
299 		/* Notification - no response required */
300 		skip_response(request);
301 		return NULL;
302 	}
303 
304 	w = begin_response(request);
305 	if (w == NULL) {
306 		skip_response(request);
307 		return NULL;
308 	}
309 
310 	spdk_json_write_name(w, "result");
311 
312 	return w;
313 }
314 
315 void
316 spdk_jsonrpc_end_result(struct spdk_jsonrpc_request *request, struct spdk_json_write_ctx *w)
317 {
318 	assert(w != NULL);
319 
320 	end_response(request, w);
321 }
322 
323 void
324 spdk_jsonrpc_send_error_response(struct spdk_jsonrpc_request *request,
325 				 int error_code, const char *msg)
326 {
327 	struct spdk_json_write_ctx *w;
328 
329 	w = begin_response(request);
330 	if (w == NULL) {
331 		skip_response(request);
332 		return;
333 	}
334 
335 	spdk_json_write_name(w, "error");
336 	spdk_json_write_object_begin(w);
337 	spdk_json_write_name(w, "code");
338 	spdk_json_write_int32(w, error_code);
339 	spdk_json_write_name(w, "message");
340 	spdk_json_write_string(w, msg);
341 	spdk_json_write_object_end(w);
342 
343 	end_response(request, w);
344 }
345 
346 void
347 spdk_jsonrpc_send_error_response_fmt(struct spdk_jsonrpc_request *request,
348 				     int error_code, const char *fmt, ...)
349 {
350 	struct spdk_json_write_ctx *w;
351 	va_list args;
352 
353 	w = begin_response(request);
354 	if (w == NULL) {
355 		skip_response(request);
356 		return;
357 	}
358 
359 	spdk_json_write_name(w, "error");
360 	spdk_json_write_object_begin(w);
361 	spdk_json_write_name(w, "code");
362 	spdk_json_write_int32(w, error_code);
363 	spdk_json_write_name(w, "message");
364 	va_start(args, fmt);
365 	spdk_json_write_string_fmt_v(w, fmt, args);
366 	va_end(args);
367 	spdk_json_write_object_end(w);
368 
369 	end_response(request, w);
370 }
371 
372 SPDK_LOG_REGISTER_COMPONENT("rpc", SPDK_LOG_RPC)
373