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