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