LCOV - code coverage report
Current view: top level - lib/nvmf - tcp.c (source / functions) Hit Total Coverage
Test: ut_cov_unit.info Lines: 663 1809 36.7 %
Date: 2024-07-12 17:22:26 Functions: 46 100 46.0 %

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

Generated by: LCOV version 1.15