LCOV - code coverage report
Current view: top level - spdk/module/bdev/nvme - bdev_nvme_rpc.c (source / functions) Hit Total Coverage
Test: Combined Lines: 573 1283 44.7 %
Date: 2024-07-12 17:03:22 Functions: 73 109 67.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 221 546 40.5 %

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

Generated by: LCOV version 1.14