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 #ifndef SPDK_JSONRPC_INTERNAL_H_ 35 #define SPDK_JSONRPC_INTERNAL_H_ 36 37 #include "spdk/stdinc.h" 38 39 #include "spdk/jsonrpc.h" 40 41 #include "spdk_internal/log.h" 42 43 #define SPDK_JSONRPC_RECV_BUF_SIZE (32 * 1024) 44 #define SPDK_JSONRPC_SEND_BUF_SIZE_INIT (32 * 1024) 45 #define SPDK_JSONRPC_SEND_BUF_SIZE_MAX (32 * 1024 * 1024) 46 #define SPDK_JSONRPC_ID_MAX_LEN 128 47 #define SPDK_JSONRPC_MAX_CONNS 64 48 #define SPDK_JSONRPC_MAX_VALUES 1024 49 #define SPDK_JSONRPC_CLIENT_MAX_VALUES 8192 50 51 struct spdk_jsonrpc_request { 52 struct spdk_jsonrpc_server_conn *conn; 53 54 /* Copy of request id value */ 55 const struct spdk_json_val *id; 56 57 /* Total space allocated for send_buf */ 58 size_t send_buf_size; 59 60 /* Number of bytes used in send_buf (<= send_buf_size) */ 61 size_t send_len; 62 63 size_t send_offset; 64 65 uint8_t *recv_buffer; 66 struct spdk_json_val *values; 67 size_t values_cnt; 68 69 uint8_t *send_buf; 70 71 struct spdk_json_write_ctx *response; 72 73 STAILQ_ENTRY(spdk_jsonrpc_request) link; 74 }; 75 76 struct spdk_jsonrpc_server_conn { 77 struct spdk_jsonrpc_server *server; 78 int sockfd; 79 bool closed; 80 size_t recv_len; 81 uint8_t recv_buf[SPDK_JSONRPC_RECV_BUF_SIZE]; 82 uint32_t outstanding_requests; 83 84 pthread_spinlock_t queue_lock; 85 STAILQ_HEAD(, spdk_jsonrpc_request) send_queue; 86 87 struct spdk_jsonrpc_request *send_request; 88 89 spdk_jsonrpc_conn_closed_fn close_cb; 90 void *close_cb_ctx; 91 92 TAILQ_ENTRY(spdk_jsonrpc_server_conn) link; 93 }; 94 95 struct spdk_jsonrpc_server { 96 int sockfd; 97 spdk_jsonrpc_handle_request_fn handle_request; 98 99 TAILQ_HEAD(, spdk_jsonrpc_server_conn) free_conns; 100 TAILQ_HEAD(, spdk_jsonrpc_server_conn) conns; 101 102 struct spdk_jsonrpc_server_conn conns_array[SPDK_JSONRPC_MAX_CONNS]; 103 }; 104 105 struct spdk_jsonrpc_client_request { 106 /* Total space allocated for send_buf */ 107 size_t send_buf_size; 108 109 /* Number of bytes used in send_buf (<= send_buf_size) */ 110 size_t send_len; 111 112 size_t send_offset; 113 114 uint8_t *send_buf; 115 }; 116 117 struct spdk_jsonrpc_client_response_internal { 118 struct spdk_jsonrpc_client_response jsonrpc; 119 bool ready; 120 uint8_t *buf; 121 size_t values_cnt; 122 struct spdk_json_val values[]; 123 }; 124 125 struct spdk_jsonrpc_client { 126 int sockfd; 127 bool connected; 128 129 size_t recv_buf_size; 130 size_t recv_offset; 131 char *recv_buf; 132 133 /* Parsed response */ 134 struct spdk_jsonrpc_client_response_internal *resp; 135 struct spdk_jsonrpc_client_request *request; 136 }; 137 138 /* jsonrpc_server_tcp */ 139 void jsonrpc_server_handle_request(struct spdk_jsonrpc_request *request, 140 const struct spdk_json_val *method, 141 const struct spdk_json_val *params); 142 void jsonrpc_server_handle_error(struct spdk_jsonrpc_request *request, int error); 143 144 /* Might be called from any thread */ 145 void jsonrpc_server_send_response(struct spdk_jsonrpc_request *request); 146 147 /* jsonrpc_server */ 148 int jsonrpc_parse_request(struct spdk_jsonrpc_server_conn *conn, const void *json, 149 size_t size); 150 151 /* Must be called only from server poll thread */ 152 void jsonrpc_free_request(struct spdk_jsonrpc_request *request); 153 154 /* 155 * Parse JSON data as RPC command response. 156 * 157 * \param client structure pointer of jsonrpc client 158 * 159 * \return 0 On success. Negative error code in error 160 * -EAGAIN - If the provided data is not a complete JSON value (SPDK_JSON_PARSE_INCOMPLETE) 161 * -EINVAL - If the provided data has invalid JSON syntax and can't be parsed (SPDK_JSON_PARSE_INVALID). 162 * -ENOSPC - No space left to store parsed response. 163 */ 164 int jsonrpc_parse_response(struct spdk_jsonrpc_client *client); 165 166 #endif 167