1 /*- 2 * BSD LICENSE 3 * 4 * Copyright (c) Intel Corporation. All rights reserved. 5 * Copyright (c) 2019-2021 Mellanox Technologies LTD. All rights reserved. 6 * Copyright (c) 2022 NVIDIA CORPORATION & AFFILIATES. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 12 * * Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * * Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in 16 * the documentation and/or other materials provided with the 17 * distribution. 18 * * Neither the name of Intel Corporation nor the names of its 19 * contributors may be used to endorse or promote products derived 20 * from this software without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 26 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 */ 34 35 #include "spdk/stdinc.h" 36 37 #include "bdev_nvme.h" 38 39 #include "spdk/config.h" 40 41 #include "spdk/string.h" 42 #include "spdk/rpc.h" 43 #include "spdk/util.h" 44 #include "spdk/env.h" 45 #include "spdk/nvme.h" 46 #include "spdk/nvme_spec.h" 47 48 #include "spdk/log.h" 49 #include "spdk/bdev_module.h" 50 51 struct open_descriptors { 52 void *desc; 53 struct spdk_bdev *bdev; 54 TAILQ_ENTRY(open_descriptors) tqlst; 55 struct spdk_thread *thread; 56 }; 57 typedef TAILQ_HEAD(, open_descriptors) open_descriptors_t; 58 59 static int 60 rpc_decode_action_on_timeout(const struct spdk_json_val *val, void *out) 61 { 62 enum spdk_bdev_timeout_action *action = out; 63 64 if (spdk_json_strequal(val, "none") == true) { 65 *action = SPDK_BDEV_NVME_TIMEOUT_ACTION_NONE; 66 } else if (spdk_json_strequal(val, "abort") == true) { 67 *action = SPDK_BDEV_NVME_TIMEOUT_ACTION_ABORT; 68 } else if (spdk_json_strequal(val, "reset") == true) { 69 *action = SPDK_BDEV_NVME_TIMEOUT_ACTION_RESET; 70 } else { 71 SPDK_NOTICELOG("Invalid parameter value: action_on_timeout\n"); 72 return -EINVAL; 73 } 74 75 return 0; 76 } 77 78 static const struct spdk_json_object_decoder rpc_bdev_nvme_options_decoders[] = { 79 {"action_on_timeout", offsetof(struct spdk_bdev_nvme_opts, action_on_timeout), rpc_decode_action_on_timeout, true}, 80 {"timeout_us", offsetof(struct spdk_bdev_nvme_opts, timeout_us), spdk_json_decode_uint64, true}, 81 {"timeout_admin_us", offsetof(struct spdk_bdev_nvme_opts, timeout_admin_us), spdk_json_decode_uint64, true}, 82 {"keep_alive_timeout_ms", offsetof(struct spdk_bdev_nvme_opts, keep_alive_timeout_ms), spdk_json_decode_uint32, true}, 83 {"retry_count", offsetof(struct spdk_bdev_nvme_opts, transport_retry_count), spdk_json_decode_uint32, true}, 84 {"arbitration_burst", offsetof(struct spdk_bdev_nvme_opts, arbitration_burst), spdk_json_decode_uint32, true}, 85 {"low_priority_weight", offsetof(struct spdk_bdev_nvme_opts, low_priority_weight), spdk_json_decode_uint32, true}, 86 {"medium_priority_weight", offsetof(struct spdk_bdev_nvme_opts, medium_priority_weight), spdk_json_decode_uint32, true}, 87 {"high_priority_weight", offsetof(struct spdk_bdev_nvme_opts, high_priority_weight), spdk_json_decode_uint32, true}, 88 {"nvme_adminq_poll_period_us", offsetof(struct spdk_bdev_nvme_opts, nvme_adminq_poll_period_us), spdk_json_decode_uint64, true}, 89 {"nvme_ioq_poll_period_us", offsetof(struct spdk_bdev_nvme_opts, nvme_ioq_poll_period_us), spdk_json_decode_uint64, true}, 90 {"io_queue_requests", offsetof(struct spdk_bdev_nvme_opts, io_queue_requests), spdk_json_decode_uint32, true}, 91 {"delay_cmd_submit", offsetof(struct spdk_bdev_nvme_opts, delay_cmd_submit), spdk_json_decode_bool, true}, 92 {"transport_retry_count", offsetof(struct spdk_bdev_nvme_opts, transport_retry_count), spdk_json_decode_uint32, true}, 93 {"bdev_retry_count", offsetof(struct spdk_bdev_nvme_opts, bdev_retry_count), spdk_json_decode_int32, true}, 94 {"transport_ack_timeout", offsetof(struct spdk_bdev_nvme_opts, transport_ack_timeout), spdk_json_decode_uint8, true}, 95 }; 96 97 static void 98 rpc_bdev_nvme_set_options(struct spdk_jsonrpc_request *request, 99 const struct spdk_json_val *params) 100 { 101 struct spdk_bdev_nvme_opts opts; 102 int rc; 103 104 bdev_nvme_get_opts(&opts); 105 if (params && spdk_json_decode_object(params, rpc_bdev_nvme_options_decoders, 106 SPDK_COUNTOF(rpc_bdev_nvme_options_decoders), 107 &opts)) { 108 SPDK_ERRLOG("spdk_json_decode_object failed\n"); 109 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 110 "spdk_json_decode_object failed"); 111 return; 112 } 113 114 rc = bdev_nvme_set_opts(&opts); 115 if (rc) { 116 spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc)); 117 return; 118 } 119 120 spdk_jsonrpc_send_bool_response(request, true); 121 122 return; 123 } 124 SPDK_RPC_REGISTER("bdev_nvme_set_options", rpc_bdev_nvme_set_options, 125 SPDK_RPC_STARTUP | SPDK_RPC_RUNTIME) 126 SPDK_RPC_REGISTER_ALIAS_DEPRECATED(bdev_nvme_set_options, set_bdev_nvme_options) 127 128 struct rpc_bdev_nvme_hotplug { 129 bool enabled; 130 uint64_t period_us; 131 }; 132 133 static const struct spdk_json_object_decoder rpc_bdev_nvme_hotplug_decoders[] = { 134 {"enable", offsetof(struct rpc_bdev_nvme_hotplug, enabled), spdk_json_decode_bool, false}, 135 {"period_us", offsetof(struct rpc_bdev_nvme_hotplug, period_us), spdk_json_decode_uint64, true}, 136 }; 137 138 static void 139 rpc_bdev_nvme_set_hotplug_done(void *ctx) 140 { 141 struct spdk_jsonrpc_request *request = ctx; 142 143 spdk_jsonrpc_send_bool_response(request, true); 144 } 145 146 static void 147 rpc_bdev_nvme_set_hotplug(struct spdk_jsonrpc_request *request, 148 const struct spdk_json_val *params) 149 { 150 struct rpc_bdev_nvme_hotplug req = {false, 0}; 151 int rc; 152 153 if (spdk_json_decode_object(params, rpc_bdev_nvme_hotplug_decoders, 154 SPDK_COUNTOF(rpc_bdev_nvme_hotplug_decoders), &req)) { 155 SPDK_ERRLOG("spdk_json_decode_object failed\n"); 156 rc = -EINVAL; 157 goto invalid; 158 } 159 160 rc = bdev_nvme_set_hotplug(req.enabled, req.period_us, rpc_bdev_nvme_set_hotplug_done, 161 request); 162 if (rc) { 163 goto invalid; 164 } 165 166 return; 167 invalid: 168 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, spdk_strerror(-rc)); 169 } 170 SPDK_RPC_REGISTER("bdev_nvme_set_hotplug", rpc_bdev_nvme_set_hotplug, SPDK_RPC_RUNTIME) 171 SPDK_RPC_REGISTER_ALIAS_DEPRECATED(bdev_nvme_set_hotplug, set_bdev_nvme_hotplug) 172 173 struct rpc_bdev_nvme_attach_controller { 174 char *name; 175 char *trtype; 176 char *adrfam; 177 char *traddr; 178 char *trsvcid; 179 char *priority; 180 char *subnqn; 181 char *hostnqn; 182 char *hostaddr; 183 char *hostsvcid; 184 bool prchk_reftag; 185 bool prchk_guard; 186 uint64_t fabrics_connect_timeout_us; 187 char *multipath; 188 int32_t ctrlr_loss_timeout_sec; 189 uint32_t reconnect_delay_sec; 190 uint32_t fast_io_fail_timeout_sec; 191 struct spdk_nvme_ctrlr_opts opts; 192 }; 193 194 static void 195 free_rpc_bdev_nvme_attach_controller(struct rpc_bdev_nvme_attach_controller *req) 196 { 197 free(req->name); 198 free(req->trtype); 199 free(req->adrfam); 200 free(req->traddr); 201 free(req->trsvcid); 202 free(req->priority); 203 free(req->subnqn); 204 free(req->hostnqn); 205 free(req->hostaddr); 206 free(req->hostsvcid); 207 free(req->multipath); 208 } 209 210 static const struct spdk_json_object_decoder rpc_bdev_nvme_attach_controller_decoders[] = { 211 {"name", offsetof(struct rpc_bdev_nvme_attach_controller, name), spdk_json_decode_string}, 212 {"trtype", offsetof(struct rpc_bdev_nvme_attach_controller, trtype), spdk_json_decode_string}, 213 {"traddr", offsetof(struct rpc_bdev_nvme_attach_controller, traddr), spdk_json_decode_string}, 214 215 {"adrfam", offsetof(struct rpc_bdev_nvme_attach_controller, adrfam), spdk_json_decode_string, true}, 216 {"trsvcid", offsetof(struct rpc_bdev_nvme_attach_controller, trsvcid), spdk_json_decode_string, true}, 217 {"priority", offsetof(struct rpc_bdev_nvme_attach_controller, priority), spdk_json_decode_string, true}, 218 {"subnqn", offsetof(struct rpc_bdev_nvme_attach_controller, subnqn), spdk_json_decode_string, true}, 219 {"hostnqn", offsetof(struct rpc_bdev_nvme_attach_controller, hostnqn), spdk_json_decode_string, true}, 220 {"hostaddr", offsetof(struct rpc_bdev_nvme_attach_controller, hostaddr), spdk_json_decode_string, true}, 221 {"hostsvcid", offsetof(struct rpc_bdev_nvme_attach_controller, hostsvcid), spdk_json_decode_string, true}, 222 223 {"prchk_reftag", offsetof(struct rpc_bdev_nvme_attach_controller, prchk_reftag), spdk_json_decode_bool, true}, 224 {"prchk_guard", offsetof(struct rpc_bdev_nvme_attach_controller, prchk_guard), spdk_json_decode_bool, true}, 225 {"hdgst", offsetof(struct rpc_bdev_nvme_attach_controller, opts.header_digest), spdk_json_decode_bool, true}, 226 {"ddgst", offsetof(struct rpc_bdev_nvme_attach_controller, opts.data_digest), spdk_json_decode_bool, true}, 227 {"fabrics_connect_timeout_us", offsetof(struct rpc_bdev_nvme_attach_controller, opts.fabrics_connect_timeout_us), spdk_json_decode_uint64, true}, 228 {"multipath", offsetof(struct rpc_bdev_nvme_attach_controller, multipath), spdk_json_decode_string, true}, 229 {"num_io_queues", offsetof(struct rpc_bdev_nvme_attach_controller, opts.num_io_queues), spdk_json_decode_uint32, true}, 230 {"ctrlr_loss_timeout_sec", offsetof(struct rpc_bdev_nvme_attach_controller, ctrlr_loss_timeout_sec), spdk_json_decode_int32, true}, 231 {"reconnect_delay_sec", offsetof(struct rpc_bdev_nvme_attach_controller, reconnect_delay_sec), spdk_json_decode_uint32, true}, 232 {"fast_io_fail_timeout_sec", offsetof(struct rpc_bdev_nvme_attach_controller, fast_io_fail_timeout_sec), spdk_json_decode_uint32, true}, 233 }; 234 235 #define NVME_MAX_BDEVS_PER_RPC 128 236 237 struct rpc_bdev_nvme_attach_controller_ctx { 238 struct rpc_bdev_nvme_attach_controller req; 239 uint32_t count; 240 size_t bdev_count; 241 const char *names[NVME_MAX_BDEVS_PER_RPC]; 242 struct spdk_jsonrpc_request *request; 243 }; 244 245 static void 246 rpc_bdev_nvme_attach_controller_examined(void *cb_ctx) 247 { 248 struct rpc_bdev_nvme_attach_controller_ctx *ctx = cb_ctx; 249 struct spdk_jsonrpc_request *request = ctx->request; 250 struct spdk_json_write_ctx *w; 251 size_t i; 252 253 w = spdk_jsonrpc_begin_result(request); 254 spdk_json_write_array_begin(w); 255 for (i = 0; i < ctx->bdev_count; i++) { 256 spdk_json_write_string(w, ctx->names[i]); 257 } 258 spdk_json_write_array_end(w); 259 spdk_jsonrpc_end_result(request, w); 260 261 free_rpc_bdev_nvme_attach_controller(&ctx->req); 262 free(ctx); 263 } 264 265 static void 266 rpc_bdev_nvme_attach_controller_done(void *cb_ctx, size_t bdev_count, int rc) 267 { 268 struct rpc_bdev_nvme_attach_controller_ctx *ctx = cb_ctx; 269 struct spdk_jsonrpc_request *request = ctx->request; 270 271 if (rc < 0) { 272 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters"); 273 free_rpc_bdev_nvme_attach_controller(&ctx->req); 274 free(ctx); 275 return; 276 } 277 278 ctx->bdev_count = bdev_count; 279 spdk_bdev_wait_for_examine(rpc_bdev_nvme_attach_controller_examined, ctx); 280 } 281 282 static void 283 rpc_bdev_nvme_attach_controller(struct spdk_jsonrpc_request *request, 284 const struct spdk_json_val *params) 285 { 286 struct rpc_bdev_nvme_attach_controller_ctx *ctx; 287 struct spdk_nvme_transport_id trid = {}; 288 const struct spdk_nvme_ctrlr_opts *opts; 289 const struct spdk_nvme_transport_id *ctrlr_trid; 290 uint32_t prchk_flags = 0; 291 struct nvme_ctrlr *ctrlr = NULL; 292 size_t len, maxlen; 293 bool multipath = false; 294 int rc; 295 296 ctx = calloc(1, sizeof(*ctx)); 297 if (!ctx) { 298 spdk_jsonrpc_send_error_response(request, -ENOMEM, spdk_strerror(ENOMEM)); 299 return; 300 } 301 302 spdk_nvme_ctrlr_get_default_ctrlr_opts(&ctx->req.opts, sizeof(ctx->req.opts)); 303 304 if (spdk_json_decode_object(params, rpc_bdev_nvme_attach_controller_decoders, 305 SPDK_COUNTOF(rpc_bdev_nvme_attach_controller_decoders), 306 &ctx->req)) { 307 SPDK_ERRLOG("spdk_json_decode_object failed\n"); 308 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 309 "spdk_json_decode_object failed"); 310 goto cleanup; 311 } 312 313 /* Parse trstring */ 314 rc = spdk_nvme_transport_id_populate_trstring(&trid, ctx->req.trtype); 315 if (rc < 0) { 316 SPDK_ERRLOG("Failed to parse trtype: %s\n", ctx->req.trtype); 317 spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "Failed to parse trtype: %s", 318 ctx->req.trtype); 319 goto cleanup; 320 } 321 322 /* Parse trtype */ 323 rc = spdk_nvme_transport_id_parse_trtype(&trid.trtype, ctx->req.trtype); 324 assert(rc == 0); 325 326 /* Parse traddr */ 327 maxlen = sizeof(trid.traddr); 328 len = strnlen(ctx->req.traddr, maxlen); 329 if (len == maxlen) { 330 spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "traddr too long: %s", 331 ctx->req.traddr); 332 goto cleanup; 333 } 334 memcpy(trid.traddr, ctx->req.traddr, len + 1); 335 336 /* Parse adrfam */ 337 if (ctx->req.adrfam) { 338 rc = spdk_nvme_transport_id_parse_adrfam(&trid.adrfam, ctx->req.adrfam); 339 if (rc < 0) { 340 SPDK_ERRLOG("Failed to parse adrfam: %s\n", ctx->req.adrfam); 341 spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "Failed to parse adrfam: %s", 342 ctx->req.adrfam); 343 goto cleanup; 344 } 345 } 346 347 /* Parse trsvcid */ 348 if (ctx->req.trsvcid) { 349 maxlen = sizeof(trid.trsvcid); 350 len = strnlen(ctx->req.trsvcid, maxlen); 351 if (len == maxlen) { 352 spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "trsvcid too long: %s", 353 ctx->req.trsvcid); 354 goto cleanup; 355 } 356 memcpy(trid.trsvcid, ctx->req.trsvcid, len + 1); 357 } 358 359 /* Parse priority for the NVMe-oF transport connection */ 360 if (ctx->req.priority) { 361 trid.priority = spdk_strtol(ctx->req.priority, 10); 362 } 363 364 /* Parse subnqn */ 365 if (ctx->req.subnqn) { 366 maxlen = sizeof(trid.subnqn); 367 len = strnlen(ctx->req.subnqn, maxlen); 368 if (len == maxlen) { 369 spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "subnqn too long: %s", 370 ctx->req.subnqn); 371 goto cleanup; 372 } 373 memcpy(trid.subnqn, ctx->req.subnqn, len + 1); 374 } 375 376 if (ctx->req.hostnqn) { 377 snprintf(ctx->req.opts.hostnqn, sizeof(ctx->req.opts.hostnqn), "%s", 378 ctx->req.hostnqn); 379 } 380 381 if (ctx->req.hostaddr) { 382 maxlen = sizeof(ctx->req.opts.src_addr); 383 len = strnlen(ctx->req.hostaddr, maxlen); 384 if (len == maxlen) { 385 spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "hostaddr too long: %s", 386 ctx->req.hostaddr); 387 goto cleanup; 388 } 389 snprintf(ctx->req.opts.src_addr, maxlen, "%s", ctx->req.hostaddr); 390 } 391 392 if (ctx->req.hostsvcid) { 393 maxlen = sizeof(ctx->req.opts.src_svcid); 394 len = strnlen(ctx->req.hostsvcid, maxlen); 395 if (len == maxlen) { 396 spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "hostsvcid too long: %s", 397 ctx->req.hostsvcid); 398 goto cleanup; 399 } 400 snprintf(ctx->req.opts.src_svcid, maxlen, "%s", ctx->req.hostsvcid); 401 } 402 403 ctrlr = nvme_ctrlr_get_by_name(ctx->req.name); 404 405 if (ctrlr) { 406 if (ctx->req.multipath == NULL) { 407 /* For now, this means add a failover path. This maintains backward compatibility 408 * with past behavior. In the future, this behavior will change to "disable". */ 409 SPDK_ERRLOG("The multipath parameter was not specified to bdev_nvme_attach_controller but " 410 "it was used to add a failover path. This behavior will default to rejecting " 411 "the request in the future. Specify the 'multipath' parameter to control the behavior\n"); 412 ctx->req.multipath = strdup("failover"); 413 if (ctx->req.multipath == NULL) { 414 SPDK_ERRLOG("cannot allocate multipath failover string\n"); 415 goto cleanup; 416 } 417 } 418 419 opts = spdk_nvme_ctrlr_get_opts(ctrlr->ctrlr); 420 ctrlr_trid = spdk_nvme_ctrlr_get_transport_id(ctrlr->ctrlr); 421 422 /* This controller already exists. Check what the user wants to do. */ 423 if (strcasecmp(ctx->req.multipath, "disable") == 0) { 424 /* The user does not want to do any form of multipathing. */ 425 spdk_jsonrpc_send_error_response_fmt(request, -EALREADY, 426 "A controller named %s already exists and multipath is disabled\n", 427 ctx->req.name); 428 goto cleanup; 429 } else if (strcasecmp(ctx->req.multipath, "failover") == 0 || 430 strcasecmp(ctx->req.multipath, "multipath") == 0) { 431 /* The user wants to add this as a failover path or add this to create multipath. */ 432 433 if (strncmp(trid.traddr, ctrlr_trid->traddr, sizeof(trid.traddr)) == 0 && 434 strncmp(trid.trsvcid, ctrlr_trid->trsvcid, sizeof(trid.trsvcid)) == 0 && 435 strncmp(ctx->req.opts.src_addr, opts->src_addr, sizeof(opts->src_addr)) == 0 && 436 strncmp(ctx->req.opts.src_svcid, opts->src_svcid, sizeof(opts->src_svcid)) == 0) { 437 /* Exactly same network path can't be added a second time */ 438 spdk_jsonrpc_send_error_response_fmt(request, -EALREADY, 439 "A controller named %s already exists with the specified network path\n", 440 ctx->req.name); 441 goto cleanup; 442 } 443 } else { 444 /* Invalid multipath option */ 445 spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, 446 "Invalid multipath parameter: %s\n", 447 ctx->req.multipath); 448 goto cleanup; 449 } 450 451 if (strncmp(trid.subnqn, 452 ctrlr_trid->subnqn, 453 SPDK_NVMF_NQN_MAX_LEN) != 0) { 454 /* Different SUBNQN is not allowed when specifying the same controller name. */ 455 spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, 456 "A controller named %s already exists, but uses a different subnqn (%s)\n", 457 ctx->req.name, ctrlr_trid->subnqn); 458 goto cleanup; 459 } 460 461 462 463 if (strncmp(ctx->req.opts.hostnqn, opts->hostnqn, SPDK_NVMF_NQN_MAX_LEN) != 0) { 464 /* Different HOSTNQN is not allowed when specifying the same controller name. */ 465 spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, 466 "A controller named %s already exists, but uses a different hostnqn (%s)\n", 467 ctx->req.name, opts->hostnqn); 468 goto cleanup; 469 } 470 471 if (ctx->req.prchk_guard || ctx->req.prchk_reftag) { 472 spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, 473 "A controller named %s already exists. To add a path, do not specify PI options.\n", 474 ctx->req.name); 475 goto cleanup; 476 } 477 } 478 479 if (ctx->req.prchk_reftag) { 480 prchk_flags |= SPDK_NVME_IO_FLAGS_PRCHK_REFTAG; 481 } 482 483 if (ctx->req.prchk_guard) { 484 prchk_flags |= SPDK_NVME_IO_FLAGS_PRCHK_GUARD; 485 } 486 487 if (ctx->req.multipath != NULL && strcasecmp(ctx->req.multipath, "multipath") == 0) { 488 multipath = true; 489 } 490 491 if (ctx->req.opts.num_io_queues == 0 || ctx->req.opts.num_io_queues > UINT16_MAX + 1) { 492 spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, 493 "num_io_queues out of bounds, min: %u max: %u\n", 494 1, UINT16_MAX + 1); 495 goto cleanup; 496 } 497 498 ctx->request = request; 499 ctx->count = NVME_MAX_BDEVS_PER_RPC; 500 rc = bdev_nvme_create(&trid, ctx->req.name, ctx->names, ctx->count, prchk_flags, 501 rpc_bdev_nvme_attach_controller_done, ctx, &ctx->req.opts, 502 multipath, ctx->req.ctrlr_loss_timeout_sec, 503 ctx->req.reconnect_delay_sec, ctx->req.fast_io_fail_timeout_sec); 504 if (rc) { 505 spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc)); 506 goto cleanup; 507 } 508 509 return; 510 511 cleanup: 512 free_rpc_bdev_nvme_attach_controller(&ctx->req); 513 free(ctx); 514 } 515 SPDK_RPC_REGISTER("bdev_nvme_attach_controller", rpc_bdev_nvme_attach_controller, 516 SPDK_RPC_RUNTIME) 517 SPDK_RPC_REGISTER_ALIAS_DEPRECATED(bdev_nvme_attach_controller, construct_nvme_bdev) 518 519 static const char * 520 nvme_ctrlr_get_state_str(struct nvme_ctrlr *nvme_ctrlr) 521 { 522 if (nvme_ctrlr->destruct) { 523 return "deleting"; 524 } else if (spdk_nvme_ctrlr_is_failed(nvme_ctrlr->ctrlr)) { 525 return "failed"; 526 } else if (nvme_ctrlr->resetting) { 527 return "resetting"; 528 } else if (nvme_ctrlr->reconnect_is_delayed > 0) { 529 return "reconnect_is_delayed"; 530 } else { 531 return "enabled"; 532 } 533 } 534 535 static void 536 rpc_dump_nvme_bdev_controller_info(struct nvme_bdev_ctrlr *nbdev_ctrlr, void *ctx) 537 { 538 struct spdk_json_write_ctx *w = ctx; 539 struct spdk_nvme_transport_id *trid; 540 struct nvme_ctrlr *nvme_ctrlr; 541 const struct spdk_nvme_ctrlr_opts *opts; 542 543 spdk_json_write_object_begin(w); 544 spdk_json_write_named_string(w, "name", nbdev_ctrlr->name); 545 546 spdk_json_write_named_array_begin(w, "ctrlrs"); 547 TAILQ_FOREACH(nvme_ctrlr, &nbdev_ctrlr->ctrlrs, tailq) { 548 spdk_json_write_object_begin(w); 549 550 spdk_json_write_named_string(w, "state", nvme_ctrlr_get_state_str(nvme_ctrlr)); 551 552 #ifdef SPDK_CONFIG_NVME_CUSE 553 size_t cuse_name_size = 128; 554 char cuse_name[cuse_name_size]; 555 556 int rc = spdk_nvme_cuse_get_ctrlr_name(nvme_ctrlr->ctrlr, cuse_name, &cuse_name_size); 557 if (rc == 0) { 558 spdk_json_write_named_string(w, "cuse_device", cuse_name); 559 } 560 #endif 561 trid = &nvme_ctrlr->active_path_id->trid; 562 spdk_json_write_named_object_begin(w, "trid"); 563 nvme_bdev_dump_trid_json(trid, w); 564 spdk_json_write_object_end(w); 565 566 opts = spdk_nvme_ctrlr_get_opts(nvme_ctrlr->ctrlr); 567 spdk_json_write_named_object_begin(w, "host"); 568 spdk_json_write_named_string(w, "nqn", opts->hostnqn); 569 spdk_json_write_named_string(w, "addr", opts->src_addr); 570 spdk_json_write_named_string(w, "svcid", opts->src_svcid); 571 spdk_json_write_object_end(w); 572 spdk_json_write_object_end(w); 573 } 574 spdk_json_write_array_end(w); 575 spdk_json_write_object_end(w); 576 } 577 578 struct rpc_bdev_nvme_get_controllers { 579 char *name; 580 }; 581 582 static void 583 free_rpc_bdev_nvme_get_controllers(struct rpc_bdev_nvme_get_controllers *r) 584 { 585 free(r->name); 586 } 587 588 static const struct spdk_json_object_decoder rpc_bdev_nvme_get_controllers_decoders[] = { 589 {"name", offsetof(struct rpc_bdev_nvme_get_controllers, name), spdk_json_decode_string, true}, 590 }; 591 592 static void 593 rpc_bdev_nvme_get_controllers(struct spdk_jsonrpc_request *request, 594 const struct spdk_json_val *params) 595 { 596 struct rpc_bdev_nvme_get_controllers req = {}; 597 struct spdk_json_write_ctx *w; 598 struct nvme_bdev_ctrlr *nbdev_ctrlr = NULL; 599 600 if (params && spdk_json_decode_object(params, rpc_bdev_nvme_get_controllers_decoders, 601 SPDK_COUNTOF(rpc_bdev_nvme_get_controllers_decoders), 602 &req)) { 603 SPDK_ERRLOG("spdk_json_decode_object failed\n"); 604 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 605 "spdk_json_decode_object failed"); 606 goto cleanup; 607 } 608 609 if (req.name) { 610 nbdev_ctrlr = nvme_bdev_ctrlr_get_by_name(req.name); 611 if (nbdev_ctrlr == NULL) { 612 SPDK_ERRLOG("ctrlr '%s' does not exist\n", req.name); 613 spdk_jsonrpc_send_error_response_fmt(request, EINVAL, "Controller %s does not exist", req.name); 614 goto cleanup; 615 } 616 } 617 618 w = spdk_jsonrpc_begin_result(request); 619 spdk_json_write_array_begin(w); 620 621 if (nbdev_ctrlr != NULL) { 622 rpc_dump_nvme_bdev_controller_info(nbdev_ctrlr, w); 623 } else { 624 nvme_bdev_ctrlr_for_each(rpc_dump_nvme_bdev_controller_info, w); 625 } 626 627 spdk_json_write_array_end(w); 628 629 spdk_jsonrpc_end_result(request, w); 630 631 cleanup: 632 free_rpc_bdev_nvme_get_controllers(&req); 633 } 634 SPDK_RPC_REGISTER("bdev_nvme_get_controllers", rpc_bdev_nvme_get_controllers, SPDK_RPC_RUNTIME) 635 SPDK_RPC_REGISTER_ALIAS_DEPRECATED(bdev_nvme_get_controllers, get_nvme_controllers) 636 637 struct rpc_bdev_nvme_detach_controller { 638 char *name; 639 char *trtype; 640 char *adrfam; 641 char *traddr; 642 char *trsvcid; 643 char *subnqn; 644 char *hostaddr; 645 char *hostsvcid; 646 }; 647 648 static void 649 free_rpc_bdev_nvme_detach_controller(struct rpc_bdev_nvme_detach_controller *req) 650 { 651 free(req->name); 652 free(req->trtype); 653 free(req->adrfam); 654 free(req->traddr); 655 free(req->trsvcid); 656 free(req->subnqn); 657 free(req->hostaddr); 658 free(req->hostsvcid); 659 } 660 661 static const struct spdk_json_object_decoder rpc_bdev_nvme_detach_controller_decoders[] = { 662 {"name", offsetof(struct rpc_bdev_nvme_detach_controller, name), spdk_json_decode_string}, 663 {"trtype", offsetof(struct rpc_bdev_nvme_detach_controller, trtype), spdk_json_decode_string, true}, 664 {"traddr", offsetof(struct rpc_bdev_nvme_detach_controller, traddr), spdk_json_decode_string, true}, 665 {"adrfam", offsetof(struct rpc_bdev_nvme_detach_controller, adrfam), spdk_json_decode_string, true}, 666 {"trsvcid", offsetof(struct rpc_bdev_nvme_detach_controller, trsvcid), spdk_json_decode_string, true}, 667 {"subnqn", offsetof(struct rpc_bdev_nvme_detach_controller, subnqn), spdk_json_decode_string, true}, 668 {"hostaddr", offsetof(struct rpc_bdev_nvme_detach_controller, hostaddr), spdk_json_decode_string, true}, 669 {"hostsvcid", offsetof(struct rpc_bdev_nvme_detach_controller, hostsvcid), spdk_json_decode_string, true}, 670 }; 671 672 static void 673 rpc_bdev_nvme_detach_controller(struct spdk_jsonrpc_request *request, 674 const struct spdk_json_val *params) 675 { 676 struct rpc_bdev_nvme_detach_controller req = {NULL}; 677 struct nvme_path_id path = {}; 678 size_t len, maxlen; 679 int rc = 0; 680 681 if (spdk_json_decode_object(params, rpc_bdev_nvme_detach_controller_decoders, 682 SPDK_COUNTOF(rpc_bdev_nvme_detach_controller_decoders), 683 &req)) { 684 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 685 "spdk_json_decode_object failed"); 686 goto cleanup; 687 } 688 689 if (req.trtype != NULL) { 690 rc = spdk_nvme_transport_id_populate_trstring(&path.trid, req.trtype); 691 if (rc < 0) { 692 SPDK_ERRLOG("Failed to parse trtype: %s\n", req.trtype); 693 spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "Failed to parse trtype: %s", 694 req.trtype); 695 goto cleanup; 696 } 697 698 rc = spdk_nvme_transport_id_parse_trtype(&path.trid.trtype, req.trtype); 699 if (rc < 0) { 700 SPDK_ERRLOG("Failed to parse trtype: %s\n", req.trtype); 701 spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "Failed to parse trtype: %s", 702 req.trtype); 703 goto cleanup; 704 } 705 } 706 707 if (req.traddr != NULL) { 708 maxlen = sizeof(path.trid.traddr); 709 len = strnlen(req.traddr, maxlen); 710 if (len == maxlen) { 711 spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "traddr too long: %s", 712 req.traddr); 713 goto cleanup; 714 } 715 memcpy(path.trid.traddr, req.traddr, len + 1); 716 } 717 718 if (req.adrfam != NULL) { 719 rc = spdk_nvme_transport_id_parse_adrfam(&path.trid.adrfam, req.adrfam); 720 if (rc < 0) { 721 SPDK_ERRLOG("Failed to parse adrfam: %s\n", req.adrfam); 722 spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "Failed to parse adrfam: %s", 723 req.adrfam); 724 goto cleanup; 725 } 726 } 727 728 if (req.trsvcid != NULL) { 729 maxlen = sizeof(path.trid.trsvcid); 730 len = strnlen(req.trsvcid, maxlen); 731 if (len == maxlen) { 732 spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "trsvcid too long: %s", 733 req.trsvcid); 734 goto cleanup; 735 } 736 memcpy(path.trid.trsvcid, req.trsvcid, len + 1); 737 } 738 739 /* Parse subnqn */ 740 if (req.subnqn != NULL) { 741 maxlen = sizeof(path.trid.subnqn); 742 len = strnlen(req.subnqn, maxlen); 743 if (len == maxlen) { 744 spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "subnqn too long: %s", 745 req.subnqn); 746 goto cleanup; 747 } 748 memcpy(path.trid.subnqn, req.subnqn, len + 1); 749 } 750 751 if (req.hostaddr) { 752 maxlen = sizeof(path.hostid.hostaddr); 753 len = strnlen(req.hostaddr, maxlen); 754 if (len == maxlen) { 755 spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "hostaddr too long: %s", 756 req.hostaddr); 757 goto cleanup; 758 } 759 snprintf(path.hostid.hostaddr, maxlen, "%s", req.hostaddr); 760 } 761 762 if (req.hostsvcid) { 763 maxlen = sizeof(path.hostid.hostsvcid); 764 len = strnlen(req.hostsvcid, maxlen); 765 if (len == maxlen) { 766 spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "hostsvcid too long: %s", 767 req.hostsvcid); 768 goto cleanup; 769 } 770 snprintf(path.hostid.hostsvcid, maxlen, "%s", req.hostsvcid); 771 } 772 773 rc = bdev_nvme_delete(req.name, &path); 774 775 if (rc != 0) { 776 spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc)); 777 goto cleanup; 778 } 779 780 spdk_jsonrpc_send_bool_response(request, true); 781 782 cleanup: 783 free_rpc_bdev_nvme_detach_controller(&req); 784 } 785 SPDK_RPC_REGISTER("bdev_nvme_detach_controller", rpc_bdev_nvme_detach_controller, 786 SPDK_RPC_RUNTIME) 787 SPDK_RPC_REGISTER_ALIAS_DEPRECATED(bdev_nvme_detach_controller, delete_nvme_controller) 788 789 struct rpc_apply_firmware { 790 char *filename; 791 char *bdev_name; 792 }; 793 794 static void 795 free_rpc_apply_firmware(struct rpc_apply_firmware *req) 796 { 797 free(req->filename); 798 free(req->bdev_name); 799 } 800 801 static const struct spdk_json_object_decoder rpc_apply_firmware_decoders[] = { 802 {"filename", offsetof(struct rpc_apply_firmware, filename), spdk_json_decode_string}, 803 {"bdev_name", offsetof(struct rpc_apply_firmware, bdev_name), spdk_json_decode_string}, 804 }; 805 806 struct firmware_update_info { 807 void *fw_image; 808 void *p; 809 unsigned int size; 810 unsigned int size_remaining; 811 unsigned int offset; 812 unsigned int transfer; 813 814 void *desc; 815 struct spdk_io_channel *ch; 816 struct spdk_jsonrpc_request *request; 817 struct spdk_nvme_ctrlr *ctrlr; 818 open_descriptors_t desc_head; 819 struct rpc_apply_firmware *req; 820 }; 821 822 static void 823 _apply_firmware_cleanup(void *ctx) 824 { 825 struct spdk_bdev_desc *desc = ctx; 826 827 spdk_bdev_close(desc); 828 } 829 830 static void 831 apply_firmware_cleanup(void *cb_arg) 832 { 833 struct open_descriptors *opt, *tmp; 834 struct firmware_update_info *firm_ctx = cb_arg; 835 836 if (!firm_ctx) { 837 return; 838 } 839 840 if (firm_ctx->fw_image) { 841 spdk_free(firm_ctx->fw_image); 842 } 843 844 if (firm_ctx->req) { 845 free_rpc_apply_firmware(firm_ctx->req); 846 free(firm_ctx->req); 847 } 848 849 if (firm_ctx->ch) { 850 spdk_put_io_channel(firm_ctx->ch); 851 } 852 853 TAILQ_FOREACH_SAFE(opt, &firm_ctx->desc_head, tqlst, tmp) { 854 TAILQ_REMOVE(&firm_ctx->desc_head, opt, tqlst); 855 /* Close the underlying bdev on its same opened thread. */ 856 if (opt->thread && opt->thread != spdk_get_thread()) { 857 spdk_thread_send_msg(opt->thread, _apply_firmware_cleanup, opt->desc); 858 } else { 859 spdk_bdev_close(opt->desc); 860 } 861 free(opt); 862 } 863 free(firm_ctx); 864 } 865 866 static void 867 apply_firmware_complete_reset(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg) 868 { 869 struct spdk_json_write_ctx *w; 870 struct firmware_update_info *firm_ctx = cb_arg; 871 872 spdk_bdev_free_io(bdev_io); 873 874 if (!success) { 875 spdk_jsonrpc_send_error_response(firm_ctx->request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 876 "firmware commit failed."); 877 apply_firmware_cleanup(firm_ctx); 878 return; 879 } 880 881 if (spdk_nvme_ctrlr_reset(firm_ctx->ctrlr) != 0) { 882 spdk_jsonrpc_send_error_response(firm_ctx->request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 883 "Controller reset failed."); 884 apply_firmware_cleanup(firm_ctx); 885 return; 886 } 887 888 w = spdk_jsonrpc_begin_result(firm_ctx->request); 889 spdk_json_write_string(w, "firmware commit succeeded. Controller reset in progress."); 890 spdk_jsonrpc_end_result(firm_ctx->request, w); 891 apply_firmware_cleanup(firm_ctx); 892 } 893 894 static void 895 apply_firmware_complete(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg) 896 { 897 struct spdk_nvme_cmd cmd = {}; 898 struct spdk_nvme_fw_commit fw_commit; 899 int slot = 0; 900 int rc; 901 struct firmware_update_info *firm_ctx = cb_arg; 902 enum spdk_nvme_fw_commit_action commit_action = SPDK_NVME_FW_COMMIT_REPLACE_AND_ENABLE_IMG; 903 904 if (!success) { 905 spdk_jsonrpc_send_error_response(firm_ctx->request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 906 "firmware download failed ."); 907 spdk_bdev_free_io(bdev_io); 908 apply_firmware_cleanup(firm_ctx); 909 return; 910 } 911 912 firm_ctx->p += firm_ctx->transfer; 913 firm_ctx->offset += firm_ctx->transfer; 914 firm_ctx->size_remaining -= firm_ctx->transfer; 915 916 switch (firm_ctx->size_remaining) { 917 case 0: 918 /* firmware download completed. Commit firmware */ 919 memset(&fw_commit, 0, sizeof(struct spdk_nvme_fw_commit)); 920 fw_commit.fs = slot; 921 fw_commit.ca = commit_action; 922 923 cmd.opc = SPDK_NVME_OPC_FIRMWARE_COMMIT; 924 memcpy(&cmd.cdw10, &fw_commit, sizeof(uint32_t)); 925 rc = spdk_bdev_nvme_admin_passthru(firm_ctx->desc, firm_ctx->ch, &cmd, NULL, 0, 926 apply_firmware_complete_reset, firm_ctx); 927 if (rc) { 928 spdk_jsonrpc_send_error_response(firm_ctx->request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 929 "firmware commit failed."); 930 spdk_bdev_free_io(bdev_io); 931 apply_firmware_cleanup(firm_ctx); 932 return; 933 } 934 break; 935 default: 936 firm_ctx->transfer = spdk_min(firm_ctx->size_remaining, 4096); 937 cmd.opc = SPDK_NVME_OPC_FIRMWARE_IMAGE_DOWNLOAD; 938 939 cmd.cdw10 = spdk_nvme_bytes_to_numd(firm_ctx->transfer); 940 cmd.cdw11 = firm_ctx->offset >> 2; 941 rc = spdk_bdev_nvme_admin_passthru(firm_ctx->desc, firm_ctx->ch, &cmd, firm_ctx->p, 942 firm_ctx->transfer, apply_firmware_complete, firm_ctx); 943 if (rc) { 944 spdk_jsonrpc_send_error_response(firm_ctx->request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 945 "firmware download failed."); 946 spdk_bdev_free_io(bdev_io); 947 apply_firmware_cleanup(firm_ctx); 948 return; 949 } 950 break; 951 } 952 } 953 954 static void 955 apply_firmware_open_cb(enum spdk_bdev_event_type type, struct spdk_bdev *bdev, void *event_ctx) 956 { 957 } 958 959 static void 960 rpc_bdev_nvme_apply_firmware(struct spdk_jsonrpc_request *request, 961 const struct spdk_json_val *params) 962 { 963 int rc; 964 int fd = -1; 965 struct stat fw_stat; 966 struct spdk_nvme_ctrlr *ctrlr; 967 char msg[1024]; 968 struct spdk_bdev *bdev; 969 struct spdk_bdev *bdev2; 970 struct open_descriptors *opt; 971 struct spdk_bdev_desc *desc; 972 struct spdk_nvme_cmd *cmd; 973 struct firmware_update_info *firm_ctx; 974 975 firm_ctx = calloc(1, sizeof(struct firmware_update_info)); 976 if (!firm_ctx) { 977 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 978 "Memory allocation error."); 979 return; 980 } 981 firm_ctx->fw_image = NULL; 982 TAILQ_INIT(&firm_ctx->desc_head); 983 firm_ctx->request = request; 984 985 firm_ctx->req = calloc(1, sizeof(struct rpc_apply_firmware)); 986 if (!firm_ctx->req) { 987 snprintf(msg, sizeof(msg), "Memory allocation error."); 988 goto err; 989 } 990 991 if (spdk_json_decode_object(params, rpc_apply_firmware_decoders, 992 SPDK_COUNTOF(rpc_apply_firmware_decoders), firm_ctx->req)) { 993 snprintf(msg, sizeof(msg), "spdk_json_decode_object failed."); 994 goto err; 995 } 996 997 if ((bdev = spdk_bdev_get_by_name(firm_ctx->req->bdev_name)) == NULL) { 998 snprintf(msg, sizeof(msg), "bdev %s were not found", firm_ctx->req->bdev_name); 999 goto err; 1000 } 1001 1002 if ((ctrlr = bdev_nvme_get_ctrlr(bdev)) == NULL) { 1003 snprintf(msg, sizeof(msg), "Controller information for %s were not found.", 1004 firm_ctx->req->bdev_name); 1005 goto err; 1006 } 1007 firm_ctx->ctrlr = ctrlr; 1008 1009 for (bdev2 = spdk_bdev_first(); bdev2; bdev2 = spdk_bdev_next(bdev2)) { 1010 1011 if (bdev_nvme_get_ctrlr(bdev2) != ctrlr) { 1012 continue; 1013 } 1014 1015 if (!(opt = malloc(sizeof(struct open_descriptors)))) { 1016 snprintf(msg, sizeof(msg), "Memory allocation error."); 1017 goto err; 1018 } 1019 1020 if (spdk_bdev_open_ext(spdk_bdev_get_name(bdev2), true, apply_firmware_open_cb, NULL, &desc) != 0) { 1021 snprintf(msg, sizeof(msg), "Device %s is in use.", firm_ctx->req->bdev_name); 1022 free(opt); 1023 goto err; 1024 } 1025 1026 /* Save the thread where the base device is opened */ 1027 opt->thread = spdk_get_thread(); 1028 1029 opt->desc = desc; 1030 opt->bdev = bdev; 1031 TAILQ_INSERT_TAIL(&firm_ctx->desc_head, opt, tqlst); 1032 } 1033 1034 /* 1035 * find a descriptor associated with our bdev 1036 */ 1037 firm_ctx->desc = NULL; 1038 TAILQ_FOREACH(opt, &firm_ctx->desc_head, tqlst) { 1039 if (opt->bdev == bdev) { 1040 firm_ctx->desc = opt->desc; 1041 break; 1042 } 1043 } 1044 1045 if (!firm_ctx->desc) { 1046 snprintf(msg, sizeof(msg), "No descriptor were found."); 1047 goto err; 1048 } 1049 1050 firm_ctx->ch = spdk_bdev_get_io_channel(firm_ctx->desc); 1051 if (!firm_ctx->ch) { 1052 snprintf(msg, sizeof(msg), "No channels were found."); 1053 goto err; 1054 } 1055 1056 fd = open(firm_ctx->req->filename, O_RDONLY); 1057 if (fd < 0) { 1058 snprintf(msg, sizeof(msg), "open file failed."); 1059 goto err; 1060 } 1061 1062 rc = fstat(fd, &fw_stat); 1063 if (rc < 0) { 1064 close(fd); 1065 snprintf(msg, sizeof(msg), "fstat failed."); 1066 goto err; 1067 } 1068 1069 firm_ctx->size = fw_stat.st_size; 1070 if (fw_stat.st_size % 4) { 1071 close(fd); 1072 snprintf(msg, sizeof(msg), "Firmware image size is not multiple of 4."); 1073 goto err; 1074 } 1075 1076 firm_ctx->fw_image = spdk_zmalloc(firm_ctx->size, 4096, NULL, 1077 SPDK_ENV_LCORE_ID_ANY, SPDK_MALLOC_DMA); 1078 if (!firm_ctx->fw_image) { 1079 close(fd); 1080 snprintf(msg, sizeof(msg), "Memory allocation error."); 1081 goto err; 1082 } 1083 firm_ctx->p = firm_ctx->fw_image; 1084 1085 if (read(fd, firm_ctx->p, firm_ctx->size) != ((ssize_t)(firm_ctx->size))) { 1086 close(fd); 1087 snprintf(msg, sizeof(msg), "Read firmware image failed!"); 1088 goto err; 1089 } 1090 close(fd); 1091 1092 firm_ctx->offset = 0; 1093 firm_ctx->size_remaining = firm_ctx->size; 1094 firm_ctx->transfer = spdk_min(firm_ctx->size_remaining, 4096); 1095 1096 cmd = malloc(sizeof(struct spdk_nvme_cmd)); 1097 if (!cmd) { 1098 snprintf(msg, sizeof(msg), "Memory allocation error."); 1099 goto err; 1100 } 1101 memset(cmd, 0, sizeof(struct spdk_nvme_cmd)); 1102 cmd->opc = SPDK_NVME_OPC_FIRMWARE_IMAGE_DOWNLOAD; 1103 1104 cmd->cdw10 = spdk_nvme_bytes_to_numd(firm_ctx->transfer); 1105 cmd->cdw11 = firm_ctx->offset >> 2; 1106 1107 rc = spdk_bdev_nvme_admin_passthru(firm_ctx->desc, firm_ctx->ch, cmd, firm_ctx->p, 1108 firm_ctx->transfer, apply_firmware_complete, firm_ctx); 1109 if (rc == 0) { 1110 /* normal return here. */ 1111 return; 1112 } 1113 1114 free(cmd); 1115 snprintf(msg, sizeof(msg), "Read firmware image failed!"); 1116 err: 1117 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, msg); 1118 apply_firmware_cleanup(firm_ctx); 1119 } 1120 SPDK_RPC_REGISTER("bdev_nvme_apply_firmware", rpc_bdev_nvme_apply_firmware, SPDK_RPC_RUNTIME) 1121 SPDK_RPC_REGISTER_ALIAS_DEPRECATED(bdev_nvme_apply_firmware, apply_nvme_firmware) 1122 1123 struct rpc_bdev_nvme_transport_stat_ctx { 1124 struct spdk_jsonrpc_request *request; 1125 struct spdk_json_write_ctx *w; 1126 }; 1127 1128 static void 1129 rpc_bdev_nvme_rdma_stats(struct spdk_json_write_ctx *w, 1130 struct spdk_nvme_transport_poll_group_stat *stat) 1131 { 1132 struct spdk_nvme_rdma_device_stat *device_stats; 1133 uint32_t i; 1134 1135 spdk_json_write_named_array_begin(w, "devices"); 1136 1137 for (i = 0; i < stat->rdma.num_devices; i++) { 1138 device_stats = &stat->rdma.device_stats[i]; 1139 spdk_json_write_object_begin(w); 1140 spdk_json_write_named_string(w, "dev_name", device_stats->name); 1141 spdk_json_write_named_uint64(w, "polls", device_stats->polls); 1142 spdk_json_write_named_uint64(w, "idle_polls", device_stats->idle_polls); 1143 spdk_json_write_named_uint64(w, "completions", device_stats->completions); 1144 spdk_json_write_named_uint64(w, "queued_requests", device_stats->queued_requests); 1145 spdk_json_write_named_uint64(w, "total_send_wrs", device_stats->total_send_wrs); 1146 spdk_json_write_named_uint64(w, "send_doorbell_updates", device_stats->send_doorbell_updates); 1147 spdk_json_write_named_uint64(w, "total_recv_wrs", device_stats->total_recv_wrs); 1148 spdk_json_write_named_uint64(w, "recv_doorbell_updates", device_stats->recv_doorbell_updates); 1149 spdk_json_write_object_end(w); 1150 } 1151 spdk_json_write_array_end(w); 1152 } 1153 1154 static void 1155 rpc_bdev_nvme_pcie_stats(struct spdk_json_write_ctx *w, 1156 struct spdk_nvme_transport_poll_group_stat *stat) 1157 { 1158 spdk_json_write_named_uint64(w, "polls", stat->pcie.polls); 1159 spdk_json_write_named_uint64(w, "idle_polls", stat->pcie.idle_polls); 1160 spdk_json_write_named_uint64(w, "completions", stat->pcie.completions); 1161 spdk_json_write_named_uint64(w, "cq_doorbell_updates", stat->pcie.cq_doorbell_updates); 1162 spdk_json_write_named_uint64(w, "queued_requests", stat->pcie.queued_requests); 1163 spdk_json_write_named_uint64(w, "submitted_requests", stat->pcie.submitted_requests); 1164 spdk_json_write_named_uint64(w, "sq_doobell_updates", stat->pcie.sq_doobell_updates); 1165 } 1166 1167 static void 1168 rpc_bdev_nvme_tcp_stats(struct spdk_json_write_ctx *w, 1169 struct spdk_nvme_transport_poll_group_stat *stat) 1170 { 1171 spdk_json_write_named_uint64(w, "polls", stat->tcp.polls); 1172 spdk_json_write_named_uint64(w, "idle_polls", stat->tcp.idle_polls); 1173 spdk_json_write_named_uint64(w, "socket_completions", stat->tcp.socket_completions); 1174 spdk_json_write_named_uint64(w, "nvme_completions", stat->tcp.nvme_completions); 1175 spdk_json_write_named_uint64(w, "queued_requests", stat->tcp.queued_requests); 1176 spdk_json_write_named_uint64(w, "submitted_requests", stat->tcp.submitted_requests); 1177 } 1178 1179 static void 1180 rpc_bdev_nvme_stats_per_channel(struct spdk_io_channel_iter *i) 1181 { 1182 struct rpc_bdev_nvme_transport_stat_ctx *ctx; 1183 struct spdk_io_channel *ch; 1184 struct nvme_poll_group *group; 1185 struct spdk_nvme_poll_group_stat *stat; 1186 struct spdk_nvme_transport_poll_group_stat *tr_stat; 1187 uint32_t j; 1188 int rc; 1189 1190 ctx = spdk_io_channel_iter_get_ctx(i); 1191 ch = spdk_io_channel_iter_get_channel(i); 1192 group = spdk_io_channel_get_ctx(ch); 1193 1194 rc = spdk_nvme_poll_group_get_stats(group->group, &stat); 1195 if (rc) { 1196 spdk_for_each_channel_continue(i, rc); 1197 return; 1198 } 1199 1200 spdk_json_write_object_begin(ctx->w); 1201 spdk_json_write_named_string(ctx->w, "thread", spdk_thread_get_name(spdk_get_thread())); 1202 spdk_json_write_named_array_begin(ctx->w, "transports"); 1203 1204 for (j = 0; j < stat->num_transports; j++) { 1205 tr_stat = stat->transport_stat[j]; 1206 spdk_json_write_object_begin(ctx->w); 1207 spdk_json_write_named_string(ctx->w, "trname", spdk_nvme_transport_id_trtype_str(tr_stat->trtype)); 1208 1209 switch (stat->transport_stat[j]->trtype) { 1210 case SPDK_NVME_TRANSPORT_RDMA: 1211 rpc_bdev_nvme_rdma_stats(ctx->w, tr_stat); 1212 break; 1213 case SPDK_NVME_TRANSPORT_PCIE: 1214 rpc_bdev_nvme_pcie_stats(ctx->w, tr_stat); 1215 break; 1216 case SPDK_NVME_TRANSPORT_TCP: 1217 rpc_bdev_nvme_tcp_stats(ctx->w, tr_stat); 1218 break; 1219 default: 1220 SPDK_WARNLOG("Can't handle trtype %d %s\n", tr_stat->trtype, 1221 spdk_nvme_transport_id_trtype_str(tr_stat->trtype)); 1222 } 1223 spdk_json_write_object_end(ctx->w); 1224 } 1225 /* transports array */ 1226 spdk_json_write_array_end(ctx->w); 1227 spdk_json_write_object_end(ctx->w); 1228 1229 spdk_nvme_poll_group_free_stats(group->group, stat); 1230 spdk_for_each_channel_continue(i, 0); 1231 } 1232 1233 static void 1234 rpc_bdev_nvme_stats_done(struct spdk_io_channel_iter *i, int status) 1235 { 1236 struct rpc_bdev_nvme_transport_stat_ctx *ctx = spdk_io_channel_iter_get_ctx(i); 1237 1238 spdk_json_write_array_end(ctx->w); 1239 spdk_json_write_object_end(ctx->w); 1240 spdk_jsonrpc_end_result(ctx->request, ctx->w); 1241 free(ctx); 1242 } 1243 1244 static void 1245 rpc_bdev_nvme_get_transport_statistics(struct spdk_jsonrpc_request *request, 1246 const struct spdk_json_val *params) 1247 { 1248 struct rpc_bdev_nvme_transport_stat_ctx *ctx; 1249 1250 if (params) { 1251 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, 1252 "'bdev_nvme_get_transport_statistics' requires no arguments"); 1253 return; 1254 } 1255 1256 ctx = calloc(1, sizeof(*ctx)); 1257 if (!ctx) { 1258 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 1259 "Memory allocation error"); 1260 return; 1261 } 1262 ctx->request = request; 1263 ctx->w = spdk_jsonrpc_begin_result(ctx->request); 1264 spdk_json_write_object_begin(ctx->w); 1265 spdk_json_write_named_array_begin(ctx->w, "poll_groups"); 1266 1267 spdk_for_each_channel(&g_nvme_bdev_ctrlrs, 1268 rpc_bdev_nvme_stats_per_channel, 1269 ctx, 1270 rpc_bdev_nvme_stats_done); 1271 } 1272 SPDK_RPC_REGISTER("bdev_nvme_get_transport_statistics", rpc_bdev_nvme_get_transport_statistics, 1273 SPDK_RPC_RUNTIME) 1274 1275 struct rpc_bdev_nvme_reset_controller_req { 1276 char *name; 1277 }; 1278 1279 static void 1280 free_rpc_bdev_nvme_reset_controller_req(struct rpc_bdev_nvme_reset_controller_req *r) 1281 { 1282 free(r->name); 1283 } 1284 1285 static const struct spdk_json_object_decoder rpc_bdev_nvme_reset_controller_req_decoders[] = { 1286 {"name", offsetof(struct rpc_bdev_nvme_reset_controller_req, name), spdk_json_decode_string}, 1287 }; 1288 1289 struct rpc_bdev_nvme_reset_controller_ctx { 1290 struct spdk_jsonrpc_request *request; 1291 bool success; 1292 struct spdk_thread *orig_thread; 1293 }; 1294 1295 static void 1296 _rpc_bdev_nvme_reset_controller_cb(void *_ctx) 1297 { 1298 struct rpc_bdev_nvme_reset_controller_ctx *ctx = _ctx; 1299 1300 spdk_jsonrpc_send_bool_response(ctx->request, ctx->success); 1301 1302 free(ctx); 1303 } 1304 1305 static void 1306 rpc_bdev_nvme_reset_controller_cb(void *cb_arg, bool success) 1307 { 1308 struct rpc_bdev_nvme_reset_controller_ctx *ctx = cb_arg; 1309 1310 ctx->success = success; 1311 1312 spdk_thread_send_msg(ctx->orig_thread, _rpc_bdev_nvme_reset_controller_cb, ctx); 1313 } 1314 1315 static void 1316 rpc_bdev_nvme_reset_controller(struct spdk_jsonrpc_request *request, 1317 const struct spdk_json_val *params) 1318 { 1319 struct rpc_bdev_nvme_reset_controller_req req = {NULL}; 1320 struct rpc_bdev_nvme_reset_controller_ctx *ctx; 1321 struct nvme_ctrlr *nvme_ctrlr; 1322 int rc; 1323 1324 ctx = calloc(1, sizeof(*ctx)); 1325 if (ctx == NULL) { 1326 SPDK_ERRLOG("Memory allocation failed\n"); 1327 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 1328 "Memory allocation failed"); 1329 return; 1330 } 1331 1332 if (spdk_json_decode_object(params, rpc_bdev_nvme_reset_controller_req_decoders, 1333 SPDK_COUNTOF(rpc_bdev_nvme_reset_controller_req_decoders), 1334 &req)) { 1335 SPDK_ERRLOG("spdk_json_decode_object failed\n"); 1336 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, spdk_strerror(EINVAL)); 1337 goto err; 1338 } 1339 1340 nvme_ctrlr = nvme_ctrlr_get_by_name(req.name); 1341 if (nvme_ctrlr == NULL) { 1342 SPDK_ERRLOG("Failed at device lookup\n"); 1343 spdk_jsonrpc_send_error_response(request, -ENODEV, spdk_strerror(ENODEV)); 1344 goto err; 1345 } 1346 1347 ctx->request = request; 1348 ctx->orig_thread = spdk_get_thread(); 1349 1350 rc = bdev_nvme_reset_rpc(nvme_ctrlr, rpc_bdev_nvme_reset_controller_cb, ctx); 1351 if (rc != 0) { 1352 SPDK_NOTICELOG("Failed at bdev_nvme_reset_rpc\n"); 1353 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, spdk_strerror(-rc)); 1354 goto err; 1355 } 1356 1357 free_rpc_bdev_nvme_reset_controller_req(&req); 1358 return; 1359 1360 err: 1361 free_rpc_bdev_nvme_reset_controller_req(&req); 1362 free(ctx); 1363 } 1364 SPDK_RPC_REGISTER("bdev_nvme_reset_controller", rpc_bdev_nvme_reset_controller, SPDK_RPC_RUNTIME) 1365 1366 struct rpc_get_controller_health_info { 1367 char *name; 1368 }; 1369 1370 struct spdk_nvme_health_info_context { 1371 struct spdk_jsonrpc_request *request; 1372 struct spdk_nvme_ctrlr *ctrlr; 1373 struct spdk_nvme_health_information_page health_page; 1374 }; 1375 1376 static void 1377 free_rpc_get_controller_health_info(struct rpc_get_controller_health_info *r) 1378 { 1379 free(r->name); 1380 } 1381 1382 static const struct spdk_json_object_decoder rpc_get_controller_health_info_decoders[] = { 1383 {"name", offsetof(struct rpc_get_controller_health_info, name), spdk_json_decode_string, true}, 1384 }; 1385 1386 static void nvme_health_info_cleanup(struct spdk_nvme_health_info_context *context, bool response) 1387 { 1388 if (response == true) { 1389 spdk_jsonrpc_send_error_response(context->request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 1390 "Internal error."); 1391 } 1392 1393 free(context); 1394 } 1395 1396 static void 1397 get_health_log_page_completion(void *cb_arg, const struct spdk_nvme_cpl *cpl) 1398 { 1399 int i; 1400 char buf[128]; 1401 struct spdk_nvme_health_info_context *context = cb_arg; 1402 struct spdk_jsonrpc_request *request = context->request; 1403 struct spdk_json_write_ctx *w; 1404 struct spdk_nvme_ctrlr *ctrlr = context->ctrlr; 1405 const struct spdk_nvme_transport_id *trid = NULL; 1406 const struct spdk_nvme_ctrlr_data *cdata = NULL; 1407 struct spdk_nvme_health_information_page *health_page = NULL; 1408 1409 if (spdk_nvme_cpl_is_error(cpl)) { 1410 nvme_health_info_cleanup(context, true); 1411 SPDK_ERRLOG("get log page failed\n"); 1412 return; 1413 } 1414 1415 if (ctrlr == NULL) { 1416 nvme_health_info_cleanup(context, true); 1417 SPDK_ERRLOG("ctrlr is NULL\n"); 1418 return; 1419 } else { 1420 trid = spdk_nvme_ctrlr_get_transport_id(ctrlr); 1421 cdata = spdk_nvme_ctrlr_get_data(ctrlr); 1422 health_page = &(context->health_page); 1423 } 1424 1425 w = spdk_jsonrpc_begin_result(request); 1426 1427 spdk_json_write_object_begin(w); 1428 snprintf(buf, sizeof(cdata->mn) + 1, "%s", cdata->mn); 1429 spdk_str_trim(buf); 1430 spdk_json_write_named_string(w, "model_number", buf); 1431 snprintf(buf, sizeof(cdata->sn) + 1, "%s", cdata->sn); 1432 spdk_str_trim(buf); 1433 spdk_json_write_named_string(w, "serial_number", buf); 1434 snprintf(buf, sizeof(cdata->fr) + 1, "%s", cdata->fr); 1435 spdk_str_trim(buf); 1436 spdk_json_write_named_string(w, "firmware_revision", buf); 1437 spdk_json_write_named_string(w, "traddr", trid->traddr); 1438 spdk_json_write_named_uint64(w, "temperature_celsius", health_page->temperature - 273); 1439 spdk_json_write_named_uint64(w, "available_spare_percentage", health_page->available_spare); 1440 spdk_json_write_named_uint64(w, "available_spare_threshold_percentage", 1441 health_page->available_spare_threshold); 1442 spdk_json_write_named_uint64(w, "percentage_used", health_page->percentage_used); 1443 spdk_json_write_named_uint128(w, "data_units_read", 1444 health_page->data_units_read[0], health_page->data_units_read[1]); 1445 spdk_json_write_named_uint128(w, "data_units_written", 1446 health_page->data_units_written[0], health_page->data_units_written[1]); 1447 spdk_json_write_named_uint128(w, "host_read_commands", 1448 health_page->host_read_commands[0], health_page->host_read_commands[1]); 1449 spdk_json_write_named_uint128(w, "host_write_commands", 1450 health_page->host_write_commands[0], health_page->host_write_commands[1]); 1451 spdk_json_write_named_uint128(w, "controller_busy_time", 1452 health_page->controller_busy_time[0], health_page->controller_busy_time[1]); 1453 spdk_json_write_named_uint128(w, "power_cycles", 1454 health_page->power_cycles[0], health_page->power_cycles[1]); 1455 spdk_json_write_named_uint128(w, "power_on_hours", 1456 health_page->power_on_hours[0], health_page->power_on_hours[1]); 1457 spdk_json_write_named_uint128(w, "unsafe_shutdowns", 1458 health_page->unsafe_shutdowns[0], health_page->unsafe_shutdowns[1]); 1459 spdk_json_write_named_uint128(w, "media_errors", 1460 health_page->media_errors[0], health_page->media_errors[1]); 1461 spdk_json_write_named_uint128(w, "num_err_log_entries", 1462 health_page->num_error_info_log_entries[0], health_page->num_error_info_log_entries[1]); 1463 spdk_json_write_named_uint64(w, "warning_temperature_time_minutes", health_page->warning_temp_time); 1464 spdk_json_write_named_uint64(w, "critical_composite_temperature_time_minutes", 1465 health_page->critical_temp_time); 1466 for (i = 0; i < 8; i++) { 1467 if (health_page->temp_sensor[i] != 0) { 1468 spdk_json_write_named_uint64(w, "temperature_sensor_celsius", health_page->temp_sensor[i] - 273); 1469 } 1470 } 1471 spdk_json_write_object_end(w); 1472 1473 spdk_jsonrpc_end_result(request, w); 1474 nvme_health_info_cleanup(context, false); 1475 } 1476 1477 static void 1478 get_health_log_page(struct spdk_nvme_health_info_context *context) 1479 { 1480 struct spdk_nvme_ctrlr *ctrlr = context->ctrlr; 1481 1482 if (spdk_nvme_ctrlr_cmd_get_log_page(ctrlr, SPDK_NVME_LOG_HEALTH_INFORMATION, 1483 SPDK_NVME_GLOBAL_NS_TAG, 1484 &(context->health_page), sizeof(context->health_page), 0, 1485 get_health_log_page_completion, context)) { 1486 nvme_health_info_cleanup(context, true); 1487 SPDK_ERRLOG("spdk_nvme_ctrlr_cmd_get_log_page() failed\n"); 1488 } 1489 } 1490 1491 static void 1492 get_temperature_threshold_feature_completion(void *cb_arg, const struct spdk_nvme_cpl *cpl) 1493 { 1494 struct spdk_nvme_health_info_context *context = cb_arg; 1495 1496 if (spdk_nvme_cpl_is_error(cpl)) { 1497 nvme_health_info_cleanup(context, true); 1498 SPDK_ERRLOG("feature SPDK_NVME_FEAT_TEMPERATURE_THRESHOLD failed in completion\n"); 1499 } else { 1500 get_health_log_page(context); 1501 } 1502 } 1503 1504 static int 1505 get_temperature_threshold_feature(struct spdk_nvme_health_info_context *context) 1506 { 1507 struct spdk_nvme_cmd cmd = {}; 1508 1509 cmd.opc = SPDK_NVME_OPC_GET_FEATURES; 1510 cmd.cdw10 = SPDK_NVME_FEAT_TEMPERATURE_THRESHOLD; 1511 1512 return spdk_nvme_ctrlr_cmd_admin_raw(context->ctrlr, &cmd, NULL, 0, 1513 get_temperature_threshold_feature_completion, context); 1514 } 1515 1516 static void 1517 get_controller_health_info(struct spdk_jsonrpc_request *request, struct spdk_nvme_ctrlr *ctrlr) 1518 { 1519 struct spdk_nvme_health_info_context *context; 1520 1521 context = calloc(1, sizeof(struct spdk_nvme_health_info_context)); 1522 if (!context) { 1523 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 1524 "Memory allocation error."); 1525 return; 1526 } 1527 1528 context->request = request; 1529 context->ctrlr = ctrlr; 1530 1531 if (get_temperature_threshold_feature(context)) { 1532 nvme_health_info_cleanup(context, true); 1533 SPDK_ERRLOG("feature SPDK_NVME_FEAT_TEMPERATURE_THRESHOLD failed to submit\n"); 1534 } 1535 1536 return; 1537 } 1538 1539 static void 1540 rpc_bdev_nvme_get_controller_health_info(struct spdk_jsonrpc_request *request, 1541 const struct spdk_json_val *params) 1542 { 1543 struct rpc_get_controller_health_info req = {}; 1544 struct nvme_ctrlr *nvme_ctrlr = NULL; 1545 1546 if (!params) { 1547 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 1548 "Missing device name"); 1549 1550 return; 1551 } 1552 if (spdk_json_decode_object(params, rpc_get_controller_health_info_decoders, 1553 SPDK_COUNTOF(rpc_get_controller_health_info_decoders), &req)) { 1554 SPDK_ERRLOG("spdk_json_decode_object failed\n"); 1555 free_rpc_get_controller_health_info(&req); 1556 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 1557 "Invalid parameters"); 1558 1559 return; 1560 } 1561 1562 nvme_ctrlr = nvme_ctrlr_get_by_name(req.name); 1563 1564 if (!nvme_ctrlr) { 1565 SPDK_ERRLOG("nvme ctrlr name '%s' does not exist\n", req.name); 1566 free_rpc_get_controller_health_info(&req); 1567 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 1568 "Device not found"); 1569 return; 1570 } 1571 1572 get_controller_health_info(request, nvme_ctrlr->ctrlr); 1573 free_rpc_get_controller_health_info(&req); 1574 1575 return; 1576 } 1577 SPDK_RPC_REGISTER("bdev_nvme_get_controller_health_info", 1578 rpc_bdev_nvme_get_controller_health_info, SPDK_RPC_RUNTIME) 1579 1580 struct rpc_bdev_nvme_start_discovery { 1581 char *name; 1582 char *trtype; 1583 char *adrfam; 1584 char *traddr; 1585 char *trsvcid; 1586 char *hostnqn; 1587 struct spdk_nvme_ctrlr_opts opts; 1588 }; 1589 1590 static void 1591 free_rpc_bdev_nvme_start_discovery(struct rpc_bdev_nvme_start_discovery *req) 1592 { 1593 free(req->name); 1594 free(req->trtype); 1595 free(req->adrfam); 1596 free(req->traddr); 1597 free(req->trsvcid); 1598 free(req->hostnqn); 1599 } 1600 1601 static const struct spdk_json_object_decoder rpc_bdev_nvme_start_discovery_decoders[] = { 1602 {"name", offsetof(struct rpc_bdev_nvme_start_discovery, name), spdk_json_decode_string}, 1603 {"trtype", offsetof(struct rpc_bdev_nvme_start_discovery, trtype), spdk_json_decode_string}, 1604 {"traddr", offsetof(struct rpc_bdev_nvme_start_discovery, traddr), spdk_json_decode_string}, 1605 {"adrfam", offsetof(struct rpc_bdev_nvme_start_discovery, adrfam), spdk_json_decode_string, true}, 1606 {"trsvcid", offsetof(struct rpc_bdev_nvme_start_discovery, trsvcid), spdk_json_decode_string, true}, 1607 {"hostnqn", offsetof(struct rpc_bdev_nvme_start_discovery, hostnqn), spdk_json_decode_string, true}, 1608 }; 1609 1610 struct rpc_bdev_nvme_start_discovery_ctx { 1611 struct rpc_bdev_nvme_start_discovery req; 1612 struct spdk_jsonrpc_request *request; 1613 }; 1614 1615 static void 1616 rpc_bdev_nvme_start_discovery_done(void *cb_ctx, int rc) 1617 { 1618 struct rpc_bdev_nvme_start_discovery_ctx *ctx = cb_ctx; 1619 struct spdk_jsonrpc_request *request = ctx->request; 1620 1621 if (rc < 0) { 1622 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters"); 1623 free_rpc_bdev_nvme_start_discovery(&ctx->req); 1624 free(ctx); 1625 return; 1626 } 1627 1628 spdk_jsonrpc_send_bool_response(ctx->request, rc == 0); 1629 1630 free_rpc_bdev_nvme_start_discovery(&ctx->req); 1631 free(ctx); 1632 } 1633 1634 static void 1635 rpc_bdev_nvme_start_discovery(struct spdk_jsonrpc_request *request, 1636 const struct spdk_json_val *params) 1637 { 1638 struct rpc_bdev_nvme_start_discovery_ctx *ctx; 1639 struct spdk_nvme_transport_id trid = {}; 1640 size_t len, maxlen; 1641 int rc; 1642 1643 ctx = calloc(1, sizeof(*ctx)); 1644 if (!ctx) { 1645 spdk_jsonrpc_send_error_response(request, -ENOMEM, spdk_strerror(ENOMEM)); 1646 return; 1647 } 1648 1649 spdk_nvme_ctrlr_get_default_ctrlr_opts(&ctx->req.opts, sizeof(ctx->req.opts)); 1650 1651 if (spdk_json_decode_object(params, rpc_bdev_nvme_start_discovery_decoders, 1652 SPDK_COUNTOF(rpc_bdev_nvme_start_discovery_decoders), 1653 &ctx->req)) { 1654 SPDK_ERRLOG("spdk_json_decode_object failed\n"); 1655 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 1656 "spdk_json_decode_object failed"); 1657 goto cleanup; 1658 } 1659 1660 /* Parse trstring */ 1661 rc = spdk_nvme_transport_id_populate_trstring(&trid, ctx->req.trtype); 1662 if (rc < 0) { 1663 SPDK_ERRLOG("Failed to parse trtype: %s\n", ctx->req.trtype); 1664 spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "Failed to parse trtype: %s", 1665 ctx->req.trtype); 1666 goto cleanup; 1667 } 1668 1669 /* Parse trtype */ 1670 rc = spdk_nvme_transport_id_parse_trtype(&trid.trtype, ctx->req.trtype); 1671 assert(rc == 0); 1672 1673 /* Parse traddr */ 1674 maxlen = sizeof(trid.traddr); 1675 len = strnlen(ctx->req.traddr, maxlen); 1676 if (len == maxlen) { 1677 spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "traddr too long: %s", 1678 ctx->req.traddr); 1679 goto cleanup; 1680 } 1681 memcpy(trid.traddr, ctx->req.traddr, len + 1); 1682 1683 /* Parse adrfam */ 1684 if (ctx->req.adrfam) { 1685 rc = spdk_nvme_transport_id_parse_adrfam(&trid.adrfam, ctx->req.adrfam); 1686 if (rc < 0) { 1687 SPDK_ERRLOG("Failed to parse adrfam: %s\n", ctx->req.adrfam); 1688 spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "Failed to parse adrfam: %s", 1689 ctx->req.adrfam); 1690 goto cleanup; 1691 } 1692 } 1693 1694 /* Parse trsvcid */ 1695 if (ctx->req.trsvcid) { 1696 maxlen = sizeof(trid.trsvcid); 1697 len = strnlen(ctx->req.trsvcid, maxlen); 1698 if (len == maxlen) { 1699 spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "trsvcid too long: %s", 1700 ctx->req.trsvcid); 1701 goto cleanup; 1702 } 1703 memcpy(trid.trsvcid, ctx->req.trsvcid, len + 1); 1704 } 1705 1706 if (ctx->req.hostnqn) { 1707 snprintf(ctx->req.opts.hostnqn, sizeof(ctx->req.opts.hostnqn), "%s", 1708 ctx->req.hostnqn); 1709 } 1710 1711 ctx->request = request; 1712 rc = bdev_nvme_start_discovery(&trid, ctx->req.name, &ctx->req.opts, 1713 rpc_bdev_nvme_start_discovery_done, ctx); 1714 if (rc) { 1715 spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc)); 1716 goto cleanup; 1717 } 1718 1719 return; 1720 1721 cleanup: 1722 free_rpc_bdev_nvme_start_discovery(&ctx->req); 1723 free(ctx); 1724 } 1725 SPDK_RPC_REGISTER("bdev_nvme_start_discovery", rpc_bdev_nvme_start_discovery, 1726 SPDK_RPC_RUNTIME) 1727 1728 struct rpc_bdev_nvme_stop_discovery { 1729 char *name; 1730 }; 1731 1732 static const struct spdk_json_object_decoder rpc_bdev_nvme_stop_discovery_decoders[] = { 1733 {"name", offsetof(struct rpc_bdev_nvme_stop_discovery, name), spdk_json_decode_string}, 1734 }; 1735 1736 struct rpc_bdev_nvme_stop_discovery_ctx { 1737 struct rpc_bdev_nvme_stop_discovery req; 1738 struct spdk_jsonrpc_request *request; 1739 }; 1740 1741 static void 1742 rpc_bdev_nvme_stop_discovery_done(void *cb_ctx) 1743 { 1744 struct rpc_bdev_nvme_stop_discovery_ctx *ctx = cb_ctx; 1745 1746 spdk_jsonrpc_send_bool_response(ctx->request, true); 1747 free(ctx->req.name); 1748 free(ctx); 1749 } 1750 1751 static void 1752 rpc_bdev_nvme_stop_discovery(struct spdk_jsonrpc_request *request, 1753 const struct spdk_json_val *params) 1754 { 1755 struct rpc_bdev_nvme_stop_discovery_ctx *ctx; 1756 int rc; 1757 1758 ctx = calloc(1, sizeof(*ctx)); 1759 if (!ctx) { 1760 spdk_jsonrpc_send_error_response(request, -ENOMEM, spdk_strerror(ENOMEM)); 1761 return; 1762 } 1763 1764 if (spdk_json_decode_object(params, rpc_bdev_nvme_stop_discovery_decoders, 1765 SPDK_COUNTOF(rpc_bdev_nvme_stop_discovery_decoders), 1766 &ctx->req)) { 1767 SPDK_ERRLOG("spdk_json_decode_object failed\n"); 1768 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 1769 "spdk_json_decode_object failed"); 1770 goto cleanup; 1771 } 1772 1773 ctx->request = request; 1774 rc = bdev_nvme_stop_discovery(ctx->req.name, rpc_bdev_nvme_stop_discovery_done, ctx); 1775 if (rc) { 1776 spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc)); 1777 goto cleanup; 1778 } 1779 1780 return; 1781 1782 cleanup: 1783 free(ctx->req.name); 1784 free(ctx); 1785 } 1786 SPDK_RPC_REGISTER("bdev_nvme_stop_discovery", rpc_bdev_nvme_stop_discovery, 1787 SPDK_RPC_RUNTIME) 1788 1789 enum error_injection_cmd_type { 1790 NVME_ADMIN_CMD = 1, 1791 NVME_IO_CMD, 1792 }; 1793 1794 struct rpc_add_error_injection { 1795 char *name; 1796 enum error_injection_cmd_type cmd_type; 1797 uint8_t opc; 1798 bool do_not_submit; 1799 uint64_t timeout_in_us; 1800 uint32_t err_count; 1801 uint8_t sct; 1802 uint8_t sc; 1803 }; 1804 1805 static void 1806 free_rpc_add_error_injection(struct rpc_add_error_injection *req) 1807 { 1808 free(req->name); 1809 } 1810 1811 static int 1812 rpc_error_injection_decode_cmd_type(const struct spdk_json_val *val, void *out) 1813 { 1814 int *cmd_type = out; 1815 1816 if (spdk_json_strequal(val, "admin")) { 1817 *cmd_type = NVME_ADMIN_CMD; 1818 } else if (spdk_json_strequal(val, "io")) { 1819 *cmd_type = NVME_IO_CMD; 1820 } else { 1821 SPDK_ERRLOG("Invalid parameter value: cmd_type\n"); 1822 return -EINVAL; 1823 } 1824 1825 return 0; 1826 } 1827 1828 static const struct spdk_json_object_decoder rpc_add_error_injection_decoders[] = { 1829 { "name", offsetof(struct rpc_add_error_injection, name), spdk_json_decode_string }, 1830 { "cmd_type", offsetof(struct rpc_add_error_injection, cmd_type), rpc_error_injection_decode_cmd_type }, 1831 { "opc", offsetof(struct rpc_add_error_injection, opc), spdk_json_decode_uint8 }, 1832 { "do_not_submit", offsetof(struct rpc_add_error_injection, do_not_submit), spdk_json_decode_bool, true }, 1833 { "timeout_in_us", offsetof(struct rpc_add_error_injection, timeout_in_us), spdk_json_decode_uint64, true }, 1834 { "err_count", offsetof(struct rpc_add_error_injection, err_count), spdk_json_decode_uint32, true }, 1835 { "sct", offsetof(struct rpc_add_error_injection, sct), spdk_json_decode_uint8, true}, 1836 { "sc", offsetof(struct rpc_add_error_injection, sc), spdk_json_decode_uint8, true}, 1837 }; 1838 1839 struct rpc_add_error_injection_ctx { 1840 struct spdk_jsonrpc_request *request; 1841 struct rpc_add_error_injection rpc; 1842 }; 1843 1844 static void 1845 rpc_add_error_injection_done(struct spdk_io_channel_iter *i, int status) 1846 { 1847 struct rpc_add_error_injection_ctx *ctx = spdk_io_channel_iter_get_ctx(i); 1848 1849 if (status) { 1850 spdk_jsonrpc_send_error_response(ctx->request, status, 1851 "Failed to add the error injection."); 1852 } else { 1853 spdk_jsonrpc_send_bool_response(ctx->request, true); 1854 } 1855 1856 free_rpc_add_error_injection(&ctx->rpc); 1857 free(ctx); 1858 } 1859 1860 static void 1861 rpc_add_error_injection_per_channel(struct spdk_io_channel_iter *i) 1862 { 1863 struct spdk_io_channel *ch = spdk_io_channel_iter_get_channel(i); 1864 struct rpc_add_error_injection_ctx *ctx = spdk_io_channel_iter_get_ctx(i); 1865 struct nvme_ctrlr_channel *ctrlr_ch = spdk_io_channel_get_ctx(ch); 1866 struct nvme_ctrlr *nvme_ctrlr = nvme_ctrlr_channel_get_ctrlr(ctrlr_ch); 1867 struct spdk_nvme_qpair *qpair = ctrlr_ch->qpair; 1868 struct spdk_nvme_ctrlr *ctrlr = nvme_ctrlr->ctrlr; 1869 int rc = 0; 1870 1871 if (qpair != NULL) { 1872 rc = spdk_nvme_qpair_add_cmd_error_injection(ctrlr, qpair, ctx->rpc.opc, 1873 ctx->rpc.do_not_submit, ctx->rpc.timeout_in_us, ctx->rpc.err_count, 1874 ctx->rpc.sct, ctx->rpc.sc); 1875 } 1876 1877 spdk_for_each_channel_continue(i, rc); 1878 } 1879 1880 static void 1881 rpc_bdev_nvme_add_error_injection( 1882 struct spdk_jsonrpc_request *request, 1883 const struct spdk_json_val *params) 1884 { 1885 struct rpc_add_error_injection_ctx *ctx; 1886 struct nvme_ctrlr *nvme_ctrlr; 1887 int rc; 1888 1889 ctx = calloc(1, sizeof(*ctx)); 1890 if (!ctx) { 1891 spdk_jsonrpc_send_error_response(request, -ENOMEM, spdk_strerror(ENOMEM)); 1892 return; 1893 } 1894 ctx->rpc.err_count = 1; 1895 ctx->request = request; 1896 1897 if (spdk_json_decode_object(params, 1898 rpc_add_error_injection_decoders, 1899 SPDK_COUNTOF(rpc_add_error_injection_decoders), 1900 &ctx->rpc)) { 1901 spdk_jsonrpc_send_error_response(request, -EINVAL, 1902 "Failed to parse the request"); 1903 goto cleanup; 1904 } 1905 1906 nvme_ctrlr = nvme_ctrlr_get_by_name(ctx->rpc.name); 1907 if (nvme_ctrlr == NULL) { 1908 SPDK_ERRLOG("No controller with specified name was found.\n"); 1909 spdk_jsonrpc_send_error_response(request, -ENODEV, spdk_strerror(ENODEV)); 1910 goto cleanup; 1911 } 1912 1913 if (ctx->rpc.cmd_type == NVME_IO_CMD) { 1914 spdk_for_each_channel(nvme_ctrlr, 1915 rpc_add_error_injection_per_channel, 1916 ctx, 1917 rpc_add_error_injection_done); 1918 1919 return; 1920 } else { 1921 rc = spdk_nvme_qpair_add_cmd_error_injection(nvme_ctrlr->ctrlr, NULL, ctx->rpc.opc, 1922 ctx->rpc.do_not_submit, ctx->rpc.timeout_in_us, ctx->rpc.err_count, 1923 ctx->rpc.sct, ctx->rpc.sc); 1924 if (rc) { 1925 spdk_jsonrpc_send_error_response(request, -rc, 1926 "Failed to add the error injection"); 1927 } else { 1928 spdk_jsonrpc_send_bool_response(ctx->request, true); 1929 } 1930 } 1931 1932 cleanup: 1933 free_rpc_add_error_injection(&ctx->rpc); 1934 free(ctx); 1935 } 1936 SPDK_RPC_REGISTER("bdev_nvme_add_error_injection", rpc_bdev_nvme_add_error_injection, 1937 SPDK_RPC_RUNTIME) 1938 1939 struct rpc_remove_error_injection { 1940 char *name; 1941 enum error_injection_cmd_type cmd_type; 1942 uint8_t opc; 1943 }; 1944 1945 static void 1946 free_rpc_remove_error_injection(struct rpc_remove_error_injection *req) 1947 { 1948 free(req->name); 1949 } 1950 1951 static const struct spdk_json_object_decoder rpc_remove_error_injection_decoders[] = { 1952 { "name", offsetof(struct rpc_remove_error_injection, name), spdk_json_decode_string }, 1953 { "cmd_type", offsetof(struct rpc_remove_error_injection, cmd_type), rpc_error_injection_decode_cmd_type }, 1954 { "opc", offsetof(struct rpc_remove_error_injection, opc), spdk_json_decode_uint8 }, 1955 }; 1956 1957 struct rpc_remove_error_injection_ctx { 1958 struct spdk_jsonrpc_request *request; 1959 struct rpc_remove_error_injection rpc; 1960 }; 1961 1962 static void 1963 rpc_remove_error_injection_done(struct spdk_io_channel_iter *i, int status) 1964 { 1965 struct rpc_remove_error_injection_ctx *ctx = spdk_io_channel_iter_get_ctx(i); 1966 1967 if (status) { 1968 spdk_jsonrpc_send_error_response(ctx->request, status, 1969 "Failed to remove the error injection."); 1970 } else { 1971 spdk_jsonrpc_send_bool_response(ctx->request, true); 1972 } 1973 1974 free_rpc_remove_error_injection(&ctx->rpc); 1975 free(ctx); 1976 } 1977 1978 static void 1979 rpc_remove_error_injection_per_channel(struct spdk_io_channel_iter *i) 1980 { 1981 struct spdk_io_channel *ch = spdk_io_channel_iter_get_channel(i); 1982 struct rpc_remove_error_injection_ctx *ctx = spdk_io_channel_iter_get_ctx(i); 1983 struct nvme_ctrlr_channel *ctrlr_ch = spdk_io_channel_get_ctx(ch); 1984 struct nvme_ctrlr *nvme_ctrlr = nvme_ctrlr_channel_get_ctrlr(ctrlr_ch); 1985 struct spdk_nvme_qpair *qpair = ctrlr_ch->qpair; 1986 struct spdk_nvme_ctrlr *ctrlr = nvme_ctrlr->ctrlr; 1987 1988 if (ctrlr_ch->qpair != NULL) { 1989 spdk_nvme_qpair_remove_cmd_error_injection(ctrlr, qpair, ctx->rpc.opc); 1990 } 1991 1992 spdk_for_each_channel_continue(i, 0); 1993 } 1994 1995 static void 1996 rpc_bdev_nvme_remove_error_injection(struct spdk_jsonrpc_request *request, 1997 const struct spdk_json_val *params) 1998 { 1999 struct rpc_remove_error_injection_ctx *ctx; 2000 struct nvme_ctrlr *nvme_ctrlr; 2001 2002 ctx = calloc(1, sizeof(*ctx)); 2003 if (!ctx) { 2004 spdk_jsonrpc_send_error_response(request, -ENOMEM, spdk_strerror(ENOMEM)); 2005 return; 2006 } 2007 ctx->request = request; 2008 2009 if (spdk_json_decode_object(params, 2010 rpc_remove_error_injection_decoders, 2011 SPDK_COUNTOF(rpc_remove_error_injection_decoders), 2012 &ctx->rpc)) { 2013 spdk_jsonrpc_send_error_response(request, -EINVAL, 2014 "Failed to parse the request"); 2015 goto cleanup; 2016 } 2017 2018 nvme_ctrlr = nvme_ctrlr_get_by_name(ctx->rpc.name); 2019 if (nvme_ctrlr == NULL) { 2020 SPDK_ERRLOG("No controller with specified name was found.\n"); 2021 spdk_jsonrpc_send_error_response(request, -ENODEV, spdk_strerror(ENODEV)); 2022 goto cleanup; 2023 } 2024 2025 if (ctx->rpc.cmd_type == NVME_IO_CMD) { 2026 spdk_for_each_channel(nvme_ctrlr, 2027 rpc_remove_error_injection_per_channel, 2028 ctx, 2029 rpc_remove_error_injection_done); 2030 return; 2031 } else { 2032 spdk_nvme_qpair_remove_cmd_error_injection(nvme_ctrlr->ctrlr, NULL, ctx->rpc.opc); 2033 spdk_jsonrpc_send_bool_response(ctx->request, true); 2034 } 2035 2036 cleanup: 2037 free_rpc_remove_error_injection(&ctx->rpc); 2038 free(ctx); 2039 } 2040 SPDK_RPC_REGISTER("bdev_nvme_remove_error_injection", rpc_bdev_nvme_remove_error_injection, 2041 SPDK_RPC_RUNTIME) 2042