LCOV - code coverage report
Current view: top level - spdk/lib/nvmf - nvmf.c (source / functions) Hit Total Coverage
Test: Combined Lines: 715 955 74.9 %
Date: 2024-08-10 23:32:34 Functions: 57 67 85.1 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 317 540 58.7 %

           Branch data     Line data    Source code
       1                 :            : /*   SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  *   Copyright (C) 2016 Intel Corporation. All rights reserved.
       3                 :            :  *   Copyright (c) 2018-2019, 2021 Mellanox Technologies LTD. All rights reserved.
       4                 :            :  *   Copyright (c) 2021, 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
       5                 :            :  */
       6                 :            : 
       7                 :            : #include "spdk/stdinc.h"
       8                 :            : 
       9                 :            : #include "spdk/bdev.h"
      10                 :            : #include "spdk/bit_array.h"
      11                 :            : #include "spdk/thread.h"
      12                 :            : #include "spdk/nvmf.h"
      13                 :            : #include "spdk/endian.h"
      14                 :            : #include "spdk/string.h"
      15                 :            : #include "spdk/log.h"
      16                 :            : #include "spdk_internal/usdt.h"
      17                 :            : 
      18                 :            : #include "nvmf_internal.h"
      19                 :            : #include "transport.h"
      20                 :            : 
      21                 :        887 : SPDK_LOG_REGISTER_COMPONENT(nvmf)
      22                 :            : 
      23                 :            : #define SPDK_NVMF_DEFAULT_MAX_SUBSYSTEMS 1024
      24                 :            : 
      25                 :            : static TAILQ_HEAD(, spdk_nvmf_tgt) g_nvmf_tgts = TAILQ_HEAD_INITIALIZER(g_nvmf_tgts);
      26                 :            : 
      27                 :            : typedef void (*nvmf_qpair_disconnect_cpl)(void *ctx, int status);
      28                 :            : 
      29                 :            : /* supplied to a single call to nvmf_qpair_disconnect */
      30                 :            : struct nvmf_qpair_disconnect_ctx {
      31                 :            :         struct spdk_nvmf_qpair *qpair;
      32                 :            :         struct spdk_nvmf_ctrlr *ctrlr;
      33                 :            :         uint16_t qid;
      34                 :            : };
      35                 :            : 
      36                 :            : /*
      37                 :            :  * There are several times when we need to iterate through the list of all qpairs and selectively delete them.
      38                 :            :  * In order to do this sequentially without overlap, we must provide a context to recover the next qpair from
      39                 :            :  * to enable calling nvmf_qpair_disconnect on the next desired qpair.
      40                 :            :  */
      41                 :            : struct nvmf_qpair_disconnect_many_ctx {
      42                 :            :         struct spdk_nvmf_subsystem *subsystem;
      43                 :            :         struct spdk_nvmf_poll_group *group;
      44                 :            :         spdk_nvmf_poll_group_mod_done cpl_fn;
      45                 :            :         void *cpl_ctx;
      46                 :            : };
      47                 :            : 
      48                 :            : static struct spdk_nvmf_referral *
      49                 :         39 : nvmf_tgt_find_referral(struct spdk_nvmf_tgt *tgt,
      50                 :            :                        const struct spdk_nvme_transport_id *trid)
      51                 :            : {
      52                 :            :         struct spdk_nvmf_referral *referral;
      53                 :            : 
      54         [ +  + ]:         60 :         TAILQ_FOREACH(referral, &tgt->referrals, link) {
      55         [ +  + ]:         39 :                 if (spdk_nvme_transport_id_compare(&referral->trid, trid) == 0) {
      56                 :         18 :                         return referral;
      57                 :            :                 }
      58                 :            :         }
      59                 :            : 
      60                 :         21 :         return NULL;
      61                 :            : }
      62                 :            : 
      63                 :            : int
      64                 :         21 : spdk_nvmf_tgt_add_referral(struct spdk_nvmf_tgt *tgt,
      65                 :            :                            const struct spdk_nvmf_referral_opts *uopts)
      66                 :            : {
      67                 :            :         struct spdk_nvmf_referral *referral;
      68                 :         21 :         struct spdk_nvmf_referral_opts opts = {};
      69                 :         21 :         struct spdk_nvme_transport_id *trid = &opts.trid;
      70                 :            : 
      71   [ -  +  -  + ]:         21 :         memcpy(&opts, uopts, spdk_min(uopts->size, sizeof(opts)));
      72         [ +  + ]:         21 :         if (trid->subnqn[0] == '\0') {
      73         [ -  + ]:         15 :                 snprintf(trid->subnqn, sizeof(trid->subnqn), "%s", SPDK_NVMF_DISCOVERY_NQN);
      74                 :            :         }
      75                 :            : 
      76         [ -  + ]:         21 :         if (!nvmf_nqn_is_valid(trid->subnqn)) {
      77                 :          0 :                 SPDK_ERRLOG("Invalid subsystem NQN\n");
      78                 :          0 :                 return -EINVAL;
      79                 :            :         }
      80                 :            : 
      81                 :            :         /* If the entry already exists, just ignore it. */
      82         [ -  + ]:         21 :         if (nvmf_tgt_find_referral(tgt, trid)) {
      83                 :          0 :                 return 0;
      84                 :            :         }
      85                 :            : 
      86                 :         21 :         referral = calloc(1, sizeof(*referral));
      87         [ -  + ]:         21 :         if (!referral) {
      88                 :          0 :                 SPDK_ERRLOG("Failed to allocate memory for a referral\n");
      89                 :          0 :                 return -ENOMEM;
      90                 :            :         }
      91                 :            : 
      92         [ +  + ]:         21 :         referral->entry.subtype = nvmf_nqn_is_discovery(trid->subnqn) ?
      93                 :            :                                   SPDK_NVMF_SUBTYPE_DISCOVERY :
      94                 :            :                                   SPDK_NVMF_SUBTYPE_NVME;
      95         [ -  + ]:         42 :         referral->entry.treq.secure_channel = opts.secure_channel ?
      96         [ -  + ]:         21 :                                               SPDK_NVMF_TREQ_SECURE_CHANNEL_REQUIRED :
      97                 :            :                                               SPDK_NVMF_TREQ_SECURE_CHANNEL_NOT_REQUIRED;
      98                 :         21 :         referral->entry.cntlid = 0xffff;
      99                 :         21 :         referral->entry.trtype = trid->trtype;
     100                 :         21 :         referral->entry.adrfam = trid->adrfam;
     101   [ -  +  -  + ]:         21 :         memcpy(&referral->trid, trid, sizeof(struct spdk_nvme_transport_id));
     102                 :         21 :         spdk_strcpy_pad(referral->entry.subnqn, trid->subnqn, sizeof(trid->subnqn), '\0');
     103                 :         21 :         spdk_strcpy_pad(referral->entry.trsvcid, trid->trsvcid, sizeof(referral->entry.trsvcid), ' ');
     104                 :         21 :         spdk_strcpy_pad(referral->entry.traddr, trid->traddr, sizeof(referral->entry.traddr), ' ');
     105                 :            : 
     106         [ +  + ]:         21 :         TAILQ_INSERT_HEAD(&tgt->referrals, referral, link);
     107                 :         21 :         nvmf_update_discovery_log(tgt, NULL);
     108                 :            : 
     109                 :         21 :         return 0;
     110                 :            : }
     111                 :            : 
     112                 :            : int
     113                 :         18 : spdk_nvmf_tgt_remove_referral(struct spdk_nvmf_tgt *tgt,
     114                 :            :                               const struct spdk_nvmf_referral_opts *uopts)
     115                 :            : {
     116                 :            :         struct spdk_nvmf_referral *referral;
     117                 :         18 :         struct spdk_nvmf_referral_opts opts = {};
     118                 :         18 :         struct spdk_nvme_transport_id *trid = &opts.trid;
     119                 :            : 
     120   [ -  +  -  + ]:         18 :         memcpy(&opts, uopts, spdk_min(uopts->size, sizeof(opts)));
     121         [ +  + ]:         18 :         if (trid->subnqn[0] == '\0') {
     122         [ -  + ]:         12 :                 snprintf(trid->subnqn, sizeof(trid->subnqn), "%s", SPDK_NVMF_DISCOVERY_NQN);
     123                 :            :         }
     124                 :            : 
     125                 :         18 :         referral = nvmf_tgt_find_referral(tgt, &opts.trid);
     126         [ -  + ]:         18 :         if (referral == NULL) {
     127                 :          0 :                 return -ENOENT;
     128                 :            :         }
     129                 :            : 
     130         [ +  + ]:         18 :         TAILQ_REMOVE(&tgt->referrals, referral, link);
     131                 :         18 :         nvmf_update_discovery_log(tgt, NULL);
     132                 :            : 
     133                 :         18 :         free(referral);
     134                 :            : 
     135                 :         18 :         return 0;
     136                 :            : }
     137                 :            : 
     138                 :            : void
     139                 :      40248 : nvmf_qpair_set_state(struct spdk_nvmf_qpair *qpair,
     140                 :            :                      enum spdk_nvmf_qpair_state state)
     141                 :            : {
     142         [ -  + ]:      40248 :         assert(qpair != NULL);
     143         [ -  + ]:      40248 :         assert(qpair->group->thread == spdk_get_thread());
     144                 :            : 
     145                 :      40248 :         qpair->state = state;
     146                 :      40248 : }
     147                 :            : 
     148                 :            : static int
     149                 : 2813817695 : nvmf_poll_group_poll(void *ctx)
     150                 :            : {
     151                 : 2813817695 :         struct spdk_nvmf_poll_group *group = ctx;
     152                 :            :         int rc;
     153                 : 2813817695 :         int count = 0;
     154                 :            :         struct spdk_nvmf_transport_poll_group *tgroup;
     155                 :            : 
     156         [ +  + ]: 4488626350 :         TAILQ_FOREACH(tgroup, &group->tgroups, link) {
     157                 : 1674808666 :                 rc = nvmf_transport_poll_group_poll(tgroup);
     158         [ +  + ]: 1674808666 :                 if (rc < 0) {
     159                 :         11 :                         return SPDK_POLLER_BUSY;
     160                 :            :                 }
     161                 : 1674808655 :                 count += rc;
     162                 :            :         }
     163                 :            : 
     164                 : 2813817684 :         return count > 0 ? SPDK_POLLER_BUSY : SPDK_POLLER_IDLE;
     165                 :            : }
     166                 :            : 
     167                 :            : /*
     168                 :            :  * Reset and clean up the poll group (I/O channel code will actually free the
     169                 :            :  * group).
     170                 :            :  */
     171                 :            : static void
     172                 :       1386 : nvmf_tgt_cleanup_poll_group(struct spdk_nvmf_poll_group *group)
     173                 :            : {
     174                 :            :         struct spdk_nvmf_transport_poll_group *tgroup, *tmp;
     175                 :            :         struct spdk_nvmf_subsystem_poll_group *sgroup;
     176                 :            :         uint32_t sid, nsid;
     177                 :            : 
     178         [ +  + ]:       2017 :         TAILQ_FOREACH_SAFE(tgroup, &group->tgroups, link, tmp) {
     179         [ -  + ]:        631 :                 TAILQ_REMOVE(&group->tgroups, tgroup, link);
     180                 :        631 :                 nvmf_transport_poll_group_destroy(tgroup);
     181                 :            :         }
     182                 :            : 
     183         [ +  + ]:    1403631 :         for (sid = 0; sid < group->num_sgroups; sid++) {
     184                 :    1402245 :                 sgroup = &group->sgroups[sid];
     185                 :            : 
     186         [ -  + ]:    1402245 :                 assert(sgroup != NULL);
     187                 :            : 
     188         [ +  + ]:    1402250 :                 for (nsid = 0; nsid < sgroup->num_ns; nsid++) {
     189         [ +  - ]:          5 :                         if (sgroup->ns_info[nsid].channel) {
     190                 :          5 :                                 spdk_put_io_channel(sgroup->ns_info[nsid].channel);
     191                 :          5 :                                 sgroup->ns_info[nsid].channel = NULL;
     192                 :            :                         }
     193                 :            :                 }
     194                 :            : 
     195                 :    1402245 :                 free(sgroup->ns_info);
     196                 :            :         }
     197                 :            : 
     198                 :       1386 :         free(group->sgroups);
     199                 :            : 
     200                 :       1386 :         spdk_poller_unregister(&group->poller);
     201                 :            : 
     202         [ +  + ]:       1386 :         if (group->destroy_cb_fn) {
     203                 :       1381 :                 group->destroy_cb_fn(group->destroy_cb_arg, 0);
     204                 :            :         }
     205                 :       1386 : }
     206                 :            : 
     207                 :            : /*
     208                 :            :  * Callback to unregister a poll group from the target, and clean up its state.
     209                 :            :  */
     210                 :            : static void
     211                 :       1386 : nvmf_tgt_destroy_poll_group(void *io_device, void *ctx_buf)
     212                 :            : {
     213                 :       1386 :         struct spdk_nvmf_tgt *tgt = io_device;
     214                 :       1386 :         struct spdk_nvmf_poll_group *group = ctx_buf;
     215                 :            : 
     216                 :        306 :         SPDK_DTRACE_PROBE1_TICKS(nvmf_destroy_poll_group, spdk_thread_get_id(group->thread));
     217                 :            : 
     218         [ -  + ]:       1386 :         pthread_mutex_lock(&tgt->mutex);
     219         [ +  + ]:       1386 :         TAILQ_REMOVE(&tgt->poll_groups, group, link);
     220                 :       1386 :         tgt->num_poll_groups--;
     221         [ -  + ]:       1386 :         pthread_mutex_unlock(&tgt->mutex);
     222                 :            : 
     223   [ +  -  +  - ]:       1386 :         assert(!(tgt->state == NVMF_TGT_PAUSING || tgt->state == NVMF_TGT_RESUMING));
     224                 :       1386 :         nvmf_tgt_cleanup_poll_group(group);
     225                 :       1386 : }
     226                 :            : 
     227                 :            : static int
     228                 :        631 : nvmf_poll_group_add_transport(struct spdk_nvmf_poll_group *group,
     229                 :            :                               struct spdk_nvmf_transport *transport)
     230                 :            : {
     231                 :        631 :         struct spdk_nvmf_transport_poll_group *tgroup = nvmf_get_transport_poll_group(group, transport);
     232                 :            : 
     233         [ -  + ]:        631 :         if (tgroup != NULL) {
     234                 :            :                 /* Transport already in the poll group */
     235                 :          0 :                 return 0;
     236                 :            :         }
     237                 :            : 
     238                 :        631 :         tgroup = nvmf_transport_poll_group_create(transport, group);
     239         [ -  + ]:        631 :         if (!tgroup) {
     240                 :          0 :                 SPDK_ERRLOG("Unable to create poll group for transport\n");
     241                 :          0 :                 return -1;
     242                 :            :         }
     243                 :        242 :         SPDK_DTRACE_PROBE2_TICKS(nvmf_transport_poll_group_create, transport,
     244                 :            :                                  spdk_thread_get_id(group->thread));
     245                 :            : 
     246                 :        631 :         tgroup->group = group;
     247                 :        631 :         TAILQ_INSERT_TAIL(&group->tgroups, tgroup, link);
     248                 :            : 
     249                 :        631 :         return 0;
     250                 :            : }
     251                 :            : 
     252                 :            : static void
     253                 :          4 : nvmf_tgt_poller_set_interrupt_mode(struct spdk_poller *poller, void *cb_arg, bool interrupt_mode)
     254                 :            : {
     255                 :          4 : }
     256                 :            : 
     257                 :            : static int
     258                 :       1386 : nvmf_tgt_create_poll_group(void *io_device, void *ctx_buf)
     259                 :            : {
     260                 :       1386 :         struct spdk_nvmf_tgt *tgt = io_device;
     261                 :       1386 :         struct spdk_nvmf_poll_group *group = ctx_buf;
     262                 :            :         struct spdk_nvmf_transport *transport;
     263                 :            :         struct spdk_nvmf_subsystem *subsystem;
     264                 :       1386 :         struct spdk_thread *thread = spdk_get_thread();
     265                 :            :         uint32_t i;
     266                 :            :         int rc;
     267                 :            : 
     268                 :       1386 :         group->tgt = tgt;
     269                 :       1386 :         TAILQ_INIT(&group->tgroups);
     270                 :       1386 :         TAILQ_INIT(&group->qpairs);
     271                 :       1386 :         group->thread = thread;
     272         [ -  + ]:       1386 :         pthread_mutex_init(&group->mutex, NULL);
     273                 :            : 
     274                 :       1386 :         group->poller = SPDK_POLLER_REGISTER(nvmf_poll_group_poll, group, 0);
     275                 :       1386 :         spdk_poller_register_interrupt(group->poller, nvmf_tgt_poller_set_interrupt_mode, NULL);
     276                 :            : 
     277                 :        306 :         SPDK_DTRACE_PROBE1_TICKS(nvmf_create_poll_group, spdk_thread_get_id(thread));
     278                 :            : 
     279         [ +  + ]:       1391 :         TAILQ_FOREACH(transport, &tgt->transports, link) {
     280                 :          5 :                 rc = nvmf_poll_group_add_transport(group, transport);
     281         [ -  + ]:          5 :                 if (rc != 0) {
     282                 :          0 :                         nvmf_tgt_cleanup_poll_group(group);
     283                 :          0 :                         return rc;
     284                 :            :                 }
     285                 :            :         }
     286                 :            : 
     287                 :       1386 :         group->num_sgroups = tgt->max_subsystems;
     288                 :       1386 :         group->sgroups = calloc(tgt->max_subsystems, sizeof(struct spdk_nvmf_subsystem_poll_group));
     289         [ -  + ]:       1386 :         if (!group->sgroups) {
     290                 :          0 :                 nvmf_tgt_cleanup_poll_group(group);
     291                 :          0 :                 return -ENOMEM;
     292                 :            :         }
     293                 :            : 
     294         [ +  + ]:    1403631 :         for (i = 0; i < tgt->max_subsystems; i++) {
     295                 :    1402245 :                 TAILQ_INIT(&group->sgroups[i].queued);
     296                 :            :         }
     297                 :            : 
     298                 :       1386 :         for (subsystem = spdk_nvmf_subsystem_get_first(tgt);
     299         [ +  + ]:       2772 :              subsystem != NULL;
     300                 :       1386 :              subsystem = spdk_nvmf_subsystem_get_next(subsystem)) {
     301         [ -  + ]:       1386 :                 if (nvmf_poll_group_add_subsystem(group, subsystem, NULL, NULL) != 0) {
     302                 :          0 :                         nvmf_tgt_cleanup_poll_group(group);
     303                 :          0 :                         return -1;
     304                 :            :                 }
     305                 :            :         }
     306                 :            : 
     307         [ -  + ]:       1386 :         pthread_mutex_lock(&tgt->mutex);
     308                 :       1386 :         tgt->num_poll_groups++;
     309                 :       1386 :         TAILQ_INSERT_TAIL(&tgt->poll_groups, group, link);
     310         [ -  + ]:       1386 :         pthread_mutex_unlock(&tgt->mutex);
     311                 :            : 
     312                 :       1386 :         return 0;
     313                 :            : }
     314                 :            : 
     315                 :            : static void
     316                 :       1381 : _nvmf_tgt_disconnect_qpairs(void *ctx)
     317                 :            : {
     318                 :            :         struct spdk_nvmf_qpair *qpair, *qpair_tmp;
     319                 :       1381 :         struct nvmf_qpair_disconnect_many_ctx *qpair_ctx = ctx;
     320                 :       1381 :         struct spdk_nvmf_poll_group *group = qpair_ctx->group;
     321                 :            :         struct spdk_io_channel *ch;
     322                 :            :         int rc;
     323                 :            : 
     324         [ -  + ]:       1381 :         TAILQ_FOREACH_SAFE(qpair, &group->qpairs, link, qpair_tmp) {
     325                 :          0 :                 rc = spdk_nvmf_qpair_disconnect(qpair);
     326   [ #  #  #  # ]:          0 :                 if (rc && rc != -EINPROGRESS) {
     327                 :          0 :                         break;
     328                 :            :                 }
     329                 :            :         }
     330                 :            : 
     331         [ +  - ]:       1381 :         if (TAILQ_EMPTY(&group->qpairs)) {
     332                 :            :                 /* When the refcount from the channels reaches 0, nvmf_tgt_destroy_poll_group will be called. */
     333                 :       1381 :                 ch = spdk_io_channel_from_ctx(group);
     334                 :       1381 :                 spdk_put_io_channel(ch);
     335                 :       1381 :                 free(qpair_ctx);
     336                 :       1381 :                 return;
     337                 :            :         }
     338                 :            : 
     339                 :            :         /* Some qpairs are in process of being disconnected. Send a message and try to remove them again */
     340                 :          0 :         spdk_thread_send_msg(spdk_get_thread(), _nvmf_tgt_disconnect_qpairs, ctx);
     341                 :            : }
     342                 :            : 
     343                 :            : static void
     344                 :       1381 : nvmf_tgt_destroy_poll_group_qpairs(struct spdk_nvmf_poll_group *group)
     345                 :            : {
     346                 :            :         struct nvmf_qpair_disconnect_many_ctx *ctx;
     347                 :            : 
     348                 :        306 :         SPDK_DTRACE_PROBE1_TICKS(nvmf_destroy_poll_group_qpairs, spdk_thread_get_id(group->thread));
     349                 :            : 
     350                 :       1381 :         ctx = calloc(1, sizeof(struct nvmf_qpair_disconnect_many_ctx));
     351         [ -  + ]:       1381 :         if (!ctx) {
     352                 :          0 :                 SPDK_ERRLOG("Failed to allocate memory for destroy poll group ctx\n");
     353                 :          0 :                 return;
     354                 :            :         }
     355                 :            : 
     356                 :       1381 :         ctx->group = group;
     357                 :       1381 :         _nvmf_tgt_disconnect_qpairs(ctx);
     358                 :            : }
     359                 :            : 
     360                 :            : struct spdk_nvmf_tgt *
     361                 :        806 : spdk_nvmf_tgt_create(struct spdk_nvmf_target_opts *_opts)
     362                 :            : {
     363                 :            :         struct spdk_nvmf_tgt *tgt, *tmp_tgt;
     364                 :        806 :         struct spdk_nvmf_target_opts opts = {
     365                 :            :                 .max_subsystems = SPDK_NVMF_DEFAULT_MAX_SUBSYSTEMS,
     366                 :            :                 .discovery_filter = SPDK_NVMF_TGT_DISCOVERY_MATCH_ANY,
     367                 :            :         };
     368                 :            : 
     369         [ -  + ]:        806 :         memcpy(&opts, _opts, _opts->size);
     370         [ -  + ]:        806 :         if (strnlen(opts.name, NVMF_TGT_NAME_MAX_LENGTH) == NVMF_TGT_NAME_MAX_LENGTH) {
     371                 :          0 :                 SPDK_ERRLOG("Provided target name exceeds the max length of %u.\n", NVMF_TGT_NAME_MAX_LENGTH);
     372                 :          0 :                 return NULL;
     373                 :            :         }
     374                 :            : 
     375         [ +  + ]:        815 :         TAILQ_FOREACH(tmp_tgt, &g_nvmf_tgts, link) {
     376   [ -  +  -  + ]:          9 :                 if (!strncmp(opts.name, tmp_tgt->name, NVMF_TGT_NAME_MAX_LENGTH)) {
     377                 :          0 :                         SPDK_ERRLOG("Provided target name must be unique.\n");
     378                 :          0 :                         return NULL;
     379                 :            :                 }
     380                 :            :         }
     381                 :            : 
     382                 :        806 :         tgt = calloc(1, sizeof(*tgt));
     383         [ -  + ]:        806 :         if (!tgt) {
     384                 :          0 :                 return NULL;
     385                 :            :         }
     386                 :            : 
     387                 :        806 :         snprintf(tgt->name, NVMF_TGT_NAME_MAX_LENGTH, "%s", opts.name);
     388                 :            : 
     389         [ +  + ]:        806 :         if (!opts.max_subsystems) {
     390                 :        755 :                 tgt->max_subsystems = SPDK_NVMF_DEFAULT_MAX_SUBSYSTEMS;
     391                 :            :         } else {
     392                 :         51 :                 tgt->max_subsystems = opts.max_subsystems;
     393                 :            :         }
     394                 :            : 
     395                 :        806 :         tgt->crdt[0] = opts.crdt[0];
     396                 :        806 :         tgt->crdt[1] = opts.crdt[1];
     397                 :        806 :         tgt->crdt[2] = opts.crdt[2];
     398                 :        806 :         tgt->discovery_filter = opts.discovery_filter;
     399                 :        806 :         tgt->discovery_genctr = 0;
     400                 :        806 :         tgt->dhchap_digests = opts.dhchap_digests;
     401                 :        806 :         tgt->dhchap_dhgroups = opts.dhchap_dhgroups;
     402                 :        806 :         TAILQ_INIT(&tgt->transports);
     403                 :        806 :         TAILQ_INIT(&tgt->poll_groups);
     404                 :        806 :         TAILQ_INIT(&tgt->referrals);
     405                 :        806 :         tgt->num_poll_groups = 0;
     406                 :            : 
     407                 :        806 :         tgt->subsystem_ids = spdk_bit_array_create(tgt->max_subsystems);
     408         [ -  + ]:        806 :         if (tgt->subsystem_ids == NULL) {
     409                 :          0 :                 free(tgt);
     410                 :          0 :                 return NULL;
     411                 :            :         }
     412                 :            : 
     413                 :        806 :         RB_INIT(&tgt->subsystems);
     414                 :            : 
     415         [ -  + ]:        806 :         pthread_mutex_init(&tgt->mutex, NULL);
     416                 :            : 
     417                 :        806 :         spdk_io_device_register(tgt,
     418                 :            :                                 nvmf_tgt_create_poll_group,
     419                 :            :                                 nvmf_tgt_destroy_poll_group,
     420                 :            :                                 sizeof(struct spdk_nvmf_poll_group),
     421                 :        806 :                                 tgt->name);
     422                 :            : 
     423                 :        806 :         tgt->state = NVMF_TGT_RUNNING;
     424                 :            : 
     425         [ +  + ]:        806 :         TAILQ_INSERT_HEAD(&g_nvmf_tgts, tgt, link);
     426                 :            : 
     427                 :        806 :         return tgt;
     428                 :            : }
     429                 :            : 
     430                 :            : static void
     431                 :       1087 : _nvmf_tgt_destroy_next_transport(void *ctx)
     432                 :            : {
     433                 :       1087 :         struct spdk_nvmf_tgt *tgt = ctx;
     434                 :            :         struct spdk_nvmf_transport *transport;
     435                 :            : 
     436         [ +  + ]:       1087 :         if (!TAILQ_EMPTY(&tgt->transports)) {
     437                 :        281 :                 transport = TAILQ_FIRST(&tgt->transports);
     438         [ -  + ]:        281 :                 TAILQ_REMOVE(&tgt->transports, transport, link);
     439                 :        281 :                 spdk_nvmf_transport_destroy(transport, _nvmf_tgt_destroy_next_transport, tgt);
     440                 :            :         } else {
     441                 :        806 :                 spdk_nvmf_tgt_destroy_done_fn *destroy_cb_fn = tgt->destroy_cb_fn;
     442                 :        806 :                 void *destroy_cb_arg = tgt->destroy_cb_arg;
     443                 :            : 
     444         [ -  + ]:        806 :                 pthread_mutex_destroy(&tgt->mutex);
     445                 :        806 :                 free(tgt);
     446                 :            : 
     447         [ +  - ]:        806 :                 if (destroy_cb_fn) {
     448                 :        806 :                         destroy_cb_fn(destroy_cb_arg, 0);
     449                 :            :                 }
     450                 :            :         }
     451                 :       1087 : }
     452                 :            : 
     453                 :            : static void
     454                 :        806 : nvmf_tgt_destroy_cb(void *io_device)
     455                 :            : {
     456                 :        806 :         struct spdk_nvmf_tgt *tgt = io_device;
     457                 :            :         struct spdk_nvmf_subsystem *subsystem, *subsystem_next;
     458                 :            :         int rc;
     459                 :            :         struct spdk_nvmf_referral *referral;
     460                 :            : 
     461         [ +  + ]:        809 :         while ((referral = TAILQ_FIRST(&tgt->referrals))) {
     462         [ -  + ]:          3 :                 TAILQ_REMOVE(&tgt->referrals, referral, link);
     463                 :          3 :                 free(referral);
     464                 :            :         }
     465                 :            : 
     466                 :        806 :         nvmf_tgt_stop_mdns_prr(tgt);
     467                 :            : 
     468                 :            :         /* We will be freeing subsystems in this loop, so we always need to get the next one
     469                 :            :          * ahead of time, since we can't call get_next() on a subsystem that's been freed.
     470                 :            :          */
     471                 :       1612 :         for (subsystem = spdk_nvmf_subsystem_get_first(tgt),
     472                 :        806 :              subsystem_next = spdk_nvmf_subsystem_get_next(subsystem);
     473         [ +  + ]:        812 :              subsystem != NULL;
     474                 :          6 :              subsystem = subsystem_next,
     475                 :          6 :              subsystem_next = spdk_nvmf_subsystem_get_next(subsystem_next)) {
     476                 :          6 :                 nvmf_subsystem_remove_all_listeners(subsystem, true);
     477                 :            : 
     478                 :          6 :                 rc = spdk_nvmf_subsystem_destroy(subsystem, nvmf_tgt_destroy_cb, tgt);
     479         [ -  + ]:          6 :                 if (rc) {
     480         [ #  # ]:          0 :                         if (rc == -EINPROGRESS) {
     481                 :            :                                 /* If rc is -EINPROGRESS, nvmf_tgt_destroy_cb will be called again when subsystem #i
     482                 :            :                                  * is destroyed, nvmf_tgt_destroy_cb will continue to destroy other subsystems if any */
     483                 :          0 :                                 return;
     484                 :            :                         } else {
     485                 :          0 :                                 SPDK_ERRLOG("Failed to destroy subsystem %s, rc %d\n", subsystem->subnqn, rc);
     486                 :            :                         }
     487                 :            :                 }
     488                 :            :         }
     489                 :        806 :         spdk_bit_array_free(&tgt->subsystem_ids);
     490                 :        806 :         _nvmf_tgt_destroy_next_transport(tgt);
     491                 :            : }
     492                 :            : 
     493                 :            : void
     494                 :        806 : spdk_nvmf_tgt_destroy(struct spdk_nvmf_tgt *tgt,
     495                 :            :                       spdk_nvmf_tgt_destroy_done_fn cb_fn,
     496                 :            :                       void *cb_arg)
     497                 :            : {
     498   [ +  -  +  - ]:        806 :         assert(!(tgt->state == NVMF_TGT_PAUSING || tgt->state == NVMF_TGT_RESUMING));
     499                 :            : 
     500                 :        806 :         tgt->destroy_cb_fn = cb_fn;
     501                 :        806 :         tgt->destroy_cb_arg = cb_arg;
     502                 :            : 
     503         [ +  + ]:        806 :         TAILQ_REMOVE(&g_nvmf_tgts, tgt, link);
     504                 :            : 
     505                 :        806 :         spdk_io_device_unregister(tgt, nvmf_tgt_destroy_cb);
     506                 :        806 : }
     507                 :            : 
     508                 :            : const char *
     509                 :         21 : spdk_nvmf_tgt_get_name(struct spdk_nvmf_tgt *tgt)
     510                 :            : {
     511                 :         21 :         return tgt->name;
     512                 :            : }
     513                 :            : 
     514                 :            : struct spdk_nvmf_tgt *
     515                 :      10346 : spdk_nvmf_get_tgt(const char *name)
     516                 :            : {
     517                 :            :         struct spdk_nvmf_tgt *tgt;
     518                 :      10346 :         uint32_t num_targets = 0;
     519                 :            : 
     520         [ +  + ]:      20692 :         TAILQ_FOREACH(tgt, &g_nvmf_tgts, link) {
     521         [ +  + ]:      10352 :                 if (name) {
     522   [ -  +  -  +  :         24 :                         if (!strncmp(tgt->name, name, NVMF_TGT_NAME_MAX_LENGTH)) {
                   +  + ]
     523                 :          6 :                                 return tgt;
     524                 :            :                         }
     525                 :            :                 }
     526                 :      10346 :                 num_targets++;
     527                 :            :         }
     528                 :            : 
     529                 :            :         /*
     530                 :            :          * special case. If there is only one target and
     531                 :            :          * no name was specified, return the only available
     532                 :            :          * target. If there is more than one target, name must
     533                 :            :          * be specified.
     534                 :            :          */
     535   [ +  +  +  - ]:      10340 :         if (!name && num_targets == 1) {
     536                 :      10328 :                 return TAILQ_FIRST(&g_nvmf_tgts);
     537                 :            :         }
     538                 :            : 
     539                 :         12 :         return NULL;
     540                 :            : }
     541                 :            : 
     542                 :            : struct spdk_nvmf_tgt *
     543                 :          9 : spdk_nvmf_get_first_tgt(void)
     544                 :            : {
     545                 :          9 :         return TAILQ_FIRST(&g_nvmf_tgts);
     546                 :            : }
     547                 :            : 
     548                 :            : struct spdk_nvmf_tgt *
     549                 :         15 : spdk_nvmf_get_next_tgt(struct spdk_nvmf_tgt *prev)
     550                 :            : {
     551                 :         15 :         return TAILQ_NEXT(prev, link);
     552                 :            : }
     553                 :            : 
     554                 :            : static void
     555                 :         23 : nvmf_write_nvme_subsystem_config(struct spdk_json_write_ctx *w,
     556                 :            :                                  struct spdk_nvmf_subsystem *subsystem)
     557                 :            : {
     558                 :            :         struct spdk_nvmf_host *host;
     559                 :            :         struct spdk_nvmf_ns *ns;
     560                 :          1 :         struct spdk_nvmf_ns_opts ns_opts;
     561                 :            :         uint32_t max_namespaces;
     562                 :            :         struct spdk_nvmf_transport *transport;
     563                 :            : 
     564         [ -  + ]:         23 :         assert(spdk_nvmf_subsystem_get_type(subsystem) == SPDK_NVMF_SUBTYPE_NVME);
     565                 :            : 
     566                 :            :         /* { */
     567                 :         23 :         spdk_json_write_object_begin(w);
     568                 :         23 :         spdk_json_write_named_string(w, "method", "nvmf_create_subsystem");
     569                 :            : 
     570                 :            :         /*     "params" : { */
     571                 :         23 :         spdk_json_write_named_object_begin(w, "params");
     572                 :         23 :         spdk_json_write_named_string(w, "nqn", spdk_nvmf_subsystem_get_nqn(subsystem));
     573                 :         23 :         spdk_json_write_named_bool(w, "allow_any_host", spdk_nvmf_subsystem_get_allow_any_host(subsystem));
     574                 :         23 :         spdk_json_write_named_string(w, "serial_number", spdk_nvmf_subsystem_get_sn(subsystem));
     575                 :         23 :         spdk_json_write_named_string(w, "model_number", spdk_nvmf_subsystem_get_mn(subsystem));
     576                 :            : 
     577                 :         23 :         max_namespaces = spdk_nvmf_subsystem_get_max_namespaces(subsystem);
     578         [ +  - ]:         23 :         if (max_namespaces != 0) {
     579                 :         23 :                 spdk_json_write_named_uint32(w, "max_namespaces", max_namespaces);
     580                 :            :         }
     581                 :            : 
     582                 :         23 :         spdk_json_write_named_uint32(w, "min_cntlid", spdk_nvmf_subsystem_get_min_cntlid(subsystem));
     583                 :         23 :         spdk_json_write_named_uint32(w, "max_cntlid", spdk_nvmf_subsystem_get_max_cntlid(subsystem));
     584                 :         23 :         spdk_json_write_named_bool(w, "ana_reporting", spdk_nvmf_subsystem_get_ana_reporting(subsystem));
     585                 :            : 
     586                 :            :         /*     } "params" */
     587                 :         23 :         spdk_json_write_object_end(w);
     588                 :            : 
     589                 :            :         /* } */
     590                 :         23 :         spdk_json_write_object_end(w);
     591                 :            : 
     592         [ +  + ]:         29 :         for (host = spdk_nvmf_subsystem_get_first_host(subsystem); host != NULL;
     593                 :          6 :              host = spdk_nvmf_subsystem_get_next_host(subsystem, host)) {
     594                 :            : 
     595                 :          6 :                 spdk_json_write_object_begin(w);
     596                 :          6 :                 spdk_json_write_named_string(w, "method", "nvmf_subsystem_add_host");
     597                 :            : 
     598                 :            :                 /*     "params" : { */
     599                 :          6 :                 spdk_json_write_named_object_begin(w, "params");
     600                 :            : 
     601                 :          6 :                 spdk_json_write_named_string(w, "nqn", spdk_nvmf_subsystem_get_nqn(subsystem));
     602                 :          6 :                 spdk_json_write_named_string(w, "host", spdk_nvmf_host_get_nqn(host));
     603         [ -  + ]:          6 :                 if (host->dhchap_key != NULL) {
     604                 :          0 :                         spdk_json_write_named_string(w, "dhchap_key",
     605                 :            :                                                      spdk_key_get_name(host->dhchap_key));
     606                 :            :                 }
     607         [ -  + ]:          6 :                 if (host->dhchap_ctrlr_key != NULL) {
     608                 :          0 :                         spdk_json_write_named_string(w, "dhchap_ctrlr_key",
     609                 :            :                                                      spdk_key_get_name(host->dhchap_ctrlr_key));
     610                 :            :                 }
     611         [ +  + ]:         12 :                 TAILQ_FOREACH(transport, &subsystem->tgt->transports, link) {
     612         [ +  - ]:          6 :                         if (transport->ops->subsystem_dump_host != NULL) {
     613                 :          6 :                                 transport->ops->subsystem_dump_host(transport, subsystem, host->nqn, w);
     614                 :            :                         }
     615                 :            :                 }
     616                 :            : 
     617                 :            :                 /*     } "params" */
     618                 :          6 :                 spdk_json_write_object_end(w);
     619                 :            : 
     620                 :            :                 /* } */
     621                 :          6 :                 spdk_json_write_object_end(w);
     622                 :            :         }
     623                 :            : 
     624         [ +  + ]:         62 :         for (ns = spdk_nvmf_subsystem_get_first_ns(subsystem); ns != NULL;
     625                 :         39 :              ns = spdk_nvmf_subsystem_get_next_ns(subsystem, ns)) {
     626                 :         39 :                 spdk_nvmf_ns_get_opts(ns, &ns_opts, sizeof(ns_opts));
     627                 :            : 
     628                 :         39 :                 spdk_json_write_object_begin(w);
     629                 :         39 :                 spdk_json_write_named_string(w, "method", "nvmf_subsystem_add_ns");
     630                 :            : 
     631                 :            :                 /*     "params" : { */
     632                 :         39 :                 spdk_json_write_named_object_begin(w, "params");
     633                 :            : 
     634                 :         39 :                 spdk_json_write_named_string(w, "nqn", spdk_nvmf_subsystem_get_nqn(subsystem));
     635                 :            : 
     636                 :            :                 /*     "namespace" : { */
     637                 :         39 :                 spdk_json_write_named_object_begin(w, "namespace");
     638                 :            : 
     639                 :         39 :                 spdk_json_write_named_uint32(w, "nsid", spdk_nvmf_ns_get_id(ns));
     640                 :         39 :                 spdk_json_write_named_string(w, "bdev_name", spdk_bdev_get_name(spdk_nvmf_ns_get_bdev(ns)));
     641                 :            : 
     642         [ -  + ]:         39 :                 if (ns->ptpl_file != NULL) {
     643                 :          0 :                         spdk_json_write_named_string(w, "ptpl_file", ns->ptpl_file);
     644                 :            :                 }
     645                 :            : 
     646         [ +  - ]:         39 :                 if (!spdk_mem_all_zero(ns_opts.nguid, sizeof(ns_opts.nguid))) {
     647                 :            :                         SPDK_STATIC_ASSERT(sizeof(ns_opts.nguid) == sizeof(uint64_t) * 2, "size mismatch");
     648                 :         39 :                         spdk_json_write_named_string_fmt(w, "nguid", "%016"PRIX64"%016"PRIX64, from_be64(&ns_opts.nguid[0]),
     649                 :            :                                                          from_be64(&ns_opts.nguid[8]));
     650                 :            :                 }
     651                 :            : 
     652         [ -  + ]:         39 :                 if (!spdk_mem_all_zero(ns_opts.eui64, sizeof(ns_opts.eui64))) {
     653                 :            :                         SPDK_STATIC_ASSERT(sizeof(ns_opts.eui64) == sizeof(uint64_t), "size mismatch");
     654                 :          0 :                         spdk_json_write_named_string_fmt(w, "eui64", "%016"PRIX64, from_be64(&ns_opts.eui64));
     655                 :            :                 }
     656                 :            : 
     657         [ +  - ]:         39 :                 if (!spdk_uuid_is_null(&ns_opts.uuid)) {
     658                 :         39 :                         spdk_json_write_named_uuid(w, "uuid",  &ns_opts.uuid);
     659                 :            :                 }
     660                 :            : 
     661         [ -  + ]:         39 :                 if (spdk_nvmf_subsystem_get_ana_reporting(subsystem)) {
     662                 :          0 :                         spdk_json_write_named_uint32(w, "anagrpid", ns_opts.anagrpid);
     663                 :            :                 }
     664                 :            : 
     665         [ -  + ]:         39 :                 spdk_json_write_named_bool(w, "no_auto_visible", !ns->always_visible);
     666                 :            : 
     667                 :            :                 /*     "namespace" */
     668                 :         39 :                 spdk_json_write_object_end(w);
     669                 :            : 
     670                 :            :                 /*     } "params" */
     671                 :         39 :                 spdk_json_write_object_end(w);
     672                 :            : 
     673                 :            :                 /* } */
     674                 :         39 :                 spdk_json_write_object_end(w);
     675                 :            : 
     676         [ -  + ]:         39 :                 TAILQ_FOREACH(host, &ns->hosts, link) {
     677                 :          0 :                         spdk_json_write_object_begin(w);
     678                 :          0 :                         spdk_json_write_named_string(w, "method", "nvmf_ns_add_host");
     679                 :          0 :                         spdk_json_write_named_object_begin(w, "params");
     680                 :          0 :                         spdk_json_write_named_string(w, "nqn", spdk_nvmf_subsystem_get_nqn(subsystem));
     681                 :          0 :                         spdk_json_write_named_uint32(w, "nsid", spdk_nvmf_ns_get_id(ns));
     682                 :          0 :                         spdk_json_write_named_string(w, "host", spdk_nvmf_host_get_nqn(host));
     683                 :          0 :                         spdk_json_write_object_end(w);
     684                 :          0 :                         spdk_json_write_object_end(w);
     685                 :            :                 }
     686                 :            :         }
     687                 :         23 : }
     688                 :            : 
     689                 :            : static void
     690                 :        130 : nvmf_write_subsystem_config_json(struct spdk_json_write_ctx *w,
     691                 :            :                                  struct spdk_nvmf_subsystem *subsystem)
     692                 :            : {
     693                 :            :         struct spdk_nvmf_subsystem_listener *listener;
     694                 :            :         struct spdk_nvmf_transport *transport;
     695                 :            :         const struct spdk_nvme_transport_id *trid;
     696                 :            : 
     697         [ +  + ]:        130 :         if (spdk_nvmf_subsystem_get_type(subsystem) == SPDK_NVMF_SUBTYPE_NVME) {
     698                 :         23 :                 nvmf_write_nvme_subsystem_config(w, subsystem);
     699                 :            :         }
     700                 :            : 
     701         [ +  + ]:        153 :         for (listener = spdk_nvmf_subsystem_get_first_listener(subsystem); listener != NULL;
     702                 :         23 :              listener = spdk_nvmf_subsystem_get_next_listener(subsystem, listener)) {
     703                 :         23 :                 transport = listener->transport;
     704                 :         23 :                 trid = spdk_nvmf_subsystem_listener_get_trid(listener);
     705                 :            : 
     706                 :         23 :                 spdk_json_write_object_begin(w);
     707                 :         23 :                 spdk_json_write_named_string(w, "method", "nvmf_subsystem_add_listener");
     708                 :            : 
     709                 :            :                 /*     "params" : { */
     710                 :         23 :                 spdk_json_write_named_object_begin(w, "params");
     711                 :            : 
     712                 :         23 :                 spdk_json_write_named_string(w, "nqn", spdk_nvmf_subsystem_get_nqn(subsystem));
     713                 :            : 
     714                 :         23 :                 spdk_json_write_named_object_begin(w, "listen_address");
     715                 :         23 :                 nvmf_transport_listen_dump_trid(trid, w);
     716                 :         23 :                 spdk_json_write_object_end(w);
     717         [ -  + ]:         23 :                 if (transport->ops->listen_dump_opts) {
     718                 :          0 :                         transport->ops->listen_dump_opts(transport, trid, w);
     719                 :            :                 }
     720                 :            : 
     721         [ -  + ]:         23 :                 spdk_json_write_named_bool(w, "secure_channel", listener->opts.secure_channel);
     722                 :            : 
     723         [ +  + ]:         23 :                 if (listener->opts.sock_impl) {
     724                 :          3 :                         spdk_json_write_named_string(w, "sock_impl", listener->opts.sock_impl);
     725                 :            :                 }
     726                 :            : 
     727                 :            :                 /*     } "params" */
     728                 :         23 :                 spdk_json_write_object_end(w);
     729                 :            : 
     730                 :            :                 /* } */
     731                 :         23 :                 spdk_json_write_object_end(w);
     732                 :            :         }
     733                 :        130 : }
     734                 :            : 
     735                 :            : void
     736                 :        107 : spdk_nvmf_tgt_write_config_json(struct spdk_json_write_ctx *w, struct spdk_nvmf_tgt *tgt)
     737                 :            : {
     738                 :            :         struct spdk_nvmf_subsystem *subsystem;
     739                 :            :         struct spdk_nvmf_transport *transport;
     740                 :            :         struct spdk_nvmf_referral *referral;
     741                 :            : 
     742                 :        107 :         spdk_json_write_object_begin(w);
     743                 :        107 :         spdk_json_write_named_string(w, "method", "nvmf_set_max_subsystems");
     744                 :            : 
     745                 :        107 :         spdk_json_write_named_object_begin(w, "params");
     746                 :        107 :         spdk_json_write_named_uint32(w, "max_subsystems", tgt->max_subsystems);
     747                 :        107 :         spdk_json_write_object_end(w);
     748                 :            : 
     749                 :        107 :         spdk_json_write_object_end(w);
     750                 :            : 
     751                 :        107 :         spdk_json_write_object_begin(w);
     752                 :        107 :         spdk_json_write_named_string(w, "method", "nvmf_set_crdt");
     753                 :        107 :         spdk_json_write_named_object_begin(w, "params");
     754                 :        107 :         spdk_json_write_named_uint32(w, "crdt1", tgt->crdt[0]);
     755                 :        107 :         spdk_json_write_named_uint32(w, "crdt2", tgt->crdt[1]);
     756                 :        107 :         spdk_json_write_named_uint32(w, "crdt3", tgt->crdt[2]);
     757                 :        107 :         spdk_json_write_object_end(w);
     758                 :        107 :         spdk_json_write_object_end(w);
     759                 :            : 
     760                 :            :         /* write transports */
     761         [ +  + ]:        155 :         TAILQ_FOREACH(transport, &tgt->transports, link) {
     762                 :         48 :                 spdk_json_write_object_begin(w);
     763                 :         48 :                 spdk_json_write_named_string(w, "method", "nvmf_create_transport");
     764                 :         48 :                 nvmf_transport_dump_opts(transport, w, true);
     765                 :         48 :                 spdk_json_write_object_end(w);
     766                 :            :         }
     767                 :            : 
     768         [ -  + ]:        107 :         TAILQ_FOREACH(referral, &tgt->referrals, link) {
     769                 :          0 :                 spdk_json_write_object_begin(w);
     770                 :          0 :                 spdk_json_write_named_string(w, "method", "nvmf_discovery_add_referral");
     771                 :            : 
     772                 :          0 :                 spdk_json_write_named_object_begin(w, "params");
     773                 :          0 :                 spdk_json_write_named_object_begin(w, "address");
     774                 :          0 :                 nvmf_transport_listen_dump_trid(&referral->trid, w);
     775                 :          0 :                 spdk_json_write_object_end(w);
     776                 :          0 :                 spdk_json_write_named_bool(w, "secure_channel",
     777                 :          0 :                                            referral->entry.treq.secure_channel ==
     778                 :            :                                            SPDK_NVMF_TREQ_SECURE_CHANNEL_REQUIRED);
     779                 :          0 :                 spdk_json_write_named_string(w, "subnqn", referral->trid.subnqn);
     780                 :          0 :                 spdk_json_write_object_end(w);
     781                 :            : 
     782                 :          0 :                 spdk_json_write_object_end(w);
     783                 :            :         }
     784                 :            : 
     785                 :        107 :         subsystem = spdk_nvmf_subsystem_get_first(tgt);
     786         [ +  + ]:        237 :         while (subsystem) {
     787                 :        130 :                 nvmf_write_subsystem_config_json(w, subsystem);
     788                 :        130 :                 subsystem = spdk_nvmf_subsystem_get_next(subsystem);
     789                 :            :         }
     790                 :        107 : }
     791                 :            : 
     792                 :            : static void
     793                 :       1111 : nvmf_listen_opts_copy(struct spdk_nvmf_listen_opts *opts,
     794                 :            :                       const struct spdk_nvmf_listen_opts *opts_src, size_t opts_size)
     795                 :            : {
     796         [ -  + ]:       1111 :         assert(opts);
     797         [ -  + ]:       1111 :         assert(opts_src);
     798                 :            : 
     799                 :       1111 :         opts->opts_size = opts_size;
     800                 :            : 
     801                 :            : #define SET_FIELD(field) \
     802                 :            :     if (offsetof(struct spdk_nvmf_listen_opts, field) + sizeof(opts->field) <= opts_size) { \
     803                 :            :                  opts->field = opts_src->field; \
     804                 :            :     } \
     805                 :            : 
     806         [ +  - ]:       1111 :         SET_FIELD(transport_specific);
     807   [ +  -  -  + ]:       1111 :         SET_FIELD(secure_channel);
     808         [ +  - ]:       1111 :         SET_FIELD(ana_state);
     809         [ +  - ]:       1111 :         SET_FIELD(sock_impl);
     810                 :            : #undef SET_FIELD
     811                 :            : 
     812                 :            :         /* Do not remove this statement, you should always update this statement when you adding a new field,
     813                 :            :          * and do not forget to add the SET_FIELD statement for your added field. */
     814                 :            :         SPDK_STATIC_ASSERT(sizeof(struct spdk_nvmf_listen_opts) == 32, "Incorrect size");
     815                 :       1111 : }
     816                 :            : 
     817                 :            : void
     818                 :        557 : spdk_nvmf_listen_opts_init(struct spdk_nvmf_listen_opts *opts, size_t opts_size)
     819                 :            : {
     820                 :        557 :         struct spdk_nvmf_listen_opts opts_local = {};
     821                 :            : 
     822                 :            :         /* local version of opts should have defaults set here */
     823                 :        557 :         opts_local.ana_state = SPDK_NVME_ANA_OPTIMIZED_STATE;
     824                 :        557 :         nvmf_listen_opts_copy(opts, &opts_local, opts_size);
     825                 :        557 : }
     826                 :            : 
     827                 :            : int
     828                 :        554 : spdk_nvmf_tgt_listen_ext(struct spdk_nvmf_tgt *tgt, const struct spdk_nvme_transport_id *trid,
     829                 :            :                          struct spdk_nvmf_listen_opts *opts)
     830                 :            : {
     831                 :            :         struct spdk_nvmf_transport *transport;
     832                 :            :         int rc;
     833                 :        554 :         struct spdk_nvmf_listen_opts opts_local = {};
     834                 :            : 
     835         [ -  + ]:        554 :         if (!opts) {
     836                 :          0 :                 SPDK_ERRLOG("opts should not be NULL\n");
     837                 :          0 :                 return -EINVAL;
     838                 :            :         }
     839                 :            : 
     840         [ -  + ]:        554 :         if (!opts->opts_size) {
     841                 :          0 :                 SPDK_ERRLOG("The opts_size in opts structure should not be zero\n");
     842                 :          0 :                 return -EINVAL;
     843                 :            :         }
     844                 :            : 
     845                 :        554 :         transport = spdk_nvmf_tgt_get_transport(tgt, trid->trstring);
     846         [ -  + ]:        554 :         if (!transport) {
     847                 :          0 :                 SPDK_ERRLOG("Unable to find %s transport. The transport must be created first also make sure it is properly registered.\n",
     848                 :            :                             trid->trstring);
     849                 :          0 :                 return -EINVAL;
     850                 :            :         }
     851                 :            : 
     852                 :        554 :         nvmf_listen_opts_copy(&opts_local, opts, opts->opts_size);
     853                 :        554 :         rc = spdk_nvmf_transport_listen(transport, trid, &opts_local);
     854         [ -  + ]:        554 :         if (rc < 0) {
     855                 :          0 :                 SPDK_ERRLOG("Unable to listen on address '%s'\n", trid->traddr);
     856                 :            :         }
     857                 :            : 
     858                 :        554 :         return rc;
     859                 :            : }
     860                 :            : 
     861                 :            : int
     862                 :          0 : spdk_nvmf_tgt_stop_listen(struct spdk_nvmf_tgt *tgt,
     863                 :            :                           struct spdk_nvme_transport_id *trid)
     864                 :            : {
     865                 :            :         struct spdk_nvmf_transport *transport;
     866                 :            :         int rc;
     867                 :            : 
     868                 :          0 :         transport = spdk_nvmf_tgt_get_transport(tgt, trid->trstring);
     869         [ #  # ]:          0 :         if (!transport) {
     870                 :          0 :                 SPDK_ERRLOG("Unable to find %s transport. The transport must be created first also make sure it is properly registered.\n",
     871                 :            :                             trid->trstring);
     872                 :          0 :                 return -EINVAL;
     873                 :            :         }
     874                 :            : 
     875                 :          0 :         rc = spdk_nvmf_transport_stop_listen(transport, trid);
     876         [ #  # ]:          0 :         if (rc < 0) {
     877                 :          0 :                 SPDK_ERRLOG("Failed to stop listening on address '%s'\n", trid->traddr);
     878                 :          0 :                 return rc;
     879                 :            :         }
     880                 :          0 :         return 0;
     881                 :            : }
     882                 :            : 
     883                 :            : struct spdk_nvmf_tgt_add_transport_ctx {
     884                 :            :         struct spdk_nvmf_tgt *tgt;
     885                 :            :         struct spdk_nvmf_transport *transport;
     886                 :            :         spdk_nvmf_tgt_add_transport_done_fn cb_fn;
     887                 :            :         void *cb_arg;
     888                 :            :         int status;
     889                 :            : };
     890                 :            : 
     891                 :            : static void
     892                 :          0 : _nvmf_tgt_remove_transport_done(struct spdk_io_channel_iter *i, int status)
     893                 :            : {
     894                 :          0 :         struct spdk_nvmf_tgt_add_transport_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
     895                 :            : 
     896                 :          0 :         ctx->cb_fn(ctx->cb_arg, ctx->status);
     897                 :          0 :         free(ctx);
     898                 :          0 : }
     899                 :            : 
     900                 :            : static void
     901                 :          0 : _nvmf_tgt_remove_transport(struct spdk_io_channel_iter *i)
     902                 :            : {
     903                 :          0 :         struct spdk_nvmf_tgt_add_transport_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
     904                 :          0 :         struct spdk_io_channel *ch = spdk_io_channel_iter_get_channel(i);
     905                 :          0 :         struct spdk_nvmf_poll_group *group = spdk_io_channel_get_ctx(ch);
     906                 :            :         struct spdk_nvmf_transport_poll_group *tgroup, *tmp;
     907                 :            : 
     908         [ #  # ]:          0 :         TAILQ_FOREACH_SAFE(tgroup, &group->tgroups, link, tmp) {
     909         [ #  # ]:          0 :                 if (tgroup->transport == ctx->transport) {
     910         [ #  # ]:          0 :                         TAILQ_REMOVE(&group->tgroups, tgroup, link);
     911                 :          0 :                         nvmf_transport_poll_group_destroy(tgroup);
     912                 :            :                 }
     913                 :            :         }
     914                 :            : 
     915                 :          0 :         spdk_for_each_channel_continue(i, 0);
     916                 :          0 : }
     917                 :            : 
     918                 :            : static void
     919                 :        281 : _nvmf_tgt_add_transport_done(struct spdk_io_channel_iter *i, int status)
     920                 :            : {
     921                 :        281 :         struct spdk_nvmf_tgt_add_transport_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
     922                 :            : 
     923         [ -  + ]:        281 :         if (status) {
     924                 :          0 :                 ctx->status = status;
     925                 :          0 :                 spdk_for_each_channel(ctx->tgt,
     926                 :            :                                       _nvmf_tgt_remove_transport,
     927                 :            :                                       ctx,
     928                 :            :                                       _nvmf_tgt_remove_transport_done);
     929                 :          0 :                 return;
     930                 :            :         }
     931                 :            : 
     932                 :        281 :         ctx->transport->tgt = ctx->tgt;
     933                 :        281 :         TAILQ_INSERT_TAIL(&ctx->tgt->transports, ctx->transport, link);
     934                 :        281 :         ctx->cb_fn(ctx->cb_arg, status);
     935                 :        281 :         free(ctx);
     936                 :            : }
     937                 :            : 
     938                 :            : static void
     939                 :        626 : _nvmf_tgt_add_transport(struct spdk_io_channel_iter *i)
     940                 :            : {
     941                 :        626 :         struct spdk_nvmf_tgt_add_transport_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
     942                 :        626 :         struct spdk_io_channel *ch = spdk_io_channel_iter_get_channel(i);
     943                 :        626 :         struct spdk_nvmf_poll_group *group = spdk_io_channel_get_ctx(ch);
     944                 :            :         int rc;
     945                 :            : 
     946                 :        626 :         rc = nvmf_poll_group_add_transport(group, ctx->transport);
     947                 :        626 :         spdk_for_each_channel_continue(i, rc);
     948                 :        626 : }
     949                 :            : 
     950                 :            : void
     951                 :        281 : spdk_nvmf_tgt_add_transport(struct spdk_nvmf_tgt *tgt,
     952                 :            :                             struct spdk_nvmf_transport *transport,
     953                 :            :                             spdk_nvmf_tgt_add_transport_done_fn cb_fn,
     954                 :            :                             void *cb_arg)
     955                 :            : {
     956                 :            :         struct spdk_nvmf_tgt_add_transport_ctx *ctx;
     957                 :            : 
     958                 :        110 :         SPDK_DTRACE_PROBE2_TICKS(nvmf_tgt_add_transport, transport, tgt->name);
     959                 :            : 
     960         [ -  + ]:        281 :         if (spdk_nvmf_tgt_get_transport(tgt, transport->ops->name)) {
     961                 :          0 :                 cb_fn(cb_arg, -EEXIST);
     962                 :          0 :                 return; /* transport already created */
     963                 :            :         }
     964                 :            : 
     965                 :        281 :         ctx = calloc(1, sizeof(*ctx));
     966         [ -  + ]:        281 :         if (!ctx) {
     967                 :          0 :                 cb_fn(cb_arg, -ENOMEM);
     968                 :          0 :                 return;
     969                 :            :         }
     970                 :            : 
     971                 :        281 :         ctx->tgt = tgt;
     972                 :        281 :         ctx->transport = transport;
     973                 :        281 :         ctx->cb_fn = cb_fn;
     974                 :        281 :         ctx->cb_arg = cb_arg;
     975                 :            : 
     976                 :        281 :         spdk_for_each_channel(tgt,
     977                 :            :                               _nvmf_tgt_add_transport,
     978                 :            :                               ctx,
     979                 :            :                               _nvmf_tgt_add_transport_done);
     980                 :            : }
     981                 :            : 
     982                 :            : struct nvmf_tgt_pause_ctx {
     983                 :            :         struct spdk_nvmf_tgt *tgt;
     984                 :            :         spdk_nvmf_tgt_pause_polling_cb_fn cb_fn;
     985                 :            :         void *cb_arg;
     986                 :            : };
     987                 :            : 
     988                 :            : static void
     989                 :          0 : _nvmf_tgt_pause_polling_done(struct spdk_io_channel_iter *i, int status)
     990                 :            : {
     991                 :          0 :         struct nvmf_tgt_pause_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
     992                 :            : 
     993                 :          0 :         ctx->tgt->state = NVMF_TGT_PAUSED;
     994                 :            : 
     995                 :          0 :         ctx->cb_fn(ctx->cb_arg, status);
     996                 :          0 :         free(ctx);
     997                 :          0 : }
     998                 :            : 
     999                 :            : static void
    1000                 :          0 : _nvmf_tgt_pause_polling(struct spdk_io_channel_iter *i)
    1001                 :            : {
    1002                 :          0 :         struct spdk_io_channel *ch = spdk_io_channel_iter_get_channel(i);
    1003                 :          0 :         struct spdk_nvmf_poll_group *group = spdk_io_channel_get_ctx(ch);
    1004                 :            : 
    1005                 :          0 :         spdk_poller_unregister(&group->poller);
    1006                 :            : 
    1007                 :          0 :         spdk_for_each_channel_continue(i, 0);
    1008                 :          0 : }
    1009                 :            : 
    1010                 :            : int
    1011                 :          0 : spdk_nvmf_tgt_pause_polling(struct spdk_nvmf_tgt *tgt, spdk_nvmf_tgt_pause_polling_cb_fn cb_fn,
    1012                 :            :                             void *cb_arg)
    1013                 :            : {
    1014                 :            :         struct nvmf_tgt_pause_ctx *ctx;
    1015                 :            : 
    1016                 :          0 :         SPDK_DTRACE_PROBE2_TICKS(nvmf_tgt_pause_polling, tgt, tgt->name);
    1017                 :            : 
    1018      [ #  #  # ]:          0 :         switch (tgt->state) {
    1019                 :          0 :         case NVMF_TGT_PAUSING:
    1020                 :            :         case NVMF_TGT_RESUMING:
    1021                 :          0 :                 return -EBUSY;
    1022                 :          0 :         case NVMF_TGT_RUNNING:
    1023                 :          0 :                 break;
    1024                 :          0 :         default:
    1025                 :          0 :                 return -EINVAL;
    1026                 :            :         }
    1027                 :            : 
    1028                 :          0 :         ctx = calloc(1, sizeof(*ctx));
    1029         [ #  # ]:          0 :         if (!ctx) {
    1030                 :          0 :                 return -ENOMEM;
    1031                 :            :         }
    1032                 :            : 
    1033                 :            : 
    1034                 :          0 :         tgt->state = NVMF_TGT_PAUSING;
    1035                 :            : 
    1036                 :          0 :         ctx->tgt = tgt;
    1037                 :          0 :         ctx->cb_fn = cb_fn;
    1038                 :          0 :         ctx->cb_arg = cb_arg;
    1039                 :            : 
    1040                 :          0 :         spdk_for_each_channel(tgt,
    1041                 :            :                               _nvmf_tgt_pause_polling,
    1042                 :            :                               ctx,
    1043                 :            :                               _nvmf_tgt_pause_polling_done);
    1044                 :          0 :         return 0;
    1045                 :            : }
    1046                 :            : 
    1047                 :            : static void
    1048                 :          0 : _nvmf_tgt_resume_polling_done(struct spdk_io_channel_iter *i, int status)
    1049                 :            : {
    1050                 :          0 :         struct nvmf_tgt_pause_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
    1051                 :            : 
    1052                 :          0 :         ctx->tgt->state = NVMF_TGT_RUNNING;
    1053                 :            : 
    1054                 :          0 :         ctx->cb_fn(ctx->cb_arg, status);
    1055                 :          0 :         free(ctx);
    1056                 :          0 : }
    1057                 :            : 
    1058                 :            : static void
    1059                 :          0 : _nvmf_tgt_resume_polling(struct spdk_io_channel_iter *i)
    1060                 :            : {
    1061                 :          0 :         struct spdk_io_channel *ch = spdk_io_channel_iter_get_channel(i);
    1062                 :          0 :         struct spdk_nvmf_poll_group *group = spdk_io_channel_get_ctx(ch);
    1063                 :            : 
    1064         [ #  # ]:          0 :         assert(group->poller == NULL);
    1065                 :          0 :         group->poller = SPDK_POLLER_REGISTER(nvmf_poll_group_poll, group, 0);
    1066                 :            : 
    1067                 :          0 :         spdk_for_each_channel_continue(i, 0);
    1068                 :          0 : }
    1069                 :            : 
    1070                 :            : int
    1071                 :          0 : spdk_nvmf_tgt_resume_polling(struct spdk_nvmf_tgt *tgt, spdk_nvmf_tgt_resume_polling_cb_fn cb_fn,
    1072                 :            :                              void *cb_arg)
    1073                 :            : {
    1074                 :            :         struct nvmf_tgt_pause_ctx *ctx;
    1075                 :            : 
    1076                 :          0 :         SPDK_DTRACE_PROBE2_TICKS(nvmf_tgt_resume_polling, tgt, tgt->name);
    1077                 :            : 
    1078      [ #  #  # ]:          0 :         switch (tgt->state) {
    1079                 :          0 :         case NVMF_TGT_PAUSING:
    1080                 :            :         case NVMF_TGT_RESUMING:
    1081                 :          0 :                 return -EBUSY;
    1082                 :          0 :         case NVMF_TGT_PAUSED:
    1083                 :          0 :                 break;
    1084                 :          0 :         default:
    1085                 :          0 :                 return -EINVAL;
    1086                 :            :         }
    1087                 :            : 
    1088                 :          0 :         ctx = calloc(1, sizeof(*ctx));
    1089         [ #  # ]:          0 :         if (!ctx) {
    1090                 :          0 :                 return -ENOMEM;
    1091                 :            :         }
    1092                 :            : 
    1093                 :          0 :         tgt->state = NVMF_TGT_RESUMING;
    1094                 :            : 
    1095                 :          0 :         ctx->tgt = tgt;
    1096                 :          0 :         ctx->cb_fn = cb_fn;
    1097                 :          0 :         ctx->cb_arg = cb_arg;
    1098                 :            : 
    1099                 :          0 :         spdk_for_each_channel(tgt,
    1100                 :            :                               _nvmf_tgt_resume_polling,
    1101                 :            :                               ctx,
    1102                 :            :                               _nvmf_tgt_resume_polling_done);
    1103                 :          0 :         return 0;
    1104                 :            : }
    1105                 :            : 
    1106                 :            : struct spdk_nvmf_subsystem *
    1107                 :      48929 : spdk_nvmf_tgt_find_subsystem(struct spdk_nvmf_tgt *tgt, const char *subnqn)
    1108                 :            : {
    1109                 :       1094 :         struct spdk_nvmf_subsystem subsystem;
    1110                 :            : 
    1111         [ -  + ]:      48929 :         if (!subnqn) {
    1112                 :          0 :                 return NULL;
    1113                 :            :         }
    1114                 :            : 
    1115                 :            :         /* Ensure that subnqn is null terminated */
    1116   [ -  +  -  + ]:      48929 :         if (!memchr(subnqn, '\0', SPDK_NVMF_NQN_MAX_LEN + 1)) {
    1117                 :          0 :                 SPDK_ERRLOG("Connect SUBNQN is not null terminated\n");
    1118                 :          0 :                 return NULL;
    1119                 :            :         }
    1120                 :            : 
    1121                 :      48929 :         snprintf(subsystem.subnqn, sizeof(subsystem.subnqn), "%s", subnqn);
    1122                 :      48929 :         return RB_FIND(subsystem_tree, &tgt->subsystems, &subsystem);
    1123                 :            : }
    1124                 :            : 
    1125                 :            : struct spdk_nvmf_transport *
    1126                 :       2250 : spdk_nvmf_tgt_get_transport(struct spdk_nvmf_tgt *tgt, const char *transport_name)
    1127                 :            : {
    1128                 :            :         struct spdk_nvmf_transport *transport;
    1129                 :            : 
    1130         [ +  + ]:       2250 :         TAILQ_FOREACH(transport, &tgt->transports, link) {
    1131   [ -  +  -  +  :       1667 :                 if (!strncasecmp(transport->ops->name, transport_name, SPDK_NVMF_TRSTRING_MAX_LEN)) {
                   +  - ]
    1132                 :       1667 :                         return transport;
    1133                 :            :                 }
    1134                 :            :         }
    1135                 :        583 :         return NULL;
    1136                 :            : }
    1137                 :            : 
    1138                 :            : struct nvmf_new_qpair_ctx {
    1139                 :            :         struct spdk_nvmf_qpair *qpair;
    1140                 :            :         struct spdk_nvmf_poll_group *group;
    1141                 :            : };
    1142                 :            : 
    1143                 :            : static void
    1144                 :       9846 : _nvmf_poll_group_add(void *_ctx)
    1145                 :            : {
    1146                 :       9846 :         struct nvmf_new_qpair_ctx *ctx = _ctx;
    1147                 :       9846 :         struct spdk_nvmf_qpair *qpair = ctx->qpair;
    1148                 :       9846 :         struct spdk_nvmf_poll_group *group = ctx->group;
    1149                 :            : 
    1150                 :       9846 :         free(_ctx);
    1151                 :            : 
    1152         [ -  + ]:       9846 :         if (spdk_nvmf_poll_group_add(group, qpair) != 0) {
    1153                 :          0 :                 SPDK_ERRLOG("Unable to add the qpair to a poll group.\n");
    1154                 :          0 :                 spdk_nvmf_qpair_disconnect(qpair);
    1155                 :            :         }
    1156                 :       9846 : }
    1157                 :            : 
    1158                 :            : void
    1159                 :       9846 : spdk_nvmf_tgt_new_qpair(struct spdk_nvmf_tgt *tgt, struct spdk_nvmf_qpair *qpair)
    1160                 :            : {
    1161                 :            :         struct spdk_nvmf_poll_group *group;
    1162                 :            :         struct nvmf_new_qpair_ctx *ctx;
    1163                 :            : 
    1164                 :       9846 :         group = spdk_nvmf_get_optimal_poll_group(qpair);
    1165         [ -  + ]:       9846 :         if (group == NULL) {
    1166         [ #  # ]:          0 :                 if (tgt->next_poll_group == NULL) {
    1167                 :          0 :                         tgt->next_poll_group = TAILQ_FIRST(&tgt->poll_groups);
    1168         [ #  # ]:          0 :                         if (tgt->next_poll_group == NULL) {
    1169                 :          0 :                                 SPDK_ERRLOG("No poll groups exist.\n");
    1170                 :          0 :                                 spdk_nvmf_qpair_disconnect(qpair);
    1171                 :          0 :                                 return;
    1172                 :            :                         }
    1173                 :            :                 }
    1174                 :          0 :                 group = tgt->next_poll_group;
    1175                 :          0 :                 tgt->next_poll_group = TAILQ_NEXT(group, link);
    1176                 :            :         }
    1177                 :            : 
    1178                 :       9846 :         ctx = calloc(1, sizeof(*ctx));
    1179         [ -  + ]:       9846 :         if (!ctx) {
    1180                 :          0 :                 SPDK_ERRLOG("Unable to send message to poll group.\n");
    1181                 :          0 :                 spdk_nvmf_qpair_disconnect(qpair);
    1182                 :          0 :                 return;
    1183                 :            :         }
    1184                 :            : 
    1185                 :       9846 :         ctx->qpair = qpair;
    1186                 :       9846 :         ctx->group = group;
    1187                 :            : 
    1188         [ -  + ]:       9846 :         pthread_mutex_lock(&group->mutex);
    1189                 :       9846 :         group->current_unassociated_qpairs++;
    1190         [ -  + ]:       9846 :         pthread_mutex_unlock(&group->mutex);
    1191                 :            : 
    1192                 :       9846 :         spdk_thread_send_msg(group->thread, _nvmf_poll_group_add, ctx);
    1193                 :            : }
    1194                 :            : 
    1195                 :            : struct spdk_nvmf_poll_group *
    1196                 :       1381 : spdk_nvmf_poll_group_create(struct spdk_nvmf_tgt *tgt)
    1197                 :            : {
    1198                 :            :         struct spdk_io_channel *ch;
    1199                 :            : 
    1200                 :       1381 :         ch = spdk_get_io_channel(tgt);
    1201         [ -  + ]:       1381 :         if (!ch) {
    1202                 :          0 :                 SPDK_ERRLOG("Unable to get I/O channel for target\n");
    1203                 :          0 :                 return NULL;
    1204                 :            :         }
    1205                 :            : 
    1206                 :       1381 :         return spdk_io_channel_get_ctx(ch);
    1207                 :            : }
    1208                 :            : 
    1209                 :            : void
    1210                 :       1381 : spdk_nvmf_poll_group_destroy(struct spdk_nvmf_poll_group *group,
    1211                 :            :                              spdk_nvmf_poll_group_destroy_done_fn cb_fn,
    1212                 :            :                              void *cb_arg)
    1213                 :            : {
    1214         [ -  + ]:       1381 :         assert(group->destroy_cb_fn == NULL);
    1215                 :       1381 :         group->destroy_cb_fn = cb_fn;
    1216                 :       1381 :         group->destroy_cb_arg = cb_arg;
    1217                 :            : 
    1218                 :            :         /* This function will put the io_channel associated with this poll group */
    1219                 :       1381 :         nvmf_tgt_destroy_poll_group_qpairs(group);
    1220                 :       1381 : }
    1221                 :            : 
    1222                 :            : int
    1223                 :       9846 : spdk_nvmf_poll_group_add(struct spdk_nvmf_poll_group *group,
    1224                 :            :                          struct spdk_nvmf_qpair *qpair)
    1225                 :            : {
    1226                 :            :         int rc;
    1227                 :            :         struct spdk_nvmf_transport_poll_group *tgroup;
    1228                 :            : 
    1229                 :       9846 :         TAILQ_INIT(&qpair->outstanding);
    1230                 :       9846 :         qpair->group = group;
    1231                 :       9846 :         qpair->ctrlr = NULL;
    1232                 :       9846 :         qpair->disconnect_started = false;
    1233                 :            : 
    1234                 :       9846 :         tgroup = nvmf_get_transport_poll_group(group, qpair->transport);
    1235         [ -  + ]:       9846 :         if (tgroup == NULL) {
    1236                 :          0 :                 return -1;
    1237                 :            :         }
    1238                 :            : 
    1239                 :       9846 :         rc = nvmf_transport_poll_group_add(tgroup, qpair);
    1240                 :            : 
    1241                 :            :         /* We add the qpair to the group only it is successfully added into the tgroup */
    1242         [ +  - ]:       9846 :         if (rc == 0) {
    1243                 :       3535 :                 SPDK_DTRACE_PROBE2_TICKS(nvmf_poll_group_add_qpair, qpair, spdk_thread_get_id(group->thread));
    1244                 :       9846 :                 TAILQ_INSERT_TAIL(&group->qpairs, qpair, link);
    1245                 :       9846 :                 nvmf_qpair_set_state(qpair, SPDK_NVMF_QPAIR_CONNECTING);
    1246                 :            :         }
    1247                 :            : 
    1248                 :       9846 :         return rc;
    1249                 :            : }
    1250                 :            : 
    1251                 :            : static void
    1252                 :       2095 : _nvmf_ctrlr_destruct(void *ctx)
    1253                 :            : {
    1254                 :       2095 :         struct spdk_nvmf_ctrlr *ctrlr = ctx;
    1255                 :            : 
    1256                 :       2095 :         nvmf_ctrlr_destruct(ctrlr);
    1257                 :       2095 : }
    1258                 :            : 
    1259                 :            : static void
    1260                 :       9822 : _nvmf_ctrlr_free_from_qpair(void *ctx)
    1261                 :            : {
    1262                 :       9822 :         struct nvmf_qpair_disconnect_ctx *qpair_ctx = ctx;
    1263                 :       9822 :         struct spdk_nvmf_ctrlr *ctrlr = qpair_ctx->ctrlr;
    1264                 :            :         uint32_t count;
    1265                 :            : 
    1266                 :       9822 :         spdk_bit_array_clear(ctrlr->qpair_mask, qpair_ctx->qid);
    1267   [ -  +  -  + ]:       9822 :         SPDK_DEBUGLOG(nvmf, "qpair_mask cleared, qid %u\n", qpair_ctx->qid);
    1268                 :       9822 :         count = spdk_bit_array_count_set(ctrlr->qpair_mask);
    1269         [ +  + ]:       9822 :         if (count == 0) {
    1270   [ -  +  -  + ]:       2095 :                 assert(!ctrlr->in_destruct);
    1271   [ -  +  -  + ]:       2095 :                 SPDK_DEBUGLOG(nvmf, "Last qpair %u, destroy ctrlr 0x%hx\n", qpair_ctx->qid, ctrlr->cntlid);
    1272                 :       2095 :                 ctrlr->in_destruct = true;
    1273                 :       2095 :                 spdk_thread_send_msg(ctrlr->subsys->thread, _nvmf_ctrlr_destruct, ctrlr);
    1274                 :            :         }
    1275                 :       9822 :         free(qpair_ctx);
    1276                 :       9822 : }
    1277                 :            : 
    1278                 :            : static void
    1279                 :       9846 : _nvmf_transport_qpair_fini_complete(void *cb_ctx)
    1280                 :            : {
    1281                 :       9846 :         struct nvmf_qpair_disconnect_ctx *qpair_ctx = cb_ctx;
    1282                 :            :         struct spdk_nvmf_ctrlr *ctrlr;
    1283                 :            : 
    1284                 :       9846 :         ctrlr = qpair_ctx->ctrlr;
    1285   [ -  +  -  + ]:       9846 :         SPDK_DEBUGLOG(nvmf, "Finish destroying qid %u\n", qpair_ctx->qid);
    1286                 :            : 
    1287         [ +  + ]:       9846 :         if (ctrlr) {
    1288         [ +  + ]:       9822 :                 if (qpair_ctx->qid == 0) {
    1289                 :            :                         /* Admin qpair is removed, so set the pointer to NULL.
    1290                 :            :                          * This operation is safe since we are on ctrlr thread now, admin qpair's thread is the same
    1291                 :            :                          * as controller's thread */
    1292         [ -  + ]:       2095 :                         assert(ctrlr->thread == spdk_get_thread());
    1293                 :       2095 :                         ctrlr->admin_qpair = NULL;
    1294                 :            :                 }
    1295                 :            :                 /* Free qpair id from controller's bit mask and destroy the controller if it is the last qpair */
    1296         [ +  - ]:       9822 :                 if (ctrlr->thread) {
    1297                 :       9822 :                         spdk_thread_send_msg(ctrlr->thread, _nvmf_ctrlr_free_from_qpair, qpair_ctx);
    1298                 :            :                 } else {
    1299                 :          0 :                         _nvmf_ctrlr_free_from_qpair(qpair_ctx);
    1300                 :            :                 }
    1301                 :            :         } else {
    1302                 :         24 :                 free(qpair_ctx);
    1303                 :            :         }
    1304                 :       9846 : }
    1305                 :            : 
    1306                 :            : void
    1307                 :       9846 : spdk_nvmf_poll_group_remove(struct spdk_nvmf_qpair *qpair)
    1308                 :            : {
    1309                 :            :         struct spdk_nvmf_transport_poll_group *tgroup;
    1310                 :            :         int rc;
    1311                 :            : 
    1312                 :       3535 :         SPDK_DTRACE_PROBE2_TICKS(nvmf_poll_group_remove_qpair, qpair,
    1313                 :            :                                  spdk_thread_get_id(qpair->group->thread));
    1314                 :       9846 :         nvmf_qpair_set_state(qpair, SPDK_NVMF_QPAIR_ERROR);
    1315                 :            : 
    1316                 :            :         /* Find the tgroup and remove the qpair from the tgroup */
    1317                 :       9846 :         tgroup = nvmf_get_transport_poll_group(qpair->group, qpair->transport);
    1318         [ +  - ]:       9846 :         if (tgroup != NULL) {
    1319                 :       9846 :                 rc = nvmf_transport_poll_group_remove(tgroup, qpair);
    1320   [ -  +  -  - ]:       9846 :                 if (rc && (rc != ENOTSUP)) {
    1321                 :          0 :                         SPDK_ERRLOG("Cannot remove qpair=%p from transport group=%p\n",
    1322                 :            :                                     qpair, tgroup);
    1323                 :            :                 }
    1324                 :            :         }
    1325                 :            : 
    1326         [ +  + ]:       9846 :         TAILQ_REMOVE(&qpair->group->qpairs, qpair, link);
    1327                 :       9846 :         qpair->group = NULL;
    1328                 :       9846 : }
    1329                 :            : 
    1330                 :            : static void
    1331                 :      34398 : _nvmf_qpair_sgroup_req_clean(struct spdk_nvmf_subsystem_poll_group *sgroup,
    1332                 :            :                              const struct spdk_nvmf_qpair *qpair)
    1333                 :            : {
    1334                 :            :         struct spdk_nvmf_request *req, *tmp;
    1335         [ -  + ]:      34398 :         TAILQ_FOREACH_SAFE(req, &sgroup->queued, link, tmp) {
    1336         [ #  # ]:          0 :                 if (req->qpair == qpair) {
    1337         [ #  # ]:          0 :                         TAILQ_REMOVE(&sgroup->queued, req, link);
    1338         [ #  # ]:          0 :                         if (nvmf_transport_req_free(req)) {
    1339                 :          0 :                                 SPDK_ERRLOG("Transport request free error!\n");
    1340                 :            :                         }
    1341                 :            :                 }
    1342                 :            :         }
    1343                 :      34398 : }
    1344                 :            : 
    1345                 :            : static void
    1346                 :       9846 : _nvmf_qpair_destroy(void *ctx, int status)
    1347                 :            : {
    1348                 :       9846 :         struct nvmf_qpair_disconnect_ctx *qpair_ctx = ctx;
    1349                 :       9846 :         struct spdk_nvmf_qpair *qpair = qpair_ctx->qpair;
    1350                 :       9846 :         struct spdk_nvmf_ctrlr *ctrlr = qpair->ctrlr;
    1351                 :            :         struct spdk_nvmf_subsystem_poll_group *sgroup;
    1352                 :            :         uint32_t sid;
    1353                 :            : 
    1354         [ -  + ]:       9846 :         assert(qpair->state == SPDK_NVMF_QPAIR_DEACTIVATING);
    1355                 :       9846 :         qpair_ctx->qid = qpair->qid;
    1356                 :            : 
    1357   [ -  +  +  + ]:       9846 :         if (qpair->connect_received) {
    1358         [ +  + ]:       9822 :                 if (0 == qpair->qid) {
    1359         [ -  + ]:       2095 :                         assert(qpair->group->stat.current_admin_qpairs > 0);
    1360                 :       2095 :                         qpair->group->stat.current_admin_qpairs--;
    1361                 :            :                 } else {
    1362         [ -  + ]:       7727 :                         assert(qpair->group->stat.current_io_qpairs > 0);
    1363                 :       7727 :                         qpair->group->stat.current_io_qpairs--;
    1364                 :            :                 }
    1365                 :            :         } else {
    1366         [ -  + ]:         24 :                 pthread_mutex_lock(&qpair->group->mutex);
    1367                 :         24 :                 qpair->group->current_unassociated_qpairs--;
    1368         [ -  + ]:         24 :                 pthread_mutex_unlock(&qpair->group->mutex);
    1369                 :            :         }
    1370                 :            : 
    1371         [ +  + ]:       9846 :         if (ctrlr) {
    1372                 :       9822 :                 sgroup = &qpair->group->sgroups[ctrlr->subsys->id];
    1373                 :       9822 :                 _nvmf_qpair_sgroup_req_clean(sgroup, qpair);
    1374                 :            :         } else {
    1375         [ +  + ]:      24600 :                 for (sid = 0; sid < qpair->group->num_sgroups; sid++) {
    1376                 :      24576 :                         sgroup = &qpair->group->sgroups[sid];
    1377         [ -  + ]:      24576 :                         assert(sgroup != NULL);
    1378                 :      24576 :                         _nvmf_qpair_sgroup_req_clean(sgroup, qpair);
    1379                 :            :                 }
    1380                 :            :         }
    1381                 :            : 
    1382                 :       9846 :         nvmf_qpair_auth_destroy(qpair);
    1383                 :       9846 :         qpair_ctx->ctrlr = ctrlr;
    1384                 :       9846 :         spdk_nvmf_poll_group_remove(qpair);
    1385                 :       9846 :         nvmf_transport_qpair_fini(qpair, _nvmf_transport_qpair_fini_complete, qpair_ctx);
    1386                 :       9846 : }
    1387                 :            : 
    1388                 :            : static void
    1389                 :       2469 : _nvmf_qpair_disconnect_msg(void *ctx)
    1390                 :            : {
    1391                 :       2469 :         struct nvmf_qpair_disconnect_ctx *qpair_ctx = ctx;
    1392                 :            : 
    1393                 :       2469 :         spdk_nvmf_qpair_disconnect(qpair_ctx->qpair);
    1394                 :       2469 :         free(ctx);
    1395                 :       2469 : }
    1396                 :            : 
    1397                 :            : int
    1398                 :    1010436 : spdk_nvmf_qpair_disconnect(struct spdk_nvmf_qpair *qpair)
    1399                 :            : {
    1400                 :    1010436 :         struct spdk_nvmf_poll_group *group = qpair->group;
    1401                 :            :         struct nvmf_qpair_disconnect_ctx *qpair_ctx;
    1402                 :            : 
    1403         [ +  + ]:    1010436 :         if (__atomic_test_and_set(&qpair->disconnect_started, __ATOMIC_RELAXED)) {
    1404                 :     998121 :                 return -EINPROGRESS;
    1405                 :            :         }
    1406                 :            : 
    1407                 :            :         /* If we get a qpair in the uninitialized state, we can just destroy it immediately */
    1408         [ -  + ]:      12315 :         if (qpair->state == SPDK_NVMF_QPAIR_UNINITIALIZED) {
    1409                 :          0 :                 nvmf_transport_qpair_fini(qpair, NULL, NULL);
    1410                 :          0 :                 return 0;
    1411                 :            :         }
    1412                 :            : 
    1413         [ -  + ]:      12315 :         assert(group != NULL);
    1414         [ +  + ]:      12315 :         if (spdk_get_thread() != group->thread) {
    1415                 :            :                 /* clear the atomic so we can set it on the next call on the proper thread. */
    1416                 :       2469 :                 __atomic_clear(&qpair->disconnect_started, __ATOMIC_RELAXED);
    1417                 :       2469 :                 qpair_ctx = calloc(1, sizeof(struct nvmf_qpair_disconnect_ctx));
    1418         [ -  + ]:       2469 :                 if (!qpair_ctx) {
    1419                 :          0 :                         SPDK_ERRLOG("Unable to allocate context for nvmf_qpair_disconnect\n");
    1420                 :          0 :                         return -ENOMEM;
    1421                 :            :                 }
    1422                 :       2469 :                 qpair_ctx->qpair = qpair;
    1423                 :       2469 :                 spdk_thread_send_msg(group->thread, _nvmf_qpair_disconnect_msg, qpair_ctx);
    1424                 :       2469 :                 return 0;
    1425                 :            :         }
    1426                 :            : 
    1427                 :       3535 :         SPDK_DTRACE_PROBE2_TICKS(nvmf_qpair_disconnect, qpair, spdk_thread_get_id(group->thread));
    1428         [ -  + ]:       9846 :         assert(spdk_nvmf_qpair_is_active(qpair));
    1429                 :       9846 :         nvmf_qpair_set_state(qpair, SPDK_NVMF_QPAIR_DEACTIVATING);
    1430                 :            : 
    1431                 :       9846 :         qpair_ctx = calloc(1, sizeof(struct nvmf_qpair_disconnect_ctx));
    1432         [ -  + ]:       9846 :         if (!qpair_ctx) {
    1433                 :          0 :                 SPDK_ERRLOG("Unable to allocate context for nvmf_qpair_disconnect\n");
    1434                 :          0 :                 return -ENOMEM;
    1435                 :            :         }
    1436                 :            : 
    1437                 :       9846 :         qpair_ctx->qpair = qpair;
    1438                 :            : 
    1439                 :            :         /* Check for outstanding I/O */
    1440         [ +  + ]:       9846 :         if (!TAILQ_EMPTY(&qpair->outstanding)) {
    1441                 :        919 :                 SPDK_DTRACE_PROBE2_TICKS(nvmf_poll_group_drain_qpair, qpair, spdk_thread_get_id(group->thread));
    1442                 :       2078 :                 qpair->state_cb = _nvmf_qpair_destroy;
    1443                 :       2078 :                 qpair->state_cb_arg = qpair_ctx;
    1444                 :       2078 :                 nvmf_qpair_abort_pending_zcopy_reqs(qpair);
    1445                 :       2078 :                 nvmf_qpair_free_aer(qpair);
    1446                 :       2078 :                 return 0;
    1447                 :            :         }
    1448                 :            : 
    1449                 :       7768 :         _nvmf_qpair_destroy(qpair_ctx, 0);
    1450                 :            : 
    1451                 :       7768 :         return 0;
    1452                 :            : }
    1453                 :            : 
    1454                 :            : int
    1455                 :        298 : spdk_nvmf_qpair_get_peer_trid(struct spdk_nvmf_qpair *qpair,
    1456                 :            :                               struct spdk_nvme_transport_id *trid)
    1457                 :            : {
    1458         [ -  + ]:        298 :         memset(trid, 0, sizeof(*trid));
    1459                 :        298 :         return nvmf_transport_qpair_get_peer_trid(qpair, trid);
    1460                 :            : }
    1461                 :            : 
    1462                 :            : int
    1463                 :          0 : spdk_nvmf_qpair_get_local_trid(struct spdk_nvmf_qpair *qpair,
    1464                 :            :                                struct spdk_nvme_transport_id *trid)
    1465                 :            : {
    1466         [ #  # ]:          0 :         memset(trid, 0, sizeof(*trid));
    1467                 :          0 :         return nvmf_transport_qpair_get_local_trid(qpair, trid);
    1468                 :            : }
    1469                 :            : 
    1470                 :            : int
    1471                 :      12567 : spdk_nvmf_qpair_get_listen_trid(struct spdk_nvmf_qpair *qpair,
    1472                 :            :                                 struct spdk_nvme_transport_id *trid)
    1473                 :            : {
    1474         [ -  + ]:      12567 :         memset(trid, 0, sizeof(*trid));
    1475                 :      12567 :         return nvmf_transport_qpair_get_listen_trid(qpair, trid);
    1476                 :            : }
    1477                 :            : 
    1478                 :            : static int
    1479                 :      35336 : poll_group_update_subsystem(struct spdk_nvmf_poll_group *group,
    1480                 :            :                             struct spdk_nvmf_subsystem *subsystem)
    1481                 :            : {
    1482                 :            :         struct spdk_nvmf_subsystem_poll_group *sgroup;
    1483                 :            :         uint32_t i, j;
    1484                 :            :         struct spdk_nvmf_ns *ns;
    1485                 :            :         struct spdk_nvmf_registrant *reg, *tmp;
    1486                 :            :         struct spdk_io_channel *ch;
    1487                 :            :         struct spdk_nvmf_subsystem_pg_ns_info *ns_info;
    1488                 :            :         struct spdk_nvmf_ctrlr *ctrlr;
    1489                 :            :         bool ns_changed;
    1490                 :            : 
    1491                 :            :         /* Make sure our poll group has memory for this subsystem allocated */
    1492         [ -  + ]:      35336 :         if (subsystem->id >= group->num_sgroups) {
    1493                 :          0 :                 return -ENOMEM;
    1494                 :            :         }
    1495                 :            : 
    1496                 :      35336 :         sgroup = &group->sgroups[subsystem->id];
    1497                 :            : 
    1498                 :            :         /* Make sure the array of namespace information is the correct size */
    1499   [ +  +  +  + ]:      35336 :         if (sgroup->num_ns == 0 && subsystem->max_nsid > 0) {
    1500                 :            :                 /* First allocation */
    1501                 :       1274 :                 sgroup->ns_info = calloc(subsystem->max_nsid, sizeof(struct spdk_nvmf_subsystem_pg_ns_info));
    1502         [ -  + ]:       1274 :                 if (!sgroup->ns_info) {
    1503                 :          0 :                         return -ENOMEM;
    1504                 :            :                 }
    1505                 :       1274 :                 sgroup->num_ns = subsystem->max_nsid;
    1506                 :            :         }
    1507                 :            : 
    1508                 :      35336 :         ns_changed = false;
    1509                 :            : 
    1510                 :            :         /* Detect bdevs that were added or removed */
    1511         [ +  + ]:     725279 :         for (i = 0; i < sgroup->num_ns; i++) {
    1512                 :     689943 :                 ns = subsystem->ns[i];
    1513                 :     689943 :                 ns_info = &sgroup->ns_info[i];
    1514                 :     689943 :                 ch = ns_info->channel;
    1515                 :            : 
    1516   [ +  +  +  + ]:     689943 :                 if (ns == NULL && ch == NULL) {
    1517                 :            :                         /* Both NULL. Leave empty */
    1518   [ +  +  +  - ]:      36864 :                 } else if (ns == NULL && ch != NULL) {
    1519                 :            :                         /* There was a channel here, but the namespace is gone. */
    1520                 :       6977 :                         ns_changed = true;
    1521                 :       6977 :                         spdk_put_io_channel(ch);
    1522                 :       6977 :                         ns_info->channel = NULL;
    1523   [ +  -  +  + ]:      29887 :                 } else if (ns != NULL && ch == NULL) {
    1524                 :            :                         /* A namespace appeared but there is no channel yet */
    1525                 :       8065 :                         ns_changed = true;
    1526                 :       8065 :                         ch = spdk_bdev_get_io_channel(ns->desc);
    1527         [ -  + ]:       8065 :                         if (ch == NULL) {
    1528                 :          0 :                                 SPDK_ERRLOG("Could not allocate I/O channel.\n");
    1529                 :          0 :                                 return -ENOMEM;
    1530                 :            :                         }
    1531                 :       8065 :                         ns_info->channel = ch;
    1532         [ -  + ]:      21822 :                 } else if (spdk_uuid_compare(&ns_info->uuid, spdk_bdev_get_uuid(ns->bdev)) != 0) {
    1533                 :            :                         /* A namespace was here before, but was replaced by a new one. */
    1534                 :          0 :                         ns_changed = true;
    1535                 :          0 :                         spdk_put_io_channel(ns_info->channel);
    1536         [ #  # ]:          0 :                         memset(ns_info, 0, sizeof(*ns_info));
    1537                 :            : 
    1538                 :          0 :                         ch = spdk_bdev_get_io_channel(ns->desc);
    1539         [ #  # ]:          0 :                         if (ch == NULL) {
    1540                 :          0 :                                 SPDK_ERRLOG("Could not allocate I/O channel.\n");
    1541                 :          0 :                                 return -ENOMEM;
    1542                 :            :                         }
    1543                 :          0 :                         ns_info->channel = ch;
    1544         [ +  + ]:      21822 :                 } else if (ns_info->num_blocks != spdk_bdev_get_num_blocks(ns->bdev)) {
    1545                 :            :                         /* Namespace is still there but size has changed */
    1546   [ -  +  -  + ]:        276 :                         SPDK_DEBUGLOG(nvmf, "Namespace resized: subsystem_id %u,"
    1547                 :            :                                       " nsid %u, pg %p, old %" PRIu64 ", new %" PRIu64 "\n",
    1548                 :            :                                       subsystem->id,
    1549                 :            :                                       ns->nsid,
    1550                 :            :                                       group,
    1551                 :            :                                       ns_info->num_blocks,
    1552                 :            :                                       spdk_bdev_get_num_blocks(ns->bdev));
    1553                 :        276 :                         ns_changed = true;
    1554                 :            :                 }
    1555                 :            : 
    1556         [ +  + ]:     689943 :                 if (ns == NULL) {
    1557         [ -  + ]:     660056 :                         memset(ns_info, 0, sizeof(*ns_info));
    1558                 :            :                 } else {
    1559                 :      29887 :                         ns_info->uuid = *spdk_bdev_get_uuid(ns->bdev);
    1560                 :      29887 :                         ns_info->num_blocks = spdk_bdev_get_num_blocks(ns->bdev);
    1561                 :      29887 :                         ns_info->crkey = ns->crkey;
    1562                 :      29887 :                         ns_info->rtype = ns->rtype;
    1563         [ -  + ]:      29887 :                         if (ns->holder) {
    1564                 :          0 :                                 ns_info->holder_id = ns->holder->hostid;
    1565                 :            :                         }
    1566                 :            : 
    1567         [ -  + ]:      29887 :                         memset(&ns_info->reg_hostid, 0, SPDK_NVMF_MAX_NUM_REGISTRANTS * sizeof(struct spdk_uuid));
    1568                 :      29887 :                         j = 0;
    1569         [ -  + ]:      29887 :                         TAILQ_FOREACH_SAFE(reg, &ns->registrants, link, tmp) {
    1570         [ #  # ]:          0 :                                 if (j >= SPDK_NVMF_MAX_NUM_REGISTRANTS) {
    1571                 :          0 :                                         SPDK_ERRLOG("Maximum %u registrants can support.\n", SPDK_NVMF_MAX_NUM_REGISTRANTS);
    1572                 :          0 :                                         return -EINVAL;
    1573                 :            :                                 }
    1574                 :          0 :                                 ns_info->reg_hostid[j++] = reg->hostid;
    1575                 :            :                         }
    1576                 :            :                 }
    1577                 :            :         }
    1578                 :            : 
    1579         [ +  + ]:      35336 :         if (ns_changed) {
    1580         [ +  + ]:      26562 :                 TAILQ_FOREACH(ctrlr, &subsystem->ctrlrs, link) {
    1581         [ +  + ]:      11256 :                         if (ctrlr->thread != spdk_get_thread()) {
    1582                 :       7498 :                                 continue;
    1583                 :            :                         }
    1584                 :            :                         /* It is possible that a ctrlr was added but the admin_qpair hasn't been
    1585                 :            :                          * assigned yet.
    1586                 :            :                          */
    1587         [ +  + ]:       3758 :                         if (!ctrlr->admin_qpair) {
    1588                 :         12 :                                 continue;
    1589                 :            :                         }
    1590         [ +  - ]:       3746 :                         if (ctrlr->admin_qpair->group == group) {
    1591                 :       3746 :                                 nvmf_ctrlr_async_event_ns_notice(ctrlr);
    1592                 :       3746 :                                 nvmf_ctrlr_async_event_ana_change_notice(ctrlr);
    1593                 :            :                         }
    1594                 :            :                 }
    1595                 :            :         }
    1596                 :            : 
    1597                 :      35336 :         return 0;
    1598                 :            : }
    1599                 :            : 
    1600                 :            : int
    1601                 :      11450 : nvmf_poll_group_update_subsystem(struct spdk_nvmf_poll_group *group,
    1602                 :            :                                  struct spdk_nvmf_subsystem *subsystem)
    1603                 :            : {
    1604                 :      11450 :         return poll_group_update_subsystem(group, subsystem);
    1605                 :            : }
    1606                 :            : 
    1607                 :            : int
    1608                 :       4036 : nvmf_poll_group_add_subsystem(struct spdk_nvmf_poll_group *group,
    1609                 :            :                               struct spdk_nvmf_subsystem *subsystem,
    1610                 :            :                               spdk_nvmf_poll_group_mod_done cb_fn, void *cb_arg)
    1611                 :            : {
    1612                 :       4036 :         int rc = 0;
    1613                 :       4036 :         struct spdk_nvmf_subsystem_poll_group *sgroup = &group->sgroups[subsystem->id];
    1614                 :            :         struct spdk_nvmf_request *req, *tmp;
    1615                 :            :         uint32_t i;
    1616                 :            : 
    1617         [ -  + ]:       4036 :         if (!TAILQ_EMPTY(&sgroup->queued)) {
    1618                 :          0 :                 SPDK_ERRLOG("sgroup->queued not empty when adding subsystem\n");
    1619         [ #  # ]:          0 :                 TAILQ_FOREACH_SAFE(req, &sgroup->queued, link, tmp) {
    1620         [ #  # ]:          0 :                         TAILQ_REMOVE(&sgroup->queued, req, link);
    1621         [ #  # ]:          0 :                         if (nvmf_transport_req_free(req)) {
    1622                 :          0 :                                 SPDK_ERRLOG("Transport request free error!\n");
    1623                 :            :                         }
    1624                 :            :                 }
    1625                 :          0 :                 assert(false);
    1626                 :            :         }
    1627                 :            : 
    1628                 :       4036 :         rc = poll_group_update_subsystem(group, subsystem);
    1629         [ -  + ]:       4036 :         if (rc) {
    1630                 :          0 :                 nvmf_poll_group_remove_subsystem(group, subsystem, NULL, NULL);
    1631                 :          0 :                 goto fini;
    1632                 :            :         }
    1633                 :            : 
    1634                 :       4036 :         sgroup->state = SPDK_NVMF_SUBSYSTEM_ACTIVE;
    1635                 :            : 
    1636         [ +  + ]:      41837 :         for (i = 0; i < sgroup->num_ns; i++) {
    1637                 :      37801 :                 sgroup->ns_info[i].state = SPDK_NVMF_SUBSYSTEM_ACTIVE;
    1638                 :            :         }
    1639                 :            : 
    1640                 :       4036 : fini:
    1641         [ +  + ]:       4036 :         if (cb_fn) {
    1642                 :       2650 :                 cb_fn(cb_arg, rc);
    1643                 :            :         }
    1644                 :            : 
    1645                 :       1038 :         SPDK_DTRACE_PROBE2_TICKS(nvmf_poll_group_add_subsystem, spdk_thread_get_id(group->thread),
    1646                 :            :                                  subsystem->subnqn);
    1647                 :            : 
    1648                 :       4036 :         return rc;
    1649                 :            : }
    1650                 :            : 
    1651                 :            : static void
    1652                 :       2650 : _nvmf_poll_group_remove_subsystem_cb(void *ctx, int status)
    1653                 :            : {
    1654                 :       2650 :         struct nvmf_qpair_disconnect_many_ctx *qpair_ctx = ctx;
    1655                 :            :         struct spdk_nvmf_subsystem *subsystem;
    1656                 :            :         struct spdk_nvmf_poll_group *group;
    1657                 :            :         struct spdk_nvmf_subsystem_poll_group *sgroup;
    1658                 :       2650 :         spdk_nvmf_poll_group_mod_done cpl_fn = NULL;
    1659                 :       2650 :         void *cpl_ctx = NULL;
    1660                 :            :         uint32_t nsid;
    1661                 :            : 
    1662                 :       2650 :         group = qpair_ctx->group;
    1663                 :       2650 :         subsystem = qpair_ctx->subsystem;
    1664                 :       2650 :         cpl_fn = qpair_ctx->cpl_fn;
    1665                 :       2650 :         cpl_ctx = qpair_ctx->cpl_ctx;
    1666                 :       2650 :         sgroup = &group->sgroups[subsystem->id];
    1667                 :            : 
    1668         [ -  + ]:       2650 :         if (status) {
    1669                 :          0 :                 goto fini;
    1670                 :            :         }
    1671                 :            : 
    1672         [ +  + ]:      40446 :         for (nsid = 0; nsid < sgroup->num_ns; nsid++) {
    1673         [ +  + ]:      37796 :                 if (sgroup->ns_info[nsid].channel) {
    1674                 :       1083 :                         spdk_put_io_channel(sgroup->ns_info[nsid].channel);
    1675                 :       1083 :                         sgroup->ns_info[nsid].channel = NULL;
    1676                 :            :                 }
    1677                 :            :         }
    1678                 :            : 
    1679                 :       2650 :         sgroup->num_ns = 0;
    1680                 :       2650 :         free(sgroup->ns_info);
    1681                 :       2650 :         sgroup->ns_info = NULL;
    1682                 :       2650 : fini:
    1683                 :       2650 :         free(qpair_ctx);
    1684         [ +  - ]:       2650 :         if (cpl_fn) {
    1685                 :       2650 :                 cpl_fn(cpl_ctx, status);
    1686                 :            :         }
    1687                 :       2650 : }
    1688                 :            : 
    1689                 :            : static void nvmf_poll_group_remove_subsystem_msg(void *ctx);
    1690                 :            : 
    1691                 :            : static void
    1692                 :     252466 : nvmf_poll_group_remove_subsystem_msg(void *ctx)
    1693                 :            : {
    1694                 :            :         struct spdk_nvmf_qpair *qpair, *qpair_tmp;
    1695                 :            :         struct spdk_nvmf_subsystem *subsystem;
    1696                 :            :         struct spdk_nvmf_poll_group *group;
    1697                 :     252466 :         struct nvmf_qpair_disconnect_many_ctx *qpair_ctx = ctx;
    1698                 :     252466 :         bool qpairs_found = false;
    1699                 :     252466 :         int rc = 0;
    1700                 :            : 
    1701                 :     252466 :         group = qpair_ctx->group;
    1702                 :     252466 :         subsystem = qpair_ctx->subsystem;
    1703                 :            : 
    1704         [ +  + ]:    1256013 :         TAILQ_FOREACH_SAFE(qpair, &group->qpairs, link, qpair_tmp) {
    1705   [ +  -  +  + ]:    1003547 :                 if ((qpair->ctrlr != NULL) && (qpair->ctrlr->subsys == subsystem)) {
    1706                 :     998206 :                         qpairs_found = true;
    1707                 :     998206 :                         rc = spdk_nvmf_qpair_disconnect(qpair);
    1708   [ +  +  -  + ]:     998206 :                         if (rc && rc != -EINPROGRESS) {
    1709                 :          0 :                                 break;
    1710                 :            :                         }
    1711                 :            :                 }
    1712                 :            :         }
    1713                 :            : 
    1714         [ +  + ]:     252466 :         if (!qpairs_found) {
    1715                 :       2650 :                 _nvmf_poll_group_remove_subsystem_cb(ctx, 0);
    1716                 :       2650 :                 return;
    1717                 :            :         }
    1718                 :            : 
    1719                 :            :         /* Some qpairs are in process of being disconnected. Send a message and try to remove them again */
    1720                 :     249816 :         spdk_thread_send_msg(spdk_get_thread(), nvmf_poll_group_remove_subsystem_msg, ctx);
    1721                 :            : }
    1722                 :            : 
    1723                 :            : void
    1724                 :       2650 : nvmf_poll_group_remove_subsystem(struct spdk_nvmf_poll_group *group,
    1725                 :            :                                  struct spdk_nvmf_subsystem *subsystem,
    1726                 :            :                                  spdk_nvmf_poll_group_mod_done cb_fn, void *cb_arg)
    1727                 :            : {
    1728                 :            :         struct spdk_nvmf_subsystem_poll_group *sgroup;
    1729                 :            :         struct nvmf_qpair_disconnect_many_ctx *ctx;
    1730                 :            :         uint32_t i;
    1731                 :            : 
    1732                 :        732 :         SPDK_DTRACE_PROBE3_TICKS(nvmf_poll_group_remove_subsystem, group, spdk_thread_get_id(group->thread),
    1733                 :            :                                  subsystem->subnqn);
    1734                 :            : 
    1735                 :       2650 :         ctx = calloc(1, sizeof(struct nvmf_qpair_disconnect_many_ctx));
    1736         [ -  + ]:       2650 :         if (!ctx) {
    1737                 :          0 :                 SPDK_ERRLOG("Unable to allocate memory for context to remove poll subsystem\n");
    1738         [ #  # ]:          0 :                 if (cb_fn) {
    1739                 :          0 :                         cb_fn(cb_arg, -1);
    1740                 :            :                 }
    1741                 :          0 :                 return;
    1742                 :            :         }
    1743                 :            : 
    1744                 :       2650 :         ctx->group = group;
    1745                 :       2650 :         ctx->subsystem = subsystem;
    1746                 :       2650 :         ctx->cpl_fn = cb_fn;
    1747                 :       2650 :         ctx->cpl_ctx = cb_arg;
    1748                 :            : 
    1749                 :       2650 :         sgroup = &group->sgroups[subsystem->id];
    1750                 :       2650 :         sgroup->state = SPDK_NVMF_SUBSYSTEM_INACTIVE;
    1751                 :            : 
    1752         [ +  + ]:      40446 :         for (i = 0; i < sgroup->num_ns; i++) {
    1753                 :      37796 :                 sgroup->ns_info[i].state = SPDK_NVMF_SUBSYSTEM_INACTIVE;
    1754                 :            :         }
    1755                 :            : 
    1756                 :       2650 :         nvmf_poll_group_remove_subsystem_msg(ctx);
    1757                 :            : }
    1758                 :            : 
    1759                 :            : void
    1760                 :      19850 : nvmf_poll_group_pause_subsystem(struct spdk_nvmf_poll_group *group,
    1761                 :            :                                 struct spdk_nvmf_subsystem *subsystem,
    1762                 :            :                                 uint32_t nsid,
    1763                 :            :                                 spdk_nvmf_poll_group_mod_done cb_fn, void *cb_arg)
    1764                 :            : {
    1765                 :            :         struct spdk_nvmf_subsystem_poll_group *sgroup;
    1766                 :      19850 :         struct spdk_nvmf_subsystem_pg_ns_info *ns_info = NULL;
    1767                 :      19850 :         int rc = 0;
    1768                 :            :         uint32_t i;
    1769                 :            : 
    1770         [ -  + ]:      19850 :         if (subsystem->id >= group->num_sgroups) {
    1771                 :          0 :                 rc = -1;
    1772                 :          0 :                 goto fini;
    1773                 :            :         }
    1774                 :            : 
    1775                 :      19850 :         sgroup = &group->sgroups[subsystem->id];
    1776         [ -  + ]:      19850 :         if (sgroup->state == SPDK_NVMF_SUBSYSTEM_PAUSED) {
    1777                 :          0 :                 goto fini;
    1778                 :            :         }
    1779                 :      19850 :         sgroup->state = SPDK_NVMF_SUBSYSTEM_PAUSING;
    1780                 :            : 
    1781         [ -  + ]:      19850 :         if (nsid == SPDK_NVME_GLOBAL_NS_TAG) {
    1782         [ #  # ]:          0 :                 for (i = 0; i < sgroup->num_ns; i++) {
    1783                 :          0 :                         ns_info = &sgroup->ns_info[i];
    1784                 :          0 :                         ns_info->state = SPDK_NVMF_SUBSYSTEM_PAUSING;
    1785                 :            :                 }
    1786                 :            :         } else {
    1787                 :            :                 /* NOTE: This implicitly also checks for 0, since 0 - 1 wraps around to UINT32_MAX. */
    1788         [ +  + ]:      19850 :                 if (nsid - 1 < sgroup->num_ns) {
    1789                 :       9148 :                         ns_info  = &sgroup->ns_info[nsid - 1];
    1790                 :       9148 :                         ns_info->state = SPDK_NVMF_SUBSYSTEM_PAUSING;
    1791                 :            :                 }
    1792                 :            :         }
    1793                 :            : 
    1794         [ +  + ]:      19850 :         if (sgroup->mgmt_io_outstanding > 0) {
    1795         [ -  + ]:          1 :                 assert(sgroup->cb_fn == NULL);
    1796                 :          1 :                 sgroup->cb_fn = cb_fn;
    1797         [ -  + ]:          1 :                 assert(sgroup->cb_arg == NULL);
    1798                 :          1 :                 sgroup->cb_arg = cb_arg;
    1799                 :          1 :                 return;
    1800                 :            :         }
    1801                 :            : 
    1802         [ -  + ]:      19849 :         if (nsid == SPDK_NVME_GLOBAL_NS_TAG) {
    1803         [ #  # ]:          0 :                 for (i = 0; i < sgroup->num_ns; i++) {
    1804                 :          0 :                         ns_info = &sgroup->ns_info[i];
    1805                 :            : 
    1806         [ #  # ]:          0 :                         if (ns_info->io_outstanding > 0) {
    1807         [ #  # ]:          0 :                                 assert(sgroup->cb_fn == NULL);
    1808                 :          0 :                                 sgroup->cb_fn = cb_fn;
    1809         [ #  # ]:          0 :                                 assert(sgroup->cb_arg == NULL);
    1810                 :          0 :                                 sgroup->cb_arg = cb_arg;
    1811                 :          0 :                                 return;
    1812                 :            :                         }
    1813                 :            :                 }
    1814                 :            :         } else {
    1815   [ +  +  +  + ]:      19849 :                 if (ns_info != NULL && ns_info->io_outstanding > 0) {
    1816         [ -  + ]:       1125 :                         assert(sgroup->cb_fn == NULL);
    1817                 :       1125 :                         sgroup->cb_fn = cb_fn;
    1818         [ -  + ]:       1125 :                         assert(sgroup->cb_arg == NULL);
    1819                 :       1125 :                         sgroup->cb_arg = cb_arg;
    1820                 :       1125 :                         return;
    1821                 :            :                 }
    1822                 :            :         }
    1823                 :            : 
    1824         [ -  + ]:      18724 :         assert(sgroup->mgmt_io_outstanding == 0);
    1825                 :      18724 :         sgroup->state = SPDK_NVMF_SUBSYSTEM_PAUSED;
    1826                 :      18724 : fini:
    1827         [ +  - ]:      18724 :         if (cb_fn) {
    1828                 :      18724 :                 cb_fn(cb_arg, rc);
    1829                 :            :         }
    1830                 :            : }
    1831                 :            : 
    1832                 :            : void
    1833                 :      19850 : nvmf_poll_group_resume_subsystem(struct spdk_nvmf_poll_group *group,
    1834                 :            :                                  struct spdk_nvmf_subsystem *subsystem,
    1835                 :            :                                  spdk_nvmf_poll_group_mod_done cb_fn, void *cb_arg)
    1836                 :            : {
    1837                 :            :         struct spdk_nvmf_request *req, *tmp;
    1838                 :            :         struct spdk_nvmf_subsystem_poll_group *sgroup;
    1839                 :      19850 :         int rc = 0;
    1840                 :            :         uint32_t i;
    1841                 :            : 
    1842         [ -  + ]:      19850 :         if (subsystem->id >= group->num_sgroups) {
    1843                 :          0 :                 rc = -1;
    1844                 :          0 :                 goto fini;
    1845                 :            :         }
    1846                 :            : 
    1847                 :      19850 :         sgroup = &group->sgroups[subsystem->id];
    1848                 :            : 
    1849         [ -  + ]:      19850 :         if (sgroup->state == SPDK_NVMF_SUBSYSTEM_ACTIVE) {
    1850                 :          0 :                 goto fini;
    1851                 :            :         }
    1852                 :            : 
    1853                 :      19850 :         rc = poll_group_update_subsystem(group, subsystem);
    1854         [ -  + ]:      19850 :         if (rc) {
    1855                 :          0 :                 goto fini;
    1856                 :            :         }
    1857                 :            : 
    1858         [ +  + ]:     305592 :         for (i = 0; i < sgroup->num_ns; i++) {
    1859                 :     285742 :                 sgroup->ns_info[i].state = SPDK_NVMF_SUBSYSTEM_ACTIVE;
    1860                 :            :         }
    1861                 :            : 
    1862                 :      19850 :         sgroup->state = SPDK_NVMF_SUBSYSTEM_ACTIVE;
    1863                 :            : 
    1864                 :            :         /* Release all queued requests */
    1865         [ +  + ]:     124211 :         TAILQ_FOREACH_SAFE(req, &sgroup->queued, link, tmp) {
    1866         [ +  + ]:     104361 :                 TAILQ_REMOVE(&sgroup->queued, req, link);
    1867         [ +  + ]:     104361 :                 if (spdk_nvmf_request_using_zcopy(req)) {
    1868                 :      99097 :                         spdk_nvmf_request_zcopy_start(req);
    1869                 :            :                 } else {
    1870                 :       5264 :                         spdk_nvmf_request_exec(req);
    1871                 :            :                 }
    1872                 :            : 
    1873                 :            :         }
    1874                 :      19850 : fini:
    1875         [ +  - ]:      19850 :         if (cb_fn) {
    1876                 :      19850 :                 cb_fn(cb_arg, rc);
    1877                 :            :         }
    1878                 :      19850 : }
    1879                 :            : 
    1880                 :            : 
    1881                 :            : struct spdk_nvmf_poll_group *
    1882                 :       9846 : spdk_nvmf_get_optimal_poll_group(struct spdk_nvmf_qpair *qpair)
    1883                 :            : {
    1884                 :            :         struct spdk_nvmf_transport_poll_group *tgroup;
    1885                 :            : 
    1886                 :       9846 :         tgroup = nvmf_transport_get_optimal_poll_group(qpair->transport, qpair);
    1887                 :            : 
    1888         [ -  + ]:       9846 :         if (tgroup == NULL) {
    1889                 :          0 :                 return NULL;
    1890                 :            :         }
    1891                 :            : 
    1892                 :       9846 :         return tgroup->group;
    1893                 :            : }
    1894                 :            : 
    1895                 :            : void
    1896                 :         44 : spdk_nvmf_poll_group_dump_stat(struct spdk_nvmf_poll_group *group, struct spdk_json_write_ctx *w)
    1897                 :            : {
    1898                 :            :         struct spdk_nvmf_transport_poll_group *tgroup;
    1899                 :            : 
    1900                 :         44 :         spdk_json_write_object_begin(w);
    1901                 :            : 
    1902                 :         44 :         spdk_json_write_named_string(w, "name", spdk_thread_get_name(spdk_get_thread()));
    1903                 :         44 :         spdk_json_write_named_uint32(w, "admin_qpairs", group->stat.admin_qpairs);
    1904                 :         44 :         spdk_json_write_named_uint32(w, "io_qpairs", group->stat.io_qpairs);
    1905                 :         44 :         spdk_json_write_named_uint32(w, "current_admin_qpairs", group->stat.current_admin_qpairs);
    1906                 :         44 :         spdk_json_write_named_uint32(w, "current_io_qpairs", group->stat.current_io_qpairs);
    1907                 :         44 :         spdk_json_write_named_uint64(w, "pending_bdev_io", group->stat.pending_bdev_io);
    1908                 :         44 :         spdk_json_write_named_uint64(w, "completed_nvme_io", group->stat.completed_nvme_io);
    1909                 :            : 
    1910                 :         44 :         spdk_json_write_named_array_begin(w, "transports");
    1911                 :            : 
    1912         [ +  + ]:         76 :         TAILQ_FOREACH(tgroup, &group->tgroups, link) {
    1913                 :         32 :                 spdk_json_write_object_begin(w);
    1914                 :            :                 /*
    1915                 :            :                  * The trtype field intentionally contains a transport name as this is more informative.
    1916                 :            :                  * The field has not been renamed for backward compatibility.
    1917                 :            :                  */
    1918                 :         32 :                 spdk_json_write_named_string(w, "trtype", spdk_nvmf_get_transport_name(tgroup->transport));
    1919                 :            : 
    1920         [ +  + ]:         32 :                 if (tgroup->transport->ops->poll_group_dump_stat) {
    1921                 :          8 :                         tgroup->transport->ops->poll_group_dump_stat(tgroup, w);
    1922                 :            :                 }
    1923                 :            : 
    1924                 :         32 :                 spdk_json_write_object_end(w);
    1925                 :            :         }
    1926                 :            : 
    1927                 :         44 :         spdk_json_write_array_end(w);
    1928                 :         44 :         spdk_json_write_object_end(w);
    1929                 :         44 : }

Generated by: LCOV version 1.14