LCOV - code coverage report
Current view: top level - spdk/lib/iscsi - conn.c (source / functions) Hit Total Coverage
Test: Combined Lines: 596 781 76.3 %
Date: 2024-07-12 13:51:55 Functions: 56 66 84.8 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 335 585 57.3 %

           Branch data     Line data    Source code
       1                 :            : /*   SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  *   Copyright (C) 2008-2012 Daisuke Aoyama <aoyama@peach.ne.jp>.
       3                 :            :  *   Copyright (C) 2016 Intel Corporation.
       4                 :            :  *   All rights reserved.
       5                 :            :  */
       6                 :            : 
       7                 :            : #include "spdk/stdinc.h"
       8                 :            : 
       9                 :            : #include "spdk/endian.h"
      10                 :            : #include "spdk/env.h"
      11                 :            : #include "spdk/likely.h"
      12                 :            : #include "spdk/thread.h"
      13                 :            : #include "spdk/queue.h"
      14                 :            : #include "spdk/trace.h"
      15                 :            : #include "spdk/sock.h"
      16                 :            : #include "spdk/string.h"
      17                 :            : 
      18                 :            : #include "spdk/log.h"
      19                 :            : 
      20                 :            : #include "iscsi/task.h"
      21                 :            : #include "iscsi/conn.h"
      22                 :            : #include "iscsi/tgt_node.h"
      23                 :            : #include "iscsi/portal_grp.h"
      24                 :            : 
      25                 :            : #define MAKE_DIGEST_WORD(BUF, CRC32C) \
      26                 :            :         (   ((*((uint8_t *)(BUF)+0)) = (uint8_t)((uint32_t)(CRC32C) >> 0)), \
      27                 :            :             ((*((uint8_t *)(BUF)+1)) = (uint8_t)((uint32_t)(CRC32C) >> 8)), \
      28                 :            :             ((*((uint8_t *)(BUF)+2)) = (uint8_t)((uint32_t)(CRC32C) >> 16)), \
      29                 :            :             ((*((uint8_t *)(BUF)+3)) = (uint8_t)((uint32_t)(CRC32C) >> 24)))
      30                 :            : 
      31                 :            : #define SPDK_ISCSI_CONNECTION_MEMSET(conn)              \
      32                 :            :         memset(&(conn)->portal, 0, sizeof(*(conn)) -     \
      33                 :            :                 offsetof(struct spdk_iscsi_conn, portal));
      34                 :            : 
      35                 :            : #define SPDK_ISCSI_CONNECTION_STATUS(status, rnstr) case(status): return(rnstr)
      36                 :            : 
      37                 :            : static struct spdk_iscsi_conn *g_conns_array = NULL;
      38                 :            : 
      39                 :            : static TAILQ_HEAD(, spdk_iscsi_conn) g_free_conns = TAILQ_HEAD_INITIALIZER(g_free_conns);
      40                 :            : static TAILQ_HEAD(, spdk_iscsi_conn) g_active_conns = TAILQ_HEAD_INITIALIZER(g_active_conns);
      41                 :            : 
      42                 :            : static pthread_mutex_t g_conns_mutex = PTHREAD_MUTEX_INITIALIZER;
      43                 :            : 
      44                 :            : static struct spdk_poller *g_shutdown_timer = NULL;
      45                 :            : 
      46                 :            : static void iscsi_conn_sock_cb(void *arg, struct spdk_sock_group *group,
      47                 :            :                                struct spdk_sock *sock);
      48                 :            : 
      49                 :            : static struct spdk_iscsi_conn *
      50                 :        551 : allocate_conn(void)
      51                 :            : {
      52                 :            :         struct spdk_iscsi_conn  *conn;
      53                 :            : 
      54         [ -  + ]:        551 :         pthread_mutex_lock(&g_conns_mutex);
      55                 :        551 :         conn = TAILQ_FIRST(&g_free_conns);
      56         [ +  - ]:        551 :         if (conn != NULL) {
      57         [ -  + ]:        551 :                 assert(!conn->is_valid);
      58         [ +  - ]:        551 :                 TAILQ_REMOVE(&g_free_conns, conn, conn_link);
      59         [ -  + ]:        551 :                 SPDK_ISCSI_CONNECTION_MEMSET(conn);
      60                 :        551 :                 conn->is_valid = 1;
      61                 :            : 
      62                 :        551 :                 TAILQ_INSERT_TAIL(&g_active_conns, conn, conn_link);
      63                 :            :         }
      64         [ -  + ]:        551 :         pthread_mutex_unlock(&g_conns_mutex);
      65                 :            : 
      66                 :        551 :         return conn;
      67                 :            : }
      68                 :            : 
      69                 :            : static void
      70                 :        551 : _free_conn(struct spdk_iscsi_conn *conn)
      71                 :            : {
      72         [ +  + ]:        551 :         TAILQ_REMOVE(&g_active_conns, conn, conn_link);
      73                 :            : 
      74         [ -  + ]:        551 :         memset(conn->portal_host, 0, sizeof(conn->portal_host));
      75         [ -  + ]:        551 :         memset(conn->portal_port, 0, sizeof(conn->portal_port));
      76                 :        551 :         conn->is_valid = 0;
      77                 :            : 
      78                 :        551 :         TAILQ_INSERT_TAIL(&g_free_conns, conn, conn_link);
      79                 :        551 : }
      80                 :            : 
      81                 :            : static void
      82                 :          0 : free_conn(struct spdk_iscsi_conn *conn)
      83                 :            : {
      84         [ #  # ]:          0 :         pthread_mutex_lock(&g_conns_mutex);
      85                 :          0 :         _free_conn(conn);
      86         [ #  # ]:          0 :         pthread_mutex_unlock(&g_conns_mutex);
      87                 :          0 : }
      88                 :            : 
      89                 :            : static void
      90                 :        627 : _iscsi_conns_cleanup(void)
      91                 :            : {
      92                 :        627 :         free(g_conns_array);
      93                 :        627 : }
      94                 :            : 
      95                 :            : int
      96                 :        627 : initialize_iscsi_conns(void)
      97                 :            : {
      98                 :            :         uint32_t i;
      99                 :            : 
     100   [ -  +  -  + ]:        627 :         SPDK_DEBUGLOG(iscsi, "spdk_iscsi_init\n");
     101                 :            : 
     102                 :        627 :         g_conns_array = calloc(MAX_ISCSI_CONNECTIONS, sizeof(struct spdk_iscsi_conn));
     103         [ -  + ]:        627 :         if (g_conns_array == NULL) {
     104                 :          0 :                 return -ENOMEM;
     105                 :            :         }
     106                 :            : 
     107         [ +  + ]:     642675 :         for (i = 0; i < MAX_ISCSI_CONNECTIONS; i++) {
     108                 :     642048 :                 g_conns_array[i].id = i;
     109                 :     642048 :                 TAILQ_INSERT_TAIL(&g_free_conns, &g_conns_array[i], conn_link);
     110                 :            :         }
     111                 :            : 
     112                 :        627 :         return 0;
     113                 :            : }
     114                 :            : 
     115                 :            : static void
     116                 :       1011 : iscsi_poll_group_add_conn(struct spdk_iscsi_poll_group *pg, struct spdk_iscsi_conn *conn)
     117                 :            : {
     118                 :            :         int rc;
     119                 :            : 
     120                 :       1011 :         rc = spdk_sock_group_add_sock(pg->sock_group, conn->sock, iscsi_conn_sock_cb, conn);
     121         [ -  + ]:       1011 :         if (rc < 0) {
     122                 :          0 :                 SPDK_ERRLOG("Failed to add sock=%p of conn=%p\n", conn->sock, conn);
     123                 :          0 :                 return;
     124                 :            :         }
     125                 :            : 
     126                 :       1011 :         conn->is_stopped = false;
     127                 :       1011 :         STAILQ_INSERT_TAIL(&pg->connections, conn, pg_link);
     128                 :            : }
     129                 :            : 
     130                 :            : static void
     131                 :       1011 : iscsi_poll_group_remove_conn(struct spdk_iscsi_poll_group *pg, struct spdk_iscsi_conn *conn)
     132                 :            : {
     133                 :            :         int rc;
     134                 :            : 
     135         [ -  + ]:       1011 :         assert(conn->sock != NULL);
     136                 :       1011 :         rc = spdk_sock_group_remove_sock(pg->sock_group, conn->sock);
     137         [ -  + ]:       1011 :         if (rc < 0) {
     138                 :          0 :                 SPDK_ERRLOG("Failed to remove sock=%p of conn=%p\n", conn->sock, conn);
     139                 :            :         }
     140                 :            : 
     141                 :       1011 :         conn->is_stopped = true;
     142   [ +  +  +  +  :       2011 :         STAILQ_REMOVE(&pg->connections, conn, spdk_iscsi_conn, pg_link);
             +  +  +  + ]
     143                 :       1011 : }
     144                 :            : 
     145                 :            : static int
     146                 :          0 : login_timeout(void *arg)
     147                 :            : {
     148                 :          0 :         struct spdk_iscsi_conn *conn = arg;
     149                 :            : 
     150         [ #  # ]:          0 :         if (conn->state < ISCSI_CONN_STATE_EXITING) {
     151                 :          0 :                 conn->state = ISCSI_CONN_STATE_EXITING;
     152                 :            :         }
     153                 :          0 :         spdk_poller_unregister(&conn->login_timer);
     154                 :            : 
     155                 :          0 :         return SPDK_POLLER_BUSY;
     156                 :            : }
     157                 :            : 
     158                 :            : static void
     159                 :        551 : iscsi_conn_start(void *ctx)
     160                 :            : {
     161                 :        551 :         struct spdk_iscsi_conn *conn = ctx;
     162                 :            : 
     163                 :        551 :         iscsi_poll_group_add_conn(conn->pg, conn);
     164                 :            : 
     165                 :        551 :         conn->login_timer = SPDK_POLLER_REGISTER(login_timeout, conn, ISCSI_LOGIN_TIMEOUT * 1000000);
     166                 :        551 : }
     167                 :            : 
     168                 :            : int
     169                 :        551 : iscsi_conn_construct(struct spdk_iscsi_portal *portal,
     170                 :            :                      struct spdk_sock *sock)
     171                 :            : {
     172                 :            :         struct spdk_iscsi_poll_group *pg;
     173                 :            :         struct spdk_iscsi_conn *conn;
     174                 :            :         int i, rc;
     175                 :            : 
     176                 :        551 :         conn = allocate_conn();
     177         [ -  + ]:        551 :         if (conn == NULL) {
     178                 :          0 :                 SPDK_ERRLOG("Could not allocate connection.\n");
     179                 :          0 :                 return -1;
     180                 :            :         }
     181                 :            : 
     182         [ -  + ]:        551 :         pthread_mutex_lock(&g_iscsi.mutex);
     183                 :        551 :         conn->timeout = g_iscsi.timeout * spdk_get_ticks_hz(); /* seconds to TSC */
     184                 :        551 :         conn->nopininterval = g_iscsi.nopininterval;
     185                 :        551 :         conn->nopininterval *= spdk_get_ticks_hz(); /* seconds to TSC */
     186                 :        551 :         conn->last_nopin = spdk_get_ticks();
     187                 :        551 :         conn->nop_outstanding = false;
     188                 :        551 :         conn->data_out_cnt = 0;
     189                 :        551 :         conn->data_in_cnt = 0;
     190         [ -  + ]:        551 :         conn->disable_chap = portal->group->disable_chap;
     191         [ -  + ]:        551 :         conn->require_chap = portal->group->require_chap;
     192         [ -  + ]:        551 :         conn->mutual_chap = portal->group->mutual_chap;
     193                 :        551 :         conn->chap_group = portal->group->chap_group;
     194         [ -  + ]:        551 :         pthread_mutex_unlock(&g_iscsi.mutex);
     195                 :        551 :         conn->MaxRecvDataSegmentLength = 8192; /* RFC3720(12.12) */
     196                 :            : 
     197                 :        551 :         conn->portal = portal;
     198                 :        551 :         conn->pg_tag = portal->group->tag;
     199   [ -  +  -  +  :        551 :         memcpy(conn->portal_host, portal->host, strlen(portal->host));
                   -  + ]
     200   [ -  +  -  +  :        551 :         memcpy(conn->portal_port, portal->port, strlen(portal->port));
                   -  + ]
     201                 :        551 :         conn->sock = sock;
     202                 :            : 
     203                 :        551 :         conn->state = ISCSI_CONN_STATE_INVALID;
     204                 :        551 :         conn->login_phase = ISCSI_SECURITY_NEGOTIATION_PHASE;
     205                 :        551 :         conn->ttt = 0;
     206                 :            : 
     207                 :        551 :         conn->partial_text_parameter = NULL;
     208                 :            : 
     209         [ +  + ]:       8265 :         for (i = 0; i < MAX_CONNECTION_PARAMS; i++) {
     210                 :       7714 :                 conn->conn_param_state_negotiated[i] = false;
     211                 :            :         }
     212                 :            : 
     213         [ +  + ]:      11020 :         for (i = 0; i < MAX_SESSION_PARAMS; i++) {
     214                 :      10469 :                 conn->sess_param_state_negotiated[i] = false;
     215                 :            :         }
     216                 :            : 
     217                 :        551 :         conn->pdu_recv_state = ISCSI_PDU_RECV_STATE_AWAIT_PDU_READY;
     218                 :            : 
     219                 :        551 :         TAILQ_INIT(&conn->write_pdu_list);
     220                 :        551 :         TAILQ_INIT(&conn->snack_pdu_list);
     221                 :        551 :         TAILQ_INIT(&conn->queued_r2t_tasks);
     222                 :        551 :         TAILQ_INIT(&conn->active_r2t_tasks);
     223                 :        551 :         TAILQ_INIT(&conn->queued_datain_tasks);
     224                 :        551 :         TAILQ_INIT(&conn->luns);
     225                 :            : 
     226                 :        551 :         rc = spdk_sock_getaddr(sock, conn->target_addr, sizeof conn->target_addr, NULL,
     227                 :        551 :                                conn->initiator_addr, sizeof conn->initiator_addr, NULL);
     228         [ -  + ]:        551 :         if (rc < 0) {
     229                 :          0 :                 SPDK_ERRLOG("spdk_sock_getaddr() failed\n");
     230                 :          0 :                 goto error_return;
     231                 :            :         }
     232                 :            : 
     233                 :            :         /* set low water mark */
     234                 :        551 :         rc = spdk_sock_set_recvlowat(conn->sock, 1);
     235         [ -  + ]:        551 :         if (rc != 0) {
     236                 :          0 :                 SPDK_ERRLOG("spdk_sock_set_recvlowat() failed\n");
     237                 :          0 :                 goto error_return;
     238                 :            :         }
     239                 :            : 
     240                 :            :         /* set default params */
     241                 :        551 :         rc = iscsi_conn_params_init(&conn->params);
     242         [ -  + ]:        551 :         if (rc < 0) {
     243                 :          0 :                 SPDK_ERRLOG("iscsi_conn_params_init() failed\n");
     244                 :          0 :                 goto error_return;
     245                 :            :         }
     246                 :        551 :         conn->logout_request_timer = NULL;
     247                 :        551 :         conn->logout_timer = NULL;
     248                 :        551 :         conn->shutdown_timer = NULL;
     249   [ -  +  -  + ]:        551 :         SPDK_DEBUGLOG(iscsi, "Launching connection on acceptor thread\n");
     250                 :        551 :         conn->pending_task_cnt = 0;
     251                 :            : 
     252                 :            :         /* Get the first poll group. */
     253                 :        551 :         pg = TAILQ_FIRST(&g_iscsi.poll_group_head);
     254         [ -  + ]:        551 :         if (pg == NULL) {
     255                 :          0 :                 SPDK_ERRLOG("There is no poll group.\n");
     256                 :          0 :                 assert(false);
     257                 :            :                 goto error_return;
     258                 :            :         }
     259                 :            : 
     260                 :        551 :         conn->pg = pg;
     261                 :        551 :         conn->trace_id = spdk_trace_register_owner(OWNER_TYPE_ISCSI_CONN, conn->initiator_addr);
     262                 :        551 :         spdk_thread_send_msg(spdk_io_channel_get_thread(spdk_io_channel_from_ctx(pg)),
     263                 :            :                              iscsi_conn_start, conn);
     264                 :        551 :         return 0;
     265                 :            : 
     266                 :          0 : error_return:
     267                 :          0 :         iscsi_param_free(conn->params);
     268                 :          0 :         free_conn(conn);
     269                 :          0 :         return -1;
     270                 :            : }
     271                 :            : 
     272                 :            : void
     273                 :    7145138 : iscsi_conn_free_pdu(struct spdk_iscsi_conn *conn, struct spdk_iscsi_pdu *pdu)
     274                 :            : {
     275                 :            :         iscsi_conn_xfer_complete_cb cb_fn;
     276                 :            :         void *cb_arg;
     277                 :            : 
     278                 :    7145138 :         cb_fn = pdu->cb_fn;
     279                 :    7145138 :         cb_arg = pdu->cb_arg;
     280                 :            : 
     281         [ -  + ]:    7145138 :         assert(cb_fn != NULL);
     282                 :    7145138 :         pdu->cb_fn = NULL;
     283                 :            : 
     284         [ +  + ]:    7145138 :         if (pdu->task) {
     285                 :    6561458 :                 iscsi_task_put(pdu->task);
     286                 :            :         }
     287                 :    7145138 :         iscsi_put_pdu(pdu);
     288                 :            : 
     289                 :    7145138 :         cb_fn(cb_arg);
     290                 :    7145138 : }
     291                 :            : 
     292                 :            : static int
     293                 :        567 : iscsi_conn_free_tasks(struct spdk_iscsi_conn *conn)
     294                 :            : {
     295                 :            :         struct spdk_iscsi_pdu *pdu, *tmp_pdu;
     296                 :            :         struct spdk_iscsi_task *iscsi_task, *tmp_iscsi_task;
     297                 :            : 
     298         [ +  + ]:        797 :         TAILQ_FOREACH_SAFE(pdu, &conn->snack_pdu_list, tailq, tmp_pdu) {
     299         [ +  + ]:        230 :                 TAILQ_REMOVE(&conn->snack_pdu_list, pdu, tailq);
     300                 :        230 :                 iscsi_conn_free_pdu(conn, pdu);
     301                 :            :         }
     302                 :            : 
     303         [ +  + ]:        591 :         TAILQ_FOREACH_SAFE(iscsi_task, &conn->queued_datain_tasks, link, tmp_iscsi_task) {
     304   [ +  +  +  + ]:         24 :                 if (!iscsi_task->is_queued) {
     305         [ +  + ]:         20 :                         TAILQ_REMOVE(&conn->queued_datain_tasks, iscsi_task, link);
     306                 :         20 :                         iscsi_task_put(iscsi_task);
     307                 :            :                 }
     308                 :            :         }
     309                 :            : 
     310                 :            :         /* We have to parse conn->write_pdu_list in the end.  In iscsi_conn_free_pdu(),
     311                 :            :          *  iscsi_conn_handle_queued_datain_tasks() may be called, and
     312                 :            :          *  iscsi_conn_handle_queued_datain_tasks() will parse conn->queued_datain_tasks
     313                 :            :          *  and may stack some PDUs to conn->write_pdu_list.  Hence when we come here, we
     314                 :            :          *  have to ensure there is no associated task in conn->queued_datain_tasks.
     315                 :            :          */
     316         [ +  + ]:        595 :         TAILQ_FOREACH_SAFE(pdu, &conn->write_pdu_list, tailq, tmp_pdu) {
     317         [ +  + ]:         28 :                 TAILQ_REMOVE(&conn->write_pdu_list, pdu, tailq);
     318                 :         28 :                 iscsi_conn_free_pdu(conn, pdu);
     319                 :            :         }
     320                 :            : 
     321         [ -  + ]:        567 :         if (conn->pending_task_cnt) {
     322                 :          0 :                 return -1;
     323                 :            :         }
     324                 :            : 
     325                 :        567 :         return 0;
     326                 :            : }
     327                 :            : 
     328                 :            : static void
     329                 :        148 : iscsi_conn_cleanup_backend(struct spdk_iscsi_conn *conn)
     330                 :            : {
     331                 :            :         int rc;
     332                 :            :         struct spdk_iscsi_tgt_node *target;
     333                 :            : 
     334         [ +  - ]:        148 :         if (conn->sess->connections > 1) {
     335                 :            :                 /* connection specific cleanup */
     336   [ -  +  -  + ]:        148 :         } else if (!g_iscsi.AllowDuplicateIsid) {
     337                 :            :                 /*
     338                 :            :                  * a> a target is connected by a single initiator, cleanup backend cancels inflight
     339                 :            :                  *    IOs and the resources (of this initiator) are reclaimed as soon as possible.
     340                 :            :                  * b> a target is connected by multiple initiators, one of these initiators
     341                 :            :                  *    disconnects with inflight IOs, resetting backend bdev leads all the inflight
     342                 :            :                  *    IOs (of multiple initiators) aborted. In this scenario, drain inflight IOs of
     343                 :            :                  *    the disconnected initiator instead.
     344                 :            :                  */
     345                 :          0 :                 target = conn->sess->target;
     346   [ #  #  #  # ]:          0 :                 if (target != NULL && iscsi_get_active_conns(target) == 1) {
     347                 :          0 :                         rc = iscsi_tgt_node_cleanup_luns(conn, target);
     348         [ #  # ]:          0 :                         if (rc < 0) {
     349                 :          0 :                                 SPDK_ERRLOG("target abort failed\n");
     350                 :            :                         }
     351                 :            :                 }
     352                 :            :         }
     353                 :        148 : }
     354                 :            : 
     355                 :            : static void
     356                 :        551 : iscsi_conn_free(struct spdk_iscsi_conn *conn)
     357                 :            : {
     358                 :            :         struct spdk_iscsi_sess *sess;
     359                 :            :         int idx;
     360                 :            :         uint32_t i;
     361                 :            : 
     362         [ -  + ]:        551 :         pthread_mutex_lock(&g_conns_mutex);
     363                 :            : 
     364         [ +  + ]:        551 :         if (conn->sess == NULL) {
     365                 :         10 :                 goto end;
     366                 :            :         }
     367                 :            : 
     368                 :        541 :         idx = -1;
     369                 :        541 :         sess = conn->sess;
     370                 :        541 :         conn->sess = NULL;
     371                 :            : 
     372         [ +  - ]:        541 :         for (i = 0; i < sess->connections; i++) {
     373         [ +  - ]:        541 :                 if (sess->conns[i] == conn) {
     374                 :        541 :                         idx = i;
     375                 :        541 :                         break;
     376                 :            :                 }
     377                 :            :         }
     378                 :            : 
     379         [ -  + ]:        541 :         if (idx < 0) {
     380                 :          0 :                 SPDK_ERRLOG("remove conn not found\n");
     381                 :            :         } else {
     382         [ -  + ]:        541 :                 for (i = idx; i < sess->connections - 1; i++) {
     383                 :          0 :                         sess->conns[i] = sess->conns[i + 1];
     384                 :            :                 }
     385                 :        541 :                 sess->conns[sess->connections - 1] = NULL;
     386                 :        541 :                 sess->connections--;
     387                 :            : 
     388         [ +  - ]:        541 :                 if (sess->connections == 0) {
     389                 :            :                         /* cleanup last connection */
     390   [ -  +  -  + ]:        541 :                         SPDK_DEBUGLOG(iscsi,
     391                 :            :                                       "cleanup last conn free sess\n");
     392                 :        541 :                         iscsi_free_sess(sess);
     393                 :            :                 }
     394                 :            :         }
     395                 :            : 
     396   [ -  +  +  - ]:        541 :         SPDK_DEBUGLOG(iscsi, "Terminating connections(tsih %d): %d\n",
     397                 :            :                       sess->tsih, sess->connections);
     398                 :            : 
     399                 :        541 : end:
     400   [ -  +  -  + ]:        551 :         SPDK_DEBUGLOG(iscsi, "cleanup free conn\n");
     401                 :        551 :         iscsi_param_free(conn->params);
     402                 :        551 :         _free_conn(conn);
     403                 :            : 
     404         [ -  + ]:        551 :         pthread_mutex_unlock(&g_conns_mutex);
     405                 :        551 : }
     406                 :            : 
     407                 :            : static void
     408                 :        466 : iscsi_conn_close_lun(struct spdk_iscsi_conn *conn,
     409                 :            :                      struct spdk_iscsi_lun *iscsi_lun)
     410                 :            : {
     411         [ -  + ]:        466 :         if (iscsi_lun == NULL) {
     412                 :          0 :                 return;
     413                 :            :         }
     414                 :            : 
     415                 :        466 :         spdk_scsi_lun_free_io_channel(iscsi_lun->desc);
     416                 :        466 :         spdk_scsi_lun_close(iscsi_lun->desc);
     417                 :        466 :         spdk_poller_unregister(&iscsi_lun->remove_poller);
     418                 :            : 
     419         [ +  + ]:        466 :         TAILQ_REMOVE(&conn->luns, iscsi_lun, tailq);
     420                 :            : 
     421                 :        466 :         free(iscsi_lun);
     422                 :            : 
     423                 :            : }
     424                 :            : 
     425                 :            : static void
     426                 :        460 : iscsi_conn_close_luns(struct spdk_iscsi_conn *conn)
     427                 :            : {
     428                 :            :         struct spdk_iscsi_lun *iscsi_lun, *tmp;
     429                 :            : 
     430         [ +  + ]:        922 :         TAILQ_FOREACH_SAFE(iscsi_lun, &conn->luns, tailq, tmp) {
     431                 :        462 :                 iscsi_conn_close_lun(conn, iscsi_lun);
     432                 :            :         }
     433                 :        460 : }
     434                 :            : 
     435                 :            : static bool
     436                 :        140 : iscsi_conn_check_tasks_for_lun(struct spdk_iscsi_conn *conn,
     437                 :            :                                struct spdk_scsi_lun *lun)
     438                 :            : {
     439                 :            :         struct spdk_iscsi_pdu *pdu, *tmp_pdu;
     440                 :            :         struct spdk_iscsi_task *task;
     441                 :            : 
     442         [ -  + ]:        140 :         assert(lun != NULL);
     443                 :            : 
     444                 :            :         /* We can remove deferred PDUs safely because they are already flushed. */
     445         [ -  + ]:        140 :         TAILQ_FOREACH_SAFE(pdu, &conn->snack_pdu_list, tailq, tmp_pdu) {
     446         [ #  # ]:          0 :                 if (lun == pdu->task->scsi.lun) {
     447         [ #  # ]:          0 :                         TAILQ_REMOVE(&conn->snack_pdu_list, pdu, tailq);
     448                 :          0 :                         iscsi_conn_free_pdu(conn, pdu);
     449                 :            :                 }
     450                 :            :         }
     451                 :            : 
     452         [ +  + ]:        419 :         TAILQ_FOREACH(task, &conn->queued_datain_tasks, link) {
     453         [ +  + ]:        402 :                 if (lun == task->scsi.lun) {
     454                 :        123 :                         return false;
     455                 :            :                 }
     456                 :            :         }
     457                 :            : 
     458                 :            :         /* This check loop works even when connection exits in the middle of LUN hotplug
     459                 :            :          *  because all PDUs in write_pdu_list are removed in iscsi_conn_free_tasks().
     460                 :            :          */
     461         [ +  + ]:        113 :         TAILQ_FOREACH(pdu, &conn->write_pdu_list, tailq) {
     462   [ +  +  +  + ]:        109 :                 if (pdu->task && lun == pdu->task->scsi.lun) {
     463                 :         13 :                         return false;
     464                 :            :                 }
     465                 :            :         }
     466                 :            : 
     467                 :          4 :         return true;
     468                 :            : }
     469                 :            : 
     470                 :            : static int
     471                 :        140 : iscsi_conn_remove_lun(void *ctx)
     472                 :            : {
     473                 :        140 :         struct spdk_iscsi_lun *iscsi_lun = ctx;
     474                 :        140 :         struct spdk_iscsi_conn *conn = iscsi_lun->conn;
     475                 :        140 :         struct spdk_scsi_lun *lun = iscsi_lun->lun;
     476                 :            : 
     477         [ +  + ]:        140 :         if (!iscsi_conn_check_tasks_for_lun(conn, lun)) {
     478                 :        136 :                 return SPDK_POLLER_BUSY;
     479                 :            :         }
     480                 :          4 :         iscsi_conn_close_lun(conn, iscsi_lun);
     481                 :          4 :         return SPDK_POLLER_BUSY;
     482                 :            : }
     483                 :            : 
     484                 :            : static void
     485                 :          4 : _iscsi_conn_hotremove_lun(void *ctx)
     486                 :            : {
     487                 :          4 :         struct spdk_iscsi_lun *iscsi_lun = ctx;
     488                 :          4 :         struct spdk_iscsi_conn *conn = iscsi_lun->conn;
     489                 :          4 :         struct spdk_scsi_lun *lun = iscsi_lun->lun;
     490                 :            : 
     491         [ -  + ]:          4 :         assert(spdk_io_channel_get_thread(spdk_io_channel_from_ctx(conn->pg)) ==
     492                 :            :                spdk_get_thread());
     493                 :            : 
     494                 :            :         /* If a connection is already in stating status, just return */
     495         [ -  + ]:          4 :         if (conn->state >= ISCSI_CONN_STATE_EXITING) {
     496                 :          0 :                 return;
     497                 :            :         }
     498                 :            : 
     499                 :          4 :         iscsi_clear_all_transfer_task(conn, lun, NULL);
     500                 :            : 
     501                 :          4 :         iscsi_lun->remove_poller = SPDK_POLLER_REGISTER(iscsi_conn_remove_lun, iscsi_lun,
     502                 :            :                                    1000);
     503                 :            : }
     504                 :            : 
     505                 :            : static void
     506                 :          4 : iscsi_conn_hotremove_lun(struct spdk_scsi_lun *lun, void *remove_ctx)
     507                 :            : {
     508                 :          4 :         struct spdk_iscsi_lun *iscsi_lun = remove_ctx;
     509                 :          4 :         struct spdk_iscsi_conn *conn = iscsi_lun->conn;
     510                 :            : 
     511                 :          4 :         spdk_thread_send_msg(spdk_io_channel_get_thread(spdk_io_channel_from_ctx(conn->pg)),
     512                 :            :                              _iscsi_conn_hotremove_lun, iscsi_lun);
     513                 :          4 : }
     514                 :            : 
     515                 :            : static int
     516                 :        466 : iscsi_conn_open_lun(struct spdk_iscsi_conn *conn, struct spdk_scsi_lun *lun)
     517                 :            : {
     518                 :            :         int rc;
     519                 :            :         struct spdk_iscsi_lun *iscsi_lun;
     520                 :            : 
     521                 :        466 :         iscsi_lun = calloc(1, sizeof(*iscsi_lun));
     522         [ -  + ]:        466 :         if (iscsi_lun == NULL) {
     523                 :          0 :                 return -ENOMEM;
     524                 :            :         }
     525                 :            : 
     526                 :        466 :         iscsi_lun->conn = conn;
     527                 :        466 :         iscsi_lun->lun = lun;
     528                 :            : 
     529                 :        466 :         rc = spdk_scsi_lun_open(lun, iscsi_conn_hotremove_lun, iscsi_lun, &iscsi_lun->desc);
     530         [ -  + ]:        466 :         if (rc != 0) {
     531                 :          0 :                 free(iscsi_lun);
     532                 :          0 :                 return rc;
     533                 :            :         }
     534                 :            : 
     535                 :        466 :         rc = spdk_scsi_lun_allocate_io_channel(iscsi_lun->desc);
     536         [ -  + ]:        466 :         if (rc != 0) {
     537                 :          0 :                 spdk_scsi_lun_close(iscsi_lun->desc);
     538                 :          0 :                 free(iscsi_lun);
     539                 :          0 :                 return rc;
     540                 :            :         }
     541                 :            : 
     542                 :        466 :         TAILQ_INSERT_TAIL(&conn->luns, iscsi_lun, tailq);
     543                 :            : 
     544                 :        466 :         return 0;
     545                 :            : }
     546                 :            : 
     547                 :            : static int
     548                 :        460 : iscsi_conn_open_luns(struct spdk_iscsi_conn *conn)
     549                 :            : {
     550                 :            :         int rc;
     551                 :            :         struct spdk_scsi_lun *lun;
     552                 :            : 
     553         [ +  + ]:        926 :         for (lun = spdk_scsi_dev_get_first_lun(conn->dev); lun != NULL;
     554                 :        466 :              lun = spdk_scsi_dev_get_next_lun(lun)) {
     555                 :        466 :                 rc = iscsi_conn_open_lun(conn, lun);
     556         [ -  + ]:        466 :                 if (rc != 0) {
     557                 :          0 :                         goto error;
     558                 :            :                 }
     559                 :            :         }
     560                 :            : 
     561                 :        460 :         return 0;
     562                 :            : 
     563                 :          0 : error:
     564                 :          0 :         iscsi_conn_close_luns(conn);
     565                 :          0 :         return -1;
     566                 :            : }
     567                 :            : 
     568                 :            : /**
     569                 :            :  *  This function will stop executing the specified connection.
     570                 :            :  */
     571                 :            : static void
     572                 :        551 : iscsi_conn_stop(struct spdk_iscsi_conn *conn)
     573                 :            : {
     574                 :            :         struct spdk_iscsi_tgt_node *target;
     575                 :            : 
     576         [ -  + ]:        551 :         assert(conn->state == ISCSI_CONN_STATE_EXITED);
     577         [ -  + ]:        551 :         assert(conn->data_in_cnt == 0);
     578         [ -  + ]:        551 :         assert(conn->data_out_cnt == 0);
     579                 :            : 
     580         [ +  + ]:        551 :         if (conn->sess != NULL &&
     581         [ +  + ]:        541 :             conn->sess->session_type == SESSION_TYPE_NORMAL &&
     582         [ +  + ]:        506 :             conn->full_feature) {
     583                 :        460 :                 target = conn->sess->target;
     584         [ -  + ]:        460 :                 pthread_mutex_lock(&target->mutex);
     585                 :        460 :                 target->num_active_conns--;
     586         [ -  + ]:        460 :                 pthread_mutex_unlock(&target->mutex);
     587                 :            : 
     588                 :        460 :                 iscsi_conn_close_luns(conn);
     589                 :            :         }
     590                 :            : 
     591         [ -  + ]:        551 :         assert(spdk_io_channel_get_thread(spdk_io_channel_from_ctx(conn->pg)) ==
     592                 :            :                spdk_get_thread());
     593                 :        551 : }
     594                 :            : 
     595                 :            : static int
     596                 :          0 : _iscsi_conn_check_shutdown(void *arg)
     597                 :            : {
     598                 :          0 :         struct spdk_iscsi_conn *conn = arg;
     599                 :            :         int rc;
     600                 :            : 
     601                 :          0 :         rc = iscsi_conn_free_tasks(conn);
     602         [ #  # ]:          0 :         if (rc < 0) {
     603                 :          0 :                 return SPDK_POLLER_BUSY;
     604                 :            :         }
     605                 :            : 
     606                 :          0 :         spdk_poller_unregister(&conn->shutdown_timer);
     607                 :            : 
     608                 :          0 :         iscsi_conn_stop(conn);
     609                 :          0 :         iscsi_conn_free(conn);
     610                 :            : 
     611                 :          0 :         return SPDK_POLLER_BUSY;
     612                 :            : }
     613                 :            : 
     614                 :            : static void
     615                 :        551 : _iscsi_conn_destruct(struct spdk_iscsi_conn *conn)
     616                 :            : {
     617                 :            :         int rc;
     618                 :            : 
     619                 :        551 :         iscsi_poll_group_remove_conn(conn->pg, conn);
     620                 :        551 :         spdk_sock_close(&conn->sock);
     621                 :        551 :         iscsi_clear_all_transfer_task(conn, NULL, NULL);
     622                 :        551 :         spdk_poller_unregister(&conn->logout_request_timer);
     623                 :        551 :         spdk_poller_unregister(&conn->logout_timer);
     624                 :        551 :         spdk_poller_unregister(&conn->login_timer);
     625                 :            : 
     626                 :        551 :         rc = iscsi_conn_free_tasks(conn);
     627         [ -  + ]:        551 :         if (rc < 0) {
     628                 :            :                 /* The connection cannot be freed yet. Check back later. */
     629                 :          0 :                 conn->shutdown_timer = SPDK_POLLER_REGISTER(_iscsi_conn_check_shutdown, conn, 1000);
     630                 :            :         } else {
     631                 :        551 :                 iscsi_conn_stop(conn);
     632                 :        551 :                 iscsi_conn_free(conn);
     633                 :            :         }
     634                 :        551 : }
     635                 :            : 
     636                 :            : static int
     637                 :          0 : _iscsi_conn_check_pending_tasks(void *arg)
     638                 :            : {
     639                 :          0 :         struct spdk_iscsi_conn *conn = arg;
     640                 :            : 
     641   [ #  #  #  # ]:          0 :         if (conn->dev != NULL &&
     642                 :          0 :             spdk_scsi_dev_has_pending_tasks(conn->dev, conn->initiator_port)) {
     643                 :          0 :                 return SPDK_POLLER_BUSY;
     644                 :            :         }
     645                 :            : 
     646                 :          0 :         spdk_poller_unregister(&conn->shutdown_timer);
     647                 :            : 
     648                 :          0 :         _iscsi_conn_destruct(conn);
     649                 :            : 
     650                 :          0 :         return SPDK_POLLER_BUSY;
     651                 :            : }
     652                 :            : 
     653                 :            : void
     654                 :        551 : iscsi_conn_destruct(struct spdk_iscsi_conn *conn)
     655                 :            : {
     656                 :            :         struct spdk_iscsi_pdu *pdu;
     657                 :            :         struct spdk_iscsi_task *task;
     658                 :            :         int opcode;
     659                 :            : 
     660                 :            :         /* If a connection is already in exited status, just return */
     661         [ -  + ]:        551 :         if (conn->state >= ISCSI_CONN_STATE_EXITED) {
     662                 :          0 :                 return;
     663                 :            :         }
     664                 :            : 
     665                 :        551 :         conn->state = ISCSI_CONN_STATE_EXITED;
     666                 :            : 
     667                 :            :         /*
     668                 :            :          * Each connection pre-allocates its next PDU - make sure these get
     669                 :            :          *  freed here.
     670                 :            :          */
     671                 :        551 :         pdu = conn->pdu_in_progress;
     672         [ +  - ]:        551 :         if (pdu) {
     673                 :            :                 /* remove the task left in the PDU too. */
     674                 :        551 :                 task = pdu->task;
     675         [ -  + ]:        551 :                 if (task) {
     676                 :          0 :                         opcode = pdu->bhs.opcode;
     677         [ #  # ]:          0 :                         switch (opcode) {
     678                 :          0 :                         case ISCSI_OP_SCSI:
     679                 :            :                         case ISCSI_OP_SCSI_DATAOUT:
     680                 :          0 :                                 spdk_scsi_task_process_abort(&task->scsi);
     681                 :          0 :                                 iscsi_task_cpl(&task->scsi);
     682                 :          0 :                                 break;
     683                 :          0 :                         default:
     684                 :          0 :                                 SPDK_ERRLOG("unexpected opcode %x\n", opcode);
     685                 :          0 :                                 iscsi_task_put(task);
     686                 :          0 :                                 break;
     687                 :            :                         }
     688                 :          0 :                 }
     689                 :        551 :                 iscsi_put_pdu(pdu);
     690                 :        551 :                 conn->pdu_in_progress = NULL;
     691                 :            :         }
     692                 :            : 
     693   [ +  +  +  + ]:        551 :         if (conn->sess != NULL && conn->pending_task_cnt > 0) {
     694                 :        148 :                 iscsi_conn_cleanup_backend(conn);
     695                 :            :         }
     696                 :            : 
     697   [ +  +  -  + ]:       1057 :         if (conn->dev != NULL &&
     698                 :        506 :             spdk_scsi_dev_has_pending_tasks(conn->dev, conn->initiator_port)) {
     699                 :          0 :                 conn->shutdown_timer = SPDK_POLLER_REGISTER(_iscsi_conn_check_pending_tasks, conn, 1000);
     700                 :            :         } else {
     701                 :        551 :                 _iscsi_conn_destruct(conn);
     702                 :            :         }
     703                 :            : }
     704                 :            : 
     705                 :            : int
     706                 :        769 : iscsi_get_active_conns(struct spdk_iscsi_tgt_node *target)
     707                 :            : {
     708                 :            :         struct spdk_iscsi_conn *conn;
     709                 :        769 :         int num = 0;
     710                 :            : 
     711         [ -  + ]:        769 :         if (g_conns_array == MAP_FAILED) {
     712                 :          0 :                 return 0;
     713                 :            :         }
     714                 :            : 
     715         [ -  + ]:        769 :         pthread_mutex_lock(&g_conns_mutex);
     716         [ -  + ]:        769 :         TAILQ_FOREACH(conn, &g_active_conns, conn_link) {
     717   [ #  #  #  # ]:          0 :                 if (target == NULL || conn->target == target) {
     718                 :          0 :                         num++;
     719                 :            :                 }
     720                 :            :         }
     721         [ -  + ]:        769 :         pthread_mutex_unlock(&g_conns_mutex);
     722                 :        769 :         return num;
     723                 :            : }
     724                 :            : 
     725                 :            : static void
     726                 :        627 : iscsi_conn_check_shutdown_cb(void *arg1)
     727                 :            : {
     728                 :        627 :         _iscsi_conns_cleanup();
     729                 :        627 :         shutdown_iscsi_conns_done();
     730                 :        627 : }
     731                 :            : 
     732                 :            : static int
     733                 :        627 : iscsi_conn_check_shutdown(void *arg)
     734                 :            : {
     735         [ -  + ]:        627 :         if (iscsi_get_active_conns(NULL) != 0) {
     736                 :          0 :                 return SPDK_POLLER_BUSY;
     737                 :            :         }
     738                 :            : 
     739                 :        627 :         spdk_poller_unregister(&g_shutdown_timer);
     740                 :            : 
     741                 :        627 :         spdk_thread_send_msg(spdk_get_thread(), iscsi_conn_check_shutdown_cb, NULL);
     742                 :            : 
     743                 :        627 :         return SPDK_POLLER_BUSY;
     744                 :            : }
     745                 :            : 
     746                 :            : static void
     747                 :          6 : iscsi_send_logout_request(struct spdk_iscsi_conn *conn)
     748                 :            : {
     749                 :            :         struct spdk_iscsi_pdu *rsp_pdu;
     750                 :            :         struct iscsi_bhs_async *rsph;
     751                 :            : 
     752                 :          6 :         rsp_pdu = iscsi_get_pdu(conn);
     753         [ -  + ]:          6 :         assert(rsp_pdu != NULL);
     754                 :            : 
     755                 :          6 :         rsph = (struct iscsi_bhs_async *)&rsp_pdu->bhs;
     756                 :          6 :         rsp_pdu->data = NULL;
     757                 :            : 
     758                 :          6 :         rsph->opcode = ISCSI_OP_ASYNC;
     759                 :          6 :         to_be32(&rsph->ffffffff, 0xFFFFFFFF);
     760                 :          6 :         rsph->async_event = 1;
     761                 :          6 :         to_be16(&rsph->param3, ISCSI_LOGOUT_REQUEST_TIMEOUT);
     762                 :            : 
     763                 :          6 :         to_be32(&rsph->stat_sn, conn->StatSN);
     764                 :          6 :         conn->StatSN++;
     765                 :          6 :         to_be32(&rsph->exp_cmd_sn, conn->sess->ExpCmdSN);
     766                 :          6 :         to_be32(&rsph->max_cmd_sn, conn->sess->MaxCmdSN);
     767                 :            : 
     768                 :          6 :         iscsi_conn_write_pdu(conn, rsp_pdu, iscsi_conn_pdu_generic_complete, NULL);
     769                 :          6 : }
     770                 :            : 
     771                 :            : static int
     772                 :          0 : logout_request_timeout(void *arg)
     773                 :            : {
     774                 :          0 :         struct spdk_iscsi_conn *conn = arg;
     775                 :            : 
     776         [ #  # ]:          0 :         if (conn->state < ISCSI_CONN_STATE_EXITING) {
     777                 :          0 :                 conn->state = ISCSI_CONN_STATE_EXITING;
     778                 :            :         }
     779                 :            : 
     780                 :          0 :         return SPDK_POLLER_BUSY;
     781                 :            : }
     782                 :            : 
     783                 :            : /* If the connection is running and logout is not requested yet, request logout
     784                 :            :  * to initiator and wait for the logout process to start.
     785                 :            :  */
     786                 :            : static void
     787                 :          6 : _iscsi_conn_request_logout(void *ctx)
     788                 :            : {
     789                 :          6 :         struct spdk_iscsi_conn *conn = ctx;
     790                 :            : 
     791         [ +  - ]:          6 :         if (conn->state > ISCSI_CONN_STATE_RUNNING ||
     792         [ -  + ]:          6 :             conn->logout_request_timer != NULL) {
     793                 :          0 :                 return;
     794                 :            :         }
     795                 :            : 
     796                 :          6 :         iscsi_send_logout_request(conn);
     797                 :            : 
     798                 :          6 :         conn->logout_request_timer = SPDK_POLLER_REGISTER(logout_request_timeout,
     799                 :            :                                      conn, ISCSI_LOGOUT_REQUEST_TIMEOUT * 1000000);
     800                 :            : }
     801                 :            : 
     802                 :            : static void
     803                 :          6 : iscsi_conn_request_logout(struct spdk_iscsi_conn *conn)
     804                 :            : {
     805                 :            :         struct spdk_thread *thread;
     806                 :            : 
     807         [ -  + ]:          6 :         if (conn->state == ISCSI_CONN_STATE_INVALID) {
     808                 :            :                 /* Move it to EXITING state if the connection is in login. */
     809                 :          0 :                 conn->state = ISCSI_CONN_STATE_EXITING;
     810         [ +  - ]:          6 :         } else if (conn->state == ISCSI_CONN_STATE_RUNNING &&
     811         [ +  - ]:          6 :                    conn->logout_request_timer == NULL) {
     812                 :          6 :                 thread = spdk_io_channel_get_thread(spdk_io_channel_from_ctx(conn->pg));
     813                 :          6 :                 spdk_thread_send_msg(thread, _iscsi_conn_request_logout, conn);
     814                 :            :         }
     815                 :          6 : }
     816                 :            : 
     817                 :            : void
     818                 :        773 : iscsi_conns_request_logout(struct spdk_iscsi_tgt_node *target, int pg_tag)
     819                 :            : {
     820                 :            :         struct spdk_iscsi_conn  *conn;
     821                 :            : 
     822         [ -  + ]:        773 :         if (g_conns_array == MAP_FAILED) {
     823                 :          0 :                 return;
     824                 :            :         }
     825                 :            : 
     826         [ -  + ]:        773 :         pthread_mutex_lock(&g_conns_mutex);
     827         [ +  + ]:        779 :         TAILQ_FOREACH(conn, &g_active_conns, conn_link) {
     828         [ +  + ]:          6 :                 if ((target == NULL) ||
     829   [ +  -  +  -  :          4 :                     (conn->target == target && (pg_tag < 0 || conn->pg_tag == pg_tag))) {
                   +  - ]
     830                 :          6 :                         iscsi_conn_request_logout(conn);
     831                 :            :                 }
     832                 :            :         }
     833         [ -  + ]:        773 :         pthread_mutex_unlock(&g_conns_mutex);
     834                 :            : }
     835                 :            : 
     836                 :            : void
     837                 :        627 : shutdown_iscsi_conns(void)
     838                 :            : {
     839                 :        627 :         iscsi_conns_request_logout(NULL, -1);
     840                 :            : 
     841                 :        627 :         g_shutdown_timer = SPDK_POLLER_REGISTER(iscsi_conn_check_shutdown, NULL, 1000);
     842                 :        627 : }
     843                 :            : 
     844                 :            : /* Do not set conn->state if the connection has already started exiting.
     845                 :            :  *  This ensures we do not move a connection from EXITED state back to EXITING.
     846                 :            :  */
     847                 :            : static void
     848                 :          0 : _iscsi_conn_drop(void *ctx)
     849                 :            : {
     850                 :          0 :         struct spdk_iscsi_conn *conn = ctx;
     851                 :            : 
     852         [ #  # ]:          0 :         if (conn->state < ISCSI_CONN_STATE_EXITING) {
     853                 :          0 :                 conn->state = ISCSI_CONN_STATE_EXITING;
     854                 :            :         }
     855                 :          0 : }
     856                 :            : 
     857                 :            : int
     858                 :        142 : iscsi_drop_conns(struct spdk_iscsi_conn *conn, const char *conn_match,
     859                 :            :                  int drop_all)
     860                 :            : {
     861                 :            :         struct spdk_iscsi_conn  *xconn;
     862                 :            :         const char              *xconn_match;
     863                 :            :         struct spdk_thread      *thread;
     864                 :            :         int                     num;
     865                 :            : 
     866   [ -  +  -  + ]:        142 :         SPDK_DEBUGLOG(iscsi, "iscsi_drop_conns\n");
     867                 :            : 
     868                 :        142 :         num = 0;
     869                 :        142 :         pthread_mutex_lock(&g_conns_mutex);
     870         [ -  + ]:        142 :         if (g_conns_array == MAP_FAILED) {
     871                 :          0 :                 goto exit;
     872                 :            :         }
     873                 :            : 
     874         [ +  + ]:       1396 :         TAILQ_FOREACH(xconn, &g_active_conns, conn_link) {
     875         [ +  + ]:       1254 :                 if (xconn == conn) {
     876                 :        142 :                         continue;
     877                 :            :                 }
     878                 :            : 
     879   [ +  -  -  + ]:       1112 :                 if (!drop_all && xconn->initiator_port == NULL) {
     880                 :          0 :                         continue;
     881                 :            :                 }
     882                 :            : 
     883                 :       1112 :                 xconn_match =
     884         [ -  + ]:       1112 :                         drop_all ? xconn->initiator_name : spdk_scsi_port_get_name(xconn->initiator_port);
     885                 :            : 
     886   [ -  +  -  +  :       1112 :                 if (!strcasecmp(conn_match, xconn_match) &&
                   -  + ]
     887         [ #  # ]:          0 :                     conn->target == xconn->target) {
     888                 :            : 
     889         [ #  # ]:          0 :                         if (num == 0) {
     890                 :            :                                 /*
     891                 :            :                                  * Only print this message before we report the
     892                 :            :                                  *  first dropped connection.
     893                 :            :                                  */
     894                 :          0 :                                 SPDK_ERRLOG("drop old connections %s by %s\n",
     895                 :            :                                             conn->target->name, conn_match);
     896                 :            :                         }
     897                 :            : 
     898                 :          0 :                         SPDK_ERRLOG("exiting conn by %s (%s)\n",
     899                 :            :                                     xconn_match, xconn->initiator_addr);
     900         [ #  # ]:          0 :                         if (xconn->sess != NULL) {
     901   [ #  #  #  # ]:          0 :                                 SPDK_DEBUGLOG(iscsi, "TSIH=%u\n", xconn->sess->tsih);
     902                 :            :                         } else {
     903   [ #  #  #  # ]:          0 :                                 SPDK_DEBUGLOG(iscsi, "TSIH=xx\n");
     904                 :            :                         }
     905                 :            : 
     906   [ #  #  #  # ]:          0 :                         SPDK_DEBUGLOG(iscsi, "CID=%u\n", xconn->cid);
     907                 :            : 
     908                 :          0 :                         thread = spdk_io_channel_get_thread(spdk_io_channel_from_ctx(xconn->pg));
     909                 :          0 :                         spdk_thread_send_msg(thread, _iscsi_conn_drop, xconn);
     910                 :            : 
     911                 :          0 :                         num++;
     912                 :            :                 }
     913                 :            :         }
     914                 :            : 
     915                 :        142 : exit:
     916                 :        142 :         pthread_mutex_unlock(&g_conns_mutex);
     917                 :            : 
     918         [ -  + ]:        142 :         if (num != 0) {
     919                 :          0 :                 SPDK_ERRLOG("exiting %d conns\n", num);
     920                 :            :         }
     921                 :            : 
     922                 :        142 :         return 0;
     923                 :            : }
     924                 :            : 
     925                 :            : static int
     926                 :         28 : _iscsi_conn_abort_queued_datain_task(struct spdk_iscsi_conn *conn,
     927                 :            :                                      struct spdk_iscsi_task *task)
     928                 :            : {
     929                 :            :         struct spdk_iscsi_task *subtask;
     930                 :            :         uint32_t remaining_size;
     931                 :            : 
     932         [ +  + ]:         28 :         if (conn->data_in_cnt >= g_iscsi.MaxLargeDataInPerConnection) {
     933                 :          8 :                 return -1;
     934                 :            :         }
     935                 :            : 
     936         [ -  + ]:         20 :         assert(task->current_data_offset <= task->scsi.transfer_len);
     937                 :            :         /* Stop split and abort read I/O for remaining data. */
     938         [ +  - ]:         20 :         if (task->current_data_offset < task->scsi.transfer_len) {
     939                 :         20 :                 remaining_size = task->scsi.transfer_len - task->current_data_offset;
     940                 :         20 :                 subtask = iscsi_task_get(conn, task, iscsi_task_cpl);
     941         [ -  + ]:         20 :                 assert(subtask != NULL);
     942                 :         20 :                 subtask->scsi.offset = task->current_data_offset;
     943                 :         20 :                 subtask->scsi.length = remaining_size;
     944                 :         20 :                 spdk_scsi_task_set_data(&subtask->scsi, NULL, 0);
     945                 :         20 :                 task->current_data_offset += subtask->scsi.length;
     946                 :            : 
     947                 :         20 :                 subtask->scsi.transfer_len = subtask->scsi.length;
     948                 :         20 :                 spdk_scsi_task_process_abort(&subtask->scsi);
     949                 :         20 :                 iscsi_task_cpl(&subtask->scsi);
     950                 :            :         }
     951                 :            : 
     952                 :            :         /* Remove the primary task from the list because all subtasks are submitted
     953                 :            :          *  or aborted.
     954                 :            :          */
     955         [ -  + ]:         20 :         assert(task->current_data_offset == task->scsi.transfer_len);
     956         [ +  + ]:         20 :         TAILQ_REMOVE(&conn->queued_datain_tasks, task, link);
     957                 :         20 :         return 0;
     958                 :            : }
     959                 :            : 
     960                 :            : int
     961                 :          0 : iscsi_conn_abort_queued_datain_task(struct spdk_iscsi_conn *conn,
     962                 :            :                                     uint32_t ref_task_tag)
     963                 :            : {
     964                 :            :         struct spdk_iscsi_task *task;
     965                 :            : 
     966         [ #  # ]:          0 :         TAILQ_FOREACH(task, &conn->queued_datain_tasks, link) {
     967         [ #  # ]:          0 :                 if (task->tag == ref_task_tag) {
     968                 :          0 :                         return _iscsi_conn_abort_queued_datain_task(conn, task);
     969                 :            :                 }
     970                 :            :         }
     971                 :            : 
     972                 :          0 :         return 0;
     973                 :            : }
     974                 :            : 
     975                 :            : int
     976                 :         14 : iscsi_conn_abort_queued_datain_tasks(struct spdk_iscsi_conn *conn,
     977                 :            :                                      struct spdk_scsi_lun *lun,
     978                 :            :                                      struct spdk_iscsi_pdu *pdu)
     979                 :            : {
     980                 :            :         struct spdk_iscsi_task *task, *task_tmp;
     981                 :            :         struct spdk_iscsi_pdu *pdu_tmp;
     982                 :            :         int rc;
     983                 :            : 
     984         [ +  + ]:         58 :         TAILQ_FOREACH_SAFE(task, &conn->queued_datain_tasks, link, task_tmp) {
     985                 :         44 :                 pdu_tmp = iscsi_task_get_pdu(task);
     986   [ +  -  +  +  :         44 :                 if ((lun == NULL || lun == task->scsi.lun) &&
                   +  - ]
     987         [ +  + ]:         24 :                     (pdu == NULL || (spdk_sn32_lt(pdu_tmp->cmd_sn, pdu->cmd_sn)))) {
     988                 :         12 :                         rc = _iscsi_conn_abort_queued_datain_task(conn, task);
     989         [ -  + ]:         12 :                         if (rc != 0) {
     990                 :          0 :                                 return rc;
     991                 :            :                         }
     992                 :            :                 }
     993                 :            :         }
     994                 :            : 
     995                 :         14 :         return 0;
     996                 :            : }
     997                 :            : 
     998                 :            : int
     999                 :    4430065 : iscsi_conn_handle_queued_datain_tasks(struct spdk_iscsi_conn *conn)
    1000                 :            : {
    1001                 :            :         struct spdk_iscsi_task *task;
    1002                 :            : 
    1003         [ +  + ]:    9175253 :         while (!TAILQ_EMPTY(&conn->queued_datain_tasks) &&
    1004         [ +  + ]:     369342 :                conn->data_in_cnt < g_iscsi.MaxLargeDataInPerConnection) {
    1005                 :     315153 :                 task = TAILQ_FIRST(&conn->queued_datain_tasks);
    1006         [ -  + ]:     315153 :                 assert(task->current_data_offset <= task->scsi.transfer_len);
    1007         [ +  - ]:     315153 :                 if (task->current_data_offset < task->scsi.transfer_len) {
    1008                 :            :                         struct spdk_iscsi_task *subtask;
    1009                 :     315153 :                         uint32_t remaining_size = 0;
    1010                 :            : 
    1011                 :     315153 :                         remaining_size = task->scsi.transfer_len - task->current_data_offset;
    1012                 :     315153 :                         subtask = iscsi_task_get(conn, task, iscsi_task_cpl);
    1013         [ -  + ]:     315153 :                         assert(subtask != NULL);
    1014                 :     315153 :                         subtask->scsi.offset = task->current_data_offset;
    1015                 :     315153 :                         spdk_scsi_task_set_data(&subtask->scsi, NULL, 0);
    1016                 :            : 
    1017         [ +  + ]:     315153 :                         if (spdk_scsi_dev_get_lun(conn->dev, task->lun_id) == NULL) {
    1018                 :            :                                 /* Stop submitting split read I/Os for remaining data. */
    1019         [ +  + ]:         30 :                                 TAILQ_REMOVE(&conn->queued_datain_tasks, task, link);
    1020                 :         30 :                                 task->current_data_offset += remaining_size;
    1021         [ -  + ]:         30 :                                 assert(task->current_data_offset == task->scsi.transfer_len);
    1022                 :         30 :                                 subtask->scsi.transfer_len = remaining_size;
    1023                 :         30 :                                 spdk_scsi_task_process_null_lun(&subtask->scsi);
    1024                 :         30 :                                 iscsi_task_cpl(&subtask->scsi);
    1025                 :         30 :                                 return 0;
    1026                 :            :                         }
    1027                 :            : 
    1028                 :     315123 :                         subtask->scsi.length = spdk_min(SPDK_BDEV_LARGE_BUF_MAX_SIZE, remaining_size);
    1029                 :     315123 :                         task->current_data_offset += subtask->scsi.length;
    1030                 :     315123 :                         iscsi_queue_task(conn, subtask);
    1031                 :            :                 }
    1032         [ +  + ]:     315123 :                 if (task->current_data_offset == task->scsi.transfer_len) {
    1033         [ +  + ]:     131888 :                         TAILQ_REMOVE(&conn->queued_datain_tasks, task, link);
    1034                 :            :                 }
    1035                 :            :         }
    1036                 :    4430035 :         return 0;
    1037                 :            : }
    1038                 :            : 
    1039                 :            : void
    1040                 :          6 : iscsi_task_mgmt_cpl(struct spdk_scsi_task *scsi_task)
    1041                 :            : {
    1042                 :          6 :         struct spdk_iscsi_task *task = iscsi_task_from_scsi_task(scsi_task);
    1043                 :            : 
    1044                 :          6 :         iscsi_task_mgmt_response(task->conn, task);
    1045                 :          6 :         iscsi_task_put(task);
    1046                 :          6 : }
    1047                 :            : 
    1048                 :            : static void
    1049                 :     286191 : process_completed_read_subtask_list_in_order(struct spdk_iscsi_conn *conn,
    1050                 :            :                 struct spdk_iscsi_task *primary)
    1051                 :            : {
    1052                 :            :         struct spdk_iscsi_task *subtask, *tmp;
    1053                 :            : 
    1054         [ +  + ]:     601448 :         TAILQ_FOREACH_SAFE(subtask, &primary->subtask_list, subtask_link, tmp) {
    1055         [ +  + ]:     315285 :                 if (subtask->scsi.offset == primary->bytes_completed) {
    1056         [ +  + ]:     315257 :                         TAILQ_REMOVE(&primary->subtask_list, subtask, subtask_link);
    1057                 :     315257 :                         primary->bytes_completed += subtask->scsi.length;
    1058         [ +  + ]:     315257 :                         if (primary->bytes_completed == primary->scsi.transfer_len) {
    1059                 :     131946 :                                 iscsi_task_put(primary);
    1060                 :            :                         }
    1061                 :     315257 :                         iscsi_task_response(conn, subtask);
    1062                 :     315257 :                         iscsi_task_put(subtask);
    1063                 :            :                 } else {
    1064                 :         28 :                         break;
    1065                 :            :                 }
    1066                 :            :         }
    1067                 :     286191 : }
    1068                 :            : 
    1069                 :            : static void
    1070                 :    4299393 : process_read_task_completion(struct spdk_iscsi_conn *conn,
    1071                 :            :                              struct spdk_iscsi_task *task,
    1072                 :            :                              struct spdk_iscsi_task *primary)
    1073                 :            : {
    1074                 :            :         struct spdk_iscsi_task *tmp;
    1075                 :            : 
    1076         [ +  + ]:    4299393 :         if (task->scsi.status != SPDK_SCSI_STATUS_GOOD) {
    1077         [ +  + ]:       1230 :                 if (primary->scsi.status == SPDK_SCSI_STATUS_GOOD) {
    1078                 :            :                         /* If the status of the completed subtask, task, is the
    1079                 :            :                          * first failure, copy it to out-of-order subtasks, and
    1080                 :            :                          * remember it as the status of the SCSI Read Command.
    1081                 :            :                          */
    1082         [ +  + ]:         42 :                         TAILQ_FOREACH(tmp, &primary->subtask_list, subtask_link) {
    1083                 :          8 :                                 spdk_scsi_task_copy_status(&tmp->scsi, &task->scsi);
    1084                 :            :                         }
    1085                 :         34 :                         spdk_scsi_task_copy_status(&primary->scsi, &task->scsi);
    1086                 :            :                 }
    1087         [ +  + ]:    4298163 :         } else if (primary->scsi.status != SPDK_SCSI_STATUS_GOOD) {
    1088                 :            :                 /* Even if the status of the completed subtask is success,
    1089                 :            :                  * if there are any failed subtask ever, copy the first failed
    1090                 :            :                  * status to it.
    1091                 :            :                  */
    1092                 :         12 :                 spdk_scsi_task_copy_status(&task->scsi, &primary->scsi);
    1093                 :            :         }
    1094                 :            : 
    1095         [ +  + ]:    4299393 :         if (task == primary) {
    1096                 :            :                 /* If read I/O size is not larger than SPDK_BDEV_LARGE_BUF_MAX_SIZE,
    1097                 :            :                  * the primary task which processes the SCSI Read Command PDU is
    1098                 :            :                  * submitted directly. Hence send SCSI Response PDU for the primary
    1099                 :            :                  * task simply.
    1100                 :            :                  */
    1101                 :    3984132 :                 primary->bytes_completed = task->scsi.length;
    1102         [ -  + ]:    3984132 :                 assert(primary->bytes_completed == task->scsi.transfer_len);
    1103                 :    3984132 :                 iscsi_task_response(conn, task);
    1104                 :    3984132 :                 iscsi_task_put(task);
    1105   [ -  +  -  + ]:     315261 :         } else if (!conn->sess->DataSequenceInOrder) {
    1106                 :            :                 /* If DataSequenceInOrder is No, send SCSI Response PDU for the completed
    1107                 :            :                  * subtask without any deferral.
    1108                 :            :                  */
    1109                 :          0 :                 primary->bytes_completed += task->scsi.length;
    1110         [ #  # ]:          0 :                 if (primary->bytes_completed == primary->scsi.transfer_len) {
    1111                 :          0 :                         iscsi_task_put(primary);
    1112                 :            :                 }
    1113                 :          0 :                 iscsi_task_response(conn, task);
    1114                 :          0 :                 iscsi_task_put(task);
    1115                 :            :         } else {
    1116                 :            :                 /* If DataSequenceInOrder is Yes, if the completed subtask is out-of-order,
    1117                 :            :                  * it is deferred until all preceding subtasks send SCSI Response PDU.
    1118                 :            :                  */
    1119         [ +  + ]:     315261 :                 if (task->scsi.offset != primary->bytes_completed) {
    1120         [ +  + ]:      29189 :                         TAILQ_FOREACH(tmp, &primary->subtask_list, subtask_link) {
    1121         [ +  + ]:        641 :                                 if (task->scsi.offset < tmp->scsi.offset) {
    1122                 :        522 :                                         TAILQ_INSERT_BEFORE(tmp, task, subtask_link);
    1123                 :        522 :                                         return;
    1124                 :            :                                 }
    1125                 :            :                         }
    1126                 :            : 
    1127                 :      28548 :                         TAILQ_INSERT_TAIL(&primary->subtask_list, task, subtask_link);
    1128                 :            :                 } else {
    1129         [ +  + ]:     286191 :                         TAILQ_INSERT_HEAD(&primary->subtask_list, task, subtask_link);
    1130                 :     286191 :                         process_completed_read_subtask_list_in_order(conn, primary);
    1131                 :            :                 }
    1132                 :            :         }
    1133                 :            : }
    1134                 :            : 
    1135                 :            : static void
    1136                 :    2414505 : process_non_read_task_completion(struct spdk_iscsi_conn *conn,
    1137                 :            :                                  struct spdk_iscsi_task *task,
    1138                 :            :                                  struct spdk_iscsi_task *primary)
    1139                 :            : {
    1140                 :    2414505 :         primary->bytes_completed += task->scsi.length;
    1141                 :            : 
    1142         [ +  + ]:    2414505 :         if (task == primary) {
    1143                 :            :                 /* This was a small write with no R2T. */
    1144                 :    1896652 :                 iscsi_task_response(conn, task);
    1145                 :    1896652 :                 iscsi_task_put(task);
    1146                 :    1896652 :                 return;
    1147                 :            :         }
    1148                 :            : 
    1149         [ +  + ]:     517853 :         if (task->scsi.status == SPDK_SCSI_STATUS_GOOD) {
    1150                 :     517849 :                 primary->scsi.data_transferred += task->scsi.data_transferred;
    1151         [ +  - ]:          4 :         } else if (primary->scsi.status == SPDK_SCSI_STATUS_GOOD) {
    1152                 :            :                 /* If the status of this subtask is the first failure, copy it to
    1153                 :            :                  * the primary task.
    1154                 :            :                  */
    1155                 :          4 :                 spdk_scsi_task_copy_status(&primary->scsi, &task->scsi);
    1156                 :            :         }
    1157                 :            : 
    1158         [ +  + ]:     517853 :         if (primary->bytes_completed == primary->scsi.transfer_len) {
    1159                 :            :                 /* If LUN is removed in the middle of the iSCSI write sequence,
    1160                 :            :                  *  primary might complete the write to the initiator because it is not
    1161                 :            :                  *  ensured that the initiator will send all data requested by R2Ts.
    1162                 :            :                  *
    1163                 :            :                  * We check it and skip the following if primary is completed. (see
    1164                 :            :                  *  iscsi_clear_all_transfer_task() in iscsi.c.)
    1165                 :            :                  */
    1166   [ +  +  +  + ]:     182637 :                 if (primary->is_r2t_active) {
    1167                 :     182633 :                         iscsi_task_response(conn, primary);
    1168                 :     182633 :                         iscsi_del_transfer_task(conn, primary->tag);
    1169                 :            :                 } else {
    1170                 :          4 :                         iscsi_task_response(conn, task);
    1171                 :            :                 }
    1172                 :            :         }
    1173                 :     517853 :         iscsi_task_put(task);
    1174                 :            : }
    1175                 :            : 
    1176                 :            : void
    1177                 :    6713794 : iscsi_task_cpl(struct spdk_scsi_task *scsi_task)
    1178                 :            : {
    1179                 :            :         struct spdk_iscsi_task *primary;
    1180                 :    6713794 :         struct spdk_iscsi_task *task = iscsi_task_from_scsi_task(scsi_task);
    1181                 :    6713794 :         struct spdk_iscsi_conn *conn = task->conn;
    1182                 :    6713794 :         struct spdk_iscsi_pdu *pdu = task->pdu;
    1183                 :            : 
    1184   [ +  +  +  + ]:    6713794 :         spdk_trace_record(TRACE_ISCSI_TASK_DONE, conn->trace_id, 0, (uintptr_t)task);
    1185                 :            : 
    1186                 :    6713794 :         task->is_queued = false;
    1187                 :    6713794 :         primary = iscsi_task_get_primary(task);
    1188                 :            : 
    1189         [ +  + ]:    6713794 :         if (iscsi_task_is_read(primary)) {
    1190                 :    4299305 :                 process_read_task_completion(conn, task, primary);
    1191                 :            :         } else {
    1192                 :    2414489 :                 process_non_read_task_completion(conn, task, primary);
    1193                 :            :         }
    1194         [ +  + ]:    6713794 :         if (!task->parent) {
    1195   [ +  -  +  + ]:    6398621 :                 spdk_trace_record(TRACE_ISCSI_PDU_COMPLETED, conn->trace_id, 0, (uintptr_t)pdu);
    1196                 :            :         }
    1197                 :    6713794 : }
    1198                 :            : 
    1199                 :            : static void
    1200                 :         22 : iscsi_conn_send_nopin(struct spdk_iscsi_conn *conn)
    1201                 :            : {
    1202                 :            :         struct spdk_iscsi_pdu *rsp_pdu;
    1203                 :            :         struct iscsi_bhs_nop_in *rsp;
    1204                 :            :         /* Only send nopin if we have logged in and are in a normal session. */
    1205         [ +  - ]:         22 :         if (conn->sess == NULL ||
    1206   [ +  -  -  + ]:         44 :             !conn->full_feature ||
    1207                 :         22 :             !iscsi_param_eq_val(conn->sess->params, "SessionType", "Normal")) {
    1208                 :          0 :                 return;
    1209                 :            :         }
    1210   [ -  +  -  + ]:         22 :         SPDK_DEBUGLOG(iscsi, "send NOPIN isid=%"PRIx64", tsih=%u, cid=%u\n",
    1211                 :            :                       conn->sess->isid, conn->sess->tsih, conn->cid);
    1212   [ -  +  -  + ]:         22 :         SPDK_DEBUGLOG(iscsi, "StatSN=%u, ExpCmdSN=%u, MaxCmdSN=%u\n",
    1213                 :            :                       conn->StatSN, conn->sess->ExpCmdSN,
    1214                 :            :                       conn->sess->MaxCmdSN);
    1215                 :         22 :         rsp_pdu = iscsi_get_pdu(conn);
    1216                 :         22 :         rsp = (struct iscsi_bhs_nop_in *) &rsp_pdu->bhs;
    1217                 :         22 :         rsp_pdu->data = NULL;
    1218                 :            :         /*
    1219                 :            :          * iscsi_get_pdu() memset's the PDU for us, so only fill out the needed
    1220                 :            :          *  fields.
    1221                 :            :          */
    1222                 :         22 :         rsp->opcode = ISCSI_OP_NOPIN;
    1223                 :         22 :         rsp->flags = 0x80;
    1224                 :            :         /*
    1225                 :            :          * Technically the to_be32() is not needed here, since
    1226                 :            :          *  to_be32(0xFFFFFFFU) returns 0xFFFFFFFFU.
    1227                 :            :          */
    1228                 :         22 :         to_be32(&rsp->itt, 0xFFFFFFFFU);
    1229                 :         22 :         to_be32(&rsp->ttt, conn->id);
    1230                 :         22 :         to_be32(&rsp->stat_sn, conn->StatSN);
    1231                 :         22 :         to_be32(&rsp->exp_cmd_sn, conn->sess->ExpCmdSN);
    1232                 :         22 :         to_be32(&rsp->max_cmd_sn, conn->sess->MaxCmdSN);
    1233                 :         22 :         iscsi_conn_write_pdu(conn, rsp_pdu, iscsi_conn_pdu_generic_complete, NULL);
    1234                 :         22 :         conn->last_nopin = spdk_get_ticks();
    1235                 :         22 :         conn->nop_outstanding = true;
    1236                 :            : }
    1237                 :            : 
    1238                 :            : void
    1239                 :       1959 : iscsi_conn_handle_nop(struct spdk_iscsi_conn *conn)
    1240                 :            : {
    1241                 :            :         uint64_t        tsc;
    1242                 :            : 
    1243                 :            :         /**
    1244                 :            :           * This function will be executed by nop_poller of iSCSI polling group, so
    1245                 :            :           * we need to check the connection state first, then do the nop interval
    1246                 :            :           * expiration check work.
    1247                 :            :           */
    1248         [ +  - ]:       1959 :         if ((conn->state == ISCSI_CONN_STATE_EXITED) ||
    1249         [ -  + ]:       1959 :             (conn->state == ISCSI_CONN_STATE_EXITING)) {
    1250                 :          0 :                 return;
    1251                 :            :         }
    1252                 :            : 
    1253                 :            :         /* Check for nop interval expiration */
    1254                 :       1959 :         tsc = spdk_get_ticks();
    1255   [ -  +  +  + ]:       1959 :         if (conn->nop_outstanding) {
    1256         [ -  + ]:         80 :                 if ((tsc - conn->last_nopin) > conn->timeout) {
    1257                 :          0 :                         SPDK_ERRLOG("Timed out waiting for NOP-Out response from initiator\n");
    1258                 :          0 :                         SPDK_ERRLOG("  tsc=0x%" PRIx64 ", last_nopin=0x%" PRIx64 "\n", tsc, conn->last_nopin);
    1259                 :          0 :                         SPDK_ERRLOG("  initiator=%s, target=%s\n", conn->initiator_name,
    1260                 :            :                                     conn->target_short_name);
    1261                 :          0 :                         conn->state = ISCSI_CONN_STATE_EXITING;
    1262                 :            :                 }
    1263         [ +  + ]:       1879 :         } else if (tsc - conn->last_nopin > conn->nopininterval) {
    1264                 :         22 :                 iscsi_conn_send_nopin(conn);
    1265                 :            :         }
    1266                 :            : }
    1267                 :            : 
    1268                 :            : /**
    1269                 :            :  * \brief Reads data for the specified iSCSI connection from its TCP socket.
    1270                 :            :  *
    1271                 :            :  * The TCP socket is marked as non-blocking, so this function may not read
    1272                 :            :  * all data requested.
    1273                 :            :  *
    1274                 :            :  * Returns SPDK_ISCSI_CONNECTION_FATAL if the recv() operation indicates a fatal
    1275                 :            :  * error with the TCP connection (including if the TCP connection was closed
    1276                 :            :  * unexpectedly.
    1277                 :            :  *
    1278                 :            :  * Otherwise returns the number of bytes successfully read.
    1279                 :            :  */
    1280                 :            : int
    1281                 :   25110495 : iscsi_conn_read_data(struct spdk_iscsi_conn *conn, int bytes,
    1282                 :            :                      void *buf)
    1283                 :            : {
    1284                 :            :         int ret;
    1285                 :            : 
    1286         [ -  + ]:   25110495 :         if (bytes == 0) {
    1287                 :          0 :                 return 0;
    1288                 :            :         }
    1289                 :            : 
    1290                 :   25110495 :         ret = spdk_sock_recv(conn->sock, buf, bytes);
    1291                 :            : 
    1292         [ +  + ]:   25110495 :         if (ret > 0) {
    1293   [ +  -  +  + ]:   11695152 :                 spdk_trace_record(TRACE_ISCSI_READ_FROM_SOCKET_DONE, conn->trace_id, ret, 0);
    1294                 :   11695152 :                 return ret;
    1295                 :            :         }
    1296                 :            : 
    1297         [ +  + ]:   13415343 :         if (ret < 0) {
    1298   [ +  +  -  + ]:   13414853 :                 if (errno == EAGAIN || errno == EWOULDBLOCK) {
    1299                 :   13414804 :                         return 0;
    1300                 :            :                 }
    1301                 :            : 
    1302                 :            :                 /* For connect reset issue, do not output error log */
    1303         [ +  - ]:         49 :                 if (errno == ECONNRESET) {
    1304   [ -  +  -  + ]:         49 :                         SPDK_DEBUGLOG(iscsi, "spdk_sock_recv() failed, errno %d: %s\n",
    1305                 :            :                                       errno, spdk_strerror(errno));
    1306                 :            :                 } else {
    1307                 :          0 :                         SPDK_ERRLOG("spdk_sock_recv() failed, errno %d: %s\n",
    1308                 :            :                                     errno, spdk_strerror(errno));
    1309                 :            :                 }
    1310                 :            :         }
    1311                 :            : 
    1312                 :            :         /* connection closed */
    1313                 :        539 :         return SPDK_ISCSI_CONNECTION_FATAL;
    1314                 :            : }
    1315                 :            : 
    1316                 :            : int
    1317                 :          0 : iscsi_conn_readv_data(struct spdk_iscsi_conn *conn,
    1318                 :            :                       struct iovec *iov, int iovcnt)
    1319                 :            : {
    1320                 :            :         int ret;
    1321                 :            : 
    1322   [ #  #  #  # ]:          0 :         if (iov == NULL || iovcnt == 0) {
    1323                 :          0 :                 return 0;
    1324                 :            :         }
    1325                 :            : 
    1326         [ #  # ]:          0 :         if (iovcnt == 1) {
    1327                 :          0 :                 return iscsi_conn_read_data(conn, iov[0].iov_len,
    1328                 :            :                                             iov[0].iov_base);
    1329                 :            :         }
    1330                 :            : 
    1331                 :          0 :         ret = spdk_sock_readv(conn->sock, iov, iovcnt);
    1332                 :            : 
    1333         [ #  # ]:          0 :         if (ret > 0) {
    1334   [ #  #  #  # ]:          0 :                 spdk_trace_record(TRACE_ISCSI_READ_FROM_SOCKET_DONE, conn->trace_id, ret, 0);
    1335                 :          0 :                 return ret;
    1336                 :            :         }
    1337                 :            : 
    1338         [ #  # ]:          0 :         if (ret < 0) {
    1339   [ #  #  #  # ]:          0 :                 if (errno == EAGAIN || errno == EWOULDBLOCK) {
    1340                 :          0 :                         return 0;
    1341                 :            :                 }
    1342                 :            : 
    1343                 :            :                 /* For connect reset issue, do not output error log */
    1344         [ #  # ]:          0 :                 if (errno == ECONNRESET) {
    1345   [ #  #  #  # ]:          0 :                         SPDK_DEBUGLOG(iscsi, "spdk_sock_readv() failed, errno %d: %s\n",
    1346                 :            :                                       errno, spdk_strerror(errno));
    1347                 :            :                 } else {
    1348                 :          0 :                         SPDK_ERRLOG("spdk_sock_readv() failed, errno %d: %s\n",
    1349                 :            :                                     errno, spdk_strerror(errno));
    1350                 :            :                 }
    1351                 :            :         }
    1352                 :            : 
    1353                 :            :         /* connection closed */
    1354                 :          0 :         return SPDK_ISCSI_CONNECTION_FATAL;
    1355                 :            : }
    1356                 :            : 
    1357                 :            : static bool
    1358                 :       2354 : iscsi_is_free_pdu_deferred(struct spdk_iscsi_pdu *pdu)
    1359                 :            : {
    1360         [ -  + ]:       2354 :         if (pdu == NULL) {
    1361                 :          0 :                 return false;
    1362                 :            :         }
    1363                 :            : 
    1364         [ +  + ]:       2354 :         if (pdu->bhs.opcode == ISCSI_OP_R2T ||
    1365         [ +  + ]:       2172 :             pdu->bhs.opcode == ISCSI_OP_SCSI_DATAIN) {
    1366                 :       1474 :                 return true;
    1367                 :            :         }
    1368                 :            : 
    1369                 :        880 :         return false;
    1370                 :            : }
    1371                 :            : 
    1372                 :            : static int
    1373                 :          0 : iscsi_dif_verify(struct spdk_iscsi_pdu *pdu, struct spdk_dif_ctx *dif_ctx)
    1374                 :            : {
    1375                 :          0 :         struct iovec iov;
    1376                 :          0 :         struct spdk_dif_error err_blk = {};
    1377                 :            :         uint32_t num_blocks;
    1378                 :            :         int rc;
    1379                 :            : 
    1380                 :          0 :         iov.iov_base = pdu->data;
    1381                 :          0 :         iov.iov_len = pdu->data_buf_len;
    1382         [ #  # ]:          0 :         num_blocks = pdu->data_buf_len / dif_ctx->block_size;
    1383                 :            : 
    1384                 :          0 :         rc = spdk_dif_verify(&iov, 1, num_blocks, dif_ctx, &err_blk);
    1385         [ #  # ]:          0 :         if (rc != 0) {
    1386                 :          0 :                 SPDK_ERRLOG("DIF error detected. type=%d, offset=%" PRIu32 "\n",
    1387                 :            :                             err_blk.err_type, err_blk.err_offset);
    1388                 :            :         }
    1389                 :            : 
    1390                 :          0 :         return rc;
    1391                 :            : }
    1392                 :            : 
    1393                 :            : static void
    1394                 :    7145172 : _iscsi_conn_pdu_write_done(void *cb_arg, int err)
    1395                 :            : {
    1396                 :    7145172 :         struct spdk_iscsi_pdu *pdu = cb_arg;
    1397                 :    7145172 :         struct spdk_iscsi_conn *conn = pdu->conn;
    1398                 :            : 
    1399         [ -  + ]:    7145172 :         assert(conn != NULL);
    1400                 :            : 
    1401         [ -  + ]:    7145172 :         if (spdk_unlikely(conn->state >= ISCSI_CONN_STATE_EXITING)) {
    1402                 :            :                 /* The other policy will recycle the resource */
    1403                 :          0 :                 return;
    1404                 :            :         }
    1405                 :            : 
    1406         [ +  + ]:    7145172 :         TAILQ_REMOVE(&conn->write_pdu_list, pdu, tailq);
    1407                 :            : 
    1408         [ +  + ]:    7145172 :         if (err != 0) {
    1409                 :          2 :                 conn->state = ISCSI_CONN_STATE_EXITING;
    1410                 :            :         }
    1411                 :            : 
    1412         [ +  + ]:    7145172 :         if ((conn->full_feature) &&
    1413   [ +  +  +  + ]:    7146404 :             (conn->sess->ErrorRecoveryLevel >= 1) &&
    1414                 :       2354 :             iscsi_is_free_pdu_deferred(pdu)) {
    1415   [ -  +  -  + ]:       1474 :                 SPDK_DEBUGLOG(iscsi, "stat_sn=%d\n",
    1416                 :            :                               from_be32(&pdu->bhs.stat_sn));
    1417                 :       1474 :                 TAILQ_INSERT_TAIL(&conn->snack_pdu_list, pdu,
    1418                 :            :                                   tailq);
    1419                 :            :         } else {
    1420                 :    7143698 :                 iscsi_conn_free_pdu(conn, pdu);
    1421                 :            :         }
    1422                 :            : }
    1423                 :            : 
    1424                 :            : void
    1425                 :    2840167 : iscsi_conn_pdu_generic_complete(void *cb_arg)
    1426                 :            : {
    1427                 :    2840167 : }
    1428                 :            : 
    1429                 :            : void
    1430                 :    7145172 : iscsi_conn_write_pdu(struct spdk_iscsi_conn *conn, struct spdk_iscsi_pdu *pdu,
    1431                 :            :                      iscsi_conn_xfer_complete_cb cb_fn,
    1432                 :            :                      void *cb_arg)
    1433                 :            : {
    1434                 :            :         uint32_t crc32c;
    1435                 :            :         ssize_t rc;
    1436                 :            : 
    1437   [ -  +  -  + ]:    7145172 :         if (spdk_unlikely(pdu->dif_insert_or_strip)) {
    1438                 :          0 :                 rc = iscsi_dif_verify(pdu, &pdu->dif_ctx);
    1439         [ #  # ]:          0 :                 if (rc != 0) {
    1440                 :          0 :                         iscsi_conn_free_pdu(conn, pdu);
    1441                 :          0 :                         conn->state = ISCSI_CONN_STATE_EXITING;
    1442                 :          0 :                         return;
    1443                 :            :                 }
    1444                 :            :         }
    1445                 :            : 
    1446         [ +  + ]:    7145172 :         if (pdu->bhs.opcode != ISCSI_OP_LOGIN_RSP) {
    1447                 :            :                 /* Header Digest */
    1448         [ +  + ]:    7138723 :                 if (conn->header_digest) {
    1449                 :     206307 :                         crc32c = iscsi_pdu_calc_header_digest(pdu);
    1450                 :     206307 :                         MAKE_DIGEST_WORD(pdu->header_digest, crc32c);
    1451                 :            :                 }
    1452                 :            : 
    1453                 :            :                 /* Data Digest */
    1454   [ -  +  -  - ]:    7138723 :                 if (conn->data_digest && DGET24(pdu->bhs.data_segment_len) != 0) {
    1455                 :          0 :                         crc32c = iscsi_pdu_calc_data_digest(pdu);
    1456                 :          0 :                         MAKE_DIGEST_WORD(pdu->data_digest, crc32c);
    1457                 :            :                 }
    1458                 :            :         }
    1459                 :            : 
    1460                 :    7145172 :         pdu->cb_fn = cb_fn;
    1461                 :    7145172 :         pdu->cb_arg = cb_arg;
    1462                 :    7145172 :         TAILQ_INSERT_TAIL(&conn->write_pdu_list, pdu, tailq);
    1463                 :            : 
    1464         [ -  + ]:    7145172 :         if (spdk_unlikely(conn->state >= ISCSI_CONN_STATE_EXITING)) {
    1465                 :          0 :                 return;
    1466                 :            :         }
    1467                 :    7145172 :         pdu->sock_req.iovcnt = iscsi_build_iovs(conn, pdu->iov, SPDK_COUNTOF(pdu->iov), pdu,
    1468                 :    7145172 :                                                 &pdu->mapped_length);
    1469                 :    7145172 :         pdu->sock_req.cb_fn = _iscsi_conn_pdu_write_done;
    1470                 :    7145172 :         pdu->sock_req.cb_arg = pdu;
    1471                 :            : 
    1472                 :    7145172 :         spdk_sock_writev_async(conn->sock, &pdu->sock_req);
    1473                 :            : }
    1474                 :            : 
    1475                 :            : static void
    1476                 :   15080376 : iscsi_conn_sock_cb(void *arg, struct spdk_sock_group *group, struct spdk_sock *sock)
    1477                 :            : {
    1478                 :   15080376 :         struct spdk_iscsi_conn *conn = arg;
    1479                 :            :         int rc;
    1480                 :            : 
    1481         [ -  + ]:   15080376 :         assert(conn != NULL);
    1482                 :            : 
    1483         [ +  - ]:   15080376 :         if ((conn->state == ISCSI_CONN_STATE_EXITED) ||
    1484         [ -  + ]:   15080376 :             (conn->state == ISCSI_CONN_STATE_EXITING)) {
    1485                 :          0 :                 return;
    1486                 :            :         }
    1487                 :            : 
    1488                 :            :         /* Handle incoming PDUs */
    1489                 :   15080376 :         rc = iscsi_handle_incoming_pdus(conn);
    1490         [ +  + ]:   15080376 :         if (rc < 0) {
    1491                 :        551 :                 conn->state = ISCSI_CONN_STATE_EXITING;
    1492                 :            :         }
    1493                 :            : }
    1494                 :            : 
    1495                 :            : static void
    1496                 :        460 : iscsi_conn_full_feature_migrate(void *arg)
    1497                 :            : {
    1498                 :        460 :         struct spdk_iscsi_conn *conn = arg;
    1499                 :            :         int rc;
    1500                 :            : 
    1501         [ -  + ]:        460 :         assert(conn->state != ISCSI_CONN_STATE_EXITED);
    1502                 :            : 
    1503                 :            :         /* Note: it is possible that connection could have moved to EXITING
    1504                 :            :          * state after this message was sent. We will still add it to the
    1505                 :            :          * poll group in this case.  When the poll group is polled
    1506                 :            :          * again, it will call iscsi_conn_destruct() on it.
    1507                 :            :          */
    1508                 :            : 
    1509         [ +  - ]:        460 :         if (conn->sess->session_type == SESSION_TYPE_NORMAL) {
    1510                 :        460 :                 rc = iscsi_conn_open_luns(conn);
    1511         [ -  + ]:        460 :                 if (rc != 0) {
    1512                 :            :                         /* If opening LUNs failed, it is a fatal error. At the first poll in the
    1513                 :            :                          * assigned poll group, this connection will be destructed.
    1514                 :            :                          */
    1515                 :          0 :                         conn->state = ISCSI_CONN_STATE_EXITING;
    1516                 :            :                 }
    1517                 :            :         }
    1518                 :            : 
    1519                 :            :         /* Add this connection to the assigned poll group. */
    1520                 :        460 :         iscsi_poll_group_add_conn(conn->pg, conn);
    1521                 :        460 : }
    1522                 :            : 
    1523                 :            : static struct spdk_iscsi_poll_group *g_next_pg = NULL;
    1524                 :            : 
    1525                 :            : void
    1526                 :        489 : iscsi_conn_schedule(struct spdk_iscsi_conn *conn)
    1527                 :            : {
    1528                 :            :         struct spdk_iscsi_poll_group    *pg;
    1529                 :            :         struct spdk_iscsi_tgt_node      *target;
    1530                 :            : 
    1531         [ +  + ]:        489 :         if (conn->sess->session_type != SESSION_TYPE_NORMAL) {
    1532                 :            :                 /* Leave all non-normal sessions on the acceptor
    1533                 :            :                  * thread. */
    1534                 :         29 :                 return;
    1535                 :            :         }
    1536         [ -  + ]:        460 :         pthread_mutex_lock(&g_iscsi.mutex);
    1537                 :            : 
    1538                 :        460 :         target = conn->sess->target;
    1539         [ -  + ]:        460 :         pthread_mutex_lock(&target->mutex);
    1540                 :        460 :         target->num_active_conns++;
    1541         [ +  + ]:        460 :         if (target->num_active_conns == 1) {
    1542                 :            :                 /**
    1543                 :            :                  * This is the only active connection for this target node.
    1544                 :            :                  *  Pick a poll group using round-robin.
    1545                 :            :                  */
    1546         [ +  + ]:        438 :                 if (g_next_pg == NULL) {
    1547                 :        404 :                         g_next_pg = TAILQ_FIRST(&g_iscsi.poll_group_head);
    1548         [ -  + ]:        404 :                         assert(g_next_pg != NULL);
    1549                 :            :                 }
    1550                 :            : 
    1551                 :        438 :                 pg = g_next_pg;
    1552                 :        438 :                 g_next_pg = TAILQ_NEXT(g_next_pg, link);
    1553                 :            : 
    1554                 :            :                 /* Save the pg in the target node so it can be used for any other connections to this target node. */
    1555                 :        438 :                 target->pg = pg;
    1556                 :            :         } else {
    1557                 :            :                 /**
    1558                 :            :                  * There are other active connections for this target node.
    1559                 :            :                  */
    1560                 :         22 :                 pg = target->pg;
    1561                 :            :         }
    1562                 :            : 
    1563         [ -  + ]:        460 :         pthread_mutex_unlock(&target->mutex);
    1564         [ -  + ]:        460 :         pthread_mutex_unlock(&g_iscsi.mutex);
    1565                 :            : 
    1566         [ -  + ]:        460 :         assert(spdk_io_channel_get_thread(spdk_io_channel_from_ctx(conn->pg)) ==
    1567                 :            :                spdk_get_thread());
    1568                 :            : 
    1569                 :            :         /* Remove this connection from the previous poll group */
    1570                 :        460 :         iscsi_poll_group_remove_conn(conn->pg, conn);
    1571                 :            : 
    1572                 :        460 :         conn->pg = pg;
    1573                 :            : 
    1574                 :        460 :         spdk_thread_send_msg(spdk_io_channel_get_thread(spdk_io_channel_from_ctx(pg)),
    1575                 :            :                              iscsi_conn_full_feature_migrate, conn);
    1576                 :            : }
    1577                 :            : 
    1578                 :            : static int
    1579                 :          0 : logout_timeout(void *arg)
    1580                 :            : {
    1581                 :          0 :         struct spdk_iscsi_conn *conn = arg;
    1582                 :            : 
    1583         [ #  # ]:          0 :         if (conn->state < ISCSI_CONN_STATE_EXITING) {
    1584                 :          0 :                 conn->state = ISCSI_CONN_STATE_EXITING;
    1585                 :            :         }
    1586                 :            : 
    1587                 :          0 :         return SPDK_POLLER_BUSY;
    1588                 :            : }
    1589                 :            : 
    1590                 :            : void
    1591                 :        339 : iscsi_conn_logout(struct spdk_iscsi_conn *conn)
    1592                 :            : {
    1593                 :        339 :         conn->is_logged_out = true;
    1594                 :        339 :         conn->logout_timer = SPDK_POLLER_REGISTER(logout_timeout, conn, ISCSI_LOGOUT_TIMEOUT * 1000000);
    1595                 :        339 : }
    1596                 :            : 
    1597                 :            : static const char *
    1598                 :          8 : iscsi_conn_get_state(struct spdk_iscsi_conn *conn)
    1599                 :            : {
    1600   [ -  +  -  -  :          8 :         switch (conn->state) {
                      - ]
    1601                 :          0 :                 SPDK_ISCSI_CONNECTION_STATUS(ISCSI_CONN_STATE_INVALID, "invalid");
    1602                 :          8 :                 SPDK_ISCSI_CONNECTION_STATUS(ISCSI_CONN_STATE_RUNNING, "running");
    1603                 :          0 :                 SPDK_ISCSI_CONNECTION_STATUS(ISCSI_CONN_STATE_EXITING, "exiting");
    1604                 :          0 :                 SPDK_ISCSI_CONNECTION_STATUS(ISCSI_CONN_STATE_EXITED, "exited");
    1605                 :            :         }
    1606                 :          0 :         return "unknown";
    1607                 :            : }
    1608                 :            : 
    1609                 :            : static const char *
    1610                 :          8 : iscsi_conn_get_login_phase(struct spdk_iscsi_conn *conn)
    1611                 :            : {
    1612   [ -  -  +  - ]:          8 :         switch (conn->login_phase) {
    1613                 :          0 :                 SPDK_ISCSI_CONNECTION_STATUS(ISCSI_SECURITY_NEGOTIATION_PHASE, "security_negotiation_phase");
    1614                 :          0 :                 SPDK_ISCSI_CONNECTION_STATUS(ISCSI_OPERATIONAL_NEGOTIATION_PHASE, "operational_negotiation_phase");
    1615                 :          8 :                 SPDK_ISCSI_CONNECTION_STATUS(ISCSI_FULL_FEATURE_PHASE, "full_feature_phase");
    1616                 :            :         }
    1617                 :          0 :         return "not_started";
    1618                 :            : }
    1619                 :            : 
    1620                 :       1360 : SPDK_TRACE_REGISTER_FN(iscsi_conn_trace, "iscsi_conn", TRACE_GROUP_ISCSI)
    1621                 :            : {
    1622                 :        649 :         spdk_trace_register_owner_type(OWNER_TYPE_ISCSI_CONN, 'c');
    1623                 :        649 :         spdk_trace_register_object(OBJECT_ISCSI_PDU, 'p');
    1624                 :        649 :         spdk_trace_register_description("ISCSI_READ_DONE", TRACE_ISCSI_READ_FROM_SOCKET_DONE,
    1625                 :            :                                         OWNER_TYPE_ISCSI_CONN, OBJECT_NONE, 0,
    1626                 :            :                                         SPDK_TRACE_ARG_TYPE_INT, "");
    1627                 :        649 :         spdk_trace_register_description("ISCSI_READ_PDU", TRACE_ISCSI_READ_PDU,
    1628                 :            :                                         OWNER_TYPE_ISCSI_CONN, OBJECT_ISCSI_PDU, 1,
    1629                 :            :                                         SPDK_TRACE_ARG_TYPE_INT, "opc");
    1630                 :        649 :         spdk_trace_register_description("ISCSI_TASK_DONE", TRACE_ISCSI_TASK_DONE,
    1631                 :            :                                         OWNER_TYPE_ISCSI_CONN, OBJECT_SCSI_TASK, 0,
    1632                 :            :                                         SPDK_TRACE_ARG_TYPE_INT, "");
    1633                 :        649 :         spdk_trace_register_description("ISCSI_TASK_QUEUE", TRACE_ISCSI_TASK_QUEUE,
    1634                 :            :                                         OWNER_TYPE_ISCSI_CONN, OBJECT_SCSI_TASK, 1,
    1635                 :            :                                         SPDK_TRACE_ARG_TYPE_PTR, "pdu");
    1636                 :        649 :         spdk_trace_register_description("ISCSI_TASK_EXECUTED", TRACE_ISCSI_TASK_EXECUTED,
    1637                 :            :                                         OWNER_TYPE_ISCSI_CONN, OBJECT_ISCSI_PDU, 0,
    1638                 :            :                                         SPDK_TRACE_ARG_TYPE_INT, "");
    1639                 :        649 :         spdk_trace_register_description("ISCSI_PDU_COMPLETED", TRACE_ISCSI_PDU_COMPLETED,
    1640                 :            :                                         OWNER_TYPE_ISCSI_CONN, OBJECT_ISCSI_PDU, 0,
    1641                 :            :                                         SPDK_TRACE_ARG_TYPE_INT, "");
    1642                 :        649 :         spdk_trace_tpoint_register_relation(TRACE_SOCK_REQ_QUEUE, OBJECT_ISCSI_PDU, 0);
    1643                 :        649 :         spdk_trace_tpoint_register_relation(TRACE_SOCK_REQ_PEND, OBJECT_ISCSI_PDU, 0);
    1644                 :        649 :         spdk_trace_tpoint_register_relation(TRACE_SOCK_REQ_COMPLETE, OBJECT_ISCSI_PDU, 0);
    1645                 :        649 : }
    1646                 :            : 
    1647                 :            : void
    1648                 :          8 : iscsi_conn_info_json(struct spdk_json_write_ctx *w, struct spdk_iscsi_conn *conn)
    1649                 :            : {
    1650                 :            :         uint16_t tsih;
    1651                 :            : 
    1652         [ -  + ]:          8 :         if (!conn->is_valid) {
    1653                 :          0 :                 return;
    1654                 :            :         }
    1655                 :            : 
    1656                 :          8 :         spdk_json_write_object_begin(w);
    1657                 :            : 
    1658                 :          8 :         spdk_json_write_named_int32(w, "id", conn->id);
    1659                 :            : 
    1660                 :          8 :         spdk_json_write_named_int32(w, "cid", conn->cid);
    1661                 :            : 
    1662                 :            :         /*
    1663                 :            :          * If we try to return data for a connection that has not
    1664                 :            :          *  logged in yet, the session will not be set.  So in this
    1665                 :            :          *  case, return -1 for the tsih rather than segfaulting
    1666                 :            :          *  on the null conn->sess.
    1667                 :            :          */
    1668         [ -  + ]:          8 :         if (conn->sess == NULL) {
    1669                 :          0 :                 tsih = -1;
    1670                 :            :         } else {
    1671                 :          8 :                 tsih = conn->sess->tsih;
    1672                 :            :         }
    1673                 :          8 :         spdk_json_write_named_int32(w, "tsih", tsih);
    1674                 :            : 
    1675                 :          8 :         spdk_json_write_named_string(w, "state", iscsi_conn_get_state(conn));
    1676                 :            : 
    1677                 :          8 :         spdk_json_write_named_string(w, "login_phase", iscsi_conn_get_login_phase(conn));
    1678                 :            : 
    1679                 :          8 :         spdk_json_write_named_string(w, "initiator_addr", conn->initiator_addr);
    1680                 :            : 
    1681                 :          8 :         spdk_json_write_named_string(w, "target_addr", conn->target_addr);
    1682                 :            : 
    1683                 :          8 :         spdk_json_write_named_string(w, "target_node_name", conn->target_short_name);
    1684                 :            : 
    1685                 :          8 :         spdk_json_write_named_string(w, "thread_name",
    1686                 :          8 :                                      spdk_thread_get_name(spdk_get_thread()));
    1687                 :            : 
    1688                 :          8 :         spdk_json_write_object_end(w);
    1689                 :            : }

Generated by: LCOV version 1.14