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