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