LCOV - code coverage report
Current view: top level - lib/nvmf - tcp.c (source / functions) Hit Total Coverage
Test: ut_cov_unit.info Lines: 670 1839 36.4 %
Date: 2024-07-15 15:53:01 Functions: 47 103 45.6 %

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

Generated by: LCOV version 1.15