LCOV - code coverage report
Current view: top level - spdk/lib/iscsi - iscsi.c (source / functions) Hit Total Coverage
Test: Combined Lines: 1828 2427 75.3 %
Date: 2024-07-13 02:07:07 Functions: 91 99 91.9 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 993 1659 59.9 %

           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/base64.h"
      10                 :            : #include "spdk/crc32.h"
      11                 :            : #include "spdk/endian.h"
      12                 :            : #include "spdk/env.h"
      13                 :            : #include "spdk/likely.h"
      14                 :            : #include "spdk/trace.h"
      15                 :            : #include "spdk/sock.h"
      16                 :            : #include "spdk/string.h"
      17                 :            : #include "spdk/queue.h"
      18                 :            : 
      19                 :            : #include "iscsi/md5.h"
      20                 :            : #include "iscsi/iscsi.h"
      21                 :            : #include "iscsi/param.h"
      22                 :            : #include "iscsi/tgt_node.h"
      23                 :            : #include "iscsi/task.h"
      24                 :            : #include "iscsi/conn.h"
      25                 :            : #include "spdk/scsi.h"
      26                 :            : #include "spdk/bdev.h"
      27                 :            : #include "iscsi/portal_grp.h"
      28                 :            : 
      29                 :            : #include "spdk/log.h"
      30                 :            : 
      31                 :            : #include "spdk_internal/sgl.h"
      32                 :            : 
      33                 :            : #define MAX_TMPBUF 1024
      34                 :            : 
      35                 :            : struct spdk_iscsi_globals g_iscsi = {
      36                 :            :         .mutex = PTHREAD_MUTEX_INITIALIZER,
      37                 :            :         .portal_head = TAILQ_HEAD_INITIALIZER(g_iscsi.portal_head),
      38                 :            :         .pg_head = TAILQ_HEAD_INITIALIZER(g_iscsi.pg_head),
      39                 :            :         .ig_head = TAILQ_HEAD_INITIALIZER(g_iscsi.ig_head),
      40                 :            :         .target_head = TAILQ_HEAD_INITIALIZER(g_iscsi.target_head),
      41                 :            :         .auth_group_head = TAILQ_HEAD_INITIALIZER(g_iscsi.auth_group_head),
      42                 :            :         .poll_group_head = TAILQ_HEAD_INITIALIZER(g_iscsi.poll_group_head),
      43                 :            : };
      44                 :            : 
      45                 :            : #define MATCH_DIGEST_WORD(BUF, CRC32C) \
      46                 :            :         (    ((((uint32_t) *((uint8_t *)(BUF)+0)) << 0)           \
      47                 :            :             | (((uint32_t) *((uint8_t *)(BUF)+1)) << 8)           \
      48                 :            :             | (((uint32_t) *((uint8_t *)(BUF)+2)) << 16)  \
      49                 :            :             | (((uint32_t) *((uint8_t *)(BUF)+3)) << 24)) \
      50                 :            :             == (CRC32C))
      51                 :            : 
      52                 :            : #ifndef SPDK_CONFIG_HAVE_ARC4RANDOM
      53                 :            : static void
      54                 :          0 : srandomdev(void)
      55                 :            : {
      56                 :            :         unsigned long seed;
      57                 :            :         time_t now;
      58                 :            :         pid_t pid;
      59                 :            : 
      60                 :          0 :         pid = getpid();
      61                 :          0 :         now = time(NULL);
      62                 :          0 :         seed = pid ^ now;
      63                 :          0 :         srandom(seed);
      64                 :          0 : }
      65                 :            : 
      66                 :            : static int g_arc4random_initialized = 0;
      67                 :            : 
      68                 :            : static uint32_t
      69                 :          0 : arc4random(void)
      70                 :            : {
      71                 :            :         uint32_t r;
      72                 :            :         uint32_t r1, r2;
      73                 :            : 
      74         [ #  # ]:          0 :         if (!g_arc4random_initialized) {
      75                 :          0 :                 srandomdev();
      76                 :          0 :                 g_arc4random_initialized = 1;
      77                 :            :         }
      78                 :          0 :         r1 = (uint32_t)(random() & 0xffff);
      79                 :          0 :         r2 = (uint32_t)(random() & 0xffff);
      80                 :          0 :         r = (r1 << 16) | r2;
      81                 :          0 :         return r;
      82                 :            : }
      83                 :            : #endif /* SPDK_CONFIG_HAVE_ARC4RANDOM */
      84                 :            : 
      85                 :            : static void
      86                 :        688 : gen_random(uint8_t *buf, size_t len)
      87                 :            : {
      88                 :            :         uint32_t r;
      89                 :            :         size_t idx;
      90                 :            : 
      91         [ +  + ]:     353288 :         for (idx = 0; idx < len; idx++) {
      92                 :     352600 :                 r = arc4random();
      93                 :     352600 :                 buf[idx] = (uint8_t) r;
      94                 :            :         }
      95                 :        688 : }
      96                 :            : 
      97                 :            : static uint64_t
      98                 :       1679 : iscsi_get_isid(const uint8_t isid[6])
      99                 :            : {
     100                 :       1679 :         return (uint64_t)isid[0] << 40 |
     101                 :       1679 :                (uint64_t)isid[1] << 32 |
     102                 :       1679 :                (uint64_t)isid[2] << 24 |
     103                 :       1679 :                (uint64_t)isid[3] << 16 |
     104                 :       1683 :                (uint64_t)isid[4] << 8 |
     105                 :       1679 :                (uint64_t)isid[5];
     106                 :            : }
     107                 :            : 
     108                 :            : static int
     109                 :        688 : bin2hex(char *buf, size_t len, const uint8_t *data, size_t data_len)
     110                 :            : {
     111                 :        688 :         const char *digits = "0123456789ABCDEF";
     112                 :        688 :         size_t total = 0;
     113                 :            :         size_t idx;
     114                 :            : 
     115         [ -  + ]:        688 :         if (len < 3) {
     116                 :          0 :                 return -1;
     117                 :            :         }
     118                 :        688 :         buf[total] = '0';
     119                 :        688 :         total++;
     120                 :        688 :         buf[total] = 'x';
     121                 :        688 :         total++;
     122                 :        688 :         buf[total] = '\0';
     123                 :            : 
     124         [ +  + ]:     358448 :         for (idx = 0; idx < data_len; idx++) {
     125         [ -  + ]:     357760 :                 if (total + 3 > len) {
     126                 :          0 :                         buf[total] = '\0';
     127                 :          0 :                         return - 1;
     128                 :            :                 }
     129                 :     357760 :                 buf[total] = digits[(data[idx] >> 4) & 0x0fU];
     130                 :     357760 :                 total++;
     131                 :     357760 :                 buf[total] = digits[data[idx] & 0x0fU];
     132                 :     357760 :                 total++;
     133                 :            :         }
     134                 :        688 :         buf[total] = '\0';
     135                 :        688 :         return total;
     136                 :            : }
     137                 :            : 
     138                 :            : static int
     139                 :        344 : hex2bin(uint8_t *data, size_t data_len, const char *str)
     140                 :            : {
     141                 :        344 :         const char *digits = "0123456789ABCDEF";
     142                 :            :         const char *dp;
     143                 :            :         const char *p;
     144                 :        344 :         size_t total = 0;
     145                 :            :         int n0, n1;
     146                 :            : 
     147                 :        344 :         p = str;
     148   [ -  +  -  -  :        344 :         if (p[0] != '0' && (p[1] != 'x' && p[1] != 'X')) {
                   -  - ]
     149                 :          0 :                 return -1;
     150                 :            :         }
     151                 :        344 :         p += 2;
     152                 :            : 
     153   [ +  +  +  - ]:       5848 :         while (p[0] != '\0' && p[1] != '\0') {
     154         [ -  + ]:       5504 :                 if (total >= data_len) {
     155                 :          0 :                         return -1;
     156                 :            :                 }
     157         [ -  + ]:       5504 :                 dp = strchr(digits, toupper((int) p[0]));
     158         [ -  + ]:       5504 :                 if (dp == NULL) {
     159                 :          0 :                         return -1;
     160                 :            :                 }
     161                 :       5504 :                 n0 = (int)(dp - digits);
     162         [ -  + ]:       5504 :                 dp = strchr(digits, toupper((int) p[1]));
     163         [ -  + ]:       5504 :                 if (dp == NULL) {
     164                 :          0 :                         return -1;
     165                 :            :                 }
     166                 :       5504 :                 n1 = (int)(dp - digits);
     167                 :            : 
     168                 :       5504 :                 data[total] = (uint8_t)(((n0 & 0x0fU) << 4) | (n1 & 0x0fU));
     169                 :       5504 :                 total++;
     170                 :       5504 :                 p += 2;
     171                 :            :         }
     172                 :        344 :         return total;
     173                 :            : }
     174                 :            : 
     175                 :            : static int
     176                 :     214184 : iscsi_reject(struct spdk_iscsi_conn *conn, struct spdk_iscsi_pdu *pdu,
     177                 :            :              int reason)
     178                 :            : {
     179                 :            :         struct spdk_iscsi_pdu *rsp_pdu;
     180                 :            :         struct iscsi_bhs_reject *rsph;
     181                 :            :         uint8_t *data;
     182                 :            :         int total_ahs_len;
     183                 :            :         int data_len;
     184                 :            :         int alloc_len;
     185                 :            : 
     186                 :     214184 :         pdu->is_rejected = true;
     187                 :            : 
     188                 :     214184 :         total_ahs_len = pdu->bhs.total_ahs_len;
     189                 :     214184 :         data_len = 0;
     190                 :     214184 :         alloc_len = ISCSI_BHS_LEN + (4 * total_ahs_len);
     191                 :            : 
     192         [ -  + ]:     214184 :         if (conn->header_digest) {
     193                 :          0 :                 alloc_len += ISCSI_DIGEST_LEN;
     194                 :            :         }
     195                 :            : 
     196                 :     214184 :         data = calloc(1, alloc_len);
     197         [ -  + ]:     214184 :         if (!data) {
     198                 :          0 :                 SPDK_ERRLOG("calloc() failed for data segment\n");
     199                 :          0 :                 return -ENOMEM;
     200                 :            :         }
     201                 :            : 
     202   [ -  +  -  + ]:     214184 :         SPDK_DEBUGLOG(iscsi, "Reject PDU reason=%d\n", reason);
     203                 :            : 
     204         [ +  + ]:     214184 :         if (conn->sess != NULL) {
     205   [ -  +  -  + ]:     214178 :                 SPDK_DEBUGLOG(iscsi,
     206                 :            :                               "StatSN=%u, ExpCmdSN=%u, MaxCmdSN=%u\n",
     207                 :            :                               conn->StatSN, conn->sess->ExpCmdSN,
     208                 :            :                               conn->sess->MaxCmdSN);
     209                 :            :         } else {
     210   [ -  +  -  + ]:          6 :                 SPDK_DEBUGLOG(iscsi, "StatSN=%u\n", conn->StatSN);
     211                 :            :         }
     212                 :            : 
     213   [ -  +  -  + ]:     214184 :         memcpy(data, &pdu->bhs, ISCSI_BHS_LEN);
     214                 :     214184 :         data_len += ISCSI_BHS_LEN;
     215                 :            : 
     216         [ -  + ]:     214184 :         if (total_ahs_len != 0) {
     217                 :          0 :                 total_ahs_len = spdk_min((4 * total_ahs_len), ISCSI_AHS_LEN);
     218   [ #  #  #  # ]:          0 :                 memcpy(data + data_len, pdu->ahs, total_ahs_len);
     219                 :          0 :                 data_len += total_ahs_len;
     220                 :            :         }
     221                 :            : 
     222         [ -  + ]:     214184 :         if (conn->header_digest) {
     223                 :          0 :                 memcpy(data + data_len, pdu->header_digest, ISCSI_DIGEST_LEN);
     224                 :          0 :                 data_len += ISCSI_DIGEST_LEN;
     225                 :            :         }
     226                 :            : 
     227                 :     214184 :         rsp_pdu = iscsi_get_pdu(conn);
     228         [ -  + ]:     214184 :         if (rsp_pdu == NULL) {
     229                 :          0 :                 free(data);
     230                 :          0 :                 return -ENOMEM;
     231                 :            :         }
     232                 :            : 
     233                 :     214184 :         rsph = (struct iscsi_bhs_reject *)&rsp_pdu->bhs;
     234                 :     214184 :         rsp_pdu->data = data;
     235                 :     214184 :         rsph->opcode = ISCSI_OP_REJECT;
     236                 :     214184 :         rsph->flags |= 0x80; /* bit 0 is default to 1 */
     237                 :     214184 :         rsph->reason = reason;
     238                 :     214184 :         DSET24(rsph->data_segment_len, data_len);
     239                 :            : 
     240                 :     214184 :         rsph->ffffffff = 0xffffffffU;
     241                 :     214184 :         to_be32(&rsph->stat_sn, conn->StatSN);
     242                 :     214184 :         conn->StatSN++;
     243                 :            : 
     244         [ +  + ]:     214184 :         if (conn->sess != NULL) {
     245                 :     214178 :                 to_be32(&rsph->exp_cmd_sn, conn->sess->ExpCmdSN);
     246                 :     214178 :                 to_be32(&rsph->max_cmd_sn, conn->sess->MaxCmdSN);
     247                 :            :         } else {
     248                 :          6 :                 to_be32(&rsph->exp_cmd_sn, 1);
     249                 :          6 :                 to_be32(&rsph->max_cmd_sn, 1);
     250                 :            :         }
     251                 :            : 
     252   [ -  +  -  + ]:     214184 :         SPDK_LOGDUMP(iscsi, "PDU", (void *)&rsp_pdu->bhs, ISCSI_BHS_LEN);
     253                 :            : 
     254                 :     214184 :         iscsi_conn_write_pdu(conn, rsp_pdu, iscsi_conn_pdu_generic_complete, NULL);
     255                 :            : 
     256                 :     214184 :         return 0;
     257                 :            : }
     258                 :            : 
     259                 :            : uint32_t
     260                 :     371344 : iscsi_pdu_calc_header_digest(struct spdk_iscsi_pdu *pdu)
     261                 :            : {
     262                 :            :         uint32_t crc32c;
     263                 :     371344 :         uint32_t ahs_len_bytes = pdu->bhs.total_ahs_len * 4;
     264                 :            : 
     265                 :     371344 :         crc32c = SPDK_CRC32C_INITIAL;
     266                 :     371344 :         crc32c = spdk_crc32c_update(&pdu->bhs, ISCSI_BHS_LEN, crc32c);
     267                 :            : 
     268         [ -  + ]:     371344 :         if (ahs_len_bytes) {
     269                 :          0 :                 crc32c = spdk_crc32c_update(pdu->ahs, ahs_len_bytes, crc32c);
     270                 :            :         }
     271                 :            : 
     272                 :            :         /* BHS and AHS are always 4-byte multiples in length, so no padding is necessary. */
     273                 :            : 
     274                 :            :         /* Finalize CRC by inverting all bits. */
     275                 :     371344 :         return crc32c ^ SPDK_CRC32C_XOR;
     276                 :            : }
     277                 :            : 
     278                 :            : /* Calculate CRC for each partial data segment. */
     279                 :            : static void
     280                 :          6 : iscsi_pdu_calc_partial_data_digest(struct spdk_iscsi_pdu *pdu)
     281                 :            : {
     282                 :          5 :         struct iovec iov;
     283                 :            :         uint32_t num_blocks;
     284                 :            : 
     285   [ +  +  +  - ]:          6 :         if (spdk_likely(!pdu->dif_insert_or_strip)) {
     286                 :         10 :                 pdu->crc32c = spdk_crc32c_update(pdu->data,
     287                 :          6 :                                                  pdu->data_valid_bytes - pdu->data_offset,
     288                 :            :                                                  pdu->crc32c);
     289                 :            :         } else {
     290                 :          0 :                 iov.iov_base = pdu->data;
     291                 :          0 :                 iov.iov_len = pdu->data_buf_len;
     292         [ #  # ]:          0 :                 num_blocks = pdu->data_buf_len / pdu->dif_ctx.block_size;
     293                 :            : 
     294                 :          0 :                 spdk_dif_update_crc32c(&iov, 1, num_blocks, &pdu->crc32c, &pdu->dif_ctx);
     295                 :            :         }
     296                 :          6 : }
     297                 :            : 
     298                 :            : static uint32_t
     299                 :          6 : iscsi_pdu_calc_partial_data_digest_done(struct spdk_iscsi_pdu *pdu)
     300                 :            : {
     301                 :          6 :         uint32_t crc32c = pdu->crc32c;
     302                 :            :         uint32_t mod;
     303                 :            : 
     304                 :            :         /* Include padding bytes into CRC if any. */
     305                 :          6 :         mod = pdu->data_valid_bytes % ISCSI_ALIGNMENT;
     306         [ -  + ]:          6 :         if (mod != 0) {
     307                 :          0 :                 uint32_t pad_length = ISCSI_ALIGNMENT - mod;
     308                 :          0 :                 uint8_t pad[3] = {0, 0, 0};
     309                 :            : 
     310         [ #  # ]:          0 :                 assert(pad_length > 0);
     311         [ #  # ]:          0 :                 assert(pad_length <= sizeof(pad));
     312                 :          0 :                 crc32c = spdk_crc32c_update(pad, pad_length, crc32c);
     313                 :            :         }
     314                 :            : 
     315                 :            :         /* Finalize CRC by inverting all bits. */
     316                 :          6 :         return crc32c ^ SPDK_CRC32C_XOR;
     317                 :            : }
     318                 :            : 
     319                 :            : uint32_t
     320                 :          0 : iscsi_pdu_calc_data_digest(struct spdk_iscsi_pdu *pdu)
     321                 :            : {
     322                 :          0 :         uint32_t data_len = DGET24(pdu->bhs.data_segment_len);
     323                 :          0 :         uint32_t crc32c;
     324                 :            :         uint32_t mod;
     325                 :          0 :         struct iovec iov;
     326                 :            :         uint32_t num_blocks;
     327                 :            : 
     328                 :            :         /* Initialize CRC. */
     329                 :          0 :         crc32c = SPDK_CRC32C_INITIAL;
     330                 :            : 
     331                 :            :         /* Calculate CRC for the whole data segment. */
     332   [ #  #  #  # ]:          0 :         if (spdk_likely(!pdu->dif_insert_or_strip)) {
     333                 :          0 :                 crc32c = spdk_crc32c_update(pdu->data, data_len, crc32c);
     334                 :            :         } else {
     335                 :          0 :                 iov.iov_base = pdu->data;
     336                 :          0 :                 iov.iov_len = pdu->data_buf_len;
     337         [ #  # ]:          0 :                 num_blocks = pdu->data_buf_len / pdu->dif_ctx.block_size;
     338                 :            : 
     339                 :          0 :                 spdk_dif_update_crc32c(&iov, 1, num_blocks, &crc32c, &pdu->dif_ctx);
     340                 :            :         }
     341                 :            : 
     342                 :            :         /* Include padding bytes into CRC if any. */
     343                 :          0 :         mod = data_len % ISCSI_ALIGNMENT;
     344         [ #  # ]:          0 :         if (mod != 0) {
     345                 :          0 :                 uint32_t pad_length = ISCSI_ALIGNMENT - mod;
     346                 :          0 :                 uint8_t pad[3] = {0, 0, 0};
     347         [ #  # ]:          0 :                 assert(pad_length > 0);
     348         [ #  # ]:          0 :                 assert(pad_length <= sizeof(pad));
     349                 :          0 :                 crc32c = spdk_crc32c_update(pad, pad_length, crc32c);
     350                 :            :         }
     351                 :            : 
     352                 :            :         /* Finalize CRC by inverting all bits. */
     353                 :          0 :         return crc32c ^ SPDK_CRC32C_XOR;
     354                 :            : }
     355                 :            : 
     356                 :            : static int
     357                 :   14768371 : iscsi_conn_read_data_segment(struct spdk_iscsi_conn *conn,
     358                 :            :                              struct spdk_iscsi_pdu *pdu,
     359                 :            :                              uint32_t data_offset, uint32_t data_len)
     360                 :            : {
     361                 :         85 :         struct iovec buf_iov, iovs[32];
     362                 :            :         int rc, _rc;
     363                 :            : 
     364   [ +  +  +  - ]:   14768371 :         if (spdk_likely(!pdu->dif_insert_or_strip)) {
     365                 :   14768371 :                 return iscsi_conn_read_data(conn, data_len, pdu->data + data_offset);
     366                 :            :         } else {
     367                 :          0 :                 buf_iov.iov_base = pdu->data;
     368                 :          0 :                 buf_iov.iov_len = pdu->data_buf_len;
     369                 :          0 :                 rc = spdk_dif_set_md_interleave_iovs(iovs, 32, &buf_iov, 1,
     370                 :            :                                                      data_offset, data_len, NULL,
     371                 :          0 :                                                      &pdu->dif_ctx);
     372         [ #  # ]:          0 :                 if (rc > 0) {
     373                 :          0 :                         rc = iscsi_conn_readv_data(conn, iovs, rc);
     374         [ #  # ]:          0 :                         if (rc > 0) {
     375                 :          0 :                                 _rc = spdk_dif_generate_stream(&buf_iov, 1, data_offset, rc,
     376                 :            :                                                                &pdu->dif_ctx);
     377         [ #  # ]:          0 :                                 if (_rc != 0) {
     378                 :          0 :                                         SPDK_ERRLOG("DIF generate failed\n");
     379                 :          0 :                                         rc = _rc;
     380                 :            :                                 }
     381                 :            :                         }
     382                 :            :                 } else {
     383                 :          0 :                         SPDK_ERRLOG("Setup iovs for interleaved metadata failed\n");
     384                 :            :                 }
     385                 :          0 :                 return rc;
     386                 :            :         }
     387                 :            : }
     388                 :            : 
     389                 :            : /* Build iovec array to leave metadata space for every data block
     390                 :            :  * when reading data segment from socket.
     391                 :            :  */
     392                 :            : static inline bool
     393                 :         24 : _iscsi_sgl_append_with_md(struct spdk_iov_sgl *s,
     394                 :            :                           void *buf, uint32_t buf_len, uint32_t data_len,
     395                 :            :                           struct spdk_dif_ctx *dif_ctx)
     396                 :            : {
     397                 :            :         int rc;
     398                 :         24 :         uint32_t total_size = 0;
     399                 :         20 :         struct iovec buf_iov;
     400                 :            : 
     401         [ +  + ]:         24 :         if (s->iov_offset >= data_len) {
     402                 :          6 :                 s->iov_offset -= data_len;
     403                 :            :         } else {
     404                 :         18 :                 buf_iov.iov_base = buf;
     405                 :         18 :                 buf_iov.iov_len = buf_len;
     406                 :         18 :                 rc = spdk_dif_set_md_interleave_iovs(s->iov, s->iovcnt, &buf_iov, 1,
     407                 :         18 :                                                      s->iov_offset, data_len - s->iov_offset,
     408                 :            :                                                      &total_size, dif_ctx);
     409         [ -  + ]:         18 :                 if (rc < 0) {
     410                 :          0 :                         SPDK_ERRLOG("Failed to setup iovs for DIF strip\n");
     411                 :          0 :                         return false;
     412                 :            :                 }
     413                 :            : 
     414                 :         18 :                 s->total_size += total_size;
     415                 :         18 :                 s->iov_offset = 0;
     416         [ -  + ]:         18 :                 assert(s->iovcnt >= rc);
     417                 :         18 :                 s->iovcnt -= rc;
     418                 :         18 :                 s->iov += rc;
     419                 :            : 
     420         [ +  + ]:         18 :                 if (s->iovcnt == 0) {
     421                 :          6 :                         return false;
     422                 :            :                 }
     423                 :            :         }
     424                 :            : 
     425                 :         18 :         return true;
     426                 :            : }
     427                 :            : 
     428                 :            : int
     429                 :   24803467 : iscsi_build_iovs(struct spdk_iscsi_conn *conn, struct iovec *iovs, int iovcnt,
     430                 :            :                  struct spdk_iscsi_pdu *pdu, uint32_t *_mapped_length)
     431                 :            : {
     432                 :         80 :         struct spdk_iov_sgl sgl;
     433                 :            :         int enable_digest;
     434                 :            :         uint32_t total_ahs_len;
     435                 :            :         uint32_t data_len;
     436                 :            : 
     437         [ -  + ]:   24803467 :         if (iovcnt == 0) {
     438                 :          0 :                 return 0;
     439                 :            :         }
     440                 :            : 
     441                 :   24803467 :         total_ahs_len = pdu->bhs.total_ahs_len;
     442                 :   24803467 :         data_len = DGET24(pdu->bhs.data_segment_len);
     443                 :   24803467 :         data_len = ISCSI_ALIGN(data_len);
     444                 :            : 
     445                 :   24803467 :         enable_digest = 1;
     446         [ +  + ]:   24803467 :         if (pdu->bhs.opcode == ISCSI_OP_LOGIN_RSP) {
     447                 :            :                 /* this PDU should be sent without digest */
     448                 :       5420 :                 enable_digest = 0;
     449                 :            :         }
     450                 :            : 
     451                 :   24803467 :         spdk_iov_sgl_init(&sgl, iovs, iovcnt, pdu->writev_offset);
     452                 :            : 
     453                 :            :         /* BHS */
     454         [ +  + ]:   24803467 :         if (!spdk_iov_sgl_append(&sgl, (uint8_t *)&pdu->bhs, ISCSI_BHS_LEN)) {
     455                 :          6 :                 goto end;
     456                 :            :         }
     457                 :            :         /* AHS */
     458         [ -  + ]:   24803461 :         if (total_ahs_len > 0) {
     459         [ #  # ]:          0 :                 if (!spdk_iov_sgl_append(&sgl, pdu->ahs, 4 * total_ahs_len)) {
     460                 :          0 :                         goto end;
     461                 :            :                 }
     462                 :            :         }
     463                 :            : 
     464                 :            :         /* Header Digest */
     465   [ +  +  +  + ]:   24803461 :         if (enable_digest && conn->header_digest) {
     466         [ +  + ]:     185792 :                 if (!spdk_iov_sgl_append(&sgl, pdu->header_digest, ISCSI_DIGEST_LEN)) {
     467                 :          6 :                         goto end;
     468                 :            :                 }
     469                 :            :         }
     470                 :            : 
     471                 :            :         /* Data Segment */
     472         [ +  + ]:   24803455 :         if (data_len > 0) {
     473   [ +  +  +  + ]:   13097588 :                 if (!pdu->dif_insert_or_strip) {
     474         [ +  + ]:   13097564 :                         if (!spdk_iov_sgl_append(&sgl, pdu->data, data_len)) {
     475                 :          6 :                                 goto end;
     476                 :            :                         }
     477                 :            :                 } else {
     478         [ +  + ]:         24 :                         if (!_iscsi_sgl_append_with_md(&sgl, pdu->data, pdu->data_buf_len,
     479                 :            :                                                        data_len, &pdu->dif_ctx)) {
     480                 :          6 :                                 goto end;
     481                 :            :                         }
     482                 :            :                 }
     483                 :            :         }
     484                 :            : 
     485                 :            :         /* Data Digest */
     486   [ +  +  +  +  :   24803443 :         if (enable_digest && conn->data_digest && data_len != 0) {
                   -  + ]
     487                 :         72 :                 spdk_iov_sgl_append(&sgl, pdu->data_digest, ISCSI_DIGEST_LEN);
     488                 :            :         }
     489                 :            : 
     490                 :   24803451 : end:
     491         [ +  - ]:   24803467 :         if (_mapped_length != NULL) {
     492                 :   24803467 :                 *_mapped_length = sgl.total_size;
     493                 :            :         }
     494                 :            : 
     495                 :   24803467 :         return iovcnt - sgl.iovcnt;
     496                 :            : }
     497                 :            : 
     498                 :            : void
     499                 :        555 : iscsi_free_sess(struct spdk_iscsi_sess *sess)
     500                 :            : {
     501         [ -  + ]:        555 :         if (sess == NULL) {
     502                 :          0 :                 return;
     503                 :            :         }
     504                 :            : 
     505                 :        555 :         sess->tag = 0;
     506                 :        555 :         sess->target = NULL;
     507                 :        555 :         sess->session_type = SESSION_TYPE_INVALID;
     508                 :        555 :         iscsi_param_free(sess->params);
     509                 :        555 :         free(sess->conns);
     510                 :        555 :         spdk_scsi_port_free(&sess->initiator_port);
     511                 :        555 :         spdk_mempool_put(g_iscsi.session_pool, (void *)sess);
     512                 :            : }
     513                 :            : 
     514                 :            : static int
     515                 :        555 : create_iscsi_sess(struct spdk_iscsi_conn *conn,
     516                 :            :                   struct spdk_iscsi_tgt_node *target,
     517                 :            :                   enum session_type session_type)
     518                 :            : {
     519                 :            :         struct spdk_iscsi_sess *sess;
     520                 :            :         int rc;
     521                 :            : 
     522                 :        555 :         sess = spdk_mempool_get(g_iscsi.session_pool);
     523         [ -  + ]:        555 :         if (!sess) {
     524                 :          0 :                 SPDK_ERRLOG("Unable to get session object\n");
     525                 :          0 :                 SPDK_ERRLOG("MaxSessions set to %d\n", g_iscsi.MaxSessions);
     526                 :          0 :                 return -ENOMEM;
     527                 :            :         }
     528                 :            : 
     529                 :            :         /* configuration values */
     530         [ -  + ]:        555 :         pthread_mutex_lock(&g_iscsi.mutex);
     531                 :            : 
     532                 :        555 :         sess->MaxConnections = g_iscsi.MaxConnectionsPerSession;
     533                 :        555 :         sess->MaxOutstandingR2T = DEFAULT_MAXOUTSTANDINGR2T;
     534                 :            : 
     535                 :        555 :         sess->DefaultTime2Wait = g_iscsi.DefaultTime2Wait;
     536                 :        555 :         sess->DefaultTime2Retain = g_iscsi.DefaultTime2Retain;
     537                 :        555 :         sess->FirstBurstLength = g_iscsi.FirstBurstLength;
     538                 :        555 :         sess->MaxBurstLength = SPDK_ISCSI_MAX_BURST_LENGTH;
     539                 :        555 :         sess->InitialR2T = DEFAULT_INITIALR2T;
     540         [ -  + ]:        555 :         sess->ImmediateData = g_iscsi.ImmediateData;
     541                 :        555 :         sess->DataPDUInOrder = DEFAULT_DATAPDUINORDER;
     542                 :        555 :         sess->DataSequenceInOrder = DEFAULT_DATASEQUENCEINORDER;
     543                 :        555 :         sess->ErrorRecoveryLevel = g_iscsi.ErrorRecoveryLevel;
     544                 :            : 
     545         [ -  + ]:        555 :         pthread_mutex_unlock(&g_iscsi.mutex);
     546                 :            : 
     547                 :        555 :         sess->tag = conn->pg_tag;
     548                 :            : 
     549                 :        555 :         sess->conns = calloc(sess->MaxConnections, sizeof(*sess->conns));
     550         [ -  + ]:        555 :         if (!sess->conns) {
     551                 :          0 :                 spdk_mempool_put(g_iscsi.session_pool, (void *)sess);
     552                 :          0 :                 SPDK_ERRLOG("calloc() failed for connection array\n");
     553                 :          0 :                 return -ENOMEM;
     554                 :            :         }
     555                 :            : 
     556                 :        555 :         sess->connections = 0;
     557                 :            : 
     558                 :        555 :         sess->conns[sess->connections] = conn;
     559                 :        555 :         sess->connections++;
     560                 :            : 
     561                 :        555 :         sess->params = NULL;
     562                 :        555 :         sess->target = target;
     563                 :        555 :         sess->isid = 0;
     564                 :        555 :         sess->session_type = session_type;
     565                 :        555 :         sess->current_text_itt = 0xffffffffU;
     566                 :            : 
     567                 :            :         /* set default params */
     568                 :        555 :         rc = iscsi_sess_params_init(&sess->params);
     569         [ -  + ]:        555 :         if (rc < 0) {
     570                 :          0 :                 SPDK_ERRLOG("iscsi_sess_params_init() failed\n");
     571                 :          0 :                 goto error_return;
     572                 :            :         }
     573                 :            :         /* replace with config value */
     574                 :        555 :         rc = iscsi_param_set_int(sess->params, "MaxConnections",
     575                 :            :                                  sess->MaxConnections);
     576         [ -  + ]:        555 :         if (rc < 0) {
     577                 :          0 :                 SPDK_ERRLOG("iscsi_param_set_int() failed\n");
     578                 :          0 :                 goto error_return;
     579                 :            :         }
     580                 :            : 
     581                 :        555 :         rc = iscsi_param_set_int(sess->params, "MaxOutstandingR2T",
     582                 :            :                                  sess->MaxOutstandingR2T);
     583         [ -  + ]:        555 :         if (rc < 0) {
     584                 :          0 :                 SPDK_ERRLOG("iscsi_param_set_int() failed\n");
     585                 :          0 :                 goto error_return;
     586                 :            :         }
     587                 :            : 
     588                 :        555 :         rc = iscsi_param_set_int(sess->params, "DefaultTime2Wait",
     589                 :            :                                  sess->DefaultTime2Wait);
     590         [ -  + ]:        555 :         if (rc < 0) {
     591                 :          0 :                 SPDK_ERRLOG("iscsi_param_set_int() failed\n");
     592                 :          0 :                 goto error_return;
     593                 :            :         }
     594                 :            : 
     595                 :        555 :         rc = iscsi_param_set_int(sess->params, "DefaultTime2Retain",
     596                 :            :                                  sess->DefaultTime2Retain);
     597         [ -  + ]:        555 :         if (rc < 0) {
     598                 :          0 :                 SPDK_ERRLOG("iscsi_param_set_int() failed\n");
     599                 :          0 :                 goto error_return;
     600                 :            :         }
     601                 :            : 
     602                 :        555 :         rc = iscsi_param_set_int(sess->params, "FirstBurstLength",
     603                 :            :                                  sess->FirstBurstLength);
     604         [ -  + ]:        555 :         if (rc < 0) {
     605                 :          0 :                 SPDK_ERRLOG("iscsi_param_set_int() failed\n");
     606                 :          0 :                 goto error_return;
     607                 :            :         }
     608                 :            : 
     609                 :        555 :         rc = iscsi_param_set_int(sess->params, "MaxBurstLength",
     610                 :            :                                  sess->MaxBurstLength);
     611         [ -  + ]:        555 :         if (rc < 0) {
     612                 :          0 :                 SPDK_ERRLOG("iscsi_param_set_int() failed\n");
     613                 :          0 :                 goto error_return;
     614                 :            :         }
     615                 :            : 
     616                 :        555 :         rc = iscsi_param_set(sess->params, "InitialR2T",
     617   [ -  +  +  - ]:        555 :                              sess->InitialR2T ? "Yes" : "No");
     618         [ -  + ]:        555 :         if (rc < 0) {
     619                 :          0 :                 SPDK_ERRLOG("iscsi_param_set() failed\n");
     620                 :          0 :                 goto error_return;
     621                 :            :         }
     622                 :            : 
     623                 :        555 :         rc = iscsi_param_set(sess->params, "ImmediateData",
     624   [ -  +  +  - ]:        555 :                              sess->ImmediateData ? "Yes" : "No");
     625         [ -  + ]:        555 :         if (rc < 0) {
     626                 :          0 :                 SPDK_ERRLOG("iscsi_param_set() failed\n");
     627                 :          0 :                 goto error_return;
     628                 :            :         }
     629                 :            : 
     630                 :        555 :         rc = iscsi_param_set(sess->params, "DataPDUInOrder",
     631   [ -  +  +  - ]:        555 :                              sess->DataPDUInOrder ? "Yes" : "No");
     632         [ -  + ]:        555 :         if (rc < 0) {
     633                 :          0 :                 SPDK_ERRLOG("iscsi_param_set() failed\n");
     634                 :          0 :                 goto error_return;
     635                 :            :         }
     636                 :            : 
     637                 :        555 :         rc = iscsi_param_set(sess->params, "DataSequenceInOrder",
     638   [ -  +  +  - ]:        555 :                              sess->DataSequenceInOrder ? "Yes" : "No");
     639         [ -  + ]:        555 :         if (rc < 0) {
     640                 :          0 :                 SPDK_ERRLOG("iscsi_param_set() failed\n");
     641                 :          0 :                 goto error_return;
     642                 :            :         }
     643                 :            : 
     644                 :        555 :         rc = iscsi_param_set_int(sess->params, "ErrorRecoveryLevel",
     645                 :            :                                  sess->ErrorRecoveryLevel);
     646         [ -  + ]:        555 :         if (rc < 0) {
     647                 :          0 :                 SPDK_ERRLOG("iscsi_param_set_int() failed\n");
     648                 :          0 :                 goto error_return;
     649                 :            :         }
     650                 :            : 
     651                 :            :         /* realloc buffer */
     652                 :        555 :         rc = iscsi_param_set_int(conn->params, "MaxRecvDataSegmentLength",
     653                 :        555 :                                  conn->MaxRecvDataSegmentLength);
     654         [ -  + ]:        555 :         if (rc < 0) {
     655                 :          0 :                 SPDK_ERRLOG("iscsi_param_set_int() failed\n");
     656                 :          0 :                 goto error_return;
     657                 :            :         }
     658                 :            : 
     659                 :            :         /* sess for first connection of session */
     660                 :        555 :         conn->sess = sess;
     661                 :        555 :         return 0;
     662                 :            : 
     663                 :          0 : error_return:
     664                 :          0 :         iscsi_free_sess(sess);
     665                 :          0 :         conn->sess = NULL;
     666                 :          0 :         return -1;
     667                 :            : }
     668                 :            : 
     669                 :            : static struct spdk_iscsi_sess *
     670                 :         12 : get_iscsi_sess_by_tsih(uint16_t tsih)
     671                 :            : {
     672                 :            :         struct spdk_iscsi_sess *session;
     673                 :            : 
     674   [ +  -  +  + ]:         12 :         if (tsih == 0 || tsih > g_iscsi.MaxSessions) {
     675                 :          6 :                 return NULL;
     676                 :            :         }
     677                 :            : 
     678                 :          6 :         session = g_iscsi.session[tsih - 1];
     679         [ -  + ]:          6 :         assert(tsih == session->tsih);
     680                 :            : 
     681                 :          6 :         return session;
     682                 :            : }
     683                 :            : 
     684                 :            : static uint8_t
     685                 :         12 : append_iscsi_sess(struct spdk_iscsi_conn *conn,
     686                 :            :                   const char *initiator_port_name, uint16_t tsih, uint16_t cid)
     687                 :            : {
     688                 :            :         struct spdk_iscsi_sess *sess;
     689                 :            : 
     690   [ -  +  -  + ]:         12 :         SPDK_DEBUGLOG(iscsi, "append session: init port name=%s, tsih=%u, cid=%u\n",
     691                 :            :                       initiator_port_name, tsih, cid);
     692                 :            : 
     693                 :         12 :         sess = get_iscsi_sess_by_tsih(tsih);
     694         [ +  + ]:         12 :         if (sess == NULL) {
     695                 :          6 :                 SPDK_ERRLOG("spdk_get_iscsi_sess_by_tsih failed\n");
     696                 :          6 :                 return ISCSI_LOGIN_CONN_ADD_FAIL;
     697                 :            :         }
     698         [ -  + ]:          6 :         if ((conn->pg_tag != sess->tag) ||
     699   [ #  #  #  #  :          0 :             (strcasecmp(initiator_port_name, spdk_scsi_port_get_name(sess->initiator_port)) != 0) ||
                   #  # ]
     700         [ #  # ]:          0 :             (conn->target != sess->target)) {
     701                 :            :                 /* no match */
     702                 :          6 :                 SPDK_ERRLOG("no MCS session for init port name=%s, tsih=%d, cid=%d\n",
     703                 :            :                             initiator_port_name, tsih, cid);
     704                 :          6 :                 return ISCSI_LOGIN_CONN_ADD_FAIL;
     705                 :            :         }
     706                 :            : 
     707         [ #  # ]:          0 :         if (sess->connections >= sess->MaxConnections) {
     708                 :            :                 /* no slot for connection */
     709                 :          0 :                 SPDK_ERRLOG("too many connections for init port name=%s, tsih=%d, cid=%d\n",
     710                 :            :                             initiator_port_name, tsih, cid);
     711                 :          0 :                 return ISCSI_LOGIN_TOO_MANY_CONNECTIONS;
     712                 :            :         }
     713                 :            : 
     714   [ #  #  #  # ]:          0 :         SPDK_DEBUGLOG(iscsi, "Connections (tsih %d): %d\n", sess->tsih, sess->connections);
     715                 :          0 :         conn->sess = sess;
     716                 :            : 
     717                 :            :         /*
     718                 :            :          * TODO: need a mutex or other sync mechanism to protect the session's
     719                 :            :          *  connection list.
     720                 :            :          */
     721                 :          0 :         sess->conns[sess->connections] = conn;
     722                 :          0 :         sess->connections++;
     723                 :            : 
     724                 :          0 :         return 0;
     725                 :            : }
     726                 :            : 
     727                 :            : static int
     728                 :       2122 : iscsi_append_text(const char *key, const char *val, uint8_t *data,
     729                 :            :                   int alloc_len, int data_len)
     730                 :            : {
     731                 :            :         int total;
     732                 :            :         int len;
     733                 :            : 
     734                 :       2122 :         total = data_len;
     735         [ -  + ]:       2122 :         if (alloc_len < 1) {
     736                 :          0 :                 return 0;
     737                 :            :         }
     738         [ -  + ]:       2122 :         if (total > alloc_len) {
     739                 :          0 :                 total = alloc_len;
     740                 :          0 :                 data[total - 1] = '\0';
     741                 :          0 :                 return total;
     742                 :            :         }
     743                 :            : 
     744         [ -  + ]:       2122 :         if (alloc_len - total < 1) {
     745                 :          0 :                 SPDK_ERRLOG("data space small %d\n", alloc_len);
     746                 :          0 :                 return total;
     747                 :            :         }
     748         [ -  + ]:       2122 :         len = snprintf((char *) data + total, alloc_len - total, "%s=%s", key, val);
     749                 :       2122 :         total += len + 1;
     750                 :            : 
     751                 :       2122 :         return total;
     752                 :            : }
     753                 :            : 
     754                 :            : static int
     755                 :       1048 : iscsi_append_param(struct spdk_iscsi_conn *conn, const char *key,
     756                 :            :                    uint8_t *data, int alloc_len, int data_len)
     757                 :            : {
     758                 :            :         struct iscsi_param *param;
     759                 :            : 
     760                 :       1048 :         param = iscsi_param_find(conn->params, key);
     761         [ +  - ]:       1048 :         if (param == NULL) {
     762                 :       1048 :                 param = iscsi_param_find(conn->sess->params, key);
     763         [ -  + ]:       1048 :                 if (param == NULL) {
     764   [ #  #  #  # ]:          0 :                         SPDK_DEBUGLOG(iscsi, "no key %.64s\n", key);
     765                 :          0 :                         return data_len;
     766                 :            :                 }
     767                 :            :         }
     768                 :       1048 :         return iscsi_append_text(param->key, param->val, data,
     769                 :            :                                  alloc_len, data_len);
     770                 :            : }
     771                 :            : 
     772                 :            : static int
     773                 :       1052 : iscsi_auth_params(struct spdk_iscsi_conn *conn,
     774                 :            :                   struct iscsi_param *params, const char *method, uint8_t *data,
     775                 :            :                   int alloc_len, int data_len)
     776                 :            : {
     777                 :            :         char *in_val;
     778                 :          0 :         char *in_next;
     779                 :            :         char *new_val;
     780                 :            :         const char *algorithm;
     781                 :            :         const char *name;
     782                 :            :         const char *response;
     783                 :            :         const char *identifier;
     784                 :            :         const char *challenge;
     785                 :            :         int total;
     786                 :            :         int rc;
     787                 :            : 
     788   [ +  -  +  -  :       1052 :         if (conn == NULL || params == NULL || method == NULL) {
                   -  + ]
     789                 :          0 :                 return -1;
     790                 :            :         }
     791   [ -  +  -  + ]:       1052 :         if (strcasecmp(method, "CHAP") == 0) {
     792                 :            :                 /* method OK */
     793                 :            :         } else {
     794                 :          0 :                 SPDK_ERRLOG("unsupported AuthMethod %.64s\n", method);
     795                 :          0 :                 return -1;
     796                 :            :         }
     797                 :            : 
     798                 :       1052 :         total = data_len;
     799         [ -  + ]:       1052 :         if (alloc_len < 1) {
     800                 :          0 :                 return 0;
     801                 :            :         }
     802         [ -  + ]:       1052 :         if (total > alloc_len) {
     803                 :          0 :                 total = alloc_len;
     804                 :          0 :                 data[total - 1] = '\0';
     805                 :          0 :                 return total;
     806                 :            :         }
     807                 :            : 
     808                 :            :         /* for temporary store */
     809                 :       1052 :         in_val = malloc(ISCSI_TEXT_MAX_VAL_LEN + 1);
     810         [ -  + ]:       1052 :         if (!in_val) {
     811                 :          0 :                 SPDK_ERRLOG("malloc() failed for temporary store\n");
     812                 :          0 :                 return -ENOMEM;
     813                 :            :         }
     814                 :            : 
     815                 :            :         /* CHAP method (RFC1994) */
     816         [ +  + ]:       1052 :         if ((algorithm = iscsi_param_get_val(params, "CHAP_A")) != NULL) {
     817         [ -  + ]:        344 :                 if (conn->auth.chap_phase != ISCSI_CHAP_PHASE_WAIT_A) {
     818                 :          0 :                         SPDK_ERRLOG("CHAP sequence error\n");
     819                 :          0 :                         goto error_return;
     820                 :            :                 }
     821                 :            : 
     822                 :            :                 /* CHAP_A is LIST type */
     823                 :        344 :                 snprintf(in_val, ISCSI_TEXT_MAX_VAL_LEN + 1, "%s", algorithm);
     824                 :        344 :                 in_next = in_val;
     825         [ +  - ]:        344 :                 while ((new_val = spdk_strsepq(&in_next, ",")) != NULL) {
     826   [ -  +  +  - ]:        344 :                         if (strcasecmp(new_val, "5") == 0) {
     827                 :            :                                 /* CHAP with MD5 */
     828                 :        344 :                                 break;
     829                 :            :                         }
     830                 :            :                 }
     831         [ -  + ]:        344 :                 if (new_val == NULL) {
     832                 :          0 :                         snprintf(in_val, ISCSI_TEXT_MAX_VAL_LEN + 1, "%s", "Reject");
     833                 :          0 :                         new_val = in_val;
     834                 :          0 :                         iscsi_append_text("CHAP_A", new_val, data, alloc_len, total);
     835                 :          0 :                         goto error_return;
     836                 :            :                 }
     837                 :            :                 /* selected algorithm is 5 (MD5) */
     838   [ -  +  -  + ]:        344 :                 SPDK_DEBUGLOG(iscsi, "got CHAP_A=%s\n", new_val);
     839                 :        344 :                 total = iscsi_append_text("CHAP_A", new_val, data, alloc_len, total);
     840                 :            : 
     841                 :            :                 /* Identifier is one octet */
     842                 :        344 :                 gen_random(conn->auth.chap_id, 1);
     843                 :        344 :                 snprintf(in_val, ISCSI_TEXT_MAX_VAL_LEN, "%d",
     844                 :        344 :                          (int) conn->auth.chap_id[0]);
     845                 :        344 :                 total = iscsi_append_text("CHAP_I", in_val, data, alloc_len, total);
     846                 :            : 
     847                 :            :                 /* Challenge Value is a variable stream of octets */
     848                 :            :                 /* (binary length MUST not exceed 1024 bytes) */
     849                 :        344 :                 conn->auth.chap_challenge_len = ISCSI_CHAP_CHALLENGE_LEN;
     850                 :        344 :                 gen_random(conn->auth.chap_challenge, conn->auth.chap_challenge_len);
     851                 :        688 :                 bin2hex(in_val, ISCSI_TEXT_MAX_VAL_LEN,
     852                 :        344 :                         conn->auth.chap_challenge, conn->auth.chap_challenge_len);
     853                 :        344 :                 total = iscsi_append_text("CHAP_C", in_val, data, alloc_len, total);
     854                 :            : 
     855                 :        344 :                 conn->auth.chap_phase = ISCSI_CHAP_PHASE_WAIT_NR;
     856         [ +  + ]:        708 :         } else if ((name = iscsi_param_get_val(params, "CHAP_N")) != NULL) {
     857                 :          0 :                 uint8_t resmd5[SPDK_MD5DIGEST_LEN];
     858                 :          0 :                 uint8_t tgtmd5[SPDK_MD5DIGEST_LEN];
     859                 :          0 :                 struct spdk_md5ctx md5ctx;
     860                 :        344 :                 size_t decoded_len = 0;
     861                 :            : 
     862         [ -  + ]:        344 :                 if (conn->auth.chap_phase != ISCSI_CHAP_PHASE_WAIT_NR) {
     863                 :          0 :                         SPDK_ERRLOG("CHAP sequence error\n");
     864                 :          0 :                         goto error_return;
     865                 :            :                 }
     866                 :            : 
     867                 :        344 :                 response = iscsi_param_get_val(params, "CHAP_R");
     868         [ -  + ]:        344 :                 if (response == NULL) {
     869                 :          0 :                         SPDK_ERRLOG("no response\n");
     870                 :          0 :                         goto error_return;
     871                 :            :                 }
     872         [ +  - ]:        344 :                 if (response[0] == '0' &&
     873   [ -  +  -  - ]:        344 :                     (response[1] == 'x' || response[1] == 'X')) {
     874                 :        344 :                         rc = hex2bin(resmd5, SPDK_MD5DIGEST_LEN, response);
     875   [ +  -  -  + ]:        344 :                         if (rc < 0 || rc != SPDK_MD5DIGEST_LEN) {
     876                 :          0 :                                 SPDK_ERRLOG("response format error\n");
     877                 :          0 :                                 goto error_return;
     878                 :            :                         }
     879         [ #  # ]:          0 :                 } else if (response[0] == '0' &&
     880   [ #  #  #  # ]:          0 :                            (response[1] == 'b' || response[1] == 'B')) {
     881                 :          0 :                         response += 2;
     882                 :          0 :                         rc = spdk_base64_decode(resmd5, &decoded_len, response);
     883   [ #  #  #  # ]:          0 :                         if (rc < 0 || decoded_len != SPDK_MD5DIGEST_LEN) {
     884                 :          0 :                                 SPDK_ERRLOG("response format error\n");
     885                 :          0 :                                 goto error_return;
     886                 :            :                         }
     887                 :            :                 } else {
     888                 :          0 :                         SPDK_ERRLOG("response format error\n");
     889                 :          0 :                         goto error_return;
     890                 :            :                 }
     891   [ -  +  -  + ]:        344 :                 SPDK_DEBUGLOG(iscsi, "got CHAP_N/CHAP_R\n");
     892                 :            : 
     893   [ -  +  -  + ]:        344 :                 SPDK_DEBUGLOG(iscsi, "ag_tag=%d\n", conn->chap_group);
     894                 :            : 
     895                 :        344 :                 rc = iscsi_chap_get_authinfo(&conn->auth, name, conn->chap_group);
     896         [ -  + ]:        344 :                 if (rc < 0) {
     897                 :            :                         /* SPDK_ERRLOG("auth user or secret is missing\n"); */
     898                 :          0 :                         SPDK_ERRLOG("iscsi_chap_get_authinfo() failed\n");
     899                 :          0 :                         goto error_return;
     900                 :            :                 }
     901   [ +  -  -  + ]:        344 :                 if (conn->auth.user[0] == '\0' || conn->auth.secret[0] == '\0') {
     902                 :            :                         /* SPDK_ERRLOG("auth user or secret is missing\n"); */
     903                 :          0 :                         SPDK_ERRLOG("auth failed (name %.64s)\n", name);
     904                 :          0 :                         goto error_return;
     905                 :            :                 }
     906                 :            : 
     907                 :        344 :                 md5init(&md5ctx);
     908                 :            :                 /* Identifier */
     909                 :        344 :                 md5update(&md5ctx, conn->auth.chap_id, 1);
     910                 :            :                 /* followed by secret */
     911                 :        344 :                 md5update(&md5ctx, conn->auth.secret,
     912         [ -  + ]:        344 :                           strlen(conn->auth.secret));
     913                 :            :                 /* followed by Challenge Value */
     914                 :        344 :                 md5update(&md5ctx, conn->auth.chap_challenge,
     915                 :        344 :                           conn->auth.chap_challenge_len);
     916                 :            :                 /* tgtmd5 is expecting Response Value */
     917                 :        344 :                 md5final(tgtmd5, &md5ctx);
     918                 :            : 
     919                 :        344 :                 bin2hex(in_val, ISCSI_TEXT_MAX_VAL_LEN, tgtmd5, SPDK_MD5DIGEST_LEN);
     920                 :            : 
     921                 :            : #if 0
     922                 :            :                 SPDK_DEBUGLOG(iscsi, "tgtmd5=%s, resmd5=%s\n", in_val, response);
     923                 :            :                 spdk_dump("tgtmd5", tgtmd5, SPDK_MD5DIGEST_LEN);
     924                 :            :                 spdk_dump("resmd5", resmd5, SPDK_MD5DIGEST_LEN);
     925                 :            : #endif
     926                 :            : 
     927                 :            :                 /* compare MD5 digest */
     928         [ -  + ]:        344 :                 if (memcmp(tgtmd5, resmd5, SPDK_MD5DIGEST_LEN) != 0) {
     929                 :            :                         /* not match */
     930                 :            :                         /* SPDK_ERRLOG("auth user or secret is missing\n"); */
     931                 :          0 :                         SPDK_ERRLOG("auth failed (name %.64s)\n", name);
     932                 :          0 :                         goto error_return;
     933                 :            :                 }
     934                 :            :                 /* OK initiator's secret */
     935                 :        344 :                 conn->authenticated = true;
     936                 :            : 
     937                 :            :                 /* mutual CHAP? */
     938                 :        344 :                 identifier = iscsi_param_get_val(params, "CHAP_I");
     939         [ -  + ]:        344 :                 if (identifier != NULL) {
     940         [ #  # ]:          0 :                         conn->auth.chap_mid[0] = (uint8_t) strtol(identifier, NULL, 10);
     941                 :          0 :                         challenge = iscsi_param_get_val(params, "CHAP_C");
     942         [ #  # ]:          0 :                         if (challenge == NULL) {
     943                 :          0 :                                 SPDK_ERRLOG("CHAP sequence error\n");
     944                 :          0 :                                 goto error_return;
     945                 :            :                         }
     946         [ #  # ]:          0 :                         if (challenge[0] == '0' &&
     947   [ #  #  #  # ]:          0 :                             (challenge[1] == 'x' || challenge[1] == 'X')) {
     948                 :          0 :                                 rc = hex2bin(conn->auth.chap_mchallenge,
     949                 :            :                                              ISCSI_CHAP_CHALLENGE_LEN, challenge);
     950         [ #  # ]:          0 :                                 if (rc < 0) {
     951                 :          0 :                                         SPDK_ERRLOG("challenge format error\n");
     952                 :          0 :                                         goto error_return;
     953                 :            :                                 }
     954                 :          0 :                                 conn->auth.chap_mchallenge_len = rc;
     955         [ #  # ]:          0 :                         } else if (challenge[0] == '0' &&
     956   [ #  #  #  # ]:          0 :                                    (challenge[1] == 'b' || challenge[1] == 'B')) {
     957                 :          0 :                                 challenge += 2;
     958                 :          0 :                                 rc = spdk_base64_decode(conn->auth.chap_mchallenge,
     959                 :            :                                                         &decoded_len, challenge);
     960         [ #  # ]:          0 :                                 if (rc < 0) {
     961                 :          0 :                                         SPDK_ERRLOG("challenge format error\n");
     962                 :          0 :                                         goto error_return;
     963                 :            :                                 }
     964                 :          0 :                                 conn->auth.chap_mchallenge_len = decoded_len;
     965                 :            :                         } else {
     966                 :          0 :                                 SPDK_ERRLOG("challenge format error\n");
     967                 :          0 :                                 goto error_return;
     968                 :            :                         }
     969                 :            : #if 0
     970                 :            :                         spdk_dump("MChallenge", conn->auth.chap_mchallenge,
     971                 :            :                                   conn->auth.chap_mchallenge_len);
     972                 :            : #endif
     973   [ #  #  #  # ]:          0 :                         SPDK_DEBUGLOG(iscsi, "got CHAP_I/CHAP_C\n");
     974                 :            : 
     975   [ #  #  #  # ]:          0 :                         if (conn->auth.muser[0] == '\0' || conn->auth.msecret[0] == '\0') {
     976                 :            :                                 /* SPDK_ERRLOG("mutual auth user or secret is missing\n"); */
     977                 :          0 :                                 SPDK_ERRLOG("auth failed (name %.64s)\n", name);
     978                 :          0 :                                 goto error_return;
     979                 :            :                         }
     980                 :            : 
     981                 :          0 :                         md5init(&md5ctx);
     982                 :            :                         /* Identifier */
     983                 :          0 :                         md5update(&md5ctx, conn->auth.chap_mid, 1);
     984                 :            :                         /* followed by secret */
     985                 :          0 :                         md5update(&md5ctx, conn->auth.msecret,
     986         [ #  # ]:          0 :                                   strlen(conn->auth.msecret));
     987                 :            :                         /* followed by Challenge Value */
     988                 :          0 :                         md5update(&md5ctx, conn->auth.chap_mchallenge,
     989                 :          0 :                                   conn->auth.chap_mchallenge_len);
     990                 :            :                         /* tgtmd5 is Response Value */
     991                 :          0 :                         md5final(tgtmd5, &md5ctx);
     992                 :            : 
     993                 :          0 :                         bin2hex(in_val, ISCSI_TEXT_MAX_VAL_LEN, tgtmd5, SPDK_MD5DIGEST_LEN);
     994                 :            : 
     995                 :          0 :                         total = iscsi_append_text("CHAP_N", conn->auth.muser, data,
     996                 :            :                                                   alloc_len, total);
     997                 :          0 :                         total = iscsi_append_text("CHAP_R", in_val, data, alloc_len, total);
     998                 :            :                 } else {
     999                 :            :                         /* not mutual */
    1000   [ -  +  -  + ]:        344 :                         if (conn->mutual_chap) {
    1001                 :          0 :                                 SPDK_ERRLOG("required mutual CHAP\n");
    1002                 :          0 :                                 goto error_return;
    1003                 :            :                         }
    1004                 :            :                 }
    1005                 :            : 
    1006                 :        344 :                 conn->auth.chap_phase = ISCSI_CHAP_PHASE_END;
    1007                 :            :         } else {
    1008                 :            :                 /* not found CHAP keys */
    1009   [ -  +  -  + ]:        364 :                 SPDK_DEBUGLOG(iscsi, "start CHAP\n");
    1010                 :        364 :                 conn->auth.chap_phase = ISCSI_CHAP_PHASE_WAIT_A;
    1011                 :            :         }
    1012                 :            : 
    1013                 :       1052 :         free(in_val);
    1014                 :       1052 :         return total;
    1015                 :            : 
    1016                 :          0 : error_return:
    1017                 :          0 :         conn->auth.chap_phase = ISCSI_CHAP_PHASE_WAIT_A;
    1018                 :          0 :         free(in_val);
    1019                 :          0 :         return -1;
    1020                 :            : }
    1021                 :            : 
    1022                 :            : static int
    1023                 :       4334 : iscsi_check_values(struct spdk_iscsi_conn *conn)
    1024                 :            : {
    1025         [ -  + ]:       4334 :         if (conn->sess->FirstBurstLength > conn->sess->MaxBurstLength) {
    1026                 :          0 :                 SPDK_ERRLOG("FirstBurstLength(%d) > MaxBurstLength(%d)\n",
    1027                 :            :                             conn->sess->FirstBurstLength,
    1028                 :            :                             conn->sess->MaxBurstLength);
    1029                 :          0 :                 return -1;
    1030                 :            :         }
    1031         [ -  + ]:       4334 :         if (conn->sess->FirstBurstLength > g_iscsi.FirstBurstLength) {
    1032                 :          0 :                 SPDK_ERRLOG("FirstBurstLength(%d) > iSCSI target restriction(%d)\n",
    1033                 :            :                             conn->sess->FirstBurstLength, g_iscsi.FirstBurstLength);
    1034                 :          0 :                 return -1;
    1035                 :            :         }
    1036         [ -  + ]:       4334 :         if (conn->sess->MaxBurstLength > 0x00ffffff) {
    1037                 :          0 :                 SPDK_ERRLOG("MaxBurstLength(%d) > 0x00ffffff\n",
    1038                 :            :                             conn->sess->MaxBurstLength);
    1039                 :          0 :                 return -1;
    1040                 :            :         }
    1041                 :            : 
    1042         [ -  + ]:       4334 :         if (conn->MaxRecvDataSegmentLength < 512) {
    1043                 :          0 :                 SPDK_ERRLOG("MaxRecvDataSegmentLength(%d) < 512\n",
    1044                 :            :                             conn->MaxRecvDataSegmentLength);
    1045                 :          0 :                 return -1;
    1046                 :            :         }
    1047         [ -  + ]:       4334 :         if (conn->MaxRecvDataSegmentLength > 0x00ffffff) {
    1048                 :          0 :                 SPDK_ERRLOG("MaxRecvDataSegmentLength(%d) > 0x00ffffff\n",
    1049                 :            :                             conn->MaxRecvDataSegmentLength);
    1050                 :          0 :                 return -1;
    1051                 :            :         }
    1052                 :       4334 :         return 0;
    1053                 :            : }
    1054                 :            : 
    1055                 :            : static int
    1056                 :       4334 : iscsi_conn_params_update(struct spdk_iscsi_conn *conn)
    1057                 :            : {
    1058                 :            :         int rc;
    1059                 :            :         uint32_t recv_buf_size;
    1060                 :            : 
    1061                 :            :         /* update internal variables */
    1062                 :       4334 :         rc = iscsi_copy_param2var(conn);
    1063         [ -  + ]:       4334 :         if (rc < 0) {
    1064                 :          0 :                 SPDK_ERRLOG("iscsi_copy_param2var() failed\n");
    1065         [ #  # ]:          0 :                 if (conn->state < ISCSI_CONN_STATE_EXITING) {
    1066                 :          0 :                         conn->state = ISCSI_CONN_STATE_EXITING;
    1067                 :            :                 }
    1068                 :          0 :                 return rc;
    1069                 :            :         }
    1070                 :            : 
    1071                 :            :         /* check value */
    1072                 :       4334 :         rc = iscsi_check_values(conn);
    1073         [ -  + ]:       4334 :         if (rc < 0) {
    1074                 :          0 :                 SPDK_ERRLOG("iscsi_check_values() failed\n");
    1075         [ #  # ]:          0 :                 if (conn->state < ISCSI_CONN_STATE_EXITING) {
    1076                 :          0 :                         conn->state = ISCSI_CONN_STATE_EXITING;
    1077                 :            :                 }
    1078                 :            :         }
    1079                 :            : 
    1080         [ -  + ]:       4334 :         if (conn->sock == NULL) {
    1081   [ #  #  #  # ]:          0 :                 SPDK_INFOLOG(iscsi, "socket is already closed.\n");
    1082                 :          0 :                 return -ENXIO;
    1083                 :            :         }
    1084                 :            : 
    1085                 :            :         /* The socket receive buffer may need to be adjusted based on the new parameters */
    1086                 :            : 
    1087                 :            :         /* Don't allow the recv buffer to be 0 or very large. */
    1088         [ +  + ]:       4334 :         recv_buf_size = spdk_max(0x1000, spdk_min(0x2000, conn->sess->FirstBurstLength));
    1089                 :            : 
    1090                 :            :         /* Add in extra space for the PDU */
    1091                 :       4334 :         recv_buf_size += ISCSI_BHS_LEN + ISCSI_AHS_LEN;
    1092                 :            : 
    1093         [ +  + ]:       4334 :         if (conn->header_digest) {
    1094                 :          8 :                 recv_buf_size += ISCSI_DIGEST_LEN;
    1095                 :            :         }
    1096                 :            : 
    1097         [ -  + ]:       4334 :         if (conn->data_digest) {
    1098                 :          0 :                 recv_buf_size += ISCSI_DIGEST_LEN;
    1099                 :            :         }
    1100                 :            : 
    1101                 :            :         /* Set up to buffer up to 4 commands with immediate data at once */
    1102                 :       4334 :         if (spdk_sock_set_recvbuf(conn->sock, recv_buf_size * 4) < 0) {
    1103                 :            :                 /* Not fatal. */
    1104                 :            :         }
    1105                 :            : 
    1106                 :       4334 :         return rc;
    1107                 :            : }
    1108                 :            : 
    1109                 :            : static void
    1110                 :         32 : iscsi_conn_login_pdu_err_complete(void *arg)
    1111                 :            : {
    1112                 :         32 :         struct spdk_iscsi_conn *conn = arg;
    1113                 :            : 
    1114         [ -  + ]:         32 :         if (conn->full_feature) {
    1115                 :          0 :                 iscsi_conn_params_update(conn);
    1116                 :            :         }
    1117                 :         32 : }
    1118                 :            : 
    1119                 :            : static void
    1120                 :       1569 : iscsi_conn_login_pdu_success_complete(void *arg)
    1121                 :            : {
    1122                 :       1569 :         struct spdk_iscsi_conn *conn = arg;
    1123                 :            : 
    1124                 :            : 
    1125         [ -  + ]:       1569 :         if (conn->state >= ISCSI_CONN_STATE_EXITING) {
    1126                 :            :                 /* Connection is being exited before this callback is executed. */
    1127   [ #  #  #  # ]:          0 :                 SPDK_DEBUGLOG(iscsi, "Connection is already exited.\n");
    1128                 :          0 :                 return;
    1129                 :            :         }
    1130         [ +  + ]:       1569 :         if (conn->full_feature) {
    1131         [ -  + ]:        507 :                 if (iscsi_conn_params_update(conn) != 0) {
    1132                 :          0 :                         return;
    1133                 :            :                 }
    1134                 :            :         }
    1135         [ +  + ]:       1569 :         if (conn->full_feature != 0) {
    1136                 :        507 :                 iscsi_conn_schedule(conn);
    1137                 :            :         }
    1138                 :            : }
    1139                 :            : 
    1140                 :            : /*
    1141                 :            :  * The response function of spdk_iscsi_op_login
    1142                 :            :  */
    1143                 :            : static void
    1144                 :       1619 : iscsi_op_login_response(struct spdk_iscsi_conn *conn,
    1145                 :            :                         struct spdk_iscsi_pdu *rsp_pdu, struct iscsi_param *params,
    1146                 :            :                         iscsi_conn_xfer_complete_cb cb_fn)
    1147                 :            : {
    1148                 :            :         struct iscsi_bhs_login_rsp *rsph;
    1149                 :            : 
    1150                 :       1619 :         rsph = (struct iscsi_bhs_login_rsp *)&rsp_pdu->bhs;
    1151                 :       1619 :         rsph->version_max = ISCSI_VERSION;
    1152                 :       1619 :         rsph->version_act = ISCSI_VERSION;
    1153                 :       1619 :         DSET24(rsph->data_segment_len, rsp_pdu->data_segment_len);
    1154                 :            : 
    1155                 :       1619 :         to_be32(&rsph->stat_sn, conn->StatSN);
    1156                 :       1619 :         conn->StatSN++;
    1157                 :            : 
    1158         [ +  + ]:       1619 :         if (conn->sess != NULL) {
    1159                 :       1595 :                 to_be32(&rsph->exp_cmd_sn, conn->sess->ExpCmdSN);
    1160                 :       1595 :                 to_be32(&rsph->max_cmd_sn, conn->sess->MaxCmdSN);
    1161                 :            :         } else {
    1162                 :         24 :                 to_be32(&rsph->exp_cmd_sn, rsp_pdu->cmd_sn);
    1163                 :         24 :                 to_be32(&rsph->max_cmd_sn, rsp_pdu->cmd_sn);
    1164                 :            :         }
    1165                 :            : 
    1166   [ -  +  -  + ]:       1619 :         SPDK_LOGDUMP(iscsi, "PDU", (uint8_t *)rsph, ISCSI_BHS_LEN);
    1167   [ -  +  -  + ]:       1619 :         SPDK_LOGDUMP(iscsi, "DATA", rsp_pdu->data, rsp_pdu->data_segment_len);
    1168                 :            : 
    1169                 :            :         /* Set T/CSG/NSG to reserved if login error. */
    1170         [ +  + ]:       1619 :         if (rsph->status_class != 0) {
    1171                 :         50 :                 rsph->flags &= ~(ISCSI_LOGIN_TRANSIT | ISCSI_LOGIN_CURRENT_STAGE_MASK |
    1172                 :            :                                  ISCSI_LOGIN_NEXT_STAGE_MASK);
    1173                 :            :         }
    1174                 :       1619 :         iscsi_param_free(params);
    1175                 :       1619 :         iscsi_conn_write_pdu(conn, rsp_pdu, cb_fn, conn);
    1176                 :       1619 : }
    1177                 :            : 
    1178                 :            : /*
    1179                 :            :  * The function which is used to initialize the internal response data
    1180                 :            :  * structure of iscsi login function.
    1181                 :            :  * return:
    1182                 :            :  * 0, success;
    1183                 :            :  * otherwise, error;
    1184                 :            :  */
    1185                 :            : static int
    1186                 :       1625 : iscsi_op_login_rsp_init(struct spdk_iscsi_conn *conn,
    1187                 :            :                         struct spdk_iscsi_pdu *pdu, struct spdk_iscsi_pdu *rsp_pdu)
    1188                 :            : {
    1189                 :            :         struct iscsi_bhs_login_req *reqh;
    1190                 :            :         struct iscsi_bhs_login_rsp *rsph;
    1191                 :            : 
    1192                 :       1625 :         rsph = (struct iscsi_bhs_login_rsp *)&rsp_pdu->bhs;
    1193                 :       1625 :         rsph->opcode = ISCSI_OP_LOGIN_RSP;
    1194                 :       1625 :         rsph->status_class = ISCSI_CLASS_SUCCESS;
    1195                 :       1625 :         rsph->status_detail = ISCSI_LOGIN_ACCEPT;
    1196                 :       1625 :         rsp_pdu->data_segment_len = 0;
    1197                 :            : 
    1198                 :            :         /* The default MaxRecvDataSegmentLength 8192 is used during login. - RFC3720 */
    1199                 :       1625 :         rsp_pdu->data = calloc(1, 8192);
    1200         [ -  + ]:       1625 :         if (!rsp_pdu->data) {
    1201                 :          0 :                 SPDK_ERRLOG("calloc() failed for data segment\n");
    1202                 :          0 :                 rsph->status_class = ISCSI_CLASS_TARGET_ERROR;
    1203                 :          0 :                 rsph->status_detail = ISCSI_LOGIN_STATUS_NO_RESOURCES;
    1204                 :          0 :                 return SPDK_ISCSI_LOGIN_ERROR_RESPONSE;
    1205                 :            :         }
    1206                 :       1625 :         rsp_pdu->data_buf_len = 8192;
    1207                 :            : 
    1208                 :       1625 :         reqh = (struct iscsi_bhs_login_req *)&pdu->bhs;
    1209                 :       1625 :         rsph->flags |= (reqh->flags & (ISCSI_LOGIN_TRANSIT | ISCSI_LOGIN_CONTINUE |
    1210                 :            :                                        ISCSI_LOGIN_CURRENT_STAGE_MASK));
    1211         [ +  + ]:       1625 :         if (ISCSI_BHS_LOGIN_GET_TBIT(rsph->flags)) {
    1212                 :       1259 :                 rsph->flags |= (reqh->flags & ISCSI_LOGIN_NEXT_STAGE_MASK);
    1213                 :            :         }
    1214                 :            : 
    1215                 :            :         /* We don't need to convert from network byte order. Just store it */
    1216   [ -  +  -  + ]:       1625 :         memcpy(&rsph->isid, reqh->isid, 6);
    1217                 :       1625 :         rsph->tsih = reqh->tsih;
    1218                 :       1625 :         rsph->itt = reqh->itt;
    1219                 :       1625 :         rsp_pdu->cmd_sn = from_be32(&reqh->cmd_sn);
    1220                 :            : 
    1221         [ -  + ]:       1625 :         if (rsph->tsih) {
    1222                 :          0 :                 rsph->stat_sn = reqh->exp_stat_sn;
    1223                 :            :         }
    1224                 :            : 
    1225   [ -  +  -  + ]:       1625 :         SPDK_LOGDUMP(iscsi, "PDU", (uint8_t *)&pdu->bhs, ISCSI_BHS_LEN);
    1226                 :            : 
    1227   [ -  +  -  + ]:       1625 :         SPDK_DEBUGLOG(iscsi,
    1228                 :            :                       "T=%d, C=%d, CSG=%d, NSG=%d, Min=%d, Max=%d, ITT=%x\n",
    1229                 :            :                       ISCSI_BHS_LOGIN_GET_TBIT(rsph->flags),
    1230                 :            :                       ISCSI_BHS_LOGIN_GET_CBIT(rsph->flags),
    1231                 :            :                       ISCSI_BHS_LOGIN_GET_CSG(rsph->flags),
    1232                 :            :                       ISCSI_BHS_LOGIN_GET_NSG(rsph->flags),
    1233                 :            :                       reqh->version_min, reqh->version_max, from_be32(&rsph->itt));
    1234                 :            : 
    1235         [ +  + ]:       1625 :         if (conn->sess != NULL) {
    1236   [ -  +  -  + ]:       1040 :                 SPDK_DEBUGLOG(iscsi,
    1237                 :            :                               "CmdSN=%u, ExpStatSN=%u, StatSN=%u, ExpCmdSN=%u,"
    1238                 :            :                               "MaxCmdSN=%u\n", rsp_pdu->cmd_sn,
    1239                 :            :                               from_be32(&rsph->stat_sn), conn->StatSN,
    1240                 :            :                               conn->sess->ExpCmdSN,
    1241                 :            :                               conn->sess->MaxCmdSN);
    1242                 :            :         } else {
    1243   [ -  +  -  + ]:        585 :                 SPDK_DEBUGLOG(iscsi,
    1244                 :            :                               "CmdSN=%u, ExpStatSN=%u, StatSN=%u\n",
    1245                 :            :                               rsp_pdu->cmd_sn, from_be32(&rsph->stat_sn),
    1246                 :            :                               conn->StatSN);
    1247                 :            :         }
    1248                 :            : 
    1249         [ +  + ]:       1625 :         if (ISCSI_BHS_LOGIN_GET_TBIT(rsph->flags) &&
    1250         [ +  + ]:       1259 :             ISCSI_BHS_LOGIN_GET_CBIT(rsph->flags)) {
    1251                 :          6 :                 SPDK_ERRLOG("transit error\n");
    1252                 :          6 :                 rsph->status_class = ISCSI_CLASS_INITIATOR_ERROR;
    1253                 :          6 :                 rsph->status_detail = ISCSI_LOGIN_INITIATOR_ERROR;
    1254                 :          6 :                 return SPDK_ISCSI_LOGIN_ERROR_RESPONSE;
    1255                 :            :         }
    1256                 :            :         /* make sure reqh->version_max < ISCSI_VERSION */
    1257         [ +  + ]:       1619 :         if (reqh->version_min > ISCSI_VERSION) {
    1258                 :         10 :                 SPDK_ERRLOG("unsupported version min %d/max %d, expecting %d\n", reqh->version_min,
    1259                 :            :                             reqh->version_max, ISCSI_VERSION);
    1260                 :            :                 /* Unsupported version */
    1261                 :            :                 /* set all reserved flag to zero */
    1262                 :         10 :                 rsph->status_class = ISCSI_CLASS_INITIATOR_ERROR;
    1263                 :         10 :                 rsph->status_detail = ISCSI_LOGIN_UNSUPPORTED_VERSION;
    1264                 :         10 :                 return SPDK_ISCSI_LOGIN_ERROR_RESPONSE;
    1265                 :            :         }
    1266                 :            : 
    1267         [ +  + ]:       1609 :         if ((ISCSI_BHS_LOGIN_GET_NSG(rsph->flags) == ISCSI_NSG_RESERVED_CODE) &&
    1268         [ +  - ]:          6 :             ISCSI_BHS_LOGIN_GET_TBIT(rsph->flags)) {
    1269                 :            :                 /* set NSG and other bits to zero */
    1270                 :          6 :                 rsph->flags &= ~(ISCSI_LOGIN_NEXT_STAGE_MASK | ISCSI_LOGIN_TRANSIT |
    1271                 :            :                                  ISCSI_LOGIN_CURRENT_STAGE_MASK);
    1272                 :          6 :                 SPDK_ERRLOG("Received reserved NSG code: %d\n", ISCSI_NSG_RESERVED_CODE);
    1273                 :            :                 /* Initiator error */
    1274                 :          6 :                 rsph->status_class = ISCSI_CLASS_INITIATOR_ERROR;
    1275                 :          6 :                 rsph->status_detail = ISCSI_LOGIN_INITIATOR_ERROR;
    1276                 :          6 :                 return SPDK_ISCSI_LOGIN_ERROR_RESPONSE;
    1277                 :            :         }
    1278                 :            : 
    1279                 :       1603 :         return 0;
    1280                 :            : }
    1281                 :            : 
    1282                 :            : static int
    1283                 :       1597 : iscsi_op_login_store_incoming_params(struct spdk_iscsi_conn *conn,
    1284                 :            :                                      struct spdk_iscsi_pdu *pdu, struct spdk_iscsi_pdu *rsp_pdu,
    1285                 :            :                                      struct iscsi_param **params)
    1286                 :            : {
    1287                 :            :         struct iscsi_bhs_login_req *reqh;
    1288                 :            :         struct iscsi_bhs_login_rsp *rsph;
    1289                 :            :         int rc;
    1290                 :            : 
    1291                 :       1597 :         reqh = (struct iscsi_bhs_login_req *)&pdu->bhs;
    1292                 :       1597 :         rsph = (struct iscsi_bhs_login_rsp *)&rsp_pdu->bhs;
    1293                 :            : 
    1294                 :       3194 :         rc = iscsi_parse_params(params, pdu->data,
    1295                 :       1597 :                                 pdu->data_segment_len, ISCSI_BHS_LOGIN_GET_CBIT(reqh->flags),
    1296                 :            :                                 &conn->partial_text_parameter);
    1297         [ +  + ]:       1597 :         if (rc < 0) {
    1298                 :          2 :                 SPDK_ERRLOG("iscsi_parse_params() failed\n");
    1299                 :          2 :                 iscsi_param_free(*params);
    1300                 :          2 :                 rsph->status_class = ISCSI_CLASS_INITIATOR_ERROR;
    1301                 :          2 :                 rsph->status_detail = ISCSI_LOGIN_INITIATOR_ERROR;
    1302                 :          2 :                 return SPDK_ISCSI_LOGIN_ERROR_PARAMETER;
    1303                 :            :         }
    1304                 :            : 
    1305                 :       1595 :         return 0;
    1306                 :            : }
    1307                 :            : 
    1308                 :            : /*
    1309                 :            :  * This function is used to initialize the port info
    1310                 :            :  * return
    1311                 :            :  * 0: success
    1312                 :            :  * otherwise: error
    1313                 :            :  */
    1314                 :            : static int
    1315                 :        557 : iscsi_op_login_initialize_port(struct spdk_iscsi_conn *conn,
    1316                 :            :                                struct spdk_iscsi_pdu *rsp_pdu,
    1317                 :            :                                char *initiator_port_name,
    1318                 :            :                                uint32_t name_length,
    1319                 :            :                                struct iscsi_param *params)
    1320                 :            : {
    1321                 :            :         const char *val;
    1322                 :            :         struct iscsi_bhs_login_rsp *rsph;
    1323                 :        557 :         rsph = (struct iscsi_bhs_login_rsp *)&rsp_pdu->bhs;
    1324                 :            : 
    1325                 :            :         /* Initiator Name and Port */
    1326                 :        557 :         val = iscsi_param_get_val(params, "InitiatorName");
    1327         [ -  + ]:        557 :         if (val == NULL) {
    1328                 :          0 :                 SPDK_ERRLOG("InitiatorName is empty\n");
    1329                 :            :                 /* Missing parameter */
    1330                 :          0 :                 rsph->status_class = ISCSI_CLASS_INITIATOR_ERROR;
    1331                 :          0 :                 rsph->status_detail = ISCSI_LOGIN_MISSING_PARMS;
    1332                 :          0 :                 return SPDK_ISCSI_LOGIN_ERROR_RESPONSE;
    1333                 :            :         }
    1334         [ -  + ]:        557 :         snprintf(conn->initiator_name, sizeof(conn->initiator_name), "%s", val);
    1335         [ -  + ]:        557 :         snprintf(initiator_port_name, name_length,
    1336                 :        557 :                  "%s,i,0x%12.12" PRIx64, val, iscsi_get_isid(rsph->isid));
    1337                 :        557 :         spdk_strlwr(conn->initiator_name);
    1338                 :        557 :         spdk_strlwr(initiator_port_name);
    1339   [ -  +  -  + ]:        557 :         SPDK_DEBUGLOG(iscsi, "Initiator name: %s\n", conn->initiator_name);
    1340   [ -  +  -  + ]:        557 :         SPDK_DEBUGLOG(iscsi, "Initiator port: %s\n", initiator_port_name);
    1341                 :            : 
    1342                 :        557 :         return 0;
    1343                 :            : }
    1344                 :            : 
    1345                 :            : /*
    1346                 :            :  * This function is used to judge the session type
    1347                 :            :  * return
    1348                 :            :  * 0: success
    1349                 :            :  * Other value: error
    1350                 :            :  */
    1351                 :            : static int
    1352                 :        557 : iscsi_op_login_session_type(struct spdk_iscsi_conn *conn,
    1353                 :            :                             struct spdk_iscsi_pdu *rsp_pdu,
    1354                 :            :                             enum session_type *session_type,
    1355                 :            :                             struct iscsi_param *params)
    1356                 :            : {
    1357                 :            :         const char *session_type_str;
    1358                 :            :         struct iscsi_bhs_login_rsp *rsph;
    1359                 :            : 
    1360                 :        557 :         rsph = (struct iscsi_bhs_login_rsp *)&rsp_pdu->bhs;
    1361                 :        557 :         session_type_str = iscsi_param_get_val(params, "SessionType");
    1362         [ -  + ]:        557 :         if (session_type_str == NULL) {
    1363         [ #  # ]:          0 :                 if (rsph->tsih != 0) {
    1364                 :          0 :                         *session_type = SESSION_TYPE_NORMAL;
    1365                 :            :                 } else {
    1366                 :          0 :                         SPDK_ERRLOG("SessionType is empty\n");
    1367                 :            :                         /* Missing parameter */
    1368                 :          0 :                         rsph->status_class = ISCSI_CLASS_INITIATOR_ERROR;
    1369                 :          0 :                         rsph->status_detail = ISCSI_LOGIN_MISSING_PARMS;
    1370                 :          0 :                         return SPDK_ISCSI_LOGIN_ERROR_RESPONSE;
    1371                 :            :                 }
    1372                 :            :         } else {
    1373   [ -  +  +  + ]:        557 :                 if (strcasecmp(session_type_str, "Discovery") == 0) {
    1374                 :         31 :                         *session_type = SESSION_TYPE_DISCOVERY;
    1375   [ -  +  +  - ]:        526 :                 } else if (strcasecmp(session_type_str, "Normal") == 0) {
    1376                 :        526 :                         *session_type = SESSION_TYPE_NORMAL;
    1377                 :            :                 } else {
    1378                 :          0 :                         *session_type = SESSION_TYPE_INVALID;
    1379                 :          0 :                         SPDK_ERRLOG("SessionType is invalid\n");
    1380                 :            :                         /* Missing parameter */
    1381                 :          0 :                         rsph->status_class = ISCSI_CLASS_INITIATOR_ERROR;
    1382                 :          0 :                         rsph->status_detail = ISCSI_LOGIN_MISSING_PARMS;
    1383                 :          0 :                         return SPDK_ISCSI_LOGIN_ERROR_RESPONSE;
    1384                 :            :                 }
    1385                 :            :         }
    1386   [ -  +  -  + ]:        557 :         SPDK_DEBUGLOG(iscsi, "Session Type: %s\n", session_type_str);
    1387                 :            : 
    1388                 :        557 :         return 0;
    1389                 :            : }
    1390                 :            : 
    1391                 :            : /*
    1392                 :            :  * This function is used to check the target info
    1393                 :            :  * return:
    1394                 :            :  * 0: success
    1395                 :            :  * otherwise: error
    1396                 :            :  */
    1397                 :            : static int
    1398                 :        568 : iscsi_op_login_check_target(struct spdk_iscsi_conn *conn,
    1399                 :            :                             struct spdk_iscsi_pdu *rsp_pdu,
    1400                 :            :                             const char *target_name,
    1401                 :            :                             struct spdk_iscsi_tgt_node **target)
    1402                 :            : {
    1403                 :            :         struct iscsi_bhs_login_rsp *rsph;
    1404                 :        568 :         char buf[MAX_TMPBUF] = {};
    1405                 :            : 
    1406                 :        568 :         rsph = (struct iscsi_bhs_login_rsp *)&rsp_pdu->bhs;
    1407                 :        568 :         *target = iscsi_find_tgt_node(target_name);
    1408         [ +  + ]:        568 :         if (*target == NULL) {
    1409                 :          6 :                 SPDK_WARNLOG("target %s not found\n", target_name);
    1410                 :            :                 /* Not found */
    1411                 :          6 :                 rsph->status_class = ISCSI_CLASS_INITIATOR_ERROR;
    1412                 :          6 :                 rsph->status_detail = ISCSI_LOGIN_TARGET_NOT_FOUND;
    1413                 :          6 :                 return SPDK_ISCSI_LOGIN_ERROR_RESPONSE;
    1414                 :            :         }
    1415         [ -  + ]:        562 :         if (iscsi_tgt_node_is_destructed(*target)) {
    1416                 :          0 :                 SPDK_ERRLOG("target %s is removed\n", target_name);
    1417                 :          0 :                 rsph->status_class = ISCSI_CLASS_INITIATOR_ERROR;
    1418                 :          0 :                 rsph->status_detail = ISCSI_LOGIN_TARGET_REMOVED;
    1419                 :          0 :                 return SPDK_ISCSI_LOGIN_ERROR_RESPONSE;
    1420                 :            :         }
    1421         [ +  + ]:        562 :         if (iscsi_tgt_node_is_redirected(conn, *target, buf, MAX_TMPBUF)) {
    1422   [ -  +  -  + ]:          2 :                 SPDK_INFOLOG(iscsi, "target %s is redirected\n", target_name);
    1423                 :          2 :                 rsp_pdu->data_segment_len = iscsi_append_text("TargetAddress",
    1424                 :            :                                             buf,
    1425                 :            :                                             rsp_pdu->data,
    1426                 :          2 :                                             rsp_pdu->data_buf_len,
    1427                 :          2 :                                             rsp_pdu->data_segment_len);
    1428                 :          2 :                 rsph->status_class = ISCSI_CLASS_REDIRECT;
    1429                 :          2 :                 rsph->status_detail = ISCSI_LOGIN_TARGET_TEMPORARILY_MOVED;
    1430                 :          2 :                 return SPDK_ISCSI_LOGIN_ERROR_RESPONSE;
    1431                 :            :         }
    1432         [ +  + ]:        560 :         if (!iscsi_tgt_node_access(conn, *target, conn->initiator_name,
    1433                 :        560 :                                    conn->initiator_addr)) {
    1434                 :          6 :                 SPDK_ERRLOG("access denied\n");
    1435                 :          6 :                 rsph->status_class = ISCSI_CLASS_INITIATOR_ERROR;
    1436                 :          6 :                 rsph->status_detail = ISCSI_LOGIN_AUTHORIZATION_FAIL;
    1437                 :          6 :                 return SPDK_ISCSI_LOGIN_ERROR_RESPONSE;
    1438                 :            :         }
    1439                 :            : 
    1440                 :        554 :         return 0;
    1441                 :            : }
    1442                 :            : 
    1443                 :            : /*
    1444                 :            :  * This function use to check the session
    1445                 :            :  * return:
    1446                 :            :  * 0, success
    1447                 :            :  * otherwise: error
    1448                 :            :  */
    1449                 :            : static int
    1450                 :        548 : iscsi_op_login_check_session(struct spdk_iscsi_conn *conn,
    1451                 :            :                              struct spdk_iscsi_pdu *rsp_pdu,
    1452                 :            :                              char *initiator_port_name, int cid)
    1453                 :            : 
    1454                 :            : {
    1455                 :        548 :         int rc = 0;
    1456                 :            :         struct iscsi_bhs_login_rsp *rsph;
    1457                 :            : 
    1458                 :        548 :         rsph = (struct iscsi_bhs_login_rsp *)&rsp_pdu->bhs;
    1459                 :            :         /* check existing session */
    1460   [ -  +  -  + ]:        548 :         SPDK_DEBUGLOG(iscsi, "isid=%"PRIx64", tsih=%u, cid=%u\n",
    1461                 :            :                       iscsi_get_isid(rsph->isid), from_be16(&rsph->tsih), cid);
    1462         [ +  + ]:        548 :         if (rsph->tsih != 0) {
    1463                 :            :                 /* multiple connections */
    1464                 :         12 :                 rc = append_iscsi_sess(conn, initiator_port_name,
    1465                 :         12 :                                        from_be16(&rsph->tsih), cid);
    1466         [ +  - ]:         12 :                 if (rc != 0) {
    1467                 :         12 :                         SPDK_ERRLOG("isid=%"PRIx64", tsih=%u, cid=%u:"
    1468                 :            :                                     "spdk_append_iscsi_sess() failed\n",
    1469                 :            :                                     iscsi_get_isid(rsph->isid), from_be16(&rsph->tsih),
    1470                 :            :                                     cid);
    1471                 :            :                         /* Can't include in session */
    1472                 :         12 :                         rsph->status_class = ISCSI_CLASS_INITIATOR_ERROR;
    1473                 :         12 :                         rsph->status_detail = rc;
    1474                 :         12 :                         return SPDK_ISCSI_LOGIN_ERROR_RESPONSE;
    1475                 :            :                 }
    1476   [ +  +  +  + ]:        536 :         } else if (!g_iscsi.AllowDuplicateIsid) {
    1477                 :            :                 /* new session, drop old sess by the initiator */
    1478                 :        166 :                 iscsi_drop_conns(conn, initiator_port_name, 0 /* drop old */);
    1479                 :            :         }
    1480                 :            : 
    1481                 :        536 :         return rc;
    1482                 :            : }
    1483                 :            : 
    1484                 :            : /*
    1485                 :            :  * This function is used to del the original param and update it with new
    1486                 :            :  * value
    1487                 :            :  * return:
    1488                 :            :  * 0: success
    1489                 :            :  * otherwise: error
    1490                 :            :  */
    1491                 :            : static int
    1492                 :        160 : iscsi_op_login_update_param(struct spdk_iscsi_conn *conn,
    1493                 :            :                             const char *key, const char *value,
    1494                 :            :                             const char *list)
    1495                 :            : {
    1496                 :        160 :         int rc = 0;
    1497                 :            :         struct iscsi_param *new_param, *orig_param;
    1498                 :            :         int index;
    1499                 :            : 
    1500                 :        160 :         orig_param = iscsi_param_find(conn->params, key);
    1501         [ -  + ]:        160 :         if (orig_param == NULL) {
    1502                 :          0 :                 SPDK_ERRLOG("orig_param %s not found\n", key);
    1503                 :          0 :                 return SPDK_ISCSI_LOGIN_ERROR_PARAMETER;
    1504                 :            :         }
    1505                 :            : 
    1506                 :        160 :         index = orig_param->state_index;
    1507                 :        160 :         rc = iscsi_param_del(&conn->params, key);
    1508         [ -  + ]:        160 :         if (rc < 0) {
    1509                 :          0 :                 SPDK_ERRLOG("iscsi_param_del(%s) failed\n", key);
    1510                 :          0 :                 return SPDK_ISCSI_LOGIN_ERROR_PARAMETER;
    1511                 :            :         }
    1512                 :        160 :         rc = iscsi_param_add(&conn->params, key, value, list, ISPT_LIST);
    1513         [ -  + ]:        160 :         if (rc < 0) {
    1514                 :          0 :                 SPDK_ERRLOG("iscsi_param_add() failed\n");
    1515                 :          0 :                 return SPDK_ISCSI_LOGIN_ERROR_PARAMETER;
    1516                 :            :         }
    1517                 :        160 :         new_param = iscsi_param_find(conn->params, key);
    1518         [ -  + ]:        160 :         if (new_param == NULL) {
    1519                 :          0 :                 SPDK_ERRLOG("iscsi_param_find() failed\n");
    1520                 :          0 :                 return SPDK_ISCSI_LOGIN_ERROR_PARAMETER;
    1521                 :            :         }
    1522                 :        160 :         new_param->state_index = index;
    1523                 :        160 :         return rc;
    1524                 :            : }
    1525                 :            : 
    1526                 :            : static int
    1527                 :        567 : iscsi_negotiate_chap_param(struct spdk_iscsi_conn *conn)
    1528                 :            : {
    1529                 :        567 :         int rc = 0;
    1530                 :            : 
    1531   [ -  +  +  + ]:        567 :         if (conn->disable_chap) {
    1532                 :        160 :                 rc = iscsi_op_login_update_param(conn, "AuthMethod", "None", "None");
    1533   [ -  +  -  + ]:        407 :         } else if (conn->require_chap) {
    1534                 :          0 :                 rc = iscsi_op_login_update_param(conn, "AuthMethod", "CHAP", "CHAP");
    1535                 :            :         }
    1536                 :            : 
    1537                 :        567 :         return rc;
    1538                 :            : }
    1539                 :            : 
    1540                 :            : /*
    1541                 :            :  * The function which is used to handle the part of session discovery
    1542                 :            :  * return:
    1543                 :            :  * 0, success;
    1544                 :            :  * otherwise: error;
    1545                 :            :  */
    1546                 :            : static int
    1547                 :         31 : iscsi_op_login_session_discovery_chap(struct spdk_iscsi_conn *conn)
    1548                 :            : {
    1549                 :         31 :         return iscsi_negotiate_chap_param(conn);
    1550                 :            : }
    1551                 :            : 
    1552                 :            : /*
    1553                 :            :  * This function is used to update the param related with chap
    1554                 :            :  * return:
    1555                 :            :  * 0: success
    1556                 :            :  * otherwise: error
    1557                 :            :  */
    1558                 :            : static int
    1559                 :        536 : iscsi_op_login_negotiate_chap_param(struct spdk_iscsi_conn *conn,
    1560                 :            :                                     struct spdk_iscsi_tgt_node *target)
    1561                 :            : {
    1562         [ -  + ]:        536 :         conn->disable_chap = target->disable_chap;
    1563         [ -  + ]:        536 :         conn->require_chap = target->require_chap;
    1564         [ -  + ]:        536 :         conn->mutual_chap = target->mutual_chap;
    1565                 :        536 :         conn->chap_group = target->chap_group;
    1566                 :            : 
    1567                 :        536 :         return iscsi_negotiate_chap_param(conn);
    1568                 :            : }
    1569                 :            : 
    1570                 :            : static int
    1571                 :        536 : iscsi_op_login_negotiate_digest_param(struct spdk_iscsi_conn *conn,
    1572                 :            :                                       struct spdk_iscsi_tgt_node *target)
    1573                 :            : {
    1574                 :            :         int rc;
    1575                 :            : 
    1576   [ -  +  -  + ]:        536 :         if (target->header_digest) {
    1577                 :            :                 /*
    1578                 :            :                  * User specified header digests, so update the list of
    1579                 :            :                  *  HeaderDigest values to remove "None" so that only
    1580                 :            :                  *  initiators who support CRC32C can connect.
    1581                 :            :                  */
    1582                 :          0 :                 rc = iscsi_op_login_update_param(conn, "HeaderDigest", "CRC32C", "CRC32C");
    1583         [ #  # ]:          0 :                 if (rc < 0) {
    1584                 :          0 :                         return rc;
    1585                 :            :                 }
    1586                 :            :         }
    1587                 :            : 
    1588   [ -  +  -  + ]:        536 :         if (target->data_digest) {
    1589                 :            :                 /*
    1590                 :            :                  * User specified data digests, so update the list of
    1591                 :            :                  *  DataDigest values to remove "None" so that only
    1592                 :            :                  *  initiators who support CRC32C can connect.
    1593                 :            :                  */
    1594                 :          0 :                 rc = iscsi_op_login_update_param(conn, "DataDigest", "CRC32C", "CRC32C");
    1595         [ #  # ]:          0 :                 if (rc < 0) {
    1596                 :          0 :                         return rc;
    1597                 :            :                 }
    1598                 :            :         }
    1599                 :            : 
    1600                 :        536 :         return 0;
    1601                 :            : }
    1602                 :            : 
    1603                 :            : /*
    1604                 :            :  * The function which is used to handle the part of normal login session
    1605                 :            :  * return:
    1606                 :            :  * 0, success;
    1607                 :            :  * SPDK_ISCSI_LOGIN_ERROR_PARAMETER, parameter error;
    1608                 :            :  */
    1609                 :            : static int
    1610                 :        568 : iscsi_op_login_session_normal(struct spdk_iscsi_conn *conn,
    1611                 :            :                               struct spdk_iscsi_pdu *rsp_pdu,
    1612                 :            :                               char *initiator_port_name,
    1613                 :            :                               struct iscsi_param *params,
    1614                 :            :                               int cid)
    1615                 :            : {
    1616                 :        568 :         struct spdk_iscsi_tgt_node *target = NULL;
    1617                 :            :         const char *target_name;
    1618                 :            :         const char *target_short_name;
    1619                 :            :         struct iscsi_bhs_login_rsp *rsph;
    1620                 :        568 :         int rc = 0;
    1621                 :            : 
    1622                 :        568 :         rsph = (struct iscsi_bhs_login_rsp *)&rsp_pdu->bhs;
    1623                 :        568 :         target_name = iscsi_param_get_val(params, "TargetName");
    1624                 :            : 
    1625         [ +  + ]:        568 :         if (target_name == NULL) {
    1626                 :         18 :                 SPDK_ERRLOG("TargetName is empty\n");
    1627                 :            :                 /* Missing parameter */
    1628                 :         18 :                 rsph->status_class = ISCSI_CLASS_INITIATOR_ERROR;
    1629                 :         18 :                 rsph->status_detail = ISCSI_LOGIN_MISSING_PARMS;
    1630                 :         18 :                 return SPDK_ISCSI_LOGIN_ERROR_RESPONSE;
    1631                 :            :         }
    1632                 :            : 
    1633         [ -  + ]:        550 :         memset(conn->target_short_name, 0, MAX_TARGET_NAME);
    1634         [ -  + ]:        550 :         target_short_name = strstr(target_name, ":");
    1635         [ +  - ]:        550 :         if (target_short_name != NULL) {
    1636                 :        550 :                 target_short_name++; /* Advance past the ':' */
    1637   [ -  +  -  + ]:        550 :                 if (strlen(target_short_name) >= MAX_TARGET_NAME) {
    1638                 :          0 :                         SPDK_ERRLOG("Target Short Name (%s) is more than %u characters\n",
    1639                 :            :                                     target_short_name, MAX_TARGET_NAME);
    1640                 :            :                         /* Invalid request */
    1641                 :          0 :                         rsph->status_class = ISCSI_CLASS_INITIATOR_ERROR;
    1642                 :          0 :                         rsph->status_detail = ISCSI_LOGIN_INVALID_LOGIN_REQUEST;
    1643                 :          0 :                         return SPDK_ISCSI_LOGIN_ERROR_RESPONSE;
    1644                 :            :                 }
    1645                 :        550 :                 snprintf(conn->target_short_name, MAX_TARGET_NAME, "%s",
    1646                 :            :                          target_short_name);
    1647                 :            :         }
    1648                 :            : 
    1649                 :        550 :         pthread_mutex_lock(&g_iscsi.mutex);
    1650                 :        550 :         rc = iscsi_op_login_check_target(conn, rsp_pdu, target_name, &target);
    1651                 :        550 :         pthread_mutex_unlock(&g_iscsi.mutex);
    1652                 :            : 
    1653         [ +  + ]:        550 :         if (rc < 0) {
    1654                 :          2 :                 return rc;
    1655                 :            :         }
    1656                 :            : 
    1657                 :        548 :         conn->target = target;
    1658                 :        548 :         conn->dev = target->dev;
    1659                 :        556 :         conn->target_port = spdk_scsi_dev_find_port_by_id(target->dev,
    1660                 :        548 :                             conn->pg_tag);
    1661                 :            : 
    1662                 :        548 :         rc = iscsi_op_login_check_session(conn, rsp_pdu,
    1663                 :            :                                           initiator_port_name, cid);
    1664         [ +  + ]:        548 :         if (rc < 0) {
    1665                 :         12 :                 return rc;
    1666                 :            :         }
    1667                 :            : 
    1668                 :            :         /* force target flags */
    1669         [ -  + ]:        536 :         pthread_mutex_lock(&target->mutex);
    1670                 :        536 :         rc = iscsi_op_login_negotiate_chap_param(conn, target);
    1671         [ -  + ]:        536 :         pthread_mutex_unlock(&target->mutex);
    1672                 :            : 
    1673         [ +  - ]:        536 :         if (rc == 0) {
    1674                 :        536 :                 rc = iscsi_op_login_negotiate_digest_param(conn, target);
    1675                 :            :         }
    1676                 :            : 
    1677         [ -  + ]:        536 :         if (rc != 0) {
    1678                 :            :                 /* Invalid request */
    1679                 :          0 :                 rsph->status_class = ISCSI_CLASS_INITIATOR_ERROR;
    1680                 :          0 :                 rsph->status_detail = ISCSI_LOGIN_INVALID_LOGIN_REQUEST;
    1681                 :            :         }
    1682                 :            : 
    1683                 :        536 :         return rc;
    1684                 :            : }
    1685                 :            : 
    1686                 :            : /*
    1687                 :            :  * This function is used to set the info in the connection data structure
    1688                 :            :  * return
    1689                 :            :  * 0: success
    1690                 :            :  * otherwise: error
    1691                 :            :  */
    1692                 :            : static int
    1693                 :        555 : iscsi_op_login_set_conn_info(struct spdk_iscsi_conn *conn,
    1694                 :            :                              struct spdk_iscsi_pdu *rsp_pdu,
    1695                 :            :                              char *initiator_port_name,
    1696                 :            :                              enum session_type session_type, int cid)
    1697                 :            : {
    1698                 :        555 :         int rc = 0;
    1699                 :            :         struct spdk_iscsi_tgt_node *target;
    1700                 :            :         struct iscsi_bhs_login_rsp *rsph;
    1701                 :          0 :         struct spdk_scsi_port *initiator_port;
    1702                 :            : 
    1703                 :        555 :         target = conn->target;
    1704                 :            : 
    1705                 :        555 :         rsph = (struct iscsi_bhs_login_rsp *)&rsp_pdu->bhs;
    1706                 :        555 :         conn->authenticated = false;
    1707                 :        555 :         conn->auth.chap_phase = ISCSI_CHAP_PHASE_WAIT_A;
    1708                 :        555 :         conn->cid = cid;
    1709                 :            : 
    1710         [ +  - ]:        555 :         if (conn->sess == NULL) {
    1711                 :            :                 /* create initiator port */
    1712                 :        555 :                 initiator_port = spdk_scsi_port_create(iscsi_get_isid(rsph->isid), 0, initiator_port_name);
    1713         [ -  + ]:        555 :                 if (initiator_port == NULL) {
    1714                 :          0 :                         SPDK_ERRLOG("create_port() failed\n");
    1715                 :          0 :                         rsph->status_class = ISCSI_CLASS_TARGET_ERROR;
    1716                 :          0 :                         rsph->status_detail = ISCSI_LOGIN_STATUS_NO_RESOURCES;
    1717                 :          0 :                         return SPDK_ISCSI_LOGIN_ERROR_RESPONSE;
    1718                 :            :                 }
    1719                 :            : 
    1720                 :            :                 /* new session */
    1721                 :        555 :                 rc = create_iscsi_sess(conn, target, session_type);
    1722         [ -  + ]:        555 :                 if (rc < 0) {
    1723                 :          0 :                         spdk_scsi_port_free(&initiator_port);
    1724                 :          0 :                         SPDK_ERRLOG("create_sess() failed\n");
    1725                 :          0 :                         rsph->status_class = ISCSI_CLASS_TARGET_ERROR;
    1726                 :          0 :                         rsph->status_detail = ISCSI_LOGIN_STATUS_NO_RESOURCES;
    1727                 :          0 :                         return SPDK_ISCSI_LOGIN_ERROR_RESPONSE;
    1728                 :            :                 }
    1729                 :            :                 /* initialize parameters */
    1730                 :        555 :                 conn->sess->initiator_port = initiator_port;
    1731                 :        555 :                 conn->StatSN = from_be32(&rsph->stat_sn);
    1732                 :        555 :                 conn->sess->isid = iscsi_get_isid(rsph->isid);
    1733                 :            : 
    1734                 :            :                 /* Initiator port TransportID */
    1735                 :       1110 :                 spdk_scsi_port_set_iscsi_transport_id(conn->sess->initiator_port,
    1736                 :        555 :                                                       conn->initiator_name,
    1737                 :        555 :                                                       conn->sess->isid);
    1738                 :            : 
    1739                 :            :                 /* Discovery sessions will not have a target. */
    1740         [ +  + ]:        555 :                 if (target != NULL) {
    1741                 :        524 :                         conn->sess->queue_depth = target->queue_depth;
    1742                 :            :                 } else {
    1743                 :            :                         /*
    1744                 :            :                          * Assume discovery sessions have an effective command
    1745                 :            :                          *  windows size of 1.
    1746                 :            :                          */
    1747                 :         31 :                         conn->sess->queue_depth = 1;
    1748                 :            :                 }
    1749                 :        555 :                 conn->sess->ExpCmdSN = rsp_pdu->cmd_sn;
    1750                 :        555 :                 conn->sess->MaxCmdSN = rsp_pdu->cmd_sn + conn->sess->queue_depth - 1;
    1751                 :            :         }
    1752                 :            : 
    1753                 :        555 :         conn->initiator_port = conn->sess->initiator_port;
    1754                 :            : 
    1755                 :        555 :         return 0;
    1756                 :            : }
    1757                 :            : 
    1758                 :            : /*
    1759                 :            :  * This function is used to set the target info
    1760                 :            :  * return
    1761                 :            :  * 0: success
    1762                 :            :  * otherwise: error
    1763                 :            :  */
    1764                 :            : static int
    1765                 :        555 : iscsi_op_login_set_target_info(struct spdk_iscsi_conn *conn,
    1766                 :            :                                struct spdk_iscsi_pdu *rsp_pdu,
    1767                 :            :                                enum session_type session_type)
    1768                 :            : {
    1769                 :          0 :         char buf[MAX_TMPBUF];
    1770                 :            :         const char *val;
    1771                 :        555 :         int rc = 0;
    1772                 :        555 :         struct spdk_iscsi_tgt_node *target = conn->target;
    1773                 :            : 
    1774                 :            :         /* declarative parameters */
    1775         [ +  + ]:        555 :         if (target != NULL) {
    1776         [ -  + ]:        524 :                 pthread_mutex_lock(&target->mutex);
    1777         [ +  - ]:        524 :                 if (target->alias[0] != '\0') {
    1778         [ -  + ]:        524 :                         snprintf(buf, sizeof buf, "%s", target->alias);
    1779                 :            :                 } else {
    1780         [ #  # ]:          0 :                         snprintf(buf, sizeof buf, "%s", "");
    1781                 :            :                 }
    1782         [ -  + ]:        524 :                 pthread_mutex_unlock(&target->mutex);
    1783                 :        524 :                 rc = iscsi_param_set(conn->sess->params, "TargetAlias", buf);
    1784         [ -  + ]:        524 :                 if (rc < 0) {
    1785                 :          0 :                         SPDK_ERRLOG("iscsi_param_set() failed\n");
    1786                 :          0 :                         return SPDK_ISCSI_LOGIN_ERROR_PARAMETER;
    1787                 :            :                 }
    1788                 :            :         }
    1789         [ -  + ]:        555 :         snprintf(buf, sizeof buf, "%s:%s,%d", conn->portal_host, conn->portal_port,
    1790                 :            :                  conn->pg_tag);
    1791                 :        555 :         rc = iscsi_param_set(conn->sess->params, "TargetAddress", buf);
    1792         [ -  + ]:        555 :         if (rc < 0) {
    1793                 :          0 :                 SPDK_ERRLOG("iscsi_param_set() failed\n");
    1794                 :          0 :                 return SPDK_ISCSI_LOGIN_ERROR_PARAMETER;
    1795                 :            :         }
    1796         [ -  + ]:        555 :         snprintf(buf, sizeof buf, "%d", conn->pg_tag);
    1797                 :        555 :         rc = iscsi_param_set(conn->sess->params, "TargetPortalGroupTag", buf);
    1798         [ -  + ]:        555 :         if (rc < 0) {
    1799                 :          0 :                 SPDK_ERRLOG("iscsi_param_set() failed\n");
    1800                 :          0 :                 return SPDK_ISCSI_LOGIN_ERROR_PARAMETER;
    1801                 :            :         }
    1802                 :            : 
    1803                 :            :         /* write in response */
    1804         [ +  + ]:        555 :         if (target != NULL) {
    1805                 :        524 :                 val = iscsi_param_get_val(conn->sess->params, "TargetAlias");
    1806   [ +  -  +  - ]:        524 :                 if (val != NULL && strlen(val) != 0) {
    1807                 :        524 :                         rsp_pdu->data_segment_len = iscsi_append_param(conn,
    1808                 :            :                                                     "TargetAlias",
    1809                 :            :                                                     rsp_pdu->data,
    1810                 :        524 :                                                     rsp_pdu->data_buf_len,
    1811                 :        524 :                                                     rsp_pdu->data_segment_len);
    1812                 :            :                 }
    1813         [ -  + ]:        524 :                 if (session_type == SESSION_TYPE_DISCOVERY) {
    1814                 :          0 :                         rsp_pdu->data_segment_len = iscsi_append_param(conn,
    1815                 :            :                                                     "TargetAddress",
    1816                 :            :                                                     rsp_pdu->data,
    1817                 :          0 :                                                     rsp_pdu->data_buf_len,
    1818                 :          0 :                                                     rsp_pdu->data_segment_len);
    1819                 :            :                 }
    1820                 :        524 :                 rsp_pdu->data_segment_len = iscsi_append_param(conn,
    1821                 :            :                                             "TargetPortalGroupTag",
    1822                 :            :                                             rsp_pdu->data,
    1823                 :        524 :                                             rsp_pdu->data_buf_len,
    1824                 :        524 :                                             rsp_pdu->data_segment_len);
    1825                 :            :         }
    1826                 :            : 
    1827                 :        555 :         return rc;
    1828                 :            : }
    1829                 :            : 
    1830                 :            : /*
    1831                 :            :  * This function is used to handle the login of iscsi initiator when there is
    1832                 :            :  * no session
    1833                 :            :  * return:
    1834                 :            :  * 0, success;
    1835                 :            :  * SPDK_ISCSI_LOGIN_ERROR_PARAMETER, parameter error;
    1836                 :            :  * SPDK_ISCSI_LOGIN_ERROR_RESPONSE,  used to notify the login fail.
    1837                 :            :  */
    1838                 :            : static int
    1839                 :        557 : iscsi_op_login_phase_none(struct spdk_iscsi_conn *conn,
    1840                 :            :                           struct spdk_iscsi_pdu *rsp_pdu,
    1841                 :            :                           struct iscsi_param *params, int cid)
    1842                 :            : {
    1843                 :          0 :         enum session_type session_type;
    1844                 :          0 :         char initiator_port_name[MAX_INITIATOR_PORT_NAME];
    1845                 :            :         struct iscsi_bhs_login_rsp *rsph;
    1846                 :        557 :         int rc = 0;
    1847                 :        557 :         rsph = (struct iscsi_bhs_login_rsp *)&rsp_pdu->bhs;
    1848                 :            : 
    1849                 :        557 :         conn->target = NULL;
    1850                 :        557 :         conn->dev = NULL;
    1851                 :            : 
    1852                 :        557 :         rc = iscsi_op_login_initialize_port(conn, rsp_pdu, initiator_port_name,
    1853                 :            :                                             MAX_INITIATOR_PORT_NAME, params);
    1854         [ -  + ]:        557 :         if (rc < 0) {
    1855                 :          0 :                 return rc;
    1856                 :            :         }
    1857                 :            : 
    1858                 :        557 :         rc = iscsi_op_login_session_type(conn, rsp_pdu, &session_type, params);
    1859         [ -  + ]:        557 :         if (rc < 0) {
    1860                 :          0 :                 return rc;
    1861                 :            :         }
    1862                 :            : 
    1863                 :            :         /* Target Name and Port */
    1864         [ +  + ]:        557 :         if (session_type == SESSION_TYPE_NORMAL) {
    1865                 :        526 :                 rc = iscsi_op_login_session_normal(conn, rsp_pdu,
    1866                 :            :                                                    initiator_port_name,
    1867                 :            :                                                    params, cid);
    1868         [ +  + ]:        526 :                 if (rc < 0) {
    1869                 :          2 :                         return rc;
    1870                 :            :                 }
    1871                 :            : 
    1872         [ +  - ]:         31 :         } else if (session_type == SESSION_TYPE_DISCOVERY) {
    1873                 :         31 :                 rsph->tsih = 0;
    1874                 :            : 
    1875                 :            :                 /* force target flags */
    1876         [ -  + ]:         31 :                 pthread_mutex_lock(&g_iscsi.mutex);
    1877                 :         31 :                 rc = iscsi_op_login_session_discovery_chap(conn);
    1878         [ -  + ]:         31 :                 pthread_mutex_unlock(&g_iscsi.mutex);
    1879         [ -  + ]:         31 :                 if (rc < 0) {
    1880                 :          0 :                         return rc;
    1881                 :            :                 }
    1882                 :            :         } else {
    1883                 :          0 :                 SPDK_ERRLOG("unknown session type\n");
    1884                 :            :                 /* Missing parameter */
    1885                 :          0 :                 rsph->status_class = ISCSI_CLASS_INITIATOR_ERROR;
    1886                 :          0 :                 rsph->status_detail = ISCSI_LOGIN_MISSING_PARMS;
    1887                 :          0 :                 return SPDK_ISCSI_LOGIN_ERROR_RESPONSE;
    1888                 :            :         }
    1889                 :            : 
    1890                 :        555 :         rc = iscsi_op_login_set_conn_info(conn, rsp_pdu, initiator_port_name,
    1891                 :            :                                           session_type, cid);
    1892         [ -  + ]:        555 :         if (rc < 0) {
    1893                 :          0 :                 return rc;
    1894                 :            :         }
    1895                 :            : 
    1896                 :            :         /* limit conns on discovery session */
    1897         [ +  + ]:        555 :         if (session_type == SESSION_TYPE_DISCOVERY) {
    1898                 :         31 :                 conn->sess->MaxConnections = 1;
    1899                 :         31 :                 rc = iscsi_param_set_int(conn->sess->params,
    1900                 :            :                                          "MaxConnections",
    1901                 :         31 :                                          conn->sess->MaxConnections);
    1902         [ -  + ]:         31 :                 if (rc < 0) {
    1903                 :          0 :                         SPDK_ERRLOG("iscsi_param_set_int() failed\n");
    1904                 :          0 :                         return SPDK_ISCSI_LOGIN_ERROR_PARAMETER;
    1905                 :            :                 }
    1906                 :            :         }
    1907                 :            : 
    1908                 :        555 :         return iscsi_op_login_set_target_info(conn, rsp_pdu, session_type);
    1909                 :            : }
    1910                 :            : 
    1911                 :            : /*
    1912                 :            :  * This function is used to set the csg bit case in rsp
    1913                 :            :  * return:
    1914                 :            :  * 0, success
    1915                 :            :  * otherwise: error
    1916                 :            :  */
    1917                 :            : static int
    1918                 :       1569 : iscsi_op_login_rsp_handle_csg_bit(struct spdk_iscsi_conn *conn,
    1919                 :            :                                   struct spdk_iscsi_pdu *rsp_pdu,
    1920                 :            :                                   struct iscsi_param *params)
    1921                 :            : {
    1922                 :            :         const char *auth_method;
    1923                 :            :         int rc;
    1924                 :            :         struct iscsi_bhs_login_rsp *rsph;
    1925                 :       1569 :         rsph = (struct iscsi_bhs_login_rsp *)&rsp_pdu->bhs;
    1926                 :            : 
    1927   [ +  +  -  - ]:       1569 :         switch (ISCSI_BHS_LOGIN_GET_CSG(rsph->flags)) {
    1928                 :       1052 :         case ISCSI_SECURITY_NEGOTIATION_PHASE:
    1929                 :            :                 /* SecurityNegotiation */
    1930                 :       1052 :                 auth_method = iscsi_param_get_val(conn->params, "AuthMethod");
    1931         [ -  + ]:       1052 :                 if (auth_method == NULL) {
    1932                 :          0 :                         SPDK_ERRLOG("AuthMethod is empty\n");
    1933                 :            :                         /* Missing parameter */
    1934                 :          0 :                         rsph->status_class = ISCSI_CLASS_INITIATOR_ERROR;
    1935                 :          0 :                         rsph->status_detail = ISCSI_LOGIN_MISSING_PARMS;
    1936                 :          0 :                         return SPDK_ISCSI_LOGIN_ERROR_RESPONSE;
    1937                 :            :                 }
    1938   [ -  +  -  + ]:       1052 :                 if (strcasecmp(auth_method, "None") == 0) {
    1939                 :          0 :                         conn->authenticated = true;
    1940                 :            :                 } else {
    1941                 :       1052 :                         rc = iscsi_auth_params(conn, params, auth_method,
    1942                 :       1052 :                                                rsp_pdu->data, rsp_pdu->data_buf_len,
    1943                 :       1052 :                                                rsp_pdu->data_segment_len);
    1944         [ -  + ]:       1052 :                         if (rc < 0) {
    1945                 :          0 :                                 SPDK_ERRLOG("iscsi_auth_params() failed\n");
    1946                 :            :                                 /* Authentication failure */
    1947                 :          0 :                                 rsph->status_class = ISCSI_CLASS_INITIATOR_ERROR;
    1948                 :          0 :                                 rsph->status_detail = ISCSI_LOGIN_AUTHENT_FAIL;
    1949                 :          0 :                                 return SPDK_ISCSI_LOGIN_ERROR_RESPONSE;
    1950                 :            :                         }
    1951                 :       1052 :                         rsp_pdu->data_segment_len = rc;
    1952   [ -  +  +  + ]:       1052 :                         if (!conn->authenticated) {
    1953                 :            :                                 /* not complete */
    1954                 :        708 :                                 rsph->flags &= ~ISCSI_LOGIN_TRANSIT;
    1955                 :            :                         } else {
    1956         [ -  + ]:        344 :                                 if (conn->auth.chap_phase != ISCSI_CHAP_PHASE_END) {
    1957   [ #  #  #  # ]:          0 :                                         SPDK_DEBUGLOG(iscsi, "CHAP phase not complete");
    1958                 :            :                                 }
    1959                 :            :                         }
    1960                 :            : 
    1961   [ -  +  -  + ]:       1052 :                         SPDK_LOGDUMP(iscsi, "Negotiated Auth Params",
    1962                 :            :                                      rsp_pdu->data, rsp_pdu->data_segment_len);
    1963                 :            :                 }
    1964                 :       1052 :                 break;
    1965                 :            : 
    1966                 :        517 :         case ISCSI_OPERATIONAL_NEGOTIATION_PHASE:
    1967                 :            :                 /* LoginOperationalNegotiation */
    1968         [ +  + ]:        517 :                 if (conn->state == ISCSI_CONN_STATE_INVALID) {
    1969   [ -  +  -  + ]:        189 :                         if (conn->require_chap) {
    1970                 :            :                                 /* Authentication failure */
    1971                 :          0 :                                 rsph->status_class = ISCSI_CLASS_INITIATOR_ERROR;
    1972                 :          0 :                                 rsph->status_detail = ISCSI_LOGIN_AUTHENT_FAIL;
    1973                 :          0 :                                 return SPDK_ISCSI_LOGIN_ERROR_RESPONSE;
    1974                 :            :                         } else {
    1975                 :            :                                 /* AuthMethod=None */
    1976                 :        189 :                                 conn->authenticated = true;
    1977                 :            :                         }
    1978                 :            :                 }
    1979   [ -  +  -  + ]:        517 :                 if (!conn->authenticated) {
    1980                 :          0 :                         SPDK_ERRLOG("authentication error\n");
    1981                 :            :                         /* Authentication failure */
    1982                 :          0 :                         rsph->status_class = ISCSI_CLASS_INITIATOR_ERROR;
    1983                 :          0 :                         rsph->status_detail = ISCSI_LOGIN_AUTHENT_FAIL;
    1984                 :          0 :                         return SPDK_ISCSI_LOGIN_ERROR_RESPONSE;
    1985                 :            :                 }
    1986                 :        517 :                 break;
    1987                 :            : 
    1988                 :          0 :         case ISCSI_FULL_FEATURE_PHASE:
    1989                 :            :                 /* FullFeaturePhase */
    1990                 :          0 :                 SPDK_ERRLOG("XXX Login in FullFeaturePhase\n");
    1991                 :            :                 /* Initiator error */
    1992                 :          0 :                 rsph->status_class = ISCSI_CLASS_INITIATOR_ERROR;
    1993                 :          0 :                 rsph->status_detail = ISCSI_LOGIN_INITIATOR_ERROR;
    1994                 :          0 :                 return SPDK_ISCSI_LOGIN_ERROR_RESPONSE;
    1995                 :            : 
    1996                 :          0 :         default:
    1997                 :          0 :                 SPDK_ERRLOG("unknown stage\n");
    1998                 :            :                 /* Initiator error */
    1999                 :          0 :                 rsph->status_class = ISCSI_CLASS_INITIATOR_ERROR;
    2000                 :          0 :                 rsph->status_detail = ISCSI_LOGIN_INITIATOR_ERROR;
    2001                 :          0 :                 return SPDK_ISCSI_LOGIN_ERROR_RESPONSE;
    2002                 :            :         }
    2003                 :            : 
    2004                 :       1569 :         return 0;
    2005                 :            : }
    2006                 :            : 
    2007                 :            : /* This function is used to notify the session info
    2008                 :            :  * return
    2009                 :            :  * 0: success
    2010                 :            :  * otherwise: error
    2011                 :            :  */
    2012                 :            : static int
    2013                 :        507 : iscsi_op_login_notify_session_info(struct spdk_iscsi_conn *conn,
    2014                 :            :                                    struct spdk_iscsi_pdu *rsp_pdu)
    2015                 :            : {
    2016                 :            :         struct iscsi_bhs_login_rsp *rsph;
    2017                 :            : 
    2018                 :        507 :         rsph = (struct iscsi_bhs_login_rsp *)&rsp_pdu->bhs;
    2019         [ +  + ]:        507 :         if (conn->sess->session_type == SESSION_TYPE_NORMAL) {
    2020                 :            :                 /* normal session */
    2021   [ -  +  -  +  :        478 :                 SPDK_DEBUGLOG(iscsi, "Login from %s (%s) on %s tgt_node%d"
             -  -  -  - ]
    2022                 :            :                               " (%s:%s,%d), ISID=%"PRIx64", TSIH=%u,"
    2023                 :            :                               " CID=%u, HeaderDigest=%s, DataDigest=%s\n",
    2024                 :            :                               conn->initiator_name, conn->initiator_addr,
    2025                 :            :                               conn->target->name, conn->target->num,
    2026                 :            :                               conn->portal_host, conn->portal_port, conn->pg_tag,
    2027                 :            :                               conn->sess->isid, conn->sess->tsih, conn->cid,
    2028                 :            :                               (iscsi_param_eq_val(conn->params, "HeaderDigest", "CRC32C")
    2029                 :            :                                ? "on" : "off"),
    2030                 :            :                               (iscsi_param_eq_val(conn->params, "DataDigest", "CRC32C")
    2031                 :            :                                ? "on" : "off"));
    2032         [ +  - ]:         29 :         } else if (conn->sess->session_type == SESSION_TYPE_DISCOVERY) {
    2033                 :            :                 /* discovery session */
    2034   [ -  +  -  +  :         29 :                 SPDK_DEBUGLOG(iscsi, "Login(discovery) from %s (%s) on"
             -  -  -  - ]
    2035                 :            :                               " (%s:%s,%d), ISID=%"PRIx64", TSIH=%u,"
    2036                 :            :                               " CID=%u, HeaderDigest=%s, DataDigest=%s\n",
    2037                 :            :                               conn->initiator_name, conn->initiator_addr,
    2038                 :            :                               conn->portal_host, conn->portal_port, conn->pg_tag,
    2039                 :            :                               conn->sess->isid, conn->sess->tsih, conn->cid,
    2040                 :            :                               (iscsi_param_eq_val(conn->params, "HeaderDigest", "CRC32C")
    2041                 :            :                                ? "on" : "off"),
    2042                 :            :                               (iscsi_param_eq_val(conn->params, "DataDigest", "CRC32C")
    2043                 :            :                                ? "on" : "off"));
    2044                 :            :         } else {
    2045                 :          0 :                 SPDK_ERRLOG("unknown session type\n");
    2046                 :            :                 /* Initiator error */
    2047                 :          0 :                 rsph->status_class = ISCSI_CLASS_INITIATOR_ERROR;
    2048                 :          0 :                 rsph->status_detail = ISCSI_LOGIN_INITIATOR_ERROR;
    2049                 :          0 :                 return SPDK_ISCSI_LOGIN_ERROR_RESPONSE;
    2050                 :            :         }
    2051                 :            : 
    2052                 :        507 :         return 0;
    2053                 :            : }
    2054                 :            : 
    2055                 :            : /*
    2056                 :            :  * This function is to handle the tbit cases
    2057                 :            :  * return
    2058                 :            :  * 0: success
    2059                 :            :  * otherwise error
    2060                 :            :  */
    2061                 :            : static int
    2062                 :        851 : iscsi_op_login_rsp_handle_t_bit(struct spdk_iscsi_conn *conn,
    2063                 :            :                                 struct spdk_iscsi_pdu *rsp_pdu)
    2064                 :            : {
    2065                 :            :         int rc;
    2066                 :            :         struct iscsi_bhs_login_rsp *rsph;
    2067                 :        851 :         rsph = (struct iscsi_bhs_login_rsp *)&rsp_pdu->bhs;
    2068                 :            : 
    2069   [ -  +  +  - ]:        851 :         switch (ISCSI_BHS_LOGIN_GET_NSG(rsph->flags)) {
    2070                 :          0 :         case ISCSI_SECURITY_NEGOTIATION_PHASE:
    2071                 :            :                 /* SecurityNegotiation */
    2072                 :          0 :                 conn->login_phase = ISCSI_SECURITY_NEGOTIATION_PHASE;
    2073                 :          0 :                 break;
    2074                 :            : 
    2075                 :        344 :         case ISCSI_OPERATIONAL_NEGOTIATION_PHASE:
    2076                 :            :                 /* LoginOperationalNegotiation */
    2077                 :        344 :                 conn->login_phase = ISCSI_OPERATIONAL_NEGOTIATION_PHASE;
    2078                 :        344 :                 break;
    2079                 :            : 
    2080                 :        507 :         case ISCSI_FULL_FEATURE_PHASE:
    2081                 :            :                 /* FullFeaturePhase */
    2082                 :        507 :                 conn->login_phase = ISCSI_FULL_FEATURE_PHASE;
    2083                 :        507 :                 to_be16(&rsph->tsih, conn->sess->tsih);
    2084                 :            : 
    2085                 :        507 :                 rc = iscsi_op_login_notify_session_info(conn, rsp_pdu);
    2086         [ -  + ]:        507 :                 if (rc < 0) {
    2087                 :          0 :                         return rc;
    2088                 :            :                 }
    2089                 :            : 
    2090                 :        507 :                 conn->full_feature = 1;
    2091                 :        507 :                 break;
    2092                 :            : 
    2093                 :          0 :         default:
    2094                 :          0 :                 SPDK_ERRLOG("unknown stage\n");
    2095                 :            :                 /* Initiator error */
    2096                 :          0 :                 rsph->status_class = ISCSI_CLASS_INITIATOR_ERROR;
    2097                 :          0 :                 rsph->status_detail = ISCSI_LOGIN_INITIATOR_ERROR;
    2098                 :          0 :                 return SPDK_ISCSI_LOGIN_ERROR_RESPONSE;
    2099                 :            :         }
    2100                 :            : 
    2101                 :        851 :         return 0;
    2102                 :            : }
    2103                 :            : 
    2104                 :            : /*
    2105                 :            :  * This function is used to set the values of the internal data structure used
    2106                 :            :  * by spdk_iscsi_op_login function
    2107                 :            :  * return:
    2108                 :            :  * 0, used to notify the a successful login
    2109                 :            :  * SPDK_ISCSI_LOGIN_ERROR_RESPONSE,  used to notify a failure login.
    2110                 :            :  */
    2111                 :            : static int
    2112                 :       1593 : iscsi_op_login_rsp_handle(struct spdk_iscsi_conn *conn,
    2113                 :            :                           struct spdk_iscsi_pdu *rsp_pdu, struct iscsi_param **params)
    2114                 :            : {
    2115                 :            :         int rc;
    2116                 :            :         struct iscsi_bhs_login_rsp *rsph;
    2117                 :       1593 :         rsph = (struct iscsi_bhs_login_rsp *)&rsp_pdu->bhs;
    2118                 :            : 
    2119                 :            :         /* negotiate parameters */
    2120                 :       1593 :         rc = iscsi_negotiate_params(conn, params, rsp_pdu->data,
    2121                 :       1593 :                                     rsp_pdu->data_buf_len,
    2122                 :       1593 :                                     rsp_pdu->data_segment_len);
    2123         [ +  + ]:       1593 :         if (rc < 0) {
    2124                 :            :                 /*
    2125                 :            :                  * iscsi_negotiate_params just returns -1 on failure,
    2126                 :            :                  *  so translate this into meaningful response codes and
    2127                 :            :                  *  return values.
    2128                 :            :                  */
    2129                 :         24 :                 rsph->status_class = ISCSI_CLASS_INITIATOR_ERROR;
    2130                 :         24 :                 rsph->status_detail = ISCSI_LOGIN_INITIATOR_ERROR;
    2131                 :         24 :                 return SPDK_ISCSI_LOGIN_ERROR_RESPONSE;
    2132                 :            :         }
    2133                 :            : 
    2134                 :       1569 :         rsp_pdu->data_segment_len = rc;
    2135   [ -  +  -  + ]:       1569 :         SPDK_LOGDUMP(iscsi, "Negotiated Params", rsp_pdu->data, rc);
    2136                 :            : 
    2137                 :            :         /* handle the CSG bit case */
    2138                 :       1569 :         rc = iscsi_op_login_rsp_handle_csg_bit(conn, rsp_pdu, *params);
    2139         [ -  + ]:       1569 :         if (rc < 0) {
    2140                 :          0 :                 return rc;
    2141                 :            :         }
    2142                 :            : 
    2143                 :            :         /* handle the T bit case */
    2144         [ +  + ]:       1569 :         if (ISCSI_BHS_LOGIN_GET_TBIT(rsph->flags)) {
    2145                 :        851 :                 rc = iscsi_op_login_rsp_handle_t_bit(conn, rsp_pdu);
    2146                 :            :         }
    2147                 :            : 
    2148                 :       1569 :         return rc;
    2149                 :            : }
    2150                 :            : 
    2151                 :            : static int
    2152                 :       1643 : iscsi_pdu_hdr_op_login(struct spdk_iscsi_conn *conn, struct spdk_iscsi_pdu *pdu)
    2153                 :            : {
    2154                 :            :         int rc;
    2155                 :            :         struct iscsi_bhs_login_req *reqh;
    2156                 :            :         struct spdk_iscsi_pdu *rsp_pdu;
    2157                 :            : 
    2158   [ +  +  +  - ]:       1643 :         if (conn->full_feature && conn->sess != NULL &&
    2159         [ +  - ]:          6 :             conn->sess->session_type == SESSION_TYPE_DISCOVERY) {
    2160                 :          6 :                 return SPDK_ISCSI_CONNECTION_FATAL;
    2161                 :            :         }
    2162                 :            : 
    2163                 :       1637 :         reqh = (struct iscsi_bhs_login_req *)&pdu->bhs;
    2164                 :       1637 :         pdu->cmd_sn = from_be32(&reqh->cmd_sn);
    2165                 :            : 
    2166                 :            :         /* During login processing, use the 8KB default FirstBurstLength as
    2167                 :            :          *  our maximum data segment length value.
    2168                 :            :          */
    2169         [ +  + ]:       1637 :         if (pdu->data_segment_len > SPDK_ISCSI_FIRST_BURST_LENGTH) {
    2170                 :          6 :                 return iscsi_reject(conn, pdu, ISCSI_REASON_PROTOCOL_ERROR);
    2171                 :            :         }
    2172                 :            : 
    2173                 :       1631 :         rsp_pdu = iscsi_get_pdu(conn);
    2174         [ +  + ]:       1631 :         if (rsp_pdu == NULL) {
    2175                 :          6 :                 return SPDK_ISCSI_CONNECTION_FATAL;
    2176                 :            :         }
    2177                 :       1625 :         rc = iscsi_op_login_rsp_init(conn, pdu, rsp_pdu);
    2178         [ +  + ]:       1625 :         if (rc < 0) {
    2179                 :         22 :                 iscsi_op_login_response(conn, rsp_pdu, NULL, iscsi_conn_login_pdu_err_complete);
    2180                 :         22 :                 return 0;
    2181                 :            :         }
    2182                 :            : 
    2183                 :       1603 :         conn->login_rsp_pdu = rsp_pdu;
    2184                 :       1603 :         return 0;
    2185                 :            : }
    2186                 :            : 
    2187                 :            : static int
    2188                 :       1601 : iscsi_pdu_payload_op_login(struct spdk_iscsi_conn *conn, struct spdk_iscsi_pdu *pdu)
    2189                 :            : {
    2190                 :            :         int rc;
    2191                 :            :         struct iscsi_bhs_login_req *reqh;
    2192                 :            :         struct spdk_iscsi_pdu *rsp_pdu;
    2193                 :       1601 :         struct iscsi_param *params = NULL;
    2194                 :            :         int cid;
    2195                 :            : 
    2196         [ +  + ]:       1601 :         if (conn->login_rsp_pdu == NULL) {
    2197                 :          4 :                 return 0;
    2198                 :            :         }
    2199                 :            : 
    2200                 :       1597 :         spdk_poller_unregister(&conn->login_timer);
    2201                 :       1597 :         rsp_pdu = conn->login_rsp_pdu;
    2202                 :            : 
    2203                 :       1597 :         reqh = (struct iscsi_bhs_login_req *)&pdu->bhs;
    2204                 :       1597 :         cid = from_be16(&reqh->cid);
    2205                 :            : 
    2206                 :       1597 :         rc = iscsi_op_login_store_incoming_params(conn, pdu, rsp_pdu, &params);
    2207         [ +  + ]:       1597 :         if (rc < 0) {
    2208                 :          2 :                 iscsi_op_login_response(conn, rsp_pdu, NULL, iscsi_conn_login_pdu_err_complete);
    2209                 :          2 :                 return 0;
    2210                 :            :         }
    2211                 :            : 
    2212         [ +  + ]:       1595 :         if (conn->state == ISCSI_CONN_STATE_INVALID) {
    2213                 :        557 :                 rc = iscsi_op_login_phase_none(conn, rsp_pdu, params, cid);
    2214   [ +  +  -  + ]:        557 :                 if (rc == SPDK_ISCSI_LOGIN_ERROR_RESPONSE || rc == SPDK_ISCSI_LOGIN_ERROR_PARAMETER) {
    2215                 :          2 :                         iscsi_op_login_response(conn, rsp_pdu, params, iscsi_conn_login_pdu_err_complete);
    2216                 :          2 :                         return 0;
    2217                 :            :                 }
    2218                 :            :         }
    2219                 :            : 
    2220                 :       1593 :         rc = iscsi_op_login_rsp_handle(conn, rsp_pdu, &params);
    2221         [ +  + ]:       1593 :         if (rc == SPDK_ISCSI_LOGIN_ERROR_RESPONSE) {
    2222                 :         24 :                 iscsi_op_login_response(conn, rsp_pdu, params, iscsi_conn_login_pdu_err_complete);
    2223                 :         24 :                 return 0;
    2224                 :            :         }
    2225                 :            : 
    2226                 :       1569 :         conn->state = ISCSI_CONN_STATE_RUNNING;
    2227                 :       1569 :         iscsi_op_login_response(conn, rsp_pdu, params, iscsi_conn_login_pdu_success_complete);
    2228                 :       1569 :         return 0;
    2229                 :            : }
    2230                 :            : 
    2231                 :            : static int
    2232                 :       3869 : iscsi_pdu_hdr_op_text(struct spdk_iscsi_conn *conn, struct spdk_iscsi_pdu *pdu)
    2233                 :            : {
    2234                 :            :         uint32_t task_tag;
    2235                 :            :         uint32_t ExpStatSN;
    2236                 :            :         int F_bit, C_bit;
    2237                 :            :         struct iscsi_bhs_text_req *reqh;
    2238                 :            : 
    2239         [ +  + ]:       3869 :         if (pdu->data_segment_len > iscsi_get_max_immediate_data_size()) {
    2240                 :          6 :                 SPDK_ERRLOG("data segment len(=%zu) > immediate data len(=%"PRIu32")\n",
    2241                 :            :                             pdu->data_segment_len, iscsi_get_max_immediate_data_size());
    2242                 :          6 :                 return iscsi_reject(conn, pdu, ISCSI_REASON_PROTOCOL_ERROR);
    2243                 :            :         }
    2244                 :            : 
    2245                 :       3863 :         reqh = (struct iscsi_bhs_text_req *)&pdu->bhs;
    2246                 :            : 
    2247                 :       3863 :         F_bit = !!(reqh->flags & ISCSI_FLAG_FINAL);
    2248                 :       3863 :         C_bit = !!(reqh->flags & ISCSI_TEXT_CONTINUE);
    2249                 :       3863 :         task_tag = from_be32(&reqh->itt);
    2250                 :       3863 :         ExpStatSN = from_be32(&reqh->exp_stat_sn);
    2251                 :            : 
    2252   [ -  +  -  + ]:       3863 :         SPDK_DEBUGLOG(iscsi, "I=%d, F=%d, C=%d, ITT=%x, TTT=%x\n",
    2253                 :            :                       reqh->immediate, F_bit, C_bit, task_tag, from_be32(&reqh->ttt));
    2254                 :            : 
    2255   [ -  +  -  + ]:       3863 :         SPDK_DEBUGLOG(iscsi,
    2256                 :            :                       "CmdSN=%u, ExpStatSN=%u, StatSN=%u, ExpCmdSN=%u, MaxCmdSN=%u\n",
    2257                 :            :                       pdu->cmd_sn, ExpStatSN, conn->StatSN, conn->sess->ExpCmdSN,
    2258                 :            :                       conn->sess->MaxCmdSN);
    2259                 :            : 
    2260         [ +  + ]:       3863 :         if (ExpStatSN != conn->StatSN) {
    2261                 :            : #if 0
    2262                 :            :                 SPDK_ERRLOG("StatSN(%u) error\n", ExpStatSN);
    2263                 :            :                 return -1;
    2264                 :            : #else
    2265                 :            :                 /* StarPort have a bug */
    2266   [ -  +  -  + ]:       3816 :                 SPDK_DEBUGLOG(iscsi, "StatSN(%u) rewound\n", ExpStatSN);
    2267                 :       3816 :                 conn->StatSN = ExpStatSN;
    2268                 :            : #endif
    2269                 :            :         }
    2270                 :            : 
    2271   [ +  +  +  + ]:       3863 :         if (F_bit && C_bit) {
    2272                 :          6 :                 SPDK_ERRLOG("final and continue\n");
    2273                 :          6 :                 return -1;
    2274                 :            :         }
    2275                 :            : 
    2276                 :            :         /*
    2277                 :            :          * If this is the first text op in a sequence, save the ITT so we can
    2278                 :            :          * compare it against the ITT for subsequent ops in the same sequence.
    2279                 :            :          * If a subsequent text op in same sequence has a different ITT, reject
    2280                 :            :          * that PDU.
    2281                 :            :          */
    2282         [ +  + ]:       3857 :         if (conn->sess->current_text_itt == 0xffffffffU) {
    2283                 :         43 :                 conn->sess->current_text_itt = task_tag;
    2284         [ +  + ]:       3814 :         } else if (conn->sess->current_text_itt != task_tag) {
    2285                 :          6 :                 SPDK_ERRLOG("The correct itt is %u, and the current itt is %u...\n",
    2286                 :            :                             conn->sess->current_text_itt, task_tag);
    2287                 :          6 :                 return iscsi_reject(conn, pdu, ISCSI_REASON_PROTOCOL_ERROR);
    2288                 :            :         }
    2289                 :            : 
    2290                 :       3851 :         return 0;
    2291                 :            : }
    2292                 :            : 
    2293                 :            : static void
    2294                 :       3827 : iscsi_conn_text_pdu_complete(void *arg)
    2295                 :            : {
    2296                 :       3827 :         struct spdk_iscsi_conn *conn = arg;
    2297                 :            : 
    2298                 :       3827 :         iscsi_conn_params_update(conn);
    2299                 :       3827 : }
    2300                 :            : 
    2301                 :            : static int
    2302                 :       3833 : iscsi_pdu_payload_op_text(struct spdk_iscsi_conn *conn, struct spdk_iscsi_pdu *pdu)
    2303                 :            : {
    2304                 :       3833 :         struct iscsi_param *params = NULL;
    2305                 :            :         struct spdk_iscsi_pdu *rsp_pdu;
    2306                 :            :         uint8_t *data;
    2307                 :            :         uint64_t lun;
    2308                 :            :         uint32_t task_tag;
    2309                 :            :         const char *val;
    2310                 :            :         int F_bit, C_bit;
    2311                 :            :         int data_len;
    2312                 :            :         int alloc_len;
    2313                 :            :         int rc;
    2314                 :            :         struct iscsi_bhs_text_req *reqh;
    2315                 :            :         struct iscsi_bhs_text_resp *rsph;
    2316                 :            : 
    2317                 :       3833 :         data_len = 0;
    2318                 :       3833 :         alloc_len = conn->MaxRecvDataSegmentLength;
    2319                 :            : 
    2320                 :       3833 :         reqh = (struct iscsi_bhs_text_req *)&pdu->bhs;
    2321                 :            : 
    2322                 :       3833 :         F_bit = !!(reqh->flags & ISCSI_FLAG_FINAL);
    2323                 :       3833 :         C_bit = !!(reqh->flags & ISCSI_TEXT_CONTINUE);
    2324                 :       3833 :         lun = from_be64(&reqh->lun);
    2325                 :       3833 :         task_tag = from_be32(&reqh->itt);
    2326                 :            : 
    2327                 :            :         /* store incoming parameters */
    2328                 :       3833 :         rc = iscsi_parse_params(&params, pdu->data, pdu->data_segment_len,
    2329                 :            :                                 C_bit, &conn->partial_text_parameter);
    2330         [ -  + ]:       3833 :         if (rc < 0) {
    2331                 :          0 :                 SPDK_ERRLOG("iscsi_parse_params() failed\n");
    2332                 :          0 :                 iscsi_param_free(params);
    2333                 :          0 :                 return -1;
    2334                 :            :         }
    2335                 :            : 
    2336   [ +  +  +  - ]:       3833 :         if (pdu->data_segment_len == 0 && params == NULL) {
    2337                 :       3796 :                 params = conn->params_text;
    2338                 :       3796 :                 conn->params_text = NULL;
    2339                 :            :         }
    2340                 :            : 
    2341                 :       3833 :         data = calloc(1, alloc_len);
    2342         [ -  + ]:       3833 :         if (!data) {
    2343                 :          0 :                 SPDK_ERRLOG("calloc() failed for data segment\n");
    2344                 :          0 :                 iscsi_param_free(params);
    2345                 :          0 :                 return -ENOMEM;
    2346                 :            :         }
    2347                 :            : 
    2348                 :            :         /* negotiate parameters */
    2349                 :       3833 :         data_len = iscsi_negotiate_params(conn, &params,
    2350                 :            :                                           data, alloc_len, data_len);
    2351         [ -  + ]:       3833 :         if (data_len < 0) {
    2352                 :          0 :                 SPDK_ERRLOG("iscsi_negotiate_params() failed\n");
    2353                 :          0 :                 iscsi_param_free(params);
    2354                 :          0 :                 free(data);
    2355                 :          0 :                 return -1;
    2356                 :            :         }
    2357                 :            : 
    2358                 :            :         /* sendtargets is special case */
    2359                 :       3833 :         val = iscsi_param_get_val(params, "SendTargets");
    2360         [ +  + ]:       3833 :         if (val != NULL) {
    2361         [ +  + ]:         31 :                 if (iscsi_param_eq_val(conn->sess->params,
    2362                 :            :                                        "SessionType", "Discovery")) {
    2363         [ -  + ]:         29 :                         if (strcasecmp(val, "") == 0) {
    2364                 :          0 :                                 val = "ALL";
    2365                 :            :                         }
    2366                 :            : 
    2367                 :         29 :                         data_len = iscsi_send_tgts(conn,
    2368                 :         29 :                                                    conn->initiator_name,
    2369                 :            :                                                    val, data, alloc_len,
    2370                 :            :                                                    data_len);
    2371                 :            :                 } else {
    2372         [ -  + ]:          2 :                         if (strcasecmp(val, "") == 0) {
    2373                 :          0 :                                 val = conn->target->name;
    2374                 :            :                         }
    2375                 :            : 
    2376   [ -  +  +  - ]:          2 :                         if (strcasecmp(val, "ALL") == 0) {
    2377                 :            :                                 /* not in discovery session */
    2378                 :          2 :                                 data_len = iscsi_append_text("SendTargets", "Reject",
    2379                 :            :                                                              data, alloc_len, data_len);
    2380                 :            :                         } else {
    2381                 :          0 :                                 data_len = iscsi_send_tgts(conn,
    2382                 :          0 :                                                            conn->initiator_name,
    2383                 :            :                                                            val, data, alloc_len,
    2384                 :            :                                                            data_len);
    2385                 :            :                         }
    2386                 :            :                 }
    2387                 :            : 
    2388         [ -  + ]:         31 :                 if (conn->send_tgt_completed_size != 0) {
    2389                 :          0 :                         F_bit = 0;
    2390                 :          0 :                         C_bit = 1;
    2391                 :            :                 }
    2392                 :            :         } else {
    2393         [ -  + ]:       3802 :                 if (iscsi_param_eq_val(conn->sess->params, "SessionType", "Discovery")) {
    2394                 :          0 :                         iscsi_param_free(params);
    2395                 :          0 :                         free(data);
    2396                 :          0 :                         return SPDK_ISCSI_CONNECTION_FATAL;
    2397                 :            :                 }
    2398                 :            :         }
    2399                 :            : 
    2400         [ +  - ]:       3833 :         if (spdk_likely(conn->send_tgt_completed_size == 0)) {
    2401                 :       3833 :                 iscsi_param_free(params);
    2402                 :            :         } else {
    2403                 :          0 :                 conn->params_text = params;
    2404                 :            :         }
    2405   [ -  +  -  + ]:       3833 :         SPDK_LOGDUMP(iscsi, "Negotiated Params", data, data_len);
    2406                 :            : 
    2407                 :            :         /* response PDU */
    2408                 :       3833 :         rsp_pdu = iscsi_get_pdu(conn);
    2409         [ -  + ]:       3833 :         if (rsp_pdu == NULL) {
    2410                 :          0 :                 free(data);
    2411                 :          0 :                 return SPDK_ISCSI_CONNECTION_FATAL;
    2412                 :            :         }
    2413                 :       3833 :         rsph = (struct iscsi_bhs_text_resp *)&rsp_pdu->bhs;
    2414                 :            : 
    2415                 :       3833 :         rsp_pdu->data = data;
    2416                 :       3833 :         rsph->opcode = ISCSI_OP_TEXT_RSP;
    2417                 :            : 
    2418         [ +  + ]:       3833 :         if (F_bit) {
    2419                 :         31 :                 rsph->flags |= ISCSI_FLAG_FINAL;
    2420                 :            :         }
    2421                 :            : 
    2422         [ +  + ]:       3833 :         if (C_bit) {
    2423                 :          6 :                 rsph->flags |= ISCSI_TEXT_CONTINUE;
    2424                 :            :         }
    2425                 :            : 
    2426                 :       3833 :         DSET24(rsph->data_segment_len, data_len);
    2427                 :       3833 :         to_be64(&rsph->lun, lun);
    2428                 :       3833 :         to_be32(&rsph->itt, task_tag);
    2429                 :            : 
    2430         [ +  + ]:       3833 :         if (F_bit) {
    2431                 :         31 :                 rsph->ttt = 0xffffffffU;
    2432                 :         31 :                 conn->sess->current_text_itt = 0xffffffffU;
    2433                 :            :         } else {
    2434                 :       3802 :                 to_be32(&rsph->ttt, 1 + conn->id);
    2435                 :            :         }
    2436                 :            : 
    2437                 :       3833 :         to_be32(&rsph->stat_sn, conn->StatSN);
    2438                 :       3833 :         conn->StatSN++;
    2439                 :            : 
    2440         [ +  + ]:       3833 :         if (reqh->immediate == 0) {
    2441                 :         37 :                 conn->sess->MaxCmdSN++;
    2442                 :            :         }
    2443                 :            : 
    2444                 :       3833 :         to_be32(&rsph->exp_cmd_sn, conn->sess->ExpCmdSN);
    2445                 :       3833 :         to_be32(&rsph->max_cmd_sn, conn->sess->MaxCmdSN);
    2446                 :            : 
    2447                 :       3833 :         iscsi_conn_write_pdu(conn, rsp_pdu, iscsi_conn_text_pdu_complete, conn);
    2448                 :       3833 :         return 0;
    2449                 :            : }
    2450                 :            : 
    2451                 :            : static void
    2452                 :        357 : iscsi_conn_logout_pdu_complete(void *arg)
    2453                 :            : {
    2454                 :        357 :         struct spdk_iscsi_conn *conn = arg;
    2455                 :            : 
    2456         [ -  + ]:        357 :         if (conn->sess == NULL) {
    2457                 :            :                 /*
    2458                 :            :                  * login failed but initiator still sent a logout rather than
    2459                 :            :                  *  just closing the TCP connection.
    2460                 :            :                  */
    2461   [ #  #  #  # ]:          0 :                 SPDK_DEBUGLOG(iscsi, "Logout(login failed) from %s (%s) on"
    2462                 :            :                               " (%s:%s,%d)\n",
    2463                 :            :                               conn->initiator_name, conn->initiator_addr,
    2464                 :            :                               conn->portal_host, conn->portal_port, conn->pg_tag);
    2465         [ +  - ]:        357 :         } else if (iscsi_param_eq_val(conn->sess->params, "SessionType", "Normal")) {
    2466   [ -  +  -  +  :        357 :                 SPDK_DEBUGLOG(iscsi, "Logout from %s (%s) on %s tgt_node%d"
             -  -  -  - ]
    2467                 :            :                               " (%s:%s,%d), ISID=%"PRIx64", TSIH=%u,"
    2468                 :            :                               " CID=%u, HeaderDigest=%s, DataDigest=%s\n",
    2469                 :            :                               conn->initiator_name, conn->initiator_addr,
    2470                 :            :                               conn->target->name, conn->target->num,
    2471                 :            :                               conn->portal_host, conn->portal_port, conn->pg_tag,
    2472                 :            :                               conn->sess->isid, conn->sess->tsih, conn->cid,
    2473                 :            :                               (iscsi_param_eq_val(conn->params, "HeaderDigest", "CRC32C")
    2474                 :            :                                ? "on" : "off"),
    2475                 :            :                               (iscsi_param_eq_val(conn->params, "DataDigest", "CRC32C")
    2476                 :            :                                ? "on" : "off"));
    2477                 :            :         } else {
    2478                 :            :                 /* discovery session */
    2479   [ #  #  #  #  :          0 :                 SPDK_DEBUGLOG(iscsi, "Logout(discovery) from %s (%s) on"
             #  #  #  # ]
    2480                 :            :                               " (%s:%s,%d), ISID=%"PRIx64", TSIH=%u,"
    2481                 :            :                               " CID=%u, HeaderDigest=%s, DataDigest=%s\n",
    2482                 :            :                               conn->initiator_name, conn->initiator_addr,
    2483                 :            :                               conn->portal_host, conn->portal_port, conn->pg_tag,
    2484                 :            :                               conn->sess->isid, conn->sess->tsih, conn->cid,
    2485                 :            :                               (iscsi_param_eq_val(conn->params, "HeaderDigest", "CRC32C")
    2486                 :            :                                ? "on" : "off"),
    2487                 :            :                               (iscsi_param_eq_val(conn->params, "DataDigest", "CRC32C")
    2488                 :            :                                ? "on" : "off"));
    2489                 :            :         }
    2490                 :        357 : }
    2491                 :            : 
    2492                 :            : static int
    2493                 :        387 : iscsi_pdu_hdr_op_logout(struct spdk_iscsi_conn *conn, struct spdk_iscsi_pdu *pdu)
    2494                 :            : {
    2495                 :            :         struct spdk_iscsi_pdu *rsp_pdu;
    2496                 :            :         uint32_t task_tag;
    2497                 :            :         uint32_t ExpStatSN;
    2498                 :            :         int response;
    2499                 :            :         struct iscsi_bhs_logout_req *reqh;
    2500                 :            :         struct iscsi_bhs_logout_resp *rsph;
    2501                 :            :         uint16_t cid;
    2502                 :            : 
    2503                 :        387 :         reqh = (struct iscsi_bhs_logout_req *)&pdu->bhs;
    2504                 :            : 
    2505                 :        387 :         cid = from_be16(&reqh->cid);
    2506                 :        387 :         task_tag = from_be32(&reqh->itt);
    2507                 :        387 :         ExpStatSN = from_be32(&reqh->exp_stat_sn);
    2508                 :            : 
    2509   [ -  +  -  + ]:        387 :         SPDK_DEBUGLOG(iscsi, "reason=%d, ITT=%x, cid=%d\n",
    2510                 :            :                       reqh->reason, task_tag, cid);
    2511                 :            : 
    2512         [ +  + ]:        387 :         if (conn->sess != NULL) {
    2513         [ +  + ]:        381 :                 if (conn->sess->session_type == SESSION_TYPE_DISCOVERY &&
    2514         [ +  - ]:          6 :                     reqh->reason != ISCSI_LOGOUT_REASON_CLOSE_SESSION) {
    2515                 :          6 :                         SPDK_ERRLOG("Target can accept logout only with reason \"close the session\" "
    2516                 :            :                                     "on discovery session. %d is not acceptable reason.\n",
    2517                 :            :                                     reqh->reason);
    2518                 :          6 :                         return SPDK_ISCSI_CONNECTION_FATAL;
    2519                 :            :                 }
    2520                 :            : 
    2521   [ -  +  -  + ]:        375 :                 SPDK_DEBUGLOG(iscsi,
    2522                 :            :                               "CmdSN=%u, ExpStatSN=%u, StatSN=%u, ExpCmdSN=%u, MaxCmdSN=%u\n",
    2523                 :            :                               pdu->cmd_sn, ExpStatSN, conn->StatSN,
    2524                 :            :                               conn->sess->ExpCmdSN, conn->sess->MaxCmdSN);
    2525                 :            : 
    2526         [ +  + ]:        375 :                 if (pdu->cmd_sn != conn->sess->ExpCmdSN) {
    2527   [ -  +  -  + ]:        194 :                         SPDK_DEBUGLOG(iscsi, "CmdSN(%u) might have dropped\n", pdu->cmd_sn);
    2528                 :            :                         /* ignore error */
    2529                 :            :                 }
    2530                 :            :         } else {
    2531   [ -  +  -  + ]:          6 :                 SPDK_DEBUGLOG(iscsi, "CmdSN=%u, ExpStatSN=%u, StatSN=%u\n",
    2532                 :            :                               pdu->cmd_sn, ExpStatSN, conn->StatSN);
    2533                 :            :         }
    2534                 :            : 
    2535         [ +  + ]:        381 :         if (ExpStatSN != conn->StatSN) {
    2536   [ -  +  -  + ]:        228 :                 SPDK_DEBUGLOG(iscsi, "StatSN(%u/%u) might have dropped\n",
    2537                 :            :                               ExpStatSN, conn->StatSN);
    2538                 :            :                 /* ignore error */
    2539                 :            :         }
    2540                 :            : 
    2541         [ +  + ]:        381 :         if (conn->cid == cid) {
    2542                 :            :                 /* connection or session closed successfully */
    2543                 :        375 :                 response = 0;
    2544                 :        375 :                 iscsi_conn_logout(conn);
    2545                 :            :         } else {
    2546                 :          6 :                 response = 1;
    2547                 :            :         }
    2548                 :            : 
    2549                 :            :         /* response PDU */
    2550                 :        381 :         rsp_pdu = iscsi_get_pdu(conn);
    2551         [ +  + ]:        381 :         if (rsp_pdu == NULL) {
    2552                 :          6 :                 return SPDK_ISCSI_CONNECTION_FATAL;
    2553                 :            :         }
    2554                 :        375 :         rsph = (struct iscsi_bhs_logout_resp *)&rsp_pdu->bhs;
    2555                 :        375 :         rsp_pdu->data = NULL;
    2556                 :        375 :         rsph->opcode = ISCSI_OP_LOGOUT_RSP;
    2557                 :        375 :         rsph->flags |= 0x80; /* bit 0 must be 1 */
    2558                 :        375 :         rsph->response = response;
    2559                 :        375 :         DSET24(rsph->data_segment_len, 0);
    2560                 :        375 :         to_be32(&rsph->itt, task_tag);
    2561                 :            : 
    2562         [ +  + ]:        375 :         if (conn->sess != NULL) {
    2563                 :        369 :                 to_be32(&rsph->stat_sn, conn->StatSN);
    2564                 :        369 :                 conn->StatSN++;
    2565                 :            : 
    2566         [ +  - ]:        369 :                 if (conn->sess->connections == 1) {
    2567                 :        369 :                         conn->sess->MaxCmdSN++;
    2568                 :            :                 }
    2569                 :            : 
    2570                 :        369 :                 to_be32(&rsph->exp_cmd_sn, conn->sess->ExpCmdSN);
    2571                 :        369 :                 to_be32(&rsph->max_cmd_sn, conn->sess->MaxCmdSN);
    2572                 :            :         } else {
    2573                 :          6 :                 to_be32(&rsph->stat_sn, conn->StatSN);
    2574                 :          6 :                 conn->StatSN++;
    2575                 :          6 :                 to_be32(&rsph->exp_cmd_sn, pdu->cmd_sn);
    2576                 :          6 :                 to_be32(&rsph->max_cmd_sn, pdu->cmd_sn);
    2577                 :            :         }
    2578                 :            : 
    2579                 :        375 :         rsph->time_2_wait = 0;
    2580                 :        375 :         rsph->time_2_retain = 0;
    2581                 :            : 
    2582                 :        375 :         iscsi_conn_write_pdu(conn, rsp_pdu, iscsi_conn_logout_pdu_complete, conn);
    2583                 :            : 
    2584                 :        375 :         return 0;
    2585                 :            : }
    2586                 :            : 
    2587                 :            : static int
    2588                 :     538401 : iscsi_send_r2t(struct spdk_iscsi_conn *conn,
    2589                 :            :                struct spdk_iscsi_task *task, int offset,
    2590                 :            :                int len, uint32_t transfer_tag, uint32_t *R2TSN)
    2591                 :            : {
    2592                 :            :         struct spdk_iscsi_pdu *rsp_pdu;
    2593                 :            :         struct iscsi_bhs_r2t *rsph;
    2594                 :            :         uint64_t fmt_lun;
    2595                 :            : 
    2596                 :            :         /* R2T PDU */
    2597                 :     538401 :         rsp_pdu = iscsi_get_pdu(conn);
    2598         [ -  + ]:     538401 :         if (rsp_pdu == NULL) {
    2599                 :          0 :                 return SPDK_ISCSI_CONNECTION_FATAL;
    2600                 :            :         }
    2601                 :     538401 :         rsph = (struct iscsi_bhs_r2t *)&rsp_pdu->bhs;
    2602                 :     538401 :         rsp_pdu->data = NULL;
    2603                 :     538401 :         rsph->opcode = ISCSI_OP_R2T;
    2604                 :     538401 :         rsph->flags |= 0x80; /* bit 0 is default to 1 */
    2605                 :     538401 :         fmt_lun = spdk_scsi_lun_id_int_to_fmt(task->lun_id);
    2606                 :     538401 :         to_be64(&rsph->lun, fmt_lun);
    2607                 :     538401 :         to_be32(&rsph->itt, task->tag);
    2608                 :     538401 :         to_be32(&rsph->ttt, transfer_tag);
    2609                 :            : 
    2610                 :     538401 :         to_be32(&rsph->stat_sn, conn->StatSN);
    2611                 :     538401 :         to_be32(&rsph->exp_cmd_sn, conn->sess->ExpCmdSN);
    2612                 :     538401 :         to_be32(&rsph->max_cmd_sn, conn->sess->MaxCmdSN);
    2613                 :            : 
    2614                 :     538401 :         to_be32(&rsph->r2t_sn, *R2TSN);
    2615                 :     538401 :         *R2TSN += 1;
    2616                 :            : 
    2617                 :     538401 :         task->r2t_datasn = 0; /* next expected datasn to ack */
    2618                 :            : 
    2619                 :     538401 :         to_be32(&rsph->buffer_offset, (uint32_t)offset);
    2620                 :     538401 :         to_be32(&rsph->desired_xfer_len, (uint32_t)len);
    2621                 :     538401 :         task->desired_data_transfer_length = (size_t)len;
    2622                 :            : 
    2623                 :            :         /* we need to hold onto this task/cmd because until the PDU has been
    2624                 :            :          * written out */
    2625                 :     538401 :         rsp_pdu->task = task;
    2626                 :     538401 :         task->scsi.ref++;
    2627                 :            : 
    2628                 :     538401 :         iscsi_conn_write_pdu(conn, rsp_pdu, iscsi_conn_pdu_generic_complete, NULL);
    2629                 :            : 
    2630                 :     538401 :         return 0;
    2631                 :            : }
    2632                 :            : 
    2633                 :            : /* This function is used to remove the r2t pdu from snack_pdu_list by < task, r2t_sn> info */
    2634                 :            : static struct spdk_iscsi_pdu *
    2635                 :         28 : iscsi_remove_r2t_pdu_from_snack_list(struct spdk_iscsi_conn *conn,
    2636                 :            :                                      struct spdk_iscsi_task *task,
    2637                 :            :                                      uint32_t r2t_sn)
    2638                 :            : {
    2639                 :            :         struct spdk_iscsi_pdu *pdu;
    2640                 :            :         struct iscsi_bhs_r2t *r2t_header;
    2641                 :            : 
    2642         [ +  - ]:         28 :         TAILQ_FOREACH(pdu, &conn->snack_pdu_list, tailq) {
    2643         [ +  - ]:         28 :                 if (pdu->bhs.opcode == ISCSI_OP_R2T) {
    2644                 :         28 :                         r2t_header = (struct iscsi_bhs_r2t *)&pdu->bhs;
    2645   [ +  -  +  - ]:         56 :                         if (pdu->task == task &&
    2646                 :         28 :                             from_be32(&r2t_header->r2t_sn) == r2t_sn) {
    2647         [ -  + ]:         28 :                                 TAILQ_REMOVE(&conn->snack_pdu_list, pdu, tailq);
    2648                 :         28 :                                 return pdu;
    2649                 :            :                         }
    2650                 :            :                 }
    2651                 :            :         }
    2652                 :            : 
    2653                 :          0 :         return NULL;
    2654                 :            : }
    2655                 :            : 
    2656                 :            : /* This function is used re-send the r2t packet */
    2657                 :            : static int
    2658                 :         28 : iscsi_send_r2t_recovery(struct spdk_iscsi_conn *conn,
    2659                 :            :                         struct spdk_iscsi_task *task, uint32_t r2t_sn,
    2660                 :            :                         bool send_new_r2tsn)
    2661                 :            : {
    2662                 :            :         struct spdk_iscsi_pdu *pdu;
    2663                 :            :         struct iscsi_bhs_r2t *rsph;
    2664                 :            :         uint32_t transfer_len;
    2665                 :            :         uint32_t len;
    2666                 :            :         int rc;
    2667                 :            : 
    2668                 :            :         /* remove the r2t pdu from the snack_list */
    2669                 :         28 :         pdu = iscsi_remove_r2t_pdu_from_snack_list(conn, task, r2t_sn);
    2670         [ -  + ]:         28 :         if (!pdu) {
    2671   [ #  #  #  # ]:          0 :                 SPDK_DEBUGLOG(iscsi, "No pdu is found\n");
    2672                 :          0 :                 return -1;
    2673                 :            :         }
    2674                 :            : 
    2675                 :            :         /* flag
    2676                 :            :          * false: only need to re-send the old r2t with changing statsn
    2677                 :            :          * true: we send a r2t with new r2tsn
    2678                 :            :          */
    2679         [ +  - ]:         28 :         if (!send_new_r2tsn) {
    2680                 :         28 :                 to_be32(&pdu->bhs.stat_sn, conn->StatSN);
    2681                 :         28 :                 iscsi_conn_write_pdu(conn, pdu, iscsi_conn_pdu_generic_complete, NULL);
    2682                 :            :         } else {
    2683                 :          0 :                 rsph = (struct iscsi_bhs_r2t *)&pdu->bhs;
    2684                 :          0 :                 transfer_len = from_be32(&rsph->desired_xfer_len);
    2685                 :            : 
    2686                 :            :                 /* still need to increase the acked r2tsn */
    2687                 :          0 :                 task->acked_r2tsn++;
    2688                 :          0 :                 len = spdk_min(conn->sess->MaxBurstLength,
    2689                 :            :                                (transfer_len - task->next_expected_r2t_offset));
    2690                 :            : 
    2691                 :            :                 /* remove the old_r2t_pdu */
    2692                 :          0 :                 iscsi_conn_free_pdu(conn, pdu);
    2693                 :            : 
    2694                 :            :                 /* re-send a new r2t pdu */
    2695                 :          0 :                 rc = iscsi_send_r2t(conn, task, task->next_expected_r2t_offset,
    2696                 :            :                                     len, task->ttt, &task->R2TSN);
    2697         [ #  # ]:          0 :                 if (rc < 0) {
    2698                 :          0 :                         return SPDK_ISCSI_CONNECTION_FATAL;
    2699                 :            :                 }
    2700                 :            :         }
    2701                 :            : 
    2702                 :         28 :         return 0;
    2703                 :            : }
    2704                 :            : 
    2705                 :            : static int
    2706                 :     817621 : add_transfer_task(struct spdk_iscsi_conn *conn, struct spdk_iscsi_task *task)
    2707                 :            : {
    2708                 :            :         uint32_t transfer_len;
    2709                 :            :         size_t max_burst_len;
    2710                 :            :         size_t segment_len;
    2711                 :            :         size_t data_len;
    2712                 :            :         int len;
    2713                 :            :         int rc;
    2714                 :            :         int data_out_req;
    2715                 :            : 
    2716                 :     817621 :         transfer_len = task->scsi.transfer_len;
    2717                 :     817621 :         data_len = iscsi_task_get_pdu(task)->data_segment_len;
    2718                 :     817621 :         max_burst_len = conn->sess->MaxBurstLength;
    2719                 :     817621 :         segment_len = SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH;
    2720         [ -  + ]:     817621 :         data_out_req = 1 + (transfer_len - data_len - 1) / segment_len;
    2721                 :     817621 :         task->data_out_cnt = data_out_req;
    2722                 :            : 
    2723                 :            :         /*
    2724                 :            :          * If we already have too many tasks using R2T, then queue this task
    2725                 :            :          *  and start sending R2T for it after some of the tasks using R2T/data
    2726                 :            :          *  out buffers complete.
    2727                 :            :          */
    2728         [ +  + ]:     817621 :         if (conn->pending_r2t >= g_iscsi.MaxR2TPerConnection) {
    2729                 :     279201 :                 TAILQ_INSERT_TAIL(&conn->queued_r2t_tasks, task, link);
    2730                 :     279201 :                 return 0;
    2731                 :            :         }
    2732                 :            : 
    2733                 :     538420 :         conn->data_out_cnt += data_out_req;
    2734                 :     538420 :         conn->pending_r2t++;
    2735                 :            : 
    2736                 :     538420 :         task->next_expected_r2t_offset = data_len;
    2737                 :     538420 :         task->current_r2t_length = 0;
    2738                 :     538420 :         task->R2TSN = 0;
    2739                 :            :         /* According to RFC3720 10.8.5, 0xffffffff is
    2740                 :            :          * reserved for TTT in R2T.
    2741                 :            :          */
    2742         [ -  + ]:     538420 :         if (++conn->ttt == 0xffffffffu) {
    2743                 :          0 :                 conn->ttt = 0;
    2744                 :            :         }
    2745                 :     538420 :         task->ttt = conn->ttt;
    2746                 :            : 
    2747         [ +  + ]:     538456 :         while (data_len != transfer_len) {
    2748                 :     538372 :                 len = spdk_min(max_burst_len, (transfer_len - data_len));
    2749                 :     538372 :                 rc = iscsi_send_r2t(conn, task, data_len, len,
    2750                 :            :                                     task->ttt, &task->R2TSN);
    2751         [ -  + ]:     538372 :                 if (rc < 0) {
    2752                 :          0 :                         SPDK_ERRLOG("iscsi_send_r2t() failed\n");
    2753                 :          0 :                         return rc;
    2754                 :            :                 }
    2755                 :     538372 :                 data_len += len;
    2756                 :     538372 :                 task->next_r2t_offset = data_len;
    2757                 :     538372 :                 task->outstanding_r2t++;
    2758         [ +  + ]:     538372 :                 if (conn->sess->MaxOutstandingR2T == task->outstanding_r2t) {
    2759                 :     538336 :                         break;
    2760                 :            :                 }
    2761                 :            :         }
    2762                 :            : 
    2763                 :     538420 :         TAILQ_INSERT_TAIL(&conn->active_r2t_tasks, task, link);
    2764                 :     538420 :         task->is_r2t_active = true;
    2765                 :     538420 :         return 0;
    2766                 :            : }
    2767                 :            : 
    2768                 :            : /* If there are additional large writes queued for R2Ts, start them now.
    2769                 :            :  *  This is called when a large write is just completed or when multiple LUNs
    2770                 :            :  *  are attached and large write tasks for the specific LUN are cleared.
    2771                 :            :  */
    2772                 :            : static void
    2773                 :     538879 : start_queued_transfer_tasks(struct spdk_iscsi_conn *conn)
    2774                 :            : {
    2775                 :            :         struct spdk_iscsi_task *task, *tmp;
    2776                 :            : 
    2777         [ +  + ]:     818032 :         TAILQ_FOREACH_SAFE(task, &conn->queued_r2t_tasks, link, tmp) {
    2778         [ +  + ]:     557514 :                 if (conn->pending_r2t < g_iscsi.MaxR2TPerConnection) {
    2779         [ +  + ]:     279153 :                         TAILQ_REMOVE(&conn->queued_r2t_tasks, task, link);
    2780                 :     279153 :                         add_transfer_task(conn, task);
    2781                 :            :                 } else {
    2782                 :     278361 :                         break;
    2783                 :            :                 }
    2784                 :            :         }
    2785                 :     538879 : }
    2786                 :            : 
    2787                 :            : bool
    2788                 :     538278 : iscsi_del_transfer_task(struct spdk_iscsi_conn *conn, uint32_t task_tag)
    2789                 :            : {
    2790                 :            :         struct spdk_iscsi_task *task, *tmp;
    2791                 :            : 
    2792         [ +  - ]:     538571 :         TAILQ_FOREACH_SAFE(task, &conn->active_r2t_tasks, link, tmp) {
    2793         [ +  + ]:     538571 :                 if (task->tag == task_tag) {
    2794         [ -  + ]:     538278 :                         assert(conn->data_out_cnt >= task->data_out_cnt);
    2795                 :     538278 :                         conn->data_out_cnt -= task->data_out_cnt;
    2796                 :            : 
    2797         [ -  + ]:     538278 :                         assert(conn->pending_r2t > 0);
    2798                 :     538278 :                         conn->pending_r2t--;
    2799                 :            : 
    2800   [ -  +  -  + ]:     538278 :                         assert(task->is_r2t_active == true);
    2801         [ +  + ]:     538278 :                         TAILQ_REMOVE(&conn->active_r2t_tasks, task, link);
    2802                 :     538278 :                         task->is_r2t_active = false;
    2803                 :     538278 :                         iscsi_task_put(task);
    2804                 :            : 
    2805                 :     538278 :                         start_queued_transfer_tasks(conn);
    2806                 :     538278 :                         return true;
    2807                 :            :                 }
    2808                 :            :         }
    2809                 :          0 :         return false;
    2810                 :            : }
    2811                 :            : 
    2812                 :            : void
    2813                 :        601 : iscsi_clear_all_transfer_task(struct spdk_iscsi_conn *conn,
    2814                 :            :                               struct spdk_scsi_lun *lun,
    2815                 :            :                               struct spdk_iscsi_pdu *pdu)
    2816                 :            : {
    2817                 :            :         struct spdk_iscsi_task *task, *task_tmp;
    2818                 :            :         struct spdk_iscsi_pdu *pdu_tmp;
    2819                 :            : 
    2820         [ +  + ]:        752 :         TAILQ_FOREACH_SAFE(task, &conn->active_r2t_tasks, link, task_tmp) {
    2821                 :        151 :                 pdu_tmp = iscsi_task_get_pdu(task);
    2822   [ +  +  +  +  :        151 :                 if ((lun == NULL || lun == task->scsi.lun) &&
                   +  + ]
    2823         [ +  + ]:         36 :                     (pdu == NULL || spdk_sn32_lt(pdu_tmp->cmd_sn, pdu->cmd_sn))) {
    2824                 :        112 :                         task->outstanding_r2t = 0;
    2825                 :        112 :                         task->next_r2t_offset = 0;
    2826                 :        112 :                         task->next_expected_r2t_offset = 0;
    2827                 :        112 :                         task->current_data_offset = 0;
    2828         [ -  + ]:        112 :                         assert(conn->data_out_cnt >= task->data_out_cnt);
    2829                 :        112 :                         conn->data_out_cnt -= task->data_out_cnt;
    2830         [ -  + ]:        112 :                         assert(conn->pending_r2t > 0);
    2831                 :        112 :                         conn->pending_r2t--;
    2832                 :            : 
    2833         [ +  + ]:        112 :                         TAILQ_REMOVE(&conn->active_r2t_tasks, task, link);
    2834                 :        112 :                         task->is_r2t_active = false;
    2835   [ +  +  +  - ]:        112 :                         if (lun != NULL && spdk_scsi_lun_is_removing(lun)) {
    2836                 :         40 :                                 spdk_scsi_task_process_null_lun(&task->scsi);
    2837                 :         40 :                                 iscsi_task_response(conn, task);
    2838                 :            :                         }
    2839                 :        112 :                         iscsi_task_put(task);
    2840                 :            :                 }
    2841                 :            :         }
    2842                 :            : 
    2843         [ +  + ]:        665 :         TAILQ_FOREACH_SAFE(task, &conn->queued_r2t_tasks, link, task_tmp) {
    2844                 :         64 :                 pdu_tmp = iscsi_task_get_pdu(task);
    2845   [ +  -  +  +  :         64 :                 if ((lun == NULL || lun == task->scsi.lun) &&
                   -  + ]
    2846         [ #  # ]:          0 :                     (pdu == NULL || spdk_sn32_lt(pdu_tmp->cmd_sn, pdu->cmd_sn))) {
    2847         [ +  + ]:         42 :                         TAILQ_REMOVE(&conn->queued_r2t_tasks, task, link);
    2848                 :         42 :                         task->is_r2t_active = false;
    2849   [ +  -  +  - ]:         42 :                         if (lun != NULL && spdk_scsi_lun_is_removing(lun)) {
    2850                 :         42 :                                 spdk_scsi_task_process_null_lun(&task->scsi);
    2851                 :         42 :                                 iscsi_task_response(conn, task);
    2852                 :            :                         }
    2853                 :         42 :                         iscsi_task_put(task);
    2854                 :            :                 }
    2855                 :            :         }
    2856                 :            : 
    2857                 :        601 :         start_queued_transfer_tasks(conn);
    2858                 :        601 : }
    2859                 :            : 
    2860                 :            : static struct spdk_iscsi_task *
    2861                 :    1935322 : get_transfer_task(struct spdk_iscsi_conn *conn, uint32_t transfer_tag)
    2862                 :            : {
    2863                 :            :         struct spdk_iscsi_task *task;
    2864                 :            : 
    2865         [ +  + ]:    2236477 :         TAILQ_FOREACH(task, &conn->active_r2t_tasks, link) {
    2866         [ +  + ]:    2232462 :                 if (task->ttt == transfer_tag) {
    2867                 :    1931307 :                         return task;
    2868                 :            :                 }
    2869                 :            :         }
    2870                 :            : 
    2871                 :       4015 :         return NULL;
    2872                 :            : }
    2873                 :            : 
    2874                 :            : static void
    2875                 :   12871333 : iscsi_conn_datain_pdu_complete(void *arg)
    2876                 :            : {
    2877                 :   12871333 :         struct spdk_iscsi_conn *conn = arg;
    2878                 :            : 
    2879                 :   12871333 :         iscsi_conn_handle_queued_datain_tasks(conn);
    2880                 :   12871333 : }
    2881                 :            : 
    2882                 :            : static int
    2883                 :   12871345 : iscsi_send_datain(struct spdk_iscsi_conn *conn,
    2884                 :            :                   struct spdk_iscsi_task *task, int datain_flag,
    2885                 :            :                   int residual_len, int offset, int DataSN, int len)
    2886                 :            : {
    2887                 :            :         struct spdk_iscsi_pdu *rsp_pdu;
    2888                 :            :         struct iscsi_bhs_data_in *rsph;
    2889                 :            :         uint32_t task_tag;
    2890                 :            :         uint32_t transfer_tag;
    2891                 :            :         int F_bit, U_bit, O_bit, S_bit;
    2892                 :            :         struct spdk_iscsi_task *primary;
    2893                 :            :         struct spdk_scsi_lun *lun_dev;
    2894                 :            : 
    2895                 :   12871345 :         primary = iscsi_task_get_primary(task);
    2896                 :            : 
    2897                 :            :         /* DATA PDU */
    2898                 :   12871345 :         rsp_pdu = iscsi_get_pdu(conn);
    2899                 :   12871345 :         rsph = (struct iscsi_bhs_data_in *)&rsp_pdu->bhs;
    2900                 :   12871345 :         rsp_pdu->data = task->scsi.iovs[0].iov_base + offset;
    2901                 :   12871345 :         rsp_pdu->data_buf_len = task->scsi.iovs[0].iov_len - offset;
    2902                 :   12871345 :         rsp_pdu->data_valid_bytes = len;
    2903                 :   12871345 :         rsp_pdu->data_from_mempool = true;
    2904                 :            : 
    2905                 :   12871345 :         task_tag = task->tag;
    2906                 :   12871345 :         transfer_tag = 0xffffffffU;
    2907                 :            : 
    2908                 :   12871345 :         F_bit = datain_flag & ISCSI_FLAG_FINAL;
    2909                 :   12871345 :         O_bit = datain_flag & ISCSI_DATAIN_OVERFLOW;
    2910                 :   12871345 :         U_bit = datain_flag & ISCSI_DATAIN_UNDERFLOW;
    2911                 :   12871345 :         S_bit = datain_flag & ISCSI_DATAIN_STATUS;
    2912                 :            : 
    2913                 :            :         /*
    2914                 :            :          * we need to hold onto this task/cmd because until the
    2915                 :            :          * PDU has been written out
    2916                 :            :          */
    2917                 :   12871345 :         rsp_pdu->task = task;
    2918                 :   12871345 :         task->scsi.ref++;
    2919                 :            : 
    2920                 :   12871345 :         rsph->opcode = ISCSI_OP_SCSI_DATAIN;
    2921                 :            : 
    2922         [ +  + ]:   12871345 :         if (F_bit) {
    2923                 :   12871321 :                 rsph->flags |= ISCSI_FLAG_FINAL;
    2924                 :            :         }
    2925                 :            : 
    2926                 :            :         /* we leave the A_bit clear */
    2927                 :            : 
    2928   [ +  +  +  + ]:   12871345 :         if (F_bit && S_bit)  {
    2929         [ +  + ]:   12608366 :                 if (O_bit) {
    2930                 :          4 :                         rsph->flags |= ISCSI_DATAIN_OVERFLOW;
    2931                 :            :                 }
    2932                 :            : 
    2933         [ +  + ]:   12608366 :                 if (U_bit) {
    2934                 :       4429 :                         rsph->flags |= ISCSI_DATAIN_UNDERFLOW;
    2935                 :            :                 }
    2936                 :            :         }
    2937                 :            : 
    2938         [ +  + ]:   12871345 :         if (S_bit) {
    2939                 :   12608366 :                 rsph->flags |= ISCSI_DATAIN_STATUS;
    2940                 :   12608366 :                 rsph->status = task->scsi.status;
    2941                 :            :         }
    2942                 :            : 
    2943                 :   12871345 :         DSET24(rsph->data_segment_len, len);
    2944                 :            : 
    2945                 :   12871345 :         to_be32(&rsph->itt, task_tag);
    2946                 :   12871345 :         to_be32(&rsph->ttt, transfer_tag);
    2947                 :            : 
    2948         [ +  + ]:   12871345 :         if (S_bit) {
    2949                 :   12608366 :                 to_be32(&rsph->stat_sn, conn->StatSN);
    2950                 :   12608366 :                 conn->StatSN++;
    2951                 :            :         }
    2952                 :            : 
    2953   [ +  +  +  +  :   12871345 :         if (F_bit && S_bit && !iscsi_task_is_immediate(primary)) {
                   +  + ]
    2954                 :   12608364 :                 conn->sess->MaxCmdSN++;
    2955                 :            :         }
    2956                 :            : 
    2957                 :   12871345 :         to_be32(&rsph->exp_cmd_sn, conn->sess->ExpCmdSN);
    2958                 :   12871345 :         to_be32(&rsph->max_cmd_sn, conn->sess->MaxCmdSN);
    2959                 :            : 
    2960                 :   12871345 :         to_be32(&rsph->data_sn, DataSN);
    2961                 :            : 
    2962         [ +  + ]:   12871345 :         if (conn->sess->ErrorRecoveryLevel >= 1) {
    2963                 :       1248 :                 primary->datain_datasn = DataSN;
    2964                 :            :         }
    2965                 :   12871345 :         DataSN++;
    2966                 :            : 
    2967                 :   12871345 :         offset += task->scsi.offset;
    2968                 :   12871345 :         to_be32(&rsph->buffer_offset, (uint32_t)offset);
    2969                 :            : 
    2970   [ +  +  +  + ]:   12871345 :         if (F_bit && S_bit) {
    2971                 :   12608366 :                 to_be32(&rsph->res_cnt, residual_len);
    2972                 :            :         }
    2973                 :            : 
    2974                 :   12871345 :         lun_dev = spdk_scsi_dev_get_lun(conn->dev, task->lun_id);
    2975         [ +  + ]:   12871345 :         if (spdk_likely(lun_dev != NULL)) {
    2976         [ -  + ]:   12871337 :                 if (spdk_unlikely(spdk_scsi_lun_get_dif_ctx(lun_dev, &task->scsi,
    2977                 :            :                                   &rsp_pdu->dif_ctx))) {
    2978                 :          0 :                         rsp_pdu->dif_insert_or_strip = true;
    2979                 :            :                 }
    2980                 :            :         }
    2981                 :            : 
    2982                 :   12871345 :         iscsi_conn_write_pdu(conn, rsp_pdu, iscsi_conn_datain_pdu_complete, conn);
    2983                 :            : 
    2984                 :   12871345 :         return DataSN;
    2985                 :            : }
    2986                 :            : 
    2987                 :            : static int
    2988                 :   12875428 : iscsi_transfer_in(struct spdk_iscsi_conn *conn, struct spdk_iscsi_task *task)
    2989                 :            : {
    2990                 :            :         uint32_t DataSN;
    2991                 :            :         uint32_t transfer_len;
    2992                 :            :         uint32_t data_len;
    2993                 :            :         uint32_t segment_len;
    2994                 :            :         uint32_t offset;
    2995                 :   12875428 :         uint32_t residual_len = 0;
    2996                 :            :         int sent_status;
    2997                 :            :         uint32_t len;
    2998                 :   12875428 :         int datain_flag = 0;
    2999                 :            :         int datain_seq_cnt;
    3000                 :            :         int i;
    3001                 :            :         uint32_t sequence_end;
    3002                 :            :         struct spdk_iscsi_task *primary;
    3003                 :            : 
    3004                 :   12875428 :         primary = iscsi_task_get_primary(task);
    3005                 :   12875428 :         segment_len = conn->MaxRecvDataSegmentLength;
    3006                 :   12875428 :         data_len = task->scsi.data_transferred;
    3007                 :   12875428 :         transfer_len = task->scsi.length;
    3008                 :            : 
    3009         [ +  + ]:   12875428 :         if (task->scsi.status != SPDK_SCSI_STATUS_GOOD) {
    3010                 :       4165 :                 return 0;
    3011                 :            :         }
    3012                 :            : 
    3013         [ +  + ]:   12871263 :         if (data_len < transfer_len) {
    3014                 :            :                 /* underflow */
    3015   [ -  +  -  + ]:       4441 :                 SPDK_DEBUGLOG(iscsi, "Underflow %u/%u\n", data_len, transfer_len);
    3016                 :       4441 :                 residual_len = transfer_len - data_len;
    3017                 :       4441 :                 transfer_len = data_len;
    3018                 :       4441 :                 datain_flag |= ISCSI_DATAIN_UNDERFLOW;
    3019         [ +  + ]:   12866822 :         } else if (data_len > transfer_len) {
    3020                 :            :                 /* overflow */
    3021   [ -  +  -  + ]:          4 :                 SPDK_DEBUGLOG(iscsi, "Overflow %u/%u\n", data_len, transfer_len);
    3022                 :          4 :                 residual_len = data_len - transfer_len;
    3023                 :          4 :                 datain_flag |= ISCSI_DATAIN_OVERFLOW;
    3024                 :            :         } else {
    3025   [ -  +  -  + ]:   12866818 :                 SPDK_DEBUGLOG(iscsi, "Transfer %u\n", transfer_len);
    3026                 :   12866818 :                 residual_len = 0;
    3027                 :            :         }
    3028                 :            : 
    3029                 :   12871263 :         DataSN = primary->datain_datasn;
    3030                 :   12871263 :         sent_status = 0;
    3031                 :            : 
    3032                 :            :         /* calculate the number of sequences for all data-in pdus */
    3033         [ -  + ]:   12871263 :         datain_seq_cnt = 1 + ((transfer_len - 1) / (int)conn->sess->MaxBurstLength);
    3034         [ +  + ]:   34155768 :         for (i = 0; i < datain_seq_cnt; i++) {
    3035                 :   21284505 :                 offset = i * conn->sess->MaxBurstLength;
    3036                 :   21284505 :                 sequence_end = spdk_min(((i + 1) * conn->sess->MaxBurstLength),
    3037                 :            :                                         transfer_len);
    3038                 :            : 
    3039                 :            :                 /* send data split by segment_len */
    3040         [ +  + ]:   34155850 :                 for (; offset < sequence_end; offset += segment_len) {
    3041                 :   12871345 :                         len = spdk_min(segment_len, (sequence_end - offset));
    3042                 :            : 
    3043                 :   12871345 :                         datain_flag &= ~(ISCSI_FLAG_FINAL | ISCSI_DATAIN_STATUS);
    3044                 :            : 
    3045         [ +  + ]:   12871345 :                         if (offset + len == sequence_end) {
    3046                 :            :                                 /* last PDU in a sequence */
    3047                 :   12871321 :                                 datain_flag |= ISCSI_FLAG_FINAL;
    3048         [ +  + ]:   12871321 :                                 if (task->scsi.sense_data_len == 0) {
    3049                 :            :                                         /* The last pdu in all data-in pdus */
    3050         [ +  + ]:   12871315 :                                         if ((offset + len) == transfer_len &&
    3051         [ +  + ]:   12871249 :                                             (primary->bytes_completed == primary->scsi.transfer_len)) {
    3052                 :   12608366 :                                                 datain_flag |= ISCSI_DATAIN_STATUS;
    3053                 :   12608366 :                                                 sent_status = 1;
    3054                 :            :                                         }
    3055                 :            :                                 }
    3056                 :            :                         }
    3057                 :            : 
    3058   [ -  +  -  + ]:   12871345 :                         SPDK_DEBUGLOG(iscsi, "Transfer=%d, Offset=%d, Len=%d\n",
    3059                 :            :                                       sequence_end, offset, len);
    3060   [ -  +  -  + ]:   12871345 :                         SPDK_DEBUGLOG(iscsi, "StatSN=%u, DataSN=%u, Offset=%u, Len=%d\n",
    3061                 :            :                                       conn->StatSN, DataSN, offset, len);
    3062                 :            : 
    3063                 :   12871345 :                         DataSN = iscsi_send_datain(conn, task, datain_flag, residual_len,
    3064                 :            :                                                    offset, DataSN, len);
    3065                 :            :                 }
    3066                 :            :         }
    3067                 :            : 
    3068         [ +  + ]:   12871263 :         if (task != primary) {
    3069                 :     484746 :                 primary->scsi.data_transferred += task->scsi.data_transferred;
    3070                 :            :         }
    3071                 :   12871263 :         primary->datain_datasn = DataSN;
    3072                 :            : 
    3073                 :   12871263 :         return sent_status;
    3074                 :            : }
    3075                 :            : 
    3076                 :            : void
    3077                 :   23808531 : iscsi_task_response(struct spdk_iscsi_conn *conn,
    3078                 :            :                     struct spdk_iscsi_task *task)
    3079                 :            : {
    3080                 :            :         struct spdk_iscsi_pdu *rsp_pdu;
    3081                 :            :         struct iscsi_bhs_scsi_resp *rsph;
    3082                 :            :         uint32_t task_tag;
    3083                 :            :         uint32_t transfer_len;
    3084                 :            :         size_t residual_len;
    3085                 :            :         size_t data_len;
    3086                 :            :         int O_bit, U_bit;
    3087                 :            :         int rc;
    3088                 :            :         struct spdk_iscsi_task *primary;
    3089                 :            : 
    3090                 :   23808531 :         primary = iscsi_task_get_primary(task);
    3091                 :            : 
    3092                 :   23808531 :         transfer_len = primary->scsi.transfer_len;
    3093                 :   23808531 :         task_tag = task->tag;
    3094                 :            : 
    3095                 :            :         /* transfer data from logical unit */
    3096                 :            :         /* (direction is view of initiator side) */
    3097         [ +  + ]:   23808531 :         if (iscsi_task_is_read(primary)) {
    3098                 :   12875428 :                 rc = iscsi_transfer_in(conn, task);
    3099         [ +  + ]:   12875428 :                 if (rc > 0) {
    3100                 :            :                         /* sent status by last DATAIN PDU */
    3101                 :   12608366 :                         return;
    3102                 :            :                 }
    3103                 :            : 
    3104         [ +  + ]:     267062 :                 if (primary->bytes_completed != primary->scsi.transfer_len) {
    3105                 :     262883 :                         return;
    3106                 :            :                 }
    3107                 :            :         }
    3108                 :            : 
    3109                 :   10937282 :         O_bit = U_bit = 0;
    3110                 :   10937282 :         residual_len = 0;
    3111                 :   10937282 :         data_len = primary->scsi.data_transferred;
    3112                 :            : 
    3113         [ +  + ]:   10937282 :         if ((transfer_len != 0) &&
    3114         [ +  + ]:    9929995 :             (task->scsi.status == SPDK_SCSI_STATUS_GOOD)) {
    3115         [ +  + ]:    9919742 :                 if (data_len < transfer_len) {
    3116                 :            :                         /* underflow */
    3117   [ -  +  -  + ]:     263549 :                         SPDK_DEBUGLOG(iscsi, "Underflow %zu/%u\n", data_len, transfer_len);
    3118                 :     263549 :                         residual_len = transfer_len - data_len;
    3119                 :     263549 :                         U_bit = 1;
    3120         [ -  + ]:    9656193 :                 } else if (data_len > transfer_len) {
    3121                 :            :                         /* overflow */
    3122   [ #  #  #  # ]:          0 :                         SPDK_DEBUGLOG(iscsi, "Overflow %zu/%u\n", data_len, transfer_len);
    3123                 :          0 :                         residual_len = data_len - transfer_len;
    3124                 :          0 :                         O_bit = 1;
    3125                 :            :                 } else {
    3126   [ -  +  -  + ]:    9656193 :                         SPDK_DEBUGLOG(iscsi, "Transfer %u\n", transfer_len);
    3127                 :            :                 }
    3128                 :            :         }
    3129                 :            : 
    3130                 :            :         /* response PDU */
    3131                 :   10937282 :         rsp_pdu = iscsi_get_pdu(conn);
    3132         [ -  + ]:   10937282 :         assert(rsp_pdu != NULL);
    3133                 :   10937282 :         rsph = (struct iscsi_bhs_scsi_resp *)&rsp_pdu->bhs;
    3134         [ -  + ]:   10937282 :         assert(task->scsi.sense_data_len <= sizeof(rsp_pdu->sense.data));
    3135   [ -  +  -  + ]:   10937282 :         memcpy(rsp_pdu->sense.data, task->scsi.sense_data, task->scsi.sense_data_len);
    3136                 :   10937282 :         to_be16(&rsp_pdu->sense.length, task->scsi.sense_data_len);
    3137                 :   10937282 :         rsp_pdu->data = (uint8_t *)&rsp_pdu->sense;
    3138                 :   10937282 :         rsp_pdu->data_from_mempool = true;
    3139                 :            : 
    3140                 :            :         /*
    3141                 :            :          * we need to hold onto this task/cmd because until the
    3142                 :            :          * PDU has been written out
    3143                 :            :          */
    3144                 :   10937282 :         rsp_pdu->task = task;
    3145                 :   10937282 :         task->scsi.ref++;
    3146                 :            : 
    3147                 :   10937282 :         rsph->opcode = ISCSI_OP_SCSI_RSP;
    3148                 :   10937282 :         rsph->flags |= 0x80; /* bit 0 is default to 1 */
    3149                 :            : 
    3150         [ -  + ]:   10937282 :         if (O_bit) {
    3151                 :          0 :                 rsph->flags |= ISCSI_SCSI_OVERFLOW;
    3152                 :            :         }
    3153                 :            : 
    3154         [ +  + ]:   10937282 :         if (U_bit) {
    3155                 :     263549 :                 rsph->flags |= ISCSI_SCSI_UNDERFLOW;
    3156                 :            :         }
    3157                 :            : 
    3158                 :   10937282 :         rsph->status = task->scsi.status;
    3159         [ +  + ]:   10937282 :         if (task->scsi.sense_data_len) {
    3160                 :            :                 /* SenseLength (2 bytes) + SenseData  */
    3161                 :      10745 :                 DSET24(rsph->data_segment_len, 2 + task->scsi.sense_data_len);
    3162                 :            :         }
    3163                 :   10937282 :         to_be32(&rsph->itt, task_tag);
    3164                 :            : 
    3165                 :   10937282 :         to_be32(&rsph->stat_sn, conn->StatSN);
    3166                 :   10937282 :         conn->StatSN++;
    3167                 :            : 
    3168         [ +  + ]:   10937282 :         if (!iscsi_task_is_immediate(primary)) {
    3169                 :   10933594 :                 conn->sess->MaxCmdSN++;
    3170                 :            :         }
    3171                 :            : 
    3172                 :   10937282 :         to_be32(&rsph->exp_cmd_sn, conn->sess->ExpCmdSN);
    3173                 :   10937282 :         to_be32(&rsph->max_cmd_sn, conn->sess->MaxCmdSN);
    3174                 :            : 
    3175                 :   10937282 :         to_be32(&rsph->bi_read_res_cnt, 0);
    3176                 :   10937282 :         to_be32(&rsph->res_cnt, residual_len);
    3177                 :            : 
    3178                 :   10937282 :         iscsi_conn_write_pdu(conn, rsp_pdu, iscsi_conn_pdu_generic_complete, NULL);
    3179                 :            : }
    3180                 :            : 
    3181                 :            : /*
    3182                 :            :  *  This function compare the input pdu's bhs with the pdu's bhs associated by
    3183                 :            :  *  active_r2t_tasks and queued_r2t_tasks in a connection
    3184                 :            :  */
    3185                 :            : static bool
    3186                 :        162 : iscsi_compare_pdu_bhs_within_existed_r2t_tasks(struct spdk_iscsi_conn *conn,
    3187                 :            :                 struct spdk_iscsi_pdu *pdu)
    3188                 :            : {
    3189                 :            :         struct spdk_iscsi_task  *task;
    3190                 :            : 
    3191         [ +  + ]:        198 :         TAILQ_FOREACH(task, &conn->active_r2t_tasks, link) {
    3192   [ -  +  -  +  :         42 :                 if (!memcmp(&pdu->bhs, iscsi_task_get_bhs(task), ISCSI_BHS_LEN)) {
                   +  + ]
    3193                 :          6 :                         return true;
    3194                 :            :                 }
    3195                 :            :         }
    3196                 :            : 
    3197         [ -  + ]:        156 :         TAILQ_FOREACH(task, &conn->queued_r2t_tasks, link) {
    3198   [ #  #  #  #  :          0 :                 if (!memcmp(&pdu->bhs, iscsi_task_get_bhs(task), ISCSI_BHS_LEN)) {
                   #  # ]
    3199                 :          0 :                         return true;
    3200                 :            :                 }
    3201                 :            :         }
    3202                 :            : 
    3203                 :        156 :         return false;
    3204                 :            : }
    3205                 :            : 
    3206                 :            : void
    3207                 :   24232725 : iscsi_queue_task(struct spdk_iscsi_conn *conn, struct spdk_iscsi_task *task)
    3208                 :            : {
    3209   [ +  +  +  + ]:   24232725 :         spdk_trace_record(TRACE_ISCSI_TASK_QUEUE, conn->id, task->scsi.length,
    3210                 :            :                           (uintptr_t)task, (uintptr_t)task->pdu);
    3211                 :   24232725 :         task->is_queued = true;
    3212                 :   24232725 :         spdk_scsi_dev_queue_task(conn->dev, &task->scsi);
    3213                 :   24232725 : }
    3214                 :            : 
    3215                 :            : static int
    3216                 :   12612247 : iscsi_pdu_payload_op_scsi_read(struct spdk_iscsi_conn *conn, struct spdk_iscsi_task *task)
    3217                 :            : {
    3218         [ +  + ]:   12612247 :         if (task->scsi.transfer_len <= SPDK_BDEV_LARGE_BUF_MAX_SIZE) {
    3219                 :   12390384 :                 task->parent = NULL;
    3220                 :   12390384 :                 task->scsi.offset = 0;
    3221                 :   12390384 :                 task->scsi.length = task->scsi.transfer_len;
    3222                 :   12390384 :                 spdk_scsi_task_set_data(&task->scsi, NULL, 0);
    3223                 :            : 
    3224                 :   12390384 :                 iscsi_queue_task(conn, task);
    3225                 :   12390384 :                 return 0;
    3226                 :            :         } else {
    3227                 :     221863 :                 TAILQ_INIT(&task->subtask_list);
    3228                 :     221863 :                 task->current_data_offset = 0;
    3229                 :     221863 :                 TAILQ_INSERT_TAIL(&conn->queued_datain_tasks, task, link);
    3230                 :            : 
    3231                 :     221863 :                 return iscsi_conn_handle_queued_datain_tasks(conn);
    3232                 :            :         }
    3233                 :            : }
    3234                 :            : 
    3235                 :            : static int
    3236                 :     966762 : iscsi_submit_write_subtask(struct spdk_iscsi_conn *conn, struct spdk_iscsi_task *task,
    3237                 :            :                            struct spdk_iscsi_pdu *pdu, struct spdk_mobj *mobj)
    3238                 :            : {
    3239                 :            :         struct spdk_iscsi_task *subtask;
    3240                 :            : 
    3241                 :     966762 :         subtask = iscsi_task_get(conn, task, iscsi_task_cpl);
    3242         [ -  + ]:     966762 :         if (subtask == NULL) {
    3243                 :          0 :                 SPDK_ERRLOG("Unable to acquire subtask\n");
    3244                 :          0 :                 return SPDK_ISCSI_CONNECTION_FATAL;
    3245                 :            :         }
    3246                 :     966762 :         subtask->scsi.offset = task->current_data_offset;
    3247                 :     966762 :         subtask->scsi.length = mobj->data_len;
    3248                 :     966762 :         iscsi_task_associate_pdu(subtask, pdu);
    3249                 :            : 
    3250                 :     966762 :         task->current_data_offset += mobj->data_len;
    3251                 :            : 
    3252   [ +  +  +  - ]:     966762 :         if (spdk_likely(!pdu->dif_insert_or_strip)) {
    3253                 :     966762 :                 spdk_scsi_task_set_data(&subtask->scsi, mobj->buf, mobj->data_len);
    3254                 :            :         } else {
    3255                 :          0 :                 spdk_scsi_task_set_data(&subtask->scsi, mobj->buf, pdu->data_buf_len);
    3256                 :            :         }
    3257                 :            : 
    3258                 :     966762 :         iscsi_queue_task(conn, subtask);
    3259                 :     966762 :         return 0;
    3260                 :            : }
    3261                 :            : 
    3262                 :            : static int
    3263                 :    9921938 : iscsi_pdu_payload_op_scsi_write(struct spdk_iscsi_conn *conn, struct spdk_iscsi_task *task)
    3264                 :            : {
    3265                 :            :         struct spdk_iscsi_pdu *pdu;
    3266                 :            :         struct iscsi_bhs_scsi_req *reqh;
    3267                 :            :         uint32_t transfer_len;
    3268                 :            :         struct spdk_mobj *mobj;
    3269                 :            :         int rc;
    3270                 :            : 
    3271                 :    9921938 :         pdu = iscsi_task_get_pdu(task);
    3272                 :    9921938 :         reqh = (struct iscsi_bhs_scsi_req *)&pdu->bhs;
    3273                 :            : 
    3274                 :    9921938 :         transfer_len = task->scsi.transfer_len;
    3275                 :            : 
    3276         [ +  - ]:    9921938 :         if (reqh->final_bit &&
    3277         [ +  + ]:    9921938 :             pdu->data_segment_len < transfer_len) {
    3278                 :            :                 /* needs R2T */
    3279                 :     538378 :                 rc = add_transfer_task(conn, task);
    3280         [ -  + ]:     538378 :                 if (rc < 0) {
    3281                 :          0 :                         SPDK_ERRLOG("add_transfer_task() failed\n");
    3282                 :          0 :                         iscsi_task_put(task);
    3283                 :          0 :                         return SPDK_ISCSI_CONNECTION_FATAL;
    3284                 :            :                 }
    3285                 :            : 
    3286                 :            :                 /* immediate writes */
    3287         [ +  + ]:     538378 :                 if (pdu->data_segment_len != 0) {
    3288                 :     538222 :                         mobj = pdu->mobj[0];
    3289         [ -  + ]:     538222 :                         assert(mobj != NULL);
    3290                 :            : 
    3291   [ +  +  +  - ]:     538222 :                         if (!pdu->dif_insert_or_strip &&
    3292         [ +  - ]:     538222 :                             mobj->data_len < SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH) {
    3293                 :            :                                 /* continue aggregation until the first data buffer is full. */
    3294                 :     538222 :                                 iscsi_task_set_mobj(task, mobj);
    3295                 :     538222 :                                 pdu->mobj[0] = NULL;
    3296                 :            :                         } else {
    3297                 :            :                                 /* we are doing the first partial write task */
    3298                 :          0 :                                 rc = iscsi_submit_write_subtask(conn, task, pdu, mobj);
    3299         [ #  # ]:          0 :                                 if (rc < 0) {
    3300                 :          0 :                                         iscsi_task_put(task);
    3301                 :          0 :                                         return SPDK_ISCSI_CONNECTION_FATAL;
    3302                 :            :                                 }
    3303                 :            :                         }
    3304                 :            :                 }
    3305                 :     538378 :                 return 0;
    3306                 :            :         }
    3307                 :            : 
    3308         [ +  - ]:    9383560 :         if (pdu->data_segment_len == transfer_len) {
    3309                 :            :                 /* we are doing small writes with no R2T */
    3310   [ -  +  +  - ]:    9383560 :                 if (spdk_likely(!pdu->dif_insert_or_strip)) {
    3311                 :    9383560 :                         spdk_scsi_task_set_data(&task->scsi, pdu->data, pdu->data_segment_len);
    3312                 :            :                 } else {
    3313                 :          0 :                         spdk_scsi_task_set_data(&task->scsi, pdu->data, pdu->data_buf_len);
    3314                 :            :                 }
    3315                 :    9383560 :                 task->scsi.length = transfer_len;
    3316                 :            :         }
    3317                 :            : 
    3318                 :    9383560 :         iscsi_queue_task(conn, task);
    3319                 :    9383560 :         return 0;
    3320                 :            : }
    3321                 :            : 
    3322                 :            : static int
    3323                 :   23545753 : iscsi_pdu_hdr_op_scsi(struct spdk_iscsi_conn *conn, struct spdk_iscsi_pdu *pdu)
    3324                 :            : {
    3325                 :            :         struct spdk_iscsi_task  *task;
    3326                 :            :         struct spdk_scsi_dev    *dev;
    3327                 :            :         uint8_t *cdb;
    3328                 :            :         uint64_t lun;
    3329                 :            :         uint32_t task_tag;
    3330                 :            :         uint32_t transfer_len;
    3331                 :            :         int R_bit, W_bit;
    3332                 :            :         int lun_i;
    3333                 :            :         struct iscsi_bhs_scsi_req *reqh;
    3334                 :            : 
    3335         [ +  + ]:   23545753 :         if (conn->sess->session_type != SESSION_TYPE_NORMAL) {
    3336                 :         12 :                 SPDK_ERRLOG("ISCSI_OP_SCSI not allowed in discovery and invalid session\n");
    3337                 :         12 :                 return SPDK_ISCSI_CONNECTION_FATAL;
    3338                 :            :         }
    3339                 :            : 
    3340                 :   23545741 :         reqh = (struct iscsi_bhs_scsi_req *)&pdu->bhs;
    3341                 :            : 
    3342                 :   23545741 :         R_bit = reqh->read_bit;
    3343                 :   23545741 :         W_bit = reqh->write_bit;
    3344                 :   23545741 :         lun = from_be64(&reqh->lun);
    3345                 :   23545741 :         task_tag = from_be32(&reqh->itt);
    3346                 :   23545741 :         transfer_len = from_be32(&reqh->expected_data_xfer_len);
    3347                 :   23545741 :         cdb = reqh->cdb;
    3348                 :            : 
    3349   [ -  +  -  + ]:   23545741 :         SPDK_LOGDUMP(iscsi, "CDB", cdb, 16);
    3350                 :            : 
    3351                 :   23545741 :         task = iscsi_task_get(conn, NULL, iscsi_task_cpl);
    3352         [ -  + ]:   23545741 :         if (!task) {
    3353                 :          0 :                 SPDK_ERRLOG("Unable to acquire task\n");
    3354                 :          0 :                 return SPDK_ISCSI_CONNECTION_FATAL;
    3355                 :            :         }
    3356                 :            : 
    3357                 :   23545741 :         iscsi_task_associate_pdu(task, pdu);
    3358                 :   23545741 :         lun_i = spdk_scsi_lun_id_fmt_to_int(lun);
    3359                 :   23545741 :         task->lun_id = lun_i;
    3360                 :   23545741 :         dev = conn->dev;
    3361                 :   23545741 :         task->scsi.lun = spdk_scsi_dev_get_lun(dev, lun_i);
    3362                 :            : 
    3363   [ +  +  +  + ]:   23545741 :         if ((R_bit != 0) && (W_bit != 0)) {
    3364                 :          6 :                 SPDK_ERRLOG("Bidirectional CDB is not supported\n");
    3365                 :          6 :                 iscsi_task_put(task);
    3366                 :          6 :                 return SPDK_ISCSI_CONNECTION_FATAL;
    3367                 :            :         }
    3368                 :            : 
    3369                 :   23545735 :         task->scsi.cdb = cdb;
    3370                 :   23545735 :         task->tag = task_tag;
    3371                 :   23545735 :         task->scsi.transfer_len = transfer_len;
    3372                 :   23545735 :         task->scsi.target_port = conn->target_port;
    3373                 :   23545735 :         task->scsi.initiator_port = conn->initiator_port;
    3374                 :   23545735 :         task->parent = NULL;
    3375                 :   23545735 :         task->scsi.status = SPDK_SCSI_STATUS_GOOD;
    3376                 :            : 
    3377         [ +  + ]:   23545735 :         if (task->scsi.lun == NULL) {
    3378                 :       4214 :                 spdk_scsi_task_process_null_lun(&task->scsi);
    3379                 :       4214 :                 iscsi_task_cpl(&task->scsi);
    3380                 :       4214 :                 return 0;
    3381                 :            :         }
    3382                 :            : 
    3383                 :            :         /* no bi-directional support */
    3384         [ +  + ]:   23541521 :         if (R_bit) {
    3385                 :   12612253 :                 task->scsi.dxfer_dir = SPDK_SCSI_DIR_FROM_DEV;
    3386         [ +  + ]:   10929268 :         } else if (W_bit) {
    3387                 :    9921978 :                 task->scsi.dxfer_dir = SPDK_SCSI_DIR_TO_DEV;
    3388                 :            : 
    3389   [ +  +  +  + ]:    9922140 :                 if ((conn->sess->ErrorRecoveryLevel >= 1) &&
    3390                 :        162 :                     (iscsi_compare_pdu_bhs_within_existed_r2t_tasks(conn, pdu))) {
    3391                 :          6 :                         iscsi_task_response(conn, task);
    3392                 :          6 :                         iscsi_task_put(task);
    3393                 :          6 :                         return 0;
    3394                 :            :                 }
    3395                 :            : 
    3396         [ +  + ]:    9921972 :                 if (pdu->data_segment_len > iscsi_get_max_immediate_data_size()) {
    3397                 :          6 :                         SPDK_ERRLOG("data segment len(=%zu) > immediate data len(=%"PRIu32")\n",
    3398                 :            :                                     pdu->data_segment_len, iscsi_get_max_immediate_data_size());
    3399                 :          6 :                         iscsi_task_put(task);
    3400                 :          6 :                         return iscsi_reject(conn, pdu, ISCSI_REASON_PROTOCOL_ERROR);
    3401                 :            :                 }
    3402                 :            : 
    3403         [ +  + ]:    9921966 :                 if (pdu->data_segment_len > transfer_len) {
    3404                 :          6 :                         SPDK_ERRLOG("data segment len(=%zu) > task transfer len(=%d)\n",
    3405                 :            :                                     pdu->data_segment_len, transfer_len);
    3406                 :          6 :                         iscsi_task_put(task);
    3407                 :          6 :                         return iscsi_reject(conn, pdu, ISCSI_REASON_PROTOCOL_ERROR);
    3408                 :            :                 }
    3409                 :            : 
    3410                 :            :                 /* check the ImmediateData and also pdu->data_segment_len */
    3411   [ +  +  +  +  :    9921960 :                 if ((!conn->sess->ImmediateData && (pdu->data_segment_len > 0)) ||
                   +  + ]
    3412         [ +  + ]:    9921952 :                     (pdu->data_segment_len > conn->sess->FirstBurstLength)) {
    3413                 :         16 :                         iscsi_task_put(task);
    3414                 :         16 :                         return iscsi_reject(conn, pdu, ISCSI_REASON_PROTOCOL_ERROR);
    3415                 :            :                 }
    3416                 :            : 
    3417         [ -  + ]:    9921944 :                 if (spdk_unlikely(spdk_scsi_lun_get_dif_ctx(task->scsi.lun, &task->scsi, &pdu->dif_ctx))) {
    3418                 :          0 :                         pdu->dif_insert_or_strip = true;
    3419   [ +  +  +  + ]:    9921944 :                 } else if (reqh->final_bit && pdu->data_segment_len < transfer_len) {
    3420                 :     538378 :                         pdu->data_buf_len = spdk_min(transfer_len,
    3421                 :            :                                                      SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH);
    3422                 :            :                 }
    3423                 :            :         } else {
    3424                 :            :                 /* neither R nor W bit set */
    3425                 :    1007290 :                 task->scsi.dxfer_dir = SPDK_SCSI_DIR_NONE;
    3426         [ +  + ]:    1007290 :                 if (transfer_len > 0) {
    3427                 :         11 :                         iscsi_task_put(task);
    3428                 :         11 :                         SPDK_ERRLOG("Reject scsi cmd with EDTL > 0 but (R | W) == 0\n");
    3429                 :         11 :                         return iscsi_reject(conn, pdu, ISCSI_REASON_INVALID_PDU_FIELD);
    3430                 :            :                 }
    3431                 :            :         }
    3432                 :            : 
    3433                 :   23541476 :         pdu->task = task;
    3434                 :   23541476 :         return 0;
    3435                 :            : }
    3436                 :            : 
    3437                 :            : static int
    3438                 :   23545672 : iscsi_pdu_payload_op_scsi(struct spdk_iscsi_conn *conn, struct spdk_iscsi_pdu *pdu)
    3439                 :            : {
    3440                 :            :         struct spdk_iscsi_task *task;
    3441                 :            : 
    3442         [ +  + ]:   23545672 :         if (pdu->task == NULL) {
    3443                 :       4214 :                 return 0;
    3444                 :            :         }
    3445                 :            : 
    3446                 :   23541458 :         task = pdu->task;
    3447                 :            : 
    3448         [ -  + ]:   23541458 :         if (spdk_scsi_dev_get_lun(conn->dev, task->lun_id) == NULL) {
    3449                 :          0 :                 spdk_scsi_task_process_null_lun(&task->scsi);
    3450                 :          0 :                 iscsi_task_cpl(&task->scsi);
    3451                 :          0 :                 return 0;
    3452                 :            :         }
    3453                 :            : 
    3454   [ +  +  +  - ]:   23541458 :         switch (task->scsi.dxfer_dir) {
    3455                 :   12612247 :         case SPDK_SCSI_DIR_FROM_DEV:
    3456                 :   12612247 :                 return iscsi_pdu_payload_op_scsi_read(conn, task);
    3457                 :    9921938 :         case SPDK_SCSI_DIR_TO_DEV:
    3458                 :    9921938 :                 return iscsi_pdu_payload_op_scsi_write(conn, task);
    3459                 :    1007273 :         case SPDK_SCSI_DIR_NONE:
    3460                 :    1007273 :                 iscsi_queue_task(conn, task);
    3461                 :    1007273 :                 return 0;
    3462                 :          0 :         default:
    3463                 :          0 :                 assert(false);
    3464                 :            :                 iscsi_task_put(task);
    3465                 :            :                 break;
    3466                 :            :         }
    3467                 :            : 
    3468                 :            :         return SPDK_ISCSI_CONNECTION_FATAL;
    3469                 :            : }
    3470                 :            : 
    3471                 :            : void
    3472                 :       3807 : iscsi_task_mgmt_response(struct spdk_iscsi_conn *conn,
    3473                 :            :                          struct spdk_iscsi_task *task)
    3474                 :            : {
    3475                 :            :         struct spdk_iscsi_pdu *rsp_pdu;
    3476                 :            :         struct iscsi_bhs_task_req *reqh;
    3477                 :            :         struct iscsi_bhs_task_resp *rsph;
    3478                 :            : 
    3479         [ -  + ]:       3807 :         if (task->pdu == NULL) {
    3480                 :            :                 /*
    3481                 :            :                  * This was an internally generated task management command,
    3482                 :            :                  *  usually from LUN cleanup when a connection closes.
    3483                 :            :                  */
    3484                 :          0 :                 return;
    3485                 :            :         }
    3486                 :            : 
    3487                 :       3807 :         reqh = (struct iscsi_bhs_task_req *)&task->pdu->bhs;
    3488                 :            :         /* response PDU */
    3489                 :       3807 :         rsp_pdu = iscsi_get_pdu(conn);
    3490                 :       3807 :         rsph = (struct iscsi_bhs_task_resp *)&rsp_pdu->bhs;
    3491                 :       3807 :         rsph->opcode = ISCSI_OP_TASK_RSP;
    3492                 :       3807 :         rsph->flags |= 0x80; /* bit 0 default to 1 */
    3493   [ -  +  +  +  :       3807 :         switch (task->scsi.response) {
                -  +  - ]
    3494                 :          0 :         case SPDK_SCSI_TASK_MGMT_RESP_COMPLETE:
    3495                 :          0 :                 rsph->response = ISCSI_TASK_FUNC_RESP_COMPLETE;
    3496                 :          0 :                 break;
    3497                 :          6 :         case SPDK_SCSI_TASK_MGMT_RESP_SUCCESS:
    3498                 :          6 :                 rsph->response = ISCSI_TASK_FUNC_RESP_COMPLETE;
    3499                 :          6 :                 break;
    3500                 :         11 :         case SPDK_SCSI_TASK_MGMT_RESP_REJECT:
    3501                 :         11 :                 rsph->response = ISCSI_TASK_FUNC_REJECTED;
    3502                 :         11 :                 break;
    3503                 :       3760 :         case SPDK_SCSI_TASK_MGMT_RESP_INVALID_LUN:
    3504                 :       3760 :                 rsph->response = ISCSI_TASK_FUNC_RESP_LUN_NOT_EXIST;
    3505                 :       3760 :                 break;
    3506                 :          0 :         case SPDK_SCSI_TASK_MGMT_RESP_TARGET_FAILURE:
    3507                 :          0 :                 rsph->response = ISCSI_TASK_FUNC_REJECTED;
    3508                 :          0 :                 break;
    3509                 :         30 :         case SPDK_SCSI_TASK_MGMT_RESP_REJECT_FUNC_NOT_SUPPORTED:
    3510                 :         30 :                 rsph->response = ISCSI_TASK_FUNC_RESP_FUNC_NOT_SUPPORTED;
    3511                 :         30 :                 break;
    3512                 :            :         }
    3513                 :       3807 :         rsph->itt = reqh->itt;
    3514                 :            : 
    3515                 :       3807 :         to_be32(&rsph->stat_sn, conn->StatSN);
    3516                 :       3807 :         conn->StatSN++;
    3517                 :            : 
    3518         [ +  + ]:       3807 :         if (reqh->immediate == 0) {
    3519                 :         42 :                 conn->sess->MaxCmdSN++;
    3520                 :            :         }
    3521                 :            : 
    3522                 :       3807 :         to_be32(&rsph->exp_cmd_sn, conn->sess->ExpCmdSN);
    3523                 :       3807 :         to_be32(&rsph->max_cmd_sn, conn->sess->MaxCmdSN);
    3524                 :            : 
    3525                 :       3807 :         iscsi_conn_write_pdu(conn, rsp_pdu, iscsi_conn_pdu_generic_complete, NULL);
    3526                 :            : }
    3527                 :            : 
    3528                 :            : static void
    3529                 :          6 : iscsi_queue_mgmt_task(struct spdk_iscsi_conn *conn, struct spdk_iscsi_task *task)
    3530                 :            : {
    3531                 :            :         struct spdk_scsi_lun *lun;
    3532                 :            : 
    3533                 :          6 :         lun = spdk_scsi_dev_get_lun(conn->dev, task->lun_id);
    3534         [ -  + ]:          6 :         if (lun == NULL) {
    3535                 :          0 :                 task->scsi.response = SPDK_SCSI_TASK_MGMT_RESP_INVALID_LUN;
    3536                 :          0 :                 iscsi_task_mgmt_response(conn, task);
    3537                 :          0 :                 iscsi_task_put(task);
    3538                 :          0 :                 return;
    3539                 :            :         }
    3540                 :            : 
    3541                 :          6 :         spdk_scsi_dev_queue_mgmt_task(conn->dev, &task->scsi);
    3542                 :            : }
    3543                 :            : 
    3544                 :            : static int
    3545                 :          0 : _iscsi_op_abort_task(void *arg)
    3546                 :            : {
    3547                 :          0 :         struct spdk_iscsi_task *task = arg;
    3548                 :            :         int rc;
    3549                 :            : 
    3550                 :          0 :         rc = iscsi_conn_abort_queued_datain_task(task->conn, task->scsi.abort_id);
    3551         [ #  # ]:          0 :         if (rc != 0) {
    3552                 :          0 :                 return SPDK_POLLER_BUSY;
    3553                 :            :         }
    3554                 :            : 
    3555                 :          0 :         spdk_poller_unregister(&task->mgmt_poller);
    3556                 :          0 :         iscsi_queue_mgmt_task(task->conn, task);
    3557                 :          0 :         return SPDK_POLLER_BUSY;
    3558                 :            : }
    3559                 :            : 
    3560                 :            : static void
    3561                 :          0 : iscsi_op_abort_task(struct spdk_iscsi_task *task, uint32_t ref_task_tag)
    3562                 :            : {
    3563                 :          0 :         task->scsi.abort_id = ref_task_tag;
    3564                 :          0 :         task->scsi.function = SPDK_SCSI_TASK_FUNC_ABORT_TASK;
    3565                 :          0 :         task->mgmt_poller = SPDK_POLLER_REGISTER(_iscsi_op_abort_task, task, 10);
    3566                 :          0 : }
    3567                 :            : 
    3568                 :            : static int
    3569                 :          6 : _iscsi_op_abort_task_set(void *arg)
    3570                 :            : {
    3571                 :          6 :         struct spdk_iscsi_task *task = arg;
    3572                 :            :         int rc;
    3573                 :            : 
    3574                 :          6 :         rc = iscsi_conn_abort_queued_datain_tasks(task->conn, task->scsi.lun,
    3575                 :            :                         task->pdu);
    3576         [ -  + ]:          6 :         if (rc != 0) {
    3577                 :          0 :                 return SPDK_POLLER_BUSY;
    3578                 :            :         }
    3579                 :            : 
    3580                 :          6 :         spdk_poller_unregister(&task->mgmt_poller);
    3581                 :          6 :         iscsi_queue_mgmt_task(task->conn, task);
    3582                 :          6 :         return SPDK_POLLER_BUSY;
    3583                 :            : }
    3584                 :            : 
    3585                 :            : void
    3586                 :          6 : iscsi_op_abort_task_set(struct spdk_iscsi_task *task, uint8_t function)
    3587                 :            : {
    3588                 :          6 :         task->scsi.function = function;
    3589                 :          6 :         task->mgmt_poller = SPDK_POLLER_REGISTER(_iscsi_op_abort_task_set, task, 10);
    3590                 :          6 : }
    3591                 :            : 
    3592                 :            : static int
    3593                 :       3813 : iscsi_pdu_hdr_op_task(struct spdk_iscsi_conn *conn, struct spdk_iscsi_pdu *pdu)
    3594                 :            : {
    3595                 :            :         struct iscsi_bhs_task_req *reqh;
    3596                 :            :         uint64_t lun;
    3597                 :            :         uint32_t task_tag;
    3598                 :            :         uint32_t ref_task_tag;
    3599                 :            :         uint8_t function;
    3600                 :            :         int lun_i;
    3601                 :            :         struct spdk_iscsi_task *task;
    3602                 :            :         struct spdk_scsi_dev *dev;
    3603                 :            : 
    3604         [ +  + ]:       3813 :         if (conn->sess->session_type != SESSION_TYPE_NORMAL) {
    3605                 :          6 :                 SPDK_ERRLOG("ISCSI_OP_TASK not allowed in discovery and invalid session\n");
    3606                 :          6 :                 return SPDK_ISCSI_CONNECTION_FATAL;
    3607                 :            :         }
    3608                 :            : 
    3609                 :       3807 :         reqh = (struct iscsi_bhs_task_req *)&pdu->bhs;
    3610                 :       3807 :         function = reqh->flags & ISCSI_TASK_FUNCTION_MASK;
    3611                 :       3807 :         lun = from_be64(&reqh->lun);
    3612                 :       3807 :         task_tag = from_be32(&reqh->itt);
    3613                 :       3807 :         ref_task_tag = from_be32(&reqh->ref_task_tag);
    3614                 :            : 
    3615   [ -  +  -  + ]:       3807 :         SPDK_DEBUGLOG(iscsi, "I=%d, func=%d, ITT=%x, ref TT=%x, LUN=0x%16.16"PRIx64"\n",
    3616                 :            :                       reqh->immediate, function, task_tag, ref_task_tag, lun);
    3617                 :            : 
    3618   [ -  +  -  + ]:       3807 :         SPDK_DEBUGLOG(iscsi, "StatSN=%u, ExpCmdSN=%u, MaxCmdSN=%u\n",
    3619                 :            :                       conn->StatSN, conn->sess->ExpCmdSN, conn->sess->MaxCmdSN);
    3620                 :            : 
    3621                 :       3807 :         lun_i = spdk_scsi_lun_id_fmt_to_int(lun);
    3622                 :       3807 :         dev = conn->dev;
    3623                 :            : 
    3624                 :       3807 :         task = iscsi_task_get(conn, NULL, iscsi_task_mgmt_cpl);
    3625         [ -  + ]:       3807 :         if (!task) {
    3626                 :          0 :                 SPDK_ERRLOG("Unable to acquire task\n");
    3627                 :          0 :                 return SPDK_ISCSI_CONNECTION_FATAL;
    3628                 :            :         }
    3629                 :            : 
    3630                 :       3807 :         iscsi_task_associate_pdu(task, pdu);
    3631                 :       3807 :         task->scsi.target_port = conn->target_port;
    3632                 :       3807 :         task->scsi.initiator_port = conn->initiator_port;
    3633                 :       3807 :         task->tag = task_tag;
    3634                 :       3807 :         task->scsi.lun = spdk_scsi_dev_get_lun(dev, lun_i);
    3635                 :       3807 :         task->lun_id = lun_i;
    3636                 :            : 
    3637         [ +  + ]:       3807 :         if (task->scsi.lun == NULL) {
    3638                 :       3760 :                 task->scsi.response = SPDK_SCSI_TASK_MGMT_RESP_INVALID_LUN;
    3639                 :       3760 :                 iscsi_task_mgmt_response(conn, task);
    3640                 :       3760 :                 iscsi_task_put(task);
    3641                 :       3760 :                 return 0;
    3642                 :            :         }
    3643                 :            : 
    3644   [ -  -  +  +  :         47 :         switch (function) {
             +  +  +  +  
                      + ]
    3645                 :            :         /* abort task identified by Referenced Task Tag field */
    3646                 :          0 :         case ISCSI_TASK_FUNC_ABORT_TASK:
    3647                 :          0 :                 SPDK_NOTICELOG("ABORT_TASK\n");
    3648                 :            : 
    3649                 :          0 :                 iscsi_del_transfer_task(conn, ref_task_tag);
    3650                 :          0 :                 iscsi_op_abort_task(task, ref_task_tag);
    3651                 :          0 :                 return 0;
    3652                 :            : 
    3653                 :            :         /* abort all tasks issued via this session on the LUN */
    3654                 :          0 :         case ISCSI_TASK_FUNC_ABORT_TASK_SET:
    3655                 :          0 :                 SPDK_NOTICELOG("ABORT_TASK_SET\n");
    3656                 :            : 
    3657                 :          0 :                 iscsi_clear_all_transfer_task(conn, task->scsi.lun, pdu);
    3658                 :          0 :                 iscsi_op_abort_task_set(task, SPDK_SCSI_TASK_FUNC_ABORT_TASK_SET);
    3659                 :          0 :                 return 0;
    3660                 :            : 
    3661                 :          6 :         case ISCSI_TASK_FUNC_CLEAR_TASK_SET:
    3662                 :          6 :                 task->scsi.response = SPDK_SCSI_TASK_MGMT_RESP_REJECT_FUNC_NOT_SUPPORTED;
    3663                 :          6 :                 SPDK_NOTICELOG("CLEAR_TASK_SET (Unsupported)\n");
    3664                 :          6 :                 break;
    3665                 :            : 
    3666                 :          6 :         case ISCSI_TASK_FUNC_CLEAR_ACA:
    3667                 :          6 :                 task->scsi.response = SPDK_SCSI_TASK_MGMT_RESP_REJECT_FUNC_NOT_SUPPORTED;
    3668                 :          6 :                 SPDK_NOTICELOG("CLEAR_ACA (Unsupported)\n");
    3669                 :          6 :                 break;
    3670                 :            : 
    3671                 :          6 :         case ISCSI_TASK_FUNC_LOGICAL_UNIT_RESET:
    3672                 :          6 :                 SPDK_NOTICELOG("LOGICAL_UNIT_RESET\n");
    3673                 :            : 
    3674                 :          6 :                 iscsi_clear_all_transfer_task(conn, task->scsi.lun, pdu);
    3675                 :          6 :                 iscsi_op_abort_task_set(task, SPDK_SCSI_TASK_FUNC_LUN_RESET);
    3676                 :          6 :                 return 0;
    3677                 :            : 
    3678                 :          6 :         case ISCSI_TASK_FUNC_TARGET_WARM_RESET:
    3679                 :          6 :                 SPDK_NOTICELOG("TARGET_WARM_RESET (Unsupported)\n");
    3680                 :          6 :                 task->scsi.response = SPDK_SCSI_TASK_MGMT_RESP_REJECT_FUNC_NOT_SUPPORTED;
    3681                 :          6 :                 break;
    3682                 :            : 
    3683                 :          6 :         case ISCSI_TASK_FUNC_TARGET_COLD_RESET:
    3684                 :          6 :                 SPDK_NOTICELOG("TARGET_COLD_RESET (Unsupported)\n");
    3685                 :          6 :                 task->scsi.response = SPDK_SCSI_TASK_MGMT_RESP_REJECT_FUNC_NOT_SUPPORTED;
    3686                 :          6 :                 break;
    3687                 :            : 
    3688                 :          6 :         case ISCSI_TASK_FUNC_TASK_REASSIGN:
    3689                 :          6 :                 SPDK_NOTICELOG("TASK_REASSIGN (Unsupported)\n");
    3690                 :          6 :                 task->scsi.response = SPDK_SCSI_TASK_MGMT_RESP_REJECT_FUNC_NOT_SUPPORTED;
    3691                 :          6 :                 break;
    3692                 :            : 
    3693                 :         11 :         default:
    3694                 :         11 :                 SPDK_ERRLOG("unsupported function %d\n", function);
    3695                 :         11 :                 task->scsi.response = SPDK_SCSI_TASK_MGMT_RESP_REJECT;
    3696                 :         11 :                 break;
    3697                 :            :         }
    3698                 :            : 
    3699                 :         41 :         iscsi_task_mgmt_response(conn, task);
    3700                 :         41 :         iscsi_task_put(task);
    3701                 :         41 :         return 0;
    3702                 :            : }
    3703                 :            : 
    3704                 :            : static int
    3705                 :       3866 : iscsi_pdu_hdr_op_nopout(struct spdk_iscsi_conn *conn, struct spdk_iscsi_pdu *pdu)
    3706                 :            : {
    3707                 :            :         struct iscsi_bhs_nop_out *reqh;
    3708                 :            :         uint32_t task_tag;
    3709                 :            :         uint32_t transfer_tag;
    3710                 :            :         int I_bit;
    3711                 :            : 
    3712         [ +  + ]:       3866 :         if (conn->sess->session_type == SESSION_TYPE_DISCOVERY) {
    3713                 :          6 :                 SPDK_ERRLOG("ISCSI_OP_NOPOUT not allowed in discovery session\n");
    3714                 :          6 :                 return SPDK_ISCSI_CONNECTION_FATAL;
    3715                 :            :         }
    3716                 :            : 
    3717                 :       3860 :         reqh = (struct iscsi_bhs_nop_out *)&pdu->bhs;
    3718                 :       3860 :         I_bit = reqh->immediate;
    3719                 :            : 
    3720         [ +  + ]:       3860 :         if (pdu->data_segment_len > SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH) {
    3721                 :          6 :                 return iscsi_reject(conn, pdu, ISCSI_REASON_PROTOCOL_ERROR);
    3722                 :            :         }
    3723                 :            : 
    3724                 :       3854 :         task_tag = from_be32(&reqh->itt);
    3725                 :       3854 :         transfer_tag = from_be32(&reqh->ttt);
    3726                 :            : 
    3727   [ -  +  -  + ]:       3854 :         SPDK_DEBUGLOG(iscsi, "I=%d, ITT=%x, TTT=%x\n",
    3728                 :            :                       I_bit, task_tag, transfer_tag);
    3729                 :            : 
    3730   [ -  +  -  + ]:       3854 :         SPDK_DEBUGLOG(iscsi, "CmdSN=%u, StatSN=%u, ExpCmdSN=%u, MaxCmdSN=%u\n",
    3731                 :            :                       pdu->cmd_sn, conn->StatSN, conn->sess->ExpCmdSN,
    3732                 :            :                       conn->sess->MaxCmdSN);
    3733                 :            : 
    3734   [ +  +  +  + ]:       3854 :         if (transfer_tag != 0xFFFFFFFF && transfer_tag != (uint32_t)conn->id) {
    3735                 :       3714 :                 SPDK_ERRLOG("invalid transfer tag 0x%x\n", transfer_tag);
    3736                 :            :                 /*
    3737                 :            :                  * Technically we should probably fail the connection here, but for now
    3738                 :            :                  *  just print the error message and continue.
    3739                 :            :                  */
    3740                 :            :         }
    3741                 :            : 
    3742   [ +  +  +  + ]:       3854 :         if (task_tag == 0xffffffffU && I_bit == 0) {
    3743                 :          6 :                 SPDK_ERRLOG("got NOPOUT ITT=0xffffffff, I=0\n");
    3744                 :          6 :                 return SPDK_ISCSI_CONNECTION_FATAL;
    3745                 :            :         }
    3746                 :            : 
    3747                 :       3848 :         return 0;
    3748                 :            : }
    3749                 :            : 
    3750                 :            : static int
    3751                 :       3842 : iscsi_pdu_payload_op_nopout(struct spdk_iscsi_conn *conn, struct spdk_iscsi_pdu *pdu)
    3752                 :            : {
    3753                 :            :         struct spdk_iscsi_pdu *rsp_pdu;
    3754                 :            :         struct iscsi_bhs_nop_out *reqh;
    3755                 :            :         struct iscsi_bhs_nop_in *rsph;
    3756                 :            :         uint8_t *data;
    3757                 :            :         uint64_t lun;
    3758                 :            :         uint32_t task_tag;
    3759                 :            :         int I_bit;
    3760                 :            :         int data_len;
    3761                 :            : 
    3762                 :       3842 :         reqh = (struct iscsi_bhs_nop_out *)&pdu->bhs;
    3763                 :       3842 :         I_bit = reqh->immediate;
    3764                 :            : 
    3765                 :       3842 :         data_len = pdu->data_segment_len;
    3766         [ -  + ]:       3842 :         if (data_len > conn->MaxRecvDataSegmentLength) {
    3767                 :          0 :                 data_len = conn->MaxRecvDataSegmentLength;
    3768                 :            :         }
    3769                 :            : 
    3770                 :       3842 :         lun = from_be64(&reqh->lun);
    3771                 :       3842 :         task_tag = from_be32(&reqh->itt);
    3772                 :            : 
    3773                 :            :         /*
    3774                 :            :          * We don't actually check to see if this is a response to the NOP-In
    3775                 :            :          * that we sent.  Our goal is to just verify that the initiator is
    3776                 :            :          * alive and responding to commands, not to verify that it tags
    3777                 :            :          * NOP-Outs correctly
    3778                 :            :          */
    3779                 :       3842 :         conn->nop_outstanding = false;
    3780                 :            : 
    3781         [ +  + ]:       3842 :         if (task_tag == 0xffffffffU) {
    3782         [ -  + ]:         65 :                 assert(I_bit == 1);
    3783   [ -  +  -  + ]:         65 :                 SPDK_DEBUGLOG(iscsi, "got NOPOUT ITT=0xffffffff\n");
    3784                 :         65 :                 return 0;
    3785                 :            :         }
    3786                 :            : 
    3787                 :       3777 :         data = calloc(1, data_len);
    3788         [ -  + ]:       3777 :         if (!data) {
    3789                 :          0 :                 SPDK_ERRLOG("calloc() failed for ping data\n");
    3790                 :          0 :                 return SPDK_ISCSI_CONNECTION_FATAL;
    3791                 :            :         }
    3792                 :            : 
    3793                 :            :         /* response of NOPOUT */
    3794         [ +  + ]:       3777 :         if (data_len > 0) {
    3795                 :            :                 /* copy ping data */
    3796   [ -  +  -  + ]:         30 :                 memcpy(data, pdu->data, data_len);
    3797                 :            :         }
    3798                 :            : 
    3799                 :            :         /* response PDU */
    3800                 :       3777 :         rsp_pdu = iscsi_get_pdu(conn);
    3801         [ -  + ]:       3777 :         assert(rsp_pdu != NULL);
    3802                 :            : 
    3803                 :       3777 :         rsph = (struct iscsi_bhs_nop_in *)&rsp_pdu->bhs;
    3804                 :       3777 :         rsp_pdu->data = data;
    3805                 :       3777 :         rsph->opcode = ISCSI_OP_NOPIN;
    3806                 :       3777 :         rsph->flags |= 0x80; /* bit 0 default to 1 */
    3807                 :       3777 :         DSET24(rsph->data_segment_len, data_len);
    3808                 :       3777 :         to_be64(&rsph->lun, lun);
    3809                 :       3777 :         to_be32(&rsph->itt, task_tag);
    3810                 :       3777 :         to_be32(&rsph->ttt, 0xffffffffU);
    3811                 :            : 
    3812                 :       3777 :         to_be32(&rsph->stat_sn, conn->StatSN);
    3813                 :       3777 :         conn->StatSN++;
    3814                 :            : 
    3815         [ +  + ]:       3777 :         if (I_bit == 0) {
    3816                 :         42 :                 conn->sess->MaxCmdSN++;
    3817                 :            :         }
    3818                 :            : 
    3819                 :       3777 :         to_be32(&rsph->exp_cmd_sn, conn->sess->ExpCmdSN);
    3820                 :       3777 :         to_be32(&rsph->max_cmd_sn, conn->sess->MaxCmdSN);
    3821                 :            : 
    3822                 :       3777 :         iscsi_conn_write_pdu(conn, rsp_pdu, iscsi_conn_pdu_generic_complete, NULL);
    3823                 :       3777 :         conn->last_nopin = spdk_get_ticks();
    3824                 :            : 
    3825                 :       3777 :         return 0;
    3826                 :            : }
    3827                 :            : 
    3828                 :            : /* This function returns the spdk_scsi_task by searching the snack list via
    3829                 :            :  * task transfertag and the pdu's opcode
    3830                 :            :  */
    3831                 :            : static struct spdk_iscsi_task *
    3832                 :          0 : get_scsi_task_from_ttt(struct spdk_iscsi_conn *conn, uint32_t transfer_tag)
    3833                 :            : {
    3834                 :            :         struct spdk_iscsi_pdu *pdu;
    3835                 :            :         struct iscsi_bhs_data_in *datain_bhs;
    3836                 :            : 
    3837         [ #  # ]:          0 :         TAILQ_FOREACH(pdu, &conn->snack_pdu_list, tailq) {
    3838         [ #  # ]:          0 :                 if (pdu->bhs.opcode == ISCSI_OP_SCSI_DATAIN) {
    3839                 :          0 :                         datain_bhs = (struct iscsi_bhs_data_in *)&pdu->bhs;
    3840         [ #  # ]:          0 :                         if (from_be32(&datain_bhs->ttt) == transfer_tag) {
    3841                 :          0 :                                 return pdu->task;
    3842                 :            :                         }
    3843                 :            :                 }
    3844                 :            :         }
    3845                 :            : 
    3846                 :          0 :         return NULL;
    3847                 :            : }
    3848                 :            : 
    3849                 :            : /* This function returns the spdk_scsi_task by searching the snack list via
    3850                 :            :  * initiator task tag and the pdu's opcode
    3851                 :            :  */
    3852                 :            : static struct spdk_iscsi_task *
    3853                 :         92 : get_scsi_task_from_itt(struct spdk_iscsi_conn *conn,
    3854                 :            :                        uint32_t task_tag, enum iscsi_op opcode)
    3855                 :            : {
    3856                 :            :         struct spdk_iscsi_pdu *pdu;
    3857                 :            : 
    3858         [ +  + ]:        120 :         TAILQ_FOREACH(pdu, &conn->snack_pdu_list, tailq) {
    3859         [ +  + ]:         92 :                 if (pdu->bhs.opcode == opcode &&
    3860         [ +  - ]:         64 :                     pdu->task != NULL &&
    3861         [ +  - ]:         64 :                     pdu->task->tag == task_tag) {
    3862                 :         64 :                         return pdu->task;
    3863                 :            :                 }
    3864                 :            :         }
    3865                 :            : 
    3866                 :         28 :         return NULL;
    3867                 :            : }
    3868                 :            : 
    3869                 :            : /* This function is used to handle the r2t snack */
    3870                 :            : static int
    3871                 :         28 : iscsi_handle_r2t_snack(struct spdk_iscsi_conn *conn,
    3872                 :            :                        struct spdk_iscsi_task *task,
    3873                 :            :                        struct spdk_iscsi_pdu *pdu, uint32_t beg_run,
    3874                 :            :                        uint32_t run_length, int32_t task_tag)
    3875                 :            : {
    3876                 :            :         int32_t last_r2tsn;
    3877                 :            :         int i;
    3878                 :            : 
    3879         [ -  + ]:         28 :         if (beg_run < task->acked_r2tsn) {
    3880                 :          0 :                 SPDK_ERRLOG("ITT: 0x%08x, R2T SNACK requests retransmission of"
    3881                 :            :                             "R2TSN: from 0x%08x to 0x%08x. But it has already"
    3882                 :            :                             "ack to R2TSN:0x%08x, protocol error.\n",
    3883                 :            :                             task_tag, beg_run, (beg_run + run_length),
    3884                 :            :                             (task->acked_r2tsn - 1));
    3885                 :          0 :                 return iscsi_reject(conn, pdu, ISCSI_REASON_PROTOCOL_ERROR);
    3886                 :            :         }
    3887                 :            : 
    3888         [ +  + ]:         28 :         if (run_length) {
    3889         [ -  + ]:          2 :                 if ((beg_run + run_length) > task->R2TSN) {
    3890                 :          0 :                         SPDK_ERRLOG("ITT: 0x%08x, received R2T SNACK with"
    3891                 :            :                                     "BegRun: 0x%08x, RunLength: 0x%08x, exceeds"
    3892                 :            :                                     "current R2TSN: 0x%08x, protocol error.\n",
    3893                 :            :                                     task_tag, beg_run, run_length,
    3894                 :            :                                     task->R2TSN);
    3895                 :            : 
    3896                 :          0 :                         return iscsi_reject(conn, pdu, ISCSI_REASON_INVALID_PDU_FIELD);
    3897                 :            :                 }
    3898                 :          2 :                 last_r2tsn = (beg_run + run_length);
    3899                 :            :         } else {
    3900                 :         26 :                 last_r2tsn = task->R2TSN;
    3901                 :            :         }
    3902                 :            : 
    3903         [ +  + ]:         56 :         for (i = beg_run; i < last_r2tsn; i++) {
    3904         [ -  + ]:         28 :                 if (iscsi_send_r2t_recovery(conn, task, i, false) < 0) {
    3905                 :          0 :                         SPDK_ERRLOG("The r2t_sn=%d of r2t_task=%p is not sent\n", i, task);
    3906                 :            :                 }
    3907                 :            :         }
    3908                 :         28 :         return 0;
    3909                 :            : }
    3910                 :            : 
    3911                 :            : /* This function is used to recover the data in packet */
    3912                 :            : static int
    3913                 :         36 : iscsi_handle_recovery_datain(struct spdk_iscsi_conn *conn,
    3914                 :            :                              struct spdk_iscsi_task *task,
    3915                 :            :                              struct spdk_iscsi_pdu *pdu, uint32_t beg_run,
    3916                 :            :                              uint32_t run_length, uint32_t task_tag)
    3917                 :            : {
    3918                 :            :         struct spdk_iscsi_pdu *old_pdu, *pdu_temp;
    3919                 :            :         uint32_t i;
    3920                 :            :         struct iscsi_bhs_data_in *datain_header;
    3921                 :            :         uint32_t last_statsn;
    3922                 :            : 
    3923                 :         36 :         task = iscsi_task_get_primary(task);
    3924                 :            : 
    3925   [ -  +  -  + ]:         36 :         SPDK_DEBUGLOG(iscsi, "iscsi_handle_recovery_datain\n");
    3926                 :            : 
    3927         [ -  + ]:         36 :         if (beg_run < task->acked_data_sn) {
    3928                 :          0 :                 SPDK_ERRLOG("ITT: 0x%08x, DATA IN SNACK requests retransmission of"
    3929                 :            :                             "DATASN: from 0x%08x to 0x%08x but already acked to "
    3930                 :            :                             "DATASN: 0x%08x protocol error\n",
    3931                 :            :                             task_tag, beg_run,
    3932                 :            :                             (beg_run + run_length), (task->acked_data_sn - 1));
    3933                 :            : 
    3934                 :          0 :                 return iscsi_reject(conn, pdu, ISCSI_REASON_PROTOCOL_ERROR);
    3935                 :            :         }
    3936                 :            : 
    3937         [ +  + ]:         36 :         if (run_length == 0) {
    3938                 :            :                 /* as the DataSN begins at 0 */
    3939                 :         28 :                 run_length = task->datain_datasn + 1;
    3940                 :            :         }
    3941                 :            : 
    3942         [ +  + ]:         36 :         if ((beg_run + run_length - 1) > task->datain_datasn) {
    3943                 :          2 :                 SPDK_ERRLOG("Initiator requests BegRun: 0x%08x, RunLength:"
    3944                 :            :                             "0x%08x greater than maximum DataSN: 0x%08x.\n",
    3945                 :            :                             beg_run, run_length, task->datain_datasn);
    3946                 :            : 
    3947                 :          2 :                 return -1;
    3948                 :            :         } else {
    3949                 :         34 :                 last_statsn = beg_run + run_length - 1;
    3950                 :            :         }
    3951                 :            : 
    3952         [ +  + ]:        106 :         for (i = beg_run; i <= last_statsn; i++) {
    3953         [ +  + ]:         80 :                 TAILQ_FOREACH_SAFE(old_pdu, &conn->snack_pdu_list, tailq, pdu_temp) {
    3954         [ +  - ]:         52 :                         if (old_pdu->bhs.opcode == ISCSI_OP_SCSI_DATAIN) {
    3955                 :         52 :                                 datain_header = (struct iscsi_bhs_data_in *)&old_pdu->bhs;
    3956   [ +  -  +  + ]:        104 :                                 if (from_be32(&datain_header->itt) == task_tag &&
    3957                 :         52 :                                     from_be32(&datain_header->data_sn) == i) {
    3958         [ +  + ]:         44 :                                         TAILQ_REMOVE(&conn->snack_pdu_list, old_pdu, tailq);
    3959                 :         44 :                                         iscsi_conn_write_pdu(conn, old_pdu, old_pdu->cb_fn, old_pdu->cb_arg);
    3960                 :         44 :                                         break;
    3961                 :            :                                 }
    3962                 :            :                         }
    3963                 :            :                 }
    3964                 :            :         }
    3965                 :         34 :         return 0;
    3966                 :            : }
    3967                 :            : 
    3968                 :            : /* This function is used to handle the status snack */
    3969                 :            : static int
    3970                 :          4 : iscsi_handle_status_snack(struct spdk_iscsi_conn *conn, struct spdk_iscsi_pdu *pdu)
    3971                 :            : {
    3972                 :            :         uint32_t beg_run;
    3973                 :            :         uint32_t run_length;
    3974                 :            :         struct iscsi_bhs_snack_req *reqh;
    3975                 :            :         uint32_t i;
    3976                 :            :         uint32_t last_statsn;
    3977                 :            :         bool found_pdu;
    3978                 :            :         struct spdk_iscsi_pdu *old_pdu;
    3979                 :            : 
    3980                 :          4 :         reqh = (struct iscsi_bhs_snack_req *)&pdu->bhs;
    3981                 :          4 :         beg_run = from_be32(&reqh->beg_run);
    3982                 :          4 :         run_length = from_be32(&reqh->run_len);
    3983                 :            : 
    3984   [ -  +  -  + ]:          4 :         SPDK_DEBUGLOG(iscsi, "beg_run=%d, run_length=%d, conn->StatSN="
    3985                 :            :                       "%d, conn->exp_statsn=%d\n", beg_run, run_length,
    3986                 :            :                       conn->StatSN, conn->exp_statsn);
    3987                 :            : 
    3988         [ -  + ]:          4 :         if (!beg_run) {
    3989                 :          0 :                 beg_run = conn->exp_statsn;
    3990         [ -  + ]:          4 :         } else if (beg_run < conn->exp_statsn) {
    3991                 :          0 :                 SPDK_ERRLOG("Got Status SNACK Begrun: 0x%08x, RunLength: 0x%08x "
    3992                 :            :                             "but already got ExpStatSN: 0x%08x on CID:%hu.\n",
    3993                 :            :                             beg_run, run_length, conn->StatSN, conn->cid);
    3994                 :            : 
    3995                 :          0 :                 return iscsi_reject(conn, pdu, ISCSI_REASON_INVALID_PDU_FIELD);
    3996                 :            :         }
    3997                 :            : 
    3998         [ -  + ]:          4 :         last_statsn = (!run_length) ? conn->StatSN : (beg_run + run_length);
    3999                 :            : 
    4000         [ +  + ]:          8 :         for (i = beg_run; i < last_statsn; i++) {
    4001                 :          4 :                 found_pdu = false;
    4002         [ +  + ]:          4 :                 TAILQ_FOREACH(old_pdu, &conn->snack_pdu_list, tailq) {
    4003         [ +  - ]:          2 :                         if (from_be32(&old_pdu->bhs.stat_sn) == i) {
    4004                 :          2 :                                 found_pdu = true;
    4005                 :          2 :                                 break;
    4006                 :            :                         }
    4007                 :            :                 }
    4008                 :            : 
    4009         [ +  + ]:          4 :                 if (!found_pdu) {
    4010                 :          2 :                         SPDK_ERRLOG("Unable to find StatSN: 0x%08x. For a Status"
    4011                 :            :                                     "SNACK, assuming this is a proactive SNACK "
    4012                 :            :                                     "for an untransmitted StatSN, ignoring.\n",
    4013                 :            :                                     beg_run);
    4014                 :            :                 } else {
    4015         [ -  + ]:          2 :                         TAILQ_REMOVE(&conn->snack_pdu_list, old_pdu, tailq);
    4016                 :          2 :                         iscsi_conn_write_pdu(conn, old_pdu, old_pdu->cb_fn, old_pdu->cb_arg);
    4017                 :            :                 }
    4018                 :            :         }
    4019                 :            : 
    4020                 :          4 :         return 0;
    4021                 :            : }
    4022                 :            : 
    4023                 :            : /* This function is used to handle the data ack snack */
    4024                 :            : static int
    4025                 :          0 : iscsi_handle_data_ack(struct spdk_iscsi_conn *conn, struct spdk_iscsi_pdu *pdu)
    4026                 :            : {
    4027                 :            :         uint32_t transfer_tag;
    4028                 :            :         uint32_t beg_run;
    4029                 :            :         uint32_t run_length;
    4030                 :            :         struct spdk_iscsi_pdu *old_pdu;
    4031                 :            :         uint32_t old_datasn;
    4032                 :            :         struct iscsi_bhs_snack_req *reqh;
    4033                 :            :         struct spdk_iscsi_task *task;
    4034                 :            :         struct iscsi_bhs_data_in *datain_header;
    4035                 :            :         struct spdk_iscsi_task *primary;
    4036                 :            : 
    4037                 :          0 :         reqh = (struct iscsi_bhs_snack_req *)&pdu->bhs;
    4038                 :          0 :         transfer_tag = from_be32(&reqh->ttt);
    4039                 :          0 :         beg_run = from_be32(&reqh->beg_run);
    4040                 :          0 :         run_length = from_be32(&reqh->run_len);
    4041                 :          0 :         task = NULL;
    4042                 :          0 :         datain_header = NULL;
    4043                 :            : 
    4044   [ #  #  #  # ]:          0 :         SPDK_DEBUGLOG(iscsi, "beg_run=%d,transfer_tag=%d,run_len=%d\n",
    4045                 :            :                       beg_run, transfer_tag, run_length);
    4046                 :            : 
    4047                 :          0 :         task = get_scsi_task_from_ttt(conn, transfer_tag);
    4048         [ #  # ]:          0 :         if (!task) {
    4049                 :          0 :                 SPDK_ERRLOG("Data ACK SNACK for TTT: 0x%08x is invalid.\n",
    4050                 :            :                             transfer_tag);
    4051                 :          0 :                 goto reject_return;
    4052                 :            :         }
    4053                 :            : 
    4054                 :          0 :         primary = iscsi_task_get_primary(task);
    4055   [ #  #  #  # ]:          0 :         if ((run_length != 0) || (beg_run < primary->acked_data_sn)) {
    4056                 :          0 :                 SPDK_ERRLOG("TTT: 0x%08x Data ACK SNACK BegRUN: %d is less than "
    4057                 :            :                             "the next expected acked DataSN: %d\n",
    4058                 :            :                             transfer_tag, beg_run, primary->acked_data_sn);
    4059                 :          0 :                 goto reject_return;
    4060                 :            :         }
    4061                 :            : 
    4062                 :          0 :         primary->acked_data_sn = beg_run;
    4063                 :            : 
    4064                 :            :         /* To free the pdu */
    4065         [ #  # ]:          0 :         TAILQ_FOREACH(old_pdu, &conn->snack_pdu_list, tailq) {
    4066         [ #  # ]:          0 :                 if (old_pdu->bhs.opcode == ISCSI_OP_SCSI_DATAIN) {
    4067                 :          0 :                         datain_header = (struct iscsi_bhs_data_in *) &old_pdu->bhs;
    4068                 :          0 :                         old_datasn = from_be32(&datain_header->data_sn);
    4069         [ #  # ]:          0 :                         if ((from_be32(&datain_header->ttt) == transfer_tag) &&
    4070         [ #  # ]:          0 :                             (old_datasn == beg_run - 1)) {
    4071         [ #  # ]:          0 :                                 TAILQ_REMOVE(&conn->snack_pdu_list, old_pdu, tailq);
    4072                 :          0 :                                 iscsi_conn_free_pdu(conn, old_pdu);
    4073                 :          0 :                                 break;
    4074                 :            :                         }
    4075                 :            :                 }
    4076                 :            :         }
    4077                 :            : 
    4078   [ #  #  #  # ]:          0 :         SPDK_DEBUGLOG(iscsi, "Received Data ACK SNACK for TTT: 0x%08x,"
    4079                 :            :                       " updated acked DataSN to 0x%08x.\n", transfer_tag,
    4080                 :            :                       (task->acked_data_sn - 1));
    4081                 :            : 
    4082                 :          0 :         return 0;
    4083                 :            : 
    4084                 :          0 : reject_return:
    4085                 :          0 :         return iscsi_reject(conn, pdu, ISCSI_REASON_INVALID_SNACK);
    4086                 :            : }
    4087                 :            : 
    4088                 :            : /* This function is used to handle the snack request from the initiator */
    4089                 :            : static int
    4090                 :       3838 : iscsi_pdu_hdr_op_snack(struct spdk_iscsi_conn *conn, struct spdk_iscsi_pdu *pdu)
    4091                 :            : {
    4092                 :            :         struct iscsi_bhs_snack_req *reqh;
    4093                 :            :         struct spdk_iscsi_task *task;
    4094                 :            :         int type;
    4095                 :            :         uint32_t task_tag;
    4096                 :            :         uint32_t beg_run;
    4097                 :            :         uint32_t run_length;
    4098                 :            :         int rc;
    4099                 :            : 
    4100         [ -  + ]:       3838 :         if (conn->sess->session_type == SESSION_TYPE_DISCOVERY) {
    4101                 :          0 :                 SPDK_ERRLOG("ISCSI_OP_SNACK not allowed in  discovery session\n");
    4102                 :          0 :                 return SPDK_ISCSI_CONNECTION_FATAL;
    4103                 :            :         }
    4104                 :            : 
    4105                 :       3838 :         reqh = (struct iscsi_bhs_snack_req *)&pdu->bhs;
    4106         [ +  + ]:       3838 :         if (!conn->sess->ErrorRecoveryLevel) {
    4107                 :       3770 :                 SPDK_ERRLOG("Got a SNACK request in ErrorRecoveryLevel=0\n");
    4108                 :       3770 :                 return iscsi_reject(conn, pdu, ISCSI_REASON_PROTOCOL_ERROR);
    4109                 :            :         }
    4110                 :            : 
    4111                 :         68 :         type = reqh->flags & ISCSI_FLAG_SNACK_TYPE_MASK;
    4112   [ -  +  -  + ]:         68 :         SPDK_DEBUGLOG(iscsi, "The value of type is %d\n", type);
    4113                 :            : 
    4114   [ +  +  -  -  :         68 :         switch (type) {
                      - ]
    4115                 :         64 :         case 0:
    4116                 :         64 :                 reqh = (struct iscsi_bhs_snack_req *)&pdu->bhs;
    4117                 :         64 :                 task_tag = from_be32(&reqh->itt);
    4118                 :         64 :                 beg_run = from_be32(&reqh->beg_run);
    4119                 :         64 :                 run_length = from_be32(&reqh->run_len);
    4120                 :            : 
    4121   [ -  +  -  + ]:         64 :                 SPDK_DEBUGLOG(iscsi, "beg_run=%d, run_length=%d, "
    4122                 :            :                               "task_tag=%x, transfer_tag=%u\n", beg_run,
    4123                 :            :                               run_length, task_tag, from_be32(&reqh->ttt));
    4124                 :            : 
    4125                 :         64 :                 task = get_scsi_task_from_itt(conn, task_tag,
    4126                 :            :                                               ISCSI_OP_SCSI_DATAIN);
    4127         [ +  + ]:         64 :                 if (task) {
    4128                 :         36 :                         return iscsi_handle_recovery_datain(conn, task, pdu,
    4129                 :            :                                                             beg_run, run_length, task_tag);
    4130                 :            :                 }
    4131                 :         28 :                 task = get_scsi_task_from_itt(conn, task_tag, ISCSI_OP_R2T);
    4132         [ +  - ]:         28 :                 if (task) {
    4133                 :         28 :                         return iscsi_handle_r2t_snack(conn, task, pdu, beg_run,
    4134                 :            :                                                       run_length, task_tag);
    4135                 :            :                 }
    4136                 :          0 :                 SPDK_ERRLOG("It is Neither datain nor r2t recovery request\n");
    4137                 :          0 :                 rc = -1;
    4138                 :          0 :                 break;
    4139                 :          4 :         case ISCSI_FLAG_SNACK_TYPE_STATUS:
    4140                 :          4 :                 rc = iscsi_handle_status_snack(conn, pdu);
    4141                 :          4 :                 break;
    4142                 :          0 :         case ISCSI_FLAG_SNACK_TYPE_DATA_ACK:
    4143                 :          0 :                 rc = iscsi_handle_data_ack(conn, pdu);
    4144                 :          0 :                 break;
    4145                 :          0 :         case ISCSI_FLAG_SNACK_TYPE_RDATA:
    4146                 :          0 :                 SPDK_ERRLOG("R-Data SNACK is Not Supported int spdk\n");
    4147                 :          0 :                 rc = iscsi_reject(conn, pdu, ISCSI_REASON_PROTOCOL_ERROR);
    4148                 :          0 :                 break;
    4149                 :          0 :         default:
    4150                 :          0 :                 SPDK_ERRLOG("Unknown SNACK type %d, protocol error\n", type);
    4151                 :          0 :                 rc = iscsi_reject(conn, pdu, ISCSI_REASON_PROTOCOL_ERROR);
    4152                 :          0 :                 break;
    4153                 :            :         }
    4154                 :            : 
    4155                 :          4 :         return rc;
    4156                 :            : }
    4157                 :            : 
    4158                 :            : static inline uint32_t
    4159                 :   22245109 : iscsi_get_mobj_max_data_len(struct spdk_mobj *mobj)
    4160                 :            : {
    4161         [ +  + ]:   22245109 :         if (mobj->mp == g_iscsi.pdu_immediate_data_pool) {
    4162                 :   10151787 :                 return iscsi_get_max_immediate_data_size();
    4163                 :            :         } else {
    4164                 :   12093322 :                 return SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH;
    4165                 :            :         }
    4166                 :            : }
    4167                 :            : 
    4168                 :            : static int
    4169                 :     969561 : iscsi_pdu_hdr_op_data(struct spdk_iscsi_conn *conn, struct spdk_iscsi_pdu *pdu)
    4170                 :            : {
    4171                 :            :         struct spdk_iscsi_task  *task;
    4172                 :            :         struct iscsi_bhs_data_out *reqh;
    4173                 :            :         struct spdk_scsi_lun    *lun_dev;
    4174                 :            :         struct spdk_mobj        *mobj;
    4175                 :            :         uint32_t transfer_tag;
    4176                 :            :         uint32_t task_tag;
    4177                 :            :         uint32_t transfer_len;
    4178                 :            :         uint32_t DataSN;
    4179                 :            :         uint32_t buffer_offset;
    4180                 :            :         uint32_t len;
    4181                 :            :         uint32_t current_desired_data_transfer_length;
    4182                 :            :         int F_bit;
    4183                 :            :         int rc;
    4184                 :            : 
    4185         [ +  + ]:     969561 :         if (conn->sess->session_type == SESSION_TYPE_DISCOVERY) {
    4186                 :          6 :                 SPDK_ERRLOG("ISCSI_OP_SCSI_DATAOUT not allowed in discovery session\n");
    4187                 :          6 :                 return SPDK_ISCSI_CONNECTION_FATAL;
    4188                 :            :         }
    4189                 :            : 
    4190                 :     969555 :         reqh = (struct iscsi_bhs_data_out *)&pdu->bhs;
    4191                 :     969555 :         F_bit = !!(reqh->flags & ISCSI_FLAG_FINAL);
    4192                 :     969555 :         transfer_tag = from_be32(&reqh->ttt);
    4193                 :     969555 :         task_tag = from_be32(&reqh->itt);
    4194                 :     969555 :         DataSN = from_be32(&reqh->data_sn);
    4195                 :     969555 :         buffer_offset = from_be32(&reqh->buffer_offset);
    4196                 :            : 
    4197         [ +  + ]:     969555 :         if (pdu->data_segment_len > SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH) {
    4198                 :          6 :                 return iscsi_reject(conn, pdu, ISCSI_REASON_PROTOCOL_ERROR);
    4199                 :            :         }
    4200                 :            : 
    4201                 :     969549 :         task = get_transfer_task(conn, transfer_tag);
    4202         [ +  + ]:     969549 :         if (task == NULL) {
    4203                 :       3925 :                 SPDK_ERRLOG("Not found task for transfer_tag=%x\n", transfer_tag);
    4204                 :       3925 :                 return iscsi_reject(conn, pdu, ISCSI_REASON_INVALID_PDU_FIELD);
    4205                 :            :         }
    4206                 :            : 
    4207                 :     965624 :         lun_dev = spdk_scsi_dev_get_lun(conn->dev, task->lun_id);
    4208                 :     965624 :         current_desired_data_transfer_length = task->desired_data_transfer_length;
    4209                 :            : 
    4210         [ +  + ]:     965624 :         if (pdu->data_segment_len > task->desired_data_transfer_length) {
    4211                 :         14 :                 SPDK_ERRLOG("the dataout pdu data length is larger than the value sent by R2T PDU\n");
    4212                 :         14 :                 return SPDK_ISCSI_CONNECTION_FATAL;
    4213                 :            :         }
    4214                 :            : 
    4215         [ +  + ]:     965610 :         if (task->tag != task_tag) {
    4216                 :          8 :                 SPDK_ERRLOG("The r2t task tag is %u, and the dataout task tag is %u\n",
    4217                 :            :                             task->tag, task_tag);
    4218                 :          8 :                 return iscsi_reject(conn, pdu, ISCSI_REASON_INVALID_PDU_FIELD);
    4219                 :            :         }
    4220                 :            : 
    4221         [ +  + ]:     965602 :         if (DataSN != task->r2t_datasn) {
    4222                 :          6 :                 SPDK_ERRLOG("DataSN(%u) exp=%d error\n", DataSN, task->r2t_datasn);
    4223         [ -  + ]:          6 :                 if (conn->sess->ErrorRecoveryLevel >= 1) {
    4224                 :          0 :                         rc = iscsi_send_r2t_recovery(conn, task, task->acked_r2tsn, true);
    4225         [ #  # ]:          0 :                         if (rc == 0) {
    4226                 :          0 :                                 return 0;
    4227                 :            :                         }
    4228                 :            :                 }
    4229                 :          6 :                 return iscsi_reject(conn, pdu, ISCSI_REASON_PROTOCOL_ERROR);
    4230                 :            :         }
    4231                 :            : 
    4232         [ +  + ]:     965596 :         if (buffer_offset != task->next_expected_r2t_offset) {
    4233                 :          6 :                 SPDK_ERRLOG("offset(%u) error\n", buffer_offset);
    4234                 :          6 :                 return SPDK_ISCSI_CONNECTION_FATAL;
    4235                 :            :         }
    4236                 :            : 
    4237                 :     965590 :         transfer_len = task->scsi.transfer_len;
    4238                 :     965590 :         task->current_r2t_length += pdu->data_segment_len;
    4239                 :     965590 :         task->next_expected_r2t_offset += pdu->data_segment_len;
    4240                 :     965590 :         task->r2t_datasn++;
    4241                 :            : 
    4242         [ +  + ]:     965590 :         if (task->current_r2t_length > conn->sess->MaxBurstLength) {
    4243                 :          6 :                 SPDK_ERRLOG("R2T burst(%u) > MaxBurstLength(%u)\n",
    4244                 :            :                             task->current_r2t_length,
    4245                 :            :                             conn->sess->MaxBurstLength);
    4246                 :          6 :                 return SPDK_ISCSI_CONNECTION_FATAL;
    4247                 :            :         }
    4248                 :            : 
    4249         [ +  + ]:     965584 :         if (F_bit) {
    4250                 :            :                 /*
    4251                 :            :                  * This R2T burst is done. Clear the length before we
    4252                 :            :                  *  receive a PDU for the next R2t burst.
    4253                 :            :                  */
    4254                 :     538289 :                 task->current_r2t_length = 0;
    4255                 :            :         }
    4256                 :            : 
    4257         [ +  + ]:     965584 :         if (task->next_expected_r2t_offset == transfer_len) {
    4258                 :     538260 :                 task->acked_r2tsn++;
    4259   [ +  +  +  - ]:     427324 :         } else if (F_bit && (task->next_r2t_offset < transfer_len)) {
    4260                 :         29 :                 task->acked_r2tsn++;
    4261                 :         29 :                 len = spdk_min(conn->sess->MaxBurstLength,
    4262                 :            :                                (transfer_len - task->next_r2t_offset));
    4263                 :         29 :                 rc = iscsi_send_r2t(conn, task, task->next_r2t_offset, len,
    4264                 :            :                                     task->ttt, &task->R2TSN);
    4265         [ -  + ]:         29 :                 if (rc < 0) {
    4266                 :          0 :                         SPDK_ERRLOG("iscsi_send_r2t() failed\n");
    4267                 :            :                 }
    4268                 :         29 :                 task->next_r2t_offset += len;
    4269                 :            :         }
    4270                 :            : 
    4271         [ +  + ]:     965584 :         if (lun_dev == NULL) {
    4272   [ -  +  -  + ]:          9 :                 SPDK_DEBUGLOG(iscsi, "LUN %d is removed, reject this PDU.\n",
    4273                 :            :                               task->lun_id);
    4274                 :          9 :                 return iscsi_reject(conn, pdu, ISCSI_REASON_PROTOCOL_ERROR);
    4275         [ -  + ]:     965575 :         } else if (spdk_unlikely(spdk_scsi_lun_get_dif_ctx(lun_dev, &task->scsi, &pdu->dif_ctx))) {
    4276                 :          0 :                 pdu->dif_insert_or_strip = true;
    4277                 :            :         }
    4278                 :            : 
    4279                 :     965575 :         mobj = iscsi_task_get_mobj(task);
    4280         [ +  + ]:     965575 :         if (mobj == NULL) {
    4281   [ +  +  +  - ]:        375 :                 if (!pdu->dif_insert_or_strip) {
    4282                 :            :                         /* More Data-OUT PDUs may follow. Increase the buffer size up to
    4283                 :            :                          * SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH to merge them into a
    4284                 :            :                          * single subtask.
    4285                 :            :                          */
    4286                 :        375 :                         pdu->data_buf_len = spdk_min(current_desired_data_transfer_length,
    4287                 :            :                                                      SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH);
    4288                 :            :                 }
    4289                 :            :         } else {
    4290                 :            :                 /* Set up the data buffer from the one saved by the primary task. */
    4291                 :     965200 :                 pdu->mobj[0] = mobj;
    4292                 :     965200 :                 pdu->data = (void *)((uint64_t)mobj->buf + mobj->data_len);
    4293                 :     965200 :                 pdu->data_from_mempool = true;
    4294                 :     965200 :                 pdu->data_buf_len = SPDK_BDEV_BUF_SIZE_WITH_MD(iscsi_get_mobj_max_data_len(mobj));
    4295                 :            : 
    4296                 :     965200 :                 iscsi_task_set_mobj(task, NULL);
    4297                 :            :         }
    4298                 :            : 
    4299                 :     965575 :         return 0;
    4300                 :            : }
    4301                 :            : 
    4302                 :            : static int
    4303                 :     965563 : iscsi_pdu_payload_op_data(struct spdk_iscsi_conn *conn, struct spdk_iscsi_pdu *pdu)
    4304                 :            : {
    4305                 :            :         struct spdk_iscsi_task *task;
    4306                 :            :         struct iscsi_bhs_data_out *reqh;
    4307                 :            :         struct spdk_mobj *mobj;
    4308                 :            :         uint32_t transfer_tag;
    4309                 :            :         int F_bit;
    4310                 :            :         int rc;
    4311                 :            : 
    4312                 :     965563 :         reqh = (struct iscsi_bhs_data_out *)&pdu->bhs;
    4313                 :     965563 :         F_bit = !!(reqh->flags & ISCSI_FLAG_FINAL);
    4314                 :     965563 :         transfer_tag = from_be32(&reqh->ttt);
    4315                 :            : 
    4316                 :     965563 :         task = get_transfer_task(conn, transfer_tag);
    4317         [ -  + ]:     965563 :         if (spdk_unlikely(task == NULL)) {
    4318                 :          0 :                 SPDK_ERRLOG("Not found for transfer_tag=%x\n", transfer_tag);
    4319                 :          0 :                 return iscsi_reject(conn, pdu, ISCSI_REASON_INVALID_PDU_FIELD);
    4320                 :            :         }
    4321                 :            : 
    4322         [ +  + ]:     965563 :         if (spdk_scsi_dev_get_lun(conn->dev, task->lun_id) == NULL) {
    4323   [ -  +  -  + ]:          3 :                 SPDK_DEBUGLOG(iscsi, "LUN %d is removed, reject this PDU.\n",
    4324                 :            :                               task->lun_id);
    4325                 :          3 :                 return iscsi_reject(conn, pdu, ISCSI_REASON_PROTOCOL_ERROR);
    4326                 :            :         }
    4327                 :            : 
    4328                 :            :         /* If current PDU is final in a sequence, submit all received data,
    4329                 :            :          * otherwise, continue aggregation until the first data buffer is full.
    4330                 :            :          * We do not use SGL and instead create a subtask per data buffer. Hence further
    4331                 :            :          * aggregation does not improve any performance.
    4332                 :            :          */
    4333                 :     965560 :         mobj = pdu->mobj[0];
    4334         [ -  + ]:     965560 :         assert(mobj != NULL);
    4335                 :            : 
    4336   [ +  +  +  + ]:     965560 :         if (F_bit || mobj->data_len >= SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH ||
    4337   [ -  +  -  + ]:         44 :             pdu->dif_insert_or_strip) {
    4338                 :     965516 :                 rc = iscsi_submit_write_subtask(conn, task, pdu, mobj);
    4339         [ -  + ]:     965516 :                 if (rc != 0) {
    4340                 :          0 :                         return rc;
    4341                 :            :                 }
    4342                 :            :         } else {
    4343         [ -  + ]:         44 :                 assert(pdu->mobj[1] == NULL);
    4344                 :         44 :                 iscsi_task_set_mobj(task, mobj);
    4345                 :         44 :                 pdu->mobj[0] = NULL;
    4346                 :         44 :                 return 0;
    4347                 :            :         }
    4348                 :            : 
    4349                 :     965516 :         mobj = pdu->mobj[1];
    4350         [ +  + ]:     965516 :         if (mobj == NULL) {
    4351                 :     537287 :                 return 0;
    4352                 :            :         }
    4353                 :            : 
    4354   [ -  +  -  + ]:     428229 :         assert(pdu->dif_insert_or_strip == false);
    4355         [ -  + ]:     428229 :         assert(mobj->data_len < SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH);
    4356                 :            : 
    4357         [ +  + ]:     428229 :         if (F_bit) {
    4358                 :       1246 :                 return iscsi_submit_write_subtask(conn, task, pdu, mobj);
    4359                 :            :         } else {
    4360                 :     426983 :                 iscsi_task_set_mobj(task, mobj);
    4361                 :     426983 :                 pdu->mobj[1] = NULL;
    4362                 :     426983 :                 return 0;
    4363                 :            :         }
    4364                 :            : }
    4365                 :            : 
    4366                 :            : static void
    4367                 :          0 : init_login_reject_response(struct spdk_iscsi_pdu *pdu, struct spdk_iscsi_pdu *rsp_pdu)
    4368                 :            : {
    4369                 :            :         struct iscsi_bhs_login_rsp *rsph;
    4370                 :            : 
    4371         [ #  # ]:          0 :         memset(rsp_pdu, 0, sizeof(struct spdk_iscsi_pdu));
    4372                 :          0 :         rsph = (struct iscsi_bhs_login_rsp *)&rsp_pdu->bhs;
    4373                 :          0 :         rsph->version_max = ISCSI_VERSION;
    4374                 :          0 :         rsph->version_act = ISCSI_VERSION;
    4375                 :          0 :         rsph->opcode = ISCSI_OP_LOGIN_RSP;
    4376                 :          0 :         rsph->status_class = ISCSI_CLASS_INITIATOR_ERROR;
    4377                 :          0 :         rsph->status_detail = ISCSI_LOGIN_INVALID_LOGIN_REQUEST;
    4378                 :          0 :         rsph->itt = pdu->bhs.itt;
    4379                 :          0 : }
    4380                 :            : 
    4381                 :            : static void
    4382                 :          4 : iscsi_pdu_dump(struct spdk_iscsi_pdu *pdu)
    4383                 :            : {
    4384                 :          4 :         spdk_log_dump(stderr, "PDU", (uint8_t *)&pdu->bhs, ISCSI_BHS_LEN);
    4385                 :          4 : }
    4386                 :            : 
    4387                 :            : /* This function is used to refree the pdu when it is acknowledged */
    4388                 :            : static void
    4389                 :       1944 : remove_acked_pdu(struct spdk_iscsi_conn *conn, uint32_t ExpStatSN)
    4390                 :            : {
    4391                 :            :         struct spdk_iscsi_pdu *pdu, *pdu_temp;
    4392                 :            :         uint32_t stat_sn;
    4393                 :            : 
    4394                 :       1944 :         conn->exp_statsn = spdk_min(ExpStatSN, conn->StatSN);
    4395         [ +  + ]:       3560 :         TAILQ_FOREACH_SAFE(pdu, &conn->snack_pdu_list, tailq, pdu_temp) {
    4396                 :       1616 :                 stat_sn = from_be32(&pdu->bhs.stat_sn);
    4397         [ +  + ]:       1616 :                 if (spdk_sn32_lt(stat_sn, conn->exp_statsn)) {
    4398         [ +  + ]:       1182 :                         TAILQ_REMOVE(&conn->snack_pdu_list, pdu, tailq);
    4399                 :       1182 :                         iscsi_conn_free_pdu(conn, pdu);
    4400                 :            :                 }
    4401                 :            :         }
    4402                 :       1944 : }
    4403                 :            : 
    4404                 :            : static int
    4405                 :   24737205 : iscsi_update_cmdsn(struct spdk_iscsi_conn *conn, struct spdk_iscsi_pdu *pdu)
    4406                 :            : {
    4407                 :            :         int opcode;
    4408                 :            :         uint32_t ExpStatSN;
    4409                 :            :         int I_bit;
    4410                 :            :         struct spdk_iscsi_sess *sess;
    4411                 :            :         struct iscsi_bhs_scsi_req *reqh;
    4412                 :            : 
    4413                 :   24737205 :         sess = conn->sess;
    4414         [ -  + ]:   24737205 :         if (!sess) {
    4415                 :          0 :                 SPDK_ERRLOG("Connection has no associated session!\n");
    4416                 :          0 :                 return SPDK_ISCSI_CONNECTION_FATAL;
    4417                 :            :         }
    4418                 :            : 
    4419                 :   24737205 :         opcode = pdu->bhs.opcode;
    4420                 :   24737205 :         reqh = (struct iscsi_bhs_scsi_req *)&pdu->bhs;
    4421                 :            : 
    4422                 :   24737205 :         pdu->cmd_sn = from_be32(&reqh->cmd_sn);
    4423                 :            : 
    4424                 :   24737205 :         I_bit = reqh->immediate;
    4425         [ +  + ]:   24737205 :         if (I_bit == 0) {
    4426   [ +  +  +  + ]:   48049916 :                 if (spdk_sn32_lt(pdu->cmd_sn, sess->ExpCmdSN) ||
    4427                 :   23542059 :                     spdk_sn32_gt(pdu->cmd_sn, sess->MaxCmdSN)) {
    4428   [ +  -  +  + ]:     965800 :                         if (sess->session_type == SESSION_TYPE_NORMAL &&
    4429                 :            :                             opcode != ISCSI_OP_SCSI_DATAOUT) {
    4430                 :        102 :                                 SPDK_ERRLOG("CmdSN(%u) ignore (ExpCmdSN=%u, MaxCmdSN=%u)\n",
    4431                 :            :                                             pdu->cmd_sn, sess->ExpCmdSN, sess->MaxCmdSN);
    4432                 :            : 
    4433         [ +  - ]:        102 :                                 if (sess->ErrorRecoveryLevel >= 1) {
    4434   [ -  +  -  + ]:        102 :                                         SPDK_DEBUGLOG(iscsi, "Skip the error in ERL 1 and 2\n");
    4435                 :            :                                 } else {
    4436                 :          0 :                                         return SPDK_PDU_FATAL;
    4437                 :            :                                 }
    4438                 :            :                         }
    4439                 :            :                 }
    4440         [ +  + ]:     229348 :         } else if (pdu->cmd_sn != sess->ExpCmdSN) {
    4441                 :        190 :                 SPDK_ERRLOG("CmdSN(%u) error ExpCmdSN=%u\n", pdu->cmd_sn, sess->ExpCmdSN);
    4442                 :            : 
    4443         [ +  - ]:        190 :                 if (sess->ErrorRecoveryLevel >= 1) {
    4444   [ -  +  -  + ]:        190 :                         SPDK_DEBUGLOG(iscsi, "Skip the error in ERL 1 and 2\n");
    4445         [ #  # ]:          0 :                 } else if (opcode != ISCSI_OP_NOPOUT) {
    4446                 :            :                         /*
    4447                 :            :                          * The Linux initiator does not send valid CmdSNs for
    4448                 :            :                          *  nopout under heavy load, so do not close the
    4449                 :            :                          *  connection in that case.
    4450                 :            :                          */
    4451                 :          0 :                         return SPDK_ISCSI_CONNECTION_FATAL;
    4452                 :            :                 }
    4453                 :            :         }
    4454                 :            : 
    4455                 :   24737205 :         ExpStatSN = from_be32(&reqh->exp_stat_sn);
    4456         [ +  + ]:   24737205 :         if (spdk_sn32_gt(ExpStatSN, conn->StatSN)) {
    4457   [ -  +  -  + ]:     114800 :                 SPDK_DEBUGLOG(iscsi, "StatSN(%u) advanced\n", ExpStatSN);
    4458                 :     114800 :                 ExpStatSN = conn->StatSN;
    4459                 :            :         }
    4460                 :            : 
    4461         [ +  + ]:   24737205 :         if (sess->ErrorRecoveryLevel >= 1) {
    4462                 :       1944 :                 remove_acked_pdu(conn, ExpStatSN);
    4463                 :            :         }
    4464                 :            : 
    4465   [ +  +  +  + ]:   24737205 :         if (!I_bit && opcode != ISCSI_OP_SCSI_DATAOUT) {
    4466                 :   23542135 :                 sess->ExpCmdSN++;
    4467                 :            :         }
    4468                 :            : 
    4469                 :   24737205 :         return 0;
    4470                 :            : }
    4471                 :            : 
    4472                 :            : static int
    4473                 :   24738810 : iscsi_pdu_hdr_handle(struct spdk_iscsi_conn *conn, struct spdk_iscsi_pdu *pdu)
    4474                 :            : {
    4475                 :            :         int opcode;
    4476                 :            :         int rc;
    4477                 :   24738810 :         struct spdk_iscsi_pdu *rsp_pdu = NULL;
    4478                 :            : 
    4479         [ -  + ]:   24738810 :         if (pdu == NULL) {
    4480                 :          0 :                 return -1;
    4481                 :            :         }
    4482                 :            : 
    4483                 :   24738810 :         opcode = pdu->bhs.opcode;
    4484                 :            : 
    4485   [ -  +  -  + ]:   24738810 :         SPDK_DEBUGLOG(iscsi, "opcode %x\n", opcode);
    4486                 :            : 
    4487         [ +  + ]:   24738810 :         if (opcode == ISCSI_OP_LOGIN) {
    4488                 :       1601 :                 return iscsi_pdu_hdr_op_login(conn, pdu);
    4489                 :            :         }
    4490                 :            : 
    4491                 :            :         /* connection in login phase but receive non-login opcode
    4492                 :            :          * return response code 0x020b to initiator.
    4493                 :            :          * */
    4494   [ +  +  -  + ]:   24737209 :         if (!conn->full_feature && conn->state == ISCSI_CONN_STATE_RUNNING) {
    4495                 :          0 :                 rsp_pdu = iscsi_get_pdu(conn);
    4496         [ #  # ]:          0 :                 if (rsp_pdu == NULL) {
    4497                 :          0 :                         return SPDK_ISCSI_CONNECTION_FATAL;
    4498                 :            :                 }
    4499                 :          0 :                 init_login_reject_response(pdu, rsp_pdu);
    4500                 :          0 :                 iscsi_conn_write_pdu(conn, rsp_pdu, iscsi_conn_pdu_generic_complete, NULL);
    4501                 :          0 :                 SPDK_ERRLOG("Received opcode %d in login phase\n", opcode);
    4502                 :          0 :                 return SPDK_ISCSI_LOGIN_ERROR_RESPONSE;
    4503         [ +  + ]:   24737209 :         } else if (conn->state == ISCSI_CONN_STATE_INVALID) {
    4504                 :          4 :                 SPDK_ERRLOG("before Full Feature\n");
    4505                 :          4 :                 iscsi_pdu_dump(pdu);
    4506                 :          4 :                 return SPDK_ISCSI_CONNECTION_FATAL;
    4507                 :            :         }
    4508                 :            : 
    4509                 :   24737205 :         rc = iscsi_update_cmdsn(conn, pdu);
    4510         [ -  + ]:   24737205 :         if (rc != 0) {
    4511                 :          0 :                 return rc;
    4512                 :            :         }
    4513                 :            : 
    4514   [ +  +  +  +  :   24737205 :         switch (opcode) {
             +  +  +  + ]
    4515                 :       3842 :         case ISCSI_OP_NOPOUT:
    4516                 :       3842 :                 rc = iscsi_pdu_hdr_op_nopout(conn, pdu);
    4517                 :       3842 :                 break;
    4518                 :            : 
    4519                 :   23545681 :         case ISCSI_OP_SCSI:
    4520                 :   23545681 :                 rc = iscsi_pdu_hdr_op_scsi(conn, pdu);
    4521                 :   23545681 :                 break;
    4522                 :       3765 :         case ISCSI_OP_TASK:
    4523                 :       3765 :                 rc = iscsi_pdu_hdr_op_task(conn, pdu);
    4524                 :       3765 :                 break;
    4525                 :            : 
    4526                 :       3833 :         case ISCSI_OP_TEXT:
    4527                 :       3833 :                 rc = iscsi_pdu_hdr_op_text(conn, pdu);
    4528                 :       3833 :                 break;
    4529                 :            : 
    4530                 :        357 :         case ISCSI_OP_LOGOUT:
    4531                 :        357 :                 rc = iscsi_pdu_hdr_op_logout(conn, pdu);
    4532                 :        357 :                 break;
    4533                 :            : 
    4534                 :     969495 :         case ISCSI_OP_SCSI_DATAOUT:
    4535                 :     969495 :                 rc = iscsi_pdu_hdr_op_data(conn, pdu);
    4536                 :     969495 :                 break;
    4537                 :            : 
    4538                 :       3838 :         case ISCSI_OP_SNACK:
    4539                 :       3838 :                 rc = iscsi_pdu_hdr_op_snack(conn, pdu);
    4540                 :       3838 :                 break;
    4541                 :            : 
    4542                 :     206394 :         default:
    4543                 :     206394 :                 SPDK_ERRLOG("unsupported opcode %x\n", opcode);
    4544                 :     206394 :                 return iscsi_reject(conn, pdu, ISCSI_REASON_PROTOCOL_ERROR);
    4545                 :            :         }
    4546                 :            : 
    4547         [ +  + ]:   24530811 :         if (rc < 0) {
    4548   [ +  +  +  + ]:         10 :                 SPDK_ERRLOG("processing PDU header (opcode=%x) failed on %s(%s)\n",
    4549                 :            :                             opcode,
    4550                 :            :                             conn->target_port != NULL ? spdk_scsi_port_get_name(conn->target_port) : "NULL",
    4551                 :            :                             conn->initiator_port != NULL ? spdk_scsi_port_get_name(conn->initiator_port) : "NULL");
    4552                 :            :         }
    4553                 :            : 
    4554                 :   24530811 :         return rc;
    4555                 :            : }
    4556                 :            : 
    4557                 :            : static int
    4558                 :   24524699 : iscsi_pdu_payload_handle(struct spdk_iscsi_conn *conn, struct spdk_iscsi_pdu *pdu)
    4559                 :            : {
    4560                 :            :         int opcode;
    4561                 :   24524699 :         int rc = 0;
    4562                 :            : 
    4563                 :   24524699 :         opcode = pdu->bhs.opcode;
    4564                 :            : 
    4565   [ -  +  -  + ]:   24524699 :         SPDK_DEBUGLOG(iscsi, "opcode %x\n", opcode);
    4566                 :            : 
    4567   [ +  +  +  +  :   24524699 :         switch (opcode) {
             +  +  +  +  
                      - ]
    4568                 :       1601 :         case ISCSI_OP_LOGIN:
    4569                 :       1601 :                 rc = iscsi_pdu_payload_op_login(conn, pdu);
    4570                 :       1601 :                 break;
    4571                 :       3842 :         case ISCSI_OP_NOPOUT:
    4572                 :       3842 :                 rc = iscsi_pdu_payload_op_nopout(conn, pdu);
    4573                 :       3842 :                 break;
    4574                 :   23545672 :         case ISCSI_OP_SCSI:
    4575                 :   23545672 :                 rc = iscsi_pdu_payload_op_scsi(conn, pdu);
    4576                 :   23545672 :                 break;
    4577                 :       3765 :         case ISCSI_OP_TASK:
    4578                 :       3765 :                 break;
    4579                 :       3833 :         case ISCSI_OP_TEXT:
    4580                 :       3833 :                 rc = iscsi_pdu_payload_op_text(conn, pdu);
    4581                 :       3833 :                 break;
    4582                 :        357 :         case ISCSI_OP_LOGOUT:
    4583                 :        357 :                 break;
    4584                 :     965563 :         case ISCSI_OP_SCSI_DATAOUT:
    4585                 :     965563 :                 rc = iscsi_pdu_payload_op_data(conn, pdu);
    4586                 :     965563 :                 break;
    4587                 :         66 :         case ISCSI_OP_SNACK:
    4588                 :         66 :                 break;
    4589                 :          0 :         default:
    4590                 :          0 :                 SPDK_ERRLOG("unsupported opcode %x\n", opcode);
    4591                 :          0 :                 return iscsi_reject(conn, pdu, ISCSI_REASON_PROTOCOL_ERROR);
    4592                 :            :         }
    4593                 :            : 
    4594         [ -  + ]:   24524699 :         if (rc < 0) {
    4595   [ #  #  #  # ]:          0 :                 SPDK_ERRLOG("processing PDU payload (opcode=%x) failed on %s(%s)\n",
    4596                 :            :                             opcode,
    4597                 :            :                             conn->target_port != NULL ? spdk_scsi_port_get_name(conn->target_port) : "NULL",
    4598                 :            :                             conn->initiator_port != NULL ? spdk_scsi_port_get_name(conn->initiator_port) : "NULL");
    4599                 :            :         }
    4600                 :            : 
    4601                 :   24524699 :         return rc;
    4602                 :            : }
    4603                 :            : 
    4604                 :            : /* Return zero if completed to read payload, positive number if still in progress,
    4605                 :            :  * or negative number if any error.
    4606                 :            :  */
    4607                 :            : static int
    4608                 :   14550766 : iscsi_pdu_payload_read(struct spdk_iscsi_conn *conn, struct spdk_iscsi_pdu *pdu)
    4609                 :            : {
    4610                 :            :         struct spdk_mempool *pool;
    4611                 :            :         struct spdk_mobj *mobj;
    4612                 :            :         uint32_t data_len;
    4613                 :            :         uint32_t read_len;
    4614                 :            :         uint32_t crc32c;
    4615                 :            :         int rc;
    4616                 :            :         uint32_t data_buf_len;
    4617                 :            : 
    4618                 :   14550766 :         data_len = pdu->data_segment_len;
    4619                 :   14550766 :         read_len = data_len - pdu->data_valid_bytes;
    4620                 :   14550766 :         data_buf_len = pdu->data_buf_len;
    4621                 :            : 
    4622                 :   14550766 :         mobj = pdu->mobj[0];
    4623         [ +  + ]:   14550766 :         if (mobj == NULL) {
    4624         [ +  + ]:    9924218 :                 if (data_buf_len <= iscsi_get_max_immediate_data_size()) {
    4625                 :    9385565 :                         pool = g_iscsi.pdu_immediate_data_pool;
    4626                 :    9385565 :                         data_buf_len = SPDK_BDEV_BUF_SIZE_WITH_MD(iscsi_get_max_immediate_data_size());
    4627         [ +  + ]:     538653 :                 } else if (data_buf_len <= SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH) {
    4628                 :     538647 :                         pool = g_iscsi.pdu_data_out_pool;
    4629                 :     538647 :                         data_buf_len = SPDK_BDEV_BUF_SIZE_WITH_MD(SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH);
    4630                 :            :                 } else {
    4631                 :          6 :                         SPDK_ERRLOG("Data(%d) > MaxSegment(%d)\n",
    4632                 :            :                                     data_len, SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH);
    4633                 :          6 :                         return -1;
    4634                 :            :                 }
    4635                 :    9924212 :                 mobj = iscsi_datapool_get(pool);
    4636         [ -  + ]:    9924212 :                 if (mobj == NULL) {
    4637                 :          0 :                         return 1;
    4638                 :            :                 }
    4639                 :            : 
    4640                 :    9924212 :                 pdu->data_buf_len = data_buf_len;
    4641                 :            : 
    4642                 :    9924212 :                 pdu->mobj[0] = mobj;
    4643                 :    9924212 :                 pdu->data = mobj->buf;
    4644                 :    9924212 :                 pdu->data_from_mempool = true;
    4645   [ +  +  +  + ]:    4626548 :         } else if (mobj->data_len == iscsi_get_mobj_max_data_len(mobj) && read_len > 0) {
    4646                 :     466683 :                 mobj = pdu->mobj[1];
    4647         [ +  + ]:     466683 :                 if (mobj == NULL) {
    4648                 :            :                         /* The first data buffer just ran out. Allocate the second data buffer and
    4649                 :            :                          * continue reading the data segment.
    4650                 :            :                          */
    4651   [ -  +  -  + ]:     428238 :                         assert(pdu->data_from_mempool == true);
    4652   [ -  +  -  + ]:     428238 :                         assert(!pdu->dif_insert_or_strip);
    4653                 :            : 
    4654         [ -  + ]:     428238 :                         if (conn->data_digest) {
    4655                 :          0 :                                 iscsi_pdu_calc_partial_data_digest(pdu);
    4656                 :            :                         }
    4657                 :     428238 :                         mobj = iscsi_datapool_get(g_iscsi.pdu_data_out_pool);
    4658         [ -  + ]:     428238 :                         if (mobj == NULL) {
    4659                 :          0 :                                 return 1;
    4660                 :            :                         }
    4661                 :     428238 :                         pdu->mobj[1] = mobj;
    4662                 :     428238 :                         pdu->data = mobj->buf;
    4663                 :     428238 :                         pdu->data_offset = pdu->data_valid_bytes;
    4664                 :     428238 :                         pdu->data_buf_len = SPDK_BDEV_BUF_SIZE_WITH_MD(SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH);
    4665                 :            :                 }
    4666                 :            :         }
    4667                 :            : 
    4668                 :            :         /* copy the actual data into local buffer */
    4669         [ +  + ]:   14550760 :         read_len = spdk_min(read_len, iscsi_get_mobj_max_data_len(mobj) - mobj->data_len);
    4670                 :            : 
    4671         [ +  + ]:   14550760 :         if (read_len > 0) {
    4672                 :   14550754 :                 rc = iscsi_conn_read_data_segment(conn,
    4673                 :            :                                                   pdu,
    4674                 :   14550754 :                                                   pdu->data_valid_bytes - pdu->data_offset,
    4675                 :            :                                                   read_len);
    4676         [ -  + ]:   14550754 :                 if (rc < 0) {
    4677                 :          0 :                         return rc;
    4678                 :            :                 }
    4679                 :            : 
    4680                 :   14550754 :                 mobj->data_len += rc;
    4681                 :   14550754 :                 pdu->data_valid_bytes += rc;
    4682         [ +  + ]:   14550754 :                 if (pdu->data_valid_bytes < data_len) {
    4683                 :    3661336 :                         return 1;
    4684                 :            :                 }
    4685                 :            :         }
    4686                 :            : 
    4687                 :            :         /* copy out the data digest */
    4688         [ +  + ]:   10889424 :         if (conn->data_digest &&
    4689         [ +  - ]:          6 :             pdu->ddigest_valid_bytes < ISCSI_DIGEST_LEN) {
    4690                 :         14 :                 rc = iscsi_conn_read_data(conn,
    4691                 :          6 :                                           ISCSI_DIGEST_LEN - pdu->ddigest_valid_bytes,
    4692                 :          6 :                                           pdu->data_digest + pdu->ddigest_valid_bytes);
    4693         [ -  + ]:          6 :                 if (rc < 0) {
    4694                 :          0 :                         return rc;
    4695                 :            :                 }
    4696                 :            : 
    4697                 :          6 :                 pdu->ddigest_valid_bytes += rc;
    4698         [ -  + ]:          6 :                 if (pdu->ddigest_valid_bytes < ISCSI_DIGEST_LEN) {
    4699                 :          0 :                         return 1;
    4700                 :            :                 }
    4701                 :            :         }
    4702                 :            : 
    4703                 :            :         /* check data digest */
    4704         [ +  + ]:   10889424 :         if (conn->data_digest) {
    4705                 :          6 :                 iscsi_pdu_calc_partial_data_digest(pdu);
    4706                 :          6 :                 crc32c = iscsi_pdu_calc_partial_data_digest_done(pdu);
    4707                 :            : 
    4708                 :          6 :                 rc = MATCH_DIGEST_WORD(pdu->data_digest, crc32c);
    4709         [ -  + ]:          6 :                 if (rc == 0) {
    4710                 :          0 :                         SPDK_ERRLOG("data digest error (%s)\n", conn->initiator_name);
    4711                 :          0 :                         return -1;
    4712                 :            :                 }
    4713                 :            :         }
    4714                 :            : 
    4715                 :   10889424 :         return 0;
    4716                 :            : }
    4717                 :            : 
    4718                 :            : static int
    4719                 :   91163256 : iscsi_read_pdu(struct spdk_iscsi_conn *conn)
    4720                 :            : {
    4721                 :            :         enum iscsi_pdu_recv_state prev_state;
    4722                 :            :         struct spdk_iscsi_pdu *pdu;
    4723                 :            :         uint32_t crc32c;
    4724                 :            :         int ahs_len;
    4725                 :            :         int rc;
    4726                 :            : 
    4727                 :            :         do {
    4728                 :  140641822 :                 prev_state = conn->pdu_recv_state;
    4729                 :  140641822 :                 pdu = conn->pdu_in_progress;
    4730                 :            : 
    4731   [ +  +  +  +  :  140641822 :                 switch (conn->pdu_recv_state) {
                      - ]
    4732                 :   24739283 :                 case ISCSI_PDU_RECV_STATE_AWAIT_PDU_READY:
    4733         [ -  + ]:   24739283 :                         assert(conn->pdu_in_progress == NULL);
    4734                 :            : 
    4735                 :   24739283 :                         conn->pdu_in_progress = iscsi_get_pdu(conn);
    4736         [ -  + ]:   24739283 :                         if (conn->pdu_in_progress == NULL) {
    4737                 :          0 :                                 return SPDK_ISCSI_CONNECTION_FATAL;
    4738                 :            :                         }
    4739                 :   24739283 :                         conn->pdu_recv_state = ISCSI_PDU_RECV_STATE_AWAIT_PDU_HDR;
    4740                 :   24739283 :                         break;
    4741                 :   87501938 :                 case ISCSI_PDU_RECV_STATE_AWAIT_PDU_HDR:
    4742         [ +  - ]:   87501938 :                         if (pdu->bhs_valid_bytes < ISCSI_BHS_LEN) {
    4743                 :  262505814 :                                 rc = iscsi_conn_read_data(conn,
    4744                 :   87501938 :                                                           ISCSI_BHS_LEN - pdu->bhs_valid_bytes,
    4745                 :   87501938 :                                                           (uint8_t *)&pdu->bhs + pdu->bhs_valid_bytes);
    4746         [ +  + ]:   87501938 :                                 if (rc < 0) {
    4747                 :        553 :                                         conn->pdu_recv_state = ISCSI_PDU_RECV_STATE_ERROR;
    4748                 :        553 :                                         break;
    4749                 :            :                                 }
    4750                 :   87501385 :                                 pdu->bhs_valid_bytes += rc;
    4751         [ +  + ]:   87501385 :                                 if (pdu->bhs_valid_bytes < ISCSI_BHS_LEN) {
    4752                 :   62762655 :                                         return 0;
    4753                 :            :                                 }
    4754                 :            :                         }
    4755                 :            : 
    4756                 :            :                         /* conn->is_logged_out must be checked after completing to process
    4757                 :            :                          * logout request, i.e., before processing PDU header in this state
    4758                 :            :                          * machine, otherwise logout response may not be sent to initiator
    4759                 :            :                          * and initiator may get logout timeout.
    4760                 :            :                          */
    4761   [ -  +  +  + ]:   24738730 :                         if (spdk_unlikely(conn->is_logged_out)) {
    4762   [ -  +  -  + ]:          4 :                                 SPDK_DEBUGLOG(iscsi, "pdu received after logout\n");
    4763                 :          4 :                                 conn->pdu_recv_state = ISCSI_PDU_RECV_STATE_ERROR;
    4764                 :          4 :                                 break;
    4765                 :            :                         }
    4766                 :            : 
    4767                 :   24738726 :                         pdu->data_segment_len = ISCSI_ALIGN(DGET24(pdu->bhs.data_segment_len));
    4768                 :   24738726 :                         pdu->data_buf_len = pdu->data_segment_len;
    4769                 :            : 
    4770                 :            :                         /* AHS */
    4771                 :   24738726 :                         ahs_len = pdu->bhs.total_ahs_len * 4;
    4772         [ -  + ]:   24738726 :                         if (ahs_len > ISCSI_AHS_LEN) {
    4773   [ #  #  #  # ]:          0 :                                 SPDK_DEBUGLOG(iscsi, "pdu ahs length %d is invalid\n", ahs_len);
    4774                 :          0 :                                 conn->pdu_recv_state = ISCSI_PDU_RECV_STATE_ERROR;
    4775                 :          0 :                                 break;
    4776                 :            :                         }
    4777                 :            : 
    4778         [ -  + ]:   24738726 :                         if (pdu->ahs_valid_bytes < ahs_len) {
    4779                 :          0 :                                 rc = iscsi_conn_read_data(conn,
    4780                 :          0 :                                                           ahs_len - pdu->ahs_valid_bytes,
    4781                 :          0 :                                                           pdu->ahs + pdu->ahs_valid_bytes);
    4782         [ #  # ]:          0 :                                 if (rc < 0) {
    4783                 :          0 :                                         conn->pdu_recv_state = ISCSI_PDU_RECV_STATE_ERROR;
    4784                 :          0 :                                         break;
    4785                 :            :                                 }
    4786                 :            : 
    4787                 :          0 :                                 pdu->ahs_valid_bytes += rc;
    4788         [ #  # ]:          0 :                                 if (pdu->ahs_valid_bytes < ahs_len) {
    4789                 :          0 :                                         return 0;
    4790                 :            :                                 }
    4791                 :            :                         }
    4792                 :            : 
    4793                 :            :                         /* Header Digest */
    4794         [ +  + ]:   24738726 :                         if (conn->header_digest &&
    4795         [ +  - ]:     185642 :                             pdu->hdigest_valid_bytes < ISCSI_DIGEST_LEN) {
    4796                 :     556926 :                                 rc = iscsi_conn_read_data(conn,
    4797                 :     185642 :                                                           ISCSI_DIGEST_LEN - pdu->hdigest_valid_bytes,
    4798                 :     185642 :                                                           pdu->header_digest + pdu->hdigest_valid_bytes);
    4799         [ -  + ]:     185642 :                                 if (rc < 0) {
    4800                 :          0 :                                         conn->pdu_recv_state = ISCSI_PDU_RECV_STATE_ERROR;
    4801                 :          0 :                                         break;
    4802                 :            :                                 }
    4803                 :            : 
    4804                 :     185642 :                                 pdu->hdigest_valid_bytes += rc;
    4805         [ -  + ]:     185642 :                                 if (pdu->hdigest_valid_bytes < ISCSI_DIGEST_LEN) {
    4806                 :          0 :                                         return 0;
    4807                 :            :                                 }
    4808                 :            :                         }
    4809                 :            : 
    4810         [ +  + ]:   24738726 :                         if (conn->header_digest) {
    4811                 :     185642 :                                 crc32c = iscsi_pdu_calc_header_digest(pdu);
    4812                 :     185642 :                                 rc = MATCH_DIGEST_WORD(pdu->header_digest, crc32c);
    4813         [ -  + ]:     185642 :                                 if (rc == 0) {
    4814                 :          0 :                                         SPDK_ERRLOG("header digest error (%s)\n", conn->initiator_name);
    4815                 :          0 :                                         conn->pdu_recv_state = ISCSI_PDU_RECV_STATE_ERROR;
    4816                 :          0 :                                         break;
    4817                 :            :                                 }
    4818                 :            :                         }
    4819                 :            : 
    4820                 :   24738726 :                         rc = iscsi_pdu_hdr_handle(conn, pdu);
    4821         [ +  + ]:   24738726 :                         if (rc < 0) {
    4822                 :          8 :                                 SPDK_ERRLOG("Critical error is detected. Close the connection\n");
    4823                 :          8 :                                 conn->pdu_recv_state = ISCSI_PDU_RECV_STATE_ERROR;
    4824                 :          8 :                                 break;
    4825                 :            :                         }
    4826                 :            : 
    4827                 :   24738718 :                         conn->pdu_recv_state = ISCSI_PDU_RECV_STATE_AWAIT_PDU_PAYLOAD;
    4828                 :   24738718 :                         break;
    4829                 :   28400036 :                 case ISCSI_PDU_RECV_STATE_AWAIT_PDU_PAYLOAD:
    4830         [ +  + ]:   28400036 :                         if (pdu->data_segment_len != 0) {
    4831                 :   14550652 :                                 rc = iscsi_pdu_payload_read(conn, pdu);
    4832         [ +  + ]:   14550652 :                                 if (rc > 0) {
    4833                 :    3661318 :                                         return 0;
    4834         [ -  + ]:   10889334 :                                 } else if (rc < 0) {
    4835                 :          0 :                                         conn->pdu_recv_state = ISCSI_PDU_RECV_STATE_ERROR;
    4836                 :          0 :                                         break;
    4837                 :            :                                 }
    4838                 :            :                         }
    4839                 :            : 
    4840                 :            :                         /* All data for this PDU has now been read from the socket. */
    4841   [ +  -  +  + ]:   24738718 :                         spdk_trace_record(TRACE_ISCSI_READ_PDU, conn->id, pdu->data_valid_bytes,
    4842                 :            :                                           (uintptr_t)pdu, pdu->bhs.opcode);
    4843                 :            : 
    4844   [ -  +  +  + ]:   24738718 :                         if (!pdu->is_rejected) {
    4845                 :   24524621 :                                 rc = iscsi_pdu_payload_handle(conn, pdu);
    4846                 :            :                         } else {
    4847                 :     214097 :                                 rc = 0;
    4848                 :            :                         }
    4849         [ +  - ]:   24738718 :                         if (rc == 0) {
    4850   [ +  -  +  + ]:   24738718 :                                 spdk_trace_record(TRACE_ISCSI_TASK_EXECUTED, 0, 0, (uintptr_t)pdu);
    4851                 :   24738718 :                                 iscsi_put_pdu(pdu);
    4852                 :   24738718 :                                 conn->pdu_in_progress = NULL;
    4853                 :   24738718 :                                 conn->pdu_recv_state = ISCSI_PDU_RECV_STATE_AWAIT_PDU_READY;
    4854                 :   24738718 :                                 return 1;
    4855                 :            :                         } else {
    4856                 :          0 :                                 conn->pdu_recv_state = ISCSI_PDU_RECV_STATE_ERROR;
    4857                 :            :                         }
    4858                 :          0 :                         break;
    4859                 :        565 :                 case ISCSI_PDU_RECV_STATE_ERROR:
    4860                 :        565 :                         return SPDK_ISCSI_CONNECTION_FATAL;
    4861                 :          0 :                 default:
    4862                 :          0 :                         assert(false);
    4863                 :            :                         SPDK_ERRLOG("code should not come here\n");
    4864                 :            :                         break;
    4865                 :            :                 }
    4866         [ +  - ]:   49478566 :         } while (prev_state != conn->pdu_recv_state);
    4867                 :            : 
    4868                 :          0 :         return 0;
    4869                 :            : }
    4870                 :            : 
    4871                 :            : #define GET_PDU_LOOP_COUNT      16
    4872                 :            : 
    4873                 :            : int
    4874                 :   66803570 : iscsi_handle_incoming_pdus(struct spdk_iscsi_conn *conn)
    4875                 :            : {
    4876                 :            :         int i, rc;
    4877                 :            : 
    4878                 :            :         /* Read new PDUs from network */
    4879         [ +  + ]:   91542288 :         for (i = 0; i < GET_PDU_LOOP_COUNT; i++) {
    4880                 :   91163256 :                 rc = iscsi_read_pdu(conn);
    4881         [ +  + ]:   91163256 :                 if (rc == 0) {
    4882                 :   66423973 :                         break;
    4883         [ +  + ]:   24739283 :                 } else if (rc < 0) {
    4884                 :        565 :                         return rc;
    4885                 :            :                 }
    4886                 :            : 
    4887   [ -  +  -  + ]:   24738718 :                 if (conn->is_stopped) {
    4888                 :          0 :                         break;
    4889                 :            :                 }
    4890                 :            :         }
    4891                 :            : 
    4892                 :   66803005 :         return i;
    4893                 :            : }

Generated by: LCOV version 1.14