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_object_end(w); 550 551 spdk_json_write_object_end(w); 552 } 553 554 subsystem = spdk_nvmf_subsystem_get_first(tgt); 555 while (subsystem) { 556 nvmf_write_subsystem_config_json(w, subsystem); 557 subsystem = spdk_nvmf_subsystem_get_next(subsystem); 558 } 559 } 560 561 int 562 spdk_nvmf_tgt_listen(struct spdk_nvmf_tgt *tgt, 563 struct spdk_nvme_transport_id *trid) 564 { 565 struct spdk_nvmf_transport *transport; 566 const char *trtype; 567 int rc; 568 569 transport = spdk_nvmf_tgt_get_transport(tgt, trid->trstring); 570 if (!transport) { 571 trtype = spdk_nvme_transport_id_trtype_str(trid->trtype); 572 if (trtype != NULL) { 573 SPDK_ERRLOG("Unable to listen on transport %s. The transport must be created first.\n", trtype); 574 } else { 575 SPDK_ERRLOG("The specified trtype %d is unknown. Please make sure that it is properly registered.\n", 576 trid->trtype); 577 } 578 579 return -EINVAL; 580 } 581 582 rc = spdk_nvmf_transport_listen(transport, trid); 583 if (rc < 0) { 584 SPDK_ERRLOG("Unable to listen on address '%s'\n", trid->traddr); 585 } 586 587 return rc; 588 } 589 590 int 591 spdk_nvmf_tgt_stop_listen(struct spdk_nvmf_tgt *tgt, 592 struct spdk_nvme_transport_id *trid) 593 { 594 struct spdk_nvmf_transport *transport; 595 const char *trtype; 596 int rc; 597 598 transport = spdk_nvmf_tgt_get_transport(tgt, trid->trstring); 599 if (!transport) { 600 trtype = spdk_nvme_transport_id_trtype_str(trid->trtype); 601 if (trtype != NULL) { 602 SPDK_ERRLOG("Unable to stop listen on transport %s. The transport must be created first.\n", 603 trtype); 604 } else { 605 SPDK_ERRLOG("The specified trtype %d is unknown. Please make sure that it is properly registered.\n", 606 trid->trtype); 607 } 608 return -EINVAL; 609 } 610 611 rc = spdk_nvmf_transport_stop_listen(transport, trid); 612 if (rc < 0) { 613 SPDK_ERRLOG("Failed to stop listening on address '%s'\n", trid->traddr); 614 return rc; 615 } 616 return 0; 617 } 618 619 struct spdk_nvmf_tgt_add_transport_ctx { 620 struct spdk_nvmf_tgt *tgt; 621 struct spdk_nvmf_transport *transport; 622 spdk_nvmf_tgt_add_transport_done_fn cb_fn; 623 void *cb_arg; 624 }; 625 626 static void 627 _nvmf_tgt_add_transport_done(struct spdk_io_channel_iter *i, int status) 628 { 629 struct spdk_nvmf_tgt_add_transport_ctx *ctx = spdk_io_channel_iter_get_ctx(i); 630 631 ctx->cb_fn(ctx->cb_arg, status); 632 633 free(ctx); 634 } 635 636 static void 637 _nvmf_tgt_add_transport(struct spdk_io_channel_iter *i) 638 { 639 struct spdk_nvmf_tgt_add_transport_ctx *ctx = spdk_io_channel_iter_get_ctx(i); 640 struct spdk_io_channel *ch = spdk_io_channel_iter_get_channel(i); 641 struct spdk_nvmf_poll_group *group = spdk_io_channel_get_ctx(ch); 642 int rc; 643 644 rc = nvmf_poll_group_add_transport(group, ctx->transport); 645 spdk_for_each_channel_continue(i, rc); 646 } 647 648 void spdk_nvmf_tgt_add_transport(struct spdk_nvmf_tgt *tgt, 649 struct spdk_nvmf_transport *transport, 650 spdk_nvmf_tgt_add_transport_done_fn cb_fn, 651 void *cb_arg) 652 { 653 struct spdk_nvmf_tgt_add_transport_ctx *ctx; 654 655 if (spdk_nvmf_tgt_get_transport(tgt, transport->ops->name)) { 656 cb_fn(cb_arg, -EEXIST); 657 return; /* transport already created */ 658 } 659 660 transport->tgt = tgt; 661 TAILQ_INSERT_TAIL(&tgt->transports, transport, link); 662 663 ctx = calloc(1, sizeof(*ctx)); 664 if (!ctx) { 665 cb_fn(cb_arg, -ENOMEM); 666 return; 667 } 668 669 ctx->tgt = tgt; 670 ctx->transport = transport; 671 ctx->cb_fn = cb_fn; 672 ctx->cb_arg = cb_arg; 673 674 spdk_for_each_channel(tgt, 675 _nvmf_tgt_add_transport, 676 ctx, 677 _nvmf_tgt_add_transport_done); 678 } 679 680 struct spdk_nvmf_subsystem * 681 spdk_nvmf_tgt_find_subsystem(struct spdk_nvmf_tgt *tgt, const char *subnqn) 682 { 683 struct spdk_nvmf_subsystem *subsystem; 684 uint32_t sid; 685 686 if (!subnqn) { 687 return NULL; 688 } 689 690 /* Ensure that subnqn is null terminated */ 691 if (!memchr(subnqn, '\0', SPDK_NVMF_NQN_MAX_LEN + 1)) { 692 SPDK_ERRLOG("Connect SUBNQN is not null terminated\n"); 693 return NULL; 694 } 695 696 for (sid = 0; sid < tgt->max_subsystems; sid++) { 697 subsystem = tgt->subsystems[sid]; 698 if (subsystem == NULL) { 699 continue; 700 } 701 702 if (strcmp(subnqn, subsystem->subnqn) == 0) { 703 return subsystem; 704 } 705 } 706 707 return NULL; 708 } 709 710 struct spdk_nvmf_transport * 711 spdk_nvmf_tgt_get_transport(struct spdk_nvmf_tgt *tgt, const char *transport_name) 712 { 713 struct spdk_nvmf_transport *transport; 714 715 TAILQ_FOREACH(transport, &tgt->transports, link) { 716 if (!strncasecmp(transport->ops->name, transport_name, SPDK_NVMF_TRSTRING_MAX_LEN)) { 717 return transport; 718 } 719 } 720 return NULL; 721 } 722 723 struct nvmf_new_qpair_ctx { 724 struct spdk_nvmf_qpair *qpair; 725 struct spdk_nvmf_poll_group *group; 726 }; 727 728 static void 729 _nvmf_poll_group_add(void *_ctx) 730 { 731 struct nvmf_new_qpair_ctx *ctx = _ctx; 732 struct spdk_nvmf_qpair *qpair = ctx->qpair; 733 struct spdk_nvmf_poll_group *group = ctx->group; 734 735 free(_ctx); 736 737 if (spdk_nvmf_poll_group_add(group, qpair) != 0) { 738 SPDK_ERRLOG("Unable to add the qpair to a poll group.\n"); 739 spdk_nvmf_qpair_disconnect(qpair, NULL, NULL); 740 } 741 } 742 743 void 744 spdk_nvmf_tgt_new_qpair(struct spdk_nvmf_tgt *tgt, struct spdk_nvmf_qpair *qpair) 745 { 746 struct spdk_nvmf_poll_group *group; 747 struct nvmf_new_qpair_ctx *ctx; 748 749 group = spdk_nvmf_get_optimal_poll_group(qpair); 750 if (group == NULL) { 751 if (tgt->next_poll_group == NULL) { 752 tgt->next_poll_group = TAILQ_FIRST(&tgt->poll_groups); 753 if (tgt->next_poll_group == NULL) { 754 SPDK_ERRLOG("No poll groups exist.\n"); 755 spdk_nvmf_qpair_disconnect(qpair, NULL, NULL); 756 return; 757 } 758 } 759 group = tgt->next_poll_group; 760 tgt->next_poll_group = TAILQ_NEXT(group, link); 761 } 762 763 ctx = calloc(1, sizeof(*ctx)); 764 if (!ctx) { 765 SPDK_ERRLOG("Unable to send message to poll group.\n"); 766 spdk_nvmf_qpair_disconnect(qpair, NULL, NULL); 767 return; 768 } 769 770 ctx->qpair = qpair; 771 ctx->group = group; 772 773 spdk_thread_send_msg(group->thread, _nvmf_poll_group_add, ctx); 774 } 775 776 uint32_t 777 spdk_nvmf_tgt_accept(struct spdk_nvmf_tgt *tgt) 778 { 779 struct spdk_nvmf_transport *transport, *tmp; 780 uint32_t count = 0; 781 782 TAILQ_FOREACH_SAFE(transport, &tgt->transports, link, tmp) { 783 count += nvmf_transport_accept(transport); 784 } 785 786 return count; 787 } 788 789 struct spdk_nvmf_poll_group * 790 spdk_nvmf_poll_group_create(struct spdk_nvmf_tgt *tgt) 791 { 792 struct spdk_io_channel *ch; 793 794 ch = spdk_get_io_channel(tgt); 795 if (!ch) { 796 SPDK_ERRLOG("Unable to get I/O channel for target\n"); 797 return NULL; 798 } 799 800 return spdk_io_channel_get_ctx(ch); 801 } 802 803 void 804 spdk_nvmf_poll_group_destroy(struct spdk_nvmf_poll_group *group, 805 spdk_nvmf_poll_group_destroy_done_fn cb_fn, 806 void *cb_arg) 807 { 808 assert(group->destroy_cb_fn == NULL); 809 group->destroy_cb_fn = cb_fn; 810 group->destroy_cb_arg = cb_arg; 811 812 /* This function will put the io_channel associated with this poll group */ 813 nvmf_tgt_destroy_poll_group_qpairs(group); 814 } 815 816 int 817 spdk_nvmf_poll_group_add(struct spdk_nvmf_poll_group *group, 818 struct spdk_nvmf_qpair *qpair) 819 { 820 int rc = -1; 821 struct spdk_nvmf_transport_poll_group *tgroup; 822 823 TAILQ_INIT(&qpair->outstanding); 824 qpair->group = group; 825 826 TAILQ_FOREACH(tgroup, &group->tgroups, link) { 827 if (tgroup->transport == qpair->transport) { 828 rc = nvmf_transport_poll_group_add(tgroup, qpair); 829 break; 830 } 831 } 832 833 /* We add the qpair to the group only it is succesfully added into the tgroup */ 834 if (rc == 0) { 835 TAILQ_INSERT_TAIL(&group->qpairs, qpair, link); 836 nvmf_qpair_set_state(qpair, SPDK_NVMF_QPAIR_ACTIVE); 837 } 838 839 return rc; 840 } 841 842 static 843 void _nvmf_ctrlr_destruct(void *ctx) 844 { 845 struct spdk_nvmf_ctrlr *ctrlr = ctx; 846 847 nvmf_ctrlr_destruct(ctrlr); 848 } 849 850 static void 851 _nvmf_transport_qpair_fini(void *ctx) 852 { 853 struct spdk_nvmf_qpair *qpair = ctx; 854 855 nvmf_transport_qpair_fini(qpair); 856 } 857 858 static void 859 _nvmf_ctrlr_free_from_qpair(void *ctx) 860 { 861 struct nvmf_qpair_disconnect_ctx *qpair_ctx = ctx; 862 struct spdk_nvmf_ctrlr *ctrlr = qpair_ctx->ctrlr; 863 uint32_t count; 864 865 spdk_bit_array_clear(ctrlr->qpair_mask, qpair_ctx->qid); 866 count = spdk_bit_array_count_set(ctrlr->qpair_mask); 867 if (count == 0) { 868 spdk_bit_array_free(&ctrlr->qpair_mask); 869 870 spdk_thread_send_msg(ctrlr->subsys->thread, _nvmf_ctrlr_destruct, ctrlr); 871 } 872 873 spdk_thread_send_msg(qpair_ctx->thread, _nvmf_transport_qpair_fini, qpair_ctx->qpair); 874 if (qpair_ctx->cb_fn) { 875 spdk_thread_send_msg(qpair_ctx->thread, qpair_ctx->cb_fn, qpair_ctx->ctx); 876 } 877 free(qpair_ctx); 878 } 879 880 void 881 spdk_nvmf_poll_group_remove(struct spdk_nvmf_qpair *qpair) 882 { 883 struct spdk_nvmf_ctrlr *ctrlr = qpair->ctrlr; 884 struct spdk_nvmf_transport_poll_group *tgroup; 885 struct spdk_nvmf_request *req, *tmp; 886 struct spdk_nvmf_subsystem_poll_group *sgroup; 887 int rc; 888 889 nvmf_qpair_set_state(qpair, SPDK_NVMF_QPAIR_ERROR); 890 891 /* Find the tgroup and remove the qpair from the tgroup */ 892 TAILQ_FOREACH(tgroup, &qpair->group->tgroups, link) { 893 if (tgroup->transport == qpair->transport) { 894 rc = nvmf_transport_poll_group_remove(tgroup, qpair); 895 if (rc && (rc != ENOTSUP)) { 896 SPDK_ERRLOG("Cannot remove qpair=%p from transport group=%p\n", 897 qpair, tgroup); 898 } 899 break; 900 } 901 } 902 903 if (ctrlr) { 904 sgroup = &qpair->group->sgroups[ctrlr->subsys->id]; 905 TAILQ_FOREACH_SAFE(req, &sgroup->queued, link, tmp) { 906 if (req->qpair == qpair) { 907 TAILQ_REMOVE(&sgroup->queued, req, link); 908 if (nvmf_transport_req_free(req)) { 909 SPDK_ERRLOG("Transport request free error!\n"); 910 } 911 } 912 } 913 } 914 915 TAILQ_REMOVE(&qpair->group->qpairs, qpair, link); 916 } 917 918 static void 919 _nvmf_qpair_destroy(void *ctx, int status) 920 { 921 struct nvmf_qpair_disconnect_ctx *qpair_ctx = ctx; 922 struct spdk_nvmf_qpair *qpair = qpair_ctx->qpair; 923 struct spdk_nvmf_ctrlr *ctrlr = qpair->ctrlr; 924 925 assert(qpair->state == SPDK_NVMF_QPAIR_DEACTIVATING); 926 qpair_ctx->qid = qpair->qid; 927 928 spdk_nvmf_poll_group_remove(qpair); 929 930 if (!ctrlr || !ctrlr->thread) { 931 nvmf_transport_qpair_fini(qpair); 932 if (qpair_ctx->cb_fn) { 933 spdk_thread_send_msg(qpair_ctx->thread, qpair_ctx->cb_fn, qpair_ctx->ctx); 934 } 935 free(qpair_ctx); 936 return; 937 } 938 939 qpair_ctx->ctrlr = ctrlr; 940 spdk_thread_send_msg(ctrlr->thread, _nvmf_ctrlr_free_from_qpair, qpair_ctx); 941 } 942 943 int 944 spdk_nvmf_qpair_disconnect(struct spdk_nvmf_qpair *qpair, nvmf_qpair_disconnect_cb cb_fn, void *ctx) 945 { 946 struct nvmf_qpair_disconnect_ctx *qpair_ctx; 947 948 /* If we get a qpair in the uninitialized state, we can just destroy it immediately */ 949 if (qpair->state == SPDK_NVMF_QPAIR_UNINITIALIZED) { 950 nvmf_transport_qpair_fini(qpair); 951 if (cb_fn) { 952 cb_fn(ctx); 953 } 954 return 0; 955 } 956 957 /* The queue pair must be disconnected from the thread that owns it */ 958 assert(qpair->group->thread == spdk_get_thread()); 959 960 if (qpair->state != SPDK_NVMF_QPAIR_ACTIVE) { 961 /* This can occur if the connection is killed by the target, 962 * which results in a notification that the connection 963 * died. Send a message to defer the processing of this 964 * callback. This allows the stack to unwind in the case 965 * where a bunch of connections are disconnected in 966 * a loop. */ 967 if (cb_fn) { 968 spdk_thread_send_msg(qpair->group->thread, cb_fn, ctx); 969 } 970 return 0; 971 } 972 973 assert(qpair->state == SPDK_NVMF_QPAIR_ACTIVE); 974 nvmf_qpair_set_state(qpair, SPDK_NVMF_QPAIR_DEACTIVATING); 975 976 qpair_ctx = calloc(1, sizeof(struct nvmf_qpair_disconnect_ctx)); 977 if (!qpair_ctx) { 978 SPDK_ERRLOG("Unable to allocate context for nvmf_qpair_disconnect\n"); 979 return -ENOMEM; 980 } 981 982 qpair_ctx->qpair = qpair; 983 qpair_ctx->cb_fn = cb_fn; 984 qpair_ctx->thread = qpair->group->thread; 985 qpair_ctx->ctx = ctx; 986 987 /* Check for outstanding I/O */ 988 if (!TAILQ_EMPTY(&qpair->outstanding)) { 989 qpair->state_cb = _nvmf_qpair_destroy; 990 qpair->state_cb_arg = qpair_ctx; 991 nvmf_qpair_free_aer(qpair); 992 return 0; 993 } 994 995 _nvmf_qpair_destroy(qpair_ctx, 0); 996 997 return 0; 998 } 999 1000 int 1001 spdk_nvmf_qpair_get_peer_trid(struct spdk_nvmf_qpair *qpair, 1002 struct spdk_nvme_transport_id *trid) 1003 { 1004 return nvmf_transport_qpair_get_peer_trid(qpair, trid); 1005 } 1006 1007 int 1008 spdk_nvmf_qpair_get_local_trid(struct spdk_nvmf_qpair *qpair, 1009 struct spdk_nvme_transport_id *trid) 1010 { 1011 return nvmf_transport_qpair_get_local_trid(qpair, trid); 1012 } 1013 1014 int 1015 spdk_nvmf_qpair_get_listen_trid(struct spdk_nvmf_qpair *qpair, 1016 struct spdk_nvme_transport_id *trid) 1017 { 1018 return nvmf_transport_qpair_get_listen_trid(qpair, trid); 1019 } 1020 1021 int 1022 nvmf_poll_group_add_transport(struct spdk_nvmf_poll_group *group, 1023 struct spdk_nvmf_transport *transport) 1024 { 1025 struct spdk_nvmf_transport_poll_group *tgroup; 1026 1027 TAILQ_FOREACH(tgroup, &group->tgroups, link) { 1028 if (tgroup->transport == transport) { 1029 /* Transport already in the poll group */ 1030 return 0; 1031 } 1032 } 1033 1034 tgroup = nvmf_transport_poll_group_create(transport); 1035 if (!tgroup) { 1036 SPDK_ERRLOG("Unable to create poll group for transport\n"); 1037 return -1; 1038 } 1039 1040 tgroup->group = group; 1041 TAILQ_INSERT_TAIL(&group->tgroups, tgroup, link); 1042 1043 return 0; 1044 } 1045 1046 static int 1047 poll_group_update_subsystem(struct spdk_nvmf_poll_group *group, 1048 struct spdk_nvmf_subsystem *subsystem) 1049 { 1050 struct spdk_nvmf_subsystem_poll_group *sgroup; 1051 uint32_t new_num_ns, old_num_ns; 1052 uint32_t i, j; 1053 struct spdk_nvmf_ns *ns; 1054 struct spdk_nvmf_registrant *reg, *tmp; 1055 struct spdk_io_channel *ch; 1056 struct spdk_nvmf_subsystem_pg_ns_info *ns_info; 1057 struct spdk_nvmf_ctrlr *ctrlr; 1058 bool ns_changed; 1059 1060 /* Make sure our poll group has memory for this subsystem allocated */ 1061 if (subsystem->id >= group->num_sgroups) { 1062 return -ENOMEM; 1063 } 1064 1065 sgroup = &group->sgroups[subsystem->id]; 1066 1067 /* Make sure the array of namespace information is the correct size */ 1068 new_num_ns = subsystem->max_nsid; 1069 old_num_ns = sgroup->num_ns; 1070 1071 ns_changed = false; 1072 1073 if (old_num_ns == 0) { 1074 if (new_num_ns > 0) { 1075 /* First allocation */ 1076 sgroup->ns_info = calloc(new_num_ns, sizeof(struct spdk_nvmf_subsystem_pg_ns_info)); 1077 if (!sgroup->ns_info) { 1078 return -ENOMEM; 1079 } 1080 } 1081 } else if (new_num_ns > old_num_ns) { 1082 void *buf; 1083 1084 /* Make the array larger */ 1085 buf = realloc(sgroup->ns_info, new_num_ns * sizeof(struct spdk_nvmf_subsystem_pg_ns_info)); 1086 if (!buf) { 1087 return -ENOMEM; 1088 } 1089 1090 sgroup->ns_info = buf; 1091 1092 /* Null out the new namespace information slots */ 1093 for (i = old_num_ns; i < new_num_ns; i++) { 1094 memset(&sgroup->ns_info[i], 0, sizeof(struct spdk_nvmf_subsystem_pg_ns_info)); 1095 } 1096 } else if (new_num_ns < old_num_ns) { 1097 void *buf; 1098 1099 /* Free the extra I/O channels */ 1100 for (i = new_num_ns; i < old_num_ns; i++) { 1101 ns_info = &sgroup->ns_info[i]; 1102 1103 if (ns_info->channel) { 1104 spdk_put_io_channel(ns_info->channel); 1105 ns_info->channel = NULL; 1106 } 1107 } 1108 1109 /* Make the array smaller */ 1110 if (new_num_ns > 0) { 1111 buf = realloc(sgroup->ns_info, new_num_ns * sizeof(struct spdk_nvmf_subsystem_pg_ns_info)); 1112 if (!buf) { 1113 return -ENOMEM; 1114 } 1115 sgroup->ns_info = buf; 1116 } else { 1117 free(sgroup->ns_info); 1118 sgroup->ns_info = NULL; 1119 } 1120 } 1121 1122 sgroup->num_ns = new_num_ns; 1123 1124 /* Detect bdevs that were added or removed */ 1125 for (i = 0; i < sgroup->num_ns; i++) { 1126 ns = subsystem->ns[i]; 1127 ns_info = &sgroup->ns_info[i]; 1128 ch = ns_info->channel; 1129 1130 if (ns == NULL && ch == NULL) { 1131 /* Both NULL. Leave empty */ 1132 } else if (ns == NULL && ch != NULL) { 1133 /* There was a channel here, but the namespace is gone. */ 1134 ns_changed = true; 1135 spdk_put_io_channel(ch); 1136 ns_info->channel = NULL; 1137 } else if (ns != NULL && ch == NULL) { 1138 /* A namespace appeared but there is no channel yet */ 1139 ns_changed = true; 1140 ch = spdk_bdev_get_io_channel(ns->desc); 1141 if (ch == NULL) { 1142 SPDK_ERRLOG("Could not allocate I/O channel.\n"); 1143 return -ENOMEM; 1144 } 1145 ns_info->channel = ch; 1146 } else if (spdk_uuid_compare(&ns_info->uuid, spdk_bdev_get_uuid(ns->bdev)) != 0) { 1147 /* A namespace was here before, but was replaced by a new one. */ 1148 ns_changed = true; 1149 spdk_put_io_channel(ns_info->channel); 1150 memset(ns_info, 0, sizeof(*ns_info)); 1151 1152 ch = spdk_bdev_get_io_channel(ns->desc); 1153 if (ch == NULL) { 1154 SPDK_ERRLOG("Could not allocate I/O channel.\n"); 1155 return -ENOMEM; 1156 } 1157 ns_info->channel = ch; 1158 } else if (ns_info->num_blocks != spdk_bdev_get_num_blocks(ns->bdev)) { 1159 /* Namespace is still there but size has changed */ 1160 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Namespace resized: subsystem_id %d," 1161 " nsid %u, pg %p, old %lu, new %lu\n", 1162 subsystem->id, 1163 ns->nsid, 1164 group, 1165 ns_info->num_blocks, 1166 spdk_bdev_get_num_blocks(ns->bdev)); 1167 ns_changed = true; 1168 } 1169 1170 if (ns == NULL) { 1171 memset(ns_info, 0, sizeof(*ns_info)); 1172 } else { 1173 ns_info->uuid = *spdk_bdev_get_uuid(ns->bdev); 1174 ns_info->num_blocks = spdk_bdev_get_num_blocks(ns->bdev); 1175 ns_info->crkey = ns->crkey; 1176 ns_info->rtype = ns->rtype; 1177 if (ns->holder) { 1178 ns_info->holder_id = ns->holder->hostid; 1179 } 1180 1181 memset(&ns_info->reg_hostid, 0, SPDK_NVMF_MAX_NUM_REGISTRANTS * sizeof(struct spdk_uuid)); 1182 j = 0; 1183 TAILQ_FOREACH_SAFE(reg, &ns->registrants, link, tmp) { 1184 if (j >= SPDK_NVMF_MAX_NUM_REGISTRANTS) { 1185 SPDK_ERRLOG("Maximum %u registrants can support.\n", SPDK_NVMF_MAX_NUM_REGISTRANTS); 1186 return -EINVAL; 1187 } 1188 ns_info->reg_hostid[j++] = reg->hostid; 1189 } 1190 } 1191 } 1192 1193 if (ns_changed) { 1194 TAILQ_FOREACH(ctrlr, &subsystem->ctrlrs, link) { 1195 if (ctrlr->admin_qpair->group == group) { 1196 nvmf_ctrlr_async_event_ns_notice(ctrlr); 1197 } 1198 } 1199 } 1200 1201 return 0; 1202 } 1203 1204 int 1205 nvmf_poll_group_update_subsystem(struct spdk_nvmf_poll_group *group, 1206 struct spdk_nvmf_subsystem *subsystem) 1207 { 1208 return poll_group_update_subsystem(group, subsystem); 1209 } 1210 1211 int 1212 nvmf_poll_group_add_subsystem(struct spdk_nvmf_poll_group *group, 1213 struct spdk_nvmf_subsystem *subsystem, 1214 spdk_nvmf_poll_group_mod_done cb_fn, void *cb_arg) 1215 { 1216 int rc = 0; 1217 struct spdk_nvmf_subsystem_poll_group *sgroup = &group->sgroups[subsystem->id]; 1218 1219 TAILQ_INIT(&sgroup->queued); 1220 1221 rc = poll_group_update_subsystem(group, subsystem); 1222 if (rc) { 1223 nvmf_poll_group_remove_subsystem(group, subsystem, NULL, NULL); 1224 goto fini; 1225 } 1226 1227 sgroup->state = SPDK_NVMF_SUBSYSTEM_ACTIVE; 1228 fini: 1229 if (cb_fn) { 1230 cb_fn(cb_arg, rc); 1231 } 1232 1233 return rc; 1234 } 1235 1236 static void 1237 _nvmf_poll_group_remove_subsystem_cb(void *ctx, int status) 1238 { 1239 struct nvmf_qpair_disconnect_many_ctx *qpair_ctx = ctx; 1240 struct spdk_nvmf_subsystem *subsystem; 1241 struct spdk_nvmf_poll_group *group; 1242 struct spdk_nvmf_subsystem_poll_group *sgroup; 1243 spdk_nvmf_poll_group_mod_done cpl_fn = NULL; 1244 void *cpl_ctx = NULL; 1245 uint32_t nsid; 1246 1247 group = qpair_ctx->group; 1248 subsystem = qpair_ctx->subsystem; 1249 cpl_fn = qpair_ctx->cpl_fn; 1250 cpl_ctx = qpair_ctx->cpl_ctx; 1251 sgroup = &group->sgroups[subsystem->id]; 1252 1253 if (status) { 1254 goto fini; 1255 } 1256 1257 for (nsid = 0; nsid < sgroup->num_ns; nsid++) { 1258 if (sgroup->ns_info[nsid].channel) { 1259 spdk_put_io_channel(sgroup->ns_info[nsid].channel); 1260 sgroup->ns_info[nsid].channel = NULL; 1261 } 1262 } 1263 1264 sgroup->num_ns = 0; 1265 free(sgroup->ns_info); 1266 sgroup->ns_info = NULL; 1267 fini: 1268 free(qpair_ctx); 1269 if (cpl_fn) { 1270 cpl_fn(cpl_ctx, status); 1271 } 1272 } 1273 1274 static void 1275 _nvmf_subsystem_disconnect_next_qpair(void *ctx) 1276 { 1277 struct spdk_nvmf_qpair *qpair; 1278 struct nvmf_qpair_disconnect_many_ctx *qpair_ctx = ctx; 1279 struct spdk_nvmf_subsystem *subsystem; 1280 struct spdk_nvmf_poll_group *group; 1281 int rc = 0; 1282 1283 group = qpair_ctx->group; 1284 subsystem = qpair_ctx->subsystem; 1285 1286 TAILQ_FOREACH(qpair, &group->qpairs, link) { 1287 if ((qpair->ctrlr != NULL) && (qpair->ctrlr->subsys == subsystem)) { 1288 break; 1289 } 1290 } 1291 1292 if (qpair) { 1293 rc = spdk_nvmf_qpair_disconnect(qpair, _nvmf_subsystem_disconnect_next_qpair, qpair_ctx); 1294 } 1295 1296 if (!qpair || rc != 0) { 1297 _nvmf_poll_group_remove_subsystem_cb(ctx, rc); 1298 } 1299 return; 1300 } 1301 1302 void 1303 nvmf_poll_group_remove_subsystem(struct spdk_nvmf_poll_group *group, 1304 struct spdk_nvmf_subsystem *subsystem, 1305 spdk_nvmf_poll_group_mod_done cb_fn, void *cb_arg) 1306 { 1307 struct spdk_nvmf_qpair *qpair; 1308 struct spdk_nvmf_subsystem_poll_group *sgroup; 1309 struct nvmf_qpair_disconnect_many_ctx *ctx; 1310 int rc = 0; 1311 1312 ctx = calloc(1, sizeof(struct nvmf_qpair_disconnect_many_ctx)); 1313 1314 if (!ctx) { 1315 SPDK_ERRLOG("Unable to allocate memory for context to remove poll subsystem\n"); 1316 goto fini; 1317 } 1318 1319 ctx->group = group; 1320 ctx->subsystem = subsystem; 1321 ctx->cpl_fn = cb_fn; 1322 ctx->cpl_ctx = cb_arg; 1323 1324 sgroup = &group->sgroups[subsystem->id]; 1325 sgroup->state = SPDK_NVMF_SUBSYSTEM_INACTIVE; 1326 1327 TAILQ_FOREACH(qpair, &group->qpairs, link) { 1328 if ((qpair->ctrlr != NULL) && (qpair->ctrlr->subsys == subsystem)) { 1329 break; 1330 } 1331 } 1332 1333 if (qpair) { 1334 rc = spdk_nvmf_qpair_disconnect(qpair, _nvmf_subsystem_disconnect_next_qpair, ctx); 1335 } else { 1336 /* call the callback immediately. It will handle any channel iteration */ 1337 _nvmf_poll_group_remove_subsystem_cb(ctx, 0); 1338 } 1339 1340 if (rc != 0) { 1341 free(ctx); 1342 goto fini; 1343 } 1344 1345 return; 1346 fini: 1347 if (cb_fn) { 1348 cb_fn(cb_arg, rc); 1349 } 1350 } 1351 1352 void 1353 nvmf_poll_group_pause_subsystem(struct spdk_nvmf_poll_group *group, 1354 struct spdk_nvmf_subsystem *subsystem, 1355 spdk_nvmf_poll_group_mod_done cb_fn, void *cb_arg) 1356 { 1357 struct spdk_nvmf_subsystem_poll_group *sgroup; 1358 int rc = 0; 1359 1360 if (subsystem->id >= group->num_sgroups) { 1361 rc = -1; 1362 goto fini; 1363 } 1364 1365 sgroup = &group->sgroups[subsystem->id]; 1366 if (sgroup == NULL) { 1367 rc = -1; 1368 goto fini; 1369 } 1370 1371 assert(sgroup->state == SPDK_NVMF_SUBSYSTEM_ACTIVE); 1372 sgroup->state = SPDK_NVMF_SUBSYSTEM_PAUSING; 1373 1374 if (sgroup->io_outstanding > 0) { 1375 sgroup->cb_fn = cb_fn; 1376 sgroup->cb_arg = cb_arg; 1377 return; 1378 } 1379 1380 assert(sgroup->io_outstanding == 0); 1381 sgroup->state = SPDK_NVMF_SUBSYSTEM_PAUSED; 1382 fini: 1383 if (cb_fn) { 1384 cb_fn(cb_arg, rc); 1385 } 1386 } 1387 1388 void 1389 nvmf_poll_group_resume_subsystem(struct spdk_nvmf_poll_group *group, 1390 struct spdk_nvmf_subsystem *subsystem, 1391 spdk_nvmf_poll_group_mod_done cb_fn, void *cb_arg) 1392 { 1393 struct spdk_nvmf_request *req, *tmp; 1394 struct spdk_nvmf_subsystem_poll_group *sgroup; 1395 int rc = 0; 1396 1397 if (subsystem->id >= group->num_sgroups) { 1398 rc = -1; 1399 goto fini; 1400 } 1401 1402 sgroup = &group->sgroups[subsystem->id]; 1403 1404 assert(sgroup->state == SPDK_NVMF_SUBSYSTEM_PAUSED); 1405 1406 rc = poll_group_update_subsystem(group, subsystem); 1407 if (rc) { 1408 goto fini; 1409 } 1410 1411 sgroup->state = SPDK_NVMF_SUBSYSTEM_ACTIVE; 1412 1413 /* Release all queued requests */ 1414 TAILQ_FOREACH_SAFE(req, &sgroup->queued, link, tmp) { 1415 TAILQ_REMOVE(&sgroup->queued, req, link); 1416 spdk_nvmf_request_exec(req); 1417 } 1418 fini: 1419 if (cb_fn) { 1420 cb_fn(cb_arg, rc); 1421 } 1422 } 1423 1424 1425 struct spdk_nvmf_poll_group * 1426 spdk_nvmf_get_optimal_poll_group(struct spdk_nvmf_qpair *qpair) 1427 { 1428 struct spdk_nvmf_transport_poll_group *tgroup; 1429 1430 tgroup = nvmf_transport_get_optimal_poll_group(qpair->transport, qpair); 1431 1432 if (tgroup == NULL) { 1433 return NULL; 1434 } 1435 1436 return tgroup->group; 1437 } 1438 1439 int 1440 spdk_nvmf_poll_group_get_stat(struct spdk_nvmf_tgt *tgt, 1441 struct spdk_nvmf_poll_group_stat *stat) 1442 { 1443 struct spdk_io_channel *ch; 1444 struct spdk_nvmf_poll_group *group; 1445 1446 if (tgt == NULL || stat == NULL) { 1447 return -EINVAL; 1448 } 1449 1450 ch = spdk_get_io_channel(tgt); 1451 group = spdk_io_channel_get_ctx(ch); 1452 *stat = group->stat; 1453 spdk_put_io_channel(ch); 1454 return 0; 1455 } 1456