LCOV - code coverage report
Current view: top level - lib/init - json_config.c (source / functions) Hit Total Coverage
Test: ut_cov_unit.info Lines: 0 269 0.0 %
Date: 2024-07-14 09:59:02 Functions: 0 18 0.0 %

          Line data    Source code
       1             : /*   SPDX-License-Identifier: BSD-3-Clause
       2             :  *   Copyright (C) 2018 Intel Corporation.
       3             :  *   All rights reserved.
       4             :  *   Copyright (c) 2022, 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
       5             :  */
       6             : 
       7             : #include "spdk/stdinc.h"
       8             : 
       9             : #include "spdk/init.h"
      10             : #include "spdk/util.h"
      11             : #include "spdk/file.h"
      12             : #include "spdk/log.h"
      13             : #include "spdk/env.h"
      14             : #include "spdk/thread.h"
      15             : #include "spdk/jsonrpc.h"
      16             : #include "spdk/rpc.h"
      17             : #include "spdk/string.h"
      18             : 
      19             : #include "spdk_internal/event.h"
      20             : 
      21             : #define SPDK_DEBUG_APP_CFG(...) SPDK_DEBUGLOG(app_config, __VA_ARGS__)
      22             : 
      23             : /* JSON configuration format is as follows
      24             :  *
      25             :  * {
      26             :  *  "subsystems" : [                          <<== *subsystems JSON array
      27             :  *    {                                       <<== *subsystems_it array entry pointer (iterator)
      28             :  *      "subsystem": "<< SUBSYSTEM NAME >>",
      29             :  *      "config": [                           <<== *config JSON array
      30             :  *         {                                  <<== *config_it array entry pointer (iterator)
      31             :  *           "method": "<< METHOD NAME >>",   <<== *method
      32             :  *           "params": { << PARAMS >> }       <<== *params
      33             :  *         },
      34             :  *         << MORE "config" ARRY ENTRIES >>
      35             :  *      ]
      36             :  *    },
      37             :  *    << MORE "subsystems" ARRAY ENTRIES >>
      38             :  *  ]
      39             :  *
      40             :  *  << ANYTHING ELSE IS IGNORED IN ROOT OBJECT>>
      41             :  * }
      42             :  *
      43             :  */
      44             : 
      45             : struct load_json_config_ctx;
      46             : typedef void (*client_resp_handler)(struct load_json_config_ctx *,
      47             :                                     struct spdk_jsonrpc_client_response *);
      48             : 
      49             : #define RPC_SOCKET_PATH_MAX SPDK_SIZEOF_MEMBER(struct sockaddr_un, sun_path)
      50             : 
      51             : /* 1s connections timeout */
      52             : #define RPC_CLIENT_CONNECT_TIMEOUT_US (1U * 1000U * 1000U)
      53             : 
      54             : /*
      55             :  * Currently there is no timeout in SPDK for any RPC command. This result that
      56             :  * we can't put a hard limit during configuration load as it most likely randomly fail.
      57             :  * So just print WARNLOG every 10s. */
      58             : #define RPC_CLIENT_REQUEST_TIMEOUT_US (10U * 1000 * 1000)
      59             : 
      60             : struct load_json_config_ctx {
      61             :         /* Thread used during configuration. */
      62             :         struct spdk_thread *thread;
      63             :         spdk_subsystem_init_fn cb_fn;
      64             :         void *cb_arg;
      65             :         bool stop_on_error;
      66             : 
      67             :         /* Current subsystem */
      68             :         struct spdk_json_val *subsystems; /* "subsystems" array */
      69             :         struct spdk_json_val *subsystems_it; /* current subsystem array position in "subsystems" array */
      70             : 
      71             :         struct spdk_json_val *subsystem_name; /* current subsystem name */
      72             : 
      73             :         /* Current "config" entry we are processing */
      74             :         struct spdk_json_val *config; /* "config" array */
      75             :         struct spdk_json_val *config_it; /* current config position in "config" array */
      76             : 
      77             :         /* Current request id we are sending. */
      78             :         uint32_t rpc_request_id;
      79             : 
      80             :         /* Whole configuration file read and parsed. */
      81             :         size_t json_data_size;
      82             :         char *json_data;
      83             : 
      84             :         size_t values_cnt;
      85             :         struct spdk_json_val *values;
      86             : 
      87             :         char rpc_socket_path_temp[RPC_SOCKET_PATH_MAX + 1];
      88             : 
      89             :         struct spdk_jsonrpc_client *client_conn;
      90             :         struct spdk_poller *client_conn_poller;
      91             : 
      92             :         client_resp_handler client_resp_cb;
      93             : 
      94             :         /* Timeout for current RPC client action. */
      95             :         uint64_t timeout;
      96             : };
      97             : 
      98             : static void app_json_config_load_subsystem(void *_ctx);
      99             : 
     100             : static void
     101           0 : app_json_config_load_done(struct load_json_config_ctx *ctx, int rc)
     102             : {
     103           0 :         spdk_poller_unregister(&ctx->client_conn_poller);
     104           0 :         if (ctx->client_conn != NULL) {
     105           0 :                 spdk_jsonrpc_client_close(ctx->client_conn);
     106             :         }
     107             : 
     108           0 :         spdk_rpc_finish();
     109             : 
     110           0 :         SPDK_DEBUG_APP_CFG("Config load finished with rc %d\n", rc);
     111           0 :         ctx->cb_fn(rc, ctx->cb_arg);
     112             : 
     113           0 :         free(ctx->json_data);
     114           0 :         free(ctx->values);
     115           0 :         free(ctx);
     116           0 : }
     117             : 
     118             : static void
     119           0 : rpc_client_set_timeout(struct load_json_config_ctx *ctx, uint64_t timeout_us)
     120             : {
     121           0 :         ctx->timeout = spdk_get_ticks() + timeout_us * spdk_get_ticks_hz() / (1000 * 1000);
     122           0 : }
     123             : 
     124             : static int
     125           0 : rpc_client_check_timeout(struct load_json_config_ctx *ctx)
     126             : {
     127           0 :         if (ctx->timeout < spdk_get_ticks()) {
     128           0 :                 SPDK_WARNLOG("RPC client command timeout.\n");
     129           0 :                 return -ETIMEDOUT;
     130             :         }
     131             : 
     132           0 :         return 0;
     133             : }
     134             : 
     135             : struct json_write_buf {
     136             :         char data[1024];
     137             :         unsigned cur_off;
     138             : };
     139             : 
     140             : static int
     141           0 : json_write_stdout(void *cb_ctx, const void *data, size_t size)
     142             : {
     143           0 :         struct json_write_buf *buf = cb_ctx;
     144             :         size_t rc;
     145             : 
     146           0 :         rc = snprintf(buf->data + buf->cur_off, sizeof(buf->data) - buf->cur_off,
     147             :                       "%s", (const char *)data);
     148           0 :         if (rc > 0) {
     149           0 :                 buf->cur_off += rc;
     150             :         }
     151           0 :         return rc == size ? 0 : -1;
     152             : }
     153             : 
     154             : static int
     155           0 : rpc_client_poller(void *arg)
     156             : {
     157           0 :         struct load_json_config_ctx *ctx = arg;
     158             :         struct spdk_jsonrpc_client_response *resp;
     159             :         client_resp_handler cb;
     160             :         int rc;
     161             : 
     162           0 :         assert(spdk_get_thread() == ctx->thread);
     163             : 
     164           0 :         rc = spdk_jsonrpc_client_poll(ctx->client_conn, 0);
     165           0 :         if (rc == 0) {
     166           0 :                 rc = rpc_client_check_timeout(ctx);
     167           0 :                 if (rc == -ETIMEDOUT) {
     168           0 :                         rpc_client_set_timeout(ctx, RPC_CLIENT_REQUEST_TIMEOUT_US);
     169           0 :                         rc = 0;
     170             :                 }
     171             :         }
     172             : 
     173           0 :         if (rc == 0) {
     174             :                 /* No response yet */
     175           0 :                 return SPDK_POLLER_BUSY;
     176           0 :         } else if (rc < 0) {
     177           0 :                 app_json_config_load_done(ctx, rc);
     178           0 :                 return SPDK_POLLER_BUSY;
     179             :         }
     180             : 
     181           0 :         resp = spdk_jsonrpc_client_get_response(ctx->client_conn);
     182           0 :         assert(resp);
     183             : 
     184           0 :         if (resp->error) {
     185           0 :                 struct json_write_buf buf = {};
     186           0 :                 struct spdk_json_write_ctx *w = spdk_json_write_begin(json_write_stdout,
     187             :                                                 &buf, SPDK_JSON_PARSE_FLAG_DECODE_IN_PLACE);
     188             : 
     189           0 :                 if (w == NULL) {
     190           0 :                         SPDK_ERRLOG("error response: (?)\n");
     191             :                 } else {
     192           0 :                         spdk_json_write_val(w, resp->error);
     193           0 :                         spdk_json_write_end(w);
     194           0 :                         SPDK_ERRLOG("error response: \n%s\n", buf.data);
     195             :                 }
     196             :         }
     197             : 
     198           0 :         if (resp->error && ctx->stop_on_error) {
     199           0 :                 spdk_jsonrpc_client_free_response(resp);
     200           0 :                 app_json_config_load_done(ctx, -EINVAL);
     201             :         } else {
     202             :                 /* We have response so we must have callback for it. */
     203           0 :                 cb = ctx->client_resp_cb;
     204           0 :                 assert(cb != NULL);
     205             : 
     206             :                 /* Mark we are done with this handler. */
     207           0 :                 ctx->client_resp_cb = NULL;
     208           0 :                 cb(ctx, resp);
     209             :         }
     210             : 
     211             : 
     212           0 :         return SPDK_POLLER_BUSY;
     213             : }
     214             : 
     215             : static int
     216           0 : rpc_client_connect_poller(void *_ctx)
     217             : {
     218           0 :         struct load_json_config_ctx *ctx = _ctx;
     219             :         int rc;
     220             : 
     221           0 :         rc = spdk_jsonrpc_client_poll(ctx->client_conn, 0);
     222           0 :         if (rc != -ENOTCONN) {
     223             :                 /* We are connected. Start regular poller and issue first request */
     224           0 :                 spdk_poller_unregister(&ctx->client_conn_poller);
     225           0 :                 ctx->client_conn_poller = SPDK_POLLER_REGISTER(rpc_client_poller, ctx, 100);
     226           0 :                 app_json_config_load_subsystem(ctx);
     227             :         } else {
     228           0 :                 rc = rpc_client_check_timeout(ctx);
     229           0 :                 if (rc) {
     230           0 :                         app_json_config_load_done(ctx, rc);
     231             :                 }
     232             : 
     233           0 :                 return SPDK_POLLER_IDLE;
     234             :         }
     235             : 
     236           0 :         return SPDK_POLLER_BUSY;
     237             : }
     238             : 
     239             : static int
     240           0 : client_send_request(struct load_json_config_ctx *ctx, struct spdk_jsonrpc_client_request *request,
     241             :                     client_resp_handler client_resp_cb)
     242             : {
     243             :         int rc;
     244             : 
     245           0 :         assert(spdk_get_thread() == ctx->thread);
     246             : 
     247           0 :         ctx->client_resp_cb = client_resp_cb;
     248           0 :         rpc_client_set_timeout(ctx, RPC_CLIENT_REQUEST_TIMEOUT_US);
     249           0 :         rc = spdk_jsonrpc_client_send_request(ctx->client_conn, request);
     250             : 
     251           0 :         if (rc) {
     252           0 :                 SPDK_DEBUG_APP_CFG("Sending request to client failed (%d)\n", rc);
     253             :         }
     254             : 
     255           0 :         return rc;
     256             : }
     257             : 
     258             : static int
     259           0 : cap_string(const struct spdk_json_val *val, void *out)
     260             : {
     261           0 :         const struct spdk_json_val **vptr = out;
     262             : 
     263           0 :         if (val->type != SPDK_JSON_VAL_STRING) {
     264           0 :                 return -EINVAL;
     265             :         }
     266             : 
     267           0 :         *vptr = val;
     268           0 :         return 0;
     269             : }
     270             : 
     271             : static int
     272           0 : cap_object(const struct spdk_json_val *val, void *out)
     273             : {
     274           0 :         const struct spdk_json_val **vptr = out;
     275             : 
     276           0 :         if (val->type != SPDK_JSON_VAL_OBJECT_BEGIN) {
     277           0 :                 return -EINVAL;
     278             :         }
     279             : 
     280           0 :         *vptr = val;
     281           0 :         return 0;
     282             : }
     283             : 
     284             : 
     285             : static int
     286           0 : cap_array_or_null(const struct spdk_json_val *val, void *out)
     287             : {
     288           0 :         const struct spdk_json_val **vptr = out;
     289             : 
     290           0 :         if (val->type != SPDK_JSON_VAL_ARRAY_BEGIN && val->type != SPDK_JSON_VAL_NULL) {
     291           0 :                 return -EINVAL;
     292             :         }
     293             : 
     294           0 :         *vptr = val;
     295           0 :         return 0;
     296             : }
     297             : 
     298             : struct config_entry {
     299             :         char *method;
     300             :         struct spdk_json_val *params;
     301             : };
     302             : 
     303             : static struct spdk_json_object_decoder jsonrpc_cmd_decoders[] = {
     304             :         {"method", offsetof(struct config_entry, method), spdk_json_decode_string},
     305             :         {"params", offsetof(struct config_entry, params), cap_object, true}
     306             : };
     307             : 
     308             : static void app_json_config_load_subsystem_config_entry(void *_ctx);
     309             : 
     310             : static void
     311           0 : app_json_config_load_subsystem_config_entry_next(struct load_json_config_ctx *ctx,
     312             :                 struct spdk_jsonrpc_client_response *resp)
     313             : {
     314             :         /* Don't care about the response */
     315           0 :         spdk_jsonrpc_client_free_response(resp);
     316             : 
     317           0 :         ctx->config_it = spdk_json_next(ctx->config_it);
     318           0 :         app_json_config_load_subsystem_config_entry(ctx);
     319           0 : }
     320             : 
     321             : /* Load "config" entry */
     322             : static void
     323           0 : app_json_config_load_subsystem_config_entry(void *_ctx)
     324             : {
     325           0 :         struct load_json_config_ctx *ctx = _ctx;
     326             :         struct spdk_jsonrpc_client_request *rpc_request;
     327             :         struct spdk_json_write_ctx *w;
     328           0 :         struct config_entry cfg = {};
     329             :         struct spdk_json_val *params_end;
     330           0 :         size_t params_len = 0;
     331           0 :         uint32_t state_mask = 0, cur_state_mask, startup_runtime = SPDK_RPC_STARTUP | SPDK_RPC_RUNTIME;
     332             :         int rc;
     333             : 
     334           0 :         if (ctx->config_it == NULL) {
     335           0 :                 SPDK_DEBUG_APP_CFG("Subsystem '%.*s': configuration done.\n", ctx->subsystem_name->len,
     336             :                                    (char *)ctx->subsystem_name->start);
     337           0 :                 ctx->subsystems_it = spdk_json_next(ctx->subsystems_it);
     338             :                 /* Invoke later to avoid recurrence */
     339           0 :                 spdk_thread_send_msg(ctx->thread, app_json_config_load_subsystem, ctx);
     340           0 :                 return;
     341             :         }
     342             : 
     343           0 :         if (spdk_json_decode_object(ctx->config_it, jsonrpc_cmd_decoders,
     344             :                                     SPDK_COUNTOF(jsonrpc_cmd_decoders), &cfg)) {
     345           0 :                 SPDK_ERRLOG("Failed to decode config entry\n");
     346           0 :                 app_json_config_load_done(ctx, -EINVAL);
     347           0 :                 goto out;
     348             :         }
     349             : 
     350           0 :         rc = spdk_rpc_get_method_state_mask(cfg.method, &state_mask);
     351           0 :         if (rc == -ENOENT) {
     352           0 :                 SPDK_ERRLOG("Method '%s' was not found\n", cfg.method);
     353           0 :                 app_json_config_load_done(ctx, rc);
     354           0 :                 goto out;
     355             :         }
     356           0 :         cur_state_mask = spdk_rpc_get_state();
     357           0 :         if ((state_mask & cur_state_mask) != cur_state_mask) {
     358           0 :                 SPDK_DEBUG_APP_CFG("Method '%s' not allowed -> skipping\n", cfg.method);
     359             :                 /* Invoke later to avoid recurrence */
     360           0 :                 ctx->config_it = spdk_json_next(ctx->config_it);
     361           0 :                 spdk_thread_send_msg(ctx->thread, app_json_config_load_subsystem_config_entry, ctx);
     362           0 :                 goto out;
     363             :         }
     364           0 :         if ((state_mask & startup_runtime) == startup_runtime && cur_state_mask == SPDK_RPC_RUNTIME) {
     365             :                 /* Some methods are allowed to be run in both STARTUP and RUNTIME states.
     366             :                  * We should not call such methods twice, so ignore the second attempt in RUNTIME state */
     367           0 :                 SPDK_DEBUG_APP_CFG("Method '%s' has already been run in STARTUP state\n", cfg.method);
     368             :                 /* Invoke later to avoid recurrence */
     369           0 :                 ctx->config_it = spdk_json_next(ctx->config_it);
     370           0 :                 spdk_thread_send_msg(ctx->thread, app_json_config_load_subsystem_config_entry, ctx);
     371           0 :                 goto out;
     372             :         }
     373             : 
     374           0 :         SPDK_DEBUG_APP_CFG("\tmethod: %s\n", cfg.method);
     375             : 
     376           0 :         if (cfg.params) {
     377             :                 /* Get _END by skipping params and going back by one element. */
     378           0 :                 params_end = cfg.params + spdk_json_val_len(cfg.params) - 1;
     379             : 
     380             :                 /* Need to add one character to include '}' */
     381           0 :                 params_len = params_end->start - cfg.params->start + 1;
     382             : 
     383           0 :                 SPDK_DEBUG_APP_CFG("\tparams: %.*s\n", (int)params_len, (char *)cfg.params->start);
     384             :         }
     385             : 
     386           0 :         rpc_request = spdk_jsonrpc_client_create_request();
     387           0 :         if (!rpc_request) {
     388           0 :                 app_json_config_load_done(ctx, -errno);
     389           0 :                 goto out;
     390             :         }
     391             : 
     392           0 :         w = spdk_jsonrpc_begin_request(rpc_request, ctx->rpc_request_id, NULL);
     393           0 :         if (!w) {
     394           0 :                 spdk_jsonrpc_client_free_request(rpc_request);
     395           0 :                 app_json_config_load_done(ctx, -ENOMEM);
     396           0 :                 goto out;
     397             :         }
     398             : 
     399           0 :         spdk_json_write_named_string(w, "method", cfg.method);
     400             : 
     401           0 :         if (cfg.params) {
     402             :                 /* No need to parse "params". Just dump the whole content of "params"
     403             :                  * directly into the request and let the remote side verify it. */
     404           0 :                 spdk_json_write_name(w, "params");
     405           0 :                 spdk_json_write_val_raw(w, cfg.params->start, params_len);
     406             :         }
     407             : 
     408           0 :         spdk_jsonrpc_end_request(rpc_request, w);
     409             : 
     410           0 :         rc = client_send_request(ctx, rpc_request, app_json_config_load_subsystem_config_entry_next);
     411           0 :         if (rc != 0) {
     412           0 :                 app_json_config_load_done(ctx, -rc);
     413           0 :                 goto out;
     414             :         }
     415           0 : out:
     416           0 :         free(cfg.method);
     417             : }
     418             : 
     419             : static void
     420           0 : subsystem_init_done(int rc, void *arg1)
     421             : {
     422           0 :         struct load_json_config_ctx *ctx = arg1;
     423             : 
     424           0 :         if (rc) {
     425           0 :                 app_json_config_load_done(ctx, rc);
     426           0 :                 return;
     427             :         }
     428             : 
     429           0 :         spdk_rpc_set_state(SPDK_RPC_RUNTIME);
     430             :         /* Another round. This time for RUNTIME methods */
     431           0 :         SPDK_DEBUG_APP_CFG("'framework_start_init' done - continuing configuration\n");
     432             : 
     433           0 :         assert(ctx != NULL);
     434           0 :         if (ctx->subsystems) {
     435           0 :                 ctx->subsystems_it = spdk_json_array_first(ctx->subsystems);
     436             :         }
     437             : 
     438           0 :         app_json_config_load_subsystem(ctx);
     439             : }
     440             : 
     441             : static struct spdk_json_object_decoder subsystem_decoders[] = {
     442             :         {"subsystem", offsetof(struct load_json_config_ctx, subsystem_name), cap_string},
     443             :         {"config", offsetof(struct load_json_config_ctx, config), cap_array_or_null}
     444             : };
     445             : 
     446             : /*
     447             :  * Start loading subsystem pointed by ctx->subsystems_it. This must point to the
     448             :  * beginning of the "subsystem" object in "subsystems" array or be NULL. If it is
     449             :  * NULL then no more subsystems to load.
     450             :  *
     451             :  * There are two iterations:
     452             :  *
     453             :  * In first iteration only STARTUP RPC methods are used, other methods are ignored. When
     454             :  * allsubsystems are walked the ctx->subsystems_it became NULL and "framework_start_init"
     455             :  * is called to let the SPDK move to RUNTIME state (initialize all subsystems) and
     456             :  * second iteration begins.
     457             :  *
     458             :  * In second iteration "subsystems" array is walked through again, this time only
     459             :  * RUNTIME RPC methods are used. When ctx->subsystems_it became NULL second time it
     460             :  * indicate that there is no more subsystems to load. The cb_fn is called to finish
     461             :  * configuration.
     462             :  */
     463             : static void
     464           0 : app_json_config_load_subsystem(void *_ctx)
     465             : {
     466           0 :         struct load_json_config_ctx *ctx = _ctx;
     467             : 
     468           0 :         if (ctx->subsystems_it == NULL) {
     469           0 :                 if (spdk_rpc_get_state() == SPDK_RPC_STARTUP) {
     470           0 :                         SPDK_DEBUG_APP_CFG("No more entries for current state, calling 'framework_start_init'\n");
     471           0 :                         spdk_subsystem_init(subsystem_init_done, ctx);
     472             :                 } else {
     473           0 :                         app_json_config_load_done(ctx, 0);
     474             :                 }
     475             : 
     476           0 :                 return;
     477             :         }
     478             : 
     479             :         /* Capture subsystem name and config array */
     480           0 :         if (spdk_json_decode_object(ctx->subsystems_it, subsystem_decoders,
     481             :                                     SPDK_COUNTOF(subsystem_decoders), ctx)) {
     482           0 :                 SPDK_ERRLOG("Failed to parse subsystem configuration\n");
     483           0 :                 app_json_config_load_done(ctx, -EINVAL);
     484           0 :                 return;
     485             :         }
     486             : 
     487           0 :         SPDK_DEBUG_APP_CFG("Loading subsystem '%.*s' configuration\n", ctx->subsystem_name->len,
     488             :                            (char *)ctx->subsystem_name->start);
     489             : 
     490             :         /* Get 'config' array first configuration entry */
     491           0 :         ctx->config_it = spdk_json_array_first(ctx->config);
     492           0 :         app_json_config_load_subsystem_config_entry(ctx);
     493             : }
     494             : 
     495             : static void *
     496           0 : read_file(const char *filename, size_t *size)
     497             : {
     498           0 :         FILE *file = fopen(filename, "r");
     499             :         void *data;
     500             : 
     501           0 :         if (file == NULL) {
     502             :                 /* errno is set by fopen */
     503           0 :                 return NULL;
     504             :         }
     505             : 
     506           0 :         data = spdk_posix_file_load(file, size);
     507           0 :         fclose(file);
     508           0 :         return data;
     509             : }
     510             : 
     511             : static int
     512           0 : app_json_config_read(const char *config_file, struct load_json_config_ctx *ctx)
     513             : {
     514           0 :         struct spdk_json_val *values = NULL;
     515           0 :         void *json = NULL, *end;
     516             :         ssize_t values_cnt, rc;
     517           0 :         size_t json_size;
     518             : 
     519           0 :         json = read_file(config_file, &json_size);
     520           0 :         if (!json) {
     521           0 :                 SPDK_ERRLOG("Read JSON configuration file %s failed: %s\n",
     522             :                             config_file, spdk_strerror(errno));
     523           0 :                 return -errno;
     524             :         }
     525             : 
     526           0 :         rc = spdk_json_parse(json, json_size, NULL, 0, &end,
     527             :                              SPDK_JSON_PARSE_FLAG_ALLOW_COMMENTS);
     528           0 :         if (rc < 0) {
     529           0 :                 SPDK_ERRLOG("Parsing JSON configuration failed (%zd)\n", rc);
     530           0 :                 goto err;
     531             :         }
     532             : 
     533           0 :         values_cnt = rc;
     534           0 :         values = calloc(values_cnt, sizeof(struct spdk_json_val));
     535           0 :         if (values == NULL) {
     536           0 :                 SPDK_ERRLOG("Out of memory\n");
     537           0 :                 goto err;
     538             :         }
     539             : 
     540           0 :         rc = spdk_json_parse(json, json_size, values, values_cnt, &end,
     541             :                              SPDK_JSON_PARSE_FLAG_ALLOW_COMMENTS);
     542           0 :         if (rc != values_cnt) {
     543           0 :                 SPDK_ERRLOG("Parsing JSON configuration failed (%zd)\n", rc);
     544           0 :                 goto err;
     545             :         }
     546             : 
     547           0 :         ctx->json_data = json;
     548           0 :         ctx->json_data_size = json_size;
     549             : 
     550           0 :         ctx->values = values;
     551           0 :         ctx->values_cnt = values_cnt;
     552             : 
     553           0 :         return 0;
     554           0 : err:
     555           0 :         free(json);
     556           0 :         free(values);
     557           0 :         return rc;
     558             : }
     559             : 
     560             : void
     561           0 : spdk_subsystem_init_from_json_config(const char *json_config_file, const char *rpc_addr,
     562             :                                      spdk_subsystem_init_fn cb_fn, void *cb_arg,
     563             :                                      bool stop_on_error)
     564             : {
     565           0 :         struct load_json_config_ctx *ctx = calloc(1, sizeof(*ctx));
     566             :         int rc;
     567             : 
     568           0 :         assert(cb_fn);
     569           0 :         if (!ctx) {
     570           0 :                 cb_fn(-ENOMEM, cb_arg);
     571           0 :                 return;
     572             :         }
     573             : 
     574           0 :         ctx->cb_fn = cb_fn;
     575           0 :         ctx->cb_arg = cb_arg;
     576           0 :         ctx->stop_on_error = stop_on_error;
     577           0 :         ctx->thread = spdk_get_thread();
     578             : 
     579           0 :         rc = app_json_config_read(json_config_file, ctx);
     580           0 :         if (rc) {
     581           0 :                 goto fail;
     582             :         }
     583             : 
     584             :         /* Capture subsystems array */
     585           0 :         rc = spdk_json_find_array(ctx->values, "subsystems", NULL, &ctx->subsystems);
     586           0 :         switch (rc) {
     587           0 :         case 0:
     588             :                 /* Get first subsystem */
     589           0 :                 ctx->subsystems_it = spdk_json_array_first(ctx->subsystems);
     590           0 :                 if (ctx->subsystems_it == NULL) {
     591           0 :                         SPDK_NOTICELOG("'subsystems' configuration is empty\n");
     592             :                 }
     593           0 :                 break;
     594           0 :         case -EPROTOTYPE:
     595           0 :                 SPDK_ERRLOG("Invalid JSON configuration: not enclosed in {}.\n");
     596           0 :                 goto fail;
     597           0 :         case -ENOENT:
     598           0 :                 SPDK_WARNLOG("No 'subsystems' key JSON configuration file.\n");
     599           0 :                 break;
     600           0 :         case -EDOM:
     601           0 :                 SPDK_ERRLOG("Invalid JSON configuration: 'subsystems' should be an array.\n");
     602           0 :                 goto fail;
     603           0 :         default:
     604           0 :                 SPDK_ERRLOG("Failed to parse JSON configuration.\n");
     605           0 :                 goto fail;
     606             :         }
     607             : 
     608             :         /* If rpc_addr is not an Unix socket use default address as prefix. */
     609           0 :         if (rpc_addr == NULL || rpc_addr[0] != '/') {
     610           0 :                 rpc_addr = SPDK_DEFAULT_RPC_ADDR;
     611             :         }
     612             : 
     613             :         /* FIXME: rpc client should use socketpair() instead of this temporary socket nonsense */
     614           0 :         rc = snprintf(ctx->rpc_socket_path_temp, sizeof(ctx->rpc_socket_path_temp), "%s.%d_config",
     615             :                       rpc_addr, getpid());
     616           0 :         if (rc >= (int)sizeof(ctx->rpc_socket_path_temp)) {
     617           0 :                 SPDK_ERRLOG("Socket name create failed\n");
     618           0 :                 goto fail;
     619             :         }
     620             : 
     621           0 :         rc = spdk_rpc_initialize(ctx->rpc_socket_path_temp, NULL);
     622           0 :         if (rc) {
     623           0 :                 goto fail;
     624             :         }
     625             : 
     626           0 :         ctx->client_conn = spdk_jsonrpc_client_connect(ctx->rpc_socket_path_temp, AF_UNIX);
     627           0 :         if (ctx->client_conn == NULL) {
     628           0 :                 SPDK_ERRLOG("Failed to connect to '%s'\n", ctx->rpc_socket_path_temp);
     629           0 :                 goto fail;
     630             :         }
     631             : 
     632           0 :         rpc_client_set_timeout(ctx, RPC_CLIENT_CONNECT_TIMEOUT_US);
     633           0 :         ctx->client_conn_poller = SPDK_POLLER_REGISTER(rpc_client_connect_poller, ctx, 100);
     634           0 :         return;
     635             : 
     636           0 : fail:
     637           0 :         app_json_config_load_done(ctx, -EINVAL);
     638             : }
     639             : 
     640           0 : SPDK_LOG_REGISTER_COMPONENT(app_config)

Generated by: LCOV version 1.15