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

Generated by: LCOV version 1.15