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