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