1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright (C) 2016 Intel Corporation. All rights reserved. 3 * Copyright (c) 2019-2021 Mellanox Technologies LTD. All rights reserved. 4 * Copyright (c) 2022-2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. 5 * Copyright (c) 2022 Dell Inc, or its subsidiaries. All rights reserved. 6 */ 7 8 #include "spdk/stdinc.h" 9 10 #include "bdev_nvme.h" 11 12 #include "spdk/config.h" 13 14 #include "spdk/string.h" 15 #include "spdk/rpc.h" 16 #include "spdk/util.h" 17 #include "spdk/env.h" 18 #include "spdk/nvme.h" 19 #include "spdk/nvme_spec.h" 20 21 #include "spdk/log.h" 22 #include "spdk/bdev_module.h" 23 24 #define TCP_PSK_INVALID_PERMISSIONS 0177 25 26 static bool g_tls_log = false; 27 28 static int 29 rpc_decode_action_on_timeout(const struct spdk_json_val *val, void *out) 30 { 31 enum spdk_bdev_timeout_action *action = out; 32 33 if (spdk_json_strequal(val, "none") == true) { 34 *action = SPDK_BDEV_NVME_TIMEOUT_ACTION_NONE; 35 } else if (spdk_json_strequal(val, "abort") == true) { 36 *action = SPDK_BDEV_NVME_TIMEOUT_ACTION_ABORT; 37 } else if (spdk_json_strequal(val, "reset") == true) { 38 *action = SPDK_BDEV_NVME_TIMEOUT_ACTION_RESET; 39 } else { 40 SPDK_NOTICELOG("Invalid parameter value: action_on_timeout\n"); 41 return -EINVAL; 42 } 43 44 return 0; 45 } 46 47 static const struct spdk_json_object_decoder rpc_bdev_nvme_options_decoders[] = { 48 {"action_on_timeout", offsetof(struct spdk_bdev_nvme_opts, action_on_timeout), rpc_decode_action_on_timeout, true}, 49 {"timeout_us", offsetof(struct spdk_bdev_nvme_opts, timeout_us), spdk_json_decode_uint64, true}, 50 {"timeout_admin_us", offsetof(struct spdk_bdev_nvme_opts, timeout_admin_us), spdk_json_decode_uint64, true}, 51 {"keep_alive_timeout_ms", offsetof(struct spdk_bdev_nvme_opts, keep_alive_timeout_ms), spdk_json_decode_uint32, true}, 52 {"retry_count", offsetof(struct spdk_bdev_nvme_opts, transport_retry_count), spdk_json_decode_uint32, true}, 53 {"arbitration_burst", offsetof(struct spdk_bdev_nvme_opts, arbitration_burst), spdk_json_decode_uint32, true}, 54 {"low_priority_weight", offsetof(struct spdk_bdev_nvme_opts, low_priority_weight), spdk_json_decode_uint32, true}, 55 {"medium_priority_weight", offsetof(struct spdk_bdev_nvme_opts, medium_priority_weight), spdk_json_decode_uint32, true}, 56 {"high_priority_weight", offsetof(struct spdk_bdev_nvme_opts, high_priority_weight), spdk_json_decode_uint32, true}, 57 {"nvme_adminq_poll_period_us", offsetof(struct spdk_bdev_nvme_opts, nvme_adminq_poll_period_us), spdk_json_decode_uint64, true}, 58 {"nvme_ioq_poll_period_us", offsetof(struct spdk_bdev_nvme_opts, nvme_ioq_poll_period_us), spdk_json_decode_uint64, true}, 59 {"io_queue_requests", offsetof(struct spdk_bdev_nvme_opts, io_queue_requests), spdk_json_decode_uint32, true}, 60 {"delay_cmd_submit", offsetof(struct spdk_bdev_nvme_opts, delay_cmd_submit), spdk_json_decode_bool, true}, 61 {"transport_retry_count", offsetof(struct spdk_bdev_nvme_opts, transport_retry_count), spdk_json_decode_uint32, true}, 62 {"bdev_retry_count", offsetof(struct spdk_bdev_nvme_opts, bdev_retry_count), spdk_json_decode_int32, true}, 63 {"transport_ack_timeout", offsetof(struct spdk_bdev_nvme_opts, transport_ack_timeout), spdk_json_decode_uint8, true}, 64 {"ctrlr_loss_timeout_sec", offsetof(struct spdk_bdev_nvme_opts, ctrlr_loss_timeout_sec), spdk_json_decode_int32, true}, 65 {"reconnect_delay_sec", offsetof(struct spdk_bdev_nvme_opts, reconnect_delay_sec), spdk_json_decode_uint32, true}, 66 {"fast_io_fail_timeout_sec", offsetof(struct spdk_bdev_nvme_opts, fast_io_fail_timeout_sec), spdk_json_decode_uint32, true}, 67 {"disable_auto_failback", offsetof(struct spdk_bdev_nvme_opts, disable_auto_failback), spdk_json_decode_bool, true}, 68 {"generate_uuids", offsetof(struct spdk_bdev_nvme_opts, generate_uuids), spdk_json_decode_bool, true}, 69 {"transport_tos", offsetof(struct spdk_bdev_nvme_opts, transport_tos), spdk_json_decode_uint8, true}, 70 {"nvme_error_stat", offsetof(struct spdk_bdev_nvme_opts, nvme_error_stat), spdk_json_decode_bool, true}, 71 {"rdma_srq_size", offsetof(struct spdk_bdev_nvme_opts, rdma_srq_size), spdk_json_decode_uint32, true}, 72 {"io_path_stat", offsetof(struct spdk_bdev_nvme_opts, io_path_stat), spdk_json_decode_bool, true}, 73 {"allow_accel_sequence", offsetof(struct spdk_bdev_nvme_opts, allow_accel_sequence), spdk_json_decode_bool, true}, 74 }; 75 76 static void 77 rpc_bdev_nvme_set_options(struct spdk_jsonrpc_request *request, 78 const struct spdk_json_val *params) 79 { 80 struct spdk_bdev_nvme_opts opts; 81 int rc; 82 83 bdev_nvme_get_opts(&opts); 84 if (params && spdk_json_decode_object(params, rpc_bdev_nvme_options_decoders, 85 SPDK_COUNTOF(rpc_bdev_nvme_options_decoders), 86 &opts)) { 87 SPDK_ERRLOG("spdk_json_decode_object failed\n"); 88 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 89 "spdk_json_decode_object failed"); 90 return; 91 } 92 93 rc = bdev_nvme_set_opts(&opts); 94 if (rc == -EPERM) { 95 spdk_jsonrpc_send_error_response(request, -EPERM, 96 "RPC not permitted with nvme controllers already attached"); 97 } else if (rc) { 98 spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc)); 99 } else { 100 spdk_jsonrpc_send_bool_response(request, true); 101 } 102 103 return; 104 } 105 SPDK_RPC_REGISTER("bdev_nvme_set_options", rpc_bdev_nvme_set_options, 106 SPDK_RPC_STARTUP | SPDK_RPC_RUNTIME) 107 108 struct rpc_bdev_nvme_hotplug { 109 bool enabled; 110 uint64_t period_us; 111 }; 112 113 static const struct spdk_json_object_decoder rpc_bdev_nvme_hotplug_decoders[] = { 114 {"enable", offsetof(struct rpc_bdev_nvme_hotplug, enabled), spdk_json_decode_bool, false}, 115 {"period_us", offsetof(struct rpc_bdev_nvme_hotplug, period_us), spdk_json_decode_uint64, true}, 116 }; 117 118 static void 119 rpc_bdev_nvme_set_hotplug_done(void *ctx) 120 { 121 struct spdk_jsonrpc_request *request = ctx; 122 123 spdk_jsonrpc_send_bool_response(request, true); 124 } 125 126 static void 127 rpc_bdev_nvme_set_hotplug(struct spdk_jsonrpc_request *request, 128 const struct spdk_json_val *params) 129 { 130 struct rpc_bdev_nvme_hotplug req = {false, 0}; 131 int rc; 132 133 if (spdk_json_decode_object(params, rpc_bdev_nvme_hotplug_decoders, 134 SPDK_COUNTOF(rpc_bdev_nvme_hotplug_decoders), &req)) { 135 SPDK_ERRLOG("spdk_json_decode_object failed\n"); 136 rc = -EINVAL; 137 goto invalid; 138 } 139 140 rc = bdev_nvme_set_hotplug(req.enabled, req.period_us, rpc_bdev_nvme_set_hotplug_done, 141 request); 142 if (rc) { 143 goto invalid; 144 } 145 146 return; 147 invalid: 148 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, spdk_strerror(-rc)); 149 } 150 SPDK_RPC_REGISTER("bdev_nvme_set_hotplug", rpc_bdev_nvme_set_hotplug, SPDK_RPC_RUNTIME) 151 152 enum bdev_nvme_multipath_mode { 153 BDEV_NVME_MP_MODE_FAILOVER, 154 BDEV_NVME_MP_MODE_MULTIPATH, 155 BDEV_NVME_MP_MODE_DISABLE, 156 }; 157 158 struct rpc_bdev_nvme_attach_controller { 159 char *name; 160 char *trtype; 161 char *adrfam; 162 char *traddr; 163 char *trsvcid; 164 char *priority; 165 char *subnqn; 166 char *hostnqn; 167 char *hostaddr; 168 char *hostsvcid; 169 char *psk; 170 enum bdev_nvme_multipath_mode multipath; 171 struct nvme_ctrlr_opts bdev_opts; 172 struct spdk_nvme_ctrlr_opts drv_opts; 173 uint32_t max_bdevs; 174 }; 175 176 static void 177 free_rpc_bdev_nvme_attach_controller(struct rpc_bdev_nvme_attach_controller *req) 178 { 179 free(req->name); 180 free(req->trtype); 181 free(req->adrfam); 182 free(req->traddr); 183 free(req->trsvcid); 184 free(req->priority); 185 free(req->subnqn); 186 free(req->hostnqn); 187 free(req->hostaddr); 188 free(req->hostsvcid); 189 free(req->psk); 190 } 191 192 static int 193 bdev_nvme_decode_reftag(const struct spdk_json_val *val, void *out) 194 { 195 uint32_t *flag = out; 196 bool reftag; 197 int rc; 198 199 rc = spdk_json_decode_bool(val, &reftag); 200 if (rc == 0 && reftag == true) { 201 *flag |= SPDK_NVME_IO_FLAGS_PRCHK_REFTAG; 202 } 203 204 return rc; 205 } 206 207 static int 208 bdev_nvme_decode_guard(const struct spdk_json_val *val, void *out) 209 { 210 uint32_t *flag = out; 211 bool guard; 212 int rc; 213 214 rc = spdk_json_decode_bool(val, &guard); 215 if (rc == 0 && guard == true) { 216 *flag |= SPDK_NVME_IO_FLAGS_PRCHK_GUARD; 217 } 218 219 return rc; 220 } 221 222 static int 223 bdev_nvme_decode_multipath(const struct spdk_json_val *val, void *out) 224 { 225 enum bdev_nvme_multipath_mode *multipath = out; 226 227 if (spdk_json_strequal(val, "failover") == true) { 228 *multipath = BDEV_NVME_MP_MODE_FAILOVER; 229 } else if (spdk_json_strequal(val, "multipath") == true) { 230 *multipath = BDEV_NVME_MP_MODE_MULTIPATH; 231 } else if (spdk_json_strequal(val, "disable") == true) { 232 *multipath = BDEV_NVME_MP_MODE_DISABLE; 233 } else { 234 SPDK_NOTICELOG("Invalid parameter value: multipath\n"); 235 return -EINVAL; 236 } 237 238 return 0; 239 } 240 241 242 static const struct spdk_json_object_decoder rpc_bdev_nvme_attach_controller_decoders[] = { 243 {"name", offsetof(struct rpc_bdev_nvme_attach_controller, name), spdk_json_decode_string}, 244 {"trtype", offsetof(struct rpc_bdev_nvme_attach_controller, trtype), spdk_json_decode_string}, 245 {"traddr", offsetof(struct rpc_bdev_nvme_attach_controller, traddr), spdk_json_decode_string}, 246 247 {"adrfam", offsetof(struct rpc_bdev_nvme_attach_controller, adrfam), spdk_json_decode_string, true}, 248 {"trsvcid", offsetof(struct rpc_bdev_nvme_attach_controller, trsvcid), spdk_json_decode_string, true}, 249 {"priority", offsetof(struct rpc_bdev_nvme_attach_controller, priority), spdk_json_decode_string, true}, 250 {"subnqn", offsetof(struct rpc_bdev_nvme_attach_controller, subnqn), spdk_json_decode_string, true}, 251 {"hostnqn", offsetof(struct rpc_bdev_nvme_attach_controller, hostnqn), spdk_json_decode_string, true}, 252 {"hostaddr", offsetof(struct rpc_bdev_nvme_attach_controller, hostaddr), spdk_json_decode_string, true}, 253 {"hostsvcid", offsetof(struct rpc_bdev_nvme_attach_controller, hostsvcid), spdk_json_decode_string, true}, 254 255 {"prchk_reftag", offsetof(struct rpc_bdev_nvme_attach_controller, bdev_opts.prchk_flags), bdev_nvme_decode_reftag, true}, 256 {"prchk_guard", offsetof(struct rpc_bdev_nvme_attach_controller, bdev_opts.prchk_flags), bdev_nvme_decode_guard, true}, 257 {"hdgst", offsetof(struct rpc_bdev_nvme_attach_controller, drv_opts.header_digest), spdk_json_decode_bool, true}, 258 {"ddgst", offsetof(struct rpc_bdev_nvme_attach_controller, drv_opts.data_digest), spdk_json_decode_bool, true}, 259 {"fabrics_connect_timeout_us", offsetof(struct rpc_bdev_nvme_attach_controller, drv_opts.fabrics_connect_timeout_us), spdk_json_decode_uint64, true}, 260 {"multipath", offsetof(struct rpc_bdev_nvme_attach_controller, multipath), bdev_nvme_decode_multipath, true}, 261 {"num_io_queues", offsetof(struct rpc_bdev_nvme_attach_controller, drv_opts.num_io_queues), spdk_json_decode_uint32, true}, 262 {"ctrlr_loss_timeout_sec", offsetof(struct rpc_bdev_nvme_attach_controller, bdev_opts.ctrlr_loss_timeout_sec), spdk_json_decode_int32, true}, 263 {"reconnect_delay_sec", offsetof(struct rpc_bdev_nvme_attach_controller, bdev_opts.reconnect_delay_sec), spdk_json_decode_uint32, true}, 264 {"fast_io_fail_timeout_sec", offsetof(struct rpc_bdev_nvme_attach_controller, bdev_opts.fast_io_fail_timeout_sec), spdk_json_decode_uint32, true}, 265 {"psk", offsetof(struct rpc_bdev_nvme_attach_controller, psk), spdk_json_decode_string, true}, 266 {"max_bdevs", offsetof(struct rpc_bdev_nvme_attach_controller, max_bdevs), spdk_json_decode_uint32, true}, 267 }; 268 269 #define DEFAULT_MAX_BDEVS_PER_RPC 128 270 271 struct rpc_bdev_nvme_attach_controller_ctx { 272 struct rpc_bdev_nvme_attach_controller req; 273 size_t bdev_count; 274 const char **names; 275 struct spdk_jsonrpc_request *request; 276 }; 277 278 static void 279 free_rpc_bdev_nvme_attach_controller_ctx(struct rpc_bdev_nvme_attach_controller_ctx *ctx) 280 { 281 free_rpc_bdev_nvme_attach_controller(&ctx->req); 282 free(ctx->names); 283 free(ctx); 284 } 285 286 static void 287 rpc_bdev_nvme_attach_controller_examined(void *cb_ctx) 288 { 289 struct rpc_bdev_nvme_attach_controller_ctx *ctx = cb_ctx; 290 struct spdk_jsonrpc_request *request = ctx->request; 291 struct spdk_json_write_ctx *w; 292 size_t i; 293 294 w = spdk_jsonrpc_begin_result(request); 295 spdk_json_write_array_begin(w); 296 for (i = 0; i < ctx->bdev_count; i++) { 297 spdk_json_write_string(w, ctx->names[i]); 298 } 299 spdk_json_write_array_end(w); 300 spdk_jsonrpc_end_result(request, w); 301 302 free_rpc_bdev_nvme_attach_controller_ctx(ctx); 303 } 304 305 static void 306 rpc_bdev_nvme_attach_controller_done(void *cb_ctx, size_t bdev_count, int rc) 307 { 308 struct rpc_bdev_nvme_attach_controller_ctx *ctx = cb_ctx; 309 struct spdk_jsonrpc_request *request = ctx->request; 310 311 if (rc < 0) { 312 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters"); 313 free_rpc_bdev_nvme_attach_controller_ctx(ctx); 314 return; 315 } 316 317 ctx->bdev_count = bdev_count; 318 spdk_bdev_wait_for_examine(rpc_bdev_nvme_attach_controller_examined, ctx); 319 } 320 321 static int 322 tcp_load_psk(const char *fname, char *buf, size_t bufsz) 323 { 324 FILE *psk_file; 325 struct stat statbuf; 326 int rc; 327 328 if (stat(fname, &statbuf) != 0) { 329 SPDK_ERRLOG("Could not read permissions for PSK file\n"); 330 return -EACCES; 331 } 332 333 if ((statbuf.st_mode & TCP_PSK_INVALID_PERMISSIONS) != 0) { 334 SPDK_ERRLOG("Incorrect permissions for PSK file\n"); 335 return -EPERM; 336 } 337 if ((size_t)statbuf.st_size >= bufsz) { 338 SPDK_ERRLOG("Invalid PSK: too long\n"); 339 return -EINVAL; 340 } 341 psk_file = fopen(fname, "r"); 342 if (psk_file == NULL) { 343 SPDK_ERRLOG("Could not open PSK file\n"); 344 return -EINVAL; 345 } 346 347 memset(buf, 0, bufsz); 348 rc = fread(buf, 1, statbuf.st_size, psk_file); 349 if (rc != statbuf.st_size) { 350 SPDK_ERRLOG("Failed to read PSK\n"); 351 fclose(psk_file); 352 return -EINVAL; 353 } 354 355 fclose(psk_file); 356 return 0; 357 } 358 359 static void 360 rpc_bdev_nvme_attach_controller(struct spdk_jsonrpc_request *request, 361 const struct spdk_json_val *params) 362 { 363 struct rpc_bdev_nvme_attach_controller_ctx *ctx; 364 struct spdk_nvme_transport_id trid = {}; 365 const struct spdk_nvme_ctrlr_opts *drv_opts; 366 const struct spdk_nvme_transport_id *ctrlr_trid; 367 struct nvme_ctrlr *ctrlr = NULL; 368 size_t len, maxlen; 369 bool multipath = false; 370 int rc; 371 372 ctx = calloc(1, sizeof(*ctx)); 373 if (!ctx) { 374 spdk_jsonrpc_send_error_response(request, -ENOMEM, spdk_strerror(ENOMEM)); 375 return; 376 } 377 378 spdk_nvme_ctrlr_get_default_ctrlr_opts(&ctx->req.drv_opts, sizeof(ctx->req.drv_opts)); 379 bdev_nvme_get_default_ctrlr_opts(&ctx->req.bdev_opts); 380 /* For now, initialize the multipath parameter to add a failover path. This maintains backward 381 * compatibility with past behavior. In the future, this behavior will change to "disable". */ 382 ctx->req.multipath = BDEV_NVME_MP_MODE_FAILOVER; 383 ctx->req.max_bdevs = DEFAULT_MAX_BDEVS_PER_RPC; 384 385 if (spdk_json_decode_object(params, rpc_bdev_nvme_attach_controller_decoders, 386 SPDK_COUNTOF(rpc_bdev_nvme_attach_controller_decoders), 387 &ctx->req)) { 388 SPDK_ERRLOG("spdk_json_decode_object failed\n"); 389 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 390 "spdk_json_decode_object failed"); 391 goto cleanup; 392 } 393 394 if (ctx->req.max_bdevs == 0) { 395 spdk_jsonrpc_send_error_response(request, -EINVAL, "max_bdevs cannot be zero"); 396 goto cleanup; 397 } 398 399 ctx->names = calloc(ctx->req.max_bdevs, sizeof(char *)); 400 if (ctx->names == NULL) { 401 spdk_jsonrpc_send_error_response(request, -ENOMEM, spdk_strerror(ENOMEM)); 402 goto cleanup; 403 } 404 405 /* Parse trstring */ 406 rc = spdk_nvme_transport_id_populate_trstring(&trid, ctx->req.trtype); 407 if (rc < 0) { 408 SPDK_ERRLOG("Failed to parse trtype: %s\n", ctx->req.trtype); 409 spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "Failed to parse trtype: %s", 410 ctx->req.trtype); 411 goto cleanup; 412 } 413 414 /* Parse trtype */ 415 rc = spdk_nvme_transport_id_parse_trtype(&trid.trtype, ctx->req.trtype); 416 assert(rc == 0); 417 418 /* Parse traddr */ 419 maxlen = sizeof(trid.traddr); 420 len = strnlen(ctx->req.traddr, maxlen); 421 if (len == maxlen) { 422 spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "traddr too long: %s", 423 ctx->req.traddr); 424 goto cleanup; 425 } 426 memcpy(trid.traddr, ctx->req.traddr, len + 1); 427 428 /* Parse adrfam */ 429 if (ctx->req.adrfam) { 430 rc = spdk_nvme_transport_id_parse_adrfam(&trid.adrfam, ctx->req.adrfam); 431 if (rc < 0) { 432 SPDK_ERRLOG("Failed to parse adrfam: %s\n", ctx->req.adrfam); 433 spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "Failed to parse adrfam: %s", 434 ctx->req.adrfam); 435 goto cleanup; 436 } 437 } 438 439 /* Parse trsvcid */ 440 if (ctx->req.trsvcid) { 441 maxlen = sizeof(trid.trsvcid); 442 len = strnlen(ctx->req.trsvcid, maxlen); 443 if (len == maxlen) { 444 spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "trsvcid too long: %s", 445 ctx->req.trsvcid); 446 goto cleanup; 447 } 448 memcpy(trid.trsvcid, ctx->req.trsvcid, len + 1); 449 } 450 451 /* Parse priority for the NVMe-oF transport connection */ 452 if (ctx->req.priority) { 453 trid.priority = spdk_strtol(ctx->req.priority, 10); 454 } 455 456 /* Parse subnqn */ 457 if (ctx->req.subnqn) { 458 maxlen = sizeof(trid.subnqn); 459 len = strnlen(ctx->req.subnqn, maxlen); 460 if (len == maxlen) { 461 spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "subnqn too long: %s", 462 ctx->req.subnqn); 463 goto cleanup; 464 } 465 memcpy(trid.subnqn, ctx->req.subnqn, len + 1); 466 } 467 468 if (ctx->req.hostnqn) { 469 snprintf(ctx->req.drv_opts.hostnqn, sizeof(ctx->req.drv_opts.hostnqn), "%s", 470 ctx->req.hostnqn); 471 } 472 473 if (ctx->req.psk) { 474 if (!g_tls_log) { 475 SPDK_NOTICELOG("TLS support is considered experimental\n"); 476 g_tls_log = true; 477 } 478 rc = tcp_load_psk(ctx->req.psk, ctx->req.drv_opts.psk, sizeof(ctx->req.drv_opts.psk)); 479 if (rc) { 480 spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "Could not retrieve PSK from file: %s", 481 ctx->req.psk); 482 goto cleanup; 483 } 484 rc = snprintf(ctx->req.bdev_opts.psk_path, sizeof(ctx->req.bdev_opts.psk_path), "%s", ctx->req.psk); 485 if (rc < 0 || (size_t)rc >= sizeof(ctx->req.bdev_opts.psk_path)) { 486 spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "Could not store PSK path: %s", 487 ctx->req.psk); 488 goto cleanup; 489 } 490 } 491 492 if (ctx->req.hostaddr) { 493 maxlen = sizeof(ctx->req.drv_opts.src_addr); 494 len = strnlen(ctx->req.hostaddr, maxlen); 495 if (len == maxlen) { 496 spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "hostaddr too long: %s", 497 ctx->req.hostaddr); 498 goto cleanup; 499 } 500 snprintf(ctx->req.drv_opts.src_addr, maxlen, "%s", ctx->req.hostaddr); 501 } 502 503 if (ctx->req.hostsvcid) { 504 maxlen = sizeof(ctx->req.drv_opts.src_svcid); 505 len = strnlen(ctx->req.hostsvcid, maxlen); 506 if (len == maxlen) { 507 spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "hostsvcid too long: %s", 508 ctx->req.hostsvcid); 509 goto cleanup; 510 } 511 snprintf(ctx->req.drv_opts.src_svcid, maxlen, "%s", ctx->req.hostsvcid); 512 } 513 514 ctrlr = nvme_ctrlr_get_by_name(ctx->req.name); 515 516 if (ctrlr) { 517 /* This controller already exists. Check what the user wants to do. */ 518 if (ctx->req.multipath == BDEV_NVME_MP_MODE_DISABLE) { 519 /* The user does not want to do any form of multipathing. */ 520 spdk_jsonrpc_send_error_response_fmt(request, -EALREADY, 521 "A controller named %s already exists and multipath is disabled\n", 522 ctx->req.name); 523 goto cleanup; 524 } 525 526 assert(ctx->req.multipath == BDEV_NVME_MP_MODE_FAILOVER || 527 ctx->req.multipath == BDEV_NVME_MP_MODE_MULTIPATH); 528 529 /* The user wants to add this as a failover path or add this to create multipath. */ 530 drv_opts = spdk_nvme_ctrlr_get_opts(ctrlr->ctrlr); 531 ctrlr_trid = spdk_nvme_ctrlr_get_transport_id(ctrlr->ctrlr); 532 533 if (strncmp(trid.traddr, ctrlr_trid->traddr, sizeof(trid.traddr)) == 0 && 534 strncmp(trid.trsvcid, ctrlr_trid->trsvcid, sizeof(trid.trsvcid)) == 0 && 535 strncmp(ctx->req.drv_opts.src_addr, drv_opts->src_addr, sizeof(drv_opts->src_addr)) == 0 && 536 strncmp(ctx->req.drv_opts.src_svcid, drv_opts->src_svcid, sizeof(drv_opts->src_svcid)) == 0) { 537 /* Exactly same network path can't be added a second time */ 538 spdk_jsonrpc_send_error_response_fmt(request, -EALREADY, 539 "A controller named %s already exists with the specified network path\n", 540 ctx->req.name); 541 goto cleanup; 542 } 543 544 if (strncmp(trid.subnqn, 545 ctrlr_trid->subnqn, 546 SPDK_NVMF_NQN_MAX_LEN) != 0) { 547 /* Different SUBNQN is not allowed when specifying the same controller name. */ 548 spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, 549 "A controller named %s already exists, but uses a different subnqn (%s)\n", 550 ctx->req.name, ctrlr_trid->subnqn); 551 goto cleanup; 552 } 553 554 if (strncmp(ctx->req.drv_opts.hostnqn, drv_opts->hostnqn, SPDK_NVMF_NQN_MAX_LEN) != 0) { 555 /* Different HOSTNQN is not allowed when specifying the same controller name. */ 556 spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, 557 "A controller named %s already exists, but uses a different hostnqn (%s)\n", 558 ctx->req.name, drv_opts->hostnqn); 559 goto cleanup; 560 } 561 562 if (ctx->req.bdev_opts.prchk_flags) { 563 spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, 564 "A controller named %s already exists. To add a path, do not specify PI options.\n", 565 ctx->req.name); 566 goto cleanup; 567 } 568 569 ctx->req.bdev_opts.prchk_flags = ctrlr->opts.prchk_flags; 570 } 571 572 if (ctx->req.multipath == BDEV_NVME_MP_MODE_MULTIPATH) { 573 multipath = true; 574 } 575 576 if (ctx->req.drv_opts.num_io_queues == 0 || ctx->req.drv_opts.num_io_queues > UINT16_MAX + 1) { 577 spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, 578 "num_io_queues out of bounds, min: %u max: %u\n", 579 1, UINT16_MAX + 1); 580 goto cleanup; 581 } 582 583 ctx->request = request; 584 /* Should already be zero due to the calloc(), but set explicitly for clarity. */ 585 ctx->req.bdev_opts.from_discovery_service = false; 586 rc = bdev_nvme_create(&trid, ctx->req.name, ctx->names, ctx->req.max_bdevs, 587 rpc_bdev_nvme_attach_controller_done, ctx, &ctx->req.drv_opts, 588 &ctx->req.bdev_opts, multipath); 589 if (rc) { 590 spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc)); 591 goto cleanup; 592 } 593 594 return; 595 596 cleanup: 597 free_rpc_bdev_nvme_attach_controller_ctx(ctx); 598 } 599 SPDK_RPC_REGISTER("bdev_nvme_attach_controller", rpc_bdev_nvme_attach_controller, 600 SPDK_RPC_RUNTIME) 601 602 static void 603 rpc_dump_nvme_bdev_controller_info(struct nvme_bdev_ctrlr *nbdev_ctrlr, void *ctx) 604 { 605 struct spdk_json_write_ctx *w = ctx; 606 struct nvme_ctrlr *nvme_ctrlr; 607 608 spdk_json_write_object_begin(w); 609 spdk_json_write_named_string(w, "name", nbdev_ctrlr->name); 610 611 spdk_json_write_named_array_begin(w, "ctrlrs"); 612 TAILQ_FOREACH(nvme_ctrlr, &nbdev_ctrlr->ctrlrs, tailq) { 613 nvme_ctrlr_info_json(w, nvme_ctrlr); 614 } 615 spdk_json_write_array_end(w); 616 spdk_json_write_object_end(w); 617 } 618 619 struct rpc_bdev_nvme_get_controllers { 620 char *name; 621 }; 622 623 static void 624 free_rpc_bdev_nvme_get_controllers(struct rpc_bdev_nvme_get_controllers *r) 625 { 626 free(r->name); 627 } 628 629 static const struct spdk_json_object_decoder rpc_bdev_nvme_get_controllers_decoders[] = { 630 {"name", offsetof(struct rpc_bdev_nvme_get_controllers, name), spdk_json_decode_string, true}, 631 }; 632 633 static void 634 rpc_bdev_nvme_get_controllers(struct spdk_jsonrpc_request *request, 635 const struct spdk_json_val *params) 636 { 637 struct rpc_bdev_nvme_get_controllers req = {}; 638 struct spdk_json_write_ctx *w; 639 struct nvme_bdev_ctrlr *nbdev_ctrlr = NULL; 640 641 if (params && spdk_json_decode_object(params, rpc_bdev_nvme_get_controllers_decoders, 642 SPDK_COUNTOF(rpc_bdev_nvme_get_controllers_decoders), 643 &req)) { 644 SPDK_ERRLOG("spdk_json_decode_object failed\n"); 645 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 646 "spdk_json_decode_object failed"); 647 goto cleanup; 648 } 649 650 if (req.name) { 651 nbdev_ctrlr = nvme_bdev_ctrlr_get_by_name(req.name); 652 if (nbdev_ctrlr == NULL) { 653 SPDK_ERRLOG("ctrlr '%s' does not exist\n", req.name); 654 spdk_jsonrpc_send_error_response_fmt(request, EINVAL, "Controller %s does not exist", req.name); 655 goto cleanup; 656 } 657 } 658 659 w = spdk_jsonrpc_begin_result(request); 660 spdk_json_write_array_begin(w); 661 662 if (nbdev_ctrlr != NULL) { 663 rpc_dump_nvme_bdev_controller_info(nbdev_ctrlr, w); 664 } else { 665 nvme_bdev_ctrlr_for_each(rpc_dump_nvme_bdev_controller_info, w); 666 } 667 668 spdk_json_write_array_end(w); 669 670 spdk_jsonrpc_end_result(request, w); 671 672 cleanup: 673 free_rpc_bdev_nvme_get_controllers(&req); 674 } 675 SPDK_RPC_REGISTER("bdev_nvme_get_controllers", rpc_bdev_nvme_get_controllers, SPDK_RPC_RUNTIME) 676 677 struct rpc_bdev_nvme_detach_controller { 678 char *name; 679 char *trtype; 680 char *adrfam; 681 char *traddr; 682 char *trsvcid; 683 char *subnqn; 684 char *hostaddr; 685 char *hostsvcid; 686 }; 687 688 static void 689 free_rpc_bdev_nvme_detach_controller(struct rpc_bdev_nvme_detach_controller *req) 690 { 691 free(req->name); 692 free(req->trtype); 693 free(req->adrfam); 694 free(req->traddr); 695 free(req->trsvcid); 696 free(req->subnqn); 697 free(req->hostaddr); 698 free(req->hostsvcid); 699 } 700 701 static const struct spdk_json_object_decoder rpc_bdev_nvme_detach_controller_decoders[] = { 702 {"name", offsetof(struct rpc_bdev_nvme_detach_controller, name), spdk_json_decode_string}, 703 {"trtype", offsetof(struct rpc_bdev_nvme_detach_controller, trtype), spdk_json_decode_string, true}, 704 {"traddr", offsetof(struct rpc_bdev_nvme_detach_controller, traddr), spdk_json_decode_string, true}, 705 {"adrfam", offsetof(struct rpc_bdev_nvme_detach_controller, adrfam), spdk_json_decode_string, true}, 706 {"trsvcid", offsetof(struct rpc_bdev_nvme_detach_controller, trsvcid), spdk_json_decode_string, true}, 707 {"subnqn", offsetof(struct rpc_bdev_nvme_detach_controller, subnqn), spdk_json_decode_string, true}, 708 {"hostaddr", offsetof(struct rpc_bdev_nvme_detach_controller, hostaddr), spdk_json_decode_string, true}, 709 {"hostsvcid", offsetof(struct rpc_bdev_nvme_detach_controller, hostsvcid), spdk_json_decode_string, true}, 710 }; 711 712 static void 713 rpc_bdev_nvme_detach_controller_done(void *arg, int rc) 714 { 715 struct spdk_jsonrpc_request *request = arg; 716 717 if (rc == 0) { 718 spdk_jsonrpc_send_bool_response(request, true); 719 } else { 720 spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc)); 721 } 722 } 723 724 static void 725 rpc_bdev_nvme_detach_controller(struct spdk_jsonrpc_request *request, 726 const struct spdk_json_val *params) 727 { 728 struct rpc_bdev_nvme_detach_controller req = {NULL}; 729 struct nvme_path_id path = {}; 730 size_t len, maxlen; 731 int rc = 0; 732 733 if (spdk_json_decode_object(params, rpc_bdev_nvme_detach_controller_decoders, 734 SPDK_COUNTOF(rpc_bdev_nvme_detach_controller_decoders), 735 &req)) { 736 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 737 "spdk_json_decode_object failed"); 738 goto cleanup; 739 } 740 741 if (req.trtype != NULL) { 742 rc = spdk_nvme_transport_id_populate_trstring(&path.trid, req.trtype); 743 if (rc < 0) { 744 SPDK_ERRLOG("Failed to parse trtype: %s\n", req.trtype); 745 spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "Failed to parse trtype: %s", 746 req.trtype); 747 goto cleanup; 748 } 749 750 rc = spdk_nvme_transport_id_parse_trtype(&path.trid.trtype, req.trtype); 751 if (rc < 0) { 752 SPDK_ERRLOG("Failed to parse trtype: %s\n", req.trtype); 753 spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "Failed to parse trtype: %s", 754 req.trtype); 755 goto cleanup; 756 } 757 } 758 759 if (req.traddr != NULL) { 760 maxlen = sizeof(path.trid.traddr); 761 len = strnlen(req.traddr, maxlen); 762 if (len == maxlen) { 763 spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "traddr too long: %s", 764 req.traddr); 765 goto cleanup; 766 } 767 memcpy(path.trid.traddr, req.traddr, len + 1); 768 } 769 770 if (req.adrfam != NULL) { 771 rc = spdk_nvme_transport_id_parse_adrfam(&path.trid.adrfam, req.adrfam); 772 if (rc < 0) { 773 SPDK_ERRLOG("Failed to parse adrfam: %s\n", req.adrfam); 774 spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "Failed to parse adrfam: %s", 775 req.adrfam); 776 goto cleanup; 777 } 778 } 779 780 if (req.trsvcid != NULL) { 781 maxlen = sizeof(path.trid.trsvcid); 782 len = strnlen(req.trsvcid, maxlen); 783 if (len == maxlen) { 784 spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "trsvcid too long: %s", 785 req.trsvcid); 786 goto cleanup; 787 } 788 memcpy(path.trid.trsvcid, req.trsvcid, len + 1); 789 } 790 791 /* Parse subnqn */ 792 if (req.subnqn != NULL) { 793 maxlen = sizeof(path.trid.subnqn); 794 len = strnlen(req.subnqn, maxlen); 795 if (len == maxlen) { 796 spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "subnqn too long: %s", 797 req.subnqn); 798 goto cleanup; 799 } 800 memcpy(path.trid.subnqn, req.subnqn, len + 1); 801 } 802 803 if (req.hostaddr) { 804 maxlen = sizeof(path.hostid.hostaddr); 805 len = strnlen(req.hostaddr, maxlen); 806 if (len == maxlen) { 807 spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "hostaddr too long: %s", 808 req.hostaddr); 809 goto cleanup; 810 } 811 snprintf(path.hostid.hostaddr, maxlen, "%s", req.hostaddr); 812 } 813 814 if (req.hostsvcid) { 815 maxlen = sizeof(path.hostid.hostsvcid); 816 len = strnlen(req.hostsvcid, maxlen); 817 if (len == maxlen) { 818 spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "hostsvcid too long: %s", 819 req.hostsvcid); 820 goto cleanup; 821 } 822 snprintf(path.hostid.hostsvcid, maxlen, "%s", req.hostsvcid); 823 } 824 825 rc = bdev_nvme_delete(req.name, &path, rpc_bdev_nvme_detach_controller_done, request); 826 827 if (rc != 0) { 828 spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc)); 829 } 830 831 cleanup: 832 free_rpc_bdev_nvme_detach_controller(&req); 833 } 834 SPDK_RPC_REGISTER("bdev_nvme_detach_controller", rpc_bdev_nvme_detach_controller, 835 SPDK_RPC_RUNTIME) 836 837 struct rpc_apply_firmware { 838 char *filename; 839 char *bdev_name; 840 }; 841 842 static void 843 free_rpc_apply_firmware(struct rpc_apply_firmware *req) 844 { 845 free(req->filename); 846 free(req->bdev_name); 847 } 848 849 static const struct spdk_json_object_decoder rpc_apply_firmware_decoders[] = { 850 {"filename", offsetof(struct rpc_apply_firmware, filename), spdk_json_decode_string}, 851 {"bdev_name", offsetof(struct rpc_apply_firmware, bdev_name), spdk_json_decode_string}, 852 }; 853 854 struct firmware_update_info { 855 void *fw_image; 856 void *p; 857 unsigned int size; 858 unsigned int size_remaining; 859 unsigned int offset; 860 unsigned int transfer; 861 bool success; 862 863 struct spdk_bdev_desc *desc; 864 struct spdk_io_channel *ch; 865 struct spdk_thread *orig_thread; 866 struct spdk_jsonrpc_request *request; 867 struct spdk_nvme_ctrlr *ctrlr; 868 struct rpc_apply_firmware req; 869 }; 870 871 static void 872 apply_firmware_cleanup(struct firmware_update_info *firm_ctx) 873 { 874 assert(firm_ctx != NULL); 875 assert(firm_ctx->orig_thread == spdk_get_thread()); 876 877 if (firm_ctx->fw_image) { 878 spdk_free(firm_ctx->fw_image); 879 } 880 881 free_rpc_apply_firmware(&firm_ctx->req); 882 883 if (firm_ctx->ch) { 884 spdk_put_io_channel(firm_ctx->ch); 885 } 886 887 if (firm_ctx->desc) { 888 spdk_bdev_close(firm_ctx->desc); 889 } 890 891 free(firm_ctx); 892 } 893 894 static void 895 _apply_firmware_complete_reset(void *ctx) 896 { 897 struct spdk_json_write_ctx *w; 898 struct firmware_update_info *firm_ctx = ctx; 899 900 assert(firm_ctx->orig_thread == spdk_get_thread()); 901 902 if (!firm_ctx->success) { 903 spdk_jsonrpc_send_error_response(firm_ctx->request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 904 "firmware commit failed."); 905 apply_firmware_cleanup(firm_ctx); 906 return; 907 } 908 909 if (spdk_nvme_ctrlr_reset(firm_ctx->ctrlr) != 0) { 910 spdk_jsonrpc_send_error_response(firm_ctx->request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 911 "Controller reset failed."); 912 apply_firmware_cleanup(firm_ctx); 913 return; 914 } 915 916 w = spdk_jsonrpc_begin_result(firm_ctx->request); 917 spdk_json_write_string(w, "firmware commit succeeded. Controller reset in progress."); 918 spdk_jsonrpc_end_result(firm_ctx->request, w); 919 apply_firmware_cleanup(firm_ctx); 920 } 921 922 static void 923 apply_firmware_complete_reset(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg) 924 { 925 struct firmware_update_info *firm_ctx = cb_arg; 926 927 spdk_bdev_free_io(bdev_io); 928 929 firm_ctx->success = success; 930 931 spdk_thread_exec_msg(firm_ctx->orig_thread, _apply_firmware_complete_reset, firm_ctx); 932 } 933 934 static void apply_firmware_complete(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg); 935 936 static void 937 _apply_firmware_complete(void *ctx) 938 { 939 struct spdk_nvme_cmd cmd = {}; 940 struct spdk_nvme_fw_commit fw_commit; 941 int slot = 0; 942 int rc; 943 struct firmware_update_info *firm_ctx = ctx; 944 enum spdk_nvme_fw_commit_action commit_action = SPDK_NVME_FW_COMMIT_REPLACE_AND_ENABLE_IMG; 945 946 assert(firm_ctx->orig_thread == spdk_get_thread()); 947 948 if (!firm_ctx->success) { 949 spdk_jsonrpc_send_error_response(firm_ctx->request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 950 "firmware download failed ."); 951 apply_firmware_cleanup(firm_ctx); 952 return; 953 } 954 955 firm_ctx->p += firm_ctx->transfer; 956 firm_ctx->offset += firm_ctx->transfer; 957 firm_ctx->size_remaining -= firm_ctx->transfer; 958 959 switch (firm_ctx->size_remaining) { 960 case 0: 961 /* firmware download completed. Commit firmware */ 962 memset(&fw_commit, 0, sizeof(struct spdk_nvme_fw_commit)); 963 fw_commit.fs = slot; 964 fw_commit.ca = commit_action; 965 966 cmd.opc = SPDK_NVME_OPC_FIRMWARE_COMMIT; 967 memcpy(&cmd.cdw10, &fw_commit, sizeof(uint32_t)); 968 rc = spdk_bdev_nvme_admin_passthru(firm_ctx->desc, firm_ctx->ch, &cmd, NULL, 0, 969 apply_firmware_complete_reset, firm_ctx); 970 if (rc) { 971 spdk_jsonrpc_send_error_response(firm_ctx->request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 972 "firmware commit failed."); 973 apply_firmware_cleanup(firm_ctx); 974 return; 975 } 976 break; 977 default: 978 firm_ctx->transfer = spdk_min(firm_ctx->size_remaining, 4096); 979 cmd.opc = SPDK_NVME_OPC_FIRMWARE_IMAGE_DOWNLOAD; 980 981 cmd.cdw10 = spdk_nvme_bytes_to_numd(firm_ctx->transfer); 982 cmd.cdw11 = firm_ctx->offset >> 2; 983 rc = spdk_bdev_nvme_admin_passthru(firm_ctx->desc, firm_ctx->ch, &cmd, firm_ctx->p, 984 firm_ctx->transfer, apply_firmware_complete, firm_ctx); 985 if (rc) { 986 spdk_jsonrpc_send_error_response(firm_ctx->request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 987 "firmware download failed."); 988 apply_firmware_cleanup(firm_ctx); 989 return; 990 } 991 break; 992 } 993 } 994 995 static void 996 apply_firmware_complete(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg) 997 { 998 struct firmware_update_info *firm_ctx = cb_arg; 999 1000 spdk_bdev_free_io(bdev_io); 1001 1002 firm_ctx->success = success; 1003 1004 spdk_thread_exec_msg(firm_ctx->orig_thread, _apply_firmware_complete, firm_ctx); 1005 } 1006 1007 static void 1008 apply_firmware_open_cb(enum spdk_bdev_event_type type, struct spdk_bdev *bdev, void *event_ctx) 1009 { 1010 } 1011 1012 static void 1013 rpc_bdev_nvme_apply_firmware(struct spdk_jsonrpc_request *request, 1014 const struct spdk_json_val *params) 1015 { 1016 int rc; 1017 int fd = -1; 1018 struct stat fw_stat; 1019 struct spdk_bdev *bdev; 1020 struct spdk_nvme_cmd cmd = {}; 1021 struct firmware_update_info *firm_ctx; 1022 1023 firm_ctx = calloc(1, sizeof(struct firmware_update_info)); 1024 if (!firm_ctx) { 1025 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 1026 "Memory allocation error."); 1027 return; 1028 } 1029 firm_ctx->fw_image = NULL; 1030 firm_ctx->request = request; 1031 firm_ctx->orig_thread = spdk_get_thread(); 1032 1033 if (spdk_json_decode_object(params, rpc_apply_firmware_decoders, 1034 SPDK_COUNTOF(rpc_apply_firmware_decoders), &firm_ctx->req)) { 1035 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 1036 "spdk_json_decode_object failed."); 1037 goto err; 1038 } 1039 1040 if (spdk_bdev_open_ext(firm_ctx->req.bdev_name, true, apply_firmware_open_cb, NULL, 1041 &firm_ctx->desc) != 0) { 1042 spdk_jsonrpc_send_error_response_fmt(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 1043 "bdev %s could not be opened", 1044 firm_ctx->req.bdev_name); 1045 goto err; 1046 } 1047 bdev = spdk_bdev_desc_get_bdev(firm_ctx->desc); 1048 1049 if ((firm_ctx->ctrlr = bdev_nvme_get_ctrlr(bdev)) == NULL) { 1050 spdk_jsonrpc_send_error_response_fmt(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 1051 "Controller information for %s were not found.", 1052 firm_ctx->req.bdev_name); 1053 goto err; 1054 } 1055 1056 firm_ctx->ch = spdk_bdev_get_io_channel(firm_ctx->desc); 1057 if (!firm_ctx->ch) { 1058 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 1059 "No channels were found."); 1060 goto err; 1061 } 1062 1063 fd = open(firm_ctx->req.filename, O_RDONLY); 1064 if (fd < 0) { 1065 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 1066 "open file failed."); 1067 goto err; 1068 } 1069 1070 rc = fstat(fd, &fw_stat); 1071 if (rc < 0) { 1072 close(fd); 1073 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 1074 "fstat failed."); 1075 goto err; 1076 } 1077 1078 firm_ctx->size = fw_stat.st_size; 1079 if (fw_stat.st_size % 4) { 1080 close(fd); 1081 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 1082 "Firmware image size is not multiple of 4."); 1083 goto err; 1084 } 1085 1086 firm_ctx->fw_image = spdk_zmalloc(firm_ctx->size, 4096, NULL, 1087 SPDK_ENV_LCORE_ID_ANY, SPDK_MALLOC_DMA); 1088 if (!firm_ctx->fw_image) { 1089 close(fd); 1090 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 1091 "Memory allocation error."); 1092 goto err; 1093 } 1094 firm_ctx->p = firm_ctx->fw_image; 1095 1096 if (read(fd, firm_ctx->p, firm_ctx->size) != ((ssize_t)(firm_ctx->size))) { 1097 close(fd); 1098 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 1099 "Read firmware image failed!"); 1100 goto err; 1101 } 1102 close(fd); 1103 1104 firm_ctx->offset = 0; 1105 firm_ctx->size_remaining = firm_ctx->size; 1106 firm_ctx->transfer = spdk_min(firm_ctx->size_remaining, 4096); 1107 1108 cmd.opc = SPDK_NVME_OPC_FIRMWARE_IMAGE_DOWNLOAD; 1109 cmd.cdw10 = spdk_nvme_bytes_to_numd(firm_ctx->transfer); 1110 cmd.cdw11 = firm_ctx->offset >> 2; 1111 1112 rc = spdk_bdev_nvme_admin_passthru(firm_ctx->desc, firm_ctx->ch, &cmd, firm_ctx->p, 1113 firm_ctx->transfer, apply_firmware_complete, firm_ctx); 1114 if (rc == 0) { 1115 /* normal return here. */ 1116 return; 1117 } 1118 1119 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 1120 "Read firmware image failed!"); 1121 err: 1122 apply_firmware_cleanup(firm_ctx); 1123 } 1124 SPDK_RPC_REGISTER("bdev_nvme_apply_firmware", rpc_bdev_nvme_apply_firmware, SPDK_RPC_RUNTIME) 1125 1126 struct rpc_bdev_nvme_transport_stat_ctx { 1127 struct spdk_jsonrpc_request *request; 1128 struct spdk_json_write_ctx *w; 1129 }; 1130 1131 static void 1132 rpc_bdev_nvme_rdma_stats(struct spdk_json_write_ctx *w, 1133 struct spdk_nvme_transport_poll_group_stat *stat) 1134 { 1135 struct spdk_nvme_rdma_device_stat *device_stats; 1136 uint32_t i; 1137 1138 spdk_json_write_named_array_begin(w, "devices"); 1139 1140 for (i = 0; i < stat->rdma.num_devices; i++) { 1141 device_stats = &stat->rdma.device_stats[i]; 1142 spdk_json_write_object_begin(w); 1143 spdk_json_write_named_string(w, "dev_name", device_stats->name); 1144 spdk_json_write_named_uint64(w, "polls", device_stats->polls); 1145 spdk_json_write_named_uint64(w, "idle_polls", device_stats->idle_polls); 1146 spdk_json_write_named_uint64(w, "completions", device_stats->completions); 1147 spdk_json_write_named_uint64(w, "queued_requests", device_stats->queued_requests); 1148 spdk_json_write_named_uint64(w, "total_send_wrs", device_stats->total_send_wrs); 1149 spdk_json_write_named_uint64(w, "send_doorbell_updates", device_stats->send_doorbell_updates); 1150 spdk_json_write_named_uint64(w, "total_recv_wrs", device_stats->total_recv_wrs); 1151 spdk_json_write_named_uint64(w, "recv_doorbell_updates", device_stats->recv_doorbell_updates); 1152 spdk_json_write_object_end(w); 1153 } 1154 spdk_json_write_array_end(w); 1155 } 1156 1157 static void 1158 rpc_bdev_nvme_pcie_stats(struct spdk_json_write_ctx *w, 1159 struct spdk_nvme_transport_poll_group_stat *stat) 1160 { 1161 spdk_json_write_named_uint64(w, "polls", stat->pcie.polls); 1162 spdk_json_write_named_uint64(w, "idle_polls", stat->pcie.idle_polls); 1163 spdk_json_write_named_uint64(w, "completions", stat->pcie.completions); 1164 spdk_json_write_named_uint64(w, "cq_mmio_doorbell_updates", stat->pcie.cq_mmio_doorbell_updates); 1165 spdk_json_write_named_uint64(w, "cq_shadow_doorbell_updates", 1166 stat->pcie.cq_shadow_doorbell_updates); 1167 spdk_json_write_named_uint64(w, "queued_requests", stat->pcie.queued_requests); 1168 spdk_json_write_named_uint64(w, "submitted_requests", stat->pcie.submitted_requests); 1169 spdk_json_write_named_uint64(w, "sq_mmio_doorbell_updates", stat->pcie.sq_mmio_doorbell_updates); 1170 spdk_json_write_named_uint64(w, "sq_shadow_doorbell_updates", 1171 stat->pcie.sq_shadow_doorbell_updates); 1172 } 1173 1174 static void 1175 rpc_bdev_nvme_tcp_stats(struct spdk_json_write_ctx *w, 1176 struct spdk_nvme_transport_poll_group_stat *stat) 1177 { 1178 spdk_json_write_named_uint64(w, "polls", stat->tcp.polls); 1179 spdk_json_write_named_uint64(w, "idle_polls", stat->tcp.idle_polls); 1180 spdk_json_write_named_uint64(w, "socket_completions", stat->tcp.socket_completions); 1181 spdk_json_write_named_uint64(w, "nvme_completions", stat->tcp.nvme_completions); 1182 spdk_json_write_named_uint64(w, "queued_requests", stat->tcp.queued_requests); 1183 spdk_json_write_named_uint64(w, "submitted_requests", stat->tcp.submitted_requests); 1184 } 1185 1186 static void 1187 rpc_bdev_nvme_stats_per_channel(struct spdk_io_channel_iter *i) 1188 { 1189 struct rpc_bdev_nvme_transport_stat_ctx *ctx; 1190 struct spdk_io_channel *ch; 1191 struct nvme_poll_group *group; 1192 struct spdk_nvme_poll_group_stat *stat; 1193 struct spdk_nvme_transport_poll_group_stat *tr_stat; 1194 uint32_t j; 1195 int rc; 1196 1197 ctx = spdk_io_channel_iter_get_ctx(i); 1198 ch = spdk_io_channel_iter_get_channel(i); 1199 group = spdk_io_channel_get_ctx(ch); 1200 1201 rc = spdk_nvme_poll_group_get_stats(group->group, &stat); 1202 if (rc) { 1203 spdk_for_each_channel_continue(i, rc); 1204 return; 1205 } 1206 1207 spdk_json_write_object_begin(ctx->w); 1208 spdk_json_write_named_string(ctx->w, "thread", spdk_thread_get_name(spdk_get_thread())); 1209 spdk_json_write_named_array_begin(ctx->w, "transports"); 1210 1211 for (j = 0; j < stat->num_transports; j++) { 1212 tr_stat = stat->transport_stat[j]; 1213 spdk_json_write_object_begin(ctx->w); 1214 spdk_json_write_named_string(ctx->w, "trname", spdk_nvme_transport_id_trtype_str(tr_stat->trtype)); 1215 1216 switch (stat->transport_stat[j]->trtype) { 1217 case SPDK_NVME_TRANSPORT_RDMA: 1218 rpc_bdev_nvme_rdma_stats(ctx->w, tr_stat); 1219 break; 1220 case SPDK_NVME_TRANSPORT_PCIE: 1221 case SPDK_NVME_TRANSPORT_VFIOUSER: 1222 rpc_bdev_nvme_pcie_stats(ctx->w, tr_stat); 1223 break; 1224 case SPDK_NVME_TRANSPORT_TCP: 1225 rpc_bdev_nvme_tcp_stats(ctx->w, tr_stat); 1226 break; 1227 default: 1228 SPDK_WARNLOG("Can't handle trtype %d %s\n", tr_stat->trtype, 1229 spdk_nvme_transport_id_trtype_str(tr_stat->trtype)); 1230 } 1231 spdk_json_write_object_end(ctx->w); 1232 } 1233 /* transports array */ 1234 spdk_json_write_array_end(ctx->w); 1235 spdk_json_write_object_end(ctx->w); 1236 1237 spdk_nvme_poll_group_free_stats(group->group, stat); 1238 spdk_for_each_channel_continue(i, 0); 1239 } 1240 1241 static void 1242 rpc_bdev_nvme_stats_done(struct spdk_io_channel_iter *i, int status) 1243 { 1244 struct rpc_bdev_nvme_transport_stat_ctx *ctx = spdk_io_channel_iter_get_ctx(i); 1245 1246 spdk_json_write_array_end(ctx->w); 1247 spdk_json_write_object_end(ctx->w); 1248 spdk_jsonrpc_end_result(ctx->request, ctx->w); 1249 free(ctx); 1250 } 1251 1252 static void 1253 rpc_bdev_nvme_get_transport_statistics(struct spdk_jsonrpc_request *request, 1254 const struct spdk_json_val *params) 1255 { 1256 struct rpc_bdev_nvme_transport_stat_ctx *ctx; 1257 1258 if (params) { 1259 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, 1260 "'bdev_nvme_get_transport_statistics' requires no arguments"); 1261 return; 1262 } 1263 1264 ctx = calloc(1, sizeof(*ctx)); 1265 if (!ctx) { 1266 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 1267 "Memory allocation error"); 1268 return; 1269 } 1270 ctx->request = request; 1271 ctx->w = spdk_jsonrpc_begin_result(ctx->request); 1272 spdk_json_write_object_begin(ctx->w); 1273 spdk_json_write_named_array_begin(ctx->w, "poll_groups"); 1274 1275 spdk_for_each_channel(&g_nvme_bdev_ctrlrs, 1276 rpc_bdev_nvme_stats_per_channel, 1277 ctx, 1278 rpc_bdev_nvme_stats_done); 1279 } 1280 SPDK_RPC_REGISTER("bdev_nvme_get_transport_statistics", rpc_bdev_nvme_get_transport_statistics, 1281 SPDK_RPC_RUNTIME) 1282 1283 struct rpc_bdev_nvme_controller_op_req { 1284 char *name; 1285 uint16_t cntlid; 1286 }; 1287 1288 static void 1289 free_rpc_bdev_nvme_controller_op_req(struct rpc_bdev_nvme_controller_op_req *r) 1290 { 1291 free(r->name); 1292 } 1293 1294 static const struct spdk_json_object_decoder rpc_bdev_nvme_controller_op_req_decoders[] = { 1295 {"name", offsetof(struct rpc_bdev_nvme_controller_op_req, name), spdk_json_decode_string}, 1296 {"cntlid", offsetof(struct rpc_bdev_nvme_controller_op_req, cntlid), spdk_json_decode_uint16, true}, 1297 }; 1298 1299 static void 1300 rpc_bdev_nvme_controller_op_cb(void *cb_arg, int rc) 1301 { 1302 struct spdk_jsonrpc_request *request = cb_arg; 1303 1304 if (rc == 0) { 1305 spdk_jsonrpc_send_bool_response(request, true); 1306 } else { 1307 spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc)); 1308 } 1309 } 1310 1311 static void 1312 rpc_bdev_nvme_controller_op(struct spdk_jsonrpc_request *request, 1313 const struct spdk_json_val *params, 1314 enum nvme_ctrlr_op op) 1315 { 1316 struct rpc_bdev_nvme_controller_op_req req = {NULL}; 1317 struct nvme_bdev_ctrlr *nbdev_ctrlr; 1318 struct nvme_ctrlr *nvme_ctrlr; 1319 1320 if (spdk_json_decode_object(params, rpc_bdev_nvme_controller_op_req_decoders, 1321 SPDK_COUNTOF(rpc_bdev_nvme_controller_op_req_decoders), 1322 &req)) { 1323 SPDK_ERRLOG("spdk_json_decode_object failed\n"); 1324 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, spdk_strerror(EINVAL)); 1325 goto exit; 1326 } 1327 1328 nbdev_ctrlr = nvme_bdev_ctrlr_get_by_name(req.name); 1329 if (nbdev_ctrlr == NULL) { 1330 SPDK_ERRLOG("Failed at NVMe bdev controller lookup\n"); 1331 spdk_jsonrpc_send_error_response(request, -ENODEV, spdk_strerror(ENODEV)); 1332 goto exit; 1333 } 1334 1335 if (req.cntlid == 0) { 1336 nvme_bdev_ctrlr_op_rpc(nbdev_ctrlr, op, rpc_bdev_nvme_controller_op_cb, request); 1337 } else { 1338 nvme_ctrlr = nvme_bdev_ctrlr_get_ctrlr_by_id(nbdev_ctrlr, req.cntlid); 1339 if (nvme_ctrlr == NULL) { 1340 SPDK_ERRLOG("Failed at NVMe controller lookup\n"); 1341 spdk_jsonrpc_send_error_response(request, -ENODEV, spdk_strerror(ENODEV)); 1342 goto exit; 1343 } 1344 nvme_ctrlr_op_rpc(nvme_ctrlr, op, rpc_bdev_nvme_controller_op_cb, request); 1345 } 1346 1347 exit: 1348 free_rpc_bdev_nvme_controller_op_req(&req); 1349 } 1350 1351 static void 1352 rpc_bdev_nvme_reset_controller(struct spdk_jsonrpc_request *request, 1353 const struct spdk_json_val *params) 1354 { 1355 rpc_bdev_nvme_controller_op(request, params, NVME_CTRLR_OP_RESET); 1356 } 1357 SPDK_RPC_REGISTER("bdev_nvme_reset_controller", rpc_bdev_nvme_reset_controller, SPDK_RPC_RUNTIME) 1358 1359 static void 1360 rpc_bdev_nvme_enable_controller(struct spdk_jsonrpc_request *request, 1361 const struct spdk_json_val *params) 1362 { 1363 rpc_bdev_nvme_controller_op(request, params, NVME_CTRLR_OP_ENABLE); 1364 } 1365 SPDK_RPC_REGISTER("bdev_nvme_enable_controller", rpc_bdev_nvme_enable_controller, SPDK_RPC_RUNTIME) 1366 1367 static void 1368 rpc_bdev_nvme_disable_controller(struct spdk_jsonrpc_request *request, 1369 const struct spdk_json_val *params) 1370 { 1371 rpc_bdev_nvme_controller_op(request, params, NVME_CTRLR_OP_DISABLE); 1372 } 1373 SPDK_RPC_REGISTER("bdev_nvme_disable_controller", rpc_bdev_nvme_disable_controller, 1374 SPDK_RPC_RUNTIME) 1375 1376 struct rpc_get_controller_health_info { 1377 char *name; 1378 }; 1379 1380 struct spdk_nvme_health_info_context { 1381 struct spdk_jsonrpc_request *request; 1382 struct spdk_nvme_ctrlr *ctrlr; 1383 struct spdk_nvme_health_information_page health_page; 1384 }; 1385 1386 static void 1387 free_rpc_get_controller_health_info(struct rpc_get_controller_health_info *r) 1388 { 1389 free(r->name); 1390 } 1391 1392 static const struct spdk_json_object_decoder rpc_get_controller_health_info_decoders[] = { 1393 {"name", offsetof(struct rpc_get_controller_health_info, name), spdk_json_decode_string, true}, 1394 }; 1395 1396 static void 1397 nvme_health_info_cleanup(struct spdk_nvme_health_info_context *context, bool response) 1398 { 1399 if (response == true) { 1400 spdk_jsonrpc_send_error_response(context->request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 1401 "Internal error."); 1402 } 1403 1404 free(context); 1405 } 1406 1407 static void 1408 get_health_log_page_completion(void *cb_arg, const struct spdk_nvme_cpl *cpl) 1409 { 1410 int i; 1411 char buf[128]; 1412 struct spdk_nvme_health_info_context *context = cb_arg; 1413 struct spdk_jsonrpc_request *request = context->request; 1414 struct spdk_json_write_ctx *w; 1415 struct spdk_nvme_ctrlr *ctrlr = context->ctrlr; 1416 const struct spdk_nvme_transport_id *trid = NULL; 1417 const struct spdk_nvme_ctrlr_data *cdata = NULL; 1418 struct spdk_nvme_health_information_page *health_page = NULL; 1419 1420 if (spdk_nvme_cpl_is_error(cpl)) { 1421 nvme_health_info_cleanup(context, true); 1422 SPDK_ERRLOG("get log page failed\n"); 1423 return; 1424 } 1425 1426 if (ctrlr == NULL) { 1427 nvme_health_info_cleanup(context, true); 1428 SPDK_ERRLOG("ctrlr is NULL\n"); 1429 return; 1430 } else { 1431 trid = spdk_nvme_ctrlr_get_transport_id(ctrlr); 1432 cdata = spdk_nvme_ctrlr_get_data(ctrlr); 1433 health_page = &(context->health_page); 1434 } 1435 1436 w = spdk_jsonrpc_begin_result(request); 1437 1438 spdk_json_write_object_begin(w); 1439 snprintf(buf, sizeof(cdata->mn) + 1, "%s", cdata->mn); 1440 spdk_str_trim(buf); 1441 spdk_json_write_named_string(w, "model_number", buf); 1442 snprintf(buf, sizeof(cdata->sn) + 1, "%s", cdata->sn); 1443 spdk_str_trim(buf); 1444 spdk_json_write_named_string(w, "serial_number", buf); 1445 snprintf(buf, sizeof(cdata->fr) + 1, "%s", cdata->fr); 1446 spdk_str_trim(buf); 1447 spdk_json_write_named_string(w, "firmware_revision", buf); 1448 spdk_json_write_named_string(w, "traddr", trid->traddr); 1449 spdk_json_write_named_uint64(w, "temperature_celsius", health_page->temperature - 273); 1450 spdk_json_write_named_uint64(w, "available_spare_percentage", health_page->available_spare); 1451 spdk_json_write_named_uint64(w, "available_spare_threshold_percentage", 1452 health_page->available_spare_threshold); 1453 spdk_json_write_named_uint64(w, "percentage_used", health_page->percentage_used); 1454 spdk_json_write_named_uint128(w, "data_units_read", 1455 health_page->data_units_read[0], health_page->data_units_read[1]); 1456 spdk_json_write_named_uint128(w, "data_units_written", 1457 health_page->data_units_written[0], health_page->data_units_written[1]); 1458 spdk_json_write_named_uint128(w, "host_read_commands", 1459 health_page->host_read_commands[0], health_page->host_read_commands[1]); 1460 spdk_json_write_named_uint128(w, "host_write_commands", 1461 health_page->host_write_commands[0], health_page->host_write_commands[1]); 1462 spdk_json_write_named_uint128(w, "controller_busy_time", 1463 health_page->controller_busy_time[0], health_page->controller_busy_time[1]); 1464 spdk_json_write_named_uint128(w, "power_cycles", 1465 health_page->power_cycles[0], health_page->power_cycles[1]); 1466 spdk_json_write_named_uint128(w, "power_on_hours", 1467 health_page->power_on_hours[0], health_page->power_on_hours[1]); 1468 spdk_json_write_named_uint128(w, "unsafe_shutdowns", 1469 health_page->unsafe_shutdowns[0], health_page->unsafe_shutdowns[1]); 1470 spdk_json_write_named_uint128(w, "media_errors", 1471 health_page->media_errors[0], health_page->media_errors[1]); 1472 spdk_json_write_named_uint128(w, "num_err_log_entries", 1473 health_page->num_error_info_log_entries[0], health_page->num_error_info_log_entries[1]); 1474 spdk_json_write_named_uint64(w, "warning_temperature_time_minutes", health_page->warning_temp_time); 1475 spdk_json_write_named_uint64(w, "critical_composite_temperature_time_minutes", 1476 health_page->critical_temp_time); 1477 for (i = 0; i < 8; i++) { 1478 if (health_page->temp_sensor[i] != 0) { 1479 spdk_json_write_named_uint64(w, "temperature_sensor_celsius", health_page->temp_sensor[i] - 273); 1480 } 1481 } 1482 spdk_json_write_object_end(w); 1483 1484 spdk_jsonrpc_end_result(request, w); 1485 nvme_health_info_cleanup(context, false); 1486 } 1487 1488 static void 1489 get_health_log_page(struct spdk_nvme_health_info_context *context) 1490 { 1491 struct spdk_nvme_ctrlr *ctrlr = context->ctrlr; 1492 1493 if (spdk_nvme_ctrlr_cmd_get_log_page(ctrlr, SPDK_NVME_LOG_HEALTH_INFORMATION, 1494 SPDK_NVME_GLOBAL_NS_TAG, 1495 &(context->health_page), sizeof(context->health_page), 0, 1496 get_health_log_page_completion, context)) { 1497 nvme_health_info_cleanup(context, true); 1498 SPDK_ERRLOG("spdk_nvme_ctrlr_cmd_get_log_page() failed\n"); 1499 } 1500 } 1501 1502 static void 1503 get_temperature_threshold_feature_completion(void *cb_arg, const struct spdk_nvme_cpl *cpl) 1504 { 1505 struct spdk_nvme_health_info_context *context = cb_arg; 1506 1507 if (spdk_nvme_cpl_is_error(cpl)) { 1508 nvme_health_info_cleanup(context, true); 1509 SPDK_ERRLOG("feature SPDK_NVME_FEAT_TEMPERATURE_THRESHOLD failed in completion\n"); 1510 } else { 1511 get_health_log_page(context); 1512 } 1513 } 1514 1515 static int 1516 get_temperature_threshold_feature(struct spdk_nvme_health_info_context *context) 1517 { 1518 struct spdk_nvme_cmd cmd = {}; 1519 1520 cmd.opc = SPDK_NVME_OPC_GET_FEATURES; 1521 cmd.cdw10 = SPDK_NVME_FEAT_TEMPERATURE_THRESHOLD; 1522 1523 return spdk_nvme_ctrlr_cmd_admin_raw(context->ctrlr, &cmd, NULL, 0, 1524 get_temperature_threshold_feature_completion, context); 1525 } 1526 1527 static void 1528 get_controller_health_info(struct spdk_jsonrpc_request *request, struct spdk_nvme_ctrlr *ctrlr) 1529 { 1530 struct spdk_nvme_health_info_context *context; 1531 1532 context = calloc(1, sizeof(struct spdk_nvme_health_info_context)); 1533 if (!context) { 1534 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 1535 "Memory allocation error."); 1536 return; 1537 } 1538 1539 context->request = request; 1540 context->ctrlr = ctrlr; 1541 1542 if (get_temperature_threshold_feature(context)) { 1543 nvme_health_info_cleanup(context, true); 1544 SPDK_ERRLOG("feature SPDK_NVME_FEAT_TEMPERATURE_THRESHOLD failed to submit\n"); 1545 } 1546 1547 return; 1548 } 1549 1550 static void 1551 rpc_bdev_nvme_get_controller_health_info(struct spdk_jsonrpc_request *request, 1552 const struct spdk_json_val *params) 1553 { 1554 struct rpc_get_controller_health_info req = {}; 1555 struct nvme_ctrlr *nvme_ctrlr = NULL; 1556 1557 if (!params) { 1558 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 1559 "Missing device name"); 1560 1561 return; 1562 } 1563 if (spdk_json_decode_object(params, rpc_get_controller_health_info_decoders, 1564 SPDK_COUNTOF(rpc_get_controller_health_info_decoders), &req)) { 1565 SPDK_ERRLOG("spdk_json_decode_object failed\n"); 1566 free_rpc_get_controller_health_info(&req); 1567 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 1568 "Invalid parameters"); 1569 1570 return; 1571 } 1572 1573 nvme_ctrlr = nvme_ctrlr_get_by_name(req.name); 1574 1575 if (!nvme_ctrlr) { 1576 SPDK_ERRLOG("nvme ctrlr name '%s' does not exist\n", req.name); 1577 free_rpc_get_controller_health_info(&req); 1578 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 1579 "Device not found"); 1580 return; 1581 } 1582 1583 get_controller_health_info(request, nvme_ctrlr->ctrlr); 1584 free_rpc_get_controller_health_info(&req); 1585 1586 return; 1587 } 1588 SPDK_RPC_REGISTER("bdev_nvme_get_controller_health_info", 1589 rpc_bdev_nvme_get_controller_health_info, SPDK_RPC_RUNTIME) 1590 1591 struct rpc_bdev_nvme_start_discovery { 1592 char *name; 1593 char *trtype; 1594 char *adrfam; 1595 char *traddr; 1596 char *trsvcid; 1597 char *hostnqn; 1598 bool wait_for_attach; 1599 uint64_t attach_timeout_ms; 1600 struct spdk_nvme_ctrlr_opts opts; 1601 struct nvme_ctrlr_opts bdev_opts; 1602 }; 1603 1604 static void 1605 free_rpc_bdev_nvme_start_discovery(struct rpc_bdev_nvme_start_discovery *req) 1606 { 1607 free(req->name); 1608 free(req->trtype); 1609 free(req->adrfam); 1610 free(req->traddr); 1611 free(req->trsvcid); 1612 free(req->hostnqn); 1613 } 1614 1615 static const struct spdk_json_object_decoder rpc_bdev_nvme_start_discovery_decoders[] = { 1616 {"name", offsetof(struct rpc_bdev_nvme_start_discovery, name), spdk_json_decode_string}, 1617 {"trtype", offsetof(struct rpc_bdev_nvme_start_discovery, trtype), spdk_json_decode_string}, 1618 {"traddr", offsetof(struct rpc_bdev_nvme_start_discovery, traddr), spdk_json_decode_string}, 1619 {"adrfam", offsetof(struct rpc_bdev_nvme_start_discovery, adrfam), spdk_json_decode_string, true}, 1620 {"trsvcid", offsetof(struct rpc_bdev_nvme_start_discovery, trsvcid), spdk_json_decode_string, true}, 1621 {"hostnqn", offsetof(struct rpc_bdev_nvme_start_discovery, hostnqn), spdk_json_decode_string, true}, 1622 {"wait_for_attach", offsetof(struct rpc_bdev_nvme_start_discovery, wait_for_attach), spdk_json_decode_bool, true}, 1623 {"attach_timeout_ms", offsetof(struct rpc_bdev_nvme_start_discovery, attach_timeout_ms), spdk_json_decode_uint64, true}, 1624 {"ctrlr_loss_timeout_sec", offsetof(struct rpc_bdev_nvme_start_discovery, bdev_opts.ctrlr_loss_timeout_sec), spdk_json_decode_int32, true}, 1625 {"reconnect_delay_sec", offsetof(struct rpc_bdev_nvme_start_discovery, bdev_opts.reconnect_delay_sec), spdk_json_decode_uint32, true}, 1626 {"fast_io_fail_timeout_sec", offsetof(struct rpc_bdev_nvme_start_discovery, bdev_opts.fast_io_fail_timeout_sec), spdk_json_decode_uint32, true}, 1627 }; 1628 1629 struct rpc_bdev_nvme_start_discovery_ctx { 1630 struct rpc_bdev_nvme_start_discovery req; 1631 struct spdk_jsonrpc_request *request; 1632 }; 1633 1634 static void 1635 rpc_bdev_nvme_start_discovery_done(void *ctx, int status) 1636 { 1637 struct spdk_jsonrpc_request *request = ctx; 1638 1639 if (status != 0) { 1640 spdk_jsonrpc_send_error_response(request, status, spdk_strerror(-status)); 1641 } else { 1642 spdk_jsonrpc_send_bool_response(request, true); 1643 } 1644 } 1645 1646 static void 1647 rpc_bdev_nvme_start_discovery(struct spdk_jsonrpc_request *request, 1648 const struct spdk_json_val *params) 1649 { 1650 struct rpc_bdev_nvme_start_discovery_ctx *ctx; 1651 struct spdk_nvme_transport_id trid = {}; 1652 size_t len, maxlen; 1653 int rc; 1654 spdk_bdev_nvme_start_discovery_fn cb_fn; 1655 void *cb_ctx; 1656 1657 ctx = calloc(1, sizeof(*ctx)); 1658 if (!ctx) { 1659 spdk_jsonrpc_send_error_response(request, -ENOMEM, spdk_strerror(ENOMEM)); 1660 return; 1661 } 1662 1663 spdk_nvme_ctrlr_get_default_ctrlr_opts(&ctx->req.opts, sizeof(ctx->req.opts)); 1664 1665 if (spdk_json_decode_object(params, rpc_bdev_nvme_start_discovery_decoders, 1666 SPDK_COUNTOF(rpc_bdev_nvme_start_discovery_decoders), 1667 &ctx->req)) { 1668 SPDK_ERRLOG("spdk_json_decode_object failed\n"); 1669 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 1670 "spdk_json_decode_object failed"); 1671 goto cleanup; 1672 } 1673 1674 /* Parse trstring */ 1675 rc = spdk_nvme_transport_id_populate_trstring(&trid, ctx->req.trtype); 1676 if (rc < 0) { 1677 SPDK_ERRLOG("Failed to parse trtype: %s\n", ctx->req.trtype); 1678 spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "Failed to parse trtype: %s", 1679 ctx->req.trtype); 1680 goto cleanup; 1681 } 1682 1683 /* Parse trtype */ 1684 rc = spdk_nvme_transport_id_parse_trtype(&trid.trtype, ctx->req.trtype); 1685 assert(rc == 0); 1686 1687 /* Parse traddr */ 1688 maxlen = sizeof(trid.traddr); 1689 len = strnlen(ctx->req.traddr, maxlen); 1690 if (len == maxlen) { 1691 spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "traddr too long: %s", 1692 ctx->req.traddr); 1693 goto cleanup; 1694 } 1695 memcpy(trid.traddr, ctx->req.traddr, len + 1); 1696 1697 /* Parse adrfam */ 1698 if (ctx->req.adrfam) { 1699 rc = spdk_nvme_transport_id_parse_adrfam(&trid.adrfam, ctx->req.adrfam); 1700 if (rc < 0) { 1701 SPDK_ERRLOG("Failed to parse adrfam: %s\n", ctx->req.adrfam); 1702 spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "Failed to parse adrfam: %s", 1703 ctx->req.adrfam); 1704 goto cleanup; 1705 } 1706 } 1707 1708 /* Parse trsvcid */ 1709 if (ctx->req.trsvcid) { 1710 maxlen = sizeof(trid.trsvcid); 1711 len = strnlen(ctx->req.trsvcid, maxlen); 1712 if (len == maxlen) { 1713 spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "trsvcid too long: %s", 1714 ctx->req.trsvcid); 1715 goto cleanup; 1716 } 1717 memcpy(trid.trsvcid, ctx->req.trsvcid, len + 1); 1718 } 1719 1720 if (ctx->req.hostnqn) { 1721 snprintf(ctx->req.opts.hostnqn, sizeof(ctx->req.opts.hostnqn), "%s", 1722 ctx->req.hostnqn); 1723 } 1724 1725 if (ctx->req.attach_timeout_ms != 0) { 1726 ctx->req.wait_for_attach = true; 1727 } 1728 1729 ctx->request = request; 1730 cb_fn = ctx->req.wait_for_attach ? rpc_bdev_nvme_start_discovery_done : NULL; 1731 cb_ctx = ctx->req.wait_for_attach ? request : NULL; 1732 rc = bdev_nvme_start_discovery(&trid, ctx->req.name, &ctx->req.opts, &ctx->req.bdev_opts, 1733 ctx->req.attach_timeout_ms, false, cb_fn, cb_ctx); 1734 if (rc) { 1735 spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc)); 1736 } else if (!ctx->req.wait_for_attach) { 1737 rpc_bdev_nvme_start_discovery_done(request, 0); 1738 } 1739 1740 cleanup: 1741 free_rpc_bdev_nvme_start_discovery(&ctx->req); 1742 free(ctx); 1743 } 1744 SPDK_RPC_REGISTER("bdev_nvme_start_discovery", rpc_bdev_nvme_start_discovery, 1745 SPDK_RPC_RUNTIME) 1746 1747 struct rpc_bdev_nvme_stop_discovery { 1748 char *name; 1749 }; 1750 1751 static const struct spdk_json_object_decoder rpc_bdev_nvme_stop_discovery_decoders[] = { 1752 {"name", offsetof(struct rpc_bdev_nvme_stop_discovery, name), spdk_json_decode_string}, 1753 }; 1754 1755 struct rpc_bdev_nvme_stop_discovery_ctx { 1756 struct rpc_bdev_nvme_stop_discovery req; 1757 struct spdk_jsonrpc_request *request; 1758 }; 1759 1760 static void 1761 rpc_bdev_nvme_stop_discovery_done(void *cb_ctx) 1762 { 1763 struct rpc_bdev_nvme_stop_discovery_ctx *ctx = cb_ctx; 1764 1765 spdk_jsonrpc_send_bool_response(ctx->request, true); 1766 free(ctx->req.name); 1767 free(ctx); 1768 } 1769 1770 static void 1771 rpc_bdev_nvme_stop_discovery(struct spdk_jsonrpc_request *request, 1772 const struct spdk_json_val *params) 1773 { 1774 struct rpc_bdev_nvme_stop_discovery_ctx *ctx; 1775 int rc; 1776 1777 ctx = calloc(1, sizeof(*ctx)); 1778 if (!ctx) { 1779 spdk_jsonrpc_send_error_response(request, -ENOMEM, spdk_strerror(ENOMEM)); 1780 return; 1781 } 1782 1783 if (spdk_json_decode_object(params, rpc_bdev_nvme_stop_discovery_decoders, 1784 SPDK_COUNTOF(rpc_bdev_nvme_stop_discovery_decoders), 1785 &ctx->req)) { 1786 SPDK_ERRLOG("spdk_json_decode_object failed\n"); 1787 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 1788 "spdk_json_decode_object failed"); 1789 goto cleanup; 1790 } 1791 1792 ctx->request = request; 1793 rc = bdev_nvme_stop_discovery(ctx->req.name, rpc_bdev_nvme_stop_discovery_done, ctx); 1794 if (rc) { 1795 spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc)); 1796 goto cleanup; 1797 } 1798 1799 return; 1800 1801 cleanup: 1802 free(ctx->req.name); 1803 free(ctx); 1804 } 1805 SPDK_RPC_REGISTER("bdev_nvme_stop_discovery", rpc_bdev_nvme_stop_discovery, 1806 SPDK_RPC_RUNTIME) 1807 1808 static void 1809 rpc_bdev_nvme_get_discovery_info(struct spdk_jsonrpc_request *request, 1810 const struct spdk_json_val *params) 1811 { 1812 struct spdk_json_write_ctx *w; 1813 1814 w = spdk_jsonrpc_begin_result(request); 1815 bdev_nvme_get_discovery_info(w); 1816 spdk_jsonrpc_end_result(request, w); 1817 } 1818 SPDK_RPC_REGISTER("bdev_nvme_get_discovery_info", rpc_bdev_nvme_get_discovery_info, 1819 SPDK_RPC_RUNTIME) 1820 1821 enum error_injection_cmd_type { 1822 NVME_ADMIN_CMD = 1, 1823 NVME_IO_CMD, 1824 }; 1825 1826 struct rpc_add_error_injection { 1827 char *name; 1828 enum error_injection_cmd_type cmd_type; 1829 uint8_t opc; 1830 bool do_not_submit; 1831 uint64_t timeout_in_us; 1832 uint32_t err_count; 1833 uint8_t sct; 1834 uint8_t sc; 1835 }; 1836 1837 static void 1838 free_rpc_add_error_injection(struct rpc_add_error_injection *req) 1839 { 1840 free(req->name); 1841 } 1842 1843 static int 1844 rpc_error_injection_decode_cmd_type(const struct spdk_json_val *val, void *out) 1845 { 1846 int *cmd_type = out; 1847 1848 if (spdk_json_strequal(val, "admin")) { 1849 *cmd_type = NVME_ADMIN_CMD; 1850 } else if (spdk_json_strequal(val, "io")) { 1851 *cmd_type = NVME_IO_CMD; 1852 } else { 1853 SPDK_ERRLOG("Invalid parameter value: cmd_type\n"); 1854 return -EINVAL; 1855 } 1856 1857 return 0; 1858 } 1859 1860 static const struct spdk_json_object_decoder rpc_add_error_injection_decoders[] = { 1861 { "name", offsetof(struct rpc_add_error_injection, name), spdk_json_decode_string }, 1862 { "cmd_type", offsetof(struct rpc_add_error_injection, cmd_type), rpc_error_injection_decode_cmd_type }, 1863 { "opc", offsetof(struct rpc_add_error_injection, opc), spdk_json_decode_uint8 }, 1864 { "do_not_submit", offsetof(struct rpc_add_error_injection, do_not_submit), spdk_json_decode_bool, true }, 1865 { "timeout_in_us", offsetof(struct rpc_add_error_injection, timeout_in_us), spdk_json_decode_uint64, true }, 1866 { "err_count", offsetof(struct rpc_add_error_injection, err_count), spdk_json_decode_uint32, true }, 1867 { "sct", offsetof(struct rpc_add_error_injection, sct), spdk_json_decode_uint8, true}, 1868 { "sc", offsetof(struct rpc_add_error_injection, sc), spdk_json_decode_uint8, true}, 1869 }; 1870 1871 struct rpc_add_error_injection_ctx { 1872 struct spdk_jsonrpc_request *request; 1873 struct rpc_add_error_injection rpc; 1874 }; 1875 1876 static void 1877 rpc_add_error_injection_done(struct spdk_io_channel_iter *i, int status) 1878 { 1879 struct rpc_add_error_injection_ctx *ctx = spdk_io_channel_iter_get_ctx(i); 1880 1881 if (status) { 1882 spdk_jsonrpc_send_error_response(ctx->request, status, 1883 "Failed to add the error injection."); 1884 } else { 1885 spdk_jsonrpc_send_bool_response(ctx->request, true); 1886 } 1887 1888 free_rpc_add_error_injection(&ctx->rpc); 1889 free(ctx); 1890 } 1891 1892 static void 1893 rpc_add_error_injection_per_channel(struct spdk_io_channel_iter *i) 1894 { 1895 struct spdk_io_channel *ch = spdk_io_channel_iter_get_channel(i); 1896 struct rpc_add_error_injection_ctx *ctx = spdk_io_channel_iter_get_ctx(i); 1897 struct nvme_ctrlr_channel *ctrlr_ch = spdk_io_channel_get_ctx(ch); 1898 struct spdk_nvme_qpair *qpair = ctrlr_ch->qpair->qpair; 1899 struct spdk_nvme_ctrlr *ctrlr = ctrlr_ch->qpair->ctrlr->ctrlr; 1900 int rc = 0; 1901 1902 if (qpair != NULL) { 1903 rc = spdk_nvme_qpair_add_cmd_error_injection(ctrlr, qpair, ctx->rpc.opc, 1904 ctx->rpc.do_not_submit, ctx->rpc.timeout_in_us, ctx->rpc.err_count, 1905 ctx->rpc.sct, ctx->rpc.sc); 1906 } 1907 1908 spdk_for_each_channel_continue(i, rc); 1909 } 1910 1911 static void 1912 rpc_bdev_nvme_add_error_injection( 1913 struct spdk_jsonrpc_request *request, 1914 const struct spdk_json_val *params) 1915 { 1916 struct rpc_add_error_injection_ctx *ctx; 1917 struct nvme_ctrlr *nvme_ctrlr; 1918 int rc; 1919 1920 ctx = calloc(1, sizeof(*ctx)); 1921 if (!ctx) { 1922 spdk_jsonrpc_send_error_response(request, -ENOMEM, spdk_strerror(ENOMEM)); 1923 return; 1924 } 1925 ctx->rpc.err_count = 1; 1926 ctx->request = request; 1927 1928 if (spdk_json_decode_object(params, 1929 rpc_add_error_injection_decoders, 1930 SPDK_COUNTOF(rpc_add_error_injection_decoders), 1931 &ctx->rpc)) { 1932 spdk_jsonrpc_send_error_response(request, -EINVAL, 1933 "Failed to parse the request"); 1934 goto cleanup; 1935 } 1936 1937 nvme_ctrlr = nvme_ctrlr_get_by_name(ctx->rpc.name); 1938 if (nvme_ctrlr == NULL) { 1939 SPDK_ERRLOG("No controller with specified name was found.\n"); 1940 spdk_jsonrpc_send_error_response(request, -ENODEV, spdk_strerror(ENODEV)); 1941 goto cleanup; 1942 } 1943 1944 if (ctx->rpc.cmd_type == NVME_IO_CMD) { 1945 spdk_for_each_channel(nvme_ctrlr, 1946 rpc_add_error_injection_per_channel, 1947 ctx, 1948 rpc_add_error_injection_done); 1949 1950 return; 1951 } else { 1952 rc = spdk_nvme_qpair_add_cmd_error_injection(nvme_ctrlr->ctrlr, NULL, ctx->rpc.opc, 1953 ctx->rpc.do_not_submit, ctx->rpc.timeout_in_us, ctx->rpc.err_count, 1954 ctx->rpc.sct, ctx->rpc.sc); 1955 if (rc) { 1956 spdk_jsonrpc_send_error_response(request, -rc, 1957 "Failed to add the error injection"); 1958 } else { 1959 spdk_jsonrpc_send_bool_response(ctx->request, true); 1960 } 1961 } 1962 1963 cleanup: 1964 free_rpc_add_error_injection(&ctx->rpc); 1965 free(ctx); 1966 } 1967 SPDK_RPC_REGISTER("bdev_nvme_add_error_injection", rpc_bdev_nvme_add_error_injection, 1968 SPDK_RPC_RUNTIME) 1969 1970 struct rpc_remove_error_injection { 1971 char *name; 1972 enum error_injection_cmd_type cmd_type; 1973 uint8_t opc; 1974 }; 1975 1976 static void 1977 free_rpc_remove_error_injection(struct rpc_remove_error_injection *req) 1978 { 1979 free(req->name); 1980 } 1981 1982 static const struct spdk_json_object_decoder rpc_remove_error_injection_decoders[] = { 1983 { "name", offsetof(struct rpc_remove_error_injection, name), spdk_json_decode_string }, 1984 { "cmd_type", offsetof(struct rpc_remove_error_injection, cmd_type), rpc_error_injection_decode_cmd_type }, 1985 { "opc", offsetof(struct rpc_remove_error_injection, opc), spdk_json_decode_uint8 }, 1986 }; 1987 1988 struct rpc_remove_error_injection_ctx { 1989 struct spdk_jsonrpc_request *request; 1990 struct rpc_remove_error_injection rpc; 1991 }; 1992 1993 static void 1994 rpc_remove_error_injection_done(struct spdk_io_channel_iter *i, int status) 1995 { 1996 struct rpc_remove_error_injection_ctx *ctx = spdk_io_channel_iter_get_ctx(i); 1997 1998 if (status) { 1999 spdk_jsonrpc_send_error_response(ctx->request, status, 2000 "Failed to remove the error injection."); 2001 } else { 2002 spdk_jsonrpc_send_bool_response(ctx->request, true); 2003 } 2004 2005 free_rpc_remove_error_injection(&ctx->rpc); 2006 free(ctx); 2007 } 2008 2009 static void 2010 rpc_remove_error_injection_per_channel(struct spdk_io_channel_iter *i) 2011 { 2012 struct spdk_io_channel *ch = spdk_io_channel_iter_get_channel(i); 2013 struct rpc_remove_error_injection_ctx *ctx = spdk_io_channel_iter_get_ctx(i); 2014 struct nvme_ctrlr_channel *ctrlr_ch = spdk_io_channel_get_ctx(ch); 2015 struct spdk_nvme_qpair *qpair = ctrlr_ch->qpair->qpair; 2016 struct spdk_nvme_ctrlr *ctrlr = ctrlr_ch->qpair->ctrlr->ctrlr; 2017 2018 if (qpair != NULL) { 2019 spdk_nvme_qpair_remove_cmd_error_injection(ctrlr, qpair, ctx->rpc.opc); 2020 } 2021 2022 spdk_for_each_channel_continue(i, 0); 2023 } 2024 2025 static void 2026 rpc_bdev_nvme_remove_error_injection(struct spdk_jsonrpc_request *request, 2027 const struct spdk_json_val *params) 2028 { 2029 struct rpc_remove_error_injection_ctx *ctx; 2030 struct nvme_ctrlr *nvme_ctrlr; 2031 2032 ctx = calloc(1, sizeof(*ctx)); 2033 if (!ctx) { 2034 spdk_jsonrpc_send_error_response(request, -ENOMEM, spdk_strerror(ENOMEM)); 2035 return; 2036 } 2037 ctx->request = request; 2038 2039 if (spdk_json_decode_object(params, 2040 rpc_remove_error_injection_decoders, 2041 SPDK_COUNTOF(rpc_remove_error_injection_decoders), 2042 &ctx->rpc)) { 2043 spdk_jsonrpc_send_error_response(request, -EINVAL, 2044 "Failed to parse the request"); 2045 goto cleanup; 2046 } 2047 2048 nvme_ctrlr = nvme_ctrlr_get_by_name(ctx->rpc.name); 2049 if (nvme_ctrlr == NULL) { 2050 SPDK_ERRLOG("No controller with specified name was found.\n"); 2051 spdk_jsonrpc_send_error_response(request, -ENODEV, spdk_strerror(ENODEV)); 2052 goto cleanup; 2053 } 2054 2055 if (ctx->rpc.cmd_type == NVME_IO_CMD) { 2056 spdk_for_each_channel(nvme_ctrlr, 2057 rpc_remove_error_injection_per_channel, 2058 ctx, 2059 rpc_remove_error_injection_done); 2060 return; 2061 } else { 2062 spdk_nvme_qpair_remove_cmd_error_injection(nvme_ctrlr->ctrlr, NULL, ctx->rpc.opc); 2063 spdk_jsonrpc_send_bool_response(ctx->request, true); 2064 } 2065 2066 cleanup: 2067 free_rpc_remove_error_injection(&ctx->rpc); 2068 free(ctx); 2069 } 2070 SPDK_RPC_REGISTER("bdev_nvme_remove_error_injection", rpc_bdev_nvme_remove_error_injection, 2071 SPDK_RPC_RUNTIME) 2072 2073 struct rpc_get_io_paths { 2074 char *name; 2075 }; 2076 2077 static void 2078 free_rpc_get_io_paths(struct rpc_get_io_paths *r) 2079 { 2080 free(r->name); 2081 } 2082 2083 static const struct spdk_json_object_decoder rpc_get_io_paths_decoders[] = { 2084 {"name", offsetof(struct rpc_get_io_paths, name), spdk_json_decode_string, true}, 2085 }; 2086 2087 struct rpc_get_io_paths_ctx { 2088 struct rpc_get_io_paths req; 2089 struct spdk_jsonrpc_request *request; 2090 struct spdk_json_write_ctx *w; 2091 }; 2092 2093 static void 2094 rpc_bdev_nvme_get_io_paths_done(struct spdk_io_channel_iter *i, int status) 2095 { 2096 struct rpc_get_io_paths_ctx *ctx = spdk_io_channel_iter_get_ctx(i); 2097 2098 spdk_json_write_array_end(ctx->w); 2099 2100 spdk_json_write_object_end(ctx->w); 2101 2102 spdk_jsonrpc_end_result(ctx->request, ctx->w); 2103 2104 free_rpc_get_io_paths(&ctx->req); 2105 free(ctx); 2106 } 2107 2108 static void 2109 _rpc_bdev_nvme_get_io_paths(struct spdk_io_channel_iter *i) 2110 { 2111 struct spdk_io_channel *_ch = spdk_io_channel_iter_get_channel(i); 2112 struct nvme_poll_group *group = spdk_io_channel_get_ctx(_ch); 2113 struct rpc_get_io_paths_ctx *ctx = spdk_io_channel_iter_get_ctx(i); 2114 struct nvme_qpair *qpair; 2115 struct nvme_io_path *io_path; 2116 struct nvme_bdev *nbdev; 2117 2118 spdk_json_write_object_begin(ctx->w); 2119 2120 spdk_json_write_named_string(ctx->w, "thread", spdk_thread_get_name(spdk_get_thread())); 2121 2122 spdk_json_write_named_array_begin(ctx->w, "io_paths"); 2123 2124 TAILQ_FOREACH(qpair, &group->qpair_list, tailq) { 2125 TAILQ_FOREACH(io_path, &qpair->io_path_list, tailq) { 2126 nbdev = io_path->nvme_ns->bdev; 2127 2128 if (ctx->req.name != NULL && 2129 strcmp(ctx->req.name, nbdev->disk.name) != 0) { 2130 continue; 2131 } 2132 2133 nvme_io_path_info_json(ctx->w, io_path); 2134 } 2135 } 2136 2137 spdk_json_write_array_end(ctx->w); 2138 2139 spdk_json_write_object_end(ctx->w); 2140 2141 spdk_for_each_channel_continue(i, 0); 2142 } 2143 2144 static void 2145 rpc_bdev_nvme_get_io_paths(struct spdk_jsonrpc_request *request, 2146 const struct spdk_json_val *params) 2147 { 2148 struct rpc_get_io_paths_ctx *ctx; 2149 2150 ctx = calloc(1, sizeof(*ctx)); 2151 if (ctx == NULL) { 2152 spdk_jsonrpc_send_error_response(request, -ENOMEM, spdk_strerror(ENOMEM)); 2153 return; 2154 } 2155 2156 if (params != NULL && 2157 spdk_json_decode_object(params, rpc_get_io_paths_decoders, 2158 SPDK_COUNTOF(rpc_get_io_paths_decoders), 2159 &ctx->req)) { 2160 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, 2161 "bdev_nvme_get_io_paths requires no parameters"); 2162 2163 free_rpc_get_io_paths(&ctx->req); 2164 free(ctx); 2165 return; 2166 } 2167 2168 ctx->request = request; 2169 ctx->w = spdk_jsonrpc_begin_result(request); 2170 2171 spdk_json_write_object_begin(ctx->w); 2172 2173 spdk_json_write_named_array_begin(ctx->w, "poll_groups"); 2174 2175 spdk_for_each_channel(&g_nvme_bdev_ctrlrs, 2176 _rpc_bdev_nvme_get_io_paths, 2177 ctx, 2178 rpc_bdev_nvme_get_io_paths_done); 2179 } 2180 SPDK_RPC_REGISTER("bdev_nvme_get_io_paths", rpc_bdev_nvme_get_io_paths, SPDK_RPC_RUNTIME) 2181 2182 struct rpc_bdev_nvme_set_preferred_path { 2183 char *name; 2184 uint16_t cntlid; 2185 }; 2186 2187 static void 2188 free_rpc_bdev_nvme_set_preferred_path(struct rpc_bdev_nvme_set_preferred_path *req) 2189 { 2190 free(req->name); 2191 } 2192 2193 static const struct spdk_json_object_decoder rpc_bdev_nvme_set_preferred_path_decoders[] = { 2194 {"name", offsetof(struct rpc_bdev_nvme_set_preferred_path, name), spdk_json_decode_string}, 2195 {"cntlid", offsetof(struct rpc_bdev_nvme_set_preferred_path, cntlid), spdk_json_decode_uint16}, 2196 }; 2197 2198 struct rpc_bdev_nvme_set_preferred_path_ctx { 2199 struct rpc_bdev_nvme_set_preferred_path req; 2200 struct spdk_jsonrpc_request *request; 2201 }; 2202 2203 static void 2204 rpc_bdev_nvme_set_preferred_path_done(void *cb_arg, int rc) 2205 { 2206 struct rpc_bdev_nvme_set_preferred_path_ctx *ctx = cb_arg; 2207 2208 if (rc == 0) { 2209 spdk_jsonrpc_send_bool_response(ctx->request, true); 2210 } else { 2211 spdk_jsonrpc_send_error_response(ctx->request, rc, spdk_strerror(-rc)); 2212 } 2213 2214 free_rpc_bdev_nvme_set_preferred_path(&ctx->req); 2215 free(ctx); 2216 } 2217 2218 static void 2219 rpc_bdev_nvme_set_preferred_path(struct spdk_jsonrpc_request *request, 2220 const struct spdk_json_val *params) 2221 { 2222 struct rpc_bdev_nvme_set_preferred_path_ctx *ctx; 2223 2224 ctx = calloc(1, sizeof(*ctx)); 2225 if (ctx == NULL) { 2226 spdk_jsonrpc_send_error_response(request, -ENOMEM, spdk_strerror(ENOMEM)); 2227 return; 2228 } 2229 2230 if (spdk_json_decode_object(params, rpc_bdev_nvme_set_preferred_path_decoders, 2231 SPDK_COUNTOF(rpc_bdev_nvme_set_preferred_path_decoders), 2232 &ctx->req)) { 2233 SPDK_ERRLOG("spdk_json_decode_object failed\n"); 2234 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 2235 "spdk_json_decode_object failed"); 2236 goto cleanup; 2237 } 2238 2239 ctx->request = request; 2240 2241 bdev_nvme_set_preferred_path(ctx->req.name, ctx->req.cntlid, 2242 rpc_bdev_nvme_set_preferred_path_done, ctx); 2243 return; 2244 2245 cleanup: 2246 free_rpc_bdev_nvme_set_preferred_path(&ctx->req); 2247 free(ctx); 2248 } 2249 SPDK_RPC_REGISTER("bdev_nvme_set_preferred_path", rpc_bdev_nvme_set_preferred_path, 2250 SPDK_RPC_RUNTIME) 2251 2252 struct rpc_set_multipath_policy { 2253 char *name; 2254 enum bdev_nvme_multipath_policy policy; 2255 enum bdev_nvme_multipath_selector selector; 2256 uint32_t rr_min_io; 2257 }; 2258 2259 static void 2260 free_rpc_set_multipath_policy(struct rpc_set_multipath_policy *req) 2261 { 2262 free(req->name); 2263 } 2264 2265 static int 2266 rpc_decode_mp_policy(const struct spdk_json_val *val, void *out) 2267 { 2268 enum bdev_nvme_multipath_policy *policy = out; 2269 2270 if (spdk_json_strequal(val, "active_passive") == true) { 2271 *policy = BDEV_NVME_MP_POLICY_ACTIVE_PASSIVE; 2272 } else if (spdk_json_strequal(val, "active_active") == true) { 2273 *policy = BDEV_NVME_MP_POLICY_ACTIVE_ACTIVE; 2274 } else { 2275 SPDK_NOTICELOG("Invalid parameter value: policy\n"); 2276 return -EINVAL; 2277 } 2278 2279 return 0; 2280 } 2281 2282 static int 2283 rpc_decode_mp_selector(const struct spdk_json_val *val, void *out) 2284 { 2285 enum bdev_nvme_multipath_selector *selector = out; 2286 2287 if (spdk_json_strequal(val, "round_robin") == true) { 2288 *selector = BDEV_NVME_MP_SELECTOR_ROUND_ROBIN; 2289 } else if (spdk_json_strequal(val, "queue_depth") == true) { 2290 *selector = BDEV_NVME_MP_SELECTOR_QUEUE_DEPTH; 2291 } else { 2292 SPDK_NOTICELOG("Invalid parameter value: selector\n"); 2293 return -EINVAL; 2294 } 2295 2296 return 0; 2297 } 2298 2299 static const struct spdk_json_object_decoder rpc_set_multipath_policy_decoders[] = { 2300 {"name", offsetof(struct rpc_set_multipath_policy, name), spdk_json_decode_string}, 2301 {"policy", offsetof(struct rpc_set_multipath_policy, policy), rpc_decode_mp_policy}, 2302 {"selector", offsetof(struct rpc_set_multipath_policy, selector), rpc_decode_mp_selector, true}, 2303 {"rr_min_io", offsetof(struct rpc_set_multipath_policy, rr_min_io), spdk_json_decode_uint32, true}, 2304 }; 2305 2306 struct rpc_set_multipath_policy_ctx { 2307 struct rpc_set_multipath_policy req; 2308 struct spdk_jsonrpc_request *request; 2309 }; 2310 2311 static void 2312 rpc_bdev_nvme_set_multipath_policy_done(void *cb_arg, int rc) 2313 { 2314 struct rpc_set_multipath_policy_ctx *ctx = cb_arg; 2315 2316 if (rc == 0) { 2317 spdk_jsonrpc_send_bool_response(ctx->request, true); 2318 } else { 2319 spdk_jsonrpc_send_error_response(ctx->request, rc, spdk_strerror(-rc)); 2320 } 2321 2322 free_rpc_set_multipath_policy(&ctx->req); 2323 free(ctx); 2324 } 2325 2326 static void 2327 rpc_bdev_nvme_set_multipath_policy(struct spdk_jsonrpc_request *request, 2328 const struct spdk_json_val *params) 2329 { 2330 struct rpc_set_multipath_policy_ctx *ctx; 2331 2332 ctx = calloc(1, sizeof(*ctx)); 2333 if (ctx == NULL) { 2334 spdk_jsonrpc_send_error_response(request, -ENOMEM, spdk_strerror(ENOMEM)); 2335 return; 2336 } 2337 2338 ctx->req.rr_min_io = UINT32_MAX; 2339 2340 if (spdk_json_decode_object(params, rpc_set_multipath_policy_decoders, 2341 SPDK_COUNTOF(rpc_set_multipath_policy_decoders), 2342 &ctx->req)) { 2343 SPDK_ERRLOG("spdk_json_decode_object failed\n"); 2344 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 2345 "spdk_json_decode_object failed"); 2346 goto cleanup; 2347 } 2348 2349 ctx->request = request; 2350 2351 if (ctx->req.policy != BDEV_NVME_MP_POLICY_ACTIVE_ACTIVE && ctx->req.selector > 0) { 2352 SPDK_ERRLOG("selector only works in active_active mode\n"); 2353 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 2354 "spdk_json_decode_object failed"); 2355 goto cleanup; 2356 } 2357 2358 bdev_nvme_set_multipath_policy(ctx->req.name, ctx->req.policy, ctx->req.selector, 2359 ctx->req.rr_min_io, 2360 rpc_bdev_nvme_set_multipath_policy_done, ctx); 2361 return; 2362 2363 cleanup: 2364 free_rpc_set_multipath_policy(&ctx->req); 2365 free(ctx); 2366 } 2367 SPDK_RPC_REGISTER("bdev_nvme_set_multipath_policy", rpc_bdev_nvme_set_multipath_policy, 2368 SPDK_RPC_RUNTIME) 2369 2370 struct rpc_bdev_nvme_start_mdns_discovery { 2371 char *name; 2372 char *svcname; 2373 char *hostnqn; 2374 struct spdk_nvme_ctrlr_opts opts; 2375 struct nvme_ctrlr_opts bdev_opts; 2376 }; 2377 2378 static void 2379 free_rpc_bdev_nvme_start_mdns_discovery(struct rpc_bdev_nvme_start_mdns_discovery *req) 2380 { 2381 free(req->name); 2382 free(req->svcname); 2383 free(req->hostnqn); 2384 } 2385 2386 static const struct spdk_json_object_decoder rpc_bdev_nvme_start_mdns_discovery_decoders[] = { 2387 {"name", offsetof(struct rpc_bdev_nvme_start_mdns_discovery, name), spdk_json_decode_string}, 2388 {"svcname", offsetof(struct rpc_bdev_nvme_start_mdns_discovery, svcname), spdk_json_decode_string}, 2389 {"hostnqn", offsetof(struct rpc_bdev_nvme_start_mdns_discovery, hostnqn), spdk_json_decode_string, true}, 2390 }; 2391 2392 struct rpc_bdev_nvme_start_mdns_discovery_ctx { 2393 struct rpc_bdev_nvme_start_mdns_discovery req; 2394 struct spdk_jsonrpc_request *request; 2395 }; 2396 2397 static void 2398 rpc_bdev_nvme_start_mdns_discovery(struct spdk_jsonrpc_request *request, 2399 const struct spdk_json_val *params) 2400 { 2401 struct rpc_bdev_nvme_start_mdns_discovery_ctx *ctx; 2402 int rc; 2403 2404 ctx = calloc(1, sizeof(*ctx)); 2405 if (!ctx) { 2406 spdk_jsonrpc_send_error_response(request, -ENOMEM, spdk_strerror(ENOMEM)); 2407 return; 2408 } 2409 2410 spdk_nvme_ctrlr_get_default_ctrlr_opts(&ctx->req.opts, sizeof(ctx->req.opts)); 2411 2412 if (spdk_json_decode_object(params, rpc_bdev_nvme_start_mdns_discovery_decoders, 2413 SPDK_COUNTOF(rpc_bdev_nvme_start_mdns_discovery_decoders), 2414 &ctx->req)) { 2415 SPDK_ERRLOG("spdk_json_decode_object failed\n"); 2416 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 2417 "spdk_json_decode_object failed"); 2418 goto cleanup; 2419 } 2420 2421 if (ctx->req.hostnqn) { 2422 snprintf(ctx->req.opts.hostnqn, sizeof(ctx->req.opts.hostnqn), "%s", 2423 ctx->req.hostnqn); 2424 } 2425 ctx->request = request; 2426 rc = bdev_nvme_start_mdns_discovery(ctx->req.name, ctx->req.svcname, &ctx->req.opts, 2427 &ctx->req.bdev_opts); 2428 if (rc) { 2429 spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc)); 2430 } else { 2431 spdk_jsonrpc_send_bool_response(request, true); 2432 } 2433 2434 cleanup: 2435 free_rpc_bdev_nvme_start_mdns_discovery(&ctx->req); 2436 free(ctx); 2437 } 2438 SPDK_RPC_REGISTER("bdev_nvme_start_mdns_discovery", rpc_bdev_nvme_start_mdns_discovery, 2439 SPDK_RPC_RUNTIME) 2440 2441 struct rpc_bdev_nvme_stop_mdns_discovery { 2442 char *name; 2443 }; 2444 2445 static const struct spdk_json_object_decoder rpc_bdev_nvme_stop_mdns_discovery_decoders[] = { 2446 {"name", offsetof(struct rpc_bdev_nvme_stop_mdns_discovery, name), spdk_json_decode_string}, 2447 }; 2448 2449 struct rpc_bdev_nvme_stop_mdns_discovery_ctx { 2450 struct rpc_bdev_nvme_stop_mdns_discovery req; 2451 struct spdk_jsonrpc_request *request; 2452 }; 2453 2454 static void 2455 rpc_bdev_nvme_stop_mdns_discovery(struct spdk_jsonrpc_request *request, 2456 const struct spdk_json_val *params) 2457 { 2458 struct rpc_bdev_nvme_stop_mdns_discovery_ctx *ctx; 2459 int rc; 2460 2461 ctx = calloc(1, sizeof(*ctx)); 2462 if (!ctx) { 2463 spdk_jsonrpc_send_error_response(request, -ENOMEM, spdk_strerror(ENOMEM)); 2464 return; 2465 } 2466 2467 if (spdk_json_decode_object(params, rpc_bdev_nvme_stop_mdns_discovery_decoders, 2468 SPDK_COUNTOF(rpc_bdev_nvme_stop_mdns_discovery_decoders), 2469 &ctx->req)) { 2470 SPDK_ERRLOG("spdk_json_decode_object failed\n"); 2471 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 2472 "spdk_json_decode_object failed"); 2473 goto cleanup; 2474 } 2475 2476 ctx->request = request; 2477 rc = bdev_nvme_stop_mdns_discovery(ctx->req.name); 2478 2479 if (rc) { 2480 spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc)); 2481 goto cleanup; 2482 } 2483 spdk_jsonrpc_send_bool_response(ctx->request, true); 2484 2485 cleanup: 2486 free(ctx->req.name); 2487 free(ctx); 2488 } 2489 SPDK_RPC_REGISTER("bdev_nvme_stop_mdns_discovery", rpc_bdev_nvme_stop_mdns_discovery, 2490 SPDK_RPC_RUNTIME) 2491 2492 static void 2493 rpc_bdev_nvme_get_mdns_discovery_info(struct spdk_jsonrpc_request *request, 2494 const struct spdk_json_val *params) 2495 { 2496 bdev_nvme_get_mdns_discovery_info(request); 2497 } 2498 2499 SPDK_RPC_REGISTER("bdev_nvme_get_mdns_discovery_info", rpc_bdev_nvme_get_mdns_discovery_info, 2500 SPDK_RPC_RUNTIME) 2501 2502 struct rpc_get_path_stat { 2503 char *name; 2504 }; 2505 2506 struct path_stat { 2507 struct spdk_bdev_io_stat stat; 2508 struct spdk_nvme_transport_id trid; 2509 struct nvme_ns *ns; 2510 }; 2511 2512 struct rpc_bdev_nvme_path_stat_ctx { 2513 struct spdk_jsonrpc_request *request; 2514 struct path_stat *path_stat; 2515 uint32_t num_paths; 2516 struct spdk_bdev_desc *desc; 2517 }; 2518 2519 static void 2520 free_rpc_get_path_stat(struct rpc_get_path_stat *req) 2521 { 2522 free(req->name); 2523 } 2524 2525 static const struct spdk_json_object_decoder rpc_get_path_stat_decoders[] = { 2526 {"name", offsetof(struct rpc_get_path_stat, name), spdk_json_decode_string}, 2527 }; 2528 2529 static void 2530 dummy_bdev_event_cb(enum spdk_bdev_event_type type, struct spdk_bdev *bdev, void *ctx) 2531 { 2532 } 2533 2534 static void 2535 rpc_bdev_nvme_path_stat_per_channel(struct spdk_io_channel_iter *i) 2536 { 2537 struct rpc_bdev_nvme_path_stat_ctx *ctx = spdk_io_channel_iter_get_ctx(i); 2538 struct spdk_io_channel *ch = spdk_io_channel_iter_get_channel(i); 2539 struct nvme_bdev_channel *nbdev_ch = spdk_io_channel_get_ctx(ch); 2540 struct nvme_io_path *io_path; 2541 struct path_stat *path_stat; 2542 uint32_t j; 2543 2544 assert(ctx->num_paths != 0); 2545 2546 for (j = 0; j < ctx->num_paths; j++) { 2547 path_stat = &ctx->path_stat[j]; 2548 2549 STAILQ_FOREACH(io_path, &nbdev_ch->io_path_list, stailq) { 2550 if (path_stat->ns == io_path->nvme_ns) { 2551 assert(io_path->stat != NULL); 2552 spdk_bdev_add_io_stat(&path_stat->stat, io_path->stat); 2553 } 2554 } 2555 } 2556 2557 spdk_for_each_channel_continue(i, 0); 2558 } 2559 2560 static void 2561 rpc_bdev_nvme_path_stat_done(struct spdk_io_channel_iter *i, int status) 2562 { 2563 struct rpc_bdev_nvme_path_stat_ctx *ctx = spdk_io_channel_iter_get_ctx(i); 2564 struct nvme_bdev *nbdev = spdk_io_channel_iter_get_io_device(i); 2565 struct spdk_json_write_ctx *w; 2566 struct path_stat *path_stat; 2567 uint32_t j; 2568 2569 assert(ctx->num_paths != 0); 2570 2571 w = spdk_jsonrpc_begin_result(ctx->request); 2572 spdk_json_write_object_begin(w); 2573 spdk_json_write_named_string(w, "name", nbdev->disk.name); 2574 spdk_json_write_named_array_begin(w, "stats"); 2575 2576 for (j = 0; j < ctx->num_paths; j++) { 2577 path_stat = &ctx->path_stat[j]; 2578 spdk_json_write_object_begin(w); 2579 2580 spdk_json_write_named_object_begin(w, "trid"); 2581 nvme_bdev_dump_trid_json(&path_stat->trid, w); 2582 spdk_json_write_object_end(w); 2583 2584 spdk_json_write_named_object_begin(w, "stat"); 2585 spdk_bdev_dump_io_stat_json(&path_stat->stat, w); 2586 spdk_json_write_object_end(w); 2587 2588 spdk_json_write_object_end(w); 2589 } 2590 2591 spdk_json_write_array_end(w); 2592 spdk_json_write_object_end(w); 2593 spdk_jsonrpc_end_result(ctx->request, w); 2594 2595 spdk_bdev_close(ctx->desc); 2596 free(ctx->path_stat); 2597 free(ctx); 2598 } 2599 2600 static void 2601 rpc_bdev_nvme_get_path_iostat(struct spdk_jsonrpc_request *request, 2602 const struct spdk_json_val *params) 2603 { 2604 struct rpc_get_path_stat req = {}; 2605 struct spdk_bdev_desc *desc = NULL; 2606 struct spdk_bdev *bdev; 2607 struct nvme_bdev *nbdev; 2608 struct nvme_ns *nvme_ns; 2609 struct path_stat *path_stat; 2610 struct rpc_bdev_nvme_path_stat_ctx *ctx; 2611 struct spdk_bdev_nvme_opts opts; 2612 uint32_t num_paths = 0, i = 0; 2613 int rc; 2614 2615 bdev_nvme_get_opts(&opts); 2616 if (!opts.io_path_stat) { 2617 SPDK_ERRLOG("RPC not enabled if enable_io_path_stat is false\n"); 2618 spdk_jsonrpc_send_error_response(request, -EPERM, 2619 "RPC not enabled if enable_io_path_stat is false"); 2620 return; 2621 } 2622 2623 if (spdk_json_decode_object(params, rpc_get_path_stat_decoders, 2624 SPDK_COUNTOF(rpc_get_path_stat_decoders), 2625 &req)) { 2626 SPDK_ERRLOG("spdk_json_decode_object failed\n"); 2627 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 2628 "spdk_json_decode_object failed"); 2629 free_rpc_get_path_stat(&req); 2630 return; 2631 } 2632 2633 rc = spdk_bdev_open_ext(req.name, false, dummy_bdev_event_cb, NULL, &desc); 2634 if (rc != 0) { 2635 SPDK_ERRLOG("Failed to open bdev '%s': %d\n", req.name, rc); 2636 spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc)); 2637 free_rpc_get_path_stat(&req); 2638 return; 2639 } 2640 2641 free_rpc_get_path_stat(&req); 2642 2643 ctx = calloc(1, sizeof(struct rpc_bdev_nvme_path_stat_ctx)); 2644 if (ctx == NULL) { 2645 spdk_bdev_close(desc); 2646 SPDK_ERRLOG("Failed to allocate rpc_bdev_nvme_path_stat_ctx struct\n"); 2647 spdk_jsonrpc_send_error_response(request, -ENOMEM, spdk_strerror(ENOMEM)); 2648 return; 2649 } 2650 2651 bdev = spdk_bdev_desc_get_bdev(desc); 2652 nbdev = bdev->ctxt; 2653 2654 pthread_mutex_lock(&nbdev->mutex); 2655 if (nbdev->ref == 0) { 2656 rc = -ENOENT; 2657 goto err; 2658 } 2659 2660 num_paths = nbdev->ref; 2661 path_stat = calloc(num_paths, sizeof(struct path_stat)); 2662 if (path_stat == NULL) { 2663 rc = -ENOMEM; 2664 SPDK_ERRLOG("Failed to allocate memory for path_stat.\n"); 2665 goto err; 2666 } 2667 2668 /* store the history stat */ 2669 TAILQ_FOREACH(nvme_ns, &nbdev->nvme_ns_list, tailq) { 2670 assert(i < num_paths); 2671 path_stat[i].ns = nvme_ns; 2672 path_stat[i].trid = nvme_ns->ctrlr->active_path_id->trid; 2673 2674 assert(nvme_ns->stat != NULL); 2675 memcpy(&path_stat[i].stat, nvme_ns->stat, sizeof(struct spdk_bdev_io_stat)); 2676 i++; 2677 } 2678 pthread_mutex_unlock(&nbdev->mutex); 2679 2680 ctx->request = request; 2681 ctx->desc = desc; 2682 ctx->path_stat = path_stat; 2683 ctx->num_paths = num_paths; 2684 2685 spdk_for_each_channel(nbdev, 2686 rpc_bdev_nvme_path_stat_per_channel, 2687 ctx, 2688 rpc_bdev_nvme_path_stat_done); 2689 return; 2690 2691 err: 2692 pthread_mutex_unlock(&nbdev->mutex); 2693 spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc)); 2694 spdk_bdev_close(desc); 2695 free(ctx); 2696 } 2697 SPDK_RPC_REGISTER("bdev_nvme_get_path_iostat", rpc_bdev_nvme_get_path_iostat, 2698 SPDK_RPC_RUNTIME) 2699