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