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