LCOV - code coverage report
Current view: top level - spdk/lib/nvmf - tcp.c (source / functions) Hit Total Coverage
Test: Combined Lines: 1415 1788 79.1 %
Date: 2024-07-11 15:05:21 Functions: 88 98 89.8 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 830 1362 60.9 %

           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) 2019, 2020 Mellanox Technologies LTD. All rights reserved.
       4                 :            :  *   Copyright (c) 2022, 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
       5                 :            :  */
       6                 :            : 
       7                 :            : #include "spdk/accel.h"
       8                 :            : #include "spdk/stdinc.h"
       9                 :            : #include "spdk/crc32.h"
      10                 :            : #include "spdk/endian.h"
      11                 :            : #include "spdk/assert.h"
      12                 :            : #include "spdk/thread.h"
      13                 :            : #include "spdk/nvmf_transport.h"
      14                 :            : #include "spdk/string.h"
      15                 :            : #include "spdk/trace.h"
      16                 :            : #include "spdk/util.h"
      17                 :            : #include "spdk/log.h"
      18                 :            : 
      19                 :            : #include "spdk_internal/assert.h"
      20                 :            : #include "spdk_internal/nvme_tcp.h"
      21                 :            : #include "spdk_internal/sock.h"
      22                 :            : 
      23                 :            : #include "nvmf_internal.h"
      24                 :            : 
      25                 :            : #include "spdk_internal/trace_defs.h"
      26                 :            : 
      27                 :            : #define NVMF_TCP_MAX_ACCEPT_SOCK_ONE_TIME 16
      28                 :            : #define SPDK_NVMF_TCP_DEFAULT_MAX_SOCK_PRIORITY 16
      29                 :            : #define SPDK_NVMF_TCP_DEFAULT_SOCK_PRIORITY 0
      30                 :            : #define SPDK_NVMF_TCP_DEFAULT_CONTROL_MSG_NUM 32
      31                 :            : #define SPDK_NVMF_TCP_DEFAULT_SUCCESS_OPTIMIZATION true
      32                 :            : 
      33                 :            : #define SPDK_NVMF_TCP_MIN_IO_QUEUE_DEPTH 2
      34                 :            : #define SPDK_NVMF_TCP_MAX_IO_QUEUE_DEPTH 65535
      35                 :            : #define SPDK_NVMF_TCP_MIN_ADMIN_QUEUE_DEPTH 2
      36                 :            : #define SPDK_NVMF_TCP_MAX_ADMIN_QUEUE_DEPTH 4096
      37                 :            : 
      38                 :            : #define SPDK_NVMF_TCP_DEFAULT_MAX_IO_QUEUE_DEPTH 128
      39                 :            : #define SPDK_NVMF_TCP_DEFAULT_MAX_ADMIN_QUEUE_DEPTH 128
      40                 :            : #define SPDK_NVMF_TCP_DEFAULT_MAX_QPAIRS_PER_CTRLR 128
      41                 :            : #define SPDK_NVMF_TCP_DEFAULT_IN_CAPSULE_DATA_SIZE 4096
      42                 :            : #define SPDK_NVMF_TCP_DEFAULT_MAX_IO_SIZE 131072
      43                 :            : #define SPDK_NVMF_TCP_DEFAULT_IO_UNIT_SIZE 131072
      44                 :            : #define SPDK_NVMF_TCP_DEFAULT_NUM_SHARED_BUFFERS 511
      45                 :            : #define SPDK_NVMF_TCP_DEFAULT_BUFFER_CACHE_SIZE UINT32_MAX
      46                 :            : #define SPDK_NVMF_TCP_DEFAULT_DIF_INSERT_OR_STRIP false
      47                 :            : #define SPDK_NVMF_TCP_DEFAULT_ABORT_TIMEOUT_SEC 1
      48                 :            : 
      49                 :            : #define TCP_PSK_INVALID_PERMISSIONS 0177
      50                 :            : 
      51                 :            : const struct spdk_nvmf_transport_ops spdk_nvmf_transport_tcp;
      52                 :            : static bool g_tls_log = false;
      53                 :            : 
      54                 :            : /* spdk nvmf related structure */
      55                 :            : enum spdk_nvmf_tcp_req_state {
      56                 :            : 
      57                 :            :         /* The request is not currently in use */
      58                 :            :         TCP_REQUEST_STATE_FREE = 0,
      59                 :            : 
      60                 :            :         /* Initial state when request first received */
      61                 :            :         TCP_REQUEST_STATE_NEW = 1,
      62                 :            : 
      63                 :            :         /* The request is queued until a data buffer is available. */
      64                 :            :         TCP_REQUEST_STATE_NEED_BUFFER = 2,
      65                 :            : 
      66                 :            :         /* The request is waiting for zcopy_start to finish */
      67                 :            :         TCP_REQUEST_STATE_AWAITING_ZCOPY_START = 3,
      68                 :            : 
      69                 :            :         /* The request has received a zero-copy buffer */
      70                 :            :         TCP_REQUEST_STATE_ZCOPY_START_COMPLETED = 4,
      71                 :            : 
      72                 :            :         /* The request is currently transferring data from the host to the controller. */
      73                 :            :         TCP_REQUEST_STATE_TRANSFERRING_HOST_TO_CONTROLLER = 5,
      74                 :            : 
      75                 :            :         /* The request is waiting for the R2T send acknowledgement. */
      76                 :            :         TCP_REQUEST_STATE_AWAITING_R2T_ACK = 6,
      77                 :            : 
      78                 :            :         /* The request is ready to execute at the block device */
      79                 :            :         TCP_REQUEST_STATE_READY_TO_EXECUTE = 7,
      80                 :            : 
      81                 :            :         /* The request is currently executing at the block device */
      82                 :            :         TCP_REQUEST_STATE_EXECUTING = 8,
      83                 :            : 
      84                 :            :         /* The request is waiting for zcopy buffers to be committed */
      85                 :            :         TCP_REQUEST_STATE_AWAITING_ZCOPY_COMMIT = 9,
      86                 :            : 
      87                 :            :         /* The request finished executing at the block device */
      88                 :            :         TCP_REQUEST_STATE_EXECUTED = 10,
      89                 :            : 
      90                 :            :         /* The request is ready to send a completion */
      91                 :            :         TCP_REQUEST_STATE_READY_TO_COMPLETE = 11,
      92                 :            : 
      93                 :            :         /* The request is currently transferring final pdus from the controller to the host. */
      94                 :            :         TCP_REQUEST_STATE_TRANSFERRING_CONTROLLER_TO_HOST = 12,
      95                 :            : 
      96                 :            :         /* The request is waiting for zcopy buffers to be released (without committing) */
      97                 :            :         TCP_REQUEST_STATE_AWAITING_ZCOPY_RELEASE = 13,
      98                 :            : 
      99                 :            :         /* The request completed and can be marked free. */
     100                 :            :         TCP_REQUEST_STATE_COMPLETED = 14,
     101                 :            : 
     102                 :            :         /* Terminator */
     103                 :            :         TCP_REQUEST_NUM_STATES,
     104                 :            : };
     105                 :            : 
     106                 :            : static const char *spdk_nvmf_tcp_term_req_fes_str[] = {
     107                 :            :         "Invalid PDU Header Field",
     108                 :            :         "PDU Sequence Error",
     109                 :            :         "Header Digiest Error",
     110                 :            :         "Data Transfer Out of Range",
     111                 :            :         "R2T Limit Exceeded",
     112                 :            :         "Unsupported parameter",
     113                 :            : };
     114                 :            : 
     115                 :       1399 : SPDK_TRACE_REGISTER_FN(nvmf_tcp_trace, "nvmf_tcp", TRACE_GROUP_NVMF_TCP)
     116                 :            : {
     117                 :        677 :         spdk_trace_register_owner(OWNER_NVMF_TCP, 't');
     118                 :        677 :         spdk_trace_register_object(OBJECT_NVMF_TCP_IO, 'r');
     119                 :        677 :         spdk_trace_register_description("TCP_REQ_NEW",
     120                 :            :                                         TRACE_TCP_REQUEST_STATE_NEW,
     121                 :            :                                         OWNER_NVMF_TCP, OBJECT_NVMF_TCP_IO, 1,
     122                 :            :                                         SPDK_TRACE_ARG_TYPE_PTR, "qpair");
     123                 :        677 :         spdk_trace_register_description("TCP_REQ_NEED_BUFFER",
     124                 :            :                                         TRACE_TCP_REQUEST_STATE_NEED_BUFFER,
     125                 :            :                                         OWNER_NVMF_TCP, OBJECT_NVMF_TCP_IO, 0,
     126                 :            :                                         SPDK_TRACE_ARG_TYPE_PTR, "qpair");
     127                 :        677 :         spdk_trace_register_description("TCP_REQ_WAIT_ZCPY_START",
     128                 :            :                                         TRACE_TCP_REQUEST_STATE_AWAIT_ZCOPY_START,
     129                 :            :                                         OWNER_NVMF_TCP, OBJECT_NVMF_TCP_IO, 0,
     130                 :            :                                         SPDK_TRACE_ARG_TYPE_PTR, "qpair");
     131                 :        677 :         spdk_trace_register_description("TCP_REQ_ZCPY_START_CPL",
     132                 :            :                                         TRACE_TCP_REQUEST_STATE_ZCOPY_START_COMPLETED,
     133                 :            :                                         OWNER_NVMF_TCP, OBJECT_NVMF_TCP_IO, 0,
     134                 :            :                                         SPDK_TRACE_ARG_TYPE_PTR, "qpair");
     135                 :        677 :         spdk_trace_register_description("TCP_REQ_TX_H_TO_C",
     136                 :            :                                         TRACE_TCP_REQUEST_STATE_TRANSFERRING_HOST_TO_CONTROLLER,
     137                 :            :                                         OWNER_NVMF_TCP, OBJECT_NVMF_TCP_IO, 0,
     138                 :            :                                         SPDK_TRACE_ARG_TYPE_PTR, "qpair");
     139                 :        677 :         spdk_trace_register_description("TCP_REQ_RDY_TO_EXECUTE",
     140                 :            :                                         TRACE_TCP_REQUEST_STATE_READY_TO_EXECUTE,
     141                 :            :                                         OWNER_NVMF_TCP, OBJECT_NVMF_TCP_IO, 0,
     142                 :            :                                         SPDK_TRACE_ARG_TYPE_PTR, "qpair");
     143                 :        677 :         spdk_trace_register_description("TCP_REQ_EXECUTING",
     144                 :            :                                         TRACE_TCP_REQUEST_STATE_EXECUTING,
     145                 :            :                                         OWNER_NVMF_TCP, OBJECT_NVMF_TCP_IO, 0,
     146                 :            :                                         SPDK_TRACE_ARG_TYPE_PTR, "qpair");
     147                 :        677 :         spdk_trace_register_description("TCP_REQ_WAIT_ZCPY_CMT",
     148                 :            :                                         TRACE_TCP_REQUEST_STATE_AWAIT_ZCOPY_COMMIT,
     149                 :            :                                         OWNER_NVMF_TCP, OBJECT_NVMF_TCP_IO, 0,
     150                 :            :                                         SPDK_TRACE_ARG_TYPE_PTR, "qpair");
     151                 :        677 :         spdk_trace_register_description("TCP_REQ_EXECUTED",
     152                 :            :                                         TRACE_TCP_REQUEST_STATE_EXECUTED,
     153                 :            :                                         OWNER_NVMF_TCP, OBJECT_NVMF_TCP_IO, 0,
     154                 :            :                                         SPDK_TRACE_ARG_TYPE_PTR, "qpair");
     155                 :        677 :         spdk_trace_register_description("TCP_REQ_RDY_TO_COMPLETE",
     156                 :            :                                         TRACE_TCP_REQUEST_STATE_READY_TO_COMPLETE,
     157                 :            :                                         OWNER_NVMF_TCP, OBJECT_NVMF_TCP_IO, 0,
     158                 :            :                                         SPDK_TRACE_ARG_TYPE_PTR, "qpair");
     159                 :        677 :         spdk_trace_register_description("TCP_REQ_TRANSFER_C2H",
     160                 :            :                                         TRACE_TCP_REQUEST_STATE_TRANSFERRING_CONTROLLER_TO_HOST,
     161                 :            :                                         OWNER_NVMF_TCP, OBJECT_NVMF_TCP_IO, 0,
     162                 :            :                                         SPDK_TRACE_ARG_TYPE_PTR, "qpair");
     163                 :        677 :         spdk_trace_register_description("TCP_REQ_AWAIT_ZCPY_RLS",
     164                 :            :                                         TRACE_TCP_REQUEST_STATE_AWAIT_ZCOPY_RELEASE,
     165                 :            :                                         OWNER_NVMF_TCP, OBJECT_NVMF_TCP_IO, 0,
     166                 :            :                                         SPDK_TRACE_ARG_TYPE_PTR, "qpair");
     167                 :        677 :         spdk_trace_register_description("TCP_REQ_COMPLETED",
     168                 :            :                                         TRACE_TCP_REQUEST_STATE_COMPLETED,
     169                 :            :                                         OWNER_NVMF_TCP, OBJECT_NVMF_TCP_IO, 0,
     170                 :            :                                         SPDK_TRACE_ARG_TYPE_PTR, "qpair");
     171                 :        677 :         spdk_trace_register_description("TCP_WRITE_START",
     172                 :            :                                         TRACE_TCP_FLUSH_WRITEBUF_START,
     173                 :            :                                         OWNER_NVMF_TCP, OBJECT_NONE, 0,
     174                 :            :                                         SPDK_TRACE_ARG_TYPE_PTR, "qpair");
     175                 :        677 :         spdk_trace_register_description("TCP_WRITE_DONE",
     176                 :            :                                         TRACE_TCP_FLUSH_WRITEBUF_DONE,
     177                 :            :                                         OWNER_NVMF_TCP, OBJECT_NONE, 0,
     178                 :            :                                         SPDK_TRACE_ARG_TYPE_PTR, "qpair");
     179                 :        677 :         spdk_trace_register_description("TCP_READ_DONE",
     180                 :            :                                         TRACE_TCP_READ_FROM_SOCKET_DONE,
     181                 :            :                                         OWNER_NVMF_TCP, OBJECT_NONE, 0,
     182                 :            :                                         SPDK_TRACE_ARG_TYPE_PTR, "qpair");
     183                 :        677 :         spdk_trace_register_description("TCP_REQ_AWAIT_R2T_ACK",
     184                 :            :                                         TRACE_TCP_REQUEST_STATE_AWAIT_R2T_ACK,
     185                 :            :                                         OWNER_NVMF_TCP, OBJECT_NVMF_TCP_IO, 0,
     186                 :            :                                         SPDK_TRACE_ARG_TYPE_PTR, "qpair");
     187                 :            : 
     188                 :        677 :         spdk_trace_register_description("TCP_QP_CREATE", TRACE_TCP_QP_CREATE,
     189                 :            :                                         OWNER_NVMF_TCP, OBJECT_NONE, 0,
     190                 :            :                                         SPDK_TRACE_ARG_TYPE_INT, "");
     191                 :        677 :         spdk_trace_register_description("TCP_QP_SOCK_INIT", TRACE_TCP_QP_SOCK_INIT,
     192                 :            :                                         OWNER_NVMF_TCP, OBJECT_NONE, 0,
     193                 :            :                                         SPDK_TRACE_ARG_TYPE_INT, "");
     194                 :        677 :         spdk_trace_register_description("TCP_QP_STATE_CHANGE", TRACE_TCP_QP_STATE_CHANGE,
     195                 :            :                                         OWNER_NVMF_TCP, OBJECT_NONE, 0,
     196                 :            :                                         SPDK_TRACE_ARG_TYPE_INT, "state");
     197                 :        677 :         spdk_trace_register_description("TCP_QP_DISCONNECT", TRACE_TCP_QP_DISCONNECT,
     198                 :            :                                         OWNER_NVMF_TCP, OBJECT_NONE, 0,
     199                 :            :                                         SPDK_TRACE_ARG_TYPE_INT, "");
     200                 :        677 :         spdk_trace_register_description("TCP_QP_DESTROY", TRACE_TCP_QP_DESTROY,
     201                 :            :                                         OWNER_NVMF_TCP, OBJECT_NONE, 0,
     202                 :            :                                         SPDK_TRACE_ARG_TYPE_INT, "");
     203                 :        677 :         spdk_trace_register_description("TCP_QP_ABORT_REQ", TRACE_TCP_QP_ABORT_REQ,
     204                 :            :                                         OWNER_NVMF_TCP, OBJECT_NONE, 0,
     205                 :            :                                         SPDK_TRACE_ARG_TYPE_PTR, "qpair");
     206                 :        677 :         spdk_trace_register_description("TCP_QP_RCV_STATE_CHANGE", TRACE_TCP_QP_RCV_STATE_CHANGE,
     207                 :            :                                         OWNER_NVMF_TCP, OBJECT_NONE, 0,
     208                 :            :                                         SPDK_TRACE_ARG_TYPE_INT, "state");
     209                 :            : 
     210                 :        677 :         spdk_trace_tpoint_register_relation(TRACE_BDEV_IO_START, OBJECT_NVMF_TCP_IO, 1);
     211                 :        677 :         spdk_trace_tpoint_register_relation(TRACE_BDEV_IO_DONE, OBJECT_NVMF_TCP_IO, 0);
     212                 :        677 : }
     213                 :            : 
     214                 :            : struct spdk_nvmf_tcp_req  {
     215                 :            :         struct spdk_nvmf_request                req;
     216                 :            :         struct spdk_nvme_cpl                    rsp;
     217                 :            :         struct spdk_nvme_cmd                    cmd;
     218                 :            : 
     219                 :            :         /* A PDU that can be used for sending responses. This is
     220                 :            :          * not the incoming PDU! */
     221                 :            :         struct nvme_tcp_pdu                     *pdu;
     222                 :            : 
     223                 :            :         /* In-capsule data buffer */
     224                 :            :         uint8_t                                 *buf;
     225                 :            : 
     226                 :            :         struct spdk_nvmf_tcp_req                *fused_pair;
     227                 :            : 
     228                 :            :         /*
     229                 :            :          * The PDU for a request may be used multiple times in serial over
     230                 :            :          * the request's lifetime. For example, first to send an R2T, then
     231                 :            :          * to send a completion. To catch mistakes where the PDU is used
     232                 :            :          * twice at the same time, add a debug flag here for init/fini.
     233                 :            :          */
     234                 :            :         bool                                    pdu_in_use;
     235                 :            :         bool                                    has_in_capsule_data;
     236                 :            :         bool                                    fused_failed;
     237                 :            : 
     238                 :            :         /* transfer_tag */
     239                 :            :         uint16_t                                ttag;
     240                 :            : 
     241                 :            :         enum spdk_nvmf_tcp_req_state            state;
     242                 :            : 
     243                 :            :         /*
     244                 :            :          * h2c_offset is used when we receive the h2c_data PDU.
     245                 :            :          */
     246                 :            :         uint32_t                                h2c_offset;
     247                 :            : 
     248                 :            :         STAILQ_ENTRY(spdk_nvmf_tcp_req)         link;
     249                 :            :         TAILQ_ENTRY(spdk_nvmf_tcp_req)          state_link;
     250                 :            : };
     251                 :            : 
     252                 :            : struct spdk_nvmf_tcp_qpair {
     253                 :            :         struct spdk_nvmf_qpair                  qpair;
     254                 :            :         struct spdk_nvmf_tcp_poll_group         *group;
     255                 :            :         struct spdk_sock                        *sock;
     256                 :            : 
     257                 :            :         enum nvme_tcp_pdu_recv_state            recv_state;
     258                 :            :         enum nvme_tcp_qpair_state               state;
     259                 :            : 
     260                 :            :         /* PDU being actively received */
     261                 :            :         struct nvme_tcp_pdu                     *pdu_in_progress;
     262                 :            : 
     263                 :            :         struct spdk_nvmf_tcp_req                *fused_first;
     264                 :            : 
     265                 :            :         /* Queues to track the requests in all states */
     266                 :            :         TAILQ_HEAD(, spdk_nvmf_tcp_req)         tcp_req_working_queue;
     267                 :            :         TAILQ_HEAD(, spdk_nvmf_tcp_req)         tcp_req_free_queue;
     268                 :            :         SLIST_HEAD(, nvme_tcp_pdu)              tcp_pdu_free_queue;
     269                 :            :         /* Number of working pdus */
     270                 :            :         uint32_t                                tcp_pdu_working_count;
     271                 :            : 
     272                 :            :         /* Number of requests in each state */
     273                 :            :         uint32_t                                state_cntr[TCP_REQUEST_NUM_STATES];
     274                 :            : 
     275                 :            :         uint8_t                                 cpda;
     276                 :            : 
     277                 :            :         bool                                    host_hdgst_enable;
     278                 :            :         bool                                    host_ddgst_enable;
     279                 :            : 
     280                 :            :         /* This is a spare PDU used for sending special management
     281                 :            :          * operations. Primarily, this is used for the initial
     282                 :            :          * connection response and c2h termination request. */
     283                 :            :         struct nvme_tcp_pdu                     *mgmt_pdu;
     284                 :            : 
     285                 :            :         /* Arrays of in-capsule buffers, requests, and pdus.
     286                 :            :          * Each array is 'resource_count' number of elements */
     287                 :            :         void                                    *bufs;
     288                 :            :         struct spdk_nvmf_tcp_req                *reqs;
     289                 :            :         struct nvme_tcp_pdu                     *pdus;
     290                 :            :         uint32_t                                resource_count;
     291                 :            :         uint32_t                                recv_buf_size;
     292                 :            : 
     293                 :            :         struct spdk_nvmf_tcp_port               *port;
     294                 :            : 
     295                 :            :         /* IP address */
     296                 :            :         char                                    initiator_addr[SPDK_NVMF_TRADDR_MAX_LEN];
     297                 :            :         char                                    target_addr[SPDK_NVMF_TRADDR_MAX_LEN];
     298                 :            : 
     299                 :            :         /* IP port */
     300                 :            :         uint16_t                                initiator_port;
     301                 :            :         uint16_t                                target_port;
     302                 :            : 
     303                 :            :         /* Wait until the host terminates the connection (e.g. after sending C2HTermReq) */
     304                 :            :         bool                                    wait_terminate;
     305                 :            : 
     306                 :            :         /* Timer used to destroy qpair after detecting transport error issue if initiator does
     307                 :            :          *  not close the connection.
     308                 :            :          */
     309                 :            :         struct spdk_poller                      *timeout_poller;
     310                 :            : 
     311                 :            :         spdk_nvmf_transport_qpair_fini_cb       fini_cb_fn;
     312                 :            :         void                                    *fini_cb_arg;
     313                 :            : 
     314                 :            :         TAILQ_ENTRY(spdk_nvmf_tcp_qpair)        link;
     315                 :            : };
     316                 :            : 
     317                 :            : struct spdk_nvmf_tcp_control_msg {
     318                 :            :         STAILQ_ENTRY(spdk_nvmf_tcp_control_msg) link;
     319                 :            : };
     320                 :            : 
     321                 :            : struct spdk_nvmf_tcp_control_msg_list {
     322                 :            :         void *msg_buf;
     323                 :            :         STAILQ_HEAD(, spdk_nvmf_tcp_control_msg) free_msgs;
     324                 :            : };
     325                 :            : 
     326                 :            : struct spdk_nvmf_tcp_poll_group {
     327                 :            :         struct spdk_nvmf_transport_poll_group   group;
     328                 :            :         struct spdk_sock_group                  *sock_group;
     329                 :            : 
     330                 :            :         TAILQ_HEAD(, spdk_nvmf_tcp_qpair)       qpairs;
     331                 :            :         TAILQ_HEAD(, spdk_nvmf_tcp_qpair)       await_req;
     332                 :            : 
     333                 :            :         struct spdk_io_channel                  *accel_channel;
     334                 :            :         struct spdk_nvmf_tcp_control_msg_list   *control_msg_list;
     335                 :            : 
     336                 :            :         TAILQ_ENTRY(spdk_nvmf_tcp_poll_group)   link;
     337                 :            : };
     338                 :            : 
     339                 :            : struct spdk_nvmf_tcp_port {
     340                 :            :         const struct spdk_nvme_transport_id     *trid;
     341                 :            :         struct spdk_sock                        *listen_sock;
     342                 :            :         TAILQ_ENTRY(spdk_nvmf_tcp_port)         link;
     343                 :            : };
     344                 :            : 
     345                 :            : struct tcp_transport_opts {
     346                 :            :         bool            c2h_success;
     347                 :            :         uint16_t        control_msg_num;
     348                 :            :         uint32_t        sock_priority;
     349                 :            : };
     350                 :            : 
     351                 :            : struct tcp_psk_entry {
     352                 :            :         char                            hostnqn[SPDK_NVMF_NQN_MAX_LEN + 1];
     353                 :            :         char                            subnqn[SPDK_NVMF_NQN_MAX_LEN + 1];
     354                 :            :         char                            psk_identity[NVMF_PSK_IDENTITY_LEN];
     355                 :            :         uint8_t                         psk[SPDK_TLS_PSK_MAX_LEN];
     356                 :            : 
     357                 :            :         /* Original path saved to emit SPDK configuration via "save_config". */
     358                 :            :         char                            psk_path[PATH_MAX];
     359                 :            :         uint32_t                        psk_size;
     360                 :            :         enum nvme_tcp_cipher_suite      tls_cipher_suite;
     361                 :            :         TAILQ_ENTRY(tcp_psk_entry)      link;
     362                 :            : };
     363                 :            : 
     364                 :            : struct spdk_nvmf_tcp_transport {
     365                 :            :         struct spdk_nvmf_transport              transport;
     366                 :            :         struct tcp_transport_opts               tcp_opts;
     367                 :            : 
     368                 :            :         struct spdk_nvmf_tcp_poll_group         *next_pg;
     369                 :            : 
     370                 :            :         struct spdk_poller                      *accept_poller;
     371                 :            : 
     372                 :            :         TAILQ_HEAD(, spdk_nvmf_tcp_port)        ports;
     373                 :            :         TAILQ_HEAD(, spdk_nvmf_tcp_poll_group)  poll_groups;
     374                 :            : 
     375                 :            :         TAILQ_HEAD(, tcp_psk_entry)             psks;
     376                 :            : };
     377                 :            : 
     378                 :            : static const struct spdk_json_object_decoder tcp_transport_opts_decoder[] = {
     379                 :            :         {
     380                 :            :                 "c2h_success", offsetof(struct tcp_transport_opts, c2h_success),
     381                 :            :                 spdk_json_decode_bool, true
     382                 :            :         },
     383                 :            :         {
     384                 :            :                 "control_msg_num", offsetof(struct tcp_transport_opts, control_msg_num),
     385                 :            :                 spdk_json_decode_uint16, true
     386                 :            :         },
     387                 :            :         {
     388                 :            :                 "sock_priority", offsetof(struct tcp_transport_opts, sock_priority),
     389                 :            :                 spdk_json_decode_uint32, true
     390                 :            :         },
     391                 :            : };
     392                 :            : 
     393                 :            : static bool nvmf_tcp_req_process(struct spdk_nvmf_tcp_transport *ttransport,
     394                 :            :                                  struct spdk_nvmf_tcp_req *tcp_req);
     395                 :            : static void nvmf_tcp_poll_group_destroy(struct spdk_nvmf_transport_poll_group *group);
     396                 :            : 
     397                 :            : static void _nvmf_tcp_send_c2h_data(struct spdk_nvmf_tcp_qpair *tqpair,
     398                 :            :                                     struct spdk_nvmf_tcp_req *tcp_req);
     399                 :            : 
     400                 :            : static inline void
     401                 :  147161885 : nvmf_tcp_req_set_state(struct spdk_nvmf_tcp_req *tcp_req,
     402                 :            :                        enum spdk_nvmf_tcp_req_state state)
     403                 :            : {
     404                 :            :         struct spdk_nvmf_qpair *qpair;
     405                 :            :         struct spdk_nvmf_tcp_qpair *tqpair;
     406                 :            : 
     407                 :  147161885 :         qpair = tcp_req->req.qpair;
     408                 :  147161885 :         tqpair = SPDK_CONTAINEROF(qpair, struct spdk_nvmf_tcp_qpair, qpair);
     409                 :            : 
     410         [ -  + ]:  147161885 :         assert(tqpair->state_cntr[tcp_req->state] > 0);
     411                 :  147161885 :         tqpair->state_cntr[tcp_req->state]--;
     412                 :  147161885 :         tqpair->state_cntr[state]++;
     413                 :            : 
     414                 :  147161885 :         tcp_req->state = state;
     415                 :  147161885 : }
     416                 :            : 
     417                 :            : static inline struct nvme_tcp_pdu *
     418                 :   25233648 : nvmf_tcp_req_pdu_init(struct spdk_nvmf_tcp_req *tcp_req)
     419                 :            : {
     420   [ -  +  -  + ]:   25233648 :         assert(tcp_req->pdu_in_use == false);
     421                 :            : 
     422         [ -  + ]:   25233648 :         memset(tcp_req->pdu, 0, sizeof(*tcp_req->pdu));
     423                 :   25233648 :         tcp_req->pdu->qpair = SPDK_CONTAINEROF(tcp_req->req.qpair, struct spdk_nvmf_tcp_qpair, qpair);
     424                 :            : 
     425                 :   25233648 :         return tcp_req->pdu;
     426                 :            : }
     427                 :            : 
     428                 :            : static struct spdk_nvmf_tcp_req *
     429                 :   15859922 : nvmf_tcp_req_get(struct spdk_nvmf_tcp_qpair *tqpair)
     430                 :            : {
     431                 :            :         struct spdk_nvmf_tcp_req *tcp_req;
     432                 :            : 
     433                 :   15859922 :         tcp_req = TAILQ_FIRST(&tqpair->tcp_req_free_queue);
     434         [ +  + ]:   15859922 :         if (spdk_unlikely(!tcp_req)) {
     435                 :      31137 :                 return NULL;
     436                 :            :         }
     437                 :            : 
     438         [ -  + ]:   15828785 :         memset(&tcp_req->rsp, 0, sizeof(tcp_req->rsp));
     439                 :   15828785 :         tcp_req->h2c_offset = 0;
     440                 :   15828785 :         tcp_req->has_in_capsule_data = false;
     441                 :   15828785 :         tcp_req->req.dif_enabled = false;
     442                 :   15828785 :         tcp_req->req.zcopy_phase = NVMF_ZCOPY_PHASE_NONE;
     443                 :            : 
     444         [ +  + ]:   15828785 :         TAILQ_REMOVE(&tqpair->tcp_req_free_queue, tcp_req, state_link);
     445                 :   15828785 :         TAILQ_INSERT_TAIL(&tqpair->tcp_req_working_queue, tcp_req, state_link);
     446                 :   15828785 :         nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_NEW);
     447                 :   15828785 :         return tcp_req;
     448                 :            : }
     449                 :            : 
     450                 :            : static inline void
     451                 :   15828781 : nvmf_tcp_req_put(struct spdk_nvmf_tcp_qpair *tqpair, struct spdk_nvmf_tcp_req *tcp_req)
     452                 :            : {
     453   [ -  +  -  + ]:   15828781 :         assert(!tcp_req->pdu_in_use);
     454                 :            : 
     455         [ +  + ]:   15828781 :         TAILQ_REMOVE(&tqpair->tcp_req_working_queue, tcp_req, state_link);
     456                 :   15828781 :         TAILQ_INSERT_TAIL(&tqpair->tcp_req_free_queue, tcp_req, state_link);
     457                 :   15828781 :         nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_FREE);
     458                 :   15828781 : }
     459                 :            : 
     460                 :            : static void
     461                 :   15515940 : nvmf_tcp_request_free(void *cb_arg)
     462                 :            : {
     463                 :            :         struct spdk_nvmf_tcp_transport *ttransport;
     464                 :   15515940 :         struct spdk_nvmf_tcp_req *tcp_req = cb_arg;
     465                 :            : 
     466         [ -  + ]:   15515940 :         assert(tcp_req != NULL);
     467                 :            : 
     468   [ -  +  -  + ]:   15515940 :         SPDK_DEBUGLOG(nvmf_tcp, "tcp_req=%p will be freed\n", tcp_req);
     469                 :   15515940 :         ttransport = SPDK_CONTAINEROF(tcp_req->req.qpair->transport,
     470                 :            :                                       struct spdk_nvmf_tcp_transport, transport);
     471                 :   15515940 :         nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_COMPLETED);
     472                 :   15515940 :         nvmf_tcp_req_process(ttransport, tcp_req);
     473                 :   15515940 : }
     474                 :            : 
     475                 :            : static int
     476                 :       3242 : nvmf_tcp_req_free(struct spdk_nvmf_request *req)
     477                 :            : {
     478                 :       3242 :         struct spdk_nvmf_tcp_req *tcp_req = SPDK_CONTAINEROF(req, struct spdk_nvmf_tcp_req, req);
     479                 :            : 
     480                 :       3242 :         nvmf_tcp_request_free(tcp_req);
     481                 :            : 
     482                 :       3242 :         return 0;
     483                 :            : }
     484                 :            : 
     485                 :            : static void
     486                 :      49176 : nvmf_tcp_drain_state_queue(struct spdk_nvmf_tcp_qpair *tqpair,
     487                 :            :                            enum spdk_nvmf_tcp_req_state state)
     488                 :            : {
     489                 :            :         struct spdk_nvmf_tcp_req *tcp_req, *req_tmp;
     490                 :            : 
     491         [ -  + ]:      49176 :         assert(state != TCP_REQUEST_STATE_FREE);
     492         [ +  + ]:      51304 :         TAILQ_FOREACH_SAFE(tcp_req, &tqpair->tcp_req_working_queue, state_link, req_tmp) {
     493         [ +  + ]:       2128 :                 if (state == tcp_req->state) {
     494                 :       1436 :                         nvmf_tcp_request_free(tcp_req);
     495                 :            :                 }
     496                 :            :         }
     497                 :      49176 : }
     498                 :            : 
     499                 :            : static void
     500                 :       8196 : nvmf_tcp_cleanup_all_states(struct spdk_nvmf_tcp_qpair *tqpair)
     501                 :            : {
     502                 :            :         struct spdk_nvmf_tcp_req *tcp_req, *req_tmp;
     503                 :            : 
     504                 :       8196 :         nvmf_tcp_drain_state_queue(tqpair, TCP_REQUEST_STATE_TRANSFERRING_CONTROLLER_TO_HOST);
     505                 :       8196 :         nvmf_tcp_drain_state_queue(tqpair, TCP_REQUEST_STATE_NEW);
     506                 :            : 
     507                 :            :         /* Wipe the requests waiting for buffer from the global list */
     508         [ +  + ]:       8342 :         TAILQ_FOREACH_SAFE(tcp_req, &tqpair->tcp_req_working_queue, state_link, req_tmp) {
     509         [ -  + ]:        146 :                 if (tcp_req->state == TCP_REQUEST_STATE_NEED_BUFFER) {
     510   [ #  #  #  #  :          0 :                         STAILQ_REMOVE(&tqpair->group->group.pending_buf_queue, &tcp_req->req,
             #  #  #  # ]
     511                 :            :                                       spdk_nvmf_request, buf_link);
     512                 :            :                 }
     513                 :            :         }
     514                 :            : 
     515                 :       8196 :         nvmf_tcp_drain_state_queue(tqpair, TCP_REQUEST_STATE_NEED_BUFFER);
     516                 :       8196 :         nvmf_tcp_drain_state_queue(tqpair, TCP_REQUEST_STATE_EXECUTING);
     517                 :       8196 :         nvmf_tcp_drain_state_queue(tqpair, TCP_REQUEST_STATE_TRANSFERRING_HOST_TO_CONTROLLER);
     518                 :       8196 :         nvmf_tcp_drain_state_queue(tqpair, TCP_REQUEST_STATE_AWAITING_R2T_ACK);
     519                 :       8196 : }
     520                 :            : 
     521                 :            : static void
     522                 :          0 : nvmf_tcp_dump_qpair_req_contents(struct spdk_nvmf_tcp_qpair *tqpair)
     523                 :            : {
     524                 :            :         int i;
     525                 :            :         struct spdk_nvmf_tcp_req *tcp_req;
     526                 :            : 
     527                 :          0 :         SPDK_ERRLOG("Dumping contents of queue pair (QID %d)\n", tqpair->qpair.qid);
     528         [ #  # ]:          0 :         for (i = 1; i < TCP_REQUEST_NUM_STATES; i++) {
     529                 :          0 :                 SPDK_ERRLOG("\tNum of requests in state[%d] = %u\n", i, tqpair->state_cntr[i]);
     530         [ #  # ]:          0 :                 TAILQ_FOREACH(tcp_req, &tqpair->tcp_req_working_queue, state_link) {
     531         [ #  # ]:          0 :                         if ((int)tcp_req->state == i) {
     532                 :          0 :                                 SPDK_ERRLOG("\t\tRequest Data From Pool: %d\n", tcp_req->req.data_from_pool);
     533                 :          0 :                                 SPDK_ERRLOG("\t\tRequest opcode: %d\n", tcp_req->req.cmd->nvmf_cmd.opcode);
     534                 :            :                         }
     535                 :            :                 }
     536                 :            :         }
     537                 :          0 : }
     538                 :            : 
     539                 :            : static void
     540                 :       8196 : _nvmf_tcp_qpair_destroy(void *_tqpair)
     541                 :            : {
     542                 :       8196 :         struct spdk_nvmf_tcp_qpair *tqpair = _tqpair;
     543                 :       8196 :         spdk_nvmf_transport_qpair_fini_cb cb_fn = tqpair->fini_cb_fn;
     544                 :       8196 :         void *cb_arg = tqpair->fini_cb_arg;
     545                 :       8196 :         int err = 0;
     546                 :            : 
     547   [ +  +  +  + ]:       8196 :         spdk_trace_record(TRACE_TCP_QP_DESTROY, 0, 0, (uintptr_t)tqpair);
     548                 :            : 
     549   [ -  +  -  + ]:       8196 :         SPDK_DEBUGLOG(nvmf_tcp, "enter\n");
     550                 :            : 
     551                 :       8196 :         err = spdk_sock_close(&tqpair->sock);
     552         [ -  + ]:       8196 :         assert(err == 0);
     553                 :       8196 :         nvmf_tcp_cleanup_all_states(tqpair);
     554                 :            : 
     555         [ -  + ]:       8196 :         if (tqpair->state_cntr[TCP_REQUEST_STATE_FREE] != tqpair->resource_count) {
     556                 :          0 :                 SPDK_ERRLOG("tqpair(%p) free tcp request num is %u but should be %u\n", tqpair,
     557                 :            :                             tqpair->state_cntr[TCP_REQUEST_STATE_FREE],
     558                 :            :                             tqpair->resource_count);
     559                 :          0 :                 err++;
     560                 :            :         }
     561                 :            : 
     562         [ -  + ]:       8196 :         if (err > 0) {
     563                 :          0 :                 nvmf_tcp_dump_qpair_req_contents(tqpair);
     564                 :            :         }
     565                 :            : 
     566                 :            :         /* The timeout poller might still be registered here if we close the qpair before host
     567                 :            :          * terminates the connection.
     568                 :            :          */
     569                 :       8196 :         spdk_poller_unregister(&tqpair->timeout_poller);
     570                 :       8196 :         spdk_dma_free(tqpair->pdus);
     571                 :       8196 :         free(tqpair->reqs);
     572                 :       8196 :         spdk_free(tqpair->bufs);
     573                 :       8196 :         free(tqpair);
     574                 :            : 
     575         [ +  + ]:       8196 :         if (cb_fn != NULL) {
     576                 :       8192 :                 cb_fn(cb_arg);
     577                 :            :         }
     578                 :            : 
     579   [ -  +  -  + ]:       8196 :         SPDK_DEBUGLOG(nvmf_tcp, "Leave\n");
     580                 :       8196 : }
     581                 :            : 
     582                 :            : static void
     583                 :       8196 : nvmf_tcp_qpair_destroy(struct spdk_nvmf_tcp_qpair *tqpair)
     584                 :            : {
     585                 :            :         /* Delay the destruction to make sure it isn't performed from the context of a sock
     586                 :            :          * callback.  Otherwise, spdk_sock_close() might not abort pending requests, causing their
     587                 :            :          * completions to be executed after the qpair is freed.  (Note: this fixed issue #2471.)
     588                 :            :          */
     589                 :       8196 :         spdk_thread_send_msg(spdk_get_thread(), _nvmf_tcp_qpair_destroy, tqpair);
     590                 :       8196 : }
     591                 :            : 
     592                 :            : static void
     593                 :         99 : nvmf_tcp_dump_opts(struct spdk_nvmf_transport *transport, struct spdk_json_write_ctx *w)
     594                 :            : {
     595                 :            :         struct spdk_nvmf_tcp_transport  *ttransport;
     596         [ -  + ]:         99 :         assert(w != NULL);
     597                 :            : 
     598                 :         99 :         ttransport = SPDK_CONTAINEROF(transport, struct spdk_nvmf_tcp_transport, transport);
     599         [ -  + ]:         99 :         spdk_json_write_named_bool(w, "c2h_success", ttransport->tcp_opts.c2h_success);
     600                 :         99 :         spdk_json_write_named_uint32(w, "sock_priority", ttransport->tcp_opts.sock_priority);
     601                 :         99 : }
     602                 :            : 
     603                 :            : static int
     604                 :        176 : nvmf_tcp_destroy(struct spdk_nvmf_transport *transport,
     605                 :            :                  spdk_nvmf_transport_destroy_done_cb cb_fn, void *cb_arg)
     606                 :            : {
     607                 :            :         struct spdk_nvmf_tcp_transport  *ttransport;
     608                 :            :         struct tcp_psk_entry *entry, *tmp;
     609                 :            : 
     610         [ -  + ]:        176 :         assert(transport != NULL);
     611                 :        176 :         ttransport = SPDK_CONTAINEROF(transport, struct spdk_nvmf_tcp_transport, transport);
     612                 :            : 
     613         [ -  + ]:        176 :         TAILQ_FOREACH_SAFE(entry, &ttransport->psks, link, tmp) {
     614         [ #  # ]:          0 :                 TAILQ_REMOVE(&ttransport->psks, entry, link);
     615                 :          0 :                 free(entry);
     616                 :            :         }
     617                 :            : 
     618                 :        176 :         spdk_poller_unregister(&ttransport->accept_poller);
     619                 :        176 :         free(ttransport);
     620                 :            : 
     621         [ +  + ]:        176 :         if (cb_fn) {
     622                 :        156 :                 cb_fn(cb_arg);
     623                 :            :         }
     624                 :        176 :         return 0;
     625                 :            : }
     626                 :            : 
     627                 :            : static int nvmf_tcp_accept(void *ctx);
     628                 :            : 
     629                 :            : static struct spdk_nvmf_transport *
     630                 :        180 : nvmf_tcp_create(struct spdk_nvmf_transport_opts *opts)
     631                 :            : {
     632                 :            :         struct spdk_nvmf_tcp_transport *ttransport;
     633                 :            :         uint32_t sge_count;
     634                 :            :         uint32_t min_shared_buffers;
     635                 :            : 
     636                 :        180 :         ttransport = calloc(1, sizeof(*ttransport));
     637         [ -  + ]:        180 :         if (!ttransport) {
     638                 :          0 :                 return NULL;
     639                 :            :         }
     640                 :            : 
     641                 :        180 :         TAILQ_INIT(&ttransport->ports);
     642                 :        180 :         TAILQ_INIT(&ttransport->poll_groups);
     643                 :        180 :         TAILQ_INIT(&ttransport->psks);
     644                 :            : 
     645                 :        180 :         ttransport->transport.ops = &spdk_nvmf_transport_tcp;
     646                 :            : 
     647                 :        180 :         ttransport->tcp_opts.c2h_success = SPDK_NVMF_TCP_DEFAULT_SUCCESS_OPTIMIZATION;
     648                 :        180 :         ttransport->tcp_opts.sock_priority = SPDK_NVMF_TCP_DEFAULT_SOCK_PRIORITY;
     649                 :        180 :         ttransport->tcp_opts.control_msg_num = SPDK_NVMF_TCP_DEFAULT_CONTROL_MSG_NUM;
     650   [ +  +  -  + ]:        336 :         if (opts->transport_specific != NULL &&
     651                 :        156 :             spdk_json_decode_object_relaxed(opts->transport_specific, tcp_transport_opts_decoder,
     652                 :            :                                             SPDK_COUNTOF(tcp_transport_opts_decoder),
     653                 :        156 :                                             &ttransport->tcp_opts)) {
     654                 :          0 :                 SPDK_ERRLOG("spdk_json_decode_object_relaxed failed\n");
     655                 :          0 :                 free(ttransport);
     656                 :          0 :                 return NULL;
     657                 :            :         }
     658                 :            : 
     659                 :        180 :         SPDK_NOTICELOG("*** TCP Transport Init ***\n");
     660                 :            : 
     661   [ -  +  -  +  :        180 :         SPDK_INFOLOG(nvmf_tcp, "*** TCP Transport Init ***\n"
             -  -  -  - ]
     662                 :            :                      "  Transport opts:  max_ioq_depth=%d, max_io_size=%d,\n"
     663                 :            :                      "  max_io_qpairs_per_ctrlr=%d, io_unit_size=%d,\n"
     664                 :            :                      "  in_capsule_data_size=%d, max_aq_depth=%d\n"
     665                 :            :                      "  num_shared_buffers=%d, c2h_success=%d,\n"
     666                 :            :                      "  dif_insert_or_strip=%d, sock_priority=%d\n"
     667                 :            :                      "  abort_timeout_sec=%d, control_msg_num=%hu\n",
     668                 :            :                      opts->max_queue_depth,
     669                 :            :                      opts->max_io_size,
     670                 :            :                      opts->max_qpairs_per_ctrlr - 1,
     671                 :            :                      opts->io_unit_size,
     672                 :            :                      opts->in_capsule_data_size,
     673                 :            :                      opts->max_aq_depth,
     674                 :            :                      opts->num_shared_buffers,
     675                 :            :                      ttransport->tcp_opts.c2h_success,
     676                 :            :                      opts->dif_insert_or_strip,
     677                 :            :                      ttransport->tcp_opts.sock_priority,
     678                 :            :                      opts->abort_timeout_sec,
     679                 :            :                      ttransport->tcp_opts.control_msg_num);
     680                 :            : 
     681         [ -  + ]:        180 :         if (ttransport->tcp_opts.sock_priority > SPDK_NVMF_TCP_DEFAULT_MAX_SOCK_PRIORITY) {
     682                 :          0 :                 SPDK_ERRLOG("Unsupported socket_priority=%d, the current range is: 0 to %d\n"
     683                 :            :                             "you can use man 7 socket to view the range of priority under SO_PRIORITY item\n",
     684                 :            :                             ttransport->tcp_opts.sock_priority, SPDK_NVMF_TCP_DEFAULT_MAX_SOCK_PRIORITY);
     685                 :          0 :                 free(ttransport);
     686                 :          0 :                 return NULL;
     687                 :            :         }
     688                 :            : 
     689         [ -  + ]:        180 :         if (ttransport->tcp_opts.control_msg_num == 0 &&
     690         [ #  # ]:          0 :             opts->in_capsule_data_size < SPDK_NVME_TCP_IN_CAPSULE_DATA_MAX_SIZE) {
     691                 :          0 :                 SPDK_WARNLOG("TCP param control_msg_num can't be 0 if ICD is less than %u bytes. Using default value %u\n",
     692                 :            :                              SPDK_NVME_TCP_IN_CAPSULE_DATA_MAX_SIZE, SPDK_NVMF_TCP_DEFAULT_CONTROL_MSG_NUM);
     693                 :          0 :                 ttransport->tcp_opts.control_msg_num = SPDK_NVMF_TCP_DEFAULT_CONTROL_MSG_NUM;
     694                 :            :         }
     695                 :            : 
     696                 :            :         /* I/O unit size cannot be larger than max I/O size */
     697         [ +  + ]:        180 :         if (opts->io_unit_size > opts->max_io_size) {
     698                 :          4 :                 SPDK_WARNLOG("TCP param io_unit_size %u can't be larger than max_io_size %u. Using max_io_size as io_unit_size\n",
     699                 :            :                              opts->io_unit_size, opts->max_io_size);
     700                 :          4 :                 opts->io_unit_size = opts->max_io_size;
     701                 :            :         }
     702                 :            : 
     703                 :            :         /* In capsule data size cannot be larger than max I/O size */
     704         [ -  + ]:        180 :         if (opts->in_capsule_data_size > opts->max_io_size) {
     705                 :          0 :                 SPDK_WARNLOG("TCP param ICD size %u can't be larger than max_io_size %u. Using max_io_size as ICD size\n",
     706                 :            :                              opts->io_unit_size, opts->max_io_size);
     707                 :          0 :                 opts->in_capsule_data_size = opts->max_io_size;
     708                 :            :         }
     709                 :            : 
     710                 :            :         /* max IO queue depth cannot be smaller than 2 or larger than 65535.
     711                 :            :          * We will not check SPDK_NVMF_TCP_MAX_IO_QUEUE_DEPTH, because max_queue_depth is 16bits and always not larger than 64k. */
     712         [ -  + ]:        180 :         if (opts->max_queue_depth < SPDK_NVMF_TCP_MIN_IO_QUEUE_DEPTH) {
     713                 :          0 :                 SPDK_WARNLOG("TCP param max_queue_depth %u can't be smaller than %u or larger than %u. Using default value %u\n",
     714                 :            :                              opts->max_queue_depth, SPDK_NVMF_TCP_MIN_IO_QUEUE_DEPTH,
     715                 :            :                              SPDK_NVMF_TCP_MAX_IO_QUEUE_DEPTH, SPDK_NVMF_TCP_DEFAULT_MAX_IO_QUEUE_DEPTH);
     716                 :          0 :                 opts->max_queue_depth = SPDK_NVMF_TCP_DEFAULT_MAX_IO_QUEUE_DEPTH;
     717                 :            :         }
     718                 :            : 
     719                 :            :         /* max admin queue depth cannot be smaller than 2 or larger than 4096 */
     720         [ +  - ]:        180 :         if (opts->max_aq_depth < SPDK_NVMF_TCP_MIN_ADMIN_QUEUE_DEPTH ||
     721         [ -  + ]:        180 :             opts->max_aq_depth > SPDK_NVMF_TCP_MAX_ADMIN_QUEUE_DEPTH) {
     722                 :          0 :                 SPDK_WARNLOG("TCP param max_aq_depth %u can't be smaller than %u or larger than %u. Using default value %u\n",
     723                 :            :                              opts->max_aq_depth, SPDK_NVMF_TCP_MIN_ADMIN_QUEUE_DEPTH,
     724                 :            :                              SPDK_NVMF_TCP_MAX_ADMIN_QUEUE_DEPTH, SPDK_NVMF_TCP_DEFAULT_MAX_ADMIN_QUEUE_DEPTH);
     725                 :          0 :                 opts->max_aq_depth = SPDK_NVMF_TCP_DEFAULT_MAX_ADMIN_QUEUE_DEPTH;
     726                 :            :         }
     727                 :            : 
     728         [ -  + ]:        180 :         sge_count = opts->max_io_size / opts->io_unit_size;
     729         [ +  + ]:        180 :         if (sge_count > SPDK_NVMF_MAX_SGL_ENTRIES) {
     730                 :          4 :                 SPDK_ERRLOG("Unsupported IO Unit size specified, %d bytes\n", opts->io_unit_size);
     731                 :          4 :                 free(ttransport);
     732                 :          4 :                 return NULL;
     733                 :            :         }
     734                 :            : 
     735                 :            :         /* If buf_cache_size == UINT32_MAX, we will dynamically pick a cache size later that we know will fit. */
     736         [ +  + ]:        176 :         if (opts->buf_cache_size < UINT32_MAX) {
     737                 :         20 :                 min_shared_buffers = spdk_env_get_core_count() * opts->buf_cache_size;
     738         [ -  + ]:         20 :                 if (min_shared_buffers > opts->num_shared_buffers) {
     739                 :          0 :                         SPDK_ERRLOG("There are not enough buffers to satisfy "
     740                 :            :                                     "per-poll group caches for each thread. (%" PRIu32 ") "
     741                 :            :                                     "supplied. (%" PRIu32 ") required\n", opts->num_shared_buffers, min_shared_buffers);
     742                 :          0 :                         SPDK_ERRLOG("Please specify a larger number of shared buffers\n");
     743                 :          0 :                         free(ttransport);
     744                 :          0 :                         return NULL;
     745                 :            :                 }
     746                 :            :         }
     747                 :            : 
     748                 :        176 :         ttransport->accept_poller = SPDK_POLLER_REGISTER(nvmf_tcp_accept, &ttransport->transport,
     749                 :            :                                     opts->acceptor_poll_rate);
     750         [ -  + ]:        176 :         if (!ttransport->accept_poller) {
     751                 :          0 :                 free(ttransport);
     752                 :          0 :                 return NULL;
     753                 :            :         }
     754                 :            : 
     755                 :        176 :         return &ttransport->transport;
     756                 :            : }
     757                 :            : 
     758                 :            : static int
     759                 :       1076 : nvmf_tcp_trsvcid_to_int(const char *trsvcid)
     760                 :            : {
     761                 :            :         unsigned long long ull;
     762                 :       1076 :         char *end = NULL;
     763                 :            : 
     764         [ -  + ]:       1076 :         ull = strtoull(trsvcid, &end, 10);
     765   [ +  -  +  -  :       1076 :         if (end == NULL || end == trsvcid || *end != '\0') {
                   -  + ]
     766                 :          0 :                 return -1;
     767                 :            :         }
     768                 :            : 
     769                 :            :         /* Valid TCP/IP port numbers are in [1, 65535] */
     770   [ +  -  -  + ]:       1076 :         if (ull == 0 || ull > 65535) {
     771                 :          0 :                 return -1;
     772                 :            :         }
     773                 :            : 
     774                 :       1076 :         return (int)ull;
     775                 :            : }
     776                 :            : 
     777                 :            : /**
     778                 :            :  * Canonicalize a listen address trid.
     779                 :            :  */
     780                 :            : static int
     781                 :        824 : nvmf_tcp_canon_listen_trid(struct spdk_nvme_transport_id *canon_trid,
     782                 :            :                            const struct spdk_nvme_transport_id *trid)
     783                 :            : {
     784                 :            :         int trsvcid_int;
     785                 :            : 
     786                 :        824 :         trsvcid_int = nvmf_tcp_trsvcid_to_int(trid->trsvcid);
     787         [ -  + ]:        824 :         if (trsvcid_int < 0) {
     788                 :          0 :                 return -EINVAL;
     789                 :            :         }
     790                 :            : 
     791         [ -  + ]:        824 :         memset(canon_trid, 0, sizeof(*canon_trid));
     792                 :        824 :         spdk_nvme_trid_populate_transport(canon_trid, SPDK_NVME_TRANSPORT_TCP);
     793                 :        824 :         canon_trid->adrfam = trid->adrfam;
     794         [ -  + ]:        824 :         snprintf(canon_trid->traddr, sizeof(canon_trid->traddr), "%s", trid->traddr);
     795         [ -  + ]:        824 :         snprintf(canon_trid->trsvcid, sizeof(canon_trid->trsvcid), "%d", trsvcid_int);
     796                 :            : 
     797                 :        824 :         return 0;
     798                 :            : }
     799                 :            : 
     800                 :            : /**
     801                 :            :  * Find an existing listening port.
     802                 :            :  */
     803                 :            : static struct spdk_nvmf_tcp_port *
     804                 :        824 : nvmf_tcp_find_port(struct spdk_nvmf_tcp_transport *ttransport,
     805                 :            :                    const struct spdk_nvme_transport_id *trid)
     806                 :            : {
     807                 :        137 :         struct spdk_nvme_transport_id canon_trid;
     808                 :            :         struct spdk_nvmf_tcp_port *port;
     809                 :            : 
     810         [ -  + ]:        824 :         if (nvmf_tcp_canon_listen_trid(&canon_trid, trid) != 0) {
     811                 :          0 :                 return NULL;
     812                 :            :         }
     813                 :            : 
     814         [ +  - ]:       1254 :         TAILQ_FOREACH(port, &ttransport->ports, link) {
     815         [ +  + ]:       1254 :                 if (spdk_nvme_transport_id_compare(&canon_trid, port->trid) == 0) {
     816                 :        824 :                         return port;
     817                 :            :                 }
     818                 :            :         }
     819                 :            : 
     820                 :          0 :         return NULL;
     821                 :            : }
     822                 :            : 
     823                 :            : static int
     824                 :         61 : tcp_sock_get_key(uint8_t *out, int out_len, const char **cipher, const char *psk_identity,
     825                 :            :                  void *get_key_ctx)
     826                 :            : {
     827                 :            :         struct tcp_psk_entry *entry;
     828                 :         61 :         struct spdk_nvmf_tcp_transport *ttransport = get_key_ctx;
     829                 :            :         size_t psk_len;
     830                 :            :         int rc;
     831                 :            : 
     832         [ +  + ]:         67 :         TAILQ_FOREACH(entry, &ttransport->psks, link) {
     833   [ -  +  -  +  :         61 :                 if (strcmp(psk_identity, entry->psk_identity) != 0) {
                   +  + ]
     834                 :          6 :                         continue;
     835                 :            :                 }
     836                 :            : 
     837                 :         55 :                 psk_len = entry->psk_size;
     838         [ -  + ]:         55 :                 if ((size_t)out_len < psk_len) {
     839                 :          0 :                         SPDK_ERRLOG("Out buffer of size: %" PRIu32 " cannot fit PSK of len: %lu\n",
     840                 :            :                                     out_len, psk_len);
     841                 :          0 :                         return -ENOBUFS;
     842                 :            :                 }
     843                 :            : 
     844                 :            :                 /* Convert PSK to the TLS PSK format. */
     845                 :         55 :                 rc = nvme_tcp_derive_tls_psk(entry->psk, psk_len, psk_identity, out, out_len,
     846                 :            :                                              entry->tls_cipher_suite);
     847         [ -  + ]:         55 :                 if (rc < 0) {
     848                 :          0 :                         SPDK_ERRLOG("Could not generate TLS PSK\n");
     849                 :            :                 }
     850                 :            : 
     851      [ +  +  - ]:         55 :                 switch (entry->tls_cipher_suite) {
     852                 :         31 :                 case NVME_TCP_CIPHER_AES_128_GCM_SHA256:
     853                 :         31 :                         *cipher = "TLS_AES_128_GCM_SHA256";
     854                 :         31 :                         break;
     855                 :         24 :                 case NVME_TCP_CIPHER_AES_256_GCM_SHA384:
     856                 :         24 :                         *cipher = "TLS_AES_256_GCM_SHA384";
     857                 :         24 :                         break;
     858                 :          0 :                 default:
     859                 :          0 :                         *cipher = NULL;
     860                 :          0 :                         return -ENOTSUP;
     861                 :            :                 }
     862                 :            : 
     863                 :         55 :                 return rc;
     864                 :            :         }
     865                 :            : 
     866                 :          6 :         SPDK_ERRLOG("Could not find PSK for identity: %s\n", psk_identity);
     867                 :            : 
     868                 :          6 :         return -EINVAL;
     869                 :            : }
     870                 :            : 
     871                 :            : static int
     872                 :        252 : nvmf_tcp_listen(struct spdk_nvmf_transport *transport, const struct spdk_nvme_transport_id *trid,
     873                 :            :                 struct spdk_nvmf_listen_opts *listen_opts)
     874                 :            : {
     875                 :            :         struct spdk_nvmf_tcp_transport *ttransport;
     876                 :            :         struct spdk_nvmf_tcp_port *port;
     877                 :            :         int trsvcid_int;
     878                 :            :         uint8_t adrfam;
     879                 :            :         const char *sock_impl_name;
     880                 :         20 :         struct spdk_sock_impl_opts impl_opts;
     881                 :        252 :         size_t impl_opts_size = sizeof(impl_opts);
     882                 :         20 :         struct spdk_sock_opts opts;
     883                 :            : 
     884         [ -  + ]:        252 :         if (!strlen(trid->trsvcid)) {
     885                 :          0 :                 SPDK_ERRLOG("Service id is required\n");
     886                 :          0 :                 return -EINVAL;
     887                 :            :         }
     888                 :            : 
     889                 :        252 :         ttransport = SPDK_CONTAINEROF(transport, struct spdk_nvmf_tcp_transport, transport);
     890                 :            : 
     891                 :        252 :         trsvcid_int = nvmf_tcp_trsvcid_to_int(trid->trsvcid);
     892         [ -  + ]:        252 :         if (trsvcid_int < 0) {
     893                 :          0 :                 SPDK_ERRLOG("Invalid trsvcid '%s'\n", trid->trsvcid);
     894                 :          0 :                 return -EINVAL;
     895                 :            :         }
     896                 :            : 
     897                 :        252 :         port = calloc(1, sizeof(*port));
     898         [ -  + ]:        252 :         if (!port) {
     899                 :          0 :                 SPDK_ERRLOG("Port allocation failed\n");
     900                 :          0 :                 return -ENOMEM;
     901                 :            :         }
     902                 :            : 
     903                 :        252 :         port->trid = trid;
     904                 :            : 
     905                 :        252 :         sock_impl_name = NULL;
     906                 :            : 
     907                 :        252 :         opts.opts_size = sizeof(opts);
     908                 :        252 :         spdk_sock_get_default_opts(&opts);
     909                 :        252 :         opts.priority = ttransport->tcp_opts.sock_priority;
     910   [ -  +  +  + ]:        252 :         if (listen_opts->secure_channel) {
     911   [ -  +  +  - ]:         20 :                 if (!g_tls_log) {
     912                 :         20 :                         SPDK_NOTICELOG("TLS support is considered experimental\n");
     913                 :         20 :                         g_tls_log = true;
     914                 :            :                 }
     915                 :         20 :                 sock_impl_name = "ssl";
     916                 :         20 :                 spdk_sock_impl_get_opts(sock_impl_name, &impl_opts, &impl_opts_size);
     917                 :         20 :                 impl_opts.tls_version = SPDK_TLS_VERSION_1_3;
     918                 :         20 :                 impl_opts.get_key = tcp_sock_get_key;
     919                 :         20 :                 impl_opts.get_key_ctx = ttransport;
     920                 :         20 :                 impl_opts.tls_cipher_suites = "TLS_AES_256_GCM_SHA384:TLS_AES_128_GCM_SHA256";
     921                 :         20 :                 opts.impl_opts = &impl_opts;
     922                 :         20 :                 opts.impl_opts_size = sizeof(impl_opts);
     923                 :            :         }
     924                 :            : 
     925                 :        252 :         port->listen_sock = spdk_sock_listen_ext(trid->traddr, trsvcid_int,
     926                 :            :                             sock_impl_name, &opts);
     927         [ -  + ]:        252 :         if (port->listen_sock == NULL) {
     928                 :          0 :                 SPDK_ERRLOG("spdk_sock_listen(%s, %d) failed: %s (%d)\n",
     929                 :            :                             trid->traddr, trsvcid_int,
     930                 :            :                             spdk_strerror(errno), errno);
     931                 :          0 :                 free(port);
     932                 :          0 :                 return -errno;
     933                 :            :         }
     934                 :            : 
     935         [ +  - ]:        252 :         if (spdk_sock_is_ipv4(port->listen_sock)) {
     936                 :        252 :                 adrfam = SPDK_NVMF_ADRFAM_IPV4;
     937         [ #  # ]:          0 :         } else if (spdk_sock_is_ipv6(port->listen_sock)) {
     938                 :          0 :                 adrfam = SPDK_NVMF_ADRFAM_IPV6;
     939                 :            :         } else {
     940                 :          0 :                 SPDK_ERRLOG("Unhandled socket type\n");
     941                 :          0 :                 adrfam = 0;
     942                 :            :         }
     943                 :            : 
     944         [ -  + ]:        252 :         if (adrfam != trid->adrfam) {
     945                 :          0 :                 SPDK_ERRLOG("Socket address family mismatch\n");
     946                 :          0 :                 spdk_sock_close(&port->listen_sock);
     947                 :          0 :                 free(port);
     948                 :          0 :                 return -EINVAL;
     949                 :            :         }
     950                 :            : 
     951                 :        252 :         SPDK_NOTICELOG("*** NVMe/TCP Target Listening on %s port %s ***\n",
     952                 :            :                        trid->traddr, trid->trsvcid);
     953                 :            : 
     954                 :        252 :         TAILQ_INSERT_TAIL(&ttransport->ports, port, link);
     955                 :        252 :         return 0;
     956                 :            : }
     957                 :            : 
     958                 :            : static void
     959                 :        252 : nvmf_tcp_stop_listen(struct spdk_nvmf_transport *transport,
     960                 :            :                      const struct spdk_nvme_transport_id *trid)
     961                 :            : {
     962                 :            :         struct spdk_nvmf_tcp_transport *ttransport;
     963                 :            :         struct spdk_nvmf_tcp_port *port;
     964                 :            : 
     965                 :        252 :         ttransport = SPDK_CONTAINEROF(transport, struct spdk_nvmf_tcp_transport, transport);
     966                 :            : 
     967   [ -  +  -  + ]:        252 :         SPDK_DEBUGLOG(nvmf_tcp, "Removing listen address %s port %s\n",
     968                 :            :                       trid->traddr, trid->trsvcid);
     969                 :            : 
     970                 :        252 :         port = nvmf_tcp_find_port(ttransport, trid);
     971         [ +  - ]:        252 :         if (port) {
     972         [ +  + ]:        252 :                 TAILQ_REMOVE(&ttransport->ports, port, link);
     973                 :        252 :                 spdk_sock_close(&port->listen_sock);
     974                 :        252 :                 free(port);
     975                 :            :         }
     976                 :        252 : }
     977                 :            : 
     978                 :            : static void nvmf_tcp_qpair_set_recv_state(struct spdk_nvmf_tcp_qpair *tqpair,
     979                 :            :                 enum nvme_tcp_pdu_recv_state state);
     980                 :            : 
     981                 :            : static void
     982                 :      49019 : nvmf_tcp_qpair_set_state(struct spdk_nvmf_tcp_qpair *tqpair, enum nvme_tcp_qpair_state state)
     983                 :            : {
     984                 :      49019 :         tqpair->state = state;
     985   [ +  +  +  + ]:      49019 :         spdk_trace_record(TRACE_TCP_QP_STATE_CHANGE, tqpair->qpair.qid, 0, (uintptr_t)tqpair,
     986                 :            :                           tqpair->state);
     987                 :      49019 : }
     988                 :            : 
     989                 :            : static void
     990                 :     401060 : nvmf_tcp_qpair_disconnect(struct spdk_nvmf_tcp_qpair *tqpair)
     991                 :            : {
     992   [ -  +  -  + ]:     401060 :         SPDK_DEBUGLOG(nvmf_tcp, "Disconnecting qpair %p\n", tqpair);
     993                 :            : 
     994   [ +  +  +  + ]:     401060 :         spdk_trace_record(TRACE_TCP_QP_DISCONNECT, 0, 0, (uintptr_t)tqpair);
     995                 :            : 
     996         [ +  + ]:     401060 :         if (tqpair->state <= NVME_TCP_QPAIR_STATE_RUNNING) {
     997                 :       8094 :                 nvmf_tcp_qpair_set_state(tqpair, NVME_TCP_QPAIR_STATE_EXITING);
     998         [ -  + ]:       8094 :                 assert(tqpair->recv_state == NVME_TCP_PDU_RECV_STATE_ERROR);
     999                 :       8094 :                 spdk_poller_unregister(&tqpair->timeout_poller);
    1000                 :            : 
    1001                 :            :                 /* This will end up calling nvmf_tcp_close_qpair */
    1002                 :       8094 :                 spdk_nvmf_qpair_disconnect(&tqpair->qpair, NULL, NULL);
    1003                 :            :         }
    1004                 :     401060 : }
    1005                 :            : 
    1006                 :            : static void
    1007                 :      16422 : _mgmt_pdu_write_done(void *_tqpair, int err)
    1008                 :            : {
    1009                 :      16422 :         struct spdk_nvmf_tcp_qpair *tqpair = _tqpair;
    1010                 :      16422 :         struct nvme_tcp_pdu *pdu = tqpair->mgmt_pdu;
    1011                 :            : 
    1012         [ +  + ]:      16422 :         if (spdk_unlikely(err != 0)) {
    1013                 :         64 :                 nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_QUIESCING);
    1014                 :         64 :                 return;
    1015                 :            :         }
    1016                 :            : 
    1017         [ -  + ]:      16358 :         assert(pdu->cb_fn != NULL);
    1018                 :      16358 :         pdu->cb_fn(pdu->cb_arg);
    1019                 :            : }
    1020                 :            : 
    1021                 :            : static void
    1022                 :   25496928 : _req_pdu_write_done(void *req, int err)
    1023                 :            : {
    1024                 :   25496928 :         struct spdk_nvmf_tcp_req *tcp_req = req;
    1025                 :   25496928 :         struct nvme_tcp_pdu *pdu = tcp_req->pdu;
    1026                 :   25496928 :         struct spdk_nvmf_tcp_qpair *tqpair = pdu->qpair;
    1027                 :            : 
    1028   [ -  +  -  + ]:   25496928 :         assert(tcp_req->pdu_in_use);
    1029                 :   25496928 :         tcp_req->pdu_in_use = false;
    1030                 :            : 
    1031                 :            :         /* If the request is in a completed state, we're waiting for write completion to free it */
    1032         [ -  + ]:   25496928 :         if (spdk_unlikely(tcp_req->state == TCP_REQUEST_STATE_COMPLETED)) {
    1033                 :          0 :                 nvmf_tcp_request_free(tcp_req);
    1034                 :          0 :                 return;
    1035                 :            :         }
    1036                 :            : 
    1037         [ +  + ]:   25496928 :         if (spdk_unlikely(err != 0)) {
    1038                 :       1398 :                 nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_QUIESCING);
    1039                 :       1398 :                 return;
    1040                 :            :         }
    1041                 :            : 
    1042         [ -  + ]:   25495530 :         assert(pdu->cb_fn != NULL);
    1043                 :   25495530 :         pdu->cb_fn(pdu->cb_arg);
    1044                 :            : }
    1045                 :            : 
    1046                 :            : static void
    1047                 :       8243 : _pdu_write_done(struct nvme_tcp_pdu *pdu, int err)
    1048                 :            : {
    1049                 :       8243 :         pdu->sock_req.cb_fn(pdu->sock_req.cb_arg, err);
    1050                 :       8243 : }
    1051                 :            : 
    1052                 :            : static void
    1053                 :   25505199 : _tcp_write_pdu(struct nvme_tcp_pdu *pdu)
    1054                 :            : {
    1055                 :            :         int rc;
    1056                 :     488951 :         uint32_t mapped_length;
    1057                 :   25505199 :         struct spdk_nvmf_tcp_qpair *tqpair = pdu->qpair;
    1058                 :            : 
    1059                 :   25505222 :         pdu->sock_req.iovcnt = nvme_tcp_build_iovs(pdu->iov, SPDK_COUNTOF(pdu->iov), pdu,
    1060   [ -  +  -  + ]:   25505199 :                                tqpair->host_hdgst_enable, tqpair->host_ddgst_enable, &mapped_length);
    1061                 :   25505199 :         spdk_sock_writev_async(tqpair->sock, &pdu->sock_req);
    1062                 :            : 
    1063         [ +  + ]:   25505199 :         if (pdu->hdr.common.pdu_type == SPDK_NVME_TCP_PDU_TYPE_IC_RESP ||
    1064         [ +  + ]:   25497016 :             pdu->hdr.common.pdu_type == SPDK_NVME_TCP_PDU_TYPE_C2H_TERM_REQ) {
    1065                 :            :                 /* Try to force the send immediately. */
    1066                 :       8243 :                 rc = spdk_sock_flush(tqpair->sock);
    1067   [ +  +  +  - ]:       8243 :                 if (rc > 0 && (uint32_t)rc == mapped_length) {
    1068                 :       8179 :                         _pdu_write_done(pdu, 0);
    1069                 :            :                 } else {
    1070         [ +  + ]:         64 :                         SPDK_ERRLOG("Could not write %s to socket: rc=%d, errno=%d\n",
    1071                 :            :                                     pdu->hdr.common.pdu_type == SPDK_NVME_TCP_PDU_TYPE_IC_RESP ?
    1072                 :            :                                     "IC_RESP" : "TERM_REQ", rc, errno);
    1073         [ -  + ]:         64 :                         _pdu_write_done(pdu, rc >= 0 ? -EAGAIN : -errno);
    1074                 :            :                 }
    1075                 :            :         }
    1076                 :   25505199 : }
    1077                 :            : 
    1078                 :            : static void
    1079                 :     923335 : data_crc32_accel_done(void *cb_arg, int status)
    1080                 :            : {
    1081                 :     923335 :         struct nvme_tcp_pdu *pdu = cb_arg;
    1082                 :            : 
    1083         [ -  + ]:     923335 :         if (spdk_unlikely(status)) {
    1084                 :          0 :                 SPDK_ERRLOG("Failed to compute the data digest for pdu =%p\n", pdu);
    1085                 :          0 :                 _pdu_write_done(pdu, status);
    1086                 :          0 :                 return;
    1087                 :            :         }
    1088                 :            : 
    1089                 :     923335 :         pdu->data_digest_crc32 ^= SPDK_CRC32C_XOR;
    1090                 :     923335 :         MAKE_DIGEST_WORD(pdu->data_digest, pdu->data_digest_crc32);
    1091                 :            : 
    1092                 :     923335 :         _tcp_write_pdu(pdu);
    1093                 :            : }
    1094                 :            : 
    1095                 :            : static void
    1096                 :   25505199 : pdu_data_crc32_compute(struct nvme_tcp_pdu *pdu)
    1097                 :            : {
    1098                 :   25505199 :         struct spdk_nvmf_tcp_qpair *tqpair = pdu->qpair;
    1099                 :   25505199 :         int rc = 0;
    1100                 :            : 
    1101                 :            :         /* Data Digest */
    1102   [ +  +  +  +  :   25505199 :         if (pdu->data_len > 0 && g_nvme_tcp_ddgst[pdu->hdr.common.pdu_type] && tqpair->host_ddgst_enable) {
          +  +  -  +  +  
                      + ]
    1103                 :            :                 /* Only support this limitated case for the first step */
    1104   [ +  +  +  -  :     923335 :                 if (spdk_likely(!pdu->dif_ctx && (pdu->data_len % SPDK_NVME_TCP_DIGEST_ALIGNMENT == 0)
             +  +  +  - ]
    1105                 :            :                                 && tqpair->group)) {
    1106                 :     729568 :                         rc = spdk_accel_submit_crc32cv(tqpair->group->accel_channel, &pdu->data_digest_crc32, pdu->data_iov,
    1107                 :            :                                                        pdu->data_iovcnt, 0, data_crc32_accel_done, pdu);
    1108         [ +  - ]:     729568 :                         if (spdk_likely(rc == 0)) {
    1109                 :     729568 :                                 return;
    1110                 :            :                         }
    1111                 :            :                 } else {
    1112                 :     193767 :                         pdu->data_digest_crc32 = nvme_tcp_pdu_calc_data_digest(pdu);
    1113                 :            :                 }
    1114                 :     193767 :                 data_crc32_accel_done(pdu, rc);
    1115                 :            :         } else {
    1116                 :   24581864 :                 _tcp_write_pdu(pdu);
    1117                 :            :         }
    1118                 :            : }
    1119                 :            : 
    1120                 :            : static void
    1121                 :   25505199 : nvmf_tcp_qpair_write_pdu(struct spdk_nvmf_tcp_qpair *tqpair,
    1122                 :            :                          struct nvme_tcp_pdu *pdu,
    1123                 :            :                          nvme_tcp_qpair_xfer_complete_cb cb_fn,
    1124                 :            :                          void *cb_arg)
    1125                 :            : {
    1126                 :            :         int hlen;
    1127                 :            :         uint32_t crc32c;
    1128                 :            : 
    1129         [ -  + ]:   25505199 :         assert(tqpair->pdu_in_progress != pdu);
    1130                 :            : 
    1131                 :   25505199 :         hlen = pdu->hdr.common.hlen;
    1132                 :   25505199 :         pdu->cb_fn = cb_fn;
    1133                 :   25505199 :         pdu->cb_arg = cb_arg;
    1134                 :            : 
    1135                 :   25505199 :         pdu->iov[0].iov_base = &pdu->hdr.raw;
    1136                 :   25505199 :         pdu->iov[0].iov_len = hlen;
    1137                 :            : 
    1138                 :            :         /* Header Digest */
    1139   [ +  +  +  +  :   25505199 :         if (g_nvme_tcp_hdgst[pdu->hdr.common.pdu_type] && tqpair->host_hdgst_enable) {
             -  +  +  + ]
    1140                 :     990278 :                 crc32c = nvme_tcp_pdu_calc_header_digest(pdu);
    1141                 :     990278 :                 MAKE_DIGEST_WORD((uint8_t *)pdu->hdr.raw + hlen, crc32c);
    1142                 :            :         }
    1143                 :            : 
    1144                 :            :         /* Data Digest */
    1145                 :   25505199 :         pdu_data_crc32_compute(pdu);
    1146                 :   25505199 : }
    1147                 :            : 
    1148                 :            : static void
    1149                 :       8243 : nvmf_tcp_qpair_write_mgmt_pdu(struct spdk_nvmf_tcp_qpair *tqpair,
    1150                 :            :                               nvme_tcp_qpair_xfer_complete_cb cb_fn,
    1151                 :            :                               void *cb_arg)
    1152                 :            : {
    1153                 :       8243 :         struct nvme_tcp_pdu *pdu = tqpair->mgmt_pdu;
    1154                 :            : 
    1155                 :       8243 :         pdu->sock_req.cb_fn = _mgmt_pdu_write_done;
    1156                 :       8243 :         pdu->sock_req.cb_arg = tqpair;
    1157                 :            : 
    1158                 :       8243 :         nvmf_tcp_qpair_write_pdu(tqpair, pdu, cb_fn, cb_arg);
    1159                 :       8243 : }
    1160                 :            : 
    1161                 :            : static void
    1162                 :   25496956 : nvmf_tcp_qpair_write_req_pdu(struct spdk_nvmf_tcp_qpair *tqpair,
    1163                 :            :                              struct spdk_nvmf_tcp_req *tcp_req,
    1164                 :            :                              nvme_tcp_qpair_xfer_complete_cb cb_fn,
    1165                 :            :                              void *cb_arg)
    1166                 :            : {
    1167                 :   25496956 :         struct nvme_tcp_pdu *pdu = tcp_req->pdu;
    1168                 :            : 
    1169                 :   25496956 :         pdu->sock_req.cb_fn = _req_pdu_write_done;
    1170                 :   25496956 :         pdu->sock_req.cb_arg = tcp_req;
    1171                 :            : 
    1172   [ -  +  -  + ]:   25496956 :         assert(!tcp_req->pdu_in_use);
    1173                 :   25496956 :         tcp_req->pdu_in_use = true;
    1174                 :            : 
    1175                 :   25496956 :         nvmf_tcp_qpair_write_pdu(tqpair, pdu, cb_fn, cb_arg);
    1176                 :   25496956 : }
    1177                 :            : 
    1178                 :            : static int
    1179                 :       8196 : nvmf_tcp_qpair_init_mem_resource(struct spdk_nvmf_tcp_qpair *tqpair)
    1180                 :            : {
    1181                 :            :         uint32_t i;
    1182                 :            :         struct spdk_nvmf_transport_opts *opts;
    1183                 :            :         uint32_t in_capsule_data_size;
    1184                 :            : 
    1185                 :       8196 :         opts = &tqpair->qpair.transport->opts;
    1186                 :            : 
    1187                 :       8196 :         in_capsule_data_size = opts->in_capsule_data_size;
    1188   [ -  +  +  + ]:       8196 :         if (opts->dif_insert_or_strip) {
    1189                 :        171 :                 in_capsule_data_size = SPDK_BDEV_BUF_SIZE_WITH_MD(in_capsule_data_size);
    1190                 :            :         }
    1191                 :            : 
    1192                 :       8196 :         tqpair->resource_count = opts->max_queue_depth;
    1193                 :            : 
    1194                 :       8196 :         tqpair->reqs = calloc(tqpair->resource_count, sizeof(*tqpair->reqs));
    1195         [ -  + ]:       8196 :         if (!tqpair->reqs) {
    1196                 :          0 :                 SPDK_ERRLOG("Unable to allocate reqs on tqpair=%p\n", tqpair);
    1197                 :          0 :                 return -1;
    1198                 :            :         }
    1199                 :            : 
    1200         [ +  + ]:       8196 :         if (in_capsule_data_size) {
    1201                 :       6261 :                 tqpair->bufs = spdk_zmalloc(tqpair->resource_count * in_capsule_data_size, 0x1000,
    1202                 :            :                                             NULL, SPDK_ENV_LCORE_ID_ANY,
    1203                 :            :                                             SPDK_MALLOC_DMA);
    1204         [ -  + ]:       6261 :                 if (!tqpair->bufs) {
    1205                 :          0 :                         SPDK_ERRLOG("Unable to allocate bufs on tqpair=%p.\n", tqpair);
    1206                 :          0 :                         return -1;
    1207                 :            :                 }
    1208                 :            :         }
    1209                 :            :         /* prepare memory space for receiving pdus and tcp_req */
    1210                 :            :         /* Add additional 1 member, which will be used for mgmt_pdu owned by the tqpair */
    1211                 :       8196 :         tqpair->pdus = spdk_dma_zmalloc((2 * tqpair->resource_count + 1) * sizeof(*tqpair->pdus), 0x1000,
    1212                 :            :                                         NULL);
    1213         [ -  + ]:       8196 :         if (!tqpair->pdus) {
    1214                 :          0 :                 SPDK_ERRLOG("Unable to allocate pdu pool on tqpair =%p.\n", tqpair);
    1215                 :          0 :                 return -1;
    1216                 :            :         }
    1217                 :            : 
    1218         [ +  + ]:    1057284 :         for (i = 0; i < tqpair->resource_count; i++) {
    1219                 :    1049088 :                 struct spdk_nvmf_tcp_req *tcp_req = &tqpair->reqs[i];
    1220                 :            : 
    1221                 :    1049088 :                 tcp_req->ttag = i + 1;
    1222                 :    1049088 :                 tcp_req->req.qpair = &tqpair->qpair;
    1223                 :            : 
    1224                 :    1049088 :                 tcp_req->pdu = &tqpair->pdus[i];
    1225                 :    1049088 :                 tcp_req->pdu->qpair = tqpair;
    1226                 :            : 
    1227                 :            :                 /* Set up memory to receive commands */
    1228         [ +  + ]:    1049088 :                 if (tqpair->bufs) {
    1229                 :     801408 :                         tcp_req->buf = (void *)((uintptr_t)tqpair->bufs + (i * in_capsule_data_size));
    1230                 :            :                 }
    1231                 :            : 
    1232                 :            :                 /* Set the cmdn and rsp */
    1233                 :    1049088 :                 tcp_req->req.rsp = (union nvmf_c2h_msg *)&tcp_req->rsp;
    1234                 :    1049088 :                 tcp_req->req.cmd = (union nvmf_h2c_msg *)&tcp_req->cmd;
    1235                 :            : 
    1236                 :    1049088 :                 tcp_req->req.stripped_data = NULL;
    1237                 :            : 
    1238                 :            :                 /* Initialize request state to FREE */
    1239                 :    1049088 :                 tcp_req->state = TCP_REQUEST_STATE_FREE;
    1240                 :    1049088 :                 TAILQ_INSERT_TAIL(&tqpair->tcp_req_free_queue, tcp_req, state_link);
    1241                 :    1049088 :                 tqpair->state_cntr[TCP_REQUEST_STATE_FREE]++;
    1242                 :            :         }
    1243                 :            : 
    1244         [ +  + ]:    1057284 :         for (; i < 2 * tqpair->resource_count; i++) {
    1245                 :    1049088 :                 struct nvme_tcp_pdu *pdu = &tqpair->pdus[i];
    1246                 :            : 
    1247                 :    1049088 :                 pdu->qpair = tqpair;
    1248                 :    1049088 :                 SLIST_INSERT_HEAD(&tqpair->tcp_pdu_free_queue, pdu, slist);
    1249                 :            :         }
    1250                 :            : 
    1251                 :       8196 :         tqpair->mgmt_pdu = &tqpair->pdus[i];
    1252                 :       8196 :         tqpair->mgmt_pdu->qpair = tqpair;
    1253                 :       8196 :         tqpair->pdu_in_progress = SLIST_FIRST(&tqpair->tcp_pdu_free_queue);
    1254                 :       8196 :         SLIST_REMOVE_HEAD(&tqpair->tcp_pdu_free_queue, slist);
    1255                 :       8196 :         tqpair->tcp_pdu_working_count = 1;
    1256                 :            : 
    1257                 :       8196 :         tqpair->recv_buf_size = (in_capsule_data_size + sizeof(struct spdk_nvme_tcp_cmd) + 2 *
    1258                 :            :                                  SPDK_NVME_TCP_DIGEST_LEN) * SPDK_NVMF_TCP_RECV_BUF_SIZE_FACTOR;
    1259                 :            : 
    1260                 :       8196 :         return 0;
    1261                 :            : }
    1262                 :            : 
    1263                 :            : static int
    1264                 :       8196 : nvmf_tcp_qpair_init(struct spdk_nvmf_qpair *qpair)
    1265                 :            : {
    1266                 :            :         struct spdk_nvmf_tcp_qpair *tqpair;
    1267                 :            : 
    1268                 :       8196 :         tqpair = SPDK_CONTAINEROF(qpair, struct spdk_nvmf_tcp_qpair, qpair);
    1269                 :            : 
    1270   [ -  +  -  + ]:       8196 :         SPDK_DEBUGLOG(nvmf_tcp, "New TCP Connection: %p\n", qpair);
    1271                 :            : 
    1272   [ +  +  +  + ]:       8196 :         spdk_trace_record(TRACE_TCP_QP_CREATE, 0, 0, (uintptr_t)tqpair);
    1273                 :            : 
    1274                 :            :         /* Initialise request state queues of the qpair */
    1275                 :       8196 :         TAILQ_INIT(&tqpair->tcp_req_free_queue);
    1276                 :       8196 :         TAILQ_INIT(&tqpair->tcp_req_working_queue);
    1277                 :       8196 :         SLIST_INIT(&tqpair->tcp_pdu_free_queue);
    1278                 :            : 
    1279                 :       8196 :         tqpair->host_hdgst_enable = true;
    1280                 :       8196 :         tqpair->host_ddgst_enable = true;
    1281                 :            : 
    1282                 :       8196 :         return 0;
    1283                 :            : }
    1284                 :            : 
    1285                 :            : static int
    1286                 :       8192 : nvmf_tcp_qpair_sock_init(struct spdk_nvmf_tcp_qpair *tqpair)
    1287                 :            : {
    1288                 :            :         int rc;
    1289                 :            : 
    1290   [ +  +  +  + ]:       8192 :         spdk_trace_record(TRACE_TCP_QP_SOCK_INIT, 0, 0, (uintptr_t)tqpair);
    1291                 :            : 
    1292                 :            :         /* set low water mark */
    1293                 :       8192 :         rc = spdk_sock_set_recvlowat(tqpair->sock, 1);
    1294         [ -  + ]:       8192 :         if (rc != 0) {
    1295                 :          0 :                 SPDK_ERRLOG("spdk_sock_set_recvlowat() failed\n");
    1296                 :          0 :                 return rc;
    1297                 :            :         }
    1298                 :            : 
    1299                 :       8192 :         return 0;
    1300                 :            : }
    1301                 :            : 
    1302                 :            : static void
    1303                 :       8192 : nvmf_tcp_handle_connect(struct spdk_nvmf_transport *transport,
    1304                 :            :                         struct spdk_nvmf_tcp_port *port,
    1305                 :            :                         struct spdk_sock *sock)
    1306                 :            : {
    1307                 :            :         struct spdk_nvmf_tcp_qpair *tqpair;
    1308                 :            :         int rc;
    1309                 :            : 
    1310   [ -  +  -  + ]:       8192 :         SPDK_DEBUGLOG(nvmf_tcp, "New connection accepted on %s port %s\n",
    1311                 :            :                       port->trid->traddr, port->trid->trsvcid);
    1312                 :            : 
    1313                 :       8192 :         tqpair = calloc(1, sizeof(struct spdk_nvmf_tcp_qpair));
    1314         [ -  + ]:       8192 :         if (tqpair == NULL) {
    1315                 :          0 :                 SPDK_ERRLOG("Could not allocate new connection.\n");
    1316                 :          0 :                 spdk_sock_close(&sock);
    1317                 :          0 :                 return;
    1318                 :            :         }
    1319                 :            : 
    1320                 :       8192 :         tqpair->sock = sock;
    1321                 :       8192 :         tqpair->state_cntr[TCP_REQUEST_STATE_FREE] = 0;
    1322                 :       8192 :         tqpair->port = port;
    1323                 :       8192 :         tqpair->qpair.transport = transport;
    1324                 :            : 
    1325                 :      16384 :         rc = spdk_sock_getaddr(tqpair->sock, tqpair->target_addr,
    1326                 :            :                                sizeof(tqpair->target_addr), &tqpair->target_port,
    1327                 :       8192 :                                tqpair->initiator_addr, sizeof(tqpair->initiator_addr),
    1328                 :            :                                &tqpair->initiator_port);
    1329         [ -  + ]:       8192 :         if (rc < 0) {
    1330                 :          0 :                 SPDK_ERRLOG("spdk_sock_getaddr() failed of tqpair=%p\n", tqpair);
    1331                 :          0 :                 nvmf_tcp_qpair_destroy(tqpair);
    1332                 :          0 :                 return;
    1333                 :            :         }
    1334                 :            : 
    1335                 :       8192 :         spdk_nvmf_tgt_new_qpair(transport->tgt, &tqpair->qpair);
    1336                 :            : }
    1337                 :            : 
    1338                 :            : static uint32_t
    1339                 :     332575 : nvmf_tcp_port_accept(struct spdk_nvmf_transport *transport, struct spdk_nvmf_tcp_port *port)
    1340                 :            : {
    1341                 :            :         struct spdk_sock *sock;
    1342                 :     332575 :         uint32_t count = 0;
    1343                 :            :         int i;
    1344                 :            : 
    1345         [ +  - ]:     340767 :         for (i = 0; i < NVMF_TCP_MAX_ACCEPT_SOCK_ONE_TIME; i++) {
    1346                 :     340767 :                 sock = spdk_sock_accept(port->listen_sock);
    1347         [ +  + ]:     340767 :                 if (sock == NULL) {
    1348                 :     332575 :                         break;
    1349                 :            :                 }
    1350                 :       8192 :                 count++;
    1351                 :       8192 :                 nvmf_tcp_handle_connect(transport, port, sock);
    1352                 :            :         }
    1353                 :            : 
    1354                 :     332575 :         return count;
    1355                 :            : }
    1356                 :            : 
    1357                 :            : static int
    1358                 :     290967 : nvmf_tcp_accept(void *ctx)
    1359                 :            : {
    1360                 :     290967 :         struct spdk_nvmf_transport *transport = ctx;
    1361                 :            :         struct spdk_nvmf_tcp_transport *ttransport;
    1362                 :            :         struct spdk_nvmf_tcp_port *port;
    1363                 :     290967 :         uint32_t count = 0;
    1364                 :            : 
    1365                 :     290967 :         ttransport = SPDK_CONTAINEROF(transport, struct spdk_nvmf_tcp_transport, transport);
    1366                 :            : 
    1367         [ +  + ]:     623542 :         TAILQ_FOREACH(port, &ttransport->ports, link) {
    1368                 :     332575 :                 count += nvmf_tcp_port_accept(transport, port);
    1369                 :            :         }
    1370                 :            : 
    1371                 :     290967 :         return count > 0 ? SPDK_POLLER_BUSY : SPDK_POLLER_IDLE;
    1372                 :            : }
    1373                 :            : 
    1374                 :            : static void
    1375                 :        572 : nvmf_tcp_discover(struct spdk_nvmf_transport *transport,
    1376                 :            :                   struct spdk_nvme_transport_id *trid,
    1377                 :            :                   struct spdk_nvmf_discovery_log_page_entry *entry)
    1378                 :            : {
    1379                 :            :         struct spdk_nvmf_tcp_port *port;
    1380                 :            :         struct spdk_nvmf_tcp_transport *ttransport;
    1381                 :            : 
    1382                 :        572 :         entry->trtype = SPDK_NVMF_TRTYPE_TCP;
    1383                 :        572 :         entry->adrfam = trid->adrfam;
    1384                 :            : 
    1385                 :        572 :         spdk_strcpy_pad(entry->trsvcid, trid->trsvcid, sizeof(entry->trsvcid), ' ');
    1386                 :        572 :         spdk_strcpy_pad(entry->traddr, trid->traddr, sizeof(entry->traddr), ' ');
    1387                 :            : 
    1388                 :        572 :         ttransport = SPDK_CONTAINEROF(transport, struct spdk_nvmf_tcp_transport, transport);
    1389                 :        572 :         port = nvmf_tcp_find_port(ttransport, trid);
    1390                 :            : 
    1391         [ -  + ]:        572 :         assert(port != NULL);
    1392                 :            : 
    1393   [ -  +  -  + ]:        572 :         if (strcmp(spdk_sock_get_impl_name(port->listen_sock), "ssl") == 0) {
    1394                 :          0 :                 entry->treq.secure_channel = SPDK_NVMF_TREQ_SECURE_CHANNEL_REQUIRED;
    1395                 :          0 :                 entry->tsas.tcp.sectype = SPDK_NVME_TCP_SECURITY_TLS_1_3;
    1396                 :            :         } else {
    1397                 :        572 :                 entry->treq.secure_channel = SPDK_NVMF_TREQ_SECURE_CHANNEL_NOT_REQUIRED;
    1398                 :        572 :                 entry->tsas.tcp.sectype = SPDK_NVME_TCP_SECURITY_NONE;
    1399                 :            :         }
    1400                 :        572 : }
    1401                 :            : 
    1402                 :            : static struct spdk_nvmf_tcp_control_msg_list *
    1403                 :        397 : nvmf_tcp_control_msg_list_create(uint16_t num_messages)
    1404                 :            : {
    1405                 :            :         struct spdk_nvmf_tcp_control_msg_list *list;
    1406                 :            :         struct spdk_nvmf_tcp_control_msg *msg;
    1407                 :            :         uint16_t i;
    1408                 :            : 
    1409                 :        397 :         list = calloc(1, sizeof(*list));
    1410         [ -  + ]:        397 :         if (!list) {
    1411                 :          0 :                 SPDK_ERRLOG("Failed to allocate memory for list structure\n");
    1412                 :          0 :                 return NULL;
    1413                 :            :         }
    1414                 :            : 
    1415                 :        397 :         list->msg_buf = spdk_zmalloc(num_messages * SPDK_NVME_TCP_IN_CAPSULE_DATA_MAX_SIZE,
    1416                 :            :                                      NVMF_DATA_BUFFER_ALIGNMENT, NULL, SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA);
    1417         [ -  + ]:        397 :         if (!list->msg_buf) {
    1418                 :          0 :                 SPDK_ERRLOG("Failed to allocate memory for control message buffers\n");
    1419                 :          0 :                 free(list);
    1420                 :          0 :                 return NULL;
    1421                 :            :         }
    1422                 :            : 
    1423                 :        397 :         STAILQ_INIT(&list->free_msgs);
    1424                 :            : 
    1425         [ +  + ]:      13101 :         for (i = 0; i < num_messages; i++) {
    1426                 :      12704 :                 msg = (struct spdk_nvmf_tcp_control_msg *)((char *)list->msg_buf + i *
    1427                 :            :                                 SPDK_NVME_TCP_IN_CAPSULE_DATA_MAX_SIZE);
    1428                 :      12704 :                 STAILQ_INSERT_TAIL(&list->free_msgs, msg, link);
    1429                 :            :         }
    1430                 :            : 
    1431                 :        397 :         return list;
    1432                 :            : }
    1433                 :            : 
    1434                 :            : static void
    1435                 :        397 : nvmf_tcp_control_msg_list_free(struct spdk_nvmf_tcp_control_msg_list *list)
    1436                 :            : {
    1437         [ -  + ]:        397 :         if (!list) {
    1438                 :          0 :                 return;
    1439                 :            :         }
    1440                 :            : 
    1441                 :        397 :         spdk_free(list->msg_buf);
    1442                 :        397 :         free(list);
    1443                 :            : }
    1444                 :            : 
    1445                 :            : static struct spdk_nvmf_transport_poll_group *
    1446                 :        397 : nvmf_tcp_poll_group_create(struct spdk_nvmf_transport *transport,
    1447                 :            :                            struct spdk_nvmf_poll_group *group)
    1448                 :            : {
    1449                 :            :         struct spdk_nvmf_tcp_transport  *ttransport;
    1450                 :            :         struct spdk_nvmf_tcp_poll_group *tgroup;
    1451                 :            : 
    1452                 :        397 :         tgroup = calloc(1, sizeof(*tgroup));
    1453         [ -  + ]:        397 :         if (!tgroup) {
    1454                 :          0 :                 return NULL;
    1455                 :            :         }
    1456                 :            : 
    1457                 :        397 :         tgroup->sock_group = spdk_sock_group_create(&tgroup->group);
    1458         [ -  + ]:        397 :         if (!tgroup->sock_group) {
    1459                 :          0 :                 goto cleanup;
    1460                 :            :         }
    1461                 :            : 
    1462                 :        397 :         TAILQ_INIT(&tgroup->qpairs);
    1463                 :        397 :         TAILQ_INIT(&tgroup->await_req);
    1464                 :            : 
    1465                 :        397 :         ttransport = SPDK_CONTAINEROF(transport, struct spdk_nvmf_tcp_transport, transport);
    1466                 :            : 
    1467         [ +  - ]:        397 :         if (transport->opts.in_capsule_data_size < SPDK_NVME_TCP_IN_CAPSULE_DATA_MAX_SIZE) {
    1468   [ -  +  -  + ]:        397 :                 SPDK_DEBUGLOG(nvmf_tcp, "ICD %u is less than min required for admin/fabric commands (%u). "
    1469                 :            :                               "Creating control messages list\n", transport->opts.in_capsule_data_size,
    1470                 :            :                               SPDK_NVME_TCP_IN_CAPSULE_DATA_MAX_SIZE);
    1471                 :        397 :                 tgroup->control_msg_list = nvmf_tcp_control_msg_list_create(ttransport->tcp_opts.control_msg_num);
    1472         [ -  + ]:        397 :                 if (!tgroup->control_msg_list) {
    1473                 :          0 :                         goto cleanup;
    1474                 :            :                 }
    1475                 :            :         }
    1476                 :            : 
    1477                 :        397 :         tgroup->accel_channel = spdk_accel_get_io_channel();
    1478         [ -  + ]:        397 :         if (spdk_unlikely(!tgroup->accel_channel)) {
    1479                 :          0 :                 SPDK_ERRLOG("Cannot create accel_channel for tgroup=%p\n", tgroup);
    1480                 :          0 :                 goto cleanup;
    1481                 :            :         }
    1482                 :            : 
    1483                 :        397 :         TAILQ_INSERT_TAIL(&ttransport->poll_groups, tgroup, link);
    1484         [ +  + ]:        397 :         if (ttransport->next_pg == NULL) {
    1485                 :        160 :                 ttransport->next_pg = tgroup;
    1486                 :            :         }
    1487                 :            : 
    1488                 :        397 :         return &tgroup->group;
    1489                 :            : 
    1490                 :          0 : cleanup:
    1491                 :          0 :         nvmf_tcp_poll_group_destroy(&tgroup->group);
    1492                 :          0 :         return NULL;
    1493                 :            : }
    1494                 :            : 
    1495                 :            : static struct spdk_nvmf_transport_poll_group *
    1496                 :       8192 : nvmf_tcp_get_optimal_poll_group(struct spdk_nvmf_qpair *qpair)
    1497                 :            : {
    1498                 :            :         struct spdk_nvmf_tcp_transport *ttransport;
    1499                 :            :         struct spdk_nvmf_tcp_poll_group **pg;
    1500                 :            :         struct spdk_nvmf_tcp_qpair *tqpair;
    1501                 :       8192 :         struct spdk_sock_group *group = NULL, *hint = NULL;
    1502                 :            :         int rc;
    1503                 :            : 
    1504                 :       8192 :         ttransport = SPDK_CONTAINEROF(qpair->transport, struct spdk_nvmf_tcp_transport, transport);
    1505                 :            : 
    1506         [ -  + ]:       8192 :         if (TAILQ_EMPTY(&ttransport->poll_groups)) {
    1507                 :          0 :                 return NULL;
    1508                 :            :         }
    1509                 :            : 
    1510                 :       8192 :         pg = &ttransport->next_pg;
    1511         [ -  + ]:       8192 :         assert(*pg != NULL);
    1512                 :       8192 :         hint = (*pg)->sock_group;
    1513                 :            : 
    1514                 :       8192 :         tqpair = SPDK_CONTAINEROF(qpair, struct spdk_nvmf_tcp_qpair, qpair);
    1515                 :       8192 :         rc = spdk_sock_get_optimal_sock_group(tqpair->sock, &group, hint);
    1516         [ -  + ]:       8192 :         if (rc != 0) {
    1517                 :          0 :                 return NULL;
    1518         [ +  + ]:       8192 :         } else if (group != NULL) {
    1519                 :            :                 /* Optimal poll group was found */
    1520                 :          3 :                 return spdk_sock_group_get_ctx(group);
    1521                 :            :         }
    1522                 :            : 
    1523                 :            :         /* The hint was used for optimal poll group, advance next_pg. */
    1524                 :       8189 :         *pg = TAILQ_NEXT(*pg, link);
    1525         [ +  + ]:       8189 :         if (*pg == NULL) {
    1526                 :       2579 :                 *pg = TAILQ_FIRST(&ttransport->poll_groups);
    1527                 :            :         }
    1528                 :            : 
    1529                 :       8189 :         return spdk_sock_group_get_ctx(hint);
    1530                 :            : }
    1531                 :            : 
    1532                 :            : static void
    1533                 :        397 : nvmf_tcp_poll_group_destroy(struct spdk_nvmf_transport_poll_group *group)
    1534                 :            : {
    1535                 :            :         struct spdk_nvmf_tcp_poll_group *tgroup, *next_tgroup;
    1536                 :            :         struct spdk_nvmf_tcp_transport *ttransport;
    1537                 :            : 
    1538                 :        397 :         tgroup = SPDK_CONTAINEROF(group, struct spdk_nvmf_tcp_poll_group, group);
    1539                 :        397 :         spdk_sock_group_close(&tgroup->sock_group);
    1540         [ +  - ]:        397 :         if (tgroup->control_msg_list) {
    1541                 :        397 :                 nvmf_tcp_control_msg_list_free(tgroup->control_msg_list);
    1542                 :            :         }
    1543                 :            : 
    1544         [ +  - ]:        397 :         if (tgroup->accel_channel) {
    1545                 :        397 :                 spdk_put_io_channel(tgroup->accel_channel);
    1546                 :            :         }
    1547                 :            : 
    1548         [ -  + ]:        397 :         if (tgroup->group.transport == NULL) {
    1549                 :            :                 /* Transport can be NULL when nvmf_tcp_poll_group_create()
    1550                 :            :                  * calls this function directly in a failure path. */
    1551                 :          0 :                 free(tgroup);
    1552                 :          0 :                 return;
    1553                 :            :         }
    1554                 :            : 
    1555                 :        397 :         ttransport = SPDK_CONTAINEROF(tgroup->group.transport, struct spdk_nvmf_tcp_transport, transport);
    1556                 :            : 
    1557                 :        397 :         next_tgroup = TAILQ_NEXT(tgroup, link);
    1558         [ +  + ]:        397 :         TAILQ_REMOVE(&ttransport->poll_groups, tgroup, link);
    1559         [ +  + ]:        397 :         if (next_tgroup == NULL) {
    1560                 :        231 :                 next_tgroup = TAILQ_FIRST(&ttransport->poll_groups);
    1561                 :            :         }
    1562         [ +  + ]:        397 :         if (ttransport->next_pg == tgroup) {
    1563                 :        246 :                 ttransport->next_pg = next_tgroup;
    1564                 :            :         }
    1565                 :            : 
    1566                 :        397 :         free(tgroup);
    1567                 :            : }
    1568                 :            : 
    1569                 :            : static void
    1570                 :   70435461 : nvmf_tcp_qpair_set_recv_state(struct spdk_nvmf_tcp_qpair *tqpair,
    1571                 :            :                               enum nvme_tcp_pdu_recv_state state)
    1572                 :            : {
    1573         [ +  + ]:   70435461 :         if (tqpair->recv_state == state) {
    1574                 :       1405 :                 SPDK_ERRLOG("The recv state of tqpair=%p is same with the state(%d) to be set\n",
    1575                 :            :                             tqpair, state);
    1576                 :       1405 :                 return;
    1577                 :            :         }
    1578                 :            : 
    1579         [ +  + ]:   70434056 :         if (spdk_unlikely(state == NVME_TCP_PDU_RECV_STATE_QUIESCING)) {
    1580   [ +  +  +  - ]:       8222 :                 if (tqpair->recv_state == NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_CH && tqpair->pdu_in_progress) {
    1581                 :       8147 :                         SLIST_INSERT_HEAD(&tqpair->tcp_pdu_free_queue, tqpair->pdu_in_progress, slist);
    1582                 :       8147 :                         tqpair->tcp_pdu_working_count--;
    1583                 :            :                 }
    1584                 :            :         }
    1585                 :            : 
    1586         [ +  + ]:   70434056 :         if (spdk_unlikely(state == NVME_TCP_PDU_RECV_STATE_ERROR)) {
    1587         [ -  + ]:       8094 :                 assert(tqpair->tcp_pdu_working_count == 0);
    1588                 :            :         }
    1589                 :            : 
    1590         [ +  + ]:   70434056 :         if (tqpair->recv_state == NVME_TCP_PDU_RECV_STATE_AWAIT_REQ) {
    1591                 :            :                 /* When leaving the await req state, move the qpair to the main list */
    1592         [ +  + ]:   15516802 :                 TAILQ_REMOVE(&tqpair->group->await_req, tqpair, link);
    1593                 :   15516802 :                 TAILQ_INSERT_TAIL(&tqpair->group->qpairs, tqpair, link);
    1594         [ +  + ]:   54917254 :         } else if (state == NVME_TCP_PDU_RECV_STATE_AWAIT_REQ) {
    1595         [ +  + ]:   15516802 :                 TAILQ_REMOVE(&tqpair->group->qpairs, tqpair, link);
    1596                 :   15516802 :                 TAILQ_INSERT_TAIL(&tqpair->group->await_req, tqpair, link);
    1597                 :            :         }
    1598                 :            : 
    1599   [ -  +  -  + ]:   70434056 :         SPDK_DEBUGLOG(nvmf_tcp, "tqpair(%p) recv state=%d\n", tqpair, state);
    1600                 :   70434056 :         tqpair->recv_state = state;
    1601                 :            : 
    1602   [ +  +  +  + ]:   70434056 :         spdk_trace_record(TRACE_TCP_QP_RCV_STATE_CHANGE, tqpair->qpair.qid, 0, (uintptr_t)tqpair,
    1603                 :            :                           tqpair->recv_state);
    1604                 :            : }
    1605                 :            : 
    1606                 :            : static int
    1607                 :          0 : nvmf_tcp_qpair_handle_timeout(void *ctx)
    1608                 :            : {
    1609                 :          0 :         struct spdk_nvmf_tcp_qpair *tqpair = ctx;
    1610                 :            : 
    1611         [ #  # ]:          0 :         assert(tqpair->recv_state == NVME_TCP_PDU_RECV_STATE_ERROR);
    1612                 :            : 
    1613                 :          0 :         SPDK_ERRLOG("No pdu coming for tqpair=%p within %d seconds\n", tqpair,
    1614                 :            :                     SPDK_NVME_TCP_QPAIR_EXIT_TIMEOUT);
    1615                 :            : 
    1616                 :          0 :         nvmf_tcp_qpair_disconnect(tqpair);
    1617                 :          0 :         return SPDK_POLLER_BUSY;
    1618                 :            : }
    1619                 :            : 
    1620                 :            : static void
    1621                 :          0 : nvmf_tcp_send_c2h_term_req_complete(void *cb_arg)
    1622                 :            : {
    1623                 :          0 :         struct spdk_nvmf_tcp_qpair *tqpair = (struct spdk_nvmf_tcp_qpair *)cb_arg;
    1624                 :            : 
    1625         [ #  # ]:          0 :         if (!tqpair->timeout_poller) {
    1626                 :          0 :                 tqpair->timeout_poller = SPDK_POLLER_REGISTER(nvmf_tcp_qpair_handle_timeout, tqpair,
    1627                 :            :                                          SPDK_NVME_TCP_QPAIR_EXIT_TIMEOUT * 1000000);
    1628                 :            :         }
    1629                 :          0 : }
    1630                 :            : 
    1631                 :            : static void
    1632                 :         60 : nvmf_tcp_send_c2h_term_req(struct spdk_nvmf_tcp_qpair *tqpair, struct nvme_tcp_pdu *pdu,
    1633                 :            :                            enum spdk_nvme_tcp_term_req_fes fes, uint32_t error_offset)
    1634                 :            : {
    1635                 :            :         struct nvme_tcp_pdu *rsp_pdu;
    1636                 :            :         struct spdk_nvme_tcp_term_req_hdr *c2h_term_req;
    1637                 :         60 :         uint32_t c2h_term_req_hdr_len = sizeof(*c2h_term_req);
    1638                 :            :         uint32_t copy_len;
    1639                 :            : 
    1640                 :         60 :         rsp_pdu = tqpair->mgmt_pdu;
    1641                 :            : 
    1642                 :         60 :         c2h_term_req = &rsp_pdu->hdr.term_req;
    1643                 :         60 :         c2h_term_req->common.pdu_type = SPDK_NVME_TCP_PDU_TYPE_C2H_TERM_REQ;
    1644                 :         60 :         c2h_term_req->common.hlen = c2h_term_req_hdr_len;
    1645                 :         60 :         c2h_term_req->fes = fes;
    1646                 :            : 
    1647   [ +  +  -  + ]:         60 :         if ((fes == SPDK_NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD) ||
    1648                 :            :             (fes == SPDK_NVME_TCP_TERM_REQ_FES_INVALID_DATA_UNSUPPORTED_PARAMETER)) {
    1649                 :         48 :                 DSET32(&c2h_term_req->fei, error_offset);
    1650                 :            :         }
    1651                 :            : 
    1652         [ +  + ]:         60 :         copy_len = spdk_min(pdu->hdr.common.hlen, SPDK_NVME_TCP_TERM_REQ_ERROR_DATA_MAX_SIZE);
    1653                 :            : 
    1654                 :            :         /* Copy the error info into the buffer */
    1655   [ -  +  -  + ]:         60 :         memcpy((uint8_t *)rsp_pdu->hdr.raw + c2h_term_req_hdr_len, pdu->hdr.raw, copy_len);
    1656                 :         60 :         nvme_tcp_pdu_set_data(rsp_pdu, (uint8_t *)rsp_pdu->hdr.raw + c2h_term_req_hdr_len, copy_len);
    1657                 :            : 
    1658                 :            :         /* Contain the header of the wrong received pdu */
    1659                 :         60 :         c2h_term_req->common.plen = c2h_term_req->common.hlen + copy_len;
    1660                 :         60 :         tqpair->wait_terminate = true;
    1661                 :         60 :         nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_QUIESCING);
    1662                 :         60 :         nvmf_tcp_qpair_write_mgmt_pdu(tqpair, nvmf_tcp_send_c2h_term_req_complete, tqpair);
    1663                 :         60 : }
    1664                 :            : 
    1665                 :            : static void
    1666                 :   15859922 : nvmf_tcp_capsule_cmd_hdr_handle(struct spdk_nvmf_tcp_transport *ttransport,
    1667                 :            :                                 struct spdk_nvmf_tcp_qpair *tqpair,
    1668                 :            :                                 struct nvme_tcp_pdu *pdu)
    1669                 :            : {
    1670                 :            :         struct spdk_nvmf_tcp_req *tcp_req;
    1671                 :            : 
    1672         [ -  + ]:   15859922 :         assert(pdu->psh_valid_bytes == pdu->psh_len);
    1673         [ -  + ]:   15859922 :         assert(pdu->hdr.common.pdu_type == SPDK_NVME_TCP_PDU_TYPE_CAPSULE_CMD);
    1674                 :            : 
    1675                 :   15859922 :         tcp_req = nvmf_tcp_req_get(tqpair);
    1676         [ +  + ]:   15859922 :         if (!tcp_req) {
    1677                 :            :                 /* Directly return and make the allocation retry again.  This can happen if we're
    1678                 :            :                  * using asynchronous writes to send the response to the host or when releasing
    1679                 :            :                  * zero-copy buffers after a response has been sent.  In both cases, the host might
    1680                 :            :                  * receive the response before we've finished processing the request and is free to
    1681                 :            :                  * send another one.
    1682                 :            :                  */
    1683         [ -  + ]:      31137 :                 if (tqpair->state_cntr[TCP_REQUEST_STATE_TRANSFERRING_CONTROLLER_TO_HOST] > 0 ||
    1684         [ #  # ]:          0 :                     tqpair->state_cntr[TCP_REQUEST_STATE_AWAITING_ZCOPY_RELEASE] > 0) {
    1685                 :      31137 :                         return;
    1686                 :            :                 }
    1687                 :            : 
    1688                 :            :                 /* The host sent more commands than the maximum queue depth. */
    1689                 :          0 :                 SPDK_ERRLOG("Cannot allocate tcp_req on tqpair=%p\n", tqpair);
    1690                 :          0 :                 nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_QUIESCING);
    1691                 :          0 :                 return;
    1692                 :            :         }
    1693                 :            : 
    1694                 :   15828785 :         pdu->req = tcp_req;
    1695         [ -  + ]:   15828785 :         assert(tcp_req->state == TCP_REQUEST_STATE_NEW);
    1696                 :   15828785 :         nvmf_tcp_req_process(ttransport, tcp_req);
    1697                 :            : }
    1698                 :            : 
    1699                 :            : static void
    1700                 :    4297624 : nvmf_tcp_capsule_cmd_payload_handle(struct spdk_nvmf_tcp_transport *ttransport,
    1701                 :            :                                     struct spdk_nvmf_tcp_qpair *tqpair,
    1702                 :            :                                     struct nvme_tcp_pdu *pdu)
    1703                 :            : {
    1704                 :            :         struct spdk_nvmf_tcp_req *tcp_req;
    1705                 :            :         struct spdk_nvme_tcp_cmd *capsule_cmd;
    1706                 :    4297624 :         uint32_t error_offset = 0;
    1707                 :            :         enum spdk_nvme_tcp_term_req_fes fes;
    1708                 :            :         struct spdk_nvme_cpl *rsp;
    1709                 :            : 
    1710                 :    4297624 :         capsule_cmd = &pdu->hdr.capsule_cmd;
    1711                 :    4297624 :         tcp_req = pdu->req;
    1712         [ -  + ]:    4297624 :         assert(tcp_req != NULL);
    1713                 :            : 
    1714                 :            :         /* Zero-copy requests don't support ICD */
    1715         [ -  + ]:    4297624 :         assert(!spdk_nvmf_request_using_zcopy(&tcp_req->req));
    1716                 :            : 
    1717         [ -  + ]:    4297624 :         if (capsule_cmd->common.pdo > SPDK_NVME_TCP_PDU_PDO_MAX_OFFSET) {
    1718                 :          0 :                 SPDK_ERRLOG("Expected ICReq capsule_cmd pdu offset <= %d, got %c\n",
    1719                 :            :                             SPDK_NVME_TCP_PDU_PDO_MAX_OFFSET, capsule_cmd->common.pdo);
    1720                 :          0 :                 fes = SPDK_NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD;
    1721                 :          0 :                 error_offset = offsetof(struct spdk_nvme_tcp_common_pdu_hdr, pdo);
    1722                 :          0 :                 goto err;
    1723                 :            :         }
    1724                 :            : 
    1725                 :    4297624 :         rsp = &tcp_req->req.rsp->nvme_cpl;
    1726         [ +  + ]:    4297624 :         if (spdk_unlikely(rsp->status.sc == SPDK_NVME_SC_COMMAND_TRANSIENT_TRANSPORT_ERROR)) {
    1727                 :        519 :                 nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_READY_TO_COMPLETE);
    1728                 :            :         } else {
    1729                 :    4297105 :                 nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_READY_TO_EXECUTE);
    1730                 :            :         }
    1731                 :            : 
    1732                 :    4297624 :         nvmf_tcp_req_process(ttransport, tcp_req);
    1733                 :            : 
    1734                 :    4297624 :         return;
    1735                 :          0 : err:
    1736                 :          0 :         nvmf_tcp_send_c2h_term_req(tqpair, pdu, fes, error_offset);
    1737                 :            : }
    1738                 :            : 
    1739                 :            : static void
    1740                 :    1005055 : nvmf_tcp_h2c_data_hdr_handle(struct spdk_nvmf_tcp_transport *ttransport,
    1741                 :            :                              struct spdk_nvmf_tcp_qpair *tqpair,
    1742                 :            :                              struct nvme_tcp_pdu *pdu)
    1743                 :            : {
    1744                 :            :         struct spdk_nvmf_tcp_req *tcp_req;
    1745                 :    1005055 :         uint32_t error_offset = 0;
    1746                 :    1005055 :         enum spdk_nvme_tcp_term_req_fes fes = 0;
    1747                 :            :         struct spdk_nvme_tcp_h2c_data_hdr *h2c_data;
    1748                 :            : 
    1749                 :    1005055 :         h2c_data = &pdu->hdr.h2c_data;
    1750                 :            : 
    1751   [ -  +  -  + ]:    1005055 :         SPDK_DEBUGLOG(nvmf_tcp, "tqpair=%p, r2t_info: datao=%u, datal=%u, cccid=%u, ttag=%u\n",
    1752                 :            :                       tqpair, h2c_data->datao, h2c_data->datal, h2c_data->cccid, h2c_data->ttag);
    1753                 :            : 
    1754         [ -  + ]:    1005055 :         if (h2c_data->ttag > tqpair->resource_count) {
    1755   [ #  #  #  # ]:          0 :                 SPDK_DEBUGLOG(nvmf_tcp, "ttag %u is larger than allowed %u.\n", h2c_data->ttag,
    1756                 :            :                               tqpair->resource_count);
    1757                 :          0 :                 fes = SPDK_NVME_TCP_TERM_REQ_FES_PDU_SEQUENCE_ERROR;
    1758                 :          0 :                 error_offset = offsetof(struct spdk_nvme_tcp_h2c_data_hdr, ttag);
    1759                 :          0 :                 goto err;
    1760                 :            :         }
    1761                 :            : 
    1762                 :    1005055 :         tcp_req = &tqpair->reqs[h2c_data->ttag - 1];
    1763                 :            : 
    1764   [ +  +  -  + ]:    1005055 :         if (spdk_unlikely(tcp_req->state != TCP_REQUEST_STATE_TRANSFERRING_HOST_TO_CONTROLLER &&
    1765                 :            :                           tcp_req->state != TCP_REQUEST_STATE_AWAITING_R2T_ACK)) {
    1766   [ #  #  #  # ]:          0 :                 SPDK_DEBUGLOG(nvmf_tcp, "tcp_req(%p), tqpair=%p, has error state in %d\n", tcp_req, tqpair,
    1767                 :            :                               tcp_req->state);
    1768                 :          0 :                 fes = SPDK_NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD;
    1769                 :          0 :                 error_offset = offsetof(struct spdk_nvme_tcp_h2c_data_hdr, ttag);
    1770                 :          0 :                 goto err;
    1771                 :            :         }
    1772                 :            : 
    1773         [ -  + ]:    1005055 :         if (spdk_unlikely(tcp_req->req.cmd->nvme_cmd.cid != h2c_data->cccid)) {
    1774   [ #  #  #  # ]:          0 :                 SPDK_DEBUGLOG(nvmf_tcp, "tcp_req(%p), tqpair=%p, expected %u but %u for cccid.\n", tcp_req, tqpair,
    1775                 :            :                               tcp_req->req.cmd->nvme_cmd.cid, h2c_data->cccid);
    1776                 :          0 :                 fes = SPDK_NVME_TCP_TERM_REQ_FES_PDU_SEQUENCE_ERROR;
    1777                 :          0 :                 error_offset = offsetof(struct spdk_nvme_tcp_h2c_data_hdr, cccid);
    1778                 :          0 :                 goto err;
    1779                 :            :         }
    1780                 :            : 
    1781         [ -  + ]:    1005055 :         if (tcp_req->h2c_offset != h2c_data->datao) {
    1782   [ #  #  #  # ]:          0 :                 SPDK_DEBUGLOG(nvmf_tcp,
    1783                 :            :                               "tcp_req(%p), tqpair=%p, expected data offset %u, but data offset is %u\n",
    1784                 :            :                               tcp_req, tqpair, tcp_req->h2c_offset, h2c_data->datao);
    1785                 :          0 :                 fes = SPDK_NVME_TCP_TERM_REQ_FES_DATA_TRANSFER_OUT_OF_RANGE;
    1786                 :          0 :                 goto err;
    1787                 :            :         }
    1788                 :            : 
    1789         [ -  + ]:    1005055 :         if ((h2c_data->datao + h2c_data->datal) > tcp_req->req.length) {
    1790   [ #  #  #  # ]:          0 :                 SPDK_DEBUGLOG(nvmf_tcp,
    1791                 :            :                               "tcp_req(%p), tqpair=%p,  (datao=%u + datal=%u) exceeds requested length=%u\n",
    1792                 :            :                               tcp_req, tqpair, h2c_data->datao, h2c_data->datal, tcp_req->req.length);
    1793                 :          0 :                 fes = SPDK_NVME_TCP_TERM_REQ_FES_DATA_TRANSFER_OUT_OF_RANGE;
    1794                 :          0 :                 goto err;
    1795                 :            :         }
    1796                 :            : 
    1797                 :    1005055 :         pdu->req = tcp_req;
    1798                 :            : 
    1799         [ -  + ]:    1005055 :         if (spdk_unlikely(tcp_req->req.dif_enabled)) {
    1800                 :          0 :                 pdu->dif_ctx = &tcp_req->req.dif.dif_ctx;
    1801                 :            :         }
    1802                 :            : 
    1803                 :    1005055 :         nvme_tcp_pdu_set_data_buf(pdu, tcp_req->req.iov, tcp_req->req.iovcnt,
    1804                 :            :                                   h2c_data->datao, h2c_data->datal);
    1805                 :    1005055 :         nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_PAYLOAD);
    1806                 :    1005055 :         return;
    1807                 :            : 
    1808                 :          0 : err:
    1809                 :          0 :         nvmf_tcp_send_c2h_term_req(tqpair, pdu, fes, error_offset);
    1810                 :            : }
    1811                 :            : 
    1812                 :            : static void
    1813                 :   15107551 : nvmf_tcp_send_capsule_resp_pdu(struct spdk_nvmf_tcp_req *tcp_req,
    1814                 :            :                                struct spdk_nvmf_tcp_qpair *tqpair)
    1815                 :            : {
    1816                 :            :         struct nvme_tcp_pdu *rsp_pdu;
    1817                 :            :         struct spdk_nvme_tcp_rsp *capsule_resp;
    1818                 :            : 
    1819   [ -  +  -  + ]:   15107551 :         SPDK_DEBUGLOG(nvmf_tcp, "enter, tqpair=%p\n", tqpair);
    1820                 :            : 
    1821                 :   15107551 :         rsp_pdu = nvmf_tcp_req_pdu_init(tcp_req);
    1822         [ -  + ]:   15107551 :         assert(rsp_pdu != NULL);
    1823                 :            : 
    1824                 :   15107551 :         capsule_resp = &rsp_pdu->hdr.capsule_resp;
    1825                 :   15107551 :         capsule_resp->common.pdu_type = SPDK_NVME_TCP_PDU_TYPE_CAPSULE_RESP;
    1826                 :   15107551 :         capsule_resp->common.plen = capsule_resp->common.hlen = sizeof(*capsule_resp);
    1827                 :   15107551 :         capsule_resp->rccqe = tcp_req->req.rsp->nvme_cpl;
    1828   [ +  +  +  + ]:   15107551 :         if (tqpair->host_hdgst_enable) {
    1829                 :     494312 :                 capsule_resp->common.flags |= SPDK_NVME_TCP_CH_FLAGS_HDGSTF;
    1830                 :     494312 :                 capsule_resp->common.plen += SPDK_NVME_TCP_DIGEST_LEN;
    1831                 :            :         }
    1832                 :            : 
    1833                 :   15107551 :         nvmf_tcp_qpair_write_req_pdu(tqpair, tcp_req, nvmf_tcp_request_free, tcp_req);
    1834                 :   15107551 : }
    1835                 :            : 
    1836                 :            : static void
    1837                 :    9383751 : nvmf_tcp_pdu_c2h_data_complete(void *cb_arg)
    1838                 :            : {
    1839                 :    9383751 :         struct spdk_nvmf_tcp_req *tcp_req = cb_arg;
    1840                 :    9383751 :         struct spdk_nvmf_tcp_qpair *tqpair = SPDK_CONTAINEROF(tcp_req->req.qpair,
    1841                 :            :                                              struct spdk_nvmf_tcp_qpair, qpair);
    1842                 :            : 
    1843         [ -  + ]:    9383751 :         assert(tqpair != NULL);
    1844                 :            : 
    1845         [ +  + ]:    9383751 :         if (spdk_unlikely(tcp_req->pdu->rw_offset < tcp_req->req.length)) {
    1846   [ -  +  -  + ]:     263308 :                 SPDK_DEBUGLOG(nvmf_tcp, "sending another C2H part, offset %u length %u\n", tcp_req->pdu->rw_offset,
    1847                 :            :                               tcp_req->req.length);
    1848                 :     263308 :                 _nvmf_tcp_send_c2h_data(tqpair, tcp_req);
    1849                 :     263308 :                 return;
    1850                 :            :         }
    1851                 :            : 
    1852         [ +  + ]:    9120443 :         if (tcp_req->pdu->hdr.c2h_data.common.flags & SPDK_NVME_TCP_C2H_DATA_FLAGS_SUCCESS) {
    1853                 :     404546 :                 nvmf_tcp_request_free(tcp_req);
    1854                 :            :         } else {
    1855                 :    8715897 :                 nvmf_tcp_send_capsule_resp_pdu(tcp_req, tqpair);
    1856                 :            :         }
    1857                 :            : }
    1858                 :            : 
    1859                 :            : static void
    1860                 :    1005063 : nvmf_tcp_r2t_complete(void *cb_arg)
    1861                 :            : {
    1862                 :    1005063 :         struct spdk_nvmf_tcp_req *tcp_req = cb_arg;
    1863                 :            :         struct spdk_nvmf_tcp_transport *ttransport;
    1864                 :            : 
    1865                 :    1005063 :         ttransport = SPDK_CONTAINEROF(tcp_req->req.qpair->transport,
    1866                 :            :                                       struct spdk_nvmf_tcp_transport, transport);
    1867                 :            : 
    1868                 :    1005063 :         nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_TRANSFERRING_HOST_TO_CONTROLLER);
    1869                 :            : 
    1870         [ +  + ]:    1005063 :         if (tcp_req->h2c_offset == tcp_req->req.length) {
    1871                 :       4634 :                 nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_READY_TO_EXECUTE);
    1872                 :       4634 :                 nvmf_tcp_req_process(ttransport, tcp_req);
    1873                 :            :         }
    1874                 :    1005063 : }
    1875                 :            : 
    1876                 :            : static void
    1877                 :    1005171 : nvmf_tcp_send_r2t_pdu(struct spdk_nvmf_tcp_qpair *tqpair,
    1878                 :            :                       struct spdk_nvmf_tcp_req *tcp_req)
    1879                 :            : {
    1880                 :            :         struct nvme_tcp_pdu *rsp_pdu;
    1881                 :            :         struct spdk_nvme_tcp_r2t_hdr *r2t;
    1882                 :            : 
    1883                 :    1005171 :         rsp_pdu = nvmf_tcp_req_pdu_init(tcp_req);
    1884         [ -  + ]:    1005171 :         assert(rsp_pdu != NULL);
    1885                 :            : 
    1886                 :    1005171 :         r2t = &rsp_pdu->hdr.r2t;
    1887                 :    1005171 :         r2t->common.pdu_type = SPDK_NVME_TCP_PDU_TYPE_R2T;
    1888                 :    1005171 :         r2t->common.plen = r2t->common.hlen = sizeof(*r2t);
    1889                 :            : 
    1890   [ -  +  -  + ]:    1005171 :         if (tqpair->host_hdgst_enable) {
    1891                 :          0 :                 r2t->common.flags |= SPDK_NVME_TCP_CH_FLAGS_HDGSTF;
    1892                 :          0 :                 r2t->common.plen += SPDK_NVME_TCP_DIGEST_LEN;
    1893                 :            :         }
    1894                 :            : 
    1895                 :    1005171 :         r2t->cccid = tcp_req->req.cmd->nvme_cmd.cid;
    1896                 :    1005171 :         r2t->ttag = tcp_req->ttag;
    1897                 :    1005171 :         r2t->r2to = tcp_req->h2c_offset;
    1898                 :    1005171 :         r2t->r2tl = tcp_req->req.length;
    1899                 :            : 
    1900                 :    1005171 :         nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_AWAITING_R2T_ACK);
    1901                 :            : 
    1902   [ -  +  -  + ]:    1005171 :         SPDK_DEBUGLOG(nvmf_tcp,
    1903                 :            :                       "tcp_req(%p) on tqpair(%p), r2t_info: cccid=%u, ttag=%u, r2to=%u, r2tl=%u\n",
    1904                 :            :                       tcp_req, tqpair, r2t->cccid, r2t->ttag, r2t->r2to, r2t->r2tl);
    1905                 :    1005171 :         nvmf_tcp_qpair_write_req_pdu(tqpair, tcp_req, nvmf_tcp_r2t_complete, tcp_req);
    1906                 :    1005171 : }
    1907                 :            : 
    1908                 :            : static void
    1909                 :    1005048 : nvmf_tcp_h2c_data_payload_handle(struct spdk_nvmf_tcp_transport *ttransport,
    1910                 :            :                                  struct spdk_nvmf_tcp_qpair *tqpair,
    1911                 :            :                                  struct nvme_tcp_pdu *pdu)
    1912                 :            : {
    1913                 :            :         struct spdk_nvmf_tcp_req *tcp_req;
    1914                 :            :         struct spdk_nvme_cpl *rsp;
    1915                 :            : 
    1916                 :    1005048 :         tcp_req = pdu->req;
    1917         [ -  + ]:    1005048 :         assert(tcp_req != NULL);
    1918                 :            : 
    1919   [ -  +  -  + ]:    1005048 :         SPDK_DEBUGLOG(nvmf_tcp, "enter\n");
    1920                 :            : 
    1921                 :    1005048 :         tcp_req->h2c_offset += pdu->data_len;
    1922                 :            : 
    1923                 :            :         /* Wait for all of the data to arrive AND for the initial R2T PDU send to be
    1924                 :            :          * acknowledged before moving on. */
    1925         [ +  - ]:    1005048 :         if (tcp_req->h2c_offset == tcp_req->req.length &&
    1926         [ +  + ]:    1005048 :             tcp_req->state == TCP_REQUEST_STATE_TRANSFERRING_HOST_TO_CONTROLLER) {
    1927                 :            :                 /* After receiving all the h2c data, we need to check whether there is
    1928                 :            :                  * transient transport error */
    1929                 :    1000408 :                 rsp = &tcp_req->req.rsp->nvme_cpl;
    1930         [ +  + ]:    1000408 :                 if (spdk_unlikely(rsp->status.sc == SPDK_NVME_SC_COMMAND_TRANSIENT_TRANSPORT_ERROR)) {
    1931                 :       1266 :                         nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_READY_TO_COMPLETE);
    1932                 :            :                 } else {
    1933                 :     999142 :                         nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_READY_TO_EXECUTE);
    1934                 :            :                 }
    1935                 :    1000408 :                 nvmf_tcp_req_process(ttransport, tcp_req);
    1936                 :            :         }
    1937                 :    1005048 : }
    1938                 :            : 
    1939                 :            : static void
    1940                 :          0 : nvmf_tcp_h2c_term_req_dump(struct spdk_nvme_tcp_term_req_hdr *h2c_term_req)
    1941                 :            : {
    1942                 :          0 :         SPDK_ERRLOG("Error info of pdu(%p): %s\n", h2c_term_req,
    1943                 :            :                     spdk_nvmf_tcp_term_req_fes_str[h2c_term_req->fes]);
    1944         [ #  # ]:          0 :         if ((h2c_term_req->fes == SPDK_NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD) ||
    1945         [ #  # ]:          0 :             (h2c_term_req->fes == SPDK_NVME_TCP_TERM_REQ_FES_INVALID_DATA_UNSUPPORTED_PARAMETER)) {
    1946   [ #  #  #  # ]:          0 :                 SPDK_DEBUGLOG(nvmf_tcp, "The offset from the start of the PDU header is %u\n",
    1947                 :            :                               DGET32(h2c_term_req->fei));
    1948                 :            :         }
    1949                 :          0 : }
    1950                 :            : 
    1951                 :            : static void
    1952                 :          0 : nvmf_tcp_h2c_term_req_hdr_handle(struct spdk_nvmf_tcp_qpair *tqpair,
    1953                 :            :                                  struct nvme_tcp_pdu *pdu)
    1954                 :            : {
    1955                 :          0 :         struct spdk_nvme_tcp_term_req_hdr *h2c_term_req = &pdu->hdr.term_req;
    1956                 :          0 :         uint32_t error_offset = 0;
    1957                 :            :         enum spdk_nvme_tcp_term_req_fes fes;
    1958                 :            : 
    1959         [ #  # ]:          0 :         if (h2c_term_req->fes > SPDK_NVME_TCP_TERM_REQ_FES_INVALID_DATA_UNSUPPORTED_PARAMETER) {
    1960                 :          0 :                 SPDK_ERRLOG("Fatal Error Status(FES) is unknown for h2c_term_req pdu=%p\n", pdu);
    1961                 :          0 :                 fes = SPDK_NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD;
    1962                 :          0 :                 error_offset = offsetof(struct spdk_nvme_tcp_term_req_hdr, fes);
    1963                 :          0 :                 goto end;
    1964                 :            :         }
    1965                 :            : 
    1966                 :            :         /* set the data buffer */
    1967                 :          0 :         nvme_tcp_pdu_set_data(pdu, (uint8_t *)pdu->hdr.raw + h2c_term_req->common.hlen,
    1968                 :          0 :                               h2c_term_req->common.plen - h2c_term_req->common.hlen);
    1969                 :          0 :         nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_PAYLOAD);
    1970                 :          0 :         return;
    1971                 :          0 : end:
    1972                 :          0 :         nvmf_tcp_send_c2h_term_req(tqpair, pdu, fes, error_offset);
    1973                 :            : }
    1974                 :            : 
    1975                 :            : static void
    1976                 :          0 : nvmf_tcp_h2c_term_req_payload_handle(struct spdk_nvmf_tcp_qpair *tqpair,
    1977                 :            :                                      struct nvme_tcp_pdu *pdu)
    1978                 :            : {
    1979                 :          0 :         struct spdk_nvme_tcp_term_req_hdr *h2c_term_req = &pdu->hdr.term_req;
    1980                 :            : 
    1981                 :          0 :         nvmf_tcp_h2c_term_req_dump(h2c_term_req);
    1982                 :          0 :         nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_QUIESCING);
    1983                 :          0 : }
    1984                 :            : 
    1985                 :            : static void
    1986                 :    5302672 : _nvmf_tcp_pdu_payload_handle(struct spdk_nvmf_tcp_qpair *tqpair, struct nvme_tcp_pdu *pdu)
    1987                 :            : {
    1988                 :    5302672 :         struct spdk_nvmf_tcp_transport *ttransport = SPDK_CONTAINEROF(tqpair->qpair.transport,
    1989                 :            :                         struct spdk_nvmf_tcp_transport, transport);
    1990                 :            : 
    1991   [ +  +  -  - ]:    5302672 :         switch (pdu->hdr.common.pdu_type) {
    1992                 :    4297624 :         case SPDK_NVME_TCP_PDU_TYPE_CAPSULE_CMD:
    1993                 :    4297624 :                 nvmf_tcp_capsule_cmd_payload_handle(ttransport, tqpair, pdu);
    1994                 :    4297624 :                 break;
    1995                 :    1005048 :         case SPDK_NVME_TCP_PDU_TYPE_H2C_DATA:
    1996                 :    1005048 :                 nvmf_tcp_h2c_data_payload_handle(ttransport, tqpair, pdu);
    1997                 :    1005048 :                 break;
    1998                 :            : 
    1999                 :          0 :         case SPDK_NVME_TCP_PDU_TYPE_H2C_TERM_REQ:
    2000                 :          0 :                 nvmf_tcp_h2c_term_req_payload_handle(tqpair, pdu);
    2001                 :          0 :                 break;
    2002                 :            : 
    2003                 :          0 :         default:
    2004                 :            :                 /* The code should not go to here */
    2005                 :          0 :                 SPDK_ERRLOG("ERROR pdu type %d\n", pdu->hdr.common.pdu_type);
    2006                 :          0 :                 break;
    2007                 :            :         }
    2008                 :    5302672 :         SLIST_INSERT_HEAD(&tqpair->tcp_pdu_free_queue, pdu, slist);
    2009                 :    5302672 :         tqpair->tcp_pdu_working_count--;
    2010                 :    5302672 : }
    2011                 :            : 
    2012                 :            : static inline void
    2013                 :       1789 : nvmf_tcp_req_set_cpl(struct spdk_nvmf_tcp_req *treq, int sct, int sc)
    2014                 :            : {
    2015                 :       1789 :         treq->req.rsp->nvme_cpl.status.sct = sct;
    2016                 :       1789 :         treq->req.rsp->nvme_cpl.status.sc = sc;
    2017                 :       1789 :         treq->req.rsp->nvme_cpl.cid = treq->req.cmd->nvme_cmd.cid;
    2018                 :       1789 : }
    2019                 :            : 
    2020                 :            : static void
    2021                 :     568067 : data_crc32_calc_done(void *cb_arg, int status)
    2022                 :            : {
    2023                 :     568067 :         struct nvme_tcp_pdu *pdu = cb_arg;
    2024                 :     568067 :         struct spdk_nvmf_tcp_qpair *tqpair = pdu->qpair;
    2025                 :            : 
    2026                 :            :         /* async crc32 calculation is failed and use direct calculation to check */
    2027         [ -  + ]:     568067 :         if (spdk_unlikely(status)) {
    2028                 :          0 :                 SPDK_ERRLOG("Data digest on tqpair=(%p) with pdu=%p failed to be calculated asynchronously\n",
    2029                 :            :                             tqpair, pdu);
    2030                 :          0 :                 pdu->data_digest_crc32 = nvme_tcp_pdu_calc_data_digest(pdu);
    2031                 :            :         }
    2032                 :     568067 :         pdu->data_digest_crc32 ^= SPDK_CRC32C_XOR;
    2033         [ +  + ]:     568067 :         if (!MATCH_DIGEST_WORD(pdu->data_digest, pdu->data_digest_crc32)) {
    2034                 :       1785 :                 SPDK_ERRLOG("Data digest error on tqpair=(%p) with pdu=%p\n", tqpair, pdu);
    2035         [ -  + ]:       1785 :                 assert(pdu->req != NULL);
    2036                 :       1785 :                 nvmf_tcp_req_set_cpl(pdu->req, SPDK_NVME_SCT_GENERIC,
    2037                 :            :                                      SPDK_NVME_SC_COMMAND_TRANSIENT_TRANSPORT_ERROR);
    2038                 :            :         }
    2039                 :     568067 :         _nvmf_tcp_pdu_payload_handle(tqpair, pdu);
    2040                 :     568067 : }
    2041                 :            : 
    2042                 :            : static void
    2043                 :    5302672 : nvmf_tcp_pdu_payload_handle(struct spdk_nvmf_tcp_qpair *tqpair, struct nvme_tcp_pdu *pdu)
    2044                 :            : {
    2045                 :    5302672 :         int rc = 0;
    2046         [ -  + ]:    5302672 :         assert(tqpair->recv_state == NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_PAYLOAD);
    2047                 :    5302672 :         tqpair->pdu_in_progress = NULL;
    2048                 :    5302672 :         nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_READY);
    2049   [ -  +  -  + ]:    5302672 :         SPDK_DEBUGLOG(nvmf_tcp, "enter\n");
    2050                 :            :         /* check data digest if need */
    2051   [ -  +  +  + ]:    5302672 :         if (pdu->ddgst_enable) {
    2052   [ +  +  +  -  :     568067 :                 if (tqpair->qpair.qid != 0 && !pdu->dif_ctx && tqpair->group &&
                   +  - ]
    2053         [ +  - ]:     567923 :                     (pdu->data_len % SPDK_NVME_TCP_DIGEST_ALIGNMENT == 0)) {
    2054                 :     567923 :                         rc = spdk_accel_submit_crc32cv(tqpair->group->accel_channel, &pdu->data_digest_crc32, pdu->data_iov,
    2055                 :            :                                                        pdu->data_iovcnt, 0, data_crc32_calc_done, pdu);
    2056         [ +  - ]:     567923 :                         if (spdk_likely(rc == 0)) {
    2057                 :     567923 :                                 return;
    2058                 :            :                         }
    2059                 :            :                 } else {
    2060                 :        144 :                         pdu->data_digest_crc32 = nvme_tcp_pdu_calc_data_digest(pdu);
    2061                 :            :                 }
    2062                 :        144 :                 data_crc32_calc_done(pdu, rc);
    2063                 :            :         } else {
    2064                 :    4734605 :                 _nvmf_tcp_pdu_payload_handle(tqpair, pdu);
    2065                 :            :         }
    2066                 :            : }
    2067                 :            : 
    2068                 :            : static void
    2069                 :      16358 : nvmf_tcp_send_icresp_complete(void *cb_arg)
    2070                 :            : {
    2071                 :      16358 :         struct spdk_nvmf_tcp_qpair *tqpair = cb_arg;
    2072                 :            : 
    2073                 :      16358 :         nvmf_tcp_qpair_set_state(tqpair, NVME_TCP_QPAIR_STATE_RUNNING);
    2074                 :      16358 : }
    2075                 :            : 
    2076                 :            : static void
    2077                 :       8191 : nvmf_tcp_icreq_handle(struct spdk_nvmf_tcp_transport *ttransport,
    2078                 :            :                       struct spdk_nvmf_tcp_qpair *tqpair,
    2079                 :            :                       struct nvme_tcp_pdu *pdu)
    2080                 :            : {
    2081                 :       8191 :         struct spdk_nvme_tcp_ic_req *ic_req = &pdu->hdr.ic_req;
    2082                 :            :         struct nvme_tcp_pdu *rsp_pdu;
    2083                 :            :         struct spdk_nvme_tcp_ic_resp *ic_resp;
    2084                 :       8191 :         uint32_t error_offset = 0;
    2085                 :            :         enum spdk_nvme_tcp_term_req_fes fes;
    2086                 :            : 
    2087                 :            :         /* Only PFV 0 is defined currently */
    2088         [ +  + ]:       8191 :         if (ic_req->pfv != 0) {
    2089                 :          8 :                 SPDK_ERRLOG("Expected ICReq PFV %u, got %u\n", 0u, ic_req->pfv);
    2090                 :          8 :                 fes = SPDK_NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD;
    2091                 :          8 :                 error_offset = offsetof(struct spdk_nvme_tcp_ic_req, pfv);
    2092                 :          8 :                 goto end;
    2093                 :            :         }
    2094                 :            : 
    2095                 :            :         /* This value is 0’s based value in units of dwords should not be larger than SPDK_NVME_TCP_HPDA_MAX */
    2096         [ -  + ]:       8183 :         if (ic_req->hpda > SPDK_NVME_TCP_HPDA_MAX) {
    2097                 :          0 :                 SPDK_ERRLOG("ICReq HPDA out of range 0 to 31, got %u\n", ic_req->hpda);
    2098                 :          0 :                 fes = SPDK_NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD;
    2099                 :          0 :                 error_offset = offsetof(struct spdk_nvme_tcp_ic_req, hpda);
    2100                 :          0 :                 goto end;
    2101                 :            :         }
    2102                 :            : 
    2103                 :            :         /* MAXR2T is 0's based */
    2104   [ -  +  -  + ]:       8183 :         SPDK_DEBUGLOG(nvmf_tcp, "maxr2t =%u\n", (ic_req->maxr2t + 1u));
    2105                 :            : 
    2106                 :       8183 :         tqpair->host_hdgst_enable = ic_req->dgst.bits.hdgst_enable ? true : false;
    2107   [ +  +  +  + ]:       8183 :         if (!tqpair->host_hdgst_enable) {
    2108                 :       8115 :                 tqpair->recv_buf_size -= SPDK_NVME_TCP_DIGEST_LEN * SPDK_NVMF_TCP_RECV_BUF_SIZE_FACTOR;
    2109                 :            :         }
    2110                 :            : 
    2111                 :       8183 :         tqpair->host_ddgst_enable = ic_req->dgst.bits.ddgst_enable ? true : false;
    2112   [ +  +  +  + ]:       8183 :         if (!tqpair->host_ddgst_enable) {
    2113                 :       8039 :                 tqpair->recv_buf_size -= SPDK_NVME_TCP_DIGEST_LEN * SPDK_NVMF_TCP_RECV_BUF_SIZE_FACTOR;
    2114                 :            :         }
    2115                 :            : 
    2116                 :       8183 :         tqpair->recv_buf_size = spdk_max(tqpair->recv_buf_size, MIN_SOCK_PIPE_SIZE);
    2117                 :            :         /* Now that we know whether digests are enabled, properly size the receive buffer */
    2118         [ -  + ]:       8183 :         if (spdk_sock_set_recvbuf(tqpair->sock, tqpair->recv_buf_size) < 0) {
    2119                 :          0 :                 SPDK_WARNLOG("Unable to allocate enough memory for receive buffer on tqpair=%p with size=%d\n",
    2120                 :            :                              tqpair,
    2121                 :            :                              tqpair->recv_buf_size);
    2122                 :            :                 /* Not fatal. */
    2123                 :            :         }
    2124                 :            : 
    2125                 :       8183 :         tqpair->cpda = spdk_min(ic_req->hpda, SPDK_NVME_TCP_CPDA_MAX);
    2126   [ -  +  -  + ]:       8183 :         SPDK_DEBUGLOG(nvmf_tcp, "cpda of tqpair=(%p) is : %u\n", tqpair, tqpair->cpda);
    2127                 :            : 
    2128                 :       8183 :         rsp_pdu = tqpair->mgmt_pdu;
    2129                 :            : 
    2130                 :       8183 :         ic_resp = &rsp_pdu->hdr.ic_resp;
    2131                 :       8183 :         ic_resp->common.pdu_type = SPDK_NVME_TCP_PDU_TYPE_IC_RESP;
    2132                 :       8183 :         ic_resp->common.hlen = ic_resp->common.plen =  sizeof(*ic_resp);
    2133                 :       8183 :         ic_resp->pfv = 0;
    2134                 :       8183 :         ic_resp->cpda = tqpair->cpda;
    2135                 :       8183 :         ic_resp->maxh2cdata = ttransport->transport.opts.max_io_size;
    2136         [ -  + ]:       8183 :         ic_resp->dgst.bits.hdgst_enable = tqpair->host_hdgst_enable ? 1 : 0;
    2137         [ -  + ]:       8183 :         ic_resp->dgst.bits.ddgst_enable = tqpair->host_ddgst_enable ? 1 : 0;
    2138                 :            : 
    2139   [ -  +  -  +  :       8183 :         SPDK_DEBUGLOG(nvmf_tcp, "host_hdgst_enable: %u\n", tqpair->host_hdgst_enable);
                   -  - ]
    2140   [ -  +  -  +  :       8183 :         SPDK_DEBUGLOG(nvmf_tcp, "host_ddgst_enable: %u\n", tqpair->host_ddgst_enable);
                   -  - ]
    2141                 :            : 
    2142                 :       8183 :         nvmf_tcp_qpair_set_state(tqpair, NVME_TCP_QPAIR_STATE_INITIALIZING);
    2143                 :       8183 :         nvmf_tcp_qpair_write_mgmt_pdu(tqpair, nvmf_tcp_send_icresp_complete, tqpair);
    2144                 :       8183 :         nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_READY);
    2145                 :       8183 :         return;
    2146                 :          8 : end:
    2147                 :          8 :         nvmf_tcp_send_c2h_term_req(tqpair, pdu, fes, error_offset);
    2148                 :            : }
    2149                 :            : 
    2150                 :            : static void
    2151                 :   16530032 : nvmf_tcp_pdu_psh_handle(struct spdk_nvmf_tcp_qpair *tqpair,
    2152                 :            :                         struct spdk_nvmf_tcp_transport *ttransport)
    2153                 :            : {
    2154                 :            :         struct nvme_tcp_pdu *pdu;
    2155                 :            :         int rc;
    2156                 :   16530032 :         uint32_t crc32c, error_offset = 0;
    2157                 :            :         enum spdk_nvme_tcp_term_req_fes fes;
    2158                 :            : 
    2159         [ -  + ]:   16530032 :         assert(tqpair->recv_state == NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_PSH);
    2160                 :   16530032 :         pdu = tqpair->pdu_in_progress;
    2161                 :            : 
    2162   [ -  +  -  + ]:   16530032 :         SPDK_DEBUGLOG(nvmf_tcp, "pdu type of tqpair(%p) is %d\n", tqpair,
    2163                 :            :                       pdu->hdr.common.pdu_type);
    2164                 :            :         /* check header digest if needed */
    2165   [ -  +  +  + ]:   16530032 :         if (pdu->has_hdgst) {
    2166   [ -  +  -  + ]:     494336 :                 SPDK_DEBUGLOG(nvmf_tcp, "Compare the header of pdu=%p on tqpair=%p\n", pdu, tqpair);
    2167                 :     494336 :                 crc32c = nvme_tcp_pdu_calc_header_digest(pdu);
    2168                 :     494336 :                 rc = MATCH_DIGEST_WORD((uint8_t *)pdu->hdr.raw + pdu->hdr.common.hlen, crc32c);
    2169         [ -  + ]:     494336 :                 if (rc == 0) {
    2170                 :          0 :                         SPDK_ERRLOG("Header digest error on tqpair=(%p) with pdu=%p\n", tqpair, pdu);
    2171                 :          0 :                         fes = SPDK_NVME_TCP_TERM_REQ_FES_HDGST_ERROR;
    2172                 :          0 :                         nvmf_tcp_send_c2h_term_req(tqpair, pdu, fes, error_offset);
    2173                 :          0 :                         return;
    2174                 :            : 
    2175                 :            :                 }
    2176                 :            :         }
    2177                 :            : 
    2178   [ +  +  +  -  :   16530032 :         switch (pdu->hdr.common.pdu_type) {
                      - ]
    2179                 :       8179 :         case SPDK_NVME_TCP_PDU_TYPE_IC_REQ:
    2180                 :       8179 :                 nvmf_tcp_icreq_handle(ttransport, tqpair, pdu);
    2181                 :       8179 :                 break;
    2182                 :   15516802 :         case SPDK_NVME_TCP_PDU_TYPE_CAPSULE_CMD:
    2183                 :   15516802 :                 nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_AWAIT_REQ);
    2184                 :   15516802 :                 break;
    2185                 :    1005051 :         case SPDK_NVME_TCP_PDU_TYPE_H2C_DATA:
    2186                 :    1005051 :                 nvmf_tcp_h2c_data_hdr_handle(ttransport, tqpair, pdu);
    2187                 :    1005051 :                 break;
    2188                 :            : 
    2189                 :          0 :         case SPDK_NVME_TCP_PDU_TYPE_H2C_TERM_REQ:
    2190                 :          0 :                 nvmf_tcp_h2c_term_req_hdr_handle(tqpair, pdu);
    2191                 :          0 :                 break;
    2192                 :            : 
    2193                 :          0 :         default:
    2194                 :          0 :                 SPDK_ERRLOG("Unexpected PDU type 0x%02x\n", tqpair->pdu_in_progress->hdr.common.pdu_type);
    2195                 :          0 :                 fes = SPDK_NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD;
    2196                 :          0 :                 error_offset = 1;
    2197                 :          0 :                 nvmf_tcp_send_c2h_term_req(tqpair, pdu, fes, error_offset);
    2198                 :          0 :                 break;
    2199                 :            :         }
    2200                 :            : }
    2201                 :            : 
    2202                 :            : static void
    2203                 :   16530076 : nvmf_tcp_pdu_ch_handle(struct spdk_nvmf_tcp_qpair *tqpair)
    2204                 :            : {
    2205                 :            :         struct nvme_tcp_pdu *pdu;
    2206                 :   16530076 :         uint32_t error_offset = 0;
    2207                 :            :         enum spdk_nvme_tcp_term_req_fes fes;
    2208                 :            :         uint8_t expected_hlen, pdo;
    2209                 :   16530076 :         bool plen_error = false, pdo_error = false;
    2210                 :            : 
    2211         [ -  + ]:   16530076 :         assert(tqpair->recv_state == NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_CH);
    2212                 :   16530076 :         pdu = tqpair->pdu_in_progress;
    2213         [ -  + ]:   16530076 :         assert(pdu);
    2214         [ +  + ]:   16530076 :         if (pdu->hdr.common.pdu_type == SPDK_NVME_TCP_PDU_TYPE_IC_REQ) {
    2215         [ +  + ]:       8195 :                 if (tqpair->state != NVME_TCP_QPAIR_STATE_INVALID) {
    2216                 :          4 :                         SPDK_ERRLOG("Already received ICreq PDU, and reject this pdu=%p\n", pdu);
    2217                 :          4 :                         fes = SPDK_NVME_TCP_TERM_REQ_FES_PDU_SEQUENCE_ERROR;
    2218                 :          4 :                         goto err;
    2219                 :            :                 }
    2220                 :       8191 :                 expected_hlen = sizeof(struct spdk_nvme_tcp_ic_req);
    2221         [ +  + ]:       8191 :                 if (pdu->hdr.common.plen != expected_hlen) {
    2222                 :          4 :                         plen_error = true;
    2223                 :            :                 }
    2224                 :            :         } else {
    2225         [ +  + ]:   16521881 :                 if (tqpair->state != NVME_TCP_QPAIR_STATE_RUNNING) {
    2226                 :          4 :                         SPDK_ERRLOG("The TCP/IP connection is not negotiated\n");
    2227                 :          4 :                         fes = SPDK_NVME_TCP_TERM_REQ_FES_PDU_SEQUENCE_ERROR;
    2228                 :          4 :                         goto err;
    2229                 :            :                 }
    2230                 :            : 
    2231   [ +  +  +  + ]:   16521877 :                 switch (pdu->hdr.common.pdu_type) {
    2232                 :   15516810 :                 case SPDK_NVME_TCP_PDU_TYPE_CAPSULE_CMD:
    2233                 :   15516810 :                         expected_hlen = sizeof(struct spdk_nvme_tcp_cmd);
    2234                 :   15516810 :                         pdo = pdu->hdr.common.pdo;
    2235   [ +  +  +  +  :   15516810 :                         if ((tqpair->cpda != 0) && (pdo % ((tqpair->cpda + 1) << 2) != 0)) {
             -  +  +  - ]
    2236                 :          4 :                                 pdo_error = true;
    2237                 :          4 :                                 break;
    2238                 :            :                         }
    2239                 :            : 
    2240         [ +  + ]:   15516806 :                         if (pdu->hdr.common.plen < expected_hlen) {
    2241                 :          4 :                                 plen_error = true;
    2242                 :            :                         }
    2243                 :   15516806 :                         break;
    2244                 :    1005059 :                 case SPDK_NVME_TCP_PDU_TYPE_H2C_DATA:
    2245                 :    1005059 :                         expected_hlen = sizeof(struct spdk_nvme_tcp_h2c_data_hdr);
    2246                 :    1005059 :                         pdo = pdu->hdr.common.pdo;
    2247   [ +  +  +  +  :    1005059 :                         if ((tqpair->cpda != 0) && (pdo % ((tqpair->cpda + 1) << 2) != 0)) {
             -  +  +  - ]
    2248                 :          4 :                                 pdo_error = true;
    2249                 :          4 :                                 break;
    2250                 :            :                         }
    2251         [ +  + ]:    1005055 :                         if (pdu->hdr.common.plen < expected_hlen) {
    2252                 :          4 :                                 plen_error = true;
    2253                 :            :                         }
    2254                 :    1005055 :                         break;
    2255                 :            : 
    2256                 :          4 :                 case SPDK_NVME_TCP_PDU_TYPE_H2C_TERM_REQ:
    2257                 :          4 :                         expected_hlen = sizeof(struct spdk_nvme_tcp_term_req_hdr);
    2258         [ -  + ]:          4 :                         if ((pdu->hdr.common.plen <= expected_hlen) ||
    2259         [ #  # ]:          0 :                             (pdu->hdr.common.plen > SPDK_NVME_TCP_TERM_REQ_PDU_MAX_SIZE)) {
    2260                 :          4 :                                 plen_error = true;
    2261                 :            :                         }
    2262                 :          4 :                         break;
    2263                 :            : 
    2264                 :          4 :                 default:
    2265                 :          4 :                         SPDK_ERRLOG("Unexpected PDU type 0x%02x\n", pdu->hdr.common.pdu_type);
    2266                 :          4 :                         fes = SPDK_NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD;
    2267                 :          4 :                         error_offset = offsetof(struct spdk_nvme_tcp_common_pdu_hdr, pdu_type);
    2268                 :          4 :                         goto err;
    2269                 :            :                 }
    2270                 :            :         }
    2271                 :            : 
    2272         [ +  + ]:   16530064 :         if (pdu->hdr.common.hlen != expected_hlen) {
    2273                 :          4 :                 SPDK_ERRLOG("PDU type=0x%02x, Expected ICReq header length %u, got %u on tqpair=%p\n",
    2274                 :            :                             pdu->hdr.common.pdu_type,
    2275                 :            :                             expected_hlen, pdu->hdr.common.hlen, tqpair);
    2276                 :          4 :                 fes = SPDK_NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD;
    2277                 :          4 :                 error_offset = offsetof(struct spdk_nvme_tcp_common_pdu_hdr, hlen);
    2278                 :          4 :                 goto err;
    2279         [ +  + ]:   16530060 :         } else if (pdo_error) {
    2280                 :          8 :                 fes = SPDK_NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD;
    2281                 :          8 :                 error_offset = offsetof(struct spdk_nvme_tcp_common_pdu_hdr, pdo);
    2282         [ +  + ]:   16530052 :         } else if (plen_error) {
    2283                 :         16 :                 fes = SPDK_NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD;
    2284                 :         16 :                 error_offset = offsetof(struct spdk_nvme_tcp_common_pdu_hdr, plen);
    2285                 :         16 :                 goto err;
    2286                 :            :         } else {
    2287                 :   16530036 :                 nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_PSH);
    2288         [ -  + ]:   16530036 :                 nvme_tcp_pdu_calc_psh_len(tqpair->pdu_in_progress, tqpair->host_hdgst_enable);
    2289                 :   16530036 :                 return;
    2290                 :            :         }
    2291                 :         40 : err:
    2292                 :         40 :         nvmf_tcp_send_c2h_term_req(tqpair, pdu, fes, error_offset);
    2293                 :            : }
    2294                 :            : 
    2295                 :            : static int
    2296                 :   84853667 : nvmf_tcp_sock_process(struct spdk_nvmf_tcp_qpair *tqpair)
    2297                 :            : {
    2298                 :   84853667 :         int rc = 0;
    2299                 :            :         struct nvme_tcp_pdu *pdu;
    2300                 :            :         enum nvme_tcp_pdu_recv_state prev_state;
    2301                 :            :         uint32_t data_len;
    2302                 :   84853667 :         struct spdk_nvmf_tcp_transport *ttransport = SPDK_CONTAINEROF(tqpair->qpair.transport,
    2303                 :            :                         struct spdk_nvmf_tcp_transport, transport);
    2304                 :            : 
    2305                 :            :         /* The loop here is to allow for several back-to-back state changes. */
    2306                 :            :         do {
    2307                 :  138749382 :                 prev_state = tqpair->recv_state;
    2308   [ -  +  -  + ]:  138749382 :                 SPDK_DEBUGLOG(nvmf_tcp, "tqpair(%p) recv pdu entering state %d\n", tqpair, prev_state);
    2309                 :            : 
    2310                 :  138749382 :                 pdu = tqpair->pdu_in_progress;
    2311   [ +  +  -  +  :  138749382 :                 assert(pdu != NULL ||
             -  -  -  - ]
    2312                 :            :                        tqpair->recv_state == NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_READY ||
    2313                 :            :                        tqpair->recv_state == NVME_TCP_PDU_RECV_STATE_QUIESCING ||
    2314                 :            :                        tqpair->recv_state == NVME_TCP_PDU_RECV_STATE_ERROR);
    2315                 :            : 
    2316   [ +  +  +  +  :  138749382 :                 switch (tqpair->recv_state) {
             +  +  +  - ]
    2317                 :            :                 /* Wait for the common header  */
    2318                 :   16538193 :                 case NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_READY:
    2319         [ +  + ]:   16538193 :                         if (!pdu) {
    2320                 :    5302672 :                                 pdu = SLIST_FIRST(&tqpair->tcp_pdu_free_queue);
    2321         [ -  + ]:    5302672 :                                 if (spdk_unlikely(!pdu)) {
    2322                 :          0 :                                         return NVME_TCP_PDU_IN_PROGRESS;
    2323                 :            :                                 }
    2324                 :    5302672 :                                 SLIST_REMOVE_HEAD(&tqpair->tcp_pdu_free_queue, slist);
    2325                 :    5302672 :                                 tqpair->pdu_in_progress = pdu;
    2326                 :    5302672 :                                 tqpair->tcp_pdu_working_count++;
    2327                 :            :                         }
    2328         [ -  + ]:   16538193 :                         memset(pdu, 0, offsetof(struct nvme_tcp_pdu, qpair));
    2329                 :   16538193 :                         nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_CH);
    2330                 :            :                 /* FALLTHROUGH */
    2331                 :   97306747 :                 case NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_CH:
    2332         [ -  + ]:   97306747 :                         if (spdk_unlikely(tqpair->state == NVME_TCP_QPAIR_STATE_INITIALIZING)) {
    2333                 :          0 :                                 return rc;
    2334                 :            :                         }
    2335                 :            : 
    2336                 :   97306747 :                         rc = nvme_tcp_read_data(tqpair->sock,
    2337                 :   97306747 :                                                 sizeof(struct spdk_nvme_tcp_common_pdu_hdr) - pdu->ch_valid_bytes,
    2338                 :   97306747 :                                                 (void *)&pdu->hdr.common + pdu->ch_valid_bytes);
    2339         [ +  + ]:   97306747 :                         if (rc < 0) {
    2340   [ -  +  -  + ]:       8094 :                                 SPDK_DEBUGLOG(nvmf_tcp, "will disconnect tqpair=%p\n", tqpair);
    2341                 :       8094 :                                 nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_QUIESCING);
    2342                 :       8094 :                                 break;
    2343         [ +  + ]:   97298653 :                         } else if (rc > 0) {
    2344                 :   16530038 :                                 pdu->ch_valid_bytes += rc;
    2345   [ +  +  +  + ]:   16530038 :                                 spdk_trace_record(TRACE_TCP_READ_FROM_SOCKET_DONE, tqpair->qpair.qid, rc, 0, tqpair);
    2346                 :            :                         }
    2347                 :            : 
    2348         [ +  + ]:   97298653 :                         if (pdu->ch_valid_bytes < sizeof(struct spdk_nvme_tcp_common_pdu_hdr)) {
    2349                 :   80768621 :                                 return NVME_TCP_PDU_IN_PROGRESS;
    2350                 :            :                         }
    2351                 :            : 
    2352                 :            :                         /* The command header of this PDU has now been read from the socket. */
    2353                 :   16530032 :                         nvmf_tcp_pdu_ch_handle(tqpair);
    2354                 :   16530032 :                         break;
    2355                 :            :                 /* Wait for the pdu specific header  */
    2356                 :   16563778 :                 case NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_PSH:
    2357                 :   16563778 :                         rc = nvme_tcp_read_data(tqpair->sock,
    2358                 :   16563778 :                                                 pdu->psh_len - pdu->psh_valid_bytes,
    2359                 :   16563778 :                                                 (void *)&pdu->hdr.raw + sizeof(struct spdk_nvme_tcp_common_pdu_hdr) + pdu->psh_valid_bytes);
    2360         [ -  + ]:   16563778 :                         if (rc < 0) {
    2361                 :          0 :                                 nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_QUIESCING);
    2362                 :          0 :                                 break;
    2363         [ +  + ]:   16563778 :                         } else if (rc > 0) {
    2364   [ +  +  +  + ]:   16562939 :                                 spdk_trace_record(TRACE_TCP_READ_FROM_SOCKET_DONE, tqpair->qpair.qid, rc, 0, tqpair);
    2365                 :   16562939 :                                 pdu->psh_valid_bytes += rc;
    2366                 :            :                         }
    2367                 :            : 
    2368         [ +  + ]:   16563778 :                         if (pdu->psh_valid_bytes < pdu->psh_len) {
    2369                 :      33746 :                                 return NVME_TCP_PDU_IN_PROGRESS;
    2370                 :            :                         }
    2371                 :            : 
    2372                 :            :                         /* All header(ch, psh, head digist) of this PDU has now been read from the socket. */
    2373                 :   16530032 :                         nvmf_tcp_pdu_psh_handle(tqpair, ttransport);
    2374                 :   16530032 :                         break;
    2375                 :            :                 /* Wait for the req slot */
    2376                 :   15859918 :                 case NVME_TCP_PDU_RECV_STATE_AWAIT_REQ:
    2377                 :   15859918 :                         nvmf_tcp_capsule_cmd_hdr_handle(ttransport, tqpair, pdu);
    2378                 :   15859918 :                         break;
    2379                 :    8609785 :                 case NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_PAYLOAD:
    2380                 :            :                         /* check whether the data is valid, if not we just return */
    2381         [ -  + ]:    8609785 :                         if (!pdu->data_len) {
    2382                 :          0 :                                 return NVME_TCP_PDU_IN_PROGRESS;
    2383                 :            :                         }
    2384                 :            : 
    2385                 :    8609785 :                         data_len = pdu->data_len;
    2386                 :            :                         /* data digest */
    2387   [ +  -  -  +  :    8609785 :                         if (spdk_unlikely((pdu->hdr.common.pdu_type != SPDK_NVME_TCP_PDU_TYPE_H2C_TERM_REQ) &&
                   +  + ]
    2388                 :            :                                           tqpair->host_ddgst_enable)) {
    2389                 :     806103 :                                 data_len += SPDK_NVME_TCP_DIGEST_LEN;
    2390                 :     806103 :                                 pdu->ddgst_enable = true;
    2391                 :            :                         }
    2392                 :            : 
    2393                 :    8609785 :                         rc = nvme_tcp_read_payload_data(tqpair->sock, pdu);
    2394         [ -  + ]:    8609785 :                         if (rc < 0) {
    2395                 :          0 :                                 nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_QUIESCING);
    2396                 :          0 :                                 break;
    2397                 :            :                         }
    2398                 :    8609785 :                         pdu->rw_offset += rc;
    2399                 :            : 
    2400         [ +  + ]:    8609785 :                         if (pdu->rw_offset < data_len) {
    2401                 :    3307113 :                                 return NVME_TCP_PDU_IN_PROGRESS;
    2402                 :            :                         }
    2403                 :            : 
    2404                 :            :                         /* Generate and insert DIF to whole data block received if DIF is enabled */
    2405   [ -  +  -  - ]:    5302672 :                         if (spdk_unlikely(pdu->dif_ctx != NULL) &&
    2406                 :          0 :                             spdk_dif_generate_stream(pdu->data_iov, pdu->data_iovcnt, 0, data_len,
    2407                 :            :                                                      pdu->dif_ctx) != 0) {
    2408                 :          0 :                                 SPDK_ERRLOG("DIF generate failed\n");
    2409                 :          0 :                                 nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_QUIESCING);
    2410                 :          0 :                                 break;
    2411                 :            :                         }
    2412                 :            : 
    2413                 :            :                         /* All of this PDU has now been read from the socket. */
    2414                 :    5302672 :                         nvmf_tcp_pdu_payload_handle(tqpair, pdu);
    2415                 :    5302672 :                         break;
    2416                 :       8094 :                 case NVME_TCP_PDU_RECV_STATE_QUIESCING:
    2417         [ -  + ]:       8094 :                         if (tqpair->tcp_pdu_working_count != 0) {
    2418                 :          0 :                                 return NVME_TCP_PDU_IN_PROGRESS;
    2419                 :            :                         }
    2420                 :       8094 :                         nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_ERROR);
    2421                 :       8094 :                         break;
    2422                 :     401060 :                 case NVME_TCP_PDU_RECV_STATE_ERROR:
    2423   [ +  +  -  +  :     401060 :                         if (spdk_sock_is_connected(tqpair->sock) && tqpair->wait_terminate) {
                   -  + ]
    2424                 :          0 :                                 return NVME_TCP_PDU_IN_PROGRESS;
    2425                 :            :                         }
    2426                 :     401060 :                         return NVME_TCP_PDU_FATAL;
    2427                 :          0 :                 default:
    2428                 :          0 :                         SPDK_ERRLOG("The state(%d) is invalid\n", tqpair->recv_state);
    2429                 :          0 :                         abort();
    2430                 :            :                         break;
    2431                 :            :                 }
    2432         [ +  + ]:   54238842 :         } while (tqpair->recv_state != prev_state);
    2433                 :            : 
    2434                 :     343127 :         return rc;
    2435                 :            : }
    2436                 :            : 
    2437                 :            : static inline void *
    2438                 :       1935 : nvmf_tcp_control_msg_get(struct spdk_nvmf_tcp_control_msg_list *list)
    2439                 :            : {
    2440                 :            :         struct spdk_nvmf_tcp_control_msg *msg;
    2441                 :            : 
    2442         [ -  + ]:       1935 :         assert(list);
    2443                 :            : 
    2444                 :       1935 :         msg = STAILQ_FIRST(&list->free_msgs);
    2445         [ -  + ]:       1935 :         if (!msg) {
    2446   [ #  #  #  # ]:          0 :                 SPDK_DEBUGLOG(nvmf_tcp, "Out of control messages\n");
    2447                 :          0 :                 return NULL;
    2448                 :            :         }
    2449         [ -  + ]:       1935 :         STAILQ_REMOVE_HEAD(&list->free_msgs, link);
    2450                 :       1935 :         return msg;
    2451                 :            : }
    2452                 :            : 
    2453                 :            : static inline void
    2454                 :       1935 : nvmf_tcp_control_msg_put(struct spdk_nvmf_tcp_control_msg_list *list, void *_msg)
    2455                 :            : {
    2456                 :       1935 :         struct spdk_nvmf_tcp_control_msg *msg = _msg;
    2457                 :            : 
    2458         [ -  + ]:       1935 :         assert(list);
    2459         [ -  + ]:       1935 :         STAILQ_INSERT_HEAD(&list->free_msgs, msg, link);
    2460                 :       1935 : }
    2461                 :            : 
    2462                 :            : static int
    2463                 :   14626396 : nvmf_tcp_req_parse_sgl(struct spdk_nvmf_tcp_req *tcp_req,
    2464                 :            :                        struct spdk_nvmf_transport *transport,
    2465                 :            :                        struct spdk_nvmf_transport_poll_group *group)
    2466                 :            : {
    2467                 :   14626396 :         struct spdk_nvmf_request                *req = &tcp_req->req;
    2468                 :            :         struct spdk_nvme_cmd                    *cmd;
    2469                 :            :         struct spdk_nvme_sgl_descriptor         *sgl;
    2470                 :            :         struct spdk_nvmf_tcp_poll_group         *tgroup;
    2471                 :            :         enum spdk_nvme_tcp_term_req_fes         fes;
    2472                 :            :         struct nvme_tcp_pdu                     *pdu;
    2473                 :            :         struct spdk_nvmf_tcp_qpair              *tqpair;
    2474                 :   14626396 :         uint32_t                                length, error_offset = 0;
    2475                 :            : 
    2476                 :   14626396 :         cmd = &req->cmd->nvme_cmd;
    2477                 :   14626396 :         sgl = &cmd->dptr.sgl1;
    2478                 :            : 
    2479         [ +  + ]:   14626396 :         if (sgl->generic.type == SPDK_NVME_SGL_TYPE_TRANSPORT_DATA_BLOCK &&
    2480         [ +  - ]:   10328755 :             sgl->unkeyed.subtype == SPDK_NVME_SGL_SUBTYPE_TRANSPORT) {
    2481                 :            :                 /* get request length from sgl */
    2482                 :   10328755 :                 length = sgl->unkeyed.length;
    2483         [ +  + ]:   10328755 :                 if (spdk_unlikely(length > transport->opts.max_io_size)) {
    2484                 :          4 :                         SPDK_ERRLOG("SGL length 0x%x exceeds max io size 0x%x\n",
    2485                 :            :                                     length, transport->opts.max_io_size);
    2486                 :          4 :                         fes = SPDK_NVME_TCP_TERM_REQ_FES_DATA_TRANSFER_LIMIT_EXCEEDED;
    2487                 :          4 :                         goto fatal_err;
    2488                 :            :                 }
    2489                 :            : 
    2490                 :            :                 /* fill request length and populate iovs */
    2491                 :   10328751 :                 req->length = length;
    2492                 :            : 
    2493   [ -  +  -  + ]:   10328751 :                 SPDK_DEBUGLOG(nvmf_tcp, "Data requested length= 0x%x\n", length);
    2494                 :            : 
    2495         [ +  + ]:   10328751 :                 if (spdk_unlikely(req->dif_enabled)) {
    2496                 :     633386 :                         req->dif.orig_length = length;
    2497                 :     633386 :                         length = spdk_dif_get_length_with_md(length, &req->dif.dif_ctx);
    2498                 :     633386 :                         req->dif.elba_length = length;
    2499                 :            :                 }
    2500                 :            : 
    2501         [ +  + ]:   10328751 :                 if (nvmf_ctrlr_use_zcopy(req)) {
    2502   [ -  +  -  + ]:     677032 :                         SPDK_DEBUGLOG(nvmf_tcp, "Using zero-copy to execute request %p\n", tcp_req);
    2503                 :     677032 :                         req->data_from_pool = false;
    2504                 :     677032 :                         return 0;
    2505                 :            :                 }
    2506                 :            : 
    2507         [ +  + ]:    9651719 :                 if (spdk_nvmf_request_get_buffers(req, group, transport, length)) {
    2508                 :            :                         /* No available buffers. Queue this request up. */
    2509   [ -  +  -  + ]:     180130 :                         SPDK_DEBUGLOG(nvmf_tcp, "No available large data buffers. Queueing request %p\n",
    2510                 :            :                                       tcp_req);
    2511                 :     180130 :                         return 0;
    2512                 :            :                 }
    2513                 :            : 
    2514   [ -  +  -  + ]:    9471589 :                 SPDK_DEBUGLOG(nvmf_tcp, "Request %p took %d buffer/s from central pool, and data=%p\n",
    2515                 :            :                               tcp_req, req->iovcnt, req->iov[0].iov_base);
    2516                 :            : 
    2517                 :    9471589 :                 return 0;
    2518         [ +  - ]:    4297641 :         } else if (sgl->generic.type == SPDK_NVME_SGL_TYPE_DATA_BLOCK &&
    2519         [ +  - ]:    4297641 :                    sgl->unkeyed.subtype == SPDK_NVME_SGL_SUBTYPE_OFFSET) {
    2520                 :    4297641 :                 uint64_t offset = sgl->address;
    2521                 :    4297641 :                 uint32_t max_len = transport->opts.in_capsule_data_size;
    2522                 :            : 
    2523   [ -  +  -  + ]:    4297641 :                 assert(tcp_req->has_in_capsule_data);
    2524                 :            :                 /* Capsule Cmd with In-capsule Data should get data length from pdu header */
    2525                 :    4297641 :                 tqpair = tcp_req->pdu->qpair;
    2526                 :            :                 /* receiving pdu is not same with the pdu in tcp_req */
    2527                 :    4297641 :                 pdu = tqpair->pdu_in_progress;
    2528                 :    4297641 :                 length = pdu->hdr.common.plen - pdu->psh_len - sizeof(struct spdk_nvme_tcp_common_pdu_hdr);
    2529   [ -  +  +  + ]:    4297641 :                 if (tqpair->host_ddgst_enable) {
    2530                 :     475659 :                         length -= SPDK_NVME_TCP_DIGEST_LEN;
    2531                 :            :                 }
    2532                 :            :                 /* This error is not defined in NVMe/TCP spec, take this error as fatal error */
    2533         [ -  + ]:    4297641 :                 if (spdk_unlikely(length != sgl->unkeyed.length)) {
    2534                 :          0 :                         SPDK_ERRLOG("In-Capsule Data length 0x%x is not equal to SGL data length 0x%x\n",
    2535                 :            :                                     length, sgl->unkeyed.length);
    2536                 :          0 :                         fes = SPDK_NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD;
    2537                 :          0 :                         error_offset = offsetof(struct spdk_nvme_tcp_common_pdu_hdr, plen);
    2538                 :          0 :                         goto fatal_err;
    2539                 :            :                 }
    2540                 :            : 
    2541   [ -  +  -  + ]:    4297641 :                 SPDK_DEBUGLOG(nvmf_tcp, "In-capsule data: offset 0x%" PRIx64 ", length 0x%x\n",
    2542                 :            :                               offset, length);
    2543                 :            : 
    2544                 :            :                 /* The NVMe/TCP transport does not use ICDOFF to control the in-capsule data offset. ICDOFF should be '0' */
    2545         [ -  + ]:    4297641 :                 if (spdk_unlikely(offset != 0)) {
    2546                 :            :                         /* Not defined fatal error in NVMe/TCP spec, handle this error as a fatal error */
    2547                 :          0 :                         SPDK_ERRLOG("In-capsule offset 0x%" PRIx64 " should be ZERO in NVMe/TCP\n", offset);
    2548                 :          0 :                         fes = SPDK_NVME_TCP_TERM_REQ_FES_INVALID_DATA_UNSUPPORTED_PARAMETER;
    2549                 :          0 :                         error_offset = offsetof(struct spdk_nvme_tcp_cmd, ccsqe.dptr.sgl1.address);
    2550                 :          0 :                         goto fatal_err;
    2551                 :            :                 }
    2552                 :            : 
    2553         [ +  + ]:    4297641 :                 if (spdk_unlikely(length > max_len)) {
    2554                 :            :                         /* According to the SPEC we should support ICD up to 8192 bytes for admin and fabric commands */
    2555         [ +  - ]:       1935 :                         if (length <= SPDK_NVME_TCP_IN_CAPSULE_DATA_MAX_SIZE &&
    2556   [ -  +  -  - ]:       1935 :                             (cmd->opc == SPDK_NVME_OPC_FABRIC || req->qpair->qid == 0)) {
    2557                 :            : 
    2558                 :            :                                 /* Get a buffer from dedicated list */
    2559   [ -  +  -  + ]:       1935 :                                 SPDK_DEBUGLOG(nvmf_tcp, "Getting a buffer from control msg list\n");
    2560                 :       1935 :                                 tgroup = SPDK_CONTAINEROF(group, struct spdk_nvmf_tcp_poll_group, group);
    2561         [ -  + ]:       1935 :                                 assert(tgroup->control_msg_list);
    2562                 :       1935 :                                 req->iov[0].iov_base = nvmf_tcp_control_msg_get(tgroup->control_msg_list);
    2563         [ -  + ]:       1935 :                                 if (!req->iov[0].iov_base) {
    2564                 :            :                                         /* No available buffers. Queue this request up. */
    2565   [ #  #  #  # ]:          0 :                                         SPDK_DEBUGLOG(nvmf_tcp, "No available ICD buffers. Queueing request %p\n", tcp_req);
    2566                 :          0 :                                         return 0;
    2567                 :            :                                 }
    2568                 :            :                         } else {
    2569                 :          0 :                                 SPDK_ERRLOG("In-capsule data length 0x%x exceeds capsule length 0x%x\n",
    2570                 :            :                                             length, max_len);
    2571                 :          0 :                                 fes = SPDK_NVME_TCP_TERM_REQ_FES_DATA_TRANSFER_LIMIT_EXCEEDED;
    2572                 :          0 :                                 goto fatal_err;
    2573                 :            :                         }
    2574                 :            :                 } else {
    2575                 :    4295706 :                         req->iov[0].iov_base = tcp_req->buf;
    2576                 :            :                 }
    2577                 :            : 
    2578                 :    4297641 :                 req->length = length;
    2579                 :    4297641 :                 req->data_from_pool = false;
    2580                 :            : 
    2581         [ -  + ]:    4297641 :                 if (spdk_unlikely(req->dif_enabled)) {
    2582                 :          0 :                         length = spdk_dif_get_length_with_md(length, &req->dif.dif_ctx);
    2583                 :          0 :                         req->dif.elba_length = length;
    2584                 :            :                 }
    2585                 :            : 
    2586                 :    4297641 :                 req->iov[0].iov_len = length;
    2587                 :    4297641 :                 req->iovcnt = 1;
    2588                 :            : 
    2589                 :    4297641 :                 return 0;
    2590                 :            :         }
    2591                 :            :         /* If we want to handle the problem here, then we can't skip the following data segment.
    2592                 :            :          * Because this function runs before reading data part, now handle all errors as fatal errors. */
    2593                 :          0 :         SPDK_ERRLOG("Invalid NVMf I/O Command SGL:  Type 0x%x, Subtype 0x%x\n",
    2594                 :            :                     sgl->generic.type, sgl->generic.subtype);
    2595                 :          0 :         fes = SPDK_NVME_TCP_TERM_REQ_FES_INVALID_DATA_UNSUPPORTED_PARAMETER;
    2596                 :          0 :         error_offset = offsetof(struct spdk_nvme_tcp_cmd, ccsqe.dptr.sgl1.generic);
    2597                 :          4 : fatal_err:
    2598                 :          4 :         nvmf_tcp_send_c2h_term_req(tcp_req->pdu->qpair, tcp_req->pdu, fes, error_offset);
    2599                 :          4 :         return -1;
    2600                 :            : }
    2601                 :            : 
    2602                 :            : static inline enum spdk_nvme_media_error_status_code
    2603                 :          0 : nvmf_tcp_dif_error_to_compl_status(uint8_t err_type) {
    2604                 :            :         enum spdk_nvme_media_error_status_code result;
    2605                 :            : 
    2606   [ #  #  #  # ]:          0 :         switch (err_type)
    2607                 :            :         {
    2608                 :          0 :         case SPDK_DIF_REFTAG_ERROR:
    2609                 :          0 :                 result = SPDK_NVME_SC_REFERENCE_TAG_CHECK_ERROR;
    2610                 :          0 :                 break;
    2611                 :          0 :         case SPDK_DIF_APPTAG_ERROR:
    2612                 :          0 :                 result = SPDK_NVME_SC_APPLICATION_TAG_CHECK_ERROR;
    2613                 :          0 :                 break;
    2614                 :          0 :         case SPDK_DIF_GUARD_ERROR:
    2615                 :          0 :                 result = SPDK_NVME_SC_GUARD_CHECK_ERROR;
    2616                 :          0 :                 break;
    2617                 :          0 :         default:
    2618                 :          0 :                 SPDK_UNREACHABLE();
    2619                 :            :                 break;
    2620                 :            :         }
    2621                 :            : 
    2622                 :          0 :         return result;
    2623                 :            : }
    2624                 :            : 
    2625                 :            : static void
    2626                 :    9384234 : _nvmf_tcp_send_c2h_data(struct spdk_nvmf_tcp_qpair *tqpair,
    2627                 :            :                         struct spdk_nvmf_tcp_req *tcp_req)
    2628                 :            : {
    2629                 :    9384234 :         struct spdk_nvmf_tcp_transport *ttransport = SPDK_CONTAINEROF(
    2630                 :            :                                 tqpair->qpair.transport, struct spdk_nvmf_tcp_transport, transport);
    2631                 :            :         struct nvme_tcp_pdu *rsp_pdu;
    2632                 :            :         struct spdk_nvme_tcp_c2h_data_hdr *c2h_data;
    2633                 :            :         uint32_t plen, pdo, alignment;
    2634                 :            :         int rc;
    2635                 :            : 
    2636   [ -  +  -  + ]:    9384234 :         SPDK_DEBUGLOG(nvmf_tcp, "enter\n");
    2637                 :            : 
    2638                 :    9384234 :         rsp_pdu = tcp_req->pdu;
    2639         [ -  + ]:    9384234 :         assert(rsp_pdu != NULL);
    2640                 :            : 
    2641                 :    9384234 :         c2h_data = &rsp_pdu->hdr.c2h_data;
    2642                 :    9384234 :         c2h_data->common.pdu_type = SPDK_NVME_TCP_PDU_TYPE_C2H_DATA;
    2643                 :    9384234 :         plen = c2h_data->common.hlen = sizeof(*c2h_data);
    2644                 :            : 
    2645   [ -  +  +  + ]:    9384234 :         if (tqpair->host_hdgst_enable) {
    2646                 :     495966 :                 plen += SPDK_NVME_TCP_DIGEST_LEN;
    2647                 :     495966 :                 c2h_data->common.flags |= SPDK_NVME_TCP_CH_FLAGS_HDGSTF;
    2648                 :            :         }
    2649                 :            : 
    2650                 :            :         /* set the psh */
    2651                 :    9384234 :         c2h_data->cccid = tcp_req->req.cmd->nvme_cmd.cid;
    2652                 :    9384234 :         c2h_data->datal = tcp_req->req.length - tcp_req->pdu->rw_offset;
    2653                 :    9384234 :         c2h_data->datao = tcp_req->pdu->rw_offset;
    2654                 :            : 
    2655                 :            :         /* set the padding */
    2656                 :    9384234 :         rsp_pdu->padding_len = 0;
    2657                 :    9384234 :         pdo = plen;
    2658         [ -  + ]:    9384234 :         if (tqpair->cpda) {
    2659         [ #  # ]:          0 :                 alignment = (tqpair->cpda + 1) << 2;
    2660   [ #  #  #  # ]:          0 :                 if (plen % alignment != 0) {
    2661         [ #  # ]:          0 :                         pdo = (plen + alignment) / alignment * alignment;
    2662                 :          0 :                         rsp_pdu->padding_len = pdo - plen;
    2663                 :          0 :                         plen = pdo;
    2664                 :            :                 }
    2665                 :            :         }
    2666                 :            : 
    2667                 :    9384234 :         c2h_data->common.pdo = pdo;
    2668                 :    9384234 :         plen += c2h_data->datal;
    2669   [ -  +  +  + ]:    9384234 :         if (tqpair->host_ddgst_enable) {
    2670                 :     923335 :                 c2h_data->common.flags |= SPDK_NVME_TCP_CH_FLAGS_DDGSTF;
    2671                 :     923335 :                 plen += SPDK_NVME_TCP_DIGEST_LEN;
    2672                 :            :         }
    2673                 :            : 
    2674                 :    9384234 :         c2h_data->common.plen = plen;
    2675                 :            : 
    2676         [ +  + ]:    9384234 :         if (spdk_unlikely(tcp_req->req.dif_enabled)) {
    2677                 :     896694 :                 rsp_pdu->dif_ctx = &tcp_req->req.dif.dif_ctx;
    2678                 :            :         }
    2679                 :            : 
    2680                 :    9384234 :         nvme_tcp_pdu_set_data_buf(rsp_pdu, tcp_req->req.iov, tcp_req->req.iovcnt,
    2681                 :            :                                   c2h_data->datao, c2h_data->datal);
    2682                 :            : 
    2683                 :            : 
    2684                 :    9384234 :         c2h_data->common.flags |= SPDK_NVME_TCP_C2H_DATA_FLAGS_LAST_PDU;
    2685                 :            :         /* Need to send the capsule response if response is not all 0 */
    2686   [ +  +  +  + ]:    9384234 :         if (ttransport->tcp_opts.c2h_success &&
    2687   [ +  +  +  - ]:     404554 :             tcp_req->rsp.cdw0 == 0 && tcp_req->rsp.cdw1 == 0) {
    2688                 :     404550 :                 c2h_data->common.flags |= SPDK_NVME_TCP_C2H_DATA_FLAGS_SUCCESS;
    2689                 :            :         }
    2690                 :            : 
    2691         [ +  + ]:    9384234 :         if (spdk_unlikely(tcp_req->req.dif_enabled)) {
    2692                 :     896694 :                 struct spdk_nvme_cpl *rsp = &tcp_req->req.rsp->nvme_cpl;
    2693                 :     896694 :                 struct spdk_dif_error err_blk = {};
    2694                 :     896694 :                 uint32_t mapped_length = 0;
    2695                 :     896694 :                 uint32_t available_iovs = SPDK_COUNTOF(rsp_pdu->iov);
    2696                 :     896694 :                 uint32_t ddgst_len = 0;
    2697                 :            : 
    2698   [ -  +  +  + ]:     896694 :                 if (tqpair->host_ddgst_enable) {
    2699                 :            :                         /* Data digest consumes additional iov entry */
    2700                 :     193767 :                         available_iovs--;
    2701                 :            :                         /* plen needs to be updated since nvme_tcp_build_iovs compares expected and actual plen */
    2702                 :     193767 :                         ddgst_len = SPDK_NVME_TCP_DIGEST_LEN;
    2703                 :     193767 :                         c2h_data->common.plen -= ddgst_len;
    2704                 :            :                 }
    2705                 :            :                 /* Temp call to estimate if data can be described by limited number of iovs.
    2706                 :            :                  * iov vector will be rebuilt in nvmf_tcp_qpair_write_pdu */
    2707         [ -  + ]:     896694 :                 nvme_tcp_build_iovs(rsp_pdu->iov, available_iovs, rsp_pdu, tqpair->host_hdgst_enable,
    2708                 :            :                                     false, &mapped_length);
    2709                 :            : 
    2710         [ +  + ]:     896694 :                 if (mapped_length != c2h_data->common.plen) {
    2711                 :     263308 :                         c2h_data->datal = mapped_length - (c2h_data->common.plen - c2h_data->datal);
    2712   [ -  +  -  + ]:     263308 :                         SPDK_DEBUGLOG(nvmf_tcp,
    2713                 :            :                                       "Part C2H, data_len %u (of %u), PDU len %u, updated PDU len %u, offset %u\n",
    2714                 :            :                                       c2h_data->datal, tcp_req->req.length, c2h_data->common.plen, mapped_length, rsp_pdu->rw_offset);
    2715                 :     263308 :                         c2h_data->common.plen = mapped_length;
    2716                 :            : 
    2717                 :            :                         /* Rebuild pdu->data_iov since data length is changed */
    2718                 :     263308 :                         nvme_tcp_pdu_set_data_buf(rsp_pdu, tcp_req->req.iov, tcp_req->req.iovcnt, c2h_data->datao,
    2719                 :            :                                                   c2h_data->datal);
    2720                 :            : 
    2721                 :     263308 :                         c2h_data->common.flags &= ~(SPDK_NVME_TCP_C2H_DATA_FLAGS_LAST_PDU |
    2722                 :            :                                                     SPDK_NVME_TCP_C2H_DATA_FLAGS_SUCCESS);
    2723                 :            :                 }
    2724                 :            : 
    2725                 :     896694 :                 c2h_data->common.plen += ddgst_len;
    2726                 :            : 
    2727         [ -  + ]:     896694 :                 assert(rsp_pdu->rw_offset <= tcp_req->req.length);
    2728                 :            : 
    2729                 :     896694 :                 rc = spdk_dif_verify_stream(rsp_pdu->data_iov, rsp_pdu->data_iovcnt,
    2730                 :            :                                             0, rsp_pdu->data_len, rsp_pdu->dif_ctx, &err_blk);
    2731         [ -  + ]:     896694 :                 if (rc != 0) {
    2732                 :          0 :                         SPDK_ERRLOG("DIF error detected. type=%d, offset=%" PRIu32 "\n",
    2733                 :            :                                     err_blk.err_type, err_blk.err_offset);
    2734                 :          0 :                         rsp->status.sct = SPDK_NVME_SCT_MEDIA_ERROR;
    2735                 :          0 :                         rsp->status.sc = nvmf_tcp_dif_error_to_compl_status(err_blk.err_type);
    2736                 :          0 :                         nvmf_tcp_send_capsule_resp_pdu(tcp_req, tqpair);
    2737                 :          0 :                         return;
    2738                 :            :                 }
    2739                 :            :         }
    2740                 :            : 
    2741                 :    9384234 :         rsp_pdu->rw_offset += c2h_data->datal;
    2742                 :    9384234 :         nvmf_tcp_qpair_write_req_pdu(tqpair, tcp_req, nvmf_tcp_pdu_c2h_data_complete, tcp_req);
    2743                 :            : }
    2744                 :            : 
    2745                 :            : static void
    2746                 :    9120926 : nvmf_tcp_send_c2h_data(struct spdk_nvmf_tcp_qpair *tqpair,
    2747                 :            :                        struct spdk_nvmf_tcp_req *tcp_req)
    2748                 :            : {
    2749                 :    9120926 :         nvmf_tcp_req_pdu_init(tcp_req);
    2750                 :    9120926 :         _nvmf_tcp_send_c2h_data(tqpair, tcp_req);
    2751                 :    9120926 : }
    2752                 :            : 
    2753                 :            : static int
    2754                 :   15512556 : request_transfer_out(struct spdk_nvmf_request *req)
    2755                 :            : {
    2756                 :            :         struct spdk_nvmf_tcp_req        *tcp_req;
    2757                 :            :         struct spdk_nvmf_qpair          *qpair;
    2758                 :            :         struct spdk_nvmf_tcp_qpair      *tqpair;
    2759                 :            :         struct spdk_nvme_cpl            *rsp;
    2760                 :            : 
    2761   [ -  +  -  + ]:   15512556 :         SPDK_DEBUGLOG(nvmf_tcp, "enter\n");
    2762                 :            : 
    2763                 :   15512556 :         qpair = req->qpair;
    2764                 :   15512556 :         rsp = &req->rsp->nvme_cpl;
    2765                 :   15512556 :         tcp_req = SPDK_CONTAINEROF(req, struct spdk_nvmf_tcp_req, req);
    2766                 :            : 
    2767                 :            :         /* Advance our sq_head pointer */
    2768         [ +  + ]:   15512556 :         if (qpair->sq_head == qpair->sq_head_max) {
    2769                 :     123131 :                 qpair->sq_head = 0;
    2770                 :            :         } else {
    2771                 :   15389425 :                 qpair->sq_head++;
    2772                 :            :         }
    2773                 :   15512556 :         rsp->sqhd = qpair->sq_head;
    2774                 :            : 
    2775                 :   15512556 :         tqpair = SPDK_CONTAINEROF(tcp_req->req.qpair, struct spdk_nvmf_tcp_qpair, qpair);
    2776                 :   15512556 :         nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_TRANSFERRING_CONTROLLER_TO_HOST);
    2777   [ +  +  +  + ]:   15512556 :         if (rsp->status.sc == SPDK_NVME_SC_SUCCESS && req->xfer == SPDK_NVME_DATA_CONTROLLER_TO_HOST) {
    2778                 :    9120910 :                 nvmf_tcp_send_c2h_data(tqpair, tcp_req);
    2779                 :            :         } else {
    2780                 :    6391646 :                 nvmf_tcp_send_capsule_resp_pdu(tcp_req, tqpair);
    2781                 :            :         }
    2782                 :            : 
    2783                 :   15512556 :         return 0;
    2784                 :            : }
    2785                 :            : 
    2786                 :            : static void
    2787                 :   15516807 : nvmf_tcp_check_fused_ordering(struct spdk_nvmf_tcp_transport *ttransport,
    2788                 :            :                               struct spdk_nvmf_tcp_qpair *tqpair,
    2789                 :            :                               struct spdk_nvmf_tcp_req *tcp_req)
    2790                 :            : {
    2791                 :            :         enum spdk_nvme_cmd_fuse last, next;
    2792                 :            : 
    2793         [ +  + ]:   15516807 :         last = tqpair->fused_first ? tqpair->fused_first->cmd.fuse : SPDK_NVME_CMD_FUSE_NONE;
    2794                 :   15516807 :         next = tcp_req->cmd.fuse;
    2795                 :            : 
    2796         [ -  + ]:   15516807 :         assert(last != SPDK_NVME_CMD_FUSE_SECOND);
    2797                 :            : 
    2798   [ +  +  +  + ]:   15516807 :         if (spdk_likely(last == SPDK_NVME_CMD_FUSE_NONE && next == SPDK_NVME_CMD_FUSE_NONE)) {
    2799                 :   15512651 :                 return;
    2800                 :            :         }
    2801                 :            : 
    2802         [ +  + ]:       4156 :         if (last == SPDK_NVME_CMD_FUSE_FIRST) {
    2803         [ +  - ]:       2078 :                 if (next == SPDK_NVME_CMD_FUSE_SECOND) {
    2804                 :            :                         /* This is a valid pair of fused commands.  Point them at each other
    2805                 :            :                          * so they can be submitted consecutively once ready to be executed.
    2806                 :            :                          */
    2807                 :       2078 :                         tqpair->fused_first->fused_pair = tcp_req;
    2808                 :       2078 :                         tcp_req->fused_pair = tqpair->fused_first;
    2809                 :       2078 :                         tqpair->fused_first = NULL;
    2810                 :       2078 :                         return;
    2811                 :            :                 } else {
    2812                 :            :                         /* Mark the last req as failed since it wasn't followed by a SECOND. */
    2813                 :          0 :                         tqpair->fused_first->fused_failed = true;
    2814                 :            : 
    2815                 :            :                         /*
    2816                 :            :                          * If the last req is in READY_TO_EXECUTE state, then call
    2817                 :            :                          * nvmf_tcp_req_process(), otherwise nothing else will kick it.
    2818                 :            :                          */
    2819         [ #  # ]:          0 :                         if (tqpair->fused_first->state == TCP_REQUEST_STATE_READY_TO_EXECUTE) {
    2820                 :          0 :                                 nvmf_tcp_req_process(ttransport, tqpair->fused_first);
    2821                 :            :                         }
    2822                 :            : 
    2823                 :          0 :                         tqpair->fused_first = NULL;
    2824                 :            :                 }
    2825                 :            :         }
    2826                 :            : 
    2827         [ +  - ]:       2078 :         if (next == SPDK_NVME_CMD_FUSE_FIRST) {
    2828                 :            :                 /* Set tqpair->fused_first here so that we know to check that the next request
    2829                 :            :                  * is a SECOND (and to fail this one if it isn't).
    2830                 :            :                  */
    2831                 :       2078 :                 tqpair->fused_first = tcp_req;
    2832         [ #  # ]:          0 :         } else if (next == SPDK_NVME_CMD_FUSE_SECOND) {
    2833                 :            :                 /* Mark this req failed since it is a SECOND and the last one was not a FIRST. */
    2834                 :          0 :                 tcp_req->fused_failed = true;
    2835                 :            :         }
    2836                 :            : }
    2837                 :            : 
    2838                 :            : static bool
    2839                 :   53769895 : nvmf_tcp_req_process(struct spdk_nvmf_tcp_transport *ttransport,
    2840                 :            :                      struct spdk_nvmf_tcp_req *tcp_req)
    2841                 :            : {
    2842                 :            :         struct spdk_nvmf_tcp_qpair              *tqpair;
    2843                 :            :         uint32_t                                plen;
    2844                 :            :         struct nvme_tcp_pdu                     *pdu;
    2845                 :            :         enum spdk_nvmf_tcp_req_state            prev_state;
    2846                 :   53769895 :         bool                                    progress = false;
    2847                 :   53769895 :         struct spdk_nvmf_transport              *transport = &ttransport->transport;
    2848                 :            :         struct spdk_nvmf_transport_poll_group   *group;
    2849                 :            :         struct spdk_nvmf_tcp_poll_group         *tgroup;
    2850                 :            : 
    2851                 :   53769895 :         tqpair = SPDK_CONTAINEROF(tcp_req->req.qpair, struct spdk_nvmf_tcp_qpair, qpair);
    2852                 :   53769895 :         group = &tqpair->group->group;
    2853         [ -  + ]:   53769895 :         assert(tcp_req->state != TCP_REQUEST_STATE_FREE);
    2854                 :            : 
    2855                 :            :         /* If the qpair is not active, we need to abort the outstanding requests. */
    2856         [ +  + ]:   53769895 :         if (tqpair->qpair.state != SPDK_NVMF_QPAIR_ACTIVE) {
    2857         [ -  + ]:     317676 :                 if (tcp_req->state == TCP_REQUEST_STATE_NEED_BUFFER) {
    2858   [ #  #  #  #  :          0 :                         STAILQ_REMOVE(&group->pending_buf_queue, &tcp_req->req, spdk_nvmf_request, buf_link);
             #  #  #  # ]
    2859                 :            :                 }
    2860                 :     317676 :                 nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_COMPLETED);
    2861                 :            :         }
    2862                 :            : 
    2863                 :            :         /* The loop here is to allow for several back-to-back state changes. */
    2864                 :            :         do {
    2865                 :  146772242 :                 prev_state = tcp_req->state;
    2866                 :            : 
    2867   [ -  +  -  + ]:  146772242 :                 SPDK_DEBUGLOG(nvmf_tcp, "Request %p entering state %d on tqpair=%p\n", tcp_req, prev_state,
    2868                 :            :                               tqpair);
    2869                 :            : 
    2870   [ +  +  +  +  :  146772242 :                 switch (tcp_req->state) {
          +  +  +  +  +  
          +  +  +  +  +  
                   +  - ]
    2871                 :   15828781 :                 case TCP_REQUEST_STATE_FREE:
    2872                 :            :                         /* Some external code must kick a request into TCP_REQUEST_STATE_NEW
    2873                 :            :                          * to escape this state. */
    2874                 :   15828781 :                         break;
    2875                 :   15516807 :                 case TCP_REQUEST_STATE_NEW:
    2876   [ +  +  +  + ]:   15516807 :                         spdk_trace_record(TRACE_TCP_REQUEST_STATE_NEW, tqpair->qpair.qid, 0, (uintptr_t)tcp_req, tqpair);
    2877                 :            : 
    2878                 :            :                         /* copy the cmd from the receive pdu */
    2879                 :   15516807 :                         tcp_req->cmd = tqpair->pdu_in_progress->hdr.capsule_cmd.ccsqe;
    2880                 :            : 
    2881         [ +  + ]:   15516807 :                         if (spdk_unlikely(spdk_nvmf_request_get_dif_ctx(&tcp_req->req, &tcp_req->req.dif.dif_ctx))) {
    2882                 :     633386 :                                 tcp_req->req.dif_enabled = true;
    2883                 :     633386 :                                 tqpair->pdu_in_progress->dif_ctx = &tcp_req->req.dif.dif_ctx;
    2884                 :            :                         }
    2885                 :            : 
    2886                 :   15516807 :                         nvmf_tcp_check_fused_ordering(ttransport, tqpair, tcp_req);
    2887                 :            : 
    2888                 :            :                         /* The next state transition depends on the data transfer needs of this request. */
    2889                 :   15516807 :                         tcp_req->req.xfer = spdk_nvmf_req_get_xfer(&tcp_req->req);
    2890                 :            : 
    2891         [ +  + ]:   15516807 :                         if (spdk_unlikely(tcp_req->req.xfer == SPDK_NVME_DATA_BIDIRECTIONAL)) {
    2892                 :          4 :                                 nvmf_tcp_req_set_cpl(tcp_req, SPDK_NVME_SCT_GENERIC, SPDK_NVME_SC_INVALID_OPCODE);
    2893                 :          4 :                                 nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_READY);
    2894                 :          4 :                                 nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_READY_TO_COMPLETE);
    2895   [ -  +  -  + ]:          4 :                                 SPDK_DEBUGLOG(nvmf_tcp, "Request %p: invalid xfer type (BIDIRECTIONAL)\n", tcp_req);
    2896                 :          4 :                                 break;
    2897                 :            :                         }
    2898                 :            : 
    2899                 :            :                         /* If no data to transfer, ready to execute. */
    2900         [ +  + ]:   15516803 :                         if (tcp_req->req.xfer == SPDK_NVME_DATA_NONE) {
    2901                 :            :                                 /* Reset the tqpair receiving pdu state */
    2902                 :    1070533 :                                 nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_READY);
    2903                 :    1070533 :                                 nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_READY_TO_EXECUTE);
    2904                 :    1070533 :                                 break;
    2905                 :            :                         }
    2906                 :            : 
    2907                 :   14446270 :                         pdu = tqpair->pdu_in_progress;
    2908                 :   14446270 :                         plen = pdu->hdr.common.hlen;
    2909   [ -  +  +  + ]:   14446270 :                         if (tqpair->host_hdgst_enable) {
    2910                 :     492786 :                                 plen += SPDK_NVME_TCP_DIGEST_LEN;
    2911                 :            :                         }
    2912         [ +  + ]:   14446270 :                         if (pdu->hdr.common.plen != plen) {
    2913                 :    4297653 :                                 tcp_req->has_in_capsule_data = true;
    2914                 :            :                         } else {
    2915                 :            :                                 /* Data is transmitted by C2H PDUs */
    2916                 :   10148617 :                                 nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_READY);
    2917                 :            :                         }
    2918                 :            : 
    2919                 :   14446270 :                         nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_NEED_BUFFER);
    2920                 :   14446270 :                         STAILQ_INSERT_TAIL(&group->pending_buf_queue, &tcp_req->req, buf_link);
    2921                 :   14446270 :                         break;
    2922                 :   15381432 :                 case TCP_REQUEST_STATE_NEED_BUFFER:
    2923   [ +  +  +  + ]:   15381432 :                         spdk_trace_record(TRACE_TCP_REQUEST_STATE_NEED_BUFFER, tqpair->qpair.qid, 0, (uintptr_t)tcp_req,
    2924                 :            :                                           tqpair);
    2925                 :            : 
    2926         [ -  + ]:   15381432 :                         assert(tcp_req->req.xfer != SPDK_NVME_DATA_NONE);
    2927                 :            : 
    2928   [ -  +  +  +  :   15381432 :                         if (!tcp_req->has_in_capsule_data && (&tcp_req->req != STAILQ_FIRST(&group->pending_buf_queue))) {
                   +  + ]
    2929   [ -  +  -  + ]:     755036 :                                 SPDK_DEBUGLOG(nvmf_tcp,
    2930                 :            :                                               "Not the first element to wait for the buf for tcp_req(%p) on tqpair=%p\n",
    2931                 :            :                                               tcp_req, tqpair);
    2932                 :            :                                 /* This request needs to wait in line to obtain a buffer */
    2933                 :     755036 :                                 break;
    2934                 :            :                         }
    2935                 :            : 
    2936                 :            :                         /* Try to get a data buffer */
    2937         [ +  + ]:   14626396 :                         if (nvmf_tcp_req_parse_sgl(tcp_req, transport, group) < 0) {
    2938                 :          4 :                                 break;
    2939                 :            :                         }
    2940                 :            : 
    2941                 :            :                         /* Get a zcopy buffer if the request can be serviced through zcopy */
    2942         [ +  + ]:   14626392 :                         if (spdk_nvmf_request_using_zcopy(&tcp_req->req)) {
    2943         [ -  + ]:     677032 :                                 if (spdk_unlikely(tcp_req->req.dif_enabled)) {
    2944         [ #  # ]:          0 :                                         assert(tcp_req->req.dif.elba_length >= tcp_req->req.length);
    2945                 :          0 :                                         tcp_req->req.length = tcp_req->req.dif.elba_length;
    2946                 :            :                                 }
    2947                 :            : 
    2948   [ +  -  +  -  :     677032 :                                 STAILQ_REMOVE(&group->pending_buf_queue, &tcp_req->req, spdk_nvmf_request, buf_link);
             -  -  -  - ]
    2949                 :     677032 :                                 nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_AWAITING_ZCOPY_START);
    2950                 :     677032 :                                 spdk_nvmf_request_zcopy_start(&tcp_req->req);
    2951                 :     677032 :                                 break;
    2952                 :            :                         }
    2953                 :            : 
    2954         [ +  + ]:   13949360 :                         if (tcp_req->req.iovcnt < 1) {
    2955   [ -  +  -  + ]:     180130 :                                 SPDK_DEBUGLOG(nvmf_tcp, "No buffer allocated for tcp_req(%p) on tqpair(%p\n)",
    2956                 :            :                                               tcp_req, tqpair);
    2957                 :            :                                 /* No buffers available. */
    2958                 :     180130 :                                 break;
    2959                 :            :                         }
    2960                 :            : 
    2961   [ +  +  +  +  :   13769230 :                         STAILQ_REMOVE(&group->pending_buf_queue, &tcp_req->req, spdk_nvmf_request, buf_link);
             -  +  +  - ]
    2962                 :            : 
    2963                 :            :                         /* If data is transferring from host to controller, we need to do a transfer from the host. */
    2964         [ +  + ]:   13769230 :                         if (tcp_req->req.xfer == SPDK_NVME_DATA_HOST_TO_CONTROLLER) {
    2965         [ +  + ]:    5041906 :                                 if (tcp_req->req.data_from_pool) {
    2966   [ -  +  -  + ]:     744261 :                                         SPDK_DEBUGLOG(nvmf_tcp, "Sending R2T for tcp_req(%p) on tqpair=%p\n", tcp_req, tqpair);
    2967                 :     744261 :                                         nvmf_tcp_send_r2t_pdu(tqpair, tcp_req);
    2968                 :            :                                 } else {
    2969                 :            :                                         struct nvme_tcp_pdu *pdu;
    2970                 :            : 
    2971                 :    4297645 :                                         nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_TRANSFERRING_HOST_TO_CONTROLLER);
    2972                 :            : 
    2973                 :    4297645 :                                         pdu = tqpair->pdu_in_progress;
    2974   [ -  +  -  + ]:    4297645 :                                         SPDK_DEBUGLOG(nvmf_tcp, "Not need to send r2t for tcp_req(%p) on tqpair=%p\n", tcp_req,
    2975                 :            :                                                       tqpair);
    2976                 :            :                                         /* No need to send r2t, contained in the capsuled data */
    2977                 :    4297645 :                                         nvme_tcp_pdu_set_data_buf(pdu, tcp_req->req.iov, tcp_req->req.iovcnt,
    2978                 :            :                                                                   0, tcp_req->req.length);
    2979                 :    4297645 :                                         nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_PAYLOAD);
    2980                 :            :                                 }
    2981                 :    5041906 :                                 break;
    2982                 :            :                         }
    2983                 :            : 
    2984                 :    8727324 :                         nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_READY_TO_EXECUTE);
    2985                 :    8727324 :                         break;
    2986                 :     677032 :                 case TCP_REQUEST_STATE_AWAITING_ZCOPY_START:
    2987   [ +  -  +  - ]:     677032 :                         spdk_trace_record(TRACE_TCP_REQUEST_STATE_AWAIT_ZCOPY_START, tqpair->qpair.qid, 0,
    2988                 :            :                                           (uintptr_t)tcp_req, tqpair);
    2989                 :            :                         /* Some external code must kick a request into  TCP_REQUEST_STATE_ZCOPY_START_COMPLETED
    2990                 :            :                          * to escape this state. */
    2991                 :     677032 :                         break;
    2992                 :     677032 :                 case TCP_REQUEST_STATE_ZCOPY_START_COMPLETED:
    2993   [ +  -  +  - ]:     677032 :                         spdk_trace_record(TRACE_TCP_REQUEST_STATE_ZCOPY_START_COMPLETED, tqpair->qpair.qid, 0,
    2994                 :            :                                           (uintptr_t)tcp_req, tqpair);
    2995   [ +  +  -  + ]:     677032 :                         if (spdk_unlikely(spdk_nvme_cpl_is_error(&tcp_req->req.rsp->nvme_cpl))) {
    2996   [ -  +  -  + ]:       1320 :                                 SPDK_DEBUGLOG(nvmf_tcp, "Zero-copy start failed for tcp_req(%p) on tqpair=%p\n",
    2997                 :            :                                               tcp_req, tqpair);
    2998                 :       1320 :                                 nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_READY_TO_COMPLETE);
    2999                 :       1320 :                                 break;
    3000                 :            :                         }
    3001         [ +  + ]:     675712 :                         if (tcp_req->req.xfer == SPDK_NVME_DATA_HOST_TO_CONTROLLER) {
    3002   [ -  +  -  + ]:     260910 :                                 SPDK_DEBUGLOG(nvmf_tcp, "Sending R2T for tcp_req(%p) on tqpair=%p\n", tcp_req, tqpair);
    3003                 :     260910 :                                 nvmf_tcp_send_r2t_pdu(tqpair, tcp_req);
    3004                 :            :                         } else {
    3005                 :     414802 :                                 nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_EXECUTED);
    3006                 :            :                         }
    3007                 :     675712 :                         break;
    3008                 :    1005171 :                 case TCP_REQUEST_STATE_AWAITING_R2T_ACK:
    3009   [ +  -  +  + ]:    1005171 :                         spdk_trace_record(TRACE_TCP_REQUEST_STATE_AWAIT_R2T_ACK, tqpair->qpair.qid, 0, (uintptr_t)tcp_req,
    3010                 :            :                                           tqpair);
    3011                 :            :                         /* The R2T completion or the h2c data incoming will kick it out of this state. */
    3012                 :    1005171 :                         break;
    3013                 :    4297645 :                 case TCP_REQUEST_STATE_TRANSFERRING_HOST_TO_CONTROLLER:
    3014                 :            : 
    3015   [ +  +  +  + ]:    4297645 :                         spdk_trace_record(TRACE_TCP_REQUEST_STATE_TRANSFERRING_HOST_TO_CONTROLLER, tqpair->qpair.qid, 0,
    3016                 :            :                                           (uintptr_t)tcp_req, tqpair);
    3017                 :            :                         /* Some external code must kick a request into TCP_REQUEST_STATE_READY_TO_EXECUTE
    3018                 :            :                          * to escape this state. */
    3019                 :    4297645 :                         break;
    3020                 :   15098738 :                 case TCP_REQUEST_STATE_READY_TO_EXECUTE:
    3021   [ +  +  +  + ]:   15098738 :                         spdk_trace_record(TRACE_TCP_REQUEST_STATE_READY_TO_EXECUTE, tqpair->qpair.qid, 0,
    3022                 :            :                                           (uintptr_t)tcp_req, tqpair);
    3023                 :            : 
    3024         [ +  + ]:   15098738 :                         if (spdk_unlikely(tcp_req->req.dif_enabled)) {
    3025         [ -  + ]:     633386 :                                 assert(tcp_req->req.dif.elba_length >= tcp_req->req.length);
    3026                 :     633386 :                                 tcp_req->req.length = tcp_req->req.dif.elba_length;
    3027                 :            :                         }
    3028                 :            : 
    3029         [ +  + ]:   15098738 :                         if (tcp_req->cmd.fuse != SPDK_NVME_CMD_FUSE_NONE) {
    3030   [ -  +  -  + ]:       4156 :                                 if (tcp_req->fused_failed) {
    3031                 :            :                                         /* This request failed FUSED semantics.  Fail it immediately, without
    3032                 :            :                                          * even sending it to the target layer.
    3033                 :            :                                          */
    3034                 :          0 :                                         nvmf_tcp_req_set_cpl(tcp_req, SPDK_NVME_SCT_GENERIC, SPDK_NVME_SC_ABORTED_MISSING_FUSED);
    3035                 :          0 :                                         nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_READY_TO_COMPLETE);
    3036                 :          0 :                                         break;
    3037                 :            :                                 }
    3038                 :            : 
    3039         [ +  + ]:       4156 :                                 if (tcp_req->fused_pair == NULL ||
    3040         [ +  - ]:       2078 :                                     tcp_req->fused_pair->state != TCP_REQUEST_STATE_READY_TO_EXECUTE) {
    3041                 :            :                                         /* This request is ready to execute, but either we don't know yet if it's
    3042                 :            :                                          * valid - i.e. this is a FIRST but we haven't received the next request yet),
    3043                 :            :                                          * or the other request of this fused pair isn't ready to execute. So
    3044                 :            :                                          * break here and this request will get processed later either when the
    3045                 :            :                                          * other request is ready or we find that this request isn't valid.
    3046                 :            :                                          */
    3047                 :            :                                         break;
    3048                 :            :                                 }
    3049                 :            :                         }
    3050                 :            : 
    3051         [ +  + ]:   15096660 :                         if (!spdk_nvmf_request_using_zcopy(&tcp_req->req)) {
    3052                 :   14835750 :                                 nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_EXECUTING);
    3053                 :            :                                 /* If we get to this point, and this request is a fused command, we know that
    3054                 :            :                                  * it is part of a valid sequence (FIRST followed by a SECOND) and that both
    3055                 :            :                                  * requests are READY_TO_EXECUTE.  So call spdk_nvmf_request_exec() both on this
    3056                 :            :                                  * request, and the other request of the fused pair, in the correct order.
    3057                 :            :                                  * Also clear the ->fused_pair pointers on both requests, since after this point
    3058                 :            :                                  * we no longer need to maintain the relationship between these two requests.
    3059                 :            :                                  */
    3060         [ +  + ]:   14835750 :                                 if (tcp_req->cmd.fuse == SPDK_NVME_CMD_FUSE_SECOND) {
    3061         [ -  + ]:       2078 :                                         assert(tcp_req->fused_pair != NULL);
    3062         [ -  + ]:       2078 :                                         assert(tcp_req->fused_pair->fused_pair == tcp_req);
    3063                 :       2078 :                                         nvmf_tcp_req_set_state(tcp_req->fused_pair, TCP_REQUEST_STATE_EXECUTING);
    3064                 :       2078 :                                         spdk_nvmf_request_exec(&tcp_req->fused_pair->req);
    3065                 :       2078 :                                         tcp_req->fused_pair->fused_pair = NULL;
    3066                 :       2078 :                                         tcp_req->fused_pair = NULL;
    3067                 :            :                                 }
    3068                 :   14835750 :                                 spdk_nvmf_request_exec(&tcp_req->req);
    3069         [ -  + ]:   14835750 :                                 if (tcp_req->cmd.fuse == SPDK_NVME_CMD_FUSE_FIRST) {
    3070         [ #  # ]:          0 :                                         assert(tcp_req->fused_pair != NULL);
    3071         [ #  # ]:          0 :                                         assert(tcp_req->fused_pair->fused_pair == tcp_req);
    3072                 :          0 :                                         nvmf_tcp_req_set_state(tcp_req->fused_pair, TCP_REQUEST_STATE_EXECUTING);
    3073                 :          0 :                                         spdk_nvmf_request_exec(&tcp_req->fused_pair->req);
    3074                 :          0 :                                         tcp_req->fused_pair->fused_pair = NULL;
    3075                 :          0 :                                         tcp_req->fused_pair = NULL;
    3076                 :            :                                 }
    3077                 :            :                         } else {
    3078                 :            :                                 /* For zero-copy, only requests with data coming from host to the
    3079                 :            :                                  * controller can end up here. */
    3080         [ -  + ]:     260910 :                                 assert(tcp_req->req.xfer == SPDK_NVME_DATA_HOST_TO_CONTROLLER);
    3081                 :     260910 :                                 nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_AWAITING_ZCOPY_COMMIT);
    3082                 :     260910 :                                 spdk_nvmf_request_zcopy_end(&tcp_req->req, true);
    3083                 :            :                         }
    3084                 :            : 
    3085                 :   15096660 :                         break;
    3086                 :   13801289 :                 case TCP_REQUEST_STATE_EXECUTING:
    3087   [ +  +  +  + ]:   13801289 :                         spdk_trace_record(TRACE_TCP_REQUEST_STATE_EXECUTING, tqpair->qpair.qid, 0, (uintptr_t)tcp_req,
    3088                 :            :                                           tqpair);
    3089                 :            :                         /* Some external code must kick a request into TCP_REQUEST_STATE_EXECUTED
    3090                 :            :                          * to escape this state. */
    3091                 :   13801289 :                         break;
    3092                 :     260910 :                 case TCP_REQUEST_STATE_AWAITING_ZCOPY_COMMIT:
    3093   [ +  -  +  - ]:     260910 :                         spdk_trace_record(TRACE_TCP_REQUEST_STATE_AWAIT_ZCOPY_COMMIT, tqpair->qpair.qid, 0,
    3094                 :            :                                           (uintptr_t)tcp_req, tqpair);
    3095                 :            :                         /* Some external code must kick a request into TCP_REQUEST_STATE_EXECUTED
    3096                 :            :                          * to escape this state. */
    3097                 :     260910 :                         break;
    3098                 :   15509447 :                 case TCP_REQUEST_STATE_EXECUTED:
    3099   [ +  +  +  + ]:   15509447 :                         spdk_trace_record(TRACE_TCP_REQUEST_STATE_EXECUTED, tqpair->qpair.qid, 0, (uintptr_t)tcp_req,
    3100                 :            :                                           tqpair);
    3101                 :            : 
    3102         [ +  + ]:   15509447 :                         if (spdk_unlikely(tcp_req->req.dif_enabled)) {
    3103                 :     633386 :                                 tcp_req->req.length = tcp_req->req.dif.orig_length;
    3104                 :            :                         }
    3105                 :            : 
    3106                 :   15509447 :                         nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_READY_TO_COMPLETE);
    3107                 :   15509447 :                         break;
    3108                 :   15512556 :                 case TCP_REQUEST_STATE_READY_TO_COMPLETE:
    3109   [ +  +  +  + ]:   15512556 :                         spdk_trace_record(TRACE_TCP_REQUEST_STATE_READY_TO_COMPLETE, tqpair->qpair.qid, 0,
    3110                 :            :                                           (uintptr_t)tcp_req, tqpair);
    3111         [ -  + ]:   15512556 :                         if (request_transfer_out(&tcp_req->req) != 0) {
    3112                 :          0 :                                 assert(0); /* No good way to handle this currently */
    3113                 :            :                         }
    3114                 :   15512556 :                         break;
    3115                 :   16547017 :                 case TCP_REQUEST_STATE_TRANSFERRING_CONTROLLER_TO_HOST:
    3116   [ +  +  +  + ]:   16547017 :                         spdk_trace_record(TRACE_TCP_REQUEST_STATE_TRANSFERRING_CONTROLLER_TO_HOST, tqpair->qpair.qid, 0,
    3117                 :            :                                           (uintptr_t)tcp_req, tqpair);
    3118                 :            :                         /* Some external code must kick a request into TCP_REQUEST_STATE_COMPLETED
    3119                 :            :                          * to escape this state. */
    3120                 :   16547017 :                         break;
    3121                 :     414802 :                 case TCP_REQUEST_STATE_AWAITING_ZCOPY_RELEASE:
    3122   [ +  -  +  - ]:     414802 :                         spdk_trace_record(TRACE_TCP_REQUEST_STATE_AWAIT_ZCOPY_RELEASE, tqpair->qpair.qid, 0,
    3123                 :            :                                           (uintptr_t)tcp_req, tqpair);
    3124                 :            :                         /* Some external code must kick a request into TCP_REQUEST_STATE_COMPLETED
    3125                 :            :                          * to escape this state. */
    3126                 :     414802 :                         break;
    3127                 :   16243583 :                 case TCP_REQUEST_STATE_COMPLETED:
    3128   [ +  +  +  + ]:   16243583 :                         spdk_trace_record(TRACE_TCP_REQUEST_STATE_COMPLETED, tqpair->qpair.qid, 0, (uintptr_t)tcp_req,
    3129                 :            :                                           tqpair);
    3130                 :            :                         /* If there's an outstanding PDU sent to the host, the request is completed
    3131                 :            :                          * due to the qpair being disconnected.  We must delay the completion until
    3132                 :            :                          * that write is done to avoid freeing the request twice. */
    3133   [ -  +  -  + ]:   16243583 :                         if (spdk_unlikely(tcp_req->pdu_in_use)) {
    3134   [ #  #  #  # ]:          0 :                                 SPDK_DEBUGLOG(nvmf_tcp, "Delaying completion due to outstanding "
    3135                 :            :                                               "write on req=%p\n", tcp_req);
    3136                 :            :                                 /* This can only happen for zcopy requests */
    3137         [ #  # ]:          0 :                                 assert(spdk_nvmf_request_using_zcopy(&tcp_req->req));
    3138         [ #  # ]:          0 :                                 assert(tqpair->qpair.state != SPDK_NVMF_QPAIR_ACTIVE);
    3139                 :          0 :                                 break;
    3140                 :            :                         }
    3141                 :            : 
    3142         [ +  + ]:   16243583 :                         if (tcp_req->req.data_from_pool) {
    3143                 :    9471585 :                                 spdk_nvmf_request_free_buffers(&tcp_req->req, group, transport);
    3144   [ -  +  +  +  :    6771998 :                         } else if (spdk_unlikely(tcp_req->has_in_capsule_data &&
          +  +  -  +  +  
             +  +  +  +  
                      + ]
    3145                 :            :                                                  (tcp_req->cmd.opc == SPDK_NVME_OPC_FABRIC ||
    3146                 :            :                                                   tqpair->qpair.qid == 0) && tcp_req->req.length > transport->opts.in_capsule_data_size)) {
    3147                 :       1935 :                                 tgroup = SPDK_CONTAINEROF(group, struct spdk_nvmf_tcp_poll_group, group);
    3148         [ -  + ]:       1935 :                                 assert(tgroup->control_msg_list);
    3149   [ -  +  -  + ]:       1935 :                                 SPDK_DEBUGLOG(nvmf_tcp, "Put buf to control msg list\n");
    3150                 :       1935 :                                 nvmf_tcp_control_msg_put(tgroup->control_msg_list,
    3151                 :            :                                                          tcp_req->req.iov[0].iov_base);
    3152         [ +  + ]:    6770063 :                         } else if (tcp_req->req.zcopy_bdev_io != NULL) {
    3153                 :            :                                 /* If the request has an unreleased zcopy bdev_io, it's either a
    3154                 :            :                                  * read, a failed write, or the qpair is being disconnected */
    3155         [ -  + ]:     414802 :                                 assert(spdk_nvmf_request_using_zcopy(&tcp_req->req));
    3156   [ -  +  -  -  :     414802 :                                 assert(tcp_req->req.xfer == SPDK_NVME_DATA_CONTROLLER_TO_HOST ||
             -  -  -  - ]
    3157                 :            :                                        spdk_nvme_cpl_is_error(&tcp_req->req.rsp->nvme_cpl) ||
    3158                 :            :                                        tqpair->qpair.state != SPDK_NVMF_QPAIR_ACTIVE);
    3159                 :     414802 :                                 nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_AWAITING_ZCOPY_RELEASE);
    3160                 :     414802 :                                 spdk_nvmf_request_zcopy_end(&tcp_req->req, false);
    3161                 :     414802 :                                 break;
    3162                 :            :                         }
    3163                 :   15828781 :                         tcp_req->req.length = 0;
    3164                 :   15828781 :                         tcp_req->req.iovcnt = 0;
    3165                 :   15828781 :                         tcp_req->fused_failed = false;
    3166         [ -  + ]:   15828781 :                         if (tcp_req->fused_pair) {
    3167                 :            :                                 /* This req was part of a valid fused pair, but failed before it got to
    3168                 :            :                                  * READ_TO_EXECUTE state.  This means we need to fail the other request
    3169                 :            :                                  * in the pair, because it is no longer part of a valid pair.  If the pair
    3170                 :            :                                  * already reached READY_TO_EXECUTE state, we need to kick it.
    3171                 :            :                                  */
    3172                 :          0 :                                 tcp_req->fused_pair->fused_failed = true;
    3173         [ #  # ]:          0 :                                 if (tcp_req->fused_pair->state == TCP_REQUEST_STATE_READY_TO_EXECUTE) {
    3174                 :          0 :                                         nvmf_tcp_req_process(ttransport, tcp_req->fused_pair);
    3175                 :            :                                 }
    3176                 :          0 :                                 tcp_req->fused_pair = NULL;
    3177                 :            :                         }
    3178                 :            : 
    3179                 :   15828781 :                         nvmf_tcp_req_put(tqpair, tcp_req);
    3180                 :   15828781 :                         break;
    3181                 :          0 :                 case TCP_REQUEST_NUM_STATES:
    3182                 :            :                 default:
    3183                 :          0 :                         assert(0);
    3184                 :            :                         break;
    3185                 :            :                 }
    3186                 :            : 
    3187         [ +  + ]:  146772242 :                 if (tcp_req->state != prev_state) {
    3188                 :   93002347 :                         progress = true;
    3189                 :            :                 }
    3190         [ +  + ]:  146772242 :         } while (tcp_req->state != prev_state);
    3191                 :            : 
    3192                 :   53769895 :         return progress;
    3193                 :            : }
    3194                 :            : 
    3195                 :            : static void
    3196                 :   84681753 : nvmf_tcp_sock_cb(void *arg, struct spdk_sock_group *group, struct spdk_sock *sock)
    3197                 :            : {
    3198                 :   84681753 :         struct spdk_nvmf_tcp_qpair *tqpair = arg;
    3199                 :            :         int rc;
    3200                 :            : 
    3201         [ -  + ]:   84681753 :         assert(tqpair != NULL);
    3202                 :   84681753 :         rc = nvmf_tcp_sock_process(tqpair);
    3203                 :            : 
    3204                 :            :         /* If there was a new socket error, disconnect */
    3205         [ +  + ]:   84681753 :         if (rc < 0) {
    3206                 :     401060 :                 nvmf_tcp_qpair_disconnect(tqpair);
    3207                 :            :         }
    3208                 :   84681753 : }
    3209                 :            : 
    3210                 :            : static int
    3211                 :       8192 : nvmf_tcp_poll_group_add(struct spdk_nvmf_transport_poll_group *group,
    3212                 :            :                         struct spdk_nvmf_qpair *qpair)
    3213                 :            : {
    3214                 :            :         struct spdk_nvmf_tcp_poll_group *tgroup;
    3215                 :            :         struct spdk_nvmf_tcp_qpair      *tqpair;
    3216                 :            :         int                             rc;
    3217                 :            : 
    3218                 :       8192 :         tgroup = SPDK_CONTAINEROF(group, struct spdk_nvmf_tcp_poll_group, group);
    3219                 :       8192 :         tqpair = SPDK_CONTAINEROF(qpair, struct spdk_nvmf_tcp_qpair, qpair);
    3220                 :            : 
    3221                 :       8192 :         rc =  nvmf_tcp_qpair_sock_init(tqpair);
    3222         [ -  + ]:       8192 :         if (rc != 0) {
    3223                 :          0 :                 SPDK_ERRLOG("Cannot set sock opt for tqpair=%p\n", tqpair);
    3224                 :          0 :                 return -1;
    3225                 :            :         }
    3226                 :            : 
    3227                 :       8192 :         rc = nvmf_tcp_qpair_init(&tqpair->qpair);
    3228         [ -  + ]:       8192 :         if (rc < 0) {
    3229                 :          0 :                 SPDK_ERRLOG("Cannot init tqpair=%p\n", tqpair);
    3230                 :          0 :                 return -1;
    3231                 :            :         }
    3232                 :            : 
    3233                 :       8192 :         rc = nvmf_tcp_qpair_init_mem_resource(tqpair);
    3234         [ -  + ]:       8192 :         if (rc < 0) {
    3235                 :          0 :                 SPDK_ERRLOG("Cannot init memory resource info for tqpair=%p\n", tqpair);
    3236                 :          0 :                 return -1;
    3237                 :            :         }
    3238                 :            : 
    3239                 :       8192 :         rc = spdk_sock_group_add_sock(tgroup->sock_group, tqpair->sock,
    3240                 :            :                                       nvmf_tcp_sock_cb, tqpair);
    3241         [ -  + ]:       8192 :         if (rc != 0) {
    3242                 :          0 :                 SPDK_ERRLOG("Could not add sock to sock_group: %s (%d)\n",
    3243                 :            :                             spdk_strerror(errno), errno);
    3244                 :          0 :                 return -1;
    3245                 :            :         }
    3246                 :            : 
    3247                 :       8192 :         tqpair->group = tgroup;
    3248                 :       8192 :         nvmf_tcp_qpair_set_state(tqpair, NVME_TCP_QPAIR_STATE_INVALID);
    3249                 :       8192 :         TAILQ_INSERT_TAIL(&tgroup->qpairs, tqpair, link);
    3250                 :            : 
    3251                 :       8192 :         return 0;
    3252                 :            : }
    3253                 :            : 
    3254                 :            : static int
    3255                 :       8192 : nvmf_tcp_poll_group_remove(struct spdk_nvmf_transport_poll_group *group,
    3256                 :            :                            struct spdk_nvmf_qpair *qpair)
    3257                 :            : {
    3258                 :            :         struct spdk_nvmf_tcp_poll_group *tgroup;
    3259                 :            :         struct spdk_nvmf_tcp_qpair              *tqpair;
    3260                 :            :         int                             rc;
    3261                 :            : 
    3262                 :       8192 :         tgroup = SPDK_CONTAINEROF(group, struct spdk_nvmf_tcp_poll_group, group);
    3263                 :       8192 :         tqpair = SPDK_CONTAINEROF(qpair, struct spdk_nvmf_tcp_qpair, qpair);
    3264                 :            : 
    3265         [ -  + ]:       8192 :         assert(tqpair->group == tgroup);
    3266                 :            : 
    3267   [ -  +  -  + ]:       8192 :         SPDK_DEBUGLOG(nvmf_tcp, "remove tqpair=%p from the tgroup=%p\n", tqpair, tgroup);
    3268         [ +  + ]:       8192 :         if (tqpair->recv_state == NVME_TCP_PDU_RECV_STATE_AWAIT_REQ) {
    3269                 :            :                 /* Change the state to move the qpair from the await_req list to the main list
    3270                 :            :                  * and prevent adding it again later by nvmf_tcp_qpair_set_recv_state() */
    3271                 :         11 :                 nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_QUIESCING);
    3272                 :            :         }
    3273         [ +  + ]:       8192 :         TAILQ_REMOVE(&tgroup->qpairs, tqpair, link);
    3274                 :            : 
    3275                 :       8192 :         rc = spdk_sock_group_remove_sock(tgroup->sock_group, tqpair->sock);
    3276         [ -  + ]:       8192 :         if (rc != 0) {
    3277                 :          0 :                 SPDK_ERRLOG("Could not remove sock from sock_group: %s (%d)\n",
    3278                 :            :                             spdk_strerror(errno), errno);
    3279                 :            :         }
    3280                 :            : 
    3281                 :       8192 :         return rc;
    3282                 :            : }
    3283                 :            : 
    3284                 :            : static int
    3285                 :   16187330 : nvmf_tcp_req_complete(struct spdk_nvmf_request *req)
    3286                 :            : {
    3287                 :            :         struct spdk_nvmf_tcp_transport *ttransport;
    3288                 :            :         struct spdk_nvmf_tcp_req *tcp_req;
    3289                 :            : 
    3290                 :   16187330 :         ttransport = SPDK_CONTAINEROF(req->qpair->transport, struct spdk_nvmf_tcp_transport, transport);
    3291                 :   16187330 :         tcp_req = SPDK_CONTAINEROF(req, struct spdk_nvmf_tcp_req, req);
    3292                 :            : 
    3293   [ +  +  +  - ]:   16187330 :         switch (tcp_req->state) {
    3294                 :   15095496 :         case TCP_REQUEST_STATE_EXECUTING:
    3295                 :            :         case TCP_REQUEST_STATE_AWAITING_ZCOPY_COMMIT:
    3296                 :   15095496 :                 nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_EXECUTED);
    3297                 :   15095496 :                 break;
    3298                 :     677032 :         case TCP_REQUEST_STATE_AWAITING_ZCOPY_START:
    3299                 :     677032 :                 nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_ZCOPY_START_COMPLETED);
    3300                 :     677032 :                 break;
    3301                 :     414802 :         case TCP_REQUEST_STATE_AWAITING_ZCOPY_RELEASE:
    3302                 :     414802 :                 nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_COMPLETED);
    3303                 :     414802 :                 break;
    3304                 :          0 :         default:
    3305                 :          0 :                 assert(0 && "Unexpected request state");
    3306                 :            :                 break;
    3307                 :            :         }
    3308                 :            : 
    3309                 :   16187330 :         nvmf_tcp_req_process(ttransport, tcp_req);
    3310                 :            : 
    3311                 :   16187330 :         return 0;
    3312                 :            : }
    3313                 :            : 
    3314                 :            : static void
    3315                 :       8192 : nvmf_tcp_close_qpair(struct spdk_nvmf_qpair *qpair,
    3316                 :            :                      spdk_nvmf_transport_qpair_fini_cb cb_fn, void *cb_arg)
    3317                 :            : {
    3318                 :            :         struct spdk_nvmf_tcp_qpair *tqpair;
    3319                 :            : 
    3320   [ -  +  -  + ]:       8192 :         SPDK_DEBUGLOG(nvmf_tcp, "Qpair: %p\n", qpair);
    3321                 :            : 
    3322                 :       8192 :         tqpair = SPDK_CONTAINEROF(qpair, struct spdk_nvmf_tcp_qpair, qpair);
    3323                 :            : 
    3324         [ -  + ]:       8192 :         assert(tqpair->fini_cb_fn == NULL);
    3325                 :       8192 :         tqpair->fini_cb_fn = cb_fn;
    3326                 :       8192 :         tqpair->fini_cb_arg = cb_arg;
    3327                 :            : 
    3328                 :       8192 :         nvmf_tcp_qpair_set_state(tqpair, NVME_TCP_QPAIR_STATE_EXITED);
    3329                 :       8192 :         nvmf_tcp_qpair_destroy(tqpair);
    3330                 :       8192 : }
    3331                 :            : 
    3332                 :            : static int
    3333                 :  860005377 : nvmf_tcp_poll_group_poll(struct spdk_nvmf_transport_poll_group *group)
    3334                 :            : {
    3335                 :            :         struct spdk_nvmf_tcp_poll_group *tgroup;
    3336                 :            :         int rc;
    3337                 :            :         struct spdk_nvmf_request *req, *req_tmp;
    3338                 :            :         struct spdk_nvmf_tcp_req *tcp_req;
    3339                 :            :         struct spdk_nvmf_tcp_qpair *tqpair, *tqpair_tmp;
    3340                 :  860005377 :         struct spdk_nvmf_tcp_transport *ttransport = SPDK_CONTAINEROF(group->transport,
    3341                 :            :                         struct spdk_nvmf_tcp_transport, transport);
    3342                 :            : 
    3343                 :  860005377 :         tgroup = SPDK_CONTAINEROF(group, struct spdk_nvmf_tcp_poll_group, group);
    3344                 :            : 
    3345   [ +  +  +  + ]:  860005377 :         if (spdk_unlikely(TAILQ_EMPTY(&tgroup->qpairs) && TAILQ_EMPTY(&tgroup->await_req))) {
    3346                 :  374186239 :                 return 0;
    3347                 :            :         }
    3348                 :            : 
    3349         [ +  + ]:  486579343 :         STAILQ_FOREACH_SAFE(req, &group->pending_buf_queue, buf_link, req_tmp) {
    3350                 :     935162 :                 tcp_req = SPDK_CONTAINEROF(req, struct spdk_nvmf_tcp_req, req);
    3351         [ +  + ]:     935162 :                 if (nvmf_tcp_req_process(ttransport, tcp_req) == false) {
    3352                 :     174957 :                         break;
    3353                 :            :                 }
    3354                 :            :         }
    3355                 :            : 
    3356                 :  485819138 :         rc = spdk_sock_group_poll(tgroup->sock_group);
    3357         [ -  + ]:  485819138 :         if (rc < 0) {
    3358                 :          0 :                 SPDK_ERRLOG("Failed to poll sock_group=%p\n", tgroup->sock_group);
    3359                 :            :         }
    3360                 :            : 
    3361         [ +  + ]:  485991052 :         TAILQ_FOREACH_SAFE(tqpair, &tgroup->await_req, link, tqpair_tmp) {
    3362                 :     171914 :                 rc = nvmf_tcp_sock_process(tqpair);
    3363                 :            : 
    3364                 :            :                 /* If there was a new socket error, disconnect */
    3365         [ -  + ]:     171914 :                 if (rc < 0) {
    3366                 :          0 :                         nvmf_tcp_qpair_disconnect(tqpair);
    3367                 :            :                 }
    3368                 :            :         }
    3369                 :            : 
    3370                 :  485819138 :         return rc;
    3371                 :            : }
    3372                 :            : 
    3373                 :            : static int
    3374                 :       9483 : nvmf_tcp_qpair_get_trid(struct spdk_nvmf_qpair *qpair,
    3375                 :            :                         struct spdk_nvme_transport_id *trid, bool peer)
    3376                 :            : {
    3377                 :            :         struct spdk_nvmf_tcp_qpair     *tqpair;
    3378                 :            :         uint16_t                        port;
    3379                 :            : 
    3380                 :       9483 :         tqpair = SPDK_CONTAINEROF(qpair, struct spdk_nvmf_tcp_qpair, qpair);
    3381                 :       9483 :         spdk_nvme_trid_populate_transport(trid, SPDK_NVME_TRANSPORT_TCP);
    3382                 :            : 
    3383         [ -  + ]:       9483 :         if (peer) {
    3384         [ #  # ]:          0 :                 snprintf(trid->traddr, sizeof(trid->traddr), "%s", tqpair->initiator_addr);
    3385                 :          0 :                 port = tqpair->initiator_port;
    3386                 :            :         } else {
    3387         [ -  + ]:       9483 :                 snprintf(trid->traddr, sizeof(trid->traddr), "%s", tqpair->target_addr);
    3388                 :       9483 :                 port = tqpair->target_port;
    3389                 :            :         }
    3390                 :            : 
    3391         [ +  - ]:       9483 :         if (spdk_sock_is_ipv4(tqpair->sock)) {
    3392                 :       9483 :                 trid->adrfam = SPDK_NVMF_ADRFAM_IPV4;
    3393         [ #  # ]:          0 :         } else if (spdk_sock_is_ipv6(tqpair->sock)) {
    3394                 :          0 :                 trid->adrfam = SPDK_NVMF_ADRFAM_IPV6;
    3395                 :            :         } else {
    3396                 :          0 :                 return -1;
    3397                 :            :         }
    3398                 :            : 
    3399         [ -  + ]:       9483 :         snprintf(trid->trsvcid, sizeof(trid->trsvcid), "%d", port);
    3400                 :       9483 :         return 0;
    3401                 :            : }
    3402                 :            : 
    3403                 :            : static int
    3404                 :          0 : nvmf_tcp_qpair_get_local_trid(struct spdk_nvmf_qpair *qpair,
    3405                 :            :                               struct spdk_nvme_transport_id *trid)
    3406                 :            : {
    3407                 :          0 :         return nvmf_tcp_qpair_get_trid(qpair, trid, 0);
    3408                 :            : }
    3409                 :            : 
    3410                 :            : static int
    3411                 :          0 : nvmf_tcp_qpair_get_peer_trid(struct spdk_nvmf_qpair *qpair,
    3412                 :            :                              struct spdk_nvme_transport_id *trid)
    3413                 :            : {
    3414                 :          0 :         return nvmf_tcp_qpair_get_trid(qpair, trid, 1);
    3415                 :            : }
    3416                 :            : 
    3417                 :            : static int
    3418                 :       9483 : nvmf_tcp_qpair_get_listen_trid(struct spdk_nvmf_qpair *qpair,
    3419                 :            :                                struct spdk_nvme_transport_id *trid)
    3420                 :            : {
    3421                 :       9483 :         return nvmf_tcp_qpair_get_trid(qpair, trid, 0);
    3422                 :            : }
    3423                 :            : 
    3424                 :            : static void
    3425                 :          0 : nvmf_tcp_req_set_abort_status(struct spdk_nvmf_request *req,
    3426                 :            :                               struct spdk_nvmf_tcp_req *tcp_req_to_abort)
    3427                 :            : {
    3428                 :          0 :         nvmf_tcp_req_set_cpl(tcp_req_to_abort, SPDK_NVME_SCT_GENERIC, SPDK_NVME_SC_ABORTED_BY_REQUEST);
    3429                 :          0 :         nvmf_tcp_req_set_state(tcp_req_to_abort, TCP_REQUEST_STATE_READY_TO_COMPLETE);
    3430                 :            : 
    3431                 :          0 :         req->rsp->nvme_cpl.cdw0 &= ~1U; /* Command was successfully aborted. */
    3432                 :          0 : }
    3433                 :            : 
    3434                 :            : static int
    3435                 :      25673 : _nvmf_tcp_qpair_abort_request(void *ctx)
    3436                 :            : {
    3437                 :      25673 :         struct spdk_nvmf_request *req = ctx;
    3438                 :      25673 :         struct spdk_nvmf_tcp_req *tcp_req_to_abort = SPDK_CONTAINEROF(req->req_to_abort,
    3439                 :            :                         struct spdk_nvmf_tcp_req, req);
    3440                 :      25673 :         struct spdk_nvmf_tcp_qpair *tqpair = SPDK_CONTAINEROF(req->req_to_abort->qpair,
    3441                 :            :                                              struct spdk_nvmf_tcp_qpair, qpair);
    3442                 :      25673 :         struct spdk_nvmf_tcp_transport *ttransport = SPDK_CONTAINEROF(tqpair->qpair.transport,
    3443                 :            :                         struct spdk_nvmf_tcp_transport, transport);
    3444                 :            :         int rc;
    3445                 :            : 
    3446                 :      25673 :         spdk_poller_unregister(&req->poller);
    3447                 :            : 
    3448   [ +  -  +  + ]:      25673 :         switch (tcp_req_to_abort->state) {
    3449                 :      20727 :         case TCP_REQUEST_STATE_EXECUTING:
    3450                 :            :         case TCP_REQUEST_STATE_AWAITING_ZCOPY_START:
    3451                 :            :         case TCP_REQUEST_STATE_AWAITING_ZCOPY_COMMIT:
    3452                 :      20727 :                 rc = nvmf_ctrlr_abort_request(req);
    3453         [ +  - ]:      20727 :                 if (rc == SPDK_NVMF_REQUEST_EXEC_STATUS_ASYNCHRONOUS) {
    3454                 :      20727 :                         return SPDK_POLLER_BUSY;
    3455                 :            :                 }
    3456                 :          0 :                 break;
    3457                 :            : 
    3458                 :          0 :         case TCP_REQUEST_STATE_NEED_BUFFER:
    3459   [ #  #  #  #  :          0 :                 STAILQ_REMOVE(&tqpair->group->group.pending_buf_queue,
             #  #  #  # ]
    3460                 :            :                               &tcp_req_to_abort->req, spdk_nvmf_request, buf_link);
    3461                 :            : 
    3462                 :          0 :                 nvmf_tcp_req_set_abort_status(req, tcp_req_to_abort);
    3463                 :          0 :                 nvmf_tcp_req_process(ttransport, tcp_req_to_abort);
    3464                 :          0 :                 break;
    3465                 :            : 
    3466                 :       2094 :         case TCP_REQUEST_STATE_AWAITING_R2T_ACK:
    3467                 :            :         case TCP_REQUEST_STATE_TRANSFERRING_HOST_TO_CONTROLLER:
    3468         [ +  - ]:       2094 :                 if (spdk_get_ticks() < req->timeout_tsc) {
    3469                 :       2094 :                         req->poller = SPDK_POLLER_REGISTER(_nvmf_tcp_qpair_abort_request, req, 0);
    3470                 :       2094 :                         return SPDK_POLLER_BUSY;
    3471                 :            :                 }
    3472                 :          0 :                 break;
    3473                 :            : 
    3474                 :       2852 :         default:
    3475                 :            :                 /* Requests in other states are either un-abortable (e.g.
    3476                 :            :                  * TRANSFERRING_CONTROLLER_TO_HOST) or should never end up here, as they're
    3477                 :            :                  * immediately transitioned to other states in nvmf_tcp_req_process() (e.g.
    3478                 :            :                  * READY_TO_EXECUTE).  But it is fine to end up here, as we'll simply complete the
    3479                 :            :                  * abort request with the bit0 of dword0 set (command not aborted).
    3480                 :            :                  */
    3481                 :       2852 :                 break;
    3482                 :            :         }
    3483                 :            : 
    3484                 :       2852 :         spdk_nvmf_request_complete(req);
    3485                 :       2852 :         return SPDK_POLLER_BUSY;
    3486                 :            : }
    3487                 :            : 
    3488                 :            : static void
    3489                 :      28963 : nvmf_tcp_qpair_abort_request(struct spdk_nvmf_qpair *qpair,
    3490                 :            :                              struct spdk_nvmf_request *req)
    3491                 :            : {
    3492                 :            :         struct spdk_nvmf_tcp_qpair *tqpair;
    3493                 :            :         struct spdk_nvmf_tcp_transport *ttransport;
    3494                 :            :         struct spdk_nvmf_transport *transport;
    3495                 :            :         uint16_t cid;
    3496                 :            :         uint32_t i;
    3497                 :      28963 :         struct spdk_nvmf_tcp_req *tcp_req_to_abort = NULL;
    3498                 :            : 
    3499                 :      28963 :         tqpair = SPDK_CONTAINEROF(qpair, struct spdk_nvmf_tcp_qpair, qpair);
    3500                 :      28963 :         ttransport = SPDK_CONTAINEROF(qpair->transport, struct spdk_nvmf_tcp_transport, transport);
    3501                 :      28963 :         transport = &ttransport->transport;
    3502                 :            : 
    3503                 :      28963 :         cid = req->cmd->nvme_cmd.cdw10_bits.abort.cid;
    3504                 :            : 
    3505         [ +  + ]:    1896289 :         for (i = 0; i < tqpair->resource_count; i++) {
    3506         [ +  + ]:    1890905 :                 if (tqpair->reqs[i].state != TCP_REQUEST_STATE_FREE &&
    3507         [ +  + ]:     717365 :                     tqpair->reqs[i].req.cmd->nvme_cmd.cid == cid) {
    3508                 :      23579 :                         tcp_req_to_abort = &tqpair->reqs[i];
    3509                 :      23579 :                         break;
    3510                 :            :                 }
    3511                 :            :         }
    3512                 :            : 
    3513   [ +  -  +  - ]:      28963 :         spdk_trace_record(TRACE_TCP_QP_ABORT_REQ, qpair->qid, 0, (uintptr_t)req, tqpair);
    3514                 :            : 
    3515         [ +  + ]:      28963 :         if (tcp_req_to_abort == NULL) {
    3516                 :       5384 :                 spdk_nvmf_request_complete(req);
    3517                 :       5384 :                 return;
    3518                 :            :         }
    3519                 :            : 
    3520                 :      23579 :         req->req_to_abort = &tcp_req_to_abort->req;
    3521                 :      47158 :         req->timeout_tsc = spdk_get_ticks() +
    3522                 :      23579 :                            transport->opts.abort_timeout_sec * spdk_get_ticks_hz();
    3523                 :      23579 :         req->poller = NULL;
    3524                 :            : 
    3525                 :      23579 :         _nvmf_tcp_qpair_abort_request(req);
    3526                 :            : }
    3527                 :            : 
    3528                 :            : struct tcp_subsystem_add_host_opts {
    3529                 :            :         char *psk;
    3530                 :            : };
    3531                 :            : 
    3532                 :            : static const struct spdk_json_object_decoder tcp_subsystem_add_host_opts_decoder[] = {
    3533                 :            :         {"psk", offsetof(struct tcp_subsystem_add_host_opts, psk), spdk_json_decode_string, true},
    3534                 :            : };
    3535                 :            : 
    3536                 :            : static int
    3537                 :         24 : tcp_load_psk(const char *fname, char *buf, size_t bufsz)
    3538                 :            : {
    3539                 :            :         FILE *psk_file;
    3540                 :          4 :         struct stat statbuf;
    3541                 :            :         int rc;
    3542                 :            : 
    3543   [ -  +  -  +  :         24 :         if (stat(fname, &statbuf) != 0) {
                   -  + ]
    3544                 :          0 :                 SPDK_ERRLOG("Could not read permissions for PSK file\n");
    3545                 :          0 :                 return -EACCES;
    3546                 :            :         }
    3547                 :            : 
    3548         [ +  + ]:         24 :         if ((statbuf.st_mode & TCP_PSK_INVALID_PERMISSIONS) != 0) {
    3549                 :          3 :                 SPDK_ERRLOG("Incorrect permissions for PSK file\n");
    3550                 :          3 :                 return -EPERM;
    3551                 :            :         }
    3552         [ -  + ]:         21 :         if ((size_t)statbuf.st_size >= bufsz) {
    3553                 :          0 :                 SPDK_ERRLOG("Invalid PSK: too long\n");
    3554                 :          0 :                 return -EINVAL;
    3555                 :            :         }
    3556                 :         21 :         psk_file = fopen(fname, "r");
    3557         [ -  + ]:         21 :         if (psk_file == NULL) {
    3558                 :          0 :                 SPDK_ERRLOG("Could not open PSK file\n");
    3559                 :          0 :                 return -EINVAL;
    3560                 :            :         }
    3561                 :            : 
    3562         [ -  + ]:         21 :         memset(buf, 0, bufsz);
    3563                 :         21 :         rc = fread(buf, 1, statbuf.st_size, psk_file);
    3564         [ -  + ]:         21 :         if (rc != statbuf.st_size) {
    3565                 :          0 :                 SPDK_ERRLOG("Failed to read PSK\n");
    3566                 :          0 :                 fclose(psk_file);
    3567                 :          0 :                 return -EINVAL;
    3568                 :            :         }
    3569                 :            : 
    3570                 :         21 :         fclose(psk_file);
    3571                 :         21 :         return 0;
    3572                 :            : }
    3573                 :            : 
    3574                 :            : static int
    3575                 :         48 : nvmf_tcp_subsystem_add_host(struct spdk_nvmf_transport *transport,
    3576                 :            :                             const struct spdk_nvmf_subsystem *subsystem,
    3577                 :            :                             const char *hostnqn,
    3578                 :            :                             const struct spdk_json_val *transport_specific)
    3579                 :            : {
    3580                 :          6 :         struct tcp_subsystem_add_host_opts opts;
    3581                 :            :         struct spdk_nvmf_tcp_transport *ttransport;
    3582                 :            :         struct tcp_psk_entry *entry;
    3583                 :          6 :         char psk_identity[NVMF_PSK_IDENTITY_LEN];
    3584                 :         48 :         uint8_t psk_configured[SPDK_TLS_PSK_MAX_LEN] = {};
    3585                 :         48 :         char psk_interchange[SPDK_TLS_PSK_MAX_LEN] = {};
    3586                 :            :         uint8_t tls_cipher_suite;
    3587                 :         48 :         int rc = 0;
    3588                 :          6 :         uint8_t psk_retained_hash;
    3589                 :          6 :         uint64_t psk_configured_size;
    3590                 :            : 
    3591         [ -  + ]:         48 :         if (transport_specific == NULL) {
    3592                 :          0 :                 return 0;
    3593                 :            :         }
    3594                 :            : 
    3595         [ -  + ]:         48 :         assert(transport != NULL);
    3596         [ -  + ]:         48 :         assert(subsystem != NULL);
    3597                 :            : 
    3598                 :         48 :         memset(&opts, 0, sizeof(opts));
    3599                 :            : 
    3600                 :            :         /* Decode PSK file path */
    3601         [ -  + ]:         48 :         if (spdk_json_decode_object_relaxed(transport_specific, tcp_subsystem_add_host_opts_decoder,
    3602                 :            :                                             SPDK_COUNTOF(tcp_subsystem_add_host_opts_decoder), &opts)) {
    3603                 :          0 :                 SPDK_ERRLOG("spdk_json_decode_object failed\n");
    3604                 :          0 :                 return -EINVAL;
    3605                 :            :         }
    3606                 :            : 
    3607         [ +  + ]:         48 :         if (opts.psk == NULL) {
    3608                 :         24 :                 return 0;
    3609                 :            :         }
    3610   [ -  +  -  + ]:         24 :         if (strlen(opts.psk) >= sizeof(entry->psk)) {
    3611                 :          0 :                 SPDK_ERRLOG("PSK path too long\n");
    3612                 :          0 :                 rc = -EINVAL;
    3613                 :          0 :                 goto end;
    3614                 :            :         }
    3615                 :            : 
    3616                 :         24 :         rc = tcp_load_psk(opts.psk, psk_interchange, sizeof(psk_interchange));
    3617         [ +  + ]:         24 :         if (rc) {
    3618                 :          3 :                 SPDK_ERRLOG("Could not retrieve PSK from file\n");
    3619                 :          3 :                 goto end;
    3620                 :            :         }
    3621                 :            : 
    3622                 :            :         /* Parse PSK interchange to get length of base64 encoded data.
    3623                 :            :          * This is then used to decide which cipher suite should be used
    3624                 :            :          * to generate PSK identity and TLS PSK later on. */
    3625                 :         21 :         rc = nvme_tcp_parse_interchange_psk(psk_interchange, psk_configured, sizeof(psk_configured),
    3626                 :            :                                             &psk_configured_size, &psk_retained_hash);
    3627         [ -  + ]:         21 :         if (rc < 0) {
    3628                 :          0 :                 SPDK_ERRLOG("Failed to parse PSK interchange!\n");
    3629                 :          0 :                 goto end;
    3630                 :            :         }
    3631                 :            : 
    3632                 :            :         /* The Base64 string encodes the configured PSK (32 or 48 bytes binary).
    3633                 :            :          * This check also ensures that psk_configured_size is smaller than
    3634                 :            :          * psk_retained buffer size. */
    3635         [ +  + ]:         21 :         if (psk_configured_size == SHA256_DIGEST_LENGTH) {
    3636                 :         12 :                 tls_cipher_suite = NVME_TCP_CIPHER_AES_128_GCM_SHA256;
    3637         [ +  - ]:          9 :         } else if (psk_configured_size == SHA384_DIGEST_LENGTH) {
    3638                 :          9 :                 tls_cipher_suite = NVME_TCP_CIPHER_AES_256_GCM_SHA384;
    3639                 :            :         } else {
    3640                 :          0 :                 SPDK_ERRLOG("Unrecognized cipher suite!\n");
    3641                 :          0 :                 rc = -EINVAL;
    3642                 :          0 :                 goto end;
    3643                 :            :         }
    3644                 :            : 
    3645                 :         21 :         ttransport = SPDK_CONTAINEROF(transport, struct spdk_nvmf_tcp_transport, transport);
    3646                 :            :         /* Generate PSK identity. */
    3647                 :         41 :         rc = nvme_tcp_generate_psk_identity(psk_identity, NVMF_PSK_IDENTITY_LEN, hostnqn,
    3648                 :         21 :                                             subsystem->subnqn, tls_cipher_suite);
    3649         [ -  + ]:         21 :         if (rc) {
    3650                 :          0 :                 rc = -EINVAL;
    3651                 :          0 :                 goto end;
    3652                 :            :         }
    3653                 :            :         /* Check if PSK identity entry already exists. */
    3654         [ -  + ]:         21 :         TAILQ_FOREACH(entry, &ttransport->psks, link) {
    3655   [ #  #  #  # ]:          0 :                 if (strncmp(entry->psk_identity, psk_identity, NVMF_PSK_IDENTITY_LEN) == 0) {
    3656                 :          0 :                         SPDK_ERRLOG("Given PSK identity: %s entry already exists!\n", psk_identity);
    3657                 :          0 :                         rc = -EEXIST;
    3658                 :          0 :                         goto end;
    3659                 :            :                 }
    3660                 :            :         }
    3661                 :         21 :         entry = calloc(1, sizeof(struct tcp_psk_entry));
    3662         [ -  + ]:         21 :         if (entry == NULL) {
    3663                 :          0 :                 SPDK_ERRLOG("Unable to allocate memory for PSK entry!\n");
    3664                 :          0 :                 rc = -ENOMEM;
    3665                 :          0 :                 goto end;
    3666                 :            :         }
    3667                 :            : 
    3668         [ -  + ]:         21 :         if (snprintf(entry->hostnqn, sizeof(entry->hostnqn), "%s", hostnqn) < 0) {
    3669                 :          0 :                 SPDK_ERRLOG("Could not write hostnqn string!\n");
    3670                 :          0 :                 rc = -EINVAL;
    3671                 :          0 :                 free(entry);
    3672                 :          0 :                 goto end;
    3673                 :            :         }
    3674         [ -  + ]:         21 :         if (snprintf(entry->subnqn, sizeof(entry->subnqn), "%s", subsystem->subnqn) < 0) {
    3675                 :          0 :                 SPDK_ERRLOG("Could not write subnqn string!\n");
    3676                 :          0 :                 rc = -EINVAL;
    3677                 :          0 :                 free(entry);
    3678                 :          0 :                 goto end;
    3679                 :            :         }
    3680         [ -  + ]:         21 :         if (snprintf(entry->psk_identity, sizeof(entry->psk_identity), "%s", psk_identity) < 0) {
    3681                 :          0 :                 SPDK_ERRLOG("Could not write PSK identity string!\n");
    3682                 :          0 :                 rc = -EINVAL;
    3683                 :          0 :                 free(entry);
    3684                 :          0 :                 goto end;
    3685                 :            :         }
    3686                 :         21 :         entry->tls_cipher_suite = tls_cipher_suite;
    3687                 :            : 
    3688                 :            :         /* No hash indicates that Configured PSK must be used as Retained PSK. */
    3689         [ -  + ]:         21 :         if (psk_retained_hash == NVME_TCP_HASH_ALGORITHM_NONE) {
    3690                 :            :                 /* Psk configured is either 32 or 48 bytes long. */
    3691         [ #  # ]:          0 :                 memcpy(entry->psk, psk_configured, psk_configured_size);
    3692                 :          0 :                 entry->psk_size = psk_configured_size;
    3693                 :            :         } else {
    3694                 :            :                 /* Derive retained PSK. */
    3695                 :         21 :                 rc = nvme_tcp_derive_retained_psk(psk_configured, psk_configured_size, hostnqn, entry->psk,
    3696                 :            :                                                   SPDK_TLS_PSK_MAX_LEN, psk_retained_hash);
    3697         [ -  + ]:         21 :                 if (rc < 0) {
    3698                 :          0 :                         SPDK_ERRLOG("Unable to derive retained PSK!\n");
    3699                 :          0 :                         free(entry);
    3700                 :          0 :                         goto end;
    3701                 :            :                 }
    3702                 :         21 :                 entry->psk_size = rc;
    3703                 :            :         }
    3704                 :            : 
    3705                 :         21 :         rc = snprintf(entry->psk_path, sizeof(entry->psk_path), "%s", opts.psk);
    3706   [ +  -  -  + ]:         21 :         if (rc < 0 || (size_t)rc >= sizeof(entry->psk_path)) {
    3707                 :          0 :                 SPDK_ERRLOG("Could not save PSK path!\n");
    3708                 :          0 :                 rc = -ENAMETOOLONG;
    3709                 :          0 :                 free(entry);
    3710                 :          0 :                 goto end;
    3711                 :            :         }
    3712                 :            : 
    3713                 :         21 :         TAILQ_INSERT_TAIL(&ttransport->psks, entry, link);
    3714                 :         21 :         rc = 0;
    3715                 :            : 
    3716                 :         24 : end:
    3717                 :         24 :         spdk_memset_s(psk_configured, sizeof(psk_configured), 0, sizeof(psk_configured));
    3718                 :         24 :         spdk_memset_s(psk_interchange, sizeof(psk_interchange), 0, sizeof(psk_interchange));
    3719                 :            : 
    3720                 :         24 :         free(opts.psk);
    3721                 :            : 
    3722                 :         24 :         return rc;
    3723                 :            : }
    3724                 :            : 
    3725                 :            : static void
    3726                 :         48 : nvmf_tcp_subsystem_remove_host(struct spdk_nvmf_transport *transport,
    3727                 :            :                                const struct spdk_nvmf_subsystem *subsystem,
    3728                 :            :                                const char *hostnqn)
    3729                 :            : {
    3730                 :            :         struct spdk_nvmf_tcp_transport *ttransport;
    3731                 :            :         struct tcp_psk_entry *entry, *tmp;
    3732                 :            : 
    3733         [ -  + ]:         48 :         assert(transport != NULL);
    3734         [ -  + ]:         48 :         assert(subsystem != NULL);
    3735                 :            : 
    3736                 :         48 :         ttransport = SPDK_CONTAINEROF(transport, struct spdk_nvmf_tcp_transport, transport);
    3737         [ +  + ]:         48 :         TAILQ_FOREACH_SAFE(entry, &ttransport->psks, link, tmp) {
    3738   [ +  +  -  +  :         21 :                 if ((strncmp(entry->hostnqn, hostnqn, SPDK_NVMF_NQN_MAX_LEN)) == 0 &&
                   +  - ]
    3739   [ +  +  -  +  :         21 :                     (strncmp(entry->subnqn, subsystem->subnqn, SPDK_NVMF_NQN_MAX_LEN)) == 0) {
                   +  - ]
    3740         [ -  + ]:         21 :                         TAILQ_REMOVE(&ttransport->psks, entry, link);
    3741                 :         21 :                         spdk_memset_s(entry->psk, sizeof(entry->psk), 0, sizeof(entry->psk));
    3742                 :         21 :                         free(entry);
    3743                 :         21 :                         break;
    3744                 :            :                 }
    3745                 :            :         }
    3746                 :         48 : }
    3747                 :            : 
    3748                 :            : static void
    3749                 :          3 : nvmf_tcp_subsystem_dump_host(struct spdk_nvmf_transport *transport,
    3750                 :            :                              const struct spdk_nvmf_subsystem *subsystem, const char *hostnqn,
    3751                 :            :                              struct spdk_json_write_ctx *w)
    3752                 :            : {
    3753                 :            :         struct spdk_nvmf_tcp_transport *ttransport;
    3754                 :            :         struct tcp_psk_entry *entry;
    3755                 :            : 
    3756         [ -  + ]:          3 :         assert(transport != NULL);
    3757         [ -  + ]:          3 :         assert(subsystem != NULL);
    3758                 :            : 
    3759                 :          3 :         ttransport = SPDK_CONTAINEROF(transport, struct spdk_nvmf_tcp_transport, transport);
    3760         [ +  - ]:          3 :         TAILQ_FOREACH(entry, &ttransport->psks, link) {
    3761   [ -  +  -  +  :          3 :                 if ((strncmp(entry->hostnqn, hostnqn, SPDK_NVMF_NQN_MAX_LEN)) == 0 &&
                   +  - ]
    3762   [ -  +  -  +  :          3 :                     (strncmp(entry->subnqn, subsystem->subnqn, SPDK_NVMF_NQN_MAX_LEN)) == 0) {
                   +  - ]
    3763                 :          3 :                         spdk_json_write_named_string(w, "psk", entry->psk_path);
    3764                 :          3 :                         break;
    3765                 :            :                 }
    3766                 :            :         }
    3767                 :          3 : }
    3768                 :            : 
    3769                 :            : static void
    3770                 :        160 : nvmf_tcp_opts_init(struct spdk_nvmf_transport_opts *opts)
    3771                 :            : {
    3772                 :        160 :         opts->max_queue_depth =              SPDK_NVMF_TCP_DEFAULT_MAX_IO_QUEUE_DEPTH;
    3773                 :        160 :         opts->max_qpairs_per_ctrlr = SPDK_NVMF_TCP_DEFAULT_MAX_QPAIRS_PER_CTRLR;
    3774                 :        160 :         opts->in_capsule_data_size = SPDK_NVMF_TCP_DEFAULT_IN_CAPSULE_DATA_SIZE;
    3775                 :        160 :         opts->max_io_size =          SPDK_NVMF_TCP_DEFAULT_MAX_IO_SIZE;
    3776                 :        160 :         opts->io_unit_size =         SPDK_NVMF_TCP_DEFAULT_IO_UNIT_SIZE;
    3777                 :        160 :         opts->max_aq_depth =         SPDK_NVMF_TCP_DEFAULT_MAX_ADMIN_QUEUE_DEPTH;
    3778                 :        160 :         opts->num_shared_buffers =   SPDK_NVMF_TCP_DEFAULT_NUM_SHARED_BUFFERS;
    3779                 :        160 :         opts->buf_cache_size =               SPDK_NVMF_TCP_DEFAULT_BUFFER_CACHE_SIZE;
    3780                 :        160 :         opts->dif_insert_or_strip =  SPDK_NVMF_TCP_DEFAULT_DIF_INSERT_OR_STRIP;
    3781                 :        160 :         opts->abort_timeout_sec =    SPDK_NVMF_TCP_DEFAULT_ABORT_TIMEOUT_SEC;
    3782                 :        160 :         opts->transport_specific =      NULL;
    3783                 :        160 : }
    3784                 :            : 
    3785                 :            : const struct spdk_nvmf_transport_ops spdk_nvmf_transport_tcp = {
    3786                 :            :         .name = "TCP",
    3787                 :            :         .type = SPDK_NVME_TRANSPORT_TCP,
    3788                 :            :         .opts_init = nvmf_tcp_opts_init,
    3789                 :            :         .create = nvmf_tcp_create,
    3790                 :            :         .dump_opts = nvmf_tcp_dump_opts,
    3791                 :            :         .destroy = nvmf_tcp_destroy,
    3792                 :            : 
    3793                 :            :         .listen = nvmf_tcp_listen,
    3794                 :            :         .stop_listen = nvmf_tcp_stop_listen,
    3795                 :            : 
    3796                 :            :         .listener_discover = nvmf_tcp_discover,
    3797                 :            : 
    3798                 :            :         .poll_group_create = nvmf_tcp_poll_group_create,
    3799                 :            :         .get_optimal_poll_group = nvmf_tcp_get_optimal_poll_group,
    3800                 :            :         .poll_group_destroy = nvmf_tcp_poll_group_destroy,
    3801                 :            :         .poll_group_add = nvmf_tcp_poll_group_add,
    3802                 :            :         .poll_group_remove = nvmf_tcp_poll_group_remove,
    3803                 :            :         .poll_group_poll = nvmf_tcp_poll_group_poll,
    3804                 :            : 
    3805                 :            :         .req_free = nvmf_tcp_req_free,
    3806                 :            :         .req_complete = nvmf_tcp_req_complete,
    3807                 :            : 
    3808                 :            :         .qpair_fini = nvmf_tcp_close_qpair,
    3809                 :            :         .qpair_get_local_trid = nvmf_tcp_qpair_get_local_trid,
    3810                 :            :         .qpair_get_peer_trid = nvmf_tcp_qpair_get_peer_trid,
    3811                 :            :         .qpair_get_listen_trid = nvmf_tcp_qpair_get_listen_trid,
    3812                 :            :         .qpair_abort_request = nvmf_tcp_qpair_abort_request,
    3813                 :            :         .subsystem_add_host = nvmf_tcp_subsystem_add_host,
    3814                 :            :         .subsystem_remove_host = nvmf_tcp_subsystem_remove_host,
    3815                 :            :         .subsystem_dump_host = nvmf_tcp_subsystem_dump_host,
    3816                 :            : };
    3817                 :            : 
    3818                 :        722 : SPDK_NVMF_TRANSPORT_REGISTER(tcp, &spdk_nvmf_transport_tcp);
    3819                 :        722 : SPDK_LOG_REGISTER_COMPONENT(nvmf_tcp)

Generated by: LCOV version 1.14