Many open source projects have moved to using SPDX identifiers to specify license information, reducing the amount of boilerplate code in every source file. This patch replaces the bulk of SPDK .c, .cpp and Makefiles with the BSD-3-Clause identifier. Almost all of these files share the exact same license text, and this patch only modifies the files that contain the most common license text. There can be slight variations because the third clause contains company names - most say "Intel Corporation", but there are instances for Nvidia, Samsung, Eideticom and even "the copyright holder". Used a bash script to automate replacement of the license text with SPDX identifier which is checked into scripts/spdx.sh. Signed-off-by: Jim Harris <james.r.harris@intel.com> Change-Id: Iaa88ab5e92ea471691dc298cfe41ebfb5d169780 Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/12904 Community-CI: Broadcom CI <spdk-ci.pdl@broadcom.com> Community-CI: Mellanox Build Bot Tested-by: SPDK CI Jenkins <sys_sgci@intel.com> Reviewed-by: Aleksey Marchuk <alexeymar@nvidia.com> Reviewed-by: Changpeng Liu <changpeng.liu@intel.com> Reviewed-by: Dong Yi <dongx.yi@intel.com> Reviewed-by: Konrad Sztyber <konrad.sztyber@intel.com> Reviewed-by: Paul Luse <paul.e.luse@intel.com> Reviewed-by: <qun.wan@intel.com>
200 lines
4.6 KiB
C
200 lines
4.6 KiB
C
/* SPDX-License-Identifier: BSD-3-Clause
|
|
* Copyright (c) Intel Corporation.
|
|
* All rights reserved.
|
|
*/
|
|
|
|
#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(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)
|