LCOV - code coverage report
Current view: top level - lib/nvmf - tcp.c (source / functions) Hit Total Coverage
Test: ut_cov_unit.info Lines: 672 1868 36.0 %
Date: 2024-08-13 22:09:28 Functions: 47 103 45.6 %

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

Generated by: LCOV version 1.15