LCOV - code coverage report
Current view: top level - spdk/lib/nvme - nvme_tcp.c (source / functions) Hit Total Coverage
Test: Combined Lines: 1203 1526 78.8 %
Date: 2024-07-12 19:12:53 Functions: 85 94 90.4 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 647 963 67.2 %

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

Generated by: LCOV version 1.14