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 reveived. */ 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, flags; 231 232 client->sockfd = socket(domain, SOCK_STREAM, protocol); 233 if (client->sockfd < 0) { 234 rc = errno; 235 SPDK_ERRLOG("socket() failed\n"); 236 return -rc; 237 } 238 239 flags = fcntl(client->sockfd, F_GETFL); 240 if (flags < 0 || fcntl(client->sockfd, F_SETFL, flags | O_NONBLOCK) < 0) { 241 rc = errno; 242 SPDK_ERRLOG("fcntl(): can't set nonblocking mode for socket (%d): %s\n", 243 errno, spdk_strerror(errno)); 244 goto err; 245 } 246 247 rc = connect(client->sockfd, server_addr, addrlen); 248 if (rc != 0) { 249 rc = errno; 250 if (rc != EINPROGRESS) { 251 SPDK_ERRLOG("could not connect to JSON-RPC server: %s\n", spdk_strerror(errno)); 252 goto err; 253 } 254 } else { 255 client->connected = true; 256 } 257 258 return -rc; 259 err: 260 close(client->sockfd); 261 client->sockfd = -1; 262 return -rc; 263 } 264 265 struct spdk_jsonrpc_client * 266 spdk_jsonrpc_client_connect(const char *addr, int addr_family) 267 { 268 struct spdk_jsonrpc_client *client = calloc(1, sizeof(struct spdk_jsonrpc_client)); 269 /* Unix Domain Socket */ 270 struct sockaddr_un addr_un = {}; 271 char *add_in = NULL; 272 int rc; 273 274 if (client == NULL) { 275 SPDK_ERRLOG("%s\n", spdk_strerror(errno)); 276 return NULL; 277 } 278 279 if (addr_family == AF_UNIX) { 280 addr_un.sun_family = AF_UNIX; 281 rc = snprintf(addr_un.sun_path, sizeof(addr_un.sun_path), "%s", addr); 282 if (rc < 0 || (size_t)rc >= sizeof(addr_un.sun_path)) { 283 rc = -EINVAL; 284 SPDK_ERRLOG("RPC Listen address Unix socket path too long\n"); 285 goto err; 286 } 287 288 rc = jsonrpc_client_connect(client, AF_UNIX, 0, (struct sockaddr *)&addr_un, sizeof(addr_un)); 289 } else { 290 /* TCP/IP socket */ 291 struct addrinfo hints; 292 struct addrinfo *res; 293 char *host, *port; 294 295 add_in = strdup(addr); 296 if (!add_in) { 297 rc = -errno; 298 SPDK_ERRLOG("%s\n", spdk_strerror(errno)); 299 goto err; 300 } 301 302 rc = spdk_parse_ip_addr(add_in, &host, &port); 303 if (rc) { 304 SPDK_ERRLOG("Invalid listen address '%s'\n", addr); 305 goto err; 306 } 307 308 if (port == NULL) { 309 port = RPC_DEFAULT_PORT; 310 } 311 312 memset(&hints, 0, sizeof(hints)); 313 hints.ai_family = AF_UNSPEC; 314 hints.ai_socktype = SOCK_STREAM; 315 hints.ai_protocol = IPPROTO_TCP; 316 317 rc = getaddrinfo(host, port, &hints, &res); 318 if (rc != 0) { 319 SPDK_ERRLOG("Unable to look up RPC connnect address '%s' (%d): %s\n", addr, rc, gai_strerror(rc)); 320 rc = -EINVAL; 321 goto err; 322 } 323 324 rc = jsonrpc_client_connect(client, res->ai_family, res->ai_protocol, res->ai_addr, 325 res->ai_addrlen); 326 freeaddrinfo(res); 327 } 328 329 err: 330 if (rc != 0 && rc != -EINPROGRESS) { 331 free(client); 332 client = NULL; 333 errno = -rc; 334 } 335 336 free(add_in); 337 return client; 338 } 339 340 void 341 spdk_jsonrpc_client_close(struct spdk_jsonrpc_client *client) 342 { 343 if (client->sockfd >= 0) { 344 close(client->sockfd); 345 } 346 347 free(client->recv_buf); 348 if (client->resp) { 349 spdk_jsonrpc_client_free_response(&client->resp->jsonrpc); 350 } 351 352 free(client); 353 } 354 355 struct spdk_jsonrpc_client_request * 356 spdk_jsonrpc_client_create_request(void) 357 { 358 struct spdk_jsonrpc_client_request *request; 359 360 request = calloc(1, sizeof(*request)); 361 if (request == NULL) { 362 return NULL; 363 } 364 365 /* memory malloc for send-buf */ 366 request->send_buf = malloc(SPDK_JSONRPC_SEND_BUF_SIZE_INIT); 367 if (!request->send_buf) { 368 SPDK_ERRLOG("memory malloc for send-buf failed\n"); 369 free(request); 370 return NULL; 371 } 372 request->send_buf_size = SPDK_JSONRPC_SEND_BUF_SIZE_INIT; 373 374 return request; 375 } 376 377 void 378 spdk_jsonrpc_client_free_request(struct spdk_jsonrpc_client_request *req) 379 { 380 free(req->send_buf); 381 free(req); 382 } 383 384 int 385 spdk_jsonrpc_client_poll(struct spdk_jsonrpc_client *client, int timeout) 386 { 387 if (client->connected) { 388 return jsonrpc_client_poll(client, timeout); 389 } else { 390 return jsonrpc_client_poll_connecting(client, timeout); 391 } 392 } 393 394 int spdk_jsonrpc_client_send_request(struct spdk_jsonrpc_client *client, 395 struct spdk_jsonrpc_client_request *req) 396 { 397 if (client->request != NULL) { 398 return -ENOSPC; 399 } 400 401 client->request = req; 402 return 0; 403 } 404 405 struct spdk_jsonrpc_client_response * 406 spdk_jsonrpc_client_get_response(struct spdk_jsonrpc_client *client) 407 { 408 struct spdk_jsonrpc_client_response_internal *r; 409 410 r = client->resp; 411 if (r == NULL || r->ready == false) { 412 return NULL; 413 } 414 415 client->resp = NULL; 416 return &r->jsonrpc; 417 } 418 419 void 420 spdk_jsonrpc_client_free_response(struct spdk_jsonrpc_client_response *resp) 421 { 422 struct spdk_jsonrpc_client_response_internal *r; 423 424 if (!resp) { 425 return; 426 } 427 428 r = SPDK_CONTAINEROF(resp, struct spdk_jsonrpc_client_response_internal, jsonrpc); 429 free(r->buf); 430 free(r); 431 } 432