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 const struct spdk_json_val *params = NULL; 66 67 if (spdk_json_decode_object(values, jsonrpc_request_decoders, 68 SPDK_COUNTOF(jsonrpc_request_decoders), 69 &req)) { 70 goto invalid; 71 } 72 73 if (req.version && (req.version->type != SPDK_JSON_VAL_STRING || 74 !spdk_json_strequal(req.version, "2.0"))) { 75 goto invalid; 76 } 77 78 if (!req.method || req.method->type != SPDK_JSON_VAL_STRING) { 79 goto invalid; 80 } 81 82 if (req.id) { 83 if (req.id->type == SPDK_JSON_VAL_STRING || 84 req.id->type == SPDK_JSON_VAL_NUMBER || 85 req.id->type == SPDK_JSON_VAL_NULL) { 86 request->id = req.id; 87 } else { 88 goto invalid; 89 } 90 } 91 92 if (req.params) { 93 /* null json value is as if there were no parameters */ 94 if (req.params->type != SPDK_JSON_VAL_NULL) { 95 if (req.params->type != SPDK_JSON_VAL_ARRAY_BEGIN && 96 req.params->type != SPDK_JSON_VAL_OBJECT_BEGIN) { 97 goto invalid; 98 } 99 params = req.params; 100 } 101 } 102 103 jsonrpc_server_handle_request(request, req.method, params); 104 return; 105 106 invalid: 107 jsonrpc_server_handle_error(request, SPDK_JSONRPC_ERROR_INVALID_REQUEST); 108 } 109 110 static int 111 jsonrpc_server_write_cb(void *cb_ctx, const void *data, size_t size) 112 { 113 struct spdk_jsonrpc_request *request = cb_ctx; 114 size_t new_size = request->send_buf_size; 115 116 while (new_size - request->send_len < size) { 117 if (new_size >= SPDK_JSONRPC_SEND_BUF_SIZE_MAX) { 118 SPDK_ERRLOG("Send buf exceeded maximum size (%zu)\n", 119 (size_t)SPDK_JSONRPC_SEND_BUF_SIZE_MAX); 120 return -1; 121 } 122 123 new_size *= 2; 124 } 125 126 if (new_size != request->send_buf_size) { 127 uint8_t *new_buf; 128 129 new_buf = realloc(request->send_buf, new_size); 130 if (new_buf == NULL) { 131 SPDK_ERRLOG("Resizing send_buf failed (current size %zu, new size %zu)\n", 132 request->send_buf_size, new_size); 133 return -1; 134 } 135 136 request->send_buf = new_buf; 137 request->send_buf_size = new_size; 138 } 139 140 memcpy(request->send_buf + request->send_len, data, size); 141 request->send_len += size; 142 143 return 0; 144 } 145 146 int 147 jsonrpc_parse_request(struct spdk_jsonrpc_server_conn *conn, const void *json, size_t size) 148 { 149 struct spdk_jsonrpc_request *request; 150 ssize_t rc; 151 size_t len; 152 void *end = NULL; 153 154 /* Check to see if we have received a full JSON value. It is safe to cast away const 155 * as we don't decode in place. */ 156 rc = spdk_json_parse((void *)json, size, NULL, 0, &end, 0); 157 if (rc == SPDK_JSON_PARSE_INCOMPLETE) { 158 return 0; 159 } 160 161 request = calloc(1, sizeof(*request)); 162 if (request == NULL) { 163 SPDK_DEBUGLOG(rpc, "Out of memory allocating request\n"); 164 return -1; 165 } 166 167 conn->outstanding_requests++; 168 169 request->conn = conn; 170 171 len = end - json; 172 request->recv_buffer = malloc(len + 1); 173 if (request->recv_buffer == NULL) { 174 SPDK_ERRLOG("Failed to allocate buffer to copy request (%zu bytes)\n", len + 1); 175 jsonrpc_free_request(request); 176 return -1; 177 } 178 179 memcpy(request->recv_buffer, json, len); 180 request->recv_buffer[len] = '\0'; 181 182 if (rc > 0 && rc <= SPDK_JSONRPC_MAX_VALUES) { 183 request->values_cnt = rc; 184 request->values = malloc(request->values_cnt * sizeof(request->values[0])); 185 if (request->values == NULL) { 186 SPDK_ERRLOG("Failed to allocate buffer for JSON values (%zu bytes)\n", 187 request->values_cnt * sizeof(request->values[0])); 188 jsonrpc_free_request(request); 189 return -1; 190 } 191 } 192 193 request->send_offset = 0; 194 request->send_len = 0; 195 request->send_buf_size = SPDK_JSONRPC_SEND_BUF_SIZE_INIT; 196 request->send_buf = malloc(request->send_buf_size); 197 if (request->send_buf == NULL) { 198 SPDK_ERRLOG("Failed to allocate send_buf (%zu bytes)\n", request->send_buf_size); 199 jsonrpc_free_request(request); 200 return -1; 201 } 202 203 request->response = spdk_json_write_begin(jsonrpc_server_write_cb, request, 0); 204 if (request->response == NULL) { 205 SPDK_ERRLOG("Failed to allocate response JSON write context.\n"); 206 jsonrpc_free_request(request); 207 return -1; 208 } 209 210 if (rc <= 0 || rc > SPDK_JSONRPC_MAX_VALUES) { 211 SPDK_DEBUGLOG(rpc, "JSON parse error\n"); 212 jsonrpc_server_handle_error(request, SPDK_JSONRPC_ERROR_PARSE_ERROR); 213 214 /* 215 * Can't recover from parse error (no guaranteed resync point in streaming JSON). 216 * Return an error to indicate that the connection should be closed. 217 */ 218 return -1; 219 } 220 221 /* Decode a second time now that there is a full JSON value available. */ 222 rc = spdk_json_parse(request->recv_buffer, size, request->values, request->values_cnt, &end, 223 SPDK_JSON_PARSE_FLAG_DECODE_IN_PLACE); 224 if (rc < 0 || rc > SPDK_JSONRPC_MAX_VALUES) { 225 SPDK_DEBUGLOG(rpc, "JSON parse error on second pass\n"); 226 jsonrpc_server_handle_error(request, SPDK_JSONRPC_ERROR_PARSE_ERROR); 227 return -1; 228 } 229 230 assert(end != NULL); 231 232 if (request->values[0].type == SPDK_JSON_VAL_OBJECT_BEGIN) { 233 parse_single_request(request, request->values); 234 } else if (request->values[0].type == SPDK_JSON_VAL_ARRAY_BEGIN) { 235 SPDK_DEBUGLOG(rpc, "Got batch array (not currently supported)\n"); 236 jsonrpc_server_handle_error(request, SPDK_JSONRPC_ERROR_INVALID_REQUEST); 237 } else { 238 SPDK_DEBUGLOG(rpc, "top-level JSON value was not array or object\n"); 239 jsonrpc_server_handle_error(request, SPDK_JSONRPC_ERROR_INVALID_REQUEST); 240 } 241 242 return len; 243 } 244 245 struct spdk_jsonrpc_server_conn * 246 spdk_jsonrpc_get_conn(struct spdk_jsonrpc_request *request) 247 { 248 return request->conn; 249 } 250 251 /* Never return NULL */ 252 static struct spdk_json_write_ctx * 253 begin_response(struct spdk_jsonrpc_request *request) 254 { 255 struct spdk_json_write_ctx *w = request->response; 256 257 spdk_json_write_object_begin(w); 258 spdk_json_write_named_string(w, "jsonrpc", "2.0"); 259 260 spdk_json_write_name(w, "id"); 261 if (request->id) { 262 spdk_json_write_val(w, request->id); 263 } else { 264 spdk_json_write_null(w); 265 } 266 267 return w; 268 } 269 270 static void 271 skip_response(struct spdk_jsonrpc_request *request) 272 { 273 request->send_len = 0; 274 spdk_json_write_end(request->response); 275 request->response = NULL; 276 jsonrpc_server_send_response(request); 277 } 278 279 static void 280 end_response(struct spdk_jsonrpc_request *request) 281 { 282 spdk_json_write_object_end(request->response); 283 spdk_json_write_end(request->response); 284 request->response = NULL; 285 286 jsonrpc_server_write_cb(request, "\n", 1); 287 jsonrpc_server_send_response(request); 288 } 289 290 void 291 jsonrpc_free_request(struct spdk_jsonrpc_request *request) 292 { 293 if (!request) { 294 return; 295 } 296 297 /* We must send or skip response explicitly */ 298 assert(request->response == NULL); 299 300 request->conn->outstanding_requests--; 301 free(request->recv_buffer); 302 free(request->values); 303 free(request->send_buf); 304 free(request); 305 } 306 307 struct spdk_json_write_ctx * 308 spdk_jsonrpc_begin_result(struct spdk_jsonrpc_request *request) 309 { 310 struct spdk_json_write_ctx *w = begin_response(request); 311 312 spdk_json_write_name(w, "result"); 313 return w; 314 } 315 316 void 317 spdk_jsonrpc_end_result(struct spdk_jsonrpc_request *request, struct spdk_json_write_ctx *w) 318 { 319 assert(w != NULL); 320 assert(w == request->response); 321 322 /* If there was no ID in request we skip response. */ 323 if (request->id && request->id->type != SPDK_JSON_VAL_NULL) { 324 end_response(request); 325 } else { 326 skip_response(request); 327 } 328 } 329 330 void 331 spdk_jsonrpc_send_bool_response(struct spdk_jsonrpc_request *request, bool value) 332 { 333 struct spdk_json_write_ctx *w; 334 335 w = spdk_jsonrpc_begin_result(request); 336 assert(w != NULL); 337 spdk_json_write_bool(w, value); 338 spdk_jsonrpc_end_result(request, w); 339 } 340 341 void 342 spdk_jsonrpc_send_error_response(struct spdk_jsonrpc_request *request, 343 int error_code, const char *msg) 344 { 345 struct spdk_json_write_ctx *w = begin_response(request); 346 347 spdk_json_write_named_object_begin(w, "error"); 348 spdk_json_write_named_int32(w, "code", error_code); 349 spdk_json_write_named_string(w, "message", msg); 350 spdk_json_write_object_end(w); 351 352 end_response(request); 353 } 354 355 void 356 spdk_jsonrpc_send_error_response_fmt(struct spdk_jsonrpc_request *request, 357 int error_code, const char *fmt, ...) 358 { 359 struct spdk_json_write_ctx *w = begin_response(request); 360 va_list args; 361 362 spdk_json_write_named_object_begin(w, "error"); 363 spdk_json_write_named_int32(w, "code", error_code); 364 va_start(args, fmt); 365 spdk_json_write_named_string_fmt_v(w, "message", fmt, args); 366 va_end(args); 367 spdk_json_write_object_end(w); 368 369 end_response(request); 370 } 371 372 SPDK_LOG_REGISTER_COMPONENT(rpc) 373