LCOV - code coverage report
Current view: top level - spdk/lib/nvmf - tcp.c (source / functions) Hit Total Coverage
Test: Combined Lines: 1477 1868 79.1 %
Date: 2024-08-13 00:13:26 Functions: 93 103 90.3 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 850 1374 61.9 %

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

Generated by: LCOV version 1.14