xref: /spdk/lib/jsonrpc/jsonrpc_client_tcp.c (revision 7506a7aa53d239f533af3bc768f0d2af55e735fe)
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 #include "spdk/string.h"
34 #include "jsonrpc_internal.h"
35 #include "spdk/util.h"
36 
37 #define RPC_DEFAULT_PORT	"5260"
38 
39 static int
40 jsonrpc_client_send_request(struct spdk_jsonrpc_client *client)
41 {
42 	ssize_t rc;
43 	struct spdk_jsonrpc_client_request *request = client->request;
44 
45 	if (!request) {
46 		return 0;
47 	}
48 
49 	if (request->send_len > 0) {
50 		rc = send(client->sockfd, request->send_buf + request->send_offset,
51 			  request->send_len, 0);
52 		if (rc < 0) {
53 			/* For EINTR we pretend that nothing was send. */
54 			if (errno == EINTR) {
55 				rc = 0;
56 			} else {
57 				rc = -errno;
58 				SPDK_ERRLOG("poll() failed (%d): %s\n", errno, spdk_strerror(errno));
59 			}
60 
61 			return rc;
62 		}
63 
64 		request->send_offset += rc;
65 		request->send_len -= rc;
66 	}
67 
68 	if (request->send_len == 0) {
69 		client->request = NULL;
70 		spdk_jsonrpc_client_free_request(request);
71 	}
72 
73 	return 0;
74 }
75 
76 static int
77 recv_buf_expand(struct spdk_jsonrpc_client *client)
78 {
79 	uint8_t *new_buf;
80 
81 	if (client->recv_buf_size * 2 > SPDK_JSONRPC_SEND_BUF_SIZE_MAX) {
82 		return -ENOSPC;
83 	}
84 
85 	new_buf = realloc(client->recv_buf, client->recv_buf_size * 2);
86 	if (new_buf == NULL) {
87 		SPDK_ERRLOG("Resizing recv_buf failed (current size %zu, new size %zu)\n",
88 			    client->recv_buf_size, client->recv_buf_size * 2);
89 		return -ENOMEM;
90 	}
91 
92 	client->recv_buf = new_buf;
93 	client->recv_buf_size *= 2;
94 
95 	return 0;
96 }
97 
98 static int
99 jsonrpc_client_resp_ready_count(struct spdk_jsonrpc_client *client)
100 {
101 	return client->resp != NULL && client->resp->ready ? 1 : 0;
102 }
103 
104 static int
105 jsonrpc_client_recv(struct spdk_jsonrpc_client *client)
106 {
107 	ssize_t rc;
108 
109 	if (client->recv_buf == NULL) {
110 		client->recv_buf = malloc(SPDK_JSONRPC_SEND_BUF_SIZE_INIT);
111 		if (!client->recv_buf) {
112 			rc = errno;
113 			SPDK_ERRLOG("malloc() failed (%d): %s\n", (int)rc, spdk_strerror(rc));
114 			return -rc;
115 		}
116 		client->recv_buf_size = SPDK_JSONRPC_SEND_BUF_SIZE_INIT;
117 		client->recv_offset = 0;
118 	} else if (client->recv_offset == client->recv_buf_size - 1) {
119 		rc = recv_buf_expand(client);
120 		if (rc) {
121 			return rc;
122 		}
123 	}
124 
125 	rc = recv(client->sockfd, client->recv_buf + client->recv_offset,
126 		  client->recv_buf_size - client->recv_offset - 1, 0);
127 	if (rc < 0) {
128 		/* For EINTR we pretend that nothing was received. */
129 		if (errno == EINTR) {
130 			return 0;
131 		} else {
132 			rc = -errno;
133 			SPDK_ERRLOG("recv() failed (%d): %s\n", errno, spdk_strerror(errno));
134 			return rc;
135 		}
136 	} else if (rc == 0) {
137 		return -EIO;
138 	}
139 
140 	client->recv_offset += rc;
141 	client->recv_buf[client->recv_offset] = '\0';
142 
143 	/* Check to see if we have received a full JSON value. */
144 	return jsonrpc_parse_response(client);
145 }
146 
147 static int
148 jsonrpc_client_poll(struct spdk_jsonrpc_client *client, int timeout)
149 {
150 	int rc;
151 	struct pollfd pfd = { .fd = client->sockfd, .events = POLLIN | POLLOUT };
152 
153 	rc = poll(&pfd, 1, timeout);
154 	if (rc == -1) {
155 		if (errno == EINTR) {
156 			/* For EINTR we pretend that nothing was received nor send. */
157 			rc = 0;
158 		} else {
159 			rc = -errno;
160 			SPDK_ERRLOG("poll() failed (%d): %s\n", errno, spdk_strerror(errno));
161 		}
162 	} else if (rc > 0) {
163 		rc = 0;
164 
165 		if (pfd.revents & POLLOUT) {
166 			rc = jsonrpc_client_send_request(client);
167 		}
168 
169 		if (rc == 0 && (pfd.revents & POLLIN)) {
170 			rc = jsonrpc_client_recv(client);
171 			/* Incomplete message in buffer isn't an error. */
172 			if (rc == -EAGAIN) {
173 				rc = 0;
174 			}
175 		}
176 	}
177 
178 	return rc ? rc : jsonrpc_client_resp_ready_count(client);
179 }
180 
181 static int
182 jsonrpc_client_poll_connecting(struct spdk_jsonrpc_client *client, int timeout)
183 {
184 	socklen_t rc_len;
185 	int rc;
186 
187 	struct pollfd pfd = {
188 		.fd = client->sockfd,
189 		.events = POLLOUT
190 	};
191 
192 	rc = poll(&pfd, 1, timeout);
193 	if (rc == 0) {
194 		return -ENOTCONN;
195 	} else if (rc == -1) {
196 		if (errno != EINTR) {
197 			SPDK_ERRLOG("poll() failed (%d): %s\n", errno, spdk_strerror(errno));
198 			goto err;
199 		}
200 
201 		/* We are still not connected. Caller will have to call us again. */
202 		return -ENOTCONN;
203 	} else if (pfd.revents & ~POLLOUT) {
204 		/* We only poll for POLLOUT */
205 		goto err;
206 	} else if ((pfd.revents & POLLOUT) == 0) {
207 		/* Is this even possible to get here? */
208 		return -ENOTCONN;
209 	}
210 
211 	rc_len = sizeof(int);
212 	/* connection might fail so need to check SO_ERROR. */
213 	if (getsockopt(client->sockfd, SOL_SOCKET, SO_ERROR, &rc, &rc_len) == -1) {
214 		goto err;
215 	}
216 
217 	if (rc == 0) {
218 		client->connected = true;
219 		return 0;
220 	}
221 
222 err:
223 	return -EIO;
224 }
225 
226 static int
227 jsonrpc_client_connect(struct spdk_jsonrpc_client *client, int domain, int protocol,
228 		       struct sockaddr *server_addr, socklen_t addrlen)
229 {
230 	int rc;
231 
232 	client->sockfd = socket(domain, SOCK_STREAM | SOCK_NONBLOCK, protocol);
233 	if (client->sockfd < 0) {
234 		rc = errno;
235 		SPDK_ERRLOG("socket() failed\n");
236 		return -rc;
237 	}
238 
239 	rc = connect(client->sockfd, server_addr, addrlen);
240 	if (rc != 0) {
241 		rc = errno;
242 		if (rc != EINPROGRESS) {
243 			SPDK_ERRLOG("could not connect to JSON-RPC server: %s\n", spdk_strerror(errno));
244 			goto err;
245 		}
246 	} else {
247 		client->connected = true;
248 	}
249 
250 	return -rc;
251 err:
252 	close(client->sockfd);
253 	client->sockfd = -1;
254 	return -rc;
255 }
256 
257 struct spdk_jsonrpc_client *
258 spdk_jsonrpc_client_connect(const char *addr, int addr_family)
259 {
260 	struct spdk_jsonrpc_client *client = calloc(1, sizeof(struct spdk_jsonrpc_client));
261 	/* Unix Domain Socket */
262 	struct sockaddr_un addr_un = {};
263 	char *add_in = NULL;
264 	int rc;
265 
266 	if (client == NULL) {
267 		SPDK_ERRLOG("%s\n", spdk_strerror(errno));
268 		return NULL;
269 	}
270 
271 	if (addr_family == AF_UNIX) {
272 		addr_un.sun_family = AF_UNIX;
273 		rc = snprintf(addr_un.sun_path, sizeof(addr_un.sun_path), "%s", addr);
274 		if (rc < 0 || (size_t)rc >= sizeof(addr_un.sun_path)) {
275 			rc = -EINVAL;
276 			SPDK_ERRLOG("RPC Listen address Unix socket path too long\n");
277 			goto err;
278 		}
279 
280 		rc = jsonrpc_client_connect(client, AF_UNIX, 0, (struct sockaddr *)&addr_un, sizeof(addr_un));
281 	} else {
282 		/* TCP/IP socket */
283 		struct addrinfo		hints;
284 		struct addrinfo		*res;
285 		char *host, *port;
286 
287 		add_in = strdup(addr);
288 		if (!add_in) {
289 			rc = -errno;
290 			SPDK_ERRLOG("%s\n", spdk_strerror(errno));
291 			goto err;
292 		}
293 
294 		rc = spdk_parse_ip_addr(add_in, &host, &port);
295 		if (rc) {
296 			SPDK_ERRLOG("Invalid listen address '%s'\n", addr);
297 			goto err;
298 		}
299 
300 		if (port == NULL) {
301 			port = RPC_DEFAULT_PORT;
302 		}
303 
304 		memset(&hints, 0, sizeof(hints));
305 		hints.ai_family = AF_UNSPEC;
306 		hints.ai_socktype = SOCK_STREAM;
307 		hints.ai_protocol = IPPROTO_TCP;
308 
309 		rc = getaddrinfo(host, port, &hints, &res);
310 		if (rc != 0) {
311 			SPDK_ERRLOG("Unable to look up RPC connect address '%s' (%d): %s\n", addr, rc, gai_strerror(rc));
312 			rc = -EINVAL;
313 			goto err;
314 		}
315 
316 		rc = jsonrpc_client_connect(client, res->ai_family, res->ai_protocol, res->ai_addr,
317 					    res->ai_addrlen);
318 		freeaddrinfo(res);
319 	}
320 
321 err:
322 	if (rc != 0 && rc != -EINPROGRESS) {
323 		free(client);
324 		client = NULL;
325 		errno = -rc;
326 	}
327 
328 	free(add_in);
329 	return client;
330 }
331 
332 void
333 spdk_jsonrpc_client_close(struct spdk_jsonrpc_client *client)
334 {
335 	if (client->sockfd >= 0) {
336 		close(client->sockfd);
337 	}
338 
339 	free(client->recv_buf);
340 	if (client->resp) {
341 		spdk_jsonrpc_client_free_response(&client->resp->jsonrpc);
342 	}
343 
344 	free(client);
345 }
346 
347 struct spdk_jsonrpc_client_request *
348 spdk_jsonrpc_client_create_request(void)
349 {
350 	struct spdk_jsonrpc_client_request *request;
351 
352 	request = calloc(1, sizeof(*request));
353 	if (request == NULL) {
354 		return NULL;
355 	}
356 
357 	/* memory malloc for send-buf */
358 	request->send_buf = malloc(SPDK_JSONRPC_SEND_BUF_SIZE_INIT);
359 	if (!request->send_buf) {
360 		SPDK_ERRLOG("memory malloc for send-buf failed\n");
361 		free(request);
362 		return NULL;
363 	}
364 	request->send_buf_size = SPDK_JSONRPC_SEND_BUF_SIZE_INIT;
365 
366 	return request;
367 }
368 
369 void
370 spdk_jsonrpc_client_free_request(struct spdk_jsonrpc_client_request *req)
371 {
372 	free(req->send_buf);
373 	free(req);
374 }
375 
376 int
377 spdk_jsonrpc_client_poll(struct spdk_jsonrpc_client *client, int timeout)
378 {
379 	if (client->connected) {
380 		return jsonrpc_client_poll(client, timeout);
381 	} else {
382 		return jsonrpc_client_poll_connecting(client, timeout);
383 	}
384 }
385 
386 int spdk_jsonrpc_client_send_request(struct spdk_jsonrpc_client *client,
387 				     struct spdk_jsonrpc_client_request *req)
388 {
389 	if (client->request != NULL) {
390 		return -ENOSPC;
391 	}
392 
393 	client->request = req;
394 	return 0;
395 }
396 
397 struct spdk_jsonrpc_client_response *
398 spdk_jsonrpc_client_get_response(struct spdk_jsonrpc_client *client)
399 {
400 	struct spdk_jsonrpc_client_response_internal *r;
401 
402 	r = client->resp;
403 	if (r == NULL || r->ready == false) {
404 		return NULL;
405 	}
406 
407 	client->resp = NULL;
408 	return &r->jsonrpc;
409 }
410 
411 void
412 spdk_jsonrpc_client_free_response(struct spdk_jsonrpc_client_response *resp)
413 {
414 	struct spdk_jsonrpc_client_response_internal *r;
415 
416 	if (!resp) {
417 		return;
418 	}
419 
420 	r = SPDK_CONTAINEROF(resp, struct spdk_jsonrpc_client_response_internal, jsonrpc);
421 	free(r->buf);
422 	free(r);
423 }
424