1 /*- 2 * BSD LICENSE 3 * 4 * Copyright (c) Intel Corporation. All rights reserved. 5 * Copyright (c) 2018-2019 Mellanox Technologies LTD. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * * Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * * Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in 15 * the documentation and/or other materials provided with the 16 * distribution. 17 * * Neither the name of Intel Corporation nor the names of its 18 * contributors may be used to endorse or promote products derived 19 * from this software without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 #include "spdk/stdinc.h" 35 36 #include "spdk/bdev.h" 37 #include "spdk/bit_array.h" 38 #include "spdk/conf.h" 39 #include "spdk/thread.h" 40 #include "spdk/nvmf.h" 41 #include "spdk/trace.h" 42 #include "spdk/endian.h" 43 #include "spdk/string.h" 44 45 #include "spdk_internal/log.h" 46 47 #include "nvmf_internal.h" 48 #include "transport.h" 49 50 SPDK_LOG_REGISTER_COMPONENT("nvmf", SPDK_LOG_NVMF) 51 52 #define SPDK_NVMF_DEFAULT_MAX_SUBSYSTEMS 1024 53 54 static TAILQ_HEAD(, spdk_nvmf_tgt) g_nvmf_tgts = TAILQ_HEAD_INITIALIZER(g_nvmf_tgts); 55 56 typedef void (*nvmf_qpair_disconnect_cpl)(void *ctx, int status); 57 static void nvmf_tgt_destroy_poll_group(void *io_device, void *ctx_buf); 58 59 /* supplied to a single call to nvmf_qpair_disconnect */ 60 struct nvmf_qpair_disconnect_ctx { 61 struct spdk_nvmf_qpair *qpair; 62 struct spdk_nvmf_ctrlr *ctrlr; 63 nvmf_qpair_disconnect_cb cb_fn; 64 struct spdk_thread *thread; 65 void *ctx; 66 uint16_t qid; 67 }; 68 69 /* 70 * There are several times when we need to iterate through the list of all qpairs and selectively delete them. 71 * In order to do this sequentially without overlap, we must provide a context to recover the next qpair from 72 * to enable calling nvmf_qpair_disconnect on the next desired qpair. 73 */ 74 struct nvmf_qpair_disconnect_many_ctx { 75 struct spdk_nvmf_subsystem *subsystem; 76 struct spdk_nvmf_poll_group *group; 77 spdk_nvmf_poll_group_mod_done cpl_fn; 78 void *cpl_ctx; 79 }; 80 81 static void 82 nvmf_qpair_set_state(struct spdk_nvmf_qpair *qpair, 83 enum spdk_nvmf_qpair_state state) 84 { 85 assert(qpair != NULL); 86 assert(qpair->group->thread == spdk_get_thread()); 87 88 qpair->state = state; 89 } 90 91 static int 92 nvmf_poll_group_poll(void *ctx) 93 { 94 struct spdk_nvmf_poll_group *group = ctx; 95 int rc; 96 int count = 0; 97 struct spdk_nvmf_transport_poll_group *tgroup; 98 99 TAILQ_FOREACH(tgroup, &group->tgroups, link) { 100 rc = nvmf_transport_poll_group_poll(tgroup); 101 if (rc < 0) { 102 return SPDK_POLLER_BUSY; 103 } 104 count += rc; 105 } 106 107 return count > 0 ? SPDK_POLLER_BUSY : SPDK_POLLER_IDLE; 108 } 109 110 static int 111 nvmf_tgt_create_poll_group(void *io_device, void *ctx_buf) 112 { 113 struct spdk_nvmf_tgt *tgt = io_device; 114 struct spdk_nvmf_poll_group *group = ctx_buf; 115 struct spdk_nvmf_transport *transport; 116 uint32_t sid; 117 118 TAILQ_INIT(&group->tgroups); 119 TAILQ_INIT(&group->qpairs); 120 121 TAILQ_FOREACH(transport, &tgt->transports, link) { 122 nvmf_poll_group_add_transport(group, transport); 123 } 124 125 group->num_sgroups = tgt->max_subsystems; 126 group->sgroups = calloc(tgt->max_subsystems, sizeof(struct spdk_nvmf_subsystem_poll_group)); 127 if (!group->sgroups) { 128 return -ENOMEM; 129 } 130 131 for (sid = 0; sid < tgt->max_subsystems; sid++) { 132 struct spdk_nvmf_subsystem *subsystem; 133 134 subsystem = tgt->subsystems[sid]; 135 if (!subsystem) { 136 continue; 137 } 138 139 if (nvmf_poll_group_add_subsystem(group, subsystem, NULL, NULL) != 0) { 140 nvmf_tgt_destroy_poll_group(io_device, ctx_buf); 141 return -1; 142 } 143 } 144 145 pthread_mutex_lock(&tgt->mutex); 146 TAILQ_INSERT_TAIL(&tgt->poll_groups, group, link); 147 pthread_mutex_unlock(&tgt->mutex); 148 149 group->poller = SPDK_POLLER_REGISTER(nvmf_poll_group_poll, group, 0); 150 group->thread = spdk_get_thread(); 151 152 return 0; 153 } 154 155 static void 156 nvmf_tgt_destroy_poll_group(void *io_device, void *ctx_buf) 157 { 158 struct spdk_nvmf_tgt *tgt = io_device; 159 struct spdk_nvmf_poll_group *group = ctx_buf; 160 struct spdk_nvmf_transport_poll_group *tgroup, *tmp; 161 struct spdk_nvmf_subsystem_poll_group *sgroup; 162 uint32_t sid, nsid; 163 164 pthread_mutex_lock(&tgt->mutex); 165 TAILQ_REMOVE(&tgt->poll_groups, group, link); 166 pthread_mutex_unlock(&tgt->mutex); 167 168 TAILQ_FOREACH_SAFE(tgroup, &group->tgroups, link, tmp) { 169 TAILQ_REMOVE(&group->tgroups, tgroup, link); 170 nvmf_transport_poll_group_destroy(tgroup); 171 } 172 173 for (sid = 0; sid < group->num_sgroups; sid++) { 174 sgroup = &group->sgroups[sid]; 175 176 for (nsid = 0; nsid < sgroup->num_ns; nsid++) { 177 if (sgroup->ns_info[nsid].channel) { 178 spdk_put_io_channel(sgroup->ns_info[nsid].channel); 179 sgroup->ns_info[nsid].channel = NULL; 180 } 181 } 182 183 free(sgroup->ns_info); 184 } 185 186 free(group->sgroups); 187 188 if (group->destroy_cb_fn) { 189 group->destroy_cb_fn(group->destroy_cb_arg, 0); 190 } 191 } 192 193 static void 194 _nvmf_tgt_disconnect_next_qpair(void *ctx) 195 { 196 struct spdk_nvmf_qpair *qpair; 197 struct nvmf_qpair_disconnect_many_ctx *qpair_ctx = ctx; 198 struct spdk_nvmf_poll_group *group = qpair_ctx->group; 199 struct spdk_io_channel *ch; 200 int rc = 0; 201 202 qpair = TAILQ_FIRST(&group->qpairs); 203 204 if (qpair) { 205 rc = spdk_nvmf_qpair_disconnect(qpair, _nvmf_tgt_disconnect_next_qpair, ctx); 206 } 207 208 if (!qpair || rc != 0) { 209 /* When the refcount from the channels reaches 0, nvmf_tgt_destroy_poll_group will be called. */ 210 ch = spdk_io_channel_from_ctx(group); 211 spdk_put_io_channel(ch); 212 free(qpair_ctx); 213 } 214 } 215 216 static void 217 nvmf_tgt_destroy_poll_group_qpairs(struct spdk_nvmf_poll_group *group) 218 { 219 struct nvmf_qpair_disconnect_many_ctx *ctx; 220 221 ctx = calloc(1, sizeof(struct nvmf_qpair_disconnect_many_ctx)); 222 223 if (!ctx) { 224 SPDK_ERRLOG("Failed to allocate memory for destroy poll group ctx\n"); 225 return; 226 } 227 228 spdk_poller_unregister(&group->poller); 229 230 ctx->group = group; 231 _nvmf_tgt_disconnect_next_qpair(ctx); 232 } 233 234 struct spdk_nvmf_tgt * 235 spdk_nvmf_tgt_create(struct spdk_nvmf_target_opts *opts) 236 { 237 struct spdk_nvmf_tgt *tgt, *tmp_tgt; 238 239 if (strnlen(opts->name, NVMF_TGT_NAME_MAX_LENGTH) == NVMF_TGT_NAME_MAX_LENGTH) { 240 SPDK_ERRLOG("Provided target name exceeds the max length of %u.\n", NVMF_TGT_NAME_MAX_LENGTH); 241 return NULL; 242 } 243 244 TAILQ_FOREACH(tmp_tgt, &g_nvmf_tgts, link) { 245 if (!strncmp(opts->name, tmp_tgt->name, NVMF_TGT_NAME_MAX_LENGTH)) { 246 SPDK_ERRLOG("Provided target name must be unique.\n"); 247 return NULL; 248 } 249 } 250 251 tgt = calloc(1, sizeof(*tgt)); 252 if (!tgt) { 253 return NULL; 254 } 255 256 snprintf(tgt->name, NVMF_TGT_NAME_MAX_LENGTH, "%s", opts->name); 257 258 if (!opts || !opts->max_subsystems) { 259 tgt->max_subsystems = SPDK_NVMF_DEFAULT_MAX_SUBSYSTEMS; 260 } else { 261 tgt->max_subsystems = opts->max_subsystems; 262 } 263 264 tgt->discovery_genctr = 0; 265 TAILQ_INIT(&tgt->transports); 266 TAILQ_INIT(&tgt->poll_groups); 267 268 tgt->subsystems = calloc(tgt->max_subsystems, sizeof(struct spdk_nvmf_subsystem *)); 269 if (!tgt->subsystems) { 270 free(tgt); 271 return NULL; 272 } 273 274 pthread_mutex_init(&tgt->mutex, NULL); 275 276 TAILQ_INSERT_HEAD(&g_nvmf_tgts, tgt, link); 277 278 spdk_io_device_register(tgt, 279 nvmf_tgt_create_poll_group, 280 nvmf_tgt_destroy_poll_group, 281 sizeof(struct spdk_nvmf_poll_group), 282 tgt->name); 283 284 return tgt; 285 } 286 287 static void 288 nvmf_tgt_destroy_cb(void *io_device) 289 { 290 struct spdk_nvmf_tgt *tgt = io_device; 291 struct spdk_nvmf_transport *transport, *transport_tmp; 292 spdk_nvmf_tgt_destroy_done_fn *destroy_cb_fn; 293 void *destroy_cb_arg; 294 uint32_t i; 295 296 if (tgt->subsystems) { 297 for (i = 0; i < tgt->max_subsystems; i++) { 298 if (tgt->subsystems[i]) { 299 nvmf_subsystem_remove_all_listeners(tgt->subsystems[i], true); 300 spdk_nvmf_subsystem_destroy(tgt->subsystems[i]); 301 } 302 } 303 free(tgt->subsystems); 304 } 305 306 TAILQ_FOREACH_SAFE(transport, &tgt->transports, link, transport_tmp) { 307 TAILQ_REMOVE(&tgt->transports, transport, link); 308 spdk_nvmf_transport_destroy(transport); 309 } 310 311 destroy_cb_fn = tgt->destroy_cb_fn; 312 destroy_cb_arg = tgt->destroy_cb_arg; 313 314 free(tgt); 315 316 if (destroy_cb_fn) { 317 destroy_cb_fn(destroy_cb_arg, 0); 318 } 319 } 320 321 void 322 spdk_nvmf_tgt_destroy(struct spdk_nvmf_tgt *tgt, 323 spdk_nvmf_tgt_destroy_done_fn cb_fn, 324 void *cb_arg) 325 { 326 tgt->destroy_cb_fn = cb_fn; 327 tgt->destroy_cb_arg = cb_arg; 328 329 TAILQ_REMOVE(&g_nvmf_tgts, tgt, link); 330 331 spdk_io_device_unregister(tgt, nvmf_tgt_destroy_cb); 332 } 333 334 const char * 335 spdk_nvmf_tgt_get_name(struct spdk_nvmf_tgt *tgt) 336 { 337 return tgt->name; 338 } 339 340 struct spdk_nvmf_tgt * 341 spdk_nvmf_get_tgt(const char *name) 342 { 343 struct spdk_nvmf_tgt *tgt; 344 uint32_t num_targets = 0; 345 346 TAILQ_FOREACH(tgt, &g_nvmf_tgts, link) { 347 if (name) { 348 if (!strncmp(tgt->name, name, NVMF_TGT_NAME_MAX_LENGTH)) { 349 return tgt; 350 } 351 } 352 num_targets++; 353 } 354 355 /* 356 * special case. If there is only one target and 357 * no name was specified, return the only available 358 * target. If there is more than one target, name must 359 * be specified. 360 */ 361 if (!name && num_targets == 1) { 362 return TAILQ_FIRST(&g_nvmf_tgts); 363 } 364 365 return NULL; 366 } 367 368 struct spdk_nvmf_tgt * 369 spdk_nvmf_get_first_tgt(void) 370 { 371 return TAILQ_FIRST(&g_nvmf_tgts); 372 } 373 374 struct spdk_nvmf_tgt * 375 spdk_nvmf_get_next_tgt(struct spdk_nvmf_tgt *prev) 376 { 377 return TAILQ_NEXT(prev, link); 378 } 379 380 static void 381 nvmf_write_subsystem_config_json(struct spdk_json_write_ctx *w, 382 struct spdk_nvmf_subsystem *subsystem) 383 { 384 struct spdk_nvmf_host *host; 385 struct spdk_nvmf_subsystem_listener *listener; 386 const struct spdk_nvme_transport_id *trid; 387 struct spdk_nvmf_ns *ns; 388 struct spdk_nvmf_ns_opts ns_opts; 389 uint32_t max_namespaces; 390 char uuid_str[SPDK_UUID_STRING_LEN]; 391 const char *adrfam; 392 393 if (spdk_nvmf_subsystem_get_type(subsystem) != SPDK_NVMF_SUBTYPE_NVME) { 394 return; 395 } 396 397 /* { */ 398 spdk_json_write_object_begin(w); 399 spdk_json_write_named_string(w, "method", "nvmf_create_subsystem"); 400 401 /* "params" : { */ 402 spdk_json_write_named_object_begin(w, "params"); 403 spdk_json_write_named_string(w, "nqn", spdk_nvmf_subsystem_get_nqn(subsystem)); 404 spdk_json_write_named_bool(w, "allow_any_host", spdk_nvmf_subsystem_get_allow_any_host(subsystem)); 405 spdk_json_write_named_string(w, "serial_number", spdk_nvmf_subsystem_get_sn(subsystem)); 406 spdk_json_write_named_string(w, "model_number", spdk_nvmf_subsystem_get_mn(subsystem)); 407 408 max_namespaces = spdk_nvmf_subsystem_get_max_namespaces(subsystem); 409 if (max_namespaces != 0) { 410 spdk_json_write_named_uint32(w, "max_namespaces", max_namespaces); 411 } 412 413 /* } "params" */ 414 spdk_json_write_object_end(w); 415 416 /* } */ 417 spdk_json_write_object_end(w); 418 419 for (listener = spdk_nvmf_subsystem_get_first_listener(subsystem); listener != NULL; 420 listener = spdk_nvmf_subsystem_get_next_listener(subsystem, listener)) { 421 trid = spdk_nvmf_subsystem_listener_get_trid(listener); 422 423 adrfam = spdk_nvme_transport_id_adrfam_str(trid->adrfam); 424 425 spdk_json_write_object_begin(w); 426 spdk_json_write_named_string(w, "method", "nvmf_subsystem_add_listener"); 427 428 /* "params" : { */ 429 spdk_json_write_named_object_begin(w, "params"); 430 431 spdk_json_write_named_string(w, "nqn", spdk_nvmf_subsystem_get_nqn(subsystem)); 432 433 /* "listen_address" : { */ 434 spdk_json_write_named_object_begin(w, "listen_address"); 435 436 spdk_json_write_named_string(w, "trtype", trid->trstring); 437 if (adrfam) { 438 spdk_json_write_named_string(w, "adrfam", adrfam); 439 } 440 441 spdk_json_write_named_string(w, "traddr", trid->traddr); 442 spdk_json_write_named_string(w, "trsvcid", trid->trsvcid); 443 /* } "listen_address" */ 444 spdk_json_write_object_end(w); 445 446 /* } "params" */ 447 spdk_json_write_object_end(w); 448 449 /* } */ 450 spdk_json_write_object_end(w); 451 } 452 453 for (host = spdk_nvmf_subsystem_get_first_host(subsystem); host != NULL; 454 host = spdk_nvmf_subsystem_get_next_host(subsystem, host)) { 455 456 spdk_json_write_object_begin(w); 457 spdk_json_write_named_string(w, "method", "nvmf_subsystem_add_host"); 458 459 /* "params" : { */ 460 spdk_json_write_named_object_begin(w, "params"); 461 462 spdk_json_write_named_string(w, "nqn", spdk_nvmf_subsystem_get_nqn(subsystem)); 463 spdk_json_write_named_string(w, "host", spdk_nvmf_host_get_nqn(host)); 464 465 /* } "params" */ 466 spdk_json_write_object_end(w); 467 468 /* } */ 469 spdk_json_write_object_end(w); 470 } 471 472 for (ns = spdk_nvmf_subsystem_get_first_ns(subsystem); ns != NULL; 473 ns = spdk_nvmf_subsystem_get_next_ns(subsystem, ns)) { 474 spdk_nvmf_ns_get_opts(ns, &ns_opts, sizeof(ns_opts)); 475 476 spdk_json_write_object_begin(w); 477 spdk_json_write_named_string(w, "method", "nvmf_subsystem_add_ns"); 478 479 /* "params" : { */ 480 spdk_json_write_named_object_begin(w, "params"); 481 482 spdk_json_write_named_string(w, "nqn", spdk_nvmf_subsystem_get_nqn(subsystem)); 483 484 /* "namespace" : { */ 485 spdk_json_write_named_object_begin(w, "namespace"); 486 487 spdk_json_write_named_uint32(w, "nsid", spdk_nvmf_ns_get_id(ns)); 488 spdk_json_write_named_string(w, "bdev_name", spdk_bdev_get_name(spdk_nvmf_ns_get_bdev(ns))); 489 490 if (!spdk_mem_all_zero(ns_opts.nguid, sizeof(ns_opts.nguid))) { 491 SPDK_STATIC_ASSERT(sizeof(ns_opts.nguid) == sizeof(uint64_t) * 2, "size mismatch"); 492 spdk_json_write_named_string_fmt(w, "nguid", "%016"PRIX64"%016"PRIX64, from_be64(&ns_opts.nguid[0]), 493 from_be64(&ns_opts.nguid[8])); 494 } 495 496 if (!spdk_mem_all_zero(ns_opts.eui64, sizeof(ns_opts.eui64))) { 497 SPDK_STATIC_ASSERT(sizeof(ns_opts.eui64) == sizeof(uint64_t), "size mismatch"); 498 spdk_json_write_named_string_fmt(w, "eui64", "%016"PRIX64, from_be64(&ns_opts.eui64)); 499 } 500 501 if (!spdk_mem_all_zero(&ns_opts.uuid, sizeof(ns_opts.uuid))) { 502 spdk_uuid_fmt_lower(uuid_str, sizeof(uuid_str), &ns_opts.uuid); 503 spdk_json_write_named_string(w, "uuid", uuid_str); 504 } 505 506 /* "namespace" */ 507 spdk_json_write_object_end(w); 508 509 /* } "params" */ 510 spdk_json_write_object_end(w); 511 512 /* } */ 513 spdk_json_write_object_end(w); 514 } 515 } 516 517 void 518 spdk_nvmf_tgt_write_config_json(struct spdk_json_write_ctx *w, struct spdk_nvmf_tgt *tgt) 519 { 520 struct spdk_nvmf_subsystem *subsystem; 521 struct spdk_nvmf_transport *transport; 522 523 spdk_json_write_object_begin(w); 524 spdk_json_write_named_string(w, "method", "nvmf_set_max_subsystems"); 525 526 spdk_json_write_named_object_begin(w, "params"); 527 spdk_json_write_named_uint32(w, "max_subsystems", tgt->max_subsystems); 528 spdk_json_write_object_end(w); 529 530 spdk_json_write_object_end(w); 531 532 /* write transports */ 533 TAILQ_FOREACH(transport, &tgt->transports, link) { 534 spdk_json_write_object_begin(w); 535 spdk_json_write_named_string(w, "method", "nvmf_create_transport"); 536 537 spdk_json_write_named_object_begin(w, "params"); 538 spdk_json_write_named_string(w, "trtype", spdk_nvme_transport_id_trtype_str(transport->ops->type)); 539 spdk_json_write_named_uint32(w, "max_queue_depth", transport->opts.max_queue_depth); 540 spdk_json_write_named_uint32(w, "max_io_qpairs_per_ctrlr", 541 transport->opts.max_qpairs_per_ctrlr - 1); 542 spdk_json_write_named_uint32(w, "in_capsule_data_size", transport->opts.in_capsule_data_size); 543 spdk_json_write_named_uint32(w, "max_io_size", transport->opts.max_io_size); 544 spdk_json_write_named_uint32(w, "io_unit_size", transport->opts.io_unit_size); 545 spdk_json_write_named_uint32(w, "max_aq_depth", transport->opts.max_aq_depth); 546 if (transport->ops->type == SPDK_NVME_TRANSPORT_RDMA) { 547 spdk_json_write_named_uint32(w, "max_srq_depth", transport->opts.max_srq_depth); 548 } 549 spdk_json_write_named_uint32(w, "abort_timeout_sec", transport->opts.abort_timeout_sec); 550 spdk_json_write_object_end(w); 551 552 spdk_json_write_object_end(w); 553 } 554 555 subsystem = spdk_nvmf_subsystem_get_first(tgt); 556 while (subsystem) { 557 nvmf_write_subsystem_config_json(w, subsystem); 558 subsystem = spdk_nvmf_subsystem_get_next(subsystem); 559 } 560 } 561 562 int 563 spdk_nvmf_tgt_listen(struct spdk_nvmf_tgt *tgt, 564 struct spdk_nvme_transport_id *trid) 565 { 566 struct spdk_nvmf_transport *transport; 567 const char *trtype; 568 int rc; 569 570 transport = spdk_nvmf_tgt_get_transport(tgt, trid->trstring); 571 if (!transport) { 572 trtype = spdk_nvme_transport_id_trtype_str(trid->trtype); 573 if (trtype != NULL) { 574 SPDK_ERRLOG("Unable to listen on transport %s. The transport must be created first.\n", trtype); 575 } else { 576 SPDK_ERRLOG("The specified trtype %d is unknown. Please make sure that it is properly registered.\n", 577 trid->trtype); 578 } 579 580 return -EINVAL; 581 } 582 583 rc = spdk_nvmf_transport_listen(transport, trid); 584 if (rc < 0) { 585 SPDK_ERRLOG("Unable to listen on address '%s'\n", trid->traddr); 586 } 587 588 return rc; 589 } 590 591 int 592 spdk_nvmf_tgt_stop_listen(struct spdk_nvmf_tgt *tgt, 593 struct spdk_nvme_transport_id *trid) 594 { 595 struct spdk_nvmf_transport *transport; 596 const char *trtype; 597 int rc; 598 599 transport = spdk_nvmf_tgt_get_transport(tgt, trid->trstring); 600 if (!transport) { 601 trtype = spdk_nvme_transport_id_trtype_str(trid->trtype); 602 if (trtype != NULL) { 603 SPDK_ERRLOG("Unable to stop listen on transport %s. The transport must be created first.\n", 604 trtype); 605 } else { 606 SPDK_ERRLOG("The specified trtype %d is unknown. Please make sure that it is properly registered.\n", 607 trid->trtype); 608 } 609 return -EINVAL; 610 } 611 612 rc = spdk_nvmf_transport_stop_listen(transport, trid); 613 if (rc < 0) { 614 SPDK_ERRLOG("Failed to stop listening on address '%s'\n", trid->traddr); 615 return rc; 616 } 617 return 0; 618 } 619 620 struct spdk_nvmf_tgt_add_transport_ctx { 621 struct spdk_nvmf_tgt *tgt; 622 struct spdk_nvmf_transport *transport; 623 spdk_nvmf_tgt_add_transport_done_fn cb_fn; 624 void *cb_arg; 625 }; 626 627 static void 628 _nvmf_tgt_add_transport_done(struct spdk_io_channel_iter *i, int status) 629 { 630 struct spdk_nvmf_tgt_add_transport_ctx *ctx = spdk_io_channel_iter_get_ctx(i); 631 632 ctx->cb_fn(ctx->cb_arg, status); 633 634 free(ctx); 635 } 636 637 static void 638 _nvmf_tgt_add_transport(struct spdk_io_channel_iter *i) 639 { 640 struct spdk_nvmf_tgt_add_transport_ctx *ctx = spdk_io_channel_iter_get_ctx(i); 641 struct spdk_io_channel *ch = spdk_io_channel_iter_get_channel(i); 642 struct spdk_nvmf_poll_group *group = spdk_io_channel_get_ctx(ch); 643 int rc; 644 645 rc = nvmf_poll_group_add_transport(group, ctx->transport); 646 spdk_for_each_channel_continue(i, rc); 647 } 648 649 void spdk_nvmf_tgt_add_transport(struct spdk_nvmf_tgt *tgt, 650 struct spdk_nvmf_transport *transport, 651 spdk_nvmf_tgt_add_transport_done_fn cb_fn, 652 void *cb_arg) 653 { 654 struct spdk_nvmf_tgt_add_transport_ctx *ctx; 655 656 if (spdk_nvmf_tgt_get_transport(tgt, transport->ops->name)) { 657 cb_fn(cb_arg, -EEXIST); 658 return; /* transport already created */ 659 } 660 661 transport->tgt = tgt; 662 TAILQ_INSERT_TAIL(&tgt->transports, transport, link); 663 664 ctx = calloc(1, sizeof(*ctx)); 665 if (!ctx) { 666 cb_fn(cb_arg, -ENOMEM); 667 return; 668 } 669 670 ctx->tgt = tgt; 671 ctx->transport = transport; 672 ctx->cb_fn = cb_fn; 673 ctx->cb_arg = cb_arg; 674 675 spdk_for_each_channel(tgt, 676 _nvmf_tgt_add_transport, 677 ctx, 678 _nvmf_tgt_add_transport_done); 679 } 680 681 struct spdk_nvmf_subsystem * 682 spdk_nvmf_tgt_find_subsystem(struct spdk_nvmf_tgt *tgt, const char *subnqn) 683 { 684 struct spdk_nvmf_subsystem *subsystem; 685 uint32_t sid; 686 687 if (!subnqn) { 688 return NULL; 689 } 690 691 /* Ensure that subnqn is null terminated */ 692 if (!memchr(subnqn, '\0', SPDK_NVMF_NQN_MAX_LEN + 1)) { 693 SPDK_ERRLOG("Connect SUBNQN is not null terminated\n"); 694 return NULL; 695 } 696 697 for (sid = 0; sid < tgt->max_subsystems; sid++) { 698 subsystem = tgt->subsystems[sid]; 699 if (subsystem == NULL) { 700 continue; 701 } 702 703 if (strcmp(subnqn, subsystem->subnqn) == 0) { 704 return subsystem; 705 } 706 } 707 708 return NULL; 709 } 710 711 struct spdk_nvmf_transport * 712 spdk_nvmf_tgt_get_transport(struct spdk_nvmf_tgt *tgt, const char *transport_name) 713 { 714 struct spdk_nvmf_transport *transport; 715 716 TAILQ_FOREACH(transport, &tgt->transports, link) { 717 if (!strncasecmp(transport->ops->name, transport_name, SPDK_NVMF_TRSTRING_MAX_LEN)) { 718 return transport; 719 } 720 } 721 return NULL; 722 } 723 724 struct nvmf_new_qpair_ctx { 725 struct spdk_nvmf_qpair *qpair; 726 struct spdk_nvmf_poll_group *group; 727 }; 728 729 static void 730 _nvmf_poll_group_add(void *_ctx) 731 { 732 struct nvmf_new_qpair_ctx *ctx = _ctx; 733 struct spdk_nvmf_qpair *qpair = ctx->qpair; 734 struct spdk_nvmf_poll_group *group = ctx->group; 735 736 free(_ctx); 737 738 if (spdk_nvmf_poll_group_add(group, qpair) != 0) { 739 SPDK_ERRLOG("Unable to add the qpair to a poll group.\n"); 740 spdk_nvmf_qpair_disconnect(qpair, NULL, NULL); 741 } 742 } 743 744 void 745 spdk_nvmf_tgt_new_qpair(struct spdk_nvmf_tgt *tgt, struct spdk_nvmf_qpair *qpair) 746 { 747 struct spdk_nvmf_poll_group *group; 748 struct nvmf_new_qpair_ctx *ctx; 749 750 group = spdk_nvmf_get_optimal_poll_group(qpair); 751 if (group == NULL) { 752 if (tgt->next_poll_group == NULL) { 753 tgt->next_poll_group = TAILQ_FIRST(&tgt->poll_groups); 754 if (tgt->next_poll_group == NULL) { 755 SPDK_ERRLOG("No poll groups exist.\n"); 756 spdk_nvmf_qpair_disconnect(qpair, NULL, NULL); 757 return; 758 } 759 } 760 group = tgt->next_poll_group; 761 tgt->next_poll_group = TAILQ_NEXT(group, link); 762 } 763 764 ctx = calloc(1, sizeof(*ctx)); 765 if (!ctx) { 766 SPDK_ERRLOG("Unable to send message to poll group.\n"); 767 spdk_nvmf_qpair_disconnect(qpair, NULL, NULL); 768 return; 769 } 770 771 ctx->qpair = qpair; 772 ctx->group = group; 773 774 spdk_thread_send_msg(group->thread, _nvmf_poll_group_add, ctx); 775 } 776 777 uint32_t 778 spdk_nvmf_tgt_accept(struct spdk_nvmf_tgt *tgt) 779 { 780 struct spdk_nvmf_transport *transport, *tmp; 781 uint32_t count = 0; 782 783 TAILQ_FOREACH_SAFE(transport, &tgt->transports, link, tmp) { 784 count += nvmf_transport_accept(transport); 785 } 786 787 return count; 788 } 789 790 struct spdk_nvmf_poll_group * 791 spdk_nvmf_poll_group_create(struct spdk_nvmf_tgt *tgt) 792 { 793 struct spdk_io_channel *ch; 794 795 ch = spdk_get_io_channel(tgt); 796 if (!ch) { 797 SPDK_ERRLOG("Unable to get I/O channel for target\n"); 798 return NULL; 799 } 800 801 return spdk_io_channel_get_ctx(ch); 802 } 803 804 void 805 spdk_nvmf_poll_group_destroy(struct spdk_nvmf_poll_group *group, 806 spdk_nvmf_poll_group_destroy_done_fn cb_fn, 807 void *cb_arg) 808 { 809 assert(group->destroy_cb_fn == NULL); 810 group->destroy_cb_fn = cb_fn; 811 group->destroy_cb_arg = cb_arg; 812 813 /* This function will put the io_channel associated with this poll group */ 814 nvmf_tgt_destroy_poll_group_qpairs(group); 815 } 816 817 int 818 spdk_nvmf_poll_group_add(struct spdk_nvmf_poll_group *group, 819 struct spdk_nvmf_qpair *qpair) 820 { 821 int rc = -1; 822 struct spdk_nvmf_transport_poll_group *tgroup; 823 824 TAILQ_INIT(&qpair->outstanding); 825 qpair->group = group; 826 827 TAILQ_FOREACH(tgroup, &group->tgroups, link) { 828 if (tgroup->transport == qpair->transport) { 829 rc = nvmf_transport_poll_group_add(tgroup, qpair); 830 break; 831 } 832 } 833 834 /* We add the qpair to the group only it is succesfully added into the tgroup */ 835 if (rc == 0) { 836 TAILQ_INSERT_TAIL(&group->qpairs, qpair, link); 837 nvmf_qpair_set_state(qpair, SPDK_NVMF_QPAIR_ACTIVE); 838 } 839 840 return rc; 841 } 842 843 static 844 void _nvmf_ctrlr_destruct(void *ctx) 845 { 846 struct spdk_nvmf_ctrlr *ctrlr = ctx; 847 848 nvmf_ctrlr_destruct(ctrlr); 849 } 850 851 static void 852 _nvmf_transport_qpair_fini(void *ctx) 853 { 854 struct spdk_nvmf_qpair *qpair = ctx; 855 856 nvmf_transport_qpair_fini(qpair); 857 } 858 859 static void 860 _nvmf_ctrlr_free_from_qpair(void *ctx) 861 { 862 struct nvmf_qpair_disconnect_ctx *qpair_ctx = ctx; 863 struct spdk_nvmf_ctrlr *ctrlr = qpair_ctx->ctrlr; 864 uint32_t count; 865 866 spdk_bit_array_clear(ctrlr->qpair_mask, qpair_ctx->qid); 867 count = spdk_bit_array_count_set(ctrlr->qpair_mask); 868 if (count == 0) { 869 spdk_bit_array_free(&ctrlr->qpair_mask); 870 871 spdk_thread_send_msg(ctrlr->subsys->thread, _nvmf_ctrlr_destruct, ctrlr); 872 } 873 874 spdk_thread_send_msg(qpair_ctx->thread, _nvmf_transport_qpair_fini, qpair_ctx->qpair); 875 if (qpair_ctx->cb_fn) { 876 spdk_thread_send_msg(qpair_ctx->thread, qpair_ctx->cb_fn, qpair_ctx->ctx); 877 } 878 free(qpair_ctx); 879 } 880 881 void 882 spdk_nvmf_poll_group_remove(struct spdk_nvmf_qpair *qpair) 883 { 884 struct spdk_nvmf_ctrlr *ctrlr = qpair->ctrlr; 885 struct spdk_nvmf_transport_poll_group *tgroup; 886 struct spdk_nvmf_request *req, *tmp; 887 struct spdk_nvmf_subsystem_poll_group *sgroup; 888 int rc; 889 890 nvmf_qpair_set_state(qpair, SPDK_NVMF_QPAIR_ERROR); 891 892 /* Find the tgroup and remove the qpair from the tgroup */ 893 TAILQ_FOREACH(tgroup, &qpair->group->tgroups, link) { 894 if (tgroup->transport == qpair->transport) { 895 rc = nvmf_transport_poll_group_remove(tgroup, qpair); 896 if (rc && (rc != ENOTSUP)) { 897 SPDK_ERRLOG("Cannot remove qpair=%p from transport group=%p\n", 898 qpair, tgroup); 899 } 900 break; 901 } 902 } 903 904 if (ctrlr) { 905 sgroup = &qpair->group->sgroups[ctrlr->subsys->id]; 906 TAILQ_FOREACH_SAFE(req, &sgroup->queued, link, tmp) { 907 if (req->qpair == qpair) { 908 TAILQ_REMOVE(&sgroup->queued, req, link); 909 if (nvmf_transport_req_free(req)) { 910 SPDK_ERRLOG("Transport request free error!\n"); 911 } 912 } 913 } 914 } 915 916 TAILQ_REMOVE(&qpair->group->qpairs, qpair, link); 917 qpair->group = NULL; 918 } 919 920 static void 921 _nvmf_qpair_destroy(void *ctx, int status) 922 { 923 struct nvmf_qpair_disconnect_ctx *qpair_ctx = ctx; 924 struct spdk_nvmf_qpair *qpair = qpair_ctx->qpair; 925 struct spdk_nvmf_ctrlr *ctrlr = qpair->ctrlr; 926 927 assert(qpair->state == SPDK_NVMF_QPAIR_DEACTIVATING); 928 qpair_ctx->qid = qpair->qid; 929 930 spdk_nvmf_poll_group_remove(qpair); 931 932 if (!ctrlr || !ctrlr->thread) { 933 nvmf_transport_qpair_fini(qpair); 934 if (qpair_ctx->cb_fn) { 935 spdk_thread_send_msg(qpair_ctx->thread, qpair_ctx->cb_fn, qpair_ctx->ctx); 936 } 937 free(qpair_ctx); 938 return; 939 } 940 941 qpair_ctx->ctrlr = ctrlr; 942 spdk_thread_send_msg(ctrlr->thread, _nvmf_ctrlr_free_from_qpair, qpair_ctx); 943 } 944 945 int 946 spdk_nvmf_qpair_disconnect(struct spdk_nvmf_qpair *qpair, nvmf_qpair_disconnect_cb cb_fn, void *ctx) 947 { 948 struct nvmf_qpair_disconnect_ctx *qpair_ctx; 949 950 /* If we get a qpair in the uninitialized state, we can just destroy it immediately */ 951 if (qpair->state == SPDK_NVMF_QPAIR_UNINITIALIZED) { 952 nvmf_transport_qpair_fini(qpair); 953 if (cb_fn) { 954 cb_fn(ctx); 955 } 956 return 0; 957 } 958 959 /* The queue pair must be disconnected from the thread that owns it */ 960 assert(qpair->group->thread == spdk_get_thread()); 961 962 if (qpair->state != SPDK_NVMF_QPAIR_ACTIVE) { 963 /* This can occur if the connection is killed by the target, 964 * which results in a notification that the connection 965 * died. Send a message to defer the processing of this 966 * callback. This allows the stack to unwind in the case 967 * where a bunch of connections are disconnected in 968 * a loop. */ 969 if (cb_fn) { 970 spdk_thread_send_msg(qpair->group->thread, cb_fn, ctx); 971 } 972 return 0; 973 } 974 975 assert(qpair->state == SPDK_NVMF_QPAIR_ACTIVE); 976 nvmf_qpair_set_state(qpair, SPDK_NVMF_QPAIR_DEACTIVATING); 977 978 qpair_ctx = calloc(1, sizeof(struct nvmf_qpair_disconnect_ctx)); 979 if (!qpair_ctx) { 980 SPDK_ERRLOG("Unable to allocate context for nvmf_qpair_disconnect\n"); 981 return -ENOMEM; 982 } 983 984 qpair_ctx->qpair = qpair; 985 qpair_ctx->cb_fn = cb_fn; 986 qpair_ctx->thread = qpair->group->thread; 987 qpair_ctx->ctx = ctx; 988 989 /* Check for outstanding I/O */ 990 if (!TAILQ_EMPTY(&qpair->outstanding)) { 991 qpair->state_cb = _nvmf_qpair_destroy; 992 qpair->state_cb_arg = qpair_ctx; 993 nvmf_qpair_free_aer(qpair); 994 return 0; 995 } 996 997 _nvmf_qpair_destroy(qpair_ctx, 0); 998 999 return 0; 1000 } 1001 1002 int 1003 spdk_nvmf_qpair_get_peer_trid(struct spdk_nvmf_qpair *qpair, 1004 struct spdk_nvme_transport_id *trid) 1005 { 1006 return nvmf_transport_qpair_get_peer_trid(qpair, trid); 1007 } 1008 1009 int 1010 spdk_nvmf_qpair_get_local_trid(struct spdk_nvmf_qpair *qpair, 1011 struct spdk_nvme_transport_id *trid) 1012 { 1013 return nvmf_transport_qpair_get_local_trid(qpair, trid); 1014 } 1015 1016 int 1017 spdk_nvmf_qpair_get_listen_trid(struct spdk_nvmf_qpair *qpair, 1018 struct spdk_nvme_transport_id *trid) 1019 { 1020 return nvmf_transport_qpair_get_listen_trid(qpair, trid); 1021 } 1022 1023 int 1024 nvmf_poll_group_add_transport(struct spdk_nvmf_poll_group *group, 1025 struct spdk_nvmf_transport *transport) 1026 { 1027 struct spdk_nvmf_transport_poll_group *tgroup; 1028 1029 TAILQ_FOREACH(tgroup, &group->tgroups, link) { 1030 if (tgroup->transport == transport) { 1031 /* Transport already in the poll group */ 1032 return 0; 1033 } 1034 } 1035 1036 tgroup = nvmf_transport_poll_group_create(transport); 1037 if (!tgroup) { 1038 SPDK_ERRLOG("Unable to create poll group for transport\n"); 1039 return -1; 1040 } 1041 1042 tgroup->group = group; 1043 TAILQ_INSERT_TAIL(&group->tgroups, tgroup, link); 1044 1045 return 0; 1046 } 1047 1048 static int 1049 poll_group_update_subsystem(struct spdk_nvmf_poll_group *group, 1050 struct spdk_nvmf_subsystem *subsystem) 1051 { 1052 struct spdk_nvmf_subsystem_poll_group *sgroup; 1053 uint32_t new_num_ns, old_num_ns; 1054 uint32_t i, j; 1055 struct spdk_nvmf_ns *ns; 1056 struct spdk_nvmf_registrant *reg, *tmp; 1057 struct spdk_io_channel *ch; 1058 struct spdk_nvmf_subsystem_pg_ns_info *ns_info; 1059 struct spdk_nvmf_ctrlr *ctrlr; 1060 bool ns_changed; 1061 1062 /* Make sure our poll group has memory for this subsystem allocated */ 1063 if (subsystem->id >= group->num_sgroups) { 1064 return -ENOMEM; 1065 } 1066 1067 sgroup = &group->sgroups[subsystem->id]; 1068 1069 /* Make sure the array of namespace information is the correct size */ 1070 new_num_ns = subsystem->max_nsid; 1071 old_num_ns = sgroup->num_ns; 1072 1073 ns_changed = false; 1074 1075 if (old_num_ns == 0) { 1076 if (new_num_ns > 0) { 1077 /* First allocation */ 1078 sgroup->ns_info = calloc(new_num_ns, sizeof(struct spdk_nvmf_subsystem_pg_ns_info)); 1079 if (!sgroup->ns_info) { 1080 return -ENOMEM; 1081 } 1082 } 1083 } else if (new_num_ns > old_num_ns) { 1084 void *buf; 1085 1086 /* Make the array larger */ 1087 buf = realloc(sgroup->ns_info, new_num_ns * sizeof(struct spdk_nvmf_subsystem_pg_ns_info)); 1088 if (!buf) { 1089 return -ENOMEM; 1090 } 1091 1092 sgroup->ns_info = buf; 1093 1094 /* Null out the new namespace information slots */ 1095 for (i = old_num_ns; i < new_num_ns; i++) { 1096 memset(&sgroup->ns_info[i], 0, sizeof(struct spdk_nvmf_subsystem_pg_ns_info)); 1097 } 1098 } else if (new_num_ns < old_num_ns) { 1099 void *buf; 1100 1101 /* Free the extra I/O channels */ 1102 for (i = new_num_ns; i < old_num_ns; i++) { 1103 ns_info = &sgroup->ns_info[i]; 1104 1105 if (ns_info->channel) { 1106 spdk_put_io_channel(ns_info->channel); 1107 ns_info->channel = NULL; 1108 } 1109 } 1110 1111 /* Make the array smaller */ 1112 if (new_num_ns > 0) { 1113 buf = realloc(sgroup->ns_info, new_num_ns * sizeof(struct spdk_nvmf_subsystem_pg_ns_info)); 1114 if (!buf) { 1115 return -ENOMEM; 1116 } 1117 sgroup->ns_info = buf; 1118 } else { 1119 free(sgroup->ns_info); 1120 sgroup->ns_info = NULL; 1121 } 1122 } 1123 1124 sgroup->num_ns = new_num_ns; 1125 1126 /* Detect bdevs that were added or removed */ 1127 for (i = 0; i < sgroup->num_ns; i++) { 1128 ns = subsystem->ns[i]; 1129 ns_info = &sgroup->ns_info[i]; 1130 ch = ns_info->channel; 1131 1132 if (ns == NULL && ch == NULL) { 1133 /* Both NULL. Leave empty */ 1134 } else if (ns == NULL && ch != NULL) { 1135 /* There was a channel here, but the namespace is gone. */ 1136 ns_changed = true; 1137 spdk_put_io_channel(ch); 1138 ns_info->channel = NULL; 1139 } else if (ns != NULL && ch == NULL) { 1140 /* A namespace appeared but there is no channel yet */ 1141 ns_changed = true; 1142 ch = spdk_bdev_get_io_channel(ns->desc); 1143 if (ch == NULL) { 1144 SPDK_ERRLOG("Could not allocate I/O channel.\n"); 1145 return -ENOMEM; 1146 } 1147 ns_info->channel = ch; 1148 } else if (spdk_uuid_compare(&ns_info->uuid, spdk_bdev_get_uuid(ns->bdev)) != 0) { 1149 /* A namespace was here before, but was replaced by a new one. */ 1150 ns_changed = true; 1151 spdk_put_io_channel(ns_info->channel); 1152 memset(ns_info, 0, sizeof(*ns_info)); 1153 1154 ch = spdk_bdev_get_io_channel(ns->desc); 1155 if (ch == NULL) { 1156 SPDK_ERRLOG("Could not allocate I/O channel.\n"); 1157 return -ENOMEM; 1158 } 1159 ns_info->channel = ch; 1160 } else if (ns_info->num_blocks != spdk_bdev_get_num_blocks(ns->bdev)) { 1161 /* Namespace is still there but size has changed */ 1162 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Namespace resized: subsystem_id %d," 1163 " nsid %u, pg %p, old %lu, new %lu\n", 1164 subsystem->id, 1165 ns->nsid, 1166 group, 1167 ns_info->num_blocks, 1168 spdk_bdev_get_num_blocks(ns->bdev)); 1169 ns_changed = true; 1170 } 1171 1172 if (ns == NULL) { 1173 memset(ns_info, 0, sizeof(*ns_info)); 1174 } else { 1175 ns_info->uuid = *spdk_bdev_get_uuid(ns->bdev); 1176 ns_info->num_blocks = spdk_bdev_get_num_blocks(ns->bdev); 1177 ns_info->crkey = ns->crkey; 1178 ns_info->rtype = ns->rtype; 1179 if (ns->holder) { 1180 ns_info->holder_id = ns->holder->hostid; 1181 } 1182 1183 memset(&ns_info->reg_hostid, 0, SPDK_NVMF_MAX_NUM_REGISTRANTS * sizeof(struct spdk_uuid)); 1184 j = 0; 1185 TAILQ_FOREACH_SAFE(reg, &ns->registrants, link, tmp) { 1186 if (j >= SPDK_NVMF_MAX_NUM_REGISTRANTS) { 1187 SPDK_ERRLOG("Maximum %u registrants can support.\n", SPDK_NVMF_MAX_NUM_REGISTRANTS); 1188 return -EINVAL; 1189 } 1190 ns_info->reg_hostid[j++] = reg->hostid; 1191 } 1192 } 1193 } 1194 1195 if (ns_changed) { 1196 TAILQ_FOREACH(ctrlr, &subsystem->ctrlrs, link) { 1197 if (ctrlr->admin_qpair->group == group) { 1198 nvmf_ctrlr_async_event_ns_notice(ctrlr); 1199 } 1200 } 1201 } 1202 1203 return 0; 1204 } 1205 1206 int 1207 nvmf_poll_group_update_subsystem(struct spdk_nvmf_poll_group *group, 1208 struct spdk_nvmf_subsystem *subsystem) 1209 { 1210 return poll_group_update_subsystem(group, subsystem); 1211 } 1212 1213 int 1214 nvmf_poll_group_add_subsystem(struct spdk_nvmf_poll_group *group, 1215 struct spdk_nvmf_subsystem *subsystem, 1216 spdk_nvmf_poll_group_mod_done cb_fn, void *cb_arg) 1217 { 1218 int rc = 0; 1219 struct spdk_nvmf_subsystem_poll_group *sgroup = &group->sgroups[subsystem->id]; 1220 1221 TAILQ_INIT(&sgroup->queued); 1222 1223 rc = poll_group_update_subsystem(group, subsystem); 1224 if (rc) { 1225 nvmf_poll_group_remove_subsystem(group, subsystem, NULL, NULL); 1226 goto fini; 1227 } 1228 1229 sgroup->state = SPDK_NVMF_SUBSYSTEM_ACTIVE; 1230 fini: 1231 if (cb_fn) { 1232 cb_fn(cb_arg, rc); 1233 } 1234 1235 return rc; 1236 } 1237 1238 static void 1239 _nvmf_poll_group_remove_subsystem_cb(void *ctx, int status) 1240 { 1241 struct nvmf_qpair_disconnect_many_ctx *qpair_ctx = ctx; 1242 struct spdk_nvmf_subsystem *subsystem; 1243 struct spdk_nvmf_poll_group *group; 1244 struct spdk_nvmf_subsystem_poll_group *sgroup; 1245 spdk_nvmf_poll_group_mod_done cpl_fn = NULL; 1246 void *cpl_ctx = NULL; 1247 uint32_t nsid; 1248 1249 group = qpair_ctx->group; 1250 subsystem = qpair_ctx->subsystem; 1251 cpl_fn = qpair_ctx->cpl_fn; 1252 cpl_ctx = qpair_ctx->cpl_ctx; 1253 sgroup = &group->sgroups[subsystem->id]; 1254 1255 if (status) { 1256 goto fini; 1257 } 1258 1259 for (nsid = 0; nsid < sgroup->num_ns; nsid++) { 1260 if (sgroup->ns_info[nsid].channel) { 1261 spdk_put_io_channel(sgroup->ns_info[nsid].channel); 1262 sgroup->ns_info[nsid].channel = NULL; 1263 } 1264 } 1265 1266 sgroup->num_ns = 0; 1267 free(sgroup->ns_info); 1268 sgroup->ns_info = NULL; 1269 fini: 1270 free(qpair_ctx); 1271 if (cpl_fn) { 1272 cpl_fn(cpl_ctx, status); 1273 } 1274 } 1275 1276 static void 1277 _nvmf_subsystem_disconnect_next_qpair(void *ctx) 1278 { 1279 struct spdk_nvmf_qpair *qpair; 1280 struct nvmf_qpair_disconnect_many_ctx *qpair_ctx = ctx; 1281 struct spdk_nvmf_subsystem *subsystem; 1282 struct spdk_nvmf_poll_group *group; 1283 int rc = 0; 1284 1285 group = qpair_ctx->group; 1286 subsystem = qpair_ctx->subsystem; 1287 1288 TAILQ_FOREACH(qpair, &group->qpairs, link) { 1289 if ((qpair->ctrlr != NULL) && (qpair->ctrlr->subsys == subsystem)) { 1290 break; 1291 } 1292 } 1293 1294 if (qpair) { 1295 rc = spdk_nvmf_qpair_disconnect(qpair, _nvmf_subsystem_disconnect_next_qpair, qpair_ctx); 1296 } 1297 1298 if (!qpair || rc != 0) { 1299 _nvmf_poll_group_remove_subsystem_cb(ctx, rc); 1300 } 1301 return; 1302 } 1303 1304 void 1305 nvmf_poll_group_remove_subsystem(struct spdk_nvmf_poll_group *group, 1306 struct spdk_nvmf_subsystem *subsystem, 1307 spdk_nvmf_poll_group_mod_done cb_fn, void *cb_arg) 1308 { 1309 struct spdk_nvmf_qpair *qpair; 1310 struct spdk_nvmf_subsystem_poll_group *sgroup; 1311 struct nvmf_qpair_disconnect_many_ctx *ctx; 1312 int rc = 0; 1313 1314 ctx = calloc(1, sizeof(struct nvmf_qpair_disconnect_many_ctx)); 1315 1316 if (!ctx) { 1317 SPDK_ERRLOG("Unable to allocate memory for context to remove poll subsystem\n"); 1318 goto fini; 1319 } 1320 1321 ctx->group = group; 1322 ctx->subsystem = subsystem; 1323 ctx->cpl_fn = cb_fn; 1324 ctx->cpl_ctx = cb_arg; 1325 1326 sgroup = &group->sgroups[subsystem->id]; 1327 sgroup->state = SPDK_NVMF_SUBSYSTEM_INACTIVE; 1328 1329 TAILQ_FOREACH(qpair, &group->qpairs, link) { 1330 if ((qpair->ctrlr != NULL) && (qpair->ctrlr->subsys == subsystem)) { 1331 break; 1332 } 1333 } 1334 1335 if (qpair) { 1336 rc = spdk_nvmf_qpair_disconnect(qpair, _nvmf_subsystem_disconnect_next_qpair, ctx); 1337 } else { 1338 /* call the callback immediately. It will handle any channel iteration */ 1339 _nvmf_poll_group_remove_subsystem_cb(ctx, 0); 1340 } 1341 1342 if (rc != 0) { 1343 free(ctx); 1344 goto fini; 1345 } 1346 1347 return; 1348 fini: 1349 if (cb_fn) { 1350 cb_fn(cb_arg, rc); 1351 } 1352 } 1353 1354 void 1355 nvmf_poll_group_pause_subsystem(struct spdk_nvmf_poll_group *group, 1356 struct spdk_nvmf_subsystem *subsystem, 1357 spdk_nvmf_poll_group_mod_done cb_fn, void *cb_arg) 1358 { 1359 struct spdk_nvmf_subsystem_poll_group *sgroup; 1360 int rc = 0; 1361 1362 if (subsystem->id >= group->num_sgroups) { 1363 rc = -1; 1364 goto fini; 1365 } 1366 1367 sgroup = &group->sgroups[subsystem->id]; 1368 if (sgroup == NULL) { 1369 rc = -1; 1370 goto fini; 1371 } 1372 1373 assert(sgroup->state == SPDK_NVMF_SUBSYSTEM_ACTIVE); 1374 sgroup->state = SPDK_NVMF_SUBSYSTEM_PAUSING; 1375 1376 if (sgroup->io_outstanding > 0) { 1377 sgroup->cb_fn = cb_fn; 1378 sgroup->cb_arg = cb_arg; 1379 return; 1380 } 1381 1382 assert(sgroup->io_outstanding == 0); 1383 sgroup->state = SPDK_NVMF_SUBSYSTEM_PAUSED; 1384 fini: 1385 if (cb_fn) { 1386 cb_fn(cb_arg, rc); 1387 } 1388 } 1389 1390 void 1391 nvmf_poll_group_resume_subsystem(struct spdk_nvmf_poll_group *group, 1392 struct spdk_nvmf_subsystem *subsystem, 1393 spdk_nvmf_poll_group_mod_done cb_fn, void *cb_arg) 1394 { 1395 struct spdk_nvmf_request *req, *tmp; 1396 struct spdk_nvmf_subsystem_poll_group *sgroup; 1397 int rc = 0; 1398 1399 if (subsystem->id >= group->num_sgroups) { 1400 rc = -1; 1401 goto fini; 1402 } 1403 1404 sgroup = &group->sgroups[subsystem->id]; 1405 1406 assert(sgroup->state == SPDK_NVMF_SUBSYSTEM_PAUSED); 1407 1408 rc = poll_group_update_subsystem(group, subsystem); 1409 if (rc) { 1410 goto fini; 1411 } 1412 1413 sgroup->state = SPDK_NVMF_SUBSYSTEM_ACTIVE; 1414 1415 /* Release all queued requests */ 1416 TAILQ_FOREACH_SAFE(req, &sgroup->queued, link, tmp) { 1417 TAILQ_REMOVE(&sgroup->queued, req, link); 1418 spdk_nvmf_request_exec(req); 1419 } 1420 fini: 1421 if (cb_fn) { 1422 cb_fn(cb_arg, rc); 1423 } 1424 } 1425 1426 1427 struct spdk_nvmf_poll_group * 1428 spdk_nvmf_get_optimal_poll_group(struct spdk_nvmf_qpair *qpair) 1429 { 1430 struct spdk_nvmf_transport_poll_group *tgroup; 1431 1432 tgroup = nvmf_transport_get_optimal_poll_group(qpair->transport, qpair); 1433 1434 if (tgroup == NULL) { 1435 return NULL; 1436 } 1437 1438 return tgroup->group; 1439 } 1440 1441 int 1442 spdk_nvmf_poll_group_get_stat(struct spdk_nvmf_tgt *tgt, 1443 struct spdk_nvmf_poll_group_stat *stat) 1444 { 1445 struct spdk_io_channel *ch; 1446 struct spdk_nvmf_poll_group *group; 1447 1448 if (tgt == NULL || stat == NULL) { 1449 return -EINVAL; 1450 } 1451 1452 ch = spdk_get_io_channel(tgt); 1453 group = spdk_io_channel_get_ctx(ch); 1454 *stat = group->stat; 1455 spdk_put_io_channel(ch); 1456 return 0; 1457 } 1458