LCOV - code coverage report
Current view: top level - lib/nvmf - tcp.c (source / functions) Hit Total Coverage
Test: ut_cov_unit.info Lines: 667 1838 36.3 %
Date: 2024-07-15 14:53:55 Functions: 46 101 45.5 %

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