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