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 1769 0.0 %
Date: 2024-12-13 09:46:16 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           0 : }
      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           0 : }
      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           0 : }
      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           0 :         }
     107             : 
     108           0 :         if (i != size || *str != '\0') {
     109             :                 /* Length mismatch */
     110           0 :                 return -1;
     111             :         }
     112             : 
     113           0 :         return 0;
     114           0 : }
     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           0 :         }
     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           0 :         }
     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           0 :         } 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           0 :         }
     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           0 :                                                      spdk_key_get_name(host->dhchap_key));
     200           0 :                 }
     201           0 :                 if (host->dhchap_ctrlr_key != NULL) {
     202           0 :                         spdk_json_write_named_string(w, "dhchap_ctrlr_key",
     203           0 :                                                      spdk_key_get_name(host->dhchap_ctrlr_key));
     204           0 :                 }
     205           0 :                 spdk_json_write_object_end(w);
     206           0 :         }
     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             :                 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           0 :                 }
     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           0 :                         }
     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           0 :                         }
     247             : 
     248           0 :                         if (!spdk_uuid_is_null(&ns_opts.uuid)) {
     249           0 :                                 spdk_json_write_named_uuid(w, "uuid", &ns_opts.uuid);
     250           0 :                         }
     251             : 
     252           0 :                         if (spdk_nvmf_subsystem_get_ana_reporting(subsystem)) {
     253           0 :                                 spdk_json_write_named_uint32(w, "anagrpid", ns_opts.anagrpid);
     254           0 :                         }
     255             : 
     256           0 :                         spdk_json_write_object_end(w);
     257           0 :                 }
     258           0 :                 spdk_json_write_array_end(w);
     259           0 :         }
     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           0 :         }
     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           0 :         }
     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           0 :         } 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           0 :                 }
     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           0 : }
     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           0 :         } 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           0 :                                     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           0 :                                                      "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           0 :                                                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           0 :                                                      "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           0 :                                                              "Invalid SN %s", req->serial_number);
     416           0 :                         goto cleanup;
     417             :                 }
     418           0 :         }
     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           0 :                                                              "Invalid MN %s", req->model_number);
     425           0 :                         goto cleanup;
     426             :                 }
     427           0 :         }
     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           0 :         } 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           0 :                                                      "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           0 :                                        request);
     460           0 :         if (rc) {
     461           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
     462             :                                                  "Failed to start subsystem");
     463           0 :         }
     464             : 
     465             : 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           0 :         }
     475           0 : }
     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           0 :                                                              "Subsystem destruction failed, rc %d", rc);
     515           0 :                         return;
     516             :                 }
     517             :         }
     518           0 :         spdk_jsonrpc_send_bool_response(request, true);
     519           0 : }
     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           0 :                                       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           0 :                                                      "Unable to change state on subsystem. rc=%d", rc);
     572           0 :         }
     573             : 
     574           0 :         return;
     575             : 
     576             : invalid:
     577           0 :         spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters");
     578             : invalid_custom_response:
     579           0 :         free_rpc_delete_subsystem(&req);
     580           0 : }
     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           0 :                                        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           0 : }
     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           0 :         }
     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           0 :                 }
     704           0 :                 nvmf_rpc_listener_ctx_free(ctx);
     705             :                 /* Can't really do anything to recover here - subsystem will remain paused. */
     706           0 :         }
     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           0 :                                                      "error stopping listener: %d", status);
     717           0 :                 ctx->response_sent = true;
     718           0 :         }
     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           0 :                 }
     725           0 :                 nvmf_rpc_listener_ctx_free(ctx);
     726             :                 /* Can't really do anything to recover here - subsystem will remain paused. */
     727           0 :         }
     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           0 :                                                      "error setting ANA state: %d", status);
     739           0 :                 ctx->response_sent = true;
     740           0 :         }
     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           0 :                 }
     747           0 :                 nvmf_rpc_listener_ctx_free(ctx);
     748             :                 /* Can't really do anything to recover here - subsystem will remain paused. */
     749           0 :         }
     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             :         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           0 :                                                      &ctx->listener_opts);
     779           0 :                 return;
     780             :         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           0 :                                                       nvmf_rpc_stop_listen_async_done, ctx);
     792           0 :                 return;
     793             :         case NVMF_RPC_LISTEN_SET_ANA_STATE:
     794           0 :                 spdk_nvmf_subsystem_set_ana_state(subsystem, &ctx->trid, ctx->ana_state, ctx->anagrpid,
     795           0 :                                                   nvmf_rpc_set_ana_state_done, ctx);
     796           0 :                 return;
     797             :         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           0 :                 }
     806             : 
     807           0 :                 nvmf_rpc_listener_ctx_free(ctx);
     808             :                 /* Can't really do anything to recover here - subsystem will remain paused. */
     809           0 :         }
     810           0 : }
     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           0 :         } 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           0 :         }
     857             : 
     858           0 :         return 0;
     859           0 : }
     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           0 :                                             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           0 :         }
     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           0 :         }
     944           0 : }
     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           0 :                                     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           0 :         }
    1018           0 : }
    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           0 :         }
    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, allow_any_host);
    1102           0 :         opts.trid = trid;
    1103           0 :         opts.secure_channel = ctx.secure_channel;
    1104           0 :         opts.allow_any_host = ctx.allow_any_host;
    1105             : 
    1106           0 :         rc = spdk_nvmf_tgt_add_referral(tgt, &opts);
    1107           0 :         if (rc != 0) {
    1108           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    1109             :                                                  "Internal error");
    1110           0 :                 nvmf_rpc_referral_ctx_free(&ctx);
    1111           0 :                 return;
    1112             :         }
    1113             : 
    1114           0 :         nvmf_rpc_referral_ctx_free(&ctx);
    1115             : 
    1116           0 :         spdk_jsonrpc_send_bool_response(request, true);
    1117           0 : }
    1118             : 
    1119           0 : SPDK_RPC_REGISTER("nvmf_discovery_add_referral", rpc_nvmf_add_referral,
    1120             :                   SPDK_RPC_RUNTIME);
    1121             : 
    1122             : static void
    1123           0 : rpc_nvmf_remove_referral(struct spdk_jsonrpc_request *request,
    1124             :                          const struct spdk_json_val *params)
    1125             : {
    1126           0 :         struct nvmf_rpc_referral_ctx ctx = {};
    1127           0 :         struct spdk_nvme_transport_id trid = {};
    1128           0 :         struct spdk_nvmf_referral_opts opts = {};
    1129             :         struct spdk_nvmf_tgt *tgt;
    1130             :         int rc;
    1131             : 
    1132           0 :         if (spdk_json_decode_object(params, nvmf_rpc_referral_decoder,
    1133             :                                     SPDK_COUNTOF(nvmf_rpc_referral_decoder),
    1134             :                                     &ctx)) {
    1135           0 :                 SPDK_ERRLOG("spdk_json_decode_object failed\n");
    1136           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters");
    1137           0 :                 nvmf_rpc_referral_ctx_free(&ctx);
    1138           0 :                 return;
    1139             :         }
    1140             : 
    1141           0 :         tgt = spdk_nvmf_get_tgt(ctx.tgt_name);
    1142           0 :         if (!tgt) {
    1143           0 :                 SPDK_ERRLOG("Unable to find a target object.\n");
    1144           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    1145             :                                                  "Unable to find a target.");
    1146           0 :                 nvmf_rpc_referral_ctx_free(&ctx);
    1147           0 :                 return;
    1148             :         }
    1149             : 
    1150           0 :         if (rpc_listen_address_to_trid(&ctx.address, &trid)) {
    1151           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
    1152             :                                                  "Invalid parameters");
    1153           0 :                 nvmf_rpc_referral_ctx_free(&ctx);
    1154           0 :                 return;
    1155             :         }
    1156             : 
    1157           0 :         if (ctx.subnqn != NULL) {
    1158           0 :                 rc = snprintf(trid.subnqn, sizeof(trid.subnqn), "%s", ctx.subnqn);
    1159           0 :                 if (rc < 0 || (size_t)rc >= sizeof(trid.subnqn)) {
    1160           0 :                         spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
    1161             :                                                          "Invalid subsystem NQN");
    1162           0 :                         nvmf_rpc_referral_ctx_free(&ctx);
    1163           0 :                         return;
    1164             :                 }
    1165           0 :         }
    1166             : 
    1167           0 :         opts.size = SPDK_SIZEOF(&opts, allow_any_host);
    1168           0 :         opts.trid = trid;
    1169             : 
    1170           0 :         if (spdk_nvmf_tgt_remove_referral(tgt, &opts)) {
    1171           0 :                 SPDK_ERRLOG("Failed to remove referral.\n");
    1172           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    1173             :                                                  "Unable to remove a referral.");
    1174           0 :                 nvmf_rpc_referral_ctx_free(&ctx);
    1175           0 :                 return;
    1176             :         }
    1177             : 
    1178           0 :         nvmf_rpc_referral_ctx_free(&ctx);
    1179             : 
    1180           0 :         spdk_jsonrpc_send_bool_response(request, true);
    1181           0 : }
    1182             : 
    1183           0 : SPDK_RPC_REGISTER("nvmf_discovery_remove_referral", rpc_nvmf_remove_referral,
    1184             :                   SPDK_RPC_RUNTIME);
    1185             : 
    1186             : static void
    1187           0 : dump_nvmf_referral(struct spdk_json_write_ctx *w,
    1188             :                    struct spdk_nvmf_referral *referral)
    1189             : {
    1190             :         struct spdk_nvmf_host                   *host;
    1191           0 :         spdk_json_write_object_begin(w);
    1192             : 
    1193           0 :         spdk_json_write_named_object_begin(w, "address");
    1194           0 :         nvmf_transport_listen_dump_trid(&referral->trid, w);
    1195           0 :         spdk_json_write_object_end(w);
    1196           0 :         spdk_json_write_named_bool(w, "secure_channel",
    1197           0 :                                    referral->entry.treq.secure_channel == SPDK_NVMF_TREQ_SECURE_CHANNEL_REQUIRED);
    1198           0 :         spdk_json_write_named_string(w, "subnqn", referral->trid.subnqn);
    1199           0 :         spdk_json_write_named_bool(w, "allow_any_host",
    1200           0 :                                    spdk_nvmf_referral_get_allow_any_host(referral));
    1201           0 :         spdk_json_write_named_array_begin(w, "hosts");
    1202           0 :         for (host = spdk_nvmf_referral_get_first_host(referral); host != NULL;
    1203           0 :              host = spdk_nvmf_referral_get_next_host(referral, host)) {
    1204           0 :                 spdk_json_write_object_begin(w);
    1205           0 :                 spdk_json_write_named_string(w, "nqn", spdk_nvmf_host_get_nqn(host));
    1206           0 :                 spdk_json_write_object_end(w);
    1207           0 :         }
    1208           0 :         spdk_json_write_array_end(w);
    1209             : 
    1210           0 :         spdk_json_write_object_end(w);
    1211           0 : }
    1212             : 
    1213             : struct rpc_get_referrals_ctx {
    1214             :         char *tgt_name;
    1215             : };
    1216             : 
    1217             : static const struct spdk_json_object_decoder rpc_get_referrals_decoders[] = {
    1218             :         {"tgt_name", offsetof(struct rpc_get_referrals_ctx, tgt_name), spdk_json_decode_string, true},
    1219             : };
    1220             : 
    1221             : static void
    1222           0 : free_rpc_get_referrals_ctx(struct rpc_get_referrals_ctx *ctx)
    1223             : {
    1224           0 :         free(ctx->tgt_name);
    1225           0 :         free(ctx);
    1226           0 : }
    1227             : 
    1228             : static void
    1229           0 : rpc_nvmf_get_referrals(struct spdk_jsonrpc_request *request,
    1230             :                        const struct spdk_json_val *params)
    1231             : {
    1232             :         struct rpc_get_referrals_ctx *ctx;
    1233             :         struct spdk_nvmf_tgt *tgt;
    1234             :         struct spdk_json_write_ctx *w;
    1235             :         struct spdk_nvmf_referral *referral;
    1236             : 
    1237           0 :         ctx = calloc(1, sizeof(*ctx));
    1238           0 :         if (!ctx) {
    1239           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    1240             :                                                  "Out of memory");
    1241           0 :                 return;
    1242             :         }
    1243             : 
    1244           0 :         if (params) {
    1245           0 :                 if (spdk_json_decode_object(params, rpc_get_referrals_decoders,
    1246             :                                             SPDK_COUNTOF(rpc_get_referrals_decoders),
    1247           0 :                                             ctx)) {
    1248           0 :                         SPDK_ERRLOG("spdk_json_decode_object failed\n");
    1249           0 :                         spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
    1250             :                                                          "Invalid parameters");
    1251           0 :                         free_rpc_get_referrals_ctx(ctx);
    1252           0 :                         return;
    1253             :                 }
    1254           0 :         }
    1255             : 
    1256           0 :         tgt = spdk_nvmf_get_tgt(ctx->tgt_name);
    1257           0 :         if (!tgt) {
    1258           0 :                 SPDK_ERRLOG("Unable to find a target object.\n");
    1259           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    1260             :                                                  "Unable to find a target");
    1261           0 :                 free_rpc_get_referrals_ctx(ctx);
    1262           0 :                 return;
    1263             :         }
    1264             : 
    1265           0 :         w = spdk_jsonrpc_begin_result(request);
    1266             : 
    1267           0 :         spdk_json_write_array_begin(w);
    1268             : 
    1269           0 :         TAILQ_FOREACH(referral, &tgt->referrals, link) {
    1270           0 :                 dump_nvmf_referral(w, referral);
    1271           0 :         }
    1272             : 
    1273           0 :         spdk_json_write_array_end(w);
    1274             : 
    1275           0 :         spdk_jsonrpc_end_result(request, w);
    1276             : 
    1277           0 :         free_rpc_get_referrals_ctx(ctx);
    1278           0 : }
    1279           0 : SPDK_RPC_REGISTER("nvmf_discovery_get_referrals", rpc_nvmf_get_referrals,
    1280             :                   SPDK_RPC_RUNTIME);
    1281             : 
    1282             : static const struct spdk_json_object_decoder nvmf_rpc_set_ana_state_decoder[] = {
    1283             :         {"nqn", offsetof(struct nvmf_rpc_listener_ctx, nqn), spdk_json_decode_string},
    1284             :         {"listen_address", offsetof(struct nvmf_rpc_listener_ctx, address), decode_rpc_listen_address},
    1285             :         {"ana_state", offsetof(struct nvmf_rpc_listener_ctx, ana_state_str), spdk_json_decode_string},
    1286             :         {"tgt_name", offsetof(struct nvmf_rpc_listener_ctx, tgt_name), spdk_json_decode_string, true},
    1287             :         {"anagrpid", offsetof(struct nvmf_rpc_listener_ctx, anagrpid), spdk_json_decode_uint32, true},
    1288             : };
    1289             : 
    1290             : static int
    1291           0 : rpc_ana_state_parse(const char *str, enum spdk_nvme_ana_state *ana_state)
    1292             : {
    1293           0 :         if (ana_state == NULL || str == NULL) {
    1294           0 :                 return -EINVAL;
    1295             :         }
    1296             : 
    1297           0 :         if (strcasecmp(str, "optimized") == 0) {
    1298           0 :                 *ana_state = SPDK_NVME_ANA_OPTIMIZED_STATE;
    1299           0 :         } else if (strcasecmp(str, "non_optimized") == 0) {
    1300           0 :                 *ana_state = SPDK_NVME_ANA_NON_OPTIMIZED_STATE;
    1301           0 :         } else if (strcasecmp(str, "inaccessible") == 0) {
    1302           0 :                 *ana_state = SPDK_NVME_ANA_INACCESSIBLE_STATE;
    1303           0 :         } else {
    1304           0 :                 return -ENOENT;
    1305             :         }
    1306             : 
    1307           0 :         return 0;
    1308           0 : }
    1309             : 
    1310             : static void
    1311           0 : rpc_nvmf_subsystem_listener_set_ana_state(struct spdk_jsonrpc_request *request,
    1312             :                 const struct spdk_json_val *params)
    1313             : {
    1314             :         struct nvmf_rpc_listener_ctx *ctx;
    1315             :         struct spdk_nvmf_subsystem *subsystem;
    1316             :         struct spdk_nvmf_tgt *tgt;
    1317             : 
    1318           0 :         ctx = calloc(1, sizeof(*ctx));
    1319           0 :         if (!ctx) {
    1320           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    1321             :                                                  "Out of memory");
    1322           0 :                 return;
    1323             :         }
    1324             : 
    1325           0 :         ctx->request = request;
    1326             : 
    1327           0 :         if (spdk_json_decode_object(params, nvmf_rpc_set_ana_state_decoder,
    1328             :                                     SPDK_COUNTOF(nvmf_rpc_set_ana_state_decoder),
    1329           0 :                                     ctx)) {
    1330           0 :                 SPDK_ERRLOG("spdk_json_decode_object failed\n");
    1331           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
    1332             :                                                  "Invalid parameters");
    1333           0 :                 nvmf_rpc_listener_ctx_free(ctx);
    1334           0 :                 return;
    1335             :         }
    1336             : 
    1337           0 :         tgt = spdk_nvmf_get_tgt(ctx->tgt_name);
    1338           0 :         if (!tgt) {
    1339           0 :                 SPDK_ERRLOG("Unable to find a target object.\n");
    1340           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    1341             :                                                  "Unable to find a target.");
    1342           0 :                 nvmf_rpc_listener_ctx_free(ctx);
    1343           0 :                 return;
    1344             :         }
    1345             : 
    1346           0 :         ctx->tgt = tgt;
    1347             : 
    1348           0 :         subsystem = spdk_nvmf_tgt_find_subsystem(tgt, ctx->nqn);
    1349           0 :         if (!subsystem) {
    1350           0 :                 SPDK_ERRLOG("Unable to find subsystem with NQN %s\n", ctx->nqn);
    1351           0 :                 spdk_jsonrpc_send_error_response_fmt(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
    1352             :                                                      "Unable to find subsystem with NQN %s",
    1353           0 :                                                      ctx->nqn);
    1354           0 :                 nvmf_rpc_listener_ctx_free(ctx);
    1355           0 :                 return;
    1356             :         }
    1357             : 
    1358           0 :         ctx->subsystem = subsystem;
    1359             : 
    1360           0 :         if (rpc_listen_address_to_trid(&ctx->address, &ctx->trid)) {
    1361           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
    1362             :                                                  "Invalid parameters");
    1363           0 :                 nvmf_rpc_listener_ctx_free(ctx);
    1364           0 :                 return;
    1365             :         }
    1366             : 
    1367           0 :         if (rpc_ana_state_parse(ctx->ana_state_str, &ctx->ana_state)) {
    1368           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
    1369             :                                                  "Invalid parameters");
    1370           0 :                 nvmf_rpc_listener_ctx_free(ctx);
    1371           0 :                 return;
    1372             :         }
    1373             : 
    1374           0 :         ctx->op = NVMF_RPC_LISTEN_SET_ANA_STATE;
    1375             : 
    1376           0 :         if (spdk_nvmf_subsystem_pause(subsystem, 0, nvmf_rpc_listen_paused, ctx)) {
    1377           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    1378             :                                                  "Internal error");
    1379           0 :                 nvmf_rpc_listener_ctx_free(ctx);
    1380           0 :         }
    1381           0 : }
    1382           0 : SPDK_RPC_REGISTER("nvmf_subsystem_listener_set_ana_state",
    1383             :                   rpc_nvmf_subsystem_listener_set_ana_state, SPDK_RPC_RUNTIME);
    1384             : 
    1385             : struct nvmf_rpc_ns_params {
    1386             :         char *bdev_name;
    1387             :         char *ptpl_file;
    1388             :         uint32_t nsid;
    1389             :         char nguid[16];
    1390             :         char eui64[8];
    1391             :         struct spdk_uuid uuid;
    1392             :         uint32_t anagrpid;
    1393             :         bool no_auto_visible;
    1394             :         bool hide_metadata;
    1395             : };
    1396             : 
    1397             : static const struct spdk_json_object_decoder rpc_ns_params_decoders[] = {
    1398             :         {"nsid", offsetof(struct nvmf_rpc_ns_params, nsid), spdk_json_decode_uint32, true},
    1399             :         {"bdev_name", offsetof(struct nvmf_rpc_ns_params, bdev_name), spdk_json_decode_string},
    1400             :         {"ptpl_file", offsetof(struct nvmf_rpc_ns_params, ptpl_file), spdk_json_decode_string, true},
    1401             :         {"nguid", offsetof(struct nvmf_rpc_ns_params, nguid), decode_ns_nguid, true},
    1402             :         {"eui64", offsetof(struct nvmf_rpc_ns_params, eui64), decode_ns_eui64, true},
    1403             :         {"uuid", offsetof(struct nvmf_rpc_ns_params, uuid), spdk_json_decode_uuid, true},
    1404             :         {"anagrpid", offsetof(struct nvmf_rpc_ns_params, anagrpid), spdk_json_decode_uint32, true},
    1405             :         {"no_auto_visible", offsetof(struct nvmf_rpc_ns_params, no_auto_visible), spdk_json_decode_bool, true},
    1406             :         {"hide_metadata", offsetof(struct nvmf_rpc_ns_params, hide_metadata), spdk_json_decode_bool, true},
    1407             : };
    1408             : 
    1409             : static int
    1410           0 : decode_rpc_ns_params(const struct spdk_json_val *val, void *out)
    1411             : {
    1412           0 :         struct nvmf_rpc_ns_params *ns_params = out;
    1413             : 
    1414           0 :         return spdk_json_decode_object(val, rpc_ns_params_decoders,
    1415             :                                        SPDK_COUNTOF(rpc_ns_params_decoders),
    1416           0 :                                        ns_params);
    1417             : }
    1418             : 
    1419             : struct nvmf_rpc_ns_ctx {
    1420             :         char *nqn;
    1421             :         char *tgt_name;
    1422             :         struct nvmf_rpc_ns_params ns_params;
    1423             : 
    1424             :         struct spdk_jsonrpc_request *request;
    1425             :         const struct spdk_json_val *params;
    1426             :         bool response_sent;
    1427             : };
    1428             : 
    1429             : static const struct spdk_json_object_decoder nvmf_rpc_subsystem_ns_decoder[] = {
    1430             :         {"nqn", offsetof(struct nvmf_rpc_ns_ctx, nqn), spdk_json_decode_string},
    1431             :         {"namespace", offsetof(struct nvmf_rpc_ns_ctx, ns_params), decode_rpc_ns_params},
    1432             :         {"tgt_name", offsetof(struct nvmf_rpc_ns_ctx, tgt_name), spdk_json_decode_string, true},
    1433             : };
    1434             : 
    1435             : static void
    1436           0 : nvmf_rpc_ns_ctx_free(struct nvmf_rpc_ns_ctx *ctx)
    1437             : {
    1438           0 :         free(ctx->nqn);
    1439           0 :         free(ctx->tgt_name);
    1440           0 :         free(ctx->ns_params.bdev_name);
    1441           0 :         free(ctx->ns_params.ptpl_file);
    1442           0 :         free(ctx);
    1443           0 : }
    1444             : 
    1445             : static void
    1446           0 : nvmf_rpc_ns_failback_resumed(struct spdk_nvmf_subsystem *subsystem,
    1447             :                              void *cb_arg, int status)
    1448             : {
    1449           0 :         struct nvmf_rpc_ns_ctx *ctx = cb_arg;
    1450           0 :         struct spdk_jsonrpc_request *request = ctx->request;
    1451             : 
    1452           0 :         if (status) {
    1453           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    1454             :                                                  "Unable to add ns, subsystem in invalid state");
    1455           0 :         } else {
    1456           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    1457             :                                                  "Unable to add ns, subsystem in active state");
    1458             :         }
    1459             : 
    1460           0 :         nvmf_rpc_ns_ctx_free(ctx);
    1461           0 : }
    1462             : 
    1463             : static void
    1464           0 : nvmf_rpc_ns_resumed(struct spdk_nvmf_subsystem *subsystem,
    1465             :                     void *cb_arg, int status)
    1466             : {
    1467           0 :         struct nvmf_rpc_ns_ctx *ctx = cb_arg;
    1468           0 :         struct spdk_jsonrpc_request *request = ctx->request;
    1469           0 :         uint32_t nsid = ctx->ns_params.nsid;
    1470           0 :         bool response_sent = ctx->response_sent;
    1471             :         struct spdk_json_write_ctx *w;
    1472             :         int rc;
    1473             : 
    1474             :         /* The case where the call to add the namespace was successful, but the subsystem couldn't be resumed. */
    1475           0 :         if (status && !ctx->response_sent) {
    1476           0 :                 rc = spdk_nvmf_subsystem_remove_ns(subsystem, nsid);
    1477           0 :                 if (rc != 0) {
    1478           0 :                         spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    1479             :                                                          "Unable to add ns, subsystem in invalid state");
    1480           0 :                         nvmf_rpc_ns_ctx_free(ctx);
    1481           0 :                         return;
    1482             :                 }
    1483             : 
    1484           0 :                 rc = spdk_nvmf_subsystem_resume(subsystem, nvmf_rpc_ns_failback_resumed, ctx);
    1485           0 :                 if (rc != 0) {
    1486           0 :                         spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, "Internal error");
    1487           0 :                         nvmf_rpc_ns_ctx_free(ctx);
    1488           0 :                         return;
    1489             :                 }
    1490             : 
    1491           0 :                 return;
    1492             :         }
    1493             : 
    1494           0 :         nvmf_rpc_ns_ctx_free(ctx);
    1495             : 
    1496           0 :         if (response_sent) {
    1497           0 :                 return;
    1498             :         }
    1499             : 
    1500           0 :         w = spdk_jsonrpc_begin_result(request);
    1501           0 :         spdk_json_write_uint32(w, nsid);
    1502           0 :         spdk_jsonrpc_end_result(request, w);
    1503           0 : }
    1504             : 
    1505             : static void
    1506           0 : nvmf_rpc_ns_paused(struct spdk_nvmf_subsystem *subsystem,
    1507             :                    void *cb_arg, int status)
    1508             : {
    1509           0 :         struct nvmf_rpc_ns_ctx *ctx = cb_arg;
    1510             :         struct spdk_nvmf_ns_opts ns_opts;
    1511             : 
    1512           0 :         spdk_nvmf_ns_opts_get_defaults(&ns_opts, sizeof(ns_opts));
    1513           0 :         ns_opts.nsid = ctx->ns_params.nsid;
    1514           0 :         ns_opts.transport_specific = ctx->params;
    1515             : 
    1516             :         SPDK_STATIC_ASSERT(sizeof(ns_opts.nguid) == sizeof(ctx->ns_params.nguid), "size mismatch");
    1517           0 :         memcpy(ns_opts.nguid, ctx->ns_params.nguid, sizeof(ns_opts.nguid));
    1518             : 
    1519             :         SPDK_STATIC_ASSERT(sizeof(ns_opts.eui64) == sizeof(ctx->ns_params.eui64), "size mismatch");
    1520           0 :         memcpy(ns_opts.eui64, ctx->ns_params.eui64, sizeof(ns_opts.eui64));
    1521             : 
    1522           0 :         if (!spdk_uuid_is_null(&ctx->ns_params.uuid)) {
    1523           0 :                 ns_opts.uuid = ctx->ns_params.uuid;
    1524           0 :         }
    1525             : 
    1526           0 :         ns_opts.anagrpid = ctx->ns_params.anagrpid;
    1527           0 :         ns_opts.no_auto_visible = ctx->ns_params.no_auto_visible;
    1528           0 :         ns_opts.hide_metadata = ctx->ns_params.hide_metadata;
    1529             : 
    1530           0 :         ctx->ns_params.nsid = spdk_nvmf_subsystem_add_ns_ext(subsystem, ctx->ns_params.bdev_name,
    1531             :                               &ns_opts, sizeof(ns_opts),
    1532           0 :                               ctx->ns_params.ptpl_file);
    1533           0 :         if (ctx->ns_params.nsid == 0) {
    1534           0 :                 SPDK_ERRLOG("Unable to add namespace\n");
    1535           0 :                 spdk_jsonrpc_send_error_response(ctx->request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
    1536             :                                                  "Invalid parameters");
    1537           0 :                 ctx->response_sent = true;
    1538           0 :                 goto resume;
    1539             :         }
    1540             : 
    1541             : resume:
    1542           0 :         if (spdk_nvmf_subsystem_resume(subsystem, nvmf_rpc_ns_resumed, ctx)) {
    1543           0 :                 spdk_jsonrpc_send_error_response(ctx->request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, "Internal error");
    1544           0 :                 nvmf_rpc_ns_ctx_free(ctx);
    1545           0 :         }
    1546           0 : }
    1547             : 
    1548             : static void
    1549           0 : rpc_nvmf_subsystem_add_ns(struct spdk_jsonrpc_request *request,
    1550             :                           const struct spdk_json_val *params)
    1551             : {
    1552             :         struct nvmf_rpc_ns_ctx *ctx;
    1553             :         struct spdk_nvmf_subsystem *subsystem;
    1554             :         struct spdk_nvmf_tgt *tgt;
    1555             :         int rc;
    1556             : 
    1557           0 :         ctx = calloc(1, sizeof(*ctx));
    1558           0 :         if (!ctx) {
    1559           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, "Out of memory");
    1560           0 :                 return;
    1561             :         }
    1562             : 
    1563           0 :         if (spdk_json_decode_object_relaxed(params, nvmf_rpc_subsystem_ns_decoder,
    1564           0 :                                             SPDK_COUNTOF(nvmf_rpc_subsystem_ns_decoder), ctx)) {
    1565           0 :                 SPDK_ERRLOG("spdk_json_decode_object failed\n");
    1566           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters");
    1567           0 :                 nvmf_rpc_ns_ctx_free(ctx);
    1568           0 :                 return;
    1569             :         }
    1570             : 
    1571           0 :         ctx->request = request;
    1572           0 :         ctx->params = params;
    1573           0 :         ctx->response_sent = false;
    1574             : 
    1575           0 :         tgt = spdk_nvmf_get_tgt(ctx->tgt_name);
    1576           0 :         if (!tgt) {
    1577           0 :                 SPDK_ERRLOG("Unable to find a target object.\n");
    1578           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    1579             :                                                  "Unable to find a target.");
    1580           0 :                 nvmf_rpc_ns_ctx_free(ctx);
    1581           0 :                 return;
    1582             :         }
    1583             : 
    1584           0 :         subsystem = spdk_nvmf_tgt_find_subsystem(tgt, ctx->nqn);
    1585           0 :         if (!subsystem) {
    1586           0 :                 SPDK_ERRLOG("Unable to find subsystem with NQN %s\n", ctx->nqn);
    1587           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters");
    1588           0 :                 nvmf_rpc_ns_ctx_free(ctx);
    1589           0 :                 return;
    1590             :         }
    1591             : 
    1592           0 :         rc = spdk_nvmf_subsystem_pause(subsystem, ctx->ns_params.nsid, nvmf_rpc_ns_paused, ctx);
    1593           0 :         if (rc != 0) {
    1594           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, "Internal error");
    1595           0 :                 nvmf_rpc_ns_ctx_free(ctx);
    1596           0 :         }
    1597           0 : }
    1598           0 : SPDK_RPC_REGISTER("nvmf_subsystem_add_ns", rpc_nvmf_subsystem_add_ns, SPDK_RPC_RUNTIME)
    1599             : 
    1600             : struct nvmf_rpc_ana_group_ctx {
    1601             :         char *nqn;
    1602             :         char *tgt_name;
    1603             :         uint32_t nsid;
    1604             :         uint32_t anagrpid;
    1605             : 
    1606             :         struct spdk_jsonrpc_request *request;
    1607             :         bool response_sent;
    1608             : };
    1609             : 
    1610             : static const struct spdk_json_object_decoder nvmf_rpc_subsystem_ana_group_decoder[] = {
    1611             :         {"nqn", offsetof(struct nvmf_rpc_ana_group_ctx, nqn), spdk_json_decode_string},
    1612             :         {"nsid", offsetof(struct nvmf_rpc_ana_group_ctx, nsid), spdk_json_decode_uint32},
    1613             :         {"anagrpid", offsetof(struct nvmf_rpc_ana_group_ctx, anagrpid), spdk_json_decode_uint32},
    1614             :         {"tgt_name", offsetof(struct nvmf_rpc_ana_group_ctx, tgt_name), spdk_json_decode_string, true},
    1615             : };
    1616             : 
    1617             : static void
    1618           0 : nvmf_rpc_ana_group_ctx_free(struct nvmf_rpc_ana_group_ctx *ctx)
    1619             : {
    1620           0 :         free(ctx->nqn);
    1621           0 :         free(ctx->tgt_name);
    1622           0 :         free(ctx);
    1623           0 : }
    1624             : 
    1625             : static void
    1626           0 : nvmf_rpc_anagrpid_resumed(struct spdk_nvmf_subsystem *subsystem,
    1627             :                           void *cb_arg, int status)
    1628             : {
    1629           0 :         struct nvmf_rpc_ana_group_ctx *ctx = cb_arg;
    1630           0 :         struct spdk_jsonrpc_request *request = ctx->request;
    1631           0 :         bool response_sent = ctx->response_sent;
    1632             : 
    1633           0 :         nvmf_rpc_ana_group_ctx_free(ctx);
    1634             : 
    1635           0 :         if (response_sent) {
    1636           0 :                 return;
    1637             :         }
    1638             : 
    1639           0 :         spdk_jsonrpc_send_bool_response(request, true);
    1640           0 : }
    1641             : 
    1642             : static void
    1643           0 : nvmf_rpc_ana_group(struct spdk_nvmf_subsystem *subsystem,
    1644             :                    void *cb_arg, int status)
    1645             : {
    1646           0 :         struct nvmf_rpc_ana_group_ctx *ctx = cb_arg;
    1647             :         int rc;
    1648             : 
    1649           0 :         rc = spdk_nvmf_subsystem_set_ns_ana_group(subsystem, ctx->nsid, ctx->anagrpid);
    1650           0 :         if (rc != 0) {
    1651           0 :                 SPDK_ERRLOG("Unable to change ANA group ID\n");
    1652           0 :                 spdk_jsonrpc_send_error_response(ctx->request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
    1653             :                                                  "Invalid parameters");
    1654           0 :                 ctx->response_sent = true;
    1655           0 :         }
    1656             : 
    1657           0 :         if (spdk_nvmf_subsystem_resume(subsystem, nvmf_rpc_anagrpid_resumed, ctx)) {
    1658           0 :                 if (!ctx->response_sent) {
    1659           0 :                         spdk_jsonrpc_send_error_response(ctx->request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    1660             :                                                          "Internal error");
    1661           0 :                 }
    1662           0 :                 nvmf_rpc_ana_group_ctx_free(ctx);
    1663           0 :         }
    1664           0 : }
    1665             : 
    1666             : static void
    1667           0 : rpc_nvmf_subsystem_set_ns_ana_group(struct spdk_jsonrpc_request *request,
    1668             :                                     const struct spdk_json_val *params)
    1669             : {
    1670             :         struct nvmf_rpc_ana_group_ctx *ctx;
    1671             :         struct spdk_nvmf_subsystem *subsystem;
    1672             :         struct spdk_nvmf_tgt *tgt;
    1673             :         int rc;
    1674             : 
    1675           0 :         ctx = calloc(1, sizeof(*ctx));
    1676           0 :         if (!ctx) {
    1677           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, "Out of memory");
    1678           0 :                 return;
    1679             :         }
    1680             : 
    1681           0 :         if (spdk_json_decode_object(params, nvmf_rpc_subsystem_ana_group_decoder,
    1682           0 :                                     SPDK_COUNTOF(nvmf_rpc_subsystem_ana_group_decoder), ctx)) {
    1683           0 :                 SPDK_ERRLOG("spdk_json_decode_object failed\n");
    1684           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters");
    1685           0 :                 nvmf_rpc_ana_group_ctx_free(ctx);
    1686           0 :                 return;
    1687             :         }
    1688             : 
    1689           0 :         ctx->request = request;
    1690           0 :         ctx->response_sent = false;
    1691             : 
    1692           0 :         tgt = spdk_nvmf_get_tgt(ctx->tgt_name);
    1693           0 :         if (!tgt) {
    1694           0 :                 SPDK_ERRLOG("Unable to find a target object.\n");
    1695           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    1696             :                                                  "Unable to find a target.");
    1697           0 :                 nvmf_rpc_ana_group_ctx_free(ctx);
    1698           0 :                 return;
    1699             :         }
    1700             : 
    1701           0 :         subsystem = spdk_nvmf_tgt_find_subsystem(tgt, ctx->nqn);
    1702           0 :         if (!subsystem) {
    1703           0 :                 SPDK_ERRLOG("Unable to find subsystem with NQN %s\n", ctx->nqn);
    1704           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters");
    1705           0 :                 nvmf_rpc_ana_group_ctx_free(ctx);
    1706           0 :                 return;
    1707             :         }
    1708             : 
    1709           0 :         rc = spdk_nvmf_subsystem_pause(subsystem, ctx->nsid, nvmf_rpc_ana_group, ctx);
    1710           0 :         if (rc != 0) {
    1711           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, "Internal error");
    1712           0 :                 nvmf_rpc_ana_group_ctx_free(ctx);
    1713           0 :         }
    1714           0 : }
    1715           0 : SPDK_RPC_REGISTER("nvmf_subsystem_set_ns_ana_group", rpc_nvmf_subsystem_set_ns_ana_group,
    1716             :                   SPDK_RPC_RUNTIME)
    1717             : 
    1718             : struct nvmf_rpc_remove_ns_ctx {
    1719             :         char *nqn;
    1720             :         char *tgt_name;
    1721             :         uint32_t nsid;
    1722             : 
    1723             :         struct spdk_jsonrpc_request *request;
    1724             :         bool response_sent;
    1725             : };
    1726             : 
    1727             : static const struct spdk_json_object_decoder nvmf_rpc_subsystem_remove_ns_decoder[] = {
    1728             :         {"nqn", offsetof(struct nvmf_rpc_remove_ns_ctx, nqn), spdk_json_decode_string},
    1729             :         {"nsid", offsetof(struct nvmf_rpc_remove_ns_ctx, nsid), spdk_json_decode_uint32},
    1730             :         {"tgt_name", offsetof(struct nvmf_rpc_remove_ns_ctx, tgt_name), spdk_json_decode_string, true},
    1731             : };
    1732             : 
    1733             : static void
    1734           0 : nvmf_rpc_remove_ns_ctx_free(struct nvmf_rpc_remove_ns_ctx *ctx)
    1735             : {
    1736           0 :         free(ctx->nqn);
    1737           0 :         free(ctx->tgt_name);
    1738           0 :         free(ctx);
    1739           0 : }
    1740             : 
    1741             : static void
    1742           0 : nvmf_rpc_remove_ns_resumed(struct spdk_nvmf_subsystem *subsystem,
    1743             :                            void *cb_arg, int status)
    1744             : {
    1745           0 :         struct nvmf_rpc_remove_ns_ctx *ctx = cb_arg;
    1746           0 :         struct spdk_jsonrpc_request *request = ctx->request;
    1747           0 :         bool response_sent = ctx->response_sent;
    1748             : 
    1749           0 :         nvmf_rpc_remove_ns_ctx_free(ctx);
    1750             : 
    1751           0 :         if (response_sent) {
    1752           0 :                 return;
    1753             :         }
    1754             : 
    1755           0 :         spdk_jsonrpc_send_bool_response(request, true);
    1756           0 : }
    1757             : 
    1758             : static void
    1759           0 : nvmf_rpc_remove_ns_paused(struct spdk_nvmf_subsystem *subsystem,
    1760             :                           void *cb_arg, int status)
    1761             : {
    1762           0 :         struct nvmf_rpc_remove_ns_ctx *ctx = cb_arg;
    1763             :         int ret;
    1764             : 
    1765           0 :         ret = spdk_nvmf_subsystem_remove_ns(subsystem, ctx->nsid);
    1766           0 :         if (ret < 0) {
    1767           0 :                 SPDK_ERRLOG("Unable to remove namespace ID %u\n", ctx->nsid);
    1768           0 :                 spdk_jsonrpc_send_error_response(ctx->request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
    1769             :                                                  "Invalid parameters");
    1770           0 :                 ctx->response_sent = true;
    1771           0 :         }
    1772             : 
    1773           0 :         if (spdk_nvmf_subsystem_resume(subsystem, nvmf_rpc_remove_ns_resumed, ctx)) {
    1774           0 :                 if (!ctx->response_sent) {
    1775           0 :                         spdk_jsonrpc_send_error_response(ctx->request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, "Internal error");
    1776           0 :                 }
    1777           0 :                 nvmf_rpc_remove_ns_ctx_free(ctx);
    1778           0 :         }
    1779           0 : }
    1780             : 
    1781             : static void
    1782           0 : rpc_nvmf_subsystem_remove_ns(struct spdk_jsonrpc_request *request,
    1783             :                              const struct spdk_json_val *params)
    1784             : {
    1785             :         struct nvmf_rpc_remove_ns_ctx *ctx;
    1786             :         struct spdk_nvmf_subsystem *subsystem;
    1787             :         struct spdk_nvmf_tgt *tgt;
    1788             :         int rc;
    1789             : 
    1790           0 :         ctx = calloc(1, sizeof(*ctx));
    1791           0 :         if (!ctx) {
    1792           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, "Out of memory");
    1793           0 :                 return;
    1794             :         }
    1795             : 
    1796           0 :         if (spdk_json_decode_object(params, nvmf_rpc_subsystem_remove_ns_decoder,
    1797             :                                     SPDK_COUNTOF(nvmf_rpc_subsystem_remove_ns_decoder),
    1798           0 :                                     ctx)) {
    1799           0 :                 SPDK_ERRLOG("spdk_json_decode_object failed\n");
    1800           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters");
    1801           0 :                 nvmf_rpc_remove_ns_ctx_free(ctx);
    1802           0 :                 return;
    1803             :         }
    1804             : 
    1805           0 :         tgt = spdk_nvmf_get_tgt(ctx->tgt_name);
    1806           0 :         if (!tgt) {
    1807           0 :                 SPDK_ERRLOG("Unable to find a target object.\n");
    1808           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    1809             :                                                  "Unable to find a target.");
    1810           0 :                 nvmf_rpc_remove_ns_ctx_free(ctx);
    1811           0 :                 return;
    1812             :         }
    1813             : 
    1814           0 :         ctx->request = request;
    1815           0 :         ctx->response_sent = false;
    1816             : 
    1817           0 :         subsystem = spdk_nvmf_tgt_find_subsystem(tgt, ctx->nqn);
    1818           0 :         if (!subsystem) {
    1819           0 :                 SPDK_ERRLOG("Unable to find subsystem with NQN %s\n", ctx->nqn);
    1820           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters");
    1821           0 :                 nvmf_rpc_remove_ns_ctx_free(ctx);
    1822           0 :                 return;
    1823             :         }
    1824             : 
    1825           0 :         rc = spdk_nvmf_subsystem_pause(subsystem, ctx->nsid, nvmf_rpc_remove_ns_paused, ctx);
    1826           0 :         if (rc != 0) {
    1827           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, "Internal error");
    1828           0 :                 nvmf_rpc_remove_ns_ctx_free(ctx);
    1829           0 :         }
    1830           0 : }
    1831           0 : SPDK_RPC_REGISTER("nvmf_subsystem_remove_ns", rpc_nvmf_subsystem_remove_ns, SPDK_RPC_RUNTIME)
    1832             : 
    1833             : struct nvmf_rpc_ns_visible_ctx {
    1834             :         struct spdk_jsonrpc_request *request;
    1835             :         char *nqn;
    1836             :         uint32_t nsid;
    1837             :         char *host;
    1838             :         char *tgt_name;
    1839             :         bool visible;
    1840             :         bool response_sent;
    1841             : };
    1842             : 
    1843             : static const struct spdk_json_object_decoder nvmf_rpc_ns_visible_decoder[] = {
    1844             :         {"nqn", offsetof(struct nvmf_rpc_ns_visible_ctx, nqn), spdk_json_decode_string},
    1845             :         {"nsid", offsetof(struct nvmf_rpc_ns_visible_ctx, nsid), spdk_json_decode_uint32},
    1846             :         {"host", offsetof(struct nvmf_rpc_ns_visible_ctx, host), spdk_json_decode_string},
    1847             :         {"tgt_name", offsetof(struct nvmf_rpc_ns_visible_ctx, tgt_name), spdk_json_decode_string, true},
    1848             : };
    1849             : 
    1850             : static void
    1851           0 : nvmf_rpc_ns_visible_ctx_free(struct nvmf_rpc_ns_visible_ctx *ctx)
    1852             : {
    1853           0 :         free(ctx->nqn);
    1854           0 :         free(ctx->host);
    1855           0 :         free(ctx->tgt_name);
    1856           0 :         free(ctx);
    1857           0 : }
    1858             : 
    1859             : static void
    1860           0 : nvmf_rpc_ns_visible_resumed(struct spdk_nvmf_subsystem *subsystem,
    1861             :                             void *cb_arg, int status)
    1862             : {
    1863           0 :         struct nvmf_rpc_ns_visible_ctx *ctx = cb_arg;
    1864           0 :         struct spdk_jsonrpc_request *request = ctx->request;
    1865           0 :         bool response_sent = ctx->response_sent;
    1866             : 
    1867           0 :         nvmf_rpc_ns_visible_ctx_free(ctx);
    1868             : 
    1869           0 :         if (!response_sent) {
    1870           0 :                 spdk_jsonrpc_send_bool_response(request, true);
    1871           0 :         }
    1872           0 : }
    1873             : 
    1874             : static void
    1875           0 : nvmf_rpc_ns_visible_paused(struct spdk_nvmf_subsystem *subsystem,
    1876             :                            void *cb_arg, int status)
    1877             : {
    1878           0 :         struct nvmf_rpc_ns_visible_ctx *ctx = cb_arg;
    1879             :         int ret;
    1880             : 
    1881           0 :         if (ctx->visible) {
    1882           0 :                 ret = spdk_nvmf_ns_add_host(subsystem, ctx->nsid, ctx->host, 0);
    1883           0 :         } else {
    1884           0 :                 ret = spdk_nvmf_ns_remove_host(subsystem, ctx->nsid, ctx->host, 0);
    1885             :         }
    1886           0 :         if (ret < 0) {
    1887           0 :                 SPDK_ERRLOG("Unable to add/remove %s to namespace ID %u\n", ctx->host, ctx->nsid);
    1888           0 :                 spdk_jsonrpc_send_error_response(ctx->request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
    1889             :                                                  "Invalid parameters");
    1890           0 :                 ctx->response_sent = true;
    1891           0 :         }
    1892             : 
    1893           0 :         if (spdk_nvmf_subsystem_resume(subsystem, nvmf_rpc_ns_visible_resumed, ctx)) {
    1894           0 :                 if (!ctx->response_sent) {
    1895           0 :                         spdk_jsonrpc_send_error_response(ctx->request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, "Internal error");
    1896           0 :                 }
    1897           0 :                 nvmf_rpc_ns_visible_ctx_free(ctx);
    1898           0 :         }
    1899           0 : }
    1900             : 
    1901             : static void
    1902           0 : nvmf_rpc_ns_visible(struct spdk_jsonrpc_request *request,
    1903             :                     const struct spdk_json_val *params,
    1904             :                     bool visible)
    1905             : {
    1906             :         struct nvmf_rpc_ns_visible_ctx *ctx;
    1907             :         struct spdk_nvmf_subsystem *subsystem;
    1908             :         struct spdk_nvmf_tgt *tgt;
    1909             :         int rc;
    1910             : 
    1911           0 :         ctx = calloc(1, sizeof(*ctx));
    1912           0 :         if (!ctx) {
    1913           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, "Out of memory");
    1914           0 :                 return;
    1915             :         }
    1916           0 :         ctx->visible = visible;
    1917             : 
    1918           0 :         if (spdk_json_decode_object(params, nvmf_rpc_ns_visible_decoder,
    1919             :                                     SPDK_COUNTOF(nvmf_rpc_ns_visible_decoder),
    1920           0 :                                     ctx)) {
    1921           0 :                 SPDK_ERRLOG("spdk_json_decode_object failed\n");
    1922           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters");
    1923           0 :                 nvmf_rpc_ns_visible_ctx_free(ctx);
    1924           0 :                 return;
    1925             :         }
    1926           0 :         ctx->request = request;
    1927             : 
    1928           0 :         tgt = spdk_nvmf_get_tgt(ctx->tgt_name);
    1929           0 :         if (!tgt) {
    1930           0 :                 SPDK_ERRLOG("Unable to find a target object.\n");
    1931           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    1932             :                                                  "Unable to find a target.");
    1933           0 :                 nvmf_rpc_ns_visible_ctx_free(ctx);
    1934           0 :                 return;
    1935             :         }
    1936             : 
    1937           0 :         subsystem = spdk_nvmf_tgt_find_subsystem(tgt, ctx->nqn);
    1938           0 :         if (!subsystem) {
    1939           0 :                 SPDK_ERRLOG("Unable to find subsystem with NQN %s\n", ctx->nqn);
    1940           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters");
    1941           0 :                 nvmf_rpc_ns_visible_ctx_free(ctx);
    1942           0 :                 return;
    1943             :         }
    1944             : 
    1945           0 :         rc = spdk_nvmf_subsystem_pause(subsystem, ctx->nsid, nvmf_rpc_ns_visible_paused, ctx);
    1946           0 :         if (rc != 0) {
    1947           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, "Internal error");
    1948           0 :                 nvmf_rpc_ns_visible_ctx_free(ctx);
    1949           0 :         }
    1950           0 : }
    1951             : 
    1952             : static void
    1953           0 : rpc_nvmf_ns_add_host(struct spdk_jsonrpc_request *request,
    1954             :                      const struct spdk_json_val *params)
    1955             : {
    1956           0 :         nvmf_rpc_ns_visible(request, params, true);
    1957           0 : }
    1958           0 : SPDK_RPC_REGISTER("nvmf_ns_add_host", rpc_nvmf_ns_add_host, SPDK_RPC_RUNTIME)
    1959             : 
    1960             : static void
    1961           0 : rpc_nvmf_ns_remove_host(struct spdk_jsonrpc_request *request,
    1962             :                         const struct spdk_json_val *params)
    1963             : {
    1964           0 :         nvmf_rpc_ns_visible(request, params, false);
    1965           0 : }
    1966           0 : SPDK_RPC_REGISTER("nvmf_ns_remove_host", rpc_nvmf_ns_remove_host, SPDK_RPC_RUNTIME)
    1967             : 
    1968             : struct nvmf_rpc_host_ctx {
    1969             :         struct spdk_jsonrpc_request *request;
    1970             :         char *nqn;
    1971             :         char *host;
    1972             :         char *tgt_name;
    1973             :         char *dhchap_key;
    1974             :         char *dhchap_ctrlr_key;
    1975             :         bool allow_any_host;
    1976             : };
    1977             : 
    1978             : static const struct spdk_json_object_decoder nvmf_rpc_subsystem_host_decoder[] = {
    1979             :         {"nqn", offsetof(struct nvmf_rpc_host_ctx, nqn), spdk_json_decode_string},
    1980             :         {"host", offsetof(struct nvmf_rpc_host_ctx, host), spdk_json_decode_string},
    1981             :         {"tgt_name", offsetof(struct nvmf_rpc_host_ctx, tgt_name), spdk_json_decode_string, true},
    1982             :         {"dhchap_key", offsetof(struct nvmf_rpc_host_ctx, dhchap_key), spdk_json_decode_string, true},
    1983             :         {"dhchap_ctrlr_key", offsetof(struct nvmf_rpc_host_ctx, dhchap_ctrlr_key), spdk_json_decode_string, true},
    1984             : };
    1985             : 
    1986             : static const struct spdk_json_object_decoder nvmf_rpc_referral_host_decoder[] = {
    1987             :         {"nqn", offsetof(struct nvmf_rpc_host_ctx, nqn), spdk_json_decode_string},
    1988             :         {"host", offsetof(struct nvmf_rpc_host_ctx, host), spdk_json_decode_string},
    1989             :         {"tgt_name", offsetof(struct nvmf_rpc_host_ctx, tgt_name), spdk_json_decode_string, true},
    1990             : };
    1991             : 
    1992             : static void
    1993           0 : nvmf_rpc_host_ctx_free(struct nvmf_rpc_host_ctx *ctx)
    1994             : {
    1995           0 :         free(ctx->nqn);
    1996           0 :         free(ctx->host);
    1997           0 :         free(ctx->tgt_name);
    1998           0 :         free(ctx->dhchap_key);
    1999           0 :         free(ctx->dhchap_ctrlr_key);
    2000           0 : }
    2001             : 
    2002             : static void
    2003           0 : rpc_nvmf_subsystem_add_host(struct spdk_jsonrpc_request *request,
    2004             :                             const struct spdk_json_val *params)
    2005             : {
    2006           0 :         struct nvmf_rpc_host_ctx ctx = {};
    2007             :         struct spdk_nvmf_subsystem *subsystem;
    2008           0 :         struct spdk_nvmf_host_opts opts = {};
    2009             :         struct spdk_nvmf_tgt *tgt;
    2010           0 :         struct spdk_key *key = NULL, *ckey = NULL;
    2011             :         int rc;
    2012             : 
    2013           0 :         if (spdk_json_decode_object_relaxed(params, nvmf_rpc_subsystem_host_decoder,
    2014             :                                             SPDK_COUNTOF(nvmf_rpc_subsystem_host_decoder),
    2015             :                                             &ctx)) {
    2016           0 :                 SPDK_ERRLOG("spdk_json_decode_object failed\n");
    2017           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters");
    2018           0 :                 goto out;
    2019             :         }
    2020             : 
    2021           0 :         tgt = spdk_nvmf_get_tgt(ctx.tgt_name);
    2022           0 :         if (!tgt) {
    2023           0 :                 SPDK_ERRLOG("Unable to find a target object.\n");
    2024           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    2025             :                                                  "Unable to find a target.");
    2026           0 :                 goto out;
    2027             :         }
    2028             : 
    2029           0 :         subsystem = spdk_nvmf_tgt_find_subsystem(tgt, ctx.nqn);
    2030           0 :         if (!subsystem) {
    2031           0 :                 SPDK_ERRLOG("Unable to find subsystem with NQN %s\n", ctx.nqn);
    2032           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters");
    2033           0 :                 goto out;
    2034             :         }
    2035             : 
    2036           0 :         if (ctx.dhchap_key != NULL) {
    2037           0 :                 key = spdk_keyring_get_key(ctx.dhchap_key);
    2038           0 :                 if (key == NULL) {
    2039           0 :                         SPDK_ERRLOG("Unable to find DH-HMAC-CHAP key: %s\n", ctx.dhchap_key);
    2040           0 :                         spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
    2041             :                                                          "Invalid parameters");
    2042           0 :                         goto out;
    2043             :                 }
    2044           0 :         }
    2045             : 
    2046           0 :         if (ctx.dhchap_ctrlr_key != NULL) {
    2047           0 :                 ckey = spdk_keyring_get_key(ctx.dhchap_ctrlr_key);
    2048           0 :                 if (ckey == NULL) {
    2049           0 :                         SPDK_ERRLOG("Unable to find DH-HMAC-CHAP ctrlr key: %s\n",
    2050             :                                     ctx.dhchap_ctrlr_key);
    2051           0 :                         spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
    2052             :                                                          "Invalid parameters");
    2053           0 :                         goto out;
    2054             :                 }
    2055           0 :         }
    2056             : 
    2057           0 :         opts.size = SPDK_SIZEOF(&opts, dhchap_ctrlr_key);
    2058           0 :         opts.params = params;
    2059           0 :         opts.dhchap_key = key;
    2060           0 :         opts.dhchap_ctrlr_key = ckey;
    2061           0 :         rc = spdk_nvmf_subsystem_add_host_ext(subsystem, ctx.host, &opts);
    2062           0 :         if (rc != 0) {
    2063           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, "Internal error");
    2064           0 :                 goto out;
    2065             :         }
    2066             : 
    2067           0 :         spdk_jsonrpc_send_bool_response(request, true);
    2068             : out:
    2069           0 :         spdk_keyring_put_key(ckey);
    2070           0 :         spdk_keyring_put_key(key);
    2071           0 :         nvmf_rpc_host_ctx_free(&ctx);
    2072           0 : }
    2073           0 : SPDK_RPC_REGISTER("nvmf_subsystem_add_host", rpc_nvmf_subsystem_add_host, SPDK_RPC_RUNTIME)
    2074             : 
    2075             : static void
    2076           0 : rpc_nvmf_subsystem_remove_host_done(void *_ctx, int status)
    2077             : {
    2078           0 :         struct nvmf_rpc_host_ctx *ctx = _ctx;
    2079             : 
    2080           0 :         spdk_jsonrpc_send_bool_response(ctx->request, true);
    2081           0 :         nvmf_rpc_host_ctx_free(ctx);
    2082           0 :         free(ctx);
    2083           0 : }
    2084             : 
    2085             : static void
    2086           0 : rpc_nvmf_subsystem_remove_host(struct spdk_jsonrpc_request *request,
    2087             :                                const struct spdk_json_val *params)
    2088             : {
    2089             :         struct nvmf_rpc_host_ctx *ctx;
    2090             :         struct spdk_nvmf_subsystem *subsystem;
    2091             :         struct spdk_nvmf_tgt *tgt;
    2092             :         int rc;
    2093             : 
    2094           0 :         ctx = calloc(1, sizeof(*ctx));
    2095           0 :         if (ctx == NULL) {
    2096           0 :                 SPDK_ERRLOG("Unable to allocate context to perform RPC\n");
    2097           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, "Out of memory");
    2098           0 :                 return;
    2099             :         }
    2100             : 
    2101           0 :         ctx->request = request;
    2102             : 
    2103           0 :         if (spdk_json_decode_object(params, nvmf_rpc_subsystem_host_decoder,
    2104             :                                     SPDK_COUNTOF(nvmf_rpc_subsystem_host_decoder),
    2105           0 :                                     ctx)) {
    2106           0 :                 SPDK_ERRLOG("spdk_json_decode_object failed\n");
    2107           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters");
    2108           0 :                 nvmf_rpc_host_ctx_free(ctx);
    2109           0 :                 free(ctx);
    2110           0 :                 return;
    2111             :         }
    2112             : 
    2113           0 :         tgt = spdk_nvmf_get_tgt(ctx->tgt_name);
    2114           0 :         if (!tgt) {
    2115           0 :                 SPDK_ERRLOG("Unable to find a target object.\n");
    2116           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    2117             :                                                  "Unable to find a target.");
    2118           0 :                 nvmf_rpc_host_ctx_free(ctx);
    2119           0 :                 free(ctx);
    2120           0 :                 return;
    2121             :         }
    2122             : 
    2123           0 :         subsystem = spdk_nvmf_tgt_find_subsystem(tgt, ctx->nqn);
    2124           0 :         if (!subsystem) {
    2125           0 :                 SPDK_ERRLOG("Unable to find subsystem with NQN %s\n", ctx->nqn);
    2126           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters");
    2127           0 :                 nvmf_rpc_host_ctx_free(ctx);
    2128           0 :                 free(ctx);
    2129           0 :                 return;
    2130             :         }
    2131             : 
    2132           0 :         rc = spdk_nvmf_subsystem_remove_host(subsystem, ctx->host);
    2133           0 :         if (rc != 0) {
    2134           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, "Internal error");
    2135           0 :                 nvmf_rpc_host_ctx_free(ctx);
    2136           0 :                 free(ctx);
    2137           0 :                 return;
    2138             :         }
    2139             : 
    2140           0 :         rc = spdk_nvmf_subsystem_disconnect_host(subsystem, ctx->host,
    2141             :                         rpc_nvmf_subsystem_remove_host_done,
    2142           0 :                         ctx);
    2143           0 :         if (rc != 0) {
    2144           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, "Internal error");
    2145           0 :                 nvmf_rpc_host_ctx_free(ctx);
    2146           0 :                 free(ctx);
    2147           0 :                 return;
    2148             :         }
    2149           0 : }
    2150           0 : SPDK_RPC_REGISTER("nvmf_subsystem_remove_host", rpc_nvmf_subsystem_remove_host,
    2151             :                   SPDK_RPC_RUNTIME)
    2152             : 
    2153             : static void
    2154           0 : rpc_nvmf_discovery_referral_add_host(struct spdk_jsonrpc_request *request,
    2155             :                                      const struct spdk_json_val *params)
    2156             : {
    2157           0 :         struct nvmf_rpc_host_ctx ctx = {};
    2158             :         struct spdk_nvmf_referral *referral;
    2159           0 :         struct spdk_nvmf_host_opts opts = {};
    2160             :         struct spdk_nvmf_tgt *tgt;
    2161           0 :         struct spdk_key *key = NULL, *ckey = NULL;
    2162             :         int rc;
    2163             : 
    2164           0 :         if (spdk_json_decode_object_relaxed(params, nvmf_rpc_referral_host_decoder,
    2165             :                                             SPDK_COUNTOF(nvmf_rpc_referral_host_decoder),
    2166             :                                             &ctx)) {
    2167           0 :                 SPDK_ERRLOG("spdk_json_decode_object failed\n");
    2168           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters");
    2169           0 :                 goto out;
    2170             :         }
    2171             : 
    2172           0 :         tgt = spdk_nvmf_get_tgt(ctx.tgt_name);
    2173           0 :         if (!tgt) {
    2174           0 :                 SPDK_ERRLOG("Unable to find a target object.\n");
    2175           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    2176             :                                                  "Unable to find a target.");
    2177           0 :                 goto out;
    2178             :         }
    2179             : 
    2180           0 :         referral = spdk_nvmf_tgt_find_referral(tgt, ctx.nqn);
    2181           0 :         if (!referral) {
    2182           0 :                 SPDK_ERRLOG("Unable to find referral with NQN %s\n", ctx.nqn);
    2183           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters");
    2184           0 :                 goto out;
    2185             :         }
    2186             : 
    2187           0 :         if (ctx.dhchap_key != NULL) {
    2188           0 :                 key = spdk_keyring_get_key(ctx.dhchap_key);
    2189           0 :                 if (key == NULL) {
    2190           0 :                         SPDK_ERRLOG("Unable to find DH-HMAC-CHAP key: %s\n", ctx.dhchap_key);
    2191           0 :                         spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
    2192             :                                                          "Invalid parameters");
    2193           0 :                         goto out;
    2194             :                 }
    2195           0 :         }
    2196           0 :         if (ctx.dhchap_ctrlr_key != NULL) {
    2197           0 :                 ckey = spdk_keyring_get_key(ctx.dhchap_ctrlr_key);
    2198           0 :                 if (ckey == NULL) {
    2199           0 :                         SPDK_ERRLOG("Unable to find DH-HMAC-CHAP ctrlr key: %s\n",
    2200             :                                     ctx.dhchap_ctrlr_key);
    2201           0 :                         spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
    2202             :                                                          "Invalid parameters");
    2203           0 :                         goto out;
    2204             :                 }
    2205           0 :         }
    2206             : 
    2207           0 :         opts.size = SPDK_SIZEOF(&opts, dhchap_ctrlr_key);
    2208           0 :         opts.params = params;
    2209           0 :         opts.dhchap_key = key;
    2210           0 :         opts.dhchap_ctrlr_key = ckey;
    2211           0 :         rc = spdk_nvmf_discovery_referral_add_host(referral, ctx.host, &opts);
    2212           0 :         if (rc != 0) {
    2213           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, "Internal error");
    2214           0 :                 goto out;
    2215             :         }
    2216             : 
    2217           0 :         spdk_jsonrpc_send_bool_response(request, true);
    2218             : out:
    2219           0 :         spdk_keyring_put_key(ckey);
    2220           0 :         spdk_keyring_put_key(key);
    2221           0 :         nvmf_rpc_host_ctx_free(&ctx);
    2222           0 : }
    2223           0 : SPDK_RPC_REGISTER("nvmf_discovery_referral_add_host", rpc_nvmf_discovery_referral_add_host,
    2224             :                   SPDK_RPC_RUNTIME)
    2225             : 
    2226             : static void
    2227           0 : rpc_nvmf_discovery_referral_remove_host(struct spdk_jsonrpc_request *request,
    2228             :                                         const struct spdk_json_val *params)
    2229             : {
    2230             :         struct nvmf_rpc_host_ctx *ctx;
    2231             :         struct spdk_nvmf_referral *referral;
    2232             :         struct spdk_nvmf_tgt *tgt;
    2233             :         int rc;
    2234             : 
    2235           0 :         ctx = calloc(1, sizeof(*ctx));
    2236           0 :         if (ctx == NULL) {
    2237           0 :                 SPDK_ERRLOG("Unable to allocate context to perform RPC\n");
    2238           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, "Out of memory");
    2239           0 :                 return;
    2240             :         }
    2241             : 
    2242           0 :         ctx->request = request;
    2243             : 
    2244           0 :         if (spdk_json_decode_object(params, nvmf_rpc_referral_host_decoder,
    2245             :                                     SPDK_COUNTOF(nvmf_rpc_referral_host_decoder),
    2246           0 :                                     ctx)) {
    2247           0 :                 SPDK_ERRLOG("spdk_json_decode_object failed\n");
    2248           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters");
    2249           0 :                 nvmf_rpc_host_ctx_free(ctx);
    2250           0 :                 free(ctx);
    2251           0 :                 return;
    2252             :         }
    2253             : 
    2254           0 :         tgt = spdk_nvmf_get_tgt(ctx->tgt_name);
    2255           0 :         if (!tgt) {
    2256           0 :                 SPDK_ERRLOG("Unable to find a target object.\n");
    2257           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    2258             :                                                  "Unable to find a target.");
    2259           0 :                 nvmf_rpc_host_ctx_free(ctx);
    2260           0 :                 free(ctx);
    2261           0 :                 return;
    2262             :         }
    2263             : 
    2264           0 :         referral = spdk_nvmf_tgt_find_referral(tgt, ctx->nqn);
    2265           0 :         if (!referral) {
    2266           0 :                 SPDK_ERRLOG("Unable to find referral with NQN %s\n", ctx->nqn);
    2267           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters");
    2268           0 :                 nvmf_rpc_host_ctx_free(ctx);
    2269           0 :                 free(ctx);
    2270           0 :                 return;
    2271             :         }
    2272             : 
    2273           0 :         rc = spdk_nvmf_discovery_referral_remove_host(referral, ctx->host);
    2274           0 :         if (rc != 0) {
    2275           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, "Internal error");
    2276           0 :                 nvmf_rpc_host_ctx_free(ctx);
    2277           0 :                 free(ctx);
    2278           0 :                 return;
    2279             :         }
    2280           0 :         spdk_jsonrpc_send_bool_response(ctx->request, true);
    2281           0 :         nvmf_rpc_host_ctx_free(ctx);
    2282           0 :         free(ctx);
    2283             : 
    2284           0 : }
    2285           0 : SPDK_RPC_REGISTER("nvmf_discovery_referral_remove_host", rpc_nvmf_discovery_referral_remove_host,
    2286             :                   SPDK_RPC_RUNTIME)
    2287             : 
    2288             : static void
    2289           0 : rpc_nvmf_subsystem_set_keys(struct spdk_jsonrpc_request *request,
    2290             :                             const struct spdk_json_val *params)
    2291             : {
    2292           0 :         struct nvmf_rpc_host_ctx ctx = {};
    2293             :         struct spdk_nvmf_subsystem *subsystem;
    2294           0 :         struct spdk_nvmf_subsystem_key_opts opts = {};
    2295             :         struct spdk_nvmf_tgt *tgt;
    2296           0 :         struct spdk_key *key = NULL, *ckey = NULL;
    2297             :         int rc;
    2298             : 
    2299           0 :         if (spdk_json_decode_object(params, nvmf_rpc_subsystem_host_decoder,
    2300             :                                     SPDK_COUNTOF(nvmf_rpc_subsystem_host_decoder), &ctx)) {
    2301           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
    2302             :                                                  "Invalid parameters");
    2303           0 :                 goto out;
    2304             :         }
    2305             : 
    2306           0 :         tgt = spdk_nvmf_get_tgt(ctx.tgt_name);
    2307           0 :         if (!tgt) {
    2308           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    2309             :                                                  "Invalid parameters");
    2310           0 :                 goto out;
    2311             :         }
    2312           0 :         subsystem = spdk_nvmf_tgt_find_subsystem(tgt, ctx.nqn);
    2313           0 :         if (!subsystem) {
    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_key != NULL) {
    2320           0 :                 key = spdk_keyring_get_key(ctx.dhchap_key);
    2321           0 :                 if (key == NULL) {
    2322           0 :                         SPDK_ERRLOG("Unable to find DH-HMAC-CHAP key: %s\n", ctx.dhchap_key);
    2323           0 :                         spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
    2324             :                                                          "Invalid parameters");
    2325           0 :                         goto out;
    2326             :                 }
    2327           0 :         }
    2328           0 :         if (ctx.dhchap_ctrlr_key != NULL) {
    2329           0 :                 ckey = spdk_keyring_get_key(ctx.dhchap_ctrlr_key);
    2330           0 :                 if (ckey == NULL) {
    2331           0 :                         SPDK_ERRLOG("Unable to find DH-HMAC-CHAP ctrlr key: %s\n",
    2332             :                                     ctx.dhchap_ctrlr_key);
    2333           0 :                         spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
    2334             :                                                          "Invalid parameters");
    2335           0 :                         goto out;
    2336             :                 }
    2337           0 :         }
    2338             : 
    2339           0 :         opts.size = SPDK_SIZEOF(&opts, dhchap_ctrlr_key);
    2340           0 :         opts.dhchap_key = key;
    2341           0 :         opts.dhchap_ctrlr_key = ckey;
    2342           0 :         rc = spdk_nvmf_subsystem_set_keys(subsystem, ctx.host, &opts);
    2343           0 :         if (rc != 0) {
    2344           0 :                 spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
    2345           0 :                 goto out;
    2346             :         }
    2347             : 
    2348           0 :         spdk_jsonrpc_send_bool_response(request, true);
    2349             : out:
    2350           0 :         spdk_keyring_put_key(ckey);
    2351           0 :         spdk_keyring_put_key(key);
    2352           0 :         nvmf_rpc_host_ctx_free(&ctx);
    2353           0 : }
    2354           0 : SPDK_RPC_REGISTER("nvmf_subsystem_set_keys", rpc_nvmf_subsystem_set_keys, SPDK_RPC_RUNTIME)
    2355             : 
    2356             : static const struct spdk_json_object_decoder nvmf_rpc_subsystem_any_host_decoder[] = {
    2357             :         {"nqn", offsetof(struct nvmf_rpc_host_ctx, nqn), spdk_json_decode_string},
    2358             :         {"allow_any_host", offsetof(struct nvmf_rpc_host_ctx, allow_any_host), spdk_json_decode_bool},
    2359             :         {"tgt_name", offsetof(struct nvmf_rpc_host_ctx, tgt_name), spdk_json_decode_string, true},
    2360             : };
    2361             : 
    2362             : static void
    2363           0 : rpc_nvmf_subsystem_allow_any_host(struct spdk_jsonrpc_request *request,
    2364             :                                   const struct spdk_json_val *params)
    2365             : {
    2366           0 :         struct nvmf_rpc_host_ctx ctx = {};
    2367             :         struct spdk_nvmf_subsystem *subsystem;
    2368             :         struct spdk_nvmf_tgt *tgt;
    2369             :         int rc;
    2370             : 
    2371           0 :         if (spdk_json_decode_object(params, nvmf_rpc_subsystem_any_host_decoder,
    2372             :                                     SPDK_COUNTOF(nvmf_rpc_subsystem_any_host_decoder),
    2373             :                                     &ctx)) {
    2374           0 :                 SPDK_ERRLOG("spdk_json_decode_object failed\n");
    2375           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters");
    2376           0 :                 nvmf_rpc_host_ctx_free(&ctx);
    2377           0 :                 return;
    2378             :         }
    2379             : 
    2380           0 :         tgt = spdk_nvmf_get_tgt(ctx.tgt_name);
    2381           0 :         if (!tgt) {
    2382           0 :                 SPDK_ERRLOG("Unable to find a target object.\n");
    2383           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    2384             :                                                  "Unable to find a target.");
    2385           0 :                 nvmf_rpc_host_ctx_free(&ctx);
    2386           0 :                 return;
    2387             :         }
    2388             : 
    2389           0 :         subsystem = spdk_nvmf_tgt_find_subsystem(tgt, ctx.nqn);
    2390           0 :         if (!subsystem) {
    2391           0 :                 SPDK_ERRLOG("Unable to find subsystem with NQN %s\n", ctx.nqn);
    2392           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters");
    2393           0 :                 nvmf_rpc_host_ctx_free(&ctx);
    2394           0 :                 return;
    2395             :         }
    2396             : 
    2397           0 :         rc = spdk_nvmf_subsystem_set_allow_any_host(subsystem, ctx.allow_any_host);
    2398           0 :         if (rc != 0) {
    2399           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, "Internal error");
    2400           0 :                 nvmf_rpc_host_ctx_free(&ctx);
    2401           0 :                 return;
    2402             :         }
    2403             : 
    2404           0 :         spdk_jsonrpc_send_bool_response(request, true);
    2405           0 :         nvmf_rpc_host_ctx_free(&ctx);
    2406           0 : }
    2407           0 : SPDK_RPC_REGISTER("nvmf_subsystem_allow_any_host", rpc_nvmf_subsystem_allow_any_host,
    2408             :                   SPDK_RPC_RUNTIME)
    2409             : 
    2410             : struct nvmf_rpc_target_ctx {
    2411             :         char *name;
    2412             :         uint32_t max_subsystems;
    2413             :         char *discovery_filter;
    2414             : };
    2415             : 
    2416             : static int
    2417           0 : decode_discovery_filter(const struct spdk_json_val *val, void *out)
    2418             : {
    2419           0 :         uint32_t *_filter = out;
    2420           0 :         uint32_t filter = SPDK_NVMF_TGT_DISCOVERY_MATCH_ANY;
    2421           0 :         char *tokens = spdk_json_strdup(val);
    2422             :         char *tok;
    2423           0 :         char *sp = NULL;
    2424           0 :         int rc = -EINVAL;
    2425           0 :         bool all_specified = false;
    2426             : 
    2427           0 :         if (!tokens) {
    2428           0 :                 return -ENOMEM;
    2429             :         }
    2430             : 
    2431           0 :         tok = strtok_r(tokens, ",", &sp);
    2432           0 :         while (tok) {
    2433           0 :                 if (strncmp(tok, "match_any", 9) == 0) {
    2434           0 :                         if (filter != SPDK_NVMF_TGT_DISCOVERY_MATCH_ANY) {
    2435           0 :                                 goto out;
    2436             :                         }
    2437           0 :                         filter = SPDK_NVMF_TGT_DISCOVERY_MATCH_ANY;
    2438           0 :                         all_specified = true;
    2439           0 :                 } else {
    2440           0 :                         if (all_specified) {
    2441           0 :                                 goto out;
    2442             :                         }
    2443           0 :                         if (strncmp(tok, "transport", 9) == 0) {
    2444           0 :                                 filter |= SPDK_NVMF_TGT_DISCOVERY_MATCH_TRANSPORT_TYPE;
    2445           0 :                         } else if (strncmp(tok, "address", 7) == 0) {
    2446           0 :                                 filter |= SPDK_NVMF_TGT_DISCOVERY_MATCH_TRANSPORT_ADDRESS;
    2447           0 :                         } else if (strncmp(tok, "svcid", 5) == 0) {
    2448           0 :                                 filter |= SPDK_NVMF_TGT_DISCOVERY_MATCH_TRANSPORT_SVCID;
    2449           0 :                         } else {
    2450           0 :                                 SPDK_ERRLOG("Invalid value %s\n", tok);
    2451           0 :                                 goto out;
    2452             :                         }
    2453             :                 }
    2454             : 
    2455           0 :                 tok = strtok_r(NULL, ",", &sp);
    2456             :         }
    2457             : 
    2458           0 :         rc = 0;
    2459           0 :         *_filter = filter;
    2460             : 
    2461             : out:
    2462           0 :         free(tokens);
    2463             : 
    2464           0 :         return rc;
    2465           0 : }
    2466             : 
    2467             : static const struct spdk_json_object_decoder nvmf_rpc_create_target_decoder[] = {
    2468             :         {"name", offsetof(struct nvmf_rpc_target_ctx, name), spdk_json_decode_string},
    2469             :         {"max_subsystems", offsetof(struct nvmf_rpc_target_ctx, max_subsystems), spdk_json_decode_uint32, true},
    2470             :         {"discovery_filter", offsetof(struct nvmf_rpc_target_ctx, discovery_filter), decode_discovery_filter, true}
    2471             : };
    2472             : 
    2473             : static void
    2474           0 : rpc_nvmf_create_target(struct spdk_jsonrpc_request *request,
    2475             :                        const struct spdk_json_val *params)
    2476             : {
    2477             :         struct spdk_nvmf_target_opts    opts;
    2478           0 :         struct nvmf_rpc_target_ctx      ctx = {0};
    2479             :         struct spdk_nvmf_tgt            *tgt;
    2480             :         struct spdk_json_write_ctx      *w;
    2481             : 
    2482             :         /* Decode parameters the first time to get the transport type */
    2483           0 :         if (spdk_json_decode_object(params, nvmf_rpc_create_target_decoder,
    2484             :                                     SPDK_COUNTOF(nvmf_rpc_create_target_decoder),
    2485             :                                     &ctx)) {
    2486           0 :                 SPDK_ERRLOG("spdk_json_decode_object failed\n");
    2487           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters");
    2488           0 :                 goto out;
    2489             :         }
    2490             : 
    2491           0 :         snprintf(opts.name, NVMF_TGT_NAME_MAX_LENGTH, "%s", ctx.name);
    2492           0 :         opts.max_subsystems = ctx.max_subsystems;
    2493           0 :         opts.size = SPDK_SIZEOF(&opts, discovery_filter);
    2494             : 
    2495           0 :         if (spdk_nvmf_get_tgt(opts.name) != NULL) {
    2496           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
    2497             :                                                  "Target already exists.");
    2498           0 :                 goto out;
    2499             :         }
    2500             : 
    2501           0 :         tgt = spdk_nvmf_tgt_create(&opts);
    2502             : 
    2503           0 :         if (tgt == NULL) {
    2504           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    2505             :                                                  "Unable to create the requested target.");
    2506           0 :                 goto out;
    2507             :         }
    2508             : 
    2509           0 :         w = spdk_jsonrpc_begin_result(request);
    2510           0 :         spdk_json_write_string(w, spdk_nvmf_tgt_get_name(tgt));
    2511           0 :         spdk_jsonrpc_end_result(request, w);
    2512             : out:
    2513           0 :         free(ctx.name);
    2514           0 :         free(ctx.discovery_filter);
    2515           0 : }
    2516           0 : /* private */ SPDK_RPC_REGISTER("nvmf_create_target", rpc_nvmf_create_target, SPDK_RPC_RUNTIME);
    2517             : 
    2518             : static const struct spdk_json_object_decoder nvmf_rpc_destroy_target_decoder[] = {
    2519             :         {"name", offsetof(struct nvmf_rpc_target_ctx, name), spdk_json_decode_string},
    2520             : };
    2521             : 
    2522             : static void
    2523           0 : nvmf_rpc_destroy_target_done(void *ctx, int status)
    2524             : {
    2525           0 :         struct spdk_jsonrpc_request     *request = ctx;
    2526             : 
    2527           0 :         spdk_jsonrpc_send_bool_response(request, true);
    2528           0 : }
    2529             : 
    2530             : static void
    2531           0 : rpc_nvmf_delete_target(struct spdk_jsonrpc_request *request,
    2532             :                        const struct spdk_json_val *params)
    2533             : {
    2534           0 :         struct nvmf_rpc_target_ctx      ctx = {0};
    2535             :         struct spdk_nvmf_tgt            *tgt;
    2536             : 
    2537             :         /* Decode parameters the first time to get the transport type */
    2538           0 :         if (spdk_json_decode_object(params, nvmf_rpc_destroy_target_decoder,
    2539             :                                     SPDK_COUNTOF(nvmf_rpc_destroy_target_decoder),
    2540             :                                     &ctx)) {
    2541           0 :                 SPDK_ERRLOG("spdk_json_decode_object failed\n");
    2542           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters");
    2543           0 :                 free(ctx.name);
    2544           0 :                 return;
    2545             :         }
    2546             : 
    2547           0 :         tgt = spdk_nvmf_get_tgt(ctx.name);
    2548             : 
    2549           0 :         if (tgt == NULL) {
    2550           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
    2551             :                                                  "The specified target doesn't exist, cannot delete it.");
    2552           0 :                 free(ctx.name);
    2553           0 :                 return;
    2554             :         }
    2555             : 
    2556           0 :         spdk_nvmf_tgt_destroy(tgt, nvmf_rpc_destroy_target_done, request);
    2557           0 :         free(ctx.name);
    2558           0 : }
    2559           0 : /* private */ SPDK_RPC_REGISTER("nvmf_delete_target", rpc_nvmf_delete_target, SPDK_RPC_RUNTIME);
    2560             : 
    2561             : static void
    2562           0 : rpc_nvmf_get_targets(struct spdk_jsonrpc_request *request,
    2563             :                      const struct spdk_json_val *params)
    2564             : {
    2565             :         struct spdk_json_write_ctx      *w;
    2566             :         struct spdk_nvmf_tgt            *tgt;
    2567             :         const char                      *name;
    2568             : 
    2569           0 :         if (params != NULL) {
    2570           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
    2571             :                                                  "nvmf_get_targets has no parameters.");
    2572           0 :                 return;
    2573             :         }
    2574             : 
    2575           0 :         w = spdk_jsonrpc_begin_result(request);
    2576           0 :         spdk_json_write_array_begin(w);
    2577             : 
    2578           0 :         tgt = spdk_nvmf_get_first_tgt();
    2579             : 
    2580           0 :         while (tgt != NULL) {
    2581           0 :                 name = spdk_nvmf_tgt_get_name(tgt);
    2582           0 :                 spdk_json_write_string(w, name);
    2583           0 :                 tgt = spdk_nvmf_get_next_tgt(tgt);
    2584             :         }
    2585             : 
    2586           0 :         spdk_json_write_array_end(w);
    2587           0 :         spdk_jsonrpc_end_result(request, w);
    2588           0 : }
    2589           0 : /* private */ SPDK_RPC_REGISTER("nvmf_get_targets", rpc_nvmf_get_targets, SPDK_RPC_RUNTIME);
    2590             : 
    2591             : struct nvmf_rpc_create_transport_ctx {
    2592             :         char                            *trtype;
    2593             :         char                            *tgt_name;
    2594             :         struct spdk_nvmf_transport_opts opts;
    2595             :         struct spdk_jsonrpc_request     *request;
    2596             :         struct spdk_nvmf_transport      *transport;
    2597             :         int                             status;
    2598             : };
    2599             : 
    2600             : /**
    2601             :  * `max_qpairs_per_ctrlr` represents both admin and IO qpairs, that confuses
    2602             :  * users when they configure a transport using RPC. So it was decided to
    2603             :  * deprecate `max_qpairs_per_ctrlr` RPC parameter and use `max_io_qpairs_per_ctrlr`
    2604             :  * But internal logic remains unchanged and SPDK expects that
    2605             :  * spdk_nvmf_transport_opts::max_qpairs_per_ctrlr includes an admin qpair.
    2606             :  * This function parses the number of IO qpairs and adds +1 for admin qpair.
    2607             :  */
    2608             : static int
    2609           0 : nvmf_rpc_decode_max_io_qpairs(const struct spdk_json_val *val, void *out)
    2610             : {
    2611           0 :         uint16_t *i = out;
    2612             :         int rc;
    2613             : 
    2614           0 :         rc = spdk_json_number_to_uint16(val, i);
    2615           0 :         if (rc == 0) {
    2616           0 :                 (*i)++;
    2617           0 :         }
    2618             : 
    2619           0 :         return rc;
    2620             : }
    2621             : 
    2622             : static const struct spdk_json_object_decoder nvmf_rpc_create_transport_decoder[] = {
    2623             :         {       "trtype", offsetof(struct nvmf_rpc_create_transport_ctx, trtype), spdk_json_decode_string},
    2624             :         {
    2625             :                 "max_queue_depth", offsetof(struct nvmf_rpc_create_transport_ctx, opts.max_queue_depth),
    2626             :                 spdk_json_decode_uint16, true
    2627             :         },
    2628             :         {
    2629             :                 "max_io_qpairs_per_ctrlr", offsetof(struct nvmf_rpc_create_transport_ctx, opts.max_qpairs_per_ctrlr),
    2630             :                 nvmf_rpc_decode_max_io_qpairs, true
    2631             :         },
    2632             :         {
    2633             :                 "in_capsule_data_size", offsetof(struct nvmf_rpc_create_transport_ctx, opts.in_capsule_data_size),
    2634             :                 spdk_json_decode_uint32, true
    2635             :         },
    2636             :         {
    2637             :                 "max_io_size", offsetof(struct nvmf_rpc_create_transport_ctx, opts.max_io_size),
    2638             :                 spdk_json_decode_uint32, true
    2639             :         },
    2640             :         {
    2641             :                 "io_unit_size", offsetof(struct nvmf_rpc_create_transport_ctx, opts.io_unit_size),
    2642             :                 spdk_json_decode_uint32, true
    2643             :         },
    2644             :         {
    2645             :                 "max_aq_depth", offsetof(struct nvmf_rpc_create_transport_ctx, opts.max_aq_depth),
    2646             :                 spdk_json_decode_uint32, true
    2647             :         },
    2648             :         {
    2649             :                 "num_shared_buffers", offsetof(struct nvmf_rpc_create_transport_ctx, opts.num_shared_buffers),
    2650             :                 spdk_json_decode_uint32, true
    2651             :         },
    2652             :         {
    2653             :                 "buf_cache_size", offsetof(struct nvmf_rpc_create_transport_ctx, opts.buf_cache_size),
    2654             :                 spdk_json_decode_uint32, true
    2655             :         },
    2656             :         {
    2657             :                 "dif_insert_or_strip", offsetof(struct nvmf_rpc_create_transport_ctx, opts.dif_insert_or_strip),
    2658             :                 spdk_json_decode_bool, true
    2659             :         },
    2660             :         {
    2661             :                 "abort_timeout_sec", offsetof(struct nvmf_rpc_create_transport_ctx, opts.abort_timeout_sec),
    2662             :                 spdk_json_decode_uint32, true
    2663             :         },
    2664             :         {
    2665             :                 "zcopy", offsetof(struct nvmf_rpc_create_transport_ctx, opts.zcopy),
    2666             :                 spdk_json_decode_bool, true
    2667             :         },
    2668             :         {
    2669             :                 "tgt_name", offsetof(struct nvmf_rpc_create_transport_ctx, tgt_name),
    2670             :                 spdk_json_decode_string, true
    2671             :         },
    2672             :         {
    2673             :                 "acceptor_poll_rate", offsetof(struct nvmf_rpc_create_transport_ctx, opts.acceptor_poll_rate),
    2674             :                 spdk_json_decode_uint32, true
    2675             :         },
    2676             :         {
    2677             :                 "ack_timeout", offsetof(struct nvmf_rpc_create_transport_ctx, opts.ack_timeout),
    2678             :                 spdk_json_decode_uint32, true
    2679             :         },
    2680             :         {
    2681             :                 "data_wr_pool_size", offsetof(struct nvmf_rpc_create_transport_ctx, opts.data_wr_pool_size),
    2682             :                 spdk_json_decode_uint32, true
    2683             :         },
    2684             :         {
    2685             :                 "disable_command_passthru", offsetof(struct nvmf_rpc_create_transport_ctx, opts.disable_command_passthru),
    2686             :                 spdk_json_decode_bool, true
    2687             :         }
    2688             : };
    2689             : 
    2690             : static void
    2691           0 : nvmf_rpc_create_transport_ctx_free(struct nvmf_rpc_create_transport_ctx *ctx)
    2692             : {
    2693           0 :         free(ctx->trtype);
    2694           0 :         free(ctx->tgt_name);
    2695           0 :         free(ctx);
    2696           0 : }
    2697             : 
    2698             : static void
    2699           0 : nvmf_rpc_transport_destroy_done_cb(void *cb_arg)
    2700             : {
    2701           0 :         struct nvmf_rpc_create_transport_ctx *ctx = cb_arg;
    2702             : 
    2703           0 :         spdk_jsonrpc_send_error_response_fmt(ctx->request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    2704           0 :                                              "Failed to add transport to tgt.(%d)", ctx->status);
    2705           0 :         nvmf_rpc_create_transport_ctx_free(ctx);
    2706           0 : }
    2707             : 
    2708             : static void
    2709           0 : nvmf_rpc_tgt_add_transport_done(void *cb_arg, int status)
    2710             : {
    2711           0 :         struct nvmf_rpc_create_transport_ctx *ctx = cb_arg;
    2712             : 
    2713           0 :         if (status) {
    2714           0 :                 SPDK_ERRLOG("Failed to add transport to tgt.(%d)\n", status);
    2715           0 :                 ctx->status = status;
    2716           0 :                 spdk_nvmf_transport_destroy(ctx->transport, nvmf_rpc_transport_destroy_done_cb, ctx);
    2717           0 :                 return;
    2718             :         }
    2719             : 
    2720           0 :         spdk_jsonrpc_send_bool_response(ctx->request, true);
    2721           0 :         nvmf_rpc_create_transport_ctx_free(ctx);
    2722           0 : }
    2723             : 
    2724             : static void
    2725           0 : nvmf_rpc_create_transport_done(void *cb_arg, struct spdk_nvmf_transport *transport)
    2726             : {
    2727           0 :         struct nvmf_rpc_create_transport_ctx *ctx = cb_arg;
    2728             : 
    2729           0 :         if (!transport) {
    2730           0 :                 SPDK_ERRLOG("Failed to create transport.\n");
    2731           0 :                 spdk_jsonrpc_send_error_response(ctx->request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    2732             :                                                  "Failed to create transport.");
    2733           0 :                 nvmf_rpc_create_transport_ctx_free(ctx);
    2734           0 :                 return;
    2735             :         }
    2736             : 
    2737           0 :         ctx->transport = transport;
    2738             : 
    2739           0 :         spdk_nvmf_tgt_add_transport(spdk_nvmf_get_tgt(ctx->tgt_name), transport,
    2740           0 :                                     nvmf_rpc_tgt_add_transport_done, ctx);
    2741           0 : }
    2742             : 
    2743             : static void
    2744           0 : rpc_nvmf_create_transport(struct spdk_jsonrpc_request *request,
    2745             :                           const struct spdk_json_val *params)
    2746             : {
    2747             :         struct nvmf_rpc_create_transport_ctx *ctx;
    2748             :         struct spdk_nvmf_tgt *tgt;
    2749             :         int rc;
    2750             : 
    2751           0 :         ctx = calloc(1, sizeof(*ctx));
    2752           0 :         if (!ctx) {
    2753           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, "Out of memory");
    2754           0 :                 return;
    2755             :         }
    2756             : 
    2757             :         /* Decode parameters the first time to get the transport type */
    2758           0 :         if (spdk_json_decode_object_relaxed(params, nvmf_rpc_create_transport_decoder,
    2759             :                                             SPDK_COUNTOF(nvmf_rpc_create_transport_decoder),
    2760           0 :                                             ctx)) {
    2761           0 :                 SPDK_ERRLOG("spdk_json_decode_object_relaxed failed\n");
    2762           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters");
    2763           0 :                 nvmf_rpc_create_transport_ctx_free(ctx);
    2764           0 :                 return;
    2765             :         }
    2766             : 
    2767           0 :         tgt = spdk_nvmf_get_tgt(ctx->tgt_name);
    2768           0 :         if (!tgt) {
    2769           0 :                 SPDK_ERRLOG("Unable to find a target object.\n");
    2770           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    2771             :                                                  "Unable to find a target.");
    2772           0 :                 nvmf_rpc_create_transport_ctx_free(ctx);
    2773           0 :                 return;
    2774             :         }
    2775             : 
    2776             :         /* Initialize all the transport options (based on transport type) and decode the
    2777             :          * parameters again to update any options passed in rpc create transport call.
    2778             :          */
    2779           0 :         if (!spdk_nvmf_transport_opts_init(ctx->trtype, &ctx->opts, sizeof(ctx->opts))) {
    2780             :                 /* This can happen if user specifies PCIE transport type which isn't valid for
    2781             :                  * NVMe-oF.
    2782             :                  */
    2783           0 :                 SPDK_ERRLOG("Invalid transport type '%s'\n", ctx->trtype);
    2784           0 :                 spdk_jsonrpc_send_error_response_fmt(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
    2785           0 :                                                      "Invalid transport type '%s'", ctx->trtype);
    2786           0 :                 nvmf_rpc_create_transport_ctx_free(ctx);
    2787           0 :                 return;
    2788             :         }
    2789             : 
    2790           0 :         if (spdk_json_decode_object_relaxed(params, nvmf_rpc_create_transport_decoder,
    2791             :                                             SPDK_COUNTOF(nvmf_rpc_create_transport_decoder),
    2792           0 :                                             ctx)) {
    2793           0 :                 SPDK_ERRLOG("spdk_json_decode_object_relaxed failed\n");
    2794           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters");
    2795           0 :                 nvmf_rpc_create_transport_ctx_free(ctx);
    2796           0 :                 return;
    2797             :         }
    2798             : 
    2799           0 :         if (spdk_nvmf_tgt_get_transport(tgt, ctx->trtype)) {
    2800           0 :                 SPDK_ERRLOG("Transport type '%s' already exists\n", ctx->trtype);
    2801           0 :                 spdk_jsonrpc_send_error_response_fmt(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    2802           0 :                                                      "Transport type '%s' already exists", ctx->trtype);
    2803           0 :                 nvmf_rpc_create_transport_ctx_free(ctx);
    2804           0 :                 return;
    2805             :         }
    2806             : 
    2807             :         /* Transport can parse additional params themselves */
    2808           0 :         ctx->opts.transport_specific = params;
    2809           0 :         ctx->request = request;
    2810             : 
    2811           0 :         rc = spdk_nvmf_transport_create_async(ctx->trtype, &ctx->opts, nvmf_rpc_create_transport_done, ctx);
    2812           0 :         if (rc) {
    2813           0 :                 SPDK_ERRLOG("Transport type '%s' create failed\n", ctx->trtype);
    2814           0 :                 spdk_jsonrpc_send_error_response_fmt(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    2815           0 :                                                      "Transport type '%s' create failed", ctx->trtype);
    2816           0 :                 nvmf_rpc_create_transport_ctx_free(ctx);
    2817           0 :         }
    2818           0 : }
    2819           0 : SPDK_RPC_REGISTER("nvmf_create_transport", rpc_nvmf_create_transport, SPDK_RPC_RUNTIME)
    2820             : 
    2821             : struct rpc_get_transport {
    2822             :         char *trtype;
    2823             :         char *tgt_name;
    2824             : };
    2825             : 
    2826             : static const struct spdk_json_object_decoder rpc_get_transport_decoders[] = {
    2827             :         {"trtype", offsetof(struct rpc_get_transport, trtype), spdk_json_decode_string, true},
    2828             :         {"tgt_name", offsetof(struct rpc_get_transport, tgt_name), spdk_json_decode_string, true},
    2829             : };
    2830             : 
    2831             : static void
    2832           0 : rpc_nvmf_get_transports(struct spdk_jsonrpc_request *request,
    2833             :                         const struct spdk_json_val *params)
    2834             : {
    2835           0 :         struct rpc_get_transport req = { 0 };
    2836             :         struct spdk_json_write_ctx *w;
    2837           0 :         struct spdk_nvmf_transport *transport = NULL;
    2838             :         struct spdk_nvmf_tgt *tgt;
    2839             : 
    2840           0 :         if (params) {
    2841           0 :                 if (spdk_json_decode_object(params, rpc_get_transport_decoders,
    2842             :                                             SPDK_COUNTOF(rpc_get_transport_decoders),
    2843             :                                             &req)) {
    2844           0 :                         SPDK_ERRLOG("spdk_json_decode_object failed\n");
    2845           0 :                         spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters");
    2846           0 :                         return;
    2847             :                 }
    2848           0 :         }
    2849             : 
    2850           0 :         tgt = spdk_nvmf_get_tgt(req.tgt_name);
    2851           0 :         if (!tgt) {
    2852           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    2853             :                                                  "Unable to find a target.");
    2854           0 :                 free(req.trtype);
    2855           0 :                 free(req.tgt_name);
    2856           0 :                 return;
    2857             :         }
    2858             : 
    2859           0 :         if (req.trtype) {
    2860           0 :                 transport = spdk_nvmf_tgt_get_transport(tgt, req.trtype);
    2861           0 :                 if (transport == NULL) {
    2862           0 :                         SPDK_ERRLOG("transport '%s' does not exist\n", req.trtype);
    2863           0 :                         spdk_jsonrpc_send_error_response(request, -ENODEV, spdk_strerror(ENODEV));
    2864           0 :                         free(req.trtype);
    2865           0 :                         free(req.tgt_name);
    2866           0 :                         return;
    2867             :                 }
    2868           0 :         }
    2869             : 
    2870           0 :         w = spdk_jsonrpc_begin_result(request);
    2871           0 :         spdk_json_write_array_begin(w);
    2872             : 
    2873           0 :         if (transport) {
    2874           0 :                 nvmf_transport_dump_opts(transport, w, false);
    2875           0 :         } else {
    2876           0 :                 for (transport = spdk_nvmf_transport_get_first(tgt); transport != NULL;
    2877           0 :                      transport = spdk_nvmf_transport_get_next(transport)) {
    2878           0 :                         nvmf_transport_dump_opts(transport, w, false);
    2879           0 :                 }
    2880             :         }
    2881             : 
    2882           0 :         spdk_json_write_array_end(w);
    2883           0 :         spdk_jsonrpc_end_result(request, w);
    2884           0 :         free(req.trtype);
    2885           0 :         free(req.tgt_name);
    2886           0 : }
    2887           0 : SPDK_RPC_REGISTER("nvmf_get_transports", rpc_nvmf_get_transports, SPDK_RPC_RUNTIME)
    2888             : 
    2889             : struct rpc_nvmf_get_stats_ctx {
    2890             :         char *tgt_name;
    2891             :         struct spdk_nvmf_tgt *tgt;
    2892             :         struct spdk_jsonrpc_request *request;
    2893             :         struct spdk_json_write_ctx *w;
    2894             : };
    2895             : 
    2896             : static const struct spdk_json_object_decoder rpc_get_stats_decoders[] = {
    2897             :         {"tgt_name", offsetof(struct rpc_nvmf_get_stats_ctx, tgt_name), spdk_json_decode_string, true},
    2898             : };
    2899             : 
    2900             : static void
    2901           0 : free_get_stats_ctx(struct rpc_nvmf_get_stats_ctx *ctx)
    2902             : {
    2903           0 :         free(ctx->tgt_name);
    2904           0 :         free(ctx);
    2905           0 : }
    2906             : 
    2907             : static void
    2908           0 : rpc_nvmf_get_stats_done(struct spdk_io_channel_iter *i, int status)
    2909             : {
    2910           0 :         struct rpc_nvmf_get_stats_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
    2911             : 
    2912           0 :         spdk_json_write_array_end(ctx->w);
    2913           0 :         spdk_json_write_object_end(ctx->w);
    2914           0 :         spdk_jsonrpc_end_result(ctx->request, ctx->w);
    2915           0 :         free_get_stats_ctx(ctx);
    2916           0 : }
    2917             : 
    2918             : static void
    2919           0 : _rpc_nvmf_get_stats(struct spdk_io_channel_iter *i)
    2920             : {
    2921           0 :         struct rpc_nvmf_get_stats_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
    2922             :         struct spdk_io_channel *ch;
    2923             :         struct spdk_nvmf_poll_group *group;
    2924             : 
    2925           0 :         ch = spdk_get_io_channel(ctx->tgt);
    2926           0 :         group = spdk_io_channel_get_ctx(ch);
    2927             : 
    2928           0 :         spdk_nvmf_poll_group_dump_stat(group, ctx->w);
    2929             : 
    2930           0 :         spdk_put_io_channel(ch);
    2931           0 :         spdk_for_each_channel_continue(i, 0);
    2932           0 : }
    2933             : 
    2934             : 
    2935             : static void
    2936           0 : rpc_nvmf_get_stats(struct spdk_jsonrpc_request *request,
    2937             :                    const struct spdk_json_val *params)
    2938             : {
    2939             :         struct rpc_nvmf_get_stats_ctx *ctx;
    2940             : 
    2941           0 :         ctx = calloc(1, sizeof(*ctx));
    2942           0 :         if (!ctx) {
    2943           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    2944             :                                                  "Memory allocation error");
    2945           0 :                 return;
    2946             :         }
    2947           0 :         ctx->request = request;
    2948             : 
    2949           0 :         if (params) {
    2950           0 :                 if (spdk_json_decode_object(params, rpc_get_stats_decoders,
    2951             :                                             SPDK_COUNTOF(rpc_get_stats_decoders),
    2952           0 :                                             ctx)) {
    2953           0 :                         SPDK_ERRLOG("spdk_json_decode_object failed\n");
    2954           0 :                         spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters");
    2955           0 :                         free_get_stats_ctx(ctx);
    2956           0 :                         return;
    2957             :                 }
    2958           0 :         }
    2959             : 
    2960           0 :         ctx->tgt = spdk_nvmf_get_tgt(ctx->tgt_name);
    2961           0 :         if (!ctx->tgt) {
    2962           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    2963             :                                                  "Unable to find a target.");
    2964           0 :                 free_get_stats_ctx(ctx);
    2965           0 :                 return;
    2966             :         }
    2967             : 
    2968           0 :         ctx->w = spdk_jsonrpc_begin_result(ctx->request);
    2969           0 :         spdk_json_write_object_begin(ctx->w);
    2970           0 :         spdk_json_write_named_uint64(ctx->w, "tick_rate", spdk_get_ticks_hz());
    2971           0 :         spdk_json_write_named_array_begin(ctx->w, "poll_groups");
    2972             : 
    2973           0 :         spdk_for_each_channel(ctx->tgt,
    2974             :                               _rpc_nvmf_get_stats,
    2975           0 :                               ctx,
    2976             :                               rpc_nvmf_get_stats_done);
    2977           0 : }
    2978             : 
    2979           0 : SPDK_RPC_REGISTER("nvmf_get_stats", rpc_nvmf_get_stats, SPDK_RPC_RUNTIME)
    2980             : 
    2981             : static void
    2982           0 : dump_nvmf_ctrlr(struct spdk_json_write_ctx *w, struct spdk_nvmf_ctrlr *ctrlr)
    2983             : {
    2984             :         uint32_t count;
    2985             : 
    2986           0 :         spdk_json_write_object_begin(w);
    2987             : 
    2988           0 :         spdk_json_write_named_uint32(w, "cntlid", ctrlr->cntlid);
    2989           0 :         spdk_json_write_named_string(w, "hostnqn", ctrlr->hostnqn);
    2990           0 :         spdk_json_write_named_uuid(w, "hostid", &ctrlr->hostid);
    2991             : 
    2992           0 :         count = spdk_bit_array_count_set(ctrlr->qpair_mask);
    2993           0 :         spdk_json_write_named_uint32(w, "num_io_qpairs", count);
    2994             : 
    2995           0 :         spdk_json_write_object_end(w);
    2996           0 : }
    2997             : 
    2998             : static const char *
    2999           0 : nvmf_qpair_state_str(enum spdk_nvmf_qpair_state state)
    3000             : {
    3001           0 :         switch (state) {
    3002             :         case SPDK_NVMF_QPAIR_UNINITIALIZED:
    3003           0 :                 return "uninitialized";
    3004             :         case SPDK_NVMF_QPAIR_CONNECTING:
    3005           0 :                 return "connecting";
    3006             :         case SPDK_NVMF_QPAIR_ENABLED:
    3007           0 :                 return "enabled";
    3008             :         case SPDK_NVMF_QPAIR_DEACTIVATING:
    3009           0 :                 return "deactivating";
    3010             :         case SPDK_NVMF_QPAIR_ERROR:
    3011           0 :                 return "error";
    3012             :         default:
    3013           0 :                 return NULL;
    3014             :         }
    3015           0 : }
    3016             : 
    3017             : static void
    3018           0 : dump_nvmf_qpair(struct spdk_json_write_ctx *w, struct spdk_nvmf_qpair *qpair)
    3019             : {
    3020           0 :         struct spdk_nvme_transport_id trid = {};
    3021             : 
    3022           0 :         spdk_json_write_object_begin(w);
    3023             : 
    3024           0 :         spdk_json_write_named_uint32(w, "cntlid", qpair->ctrlr->cntlid);
    3025           0 :         spdk_json_write_named_uint32(w, "qid", qpair->qid);
    3026           0 :         spdk_json_write_named_string(w, "state", nvmf_qpair_state_str(qpair->state));
    3027           0 :         spdk_json_write_named_string(w, "thread", spdk_thread_get_name(spdk_get_thread()));
    3028           0 :         spdk_json_write_named_string(w, "hostnqn", qpair->ctrlr->hostnqn);
    3029             : 
    3030           0 :         if (spdk_nvmf_qpair_get_listen_trid(qpair, &trid) == 0) {
    3031           0 :                 spdk_json_write_named_object_begin(w, "listen_address");
    3032           0 :                 nvmf_transport_listen_dump_trid(&trid, w);
    3033           0 :                 spdk_json_write_object_end(w);
    3034           0 :                 if (qpair->transport->ops->listen_dump_opts) {
    3035           0 :                         qpair->transport->ops->listen_dump_opts(qpair->transport, &trid, w);
    3036           0 :                 }
    3037           0 :         }
    3038             : 
    3039           0 :         memset(&trid, 0, sizeof(trid));
    3040           0 :         if (spdk_nvmf_qpair_get_peer_trid(qpair, &trid) == 0) {
    3041           0 :                 spdk_json_write_named_object_begin(w, "peer_address");
    3042           0 :                 nvmf_transport_listen_dump_trid(&trid, w);
    3043           0 :                 spdk_json_write_object_end(w);
    3044           0 :         }
    3045             : 
    3046           0 :         nvmf_qpair_auth_dump(qpair, w);
    3047           0 :         spdk_json_write_object_end(w);
    3048           0 : }
    3049             : 
    3050             : static const char *
    3051           0 : nvme_ana_state_str(enum spdk_nvme_ana_state ana_state)
    3052             : {
    3053           0 :         switch (ana_state) {
    3054             :         case SPDK_NVME_ANA_OPTIMIZED_STATE:
    3055           0 :                 return "optimized";
    3056             :         case SPDK_NVME_ANA_NON_OPTIMIZED_STATE:
    3057           0 :                 return "non_optimized";
    3058             :         case SPDK_NVME_ANA_INACCESSIBLE_STATE:
    3059           0 :                 return "inaccessible";
    3060             :         case SPDK_NVME_ANA_PERSISTENT_LOSS_STATE:
    3061           0 :                 return "persistent_loss";
    3062             :         case SPDK_NVME_ANA_CHANGE_STATE:
    3063           0 :                 return "change";
    3064             :         default:
    3065           0 :                 return NULL;
    3066             :         }
    3067           0 : }
    3068             : 
    3069             : static void
    3070           0 : dump_nvmf_subsystem_listener(struct spdk_json_write_ctx *w,
    3071             :                              struct spdk_nvmf_subsystem_listener *listener)
    3072             : {
    3073           0 :         const struct spdk_nvme_transport_id *trid = listener->trid;
    3074             :         uint32_t i;
    3075             : 
    3076           0 :         spdk_json_write_object_begin(w);
    3077             : 
    3078           0 :         spdk_json_write_named_object_begin(w, "address");
    3079           0 :         nvmf_transport_listen_dump_trid(trid, w);
    3080           0 :         spdk_json_write_object_end(w);
    3081             : 
    3082           0 :         if (spdk_nvmf_subsystem_get_ana_reporting(listener->subsystem)) {
    3083           0 :                 spdk_json_write_named_array_begin(w, "ana_states");
    3084           0 :                 for (i = 0; i < listener->subsystem->max_nsid; i++) {
    3085           0 :                         spdk_json_write_object_begin(w);
    3086           0 :                         spdk_json_write_named_uint32(w, "ana_group", i + 1);
    3087           0 :                         spdk_json_write_named_string(w, "ana_state",
    3088           0 :                                                      nvme_ana_state_str(listener->ana_state[i]));
    3089           0 :                         spdk_json_write_object_end(w);
    3090           0 :                 }
    3091           0 :                 spdk_json_write_array_end(w);
    3092           0 :         }
    3093             : 
    3094           0 :         spdk_json_write_object_end(w);
    3095           0 : }
    3096             : 
    3097             : struct rpc_subsystem_query_ctx {
    3098             :         char *nqn;
    3099             :         char *tgt_name;
    3100             :         struct spdk_nvmf_subsystem *subsystem;
    3101             :         struct spdk_jsonrpc_request *request;
    3102             :         struct spdk_json_write_ctx *w;
    3103             : };
    3104             : 
    3105             : static const struct spdk_json_object_decoder rpc_subsystem_query_decoders[] = {
    3106             :         {"nqn", offsetof(struct rpc_subsystem_query_ctx, nqn), spdk_json_decode_string},
    3107             :         {"tgt_name", offsetof(struct rpc_subsystem_query_ctx, tgt_name), spdk_json_decode_string, true},
    3108             : };
    3109             : 
    3110             : static void
    3111           0 : free_rpc_subsystem_query_ctx(struct rpc_subsystem_query_ctx *ctx)
    3112             : {
    3113           0 :         free(ctx->nqn);
    3114           0 :         free(ctx->tgt_name);
    3115           0 :         free(ctx);
    3116           0 : }
    3117             : 
    3118             : static void
    3119           0 : rpc_nvmf_get_controllers_paused(struct spdk_nvmf_subsystem *subsystem,
    3120             :                                 void *cb_arg, int status)
    3121             : {
    3122           0 :         struct rpc_subsystem_query_ctx *ctx = cb_arg;
    3123             :         struct spdk_json_write_ctx *w;
    3124             :         struct spdk_nvmf_ctrlr *ctrlr;
    3125             : 
    3126           0 :         w = spdk_jsonrpc_begin_result(ctx->request);
    3127             : 
    3128           0 :         spdk_json_write_array_begin(w);
    3129           0 :         TAILQ_FOREACH(ctrlr, &ctx->subsystem->ctrlrs, link) {
    3130           0 :                 dump_nvmf_ctrlr(w, ctrlr);
    3131           0 :         }
    3132           0 :         spdk_json_write_array_end(w);
    3133             : 
    3134           0 :         spdk_jsonrpc_end_result(ctx->request, w);
    3135             : 
    3136           0 :         if (spdk_nvmf_subsystem_resume(ctx->subsystem, NULL, NULL)) {
    3137           0 :                 SPDK_ERRLOG("Resuming subsystem with NQN %s failed\n", ctx->nqn);
    3138             :                 /* FIXME: RPC should fail if resuming the subsystem failed. */
    3139           0 :         }
    3140             : 
    3141           0 :         free_rpc_subsystem_query_ctx(ctx);
    3142           0 : }
    3143             : 
    3144             : static void
    3145           0 : rpc_nvmf_get_qpairs_done(struct spdk_io_channel_iter *i, int status)
    3146             : {
    3147           0 :         struct rpc_subsystem_query_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
    3148             : 
    3149           0 :         spdk_json_write_array_end(ctx->w);
    3150           0 :         spdk_jsonrpc_end_result(ctx->request, ctx->w);
    3151             : 
    3152           0 :         if (spdk_nvmf_subsystem_resume(ctx->subsystem, NULL, NULL)) {
    3153           0 :                 SPDK_ERRLOG("Resuming subsystem with NQN %s failed\n", ctx->nqn);
    3154             :                 /* FIXME: RPC should fail if resuming the subsystem failed. */
    3155           0 :         }
    3156             : 
    3157           0 :         free_rpc_subsystem_query_ctx(ctx);
    3158           0 : }
    3159             : 
    3160             : static void
    3161           0 : rpc_nvmf_get_qpairs(struct spdk_io_channel_iter *i)
    3162             : {
    3163           0 :         struct rpc_subsystem_query_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
    3164             :         struct spdk_io_channel *ch;
    3165             :         struct spdk_nvmf_poll_group *group;
    3166             :         struct spdk_nvmf_qpair *qpair;
    3167             : 
    3168           0 :         ch = spdk_io_channel_iter_get_channel(i);
    3169           0 :         group = spdk_io_channel_get_ctx(ch);
    3170             : 
    3171           0 :         TAILQ_FOREACH(qpair, &group->qpairs, link) {
    3172           0 :                 if (qpair->ctrlr->subsys == ctx->subsystem) {
    3173           0 :                         dump_nvmf_qpair(ctx->w, qpair);
    3174           0 :                 }
    3175           0 :         }
    3176             : 
    3177           0 :         spdk_for_each_channel_continue(i, 0);
    3178           0 : }
    3179             : 
    3180             : static void
    3181           0 : rpc_nvmf_get_qpairs_paused(struct spdk_nvmf_subsystem *subsystem,
    3182             :                            void *cb_arg, int status)
    3183             : {
    3184           0 :         struct rpc_subsystem_query_ctx *ctx = cb_arg;
    3185             : 
    3186           0 :         ctx->w = spdk_jsonrpc_begin_result(ctx->request);
    3187             : 
    3188           0 :         spdk_json_write_array_begin(ctx->w);
    3189             : 
    3190           0 :         spdk_for_each_channel(ctx->subsystem->tgt,
    3191             :                               rpc_nvmf_get_qpairs,
    3192           0 :                               ctx,
    3193             :                               rpc_nvmf_get_qpairs_done);
    3194           0 : }
    3195             : 
    3196             : static void
    3197           0 : rpc_nvmf_get_listeners_paused(struct spdk_nvmf_subsystem *subsystem,
    3198             :                               void *cb_arg, int status)
    3199             : {
    3200           0 :         struct rpc_subsystem_query_ctx *ctx = cb_arg;
    3201             :         struct spdk_json_write_ctx *w;
    3202             :         struct spdk_nvmf_subsystem_listener *listener;
    3203             : 
    3204           0 :         w = spdk_jsonrpc_begin_result(ctx->request);
    3205             : 
    3206           0 :         spdk_json_write_array_begin(w);
    3207             : 
    3208           0 :         for (listener = spdk_nvmf_subsystem_get_first_listener(ctx->subsystem);
    3209           0 :              listener != NULL;
    3210           0 :              listener = spdk_nvmf_subsystem_get_next_listener(ctx->subsystem, listener)) {
    3211           0 :                 dump_nvmf_subsystem_listener(w, listener);
    3212           0 :         }
    3213           0 :         spdk_json_write_array_end(w);
    3214             : 
    3215           0 :         spdk_jsonrpc_end_result(ctx->request, w);
    3216             : 
    3217           0 :         if (spdk_nvmf_subsystem_resume(ctx->subsystem, NULL, NULL)) {
    3218           0 :                 SPDK_ERRLOG("Resuming subsystem with NQN %s failed\n", ctx->nqn);
    3219             :                 /* FIXME: RPC should fail if resuming the subsystem failed. */
    3220           0 :         }
    3221             : 
    3222           0 :         free_rpc_subsystem_query_ctx(ctx);
    3223           0 : }
    3224             : 
    3225             : static void
    3226           0 : _rpc_nvmf_subsystem_query(struct spdk_jsonrpc_request *request,
    3227             :                           const struct spdk_json_val *params,
    3228             :                           spdk_nvmf_subsystem_state_change_done cb_fn)
    3229             : {
    3230             :         struct rpc_subsystem_query_ctx *ctx;
    3231             :         struct spdk_nvmf_subsystem *subsystem;
    3232             :         struct spdk_nvmf_tgt *tgt;
    3233             : 
    3234           0 :         ctx = calloc(1, sizeof(*ctx));
    3235           0 :         if (!ctx) {
    3236           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    3237             :                                                  "Out of memory");
    3238           0 :                 return;
    3239             :         }
    3240             : 
    3241           0 :         ctx->request = request;
    3242             : 
    3243           0 :         if (spdk_json_decode_object(params, rpc_subsystem_query_decoders,
    3244             :                                     SPDK_COUNTOF(rpc_subsystem_query_decoders),
    3245           0 :                                     ctx)) {
    3246           0 :                 SPDK_ERRLOG("spdk_json_decode_object failed\n");
    3247           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
    3248             :                                                  "Invalid parameters");
    3249           0 :                 free_rpc_subsystem_query_ctx(ctx);
    3250           0 :                 return;
    3251             :         }
    3252             : 
    3253           0 :         tgt = spdk_nvmf_get_tgt(ctx->tgt_name);
    3254           0 :         if (!tgt) {
    3255           0 :                 SPDK_ERRLOG("Unable to find a target object.\n");
    3256           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    3257             :                                                  "Unable to find a target");
    3258           0 :                 free_rpc_subsystem_query_ctx(ctx);
    3259           0 :                 return;
    3260             :         }
    3261             : 
    3262           0 :         subsystem = spdk_nvmf_tgt_find_subsystem(tgt, ctx->nqn);
    3263           0 :         if (!subsystem) {
    3264           0 :                 SPDK_ERRLOG("Unable to find subsystem with NQN %s\n", ctx->nqn);
    3265           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
    3266             :                                                  "Invalid parameters");
    3267           0 :                 free_rpc_subsystem_query_ctx(ctx);
    3268           0 :                 return;
    3269             :         }
    3270             : 
    3271           0 :         ctx->subsystem = subsystem;
    3272             : 
    3273           0 :         if (spdk_nvmf_subsystem_pause(subsystem, 0, cb_fn, ctx)) {
    3274           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    3275             :                                                  "Internal error");
    3276           0 :                 free_rpc_subsystem_query_ctx(ctx);
    3277           0 :                 return;
    3278             :         }
    3279           0 : }
    3280             : 
    3281             : static void
    3282           0 : rpc_nvmf_subsystem_get_controllers(struct spdk_jsonrpc_request *request,
    3283             :                                    const struct spdk_json_val *params)
    3284             : {
    3285           0 :         _rpc_nvmf_subsystem_query(request, params, rpc_nvmf_get_controllers_paused);
    3286           0 : }
    3287           0 : SPDK_RPC_REGISTER("nvmf_subsystem_get_controllers", rpc_nvmf_subsystem_get_controllers,
    3288             :                   SPDK_RPC_RUNTIME);
    3289             : 
    3290             : static void
    3291           0 : rpc_nvmf_subsystem_get_qpairs(struct spdk_jsonrpc_request *request,
    3292             :                               const struct spdk_json_val *params)
    3293             : {
    3294           0 :         _rpc_nvmf_subsystem_query(request, params, rpc_nvmf_get_qpairs_paused);
    3295           0 : }
    3296           0 : SPDK_RPC_REGISTER("nvmf_subsystem_get_qpairs", rpc_nvmf_subsystem_get_qpairs, SPDK_RPC_RUNTIME);
    3297             : 
    3298             : static void
    3299           0 : rpc_nvmf_subsystem_get_listeners(struct spdk_jsonrpc_request *request,
    3300             :                                  const struct spdk_json_val *params)
    3301             : {
    3302           0 :         _rpc_nvmf_subsystem_query(request, params, rpc_nvmf_get_listeners_paused);
    3303           0 : }
    3304           0 : SPDK_RPC_REGISTER("nvmf_subsystem_get_listeners", rpc_nvmf_subsystem_get_listeners,
    3305             :                   SPDK_RPC_RUNTIME);
    3306             : 
    3307             : struct rpc_mdns_prr {
    3308             :         char *tgt_name;
    3309             : };
    3310             : 
    3311             : static const struct spdk_json_object_decoder rpc_mdns_prr_decoders[] = {
    3312             :         {"tgt_name", offsetof(struct rpc_mdns_prr, tgt_name), spdk_json_decode_string, true},
    3313             : };
    3314             : 
    3315             : static void
    3316           0 : rpc_nvmf_publish_mdns_prr(struct spdk_jsonrpc_request *request,
    3317             :                           const struct spdk_json_val *params)
    3318             : {
    3319             :         int rc;
    3320           0 :         struct rpc_mdns_prr req = { 0 };
    3321             :         struct spdk_nvmf_tgt *tgt;
    3322             : 
    3323           0 :         if (params) {
    3324           0 :                 if (spdk_json_decode_object(params, rpc_mdns_prr_decoders,
    3325             :                                             SPDK_COUNTOF(rpc_mdns_prr_decoders),
    3326             :                                             &req)) {
    3327           0 :                         SPDK_ERRLOG("spdk_json_decode_object failed\n");
    3328           0 :                         spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters");
    3329           0 :                         return;
    3330             :                 }
    3331           0 :         }
    3332             : 
    3333           0 :         tgt = spdk_nvmf_get_tgt(req.tgt_name);
    3334           0 :         if (!tgt) {
    3335           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    3336             :                                                  "Unable to find a target.");
    3337           0 :                 free(req.tgt_name);
    3338           0 :                 return;
    3339             :         }
    3340             : 
    3341           0 :         rc = nvmf_publish_mdns_prr(tgt);
    3342           0 :         if (rc) {
    3343           0 :                 spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
    3344           0 :                 free(req.tgt_name);
    3345           0 :                 return;
    3346             :         }
    3347             : 
    3348           0 :         spdk_jsonrpc_send_bool_response(request, true);
    3349           0 :         free(req.tgt_name);
    3350           0 : }
    3351           0 : SPDK_RPC_REGISTER("nvmf_publish_mdns_prr", rpc_nvmf_publish_mdns_prr, SPDK_RPC_RUNTIME);
    3352             : 
    3353             : static void
    3354           0 : rpc_nvmf_stop_mdns_prr(struct spdk_jsonrpc_request *request,
    3355             :                        const struct spdk_json_val *params)
    3356             : {
    3357           0 :         struct rpc_mdns_prr req = { 0 };
    3358             :         struct spdk_nvmf_tgt *tgt;
    3359             : 
    3360           0 :         if (params) {
    3361           0 :                 if (spdk_json_decode_object(params, rpc_mdns_prr_decoders,
    3362             :                                             SPDK_COUNTOF(rpc_mdns_prr_decoders),
    3363             :                                             &req)) {
    3364           0 :                         SPDK_ERRLOG("spdk_json_decode_object failed\n");
    3365           0 :                         spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters");
    3366           0 :                         return;
    3367             :                 }
    3368           0 :         }
    3369             : 
    3370           0 :         tgt = spdk_nvmf_get_tgt(req.tgt_name);
    3371           0 :         if (!tgt) {
    3372           0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
    3373             :                                                  "Unable to find a target.");
    3374           0 :                 free(req.tgt_name);
    3375           0 :                 return;
    3376             :         }
    3377             : 
    3378           0 :         nvmf_tgt_stop_mdns_prr(tgt);
    3379             : 
    3380           0 :         spdk_jsonrpc_send_bool_response(request, true);
    3381           0 :         free(req.tgt_name);
    3382           0 : }
    3383           0 : SPDK_RPC_REGISTER("nvmf_stop_mdns_prr", rpc_nvmf_stop_mdns_prr, SPDK_RPC_RUNTIME);

Generated by: LCOV version 1.15