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