xref: /spdk/lib/bdev/bdev_rpc.c (revision 12fbe739a31b09aff0d05f354d4f3bbef99afc55)
1 /*   SPDX-License-Identifier: BSD-3-Clause
2  *   Copyright (C) 2018 Intel Corporation.
3  *   All rights reserved.
4  *   Copyright (c) 2022-2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
5  */
6 
7 #include "spdk/bdev.h"
8 
9 #include "spdk/env.h"
10 #include "spdk/rpc.h"
11 #include "spdk/util.h"
12 #include "spdk/string.h"
13 #include "spdk/base64.h"
14 #include "spdk/bdev_module.h"
15 #include "spdk/dma.h"
16 
17 #include "spdk/log.h"
18 
19 #include "bdev_internal.h"
20 
21 static void
22 dummy_bdev_event_cb(enum spdk_bdev_event_type type, struct spdk_bdev *bdev, void *ctx)
23 {
24 }
25 
26 struct spdk_rpc_set_bdev_opts {
27 	uint32_t bdev_io_pool_size;
28 	uint32_t bdev_io_cache_size;
29 	bool bdev_auto_examine;
30 };
31 
32 static const struct spdk_json_object_decoder rpc_set_bdev_opts_decoders[] = {
33 	{"bdev_io_pool_size", offsetof(struct spdk_rpc_set_bdev_opts, bdev_io_pool_size), spdk_json_decode_uint32, true},
34 	{"bdev_io_cache_size", offsetof(struct spdk_rpc_set_bdev_opts, bdev_io_cache_size), spdk_json_decode_uint32, true},
35 	{"bdev_auto_examine", offsetof(struct spdk_rpc_set_bdev_opts, bdev_auto_examine), spdk_json_decode_bool, true},
36 };
37 
38 static void
39 rpc_bdev_set_options(struct spdk_jsonrpc_request *request, const struct spdk_json_val *params)
40 {
41 	struct spdk_rpc_set_bdev_opts rpc_opts;
42 	struct spdk_bdev_opts bdev_opts;
43 	int rc;
44 
45 	rpc_opts.bdev_io_pool_size = UINT32_MAX;
46 	rpc_opts.bdev_io_cache_size = UINT32_MAX;
47 	rpc_opts.bdev_auto_examine = true;
48 
49 	if (params != NULL) {
50 		if (spdk_json_decode_object(params, rpc_set_bdev_opts_decoders,
51 					    SPDK_COUNTOF(rpc_set_bdev_opts_decoders), &rpc_opts)) {
52 			SPDK_ERRLOG("spdk_json_decode_object() failed\n");
53 			spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
54 							 "Invalid parameters");
55 			return;
56 		}
57 	}
58 
59 	spdk_bdev_get_opts(&bdev_opts, sizeof(bdev_opts));
60 	if (rpc_opts.bdev_io_pool_size != UINT32_MAX) {
61 		bdev_opts.bdev_io_pool_size = rpc_opts.bdev_io_pool_size;
62 	}
63 	if (rpc_opts.bdev_io_cache_size != UINT32_MAX) {
64 		bdev_opts.bdev_io_cache_size = rpc_opts.bdev_io_cache_size;
65 	}
66 	bdev_opts.bdev_auto_examine = rpc_opts.bdev_auto_examine;
67 
68 	rc = spdk_bdev_set_opts(&bdev_opts);
69 
70 	if (rc != 0) {
71 		spdk_jsonrpc_send_error_response_fmt(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
72 						     "Pool size %" PRIu32 " too small for cache size %" PRIu32,
73 						     bdev_opts.bdev_io_pool_size, bdev_opts.bdev_io_cache_size);
74 		return;
75 	}
76 
77 	spdk_jsonrpc_send_bool_response(request, true);
78 }
79 SPDK_RPC_REGISTER("bdev_set_options", rpc_bdev_set_options, SPDK_RPC_STARTUP)
80 
81 static void
82 rpc_bdev_wait_for_examine_cpl(void *arg)
83 {
84 	struct spdk_jsonrpc_request *request = arg;
85 
86 	spdk_jsonrpc_send_bool_response(request, true);
87 }
88 
89 static void
90 rpc_bdev_wait_for_examine(struct spdk_jsonrpc_request *request,
91 			  const struct spdk_json_val *params)
92 {
93 	int rc;
94 
95 	if (params != NULL) {
96 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
97 						 "bdev_wait_for_examine requires no parameters");
98 		return;
99 	}
100 
101 	rc = spdk_bdev_wait_for_examine(rpc_bdev_wait_for_examine_cpl, request);
102 	if (rc != 0) {
103 		spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
104 	}
105 }
106 SPDK_RPC_REGISTER("bdev_wait_for_examine", rpc_bdev_wait_for_examine, SPDK_RPC_RUNTIME)
107 
108 struct rpc_bdev_examine {
109 	char *name;
110 };
111 
112 static void
113 free_rpc_bdev_examine(struct rpc_bdev_examine *r)
114 {
115 	free(r->name);
116 }
117 
118 static const struct spdk_json_object_decoder rpc_examine_bdev_decoders[] = {
119 	{"name", offsetof(struct rpc_bdev_examine, name), spdk_json_decode_string},
120 };
121 
122 static void
123 rpc_bdev_examine_bdev(struct spdk_jsonrpc_request *request,
124 		      const struct spdk_json_val *params)
125 {
126 	struct rpc_bdev_examine req = {NULL};
127 	int rc;
128 
129 	if (spdk_json_decode_object(params, rpc_examine_bdev_decoders,
130 				    SPDK_COUNTOF(rpc_examine_bdev_decoders),
131 				    &req)) {
132 		SPDK_ERRLOG("spdk_json_decode_object() failed\n");
133 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
134 						 "spdk_json_decode_object failed");
135 		goto cleanup;
136 	}
137 
138 	rc = spdk_bdev_examine(req.name);
139 	if (rc != 0) {
140 		spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
141 		goto cleanup;
142 	}
143 
144 	spdk_jsonrpc_send_bool_response(request, true);
145 
146 cleanup:
147 	free_rpc_bdev_examine(&req);
148 }
149 SPDK_RPC_REGISTER("bdev_examine", rpc_bdev_examine_bdev, SPDK_RPC_RUNTIME)
150 
151 struct rpc_get_iostat_ctx {
152 	int bdev_count;
153 	int rc;
154 	struct spdk_jsonrpc_request *request;
155 	struct spdk_json_write_ctx *w;
156 	bool per_channel;
157 };
158 
159 struct bdev_get_iostat_ctx {
160 	struct spdk_bdev_io_stat *stat;
161 	struct rpc_get_iostat_ctx *rpc_ctx;
162 	struct spdk_bdev_desc *desc;
163 };
164 
165 static void
166 rpc_get_iostat_started(struct rpc_get_iostat_ctx *rpc_ctx)
167 {
168 	rpc_ctx->w = spdk_jsonrpc_begin_result(rpc_ctx->request);
169 
170 	spdk_json_write_object_begin(rpc_ctx->w);
171 	spdk_json_write_named_uint64(rpc_ctx->w, "tick_rate", spdk_get_ticks_hz());
172 	spdk_json_write_named_uint64(rpc_ctx->w, "ticks", spdk_get_ticks());
173 }
174 
175 static void
176 rpc_get_iostat_done(struct rpc_get_iostat_ctx *rpc_ctx)
177 {
178 	if (--rpc_ctx->bdev_count != 0) {
179 		return;
180 	}
181 
182 	if (rpc_ctx->rc == 0) {
183 		spdk_json_write_array_end(rpc_ctx->w);
184 		spdk_json_write_object_end(rpc_ctx->w);
185 		spdk_jsonrpc_end_result(rpc_ctx->request, rpc_ctx->w);
186 	} else {
187 		/* Return error response after processing all specified bdevs
188 		 * completed or failed.
189 		 */
190 		spdk_jsonrpc_send_error_response(rpc_ctx->request, rpc_ctx->rc,
191 						 spdk_strerror(-rpc_ctx->rc));
192 	}
193 
194 	free(rpc_ctx);
195 }
196 
197 static struct bdev_get_iostat_ctx *
198 bdev_iostat_ctx_alloc(bool iostat_ext)
199 {
200 	struct bdev_get_iostat_ctx *ctx;
201 
202 	ctx = calloc(1, sizeof(struct bdev_get_iostat_ctx));
203 	if (ctx == NULL) {
204 		return NULL;
205 	}
206 
207 	ctx->stat = bdev_alloc_io_stat(iostat_ext);
208 	if (ctx->stat == NULL) {
209 		free(ctx);
210 		return NULL;
211 	}
212 
213 	return ctx;
214 }
215 
216 static void
217 bdev_iostat_ctx_free(struct bdev_get_iostat_ctx *ctx)
218 {
219 	bdev_free_io_stat(ctx->stat);
220 	free(ctx);
221 }
222 
223 static void
224 bdev_get_iostat_done(struct spdk_bdev *bdev, struct spdk_bdev_io_stat *stat,
225 		     void *cb_arg, int rc)
226 {
227 	struct bdev_get_iostat_ctx *bdev_ctx = cb_arg;
228 	struct rpc_get_iostat_ctx *rpc_ctx = bdev_ctx->rpc_ctx;
229 	struct spdk_json_write_ctx *w = rpc_ctx->w;
230 
231 	if (rc != 0 || rpc_ctx->rc != 0) {
232 		if (rpc_ctx->rc == 0) {
233 			rpc_ctx->rc = rc;
234 		}
235 		goto done;
236 	}
237 
238 	assert(stat == bdev_ctx->stat);
239 
240 	spdk_json_write_object_begin(w);
241 
242 	spdk_json_write_named_string(w, "name", spdk_bdev_get_name(bdev));
243 
244 	spdk_bdev_dump_io_stat_json(stat, w);
245 
246 	if (spdk_bdev_get_qd_sampling_period(bdev)) {
247 		spdk_json_write_named_uint64(w, "queue_depth_polling_period",
248 					     spdk_bdev_get_qd_sampling_period(bdev));
249 
250 		spdk_json_write_named_uint64(w, "queue_depth", spdk_bdev_get_qd(bdev));
251 
252 		spdk_json_write_named_uint64(w, "io_time", spdk_bdev_get_io_time(bdev));
253 
254 		spdk_json_write_named_uint64(w, "weighted_io_time",
255 					     spdk_bdev_get_weighted_io_time(bdev));
256 	}
257 
258 	if (bdev->fn_table->dump_device_stat_json) {
259 		spdk_json_write_named_object_begin(w, "driver_specific");
260 		bdev->fn_table->dump_device_stat_json(bdev->ctxt, w);
261 		spdk_json_write_object_end(w);
262 	}
263 
264 	spdk_json_write_object_end(w);
265 
266 done:
267 	rpc_get_iostat_done(rpc_ctx);
268 
269 	spdk_bdev_close(bdev_ctx->desc);
270 	bdev_iostat_ctx_free(bdev_ctx);
271 }
272 
273 static int
274 bdev_get_iostat(void *ctx, struct spdk_bdev *bdev)
275 {
276 	struct rpc_get_iostat_ctx *rpc_ctx = ctx;
277 	struct bdev_get_iostat_ctx *bdev_ctx;
278 	int rc;
279 
280 	bdev_ctx = bdev_iostat_ctx_alloc(true);
281 	if (bdev_ctx == NULL) {
282 		SPDK_ERRLOG("Failed to allocate bdev_iostat_ctx struct\n");
283 		return -ENOMEM;
284 	}
285 
286 	rc = spdk_bdev_open_ext(spdk_bdev_get_name(bdev), false, dummy_bdev_event_cb, NULL,
287 				&bdev_ctx->desc);
288 	if (rc != 0) {
289 		bdev_iostat_ctx_free(bdev_ctx);
290 		SPDK_ERRLOG("Failed to open bdev\n");
291 		return rc;
292 	}
293 
294 	rpc_ctx->bdev_count++;
295 	bdev_ctx->rpc_ctx = rpc_ctx;
296 	spdk_bdev_get_device_stat(bdev, bdev_ctx->stat, bdev_get_iostat_done, bdev_ctx);
297 
298 	return 0;
299 }
300 
301 static void
302 bdev_get_per_channel_stat_done(struct spdk_bdev *bdev, void *ctx, int status)
303 {
304 	struct bdev_get_iostat_ctx *bdev_ctx = ctx;
305 
306 	rpc_get_iostat_done(bdev_ctx->rpc_ctx);
307 
308 	spdk_bdev_close(bdev_ctx->desc);
309 
310 	bdev_iostat_ctx_free(bdev_ctx);
311 }
312 
313 static void
314 bdev_get_per_channel_stat(struct spdk_bdev_channel_iter *i, struct spdk_bdev *bdev,
315 			  struct spdk_io_channel *ch, void *ctx)
316 {
317 	struct bdev_get_iostat_ctx *bdev_ctx = ctx;
318 	struct spdk_json_write_ctx *w = bdev_ctx->rpc_ctx->w;
319 
320 	spdk_bdev_get_io_stat(bdev, ch, bdev_ctx->stat);
321 
322 	spdk_json_write_object_begin(w);
323 	spdk_json_write_named_uint64(w, "thread_id", spdk_thread_get_id(spdk_get_thread()));
324 	spdk_bdev_dump_io_stat_json(bdev_ctx->stat, w);
325 	spdk_json_write_object_end(w);
326 
327 	spdk_bdev_for_each_channel_continue(i, 0);
328 }
329 
330 struct rpc_bdev_get_iostat {
331 	char *name;
332 	bool per_channel;
333 };
334 
335 static void
336 free_rpc_bdev_get_iostat(struct rpc_bdev_get_iostat *r)
337 {
338 	free(r->name);
339 }
340 
341 static const struct spdk_json_object_decoder rpc_bdev_get_iostat_decoders[] = {
342 	{"name", offsetof(struct rpc_bdev_get_iostat, name), spdk_json_decode_string, true},
343 	{"per_channel", offsetof(struct rpc_bdev_get_iostat, per_channel), spdk_json_decode_bool, true},
344 };
345 
346 static void
347 rpc_bdev_get_iostat(struct spdk_jsonrpc_request *request,
348 		    const struct spdk_json_val *params)
349 {
350 	struct rpc_bdev_get_iostat req = {};
351 	struct spdk_bdev_desc *desc = NULL;
352 	struct rpc_get_iostat_ctx *rpc_ctx;
353 	struct bdev_get_iostat_ctx *bdev_ctx;
354 	struct spdk_bdev *bdev;
355 	int rc;
356 
357 	if (params != NULL) {
358 		if (spdk_json_decode_object(params, rpc_bdev_get_iostat_decoders,
359 					    SPDK_COUNTOF(rpc_bdev_get_iostat_decoders),
360 					    &req)) {
361 			SPDK_ERRLOG("spdk_json_decode_object failed\n");
362 			spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
363 							 "spdk_json_decode_object failed");
364 			free_rpc_bdev_get_iostat(&req);
365 			return;
366 		}
367 
368 		if (req.per_channel == true && !req.name) {
369 			SPDK_ERRLOG("Bdev name is required for per channel IO statistics\n");
370 			spdk_jsonrpc_send_error_response(request, -EINVAL, spdk_strerror(EINVAL));
371 			free_rpc_bdev_get_iostat(&req);
372 			return;
373 		}
374 
375 		if (req.name) {
376 			rc = spdk_bdev_open_ext(req.name, false, dummy_bdev_event_cb, NULL, &desc);
377 			if (rc != 0) {
378 				SPDK_ERRLOG("Failed to open bdev '%s': %d\n", req.name, rc);
379 				spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
380 				free_rpc_bdev_get_iostat(&req);
381 				return;
382 			}
383 		}
384 	}
385 
386 	free_rpc_bdev_get_iostat(&req);
387 
388 	rpc_ctx = calloc(1, sizeof(struct rpc_get_iostat_ctx));
389 	if (rpc_ctx == NULL) {
390 		SPDK_ERRLOG("Failed to allocate rpc_iostat_ctx struct\n");
391 		spdk_jsonrpc_send_error_response(request, -ENOMEM, spdk_strerror(ENOMEM));
392 		return;
393 	}
394 
395 	/*
396 	 * Increment initial bdev_count so that it will never reach 0 in the middle
397 	 * of iterating.
398 	 */
399 	rpc_ctx->bdev_count++;
400 	rpc_ctx->request = request;
401 	rpc_ctx->per_channel = req.per_channel;
402 
403 	if (desc != NULL) {
404 		bdev = spdk_bdev_desc_get_bdev(desc);
405 
406 		bdev_ctx = bdev_iostat_ctx_alloc(req.per_channel == false);
407 		if (bdev_ctx == NULL) {
408 			SPDK_ERRLOG("Failed to allocate bdev_iostat_ctx struct\n");
409 			rpc_ctx->rc = -ENOMEM;
410 
411 			spdk_bdev_close(desc);
412 		} else {
413 			bdev_ctx->desc = desc;
414 
415 			rpc_ctx->bdev_count++;
416 			bdev_ctx->rpc_ctx = rpc_ctx;
417 			if (req.per_channel == false) {
418 				spdk_bdev_get_device_stat(bdev, bdev_ctx->stat, bdev_get_iostat_done,
419 							  bdev_ctx);
420 			} else {
421 				/* If per_channel is true, there is no failure after here and
422 				 * we have to start RPC response before executing
423 				 * spdk_bdev_for_each_channel().
424 				 */
425 				rpc_get_iostat_started(rpc_ctx);
426 				spdk_json_write_named_string(rpc_ctx->w, "name", spdk_bdev_get_name(bdev));
427 				spdk_json_write_named_array_begin(rpc_ctx->w, "channels");
428 
429 				spdk_bdev_for_each_channel(bdev,
430 							   bdev_get_per_channel_stat,
431 							   bdev_ctx,
432 							   bdev_get_per_channel_stat_done);
433 
434 				rpc_get_iostat_done(rpc_ctx);
435 				return;
436 			}
437 		}
438 	} else {
439 		rc = spdk_for_each_bdev(rpc_ctx, bdev_get_iostat);
440 		if (rc != 0 && rpc_ctx->rc == 0) {
441 			rpc_ctx->rc = rc;
442 		}
443 	}
444 
445 	if (rpc_ctx->rc == 0) {
446 		/* We want to fail the RPC for all failures. If per_channel is false,
447 		 * it is enough to defer starting RPC response until it is ensured that
448 		 * all spdk_bdev_for_each_channel() calls will succeed or there is no bdev.
449 		 */
450 		rpc_get_iostat_started(rpc_ctx);
451 		spdk_json_write_named_array_begin(rpc_ctx->w, "bdevs");
452 	}
453 
454 	rpc_get_iostat_done(rpc_ctx);
455 }
456 SPDK_RPC_REGISTER("bdev_get_iostat", rpc_bdev_get_iostat, SPDK_RPC_RUNTIME)
457 
458 struct rpc_reset_iostat_ctx {
459 	int bdev_count;
460 	int rc;
461 	struct spdk_jsonrpc_request *request;
462 	struct spdk_json_write_ctx *w;
463 	enum spdk_bdev_reset_stat_mode mode;
464 };
465 
466 struct bdev_reset_iostat_ctx {
467 	struct rpc_reset_iostat_ctx *rpc_ctx;
468 	struct spdk_bdev_desc *desc;
469 };
470 
471 static void
472 rpc_reset_iostat_done(struct rpc_reset_iostat_ctx *rpc_ctx)
473 {
474 	if (--rpc_ctx->bdev_count != 0) {
475 		return;
476 	}
477 
478 	if (rpc_ctx->rc == 0) {
479 		spdk_jsonrpc_send_bool_response(rpc_ctx->request, true);
480 	} else {
481 		spdk_jsonrpc_send_error_response(rpc_ctx->request, rpc_ctx->rc,
482 						 spdk_strerror(-rpc_ctx->rc));
483 	}
484 
485 	free(rpc_ctx);
486 }
487 
488 static void
489 bdev_reset_iostat_done(struct spdk_bdev *bdev, void *cb_arg, int rc)
490 {
491 	struct bdev_reset_iostat_ctx *bdev_ctx = cb_arg;
492 	struct rpc_reset_iostat_ctx *rpc_ctx = bdev_ctx->rpc_ctx;
493 
494 	if (rc != 0 || rpc_ctx->rc != 0) {
495 		if (rpc_ctx->rc == 0) {
496 			rpc_ctx->rc = rc;
497 		}
498 	}
499 
500 	rpc_reset_iostat_done(rpc_ctx);
501 
502 	spdk_bdev_close(bdev_ctx->desc);
503 	free(bdev_ctx);
504 }
505 
506 static int
507 bdev_reset_iostat(void *ctx, struct spdk_bdev *bdev)
508 {
509 	struct rpc_reset_iostat_ctx *rpc_ctx = ctx;
510 	struct bdev_reset_iostat_ctx *bdev_ctx;
511 	int rc;
512 
513 	bdev_ctx = calloc(1, sizeof(struct bdev_reset_iostat_ctx));
514 	if (bdev_ctx == NULL) {
515 		SPDK_ERRLOG("Failed to allocate bdev_iostat_ctx struct\n");
516 		return -ENOMEM;
517 	}
518 
519 	rc = spdk_bdev_open_ext(spdk_bdev_get_name(bdev), false, dummy_bdev_event_cb, NULL,
520 				&bdev_ctx->desc);
521 	if (rc != 0) {
522 		free(bdev_ctx);
523 		SPDK_ERRLOG("Failed to open bdev\n");
524 		return rc;
525 	}
526 
527 	if (bdev->fn_table->reset_device_stat) {
528 		bdev->fn_table->reset_device_stat(bdev->ctxt);
529 	}
530 
531 	rpc_ctx->bdev_count++;
532 	bdev_ctx->rpc_ctx = rpc_ctx;
533 	bdev_reset_device_stat(bdev, rpc_ctx->mode, bdev_reset_iostat_done, bdev_ctx);
534 
535 	return 0;
536 }
537 
538 struct rpc_bdev_reset_iostat {
539 	char *name;
540 	enum spdk_bdev_reset_stat_mode mode;
541 };
542 
543 static void
544 free_rpc_bdev_reset_iostat(struct rpc_bdev_reset_iostat *r)
545 {
546 	free(r->name);
547 }
548 
549 static int
550 rpc_decode_reset_iostat_mode(const struct spdk_json_val *val, void *out)
551 {
552 	enum spdk_bdev_reset_stat_mode *mode = out;
553 
554 	if (spdk_json_strequal(val, "all") == true) {
555 		*mode = SPDK_BDEV_RESET_STAT_ALL;
556 	} else if (spdk_json_strequal(val, "maxmin") == true) {
557 		*mode = SPDK_BDEV_RESET_STAT_MAXMIN;
558 	} else {
559 		SPDK_NOTICELOG("Invalid parameter value: mode\n");
560 		return -EINVAL;
561 	}
562 
563 	return 0;
564 }
565 
566 static const struct spdk_json_object_decoder rpc_bdev_reset_iostat_decoders[] = {
567 	{"name", offsetof(struct rpc_bdev_reset_iostat, name), spdk_json_decode_string, true},
568 	{"mode", offsetof(struct rpc_bdev_reset_iostat, mode), rpc_decode_reset_iostat_mode, true},
569 };
570 
571 static void
572 rpc_bdev_reset_iostat(struct spdk_jsonrpc_request *request, const struct spdk_json_val *params)
573 {
574 	struct rpc_bdev_reset_iostat req = { .mode = SPDK_BDEV_RESET_STAT_ALL, };
575 	struct spdk_bdev_desc *desc = NULL;
576 	struct rpc_reset_iostat_ctx *rpc_ctx;
577 	struct bdev_reset_iostat_ctx *bdev_ctx;
578 	int rc;
579 
580 	if (params != NULL) {
581 		if (spdk_json_decode_object(params, rpc_bdev_reset_iostat_decoders,
582 					    SPDK_COUNTOF(rpc_bdev_reset_iostat_decoders),
583 					    &req)) {
584 			SPDK_ERRLOG("spdk_json_decode_object failed\n");
585 			spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
586 							 "spdk_json_decode_object failed");
587 			free_rpc_bdev_reset_iostat(&req);
588 			return;
589 		}
590 
591 		if (req.name) {
592 			rc = spdk_bdev_open_ext(req.name, false, dummy_bdev_event_cb, NULL, &desc);
593 			if (rc != 0) {
594 				SPDK_ERRLOG("Failed to open bdev '%s': %d\n", req.name, rc);
595 				spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
596 				free_rpc_bdev_reset_iostat(&req);
597 				return;
598 			}
599 		}
600 	}
601 
602 
603 	rpc_ctx = calloc(1, sizeof(struct rpc_reset_iostat_ctx));
604 	if (rpc_ctx == NULL) {
605 		SPDK_ERRLOG("Failed to allocate rpc_iostat_ctx struct\n");
606 		spdk_jsonrpc_send_error_response(request, -ENOMEM, spdk_strerror(ENOMEM));
607 		free_rpc_bdev_reset_iostat(&req);
608 		return;
609 	}
610 
611 	/*
612 	 * Increment initial bdev_count so that it will never reach 0 in the middle
613 	 * of iterating.
614 	 */
615 	rpc_ctx->bdev_count++;
616 	rpc_ctx->request = request;
617 	rpc_ctx->mode = req.mode;
618 
619 	free_rpc_bdev_reset_iostat(&req);
620 
621 	if (desc != NULL) {
622 		bdev_ctx = calloc(1, sizeof(struct bdev_reset_iostat_ctx));
623 		if (bdev_ctx == NULL) {
624 			SPDK_ERRLOG("Failed to allocate bdev_iostat_ctx struct\n");
625 			rpc_ctx->rc = -ENOMEM;
626 
627 			spdk_bdev_close(desc);
628 		} else {
629 			bdev_ctx->desc = desc;
630 
631 			rpc_ctx->bdev_count++;
632 			bdev_ctx->rpc_ctx = rpc_ctx;
633 			bdev_reset_device_stat(spdk_bdev_desc_get_bdev(desc), rpc_ctx->mode,
634 					       bdev_reset_iostat_done, bdev_ctx);
635 		}
636 	} else {
637 		rc = spdk_for_each_bdev(rpc_ctx, bdev_reset_iostat);
638 		if (rc != 0 && rpc_ctx->rc == 0) {
639 			rpc_ctx->rc = rc;
640 		}
641 	}
642 
643 	rpc_reset_iostat_done(rpc_ctx);
644 }
645 SPDK_RPC_REGISTER("bdev_reset_iostat", rpc_bdev_reset_iostat, SPDK_RPC_RUNTIME)
646 
647 static int
648 rpc_dump_bdev_info(void *ctx, struct spdk_bdev *bdev)
649 {
650 	struct spdk_json_write_ctx *w = ctx;
651 	struct spdk_bdev_alias *tmp;
652 	uint64_t qos_limits[SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES];
653 	struct spdk_memory_domain **domains;
654 	int i, rc;
655 
656 	spdk_json_write_object_begin(w);
657 
658 	spdk_json_write_named_string(w, "name", spdk_bdev_get_name(bdev));
659 
660 	spdk_json_write_named_array_begin(w, "aliases");
661 
662 	TAILQ_FOREACH(tmp, spdk_bdev_get_aliases(bdev), tailq) {
663 		spdk_json_write_string(w, tmp->alias.name);
664 	}
665 
666 	spdk_json_write_array_end(w);
667 
668 	spdk_json_write_named_string(w, "product_name", spdk_bdev_get_product_name(bdev));
669 	spdk_json_write_named_uint32(w, "block_size", spdk_bdev_get_block_size(bdev));
670 	spdk_json_write_named_uint64(w, "num_blocks", spdk_bdev_get_num_blocks(bdev));
671 	spdk_json_write_named_uuid(w, "uuid", &bdev->uuid);
672 
673 	if (spdk_bdev_get_md_size(bdev) != 0) {
674 		spdk_json_write_named_uint32(w, "md_size", spdk_bdev_get_md_size(bdev));
675 		spdk_json_write_named_bool(w, "md_interleave", spdk_bdev_is_md_interleaved(bdev));
676 		spdk_json_write_named_uint32(w, "dif_type", spdk_bdev_get_dif_type(bdev));
677 		if (spdk_bdev_get_dif_type(bdev) != SPDK_DIF_DISABLE) {
678 			spdk_json_write_named_bool(w, "dif_is_head_of_md", spdk_bdev_is_dif_head_of_md(bdev));
679 			spdk_json_write_named_object_begin(w, "enabled_dif_check_types");
680 			spdk_json_write_named_bool(w, "reftag",
681 						   spdk_bdev_is_dif_check_enabled(bdev, SPDK_DIF_CHECK_TYPE_REFTAG));
682 			spdk_json_write_named_bool(w, "apptag",
683 						   spdk_bdev_is_dif_check_enabled(bdev, SPDK_DIF_CHECK_TYPE_APPTAG));
684 			spdk_json_write_named_bool(w, "guard",
685 						   spdk_bdev_is_dif_check_enabled(bdev, SPDK_DIF_CHECK_TYPE_GUARD));
686 			spdk_json_write_object_end(w);
687 		}
688 	}
689 
690 	spdk_json_write_named_object_begin(w, "assigned_rate_limits");
691 	spdk_bdev_get_qos_rate_limits(bdev, qos_limits);
692 	for (i = 0; i < SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES; i++) {
693 		spdk_json_write_named_uint64(w, spdk_bdev_get_qos_rpc_type(i), qos_limits[i]);
694 	}
695 	spdk_json_write_object_end(w);
696 
697 	spdk_json_write_named_bool(w, "claimed",
698 				   (bdev->internal.claim_type != SPDK_BDEV_CLAIM_NONE));
699 	if (bdev->internal.claim_type != SPDK_BDEV_CLAIM_NONE) {
700 		spdk_json_write_named_string(w, "claim_type",
701 					     spdk_bdev_claim_get_name(bdev->internal.claim_type));
702 	}
703 
704 	spdk_json_write_named_bool(w, "zoned", bdev->zoned);
705 	if (bdev->zoned) {
706 		spdk_json_write_named_uint64(w, "zone_size", bdev->zone_size);
707 		spdk_json_write_named_uint64(w, "max_open_zones", bdev->max_open_zones);
708 		spdk_json_write_named_uint64(w, "optimal_open_zones", bdev->optimal_open_zones);
709 	}
710 
711 	spdk_json_write_named_object_begin(w, "supported_io_types");
712 	spdk_json_write_named_bool(w, "read",
713 				   spdk_bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_READ));
714 	spdk_json_write_named_bool(w, "write",
715 				   spdk_bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_WRITE));
716 	spdk_json_write_named_bool(w, "unmap",
717 				   spdk_bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_UNMAP));
718 	spdk_json_write_named_bool(w, "write_zeroes",
719 				   spdk_bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_WRITE_ZEROES));
720 	spdk_json_write_named_bool(w, "flush",
721 				   spdk_bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_FLUSH));
722 	spdk_json_write_named_bool(w, "reset",
723 				   spdk_bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_RESET));
724 	spdk_json_write_named_bool(w, "compare",
725 				   spdk_bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_COMPARE));
726 	spdk_json_write_named_bool(w, "compare_and_write",
727 				   spdk_bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_COMPARE_AND_WRITE));
728 	spdk_json_write_named_bool(w, "abort",
729 				   spdk_bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_ABORT));
730 	spdk_json_write_named_bool(w, "nvme_admin",
731 				   spdk_bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_NVME_ADMIN));
732 	spdk_json_write_named_bool(w, "nvme_io",
733 				   spdk_bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_NVME_IO));
734 	spdk_json_write_object_end(w);
735 
736 	rc = spdk_bdev_get_memory_domains(bdev, NULL, 0);
737 	if (rc > 0) {
738 		domains = calloc(rc, sizeof(struct spdk_memory_domain *));
739 		if (domains) {
740 			i = spdk_bdev_get_memory_domains(bdev, domains, rc);
741 			if (i == rc) {
742 				spdk_json_write_named_array_begin(w, "memory_domains");
743 				for (i = 0; i < rc; i++) {
744 					spdk_json_write_object_begin(w);
745 					spdk_json_write_named_string(w, "dma_device_id", spdk_memory_domain_get_dma_device_id(domains[i]));
746 					spdk_json_write_named_int32(w, "dma_device_type",
747 								    spdk_memory_domain_get_dma_device_type(domains[i]));
748 					spdk_json_write_object_end(w);
749 				}
750 				spdk_json_write_array_end(w);
751 			} else {
752 				SPDK_ERRLOG("Unexpected number of memory domains %d (should be %d)\n", i, rc);
753 			}
754 
755 			free(domains);
756 		} else {
757 			SPDK_ERRLOG("Memory allocation failed\n");
758 		}
759 	}
760 
761 	spdk_json_write_named_object_begin(w, "driver_specific");
762 	spdk_bdev_dump_info_json(bdev, w);
763 	spdk_json_write_object_end(w);
764 
765 	spdk_json_write_object_end(w);
766 
767 	return 0;
768 }
769 
770 struct rpc_bdev_get_bdevs {
771 	char		*name;
772 	uint64_t	timeout;
773 };
774 
775 struct rpc_bdev_get_bdevs_ctx {
776 	struct rpc_bdev_get_bdevs	rpc;
777 	struct spdk_jsonrpc_request	*request;
778 	struct spdk_poller		*poller;
779 	uint64_t			timeout_ticks;
780 };
781 
782 static void
783 free_rpc_bdev_get_bdevs(struct rpc_bdev_get_bdevs *r)
784 {
785 	free(r->name);
786 }
787 
788 static const struct spdk_json_object_decoder rpc_bdev_get_bdevs_decoders[] = {
789 	{"name", offsetof(struct rpc_bdev_get_bdevs, name), spdk_json_decode_string, true},
790 	{"timeout", offsetof(struct rpc_bdev_get_bdevs, timeout), spdk_json_decode_uint64, true},
791 };
792 
793 static void
794 rpc_bdev_get_bdev_cb(struct spdk_bdev_desc *desc, int rc, void *cb_arg)
795 {
796 	struct spdk_jsonrpc_request *request = cb_arg;
797 	struct spdk_json_write_ctx *w;
798 
799 	if (rc == 0) {
800 		w = spdk_jsonrpc_begin_result(request);
801 
802 		spdk_json_write_array_begin(w);
803 		rpc_dump_bdev_info(w, spdk_bdev_desc_get_bdev(desc));
804 		spdk_json_write_array_end(w);
805 		spdk_jsonrpc_end_result(request, w);
806 
807 		spdk_bdev_close(desc);
808 	} else {
809 		spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
810 	}
811 }
812 
813 static void
814 rpc_bdev_get_bdevs(struct spdk_jsonrpc_request *request,
815 		   const struct spdk_json_val *params)
816 {
817 	struct rpc_bdev_get_bdevs req = {};
818 	struct spdk_bdev_open_async_opts opts = {};
819 	struct spdk_json_write_ctx *w;
820 	int rc;
821 
822 	if (params && spdk_json_decode_object(params, rpc_bdev_get_bdevs_decoders,
823 					      SPDK_COUNTOF(rpc_bdev_get_bdevs_decoders),
824 					      &req)) {
825 		SPDK_ERRLOG("spdk_json_decode_object failed\n");
826 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
827 						 "spdk_json_decode_object failed");
828 		free_rpc_bdev_get_bdevs(&req);
829 		return;
830 	}
831 
832 	if (req.name) {
833 		opts.size = sizeof(opts);
834 		opts.timeout_ms = req.timeout;
835 
836 		rc = spdk_bdev_open_async(req.name, false, dummy_bdev_event_cb, NULL, &opts,
837 					  rpc_bdev_get_bdev_cb, request);
838 		if (rc != 0) {
839 			SPDK_ERRLOG("spdk_bdev_open_async failed for '%s': rc=%d\n", req.name, rc);
840 			spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
841 		}
842 
843 		free_rpc_bdev_get_bdevs(&req);
844 		return;
845 	}
846 
847 	free_rpc_bdev_get_bdevs(&req);
848 
849 	w = spdk_jsonrpc_begin_result(request);
850 	spdk_json_write_array_begin(w);
851 
852 	spdk_for_each_bdev(w, rpc_dump_bdev_info);
853 
854 	spdk_json_write_array_end(w);
855 
856 	spdk_jsonrpc_end_result(request, w);
857 }
858 SPDK_RPC_REGISTER("bdev_get_bdevs", rpc_bdev_get_bdevs, SPDK_RPC_RUNTIME)
859 
860 struct rpc_bdev_set_qd_sampling_period {
861 	char *name;
862 	uint64_t period;
863 };
864 
865 static void
866 free_rpc_bdev_set_qd_sampling_period(struct rpc_bdev_set_qd_sampling_period *r)
867 {
868 	free(r->name);
869 }
870 
871 static const struct spdk_json_object_decoder
872 	rpc_bdev_set_qd_sampling_period_decoders[] = {
873 	{"name", offsetof(struct rpc_bdev_set_qd_sampling_period, name), spdk_json_decode_string},
874 	{"period", offsetof(struct rpc_bdev_set_qd_sampling_period, period), spdk_json_decode_uint64},
875 };
876 
877 static void
878 rpc_bdev_set_qd_sampling_period(struct spdk_jsonrpc_request *request,
879 				const struct spdk_json_val *params)
880 {
881 	struct rpc_bdev_set_qd_sampling_period req = {0};
882 	struct spdk_bdev_desc *desc;
883 	int rc;
884 
885 	if (spdk_json_decode_object(params, rpc_bdev_set_qd_sampling_period_decoders,
886 				    SPDK_COUNTOF(rpc_bdev_set_qd_sampling_period_decoders),
887 				    &req)) {
888 		SPDK_ERRLOG("spdk_json_decode_object failed\n");
889 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
890 						 "spdk_json_decode_object failed");
891 		goto cleanup;
892 	}
893 
894 	rc = spdk_bdev_open_ext(req.name, false, dummy_bdev_event_cb, NULL, &desc);
895 	if (rc != 0) {
896 		SPDK_ERRLOG("Failed to open bdev '%s': %d\n", req.name, rc);
897 		spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
898 		goto cleanup;
899 	}
900 
901 	spdk_bdev_set_qd_sampling_period(spdk_bdev_desc_get_bdev(desc), req.period);
902 	spdk_jsonrpc_send_bool_response(request, true);
903 
904 	spdk_bdev_close(desc);
905 
906 cleanup:
907 	free_rpc_bdev_set_qd_sampling_period(&req);
908 }
909 SPDK_RPC_REGISTER("bdev_set_qd_sampling_period",
910 		  rpc_bdev_set_qd_sampling_period,
911 		  SPDK_RPC_RUNTIME)
912 
913 struct rpc_bdev_set_qos_limit {
914 	char		*name;
915 	uint64_t	limits[SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES];
916 };
917 
918 static void
919 free_rpc_bdev_set_qos_limit(struct rpc_bdev_set_qos_limit *r)
920 {
921 	free(r->name);
922 }
923 
924 static const struct spdk_json_object_decoder rpc_bdev_set_qos_limit_decoders[] = {
925 	{"name", offsetof(struct rpc_bdev_set_qos_limit, name), spdk_json_decode_string},
926 	{
927 		"rw_ios_per_sec", offsetof(struct rpc_bdev_set_qos_limit,
928 					   limits[SPDK_BDEV_QOS_RW_IOPS_RATE_LIMIT]),
929 		spdk_json_decode_uint64, true
930 	},
931 	{
932 		"rw_mbytes_per_sec", offsetof(struct rpc_bdev_set_qos_limit,
933 					      limits[SPDK_BDEV_QOS_RW_BPS_RATE_LIMIT]),
934 		spdk_json_decode_uint64, true
935 	},
936 	{
937 		"r_mbytes_per_sec", offsetof(struct rpc_bdev_set_qos_limit,
938 					     limits[SPDK_BDEV_QOS_R_BPS_RATE_LIMIT]),
939 		spdk_json_decode_uint64, true
940 	},
941 	{
942 		"w_mbytes_per_sec", offsetof(struct rpc_bdev_set_qos_limit,
943 					     limits[SPDK_BDEV_QOS_W_BPS_RATE_LIMIT]),
944 		spdk_json_decode_uint64, true
945 	},
946 };
947 
948 static void
949 rpc_bdev_set_qos_limit_complete(void *cb_arg, int status)
950 {
951 	struct spdk_jsonrpc_request *request = cb_arg;
952 
953 	if (status != 0) {
954 		spdk_jsonrpc_send_error_response_fmt(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
955 						     "Failed to configure rate limit: %s",
956 						     spdk_strerror(-status));
957 		return;
958 	}
959 
960 	spdk_jsonrpc_send_bool_response(request, true);
961 }
962 
963 static void
964 rpc_bdev_set_qos_limit(struct spdk_jsonrpc_request *request,
965 		       const struct spdk_json_val *params)
966 {
967 	struct rpc_bdev_set_qos_limit req = {NULL, {UINT64_MAX, UINT64_MAX, UINT64_MAX, UINT64_MAX}};
968 	struct spdk_bdev_desc *desc;
969 	int i, rc;
970 
971 	if (spdk_json_decode_object(params, rpc_bdev_set_qos_limit_decoders,
972 				    SPDK_COUNTOF(rpc_bdev_set_qos_limit_decoders),
973 				    &req)) {
974 		SPDK_ERRLOG("spdk_json_decode_object failed\n");
975 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
976 						 "spdk_json_decode_object failed");
977 		goto cleanup;
978 	}
979 
980 	rc = spdk_bdev_open_ext(req.name, false, dummy_bdev_event_cb, NULL, &desc);
981 	if (rc != 0) {
982 		SPDK_ERRLOG("Failed to open bdev '%s': %d\n", req.name, rc);
983 		spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
984 		goto cleanup;
985 	}
986 
987 	for (i = 0; i < SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES; i++) {
988 		if (req.limits[i] != UINT64_MAX) {
989 			break;
990 		}
991 	}
992 	if (i == SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES) {
993 		SPDK_ERRLOG("no rate limits specified\n");
994 		spdk_bdev_close(desc);
995 		spdk_jsonrpc_send_error_response(request, -EINVAL, "No rate limits specified");
996 		goto cleanup;
997 	}
998 
999 	spdk_bdev_set_qos_rate_limits(spdk_bdev_desc_get_bdev(desc), req.limits,
1000 				      rpc_bdev_set_qos_limit_complete, request);
1001 
1002 	spdk_bdev_close(desc);
1003 
1004 cleanup:
1005 	free_rpc_bdev_set_qos_limit(&req);
1006 }
1007 
1008 SPDK_RPC_REGISTER("bdev_set_qos_limit", rpc_bdev_set_qos_limit, SPDK_RPC_RUNTIME)
1009 
1010 /* SPDK_RPC_ENABLE_BDEV_HISTOGRAM */
1011 
1012 struct rpc_bdev_enable_histogram_request {
1013 	char *name;
1014 	bool enable;
1015 };
1016 
1017 static void
1018 free_rpc_bdev_enable_histogram_request(struct rpc_bdev_enable_histogram_request *r)
1019 {
1020 	free(r->name);
1021 }
1022 
1023 static const struct spdk_json_object_decoder rpc_bdev_enable_histogram_request_decoders[] = {
1024 	{"name", offsetof(struct rpc_bdev_enable_histogram_request, name), spdk_json_decode_string},
1025 	{"enable", offsetof(struct rpc_bdev_enable_histogram_request, enable), spdk_json_decode_bool},
1026 };
1027 
1028 static void
1029 bdev_histogram_status_cb(void *cb_arg, int status)
1030 {
1031 	struct spdk_jsonrpc_request *request = cb_arg;
1032 
1033 	if (status == 0) {
1034 		spdk_jsonrpc_send_bool_response(request, true);
1035 	} else {
1036 		spdk_jsonrpc_send_error_response(request, status, spdk_strerror(-status));
1037 	}
1038 }
1039 
1040 static void
1041 rpc_bdev_enable_histogram(struct spdk_jsonrpc_request *request,
1042 			  const struct spdk_json_val *params)
1043 {
1044 	struct rpc_bdev_enable_histogram_request req = {NULL};
1045 	struct spdk_bdev_desc *desc;
1046 	int rc;
1047 
1048 	if (spdk_json_decode_object(params, rpc_bdev_enable_histogram_request_decoders,
1049 				    SPDK_COUNTOF(rpc_bdev_enable_histogram_request_decoders),
1050 				    &req)) {
1051 		SPDK_ERRLOG("spdk_json_decode_object failed\n");
1052 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
1053 						 "spdk_json_decode_object failed");
1054 		goto cleanup;
1055 	}
1056 
1057 	rc = spdk_bdev_open_ext(req.name, false, dummy_bdev_event_cb, NULL, &desc);
1058 	if (rc != 0) {
1059 		spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
1060 		goto cleanup;
1061 	}
1062 
1063 	spdk_bdev_histogram_enable(spdk_bdev_desc_get_bdev(desc), bdev_histogram_status_cb,
1064 				   request, req.enable);
1065 
1066 	spdk_bdev_close(desc);
1067 
1068 cleanup:
1069 	free_rpc_bdev_enable_histogram_request(&req);
1070 }
1071 
1072 SPDK_RPC_REGISTER("bdev_enable_histogram", rpc_bdev_enable_histogram, SPDK_RPC_RUNTIME)
1073 
1074 /* SPDK_RPC_GET_BDEV_HISTOGRAM */
1075 
1076 struct rpc_bdev_get_histogram_request {
1077 	char *name;
1078 };
1079 
1080 static const struct spdk_json_object_decoder rpc_bdev_get_histogram_request_decoders[] = {
1081 	{"name", offsetof(struct rpc_bdev_get_histogram_request, name), spdk_json_decode_string}
1082 };
1083 
1084 static void
1085 free_rpc_bdev_get_histogram_request(struct rpc_bdev_get_histogram_request *r)
1086 {
1087 	free(r->name);
1088 }
1089 
1090 static void
1091 _rpc_bdev_histogram_data_cb(void *cb_arg, int status, struct spdk_histogram_data *histogram)
1092 {
1093 	struct spdk_jsonrpc_request *request = cb_arg;
1094 	struct spdk_json_write_ctx *w;
1095 	int rc;
1096 	char *encoded_histogram;
1097 	size_t src_len, dst_len;
1098 
1099 
1100 	if (status != 0) {
1101 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
1102 						 spdk_strerror(-status));
1103 		goto invalid;
1104 	}
1105 
1106 	src_len = SPDK_HISTOGRAM_NUM_BUCKETS(histogram) * sizeof(uint64_t);
1107 	dst_len = spdk_base64_get_encoded_strlen(src_len) + 1;
1108 
1109 	encoded_histogram = malloc(dst_len);
1110 	if (encoded_histogram == NULL) {
1111 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
1112 						 spdk_strerror(ENOMEM));
1113 		goto invalid;
1114 	}
1115 
1116 	rc = spdk_base64_encode(encoded_histogram, histogram->bucket, src_len);
1117 	if (rc != 0) {
1118 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
1119 						 spdk_strerror(-rc));
1120 		goto free_encoded_histogram;
1121 	}
1122 
1123 	w = spdk_jsonrpc_begin_result(request);
1124 	spdk_json_write_object_begin(w);
1125 	spdk_json_write_named_string(w, "histogram", encoded_histogram);
1126 	spdk_json_write_named_int64(w, "bucket_shift", histogram->bucket_shift);
1127 	spdk_json_write_named_int64(w, "tsc_rate", spdk_get_ticks_hz());
1128 	spdk_json_write_object_end(w);
1129 	spdk_jsonrpc_end_result(request, w);
1130 
1131 free_encoded_histogram:
1132 	free(encoded_histogram);
1133 invalid:
1134 	spdk_histogram_data_free(histogram);
1135 }
1136 
1137 static void
1138 rpc_bdev_get_histogram(struct spdk_jsonrpc_request *request,
1139 		       const struct spdk_json_val *params)
1140 {
1141 	struct rpc_bdev_get_histogram_request req = {NULL};
1142 	struct spdk_histogram_data *histogram;
1143 	struct spdk_bdev_desc *desc;
1144 	int rc;
1145 
1146 	if (spdk_json_decode_object(params, rpc_bdev_get_histogram_request_decoders,
1147 				    SPDK_COUNTOF(rpc_bdev_get_histogram_request_decoders),
1148 				    &req)) {
1149 		SPDK_ERRLOG("spdk_json_decode_object failed\n");
1150 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
1151 						 "spdk_json_decode_object failed");
1152 		goto cleanup;
1153 	}
1154 
1155 	rc = spdk_bdev_open_ext(req.name, false, dummy_bdev_event_cb, NULL, &desc);
1156 	if (rc != 0) {
1157 		spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
1158 		goto cleanup;
1159 	}
1160 
1161 	histogram = spdk_histogram_data_alloc();
1162 	if (histogram == NULL) {
1163 		spdk_bdev_close(desc);
1164 		spdk_jsonrpc_send_error_response(request, -ENOMEM, spdk_strerror(ENOMEM));
1165 		goto cleanup;
1166 	}
1167 
1168 	spdk_bdev_histogram_get(spdk_bdev_desc_get_bdev(desc), histogram,
1169 				_rpc_bdev_histogram_data_cb, request);
1170 
1171 	spdk_bdev_close(desc);
1172 
1173 cleanup:
1174 	free_rpc_bdev_get_histogram_request(&req);
1175 }
1176 
1177 SPDK_RPC_REGISTER("bdev_get_histogram", rpc_bdev_get_histogram, SPDK_RPC_RUNTIME)
1178