xref: /spdk/lib/bdev/bdev_rpc.c (revision 60982c759db49b4f4579f16e3b24df0725ba4b94)
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 	char uuid_str[SPDK_UUID_STRING_LEN];
655 	int i, rc;
656 
657 	spdk_json_write_object_begin(w);
658 
659 	spdk_json_write_named_string(w, "name", spdk_bdev_get_name(bdev));
660 
661 	spdk_json_write_named_array_begin(w, "aliases");
662 
663 	TAILQ_FOREACH(tmp, spdk_bdev_get_aliases(bdev), tailq) {
664 		spdk_json_write_string(w, tmp->alias.name);
665 	}
666 
667 	spdk_json_write_array_end(w);
668 
669 	spdk_json_write_named_string(w, "product_name", spdk_bdev_get_product_name(bdev));
670 
671 	spdk_json_write_named_uint32(w, "block_size", spdk_bdev_get_block_size(bdev));
672 
673 	spdk_json_write_named_uint64(w, "num_blocks", spdk_bdev_get_num_blocks(bdev));
674 
675 	spdk_uuid_fmt_lower(uuid_str, sizeof(uuid_str), &bdev->uuid);
676 	spdk_json_write_named_string(w, "uuid", uuid_str);
677 
678 	if (spdk_bdev_get_md_size(bdev) != 0) {
679 		spdk_json_write_named_uint32(w, "md_size", spdk_bdev_get_md_size(bdev));
680 		spdk_json_write_named_bool(w, "md_interleave", spdk_bdev_is_md_interleaved(bdev));
681 		spdk_json_write_named_uint32(w, "dif_type", spdk_bdev_get_dif_type(bdev));
682 		if (spdk_bdev_get_dif_type(bdev) != SPDK_DIF_DISABLE) {
683 			spdk_json_write_named_bool(w, "dif_is_head_of_md", spdk_bdev_is_dif_head_of_md(bdev));
684 			spdk_json_write_named_object_begin(w, "enabled_dif_check_types");
685 			spdk_json_write_named_bool(w, "reftag",
686 						   spdk_bdev_is_dif_check_enabled(bdev, SPDK_DIF_CHECK_TYPE_REFTAG));
687 			spdk_json_write_named_bool(w, "apptag",
688 						   spdk_bdev_is_dif_check_enabled(bdev, SPDK_DIF_CHECK_TYPE_APPTAG));
689 			spdk_json_write_named_bool(w, "guard",
690 						   spdk_bdev_is_dif_check_enabled(bdev, SPDK_DIF_CHECK_TYPE_GUARD));
691 			spdk_json_write_object_end(w);
692 		}
693 	}
694 
695 	spdk_json_write_named_object_begin(w, "assigned_rate_limits");
696 	spdk_bdev_get_qos_rate_limits(bdev, qos_limits);
697 	for (i = 0; i < SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES; i++) {
698 		spdk_json_write_named_uint64(w, spdk_bdev_get_qos_rpc_type(i), qos_limits[i]);
699 	}
700 	spdk_json_write_object_end(w);
701 
702 	spdk_json_write_named_bool(w, "claimed",
703 				   (bdev->internal.claim_type != SPDK_BDEV_CLAIM_NONE));
704 	if (bdev->internal.claim_type != SPDK_BDEV_CLAIM_NONE) {
705 		spdk_json_write_named_string(w, "claim_type",
706 					     spdk_bdev_claim_get_name(bdev->internal.claim_type));
707 	}
708 
709 	spdk_json_write_named_bool(w, "zoned", bdev->zoned);
710 	if (bdev->zoned) {
711 		spdk_json_write_named_uint64(w, "zone_size", bdev->zone_size);
712 		spdk_json_write_named_uint64(w, "max_open_zones", bdev->max_open_zones);
713 		spdk_json_write_named_uint64(w, "optimal_open_zones", bdev->optimal_open_zones);
714 	}
715 
716 	spdk_json_write_named_object_begin(w, "supported_io_types");
717 	spdk_json_write_named_bool(w, "read",
718 				   spdk_bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_READ));
719 	spdk_json_write_named_bool(w, "write",
720 				   spdk_bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_WRITE));
721 	spdk_json_write_named_bool(w, "unmap",
722 				   spdk_bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_UNMAP));
723 	spdk_json_write_named_bool(w, "write_zeroes",
724 				   spdk_bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_WRITE_ZEROES));
725 	spdk_json_write_named_bool(w, "flush",
726 				   spdk_bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_FLUSH));
727 	spdk_json_write_named_bool(w, "reset",
728 				   spdk_bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_RESET));
729 	spdk_json_write_named_bool(w, "compare",
730 				   spdk_bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_COMPARE));
731 	spdk_json_write_named_bool(w, "compare_and_write",
732 				   spdk_bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_COMPARE_AND_WRITE));
733 	spdk_json_write_named_bool(w, "abort",
734 				   spdk_bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_ABORT));
735 	spdk_json_write_named_bool(w, "nvme_admin",
736 				   spdk_bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_NVME_ADMIN));
737 	spdk_json_write_named_bool(w, "nvme_io",
738 				   spdk_bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_NVME_IO));
739 	spdk_json_write_object_end(w);
740 
741 	rc = spdk_bdev_get_memory_domains(bdev, NULL, 0);
742 	if (rc > 0) {
743 		domains = calloc(rc, sizeof(struct spdk_memory_domain *));
744 		if (domains) {
745 			i = spdk_bdev_get_memory_domains(bdev, domains, rc);
746 			if (i == rc) {
747 				spdk_json_write_named_array_begin(w, "memory_domains");
748 				for (i = 0; i < rc; i++) {
749 					spdk_json_write_object_begin(w);
750 					spdk_json_write_named_string(w, "dma_device_id", spdk_memory_domain_get_dma_device_id(domains[i]));
751 					spdk_json_write_named_int32(w, "dma_device_type",
752 								    spdk_memory_domain_get_dma_device_type(domains[i]));
753 					spdk_json_write_object_end(w);
754 				}
755 				spdk_json_write_array_end(w);
756 			} else {
757 				SPDK_ERRLOG("Unexpected number of memory domains %d (should be %d)\n", i, rc);
758 			}
759 
760 			free(domains);
761 		} else {
762 			SPDK_ERRLOG("Memory allocation failed\n");
763 		}
764 	}
765 
766 	spdk_json_write_named_object_begin(w, "driver_specific");
767 	spdk_bdev_dump_info_json(bdev, w);
768 	spdk_json_write_object_end(w);
769 
770 	spdk_json_write_object_end(w);
771 
772 	return 0;
773 }
774 
775 struct rpc_bdev_get_bdevs {
776 	char		*name;
777 	uint64_t	timeout;
778 };
779 
780 struct rpc_bdev_get_bdevs_ctx {
781 	struct rpc_bdev_get_bdevs	rpc;
782 	struct spdk_jsonrpc_request	*request;
783 	struct spdk_poller		*poller;
784 	uint64_t			timeout_ticks;
785 };
786 
787 static void
788 free_rpc_bdev_get_bdevs(struct rpc_bdev_get_bdevs *r)
789 {
790 	free(r->name);
791 }
792 
793 static const struct spdk_json_object_decoder rpc_bdev_get_bdevs_decoders[] = {
794 	{"name", offsetof(struct rpc_bdev_get_bdevs, name), spdk_json_decode_string, true},
795 	{"timeout", offsetof(struct rpc_bdev_get_bdevs, timeout), spdk_json_decode_uint64, true},
796 };
797 
798 static int
799 get_bdevs_poller(void *_ctx)
800 {
801 	struct rpc_bdev_get_bdevs_ctx *ctx = _ctx;
802 	struct spdk_json_write_ctx *w;
803 	struct spdk_bdev_desc *desc;
804 	int rc;
805 
806 	rc = spdk_bdev_open_ext(ctx->rpc.name, false, dummy_bdev_event_cb, NULL, &desc);
807 	if (rc != 0 && spdk_get_ticks() < ctx->timeout_ticks) {
808 		return SPDK_POLLER_BUSY;
809 	}
810 
811 	if (rc != 0) {
812 		SPDK_ERRLOG("Timed out while waiting for bdev '%s' to appear\n", ctx->rpc.name);
813 		spdk_jsonrpc_send_error_response(ctx->request, -ENODEV, spdk_strerror(ENODEV));
814 	} else {
815 		w = spdk_jsonrpc_begin_result(ctx->request);
816 		spdk_json_write_array_begin(w);
817 		rpc_dump_bdev_info(w, spdk_bdev_desc_get_bdev(desc));
818 		spdk_json_write_array_end(w);
819 		spdk_jsonrpc_end_result(ctx->request, w);
820 
821 		spdk_bdev_close(desc);
822 	}
823 
824 	spdk_poller_unregister(&ctx->poller);
825 	free_rpc_bdev_get_bdevs(&ctx->rpc);
826 	free(ctx);
827 
828 	return SPDK_POLLER_BUSY;
829 }
830 
831 static void
832 rpc_bdev_get_bdevs(struct spdk_jsonrpc_request *request,
833 		   const struct spdk_json_val *params)
834 {
835 	struct rpc_bdev_get_bdevs req = {};
836 	struct rpc_bdev_get_bdevs_ctx *ctx;
837 	struct spdk_json_write_ctx *w;
838 	struct spdk_bdev_desc *desc = NULL;
839 	int rc;
840 
841 	if (params && spdk_json_decode_object(params, rpc_bdev_get_bdevs_decoders,
842 					      SPDK_COUNTOF(rpc_bdev_get_bdevs_decoders),
843 					      &req)) {
844 		SPDK_ERRLOG("spdk_json_decode_object failed\n");
845 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
846 						 "spdk_json_decode_object failed");
847 		free_rpc_bdev_get_bdevs(&req);
848 		return;
849 	}
850 
851 	if (req.name) {
852 		rc = spdk_bdev_open_ext(req.name, false, dummy_bdev_event_cb, NULL, &desc);
853 		if (rc != 0) {
854 			if (req.timeout == 0) {
855 				SPDK_ERRLOG("bdev '%s' does not exist\n", req.name);
856 				spdk_jsonrpc_send_error_response(request, -ENODEV, spdk_strerror(ENODEV));
857 				free_rpc_bdev_get_bdevs(&req);
858 				return;
859 			}
860 
861 			ctx = calloc(1, sizeof(*ctx));
862 			if (ctx == NULL) {
863 				SPDK_ERRLOG("Failed to allocate bdev_get_bdevs context\n");
864 				spdk_jsonrpc_send_error_response(request, -ENOMEM, spdk_strerror(ENOMEM));
865 				free_rpc_bdev_get_bdevs(&req);
866 				return;
867 			}
868 
869 			ctx->poller = SPDK_POLLER_REGISTER(get_bdevs_poller, ctx, 10 * 1000);
870 			if (ctx->poller == NULL) {
871 				SPDK_ERRLOG("Failed to register bdev_get_bdevs poller\n");
872 				spdk_jsonrpc_send_error_response(request, -ENOMEM, spdk_strerror(ENOMEM));
873 				free_rpc_bdev_get_bdevs(&req);
874 				free(ctx);
875 				return;
876 			}
877 
878 			memcpy(&ctx->rpc, &req, sizeof(req));
879 			ctx->timeout_ticks = spdk_get_ticks() + req.timeout *
880 					     spdk_get_ticks_hz() / 1000ull;
881 			ctx->request = request;
882 			return;
883 		}
884 	}
885 
886 	free_rpc_bdev_get_bdevs(&req);
887 	w = spdk_jsonrpc_begin_result(request);
888 	spdk_json_write_array_begin(w);
889 
890 	if (desc != NULL) {
891 		rpc_dump_bdev_info(w, spdk_bdev_desc_get_bdev(desc));
892 		spdk_bdev_close(desc);
893 	} else {
894 		spdk_for_each_bdev(w, rpc_dump_bdev_info);
895 	}
896 
897 	spdk_json_write_array_end(w);
898 
899 	spdk_jsonrpc_end_result(request, w);
900 }
901 SPDK_RPC_REGISTER("bdev_get_bdevs", rpc_bdev_get_bdevs, SPDK_RPC_RUNTIME)
902 
903 struct rpc_bdev_set_qd_sampling_period {
904 	char *name;
905 	uint64_t period;
906 };
907 
908 static void
909 free_rpc_bdev_set_qd_sampling_period(struct rpc_bdev_set_qd_sampling_period *r)
910 {
911 	free(r->name);
912 }
913 
914 static const struct spdk_json_object_decoder
915 	rpc_bdev_set_qd_sampling_period_decoders[] = {
916 	{"name", offsetof(struct rpc_bdev_set_qd_sampling_period, name), spdk_json_decode_string},
917 	{"period", offsetof(struct rpc_bdev_set_qd_sampling_period, period), spdk_json_decode_uint64},
918 };
919 
920 static void
921 rpc_bdev_set_qd_sampling_period(struct spdk_jsonrpc_request *request,
922 				const struct spdk_json_val *params)
923 {
924 	struct rpc_bdev_set_qd_sampling_period req = {0};
925 	struct spdk_bdev_desc *desc;
926 	int rc;
927 
928 	if (spdk_json_decode_object(params, rpc_bdev_set_qd_sampling_period_decoders,
929 				    SPDK_COUNTOF(rpc_bdev_set_qd_sampling_period_decoders),
930 				    &req)) {
931 		SPDK_ERRLOG("spdk_json_decode_object failed\n");
932 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
933 						 "spdk_json_decode_object failed");
934 		goto cleanup;
935 	}
936 
937 	rc = spdk_bdev_open_ext(req.name, false, dummy_bdev_event_cb, NULL, &desc);
938 	if (rc != 0) {
939 		SPDK_ERRLOG("Failed to open bdev '%s': %d\n", req.name, rc);
940 		spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
941 		goto cleanup;
942 	}
943 
944 	spdk_bdev_set_qd_sampling_period(spdk_bdev_desc_get_bdev(desc), req.period);
945 	spdk_jsonrpc_send_bool_response(request, true);
946 
947 	spdk_bdev_close(desc);
948 
949 cleanup:
950 	free_rpc_bdev_set_qd_sampling_period(&req);
951 }
952 SPDK_RPC_REGISTER("bdev_set_qd_sampling_period",
953 		  rpc_bdev_set_qd_sampling_period,
954 		  SPDK_RPC_RUNTIME)
955 
956 struct rpc_bdev_set_qos_limit {
957 	char		*name;
958 	uint64_t	limits[SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES];
959 };
960 
961 static void
962 free_rpc_bdev_set_qos_limit(struct rpc_bdev_set_qos_limit *r)
963 {
964 	free(r->name);
965 }
966 
967 static const struct spdk_json_object_decoder rpc_bdev_set_qos_limit_decoders[] = {
968 	{"name", offsetof(struct rpc_bdev_set_qos_limit, name), spdk_json_decode_string},
969 	{
970 		"rw_ios_per_sec", offsetof(struct rpc_bdev_set_qos_limit,
971 					   limits[SPDK_BDEV_QOS_RW_IOPS_RATE_LIMIT]),
972 		spdk_json_decode_uint64, true
973 	},
974 	{
975 		"rw_mbytes_per_sec", offsetof(struct rpc_bdev_set_qos_limit,
976 					      limits[SPDK_BDEV_QOS_RW_BPS_RATE_LIMIT]),
977 		spdk_json_decode_uint64, true
978 	},
979 	{
980 		"r_mbytes_per_sec", offsetof(struct rpc_bdev_set_qos_limit,
981 					     limits[SPDK_BDEV_QOS_R_BPS_RATE_LIMIT]),
982 		spdk_json_decode_uint64, true
983 	},
984 	{
985 		"w_mbytes_per_sec", offsetof(struct rpc_bdev_set_qos_limit,
986 					     limits[SPDK_BDEV_QOS_W_BPS_RATE_LIMIT]),
987 		spdk_json_decode_uint64, true
988 	},
989 };
990 
991 static void
992 rpc_bdev_set_qos_limit_complete(void *cb_arg, int status)
993 {
994 	struct spdk_jsonrpc_request *request = cb_arg;
995 
996 	if (status != 0) {
997 		spdk_jsonrpc_send_error_response_fmt(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
998 						     "Failed to configure rate limit: %s",
999 						     spdk_strerror(-status));
1000 		return;
1001 	}
1002 
1003 	spdk_jsonrpc_send_bool_response(request, true);
1004 }
1005 
1006 static void
1007 rpc_bdev_set_qos_limit(struct spdk_jsonrpc_request *request,
1008 		       const struct spdk_json_val *params)
1009 {
1010 	struct rpc_bdev_set_qos_limit req = {NULL, {UINT64_MAX, UINT64_MAX, UINT64_MAX, UINT64_MAX}};
1011 	struct spdk_bdev_desc *desc;
1012 	int i, rc;
1013 
1014 	if (spdk_json_decode_object(params, rpc_bdev_set_qos_limit_decoders,
1015 				    SPDK_COUNTOF(rpc_bdev_set_qos_limit_decoders),
1016 				    &req)) {
1017 		SPDK_ERRLOG("spdk_json_decode_object failed\n");
1018 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
1019 						 "spdk_json_decode_object failed");
1020 		goto cleanup;
1021 	}
1022 
1023 	rc = spdk_bdev_open_ext(req.name, false, dummy_bdev_event_cb, NULL, &desc);
1024 	if (rc != 0) {
1025 		SPDK_ERRLOG("Failed to open bdev '%s': %d\n", req.name, rc);
1026 		spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
1027 		goto cleanup;
1028 	}
1029 
1030 	for (i = 0; i < SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES; i++) {
1031 		if (req.limits[i] != UINT64_MAX) {
1032 			break;
1033 		}
1034 	}
1035 	if (i == SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES) {
1036 		SPDK_ERRLOG("no rate limits specified\n");
1037 		spdk_bdev_close(desc);
1038 		spdk_jsonrpc_send_error_response(request, -EINVAL, "No rate limits specified");
1039 		goto cleanup;
1040 	}
1041 
1042 	spdk_bdev_set_qos_rate_limits(spdk_bdev_desc_get_bdev(desc), req.limits,
1043 				      rpc_bdev_set_qos_limit_complete, request);
1044 
1045 	spdk_bdev_close(desc);
1046 
1047 cleanup:
1048 	free_rpc_bdev_set_qos_limit(&req);
1049 }
1050 
1051 SPDK_RPC_REGISTER("bdev_set_qos_limit", rpc_bdev_set_qos_limit, SPDK_RPC_RUNTIME)
1052 
1053 /* SPDK_RPC_ENABLE_BDEV_HISTOGRAM */
1054 
1055 struct rpc_bdev_enable_histogram_request {
1056 	char *name;
1057 	bool enable;
1058 };
1059 
1060 static void
1061 free_rpc_bdev_enable_histogram_request(struct rpc_bdev_enable_histogram_request *r)
1062 {
1063 	free(r->name);
1064 }
1065 
1066 static const struct spdk_json_object_decoder rpc_bdev_enable_histogram_request_decoders[] = {
1067 	{"name", offsetof(struct rpc_bdev_enable_histogram_request, name), spdk_json_decode_string},
1068 	{"enable", offsetof(struct rpc_bdev_enable_histogram_request, enable), spdk_json_decode_bool},
1069 };
1070 
1071 static void
1072 bdev_histogram_status_cb(void *cb_arg, int status)
1073 {
1074 	struct spdk_jsonrpc_request *request = cb_arg;
1075 
1076 	if (status == 0) {
1077 		spdk_jsonrpc_send_bool_response(request, true);
1078 	} else {
1079 		spdk_jsonrpc_send_error_response(request, status, spdk_strerror(-status));
1080 	}
1081 }
1082 
1083 static void
1084 rpc_bdev_enable_histogram(struct spdk_jsonrpc_request *request,
1085 			  const struct spdk_json_val *params)
1086 {
1087 	struct rpc_bdev_enable_histogram_request req = {NULL};
1088 	struct spdk_bdev_desc *desc;
1089 	int rc;
1090 
1091 	if (spdk_json_decode_object(params, rpc_bdev_enable_histogram_request_decoders,
1092 				    SPDK_COUNTOF(rpc_bdev_enable_histogram_request_decoders),
1093 				    &req)) {
1094 		SPDK_ERRLOG("spdk_json_decode_object failed\n");
1095 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
1096 						 "spdk_json_decode_object failed");
1097 		goto cleanup;
1098 	}
1099 
1100 	rc = spdk_bdev_open_ext(req.name, false, dummy_bdev_event_cb, NULL, &desc);
1101 	if (rc != 0) {
1102 		spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
1103 		goto cleanup;
1104 	}
1105 
1106 	spdk_bdev_histogram_enable(spdk_bdev_desc_get_bdev(desc), bdev_histogram_status_cb,
1107 				   request, req.enable);
1108 
1109 	spdk_bdev_close(desc);
1110 
1111 cleanup:
1112 	free_rpc_bdev_enable_histogram_request(&req);
1113 }
1114 
1115 SPDK_RPC_REGISTER("bdev_enable_histogram", rpc_bdev_enable_histogram, SPDK_RPC_RUNTIME)
1116 
1117 /* SPDK_RPC_GET_BDEV_HISTOGRAM */
1118 
1119 struct rpc_bdev_get_histogram_request {
1120 	char *name;
1121 };
1122 
1123 static const struct spdk_json_object_decoder rpc_bdev_get_histogram_request_decoders[] = {
1124 	{"name", offsetof(struct rpc_bdev_get_histogram_request, name), spdk_json_decode_string}
1125 };
1126 
1127 static void
1128 free_rpc_bdev_get_histogram_request(struct rpc_bdev_get_histogram_request *r)
1129 {
1130 	free(r->name);
1131 }
1132 
1133 static void
1134 _rpc_bdev_histogram_data_cb(void *cb_arg, int status, struct spdk_histogram_data *histogram)
1135 {
1136 	struct spdk_jsonrpc_request *request = cb_arg;
1137 	struct spdk_json_write_ctx *w;
1138 	int rc;
1139 	char *encoded_histogram;
1140 	size_t src_len, dst_len;
1141 
1142 
1143 	if (status != 0) {
1144 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
1145 						 spdk_strerror(-status));
1146 		goto invalid;
1147 	}
1148 
1149 	src_len = SPDK_HISTOGRAM_NUM_BUCKETS(histogram) * sizeof(uint64_t);
1150 	dst_len = spdk_base64_get_encoded_strlen(src_len) + 1;
1151 
1152 	encoded_histogram = malloc(dst_len);
1153 	if (encoded_histogram == NULL) {
1154 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
1155 						 spdk_strerror(ENOMEM));
1156 		goto invalid;
1157 	}
1158 
1159 	rc = spdk_base64_encode(encoded_histogram, histogram->bucket, src_len);
1160 	if (rc != 0) {
1161 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
1162 						 spdk_strerror(-rc));
1163 		goto free_encoded_histogram;
1164 	}
1165 
1166 	w = spdk_jsonrpc_begin_result(request);
1167 	spdk_json_write_object_begin(w);
1168 	spdk_json_write_named_string(w, "histogram", encoded_histogram);
1169 	spdk_json_write_named_int64(w, "bucket_shift", histogram->bucket_shift);
1170 	spdk_json_write_named_int64(w, "tsc_rate", spdk_get_ticks_hz());
1171 	spdk_json_write_object_end(w);
1172 	spdk_jsonrpc_end_result(request, w);
1173 
1174 free_encoded_histogram:
1175 	free(encoded_histogram);
1176 invalid:
1177 	spdk_histogram_data_free(histogram);
1178 }
1179 
1180 static void
1181 rpc_bdev_get_histogram(struct spdk_jsonrpc_request *request,
1182 		       const struct spdk_json_val *params)
1183 {
1184 	struct rpc_bdev_get_histogram_request req = {NULL};
1185 	struct spdk_histogram_data *histogram;
1186 	struct spdk_bdev_desc *desc;
1187 	int rc;
1188 
1189 	if (spdk_json_decode_object(params, rpc_bdev_get_histogram_request_decoders,
1190 				    SPDK_COUNTOF(rpc_bdev_get_histogram_request_decoders),
1191 				    &req)) {
1192 		SPDK_ERRLOG("spdk_json_decode_object failed\n");
1193 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
1194 						 "spdk_json_decode_object failed");
1195 		goto cleanup;
1196 	}
1197 
1198 	rc = spdk_bdev_open_ext(req.name, false, dummy_bdev_event_cb, NULL, &desc);
1199 	if (rc != 0) {
1200 		spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
1201 		goto cleanup;
1202 	}
1203 
1204 	histogram = spdk_histogram_data_alloc();
1205 	if (histogram == NULL) {
1206 		spdk_bdev_close(desc);
1207 		spdk_jsonrpc_send_error_response(request, -ENOMEM, spdk_strerror(ENOMEM));
1208 		goto cleanup;
1209 	}
1210 
1211 	spdk_bdev_histogram_get(spdk_bdev_desc_get_bdev(desc), histogram,
1212 				_rpc_bdev_histogram_data_cb, request);
1213 
1214 	spdk_bdev_close(desc);
1215 
1216 cleanup:
1217 	free_rpc_bdev_get_histogram_request(&req);
1218 }
1219 
1220 SPDK_RPC_REGISTER("bdev_get_histogram", rpc_bdev_get_histogram, SPDK_RPC_RUNTIME)
1221