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