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