LCOV - code coverage report
Current view: top level - module/bdev/nvme - bdev_nvme_rpc.c (source / functions) Hit Total Coverage
Test: ut_cov_unit.info Lines: 0 1423 0.0 %
Date: 2024-11-18 11:47:07 Functions: 0 113 0.0 %

          Line data    Source code
       1             : /*   SPDX-License-Identifier: BSD-3-Clause
       2             :  *   Copyright (C) 2016 Intel Corporation. All rights reserved.
       3             :  *   Copyright (c) 2019-2021 Mellanox Technologies LTD. All rights reserved.
       4             :  *   Copyright (c) 2022-2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
       5             :  *   Copyright (c) 2022 Dell Inc, or its subsidiaries. All rights reserved.
       6             :  */
       7             : 
       8             : #include "spdk/stdinc.h"
       9             : 
      10             : #include "bdev_nvme.h"
      11             : 
      12             : #include "spdk/config.h"
      13             : 
      14             : #include "spdk/string.h"
      15             : #include "spdk/rpc.h"
      16             : #include "spdk/util.h"
      17             : #include "spdk/env.h"
      18             : #include "spdk/nvme.h"
      19             : #include "spdk/nvme_spec.h"
      20             : 
      21             : #include "spdk/log.h"
      22             : #include "spdk/bdev_module.h"
      23             : 
      24             : static bool g_tls_log = false;
      25             : 
      26             : static int
      27           0 : rpc_decode_action_on_timeout(const struct spdk_json_val *val, void *out)
      28             : {
      29           0 :         enum spdk_bdev_timeout_action *action = out;
      30             : 
      31           0 :         if (spdk_json_strequal(val, "none") == true) {
      32           0 :                 *action = SPDK_BDEV_NVME_TIMEOUT_ACTION_NONE;
      33           0 :         } else if (spdk_json_strequal(val, "abort") == true) {
      34           0 :                 *action = SPDK_BDEV_NVME_TIMEOUT_ACTION_ABORT;
      35           0 :         } else if (spdk_json_strequal(val, "reset") == true) {
      36           0 :                 *action = SPDK_BDEV_NVME_TIMEOUT_ACTION_RESET;
      37           0 :         } else {
      38           0 :                 SPDK_NOTICELOG("Invalid parameter value: action_on_timeout\n");
      39           0 :                 return -EINVAL;
      40             :         }
      41             : 
      42           0 :         return 0;
      43           0 : }
      44             : 
      45             : static int
      46           0 : rpc_decode_digest(const struct spdk_json_val *val, void *out)
      47             : {
      48           0 :         uint32_t *flags = out;
      49           0 :         char *digest = NULL;
      50             :         int rc;
      51             : 
      52           0 :         rc = spdk_json_decode_string(val, &digest);
      53           0 :         if (rc != 0) {
      54           0 :                 return rc;
      55             :         }
      56             : 
      57           0 :         rc = spdk_nvme_dhchap_get_digest_id(digest);
      58           0 :         if (rc >= 0) {
      59           0 :                 *flags |= SPDK_BIT(rc);
      60           0 :                 rc = 0;
      61           0 :         }
      62           0 :         free(digest);
      63             : 
      64           0 :         return rc;
      65           0 : }
      66             : 
      67             : static int
      68           0 : rpc_decode_digest_array(const struct spdk_json_val *val, void *out)
      69             : {
      70           0 :         uint32_t *flags = out;
      71             :         size_t count;
      72             : 
      73           0 :         *flags = 0;
      74             : 
      75           0 :         return spdk_json_decode_array(val, rpc_decode_digest, out, 32, &count, 0);
      76             : }
      77             : 
      78             : static int
      79           0 : rpc_decode_dhgroup(const struct spdk_json_val *val, void *out)
      80             : {
      81           0 :         uint32_t *flags = out;
      82           0 :         char *dhgroup = NULL;
      83             :         int rc;
      84             : 
      85           0 :         rc = spdk_json_decode_string(val, &dhgroup);
      86           0 :         if (rc != 0) {
      87           0 :                 return rc;
      88             :         }
      89             : 
      90           0 :         rc = spdk_nvme_dhchap_get_dhgroup_id(dhgroup);
      91           0 :         if (rc >= 0) {
      92           0 :                 *flags |= SPDK_BIT(rc);
      93           0 :                 rc = 0;
      94           0 :         }
      95           0 :         free(dhgroup);
      96             : 
      97           0 :         return rc;
      98           0 : }
      99             : 
     100             : static int
     101           0 : rpc_decode_dhgroup_array(const struct spdk_json_val *val, void *out)
     102             : {
     103           0 :         uint32_t *flags = out;
     104             :         size_t count;
     105             : 
     106           0 :         *flags = 0;
     107             : 
     108           0 :         return spdk_json_decode_array(val, rpc_decode_dhgroup, out, 32, &count, 0);
     109             : }
     110             : 
     111             : static const struct spdk_json_object_decoder rpc_bdev_nvme_options_decoders[] = {
     112             :         {"action_on_timeout", offsetof(struct spdk_bdev_nvme_opts, action_on_timeout), rpc_decode_action_on_timeout, true},
     113             :         {"timeout_us", offsetof(struct spdk_bdev_nvme_opts, timeout_us), spdk_json_decode_uint64, true},
     114             :         {"timeout_admin_us", offsetof(struct spdk_bdev_nvme_opts, timeout_admin_us), spdk_json_decode_uint64, true},
     115             :         {"keep_alive_timeout_ms", offsetof(struct spdk_bdev_nvme_opts, keep_alive_timeout_ms), spdk_json_decode_uint32, true},
     116             :         {"arbitration_burst", offsetof(struct spdk_bdev_nvme_opts, arbitration_burst), spdk_json_decode_uint32, true},
     117             :         {"low_priority_weight", offsetof(struct spdk_bdev_nvme_opts, low_priority_weight), spdk_json_decode_uint32, true},
     118             :         {"medium_priority_weight", offsetof(struct spdk_bdev_nvme_opts, medium_priority_weight), spdk_json_decode_uint32, true},
     119             :         {"high_priority_weight", offsetof(struct spdk_bdev_nvme_opts, high_priority_weight), spdk_json_decode_uint32, true},
     120             :         {"nvme_adminq_poll_period_us", offsetof(struct spdk_bdev_nvme_opts, nvme_adminq_poll_period_us), spdk_json_decode_uint64, true},
     121             :         {"nvme_ioq_poll_period_us", offsetof(struct spdk_bdev_nvme_opts, nvme_ioq_poll_period_us), spdk_json_decode_uint64, true},
     122             :         {"io_queue_requests", offsetof(struct spdk_bdev_nvme_opts, io_queue_requests), spdk_json_decode_uint32, true},
     123             :         {"delay_cmd_submit", offsetof(struct spdk_bdev_nvme_opts, delay_cmd_submit), spdk_json_decode_bool, true},
     124             :         {"transport_retry_count", offsetof(struct spdk_bdev_nvme_opts, transport_retry_count), spdk_json_decode_uint32, true},
     125             :         {"bdev_retry_count", offsetof(struct spdk_bdev_nvme_opts, bdev_retry_count), spdk_json_decode_int32, true},
     126             :         {"transport_ack_timeout", offsetof(struct spdk_bdev_nvme_opts, transport_ack_timeout), spdk_json_decode_uint8, true},
     127             :         {"ctrlr_loss_timeout_sec", offsetof(struct spdk_bdev_nvme_opts, ctrlr_loss_timeout_sec), spdk_json_decode_int32, true},
     128             :         {"reconnect_delay_sec", offsetof(struct spdk_bdev_nvme_opts, reconnect_delay_sec), spdk_json_decode_uint32, true},
     129             :         {"fast_io_fail_timeout_sec", offsetof(struct spdk_bdev_nvme_opts, fast_io_fail_timeout_sec), spdk_json_decode_uint32, true},
     130             :         {"disable_auto_failback", offsetof(struct spdk_bdev_nvme_opts, disable_auto_failback), spdk_json_decode_bool, true},
     131             :         {"generate_uuids", offsetof(struct spdk_bdev_nvme_opts, generate_uuids), spdk_json_decode_bool, true},
     132             :         {"transport_tos", offsetof(struct spdk_bdev_nvme_opts, transport_tos), spdk_json_decode_uint8, true},
     133             :         {"nvme_error_stat", offsetof(struct spdk_bdev_nvme_opts, nvme_error_stat), spdk_json_decode_bool, true},
     134             :         {"rdma_srq_size", offsetof(struct spdk_bdev_nvme_opts, rdma_srq_size), spdk_json_decode_uint32, true},
     135             :         {"io_path_stat", offsetof(struct spdk_bdev_nvme_opts, io_path_stat), spdk_json_decode_bool, true},
     136             :         {"allow_accel_sequence", offsetof(struct spdk_bdev_nvme_opts, allow_accel_sequence), spdk_json_decode_bool, true},
     137             :         {"rdma_max_cq_size", offsetof(struct spdk_bdev_nvme_opts, rdma_max_cq_size), spdk_json_decode_uint32, true},
     138             :         {"rdma_cm_event_timeout_ms", offsetof(struct spdk_bdev_nvme_opts, rdma_cm_event_timeout_ms), spdk_json_decode_uint16, true},
     139             :         {"dhchap_digests", offsetof(struct spdk_bdev_nvme_opts, dhchap_digests), rpc_decode_digest_array, true},
     140             :         {"dhchap_dhgroups", offsetof(struct spdk_bdev_nvme_opts, dhchap_dhgroups), rpc_decode_dhgroup_array, true},
     141             : };
     142             : 
     143             : static void
     144           0 : rpc_bdev_nvme_set_options(struct spdk_jsonrpc_request *request,
     145             :                           const struct spdk_json_val *params)
     146             : {
     147             :         struct spdk_bdev_nvme_opts opts;
     148             :         int rc;
     149             : 
     150           0 :         bdev_nvme_get_opts(&opts);
     151           0 :         if (params && spdk_json_decode_object(params, rpc_bdev_nvme_options_decoders,
     152             :                                               SPDK_COUNTOF(rpc_bdev_nvme_options_decoders),
     153             :                                               &opts)) {
     154           0 :                 SPDK_ERRLOG("spdk_json_decode_object failed\n");
     155           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
     156             :                                                  "spdk_json_decode_object failed");
     157           0 :                 return;
     158             :         }
     159             : 
     160           0 :         rc = bdev_nvme_set_opts(&opts);
     161           0 :         if (rc == -EPERM) {
     162           0 :                 spdk_jsonrpc_send_error_response(request, -EPERM,
     163             :                                                  "RPC not permitted with nvme controllers already attached");
     164           0 :         } else if (rc) {
     165           0 :                 spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
     166           0 :         } else {
     167           0 :                 spdk_jsonrpc_send_bool_response(request, true);
     168             :         }
     169             : 
     170           0 :         return;
     171           0 : }
     172           0 : SPDK_RPC_REGISTER("bdev_nvme_set_options", rpc_bdev_nvme_set_options,
     173             :                   SPDK_RPC_STARTUP | SPDK_RPC_RUNTIME)
     174             : 
     175             : struct rpc_bdev_nvme_hotplug {
     176             :         bool enabled;
     177             :         uint64_t period_us;
     178             : };
     179             : 
     180             : static const struct spdk_json_object_decoder rpc_bdev_nvme_hotplug_decoders[] = {
     181             :         {"enable", offsetof(struct rpc_bdev_nvme_hotplug, enabled), spdk_json_decode_bool, false},
     182             :         {"period_us", offsetof(struct rpc_bdev_nvme_hotplug, period_us), spdk_json_decode_uint64, true},
     183             : };
     184             : 
     185             : static void
     186           0 : rpc_bdev_nvme_set_hotplug_done(void *ctx)
     187             : {
     188           0 :         struct spdk_jsonrpc_request *request = ctx;
     189             : 
     190           0 :         spdk_jsonrpc_send_bool_response(request, true);
     191           0 : }
     192             : 
     193             : static void
     194           0 : rpc_bdev_nvme_set_hotplug(struct spdk_jsonrpc_request *request,
     195             :                           const struct spdk_json_val *params)
     196             : {
     197           0 :         struct rpc_bdev_nvme_hotplug req = {false, 0};
     198             :         int rc;
     199             : 
     200           0 :         if (spdk_json_decode_object(params, rpc_bdev_nvme_hotplug_decoders,
     201             :                                     SPDK_COUNTOF(rpc_bdev_nvme_hotplug_decoders), &req)) {
     202           0 :                 SPDK_ERRLOG("spdk_json_decode_object failed\n");
     203           0 :                 rc = -EINVAL;
     204           0 :                 goto invalid;
     205             :         }
     206             : 
     207           0 :         rc = bdev_nvme_set_hotplug(req.enabled, req.period_us, rpc_bdev_nvme_set_hotplug_done,
     208           0 :                                    request);
     209           0 :         if (rc) {
     210           0 :                 goto invalid;
     211             :         }
     212             : 
     213           0 :         return;
     214             : invalid:
     215           0 :         spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, spdk_strerror(-rc));
     216           0 : }
     217           0 : SPDK_RPC_REGISTER("bdev_nvme_set_hotplug", rpc_bdev_nvme_set_hotplug, SPDK_RPC_RUNTIME)
     218             : 
     219             : enum bdev_nvme_multipath_mode {
     220             :         BDEV_NVME_MP_MODE_FAILOVER,
     221             :         BDEV_NVME_MP_MODE_MULTIPATH,
     222             :         BDEV_NVME_MP_MODE_DISABLE,
     223             : };
     224             : 
     225             : struct rpc_bdev_nvme_attach_controller {
     226             :         char *name;
     227             :         char *trtype;
     228             :         char *adrfam;
     229             :         char *traddr;
     230             :         char *trsvcid;
     231             :         char *priority;
     232             :         char *subnqn;
     233             :         char *hostnqn;
     234             :         char *hostaddr;
     235             :         char *hostsvcid;
     236             :         char *psk;
     237             :         char *dhchap_key;
     238             :         char *dhchap_ctrlr_key;
     239             :         enum bdev_nvme_multipath_mode multipath;
     240             :         struct spdk_bdev_nvme_ctrlr_opts bdev_opts;
     241             :         struct spdk_nvme_ctrlr_opts drv_opts;
     242             :         uint32_t max_bdevs;
     243             : };
     244             : 
     245             : static void
     246           0 : free_rpc_bdev_nvme_attach_controller(struct rpc_bdev_nvme_attach_controller *req)
     247             : {
     248           0 :         free(req->name);
     249           0 :         free(req->trtype);
     250           0 :         free(req->adrfam);
     251           0 :         free(req->traddr);
     252           0 :         free(req->trsvcid);
     253           0 :         free(req->priority);
     254           0 :         free(req->subnqn);
     255           0 :         free(req->hostnqn);
     256           0 :         free(req->hostaddr);
     257           0 :         free(req->hostsvcid);
     258           0 :         free(req->psk);
     259           0 :         free(req->dhchap_key);
     260           0 :         free(req->dhchap_ctrlr_key);
     261           0 : }
     262             : 
     263             : static int
     264           0 : bdev_nvme_decode_reftag(const struct spdk_json_val *val, void *out)
     265             : {
     266           0 :         uint32_t *flag = out;
     267             :         bool reftag;
     268             :         int rc;
     269             : 
     270           0 :         rc = spdk_json_decode_bool(val, &reftag);
     271           0 :         if (rc == 0 && reftag == true) {
     272           0 :                 *flag |= SPDK_NVME_IO_FLAGS_PRCHK_REFTAG;
     273           0 :         }
     274             : 
     275           0 :         return rc;
     276             : }
     277             : 
     278             : static int
     279           0 : bdev_nvme_decode_guard(const struct spdk_json_val *val, void *out)
     280             : {
     281           0 :         uint32_t *flag = out;
     282             :         bool guard;
     283             :         int rc;
     284             : 
     285           0 :         rc = spdk_json_decode_bool(val, &guard);
     286           0 :         if (rc == 0 && guard == true) {
     287           0 :                 *flag |= SPDK_NVME_IO_FLAGS_PRCHK_GUARD;
     288           0 :         }
     289             : 
     290           0 :         return rc;
     291             : }
     292             : 
     293             : static int
     294           0 : bdev_nvme_decode_multipath(const struct spdk_json_val *val, void *out)
     295             : {
     296           0 :         enum bdev_nvme_multipath_mode *multipath = out;
     297             : 
     298           0 :         if (spdk_json_strequal(val, "failover") == true) {
     299           0 :                 *multipath = BDEV_NVME_MP_MODE_FAILOVER;
     300           0 :         } else if (spdk_json_strequal(val, "multipath") == true) {
     301           0 :                 *multipath = BDEV_NVME_MP_MODE_MULTIPATH;
     302           0 :         } else if (spdk_json_strequal(val, "disable") == true) {
     303           0 :                 *multipath = BDEV_NVME_MP_MODE_DISABLE;
     304           0 :         } else {
     305           0 :                 SPDK_NOTICELOG("Invalid parameter value: multipath\n");
     306           0 :                 return -EINVAL;
     307             :         }
     308             : 
     309           0 :         return 0;
     310           0 : }
     311             : 
     312             : 
     313             : static const struct spdk_json_object_decoder rpc_bdev_nvme_attach_controller_decoders[] = {
     314             :         {"name", offsetof(struct rpc_bdev_nvme_attach_controller, name), spdk_json_decode_string},
     315             :         {"trtype", offsetof(struct rpc_bdev_nvme_attach_controller, trtype), spdk_json_decode_string},
     316             :         {"traddr", offsetof(struct rpc_bdev_nvme_attach_controller, traddr), spdk_json_decode_string},
     317             : 
     318             :         {"adrfam", offsetof(struct rpc_bdev_nvme_attach_controller, adrfam), spdk_json_decode_string, true},
     319             :         {"trsvcid", offsetof(struct rpc_bdev_nvme_attach_controller, trsvcid), spdk_json_decode_string, true},
     320             :         {"priority", offsetof(struct rpc_bdev_nvme_attach_controller, priority), spdk_json_decode_string, true},
     321             :         {"subnqn", offsetof(struct rpc_bdev_nvme_attach_controller, subnqn), spdk_json_decode_string, true},
     322             :         {"hostnqn", offsetof(struct rpc_bdev_nvme_attach_controller, hostnqn), spdk_json_decode_string, true},
     323             :         {"hostaddr", offsetof(struct rpc_bdev_nvme_attach_controller, hostaddr), spdk_json_decode_string, true},
     324             :         {"hostsvcid", offsetof(struct rpc_bdev_nvme_attach_controller, hostsvcid), spdk_json_decode_string, true},
     325             : 
     326             :         {"prchk_reftag", offsetof(struct rpc_bdev_nvme_attach_controller, bdev_opts.prchk_flags), bdev_nvme_decode_reftag, true},
     327             :         {"prchk_guard", offsetof(struct rpc_bdev_nvme_attach_controller, bdev_opts.prchk_flags), bdev_nvme_decode_guard, true},
     328             :         {"hdgst", offsetof(struct rpc_bdev_nvme_attach_controller, drv_opts.header_digest), spdk_json_decode_bool, true},
     329             :         {"ddgst", offsetof(struct rpc_bdev_nvme_attach_controller, drv_opts.data_digest), spdk_json_decode_bool, true},
     330             :         {"fabrics_connect_timeout_us", offsetof(struct rpc_bdev_nvme_attach_controller, drv_opts.fabrics_connect_timeout_us), spdk_json_decode_uint64, true},
     331             :         {"multipath", offsetof(struct rpc_bdev_nvme_attach_controller, multipath), bdev_nvme_decode_multipath, true},
     332             :         {"num_io_queues", offsetof(struct rpc_bdev_nvme_attach_controller, drv_opts.num_io_queues), spdk_json_decode_uint32, true},
     333             :         {"ctrlr_loss_timeout_sec", offsetof(struct rpc_bdev_nvme_attach_controller, bdev_opts.ctrlr_loss_timeout_sec), spdk_json_decode_int32, true},
     334             :         {"reconnect_delay_sec", offsetof(struct rpc_bdev_nvme_attach_controller, bdev_opts.reconnect_delay_sec), spdk_json_decode_uint32, true},
     335             :         {"fast_io_fail_timeout_sec", offsetof(struct rpc_bdev_nvme_attach_controller, bdev_opts.fast_io_fail_timeout_sec), spdk_json_decode_uint32, true},
     336             :         {"psk", offsetof(struct rpc_bdev_nvme_attach_controller, psk), spdk_json_decode_string, true},
     337             :         {"max_bdevs", offsetof(struct rpc_bdev_nvme_attach_controller, max_bdevs), spdk_json_decode_uint32, true},
     338             :         {"dhchap_key", offsetof(struct rpc_bdev_nvme_attach_controller, dhchap_key), spdk_json_decode_string, true},
     339             :         {"dhchap_ctrlr_key", offsetof(struct rpc_bdev_nvme_attach_controller, dhchap_ctrlr_key), spdk_json_decode_string, true},
     340             :         {"allow_unrecognized_csi", offsetof(struct rpc_bdev_nvme_attach_controller, bdev_opts.allow_unrecognized_csi), spdk_json_decode_bool, true},
     341             : };
     342             : 
     343             : #define DEFAULT_MAX_BDEVS_PER_RPC 128
     344             : 
     345             : struct rpc_bdev_nvme_attach_controller_ctx {
     346             :         struct rpc_bdev_nvme_attach_controller req;
     347             :         size_t bdev_count;
     348             :         const char **names;
     349             :         struct spdk_jsonrpc_request *request;
     350             : };
     351             : 
     352             : static void
     353           0 : free_rpc_bdev_nvme_attach_controller_ctx(struct rpc_bdev_nvme_attach_controller_ctx *ctx)
     354             : {
     355           0 :         free_rpc_bdev_nvme_attach_controller(&ctx->req);
     356           0 :         free(ctx->names);
     357           0 :         free(ctx);
     358           0 : }
     359             : 
     360             : static void
     361           0 : rpc_bdev_nvme_attach_controller_examined(void *cb_ctx)
     362             : {
     363           0 :         struct rpc_bdev_nvme_attach_controller_ctx *ctx = cb_ctx;
     364           0 :         struct spdk_jsonrpc_request *request = ctx->request;
     365             :         struct spdk_json_write_ctx *w;
     366             :         size_t i;
     367             : 
     368           0 :         w = spdk_jsonrpc_begin_result(request);
     369           0 :         spdk_json_write_array_begin(w);
     370           0 :         for (i = 0; i < ctx->bdev_count; i++) {
     371           0 :                 spdk_json_write_string(w, ctx->names[i]);
     372           0 :         }
     373           0 :         spdk_json_write_array_end(w);
     374           0 :         spdk_jsonrpc_end_result(request, w);
     375             : 
     376           0 :         free_rpc_bdev_nvme_attach_controller_ctx(ctx);
     377           0 : }
     378             : 
     379             : static void
     380           0 : rpc_bdev_nvme_attach_controller_done(void *cb_ctx, size_t bdev_count, int rc)
     381             : {
     382           0 :         struct rpc_bdev_nvme_attach_controller_ctx *ctx = cb_ctx;
     383           0 :         struct spdk_jsonrpc_request *request = ctx->request;
     384             : 
     385           0 :         if (rc < 0) {
     386           0 :                 spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
     387           0 :                 free_rpc_bdev_nvme_attach_controller_ctx(ctx);
     388           0 :                 return;
     389             :         }
     390             : 
     391           0 :         ctx->bdev_count = bdev_count;
     392           0 :         spdk_bdev_wait_for_examine(rpc_bdev_nvme_attach_controller_examined, ctx);
     393           0 : }
     394             : 
     395             : static void
     396           0 : rpc_bdev_nvme_attach_controller(struct spdk_jsonrpc_request *request,
     397             :                                 const struct spdk_json_val *params)
     398             : {
     399             :         struct rpc_bdev_nvme_attach_controller_ctx *ctx;
     400           0 :         struct spdk_nvme_transport_id trid = {};
     401             :         const struct spdk_nvme_ctrlr_opts *drv_opts;
     402             :         const struct spdk_nvme_transport_id *ctrlr_trid;
     403           0 :         struct nvme_ctrlr *ctrlr = NULL;
     404             :         size_t len, maxlen;
     405             :         int rc;
     406             : 
     407           0 :         ctx = calloc(1, sizeof(*ctx));
     408           0 :         if (!ctx) {
     409           0 :                 spdk_jsonrpc_send_error_response(request, -ENOMEM, spdk_strerror(ENOMEM));
     410           0 :                 return;
     411             :         }
     412             : 
     413           0 :         spdk_nvme_ctrlr_get_default_ctrlr_opts(&ctx->req.drv_opts, sizeof(ctx->req.drv_opts));
     414           0 :         spdk_bdev_nvme_get_default_ctrlr_opts(&ctx->req.bdev_opts);
     415           0 :         ctx->req.multipath = BDEV_NVME_MP_MODE_MULTIPATH;
     416           0 :         ctx->req.max_bdevs = DEFAULT_MAX_BDEVS_PER_RPC;
     417             : 
     418           0 :         if (spdk_json_decode_object(params, rpc_bdev_nvme_attach_controller_decoders,
     419             :                                     SPDK_COUNTOF(rpc_bdev_nvme_attach_controller_decoders),
     420           0 :                                     &ctx->req)) {
     421           0 :                 SPDK_ERRLOG("spdk_json_decode_object failed\n");
     422           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
     423             :                                                  "spdk_json_decode_object failed");
     424           0 :                 goto cleanup;
     425             :         }
     426             : 
     427           0 :         if (ctx->req.max_bdevs == 0) {
     428           0 :                 spdk_jsonrpc_send_error_response(request, -EINVAL, "max_bdevs cannot be zero");
     429           0 :                 goto cleanup;
     430             :         }
     431             : 
     432           0 :         ctx->names = calloc(ctx->req.max_bdevs, sizeof(char *));
     433           0 :         if (ctx->names == NULL) {
     434           0 :                 spdk_jsonrpc_send_error_response(request, -ENOMEM, spdk_strerror(ENOMEM));
     435           0 :                 goto cleanup;
     436             :         }
     437             : 
     438             :         /* Parse trstring */
     439           0 :         rc = spdk_nvme_transport_id_populate_trstring(&trid, ctx->req.trtype);
     440           0 :         if (rc < 0) {
     441           0 :                 SPDK_ERRLOG("Failed to parse trtype: %s\n", ctx->req.trtype);
     442           0 :                 spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "Failed to parse trtype: %s",
     443           0 :                                                      ctx->req.trtype);
     444           0 :                 goto cleanup;
     445             :         }
     446             : 
     447             :         /* Parse trtype */
     448           0 :         rc = spdk_nvme_transport_id_parse_trtype(&trid.trtype, ctx->req.trtype);
     449           0 :         assert(rc == 0);
     450             : 
     451             :         /* Parse traddr */
     452           0 :         maxlen = sizeof(trid.traddr);
     453           0 :         len = strnlen(ctx->req.traddr, maxlen);
     454           0 :         if (len == maxlen) {
     455           0 :                 spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "traddr too long: %s",
     456           0 :                                                      ctx->req.traddr);
     457           0 :                 goto cleanup;
     458             :         }
     459           0 :         memcpy(trid.traddr, ctx->req.traddr, len + 1);
     460             : 
     461             :         /* Parse adrfam */
     462           0 :         if (ctx->req.adrfam) {
     463           0 :                 rc = spdk_nvme_transport_id_parse_adrfam(&trid.adrfam, ctx->req.adrfam);
     464           0 :                 if (rc < 0) {
     465           0 :                         SPDK_ERRLOG("Failed to parse adrfam: %s\n", ctx->req.adrfam);
     466           0 :                         spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "Failed to parse adrfam: %s",
     467           0 :                                                              ctx->req.adrfam);
     468           0 :                         goto cleanup;
     469             :                 }
     470           0 :         }
     471             : 
     472             :         /* Parse trsvcid */
     473           0 :         if (ctx->req.trsvcid) {
     474           0 :                 maxlen = sizeof(trid.trsvcid);
     475           0 :                 len = strnlen(ctx->req.trsvcid, maxlen);
     476           0 :                 if (len == maxlen) {
     477           0 :                         spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "trsvcid too long: %s",
     478           0 :                                                              ctx->req.trsvcid);
     479           0 :                         goto cleanup;
     480             :                 }
     481           0 :                 memcpy(trid.trsvcid, ctx->req.trsvcid, len + 1);
     482           0 :         }
     483             : 
     484             :         /* Parse priority for the NVMe-oF transport connection */
     485           0 :         if (ctx->req.priority) {
     486           0 :                 trid.priority = spdk_strtol(ctx->req.priority, 10);
     487           0 :         }
     488             : 
     489             :         /* Parse subnqn */
     490           0 :         if (ctx->req.subnqn) {
     491           0 :                 maxlen = sizeof(trid.subnqn);
     492           0 :                 len = strnlen(ctx->req.subnqn, maxlen);
     493           0 :                 if (len == maxlen) {
     494           0 :                         spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "subnqn too long: %s",
     495           0 :                                                              ctx->req.subnqn);
     496           0 :                         goto cleanup;
     497             :                 }
     498           0 :                 memcpy(trid.subnqn, ctx->req.subnqn, len + 1);
     499           0 :         }
     500             : 
     501           0 :         if (ctx->req.hostnqn) {
     502           0 :                 maxlen = sizeof(ctx->req.drv_opts.hostnqn);
     503           0 :                 len = strnlen(ctx->req.hostnqn, maxlen);
     504           0 :                 if (len == maxlen) {
     505           0 :                         spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "hostnqn too long: %s",
     506           0 :                                                              ctx->req.hostnqn);
     507           0 :                         goto cleanup;
     508             :                 }
     509           0 :                 memcpy(ctx->req.drv_opts.hostnqn, ctx->req.hostnqn, len + 1);
     510           0 :         }
     511             : 
     512           0 :         if (ctx->req.psk) {
     513           0 :                 if (!g_tls_log) {
     514           0 :                         SPDK_NOTICELOG("TLS support is considered experimental\n");
     515           0 :                         g_tls_log = true;
     516           0 :                 }
     517           0 :         }
     518             : 
     519           0 :         if (ctx->req.hostaddr) {
     520           0 :                 maxlen = sizeof(ctx->req.drv_opts.src_addr);
     521           0 :                 len = strnlen(ctx->req.hostaddr, maxlen);
     522           0 :                 if (len == maxlen) {
     523           0 :                         spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "hostaddr too long: %s",
     524           0 :                                                              ctx->req.hostaddr);
     525           0 :                         goto cleanup;
     526             :                 }
     527           0 :                 snprintf(ctx->req.drv_opts.src_addr, maxlen, "%s", ctx->req.hostaddr);
     528           0 :         }
     529             : 
     530           0 :         if (ctx->req.hostsvcid) {
     531           0 :                 maxlen = sizeof(ctx->req.drv_opts.src_svcid);
     532           0 :                 len = strnlen(ctx->req.hostsvcid, maxlen);
     533           0 :                 if (len == maxlen) {
     534           0 :                         spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "hostsvcid too long: %s",
     535           0 :                                                              ctx->req.hostsvcid);
     536           0 :                         goto cleanup;
     537             :                 }
     538           0 :                 snprintf(ctx->req.drv_opts.src_svcid, maxlen, "%s", ctx->req.hostsvcid);
     539           0 :         }
     540             : 
     541           0 :         ctrlr = nvme_ctrlr_get_by_name(ctx->req.name);
     542             : 
     543           0 :         if (ctrlr) {
     544             :                 /* This controller already exists. Check what the user wants to do. */
     545           0 :                 if (ctx->req.multipath == BDEV_NVME_MP_MODE_DISABLE) {
     546             :                         /* The user does not want to do any form of multipathing. */
     547           0 :                         spdk_jsonrpc_send_error_response_fmt(request, -EALREADY,
     548             :                                                              "A controller named %s already exists and multipath is disabled",
     549           0 :                                                              ctx->req.name);
     550           0 :                         goto cleanup;
     551             :                 }
     552             : 
     553           0 :                 assert(ctx->req.multipath == BDEV_NVME_MP_MODE_FAILOVER ||
     554             :                        ctx->req.multipath == BDEV_NVME_MP_MODE_MULTIPATH);
     555             : 
     556             :                 /* The user wants to add this as a failover path or add this to create multipath. */
     557           0 :                 drv_opts = spdk_nvme_ctrlr_get_opts(ctrlr->ctrlr);
     558           0 :                 ctrlr_trid = spdk_nvme_ctrlr_get_transport_id(ctrlr->ctrlr);
     559             : 
     560           0 :                 if (strncmp(trid.traddr, ctrlr_trid->traddr, sizeof(trid.traddr)) == 0 &&
     561           0 :                     strncmp(trid.trsvcid, ctrlr_trid->trsvcid, sizeof(trid.trsvcid)) == 0 &&
     562           0 :                     strncmp(ctx->req.drv_opts.src_addr, drv_opts->src_addr, sizeof(drv_opts->src_addr)) == 0 &&
     563           0 :                     strncmp(ctx->req.drv_opts.src_svcid, drv_opts->src_svcid, sizeof(drv_opts->src_svcid)) == 0) {
     564             :                         /* Exactly same network path can't be added a second time */
     565           0 :                         spdk_jsonrpc_send_error_response_fmt(request, -EALREADY,
     566             :                                                              "A controller named %s already exists with the specified network path",
     567           0 :                                                              ctx->req.name);
     568           0 :                         goto cleanup;
     569             :                 }
     570             : 
     571           0 :                 if (strncmp(trid.subnqn,
     572           0 :                             ctrlr_trid->subnqn,
     573           0 :                             SPDK_NVMF_NQN_MAX_LEN) != 0) {
     574             :                         /* Different SUBNQN is not allowed when specifying the same controller name. */
     575           0 :                         spdk_jsonrpc_send_error_response_fmt(request, -EINVAL,
     576             :                                                              "A controller named %s already exists, but uses a different subnqn (%s)",
     577           0 :                                                              ctx->req.name, ctrlr_trid->subnqn);
     578           0 :                         goto cleanup;
     579             :                 }
     580             : 
     581           0 :                 if (strncmp(ctx->req.drv_opts.hostnqn, drv_opts->hostnqn, SPDK_NVMF_NQN_MAX_LEN) != 0) {
     582             :                         /* Different HOSTNQN is not allowed when specifying the same controller name. */
     583           0 :                         spdk_jsonrpc_send_error_response_fmt(request, -EINVAL,
     584             :                                                              "A controller named %s already exists, but uses a different hostnqn (%s)",
     585           0 :                                                              ctx->req.name, drv_opts->hostnqn);
     586           0 :                         goto cleanup;
     587             :                 }
     588             : 
     589           0 :                 if (ctx->req.bdev_opts.prchk_flags) {
     590           0 :                         spdk_jsonrpc_send_error_response_fmt(request, -EINVAL,
     591             :                                                              "A controller named %s already exists. To add a path, do not specify PI options.",
     592           0 :                                                              ctx->req.name);
     593           0 :                         goto cleanup;
     594             :                 }
     595             : 
     596           0 :                 ctx->req.bdev_opts.prchk_flags = ctrlr->opts.prchk_flags;
     597           0 :         }
     598             : 
     599           0 :         if (ctx->req.multipath != BDEV_NVME_MP_MODE_MULTIPATH) {
     600           0 :                 ctx->req.bdev_opts.multipath = false;
     601           0 :         }
     602             : 
     603           0 :         if (ctx->req.drv_opts.num_io_queues == 0 || ctx->req.drv_opts.num_io_queues > UINT16_MAX + 1) {
     604           0 :                 spdk_jsonrpc_send_error_response_fmt(request, -EINVAL,
     605             :                                                      "num_io_queues out of bounds, min: %u max: %u",
     606             :                                                      1, UINT16_MAX + 1);
     607           0 :                 goto cleanup;
     608             :         }
     609             : 
     610           0 :         ctx->request = request;
     611             :         /* Should already be zero due to the calloc(), but set explicitly for clarity. */
     612           0 :         ctx->req.bdev_opts.from_discovery_service = false;
     613           0 :         ctx->req.bdev_opts.psk = ctx->req.psk;
     614           0 :         ctx->req.bdev_opts.dhchap_key = ctx->req.dhchap_key;
     615           0 :         ctx->req.bdev_opts.dhchap_ctrlr_key = ctx->req.dhchap_ctrlr_key;
     616           0 :         rc = spdk_bdev_nvme_create(&trid, ctx->req.name, ctx->names, ctx->req.max_bdevs,
     617           0 :                                    rpc_bdev_nvme_attach_controller_done, ctx, &ctx->req.drv_opts,
     618           0 :                                    &ctx->req.bdev_opts);
     619           0 :         if (rc) {
     620           0 :                 spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
     621           0 :                 goto cleanup;
     622             :         }
     623             : 
     624           0 :         return;
     625             : 
     626             : cleanup:
     627           0 :         free_rpc_bdev_nvme_attach_controller_ctx(ctx);
     628           0 : }
     629           0 : SPDK_RPC_REGISTER("bdev_nvme_attach_controller", rpc_bdev_nvme_attach_controller,
     630             :                   SPDK_RPC_RUNTIME)
     631             : 
     632             : static void
     633           0 : rpc_dump_nvme_bdev_controller_info(struct nvme_bdev_ctrlr *nbdev_ctrlr, void *ctx)
     634             : {
     635           0 :         struct spdk_json_write_ctx      *w = ctx;
     636             :         struct nvme_ctrlr               *nvme_ctrlr;
     637             : 
     638           0 :         spdk_json_write_object_begin(w);
     639           0 :         spdk_json_write_named_string(w, "name", nbdev_ctrlr->name);
     640             : 
     641           0 :         spdk_json_write_named_array_begin(w, "ctrlrs");
     642           0 :         TAILQ_FOREACH(nvme_ctrlr, &nbdev_ctrlr->ctrlrs, tailq) {
     643           0 :                 nvme_ctrlr_info_json(w, nvme_ctrlr);
     644           0 :         }
     645           0 :         spdk_json_write_array_end(w);
     646           0 :         spdk_json_write_object_end(w);
     647           0 : }
     648             : 
     649             : struct rpc_bdev_nvme_get_controllers {
     650             :         char *name;
     651             : };
     652             : 
     653             : static void
     654           0 : free_rpc_bdev_nvme_get_controllers(struct rpc_bdev_nvme_get_controllers *r)
     655             : {
     656           0 :         free(r->name);
     657           0 : }
     658             : 
     659             : static const struct spdk_json_object_decoder rpc_bdev_nvme_get_controllers_decoders[] = {
     660             :         {"name", offsetof(struct rpc_bdev_nvme_get_controllers, name), spdk_json_decode_string, true},
     661             : };
     662             : 
     663             : static void
     664           0 : rpc_bdev_nvme_get_controllers(struct spdk_jsonrpc_request *request,
     665             :                               const struct spdk_json_val *params)
     666             : {
     667           0 :         struct rpc_bdev_nvme_get_controllers req = {};
     668             :         struct spdk_json_write_ctx *w;
     669           0 :         struct nvme_bdev_ctrlr *nbdev_ctrlr = NULL;
     670             : 
     671           0 :         if (params && spdk_json_decode_object(params, rpc_bdev_nvme_get_controllers_decoders,
     672             :                                               SPDK_COUNTOF(rpc_bdev_nvme_get_controllers_decoders),
     673             :                                               &req)) {
     674           0 :                 SPDK_ERRLOG("spdk_json_decode_object failed\n");
     675           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
     676             :                                                  "spdk_json_decode_object failed");
     677           0 :                 goto cleanup;
     678             :         }
     679             : 
     680           0 :         if (req.name) {
     681           0 :                 nbdev_ctrlr = nvme_bdev_ctrlr_get_by_name(req.name);
     682           0 :                 if (nbdev_ctrlr == NULL) {
     683           0 :                         SPDK_ERRLOG("ctrlr '%s' does not exist\n", req.name);
     684           0 :                         spdk_jsonrpc_send_error_response_fmt(request, EINVAL, "Controller %s does not exist", req.name);
     685           0 :                         goto cleanup;
     686             :                 }
     687           0 :         }
     688             : 
     689           0 :         w = spdk_jsonrpc_begin_result(request);
     690           0 :         spdk_json_write_array_begin(w);
     691             : 
     692           0 :         if (nbdev_ctrlr != NULL) {
     693           0 :                 rpc_dump_nvme_bdev_controller_info(nbdev_ctrlr, w);
     694           0 :         } else {
     695           0 :                 nvme_bdev_ctrlr_for_each(rpc_dump_nvme_bdev_controller_info, w);
     696             :         }
     697             : 
     698           0 :         spdk_json_write_array_end(w);
     699             : 
     700           0 :         spdk_jsonrpc_end_result(request, w);
     701             : 
     702             : cleanup:
     703           0 :         free_rpc_bdev_nvme_get_controllers(&req);
     704           0 : }
     705           0 : SPDK_RPC_REGISTER("bdev_nvme_get_controllers", rpc_bdev_nvme_get_controllers, SPDK_RPC_RUNTIME)
     706             : 
     707             : struct rpc_bdev_nvme_detach_controller {
     708             :         char *name;
     709             :         char *trtype;
     710             :         char *adrfam;
     711             :         char *traddr;
     712             :         char *trsvcid;
     713             :         char *subnqn;
     714             :         char *hostaddr;
     715             :         char *hostsvcid;
     716             : };
     717             : 
     718             : static void
     719           0 : free_rpc_bdev_nvme_detach_controller(struct rpc_bdev_nvme_detach_controller *req)
     720             : {
     721           0 :         free(req->name);
     722           0 :         free(req->trtype);
     723           0 :         free(req->adrfam);
     724           0 :         free(req->traddr);
     725           0 :         free(req->trsvcid);
     726           0 :         free(req->subnqn);
     727           0 :         free(req->hostaddr);
     728           0 :         free(req->hostsvcid);
     729           0 : }
     730             : 
     731             : static const struct spdk_json_object_decoder rpc_bdev_nvme_detach_controller_decoders[] = {
     732             :         {"name", offsetof(struct rpc_bdev_nvme_detach_controller, name), spdk_json_decode_string},
     733             :         {"trtype", offsetof(struct rpc_bdev_nvme_detach_controller, trtype), spdk_json_decode_string, true},
     734             :         {"traddr", offsetof(struct rpc_bdev_nvme_detach_controller, traddr), spdk_json_decode_string, true},
     735             :         {"adrfam", offsetof(struct rpc_bdev_nvme_detach_controller, adrfam), spdk_json_decode_string, true},
     736             :         {"trsvcid", offsetof(struct rpc_bdev_nvme_detach_controller, trsvcid), spdk_json_decode_string, true},
     737             :         {"subnqn", offsetof(struct rpc_bdev_nvme_detach_controller, subnqn), spdk_json_decode_string, true},
     738             :         {"hostaddr", offsetof(struct rpc_bdev_nvme_detach_controller, hostaddr), spdk_json_decode_string, true},
     739             :         {"hostsvcid", offsetof(struct rpc_bdev_nvme_detach_controller, hostsvcid), spdk_json_decode_string, true},
     740             : };
     741             : 
     742             : static void
     743           0 : rpc_bdev_nvme_detach_controller_done(void *arg, int rc)
     744             : {
     745           0 :         struct spdk_jsonrpc_request *request = arg;
     746             : 
     747           0 :         if (rc == 0) {
     748           0 :                 spdk_jsonrpc_send_bool_response(request, true);
     749           0 :         } else {
     750           0 :                 spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
     751             :         }
     752           0 : }
     753             : 
     754             : static void
     755           0 : rpc_bdev_nvme_detach_controller(struct spdk_jsonrpc_request *request,
     756             :                                 const struct spdk_json_val *params)
     757             : {
     758           0 :         struct rpc_bdev_nvme_detach_controller req = {NULL};
     759           0 :         struct nvme_path_id path = {};
     760             :         size_t len, maxlen;
     761           0 :         int rc = 0;
     762             : 
     763           0 :         if (spdk_json_decode_object(params, rpc_bdev_nvme_detach_controller_decoders,
     764             :                                     SPDK_COUNTOF(rpc_bdev_nvme_detach_controller_decoders),
     765             :                                     &req)) {
     766           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
     767             :                                                  "spdk_json_decode_object failed");
     768           0 :                 goto cleanup;
     769             :         }
     770             : 
     771           0 :         if (req.trtype != NULL) {
     772           0 :                 rc = spdk_nvme_transport_id_populate_trstring(&path.trid, req.trtype);
     773           0 :                 if (rc < 0) {
     774           0 :                         SPDK_ERRLOG("Failed to parse trtype: %s\n", req.trtype);
     775           0 :                         spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "Failed to parse trtype: %s",
     776           0 :                                                              req.trtype);
     777           0 :                         goto cleanup;
     778             :                 }
     779             : 
     780           0 :                 rc = spdk_nvme_transport_id_parse_trtype(&path.trid.trtype, req.trtype);
     781           0 :                 if (rc < 0) {
     782           0 :                         SPDK_ERRLOG("Failed to parse trtype: %s\n", req.trtype);
     783           0 :                         spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "Failed to parse trtype: %s",
     784           0 :                                                              req.trtype);
     785           0 :                         goto cleanup;
     786             :                 }
     787           0 :         }
     788             : 
     789           0 :         if (req.traddr != NULL) {
     790           0 :                 maxlen = sizeof(path.trid.traddr);
     791           0 :                 len = strnlen(req.traddr, maxlen);
     792           0 :                 if (len == maxlen) {
     793           0 :                         spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "traddr too long: %s",
     794           0 :                                                              req.traddr);
     795           0 :                         goto cleanup;
     796             :                 }
     797           0 :                 memcpy(path.trid.traddr, req.traddr, len + 1);
     798           0 :         }
     799             : 
     800           0 :         if (req.adrfam != NULL) {
     801           0 :                 rc = spdk_nvme_transport_id_parse_adrfam(&path.trid.adrfam, req.adrfam);
     802           0 :                 if (rc < 0) {
     803           0 :                         SPDK_ERRLOG("Failed to parse adrfam: %s\n", req.adrfam);
     804           0 :                         spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "Failed to parse adrfam: %s",
     805           0 :                                                              req.adrfam);
     806           0 :                         goto cleanup;
     807             :                 }
     808           0 :         }
     809             : 
     810           0 :         if (req.trsvcid != NULL) {
     811           0 :                 maxlen = sizeof(path.trid.trsvcid);
     812           0 :                 len = strnlen(req.trsvcid, maxlen);
     813           0 :                 if (len == maxlen) {
     814           0 :                         spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "trsvcid too long: %s",
     815           0 :                                                              req.trsvcid);
     816           0 :                         goto cleanup;
     817             :                 }
     818           0 :                 memcpy(path.trid.trsvcid, req.trsvcid, len + 1);
     819           0 :         }
     820             : 
     821             :         /* Parse subnqn */
     822           0 :         if (req.subnqn != NULL) {
     823           0 :                 maxlen = sizeof(path.trid.subnqn);
     824           0 :                 len = strnlen(req.subnqn, maxlen);
     825           0 :                 if (len == maxlen) {
     826           0 :                         spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "subnqn too long: %s",
     827           0 :                                                              req.subnqn);
     828           0 :                         goto cleanup;
     829             :                 }
     830           0 :                 memcpy(path.trid.subnqn, req.subnqn, len + 1);
     831           0 :         }
     832             : 
     833           0 :         if (req.hostaddr) {
     834           0 :                 maxlen = sizeof(path.hostid.hostaddr);
     835           0 :                 len = strnlen(req.hostaddr, maxlen);
     836           0 :                 if (len == maxlen) {
     837           0 :                         spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "hostaddr too long: %s",
     838           0 :                                                              req.hostaddr);
     839           0 :                         goto cleanup;
     840             :                 }
     841           0 :                 snprintf(path.hostid.hostaddr, maxlen, "%s", req.hostaddr);
     842           0 :         }
     843             : 
     844           0 :         if (req.hostsvcid) {
     845           0 :                 maxlen = sizeof(path.hostid.hostsvcid);
     846           0 :                 len = strnlen(req.hostsvcid, maxlen);
     847           0 :                 if (len == maxlen) {
     848           0 :                         spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "hostsvcid too long: %s",
     849           0 :                                                              req.hostsvcid);
     850           0 :                         goto cleanup;
     851             :                 }
     852           0 :                 snprintf(path.hostid.hostsvcid, maxlen, "%s", req.hostsvcid);
     853           0 :         }
     854             : 
     855           0 :         rc = bdev_nvme_delete(req.name, &path, rpc_bdev_nvme_detach_controller_done, request);
     856             : 
     857           0 :         if (rc != 0) {
     858           0 :                 spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
     859           0 :         }
     860             : 
     861             : cleanup:
     862           0 :         free_rpc_bdev_nvme_detach_controller(&req);
     863           0 : }
     864           0 : SPDK_RPC_REGISTER("bdev_nvme_detach_controller", rpc_bdev_nvme_detach_controller,
     865             :                   SPDK_RPC_RUNTIME)
     866             : 
     867             : struct rpc_apply_firmware {
     868             :         char *filename;
     869             :         char *bdev_name;
     870             : };
     871             : 
     872             : static void
     873           0 : free_rpc_apply_firmware(struct rpc_apply_firmware *req)
     874             : {
     875           0 :         free(req->filename);
     876           0 :         free(req->bdev_name);
     877           0 : }
     878             : 
     879             : static const struct spdk_json_object_decoder rpc_apply_firmware_decoders[] = {
     880             :         {"filename", offsetof(struct rpc_apply_firmware, filename), spdk_json_decode_string},
     881             :         {"bdev_name", offsetof(struct rpc_apply_firmware, bdev_name), spdk_json_decode_string},
     882             : };
     883             : 
     884             : struct firmware_update_info {
     885             :         void                            *fw_image;
     886             :         void                            *p;
     887             :         unsigned int                    size;
     888             :         unsigned int                    size_remaining;
     889             :         unsigned int                    offset;
     890             :         unsigned int                    transfer;
     891             :         bool                            success;
     892             : 
     893             :         struct spdk_bdev_desc           *desc;
     894             :         struct spdk_io_channel          *ch;
     895             :         struct spdk_thread              *orig_thread;
     896             :         struct spdk_jsonrpc_request     *request;
     897             :         struct spdk_nvme_ctrlr          *ctrlr;
     898             :         struct rpc_apply_firmware       req;
     899             : };
     900             : 
     901             : static void
     902           0 : apply_firmware_cleanup(struct firmware_update_info *firm_ctx)
     903             : {
     904           0 :         assert(firm_ctx != NULL);
     905           0 :         assert(firm_ctx->orig_thread == spdk_get_thread());
     906             : 
     907           0 :         if (firm_ctx->fw_image) {
     908           0 :                 spdk_free(firm_ctx->fw_image);
     909           0 :         }
     910             : 
     911           0 :         free_rpc_apply_firmware(&firm_ctx->req);
     912             : 
     913           0 :         if (firm_ctx->ch) {
     914           0 :                 spdk_put_io_channel(firm_ctx->ch);
     915           0 :         }
     916             : 
     917           0 :         if (firm_ctx->desc) {
     918           0 :                 spdk_bdev_close(firm_ctx->desc);
     919           0 :         }
     920             : 
     921           0 :         free(firm_ctx);
     922           0 : }
     923             : 
     924             : static void
     925           0 : _apply_firmware_complete_reset(void *ctx)
     926             : {
     927             :         struct spdk_json_write_ctx              *w;
     928           0 :         struct firmware_update_info *firm_ctx = ctx;
     929             : 
     930           0 :         assert(firm_ctx->orig_thread == spdk_get_thread());
     931             : 
     932           0 :         if (!firm_ctx->success) {
     933           0 :                 spdk_jsonrpc_send_error_response(firm_ctx->request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
     934             :                                                  "firmware commit failed.");
     935           0 :                 apply_firmware_cleanup(firm_ctx);
     936           0 :                 return;
     937             :         }
     938             : 
     939           0 :         if (spdk_nvme_ctrlr_reset(firm_ctx->ctrlr) != 0) {
     940           0 :                 spdk_jsonrpc_send_error_response(firm_ctx->request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
     941             :                                                  "Controller reset failed.");
     942           0 :                 apply_firmware_cleanup(firm_ctx);
     943           0 :                 return;
     944             :         }
     945             : 
     946           0 :         w = spdk_jsonrpc_begin_result(firm_ctx->request);
     947           0 :         spdk_json_write_string(w, "firmware commit succeeded. Controller reset in progress.");
     948           0 :         spdk_jsonrpc_end_result(firm_ctx->request, w);
     949           0 :         apply_firmware_cleanup(firm_ctx);
     950           0 : }
     951             : 
     952             : static void
     953           0 : apply_firmware_complete_reset(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg)
     954             : {
     955           0 :         struct firmware_update_info *firm_ctx = cb_arg;
     956             : 
     957           0 :         spdk_bdev_free_io(bdev_io);
     958             : 
     959           0 :         firm_ctx->success = success;
     960             : 
     961           0 :         spdk_thread_exec_msg(firm_ctx->orig_thread, _apply_firmware_complete_reset, firm_ctx);
     962           0 : }
     963             : 
     964             : static void apply_firmware_complete(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg);
     965             : 
     966             : static void
     967           0 : _apply_firmware_complete(void *ctx)
     968             : {
     969           0 :         struct spdk_nvme_cmd                    cmd = {};
     970             :         struct spdk_nvme_fw_commit              fw_commit;
     971           0 :         int                                     slot = 0;
     972             :         int                                     rc;
     973           0 :         struct firmware_update_info *firm_ctx = ctx;
     974           0 :         enum spdk_nvme_fw_commit_action commit_action = SPDK_NVME_FW_COMMIT_REPLACE_AND_ENABLE_IMG;
     975             : 
     976           0 :         assert(firm_ctx->orig_thread == spdk_get_thread());
     977             : 
     978           0 :         if (!firm_ctx->success) {
     979           0 :                 spdk_jsonrpc_send_error_response(firm_ctx->request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
     980             :                                                  "firmware download failed .");
     981           0 :                 apply_firmware_cleanup(firm_ctx);
     982           0 :                 return;
     983             :         }
     984             : 
     985           0 :         firm_ctx->p += firm_ctx->transfer;
     986           0 :         firm_ctx->offset += firm_ctx->transfer;
     987           0 :         firm_ctx->size_remaining -= firm_ctx->transfer;
     988             : 
     989           0 :         switch (firm_ctx->size_remaining) {
     990             :         case 0:
     991             :                 /* firmware download completed. Commit firmware */
     992           0 :                 memset(&fw_commit, 0, sizeof(struct spdk_nvme_fw_commit));
     993           0 :                 fw_commit.fs = slot;
     994           0 :                 fw_commit.ca = commit_action;
     995             : 
     996           0 :                 cmd.opc = SPDK_NVME_OPC_FIRMWARE_COMMIT;
     997           0 :                 memcpy(&cmd.cdw10, &fw_commit, sizeof(uint32_t));
     998           0 :                 rc = spdk_bdev_nvme_admin_passthru(firm_ctx->desc, firm_ctx->ch, &cmd, NULL, 0,
     999           0 :                                                    apply_firmware_complete_reset, firm_ctx);
    1000           0 :                 if (rc) {
    1001           0 :                         spdk_jsonrpc_send_error_response(firm_ctx->request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    1002             :                                                          "firmware commit failed.");
    1003           0 :                         apply_firmware_cleanup(firm_ctx);
    1004           0 :                         return;
    1005             :                 }
    1006           0 :                 break;
    1007             :         default:
    1008           0 :                 firm_ctx->transfer = spdk_min(firm_ctx->size_remaining, 4096);
    1009           0 :                 cmd.opc = SPDK_NVME_OPC_FIRMWARE_IMAGE_DOWNLOAD;
    1010             : 
    1011           0 :                 cmd.cdw10 = spdk_nvme_bytes_to_numd(firm_ctx->transfer);
    1012           0 :                 cmd.cdw11 = firm_ctx->offset >> 2;
    1013           0 :                 rc = spdk_bdev_nvme_admin_passthru(firm_ctx->desc, firm_ctx->ch, &cmd, firm_ctx->p,
    1014           0 :                                                    firm_ctx->transfer, apply_firmware_complete, firm_ctx);
    1015           0 :                 if (rc) {
    1016           0 :                         spdk_jsonrpc_send_error_response(firm_ctx->request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    1017             :                                                          "firmware download failed.");
    1018           0 :                         apply_firmware_cleanup(firm_ctx);
    1019           0 :                         return;
    1020             :                 }
    1021           0 :                 break;
    1022             :         }
    1023           0 : }
    1024             : 
    1025             : static void
    1026           0 : apply_firmware_complete(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg)
    1027             : {
    1028           0 :         struct firmware_update_info *firm_ctx = cb_arg;
    1029             : 
    1030           0 :         spdk_bdev_free_io(bdev_io);
    1031             : 
    1032           0 :         firm_ctx->success = success;
    1033             : 
    1034           0 :         spdk_thread_exec_msg(firm_ctx->orig_thread, _apply_firmware_complete, firm_ctx);
    1035           0 : }
    1036             : 
    1037             : static void
    1038           0 : apply_firmware_open_cb(enum spdk_bdev_event_type type, struct spdk_bdev *bdev, void *event_ctx)
    1039             : {
    1040           0 : }
    1041             : 
    1042             : static void
    1043           0 : rpc_bdev_nvme_apply_firmware(struct spdk_jsonrpc_request *request,
    1044             :                              const struct spdk_json_val *params)
    1045             : {
    1046             :         int                                     rc;
    1047           0 :         int                                     fd = -1;
    1048             :         struct stat                             fw_stat;
    1049             :         struct spdk_bdev                        *bdev;
    1050           0 :         struct spdk_nvme_cmd                    cmd = {};
    1051             :         struct firmware_update_info             *firm_ctx;
    1052             : 
    1053           0 :         firm_ctx = calloc(1, sizeof(struct firmware_update_info));
    1054           0 :         if (!firm_ctx) {
    1055           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    1056             :                                                  "Memory allocation error.");
    1057           0 :                 return;
    1058             :         }
    1059           0 :         firm_ctx->fw_image = NULL;
    1060           0 :         firm_ctx->request = request;
    1061           0 :         firm_ctx->orig_thread = spdk_get_thread();
    1062             : 
    1063           0 :         if (spdk_json_decode_object(params, rpc_apply_firmware_decoders,
    1064           0 :                                     SPDK_COUNTOF(rpc_apply_firmware_decoders), &firm_ctx->req)) {
    1065           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    1066             :                                                  "spdk_json_decode_object failed.");
    1067           0 :                 goto err;
    1068             :         }
    1069             : 
    1070           0 :         if (spdk_bdev_open_ext(firm_ctx->req.bdev_name, true, apply_firmware_open_cb, NULL,
    1071           0 :                                &firm_ctx->desc) != 0) {
    1072           0 :                 spdk_jsonrpc_send_error_response_fmt(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    1073             :                                                      "bdev %s could not be opened",
    1074           0 :                                                      firm_ctx->req.bdev_name);
    1075           0 :                 goto err;
    1076             :         }
    1077           0 :         bdev = spdk_bdev_desc_get_bdev(firm_ctx->desc);
    1078             : 
    1079           0 :         if ((firm_ctx->ctrlr = bdev_nvme_get_ctrlr(bdev)) == NULL) {
    1080           0 :                 spdk_jsonrpc_send_error_response_fmt(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    1081             :                                                      "Controller information for %s were not found.",
    1082           0 :                                                      firm_ctx->req.bdev_name);
    1083           0 :                 goto err;
    1084             :         }
    1085             : 
    1086           0 :         firm_ctx->ch = spdk_bdev_get_io_channel(firm_ctx->desc);
    1087           0 :         if (!firm_ctx->ch) {
    1088           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    1089             :                                                  "No channels were found.");
    1090           0 :                 goto err;
    1091             :         }
    1092             : 
    1093           0 :         fd = open(firm_ctx->req.filename, O_RDONLY);
    1094           0 :         if (fd < 0) {
    1095           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    1096             :                                                  "open file failed.");
    1097           0 :                 goto err;
    1098             :         }
    1099             : 
    1100           0 :         rc = fstat(fd, &fw_stat);
    1101           0 :         if (rc < 0) {
    1102           0 :                 close(fd);
    1103           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    1104             :                                                  "fstat failed.");
    1105           0 :                 goto err;
    1106             :         }
    1107             : 
    1108           0 :         firm_ctx->size = fw_stat.st_size;
    1109           0 :         if (fw_stat.st_size % 4) {
    1110           0 :                 close(fd);
    1111           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    1112             :                                                  "Firmware image size is not multiple of 4.");
    1113           0 :                 goto err;
    1114             :         }
    1115             : 
    1116           0 :         firm_ctx->fw_image = spdk_zmalloc(firm_ctx->size, 4096, NULL,
    1117             :                                           SPDK_ENV_LCORE_ID_ANY, SPDK_MALLOC_DMA);
    1118           0 :         if (!firm_ctx->fw_image) {
    1119           0 :                 close(fd);
    1120           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    1121             :                                                  "Memory allocation error.");
    1122           0 :                 goto err;
    1123             :         }
    1124           0 :         firm_ctx->p = firm_ctx->fw_image;
    1125             : 
    1126           0 :         if (read(fd, firm_ctx->p, firm_ctx->size) != ((ssize_t)(firm_ctx->size))) {
    1127           0 :                 close(fd);
    1128           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    1129             :                                                  "Read firmware image failed!");
    1130           0 :                 goto err;
    1131             :         }
    1132           0 :         close(fd);
    1133             : 
    1134           0 :         firm_ctx->offset = 0;
    1135           0 :         firm_ctx->size_remaining = firm_ctx->size;
    1136           0 :         firm_ctx->transfer = spdk_min(firm_ctx->size_remaining, 4096);
    1137             : 
    1138           0 :         cmd.opc = SPDK_NVME_OPC_FIRMWARE_IMAGE_DOWNLOAD;
    1139           0 :         cmd.cdw10 = spdk_nvme_bytes_to_numd(firm_ctx->transfer);
    1140           0 :         cmd.cdw11 = firm_ctx->offset >> 2;
    1141             : 
    1142           0 :         rc = spdk_bdev_nvme_admin_passthru(firm_ctx->desc, firm_ctx->ch, &cmd, firm_ctx->p,
    1143           0 :                                            firm_ctx->transfer, apply_firmware_complete, firm_ctx);
    1144           0 :         if (rc == 0) {
    1145             :                 /* normal return here. */
    1146           0 :                 return;
    1147             :         }
    1148             : 
    1149           0 :         spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    1150             :                                          "Read firmware image failed!");
    1151             : err:
    1152           0 :         apply_firmware_cleanup(firm_ctx);
    1153           0 : }
    1154           0 : SPDK_RPC_REGISTER("bdev_nvme_apply_firmware", rpc_bdev_nvme_apply_firmware, SPDK_RPC_RUNTIME)
    1155             : 
    1156             : struct rpc_bdev_nvme_transport_stat_ctx {
    1157             :         struct spdk_jsonrpc_request *request;
    1158             :         struct spdk_json_write_ctx *w;
    1159             : };
    1160             : 
    1161             : static void
    1162           0 : rpc_bdev_nvme_rdma_stats(struct spdk_json_write_ctx *w,
    1163             :                          struct spdk_nvme_transport_poll_group_stat *stat)
    1164             : {
    1165             :         struct spdk_nvme_rdma_device_stat *device_stats;
    1166             :         uint32_t i;
    1167             : 
    1168           0 :         spdk_json_write_named_array_begin(w, "devices");
    1169             : 
    1170           0 :         for (i = 0; i < stat->rdma.num_devices; i++) {
    1171           0 :                 device_stats = &stat->rdma.device_stats[i];
    1172           0 :                 spdk_json_write_object_begin(w);
    1173           0 :                 spdk_json_write_named_string(w, "dev_name", device_stats->name);
    1174           0 :                 spdk_json_write_named_uint64(w, "polls", device_stats->polls);
    1175           0 :                 spdk_json_write_named_uint64(w, "idle_polls", device_stats->idle_polls);
    1176           0 :                 spdk_json_write_named_uint64(w, "completions", device_stats->completions);
    1177           0 :                 spdk_json_write_named_uint64(w, "queued_requests", device_stats->queued_requests);
    1178           0 :                 spdk_json_write_named_uint64(w, "total_send_wrs", device_stats->total_send_wrs);
    1179           0 :                 spdk_json_write_named_uint64(w, "send_doorbell_updates", device_stats->send_doorbell_updates);
    1180           0 :                 spdk_json_write_named_uint64(w, "total_recv_wrs", device_stats->total_recv_wrs);
    1181           0 :                 spdk_json_write_named_uint64(w, "recv_doorbell_updates", device_stats->recv_doorbell_updates);
    1182           0 :                 spdk_json_write_object_end(w);
    1183           0 :         }
    1184           0 :         spdk_json_write_array_end(w);
    1185           0 : }
    1186             : 
    1187             : static void
    1188           0 : rpc_bdev_nvme_pcie_stats(struct spdk_json_write_ctx *w,
    1189             :                          struct spdk_nvme_transport_poll_group_stat *stat)
    1190             : {
    1191           0 :         spdk_json_write_named_uint64(w, "polls", stat->pcie.polls);
    1192           0 :         spdk_json_write_named_uint64(w, "idle_polls", stat->pcie.idle_polls);
    1193           0 :         spdk_json_write_named_uint64(w, "completions", stat->pcie.completions);
    1194           0 :         spdk_json_write_named_uint64(w, "cq_mmio_doorbell_updates", stat->pcie.cq_mmio_doorbell_updates);
    1195           0 :         spdk_json_write_named_uint64(w, "cq_shadow_doorbell_updates",
    1196           0 :                                      stat->pcie.cq_shadow_doorbell_updates);
    1197           0 :         spdk_json_write_named_uint64(w, "queued_requests", stat->pcie.queued_requests);
    1198           0 :         spdk_json_write_named_uint64(w, "submitted_requests", stat->pcie.submitted_requests);
    1199           0 :         spdk_json_write_named_uint64(w, "sq_mmio_doorbell_updates", stat->pcie.sq_mmio_doorbell_updates);
    1200           0 :         spdk_json_write_named_uint64(w, "sq_shadow_doorbell_updates",
    1201           0 :                                      stat->pcie.sq_shadow_doorbell_updates);
    1202           0 : }
    1203             : 
    1204             : static void
    1205           0 : rpc_bdev_nvme_tcp_stats(struct spdk_json_write_ctx *w,
    1206             :                         struct spdk_nvme_transport_poll_group_stat *stat)
    1207             : {
    1208           0 :         spdk_json_write_named_uint64(w, "polls", stat->tcp.polls);
    1209           0 :         spdk_json_write_named_uint64(w, "idle_polls", stat->tcp.idle_polls);
    1210           0 :         spdk_json_write_named_uint64(w, "socket_completions", stat->tcp.socket_completions);
    1211           0 :         spdk_json_write_named_uint64(w, "nvme_completions", stat->tcp.nvme_completions);
    1212           0 :         spdk_json_write_named_uint64(w, "queued_requests", stat->tcp.queued_requests);
    1213           0 :         spdk_json_write_named_uint64(w, "submitted_requests", stat->tcp.submitted_requests);
    1214           0 : }
    1215             : 
    1216             : static void
    1217           0 : rpc_bdev_nvme_stats_per_channel(struct spdk_io_channel_iter *i)
    1218             : {
    1219             :         struct rpc_bdev_nvme_transport_stat_ctx *ctx;
    1220             :         struct spdk_io_channel *ch;
    1221             :         struct nvme_poll_group *group;
    1222             :         struct spdk_nvme_poll_group_stat *stat;
    1223             :         struct spdk_nvme_transport_poll_group_stat *tr_stat;
    1224             :         uint32_t j;
    1225             :         int rc;
    1226             : 
    1227           0 :         ctx = spdk_io_channel_iter_get_ctx(i);
    1228           0 :         ch = spdk_io_channel_iter_get_channel(i);
    1229           0 :         group = spdk_io_channel_get_ctx(ch);
    1230             : 
    1231           0 :         rc = spdk_nvme_poll_group_get_stats(group->group, &stat);
    1232           0 :         if (rc) {
    1233           0 :                 spdk_for_each_channel_continue(i, rc);
    1234           0 :                 return;
    1235             :         }
    1236             : 
    1237           0 :         spdk_json_write_object_begin(ctx->w);
    1238           0 :         spdk_json_write_named_string(ctx->w, "thread", spdk_thread_get_name(spdk_get_thread()));
    1239           0 :         spdk_json_write_named_array_begin(ctx->w, "transports");
    1240             : 
    1241           0 :         for (j = 0; j < stat->num_transports; j++) {
    1242           0 :                 tr_stat = stat->transport_stat[j];
    1243           0 :                 spdk_json_write_object_begin(ctx->w);
    1244           0 :                 spdk_json_write_named_string(ctx->w, "trname", spdk_nvme_transport_id_trtype_str(tr_stat->trtype));
    1245             : 
    1246           0 :                 switch (stat->transport_stat[j]->trtype) {
    1247             :                 case SPDK_NVME_TRANSPORT_RDMA:
    1248           0 :                         rpc_bdev_nvme_rdma_stats(ctx->w, tr_stat);
    1249           0 :                         break;
    1250             :                 case SPDK_NVME_TRANSPORT_PCIE:
    1251             :                 case SPDK_NVME_TRANSPORT_VFIOUSER:
    1252           0 :                         rpc_bdev_nvme_pcie_stats(ctx->w, tr_stat);
    1253           0 :                         break;
    1254             :                 case SPDK_NVME_TRANSPORT_TCP:
    1255           0 :                         rpc_bdev_nvme_tcp_stats(ctx->w, tr_stat);
    1256           0 :                         break;
    1257             :                 default:
    1258           0 :                         SPDK_WARNLOG("Can't handle trtype %d %s\n", tr_stat->trtype,
    1259             :                                      spdk_nvme_transport_id_trtype_str(tr_stat->trtype));
    1260           0 :                 }
    1261           0 :                 spdk_json_write_object_end(ctx->w);
    1262           0 :         }
    1263             :         /* transports array */
    1264           0 :         spdk_json_write_array_end(ctx->w);
    1265           0 :         spdk_json_write_object_end(ctx->w);
    1266             : 
    1267           0 :         spdk_nvme_poll_group_free_stats(group->group, stat);
    1268           0 :         spdk_for_each_channel_continue(i, 0);
    1269           0 : }
    1270             : 
    1271             : static void
    1272           0 : rpc_bdev_nvme_stats_done(struct spdk_io_channel_iter *i, int status)
    1273             : {
    1274           0 :         struct rpc_bdev_nvme_transport_stat_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
    1275             : 
    1276           0 :         spdk_json_write_array_end(ctx->w);
    1277           0 :         spdk_json_write_object_end(ctx->w);
    1278           0 :         spdk_jsonrpc_end_result(ctx->request, ctx->w);
    1279           0 :         free(ctx);
    1280           0 : }
    1281             : 
    1282             : static void
    1283           0 : rpc_bdev_nvme_get_transport_statistics(struct spdk_jsonrpc_request *request,
    1284             :                                        const struct spdk_json_val *params)
    1285             : {
    1286             :         struct rpc_bdev_nvme_transport_stat_ctx *ctx;
    1287             : 
    1288           0 :         if (params) {
    1289           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
    1290             :                                                  "'bdev_nvme_get_transport_statistics' requires no arguments");
    1291           0 :                 return;
    1292             :         }
    1293             : 
    1294           0 :         ctx = calloc(1, sizeof(*ctx));
    1295           0 :         if (!ctx) {
    1296           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    1297             :                                                  "Memory allocation error");
    1298           0 :                 return;
    1299             :         }
    1300           0 :         ctx->request = request;
    1301           0 :         ctx->w = spdk_jsonrpc_begin_result(ctx->request);
    1302           0 :         spdk_json_write_object_begin(ctx->w);
    1303           0 :         spdk_json_write_named_array_begin(ctx->w, "poll_groups");
    1304             : 
    1305           0 :         spdk_for_each_channel(&g_nvme_bdev_ctrlrs,
    1306             :                               rpc_bdev_nvme_stats_per_channel,
    1307           0 :                               ctx,
    1308             :                               rpc_bdev_nvme_stats_done);
    1309           0 : }
    1310           0 : SPDK_RPC_REGISTER("bdev_nvme_get_transport_statistics", rpc_bdev_nvme_get_transport_statistics,
    1311             :                   SPDK_RPC_RUNTIME)
    1312             : 
    1313             : struct rpc_bdev_nvme_controller_op_req {
    1314             :         char *name;
    1315             :         uint16_t cntlid;
    1316             : };
    1317             : 
    1318             : static void
    1319           0 : free_rpc_bdev_nvme_controller_op_req(struct rpc_bdev_nvme_controller_op_req *r)
    1320             : {
    1321           0 :         free(r->name);
    1322           0 : }
    1323             : 
    1324             : static const struct spdk_json_object_decoder rpc_bdev_nvme_controller_op_req_decoders[] = {
    1325             :         {"name", offsetof(struct rpc_bdev_nvme_controller_op_req, name), spdk_json_decode_string},
    1326             :         {"cntlid", offsetof(struct rpc_bdev_nvme_controller_op_req, cntlid), spdk_json_decode_uint16, true},
    1327             : };
    1328             : 
    1329             : static void
    1330           0 : rpc_bdev_nvme_controller_op_cb(void *cb_arg, int rc)
    1331             : {
    1332           0 :         struct spdk_jsonrpc_request *request = cb_arg;
    1333             : 
    1334           0 :         if (rc == 0) {
    1335           0 :                 spdk_jsonrpc_send_bool_response(request, true);
    1336           0 :         } else {
    1337           0 :                 spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
    1338             :         }
    1339           0 : }
    1340             : 
    1341             : static void
    1342           0 : rpc_bdev_nvme_controller_op(struct spdk_jsonrpc_request *request,
    1343             :                             const struct spdk_json_val *params,
    1344             :                             enum nvme_ctrlr_op op)
    1345             : {
    1346           0 :         struct rpc_bdev_nvme_controller_op_req req = {NULL};
    1347             :         struct nvme_bdev_ctrlr *nbdev_ctrlr;
    1348             :         struct nvme_ctrlr *nvme_ctrlr;
    1349             : 
    1350           0 :         if (spdk_json_decode_object(params, rpc_bdev_nvme_controller_op_req_decoders,
    1351             :                                     SPDK_COUNTOF(rpc_bdev_nvme_controller_op_req_decoders),
    1352             :                                     &req)) {
    1353           0 :                 SPDK_ERRLOG("spdk_json_decode_object failed\n");
    1354           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, spdk_strerror(EINVAL));
    1355           0 :                 goto exit;
    1356             :         }
    1357             : 
    1358           0 :         nbdev_ctrlr = nvme_bdev_ctrlr_get_by_name(req.name);
    1359           0 :         if (nbdev_ctrlr == NULL) {
    1360           0 :                 SPDK_ERRLOG("Failed at NVMe bdev controller lookup\n");
    1361           0 :                 spdk_jsonrpc_send_error_response(request, -ENODEV, spdk_strerror(ENODEV));
    1362           0 :                 goto exit;
    1363             :         }
    1364             : 
    1365           0 :         if (req.cntlid == 0) {
    1366           0 :                 nvme_bdev_ctrlr_op_rpc(nbdev_ctrlr, op, rpc_bdev_nvme_controller_op_cb, request);
    1367           0 :         } else {
    1368           0 :                 nvme_ctrlr = nvme_bdev_ctrlr_get_ctrlr_by_id(nbdev_ctrlr, req.cntlid);
    1369           0 :                 if (nvme_ctrlr == NULL) {
    1370           0 :                         SPDK_ERRLOG("Failed at NVMe controller lookup\n");
    1371           0 :                         spdk_jsonrpc_send_error_response(request, -ENODEV, spdk_strerror(ENODEV));
    1372           0 :                         goto exit;
    1373             :                 }
    1374           0 :                 nvme_ctrlr_op_rpc(nvme_ctrlr, op, rpc_bdev_nvme_controller_op_cb, request);
    1375             :         }
    1376             : 
    1377             : exit:
    1378           0 :         free_rpc_bdev_nvme_controller_op_req(&req);
    1379           0 : }
    1380             : 
    1381             : static void
    1382           0 : rpc_bdev_nvme_reset_controller(struct spdk_jsonrpc_request *request,
    1383             :                                const struct spdk_json_val *params)
    1384             : {
    1385           0 :         rpc_bdev_nvme_controller_op(request, params, NVME_CTRLR_OP_RESET);
    1386           0 : }
    1387           0 : SPDK_RPC_REGISTER("bdev_nvme_reset_controller", rpc_bdev_nvme_reset_controller, SPDK_RPC_RUNTIME)
    1388             : 
    1389             : static void
    1390           0 : rpc_bdev_nvme_enable_controller(struct spdk_jsonrpc_request *request,
    1391             :                                 const struct spdk_json_val *params)
    1392             : {
    1393           0 :         rpc_bdev_nvme_controller_op(request, params, NVME_CTRLR_OP_ENABLE);
    1394           0 : }
    1395           0 : SPDK_RPC_REGISTER("bdev_nvme_enable_controller", rpc_bdev_nvme_enable_controller, SPDK_RPC_RUNTIME)
    1396             : 
    1397             : static void
    1398           0 : rpc_bdev_nvme_disable_controller(struct spdk_jsonrpc_request *request,
    1399             :                                  const struct spdk_json_val *params)
    1400             : {
    1401           0 :         rpc_bdev_nvme_controller_op(request, params, NVME_CTRLR_OP_DISABLE);
    1402           0 : }
    1403           0 : SPDK_RPC_REGISTER("bdev_nvme_disable_controller", rpc_bdev_nvme_disable_controller,
    1404             :                   SPDK_RPC_RUNTIME)
    1405             : 
    1406             : struct rpc_get_controller_health_info {
    1407             :         char *name;
    1408             : };
    1409             : 
    1410             : struct spdk_nvme_health_info_context {
    1411             :         struct spdk_jsonrpc_request *request;
    1412             :         struct spdk_nvme_ctrlr *ctrlr;
    1413             :         struct spdk_nvme_health_information_page health_page;
    1414             : };
    1415             : 
    1416             : static void
    1417           0 : free_rpc_get_controller_health_info(struct rpc_get_controller_health_info *r)
    1418             : {
    1419           0 :         free(r->name);
    1420           0 : }
    1421             : 
    1422             : static const struct spdk_json_object_decoder rpc_get_controller_health_info_decoders[] = {
    1423             :         {"name", offsetof(struct rpc_get_controller_health_info, name), spdk_json_decode_string, true},
    1424             : };
    1425             : 
    1426             : static void
    1427           0 : nvme_health_info_cleanup(struct spdk_nvme_health_info_context *context, bool response)
    1428             : {
    1429           0 :         if (response == true) {
    1430           0 :                 spdk_jsonrpc_send_error_response(context->request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    1431             :                                                  "Internal error.");
    1432           0 :         }
    1433             : 
    1434           0 :         free(context);
    1435           0 : }
    1436             : 
    1437             : static void
    1438           0 : get_health_log_page_completion(void *cb_arg, const struct spdk_nvme_cpl *cpl)
    1439             : {
    1440             :         int i;
    1441             :         char buf[128];
    1442           0 :         struct spdk_nvme_health_info_context *context = cb_arg;
    1443           0 :         struct spdk_jsonrpc_request *request = context->request;
    1444             :         struct spdk_json_write_ctx *w;
    1445           0 :         struct spdk_nvme_ctrlr *ctrlr = context->ctrlr;
    1446           0 :         const struct spdk_nvme_transport_id *trid = NULL;
    1447           0 :         const struct spdk_nvme_ctrlr_data *cdata = NULL;
    1448           0 :         struct spdk_nvme_health_information_page *health_page = NULL;
    1449             : 
    1450           0 :         if (spdk_nvme_cpl_is_error(cpl)) {
    1451           0 :                 nvme_health_info_cleanup(context, true);
    1452           0 :                 SPDK_ERRLOG("get log page failed\n");
    1453           0 :                 return;
    1454             :         }
    1455             : 
    1456           0 :         if (ctrlr == NULL) {
    1457           0 :                 nvme_health_info_cleanup(context, true);
    1458           0 :                 SPDK_ERRLOG("ctrlr is NULL\n");
    1459           0 :                 return;
    1460             :         } else {
    1461           0 :                 trid = spdk_nvme_ctrlr_get_transport_id(ctrlr);
    1462           0 :                 cdata = spdk_nvme_ctrlr_get_data(ctrlr);
    1463           0 :                 health_page = &(context->health_page);
    1464             :         }
    1465             : 
    1466           0 :         w = spdk_jsonrpc_begin_result(request);
    1467             : 
    1468           0 :         spdk_json_write_object_begin(w);
    1469           0 :         snprintf(buf, sizeof(cdata->mn) + 1, "%s", cdata->mn);
    1470           0 :         spdk_str_trim(buf);
    1471           0 :         spdk_json_write_named_string(w, "model_number", buf);
    1472           0 :         snprintf(buf, sizeof(cdata->sn) + 1, "%s", cdata->sn);
    1473           0 :         spdk_str_trim(buf);
    1474           0 :         spdk_json_write_named_string(w, "serial_number", buf);
    1475           0 :         snprintf(buf, sizeof(cdata->fr) + 1, "%s", cdata->fr);
    1476           0 :         spdk_str_trim(buf);
    1477           0 :         spdk_json_write_named_string(w, "firmware_revision", buf);
    1478           0 :         spdk_json_write_named_string(w, "traddr", trid->traddr);
    1479           0 :         spdk_json_write_named_uint64(w, "critical_warning", health_page->critical_warning.raw);
    1480           0 :         spdk_json_write_named_uint64(w, "temperature_celsius", health_page->temperature - 273);
    1481           0 :         spdk_json_write_named_uint64(w, "available_spare_percentage", health_page->available_spare);
    1482           0 :         spdk_json_write_named_uint64(w, "available_spare_threshold_percentage",
    1483           0 :                                      health_page->available_spare_threshold);
    1484           0 :         spdk_json_write_named_uint64(w, "percentage_used", health_page->percentage_used);
    1485           0 :         spdk_json_write_named_uint128(w, "data_units_read",
    1486           0 :                                       health_page->data_units_read[0], health_page->data_units_read[1]);
    1487           0 :         spdk_json_write_named_uint128(w, "data_units_written",
    1488           0 :                                       health_page->data_units_written[0], health_page->data_units_written[1]);
    1489           0 :         spdk_json_write_named_uint128(w, "host_read_commands",
    1490           0 :                                       health_page->host_read_commands[0], health_page->host_read_commands[1]);
    1491           0 :         spdk_json_write_named_uint128(w, "host_write_commands",
    1492           0 :                                       health_page->host_write_commands[0], health_page->host_write_commands[1]);
    1493           0 :         spdk_json_write_named_uint128(w, "controller_busy_time",
    1494           0 :                                       health_page->controller_busy_time[0], health_page->controller_busy_time[1]);
    1495           0 :         spdk_json_write_named_uint128(w, "power_cycles",
    1496           0 :                                       health_page->power_cycles[0], health_page->power_cycles[1]);
    1497           0 :         spdk_json_write_named_uint128(w, "power_on_hours",
    1498           0 :                                       health_page->power_on_hours[0], health_page->power_on_hours[1]);
    1499           0 :         spdk_json_write_named_uint128(w, "unsafe_shutdowns",
    1500           0 :                                       health_page->unsafe_shutdowns[0], health_page->unsafe_shutdowns[1]);
    1501           0 :         spdk_json_write_named_uint128(w, "media_errors",
    1502           0 :                                       health_page->media_errors[0], health_page->media_errors[1]);
    1503           0 :         spdk_json_write_named_uint128(w, "num_err_log_entries",
    1504           0 :                                       health_page->num_error_info_log_entries[0], health_page->num_error_info_log_entries[1]);
    1505           0 :         spdk_json_write_named_uint64(w, "warning_temperature_time_minutes", health_page->warning_temp_time);
    1506           0 :         spdk_json_write_named_uint64(w, "critical_composite_temperature_time_minutes",
    1507           0 :                                      health_page->critical_temp_time);
    1508           0 :         for (i = 0; i < 8; i++) {
    1509           0 :                 if (health_page->temp_sensor[i] != 0) {
    1510           0 :                         spdk_json_write_named_uint64(w, "temperature_sensor_celsius", health_page->temp_sensor[i] - 273);
    1511           0 :                 }
    1512           0 :         }
    1513           0 :         spdk_json_write_object_end(w);
    1514             : 
    1515           0 :         spdk_jsonrpc_end_result(request, w);
    1516           0 :         nvme_health_info_cleanup(context, false);
    1517           0 : }
    1518             : 
    1519             : static void
    1520           0 : get_health_log_page(struct spdk_nvme_health_info_context *context)
    1521             : {
    1522           0 :         struct spdk_nvme_ctrlr *ctrlr = context->ctrlr;
    1523             : 
    1524           0 :         if (spdk_nvme_ctrlr_cmd_get_log_page(ctrlr, SPDK_NVME_LOG_HEALTH_INFORMATION,
    1525             :                                              SPDK_NVME_GLOBAL_NS_TAG,
    1526           0 :                                              &(context->health_page), sizeof(context->health_page), 0,
    1527           0 :                                              get_health_log_page_completion, context)) {
    1528           0 :                 nvme_health_info_cleanup(context, true);
    1529           0 :                 SPDK_ERRLOG("spdk_nvme_ctrlr_cmd_get_log_page() failed\n");
    1530           0 :         }
    1531           0 : }
    1532             : 
    1533             : static void
    1534           0 : get_temperature_threshold_feature_completion(void *cb_arg, const struct spdk_nvme_cpl *cpl)
    1535             : {
    1536           0 :         struct spdk_nvme_health_info_context *context = cb_arg;
    1537             : 
    1538           0 :         if (spdk_nvme_cpl_is_error(cpl)) {
    1539           0 :                 nvme_health_info_cleanup(context, true);
    1540           0 :                 SPDK_ERRLOG("feature SPDK_NVME_FEAT_TEMPERATURE_THRESHOLD failed in completion\n");
    1541           0 :         } else {
    1542           0 :                 get_health_log_page(context);
    1543             :         }
    1544           0 : }
    1545             : 
    1546             : static int
    1547           0 : get_temperature_threshold_feature(struct spdk_nvme_health_info_context *context)
    1548             : {
    1549           0 :         struct spdk_nvme_cmd cmd = {};
    1550             : 
    1551           0 :         cmd.opc = SPDK_NVME_OPC_GET_FEATURES;
    1552           0 :         cmd.cdw10 = SPDK_NVME_FEAT_TEMPERATURE_THRESHOLD;
    1553             : 
    1554           0 :         return spdk_nvme_ctrlr_cmd_admin_raw(context->ctrlr, &cmd, NULL, 0,
    1555           0 :                                              get_temperature_threshold_feature_completion, context);
    1556             : }
    1557             : 
    1558             : static void
    1559           0 : get_controller_health_info(struct spdk_jsonrpc_request *request, struct spdk_nvme_ctrlr *ctrlr)
    1560             : {
    1561             :         struct spdk_nvme_health_info_context *context;
    1562             : 
    1563           0 :         context = calloc(1, sizeof(struct spdk_nvme_health_info_context));
    1564           0 :         if (!context) {
    1565           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    1566             :                                                  "Memory allocation error.");
    1567           0 :                 return;
    1568             :         }
    1569             : 
    1570           0 :         context->request = request;
    1571           0 :         context->ctrlr = ctrlr;
    1572             : 
    1573           0 :         if (get_temperature_threshold_feature(context)) {
    1574           0 :                 nvme_health_info_cleanup(context, true);
    1575           0 :                 SPDK_ERRLOG("feature SPDK_NVME_FEAT_TEMPERATURE_THRESHOLD failed to submit\n");
    1576           0 :         }
    1577             : 
    1578           0 :         return;
    1579           0 : }
    1580             : 
    1581             : static void
    1582           0 : rpc_bdev_nvme_get_controller_health_info(struct spdk_jsonrpc_request *request,
    1583             :                 const struct spdk_json_val *params)
    1584             : {
    1585           0 :         struct rpc_get_controller_health_info req = {};
    1586           0 :         struct nvme_ctrlr *nvme_ctrlr = NULL;
    1587             : 
    1588           0 :         if (!params) {
    1589           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    1590             :                                                  "Missing device name");
    1591             : 
    1592           0 :                 return;
    1593             :         }
    1594           0 :         if (spdk_json_decode_object(params, rpc_get_controller_health_info_decoders,
    1595             :                                     SPDK_COUNTOF(rpc_get_controller_health_info_decoders), &req)) {
    1596           0 :                 SPDK_ERRLOG("spdk_json_decode_object failed\n");
    1597           0 :                 free_rpc_get_controller_health_info(&req);
    1598           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    1599             :                                                  "Invalid parameters");
    1600             : 
    1601           0 :                 return;
    1602             :         }
    1603             : 
    1604           0 :         nvme_ctrlr = nvme_ctrlr_get_by_name(req.name);
    1605             : 
    1606           0 :         if (!nvme_ctrlr) {
    1607           0 :                 SPDK_ERRLOG("nvme ctrlr name '%s' does not exist\n", req.name);
    1608           0 :                 free_rpc_get_controller_health_info(&req);
    1609           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    1610             :                                                  "Device not found");
    1611           0 :                 return;
    1612             :         }
    1613             : 
    1614           0 :         get_controller_health_info(request, nvme_ctrlr->ctrlr);
    1615           0 :         free_rpc_get_controller_health_info(&req);
    1616             : 
    1617           0 :         return;
    1618           0 : }
    1619           0 : SPDK_RPC_REGISTER("bdev_nvme_get_controller_health_info",
    1620             :                   rpc_bdev_nvme_get_controller_health_info, SPDK_RPC_RUNTIME)
    1621             : 
    1622             : struct rpc_bdev_nvme_start_discovery {
    1623             :         char *name;
    1624             :         char *trtype;
    1625             :         char *adrfam;
    1626             :         char *traddr;
    1627             :         char *trsvcid;
    1628             :         char *hostnqn;
    1629             :         bool wait_for_attach;
    1630             :         uint64_t attach_timeout_ms;
    1631             :         struct spdk_nvme_ctrlr_opts opts;
    1632             :         struct spdk_bdev_nvme_ctrlr_opts bdev_opts;
    1633             : };
    1634             : 
    1635             : static void
    1636           0 : free_rpc_bdev_nvme_start_discovery(struct rpc_bdev_nvme_start_discovery *req)
    1637             : {
    1638           0 :         free(req->name);
    1639           0 :         free(req->trtype);
    1640           0 :         free(req->adrfam);
    1641           0 :         free(req->traddr);
    1642           0 :         free(req->trsvcid);
    1643           0 :         free(req->hostnqn);
    1644           0 : }
    1645             : 
    1646             : static const struct spdk_json_object_decoder rpc_bdev_nvme_start_discovery_decoders[] = {
    1647             :         {"name", offsetof(struct rpc_bdev_nvme_start_discovery, name), spdk_json_decode_string},
    1648             :         {"trtype", offsetof(struct rpc_bdev_nvme_start_discovery, trtype), spdk_json_decode_string},
    1649             :         {"traddr", offsetof(struct rpc_bdev_nvme_start_discovery, traddr), spdk_json_decode_string},
    1650             :         {"adrfam", offsetof(struct rpc_bdev_nvme_start_discovery, adrfam), spdk_json_decode_string, true},
    1651             :         {"trsvcid", offsetof(struct rpc_bdev_nvme_start_discovery, trsvcid), spdk_json_decode_string, true},
    1652             :         {"hostnqn", offsetof(struct rpc_bdev_nvme_start_discovery, hostnqn), spdk_json_decode_string, true},
    1653             :         {"wait_for_attach", offsetof(struct rpc_bdev_nvme_start_discovery, wait_for_attach), spdk_json_decode_bool, true},
    1654             :         {"attach_timeout_ms", offsetof(struct rpc_bdev_nvme_start_discovery, attach_timeout_ms), spdk_json_decode_uint64, true},
    1655             :         {"ctrlr_loss_timeout_sec", offsetof(struct rpc_bdev_nvme_start_discovery, bdev_opts.ctrlr_loss_timeout_sec), spdk_json_decode_int32, true},
    1656             :         {"reconnect_delay_sec", offsetof(struct rpc_bdev_nvme_start_discovery, bdev_opts.reconnect_delay_sec), spdk_json_decode_uint32, true},
    1657             :         {"fast_io_fail_timeout_sec", offsetof(struct rpc_bdev_nvme_start_discovery, bdev_opts.fast_io_fail_timeout_sec), spdk_json_decode_uint32, true},
    1658             : };
    1659             : 
    1660             : struct rpc_bdev_nvme_start_discovery_ctx {
    1661             :         struct rpc_bdev_nvme_start_discovery req;
    1662             :         struct spdk_jsonrpc_request *request;
    1663             : };
    1664             : 
    1665             : static void
    1666           0 : rpc_bdev_nvme_start_discovery_done(void *ctx, int status)
    1667             : {
    1668           0 :         struct spdk_jsonrpc_request *request = ctx;
    1669             : 
    1670           0 :         if (status != 0) {
    1671           0 :                 spdk_jsonrpc_send_error_response(request, status, spdk_strerror(-status));
    1672           0 :         } else {
    1673           0 :                 spdk_jsonrpc_send_bool_response(request, true);
    1674             :         }
    1675           0 : }
    1676             : 
    1677             : static void
    1678           0 : rpc_bdev_nvme_start_discovery(struct spdk_jsonrpc_request *request,
    1679             :                               const struct spdk_json_val *params)
    1680             : {
    1681             :         struct rpc_bdev_nvme_start_discovery_ctx *ctx;
    1682           0 :         struct spdk_nvme_transport_id trid = {};
    1683             :         size_t len, maxlen;
    1684             :         int rc;
    1685             :         spdk_bdev_nvme_start_discovery_fn cb_fn;
    1686             :         void *cb_ctx;
    1687             : 
    1688           0 :         ctx = calloc(1, sizeof(*ctx));
    1689           0 :         if (!ctx) {
    1690           0 :                 spdk_jsonrpc_send_error_response(request, -ENOMEM, spdk_strerror(ENOMEM));
    1691           0 :                 return;
    1692             :         }
    1693             : 
    1694           0 :         spdk_nvme_ctrlr_get_default_ctrlr_opts(&ctx->req.opts, sizeof(ctx->req.opts));
    1695             : 
    1696           0 :         if (spdk_json_decode_object(params, rpc_bdev_nvme_start_discovery_decoders,
    1697             :                                     SPDK_COUNTOF(rpc_bdev_nvme_start_discovery_decoders),
    1698           0 :                                     &ctx->req)) {
    1699           0 :                 SPDK_ERRLOG("spdk_json_decode_object failed\n");
    1700           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    1701             :                                                  "spdk_json_decode_object failed");
    1702           0 :                 goto cleanup;
    1703             :         }
    1704             : 
    1705             :         /* Parse trstring */
    1706           0 :         rc = spdk_nvme_transport_id_populate_trstring(&trid, ctx->req.trtype);
    1707           0 :         if (rc < 0) {
    1708           0 :                 SPDK_ERRLOG("Failed to parse trtype: %s\n", ctx->req.trtype);
    1709           0 :                 spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "Failed to parse trtype: %s",
    1710           0 :                                                      ctx->req.trtype);
    1711           0 :                 goto cleanup;
    1712             :         }
    1713             : 
    1714             :         /* Parse trtype */
    1715           0 :         rc = spdk_nvme_transport_id_parse_trtype(&trid.trtype, ctx->req.trtype);
    1716           0 :         assert(rc == 0);
    1717             : 
    1718             :         /* Parse traddr */
    1719           0 :         maxlen = sizeof(trid.traddr);
    1720           0 :         len = strnlen(ctx->req.traddr, maxlen);
    1721           0 :         if (len == maxlen) {
    1722           0 :                 spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "traddr too long: %s",
    1723           0 :                                                      ctx->req.traddr);
    1724           0 :                 goto cleanup;
    1725             :         }
    1726           0 :         memcpy(trid.traddr, ctx->req.traddr, len + 1);
    1727             : 
    1728             :         /* Parse adrfam */
    1729           0 :         if (ctx->req.adrfam) {
    1730           0 :                 rc = spdk_nvme_transport_id_parse_adrfam(&trid.adrfam, ctx->req.adrfam);
    1731           0 :                 if (rc < 0) {
    1732           0 :                         SPDK_ERRLOG("Failed to parse adrfam: %s\n", ctx->req.adrfam);
    1733           0 :                         spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "Failed to parse adrfam: %s",
    1734           0 :                                                              ctx->req.adrfam);
    1735           0 :                         goto cleanup;
    1736             :                 }
    1737           0 :         }
    1738             : 
    1739             :         /* Parse trsvcid */
    1740           0 :         if (ctx->req.trsvcid) {
    1741           0 :                 maxlen = sizeof(trid.trsvcid);
    1742           0 :                 len = strnlen(ctx->req.trsvcid, maxlen);
    1743           0 :                 if (len == maxlen) {
    1744           0 :                         spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "trsvcid too long: %s",
    1745           0 :                                                              ctx->req.trsvcid);
    1746           0 :                         goto cleanup;
    1747             :                 }
    1748           0 :                 memcpy(trid.trsvcid, ctx->req.trsvcid, len + 1);
    1749           0 :         }
    1750             : 
    1751           0 :         if (ctx->req.hostnqn) {
    1752           0 :                 snprintf(ctx->req.opts.hostnqn, sizeof(ctx->req.opts.hostnqn), "%s",
    1753           0 :                          ctx->req.hostnqn);
    1754           0 :         }
    1755             : 
    1756           0 :         if (ctx->req.attach_timeout_ms != 0) {
    1757           0 :                 ctx->req.wait_for_attach = true;
    1758           0 :         }
    1759             : 
    1760           0 :         ctx->request = request;
    1761           0 :         cb_fn = ctx->req.wait_for_attach ? rpc_bdev_nvme_start_discovery_done : NULL;
    1762           0 :         cb_ctx = ctx->req.wait_for_attach ? request : NULL;
    1763           0 :         rc = bdev_nvme_start_discovery(&trid, ctx->req.name, &ctx->req.opts, &ctx->req.bdev_opts,
    1764           0 :                                        ctx->req.attach_timeout_ms, false, cb_fn, cb_ctx);
    1765           0 :         if (rc) {
    1766           0 :                 spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
    1767           0 :         } else if (!ctx->req.wait_for_attach) {
    1768           0 :                 rpc_bdev_nvme_start_discovery_done(request, 0);
    1769           0 :         }
    1770             : 
    1771             : cleanup:
    1772           0 :         free_rpc_bdev_nvme_start_discovery(&ctx->req);
    1773           0 :         free(ctx);
    1774           0 : }
    1775           0 : SPDK_RPC_REGISTER("bdev_nvme_start_discovery", rpc_bdev_nvme_start_discovery,
    1776             :                   SPDK_RPC_RUNTIME)
    1777             : 
    1778             : struct rpc_bdev_nvme_stop_discovery {
    1779             :         char *name;
    1780             : };
    1781             : 
    1782             : static const struct spdk_json_object_decoder rpc_bdev_nvme_stop_discovery_decoders[] = {
    1783             :         {"name", offsetof(struct rpc_bdev_nvme_stop_discovery, name), spdk_json_decode_string},
    1784             : };
    1785             : 
    1786             : struct rpc_bdev_nvme_stop_discovery_ctx {
    1787             :         struct rpc_bdev_nvme_stop_discovery req;
    1788             :         struct spdk_jsonrpc_request *request;
    1789             : };
    1790             : 
    1791             : static void
    1792           0 : rpc_bdev_nvme_stop_discovery_done(void *cb_ctx)
    1793             : {
    1794           0 :         struct rpc_bdev_nvme_stop_discovery_ctx *ctx = cb_ctx;
    1795             : 
    1796           0 :         spdk_jsonrpc_send_bool_response(ctx->request, true);
    1797           0 :         free(ctx->req.name);
    1798           0 :         free(ctx);
    1799           0 : }
    1800             : 
    1801             : static void
    1802           0 : rpc_bdev_nvme_stop_discovery(struct spdk_jsonrpc_request *request,
    1803             :                              const struct spdk_json_val *params)
    1804             : {
    1805             :         struct rpc_bdev_nvme_stop_discovery_ctx *ctx;
    1806             :         int rc;
    1807             : 
    1808           0 :         ctx = calloc(1, sizeof(*ctx));
    1809           0 :         if (!ctx) {
    1810           0 :                 spdk_jsonrpc_send_error_response(request, -ENOMEM, spdk_strerror(ENOMEM));
    1811           0 :                 return;
    1812             :         }
    1813             : 
    1814           0 :         if (spdk_json_decode_object(params, rpc_bdev_nvme_stop_discovery_decoders,
    1815             :                                     SPDK_COUNTOF(rpc_bdev_nvme_stop_discovery_decoders),
    1816           0 :                                     &ctx->req)) {
    1817           0 :                 SPDK_ERRLOG("spdk_json_decode_object failed\n");
    1818           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    1819             :                                                  "spdk_json_decode_object failed");
    1820           0 :                 goto cleanup;
    1821             :         }
    1822             : 
    1823           0 :         ctx->request = request;
    1824           0 :         rc = bdev_nvme_stop_discovery(ctx->req.name, rpc_bdev_nvme_stop_discovery_done, ctx);
    1825           0 :         if (rc) {
    1826           0 :                 spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
    1827           0 :                 goto cleanup;
    1828             :         }
    1829             : 
    1830           0 :         return;
    1831             : 
    1832             : cleanup:
    1833           0 :         free(ctx->req.name);
    1834           0 :         free(ctx);
    1835           0 : }
    1836           0 : SPDK_RPC_REGISTER("bdev_nvme_stop_discovery", rpc_bdev_nvme_stop_discovery,
    1837             :                   SPDK_RPC_RUNTIME)
    1838             : 
    1839             : static void
    1840           0 : rpc_bdev_nvme_get_discovery_info(struct spdk_jsonrpc_request *request,
    1841             :                                  const struct spdk_json_val *params)
    1842             : {
    1843             :         struct spdk_json_write_ctx *w;
    1844             : 
    1845           0 :         w = spdk_jsonrpc_begin_result(request);
    1846           0 :         bdev_nvme_get_discovery_info(w);
    1847           0 :         spdk_jsonrpc_end_result(request, w);
    1848           0 : }
    1849           0 : SPDK_RPC_REGISTER("bdev_nvme_get_discovery_info", rpc_bdev_nvme_get_discovery_info,
    1850             :                   SPDK_RPC_RUNTIME)
    1851             : 
    1852             : enum error_injection_cmd_type {
    1853             :         NVME_ADMIN_CMD = 1,
    1854             :         NVME_IO_CMD,
    1855             : };
    1856             : 
    1857             : struct rpc_add_error_injection {
    1858             :         char *name;
    1859             :         enum error_injection_cmd_type cmd_type;
    1860             :         uint8_t opc;
    1861             :         bool do_not_submit;
    1862             :         uint64_t timeout_in_us;
    1863             :         uint32_t err_count;
    1864             :         uint8_t sct;
    1865             :         uint8_t sc;
    1866             : };
    1867             : 
    1868             : static void
    1869           0 : free_rpc_add_error_injection(struct rpc_add_error_injection *req)
    1870             : {
    1871           0 :         free(req->name);
    1872           0 : }
    1873             : 
    1874             : static int
    1875           0 : rpc_error_injection_decode_cmd_type(const struct spdk_json_val *val, void *out)
    1876             : {
    1877           0 :         int *cmd_type = out;
    1878             : 
    1879           0 :         if (spdk_json_strequal(val, "admin")) {
    1880           0 :                 *cmd_type = NVME_ADMIN_CMD;
    1881           0 :         } else if (spdk_json_strequal(val, "io")) {
    1882           0 :                 *cmd_type = NVME_IO_CMD;
    1883           0 :         } else {
    1884           0 :                 SPDK_ERRLOG("Invalid parameter value: cmd_type\n");
    1885           0 :                 return -EINVAL;
    1886             :         }
    1887             : 
    1888           0 :         return 0;
    1889           0 : }
    1890             : 
    1891             : static const struct spdk_json_object_decoder rpc_add_error_injection_decoders[] = {
    1892             :         { "name", offsetof(struct rpc_add_error_injection, name), spdk_json_decode_string },
    1893             :         { "cmd_type", offsetof(struct rpc_add_error_injection, cmd_type), rpc_error_injection_decode_cmd_type },
    1894             :         { "opc", offsetof(struct rpc_add_error_injection, opc), spdk_json_decode_uint8 },
    1895             :         { "do_not_submit", offsetof(struct rpc_add_error_injection, do_not_submit), spdk_json_decode_bool, true },
    1896             :         { "timeout_in_us", offsetof(struct rpc_add_error_injection, timeout_in_us), spdk_json_decode_uint64, true },
    1897             :         { "err_count", offsetof(struct rpc_add_error_injection, err_count), spdk_json_decode_uint32, true },
    1898             :         { "sct", offsetof(struct rpc_add_error_injection, sct), spdk_json_decode_uint8, true},
    1899             :         { "sc", offsetof(struct rpc_add_error_injection, sc), spdk_json_decode_uint8, true},
    1900             : };
    1901             : 
    1902             : struct rpc_add_error_injection_ctx {
    1903             :         struct spdk_jsonrpc_request *request;
    1904             :         struct rpc_add_error_injection rpc;
    1905             : };
    1906             : 
    1907             : static void
    1908           0 : rpc_add_error_injection_done(struct nvme_ctrlr *nvme_ctrlr, void *_ctx, int status)
    1909             : {
    1910           0 :         struct rpc_add_error_injection_ctx *ctx = _ctx;
    1911             : 
    1912           0 :         if (status) {
    1913           0 :                 spdk_jsonrpc_send_error_response(ctx->request, status,
    1914             :                                                  "Failed to add the error injection.");
    1915           0 :         } else {
    1916           0 :                 spdk_jsonrpc_send_bool_response(ctx->request, true);
    1917             :         }
    1918             : 
    1919           0 :         free_rpc_add_error_injection(&ctx->rpc);
    1920           0 :         free(ctx);
    1921           0 : }
    1922             : 
    1923             : static void
    1924           0 : rpc_add_error_injection_per_channel(struct nvme_ctrlr_channel_iter *i,
    1925             :                                     struct nvme_ctrlr *nvme_ctrlr,
    1926             :                                     struct nvme_ctrlr_channel *ctrlr_ch,
    1927             :                                     void *_ctx)
    1928             : {
    1929           0 :         struct rpc_add_error_injection_ctx *ctx = _ctx;
    1930           0 :         struct spdk_nvme_qpair *qpair = ctrlr_ch->qpair->qpair;
    1931           0 :         struct spdk_nvme_ctrlr *ctrlr = ctrlr_ch->qpair->ctrlr->ctrlr;
    1932           0 :         int rc = 0;
    1933             : 
    1934           0 :         if (qpair != NULL) {
    1935           0 :                 rc = spdk_nvme_qpair_add_cmd_error_injection(ctrlr, qpair, ctx->rpc.opc,
    1936           0 :                                 ctx->rpc.do_not_submit, ctx->rpc.timeout_in_us, ctx->rpc.err_count,
    1937           0 :                                 ctx->rpc.sct, ctx->rpc.sc);
    1938           0 :         }
    1939             : 
    1940           0 :         nvme_ctrlr_for_each_channel_continue(i, rc);
    1941           0 : }
    1942             : 
    1943             : static void
    1944           0 : rpc_bdev_nvme_add_error_injection(
    1945             :         struct spdk_jsonrpc_request *request,
    1946             :         const struct spdk_json_val *params)
    1947             : {
    1948             :         struct rpc_add_error_injection_ctx *ctx;
    1949             :         struct nvme_ctrlr *nvme_ctrlr;
    1950             :         int rc;
    1951             : 
    1952           0 :         ctx = calloc(1, sizeof(*ctx));
    1953           0 :         if (!ctx) {
    1954           0 :                 spdk_jsonrpc_send_error_response(request, -ENOMEM, spdk_strerror(ENOMEM));
    1955           0 :                 return;
    1956             :         }
    1957           0 :         ctx->rpc.err_count = 1;
    1958           0 :         ctx->request = request;
    1959             : 
    1960           0 :         if (spdk_json_decode_object(params,
    1961             :                                     rpc_add_error_injection_decoders,
    1962             :                                     SPDK_COUNTOF(rpc_add_error_injection_decoders),
    1963           0 :                                     &ctx->rpc)) {
    1964           0 :                 spdk_jsonrpc_send_error_response(request, -EINVAL,
    1965             :                                                  "Failed to parse the request");
    1966           0 :                 goto cleanup;
    1967             :         }
    1968             : 
    1969           0 :         nvme_ctrlr = nvme_ctrlr_get_by_name(ctx->rpc.name);
    1970           0 :         if (nvme_ctrlr == NULL) {
    1971           0 :                 SPDK_ERRLOG("No controller with specified name was found.\n");
    1972           0 :                 spdk_jsonrpc_send_error_response(request, -ENODEV, spdk_strerror(ENODEV));
    1973           0 :                 goto cleanup;
    1974             :         }
    1975             : 
    1976           0 :         if (ctx->rpc.cmd_type == NVME_IO_CMD) {
    1977           0 :                 nvme_ctrlr_for_each_channel(nvme_ctrlr,
    1978             :                                             rpc_add_error_injection_per_channel,
    1979           0 :                                             ctx,
    1980             :                                             rpc_add_error_injection_done);
    1981             : 
    1982           0 :                 return;
    1983             :         } else {
    1984           0 :                 rc = spdk_nvme_qpair_add_cmd_error_injection(nvme_ctrlr->ctrlr, NULL, ctx->rpc.opc,
    1985           0 :                                 ctx->rpc.do_not_submit, ctx->rpc.timeout_in_us, ctx->rpc.err_count,
    1986           0 :                                 ctx->rpc.sct, ctx->rpc.sc);
    1987           0 :                 if (rc) {
    1988           0 :                         spdk_jsonrpc_send_error_response(request, -rc,
    1989             :                                                          "Failed to add the error injection");
    1990           0 :                 } else {
    1991           0 :                         spdk_jsonrpc_send_bool_response(ctx->request, true);
    1992             :                 }
    1993             :         }
    1994             : 
    1995             : cleanup:
    1996           0 :         free_rpc_add_error_injection(&ctx->rpc);
    1997           0 :         free(ctx);
    1998           0 : }
    1999           0 : SPDK_RPC_REGISTER("bdev_nvme_add_error_injection", rpc_bdev_nvme_add_error_injection,
    2000             :                   SPDK_RPC_RUNTIME)
    2001             : 
    2002             : struct rpc_remove_error_injection {
    2003             :         char *name;
    2004             :         enum error_injection_cmd_type cmd_type;
    2005             :         uint8_t opc;
    2006             : };
    2007             : 
    2008             : static void
    2009           0 : free_rpc_remove_error_injection(struct rpc_remove_error_injection *req)
    2010             : {
    2011           0 :         free(req->name);
    2012           0 : }
    2013             : 
    2014             : static const struct spdk_json_object_decoder rpc_remove_error_injection_decoders[] = {
    2015             :         { "name", offsetof(struct rpc_remove_error_injection, name), spdk_json_decode_string },
    2016             :         { "cmd_type", offsetof(struct rpc_remove_error_injection, cmd_type), rpc_error_injection_decode_cmd_type },
    2017             :         { "opc", offsetof(struct rpc_remove_error_injection, opc), spdk_json_decode_uint8 },
    2018             : };
    2019             : 
    2020             : struct rpc_remove_error_injection_ctx {
    2021             :         struct spdk_jsonrpc_request *request;
    2022             :         struct rpc_remove_error_injection rpc;
    2023             : };
    2024             : 
    2025             : static void
    2026           0 : rpc_remove_error_injection_done(struct nvme_ctrlr *nvme_ctrlr, void *_ctx, int status)
    2027             : {
    2028           0 :         struct rpc_remove_error_injection_ctx *ctx = _ctx;
    2029             : 
    2030           0 :         if (status) {
    2031           0 :                 spdk_jsonrpc_send_error_response(ctx->request, status,
    2032             :                                                  "Failed to remove the error injection.");
    2033           0 :         } else {
    2034           0 :                 spdk_jsonrpc_send_bool_response(ctx->request, true);
    2035             :         }
    2036             : 
    2037           0 :         free_rpc_remove_error_injection(&ctx->rpc);
    2038           0 :         free(ctx);
    2039           0 : }
    2040             : 
    2041             : static void
    2042           0 : rpc_remove_error_injection_per_channel(struct nvme_ctrlr_channel_iter *i,
    2043             :                                        struct nvme_ctrlr *nvme_ctrlr,
    2044             :                                        struct nvme_ctrlr_channel *ctrlr_ch,
    2045             :                                        void *_ctx)
    2046             : {
    2047           0 :         struct rpc_remove_error_injection_ctx *ctx = _ctx;
    2048           0 :         struct spdk_nvme_qpair *qpair = ctrlr_ch->qpair->qpair;
    2049           0 :         struct spdk_nvme_ctrlr *ctrlr = ctrlr_ch->qpair->ctrlr->ctrlr;
    2050             : 
    2051           0 :         if (qpair != NULL) {
    2052           0 :                 spdk_nvme_qpair_remove_cmd_error_injection(ctrlr, qpair, ctx->rpc.opc);
    2053           0 :         }
    2054             : 
    2055           0 :         nvme_ctrlr_for_each_channel_continue(i, 0);
    2056           0 : }
    2057             : 
    2058             : static void
    2059           0 : rpc_bdev_nvme_remove_error_injection(struct spdk_jsonrpc_request *request,
    2060             :                                      const struct spdk_json_val *params)
    2061             : {
    2062             :         struct rpc_remove_error_injection_ctx *ctx;
    2063             :         struct nvme_ctrlr *nvme_ctrlr;
    2064             : 
    2065           0 :         ctx = calloc(1, sizeof(*ctx));
    2066           0 :         if (!ctx) {
    2067           0 :                 spdk_jsonrpc_send_error_response(request, -ENOMEM, spdk_strerror(ENOMEM));
    2068           0 :                 return;
    2069             :         }
    2070           0 :         ctx->request = request;
    2071             : 
    2072           0 :         if (spdk_json_decode_object(params,
    2073             :                                     rpc_remove_error_injection_decoders,
    2074             :                                     SPDK_COUNTOF(rpc_remove_error_injection_decoders),
    2075           0 :                                     &ctx->rpc)) {
    2076           0 :                 spdk_jsonrpc_send_error_response(request, -EINVAL,
    2077             :                                                  "Failed to parse the request");
    2078           0 :                 goto cleanup;
    2079             :         }
    2080             : 
    2081           0 :         nvme_ctrlr = nvme_ctrlr_get_by_name(ctx->rpc.name);
    2082           0 :         if (nvme_ctrlr == NULL) {
    2083           0 :                 SPDK_ERRLOG("No controller with specified name was found.\n");
    2084           0 :                 spdk_jsonrpc_send_error_response(request, -ENODEV, spdk_strerror(ENODEV));
    2085           0 :                 goto cleanup;
    2086             :         }
    2087             : 
    2088           0 :         if (ctx->rpc.cmd_type == NVME_IO_CMD) {
    2089           0 :                 nvme_ctrlr_for_each_channel(nvme_ctrlr,
    2090             :                                             rpc_remove_error_injection_per_channel,
    2091           0 :                                             ctx,
    2092             :                                             rpc_remove_error_injection_done);
    2093           0 :                 return;
    2094             :         } else {
    2095           0 :                 spdk_nvme_qpair_remove_cmd_error_injection(nvme_ctrlr->ctrlr, NULL, ctx->rpc.opc);
    2096           0 :                 spdk_jsonrpc_send_bool_response(ctx->request, true);
    2097             :         }
    2098             : 
    2099             : cleanup:
    2100           0 :         free_rpc_remove_error_injection(&ctx->rpc);
    2101           0 :         free(ctx);
    2102           0 : }
    2103           0 : SPDK_RPC_REGISTER("bdev_nvme_remove_error_injection", rpc_bdev_nvme_remove_error_injection,
    2104             :                   SPDK_RPC_RUNTIME)
    2105             : 
    2106             : struct rpc_get_io_paths {
    2107             :         char *name;
    2108             : };
    2109             : 
    2110             : static void
    2111           0 : free_rpc_get_io_paths(struct rpc_get_io_paths *r)
    2112             : {
    2113           0 :         free(r->name);
    2114           0 : }
    2115             : 
    2116             : static const struct spdk_json_object_decoder rpc_get_io_paths_decoders[] = {
    2117             :         {"name", offsetof(struct rpc_get_io_paths, name), spdk_json_decode_string, true},
    2118             : };
    2119             : 
    2120             : struct rpc_get_io_paths_ctx {
    2121             :         struct rpc_get_io_paths req;
    2122             :         struct spdk_jsonrpc_request *request;
    2123             :         struct spdk_json_write_ctx *w;
    2124             : };
    2125             : 
    2126             : static void
    2127           0 : rpc_bdev_nvme_get_io_paths_done(struct spdk_io_channel_iter *i, int status)
    2128             : {
    2129           0 :         struct rpc_get_io_paths_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
    2130             : 
    2131           0 :         spdk_json_write_array_end(ctx->w);
    2132             : 
    2133           0 :         spdk_json_write_object_end(ctx->w);
    2134             : 
    2135           0 :         spdk_jsonrpc_end_result(ctx->request, ctx->w);
    2136             : 
    2137           0 :         free_rpc_get_io_paths(&ctx->req);
    2138           0 :         free(ctx);
    2139           0 : }
    2140             : 
    2141             : static void
    2142           0 : _rpc_bdev_nvme_get_io_paths(struct spdk_io_channel_iter *i)
    2143             : {
    2144           0 :         struct spdk_io_channel *_ch = spdk_io_channel_iter_get_channel(i);
    2145           0 :         struct nvme_poll_group *group = spdk_io_channel_get_ctx(_ch);
    2146           0 :         struct rpc_get_io_paths_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
    2147             :         struct nvme_qpair *qpair;
    2148             :         struct nvme_io_path *io_path;
    2149             :         struct nvme_bdev *nbdev;
    2150             : 
    2151           0 :         spdk_json_write_object_begin(ctx->w);
    2152             : 
    2153           0 :         spdk_json_write_named_string(ctx->w, "thread", spdk_thread_get_name(spdk_get_thread()));
    2154             : 
    2155           0 :         spdk_json_write_named_array_begin(ctx->w, "io_paths");
    2156             : 
    2157           0 :         TAILQ_FOREACH(qpair, &group->qpair_list, tailq) {
    2158           0 :                 TAILQ_FOREACH(io_path, &qpair->io_path_list, tailq) {
    2159           0 :                         nbdev = io_path->nvme_ns->bdev;
    2160             : 
    2161           0 :                         if (ctx->req.name != NULL &&
    2162           0 :                             strcmp(ctx->req.name, nbdev->disk.name) != 0) {
    2163           0 :                                 continue;
    2164             :                         }
    2165             : 
    2166           0 :                         nvme_io_path_info_json(ctx->w, io_path);
    2167           0 :                 }
    2168           0 :         }
    2169             : 
    2170           0 :         spdk_json_write_array_end(ctx->w);
    2171             : 
    2172           0 :         spdk_json_write_object_end(ctx->w);
    2173             : 
    2174           0 :         spdk_for_each_channel_continue(i, 0);
    2175           0 : }
    2176             : 
    2177             : static void
    2178           0 : rpc_bdev_nvme_get_io_paths(struct spdk_jsonrpc_request *request,
    2179             :                            const struct spdk_json_val *params)
    2180             : {
    2181             :         struct rpc_get_io_paths_ctx *ctx;
    2182             : 
    2183           0 :         ctx = calloc(1, sizeof(*ctx));
    2184           0 :         if (ctx == NULL) {
    2185           0 :                 spdk_jsonrpc_send_error_response(request, -ENOMEM, spdk_strerror(ENOMEM));
    2186           0 :                 return;
    2187             :         }
    2188             : 
    2189           0 :         if (params != NULL &&
    2190           0 :             spdk_json_decode_object(params, rpc_get_io_paths_decoders,
    2191             :                                     SPDK_COUNTOF(rpc_get_io_paths_decoders),
    2192           0 :                                     &ctx->req)) {
    2193           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
    2194             :                                                  "bdev_nvme_get_io_paths requires no parameters");
    2195             : 
    2196           0 :                 free_rpc_get_io_paths(&ctx->req);
    2197           0 :                 free(ctx);
    2198           0 :                 return;
    2199             :         }
    2200             : 
    2201           0 :         ctx->request = request;
    2202           0 :         ctx->w = spdk_jsonrpc_begin_result(request);
    2203             : 
    2204           0 :         spdk_json_write_object_begin(ctx->w);
    2205             : 
    2206           0 :         spdk_json_write_named_array_begin(ctx->w, "poll_groups");
    2207             : 
    2208           0 :         spdk_for_each_channel(&g_nvme_bdev_ctrlrs,
    2209             :                               _rpc_bdev_nvme_get_io_paths,
    2210           0 :                               ctx,
    2211             :                               rpc_bdev_nvme_get_io_paths_done);
    2212           0 : }
    2213           0 : SPDK_RPC_REGISTER("bdev_nvme_get_io_paths", rpc_bdev_nvme_get_io_paths, SPDK_RPC_RUNTIME)
    2214             : 
    2215             : struct rpc_bdev_nvme_set_preferred_path {
    2216             :         char *name;
    2217             :         uint16_t cntlid;
    2218             : };
    2219             : 
    2220             : static void
    2221           0 : free_rpc_bdev_nvme_set_preferred_path(struct rpc_bdev_nvme_set_preferred_path *req)
    2222             : {
    2223           0 :         free(req->name);
    2224           0 : }
    2225             : 
    2226             : static const struct spdk_json_object_decoder rpc_bdev_nvme_set_preferred_path_decoders[] = {
    2227             :         {"name", offsetof(struct rpc_bdev_nvme_set_preferred_path, name), spdk_json_decode_string},
    2228             :         {"cntlid", offsetof(struct rpc_bdev_nvme_set_preferred_path, cntlid), spdk_json_decode_uint16},
    2229             : };
    2230             : 
    2231             : struct rpc_bdev_nvme_set_preferred_path_ctx {
    2232             :         struct rpc_bdev_nvme_set_preferred_path req;
    2233             :         struct spdk_jsonrpc_request *request;
    2234             : };
    2235             : 
    2236             : static void
    2237           0 : rpc_bdev_nvme_set_preferred_path_done(void *cb_arg, int rc)
    2238             : {
    2239           0 :         struct rpc_bdev_nvme_set_preferred_path_ctx *ctx = cb_arg;
    2240             : 
    2241           0 :         if (rc == 0) {
    2242           0 :                 spdk_jsonrpc_send_bool_response(ctx->request, true);
    2243           0 :         } else {
    2244           0 :                 spdk_jsonrpc_send_error_response(ctx->request, rc, spdk_strerror(-rc));
    2245             :         }
    2246             : 
    2247           0 :         free_rpc_bdev_nvme_set_preferred_path(&ctx->req);
    2248           0 :         free(ctx);
    2249           0 : }
    2250             : 
    2251             : static void
    2252           0 : rpc_bdev_nvme_set_preferred_path(struct spdk_jsonrpc_request *request,
    2253             :                                  const struct spdk_json_val *params)
    2254             : {
    2255             :         struct rpc_bdev_nvme_set_preferred_path_ctx *ctx;
    2256             : 
    2257           0 :         ctx = calloc(1, sizeof(*ctx));
    2258           0 :         if (ctx == NULL) {
    2259           0 :                 spdk_jsonrpc_send_error_response(request, -ENOMEM, spdk_strerror(ENOMEM));
    2260           0 :                 return;
    2261             :         }
    2262             : 
    2263           0 :         if (spdk_json_decode_object(params, rpc_bdev_nvme_set_preferred_path_decoders,
    2264             :                                     SPDK_COUNTOF(rpc_bdev_nvme_set_preferred_path_decoders),
    2265           0 :                                     &ctx->req)) {
    2266           0 :                 SPDK_ERRLOG("spdk_json_decode_object failed\n");
    2267           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    2268             :                                                  "spdk_json_decode_object failed");
    2269           0 :                 goto cleanup;
    2270             :         }
    2271             : 
    2272           0 :         ctx->request = request;
    2273             : 
    2274           0 :         bdev_nvme_set_preferred_path(ctx->req.name, ctx->req.cntlid,
    2275           0 :                                      rpc_bdev_nvme_set_preferred_path_done, ctx);
    2276           0 :         return;
    2277             : 
    2278             : cleanup:
    2279           0 :         free_rpc_bdev_nvme_set_preferred_path(&ctx->req);
    2280           0 :         free(ctx);
    2281           0 : }
    2282           0 : SPDK_RPC_REGISTER("bdev_nvme_set_preferred_path", rpc_bdev_nvme_set_preferred_path,
    2283             :                   SPDK_RPC_RUNTIME)
    2284             : 
    2285             : struct rpc_set_multipath_policy {
    2286             :         char *name;
    2287             :         enum spdk_bdev_nvme_multipath_policy policy;
    2288             :         enum spdk_bdev_nvme_multipath_selector selector;
    2289             :         uint32_t rr_min_io;
    2290             : };
    2291             : 
    2292             : static void
    2293           0 : free_rpc_set_multipath_policy(struct rpc_set_multipath_policy *req)
    2294             : {
    2295           0 :         free(req->name);
    2296           0 : }
    2297             : 
    2298             : static int
    2299           0 : rpc_decode_mp_policy(const struct spdk_json_val *val, void *out)
    2300             : {
    2301           0 :         enum spdk_bdev_nvme_multipath_policy *policy = out;
    2302             : 
    2303           0 :         if (spdk_json_strequal(val, "active_passive") == true) {
    2304           0 :                 *policy = BDEV_NVME_MP_POLICY_ACTIVE_PASSIVE;
    2305           0 :         } else if (spdk_json_strequal(val, "active_active") == true) {
    2306           0 :                 *policy = BDEV_NVME_MP_POLICY_ACTIVE_ACTIVE;
    2307           0 :         } else {
    2308           0 :                 SPDK_NOTICELOG("Invalid parameter value: policy\n");
    2309           0 :                 return -EINVAL;
    2310             :         }
    2311             : 
    2312           0 :         return 0;
    2313           0 : }
    2314             : 
    2315             : static int
    2316           0 : rpc_decode_mp_selector(const struct spdk_json_val *val, void *out)
    2317             : {
    2318           0 :         enum spdk_bdev_nvme_multipath_selector *selector = out;
    2319             : 
    2320           0 :         if (spdk_json_strequal(val, "round_robin") == true) {
    2321           0 :                 *selector = BDEV_NVME_MP_SELECTOR_ROUND_ROBIN;
    2322           0 :         } else if (spdk_json_strequal(val, "queue_depth") == true) {
    2323           0 :                 *selector = BDEV_NVME_MP_SELECTOR_QUEUE_DEPTH;
    2324           0 :         } else {
    2325           0 :                 SPDK_NOTICELOG("Invalid parameter value: selector\n");
    2326           0 :                 return -EINVAL;
    2327             :         }
    2328             : 
    2329           0 :         return 0;
    2330           0 : }
    2331             : 
    2332             : static const struct spdk_json_object_decoder rpc_set_multipath_policy_decoders[] = {
    2333             :         {"name", offsetof(struct rpc_set_multipath_policy, name), spdk_json_decode_string},
    2334             :         {"policy", offsetof(struct rpc_set_multipath_policy, policy), rpc_decode_mp_policy},
    2335             :         {"selector", offsetof(struct rpc_set_multipath_policy, selector), rpc_decode_mp_selector, true},
    2336             :         {"rr_min_io", offsetof(struct rpc_set_multipath_policy, rr_min_io), spdk_json_decode_uint32, true},
    2337             : };
    2338             : 
    2339             : struct rpc_set_multipath_policy_ctx {
    2340             :         struct rpc_set_multipath_policy req;
    2341             :         struct spdk_jsonrpc_request *request;
    2342             : };
    2343             : 
    2344             : static void
    2345           0 : rpc_bdev_nvme_set_multipath_policy_done(void *cb_arg, int rc)
    2346             : {
    2347           0 :         struct rpc_set_multipath_policy_ctx *ctx = cb_arg;
    2348             : 
    2349           0 :         if (rc == 0) {
    2350           0 :                 spdk_jsonrpc_send_bool_response(ctx->request, true);
    2351           0 :         } else {
    2352           0 :                 spdk_jsonrpc_send_error_response(ctx->request, rc, spdk_strerror(-rc));
    2353             :         }
    2354             : 
    2355           0 :         free_rpc_set_multipath_policy(&ctx->req);
    2356           0 :         free(ctx);
    2357           0 : }
    2358             : 
    2359             : static void
    2360           0 : rpc_bdev_nvme_set_multipath_policy(struct spdk_jsonrpc_request *request,
    2361             :                                    const struct spdk_json_val *params)
    2362             : {
    2363             :         struct rpc_set_multipath_policy_ctx *ctx;
    2364             : 
    2365           0 :         ctx = calloc(1, sizeof(*ctx));
    2366           0 :         if (ctx == NULL) {
    2367           0 :                 spdk_jsonrpc_send_error_response(request, -ENOMEM, spdk_strerror(ENOMEM));
    2368           0 :                 return;
    2369             :         }
    2370             : 
    2371           0 :         ctx->req.rr_min_io = UINT32_MAX;
    2372           0 :         ctx->req.selector = UINT32_MAX;
    2373             : 
    2374           0 :         if (spdk_json_decode_object(params, rpc_set_multipath_policy_decoders,
    2375             :                                     SPDK_COUNTOF(rpc_set_multipath_policy_decoders),
    2376           0 :                                     &ctx->req)) {
    2377           0 :                 SPDK_ERRLOG("spdk_json_decode_object failed\n");
    2378           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    2379             :                                                  "spdk_json_decode_object failed");
    2380           0 :                 goto cleanup;
    2381             :         }
    2382             : 
    2383           0 :         ctx->request = request;
    2384           0 :         if (ctx->req.selector == UINT32_MAX) {
    2385           0 :                 if (ctx->req.policy == BDEV_NVME_MP_POLICY_ACTIVE_ACTIVE) {
    2386           0 :                         ctx->req.selector = BDEV_NVME_MP_SELECTOR_ROUND_ROBIN;
    2387           0 :                 } else {
    2388           0 :                         ctx->req.selector = 0;
    2389             :                 }
    2390           0 :         }
    2391             : 
    2392           0 :         if (ctx->req.policy != BDEV_NVME_MP_POLICY_ACTIVE_ACTIVE && ctx->req.selector > 0) {
    2393           0 :                 SPDK_ERRLOG("selector only works in active_active mode\n");
    2394           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    2395             :                                                  "spdk_json_decode_object failed");
    2396           0 :                 goto cleanup;
    2397             :         }
    2398             : 
    2399           0 :         spdk_bdev_nvme_set_multipath_policy(ctx->req.name, ctx->req.policy, ctx->req.selector,
    2400           0 :                                             ctx->req.rr_min_io,
    2401           0 :                                             rpc_bdev_nvme_set_multipath_policy_done, ctx);
    2402           0 :         return;
    2403             : 
    2404             : cleanup:
    2405           0 :         free_rpc_set_multipath_policy(&ctx->req);
    2406           0 :         free(ctx);
    2407           0 : }
    2408           0 : SPDK_RPC_REGISTER("bdev_nvme_set_multipath_policy", rpc_bdev_nvme_set_multipath_policy,
    2409             :                   SPDK_RPC_RUNTIME)
    2410             : 
    2411             : struct rpc_bdev_nvme_start_mdns_discovery {
    2412             :         char *name;
    2413             :         char *svcname;
    2414             :         char *hostnqn;
    2415             :         struct spdk_nvme_ctrlr_opts opts;
    2416             :         struct spdk_bdev_nvme_ctrlr_opts bdev_opts;
    2417             : };
    2418             : 
    2419             : static void
    2420           0 : free_rpc_bdev_nvme_start_mdns_discovery(struct rpc_bdev_nvme_start_mdns_discovery *req)
    2421             : {
    2422           0 :         free(req->name);
    2423           0 :         free(req->svcname);
    2424           0 :         free(req->hostnqn);
    2425           0 : }
    2426             : 
    2427             : static const struct spdk_json_object_decoder rpc_bdev_nvme_start_mdns_discovery_decoders[] = {
    2428             :         {"name", offsetof(struct rpc_bdev_nvme_start_mdns_discovery, name), spdk_json_decode_string},
    2429             :         {"svcname", offsetof(struct rpc_bdev_nvme_start_mdns_discovery, svcname), spdk_json_decode_string},
    2430             :         {"hostnqn", offsetof(struct rpc_bdev_nvme_start_mdns_discovery, hostnqn), spdk_json_decode_string, true},
    2431             : };
    2432             : 
    2433             : struct rpc_bdev_nvme_start_mdns_discovery_ctx {
    2434             :         struct rpc_bdev_nvme_start_mdns_discovery req;
    2435             :         struct spdk_jsonrpc_request *request;
    2436             : };
    2437             : 
    2438             : static void
    2439           0 : rpc_bdev_nvme_start_mdns_discovery(struct spdk_jsonrpc_request *request,
    2440             :                                    const struct spdk_json_val *params)
    2441             : {
    2442             :         struct rpc_bdev_nvme_start_mdns_discovery_ctx *ctx;
    2443             :         int rc;
    2444             : 
    2445           0 :         ctx = calloc(1, sizeof(*ctx));
    2446           0 :         if (!ctx) {
    2447           0 :                 spdk_jsonrpc_send_error_response(request, -ENOMEM, spdk_strerror(ENOMEM));
    2448           0 :                 return;
    2449             :         }
    2450             : 
    2451           0 :         spdk_nvme_ctrlr_get_default_ctrlr_opts(&ctx->req.opts, sizeof(ctx->req.opts));
    2452             : 
    2453           0 :         if (spdk_json_decode_object(params, rpc_bdev_nvme_start_mdns_discovery_decoders,
    2454             :                                     SPDK_COUNTOF(rpc_bdev_nvme_start_mdns_discovery_decoders),
    2455           0 :                                     &ctx->req)) {
    2456           0 :                 SPDK_ERRLOG("spdk_json_decode_object failed\n");
    2457           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    2458             :                                                  "spdk_json_decode_object failed");
    2459           0 :                 goto cleanup;
    2460             :         }
    2461             : 
    2462           0 :         if (ctx->req.hostnqn) {
    2463           0 :                 snprintf(ctx->req.opts.hostnqn, sizeof(ctx->req.opts.hostnqn), "%s",
    2464           0 :                          ctx->req.hostnqn);
    2465           0 :         }
    2466           0 :         ctx->request = request;
    2467           0 :         rc = bdev_nvme_start_mdns_discovery(ctx->req.name, ctx->req.svcname, &ctx->req.opts,
    2468           0 :                                             &ctx->req.bdev_opts);
    2469           0 :         if (rc) {
    2470           0 :                 spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
    2471           0 :         } else {
    2472           0 :                 spdk_jsonrpc_send_bool_response(request, true);
    2473             :         }
    2474             : 
    2475             : cleanup:
    2476           0 :         free_rpc_bdev_nvme_start_mdns_discovery(&ctx->req);
    2477           0 :         free(ctx);
    2478           0 : }
    2479           0 : SPDK_RPC_REGISTER("bdev_nvme_start_mdns_discovery", rpc_bdev_nvme_start_mdns_discovery,
    2480             :                   SPDK_RPC_RUNTIME)
    2481             : 
    2482             : struct rpc_bdev_nvme_stop_mdns_discovery {
    2483             :         char *name;
    2484             : };
    2485             : 
    2486             : static const struct spdk_json_object_decoder rpc_bdev_nvme_stop_mdns_discovery_decoders[] = {
    2487             :         {"name", offsetof(struct rpc_bdev_nvme_stop_mdns_discovery, name), spdk_json_decode_string},
    2488             : };
    2489             : 
    2490             : struct rpc_bdev_nvme_stop_mdns_discovery_ctx {
    2491             :         struct rpc_bdev_nvme_stop_mdns_discovery req;
    2492             :         struct spdk_jsonrpc_request *request;
    2493             : };
    2494             : 
    2495             : static void
    2496           0 : rpc_bdev_nvme_stop_mdns_discovery(struct spdk_jsonrpc_request *request,
    2497             :                                   const struct spdk_json_val *params)
    2498             : {
    2499             :         struct rpc_bdev_nvme_stop_mdns_discovery_ctx *ctx;
    2500             :         int rc;
    2501             : 
    2502           0 :         ctx = calloc(1, sizeof(*ctx));
    2503           0 :         if (!ctx) {
    2504           0 :                 spdk_jsonrpc_send_error_response(request, -ENOMEM, spdk_strerror(ENOMEM));
    2505           0 :                 return;
    2506             :         }
    2507             : 
    2508           0 :         if (spdk_json_decode_object(params, rpc_bdev_nvme_stop_mdns_discovery_decoders,
    2509             :                                     SPDK_COUNTOF(rpc_bdev_nvme_stop_mdns_discovery_decoders),
    2510           0 :                                     &ctx->req)) {
    2511           0 :                 SPDK_ERRLOG("spdk_json_decode_object failed\n");
    2512           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    2513             :                                                  "spdk_json_decode_object failed");
    2514           0 :                 goto cleanup;
    2515             :         }
    2516             : 
    2517           0 :         ctx->request = request;
    2518           0 :         rc = bdev_nvme_stop_mdns_discovery(ctx->req.name);
    2519             : 
    2520           0 :         if (rc) {
    2521           0 :                 spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
    2522           0 :                 goto cleanup;
    2523             :         }
    2524           0 :         spdk_jsonrpc_send_bool_response(ctx->request, true);
    2525             : 
    2526             : cleanup:
    2527           0 :         free(ctx->req.name);
    2528           0 :         free(ctx);
    2529           0 : }
    2530           0 : SPDK_RPC_REGISTER("bdev_nvme_stop_mdns_discovery", rpc_bdev_nvme_stop_mdns_discovery,
    2531             :                   SPDK_RPC_RUNTIME)
    2532             : 
    2533             : static void
    2534           0 : rpc_bdev_nvme_get_mdns_discovery_info(struct spdk_jsonrpc_request *request,
    2535             :                                       const struct spdk_json_val *params)
    2536             : {
    2537           0 :         bdev_nvme_get_mdns_discovery_info(request);
    2538           0 : }
    2539             : 
    2540           0 : SPDK_RPC_REGISTER("bdev_nvme_get_mdns_discovery_info", rpc_bdev_nvme_get_mdns_discovery_info,
    2541             :                   SPDK_RPC_RUNTIME)
    2542             : 
    2543             : struct rpc_get_path_stat {
    2544             :         char    *name;
    2545             : };
    2546             : 
    2547             : struct path_stat {
    2548             :         struct spdk_bdev_io_stat        stat;
    2549             :         struct spdk_nvme_transport_id   trid;
    2550             :         struct nvme_ns                  *ns;
    2551             : };
    2552             : 
    2553             : struct rpc_bdev_nvme_path_stat_ctx {
    2554             :         struct spdk_jsonrpc_request     *request;
    2555             :         struct path_stat                *path_stat;
    2556             :         uint32_t                        num_paths;
    2557             :         struct spdk_bdev_desc           *desc;
    2558             : };
    2559             : 
    2560             : static void
    2561           0 : free_rpc_get_path_stat(struct rpc_get_path_stat *req)
    2562             : {
    2563           0 :         free(req->name);
    2564           0 : }
    2565             : 
    2566             : static const struct spdk_json_object_decoder rpc_get_path_stat_decoders[] = {
    2567             :         {"name", offsetof(struct rpc_get_path_stat, name), spdk_json_decode_string},
    2568             : };
    2569             : 
    2570             : static void
    2571           0 : dummy_bdev_event_cb(enum spdk_bdev_event_type type, struct spdk_bdev *bdev, void *ctx)
    2572             : {
    2573           0 : }
    2574             : 
    2575             : static void
    2576           0 : rpc_bdev_nvme_path_stat_per_channel(struct nvme_bdev_channel_iter *i,
    2577             :                                     struct nvme_bdev *nbdev,
    2578             :                                     struct nvme_bdev_channel *nbdev_ch,
    2579             :                                     void *_ctx)
    2580             : {
    2581           0 :         struct rpc_bdev_nvme_path_stat_ctx *ctx = _ctx;
    2582             :         struct nvme_io_path *io_path;
    2583             :         struct path_stat *path_stat;
    2584             :         uint32_t j;
    2585             : 
    2586           0 :         assert(ctx->num_paths != 0);
    2587             : 
    2588           0 :         for (j = 0; j < ctx->num_paths; j++) {
    2589           0 :                 path_stat = &ctx->path_stat[j];
    2590             : 
    2591           0 :                 STAILQ_FOREACH(io_path, &nbdev_ch->io_path_list, stailq) {
    2592           0 :                         if (path_stat->ns == io_path->nvme_ns) {
    2593           0 :                                 assert(io_path->stat != NULL);
    2594           0 :                                 spdk_bdev_add_io_stat(&path_stat->stat, io_path->stat);
    2595           0 :                         }
    2596           0 :                 }
    2597           0 :         }
    2598             : 
    2599           0 :         nvme_bdev_for_each_channel_continue(i, 0);
    2600           0 : }
    2601             : 
    2602             : static void
    2603           0 : rpc_bdev_nvme_path_stat_done(struct nvme_bdev *nbdev, void *_ctx, int status)
    2604             : {
    2605           0 :         struct rpc_bdev_nvme_path_stat_ctx *ctx = _ctx;
    2606             :         struct spdk_json_write_ctx *w;
    2607             :         struct path_stat *path_stat;
    2608             :         uint32_t j;
    2609             : 
    2610           0 :         assert(ctx->num_paths != 0);
    2611             : 
    2612           0 :         w = spdk_jsonrpc_begin_result(ctx->request);
    2613           0 :         spdk_json_write_object_begin(w);
    2614           0 :         spdk_json_write_named_string(w, "name", nbdev->disk.name);
    2615           0 :         spdk_json_write_named_array_begin(w, "stats");
    2616             : 
    2617           0 :         for (j = 0; j < ctx->num_paths; j++) {
    2618           0 :                 path_stat = &ctx->path_stat[j];
    2619           0 :                 spdk_json_write_object_begin(w);
    2620             : 
    2621           0 :                 spdk_json_write_named_object_begin(w, "trid");
    2622           0 :                 nvme_bdev_dump_trid_json(&path_stat->trid, w);
    2623           0 :                 spdk_json_write_object_end(w);
    2624             : 
    2625           0 :                 spdk_json_write_named_object_begin(w, "stat");
    2626           0 :                 spdk_bdev_dump_io_stat_json(&path_stat->stat, w);
    2627           0 :                 spdk_json_write_object_end(w);
    2628             : 
    2629           0 :                 spdk_json_write_object_end(w);
    2630           0 :         }
    2631             : 
    2632           0 :         spdk_json_write_array_end(w);
    2633           0 :         spdk_json_write_object_end(w);
    2634           0 :         spdk_jsonrpc_end_result(ctx->request, w);
    2635             : 
    2636           0 :         spdk_bdev_close(ctx->desc);
    2637           0 :         free(ctx->path_stat);
    2638           0 :         free(ctx);
    2639           0 : }
    2640             : 
    2641             : static void
    2642           0 : rpc_bdev_nvme_get_path_iostat(struct spdk_jsonrpc_request *request,
    2643             :                               const struct spdk_json_val *params)
    2644             : {
    2645           0 :         struct rpc_get_path_stat req = {};
    2646           0 :         struct spdk_bdev_desc *desc = NULL;
    2647             :         struct spdk_bdev *bdev;
    2648             :         struct nvme_bdev *nbdev;
    2649             :         struct nvme_ns *nvme_ns;
    2650             :         struct path_stat *path_stat;
    2651             :         struct rpc_bdev_nvme_path_stat_ctx *ctx;
    2652             :         struct spdk_bdev_nvme_opts opts;
    2653           0 :         uint32_t num_paths = 0, i = 0;
    2654             :         int rc;
    2655             : 
    2656           0 :         bdev_nvme_get_opts(&opts);
    2657           0 :         if (!opts.io_path_stat) {
    2658           0 :                 SPDK_ERRLOG("RPC not enabled if enable_io_path_stat is false\n");
    2659           0 :                 spdk_jsonrpc_send_error_response(request, -EPERM,
    2660             :                                                  "RPC not enabled if enable_io_path_stat is false");
    2661           0 :                 return;
    2662             :         }
    2663             : 
    2664           0 :         if (spdk_json_decode_object(params, rpc_get_path_stat_decoders,
    2665             :                                     SPDK_COUNTOF(rpc_get_path_stat_decoders),
    2666             :                                     &req)) {
    2667           0 :                 SPDK_ERRLOG("spdk_json_decode_object failed\n");
    2668           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    2669             :                                                  "spdk_json_decode_object failed");
    2670           0 :                 free_rpc_get_path_stat(&req);
    2671           0 :                 return;
    2672             :         }
    2673             : 
    2674           0 :         rc = spdk_bdev_open_ext(req.name, false, dummy_bdev_event_cb, NULL, &desc);
    2675           0 :         if (rc != 0) {
    2676           0 :                 SPDK_ERRLOG("Failed to open bdev '%s': %d\n", req.name, rc);
    2677           0 :                 spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
    2678           0 :                 free_rpc_get_path_stat(&req);
    2679           0 :                 return;
    2680             :         }
    2681             : 
    2682           0 :         free_rpc_get_path_stat(&req);
    2683             : 
    2684           0 :         ctx = calloc(1, sizeof(struct rpc_bdev_nvme_path_stat_ctx));
    2685           0 :         if (ctx == NULL) {
    2686           0 :                 spdk_bdev_close(desc);
    2687           0 :                 SPDK_ERRLOG("Failed to allocate rpc_bdev_nvme_path_stat_ctx struct\n");
    2688           0 :                 spdk_jsonrpc_send_error_response(request, -ENOMEM, spdk_strerror(ENOMEM));
    2689           0 :                 return;
    2690             :         }
    2691             : 
    2692           0 :         bdev = spdk_bdev_desc_get_bdev(desc);
    2693           0 :         nbdev = bdev->ctxt;
    2694             : 
    2695           0 :         pthread_mutex_lock(&nbdev->mutex);
    2696           0 :         if (nbdev->ref == 0) {
    2697           0 :                 rc = -ENOENT;
    2698           0 :                 goto err;
    2699             :         }
    2700             : 
    2701           0 :         num_paths = nbdev->ref;
    2702           0 :         path_stat = calloc(num_paths, sizeof(struct path_stat));
    2703           0 :         if (path_stat == NULL) {
    2704           0 :                 rc = -ENOMEM;
    2705           0 :                 SPDK_ERRLOG("Failed to allocate memory for path_stat.\n");
    2706           0 :                 goto err;
    2707             :         }
    2708             : 
    2709             :         /* store the history stat */
    2710           0 :         TAILQ_FOREACH(nvme_ns, &nbdev->nvme_ns_list, tailq) {
    2711           0 :                 assert(i < num_paths);
    2712           0 :                 path_stat[i].ns = nvme_ns;
    2713           0 :                 path_stat[i].trid = nvme_ns->ctrlr->active_path_id->trid;
    2714             : 
    2715           0 :                 assert(nvme_ns->stat != NULL);
    2716           0 :                 memcpy(&path_stat[i].stat, nvme_ns->stat, sizeof(struct spdk_bdev_io_stat));
    2717           0 :                 i++;
    2718           0 :         }
    2719           0 :         pthread_mutex_unlock(&nbdev->mutex);
    2720             : 
    2721           0 :         ctx->request = request;
    2722           0 :         ctx->desc = desc;
    2723           0 :         ctx->path_stat = path_stat;
    2724           0 :         ctx->num_paths = num_paths;
    2725             : 
    2726           0 :         nvme_bdev_for_each_channel(nbdev,
    2727             :                                    rpc_bdev_nvme_path_stat_per_channel,
    2728           0 :                                    ctx,
    2729             :                                    rpc_bdev_nvme_path_stat_done);
    2730           0 :         return;
    2731             : 
    2732             : err:
    2733           0 :         pthread_mutex_unlock(&nbdev->mutex);
    2734           0 :         spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
    2735           0 :         spdk_bdev_close(desc);
    2736           0 :         free(ctx);
    2737           0 : }
    2738           0 : SPDK_RPC_REGISTER("bdev_nvme_get_path_iostat", rpc_bdev_nvme_get_path_iostat,
    2739             :                   SPDK_RPC_RUNTIME)
    2740             : 
    2741             : struct rpc_bdev_nvme_set_keys {
    2742             :         char *name;
    2743             :         char *dhchap_key;
    2744             :         char *dhchap_ctrlr_key;
    2745             : };
    2746             : 
    2747             : static const struct spdk_json_object_decoder rpc_bdev_nvme_set_keys_decoders[] = {
    2748             :         {"name", offsetof(struct rpc_bdev_nvme_set_keys, name), spdk_json_decode_string},
    2749             :         {"dhchap_key", offsetof(struct rpc_bdev_nvme_set_keys, dhchap_key), spdk_json_decode_string, true},
    2750             :         {"dhchap_ctrlr_key", offsetof(struct rpc_bdev_nvme_set_keys, dhchap_ctrlr_key), spdk_json_decode_string, true},
    2751             : };
    2752             : 
    2753             : static void
    2754           0 : free_rpc_bdev_nvme_set_keys(struct rpc_bdev_nvme_set_keys *req)
    2755             : {
    2756           0 :         free(req->name);
    2757           0 :         free(req->dhchap_key);
    2758           0 :         free(req->dhchap_ctrlr_key);
    2759           0 : }
    2760             : 
    2761             : static void
    2762           0 : rpc_bdev_nvme_set_keys_done(void *ctx, int status)
    2763             : {
    2764           0 :         struct spdk_jsonrpc_request *request = ctx;
    2765             : 
    2766           0 :         if (status != 0) {
    2767           0 :                 spdk_jsonrpc_send_error_response(request, status, spdk_strerror(-status));
    2768           0 :         } else {
    2769           0 :                 spdk_jsonrpc_send_bool_response(request, true);
    2770             :         }
    2771           0 : }
    2772             : 
    2773             : static void
    2774           0 : rpc_bdev_nvme_set_keys(struct spdk_jsonrpc_request *request, const struct spdk_json_val *params)
    2775             : {
    2776           0 :         struct rpc_bdev_nvme_set_keys req = {};
    2777             :         int rc;
    2778             : 
    2779           0 :         if (spdk_json_decode_object(params, rpc_bdev_nvme_set_keys_decoders,
    2780             :                                     SPDK_COUNTOF(rpc_bdev_nvme_set_keys_decoders), &req)) {
    2781           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
    2782             :                                                  "spdk_json_decode_object failed");
    2783           0 :                 return;
    2784             :         }
    2785             : 
    2786           0 :         rc = bdev_nvme_set_keys(req.name, req.dhchap_key, req.dhchap_ctrlr_key,
    2787           0 :                                 rpc_bdev_nvme_set_keys_done, request);
    2788           0 :         if (rc != 0) {
    2789           0 :                 spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
    2790           0 :         }
    2791           0 :         free_rpc_bdev_nvme_set_keys(&req);
    2792           0 : }
    2793           0 : SPDK_RPC_REGISTER("bdev_nvme_set_keys", rpc_bdev_nvme_set_keys, SPDK_RPC_RUNTIME)

Generated by: LCOV version 1.15