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