xref: /spdk/lib/jsonrpc/jsonrpc_internal.h (revision 2fb672af968d9495a388262bd76f63c74b347af1)
1 /*   SPDX-License-Identifier: BSD-3-Clause
2  *   Copyright (C) 2016 Intel Corporation.
3  *   All rights reserved.
4  */
5 
6 #ifndef SPDK_JSONRPC_INTERNAL_H_
7 #define SPDK_JSONRPC_INTERNAL_H_
8 
9 #include "spdk/stdinc.h"
10 
11 #include "spdk/jsonrpc.h"
12 
13 #include "spdk/log.h"
14 
15 #define SPDK_JSONRPC_RECV_BUF_SIZE	(32 * 1024)
16 #define SPDK_JSONRPC_SEND_BUF_SIZE_INIT	(32 * 1024)
17 #define SPDK_JSONRPC_SEND_BUF_SIZE_MAX	(32 * 1024 * 1024)
18 #define SPDK_JSONRPC_ID_MAX_LEN		128
19 #define SPDK_JSONRPC_MAX_CONNS		64
20 #define SPDK_JSONRPC_MAX_VALUES		1024
21 #define SPDK_JSONRPC_CLIENT_MAX_VALUES		8192
22 
23 struct spdk_jsonrpc_request {
24 	struct spdk_jsonrpc_server_conn *conn;
25 
26 	/* Copy of request id value */
27 	const struct spdk_json_val *id;
28 
29 	/* Total space allocated for send_buf */
30 	size_t send_buf_size;
31 
32 	/* Number of bytes used in send_buf (<= send_buf_size) */
33 	size_t send_len;
34 
35 	size_t send_offset;
36 
37 	uint8_t *recv_buffer;
38 	struct spdk_json_val *values;
39 	size_t values_cnt;
40 
41 	uint8_t *send_buf;
42 
43 	struct spdk_json_write_ctx *response;
44 
45 	STAILQ_ENTRY(spdk_jsonrpc_request) link;
46 };
47 
48 struct spdk_jsonrpc_server_conn {
49 	struct spdk_jsonrpc_server *server;
50 	int sockfd;
51 	bool closed;
52 	size_t recv_len;
53 	uint8_t recv_buf[SPDK_JSONRPC_RECV_BUF_SIZE];
54 	uint32_t outstanding_requests;
55 
56 	pthread_spinlock_t queue_lock;
57 	STAILQ_HEAD(, spdk_jsonrpc_request) send_queue;
58 	/* List of incomplete requests that are not yet ready to be sent.
59 	 * This is a safety net for cases, where server shutdown is called
60 	 * before all requests are placed into send_queue. */
61 	STAILQ_HEAD(, spdk_jsonrpc_request) outstanding_queue;
62 
63 	struct spdk_jsonrpc_request *send_request;
64 
65 	spdk_jsonrpc_conn_closed_fn close_cb;
66 	void *close_cb_ctx;
67 
68 	TAILQ_ENTRY(spdk_jsonrpc_server_conn) link;
69 };
70 
71 struct spdk_jsonrpc_server {
72 	int sockfd;
73 	spdk_jsonrpc_handle_request_fn handle_request;
74 
75 	TAILQ_HEAD(, spdk_jsonrpc_server_conn) free_conns;
76 	TAILQ_HEAD(, spdk_jsonrpc_server_conn) conns;
77 
78 	struct spdk_jsonrpc_server_conn conns_array[SPDK_JSONRPC_MAX_CONNS];
79 };
80 
81 struct spdk_jsonrpc_client_request {
82 	/* Total space allocated for send_buf */
83 	size_t send_buf_size;
84 
85 	/* Number of bytes used in send_buf (<= send_buf_size) */
86 	size_t send_len;
87 
88 	size_t send_offset;
89 
90 	uint8_t *send_buf;
91 };
92 
93 struct spdk_jsonrpc_client_response_internal {
94 	struct spdk_jsonrpc_client_response jsonrpc;
95 	bool ready;
96 	uint8_t *buf;
97 	size_t values_cnt;
98 	struct spdk_json_val values[];
99 };
100 
101 struct spdk_jsonrpc_client {
102 	int sockfd;
103 	bool connected;
104 
105 	size_t recv_buf_size;
106 	size_t recv_offset;
107 	char *recv_buf;
108 
109 	/* Parsed response */
110 	struct spdk_jsonrpc_client_response_internal *resp;
111 	struct spdk_jsonrpc_client_request *request;
112 };
113 
114 /* jsonrpc_server_tcp */
115 void jsonrpc_server_handle_request(struct spdk_jsonrpc_request *request,
116 				   const struct spdk_json_val *method,
117 				   const struct spdk_json_val *params);
118 void jsonrpc_server_handle_error(struct spdk_jsonrpc_request *request, int error);
119 
120 /* Might be called from any thread */
121 void jsonrpc_server_send_response(struct spdk_jsonrpc_request *request);
122 
123 /* jsonrpc_server */
124 int jsonrpc_parse_request(struct spdk_jsonrpc_server_conn *conn, const void *json,
125 			  size_t size);
126 
127 /* Must be called only from server poll thread */
128 void jsonrpc_free_request(struct spdk_jsonrpc_request *request);
129 
130 /* Must be called only from server poll thread */
131 void jsonrpc_complete_request(struct spdk_jsonrpc_request *request);
132 
133 /*
134  * Parse JSON data as RPC command response.
135  *
136  * \param client structure pointer of jsonrpc client
137  *
138  * \return 0 On success. Negative error code in error
139  * -EAGAIN - If the provided data is not a complete JSON value (SPDK_JSON_PARSE_INCOMPLETE)
140  * -EINVAL - If the provided data has invalid JSON syntax and can't be parsed (SPDK_JSON_PARSE_INVALID).
141  * -ENOSPC - No space left to store parsed response.
142  */
143 int jsonrpc_parse_response(struct spdk_jsonrpc_client *client);
144 
145 #endif
146