LCOV - code coverage report
Current view: top level - lib/nvmf - tcp.c (source / functions) Hit Total Coverage
Test: ut_cov_unit.info Lines: 664 1827 36.3 %
Date: 2024-07-15 21:05:59 Functions: 46 100 46.0 %

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

Generated by: LCOV version 1.15