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 char uuid_str[SPDK_UUID_STRING_LEN]; 667 int i, rc; 668 669 spdk_json_write_object_begin(w); 670 671 spdk_json_write_named_string(w, "name", spdk_bdev_get_name(bdev)); 672 673 spdk_json_write_named_array_begin(w, "aliases"); 674 675 TAILQ_FOREACH(tmp, spdk_bdev_get_aliases(bdev), tailq) { 676 spdk_json_write_string(w, tmp->alias.name); 677 } 678 679 spdk_json_write_array_end(w); 680 681 spdk_json_write_named_string(w, "product_name", spdk_bdev_get_product_name(bdev)); 682 683 spdk_json_write_named_uint32(w, "block_size", spdk_bdev_get_block_size(bdev)); 684 685 spdk_json_write_named_uint64(w, "num_blocks", spdk_bdev_get_num_blocks(bdev)); 686 687 spdk_uuid_fmt_lower(uuid_str, sizeof(uuid_str), &bdev->uuid); 688 spdk_json_write_named_string(w, "uuid", uuid_str); 689 690 if (spdk_bdev_get_md_size(bdev) != 0) { 691 spdk_json_write_named_uint32(w, "md_size", spdk_bdev_get_md_size(bdev)); 692 spdk_json_write_named_bool(w, "md_interleave", spdk_bdev_is_md_interleaved(bdev)); 693 spdk_json_write_named_uint32(w, "dif_type", spdk_bdev_get_dif_type(bdev)); 694 if (spdk_bdev_get_dif_type(bdev) != SPDK_DIF_DISABLE) { 695 spdk_json_write_named_bool(w, "dif_is_head_of_md", spdk_bdev_is_dif_head_of_md(bdev)); 696 spdk_json_write_named_object_begin(w, "enabled_dif_check_types"); 697 spdk_json_write_named_bool(w, "reftag", 698 spdk_bdev_is_dif_check_enabled(bdev, SPDK_DIF_CHECK_TYPE_REFTAG)); 699 spdk_json_write_named_bool(w, "apptag", 700 spdk_bdev_is_dif_check_enabled(bdev, SPDK_DIF_CHECK_TYPE_APPTAG)); 701 spdk_json_write_named_bool(w, "guard", 702 spdk_bdev_is_dif_check_enabled(bdev, SPDK_DIF_CHECK_TYPE_GUARD)); 703 spdk_json_write_object_end(w); 704 } 705 } 706 707 spdk_json_write_named_object_begin(w, "assigned_rate_limits"); 708 spdk_bdev_get_qos_rate_limits(bdev, qos_limits); 709 for (i = 0; i < SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES; i++) { 710 spdk_json_write_named_uint64(w, spdk_bdev_get_qos_rpc_type(i), qos_limits[i]); 711 } 712 spdk_json_write_object_end(w); 713 714 spdk_json_write_named_bool(w, "claimed", 715 (bdev->internal.claim_type != SPDK_BDEV_CLAIM_NONE)); 716 if (bdev->internal.claim_type != SPDK_BDEV_CLAIM_NONE) { 717 spdk_json_write_named_string(w, "claim_type", 718 spdk_bdev_claim_get_name(bdev->internal.claim_type)); 719 } 720 721 spdk_json_write_named_bool(w, "zoned", bdev->zoned); 722 if (bdev->zoned) { 723 spdk_json_write_named_uint64(w, "zone_size", bdev->zone_size); 724 spdk_json_write_named_uint64(w, "max_open_zones", bdev->max_open_zones); 725 spdk_json_write_named_uint64(w, "optimal_open_zones", bdev->optimal_open_zones); 726 } 727 728 spdk_json_write_named_object_begin(w, "supported_io_types"); 729 spdk_json_write_named_bool(w, "read", 730 spdk_bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_READ)); 731 spdk_json_write_named_bool(w, "write", 732 spdk_bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_WRITE)); 733 spdk_json_write_named_bool(w, "unmap", 734 spdk_bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_UNMAP)); 735 spdk_json_write_named_bool(w, "write_zeroes", 736 spdk_bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_WRITE_ZEROES)); 737 spdk_json_write_named_bool(w, "flush", 738 spdk_bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_FLUSH)); 739 spdk_json_write_named_bool(w, "reset", 740 spdk_bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_RESET)); 741 spdk_json_write_named_bool(w, "compare", 742 spdk_bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_COMPARE)); 743 spdk_json_write_named_bool(w, "compare_and_write", 744 spdk_bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_COMPARE_AND_WRITE)); 745 spdk_json_write_named_bool(w, "abort", 746 spdk_bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_ABORT)); 747 spdk_json_write_named_bool(w, "nvme_admin", 748 spdk_bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_NVME_ADMIN)); 749 spdk_json_write_named_bool(w, "nvme_io", 750 spdk_bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_NVME_IO)); 751 spdk_json_write_object_end(w); 752 753 rc = spdk_bdev_get_memory_domains(bdev, NULL, 0); 754 if (rc > 0) { 755 domains = calloc(rc, sizeof(struct spdk_memory_domain *)); 756 if (domains) { 757 i = spdk_bdev_get_memory_domains(bdev, domains, rc); 758 if (i == rc) { 759 spdk_json_write_named_array_begin(w, "memory_domains"); 760 for (i = 0; i < rc; i++) { 761 spdk_json_write_object_begin(w); 762 spdk_json_write_named_string(w, "dma_device_id", spdk_memory_domain_get_dma_device_id(domains[i])); 763 spdk_json_write_named_int32(w, "dma_device_type", 764 spdk_memory_domain_get_dma_device_type(domains[i])); 765 spdk_json_write_object_end(w); 766 } 767 spdk_json_write_array_end(w); 768 } else { 769 SPDK_ERRLOG("Unexpected number of memory domains %d (should be %d)\n", i, rc); 770 } 771 772 free(domains); 773 } else { 774 SPDK_ERRLOG("Memory allocation failed\n"); 775 } 776 } 777 778 spdk_json_write_named_object_begin(w, "driver_specific"); 779 spdk_bdev_dump_info_json(bdev, w); 780 spdk_json_write_object_end(w); 781 782 spdk_json_write_object_end(w); 783 784 return 0; 785 } 786 787 struct rpc_bdev_get_bdevs { 788 char *name; 789 uint64_t timeout; 790 }; 791 792 struct rpc_bdev_get_bdevs_ctx { 793 struct rpc_bdev_get_bdevs rpc; 794 struct spdk_jsonrpc_request *request; 795 struct spdk_poller *poller; 796 uint64_t timeout_ticks; 797 }; 798 799 static void 800 free_rpc_bdev_get_bdevs(struct rpc_bdev_get_bdevs *r) 801 { 802 free(r->name); 803 } 804 805 static const struct spdk_json_object_decoder rpc_bdev_get_bdevs_decoders[] = { 806 {"name", offsetof(struct rpc_bdev_get_bdevs, name), spdk_json_decode_string, true}, 807 {"timeout", offsetof(struct rpc_bdev_get_bdevs, timeout), spdk_json_decode_uint64, true}, 808 }; 809 810 static int 811 get_bdevs_poller(void *_ctx) 812 { 813 struct rpc_bdev_get_bdevs_ctx *ctx = _ctx; 814 struct spdk_json_write_ctx *w; 815 struct spdk_bdev_desc *desc; 816 int rc; 817 818 rc = spdk_bdev_open_ext(ctx->rpc.name, false, dummy_bdev_event_cb, NULL, &desc); 819 if (rc != 0 && spdk_get_ticks() < ctx->timeout_ticks) { 820 return SPDK_POLLER_BUSY; 821 } 822 823 if (rc != 0) { 824 SPDK_ERRLOG("Timed out while waiting for bdev '%s' to appear\n", ctx->rpc.name); 825 spdk_jsonrpc_send_error_response(ctx->request, -ENODEV, spdk_strerror(ENODEV)); 826 } else { 827 w = spdk_jsonrpc_begin_result(ctx->request); 828 spdk_json_write_array_begin(w); 829 rpc_dump_bdev_info(w, spdk_bdev_desc_get_bdev(desc)); 830 spdk_json_write_array_end(w); 831 spdk_jsonrpc_end_result(ctx->request, w); 832 833 spdk_bdev_close(desc); 834 } 835 836 spdk_poller_unregister(&ctx->poller); 837 free_rpc_bdev_get_bdevs(&ctx->rpc); 838 free(ctx); 839 840 return SPDK_POLLER_BUSY; 841 } 842 843 static void 844 rpc_bdev_get_bdevs(struct spdk_jsonrpc_request *request, 845 const struct spdk_json_val *params) 846 { 847 struct rpc_bdev_get_bdevs req = {}; 848 struct rpc_bdev_get_bdevs_ctx *ctx; 849 struct spdk_json_write_ctx *w; 850 struct spdk_bdev_desc *desc = NULL; 851 int rc; 852 853 if (params && spdk_json_decode_object(params, rpc_bdev_get_bdevs_decoders, 854 SPDK_COUNTOF(rpc_bdev_get_bdevs_decoders), 855 &req)) { 856 SPDK_ERRLOG("spdk_json_decode_object failed\n"); 857 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 858 "spdk_json_decode_object failed"); 859 free_rpc_bdev_get_bdevs(&req); 860 return; 861 } 862 863 if (req.name) { 864 rc = spdk_bdev_open_ext(req.name, false, dummy_bdev_event_cb, NULL, &desc); 865 if (rc != 0) { 866 if (req.timeout == 0) { 867 SPDK_ERRLOG("bdev '%s' does not exist\n", req.name); 868 spdk_jsonrpc_send_error_response(request, -ENODEV, spdk_strerror(ENODEV)); 869 free_rpc_bdev_get_bdevs(&req); 870 return; 871 } 872 873 ctx = calloc(1, sizeof(*ctx)); 874 if (ctx == NULL) { 875 SPDK_ERRLOG("Failed to allocate bdev_get_bdevs context\n"); 876 spdk_jsonrpc_send_error_response(request, -ENOMEM, spdk_strerror(ENOMEM)); 877 free_rpc_bdev_get_bdevs(&req); 878 return; 879 } 880 881 ctx->poller = SPDK_POLLER_REGISTER(get_bdevs_poller, ctx, 10 * 1000); 882 if (ctx->poller == NULL) { 883 SPDK_ERRLOG("Failed to register bdev_get_bdevs poller\n"); 884 spdk_jsonrpc_send_error_response(request, -ENOMEM, spdk_strerror(ENOMEM)); 885 free_rpc_bdev_get_bdevs(&req); 886 free(ctx); 887 return; 888 } 889 890 memcpy(&ctx->rpc, &req, sizeof(req)); 891 ctx->timeout_ticks = spdk_get_ticks() + req.timeout * 892 spdk_get_ticks_hz() / 1000ull; 893 ctx->request = request; 894 return; 895 } 896 } 897 898 free_rpc_bdev_get_bdevs(&req); 899 w = spdk_jsonrpc_begin_result(request); 900 spdk_json_write_array_begin(w); 901 902 if (desc != NULL) { 903 rpc_dump_bdev_info(w, spdk_bdev_desc_get_bdev(desc)); 904 spdk_bdev_close(desc); 905 } else { 906 spdk_for_each_bdev(w, rpc_dump_bdev_info); 907 } 908 909 spdk_json_write_array_end(w); 910 911 spdk_jsonrpc_end_result(request, w); 912 } 913 SPDK_RPC_REGISTER("bdev_get_bdevs", rpc_bdev_get_bdevs, SPDK_RPC_RUNTIME) 914 915 struct rpc_bdev_set_qd_sampling_period { 916 char *name; 917 uint64_t period; 918 }; 919 920 static void 921 free_rpc_bdev_set_qd_sampling_period(struct rpc_bdev_set_qd_sampling_period *r) 922 { 923 free(r->name); 924 } 925 926 static const struct spdk_json_object_decoder 927 rpc_bdev_set_qd_sampling_period_decoders[] = { 928 {"name", offsetof(struct rpc_bdev_set_qd_sampling_period, name), spdk_json_decode_string}, 929 {"period", offsetof(struct rpc_bdev_set_qd_sampling_period, period), spdk_json_decode_uint64}, 930 }; 931 932 static void 933 rpc_bdev_set_qd_sampling_period(struct spdk_jsonrpc_request *request, 934 const struct spdk_json_val *params) 935 { 936 struct rpc_bdev_set_qd_sampling_period req = {0}; 937 struct spdk_bdev_desc *desc; 938 int rc; 939 940 if (spdk_json_decode_object(params, rpc_bdev_set_qd_sampling_period_decoders, 941 SPDK_COUNTOF(rpc_bdev_set_qd_sampling_period_decoders), 942 &req)) { 943 SPDK_ERRLOG("spdk_json_decode_object failed\n"); 944 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 945 "spdk_json_decode_object failed"); 946 goto cleanup; 947 } 948 949 rc = spdk_bdev_open_ext(req.name, false, dummy_bdev_event_cb, NULL, &desc); 950 if (rc != 0) { 951 SPDK_ERRLOG("Failed to open bdev '%s': %d\n", req.name, rc); 952 spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc)); 953 goto cleanup; 954 } 955 956 spdk_bdev_set_qd_sampling_period(spdk_bdev_desc_get_bdev(desc), req.period); 957 spdk_jsonrpc_send_bool_response(request, true); 958 959 spdk_bdev_close(desc); 960 961 cleanup: 962 free_rpc_bdev_set_qd_sampling_period(&req); 963 } 964 SPDK_RPC_REGISTER("bdev_set_qd_sampling_period", 965 rpc_bdev_set_qd_sampling_period, 966 SPDK_RPC_RUNTIME) 967 968 struct rpc_bdev_set_qos_limit { 969 char *name; 970 uint64_t limits[SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES]; 971 }; 972 973 static void 974 free_rpc_bdev_set_qos_limit(struct rpc_bdev_set_qos_limit *r) 975 { 976 free(r->name); 977 } 978 979 static const struct spdk_json_object_decoder rpc_bdev_set_qos_limit_decoders[] = { 980 {"name", offsetof(struct rpc_bdev_set_qos_limit, name), spdk_json_decode_string}, 981 { 982 "rw_ios_per_sec", offsetof(struct rpc_bdev_set_qos_limit, 983 limits[SPDK_BDEV_QOS_RW_IOPS_RATE_LIMIT]), 984 spdk_json_decode_uint64, true 985 }, 986 { 987 "rw_mbytes_per_sec", offsetof(struct rpc_bdev_set_qos_limit, 988 limits[SPDK_BDEV_QOS_RW_BPS_RATE_LIMIT]), 989 spdk_json_decode_uint64, true 990 }, 991 { 992 "r_mbytes_per_sec", offsetof(struct rpc_bdev_set_qos_limit, 993 limits[SPDK_BDEV_QOS_R_BPS_RATE_LIMIT]), 994 spdk_json_decode_uint64, true 995 }, 996 { 997 "w_mbytes_per_sec", offsetof(struct rpc_bdev_set_qos_limit, 998 limits[SPDK_BDEV_QOS_W_BPS_RATE_LIMIT]), 999 spdk_json_decode_uint64, true 1000 }, 1001 }; 1002 1003 static void 1004 rpc_bdev_set_qos_limit_complete(void *cb_arg, int status) 1005 { 1006 struct spdk_jsonrpc_request *request = cb_arg; 1007 1008 if (status != 0) { 1009 spdk_jsonrpc_send_error_response_fmt(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, 1010 "Failed to configure rate limit: %s", 1011 spdk_strerror(-status)); 1012 return; 1013 } 1014 1015 spdk_jsonrpc_send_bool_response(request, true); 1016 } 1017 1018 static void 1019 rpc_bdev_set_qos_limit(struct spdk_jsonrpc_request *request, 1020 const struct spdk_json_val *params) 1021 { 1022 struct rpc_bdev_set_qos_limit req = {NULL, {UINT64_MAX, UINT64_MAX, UINT64_MAX, UINT64_MAX}}; 1023 struct spdk_bdev_desc *desc; 1024 int i, rc; 1025 1026 if (spdk_json_decode_object(params, rpc_bdev_set_qos_limit_decoders, 1027 SPDK_COUNTOF(rpc_bdev_set_qos_limit_decoders), 1028 &req)) { 1029 SPDK_ERRLOG("spdk_json_decode_object failed\n"); 1030 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 1031 "spdk_json_decode_object failed"); 1032 goto cleanup; 1033 } 1034 1035 rc = spdk_bdev_open_ext(req.name, false, dummy_bdev_event_cb, NULL, &desc); 1036 if (rc != 0) { 1037 SPDK_ERRLOG("Failed to open bdev '%s': %d\n", req.name, rc); 1038 spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc)); 1039 goto cleanup; 1040 } 1041 1042 for (i = 0; i < SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES; i++) { 1043 if (req.limits[i] != UINT64_MAX) { 1044 break; 1045 } 1046 } 1047 if (i == SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES) { 1048 SPDK_ERRLOG("no rate limits specified\n"); 1049 spdk_bdev_close(desc); 1050 spdk_jsonrpc_send_error_response(request, -EINVAL, "No rate limits specified"); 1051 goto cleanup; 1052 } 1053 1054 spdk_bdev_set_qos_rate_limits(spdk_bdev_desc_get_bdev(desc), req.limits, 1055 rpc_bdev_set_qos_limit_complete, request); 1056 1057 spdk_bdev_close(desc); 1058 1059 cleanup: 1060 free_rpc_bdev_set_qos_limit(&req); 1061 } 1062 1063 SPDK_RPC_REGISTER("bdev_set_qos_limit", rpc_bdev_set_qos_limit, SPDK_RPC_RUNTIME) 1064 1065 /* SPDK_RPC_ENABLE_BDEV_HISTOGRAM */ 1066 1067 struct rpc_bdev_enable_histogram_request { 1068 char *name; 1069 bool enable; 1070 }; 1071 1072 static void 1073 free_rpc_bdev_enable_histogram_request(struct rpc_bdev_enable_histogram_request *r) 1074 { 1075 free(r->name); 1076 } 1077 1078 static const struct spdk_json_object_decoder rpc_bdev_enable_histogram_request_decoders[] = { 1079 {"name", offsetof(struct rpc_bdev_enable_histogram_request, name), spdk_json_decode_string}, 1080 {"enable", offsetof(struct rpc_bdev_enable_histogram_request, enable), spdk_json_decode_bool}, 1081 }; 1082 1083 static void 1084 bdev_histogram_status_cb(void *cb_arg, int status) 1085 { 1086 struct spdk_jsonrpc_request *request = cb_arg; 1087 1088 if (status == 0) { 1089 spdk_jsonrpc_send_bool_response(request, true); 1090 } else { 1091 spdk_jsonrpc_send_error_response(request, status, spdk_strerror(-status)); 1092 } 1093 } 1094 1095 static void 1096 rpc_bdev_enable_histogram(struct spdk_jsonrpc_request *request, 1097 const struct spdk_json_val *params) 1098 { 1099 struct rpc_bdev_enable_histogram_request req = {NULL}; 1100 struct spdk_bdev_desc *desc; 1101 int rc; 1102 1103 if (spdk_json_decode_object(params, rpc_bdev_enable_histogram_request_decoders, 1104 SPDK_COUNTOF(rpc_bdev_enable_histogram_request_decoders), 1105 &req)) { 1106 SPDK_ERRLOG("spdk_json_decode_object failed\n"); 1107 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 1108 "spdk_json_decode_object failed"); 1109 goto cleanup; 1110 } 1111 1112 rc = spdk_bdev_open_ext(req.name, false, dummy_bdev_event_cb, NULL, &desc); 1113 if (rc != 0) { 1114 spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc)); 1115 goto cleanup; 1116 } 1117 1118 spdk_bdev_histogram_enable(spdk_bdev_desc_get_bdev(desc), bdev_histogram_status_cb, 1119 request, req.enable); 1120 1121 spdk_bdev_close(desc); 1122 1123 cleanup: 1124 free_rpc_bdev_enable_histogram_request(&req); 1125 } 1126 1127 SPDK_RPC_REGISTER("bdev_enable_histogram", rpc_bdev_enable_histogram, SPDK_RPC_RUNTIME) 1128 1129 /* SPDK_RPC_GET_BDEV_HISTOGRAM */ 1130 1131 struct rpc_bdev_get_histogram_request { 1132 char *name; 1133 }; 1134 1135 static const struct spdk_json_object_decoder rpc_bdev_get_histogram_request_decoders[] = { 1136 {"name", offsetof(struct rpc_bdev_get_histogram_request, name), spdk_json_decode_string} 1137 }; 1138 1139 static void 1140 free_rpc_bdev_get_histogram_request(struct rpc_bdev_get_histogram_request *r) 1141 { 1142 free(r->name); 1143 } 1144 1145 static void 1146 _rpc_bdev_histogram_data_cb(void *cb_arg, int status, struct spdk_histogram_data *histogram) 1147 { 1148 struct spdk_jsonrpc_request *request = cb_arg; 1149 struct spdk_json_write_ctx *w; 1150 int rc; 1151 char *encoded_histogram; 1152 size_t src_len, dst_len; 1153 1154 1155 if (status != 0) { 1156 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 1157 spdk_strerror(-status)); 1158 goto invalid; 1159 } 1160 1161 src_len = SPDK_HISTOGRAM_NUM_BUCKETS(histogram) * sizeof(uint64_t); 1162 dst_len = spdk_base64_get_encoded_strlen(src_len) + 1; 1163 1164 encoded_histogram = malloc(dst_len); 1165 if (encoded_histogram == NULL) { 1166 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 1167 spdk_strerror(ENOMEM)); 1168 goto invalid; 1169 } 1170 1171 rc = spdk_base64_encode(encoded_histogram, histogram->bucket, src_len); 1172 if (rc != 0) { 1173 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 1174 spdk_strerror(-rc)); 1175 goto free_encoded_histogram; 1176 } 1177 1178 w = spdk_jsonrpc_begin_result(request); 1179 spdk_json_write_object_begin(w); 1180 spdk_json_write_named_string(w, "histogram", encoded_histogram); 1181 spdk_json_write_named_int64(w, "bucket_shift", histogram->bucket_shift); 1182 spdk_json_write_named_int64(w, "tsc_rate", spdk_get_ticks_hz()); 1183 spdk_json_write_object_end(w); 1184 spdk_jsonrpc_end_result(request, w); 1185 1186 free_encoded_histogram: 1187 free(encoded_histogram); 1188 invalid: 1189 spdk_histogram_data_free(histogram); 1190 } 1191 1192 static void 1193 rpc_bdev_get_histogram(struct spdk_jsonrpc_request *request, 1194 const struct spdk_json_val *params) 1195 { 1196 struct rpc_bdev_get_histogram_request req = {NULL}; 1197 struct spdk_histogram_data *histogram; 1198 struct spdk_bdev_desc *desc; 1199 int rc; 1200 1201 if (spdk_json_decode_object(params, rpc_bdev_get_histogram_request_decoders, 1202 SPDK_COUNTOF(rpc_bdev_get_histogram_request_decoders), 1203 &req)) { 1204 SPDK_ERRLOG("spdk_json_decode_object failed\n"); 1205 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 1206 "spdk_json_decode_object failed"); 1207 goto cleanup; 1208 } 1209 1210 rc = spdk_bdev_open_ext(req.name, false, dummy_bdev_event_cb, NULL, &desc); 1211 if (rc != 0) { 1212 spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc)); 1213 goto cleanup; 1214 } 1215 1216 histogram = spdk_histogram_data_alloc(); 1217 if (histogram == NULL) { 1218 spdk_bdev_close(desc); 1219 spdk_jsonrpc_send_error_response(request, -ENOMEM, spdk_strerror(ENOMEM)); 1220 goto cleanup; 1221 } 1222 1223 spdk_bdev_histogram_get(spdk_bdev_desc_get_bdev(desc), histogram, 1224 _rpc_bdev_histogram_data_cb, request); 1225 1226 spdk_bdev_close(desc); 1227 1228 cleanup: 1229 free_rpc_bdev_get_histogram_request(&req); 1230 } 1231 1232 SPDK_RPC_REGISTER("bdev_get_histogram", rpc_bdev_get_histogram, SPDK_RPC_RUNTIME) 1233