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