LCOV - code coverage report
Current view: top level - spdk/lib/nvme - nvme_tcp.c (source / functions) Hit Total Coverage
Test: Combined Lines: 1209 1532 78.9 %
Date: 2024-07-15 12:59:45 Functions: 86 96 89.6 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 642 949 67.7 %

           Branch data     Line data    Source code
       1                 :            : /*   SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  *   Copyright (C) 2018 Intel Corporation. All rights reserved.
       3                 :            :  *   Copyright (c) 2020 Mellanox Technologies LTD. All rights reserved.
       4                 :            :  *   Copyright (c) 2021-2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
       5                 :            :  */
       6                 :            : 
       7                 :            : /*
       8                 :            :  * NVMe/TCP transport
       9                 :            :  */
      10                 :            : 
      11                 :            : #include "nvme_internal.h"
      12                 :            : 
      13                 :            : #include "spdk/endian.h"
      14                 :            : #include "spdk/likely.h"
      15                 :            : #include "spdk/string.h"
      16                 :            : #include "spdk/stdinc.h"
      17                 :            : #include "spdk/crc32.h"
      18                 :            : #include "spdk/assert.h"
      19                 :            : #include "spdk/trace.h"
      20                 :            : #include "spdk/util.h"
      21                 :            : #include "spdk/nvmf.h"
      22                 :            : #include "spdk/dma.h"
      23                 :            : 
      24                 :            : #include "spdk_internal/nvme_tcp.h"
      25                 :            : #include "spdk_internal/trace_defs.h"
      26                 :            : 
      27                 :            : #define NVME_TCP_RW_BUFFER_SIZE 131072
      28                 :            : 
      29                 :            : /* For async connect workloads, allow more time since we are more likely
      30                 :            :  * to be processing lots ICREQs at once.
      31                 :            :  */
      32                 :            : #define ICREQ_TIMEOUT_SYNC 2 /* in seconds */
      33                 :            : #define ICREQ_TIMEOUT_ASYNC 10 /* in seconds */
      34                 :            : 
      35                 :            : #define NVME_TCP_HPDA_DEFAULT                   0
      36                 :            : #define NVME_TCP_MAX_R2T_DEFAULT                1
      37                 :            : #define NVME_TCP_PDU_H2C_MIN_DATA_SIZE          4096
      38                 :            : 
      39                 :            : /*
      40                 :            :  * Maximum value of transport_ack_timeout used by TCP controller
      41                 :            :  */
      42                 :            : #define NVME_TCP_CTRLR_MAX_TRANSPORT_ACK_TIMEOUT        31
      43                 :            : 
      44                 :            : 
      45                 :            : /* NVMe TCP transport extensions for spdk_nvme_ctrlr */
      46                 :            : struct nvme_tcp_ctrlr {
      47                 :            :         struct spdk_nvme_ctrlr                  ctrlr;
      48                 :            :         char                                    psk_identity[NVMF_PSK_IDENTITY_LEN];
      49                 :            :         uint8_t                                 psk[SPDK_TLS_PSK_MAX_LEN];
      50                 :            :         int                                     psk_size;
      51                 :            :         char                                    *tls_cipher_suite;
      52                 :            : };
      53                 :            : 
      54                 :            : struct nvme_tcp_poll_group {
      55                 :            :         struct spdk_nvme_transport_poll_group group;
      56                 :            :         struct spdk_sock_group *sock_group;
      57                 :            :         uint32_t completions_per_qpair;
      58                 :            :         int64_t num_completions;
      59                 :            : 
      60                 :            :         TAILQ_HEAD(, nvme_tcp_qpair) needs_poll;
      61                 :            :         struct spdk_nvme_tcp_stat stats;
      62                 :            : };
      63                 :            : 
      64                 :            : /* NVMe TCP qpair extensions for spdk_nvme_qpair */
      65                 :            : struct nvme_tcp_qpair {
      66                 :            :         struct spdk_nvme_qpair                  qpair;
      67                 :            :         struct spdk_sock                        *sock;
      68                 :            : 
      69                 :            :         TAILQ_HEAD(, nvme_tcp_req)              free_reqs;
      70                 :            :         TAILQ_HEAD(, nvme_tcp_req)              outstanding_reqs;
      71                 :            : 
      72                 :            :         TAILQ_HEAD(, nvme_tcp_pdu)              send_queue;
      73                 :            :         struct nvme_tcp_pdu                     *recv_pdu;
      74                 :            :         struct nvme_tcp_pdu                     *send_pdu; /* only for error pdu and init pdu */
      75                 :            :         struct nvme_tcp_pdu                     *send_pdus; /* Used by tcp_reqs */
      76                 :            :         enum nvme_tcp_pdu_recv_state            recv_state;
      77                 :            :         struct nvme_tcp_req                     *tcp_reqs;
      78                 :            :         struct spdk_nvme_tcp_stat               *stats;
      79                 :            : 
      80                 :            :         uint16_t                                num_entries;
      81                 :            :         uint16_t                                async_complete;
      82                 :            : 
      83                 :            :         struct {
      84                 :            :                 uint16_t host_hdgst_enable: 1;
      85                 :            :                 uint16_t host_ddgst_enable: 1;
      86                 :            :                 uint16_t icreq_send_ack: 1;
      87                 :            :                 uint16_t in_connect_poll: 1;
      88                 :            :                 uint16_t reserved: 12;
      89                 :            :         } flags;
      90                 :            : 
      91                 :            :         /** Specifies the maximum number of PDU-Data bytes per H2C Data Transfer PDU */
      92                 :            :         uint32_t                                maxh2cdata;
      93                 :            : 
      94                 :            :         uint32_t                                maxr2t;
      95                 :            : 
      96                 :            :         /* 0 based value, which is used to guide the padding */
      97                 :            :         uint8_t                                 cpda;
      98                 :            : 
      99                 :            :         enum nvme_tcp_qpair_state               state;
     100                 :            : 
     101                 :            :         TAILQ_ENTRY(nvme_tcp_qpair)             link;
     102                 :            :         bool                                    needs_poll;
     103                 :            : 
     104                 :            :         uint64_t                                icreq_timeout_tsc;
     105                 :            : 
     106                 :            :         bool                                    shared_stats;
     107                 :            : };
     108                 :            : 
     109                 :            : enum nvme_tcp_req_state {
     110                 :            :         NVME_TCP_REQ_FREE,
     111                 :            :         NVME_TCP_REQ_ACTIVE,
     112                 :            :         NVME_TCP_REQ_ACTIVE_R2T,
     113                 :            : };
     114                 :            : 
     115                 :            : struct nvme_tcp_req {
     116                 :            :         struct nvme_request                     *req;
     117                 :            :         enum nvme_tcp_req_state                 state;
     118                 :            :         uint16_t                                cid;
     119                 :            :         uint16_t                                ttag;
     120                 :            :         uint32_t                                datao;
     121                 :            :         uint32_t                                expected_datao;
     122                 :            :         uint32_t                                r2tl_remain;
     123                 :            :         uint32_t                                active_r2ts;
     124                 :            :         /* Used to hold a value received from subsequent R2T while we are still
     125                 :            :          * waiting for H2C complete */
     126                 :            :         uint16_t                                ttag_r2t_next;
     127                 :            :         bool                                    in_capsule_data;
     128                 :            :         /* It is used to track whether the req can be safely freed */
     129                 :            :         union {
     130                 :            :                 uint8_t raw;
     131                 :            :                 struct {
     132                 :            :                         /* The last send operation completed - kernel released send buffer */
     133                 :            :                         uint8_t                         send_ack : 1;
     134                 :            :                         /* Data transfer completed - target send resp or last data bit */
     135                 :            :                         uint8_t                         data_recv : 1;
     136                 :            :                         /* tcp_req is waiting for completion of the previous send operation (buffer reclaim notification
     137                 :            :                          * from kernel) to send H2C */
     138                 :            :                         uint8_t                         h2c_send_waiting_ack : 1;
     139                 :            :                         /* tcp_req received subsequent r2t while it is still waiting for send_ack.
     140                 :            :                          * Rare case, actual when dealing with target that can send several R2T requests.
     141                 :            :                          * SPDK TCP target sends 1 R2T for the whole data buffer */
     142                 :            :                         uint8_t                         r2t_waiting_h2c_complete : 1;
     143                 :            :                         /* Accel operation is in progress */
     144                 :            :                         uint8_t                         in_progress_accel : 1;
     145                 :            :                         uint8_t                         domain_in_use: 1;
     146                 :            :                         uint8_t                         reserved : 2;
     147                 :            :                 } bits;
     148                 :            :         } ordering;
     149                 :            :         struct nvme_tcp_pdu                     *pdu;
     150                 :            :         struct iovec                            iov[NVME_TCP_MAX_SGL_DESCRIPTORS];
     151                 :            :         uint32_t                                iovcnt;
     152                 :            :         /* Used to hold a value received from subsequent R2T while we are still
     153                 :            :          * waiting for H2C ack */
     154                 :            :         uint32_t                                r2tl_remain_next;
     155                 :            :         struct nvme_tcp_qpair                   *tqpair;
     156                 :            :         TAILQ_ENTRY(nvme_tcp_req)               link;
     157                 :            :         struct spdk_nvme_cpl                    rsp;
     158                 :            :         uint8_t                                 rsvd1[32];
     159                 :            : };
     160                 :            : SPDK_STATIC_ASSERT(sizeof(struct nvme_tcp_req) % SPDK_CACHE_LINE_SIZE == 0, "unaligned size");
     161                 :            : 
     162                 :            : static struct spdk_nvme_tcp_stat g_dummy_stats = {};
     163                 :            : 
     164                 :            : static void nvme_tcp_send_h2c_data(struct nvme_tcp_req *tcp_req);
     165                 :            : static int64_t nvme_tcp_poll_group_process_completions(struct spdk_nvme_transport_poll_group
     166                 :            :                 *tgroup, uint32_t completions_per_qpair, spdk_nvme_disconnected_qpair_cb disconnected_qpair_cb);
     167                 :            : static void nvme_tcp_icresp_handle(struct nvme_tcp_qpair *tqpair, struct nvme_tcp_pdu *pdu);
     168                 :            : static void nvme_tcp_req_complete(struct nvme_tcp_req *tcp_req, struct nvme_tcp_qpair *tqpair,
     169                 :            :                                   struct spdk_nvme_cpl *rsp, bool print_on_error);
     170                 :            : 
     171                 :            : static inline struct nvme_tcp_qpair *
     172                 :  133097768 : nvme_tcp_qpair(struct spdk_nvme_qpair *qpair)
     173                 :            : {
     174         [ -  + ]:  133097768 :         assert(qpair->trtype == SPDK_NVME_TRANSPORT_TCP);
     175                 :  133097768 :         return SPDK_CONTAINEROF(qpair, struct nvme_tcp_qpair, qpair);
     176                 :            : }
     177                 :            : 
     178                 :            : static inline struct nvme_tcp_poll_group *
     179                 :  341137448 : nvme_tcp_poll_group(struct spdk_nvme_transport_poll_group *group)
     180                 :            : {
     181                 :  341137448 :         return SPDK_CONTAINEROF(group, struct nvme_tcp_poll_group, group);
     182                 :            : }
     183                 :            : 
     184                 :            : static inline struct nvme_tcp_ctrlr *
     185                 :       1295 : nvme_tcp_ctrlr(struct spdk_nvme_ctrlr *ctrlr)
     186                 :            : {
     187         [ -  + ]:       1295 :         assert(ctrlr->trid.trtype == SPDK_NVME_TRANSPORT_TCP);
     188                 :       1295 :         return SPDK_CONTAINEROF(ctrlr, struct nvme_tcp_ctrlr, ctrlr);
     189                 :            : }
     190                 :            : 
     191                 :            : static struct nvme_tcp_req *
     192                 :   17431709 : nvme_tcp_req_get(struct nvme_tcp_qpair *tqpair)
     193                 :            : {
     194                 :            :         struct nvme_tcp_req *tcp_req;
     195                 :            : 
     196                 :   17431709 :         tcp_req = TAILQ_FIRST(&tqpair->free_reqs);
     197         [ +  + ]:   17431709 :         if (!tcp_req) {
     198                 :     440975 :                 return NULL;
     199                 :            :         }
     200                 :            : 
     201         [ -  + ]:   16990734 :         assert(tcp_req->state == NVME_TCP_REQ_FREE);
     202                 :   16990734 :         tcp_req->state = NVME_TCP_REQ_ACTIVE;
     203         [ +  + ]:   16990734 :         TAILQ_REMOVE(&tqpair->free_reqs, tcp_req, link);
     204                 :   16990734 :         tcp_req->datao = 0;
     205                 :   16990734 :         tcp_req->expected_datao = 0;
     206                 :   16990734 :         tcp_req->req = NULL;
     207                 :   16990734 :         tcp_req->in_capsule_data = false;
     208                 :   16990734 :         tcp_req->r2tl_remain = 0;
     209                 :   16990734 :         tcp_req->r2tl_remain_next = 0;
     210                 :   16990734 :         tcp_req->active_r2ts = 0;
     211                 :   16990734 :         tcp_req->iovcnt = 0;
     212                 :   16990734 :         tcp_req->ordering.raw = 0;
     213         [ -  + ]:   16990734 :         memset(tcp_req->pdu, 0, sizeof(struct nvme_tcp_pdu));
     214         [ -  + ]:   16990734 :         memset(&tcp_req->rsp, 0, sizeof(struct spdk_nvme_cpl));
     215                 :            : 
     216                 :   16990734 :         return tcp_req;
     217                 :            : }
     218                 :            : 
     219                 :            : static void
     220                 :   16990770 : nvme_tcp_req_put(struct nvme_tcp_qpair *tqpair, struct nvme_tcp_req *tcp_req)
     221                 :            : {
     222         [ -  + ]:   16990770 :         assert(tcp_req->state != NVME_TCP_REQ_FREE);
     223                 :   16990770 :         tcp_req->state = NVME_TCP_REQ_FREE;
     224         [ +  + ]:   16990770 :         TAILQ_INSERT_HEAD(&tqpair->free_reqs, tcp_req, link);
     225                 :   16990770 : }
     226                 :            : 
     227                 :            : static inline void
     228                 :          0 : nvme_tcp_accel_submit_crc32c(struct nvme_tcp_poll_group *tgroup, struct nvme_tcp_req *treq,
     229                 :            :                              uint32_t *dst, struct iovec *iovs, uint32_t iovcnt, uint32_t seed,
     230                 :            :                              spdk_nvme_accel_completion_cb cb_fn, void *cb_arg)
     231                 :            : {
     232                 :          0 :         struct spdk_nvme_poll_group *pg = tgroup->group.group;
     233                 :            : 
     234                 :          0 :         treq->ordering.bits.in_progress_accel = 1;
     235                 :          0 :         pg->accel_fn_table.submit_accel_crc32c(pg->ctx, dst, iovs, iovcnt, seed, cb_fn, cb_arg);
     236                 :          0 : }
     237                 :            : 
     238                 :            : static inline void
     239                 :     806534 : nvme_tcp_accel_finish_sequence(struct nvme_tcp_poll_group *tgroup, struct nvme_tcp_req *treq,
     240                 :            :                                void *seq, spdk_nvme_accel_completion_cb cb_fn, void *cb_arg)
     241                 :            : {
     242                 :     806534 :         struct spdk_nvme_poll_group *pg = tgroup->group.group;
     243                 :            : 
     244                 :     806534 :         treq->ordering.bits.in_progress_accel = 1;
     245                 :     806534 :         pg->accel_fn_table.finish_sequence(seq, cb_fn, cb_arg);
     246                 :     806534 : }
     247                 :            : 
     248                 :            : static inline void
     249                 :     392842 : nvme_tcp_accel_reverse_sequence(struct nvme_tcp_poll_group *tgroup, void *seq)
     250                 :            : {
     251                 :     392842 :         struct spdk_nvme_poll_group *pg = tgroup->group.group;
     252                 :            : 
     253                 :     392842 :         pg->accel_fn_table.reverse_sequence(seq);
     254                 :     392842 : }
     255                 :            : 
     256                 :            : static inline int
     257                 :     806534 : nvme_tcp_accel_append_crc32c(struct nvme_tcp_poll_group *tgroup, void **seq, uint32_t *dst,
     258                 :            :                              struct iovec *iovs, uint32_t iovcnt, uint32_t seed,
     259                 :            :                              spdk_nvme_accel_step_cb cb_fn, void *cb_arg)
     260                 :            : {
     261                 :     806534 :         struct spdk_nvme_poll_group *pg = tgroup->group.group;
     262                 :            : 
     263                 :     806534 :         return pg->accel_fn_table.append_crc32c(pg->ctx, seq, dst, iovs, iovcnt, NULL, NULL,
     264                 :            :                                                 seed, cb_fn, cb_arg);
     265                 :            : }
     266                 :            : 
     267                 :            : static void
     268                 :       3656 : nvme_tcp_free_reqs(struct nvme_tcp_qpair *tqpair)
     269                 :            : {
     270                 :       3656 :         free(tqpair->tcp_reqs);
     271                 :       3656 :         tqpair->tcp_reqs = NULL;
     272                 :            : 
     273                 :       3656 :         spdk_free(tqpair->send_pdus);
     274                 :       3656 :         tqpair->send_pdus = NULL;
     275                 :       3656 : }
     276                 :            : 
     277                 :            : static int
     278                 :       3674 : nvme_tcp_alloc_reqs(struct nvme_tcp_qpair *tqpair)
     279                 :            : {
     280                 :            :         uint16_t i;
     281                 :            :         struct nvme_tcp_req *tcp_req;
     282                 :            : 
     283                 :       3674 :         tqpair->tcp_reqs = aligned_alloc(SPDK_CACHE_LINE_SIZE,
     284                 :       3674 :                                          tqpair->num_entries * sizeof(*tcp_req));
     285         [ -  + ]:       3674 :         if (tqpair->tcp_reqs == NULL) {
     286                 :          0 :                 SPDK_ERRLOG("Failed to allocate tcp_reqs on tqpair=%p\n", tqpair);
     287                 :          0 :                 goto fail;
     288                 :            :         }
     289                 :            : 
     290                 :            :         /* Add additional 2 member for the send_pdu, recv_pdu owned by the tqpair */
     291                 :       3674 :         tqpair->send_pdus = spdk_zmalloc((tqpair->num_entries + 2) * sizeof(struct nvme_tcp_pdu),
     292                 :            :                                          0x1000, NULL,
     293                 :            :                                          SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA);
     294                 :            : 
     295         [ -  + ]:       3674 :         if (tqpair->send_pdus == NULL) {
     296                 :          0 :                 SPDK_ERRLOG("Failed to allocate send_pdus on tqpair=%p\n", tqpair);
     297                 :          0 :                 goto fail;
     298                 :            :         }
     299                 :            : 
     300         [ -  + ]:       3674 :         memset(tqpair->tcp_reqs, 0, tqpair->num_entries * sizeof(*tcp_req));
     301                 :       3674 :         TAILQ_INIT(&tqpair->send_queue);
     302                 :       3674 :         TAILQ_INIT(&tqpair->free_reqs);
     303                 :       3674 :         TAILQ_INIT(&tqpair->outstanding_reqs);
     304                 :       3674 :         tqpair->qpair.queue_depth = 0;
     305         [ +  + ]:     766154 :         for (i = 0; i < tqpair->num_entries; i++) {
     306                 :     762480 :                 tcp_req = &tqpair->tcp_reqs[i];
     307                 :     762480 :                 tcp_req->cid = i;
     308                 :     762480 :                 tcp_req->tqpair = tqpair;
     309                 :     762480 :                 tcp_req->pdu = &tqpair->send_pdus[i];
     310                 :     762480 :                 TAILQ_INSERT_TAIL(&tqpair->free_reqs, tcp_req, link);
     311                 :            :         }
     312                 :            : 
     313                 :       3674 :         tqpair->send_pdu = &tqpair->send_pdus[i];
     314                 :       3674 :         tqpair->recv_pdu = &tqpair->send_pdus[i + 1];
     315                 :            : 
     316                 :       3674 :         return 0;
     317                 :          0 : fail:
     318                 :          0 :         nvme_tcp_free_reqs(tqpair);
     319                 :          0 :         return -ENOMEM;
     320                 :            : }
     321                 :            : 
     322                 :            : static inline void
     323                 :   84463990 : nvme_tcp_qpair_set_recv_state(struct nvme_tcp_qpair *tqpair,
     324                 :            :                               enum nvme_tcp_pdu_recv_state state)
     325                 :            : {
     326         [ +  + ]:   84463990 :         if (tqpair->recv_state == state) {
     327                 :        489 :                 SPDK_ERRLOG("The recv state of tqpair=%p is same with the state(%d) to be set\n",
     328                 :            :                             tqpair, state);
     329                 :        489 :                 return;
     330                 :            :         }
     331                 :            : 
     332         [ +  + ]:   84463501 :         if (state == NVME_TCP_PDU_RECV_STATE_ERROR) {
     333         [ -  + ]:         25 :                 assert(TAILQ_EMPTY(&tqpair->outstanding_reqs));
     334                 :            :         }
     335                 :            : 
     336                 :   84463501 :         tqpair->recv_state = state;
     337                 :            : }
     338                 :            : 
     339                 :            : static void nvme_tcp_qpair_abort_reqs(struct spdk_nvme_qpair *qpair, uint32_t dnr);
     340                 :            : 
     341                 :            : static void
     342                 :      14091 : nvme_tcp_ctrlr_disconnect_qpair(struct spdk_nvme_ctrlr *ctrlr, struct spdk_nvme_qpair *qpair)
     343                 :            : {
     344                 :      14091 :         struct nvme_tcp_qpair *tqpair = nvme_tcp_qpair(qpair);
     345                 :            :         struct nvme_tcp_pdu *pdu;
     346                 :            :         int rc;
     347                 :            :         struct nvme_tcp_poll_group *group;
     348                 :            : 
     349   [ +  +  +  + ]:      14091 :         if (tqpair->needs_poll) {
     350                 :         16 :                 group = nvme_tcp_poll_group(qpair->poll_group);
     351         [ -  + ]:         16 :                 TAILQ_REMOVE(&group->needs_poll, tqpair, link);
     352                 :         16 :                 tqpair->needs_poll = false;
     353                 :            :         }
     354                 :            : 
     355                 :      14091 :         rc = spdk_sock_close(&tqpair->sock);
     356                 :            : 
     357         [ +  + ]:      14091 :         if (tqpair->sock != NULL) {
     358                 :          6 :                 SPDK_ERRLOG("tqpair=%p, errno=%d, rc=%d\n", tqpair, errno, rc);
     359                 :            :                 /* Set it to NULL manually */
     360                 :          6 :                 tqpair->sock = NULL;
     361                 :            :         }
     362                 :            : 
     363                 :            :         /* clear the send_queue */
     364         [ +  + ]:      14146 :         while (!TAILQ_EMPTY(&tqpair->send_queue)) {
     365                 :         55 :                 pdu = TAILQ_FIRST(&tqpair->send_queue);
     366                 :            :                 /* Remove the pdu from the send_queue to prevent the wrong sending out
     367                 :            :                  * in the next round connection
     368                 :            :                  */
     369         [ +  + ]:         55 :                 TAILQ_REMOVE(&tqpair->send_queue, pdu, tailq);
     370                 :            :         }
     371                 :            : 
     372                 :      14091 :         nvme_tcp_qpair_abort_reqs(qpair, 0);
     373                 :            : 
     374                 :            :         /* If the qpair is marked as asynchronous, let it go through the process_completions() to
     375                 :            :          * let any outstanding requests (e.g. those with outstanding accel operations) complete.
     376                 :            :          * Otherwise, there's no way of waiting for them, so tqpair->outstanding_reqs has to be
     377                 :            :          * empty.
     378                 :            :          */
     379         [ +  + ]:      14091 :         if (qpair->async) {
     380                 :       2524 :                 nvme_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_QUIESCING);
     381                 :            :         } else {
     382         [ -  + ]:      11567 :                 assert(TAILQ_EMPTY(&tqpair->outstanding_reqs));
     383                 :      11567 :                 nvme_transport_ctrlr_disconnect_qpair_done(qpair);
     384                 :            :         }
     385                 :      14091 : }
     386                 :            : 
     387                 :            : static int
     388                 :       3644 : nvme_tcp_ctrlr_delete_io_qpair(struct spdk_nvme_ctrlr *ctrlr, struct spdk_nvme_qpair *qpair)
     389                 :            : {
     390                 :       3644 :         struct nvme_tcp_qpair *tqpair = nvme_tcp_qpair(qpair);
     391                 :            : 
     392         [ -  + ]:       3644 :         assert(qpair != NULL);
     393                 :       3644 :         nvme_tcp_qpair_abort_reqs(qpair, 0);
     394         [ -  + ]:       3644 :         assert(TAILQ_EMPTY(&tqpair->outstanding_reqs));
     395                 :            : 
     396                 :       3644 :         nvme_qpair_deinit(qpair);
     397                 :       3644 :         nvme_tcp_free_reqs(tqpair);
     398   [ +  +  +  + ]:       3644 :         if (!tqpair->shared_stats) {
     399                 :       2767 :                 free(tqpair->stats);
     400                 :            :         }
     401                 :       3644 :         free(tqpair);
     402                 :            : 
     403                 :       3644 :         return 0;
     404                 :            : }
     405                 :            : 
     406                 :            : static int
     407                 :       1240 : nvme_tcp_ctrlr_enable(struct spdk_nvme_ctrlr *ctrlr)
     408                 :            : {
     409                 :       1240 :         return 0;
     410                 :            : }
     411                 :            : 
     412                 :            : static int
     413                 :       1283 : nvme_tcp_ctrlr_destruct(struct spdk_nvme_ctrlr *ctrlr)
     414                 :            : {
     415                 :       1283 :         struct nvme_tcp_ctrlr *tctrlr = nvme_tcp_ctrlr(ctrlr);
     416                 :            : 
     417         [ +  + ]:       1283 :         if (ctrlr->adminq) {
     418                 :       1254 :                 nvme_tcp_ctrlr_delete_io_qpair(ctrlr, ctrlr->adminq);
     419                 :            :         }
     420                 :            : 
     421                 :       1283 :         nvme_ctrlr_destruct_finish(ctrlr);
     422                 :            : 
     423                 :       1283 :         free(tctrlr);
     424                 :            : 
     425                 :       1283 :         return 0;
     426                 :            : }
     427                 :            : 
     428                 :            : /* If there are queued requests, we assume they are queued because they are waiting
     429                 :            :  * for resources to be released. Those resources are almost certainly released in
     430                 :            :  * response to a PDU completing. However, to attempt to make forward progress
     431                 :            :  * the qpair needs to be polled and we can't rely on another network event to make
     432                 :            :  * that happen. Add it to a list of qpairs to poll regardless of network activity.
     433                 :            :  *
     434                 :            :  * Besides, when tqpair state is NVME_TCP_QPAIR_STATE_FABRIC_CONNECT_POLL or
     435                 :            :  * NVME_TCP_QPAIR_STATE_INITIALIZING, need to add it to needs_poll list too to make
     436                 :            :  * forward progress in case that the resources are released after icreq's or CONNECT's
     437                 :            :  * resp is processed. */
     438                 :            : static void
     439                 :   18073290 : nvme_tcp_cond_schedule_qpair_polling(struct nvme_tcp_qpair *tqpair)
     440                 :            : {
     441                 :            :         struct nvme_tcp_poll_group *pgroup;
     442                 :            : 
     443   [ -  +  +  +  :   18073290 :         if (tqpair->needs_poll || !tqpair->qpair.poll_group) {
                   +  + ]
     444                 :   12645351 :                 return;
     445                 :            :         }
     446                 :            : 
     447         [ +  + ]:    5427939 :         if (STAILQ_EMPTY(&tqpair->qpair.queued_req) &&
     448   [ +  +  +  - ]:    4163708 :             spdk_likely(tqpair->state != NVME_TCP_QPAIR_STATE_FABRIC_CONNECT_POLL &&
     449                 :            :                         tqpair->state != NVME_TCP_QPAIR_STATE_INITIALIZING)) {
     450                 :    4163540 :                 return;
     451                 :            :         }
     452                 :            : 
     453                 :    1264399 :         pgroup = nvme_tcp_poll_group(tqpair->qpair.poll_group);
     454                 :    1264399 :         TAILQ_INSERT_TAIL(&pgroup->needs_poll, tqpair, link);
     455                 :    1264399 :         tqpair->needs_poll = true;
     456                 :            : }
     457                 :            : 
     458                 :            : static void
     459                 :   17680448 : pdu_write_done(void *cb_arg, int err)
     460                 :            : {
     461                 :   17680448 :         struct nvme_tcp_pdu *pdu = cb_arg;
     462                 :   17680448 :         struct nvme_tcp_qpair *tqpair = pdu->qpair;
     463                 :            : 
     464                 :   17680448 :         nvme_tcp_cond_schedule_qpair_polling(tqpair);
     465         [ +  + ]:   17680448 :         TAILQ_REMOVE(&tqpair->send_queue, pdu, tailq);
     466                 :            : 
     467         [ +  + ]:   17680448 :         if (err != 0) {
     468                 :        550 :                 nvme_transport_ctrlr_disconnect_qpair(tqpair->qpair.ctrlr, &tqpair->qpair);
     469                 :        550 :                 return;
     470                 :            :         }
     471                 :            : 
     472         [ -  + ]:   17679898 :         assert(pdu->cb_fn != NULL);
     473                 :   17679898 :         pdu->cb_fn(pdu->cb_arg);
     474                 :            : }
     475                 :            : 
     476                 :            : static void
     477                 :          0 : pdu_write_fail(struct nvme_tcp_pdu *pdu, int status)
     478                 :            : {
     479                 :          0 :         struct nvme_tcp_qpair *tqpair = pdu->qpair;
     480                 :            : 
     481                 :            :         /* This function is similar to pdu_write_done(), but it should be called before a PDU is
     482                 :            :          * sent over the socket */
     483                 :          0 :         TAILQ_INSERT_TAIL(&tqpair->send_queue, pdu, tailq);
     484                 :          0 :         pdu_write_done(pdu, status);
     485                 :          0 : }
     486                 :            : 
     487                 :            : static void
     488                 :          0 : pdu_seq_fail(struct nvme_tcp_pdu *pdu, int status)
     489                 :            : {
     490                 :          0 :         struct nvme_tcp_req *treq = pdu->req;
     491                 :            : 
     492                 :          0 :         SPDK_ERRLOG("Failed to execute accel sequence: %d\n", status);
     493                 :          0 :         nvme_tcp_cond_schedule_qpair_polling(pdu->qpair);
     494                 :          0 :         treq->rsp.status.sc = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR;
     495                 :          0 :         nvme_tcp_req_complete(treq, treq->tqpair, &treq->rsp, true);
     496                 :          0 : }
     497                 :            : 
     498                 :            : static void
     499                 :   17680586 : _tcp_write_pdu(struct nvme_tcp_pdu *pdu)
     500                 :            : {
     501                 :   17680586 :         uint32_t mapped_length = 0;
     502                 :   17680586 :         struct nvme_tcp_qpair *tqpair = pdu->qpair;
     503                 :            : 
     504                 :   17680632 :         pdu->sock_req.iovcnt = nvme_tcp_build_iovs(pdu->iov, SPDK_COUNTOF(pdu->iov), pdu,
     505                 :   17680586 :                                (bool)tqpair->flags.host_hdgst_enable, (bool)tqpair->flags.host_ddgst_enable,
     506                 :            :                                &mapped_length);
     507                 :   17680586 :         TAILQ_INSERT_TAIL(&tqpair->send_queue, pdu, tailq);
     508         [ -  + ]:   17680586 :         if (spdk_unlikely(mapped_length < pdu->data_len)) {
     509                 :          0 :                 SPDK_ERRLOG("could not map the whole %u bytes (mapped only %u bytes)\n", pdu->data_len,
     510                 :            :                             mapped_length);
     511                 :          0 :                 pdu_write_done(pdu, -EINVAL);
     512                 :          0 :                 return;
     513                 :            :         }
     514                 :   17680586 :         pdu->sock_req.cb_fn = pdu_write_done;
     515                 :   17680586 :         pdu->sock_req.cb_arg = pdu;
     516                 :   17680586 :         tqpair->stats->submitted_requests++;
     517                 :   17680586 :         spdk_sock_writev_async(tqpair->sock, &pdu->sock_req);
     518                 :            : }
     519                 :            : 
     520                 :            : static void
     521                 :     413692 : tcp_write_pdu_seq_cb(void *ctx, int status)
     522                 :            : {
     523                 :     413692 :         struct nvme_tcp_pdu *pdu = ctx;
     524                 :     413692 :         struct nvme_tcp_req *treq = pdu->req;
     525                 :     413692 :         struct nvme_request *req = treq->req;
     526                 :            : 
     527         [ -  + ]:     413692 :         assert(treq->ordering.bits.in_progress_accel);
     528                 :     413692 :         treq->ordering.bits.in_progress_accel = 0;
     529                 :            : 
     530                 :     413692 :         req->accel_sequence = NULL;
     531         [ -  + ]:     413692 :         if (spdk_unlikely(status != 0)) {
     532                 :          0 :                 pdu_seq_fail(pdu, status);
     533                 :          0 :                 return;
     534                 :            :         }
     535                 :            : 
     536                 :     413692 :         _tcp_write_pdu(pdu);
     537                 :            : }
     538                 :            : 
     539                 :            : static void
     540                 :   17680586 : tcp_write_pdu(struct nvme_tcp_pdu *pdu)
     541                 :            : {
     542                 :   17680586 :         struct nvme_tcp_req *treq = pdu->req;
     543                 :   17680586 :         struct nvme_tcp_qpair *tqpair = pdu->qpair;
     544                 :            :         struct nvme_tcp_poll_group *tgroup;
     545                 :            :         struct nvme_request *req;
     546                 :            : 
     547         [ +  + ]:   17680586 :         if (spdk_likely(treq != NULL)) {
     548                 :   17676244 :                 req = treq->req;
     549   [ +  +  +  + ]:   18175279 :                 if (req->accel_sequence != NULL &&
     550                 :     499035 :                     spdk_nvme_opc_get_data_transfer(req->cmd.opc) == SPDK_NVME_DATA_HOST_TO_CONTROLLER &&
     551         [ +  + ]:     425652 :                     pdu->data_len > 0) {
     552         [ -  + ]:     413692 :                         assert(tqpair->qpair.poll_group != NULL);
     553                 :     413692 :                         tgroup = nvme_tcp_poll_group(tqpair->qpair.poll_group);
     554                 :     413692 :                         nvme_tcp_accel_finish_sequence(tgroup, treq, req->accel_sequence,
     555                 :            :                                                        tcp_write_pdu_seq_cb, pdu);
     556                 :     413692 :                         return;
     557                 :            :                 }
     558                 :            :         }
     559                 :            : 
     560                 :   17266894 :         _tcp_write_pdu(pdu);
     561                 :            : }
     562                 :            : 
     563                 :            : static void
     564                 :          0 : pdu_accel_compute_crc32_done(void *cb_arg, int status)
     565                 :            : {
     566                 :          0 :         struct nvme_tcp_pdu *pdu = cb_arg;
     567                 :          0 :         struct nvme_tcp_req *req = pdu->req;
     568                 :            : 
     569         [ #  # ]:          0 :         assert(req->ordering.bits.in_progress_accel);
     570                 :          0 :         req->ordering.bits.in_progress_accel = 0;
     571                 :            : 
     572         [ #  # ]:          0 :         if (spdk_unlikely(status)) {
     573                 :          0 :                 SPDK_ERRLOG("Failed to compute the data digest for pdu =%p\n", pdu);
     574                 :          0 :                 pdu_write_fail(pdu, status);
     575                 :          0 :                 return;
     576                 :            :         }
     577                 :            : 
     578                 :          0 :         pdu->data_digest_crc32 ^= SPDK_CRC32C_XOR;
     579                 :          0 :         MAKE_DIGEST_WORD(pdu->data_digest, pdu->data_digest_crc32);
     580                 :            : 
     581                 :          0 :         _tcp_write_pdu(pdu);
     582                 :            : }
     583                 :            : 
     584                 :            : static void
     585                 :          0 : pdu_accel_compute_crc32_seq_cb(void *cb_arg, int status)
     586                 :            : {
     587                 :          0 :         struct nvme_tcp_pdu *pdu = cb_arg;
     588                 :          0 :         struct nvme_tcp_qpair *tqpair = pdu->qpair;
     589                 :          0 :         struct nvme_tcp_poll_group *tgroup = nvme_tcp_poll_group(tqpair->qpair.poll_group);
     590                 :          0 :         struct nvme_tcp_req *treq = pdu->req;
     591                 :          0 :         struct nvme_request *req = treq->req;
     592                 :            : 
     593         [ #  # ]:          0 :         assert(treq->ordering.bits.in_progress_accel);
     594                 :          0 :         treq->ordering.bits.in_progress_accel = 0;
     595                 :            : 
     596                 :          0 :         req->accel_sequence = NULL;
     597         [ #  # ]:          0 :         if (spdk_unlikely(status != 0)) {
     598                 :          0 :                 pdu_seq_fail(pdu, status);
     599                 :          0 :                 return;
     600                 :            :         }
     601                 :            : 
     602                 :          0 :         nvme_tcp_accel_submit_crc32c(tgroup, pdu->req, &pdu->data_digest_crc32,
     603                 :          0 :                                      pdu->data_iov, pdu->data_iovcnt, 0,
     604                 :            :                                      pdu_accel_compute_crc32_done, pdu);
     605                 :            : }
     606                 :            : 
     607                 :            : static void
     608                 :     413692 : pdu_accel_seq_compute_crc32_done(void *cb_arg)
     609                 :            : {
     610                 :     413692 :         struct nvme_tcp_pdu *pdu = cb_arg;
     611                 :            : 
     612                 :     413692 :         pdu->data_digest_crc32 ^= SPDK_CRC32C_XOR;
     613                 :     413692 :         MAKE_DIGEST_WORD(pdu->data_digest, pdu->data_digest_crc32);
     614                 :     413692 : }
     615                 :            : 
     616                 :            : static bool
     617                 :     433860 : pdu_accel_compute_crc32(struct nvme_tcp_pdu *pdu)
     618                 :            : {
     619                 :     433860 :         struct nvme_tcp_qpair *tqpair = pdu->qpair;
     620                 :     433860 :         struct nvme_tcp_poll_group *tgroup = nvme_tcp_poll_group(tqpair->qpair.poll_group);
     621                 :     433860 :         struct nvme_request *req = ((struct nvme_tcp_req *)pdu->req)->req;
     622                 :            :         int rc;
     623                 :            : 
     624                 :            :         /* Only support this limited case for the first step */
     625   [ +  +  -  +  :     433860 :         if (spdk_unlikely(nvme_qpair_get_state(&tqpair->qpair) < NVME_QPAIR_CONNECTED ||
             +  +  -  + ]
     626                 :            :                           pdu->dif_ctx != NULL ||
     627                 :            :                           pdu->data_len % SPDK_NVME_TCP_DIGEST_ALIGNMENT != 0)) {
     628                 :        118 :                 return false;
     629                 :            :         }
     630                 :            : 
     631         [ -  + ]:     433742 :         if (tqpair->qpair.poll_group == NULL) {
     632                 :          0 :                 return false;
     633                 :            :         }
     634                 :            : 
     635         [ +  + ]:     433742 :         if (tgroup->group.group->accel_fn_table.append_crc32c != NULL) {
     636                 :     827384 :                 rc = nvme_tcp_accel_append_crc32c(tgroup, &req->accel_sequence,
     637                 :            :                                                   &pdu->data_digest_crc32,
     638                 :     413692 :                                                   pdu->data_iov, pdu->data_iovcnt, 0,
     639                 :            :                                                   pdu_accel_seq_compute_crc32_done, pdu);
     640         [ -  + ]:     413692 :                 if (spdk_unlikely(rc != 0)) {
     641                 :            :                         /* If accel is out of resources, fall back to non-accelerated crc32 */
     642         [ #  # ]:          0 :                         if (rc == -ENOMEM) {
     643                 :          0 :                                 return false;
     644                 :            :                         }
     645                 :            : 
     646                 :          0 :                         SPDK_ERRLOG("Failed to append crc32c operation: %d\n", rc);
     647                 :          0 :                         pdu_write_fail(pdu, rc);
     648                 :          0 :                         return true;
     649                 :            :                 }
     650                 :            : 
     651                 :     413692 :                 tcp_write_pdu(pdu);
     652                 :     413692 :                 return true;
     653         [ -  + ]:      20050 :         } else if (tgroup->group.group->accel_fn_table.submit_accel_crc32c != NULL) {
     654         [ #  # ]:          0 :                 if (req->accel_sequence != NULL) {
     655                 :          0 :                         nvme_tcp_accel_finish_sequence(tgroup, pdu->req, req->accel_sequence,
     656                 :            :                                                        pdu_accel_compute_crc32_seq_cb, pdu);
     657                 :            :                 } else {
     658                 :          0 :                         nvme_tcp_accel_submit_crc32c(tgroup, pdu->req, &pdu->data_digest_crc32,
     659                 :          0 :                                                      pdu->data_iov, pdu->data_iovcnt, 0,
     660                 :            :                                                      pdu_accel_compute_crc32_done, pdu);
     661                 :            :                 }
     662                 :            : 
     663                 :          0 :                 return true;
     664                 :            :         }
     665                 :            : 
     666                 :      20050 :         return false;
     667                 :            : }
     668                 :            : 
     669                 :            : static void
     670                 :          0 : pdu_compute_crc32_seq_cb(void *cb_arg, int status)
     671                 :            : {
     672                 :          0 :         struct nvme_tcp_pdu *pdu = cb_arg;
     673                 :          0 :         struct nvme_tcp_req *treq = pdu->req;
     674                 :          0 :         struct nvme_request *req = treq->req;
     675                 :            :         uint32_t crc32c;
     676                 :            : 
     677         [ #  # ]:          0 :         assert(treq->ordering.bits.in_progress_accel);
     678                 :          0 :         treq->ordering.bits.in_progress_accel = 0;
     679                 :            : 
     680                 :          0 :         req->accel_sequence = NULL;
     681         [ #  # ]:          0 :         if (spdk_unlikely(status != 0)) {
     682                 :          0 :                 pdu_seq_fail(pdu, status);
     683                 :          0 :                 return;
     684                 :            :         }
     685                 :            : 
     686                 :          0 :         crc32c = nvme_tcp_pdu_calc_data_digest(pdu);
     687                 :          0 :         crc32c = crc32c ^ SPDK_CRC32C_XOR;
     688                 :          0 :         MAKE_DIGEST_WORD(pdu->data_digest, crc32c);
     689                 :            : 
     690                 :          0 :         _tcp_write_pdu(pdu);
     691                 :            : }
     692                 :            : 
     693                 :            : static void
     694                 :   17680586 : pdu_compute_crc32(struct nvme_tcp_pdu *pdu)
     695                 :            : {
     696                 :   17680586 :         struct nvme_tcp_qpair *tqpair = pdu->qpair;
     697                 :            :         struct nvme_tcp_poll_group *tgroup;
     698                 :            :         struct nvme_request *req;
     699                 :            :         uint32_t crc32c;
     700                 :            : 
     701                 :            :         /* Data Digest */
     702   [ +  +  +  +  :   17680586 :         if (pdu->data_len > 0 && g_nvme_tcp_ddgst[pdu->hdr.common.pdu_type] &&
             +  +  +  + ]
     703                 :            :             tqpair->flags.host_ddgst_enable) {
     704         [ +  + ]:     433860 :                 if (pdu_accel_compute_crc32(pdu)) {
     705                 :     413692 :                         return;
     706                 :            :                 }
     707                 :            : 
     708                 :      20168 :                 req = ((struct nvme_tcp_req *)pdu->req)->req;
     709         [ -  + ]:      20168 :                 if (req->accel_sequence != NULL) {
     710                 :          0 :                         tgroup = nvme_tcp_poll_group(tqpair->qpair.poll_group);
     711                 :          0 :                         nvme_tcp_accel_finish_sequence(tgroup, pdu->req, req->accel_sequence,
     712                 :            :                                                        pdu_compute_crc32_seq_cb, pdu);
     713                 :          0 :                         return;
     714                 :            :                 }
     715                 :            : 
     716                 :      20168 :                 crc32c = nvme_tcp_pdu_calc_data_digest(pdu);
     717                 :      20168 :                 crc32c = crc32c ^ SPDK_CRC32C_XOR;
     718                 :      20168 :                 MAKE_DIGEST_WORD(pdu->data_digest, crc32c);
     719                 :            :         }
     720                 :            : 
     721                 :   17266894 :         tcp_write_pdu(pdu);
     722                 :            : }
     723                 :            : 
     724                 :            : static int
     725                 :   17680586 : nvme_tcp_qpair_write_pdu(struct nvme_tcp_qpair *tqpair,
     726                 :            :                          struct nvme_tcp_pdu *pdu,
     727                 :            :                          nvme_tcp_qpair_xfer_complete_cb cb_fn,
     728                 :            :                          void *cb_arg)
     729                 :            : {
     730                 :            :         int hlen;
     731                 :            :         uint32_t crc32c;
     732                 :            : 
     733                 :   17680586 :         hlen = pdu->hdr.common.hlen;
     734                 :   17680586 :         pdu->cb_fn = cb_fn;
     735                 :   17680586 :         pdu->cb_arg = cb_arg;
     736                 :   17680586 :         pdu->qpair = tqpair;
     737                 :            : 
     738                 :            :         /* Header Digest */
     739   [ +  +  +  +  :   17680586 :         if (g_nvme_tcp_hdgst[pdu->hdr.common.pdu_type] && tqpair->flags.host_hdgst_enable) {
                   +  + ]
     740                 :      62287 :                 crc32c = nvme_tcp_pdu_calc_header_digest(pdu);
     741                 :      62287 :                 MAKE_DIGEST_WORD((uint8_t *)&pdu->hdr.raw[hlen], crc32c);
     742                 :            :         }
     743                 :            : 
     744                 :   17680586 :         pdu_compute_crc32(pdu);
     745                 :            : 
     746                 :   17680586 :         return 0;
     747                 :            : }
     748                 :            : 
     749                 :            : static int
     750                 :   17983984 : nvme_tcp_try_memory_translation(struct nvme_tcp_req *tcp_req, void **addr, uint32_t length)
     751                 :            : {
     752                 :   17983984 :         struct nvme_request *req = tcp_req->req;
     753                 :   17983984 :         struct spdk_memory_domain_translation_result translation = {
     754                 :            :                 .iov_count = 0,
     755                 :            :                 .size = sizeof(translation)
     756                 :            :         };
     757                 :            :         int rc;
     758                 :            : 
     759         [ +  + ]:   17983984 :         if (!tcp_req->ordering.bits.domain_in_use) {
     760                 :   17910601 :                 return 0;
     761                 :            :         }
     762                 :            : 
     763                 :     146766 :         rc = spdk_memory_domain_translate_data(req->payload.opts->memory_domain,
     764                 :      73383 :                                                req->payload.opts->memory_domain_ctx, spdk_memory_domain_get_system_domain(), NULL, *addr, length,
     765                 :            :                                                &translation);
     766   [ +  -  -  + ]:      73383 :         if (spdk_unlikely(rc || translation.iov_count != 1)) {
     767                 :          0 :                 SPDK_ERRLOG("DMA memory translation failed, rc %d, iov_count %u\n", rc, translation.iov_count);
     768                 :          0 :                 return -EFAULT;
     769                 :            :         }
     770                 :            : 
     771         [ -  + ]:      73383 :         assert(length == translation.iov.iov_len);
     772                 :      73383 :         *addr = translation.iov.iov_base;
     773                 :      73383 :         return 0;
     774                 :            : }
     775                 :            : 
     776                 :            : /*
     777                 :            :  * Build SGL describing contiguous payload buffer.
     778                 :            :  */
     779                 :            : static int
     780                 :   16529959 : nvme_tcp_build_contig_request(struct nvme_tcp_qpair *tqpair, struct nvme_tcp_req *tcp_req)
     781                 :            : {
     782                 :   16529959 :         struct nvme_request *req = tcp_req->req;
     783                 :            : 
     784                 :            :         /* ubsan complains about applying zero offset to null pointer if contig_or_cb_arg is NULL,
     785                 :            :          * so just double cast it to make it go away */
     786                 :   16529959 :         void *addr = (void *)((uintptr_t)req->payload.contig_or_cb_arg + req->payload_offset);
     787                 :   16529959 :         size_t length = req->payload_size;
     788                 :            :         int rc;
     789                 :            : 
     790   [ -  +  +  + ]:   16529959 :         SPDK_DEBUGLOG(nvme, "enter\n");
     791                 :            : 
     792         [ -  + ]:   16529959 :         assert(nvme_payload_type(&req->payload) == NVME_PAYLOAD_TYPE_CONTIG);
     793                 :   16529959 :         rc = nvme_tcp_try_memory_translation(tcp_req, &addr, length);
     794         [ -  + ]:   16529959 :         if (spdk_unlikely(rc)) {
     795                 :          0 :                 return rc;
     796                 :            :         }
     797                 :            : 
     798                 :   16529959 :         tcp_req->iov[0].iov_base = addr;
     799                 :   16529959 :         tcp_req->iov[0].iov_len = length;
     800                 :   16529959 :         tcp_req->iovcnt = 1;
     801                 :   16529959 :         return 0;
     802                 :            : }
     803                 :            : 
     804                 :            : /*
     805                 :            :  * Build SGL describing scattered payload buffer.
     806                 :            :  */
     807                 :            : static int
     808                 :     349064 : nvme_tcp_build_sgl_request(struct nvme_tcp_qpair *tqpair, struct nvme_tcp_req *tcp_req)
     809                 :            : {
     810                 :            :         int rc;
     811                 :     349064 :         uint32_t length, remaining_size, iovcnt = 0, max_num_sgl;
     812                 :     349064 :         struct nvme_request *req = tcp_req->req;
     813                 :            : 
     814   [ -  +  -  + ]:     349064 :         SPDK_DEBUGLOG(nvme, "enter\n");
     815                 :            : 
     816         [ -  + ]:     349064 :         assert(req->payload_size != 0);
     817         [ -  + ]:     349064 :         assert(nvme_payload_type(&req->payload) == NVME_PAYLOAD_TYPE_SGL);
     818         [ -  + ]:     349064 :         assert(req->payload.reset_sgl_fn != NULL);
     819         [ -  + ]:     349064 :         assert(req->payload.next_sge_fn != NULL);
     820                 :     349064 :         req->payload.reset_sgl_fn(req->payload.contig_or_cb_arg, req->payload_offset);
     821                 :            : 
     822                 :     349064 :         max_num_sgl = spdk_min(req->qpair->ctrlr->max_sges, NVME_TCP_MAX_SGL_DESCRIPTORS);
     823                 :     349064 :         remaining_size = req->payload_size;
     824                 :            : 
     825                 :            :         do {
     826                 :     919264 :                 void *addr;
     827                 :            : 
     828                 :    1454025 :                 rc = req->payload.next_sge_fn(req->payload.contig_or_cb_arg, &addr, &length);
     829         [ -  + ]:    1454025 :                 if (rc) {
     830                 :          0 :                         return -1;
     831                 :            :                 }
     832                 :            : 
     833                 :    1454025 :                 rc = nvme_tcp_try_memory_translation(tcp_req, &addr, length);
     834         [ -  + ]:    1454025 :                 if (spdk_unlikely(rc)) {
     835                 :          0 :                         return rc;
     836                 :            :                 }
     837                 :            : 
     838                 :    1454025 :                 length = spdk_min(length, remaining_size);
     839                 :    1454025 :                 tcp_req->iov[iovcnt].iov_base = addr;
     840                 :    1454025 :                 tcp_req->iov[iovcnt].iov_len = length;
     841                 :    1454025 :                 remaining_size -= length;
     842                 :    1454025 :                 iovcnt++;
     843   [ +  +  +  + ]:    1454025 :         } while (remaining_size > 0 && iovcnt < max_num_sgl);
     844                 :            : 
     845                 :            : 
     846                 :            :         /* Should be impossible if we did our sgl checks properly up the stack, but do a sanity check here. */
     847         [ +  + ]:     349064 :         if (remaining_size > 0) {
     848                 :         12 :                 SPDK_ERRLOG("Failed to construct tcp_req=%p, and the iovcnt=%u, remaining_size=%u\n",
     849                 :            :                             tcp_req, iovcnt, remaining_size);
     850                 :         12 :                 return -1;
     851                 :            :         }
     852                 :            : 
     853                 :     349052 :         tcp_req->iovcnt = iovcnt;
     854                 :            : 
     855                 :     349052 :         return 0;
     856                 :            : }
     857                 :            : 
     858                 :            : static int
     859                 :   16990740 : nvme_tcp_req_init(struct nvme_tcp_qpair *tqpair, struct nvme_request *req,
     860                 :            :                   struct nvme_tcp_req *tcp_req)
     861                 :            : {
     862                 :   16990740 :         struct spdk_nvme_ctrlr *ctrlr = tqpair->qpair.ctrlr;
     863                 :   16990740 :         int rc = 0;
     864                 :            :         enum spdk_nvme_data_transfer xfer;
     865                 :            :         uint32_t max_in_capsule_data_size;
     866                 :            : 
     867                 :   16990740 :         tcp_req->req = req;
     868   [ +  +  +  + ]:   16990740 :         tcp_req->ordering.bits.domain_in_use = (req->payload.opts && req->payload.opts->memory_domain);
     869                 :            : 
     870                 :   16990740 :         req->cmd.cid = tcp_req->cid;
     871                 :   16990740 :         req->cmd.psdt = SPDK_NVME_PSDT_SGL_MPTR_CONTIG;
     872                 :   16990740 :         req->cmd.dptr.sgl1.unkeyed.type = SPDK_NVME_SGL_TYPE_TRANSPORT_DATA_BLOCK;
     873                 :   16990740 :         req->cmd.dptr.sgl1.unkeyed.subtype = SPDK_NVME_SGL_SUBTYPE_TRANSPORT;
     874                 :   16990740 :         req->cmd.dptr.sgl1.unkeyed.length = req->payload_size;
     875                 :            : 
     876         [ +  + ]:   16990740 :         if (spdk_unlikely(req->cmd.opc == SPDK_NVME_OPC_FABRIC)) {
     877                 :      68167 :                 struct spdk_nvmf_capsule_cmd *nvmf_cmd = (struct spdk_nvmf_capsule_cmd *)&req->cmd;
     878                 :            : 
     879                 :      68167 :                 xfer = spdk_nvme_opc_get_data_transfer(nvmf_cmd->fctype);
     880                 :            :         } else {
     881                 :   16922573 :                 xfer = spdk_nvme_opc_get_data_transfer(req->cmd.opc);
     882                 :            :         }
     883                 :            : 
     884                 :            :         /* For c2h delay filling in the iov until the data arrives.
     885                 :            :          * For h2c some delay is also possible if data doesn't fit into cmd capsule (not implemented). */
     886         [ +  + ]:   16990740 :         if (nvme_payload_type(&req->payload) == NVME_PAYLOAD_TYPE_CONTIG) {
     887         [ +  + ]:   16641694 :                 if (xfer != SPDK_NVME_DATA_CONTROLLER_TO_HOST) {
     888                 :    8501423 :                         rc = nvme_tcp_build_contig_request(tqpair, tcp_req);
     889                 :            :                 }
     890         [ +  - ]:     349046 :         } else if (nvme_payload_type(&req->payload) == NVME_PAYLOAD_TYPE_SGL) {
     891         [ +  + ]:     349046 :                 if (xfer != SPDK_NVME_DATA_CONTROLLER_TO_HOST) {
     892                 :     144985 :                         rc = nvme_tcp_build_sgl_request(tqpair, tcp_req);
     893                 :            :                 }
     894                 :            :         } else {
     895                 :          0 :                 rc = -1;
     896                 :            :         }
     897                 :            : 
     898         [ +  + ]:   16990740 :         if (rc) {
     899                 :          6 :                 return rc;
     900                 :            :         }
     901                 :            : 
     902         [ +  + ]:   16990734 :         if (xfer == SPDK_NVME_DATA_HOST_TO_CONTROLLER) {
     903                 :    7744857 :                 max_in_capsule_data_size = ctrlr->ioccsz_bytes;
     904   [ +  +  +  + ]:    7744857 :                 if (spdk_unlikely((req->cmd.opc == SPDK_NVME_OPC_FABRIC) ||
     905                 :            :                                   nvme_qpair_is_admin_queue(&tqpair->qpair))) {
     906                 :      35345 :                         max_in_capsule_data_size = SPDK_NVME_TCP_IN_CAPSULE_DATA_MAX_SIZE;
     907                 :            :                 }
     908                 :            : 
     909         [ +  + ]:    7744857 :                 if (req->payload_size <= max_in_capsule_data_size) {
     910                 :    7059166 :                         req->cmd.dptr.sgl1.unkeyed.type = SPDK_NVME_SGL_TYPE_DATA_BLOCK;
     911                 :    7059166 :                         req->cmd.dptr.sgl1.unkeyed.subtype = SPDK_NVME_SGL_SUBTYPE_OFFSET;
     912                 :    7059166 :                         req->cmd.dptr.sgl1.address = 0;
     913                 :    7059166 :                         tcp_req->in_capsule_data = true;
     914                 :            :                 }
     915                 :            :         }
     916                 :            : 
     917                 :   16990734 :         return 0;
     918                 :            : }
     919                 :            : 
     920                 :            : static inline bool
     921                 :   34657620 : nvme_tcp_req_complete_safe(struct nvme_tcp_req *tcp_req)
     922                 :            : {
     923   [ +  +  +  + ]:   34657620 :         if (!(tcp_req->ordering.bits.send_ack && tcp_req->ordering.bits.data_recv &&
     924         [ -  + ]:   16981941 :               !tcp_req->ordering.bits.in_progress_accel)) {
     925                 :   17675679 :                 return false;
     926                 :            :         }
     927                 :            : 
     928         [ -  + ]:   16981941 :         assert(tcp_req->state == NVME_TCP_REQ_ACTIVE);
     929         [ -  + ]:   16981941 :         assert(tcp_req->tqpair != NULL);
     930         [ -  + ]:   16981941 :         assert(tcp_req->req != NULL);
     931                 :            : 
     932                 :   16981941 :         nvme_tcp_req_complete(tcp_req, tcp_req->tqpair, &tcp_req->rsp, true);
     933                 :   16981941 :         return true;
     934                 :            : }
     935                 :            : 
     936                 :            : static void
     937                 :   16990175 : nvme_tcp_qpair_cmd_send_complete(void *cb_arg)
     938                 :            : {
     939                 :   16990175 :         struct nvme_tcp_req *tcp_req = cb_arg;
     940                 :            : 
     941   [ -  +  +  + ]:   16990175 :         SPDK_DEBUGLOG(nvme, "tcp req %p, cid %u, qid %u\n", tcp_req, tcp_req->cid,
     942                 :            :                       tcp_req->tqpair->qpair.id);
     943                 :   16990175 :         tcp_req->ordering.bits.send_ack = 1;
     944                 :            :         /* Handle the r2t case */
     945         [ -  + ]:   16990175 :         if (spdk_unlikely(tcp_req->ordering.bits.h2c_send_waiting_ack)) {
     946   [ #  #  #  # ]:          0 :                 SPDK_DEBUGLOG(nvme, "tcp req %p, send H2C data\n", tcp_req);
     947                 :          0 :                 nvme_tcp_send_h2c_data(tcp_req);
     948                 :            :         } else {
     949   [ -  +  +  +  :   16990175 :                 if (tcp_req->in_capsule_data && tcp_req->ordering.bits.domain_in_use) {
                   +  + ]
     950                 :     122846 :                         spdk_memory_domain_invalidate_data(tcp_req->req->payload.opts->memory_domain,
     951                 :      61423 :                                                            tcp_req->req->payload.opts->memory_domain_ctx, tcp_req->iov, tcp_req->iovcnt);
     952                 :            :                 }
     953                 :            : 
     954                 :   16990175 :                 nvme_tcp_req_complete_safe(tcp_req);
     955                 :            :         }
     956                 :   16990175 : }
     957                 :            : 
     958                 :            : static int
     959                 :   16990734 : nvme_tcp_qpair_capsule_cmd_send(struct nvme_tcp_qpair *tqpair,
     960                 :            :                                 struct nvme_tcp_req *tcp_req)
     961                 :            : {
     962                 :            :         struct nvme_tcp_pdu *pdu;
     963                 :            :         struct spdk_nvme_tcp_cmd *capsule_cmd;
     964                 :   16990734 :         uint32_t plen = 0, alignment;
     965                 :            :         uint8_t pdo;
     966                 :            : 
     967   [ -  +  +  + ]:   16990734 :         SPDK_DEBUGLOG(nvme, "enter\n");
     968                 :   16990734 :         pdu = tcp_req->pdu;
     969                 :   16990734 :         pdu->req = tcp_req;
     970                 :            : 
     971                 :   16990734 :         capsule_cmd = &pdu->hdr.capsule_cmd;
     972                 :   16990734 :         capsule_cmd->common.pdu_type = SPDK_NVME_TCP_PDU_TYPE_CAPSULE_CMD;
     973                 :   16990734 :         plen = capsule_cmd->common.hlen = sizeof(*capsule_cmd);
     974                 :   16990734 :         capsule_cmd->ccsqe = tcp_req->req->cmd;
     975                 :            : 
     976   [ -  +  +  + ]:   16990734 :         SPDK_DEBUGLOG(nvme, "capsule_cmd cid=%u on tqpair(%p)\n", tcp_req->req->cmd.cid, tqpair);
     977                 :            : 
     978         [ +  + ]:   16990734 :         if (tqpair->flags.host_hdgst_enable) {
     979   [ -  +  -  + ]:      62281 :                 SPDK_DEBUGLOG(nvme, "Header digest is enabled for capsule command on tcp_req=%p\n",
     980                 :            :                               tcp_req);
     981                 :      62281 :                 capsule_cmd->common.flags |= SPDK_NVME_TCP_CH_FLAGS_HDGSTF;
     982                 :      62281 :                 plen += SPDK_NVME_TCP_DIGEST_LEN;
     983                 :            :         }
     984                 :            : 
     985   [ +  +  -  +  :   16990734 :         if ((tcp_req->req->payload_size == 0) || !tcp_req->in_capsule_data) {
                   +  + ]
     986                 :   10183899 :                 goto end;
     987                 :            :         }
     988                 :            : 
     989                 :    6806835 :         pdo = plen;
     990                 :    6806835 :         pdu->padding_len = 0;
     991         [ +  + ]:    6806835 :         if (tqpair->cpda) {
     992         [ -  + ]:          6 :                 alignment = (tqpair->cpda + 1) << 2;
     993         [ +  - ]:          6 :                 if (alignment > plen) {
     994                 :          6 :                         pdu->padding_len = alignment - plen;
     995                 :          6 :                         pdo = alignment;
     996                 :          6 :                         plen = alignment;
     997                 :            :                 }
     998                 :            :         }
     999                 :            : 
    1000                 :    6806835 :         capsule_cmd->common.pdo = pdo;
    1001                 :    6806835 :         plen += tcp_req->req->payload_size;
    1002         [ +  + ]:    6806835 :         if (tqpair->flags.host_ddgst_enable) {
    1003                 :     348997 :                 capsule_cmd->common.flags |= SPDK_NVME_TCP_CH_FLAGS_DDGSTF;
    1004                 :     348997 :                 plen += SPDK_NVME_TCP_DIGEST_LEN;
    1005                 :            :         }
    1006                 :            : 
    1007                 :    6806835 :         tcp_req->datao = 0;
    1008                 :    6806835 :         nvme_tcp_pdu_set_data_buf(pdu, tcp_req->iov, tcp_req->iovcnt,
    1009                 :    6806835 :                                   0, tcp_req->req->payload_size);
    1010                 :   16990734 : end:
    1011                 :   16990734 :         capsule_cmd->common.plen = plen;
    1012                 :   16990734 :         return nvme_tcp_qpair_write_pdu(tqpair, pdu, nvme_tcp_qpair_cmd_send_complete, tcp_req);
    1013                 :            : 
    1014                 :            : }
    1015                 :            : 
    1016                 :            : static int
    1017                 :   17431691 : nvme_tcp_qpair_submit_request(struct spdk_nvme_qpair *qpair,
    1018                 :            :                               struct nvme_request *req)
    1019                 :            : {
    1020                 :            :         struct nvme_tcp_qpair *tqpair;
    1021                 :            :         struct nvme_tcp_req *tcp_req;
    1022                 :            : 
    1023                 :   17431691 :         tqpair = nvme_tcp_qpair(qpair);
    1024         [ -  + ]:   17431691 :         assert(tqpair != NULL);
    1025         [ -  + ]:   17431691 :         assert(req != NULL);
    1026                 :            : 
    1027                 :   17431691 :         tcp_req = nvme_tcp_req_get(tqpair);
    1028         [ +  + ]:   17431691 :         if (!tcp_req) {
    1029                 :     440969 :                 tqpair->stats->queued_requests++;
    1030                 :            :                 /* Inform the upper layer to try again later. */
    1031                 :     440969 :                 return -EAGAIN;
    1032                 :            :         }
    1033                 :            : 
    1034         [ +  + ]:   16990722 :         if (spdk_unlikely(nvme_tcp_req_init(tqpair, req, tcp_req))) {
    1035                 :          6 :                 SPDK_ERRLOG("nvme_tcp_req_init() failed\n");
    1036                 :          6 :                 nvme_tcp_req_put(tqpair, tcp_req);
    1037                 :          6 :                 return -1;
    1038                 :            :         }
    1039                 :            : 
    1040                 :   16990716 :         tqpair->qpair.queue_depth++;
    1041   [ +  +  +  + ]:   16990716 :         spdk_trace_record(TRACE_NVME_TCP_SUBMIT, qpair->id, 0, (uintptr_t)tcp_req->pdu, req->cb_arg,
    1042                 :            :                           (uint32_t)req->cmd.cid, (uint32_t)req->cmd.opc,
    1043                 :            :                           req->cmd.cdw10, req->cmd.cdw11, req->cmd.cdw12, tqpair->qpair.queue_depth);
    1044                 :   16990716 :         TAILQ_INSERT_TAIL(&tqpair->outstanding_reqs, tcp_req, link);
    1045                 :   16990716 :         return nvme_tcp_qpair_capsule_cmd_send(tqpair, tcp_req);
    1046                 :            : }
    1047                 :            : 
    1048                 :            : static int
    1049                 :       1240 : nvme_tcp_qpair_reset(struct spdk_nvme_qpair *qpair)
    1050                 :            : {
    1051                 :       1240 :         return 0;
    1052                 :            : }
    1053                 :            : 
    1054                 :            : static void
    1055                 :   16990764 : nvme_tcp_req_complete(struct nvme_tcp_req *tcp_req,
    1056                 :            :                       struct nvme_tcp_qpair *tqpair,
    1057                 :            :                       struct spdk_nvme_cpl *rsp,
    1058                 :            :                       bool print_on_error)
    1059                 :            : {
    1060                 :     275547 :         struct spdk_nvme_cpl    cpl;
    1061                 :            :         struct spdk_nvme_qpair  *qpair;
    1062                 :            :         struct nvme_request     *req;
    1063                 :            :         bool                    print_error;
    1064                 :            : 
    1065         [ -  + ]:   16990764 :         assert(tcp_req->req != NULL);
    1066                 :   16990764 :         req = tcp_req->req;
    1067                 :   16990764 :         qpair = req->qpair;
    1068                 :            : 
    1069   [ -  +  +  + ]:   16990764 :         SPDK_DEBUGLOG(nvme, "complete tcp_req(%p) on tqpair=%p\n", tcp_req, tqpair);
    1070                 :            : 
    1071         [ +  + ]:   16990764 :         if (!tcp_req->tqpair->qpair.in_completion_context) {
    1072                 :     399727 :                 tcp_req->tqpair->async_complete++;
    1073                 :            :         }
    1074                 :            : 
    1075                 :            :         /* Cache arguments to be passed to nvme_complete_request since tcp_req can be zeroed when released */
    1076                 :   16990764 :         memcpy(&cpl, rsp, sizeof(cpl));
    1077                 :            : 
    1078   [ +  +  -  + ]:   16990764 :         if (spdk_unlikely(spdk_nvme_cpl_is_error(rsp))) {
    1079   [ +  +  +  +  :    1113397 :                 print_error = print_on_error && !qpair->ctrlr->opts.disable_error_logging;
                   +  + ]
    1080                 :            : 
    1081         [ +  + ]:    1113397 :                 if (print_error) {
    1082                 :    1095954 :                         spdk_nvme_qpair_print_command(qpair, &req->cmd);
    1083                 :            :                 }
    1084                 :            : 
    1085   [ +  +  +  + ]:    1113397 :                 if (print_error || SPDK_DEBUGLOG_FLAG_ENABLED("nvme")) {
    1086                 :    1095978 :                         spdk_nvme_qpair_print_completion(qpair, rsp);
    1087                 :            :                 }
    1088                 :            :         }
    1089                 :            : 
    1090                 :   16990764 :         tqpair->qpair.queue_depth--;
    1091   [ +  +  +  + ]:   16990764 :         spdk_trace_record(TRACE_NVME_TCP_COMPLETE, qpair->id, 0, (uintptr_t)tcp_req->pdu, req->cb_arg,
    1092                 :            :                           (uint32_t)req->cmd.cid, (uint32_t)cpl.status_raw, tqpair->qpair.queue_depth);
    1093         [ +  + ]:   16990764 :         TAILQ_REMOVE(&tcp_req->tqpair->outstanding_reqs, tcp_req, link);
    1094                 :   16990764 :         nvme_tcp_req_put(tqpair, tcp_req);
    1095                 :   16990764 :         nvme_complete_request(req->cb_fn, req->cb_arg, req->qpair, req, &cpl);
    1096                 :   16990764 : }
    1097                 :            : 
    1098                 :            : static void
    1099                 :      17735 : nvme_tcp_qpair_abort_reqs(struct spdk_nvme_qpair *qpair, uint32_t dnr)
    1100                 :            : {
    1101                 :            :         struct nvme_tcp_req *tcp_req, *tmp;
    1102                 :      17735 :         struct spdk_nvme_cpl cpl = {};
    1103                 :      17735 :         struct nvme_tcp_qpair *tqpair = nvme_tcp_qpair(qpair);
    1104                 :            : 
    1105                 :      17735 :         cpl.sqid = qpair->id;
    1106                 :      17735 :         cpl.status.sc = SPDK_NVME_SC_ABORTED_SQ_DELETION;
    1107                 :      17735 :         cpl.status.sct = SPDK_NVME_SCT_GENERIC;
    1108                 :      17735 :         cpl.status.dnr = dnr;
    1109                 :            : 
    1110         [ +  + ]:      21788 :         TAILQ_FOREACH_SAFE(tcp_req, &tqpair->outstanding_reqs, link, tmp) {
    1111                 :            :                 /* We cannot abort requests with accel operations in progress */
    1112         [ +  + ]:       4053 :                 if (tcp_req->ordering.bits.in_progress_accel) {
    1113                 :         12 :                         continue;
    1114                 :            :                 }
    1115                 :            : 
    1116                 :       4041 :                 nvme_tcp_req_complete(tcp_req, tqpair, &cpl, true);
    1117                 :            :         }
    1118                 :      17735 : }
    1119                 :            : 
    1120                 :            : static void
    1121                 :          6 : nvme_tcp_qpair_send_h2c_term_req_complete(void *cb_arg)
    1122                 :            : {
    1123                 :          6 :         struct nvme_tcp_qpair *tqpair = cb_arg;
    1124                 :            : 
    1125                 :          6 :         tqpair->state = NVME_TCP_QPAIR_STATE_EXITING;
    1126                 :          6 : }
    1127                 :            : 
    1128                 :            : static void
    1129                 :         96 : nvme_tcp_qpair_send_h2c_term_req(struct nvme_tcp_qpair *tqpair, struct nvme_tcp_pdu *pdu,
    1130                 :            :                                  enum spdk_nvme_tcp_term_req_fes fes, uint32_t error_offset)
    1131                 :            : {
    1132                 :            :         struct nvme_tcp_pdu *rsp_pdu;
    1133                 :            :         struct spdk_nvme_tcp_term_req_hdr *h2c_term_req;
    1134                 :         96 :         uint32_t h2c_term_req_hdr_len = sizeof(*h2c_term_req);
    1135                 :            :         uint8_t copy_len;
    1136                 :            : 
    1137                 :         96 :         rsp_pdu = tqpair->send_pdu;
    1138         [ -  + ]:         96 :         memset(rsp_pdu, 0, sizeof(*rsp_pdu));
    1139                 :         96 :         h2c_term_req = &rsp_pdu->hdr.term_req;
    1140                 :         96 :         h2c_term_req->common.pdu_type = SPDK_NVME_TCP_PDU_TYPE_H2C_TERM_REQ;
    1141                 :         96 :         h2c_term_req->common.hlen = h2c_term_req_hdr_len;
    1142                 :            : 
    1143   [ +  +  -  + ]:         96 :         if ((fes == SPDK_NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD) ||
    1144                 :            :             (fes == SPDK_NVME_TCP_TERM_REQ_FES_INVALID_DATA_UNSUPPORTED_PARAMETER)) {
    1145                 :         84 :                 DSET32(&h2c_term_req->fei, error_offset);
    1146                 :            :         }
    1147                 :            : 
    1148                 :         96 :         copy_len = pdu->hdr.common.hlen;
    1149         [ +  + ]:         96 :         if (copy_len > SPDK_NVME_TCP_TERM_REQ_ERROR_DATA_MAX_SIZE) {
    1150                 :          6 :                 copy_len = SPDK_NVME_TCP_TERM_REQ_ERROR_DATA_MAX_SIZE;
    1151                 :            :         }
    1152                 :            : 
    1153                 :            :         /* Copy the error info into the buffer */
    1154   [ -  +  -  + ]:         96 :         memcpy((uint8_t *)rsp_pdu->hdr.raw + h2c_term_req_hdr_len, pdu->hdr.raw, copy_len);
    1155                 :         96 :         nvme_tcp_pdu_set_data(rsp_pdu, (uint8_t *)rsp_pdu->hdr.raw + h2c_term_req_hdr_len, copy_len);
    1156                 :            : 
    1157                 :            :         /* Contain the header len of the wrong received pdu */
    1158                 :         96 :         h2c_term_req->common.plen = h2c_term_req->common.hlen + copy_len;
    1159                 :         96 :         nvme_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_QUIESCING);
    1160                 :         96 :         nvme_tcp_qpair_write_pdu(tqpair, rsp_pdu, nvme_tcp_qpair_send_h2c_term_req_complete, tqpair);
    1161                 :         96 : }
    1162                 :            : 
    1163                 :            : static bool
    1164                 :   25403754 : nvme_tcp_qpair_recv_state_valid(struct nvme_tcp_qpair *tqpair)
    1165                 :            : {
    1166         [ +  + ]:   25403754 :         switch (tqpair->state) {
    1167                 :   25403748 :         case NVME_TCP_QPAIR_STATE_FABRIC_CONNECT_SEND:
    1168                 :            :         case NVME_TCP_QPAIR_STATE_FABRIC_CONNECT_POLL:
    1169                 :            :         case NVME_TCP_QPAIR_STATE_RUNNING:
    1170                 :   25403748 :                 return true;
    1171                 :          6 :         default:
    1172                 :          6 :                 return false;
    1173                 :            :         }
    1174                 :            : }
    1175                 :            : 
    1176                 :            : static void
    1177                 :   25408000 : nvme_tcp_pdu_ch_handle(struct nvme_tcp_qpair *tqpair)
    1178                 :            : {
    1179                 :            :         struct nvme_tcp_pdu *pdu;
    1180                 :   25408000 :         uint32_t error_offset = 0;
    1181                 :            :         enum spdk_nvme_tcp_term_req_fes fes;
    1182                 :   25408000 :         uint32_t expected_hlen, hd_len = 0;
    1183                 :   25408000 :         bool plen_error = false;
    1184                 :            : 
    1185                 :   25408000 :         pdu = tqpair->recv_pdu;
    1186                 :            : 
    1187   [ -  +  +  + ]:   25408000 :         SPDK_DEBUGLOG(nvme, "pdu type = %d\n", pdu->hdr.common.pdu_type);
    1188         [ +  + ]:   25408000 :         if (pdu->hdr.common.pdu_type == SPDK_NVME_TCP_PDU_TYPE_IC_RESP) {
    1189         [ +  + ]:       4246 :                 if (tqpair->state != NVME_TCP_QPAIR_STATE_INVALID) {
    1190                 :          6 :                         SPDK_ERRLOG("Already received IC_RESP PDU, and we should reject this pdu=%p\n", pdu);
    1191                 :          6 :                         fes = SPDK_NVME_TCP_TERM_REQ_FES_PDU_SEQUENCE_ERROR;
    1192                 :          6 :                         goto err;
    1193                 :            :                 }
    1194                 :       4240 :                 expected_hlen = sizeof(struct spdk_nvme_tcp_ic_resp);
    1195         [ +  + ]:       4240 :                 if (pdu->hdr.common.plen != expected_hlen) {
    1196                 :          6 :                         plen_error = true;
    1197                 :            :                 }
    1198                 :            :         } else {
    1199         [ +  + ]:   25403754 :                 if (spdk_unlikely(!nvme_tcp_qpair_recv_state_valid(tqpair))) {
    1200                 :          6 :                         SPDK_ERRLOG("The TCP/IP tqpair connection is not negotiated\n");
    1201                 :          6 :                         fes = SPDK_NVME_TCP_TERM_REQ_FES_PDU_SEQUENCE_ERROR;
    1202                 :          6 :                         goto err;
    1203                 :            :                 }
    1204                 :            : 
    1205   [ +  +  +  +  :   25403748 :                 switch (pdu->hdr.common.pdu_type) {
                      + ]
    1206                 :   16485623 :                 case SPDK_NVME_TCP_PDU_TYPE_CAPSULE_RESP:
    1207                 :   16485623 :                         expected_hlen = sizeof(struct spdk_nvme_tcp_rsp);
    1208         [ +  + ]:   16485623 :                         if (pdu->hdr.common.flags & SPDK_NVME_TCP_CH_FLAGS_HDGSTF) {
    1209                 :      62251 :                                 hd_len = SPDK_NVME_TCP_DIGEST_LEN;
    1210                 :            :                         }
    1211                 :            : 
    1212         [ +  + ]:   16485623 :                         if (pdu->hdr.common.plen != (expected_hlen + hd_len)) {
    1213                 :          6 :                                 plen_error = true;
    1214                 :            :                         }
    1215                 :   16485623 :                         break;
    1216                 :    8232603 :                 case SPDK_NVME_TCP_PDU_TYPE_C2H_DATA:
    1217                 :    8232603 :                         expected_hlen = sizeof(struct spdk_nvme_tcp_c2h_data_hdr);
    1218         [ +  + ]:    8232603 :                         if (pdu->hdr.common.plen < pdu->hdr.common.pdo) {
    1219                 :          6 :                                 plen_error = true;
    1220                 :            :                         }
    1221                 :    8232603 :                         break;
    1222                 :          6 :                 case SPDK_NVME_TCP_PDU_TYPE_C2H_TERM_REQ:
    1223                 :          6 :                         expected_hlen = sizeof(struct spdk_nvme_tcp_term_req_hdr);
    1224         [ -  + ]:          6 :                         if ((pdu->hdr.common.plen <= expected_hlen) ||
    1225         [ #  # ]:          0 :                             (pdu->hdr.common.plen > SPDK_NVME_TCP_TERM_REQ_PDU_MAX_SIZE)) {
    1226                 :          6 :                                 plen_error = true;
    1227                 :            :                         }
    1228                 :          6 :                         break;
    1229                 :     685504 :                 case SPDK_NVME_TCP_PDU_TYPE_R2T:
    1230                 :     685504 :                         expected_hlen = sizeof(struct spdk_nvme_tcp_r2t_hdr);
    1231         [ +  + ]:     685504 :                         if (pdu->hdr.common.flags & SPDK_NVME_TCP_CH_FLAGS_HDGSTF) {
    1232                 :          6 :                                 hd_len = SPDK_NVME_TCP_DIGEST_LEN;
    1233                 :            :                         }
    1234                 :            : 
    1235         [ +  + ]:     685504 :                         if (pdu->hdr.common.plen != (expected_hlen + hd_len)) {
    1236                 :          6 :                                 plen_error = true;
    1237                 :            :                         }
    1238                 :     685504 :                         break;
    1239                 :            : 
    1240                 :         12 :                 default:
    1241                 :         12 :                         SPDK_ERRLOG("Unexpected PDU type 0x%02x\n", tqpair->recv_pdu->hdr.common.pdu_type);
    1242                 :         12 :                         fes = SPDK_NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD;
    1243                 :         12 :                         error_offset = offsetof(struct spdk_nvme_tcp_common_pdu_hdr, pdu_type);
    1244                 :         12 :                         goto err;
    1245                 :            :                 }
    1246                 :            :         }
    1247                 :            : 
    1248         [ +  + ]:   25407976 :         if (pdu->hdr.common.hlen != expected_hlen) {
    1249                 :          6 :                 SPDK_ERRLOG("Expected PDU header length %u, got %u\n",
    1250                 :            :                             expected_hlen, pdu->hdr.common.hlen);
    1251                 :          6 :                 fes = SPDK_NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD;
    1252                 :          6 :                 error_offset = offsetof(struct spdk_nvme_tcp_common_pdu_hdr, hlen);
    1253                 :          6 :                 goto err;
    1254                 :            : 
    1255         [ +  + ]:   25407970 :         } else if (plen_error) {
    1256                 :         30 :                 fes = SPDK_NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD;
    1257                 :         30 :                 error_offset = offsetof(struct spdk_nvme_tcp_common_pdu_hdr, plen);
    1258                 :         30 :                 goto err;
    1259                 :            :         } else {
    1260                 :   25407940 :                 nvme_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_PSH);
    1261                 :   25407940 :                 nvme_tcp_pdu_calc_psh_len(tqpair->recv_pdu, tqpair->flags.host_hdgst_enable);
    1262                 :   25407940 :                 return;
    1263                 :            :         }
    1264                 :         60 : err:
    1265                 :         60 :         nvme_tcp_qpair_send_h2c_term_req(tqpair, pdu, fes, error_offset);
    1266                 :            : }
    1267                 :            : 
    1268                 :            : static struct nvme_tcp_req *
    1269                 :   25403724 : get_nvme_active_req_by_cid(struct nvme_tcp_qpair *tqpair, uint32_t cid)
    1270                 :            : {
    1271         [ -  + ]:   25403724 :         assert(tqpair != NULL);
    1272   [ +  +  -  + ]:   25403724 :         if ((cid >= tqpair->num_entries) || (tqpair->tcp_reqs[cid].state == NVME_TCP_REQ_FREE)) {
    1273                 :          6 :                 return NULL;
    1274                 :            :         }
    1275                 :            : 
    1276                 :   25403718 :         return &tqpair->tcp_reqs[cid];
    1277                 :            : }
    1278                 :            : 
    1279                 :            : static void
    1280                 :     392842 : nvme_tcp_recv_payload_seq_cb(void *cb_arg, int status)
    1281                 :            : {
    1282                 :     392842 :         struct nvme_tcp_req *treq = cb_arg;
    1283                 :     392842 :         struct nvme_request *req = treq->req;
    1284                 :     392842 :         struct nvme_tcp_qpair *tqpair = treq->tqpair;
    1285                 :            : 
    1286         [ -  + ]:     392842 :         assert(treq->ordering.bits.in_progress_accel);
    1287                 :     392842 :         treq->ordering.bits.in_progress_accel = 0;
    1288                 :            : 
    1289                 :     392842 :         nvme_tcp_cond_schedule_qpair_polling(tqpair);
    1290                 :            : 
    1291                 :     392842 :         req->accel_sequence = NULL;
    1292         [ -  + ]:     392842 :         if (spdk_unlikely(status != 0)) {
    1293                 :          0 :                 pdu_seq_fail(treq->pdu, status);
    1294                 :          0 :                 return;
    1295                 :            :         }
    1296                 :            : 
    1297                 :     392842 :         nvme_tcp_req_complete_safe(treq);
    1298                 :            : }
    1299                 :            : 
    1300                 :            : static void
    1301                 :    8232612 : nvme_tcp_c2h_data_payload_handle(struct nvme_tcp_qpair *tqpair,
    1302                 :            :                                  struct nvme_tcp_pdu *pdu, uint32_t *reaped)
    1303                 :            : {
    1304                 :            :         struct nvme_tcp_req *tcp_req;
    1305                 :            :         struct nvme_tcp_poll_group *tgroup;
    1306                 :            :         struct spdk_nvme_tcp_c2h_data_hdr *c2h_data;
    1307                 :            :         uint8_t flags;
    1308                 :            : 
    1309                 :    8232612 :         tcp_req = pdu->req;
    1310         [ -  + ]:    8232612 :         assert(tcp_req != NULL);
    1311                 :            : 
    1312   [ -  +  +  + ]:    8232612 :         SPDK_DEBUGLOG(nvme, "enter\n");
    1313                 :    8232612 :         c2h_data = &pdu->hdr.c2h_data;
    1314                 :    8232612 :         tcp_req->datao += pdu->data_len;
    1315                 :    8232612 :         flags = c2h_data->common.flags;
    1316                 :            : 
    1317         [ +  + ]:    8232612 :         if (flags & SPDK_NVME_TCP_C2H_DATA_FLAGS_LAST_PDU) {
    1318         [ +  + ]:    7969720 :                 if (tcp_req->datao == tcp_req->req->payload_size) {
    1319                 :    7969240 :                         tcp_req->rsp.status.p = 0;
    1320                 :            :                 } else {
    1321                 :        480 :                         tcp_req->rsp.status.p = 1;
    1322                 :            :                 }
    1323                 :            : 
    1324                 :    7969720 :                 tcp_req->rsp.cid = tcp_req->cid;
    1325                 :    7969720 :                 tcp_req->rsp.sqid = tqpair->qpair.id;
    1326         [ +  + ]:    7969720 :                 if (flags & SPDK_NVME_TCP_C2H_DATA_FLAGS_SUCCESS) {
    1327                 :     496300 :                         tcp_req->ordering.bits.data_recv = 1;
    1328         [ +  + ]:     496300 :                         if (tcp_req->req->accel_sequence != NULL) {
    1329                 :      73383 :                                 tgroup = nvme_tcp_poll_group(tqpair->qpair.poll_group);
    1330                 :      73383 :                                 nvme_tcp_accel_reverse_sequence(tgroup, tcp_req->req->accel_sequence);
    1331                 :      73383 :                                 nvme_tcp_accel_finish_sequence(tgroup, tcp_req,
    1332                 :      73383 :                                                                tcp_req->req->accel_sequence,
    1333                 :            :                                                                nvme_tcp_recv_payload_seq_cb,
    1334                 :            :                                                                tcp_req);
    1335                 :      73383 :                                 return;
    1336                 :            :                         }
    1337                 :            : 
    1338         [ +  - ]:     422917 :                         if (nvme_tcp_req_complete_safe(tcp_req)) {
    1339                 :     422917 :                                 (*reaped)++;
    1340                 :            :                         }
    1341                 :            :                 }
    1342                 :            :         }
    1343                 :            : }
    1344                 :            : 
    1345                 :            : static const char *spdk_nvme_tcp_term_req_fes_str[] = {
    1346                 :            :         "Invalid PDU Header Field",
    1347                 :            :         "PDU Sequence Error",
    1348                 :            :         "Header Digest Error",
    1349                 :            :         "Data Transfer Out of Range",
    1350                 :            :         "Data Transfer Limit Exceeded",
    1351                 :            :         "Unsupported parameter",
    1352                 :            : };
    1353                 :            : 
    1354                 :            : static void
    1355                 :         12 : nvme_tcp_c2h_term_req_dump(struct spdk_nvme_tcp_term_req_hdr *c2h_term_req)
    1356                 :            : {
    1357                 :         12 :         SPDK_ERRLOG("Error info of pdu(%p): %s\n", c2h_term_req,
    1358                 :            :                     spdk_nvme_tcp_term_req_fes_str[c2h_term_req->fes]);
    1359         [ -  + ]:         12 :         if ((c2h_term_req->fes == SPDK_NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD) ||
    1360         [ #  # ]:          0 :             (c2h_term_req->fes == SPDK_NVME_TCP_TERM_REQ_FES_INVALID_DATA_UNSUPPORTED_PARAMETER)) {
    1361   [ -  +  -  + ]:         12 :                 SPDK_DEBUGLOG(nvme, "The offset from the start of the PDU header is %u\n",
    1362                 :            :                               DGET32(c2h_term_req->fei));
    1363                 :            :         }
    1364                 :            :         /* we may also need to dump some other info here */
    1365                 :         12 : }
    1366                 :            : 
    1367                 :            : static void
    1368                 :         12 : nvme_tcp_c2h_term_req_payload_handle(struct nvme_tcp_qpair *tqpair,
    1369                 :            :                                      struct nvme_tcp_pdu *pdu)
    1370                 :            : {
    1371                 :         12 :         nvme_tcp_c2h_term_req_dump(&pdu->hdr.term_req);
    1372                 :         12 :         nvme_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_QUIESCING);
    1373                 :         12 : }
    1374                 :            : 
    1375                 :            : static void
    1376                 :    7839758 : _nvme_tcp_pdu_payload_handle(struct nvme_tcp_qpair *tqpair, uint32_t *reaped)
    1377                 :            : {
    1378                 :            :         struct nvme_tcp_pdu *pdu;
    1379                 :            : 
    1380         [ -  + ]:    7839758 :         assert(tqpair != NULL);
    1381                 :    7839758 :         pdu = tqpair->recv_pdu;
    1382                 :            : 
    1383      [ +  +  - ]:    7839758 :         switch (pdu->hdr.common.pdu_type) {
    1384                 :    7839752 :         case SPDK_NVME_TCP_PDU_TYPE_C2H_DATA:
    1385                 :    7839752 :                 nvme_tcp_c2h_data_payload_handle(tqpair, pdu, reaped);
    1386                 :    7839752 :                 nvme_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_READY);
    1387                 :    7839752 :                 break;
    1388                 :            : 
    1389                 :          6 :         case SPDK_NVME_TCP_PDU_TYPE_C2H_TERM_REQ:
    1390                 :          6 :                 nvme_tcp_c2h_term_req_payload_handle(tqpair, pdu);
    1391                 :          6 :                 break;
    1392                 :            : 
    1393                 :          0 :         default:
    1394                 :            :                 /* The code should not go to here */
    1395                 :          0 :                 SPDK_ERRLOG("The code should not go to here\n");
    1396                 :          0 :                 break;
    1397                 :            :         }
    1398                 :    7839758 : }
    1399                 :            : 
    1400                 :            : static void
    1401                 :          0 : nvme_tcp_accel_recv_compute_crc32_done(void *cb_arg, int status)
    1402                 :            : {
    1403                 :          0 :         struct nvme_tcp_req *tcp_req = cb_arg;
    1404                 :            :         struct nvme_tcp_pdu *pdu;
    1405                 :            :         struct nvme_tcp_qpair *tqpair;
    1406                 :            :         int rc;
    1407                 :          0 :         int dummy_reaped = 0;
    1408                 :            : 
    1409                 :          0 :         pdu = tcp_req->pdu;
    1410         [ #  # ]:          0 :         assert(pdu != NULL);
    1411                 :            : 
    1412                 :          0 :         tqpair = tcp_req->tqpair;
    1413         [ #  # ]:          0 :         assert(tqpair != NULL);
    1414                 :            : 
    1415         [ #  # ]:          0 :         assert(tcp_req->ordering.bits.in_progress_accel);
    1416                 :          0 :         tcp_req->ordering.bits.in_progress_accel = 0;
    1417                 :            : 
    1418                 :          0 :         nvme_tcp_cond_schedule_qpair_polling(tqpair);
    1419                 :            : 
    1420         [ #  # ]:          0 :         if (spdk_unlikely(status)) {
    1421                 :          0 :                 SPDK_ERRLOG("Failed to compute the data digest for pdu =%p\n", pdu);
    1422                 :          0 :                 tcp_req->rsp.status.sc = SPDK_NVME_SC_COMMAND_TRANSIENT_TRANSPORT_ERROR;
    1423                 :          0 :                 goto end;
    1424                 :            :         }
    1425                 :            : 
    1426                 :          0 :         pdu->data_digest_crc32 ^= SPDK_CRC32C_XOR;
    1427                 :          0 :         rc = MATCH_DIGEST_WORD(pdu->data_digest, pdu->data_digest_crc32);
    1428         [ #  # ]:          0 :         if (rc == 0) {
    1429                 :          0 :                 SPDK_ERRLOG("data digest error on tqpair=(%p) with pdu=%p\n", tqpair, pdu);
    1430                 :          0 :                 tcp_req->rsp.status.sc = SPDK_NVME_SC_COMMAND_TRANSIENT_TRANSPORT_ERROR;
    1431                 :            :         }
    1432                 :            : 
    1433                 :          0 : end:
    1434                 :          0 :         nvme_tcp_c2h_data_payload_handle(tqpair, tcp_req->pdu, &dummy_reaped);
    1435                 :          0 : }
    1436                 :            : 
    1437                 :            : static void
    1438                 :     392842 : nvme_tcp_req_copy_pdu(struct nvme_tcp_req *treq, struct nvme_tcp_pdu *pdu)
    1439                 :            : {
    1440                 :     392842 :         treq->pdu->hdr = pdu->hdr;
    1441                 :     392842 :         treq->pdu->req = treq;
    1442                 :     392842 :         memcpy(treq->pdu->data_digest, pdu->data_digest, sizeof(pdu->data_digest));
    1443   [ -  +  -  + ]:     392842 :         memcpy(treq->pdu->data_iov, pdu->data_iov, sizeof(pdu->data_iov[0]) * pdu->data_iovcnt);
    1444                 :     392842 :         treq->pdu->data_iovcnt = pdu->data_iovcnt;
    1445                 :     392842 :         treq->pdu->data_len = pdu->data_len;
    1446                 :     392842 : }
    1447                 :            : 
    1448                 :            : static void
    1449                 :     392842 : nvme_tcp_accel_seq_recv_compute_crc32_done(void *cb_arg)
    1450                 :            : {
    1451                 :     392842 :         struct nvme_tcp_req *treq = cb_arg;
    1452                 :     392842 :         struct nvme_tcp_qpair *tqpair = treq->tqpair;
    1453                 :     392842 :         struct nvme_tcp_pdu *pdu = treq->pdu;
    1454                 :            :         bool result;
    1455                 :            : 
    1456                 :     392842 :         pdu->data_digest_crc32 ^= SPDK_CRC32C_XOR;
    1457                 :     392842 :         result = MATCH_DIGEST_WORD(pdu->data_digest, pdu->data_digest_crc32);
    1458         [ +  + ]:     392842 :         if (spdk_unlikely(!result)) {
    1459                 :       1704 :                 SPDK_ERRLOG("data digest error on tqpair=(%p)\n", tqpair);
    1460                 :       1704 :                 treq->rsp.status.sc = SPDK_NVME_SC_COMMAND_TRANSIENT_TRANSPORT_ERROR;
    1461                 :            :         }
    1462                 :     392842 : }
    1463                 :            : 
    1464                 :            : static bool
    1465                 :     603572 : nvme_tcp_accel_recv_compute_crc32(struct nvme_tcp_req *treq, struct nvme_tcp_pdu *pdu)
    1466                 :            : {
    1467                 :     603572 :         struct nvme_tcp_qpair *tqpair = treq->tqpair;
    1468                 :     603572 :         struct nvme_tcp_poll_group *tgroup = nvme_tcp_poll_group(tqpair->qpair.poll_group);
    1469                 :     603572 :         struct nvme_request *req = treq->req;
    1470                 :     603572 :         int rc, dummy = 0;
    1471                 :            : 
    1472                 :            :         /* Only support this limited case that the request has only one c2h pdu */
    1473   [ +  -  +  +  :     603572 :         if (spdk_unlikely(nvme_qpair_get_state(&tqpair->qpair) < NVME_QPAIR_CONNECTED ||
          +  +  -  +  +  
          +  -  +  +  +  
                   +  + ]
    1474                 :            :                           tqpair->qpair.poll_group == NULL || pdu->dif_ctx != NULL ||
    1475                 :            :                           pdu->data_len % SPDK_NVME_TCP_DIGEST_ALIGNMENT != 0 ||
    1476                 :            :                           pdu->data_len != req->payload_size)) {
    1477                 :     191069 :                 return false;
    1478                 :            :         }
    1479                 :            : 
    1480         [ +  + ]:     412503 :         if (tgroup->group.group->accel_fn_table.append_crc32c != NULL) {
    1481                 :     392842 :                 nvme_tcp_req_copy_pdu(treq, pdu);
    1482                 :     785684 :                 rc = nvme_tcp_accel_append_crc32c(tgroup, &req->accel_sequence,
    1483                 :     392842 :                                                   &treq->pdu->data_digest_crc32,
    1484                 :     785684 :                                                   treq->pdu->data_iov, treq->pdu->data_iovcnt, 0,
    1485                 :            :                                                   nvme_tcp_accel_seq_recv_compute_crc32_done, treq);
    1486         [ -  + ]:     392842 :                 if (spdk_unlikely(rc != 0)) {
    1487                 :            :                         /* If accel is out of resources, fall back to non-accelerated crc32 */
    1488         [ #  # ]:          0 :                         if (rc == -ENOMEM) {
    1489                 :          0 :                                 return false;
    1490                 :            :                         }
    1491                 :            : 
    1492                 :          0 :                         SPDK_ERRLOG("Failed to append crc32c operation: %d\n", rc);
    1493                 :          0 :                         treq->rsp.status.sc = SPDK_NVME_SC_COMMAND_TRANSIENT_TRANSPORT_ERROR;
    1494                 :            :                 }
    1495                 :            : 
    1496                 :     392842 :                 nvme_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_READY);
    1497                 :     392842 :                 nvme_tcp_c2h_data_payload_handle(tqpair, treq->pdu, &dummy);
    1498                 :     392842 :                 return true;
    1499         [ -  + ]:      19661 :         } else if (tgroup->group.group->accel_fn_table.submit_accel_crc32c != NULL) {
    1500                 :          0 :                 nvme_tcp_req_copy_pdu(treq, pdu);
    1501                 :          0 :                 nvme_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_READY);
    1502                 :          0 :                 nvme_tcp_accel_submit_crc32c(tgroup, treq, &treq->pdu->data_digest_crc32,
    1503                 :          0 :                                              treq->pdu->data_iov, treq->pdu->data_iovcnt, 0,
    1504                 :            :                                              nvme_tcp_accel_recv_compute_crc32_done, treq);
    1505                 :          0 :                 return true;
    1506                 :            :         }
    1507                 :            : 
    1508                 :      19661 :         return false;
    1509                 :            : }
    1510                 :            : 
    1511                 :            : static void
    1512                 :    8232600 : nvme_tcp_pdu_payload_handle(struct nvme_tcp_qpair *tqpair,
    1513                 :            :                             uint32_t *reaped)
    1514                 :            : {
    1515                 :    8232600 :         int rc = 0;
    1516                 :    8232600 :         struct nvme_tcp_pdu *pdu = tqpair->recv_pdu;
    1517                 :            :         uint32_t crc32c;
    1518                 :    8232600 :         struct nvme_tcp_req *tcp_req = pdu->req;
    1519                 :            : 
    1520         [ -  + ]:    8232600 :         assert(tqpair->recv_state == NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_PAYLOAD);
    1521   [ -  +  +  + ]:    8232600 :         SPDK_DEBUGLOG(nvme, "enter\n");
    1522                 :            : 
    1523                 :            :         /* The request can be NULL, e.g. in case of C2HTermReq */
    1524         [ +  - ]:    8232600 :         if (spdk_likely(tcp_req != NULL)) {
    1525                 :    8232600 :                 tcp_req->expected_datao += pdu->data_len;
    1526                 :            :         }
    1527                 :            : 
    1528                 :            :         /* check data digest if need */
    1529   [ -  +  +  + ]:    8232600 :         if (pdu->ddgst_enable) {
    1530                 :            :                 /* But if the data digest is enabled, tcp_req cannot be NULL */
    1531         [ -  + ]:     603572 :                 assert(tcp_req != NULL);
    1532         [ +  + ]:     603572 :                 if (nvme_tcp_accel_recv_compute_crc32(tcp_req, pdu)) {
    1533                 :     392842 :                         return;
    1534                 :            :                 }
    1535                 :            : 
    1536                 :     210730 :                 crc32c = nvme_tcp_pdu_calc_data_digest(pdu);
    1537                 :     210730 :                 crc32c = crc32c ^ SPDK_CRC32C_XOR;
    1538                 :     210730 :                 rc = MATCH_DIGEST_WORD(pdu->data_digest, crc32c);
    1539         [ -  + ]:     210730 :                 if (rc == 0) {
    1540                 :          0 :                         SPDK_ERRLOG("data digest error on tqpair=(%p) with pdu=%p\n", tqpair, pdu);
    1541                 :          0 :                         tcp_req = pdu->req;
    1542         [ #  # ]:          0 :                         assert(tcp_req != NULL);
    1543                 :          0 :                         tcp_req->rsp.status.sc = SPDK_NVME_SC_COMMAND_TRANSIENT_TRANSPORT_ERROR;
    1544                 :            :                 }
    1545                 :            :         }
    1546                 :            : 
    1547                 :    7839758 :         _nvme_tcp_pdu_payload_handle(tqpair, reaped);
    1548                 :            : }
    1549                 :            : 
    1550                 :            : static void
    1551                 :       4219 : nvme_tcp_send_icreq_complete(void *cb_arg)
    1552                 :            : {
    1553                 :       4219 :         struct nvme_tcp_qpair *tqpair = cb_arg;
    1554                 :            : 
    1555   [ -  +  +  + ]:       4219 :         SPDK_DEBUGLOG(nvme, "Complete the icreq send for tqpair=%p %u\n", tqpair, tqpair->qpair.id);
    1556                 :            : 
    1557                 :       4219 :         tqpair->flags.icreq_send_ack = true;
    1558                 :            : 
    1559         [ -  + ]:       4219 :         if (tqpair->state == NVME_TCP_QPAIR_STATE_INITIALIZING) {
    1560   [ #  #  #  # ]:          0 :                 SPDK_DEBUGLOG(nvme, "tqpair %p %u, finalize icresp\n", tqpair, tqpair->qpair.id);
    1561                 :          0 :                 tqpair->state = NVME_TCP_QPAIR_STATE_FABRIC_CONNECT_SEND;
    1562                 :            :         }
    1563                 :       4219 : }
    1564                 :            : 
    1565                 :            : static void
    1566                 :       4252 : nvme_tcp_icresp_handle(struct nvme_tcp_qpair *tqpair,
    1567                 :            :                        struct nvme_tcp_pdu *pdu)
    1568                 :            : {
    1569                 :       4252 :         struct spdk_nvme_tcp_ic_resp *ic_resp = &pdu->hdr.ic_resp;
    1570                 :       4252 :         uint32_t error_offset = 0;
    1571                 :            :         enum spdk_nvme_tcp_term_req_fes fes;
    1572                 :            :         int recv_buf_size;
    1573                 :            : 
    1574                 :            :         /* Only PFV 0 is defined currently */
    1575         [ +  + ]:       4252 :         if (ic_resp->pfv != 0) {
    1576                 :          6 :                 SPDK_ERRLOG("Expected ICResp PFV %u, got %u\n", 0u, ic_resp->pfv);
    1577                 :          6 :                 fes = SPDK_NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD;
    1578                 :          6 :                 error_offset = offsetof(struct spdk_nvme_tcp_ic_resp, pfv);
    1579                 :          6 :                 goto end;
    1580                 :            :         }
    1581                 :            : 
    1582         [ +  + ]:       4246 :         if (ic_resp->maxh2cdata < NVME_TCP_PDU_H2C_MIN_DATA_SIZE) {
    1583                 :          6 :                 SPDK_ERRLOG("Expected ICResp maxh2cdata >=%u, got %u\n", NVME_TCP_PDU_H2C_MIN_DATA_SIZE,
    1584                 :            :                             ic_resp->maxh2cdata);
    1585                 :          6 :                 fes = SPDK_NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD;
    1586                 :          6 :                 error_offset = offsetof(struct spdk_nvme_tcp_ic_resp, maxh2cdata);
    1587                 :          6 :                 goto end;
    1588                 :            :         }
    1589                 :       4240 :         tqpair->maxh2cdata = ic_resp->maxh2cdata;
    1590                 :            : 
    1591         [ +  + ]:       4240 :         if (ic_resp->cpda > SPDK_NVME_TCP_CPDA_MAX) {
    1592                 :          6 :                 SPDK_ERRLOG("Expected ICResp cpda <=%u, got %u\n", SPDK_NVME_TCP_CPDA_MAX, ic_resp->cpda);
    1593                 :          6 :                 fes = SPDK_NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD;
    1594                 :          6 :                 error_offset = offsetof(struct spdk_nvme_tcp_ic_resp, cpda);
    1595                 :          6 :                 goto end;
    1596                 :            :         }
    1597                 :       4234 :         tqpair->cpda = ic_resp->cpda;
    1598                 :            : 
    1599                 :       4234 :         tqpair->flags.host_hdgst_enable = ic_resp->dgst.bits.hdgst_enable ? true : false;
    1600                 :       4234 :         tqpair->flags.host_ddgst_enable = ic_resp->dgst.bits.ddgst_enable ? true : false;
    1601   [ -  +  +  + ]:       4234 :         SPDK_DEBUGLOG(nvme, "host_hdgst_enable: %u\n", tqpair->flags.host_hdgst_enable);
    1602   [ -  +  +  + ]:       4234 :         SPDK_DEBUGLOG(nvme, "host_ddgst_enable: %u\n", tqpair->flags.host_ddgst_enable);
    1603                 :            : 
    1604                 :            :         /* Now that we know whether digests are enabled, properly size the receive buffer to
    1605                 :            :          * handle several incoming 4K read commands according to SPDK_NVMF_TCP_RECV_BUF_SIZE_FACTOR
    1606                 :            :          * parameter. */
    1607                 :       4234 :         recv_buf_size = 0x1000 + sizeof(struct spdk_nvme_tcp_c2h_data_hdr);
    1608                 :            : 
    1609         [ +  + ]:       4234 :         if (tqpair->flags.host_hdgst_enable) {
    1610                 :         36 :                 recv_buf_size += SPDK_NVME_TCP_DIGEST_LEN;
    1611                 :            :         }
    1612                 :            : 
    1613         [ +  + ]:       4234 :         if (tqpair->flags.host_ddgst_enable) {
    1614                 :        112 :                 recv_buf_size += SPDK_NVME_TCP_DIGEST_LEN;
    1615                 :            :         }
    1616                 :            : 
    1617         [ -  + ]:       4234 :         if (spdk_sock_set_recvbuf(tqpair->sock, recv_buf_size * SPDK_NVMF_TCP_RECV_BUF_SIZE_FACTOR) < 0) {
    1618                 :          0 :                 SPDK_WARNLOG("Unable to allocate enough memory for receive buffer on tqpair=%p with size=%d\n",
    1619                 :            :                              tqpair,
    1620                 :            :                              recv_buf_size);
    1621                 :            :                 /* Not fatal. */
    1622                 :            :         }
    1623                 :            : 
    1624                 :       4234 :         nvme_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_READY);
    1625                 :            : 
    1626         [ +  + ]:       4234 :         if (!tqpair->flags.icreq_send_ack) {
    1627                 :          6 :                 tqpair->state = NVME_TCP_QPAIR_STATE_INITIALIZING;
    1628   [ -  +  -  + ]:          6 :                 SPDK_DEBUGLOG(nvme, "tqpair %p %u, waiting icreq ack\n", tqpair, tqpair->qpair.id);
    1629                 :          6 :                 return;
    1630                 :            :         }
    1631                 :            : 
    1632                 :       4228 :         tqpair->state = NVME_TCP_QPAIR_STATE_FABRIC_CONNECT_SEND;
    1633                 :       4228 :         return;
    1634                 :         18 : end:
    1635                 :         18 :         nvme_tcp_qpair_send_h2c_term_req(tqpair, pdu, fes, error_offset);
    1636                 :            : }
    1637                 :            : 
    1638                 :            : static void
    1639                 :   16485629 : nvme_tcp_capsule_resp_hdr_handle(struct nvme_tcp_qpair *tqpair, struct nvme_tcp_pdu *pdu,
    1640                 :            :                                  uint32_t *reaped)
    1641                 :            : {
    1642                 :            :         struct nvme_tcp_req *tcp_req;
    1643                 :            :         struct nvme_tcp_poll_group *tgroup;
    1644                 :   16485629 :         struct spdk_nvme_tcp_rsp *capsule_resp = &pdu->hdr.capsule_resp;
    1645                 :   16485629 :         uint32_t cid, error_offset = 0;
    1646                 :            :         enum spdk_nvme_tcp_term_req_fes fes;
    1647                 :            : 
    1648   [ -  +  +  + ]:   16485629 :         SPDK_DEBUGLOG(nvme, "enter\n");
    1649                 :   16485629 :         cid = capsule_resp->rccqe.cid;
    1650                 :   16485629 :         tcp_req = get_nvme_active_req_by_cid(tqpair, cid);
    1651                 :            : 
    1652         [ +  + ]:   16485629 :         if (!tcp_req) {
    1653                 :          6 :                 SPDK_ERRLOG("no tcp_req is found with cid=%u for tqpair=%p\n", cid, tqpair);
    1654                 :          6 :                 fes = SPDK_NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD;
    1655                 :          6 :                 error_offset = offsetof(struct spdk_nvme_tcp_rsp, rccqe);
    1656                 :          6 :                 goto end;
    1657                 :            :         }
    1658                 :            : 
    1659         [ -  + ]:   16485623 :         assert(tcp_req->req != NULL);
    1660                 :            : 
    1661                 :   16485623 :         tcp_req->rsp = capsule_resp->rccqe;
    1662                 :   16485623 :         tcp_req->ordering.bits.data_recv = 1;
    1663                 :            : 
    1664                 :            :         /* Recv the pdu again */
    1665                 :   16485623 :         nvme_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_READY);
    1666                 :            : 
    1667         [ +  + ]:   16485623 :         if (tcp_req->req->accel_sequence != NULL) {
    1668                 :     319459 :                 tgroup = nvme_tcp_poll_group(tqpair->qpair.poll_group);
    1669                 :     319459 :                 nvme_tcp_accel_reverse_sequence(tgroup, tcp_req->req->accel_sequence);
    1670                 :     319459 :                 nvme_tcp_accel_finish_sequence(tgroup, tcp_req, tcp_req->req->accel_sequence,
    1671                 :            :                                                nvme_tcp_recv_payload_seq_cb, tcp_req);
    1672                 :     319459 :                 return;
    1673                 :            :         }
    1674                 :            : 
    1675         [ +  - ]:   16166164 :         if (nvme_tcp_req_complete_safe(tcp_req)) {
    1676                 :   16166164 :                 (*reaped)++;
    1677                 :            :         }
    1678                 :            : 
    1679                 :   16166164 :         return;
    1680                 :            : 
    1681                 :          6 : end:
    1682                 :          6 :         nvme_tcp_qpair_send_h2c_term_req(tqpair, pdu, fes, error_offset);
    1683                 :            : }
    1684                 :            : 
    1685                 :            : static void
    1686                 :          0 : nvme_tcp_c2h_term_req_hdr_handle(struct nvme_tcp_qpair *tqpair,
    1687                 :            :                                  struct nvme_tcp_pdu *pdu)
    1688                 :            : {
    1689                 :          0 :         struct spdk_nvme_tcp_term_req_hdr *c2h_term_req = &pdu->hdr.term_req;
    1690                 :          0 :         uint32_t error_offset = 0;
    1691                 :            :         enum spdk_nvme_tcp_term_req_fes fes;
    1692                 :            : 
    1693         [ #  # ]:          0 :         if (c2h_term_req->fes > SPDK_NVME_TCP_TERM_REQ_FES_INVALID_DATA_UNSUPPORTED_PARAMETER) {
    1694                 :          0 :                 SPDK_ERRLOG("Fatal Error Status(FES) is unknown for c2h_term_req pdu=%p\n", pdu);
    1695                 :          0 :                 fes = SPDK_NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD;
    1696                 :          0 :                 error_offset = offsetof(struct spdk_nvme_tcp_term_req_hdr, fes);
    1697                 :          0 :                 goto end;
    1698                 :            :         }
    1699                 :            : 
    1700                 :            :         /* set the data buffer */
    1701                 :          0 :         nvme_tcp_pdu_set_data(pdu, (uint8_t *)pdu->hdr.raw + c2h_term_req->common.hlen,
    1702                 :          0 :                               c2h_term_req->common.plen - c2h_term_req->common.hlen);
    1703                 :          0 :         nvme_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_PAYLOAD);
    1704                 :          0 :         return;
    1705                 :          0 : end:
    1706                 :          0 :         nvme_tcp_qpair_send_h2c_term_req(tqpair, pdu, fes, error_offset);
    1707                 :            : }
    1708                 :            : 
    1709                 :            : static void
    1710                 :    8232597 : nvme_tcp_c2h_data_hdr_handle(struct nvme_tcp_qpair *tqpair, struct nvme_tcp_pdu *pdu)
    1711                 :            : {
    1712                 :            :         struct nvme_tcp_req *tcp_req;
    1713                 :    8232597 :         struct spdk_nvme_tcp_c2h_data_hdr *c2h_data = &pdu->hdr.c2h_data;
    1714                 :    8232597 :         uint32_t error_offset = 0;
    1715                 :            :         enum spdk_nvme_tcp_term_req_fes fes;
    1716                 :    8232597 :         int flags = c2h_data->common.flags;
    1717                 :            :         int rc;
    1718                 :            : 
    1719   [ -  +  +  + ]:    8232597 :         SPDK_DEBUGLOG(nvme, "enter\n");
    1720   [ -  +  +  + ]:    8232597 :         SPDK_DEBUGLOG(nvme, "c2h_data info on tqpair(%p): datao=%u, datal=%u, cccid=%d\n",
    1721                 :            :                       tqpair, c2h_data->datao, c2h_data->datal, c2h_data->cccid);
    1722                 :    8232597 :         tcp_req = get_nvme_active_req_by_cid(tqpair, c2h_data->cccid);
    1723         [ -  + ]:    8232597 :         if (!tcp_req) {
    1724                 :          0 :                 SPDK_ERRLOG("no tcp_req found for c2hdata cid=%d\n", c2h_data->cccid);
    1725                 :          0 :                 fes = SPDK_NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD;
    1726                 :          0 :                 error_offset = offsetof(struct spdk_nvme_tcp_c2h_data_hdr, cccid);
    1727                 :          0 :                 goto end;
    1728                 :            : 
    1729                 :            :         }
    1730                 :            : 
    1731   [ -  +  +  + ]:    8232597 :         SPDK_DEBUGLOG(nvme, "tcp_req(%p) on tqpair(%p): expected_datao=%u, payload_size=%u\n",
    1732                 :            :                       tcp_req, tqpair, tcp_req->expected_datao, tcp_req->req->payload_size);
    1733                 :            : 
    1734   [ +  +  -  + ]:    8232597 :         if (spdk_unlikely((flags & SPDK_NVME_TCP_C2H_DATA_FLAGS_SUCCESS) &&
    1735                 :            :                           !(flags & SPDK_NVME_TCP_C2H_DATA_FLAGS_LAST_PDU))) {
    1736                 :          0 :                 SPDK_ERRLOG("Invalid flag flags=%d in c2h_data=%p\n", flags, c2h_data);
    1737                 :          0 :                 fes = SPDK_NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD;
    1738                 :          0 :                 error_offset = offsetof(struct spdk_nvme_tcp_c2h_data_hdr, common);
    1739                 :          0 :                 goto end;
    1740                 :            :         }
    1741                 :            : 
    1742         [ -  + ]:    8232597 :         if (c2h_data->datal > tcp_req->req->payload_size) {
    1743                 :          0 :                 SPDK_ERRLOG("Invalid datal for tcp_req(%p), datal(%u) exceeds payload_size(%u)\n",
    1744                 :            :                             tcp_req, c2h_data->datal, tcp_req->req->payload_size);
    1745                 :          0 :                 fes = SPDK_NVME_TCP_TERM_REQ_FES_DATA_TRANSFER_OUT_OF_RANGE;
    1746                 :          0 :                 goto end;
    1747                 :            :         }
    1748                 :            : 
    1749         [ -  + ]:    8232597 :         if (tcp_req->expected_datao != c2h_data->datao) {
    1750                 :          0 :                 SPDK_ERRLOG("Invalid datao for tcp_req(%p), received datal(%u) != expected datao(%u) in tcp_req\n",
    1751                 :            :                             tcp_req, c2h_data->datao, tcp_req->expected_datao);
    1752                 :          0 :                 fes = SPDK_NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD;
    1753                 :          0 :                 error_offset = offsetof(struct spdk_nvme_tcp_c2h_data_hdr, datao);
    1754                 :          0 :                 goto end;
    1755                 :            :         }
    1756                 :            : 
    1757         [ -  + ]:    8232597 :         if ((c2h_data->datao + c2h_data->datal) > tcp_req->req->payload_size) {
    1758                 :          0 :                 SPDK_ERRLOG("Invalid data range for tcp_req(%p), received (datao(%u) + datal(%u)) > datao(%u) in tcp_req\n",
    1759                 :            :                             tcp_req, c2h_data->datao, c2h_data->datal, tcp_req->req->payload_size);
    1760                 :          0 :                 fes = SPDK_NVME_TCP_TERM_REQ_FES_DATA_TRANSFER_OUT_OF_RANGE;
    1761                 :          0 :                 error_offset = offsetof(struct spdk_nvme_tcp_c2h_data_hdr, datal);
    1762                 :          0 :                 goto end;
    1763                 :            : 
    1764                 :            :         }
    1765                 :            : 
    1766         [ +  + ]:    8232597 :         if (nvme_payload_type(&tcp_req->req->payload) == NVME_PAYLOAD_TYPE_CONTIG) {
    1767                 :    8028536 :                 rc = nvme_tcp_build_contig_request(tqpair, tcp_req);
    1768                 :            :         } else {
    1769         [ -  + ]:     204061 :                 assert(nvme_payload_type(&tcp_req->req->payload) == NVME_PAYLOAD_TYPE_SGL);
    1770                 :     204061 :                 rc = nvme_tcp_build_sgl_request(tqpair, tcp_req);
    1771                 :            :         }
    1772                 :            : 
    1773         [ -  + ]:    8232597 :         if (rc) {
    1774                 :            :                 /* Not the right error message but at least it handles the failure. */
    1775                 :          0 :                 fes = SPDK_NVME_TCP_TERM_REQ_FES_DATA_TRANSFER_LIMIT_EXCEEDED;
    1776                 :          0 :                 goto end;
    1777                 :            :         }
    1778                 :            : 
    1779                 :    8232597 :         nvme_tcp_pdu_set_data_buf(pdu, tcp_req->iov, tcp_req->iovcnt,
    1780                 :            :                                   c2h_data->datao, c2h_data->datal);
    1781                 :    8232597 :         pdu->req = tcp_req;
    1782                 :            : 
    1783                 :    8232597 :         nvme_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_PAYLOAD);
    1784                 :    8232597 :         return;
    1785                 :            : 
    1786                 :          0 : end:
    1787                 :          0 :         nvme_tcp_qpair_send_h2c_term_req(tqpair, pdu, fes, error_offset);
    1788                 :            : }
    1789                 :            : 
    1790                 :            : static void
    1791                 :     685498 : nvme_tcp_qpair_h2c_data_send_complete(void *cb_arg)
    1792                 :            : {
    1793                 :     685498 :         struct nvme_tcp_req *tcp_req = cb_arg;
    1794                 :            : 
    1795         [ -  + ]:     685498 :         assert(tcp_req != NULL);
    1796                 :            : 
    1797                 :     685498 :         tcp_req->ordering.bits.send_ack = 1;
    1798         [ -  + ]:     685498 :         if (tcp_req->r2tl_remain) {
    1799                 :          0 :                 nvme_tcp_send_h2c_data(tcp_req);
    1800                 :            :         } else {
    1801         [ -  + ]:     685498 :                 assert(tcp_req->active_r2ts > 0);
    1802                 :     685498 :                 tcp_req->active_r2ts--;
    1803                 :     685498 :                 tcp_req->state = NVME_TCP_REQ_ACTIVE;
    1804                 :            : 
    1805         [ -  + ]:     685498 :                 if (tcp_req->ordering.bits.r2t_waiting_h2c_complete) {
    1806                 :          0 :                         tcp_req->ordering.bits.r2t_waiting_h2c_complete = 0;
    1807   [ #  #  #  # ]:          0 :                         SPDK_DEBUGLOG(nvme, "tcp_req %p: continue r2t\n", tcp_req);
    1808         [ #  # ]:          0 :                         assert(tcp_req->active_r2ts > 0);
    1809                 :          0 :                         tcp_req->ttag = tcp_req->ttag_r2t_next;
    1810                 :          0 :                         tcp_req->r2tl_remain = tcp_req->r2tl_remain_next;
    1811                 :          0 :                         tcp_req->state = NVME_TCP_REQ_ACTIVE_R2T;
    1812                 :          0 :                         nvme_tcp_send_h2c_data(tcp_req);
    1813                 :          0 :                         return;
    1814                 :            :                 }
    1815                 :            : 
    1816         [ +  + ]:     685498 :                 if (tcp_req->ordering.bits.domain_in_use) {
    1817                 :      23920 :                         spdk_memory_domain_invalidate_data(tcp_req->req->payload.opts->memory_domain,
    1818                 :      11960 :                                                            tcp_req->req->payload.opts->memory_domain_ctx, tcp_req->iov, tcp_req->iovcnt);
    1819                 :            :                 }
    1820                 :            : 
    1821                 :            :                 /* Need also call this function to free the resource */
    1822                 :     685498 :                 nvme_tcp_req_complete_safe(tcp_req);
    1823                 :            :         }
    1824                 :            : }
    1825                 :            : 
    1826                 :            : static void
    1827                 :     685498 : nvme_tcp_send_h2c_data(struct nvme_tcp_req *tcp_req)
    1828                 :            : {
    1829                 :     685498 :         struct nvme_tcp_qpair *tqpair = nvme_tcp_qpair(tcp_req->req->qpair);
    1830                 :            :         struct nvme_tcp_pdu *rsp_pdu;
    1831                 :            :         struct spdk_nvme_tcp_h2c_data_hdr *h2c_data;
    1832                 :            :         uint32_t plen, pdo, alignment;
    1833                 :            : 
    1834                 :            :         /* Reinit the send_ack and h2c_send_waiting_ack bits */
    1835                 :     685498 :         tcp_req->ordering.bits.send_ack = 0;
    1836                 :     685498 :         tcp_req->ordering.bits.h2c_send_waiting_ack = 0;
    1837                 :     685498 :         rsp_pdu = tcp_req->pdu;
    1838         [ -  + ]:     685498 :         memset(rsp_pdu, 0, sizeof(*rsp_pdu));
    1839                 :     685498 :         rsp_pdu->req = tcp_req;
    1840                 :     685498 :         h2c_data = &rsp_pdu->hdr.h2c_data;
    1841                 :            : 
    1842                 :     685498 :         h2c_data->common.pdu_type = SPDK_NVME_TCP_PDU_TYPE_H2C_DATA;
    1843                 :     685498 :         plen = h2c_data->common.hlen = sizeof(*h2c_data);
    1844                 :     685498 :         h2c_data->cccid = tcp_req->cid;
    1845                 :     685498 :         h2c_data->ttag = tcp_req->ttag;
    1846                 :     685498 :         h2c_data->datao = tcp_req->datao;
    1847                 :            : 
    1848                 :     685498 :         h2c_data->datal = spdk_min(tcp_req->r2tl_remain, tqpair->maxh2cdata);
    1849                 :     685498 :         nvme_tcp_pdu_set_data_buf(rsp_pdu, tcp_req->iov, tcp_req->iovcnt,
    1850                 :            :                                   h2c_data->datao, h2c_data->datal);
    1851                 :     685498 :         tcp_req->r2tl_remain -= h2c_data->datal;
    1852                 :            : 
    1853         [ -  + ]:     685498 :         if (tqpair->flags.host_hdgst_enable) {
    1854                 :          0 :                 h2c_data->common.flags |= SPDK_NVME_TCP_CH_FLAGS_HDGSTF;
    1855                 :          0 :                 plen += SPDK_NVME_TCP_DIGEST_LEN;
    1856                 :            :         }
    1857                 :            : 
    1858                 :     685498 :         rsp_pdu->padding_len = 0;
    1859                 :     685498 :         pdo = plen;
    1860         [ -  + ]:     685498 :         if (tqpair->cpda) {
    1861         [ #  # ]:          0 :                 alignment = (tqpair->cpda + 1) << 2;
    1862         [ #  # ]:          0 :                 if (alignment > plen) {
    1863                 :          0 :                         rsp_pdu->padding_len = alignment - plen;
    1864                 :          0 :                         pdo = plen = alignment;
    1865                 :            :                 }
    1866                 :            :         }
    1867                 :            : 
    1868                 :     685498 :         h2c_data->common.pdo = pdo;
    1869                 :     685498 :         plen += h2c_data->datal;
    1870         [ +  + ]:     685498 :         if (tqpair->flags.host_ddgst_enable) {
    1871                 :      84857 :                 h2c_data->common.flags |= SPDK_NVME_TCP_CH_FLAGS_DDGSTF;
    1872                 :      84857 :                 plen += SPDK_NVME_TCP_DIGEST_LEN;
    1873                 :            :         }
    1874                 :            : 
    1875                 :     685498 :         h2c_data->common.plen = plen;
    1876                 :     685498 :         tcp_req->datao += h2c_data->datal;
    1877         [ +  - ]:     685498 :         if (!tcp_req->r2tl_remain) {
    1878                 :     685498 :                 h2c_data->common.flags |= SPDK_NVME_TCP_H2C_DATA_FLAGS_LAST_PDU;
    1879                 :            :         }
    1880                 :            : 
    1881   [ -  +  -  + ]:     685498 :         SPDK_DEBUGLOG(nvme, "h2c_data info: datao=%u, datal=%u, pdu_len=%u for tqpair=%p\n",
    1882                 :            :                       h2c_data->datao, h2c_data->datal, h2c_data->common.plen, tqpair);
    1883                 :            : 
    1884                 :     685498 :         nvme_tcp_qpair_write_pdu(tqpair, rsp_pdu, nvme_tcp_qpair_h2c_data_send_complete, tcp_req);
    1885                 :     685498 : }
    1886                 :            : 
    1887                 :            : static void
    1888                 :     685498 : nvme_tcp_r2t_hdr_handle(struct nvme_tcp_qpair *tqpair, struct nvme_tcp_pdu *pdu)
    1889                 :            : {
    1890                 :            :         struct nvme_tcp_req *tcp_req;
    1891                 :     685498 :         struct spdk_nvme_tcp_r2t_hdr *r2t = &pdu->hdr.r2t;
    1892                 :     685498 :         uint32_t cid, error_offset = 0;
    1893                 :            :         enum spdk_nvme_tcp_term_req_fes fes;
    1894                 :            : 
    1895   [ -  +  -  + ]:     685498 :         SPDK_DEBUGLOG(nvme, "enter\n");
    1896                 :     685498 :         cid = r2t->cccid;
    1897                 :     685498 :         tcp_req = get_nvme_active_req_by_cid(tqpair, cid);
    1898         [ -  + ]:     685498 :         if (!tcp_req) {
    1899                 :          0 :                 SPDK_ERRLOG("Cannot find tcp_req for tqpair=%p\n", tqpair);
    1900                 :          0 :                 fes = SPDK_NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD;
    1901                 :          0 :                 error_offset = offsetof(struct spdk_nvme_tcp_r2t_hdr, cccid);
    1902                 :          0 :                 goto end;
    1903                 :            :         }
    1904                 :            : 
    1905   [ -  +  -  + ]:     685498 :         SPDK_DEBUGLOG(nvme, "r2t info: r2to=%u, r2tl=%u for tqpair=%p\n", r2t->r2to, r2t->r2tl,
    1906                 :            :                       tqpair);
    1907                 :            : 
    1908         [ +  - ]:     685498 :         if (tcp_req->state == NVME_TCP_REQ_ACTIVE) {
    1909         [ -  + ]:     685498 :                 assert(tcp_req->active_r2ts == 0);
    1910                 :     685498 :                 tcp_req->state = NVME_TCP_REQ_ACTIVE_R2T;
    1911                 :            :         }
    1912                 :            : 
    1913         [ -  + ]:     685498 :         if (tcp_req->datao != r2t->r2to) {
    1914                 :          0 :                 fes = SPDK_NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD;
    1915                 :          0 :                 error_offset = offsetof(struct spdk_nvme_tcp_r2t_hdr, r2to);
    1916                 :          0 :                 goto end;
    1917                 :            : 
    1918                 :            :         }
    1919                 :            : 
    1920         [ -  + ]:     685498 :         if ((r2t->r2tl + r2t->r2to) > tcp_req->req->payload_size) {
    1921                 :          0 :                 SPDK_ERRLOG("Invalid R2T info for tcp_req=%p: (r2to(%u) + r2tl(%u)) exceeds payload_size(%u)\n",
    1922                 :            :                             tcp_req, r2t->r2to, r2t->r2tl, tqpair->maxh2cdata);
    1923                 :          0 :                 fes = SPDK_NVME_TCP_TERM_REQ_FES_DATA_TRANSFER_OUT_OF_RANGE;
    1924                 :          0 :                 error_offset = offsetof(struct spdk_nvme_tcp_r2t_hdr, r2tl);
    1925                 :          0 :                 goto end;
    1926                 :            :         }
    1927                 :            : 
    1928                 :     685498 :         tcp_req->active_r2ts++;
    1929         [ -  + ]:     685498 :         if (spdk_unlikely(tcp_req->active_r2ts > tqpair->maxr2t)) {
    1930   [ #  #  #  # ]:          0 :                 if (tcp_req->state == NVME_TCP_REQ_ACTIVE_R2T && !tcp_req->ordering.bits.send_ack) {
    1931                 :            :                         /* We receive a subsequent R2T while we are waiting for H2C transfer to complete */
    1932   [ #  #  #  # ]:          0 :                         SPDK_DEBUGLOG(nvme, "received a subsequent R2T\n");
    1933         [ #  # ]:          0 :                         assert(tcp_req->active_r2ts == tqpair->maxr2t + 1);
    1934                 :          0 :                         tcp_req->ttag_r2t_next = r2t->ttag;
    1935                 :          0 :                         tcp_req->r2tl_remain_next = r2t->r2tl;
    1936                 :          0 :                         tcp_req->ordering.bits.r2t_waiting_h2c_complete = 1;
    1937                 :          0 :                         nvme_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_READY);
    1938                 :          0 :                         return;
    1939                 :            :                 } else {
    1940                 :          0 :                         fes = SPDK_NVME_TCP_TERM_REQ_FES_R2T_LIMIT_EXCEEDED;
    1941                 :          0 :                         SPDK_ERRLOG("Invalid R2T: Maximum number of R2T exceeded! Max: %u for tqpair=%p\n", tqpair->maxr2t,
    1942                 :            :                                     tqpair);
    1943                 :          0 :                         goto end;
    1944                 :            :                 }
    1945                 :            :         }
    1946                 :            : 
    1947                 :     685498 :         tcp_req->ttag = r2t->ttag;
    1948                 :     685498 :         tcp_req->r2tl_remain = r2t->r2tl;
    1949                 :     685498 :         nvme_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_READY);
    1950                 :            : 
    1951         [ +  - ]:     685498 :         if (spdk_likely(tcp_req->ordering.bits.send_ack)) {
    1952                 :     685498 :                 nvme_tcp_send_h2c_data(tcp_req);
    1953                 :            :         } else {
    1954                 :          0 :                 tcp_req->ordering.bits.h2c_send_waiting_ack = 1;
    1955                 :            :         }
    1956                 :            : 
    1957                 :     685498 :         return;
    1958                 :            : 
    1959                 :          0 : end:
    1960                 :          0 :         nvme_tcp_qpair_send_h2c_term_req(tqpair, pdu, fes, error_offset);
    1961                 :            : 
    1962                 :            : }
    1963                 :            : 
    1964                 :            : static void
    1965                 :   25407934 : nvme_tcp_pdu_psh_handle(struct nvme_tcp_qpair *tqpair, uint32_t *reaped)
    1966                 :            : {
    1967                 :            :         struct nvme_tcp_pdu *pdu;
    1968                 :            :         int rc;
    1969                 :   25407934 :         uint32_t crc32c, error_offset = 0;
    1970                 :            :         enum spdk_nvme_tcp_term_req_fes fes;
    1971                 :            : 
    1972         [ -  + ]:   25407934 :         assert(tqpair->recv_state == NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_PSH);
    1973                 :   25407934 :         pdu = tqpair->recv_pdu;
    1974                 :            : 
    1975   [ -  +  +  + ]:   25407934 :         SPDK_DEBUGLOG(nvme, "enter: pdu type =%u\n", pdu->hdr.common.pdu_type);
    1976                 :            :         /* check header digest if needed */
    1977   [ -  +  +  + ]:   25407934 :         if (pdu->has_hdgst) {
    1978                 :     272877 :                 crc32c = nvme_tcp_pdu_calc_header_digest(pdu);
    1979                 :     272877 :                 rc = MATCH_DIGEST_WORD((uint8_t *)pdu->hdr.raw + pdu->hdr.common.hlen, crc32c);
    1980         [ -  + ]:     272877 :                 if (rc == 0) {
    1981                 :          0 :                         SPDK_ERRLOG("header digest error on tqpair=(%p) with pdu=%p\n", tqpair, pdu);
    1982                 :          0 :                         fes = SPDK_NVME_TCP_TERM_REQ_FES_HDGST_ERROR;
    1983                 :          0 :                         nvme_tcp_qpair_send_h2c_term_req(tqpair, pdu, fes, error_offset);
    1984                 :          0 :                         return;
    1985                 :            : 
    1986                 :            :                 }
    1987                 :            :         }
    1988                 :            : 
    1989   [ +  +  +  -  :   25407934 :         switch (pdu->hdr.common.pdu_type) {
                   +  - ]
    1990                 :       4222 :         case SPDK_NVME_TCP_PDU_TYPE_IC_RESP:
    1991                 :       4222 :                 nvme_tcp_icresp_handle(tqpair, pdu);
    1992                 :       4222 :                 break;
    1993                 :   16485617 :         case SPDK_NVME_TCP_PDU_TYPE_CAPSULE_RESP:
    1994                 :   16485617 :                 nvme_tcp_capsule_resp_hdr_handle(tqpair, pdu, reaped);
    1995                 :   16485617 :                 break;
    1996                 :    8232597 :         case SPDK_NVME_TCP_PDU_TYPE_C2H_DATA:
    1997                 :    8232597 :                 nvme_tcp_c2h_data_hdr_handle(tqpair, pdu);
    1998                 :    8232597 :                 break;
    1999                 :            : 
    2000                 :          0 :         case SPDK_NVME_TCP_PDU_TYPE_C2H_TERM_REQ:
    2001                 :          0 :                 nvme_tcp_c2h_term_req_hdr_handle(tqpair, pdu);
    2002                 :          0 :                 break;
    2003                 :     685498 :         case SPDK_NVME_TCP_PDU_TYPE_R2T:
    2004                 :     685498 :                 nvme_tcp_r2t_hdr_handle(tqpair, pdu);
    2005                 :     685498 :                 break;
    2006                 :            : 
    2007                 :          0 :         default:
    2008                 :          0 :                 SPDK_ERRLOG("Unexpected PDU type 0x%02x\n", tqpair->recv_pdu->hdr.common.pdu_type);
    2009                 :          0 :                 fes = SPDK_NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD;
    2010                 :          0 :                 error_offset = 1;
    2011                 :          0 :                 nvme_tcp_qpair_send_h2c_term_req(tqpair, pdu, fes, error_offset);
    2012                 :          0 :                 break;
    2013                 :            :         }
    2014                 :            : 
    2015                 :            : }
    2016                 :            : 
    2017                 :            : static int
    2018                 :   67796506 : nvme_tcp_read_pdu(struct nvme_tcp_qpair *tqpair, uint32_t *reaped, uint32_t max_completions)
    2019                 :            : {
    2020                 :   67796506 :         int rc = 0;
    2021                 :            :         struct nvme_tcp_pdu *pdu;
    2022                 :            :         uint32_t data_len;
    2023                 :            :         enum nvme_tcp_pdu_recv_state prev_state;
    2024                 :            : 
    2025                 :   67796506 :         *reaped = tqpair->async_complete;
    2026                 :   67796506 :         tqpair->async_complete = 0;
    2027                 :            : 
    2028                 :            :         /* The loop here is to allow for several back-to-back state changes. */
    2029                 :            :         do {
    2030         [ +  + ]:  152257203 :                 if (*reaped >= max_completions) {
    2031                 :     370247 :                         break;
    2032                 :            :                 }
    2033                 :            : 
    2034                 :  151886956 :                 prev_state = tqpair->recv_state;
    2035                 :  151886956 :                 pdu = tqpair->recv_pdu;
    2036   [ +  +  +  +  :  151886956 :                 switch (tqpair->recv_state) {
                +  +  - ]
    2037                 :            :                 /* If in a new state */
    2038                 :   25412132 :                 case NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_READY:
    2039         [ -  + ]:   25412132 :                         memset(pdu, 0, sizeof(struct nvme_tcp_pdu));
    2040                 :   25412132 :                         nvme_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_CH);
    2041                 :   25412132 :                         break;
    2042                 :            :                 /* Wait for the pdu common header */
    2043                 :   81954960 :                 case NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_CH:
    2044         [ -  + ]:   81954960 :                         assert(pdu->ch_valid_bytes < sizeof(struct spdk_nvme_tcp_common_pdu_hdr));
    2045                 :   81954960 :                         rc = nvme_tcp_read_data(tqpair->sock,
    2046                 :   81954960 :                                                 sizeof(struct spdk_nvme_tcp_common_pdu_hdr) - pdu->ch_valid_bytes,
    2047                 :   81954960 :                                                 (uint8_t *)&pdu->hdr.common + pdu->ch_valid_bytes);
    2048         [ +  + ]:   81954960 :                         if (rc < 0) {
    2049                 :         74 :                                 nvme_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_QUIESCING);
    2050                 :         74 :                                 break;
    2051                 :            :                         }
    2052                 :   81954886 :                         pdu->ch_valid_bytes += rc;
    2053         [ +  + ]:   81954886 :                         if (pdu->ch_valid_bytes < sizeof(struct spdk_nvme_tcp_common_pdu_hdr)) {
    2054                 :   56546946 :                                 return NVME_TCP_PDU_IN_PROGRESS;
    2055                 :            :                         }
    2056                 :            : 
    2057                 :            :                         /* The command header of this PDU has now been read from the socket. */
    2058                 :   25407940 :                         nvme_tcp_pdu_ch_handle(tqpair);
    2059                 :   25407940 :                         break;
    2060                 :            :                 /* Wait for the pdu specific header  */
    2061                 :   25408382 :                 case NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_PSH:
    2062         [ -  + ]:   25408382 :                         assert(pdu->psh_valid_bytes < pdu->psh_len);
    2063                 :   25408382 :                         rc = nvme_tcp_read_data(tqpair->sock,
    2064                 :   25408382 :                                                 pdu->psh_len - pdu->psh_valid_bytes,
    2065                 :   25408382 :                                                 (uint8_t *)&pdu->hdr.raw + sizeof(struct spdk_nvme_tcp_common_pdu_hdr) + pdu->psh_valid_bytes);
    2066         [ -  + ]:   25408382 :                         if (rc < 0) {
    2067                 :          0 :                                 nvme_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_QUIESCING);
    2068                 :          0 :                                 break;
    2069                 :            :                         }
    2070                 :            : 
    2071                 :   25408382 :                         pdu->psh_valid_bytes += rc;
    2072         [ +  + ]:   25408382 :                         if (pdu->psh_valid_bytes < pdu->psh_len) {
    2073                 :        448 :                                 return NVME_TCP_PDU_IN_PROGRESS;
    2074                 :            :                         }
    2075                 :            : 
    2076                 :            :                         /* All header(ch, psh, head digist) of this PDU has now been read from the socket. */
    2077                 :   25407934 :                         nvme_tcp_pdu_psh_handle(tqpair, reaped);
    2078                 :   25407934 :                         break;
    2079                 :   19111337 :                 case NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_PAYLOAD:
    2080                 :            :                         /* check whether the data is valid, if not we just return */
    2081         [ -  + ]:   19111337 :                         if (!pdu->data_len) {
    2082                 :          0 :                                 return NVME_TCP_PDU_IN_PROGRESS;
    2083                 :            :                         }
    2084                 :            : 
    2085                 :   19111337 :                         data_len = pdu->data_len;
    2086                 :            :                         /* data digest */
    2087   [ +  -  +  + ]:   19111337 :                         if (spdk_unlikely((pdu->hdr.common.pdu_type == SPDK_NVME_TCP_PDU_TYPE_C2H_DATA) &&
    2088                 :            :                                           tqpair->flags.host_ddgst_enable)) {
    2089                 :    1261596 :                                 data_len += SPDK_NVME_TCP_DIGEST_LEN;
    2090                 :    1261596 :                                 pdu->ddgst_enable = true;
    2091                 :            :                         }
    2092                 :            : 
    2093                 :   19111337 :                         rc = nvme_tcp_read_payload_data(tqpair->sock, pdu);
    2094         [ +  + ]:   19111337 :                         if (rc < 0) {
    2095                 :          4 :                                 nvme_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_QUIESCING);
    2096                 :          4 :                                 break;
    2097                 :            :                         }
    2098                 :            : 
    2099                 :   19111333 :                         pdu->rw_offset += rc;
    2100         [ +  + ]:   19111333 :                         if (pdu->rw_offset < data_len) {
    2101                 :   10878745 :                                 return NVME_TCP_PDU_IN_PROGRESS;
    2102                 :            :                         }
    2103                 :            : 
    2104         [ -  + ]:    8232588 :                         assert(pdu->rw_offset == data_len);
    2105                 :            :                         /* All of this PDU has now been read from the socket. */
    2106                 :    8232588 :                         nvme_tcp_pdu_payload_handle(tqpair, reaped);
    2107                 :    8232588 :                         break;
    2108                 :        120 :                 case NVME_TCP_PDU_RECV_STATE_QUIESCING:
    2109         [ +  + ]:        120 :                         if (TAILQ_EMPTY(&tqpair->outstanding_reqs)) {
    2110         [ +  + ]:         25 :                                 if (nvme_qpair_get_state(&tqpair->qpair) == NVME_QPAIR_DISCONNECTING) {
    2111                 :          6 :                                         nvme_transport_ctrlr_disconnect_qpair_done(&tqpair->qpair);
    2112                 :            :                                 }
    2113                 :            : 
    2114                 :         25 :                                 nvme_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_ERROR);
    2115                 :            :                         }
    2116                 :        120 :                         break;
    2117                 :         25 :                 case NVME_TCP_PDU_RECV_STATE_ERROR:
    2118         [ -  + ]:         25 :                         memset(pdu, 0, sizeof(struct nvme_tcp_pdu));
    2119                 :         25 :                         return NVME_TCP_PDU_FATAL;
    2120                 :          0 :                 default:
    2121                 :          0 :                         assert(0);
    2122                 :            :                         break;
    2123                 :            :                 }
    2124         [ +  + ]:   84460792 :         } while (prev_state != tqpair->recv_state);
    2125                 :            : 
    2126                 :     370342 :         return rc > 0 ? 0 : rc;
    2127                 :            : }
    2128                 :            : 
    2129                 :            : static void
    2130                 :          0 : nvme_tcp_qpair_check_timeout(struct spdk_nvme_qpair *qpair)
    2131                 :            : {
    2132                 :            :         uint64_t t02;
    2133                 :            :         struct nvme_tcp_req *tcp_req, *tmp;
    2134                 :          0 :         struct nvme_tcp_qpair *tqpair = nvme_tcp_qpair(qpair);
    2135                 :          0 :         struct spdk_nvme_ctrlr *ctrlr = qpair->ctrlr;
    2136                 :            :         struct spdk_nvme_ctrlr_process *active_proc;
    2137                 :            : 
    2138                 :            :         /* Don't check timeouts during controller initialization. */
    2139         [ #  # ]:          0 :         if (ctrlr->state != NVME_CTRLR_STATE_READY) {
    2140                 :          0 :                 return;
    2141                 :            :         }
    2142                 :            : 
    2143         [ #  # ]:          0 :         if (nvme_qpair_is_admin_queue(qpair)) {
    2144                 :          0 :                 active_proc = nvme_ctrlr_get_current_process(ctrlr);
    2145                 :            :         } else {
    2146                 :          0 :                 active_proc = qpair->active_proc;
    2147                 :            :         }
    2148                 :            : 
    2149                 :            :         /* Only check timeouts if the current process has a timeout callback. */
    2150   [ #  #  #  # ]:          0 :         if (active_proc == NULL || active_proc->timeout_cb_fn == NULL) {
    2151                 :          0 :                 return;
    2152                 :            :         }
    2153                 :            : 
    2154                 :          0 :         t02 = spdk_get_ticks();
    2155         [ #  # ]:          0 :         TAILQ_FOREACH_SAFE(tcp_req, &tqpair->outstanding_reqs, link, tmp) {
    2156   [ #  #  #  # ]:          0 :                 if (ctrlr->is_failed) {
    2157                 :            :                         /* The controller state may be changed to failed in one of the nvme_request_check_timeout callbacks. */
    2158                 :          0 :                         return;
    2159                 :            :                 }
    2160         [ #  # ]:          0 :                 assert(tcp_req->req != NULL);
    2161                 :            : 
    2162         [ #  # ]:          0 :                 if (nvme_request_check_timeout(tcp_req->req, tcp_req->cid, active_proc, t02)) {
    2163                 :            :                         /*
    2164                 :            :                          * The requests are in order, so as soon as one has not timed out,
    2165                 :            :                          * stop iterating.
    2166                 :            :                          */
    2167                 :          0 :                         break;
    2168                 :            :                 }
    2169                 :            :         }
    2170                 :            : }
    2171                 :            : 
    2172                 :            : static int nvme_tcp_ctrlr_connect_qpair_poll(struct spdk_nvme_ctrlr *ctrlr,
    2173                 :            :                 struct spdk_nvme_qpair *qpair);
    2174                 :            : 
    2175                 :            : static int
    2176                 :   67796974 : nvme_tcp_qpair_process_completions(struct spdk_nvme_qpair *qpair, uint32_t max_completions)
    2177                 :            : {
    2178                 :   67796974 :         struct nvme_tcp_qpair *tqpair = nvme_tcp_qpair(qpair);
    2179                 :     535349 :         uint32_t reaped;
    2180                 :            :         int rc;
    2181                 :            : 
    2182         [ +  + ]:   67796974 :         if (qpair->poll_group == NULL) {
    2183                 :   32963516 :                 rc = spdk_sock_flush(tqpair->sock);
    2184   [ +  +  +  + ]:   32963516 :                 if (rc < 0 && errno != EAGAIN) {
    2185                 :        468 :                         SPDK_ERRLOG("Failed to flush tqpair=%p (%d): %s\n", tqpair,
    2186                 :            :                                     errno, spdk_strerror(errno));
    2187   [ -  +  -  + ]:        468 :                         if (spdk_unlikely(tqpair->qpair.ctrlr->timeout_enabled)) {
    2188                 :          0 :                                 nvme_tcp_qpair_check_timeout(qpair);
    2189                 :            :                         }
    2190                 :            : 
    2191         [ +  + ]:        468 :                         if (nvme_qpair_get_state(qpair) == NVME_QPAIR_DISCONNECTING) {
    2192         [ +  - ]:        447 :                                 if (TAILQ_EMPTY(&tqpair->outstanding_reqs)) {
    2193                 :        447 :                                         nvme_transport_ctrlr_disconnect_qpair_done(qpair);
    2194                 :            :                                 }
    2195                 :            : 
    2196                 :            :                                 /* Don't return errors until the qpair gets disconnected */
    2197                 :        447 :                                 return 0;
    2198                 :            :                         }
    2199                 :            : 
    2200                 :         21 :                         goto fail;
    2201                 :            :                 }
    2202                 :            :         }
    2203                 :            : 
    2204         [ +  + ]:   67796506 :         if (max_completions == 0) {
    2205                 :   62680502 :                 max_completions = spdk_max(tqpair->num_entries, 1);
    2206                 :            :         } else {
    2207                 :    5116004 :                 max_completions = spdk_min(max_completions, tqpair->num_entries);
    2208                 :            :         }
    2209                 :            : 
    2210                 :   67796506 :         reaped = 0;
    2211                 :   67796506 :         rc = nvme_tcp_read_pdu(tqpair, &reaped, max_completions);
    2212         [ +  + ]:   67796506 :         if (rc < 0) {
    2213   [ -  +  -  + ]:         84 :                 SPDK_DEBUGLOG(nvme, "Error polling CQ! (%d): %s\n",
    2214                 :            :                               errno, spdk_strerror(errno));
    2215                 :         84 :                 goto fail;
    2216                 :            :         }
    2217                 :            : 
    2218   [ -  +  -  + ]:   67796422 :         if (spdk_unlikely(tqpair->qpair.ctrlr->timeout_enabled)) {
    2219                 :          0 :                 nvme_tcp_qpair_check_timeout(qpair);
    2220                 :            :         }
    2221                 :            : 
    2222         [ +  + ]:   67796422 :         if (spdk_unlikely(nvme_qpair_get_state(qpair) == NVME_QPAIR_CONNECTING)) {
    2223                 :    9423623 :                 rc = nvme_tcp_ctrlr_connect_qpair_poll(qpair->ctrlr, qpair);
    2224   [ +  +  +  + ]:    9423623 :                 if (rc != 0 && rc != -EAGAIN) {
    2225                 :        609 :                         SPDK_ERRLOG("Failed to connect tqpair=%p\n", tqpair);
    2226                 :        609 :                         goto fail;
    2227         [ +  + ]:    9423014 :                 } else if (rc == 0) {
    2228                 :            :                         /* Once the connection is completed, we can submit queued requests */
    2229                 :       3601 :                         nvme_qpair_resubmit_requests(qpair, tqpair->num_entries);
    2230                 :            :                 }
    2231                 :            :         }
    2232                 :            : 
    2233                 :   67795813 :         return reaped;
    2234                 :        714 : fail:
    2235                 :            : 
    2236                 :            :         /*
    2237                 :            :          * Since admin queues take the ctrlr_lock before entering this function,
    2238                 :            :          * we can call nvme_transport_ctrlr_disconnect_qpair. For other qpairs we need
    2239                 :            :          * to call the generic function which will take the lock for us.
    2240                 :            :          */
    2241                 :        714 :         qpair->transport_failure_reason = SPDK_NVME_QPAIR_FAILURE_UNKNOWN;
    2242                 :            : 
    2243         [ +  + ]:        714 :         if (nvme_qpair_is_admin_queue(qpair)) {
    2244                 :         90 :                 enum nvme_qpair_state state_prev = nvme_qpair_get_state(qpair);
    2245                 :            : 
    2246                 :         90 :                 nvme_transport_ctrlr_disconnect_qpair(qpair->ctrlr, qpair);
    2247                 :            : 
    2248   [ +  +  +  + ]:         90 :                 if (state_prev == NVME_QPAIR_CONNECTING && qpair->poll_status != NULL) {
    2249                 :            :                         /* Needed to free the poll_status */
    2250                 :         16 :                         nvme_tcp_ctrlr_connect_qpair_poll(qpair->ctrlr, qpair);
    2251                 :            :                 }
    2252                 :            :         } else {
    2253                 :        624 :                 nvme_ctrlr_disconnect_qpair(qpair);
    2254                 :            :         }
    2255                 :        714 :         return -ENXIO;
    2256                 :            : }
    2257                 :            : 
    2258                 :            : static void
    2259                 :   34833458 : nvme_tcp_qpair_sock_cb(void *ctx, struct spdk_sock_group *group, struct spdk_sock *sock)
    2260                 :            : {
    2261                 :   34833458 :         struct spdk_nvme_qpair *qpair = ctx;
    2262                 :   34833458 :         struct nvme_tcp_poll_group *pgroup = nvme_tcp_poll_group(qpair->poll_group);
    2263                 :            :         int32_t num_completions;
    2264                 :   34833458 :         struct nvme_tcp_qpair *tqpair = nvme_tcp_qpair(qpair);
    2265                 :            : 
    2266   [ -  +  +  + ]:   34833458 :         if (tqpair->needs_poll) {
    2267         [ +  + ]:    1264373 :                 TAILQ_REMOVE(&pgroup->needs_poll, tqpair, link);
    2268                 :    1264373 :                 tqpair->needs_poll = false;
    2269                 :            :         }
    2270                 :            : 
    2271                 :   34833458 :         num_completions = spdk_nvme_qpair_process_completions(qpair, pgroup->completions_per_qpair);
    2272                 :            : 
    2273   [ +  -  +  - ]:   34833458 :         if (pgroup->num_completions >= 0 && num_completions >= 0) {
    2274                 :   34833458 :                 pgroup->num_completions += num_completions;
    2275                 :   34833458 :                 pgroup->stats.nvme_completions += num_completions;
    2276                 :            :         } else {
    2277                 :          0 :                 pgroup->num_completions = -ENXIO;
    2278                 :            :         }
    2279                 :   34833458 : }
    2280                 :            : 
    2281                 :            : static int
    2282                 :       4246 : nvme_tcp_qpair_icreq_send(struct nvme_tcp_qpair *tqpair)
    2283                 :            : {
    2284                 :            :         struct spdk_nvme_tcp_ic_req *ic_req;
    2285                 :            :         struct nvme_tcp_pdu *pdu;
    2286                 :            :         uint32_t timeout_in_sec;
    2287                 :            : 
    2288                 :       4246 :         pdu = tqpair->send_pdu;
    2289         [ -  + ]:       4246 :         memset(tqpair->send_pdu, 0, sizeof(*tqpair->send_pdu));
    2290                 :       4246 :         ic_req = &pdu->hdr.ic_req;
    2291                 :            : 
    2292                 :       4246 :         ic_req->common.pdu_type = SPDK_NVME_TCP_PDU_TYPE_IC_REQ;
    2293                 :       4246 :         ic_req->common.hlen = ic_req->common.plen = sizeof(*ic_req);
    2294                 :       4246 :         ic_req->pfv = 0;
    2295                 :       4246 :         ic_req->maxr2t = NVME_TCP_MAX_R2T_DEFAULT - 1;
    2296                 :       4246 :         ic_req->hpda = NVME_TCP_HPDA_DEFAULT;
    2297                 :            : 
    2298         [ -  + ]:       4246 :         ic_req->dgst.bits.hdgst_enable = tqpair->qpair.ctrlr->opts.header_digest;
    2299         [ -  + ]:       4246 :         ic_req->dgst.bits.ddgst_enable = tqpair->qpair.ctrlr->opts.data_digest;
    2300                 :            : 
    2301                 :       4246 :         nvme_tcp_qpair_write_pdu(tqpair, pdu, nvme_tcp_send_icreq_complete, tqpair);
    2302                 :            : 
    2303         [ +  + ]:       4246 :         timeout_in_sec = tqpair->qpair.async ? ICREQ_TIMEOUT_ASYNC : ICREQ_TIMEOUT_SYNC;
    2304                 :       4246 :         tqpair->icreq_timeout_tsc = spdk_get_ticks() + (timeout_in_sec * spdk_get_ticks_hz());
    2305                 :       4246 :         return 0;
    2306                 :            : }
    2307                 :            : 
    2308                 :            : static int
    2309                 :      14132 : nvme_tcp_qpair_connect_sock(struct spdk_nvme_ctrlr *ctrlr, struct spdk_nvme_qpair *qpair)
    2310                 :            : {
    2311                 :        153 :         struct sockaddr_storage dst_addr;
    2312                 :        153 :         struct sockaddr_storage src_addr;
    2313                 :            :         int rc;
    2314                 :            :         struct nvme_tcp_qpair *tqpair;
    2315                 :            :         int family;
    2316                 :        153 :         long int port, src_port;
    2317                 :            :         char *sock_impl_name;
    2318                 :      14132 :         struct spdk_sock_impl_opts impl_opts = {};
    2319                 :      14132 :         size_t impl_opts_size = sizeof(impl_opts);
    2320                 :        153 :         struct spdk_sock_opts opts;
    2321                 :            :         struct nvme_tcp_ctrlr *tcp_ctrlr;
    2322                 :            : 
    2323                 :      14132 :         tqpair = nvme_tcp_qpair(qpair);
    2324                 :            : 
    2325      [ +  -  + ]:      14132 :         switch (ctrlr->trid.adrfam) {
    2326                 :      14120 :         case SPDK_NVMF_ADRFAM_IPV4:
    2327                 :      14120 :                 family = AF_INET;
    2328                 :      14120 :                 break;
    2329                 :          0 :         case SPDK_NVMF_ADRFAM_IPV6:
    2330                 :          0 :                 family = AF_INET6;
    2331                 :          0 :                 break;
    2332                 :         12 :         default:
    2333                 :         12 :                 SPDK_ERRLOG("Unhandled ADRFAM %d\n", ctrlr->trid.adrfam);
    2334                 :         12 :                 rc = -1;
    2335                 :         12 :                 return rc;
    2336                 :            :         }
    2337                 :            : 
    2338   [ -  +  +  + ]:      14120 :         SPDK_DEBUGLOG(nvme, "adrfam %d ai_family %d\n", ctrlr->trid.adrfam, family);
    2339                 :            : 
    2340         [ -  + ]:      14120 :         memset(&dst_addr, 0, sizeof(dst_addr));
    2341                 :            : 
    2342   [ -  +  +  + ]:      14120 :         SPDK_DEBUGLOG(nvme, "trsvcid is %s\n", ctrlr->trid.trsvcid);
    2343                 :      14120 :         rc = nvme_parse_addr(&dst_addr, family, ctrlr->trid.traddr, ctrlr->trid.trsvcid, &port);
    2344         [ +  + ]:      14120 :         if (rc != 0) {
    2345                 :         12 :                 SPDK_ERRLOG("dst_addr nvme_parse_addr() failed\n");
    2346                 :         12 :                 return rc;
    2347                 :            :         }
    2348                 :            : 
    2349   [ +  +  -  + ]:      14108 :         if (ctrlr->opts.src_addr[0] || ctrlr->opts.src_svcid[0]) {
    2350         [ -  + ]:         44 :                 memset(&src_addr, 0, sizeof(src_addr));
    2351                 :         44 :                 rc = nvme_parse_addr(&src_addr, family, ctrlr->opts.src_addr, ctrlr->opts.src_svcid, &src_port);
    2352         [ -  + ]:         44 :                 if (rc != 0) {
    2353                 :          0 :                         SPDK_ERRLOG("src_addr nvme_parse_addr() failed\n");
    2354                 :          0 :                         return rc;
    2355                 :            :                 }
    2356                 :            :         }
    2357                 :            : 
    2358                 :      14108 :         tcp_ctrlr = SPDK_CONTAINEROF(ctrlr, struct nvme_tcp_ctrlr, ctrlr);
    2359         [ +  + ]:      14108 :         sock_impl_name = tcp_ctrlr->psk[0] ? "ssl" : NULL;
    2360   [ -  +  +  + ]:      14108 :         SPDK_DEBUGLOG(nvme, "sock_impl_name is %s\n", sock_impl_name);
    2361                 :            : 
    2362         [ +  + ]:      14108 :         if (sock_impl_name) {
    2363                 :        130 :                 spdk_sock_impl_get_opts(sock_impl_name, &impl_opts, &impl_opts_size);
    2364                 :        130 :                 impl_opts.tls_version = SPDK_TLS_VERSION_1_3;
    2365                 :        130 :                 impl_opts.psk_identity = tcp_ctrlr->psk_identity;
    2366                 :        130 :                 impl_opts.psk_key = tcp_ctrlr->psk;
    2367                 :        130 :                 impl_opts.psk_key_size = tcp_ctrlr->psk_size;
    2368                 :        130 :                 impl_opts.tls_cipher_suites = tcp_ctrlr->tls_cipher_suite;
    2369                 :            :         }
    2370                 :      14108 :         opts.opts_size = sizeof(opts);
    2371                 :      14108 :         spdk_sock_get_default_opts(&opts);
    2372                 :      14108 :         opts.priority = ctrlr->trid.priority;
    2373                 :      14108 :         opts.zcopy = !nvme_qpair_is_admin_queue(qpair);
    2374         [ +  + ]:      14108 :         if (ctrlr->opts.transport_ack_timeout) {
    2375         [ -  + ]:         45 :                 opts.ack_timeout = 1ULL << ctrlr->opts.transport_ack_timeout;
    2376                 :            :         }
    2377         [ +  + ]:      14108 :         if (sock_impl_name) {
    2378                 :        130 :                 opts.impl_opts = &impl_opts;
    2379                 :        130 :                 opts.impl_opts_size = sizeof(impl_opts);
    2380                 :            :         }
    2381                 :      14108 :         tqpair->sock = spdk_sock_connect_ext(ctrlr->trid.traddr, port, sock_impl_name, &opts);
    2382         [ +  + ]:      14108 :         if (!tqpair->sock) {
    2383                 :       9844 :                 SPDK_ERRLOG("sock connection error of tqpair=%p with addr=%s, port=%ld\n",
    2384                 :            :                             tqpair, ctrlr->trid.traddr, port);
    2385                 :       9844 :                 rc = -1;
    2386                 :       9844 :                 return rc;
    2387                 :            :         }
    2388                 :            : 
    2389                 :       4264 :         return 0;
    2390                 :            : }
    2391                 :            : 
    2392                 :            : static int
    2393                 :    9423639 : nvme_tcp_ctrlr_connect_qpair_poll(struct spdk_nvme_ctrlr *ctrlr, struct spdk_nvme_qpair *qpair)
    2394                 :            : {
    2395                 :            :         struct nvme_tcp_qpair *tqpair;
    2396                 :            :         int rc;
    2397                 :            : 
    2398                 :    9423639 :         tqpair = nvme_tcp_qpair(qpair);
    2399                 :            : 
    2400                 :            :         /* Prevent this function from being called recursively, as it could lead to issues with
    2401                 :            :          * nvme_fabric_qpair_connect_poll() if the connect response is received in the recursive
    2402                 :            :          * call.
    2403                 :            :          */
    2404         [ +  + ]:    9423639 :         if (tqpair->flags.in_connect_poll) {
    2405                 :     155343 :                 return -EAGAIN;
    2406                 :            :         }
    2407                 :            : 
    2408                 :    9268296 :         tqpair->flags.in_connect_poll = 1;
    2409                 :            : 
    2410   [ +  +  +  -  :    9268296 :         switch (tqpair->state) {
                      - ]
    2411                 :    9094834 :         case NVME_TCP_QPAIR_STATE_INVALID:
    2412                 :            :         case NVME_TCP_QPAIR_STATE_INITIALIZING:
    2413         [ -  + ]:    9094834 :                 if (spdk_get_ticks() > tqpair->icreq_timeout_tsc) {
    2414                 :          0 :                         SPDK_ERRLOG("Failed to construct the tqpair=%p via correct icresp\n", tqpair);
    2415                 :          0 :                         rc = -ETIMEDOUT;
    2416                 :          0 :                         break;
    2417                 :            :                 }
    2418                 :    9094834 :                 rc = -EAGAIN;
    2419                 :    9094834 :                 break;
    2420                 :       4222 :         case NVME_TCP_QPAIR_STATE_FABRIC_CONNECT_SEND:
    2421                 :       4222 :                 rc = nvme_fabric_qpair_connect_async(&tqpair->qpair, tqpair->num_entries + 1);
    2422         [ -  + ]:       4222 :                 if (rc < 0) {
    2423                 :          0 :                         SPDK_ERRLOG("Failed to send an NVMe-oF Fabric CONNECT command\n");
    2424                 :          0 :                         break;
    2425                 :            :                 }
    2426                 :       4222 :                 tqpair->state = NVME_TCP_QPAIR_STATE_FABRIC_CONNECT_POLL;
    2427                 :       4222 :                 rc = -EAGAIN;
    2428                 :       4222 :                 break;
    2429                 :     169240 :         case NVME_TCP_QPAIR_STATE_FABRIC_CONNECT_POLL:
    2430                 :     169240 :                 rc = nvme_fabric_qpair_connect_poll(&tqpair->qpair);
    2431         [ +  + ]:     169240 :                 if (rc == 0) {
    2432                 :       3601 :                         tqpair->state = NVME_TCP_QPAIR_STATE_RUNNING;
    2433                 :       3601 :                         nvme_qpair_set_state(qpair, NVME_QPAIR_CONNECTED);
    2434         [ +  + ]:     165639 :                 } else if (rc != -EAGAIN) {
    2435                 :        621 :                         SPDK_ERRLOG("Failed to poll NVMe-oF Fabric CONNECT command\n");
    2436                 :            :                 }
    2437                 :     169240 :                 break;
    2438                 :          0 :         case NVME_TCP_QPAIR_STATE_RUNNING:
    2439                 :          0 :                 rc = 0;
    2440                 :          0 :                 break;
    2441                 :          0 :         default:
    2442                 :          0 :                 assert(false);
    2443                 :            :                 rc = -EINVAL;
    2444                 :            :                 break;
    2445                 :            :         }
    2446                 :            : 
    2447                 :    9268296 :         tqpair->flags.in_connect_poll = 0;
    2448                 :    9268296 :         return rc;
    2449                 :            : }
    2450                 :            : 
    2451                 :            : static int
    2452                 :      14067 : nvme_tcp_ctrlr_connect_qpair(struct spdk_nvme_ctrlr *ctrlr, struct spdk_nvme_qpair *qpair)
    2453                 :            : {
    2454                 :      14067 :         int rc = 0;
    2455                 :            :         struct nvme_tcp_qpair *tqpair;
    2456                 :            :         struct nvme_tcp_poll_group *tgroup;
    2457                 :            : 
    2458                 :      14067 :         tqpair = nvme_tcp_qpair(qpair);
    2459                 :            : 
    2460         [ +  + ]:      14067 :         if (!tqpair->sock) {
    2461                 :      10452 :                 rc = nvme_tcp_qpair_connect_sock(ctrlr, qpair);
    2462         [ +  + ]:      10452 :                 if (rc < 0) {
    2463                 :       9827 :                         return rc;
    2464                 :            :                 }
    2465                 :            :         }
    2466                 :            : 
    2467         [ +  + ]:       4240 :         if (qpair->poll_group) {
    2468                 :        877 :                 rc = nvme_poll_group_connect_qpair(qpair);
    2469         [ -  + ]:        877 :                 if (rc) {
    2470                 :          0 :                         SPDK_ERRLOG("Unable to activate the tcp qpair.\n");
    2471                 :          0 :                         return rc;
    2472                 :            :                 }
    2473                 :        877 :                 tgroup = nvme_tcp_poll_group(qpair->poll_group);
    2474                 :        877 :                 tqpair->stats = &tgroup->stats;
    2475                 :        877 :                 tqpair->shared_stats = true;
    2476                 :            :         } else {
    2477                 :            :                 /* When resetting a controller, we disconnect adminq and then reconnect. The stats
    2478                 :            :                  * is not freed when disconnecting. So when reconnecting, don't allocate memory
    2479                 :            :                  * again.
    2480                 :            :                  */
    2481         [ +  + ]:       3363 :                 if (tqpair->stats == NULL) {
    2482                 :       2738 :                         tqpair->stats = calloc(1, sizeof(*tqpair->stats));
    2483         [ -  + ]:       2738 :                         if (!tqpair->stats) {
    2484                 :          0 :                                 SPDK_ERRLOG("tcp stats memory allocation failed\n");
    2485                 :          0 :                                 return -ENOMEM;
    2486                 :            :                         }
    2487                 :            :                 }
    2488                 :            :         }
    2489                 :            : 
    2490                 :       4240 :         tqpair->maxr2t = NVME_TCP_MAX_R2T_DEFAULT;
    2491                 :            :         /* Explicitly set the state and recv_state of tqpair */
    2492                 :       4240 :         tqpair->state = NVME_TCP_QPAIR_STATE_INVALID;
    2493         [ +  + ]:       4240 :         if (tqpair->recv_state != NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_READY) {
    2494                 :        625 :                 nvme_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_READY);
    2495                 :            :         }
    2496                 :       4240 :         rc = nvme_tcp_qpair_icreq_send(tqpair);
    2497         [ -  + ]:       4240 :         if (rc != 0) {
    2498                 :          0 :                 SPDK_ERRLOG("Unable to connect the tqpair\n");
    2499                 :          0 :                 return rc;
    2500                 :            :         }
    2501                 :            : 
    2502                 :       4240 :         return rc;
    2503                 :            : }
    2504                 :            : 
    2505                 :            : static struct spdk_nvme_qpair *
    2506                 :       3674 : nvme_tcp_ctrlr_create_qpair(struct spdk_nvme_ctrlr *ctrlr,
    2507                 :            :                             uint16_t qid, uint32_t qsize,
    2508                 :            :                             enum spdk_nvme_qprio qprio,
    2509                 :            :                             uint32_t num_requests, bool async)
    2510                 :            : {
    2511                 :            :         struct nvme_tcp_qpair *tqpair;
    2512                 :            :         struct spdk_nvme_qpair *qpair;
    2513                 :            :         int rc;
    2514                 :            : 
    2515         [ +  + ]:       3674 :         if (qsize < SPDK_NVME_QUEUE_MIN_ENTRIES) {
    2516                 :         18 :                 SPDK_ERRLOG("Failed to create qpair with size %u. Minimum queue size is %d.\n",
    2517                 :            :                             qsize, SPDK_NVME_QUEUE_MIN_ENTRIES);
    2518                 :         18 :                 return NULL;
    2519                 :            :         }
    2520                 :            : 
    2521                 :       3656 :         tqpair = calloc(1, sizeof(struct nvme_tcp_qpair));
    2522         [ -  + ]:       3656 :         if (!tqpair) {
    2523                 :          0 :                 SPDK_ERRLOG("failed to get create tqpair\n");
    2524                 :          0 :                 return NULL;
    2525                 :            :         }
    2526                 :            : 
    2527                 :            :         /* Set num_entries one less than queue size. According to NVMe
    2528                 :            :          * and NVMe-oF specs we can not submit queue size requests,
    2529                 :            :          * one slot shall always remain empty.
    2530                 :            :          */
    2531                 :       3656 :         tqpair->num_entries = qsize - 1;
    2532                 :       3656 :         qpair = &tqpair->qpair;
    2533                 :       3656 :         rc = nvme_qpair_init(qpair, qid, ctrlr, qprio, num_requests, async);
    2534         [ -  + ]:       3656 :         if (rc != 0) {
    2535                 :          0 :                 free(tqpair);
    2536                 :          0 :                 return NULL;
    2537                 :            :         }
    2538                 :            : 
    2539                 :       3656 :         rc = nvme_tcp_alloc_reqs(tqpair);
    2540         [ -  + ]:       3656 :         if (rc) {
    2541                 :          0 :                 nvme_tcp_ctrlr_delete_io_qpair(ctrlr, qpair);
    2542                 :          0 :                 return NULL;
    2543                 :            :         }
    2544                 :            : 
    2545                 :            :         /* spdk_nvme_qpair_get_optimal_poll_group needs socket information.
    2546                 :            :          * So create the socket first when creating a qpair. */
    2547                 :       3656 :         rc = nvme_tcp_qpair_connect_sock(ctrlr, qpair);
    2548         [ +  + ]:       3656 :         if (rc) {
    2549                 :         23 :                 nvme_tcp_ctrlr_delete_io_qpair(ctrlr, qpair);
    2550                 :         23 :                 return NULL;
    2551                 :            :         }
    2552                 :            : 
    2553                 :       3633 :         return qpair;
    2554                 :            : }
    2555                 :            : 
    2556                 :            : static struct spdk_nvme_qpair *
    2557                 :       2379 : nvme_tcp_ctrlr_create_io_qpair(struct spdk_nvme_ctrlr *ctrlr, uint16_t qid,
    2558                 :            :                                const struct spdk_nvme_io_qpair_opts *opts)
    2559                 :            : {
    2560                 :       2387 :         return nvme_tcp_ctrlr_create_qpair(ctrlr, qid, opts->io_queue_size, opts->qprio,
    2561         [ -  + ]:       2379 :                                            opts->io_queue_requests, opts->async_mode);
    2562                 :            : }
    2563                 :            : 
    2564         [ -  + ]:       2810 : SPDK_LOG_DEPRECATION_REGISTER(nvme_ctrlr_psk, "spdk_nvme_ctrlr_opts.psk", "v24.09", 0);
    2565                 :            : 
    2566                 :            : static int
    2567                 :         62 : nvme_tcp_generate_tls_credentials(struct nvme_tcp_ctrlr *tctrlr)
    2568                 :            : {
    2569                 :         62 :         struct spdk_nvme_ctrlr *ctrlr = &tctrlr->ctrlr;
    2570                 :            :         int rc;
    2571                 :         62 :         uint8_t psk_retained[SPDK_TLS_PSK_MAX_LEN] = {};
    2572                 :         62 :         uint8_t psk_configured[SPDK_TLS_PSK_MAX_LEN] = {};
    2573                 :         62 :         uint8_t pskbuf[SPDK_TLS_PSK_MAX_LEN + 1] = {};
    2574                 :            :         uint8_t tls_cipher_suite;
    2575                 :          0 :         uint8_t psk_retained_hash;
    2576                 :          0 :         uint64_t psk_configured_size;
    2577                 :            :         uint8_t *psk;
    2578                 :            : 
    2579         [ +  + ]:         62 :         if (ctrlr->opts.tls_psk != NULL) {
    2580                 :         33 :                 rc = spdk_key_get_key(ctrlr->opts.tls_psk, pskbuf, SPDK_TLS_PSK_MAX_LEN);
    2581         [ +  + ]:         33 :                 if (rc < 0) {
    2582                 :          3 :                         SPDK_ERRLOG("Failed to obtain key '%s': %s\n",
    2583                 :            :                                     spdk_key_get_name(ctrlr->opts.tls_psk), spdk_strerror(-rc));
    2584                 :          3 :                         goto finish;
    2585                 :            :                 }
    2586                 :            : 
    2587                 :         30 :                 psk = pskbuf;
    2588                 :            :         } else {
    2589                 :         29 :                 SPDK_LOG_DEPRECATED(nvme_ctrlr_psk);
    2590                 :         29 :                 psk = ctrlr->opts.psk;
    2591                 :            :         }
    2592                 :            : 
    2593                 :         59 :         rc = nvme_tcp_parse_interchange_psk(psk, psk_configured, sizeof(psk_configured),
    2594                 :            :                                             &psk_configured_size, &psk_retained_hash);
    2595         [ -  + ]:         59 :         if (rc < 0) {
    2596                 :          0 :                 SPDK_ERRLOG("Failed to parse PSK interchange!\n");
    2597                 :          0 :                 goto finish;
    2598                 :            :         }
    2599                 :            : 
    2600                 :            :         /* The Base64 string encodes the configured PSK (32 or 48 bytes binary).
    2601                 :            :          * This check also ensures that psk_configured_size is smaller than
    2602                 :            :          * psk_retained buffer size. */
    2603         [ +  + ]:         59 :         if (psk_configured_size == SHA256_DIGEST_LENGTH) {
    2604                 :         41 :                 tls_cipher_suite = NVME_TCP_CIPHER_AES_128_GCM_SHA256;
    2605                 :         41 :                 tctrlr->tls_cipher_suite = "TLS_AES_128_GCM_SHA256";
    2606         [ +  - ]:         18 :         } else if (psk_configured_size == SHA384_DIGEST_LENGTH) {
    2607                 :         18 :                 tls_cipher_suite = NVME_TCP_CIPHER_AES_256_GCM_SHA384;
    2608                 :         18 :                 tctrlr->tls_cipher_suite = "TLS_AES_256_GCM_SHA384";
    2609                 :            :         } else {
    2610                 :          0 :                 SPDK_ERRLOG("Unrecognized cipher suite!\n");
    2611                 :          0 :                 rc = -ENOTSUP;
    2612                 :          0 :                 goto finish;
    2613                 :            :         }
    2614                 :            : 
    2615                 :         59 :         rc = nvme_tcp_generate_psk_identity(tctrlr->psk_identity, sizeof(tctrlr->psk_identity),
    2616                 :         59 :                                             ctrlr->opts.hostnqn, ctrlr->trid.subnqn,
    2617                 :            :                                             tls_cipher_suite);
    2618         [ -  + ]:         59 :         if (rc) {
    2619                 :          0 :                 SPDK_ERRLOG("could not generate PSK identity\n");
    2620                 :          0 :                 goto finish;
    2621                 :            :         }
    2622                 :            : 
    2623                 :            :         /* No hash indicates that Configured PSK must be used as Retained PSK. */
    2624         [ +  + ]:         59 :         if (psk_retained_hash == NVME_TCP_HASH_ALGORITHM_NONE) {
    2625         [ -  + ]:         21 :                 assert(psk_configured_size < sizeof(psk_retained));
    2626   [ -  +  -  + ]:         21 :                 memcpy(psk_retained, psk_configured, psk_configured_size);
    2627                 :         21 :                 rc = psk_configured_size;
    2628                 :            :         } else {
    2629                 :            :                 /* Derive retained PSK. */
    2630                 :         38 :                 rc = nvme_tcp_derive_retained_psk(psk_configured, psk_configured_size, ctrlr->opts.hostnqn,
    2631                 :            :                                                   psk_retained, sizeof(psk_retained), psk_retained_hash);
    2632         [ -  + ]:         38 :                 if (rc < 0) {
    2633                 :          0 :                         SPDK_ERRLOG("Unable to derive retained PSK!\n");
    2634                 :          0 :                         goto finish;
    2635                 :            :                 }
    2636                 :            :         }
    2637                 :            : 
    2638                 :         59 :         rc = nvme_tcp_derive_tls_psk(psk_retained, rc, tctrlr->psk_identity, tctrlr->psk,
    2639                 :            :                                      sizeof(tctrlr->psk), tls_cipher_suite);
    2640         [ -  + ]:         59 :         if (rc < 0) {
    2641                 :          0 :                 SPDK_ERRLOG("Could not generate TLS PSK!\n");
    2642                 :          0 :                 goto finish;
    2643                 :            :         }
    2644                 :            : 
    2645                 :         59 :         tctrlr->psk_size = rc;
    2646                 :         59 :         rc = 0;
    2647                 :         62 : finish:
    2648                 :         62 :         spdk_memset_s(psk_configured, sizeof(psk_configured), 0, sizeof(psk_configured));
    2649                 :         62 :         spdk_memset_s(pskbuf, sizeof(pskbuf), 0, sizeof(pskbuf));
    2650                 :            : 
    2651                 :         62 :         return rc;
    2652                 :            : }
    2653                 :            : 
    2654                 :            : /* We have to use the typedef in the function declaration to appease astyle. */
    2655                 :            : typedef struct spdk_nvme_ctrlr spdk_nvme_ctrlr_t;
    2656                 :            : 
    2657                 :            : static spdk_nvme_ctrlr_t *
    2658                 :       1298 : nvme_tcp_ctrlr_construct(const struct spdk_nvme_transport_id *trid,
    2659                 :            :                          const struct spdk_nvme_ctrlr_opts *opts,
    2660                 :            :                          void *devhandle)
    2661                 :            : {
    2662                 :            :         struct nvme_tcp_ctrlr *tctrlr;
    2663                 :            :         int rc;
    2664                 :            : 
    2665                 :       1298 :         tctrlr = calloc(1, sizeof(*tctrlr));
    2666         [ -  + ]:       1298 :         if (tctrlr == NULL) {
    2667                 :          0 :                 SPDK_ERRLOG("could not allocate ctrlr\n");
    2668                 :          0 :                 return NULL;
    2669                 :            :         }
    2670                 :            : 
    2671                 :       1298 :         tctrlr->ctrlr.opts = *opts;
    2672                 :       1298 :         tctrlr->ctrlr.trid = *trid;
    2673                 :            : 
    2674   [ +  +  +  + ]:       1298 :         if (opts->psk[0] != '\0' || opts->tls_psk != NULL) {
    2675                 :            :                 /* Only allow either one at a time */
    2676   [ +  +  -  + ]:         62 :                 if (opts->tls_psk != NULL && opts->psk[0] != '\0') {
    2677                 :          0 :                         SPDK_ERRLOG("Either spdk_nvme_ctrlr_opts.tls_psk or .psk can be set at "
    2678                 :            :                                     "the same time\n");
    2679                 :          0 :                         free(tctrlr);
    2680                 :          0 :                         return NULL;
    2681                 :            :                 }
    2682                 :         62 :                 rc = nvme_tcp_generate_tls_credentials(tctrlr);
    2683                 :         62 :                 spdk_memset_s(&tctrlr->ctrlr.opts.psk, sizeof(tctrlr->ctrlr.opts.psk), 0,
    2684                 :            :                               sizeof(tctrlr->ctrlr.opts.psk));
    2685                 :            : 
    2686         [ +  + ]:         62 :                 if (rc != 0) {
    2687                 :          3 :                         free(tctrlr);
    2688                 :          3 :                         return NULL;
    2689                 :            :                 }
    2690                 :            :         }
    2691                 :            : 
    2692         [ +  + ]:       1295 :         if (opts->transport_ack_timeout > NVME_TCP_CTRLR_MAX_TRANSPORT_ACK_TIMEOUT) {
    2693                 :         30 :                 SPDK_NOTICELOG("transport_ack_timeout exceeds max value %d, use max value\n",
    2694                 :            :                                NVME_TCP_CTRLR_MAX_TRANSPORT_ACK_TIMEOUT);
    2695                 :         30 :                 tctrlr->ctrlr.opts.transport_ack_timeout = NVME_TCP_CTRLR_MAX_TRANSPORT_ACK_TIMEOUT;
    2696                 :            :         }
    2697                 :            : 
    2698                 :       1295 :         rc = nvme_ctrlr_construct(&tctrlr->ctrlr);
    2699         [ -  + ]:       1295 :         if (rc != 0) {
    2700                 :          0 :                 free(tctrlr);
    2701                 :          0 :                 return NULL;
    2702                 :            :         }
    2703                 :            : 
    2704                 :            :         /* Sequence might be used not only for data digest offload purposes but
    2705                 :            :          * to handle a potential COPY operation appended as the result of translation. */
    2706                 :       1295 :         tctrlr->ctrlr.flags |= SPDK_NVME_CTRLR_ACCEL_SEQUENCE_SUPPORTED;
    2707                 :       1305 :         tctrlr->ctrlr.adminq = nvme_tcp_ctrlr_create_qpair(&tctrlr->ctrlr, 0,
    2708                 :       1295 :                                tctrlr->ctrlr.opts.admin_queue_size, 0,
    2709                 :       1295 :                                tctrlr->ctrlr.opts.admin_queue_size, true);
    2710         [ +  + ]:       1295 :         if (!tctrlr->ctrlr.adminq) {
    2711                 :         29 :                 SPDK_ERRLOG("failed to create admin qpair\n");
    2712                 :         29 :                 nvme_tcp_ctrlr_destruct(&tctrlr->ctrlr);
    2713                 :         29 :                 return NULL;
    2714                 :            :         }
    2715                 :            : 
    2716         [ -  + ]:       1266 :         if (nvme_ctrlr_add_process(&tctrlr->ctrlr, 0) != 0) {
    2717                 :          0 :                 SPDK_ERRLOG("nvme_ctrlr_add_process() failed\n");
    2718                 :          0 :                 nvme_ctrlr_destruct(&tctrlr->ctrlr);
    2719                 :          0 :                 return NULL;
    2720                 :            :         }
    2721                 :            : 
    2722                 :       1266 :         return &tctrlr->ctrlr;
    2723                 :            : }
    2724                 :            : 
    2725                 :            : static uint32_t
    2726                 :       1240 : nvme_tcp_ctrlr_get_max_xfer_size(struct spdk_nvme_ctrlr *ctrlr)
    2727                 :            : {
    2728                 :            :         /* TCP transport doesn't limit maximum IO transfer size. */
    2729                 :       1240 :         return UINT32_MAX;
    2730                 :            : }
    2731                 :            : 
    2732                 :            : static uint16_t
    2733                 :       1240 : nvme_tcp_ctrlr_get_max_sges(struct spdk_nvme_ctrlr *ctrlr)
    2734                 :            : {
    2735                 :       1240 :         return NVME_TCP_MAX_SGL_DESCRIPTORS;
    2736                 :            : }
    2737                 :            : 
    2738                 :            : static int
    2739                 :     420690 : nvme_tcp_qpair_iterate_requests(struct spdk_nvme_qpair *qpair,
    2740                 :            :                                 int (*iter_fn)(struct nvme_request *req, void *arg),
    2741                 :            :                                 void *arg)
    2742                 :            : {
    2743                 :     420690 :         struct nvme_tcp_qpair *tqpair = nvme_tcp_qpair(qpair);
    2744                 :            :         struct nvme_tcp_req *tcp_req, *tmp;
    2745                 :            :         int rc;
    2746                 :            : 
    2747         [ -  + ]:     420690 :         assert(iter_fn != NULL);
    2748                 :            : 
    2749         [ +  + ]:   18818772 :         TAILQ_FOREACH_SAFE(tcp_req, &tqpair->outstanding_reqs, link, tmp) {
    2750         [ -  + ]:   18398123 :                 assert(tcp_req->req != NULL);
    2751                 :            : 
    2752                 :   18398123 :                 rc = iter_fn(tcp_req->req, arg);
    2753         [ +  + ]:   18398123 :                 if (rc != 0) {
    2754                 :         41 :                         return rc;
    2755                 :            :                 }
    2756                 :            :         }
    2757                 :            : 
    2758                 :     420649 :         return 0;
    2759                 :            : }
    2760                 :            : 
    2761                 :            : static void
    2762                 :       1630 : nvme_tcp_admin_qpair_abort_aers(struct spdk_nvme_qpair *qpair)
    2763                 :            : {
    2764                 :            :         struct nvme_tcp_req *tcp_req, *tmp;
    2765                 :       1630 :         struct spdk_nvme_cpl cpl = {};
    2766                 :       1630 :         struct nvme_tcp_qpair *tqpair = nvme_tcp_qpair(qpair);
    2767                 :            : 
    2768                 :       1630 :         cpl.status.sc = SPDK_NVME_SC_ABORTED_SQ_DELETION;
    2769                 :       1630 :         cpl.status.sct = SPDK_NVME_SCT_GENERIC;
    2770                 :            : 
    2771         [ +  + ]:       6413 :         TAILQ_FOREACH_SAFE(tcp_req, &tqpair->outstanding_reqs, link, tmp) {
    2772         [ -  + ]:       4783 :                 assert(tcp_req->req != NULL);
    2773         [ +  + ]:       4783 :                 if (tcp_req->req->cmd.opc != SPDK_NVME_OPC_ASYNC_EVENT_REQUEST) {
    2774                 :          1 :                         continue;
    2775                 :            :                 }
    2776                 :            : 
    2777                 :       4782 :                 nvme_tcp_req_complete(tcp_req, tqpair, &cpl, false);
    2778                 :            :         }
    2779                 :       1630 : }
    2780                 :            : 
    2781                 :            : static struct spdk_nvme_transport_poll_group *
    2782                 :        830 : nvme_tcp_poll_group_create(void)
    2783                 :            : {
    2784                 :        830 :         struct nvme_tcp_poll_group *group = calloc(1, sizeof(*group));
    2785                 :            : 
    2786         [ -  + ]:        830 :         if (group == NULL) {
    2787                 :          0 :                 SPDK_ERRLOG("Unable to allocate poll group.\n");
    2788                 :          0 :                 return NULL;
    2789                 :            :         }
    2790                 :            : 
    2791                 :        830 :         TAILQ_INIT(&group->needs_poll);
    2792                 :            : 
    2793                 :        830 :         group->sock_group = spdk_sock_group_create(group);
    2794         [ -  + ]:        830 :         if (group->sock_group == NULL) {
    2795                 :          0 :                 free(group);
    2796                 :          0 :                 SPDK_ERRLOG("Unable to allocate sock group.\n");
    2797                 :          0 :                 return NULL;
    2798                 :            :         }
    2799                 :            : 
    2800                 :        830 :         return &group->group;
    2801                 :            : }
    2802                 :            : 
    2803                 :            : static struct spdk_nvme_transport_poll_group *
    2804                 :          0 : nvme_tcp_qpair_get_optimal_poll_group(struct spdk_nvme_qpair *qpair)
    2805                 :            : {
    2806                 :          0 :         struct nvme_tcp_qpair *tqpair = nvme_tcp_qpair(qpair);
    2807                 :          0 :         struct spdk_sock_group *group = NULL;
    2808                 :            :         int rc;
    2809                 :            : 
    2810                 :          0 :         rc = spdk_sock_get_optimal_sock_group(tqpair->sock, &group, NULL);
    2811   [ #  #  #  # ]:          0 :         if (!rc && group != NULL) {
    2812                 :          0 :                 return spdk_sock_group_get_ctx(group);
    2813                 :            :         }
    2814                 :            : 
    2815                 :          0 :         return NULL;
    2816                 :            : }
    2817                 :            : 
    2818                 :            : static int
    2819                 :        877 : nvme_tcp_poll_group_connect_qpair(struct spdk_nvme_qpair *qpair)
    2820                 :            : {
    2821                 :        877 :         struct nvme_tcp_poll_group *group = nvme_tcp_poll_group(qpair->poll_group);
    2822                 :        877 :         struct nvme_tcp_qpair *tqpair = nvme_tcp_qpair(qpair);
    2823                 :            : 
    2824         [ -  + ]:        877 :         if (spdk_sock_group_add_sock(group->sock_group, tqpair->sock, nvme_tcp_qpair_sock_cb, qpair)) {
    2825                 :          0 :                 return -EPROTO;
    2826                 :            :         }
    2827                 :        877 :         return 0;
    2828                 :            : }
    2829                 :            : 
    2830                 :            : static int
    2831                 :        877 : nvme_tcp_poll_group_disconnect_qpair(struct spdk_nvme_qpair *qpair)
    2832                 :            : {
    2833                 :        877 :         struct nvme_tcp_poll_group *group = nvme_tcp_poll_group(qpair->poll_group);
    2834                 :        877 :         struct nvme_tcp_qpair *tqpair = nvme_tcp_qpair(qpair);
    2835                 :            : 
    2836   [ -  +  +  + ]:        877 :         if (tqpair->needs_poll) {
    2837         [ -  + ]:         12 :                 TAILQ_REMOVE(&group->needs_poll, tqpair, link);
    2838                 :         12 :                 tqpair->needs_poll = false;
    2839                 :            :         }
    2840                 :            : 
    2841   [ +  -  +  - ]:        877 :         if (tqpair->sock && group->sock_group) {
    2842         [ -  + ]:        877 :                 if (spdk_sock_group_remove_sock(group->sock_group, tqpair->sock)) {
    2843                 :          0 :                         return -EPROTO;
    2844                 :            :                 }
    2845                 :            :         }
    2846                 :        877 :         return 0;
    2847                 :            : }
    2848                 :            : 
    2849                 :            : static int
    2850                 :        877 : nvme_tcp_poll_group_add(struct spdk_nvme_transport_poll_group *tgroup,
    2851                 :            :                         struct spdk_nvme_qpair *qpair)
    2852                 :            : {
    2853                 :        877 :         struct nvme_tcp_qpair *tqpair = nvme_tcp_qpair(qpair);
    2854                 :        877 :         struct nvme_tcp_poll_group *group = nvme_tcp_poll_group(tgroup);
    2855                 :            : 
    2856                 :            :         /* disconnected qpairs won't have a sock to add. */
    2857         [ -  + ]:        877 :         if (nvme_qpair_get_state(qpair) >= NVME_QPAIR_CONNECTED) {
    2858         [ #  # ]:          0 :                 if (spdk_sock_group_add_sock(group->sock_group, tqpair->sock, nvme_tcp_qpair_sock_cb, qpair)) {
    2859                 :          0 :                         return -EPROTO;
    2860                 :            :                 }
    2861                 :            :         }
    2862                 :            : 
    2863                 :        877 :         return 0;
    2864                 :            : }
    2865                 :            : 
    2866                 :            : static int
    2867                 :        877 : nvme_tcp_poll_group_remove(struct spdk_nvme_transport_poll_group *tgroup,
    2868                 :            :                            struct spdk_nvme_qpair *qpair)
    2869                 :            : {
    2870                 :            :         struct nvme_tcp_qpair *tqpair;
    2871                 :            :         struct nvme_tcp_poll_group *group;
    2872                 :            : 
    2873         [ -  + ]:        877 :         assert(qpair->poll_group_tailq_head == &tgroup->disconnected_qpairs);
    2874                 :            : 
    2875                 :        877 :         tqpair = nvme_tcp_qpair(qpair);
    2876                 :        877 :         group = nvme_tcp_poll_group(tgroup);
    2877                 :            : 
    2878   [ -  +  -  + ]:        877 :         assert(tqpair->shared_stats == true);
    2879                 :        877 :         tqpair->stats = &g_dummy_stats;
    2880                 :            : 
    2881   [ -  +  +  + ]:        877 :         if (tqpair->needs_poll) {
    2882         [ -  + ]:          4 :                 TAILQ_REMOVE(&group->needs_poll, tqpair, link);
    2883                 :          4 :                 tqpair->needs_poll = false;
    2884                 :            :         }
    2885                 :            : 
    2886                 :        877 :         return 0;
    2887                 :            : }
    2888                 :            : 
    2889                 :            : static int64_t
    2890                 :  303190376 : nvme_tcp_poll_group_process_completions(struct spdk_nvme_transport_poll_group *tgroup,
    2891                 :            :                                         uint32_t completions_per_qpair, spdk_nvme_disconnected_qpair_cb disconnected_qpair_cb)
    2892                 :            : {
    2893                 :  303190376 :         struct nvme_tcp_poll_group *group = nvme_tcp_poll_group(tgroup);
    2894                 :            :         struct spdk_nvme_qpair *qpair, *tmp_qpair;
    2895                 :            :         struct nvme_tcp_qpair *tqpair, *tmp_tqpair;
    2896                 :            :         int num_events;
    2897                 :            : 
    2898                 :  303190376 :         group->completions_per_qpair = completions_per_qpair;
    2899                 :  303190376 :         group->num_completions = 0;
    2900                 :  303190376 :         group->stats.polls++;
    2901                 :            : 
    2902                 :  303190376 :         num_events = spdk_sock_group_poll(group->sock_group);
    2903                 :            : 
    2904         [ +  + ]:  305627363 :         STAILQ_FOREACH_SAFE(qpair, &tgroup->disconnected_qpairs, poll_group_stailq, tmp_qpair) {
    2905                 :    2436987 :                 tqpair = nvme_tcp_qpair(qpair);
    2906         [ +  + ]:    2436987 :                 if (nvme_qpair_get_state(qpair) == NVME_QPAIR_DISCONNECTING) {
    2907         [ +  + ]:        810 :                         if (TAILQ_EMPTY(&tqpair->outstanding_reqs)) {
    2908                 :        804 :                                 nvme_transport_ctrlr_disconnect_qpair_done(qpair);
    2909                 :            :                         }
    2910                 :            :                 }
    2911                 :            :                 /* Wait until the qpair transitions to the DISCONNECTED state, otherwise user might
    2912                 :            :                  * want to free it from disconnect_qpair_cb, while it's not fully disconnected (and
    2913                 :            :                  * might still have outstanding requests) */
    2914         [ +  + ]:    2436987 :                 if (nvme_qpair_get_state(qpair) == NVME_QPAIR_DISCONNECTED) {
    2915                 :    2436981 :                         disconnected_qpair_cb(qpair, tgroup->group->ctx);
    2916                 :            :                 }
    2917                 :            :         }
    2918                 :            : 
    2919                 :            :         /* If any qpairs were marked as needing to be polled due to an asynchronous write completion
    2920                 :            :          * and they weren't polled as a consequence of calling spdk_sock_group_poll above, poll them now. */
    2921         [ +  + ]:  303684741 :         TAILQ_FOREACH_SAFE(tqpair, &group->needs_poll, link, tmp_tqpair) {
    2922                 :     494365 :                 nvme_tcp_qpair_sock_cb(&tqpair->qpair, group->sock_group, tqpair->sock);
    2923                 :            :         }
    2924                 :            : 
    2925         [ -  + ]:  303190376 :         if (spdk_unlikely(num_events < 0)) {
    2926                 :          0 :                 return num_events;
    2927                 :            :         }
    2928                 :            : 
    2929                 :  303190376 :         group->stats.idle_polls += !num_events;
    2930                 :  303190376 :         group->stats.socket_completions += num_events;
    2931                 :            : 
    2932                 :  303190376 :         return group->num_completions;
    2933                 :            : }
    2934                 :            : 
    2935                 :            : static int
    2936                 :        830 : nvme_tcp_poll_group_destroy(struct spdk_nvme_transport_poll_group *tgroup)
    2937                 :            : {
    2938                 :            :         int rc;
    2939                 :        830 :         struct nvme_tcp_poll_group *group = nvme_tcp_poll_group(tgroup);
    2940                 :            : 
    2941   [ +  -  -  + ]:        830 :         if (!STAILQ_EMPTY(&tgroup->connected_qpairs) || !STAILQ_EMPTY(&tgroup->disconnected_qpairs)) {
    2942                 :          0 :                 return -EBUSY;
    2943                 :            :         }
    2944                 :            : 
    2945                 :        830 :         rc = spdk_sock_group_close(&group->sock_group);
    2946         [ -  + ]:        830 :         if (rc != 0) {
    2947                 :          0 :                 SPDK_ERRLOG("Failed to close the sock group for a tcp poll group.\n");
    2948                 :          0 :                 assert(false);
    2949                 :            :         }
    2950                 :            : 
    2951                 :        830 :         free(tgroup);
    2952                 :            : 
    2953                 :        830 :         return 0;
    2954                 :            : }
    2955                 :            : 
    2956                 :            : static int
    2957                 :         24 : nvme_tcp_poll_group_get_stats(struct spdk_nvme_transport_poll_group *tgroup,
    2958                 :            :                               struct spdk_nvme_transport_poll_group_stat **_stats)
    2959                 :            : {
    2960                 :            :         struct nvme_tcp_poll_group *group;
    2961                 :            :         struct spdk_nvme_transport_poll_group_stat *stats;
    2962                 :            : 
    2963   [ +  +  +  + ]:         24 :         if (tgroup == NULL || _stats == NULL) {
    2964                 :         12 :                 SPDK_ERRLOG("Invalid stats or group pointer\n");
    2965                 :         12 :                 return -EINVAL;
    2966                 :            :         }
    2967                 :            : 
    2968                 :         12 :         group = nvme_tcp_poll_group(tgroup);
    2969                 :            : 
    2970                 :         12 :         stats = calloc(1, sizeof(*stats));
    2971         [ -  + ]:         12 :         if (!stats) {
    2972                 :          0 :                 SPDK_ERRLOG("Can't allocate memory for TCP stats\n");
    2973                 :          0 :                 return -ENOMEM;
    2974                 :            :         }
    2975                 :         12 :         stats->trtype = SPDK_NVME_TRANSPORT_TCP;
    2976   [ -  +  -  + ]:         12 :         memcpy(&stats->tcp, &group->stats, sizeof(group->stats));
    2977                 :            : 
    2978                 :         12 :         *_stats = stats;
    2979                 :            : 
    2980                 :         12 :         return 0;
    2981                 :            : }
    2982                 :            : 
    2983                 :            : static void
    2984                 :         12 : nvme_tcp_poll_group_free_stats(struct spdk_nvme_transport_poll_group *tgroup,
    2985                 :            :                                struct spdk_nvme_transport_poll_group_stat *stats)
    2986                 :            : {
    2987                 :         12 :         free(stats);
    2988                 :         12 : }
    2989                 :            : 
    2990                 :            : static int
    2991                 :       3272 : nvme_tcp_ctrlr_get_memory_domains(const struct spdk_nvme_ctrlr *ctrlr,
    2992                 :            :                                   struct spdk_memory_domain **domains, int array_size)
    2993                 :            : {
    2994   [ +  +  +  - ]:       3272 :         if (domains && array_size > 0) {
    2995                 :        241 :                 domains[0] = spdk_memory_domain_get_system_domain();
    2996                 :            :         }
    2997                 :            : 
    2998                 :       3272 :         return 1;
    2999                 :            : }
    3000                 :            : 
    3001                 :            : const struct spdk_nvme_transport_ops tcp_ops = {
    3002                 :            :         .name = "TCP",
    3003                 :            :         .type = SPDK_NVME_TRANSPORT_TCP,
    3004                 :            :         .ctrlr_construct = nvme_tcp_ctrlr_construct,
    3005                 :            :         .ctrlr_scan = nvme_fabric_ctrlr_scan,
    3006                 :            :         .ctrlr_destruct = nvme_tcp_ctrlr_destruct,
    3007                 :            :         .ctrlr_enable = nvme_tcp_ctrlr_enable,
    3008                 :            : 
    3009                 :            :         .ctrlr_set_reg_4 = nvme_fabric_ctrlr_set_reg_4,
    3010                 :            :         .ctrlr_set_reg_8 = nvme_fabric_ctrlr_set_reg_8,
    3011                 :            :         .ctrlr_get_reg_4 = nvme_fabric_ctrlr_get_reg_4,
    3012                 :            :         .ctrlr_get_reg_8 = nvme_fabric_ctrlr_get_reg_8,
    3013                 :            :         .ctrlr_set_reg_4_async = nvme_fabric_ctrlr_set_reg_4_async,
    3014                 :            :         .ctrlr_set_reg_8_async = nvme_fabric_ctrlr_set_reg_8_async,
    3015                 :            :         .ctrlr_get_reg_4_async = nvme_fabric_ctrlr_get_reg_4_async,
    3016                 :            :         .ctrlr_get_reg_8_async = nvme_fabric_ctrlr_get_reg_8_async,
    3017                 :            : 
    3018                 :            :         .ctrlr_get_max_xfer_size = nvme_tcp_ctrlr_get_max_xfer_size,
    3019                 :            :         .ctrlr_get_max_sges = nvme_tcp_ctrlr_get_max_sges,
    3020                 :            : 
    3021                 :            :         .ctrlr_create_io_qpair = nvme_tcp_ctrlr_create_io_qpair,
    3022                 :            :         .ctrlr_delete_io_qpair = nvme_tcp_ctrlr_delete_io_qpair,
    3023                 :            :         .ctrlr_connect_qpair = nvme_tcp_ctrlr_connect_qpair,
    3024                 :            :         .ctrlr_disconnect_qpair = nvme_tcp_ctrlr_disconnect_qpair,
    3025                 :            : 
    3026                 :            :         .ctrlr_get_memory_domains = nvme_tcp_ctrlr_get_memory_domains,
    3027                 :            : 
    3028                 :            :         .qpair_abort_reqs = nvme_tcp_qpair_abort_reqs,
    3029                 :            :         .qpair_reset = nvme_tcp_qpair_reset,
    3030                 :            :         .qpair_submit_request = nvme_tcp_qpair_submit_request,
    3031                 :            :         .qpair_process_completions = nvme_tcp_qpair_process_completions,
    3032                 :            :         .qpair_iterate_requests = nvme_tcp_qpair_iterate_requests,
    3033                 :            :         .admin_qpair_abort_aers = nvme_tcp_admin_qpair_abort_aers,
    3034                 :            : 
    3035                 :            :         .poll_group_create = nvme_tcp_poll_group_create,
    3036                 :            :         .qpair_get_optimal_poll_group = nvme_tcp_qpair_get_optimal_poll_group,
    3037                 :            :         .poll_group_connect_qpair = nvme_tcp_poll_group_connect_qpair,
    3038                 :            :         .poll_group_disconnect_qpair = nvme_tcp_poll_group_disconnect_qpair,
    3039                 :            :         .poll_group_add = nvme_tcp_poll_group_add,
    3040                 :            :         .poll_group_remove = nvme_tcp_poll_group_remove,
    3041                 :            :         .poll_group_process_completions = nvme_tcp_poll_group_process_completions,
    3042                 :            :         .poll_group_destroy = nvme_tcp_poll_group_destroy,
    3043                 :            :         .poll_group_get_stats = nvme_tcp_poll_group_get_stats,
    3044                 :            :         .poll_group_free_stats = nvme_tcp_poll_group_free_stats,
    3045                 :            : };
    3046                 :            : 
    3047                 :       2810 : SPDK_NVME_TRANSPORT_REGISTER(tcp, &tcp_ops);
    3048                 :            : 
    3049                 :       5036 : SPDK_TRACE_REGISTER_FN(nvme_tcp, "nvme_tcp", TRACE_GROUP_NVME_TCP)
    3050                 :            : {
    3051                 :       2226 :         struct spdk_trace_tpoint_opts opts[] = {
    3052                 :            :                 {
    3053                 :            :                         "NVME_TCP_SUBMIT", TRACE_NVME_TCP_SUBMIT,
    3054                 :            :                         OWNER_TYPE_NVME_TCP_QP, OBJECT_NVME_TCP_REQ, 1,
    3055                 :            :                         {       { "ctx", SPDK_TRACE_ARG_TYPE_PTR, 8 },
    3056                 :            :                                 { "cid", SPDK_TRACE_ARG_TYPE_INT, 4 },
    3057                 :            :                                 { "opc", SPDK_TRACE_ARG_TYPE_INT, 4 },
    3058                 :            :                                 { "dw10", SPDK_TRACE_ARG_TYPE_PTR, 4 },
    3059                 :            :                                 { "dw11", SPDK_TRACE_ARG_TYPE_PTR, 4 },
    3060                 :            :                                 { "dw12", SPDK_TRACE_ARG_TYPE_PTR, 4 },
    3061                 :            :                                 { "qd", SPDK_TRACE_ARG_TYPE_INT, 4 }
    3062                 :            :                         }
    3063                 :            :                 },
    3064                 :            :                 {
    3065                 :            :                         "NVME_TCP_COMPLETE", TRACE_NVME_TCP_COMPLETE,
    3066                 :            :                         OWNER_TYPE_NVME_TCP_QP, OBJECT_NVME_TCP_REQ, 0,
    3067                 :            :                         {       { "ctx", SPDK_TRACE_ARG_TYPE_PTR, 8 },
    3068                 :            :                                 { "cid", SPDK_TRACE_ARG_TYPE_INT, 4 },
    3069                 :            :                                 { "cpl", SPDK_TRACE_ARG_TYPE_PTR, 4 },
    3070                 :            :                                 { "qd", SPDK_TRACE_ARG_TYPE_INT, 4 }
    3071                 :            :                         }
    3072                 :            :                 },
    3073                 :            :         };
    3074                 :            : 
    3075                 :       2226 :         spdk_trace_register_object(OBJECT_NVME_TCP_REQ, 'p');
    3076                 :       2226 :         spdk_trace_register_owner_type(OWNER_TYPE_NVME_TCP_QP, 'q');
    3077                 :       2226 :         spdk_trace_register_description_ext(opts, SPDK_COUNTOF(opts));
    3078                 :            : 
    3079                 :       2226 :         spdk_trace_tpoint_register_relation(TRACE_SOCK_REQ_QUEUE, OBJECT_NVME_TCP_REQ, 0);
    3080                 :       2226 :         spdk_trace_tpoint_register_relation(TRACE_SOCK_REQ_PEND, OBJECT_NVME_TCP_REQ, 0);
    3081                 :       2226 :         spdk_trace_tpoint_register_relation(TRACE_SOCK_REQ_COMPLETE, OBJECT_NVME_TCP_REQ, 0);
    3082                 :       2226 : }

Generated by: LCOV version 1.14