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