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