diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 18:45:59 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 18:45:59 +0000 |
commit | 19fcec84d8d7d21e796c7624e521b60d28ee21ed (patch) | |
tree | 42d26aa27d1e3f7c0b8bd3fd14e7d7082f5008dc /src/spdk/lib/jsonrpc | |
parent | Initial commit. (diff) | |
download | ceph-19fcec84d8d7d21e796c7624e521b60d28ee21ed.tar.xz ceph-19fcec84d8d7d21e796c7624e521b60d28ee21ed.zip |
Adding upstream version 16.2.11+ds.upstream/16.2.11+dsupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/spdk/lib/jsonrpc')
-rw-r--r-- | src/spdk/lib/jsonrpc/Makefile | 46 | ||||
-rw-r--r-- | src/spdk/lib/jsonrpc/jsonrpc_client.c | 227 | ||||
-rw-r--r-- | src/spdk/lib/jsonrpc/jsonrpc_client_tcp.c | 431 | ||||
-rw-r--r-- | src/spdk/lib/jsonrpc/jsonrpc_internal.h | 166 | ||||
-rw-r--r-- | src/spdk/lib/jsonrpc/jsonrpc_server.c | 361 | ||||
-rw-r--r-- | src/spdk/lib/jsonrpc/jsonrpc_server_tcp.c | 441 | ||||
-rw-r--r-- | src/spdk/lib/jsonrpc/spdk_jsonrpc.map | 28 |
7 files changed, 1700 insertions, 0 deletions
diff --git a/src/spdk/lib/jsonrpc/Makefile b/src/spdk/lib/jsonrpc/Makefile new file mode 100644 index 000000000..7eb8dd683 --- /dev/null +++ b/src/spdk/lib/jsonrpc/Makefile @@ -0,0 +1,46 @@ +# +# BSD LICENSE +# +# Copyright (c) Intel Corporation. +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in +# the documentation and/or other materials provided with the +# distribution. +# * Neither the name of Intel Corporation nor the names of its +# contributors may be used to endorse or promote products derived +# from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# + +SPDK_ROOT_DIR := $(abspath $(CURDIR)/../..) +include $(SPDK_ROOT_DIR)/mk/spdk.common.mk + +SO_VER := 2 +SO_MINOR := 0 + +LIBNAME = jsonrpc +C_SRCS = jsonrpc_server.c jsonrpc_server_tcp.c +C_SRCS += jsonrpc_client.c jsonrpc_client_tcp.c + +SPDK_MAP_FILE = $(abspath $(CURDIR)/spdk_jsonrpc.map) + +include $(SPDK_ROOT_DIR)/mk/spdk.lib.mk diff --git a/src/spdk/lib/jsonrpc/jsonrpc_client.c b/src/spdk/lib/jsonrpc/jsonrpc_client.c new file mode 100644 index 000000000..e3940a4d4 --- /dev/null +++ b/src/spdk/lib/jsonrpc/jsonrpc_client.c @@ -0,0 +1,227 @@ +/*- + * BSD LICENSE + * + * Copyright (c) Intel Corporation. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Intel Corporation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "spdk/util.h" +#include "jsonrpc_internal.h" + +static int +capture_version(const struct spdk_json_val *val, void *out) +{ + const struct spdk_json_val **vptr = out; + + if (spdk_json_strequal(val, "2.0") != true) { + return SPDK_JSON_PARSE_INVALID; + } + + *vptr = val; + return 0; +} + +static int +capture_id(const struct spdk_json_val *val, void *out) +{ + const struct spdk_json_val **vptr = out; + + if (val->type != SPDK_JSON_VAL_STRING && val->type != SPDK_JSON_VAL_NUMBER) { + return -EINVAL; + } + + *vptr = val; + return 0; +} + +static int +capture_any(const struct spdk_json_val *val, void *out) +{ + const struct spdk_json_val **vptr = out; + + *vptr = val; + return 0; +} + +static const struct spdk_json_object_decoder jsonrpc_response_decoders[] = { + {"jsonrpc", offsetof(struct spdk_jsonrpc_client_response, version), capture_version}, + {"id", offsetof(struct spdk_jsonrpc_client_response, id), capture_id, true}, + {"result", offsetof(struct spdk_jsonrpc_client_response, result), capture_any, true}, + {"error", offsetof(struct spdk_jsonrpc_client_response, error), capture_any, true}, +}; + +int +jsonrpc_parse_response(struct spdk_jsonrpc_client *client) +{ + struct spdk_jsonrpc_client_response_internal *r; + ssize_t rc; + size_t buf_len; + size_t values_cnt; + void *end = NULL; + + + /* Check to see if we have received a full JSON value. */ + rc = spdk_json_parse(client->recv_buf, client->recv_offset, NULL, 0, &end, 0); + if (rc == SPDK_JSON_PARSE_INCOMPLETE) { + return 0; + } + + SPDK_DEBUGLOG(SPDK_LOG_RPC_CLIENT, "JSON string is :\n%s\n", client->recv_buf); + if (rc < 0 || rc > SPDK_JSONRPC_CLIENT_MAX_VALUES) { + SPDK_ERRLOG("JSON parse error (rc: %zd)\n", rc); + /* + * Can't recover from parse error (no guaranteed resync point in streaming JSON). + * Return an error to indicate that the connection should be closed. + */ + return -EINVAL; + } + + values_cnt = rc; + + r = calloc(1, sizeof(*r) + sizeof(struct spdk_json_val) * (values_cnt + 1)); + if (!r) { + return -errno; + } + + if (client->resp) { + free(r); + return -ENOSPC; + } + + client->resp = r; + + r->buf = client->recv_buf; + buf_len = client->recv_offset; + r->values_cnt = values_cnt; + + client->recv_buf_size = 0; + client->recv_offset = 0; + client->recv_buf = NULL; + + /* Decode a second time now that there is a full JSON value available. */ + rc = spdk_json_parse(r->buf, buf_len, r->values, values_cnt, &end, + SPDK_JSON_PARSE_FLAG_DECODE_IN_PLACE); + if (rc != (ssize_t)values_cnt) { + SPDK_ERRLOG("JSON parse error on second pass (rc: %zd, expected: %zu)\n", rc, values_cnt); + goto err; + } + + assert(end != NULL); + + if (r->values[0].type != SPDK_JSON_VAL_OBJECT_BEGIN) { + SPDK_ERRLOG("top-level JSON value was not object\n"); + goto err; + } + + if (spdk_json_decode_object(r->values, jsonrpc_response_decoders, + SPDK_COUNTOF(jsonrpc_response_decoders), &r->jsonrpc)) { + goto err; + } + + r->ready = 1; + return 1; + +err: + client->resp = NULL; + spdk_jsonrpc_client_free_response(&r->jsonrpc); + return -EINVAL; +} + +static int +jsonrpc_client_write_cb(void *cb_ctx, const void *data, size_t size) +{ + struct spdk_jsonrpc_client_request *request = cb_ctx; + size_t new_size = request->send_buf_size; + + while (new_size - request->send_len < size) { + if (new_size >= SPDK_JSONRPC_SEND_BUF_SIZE_MAX) { + SPDK_ERRLOG("Send buf exceeded maximum size (%zu)\n", + (size_t)SPDK_JSONRPC_SEND_BUF_SIZE_MAX); + return -ENOSPC; + } + + new_size *= 2; + } + + if (new_size != request->send_buf_size) { + uint8_t *new_buf; + + new_buf = realloc(request->send_buf, new_size); + if (new_buf == NULL) { + SPDK_ERRLOG("Resizing send_buf failed (current size %zu, new size %zu)\n", + request->send_buf_size, new_size); + return -ENOMEM; + } + + request->send_buf = new_buf; + request->send_buf_size = new_size; + } + + memcpy(request->send_buf + request->send_len, data, size); + request->send_len += size; + + return 0; +} + +struct spdk_json_write_ctx * +spdk_jsonrpc_begin_request(struct spdk_jsonrpc_client_request *request, int32_t id, + const char *method) +{ + struct spdk_json_write_ctx *w; + + w = spdk_json_write_begin(jsonrpc_client_write_cb, request, 0); + if (w == NULL) { + return NULL; + } + + spdk_json_write_object_begin(w); + spdk_json_write_named_string(w, "jsonrpc", "2.0"); + + if (id >= 0) { + spdk_json_write_named_int32(w, "id", id); + } + + if (method) { + spdk_json_write_named_string(w, "method", method); + } + + return w; +} + +void +spdk_jsonrpc_end_request(struct spdk_jsonrpc_client_request *request, struct spdk_json_write_ctx *w) +{ + assert(w != NULL); + + spdk_json_write_object_end(w); + spdk_json_write_end(w); + jsonrpc_client_write_cb(request, "\n", 1); +} + +SPDK_LOG_REGISTER_COMPONENT("rpc_client", SPDK_LOG_RPC_CLIENT) diff --git a/src/spdk/lib/jsonrpc/jsonrpc_client_tcp.c b/src/spdk/lib/jsonrpc/jsonrpc_client_tcp.c new file mode 100644 index 000000000..512f6261c --- /dev/null +++ b/src/spdk/lib/jsonrpc/jsonrpc_client_tcp.c @@ -0,0 +1,431 @@ +/*- + * BSD LICENSE + * + * Copyright (c) Intel Corporation. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Intel Corporation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +#include "spdk/string.h" +#include "jsonrpc_internal.h" +#include "spdk/util.h" + +#define RPC_DEFAULT_PORT "5260" + +static int +jsonrpc_client_send_request(struct spdk_jsonrpc_client *client) +{ + ssize_t rc; + struct spdk_jsonrpc_client_request *request = client->request; + + if (!request) { + return 0; + } + + if (request->send_len > 0) { + rc = send(client->sockfd, request->send_buf + request->send_offset, + request->send_len, 0); + if (rc < 0) { + /* For EINTR we pretend that nothing was send. */ + if (errno == EINTR) { + rc = 0; + } else { + rc = -errno; + SPDK_ERRLOG("poll() failed (%d): %s\n", errno, spdk_strerror(errno)); + } + + return rc; + } + + request->send_offset += rc; + request->send_len -= rc; + } + + if (request->send_len == 0) { + client->request = NULL; + spdk_jsonrpc_client_free_request(request); + } + + return 0; +} + +static int +recv_buf_expand(struct spdk_jsonrpc_client *client) +{ + uint8_t *new_buf; + + if (client->recv_buf_size * 2 > SPDK_JSONRPC_SEND_BUF_SIZE_MAX) { + return -ENOSPC; + } + + new_buf = realloc(client->recv_buf, client->recv_buf_size * 2); + if (new_buf == NULL) { + SPDK_ERRLOG("Resizing recv_buf failed (current size %zu, new size %zu)\n", + client->recv_buf_size, client->recv_buf_size * 2); + return -ENOMEM; + } + + client->recv_buf = new_buf; + client->recv_buf_size *= 2; + + return 0; +} + +static int +jsonrpc_client_resp_ready_count(struct spdk_jsonrpc_client *client) +{ + return client->resp != NULL && client->resp->ready ? 1 : 0; +} + +static int +jsonrpc_client_recv(struct spdk_jsonrpc_client *client) +{ + ssize_t rc; + + if (client->recv_buf == NULL) { + client->recv_buf = malloc(SPDK_JSONRPC_SEND_BUF_SIZE_INIT); + if (!client->recv_buf) { + rc = errno; + SPDK_ERRLOG("malloc() failed (%d): %s\n", (int)rc, spdk_strerror(rc)); + return -rc; + } + client->recv_buf_size = SPDK_JSONRPC_SEND_BUF_SIZE_INIT; + client->recv_offset = 0; + } else if (client->recv_offset == client->recv_buf_size - 1) { + rc = recv_buf_expand(client); + if (rc) { + return rc; + } + } + + rc = recv(client->sockfd, client->recv_buf + client->recv_offset, + client->recv_buf_size - client->recv_offset - 1, 0); + if (rc < 0) { + /* For EINTR we pretend that nothing was reveived. */ + if (errno == EINTR) { + return 0; + } else { + rc = -errno; + SPDK_ERRLOG("recv() failed (%d): %s\n", errno, spdk_strerror(errno)); + return rc; + } + } else if (rc == 0) { + return -EIO; + } + + client->recv_offset += rc; + client->recv_buf[client->recv_offset] = '\0'; + + /* Check to see if we have received a full JSON value. */ + return jsonrpc_parse_response(client); +} + +static int +jsonrpc_client_poll(struct spdk_jsonrpc_client *client, int timeout) +{ + int rc; + struct pollfd pfd = { .fd = client->sockfd, .events = POLLIN | POLLOUT }; + + rc = poll(&pfd, 1, timeout); + if (rc == -1) { + if (errno == EINTR) { + /* For EINTR we pretend that nothing was received nor send. */ + rc = 0; + } else { + rc = -errno; + SPDK_ERRLOG("poll() failed (%d): %s\n", errno, spdk_strerror(errno)); + } + } else if (rc > 0) { + rc = 0; + + if (pfd.revents & POLLOUT) { + rc = jsonrpc_client_send_request(client); + } + + if (rc == 0 && (pfd.revents & POLLIN)) { + rc = jsonrpc_client_recv(client); + /* Incomplete message in buffer isn't an error. */ + if (rc == -EAGAIN) { + rc = 0; + } + } + } + + return rc ? rc : jsonrpc_client_resp_ready_count(client); +} + +static int +jsonrpc_client_poll_connecting(struct spdk_jsonrpc_client *client, int timeout) +{ + socklen_t rc_len; + int rc; + + struct pollfd pfd = { + .fd = client->sockfd, + .events = POLLOUT + }; + + rc = poll(&pfd, 1, timeout); + if (rc == 0) { + return -ENOTCONN; + } else if (rc == -1) { + if (errno != EINTR) { + SPDK_ERRLOG("poll() failed (%d): %s\n", errno, spdk_strerror(errno)); + goto err; + } + + /* We are still not connected. Caller will have to call us again. */ + return -ENOTCONN; + } else if (pfd.revents & ~POLLOUT) { + /* We only poll for POLLOUT */ + goto err; + } else if ((pfd.revents & POLLOUT) == 0) { + /* Is this even possible to get here? */ + return -ENOTCONN; + } + + rc_len = sizeof(int); + /* connection might fail so need to check SO_ERROR. */ + if (getsockopt(client->sockfd, SOL_SOCKET, SO_ERROR, &rc, &rc_len) == -1) { + goto err; + } + + if (rc == 0) { + client->connected = true; + return 0; + } + +err: + return -EIO; +} + +static int +jsonrpc_client_connect(struct spdk_jsonrpc_client *client, int domain, int protocol, + struct sockaddr *server_addr, socklen_t addrlen) +{ + int rc, flags; + + client->sockfd = socket(domain, SOCK_STREAM, protocol); + if (client->sockfd < 0) { + rc = errno; + SPDK_ERRLOG("socket() failed\n"); + return -rc; + } + + flags = fcntl(client->sockfd, F_GETFL); + if (flags < 0 || fcntl(client->sockfd, F_SETFL, flags | O_NONBLOCK) < 0) { + rc = errno; + SPDK_ERRLOG("fcntl(): can't set nonblocking mode for socket (%d): %s\n", + errno, spdk_strerror(errno)); + goto err; + } + + rc = connect(client->sockfd, server_addr, addrlen); + if (rc != 0) { + rc = errno; + if (rc != EINPROGRESS) { + SPDK_ERRLOG("could not connect to JSON-RPC server: %s\n", spdk_strerror(errno)); + goto err; + } + } else { + client->connected = true; + } + + return -rc; +err: + close(client->sockfd); + client->sockfd = -1; + return -rc; +} + +struct spdk_jsonrpc_client * +spdk_jsonrpc_client_connect(const char *addr, int addr_family) +{ + struct spdk_jsonrpc_client *client = calloc(1, sizeof(struct spdk_jsonrpc_client)); + /* Unix Domain Socket */ + struct sockaddr_un addr_un = {}; + char *add_in = NULL; + int rc; + + if (client == NULL) { + SPDK_ERRLOG("%s\n", spdk_strerror(errno)); + return NULL; + } + + if (addr_family == AF_UNIX) { + addr_un.sun_family = AF_UNIX; + rc = snprintf(addr_un.sun_path, sizeof(addr_un.sun_path), "%s", addr); + if (rc < 0 || (size_t)rc >= sizeof(addr_un.sun_path)) { + rc = -EINVAL; + SPDK_ERRLOG("RPC Listen address Unix socket path too long\n"); + goto err; + } + + rc = jsonrpc_client_connect(client, AF_UNIX, 0, (struct sockaddr *)&addr_un, sizeof(addr_un)); + } else { + /* TCP/IP socket */ + struct addrinfo hints; + struct addrinfo *res; + char *host, *port; + + add_in = strdup(addr); + if (!add_in) { + rc = -errno; + SPDK_ERRLOG("%s\n", spdk_strerror(errno)); + goto err; + } + + rc = spdk_parse_ip_addr(add_in, &host, &port); + if (rc) { + SPDK_ERRLOG("Invalid listen address '%s'\n", addr); + goto err; + } + + if (port == NULL) { + port = RPC_DEFAULT_PORT; + } + + memset(&hints, 0, sizeof(hints)); + hints.ai_family = AF_UNSPEC; + hints.ai_socktype = SOCK_STREAM; + hints.ai_protocol = IPPROTO_TCP; + + rc = getaddrinfo(host, port, &hints, &res); + if (rc != 0) { + SPDK_ERRLOG("Unable to look up RPC connnect address '%s' (%d): %s\n", addr, rc, gai_strerror(rc)); + rc = -EINVAL; + goto err; + } + + rc = jsonrpc_client_connect(client, res->ai_family, res->ai_protocol, res->ai_addr, + res->ai_addrlen); + freeaddrinfo(res); + } + +err: + if (rc != 0 && rc != -EINPROGRESS) { + free(client); + client = NULL; + errno = -rc; + } + + free(add_in); + return client; +} + +void +spdk_jsonrpc_client_close(struct spdk_jsonrpc_client *client) +{ + if (client->sockfd >= 0) { + close(client->sockfd); + } + + free(client->recv_buf); + if (client->resp) { + spdk_jsonrpc_client_free_response(&client->resp->jsonrpc); + } + + free(client); +} + +struct spdk_jsonrpc_client_request * +spdk_jsonrpc_client_create_request(void) +{ + struct spdk_jsonrpc_client_request *request; + + request = calloc(1, sizeof(*request)); + if (request == NULL) { + return NULL; + } + + /* memory malloc for send-buf */ + request->send_buf = malloc(SPDK_JSONRPC_SEND_BUF_SIZE_INIT); + if (!request->send_buf) { + SPDK_ERRLOG("memory malloc for send-buf failed\n"); + free(request); + return NULL; + } + request->send_buf_size = SPDK_JSONRPC_SEND_BUF_SIZE_INIT; + + return request; +} + +void +spdk_jsonrpc_client_free_request(struct spdk_jsonrpc_client_request *req) +{ + free(req->send_buf); + free(req); +} + +int +spdk_jsonrpc_client_poll(struct spdk_jsonrpc_client *client, int timeout) +{ + if (client->connected) { + return jsonrpc_client_poll(client, timeout); + } else { + return jsonrpc_client_poll_connecting(client, timeout); + } +} + +int spdk_jsonrpc_client_send_request(struct spdk_jsonrpc_client *client, + struct spdk_jsonrpc_client_request *req) +{ + if (client->request != NULL) { + return -ENOSPC; + } + + client->request = req; + return 0; +} + +struct spdk_jsonrpc_client_response * +spdk_jsonrpc_client_get_response(struct spdk_jsonrpc_client *client) +{ + struct spdk_jsonrpc_client_response_internal *r; + + r = client->resp; + if (r == NULL || r->ready == false) { + return NULL; + } + + client->resp = NULL; + return &r->jsonrpc; +} + +void +spdk_jsonrpc_client_free_response(struct spdk_jsonrpc_client_response *resp) +{ + struct spdk_jsonrpc_client_response_internal *r; + + if (!resp) { + return; + } + + r = SPDK_CONTAINEROF(resp, struct spdk_jsonrpc_client_response_internal, jsonrpc); + free(r->buf); + free(r); +} diff --git a/src/spdk/lib/jsonrpc/jsonrpc_internal.h b/src/spdk/lib/jsonrpc/jsonrpc_internal.h new file mode 100644 index 000000000..f51bedf62 --- /dev/null +++ b/src/spdk/lib/jsonrpc/jsonrpc_internal.h @@ -0,0 +1,166 @@ +/*- + * BSD LICENSE + * + * Copyright (c) Intel Corporation. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Intel Corporation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef SPDK_JSONRPC_INTERNAL_H_ +#define SPDK_JSONRPC_INTERNAL_H_ + +#include "spdk/stdinc.h" + +#include "spdk/jsonrpc.h" + +#include "spdk_internal/log.h" + +#define SPDK_JSONRPC_RECV_BUF_SIZE (32 * 1024) +#define SPDK_JSONRPC_SEND_BUF_SIZE_INIT (32 * 1024) +#define SPDK_JSONRPC_SEND_BUF_SIZE_MAX (32 * 1024 * 1024) +#define SPDK_JSONRPC_ID_MAX_LEN 128 +#define SPDK_JSONRPC_MAX_CONNS 64 +#define SPDK_JSONRPC_MAX_VALUES 1024 +#define SPDK_JSONRPC_CLIENT_MAX_VALUES 8192 + +struct spdk_jsonrpc_request { + struct spdk_jsonrpc_server_conn *conn; + + /* Copy of request id value */ + const struct spdk_json_val *id; + + /* Total space allocated for send_buf */ + size_t send_buf_size; + + /* Number of bytes used in send_buf (<= send_buf_size) */ + size_t send_len; + + size_t send_offset; + + uint8_t *recv_buffer; + struct spdk_json_val *values; + size_t values_cnt; + + uint8_t *send_buf; + + struct spdk_json_write_ctx *response; + + STAILQ_ENTRY(spdk_jsonrpc_request) link; +}; + +struct spdk_jsonrpc_server_conn { + struct spdk_jsonrpc_server *server; + int sockfd; + bool closed; + size_t recv_len; + uint8_t recv_buf[SPDK_JSONRPC_RECV_BUF_SIZE]; + uint32_t outstanding_requests; + + pthread_spinlock_t queue_lock; + STAILQ_HEAD(, spdk_jsonrpc_request) send_queue; + + struct spdk_jsonrpc_request *send_request; + + spdk_jsonrpc_conn_closed_fn close_cb; + void *close_cb_ctx; + + TAILQ_ENTRY(spdk_jsonrpc_server_conn) link; +}; + +struct spdk_jsonrpc_server { + int sockfd; + spdk_jsonrpc_handle_request_fn handle_request; + + TAILQ_HEAD(, spdk_jsonrpc_server_conn) free_conns; + TAILQ_HEAD(, spdk_jsonrpc_server_conn) conns; + + struct spdk_jsonrpc_server_conn conns_array[SPDK_JSONRPC_MAX_CONNS]; +}; + +struct spdk_jsonrpc_client_request { + /* Total space allocated for send_buf */ + size_t send_buf_size; + + /* Number of bytes used in send_buf (<= send_buf_size) */ + size_t send_len; + + size_t send_offset; + + uint8_t *send_buf; +}; + +struct spdk_jsonrpc_client_response_internal { + struct spdk_jsonrpc_client_response jsonrpc; + bool ready; + uint8_t *buf; + size_t values_cnt; + struct spdk_json_val values[]; +}; + +struct spdk_jsonrpc_client { + int sockfd; + bool connected; + + size_t recv_buf_size; + size_t recv_offset; + char *recv_buf; + + /* Parsed response */ + struct spdk_jsonrpc_client_response_internal *resp; + struct spdk_jsonrpc_client_request *request; +}; + +/* jsonrpc_server_tcp */ +void jsonrpc_server_handle_request(struct spdk_jsonrpc_request *request, + const struct spdk_json_val *method, + const struct spdk_json_val *params); +void jsonrpc_server_handle_error(struct spdk_jsonrpc_request *request, int error); + +/* Might be called from any thread */ +void jsonrpc_server_send_response(struct spdk_jsonrpc_request *request); + +/* jsonrpc_server */ +int jsonrpc_parse_request(struct spdk_jsonrpc_server_conn *conn, const void *json, + size_t size); + +/* Must be called only from server poll thread */ +void jsonrpc_free_request(struct spdk_jsonrpc_request *request); + +/* + * Parse JSON data as RPC command response. + * + * \param client structure pointer of jsonrpc client + * + * \return 0 On success. Negative error code in error + * -EAGAIN - If the provided data is not a complete JSON value (SPDK_JSON_PARSE_INCOMPLETE) + * -EINVAL - If the provided data has invalid JSON syntax and can't be parsed (SPDK_JSON_PARSE_INVALID). + * -ENOSPC - No space left to store parsed response. + */ +int jsonrpc_parse_response(struct spdk_jsonrpc_client *client); + +#endif diff --git a/src/spdk/lib/jsonrpc/jsonrpc_server.c b/src/spdk/lib/jsonrpc/jsonrpc_server.c new file mode 100644 index 000000000..774612b25 --- /dev/null +++ b/src/spdk/lib/jsonrpc/jsonrpc_server.c @@ -0,0 +1,361 @@ +/*- + * BSD LICENSE + * + * Copyright (c) Intel Corporation. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Intel Corporation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "jsonrpc_internal.h" + +#include "spdk/util.h" + +struct jsonrpc_request { + const struct spdk_json_val *version; + const struct spdk_json_val *method; + const struct spdk_json_val *params; + const struct spdk_json_val *id; +}; + +static int +capture_val(const struct spdk_json_val *val, void *out) +{ + const struct spdk_json_val **vptr = out; + + *vptr = val; + return 0; +} + +static const struct spdk_json_object_decoder jsonrpc_request_decoders[] = { + {"jsonrpc", offsetof(struct jsonrpc_request, version), capture_val, true}, + {"method", offsetof(struct jsonrpc_request, method), capture_val}, + {"params", offsetof(struct jsonrpc_request, params), capture_val, true}, + {"id", offsetof(struct jsonrpc_request, id), capture_val, true}, +}; + +static void +parse_single_request(struct spdk_jsonrpc_request *request, struct spdk_json_val *values) +{ + struct jsonrpc_request req = {}; + const struct spdk_json_val *params = NULL; + + if (spdk_json_decode_object(values, jsonrpc_request_decoders, + SPDK_COUNTOF(jsonrpc_request_decoders), + &req)) { + goto invalid; + } + + if (req.version && (req.version->type != SPDK_JSON_VAL_STRING || + !spdk_json_strequal(req.version, "2.0"))) { + goto invalid; + } + + if (!req.method || req.method->type != SPDK_JSON_VAL_STRING) { + goto invalid; + } + + if (req.id) { + if (req.id->type == SPDK_JSON_VAL_STRING || + req.id->type == SPDK_JSON_VAL_NUMBER || + req.id->type == SPDK_JSON_VAL_NULL) { + request->id = req.id; + } else { + goto invalid; + } + } + + if (req.params) { + /* null json value is as if there were no parameters */ + if (req.params->type != SPDK_JSON_VAL_NULL) { + if (req.params->type != SPDK_JSON_VAL_ARRAY_BEGIN && + req.params->type != SPDK_JSON_VAL_OBJECT_BEGIN) { + goto invalid; + } + params = req.params; + } + } + + jsonrpc_server_handle_request(request, req.method, params); + return; + +invalid: + jsonrpc_server_handle_error(request, SPDK_JSONRPC_ERROR_INVALID_REQUEST); +} + +static int +jsonrpc_server_write_cb(void *cb_ctx, const void *data, size_t size) +{ + struct spdk_jsonrpc_request *request = cb_ctx; + size_t new_size = request->send_buf_size; + + while (new_size - request->send_len < size) { + if (new_size >= SPDK_JSONRPC_SEND_BUF_SIZE_MAX) { + SPDK_ERRLOG("Send buf exceeded maximum size (%zu)\n", + (size_t)SPDK_JSONRPC_SEND_BUF_SIZE_MAX); + return -1; + } + + new_size *= 2; + } + + if (new_size != request->send_buf_size) { + uint8_t *new_buf; + + new_buf = realloc(request->send_buf, new_size); + if (new_buf == NULL) { + SPDK_ERRLOG("Resizing send_buf failed (current size %zu, new size %zu)\n", + request->send_buf_size, new_size); + return -1; + } + + request->send_buf = new_buf; + request->send_buf_size = new_size; + } + + memcpy(request->send_buf + request->send_len, data, size); + request->send_len += size; + + return 0; +} + +int +jsonrpc_parse_request(struct spdk_jsonrpc_server_conn *conn, const void *json, size_t size) +{ + struct spdk_jsonrpc_request *request; + ssize_t rc; + size_t len; + void *end = NULL; + + /* Check to see if we have received a full JSON value. It is safe to cast away const + * as we don't decode in place. */ + rc = spdk_json_parse((void *)json, size, NULL, 0, &end, 0); + if (rc == SPDK_JSON_PARSE_INCOMPLETE) { + return 0; + } + + request = calloc(1, sizeof(*request)); + if (request == NULL) { + SPDK_DEBUGLOG(SPDK_LOG_RPC, "Out of memory allocating request\n"); + return -1; + } + + conn->outstanding_requests++; + + request->conn = conn; + + len = end - json; + request->recv_buffer = malloc(len + 1); + if (request->recv_buffer == NULL) { + SPDK_ERRLOG("Failed to allocate buffer to copy request (%zu bytes)\n", len + 1); + jsonrpc_free_request(request); + return -1; + } + + memcpy(request->recv_buffer, json, len); + request->recv_buffer[len] = '\0'; + + if (rc > 0 && rc <= SPDK_JSONRPC_MAX_VALUES) { + request->values_cnt = rc; + request->values = malloc(request->values_cnt * sizeof(request->values[0])); + if (request->values == NULL) { + SPDK_ERRLOG("Failed to allocate buffer for JSON values (%zu bytes)\n", + request->values_cnt * sizeof(request->values[0])); + jsonrpc_free_request(request); + return -1; + } + } + + request->send_offset = 0; + request->send_len = 0; + request->send_buf_size = SPDK_JSONRPC_SEND_BUF_SIZE_INIT; + request->send_buf = malloc(request->send_buf_size); + if (request->send_buf == NULL) { + SPDK_ERRLOG("Failed to allocate send_buf (%zu bytes)\n", request->send_buf_size); + jsonrpc_free_request(request); + return -1; + } + + request->response = spdk_json_write_begin(jsonrpc_server_write_cb, request, 0); + if (request->response == NULL) { + SPDK_ERRLOG("Failed to allocate response JSON write context.\n"); + jsonrpc_free_request(request); + return -1; + } + + if (rc <= 0 || rc > SPDK_JSONRPC_MAX_VALUES) { + SPDK_DEBUGLOG(SPDK_LOG_RPC, "JSON parse error\n"); + jsonrpc_server_handle_error(request, SPDK_JSONRPC_ERROR_PARSE_ERROR); + + /* + * Can't recover from parse error (no guaranteed resync point in streaming JSON). + * Return an error to indicate that the connection should be closed. + */ + return -1; + } + + /* Decode a second time now that there is a full JSON value available. */ + rc = spdk_json_parse(request->recv_buffer, size, request->values, request->values_cnt, &end, + SPDK_JSON_PARSE_FLAG_DECODE_IN_PLACE); + if (rc < 0 || rc > SPDK_JSONRPC_MAX_VALUES) { + SPDK_DEBUGLOG(SPDK_LOG_RPC, "JSON parse error on second pass\n"); + jsonrpc_server_handle_error(request, SPDK_JSONRPC_ERROR_PARSE_ERROR); + return -1; + } + + assert(end != NULL); + + if (request->values[0].type == SPDK_JSON_VAL_OBJECT_BEGIN) { + parse_single_request(request, request->values); + } else if (request->values[0].type == SPDK_JSON_VAL_ARRAY_BEGIN) { + SPDK_DEBUGLOG(SPDK_LOG_RPC, "Got batch array (not currently supported)\n"); + jsonrpc_server_handle_error(request, SPDK_JSONRPC_ERROR_INVALID_REQUEST); + } else { + SPDK_DEBUGLOG(SPDK_LOG_RPC, "top-level JSON value was not array or object\n"); + jsonrpc_server_handle_error(request, SPDK_JSONRPC_ERROR_INVALID_REQUEST); + } + + return len; +} + +struct spdk_jsonrpc_server_conn * +spdk_jsonrpc_get_conn(struct spdk_jsonrpc_request *request) +{ + return request->conn; +} + +/* Never return NULL */ +static struct spdk_json_write_ctx * +begin_response(struct spdk_jsonrpc_request *request) +{ + struct spdk_json_write_ctx *w = request->response; + + spdk_json_write_object_begin(w); + spdk_json_write_named_string(w, "jsonrpc", "2.0"); + + spdk_json_write_name(w, "id"); + if (request->id) { + spdk_json_write_val(w, request->id); + } else { + spdk_json_write_null(w); + } + + return w; +} + +static void +skip_response(struct spdk_jsonrpc_request *request) +{ + request->send_len = 0; + spdk_json_write_end(request->response); + request->response = NULL; + jsonrpc_server_send_response(request); +} + +static void +end_response(struct spdk_jsonrpc_request *request) +{ + spdk_json_write_object_end(request->response); + spdk_json_write_end(request->response); + request->response = NULL; + + jsonrpc_server_write_cb(request, "\n", 1); + jsonrpc_server_send_response(request); +} + +void +jsonrpc_free_request(struct spdk_jsonrpc_request *request) +{ + if (!request) { + return; + } + + /* We must send or skip response explicitly */ + assert(request->response == NULL); + + request->conn->outstanding_requests--; + free(request->recv_buffer); + free(request->values); + free(request->send_buf); + free(request); +} + +struct spdk_json_write_ctx * +spdk_jsonrpc_begin_result(struct spdk_jsonrpc_request *request) +{ + struct spdk_json_write_ctx *w = begin_response(request); + + spdk_json_write_name(w, "result"); + return w; +} + +void +spdk_jsonrpc_end_result(struct spdk_jsonrpc_request *request, struct spdk_json_write_ctx *w) +{ + assert(w != NULL); + assert(w == request->response); + + /* If there was no ID in request we skip response. */ + if (request->id && request->id->type != SPDK_JSON_VAL_NULL) { + end_response(request); + } else { + skip_response(request); + } +} + +void +spdk_jsonrpc_send_error_response(struct spdk_jsonrpc_request *request, + int error_code, const char *msg) +{ + struct spdk_json_write_ctx *w = begin_response(request); + + spdk_json_write_named_object_begin(w, "error"); + spdk_json_write_named_int32(w, "code", error_code); + spdk_json_write_named_string(w, "message", msg); + spdk_json_write_object_end(w); + + end_response(request); +} + +void +spdk_jsonrpc_send_error_response_fmt(struct spdk_jsonrpc_request *request, + int error_code, const char *fmt, ...) +{ + struct spdk_json_write_ctx *w = begin_response(request); + va_list args; + + spdk_json_write_named_object_begin(w, "error"); + spdk_json_write_named_int32(w, "code", error_code); + va_start(args, fmt); + spdk_json_write_named_string_fmt_v(w, "message", fmt, args); + va_end(args); + spdk_json_write_object_end(w); + + end_response(request); +} + +SPDK_LOG_REGISTER_COMPONENT("rpc", SPDK_LOG_RPC) diff --git a/src/spdk/lib/jsonrpc/jsonrpc_server_tcp.c b/src/spdk/lib/jsonrpc/jsonrpc_server_tcp.c new file mode 100644 index 000000000..1e38f713f --- /dev/null +++ b/src/spdk/lib/jsonrpc/jsonrpc_server_tcp.c @@ -0,0 +1,441 @@ +/*- + * BSD LICENSE + * + * Copyright (c) Intel Corporation. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Intel Corporation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "jsonrpc_internal.h" +#include "spdk/string.h" +#include "spdk/util.h" + +struct spdk_jsonrpc_server * +spdk_jsonrpc_server_listen(int domain, int protocol, + struct sockaddr *listen_addr, socklen_t addrlen, + spdk_jsonrpc_handle_request_fn handle_request) +{ + struct spdk_jsonrpc_server *server; + int rc, val, flag, i; + + server = calloc(1, sizeof(struct spdk_jsonrpc_server)); + if (server == NULL) { + return NULL; + } + + TAILQ_INIT(&server->free_conns); + TAILQ_INIT(&server->conns); + + for (i = 0; i < SPDK_JSONRPC_MAX_CONNS; i++) { + TAILQ_INSERT_TAIL(&server->free_conns, &server->conns_array[i], link); + } + + server->handle_request = handle_request; + + server->sockfd = socket(domain, SOCK_STREAM, protocol); + if (server->sockfd < 0) { + SPDK_ERRLOG("socket() failed\n"); + free(server); + return NULL; + } + + val = 1; + setsockopt(server->sockfd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)); + + flag = fcntl(server->sockfd, F_GETFL); + if (fcntl(server->sockfd, F_SETFL, flag | O_NONBLOCK) < 0) { + SPDK_ERRLOG("fcntl can't set nonblocking mode for socket, fd: %d (%s)\n", + server->sockfd, spdk_strerror(errno)); + close(server->sockfd); + free(server); + return NULL; + } + + rc = bind(server->sockfd, listen_addr, addrlen); + if (rc != 0) { + SPDK_ERRLOG("could not bind JSON-RPC server: %s\n", spdk_strerror(errno)); + close(server->sockfd); + free(server); + return NULL; + } + + rc = listen(server->sockfd, 512); + if (rc != 0) { + SPDK_ERRLOG("listen() failed, errno = %d\n", errno); + close(server->sockfd); + free(server); + return NULL; + } + + return server; +} + +static struct spdk_jsonrpc_request * +jsonrpc_server_dequeue_request(struct spdk_jsonrpc_server_conn *conn) +{ + struct spdk_jsonrpc_request *request = NULL; + + pthread_spin_lock(&conn->queue_lock); + request = STAILQ_FIRST(&conn->send_queue); + if (request) { + STAILQ_REMOVE_HEAD(&conn->send_queue, link); + } + pthread_spin_unlock(&conn->queue_lock); + return request; +} + +static void +jsonrpc_server_free_conn_request(struct spdk_jsonrpc_server_conn *conn) +{ + struct spdk_jsonrpc_request *request; + + jsonrpc_free_request(conn->send_request); + conn->send_request = NULL ; + while ((request = jsonrpc_server_dequeue_request(conn)) != NULL) { + jsonrpc_free_request(request); + } +} + +static void +jsonrpc_server_conn_close(struct spdk_jsonrpc_server_conn *conn) +{ + conn->closed = true; + + if (conn->sockfd >= 0) { + jsonrpc_server_free_conn_request(conn); + close(conn->sockfd); + conn->sockfd = -1; + + if (conn->close_cb) { + conn->close_cb(conn, conn->close_cb_ctx); + } + } +} + +void +spdk_jsonrpc_server_shutdown(struct spdk_jsonrpc_server *server) +{ + struct spdk_jsonrpc_server_conn *conn; + + close(server->sockfd); + + TAILQ_FOREACH(conn, &server->conns, link) { + jsonrpc_server_conn_close(conn); + } + + free(server); +} + +static void +jsonrpc_server_conn_remove(struct spdk_jsonrpc_server_conn *conn) +{ + struct spdk_jsonrpc_server *server = conn->server; + + jsonrpc_server_conn_close(conn); + + pthread_spin_destroy(&conn->queue_lock); + assert(STAILQ_EMPTY(&conn->send_queue)); + + TAILQ_REMOVE(&server->conns, conn, link); + TAILQ_INSERT_HEAD(&server->free_conns, conn, link); +} + +int +spdk_jsonrpc_conn_add_close_cb(struct spdk_jsonrpc_server_conn *conn, + spdk_jsonrpc_conn_closed_fn cb, void *ctx) +{ + int rc = 0; + + pthread_spin_lock(&conn->queue_lock); + if (conn->close_cb == NULL) { + conn->close_cb = cb; + conn->close_cb_ctx = ctx; + } else { + rc = conn->close_cb == cb && conn->close_cb_ctx == ctx ? -EEXIST : -ENOSPC; + } + pthread_spin_unlock(&conn->queue_lock); + + return rc; +} + +int +spdk_jsonrpc_conn_del_close_cb(struct spdk_jsonrpc_server_conn *conn, + spdk_jsonrpc_conn_closed_fn cb, void *ctx) +{ + int rc = 0; + + pthread_spin_lock(&conn->queue_lock); + if (conn->close_cb == NULL || conn->close_cb != cb || conn->close_cb_ctx != ctx) { + rc = -ENOENT; + } else { + conn->close_cb = NULL; + } + pthread_spin_unlock(&conn->queue_lock); + + return rc; +} + +static int +jsonrpc_server_accept(struct spdk_jsonrpc_server *server) +{ + struct spdk_jsonrpc_server_conn *conn; + int rc, flag; + + rc = accept(server->sockfd, NULL, NULL); + if (rc >= 0) { + conn = TAILQ_FIRST(&server->free_conns); + assert(conn != NULL); + + conn->server = server; + conn->sockfd = rc; + conn->closed = false; + conn->recv_len = 0; + conn->outstanding_requests = 0; + STAILQ_INIT(&conn->send_queue); + conn->send_request = NULL; + + if (pthread_spin_init(&conn->queue_lock, PTHREAD_PROCESS_PRIVATE)) { + SPDK_ERRLOG("Unable to create queue lock for socket: %d", conn->sockfd); + close(conn->sockfd); + return -1; + } + + flag = fcntl(conn->sockfd, F_GETFL); + if (fcntl(conn->sockfd, F_SETFL, flag | O_NONBLOCK) < 0) { + SPDK_ERRLOG("fcntl can't set nonblocking mode for socket, fd: %d (%s)\n", + conn->sockfd, spdk_strerror(errno)); + close(conn->sockfd); + pthread_spin_destroy(&conn->queue_lock); + return -1; + } + + TAILQ_REMOVE(&server->free_conns, conn, link); + TAILQ_INSERT_TAIL(&server->conns, conn, link); + return 0; + } + + if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) { + return 0; + } + + return -1; +} + +void +jsonrpc_server_handle_request(struct spdk_jsonrpc_request *request, + const struct spdk_json_val *method, const struct spdk_json_val *params) +{ + request->conn->server->handle_request(request, method, params); +} + +void +jsonrpc_server_handle_error(struct spdk_jsonrpc_request *request, int error) +{ + const char *msg; + + switch (error) { + case SPDK_JSONRPC_ERROR_PARSE_ERROR: + msg = "Parse error"; + break; + + case SPDK_JSONRPC_ERROR_INVALID_REQUEST: + msg = "Invalid request"; + break; + + case SPDK_JSONRPC_ERROR_METHOD_NOT_FOUND: + msg = "Method not found"; + break; + + case SPDK_JSONRPC_ERROR_INVALID_PARAMS: + msg = "Invalid parameters"; + break; + + case SPDK_JSONRPC_ERROR_INTERNAL_ERROR: + msg = "Internal error"; + break; + + default: + msg = "Error"; + break; + } + + spdk_jsonrpc_send_error_response(request, error, msg); +} + +static int +jsonrpc_server_conn_recv(struct spdk_jsonrpc_server_conn *conn) +{ + ssize_t rc, offset; + size_t recv_avail = SPDK_JSONRPC_RECV_BUF_SIZE - conn->recv_len; + + rc = recv(conn->sockfd, conn->recv_buf + conn->recv_len, recv_avail, 0); + if (rc == -1) { + if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) { + return 0; + } + SPDK_DEBUGLOG(SPDK_LOG_RPC, "recv() failed: %s\n", spdk_strerror(errno)); + return -1; + } + + if (rc == 0) { + SPDK_DEBUGLOG(SPDK_LOG_RPC, "remote closed connection\n"); + conn->closed = true; + return 0; + } + + conn->recv_len += rc; + + offset = 0; + do { + rc = jsonrpc_parse_request(conn, conn->recv_buf + offset, conn->recv_len - offset); + if (rc < 0) { + SPDK_ERRLOG("jsonrpc parse request failed\n"); + return -1; + } + + offset += rc; + } while (rc > 0); + + if (offset > 0) { + /* + * Successfully parsed a requests - move any data past the end of the + * parsed requests down to the beginning. + */ + assert((size_t)offset <= conn->recv_len); + memmove(conn->recv_buf, conn->recv_buf + offset, conn->recv_len - offset); + conn->recv_len -= offset; + } + + return 0; +} + +void +jsonrpc_server_send_response(struct spdk_jsonrpc_request *request) +{ + struct spdk_jsonrpc_server_conn *conn = request->conn; + + /* Queue the response to be sent */ + pthread_spin_lock(&conn->queue_lock); + STAILQ_INSERT_TAIL(&conn->send_queue, request, link); + pthread_spin_unlock(&conn->queue_lock); +} + + +static int +jsonrpc_server_conn_send(struct spdk_jsonrpc_server_conn *conn) +{ + struct spdk_jsonrpc_request *request; + ssize_t rc; + +more: + if (conn->outstanding_requests == 0) { + return 0; + } + + if (conn->send_request == NULL) { + conn->send_request = jsonrpc_server_dequeue_request(conn); + } + + request = conn->send_request; + if (request == NULL) { + /* Nothing to send right now */ + return 0; + } + + if (request->send_len > 0) { + rc = send(conn->sockfd, request->send_buf + request->send_offset, + request->send_len, 0); + if (rc < 0) { + if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) { + return 0; + } + + SPDK_DEBUGLOG(SPDK_LOG_RPC, "send() failed: %s\n", spdk_strerror(errno)); + return -1; + } + + request->send_offset += rc; + request->send_len -= rc; + } + + if (request->send_len == 0) { + /* + * Full response has been sent. + * Free it and set send_request to NULL to move on to the next queued response. + */ + conn->send_request = NULL; + jsonrpc_free_request(request); + goto more; + } + + return 0; +} + +int +spdk_jsonrpc_server_poll(struct spdk_jsonrpc_server *server) +{ + int rc; + struct spdk_jsonrpc_server_conn *conn, *conn_tmp; + + TAILQ_FOREACH_SAFE(conn, &server->conns, link, conn_tmp) { + /* If we can't receive and there are no outstanding requests close the connection. */ + if (conn->closed == true && conn->outstanding_requests == 0) { + jsonrpc_server_conn_close(conn); + } + + if (conn->sockfd == -1 && conn->outstanding_requests == 0) { + jsonrpc_server_conn_remove(conn); + } + } + + /* Check listen socket */ + if (!TAILQ_EMPTY(&server->free_conns)) { + jsonrpc_server_accept(server); + } + + TAILQ_FOREACH(conn, &server->conns, link) { + if (conn->sockfd == -1) { + continue; + } + + rc = jsonrpc_server_conn_send(conn); + if (rc != 0) { + jsonrpc_server_conn_close(conn); + continue; + } + + if (!conn->closed) { + rc = jsonrpc_server_conn_recv(conn); + if (rc != 0) { + jsonrpc_server_conn_close(conn); + } + } + } + + return 0; +} diff --git a/src/spdk/lib/jsonrpc/spdk_jsonrpc.map b/src/spdk/lib/jsonrpc/spdk_jsonrpc.map new file mode 100644 index 000000000..461fd0766 --- /dev/null +++ b/src/spdk/lib/jsonrpc/spdk_jsonrpc.map @@ -0,0 +1,28 @@ +{ + global: + + # public functions + spdk_jsonrpc_server_listen; + spdk_jsonrpc_server_poll; + spdk_jsonrpc_server_shutdown; + spdk_jsonrpc_get_conn; + spdk_jsonrpc_conn_add_close_cb; + spdk_jsonrpc_conn_del_close_cb; + spdk_jsonrpc_begin_result; + spdk_jsonrpc_end_result; + spdk_jsonrpc_send_error_response; + spdk_jsonrpc_send_error_response_fmt; + spdk_jsonrpc_begin_request; + spdk_jsonrpc_end_request; + spdk_jsonrpc_client_connect; + spdk_jsonrpc_client_close; + spdk_jsonrpc_client_create_request; + spdk_jsonrpc_client_free_request; + spdk_jsonrpc_client_send_request; + spdk_jsonrpc_client_poll; + spdk_jsonrpc_client_get_response; + spdk_jsonrpc_client_free_response; + + + local: *; +}; |