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