1 /*- 2 * BSD LICENSE 3 * 4 * Copyright (c) Intel Corporation. 5 * 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 typedef void (*nvmf_qpair_disconnect_cpl)(void *ctx, int status); 55 static void spdk_nvmf_tgt_destroy_poll_group(void *io_device, void *ctx_buf); 56 57 /* supplied to a single call to nvmf_qpair_disconnect */ 58 struct nvmf_qpair_disconnect_ctx { 59 struct spdk_nvmf_qpair *qpair; 60 struct spdk_nvmf_ctrlr *ctrlr; 61 nvmf_qpair_disconnect_cb cb_fn; 62 struct spdk_thread *thread; 63 void *ctx; 64 uint16_t qid; 65 }; 66 67 /* 68 * There are several times when we need to iterate through the list of all qpairs and selectively delete them. 69 * In order to do this sequentially without overlap, we must provide a context to recover the next qpair from 70 * to enable calling nvmf_qpair_disconnect on the next desired qpair. 71 */ 72 struct nvmf_qpair_disconnect_many_ctx { 73 struct spdk_nvmf_subsystem *subsystem; 74 struct spdk_nvmf_poll_group *group; 75 spdk_nvmf_poll_group_mod_done cpl_fn; 76 void *cpl_ctx; 77 }; 78 79 static void 80 spdk_nvmf_qpair_set_state(struct spdk_nvmf_qpair *qpair, 81 enum spdk_nvmf_qpair_state state) 82 { 83 assert(qpair != NULL); 84 assert(qpair->group->thread == spdk_get_thread()); 85 86 qpair->state = state; 87 } 88 89 static int 90 spdk_nvmf_poll_group_poll(void *ctx) 91 { 92 struct spdk_nvmf_poll_group *group = ctx; 93 int rc; 94 int count = 0; 95 struct spdk_nvmf_transport_poll_group *tgroup; 96 97 TAILQ_FOREACH(tgroup, &group->tgroups, link) { 98 rc = spdk_nvmf_transport_poll_group_poll(tgroup); 99 if (rc < 0) { 100 return -1; 101 } 102 count += rc; 103 } 104 105 return count; 106 } 107 108 static int 109 spdk_nvmf_tgt_create_poll_group(void *io_device, void *ctx_buf) 110 { 111 struct spdk_nvmf_tgt *tgt = io_device; 112 struct spdk_nvmf_poll_group *group = ctx_buf; 113 struct spdk_nvmf_transport *transport; 114 uint32_t sid; 115 116 TAILQ_INIT(&group->tgroups); 117 TAILQ_INIT(&group->qpairs); 118 119 TAILQ_FOREACH(transport, &tgt->transports, link) { 120 spdk_nvmf_poll_group_add_transport(group, transport); 121 } 122 123 group->num_sgroups = tgt->max_subsystems; 124 group->sgroups = calloc(tgt->max_subsystems, sizeof(struct spdk_nvmf_subsystem_poll_group)); 125 if (!group->sgroups) { 126 return -ENOMEM; 127 } 128 129 for (sid = 0; sid < tgt->max_subsystems; sid++) { 130 struct spdk_nvmf_subsystem *subsystem; 131 132 subsystem = tgt->subsystems[sid]; 133 if (!subsystem) { 134 continue; 135 } 136 137 if (spdk_nvmf_poll_group_add_subsystem(group, subsystem, NULL, NULL) != 0) { 138 spdk_nvmf_tgt_destroy_poll_group(io_device, ctx_buf); 139 return -1; 140 } 141 } 142 143 group->poller = spdk_poller_register(spdk_nvmf_poll_group_poll, group, 0); 144 group->thread = spdk_get_thread(); 145 146 return 0; 147 } 148 149 static void 150 spdk_nvmf_tgt_destroy_poll_group(void *io_device, void *ctx_buf) 151 { 152 struct spdk_nvmf_poll_group *group = ctx_buf; 153 struct spdk_nvmf_transport_poll_group *tgroup, *tmp; 154 struct spdk_nvmf_subsystem_poll_group *sgroup; 155 uint32_t sid, nsid; 156 157 TAILQ_FOREACH_SAFE(tgroup, &group->tgroups, link, tmp) { 158 TAILQ_REMOVE(&group->tgroups, tgroup, link); 159 spdk_nvmf_transport_poll_group_destroy(tgroup); 160 } 161 162 for (sid = 0; sid < group->num_sgroups; sid++) { 163 sgroup = &group->sgroups[sid]; 164 165 for (nsid = 0; nsid < sgroup->num_channels; nsid++) { 166 if (sgroup->channels[nsid]) { 167 spdk_put_io_channel(sgroup->channels[nsid]); 168 sgroup->channels[nsid] = NULL; 169 } 170 } 171 172 free(sgroup->channels); 173 } 174 175 free(group->sgroups); 176 } 177 178 static void 179 _nvmf_tgt_disconnect_next_qpair(void *ctx) 180 { 181 struct spdk_nvmf_qpair *qpair; 182 struct nvmf_qpair_disconnect_many_ctx *qpair_ctx = ctx; 183 struct spdk_nvmf_poll_group *group = qpair_ctx->group; 184 struct spdk_io_channel *ch; 185 int rc = 0; 186 187 qpair = TAILQ_FIRST(&group->qpairs); 188 189 if (qpair) { 190 rc = spdk_nvmf_qpair_disconnect(qpair, _nvmf_tgt_disconnect_next_qpair, ctx); 191 } 192 193 if (!qpair || rc != 0) { 194 /* When the refcount from the channels reaches 0, spdk_nvmf_tgt_destroy_poll_group will be called. */ 195 ch = spdk_io_channel_from_ctx(group); 196 spdk_put_io_channel(ch); 197 free(qpair_ctx); 198 } 199 } 200 201 static void 202 spdk_nvmf_tgt_destroy_poll_group_qpairs(struct spdk_nvmf_poll_group *group) 203 { 204 struct nvmf_qpair_disconnect_many_ctx *ctx; 205 206 ctx = calloc(1, sizeof(struct nvmf_qpair_disconnect_many_ctx)); 207 208 if (!ctx) { 209 SPDK_ERRLOG("Failed to allocate memory for destroy poll group ctx\n"); 210 return; 211 } 212 213 spdk_poller_unregister(&group->poller); 214 215 ctx->group = group; 216 _nvmf_tgt_disconnect_next_qpair(ctx); 217 } 218 219 struct spdk_nvmf_tgt * 220 spdk_nvmf_tgt_create(uint32_t max_subsystems) 221 { 222 struct spdk_nvmf_tgt *tgt; 223 224 tgt = calloc(1, sizeof(*tgt)); 225 if (!tgt) { 226 return NULL; 227 } 228 229 if (!max_subsystems) { 230 tgt->max_subsystems = SPDK_NVMF_DEFAULT_MAX_SUBSYSTEMS; 231 } else { 232 tgt->max_subsystems = max_subsystems; 233 } 234 235 tgt->discovery_genctr = 0; 236 tgt->discovery_log_page = NULL; 237 tgt->discovery_log_page_size = 0; 238 TAILQ_INIT(&tgt->transports); 239 240 tgt->subsystems = calloc(tgt->max_subsystems, sizeof(struct spdk_nvmf_subsystem *)); 241 if (!tgt->subsystems) { 242 free(tgt); 243 return NULL; 244 } 245 246 spdk_io_device_register(tgt, 247 spdk_nvmf_tgt_create_poll_group, 248 spdk_nvmf_tgt_destroy_poll_group, 249 sizeof(struct spdk_nvmf_poll_group), 250 "nvmf_tgt"); 251 252 return tgt; 253 } 254 255 static void 256 spdk_nvmf_tgt_destroy_cb(void *io_device) 257 { 258 struct spdk_nvmf_tgt *tgt = io_device; 259 struct spdk_nvmf_transport *transport, *transport_tmp; 260 spdk_nvmf_tgt_destroy_done_fn *destroy_cb_fn; 261 void *destroy_cb_arg; 262 uint32_t i; 263 264 if (tgt->discovery_log_page) { 265 free(tgt->discovery_log_page); 266 } 267 268 if (tgt->subsystems) { 269 for (i = 0; i < tgt->max_subsystems; i++) { 270 if (tgt->subsystems[i]) { 271 spdk_nvmf_subsystem_destroy(tgt->subsystems[i]); 272 } 273 } 274 free(tgt->subsystems); 275 } 276 277 TAILQ_FOREACH_SAFE(transport, &tgt->transports, link, transport_tmp) { 278 TAILQ_REMOVE(&tgt->transports, transport, link); 279 spdk_nvmf_transport_destroy(transport); 280 } 281 282 destroy_cb_fn = tgt->destroy_cb_fn; 283 destroy_cb_arg = tgt->destroy_cb_arg; 284 285 free(tgt); 286 287 if (destroy_cb_fn) { 288 destroy_cb_fn(destroy_cb_arg, 0); 289 } 290 } 291 292 void 293 spdk_nvmf_tgt_destroy(struct spdk_nvmf_tgt *tgt, 294 spdk_nvmf_tgt_destroy_done_fn cb_fn, 295 void *cb_arg) 296 { 297 tgt->destroy_cb_fn = cb_fn; 298 tgt->destroy_cb_arg = cb_arg; 299 300 spdk_io_device_unregister(tgt, spdk_nvmf_tgt_destroy_cb); 301 } 302 303 static void 304 spdk_nvmf_write_subsystem_config_json(struct spdk_json_write_ctx *w, 305 struct spdk_nvmf_subsystem *subsystem) 306 { 307 struct spdk_nvmf_host *host; 308 struct spdk_nvmf_listener *listener; 309 const struct spdk_nvme_transport_id *trid; 310 struct spdk_nvmf_ns *ns; 311 struct spdk_nvmf_ns_opts ns_opts; 312 uint32_t max_namespaces; 313 char uuid_str[SPDK_UUID_STRING_LEN]; 314 const char *trtype; 315 const char *adrfam; 316 317 if (spdk_nvmf_subsystem_get_type(subsystem) != SPDK_NVMF_SUBTYPE_NVME) { 318 return; 319 } 320 321 /* { */ 322 spdk_json_write_object_begin(w); 323 spdk_json_write_named_string(w, "method", "nvmf_subsystem_create"); 324 325 /* "params" : { */ 326 spdk_json_write_named_object_begin(w, "params"); 327 spdk_json_write_named_string(w, "nqn", spdk_nvmf_subsystem_get_nqn(subsystem)); 328 spdk_json_write_named_bool(w, "allow_any_host", spdk_nvmf_subsystem_get_allow_any_host(subsystem)); 329 spdk_json_write_named_string(w, "serial_number", spdk_nvmf_subsystem_get_sn(subsystem)); 330 331 max_namespaces = spdk_nvmf_subsystem_get_max_namespaces(subsystem); 332 if (max_namespaces != 0) { 333 spdk_json_write_named_uint32(w, "max_namespaces", max_namespaces); 334 } 335 336 /* } "params" */ 337 spdk_json_write_object_end(w); 338 339 /* } */ 340 spdk_json_write_object_end(w); 341 342 for (listener = spdk_nvmf_subsystem_get_first_listener(subsystem); listener != NULL; 343 listener = spdk_nvmf_subsystem_get_next_listener(subsystem, listener)) { 344 trid = spdk_nvmf_listener_get_trid(listener); 345 346 trtype = spdk_nvme_transport_id_trtype_str(trid->trtype); 347 adrfam = spdk_nvme_transport_id_adrfam_str(trid->adrfam); 348 349 spdk_json_write_object_begin(w); 350 spdk_json_write_named_string(w, "method", "nvmf_subsystem_add_listener"); 351 352 /* "params" : { */ 353 spdk_json_write_named_object_begin(w, "params"); 354 355 spdk_json_write_named_string(w, "nqn", spdk_nvmf_subsystem_get_nqn(subsystem)); 356 357 /* "listen_address" : { */ 358 spdk_json_write_named_object_begin(w, "listen_address"); 359 360 spdk_json_write_named_string(w, "trtype", trtype); 361 if (adrfam) { 362 spdk_json_write_named_string(w, "adrfam", adrfam); 363 } 364 365 spdk_json_write_named_string(w, "traddr", trid->traddr); 366 spdk_json_write_named_string(w, "trsvcid", trid->trsvcid); 367 /* } "listen_address" */ 368 spdk_json_write_object_end(w); 369 370 /* } "params" */ 371 spdk_json_write_object_end(w); 372 373 /* } */ 374 spdk_json_write_object_end(w); 375 } 376 377 for (host = spdk_nvmf_subsystem_get_first_host(subsystem); host != NULL; 378 host = spdk_nvmf_subsystem_get_next_host(subsystem, host)) { 379 380 spdk_json_write_object_begin(w); 381 spdk_json_write_named_string(w, "method", "nvmf_subsystem_add_host"); 382 383 /* "params" : { */ 384 spdk_json_write_named_object_begin(w, "params"); 385 386 spdk_json_write_named_string(w, "nqn", spdk_nvmf_subsystem_get_nqn(subsystem)); 387 spdk_json_write_named_string(w, "host", spdk_nvmf_host_get_nqn(host)); 388 389 /* } "params" */ 390 spdk_json_write_object_end(w); 391 392 /* } */ 393 spdk_json_write_object_end(w); 394 } 395 396 for (ns = spdk_nvmf_subsystem_get_first_ns(subsystem); ns != NULL; 397 ns = spdk_nvmf_subsystem_get_next_ns(subsystem, ns)) { 398 spdk_nvmf_ns_get_opts(ns, &ns_opts, sizeof(ns_opts)); 399 400 spdk_json_write_object_begin(w); 401 spdk_json_write_named_string(w, "method", "nvmf_subsystem_add_ns"); 402 403 /* "params" : { */ 404 spdk_json_write_named_object_begin(w, "params"); 405 406 spdk_json_write_named_string(w, "nqn", spdk_nvmf_subsystem_get_nqn(subsystem)); 407 408 /* "namespace" : { */ 409 spdk_json_write_named_object_begin(w, "namespace"); 410 411 spdk_json_write_named_uint32(w, "nsid", spdk_nvmf_ns_get_id(ns)); 412 spdk_json_write_named_string(w, "bdev_name", spdk_bdev_get_name(spdk_nvmf_ns_get_bdev(ns))); 413 414 if (!spdk_mem_all_zero(ns_opts.nguid, sizeof(ns_opts.nguid))) { 415 SPDK_STATIC_ASSERT(sizeof(ns_opts.nguid) == sizeof(uint64_t) * 2, "size mismatch"); 416 spdk_json_write_named_string_fmt(w, "nguid", "%016"PRIX64"%016"PRIX64, from_be64(&ns_opts.nguid[0]), 417 from_be64(&ns_opts.nguid[8])); 418 } 419 420 if (!spdk_mem_all_zero(ns_opts.eui64, sizeof(ns_opts.eui64))) { 421 SPDK_STATIC_ASSERT(sizeof(ns_opts.eui64) == sizeof(uint64_t), "size mismatch"); 422 spdk_json_write_named_string_fmt(w, "eui64", "%016"PRIX64, from_be64(&ns_opts.eui64)); 423 } 424 425 if (!spdk_mem_all_zero(&ns_opts.uuid, sizeof(ns_opts.uuid))) { 426 spdk_uuid_fmt_lower(uuid_str, sizeof(uuid_str), &ns_opts.uuid); 427 spdk_json_write_named_string(w, "uuid", uuid_str); 428 } 429 430 /* "namespace" */ 431 spdk_json_write_object_end(w); 432 433 /* } "params" */ 434 spdk_json_write_object_end(w); 435 436 /* } */ 437 spdk_json_write_object_end(w); 438 } 439 } 440 441 void 442 spdk_nvmf_tgt_write_config_json(struct spdk_json_write_ctx *w, struct spdk_nvmf_tgt *tgt) 443 { 444 struct spdk_nvmf_subsystem *subsystem; 445 struct spdk_nvmf_transport *transport; 446 447 spdk_json_write_object_begin(w); 448 spdk_json_write_named_string(w, "method", "set_nvmf_target_max_subsystems"); 449 450 spdk_json_write_named_object_begin(w, "params"); 451 spdk_json_write_named_uint32(w, "max_subsystems", tgt->max_subsystems); 452 spdk_json_write_object_end(w); 453 454 spdk_json_write_object_end(w); 455 456 /* write transports */ 457 TAILQ_FOREACH(transport, &tgt->transports, link) { 458 spdk_json_write_object_begin(w); 459 spdk_json_write_named_string(w, "method", "nvmf_create_transport"); 460 461 spdk_json_write_named_object_begin(w, "params"); 462 spdk_json_write_named_string(w, "trtype", spdk_nvme_transport_id_trtype_str(transport->ops->type)); 463 spdk_json_write_named_uint32(w, "max_queue_depth", transport->opts.max_queue_depth); 464 spdk_json_write_named_uint32(w, "max_qpairs_per_ctrlr", transport->opts.max_qpairs_per_ctrlr); 465 spdk_json_write_named_uint32(w, "in_capsule_data_size", transport->opts.in_capsule_data_size); 466 spdk_json_write_named_uint32(w, "max_io_size", transport->opts.max_io_size); 467 spdk_json_write_named_uint32(w, "io_unit_size", transport->opts.io_unit_size); 468 spdk_json_write_named_uint32(w, "max_aq_depth", transport->opts.max_aq_depth); 469 spdk_json_write_object_end(w); 470 471 spdk_json_write_object_end(w); 472 } 473 474 subsystem = spdk_nvmf_subsystem_get_first(tgt); 475 while (subsystem) { 476 spdk_nvmf_write_subsystem_config_json(w, subsystem); 477 subsystem = spdk_nvmf_subsystem_get_next(subsystem); 478 } 479 } 480 481 void 482 spdk_nvmf_tgt_listen(struct spdk_nvmf_tgt *tgt, 483 struct spdk_nvme_transport_id *trid, 484 spdk_nvmf_tgt_listen_done_fn cb_fn, 485 void *cb_arg) 486 { 487 struct spdk_nvmf_transport *transport; 488 const char *trtype; 489 int rc; 490 491 transport = spdk_nvmf_tgt_get_transport(tgt, trid->trtype); 492 if (!transport) { 493 trtype = spdk_nvme_transport_id_trtype_str(trid->trtype); 494 if (trtype != NULL) { 495 SPDK_ERRLOG("Unable to listen on transport %s. The transport must be created first.\n", trtype); 496 } else { 497 SPDK_ERRLOG("The specified trtype %d is unknown. Please make sure that it is properly registered.\n", 498 trid->trtype); 499 } 500 cb_fn(cb_arg, -EINVAL); 501 return; 502 } 503 504 rc = spdk_nvmf_transport_listen(transport, trid); 505 if (rc < 0) { 506 SPDK_ERRLOG("Unable to listen on address '%s'\n", trid->traddr); 507 cb_fn(cb_arg, rc); 508 return; 509 } 510 511 tgt->discovery_genctr++; 512 513 cb_fn(cb_arg, 0); 514 } 515 516 struct spdk_nvmf_tgt_add_transport_ctx { 517 struct spdk_nvmf_tgt *tgt; 518 struct spdk_nvmf_transport *transport; 519 spdk_nvmf_tgt_add_transport_done_fn cb_fn; 520 void *cb_arg; 521 }; 522 523 static void 524 _spdk_nvmf_tgt_add_transport_done(struct spdk_io_channel_iter *i, int status) 525 { 526 struct spdk_nvmf_tgt_add_transport_ctx *ctx = spdk_io_channel_iter_get_ctx(i); 527 528 ctx->cb_fn(ctx->cb_arg, status); 529 530 free(ctx); 531 } 532 533 static void 534 _spdk_nvmf_tgt_add_transport(struct spdk_io_channel_iter *i) 535 { 536 struct spdk_nvmf_tgt_add_transport_ctx *ctx = spdk_io_channel_iter_get_ctx(i); 537 struct spdk_io_channel *ch = spdk_io_channel_iter_get_channel(i); 538 struct spdk_nvmf_poll_group *group = spdk_io_channel_get_ctx(ch); 539 int rc; 540 541 rc = spdk_nvmf_poll_group_add_transport(group, ctx->transport); 542 spdk_for_each_channel_continue(i, rc); 543 } 544 545 void spdk_nvmf_tgt_add_transport(struct spdk_nvmf_tgt *tgt, 546 struct spdk_nvmf_transport *transport, 547 spdk_nvmf_tgt_add_transport_done_fn cb_fn, 548 void *cb_arg) 549 { 550 struct spdk_nvmf_tgt_add_transport_ctx *ctx; 551 552 if (spdk_nvmf_tgt_get_transport(tgt, transport->ops->type)) { 553 cb_fn(cb_arg, -EEXIST); 554 return; /* transport already created */ 555 } 556 557 transport->tgt = tgt; 558 TAILQ_INSERT_TAIL(&tgt->transports, transport, link); 559 560 ctx = calloc(1, sizeof(*ctx)); 561 if (!ctx) { 562 cb_fn(cb_arg, -ENOMEM); 563 return; 564 } 565 566 ctx->tgt = tgt; 567 ctx->transport = transport; 568 ctx->cb_fn = cb_fn; 569 ctx->cb_arg = cb_arg; 570 571 spdk_for_each_channel(tgt, 572 _spdk_nvmf_tgt_add_transport, 573 ctx, 574 _spdk_nvmf_tgt_add_transport_done); 575 } 576 577 struct spdk_nvmf_subsystem * 578 spdk_nvmf_tgt_find_subsystem(struct spdk_nvmf_tgt *tgt, const char *subnqn) 579 { 580 struct spdk_nvmf_subsystem *subsystem; 581 uint32_t sid; 582 583 if (!subnqn) { 584 return NULL; 585 } 586 587 for (sid = 0; sid < tgt->max_subsystems; sid++) { 588 subsystem = tgt->subsystems[sid]; 589 if (subsystem == NULL) { 590 continue; 591 } 592 593 if (strcmp(subnqn, subsystem->subnqn) == 0) { 594 return subsystem; 595 } 596 } 597 598 return NULL; 599 } 600 601 struct spdk_nvmf_transport * 602 spdk_nvmf_tgt_get_transport(struct spdk_nvmf_tgt *tgt, enum spdk_nvme_transport_type type) 603 { 604 struct spdk_nvmf_transport *transport; 605 606 TAILQ_FOREACH(transport, &tgt->transports, link) { 607 if (transport->ops->type == type) { 608 return transport; 609 } 610 } 611 612 return NULL; 613 } 614 615 void 616 spdk_nvmf_tgt_accept(struct spdk_nvmf_tgt *tgt, new_qpair_fn cb_fn) 617 { 618 struct spdk_nvmf_transport *transport, *tmp; 619 620 TAILQ_FOREACH_SAFE(transport, &tgt->transports, link, tmp) { 621 spdk_nvmf_transport_accept(transport, cb_fn); 622 } 623 } 624 625 struct spdk_nvmf_poll_group * 626 spdk_nvmf_poll_group_create(struct spdk_nvmf_tgt *tgt) 627 { 628 struct spdk_io_channel *ch; 629 630 ch = spdk_get_io_channel(tgt); 631 if (!ch) { 632 SPDK_ERRLOG("Unable to get I/O channel for target\n"); 633 return NULL; 634 } 635 636 return spdk_io_channel_get_ctx(ch); 637 } 638 639 void 640 spdk_nvmf_poll_group_destroy(struct spdk_nvmf_poll_group *group) 641 { 642 /* This function will put the io_channel associated with this poll group */ 643 spdk_nvmf_tgt_destroy_poll_group_qpairs(group); 644 } 645 646 int 647 spdk_nvmf_poll_group_add(struct spdk_nvmf_poll_group *group, 648 struct spdk_nvmf_qpair *qpair) 649 { 650 int rc = -1; 651 struct spdk_nvmf_transport_poll_group *tgroup; 652 653 TAILQ_INIT(&qpair->outstanding); 654 qpair->group = group; 655 656 TAILQ_FOREACH(tgroup, &group->tgroups, link) { 657 if (tgroup->transport == qpair->transport) { 658 rc = spdk_nvmf_transport_poll_group_add(tgroup, qpair); 659 break; 660 } 661 } 662 663 /* We add the qpair to the group only it is succesfully added into the tgroup */ 664 if (rc == 0) { 665 TAILQ_INSERT_TAIL(&group->qpairs, qpair, link); 666 spdk_nvmf_qpair_set_state(qpair, SPDK_NVMF_QPAIR_ACTIVE); 667 } 668 669 return rc; 670 } 671 672 static 673 void _nvmf_ctrlr_destruct(void *ctx) 674 { 675 struct spdk_nvmf_ctrlr *ctrlr = ctx; 676 677 spdk_nvmf_ctrlr_destruct(ctrlr); 678 } 679 680 static void 681 _spdk_nvmf_ctrlr_free_from_qpair(void *ctx) 682 { 683 struct nvmf_qpair_disconnect_ctx *qpair_ctx = ctx; 684 struct spdk_nvmf_ctrlr *ctrlr = qpair_ctx->ctrlr; 685 uint32_t count; 686 687 spdk_bit_array_clear(ctrlr->qpair_mask, qpair_ctx->qid); 688 count = spdk_bit_array_count_set(ctrlr->qpair_mask); 689 if (count == 0) { 690 spdk_bit_array_free(&ctrlr->qpair_mask); 691 692 spdk_thread_send_msg(ctrlr->subsys->thread, _nvmf_ctrlr_destruct, ctrlr); 693 } 694 695 if (qpair_ctx->cb_fn) { 696 spdk_thread_send_msg(qpair_ctx->thread, qpair_ctx->cb_fn, qpair_ctx->ctx); 697 } 698 free(qpair_ctx); 699 } 700 701 static void 702 _spdk_nvmf_qpair_destroy(void *ctx, int status) 703 { 704 struct nvmf_qpair_disconnect_ctx *qpair_ctx = ctx; 705 struct spdk_nvmf_qpair *qpair = qpair_ctx->qpair; 706 struct spdk_nvmf_ctrlr *ctrlr = qpair->ctrlr; 707 struct spdk_nvmf_transport_poll_group *tgroup; 708 struct spdk_nvmf_request *req, *tmp; 709 struct spdk_nvmf_subsystem_poll_group *sgroup; 710 int rc; 711 712 assert(qpair->state == SPDK_NVMF_QPAIR_DEACTIVATING); 713 spdk_nvmf_qpair_set_state(qpair, SPDK_NVMF_QPAIR_ERROR); 714 qpair_ctx->qid = qpair->qid; 715 716 /* Find the tgroup and remove the qpair from the tgroup */ 717 TAILQ_FOREACH(tgroup, &qpair->group->tgroups, link) { 718 if (tgroup->transport == qpair->transport) { 719 rc = spdk_nvmf_transport_poll_group_remove(tgroup, qpair); 720 if (rc && (rc != ENOTSUP)) { 721 SPDK_ERRLOG("Cannot remove qpair=%p from transport group=%p\n", 722 qpair, tgroup); 723 } 724 break; 725 } 726 } 727 728 if (ctrlr) { 729 sgroup = &qpair->group->sgroups[ctrlr->subsys->id]; 730 TAILQ_FOREACH_SAFE(req, &sgroup->queued, link, tmp) { 731 if (req->qpair == qpair) { 732 TAILQ_REMOVE(&sgroup->queued, req, link); 733 if (spdk_nvmf_transport_req_free(req)) { 734 SPDK_ERRLOG("Transport request free error!\n"); 735 } 736 } 737 } 738 } 739 740 TAILQ_REMOVE(&qpair->group->qpairs, qpair, link); 741 742 spdk_nvmf_transport_qpair_fini(qpair); 743 744 if (!ctrlr || !ctrlr->thread) { 745 if (qpair_ctx->cb_fn) { 746 spdk_thread_send_msg(qpair_ctx->thread, qpair_ctx->cb_fn, qpair_ctx->ctx); 747 } 748 free(qpair_ctx); 749 return; 750 } 751 752 qpair_ctx->ctrlr = ctrlr; 753 spdk_thread_send_msg(ctrlr->thread, _spdk_nvmf_ctrlr_free_from_qpair, qpair_ctx); 754 755 } 756 757 int 758 spdk_nvmf_qpair_disconnect(struct spdk_nvmf_qpair *qpair, nvmf_qpair_disconnect_cb cb_fn, void *ctx) 759 { 760 struct nvmf_qpair_disconnect_ctx *qpair_ctx; 761 762 /* If we get a qpair in the uninitialized state, we can just destroy it immediately */ 763 if (qpair->state == SPDK_NVMF_QPAIR_UNINITIALIZED) { 764 spdk_nvmf_transport_qpair_fini(qpair); 765 if (cb_fn) { 766 cb_fn(ctx); 767 } 768 return 0; 769 } 770 771 /* The queue pair must be disconnected from the thread that owns it */ 772 assert(qpair->group->thread == spdk_get_thread()); 773 774 if (qpair->state != SPDK_NVMF_QPAIR_ACTIVE) { 775 /* This can occur if the connection is killed by the target, 776 * which results in a notification that the connection 777 * died. Send a message to defer the processing of this 778 * callback. This allows the stack to unwind in the case 779 * where a bunch of connections are disconnected in 780 * a loop. */ 781 if (cb_fn) { 782 spdk_thread_send_msg(qpair->group->thread, cb_fn, ctx); 783 } 784 return 0; 785 } 786 787 assert(qpair->state == SPDK_NVMF_QPAIR_ACTIVE); 788 spdk_nvmf_qpair_set_state(qpair, SPDK_NVMF_QPAIR_DEACTIVATING); 789 790 qpair_ctx = calloc(1, sizeof(struct nvmf_qpair_disconnect_ctx)); 791 if (!qpair_ctx) { 792 SPDK_ERRLOG("Unable to allocate context for nvmf_qpair_disconnect\n"); 793 return -ENOMEM; 794 } 795 796 qpair_ctx->qpair = qpair; 797 qpair_ctx->cb_fn = cb_fn; 798 qpair_ctx->thread = qpair->group->thread; 799 qpair_ctx->ctx = ctx; 800 801 /* Check for outstanding I/O */ 802 if (!TAILQ_EMPTY(&qpair->outstanding)) { 803 qpair->state_cb = _spdk_nvmf_qpair_destroy; 804 qpair->state_cb_arg = qpair_ctx; 805 spdk_nvmf_qpair_free_aer(qpair); 806 return 0; 807 } 808 809 _spdk_nvmf_qpair_destroy(qpair_ctx, 0); 810 811 return 0; 812 } 813 814 int 815 spdk_nvmf_qpair_get_peer_trid(struct spdk_nvmf_qpair *qpair, 816 struct spdk_nvme_transport_id *trid) 817 { 818 return spdk_nvmf_transport_qpair_get_peer_trid(qpair, trid); 819 } 820 821 int 822 spdk_nvmf_qpair_get_local_trid(struct spdk_nvmf_qpair *qpair, 823 struct spdk_nvme_transport_id *trid) 824 { 825 return spdk_nvmf_transport_qpair_get_local_trid(qpair, trid); 826 } 827 828 int 829 spdk_nvmf_qpair_get_listen_trid(struct spdk_nvmf_qpair *qpair, 830 struct spdk_nvme_transport_id *trid) 831 { 832 return spdk_nvmf_transport_qpair_get_listen_trid(qpair, trid); 833 } 834 835 int 836 spdk_nvmf_poll_group_add_transport(struct spdk_nvmf_poll_group *group, 837 struct spdk_nvmf_transport *transport) 838 { 839 struct spdk_nvmf_transport_poll_group *tgroup; 840 841 TAILQ_FOREACH(tgroup, &group->tgroups, link) { 842 if (tgroup->transport == transport) { 843 /* Transport already in the poll group */ 844 return 0; 845 } 846 } 847 848 tgroup = spdk_nvmf_transport_poll_group_create(transport); 849 if (!tgroup) { 850 SPDK_ERRLOG("Unable to create poll group for transport\n"); 851 return -1; 852 } 853 854 TAILQ_INSERT_TAIL(&group->tgroups, tgroup, link); 855 856 return 0; 857 } 858 859 static int 860 poll_group_update_subsystem(struct spdk_nvmf_poll_group *group, 861 struct spdk_nvmf_subsystem *subsystem) 862 { 863 struct spdk_nvmf_subsystem_poll_group *sgroup; 864 uint32_t new_num_channels, old_num_channels; 865 uint32_t i; 866 struct spdk_nvmf_ns *ns; 867 868 /* Make sure our poll group has memory for this subsystem allocated */ 869 if (subsystem->id >= group->num_sgroups) { 870 return -ENOMEM; 871 } 872 873 sgroup = &group->sgroups[subsystem->id]; 874 875 /* Make sure the array of channels is the correct size */ 876 new_num_channels = subsystem->max_nsid; 877 old_num_channels = sgroup->num_channels; 878 879 if (old_num_channels == 0) { 880 if (new_num_channels > 0) { 881 /* First allocation */ 882 sgroup->channels = calloc(new_num_channels, sizeof(sgroup->channels[0])); 883 if (!sgroup->channels) { 884 return -ENOMEM; 885 } 886 } 887 } else if (new_num_channels > old_num_channels) { 888 void *buf; 889 890 /* Make the array larger */ 891 buf = realloc(sgroup->channels, new_num_channels * sizeof(sgroup->channels[0])); 892 if (!buf) { 893 return -ENOMEM; 894 } 895 896 sgroup->channels = buf; 897 898 /* Null out the new channels slots */ 899 for (i = old_num_channels; i < new_num_channels; i++) { 900 sgroup->channels[i] = NULL; 901 } 902 } else if (new_num_channels < old_num_channels) { 903 void *buf; 904 905 /* Free the extra I/O channels */ 906 for (i = new_num_channels; i < old_num_channels; i++) { 907 if (sgroup->channels[i]) { 908 spdk_put_io_channel(sgroup->channels[i]); 909 sgroup->channels[i] = NULL; 910 } 911 } 912 913 /* Make the array smaller */ 914 if (new_num_channels > 0) { 915 buf = realloc(sgroup->channels, new_num_channels * sizeof(sgroup->channels[0])); 916 if (!buf) { 917 return -ENOMEM; 918 } 919 sgroup->channels = buf; 920 } else { 921 free(sgroup->channels); 922 sgroup->channels = NULL; 923 } 924 } 925 926 sgroup->num_channels = new_num_channels; 927 928 /* Detect bdevs that were added or removed */ 929 for (i = 0; i < sgroup->num_channels; i++) { 930 ns = subsystem->ns[i]; 931 if (ns == NULL && sgroup->channels[i] == NULL) { 932 /* Both NULL. Leave empty */ 933 } else if (ns == NULL && sgroup->channels[i] != NULL) { 934 /* There was a channel here, but the namespace is gone. */ 935 spdk_put_io_channel(sgroup->channels[i]); 936 sgroup->channels[i] = NULL; 937 } else if (ns != NULL && sgroup->channels[i] == NULL) { 938 /* A namespace appeared but there is no channel yet */ 939 sgroup->channels[i] = spdk_bdev_get_io_channel(ns->desc); 940 if (sgroup->channels[i] == NULL) { 941 SPDK_ERRLOG("Could not allocate I/O channel.\n"); 942 return -ENOMEM; 943 } 944 } else { 945 /* A namespace was present before and didn't change. */ 946 } 947 } 948 949 return 0; 950 } 951 952 int 953 spdk_nvmf_poll_group_update_subsystem(struct spdk_nvmf_poll_group *group, 954 struct spdk_nvmf_subsystem *subsystem) 955 { 956 return poll_group_update_subsystem(group, subsystem); 957 } 958 959 int 960 spdk_nvmf_poll_group_add_subsystem(struct spdk_nvmf_poll_group *group, 961 struct spdk_nvmf_subsystem *subsystem, 962 spdk_nvmf_poll_group_mod_done cb_fn, void *cb_arg) 963 { 964 int rc = 0; 965 struct spdk_nvmf_subsystem_poll_group *sgroup = &group->sgroups[subsystem->id]; 966 967 TAILQ_INIT(&sgroup->queued); 968 969 rc = poll_group_update_subsystem(group, subsystem); 970 if (rc) { 971 spdk_nvmf_poll_group_remove_subsystem(group, subsystem, NULL, NULL); 972 goto fini; 973 } 974 975 sgroup->state = SPDK_NVMF_SUBSYSTEM_ACTIVE; 976 fini: 977 if (cb_fn) { 978 cb_fn(cb_arg, rc); 979 } 980 981 return rc; 982 } 983 984 static void 985 _nvmf_poll_group_remove_subsystem_cb(void *ctx, int status) 986 { 987 struct nvmf_qpair_disconnect_many_ctx *qpair_ctx = ctx; 988 struct spdk_nvmf_subsystem *subsystem; 989 struct spdk_nvmf_poll_group *group; 990 struct spdk_nvmf_subsystem_poll_group *sgroup; 991 spdk_nvmf_poll_group_mod_done cpl_fn = NULL; 992 void *cpl_ctx = NULL; 993 uint32_t nsid; 994 995 group = qpair_ctx->group; 996 subsystem = qpair_ctx->subsystem; 997 cpl_fn = qpair_ctx->cpl_fn; 998 cpl_ctx = qpair_ctx->cpl_ctx; 999 sgroup = &group->sgroups[subsystem->id]; 1000 1001 if (status) { 1002 goto fini; 1003 } 1004 1005 for (nsid = 0; nsid < sgroup->num_channels; nsid++) { 1006 if (sgroup->channels[nsid]) { 1007 spdk_put_io_channel(sgroup->channels[nsid]); 1008 sgroup->channels[nsid] = NULL; 1009 } 1010 } 1011 1012 sgroup->num_channels = 0; 1013 free(sgroup->channels); 1014 sgroup->channels = NULL; 1015 fini: 1016 free(qpair_ctx); 1017 if (cpl_fn) { 1018 cpl_fn(cpl_ctx, status); 1019 } 1020 } 1021 1022 static void 1023 _nvmf_subsystem_disconnect_next_qpair(void *ctx) 1024 { 1025 struct spdk_nvmf_qpair *qpair; 1026 struct nvmf_qpair_disconnect_many_ctx *qpair_ctx = ctx; 1027 struct spdk_nvmf_subsystem *subsystem; 1028 struct spdk_nvmf_poll_group *group; 1029 int rc = 0; 1030 1031 group = qpair_ctx->group; 1032 subsystem = qpair_ctx->subsystem; 1033 1034 TAILQ_FOREACH(qpair, &group->qpairs, link) { 1035 if ((qpair->ctrlr != NULL) && (qpair->ctrlr->subsys == subsystem)) { 1036 break; 1037 } 1038 } 1039 1040 if (qpair) { 1041 rc = spdk_nvmf_qpair_disconnect(qpair, _nvmf_subsystem_disconnect_next_qpair, qpair_ctx); 1042 } 1043 1044 if (!qpair || rc != 0) { 1045 _nvmf_poll_group_remove_subsystem_cb(ctx, rc); 1046 } 1047 return; 1048 } 1049 1050 void 1051 spdk_nvmf_poll_group_remove_subsystem(struct spdk_nvmf_poll_group *group, 1052 struct spdk_nvmf_subsystem *subsystem, 1053 spdk_nvmf_poll_group_mod_done cb_fn, void *cb_arg) 1054 { 1055 struct spdk_nvmf_qpair *qpair; 1056 struct spdk_nvmf_subsystem_poll_group *sgroup; 1057 struct nvmf_qpair_disconnect_many_ctx *ctx; 1058 int rc = 0; 1059 1060 ctx = calloc(1, sizeof(struct nvmf_qpair_disconnect_many_ctx)); 1061 1062 if (!ctx) { 1063 SPDK_ERRLOG("Unable to allocate memory for context to remove poll subsystem\n"); 1064 goto fini; 1065 } 1066 1067 ctx->group = group; 1068 ctx->subsystem = subsystem; 1069 ctx->cpl_fn = cb_fn; 1070 ctx->cpl_ctx = cb_arg; 1071 1072 sgroup = &group->sgroups[subsystem->id]; 1073 sgroup->state = SPDK_NVMF_SUBSYSTEM_INACTIVE; 1074 1075 TAILQ_FOREACH(qpair, &group->qpairs, link) { 1076 if ((qpair->ctrlr != NULL) && (qpair->ctrlr->subsys == subsystem)) { 1077 break; 1078 } 1079 } 1080 1081 if (qpair) { 1082 rc = spdk_nvmf_qpair_disconnect(qpair, _nvmf_subsystem_disconnect_next_qpair, ctx); 1083 } else { 1084 /* call the callback immediately. It will handle any channel iteration */ 1085 _nvmf_poll_group_remove_subsystem_cb(ctx, 0); 1086 } 1087 1088 if (rc != 0) { 1089 free(ctx); 1090 goto fini; 1091 } 1092 1093 return; 1094 fini: 1095 if (cb_fn) { 1096 cb_fn(cb_arg, rc); 1097 } 1098 } 1099 1100 void 1101 spdk_nvmf_poll_group_pause_subsystem(struct spdk_nvmf_poll_group *group, 1102 struct spdk_nvmf_subsystem *subsystem, 1103 spdk_nvmf_poll_group_mod_done cb_fn, void *cb_arg) 1104 { 1105 struct spdk_nvmf_subsystem_poll_group *sgroup; 1106 int rc = 0; 1107 1108 if (subsystem->id >= group->num_sgroups) { 1109 rc = -1; 1110 goto fini; 1111 } 1112 1113 sgroup = &group->sgroups[subsystem->id]; 1114 if (sgroup == NULL) { 1115 rc = -1; 1116 goto fini; 1117 } 1118 1119 assert(sgroup->state == SPDK_NVMF_SUBSYSTEM_ACTIVE); 1120 /* TODO: This currently does not quiesce I/O */ 1121 sgroup->state = SPDK_NVMF_SUBSYSTEM_PAUSED; 1122 fini: 1123 if (cb_fn) { 1124 cb_fn(cb_arg, rc); 1125 } 1126 } 1127 1128 void 1129 spdk_nvmf_poll_group_resume_subsystem(struct spdk_nvmf_poll_group *group, 1130 struct spdk_nvmf_subsystem *subsystem, 1131 spdk_nvmf_poll_group_mod_done cb_fn, void *cb_arg) 1132 { 1133 struct spdk_nvmf_request *req, *tmp; 1134 struct spdk_nvmf_subsystem_poll_group *sgroup; 1135 int rc = 0; 1136 1137 if (subsystem->id >= group->num_sgroups) { 1138 rc = -1; 1139 goto fini; 1140 } 1141 1142 sgroup = &group->sgroups[subsystem->id]; 1143 1144 assert(sgroup->state == SPDK_NVMF_SUBSYSTEM_PAUSED); 1145 1146 rc = poll_group_update_subsystem(group, subsystem); 1147 if (rc) { 1148 goto fini; 1149 } 1150 1151 sgroup->state = SPDK_NVMF_SUBSYSTEM_ACTIVE; 1152 1153 /* Release all queued requests */ 1154 TAILQ_FOREACH_SAFE(req, &sgroup->queued, link, tmp) { 1155 TAILQ_REMOVE(&sgroup->queued, req, link); 1156 spdk_nvmf_request_exec(req); 1157 } 1158 fini: 1159 if (cb_fn) { 1160 cb_fn(cb_arg, rc); 1161 } 1162 } 1163