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