LCOV - code coverage report
Current view: top level - spdk/lib/nvmf - tcp.c (source / functions) Hit Total Coverage
Test: Combined Lines: 1455 1839 79.1 %
Date: 2024-07-15 18:04:22 Functions: 93 103 90.3 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 829 1336 62.1 %

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

Generated by: LCOV version 1.14