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