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) 2020 Mellanox Technologies LTD. All rights reserved.
4 : : * Copyright (c) 2021-2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
5 : : */
6 : :
7 : : /*
8 : : * NVMe/TCP transport
9 : : */
10 : :
11 : : #include "nvme_internal.h"
12 : :
13 : : #include "spdk/endian.h"
14 : : #include "spdk/likely.h"
15 : : #include "spdk/string.h"
16 : : #include "spdk/stdinc.h"
17 : : #include "spdk/crc32.h"
18 : : #include "spdk/assert.h"
19 : : #include "spdk/trace.h"
20 : : #include "spdk/util.h"
21 : : #include "spdk/nvmf.h"
22 : : #include "spdk/dma.h"
23 : :
24 : : #include "spdk_internal/nvme_tcp.h"
25 : : #include "spdk_internal/trace_defs.h"
26 : :
27 : : #define NVME_TCP_RW_BUFFER_SIZE 131072
28 : :
29 : : /* For async connect workloads, allow more time since we are more likely
30 : : * to be processing lots ICREQs at once.
31 : : */
32 : : #define ICREQ_TIMEOUT_SYNC 2 /* in seconds */
33 : : #define ICREQ_TIMEOUT_ASYNC 10 /* in seconds */
34 : :
35 : : #define NVME_TCP_HPDA_DEFAULT 0
36 : : #define NVME_TCP_MAX_R2T_DEFAULT 1
37 : : #define NVME_TCP_PDU_H2C_MIN_DATA_SIZE 4096
38 : :
39 : : /*
40 : : * Maximum value of transport_ack_timeout used by TCP controller
41 : : */
42 : : #define NVME_TCP_CTRLR_MAX_TRANSPORT_ACK_TIMEOUT 31
43 : :
44 : :
45 : : /* NVMe TCP transport extensions for spdk_nvme_ctrlr */
46 : : struct nvme_tcp_ctrlr {
47 : : struct spdk_nvme_ctrlr ctrlr;
48 : : char psk_identity[NVMF_PSK_IDENTITY_LEN];
49 : : uint8_t psk[SPDK_TLS_PSK_MAX_LEN];
50 : : int psk_size;
51 : : char *tls_cipher_suite;
52 : : };
53 : :
54 : : struct nvme_tcp_poll_group {
55 : : struct spdk_nvme_transport_poll_group group;
56 : : struct spdk_sock_group *sock_group;
57 : : uint32_t completions_per_qpair;
58 : : int64_t num_completions;
59 : :
60 : : TAILQ_HEAD(, nvme_tcp_qpair) needs_poll;
61 : : struct spdk_nvme_tcp_stat stats;
62 : : };
63 : :
64 : : /* NVMe TCP qpair extensions for spdk_nvme_qpair */
65 : : struct nvme_tcp_qpair {
66 : : struct spdk_nvme_qpair qpair;
67 : : struct spdk_sock *sock;
68 : :
69 : : TAILQ_HEAD(, nvme_tcp_req) free_reqs;
70 : : TAILQ_HEAD(, nvme_tcp_req) outstanding_reqs;
71 : :
72 : : TAILQ_HEAD(, nvme_tcp_pdu) send_queue;
73 : : struct nvme_tcp_pdu *recv_pdu;
74 : : struct nvme_tcp_pdu *send_pdu; /* only for error pdu and init pdu */
75 : : struct nvme_tcp_pdu *send_pdus; /* Used by tcp_reqs */
76 : : enum nvme_tcp_pdu_recv_state recv_state;
77 : : struct nvme_tcp_req *tcp_reqs;
78 : : struct spdk_nvme_tcp_stat *stats;
79 : :
80 : : uint16_t num_entries;
81 : : uint16_t async_complete;
82 : :
83 : : struct {
84 : : uint16_t host_hdgst_enable: 1;
85 : : uint16_t host_ddgst_enable: 1;
86 : : uint16_t icreq_send_ack: 1;
87 : : uint16_t in_connect_poll: 1;
88 : : uint16_t reserved: 12;
89 : : } flags;
90 : :
91 : : /** Specifies the maximum number of PDU-Data bytes per H2C Data Transfer PDU */
92 : : uint32_t maxh2cdata;
93 : :
94 : : uint32_t maxr2t;
95 : :
96 : : /* 0 based value, which is used to guide the padding */
97 : : uint8_t cpda;
98 : :
99 : : enum nvme_tcp_qpair_state state;
100 : :
101 : : TAILQ_ENTRY(nvme_tcp_qpair) link;
102 : : bool needs_poll;
103 : :
104 : : uint64_t icreq_timeout_tsc;
105 : :
106 : : bool shared_stats;
107 : : };
108 : :
109 : : enum nvme_tcp_req_state {
110 : : NVME_TCP_REQ_FREE,
111 : : NVME_TCP_REQ_ACTIVE,
112 : : NVME_TCP_REQ_ACTIVE_R2T,
113 : : };
114 : :
115 : : struct nvme_tcp_req {
116 : : struct nvme_request *req;
117 : : enum nvme_tcp_req_state state;
118 : : uint16_t cid;
119 : : uint16_t ttag;
120 : : uint32_t datao;
121 : : uint32_t expected_datao;
122 : : uint32_t r2tl_remain;
123 : : uint32_t active_r2ts;
124 : : /* Used to hold a value received from subsequent R2T while we are still
125 : : * waiting for H2C complete */
126 : : uint16_t ttag_r2t_next;
127 : : bool in_capsule_data;
128 : : /* It is used to track whether the req can be safely freed */
129 : : union {
130 : : uint8_t raw;
131 : : struct {
132 : : /* The last send operation completed - kernel released send buffer */
133 : : uint8_t send_ack : 1;
134 : : /* Data transfer completed - target send resp or last data bit */
135 : : uint8_t data_recv : 1;
136 : : /* tcp_req is waiting for completion of the previous send operation (buffer reclaim notification
137 : : * from kernel) to send H2C */
138 : : uint8_t h2c_send_waiting_ack : 1;
139 : : /* tcp_req received subsequent r2t while it is still waiting for send_ack.
140 : : * Rare case, actual when dealing with target that can send several R2T requests.
141 : : * SPDK TCP target sends 1 R2T for the whole data buffer */
142 : : uint8_t r2t_waiting_h2c_complete : 1;
143 : : /* Accel operation is in progress */
144 : : uint8_t in_progress_accel : 1;
145 : : uint8_t domain_in_use: 1;
146 : : uint8_t reserved : 2;
147 : : } bits;
148 : : } ordering;
149 : : struct nvme_tcp_pdu *pdu;
150 : : struct iovec iov[NVME_TCP_MAX_SGL_DESCRIPTORS];
151 : : uint32_t iovcnt;
152 : : /* Used to hold a value received from subsequent R2T while we are still
153 : : * waiting for H2C ack */
154 : : uint32_t r2tl_remain_next;
155 : : struct nvme_tcp_qpair *tqpair;
156 : : TAILQ_ENTRY(nvme_tcp_req) link;
157 : : struct spdk_nvme_cpl rsp;
158 : : uint8_t rsvd1[32];
159 : : };
160 : : SPDK_STATIC_ASSERT(sizeof(struct nvme_tcp_req) % SPDK_CACHE_LINE_SIZE == 0, "unaligned size");
161 : :
162 : : static struct spdk_nvme_tcp_stat g_dummy_stats = {};
163 : :
164 : : static void nvme_tcp_send_h2c_data(struct nvme_tcp_req *tcp_req);
165 : : static int64_t nvme_tcp_poll_group_process_completions(struct spdk_nvme_transport_poll_group
166 : : *tgroup, uint32_t completions_per_qpair, spdk_nvme_disconnected_qpair_cb disconnected_qpair_cb);
167 : : static void nvme_tcp_icresp_handle(struct nvme_tcp_qpair *tqpair, struct nvme_tcp_pdu *pdu);
168 : : static void nvme_tcp_req_complete(struct nvme_tcp_req *tcp_req, struct nvme_tcp_qpair *tqpair,
169 : : struct spdk_nvme_cpl *rsp, bool print_on_error);
170 : :
171 : : static inline struct nvme_tcp_qpair *
172 : 120112732 : nvme_tcp_qpair(struct spdk_nvme_qpair *qpair)
173 : : {
174 [ - + ]: 120112732 : assert(qpair->trtype == SPDK_NVME_TRANSPORT_TCP);
175 : 120112732 : return SPDK_CONTAINEROF(qpair, struct nvme_tcp_qpair, qpair);
176 : : }
177 : :
178 : : static inline struct nvme_tcp_poll_group *
179 : 296963943 : nvme_tcp_poll_group(struct spdk_nvme_transport_poll_group *group)
180 : : {
181 : 296963943 : return SPDK_CONTAINEROF(group, struct nvme_tcp_poll_group, group);
182 : : }
183 : :
184 : : static inline struct nvme_tcp_ctrlr *
185 : 1320 : nvme_tcp_ctrlr(struct spdk_nvme_ctrlr *ctrlr)
186 : : {
187 [ - + ]: 1320 : assert(ctrlr->trid.trtype == SPDK_NVME_TRANSPORT_TCP);
188 : 1320 : return SPDK_CONTAINEROF(ctrlr, struct nvme_tcp_ctrlr, ctrlr);
189 : : }
190 : :
191 : : static struct nvme_tcp_req *
192 : 15330471 : nvme_tcp_req_get(struct nvme_tcp_qpair *tqpair)
193 : : {
194 : : struct nvme_tcp_req *tcp_req;
195 : :
196 : 15330471 : tcp_req = TAILQ_FIRST(&tqpair->free_reqs);
197 [ + + ]: 15330471 : if (!tcp_req) {
198 : 384125 : return NULL;
199 : : }
200 : :
201 [ - + ]: 14946346 : assert(tcp_req->state == NVME_TCP_REQ_FREE);
202 : 14946346 : tcp_req->state = NVME_TCP_REQ_ACTIVE;
203 [ + + ]: 14946346 : TAILQ_REMOVE(&tqpair->free_reqs, tcp_req, link);
204 : 14946346 : tcp_req->datao = 0;
205 : 14946346 : tcp_req->expected_datao = 0;
206 : 14946346 : tcp_req->req = NULL;
207 : 14946346 : tcp_req->in_capsule_data = false;
208 : 14946346 : tcp_req->r2tl_remain = 0;
209 : 14946346 : tcp_req->r2tl_remain_next = 0;
210 : 14946346 : tcp_req->active_r2ts = 0;
211 : 14946346 : tcp_req->iovcnt = 0;
212 : 14946346 : tcp_req->ordering.raw = 0;
213 [ - + ]: 14946346 : memset(tcp_req->pdu, 0, sizeof(struct nvme_tcp_pdu));
214 [ - + ]: 14946346 : memset(&tcp_req->rsp, 0, sizeof(struct spdk_nvme_cpl));
215 : :
216 : 14946346 : return tcp_req;
217 : : }
218 : :
219 : : static void
220 : 14946376 : nvme_tcp_req_put(struct nvme_tcp_qpair *tqpair, struct nvme_tcp_req *tcp_req)
221 : : {
222 [ - + ]: 14946376 : assert(tcp_req->state != NVME_TCP_REQ_FREE);
223 : 14946376 : tcp_req->state = NVME_TCP_REQ_FREE;
224 [ + + ]: 14946376 : TAILQ_INSERT_HEAD(&tqpair->free_reqs, tcp_req, link);
225 : 14946376 : }
226 : :
227 : : static inline void
228 : 0 : nvme_tcp_accel_submit_crc32c(struct nvme_tcp_poll_group *tgroup, struct nvme_tcp_req *treq,
229 : : uint32_t *dst, struct iovec *iovs, uint32_t iovcnt, uint32_t seed,
230 : : spdk_nvme_accel_completion_cb cb_fn, void *cb_arg)
231 : : {
232 : 0 : struct spdk_nvme_poll_group *pg = tgroup->group.group;
233 : :
234 : 0 : treq->ordering.bits.in_progress_accel = 1;
235 : 0 : pg->accel_fn_table.submit_accel_crc32c(pg->ctx, dst, iovs, iovcnt, seed, cb_fn, cb_arg);
236 : 0 : }
237 : :
238 : : static inline void
239 : 757700 : nvme_tcp_accel_finish_sequence(struct nvme_tcp_poll_group *tgroup, struct nvme_tcp_req *treq,
240 : : void *seq, spdk_nvme_accel_completion_cb cb_fn, void *cb_arg)
241 : : {
242 : 757700 : struct spdk_nvme_poll_group *pg = tgroup->group.group;
243 : :
244 : 757700 : treq->ordering.bits.in_progress_accel = 1;
245 : 757700 : pg->accel_fn_table.finish_sequence(seq, cb_fn, cb_arg);
246 : 757700 : }
247 : :
248 : : static inline void
249 : 377744 : nvme_tcp_accel_reverse_sequence(struct nvme_tcp_poll_group *tgroup, void *seq)
250 : : {
251 : 377744 : struct spdk_nvme_poll_group *pg = tgroup->group.group;
252 : :
253 : 377744 : pg->accel_fn_table.reverse_sequence(seq);
254 : 377744 : }
255 : :
256 : : static inline int
257 : 757700 : nvme_tcp_accel_append_crc32c(struct nvme_tcp_poll_group *tgroup, void **seq, uint32_t *dst,
258 : : struct iovec *iovs, uint32_t iovcnt, uint32_t seed,
259 : : spdk_nvme_accel_step_cb cb_fn, void *cb_arg)
260 : : {
261 : 757700 : struct spdk_nvme_poll_group *pg = tgroup->group.group;
262 : :
263 : 757700 : return pg->accel_fn_table.append_crc32c(pg->ctx, seq, dst, iovs, iovcnt, NULL, NULL,
264 : : seed, cb_fn, cb_arg);
265 : : }
266 : :
267 : : static void
268 : 3704 : nvme_tcp_free_reqs(struct nvme_tcp_qpair *tqpair)
269 : : {
270 : 3704 : free(tqpair->tcp_reqs);
271 : 3704 : tqpair->tcp_reqs = NULL;
272 : :
273 : 3704 : spdk_free(tqpair->send_pdus);
274 : 3704 : tqpair->send_pdus = NULL;
275 : 3704 : }
276 : :
277 : : static int
278 : 3719 : nvme_tcp_alloc_reqs(struct nvme_tcp_qpair *tqpair)
279 : : {
280 : : uint16_t i;
281 : : struct nvme_tcp_req *tcp_req;
282 : :
283 : 3719 : tqpair->tcp_reqs = aligned_alloc(SPDK_CACHE_LINE_SIZE,
284 : 3719 : tqpair->num_entries * sizeof(*tcp_req));
285 [ - + ]: 3719 : if (tqpair->tcp_reqs == NULL) {
286 : 0 : SPDK_ERRLOG("Failed to allocate tcp_reqs on tqpair=%p\n", tqpair);
287 : 0 : goto fail;
288 : : }
289 : :
290 : : /* Add additional 2 member for the send_pdu, recv_pdu owned by the tqpair */
291 : 3719 : tqpair->send_pdus = spdk_zmalloc((tqpair->num_entries + 2) * sizeof(struct nvme_tcp_pdu),
292 : : 0x1000, NULL,
293 : : SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA);
294 : :
295 [ - + ]: 3719 : if (tqpair->send_pdus == NULL) {
296 : 0 : SPDK_ERRLOG("Failed to allocate send_pdus on tqpair=%p\n", tqpair);
297 : 0 : goto fail;
298 : : }
299 : :
300 [ - + ]: 3719 : memset(tqpair->tcp_reqs, 0, tqpair->num_entries * sizeof(*tcp_req));
301 : 3719 : TAILQ_INIT(&tqpair->send_queue);
302 : 3719 : TAILQ_INIT(&tqpair->free_reqs);
303 : 3719 : TAILQ_INIT(&tqpair->outstanding_reqs);
304 : 3719 : tqpair->qpair.queue_depth = 0;
305 [ + + ]: 704631 : for (i = 0; i < tqpair->num_entries; i++) {
306 : 700912 : tcp_req = &tqpair->tcp_reqs[i];
307 : 700912 : tcp_req->cid = i;
308 : 700912 : tcp_req->tqpair = tqpair;
309 : 700912 : tcp_req->pdu = &tqpair->send_pdus[i];
310 : 700912 : TAILQ_INSERT_TAIL(&tqpair->free_reqs, tcp_req, link);
311 : : }
312 : :
313 : 3719 : tqpair->send_pdu = &tqpair->send_pdus[i];
314 : 3719 : tqpair->recv_pdu = &tqpair->send_pdus[i + 1];
315 : :
316 : 3719 : return 0;
317 : 0 : fail:
318 : 0 : nvme_tcp_free_reqs(tqpair);
319 : 0 : return -ENOMEM;
320 : : }
321 : :
322 : : static inline void
323 : 74713434 : nvme_tcp_qpair_set_recv_state(struct nvme_tcp_qpair *tqpair,
324 : : enum nvme_tcp_pdu_recv_state state)
325 : : {
326 [ + + ]: 74713434 : if (tqpair->recv_state == state) {
327 : 461 : SPDK_ERRLOG("The recv state of tqpair=%p is same with the state(%d) to be set\n",
328 : : tqpair, state);
329 : 461 : return;
330 : : }
331 : :
332 [ + + ]: 74712973 : if (state == NVME_TCP_PDU_RECV_STATE_ERROR) {
333 [ - + ]: 24 : assert(TAILQ_EMPTY(&tqpair->outstanding_reqs));
334 : : }
335 : :
336 : 74712973 : tqpair->recv_state = state;
337 : : }
338 : :
339 : : static void nvme_tcp_qpair_abort_reqs(struct spdk_nvme_qpair *qpair, uint32_t dnr);
340 : :
341 : : static void
342 : 16035 : nvme_tcp_ctrlr_disconnect_qpair(struct spdk_nvme_ctrlr *ctrlr, struct spdk_nvme_qpair *qpair)
343 : : {
344 : 16035 : struct nvme_tcp_qpair *tqpair = nvme_tcp_qpair(qpair);
345 : : struct nvme_tcp_pdu *pdu;
346 : : int rc;
347 : : struct nvme_tcp_poll_group *group;
348 : :
349 [ + + + + ]: 16035 : if (tqpair->needs_poll) {
350 : 14 : group = nvme_tcp_poll_group(qpair->poll_group);
351 [ - + ]: 14 : TAILQ_REMOVE(&group->needs_poll, tqpair, link);
352 : 14 : tqpair->needs_poll = false;
353 : : }
354 : :
355 : 16035 : rc = spdk_sock_close(&tqpair->sock);
356 : :
357 [ + + ]: 16035 : if (tqpair->sock != NULL) {
358 : 5 : SPDK_ERRLOG("tqpair=%p, errno=%d, rc=%d\n", tqpair, errno, rc);
359 : : /* Set it to NULL manually */
360 : 5 : tqpair->sock = NULL;
361 : : }
362 : :
363 : : /* clear the send_queue */
364 [ + + ]: 16075 : while (!TAILQ_EMPTY(&tqpair->send_queue)) {
365 : 40 : pdu = TAILQ_FIRST(&tqpair->send_queue);
366 : : /* Remove the pdu from the send_queue to prevent the wrong sending out
367 : : * in the next round connection
368 : : */
369 [ + + ]: 40 : TAILQ_REMOVE(&tqpair->send_queue, pdu, tailq);
370 : : }
371 : :
372 : 16035 : nvme_tcp_qpair_abort_reqs(qpair, 0);
373 : :
374 : : /* If the qpair is marked as asynchronous, let it go through the process_completions() to
375 : : * let any outstanding requests (e.g. those with outstanding accel operations) complete.
376 : : * Otherwise, there's no way of waiting for them, so tqpair->outstanding_reqs has to be
377 : : * empty.
378 : : */
379 [ + + ]: 16035 : if (qpair->async) {
380 : 2543 : nvme_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_QUIESCING);
381 : : } else {
382 [ - + ]: 13492 : assert(TAILQ_EMPTY(&tqpair->outstanding_reqs));
383 : 13492 : nvme_transport_ctrlr_disconnect_qpair_done(qpair);
384 : : }
385 : 16035 : }
386 : :
387 : : static int
388 : 3694 : nvme_tcp_ctrlr_delete_io_qpair(struct spdk_nvme_ctrlr *ctrlr, struct spdk_nvme_qpair *qpair)
389 : : {
390 : 3694 : struct nvme_tcp_qpair *tqpair = nvme_tcp_qpair(qpair);
391 : :
392 [ - + ]: 3694 : assert(qpair != NULL);
393 : 3694 : nvme_tcp_qpair_abort_reqs(qpair, 0);
394 [ - + ]: 3694 : assert(TAILQ_EMPTY(&tqpair->outstanding_reqs));
395 : :
396 : 3694 : nvme_qpair_deinit(qpair);
397 : 3694 : nvme_tcp_free_reqs(tqpair);
398 [ + + + + ]: 3694 : if (!tqpair->shared_stats) {
399 : 2811 : free(tqpair->stats);
400 : : }
401 : 3694 : free(tqpair);
402 : :
403 : 3694 : return 0;
404 : : }
405 : :
406 : : static int
407 : 1266 : nvme_tcp_ctrlr_enable(struct spdk_nvme_ctrlr *ctrlr)
408 : : {
409 : 1266 : return 0;
410 : : }
411 : :
412 : : static int
413 : 1310 : nvme_tcp_ctrlr_destruct(struct spdk_nvme_ctrlr *ctrlr)
414 : : {
415 : 1310 : struct nvme_tcp_ctrlr *tctrlr = nvme_tcp_ctrlr(ctrlr);
416 : :
417 [ + + ]: 1310 : if (ctrlr->adminq) {
418 : 1281 : nvme_tcp_ctrlr_delete_io_qpair(ctrlr, ctrlr->adminq);
419 : : }
420 : :
421 : 1310 : nvme_ctrlr_destruct_finish(ctrlr);
422 : :
423 : 1310 : free(tctrlr);
424 : :
425 : 1310 : return 0;
426 : : }
427 : :
428 : : /* If there are queued requests, we assume they are queued because they are waiting
429 : : * for resources to be released. Those resources are almost certainly released in
430 : : * response to a PDU completing. However, to attempt to make forward progress
431 : : * the qpair needs to be polled and we can't rely on another network event to make
432 : : * that happen. Add it to a list of qpairs to poll regardless of network activity.
433 : : *
434 : : * Besides, when tqpair state is NVME_TCP_QPAIR_STATE_FABRIC_CONNECT_POLL or
435 : : * NVME_TCP_QPAIR_STATE_INITIALIZING, need to add it to needs_poll list too to make
436 : : * forward progress in case that the resources are released after icreq's or CONNECT's
437 : : * resp is processed. */
438 : : static void
439 : 15857545 : nvme_tcp_cond_schedule_qpair_polling(struct nvme_tcp_qpair *tqpair)
440 : : {
441 : : struct nvme_tcp_poll_group *pgroup;
442 : :
443 [ - + + + : 15857545 : if (tqpair->needs_poll || !tqpair->qpair.poll_group) {
+ + ]
444 : 11071661 : return;
445 : : }
446 : :
447 [ + + ]: 4785884 : if (STAILQ_EMPTY(&tqpair->qpair.queued_req) &&
448 [ + + + - ]: 3639123 : spdk_likely(tqpair->state != NVME_TCP_QPAIR_STATE_FABRIC_CONNECT_POLL &&
449 : : tqpair->state != NVME_TCP_QPAIR_STATE_INITIALIZING)) {
450 : 3638949 : return;
451 : : }
452 : :
453 : 1146935 : pgroup = nvme_tcp_poll_group(tqpair->qpair.poll_group);
454 : 1146935 : TAILQ_INSERT_TAIL(&pgroup->needs_poll, tqpair, link);
455 : 1146935 : tqpair->needs_poll = true;
456 : : }
457 : :
458 : : static void
459 : 15479801 : pdu_write_done(void *cb_arg, int err)
460 : : {
461 : 15479801 : struct nvme_tcp_pdu *pdu = cb_arg;
462 : 15479801 : struct nvme_tcp_qpair *tqpair = pdu->qpair;
463 : :
464 : 15479801 : nvme_tcp_cond_schedule_qpair_polling(tqpair);
465 [ + + ]: 15479801 : TAILQ_REMOVE(&tqpair->send_queue, pdu, tailq);
466 : :
467 [ + + ]: 15479801 : if (err != 0) {
468 : 638 : nvme_transport_ctrlr_disconnect_qpair(tqpair->qpair.ctrlr, &tqpair->qpair);
469 : 638 : return;
470 : : }
471 : :
472 [ - + ]: 15479163 : assert(pdu->cb_fn != NULL);
473 : 15479163 : pdu->cb_fn(pdu->cb_arg);
474 : : }
475 : :
476 : : static void
477 : 0 : pdu_write_fail(struct nvme_tcp_pdu *pdu, int status)
478 : : {
479 : 0 : struct nvme_tcp_qpair *tqpair = pdu->qpair;
480 : :
481 : : /* This function is similar to pdu_write_done(), but it should be called before a PDU is
482 : : * sent over the socket */
483 : 0 : TAILQ_INSERT_TAIL(&tqpair->send_queue, pdu, tailq);
484 : 0 : pdu_write_done(pdu, status);
485 : 0 : }
486 : :
487 : : static void
488 : 0 : pdu_seq_fail(struct nvme_tcp_pdu *pdu, int status)
489 : : {
490 : 0 : struct nvme_tcp_req *treq = pdu->req;
491 : :
492 : 0 : SPDK_ERRLOG("Failed to execute accel sequence: %d\n", status);
493 : 0 : nvme_tcp_cond_schedule_qpair_polling(pdu->qpair);
494 : 0 : treq->rsp.status.sc = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR;
495 : 0 : nvme_tcp_req_complete(treq, treq->tqpair, &treq->rsp, true);
496 : 0 : }
497 : :
498 : : static void
499 : 15479916 : _tcp_write_pdu(struct nvme_tcp_pdu *pdu)
500 : : {
501 : 15479916 : uint32_t mapped_length = 0;
502 : 15479916 : struct nvme_tcp_qpair *tqpair = pdu->qpair;
503 : :
504 : 15479939 : pdu->sock_req.iovcnt = nvme_tcp_build_iovs(pdu->iov, SPDK_COUNTOF(pdu->iov), pdu,
505 : 15479916 : (bool)tqpair->flags.host_hdgst_enable, (bool)tqpair->flags.host_ddgst_enable,
506 : : &mapped_length);
507 : 15479916 : TAILQ_INSERT_TAIL(&tqpair->send_queue, pdu, tailq);
508 [ - + ]: 15479916 : if (spdk_unlikely(mapped_length < pdu->data_len)) {
509 : 0 : SPDK_ERRLOG("could not map the whole %u bytes (mapped only %u bytes)\n", pdu->data_len,
510 : : mapped_length);
511 : 0 : pdu_write_done(pdu, -EINVAL);
512 : 0 : return;
513 : : }
514 : 15479916 : pdu->sock_req.cb_fn = pdu_write_done;
515 : 15479916 : pdu->sock_req.cb_arg = pdu;
516 : 15479916 : tqpair->stats->submitted_requests++;
517 : 15479916 : spdk_sock_writev_async(tqpair->sock, &pdu->sock_req);
518 : : }
519 : :
520 : : static void
521 : 379956 : tcp_write_pdu_seq_cb(void *ctx, int status)
522 : : {
523 : 379956 : struct nvme_tcp_pdu *pdu = ctx;
524 : 379956 : struct nvme_tcp_req *treq = pdu->req;
525 : 379956 : struct nvme_request *req = treq->req;
526 : :
527 [ - + ]: 379956 : assert(treq->ordering.bits.in_progress_accel);
528 : 379956 : treq->ordering.bits.in_progress_accel = 0;
529 : :
530 : 379956 : req->accel_sequence = NULL;
531 [ - + ]: 379956 : if (spdk_unlikely(status != 0)) {
532 : 0 : pdu_seq_fail(pdu, status);
533 : 0 : return;
534 : : }
535 : :
536 : 379956 : _tcp_write_pdu(pdu);
537 : : }
538 : :
539 : : static void
540 : 15479916 : tcp_write_pdu(struct nvme_tcp_pdu *pdu)
541 : : {
542 : 15479916 : struct nvme_tcp_req *treq = pdu->req;
543 : 15479916 : struct nvme_tcp_qpair *tqpair = pdu->qpair;
544 : : struct nvme_tcp_poll_group *tgroup;
545 : : struct nvme_request *req;
546 : :
547 [ + + ]: 15479916 : if (spdk_likely(treq != NULL)) {
548 : 15475553 : req = treq->req;
549 [ + + + + ]: 15947431 : if (req->accel_sequence != NULL &&
550 : 471878 : spdk_nvme_opc_get_data_transfer(req->cmd.opc) == SPDK_NVME_DATA_HOST_TO_CONTROLLER &&
551 [ + + ]: 392961 : pdu->data_len > 0) {
552 [ - + ]: 379956 : assert(tqpair->qpair.poll_group != NULL);
553 : 379956 : tgroup = nvme_tcp_poll_group(tqpair->qpair.poll_group);
554 : 379956 : nvme_tcp_accel_finish_sequence(tgroup, treq, req->accel_sequence,
555 : : tcp_write_pdu_seq_cb, pdu);
556 : 379956 : return;
557 : : }
558 : : }
559 : :
560 : 15099960 : _tcp_write_pdu(pdu);
561 : : }
562 : :
563 : : static void
564 : 0 : pdu_accel_compute_crc32_done(void *cb_arg, int status)
565 : : {
566 : 0 : struct nvme_tcp_pdu *pdu = cb_arg;
567 : 0 : struct nvme_tcp_req *req = pdu->req;
568 : :
569 [ # # ]: 0 : assert(req->ordering.bits.in_progress_accel);
570 : 0 : req->ordering.bits.in_progress_accel = 0;
571 : :
572 [ # # ]: 0 : if (spdk_unlikely(status)) {
573 : 0 : SPDK_ERRLOG("Failed to compute the data digest for pdu =%p\n", pdu);
574 : 0 : pdu_write_fail(pdu, status);
575 : 0 : return;
576 : : }
577 : :
578 : 0 : pdu->data_digest_crc32 ^= SPDK_CRC32C_XOR;
579 : 0 : MAKE_DIGEST_WORD(pdu->data_digest, pdu->data_digest_crc32);
580 : :
581 : 0 : _tcp_write_pdu(pdu);
582 : : }
583 : :
584 : : static void
585 : 0 : pdu_accel_compute_crc32_seq_cb(void *cb_arg, int status)
586 : : {
587 : 0 : struct nvme_tcp_pdu *pdu = cb_arg;
588 : 0 : struct nvme_tcp_qpair *tqpair = pdu->qpair;
589 : 0 : struct nvme_tcp_poll_group *tgroup = nvme_tcp_poll_group(tqpair->qpair.poll_group);
590 : 0 : struct nvme_tcp_req *treq = pdu->req;
591 : 0 : struct nvme_request *req = treq->req;
592 : :
593 [ # # ]: 0 : assert(treq->ordering.bits.in_progress_accel);
594 : 0 : treq->ordering.bits.in_progress_accel = 0;
595 : :
596 : 0 : req->accel_sequence = NULL;
597 [ # # ]: 0 : if (spdk_unlikely(status != 0)) {
598 : 0 : pdu_seq_fail(pdu, status);
599 : 0 : return;
600 : : }
601 : :
602 : 0 : nvme_tcp_accel_submit_crc32c(tgroup, pdu->req, &pdu->data_digest_crc32,
603 : 0 : pdu->data_iov, pdu->data_iovcnt, 0,
604 : : pdu_accel_compute_crc32_done, pdu);
605 : : }
606 : :
607 : : static void
608 : 379956 : pdu_accel_seq_compute_crc32_done(void *cb_arg)
609 : : {
610 : 379956 : struct nvme_tcp_pdu *pdu = cb_arg;
611 : :
612 : 379956 : pdu->data_digest_crc32 ^= SPDK_CRC32C_XOR;
613 : 379956 : MAKE_DIGEST_WORD(pdu->data_digest, pdu->data_digest_crc32);
614 : 379956 : }
615 : :
616 : : static bool
617 : 397879 : pdu_accel_compute_crc32(struct nvme_tcp_pdu *pdu)
618 : : {
619 : 397879 : struct nvme_tcp_qpair *tqpair = pdu->qpair;
620 : 397879 : struct nvme_tcp_poll_group *tgroup = nvme_tcp_poll_group(tqpair->qpair.poll_group);
621 : 397879 : struct nvme_request *req = ((struct nvme_tcp_req *)pdu->req)->req;
622 : : int rc;
623 : :
624 : : /* Only support this limited case for the first step */
625 [ + + - + : 397879 : if (spdk_unlikely(nvme_qpair_get_state(&tqpair->qpair) < NVME_QPAIR_CONNECTED ||
+ + - + ]
626 : : pdu->dif_ctx != NULL ||
627 : : pdu->data_len % SPDK_NVME_TCP_DIGEST_ALIGNMENT != 0)) {
628 : 115 : return false;
629 : : }
630 : :
631 [ - + ]: 397764 : if (tqpair->qpair.poll_group == NULL) {
632 : 0 : return false;
633 : : }
634 : :
635 [ + + ]: 397764 : if (tgroup->group.group->accel_fn_table.append_crc32c != NULL) {
636 : 759912 : rc = nvme_tcp_accel_append_crc32c(tgroup, &req->accel_sequence,
637 : : &pdu->data_digest_crc32,
638 : 379956 : pdu->data_iov, pdu->data_iovcnt, 0,
639 : : pdu_accel_seq_compute_crc32_done, pdu);
640 [ - + ]: 379956 : if (spdk_unlikely(rc != 0)) {
641 : : /* If accel is out of resources, fall back to non-accelerated crc32 */
642 [ # # ]: 0 : if (rc == -ENOMEM) {
643 : 0 : return false;
644 : : }
645 : :
646 : 0 : SPDK_ERRLOG("Failed to append crc32c operation: %d\n", rc);
647 : 0 : pdu_write_fail(pdu, rc);
648 : 0 : return true;
649 : : }
650 : :
651 : 379956 : tcp_write_pdu(pdu);
652 : 379956 : return true;
653 [ - + ]: 17808 : } else if (tgroup->group.group->accel_fn_table.submit_accel_crc32c != NULL) {
654 [ # # ]: 0 : if (req->accel_sequence != NULL) {
655 : 0 : nvme_tcp_accel_finish_sequence(tgroup, pdu->req, req->accel_sequence,
656 : : pdu_accel_compute_crc32_seq_cb, pdu);
657 : : } else {
658 : 0 : nvme_tcp_accel_submit_crc32c(tgroup, pdu->req, &pdu->data_digest_crc32,
659 : 0 : pdu->data_iov, pdu->data_iovcnt, 0,
660 : : pdu_accel_compute_crc32_done, pdu);
661 : : }
662 : :
663 : 0 : return true;
664 : : }
665 : :
666 : 17808 : return false;
667 : : }
668 : :
669 : : static void
670 : 0 : pdu_compute_crc32_seq_cb(void *cb_arg, int status)
671 : : {
672 : 0 : struct nvme_tcp_pdu *pdu = cb_arg;
673 : 0 : struct nvme_tcp_req *treq = pdu->req;
674 : 0 : struct nvme_request *req = treq->req;
675 : : uint32_t crc32c;
676 : :
677 [ # # ]: 0 : assert(treq->ordering.bits.in_progress_accel);
678 : 0 : treq->ordering.bits.in_progress_accel = 0;
679 : :
680 : 0 : req->accel_sequence = NULL;
681 [ # # ]: 0 : if (spdk_unlikely(status != 0)) {
682 : 0 : pdu_seq_fail(pdu, status);
683 : 0 : return;
684 : : }
685 : :
686 : 0 : crc32c = nvme_tcp_pdu_calc_data_digest(pdu);
687 : 0 : crc32c = crc32c ^ SPDK_CRC32C_XOR;
688 : 0 : MAKE_DIGEST_WORD(pdu->data_digest, crc32c);
689 : :
690 : 0 : _tcp_write_pdu(pdu);
691 : : }
692 : :
693 : : static void
694 : 15479916 : pdu_compute_crc32(struct nvme_tcp_pdu *pdu)
695 : : {
696 : 15479916 : struct nvme_tcp_qpair *tqpair = pdu->qpair;
697 : : struct nvme_tcp_poll_group *tgroup;
698 : : struct nvme_request *req;
699 : : uint32_t crc32c;
700 : :
701 : : /* Data Digest */
702 [ + + + + : 15479916 : if (pdu->data_len > 0 && g_nvme_tcp_ddgst[pdu->hdr.common.pdu_type] &&
+ + + + ]
703 : : tqpair->flags.host_ddgst_enable) {
704 [ + + ]: 397879 : if (pdu_accel_compute_crc32(pdu)) {
705 : 379956 : return;
706 : : }
707 : :
708 : 17923 : req = ((struct nvme_tcp_req *)pdu->req)->req;
709 [ - + ]: 17923 : if (req->accel_sequence != NULL) {
710 : 0 : tgroup = nvme_tcp_poll_group(tqpair->qpair.poll_group);
711 : 0 : nvme_tcp_accel_finish_sequence(tgroup, pdu->req, req->accel_sequence,
712 : : pdu_compute_crc32_seq_cb, pdu);
713 : 0 : return;
714 : : }
715 : :
716 : 17923 : crc32c = nvme_tcp_pdu_calc_data_digest(pdu);
717 : 17923 : crc32c = crc32c ^ SPDK_CRC32C_XOR;
718 : 17923 : MAKE_DIGEST_WORD(pdu->data_digest, crc32c);
719 : : }
720 : :
721 : 15099960 : tcp_write_pdu(pdu);
722 : : }
723 : :
724 : : static int
725 : 15479916 : nvme_tcp_qpair_write_pdu(struct nvme_tcp_qpair *tqpair,
726 : : struct nvme_tcp_pdu *pdu,
727 : : nvme_tcp_qpair_xfer_complete_cb cb_fn,
728 : : void *cb_arg)
729 : : {
730 : : int hlen;
731 : : uint32_t crc32c;
732 : :
733 : 15479916 : hlen = pdu->hdr.common.hlen;
734 : 15479916 : pdu->cb_fn = cb_fn;
735 : 15479916 : pdu->cb_arg = cb_arg;
736 : 15479916 : pdu->qpair = tqpair;
737 : :
738 : : /* Header Digest */
739 [ + + + + : 15479916 : if (g_nvme_tcp_hdgst[pdu->hdr.common.pdu_type] && tqpair->flags.host_hdgst_enable) {
+ + ]
740 : 56721 : crc32c = nvme_tcp_pdu_calc_header_digest(pdu);
741 : 56721 : MAKE_DIGEST_WORD((uint8_t *)&pdu->hdr.raw[hlen], crc32c);
742 : : }
743 : :
744 : 15479916 : pdu_compute_crc32(pdu);
745 : :
746 : 15479916 : return 0;
747 : : }
748 : :
749 : : static int
750 : 15414240 : nvme_tcp_try_memory_translation(struct nvme_tcp_req *tcp_req, void **addr, uint32_t length)
751 : : {
752 : 15414240 : struct nvme_request *req = tcp_req->req;
753 : 15414240 : struct spdk_memory_domain_translation_result translation = {
754 : : .iov_count = 0,
755 : : .size = sizeof(translation)
756 : : };
757 : : int rc;
758 : :
759 [ + + ]: 15414240 : if (!tcp_req->ordering.bits.domain_in_use) {
760 : 15335323 : return 0;
761 : : }
762 : :
763 : 157834 : rc = spdk_memory_domain_translate_data(req->payload.opts->memory_domain,
764 : 78917 : req->payload.opts->memory_domain_ctx, spdk_memory_domain_get_system_domain(), NULL, *addr, length,
765 : : &translation);
766 [ + - - + ]: 78917 : if (spdk_unlikely(rc || translation.iov_count != 1)) {
767 : 0 : SPDK_ERRLOG("DMA memory translation failed, rc %d, iov_count %u\n", rc, translation.iov_count);
768 : 0 : return -EFAULT;
769 : : }
770 : :
771 [ - + ]: 78917 : assert(length == translation.iov.iov_len);
772 : 78917 : *addr = translation.iov.iov_base;
773 : 78917 : return 0;
774 : : }
775 : :
776 : : /*
777 : : * Build SGL describing contiguous payload buffer.
778 : : */
779 : : static int
780 : 14857351 : nvme_tcp_build_contig_request(struct nvme_tcp_qpair *tqpair, struct nvme_tcp_req *tcp_req)
781 : : {
782 : 14857351 : struct nvme_request *req = tcp_req->req;
783 : :
784 : : /* ubsan complains about applying zero offset to null pointer if contig_or_cb_arg is NULL,
785 : : * so just double cast it to make it go away */
786 : 14857351 : void *addr = (void *)((uintptr_t)req->payload.contig_or_cb_arg + req->payload_offset);
787 : 14857351 : size_t length = req->payload_size;
788 : : int rc;
789 : :
790 [ - + + + ]: 14857351 : SPDK_DEBUGLOG(nvme, "enter\n");
791 : :
792 [ - + ]: 14857351 : assert(nvme_payload_type(&req->payload) == NVME_PAYLOAD_TYPE_CONTIG);
793 : 14857351 : rc = nvme_tcp_try_memory_translation(tcp_req, &addr, length);
794 [ - + ]: 14857351 : if (spdk_unlikely(rc)) {
795 : 0 : return rc;
796 : : }
797 : :
798 : 14857351 : tcp_req->iov[0].iov_base = addr;
799 : 14857351 : tcp_req->iov[0].iov_len = length;
800 : 14857351 : tcp_req->iovcnt = 1;
801 : 14857351 : return 0;
802 : : }
803 : :
804 : : /*
805 : : * Build SGL describing scattered payload buffer.
806 : : */
807 : : static int
808 : 165310 : nvme_tcp_build_sgl_request(struct nvme_tcp_qpair *tqpair, struct nvme_tcp_req *tcp_req)
809 : : {
810 : : int rc;
811 : 165310 : uint32_t length, remaining_size, iovcnt = 0, max_num_sgl;
812 : 165310 : struct nvme_request *req = tcp_req->req;
813 : :
814 [ - + - + ]: 165310 : SPDK_DEBUGLOG(nvme, "enter\n");
815 : :
816 [ - + ]: 165310 : assert(req->payload_size != 0);
817 [ - + ]: 165310 : assert(nvme_payload_type(&req->payload) == NVME_PAYLOAD_TYPE_SGL);
818 [ - + ]: 165310 : assert(req->payload.reset_sgl_fn != NULL);
819 [ - + ]: 165310 : assert(req->payload.next_sge_fn != NULL);
820 : 165310 : req->payload.reset_sgl_fn(req->payload.contig_or_cb_arg, req->payload_offset);
821 : :
822 : 165310 : max_num_sgl = spdk_min(req->qpair->ctrlr->max_sges, NVME_TCP_MAX_SGL_DESCRIPTORS);
823 : 165310 : remaining_size = req->payload_size;
824 : :
825 : : do {
826 : 65636 : void *addr;
827 : :
828 : 556889 : rc = req->payload.next_sge_fn(req->payload.contig_or_cb_arg, &addr, &length);
829 [ - + ]: 556889 : if (rc) {
830 : 0 : return -1;
831 : : }
832 : :
833 : 556889 : rc = nvme_tcp_try_memory_translation(tcp_req, &addr, length);
834 [ - + ]: 556889 : if (spdk_unlikely(rc)) {
835 : 0 : return rc;
836 : : }
837 : :
838 : 556889 : length = spdk_min(length, remaining_size);
839 : 556889 : tcp_req->iov[iovcnt].iov_base = addr;
840 : 556889 : tcp_req->iov[iovcnt].iov_len = length;
841 : 556889 : remaining_size -= length;
842 : 556889 : iovcnt++;
843 [ + + + + ]: 556889 : } while (remaining_size > 0 && iovcnt < max_num_sgl);
844 : :
845 : :
846 : : /* Should be impossible if we did our sgl checks properly up the stack, but do a sanity check here. */
847 [ + + ]: 165310 : if (remaining_size > 0) {
848 : 10 : SPDK_ERRLOG("Failed to construct tcp_req=%p, and the iovcnt=%u, remaining_size=%u\n",
849 : : tcp_req, iovcnt, remaining_size);
850 : 10 : return -1;
851 : : }
852 : :
853 : 165300 : tcp_req->iovcnt = iovcnt;
854 : :
855 : 165300 : return 0;
856 : : }
857 : :
858 : : static int
859 : 14946351 : nvme_tcp_req_init(struct nvme_tcp_qpair *tqpair, struct nvme_request *req,
860 : : struct nvme_tcp_req *tcp_req)
861 : : {
862 : 14946351 : struct spdk_nvme_ctrlr *ctrlr = tqpair->qpair.ctrlr;
863 : 14946351 : int rc = 0;
864 : : enum spdk_nvme_data_transfer xfer;
865 : : uint32_t max_in_capsule_data_size;
866 : :
867 : 14946351 : tcp_req->req = req;
868 [ + + + + ]: 14946351 : tcp_req->ordering.bits.domain_in_use = (req->payload.opts && req->payload.opts->memory_domain);
869 : :
870 : 14946351 : req->cmd.cid = tcp_req->cid;
871 : 14946351 : req->cmd.psdt = SPDK_NVME_PSDT_SGL_MPTR_CONTIG;
872 : 14946351 : req->cmd.dptr.sgl1.unkeyed.type = SPDK_NVME_SGL_TYPE_TRANSPORT_DATA_BLOCK;
873 : 14946351 : req->cmd.dptr.sgl1.unkeyed.subtype = SPDK_NVME_SGL_SUBTYPE_TRANSPORT;
874 : 14946351 : req->cmd.dptr.sgl1.unkeyed.length = req->payload_size;
875 : :
876 [ + + ]: 14946351 : if (spdk_unlikely(req->cmd.opc == SPDK_NVME_OPC_FABRIC)) {
877 : 66114 : struct spdk_nvmf_capsule_cmd *nvmf_cmd = (struct spdk_nvmf_capsule_cmd *)&req->cmd;
878 : :
879 : 66114 : xfer = spdk_nvme_opc_get_data_transfer(nvmf_cmd->fctype);
880 : : } else {
881 : 14880237 : xfer = spdk_nvme_opc_get_data_transfer(req->cmd.opc);
882 : : }
883 : :
884 : : /* For c2h delay filling in the iov until the data arrives.
885 : : * For h2c some delay is also possible if data doesn't fit into cmd capsule (not implemented). */
886 [ + + ]: 14946351 : if (nvme_payload_type(&req->payload) == NVME_PAYLOAD_TYPE_CONTIG) {
887 [ + + ]: 14781056 : if (xfer != SPDK_NVME_DATA_CONTROLLER_TO_HOST) {
888 : 7528954 : rc = nvme_tcp_build_contig_request(tqpair, tcp_req);
889 : : }
890 [ + - ]: 165295 : } else if (nvme_payload_type(&req->payload) == NVME_PAYLOAD_TYPE_SGL) {
891 [ + + ]: 165295 : if (xfer != SPDK_NVME_DATA_CONTROLLER_TO_HOST) {
892 : 54436 : rc = nvme_tcp_build_sgl_request(tqpair, tcp_req);
893 : : }
894 : : } else {
895 : 0 : rc = -1;
896 : : }
897 : :
898 [ + + ]: 14946351 : if (rc) {
899 : 5 : return rc;
900 : : }
901 : :
902 [ + + ]: 14946346 : if (xfer == SPDK_NVME_DATA_HOST_TO_CONTROLLER) {
903 : 6995643 : max_in_capsule_data_size = ctrlr->ioccsz_bytes;
904 [ + + + + ]: 6995643 : if (spdk_unlikely((req->cmd.opc == SPDK_NVME_OPC_FABRIC) ||
905 : : nvme_qpair_is_admin_queue(&tqpair->qpair))) {
906 : 25445 : max_in_capsule_data_size = SPDK_NVME_TCP_IN_CAPSULE_DATA_MAX_SIZE;
907 : : }
908 : :
909 [ + + ]: 6995643 : if (req->payload_size <= max_in_capsule_data_size) {
910 : 6466373 : req->cmd.dptr.sgl1.unkeyed.type = SPDK_NVME_SGL_TYPE_DATA_BLOCK;
911 : 6466373 : req->cmd.dptr.sgl1.unkeyed.subtype = SPDK_NVME_SGL_SUBTYPE_OFFSET;
912 : 6466373 : req->cmd.dptr.sgl1.address = 0;
913 : 6466373 : tcp_req->in_capsule_data = true;
914 : : }
915 : : }
916 : :
917 : 14946346 : return 0;
918 : : }
919 : :
920 : : static inline bool
921 : 30412314 : nvme_tcp_req_complete_safe(struct nvme_tcp_req *tcp_req)
922 : : {
923 [ + + + + ]: 30412314 : if (!(tcp_req->ordering.bits.send_ack && tcp_req->ordering.bits.data_recv &&
924 [ - + ]: 14937409 : !tcp_req->ordering.bits.in_progress_accel)) {
925 : 15474905 : return false;
926 : : }
927 : :
928 [ - + ]: 14937409 : assert(tcp_req->state == NVME_TCP_REQ_ACTIVE);
929 [ - + ]: 14937409 : assert(tcp_req->tqpair != NULL);
930 [ - + ]: 14937409 : assert(tcp_req->req != NULL);
931 : :
932 : 14937409 : nvme_tcp_req_complete(tcp_req, tcp_req->tqpair, &tcp_req->rsp, true);
933 : 14937409 : return true;
934 : : }
935 : :
936 : : static void
937 : 14945749 : nvme_tcp_qpair_cmd_send_complete(void *cb_arg)
938 : : {
939 : 14945749 : struct nvme_tcp_req *tcp_req = cb_arg;
940 : :
941 [ - + + + ]: 14945749 : SPDK_DEBUGLOG(nvme, "tcp req %p, cid %u, qid %u\n", tcp_req, tcp_req->cid,
942 : : tcp_req->tqpair->qpair.id);
943 : 14945749 : tcp_req->ordering.bits.send_ack = 1;
944 : : /* Handle the r2t case */
945 [ - + ]: 14945749 : if (spdk_unlikely(tcp_req->ordering.bits.h2c_send_waiting_ack)) {
946 [ # # # # ]: 0 : SPDK_DEBUGLOG(nvme, "tcp req %p, send H2C data\n", tcp_req);
947 : 0 : nvme_tcp_send_h2c_data(tcp_req);
948 : : } else {
949 [ - + + + : 14945749 : if (tcp_req->in_capsule_data && tcp_req->ordering.bits.domain_in_use) {
+ + ]
950 : 131824 : spdk_memory_domain_invalidate_data(tcp_req->req->payload.opts->memory_domain,
951 : 65912 : tcp_req->req->payload.opts->memory_domain_ctx, tcp_req->iov, tcp_req->iovcnt);
952 : : }
953 : :
954 : 14945749 : nvme_tcp_req_complete_safe(tcp_req);
955 : : }
956 : 14945749 : }
957 : :
958 : : static int
959 : 14946346 : nvme_tcp_qpair_capsule_cmd_send(struct nvme_tcp_qpair *tqpair,
960 : : struct nvme_tcp_req *tcp_req)
961 : : {
962 : : struct nvme_tcp_pdu *pdu;
963 : : struct spdk_nvme_tcp_cmd *capsule_cmd;
964 : 14946346 : uint32_t plen = 0, alignment;
965 : : uint8_t pdo;
966 : :
967 [ - + + + ]: 14946346 : SPDK_DEBUGLOG(nvme, "enter\n");
968 : 14946346 : pdu = tcp_req->pdu;
969 : 14946346 : pdu->req = tcp_req;
970 : :
971 : 14946346 : capsule_cmd = &pdu->hdr.capsule_cmd;
972 : 14946346 : capsule_cmd->common.pdu_type = SPDK_NVME_TCP_PDU_TYPE_CAPSULE_CMD;
973 : 14946346 : plen = capsule_cmd->common.hlen = sizeof(*capsule_cmd);
974 : 14946346 : capsule_cmd->ccsqe = tcp_req->req->cmd;
975 : :
976 [ - + + + ]: 14946346 : SPDK_DEBUGLOG(nvme, "capsule_cmd cid=%u on tqpair(%p)\n", tcp_req->req->cmd.cid, tqpair);
977 : :
978 [ + + ]: 14946346 : if (tqpair->flags.host_hdgst_enable) {
979 [ - + - + ]: 56716 : SPDK_DEBUGLOG(nvme, "Header digest is enabled for capsule command on tcp_req=%p\n",
980 : : tcp_req);
981 : 56716 : capsule_cmd->common.flags |= SPDK_NVME_TCP_CH_FLAGS_HDGSTF;
982 : 56716 : plen += SPDK_NVME_TCP_DIGEST_LEN;
983 : : }
984 : :
985 [ + + - + : 14946346 : if ((tcp_req->req->payload_size == 0) || !tcp_req->in_capsule_data) {
+ + ]
986 : 8616495 : goto end;
987 : : }
988 : :
989 : 6329851 : pdo = plen;
990 : 6329851 : pdu->padding_len = 0;
991 [ + + ]: 6329851 : if (tqpair->cpda) {
992 [ - + ]: 5 : alignment = (tqpair->cpda + 1) << 2;
993 [ + - ]: 5 : if (alignment > plen) {
994 : 5 : pdu->padding_len = alignment - plen;
995 : 5 : pdo = alignment;
996 : 5 : plen = alignment;
997 : : }
998 : : }
999 : :
1000 : 6329851 : capsule_cmd->common.pdo = pdo;
1001 : 6329851 : plen += tcp_req->req->payload_size;
1002 [ + + ]: 6329851 : if (tqpair->flags.host_ddgst_enable) {
1003 : 321567 : capsule_cmd->common.flags |= SPDK_NVME_TCP_CH_FLAGS_DDGSTF;
1004 : 321567 : plen += SPDK_NVME_TCP_DIGEST_LEN;
1005 : : }
1006 : :
1007 : 6329851 : tcp_req->datao = 0;
1008 : 6329851 : nvme_tcp_pdu_set_data_buf(pdu, tcp_req->iov, tcp_req->iovcnt,
1009 : 6329851 : 0, tcp_req->req->payload_size);
1010 : 14946346 : end:
1011 : 14946346 : capsule_cmd->common.plen = plen;
1012 : 14946346 : return nvme_tcp_qpair_write_pdu(tqpair, pdu, nvme_tcp_qpair_cmd_send_complete, tcp_req);
1013 : :
1014 : : }
1015 : :
1016 : : static int
1017 : 15330456 : nvme_tcp_qpair_submit_request(struct spdk_nvme_qpair *qpair,
1018 : : struct nvme_request *req)
1019 : : {
1020 : : struct nvme_tcp_qpair *tqpair;
1021 : : struct nvme_tcp_req *tcp_req;
1022 : :
1023 : 15330456 : tqpair = nvme_tcp_qpair(qpair);
1024 [ - + ]: 15330456 : assert(tqpair != NULL);
1025 [ - + ]: 15330456 : assert(req != NULL);
1026 : :
1027 : 15330456 : tcp_req = nvme_tcp_req_get(tqpair);
1028 [ + + ]: 15330456 : if (!tcp_req) {
1029 : 384120 : tqpair->stats->queued_requests++;
1030 : : /* Inform the upper layer to try again later. */
1031 : 384120 : return -EAGAIN;
1032 : : }
1033 : :
1034 [ + + ]: 14946336 : if (spdk_unlikely(nvme_tcp_req_init(tqpair, req, tcp_req))) {
1035 : 5 : SPDK_ERRLOG("nvme_tcp_req_init() failed\n");
1036 : 5 : nvme_tcp_req_put(tqpair, tcp_req);
1037 : 5 : return -1;
1038 : : }
1039 : :
1040 : 14946331 : tqpair->qpair.queue_depth++;
1041 [ + + + + ]: 14946331 : spdk_trace_record(TRACE_NVME_TCP_SUBMIT, qpair->id, 0, (uintptr_t)tcp_req->pdu, req->cb_arg,
1042 : : (uint32_t)req->cmd.cid, (uint32_t)req->cmd.opc,
1043 : : req->cmd.cdw10, req->cmd.cdw11, req->cmd.cdw12, tqpair->qpair.queue_depth);
1044 : 14946331 : TAILQ_INSERT_TAIL(&tqpair->outstanding_reqs, tcp_req, link);
1045 : 14946331 : return nvme_tcp_qpair_capsule_cmd_send(tqpair, tcp_req);
1046 : : }
1047 : :
1048 : : static int
1049 : 1266 : nvme_tcp_qpair_reset(struct spdk_nvme_qpair *qpair)
1050 : : {
1051 : 1266 : return 0;
1052 : : }
1053 : :
1054 : : static void
1055 : 14946371 : nvme_tcp_req_complete(struct nvme_tcp_req *tcp_req,
1056 : : struct nvme_tcp_qpair *tqpair,
1057 : : struct spdk_nvme_cpl *rsp,
1058 : : bool print_on_error)
1059 : : {
1060 : 66665 : struct spdk_nvme_cpl cpl;
1061 : : struct spdk_nvme_qpair *qpair;
1062 : : struct nvme_request *req;
1063 : : bool print_error;
1064 : :
1065 [ - + ]: 14946371 : assert(tcp_req->req != NULL);
1066 : 14946371 : req = tcp_req->req;
1067 : 14946371 : qpair = req->qpair;
1068 : :
1069 [ - + + + ]: 14946371 : SPDK_DEBUGLOG(nvme, "complete tcp_req(%p) on tqpair=%p\n", tcp_req, tqpair);
1070 : :
1071 [ + + ]: 14946371 : if (!tcp_req->tqpair->qpair.in_completion_context) {
1072 : 384826 : tcp_req->tqpair->async_complete++;
1073 : : }
1074 : :
1075 : : /* Cache arguments to be passed to nvme_complete_request since tcp_req can be zeroed when released */
1076 : 14946371 : memcpy(&cpl, rsp, sizeof(cpl));
1077 : :
1078 [ + + - + ]: 14946371 : if (spdk_unlikely(spdk_nvme_cpl_is_error(rsp))) {
1079 [ + + + + : 571071 : print_error = print_on_error && !qpair->ctrlr->opts.disable_error_logging;
+ + ]
1080 : :
1081 [ + + ]: 571071 : if (print_error) {
1082 : 556521 : spdk_nvme_qpair_print_command(qpair, &req->cmd);
1083 : : }
1084 : :
1085 [ + + + + ]: 571071 : if (print_error || SPDK_DEBUGLOG_FLAG_ENABLED("nvme")) {
1086 : 556545 : spdk_nvme_qpair_print_completion(qpair, rsp);
1087 : : }
1088 : : }
1089 : :
1090 : 14946371 : tqpair->qpair.queue_depth--;
1091 [ + + + + ]: 14946371 : spdk_trace_record(TRACE_NVME_TCP_COMPLETE, qpair->id, 0, (uintptr_t)tcp_req->pdu, req->cb_arg,
1092 : : (uint32_t)req->cmd.cid, (uint32_t)cpl.status_raw, tqpair->qpair.queue_depth);
1093 [ + + ]: 14946371 : TAILQ_REMOVE(&tcp_req->tqpair->outstanding_reqs, tcp_req, link);
1094 : 14946371 : nvme_tcp_req_put(tqpair, tcp_req);
1095 : 14946371 : nvme_complete_request(req->cb_fn, req->cb_arg, req->qpair, req, &cpl);
1096 : 14946371 : }
1097 : :
1098 : : static void
1099 : 19729 : nvme_tcp_qpair_abort_reqs(struct spdk_nvme_qpair *qpair, uint32_t dnr)
1100 : : {
1101 : : struct nvme_tcp_req *tcp_req, *tmp;
1102 : 19729 : struct spdk_nvme_cpl cpl = {};
1103 : 19729 : struct nvme_tcp_qpair *tqpair = nvme_tcp_qpair(qpair);
1104 : :
1105 : 19729 : cpl.sqid = qpair->id;
1106 : 19729 : cpl.status.sc = SPDK_NVME_SC_ABORTED_SQ_DELETION;
1107 : 19729 : cpl.status.sct = SPDK_NVME_SCT_GENERIC;
1108 : 19729 : cpl.status.dnr = dnr;
1109 : :
1110 [ + + ]: 23819 : TAILQ_FOREACH_SAFE(tcp_req, &tqpair->outstanding_reqs, link, tmp) {
1111 : : /* We cannot abort requests with accel operations in progress */
1112 [ + + ]: 4090 : if (tcp_req->ordering.bits.in_progress_accel) {
1113 : 10 : continue;
1114 : : }
1115 : :
1116 : 4080 : nvme_tcp_req_complete(tcp_req, tqpair, &cpl, true);
1117 : : }
1118 : 19729 : }
1119 : :
1120 : : static void
1121 : 7 : nvme_tcp_qpair_send_h2c_term_req_complete(void *cb_arg)
1122 : : {
1123 : 7 : struct nvme_tcp_qpair *tqpair = cb_arg;
1124 : :
1125 : 7 : tqpair->state = NVME_TCP_QPAIR_STATE_EXITING;
1126 : 7 : }
1127 : :
1128 : : static void
1129 : 82 : nvme_tcp_qpair_send_h2c_term_req(struct nvme_tcp_qpair *tqpair, struct nvme_tcp_pdu *pdu,
1130 : : enum spdk_nvme_tcp_term_req_fes fes, uint32_t error_offset)
1131 : : {
1132 : : struct nvme_tcp_pdu *rsp_pdu;
1133 : : struct spdk_nvme_tcp_term_req_hdr *h2c_term_req;
1134 : 82 : uint32_t h2c_term_req_hdr_len = sizeof(*h2c_term_req);
1135 : : uint8_t copy_len;
1136 : :
1137 : 82 : rsp_pdu = tqpair->send_pdu;
1138 [ - + ]: 82 : memset(rsp_pdu, 0, sizeof(*rsp_pdu));
1139 : 82 : h2c_term_req = &rsp_pdu->hdr.term_req;
1140 : 82 : h2c_term_req->common.pdu_type = SPDK_NVME_TCP_PDU_TYPE_H2C_TERM_REQ;
1141 : 82 : h2c_term_req->common.hlen = h2c_term_req_hdr_len;
1142 : :
1143 [ + + - + ]: 82 : if ((fes == SPDK_NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD) ||
1144 : : (fes == SPDK_NVME_TCP_TERM_REQ_FES_INVALID_DATA_UNSUPPORTED_PARAMETER)) {
1145 : 72 : DSET32(&h2c_term_req->fei, error_offset);
1146 : : }
1147 : :
1148 : 82 : copy_len = pdu->hdr.common.hlen;
1149 [ + + ]: 82 : if (copy_len > SPDK_NVME_TCP_TERM_REQ_ERROR_DATA_MAX_SIZE) {
1150 : 5 : copy_len = SPDK_NVME_TCP_TERM_REQ_ERROR_DATA_MAX_SIZE;
1151 : : }
1152 : :
1153 : : /* Copy the error info into the buffer */
1154 [ - + - + ]: 82 : memcpy((uint8_t *)rsp_pdu->hdr.raw + h2c_term_req_hdr_len, pdu->hdr.raw, copy_len);
1155 : 82 : nvme_tcp_pdu_set_data(rsp_pdu, (uint8_t *)rsp_pdu->hdr.raw + h2c_term_req_hdr_len, copy_len);
1156 : :
1157 : : /* Contain the header len of the wrong received pdu */
1158 : 82 : h2c_term_req->common.plen = h2c_term_req->common.hlen + copy_len;
1159 : 82 : nvme_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_QUIESCING);
1160 : 82 : nvme_tcp_qpair_write_pdu(tqpair, rsp_pdu, nvme_tcp_qpair_send_h2c_term_req_complete, tqpair);
1161 : 82 : }
1162 : :
1163 : : static bool
1164 : 22417966 : nvme_tcp_qpair_recv_state_valid(struct nvme_tcp_qpair *tqpair)
1165 : : {
1166 [ + + ]: 22417966 : switch (tqpair->state) {
1167 : 22417961 : case NVME_TCP_QPAIR_STATE_FABRIC_CONNECT_SEND:
1168 : : case NVME_TCP_QPAIR_STATE_FABRIC_CONNECT_POLL:
1169 : : case NVME_TCP_QPAIR_STATE_RUNNING:
1170 : 22417961 : return true;
1171 : 5 : default:
1172 : 5 : return false;
1173 : : }
1174 : : }
1175 : :
1176 : : static void
1177 : 22422244 : nvme_tcp_pdu_ch_handle(struct nvme_tcp_qpair *tqpair)
1178 : : {
1179 : : struct nvme_tcp_pdu *pdu;
1180 : 22422244 : uint32_t error_offset = 0;
1181 : : enum spdk_nvme_tcp_term_req_fes fes;
1182 : 22422244 : uint32_t expected_hlen, hd_len = 0;
1183 : 22422244 : bool plen_error = false;
1184 : :
1185 : 22422244 : pdu = tqpair->recv_pdu;
1186 : :
1187 [ - + + + ]: 22422244 : SPDK_DEBUGLOG(nvme, "pdu type = %d\n", pdu->hdr.common.pdu_type);
1188 [ + + ]: 22422244 : if (pdu->hdr.common.pdu_type == SPDK_NVME_TCP_PDU_TYPE_IC_RESP) {
1189 [ + + ]: 4278 : if (tqpair->state != NVME_TCP_QPAIR_STATE_INVALID) {
1190 : 5 : SPDK_ERRLOG("Already received IC_RESP PDU, and we should reject this pdu=%p\n", pdu);
1191 : 5 : fes = SPDK_NVME_TCP_TERM_REQ_FES_PDU_SEQUENCE_ERROR;
1192 : 5 : goto err;
1193 : : }
1194 : 4273 : expected_hlen = sizeof(struct spdk_nvme_tcp_ic_resp);
1195 [ + + ]: 4273 : if (pdu->hdr.common.plen != expected_hlen) {
1196 : 5 : plen_error = true;
1197 : : }
1198 : : } else {
1199 [ + + ]: 22417966 : if (spdk_unlikely(!nvme_tcp_qpair_recv_state_valid(tqpair))) {
1200 : 5 : SPDK_ERRLOG("The TCP/IP tqpair connection is not negotiated\n");
1201 : 5 : fes = SPDK_NVME_TCP_TERM_REQ_FES_PDU_SEQUENCE_ERROR;
1202 : 5 : goto err;
1203 : : }
1204 : :
1205 [ + + + + : 22417961 : switch (pdu->hdr.common.pdu_type) {
+ ]
1206 : 14449481 : case SPDK_NVME_TCP_PDU_TYPE_CAPSULE_RESP:
1207 : 14449481 : expected_hlen = sizeof(struct spdk_nvme_tcp_rsp);
1208 [ + + ]: 14449481 : if (pdu->hdr.common.flags & SPDK_NVME_TCP_CH_FLAGS_HDGSTF) {
1209 : 56687 : hd_len = SPDK_NVME_TCP_DIGEST_LEN;
1210 : : }
1211 : :
1212 [ + + ]: 14449481 : if (pdu->hdr.common.plen != (expected_hlen + hd_len)) {
1213 : 5 : plen_error = true;
1214 : : }
1215 : 14449481 : break;
1216 : 7439261 : case SPDK_NVME_TCP_PDU_TYPE_C2H_DATA:
1217 : 7439261 : expected_hlen = sizeof(struct spdk_nvme_tcp_c2h_data_hdr);
1218 [ + + ]: 7439261 : if (pdu->hdr.common.plen < pdu->hdr.common.pdo) {
1219 : 5 : plen_error = true;
1220 : : }
1221 : 7439261 : break;
1222 : 5 : case SPDK_NVME_TCP_PDU_TYPE_C2H_TERM_REQ:
1223 : 5 : expected_hlen = sizeof(struct spdk_nvme_tcp_term_req_hdr);
1224 [ - + ]: 5 : if ((pdu->hdr.common.plen <= expected_hlen) ||
1225 [ # # ]: 0 : (pdu->hdr.common.plen > SPDK_NVME_TCP_TERM_REQ_PDU_MAX_SIZE)) {
1226 : 5 : plen_error = true;
1227 : : }
1228 : 5 : break;
1229 : 529202 : case SPDK_NVME_TCP_PDU_TYPE_R2T:
1230 : 529202 : expected_hlen = sizeof(struct spdk_nvme_tcp_r2t_hdr);
1231 [ + + ]: 529202 : if (pdu->hdr.common.flags & SPDK_NVME_TCP_CH_FLAGS_HDGSTF) {
1232 : 5 : hd_len = SPDK_NVME_TCP_DIGEST_LEN;
1233 : : }
1234 : :
1235 [ + + ]: 529202 : if (pdu->hdr.common.plen != (expected_hlen + hd_len)) {
1236 : 5 : plen_error = true;
1237 : : }
1238 : 529202 : break;
1239 : :
1240 : 12 : default:
1241 : 12 : SPDK_ERRLOG("Unexpected PDU type 0x%02x\n", tqpair->recv_pdu->hdr.common.pdu_type);
1242 : 12 : fes = SPDK_NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD;
1243 : 12 : error_offset = offsetof(struct spdk_nvme_tcp_common_pdu_hdr, pdu_type);
1244 : 12 : goto err;
1245 : : }
1246 : : }
1247 : :
1248 [ + + ]: 22422222 : if (pdu->hdr.common.hlen != expected_hlen) {
1249 : 5 : SPDK_ERRLOG("Expected PDU header length %u, got %u\n",
1250 : : expected_hlen, pdu->hdr.common.hlen);
1251 : 5 : fes = SPDK_NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD;
1252 : 5 : error_offset = offsetof(struct spdk_nvme_tcp_common_pdu_hdr, hlen);
1253 : 5 : goto err;
1254 : :
1255 [ + + ]: 22422217 : } else if (plen_error) {
1256 : 25 : fes = SPDK_NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD;
1257 : 25 : error_offset = offsetof(struct spdk_nvme_tcp_common_pdu_hdr, plen);
1258 : 25 : goto err;
1259 : : } else {
1260 : 22422192 : nvme_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_PSH);
1261 : 22422192 : nvme_tcp_pdu_calc_psh_len(tqpair->recv_pdu, tqpair->flags.host_hdgst_enable);
1262 : 22422192 : return;
1263 : : }
1264 : 52 : err:
1265 : 52 : nvme_tcp_qpair_send_h2c_term_req(tqpair, pdu, fes, error_offset);
1266 : : }
1267 : :
1268 : : static struct nvme_tcp_req *
1269 : 22417939 : get_nvme_active_req_by_cid(struct nvme_tcp_qpair *tqpair, uint32_t cid)
1270 : : {
1271 [ - + ]: 22417939 : assert(tqpair != NULL);
1272 [ + + - + ]: 22417939 : if ((cid >= tqpair->num_entries) || (tqpair->tcp_reqs[cid].state == NVME_TCP_REQ_FREE)) {
1273 : 5 : return NULL;
1274 : : }
1275 : :
1276 : 22417934 : return &tqpair->tcp_reqs[cid];
1277 : : }
1278 : :
1279 : : static void
1280 : 377744 : nvme_tcp_recv_payload_seq_cb(void *cb_arg, int status)
1281 : : {
1282 : 377744 : struct nvme_tcp_req *treq = cb_arg;
1283 : 377744 : struct nvme_request *req = treq->req;
1284 : 377744 : struct nvme_tcp_qpair *tqpair = treq->tqpair;
1285 : :
1286 [ - + ]: 377744 : assert(treq->ordering.bits.in_progress_accel);
1287 : 377744 : treq->ordering.bits.in_progress_accel = 0;
1288 : :
1289 : 377744 : nvme_tcp_cond_schedule_qpair_polling(tqpair);
1290 : :
1291 : 377744 : req->accel_sequence = NULL;
1292 [ - + ]: 377744 : if (spdk_unlikely(status != 0)) {
1293 : 0 : pdu_seq_fail(treq->pdu, status);
1294 : 0 : return;
1295 : : }
1296 : :
1297 : 377744 : nvme_tcp_req_complete_safe(treq);
1298 : : }
1299 : :
1300 : : static void
1301 : 7439272 : nvme_tcp_c2h_data_payload_handle(struct nvme_tcp_qpair *tqpair,
1302 : : struct nvme_tcp_pdu *pdu, uint32_t *reaped)
1303 : : {
1304 : : struct nvme_tcp_req *tcp_req;
1305 : : struct nvme_tcp_poll_group *tgroup;
1306 : : struct spdk_nvme_tcp_c2h_data_hdr *c2h_data;
1307 : : uint8_t flags;
1308 : :
1309 : 7439272 : tcp_req = pdu->req;
1310 [ - + ]: 7439272 : assert(tcp_req != NULL);
1311 : :
1312 [ - + + + ]: 7439272 : SPDK_DEBUGLOG(nvme, "enter\n");
1313 : 7439272 : c2h_data = &pdu->hdr.c2h_data;
1314 : 7439272 : tcp_req->datao += pdu->data_len;
1315 : 7439272 : flags = c2h_data->common.flags;
1316 : :
1317 [ + + ]: 7439272 : if (flags & SPDK_NVME_TCP_C2H_DATA_FLAGS_LAST_PDU) {
1318 [ + + ]: 7192484 : if (tcp_req->datao == tcp_req->req->payload_size) {
1319 : 7192006 : tcp_req->rsp.status.p = 0;
1320 : : } else {
1321 : 478 : tcp_req->rsp.status.p = 1;
1322 : : }
1323 : :
1324 : 7192484 : tcp_req->rsp.cid = tcp_req->cid;
1325 : 7192484 : tcp_req->rsp.sqid = tqpair->qpair.id;
1326 [ + + ]: 7192484 : if (flags & SPDK_NVME_TCP_C2H_DATA_FLAGS_SUCCESS) {
1327 : 487913 : tcp_req->ordering.bits.data_recv = 1;
1328 [ + + ]: 487913 : if (tcp_req->req->accel_sequence != NULL) {
1329 : 78917 : tgroup = nvme_tcp_poll_group(tqpair->qpair.poll_group);
1330 : 78917 : nvme_tcp_accel_reverse_sequence(tgroup, tcp_req->req->accel_sequence);
1331 : 78917 : nvme_tcp_accel_finish_sequence(tgroup, tcp_req,
1332 : 78917 : tcp_req->req->accel_sequence,
1333 : : nvme_tcp_recv_payload_seq_cb,
1334 : : tcp_req);
1335 : 78917 : return;
1336 : : }
1337 : :
1338 [ + - ]: 408996 : if (nvme_tcp_req_complete_safe(tcp_req)) {
1339 : 408996 : (*reaped)++;
1340 : : }
1341 : : }
1342 : : }
1343 : : }
1344 : :
1345 : : static const char *spdk_nvme_tcp_term_req_fes_str[] = {
1346 : : "Invalid PDU Header Field",
1347 : : "PDU Sequence Error",
1348 : : "Header Digest Error",
1349 : : "Data Transfer Out of Range",
1350 : : "Data Transfer Limit Exceeded",
1351 : : "Unsupported parameter",
1352 : : };
1353 : :
1354 : : static void
1355 : 10 : nvme_tcp_c2h_term_req_dump(struct spdk_nvme_tcp_term_req_hdr *c2h_term_req)
1356 : : {
1357 : 10 : SPDK_ERRLOG("Error info of pdu(%p): %s\n", c2h_term_req,
1358 : : spdk_nvme_tcp_term_req_fes_str[c2h_term_req->fes]);
1359 [ - + ]: 10 : if ((c2h_term_req->fes == SPDK_NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD) ||
1360 [ # # ]: 0 : (c2h_term_req->fes == SPDK_NVME_TCP_TERM_REQ_FES_INVALID_DATA_UNSUPPORTED_PARAMETER)) {
1361 [ - + - + ]: 10 : SPDK_DEBUGLOG(nvme, "The offset from the start of the PDU header is %u\n",
1362 : : DGET32(c2h_term_req->fei));
1363 : : }
1364 : : /* we may also need to dump some other info here */
1365 : 10 : }
1366 : :
1367 : : static void
1368 : 10 : nvme_tcp_c2h_term_req_payload_handle(struct nvme_tcp_qpair *tqpair,
1369 : : struct nvme_tcp_pdu *pdu)
1370 : : {
1371 : 10 : nvme_tcp_c2h_term_req_dump(&pdu->hdr.term_req);
1372 : 10 : nvme_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_QUIESCING);
1373 : 10 : }
1374 : :
1375 : : static void
1376 : 7061518 : _nvme_tcp_pdu_payload_handle(struct nvme_tcp_qpair *tqpair, uint32_t *reaped)
1377 : : {
1378 : : struct nvme_tcp_pdu *pdu;
1379 : :
1380 [ - + ]: 7061518 : assert(tqpair != NULL);
1381 : 7061518 : pdu = tqpair->recv_pdu;
1382 : :
1383 [ + + - ]: 7061518 : switch (pdu->hdr.common.pdu_type) {
1384 : 7061513 : case SPDK_NVME_TCP_PDU_TYPE_C2H_DATA:
1385 : 7061513 : nvme_tcp_c2h_data_payload_handle(tqpair, pdu, reaped);
1386 : 7061513 : nvme_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_READY);
1387 : 7061513 : break;
1388 : :
1389 : 5 : case SPDK_NVME_TCP_PDU_TYPE_C2H_TERM_REQ:
1390 : 5 : nvme_tcp_c2h_term_req_payload_handle(tqpair, pdu);
1391 : 5 : break;
1392 : :
1393 : 0 : default:
1394 : : /* The code should not go to here */
1395 : 0 : SPDK_ERRLOG("The code should not go to here\n");
1396 : 0 : break;
1397 : : }
1398 : 7061518 : }
1399 : :
1400 : : static void
1401 : 0 : nvme_tcp_accel_recv_compute_crc32_done(void *cb_arg, int status)
1402 : : {
1403 : 0 : struct nvme_tcp_req *tcp_req = cb_arg;
1404 : : struct nvme_tcp_pdu *pdu;
1405 : : struct nvme_tcp_qpair *tqpair;
1406 : : int rc;
1407 : 0 : int dummy_reaped = 0;
1408 : :
1409 : 0 : pdu = tcp_req->pdu;
1410 [ # # ]: 0 : assert(pdu != NULL);
1411 : :
1412 : 0 : tqpair = tcp_req->tqpair;
1413 [ # # ]: 0 : assert(tqpair != NULL);
1414 : :
1415 [ # # ]: 0 : assert(tcp_req->ordering.bits.in_progress_accel);
1416 : 0 : tcp_req->ordering.bits.in_progress_accel = 0;
1417 : :
1418 : 0 : nvme_tcp_cond_schedule_qpair_polling(tqpair);
1419 : :
1420 [ # # ]: 0 : if (spdk_unlikely(status)) {
1421 : 0 : SPDK_ERRLOG("Failed to compute the data digest for pdu =%p\n", pdu);
1422 : 0 : tcp_req->rsp.status.sc = SPDK_NVME_SC_COMMAND_TRANSIENT_TRANSPORT_ERROR;
1423 : 0 : goto end;
1424 : : }
1425 : :
1426 : 0 : pdu->data_digest_crc32 ^= SPDK_CRC32C_XOR;
1427 : 0 : rc = MATCH_DIGEST_WORD(pdu->data_digest, pdu->data_digest_crc32);
1428 [ # # ]: 0 : if (rc == 0) {
1429 : 0 : SPDK_ERRLOG("data digest error on tqpair=(%p) with pdu=%p\n", tqpair, pdu);
1430 : 0 : tcp_req->rsp.status.sc = SPDK_NVME_SC_COMMAND_TRANSIENT_TRANSPORT_ERROR;
1431 : : }
1432 : :
1433 : 0 : end:
1434 : 0 : nvme_tcp_c2h_data_payload_handle(tqpair, tcp_req->pdu, &dummy_reaped);
1435 : 0 : }
1436 : :
1437 : : static void
1438 : 377744 : nvme_tcp_req_copy_pdu(struct nvme_tcp_req *treq, struct nvme_tcp_pdu *pdu)
1439 : : {
1440 : 377744 : treq->pdu->hdr = pdu->hdr;
1441 : 377744 : treq->pdu->req = treq;
1442 : 377744 : memcpy(treq->pdu->data_digest, pdu->data_digest, sizeof(pdu->data_digest));
1443 [ - + - + ]: 377744 : memcpy(treq->pdu->data_iov, pdu->data_iov, sizeof(pdu->data_iov[0]) * pdu->data_iovcnt);
1444 : 377744 : treq->pdu->data_iovcnt = pdu->data_iovcnt;
1445 : 377744 : treq->pdu->data_len = pdu->data_len;
1446 : 377744 : }
1447 : :
1448 : : static void
1449 : 377744 : nvme_tcp_accel_seq_recv_compute_crc32_done(void *cb_arg)
1450 : : {
1451 : 377744 : struct nvme_tcp_req *treq = cb_arg;
1452 : 377744 : struct nvme_tcp_qpair *tqpair = treq->tqpair;
1453 : 377744 : struct nvme_tcp_pdu *pdu = treq->pdu;
1454 : : bool result;
1455 : :
1456 : 377744 : pdu->data_digest_crc32 ^= SPDK_CRC32C_XOR;
1457 : 377744 : result = MATCH_DIGEST_WORD(pdu->data_digest, pdu->data_digest_crc32);
1458 [ + + ]: 377744 : if (spdk_unlikely(!result)) {
1459 : 1666 : SPDK_ERRLOG("data digest error on tqpair=(%p)\n", tqpair);
1460 : 1666 : treq->rsp.status.sc = SPDK_NVME_SC_COMMAND_TRANSIENT_TRANSPORT_ERROR;
1461 : : }
1462 : 377744 : }
1463 : :
1464 : : static bool
1465 : 573312 : nvme_tcp_accel_recv_compute_crc32(struct nvme_tcp_req *treq, struct nvme_tcp_pdu *pdu)
1466 : : {
1467 : 573312 : struct nvme_tcp_qpair *tqpair = treq->tqpair;
1468 : 573312 : struct nvme_tcp_poll_group *tgroup = nvme_tcp_poll_group(tqpair->qpair.poll_group);
1469 : 573312 : struct nvme_request *req = treq->req;
1470 : 573312 : int rc, dummy = 0;
1471 : :
1472 : : /* Only support this limited case that the request has only one c2h pdu */
1473 [ + - + + : 573312 : if (spdk_unlikely(nvme_qpair_get_state(&tqpair->qpair) < NVME_QPAIR_CONNECTED ||
+ + - + +
+ - + + +
+ + ]
1474 : : tqpair->qpair.poll_group == NULL || pdu->dif_ctx != NULL ||
1475 : : pdu->data_len % SPDK_NVME_TCP_DIGEST_ALIGNMENT != 0 ||
1476 : : pdu->data_len != req->payload_size)) {
1477 : 177938 : return false;
1478 : : }
1479 : :
1480 [ + + ]: 395374 : if (tgroup->group.group->accel_fn_table.append_crc32c != NULL) {
1481 : 377744 : nvme_tcp_req_copy_pdu(treq, pdu);
1482 : 755488 : rc = nvme_tcp_accel_append_crc32c(tgroup, &req->accel_sequence,
1483 : 377744 : &treq->pdu->data_digest_crc32,
1484 : 755488 : treq->pdu->data_iov, treq->pdu->data_iovcnt, 0,
1485 : : nvme_tcp_accel_seq_recv_compute_crc32_done, treq);
1486 [ - + ]: 377744 : if (spdk_unlikely(rc != 0)) {
1487 : : /* If accel is out of resources, fall back to non-accelerated crc32 */
1488 [ # # ]: 0 : if (rc == -ENOMEM) {
1489 : 0 : return false;
1490 : : }
1491 : :
1492 : 0 : SPDK_ERRLOG("Failed to append crc32c operation: %d\n", rc);
1493 : 0 : treq->rsp.status.sc = SPDK_NVME_SC_COMMAND_TRANSIENT_TRANSPORT_ERROR;
1494 : : }
1495 : :
1496 : 377744 : nvme_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_READY);
1497 : 377744 : nvme_tcp_c2h_data_payload_handle(tqpair, treq->pdu, &dummy);
1498 : 377744 : return true;
1499 [ - + ]: 17630 : } else if (tgroup->group.group->accel_fn_table.submit_accel_crc32c != NULL) {
1500 : 0 : nvme_tcp_req_copy_pdu(treq, pdu);
1501 : 0 : nvme_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_READY);
1502 : 0 : nvme_tcp_accel_submit_crc32c(tgroup, treq, &treq->pdu->data_digest_crc32,
1503 : 0 : treq->pdu->data_iov, treq->pdu->data_iovcnt, 0,
1504 : : nvme_tcp_accel_recv_compute_crc32_done, treq);
1505 : 0 : return true;
1506 : : }
1507 : :
1508 : 17630 : return false;
1509 : : }
1510 : :
1511 : : static void
1512 : 7439262 : nvme_tcp_pdu_payload_handle(struct nvme_tcp_qpair *tqpair,
1513 : : uint32_t *reaped)
1514 : : {
1515 : 7439262 : int rc = 0;
1516 : 7439262 : struct nvme_tcp_pdu *pdu = tqpair->recv_pdu;
1517 : : uint32_t crc32c;
1518 : 7439262 : struct nvme_tcp_req *tcp_req = pdu->req;
1519 : :
1520 [ - + ]: 7439262 : assert(tqpair->recv_state == NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_PAYLOAD);
1521 [ - + + + ]: 7439262 : SPDK_DEBUGLOG(nvme, "enter\n");
1522 : :
1523 : : /* The request can be NULL, e.g. in case of C2HTermReq */
1524 [ + - ]: 7439262 : if (spdk_likely(tcp_req != NULL)) {
1525 : 7439262 : tcp_req->expected_datao += pdu->data_len;
1526 : : }
1527 : :
1528 : : /* check data digest if need */
1529 [ - + + + ]: 7439262 : if (pdu->ddgst_enable) {
1530 : : /* But if the data digest is enabled, tcp_req cannot be NULL */
1531 [ - + ]: 573312 : assert(tcp_req != NULL);
1532 [ + + ]: 573312 : if (nvme_tcp_accel_recv_compute_crc32(tcp_req, pdu)) {
1533 : 377744 : return;
1534 : : }
1535 : :
1536 : 195568 : crc32c = nvme_tcp_pdu_calc_data_digest(pdu);
1537 : 195568 : crc32c = crc32c ^ SPDK_CRC32C_XOR;
1538 : 195568 : rc = MATCH_DIGEST_WORD(pdu->data_digest, crc32c);
1539 [ - + ]: 195568 : if (rc == 0) {
1540 : 0 : SPDK_ERRLOG("data digest error on tqpair=(%p) with pdu=%p\n", tqpair, pdu);
1541 : 0 : tcp_req = pdu->req;
1542 [ # # ]: 0 : assert(tcp_req != NULL);
1543 : 0 : tcp_req->rsp.status.sc = SPDK_NVME_SC_COMMAND_TRANSIENT_TRANSPORT_ERROR;
1544 : : }
1545 : : }
1546 : :
1547 : 7061518 : _nvme_tcp_pdu_payload_handle(tqpair, reaped);
1548 : : }
1549 : :
1550 : : static void
1551 : 4256 : nvme_tcp_send_icreq_complete(void *cb_arg)
1552 : : {
1553 : 4256 : struct nvme_tcp_qpair *tqpair = cb_arg;
1554 : :
1555 [ - + + + ]: 4256 : SPDK_DEBUGLOG(nvme, "Complete the icreq send for tqpair=%p %u\n", tqpair, tqpair->qpair.id);
1556 : :
1557 : 4256 : tqpair->flags.icreq_send_ack = true;
1558 : :
1559 [ - + ]: 4256 : if (tqpair->state == NVME_TCP_QPAIR_STATE_INITIALIZING) {
1560 [ # # # # ]: 0 : SPDK_DEBUGLOG(nvme, "tqpair %p %u, finalize icresp\n", tqpair, tqpair->qpair.id);
1561 : 0 : tqpair->state = NVME_TCP_QPAIR_STATE_FABRIC_CONNECT_SEND;
1562 : : }
1563 : 4256 : }
1564 : :
1565 : : static void
1566 : 4283 : nvme_tcp_icresp_handle(struct nvme_tcp_qpair *tqpair,
1567 : : struct nvme_tcp_pdu *pdu)
1568 : : {
1569 : 4283 : struct spdk_nvme_tcp_ic_resp *ic_resp = &pdu->hdr.ic_resp;
1570 : 4283 : uint32_t error_offset = 0;
1571 : : enum spdk_nvme_tcp_term_req_fes fes;
1572 : : int recv_buf_size;
1573 : :
1574 : : /* Only PFV 0 is defined currently */
1575 [ + + ]: 4283 : if (ic_resp->pfv != 0) {
1576 : 5 : SPDK_ERRLOG("Expected ICResp PFV %u, got %u\n", 0u, ic_resp->pfv);
1577 : 5 : fes = SPDK_NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD;
1578 : 5 : error_offset = offsetof(struct spdk_nvme_tcp_ic_resp, pfv);
1579 : 5 : goto end;
1580 : : }
1581 : :
1582 [ + + ]: 4278 : if (ic_resp->maxh2cdata < NVME_TCP_PDU_H2C_MIN_DATA_SIZE) {
1583 : 5 : SPDK_ERRLOG("Expected ICResp maxh2cdata >=%u, got %u\n", NVME_TCP_PDU_H2C_MIN_DATA_SIZE,
1584 : : ic_resp->maxh2cdata);
1585 : 5 : fes = SPDK_NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD;
1586 : 5 : error_offset = offsetof(struct spdk_nvme_tcp_ic_resp, maxh2cdata);
1587 : 5 : goto end;
1588 : : }
1589 : 4273 : tqpair->maxh2cdata = ic_resp->maxh2cdata;
1590 : :
1591 [ + + ]: 4273 : if (ic_resp->cpda > SPDK_NVME_TCP_CPDA_MAX) {
1592 : 5 : SPDK_ERRLOG("Expected ICResp cpda <=%u, got %u\n", SPDK_NVME_TCP_CPDA_MAX, ic_resp->cpda);
1593 : 5 : fes = SPDK_NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD;
1594 : 5 : error_offset = offsetof(struct spdk_nvme_tcp_ic_resp, cpda);
1595 : 5 : goto end;
1596 : : }
1597 : 4268 : tqpair->cpda = ic_resp->cpda;
1598 : :
1599 : 4268 : tqpair->flags.host_hdgst_enable = ic_resp->dgst.bits.hdgst_enable ? true : false;
1600 : 4268 : tqpair->flags.host_ddgst_enable = ic_resp->dgst.bits.ddgst_enable ? true : false;
1601 [ - + + + ]: 4268 : SPDK_DEBUGLOG(nvme, "host_hdgst_enable: %u\n", tqpair->flags.host_hdgst_enable);
1602 [ - + + + ]: 4268 : SPDK_DEBUGLOG(nvme, "host_ddgst_enable: %u\n", tqpair->flags.host_ddgst_enable);
1603 : :
1604 : : /* Now that we know whether digests are enabled, properly size the receive buffer to
1605 : : * handle several incoming 4K read commands according to SPDK_NVMF_TCP_RECV_BUF_SIZE_FACTOR
1606 : : * parameter. */
1607 : 4268 : recv_buf_size = 0x1000 + sizeof(struct spdk_nvme_tcp_c2h_data_hdr);
1608 : :
1609 [ + + ]: 4268 : if (tqpair->flags.host_hdgst_enable) {
1610 : 34 : recv_buf_size += SPDK_NVME_TCP_DIGEST_LEN;
1611 : : }
1612 : :
1613 [ + + ]: 4268 : if (tqpair->flags.host_ddgst_enable) {
1614 : 110 : recv_buf_size += SPDK_NVME_TCP_DIGEST_LEN;
1615 : : }
1616 : :
1617 [ - + ]: 4268 : if (spdk_sock_set_recvbuf(tqpair->sock, recv_buf_size * SPDK_NVMF_TCP_RECV_BUF_SIZE_FACTOR) < 0) {
1618 : 0 : SPDK_WARNLOG("Unable to allocate enough memory for receive buffer on tqpair=%p with size=%d\n",
1619 : : tqpair,
1620 : : recv_buf_size);
1621 : : /* Not fatal. */
1622 : : }
1623 : :
1624 : 4268 : nvme_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_READY);
1625 : :
1626 [ + + ]: 4268 : if (!tqpair->flags.icreq_send_ack) {
1627 : 5 : tqpair->state = NVME_TCP_QPAIR_STATE_INITIALIZING;
1628 [ - + - + ]: 5 : SPDK_DEBUGLOG(nvme, "tqpair %p %u, waiting icreq ack\n", tqpair, tqpair->qpair.id);
1629 : 5 : return;
1630 : : }
1631 : :
1632 : 4263 : tqpair->state = NVME_TCP_QPAIR_STATE_FABRIC_CONNECT_SEND;
1633 : 4263 : return;
1634 : 15 : end:
1635 : 15 : nvme_tcp_qpair_send_h2c_term_req(tqpair, pdu, fes, error_offset);
1636 : : }
1637 : :
1638 : : static void
1639 : 14449486 : nvme_tcp_capsule_resp_hdr_handle(struct nvme_tcp_qpair *tqpair, struct nvme_tcp_pdu *pdu,
1640 : : uint32_t *reaped)
1641 : : {
1642 : : struct nvme_tcp_req *tcp_req;
1643 : : struct nvme_tcp_poll_group *tgroup;
1644 : 14449486 : struct spdk_nvme_tcp_rsp *capsule_resp = &pdu->hdr.capsule_resp;
1645 : 14449486 : uint32_t cid, error_offset = 0;
1646 : : enum spdk_nvme_tcp_term_req_fes fes;
1647 : :
1648 [ - + + + ]: 14449486 : SPDK_DEBUGLOG(nvme, "enter\n");
1649 : 14449486 : cid = capsule_resp->rccqe.cid;
1650 : 14449486 : tcp_req = get_nvme_active_req_by_cid(tqpair, cid);
1651 : :
1652 [ + + ]: 14449486 : if (!tcp_req) {
1653 : 5 : SPDK_ERRLOG("no tcp_req is found with cid=%u for tqpair=%p\n", cid, tqpair);
1654 : 5 : fes = SPDK_NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD;
1655 : 5 : error_offset = offsetof(struct spdk_nvme_tcp_rsp, rccqe);
1656 : 5 : goto end;
1657 : : }
1658 : :
1659 [ - + ]: 14449481 : assert(tcp_req->req != NULL);
1660 : :
1661 : 14449481 : tcp_req->rsp = capsule_resp->rccqe;
1662 : 14449481 : tcp_req->ordering.bits.data_recv = 1;
1663 : :
1664 : : /* Recv the pdu again */
1665 : 14449481 : nvme_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_READY);
1666 : :
1667 [ + + ]: 14449481 : if (tcp_req->req->accel_sequence != NULL) {
1668 : 298827 : tgroup = nvme_tcp_poll_group(tqpair->qpair.poll_group);
1669 : 298827 : nvme_tcp_accel_reverse_sequence(tgroup, tcp_req->req->accel_sequence);
1670 : 298827 : nvme_tcp_accel_finish_sequence(tgroup, tcp_req, tcp_req->req->accel_sequence,
1671 : : nvme_tcp_recv_payload_seq_cb, tcp_req);
1672 : 298827 : return;
1673 : : }
1674 : :
1675 [ + - ]: 14150654 : if (nvme_tcp_req_complete_safe(tcp_req)) {
1676 : 14150654 : (*reaped)++;
1677 : : }
1678 : :
1679 : 14150654 : return;
1680 : :
1681 : 5 : end:
1682 : 5 : nvme_tcp_qpair_send_h2c_term_req(tqpair, pdu, fes, error_offset);
1683 : : }
1684 : :
1685 : : static void
1686 : 0 : nvme_tcp_c2h_term_req_hdr_handle(struct nvme_tcp_qpair *tqpair,
1687 : : struct nvme_tcp_pdu *pdu)
1688 : : {
1689 : 0 : struct spdk_nvme_tcp_term_req_hdr *c2h_term_req = &pdu->hdr.term_req;
1690 : 0 : uint32_t error_offset = 0;
1691 : : enum spdk_nvme_tcp_term_req_fes fes;
1692 : :
1693 [ # # ]: 0 : if (c2h_term_req->fes > SPDK_NVME_TCP_TERM_REQ_FES_INVALID_DATA_UNSUPPORTED_PARAMETER) {
1694 : 0 : SPDK_ERRLOG("Fatal Error Status(FES) is unknown for c2h_term_req pdu=%p\n", pdu);
1695 : 0 : fes = SPDK_NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD;
1696 : 0 : error_offset = offsetof(struct spdk_nvme_tcp_term_req_hdr, fes);
1697 : 0 : goto end;
1698 : : }
1699 : :
1700 : : /* set the data buffer */
1701 : 0 : nvme_tcp_pdu_set_data(pdu, (uint8_t *)pdu->hdr.raw + c2h_term_req->common.hlen,
1702 : 0 : c2h_term_req->common.plen - c2h_term_req->common.hlen);
1703 : 0 : nvme_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_PAYLOAD);
1704 : 0 : return;
1705 : 0 : end:
1706 : 0 : nvme_tcp_qpair_send_h2c_term_req(tqpair, pdu, fes, error_offset);
1707 : : }
1708 : :
1709 : : static void
1710 : 7439256 : nvme_tcp_c2h_data_hdr_handle(struct nvme_tcp_qpair *tqpair, struct nvme_tcp_pdu *pdu)
1711 : : {
1712 : : struct nvme_tcp_req *tcp_req;
1713 : 7439256 : struct spdk_nvme_tcp_c2h_data_hdr *c2h_data = &pdu->hdr.c2h_data;
1714 : 7439256 : uint32_t error_offset = 0;
1715 : : enum spdk_nvme_tcp_term_req_fes fes;
1716 : 7439256 : int flags = c2h_data->common.flags;
1717 : : int rc;
1718 : :
1719 [ - + + + ]: 7439256 : SPDK_DEBUGLOG(nvme, "enter\n");
1720 [ - + + + ]: 7439256 : SPDK_DEBUGLOG(nvme, "c2h_data info on tqpair(%p): datao=%u, datal=%u, cccid=%d\n",
1721 : : tqpair, c2h_data->datao, c2h_data->datal, c2h_data->cccid);
1722 : 7439256 : tcp_req = get_nvme_active_req_by_cid(tqpair, c2h_data->cccid);
1723 [ - + ]: 7439256 : if (!tcp_req) {
1724 : 0 : SPDK_ERRLOG("no tcp_req found for c2hdata cid=%d\n", c2h_data->cccid);
1725 : 0 : fes = SPDK_NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD;
1726 : 0 : error_offset = offsetof(struct spdk_nvme_tcp_c2h_data_hdr, cccid);
1727 : 0 : goto end;
1728 : :
1729 : : }
1730 : :
1731 [ - + + + ]: 7439256 : SPDK_DEBUGLOG(nvme, "tcp_req(%p) on tqpair(%p): expected_datao=%u, payload_size=%u\n",
1732 : : tcp_req, tqpair, tcp_req->expected_datao, tcp_req->req->payload_size);
1733 : :
1734 [ + + - + ]: 7439256 : if (spdk_unlikely((flags & SPDK_NVME_TCP_C2H_DATA_FLAGS_SUCCESS) &&
1735 : : !(flags & SPDK_NVME_TCP_C2H_DATA_FLAGS_LAST_PDU))) {
1736 : 0 : SPDK_ERRLOG("Invalid flag flags=%d in c2h_data=%p\n", flags, c2h_data);
1737 : 0 : fes = SPDK_NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD;
1738 : 0 : error_offset = offsetof(struct spdk_nvme_tcp_c2h_data_hdr, common);
1739 : 0 : goto end;
1740 : : }
1741 : :
1742 [ - + ]: 7439256 : if (c2h_data->datal > tcp_req->req->payload_size) {
1743 : 0 : SPDK_ERRLOG("Invalid datal for tcp_req(%p), datal(%u) exceeds payload_size(%u)\n",
1744 : : tcp_req, c2h_data->datal, tcp_req->req->payload_size);
1745 : 0 : fes = SPDK_NVME_TCP_TERM_REQ_FES_DATA_TRANSFER_OUT_OF_RANGE;
1746 : 0 : goto end;
1747 : : }
1748 : :
1749 [ - + ]: 7439256 : if (tcp_req->expected_datao != c2h_data->datao) {
1750 : 0 : SPDK_ERRLOG("Invalid datao for tcp_req(%p), received datal(%u) != expected datao(%u) in tcp_req\n",
1751 : : tcp_req, c2h_data->datao, tcp_req->expected_datao);
1752 : 0 : fes = SPDK_NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD;
1753 : 0 : error_offset = offsetof(struct spdk_nvme_tcp_c2h_data_hdr, datao);
1754 : 0 : goto end;
1755 : : }
1756 : :
1757 [ - + ]: 7439256 : if ((c2h_data->datao + c2h_data->datal) > tcp_req->req->payload_size) {
1758 : 0 : SPDK_ERRLOG("Invalid data range for tcp_req(%p), received (datao(%u) + datal(%u)) > datao(%u) in tcp_req\n",
1759 : : tcp_req, c2h_data->datao, c2h_data->datal, tcp_req->req->payload_size);
1760 : 0 : fes = SPDK_NVME_TCP_TERM_REQ_FES_DATA_TRANSFER_OUT_OF_RANGE;
1761 : 0 : error_offset = offsetof(struct spdk_nvme_tcp_c2h_data_hdr, datal);
1762 : 0 : goto end;
1763 : :
1764 : : }
1765 : :
1766 [ + + ]: 7439256 : if (nvme_payload_type(&tcp_req->req->payload) == NVME_PAYLOAD_TYPE_CONTIG) {
1767 : 7328397 : rc = nvme_tcp_build_contig_request(tqpair, tcp_req);
1768 : : } else {
1769 [ - + ]: 110859 : assert(nvme_payload_type(&tcp_req->req->payload) == NVME_PAYLOAD_TYPE_SGL);
1770 : 110859 : rc = nvme_tcp_build_sgl_request(tqpair, tcp_req);
1771 : : }
1772 : :
1773 [ - + ]: 7439256 : if (rc) {
1774 : : /* Not the right error message but at least it handles the failure. */
1775 : 0 : fes = SPDK_NVME_TCP_TERM_REQ_FES_DATA_TRANSFER_LIMIT_EXCEEDED;
1776 : 0 : goto end;
1777 : : }
1778 : :
1779 : 7439256 : nvme_tcp_pdu_set_data_buf(pdu, tcp_req->iov, tcp_req->iovcnt,
1780 : : c2h_data->datao, c2h_data->datal);
1781 : 7439256 : pdu->req = tcp_req;
1782 : :
1783 : 7439256 : nvme_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_PAYLOAD);
1784 : 7439256 : return;
1785 : :
1786 : 0 : end:
1787 : 0 : nvme_tcp_qpair_send_h2c_term_req(tqpair, pdu, fes, error_offset);
1788 : : }
1789 : :
1790 : : static void
1791 : 529151 : nvme_tcp_qpair_h2c_data_send_complete(void *cb_arg)
1792 : : {
1793 : 529151 : struct nvme_tcp_req *tcp_req = cb_arg;
1794 : :
1795 [ - + ]: 529151 : assert(tcp_req != NULL);
1796 : :
1797 : 529151 : tcp_req->ordering.bits.send_ack = 1;
1798 [ - + ]: 529151 : if (tcp_req->r2tl_remain) {
1799 : 0 : nvme_tcp_send_h2c_data(tcp_req);
1800 : : } else {
1801 [ - + ]: 529151 : assert(tcp_req->active_r2ts > 0);
1802 : 529151 : tcp_req->active_r2ts--;
1803 : 529151 : tcp_req->state = NVME_TCP_REQ_ACTIVE;
1804 : :
1805 [ - + ]: 529151 : if (tcp_req->ordering.bits.r2t_waiting_h2c_complete) {
1806 : 0 : tcp_req->ordering.bits.r2t_waiting_h2c_complete = 0;
1807 [ # # # # ]: 0 : SPDK_DEBUGLOG(nvme, "tcp_req %p: continue r2t\n", tcp_req);
1808 [ # # ]: 0 : assert(tcp_req->active_r2ts > 0);
1809 : 0 : tcp_req->ttag = tcp_req->ttag_r2t_next;
1810 : 0 : tcp_req->r2tl_remain = tcp_req->r2tl_remain_next;
1811 : 0 : tcp_req->state = NVME_TCP_REQ_ACTIVE_R2T;
1812 : 0 : nvme_tcp_send_h2c_data(tcp_req);
1813 : 0 : return;
1814 : : }
1815 : :
1816 [ + + ]: 529151 : if (tcp_req->ordering.bits.domain_in_use) {
1817 : 26010 : spdk_memory_domain_invalidate_data(tcp_req->req->payload.opts->memory_domain,
1818 : 13005 : tcp_req->req->payload.opts->memory_domain_ctx, tcp_req->iov, tcp_req->iovcnt);
1819 : : }
1820 : :
1821 : : /* Need also call this function to free the resource */
1822 : 529151 : nvme_tcp_req_complete_safe(tcp_req);
1823 : : }
1824 : : }
1825 : :
1826 : : static void
1827 : 529197 : nvme_tcp_send_h2c_data(struct nvme_tcp_req *tcp_req)
1828 : : {
1829 : 529197 : struct nvme_tcp_qpair *tqpair = nvme_tcp_qpair(tcp_req->req->qpair);
1830 : : struct nvme_tcp_pdu *rsp_pdu;
1831 : : struct spdk_nvme_tcp_h2c_data_hdr *h2c_data;
1832 : : uint32_t plen, pdo, alignment;
1833 : :
1834 : : /* Reinit the send_ack and h2c_send_waiting_ack bits */
1835 : 529197 : tcp_req->ordering.bits.send_ack = 0;
1836 : 529197 : tcp_req->ordering.bits.h2c_send_waiting_ack = 0;
1837 : 529197 : rsp_pdu = tcp_req->pdu;
1838 [ - + ]: 529197 : memset(rsp_pdu, 0, sizeof(*rsp_pdu));
1839 : 529197 : rsp_pdu->req = tcp_req;
1840 : 529197 : h2c_data = &rsp_pdu->hdr.h2c_data;
1841 : :
1842 : 529197 : h2c_data->common.pdu_type = SPDK_NVME_TCP_PDU_TYPE_H2C_DATA;
1843 : 529197 : plen = h2c_data->common.hlen = sizeof(*h2c_data);
1844 : 529197 : h2c_data->cccid = tcp_req->cid;
1845 : 529197 : h2c_data->ttag = tcp_req->ttag;
1846 : 529197 : h2c_data->datao = tcp_req->datao;
1847 : :
1848 : 529197 : h2c_data->datal = spdk_min(tcp_req->r2tl_remain, tqpair->maxh2cdata);
1849 : 529197 : nvme_tcp_pdu_set_data_buf(rsp_pdu, tcp_req->iov, tcp_req->iovcnt,
1850 : : h2c_data->datao, h2c_data->datal);
1851 : 529197 : tcp_req->r2tl_remain -= h2c_data->datal;
1852 : :
1853 [ - + ]: 529197 : if (tqpair->flags.host_hdgst_enable) {
1854 : 0 : h2c_data->common.flags |= SPDK_NVME_TCP_CH_FLAGS_HDGSTF;
1855 : 0 : plen += SPDK_NVME_TCP_DIGEST_LEN;
1856 : : }
1857 : :
1858 : 529197 : rsp_pdu->padding_len = 0;
1859 : 529197 : pdo = plen;
1860 [ - + ]: 529197 : if (tqpair->cpda) {
1861 [ # # ]: 0 : alignment = (tqpair->cpda + 1) << 2;
1862 [ # # ]: 0 : if (alignment > plen) {
1863 : 0 : rsp_pdu->padding_len = alignment - plen;
1864 : 0 : pdo = plen = alignment;
1865 : : }
1866 : : }
1867 : :
1868 : 529197 : h2c_data->common.pdo = pdo;
1869 : 529197 : plen += h2c_data->datal;
1870 [ + + ]: 529197 : if (tqpair->flags.host_ddgst_enable) {
1871 : 76307 : h2c_data->common.flags |= SPDK_NVME_TCP_CH_FLAGS_DDGSTF;
1872 : 76307 : plen += SPDK_NVME_TCP_DIGEST_LEN;
1873 : : }
1874 : :
1875 : 529197 : h2c_data->common.plen = plen;
1876 : 529197 : tcp_req->datao += h2c_data->datal;
1877 [ + - ]: 529197 : if (!tcp_req->r2tl_remain) {
1878 : 529197 : h2c_data->common.flags |= SPDK_NVME_TCP_H2C_DATA_FLAGS_LAST_PDU;
1879 : : }
1880 : :
1881 [ - + - + ]: 529197 : SPDK_DEBUGLOG(nvme, "h2c_data info: datao=%u, datal=%u, pdu_len=%u for tqpair=%p\n",
1882 : : h2c_data->datao, h2c_data->datal, h2c_data->common.plen, tqpair);
1883 : :
1884 : 529197 : nvme_tcp_qpair_write_pdu(tqpair, rsp_pdu, nvme_tcp_qpair_h2c_data_send_complete, tcp_req);
1885 : 529197 : }
1886 : :
1887 : : static void
1888 : 529197 : nvme_tcp_r2t_hdr_handle(struct nvme_tcp_qpair *tqpair, struct nvme_tcp_pdu *pdu)
1889 : : {
1890 : : struct nvme_tcp_req *tcp_req;
1891 : 529197 : struct spdk_nvme_tcp_r2t_hdr *r2t = &pdu->hdr.r2t;
1892 : 529197 : uint32_t cid, error_offset = 0;
1893 : : enum spdk_nvme_tcp_term_req_fes fes;
1894 : :
1895 [ - + - + ]: 529197 : SPDK_DEBUGLOG(nvme, "enter\n");
1896 : 529197 : cid = r2t->cccid;
1897 : 529197 : tcp_req = get_nvme_active_req_by_cid(tqpair, cid);
1898 [ - + ]: 529197 : if (!tcp_req) {
1899 : 0 : SPDK_ERRLOG("Cannot find tcp_req for tqpair=%p\n", tqpair);
1900 : 0 : fes = SPDK_NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD;
1901 : 0 : error_offset = offsetof(struct spdk_nvme_tcp_r2t_hdr, cccid);
1902 : 0 : goto end;
1903 : : }
1904 : :
1905 [ - + - + ]: 529197 : SPDK_DEBUGLOG(nvme, "r2t info: r2to=%u, r2tl=%u for tqpair=%p\n", r2t->r2to, r2t->r2tl,
1906 : : tqpair);
1907 : :
1908 [ + - ]: 529197 : if (tcp_req->state == NVME_TCP_REQ_ACTIVE) {
1909 [ - + ]: 529197 : assert(tcp_req->active_r2ts == 0);
1910 : 529197 : tcp_req->state = NVME_TCP_REQ_ACTIVE_R2T;
1911 : : }
1912 : :
1913 [ - + ]: 529197 : if (tcp_req->datao != r2t->r2to) {
1914 : 0 : fes = SPDK_NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD;
1915 : 0 : error_offset = offsetof(struct spdk_nvme_tcp_r2t_hdr, r2to);
1916 : 0 : goto end;
1917 : :
1918 : : }
1919 : :
1920 [ - + ]: 529197 : if ((r2t->r2tl + r2t->r2to) > tcp_req->req->payload_size) {
1921 : 0 : SPDK_ERRLOG("Invalid R2T info for tcp_req=%p: (r2to(%u) + r2tl(%u)) exceeds payload_size(%u)\n",
1922 : : tcp_req, r2t->r2to, r2t->r2tl, tqpair->maxh2cdata);
1923 : 0 : fes = SPDK_NVME_TCP_TERM_REQ_FES_DATA_TRANSFER_OUT_OF_RANGE;
1924 : 0 : error_offset = offsetof(struct spdk_nvme_tcp_r2t_hdr, r2tl);
1925 : 0 : goto end;
1926 : : }
1927 : :
1928 : 529197 : tcp_req->active_r2ts++;
1929 [ - + ]: 529197 : if (spdk_unlikely(tcp_req->active_r2ts > tqpair->maxr2t)) {
1930 [ # # # # ]: 0 : if (tcp_req->state == NVME_TCP_REQ_ACTIVE_R2T && !tcp_req->ordering.bits.send_ack) {
1931 : : /* We receive a subsequent R2T while we are waiting for H2C transfer to complete */
1932 [ # # # # ]: 0 : SPDK_DEBUGLOG(nvme, "received a subsequent R2T\n");
1933 [ # # ]: 0 : assert(tcp_req->active_r2ts == tqpair->maxr2t + 1);
1934 : 0 : tcp_req->ttag_r2t_next = r2t->ttag;
1935 : 0 : tcp_req->r2tl_remain_next = r2t->r2tl;
1936 : 0 : tcp_req->ordering.bits.r2t_waiting_h2c_complete = 1;
1937 : 0 : nvme_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_READY);
1938 : 0 : return;
1939 : : } else {
1940 : 0 : fes = SPDK_NVME_TCP_TERM_REQ_FES_R2T_LIMIT_EXCEEDED;
1941 : 0 : SPDK_ERRLOG("Invalid R2T: Maximum number of R2T exceeded! Max: %u for tqpair=%p\n", tqpair->maxr2t,
1942 : : tqpair);
1943 : 0 : goto end;
1944 : : }
1945 : : }
1946 : :
1947 : 529197 : tcp_req->ttag = r2t->ttag;
1948 : 529197 : tcp_req->r2tl_remain = r2t->r2tl;
1949 : 529197 : nvme_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_READY);
1950 : :
1951 [ + - ]: 529197 : if (spdk_likely(tcp_req->ordering.bits.send_ack)) {
1952 : 529197 : nvme_tcp_send_h2c_data(tcp_req);
1953 : : } else {
1954 : 0 : tcp_req->ordering.bits.h2c_send_waiting_ack = 1;
1955 : : }
1956 : :
1957 : 529197 : return;
1958 : :
1959 : 0 : end:
1960 : 0 : nvme_tcp_qpair_send_h2c_term_req(tqpair, pdu, fes, error_offset);
1961 : :
1962 : : }
1963 : :
1964 : : static void
1965 : 22422187 : nvme_tcp_pdu_psh_handle(struct nvme_tcp_qpair *tqpair, uint32_t *reaped)
1966 : : {
1967 : : struct nvme_tcp_pdu *pdu;
1968 : : int rc;
1969 : 22422187 : uint32_t crc32c, error_offset = 0;
1970 : : enum spdk_nvme_tcp_term_req_fes fes;
1971 : :
1972 [ - + ]: 22422187 : assert(tqpair->recv_state == NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_PSH);
1973 : 22422187 : pdu = tqpair->recv_pdu;
1974 : :
1975 [ - + + + ]: 22422187 : SPDK_DEBUGLOG(nvme, "enter: pdu type =%u\n", pdu->hdr.common.pdu_type);
1976 : : /* check header digest if needed */
1977 [ - + + + ]: 22422187 : if (pdu->has_hdgst) {
1978 : 252152 : crc32c = nvme_tcp_pdu_calc_header_digest(pdu);
1979 : 252152 : rc = MATCH_DIGEST_WORD((uint8_t *)pdu->hdr.raw + pdu->hdr.common.hlen, crc32c);
1980 [ - + ]: 252152 : if (rc == 0) {
1981 : 0 : SPDK_ERRLOG("header digest error on tqpair=(%p) with pdu=%p\n", tqpair, pdu);
1982 : 0 : fes = SPDK_NVME_TCP_TERM_REQ_FES_HDGST_ERROR;
1983 : 0 : nvme_tcp_qpair_send_h2c_term_req(tqpair, pdu, fes, error_offset);
1984 : 0 : return;
1985 : :
1986 : : }
1987 : : }
1988 : :
1989 [ + + + - : 22422187 : switch (pdu->hdr.common.pdu_type) {
+ - ]
1990 : 4258 : case SPDK_NVME_TCP_PDU_TYPE_IC_RESP:
1991 : 4258 : nvme_tcp_icresp_handle(tqpair, pdu);
1992 : 4258 : break;
1993 : 14449476 : case SPDK_NVME_TCP_PDU_TYPE_CAPSULE_RESP:
1994 : 14449476 : nvme_tcp_capsule_resp_hdr_handle(tqpair, pdu, reaped);
1995 : 14449476 : break;
1996 : 7439256 : case SPDK_NVME_TCP_PDU_TYPE_C2H_DATA:
1997 : 7439256 : nvme_tcp_c2h_data_hdr_handle(tqpair, pdu);
1998 : 7439256 : break;
1999 : :
2000 : 0 : case SPDK_NVME_TCP_PDU_TYPE_C2H_TERM_REQ:
2001 : 0 : nvme_tcp_c2h_term_req_hdr_handle(tqpair, pdu);
2002 : 0 : break;
2003 : 529197 : case SPDK_NVME_TCP_PDU_TYPE_R2T:
2004 : 529197 : nvme_tcp_r2t_hdr_handle(tqpair, pdu);
2005 : 529197 : break;
2006 : :
2007 : 0 : default:
2008 : 0 : SPDK_ERRLOG("Unexpected PDU type 0x%02x\n", tqpair->recv_pdu->hdr.common.pdu_type);
2009 : 0 : fes = SPDK_NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD;
2010 : 0 : error_offset = 1;
2011 : 0 : nvme_tcp_qpair_send_h2c_term_req(tqpair, pdu, fes, error_offset);
2012 : 0 : break;
2013 : : }
2014 : :
2015 : : }
2016 : :
2017 : : static int
2018 : 61741073 : nvme_tcp_read_pdu(struct nvme_tcp_qpair *tqpair, uint32_t *reaped, uint32_t max_completions)
2019 : : {
2020 : 61741073 : int rc = 0;
2021 : : struct nvme_tcp_pdu *pdu;
2022 : : uint32_t data_len;
2023 : : enum nvme_tcp_pdu_recv_state prev_state;
2024 : :
2025 : 61741073 : *reaped = tqpair->async_complete;
2026 : 61741073 : tqpair->async_complete = 0;
2027 : :
2028 : : /* The loop here is to allow for several back-to-back state changes. */
2029 : : do {
2030 [ + + ]: 136451233 : if (*reaped >= max_completions) {
2031 : 332486 : break;
2032 : : }
2033 : :
2034 : 136118747 : prev_state = tqpair->recv_state;
2035 : 136118747 : pdu = tqpair->recv_pdu;
2036 [ + + + + : 136118747 : switch (tqpair->recv_state) {
+ + - ]
2037 : : /* If in a new state */
2038 : 22426429 : case NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_READY:
2039 [ - + ]: 22426429 : memset(pdu, 0, sizeof(struct nvme_tcp_pdu));
2040 : 22426429 : nvme_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_CH);
2041 : 22426429 : break;
2042 : : /* Wait for the pdu common header */
2043 : 75853683 : case NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_CH:
2044 [ - + ]: 75853683 : assert(pdu->ch_valid_bytes < sizeof(struct spdk_nvme_tcp_common_pdu_hdr));
2045 : 75853683 : rc = nvme_tcp_read_data(tqpair->sock,
2046 : 75853683 : sizeof(struct spdk_nvme_tcp_common_pdu_hdr) - pdu->ch_valid_bytes,
2047 : 75853683 : (uint8_t *)&pdu->hdr.common + pdu->ch_valid_bytes);
2048 [ + + ]: 75853683 : if (rc < 0) {
2049 : 73 : nvme_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_QUIESCING);
2050 : 73 : break;
2051 : : }
2052 : 75853610 : pdu->ch_valid_bytes += rc;
2053 [ + + ]: 75853610 : if (pdu->ch_valid_bytes < sizeof(struct spdk_nvme_tcp_common_pdu_hdr)) {
2054 : 53431416 : return NVME_TCP_PDU_IN_PROGRESS;
2055 : : }
2056 : :
2057 : : /* The command header of this PDU has now been read from the socket. */
2058 : 22422194 : nvme_tcp_pdu_ch_handle(tqpair);
2059 : 22422194 : break;
2060 : : /* Wait for the pdu specific header */
2061 : 22422582 : case NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_PSH:
2062 [ - + ]: 22422582 : assert(pdu->psh_valid_bytes < pdu->psh_len);
2063 : 22422582 : rc = nvme_tcp_read_data(tqpair->sock,
2064 : 22422582 : pdu->psh_len - pdu->psh_valid_bytes,
2065 : 22422582 : (uint8_t *)&pdu->hdr.raw + sizeof(struct spdk_nvme_tcp_common_pdu_hdr) + pdu->psh_valid_bytes);
2066 [ - + ]: 22422582 : if (rc < 0) {
2067 : 0 : nvme_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_QUIESCING);
2068 : 0 : break;
2069 : : }
2070 : :
2071 : 22422582 : pdu->psh_valid_bytes += rc;
2072 [ + + ]: 22422582 : if (pdu->psh_valid_bytes < pdu->psh_len) {
2073 : 395 : return NVME_TCP_PDU_IN_PROGRESS;
2074 : : }
2075 : :
2076 : : /* All header(ch, psh, head digits) of this PDU has now been read from the socket. */
2077 : 22422187 : nvme_tcp_pdu_psh_handle(tqpair, reaped);
2078 : 22422187 : break;
2079 : 15414226 : case NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_PAYLOAD:
2080 : : /* check whether the data is valid, if not we just return */
2081 [ - + ]: 15414226 : if (!pdu->data_len) {
2082 : 0 : return NVME_TCP_PDU_IN_PROGRESS;
2083 : : }
2084 : :
2085 : 15414226 : data_len = pdu->data_len;
2086 : : /* data digest */
2087 [ + - + + ]: 15414226 : if (spdk_unlikely((pdu->hdr.common.pdu_type == SPDK_NVME_TCP_PDU_TYPE_C2H_DATA) &&
2088 : : tqpair->flags.host_ddgst_enable)) {
2089 : 1349887 : data_len += SPDK_NVME_TCP_DIGEST_LEN;
2090 : 1349887 : pdu->ddgst_enable = true;
2091 : : }
2092 : :
2093 : 15414226 : rc = nvme_tcp_read_payload_data(tqpair->sock, pdu);
2094 [ + + ]: 15414226 : if (rc < 0) {
2095 : 1 : nvme_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_QUIESCING);
2096 : 1 : break;
2097 : : }
2098 : :
2099 : 15414225 : pdu->rw_offset += rc;
2100 [ + + ]: 15414225 : if (pdu->rw_offset < data_len) {
2101 : 7974973 : return NVME_TCP_PDU_IN_PROGRESS;
2102 : : }
2103 : :
2104 [ - + ]: 7439252 : assert(pdu->rw_offset == data_len);
2105 : : /* All of this PDU has now been read from the socket. */
2106 : 7439252 : nvme_tcp_pdu_payload_handle(tqpair, reaped);
2107 : 7439252 : break;
2108 : 1803 : case NVME_TCP_PDU_RECV_STATE_QUIESCING:
2109 [ + + ]: 1803 : if (TAILQ_EMPTY(&tqpair->outstanding_reqs)) {
2110 [ + + ]: 24 : if (nvme_qpair_get_state(&tqpair->qpair) == NVME_QPAIR_DISCONNECTING) {
2111 : 5 : nvme_transport_ctrlr_disconnect_qpair_done(&tqpair->qpair);
2112 : : }
2113 : :
2114 : 24 : nvme_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_ERROR);
2115 : : }
2116 : 1803 : break;
2117 : 24 : case NVME_TCP_PDU_RECV_STATE_ERROR:
2118 [ - + ]: 24 : memset(pdu, 0, sizeof(struct nvme_tcp_pdu));
2119 : 24 : return NVME_TCP_PDU_FATAL;
2120 : 0 : default:
2121 : 0 : assert(0);
2122 : : break;
2123 : : }
2124 [ + + ]: 74711939 : } while (prev_state != tqpair->recv_state);
2125 : :
2126 : 334265 : return rc > 0 ? 0 : rc;
2127 : : }
2128 : :
2129 : : static void
2130 : 0 : nvme_tcp_qpair_check_timeout(struct spdk_nvme_qpair *qpair)
2131 : : {
2132 : : uint64_t t02;
2133 : : struct nvme_tcp_req *tcp_req, *tmp;
2134 : 0 : struct nvme_tcp_qpair *tqpair = nvme_tcp_qpair(qpair);
2135 : 0 : struct spdk_nvme_ctrlr *ctrlr = qpair->ctrlr;
2136 : : struct spdk_nvme_ctrlr_process *active_proc;
2137 : :
2138 : : /* Don't check timeouts during controller initialization. */
2139 [ # # ]: 0 : if (ctrlr->state != NVME_CTRLR_STATE_READY) {
2140 : 0 : return;
2141 : : }
2142 : :
2143 [ # # ]: 0 : if (nvme_qpair_is_admin_queue(qpair)) {
2144 : 0 : active_proc = nvme_ctrlr_get_current_process(ctrlr);
2145 : : } else {
2146 : 0 : active_proc = qpair->active_proc;
2147 : : }
2148 : :
2149 : : /* Only check timeouts if the current process has a timeout callback. */
2150 [ # # # # ]: 0 : if (active_proc == NULL || active_proc->timeout_cb_fn == NULL) {
2151 : 0 : return;
2152 : : }
2153 : :
2154 : 0 : t02 = spdk_get_ticks();
2155 [ # # ]: 0 : TAILQ_FOREACH_SAFE(tcp_req, &tqpair->outstanding_reqs, link, tmp) {
2156 [ # # # # ]: 0 : if (ctrlr->is_failed) {
2157 : : /* The controller state may be changed to failed in one of the nvme_request_check_timeout callbacks. */
2158 : 0 : return;
2159 : : }
2160 [ # # ]: 0 : assert(tcp_req->req != NULL);
2161 : :
2162 [ # # ]: 0 : if (nvme_request_check_timeout(tcp_req->req, tcp_req->cid, active_proc, t02)) {
2163 : : /*
2164 : : * The requests are in order, so as soon as one has not timed out,
2165 : : * stop iterating.
2166 : : */
2167 : 0 : break;
2168 : : }
2169 : : }
2170 : : }
2171 : :
2172 : : static int nvme_tcp_ctrlr_connect_qpair_poll(struct spdk_nvme_ctrlr *ctrlr,
2173 : : struct spdk_nvme_qpair *qpair);
2174 : :
2175 : : static int
2176 : 61741530 : nvme_tcp_qpair_process_completions(struct spdk_nvme_qpair *qpair, uint32_t max_completions)
2177 : : {
2178 : 61741530 : struct nvme_tcp_qpair *tqpair = nvme_tcp_qpair(qpair);
2179 : 200374 : uint32_t reaped;
2180 : : int rc;
2181 : :
2182 [ + + ]: 61741530 : if (qpair->poll_group == NULL) {
2183 : 30279437 : rc = spdk_sock_flush(tqpair->sock);
2184 [ + + + + ]: 30279437 : if (rc < 0 && errno != EAGAIN) {
2185 : 457 : SPDK_ERRLOG("Failed to flush tqpair=%p (%d): %s\n", tqpair,
2186 : : errno, spdk_strerror(errno));
2187 [ - + - + ]: 457 : if (spdk_unlikely(tqpair->qpair.ctrlr->timeout_enabled)) {
2188 : 0 : nvme_tcp_qpair_check_timeout(qpair);
2189 : : }
2190 : :
2191 [ + + ]: 457 : if (nvme_qpair_get_state(qpair) == NVME_QPAIR_DISCONNECTING) {
2192 [ + - ]: 437 : if (TAILQ_EMPTY(&tqpair->outstanding_reqs)) {
2193 : 437 : nvme_transport_ctrlr_disconnect_qpair_done(qpair);
2194 : : }
2195 : :
2196 : : /* Don't return errors until the qpair gets disconnected */
2197 : 437 : return 0;
2198 : : }
2199 : :
2200 : 20 : goto fail;
2201 : : }
2202 : : }
2203 : :
2204 [ + + ]: 61741073 : if (max_completions == 0) {
2205 : 57092405 : max_completions = spdk_max(tqpair->num_entries, 1);
2206 : : } else {
2207 : 4648668 : max_completions = spdk_min(max_completions, tqpair->num_entries);
2208 : : }
2209 : :
2210 : 61741073 : reaped = 0;
2211 : 61741073 : rc = nvme_tcp_read_pdu(tqpair, &reaped, max_completions);
2212 [ + + ]: 61741073 : if (rc < 0) {
2213 [ - + - + ]: 79 : SPDK_DEBUGLOG(nvme, "Error polling CQ! (%d): %s\n",
2214 : : errno, spdk_strerror(errno));
2215 : 79 : goto fail;
2216 : : }
2217 : :
2218 [ - + - + ]: 61740994 : if (spdk_unlikely(tqpair->qpair.ctrlr->timeout_enabled)) {
2219 : 0 : nvme_tcp_qpair_check_timeout(qpair);
2220 : : }
2221 : :
2222 [ + + ]: 61740994 : if (spdk_unlikely(nvme_qpair_get_state(qpair) == NVME_QPAIR_CONNECTING)) {
2223 : 8652908 : rc = nvme_tcp_ctrlr_connect_qpair_poll(qpair->ctrlr, qpair);
2224 [ + + + + ]: 8652908 : if (rc != 0 && rc != -EAGAIN) {
2225 : 596 : SPDK_ERRLOG("Failed to connect tqpair=%p\n", tqpair);
2226 : 596 : goto fail;
2227 [ + + ]: 8652312 : } else if (rc == 0) {
2228 : : /* Once the connection is completed, we can submit queued requests */
2229 : 3650 : nvme_qpair_resubmit_requests(qpair, tqpair->num_entries);
2230 : : }
2231 : : }
2232 : :
2233 : 61740398 : return reaped;
2234 : 695 : fail:
2235 : :
2236 : : /*
2237 : : * Since admin queues take the ctrlr_lock before entering this function,
2238 : : * we can call nvme_transport_ctrlr_disconnect_qpair. For other qpairs we need
2239 : : * to call the generic function which will take the lock for us.
2240 : : */
2241 : 695 : qpair->transport_failure_reason = SPDK_NVME_QPAIR_FAILURE_UNKNOWN;
2242 : :
2243 [ + + ]: 695 : if (nvme_qpair_is_admin_queue(qpair)) {
2244 : 88 : enum nvme_qpair_state state_prev = nvme_qpair_get_state(qpair);
2245 : :
2246 : 88 : nvme_transport_ctrlr_disconnect_qpair(qpair->ctrlr, qpair);
2247 : :
2248 [ + + + + ]: 88 : if (state_prev == NVME_QPAIR_CONNECTING && qpair->poll_status != NULL) {
2249 : : /* Needed to free the poll_status */
2250 : 16 : nvme_tcp_ctrlr_connect_qpair_poll(qpair->ctrlr, qpair);
2251 : : }
2252 : : } else {
2253 : 607 : nvme_ctrlr_disconnect_qpair(qpair);
2254 : : }
2255 : 695 : return -ENXIO;
2256 : : }
2257 : :
2258 : : static void
2259 : 31462093 : nvme_tcp_qpair_sock_cb(void *ctx, struct spdk_sock_group *group, struct spdk_sock *sock)
2260 : : {
2261 : 31462093 : struct spdk_nvme_qpair *qpair = ctx;
2262 : 31462093 : struct nvme_tcp_poll_group *pgroup = nvme_tcp_poll_group(qpair->poll_group);
2263 : : int32_t num_completions;
2264 : 31462093 : struct nvme_tcp_qpair *tqpair = nvme_tcp_qpair(qpair);
2265 : :
2266 [ - + + + ]: 31462093 : if (tqpair->needs_poll) {
2267 [ + + ]: 1146914 : TAILQ_REMOVE(&pgroup->needs_poll, tqpair, link);
2268 : 1146914 : tqpair->needs_poll = false;
2269 : : }
2270 : :
2271 : 31462093 : num_completions = spdk_nvme_qpair_process_completions(qpair, pgroup->completions_per_qpair);
2272 : :
2273 [ + - + - ]: 31462093 : if (pgroup->num_completions >= 0 && num_completions >= 0) {
2274 : 31462093 : pgroup->num_completions += num_completions;
2275 : 31462093 : pgroup->stats.nvme_completions += num_completions;
2276 : : } else {
2277 : 0 : pgroup->num_completions = -ENXIO;
2278 : : }
2279 : 31462093 : }
2280 : :
2281 : : static int
2282 : 4281 : nvme_tcp_qpair_icreq_send(struct nvme_tcp_qpair *tqpair)
2283 : : {
2284 : : struct spdk_nvme_tcp_ic_req *ic_req;
2285 : : struct nvme_tcp_pdu *pdu;
2286 : : uint32_t timeout_in_sec;
2287 : :
2288 : 4281 : pdu = tqpair->send_pdu;
2289 [ - + ]: 4281 : memset(tqpair->send_pdu, 0, sizeof(*tqpair->send_pdu));
2290 : 4281 : ic_req = &pdu->hdr.ic_req;
2291 : :
2292 : 4281 : ic_req->common.pdu_type = SPDK_NVME_TCP_PDU_TYPE_IC_REQ;
2293 : 4281 : ic_req->common.hlen = ic_req->common.plen = sizeof(*ic_req);
2294 : 4281 : ic_req->pfv = 0;
2295 : 4281 : ic_req->maxr2t = NVME_TCP_MAX_R2T_DEFAULT - 1;
2296 : 4281 : ic_req->hpda = NVME_TCP_HPDA_DEFAULT;
2297 : :
2298 [ - + ]: 4281 : ic_req->dgst.bits.hdgst_enable = tqpair->qpair.ctrlr->opts.header_digest;
2299 [ - + ]: 4281 : ic_req->dgst.bits.ddgst_enable = tqpair->qpair.ctrlr->opts.data_digest;
2300 : :
2301 : 4281 : nvme_tcp_qpair_write_pdu(tqpair, pdu, nvme_tcp_send_icreq_complete, tqpair);
2302 : :
2303 [ + + ]: 4281 : timeout_in_sec = tqpair->qpair.async ? ICREQ_TIMEOUT_ASYNC : ICREQ_TIMEOUT_SYNC;
2304 : 4281 : tqpair->icreq_timeout_tsc = spdk_get_ticks() + (timeout_in_sec * spdk_get_ticks_hz());
2305 : 4281 : return 0;
2306 : : }
2307 : :
2308 : : static int
2309 : 16074 : nvme_tcp_qpair_connect_sock(struct spdk_nvme_ctrlr *ctrlr, struct spdk_nvme_qpair *qpair)
2310 : : {
2311 : 132 : struct sockaddr_storage dst_addr;
2312 : 132 : struct sockaddr_storage src_addr;
2313 : : int rc;
2314 : : struct nvme_tcp_qpair *tqpair;
2315 : : int family;
2316 : 16074 : long int port, src_port = 0;
2317 : : char *sock_impl_name;
2318 : 16074 : struct spdk_sock_impl_opts impl_opts = {};
2319 : 16074 : size_t impl_opts_size = sizeof(impl_opts);
2320 : 132 : struct spdk_sock_opts opts;
2321 : : struct nvme_tcp_ctrlr *tcp_ctrlr;
2322 : :
2323 : 16074 : tqpair = nvme_tcp_qpair(qpair);
2324 : :
2325 [ + - + ]: 16074 : switch (ctrlr->trid.adrfam) {
2326 : 16064 : case SPDK_NVMF_ADRFAM_IPV4:
2327 : 16064 : family = AF_INET;
2328 : 16064 : break;
2329 : 0 : case SPDK_NVMF_ADRFAM_IPV6:
2330 : 0 : family = AF_INET6;
2331 : 0 : break;
2332 : 10 : default:
2333 : 10 : SPDK_ERRLOG("Unhandled ADRFAM %d\n", ctrlr->trid.adrfam);
2334 : 10 : rc = -1;
2335 : 10 : return rc;
2336 : : }
2337 : :
2338 [ - + + + ]: 16064 : SPDK_DEBUGLOG(nvme, "adrfam %d ai_family %d\n", ctrlr->trid.adrfam, family);
2339 : :
2340 [ - + ]: 16064 : memset(&dst_addr, 0, sizeof(dst_addr));
2341 : :
2342 [ - + + + ]: 16064 : SPDK_DEBUGLOG(nvme, "trsvcid is %s\n", ctrlr->trid.trsvcid);
2343 : 16064 : rc = nvme_parse_addr(&dst_addr, family, ctrlr->trid.traddr, ctrlr->trid.trsvcid, &port);
2344 [ + + ]: 16064 : if (rc != 0) {
2345 : 10 : SPDK_ERRLOG("dst_addr nvme_parse_addr() failed\n");
2346 : 10 : return rc;
2347 : : }
2348 : :
2349 [ + + - + ]: 16054 : if (ctrlr->opts.src_addr[0] || ctrlr->opts.src_svcid[0]) {
2350 [ - + ]: 42 : memset(&src_addr, 0, sizeof(src_addr));
2351 : 84 : rc = nvme_parse_addr(&src_addr, family,
2352 [ + - ]: 42 : ctrlr->opts.src_addr[0] ? ctrlr->opts.src_addr : NULL,
2353 [ + + ]: 42 : ctrlr->opts.src_svcid[0] ? ctrlr->opts.src_svcid : NULL,
2354 : : &src_port);
2355 [ - + ]: 42 : if (rc != 0) {
2356 : 0 : SPDK_ERRLOG("src_addr nvme_parse_addr() failed\n");
2357 : 0 : return rc;
2358 : : }
2359 : : }
2360 : :
2361 : 16054 : tcp_ctrlr = SPDK_CONTAINEROF(ctrlr, struct nvme_tcp_ctrlr, ctrlr);
2362 [ + + ]: 16054 : sock_impl_name = tcp_ctrlr->psk[0] ? "ssl" : NULL;
2363 [ - + + + ]: 16054 : SPDK_DEBUGLOG(nvme, "sock_impl_name is %s\n", sock_impl_name);
2364 : :
2365 [ + + ]: 16054 : if (sock_impl_name) {
2366 : 130 : spdk_sock_impl_get_opts(sock_impl_name, &impl_opts, &impl_opts_size);
2367 : 130 : impl_opts.tls_version = SPDK_TLS_VERSION_1_3;
2368 : 130 : impl_opts.psk_identity = tcp_ctrlr->psk_identity;
2369 : 130 : impl_opts.psk_key = tcp_ctrlr->psk;
2370 : 130 : impl_opts.psk_key_size = tcp_ctrlr->psk_size;
2371 : 130 : impl_opts.tls_cipher_suites = tcp_ctrlr->tls_cipher_suite;
2372 : : }
2373 : 16054 : opts.opts_size = sizeof(opts);
2374 : 16054 : spdk_sock_get_default_opts(&opts);
2375 : 16054 : opts.priority = ctrlr->trid.priority;
2376 : 16054 : opts.zcopy = !nvme_qpair_is_admin_queue(qpair);
2377 [ + + ]: 16054 : opts.src_addr = ctrlr->opts.src_addr[0] ? ctrlr->opts.src_addr : NULL;
2378 : 16054 : opts.src_port = src_port;
2379 [ + + ]: 16054 : if (ctrlr->opts.transport_ack_timeout) {
2380 [ - + ]: 42 : opts.ack_timeout = 1ULL << ctrlr->opts.transport_ack_timeout;
2381 : : }
2382 [ + + ]: 16054 : if (sock_impl_name) {
2383 : 130 : opts.impl_opts = &impl_opts;
2384 : 130 : opts.impl_opts_size = sizeof(impl_opts);
2385 : : }
2386 : 16054 : tqpair->sock = spdk_sock_connect_ext(ctrlr->trid.traddr, port, sock_impl_name, &opts);
2387 [ + + ]: 16054 : if (!tqpair->sock) {
2388 : 11758 : SPDK_ERRLOG("sock connection error of tqpair=%p with addr=%s, port=%ld\n",
2389 : : tqpair, ctrlr->trid.traddr, port);
2390 : 11758 : rc = -1;
2391 : 11758 : return rc;
2392 : : }
2393 : :
2394 : 4296 : return 0;
2395 : : }
2396 : :
2397 : : static int
2398 : 8652924 : nvme_tcp_ctrlr_connect_qpair_poll(struct spdk_nvme_ctrlr *ctrlr, struct spdk_nvme_qpair *qpair)
2399 : : {
2400 : : struct nvme_tcp_qpair *tqpair;
2401 : : int rc;
2402 : :
2403 : 8652924 : tqpair = nvme_tcp_qpair(qpair);
2404 : :
2405 : : /* Prevent this function from being called recursively, as it could lead to issues with
2406 : : * nvme_fabric_qpair_connect_poll() if the connect response is received in the recursive
2407 : : * call.
2408 : : */
2409 [ + + ]: 8652924 : if (tqpair->flags.in_connect_poll) {
2410 : 156069 : return -EAGAIN;
2411 : : }
2412 : :
2413 : 8496855 : tqpair->flags.in_connect_poll = 1;
2414 : :
2415 [ + + + - : 8496855 : switch (tqpair->state) {
- ]
2416 : 8322651 : case NVME_TCP_QPAIR_STATE_INVALID:
2417 : : case NVME_TCP_QPAIR_STATE_INITIALIZING:
2418 [ - + ]: 8322651 : if (spdk_get_ticks() > tqpair->icreq_timeout_tsc) {
2419 : 0 : SPDK_ERRLOG("Failed to construct the tqpair=%p via correct icresp\n", tqpair);
2420 : 0 : rc = -ETIMEDOUT;
2421 : 0 : break;
2422 : : }
2423 : 8322651 : rc = -EAGAIN;
2424 : 8322651 : break;
2425 : 4258 : case NVME_TCP_QPAIR_STATE_FABRIC_CONNECT_SEND:
2426 : 4258 : rc = nvme_fabric_qpair_connect_async(&tqpair->qpair, tqpair->num_entries + 1);
2427 [ - + ]: 4258 : if (rc < 0) {
2428 : 0 : SPDK_ERRLOG("Failed to send an NVMe-oF Fabric CONNECT command\n");
2429 : 0 : break;
2430 : : }
2431 : 4258 : tqpair->state = NVME_TCP_QPAIR_STATE_FABRIC_CONNECT_POLL;
2432 : 4258 : rc = -EAGAIN;
2433 : 4258 : break;
2434 : 169946 : case NVME_TCP_QPAIR_STATE_FABRIC_CONNECT_POLL:
2435 : 169946 : rc = nvme_fabric_qpair_connect_poll(&tqpair->qpair);
2436 [ + + ]: 169946 : if (rc == 0) {
2437 : 3650 : tqpair->state = NVME_TCP_QPAIR_STATE_RUNNING;
2438 : 3650 : nvme_qpair_set_state(qpair, NVME_QPAIR_CONNECTED);
2439 [ + + ]: 166296 : } else if (rc != -EAGAIN) {
2440 : 608 : SPDK_ERRLOG("Failed to poll NVMe-oF Fabric CONNECT command\n");
2441 : : }
2442 : 169946 : break;
2443 : 0 : case NVME_TCP_QPAIR_STATE_RUNNING:
2444 : 0 : rc = 0;
2445 : 0 : break;
2446 : 0 : default:
2447 : 0 : assert(false);
2448 : : rc = -EINVAL;
2449 : : break;
2450 : : }
2451 : :
2452 : 8496855 : tqpair->flags.in_connect_poll = 0;
2453 : 8496855 : return rc;
2454 : : }
2455 : :
2456 : : static int
2457 : 16015 : nvme_tcp_ctrlr_connect_qpair(struct spdk_nvme_ctrlr *ctrlr, struct spdk_nvme_qpair *qpair)
2458 : : {
2459 : 16015 : int rc = 0;
2460 : : struct nvme_tcp_qpair *tqpair;
2461 : : struct nvme_tcp_poll_group *tgroup;
2462 : :
2463 : 16015 : tqpair = nvme_tcp_qpair(qpair);
2464 : :
2465 [ + + ]: 16015 : if (!tqpair->sock) {
2466 : 12350 : rc = nvme_tcp_qpair_connect_sock(ctrlr, qpair);
2467 [ + + ]: 12350 : if (rc < 0) {
2468 : 11739 : return rc;
2469 : : }
2470 : : }
2471 : :
2472 [ + + ]: 4276 : if (qpair->poll_group) {
2473 : 883 : rc = nvme_poll_group_connect_qpair(qpair);
2474 [ - + ]: 883 : if (rc) {
2475 : 0 : SPDK_ERRLOG("Unable to activate the tcp qpair.\n");
2476 : 0 : return rc;
2477 : : }
2478 : 883 : tgroup = nvme_tcp_poll_group(qpair->poll_group);
2479 : 883 : tqpair->stats = &tgroup->stats;
2480 : 883 : tqpair->shared_stats = true;
2481 : : } else {
2482 : : /* When resetting a controller, we disconnect adminq and then reconnect. The stats
2483 : : * is not freed when disconnecting. So when reconnecting, don't allocate memory
2484 : : * again.
2485 : : */
2486 [ + + ]: 3393 : if (tqpair->stats == NULL) {
2487 : 2782 : tqpair->stats = calloc(1, sizeof(*tqpair->stats));
2488 [ - + ]: 2782 : if (!tqpair->stats) {
2489 : 0 : SPDK_ERRLOG("tcp stats memory allocation failed\n");
2490 : 0 : return -ENOMEM;
2491 : : }
2492 : : }
2493 : : }
2494 : :
2495 : 4276 : tqpair->maxr2t = NVME_TCP_MAX_R2T_DEFAULT;
2496 : : /* Explicitly set the state and recv_state of tqpair */
2497 : 4276 : tqpair->state = NVME_TCP_QPAIR_STATE_INVALID;
2498 [ + + ]: 4276 : if (tqpair->recv_state != NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_READY) {
2499 : 611 : nvme_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_READY);
2500 : : }
2501 : 4276 : rc = nvme_tcp_qpair_icreq_send(tqpair);
2502 [ - + ]: 4276 : if (rc != 0) {
2503 : 0 : SPDK_ERRLOG("Unable to connect the tqpair\n");
2504 : 0 : return rc;
2505 : : }
2506 : :
2507 : 4276 : return rc;
2508 : : }
2509 : :
2510 : : static struct spdk_nvme_qpair *
2511 : 3719 : nvme_tcp_ctrlr_create_qpair(struct spdk_nvme_ctrlr *ctrlr,
2512 : : uint16_t qid, uint32_t qsize,
2513 : : enum spdk_nvme_qprio qprio,
2514 : : uint32_t num_requests, bool async)
2515 : : {
2516 : : struct nvme_tcp_qpair *tqpair;
2517 : : struct spdk_nvme_qpair *qpair;
2518 : : int rc;
2519 : :
2520 [ + + ]: 3719 : if (qsize < SPDK_NVME_QUEUE_MIN_ENTRIES) {
2521 : 15 : SPDK_ERRLOG("Failed to create qpair with size %u. Minimum queue size is %d.\n",
2522 : : qsize, SPDK_NVME_QUEUE_MIN_ENTRIES);
2523 : 15 : return NULL;
2524 : : }
2525 : :
2526 : 3704 : tqpair = calloc(1, sizeof(struct nvme_tcp_qpair));
2527 [ - + ]: 3704 : if (!tqpair) {
2528 : 0 : SPDK_ERRLOG("failed to get create tqpair\n");
2529 : 0 : return NULL;
2530 : : }
2531 : :
2532 : : /* Set num_entries one less than queue size. According to NVMe
2533 : : * and NVMe-oF specs we can not submit queue size requests,
2534 : : * one slot shall always remain empty.
2535 : : */
2536 : 3704 : tqpair->num_entries = qsize - 1;
2537 : 3704 : qpair = &tqpair->qpair;
2538 : 3704 : rc = nvme_qpair_init(qpair, qid, ctrlr, qprio, num_requests, async);
2539 [ - + ]: 3704 : if (rc != 0) {
2540 : 0 : free(tqpair);
2541 : 0 : return NULL;
2542 : : }
2543 : :
2544 : 3704 : rc = nvme_tcp_alloc_reqs(tqpair);
2545 [ - + ]: 3704 : if (rc) {
2546 : 0 : nvme_tcp_ctrlr_delete_io_qpair(ctrlr, qpair);
2547 : 0 : return NULL;
2548 : : }
2549 : :
2550 : : /* spdk_nvme_qpair_get_optimal_poll_group needs socket information.
2551 : : * So create the socket first when creating a qpair. */
2552 : 3704 : rc = nvme_tcp_qpair_connect_sock(ctrlr, qpair);
2553 [ + + ]: 3704 : if (rc) {
2554 : 24 : nvme_tcp_ctrlr_delete_io_qpair(ctrlr, qpair);
2555 : 24 : return NULL;
2556 : : }
2557 : :
2558 : 3680 : return qpair;
2559 : : }
2560 : :
2561 : : static struct spdk_nvme_qpair *
2562 : 2399 : nvme_tcp_ctrlr_create_io_qpair(struct spdk_nvme_ctrlr *ctrlr, uint16_t qid,
2563 : : const struct spdk_nvme_io_qpair_opts *opts)
2564 : : {
2565 : 2403 : return nvme_tcp_ctrlr_create_qpair(ctrlr, qid, opts->io_queue_size, opts->qprio,
2566 [ - + ]: 2399 : opts->io_queue_requests, opts->async_mode);
2567 : : }
2568 : :
2569 [ - + ]: 2382 : SPDK_LOG_DEPRECATION_REGISTER(nvme_ctrlr_psk, "spdk_nvme_ctrlr_opts.psk", "v24.09", 0);
2570 : :
2571 : : static int
2572 : 62 : nvme_tcp_generate_tls_credentials(struct nvme_tcp_ctrlr *tctrlr)
2573 : : {
2574 : 62 : struct spdk_nvme_ctrlr *ctrlr = &tctrlr->ctrlr;
2575 : : int rc;
2576 : 62 : uint8_t psk_retained[SPDK_TLS_PSK_MAX_LEN] = {};
2577 : 62 : uint8_t psk_configured[SPDK_TLS_PSK_MAX_LEN] = {};
2578 : 62 : uint8_t pskbuf[SPDK_TLS_PSK_MAX_LEN + 1] = {};
2579 : : uint8_t tls_cipher_suite;
2580 : 0 : uint8_t psk_retained_hash;
2581 : 0 : uint64_t psk_configured_size;
2582 : : uint8_t *psk;
2583 : :
2584 [ + + ]: 62 : if (ctrlr->opts.tls_psk != NULL) {
2585 : 33 : rc = spdk_key_get_key(ctrlr->opts.tls_psk, pskbuf, SPDK_TLS_PSK_MAX_LEN);
2586 [ + + ]: 33 : if (rc < 0) {
2587 : 3 : SPDK_ERRLOG("Failed to obtain key '%s': %s\n",
2588 : : spdk_key_get_name(ctrlr->opts.tls_psk), spdk_strerror(-rc));
2589 : 3 : goto finish;
2590 : : }
2591 : :
2592 : 30 : psk = pskbuf;
2593 : : } else {
2594 : 29 : SPDK_LOG_DEPRECATED(nvme_ctrlr_psk);
2595 : 29 : psk = ctrlr->opts.psk;
2596 : : }
2597 : :
2598 : 59 : rc = nvme_tcp_parse_interchange_psk(psk, psk_configured, sizeof(psk_configured),
2599 : : &psk_configured_size, &psk_retained_hash);
2600 [ - + ]: 59 : if (rc < 0) {
2601 : 0 : SPDK_ERRLOG("Failed to parse PSK interchange!\n");
2602 : 0 : goto finish;
2603 : : }
2604 : :
2605 : : /* The Base64 string encodes the configured PSK (32 or 48 bytes binary).
2606 : : * This check also ensures that psk_configured_size is smaller than
2607 : : * psk_retained buffer size. */
2608 [ + + ]: 59 : if (psk_configured_size == SHA256_DIGEST_LENGTH) {
2609 : 41 : tls_cipher_suite = NVME_TCP_CIPHER_AES_128_GCM_SHA256;
2610 : 41 : tctrlr->tls_cipher_suite = "TLS_AES_128_GCM_SHA256";
2611 [ + - ]: 18 : } else if (psk_configured_size == SHA384_DIGEST_LENGTH) {
2612 : 18 : tls_cipher_suite = NVME_TCP_CIPHER_AES_256_GCM_SHA384;
2613 : 18 : tctrlr->tls_cipher_suite = "TLS_AES_256_GCM_SHA384";
2614 : : } else {
2615 : 0 : SPDK_ERRLOG("Unrecognized cipher suite!\n");
2616 : 0 : rc = -ENOTSUP;
2617 : 0 : goto finish;
2618 : : }
2619 : :
2620 : 59 : rc = nvme_tcp_generate_psk_identity(tctrlr->psk_identity, sizeof(tctrlr->psk_identity),
2621 : 59 : ctrlr->opts.hostnqn, ctrlr->trid.subnqn,
2622 : : tls_cipher_suite);
2623 [ - + ]: 59 : if (rc) {
2624 : 0 : SPDK_ERRLOG("could not generate PSK identity\n");
2625 : 0 : goto finish;
2626 : : }
2627 : :
2628 : : /* No hash indicates that Configured PSK must be used as Retained PSK. */
2629 [ + + ]: 59 : if (psk_retained_hash == NVME_TCP_HASH_ALGORITHM_NONE) {
2630 [ - + ]: 21 : assert(psk_configured_size < sizeof(psk_retained));
2631 [ - + - + ]: 21 : memcpy(psk_retained, psk_configured, psk_configured_size);
2632 : 21 : rc = psk_configured_size;
2633 : : } else {
2634 : : /* Derive retained PSK. */
2635 : 38 : rc = nvme_tcp_derive_retained_psk(psk_configured, psk_configured_size, ctrlr->opts.hostnqn,
2636 : : psk_retained, sizeof(psk_retained), psk_retained_hash);
2637 [ - + ]: 38 : if (rc < 0) {
2638 : 0 : SPDK_ERRLOG("Unable to derive retained PSK!\n");
2639 : 0 : goto finish;
2640 : : }
2641 : : }
2642 : :
2643 : 59 : rc = nvme_tcp_derive_tls_psk(psk_retained, rc, tctrlr->psk_identity, tctrlr->psk,
2644 : : sizeof(tctrlr->psk), tls_cipher_suite);
2645 [ - + ]: 59 : if (rc < 0) {
2646 : 0 : SPDK_ERRLOG("Could not generate TLS PSK!\n");
2647 : 0 : goto finish;
2648 : : }
2649 : :
2650 : 59 : tctrlr->psk_size = rc;
2651 : 59 : rc = 0;
2652 : 62 : finish:
2653 : 62 : spdk_memset_s(psk_configured, sizeof(psk_configured), 0, sizeof(psk_configured));
2654 : 62 : spdk_memset_s(pskbuf, sizeof(pskbuf), 0, sizeof(pskbuf));
2655 : :
2656 : 62 : return rc;
2657 : : }
2658 : :
2659 : : /* We have to use the typedef in the function declaration to appease astyle. */
2660 : : typedef struct spdk_nvme_ctrlr spdk_nvme_ctrlr_t;
2661 : :
2662 : : static spdk_nvme_ctrlr_t *
2663 : 1323 : nvme_tcp_ctrlr_construct(const struct spdk_nvme_transport_id *trid,
2664 : : const struct spdk_nvme_ctrlr_opts *opts,
2665 : : void *devhandle)
2666 : : {
2667 : : struct nvme_tcp_ctrlr *tctrlr;
2668 : : int rc;
2669 : :
2670 : 1323 : tctrlr = calloc(1, sizeof(*tctrlr));
2671 [ - + ]: 1323 : if (tctrlr == NULL) {
2672 : 0 : SPDK_ERRLOG("could not allocate ctrlr\n");
2673 : 0 : return NULL;
2674 : : }
2675 : :
2676 : 1323 : tctrlr->ctrlr.opts = *opts;
2677 : 1323 : tctrlr->ctrlr.trid = *trid;
2678 : :
2679 [ + + + + ]: 1323 : if (opts->psk[0] != '\0' || opts->tls_psk != NULL) {
2680 : : /* Only allow either one at a time */
2681 [ + + - + ]: 62 : if (opts->tls_psk != NULL && opts->psk[0] != '\0') {
2682 : 0 : SPDK_ERRLOG("Either spdk_nvme_ctrlr_opts.tls_psk or .psk can be set at "
2683 : : "the same time\n");
2684 : 0 : free(tctrlr);
2685 : 0 : return NULL;
2686 : : }
2687 : 62 : rc = nvme_tcp_generate_tls_credentials(tctrlr);
2688 : 62 : spdk_memset_s(&tctrlr->ctrlr.opts.psk, sizeof(tctrlr->ctrlr.opts.psk), 0,
2689 : : sizeof(tctrlr->ctrlr.opts.psk));
2690 : :
2691 [ + + ]: 62 : if (rc != 0) {
2692 : 3 : free(tctrlr);
2693 : 3 : return NULL;
2694 : : }
2695 : : }
2696 : :
2697 [ + + ]: 1320 : if (opts->transport_ack_timeout > NVME_TCP_CTRLR_MAX_TRANSPORT_ACK_TIMEOUT) {
2698 : 25 : SPDK_NOTICELOG("transport_ack_timeout exceeds max value %d, use max value\n",
2699 : : NVME_TCP_CTRLR_MAX_TRANSPORT_ACK_TIMEOUT);
2700 : 25 : tctrlr->ctrlr.opts.transport_ack_timeout = NVME_TCP_CTRLR_MAX_TRANSPORT_ACK_TIMEOUT;
2701 : : }
2702 : :
2703 : 1320 : rc = nvme_ctrlr_construct(&tctrlr->ctrlr);
2704 [ - + ]: 1320 : if (rc != 0) {
2705 : 0 : free(tctrlr);
2706 : 0 : return NULL;
2707 : : }
2708 : :
2709 : : /* Sequence might be used not only for data digest offload purposes but
2710 : : * to handle a potential COPY operation appended as the result of translation. */
2711 : 1320 : tctrlr->ctrlr.flags |= SPDK_NVME_CTRLR_ACCEL_SEQUENCE_SUPPORTED;
2712 : 1325 : tctrlr->ctrlr.adminq = nvme_tcp_ctrlr_create_qpair(&tctrlr->ctrlr, 0,
2713 : 1320 : tctrlr->ctrlr.opts.admin_queue_size, 0,
2714 : 1320 : tctrlr->ctrlr.opts.admin_queue_size, true);
2715 [ + + ]: 1320 : if (!tctrlr->ctrlr.adminq) {
2716 : 29 : SPDK_ERRLOG("failed to create admin qpair\n");
2717 : 29 : nvme_tcp_ctrlr_destruct(&tctrlr->ctrlr);
2718 : 29 : return NULL;
2719 : : }
2720 : :
2721 [ - + ]: 1291 : if (nvme_ctrlr_add_process(&tctrlr->ctrlr, 0) != 0) {
2722 : 0 : SPDK_ERRLOG("nvme_ctrlr_add_process() failed\n");
2723 : 0 : nvme_ctrlr_destruct(&tctrlr->ctrlr);
2724 : 0 : return NULL;
2725 : : }
2726 : :
2727 : 1291 : return &tctrlr->ctrlr;
2728 : : }
2729 : :
2730 : : static uint32_t
2731 : 1266 : nvme_tcp_ctrlr_get_max_xfer_size(struct spdk_nvme_ctrlr *ctrlr)
2732 : : {
2733 : : /* TCP transport doesn't limit maximum IO transfer size. */
2734 : 1266 : return UINT32_MAX;
2735 : : }
2736 : :
2737 : : static uint16_t
2738 : 1266 : nvme_tcp_ctrlr_get_max_sges(struct spdk_nvme_ctrlr *ctrlr)
2739 : : {
2740 : 1266 : return NVME_TCP_MAX_SGL_DESCRIPTORS;
2741 : : }
2742 : :
2743 : : static int
2744 : 318836 : nvme_tcp_qpair_iterate_requests(struct spdk_nvme_qpair *qpair,
2745 : : int (*iter_fn)(struct nvme_request *req, void *arg),
2746 : : void *arg)
2747 : : {
2748 : 318836 : struct nvme_tcp_qpair *tqpair = nvme_tcp_qpair(qpair);
2749 : : struct nvme_tcp_req *tcp_req, *tmp;
2750 : : int rc;
2751 : :
2752 [ - + ]: 318836 : assert(iter_fn != NULL);
2753 : :
2754 [ + + ]: 15107087 : TAILQ_FOREACH_SAFE(tcp_req, &tqpair->outstanding_reqs, link, tmp) {
2755 [ - + ]: 14788259 : assert(tcp_req->req != NULL);
2756 : :
2757 : 14788259 : rc = iter_fn(tcp_req->req, arg);
2758 [ + + ]: 14788259 : if (rc != 0) {
2759 : 8 : return rc;
2760 : : }
2761 : : }
2762 : :
2763 : 318828 : return 0;
2764 : : }
2765 : :
2766 : : static void
2767 : 1647 : nvme_tcp_admin_qpair_abort_aers(struct spdk_nvme_qpair *qpair)
2768 : : {
2769 : : struct nvme_tcp_req *tcp_req, *tmp;
2770 : 1647 : struct spdk_nvme_cpl cpl = {};
2771 : 1647 : struct nvme_tcp_qpair *tqpair = nvme_tcp_qpair(qpair);
2772 : :
2773 : 1647 : cpl.status.sc = SPDK_NVME_SC_ABORTED_SQ_DELETION;
2774 : 1647 : cpl.status.sct = SPDK_NVME_SCT_GENERIC;
2775 : :
2776 [ + + ]: 6529 : TAILQ_FOREACH_SAFE(tcp_req, &tqpair->outstanding_reqs, link, tmp) {
2777 [ - + ]: 4882 : assert(tcp_req->req != NULL);
2778 [ - + ]: 4882 : if (tcp_req->req->cmd.opc != SPDK_NVME_OPC_ASYNC_EVENT_REQUEST) {
2779 : 0 : continue;
2780 : : }
2781 : :
2782 : 4882 : nvme_tcp_req_complete(tcp_req, tqpair, &cpl, false);
2783 : : }
2784 : 1647 : }
2785 : :
2786 : : static struct spdk_nvme_transport_poll_group *
2787 : 835 : nvme_tcp_poll_group_create(void)
2788 : : {
2789 : 835 : struct nvme_tcp_poll_group *group = calloc(1, sizeof(*group));
2790 : :
2791 [ - + ]: 835 : if (group == NULL) {
2792 : 0 : SPDK_ERRLOG("Unable to allocate poll group.\n");
2793 : 0 : return NULL;
2794 : : }
2795 : :
2796 : 835 : TAILQ_INIT(&group->needs_poll);
2797 : :
2798 : 835 : group->sock_group = spdk_sock_group_create(group);
2799 [ - + ]: 835 : if (group->sock_group == NULL) {
2800 : 0 : free(group);
2801 : 0 : SPDK_ERRLOG("Unable to allocate sock group.\n");
2802 : 0 : return NULL;
2803 : : }
2804 : :
2805 : 835 : return &group->group;
2806 : : }
2807 : :
2808 : : static struct spdk_nvme_transport_poll_group *
2809 : 0 : nvme_tcp_qpair_get_optimal_poll_group(struct spdk_nvme_qpair *qpair)
2810 : : {
2811 : 0 : struct nvme_tcp_qpair *tqpair = nvme_tcp_qpair(qpair);
2812 : 0 : struct spdk_sock_group *group = NULL;
2813 : : int rc;
2814 : :
2815 : 0 : rc = spdk_sock_get_optimal_sock_group(tqpair->sock, &group, NULL);
2816 [ # # # # ]: 0 : if (!rc && group != NULL) {
2817 : 0 : return spdk_sock_group_get_ctx(group);
2818 : : }
2819 : :
2820 : 0 : return NULL;
2821 : : }
2822 : :
2823 : : static int
2824 : 883 : nvme_tcp_poll_group_connect_qpair(struct spdk_nvme_qpair *qpair)
2825 : : {
2826 : 883 : struct nvme_tcp_poll_group *group = nvme_tcp_poll_group(qpair->poll_group);
2827 : 883 : struct nvme_tcp_qpair *tqpair = nvme_tcp_qpair(qpair);
2828 : :
2829 [ - + ]: 883 : if (spdk_sock_group_add_sock(group->sock_group, tqpair->sock, nvme_tcp_qpair_sock_cb, qpair)) {
2830 : 0 : return -EPROTO;
2831 : : }
2832 : 883 : return 0;
2833 : : }
2834 : :
2835 : : static int
2836 : 883 : nvme_tcp_poll_group_disconnect_qpair(struct spdk_nvme_qpair *qpair)
2837 : : {
2838 : 883 : struct nvme_tcp_poll_group *group = nvme_tcp_poll_group(qpair->poll_group);
2839 : 883 : struct nvme_tcp_qpair *tqpair = nvme_tcp_qpair(qpair);
2840 : :
2841 [ - + + + ]: 883 : if (tqpair->needs_poll) {
2842 [ - + ]: 10 : TAILQ_REMOVE(&group->needs_poll, tqpair, link);
2843 : 10 : tqpair->needs_poll = false;
2844 : : }
2845 : :
2846 [ + - + - ]: 883 : if (tqpair->sock && group->sock_group) {
2847 [ - + ]: 883 : if (spdk_sock_group_remove_sock(group->sock_group, tqpair->sock)) {
2848 : 0 : return -EPROTO;
2849 : : }
2850 : : }
2851 : 883 : return 0;
2852 : : }
2853 : :
2854 : : static int
2855 : 883 : nvme_tcp_poll_group_add(struct spdk_nvme_transport_poll_group *tgroup,
2856 : : struct spdk_nvme_qpair *qpair)
2857 : : {
2858 : 883 : struct nvme_tcp_qpair *tqpair = nvme_tcp_qpair(qpair);
2859 : 883 : struct nvme_tcp_poll_group *group = nvme_tcp_poll_group(tgroup);
2860 : :
2861 : : /* disconnected qpairs won't have a sock to add. */
2862 [ - + ]: 883 : if (nvme_qpair_get_state(qpair) >= NVME_QPAIR_CONNECTED) {
2863 [ # # ]: 0 : if (spdk_sock_group_add_sock(group->sock_group, tqpair->sock, nvme_tcp_qpair_sock_cb, qpair)) {
2864 : 0 : return -EPROTO;
2865 : : }
2866 : : }
2867 : :
2868 : 883 : return 0;
2869 : : }
2870 : :
2871 : : static int
2872 : 883 : nvme_tcp_poll_group_remove(struct spdk_nvme_transport_poll_group *tgroup,
2873 : : struct spdk_nvme_qpair *qpair)
2874 : : {
2875 : : struct nvme_tcp_qpair *tqpair;
2876 : : struct nvme_tcp_poll_group *group;
2877 : :
2878 [ - + ]: 883 : assert(qpair->poll_group_tailq_head == &tgroup->disconnected_qpairs);
2879 : :
2880 : 883 : tqpair = nvme_tcp_qpair(qpair);
2881 : 883 : group = nvme_tcp_poll_group(tgroup);
2882 : :
2883 [ - + - + ]: 883 : assert(tqpair->shared_stats == true);
2884 : 883 : tqpair->stats = &g_dummy_stats;
2885 : :
2886 [ - + + + ]: 883 : if (tqpair->needs_poll) {
2887 [ - + ]: 2 : TAILQ_REMOVE(&group->needs_poll, tqpair, link);
2888 : 2 : tqpair->needs_poll = false;
2889 : : }
2890 : :
2891 : 883 : return 0;
2892 : : }
2893 : :
2894 : : static int64_t
2895 : 262620744 : nvme_tcp_poll_group_process_completions(struct spdk_nvme_transport_poll_group *tgroup,
2896 : : uint32_t completions_per_qpair, spdk_nvme_disconnected_qpair_cb disconnected_qpair_cb)
2897 : : {
2898 : 262620744 : struct nvme_tcp_poll_group *group = nvme_tcp_poll_group(tgroup);
2899 : : struct spdk_nvme_qpair *qpair, *tmp_qpair;
2900 : : struct nvme_tcp_qpair *tqpair, *tmp_tqpair;
2901 : : int num_events;
2902 : :
2903 : 262620744 : group->completions_per_qpair = completions_per_qpair;
2904 : 262620744 : group->num_completions = 0;
2905 : 262620744 : group->stats.polls++;
2906 : :
2907 : 262620744 : num_events = spdk_sock_group_poll(group->sock_group);
2908 : :
2909 [ + + ]: 264621694 : STAILQ_FOREACH_SAFE(qpair, &tgroup->disconnected_qpairs, poll_group_stailq, tmp_qpair) {
2910 : 2000950 : tqpair = nvme_tcp_qpair(qpair);
2911 [ + + ]: 2000950 : if (nvme_qpair_get_state(qpair) == NVME_QPAIR_DISCONNECTING) {
2912 [ + + ]: 802 : if (TAILQ_EMPTY(&tqpair->outstanding_reqs)) {
2913 : 797 : nvme_transport_ctrlr_disconnect_qpair_done(qpair);
2914 : : }
2915 : : }
2916 : : /* Wait until the qpair transitions to the DISCONNECTED state, otherwise user might
2917 : : * want to free it from disconnect_qpair_cb, while it's not fully disconnected (and
2918 : : * might still have outstanding requests) */
2919 [ + + ]: 2000950 : if (nvme_qpair_get_state(qpair) == NVME_QPAIR_DISCONNECTED) {
2920 : 2000945 : disconnected_qpair_cb(qpair, tgroup->group->ctx);
2921 : : }
2922 : : }
2923 : :
2924 : : /* If any qpairs were marked as needing to be polled due to an asynchronous write completion
2925 : : * and they weren't polled as a consequence of calling spdk_sock_group_poll above, poll them now. */
2926 [ + + ]: 263048943 : TAILQ_FOREACH_SAFE(tqpair, &group->needs_poll, link, tmp_tqpair) {
2927 : 428199 : nvme_tcp_qpair_sock_cb(&tqpair->qpair, group->sock_group, tqpair->sock);
2928 : : }
2929 : :
2930 [ - + ]: 262620744 : if (spdk_unlikely(num_events < 0)) {
2931 : 0 : return num_events;
2932 : : }
2933 : :
2934 : 262620744 : group->stats.idle_polls += !num_events;
2935 : 262620744 : group->stats.socket_completions += num_events;
2936 : :
2937 : 262620744 : return group->num_completions;
2938 : : }
2939 : :
2940 : : static int
2941 : 835 : nvme_tcp_poll_group_destroy(struct spdk_nvme_transport_poll_group *tgroup)
2942 : : {
2943 : : int rc;
2944 : 835 : struct nvme_tcp_poll_group *group = nvme_tcp_poll_group(tgroup);
2945 : :
2946 [ + - - + ]: 835 : if (!STAILQ_EMPTY(&tgroup->connected_qpairs) || !STAILQ_EMPTY(&tgroup->disconnected_qpairs)) {
2947 : 0 : return -EBUSY;
2948 : : }
2949 : :
2950 : 835 : rc = spdk_sock_group_close(&group->sock_group);
2951 [ - + ]: 835 : if (rc != 0) {
2952 : 0 : SPDK_ERRLOG("Failed to close the sock group for a tcp poll group.\n");
2953 : 0 : assert(false);
2954 : : }
2955 : :
2956 : 835 : free(tgroup);
2957 : :
2958 : 835 : return 0;
2959 : : }
2960 : :
2961 : : static int
2962 : 21 : nvme_tcp_poll_group_get_stats(struct spdk_nvme_transport_poll_group *tgroup,
2963 : : struct spdk_nvme_transport_poll_group_stat **_stats)
2964 : : {
2965 : : struct nvme_tcp_poll_group *group;
2966 : : struct spdk_nvme_transport_poll_group_stat *stats;
2967 : :
2968 [ + + + + ]: 21 : if (tgroup == NULL || _stats == NULL) {
2969 : 10 : SPDK_ERRLOG("Invalid stats or group pointer\n");
2970 : 10 : return -EINVAL;
2971 : : }
2972 : :
2973 : 11 : group = nvme_tcp_poll_group(tgroup);
2974 : :
2975 : 11 : stats = calloc(1, sizeof(*stats));
2976 [ - + ]: 11 : if (!stats) {
2977 : 0 : SPDK_ERRLOG("Can't allocate memory for TCP stats\n");
2978 : 0 : return -ENOMEM;
2979 : : }
2980 : 11 : stats->trtype = SPDK_NVME_TRANSPORT_TCP;
2981 [ - + - + ]: 11 : memcpy(&stats->tcp, &group->stats, sizeof(group->stats));
2982 : :
2983 : 11 : *_stats = stats;
2984 : :
2985 : 11 : return 0;
2986 : : }
2987 : :
2988 : : static void
2989 : 11 : nvme_tcp_poll_group_free_stats(struct spdk_nvme_transport_poll_group *tgroup,
2990 : : struct spdk_nvme_transport_poll_group_stat *stats)
2991 : : {
2992 : 11 : free(stats);
2993 : 11 : }
2994 : :
2995 : : static int
2996 : 3174 : nvme_tcp_ctrlr_get_memory_domains(const struct spdk_nvme_ctrlr *ctrlr,
2997 : : struct spdk_memory_domain **domains, int array_size)
2998 : : {
2999 [ + + + - ]: 3174 : if (domains && array_size > 0) {
3000 : 217 : domains[0] = spdk_memory_domain_get_system_domain();
3001 : : }
3002 : :
3003 : 3174 : return 1;
3004 : : }
3005 : :
3006 : : const struct spdk_nvme_transport_ops tcp_ops = {
3007 : : .name = "TCP",
3008 : : .type = SPDK_NVME_TRANSPORT_TCP,
3009 : : .ctrlr_construct = nvme_tcp_ctrlr_construct,
3010 : : .ctrlr_scan = nvme_fabric_ctrlr_scan,
3011 : : .ctrlr_destruct = nvme_tcp_ctrlr_destruct,
3012 : : .ctrlr_enable = nvme_tcp_ctrlr_enable,
3013 : :
3014 : : .ctrlr_set_reg_4 = nvme_fabric_ctrlr_set_reg_4,
3015 : : .ctrlr_set_reg_8 = nvme_fabric_ctrlr_set_reg_8,
3016 : : .ctrlr_get_reg_4 = nvme_fabric_ctrlr_get_reg_4,
3017 : : .ctrlr_get_reg_8 = nvme_fabric_ctrlr_get_reg_8,
3018 : : .ctrlr_set_reg_4_async = nvme_fabric_ctrlr_set_reg_4_async,
3019 : : .ctrlr_set_reg_8_async = nvme_fabric_ctrlr_set_reg_8_async,
3020 : : .ctrlr_get_reg_4_async = nvme_fabric_ctrlr_get_reg_4_async,
3021 : : .ctrlr_get_reg_8_async = nvme_fabric_ctrlr_get_reg_8_async,
3022 : :
3023 : : .ctrlr_get_max_xfer_size = nvme_tcp_ctrlr_get_max_xfer_size,
3024 : : .ctrlr_get_max_sges = nvme_tcp_ctrlr_get_max_sges,
3025 : :
3026 : : .ctrlr_create_io_qpair = nvme_tcp_ctrlr_create_io_qpair,
3027 : : .ctrlr_delete_io_qpair = nvme_tcp_ctrlr_delete_io_qpair,
3028 : : .ctrlr_connect_qpair = nvme_tcp_ctrlr_connect_qpair,
3029 : : .ctrlr_disconnect_qpair = nvme_tcp_ctrlr_disconnect_qpair,
3030 : :
3031 : : .ctrlr_get_memory_domains = nvme_tcp_ctrlr_get_memory_domains,
3032 : :
3033 : : .qpair_abort_reqs = nvme_tcp_qpair_abort_reqs,
3034 : : .qpair_reset = nvme_tcp_qpair_reset,
3035 : : .qpair_submit_request = nvme_tcp_qpair_submit_request,
3036 : : .qpair_process_completions = nvme_tcp_qpair_process_completions,
3037 : : .qpair_iterate_requests = nvme_tcp_qpair_iterate_requests,
3038 : : .admin_qpair_abort_aers = nvme_tcp_admin_qpair_abort_aers,
3039 : :
3040 : : .poll_group_create = nvme_tcp_poll_group_create,
3041 : : .qpair_get_optimal_poll_group = nvme_tcp_qpair_get_optimal_poll_group,
3042 : : .poll_group_connect_qpair = nvme_tcp_poll_group_connect_qpair,
3043 : : .poll_group_disconnect_qpair = nvme_tcp_poll_group_disconnect_qpair,
3044 : : .poll_group_add = nvme_tcp_poll_group_add,
3045 : : .poll_group_remove = nvme_tcp_poll_group_remove,
3046 : : .poll_group_process_completions = nvme_tcp_poll_group_process_completions,
3047 : : .poll_group_destroy = nvme_tcp_poll_group_destroy,
3048 : : .poll_group_get_stats = nvme_tcp_poll_group_get_stats,
3049 : : .poll_group_free_stats = nvme_tcp_poll_group_free_stats,
3050 : : };
3051 : :
3052 : 2382 : SPDK_NVME_TRANSPORT_REGISTER(tcp, &tcp_ops);
3053 : :
3054 : 4189 : SPDK_TRACE_REGISTER_FN(nvme_tcp, "nvme_tcp", TRACE_GROUP_NVME_TCP)
3055 : : {
3056 : 1807 : struct spdk_trace_tpoint_opts opts[] = {
3057 : : {
3058 : : "NVME_TCP_SUBMIT", TRACE_NVME_TCP_SUBMIT,
3059 : : OWNER_TYPE_NVME_TCP_QP, OBJECT_NVME_TCP_REQ, 1,
3060 : : { { "ctx", SPDK_TRACE_ARG_TYPE_PTR, 8 },
3061 : : { "cid", SPDK_TRACE_ARG_TYPE_INT, 4 },
3062 : : { "opc", SPDK_TRACE_ARG_TYPE_INT, 4 },
3063 : : { "dw10", SPDK_TRACE_ARG_TYPE_PTR, 4 },
3064 : : { "dw11", SPDK_TRACE_ARG_TYPE_PTR, 4 },
3065 : : { "dw12", SPDK_TRACE_ARG_TYPE_PTR, 4 },
3066 : : { "qd", SPDK_TRACE_ARG_TYPE_INT, 4 }
3067 : : }
3068 : : },
3069 : : {
3070 : : "NVME_TCP_COMPLETE", TRACE_NVME_TCP_COMPLETE,
3071 : : OWNER_TYPE_NVME_TCP_QP, OBJECT_NVME_TCP_REQ, 0,
3072 : : { { "ctx", SPDK_TRACE_ARG_TYPE_PTR, 8 },
3073 : : { "cid", SPDK_TRACE_ARG_TYPE_INT, 4 },
3074 : : { "cpl", SPDK_TRACE_ARG_TYPE_PTR, 4 },
3075 : : { "qd", SPDK_TRACE_ARG_TYPE_INT, 4 }
3076 : : }
3077 : : },
3078 : : };
3079 : :
3080 : 1807 : spdk_trace_register_object(OBJECT_NVME_TCP_REQ, 'p');
3081 : 1807 : spdk_trace_register_owner_type(OWNER_TYPE_NVME_TCP_QP, 'q');
3082 : 1807 : spdk_trace_register_description_ext(opts, SPDK_COUNTOF(opts));
3083 : :
3084 : 1807 : spdk_trace_tpoint_register_relation(TRACE_SOCK_REQ_QUEUE, OBJECT_NVME_TCP_REQ, 0);
3085 : 1807 : spdk_trace_tpoint_register_relation(TRACE_SOCK_REQ_PEND, OBJECT_NVME_TCP_REQ, 0);
3086 : 1807 : spdk_trace_tpoint_register_relation(TRACE_SOCK_REQ_COMPLETE, OBJECT_NVME_TCP_REQ, 0);
3087 : 1807 : }
|