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