2022-06-03 19:15:11 +00:00
|
|
|
/* SPDX-License-Identifier: BSD-3-Clause
|
2022-11-01 20:26:26 +00:00
|
|
|
* Copyright (C) 2018 Intel Corporation.
|
2018-08-14 16:53:07 +00:00
|
|
|
* All rights reserved.
|
2022-09-05 09:52:01 +00:00
|
|
|
* Copyright (c) 2022 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
|
2018-08-14 16:53:07 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "spdk/stdinc.h"
|
|
|
|
|
2021-03-01 21:06:04 +00:00
|
|
|
#include "spdk/init.h"
|
2018-08-14 16:53:07 +00:00
|
|
|
#include "spdk/util.h"
|
2019-05-07 06:35:01 +00:00
|
|
|
#include "spdk/file.h"
|
2018-08-14 16:53:07 +00:00
|
|
|
#include "spdk/log.h"
|
|
|
|
#include "spdk/env.h"
|
|
|
|
#include "spdk/thread.h"
|
|
|
|
#include "spdk/jsonrpc.h"
|
|
|
|
#include "spdk/rpc.h"
|
2021-10-28 21:38:04 +00:00
|
|
|
#include "spdk/string.h"
|
2018-08-14 16:53:07 +00:00
|
|
|
|
|
|
|
#include "spdk_internal/event.h"
|
|
|
|
|
2020-09-04 11:27:29 +00:00
|
|
|
#define SPDK_DEBUG_APP_CFG(...) SPDK_DEBUGLOG(app_config, __VA_ARGS__)
|
2018-08-14 16:53:07 +00:00
|
|
|
|
|
|
|
/* JSON configuration format is as follows
|
|
|
|
*
|
|
|
|
* {
|
|
|
|
* "subsystems" : [ <<== *subsystems JSON array
|
|
|
|
* { <<== *subsystems_it array entry pointer (iterator)
|
|
|
|
* "subsystem": "<< SUBSYSTEM NAME >>",
|
|
|
|
* "config": [ <<== *config JSON array
|
|
|
|
* { <<== *config_it array entry pointer (iterator)
|
|
|
|
* "method": "<< METHOD NAME >>", <<== *method
|
|
|
|
* "params": { << PARAMS >> } <<== *params
|
|
|
|
* },
|
|
|
|
* << MORE "config" ARRY ENTRIES >>
|
|
|
|
* ]
|
|
|
|
* },
|
|
|
|
* << MORE "subsystems" ARRAY ENTRIES >>
|
|
|
|
* ]
|
|
|
|
*
|
2021-11-25 01:40:58 +00:00
|
|
|
* << ANYTHING ELSE IS IGNORED IN ROOT OBJECT>>
|
2018-08-14 16:53:07 +00:00
|
|
|
* }
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct load_json_config_ctx;
|
|
|
|
typedef void (*client_resp_handler)(struct load_json_config_ctx *,
|
|
|
|
struct spdk_jsonrpc_client_response *);
|
|
|
|
|
2021-07-12 00:09:19 +00:00
|
|
|
#define RPC_SOCKET_PATH_MAX SPDK_SIZEOF_MEMBER(struct sockaddr_un, sun_path)
|
2018-08-14 16:53:07 +00:00
|
|
|
|
|
|
|
/* 1s connections timeout */
|
|
|
|
#define RPC_CLIENT_CONNECT_TIMEOUT_US (1U * 1000U * 1000U)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Currently there is no timeout in SPDK for any RPC command. This result that
|
|
|
|
* we can't put a hard limit during configuration load as it most likely randomly fail.
|
|
|
|
* So just print WARNLOG every 10s. */
|
|
|
|
#define RPC_CLIENT_REQUEST_TIMEOUT_US (10U * 1000 * 1000)
|
|
|
|
|
|
|
|
struct load_json_config_ctx {
|
|
|
|
/* Thread used during configuration. */
|
|
|
|
struct spdk_thread *thread;
|
2021-03-02 19:34:25 +00:00
|
|
|
spdk_subsystem_init_fn cb_fn;
|
2019-03-04 19:38:24 +00:00
|
|
|
void *cb_arg;
|
2020-02-06 12:38:46 +00:00
|
|
|
bool stop_on_error;
|
2018-08-14 16:53:07 +00:00
|
|
|
|
|
|
|
/* Current subsystem */
|
|
|
|
struct spdk_json_val *subsystems; /* "subsystems" array */
|
|
|
|
struct spdk_json_val *subsystems_it; /* current subsystem array position in "subsystems" array */
|
|
|
|
|
|
|
|
struct spdk_json_val *subsystem_name; /* current subsystem name */
|
|
|
|
|
|
|
|
/* Current "config" entry we are processing */
|
|
|
|
struct spdk_json_val *config; /* "config" array */
|
|
|
|
struct spdk_json_val *config_it; /* current config position in "config" array */
|
|
|
|
|
|
|
|
/* Current request id we are sending. */
|
|
|
|
uint32_t rpc_request_id;
|
|
|
|
|
|
|
|
/* Whole configuration file read and parsed. */
|
|
|
|
size_t json_data_size;
|
|
|
|
char *json_data;
|
|
|
|
|
|
|
|
size_t values_cnt;
|
|
|
|
struct spdk_json_val *values;
|
|
|
|
|
|
|
|
char rpc_socket_path_temp[RPC_SOCKET_PATH_MAX + 1];
|
|
|
|
|
|
|
|
struct spdk_jsonrpc_client *client_conn;
|
|
|
|
struct spdk_poller *client_conn_poller;
|
|
|
|
|
|
|
|
client_resp_handler client_resp_cb;
|
|
|
|
|
|
|
|
/* Timeout for current RPC client action. */
|
|
|
|
uint64_t timeout;
|
|
|
|
};
|
|
|
|
|
2020-05-10 23:06:38 +00:00
|
|
|
static void app_json_config_load_subsystem(void *_ctx);
|
2018-08-14 16:53:07 +00:00
|
|
|
|
|
|
|
static void
|
2020-05-10 23:06:38 +00:00
|
|
|
app_json_config_load_done(struct load_json_config_ctx *ctx, int rc)
|
2018-08-14 16:53:07 +00:00
|
|
|
{
|
|
|
|
spdk_poller_unregister(&ctx->client_conn_poller);
|
2019-06-18 05:09:35 +00:00
|
|
|
if (ctx->client_conn != NULL) {
|
|
|
|
spdk_jsonrpc_client_close(ctx->client_conn);
|
|
|
|
}
|
|
|
|
|
2018-08-14 16:53:07 +00:00
|
|
|
spdk_rpc_finish();
|
|
|
|
|
2019-08-07 11:42:32 +00:00
|
|
|
SPDK_DEBUG_APP_CFG("Config load finished with rc %d\n", rc);
|
|
|
|
ctx->cb_fn(rc, ctx->cb_arg);
|
2018-08-14 16:53:07 +00:00
|
|
|
|
|
|
|
free(ctx->json_data);
|
|
|
|
free(ctx->values);
|
|
|
|
free(ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
rpc_client_set_timeout(struct load_json_config_ctx *ctx, uint64_t timeout_us)
|
|
|
|
{
|
|
|
|
ctx->timeout = spdk_get_ticks() + timeout_us * spdk_get_ticks_hz() / (1000 * 1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
rpc_client_check_timeout(struct load_json_config_ctx *ctx)
|
|
|
|
{
|
|
|
|
if (ctx->timeout < spdk_get_ticks()) {
|
|
|
|
SPDK_WARNLOG("RPC client command timeout.\n");
|
|
|
|
return -ETIMEDOUT;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-06-19 11:36:01 +00:00
|
|
|
struct json_write_buf {
|
|
|
|
char data[1024];
|
|
|
|
unsigned cur_off;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int
|
|
|
|
json_write_stdout(void *cb_ctx, const void *data, size_t size)
|
|
|
|
{
|
|
|
|
struct json_write_buf *buf = cb_ctx;
|
|
|
|
size_t rc;
|
|
|
|
|
|
|
|
rc = snprintf(buf->data + buf->cur_off, sizeof(buf->data) - buf->cur_off,
|
|
|
|
"%s", (const char *)data);
|
|
|
|
if (rc > 0) {
|
|
|
|
buf->cur_off += rc;
|
|
|
|
}
|
|
|
|
return rc == size ? 0 : -1;
|
|
|
|
}
|
|
|
|
|
2018-08-14 16:53:07 +00:00
|
|
|
static int
|
|
|
|
rpc_client_poller(void *arg)
|
|
|
|
{
|
|
|
|
struct load_json_config_ctx *ctx = arg;
|
|
|
|
struct spdk_jsonrpc_client_response *resp;
|
|
|
|
client_resp_handler cb;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
assert(spdk_get_thread() == ctx->thread);
|
|
|
|
|
|
|
|
rc = spdk_jsonrpc_client_poll(ctx->client_conn, 0);
|
|
|
|
if (rc == 0) {
|
|
|
|
rc = rpc_client_check_timeout(ctx);
|
|
|
|
if (rc == -ETIMEDOUT) {
|
|
|
|
rpc_client_set_timeout(ctx, RPC_CLIENT_REQUEST_TIMEOUT_US);
|
|
|
|
rc = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rc == 0) {
|
|
|
|
/* No response yet */
|
2020-05-04 09:51:27 +00:00
|
|
|
return SPDK_POLLER_BUSY;
|
2018-08-14 16:53:07 +00:00
|
|
|
} else if (rc < 0) {
|
2020-05-10 23:06:38 +00:00
|
|
|
app_json_config_load_done(ctx, rc);
|
2020-05-04 09:51:27 +00:00
|
|
|
return SPDK_POLLER_BUSY;
|
2018-08-14 16:53:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
resp = spdk_jsonrpc_client_get_response(ctx->client_conn);
|
|
|
|
assert(resp);
|
|
|
|
|
|
|
|
if (resp->error) {
|
2020-06-19 11:36:01 +00:00
|
|
|
struct json_write_buf buf = {};
|
|
|
|
struct spdk_json_write_ctx *w = spdk_json_write_begin(json_write_stdout,
|
|
|
|
&buf, SPDK_JSON_PARSE_FLAG_DECODE_IN_PLACE);
|
|
|
|
|
|
|
|
if (w == NULL) {
|
|
|
|
SPDK_ERRLOG("error response: (?)\n");
|
|
|
|
} else {
|
|
|
|
spdk_json_write_val(w, resp->error);
|
|
|
|
spdk_json_write_end(w);
|
|
|
|
SPDK_ERRLOG("error response: \n%s\n", buf.data);
|
|
|
|
}
|
2020-02-06 12:38:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (resp->error && ctx->stop_on_error) {
|
2018-08-14 16:53:07 +00:00
|
|
|
spdk_jsonrpc_client_free_response(resp);
|
2020-05-10 23:06:38 +00:00
|
|
|
app_json_config_load_done(ctx, -EINVAL);
|
2018-08-14 16:53:07 +00:00
|
|
|
} else {
|
|
|
|
/* We have response so we must have callback for it. */
|
|
|
|
cb = ctx->client_resp_cb;
|
|
|
|
assert(cb != NULL);
|
|
|
|
|
|
|
|
/* Mark we are done with this handler. */
|
|
|
|
ctx->client_resp_cb = NULL;
|
|
|
|
cb(ctx, resp);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-04 09:51:27 +00:00
|
|
|
return SPDK_POLLER_BUSY;
|
2018-08-14 16:53:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
rpc_client_connect_poller(void *_ctx)
|
|
|
|
{
|
|
|
|
struct load_json_config_ctx *ctx = _ctx;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
rc = spdk_jsonrpc_client_poll(ctx->client_conn, 0);
|
|
|
|
if (rc != -ENOTCONN) {
|
|
|
|
/* We are connected. Start regular poller and issue first request */
|
|
|
|
spdk_poller_unregister(&ctx->client_conn_poller);
|
2020-04-14 06:49:46 +00:00
|
|
|
ctx->client_conn_poller = SPDK_POLLER_REGISTER(rpc_client_poller, ctx, 100);
|
2020-05-10 23:06:38 +00:00
|
|
|
app_json_config_load_subsystem(ctx);
|
2018-08-14 16:53:07 +00:00
|
|
|
} else {
|
|
|
|
rc = rpc_client_check_timeout(ctx);
|
|
|
|
if (rc) {
|
2020-05-10 23:06:38 +00:00
|
|
|
app_json_config_load_done(ctx, rc);
|
2018-08-14 16:53:07 +00:00
|
|
|
}
|
2020-05-04 09:51:27 +00:00
|
|
|
|
|
|
|
return SPDK_POLLER_IDLE;
|
2018-08-14 16:53:07 +00:00
|
|
|
}
|
|
|
|
|
2020-05-04 09:51:27 +00:00
|
|
|
return SPDK_POLLER_BUSY;
|
2018-08-14 16:53:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
client_send_request(struct load_json_config_ctx *ctx, struct spdk_jsonrpc_client_request *request,
|
|
|
|
client_resp_handler client_resp_cb)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
assert(spdk_get_thread() == ctx->thread);
|
|
|
|
|
|
|
|
ctx->client_resp_cb = client_resp_cb;
|
|
|
|
rpc_client_set_timeout(ctx, RPC_CLIENT_REQUEST_TIMEOUT_US);
|
|
|
|
rc = spdk_jsonrpc_client_send_request(ctx->client_conn, request);
|
|
|
|
|
|
|
|
if (rc) {
|
|
|
|
SPDK_DEBUG_APP_CFG("Sending request to client failed (%d)\n", rc);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
cap_string(const struct spdk_json_val *val, void *out)
|
|
|
|
{
|
|
|
|
const struct spdk_json_val **vptr = out;
|
|
|
|
|
|
|
|
if (val->type != SPDK_JSON_VAL_STRING) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
*vptr = val;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
cap_object(const struct spdk_json_val *val, void *out)
|
|
|
|
{
|
|
|
|
const struct spdk_json_val **vptr = out;
|
|
|
|
|
|
|
|
if (val->type != SPDK_JSON_VAL_OBJECT_BEGIN) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
*vptr = val;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
cap_array_or_null(const struct spdk_json_val *val, void *out)
|
|
|
|
{
|
|
|
|
const struct spdk_json_val **vptr = out;
|
|
|
|
|
|
|
|
if (val->type != SPDK_JSON_VAL_ARRAY_BEGIN && val->type != SPDK_JSON_VAL_NULL) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
*vptr = val;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct config_entry {
|
|
|
|
char *method;
|
|
|
|
struct spdk_json_val *params;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct spdk_json_object_decoder jsonrpc_cmd_decoders[] = {
|
|
|
|
{"method", offsetof(struct config_entry, method), spdk_json_decode_string},
|
|
|
|
{"params", offsetof(struct config_entry, params), cap_object, true}
|
|
|
|
};
|
|
|
|
|
2020-05-10 23:06:38 +00:00
|
|
|
static void app_json_config_load_subsystem_config_entry(void *_ctx);
|
2018-08-14 16:53:07 +00:00
|
|
|
|
|
|
|
static void
|
2020-05-10 23:06:38 +00:00
|
|
|
app_json_config_load_subsystem_config_entry_next(struct load_json_config_ctx *ctx,
|
2018-08-14 16:53:07 +00:00
|
|
|
struct spdk_jsonrpc_client_response *resp)
|
|
|
|
{
|
2020-02-06 12:38:46 +00:00
|
|
|
/* Don't care about the response */
|
2018-08-14 16:53:07 +00:00
|
|
|
spdk_jsonrpc_client_free_response(resp);
|
|
|
|
|
|
|
|
ctx->config_it = spdk_json_next(ctx->config_it);
|
2020-05-10 23:06:38 +00:00
|
|
|
app_json_config_load_subsystem_config_entry(ctx);
|
2018-08-14 16:53:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Load "config" entry */
|
|
|
|
static void
|
2020-05-10 23:06:38 +00:00
|
|
|
app_json_config_load_subsystem_config_entry(void *_ctx)
|
2018-08-14 16:53:07 +00:00
|
|
|
{
|
|
|
|
struct load_json_config_ctx *ctx = _ctx;
|
|
|
|
struct spdk_jsonrpc_client_request *rpc_request;
|
|
|
|
struct spdk_json_write_ctx *w;
|
|
|
|
struct config_entry cfg = {};
|
|
|
|
struct spdk_json_val *params_end;
|
2020-08-03 08:52:37 +00:00
|
|
|
size_t params_len = 0;
|
2022-09-05 09:52:01 +00:00
|
|
|
uint32_t state_mask = 0, cur_state_mask, startup_runtime = SPDK_RPC_STARTUP | SPDK_RPC_RUNTIME;
|
2018-08-14 16:53:07 +00:00
|
|
|
int rc;
|
|
|
|
|
|
|
|
if (ctx->config_it == NULL) {
|
|
|
|
SPDK_DEBUG_APP_CFG("Subsystem '%.*s': configuration done.\n", ctx->subsystem_name->len,
|
|
|
|
(char *)ctx->subsystem_name->start);
|
|
|
|
ctx->subsystems_it = spdk_json_next(ctx->subsystems_it);
|
2022-11-02 15:09:36 +00:00
|
|
|
/* Invoke later to avoid recurrence */
|
2020-05-10 23:06:38 +00:00
|
|
|
spdk_thread_send_msg(ctx->thread, app_json_config_load_subsystem, ctx);
|
2018-08-14 16:53:07 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (spdk_json_decode_object(ctx->config_it, jsonrpc_cmd_decoders,
|
|
|
|
SPDK_COUNTOF(jsonrpc_cmd_decoders), &cfg)) {
|
2020-07-08 17:10:19 +00:00
|
|
|
SPDK_ERRLOG("Failed to decode config entry\n");
|
2020-05-10 23:06:38 +00:00
|
|
|
app_json_config_load_done(ctx, -EINVAL);
|
2018-08-14 16:53:07 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2022-09-05 09:52:01 +00:00
|
|
|
rc = spdk_rpc_get_method_state_mask(cfg.method, &state_mask);
|
|
|
|
if (rc == -ENOENT) {
|
|
|
|
SPDK_ERRLOG("Method '%s' was not found\n", cfg.method);
|
|
|
|
app_json_config_load_done(ctx, rc);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
cur_state_mask = spdk_rpc_get_state();
|
|
|
|
if ((state_mask & cur_state_mask) != cur_state_mask) {
|
2018-08-14 16:53:07 +00:00
|
|
|
SPDK_DEBUG_APP_CFG("Method '%s' not allowed -> skipping\n", cfg.method);
|
2022-11-02 15:09:36 +00:00
|
|
|
/* Invoke later to avoid recurrence */
|
2018-08-14 16:53:07 +00:00
|
|
|
ctx->config_it = spdk_json_next(ctx->config_it);
|
2020-05-10 23:06:38 +00:00
|
|
|
spdk_thread_send_msg(ctx->thread, app_json_config_load_subsystem_config_entry, ctx);
|
2018-08-14 16:53:07 +00:00
|
|
|
goto out;
|
|
|
|
}
|
2022-09-05 09:52:01 +00:00
|
|
|
if ((state_mask & startup_runtime) == startup_runtime && cur_state_mask == SPDK_RPC_RUNTIME) {
|
|
|
|
/* Some methods are allowed to be run in both STARTUP and RUNTIME states.
|
|
|
|
* We should not call such methods twice, so ignore the second attempt in RUNTIME state */
|
|
|
|
SPDK_DEBUG_APP_CFG("Method '%s' has already been run in STARTUP state\n", cfg.method);
|
2022-11-02 15:09:36 +00:00
|
|
|
/* Invoke later to avoid recurrence */
|
2022-09-05 09:52:01 +00:00
|
|
|
ctx->config_it = spdk_json_next(ctx->config_it);
|
|
|
|
spdk_thread_send_msg(ctx->thread, app_json_config_load_subsystem_config_entry, ctx);
|
|
|
|
goto out;
|
|
|
|
}
|
2018-08-14 16:53:07 +00:00
|
|
|
|
2020-07-08 16:16:12 +00:00
|
|
|
SPDK_DEBUG_APP_CFG("\tmethod: %s\n", cfg.method);
|
2018-08-14 16:53:07 +00:00
|
|
|
|
2020-07-08 16:16:12 +00:00
|
|
|
if (cfg.params) {
|
|
|
|
/* Get _END by skipping params and going back by one element. */
|
|
|
|
params_end = cfg.params + spdk_json_val_len(cfg.params) - 1;
|
2018-08-14 16:53:07 +00:00
|
|
|
|
2020-07-08 16:16:12 +00:00
|
|
|
/* Need to add one character to include '}' */
|
|
|
|
params_len = params_end->start - cfg.params->start + 1;
|
|
|
|
|
|
|
|
SPDK_DEBUG_APP_CFG("\tparams: %.*s\n", (int)params_len, (char *)cfg.params->start);
|
|
|
|
}
|
2018-08-14 16:53:07 +00:00
|
|
|
|
|
|
|
rpc_request = spdk_jsonrpc_client_create_request();
|
|
|
|
if (!rpc_request) {
|
2020-05-10 23:06:38 +00:00
|
|
|
app_json_config_load_done(ctx, -errno);
|
2018-08-14 16:53:07 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
w = spdk_jsonrpc_begin_request(rpc_request, ctx->rpc_request_id, NULL);
|
|
|
|
if (!w) {
|
|
|
|
spdk_jsonrpc_client_free_request(rpc_request);
|
2020-05-10 23:06:38 +00:00
|
|
|
app_json_config_load_done(ctx, -ENOMEM);
|
2018-08-14 16:53:07 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
spdk_json_write_named_string(w, "method", cfg.method);
|
|
|
|
|
2020-07-08 16:16:12 +00:00
|
|
|
if (cfg.params) {
|
|
|
|
/* No need to parse "params". Just dump the whole content of "params"
|
|
|
|
* directly into the request and let the remote side verify it. */
|
|
|
|
spdk_json_write_name(w, "params");
|
|
|
|
spdk_json_write_val_raw(w, cfg.params->start, params_len);
|
|
|
|
}
|
|
|
|
|
2018-08-14 16:53:07 +00:00
|
|
|
spdk_jsonrpc_end_request(rpc_request, w);
|
|
|
|
|
2020-05-10 23:06:38 +00:00
|
|
|
rc = client_send_request(ctx, rpc_request, app_json_config_load_subsystem_config_entry_next);
|
2018-08-14 16:53:07 +00:00
|
|
|
if (rc != 0) {
|
2020-05-10 23:06:38 +00:00
|
|
|
app_json_config_load_done(ctx, -rc);
|
2018-08-14 16:53:07 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
out:
|
|
|
|
free(cfg.method);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2019-08-02 11:29:09 +00:00
|
|
|
subsystem_init_done(int rc, void *arg1)
|
2018-08-14 16:53:07 +00:00
|
|
|
{
|
2019-08-02 11:29:09 +00:00
|
|
|
struct load_json_config_ctx *ctx = arg1;
|
|
|
|
|
|
|
|
if (rc) {
|
2020-05-10 23:06:38 +00:00
|
|
|
app_json_config_load_done(ctx, rc);
|
2019-08-02 11:29:09 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-08-14 16:53:07 +00:00
|
|
|
|
2019-08-02 11:29:09 +00:00
|
|
|
spdk_rpc_set_state(SPDK_RPC_RUNTIME);
|
2018-08-14 16:53:07 +00:00
|
|
|
/* Another round. This time for RUNTIME methods */
|
2019-09-11 13:30:14 +00:00
|
|
|
SPDK_DEBUG_APP_CFG("'framework_start_init' done - continuing configuration\n");
|
2019-08-02 11:29:09 +00:00
|
|
|
|
|
|
|
assert(ctx != NULL);
|
2018-08-14 16:53:07 +00:00
|
|
|
if (ctx->subsystems) {
|
|
|
|
ctx->subsystems_it = spdk_json_array_first(ctx->subsystems);
|
|
|
|
}
|
|
|
|
|
2020-05-10 23:06:38 +00:00
|
|
|
app_json_config_load_subsystem(ctx);
|
2018-08-14 16:53:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct spdk_json_object_decoder subsystem_decoders[] = {
|
|
|
|
{"subsystem", offsetof(struct load_json_config_ctx, subsystem_name), cap_string},
|
|
|
|
{"config", offsetof(struct load_json_config_ctx, config), cap_array_or_null}
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Start loading subsystem pointed by ctx->subsystems_it. This must point to the
|
|
|
|
* beginning of the "subsystem" object in "subsystems" array or be NULL. If it is
|
|
|
|
* NULL then no more subsystems to load.
|
|
|
|
*
|
|
|
|
* There are two iterations:
|
|
|
|
*
|
|
|
|
* In first iteration only STARTUP RPC methods are used, other methods are ignored. When
|
2019-09-11 13:30:14 +00:00
|
|
|
* allsubsystems are walked the ctx->subsystems_it became NULL and "framework_start_init"
|
2018-08-14 16:53:07 +00:00
|
|
|
* is called to let the SPDK move to RUNTIME state (initialize all subsystems) and
|
|
|
|
* second iteration begins.
|
|
|
|
*
|
|
|
|
* In second iteration "subsystems" array is walked through again, this time only
|
|
|
|
* RUNTIME RPC methods are used. When ctx->subsystems_it became NULL second time it
|
2019-03-04 19:38:24 +00:00
|
|
|
* indicate that there is no more subsystems to load. The cb_fn is called to finish
|
2018-08-14 16:53:07 +00:00
|
|
|
* configuration.
|
|
|
|
*/
|
|
|
|
static void
|
2020-05-10 23:06:38 +00:00
|
|
|
app_json_config_load_subsystem(void *_ctx)
|
2018-08-14 16:53:07 +00:00
|
|
|
{
|
|
|
|
struct load_json_config_ctx *ctx = _ctx;
|
|
|
|
|
|
|
|
if (ctx->subsystems_it == NULL) {
|
|
|
|
if (spdk_rpc_get_state() == SPDK_RPC_STARTUP) {
|
2019-09-11 13:30:14 +00:00
|
|
|
SPDK_DEBUG_APP_CFG("No more entries for current state, calling 'framework_start_init'\n");
|
2019-08-02 11:29:09 +00:00
|
|
|
spdk_subsystem_init(subsystem_init_done, ctx);
|
2018-08-14 16:53:07 +00:00
|
|
|
} else {
|
2020-05-10 23:06:38 +00:00
|
|
|
app_json_config_load_done(ctx, 0);
|
2018-08-14 16:53:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Capture subsystem name and config array */
|
|
|
|
if (spdk_json_decode_object(ctx->subsystems_it, subsystem_decoders,
|
|
|
|
SPDK_COUNTOF(subsystem_decoders), ctx)) {
|
|
|
|
SPDK_ERRLOG("Failed to parse subsystem configuration\n");
|
2020-05-10 23:06:38 +00:00
|
|
|
app_json_config_load_done(ctx, -EINVAL);
|
2018-08-14 16:53:07 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
SPDK_DEBUG_APP_CFG("Loading subsystem '%.*s' configuration\n", ctx->subsystem_name->len,
|
|
|
|
(char *)ctx->subsystem_name->start);
|
|
|
|
|
|
|
|
/* Get 'config' array first configuration entry */
|
|
|
|
ctx->config_it = spdk_json_array_first(ctx->config);
|
2020-05-10 23:06:38 +00:00
|
|
|
app_json_config_load_subsystem_config_entry(ctx);
|
2018-08-14 16:53:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void *
|
|
|
|
read_file(const char *filename, size_t *size)
|
|
|
|
{
|
|
|
|
FILE *file = fopen(filename, "r");
|
2019-05-07 06:35:01 +00:00
|
|
|
void *data;
|
2018-08-14 16:53:07 +00:00
|
|
|
|
|
|
|
if (file == NULL) {
|
|
|
|
/* errno is set by fopen */
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2019-05-07 06:35:01 +00:00
|
|
|
data = spdk_posix_file_load(file, size);
|
2018-08-14 16:53:07 +00:00
|
|
|
fclose(file);
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2020-05-10 23:06:38 +00:00
|
|
|
app_json_config_read(const char *config_file, struct load_json_config_ctx *ctx)
|
2018-08-14 16:53:07 +00:00
|
|
|
{
|
|
|
|
struct spdk_json_val *values = NULL;
|
|
|
|
void *json = NULL, *end;
|
|
|
|
ssize_t values_cnt, rc;
|
|
|
|
size_t json_size;
|
|
|
|
|
|
|
|
json = read_file(config_file, &json_size);
|
|
|
|
if (!json) {
|
2021-10-28 21:38:04 +00:00
|
|
|
SPDK_ERRLOG("Read JSON configuration file %s failed: %s\n",
|
|
|
|
config_file, spdk_strerror(errno));
|
2018-08-14 16:53:07 +00:00
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = spdk_json_parse(json, json_size, NULL, 0, &end,
|
|
|
|
SPDK_JSON_PARSE_FLAG_ALLOW_COMMENTS);
|
|
|
|
if (rc < 0) {
|
2020-04-18 12:12:43 +00:00
|
|
|
SPDK_ERRLOG("Parsing JSON configuration failed (%zd)\n", rc);
|
2018-08-14 16:53:07 +00:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
values_cnt = rc;
|
|
|
|
values = calloc(values_cnt, sizeof(struct spdk_json_val));
|
|
|
|
if (values == NULL) {
|
2020-04-18 12:12:43 +00:00
|
|
|
SPDK_ERRLOG("Out of memory\n");
|
2018-08-14 16:53:07 +00:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = spdk_json_parse(json, json_size, values, values_cnt, &end,
|
|
|
|
SPDK_JSON_PARSE_FLAG_ALLOW_COMMENTS);
|
|
|
|
if (rc != values_cnt) {
|
2020-04-18 12:12:43 +00:00
|
|
|
SPDK_ERRLOG("Parsing JSON configuration failed (%zd)\n", rc);
|
2018-08-14 16:53:07 +00:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx->json_data = json;
|
|
|
|
ctx->json_data_size = json_size;
|
|
|
|
|
|
|
|
ctx->values = values;
|
|
|
|
ctx->values_cnt = values_cnt;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
err:
|
|
|
|
free(json);
|
|
|
|
free(values);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2021-03-02 19:34:25 +00:00
|
|
|
spdk_subsystem_init_from_json_config(const char *json_config_file, const char *rpc_addr,
|
|
|
|
spdk_subsystem_init_fn cb_fn, void *cb_arg,
|
|
|
|
bool stop_on_error)
|
2018-08-14 16:53:07 +00:00
|
|
|
{
|
|
|
|
struct load_json_config_ctx *ctx = calloc(1, sizeof(*ctx));
|
|
|
|
int rc;
|
|
|
|
|
2019-03-04 19:38:24 +00:00
|
|
|
assert(cb_fn);
|
2018-08-14 16:53:07 +00:00
|
|
|
if (!ctx) {
|
2019-08-07 11:42:32 +00:00
|
|
|
cb_fn(-ENOMEM, cb_arg);
|
2018-08-14 16:53:07 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-03-04 19:38:24 +00:00
|
|
|
ctx->cb_fn = cb_fn;
|
|
|
|
ctx->cb_arg = cb_arg;
|
2020-02-06 12:38:46 +00:00
|
|
|
ctx->stop_on_error = stop_on_error;
|
2018-08-14 16:53:07 +00:00
|
|
|
ctx->thread = spdk_get_thread();
|
|
|
|
|
2020-05-10 23:06:38 +00:00
|
|
|
rc = app_json_config_read(json_config_file, ctx);
|
2018-08-14 16:53:07 +00:00
|
|
|
if (rc) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Capture subsystems array */
|
|
|
|
rc = spdk_json_find_array(ctx->values, "subsystems", NULL, &ctx->subsystems);
|
2022-08-29 10:16:42 +00:00
|
|
|
switch (rc) {
|
|
|
|
case 0:
|
2018-08-14 16:53:07 +00:00
|
|
|
/* Get first subsystem */
|
|
|
|
ctx->subsystems_it = spdk_json_array_first(ctx->subsystems);
|
|
|
|
if (ctx->subsystems_it == NULL) {
|
|
|
|
SPDK_NOTICELOG("'subsystems' configuration is empty\n");
|
|
|
|
}
|
2022-08-29 10:16:42 +00:00
|
|
|
break;
|
|
|
|
case -EPROTOTYPE:
|
|
|
|
SPDK_ERRLOG("Invalid JSON configuration: not enclosed in {}.\n");
|
|
|
|
goto fail;
|
|
|
|
case -ENOENT:
|
|
|
|
SPDK_WARNLOG("No 'subsystems' key JSON configuration file.\n");
|
|
|
|
break;
|
|
|
|
case -EDOM:
|
|
|
|
SPDK_ERRLOG("Invalid JSON configuration: 'subsystems' should be an array.\n");
|
|
|
|
goto fail;
|
|
|
|
default:
|
|
|
|
SPDK_ERRLOG("Failed to parse JSON configuration.\n");
|
|
|
|
goto fail;
|
2018-08-14 16:53:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* If rpc_addr is not an Unix socket use default address as prefix. */
|
|
|
|
if (rpc_addr == NULL || rpc_addr[0] != '/') {
|
|
|
|
rpc_addr = SPDK_DEFAULT_RPC_ADDR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* FIXME: rpc client should use socketpair() instead of this temporary socket nonsense */
|
|
|
|
rc = snprintf(ctx->rpc_socket_path_temp, sizeof(ctx->rpc_socket_path_temp), "%s.%d_config",
|
|
|
|
rpc_addr, getpid());
|
|
|
|
if (rc >= (int)sizeof(ctx->rpc_socket_path_temp)) {
|
|
|
|
SPDK_ERRLOG("Socket name create failed\n");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2021-03-02 19:45:59 +00:00
|
|
|
rc = spdk_rpc_initialize(ctx->rpc_socket_path_temp);
|
|
|
|
if (rc) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2018-08-14 16:53:07 +00:00
|
|
|
ctx->client_conn = spdk_jsonrpc_client_connect(ctx->rpc_socket_path_temp, AF_UNIX);
|
|
|
|
if (ctx->client_conn == NULL) {
|
|
|
|
SPDK_ERRLOG("Failed to connect to '%s'\n", ctx->rpc_socket_path_temp);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
rpc_client_set_timeout(ctx, RPC_CLIENT_CONNECT_TIMEOUT_US);
|
2020-04-14 06:49:46 +00:00
|
|
|
ctx->client_conn_poller = SPDK_POLLER_REGISTER(rpc_client_connect_poller, ctx, 100);
|
2018-08-14 16:53:07 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
fail:
|
2020-05-10 23:06:38 +00:00
|
|
|
app_json_config_load_done(ctx, -EINVAL);
|
2018-08-14 16:53:07 +00:00
|
|
|
}
|
|
|
|
|
2020-09-04 11:27:29 +00:00
|
|
|
SPDK_LOG_REGISTER_COMPONENT(app_config)
|