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