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 spdk_bdev_free_io(bdev_io); 905 906 if (!success) { 907 spdk_jsonrpc_send_error_response(firm_ctx->request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 908 "firmware download failed ."); 909 apply_firmware_cleanup(firm_ctx); 910 return; 911 } 912 913 firm_ctx->p += firm_ctx->transfer; 914 firm_ctx->offset += firm_ctx->transfer; 915 firm_ctx->size_remaining -= firm_ctx->transfer; 916 917 switch (firm_ctx->size_remaining) { 918 case 0: 919 /* firmware download completed. Commit firmware */ 920 memset(&fw_commit, 0, sizeof(struct spdk_nvme_fw_commit)); 921 fw_commit.fs = slot; 922 fw_commit.ca = commit_action; 923 924 cmd.opc = SPDK_NVME_OPC_FIRMWARE_COMMIT; 925 memcpy(&cmd.cdw10, &fw_commit, sizeof(uint32_t)); 926 rc = spdk_bdev_nvme_admin_passthru(firm_ctx->desc, firm_ctx->ch, &cmd, NULL, 0, 927 apply_firmware_complete_reset, firm_ctx); 928 if (rc) { 929 spdk_jsonrpc_send_error_response(firm_ctx->request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 930 "firmware commit failed."); 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 apply_firmware_cleanup(firm_ctx); 947 return; 948 } 949 break; 950 } 951 } 952 953 static void 954 apply_firmware_open_cb(enum spdk_bdev_event_type type, struct spdk_bdev *bdev, void *event_ctx) 955 { 956 } 957 958 static void 959 rpc_bdev_nvme_apply_firmware(struct spdk_jsonrpc_request *request, 960 const struct spdk_json_val *params) 961 { 962 int rc; 963 int fd = -1; 964 struct stat fw_stat; 965 struct spdk_nvme_ctrlr *ctrlr; 966 char msg[1024]; 967 struct spdk_bdev *bdev; 968 struct spdk_bdev *bdev2; 969 struct open_descriptors *opt; 970 struct spdk_bdev_desc *desc; 971 struct spdk_nvme_cmd *cmd; 972 struct firmware_update_info *firm_ctx; 973 974 firm_ctx = calloc(1, sizeof(struct firmware_update_info)); 975 if (!firm_ctx) { 976 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 977 "Memory allocation error."); 978 return; 979 } 980 firm_ctx->fw_image = NULL; 981 TAILQ_INIT(&firm_ctx->desc_head); 982 firm_ctx->request = request; 983 984 firm_ctx->req = calloc(1, sizeof(struct rpc_apply_firmware)); 985 if (!firm_ctx->req) { 986 snprintf(msg, sizeof(msg), "Memory allocation error."); 987 goto err; 988 } 989 990 if (spdk_json_decode_object(params, rpc_apply_firmware_decoders, 991 SPDK_COUNTOF(rpc_apply_firmware_decoders), firm_ctx->req)) { 992 snprintf(msg, sizeof(msg), "spdk_json_decode_object failed."); 993 goto err; 994 } 995 996 if ((bdev = spdk_bdev_get_by_name(firm_ctx->req->bdev_name)) == NULL) { 997 snprintf(msg, sizeof(msg), "bdev %s were not found", firm_ctx->req->bdev_name); 998 goto err; 999 } 1000 1001 if ((ctrlr = bdev_nvme_get_ctrlr(bdev)) == NULL) { 1002 snprintf(msg, sizeof(msg), "Controller information for %s were not found.", 1003 firm_ctx->req->bdev_name); 1004 goto err; 1005 } 1006 firm_ctx->ctrlr = ctrlr; 1007 1008 for (bdev2 = spdk_bdev_first(); bdev2; bdev2 = spdk_bdev_next(bdev2)) { 1009 1010 if (bdev_nvme_get_ctrlr(bdev2) != ctrlr) { 1011 continue; 1012 } 1013 1014 if (!(opt = malloc(sizeof(struct open_descriptors)))) { 1015 snprintf(msg, sizeof(msg), "Memory allocation error."); 1016 goto err; 1017 } 1018 1019 if (spdk_bdev_open_ext(spdk_bdev_get_name(bdev2), true, apply_firmware_open_cb, NULL, &desc) != 0) { 1020 snprintf(msg, sizeof(msg), "Device %s is in use.", firm_ctx->req->bdev_name); 1021 free(opt); 1022 goto err; 1023 } 1024 1025 /* Save the thread where the base device is opened */ 1026 opt->thread = spdk_get_thread(); 1027 1028 opt->desc = desc; 1029 opt->bdev = bdev; 1030 TAILQ_INSERT_TAIL(&firm_ctx->desc_head, opt, tqlst); 1031 } 1032 1033 /* 1034 * find a descriptor associated with our bdev 1035 */ 1036 firm_ctx->desc = NULL; 1037 TAILQ_FOREACH(opt, &firm_ctx->desc_head, tqlst) { 1038 if (opt->bdev == bdev) { 1039 firm_ctx->desc = opt->desc; 1040 break; 1041 } 1042 } 1043 1044 if (!firm_ctx->desc) { 1045 snprintf(msg, sizeof(msg), "No descriptor were found."); 1046 goto err; 1047 } 1048 1049 firm_ctx->ch = spdk_bdev_get_io_channel(firm_ctx->desc); 1050 if (!firm_ctx->ch) { 1051 snprintf(msg, sizeof(msg), "No channels were found."); 1052 goto err; 1053 } 1054 1055 fd = open(firm_ctx->req->filename, O_RDONLY); 1056 if (fd < 0) { 1057 snprintf(msg, sizeof(msg), "open file failed."); 1058 goto err; 1059 } 1060 1061 rc = fstat(fd, &fw_stat); 1062 if (rc < 0) { 1063 close(fd); 1064 snprintf(msg, sizeof(msg), "fstat failed."); 1065 goto err; 1066 } 1067 1068 firm_ctx->size = fw_stat.st_size; 1069 if (fw_stat.st_size % 4) { 1070 close(fd); 1071 snprintf(msg, sizeof(msg), "Firmware image size is not multiple of 4."); 1072 goto err; 1073 } 1074 1075 firm_ctx->fw_image = spdk_zmalloc(firm_ctx->size, 4096, NULL, 1076 SPDK_ENV_LCORE_ID_ANY, SPDK_MALLOC_DMA); 1077 if (!firm_ctx->fw_image) { 1078 close(fd); 1079 snprintf(msg, sizeof(msg), "Memory allocation error."); 1080 goto err; 1081 } 1082 firm_ctx->p = firm_ctx->fw_image; 1083 1084 if (read(fd, firm_ctx->p, firm_ctx->size) != ((ssize_t)(firm_ctx->size))) { 1085 close(fd); 1086 snprintf(msg, sizeof(msg), "Read firmware image failed!"); 1087 goto err; 1088 } 1089 close(fd); 1090 1091 firm_ctx->offset = 0; 1092 firm_ctx->size_remaining = firm_ctx->size; 1093 firm_ctx->transfer = spdk_min(firm_ctx->size_remaining, 4096); 1094 1095 cmd = malloc(sizeof(struct spdk_nvme_cmd)); 1096 if (!cmd) { 1097 snprintf(msg, sizeof(msg), "Memory allocation error."); 1098 goto err; 1099 } 1100 memset(cmd, 0, sizeof(struct spdk_nvme_cmd)); 1101 cmd->opc = SPDK_NVME_OPC_FIRMWARE_IMAGE_DOWNLOAD; 1102 1103 cmd->cdw10 = spdk_nvme_bytes_to_numd(firm_ctx->transfer); 1104 cmd->cdw11 = firm_ctx->offset >> 2; 1105 1106 rc = spdk_bdev_nvme_admin_passthru(firm_ctx->desc, firm_ctx->ch, cmd, firm_ctx->p, 1107 firm_ctx->transfer, apply_firmware_complete, firm_ctx); 1108 if (rc == 0) { 1109 /* normal return here. */ 1110 return; 1111 } 1112 1113 free(cmd); 1114 snprintf(msg, sizeof(msg), "Read firmware image failed!"); 1115 err: 1116 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, msg); 1117 apply_firmware_cleanup(firm_ctx); 1118 } 1119 SPDK_RPC_REGISTER("bdev_nvme_apply_firmware", rpc_bdev_nvme_apply_firmware, SPDK_RPC_RUNTIME) 1120 SPDK_RPC_REGISTER_ALIAS_DEPRECATED(bdev_nvme_apply_firmware, apply_nvme_firmware) 1121 1122 struct rpc_bdev_nvme_transport_stat_ctx { 1123 struct spdk_jsonrpc_request *request; 1124 struct spdk_json_write_ctx *w; 1125 }; 1126 1127 static void 1128 rpc_bdev_nvme_rdma_stats(struct spdk_json_write_ctx *w, 1129 struct spdk_nvme_transport_poll_group_stat *stat) 1130 { 1131 struct spdk_nvme_rdma_device_stat *device_stats; 1132 uint32_t i; 1133 1134 spdk_json_write_named_array_begin(w, "devices"); 1135 1136 for (i = 0; i < stat->rdma.num_devices; i++) { 1137 device_stats = &stat->rdma.device_stats[i]; 1138 spdk_json_write_object_begin(w); 1139 spdk_json_write_named_string(w, "dev_name", device_stats->name); 1140 spdk_json_write_named_uint64(w, "polls", device_stats->polls); 1141 spdk_json_write_named_uint64(w, "idle_polls", device_stats->idle_polls); 1142 spdk_json_write_named_uint64(w, "completions", device_stats->completions); 1143 spdk_json_write_named_uint64(w, "queued_requests", device_stats->queued_requests); 1144 spdk_json_write_named_uint64(w, "total_send_wrs", device_stats->total_send_wrs); 1145 spdk_json_write_named_uint64(w, "send_doorbell_updates", device_stats->send_doorbell_updates); 1146 spdk_json_write_named_uint64(w, "total_recv_wrs", device_stats->total_recv_wrs); 1147 spdk_json_write_named_uint64(w, "recv_doorbell_updates", device_stats->recv_doorbell_updates); 1148 spdk_json_write_object_end(w); 1149 } 1150 spdk_json_write_array_end(w); 1151 } 1152 1153 static void 1154 rpc_bdev_nvme_pcie_stats(struct spdk_json_write_ctx *w, 1155 struct spdk_nvme_transport_poll_group_stat *stat) 1156 { 1157 spdk_json_write_named_uint64(w, "polls", stat->pcie.polls); 1158 spdk_json_write_named_uint64(w, "idle_polls", stat->pcie.idle_polls); 1159 spdk_json_write_named_uint64(w, "completions", stat->pcie.completions); 1160 spdk_json_write_named_uint64(w, "cq_doorbell_updates", stat->pcie.cq_doorbell_updates); 1161 spdk_json_write_named_uint64(w, "queued_requests", stat->pcie.queued_requests); 1162 spdk_json_write_named_uint64(w, "submitted_requests", stat->pcie.submitted_requests); 1163 spdk_json_write_named_uint64(w, "sq_doobell_updates", stat->pcie.sq_doobell_updates); 1164 } 1165 1166 static void 1167 rpc_bdev_nvme_tcp_stats(struct spdk_json_write_ctx *w, 1168 struct spdk_nvme_transport_poll_group_stat *stat) 1169 { 1170 spdk_json_write_named_uint64(w, "polls", stat->tcp.polls); 1171 spdk_json_write_named_uint64(w, "idle_polls", stat->tcp.idle_polls); 1172 spdk_json_write_named_uint64(w, "socket_completions", stat->tcp.socket_completions); 1173 spdk_json_write_named_uint64(w, "nvme_completions", stat->tcp.nvme_completions); 1174 spdk_json_write_named_uint64(w, "queued_requests", stat->tcp.queued_requests); 1175 spdk_json_write_named_uint64(w, "submitted_requests", stat->tcp.submitted_requests); 1176 } 1177 1178 static void 1179 rpc_bdev_nvme_stats_per_channel(struct spdk_io_channel_iter *i) 1180 { 1181 struct rpc_bdev_nvme_transport_stat_ctx *ctx; 1182 struct spdk_io_channel *ch; 1183 struct nvme_poll_group *group; 1184 struct spdk_nvme_poll_group_stat *stat; 1185 struct spdk_nvme_transport_poll_group_stat *tr_stat; 1186 uint32_t j; 1187 int rc; 1188 1189 ctx = spdk_io_channel_iter_get_ctx(i); 1190 ch = spdk_io_channel_iter_get_channel(i); 1191 group = spdk_io_channel_get_ctx(ch); 1192 1193 rc = spdk_nvme_poll_group_get_stats(group->group, &stat); 1194 if (rc) { 1195 spdk_for_each_channel_continue(i, rc); 1196 return; 1197 } 1198 1199 spdk_json_write_object_begin(ctx->w); 1200 spdk_json_write_named_string(ctx->w, "thread", spdk_thread_get_name(spdk_get_thread())); 1201 spdk_json_write_named_array_begin(ctx->w, "transports"); 1202 1203 for (j = 0; j < stat->num_transports; j++) { 1204 tr_stat = stat->transport_stat[j]; 1205 spdk_json_write_object_begin(ctx->w); 1206 spdk_json_write_named_string(ctx->w, "trname", spdk_nvme_transport_id_trtype_str(tr_stat->trtype)); 1207 1208 switch (stat->transport_stat[j]->trtype) { 1209 case SPDK_NVME_TRANSPORT_RDMA: 1210 rpc_bdev_nvme_rdma_stats(ctx->w, tr_stat); 1211 break; 1212 case SPDK_NVME_TRANSPORT_PCIE: 1213 rpc_bdev_nvme_pcie_stats(ctx->w, tr_stat); 1214 break; 1215 case SPDK_NVME_TRANSPORT_TCP: 1216 rpc_bdev_nvme_tcp_stats(ctx->w, tr_stat); 1217 break; 1218 default: 1219 SPDK_WARNLOG("Can't handle trtype %d %s\n", tr_stat->trtype, 1220 spdk_nvme_transport_id_trtype_str(tr_stat->trtype)); 1221 } 1222 spdk_json_write_object_end(ctx->w); 1223 } 1224 /* transports array */ 1225 spdk_json_write_array_end(ctx->w); 1226 spdk_json_write_object_end(ctx->w); 1227 1228 spdk_nvme_poll_group_free_stats(group->group, stat); 1229 spdk_for_each_channel_continue(i, 0); 1230 } 1231 1232 static void 1233 rpc_bdev_nvme_stats_done(struct spdk_io_channel_iter *i, int status) 1234 { 1235 struct rpc_bdev_nvme_transport_stat_ctx *ctx = spdk_io_channel_iter_get_ctx(i); 1236 1237 spdk_json_write_array_end(ctx->w); 1238 spdk_json_write_object_end(ctx->w); 1239 spdk_jsonrpc_end_result(ctx->request, ctx->w); 1240 free(ctx); 1241 } 1242 1243 static void 1244 rpc_bdev_nvme_get_transport_statistics(struct spdk_jsonrpc_request *request, 1245 const struct spdk_json_val *params) 1246 { 1247 struct rpc_bdev_nvme_transport_stat_ctx *ctx; 1248 1249 if (params) { 1250 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, 1251 "'bdev_nvme_get_transport_statistics' requires no arguments"); 1252 return; 1253 } 1254 1255 ctx = calloc(1, sizeof(*ctx)); 1256 if (!ctx) { 1257 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 1258 "Memory allocation error"); 1259 return; 1260 } 1261 ctx->request = request; 1262 ctx->w = spdk_jsonrpc_begin_result(ctx->request); 1263 spdk_json_write_object_begin(ctx->w); 1264 spdk_json_write_named_array_begin(ctx->w, "poll_groups"); 1265 1266 spdk_for_each_channel(&g_nvme_bdev_ctrlrs, 1267 rpc_bdev_nvme_stats_per_channel, 1268 ctx, 1269 rpc_bdev_nvme_stats_done); 1270 } 1271 SPDK_RPC_REGISTER("bdev_nvme_get_transport_statistics", rpc_bdev_nvme_get_transport_statistics, 1272 SPDK_RPC_RUNTIME) 1273 1274 struct rpc_bdev_nvme_reset_controller_req { 1275 char *name; 1276 }; 1277 1278 static void 1279 free_rpc_bdev_nvme_reset_controller_req(struct rpc_bdev_nvme_reset_controller_req *r) 1280 { 1281 free(r->name); 1282 } 1283 1284 static const struct spdk_json_object_decoder rpc_bdev_nvme_reset_controller_req_decoders[] = { 1285 {"name", offsetof(struct rpc_bdev_nvme_reset_controller_req, name), spdk_json_decode_string}, 1286 }; 1287 1288 struct rpc_bdev_nvme_reset_controller_ctx { 1289 struct spdk_jsonrpc_request *request; 1290 bool success; 1291 struct spdk_thread *orig_thread; 1292 }; 1293 1294 static void 1295 _rpc_bdev_nvme_reset_controller_cb(void *_ctx) 1296 { 1297 struct rpc_bdev_nvme_reset_controller_ctx *ctx = _ctx; 1298 1299 spdk_jsonrpc_send_bool_response(ctx->request, ctx->success); 1300 1301 free(ctx); 1302 } 1303 1304 static void 1305 rpc_bdev_nvme_reset_controller_cb(void *cb_arg, bool success) 1306 { 1307 struct rpc_bdev_nvme_reset_controller_ctx *ctx = cb_arg; 1308 1309 ctx->success = success; 1310 1311 spdk_thread_send_msg(ctx->orig_thread, _rpc_bdev_nvme_reset_controller_cb, ctx); 1312 } 1313 1314 static void 1315 rpc_bdev_nvme_reset_controller(struct spdk_jsonrpc_request *request, 1316 const struct spdk_json_val *params) 1317 { 1318 struct rpc_bdev_nvme_reset_controller_req req = {NULL}; 1319 struct rpc_bdev_nvme_reset_controller_ctx *ctx; 1320 struct nvme_ctrlr *nvme_ctrlr; 1321 int rc; 1322 1323 ctx = calloc(1, sizeof(*ctx)); 1324 if (ctx == NULL) { 1325 SPDK_ERRLOG("Memory allocation failed\n"); 1326 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 1327 "Memory allocation failed"); 1328 return; 1329 } 1330 1331 if (spdk_json_decode_object(params, rpc_bdev_nvme_reset_controller_req_decoders, 1332 SPDK_COUNTOF(rpc_bdev_nvme_reset_controller_req_decoders), 1333 &req)) { 1334 SPDK_ERRLOG("spdk_json_decode_object failed\n"); 1335 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, spdk_strerror(EINVAL)); 1336 goto err; 1337 } 1338 1339 nvme_ctrlr = nvme_ctrlr_get_by_name(req.name); 1340 if (nvme_ctrlr == NULL) { 1341 SPDK_ERRLOG("Failed at device lookup\n"); 1342 spdk_jsonrpc_send_error_response(request, -ENODEV, spdk_strerror(ENODEV)); 1343 goto err; 1344 } 1345 1346 ctx->request = request; 1347 ctx->orig_thread = spdk_get_thread(); 1348 1349 rc = bdev_nvme_reset_rpc(nvme_ctrlr, rpc_bdev_nvme_reset_controller_cb, ctx); 1350 if (rc != 0) { 1351 SPDK_NOTICELOG("Failed at bdev_nvme_reset_rpc\n"); 1352 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, spdk_strerror(-rc)); 1353 goto err; 1354 } 1355 1356 free_rpc_bdev_nvme_reset_controller_req(&req); 1357 return; 1358 1359 err: 1360 free_rpc_bdev_nvme_reset_controller_req(&req); 1361 free(ctx); 1362 } 1363 SPDK_RPC_REGISTER("bdev_nvme_reset_controller", rpc_bdev_nvme_reset_controller, SPDK_RPC_RUNTIME) 1364 1365 struct rpc_get_controller_health_info { 1366 char *name; 1367 }; 1368 1369 struct spdk_nvme_health_info_context { 1370 struct spdk_jsonrpc_request *request; 1371 struct spdk_nvme_ctrlr *ctrlr; 1372 struct spdk_nvme_health_information_page health_page; 1373 }; 1374 1375 static void 1376 free_rpc_get_controller_health_info(struct rpc_get_controller_health_info *r) 1377 { 1378 free(r->name); 1379 } 1380 1381 static const struct spdk_json_object_decoder rpc_get_controller_health_info_decoders[] = { 1382 {"name", offsetof(struct rpc_get_controller_health_info, name), spdk_json_decode_string, true}, 1383 }; 1384 1385 static void nvme_health_info_cleanup(struct spdk_nvme_health_info_context *context, bool response) 1386 { 1387 if (response == true) { 1388 spdk_jsonrpc_send_error_response(context->request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 1389 "Internal error."); 1390 } 1391 1392 free(context); 1393 } 1394 1395 static void 1396 get_health_log_page_completion(void *cb_arg, const struct spdk_nvme_cpl *cpl) 1397 { 1398 int i; 1399 char buf[128]; 1400 struct spdk_nvme_health_info_context *context = cb_arg; 1401 struct spdk_jsonrpc_request *request = context->request; 1402 struct spdk_json_write_ctx *w; 1403 struct spdk_nvme_ctrlr *ctrlr = context->ctrlr; 1404 const struct spdk_nvme_transport_id *trid = NULL; 1405 const struct spdk_nvme_ctrlr_data *cdata = NULL; 1406 struct spdk_nvme_health_information_page *health_page = NULL; 1407 1408 if (spdk_nvme_cpl_is_error(cpl)) { 1409 nvme_health_info_cleanup(context, true); 1410 SPDK_ERRLOG("get log page failed\n"); 1411 return; 1412 } 1413 1414 if (ctrlr == NULL) { 1415 nvme_health_info_cleanup(context, true); 1416 SPDK_ERRLOG("ctrlr is NULL\n"); 1417 return; 1418 } else { 1419 trid = spdk_nvme_ctrlr_get_transport_id(ctrlr); 1420 cdata = spdk_nvme_ctrlr_get_data(ctrlr); 1421 health_page = &(context->health_page); 1422 } 1423 1424 w = spdk_jsonrpc_begin_result(request); 1425 1426 spdk_json_write_object_begin(w); 1427 snprintf(buf, sizeof(cdata->mn) + 1, "%s", cdata->mn); 1428 spdk_str_trim(buf); 1429 spdk_json_write_named_string(w, "model_number", buf); 1430 snprintf(buf, sizeof(cdata->sn) + 1, "%s", cdata->sn); 1431 spdk_str_trim(buf); 1432 spdk_json_write_named_string(w, "serial_number", buf); 1433 snprintf(buf, sizeof(cdata->fr) + 1, "%s", cdata->fr); 1434 spdk_str_trim(buf); 1435 spdk_json_write_named_string(w, "firmware_revision", buf); 1436 spdk_json_write_named_string(w, "traddr", trid->traddr); 1437 spdk_json_write_named_uint64(w, "temperature_celsius", health_page->temperature - 273); 1438 spdk_json_write_named_uint64(w, "available_spare_percentage", health_page->available_spare); 1439 spdk_json_write_named_uint64(w, "available_spare_threshold_percentage", 1440 health_page->available_spare_threshold); 1441 spdk_json_write_named_uint64(w, "percentage_used", health_page->percentage_used); 1442 spdk_json_write_named_uint128(w, "data_units_read", 1443 health_page->data_units_read[0], health_page->data_units_read[1]); 1444 spdk_json_write_named_uint128(w, "data_units_written", 1445 health_page->data_units_written[0], health_page->data_units_written[1]); 1446 spdk_json_write_named_uint128(w, "host_read_commands", 1447 health_page->host_read_commands[0], health_page->host_read_commands[1]); 1448 spdk_json_write_named_uint128(w, "host_write_commands", 1449 health_page->host_write_commands[0], health_page->host_write_commands[1]); 1450 spdk_json_write_named_uint128(w, "controller_busy_time", 1451 health_page->controller_busy_time[0], health_page->controller_busy_time[1]); 1452 spdk_json_write_named_uint128(w, "power_cycles", 1453 health_page->power_cycles[0], health_page->power_cycles[1]); 1454 spdk_json_write_named_uint128(w, "power_on_hours", 1455 health_page->power_on_hours[0], health_page->power_on_hours[1]); 1456 spdk_json_write_named_uint128(w, "unsafe_shutdowns", 1457 health_page->unsafe_shutdowns[0], health_page->unsafe_shutdowns[1]); 1458 spdk_json_write_named_uint128(w, "media_errors", 1459 health_page->media_errors[0], health_page->media_errors[1]); 1460 spdk_json_write_named_uint128(w, "num_err_log_entries", 1461 health_page->num_error_info_log_entries[0], health_page->num_error_info_log_entries[1]); 1462 spdk_json_write_named_uint64(w, "warning_temperature_time_minutes", health_page->warning_temp_time); 1463 spdk_json_write_named_uint64(w, "critical_composite_temperature_time_minutes", 1464 health_page->critical_temp_time); 1465 for (i = 0; i < 8; i++) { 1466 if (health_page->temp_sensor[i] != 0) { 1467 spdk_json_write_named_uint64(w, "temperature_sensor_celsius", health_page->temp_sensor[i] - 273); 1468 } 1469 } 1470 spdk_json_write_object_end(w); 1471 1472 spdk_jsonrpc_end_result(request, w); 1473 nvme_health_info_cleanup(context, false); 1474 } 1475 1476 static void 1477 get_health_log_page(struct spdk_nvme_health_info_context *context) 1478 { 1479 struct spdk_nvme_ctrlr *ctrlr = context->ctrlr; 1480 1481 if (spdk_nvme_ctrlr_cmd_get_log_page(ctrlr, SPDK_NVME_LOG_HEALTH_INFORMATION, 1482 SPDK_NVME_GLOBAL_NS_TAG, 1483 &(context->health_page), sizeof(context->health_page), 0, 1484 get_health_log_page_completion, context)) { 1485 nvme_health_info_cleanup(context, true); 1486 SPDK_ERRLOG("spdk_nvme_ctrlr_cmd_get_log_page() failed\n"); 1487 } 1488 } 1489 1490 static void 1491 get_temperature_threshold_feature_completion(void *cb_arg, const struct spdk_nvme_cpl *cpl) 1492 { 1493 struct spdk_nvme_health_info_context *context = cb_arg; 1494 1495 if (spdk_nvme_cpl_is_error(cpl)) { 1496 nvme_health_info_cleanup(context, true); 1497 SPDK_ERRLOG("feature SPDK_NVME_FEAT_TEMPERATURE_THRESHOLD failed in completion\n"); 1498 } else { 1499 get_health_log_page(context); 1500 } 1501 } 1502 1503 static int 1504 get_temperature_threshold_feature(struct spdk_nvme_health_info_context *context) 1505 { 1506 struct spdk_nvme_cmd cmd = {}; 1507 1508 cmd.opc = SPDK_NVME_OPC_GET_FEATURES; 1509 cmd.cdw10 = SPDK_NVME_FEAT_TEMPERATURE_THRESHOLD; 1510 1511 return spdk_nvme_ctrlr_cmd_admin_raw(context->ctrlr, &cmd, NULL, 0, 1512 get_temperature_threshold_feature_completion, context); 1513 } 1514 1515 static void 1516 get_controller_health_info(struct spdk_jsonrpc_request *request, struct spdk_nvme_ctrlr *ctrlr) 1517 { 1518 struct spdk_nvme_health_info_context *context; 1519 1520 context = calloc(1, sizeof(struct spdk_nvme_health_info_context)); 1521 if (!context) { 1522 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 1523 "Memory allocation error."); 1524 return; 1525 } 1526 1527 context->request = request; 1528 context->ctrlr = ctrlr; 1529 1530 if (get_temperature_threshold_feature(context)) { 1531 nvme_health_info_cleanup(context, true); 1532 SPDK_ERRLOG("feature SPDK_NVME_FEAT_TEMPERATURE_THRESHOLD failed to submit\n"); 1533 } 1534 1535 return; 1536 } 1537 1538 static void 1539 rpc_bdev_nvme_get_controller_health_info(struct spdk_jsonrpc_request *request, 1540 const struct spdk_json_val *params) 1541 { 1542 struct rpc_get_controller_health_info req = {}; 1543 struct nvme_ctrlr *nvme_ctrlr = NULL; 1544 1545 if (!params) { 1546 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 1547 "Missing device name"); 1548 1549 return; 1550 } 1551 if (spdk_json_decode_object(params, rpc_get_controller_health_info_decoders, 1552 SPDK_COUNTOF(rpc_get_controller_health_info_decoders), &req)) { 1553 SPDK_ERRLOG("spdk_json_decode_object failed\n"); 1554 free_rpc_get_controller_health_info(&req); 1555 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 1556 "Invalid parameters"); 1557 1558 return; 1559 } 1560 1561 nvme_ctrlr = nvme_ctrlr_get_by_name(req.name); 1562 1563 if (!nvme_ctrlr) { 1564 SPDK_ERRLOG("nvme ctrlr name '%s' does not exist\n", req.name); 1565 free_rpc_get_controller_health_info(&req); 1566 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 1567 "Device not found"); 1568 return; 1569 } 1570 1571 get_controller_health_info(request, nvme_ctrlr->ctrlr); 1572 free_rpc_get_controller_health_info(&req); 1573 1574 return; 1575 } 1576 SPDK_RPC_REGISTER("bdev_nvme_get_controller_health_info", 1577 rpc_bdev_nvme_get_controller_health_info, SPDK_RPC_RUNTIME) 1578 1579 struct rpc_bdev_nvme_start_discovery { 1580 char *name; 1581 char *trtype; 1582 char *adrfam; 1583 char *traddr; 1584 char *trsvcid; 1585 char *hostnqn; 1586 struct spdk_nvme_ctrlr_opts opts; 1587 }; 1588 1589 static void 1590 free_rpc_bdev_nvme_start_discovery(struct rpc_bdev_nvme_start_discovery *req) 1591 { 1592 free(req->name); 1593 free(req->trtype); 1594 free(req->adrfam); 1595 free(req->traddr); 1596 free(req->trsvcid); 1597 free(req->hostnqn); 1598 } 1599 1600 static const struct spdk_json_object_decoder rpc_bdev_nvme_start_discovery_decoders[] = { 1601 {"name", offsetof(struct rpc_bdev_nvme_start_discovery, name), spdk_json_decode_string}, 1602 {"trtype", offsetof(struct rpc_bdev_nvme_start_discovery, trtype), spdk_json_decode_string}, 1603 {"traddr", offsetof(struct rpc_bdev_nvme_start_discovery, traddr), spdk_json_decode_string}, 1604 {"adrfam", offsetof(struct rpc_bdev_nvme_start_discovery, adrfam), spdk_json_decode_string, true}, 1605 {"trsvcid", offsetof(struct rpc_bdev_nvme_start_discovery, trsvcid), spdk_json_decode_string, true}, 1606 {"hostnqn", offsetof(struct rpc_bdev_nvme_start_discovery, hostnqn), spdk_json_decode_string, true}, 1607 }; 1608 1609 struct rpc_bdev_nvme_start_discovery_ctx { 1610 struct rpc_bdev_nvme_start_discovery req; 1611 struct spdk_jsonrpc_request *request; 1612 }; 1613 1614 static void 1615 rpc_bdev_nvme_start_discovery_done(void *cb_ctx, int rc) 1616 { 1617 struct rpc_bdev_nvme_start_discovery_ctx *ctx = cb_ctx; 1618 struct spdk_jsonrpc_request *request = ctx->request; 1619 1620 if (rc < 0) { 1621 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters"); 1622 free_rpc_bdev_nvme_start_discovery(&ctx->req); 1623 free(ctx); 1624 return; 1625 } 1626 1627 spdk_jsonrpc_send_bool_response(ctx->request, rc == 0); 1628 1629 free_rpc_bdev_nvme_start_discovery(&ctx->req); 1630 free(ctx); 1631 } 1632 1633 static void 1634 rpc_bdev_nvme_start_discovery(struct spdk_jsonrpc_request *request, 1635 const struct spdk_json_val *params) 1636 { 1637 struct rpc_bdev_nvme_start_discovery_ctx *ctx; 1638 struct spdk_nvme_transport_id trid = {}; 1639 size_t len, maxlen; 1640 int rc; 1641 1642 ctx = calloc(1, sizeof(*ctx)); 1643 if (!ctx) { 1644 spdk_jsonrpc_send_error_response(request, -ENOMEM, spdk_strerror(ENOMEM)); 1645 return; 1646 } 1647 1648 spdk_nvme_ctrlr_get_default_ctrlr_opts(&ctx->req.opts, sizeof(ctx->req.opts)); 1649 1650 if (spdk_json_decode_object(params, rpc_bdev_nvme_start_discovery_decoders, 1651 SPDK_COUNTOF(rpc_bdev_nvme_start_discovery_decoders), 1652 &ctx->req)) { 1653 SPDK_ERRLOG("spdk_json_decode_object failed\n"); 1654 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 1655 "spdk_json_decode_object failed"); 1656 goto cleanup; 1657 } 1658 1659 /* Parse trstring */ 1660 rc = spdk_nvme_transport_id_populate_trstring(&trid, ctx->req.trtype); 1661 if (rc < 0) { 1662 SPDK_ERRLOG("Failed to parse trtype: %s\n", ctx->req.trtype); 1663 spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "Failed to parse trtype: %s", 1664 ctx->req.trtype); 1665 goto cleanup; 1666 } 1667 1668 /* Parse trtype */ 1669 rc = spdk_nvme_transport_id_parse_trtype(&trid.trtype, ctx->req.trtype); 1670 assert(rc == 0); 1671 1672 /* Parse traddr */ 1673 maxlen = sizeof(trid.traddr); 1674 len = strnlen(ctx->req.traddr, maxlen); 1675 if (len == maxlen) { 1676 spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "traddr too long: %s", 1677 ctx->req.traddr); 1678 goto cleanup; 1679 } 1680 memcpy(trid.traddr, ctx->req.traddr, len + 1); 1681 1682 /* Parse adrfam */ 1683 if (ctx->req.adrfam) { 1684 rc = spdk_nvme_transport_id_parse_adrfam(&trid.adrfam, ctx->req.adrfam); 1685 if (rc < 0) { 1686 SPDK_ERRLOG("Failed to parse adrfam: %s\n", ctx->req.adrfam); 1687 spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "Failed to parse adrfam: %s", 1688 ctx->req.adrfam); 1689 goto cleanup; 1690 } 1691 } 1692 1693 /* Parse trsvcid */ 1694 if (ctx->req.trsvcid) { 1695 maxlen = sizeof(trid.trsvcid); 1696 len = strnlen(ctx->req.trsvcid, maxlen); 1697 if (len == maxlen) { 1698 spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "trsvcid too long: %s", 1699 ctx->req.trsvcid); 1700 goto cleanup; 1701 } 1702 memcpy(trid.trsvcid, ctx->req.trsvcid, len + 1); 1703 } 1704 1705 if (ctx->req.hostnqn) { 1706 snprintf(ctx->req.opts.hostnqn, sizeof(ctx->req.opts.hostnqn), "%s", 1707 ctx->req.hostnqn); 1708 } 1709 1710 ctx->request = request; 1711 rc = bdev_nvme_start_discovery(&trid, ctx->req.name, &ctx->req.opts, 1712 rpc_bdev_nvme_start_discovery_done, ctx); 1713 if (rc) { 1714 spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc)); 1715 goto cleanup; 1716 } 1717 1718 return; 1719 1720 cleanup: 1721 free_rpc_bdev_nvme_start_discovery(&ctx->req); 1722 free(ctx); 1723 } 1724 SPDK_RPC_REGISTER("bdev_nvme_start_discovery", rpc_bdev_nvme_start_discovery, 1725 SPDK_RPC_RUNTIME) 1726 1727 struct rpc_bdev_nvme_stop_discovery { 1728 char *name; 1729 }; 1730 1731 static const struct spdk_json_object_decoder rpc_bdev_nvme_stop_discovery_decoders[] = { 1732 {"name", offsetof(struct rpc_bdev_nvme_stop_discovery, name), spdk_json_decode_string}, 1733 }; 1734 1735 struct rpc_bdev_nvme_stop_discovery_ctx { 1736 struct rpc_bdev_nvme_stop_discovery req; 1737 struct spdk_jsonrpc_request *request; 1738 }; 1739 1740 static void 1741 rpc_bdev_nvme_stop_discovery_done(void *cb_ctx) 1742 { 1743 struct rpc_bdev_nvme_stop_discovery_ctx *ctx = cb_ctx; 1744 1745 spdk_jsonrpc_send_bool_response(ctx->request, true); 1746 free(ctx->req.name); 1747 free(ctx); 1748 } 1749 1750 static void 1751 rpc_bdev_nvme_stop_discovery(struct spdk_jsonrpc_request *request, 1752 const struct spdk_json_val *params) 1753 { 1754 struct rpc_bdev_nvme_stop_discovery_ctx *ctx; 1755 int rc; 1756 1757 ctx = calloc(1, sizeof(*ctx)); 1758 if (!ctx) { 1759 spdk_jsonrpc_send_error_response(request, -ENOMEM, spdk_strerror(ENOMEM)); 1760 return; 1761 } 1762 1763 if (spdk_json_decode_object(params, rpc_bdev_nvme_stop_discovery_decoders, 1764 SPDK_COUNTOF(rpc_bdev_nvme_stop_discovery_decoders), 1765 &ctx->req)) { 1766 SPDK_ERRLOG("spdk_json_decode_object failed\n"); 1767 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 1768 "spdk_json_decode_object failed"); 1769 goto cleanup; 1770 } 1771 1772 ctx->request = request; 1773 rc = bdev_nvme_stop_discovery(ctx->req.name, rpc_bdev_nvme_stop_discovery_done, ctx); 1774 if (rc) { 1775 spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc)); 1776 goto cleanup; 1777 } 1778 1779 return; 1780 1781 cleanup: 1782 free(ctx->req.name); 1783 free(ctx); 1784 } 1785 SPDK_RPC_REGISTER("bdev_nvme_stop_discovery", rpc_bdev_nvme_stop_discovery, 1786 SPDK_RPC_RUNTIME) 1787 1788 enum error_injection_cmd_type { 1789 NVME_ADMIN_CMD = 1, 1790 NVME_IO_CMD, 1791 }; 1792 1793 struct rpc_add_error_injection { 1794 char *name; 1795 enum error_injection_cmd_type cmd_type; 1796 uint8_t opc; 1797 bool do_not_submit; 1798 uint64_t timeout_in_us; 1799 uint32_t err_count; 1800 uint8_t sct; 1801 uint8_t sc; 1802 }; 1803 1804 static void 1805 free_rpc_add_error_injection(struct rpc_add_error_injection *req) 1806 { 1807 free(req->name); 1808 } 1809 1810 static int 1811 rpc_error_injection_decode_cmd_type(const struct spdk_json_val *val, void *out) 1812 { 1813 int *cmd_type = out; 1814 1815 if (spdk_json_strequal(val, "admin")) { 1816 *cmd_type = NVME_ADMIN_CMD; 1817 } else if (spdk_json_strequal(val, "io")) { 1818 *cmd_type = NVME_IO_CMD; 1819 } else { 1820 SPDK_ERRLOG("Invalid parameter value: cmd_type\n"); 1821 return -EINVAL; 1822 } 1823 1824 return 0; 1825 } 1826 1827 static const struct spdk_json_object_decoder rpc_add_error_injection_decoders[] = { 1828 { "name", offsetof(struct rpc_add_error_injection, name), spdk_json_decode_string }, 1829 { "cmd_type", offsetof(struct rpc_add_error_injection, cmd_type), rpc_error_injection_decode_cmd_type }, 1830 { "opc", offsetof(struct rpc_add_error_injection, opc), spdk_json_decode_uint8 }, 1831 { "do_not_submit", offsetof(struct rpc_add_error_injection, do_not_submit), spdk_json_decode_bool, true }, 1832 { "timeout_in_us", offsetof(struct rpc_add_error_injection, timeout_in_us), spdk_json_decode_uint64, true }, 1833 { "err_count", offsetof(struct rpc_add_error_injection, err_count), spdk_json_decode_uint32, true }, 1834 { "sct", offsetof(struct rpc_add_error_injection, sct), spdk_json_decode_uint8, true}, 1835 { "sc", offsetof(struct rpc_add_error_injection, sc), spdk_json_decode_uint8, true}, 1836 }; 1837 1838 struct rpc_add_error_injection_ctx { 1839 struct spdk_jsonrpc_request *request; 1840 struct rpc_add_error_injection rpc; 1841 }; 1842 1843 static void 1844 rpc_add_error_injection_done(struct spdk_io_channel_iter *i, int status) 1845 { 1846 struct rpc_add_error_injection_ctx *ctx = spdk_io_channel_iter_get_ctx(i); 1847 1848 if (status) { 1849 spdk_jsonrpc_send_error_response(ctx->request, status, 1850 "Failed to add the error injection."); 1851 } else { 1852 spdk_jsonrpc_send_bool_response(ctx->request, true); 1853 } 1854 1855 free_rpc_add_error_injection(&ctx->rpc); 1856 free(ctx); 1857 } 1858 1859 static void 1860 rpc_add_error_injection_per_channel(struct spdk_io_channel_iter *i) 1861 { 1862 struct spdk_io_channel *ch = spdk_io_channel_iter_get_channel(i); 1863 struct rpc_add_error_injection_ctx *ctx = spdk_io_channel_iter_get_ctx(i); 1864 struct nvme_ctrlr_channel *ctrlr_ch = spdk_io_channel_get_ctx(ch); 1865 struct nvme_ctrlr *nvme_ctrlr = nvme_ctrlr_channel_get_ctrlr(ctrlr_ch); 1866 struct spdk_nvme_qpair *qpair = ctrlr_ch->qpair; 1867 struct spdk_nvme_ctrlr *ctrlr = nvme_ctrlr->ctrlr; 1868 int rc = 0; 1869 1870 if (qpair != NULL) { 1871 rc = spdk_nvme_qpair_add_cmd_error_injection(ctrlr, qpair, ctx->rpc.opc, 1872 ctx->rpc.do_not_submit, ctx->rpc.timeout_in_us, ctx->rpc.err_count, 1873 ctx->rpc.sct, ctx->rpc.sc); 1874 } 1875 1876 spdk_for_each_channel_continue(i, rc); 1877 } 1878 1879 static void 1880 rpc_bdev_nvme_add_error_injection( 1881 struct spdk_jsonrpc_request *request, 1882 const struct spdk_json_val *params) 1883 { 1884 struct rpc_add_error_injection_ctx *ctx; 1885 struct nvme_ctrlr *nvme_ctrlr; 1886 int rc; 1887 1888 ctx = calloc(1, sizeof(*ctx)); 1889 if (!ctx) { 1890 spdk_jsonrpc_send_error_response(request, -ENOMEM, spdk_strerror(ENOMEM)); 1891 return; 1892 } 1893 ctx->rpc.err_count = 1; 1894 ctx->request = request; 1895 1896 if (spdk_json_decode_object(params, 1897 rpc_add_error_injection_decoders, 1898 SPDK_COUNTOF(rpc_add_error_injection_decoders), 1899 &ctx->rpc)) { 1900 spdk_jsonrpc_send_error_response(request, -EINVAL, 1901 "Failed to parse the request"); 1902 goto cleanup; 1903 } 1904 1905 nvme_ctrlr = nvme_ctrlr_get_by_name(ctx->rpc.name); 1906 if (nvme_ctrlr == NULL) { 1907 SPDK_ERRLOG("No controller with specified name was found.\n"); 1908 spdk_jsonrpc_send_error_response(request, -ENODEV, spdk_strerror(ENODEV)); 1909 goto cleanup; 1910 } 1911 1912 if (ctx->rpc.cmd_type == NVME_IO_CMD) { 1913 spdk_for_each_channel(nvme_ctrlr, 1914 rpc_add_error_injection_per_channel, 1915 ctx, 1916 rpc_add_error_injection_done); 1917 1918 return; 1919 } else { 1920 rc = spdk_nvme_qpair_add_cmd_error_injection(nvme_ctrlr->ctrlr, NULL, ctx->rpc.opc, 1921 ctx->rpc.do_not_submit, ctx->rpc.timeout_in_us, ctx->rpc.err_count, 1922 ctx->rpc.sct, ctx->rpc.sc); 1923 if (rc) { 1924 spdk_jsonrpc_send_error_response(request, -rc, 1925 "Failed to add the error injection"); 1926 } else { 1927 spdk_jsonrpc_send_bool_response(ctx->request, true); 1928 } 1929 } 1930 1931 cleanup: 1932 free_rpc_add_error_injection(&ctx->rpc); 1933 free(ctx); 1934 } 1935 SPDK_RPC_REGISTER("bdev_nvme_add_error_injection", rpc_bdev_nvme_add_error_injection, 1936 SPDK_RPC_RUNTIME) 1937 1938 struct rpc_remove_error_injection { 1939 char *name; 1940 enum error_injection_cmd_type cmd_type; 1941 uint8_t opc; 1942 }; 1943 1944 static void 1945 free_rpc_remove_error_injection(struct rpc_remove_error_injection *req) 1946 { 1947 free(req->name); 1948 } 1949 1950 static const struct spdk_json_object_decoder rpc_remove_error_injection_decoders[] = { 1951 { "name", offsetof(struct rpc_remove_error_injection, name), spdk_json_decode_string }, 1952 { "cmd_type", offsetof(struct rpc_remove_error_injection, cmd_type), rpc_error_injection_decode_cmd_type }, 1953 { "opc", offsetof(struct rpc_remove_error_injection, opc), spdk_json_decode_uint8 }, 1954 }; 1955 1956 struct rpc_remove_error_injection_ctx { 1957 struct spdk_jsonrpc_request *request; 1958 struct rpc_remove_error_injection rpc; 1959 }; 1960 1961 static void 1962 rpc_remove_error_injection_done(struct spdk_io_channel_iter *i, int status) 1963 { 1964 struct rpc_remove_error_injection_ctx *ctx = spdk_io_channel_iter_get_ctx(i); 1965 1966 if (status) { 1967 spdk_jsonrpc_send_error_response(ctx->request, status, 1968 "Failed to remove the error injection."); 1969 } else { 1970 spdk_jsonrpc_send_bool_response(ctx->request, true); 1971 } 1972 1973 free_rpc_remove_error_injection(&ctx->rpc); 1974 free(ctx); 1975 } 1976 1977 static void 1978 rpc_remove_error_injection_per_channel(struct spdk_io_channel_iter *i) 1979 { 1980 struct spdk_io_channel *ch = spdk_io_channel_iter_get_channel(i); 1981 struct rpc_remove_error_injection_ctx *ctx = spdk_io_channel_iter_get_ctx(i); 1982 struct nvme_ctrlr_channel *ctrlr_ch = spdk_io_channel_get_ctx(ch); 1983 struct nvme_ctrlr *nvme_ctrlr = nvme_ctrlr_channel_get_ctrlr(ctrlr_ch); 1984 struct spdk_nvme_qpair *qpair = ctrlr_ch->qpair; 1985 struct spdk_nvme_ctrlr *ctrlr = nvme_ctrlr->ctrlr; 1986 1987 if (ctrlr_ch->qpair != NULL) { 1988 spdk_nvme_qpair_remove_cmd_error_injection(ctrlr, qpair, ctx->rpc.opc); 1989 } 1990 1991 spdk_for_each_channel_continue(i, 0); 1992 } 1993 1994 static void 1995 rpc_bdev_nvme_remove_error_injection(struct spdk_jsonrpc_request *request, 1996 const struct spdk_json_val *params) 1997 { 1998 struct rpc_remove_error_injection_ctx *ctx; 1999 struct nvme_ctrlr *nvme_ctrlr; 2000 2001 ctx = calloc(1, sizeof(*ctx)); 2002 if (!ctx) { 2003 spdk_jsonrpc_send_error_response(request, -ENOMEM, spdk_strerror(ENOMEM)); 2004 return; 2005 } 2006 ctx->request = request; 2007 2008 if (spdk_json_decode_object(params, 2009 rpc_remove_error_injection_decoders, 2010 SPDK_COUNTOF(rpc_remove_error_injection_decoders), 2011 &ctx->rpc)) { 2012 spdk_jsonrpc_send_error_response(request, -EINVAL, 2013 "Failed to parse the request"); 2014 goto cleanup; 2015 } 2016 2017 nvme_ctrlr = nvme_ctrlr_get_by_name(ctx->rpc.name); 2018 if (nvme_ctrlr == NULL) { 2019 SPDK_ERRLOG("No controller with specified name was found.\n"); 2020 spdk_jsonrpc_send_error_response(request, -ENODEV, spdk_strerror(ENODEV)); 2021 goto cleanup; 2022 } 2023 2024 if (ctx->rpc.cmd_type == NVME_IO_CMD) { 2025 spdk_for_each_channel(nvme_ctrlr, 2026 rpc_remove_error_injection_per_channel, 2027 ctx, 2028 rpc_remove_error_injection_done); 2029 return; 2030 } else { 2031 spdk_nvme_qpair_remove_cmd_error_injection(nvme_ctrlr->ctrlr, NULL, ctx->rpc.opc); 2032 spdk_jsonrpc_send_bool_response(ctx->request, true); 2033 } 2034 2035 cleanup: 2036 free_rpc_remove_error_injection(&ctx->rpc); 2037 free(ctx); 2038 } 2039 SPDK_RPC_REGISTER("bdev_nvme_remove_error_injection", rpc_bdev_nvme_remove_error_injection, 2040 SPDK_RPC_RUNTIME) 2041