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