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