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

Generated by: LCOV version 1.15