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