LCOV - code coverage report
Current view: top level - lib/nvmf - nvmf_rpc.c (source / functions) Hit Total Coverage
Test: ut_cov_unit.info Lines: 0 1610 0.0 %
Date: 2024-11-05 10:06:02 Functions: 0 125 0.0 %

          Line data    Source code
       1             : /*   SPDX-License-Identifier: BSD-3-Clause
       2             :  *   Copyright (C) 2016 Intel Corporation. All rights reserved.
       3             :  *   Copyright (c) 2018-2021 Mellanox Technologies LTD. All rights reserved.
       4             :  *   Copyright (c) 2021 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
       5             :  */
       6             : 
       7             : #include "spdk/bdev.h"
       8             : #include "spdk/log.h"
       9             : #include "spdk/rpc.h"
      10             : #include "spdk/env.h"
      11             : #include "spdk/nvme.h"
      12             : #include "spdk/nvmf.h"
      13             : #include "spdk/string.h"
      14             : #include "spdk/util.h"
      15             : #include "spdk/bit_array.h"
      16             : #include "spdk/config.h"
      17             : 
      18             : #include "spdk_internal/assert.h"
      19             : 
      20             : #include "nvmf_internal.h"
      21             : 
      22             : static int rpc_ana_state_parse(const char *str, enum spdk_nvme_ana_state *ana_state);
      23             : 
      24             : static int
      25           0 : json_write_hex_str(struct spdk_json_write_ctx *w, const void *data, size_t size)
      26             : {
      27             :         static const char hex_char[16] = "0123456789ABCDEF";
      28           0 :         const uint8_t *buf = data;
      29             :         char *str, *out;
      30             :         int rc;
      31             : 
      32           0 :         str = malloc(size * 2 + 1);
      33           0 :         if (str == NULL) {
      34           0 :                 return -1;
      35             :         }
      36             : 
      37           0 :         out = str;
      38           0 :         while (size--) {
      39           0 :                 unsigned byte = *buf++;
      40             : 
      41           0 :                 out[0] = hex_char[(byte >> 4) & 0xF];
      42           0 :                 out[1] = hex_char[byte & 0xF];
      43             : 
      44           0 :                 out += 2;
      45             :         }
      46           0 :         *out = '\0';
      47             : 
      48           0 :         rc = spdk_json_write_string(w, str);
      49           0 :         free(str);
      50             : 
      51           0 :         return rc;
      52             : }
      53             : 
      54             : static int
      55           0 : hex_nybble_to_num(char c)
      56             : {
      57           0 :         if (c >= '0' && c <= '9') {
      58           0 :                 return c - '0';
      59             :         }
      60             : 
      61           0 :         if (c >= 'a' && c <= 'f') {
      62           0 :                 return c - 'a' + 0xA;
      63             :         }
      64             : 
      65           0 :         if (c >= 'A' && c <= 'F') {
      66           0 :                 return c - 'A' + 0xA;
      67             :         }
      68             : 
      69           0 :         return -1;
      70             : }
      71             : 
      72             : static int
      73           0 : hex_byte_to_num(const char *str)
      74             : {
      75             :         int hi, lo;
      76             : 
      77           0 :         hi = hex_nybble_to_num(str[0]);
      78           0 :         if (hi < 0) {
      79           0 :                 return hi;
      80             :         }
      81             : 
      82           0 :         lo = hex_nybble_to_num(str[1]);
      83           0 :         if (lo < 0) {
      84           0 :                 return lo;
      85             :         }
      86             : 
      87           0 :         return hi * 16 + lo;
      88             : }
      89             : 
      90             : static int
      91           0 : decode_hex_string_be(const char *str, uint8_t *out, size_t size)
      92             : {
      93             :         size_t i;
      94             : 
      95             :         /* Decode a string in "ABCDEF012345" format to its binary representation */
      96           0 :         for (i = 0; i < size; i++) {
      97           0 :                 int num = hex_byte_to_num(str);
      98             : 
      99           0 :                 if (num < 0) {
     100             :                         /* Invalid hex byte or end of string */
     101           0 :                         return -1;
     102             :                 }
     103             : 
     104           0 :                 out[i] = (uint8_t)num;
     105           0 :                 str += 2;
     106             :         }
     107             : 
     108           0 :         if (i != size || *str != '\0') {
     109             :                 /* Length mismatch */
     110           0 :                 return -1;
     111             :         }
     112             : 
     113           0 :         return 0;
     114             : }
     115             : 
     116             : static int
     117           0 : decode_ns_nguid(const struct spdk_json_val *val, void *out)
     118             : {
     119           0 :         char *str = NULL;
     120             :         int rc;
     121             : 
     122           0 :         rc = spdk_json_decode_string(val, &str);
     123           0 :         if (rc == 0) {
     124             :                 /* 16-byte NGUID */
     125           0 :                 rc = decode_hex_string_be(str, out, 16);
     126             :         }
     127             : 
     128           0 :         free(str);
     129           0 :         return rc;
     130             : }
     131             : 
     132             : static int
     133           0 : decode_ns_eui64(const struct spdk_json_val *val, void *out)
     134             : {
     135           0 :         char *str = NULL;
     136             :         int rc;
     137             : 
     138           0 :         rc = spdk_json_decode_string(val, &str);
     139           0 :         if (rc == 0) {
     140             :                 /* 8-byte EUI-64 */
     141           0 :                 rc = decode_hex_string_be(str, out, 8);
     142             :         }
     143             : 
     144           0 :         free(str);
     145           0 :         return rc;
     146             : }
     147             : 
     148             : struct rpc_get_subsystem {
     149             :         char *nqn;
     150             :         char *tgt_name;
     151             : };
     152             : 
     153             : static const struct spdk_json_object_decoder rpc_get_subsystem_decoders[] = {
     154             :         {"nqn", offsetof(struct rpc_get_subsystem, nqn), spdk_json_decode_string, true},
     155             :         {"tgt_name", offsetof(struct rpc_get_subsystem, tgt_name), spdk_json_decode_string, true},
     156             : };
     157             : 
     158             : static void
     159           0 : dump_nvmf_subsystem(struct spdk_json_write_ctx *w, struct spdk_nvmf_subsystem *subsystem)
     160             : {
     161             :         struct spdk_nvmf_host                   *host;
     162             :         struct spdk_nvmf_subsystem_listener     *listener;
     163             : 
     164           0 :         spdk_json_write_object_begin(w);
     165             : 
     166           0 :         spdk_json_write_named_string(w, "nqn", spdk_nvmf_subsystem_get_nqn(subsystem));
     167           0 :         spdk_json_write_name(w, "subtype");
     168           0 :         if (spdk_nvmf_subsystem_get_type(subsystem) == SPDK_NVMF_SUBTYPE_NVME) {
     169           0 :                 spdk_json_write_string(w, "NVMe");
     170             :         } else {
     171           0 :                 spdk_json_write_string(w, "Discovery");
     172             :         }
     173             : 
     174           0 :         spdk_json_write_named_array_begin(w, "listen_addresses");
     175             : 
     176           0 :         for (listener = spdk_nvmf_subsystem_get_first_listener(subsystem); listener != NULL;
     177           0 :              listener = spdk_nvmf_subsystem_get_next_listener(subsystem, listener)) {
     178             :                 const struct spdk_nvme_transport_id *trid;
     179             : 
     180           0 :                 trid = spdk_nvmf_subsystem_listener_get_trid(listener);
     181             : 
     182           0 :                 spdk_json_write_object_begin(w);
     183           0 :                 nvmf_transport_listen_dump_trid(trid, w);
     184           0 :                 spdk_json_write_object_end(w);
     185             :         }
     186           0 :         spdk_json_write_array_end(w);
     187             : 
     188           0 :         spdk_json_write_named_bool(w, "allow_any_host",
     189           0 :                                    spdk_nvmf_subsystem_get_allow_any_host(subsystem));
     190             : 
     191           0 :         spdk_json_write_named_array_begin(w, "hosts");
     192             : 
     193           0 :         for (host = spdk_nvmf_subsystem_get_first_host(subsystem); host != NULL;
     194           0 :              host = spdk_nvmf_subsystem_get_next_host(subsystem, host)) {
     195           0 :                 spdk_json_write_object_begin(w);
     196           0 :                 spdk_json_write_named_string(w, "nqn", spdk_nvmf_host_get_nqn(host));
     197           0 :                 if (host->dhchap_key != NULL) {
     198           0 :                         spdk_json_write_named_string(w, "dhchap_key",
     199             :                                                      spdk_key_get_name(host->dhchap_key));
     200             :                 }
     201           0 :                 if (host->dhchap_ctrlr_key != NULL) {
     202           0 :                         spdk_json_write_named_string(w, "dhchap_ctrlr_key",
     203             :                                                      spdk_key_get_name(host->dhchap_ctrlr_key));
     204             :                 }
     205           0 :                 spdk_json_write_object_end(w);
     206             :         }
     207           0 :         spdk_json_write_array_end(w);
     208             : 
     209           0 :         if (spdk_nvmf_subsystem_get_type(subsystem) == SPDK_NVMF_SUBTYPE_NVME) {
     210             :                 struct spdk_nvmf_ns *ns;
     211           0 :                 struct spdk_nvmf_ns_opts ns_opts;
     212             :                 uint32_t max_namespaces;
     213             : 
     214           0 :                 spdk_json_write_named_string(w, "serial_number", spdk_nvmf_subsystem_get_sn(subsystem));
     215             : 
     216           0 :                 spdk_json_write_named_string(w, "model_number", spdk_nvmf_subsystem_get_mn(subsystem));
     217             : 
     218           0 :                 max_namespaces = spdk_nvmf_subsystem_get_max_namespaces(subsystem);
     219           0 :                 if (max_namespaces != 0) {
     220           0 :                         spdk_json_write_named_uint32(w, "max_namespaces", max_namespaces);
     221             :                 }
     222             : 
     223           0 :                 spdk_json_write_named_uint32(w, "min_cntlid", spdk_nvmf_subsystem_get_min_cntlid(subsystem));
     224           0 :                 spdk_json_write_named_uint32(w, "max_cntlid", spdk_nvmf_subsystem_get_max_cntlid(subsystem));
     225             : 
     226           0 :                 spdk_json_write_named_array_begin(w, "namespaces");
     227           0 :                 for (ns = spdk_nvmf_subsystem_get_first_ns(subsystem); ns != NULL;
     228           0 :                      ns = spdk_nvmf_subsystem_get_next_ns(subsystem, ns)) {
     229           0 :                         spdk_nvmf_ns_get_opts(ns, &ns_opts, sizeof(ns_opts));
     230           0 :                         spdk_json_write_object_begin(w);
     231           0 :                         spdk_json_write_named_int32(w, "nsid", spdk_nvmf_ns_get_id(ns));
     232           0 :                         spdk_json_write_named_string(w, "bdev_name",
     233           0 :                                                      spdk_bdev_get_name(spdk_nvmf_ns_get_bdev(ns)));
     234             :                         /* NOTE: "name" is kept for compatibility only - new code should use bdev_name. */
     235           0 :                         spdk_json_write_named_string(w, "name",
     236           0 :                                                      spdk_bdev_get_name(spdk_nvmf_ns_get_bdev(ns)));
     237             : 
     238           0 :                         if (!spdk_mem_all_zero(ns_opts.nguid, sizeof(ns_opts.nguid))) {
     239           0 :                                 spdk_json_write_name(w, "nguid");
     240           0 :                                 json_write_hex_str(w, ns_opts.nguid, sizeof(ns_opts.nguid));
     241             :                         }
     242             : 
     243           0 :                         if (!spdk_mem_all_zero(ns_opts.eui64, sizeof(ns_opts.eui64))) {
     244           0 :                                 spdk_json_write_name(w, "eui64");
     245           0 :                                 json_write_hex_str(w, ns_opts.eui64, sizeof(ns_opts.eui64));
     246             :                         }
     247             : 
     248           0 :                         if (!spdk_uuid_is_null(&ns_opts.uuid)) {
     249           0 :                                 spdk_json_write_named_uuid(w, "uuid", &ns_opts.uuid);
     250             :                         }
     251             : 
     252           0 :                         if (spdk_nvmf_subsystem_get_ana_reporting(subsystem)) {
     253           0 :                                 spdk_json_write_named_uint32(w, "anagrpid", ns_opts.anagrpid);
     254             :                         }
     255             : 
     256           0 :                         spdk_json_write_object_end(w);
     257             :                 }
     258           0 :                 spdk_json_write_array_end(w);
     259             :         }
     260           0 :         spdk_json_write_object_end(w);
     261           0 : }
     262             : 
     263             : static void
     264           0 : rpc_nvmf_get_subsystems(struct spdk_jsonrpc_request *request,
     265             :                         const struct spdk_json_val *params)
     266             : {
     267           0 :         struct rpc_get_subsystem req = { 0 };
     268             :         struct spdk_json_write_ctx *w;
     269           0 :         struct spdk_nvmf_subsystem *subsystem = NULL;
     270             :         struct spdk_nvmf_tgt *tgt;
     271             : 
     272           0 :         if (params) {
     273           0 :                 if (spdk_json_decode_object(params, rpc_get_subsystem_decoders,
     274             :                                             SPDK_COUNTOF(rpc_get_subsystem_decoders),
     275             :                                             &req)) {
     276           0 :                         SPDK_ERRLOG("spdk_json_decode_object failed\n");
     277           0 :                         spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters");
     278           0 :                         return;
     279             :                 }
     280             :         }
     281             : 
     282           0 :         tgt = spdk_nvmf_get_tgt(req.tgt_name);
     283           0 :         if (!tgt) {
     284           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
     285             :                                                  "Unable to find a target.");
     286           0 :                 free(req.tgt_name);
     287           0 :                 free(req.nqn);
     288           0 :                 return;
     289             :         }
     290             : 
     291           0 :         if (req.nqn) {
     292           0 :                 subsystem = spdk_nvmf_tgt_find_subsystem(tgt, req.nqn);
     293           0 :                 if (!subsystem) {
     294           0 :                         SPDK_ERRLOG("subsystem '%s' does not exist\n", req.nqn);
     295           0 :                         spdk_jsonrpc_send_error_response(request, -ENODEV, spdk_strerror(ENODEV));
     296           0 :                         free(req.tgt_name);
     297           0 :                         free(req.nqn);
     298           0 :                         return;
     299             :                 }
     300             :         }
     301             : 
     302           0 :         w = spdk_jsonrpc_begin_result(request);
     303           0 :         spdk_json_write_array_begin(w);
     304             : 
     305           0 :         if (subsystem) {
     306           0 :                 dump_nvmf_subsystem(w, subsystem);
     307             :         } else {
     308           0 :                 for (subsystem = spdk_nvmf_subsystem_get_first(tgt); subsystem != NULL;
     309           0 :                      subsystem = spdk_nvmf_subsystem_get_next(subsystem)) {
     310           0 :                         dump_nvmf_subsystem(w, subsystem);
     311             :                 }
     312             :         }
     313             : 
     314           0 :         spdk_json_write_array_end(w);
     315           0 :         spdk_jsonrpc_end_result(request, w);
     316           0 :         free(req.tgt_name);
     317           0 :         free(req.nqn);
     318             : }
     319           0 : SPDK_RPC_REGISTER("nvmf_get_subsystems", rpc_nvmf_get_subsystems, SPDK_RPC_RUNTIME)
     320             : 
     321             : struct rpc_subsystem_create {
     322             :         char *nqn;
     323             :         char *serial_number;
     324             :         char *model_number;
     325             :         char *tgt_name;
     326             :         uint32_t max_namespaces;
     327             :         bool allow_any_host;
     328             :         bool ana_reporting;
     329             :         uint16_t min_cntlid;
     330             :         uint16_t max_cntlid;
     331             :         uint64_t max_discard_size_kib;
     332             :         uint64_t max_write_zeroes_size_kib;
     333             :         bool passthrough;
     334             : };
     335             : 
     336             : static const struct spdk_json_object_decoder rpc_subsystem_create_decoders[] = {
     337             :         {"nqn", offsetof(struct rpc_subsystem_create, nqn), spdk_json_decode_string},
     338             :         {"serial_number", offsetof(struct rpc_subsystem_create, serial_number), spdk_json_decode_string, true},
     339             :         {"model_number", offsetof(struct rpc_subsystem_create, model_number), spdk_json_decode_string, true},
     340             :         {"tgt_name", offsetof(struct rpc_subsystem_create, tgt_name), spdk_json_decode_string, true},
     341             :         {"max_namespaces", offsetof(struct rpc_subsystem_create, max_namespaces), spdk_json_decode_uint32, true},
     342             :         {"allow_any_host", offsetof(struct rpc_subsystem_create, allow_any_host), spdk_json_decode_bool, true},
     343             :         {"ana_reporting", offsetof(struct rpc_subsystem_create, ana_reporting), spdk_json_decode_bool, true},
     344             :         {"min_cntlid", offsetof(struct rpc_subsystem_create, min_cntlid), spdk_json_decode_uint16, true},
     345             :         {"max_cntlid", offsetof(struct rpc_subsystem_create, max_cntlid), spdk_json_decode_uint16, true},
     346             :         {"max_discard_size_kib", offsetof(struct rpc_subsystem_create, max_discard_size_kib), spdk_json_decode_uint64, true},
     347             :         {"max_write_zeroes_size_kib", offsetof(struct rpc_subsystem_create, max_write_zeroes_size_kib), spdk_json_decode_uint64, true},
     348             :         {"passthrough", offsetof(struct rpc_subsystem_create, passthrough), spdk_json_decode_bool, true},
     349             : };
     350             : 
     351             : static void
     352           0 : rpc_nvmf_subsystem_started(struct spdk_nvmf_subsystem *subsystem,
     353             :                            void *cb_arg, int status)
     354             : {
     355           0 :         struct spdk_jsonrpc_request *request = cb_arg;
     356             : 
     357           0 :         if (!status) {
     358           0 :                 spdk_jsonrpc_send_bool_response(request, true);
     359             :         } else {
     360           0 :                 spdk_jsonrpc_send_error_response_fmt(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
     361             :                                                      "Subsystem %s start failed",
     362           0 :                                                      subsystem->subnqn);
     363           0 :                 spdk_nvmf_subsystem_destroy(subsystem, NULL, NULL);
     364             :         }
     365           0 : }
     366             : 
     367             : static void
     368           0 : rpc_nvmf_create_subsystem(struct spdk_jsonrpc_request *request,
     369             :                           const struct spdk_json_val *params)
     370             : {
     371             :         struct rpc_subsystem_create *req;
     372           0 :         struct spdk_nvmf_subsystem *subsystem = NULL;
     373             :         struct spdk_nvmf_tgt *tgt;
     374           0 :         int rc = -1;
     375             : 
     376           0 :         req = calloc(1, sizeof(*req));
     377           0 :         if (!req) {
     378           0 :                 SPDK_ERRLOG("Memory allocation failed\n");
     379           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
     380             :                                                  "Memory allocation failed");
     381           0 :                 return;
     382             :         }
     383           0 :         req->min_cntlid = NVMF_MIN_CNTLID;
     384           0 :         req->max_cntlid = NVMF_MAX_CNTLID;
     385             : 
     386           0 :         if (spdk_json_decode_object(params, rpc_subsystem_create_decoders,
     387             :                                     SPDK_COUNTOF(rpc_subsystem_create_decoders),
     388             :                                     req)) {
     389           0 :                 SPDK_ERRLOG("spdk_json_decode_object failed\n");
     390           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters");
     391           0 :                 goto cleanup;
     392             :         }
     393             : 
     394           0 :         tgt = spdk_nvmf_get_tgt(req->tgt_name);
     395           0 :         if (!tgt) {
     396           0 :                 SPDK_ERRLOG("Unable to find target %s\n", req->tgt_name);
     397           0 :                 spdk_jsonrpc_send_error_response_fmt(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
     398             :                                                      "Unable to find target %s", req->tgt_name);
     399           0 :                 goto cleanup;
     400             :         }
     401             : 
     402           0 :         subsystem = spdk_nvmf_subsystem_create(tgt, req->nqn, SPDK_NVMF_SUBTYPE_NVME,
     403             :                                                req->max_namespaces);
     404           0 :         if (!subsystem) {
     405           0 :                 SPDK_ERRLOG("Unable to create subsystem %s\n", req->nqn);
     406           0 :                 spdk_jsonrpc_send_error_response_fmt(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
     407             :                                                      "Unable to create subsystem %s", req->nqn);
     408           0 :                 goto cleanup;
     409             :         }
     410             : 
     411           0 :         if (req->serial_number) {
     412           0 :                 if (spdk_nvmf_subsystem_set_sn(subsystem, req->serial_number)) {
     413           0 :                         SPDK_ERRLOG("Subsystem %s: invalid serial number '%s'\n", req->nqn, req->serial_number);
     414           0 :                         spdk_jsonrpc_send_error_response_fmt(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
     415             :                                                              "Invalid SN %s", req->serial_number);
     416           0 :                         goto cleanup;
     417             :                 }
     418             :         }
     419             : 
     420           0 :         if (req->model_number) {
     421           0 :                 if (spdk_nvmf_subsystem_set_mn(subsystem, req->model_number)) {
     422           0 :                         SPDK_ERRLOG("Subsystem %s: invalid model number '%s'\n", req->nqn, req->model_number);
     423           0 :                         spdk_jsonrpc_send_error_response_fmt(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
     424             :                                                              "Invalid MN %s", req->model_number);
     425           0 :                         goto cleanup;
     426             :                 }
     427             :         }
     428             : 
     429           0 :         spdk_nvmf_subsystem_set_allow_any_host(subsystem, req->allow_any_host);
     430             : 
     431           0 :         spdk_nvmf_subsystem_set_ana_reporting(subsystem, req->ana_reporting);
     432             : 
     433           0 :         if (spdk_nvmf_subsystem_set_cntlid_range(subsystem, req->min_cntlid, req->max_cntlid)) {
     434           0 :                 SPDK_ERRLOG("Subsystem %s: invalid cntlid range [%u-%u]\n", req->nqn, req->min_cntlid,
     435             :                             req->max_cntlid);
     436           0 :                 spdk_jsonrpc_send_error_response_fmt(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
     437           0 :                                                      "Invalid cntlid range [%u-%u]", req->min_cntlid, req->max_cntlid);
     438           0 :                 goto cleanup;
     439             :         }
     440             : 
     441           0 :         subsystem->max_discard_size_kib = req->max_discard_size_kib;
     442             : 
     443             :         /* max_write_zeroes_size_kib must be aligned to 4 and power of 2 */
     444           0 :         if (req->max_write_zeroes_size_kib == 0 || (req->max_write_zeroes_size_kib > 2 &&
     445           0 :                         spdk_u64_is_pow2(req->max_write_zeroes_size_kib))) {
     446           0 :                 subsystem->max_write_zeroes_size_kib = req->max_write_zeroes_size_kib;
     447             :         } else {
     448           0 :                 SPDK_ERRLOG("Subsystem %s: invalid max_write_zeroes_size_kib %"PRIu64"\n", req->nqn,
     449             :                             req->max_write_zeroes_size_kib);
     450           0 :                 spdk_jsonrpc_send_error_response_fmt(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
     451             :                                                      "Invalid max_write_zeroes_size_kib %"PRIu64, req->max_write_zeroes_size_kib);
     452           0 :                 goto cleanup;
     453             :         }
     454             : 
     455           0 :         subsystem->passthrough = req->passthrough;
     456             : 
     457           0 :         rc = spdk_nvmf_subsystem_start(subsystem,
     458             :                                        rpc_nvmf_subsystem_started,
     459             :                                        request);
     460           0 :         if (rc) {
     461           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
     462             :                                                  "Failed to start subsystem");
     463             :         }
     464             : 
     465           0 : cleanup:
     466           0 :         free(req->nqn);
     467           0 :         free(req->tgt_name);
     468           0 :         free(req->serial_number);
     469           0 :         free(req->model_number);
     470           0 :         free(req);
     471             : 
     472           0 :         if (rc && subsystem) {
     473           0 :                 spdk_nvmf_subsystem_destroy(subsystem, NULL, NULL);
     474             :         }
     475             : }
     476           0 : SPDK_RPC_REGISTER("nvmf_create_subsystem", rpc_nvmf_create_subsystem, SPDK_RPC_RUNTIME)
     477             : 
     478             : struct rpc_delete_subsystem {
     479             :         char *nqn;
     480             :         char *tgt_name;
     481             : };
     482             : 
     483             : static void
     484           0 : free_rpc_delete_subsystem(struct rpc_delete_subsystem *r)
     485             : {
     486           0 :         free(r->nqn);
     487           0 :         free(r->tgt_name);
     488           0 : }
     489             : 
     490             : static void
     491           0 : rpc_nvmf_subsystem_destroy_complete_cb(void *cb_arg)
     492             : {
     493           0 :         struct spdk_jsonrpc_request *request = cb_arg;
     494             : 
     495           0 :         spdk_jsonrpc_send_bool_response(request, true);
     496           0 : }
     497             : 
     498             : static void
     499           0 : rpc_nvmf_subsystem_stopped(struct spdk_nvmf_subsystem *subsystem,
     500             :                            void *cb_arg, int status)
     501             : {
     502           0 :         struct spdk_jsonrpc_request *request = cb_arg;
     503             :         int rc;
     504             : 
     505           0 :         nvmf_subsystem_remove_all_listeners(subsystem, true);
     506           0 :         rc = spdk_nvmf_subsystem_destroy(subsystem, rpc_nvmf_subsystem_destroy_complete_cb, request);
     507           0 :         if (rc) {
     508           0 :                 if (rc == -EINPROGRESS) {
     509             :                         /* response will be sent in completion callback */
     510           0 :                         return;
     511             :                 } else {
     512           0 :                         SPDK_ERRLOG("Subsystem destruction failed, rc %d\n", rc);
     513           0 :                         spdk_jsonrpc_send_error_response_fmt(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
     514             :                                                              "Subsystem destruction failed, rc %d", rc);
     515           0 :                         return;
     516             :                 }
     517             :         }
     518           0 :         spdk_jsonrpc_send_bool_response(request, true);
     519             : }
     520             : 
     521             : static const struct spdk_json_object_decoder rpc_delete_subsystem_decoders[] = {
     522             :         {"nqn", offsetof(struct rpc_delete_subsystem, nqn), spdk_json_decode_string},
     523             :         {"tgt_name", offsetof(struct rpc_delete_subsystem, tgt_name), spdk_json_decode_string, true},
     524             : };
     525             : 
     526             : static void
     527           0 : rpc_nvmf_delete_subsystem(struct spdk_jsonrpc_request *request,
     528             :                           const struct spdk_json_val *params)
     529             : {
     530           0 :         struct rpc_delete_subsystem req = { 0 };
     531             :         struct spdk_nvmf_subsystem *subsystem;
     532             :         struct spdk_nvmf_tgt *tgt;
     533             :         int rc;
     534             : 
     535           0 :         if (spdk_json_decode_object(params, rpc_delete_subsystem_decoders,
     536             :                                     SPDK_COUNTOF(rpc_delete_subsystem_decoders),
     537             :                                     &req)) {
     538           0 :                 SPDK_ERRLOG("spdk_json_decode_object failed\n");
     539           0 :                 goto invalid;
     540             :         }
     541             : 
     542           0 :         if (req.nqn == NULL) {
     543           0 :                 SPDK_ERRLOG("missing name param\n");
     544           0 :                 goto invalid;
     545             :         }
     546             : 
     547           0 :         tgt = spdk_nvmf_get_tgt(req.tgt_name);
     548           0 :         if (!tgt) {
     549           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
     550             :                                                  "Unable to find a target.");
     551           0 :                 goto invalid_custom_response;
     552             :         }
     553             : 
     554           0 :         subsystem = spdk_nvmf_tgt_find_subsystem(tgt, req.nqn);
     555           0 :         if (!subsystem) {
     556           0 :                 goto invalid;
     557             :         }
     558             : 
     559           0 :         free_rpc_delete_subsystem(&req);
     560             : 
     561           0 :         rc = spdk_nvmf_subsystem_stop(subsystem,
     562             :                                       rpc_nvmf_subsystem_stopped,
     563             :                                       request);
     564           0 :         if (rc == -EBUSY) {
     565           0 :                 SPDK_ERRLOG("Subsystem currently in another state change try again later.\n");
     566           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
     567             :                                                  "Subsystem currently in another state change try again later.");
     568           0 :         } else if (rc != 0) {
     569           0 :                 SPDK_ERRLOG("Unable to change state on subsystem. rc=%d\n", rc);
     570           0 :                 spdk_jsonrpc_send_error_response_fmt(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
     571             :                                                      "Unable to change state on subsystem. rc=%d", rc);
     572             :         }
     573             : 
     574           0 :         return;
     575             : 
     576           0 : invalid:
     577           0 :         spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters");
     578           0 : invalid_custom_response:
     579           0 :         free_rpc_delete_subsystem(&req);
     580             : }
     581           0 : SPDK_RPC_REGISTER("nvmf_delete_subsystem", rpc_nvmf_delete_subsystem, SPDK_RPC_RUNTIME)
     582             : 
     583             : struct rpc_listen_address {
     584             :         char *trtype;
     585             :         char *adrfam;
     586             :         char *traddr;
     587             :         char *trsvcid;
     588             : };
     589             : 
     590             : static const struct spdk_json_object_decoder rpc_listen_address_decoders[] = {
     591             :         {"trtype", offsetof(struct rpc_listen_address, trtype), spdk_json_decode_string, true},
     592             :         {"adrfam", offsetof(struct rpc_listen_address, adrfam), spdk_json_decode_string, true},
     593             :         {"traddr", offsetof(struct rpc_listen_address, traddr), spdk_json_decode_string},
     594             :         {"trsvcid", offsetof(struct rpc_listen_address, trsvcid), spdk_json_decode_string, true},
     595             : };
     596             : 
     597             : static int
     598           0 : decode_rpc_listen_address(const struct spdk_json_val *val, void *out)
     599             : {
     600           0 :         struct rpc_listen_address *req = (struct rpc_listen_address *)out;
     601             : 
     602           0 :         return spdk_json_decode_object(val, rpc_listen_address_decoders,
     603             :                                        SPDK_COUNTOF(rpc_listen_address_decoders), req);
     604             : }
     605             : 
     606             : static void
     607           0 : free_rpc_listen_address(struct rpc_listen_address *r)
     608             : {
     609           0 :         free(r->trtype);
     610           0 :         free(r->adrfam);
     611           0 :         free(r->traddr);
     612           0 :         free(r->trsvcid);
     613           0 : }
     614             : 
     615             : enum nvmf_rpc_listen_op {
     616             :         NVMF_RPC_LISTEN_ADD,
     617             :         NVMF_RPC_LISTEN_REMOVE,
     618             :         NVMF_RPC_LISTEN_SET_ANA_STATE,
     619             : };
     620             : 
     621             : struct nvmf_rpc_listener_ctx {
     622             :         char                            *nqn;
     623             :         char                            *tgt_name;
     624             :         struct spdk_nvmf_tgt            *tgt;
     625             :         struct spdk_nvmf_transport      *transport;
     626             :         struct spdk_nvmf_subsystem      *subsystem;
     627             :         struct rpc_listen_address       address;
     628             :         char                            *ana_state_str;
     629             :         enum spdk_nvme_ana_state        ana_state;
     630             :         uint32_t                        anagrpid;
     631             : 
     632             :         struct spdk_jsonrpc_request     *request;
     633             :         struct spdk_nvme_transport_id   trid;
     634             :         enum nvmf_rpc_listen_op         op;
     635             :         bool                            response_sent;
     636             :         struct spdk_nvmf_listen_opts    opts;
     637             : 
     638             :         /* Hole at bytes 705-711 */
     639             :         uint8_t reserved1[7];
     640             : 
     641             :         /* Additional options for listener creation.
     642             :          * Must be 8-byte aligned. */
     643             :         struct spdk_nvmf_listener_opts  listener_opts;
     644             : };
     645             : 
     646             : static const struct spdk_json_object_decoder nvmf_rpc_listener_decoder[] = {
     647             :         {"nqn", offsetof(struct nvmf_rpc_listener_ctx, nqn), spdk_json_decode_string},
     648             :         {"listen_address", offsetof(struct nvmf_rpc_listener_ctx, address), decode_rpc_listen_address},
     649             :         {"tgt_name", offsetof(struct nvmf_rpc_listener_ctx, tgt_name), spdk_json_decode_string, true},
     650             :         {"secure_channel", offsetof(struct nvmf_rpc_listener_ctx, listener_opts.secure_channel), spdk_json_decode_bool, true},
     651             :         {"ana_state", offsetof(struct nvmf_rpc_listener_ctx, ana_state_str), spdk_json_decode_string, true},
     652             :         {"sock_impl", offsetof(struct nvmf_rpc_listener_ctx, listener_opts.sock_impl), spdk_json_decode_string, true},
     653             : };
     654             : 
     655             : static void
     656           0 : nvmf_rpc_listener_ctx_free(struct nvmf_rpc_listener_ctx *ctx)
     657             : {
     658           0 :         free(ctx->nqn);
     659           0 :         free(ctx->tgt_name);
     660           0 :         free_rpc_listen_address(&ctx->address);
     661           0 :         free(ctx->ana_state_str);
     662           0 :         free(ctx);
     663           0 : }
     664             : 
     665             : static void
     666           0 : nvmf_rpc_listen_resumed(struct spdk_nvmf_subsystem *subsystem,
     667             :                         void *cb_arg, int status)
     668             : {
     669           0 :         struct nvmf_rpc_listener_ctx *ctx = cb_arg;
     670             :         struct spdk_jsonrpc_request *request;
     671             : 
     672           0 :         request = ctx->request;
     673           0 :         if (ctx->response_sent) {
     674             :                 /* If an error occurred, the response has already been sent. */
     675           0 :                 nvmf_rpc_listener_ctx_free(ctx);
     676           0 :                 return;
     677             :         }
     678             : 
     679           0 :         nvmf_rpc_listener_ctx_free(ctx);
     680             : 
     681           0 :         spdk_jsonrpc_send_bool_response(request, true);
     682             : }
     683             : 
     684             : static void
     685           0 : nvmf_rpc_subsystem_listen(void *cb_arg, int status)
     686             : {
     687           0 :         struct nvmf_rpc_listener_ctx *ctx = cb_arg;
     688             : 
     689           0 :         if (status) {
     690             :                 /* Destroy the listener that we just created. Ignore the error code because
     691             :                  * the RPC is failing already anyway. */
     692           0 :                 spdk_nvmf_tgt_stop_listen(ctx->tgt, &ctx->trid);
     693             : 
     694           0 :                 spdk_jsonrpc_send_error_response(ctx->request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
     695             :                                                  "Invalid parameters");
     696           0 :                 ctx->response_sent = true;
     697             :         }
     698             : 
     699           0 :         if (spdk_nvmf_subsystem_resume(ctx->subsystem, nvmf_rpc_listen_resumed, ctx)) {
     700           0 :                 if (!ctx->response_sent) {
     701           0 :                         spdk_jsonrpc_send_error_response(ctx->request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
     702             :                                                          "Internal error");
     703             :                 }
     704           0 :                 nvmf_rpc_listener_ctx_free(ctx);
     705             :                 /* Can't really do anything to recover here - subsystem will remain paused. */
     706             :         }
     707           0 : }
     708             : static void
     709           0 : nvmf_rpc_stop_listen_async_done(void *cb_arg, int status)
     710             : {
     711           0 :         struct nvmf_rpc_listener_ctx *ctx = cb_arg;
     712             : 
     713           0 :         if (status) {
     714           0 :                 SPDK_ERRLOG("Unable to stop listener.\n");
     715           0 :                 spdk_jsonrpc_send_error_response_fmt(ctx->request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
     716             :                                                      "error stopping listener: %d", status);
     717           0 :                 ctx->response_sent = true;
     718             :         }
     719             : 
     720           0 :         if (spdk_nvmf_subsystem_resume(ctx->subsystem, nvmf_rpc_listen_resumed, ctx)) {
     721           0 :                 if (!ctx->response_sent) {
     722           0 :                         spdk_jsonrpc_send_error_response(ctx->request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
     723             :                                                          "Internal error");
     724             :                 }
     725           0 :                 nvmf_rpc_listener_ctx_free(ctx);
     726             :                 /* Can't really do anything to recover here - subsystem will remain paused. */
     727             :         }
     728           0 : }
     729             : 
     730             : static void
     731           0 : nvmf_rpc_set_ana_state_done(void *cb_arg, int status)
     732             : {
     733           0 :         struct nvmf_rpc_listener_ctx *ctx = cb_arg;
     734             : 
     735           0 :         if (status) {
     736           0 :                 SPDK_ERRLOG("Unable to set ANA state.\n");
     737           0 :                 spdk_jsonrpc_send_error_response_fmt(ctx->request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
     738             :                                                      "error setting ANA state: %d", status);
     739           0 :                 ctx->response_sent = true;
     740             :         }
     741             : 
     742           0 :         if (spdk_nvmf_subsystem_resume(ctx->subsystem, nvmf_rpc_listen_resumed, ctx)) {
     743           0 :                 if (!ctx->response_sent) {
     744           0 :                         spdk_jsonrpc_send_error_response(ctx->request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
     745             :                                                          "Internal error");
     746             :                 }
     747           0 :                 nvmf_rpc_listener_ctx_free(ctx);
     748             :                 /* Can't really do anything to recover here - subsystem will remain paused. */
     749             :         }
     750           0 : }
     751             : 
     752             : static void
     753           0 : nvmf_rpc_listen_paused(struct spdk_nvmf_subsystem *subsystem,
     754             :                        void *cb_arg, int status)
     755             : {
     756           0 :         struct nvmf_rpc_listener_ctx *ctx = cb_arg;
     757             :         int rc;
     758             : 
     759           0 :         switch (ctx->op) {
     760           0 :         case NVMF_RPC_LISTEN_ADD:
     761           0 :                 if (nvmf_subsystem_find_listener(subsystem, &ctx->trid)) {
     762           0 :                         SPDK_ERRLOG("Listener already exists\n");
     763           0 :                         spdk_jsonrpc_send_error_response(ctx->request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
     764             :                                                          "Invalid parameters");
     765           0 :                         ctx->response_sent = true;
     766           0 :                         break;
     767             :                 }
     768             : 
     769           0 :                 rc = spdk_nvmf_tgt_listen_ext(ctx->tgt, &ctx->trid, &ctx->opts);
     770           0 :                 if (rc) {
     771           0 :                         spdk_jsonrpc_send_error_response(ctx->request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
     772             :                                                          "Invalid parameters");
     773           0 :                         ctx->response_sent = true;
     774           0 :                         break;
     775             :                 }
     776             : 
     777           0 :                 spdk_nvmf_subsystem_add_listener_ext(ctx->subsystem, &ctx->trid, nvmf_rpc_subsystem_listen, ctx,
     778             :                                                      &ctx->listener_opts);
     779           0 :                 return;
     780           0 :         case NVMF_RPC_LISTEN_REMOVE:
     781           0 :                 rc = spdk_nvmf_subsystem_remove_listener(subsystem, &ctx->trid);
     782           0 :                 if (rc) {
     783           0 :                         SPDK_ERRLOG("Unable to remove listener, rc %d\n", rc);
     784           0 :                         spdk_jsonrpc_send_error_response(ctx->request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
     785             :                                                          "Invalid parameters");
     786           0 :                         ctx->response_sent = true;
     787           0 :                         break;
     788             :                 }
     789             : 
     790           0 :                 spdk_nvmf_transport_stop_listen_async(ctx->transport, &ctx->trid, subsystem,
     791             :                                                       nvmf_rpc_stop_listen_async_done, ctx);
     792           0 :                 return;
     793           0 :         case NVMF_RPC_LISTEN_SET_ANA_STATE:
     794           0 :                 spdk_nvmf_subsystem_set_ana_state(subsystem, &ctx->trid, ctx->ana_state, ctx->anagrpid,
     795             :                                                   nvmf_rpc_set_ana_state_done, ctx);
     796           0 :                 return;
     797           0 :         default:
     798           0 :                 SPDK_UNREACHABLE();
     799             :         }
     800             : 
     801           0 :         if (spdk_nvmf_subsystem_resume(subsystem, nvmf_rpc_listen_resumed, ctx)) {
     802           0 :                 if (!ctx->response_sent) {
     803           0 :                         spdk_jsonrpc_send_error_response(ctx->request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
     804             :                                                          "Internal error");
     805             :                 }
     806             : 
     807           0 :                 nvmf_rpc_listener_ctx_free(ctx);
     808             :                 /* Can't really do anything to recover here - subsystem will remain paused. */
     809             :         }
     810             : }
     811             : 
     812             : static int
     813           0 : rpc_listen_address_to_trid(const struct rpc_listen_address *address,
     814             :                            struct spdk_nvme_transport_id *trid)
     815             : {
     816             :         size_t len;
     817             : 
     818           0 :         memset(trid, 0, sizeof(*trid));
     819             : 
     820           0 :         if (spdk_nvme_transport_id_populate_trstring(trid, address->trtype)) {
     821           0 :                 SPDK_ERRLOG("Invalid trtype string: %s\n", address->trtype);
     822           0 :                 return -EINVAL;
     823             :         }
     824             : 
     825           0 :         if (spdk_nvme_transport_id_parse_trtype(&trid->trtype, address->trtype)) {
     826           0 :                 SPDK_ERRLOG("Invalid trtype type: %s\n", address->trtype);
     827           0 :                 return -EINVAL;
     828             :         }
     829             : 
     830           0 :         if (address->adrfam) {
     831           0 :                 if (spdk_nvme_transport_id_parse_adrfam(&trid->adrfam, address->adrfam)) {
     832           0 :                         SPDK_ERRLOG("Invalid adrfam: %s\n", address->adrfam);
     833           0 :                         return -EINVAL;
     834             :                 }
     835             :         } else {
     836           0 :                 trid->adrfam = SPDK_NVMF_ADRFAM_IPV4;
     837             :         }
     838             : 
     839           0 :         len = strlen(address->traddr);
     840           0 :         if (len > sizeof(trid->traddr) - 1) {
     841           0 :                 SPDK_ERRLOG("Transport address longer than %zu characters: %s\n",
     842             :                             sizeof(trid->traddr) - 1, address->traddr);
     843           0 :                 return -EINVAL;
     844             :         }
     845           0 :         memcpy(trid->traddr, address->traddr, len + 1);
     846             : 
     847           0 :         trid->trsvcid[0] = '\0';
     848           0 :         if (address->trsvcid) {
     849           0 :                 len = strlen(address->trsvcid);
     850           0 :                 if (len > sizeof(trid->trsvcid) - 1) {
     851           0 :                         SPDK_ERRLOG("Transport service id longer than %zu characters: %s\n",
     852             :                                     sizeof(trid->trsvcid) - 1, address->trsvcid);
     853           0 :                         return -EINVAL;
     854             :                 }
     855           0 :                 memcpy(trid->trsvcid, address->trsvcid, len + 1);
     856             :         }
     857             : 
     858           0 :         return 0;
     859             : }
     860             : 
     861             : static void
     862           0 : rpc_nvmf_subsystem_add_listener(struct spdk_jsonrpc_request *request,
     863             :                                 const struct spdk_json_val *params)
     864             : {
     865             :         struct nvmf_rpc_listener_ctx *ctx;
     866             :         struct spdk_nvmf_subsystem *subsystem;
     867             :         struct spdk_nvmf_tgt *tgt;
     868             :         int rc;
     869             : 
     870           0 :         ctx = calloc(1, sizeof(*ctx));
     871           0 :         if (!ctx) {
     872           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, "Out of memory");
     873           0 :                 return;
     874             :         }
     875             : 
     876           0 :         ctx->request = request;
     877             : 
     878           0 :         spdk_nvmf_subsystem_listener_opts_init(&ctx->listener_opts, sizeof(ctx->listener_opts));
     879             : 
     880           0 :         if (spdk_json_decode_object_relaxed(params, nvmf_rpc_listener_decoder,
     881             :                                             SPDK_COUNTOF(nvmf_rpc_listener_decoder),
     882             :                                             ctx)) {
     883           0 :                 SPDK_ERRLOG("spdk_json_decode_object_relaxed failed\n");
     884           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters");
     885           0 :                 nvmf_rpc_listener_ctx_free(ctx);
     886           0 :                 return;
     887             :         }
     888             : 
     889           0 :         tgt = spdk_nvmf_get_tgt(ctx->tgt_name);
     890           0 :         if (!tgt) {
     891           0 :                 SPDK_ERRLOG("Unable to find a target object.\n");
     892           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
     893             :                                                  "Unable to find a target.");
     894           0 :                 nvmf_rpc_listener_ctx_free(ctx);
     895           0 :                 return;
     896             :         }
     897           0 :         ctx->tgt = tgt;
     898             : 
     899           0 :         subsystem = spdk_nvmf_tgt_find_subsystem(tgt, ctx->nqn);
     900           0 :         if (!subsystem) {
     901           0 :                 SPDK_ERRLOG("Unable to find subsystem with NQN %s\n", ctx->nqn);
     902           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters");
     903           0 :                 nvmf_rpc_listener_ctx_free(ctx);
     904           0 :                 return;
     905             :         }
     906             : 
     907           0 :         ctx->subsystem = subsystem;
     908             : 
     909           0 :         if (rpc_listen_address_to_trid(&ctx->address, &ctx->trid)) {
     910           0 :                 spdk_jsonrpc_send_error_response(ctx->request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
     911             :                                                  "Invalid parameters");
     912           0 :                 nvmf_rpc_listener_ctx_free(ctx);
     913           0 :                 return;
     914             :         }
     915             : 
     916           0 :         ctx->op = NVMF_RPC_LISTEN_ADD;
     917           0 :         spdk_nvmf_listen_opts_init(&ctx->opts, sizeof(ctx->opts));
     918           0 :         ctx->opts.transport_specific = params;
     919           0 :         if (spdk_nvmf_subsystem_get_allow_any_host(subsystem) && ctx->listener_opts.secure_channel) {
     920           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
     921             :                                                  "Cannot establish secure channel, when 'allow_any_host' is set");
     922           0 :                 nvmf_rpc_listener_ctx_free(ctx);
     923           0 :                 return;
     924             :         }
     925           0 :         ctx->opts.secure_channel = ctx->listener_opts.secure_channel;
     926             : 
     927           0 :         if (ctx->ana_state_str) {
     928           0 :                 if (rpc_ana_state_parse(ctx->ana_state_str, &ctx->ana_state)) {
     929           0 :                         spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
     930             :                                                          "Invalid parameters");
     931           0 :                         nvmf_rpc_listener_ctx_free(ctx);
     932           0 :                         return;
     933             :                 }
     934           0 :                 ctx->listener_opts.ana_state = ctx->ana_state;
     935             :         }
     936             : 
     937           0 :         ctx->opts.sock_impl = ctx->listener_opts.sock_impl;
     938             : 
     939           0 :         rc = spdk_nvmf_subsystem_pause(subsystem, 0, nvmf_rpc_listen_paused, ctx);
     940           0 :         if (rc != 0) {
     941           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, "Internal error");
     942           0 :                 nvmf_rpc_listener_ctx_free(ctx);
     943             :         }
     944             : }
     945           0 : SPDK_RPC_REGISTER("nvmf_subsystem_add_listener", rpc_nvmf_subsystem_add_listener,
     946             :                   SPDK_RPC_RUNTIME);
     947             : 
     948             : static void
     949           0 : rpc_nvmf_subsystem_remove_listener(struct spdk_jsonrpc_request *request,
     950             :                                    const struct spdk_json_val *params)
     951             : {
     952             :         struct nvmf_rpc_listener_ctx *ctx;
     953             :         struct spdk_nvmf_subsystem *subsystem;
     954             :         struct spdk_nvmf_tgt *tgt;
     955             :         int rc;
     956             : 
     957           0 :         ctx = calloc(1, sizeof(*ctx));
     958           0 :         if (!ctx) {
     959           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, "Out of memory");
     960           0 :                 return;
     961             :         }
     962             : 
     963           0 :         ctx->request = request;
     964             : 
     965           0 :         if (spdk_json_decode_object(params, nvmf_rpc_listener_decoder,
     966             :                                     SPDK_COUNTOF(nvmf_rpc_listener_decoder),
     967             :                                     ctx)) {
     968           0 :                 SPDK_ERRLOG("spdk_json_decode_object failed\n");
     969           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters");
     970           0 :                 nvmf_rpc_listener_ctx_free(ctx);
     971           0 :                 return;
     972             :         }
     973             : 
     974           0 :         tgt = spdk_nvmf_get_tgt(ctx->tgt_name);
     975           0 :         if (!tgt) {
     976           0 :                 SPDK_ERRLOG("Unable to find a target object.\n");
     977           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
     978             :                                                  "Unable to find a target.");
     979           0 :                 nvmf_rpc_listener_ctx_free(ctx);
     980           0 :                 return;
     981             :         }
     982           0 :         ctx->tgt = tgt;
     983             : 
     984           0 :         subsystem = spdk_nvmf_tgt_find_subsystem(tgt, ctx->nqn);
     985           0 :         if (!subsystem) {
     986           0 :                 SPDK_ERRLOG("Unable to find subsystem with NQN %s\n", ctx->nqn);
     987           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters");
     988           0 :                 nvmf_rpc_listener_ctx_free(ctx);
     989           0 :                 return;
     990             :         }
     991             : 
     992           0 :         ctx->subsystem = subsystem;
     993             : 
     994           0 :         if (rpc_listen_address_to_trid(&ctx->address, &ctx->trid)) {
     995           0 :                 spdk_jsonrpc_send_error_response(ctx->request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
     996             :                                                  "Invalid parameters");
     997           0 :                 nvmf_rpc_listener_ctx_free(ctx);
     998           0 :                 return;
     999             :         }
    1000             : 
    1001           0 :         ctx->transport = spdk_nvmf_tgt_get_transport(tgt, ctx->trid.trstring);
    1002           0 :         if (!ctx->transport) {
    1003           0 :                 SPDK_ERRLOG("Unable to find %s transport. The transport must be created first also make sure it is properly registered.\n",
    1004             :                             ctx->trid.trstring);
    1005           0 :                 spdk_jsonrpc_send_error_response(ctx->request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
    1006             :                                                  "Invalid parameters");
    1007           0 :                 nvmf_rpc_listener_ctx_free(ctx);
    1008           0 :                 return;
    1009             :         }
    1010             : 
    1011           0 :         ctx->op = NVMF_RPC_LISTEN_REMOVE;
    1012             : 
    1013           0 :         rc = spdk_nvmf_subsystem_pause(subsystem, 0, nvmf_rpc_listen_paused, ctx);
    1014           0 :         if (rc != 0) {
    1015           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, "Internal error");
    1016           0 :                 nvmf_rpc_listener_ctx_free(ctx);
    1017             :         }
    1018             : }
    1019           0 : SPDK_RPC_REGISTER("nvmf_subsystem_remove_listener", rpc_nvmf_subsystem_remove_listener,
    1020             :                   SPDK_RPC_RUNTIME);
    1021             : 
    1022             : struct nvmf_rpc_referral_ctx {
    1023             :         char                            *tgt_name;
    1024             :         struct rpc_listen_address       address;
    1025             :         bool                            secure_channel;
    1026             :         char                            *subnqn;
    1027             :         bool                            allow_any_host;
    1028             : };
    1029             : 
    1030             : static const struct spdk_json_object_decoder nvmf_rpc_referral_decoder[] = {
    1031             :         {"address", offsetof(struct nvmf_rpc_referral_ctx, address), decode_rpc_listen_address},
    1032             :         {"tgt_name", offsetof(struct nvmf_rpc_referral_ctx, tgt_name), spdk_json_decode_string, true},
    1033             :         {"secure_channel", offsetof(struct nvmf_rpc_referral_ctx, secure_channel), spdk_json_decode_bool, true},
    1034             :         {"subnqn", offsetof(struct nvmf_rpc_referral_ctx, subnqn), spdk_json_decode_string, true},
    1035             :         {"allow_any_host", offsetof(struct nvmf_rpc_referral_ctx, allow_any_host), spdk_json_decode_bool, true},
    1036             : };
    1037             : 
    1038             : static void
    1039           0 : nvmf_rpc_referral_ctx_free(struct nvmf_rpc_referral_ctx *ctx)
    1040             : {
    1041           0 :         free(ctx->tgt_name);
    1042           0 :         free(ctx->subnqn);
    1043           0 :         free_rpc_listen_address(&ctx->address);
    1044           0 : }
    1045             : 
    1046             : static void
    1047           0 : rpc_nvmf_add_referral(struct spdk_jsonrpc_request *request,
    1048             :                       const struct spdk_json_val *params)
    1049             : {
    1050           0 :         struct nvmf_rpc_referral_ctx ctx = {};
    1051           0 :         struct spdk_nvme_transport_id trid = {};
    1052             :         struct spdk_nvmf_tgt *tgt;
    1053           0 :         struct spdk_nvmf_referral_opts opts = {};
    1054             :         int rc;
    1055             : 
    1056           0 :         if (spdk_json_decode_object_relaxed(params, nvmf_rpc_referral_decoder,
    1057             :                                             SPDK_COUNTOF(nvmf_rpc_referral_decoder),
    1058             :                                             &ctx)) {
    1059           0 :                 SPDK_ERRLOG("spdk_json_decode_object_relaxed failed\n");
    1060           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters");
    1061           0 :                 nvmf_rpc_referral_ctx_free(&ctx);
    1062           0 :                 return;
    1063             :         }
    1064             : 
    1065           0 :         tgt = spdk_nvmf_get_tgt(ctx.tgt_name);
    1066           0 :         if (!tgt) {
    1067           0 :                 SPDK_ERRLOG("Unable to find a target object.\n");
    1068           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    1069             :                                                  "Unable to find a target.");
    1070           0 :                 nvmf_rpc_referral_ctx_free(&ctx);
    1071           0 :                 return;
    1072             :         }
    1073             : 
    1074           0 :         if (rpc_listen_address_to_trid(&ctx.address, &trid)) {
    1075           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
    1076             :                                                  "Invalid parameters");
    1077           0 :                 nvmf_rpc_referral_ctx_free(&ctx);
    1078           0 :                 return;
    1079             :         }
    1080             : 
    1081           0 :         if (ctx.subnqn != NULL) {
    1082           0 :                 rc = snprintf(trid.subnqn, sizeof(trid.subnqn), "%s", ctx.subnqn);
    1083           0 :                 if (rc < 0 || (size_t)rc >= sizeof(trid.subnqn)) {
    1084           0 :                         spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
    1085             :                                                          "Invalid subsystem NQN");
    1086           0 :                         nvmf_rpc_referral_ctx_free(&ctx);
    1087           0 :                         return;
    1088             :                 }
    1089             :         }
    1090             : 
    1091           0 :         if ((trid.trtype == SPDK_NVME_TRANSPORT_TCP ||
    1092           0 :              trid.trtype == SPDK_NVME_TRANSPORT_RDMA) &&
    1093           0 :             !strlen(trid.trsvcid)) {
    1094           0 :                 SPDK_ERRLOG("Service ID is required.\n");
    1095           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    1096             :                                                  "Service ID is required.");
    1097           0 :                 nvmf_rpc_referral_ctx_free(&ctx);
    1098           0 :                 return;
    1099             :         }
    1100             : 
    1101           0 :         opts.size = SPDK_SIZEOF(&opts, secure_channel);
    1102           0 :         opts.trid = trid;
    1103           0 :         opts.secure_channel = ctx.secure_channel;
    1104           0 :         opts.size = SPDK_SIZEOF(&opts, allow_any_host);
    1105           0 :         opts.allow_any_host = ctx.allow_any_host;
    1106             : 
    1107           0 :         rc = spdk_nvmf_tgt_add_referral(tgt, &opts);
    1108           0 :         if (rc != 0) {
    1109           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    1110             :                                                  "Internal error");
    1111           0 :                 nvmf_rpc_referral_ctx_free(&ctx);
    1112           0 :                 return;
    1113             :         }
    1114             : 
    1115           0 :         nvmf_rpc_referral_ctx_free(&ctx);
    1116             : 
    1117           0 :         spdk_jsonrpc_send_bool_response(request, true);
    1118             : }
    1119             : 
    1120           0 : SPDK_RPC_REGISTER("nvmf_discovery_add_referral", rpc_nvmf_add_referral,
    1121             :                   SPDK_RPC_RUNTIME);
    1122             : 
    1123             : static void
    1124           0 : rpc_nvmf_remove_referral(struct spdk_jsonrpc_request *request,
    1125             :                          const struct spdk_json_val *params)
    1126             : {
    1127           0 :         struct nvmf_rpc_referral_ctx ctx = {};
    1128           0 :         struct spdk_nvme_transport_id trid = {};
    1129           0 :         struct spdk_nvmf_referral_opts opts = {};
    1130             :         struct spdk_nvmf_tgt *tgt;
    1131             :         int rc;
    1132             : 
    1133           0 :         if (spdk_json_decode_object(params, nvmf_rpc_referral_decoder,
    1134             :                                     SPDK_COUNTOF(nvmf_rpc_referral_decoder),
    1135             :                                     &ctx)) {
    1136           0 :                 SPDK_ERRLOG("spdk_json_decode_object failed\n");
    1137           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters");
    1138           0 :                 nvmf_rpc_referral_ctx_free(&ctx);
    1139           0 :                 return;
    1140             :         }
    1141             : 
    1142           0 :         tgt = spdk_nvmf_get_tgt(ctx.tgt_name);
    1143           0 :         if (!tgt) {
    1144           0 :                 SPDK_ERRLOG("Unable to find a target object.\n");
    1145           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    1146             :                                                  "Unable to find a target.");
    1147           0 :                 nvmf_rpc_referral_ctx_free(&ctx);
    1148           0 :                 return;
    1149             :         }
    1150             : 
    1151           0 :         if (rpc_listen_address_to_trid(&ctx.address, &trid)) {
    1152           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
    1153             :                                                  "Invalid parameters");
    1154           0 :                 nvmf_rpc_referral_ctx_free(&ctx);
    1155           0 :                 return;
    1156             :         }
    1157             : 
    1158           0 :         if (ctx.subnqn != NULL) {
    1159           0 :                 rc = snprintf(trid.subnqn, sizeof(trid.subnqn), "%s", ctx.subnqn);
    1160           0 :                 if (rc < 0 || (size_t)rc >= sizeof(trid.subnqn)) {
    1161           0 :                         spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
    1162             :                                                          "Invalid subsystem NQN");
    1163           0 :                         nvmf_rpc_referral_ctx_free(&ctx);
    1164           0 :                         return;
    1165             :                 }
    1166             :         }
    1167             : 
    1168           0 :         opts.size = SPDK_SIZEOF(&opts, secure_channel);
    1169           0 :         opts.trid = trid;
    1170           0 :         opts.size = SPDK_SIZEOF(&opts, allow_any_host);
    1171             : 
    1172           0 :         if (spdk_nvmf_tgt_remove_referral(tgt, &opts)) {
    1173           0 :                 SPDK_ERRLOG("Failed to remove referral.\n");
    1174           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    1175             :                                                  "Unable to remove a referral.");
    1176           0 :                 nvmf_rpc_referral_ctx_free(&ctx);
    1177           0 :                 return;
    1178             :         }
    1179             : 
    1180           0 :         nvmf_rpc_referral_ctx_free(&ctx);
    1181             : 
    1182           0 :         spdk_jsonrpc_send_bool_response(request, true);
    1183             : }
    1184             : 
    1185           0 : SPDK_RPC_REGISTER("nvmf_discovery_remove_referral", rpc_nvmf_remove_referral,
    1186             :                   SPDK_RPC_RUNTIME);
    1187             : 
    1188             : static void
    1189           0 : dump_nvmf_referral(struct spdk_json_write_ctx *w,
    1190             :                    struct spdk_nvmf_referral *referral)
    1191             : {
    1192           0 :         spdk_json_write_object_begin(w);
    1193             : 
    1194           0 :         spdk_json_write_named_object_begin(w, "address");
    1195           0 :         nvmf_transport_listen_dump_trid(&referral->trid, w);
    1196           0 :         spdk_json_write_object_end(w);
    1197           0 :         spdk_json_write_named_bool(w, "secure_channel",
    1198           0 :                                    referral->entry.treq.secure_channel == SPDK_NVMF_TREQ_SECURE_CHANNEL_REQUIRED);
    1199           0 :         spdk_json_write_named_string(w, "subnqn", referral->trid.subnqn);
    1200           0 :         spdk_json_write_named_bool(w, "allow_any_host",
    1201           0 :                                    spdk_nvmf_referral_get_allow_any_host(referral));
    1202           0 :         spdk_json_write_object_end(w);
    1203           0 : }
    1204             : 
    1205             : struct rpc_get_referrals_ctx {
    1206             :         char *tgt_name;
    1207             : };
    1208             : 
    1209             : static const struct spdk_json_object_decoder rpc_get_referrals_decoders[] = {
    1210             :         {"tgt_name", offsetof(struct rpc_get_referrals_ctx, tgt_name), spdk_json_decode_string, true},
    1211             : };
    1212             : 
    1213             : static void
    1214           0 : free_rpc_get_referrals_ctx(struct rpc_get_referrals_ctx *ctx)
    1215             : {
    1216           0 :         free(ctx->tgt_name);
    1217           0 :         free(ctx);
    1218           0 : }
    1219             : 
    1220             : static void
    1221           0 : rpc_nvmf_get_referrals(struct spdk_jsonrpc_request *request,
    1222             :                        const struct spdk_json_val *params)
    1223             : {
    1224             :         struct rpc_get_referrals_ctx *ctx;
    1225             :         struct spdk_nvmf_tgt *tgt;
    1226             :         struct spdk_json_write_ctx *w;
    1227             :         struct spdk_nvmf_referral *referral;
    1228             : 
    1229           0 :         ctx = calloc(1, sizeof(*ctx));
    1230           0 :         if (!ctx) {
    1231           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    1232             :                                                  "Out of memory");
    1233           0 :                 return;
    1234             :         }
    1235             : 
    1236           0 :         if (params) {
    1237           0 :                 if (spdk_json_decode_object(params, rpc_get_referrals_decoders,
    1238             :                                             SPDK_COUNTOF(rpc_get_referrals_decoders),
    1239             :                                             ctx)) {
    1240           0 :                         SPDK_ERRLOG("spdk_json_decode_object failed\n");
    1241           0 :                         spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
    1242             :                                                          "Invalid parameters");
    1243           0 :                         free_rpc_get_referrals_ctx(ctx);
    1244           0 :                         return;
    1245             :                 }
    1246             :         }
    1247             : 
    1248           0 :         tgt = spdk_nvmf_get_tgt(ctx->tgt_name);
    1249           0 :         if (!tgt) {
    1250           0 :                 SPDK_ERRLOG("Unable to find a target object.\n");
    1251           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    1252             :                                                  "Unable to find a target");
    1253           0 :                 free_rpc_get_referrals_ctx(ctx);
    1254           0 :                 return;
    1255             :         }
    1256             : 
    1257           0 :         w = spdk_jsonrpc_begin_result(request);
    1258             : 
    1259           0 :         spdk_json_write_array_begin(w);
    1260             : 
    1261           0 :         TAILQ_FOREACH(referral, &tgt->referrals, link) {
    1262           0 :                 dump_nvmf_referral(w, referral);
    1263             :         }
    1264             : 
    1265           0 :         spdk_json_write_array_end(w);
    1266             : 
    1267           0 :         spdk_jsonrpc_end_result(request, w);
    1268             : 
    1269           0 :         free_rpc_get_referrals_ctx(ctx);
    1270             : }
    1271           0 : SPDK_RPC_REGISTER("nvmf_discovery_get_referrals", rpc_nvmf_get_referrals,
    1272             :                   SPDK_RPC_RUNTIME);
    1273             : 
    1274             : static const struct spdk_json_object_decoder nvmf_rpc_set_ana_state_decoder[] = {
    1275             :         {"nqn", offsetof(struct nvmf_rpc_listener_ctx, nqn), spdk_json_decode_string},
    1276             :         {"listen_address", offsetof(struct nvmf_rpc_listener_ctx, address), decode_rpc_listen_address},
    1277             :         {"ana_state", offsetof(struct nvmf_rpc_listener_ctx, ana_state_str), spdk_json_decode_string},
    1278             :         {"tgt_name", offsetof(struct nvmf_rpc_listener_ctx, tgt_name), spdk_json_decode_string, true},
    1279             :         {"anagrpid", offsetof(struct nvmf_rpc_listener_ctx, anagrpid), spdk_json_decode_uint32, true},
    1280             : };
    1281             : 
    1282             : static int
    1283           0 : rpc_ana_state_parse(const char *str, enum spdk_nvme_ana_state *ana_state)
    1284             : {
    1285           0 :         if (ana_state == NULL || str == NULL) {
    1286           0 :                 return -EINVAL;
    1287             :         }
    1288             : 
    1289           0 :         if (strcasecmp(str, "optimized") == 0) {
    1290           0 :                 *ana_state = SPDK_NVME_ANA_OPTIMIZED_STATE;
    1291           0 :         } else if (strcasecmp(str, "non_optimized") == 0) {
    1292           0 :                 *ana_state = SPDK_NVME_ANA_NON_OPTIMIZED_STATE;
    1293           0 :         } else if (strcasecmp(str, "inaccessible") == 0) {
    1294           0 :                 *ana_state = SPDK_NVME_ANA_INACCESSIBLE_STATE;
    1295             :         } else {
    1296           0 :                 return -ENOENT;
    1297             :         }
    1298             : 
    1299           0 :         return 0;
    1300             : }
    1301             : 
    1302             : static void
    1303           0 : rpc_nvmf_subsystem_listener_set_ana_state(struct spdk_jsonrpc_request *request,
    1304             :                 const struct spdk_json_val *params)
    1305             : {
    1306             :         struct nvmf_rpc_listener_ctx *ctx;
    1307             :         struct spdk_nvmf_subsystem *subsystem;
    1308             :         struct spdk_nvmf_tgt *tgt;
    1309             : 
    1310           0 :         ctx = calloc(1, sizeof(*ctx));
    1311           0 :         if (!ctx) {
    1312           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    1313             :                                                  "Out of memory");
    1314           0 :                 return;
    1315             :         }
    1316             : 
    1317           0 :         ctx->request = request;
    1318             : 
    1319           0 :         if (spdk_json_decode_object(params, nvmf_rpc_set_ana_state_decoder,
    1320             :                                     SPDK_COUNTOF(nvmf_rpc_set_ana_state_decoder),
    1321             :                                     ctx)) {
    1322           0 :                 SPDK_ERRLOG("spdk_json_decode_object failed\n");
    1323           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
    1324             :                                                  "Invalid parameters");
    1325           0 :                 nvmf_rpc_listener_ctx_free(ctx);
    1326           0 :                 return;
    1327             :         }
    1328             : 
    1329           0 :         tgt = spdk_nvmf_get_tgt(ctx->tgt_name);
    1330           0 :         if (!tgt) {
    1331           0 :                 SPDK_ERRLOG("Unable to find a target object.\n");
    1332           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    1333             :                                                  "Unable to find a target.");
    1334           0 :                 nvmf_rpc_listener_ctx_free(ctx);
    1335           0 :                 return;
    1336             :         }
    1337             : 
    1338           0 :         ctx->tgt = tgt;
    1339             : 
    1340           0 :         subsystem = spdk_nvmf_tgt_find_subsystem(tgt, ctx->nqn);
    1341           0 :         if (!subsystem) {
    1342           0 :                 SPDK_ERRLOG("Unable to find subsystem with NQN %s\n", ctx->nqn);
    1343           0 :                 spdk_jsonrpc_send_error_response_fmt(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
    1344             :                                                      "Unable to find subsystem with NQN %s",
    1345             :                                                      ctx->nqn);
    1346           0 :                 nvmf_rpc_listener_ctx_free(ctx);
    1347           0 :                 return;
    1348             :         }
    1349             : 
    1350           0 :         ctx->subsystem = subsystem;
    1351             : 
    1352           0 :         if (rpc_listen_address_to_trid(&ctx->address, &ctx->trid)) {
    1353           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
    1354             :                                                  "Invalid parameters");
    1355           0 :                 nvmf_rpc_listener_ctx_free(ctx);
    1356           0 :                 return;
    1357             :         }
    1358             : 
    1359           0 :         if (rpc_ana_state_parse(ctx->ana_state_str, &ctx->ana_state)) {
    1360           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
    1361             :                                                  "Invalid parameters");
    1362           0 :                 nvmf_rpc_listener_ctx_free(ctx);
    1363           0 :                 return;
    1364             :         }
    1365             : 
    1366           0 :         ctx->op = NVMF_RPC_LISTEN_SET_ANA_STATE;
    1367             : 
    1368           0 :         if (spdk_nvmf_subsystem_pause(subsystem, 0, nvmf_rpc_listen_paused, ctx)) {
    1369           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    1370             :                                                  "Internal error");
    1371           0 :                 nvmf_rpc_listener_ctx_free(ctx);
    1372             :         }
    1373             : }
    1374           0 : SPDK_RPC_REGISTER("nvmf_subsystem_listener_set_ana_state",
    1375             :                   rpc_nvmf_subsystem_listener_set_ana_state, SPDK_RPC_RUNTIME);
    1376             : 
    1377             : struct nvmf_rpc_ns_params {
    1378             :         char *bdev_name;
    1379             :         char *ptpl_file;
    1380             :         uint32_t nsid;
    1381             :         char nguid[16];
    1382             :         char eui64[8];
    1383             :         struct spdk_uuid uuid;
    1384             :         uint32_t anagrpid;
    1385             :         bool no_auto_visible;
    1386             : };
    1387             : 
    1388             : static const struct spdk_json_object_decoder rpc_ns_params_decoders[] = {
    1389             :         {"nsid", offsetof(struct nvmf_rpc_ns_params, nsid), spdk_json_decode_uint32, true},
    1390             :         {"bdev_name", offsetof(struct nvmf_rpc_ns_params, bdev_name), spdk_json_decode_string},
    1391             :         {"ptpl_file", offsetof(struct nvmf_rpc_ns_params, ptpl_file), spdk_json_decode_string, true},
    1392             :         {"nguid", offsetof(struct nvmf_rpc_ns_params, nguid), decode_ns_nguid, true},
    1393             :         {"eui64", offsetof(struct nvmf_rpc_ns_params, eui64), decode_ns_eui64, true},
    1394             :         {"uuid", offsetof(struct nvmf_rpc_ns_params, uuid), spdk_json_decode_uuid, true},
    1395             :         {"anagrpid", offsetof(struct nvmf_rpc_ns_params, anagrpid), spdk_json_decode_uint32, true},
    1396             :         {"no_auto_visible", offsetof(struct nvmf_rpc_ns_params, no_auto_visible), spdk_json_decode_bool, true},
    1397             : };
    1398             : 
    1399             : static int
    1400           0 : decode_rpc_ns_params(const struct spdk_json_val *val, void *out)
    1401             : {
    1402           0 :         struct nvmf_rpc_ns_params *ns_params = out;
    1403             : 
    1404           0 :         return spdk_json_decode_object(val, rpc_ns_params_decoders,
    1405             :                                        SPDK_COUNTOF(rpc_ns_params_decoders),
    1406             :                                        ns_params);
    1407             : }
    1408             : 
    1409             : struct nvmf_rpc_ns_ctx {
    1410             :         char *nqn;
    1411             :         char *tgt_name;
    1412             :         struct nvmf_rpc_ns_params ns_params;
    1413             : 
    1414             :         struct spdk_jsonrpc_request *request;
    1415             :         const struct spdk_json_val *params;
    1416             :         bool response_sent;
    1417             : };
    1418             : 
    1419             : static const struct spdk_json_object_decoder nvmf_rpc_subsystem_ns_decoder[] = {
    1420             :         {"nqn", offsetof(struct nvmf_rpc_ns_ctx, nqn), spdk_json_decode_string},
    1421             :         {"namespace", offsetof(struct nvmf_rpc_ns_ctx, ns_params), decode_rpc_ns_params},
    1422             :         {"tgt_name", offsetof(struct nvmf_rpc_ns_ctx, tgt_name), spdk_json_decode_string, true},
    1423             : };
    1424             : 
    1425             : static void
    1426           0 : nvmf_rpc_ns_ctx_free(struct nvmf_rpc_ns_ctx *ctx)
    1427             : {
    1428           0 :         free(ctx->nqn);
    1429           0 :         free(ctx->tgt_name);
    1430           0 :         free(ctx->ns_params.bdev_name);
    1431           0 :         free(ctx->ns_params.ptpl_file);
    1432           0 :         free(ctx);
    1433           0 : }
    1434             : 
    1435             : static void
    1436           0 : nvmf_rpc_ns_failback_resumed(struct spdk_nvmf_subsystem *subsystem,
    1437             :                              void *cb_arg, int status)
    1438             : {
    1439           0 :         struct nvmf_rpc_ns_ctx *ctx = cb_arg;
    1440           0 :         struct spdk_jsonrpc_request *request = ctx->request;
    1441             : 
    1442           0 :         if (status) {
    1443           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    1444             :                                                  "Unable to add ns, subsystem in invalid state");
    1445             :         } else {
    1446           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    1447             :                                                  "Unable to add ns, subsystem in active state");
    1448             :         }
    1449             : 
    1450           0 :         nvmf_rpc_ns_ctx_free(ctx);
    1451           0 : }
    1452             : 
    1453             : static void
    1454           0 : nvmf_rpc_ns_resumed(struct spdk_nvmf_subsystem *subsystem,
    1455             :                     void *cb_arg, int status)
    1456             : {
    1457           0 :         struct nvmf_rpc_ns_ctx *ctx = cb_arg;
    1458           0 :         struct spdk_jsonrpc_request *request = ctx->request;
    1459           0 :         uint32_t nsid = ctx->ns_params.nsid;
    1460           0 :         bool response_sent = ctx->response_sent;
    1461             :         struct spdk_json_write_ctx *w;
    1462             :         int rc;
    1463             : 
    1464             :         /* The case where the call to add the namespace was successful, but the subsystem couldn't be resumed. */
    1465           0 :         if (status && !ctx->response_sent) {
    1466           0 :                 rc = spdk_nvmf_subsystem_remove_ns(subsystem, nsid);
    1467           0 :                 if (rc != 0) {
    1468           0 :                         spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    1469             :                                                          "Unable to add ns, subsystem in invalid state");
    1470           0 :                         nvmf_rpc_ns_ctx_free(ctx);
    1471           0 :                         return;
    1472             :                 }
    1473             : 
    1474           0 :                 rc = spdk_nvmf_subsystem_resume(subsystem, nvmf_rpc_ns_failback_resumed, ctx);
    1475           0 :                 if (rc != 0) {
    1476           0 :                         spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, "Internal error");
    1477           0 :                         nvmf_rpc_ns_ctx_free(ctx);
    1478           0 :                         return;
    1479             :                 }
    1480             : 
    1481           0 :                 return;
    1482             :         }
    1483             : 
    1484           0 :         nvmf_rpc_ns_ctx_free(ctx);
    1485             : 
    1486           0 :         if (response_sent) {
    1487           0 :                 return;
    1488             :         }
    1489             : 
    1490           0 :         w = spdk_jsonrpc_begin_result(request);
    1491           0 :         spdk_json_write_uint32(w, nsid);
    1492           0 :         spdk_jsonrpc_end_result(request, w);
    1493             : }
    1494             : 
    1495             : static void
    1496           0 : nvmf_rpc_ns_paused(struct spdk_nvmf_subsystem *subsystem,
    1497             :                    void *cb_arg, int status)
    1498             : {
    1499           0 :         struct nvmf_rpc_ns_ctx *ctx = cb_arg;
    1500           0 :         struct spdk_nvmf_ns_opts ns_opts;
    1501             : 
    1502           0 :         spdk_nvmf_ns_opts_get_defaults(&ns_opts, sizeof(ns_opts));
    1503           0 :         ns_opts.nsid = ctx->ns_params.nsid;
    1504           0 :         ns_opts.transport_specific = ctx->params;
    1505             : 
    1506             :         SPDK_STATIC_ASSERT(sizeof(ns_opts.nguid) == sizeof(ctx->ns_params.nguid), "size mismatch");
    1507           0 :         memcpy(ns_opts.nguid, ctx->ns_params.nguid, sizeof(ns_opts.nguid));
    1508             : 
    1509             :         SPDK_STATIC_ASSERT(sizeof(ns_opts.eui64) == sizeof(ctx->ns_params.eui64), "size mismatch");
    1510           0 :         memcpy(ns_opts.eui64, ctx->ns_params.eui64, sizeof(ns_opts.eui64));
    1511             : 
    1512           0 :         if (!spdk_uuid_is_null(&ctx->ns_params.uuid)) {
    1513           0 :                 ns_opts.uuid = ctx->ns_params.uuid;
    1514             :         }
    1515             : 
    1516           0 :         ns_opts.anagrpid = ctx->ns_params.anagrpid;
    1517           0 :         ns_opts.no_auto_visible = ctx->ns_params.no_auto_visible;
    1518             : 
    1519           0 :         ctx->ns_params.nsid = spdk_nvmf_subsystem_add_ns_ext(subsystem, ctx->ns_params.bdev_name,
    1520             :                               &ns_opts, sizeof(ns_opts),
    1521           0 :                               ctx->ns_params.ptpl_file);
    1522           0 :         if (ctx->ns_params.nsid == 0) {
    1523           0 :                 SPDK_ERRLOG("Unable to add namespace\n");
    1524           0 :                 spdk_jsonrpc_send_error_response(ctx->request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
    1525             :                                                  "Invalid parameters");
    1526           0 :                 ctx->response_sent = true;
    1527           0 :                 goto resume;
    1528             :         }
    1529             : 
    1530           0 : resume:
    1531           0 :         if (spdk_nvmf_subsystem_resume(subsystem, nvmf_rpc_ns_resumed, ctx)) {
    1532           0 :                 spdk_jsonrpc_send_error_response(ctx->request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, "Internal error");
    1533           0 :                 nvmf_rpc_ns_ctx_free(ctx);
    1534             :         }
    1535           0 : }
    1536             : 
    1537             : static void
    1538           0 : rpc_nvmf_subsystem_add_ns(struct spdk_jsonrpc_request *request,
    1539             :                           const struct spdk_json_val *params)
    1540             : {
    1541             :         struct nvmf_rpc_ns_ctx *ctx;
    1542             :         struct spdk_nvmf_subsystem *subsystem;
    1543             :         struct spdk_nvmf_tgt *tgt;
    1544             :         int rc;
    1545             : 
    1546           0 :         ctx = calloc(1, sizeof(*ctx));
    1547           0 :         if (!ctx) {
    1548           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, "Out of memory");
    1549           0 :                 return;
    1550             :         }
    1551             : 
    1552           0 :         if (spdk_json_decode_object_relaxed(params, nvmf_rpc_subsystem_ns_decoder,
    1553             :                                             SPDK_COUNTOF(nvmf_rpc_subsystem_ns_decoder), ctx)) {
    1554           0 :                 SPDK_ERRLOG("spdk_json_decode_object failed\n");
    1555           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters");
    1556           0 :                 nvmf_rpc_ns_ctx_free(ctx);
    1557           0 :                 return;
    1558             :         }
    1559             : 
    1560           0 :         ctx->request = request;
    1561           0 :         ctx->params = params;
    1562           0 :         ctx->response_sent = false;
    1563             : 
    1564           0 :         tgt = spdk_nvmf_get_tgt(ctx->tgt_name);
    1565           0 :         if (!tgt) {
    1566           0 :                 SPDK_ERRLOG("Unable to find a target object.\n");
    1567           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    1568             :                                                  "Unable to find a target.");
    1569           0 :                 nvmf_rpc_ns_ctx_free(ctx);
    1570           0 :                 return;
    1571             :         }
    1572             : 
    1573           0 :         subsystem = spdk_nvmf_tgt_find_subsystem(tgt, ctx->nqn);
    1574           0 :         if (!subsystem) {
    1575           0 :                 SPDK_ERRLOG("Unable to find subsystem with NQN %s\n", ctx->nqn);
    1576           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters");
    1577           0 :                 nvmf_rpc_ns_ctx_free(ctx);
    1578           0 :                 return;
    1579             :         }
    1580             : 
    1581           0 :         rc = spdk_nvmf_subsystem_pause(subsystem, ctx->ns_params.nsid, nvmf_rpc_ns_paused, ctx);
    1582           0 :         if (rc != 0) {
    1583           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, "Internal error");
    1584           0 :                 nvmf_rpc_ns_ctx_free(ctx);
    1585             :         }
    1586             : }
    1587           0 : SPDK_RPC_REGISTER("nvmf_subsystem_add_ns", rpc_nvmf_subsystem_add_ns, SPDK_RPC_RUNTIME)
    1588             : 
    1589             : struct nvmf_rpc_ana_group_ctx {
    1590             :         char *nqn;
    1591             :         char *tgt_name;
    1592             :         uint32_t nsid;
    1593             :         uint32_t anagrpid;
    1594             : 
    1595             :         struct spdk_jsonrpc_request *request;
    1596             :         bool response_sent;
    1597             : };
    1598             : 
    1599             : static const struct spdk_json_object_decoder nvmf_rpc_subsystem_ana_group_decoder[] = {
    1600             :         {"nqn", offsetof(struct nvmf_rpc_ana_group_ctx, nqn), spdk_json_decode_string},
    1601             :         {"nsid", offsetof(struct nvmf_rpc_ana_group_ctx, nsid), spdk_json_decode_uint32},
    1602             :         {"anagrpid", offsetof(struct nvmf_rpc_ana_group_ctx, anagrpid), spdk_json_decode_uint32},
    1603             :         {"tgt_name", offsetof(struct nvmf_rpc_ana_group_ctx, tgt_name), spdk_json_decode_string, true},
    1604             : };
    1605             : 
    1606             : static void
    1607           0 : nvmf_rpc_ana_group_ctx_free(struct nvmf_rpc_ana_group_ctx *ctx)
    1608             : {
    1609           0 :         free(ctx->nqn);
    1610           0 :         free(ctx->tgt_name);
    1611           0 :         free(ctx);
    1612           0 : }
    1613             : 
    1614             : static void
    1615           0 : nvmf_rpc_anagrpid_resumed(struct spdk_nvmf_subsystem *subsystem,
    1616             :                           void *cb_arg, int status)
    1617             : {
    1618           0 :         struct nvmf_rpc_ana_group_ctx *ctx = cb_arg;
    1619           0 :         struct spdk_jsonrpc_request *request = ctx->request;
    1620           0 :         bool response_sent = ctx->response_sent;
    1621             : 
    1622           0 :         nvmf_rpc_ana_group_ctx_free(ctx);
    1623             : 
    1624           0 :         if (response_sent) {
    1625           0 :                 return;
    1626             :         }
    1627             : 
    1628           0 :         spdk_jsonrpc_send_bool_response(request, true);
    1629             : }
    1630             : 
    1631             : static void
    1632           0 : nvmf_rpc_ana_group(struct spdk_nvmf_subsystem *subsystem,
    1633             :                    void *cb_arg, int status)
    1634             : {
    1635           0 :         struct nvmf_rpc_ana_group_ctx *ctx = cb_arg;
    1636             :         int rc;
    1637             : 
    1638           0 :         rc = spdk_nvmf_subsystem_set_ns_ana_group(subsystem, ctx->nsid, ctx->anagrpid);
    1639           0 :         if (rc != 0) {
    1640           0 :                 SPDK_ERRLOG("Unable to change ANA group ID\n");
    1641           0 :                 spdk_jsonrpc_send_error_response(ctx->request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
    1642             :                                                  "Invalid parameters");
    1643           0 :                 ctx->response_sent = true;
    1644             :         }
    1645             : 
    1646           0 :         if (spdk_nvmf_subsystem_resume(subsystem, nvmf_rpc_anagrpid_resumed, ctx)) {
    1647           0 :                 if (!ctx->response_sent) {
    1648           0 :                         spdk_jsonrpc_send_error_response(ctx->request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    1649             :                                                          "Internal error");
    1650             :                 }
    1651           0 :                 nvmf_rpc_ana_group_ctx_free(ctx);
    1652             :         }
    1653           0 : }
    1654             : 
    1655             : static void
    1656           0 : rpc_nvmf_subsystem_set_ns_ana_group(struct spdk_jsonrpc_request *request,
    1657             :                                     const struct spdk_json_val *params)
    1658             : {
    1659             :         struct nvmf_rpc_ana_group_ctx *ctx;
    1660             :         struct spdk_nvmf_subsystem *subsystem;
    1661             :         struct spdk_nvmf_tgt *tgt;
    1662             :         int rc;
    1663             : 
    1664           0 :         ctx = calloc(1, sizeof(*ctx));
    1665           0 :         if (!ctx) {
    1666           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, "Out of memory");
    1667           0 :                 return;
    1668             :         }
    1669             : 
    1670           0 :         if (spdk_json_decode_object(params, nvmf_rpc_subsystem_ana_group_decoder,
    1671             :                                     SPDK_COUNTOF(nvmf_rpc_subsystem_ana_group_decoder), ctx)) {
    1672           0 :                 SPDK_ERRLOG("spdk_json_decode_object failed\n");
    1673           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters");
    1674           0 :                 nvmf_rpc_ana_group_ctx_free(ctx);
    1675           0 :                 return;
    1676             :         }
    1677             : 
    1678           0 :         ctx->request = request;
    1679           0 :         ctx->response_sent = false;
    1680             : 
    1681           0 :         tgt = spdk_nvmf_get_tgt(ctx->tgt_name);
    1682           0 :         if (!tgt) {
    1683           0 :                 SPDK_ERRLOG("Unable to find a target object.\n");
    1684           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    1685             :                                                  "Unable to find a target.");
    1686           0 :                 nvmf_rpc_ana_group_ctx_free(ctx);
    1687           0 :                 return;
    1688             :         }
    1689             : 
    1690           0 :         subsystem = spdk_nvmf_tgt_find_subsystem(tgt, ctx->nqn);
    1691           0 :         if (!subsystem) {
    1692           0 :                 SPDK_ERRLOG("Unable to find subsystem with NQN %s\n", ctx->nqn);
    1693           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters");
    1694           0 :                 nvmf_rpc_ana_group_ctx_free(ctx);
    1695           0 :                 return;
    1696             :         }
    1697             : 
    1698           0 :         rc = spdk_nvmf_subsystem_pause(subsystem, ctx->nsid, nvmf_rpc_ana_group, ctx);
    1699           0 :         if (rc != 0) {
    1700           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, "Internal error");
    1701           0 :                 nvmf_rpc_ana_group_ctx_free(ctx);
    1702             :         }
    1703             : }
    1704           0 : SPDK_RPC_REGISTER("nvmf_subsystem_set_ns_ana_group", rpc_nvmf_subsystem_set_ns_ana_group,
    1705             :                   SPDK_RPC_RUNTIME)
    1706             : 
    1707             : struct nvmf_rpc_remove_ns_ctx {
    1708             :         char *nqn;
    1709             :         char *tgt_name;
    1710             :         uint32_t nsid;
    1711             : 
    1712             :         struct spdk_jsonrpc_request *request;
    1713             :         bool response_sent;
    1714             : };
    1715             : 
    1716             : static const struct spdk_json_object_decoder nvmf_rpc_subsystem_remove_ns_decoder[] = {
    1717             :         {"nqn", offsetof(struct nvmf_rpc_remove_ns_ctx, nqn), spdk_json_decode_string},
    1718             :         {"nsid", offsetof(struct nvmf_rpc_remove_ns_ctx, nsid), spdk_json_decode_uint32},
    1719             :         {"tgt_name", offsetof(struct nvmf_rpc_remove_ns_ctx, tgt_name), spdk_json_decode_string, true},
    1720             : };
    1721             : 
    1722             : static void
    1723           0 : nvmf_rpc_remove_ns_ctx_free(struct nvmf_rpc_remove_ns_ctx *ctx)
    1724             : {
    1725           0 :         free(ctx->nqn);
    1726           0 :         free(ctx->tgt_name);
    1727           0 :         free(ctx);
    1728           0 : }
    1729             : 
    1730             : static void
    1731           0 : nvmf_rpc_remove_ns_resumed(struct spdk_nvmf_subsystem *subsystem,
    1732             :                            void *cb_arg, int status)
    1733             : {
    1734           0 :         struct nvmf_rpc_remove_ns_ctx *ctx = cb_arg;
    1735           0 :         struct spdk_jsonrpc_request *request = ctx->request;
    1736           0 :         bool response_sent = ctx->response_sent;
    1737             : 
    1738           0 :         nvmf_rpc_remove_ns_ctx_free(ctx);
    1739             : 
    1740           0 :         if (response_sent) {
    1741           0 :                 return;
    1742             :         }
    1743             : 
    1744           0 :         spdk_jsonrpc_send_bool_response(request, true);
    1745             : }
    1746             : 
    1747             : static void
    1748           0 : nvmf_rpc_remove_ns_paused(struct spdk_nvmf_subsystem *subsystem,
    1749             :                           void *cb_arg, int status)
    1750             : {
    1751           0 :         struct nvmf_rpc_remove_ns_ctx *ctx = cb_arg;
    1752             :         int ret;
    1753             : 
    1754           0 :         ret = spdk_nvmf_subsystem_remove_ns(subsystem, ctx->nsid);
    1755           0 :         if (ret < 0) {
    1756           0 :                 SPDK_ERRLOG("Unable to remove namespace ID %u\n", ctx->nsid);
    1757           0 :                 spdk_jsonrpc_send_error_response(ctx->request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
    1758             :                                                  "Invalid parameters");
    1759           0 :                 ctx->response_sent = true;
    1760             :         }
    1761             : 
    1762           0 :         if (spdk_nvmf_subsystem_resume(subsystem, nvmf_rpc_remove_ns_resumed, ctx)) {
    1763           0 :                 if (!ctx->response_sent) {
    1764           0 :                         spdk_jsonrpc_send_error_response(ctx->request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, "Internal error");
    1765             :                 }
    1766           0 :                 nvmf_rpc_remove_ns_ctx_free(ctx);
    1767             :         }
    1768           0 : }
    1769             : 
    1770             : static void
    1771           0 : rpc_nvmf_subsystem_remove_ns(struct spdk_jsonrpc_request *request,
    1772             :                              const struct spdk_json_val *params)
    1773             : {
    1774             :         struct nvmf_rpc_remove_ns_ctx *ctx;
    1775             :         struct spdk_nvmf_subsystem *subsystem;
    1776             :         struct spdk_nvmf_tgt *tgt;
    1777             :         int rc;
    1778             : 
    1779           0 :         ctx = calloc(1, sizeof(*ctx));
    1780           0 :         if (!ctx) {
    1781           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, "Out of memory");
    1782           0 :                 return;
    1783             :         }
    1784             : 
    1785           0 :         if (spdk_json_decode_object(params, nvmf_rpc_subsystem_remove_ns_decoder,
    1786             :                                     SPDK_COUNTOF(nvmf_rpc_subsystem_remove_ns_decoder),
    1787             :                                     ctx)) {
    1788           0 :                 SPDK_ERRLOG("spdk_json_decode_object failed\n");
    1789           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters");
    1790           0 :                 nvmf_rpc_remove_ns_ctx_free(ctx);
    1791           0 :                 return;
    1792             :         }
    1793             : 
    1794           0 :         tgt = spdk_nvmf_get_tgt(ctx->tgt_name);
    1795           0 :         if (!tgt) {
    1796           0 :                 SPDK_ERRLOG("Unable to find a target object.\n");
    1797           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    1798             :                                                  "Unable to find a target.");
    1799           0 :                 nvmf_rpc_remove_ns_ctx_free(ctx);
    1800           0 :                 return;
    1801             :         }
    1802             : 
    1803           0 :         ctx->request = request;
    1804           0 :         ctx->response_sent = false;
    1805             : 
    1806           0 :         subsystem = spdk_nvmf_tgt_find_subsystem(tgt, ctx->nqn);
    1807           0 :         if (!subsystem) {
    1808           0 :                 SPDK_ERRLOG("Unable to find subsystem with NQN %s\n", ctx->nqn);
    1809           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters");
    1810           0 :                 nvmf_rpc_remove_ns_ctx_free(ctx);
    1811           0 :                 return;
    1812             :         }
    1813             : 
    1814           0 :         rc = spdk_nvmf_subsystem_pause(subsystem, ctx->nsid, nvmf_rpc_remove_ns_paused, ctx);
    1815           0 :         if (rc != 0) {
    1816           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, "Internal error");
    1817           0 :                 nvmf_rpc_remove_ns_ctx_free(ctx);
    1818             :         }
    1819             : }
    1820           0 : SPDK_RPC_REGISTER("nvmf_subsystem_remove_ns", rpc_nvmf_subsystem_remove_ns, SPDK_RPC_RUNTIME)
    1821             : 
    1822             : struct nvmf_rpc_ns_visible_ctx {
    1823             :         struct spdk_jsonrpc_request *request;
    1824             :         char *nqn;
    1825             :         uint32_t nsid;
    1826             :         char *host;
    1827             :         char *tgt_name;
    1828             :         bool visible;
    1829             :         bool response_sent;
    1830             : };
    1831             : 
    1832             : static const struct spdk_json_object_decoder nvmf_rpc_ns_visible_decoder[] = {
    1833             :         {"nqn", offsetof(struct nvmf_rpc_ns_visible_ctx, nqn), spdk_json_decode_string},
    1834             :         {"nsid", offsetof(struct nvmf_rpc_ns_visible_ctx, nsid), spdk_json_decode_uint32},
    1835             :         {"host", offsetof(struct nvmf_rpc_ns_visible_ctx, host), spdk_json_decode_string},
    1836             :         {"tgt_name", offsetof(struct nvmf_rpc_ns_visible_ctx, tgt_name), spdk_json_decode_string, true},
    1837             : };
    1838             : 
    1839             : static void
    1840           0 : nvmf_rpc_ns_visible_ctx_free(struct nvmf_rpc_ns_visible_ctx *ctx)
    1841             : {
    1842           0 :         free(ctx->nqn);
    1843           0 :         free(ctx->host);
    1844           0 :         free(ctx->tgt_name);
    1845           0 :         free(ctx);
    1846           0 : }
    1847             : 
    1848             : static void
    1849           0 : nvmf_rpc_ns_visible_resumed(struct spdk_nvmf_subsystem *subsystem,
    1850             :                             void *cb_arg, int status)
    1851             : {
    1852           0 :         struct nvmf_rpc_ns_visible_ctx *ctx = cb_arg;
    1853           0 :         struct spdk_jsonrpc_request *request = ctx->request;
    1854           0 :         bool response_sent = ctx->response_sent;
    1855             : 
    1856           0 :         nvmf_rpc_ns_visible_ctx_free(ctx);
    1857             : 
    1858           0 :         if (!response_sent) {
    1859           0 :                 spdk_jsonrpc_send_bool_response(request, true);
    1860             :         }
    1861           0 : }
    1862             : 
    1863             : static void
    1864           0 : nvmf_rpc_ns_visible_paused(struct spdk_nvmf_subsystem *subsystem,
    1865             :                            void *cb_arg, int status)
    1866             : {
    1867           0 :         struct nvmf_rpc_ns_visible_ctx *ctx = cb_arg;
    1868             :         int ret;
    1869             : 
    1870           0 :         if (ctx->visible) {
    1871           0 :                 ret = spdk_nvmf_ns_add_host(subsystem, ctx->nsid, ctx->host, 0);
    1872             :         } else {
    1873           0 :                 ret = spdk_nvmf_ns_remove_host(subsystem, ctx->nsid, ctx->host, 0);
    1874             :         }
    1875           0 :         if (ret < 0) {
    1876           0 :                 SPDK_ERRLOG("Unable to add/remove %s to namespace ID %u\n", ctx->host, ctx->nsid);
    1877           0 :                 spdk_jsonrpc_send_error_response(ctx->request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
    1878             :                                                  "Invalid parameters");
    1879           0 :                 ctx->response_sent = true;
    1880             :         }
    1881             : 
    1882           0 :         if (spdk_nvmf_subsystem_resume(subsystem, nvmf_rpc_ns_visible_resumed, ctx)) {
    1883           0 :                 if (!ctx->response_sent) {
    1884           0 :                         spdk_jsonrpc_send_error_response(ctx->request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, "Internal error");
    1885             :                 }
    1886           0 :                 nvmf_rpc_ns_visible_ctx_free(ctx);
    1887             :         }
    1888           0 : }
    1889             : 
    1890             : static void
    1891           0 : nvmf_rpc_ns_visible(struct spdk_jsonrpc_request *request,
    1892             :                     const struct spdk_json_val *params,
    1893             :                     bool visible)
    1894             : {
    1895             :         struct nvmf_rpc_ns_visible_ctx *ctx;
    1896             :         struct spdk_nvmf_subsystem *subsystem;
    1897             :         struct spdk_nvmf_tgt *tgt;
    1898             :         int rc;
    1899             : 
    1900           0 :         ctx = calloc(1, sizeof(*ctx));
    1901           0 :         if (!ctx) {
    1902           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, "Out of memory");
    1903           0 :                 return;
    1904             :         }
    1905           0 :         ctx->visible = visible;
    1906             : 
    1907           0 :         if (spdk_json_decode_object(params, nvmf_rpc_ns_visible_decoder,
    1908             :                                     SPDK_COUNTOF(nvmf_rpc_ns_visible_decoder),
    1909             :                                     ctx)) {
    1910           0 :                 SPDK_ERRLOG("spdk_json_decode_object failed\n");
    1911           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters");
    1912           0 :                 nvmf_rpc_ns_visible_ctx_free(ctx);
    1913           0 :                 return;
    1914             :         }
    1915           0 :         ctx->request = request;
    1916             : 
    1917           0 :         tgt = spdk_nvmf_get_tgt(ctx->tgt_name);
    1918           0 :         if (!tgt) {
    1919           0 :                 SPDK_ERRLOG("Unable to find a target object.\n");
    1920           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    1921             :                                                  "Unable to find a target.");
    1922           0 :                 nvmf_rpc_ns_visible_ctx_free(ctx);
    1923           0 :                 return;
    1924             :         }
    1925             : 
    1926           0 :         subsystem = spdk_nvmf_tgt_find_subsystem(tgt, ctx->nqn);
    1927           0 :         if (!subsystem) {
    1928           0 :                 SPDK_ERRLOG("Unable to find subsystem with NQN %s\n", ctx->nqn);
    1929           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters");
    1930           0 :                 nvmf_rpc_ns_visible_ctx_free(ctx);
    1931           0 :                 return;
    1932             :         }
    1933             : 
    1934           0 :         rc = spdk_nvmf_subsystem_pause(subsystem, ctx->nsid, nvmf_rpc_ns_visible_paused, ctx);
    1935           0 :         if (rc != 0) {
    1936           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, "Internal error");
    1937           0 :                 nvmf_rpc_ns_visible_ctx_free(ctx);
    1938             :         }
    1939             : }
    1940             : 
    1941             : static void
    1942           0 : rpc_nvmf_ns_add_host(struct spdk_jsonrpc_request *request,
    1943             :                      const struct spdk_json_val *params)
    1944             : {
    1945           0 :         nvmf_rpc_ns_visible(request, params, true);
    1946           0 : }
    1947           0 : SPDK_RPC_REGISTER("nvmf_ns_add_host", rpc_nvmf_ns_add_host, SPDK_RPC_RUNTIME)
    1948             : 
    1949             : static void
    1950           0 : rpc_nvmf_ns_remove_host(struct spdk_jsonrpc_request *request,
    1951             :                         const struct spdk_json_val *params)
    1952             : {
    1953           0 :         nvmf_rpc_ns_visible(request, params, false);
    1954           0 : }
    1955           0 : SPDK_RPC_REGISTER("nvmf_ns_remove_host", rpc_nvmf_ns_remove_host, SPDK_RPC_RUNTIME)
    1956             : 
    1957             : struct nvmf_rpc_host_ctx {
    1958             :         struct spdk_jsonrpc_request *request;
    1959             :         char *nqn;
    1960             :         char *host;
    1961             :         char *tgt_name;
    1962             :         char *dhchap_key;
    1963             :         char *dhchap_ctrlr_key;
    1964             :         bool allow_any_host;
    1965             : };
    1966             : 
    1967             : static const struct spdk_json_object_decoder nvmf_rpc_subsystem_host_decoder[] = {
    1968             :         {"nqn", offsetof(struct nvmf_rpc_host_ctx, nqn), spdk_json_decode_string},
    1969             :         {"host", offsetof(struct nvmf_rpc_host_ctx, host), spdk_json_decode_string},
    1970             :         {"tgt_name", offsetof(struct nvmf_rpc_host_ctx, tgt_name), spdk_json_decode_string, true},
    1971             :         {"dhchap_key", offsetof(struct nvmf_rpc_host_ctx, dhchap_key), spdk_json_decode_string, true},
    1972             :         {"dhchap_ctrlr_key", offsetof(struct nvmf_rpc_host_ctx, dhchap_ctrlr_key), spdk_json_decode_string, true},
    1973             : };
    1974             : 
    1975             : static const struct spdk_json_object_decoder nvmf_rpc_referral_host_decoder[] = {
    1976             :         {"nqn", offsetof(struct nvmf_rpc_host_ctx, nqn), spdk_json_decode_string},
    1977             :         {"host", offsetof(struct nvmf_rpc_host_ctx, host), spdk_json_decode_string},
    1978             :         {"tgt_name", offsetof(struct nvmf_rpc_host_ctx, tgt_name), spdk_json_decode_string, true},
    1979             :         {"dhchap_key", offsetof(struct nvmf_rpc_host_ctx, dhchap_key), spdk_json_decode_string, true},
    1980             :         {"dhchap_ctrlr_key", offsetof(struct nvmf_rpc_host_ctx, dhchap_ctrlr_key), spdk_json_decode_string, true},
    1981             : };
    1982             : 
    1983             : static void
    1984           0 : nvmf_rpc_host_ctx_free(struct nvmf_rpc_host_ctx *ctx)
    1985             : {
    1986           0 :         free(ctx->nqn);
    1987           0 :         free(ctx->host);
    1988           0 :         free(ctx->tgt_name);
    1989           0 :         free(ctx->dhchap_key);
    1990           0 :         free(ctx->dhchap_ctrlr_key);
    1991           0 : }
    1992             : 
    1993             : static void
    1994           0 : rpc_nvmf_subsystem_add_host(struct spdk_jsonrpc_request *request,
    1995             :                             const struct spdk_json_val *params)
    1996             : {
    1997           0 :         struct nvmf_rpc_host_ctx ctx = {};
    1998             :         struct spdk_nvmf_subsystem *subsystem;
    1999           0 :         struct spdk_nvmf_host_opts opts = {};
    2000             :         struct spdk_nvmf_tgt *tgt;
    2001           0 :         struct spdk_key *key = NULL, *ckey = NULL;
    2002             :         int rc;
    2003             : 
    2004           0 :         if (spdk_json_decode_object_relaxed(params, nvmf_rpc_subsystem_host_decoder,
    2005             :                                             SPDK_COUNTOF(nvmf_rpc_subsystem_host_decoder),
    2006             :                                             &ctx)) {
    2007           0 :                 SPDK_ERRLOG("spdk_json_decode_object failed\n");
    2008           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters");
    2009           0 :                 goto out;
    2010             :         }
    2011             : 
    2012           0 :         tgt = spdk_nvmf_get_tgt(ctx.tgt_name);
    2013           0 :         if (!tgt) {
    2014           0 :                 SPDK_ERRLOG("Unable to find a target object.\n");
    2015           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    2016             :                                                  "Unable to find a target.");
    2017           0 :                 goto out;
    2018             :         }
    2019             : 
    2020           0 :         subsystem = spdk_nvmf_tgt_find_subsystem(tgt, ctx.nqn);
    2021           0 :         if (!subsystem) {
    2022           0 :                 SPDK_ERRLOG("Unable to find subsystem with NQN %s\n", ctx.nqn);
    2023           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters");
    2024           0 :                 goto out;
    2025             :         }
    2026             : 
    2027           0 :         if (ctx.dhchap_key != NULL) {
    2028           0 :                 key = spdk_keyring_get_key(ctx.dhchap_key);
    2029           0 :                 if (key == NULL) {
    2030           0 :                         SPDK_ERRLOG("Unable to find DH-HMAC-CHAP key: %s\n", ctx.dhchap_key);
    2031           0 :                         spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
    2032             :                                                          "Invalid parameters");
    2033           0 :                         goto out;
    2034             :                 }
    2035             :         }
    2036             : 
    2037           0 :         if (ctx.dhchap_ctrlr_key != NULL) {
    2038           0 :                 ckey = spdk_keyring_get_key(ctx.dhchap_ctrlr_key);
    2039           0 :                 if (ckey == NULL) {
    2040           0 :                         SPDK_ERRLOG("Unable to find DH-HMAC-CHAP ctrlr key: %s\n",
    2041             :                                     ctx.dhchap_ctrlr_key);
    2042           0 :                         spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
    2043             :                                                          "Invalid parameters");
    2044           0 :                         goto out;
    2045             :                 }
    2046             :         }
    2047             : 
    2048           0 :         opts.size = SPDK_SIZEOF(&opts, dhchap_ctrlr_key);
    2049           0 :         opts.params = params;
    2050           0 :         opts.dhchap_key = key;
    2051           0 :         opts.dhchap_ctrlr_key = ckey;
    2052           0 :         rc = spdk_nvmf_subsystem_add_host_ext(subsystem, ctx.host, &opts);
    2053           0 :         if (rc != 0) {
    2054           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, "Internal error");
    2055           0 :                 goto out;
    2056             :         }
    2057             : 
    2058           0 :         spdk_jsonrpc_send_bool_response(request, true);
    2059           0 : out:
    2060           0 :         spdk_keyring_put_key(ckey);
    2061           0 :         spdk_keyring_put_key(key);
    2062           0 :         nvmf_rpc_host_ctx_free(&ctx);
    2063           0 : }
    2064           0 : SPDK_RPC_REGISTER("nvmf_subsystem_add_host", rpc_nvmf_subsystem_add_host, SPDK_RPC_RUNTIME)
    2065             : 
    2066             : static void
    2067           0 : rpc_nvmf_subsystem_remove_host_done(void *_ctx, int status)
    2068             : {
    2069           0 :         struct nvmf_rpc_host_ctx *ctx = _ctx;
    2070             : 
    2071           0 :         spdk_jsonrpc_send_bool_response(ctx->request, true);
    2072           0 :         nvmf_rpc_host_ctx_free(ctx);
    2073           0 :         free(ctx);
    2074           0 : }
    2075             : 
    2076             : static void
    2077           0 : rpc_nvmf_subsystem_remove_host(struct spdk_jsonrpc_request *request,
    2078             :                                const struct spdk_json_val *params)
    2079             : {
    2080             :         struct nvmf_rpc_host_ctx *ctx;
    2081             :         struct spdk_nvmf_subsystem *subsystem;
    2082             :         struct spdk_nvmf_tgt *tgt;
    2083             :         int rc;
    2084             : 
    2085           0 :         ctx = calloc(1, sizeof(*ctx));
    2086           0 :         if (ctx == NULL) {
    2087           0 :                 SPDK_ERRLOG("Unable to allocate context to perform RPC\n");
    2088           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, "Out of memory");
    2089           0 :                 return;
    2090             :         }
    2091             : 
    2092           0 :         ctx->request = request;
    2093             : 
    2094           0 :         if (spdk_json_decode_object(params, nvmf_rpc_subsystem_host_decoder,
    2095             :                                     SPDK_COUNTOF(nvmf_rpc_subsystem_host_decoder),
    2096             :                                     ctx)) {
    2097           0 :                 SPDK_ERRLOG("spdk_json_decode_object failed\n");
    2098           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters");
    2099           0 :                 nvmf_rpc_host_ctx_free(ctx);
    2100           0 :                 free(ctx);
    2101           0 :                 return;
    2102             :         }
    2103             : 
    2104           0 :         tgt = spdk_nvmf_get_tgt(ctx->tgt_name);
    2105           0 :         if (!tgt) {
    2106           0 :                 SPDK_ERRLOG("Unable to find a target object.\n");
    2107           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    2108             :                                                  "Unable to find a target.");
    2109           0 :                 nvmf_rpc_host_ctx_free(ctx);
    2110           0 :                 free(ctx);
    2111           0 :                 return;
    2112             :         }
    2113             : 
    2114           0 :         subsystem = spdk_nvmf_tgt_find_subsystem(tgt, ctx->nqn);
    2115           0 :         if (!subsystem) {
    2116           0 :                 SPDK_ERRLOG("Unable to find subsystem with NQN %s\n", ctx->nqn);
    2117           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters");
    2118           0 :                 nvmf_rpc_host_ctx_free(ctx);
    2119           0 :                 free(ctx);
    2120           0 :                 return;
    2121             :         }
    2122             : 
    2123           0 :         rc = spdk_nvmf_subsystem_remove_host(subsystem, ctx->host);
    2124           0 :         if (rc != 0) {
    2125           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, "Internal error");
    2126           0 :                 nvmf_rpc_host_ctx_free(ctx);
    2127           0 :                 free(ctx);
    2128           0 :                 return;
    2129             :         }
    2130             : 
    2131           0 :         rc = spdk_nvmf_subsystem_disconnect_host(subsystem, ctx->host,
    2132             :                         rpc_nvmf_subsystem_remove_host_done,
    2133             :                         ctx);
    2134           0 :         if (rc != 0) {
    2135           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, "Internal error");
    2136           0 :                 nvmf_rpc_host_ctx_free(ctx);
    2137           0 :                 free(ctx);
    2138           0 :                 return;
    2139             :         }
    2140             : }
    2141           0 : SPDK_RPC_REGISTER("nvmf_subsystem_remove_host", rpc_nvmf_subsystem_remove_host,
    2142             :                   SPDK_RPC_RUNTIME)
    2143             : 
    2144             : static void
    2145           0 : rpc_nvmf_discovery_referral_add_host(struct spdk_jsonrpc_request *request,
    2146             :                                      const struct spdk_json_val *params)
    2147             : {
    2148           0 :         struct nvmf_rpc_host_ctx ctx = {};
    2149             :         struct spdk_nvmf_referral *referral;
    2150           0 :         struct spdk_nvmf_host_opts opts = {};
    2151             :         struct spdk_nvmf_tgt *tgt;
    2152           0 :         struct spdk_key *key = NULL, *ckey = NULL;
    2153             :         int rc;
    2154             : 
    2155           0 :         if (spdk_json_decode_object_relaxed(params, nvmf_rpc_referral_host_decoder,
    2156             :                                             SPDK_COUNTOF(nvmf_rpc_referral_host_decoder),
    2157             :                                             &ctx)) {
    2158           0 :                 SPDK_ERRLOG("spdk_json_decode_object failed\n");
    2159           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters");
    2160           0 :                 goto out;
    2161             :         }
    2162             : 
    2163           0 :         tgt = spdk_nvmf_get_tgt(ctx.tgt_name);
    2164           0 :         if (!tgt) {
    2165           0 :                 SPDK_ERRLOG("Unable to find a target object.\n");
    2166           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    2167             :                                                  "Unable to find a target.");
    2168           0 :                 goto out;
    2169             :         }
    2170             : 
    2171           0 :         referral = spdk_nvmf_tgt_find_referral(tgt, ctx.nqn);
    2172           0 :         if (!referral) {
    2173           0 :                 SPDK_ERRLOG("Unable to find referral with NQN %s\n", ctx.nqn);
    2174           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters");
    2175           0 :                 goto out;
    2176             :         }
    2177             : 
    2178           0 :         if (ctx.dhchap_key != NULL) {
    2179           0 :                 key = spdk_keyring_get_key(ctx.dhchap_key);
    2180           0 :                 if (key == NULL) {
    2181           0 :                         SPDK_ERRLOG("Unable to find DH-HMAC-CHAP key: %s\n", ctx.dhchap_key);
    2182           0 :                         spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
    2183             :                                                          "Invalid parameters");
    2184           0 :                         goto out;
    2185             :                 }
    2186             :         }
    2187           0 :         if (ctx.dhchap_ctrlr_key != NULL) {
    2188           0 :                 ckey = spdk_keyring_get_key(ctx.dhchap_ctrlr_key);
    2189           0 :                 if (ckey == NULL) {
    2190           0 :                         SPDK_ERRLOG("Unable to find DH-HMAC-CHAP ctrlr key: %s\n",
    2191             :                                     ctx.dhchap_ctrlr_key);
    2192           0 :                         spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
    2193             :                                                          "Invalid parameters");
    2194           0 :                         goto out;
    2195             :                 }
    2196             :         }
    2197             : 
    2198           0 :         opts.size = SPDK_SIZEOF(&opts, dhchap_ctrlr_key);
    2199           0 :         opts.params = params;
    2200           0 :         opts.dhchap_key = key;
    2201           0 :         opts.dhchap_ctrlr_key = ckey;
    2202           0 :         rc = spdk_nvmf_discovery_referral_add_host_ext(referral, ctx.host, &opts);
    2203           0 :         if (rc != 0) {
    2204           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, "Internal error");
    2205           0 :                 goto out;
    2206             :         }
    2207             : 
    2208           0 :         spdk_jsonrpc_send_bool_response(request, true);
    2209           0 : out:
    2210           0 :         spdk_keyring_put_key(ckey);
    2211           0 :         spdk_keyring_put_key(key);
    2212           0 :         nvmf_rpc_host_ctx_free(&ctx);
    2213           0 : }
    2214           0 : SPDK_RPC_REGISTER("nvmf_discovery_referral_add_host", rpc_nvmf_discovery_referral_add_host,
    2215             :                   SPDK_RPC_RUNTIME)
    2216             : 
    2217             : static void
    2218           0 : rpc_nvmf_discovery_referral_remove_host(struct spdk_jsonrpc_request *request,
    2219             :                                         const struct spdk_json_val *params)
    2220             : {
    2221             :         struct nvmf_rpc_host_ctx *ctx;
    2222             :         struct spdk_nvmf_referral *referral;
    2223             :         struct spdk_nvmf_tgt *tgt;
    2224             :         int rc;
    2225             : 
    2226           0 :         ctx = calloc(1, sizeof(*ctx));
    2227           0 :         if (ctx == NULL) {
    2228           0 :                 SPDK_ERRLOG("Unable to allocate context to perform RPC\n");
    2229           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, "Out of memory");
    2230           0 :                 return;
    2231             :         }
    2232             : 
    2233           0 :         ctx->request = request;
    2234             : 
    2235           0 :         if (spdk_json_decode_object(params, nvmf_rpc_referral_host_decoder,
    2236             :                                     SPDK_COUNTOF(nvmf_rpc_referral_host_decoder),
    2237             :                                     ctx)) {
    2238           0 :                 SPDK_ERRLOG("spdk_json_decode_object failed\n");
    2239           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters");
    2240           0 :                 nvmf_rpc_host_ctx_free(ctx);
    2241           0 :                 free(ctx);
    2242           0 :                 return;
    2243             :         }
    2244             : 
    2245           0 :         tgt = spdk_nvmf_get_tgt(ctx->tgt_name);
    2246           0 :         if (!tgt) {
    2247           0 :                 SPDK_ERRLOG("Unable to find a target object.\n");
    2248           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    2249             :                                                  "Unable to find a target.");
    2250           0 :                 nvmf_rpc_host_ctx_free(ctx);
    2251           0 :                 free(ctx);
    2252           0 :                 return;
    2253             :         }
    2254             : 
    2255           0 :         referral = spdk_nvmf_tgt_find_referral(tgt, ctx->nqn);
    2256           0 :         if (!referral) {
    2257           0 :                 SPDK_ERRLOG("Unable to find referral with NQN %s\n", ctx->nqn);
    2258           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters");
    2259           0 :                 nvmf_rpc_host_ctx_free(ctx);
    2260           0 :                 free(ctx);
    2261           0 :                 return;
    2262             :         }
    2263             : 
    2264           0 :         rc = spdk_nvmf_discovery_referral_remove_host(referral, ctx->host);
    2265           0 :         if (rc != 0) {
    2266           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, "Internal error");
    2267           0 :                 nvmf_rpc_host_ctx_free(ctx);
    2268           0 :                 free(ctx);
    2269           0 :                 return;
    2270             :         }
    2271           0 :         spdk_jsonrpc_send_bool_response(ctx->request, true);
    2272           0 :         nvmf_rpc_host_ctx_free(ctx);
    2273           0 :         free(ctx);
    2274             : 
    2275             : }
    2276           0 : SPDK_RPC_REGISTER("nvmf_discovery_referral_remove_host", rpc_nvmf_discovery_referral_remove_host,
    2277             :                   SPDK_RPC_RUNTIME)
    2278             : 
    2279             : static void
    2280           0 : rpc_nvmf_subsystem_set_keys(struct spdk_jsonrpc_request *request,
    2281             :                             const struct spdk_json_val *params)
    2282             : {
    2283           0 :         struct nvmf_rpc_host_ctx ctx = {};
    2284             :         struct spdk_nvmf_subsystem *subsystem;
    2285           0 :         struct spdk_nvmf_subsystem_key_opts opts = {};
    2286             :         struct spdk_nvmf_tgt *tgt;
    2287           0 :         struct spdk_key *key = NULL, *ckey = NULL;
    2288             :         int rc;
    2289             : 
    2290           0 :         if (spdk_json_decode_object(params, nvmf_rpc_subsystem_host_decoder,
    2291             :                                     SPDK_COUNTOF(nvmf_rpc_subsystem_host_decoder), &ctx)) {
    2292           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
    2293             :                                                  "Invalid parameters");
    2294           0 :                 goto out;
    2295             :         }
    2296             : 
    2297           0 :         tgt = spdk_nvmf_get_tgt(ctx.tgt_name);
    2298           0 :         if (!tgt) {
    2299           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    2300             :                                                  "Invalid parameters");
    2301           0 :                 goto out;
    2302             :         }
    2303           0 :         subsystem = spdk_nvmf_tgt_find_subsystem(tgt, ctx.nqn);
    2304           0 :         if (!subsystem) {
    2305           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
    2306             :                                                  "Invalid parameters");
    2307           0 :                 goto out;
    2308             :         }
    2309             : 
    2310           0 :         if (ctx.dhchap_key != NULL) {
    2311           0 :                 key = spdk_keyring_get_key(ctx.dhchap_key);
    2312           0 :                 if (key == NULL) {
    2313           0 :                         SPDK_ERRLOG("Unable to find DH-HMAC-CHAP key: %s\n", ctx.dhchap_key);
    2314           0 :                         spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
    2315             :                                                          "Invalid parameters");
    2316           0 :                         goto out;
    2317             :                 }
    2318             :         }
    2319           0 :         if (ctx.dhchap_ctrlr_key != NULL) {
    2320           0 :                 ckey = spdk_keyring_get_key(ctx.dhchap_ctrlr_key);
    2321           0 :                 if (ckey == NULL) {
    2322           0 :                         SPDK_ERRLOG("Unable to find DH-HMAC-CHAP ctrlr key: %s\n",
    2323             :                                     ctx.dhchap_ctrlr_key);
    2324           0 :                         spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
    2325             :                                                          "Invalid parameters");
    2326           0 :                         goto out;
    2327             :                 }
    2328             :         }
    2329             : 
    2330           0 :         opts.size = SPDK_SIZEOF(&opts, dhchap_ctrlr_key);
    2331           0 :         opts.dhchap_key = key;
    2332           0 :         opts.dhchap_ctrlr_key = ckey;
    2333           0 :         rc = spdk_nvmf_subsystem_set_keys(subsystem, ctx.host, &opts);
    2334           0 :         if (rc != 0) {
    2335           0 :                 spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
    2336           0 :                 goto out;
    2337             :         }
    2338             : 
    2339           0 :         spdk_jsonrpc_send_bool_response(request, true);
    2340           0 : out:
    2341           0 :         spdk_keyring_put_key(ckey);
    2342           0 :         spdk_keyring_put_key(key);
    2343           0 :         nvmf_rpc_host_ctx_free(&ctx);
    2344           0 : }
    2345           0 : SPDK_RPC_REGISTER("nvmf_subsystem_set_keys", rpc_nvmf_subsystem_set_keys, SPDK_RPC_RUNTIME)
    2346             : 
    2347             : static const struct spdk_json_object_decoder nvmf_rpc_subsystem_any_host_decoder[] = {
    2348             :         {"nqn", offsetof(struct nvmf_rpc_host_ctx, nqn), spdk_json_decode_string},
    2349             :         {"allow_any_host", offsetof(struct nvmf_rpc_host_ctx, allow_any_host), spdk_json_decode_bool},
    2350             :         {"tgt_name", offsetof(struct nvmf_rpc_host_ctx, tgt_name), spdk_json_decode_string, true},
    2351             : };
    2352             : 
    2353             : static void
    2354           0 : rpc_nvmf_subsystem_allow_any_host(struct spdk_jsonrpc_request *request,
    2355             :                                   const struct spdk_json_val *params)
    2356             : {
    2357           0 :         struct nvmf_rpc_host_ctx ctx = {};
    2358             :         struct spdk_nvmf_subsystem *subsystem;
    2359             :         struct spdk_nvmf_tgt *tgt;
    2360             :         int rc;
    2361             : 
    2362           0 :         if (spdk_json_decode_object(params, nvmf_rpc_subsystem_any_host_decoder,
    2363             :                                     SPDK_COUNTOF(nvmf_rpc_subsystem_any_host_decoder),
    2364             :                                     &ctx)) {
    2365           0 :                 SPDK_ERRLOG("spdk_json_decode_object failed\n");
    2366           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters");
    2367           0 :                 nvmf_rpc_host_ctx_free(&ctx);
    2368           0 :                 return;
    2369             :         }
    2370             : 
    2371           0 :         tgt = spdk_nvmf_get_tgt(ctx.tgt_name);
    2372           0 :         if (!tgt) {
    2373           0 :                 SPDK_ERRLOG("Unable to find a target object.\n");
    2374           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    2375             :                                                  "Unable to find a target.");
    2376           0 :                 nvmf_rpc_host_ctx_free(&ctx);
    2377           0 :                 return;
    2378             :         }
    2379             : 
    2380           0 :         subsystem = spdk_nvmf_tgt_find_subsystem(tgt, ctx.nqn);
    2381           0 :         if (!subsystem) {
    2382           0 :                 SPDK_ERRLOG("Unable to find subsystem with NQN %s\n", ctx.nqn);
    2383           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters");
    2384           0 :                 nvmf_rpc_host_ctx_free(&ctx);
    2385           0 :                 return;
    2386             :         }
    2387             : 
    2388           0 :         rc = spdk_nvmf_subsystem_set_allow_any_host(subsystem, ctx.allow_any_host);
    2389           0 :         if (rc != 0) {
    2390           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, "Internal error");
    2391           0 :                 nvmf_rpc_host_ctx_free(&ctx);
    2392           0 :                 return;
    2393             :         }
    2394             : 
    2395           0 :         spdk_jsonrpc_send_bool_response(request, true);
    2396           0 :         nvmf_rpc_host_ctx_free(&ctx);
    2397             : }
    2398           0 : SPDK_RPC_REGISTER("nvmf_subsystem_allow_any_host", rpc_nvmf_subsystem_allow_any_host,
    2399             :                   SPDK_RPC_RUNTIME)
    2400             : 
    2401             : struct nvmf_rpc_target_ctx {
    2402             :         char *name;
    2403             :         uint32_t max_subsystems;
    2404             :         char *discovery_filter;
    2405             : };
    2406             : 
    2407             : static int
    2408           0 : decode_discovery_filter(const struct spdk_json_val *val, void *out)
    2409             : {
    2410           0 :         uint32_t *_filter = out;
    2411           0 :         uint32_t filter = SPDK_NVMF_TGT_DISCOVERY_MATCH_ANY;
    2412           0 :         char *tokens = spdk_json_strdup(val);
    2413             :         char *tok;
    2414           0 :         int rc = -EINVAL;
    2415           0 :         bool all_specified = false;
    2416             : 
    2417           0 :         if (!tokens) {
    2418           0 :                 return -ENOMEM;
    2419             :         }
    2420             : 
    2421           0 :         tok = strtok(tokens, ",");
    2422           0 :         while (tok) {
    2423           0 :                 if (strncmp(tok, "match_any", 9) == 0) {
    2424           0 :                         if (filter != SPDK_NVMF_TGT_DISCOVERY_MATCH_ANY) {
    2425           0 :                                 goto out;
    2426             :                         }
    2427           0 :                         filter = SPDK_NVMF_TGT_DISCOVERY_MATCH_ANY;
    2428           0 :                         all_specified = true;
    2429             :                 } else {
    2430           0 :                         if (all_specified) {
    2431           0 :                                 goto out;
    2432             :                         }
    2433           0 :                         if (strncmp(tok, "transport", 9) == 0) {
    2434           0 :                                 filter |= SPDK_NVMF_TGT_DISCOVERY_MATCH_TRANSPORT_TYPE;
    2435           0 :                         } else if (strncmp(tok, "address", 7) == 0) {
    2436           0 :                                 filter |= SPDK_NVMF_TGT_DISCOVERY_MATCH_TRANSPORT_ADDRESS;
    2437           0 :                         } else if (strncmp(tok, "svcid", 5) == 0) {
    2438           0 :                                 filter |= SPDK_NVMF_TGT_DISCOVERY_MATCH_TRANSPORT_SVCID;
    2439             :                         } else {
    2440           0 :                                 SPDK_ERRLOG("Invalid value %s\n", tok);
    2441           0 :                                 goto out;
    2442             :                         }
    2443             :                 }
    2444             : 
    2445           0 :                 tok = strtok(NULL, ",");
    2446             :         }
    2447             : 
    2448           0 :         rc = 0;
    2449           0 :         *_filter = filter;
    2450             : 
    2451           0 : out:
    2452           0 :         free(tokens);
    2453             : 
    2454           0 :         return rc;
    2455             : }
    2456             : 
    2457             : static const struct spdk_json_object_decoder nvmf_rpc_create_target_decoder[] = {
    2458             :         {"name", offsetof(struct nvmf_rpc_target_ctx, name), spdk_json_decode_string},
    2459             :         {"max_subsystems", offsetof(struct nvmf_rpc_target_ctx, max_subsystems), spdk_json_decode_uint32, true},
    2460             :         {"discovery_filter", offsetof(struct nvmf_rpc_target_ctx, discovery_filter), decode_discovery_filter, true}
    2461             : };
    2462             : 
    2463             : static void
    2464           0 : rpc_nvmf_create_target(struct spdk_jsonrpc_request *request,
    2465             :                        const struct spdk_json_val *params)
    2466             : {
    2467           0 :         struct spdk_nvmf_target_opts    opts;
    2468           0 :         struct nvmf_rpc_target_ctx      ctx = {0};
    2469             :         struct spdk_nvmf_tgt            *tgt;
    2470             :         struct spdk_json_write_ctx      *w;
    2471             : 
    2472             :         /* Decode parameters the first time to get the transport type */
    2473           0 :         if (spdk_json_decode_object(params, nvmf_rpc_create_target_decoder,
    2474             :                                     SPDK_COUNTOF(nvmf_rpc_create_target_decoder),
    2475             :                                     &ctx)) {
    2476           0 :                 SPDK_ERRLOG("spdk_json_decode_object failed\n");
    2477           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters");
    2478           0 :                 goto out;
    2479             :         }
    2480             : 
    2481           0 :         snprintf(opts.name, NVMF_TGT_NAME_MAX_LENGTH, "%s", ctx.name);
    2482           0 :         opts.max_subsystems = ctx.max_subsystems;
    2483           0 :         opts.size = SPDK_SIZEOF(&opts, discovery_filter);
    2484             : 
    2485           0 :         if (spdk_nvmf_get_tgt(opts.name) != NULL) {
    2486           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
    2487             :                                                  "Target already exists.");
    2488           0 :                 goto out;
    2489             :         }
    2490             : 
    2491           0 :         tgt = spdk_nvmf_tgt_create(&opts);
    2492             : 
    2493           0 :         if (tgt == NULL) {
    2494           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    2495             :                                                  "Unable to create the requested target.");
    2496           0 :                 goto out;
    2497             :         }
    2498             : 
    2499           0 :         w = spdk_jsonrpc_begin_result(request);
    2500           0 :         spdk_json_write_string(w, spdk_nvmf_tgt_get_name(tgt));
    2501           0 :         spdk_jsonrpc_end_result(request, w);
    2502           0 : out:
    2503           0 :         free(ctx.name);
    2504           0 :         free(ctx.discovery_filter);
    2505           0 : }
    2506           0 : /* private */ SPDK_RPC_REGISTER("nvmf_create_target", rpc_nvmf_create_target, SPDK_RPC_RUNTIME);
    2507             : 
    2508             : static const struct spdk_json_object_decoder nvmf_rpc_destroy_target_decoder[] = {
    2509             :         {"name", offsetof(struct nvmf_rpc_target_ctx, name), spdk_json_decode_string},
    2510             : };
    2511             : 
    2512             : static void
    2513           0 : nvmf_rpc_destroy_target_done(void *ctx, int status)
    2514             : {
    2515           0 :         struct spdk_jsonrpc_request     *request = ctx;
    2516             : 
    2517           0 :         spdk_jsonrpc_send_bool_response(request, true);
    2518           0 : }
    2519             : 
    2520             : static void
    2521           0 : rpc_nvmf_delete_target(struct spdk_jsonrpc_request *request,
    2522             :                        const struct spdk_json_val *params)
    2523             : {
    2524           0 :         struct nvmf_rpc_target_ctx      ctx = {0};
    2525             :         struct spdk_nvmf_tgt            *tgt;
    2526             : 
    2527             :         /* Decode parameters the first time to get the transport type */
    2528           0 :         if (spdk_json_decode_object(params, nvmf_rpc_destroy_target_decoder,
    2529             :                                     SPDK_COUNTOF(nvmf_rpc_destroy_target_decoder),
    2530             :                                     &ctx)) {
    2531           0 :                 SPDK_ERRLOG("spdk_json_decode_object failed\n");
    2532           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters");
    2533           0 :                 free(ctx.name);
    2534           0 :                 return;
    2535             :         }
    2536             : 
    2537           0 :         tgt = spdk_nvmf_get_tgt(ctx.name);
    2538             : 
    2539           0 :         if (tgt == NULL) {
    2540           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
    2541             :                                                  "The specified target doesn't exist, cannot delete it.");
    2542           0 :                 free(ctx.name);
    2543           0 :                 return;
    2544             :         }
    2545             : 
    2546           0 :         spdk_nvmf_tgt_destroy(tgt, nvmf_rpc_destroy_target_done, request);
    2547           0 :         free(ctx.name);
    2548             : }
    2549           0 : /* private */ SPDK_RPC_REGISTER("nvmf_delete_target", rpc_nvmf_delete_target, SPDK_RPC_RUNTIME);
    2550             : 
    2551             : static void
    2552           0 : rpc_nvmf_get_targets(struct spdk_jsonrpc_request *request,
    2553             :                      const struct spdk_json_val *params)
    2554             : {
    2555             :         struct spdk_json_write_ctx      *w;
    2556             :         struct spdk_nvmf_tgt            *tgt;
    2557             :         const char                      *name;
    2558             : 
    2559           0 :         if (params != NULL) {
    2560           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
    2561             :                                                  "nvmf_get_targets has no parameters.");
    2562           0 :                 return;
    2563             :         }
    2564             : 
    2565           0 :         w = spdk_jsonrpc_begin_result(request);
    2566           0 :         spdk_json_write_array_begin(w);
    2567             : 
    2568           0 :         tgt = spdk_nvmf_get_first_tgt();
    2569             : 
    2570           0 :         while (tgt != NULL) {
    2571           0 :                 name = spdk_nvmf_tgt_get_name(tgt);
    2572           0 :                 spdk_json_write_string(w, name);
    2573           0 :                 tgt = spdk_nvmf_get_next_tgt(tgt);
    2574             :         }
    2575             : 
    2576           0 :         spdk_json_write_array_end(w);
    2577           0 :         spdk_jsonrpc_end_result(request, w);
    2578             : }
    2579           0 : /* private */ SPDK_RPC_REGISTER("nvmf_get_targets", rpc_nvmf_get_targets, SPDK_RPC_RUNTIME);
    2580             : 
    2581             : struct nvmf_rpc_create_transport_ctx {
    2582             :         char                            *trtype;
    2583             :         char                            *tgt_name;
    2584             :         struct spdk_nvmf_transport_opts opts;
    2585             :         struct spdk_jsonrpc_request     *request;
    2586             :         struct spdk_nvmf_transport      *transport;
    2587             :         int                             status;
    2588             : };
    2589             : 
    2590             : /**
    2591             :  * `max_qpairs_per_ctrlr` represents both admin and IO qpairs, that confuses
    2592             :  * users when they configure a transport using RPC. So it was decided to
    2593             :  * deprecate `max_qpairs_per_ctrlr` RPC parameter and use `max_io_qpairs_per_ctrlr`
    2594             :  * But internal logic remains unchanged and SPDK expects that
    2595             :  * spdk_nvmf_transport_opts::max_qpairs_per_ctrlr includes an admin qpair.
    2596             :  * This function parses the number of IO qpairs and adds +1 for admin qpair.
    2597             :  */
    2598             : static int
    2599           0 : nvmf_rpc_decode_max_io_qpairs(const struct spdk_json_val *val, void *out)
    2600             : {
    2601           0 :         uint16_t *i = out;
    2602             :         int rc;
    2603             : 
    2604           0 :         rc = spdk_json_number_to_uint16(val, i);
    2605           0 :         if (rc == 0) {
    2606           0 :                 (*i)++;
    2607             :         }
    2608             : 
    2609           0 :         return rc;
    2610             : }
    2611             : 
    2612             : static const struct spdk_json_object_decoder nvmf_rpc_create_transport_decoder[] = {
    2613             :         {       "trtype", offsetof(struct nvmf_rpc_create_transport_ctx, trtype), spdk_json_decode_string},
    2614             :         {
    2615             :                 "max_queue_depth", offsetof(struct nvmf_rpc_create_transport_ctx, opts.max_queue_depth),
    2616             :                 spdk_json_decode_uint16, true
    2617             :         },
    2618             :         {
    2619             :                 "max_io_qpairs_per_ctrlr", offsetof(struct nvmf_rpc_create_transport_ctx, opts.max_qpairs_per_ctrlr),
    2620             :                 nvmf_rpc_decode_max_io_qpairs, true
    2621             :         },
    2622             :         {
    2623             :                 "in_capsule_data_size", offsetof(struct nvmf_rpc_create_transport_ctx, opts.in_capsule_data_size),
    2624             :                 spdk_json_decode_uint32, true
    2625             :         },
    2626             :         {
    2627             :                 "max_io_size", offsetof(struct nvmf_rpc_create_transport_ctx, opts.max_io_size),
    2628             :                 spdk_json_decode_uint32, true
    2629             :         },
    2630             :         {
    2631             :                 "io_unit_size", offsetof(struct nvmf_rpc_create_transport_ctx, opts.io_unit_size),
    2632             :                 spdk_json_decode_uint32, true
    2633             :         },
    2634             :         {
    2635             :                 "max_aq_depth", offsetof(struct nvmf_rpc_create_transport_ctx, opts.max_aq_depth),
    2636             :                 spdk_json_decode_uint32, true
    2637             :         },
    2638             :         {
    2639             :                 "num_shared_buffers", offsetof(struct nvmf_rpc_create_transport_ctx, opts.num_shared_buffers),
    2640             :                 spdk_json_decode_uint32, true
    2641             :         },
    2642             :         {
    2643             :                 "buf_cache_size", offsetof(struct nvmf_rpc_create_transport_ctx, opts.buf_cache_size),
    2644             :                 spdk_json_decode_uint32, true
    2645             :         },
    2646             :         {
    2647             :                 "dif_insert_or_strip", offsetof(struct nvmf_rpc_create_transport_ctx, opts.dif_insert_or_strip),
    2648             :                 spdk_json_decode_bool, true
    2649             :         },
    2650             :         {
    2651             :                 "abort_timeout_sec", offsetof(struct nvmf_rpc_create_transport_ctx, opts.abort_timeout_sec),
    2652             :                 spdk_json_decode_uint32, true
    2653             :         },
    2654             :         {
    2655             :                 "zcopy", offsetof(struct nvmf_rpc_create_transport_ctx, opts.zcopy),
    2656             :                 spdk_json_decode_bool, true
    2657             :         },
    2658             :         {
    2659             :                 "tgt_name", offsetof(struct nvmf_rpc_create_transport_ctx, tgt_name),
    2660             :                 spdk_json_decode_string, true
    2661             :         },
    2662             :         {
    2663             :                 "acceptor_poll_rate", offsetof(struct nvmf_rpc_create_transport_ctx, opts.acceptor_poll_rate),
    2664             :                 spdk_json_decode_uint32, true
    2665             :         },
    2666             :         {
    2667             :                 "ack_timeout", offsetof(struct nvmf_rpc_create_transport_ctx, opts.ack_timeout),
    2668             :                 spdk_json_decode_uint32, true
    2669             :         },
    2670             :         {
    2671             :                 "data_wr_pool_size", offsetof(struct nvmf_rpc_create_transport_ctx, opts.data_wr_pool_size),
    2672             :                 spdk_json_decode_uint32, true
    2673             :         },
    2674             :         {
    2675             :                 "disable_command_passthru", offsetof(struct nvmf_rpc_create_transport_ctx, opts.disable_command_passthru),
    2676             :                 spdk_json_decode_bool, true
    2677             :         }
    2678             : };
    2679             : 
    2680             : static void
    2681           0 : nvmf_rpc_create_transport_ctx_free(struct nvmf_rpc_create_transport_ctx *ctx)
    2682             : {
    2683           0 :         free(ctx->trtype);
    2684           0 :         free(ctx->tgt_name);
    2685           0 :         free(ctx);
    2686           0 : }
    2687             : 
    2688             : static void
    2689           0 : nvmf_rpc_transport_destroy_done_cb(void *cb_arg)
    2690             : {
    2691           0 :         struct nvmf_rpc_create_transport_ctx *ctx = cb_arg;
    2692             : 
    2693           0 :         spdk_jsonrpc_send_error_response_fmt(ctx->request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    2694             :                                              "Failed to add transport to tgt.(%d)", ctx->status);
    2695           0 :         nvmf_rpc_create_transport_ctx_free(ctx);
    2696           0 : }
    2697             : 
    2698             : static void
    2699           0 : nvmf_rpc_tgt_add_transport_done(void *cb_arg, int status)
    2700             : {
    2701           0 :         struct nvmf_rpc_create_transport_ctx *ctx = cb_arg;
    2702             : 
    2703           0 :         if (status) {
    2704           0 :                 SPDK_ERRLOG("Failed to add transport to tgt.(%d)\n", status);
    2705           0 :                 ctx->status = status;
    2706           0 :                 spdk_nvmf_transport_destroy(ctx->transport, nvmf_rpc_transport_destroy_done_cb, ctx);
    2707           0 :                 return;
    2708             :         }
    2709             : 
    2710           0 :         spdk_jsonrpc_send_bool_response(ctx->request, true);
    2711           0 :         nvmf_rpc_create_transport_ctx_free(ctx);
    2712             : }
    2713             : 
    2714             : static void
    2715           0 : nvmf_rpc_create_transport_done(void *cb_arg, struct spdk_nvmf_transport *transport)
    2716             : {
    2717           0 :         struct nvmf_rpc_create_transport_ctx *ctx = cb_arg;
    2718             : 
    2719           0 :         if (!transport) {
    2720           0 :                 SPDK_ERRLOG("Failed to create transport.\n");
    2721           0 :                 spdk_jsonrpc_send_error_response(ctx->request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    2722             :                                                  "Failed to create transport.");
    2723           0 :                 nvmf_rpc_create_transport_ctx_free(ctx);
    2724           0 :                 return;
    2725             :         }
    2726             : 
    2727           0 :         ctx->transport = transport;
    2728             : 
    2729           0 :         spdk_nvmf_tgt_add_transport(spdk_nvmf_get_tgt(ctx->tgt_name), transport,
    2730             :                                     nvmf_rpc_tgt_add_transport_done, ctx);
    2731             : }
    2732             : 
    2733             : static void
    2734           0 : rpc_nvmf_create_transport(struct spdk_jsonrpc_request *request,
    2735             :                           const struct spdk_json_val *params)
    2736             : {
    2737             :         struct nvmf_rpc_create_transport_ctx *ctx;
    2738             :         struct spdk_nvmf_tgt *tgt;
    2739             :         int rc;
    2740             : 
    2741           0 :         ctx = calloc(1, sizeof(*ctx));
    2742           0 :         if (!ctx) {
    2743           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, "Out of memory");
    2744           0 :                 return;
    2745             :         }
    2746             : 
    2747             :         /* Decode parameters the first time to get the transport type */
    2748           0 :         if (spdk_json_decode_object_relaxed(params, nvmf_rpc_create_transport_decoder,
    2749             :                                             SPDK_COUNTOF(nvmf_rpc_create_transport_decoder),
    2750             :                                             ctx)) {
    2751           0 :                 SPDK_ERRLOG("spdk_json_decode_object_relaxed failed\n");
    2752           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters");
    2753           0 :                 nvmf_rpc_create_transport_ctx_free(ctx);
    2754           0 :                 return;
    2755             :         }
    2756             : 
    2757           0 :         tgt = spdk_nvmf_get_tgt(ctx->tgt_name);
    2758           0 :         if (!tgt) {
    2759           0 :                 SPDK_ERRLOG("Unable to find a target object.\n");
    2760           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    2761             :                                                  "Unable to find a target.");
    2762           0 :                 nvmf_rpc_create_transport_ctx_free(ctx);
    2763           0 :                 return;
    2764             :         }
    2765             : 
    2766             :         /* Initialize all the transport options (based on transport type) and decode the
    2767             :          * parameters again to update any options passed in rpc create transport call.
    2768             :          */
    2769           0 :         if (!spdk_nvmf_transport_opts_init(ctx->trtype, &ctx->opts, sizeof(ctx->opts))) {
    2770             :                 /* This can happen if user specifies PCIE transport type which isn't valid for
    2771             :                  * NVMe-oF.
    2772             :                  */
    2773           0 :                 SPDK_ERRLOG("Invalid transport type '%s'\n", ctx->trtype);
    2774           0 :                 spdk_jsonrpc_send_error_response_fmt(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
    2775             :                                                      "Invalid transport type '%s'", ctx->trtype);
    2776           0 :                 nvmf_rpc_create_transport_ctx_free(ctx);
    2777           0 :                 return;
    2778             :         }
    2779             : 
    2780           0 :         if (spdk_json_decode_object_relaxed(params, nvmf_rpc_create_transport_decoder,
    2781             :                                             SPDK_COUNTOF(nvmf_rpc_create_transport_decoder),
    2782             :                                             ctx)) {
    2783           0 :                 SPDK_ERRLOG("spdk_json_decode_object_relaxed failed\n");
    2784           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters");
    2785           0 :                 nvmf_rpc_create_transport_ctx_free(ctx);
    2786           0 :                 return;
    2787             :         }
    2788             : 
    2789           0 :         if (spdk_nvmf_tgt_get_transport(tgt, ctx->trtype)) {
    2790           0 :                 SPDK_ERRLOG("Transport type '%s' already exists\n", ctx->trtype);
    2791           0 :                 spdk_jsonrpc_send_error_response_fmt(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    2792             :                                                      "Transport type '%s' already exists", ctx->trtype);
    2793           0 :                 nvmf_rpc_create_transport_ctx_free(ctx);
    2794           0 :                 return;
    2795             :         }
    2796             : 
    2797             :         /* Transport can parse additional params themselves */
    2798           0 :         ctx->opts.transport_specific = params;
    2799           0 :         ctx->request = request;
    2800             : 
    2801           0 :         rc = spdk_nvmf_transport_create_async(ctx->trtype, &ctx->opts, nvmf_rpc_create_transport_done, ctx);
    2802           0 :         if (rc) {
    2803           0 :                 SPDK_ERRLOG("Transport type '%s' create failed\n", ctx->trtype);
    2804           0 :                 spdk_jsonrpc_send_error_response_fmt(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    2805             :                                                      "Transport type '%s' create failed", ctx->trtype);
    2806           0 :                 nvmf_rpc_create_transport_ctx_free(ctx);
    2807             :         }
    2808             : }
    2809           0 : SPDK_RPC_REGISTER("nvmf_create_transport", rpc_nvmf_create_transport, SPDK_RPC_RUNTIME)
    2810             : 
    2811             : struct rpc_get_transport {
    2812             :         char *trtype;
    2813             :         char *tgt_name;
    2814             : };
    2815             : 
    2816             : static const struct spdk_json_object_decoder rpc_get_transport_decoders[] = {
    2817             :         {"trtype", offsetof(struct rpc_get_transport, trtype), spdk_json_decode_string, true},
    2818             :         {"tgt_name", offsetof(struct rpc_get_transport, tgt_name), spdk_json_decode_string, true},
    2819             : };
    2820             : 
    2821             : static void
    2822           0 : rpc_nvmf_get_transports(struct spdk_jsonrpc_request *request,
    2823             :                         const struct spdk_json_val *params)
    2824             : {
    2825           0 :         struct rpc_get_transport req = { 0 };
    2826             :         struct spdk_json_write_ctx *w;
    2827           0 :         struct spdk_nvmf_transport *transport = NULL;
    2828             :         struct spdk_nvmf_tgt *tgt;
    2829             : 
    2830           0 :         if (params) {
    2831           0 :                 if (spdk_json_decode_object(params, rpc_get_transport_decoders,
    2832             :                                             SPDK_COUNTOF(rpc_get_transport_decoders),
    2833             :                                             &req)) {
    2834           0 :                         SPDK_ERRLOG("spdk_json_decode_object failed\n");
    2835           0 :                         spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters");
    2836           0 :                         return;
    2837             :                 }
    2838             :         }
    2839             : 
    2840           0 :         tgt = spdk_nvmf_get_tgt(req.tgt_name);
    2841           0 :         if (!tgt) {
    2842           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    2843             :                                                  "Unable to find a target.");
    2844           0 :                 free(req.trtype);
    2845           0 :                 free(req.tgt_name);
    2846           0 :                 return;
    2847             :         }
    2848             : 
    2849           0 :         if (req.trtype) {
    2850           0 :                 transport = spdk_nvmf_tgt_get_transport(tgt, req.trtype);
    2851           0 :                 if (transport == NULL) {
    2852           0 :                         SPDK_ERRLOG("transport '%s' does not exist\n", req.trtype);
    2853           0 :                         spdk_jsonrpc_send_error_response(request, -ENODEV, spdk_strerror(ENODEV));
    2854           0 :                         free(req.trtype);
    2855           0 :                         free(req.tgt_name);
    2856           0 :                         return;
    2857             :                 }
    2858             :         }
    2859             : 
    2860           0 :         w = spdk_jsonrpc_begin_result(request);
    2861           0 :         spdk_json_write_array_begin(w);
    2862             : 
    2863           0 :         if (transport) {
    2864           0 :                 nvmf_transport_dump_opts(transport, w, false);
    2865             :         } else {
    2866           0 :                 for (transport = spdk_nvmf_transport_get_first(tgt); transport != NULL;
    2867           0 :                      transport = spdk_nvmf_transport_get_next(transport)) {
    2868           0 :                         nvmf_transport_dump_opts(transport, w, false);
    2869             :                 }
    2870             :         }
    2871             : 
    2872           0 :         spdk_json_write_array_end(w);
    2873           0 :         spdk_jsonrpc_end_result(request, w);
    2874           0 :         free(req.trtype);
    2875           0 :         free(req.tgt_name);
    2876             : }
    2877           0 : SPDK_RPC_REGISTER("nvmf_get_transports", rpc_nvmf_get_transports, SPDK_RPC_RUNTIME)
    2878             : 
    2879             : struct rpc_nvmf_get_stats_ctx {
    2880             :         char *tgt_name;
    2881             :         struct spdk_nvmf_tgt *tgt;
    2882             :         struct spdk_jsonrpc_request *request;
    2883             :         struct spdk_json_write_ctx *w;
    2884             : };
    2885             : 
    2886             : static const struct spdk_json_object_decoder rpc_get_stats_decoders[] = {
    2887             :         {"tgt_name", offsetof(struct rpc_nvmf_get_stats_ctx, tgt_name), spdk_json_decode_string, true},
    2888             : };
    2889             : 
    2890             : static void
    2891           0 : free_get_stats_ctx(struct rpc_nvmf_get_stats_ctx *ctx)
    2892             : {
    2893           0 :         free(ctx->tgt_name);
    2894           0 :         free(ctx);
    2895           0 : }
    2896             : 
    2897             : static void
    2898           0 : rpc_nvmf_get_stats_done(struct spdk_io_channel_iter *i, int status)
    2899             : {
    2900           0 :         struct rpc_nvmf_get_stats_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
    2901             : 
    2902           0 :         spdk_json_write_array_end(ctx->w);
    2903           0 :         spdk_json_write_object_end(ctx->w);
    2904           0 :         spdk_jsonrpc_end_result(ctx->request, ctx->w);
    2905           0 :         free_get_stats_ctx(ctx);
    2906           0 : }
    2907             : 
    2908             : static void
    2909           0 : _rpc_nvmf_get_stats(struct spdk_io_channel_iter *i)
    2910             : {
    2911           0 :         struct rpc_nvmf_get_stats_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
    2912             :         struct spdk_io_channel *ch;
    2913             :         struct spdk_nvmf_poll_group *group;
    2914             : 
    2915           0 :         ch = spdk_get_io_channel(ctx->tgt);
    2916           0 :         group = spdk_io_channel_get_ctx(ch);
    2917             : 
    2918           0 :         spdk_nvmf_poll_group_dump_stat(group, ctx->w);
    2919             : 
    2920           0 :         spdk_put_io_channel(ch);
    2921           0 :         spdk_for_each_channel_continue(i, 0);
    2922           0 : }
    2923             : 
    2924             : 
    2925             : static void
    2926           0 : rpc_nvmf_get_stats(struct spdk_jsonrpc_request *request,
    2927             :                    const struct spdk_json_val *params)
    2928             : {
    2929             :         struct rpc_nvmf_get_stats_ctx *ctx;
    2930             : 
    2931           0 :         ctx = calloc(1, sizeof(*ctx));
    2932           0 :         if (!ctx) {
    2933           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    2934             :                                                  "Memory allocation error");
    2935           0 :                 return;
    2936             :         }
    2937           0 :         ctx->request = request;
    2938             : 
    2939           0 :         if (params) {
    2940           0 :                 if (spdk_json_decode_object(params, rpc_get_stats_decoders,
    2941             :                                             SPDK_COUNTOF(rpc_get_stats_decoders),
    2942             :                                             ctx)) {
    2943           0 :                         SPDK_ERRLOG("spdk_json_decode_object failed\n");
    2944           0 :                         spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters");
    2945           0 :                         free_get_stats_ctx(ctx);
    2946           0 :                         return;
    2947             :                 }
    2948             :         }
    2949             : 
    2950           0 :         ctx->tgt = spdk_nvmf_get_tgt(ctx->tgt_name);
    2951           0 :         if (!ctx->tgt) {
    2952           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    2953             :                                                  "Unable to find a target.");
    2954           0 :                 free_get_stats_ctx(ctx);
    2955           0 :                 return;
    2956             :         }
    2957             : 
    2958           0 :         ctx->w = spdk_jsonrpc_begin_result(ctx->request);
    2959           0 :         spdk_json_write_object_begin(ctx->w);
    2960           0 :         spdk_json_write_named_uint64(ctx->w, "tick_rate", spdk_get_ticks_hz());
    2961           0 :         spdk_json_write_named_array_begin(ctx->w, "poll_groups");
    2962             : 
    2963           0 :         spdk_for_each_channel(ctx->tgt,
    2964             :                               _rpc_nvmf_get_stats,
    2965             :                               ctx,
    2966             :                               rpc_nvmf_get_stats_done);
    2967             : }
    2968             : 
    2969           0 : SPDK_RPC_REGISTER("nvmf_get_stats", rpc_nvmf_get_stats, SPDK_RPC_RUNTIME)
    2970             : 
    2971             : static void
    2972           0 : dump_nvmf_ctrlr(struct spdk_json_write_ctx *w, struct spdk_nvmf_ctrlr *ctrlr)
    2973             : {
    2974             :         uint32_t count;
    2975             : 
    2976           0 :         spdk_json_write_object_begin(w);
    2977             : 
    2978           0 :         spdk_json_write_named_uint32(w, "cntlid", ctrlr->cntlid);
    2979           0 :         spdk_json_write_named_string(w, "hostnqn", ctrlr->hostnqn);
    2980           0 :         spdk_json_write_named_uuid(w, "hostid", &ctrlr->hostid);
    2981             : 
    2982           0 :         count = spdk_bit_array_count_set(ctrlr->qpair_mask);
    2983           0 :         spdk_json_write_named_uint32(w, "num_io_qpairs", count);
    2984             : 
    2985           0 :         spdk_json_write_object_end(w);
    2986           0 : }
    2987             : 
    2988             : static const char *
    2989           0 : nvmf_qpair_state_str(enum spdk_nvmf_qpair_state state)
    2990             : {
    2991           0 :         switch (state) {
    2992           0 :         case SPDK_NVMF_QPAIR_UNINITIALIZED:
    2993           0 :                 return "uninitialized";
    2994           0 :         case SPDK_NVMF_QPAIR_CONNECTING:
    2995           0 :                 return "connecting";
    2996           0 :         case SPDK_NVMF_QPAIR_ENABLED:
    2997           0 :                 return "enabled";
    2998           0 :         case SPDK_NVMF_QPAIR_DEACTIVATING:
    2999           0 :                 return "deactivating";
    3000           0 :         case SPDK_NVMF_QPAIR_ERROR:
    3001           0 :                 return "error";
    3002           0 :         default:
    3003           0 :                 return NULL;
    3004             :         }
    3005             : }
    3006             : 
    3007             : static void
    3008           0 : dump_nvmf_qpair(struct spdk_json_write_ctx *w, struct spdk_nvmf_qpair *qpair)
    3009             : {
    3010           0 :         struct spdk_nvme_transport_id trid = {};
    3011             : 
    3012           0 :         spdk_json_write_object_begin(w);
    3013             : 
    3014           0 :         spdk_json_write_named_uint32(w, "cntlid", qpair->ctrlr->cntlid);
    3015           0 :         spdk_json_write_named_uint32(w, "qid", qpair->qid);
    3016           0 :         spdk_json_write_named_string(w, "state", nvmf_qpair_state_str(qpair->state));
    3017           0 :         spdk_json_write_named_string(w, "thread", spdk_thread_get_name(spdk_get_thread()));
    3018           0 :         spdk_json_write_named_string(w, "hostnqn", qpair->ctrlr->hostnqn);
    3019             : 
    3020           0 :         if (spdk_nvmf_qpair_get_listen_trid(qpair, &trid) == 0) {
    3021           0 :                 spdk_json_write_named_object_begin(w, "listen_address");
    3022           0 :                 nvmf_transport_listen_dump_trid(&trid, w);
    3023           0 :                 spdk_json_write_object_end(w);
    3024           0 :                 if (qpair->transport->ops->listen_dump_opts) {
    3025           0 :                         qpair->transport->ops->listen_dump_opts(qpair->transport, &trid, w);
    3026             :                 }
    3027             :         }
    3028             : 
    3029           0 :         memset(&trid, 0, sizeof(trid));
    3030           0 :         if (spdk_nvmf_qpair_get_peer_trid(qpair, &trid) == 0) {
    3031           0 :                 spdk_json_write_named_object_begin(w, "peer_address");
    3032           0 :                 nvmf_transport_listen_dump_trid(&trid, w);
    3033           0 :                 spdk_json_write_object_end(w);
    3034             :         }
    3035             : 
    3036           0 :         nvmf_qpair_auth_dump(qpair, w);
    3037           0 :         spdk_json_write_object_end(w);
    3038           0 : }
    3039             : 
    3040             : static const char *
    3041           0 : nvme_ana_state_str(enum spdk_nvme_ana_state ana_state)
    3042             : {
    3043           0 :         switch (ana_state) {
    3044           0 :         case SPDK_NVME_ANA_OPTIMIZED_STATE:
    3045           0 :                 return "optimized";
    3046           0 :         case SPDK_NVME_ANA_NON_OPTIMIZED_STATE:
    3047           0 :                 return "non_optimized";
    3048           0 :         case SPDK_NVME_ANA_INACCESSIBLE_STATE:
    3049           0 :                 return "inaccessible";
    3050           0 :         case SPDK_NVME_ANA_PERSISTENT_LOSS_STATE:
    3051           0 :                 return "persistent_loss";
    3052           0 :         case SPDK_NVME_ANA_CHANGE_STATE:
    3053           0 :                 return "change";
    3054           0 :         default:
    3055           0 :                 return NULL;
    3056             :         }
    3057             : }
    3058             : 
    3059             : static void
    3060           0 : dump_nvmf_subsystem_listener(struct spdk_json_write_ctx *w,
    3061             :                              struct spdk_nvmf_subsystem_listener *listener)
    3062             : {
    3063           0 :         const struct spdk_nvme_transport_id *trid = listener->trid;
    3064             :         uint32_t i;
    3065             : 
    3066           0 :         spdk_json_write_object_begin(w);
    3067             : 
    3068           0 :         spdk_json_write_named_object_begin(w, "address");
    3069           0 :         nvmf_transport_listen_dump_trid(trid, w);
    3070           0 :         spdk_json_write_object_end(w);
    3071             : 
    3072           0 :         if (spdk_nvmf_subsystem_get_ana_reporting(listener->subsystem)) {
    3073           0 :                 spdk_json_write_named_array_begin(w, "ana_states");
    3074           0 :                 for (i = 0; i < listener->subsystem->max_nsid; i++) {
    3075           0 :                         spdk_json_write_object_begin(w);
    3076           0 :                         spdk_json_write_named_uint32(w, "ana_group", i + 1);
    3077           0 :                         spdk_json_write_named_string(w, "ana_state",
    3078           0 :                                                      nvme_ana_state_str(listener->ana_state[i]));
    3079           0 :                         spdk_json_write_object_end(w);
    3080             :                 }
    3081           0 :                 spdk_json_write_array_end(w);
    3082             :         }
    3083             : 
    3084           0 :         spdk_json_write_object_end(w);
    3085           0 : }
    3086             : 
    3087             : struct rpc_subsystem_query_ctx {
    3088             :         char *nqn;
    3089             :         char *tgt_name;
    3090             :         struct spdk_nvmf_subsystem *subsystem;
    3091             :         struct spdk_jsonrpc_request *request;
    3092             :         struct spdk_json_write_ctx *w;
    3093             : };
    3094             : 
    3095             : static const struct spdk_json_object_decoder rpc_subsystem_query_decoders[] = {
    3096             :         {"nqn", offsetof(struct rpc_subsystem_query_ctx, nqn), spdk_json_decode_string},
    3097             :         {"tgt_name", offsetof(struct rpc_subsystem_query_ctx, tgt_name), spdk_json_decode_string, true},
    3098             : };
    3099             : 
    3100             : static void
    3101           0 : free_rpc_subsystem_query_ctx(struct rpc_subsystem_query_ctx *ctx)
    3102             : {
    3103           0 :         free(ctx->nqn);
    3104           0 :         free(ctx->tgt_name);
    3105           0 :         free(ctx);
    3106           0 : }
    3107             : 
    3108             : static void
    3109           0 : rpc_nvmf_get_controllers_paused(struct spdk_nvmf_subsystem *subsystem,
    3110             :                                 void *cb_arg, int status)
    3111             : {
    3112           0 :         struct rpc_subsystem_query_ctx *ctx = cb_arg;
    3113             :         struct spdk_json_write_ctx *w;
    3114             :         struct spdk_nvmf_ctrlr *ctrlr;
    3115             : 
    3116           0 :         w = spdk_jsonrpc_begin_result(ctx->request);
    3117             : 
    3118           0 :         spdk_json_write_array_begin(w);
    3119           0 :         TAILQ_FOREACH(ctrlr, &ctx->subsystem->ctrlrs, link) {
    3120           0 :                 dump_nvmf_ctrlr(w, ctrlr);
    3121             :         }
    3122           0 :         spdk_json_write_array_end(w);
    3123             : 
    3124           0 :         spdk_jsonrpc_end_result(ctx->request, w);
    3125             : 
    3126           0 :         if (spdk_nvmf_subsystem_resume(ctx->subsystem, NULL, NULL)) {
    3127           0 :                 SPDK_ERRLOG("Resuming subsystem with NQN %s failed\n", ctx->nqn);
    3128             :                 /* FIXME: RPC should fail if resuming the subsystem failed. */
    3129             :         }
    3130             : 
    3131           0 :         free_rpc_subsystem_query_ctx(ctx);
    3132           0 : }
    3133             : 
    3134             : static void
    3135           0 : rpc_nvmf_get_qpairs_done(struct spdk_io_channel_iter *i, int status)
    3136             : {
    3137           0 :         struct rpc_subsystem_query_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
    3138             : 
    3139           0 :         spdk_json_write_array_end(ctx->w);
    3140           0 :         spdk_jsonrpc_end_result(ctx->request, ctx->w);
    3141             : 
    3142           0 :         if (spdk_nvmf_subsystem_resume(ctx->subsystem, NULL, NULL)) {
    3143           0 :                 SPDK_ERRLOG("Resuming subsystem with NQN %s failed\n", ctx->nqn);
    3144             :                 /* FIXME: RPC should fail if resuming the subsystem failed. */
    3145             :         }
    3146             : 
    3147           0 :         free_rpc_subsystem_query_ctx(ctx);
    3148           0 : }
    3149             : 
    3150             : static void
    3151           0 : rpc_nvmf_get_qpairs(struct spdk_io_channel_iter *i)
    3152             : {
    3153           0 :         struct rpc_subsystem_query_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
    3154             :         struct spdk_io_channel *ch;
    3155             :         struct spdk_nvmf_poll_group *group;
    3156             :         struct spdk_nvmf_qpair *qpair;
    3157             : 
    3158           0 :         ch = spdk_io_channel_iter_get_channel(i);
    3159           0 :         group = spdk_io_channel_get_ctx(ch);
    3160             : 
    3161           0 :         TAILQ_FOREACH(qpair, &group->qpairs, link) {
    3162           0 :                 if (qpair->ctrlr->subsys == ctx->subsystem) {
    3163           0 :                         dump_nvmf_qpair(ctx->w, qpair);
    3164             :                 }
    3165             :         }
    3166             : 
    3167           0 :         spdk_for_each_channel_continue(i, 0);
    3168           0 : }
    3169             : 
    3170             : static void
    3171           0 : rpc_nvmf_get_qpairs_paused(struct spdk_nvmf_subsystem *subsystem,
    3172             :                            void *cb_arg, int status)
    3173             : {
    3174           0 :         struct rpc_subsystem_query_ctx *ctx = cb_arg;
    3175             : 
    3176           0 :         ctx->w = spdk_jsonrpc_begin_result(ctx->request);
    3177             : 
    3178           0 :         spdk_json_write_array_begin(ctx->w);
    3179             : 
    3180           0 :         spdk_for_each_channel(ctx->subsystem->tgt,
    3181             :                               rpc_nvmf_get_qpairs,
    3182             :                               ctx,
    3183             :                               rpc_nvmf_get_qpairs_done);
    3184           0 : }
    3185             : 
    3186             : static void
    3187           0 : rpc_nvmf_get_listeners_paused(struct spdk_nvmf_subsystem *subsystem,
    3188             :                               void *cb_arg, int status)
    3189             : {
    3190           0 :         struct rpc_subsystem_query_ctx *ctx = cb_arg;
    3191             :         struct spdk_json_write_ctx *w;
    3192             :         struct spdk_nvmf_subsystem_listener *listener;
    3193             : 
    3194           0 :         w = spdk_jsonrpc_begin_result(ctx->request);
    3195             : 
    3196           0 :         spdk_json_write_array_begin(w);
    3197             : 
    3198           0 :         for (listener = spdk_nvmf_subsystem_get_first_listener(ctx->subsystem);
    3199           0 :              listener != NULL;
    3200           0 :              listener = spdk_nvmf_subsystem_get_next_listener(ctx->subsystem, listener)) {
    3201           0 :                 dump_nvmf_subsystem_listener(w, listener);
    3202             :         }
    3203           0 :         spdk_json_write_array_end(w);
    3204             : 
    3205           0 :         spdk_jsonrpc_end_result(ctx->request, w);
    3206             : 
    3207           0 :         if (spdk_nvmf_subsystem_resume(ctx->subsystem, NULL, NULL)) {
    3208           0 :                 SPDK_ERRLOG("Resuming subsystem with NQN %s failed\n", ctx->nqn);
    3209             :                 /* FIXME: RPC should fail if resuming the subsystem failed. */
    3210             :         }
    3211             : 
    3212           0 :         free_rpc_subsystem_query_ctx(ctx);
    3213           0 : }
    3214             : 
    3215             : static void
    3216           0 : _rpc_nvmf_subsystem_query(struct spdk_jsonrpc_request *request,
    3217             :                           const struct spdk_json_val *params,
    3218             :                           spdk_nvmf_subsystem_state_change_done cb_fn)
    3219             : {
    3220             :         struct rpc_subsystem_query_ctx *ctx;
    3221             :         struct spdk_nvmf_subsystem *subsystem;
    3222             :         struct spdk_nvmf_tgt *tgt;
    3223             : 
    3224           0 :         ctx = calloc(1, sizeof(*ctx));
    3225           0 :         if (!ctx) {
    3226           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    3227             :                                                  "Out of memory");
    3228           0 :                 return;
    3229             :         }
    3230             : 
    3231           0 :         ctx->request = request;
    3232             : 
    3233           0 :         if (spdk_json_decode_object(params, rpc_subsystem_query_decoders,
    3234             :                                     SPDK_COUNTOF(rpc_subsystem_query_decoders),
    3235             :                                     ctx)) {
    3236           0 :                 SPDK_ERRLOG("spdk_json_decode_object failed\n");
    3237           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
    3238             :                                                  "Invalid parameters");
    3239           0 :                 free_rpc_subsystem_query_ctx(ctx);
    3240           0 :                 return;
    3241             :         }
    3242             : 
    3243           0 :         tgt = spdk_nvmf_get_tgt(ctx->tgt_name);
    3244           0 :         if (!tgt) {
    3245           0 :                 SPDK_ERRLOG("Unable to find a target object.\n");
    3246           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    3247             :                                                  "Unable to find a target");
    3248           0 :                 free_rpc_subsystem_query_ctx(ctx);
    3249           0 :                 return;
    3250             :         }
    3251             : 
    3252           0 :         subsystem = spdk_nvmf_tgt_find_subsystem(tgt, ctx->nqn);
    3253           0 :         if (!subsystem) {
    3254           0 :                 SPDK_ERRLOG("Unable to find subsystem with NQN %s\n", ctx->nqn);
    3255           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
    3256             :                                                  "Invalid parameters");
    3257           0 :                 free_rpc_subsystem_query_ctx(ctx);
    3258           0 :                 return;
    3259             :         }
    3260             : 
    3261           0 :         ctx->subsystem = subsystem;
    3262             : 
    3263           0 :         if (spdk_nvmf_subsystem_pause(subsystem, 0, cb_fn, ctx)) {
    3264           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    3265             :                                                  "Internal error");
    3266           0 :                 free_rpc_subsystem_query_ctx(ctx);
    3267           0 :                 return;
    3268             :         }
    3269             : }
    3270             : 
    3271             : static void
    3272           0 : rpc_nvmf_subsystem_get_controllers(struct spdk_jsonrpc_request *request,
    3273             :                                    const struct spdk_json_val *params)
    3274             : {
    3275           0 :         _rpc_nvmf_subsystem_query(request, params, rpc_nvmf_get_controllers_paused);
    3276           0 : }
    3277           0 : SPDK_RPC_REGISTER("nvmf_subsystem_get_controllers", rpc_nvmf_subsystem_get_controllers,
    3278             :                   SPDK_RPC_RUNTIME);
    3279             : 
    3280             : static void
    3281           0 : rpc_nvmf_subsystem_get_qpairs(struct spdk_jsonrpc_request *request,
    3282             :                               const struct spdk_json_val *params)
    3283             : {
    3284           0 :         _rpc_nvmf_subsystem_query(request, params, rpc_nvmf_get_qpairs_paused);
    3285           0 : }
    3286           0 : SPDK_RPC_REGISTER("nvmf_subsystem_get_qpairs", rpc_nvmf_subsystem_get_qpairs, SPDK_RPC_RUNTIME);
    3287             : 
    3288             : static void
    3289           0 : rpc_nvmf_subsystem_get_listeners(struct spdk_jsonrpc_request *request,
    3290             :                                  const struct spdk_json_val *params)
    3291             : {
    3292           0 :         _rpc_nvmf_subsystem_query(request, params, rpc_nvmf_get_listeners_paused);
    3293           0 : }
    3294           0 : SPDK_RPC_REGISTER("nvmf_subsystem_get_listeners", rpc_nvmf_subsystem_get_listeners,
    3295             :                   SPDK_RPC_RUNTIME);
    3296             : 
    3297             : struct rpc_mdns_prr {
    3298             :         char *tgt_name;
    3299             : };
    3300             : 
    3301             : static const struct spdk_json_object_decoder rpc_mdns_prr_decoders[] = {
    3302             :         {"tgt_name", offsetof(struct rpc_mdns_prr, tgt_name), spdk_json_decode_string, true},
    3303             : };
    3304             : 
    3305             : static void
    3306           0 : rpc_nvmf_publish_mdns_prr(struct spdk_jsonrpc_request *request,
    3307             :                           const struct spdk_json_val *params)
    3308             : {
    3309             :         int rc;
    3310           0 :         struct rpc_mdns_prr req = { 0 };
    3311             :         struct spdk_nvmf_tgt *tgt;
    3312             : 
    3313           0 :         if (params) {
    3314           0 :                 if (spdk_json_decode_object(params, rpc_mdns_prr_decoders,
    3315             :                                             SPDK_COUNTOF(rpc_mdns_prr_decoders),
    3316             :                                             &req)) {
    3317           0 :                         SPDK_ERRLOG("spdk_json_decode_object failed\n");
    3318           0 :                         spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters");
    3319           0 :                         return;
    3320             :                 }
    3321             :         }
    3322             : 
    3323           0 :         tgt = spdk_nvmf_get_tgt(req.tgt_name);
    3324           0 :         if (!tgt) {
    3325           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    3326             :                                                  "Unable to find a target.");
    3327           0 :                 free(req.tgt_name);
    3328           0 :                 return;
    3329             :         }
    3330             : 
    3331           0 :         rc = nvmf_publish_mdns_prr(tgt);
    3332           0 :         if (rc) {
    3333           0 :                 spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
    3334           0 :                 free(req.tgt_name);
    3335           0 :                 return;
    3336             :         }
    3337             : 
    3338           0 :         spdk_jsonrpc_send_bool_response(request, true);
    3339           0 :         free(req.tgt_name);
    3340             : }
    3341           0 : SPDK_RPC_REGISTER("nvmf_publish_mdns_prr", rpc_nvmf_publish_mdns_prr, SPDK_RPC_RUNTIME);
    3342             : 
    3343             : static void
    3344           0 : rpc_nvmf_stop_mdns_prr(struct spdk_jsonrpc_request *request,
    3345             :                        const struct spdk_json_val *params)
    3346             : {
    3347           0 :         struct rpc_mdns_prr req = { 0 };
    3348             :         struct spdk_nvmf_tgt *tgt;
    3349             : 
    3350           0 :         if (params) {
    3351           0 :                 if (spdk_json_decode_object(params, rpc_mdns_prr_decoders,
    3352             :                                             SPDK_COUNTOF(rpc_mdns_prr_decoders),
    3353             :                                             &req)) {
    3354           0 :                         SPDK_ERRLOG("spdk_json_decode_object failed\n");
    3355           0 :                         spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters");
    3356           0 :                         return;
    3357             :                 }
    3358             :         }
    3359             : 
    3360           0 :         tgt = spdk_nvmf_get_tgt(req.tgt_name);
    3361           0 :         if (!tgt) {
    3362           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    3363             :                                                  "Unable to find a target.");
    3364           0 :                 free(req.tgt_name);
    3365           0 :                 return;
    3366             :         }
    3367             : 
    3368           0 :         nvmf_tgt_stop_mdns_prr(tgt);
    3369             : 
    3370           0 :         spdk_jsonrpc_send_bool_response(request, true);
    3371           0 :         free(req.tgt_name);
    3372             : }
    3373           0 : SPDK_RPC_REGISTER("nvmf_stop_mdns_prr", rpc_nvmf_stop_mdns_prr, SPDK_RPC_RUNTIME);

Generated by: LCOV version 1.15