xref: /spdk/lib/nvmf/tcp.c (revision 18dec401118816b7fbb2f8be2a858a5b7f10a7f1)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright (c) Intel Corporation. All rights reserved.
5  *   Copyright (c) 2019, 2020 Mellanox Technologies LTD. All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include "spdk/stdinc.h"
35 #include "spdk/crc32.h"
36 #include "spdk/endian.h"
37 #include "spdk/assert.h"
38 #include "spdk/thread.h"
39 #include "spdk/nvmf_transport.h"
40 #include "spdk/sock.h"
41 #include "spdk/string.h"
42 #include "spdk/trace.h"
43 #include "spdk/util.h"
44 
45 #include "spdk_internal/assert.h"
46 #include "spdk_internal/log.h"
47 #include "spdk_internal/nvme_tcp.h"
48 
49 #define NVMF_TCP_MAX_ACCEPT_SOCK_ONE_TIME 16
50 #define SPDK_NVMF_TCP_DEFAULT_MAX_SOCK_PRIORITY 6
51 #define SPDK_NVMF_TCP_RECV_BUF_SIZE_FACTOR 4
52 
53 const struct spdk_nvmf_transport_ops spdk_nvmf_transport_tcp;
54 
55 /* spdk nvmf related structure */
56 enum spdk_nvmf_tcp_req_state {
57 
58 	/* The request is not currently in use */
59 	TCP_REQUEST_STATE_FREE = 0,
60 
61 	/* Initial state when request first received */
62 	TCP_REQUEST_STATE_NEW,
63 
64 	/* The request is queued until a data buffer is available. */
65 	TCP_REQUEST_STATE_NEED_BUFFER,
66 
67 	/* The request is currently transferring data from the host to the controller. */
68 	TCP_REQUEST_STATE_TRANSFERRING_HOST_TO_CONTROLLER,
69 
70 	/* The request is waiting for the R2T send acknowledgement. */
71 	TCP_REQUEST_STATE_AWAITING_R2T_ACK,
72 
73 	/* The request is ready to execute at the block device */
74 	TCP_REQUEST_STATE_READY_TO_EXECUTE,
75 
76 	/* The request is currently executing at the block device */
77 	TCP_REQUEST_STATE_EXECUTING,
78 
79 	/* The request finished executing at the block device */
80 	TCP_REQUEST_STATE_EXECUTED,
81 
82 	/* The request is ready to send a completion */
83 	TCP_REQUEST_STATE_READY_TO_COMPLETE,
84 
85 	/* The request is currently transferring final pdus from the controller to the host. */
86 	TCP_REQUEST_STATE_TRANSFERRING_CONTROLLER_TO_HOST,
87 
88 	/* The request completed and can be marked free. */
89 	TCP_REQUEST_STATE_COMPLETED,
90 
91 	/* Terminator */
92 	TCP_REQUEST_NUM_STATES,
93 };
94 
95 static const char *spdk_nvmf_tcp_term_req_fes_str[] = {
96 	"Invalid PDU Header Field",
97 	"PDU Sequence Error",
98 	"Header Digiest Error",
99 	"Data Transfer Out of Range",
100 	"R2T Limit Exceeded",
101 	"Unsupported parameter",
102 };
103 
104 #define OBJECT_NVMF_TCP_IO				0x80
105 
106 #define TRACE_GROUP_NVMF_TCP				0x5
107 #define TRACE_TCP_REQUEST_STATE_NEW					SPDK_TPOINT_ID(TRACE_GROUP_NVMF_TCP, 0x0)
108 #define TRACE_TCP_REQUEST_STATE_NEED_BUFFER				SPDK_TPOINT_ID(TRACE_GROUP_NVMF_TCP, 0x1)
109 #define TRACE_TCP_REQUEST_STATE_TRANSFERRING_HOST_TO_CONTROLLER		SPDK_TPOINT_ID(TRACE_GROUP_NVMF_TCP, 0x2)
110 #define TRACE_TCP_REQUEST_STATE_READY_TO_EXECUTE			SPDK_TPOINT_ID(TRACE_GROUP_NVMF_TCP, 0x3)
111 #define TRACE_TCP_REQUEST_STATE_EXECUTING				SPDK_TPOINT_ID(TRACE_GROUP_NVMF_TCP, 0x4)
112 #define TRACE_TCP_REQUEST_STATE_EXECUTED				SPDK_TPOINT_ID(TRACE_GROUP_NVMF_TCP, 0x5)
113 #define TRACE_TCP_REQUEST_STATE_READY_TO_COMPLETE			SPDK_TPOINT_ID(TRACE_GROUP_NVMF_TCP, 0x6)
114 #define TRACE_TCP_REQUEST_STATE_TRANSFERRING_CONTROLLER_TO_HOST		SPDK_TPOINT_ID(TRACE_GROUP_NVMF_TCP, 0x7)
115 #define TRACE_TCP_REQUEST_STATE_COMPLETED				SPDK_TPOINT_ID(TRACE_GROUP_NVMF_TCP, 0x8)
116 #define TRACE_TCP_FLUSH_WRITEBUF_START					SPDK_TPOINT_ID(TRACE_GROUP_NVMF_TCP, 0x9)
117 #define TRACE_TCP_FLUSH_WRITEBUF_DONE					SPDK_TPOINT_ID(TRACE_GROUP_NVMF_TCP, 0xA)
118 #define TRACE_TCP_READ_FROM_SOCKET_DONE					SPDK_TPOINT_ID(TRACE_GROUP_NVMF_TCP, 0xB)
119 #define TRACE_TCP_REQUEST_STATE_AWAIT_R2T_ACK				SPDK_TPOINT_ID(TRACE_GROUP_NVMF_TCP, 0xC)
120 
121 SPDK_TRACE_REGISTER_FN(nvmf_tcp_trace, "nvmf_tcp", TRACE_GROUP_NVMF_TCP)
122 {
123 	spdk_trace_register_object(OBJECT_NVMF_TCP_IO, 'r');
124 	spdk_trace_register_description("TCP_REQ_NEW",
125 					TRACE_TCP_REQUEST_STATE_NEW,
126 					OWNER_NONE, OBJECT_NVMF_TCP_IO, 1, 1, "");
127 	spdk_trace_register_description("TCP_REQ_NEED_BUFFER",
128 					TRACE_TCP_REQUEST_STATE_NEED_BUFFER,
129 					OWNER_NONE, OBJECT_NVMF_TCP_IO, 0, 1, "");
130 	spdk_trace_register_description("TCP_REQ_TX_H_TO_C",
131 					TRACE_TCP_REQUEST_STATE_TRANSFERRING_HOST_TO_CONTROLLER,
132 					OWNER_NONE, OBJECT_NVMF_TCP_IO, 0, 1, "");
133 	spdk_trace_register_description("TCP_REQ_RDY_TO_EXECUTE",
134 					TRACE_TCP_REQUEST_STATE_READY_TO_EXECUTE,
135 					OWNER_NONE, OBJECT_NVMF_TCP_IO, 0, 1, "");
136 	spdk_trace_register_description("TCP_REQ_EXECUTING",
137 					TRACE_TCP_REQUEST_STATE_EXECUTING,
138 					OWNER_NONE, OBJECT_NVMF_TCP_IO, 0, 1, "");
139 	spdk_trace_register_description("TCP_REQ_EXECUTED",
140 					TRACE_TCP_REQUEST_STATE_EXECUTED,
141 					OWNER_NONE, OBJECT_NVMF_TCP_IO, 0, 1, "");
142 	spdk_trace_register_description("TCP_REQ_RDY_TO_COMPLETE",
143 					TRACE_TCP_REQUEST_STATE_READY_TO_COMPLETE,
144 					OWNER_NONE, OBJECT_NVMF_TCP_IO, 0, 1, "");
145 	spdk_trace_register_description("TCP_REQ_TRANSFER_C2H",
146 					TRACE_TCP_REQUEST_STATE_TRANSFERRING_CONTROLLER_TO_HOST,
147 					OWNER_NONE, OBJECT_NVMF_TCP_IO, 0, 1, "");
148 	spdk_trace_register_description("TCP_REQ_COMPLETED",
149 					TRACE_TCP_REQUEST_STATE_COMPLETED,
150 					OWNER_NONE, OBJECT_NVMF_TCP_IO, 0, 1, "");
151 	spdk_trace_register_description("TCP_WRITE_START",
152 					TRACE_TCP_FLUSH_WRITEBUF_START,
153 					OWNER_NONE, OBJECT_NONE, 0, 0, "");
154 	spdk_trace_register_description("TCP_WRITE_DONE",
155 					TRACE_TCP_FLUSH_WRITEBUF_DONE,
156 					OWNER_NONE, OBJECT_NONE, 0, 0, "");
157 	spdk_trace_register_description("TCP_READ_DONE",
158 					TRACE_TCP_READ_FROM_SOCKET_DONE,
159 					OWNER_NONE, OBJECT_NONE, 0, 0, "");
160 	spdk_trace_register_description("TCP_REQ_AWAIT_R2T_ACK",
161 					TRACE_TCP_REQUEST_STATE_AWAIT_R2T_ACK,
162 					OWNER_NONE, OBJECT_NVMF_TCP_IO, 0, 1, "");
163 }
164 
165 struct spdk_nvmf_tcp_req  {
166 	struct spdk_nvmf_request		req;
167 	struct spdk_nvme_cpl			rsp;
168 	struct spdk_nvme_cmd			cmd;
169 
170 	/* A PDU that can be used for sending responses. This is
171 	 * not the incoming PDU! */
172 	struct nvme_tcp_pdu			*pdu;
173 
174 	/*
175 	 * The PDU for a request may be used multiple times in serial over
176 	 * the request's lifetime. For example, first to send an R2T, then
177 	 * to send a completion. To catch mistakes where the PDU is used
178 	 * twice at the same time, add a debug flag here for init/fini.
179 	 */
180 	bool					pdu_in_use;
181 
182 	/* In-capsule data buffer */
183 	uint8_t					*buf;
184 
185 	bool					has_incapsule_data;
186 
187 	/* transfer_tag */
188 	uint16_t				ttag;
189 
190 	enum spdk_nvmf_tcp_req_state		state;
191 
192 	/*
193 	 * h2c_offset is used when we receive the h2c_data PDU.
194 	 */
195 	uint32_t				h2c_offset;
196 
197 	STAILQ_ENTRY(spdk_nvmf_tcp_req)		link;
198 	TAILQ_ENTRY(spdk_nvmf_tcp_req)		state_link;
199 };
200 
201 struct spdk_nvmf_tcp_qpair {
202 	struct spdk_nvmf_qpair			qpair;
203 	struct spdk_nvmf_tcp_poll_group		*group;
204 	struct spdk_nvmf_tcp_port		*port;
205 	struct spdk_sock			*sock;
206 
207 	enum nvme_tcp_pdu_recv_state		recv_state;
208 	enum nvme_tcp_qpair_state		state;
209 
210 	/* PDU being actively received */
211 	struct nvme_tcp_pdu			pdu_in_progress;
212 	uint32_t				recv_buf_size;
213 
214 	/* This is a spare PDU used for sending special management
215 	 * operations. Primarily, this is used for the initial
216 	 * connection response and c2h termination request. */
217 	struct nvme_tcp_pdu			mgmt_pdu;
218 
219 	TAILQ_HEAD(, nvme_tcp_pdu)		send_queue;
220 
221 	/* Arrays of in-capsule buffers, requests, and pdus.
222 	 * Each array is 'resource_count' number of elements */
223 	void					*bufs;
224 	struct spdk_nvmf_tcp_req		*reqs;
225 	struct nvme_tcp_pdu			*pdus;
226 	uint32_t				resource_count;
227 
228 	/* Queues to track the requests in all states */
229 	TAILQ_HEAD(, spdk_nvmf_tcp_req)		state_queue[TCP_REQUEST_NUM_STATES];
230 	/* Number of requests in each state */
231 	uint32_t				state_cntr[TCP_REQUEST_NUM_STATES];
232 
233 	uint8_t					cpda;
234 
235 	bool					host_hdgst_enable;
236 	bool					host_ddgst_enable;
237 
238 	/* IP address */
239 	char					initiator_addr[SPDK_NVMF_TRADDR_MAX_LEN];
240 	char					target_addr[SPDK_NVMF_TRADDR_MAX_LEN];
241 
242 	/* IP port */
243 	uint16_t				initiator_port;
244 	uint16_t				target_port;
245 
246 	/* Timer used to destroy qpair after detecting transport error issue if initiator does
247 	 *  not close the connection.
248 	 */
249 	struct spdk_poller			*timeout_poller;
250 
251 	TAILQ_ENTRY(spdk_nvmf_tcp_qpair)	link;
252 };
253 
254 struct spdk_nvmf_tcp_poll_group {
255 	struct spdk_nvmf_transport_poll_group	group;
256 	struct spdk_sock_group			*sock_group;
257 
258 	TAILQ_HEAD(, spdk_nvmf_tcp_qpair)	qpairs;
259 	TAILQ_HEAD(, spdk_nvmf_tcp_qpair)	await_req;
260 };
261 
262 struct spdk_nvmf_tcp_port {
263 	const struct spdk_nvme_transport_id	*trid;
264 	struct spdk_sock			*listen_sock;
265 	TAILQ_ENTRY(spdk_nvmf_tcp_port)		link;
266 };
267 
268 struct spdk_nvmf_tcp_transport {
269 	struct spdk_nvmf_transport		transport;
270 
271 	pthread_mutex_t				lock;
272 
273 	TAILQ_HEAD(, spdk_nvmf_tcp_port)	ports;
274 };
275 
276 static bool spdk_nvmf_tcp_req_process(struct spdk_nvmf_tcp_transport *ttransport,
277 				      struct spdk_nvmf_tcp_req *tcp_req);
278 
279 static void
280 spdk_nvmf_tcp_req_set_state(struct spdk_nvmf_tcp_req *tcp_req,
281 			    enum spdk_nvmf_tcp_req_state state)
282 {
283 	struct spdk_nvmf_qpair *qpair;
284 	struct spdk_nvmf_tcp_qpair *tqpair;
285 
286 	qpair = tcp_req->req.qpair;
287 	tqpair = SPDK_CONTAINEROF(qpair, struct spdk_nvmf_tcp_qpair, qpair);
288 
289 	TAILQ_REMOVE(&tqpair->state_queue[tcp_req->state], tcp_req, state_link);
290 	assert(tqpair->state_cntr[tcp_req->state] > 0);
291 	tqpair->state_cntr[tcp_req->state]--;
292 
293 	TAILQ_INSERT_TAIL(&tqpair->state_queue[state], tcp_req, state_link);
294 	tqpair->state_cntr[state]++;
295 
296 	tcp_req->state = state;
297 }
298 
299 static inline struct nvme_tcp_pdu *
300 nvmf_tcp_req_pdu_init(struct spdk_nvmf_tcp_req *tcp_req)
301 {
302 	assert(tcp_req->pdu_in_use == false);
303 	tcp_req->pdu_in_use = true;
304 
305 	memset(tcp_req->pdu, 0, sizeof(*tcp_req->pdu));
306 	tcp_req->pdu->qpair = SPDK_CONTAINEROF(tcp_req->req.qpair, struct spdk_nvmf_tcp_qpair, qpair);
307 
308 	return tcp_req->pdu;
309 }
310 
311 static inline void
312 nvmf_tcp_req_pdu_fini(struct spdk_nvmf_tcp_req *tcp_req)
313 {
314 	tcp_req->pdu_in_use = false;
315 }
316 
317 static struct spdk_nvmf_tcp_req *
318 spdk_nvmf_tcp_req_get(struct spdk_nvmf_tcp_qpair *tqpair)
319 {
320 	struct spdk_nvmf_tcp_req *tcp_req;
321 
322 	tcp_req = TAILQ_FIRST(&tqpair->state_queue[TCP_REQUEST_STATE_FREE]);
323 	if (!tcp_req) {
324 		return NULL;
325 	}
326 
327 	memset(&tcp_req->rsp, 0, sizeof(tcp_req->rsp));
328 	tcp_req->h2c_offset = 0;
329 	tcp_req->has_incapsule_data = false;
330 	tcp_req->req.dif.dif_insert_or_strip = false;
331 
332 	spdk_nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_NEW);
333 	return tcp_req;
334 }
335 
336 static void
337 nvmf_tcp_request_free(struct spdk_nvmf_tcp_req *tcp_req)
338 {
339 	struct spdk_nvmf_tcp_transport *ttransport;
340 
341 	assert(tcp_req != NULL);
342 
343 	SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "tcp_req=%p will be freed\n", tcp_req);
344 	ttransport = SPDK_CONTAINEROF(tcp_req->req.qpair->transport,
345 				      struct spdk_nvmf_tcp_transport, transport);
346 	spdk_nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_COMPLETED);
347 	spdk_nvmf_tcp_req_process(ttransport, tcp_req);
348 }
349 
350 static int
351 spdk_nvmf_tcp_req_free(struct spdk_nvmf_request *req)
352 {
353 	struct spdk_nvmf_tcp_req *tcp_req = SPDK_CONTAINEROF(req, struct spdk_nvmf_tcp_req, req);
354 
355 	nvmf_tcp_request_free(tcp_req);
356 
357 	return 0;
358 }
359 
360 static void
361 spdk_nvmf_tcp_drain_state_queue(struct spdk_nvmf_tcp_qpair *tqpair,
362 				enum spdk_nvmf_tcp_req_state state)
363 {
364 	struct spdk_nvmf_tcp_req *tcp_req, *req_tmp;
365 
366 	TAILQ_FOREACH_SAFE(tcp_req, &tqpair->state_queue[state], state_link, req_tmp) {
367 		nvmf_tcp_request_free(tcp_req);
368 	}
369 }
370 
371 static void
372 spdk_nvmf_tcp_cleanup_all_states(struct spdk_nvmf_tcp_qpair *tqpair)
373 {
374 	struct spdk_nvmf_tcp_req *tcp_req, *req_tmp;
375 
376 	assert(TAILQ_EMPTY(&tqpair->send_queue));
377 
378 	spdk_nvmf_tcp_drain_state_queue(tqpair, TCP_REQUEST_STATE_TRANSFERRING_CONTROLLER_TO_HOST);
379 	spdk_nvmf_tcp_drain_state_queue(tqpair, TCP_REQUEST_STATE_NEW);
380 
381 	/* Wipe the requests waiting for buffer from the global list */
382 	TAILQ_FOREACH_SAFE(tcp_req, &tqpair->state_queue[TCP_REQUEST_STATE_NEED_BUFFER], state_link,
383 			   req_tmp) {
384 		STAILQ_REMOVE(&tqpair->group->group.pending_buf_queue, &tcp_req->req,
385 			      spdk_nvmf_request, buf_link);
386 	}
387 
388 	spdk_nvmf_tcp_drain_state_queue(tqpair, TCP_REQUEST_STATE_NEED_BUFFER);
389 	spdk_nvmf_tcp_drain_state_queue(tqpair, TCP_REQUEST_STATE_EXECUTING);
390 	spdk_nvmf_tcp_drain_state_queue(tqpair, TCP_REQUEST_STATE_TRANSFERRING_HOST_TO_CONTROLLER);
391 	spdk_nvmf_tcp_drain_state_queue(tqpair, TCP_REQUEST_STATE_AWAITING_R2T_ACK);
392 }
393 
394 static void
395 nvmf_tcp_dump_qpair_req_contents(struct spdk_nvmf_tcp_qpair *tqpair)
396 {
397 	int i;
398 	struct spdk_nvmf_tcp_req *tcp_req;
399 
400 	SPDK_ERRLOG("Dumping contents of queue pair (QID %d)\n", tqpair->qpair.qid);
401 	for (i = 1; i < TCP_REQUEST_NUM_STATES; i++) {
402 		SPDK_ERRLOG("\tNum of requests in state[%d] = %u\n", i, tqpair->state_cntr[i]);
403 		TAILQ_FOREACH(tcp_req, &tqpair->state_queue[i], state_link) {
404 			SPDK_ERRLOG("\t\tRequest Data From Pool: %d\n", tcp_req->req.data_from_pool);
405 			SPDK_ERRLOG("\t\tRequest opcode: %d\n", tcp_req->req.cmd->nvmf_cmd.opcode);
406 		}
407 	}
408 }
409 
410 static void
411 spdk_nvmf_tcp_qpair_destroy(struct spdk_nvmf_tcp_qpair *tqpair)
412 {
413 	int err = 0;
414 
415 	SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "enter\n");
416 
417 	err = spdk_sock_close(&tqpair->sock);
418 	assert(err == 0);
419 	spdk_nvmf_tcp_cleanup_all_states(tqpair);
420 
421 	if (tqpair->state_cntr[TCP_REQUEST_STATE_FREE] != tqpair->resource_count) {
422 		SPDK_ERRLOG("tqpair(%p) free tcp request num is %u but should be %u\n", tqpair,
423 			    tqpair->state_cntr[TCP_REQUEST_STATE_FREE],
424 			    tqpair->resource_count);
425 		err++;
426 	}
427 
428 	if (err > 0) {
429 		nvmf_tcp_dump_qpair_req_contents(tqpair);
430 	}
431 
432 	spdk_dma_free(tqpair->pdus);
433 	free(tqpair->reqs);
434 	spdk_free(tqpair->bufs);
435 	free(tqpair);
436 	SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "Leave\n");
437 }
438 
439 static int
440 spdk_nvmf_tcp_destroy(struct spdk_nvmf_transport *transport)
441 {
442 	struct spdk_nvmf_tcp_transport	*ttransport;
443 
444 	assert(transport != NULL);
445 	ttransport = SPDK_CONTAINEROF(transport, struct spdk_nvmf_tcp_transport, transport);
446 
447 	pthread_mutex_destroy(&ttransport->lock);
448 	free(ttransport);
449 	return 0;
450 }
451 
452 static struct spdk_nvmf_transport *
453 spdk_nvmf_tcp_create(struct spdk_nvmf_transport_opts *opts)
454 {
455 	struct spdk_nvmf_tcp_transport *ttransport;
456 	uint32_t sge_count;
457 	uint32_t min_shared_buffers;
458 
459 	ttransport = calloc(1, sizeof(*ttransport));
460 	if (!ttransport) {
461 		return NULL;
462 	}
463 
464 	TAILQ_INIT(&ttransport->ports);
465 
466 	ttransport->transport.ops = &spdk_nvmf_transport_tcp;
467 
468 	SPDK_NOTICELOG("*** TCP Transport Init ***\n");
469 
470 	SPDK_INFOLOG(SPDK_LOG_NVMF_TCP, "*** TCP Transport Init ***\n"
471 		     "  Transport opts:  max_ioq_depth=%d, max_io_size=%d,\n"
472 		     "  max_qpairs_per_ctrlr=%d, io_unit_size=%d,\n"
473 		     "  in_capsule_data_size=%d, max_aq_depth=%d\n"
474 		     "  num_shared_buffers=%d, c2h_success=%d,\n"
475 		     "  dif_insert_or_strip=%d, sock_priority=%d\n",
476 		     opts->max_queue_depth,
477 		     opts->max_io_size,
478 		     opts->max_qpairs_per_ctrlr,
479 		     opts->io_unit_size,
480 		     opts->in_capsule_data_size,
481 		     opts->max_aq_depth,
482 		     opts->num_shared_buffers,
483 		     opts->c2h_success,
484 		     opts->dif_insert_or_strip,
485 		     opts->sock_priority);
486 
487 	if (opts->sock_priority > SPDK_NVMF_TCP_DEFAULT_MAX_SOCK_PRIORITY) {
488 		SPDK_ERRLOG("Unsupported socket_priority=%d, the current range is: 0 to %d\n"
489 			    "you can use man 7 socket to view the range of priority under SO_PRIORITY item\n",
490 			    opts->sock_priority, SPDK_NVMF_TCP_DEFAULT_MAX_SOCK_PRIORITY);
491 		free(ttransport);
492 		return NULL;
493 	}
494 
495 	/* I/O unit size cannot be larger than max I/O size */
496 	if (opts->io_unit_size > opts->max_io_size) {
497 		opts->io_unit_size = opts->max_io_size;
498 	}
499 
500 	sge_count = opts->max_io_size / opts->io_unit_size;
501 	if (sge_count > SPDK_NVMF_MAX_SGL_ENTRIES) {
502 		SPDK_ERRLOG("Unsupported IO Unit size specified, %d bytes\n", opts->io_unit_size);
503 		free(ttransport);
504 		return NULL;
505 	}
506 
507 	min_shared_buffers = spdk_thread_get_count() * opts->buf_cache_size;
508 	if (min_shared_buffers > opts->num_shared_buffers) {
509 		SPDK_ERRLOG("There are not enough buffers to satisfy"
510 			    "per-poll group caches for each thread. (%" PRIu32 ")"
511 			    "supplied. (%" PRIu32 ") required\n", opts->num_shared_buffers, min_shared_buffers);
512 		SPDK_ERRLOG("Please specify a larger number of shared buffers\n");
513 		spdk_nvmf_tcp_destroy(&ttransport->transport);
514 		return NULL;
515 	}
516 
517 	pthread_mutex_init(&ttransport->lock, NULL);
518 
519 	return &ttransport->transport;
520 }
521 
522 static int
523 _spdk_nvmf_tcp_trsvcid_to_int(const char *trsvcid)
524 {
525 	unsigned long long ull;
526 	char *end = NULL;
527 
528 	ull = strtoull(trsvcid, &end, 10);
529 	if (end == NULL || end == trsvcid || *end != '\0') {
530 		return -1;
531 	}
532 
533 	/* Valid TCP/IP port numbers are in [0, 65535] */
534 	if (ull > 65535) {
535 		return -1;
536 	}
537 
538 	return (int)ull;
539 }
540 
541 /**
542  * Canonicalize a listen address trid.
543  */
544 static int
545 _spdk_nvmf_tcp_canon_listen_trid(struct spdk_nvme_transport_id *canon_trid,
546 				 const struct spdk_nvme_transport_id *trid)
547 {
548 	int trsvcid_int;
549 
550 	trsvcid_int = _spdk_nvmf_tcp_trsvcid_to_int(trid->trsvcid);
551 	if (trsvcid_int < 0) {
552 		return -EINVAL;
553 	}
554 
555 	memset(canon_trid, 0, sizeof(*canon_trid));
556 	spdk_nvme_trid_populate_transport(canon_trid, SPDK_NVME_TRANSPORT_TCP);
557 	canon_trid->adrfam = trid->adrfam;
558 	snprintf(canon_trid->traddr, sizeof(canon_trid->traddr), "%s", trid->traddr);
559 	snprintf(canon_trid->trsvcid, sizeof(canon_trid->trsvcid), "%d", trsvcid_int);
560 
561 	return 0;
562 }
563 
564 /**
565  * Find an existing listening port.
566  *
567  * Caller must hold ttransport->lock.
568  */
569 static struct spdk_nvmf_tcp_port *
570 _spdk_nvmf_tcp_find_port(struct spdk_nvmf_tcp_transport *ttransport,
571 			 const struct spdk_nvme_transport_id *trid)
572 {
573 	struct spdk_nvme_transport_id canon_trid;
574 	struct spdk_nvmf_tcp_port *port;
575 
576 	if (_spdk_nvmf_tcp_canon_listen_trid(&canon_trid, trid) != 0) {
577 		return NULL;
578 	}
579 
580 	TAILQ_FOREACH(port, &ttransport->ports, link) {
581 		if (spdk_nvme_transport_id_compare(&canon_trid, port->trid) == 0) {
582 			return port;
583 		}
584 	}
585 
586 	return NULL;
587 }
588 
589 static int
590 spdk_nvmf_tcp_listen(struct spdk_nvmf_transport *transport,
591 		     const struct spdk_nvme_transport_id *trid)
592 {
593 	struct spdk_nvmf_tcp_transport *ttransport;
594 	struct spdk_nvmf_tcp_port *port;
595 	int trsvcid_int;
596 	uint8_t adrfam;
597 
598 	ttransport = SPDK_CONTAINEROF(transport, struct spdk_nvmf_tcp_transport, transport);
599 
600 	trsvcid_int = _spdk_nvmf_tcp_trsvcid_to_int(trid->trsvcid);
601 	if (trsvcid_int < 0) {
602 		SPDK_ERRLOG("Invalid trsvcid '%s'\n", trid->trsvcid);
603 		return -EINVAL;
604 	}
605 
606 	pthread_mutex_lock(&ttransport->lock);
607 	port = calloc(1, sizeof(*port));
608 	if (!port) {
609 		SPDK_ERRLOG("Port allocation failed\n");
610 		pthread_mutex_unlock(&ttransport->lock);
611 		return -ENOMEM;
612 	}
613 
614 	port->trid = trid;
615 	port->listen_sock = spdk_sock_listen(trid->traddr, trsvcid_int, NULL);
616 	if (port->listen_sock == NULL) {
617 		SPDK_ERRLOG("spdk_sock_listen(%s, %d) failed: %s (%d)\n",
618 			    trid->traddr, trsvcid_int,
619 			    spdk_strerror(errno), errno);
620 		free(port);
621 		pthread_mutex_unlock(&ttransport->lock);
622 		return -errno;
623 	}
624 
625 	if (spdk_sock_is_ipv4(port->listen_sock)) {
626 		adrfam = SPDK_NVMF_ADRFAM_IPV4;
627 	} else if (spdk_sock_is_ipv6(port->listen_sock)) {
628 		adrfam = SPDK_NVMF_ADRFAM_IPV6;
629 	} else {
630 		SPDK_ERRLOG("Unhandled socket type\n");
631 		adrfam = 0;
632 	}
633 
634 	if (adrfam != trid->adrfam) {
635 		SPDK_ERRLOG("Socket address family mismatch\n");
636 		spdk_sock_close(&port->listen_sock);
637 		free(port);
638 		pthread_mutex_unlock(&ttransport->lock);
639 		return -EINVAL;
640 	}
641 
642 	SPDK_NOTICELOG("*** NVMe/TCP Target Listening on %s port %s ***\n",
643 		       trid->traddr, trid->trsvcid);
644 
645 	TAILQ_INSERT_TAIL(&ttransport->ports, port, link);
646 	pthread_mutex_unlock(&ttransport->lock);
647 	return 0;
648 }
649 
650 static void
651 spdk_nvmf_tcp_stop_listen(struct spdk_nvmf_transport *transport,
652 			  const struct spdk_nvme_transport_id *trid)
653 {
654 	struct spdk_nvmf_tcp_transport *ttransport;
655 	struct spdk_nvmf_tcp_port *port;
656 
657 	ttransport = SPDK_CONTAINEROF(transport, struct spdk_nvmf_tcp_transport, transport);
658 
659 	SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "Removing listen address %s port %s\n",
660 		      trid->traddr, trid->trsvcid);
661 
662 	pthread_mutex_lock(&ttransport->lock);
663 	port = _spdk_nvmf_tcp_find_port(ttransport, trid);
664 	if (port) {
665 		TAILQ_REMOVE(&ttransport->ports, port, link);
666 		spdk_sock_close(&port->listen_sock);
667 		free(port);
668 	}
669 
670 	pthread_mutex_unlock(&ttransport->lock);
671 }
672 
673 static void spdk_nvmf_tcp_qpair_set_recv_state(struct spdk_nvmf_tcp_qpair *tqpair,
674 		enum nvme_tcp_pdu_recv_state state);
675 
676 static void
677 spdk_nvmf_tcp_qpair_disconnect(struct spdk_nvmf_tcp_qpair *tqpair)
678 {
679 	SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "Disconnecting qpair %p\n", tqpair);
680 
681 	if (tqpair->state <= NVME_TCP_QPAIR_STATE_RUNNING) {
682 		tqpair->state = NVME_TCP_QPAIR_STATE_EXITING;
683 		spdk_nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_ERROR);
684 		spdk_poller_unregister(&tqpair->timeout_poller);
685 
686 		/* This will end up calling spdk_nvmf_tcp_close_qpair */
687 		spdk_nvmf_qpair_disconnect(&tqpair->qpair, NULL, NULL);
688 	}
689 }
690 
691 static void
692 _pdu_write_done(void *_pdu, int err)
693 {
694 	struct nvme_tcp_pdu			*pdu = _pdu;
695 	struct spdk_nvmf_tcp_qpair		*tqpair = pdu->qpair;
696 
697 	TAILQ_REMOVE(&tqpair->send_queue, pdu, tailq);
698 
699 	if (err != 0) {
700 		spdk_nvmf_tcp_qpair_disconnect(tqpair);
701 		return;
702 	}
703 
704 	assert(pdu->cb_fn != NULL);
705 	pdu->cb_fn(pdu->cb_arg);
706 }
707 
708 static void
709 spdk_nvmf_tcp_qpair_write_pdu(struct spdk_nvmf_tcp_qpair *tqpair,
710 			      struct nvme_tcp_pdu *pdu,
711 			      nvme_tcp_qpair_xfer_complete_cb cb_fn,
712 			      void *cb_arg)
713 {
714 	int hlen;
715 	uint32_t crc32c;
716 	uint32_t mapped_length = 0;
717 	ssize_t rc;
718 
719 	assert(&tqpair->pdu_in_progress != pdu);
720 
721 	hlen = pdu->hdr.common.hlen;
722 
723 	/* Header Digest */
724 	if (g_nvme_tcp_hdgst[pdu->hdr.common.pdu_type] && tqpair->host_hdgst_enable) {
725 		crc32c = nvme_tcp_pdu_calc_header_digest(pdu);
726 		MAKE_DIGEST_WORD((uint8_t *)pdu->hdr.raw + hlen, crc32c);
727 	}
728 
729 	/* Data Digest */
730 	if (pdu->data_len > 0 && g_nvme_tcp_ddgst[pdu->hdr.common.pdu_type] && tqpair->host_ddgst_enable) {
731 		crc32c = nvme_tcp_pdu_calc_data_digest(pdu);
732 		MAKE_DIGEST_WORD(pdu->data_digest, crc32c);
733 	}
734 
735 	pdu->cb_fn = cb_fn;
736 	pdu->cb_arg = cb_arg;
737 
738 	pdu->sock_req.iovcnt = nvme_tcp_build_iovs(pdu->iov, SPDK_COUNTOF(pdu->iov), pdu,
739 			       tqpair->host_hdgst_enable, tqpair->host_ddgst_enable,
740 			       &mapped_length);
741 	pdu->sock_req.cb_fn = _pdu_write_done;
742 	pdu->sock_req.cb_arg = pdu;
743 	TAILQ_INSERT_TAIL(&tqpair->send_queue, pdu, tailq);
744 	if (pdu->hdr.common.pdu_type == SPDK_NVME_TCP_PDU_TYPE_IC_RESP ||
745 	    pdu->hdr.common.pdu_type == SPDK_NVME_TCP_PDU_TYPE_C2H_TERM_REQ) {
746 		rc = spdk_sock_writev(tqpair->sock, pdu->iov, pdu->sock_req.iovcnt);
747 		if (rc == mapped_length) {
748 			_pdu_write_done(pdu, 0);
749 		} else {
750 			SPDK_ERRLOG("IC_RESP or TERM_REQ could not write to socket.\n");
751 			_pdu_write_done(pdu, -1);
752 		}
753 	} else {
754 		spdk_sock_writev_async(tqpair->sock, &pdu->sock_req);
755 	}
756 }
757 
758 static int
759 spdk_nvmf_tcp_qpair_init_mem_resource(struct spdk_nvmf_tcp_qpair *tqpair)
760 {
761 	uint32_t i;
762 	struct spdk_nvmf_transport_opts *opts;
763 	uint32_t in_capsule_data_size;
764 
765 	opts = &tqpair->qpair.transport->opts;
766 
767 	in_capsule_data_size = opts->in_capsule_data_size;
768 	if (opts->dif_insert_or_strip) {
769 		in_capsule_data_size = SPDK_BDEV_BUF_SIZE_WITH_MD(in_capsule_data_size);
770 	}
771 
772 	tqpair->resource_count = opts->max_queue_depth;
773 
774 	tqpair->mgmt_pdu.qpair = tqpair;
775 
776 	tqpair->reqs = calloc(tqpair->resource_count, sizeof(*tqpair->reqs));
777 	if (!tqpair->reqs) {
778 		SPDK_ERRLOG("Unable to allocate reqs on tqpair=%p\n", tqpair);
779 		return -1;
780 	}
781 
782 	if (in_capsule_data_size) {
783 		tqpair->bufs = spdk_zmalloc(tqpair->resource_count * in_capsule_data_size, 0x1000,
784 					    NULL, SPDK_ENV_LCORE_ID_ANY,
785 					    SPDK_MALLOC_DMA);
786 		if (!tqpair->bufs) {
787 			SPDK_ERRLOG("Unable to allocate bufs on tqpair=%p.\n", tqpair);
788 			return -1;
789 		}
790 	}
791 
792 	tqpair->pdus = spdk_dma_malloc(tqpair->resource_count * sizeof(*tqpair->pdus), 0x1000, NULL);
793 	if (!tqpair->pdus) {
794 		SPDK_ERRLOG("Unable to allocate pdu pool on tqpair =%p.\n", tqpair);
795 		return -1;
796 	}
797 
798 	for (i = 0; i < tqpair->resource_count; i++) {
799 		struct spdk_nvmf_tcp_req *tcp_req = &tqpair->reqs[i];
800 
801 		tcp_req->ttag = i + 1;
802 		tcp_req->req.qpair = &tqpair->qpair;
803 
804 		tcp_req->pdu = &tqpair->pdus[i];
805 		tcp_req->pdu->qpair = tqpair;
806 
807 		/* Set up memory to receive commands */
808 		if (tqpair->bufs) {
809 			tcp_req->buf = (void *)((uintptr_t)tqpair->bufs + (i * in_capsule_data_size));
810 		}
811 
812 		/* Set the cmdn and rsp */
813 		tcp_req->req.rsp = (union nvmf_c2h_msg *)&tcp_req->rsp;
814 		tcp_req->req.cmd = (union nvmf_h2c_msg *)&tcp_req->cmd;
815 
816 		/* Initialize request state to FREE */
817 		tcp_req->state = TCP_REQUEST_STATE_FREE;
818 		TAILQ_INSERT_TAIL(&tqpair->state_queue[tcp_req->state], tcp_req, state_link);
819 		tqpair->state_cntr[TCP_REQUEST_STATE_FREE]++;
820 	}
821 
822 	tqpair->recv_buf_size = (in_capsule_data_size + sizeof(struct spdk_nvme_tcp_cmd) + 2 *
823 				 SPDK_NVME_TCP_DIGEST_LEN) * SPDK_NVMF_TCP_RECV_BUF_SIZE_FACTOR;
824 
825 	return 0;
826 }
827 
828 static int
829 spdk_nvmf_tcp_qpair_init(struct spdk_nvmf_qpair *qpair)
830 {
831 	struct spdk_nvmf_tcp_qpair *tqpair;
832 	int i;
833 
834 	tqpair = SPDK_CONTAINEROF(qpair, struct spdk_nvmf_tcp_qpair, qpair);
835 
836 	SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "New TCP Connection: %p\n", qpair);
837 
838 	TAILQ_INIT(&tqpair->send_queue);
839 
840 	/* Initialise request state queues of the qpair */
841 	for (i = TCP_REQUEST_STATE_FREE; i < TCP_REQUEST_NUM_STATES; i++) {
842 		TAILQ_INIT(&tqpair->state_queue[i]);
843 	}
844 
845 	tqpair->host_hdgst_enable = true;
846 	tqpair->host_ddgst_enable = true;
847 
848 	return 0;
849 }
850 
851 static int
852 spdk_nvmf_tcp_qpair_sock_init(struct spdk_nvmf_tcp_qpair *tqpair)
853 {
854 	int rc;
855 
856 	/* set low water mark */
857 	rc = spdk_sock_set_recvlowat(tqpair->sock, sizeof(struct spdk_nvme_tcp_c2h_data_hdr));
858 	if (rc != 0) {
859 		SPDK_ERRLOG("spdk_sock_set_recvlowat() failed\n");
860 		return rc;
861 	}
862 
863 	return 0;
864 }
865 
866 static void
867 _spdk_nvmf_tcp_handle_connect(struct spdk_nvmf_transport *transport,
868 			      struct spdk_nvmf_tcp_port *port,
869 			      struct spdk_sock *sock,
870 			      new_qpair_fn cb_fn, void *cb_arg)
871 {
872 	struct spdk_nvmf_tcp_qpair *tqpair;
873 	int rc;
874 
875 	SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "New connection accepted on %s port %s\n",
876 		      port->trid->traddr, port->trid->trsvcid);
877 
878 	if (transport->opts.sock_priority) {
879 		rc = spdk_sock_set_priority(sock, transport->opts.sock_priority);
880 		if (rc) {
881 			SPDK_ERRLOG("Failed to set the priority of the socket\n");
882 			spdk_sock_close(&sock);
883 			return;
884 		}
885 	}
886 
887 	tqpair = calloc(1, sizeof(struct spdk_nvmf_tcp_qpair));
888 	if (tqpair == NULL) {
889 		SPDK_ERRLOG("Could not allocate new connection.\n");
890 		spdk_sock_close(&sock);
891 		return;
892 	}
893 
894 	tqpair->sock = sock;
895 	tqpair->state_cntr[TCP_REQUEST_STATE_FREE] = 0;
896 	tqpair->port = port;
897 	tqpair->qpair.transport = transport;
898 
899 	rc = spdk_sock_getaddr(tqpair->sock, tqpair->target_addr,
900 			       sizeof(tqpair->target_addr), &tqpair->target_port,
901 			       tqpair->initiator_addr, sizeof(tqpair->initiator_addr),
902 			       &tqpair->initiator_port);
903 	if (rc < 0) {
904 		SPDK_ERRLOG("spdk_sock_getaddr() failed of tqpair=%p\n", tqpair);
905 		spdk_nvmf_tcp_qpair_destroy(tqpair);
906 		return;
907 	}
908 
909 	cb_fn(&tqpair->qpair, cb_arg);
910 }
911 
912 static void
913 spdk_nvmf_tcp_port_accept(struct spdk_nvmf_transport *transport, struct spdk_nvmf_tcp_port *port,
914 			  new_qpair_fn cb_fn, void *cb_arg)
915 {
916 	struct spdk_sock *sock;
917 	int i;
918 
919 	for (i = 0; i < NVMF_TCP_MAX_ACCEPT_SOCK_ONE_TIME; i++) {
920 		sock = spdk_sock_accept(port->listen_sock);
921 		if (sock == NULL) {
922 			break;
923 		}
924 		_spdk_nvmf_tcp_handle_connect(transport, port, sock, cb_fn, cb_arg);
925 	}
926 }
927 
928 static void
929 spdk_nvmf_tcp_accept(struct spdk_nvmf_transport *transport, new_qpair_fn cb_fn, void *cb_arg)
930 {
931 	struct spdk_nvmf_tcp_transport *ttransport;
932 	struct spdk_nvmf_tcp_port *port;
933 
934 	ttransport = SPDK_CONTAINEROF(transport, struct spdk_nvmf_tcp_transport, transport);
935 
936 	TAILQ_FOREACH(port, &ttransport->ports, link) {
937 		spdk_nvmf_tcp_port_accept(transport, port, cb_fn, cb_arg);
938 	}
939 }
940 
941 static void
942 spdk_nvmf_tcp_discover(struct spdk_nvmf_transport *transport,
943 		       struct spdk_nvme_transport_id *trid,
944 		       struct spdk_nvmf_discovery_log_page_entry *entry)
945 {
946 	entry->trtype = SPDK_NVMF_TRTYPE_TCP;
947 	entry->adrfam = trid->adrfam;
948 	entry->treq.secure_channel = SPDK_NVMF_TREQ_SECURE_CHANNEL_NOT_REQUIRED;
949 
950 	spdk_strcpy_pad(entry->trsvcid, trid->trsvcid, sizeof(entry->trsvcid), ' ');
951 	spdk_strcpy_pad(entry->traddr, trid->traddr, sizeof(entry->traddr), ' ');
952 
953 	entry->tsas.tcp.sectype = SPDK_NVME_TCP_SECURITY_NONE;
954 }
955 
956 static struct spdk_nvmf_transport_poll_group *
957 spdk_nvmf_tcp_poll_group_create(struct spdk_nvmf_transport *transport)
958 {
959 	struct spdk_nvmf_tcp_poll_group *tgroup;
960 
961 	tgroup = calloc(1, sizeof(*tgroup));
962 	if (!tgroup) {
963 		return NULL;
964 	}
965 
966 	tgroup->sock_group = spdk_sock_group_create(&tgroup->group);
967 	if (!tgroup->sock_group) {
968 		goto cleanup;
969 	}
970 
971 	TAILQ_INIT(&tgroup->qpairs);
972 	TAILQ_INIT(&tgroup->await_req);
973 
974 	return &tgroup->group;
975 
976 cleanup:
977 	free(tgroup);
978 	return NULL;
979 }
980 
981 static struct spdk_nvmf_transport_poll_group *
982 spdk_nvmf_tcp_get_optimal_poll_group(struct spdk_nvmf_qpair *qpair)
983 {
984 	struct spdk_nvmf_tcp_qpair *tqpair;
985 	struct spdk_sock_group *group = NULL;
986 	int rc;
987 
988 	tqpair = SPDK_CONTAINEROF(qpair, struct spdk_nvmf_tcp_qpair, qpair);
989 	rc = spdk_sock_get_optimal_sock_group(tqpair->sock, &group);
990 	if (!rc && group != NULL) {
991 		return spdk_sock_group_get_ctx(group);
992 	}
993 
994 	return NULL;
995 }
996 
997 static void
998 spdk_nvmf_tcp_poll_group_destroy(struct spdk_nvmf_transport_poll_group *group)
999 {
1000 	struct spdk_nvmf_tcp_poll_group *tgroup;
1001 
1002 	tgroup = SPDK_CONTAINEROF(group, struct spdk_nvmf_tcp_poll_group, group);
1003 	spdk_sock_group_close(&tgroup->sock_group);
1004 
1005 	free(tgroup);
1006 }
1007 
1008 static void
1009 spdk_nvmf_tcp_qpair_set_recv_state(struct spdk_nvmf_tcp_qpair *tqpair,
1010 				   enum nvme_tcp_pdu_recv_state state)
1011 {
1012 	if (tqpair->recv_state == state) {
1013 		SPDK_ERRLOG("The recv state of tqpair=%p is same with the state(%d) to be set\n",
1014 			    tqpair, state);
1015 		return;
1016 	}
1017 
1018 	if (tqpair->recv_state == NVME_TCP_PDU_RECV_STATE_AWAIT_REQ) {
1019 		/* When leaving the await req state, move the qpair to the main list */
1020 		TAILQ_REMOVE(&tqpair->group->await_req, tqpair, link);
1021 		TAILQ_INSERT_TAIL(&tqpair->group->qpairs, tqpair, link);
1022 	}
1023 
1024 	SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "tqpair(%p) recv state=%d\n", tqpair, state);
1025 	tqpair->recv_state = state;
1026 
1027 	switch (state) {
1028 	case NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_CH:
1029 	case NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_PSH:
1030 	case NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_PAYLOAD:
1031 		break;
1032 	case NVME_TCP_PDU_RECV_STATE_AWAIT_REQ:
1033 		TAILQ_REMOVE(&tqpair->group->qpairs, tqpair, link);
1034 		TAILQ_INSERT_TAIL(&tqpair->group->await_req, tqpair, link);
1035 		break;
1036 	case NVME_TCP_PDU_RECV_STATE_ERROR:
1037 	case NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_READY:
1038 		memset(&tqpair->pdu_in_progress, 0, sizeof(tqpair->pdu_in_progress));
1039 		break;
1040 	default:
1041 		SPDK_ERRLOG("The state(%d) is invalid\n", state);
1042 		abort();
1043 		break;
1044 	}
1045 }
1046 
1047 static int
1048 spdk_nvmf_tcp_qpair_handle_timeout(void *ctx)
1049 {
1050 	struct spdk_nvmf_tcp_qpair *tqpair = ctx;
1051 
1052 	assert(tqpair->recv_state == NVME_TCP_PDU_RECV_STATE_ERROR);
1053 
1054 	SPDK_ERRLOG("No pdu coming for tqpair=%p within %d seconds\n", tqpair,
1055 		    SPDK_NVME_TCP_QPAIR_EXIT_TIMEOUT);
1056 
1057 	spdk_nvmf_tcp_qpair_disconnect(tqpair);
1058 	return 0;
1059 }
1060 
1061 static void
1062 spdk_nvmf_tcp_send_c2h_term_req_complete(void *cb_arg)
1063 {
1064 	struct spdk_nvmf_tcp_qpair *tqpair = (struct spdk_nvmf_tcp_qpair *)cb_arg;
1065 
1066 	if (!tqpair->timeout_poller) {
1067 		tqpair->timeout_poller = SPDK_POLLER_REGISTER(spdk_nvmf_tcp_qpair_handle_timeout, tqpair,
1068 					 SPDK_NVME_TCP_QPAIR_EXIT_TIMEOUT * 1000000);
1069 	}
1070 }
1071 
1072 static void
1073 spdk_nvmf_tcp_send_c2h_term_req(struct spdk_nvmf_tcp_qpair *tqpair, struct nvme_tcp_pdu *pdu,
1074 				enum spdk_nvme_tcp_term_req_fes fes, uint32_t error_offset)
1075 {
1076 	struct nvme_tcp_pdu *rsp_pdu;
1077 	struct spdk_nvme_tcp_term_req_hdr *c2h_term_req;
1078 	uint32_t c2h_term_req_hdr_len = sizeof(*c2h_term_req);
1079 	uint32_t copy_len;
1080 
1081 	rsp_pdu = &tqpair->mgmt_pdu;
1082 
1083 	c2h_term_req = &rsp_pdu->hdr.term_req;
1084 	c2h_term_req->common.pdu_type = SPDK_NVME_TCP_PDU_TYPE_C2H_TERM_REQ;
1085 	c2h_term_req->common.hlen = c2h_term_req_hdr_len;
1086 
1087 	if ((fes == SPDK_NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD) ||
1088 	    (fes == SPDK_NVME_TCP_TERM_REQ_FES_INVALID_DATA_UNSUPPORTED_PARAMETER)) {
1089 		DSET32(&c2h_term_req->fei, error_offset);
1090 	}
1091 
1092 	copy_len = spdk_min(pdu->hdr.common.hlen, SPDK_NVME_TCP_TERM_REQ_ERROR_DATA_MAX_SIZE);
1093 
1094 	/* Copy the error info into the buffer */
1095 	memcpy((uint8_t *)rsp_pdu->hdr.raw + c2h_term_req_hdr_len, pdu->hdr.raw, copy_len);
1096 	nvme_tcp_pdu_set_data(rsp_pdu, (uint8_t *)rsp_pdu->hdr.raw + c2h_term_req_hdr_len, copy_len);
1097 
1098 	/* Contain the header of the wrong received pdu */
1099 	c2h_term_req->common.plen = c2h_term_req->common.hlen + copy_len;
1100 	spdk_nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_ERROR);
1101 	spdk_nvmf_tcp_qpair_write_pdu(tqpair, rsp_pdu, spdk_nvmf_tcp_send_c2h_term_req_complete, tqpair);
1102 }
1103 
1104 static void
1105 spdk_nvmf_tcp_capsule_cmd_hdr_handle(struct spdk_nvmf_tcp_transport *ttransport,
1106 				     struct spdk_nvmf_tcp_qpair *tqpair,
1107 				     struct nvme_tcp_pdu *pdu)
1108 {
1109 	struct spdk_nvmf_tcp_req *tcp_req;
1110 
1111 	assert(pdu->psh_valid_bytes == pdu->psh_len);
1112 	assert(pdu->hdr.common.pdu_type == SPDK_NVME_TCP_PDU_TYPE_CAPSULE_CMD);
1113 
1114 	tcp_req = spdk_nvmf_tcp_req_get(tqpair);
1115 	if (!tcp_req) {
1116 		/* Directly return and make the allocation retry again */
1117 		if (tqpair->state_cntr[TCP_REQUEST_STATE_TRANSFERRING_CONTROLLER_TO_HOST] > 0) {
1118 			return;
1119 		}
1120 
1121 		/* The host sent more commands than the maximum queue depth. */
1122 		SPDK_ERRLOG("Cannot allocate tcp_req on tqpair=%p\n", tqpair);
1123 		spdk_nvmf_tcp_qpair_disconnect(tqpair);
1124 		return;
1125 	}
1126 
1127 	pdu->req = tcp_req;
1128 	assert(tcp_req->state == TCP_REQUEST_STATE_NEW);
1129 	spdk_nvmf_tcp_req_process(ttransport, tcp_req);
1130 }
1131 
1132 static void
1133 spdk_nvmf_tcp_capsule_cmd_payload_handle(struct spdk_nvmf_tcp_transport *ttransport,
1134 		struct spdk_nvmf_tcp_qpair *tqpair,
1135 		struct nvme_tcp_pdu *pdu)
1136 {
1137 	struct spdk_nvmf_tcp_req *tcp_req;
1138 	struct spdk_nvme_tcp_cmd *capsule_cmd;
1139 	uint32_t error_offset = 0;
1140 	enum spdk_nvme_tcp_term_req_fes fes;
1141 
1142 	capsule_cmd = &pdu->hdr.capsule_cmd;
1143 	tcp_req = pdu->req;
1144 	assert(tcp_req != NULL);
1145 	if (capsule_cmd->common.pdo > SPDK_NVME_TCP_PDU_PDO_MAX_OFFSET) {
1146 		SPDK_ERRLOG("Expected ICReq capsule_cmd pdu offset <= %d, got %c\n",
1147 			    SPDK_NVME_TCP_PDU_PDO_MAX_OFFSET, capsule_cmd->common.pdo);
1148 		fes = SPDK_NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD;
1149 		error_offset = offsetof(struct spdk_nvme_tcp_common_pdu_hdr, pdo);
1150 		goto err;
1151 	}
1152 
1153 	spdk_nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_READY);
1154 	spdk_nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_READY_TO_EXECUTE);
1155 	spdk_nvmf_tcp_req_process(ttransport, tcp_req);
1156 
1157 	return;
1158 err:
1159 	spdk_nvmf_tcp_send_c2h_term_req(tqpair, pdu, fes, error_offset);
1160 }
1161 
1162 static int
1163 nvmf_tcp_find_req_in_state(struct spdk_nvmf_tcp_qpair *tqpair,
1164 			   enum spdk_nvmf_tcp_req_state state,
1165 			   uint16_t cid, uint16_t tag,
1166 			   struct spdk_nvmf_tcp_req **req)
1167 {
1168 	struct spdk_nvmf_tcp_req *tcp_req = NULL;
1169 
1170 	TAILQ_FOREACH(tcp_req, &tqpair->state_queue[state], state_link) {
1171 		if (tcp_req->req.cmd->nvme_cmd.cid != cid) {
1172 			continue;
1173 		}
1174 
1175 		if (tcp_req->ttag == tag) {
1176 			*req = tcp_req;
1177 			return 0;
1178 		}
1179 
1180 		*req = NULL;
1181 		return -1;
1182 	}
1183 
1184 	/* Didn't find it, but not an error */
1185 	*req = NULL;
1186 	return 0;
1187 }
1188 
1189 static void
1190 spdk_nvmf_tcp_h2c_data_hdr_handle(struct spdk_nvmf_tcp_transport *ttransport,
1191 				  struct spdk_nvmf_tcp_qpair *tqpair,
1192 				  struct nvme_tcp_pdu *pdu)
1193 {
1194 	struct spdk_nvmf_tcp_req *tcp_req;
1195 	uint32_t error_offset = 0;
1196 	enum spdk_nvme_tcp_term_req_fes fes = 0;
1197 	struct spdk_nvme_tcp_h2c_data_hdr *h2c_data;
1198 	int rc;
1199 
1200 	h2c_data = &pdu->hdr.h2c_data;
1201 
1202 	SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "tqpair=%p, r2t_info: datao=%u, datal=%u, cccid=%u, ttag=%u\n",
1203 		      tqpair, h2c_data->datao, h2c_data->datal, h2c_data->cccid, h2c_data->ttag);
1204 
1205 	rc = nvmf_tcp_find_req_in_state(tqpair, TCP_REQUEST_STATE_TRANSFERRING_HOST_TO_CONTROLLER,
1206 					h2c_data->cccid, h2c_data->ttag, &tcp_req);
1207 	if (rc == 0 && tcp_req == NULL) {
1208 		rc = nvmf_tcp_find_req_in_state(tqpair, TCP_REQUEST_STATE_AWAITING_R2T_ACK, h2c_data->cccid,
1209 						h2c_data->ttag, &tcp_req);
1210 	}
1211 
1212 	if (!tcp_req) {
1213 		SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "tcp_req is not found for tqpair=%p\n", tqpair);
1214 		fes = SPDK_NVME_TCP_TERM_REQ_FES_INVALID_DATA_UNSUPPORTED_PARAMETER;
1215 		if (rc == 0) {
1216 			error_offset = offsetof(struct spdk_nvme_tcp_h2c_data_hdr, cccid);
1217 		} else {
1218 			error_offset = offsetof(struct spdk_nvme_tcp_h2c_data_hdr, ttag);
1219 		}
1220 		goto err;
1221 	}
1222 
1223 	if (tcp_req->h2c_offset != h2c_data->datao) {
1224 		SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP,
1225 			      "tcp_req(%p), tqpair=%p, expected data offset %u, but data offset is %u\n",
1226 			      tcp_req, tqpair, tcp_req->h2c_offset, h2c_data->datao);
1227 		fes = SPDK_NVME_TCP_TERM_REQ_FES_DATA_TRANSFER_OUT_OF_RANGE;
1228 		goto err;
1229 	}
1230 
1231 	if ((h2c_data->datao + h2c_data->datal) > tcp_req->req.length) {
1232 		SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP,
1233 			      "tcp_req(%p), tqpair=%p,  (datao=%u + datal=%u) execeeds requested length=%u\n",
1234 			      tcp_req, tqpair, h2c_data->datao, h2c_data->datal, tcp_req->req.length);
1235 		fes = SPDK_NVME_TCP_TERM_REQ_FES_DATA_TRANSFER_OUT_OF_RANGE;
1236 		goto err;
1237 	}
1238 
1239 	pdu->req = tcp_req;
1240 
1241 	if (spdk_unlikely(tcp_req->req.dif.dif_insert_or_strip)) {
1242 		pdu->dif_ctx = &tcp_req->req.dif.dif_ctx;
1243 	}
1244 
1245 	nvme_tcp_pdu_set_data_buf(pdu, tcp_req->req.iov, tcp_req->req.iovcnt,
1246 				  h2c_data->datao, h2c_data->datal);
1247 	spdk_nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_PAYLOAD);
1248 	return;
1249 
1250 err:
1251 	spdk_nvmf_tcp_send_c2h_term_req(tqpair, pdu, fes, error_offset);
1252 }
1253 
1254 static void
1255 spdk_nvmf_tcp_pdu_cmd_complete(void *cb_arg)
1256 {
1257 	struct spdk_nvmf_tcp_req *tcp_req = cb_arg;
1258 	nvmf_tcp_request_free(tcp_req);
1259 }
1260 
1261 static void
1262 spdk_nvmf_tcp_send_capsule_resp_pdu(struct spdk_nvmf_tcp_req *tcp_req,
1263 				    struct spdk_nvmf_tcp_qpair *tqpair)
1264 {
1265 	struct nvme_tcp_pdu *rsp_pdu;
1266 	struct spdk_nvme_tcp_rsp *capsule_resp;
1267 
1268 	SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "enter, tqpair=%p\n", tqpair);
1269 
1270 	rsp_pdu = nvmf_tcp_req_pdu_init(tcp_req);
1271 	assert(rsp_pdu != NULL);
1272 
1273 	capsule_resp = &rsp_pdu->hdr.capsule_resp;
1274 	capsule_resp->common.pdu_type = SPDK_NVME_TCP_PDU_TYPE_CAPSULE_RESP;
1275 	capsule_resp->common.plen = capsule_resp->common.hlen = sizeof(*capsule_resp);
1276 	capsule_resp->rccqe = tcp_req->req.rsp->nvme_cpl;
1277 	if (tqpair->host_hdgst_enable) {
1278 		capsule_resp->common.flags |= SPDK_NVME_TCP_CH_FLAGS_HDGSTF;
1279 		capsule_resp->common.plen += SPDK_NVME_TCP_DIGEST_LEN;
1280 	}
1281 
1282 	spdk_nvmf_tcp_qpair_write_pdu(tqpair, rsp_pdu, spdk_nvmf_tcp_pdu_cmd_complete, tcp_req);
1283 }
1284 
1285 static void
1286 spdk_nvmf_tcp_pdu_c2h_data_complete(void *cb_arg)
1287 {
1288 	struct spdk_nvmf_tcp_req *tcp_req = cb_arg;
1289 	struct spdk_nvmf_tcp_qpair *tqpair = SPDK_CONTAINEROF(tcp_req->req.qpair,
1290 					     struct spdk_nvmf_tcp_qpair, qpair);
1291 
1292 	assert(tqpair != NULL);
1293 	if (tqpair->qpair.transport->opts.c2h_success) {
1294 		nvmf_tcp_request_free(tcp_req);
1295 	} else {
1296 		nvmf_tcp_req_pdu_fini(tcp_req);
1297 		spdk_nvmf_tcp_send_capsule_resp_pdu(tcp_req, tqpair);
1298 	}
1299 }
1300 
1301 static void
1302 spdk_nvmf_tcp_r2t_complete(void *cb_arg)
1303 {
1304 	struct spdk_nvmf_tcp_req *tcp_req = cb_arg;
1305 	struct spdk_nvmf_tcp_transport *ttransport;
1306 
1307 	nvmf_tcp_req_pdu_fini(tcp_req);
1308 
1309 	ttransport = SPDK_CONTAINEROF(tcp_req->req.qpair->transport,
1310 				      struct spdk_nvmf_tcp_transport, transport);
1311 
1312 	spdk_nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_TRANSFERRING_HOST_TO_CONTROLLER);
1313 
1314 	if (tcp_req->h2c_offset == tcp_req->req.length) {
1315 		spdk_nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_READY_TO_EXECUTE);
1316 		spdk_nvmf_tcp_req_process(ttransport, tcp_req);
1317 	}
1318 }
1319 
1320 static void
1321 spdk_nvmf_tcp_send_r2t_pdu(struct spdk_nvmf_tcp_qpair *tqpair,
1322 			   struct spdk_nvmf_tcp_req *tcp_req)
1323 {
1324 	struct nvme_tcp_pdu *rsp_pdu;
1325 	struct spdk_nvme_tcp_r2t_hdr *r2t;
1326 
1327 	rsp_pdu = nvmf_tcp_req_pdu_init(tcp_req);
1328 	assert(rsp_pdu != NULL);
1329 
1330 	r2t = &rsp_pdu->hdr.r2t;
1331 	r2t->common.pdu_type = SPDK_NVME_TCP_PDU_TYPE_R2T;
1332 	r2t->common.plen = r2t->common.hlen = sizeof(*r2t);
1333 
1334 	if (tqpair->host_hdgst_enable) {
1335 		r2t->common.flags |= SPDK_NVME_TCP_CH_FLAGS_HDGSTF;
1336 		r2t->common.plen += SPDK_NVME_TCP_DIGEST_LEN;
1337 	}
1338 
1339 	r2t->cccid = tcp_req->req.cmd->nvme_cmd.cid;
1340 	r2t->ttag = tcp_req->ttag;
1341 	r2t->r2to = tcp_req->h2c_offset;
1342 	r2t->r2tl = tcp_req->req.length;
1343 
1344 	spdk_nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_AWAITING_R2T_ACK);
1345 
1346 	SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP,
1347 		      "tcp_req(%p) on tqpair(%p), r2t_info: cccid=%u, ttag=%u, r2to=%u, r2tl=%u\n",
1348 		      tcp_req, tqpair, r2t->cccid, r2t->ttag, r2t->r2to, r2t->r2tl);
1349 	spdk_nvmf_tcp_qpair_write_pdu(tqpair, rsp_pdu, spdk_nvmf_tcp_r2t_complete, tcp_req);
1350 }
1351 
1352 static void
1353 spdk_nvmf_tcp_h2c_data_payload_handle(struct spdk_nvmf_tcp_transport *ttransport,
1354 				      struct spdk_nvmf_tcp_qpair *tqpair,
1355 				      struct nvme_tcp_pdu *pdu)
1356 {
1357 	struct spdk_nvmf_tcp_req *tcp_req;
1358 
1359 	tcp_req = pdu->req;
1360 	assert(tcp_req != NULL);
1361 
1362 	SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "enter\n");
1363 
1364 	tcp_req->h2c_offset += pdu->data_len;
1365 
1366 	spdk_nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_READY);
1367 
1368 	/* Wait for all of the data to arrive AND for the initial R2T PDU send to be
1369 	 * acknowledged before moving on. */
1370 	if (tcp_req->h2c_offset == tcp_req->req.length &&
1371 	    tcp_req->state == TCP_REQUEST_STATE_TRANSFERRING_HOST_TO_CONTROLLER) {
1372 		spdk_nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_READY_TO_EXECUTE);
1373 		spdk_nvmf_tcp_req_process(ttransport, tcp_req);
1374 	}
1375 }
1376 
1377 static void
1378 spdk_nvmf_tcp_h2c_term_req_dump(struct spdk_nvme_tcp_term_req_hdr *h2c_term_req)
1379 {
1380 	SPDK_ERRLOG("Error info of pdu(%p): %s\n", h2c_term_req,
1381 		    spdk_nvmf_tcp_term_req_fes_str[h2c_term_req->fes]);
1382 	if ((h2c_term_req->fes == SPDK_NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD) ||
1383 	    (h2c_term_req->fes == SPDK_NVME_TCP_TERM_REQ_FES_INVALID_DATA_UNSUPPORTED_PARAMETER)) {
1384 		SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "The offset from the start of the PDU header is %u\n",
1385 			      DGET32(h2c_term_req->fei));
1386 	}
1387 }
1388 
1389 static void
1390 spdk_nvmf_tcp_h2c_term_req_hdr_handle(struct spdk_nvmf_tcp_qpair *tqpair,
1391 				      struct nvme_tcp_pdu *pdu)
1392 {
1393 	struct spdk_nvme_tcp_term_req_hdr *h2c_term_req = &pdu->hdr.term_req;
1394 	uint32_t error_offset = 0;
1395 	enum spdk_nvme_tcp_term_req_fes fes;
1396 
1397 
1398 	if (h2c_term_req->fes > SPDK_NVME_TCP_TERM_REQ_FES_INVALID_DATA_UNSUPPORTED_PARAMETER) {
1399 		SPDK_ERRLOG("Fatal Error Stauts(FES) is unknown for h2c_term_req pdu=%p\n", pdu);
1400 		fes = SPDK_NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD;
1401 		error_offset = offsetof(struct spdk_nvme_tcp_term_req_hdr, fes);
1402 		goto end;
1403 	}
1404 
1405 	/* set the data buffer */
1406 	nvme_tcp_pdu_set_data(pdu, (uint8_t *)pdu->hdr.raw + h2c_term_req->common.hlen,
1407 			      h2c_term_req->common.plen - h2c_term_req->common.hlen);
1408 	spdk_nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_PAYLOAD);
1409 	return;
1410 end:
1411 	spdk_nvmf_tcp_send_c2h_term_req(tqpair, pdu, fes, error_offset);
1412 }
1413 
1414 static void
1415 spdk_nvmf_tcp_h2c_term_req_payload_handle(struct spdk_nvmf_tcp_qpair *tqpair,
1416 		struct nvme_tcp_pdu *pdu)
1417 {
1418 	struct spdk_nvme_tcp_term_req_hdr *h2c_term_req = &pdu->hdr.term_req;
1419 
1420 	spdk_nvmf_tcp_h2c_term_req_dump(h2c_term_req);
1421 	spdk_nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_ERROR);
1422 }
1423 
1424 static void
1425 spdk_nvmf_tcp_pdu_payload_handle(struct spdk_nvmf_tcp_qpair *tqpair,
1426 				 struct spdk_nvmf_tcp_transport *ttransport)
1427 {
1428 	int rc = 0;
1429 	struct nvme_tcp_pdu *pdu;
1430 	uint32_t crc32c, error_offset = 0;
1431 	enum spdk_nvme_tcp_term_req_fes fes;
1432 
1433 	assert(tqpair->recv_state == NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_PAYLOAD);
1434 	pdu = &tqpair->pdu_in_progress;
1435 
1436 	SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "enter\n");
1437 	/* check data digest if need */
1438 	if (pdu->ddgst_enable) {
1439 		crc32c = nvme_tcp_pdu_calc_data_digest(pdu);
1440 		rc = MATCH_DIGEST_WORD(pdu->data_digest, crc32c);
1441 		if (rc == 0) {
1442 			SPDK_ERRLOG("Data digest error on tqpair=(%p) with pdu=%p\n", tqpair, pdu);
1443 			fes = SPDK_NVME_TCP_TERM_REQ_FES_HDGST_ERROR;
1444 			spdk_nvmf_tcp_send_c2h_term_req(tqpair, pdu, fes, error_offset);
1445 			return;
1446 
1447 		}
1448 	}
1449 
1450 	switch (pdu->hdr.common.pdu_type) {
1451 	case SPDK_NVME_TCP_PDU_TYPE_CAPSULE_CMD:
1452 		spdk_nvmf_tcp_capsule_cmd_payload_handle(ttransport, tqpair, pdu);
1453 		break;
1454 	case SPDK_NVME_TCP_PDU_TYPE_H2C_DATA:
1455 		spdk_nvmf_tcp_h2c_data_payload_handle(ttransport, tqpair, pdu);
1456 		break;
1457 
1458 	case SPDK_NVME_TCP_PDU_TYPE_H2C_TERM_REQ:
1459 		spdk_nvmf_tcp_h2c_term_req_payload_handle(tqpair, pdu);
1460 		break;
1461 
1462 	default:
1463 		/* The code should not go to here */
1464 		SPDK_ERRLOG("The code should not go to here\n");
1465 		break;
1466 	}
1467 }
1468 
1469 static void
1470 spdk_nvmf_tcp_send_icresp_complete(void *cb_arg)
1471 {
1472 	struct spdk_nvmf_tcp_qpair *tqpair = cb_arg;
1473 
1474 	tqpair->state = NVME_TCP_QPAIR_STATE_RUNNING;
1475 }
1476 
1477 static void
1478 spdk_nvmf_tcp_icreq_handle(struct spdk_nvmf_tcp_transport *ttransport,
1479 			   struct spdk_nvmf_tcp_qpair *tqpair,
1480 			   struct nvme_tcp_pdu *pdu)
1481 {
1482 	struct spdk_nvme_tcp_ic_req *ic_req = &pdu->hdr.ic_req;
1483 	struct nvme_tcp_pdu *rsp_pdu;
1484 	struct spdk_nvme_tcp_ic_resp *ic_resp;
1485 	uint32_t error_offset = 0;
1486 	enum spdk_nvme_tcp_term_req_fes fes;
1487 
1488 	/* Only PFV 0 is defined currently */
1489 	if (ic_req->pfv != 0) {
1490 		SPDK_ERRLOG("Expected ICReq PFV %u, got %u\n", 0u, ic_req->pfv);
1491 		fes = SPDK_NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD;
1492 		error_offset = offsetof(struct spdk_nvme_tcp_ic_req, pfv);
1493 		goto end;
1494 	}
1495 
1496 	/* MAXR2T is 0's based */
1497 	SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "maxr2t =%u\n", (ic_req->maxr2t + 1u));
1498 
1499 	tqpair->host_hdgst_enable = ic_req->dgst.bits.hdgst_enable ? true : false;
1500 	if (!tqpair->host_hdgst_enable) {
1501 		tqpair->recv_buf_size -= SPDK_NVME_TCP_DIGEST_LEN * SPDK_NVMF_TCP_RECV_BUF_SIZE_FACTOR;
1502 	}
1503 
1504 	tqpair->host_ddgst_enable = ic_req->dgst.bits.ddgst_enable ? true : false;
1505 	if (!tqpair->host_ddgst_enable) {
1506 		tqpair->recv_buf_size -= SPDK_NVME_TCP_DIGEST_LEN * SPDK_NVMF_TCP_RECV_BUF_SIZE_FACTOR;
1507 	}
1508 
1509 	/* Now that we know whether digests are enabled, properly size the receive buffer */
1510 	if (spdk_sock_set_recvbuf(tqpair->sock, tqpair->recv_buf_size) < 0) {
1511 		SPDK_WARNLOG("Unable to allocate enough memory for receive buffer on tqpair=%p with size=%d\n",
1512 			     tqpair,
1513 			     tqpair->recv_buf_size);
1514 		/* Not fatal. */
1515 	}
1516 
1517 	tqpair->cpda = spdk_min(ic_req->hpda, SPDK_NVME_TCP_CPDA_MAX);
1518 	SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "cpda of tqpair=(%p) is : %u\n", tqpair, tqpair->cpda);
1519 
1520 	rsp_pdu = &tqpair->mgmt_pdu;
1521 
1522 	ic_resp = &rsp_pdu->hdr.ic_resp;
1523 	ic_resp->common.pdu_type = SPDK_NVME_TCP_PDU_TYPE_IC_RESP;
1524 	ic_resp->common.hlen = ic_resp->common.plen =  sizeof(*ic_resp);
1525 	ic_resp->pfv = 0;
1526 	ic_resp->cpda = tqpair->cpda;
1527 	ic_resp->maxh2cdata = ttransport->transport.opts.max_io_size;
1528 	ic_resp->dgst.bits.hdgst_enable = tqpair->host_hdgst_enable ? 1 : 0;
1529 	ic_resp->dgst.bits.ddgst_enable = tqpair->host_ddgst_enable ? 1 : 0;
1530 
1531 	SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "host_hdgst_enable: %u\n", tqpair->host_hdgst_enable);
1532 	SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "host_ddgst_enable: %u\n", tqpair->host_ddgst_enable);
1533 
1534 	tqpair->state = NVME_TCP_QPAIR_STATE_INITIALIZING;
1535 	spdk_nvmf_tcp_qpair_write_pdu(tqpair, rsp_pdu, spdk_nvmf_tcp_send_icresp_complete, tqpair);
1536 	spdk_nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_READY);
1537 	return;
1538 end:
1539 	spdk_nvmf_tcp_send_c2h_term_req(tqpair, pdu, fes, error_offset);
1540 }
1541 
1542 static void
1543 spdk_nvmf_tcp_pdu_psh_handle(struct spdk_nvmf_tcp_qpair *tqpair,
1544 			     struct spdk_nvmf_tcp_transport *ttransport)
1545 {
1546 	struct nvme_tcp_pdu *pdu;
1547 	int rc;
1548 	uint32_t crc32c, error_offset = 0;
1549 	enum spdk_nvme_tcp_term_req_fes fes;
1550 
1551 	assert(tqpair->recv_state == NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_PSH);
1552 	pdu = &tqpair->pdu_in_progress;
1553 
1554 	SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "pdu type of tqpair(%p) is %d\n", tqpair,
1555 		      pdu->hdr.common.pdu_type);
1556 	/* check header digest if needed */
1557 	if (pdu->has_hdgst) {
1558 		SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "Compare the header of pdu=%p on tqpair=%p\n", pdu, tqpair);
1559 		crc32c = nvme_tcp_pdu_calc_header_digest(pdu);
1560 		rc = MATCH_DIGEST_WORD((uint8_t *)pdu->hdr.raw + pdu->hdr.common.hlen, crc32c);
1561 		if (rc == 0) {
1562 			SPDK_ERRLOG("Header digest error on tqpair=(%p) with pdu=%p\n", tqpair, pdu);
1563 			fes = SPDK_NVME_TCP_TERM_REQ_FES_HDGST_ERROR;
1564 			spdk_nvmf_tcp_send_c2h_term_req(tqpair, pdu, fes, error_offset);
1565 			return;
1566 
1567 		}
1568 	}
1569 
1570 	switch (pdu->hdr.common.pdu_type) {
1571 	case SPDK_NVME_TCP_PDU_TYPE_IC_REQ:
1572 		spdk_nvmf_tcp_icreq_handle(ttransport, tqpair, pdu);
1573 		break;
1574 	case SPDK_NVME_TCP_PDU_TYPE_CAPSULE_CMD:
1575 		spdk_nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_AWAIT_REQ);
1576 		break;
1577 	case SPDK_NVME_TCP_PDU_TYPE_H2C_DATA:
1578 		spdk_nvmf_tcp_h2c_data_hdr_handle(ttransport, tqpair, pdu);
1579 		break;
1580 
1581 	case SPDK_NVME_TCP_PDU_TYPE_H2C_TERM_REQ:
1582 		spdk_nvmf_tcp_h2c_term_req_hdr_handle(tqpair, pdu);
1583 		break;
1584 
1585 	default:
1586 		SPDK_ERRLOG("Unexpected PDU type 0x%02x\n", tqpair->pdu_in_progress.hdr.common.pdu_type);
1587 		fes = SPDK_NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD;
1588 		error_offset = 1;
1589 		spdk_nvmf_tcp_send_c2h_term_req(tqpair, pdu, fes, error_offset);
1590 		break;
1591 	}
1592 }
1593 
1594 static void
1595 spdk_nvmf_tcp_pdu_ch_handle(struct spdk_nvmf_tcp_qpair *tqpair)
1596 {
1597 	struct nvme_tcp_pdu *pdu;
1598 	uint32_t error_offset = 0;
1599 	enum spdk_nvme_tcp_term_req_fes fes;
1600 	uint8_t expected_hlen, pdo;
1601 	bool plen_error = false, pdo_error = false;
1602 
1603 	assert(tqpair->recv_state == NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_CH);
1604 	pdu = &tqpair->pdu_in_progress;
1605 
1606 	if (pdu->hdr.common.pdu_type == SPDK_NVME_TCP_PDU_TYPE_IC_REQ) {
1607 		if (tqpair->state != NVME_TCP_QPAIR_STATE_INVALID) {
1608 			SPDK_ERRLOG("Already received ICreq PDU, and reject this pdu=%p\n", pdu);
1609 			fes = SPDK_NVME_TCP_TERM_REQ_FES_PDU_SEQUENCE_ERROR;
1610 			goto err;
1611 		}
1612 		expected_hlen = sizeof(struct spdk_nvme_tcp_ic_req);
1613 		if (pdu->hdr.common.plen != expected_hlen) {
1614 			plen_error = true;
1615 		}
1616 	} else {
1617 		if (tqpair->state != NVME_TCP_QPAIR_STATE_RUNNING) {
1618 			SPDK_ERRLOG("The TCP/IP connection is not negotitated\n");
1619 			fes = SPDK_NVME_TCP_TERM_REQ_FES_PDU_SEQUENCE_ERROR;
1620 			goto err;
1621 		}
1622 
1623 		switch (pdu->hdr.common.pdu_type) {
1624 		case SPDK_NVME_TCP_PDU_TYPE_CAPSULE_CMD:
1625 			expected_hlen = sizeof(struct spdk_nvme_tcp_cmd);
1626 			pdo = pdu->hdr.common.pdo;
1627 			if ((tqpair->cpda != 0) && (pdo != ((tqpair->cpda + 1) << 2))) {
1628 				pdo_error = true;
1629 				break;
1630 			}
1631 
1632 			if (pdu->hdr.common.plen < expected_hlen) {
1633 				plen_error = true;
1634 			}
1635 			break;
1636 		case SPDK_NVME_TCP_PDU_TYPE_H2C_DATA:
1637 			expected_hlen = sizeof(struct spdk_nvme_tcp_h2c_data_hdr);
1638 			pdo = pdu->hdr.common.pdo;
1639 			if ((tqpair->cpda != 0) && (pdo != ((tqpair->cpda + 1) << 2))) {
1640 				pdo_error = true;
1641 				break;
1642 			}
1643 			if (pdu->hdr.common.plen < expected_hlen) {
1644 				plen_error = true;
1645 			}
1646 			break;
1647 
1648 		case SPDK_NVME_TCP_PDU_TYPE_H2C_TERM_REQ:
1649 			expected_hlen = sizeof(struct spdk_nvme_tcp_term_req_hdr);
1650 			if ((pdu->hdr.common.plen <= expected_hlen) ||
1651 			    (pdu->hdr.common.plen > SPDK_NVME_TCP_TERM_REQ_PDU_MAX_SIZE)) {
1652 				plen_error = true;
1653 			}
1654 			break;
1655 
1656 		default:
1657 			SPDK_ERRLOG("Unexpected PDU type 0x%02x\n", pdu->hdr.common.pdu_type);
1658 			fes = SPDK_NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD;
1659 			error_offset = offsetof(struct spdk_nvme_tcp_common_pdu_hdr, pdu_type);
1660 			goto err;
1661 		}
1662 	}
1663 
1664 	if (pdu->hdr.common.hlen != expected_hlen) {
1665 		SPDK_ERRLOG("PDU type=0x%02x, Expected ICReq header length %u, got %u on tqpair=%p\n",
1666 			    pdu->hdr.common.pdu_type,
1667 			    expected_hlen, pdu->hdr.common.hlen, tqpair);
1668 		fes = SPDK_NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD;
1669 		error_offset = offsetof(struct spdk_nvme_tcp_common_pdu_hdr, hlen);
1670 		goto err;
1671 	} else if (pdo_error) {
1672 		fes = SPDK_NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD;
1673 		error_offset = offsetof(struct spdk_nvme_tcp_common_pdu_hdr, pdo);
1674 	} else if (plen_error) {
1675 		fes = SPDK_NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD;
1676 		error_offset = offsetof(struct spdk_nvme_tcp_common_pdu_hdr, plen);
1677 		goto err;
1678 	} else {
1679 		spdk_nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_PSH);
1680 		nvme_tcp_pdu_calc_psh_len(&tqpair->pdu_in_progress, tqpair->host_hdgst_enable);
1681 		return;
1682 	}
1683 err:
1684 	spdk_nvmf_tcp_send_c2h_term_req(tqpair, pdu, fes, error_offset);
1685 }
1686 
1687 static int
1688 nvmf_tcp_pdu_payload_insert_dif(struct nvme_tcp_pdu *pdu, uint32_t read_offset,
1689 				int read_len)
1690 {
1691 	int rc;
1692 
1693 	rc = spdk_dif_generate_stream(pdu->data_iov, pdu->data_iovcnt,
1694 				      read_offset, read_len, pdu->dif_ctx);
1695 	if (rc != 0) {
1696 		SPDK_ERRLOG("DIF generate failed\n");
1697 	}
1698 
1699 	return rc;
1700 }
1701 
1702 static int
1703 spdk_nvmf_tcp_sock_process(struct spdk_nvmf_tcp_qpair *tqpair)
1704 {
1705 	int rc = 0;
1706 	struct nvme_tcp_pdu *pdu;
1707 	enum nvme_tcp_pdu_recv_state prev_state;
1708 	uint32_t data_len;
1709 	struct spdk_nvmf_tcp_transport *ttransport = SPDK_CONTAINEROF(tqpair->qpair.transport,
1710 			struct spdk_nvmf_tcp_transport, transport);
1711 
1712 	/* The loop here is to allow for several back-to-back state changes. */
1713 	do {
1714 		prev_state = tqpair->recv_state;
1715 		SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "tqpair(%p) recv pdu entering state %d\n", tqpair, prev_state);
1716 
1717 		pdu = &tqpair->pdu_in_progress;
1718 		switch (tqpair->recv_state) {
1719 		/* Wait for the common header  */
1720 		case NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_READY:
1721 		case NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_CH:
1722 			if (spdk_unlikely(tqpair->state == NVME_TCP_QPAIR_STATE_INITIALIZING)) {
1723 				return rc;
1724 			}
1725 
1726 			rc = nvme_tcp_read_data(tqpair->sock,
1727 						sizeof(struct spdk_nvme_tcp_common_pdu_hdr) - pdu->ch_valid_bytes,
1728 						(void *)&pdu->hdr.common + pdu->ch_valid_bytes);
1729 			if (rc < 0) {
1730 				SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "will disconnect tqpair=%p\n", tqpair);
1731 				return NVME_TCP_PDU_FATAL;
1732 			} else if (rc > 0) {
1733 				pdu->ch_valid_bytes += rc;
1734 				spdk_trace_record(TRACE_TCP_READ_FROM_SOCKET_DONE, 0, rc, 0, 0);
1735 				if (spdk_likely(tqpair->recv_state == NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_READY)) {
1736 					spdk_nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_CH);
1737 				}
1738 			}
1739 
1740 			if (pdu->ch_valid_bytes < sizeof(struct spdk_nvme_tcp_common_pdu_hdr)) {
1741 				return NVME_TCP_PDU_IN_PROGRESS;
1742 			}
1743 
1744 			/* The command header of this PDU has now been read from the socket. */
1745 			spdk_nvmf_tcp_pdu_ch_handle(tqpair);
1746 			break;
1747 		/* Wait for the pdu specific header  */
1748 		case NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_PSH:
1749 			rc = nvme_tcp_read_data(tqpair->sock,
1750 						pdu->psh_len - pdu->psh_valid_bytes,
1751 						(void *)&pdu->hdr.raw + sizeof(struct spdk_nvme_tcp_common_pdu_hdr) + pdu->psh_valid_bytes);
1752 			if (rc < 0) {
1753 				return NVME_TCP_PDU_FATAL;
1754 			} else if (rc > 0) {
1755 				spdk_trace_record(TRACE_TCP_READ_FROM_SOCKET_DONE,
1756 						  0, rc, 0, 0);
1757 				pdu->psh_valid_bytes += rc;
1758 			}
1759 
1760 			if (pdu->psh_valid_bytes < pdu->psh_len) {
1761 				return NVME_TCP_PDU_IN_PROGRESS;
1762 			}
1763 
1764 			/* All header(ch, psh, head digist) of this PDU has now been read from the socket. */
1765 			spdk_nvmf_tcp_pdu_psh_handle(tqpair, ttransport);
1766 			break;
1767 		/* Wait for the req slot */
1768 		case NVME_TCP_PDU_RECV_STATE_AWAIT_REQ:
1769 			spdk_nvmf_tcp_capsule_cmd_hdr_handle(ttransport, tqpair, pdu);
1770 			break;
1771 		case NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_PAYLOAD:
1772 			/* check whether the data is valid, if not we just return */
1773 			if (!pdu->data_len) {
1774 				return NVME_TCP_PDU_IN_PROGRESS;
1775 			}
1776 
1777 			data_len = pdu->data_len;
1778 			/* data digest */
1779 			if (spdk_unlikely((pdu->hdr.common.pdu_type != SPDK_NVME_TCP_PDU_TYPE_H2C_TERM_REQ) &&
1780 					  tqpair->host_ddgst_enable)) {
1781 				data_len += SPDK_NVME_TCP_DIGEST_LEN;
1782 				pdu->ddgst_enable = true;
1783 			}
1784 
1785 			rc = nvme_tcp_read_payload_data(tqpair->sock, pdu);
1786 			if (rc < 0) {
1787 				return NVME_TCP_PDU_IN_PROGRESS;
1788 			}
1789 			pdu->readv_offset += rc;
1790 
1791 			if (spdk_unlikely(pdu->dif_ctx != NULL)) {
1792 				rc = nvmf_tcp_pdu_payload_insert_dif(pdu, pdu->readv_offset - rc, rc);
1793 				if (rc != 0) {
1794 					return NVME_TCP_PDU_FATAL;
1795 				}
1796 			}
1797 
1798 			if (pdu->readv_offset < data_len) {
1799 				return NVME_TCP_PDU_IN_PROGRESS;
1800 			}
1801 
1802 			/* All of this PDU has now been read from the socket. */
1803 			spdk_nvmf_tcp_pdu_payload_handle(tqpair, ttransport);
1804 			break;
1805 		case NVME_TCP_PDU_RECV_STATE_ERROR:
1806 			if (!spdk_sock_is_connected(tqpair->sock)) {
1807 				return NVME_TCP_PDU_FATAL;
1808 			}
1809 			break;
1810 		default:
1811 			assert(0);
1812 			SPDK_ERRLOG("code should not come to here");
1813 			break;
1814 		}
1815 	} while (tqpair->recv_state != prev_state);
1816 
1817 	return rc;
1818 }
1819 
1820 static int
1821 spdk_nvmf_tcp_req_parse_sgl(struct spdk_nvmf_tcp_req *tcp_req,
1822 			    struct spdk_nvmf_transport *transport,
1823 			    struct spdk_nvmf_transport_poll_group *group)
1824 {
1825 	struct spdk_nvmf_request		*req = &tcp_req->req;
1826 	struct spdk_nvme_cmd			*cmd;
1827 	struct spdk_nvme_cpl			*rsp;
1828 	struct spdk_nvme_sgl_descriptor		*sgl;
1829 	uint32_t				length;
1830 
1831 	cmd = &req->cmd->nvme_cmd;
1832 	rsp = &req->rsp->nvme_cpl;
1833 	sgl = &cmd->dptr.sgl1;
1834 
1835 	length = sgl->unkeyed.length;
1836 
1837 	if (sgl->generic.type == SPDK_NVME_SGL_TYPE_TRANSPORT_DATA_BLOCK &&
1838 	    sgl->unkeyed.subtype == SPDK_NVME_SGL_SUBTYPE_TRANSPORT) {
1839 		if (length > transport->opts.max_io_size) {
1840 			SPDK_ERRLOG("SGL length 0x%x exceeds max io size 0x%x\n",
1841 				    length, transport->opts.max_io_size);
1842 			rsp->status.sc = SPDK_NVME_SC_DATA_SGL_LENGTH_INVALID;
1843 			return -1;
1844 		}
1845 
1846 		/* fill request length and populate iovs */
1847 		req->length = length;
1848 
1849 		SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "Data requested length= 0x%x\n", length);
1850 
1851 		if (spdk_unlikely(req->dif.dif_insert_or_strip)) {
1852 			req->dif.orig_length = length;
1853 			length = spdk_dif_get_length_with_md(length, &req->dif.dif_ctx);
1854 			req->dif.elba_length = length;
1855 		}
1856 
1857 		if (spdk_nvmf_request_get_buffers(req, group, transport, length)) {
1858 			/* No available buffers. Queue this request up. */
1859 			SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "No available large data buffers. Queueing request %p\n",
1860 				      tcp_req);
1861 			return 0;
1862 		}
1863 
1864 		/* backward compatible */
1865 		req->data = req->iov[0].iov_base;
1866 
1867 		SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "Request %p took %d buffer/s from central pool, and data=%p\n",
1868 			      tcp_req, req->iovcnt, req->data);
1869 
1870 		return 0;
1871 	} else if (sgl->generic.type == SPDK_NVME_SGL_TYPE_DATA_BLOCK &&
1872 		   sgl->unkeyed.subtype == SPDK_NVME_SGL_SUBTYPE_OFFSET) {
1873 		uint64_t offset = sgl->address;
1874 		uint32_t max_len = transport->opts.in_capsule_data_size;
1875 
1876 		SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "In-capsule data: offset 0x%" PRIx64 ", length 0x%x\n",
1877 			      offset, length);
1878 
1879 		if (offset > max_len) {
1880 			SPDK_ERRLOG("In-capsule offset 0x%" PRIx64 " exceeds capsule length 0x%x\n",
1881 				    offset, max_len);
1882 			rsp->status.sc = SPDK_NVME_SC_INVALID_SGL_OFFSET;
1883 			return -1;
1884 		}
1885 		max_len -= (uint32_t)offset;
1886 
1887 		if (length > max_len) {
1888 			SPDK_ERRLOG("In-capsule data length 0x%x exceeds capsule length 0x%x\n",
1889 				    length, max_len);
1890 			rsp->status.sc = SPDK_NVME_SC_DATA_SGL_LENGTH_INVALID;
1891 			return -1;
1892 		}
1893 
1894 		req->data = tcp_req->buf + offset;
1895 		req->data_from_pool = false;
1896 		req->length = length;
1897 
1898 		if (spdk_unlikely(req->dif.dif_insert_or_strip)) {
1899 			length = spdk_dif_get_length_with_md(length, &req->dif.dif_ctx);
1900 			req->dif.elba_length = length;
1901 		}
1902 
1903 		req->iov[0].iov_base = req->data;
1904 		req->iov[0].iov_len = length;
1905 		req->iovcnt = 1;
1906 
1907 		return 0;
1908 	}
1909 
1910 	SPDK_ERRLOG("Invalid NVMf I/O Command SGL:  Type 0x%x, Subtype 0x%x\n",
1911 		    sgl->generic.type, sgl->generic.subtype);
1912 	rsp->status.sc = SPDK_NVME_SC_SGL_DESCRIPTOR_TYPE_INVALID;
1913 	return -1;
1914 }
1915 
1916 static inline enum spdk_nvme_media_error_status_code
1917 nvmf_tcp_dif_error_to_compl_status(uint8_t err_type) {
1918 	enum spdk_nvme_media_error_status_code result;
1919 
1920 	switch (err_type)
1921 	{
1922 	case SPDK_DIF_REFTAG_ERROR:
1923 		result = SPDK_NVME_SC_REFERENCE_TAG_CHECK_ERROR;
1924 		break;
1925 	case SPDK_DIF_APPTAG_ERROR:
1926 		result = SPDK_NVME_SC_APPLICATION_TAG_CHECK_ERROR;
1927 		break;
1928 	case SPDK_DIF_GUARD_ERROR:
1929 		result = SPDK_NVME_SC_GUARD_CHECK_ERROR;
1930 		break;
1931 	default:
1932 		SPDK_UNREACHABLE();
1933 		break;
1934 	}
1935 
1936 	return result;
1937 }
1938 
1939 static void
1940 spdk_nvmf_tcp_send_c2h_data(struct spdk_nvmf_tcp_qpair *tqpair,
1941 			    struct spdk_nvmf_tcp_req *tcp_req)
1942 {
1943 	struct nvme_tcp_pdu *rsp_pdu;
1944 	struct spdk_nvme_tcp_c2h_data_hdr *c2h_data;
1945 	uint32_t plen, pdo, alignment;
1946 	int rc;
1947 
1948 	SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "enter\n");
1949 
1950 	rsp_pdu = nvmf_tcp_req_pdu_init(tcp_req);
1951 	assert(rsp_pdu != NULL);
1952 
1953 	c2h_data = &rsp_pdu->hdr.c2h_data;
1954 	c2h_data->common.pdu_type = SPDK_NVME_TCP_PDU_TYPE_C2H_DATA;
1955 	plen = c2h_data->common.hlen = sizeof(*c2h_data);
1956 
1957 	if (tqpair->host_hdgst_enable) {
1958 		plen += SPDK_NVME_TCP_DIGEST_LEN;
1959 		c2h_data->common.flags |= SPDK_NVME_TCP_CH_FLAGS_HDGSTF;
1960 	}
1961 
1962 	/* set the psh */
1963 	c2h_data->cccid = tcp_req->req.cmd->nvme_cmd.cid;
1964 	c2h_data->datal = tcp_req->req.length;
1965 	c2h_data->datao = 0;
1966 
1967 	/* set the padding */
1968 	rsp_pdu->padding_len = 0;
1969 	pdo = plen;
1970 	if (tqpair->cpda) {
1971 		alignment = (tqpair->cpda + 1) << 2;
1972 		if (alignment > plen) {
1973 			rsp_pdu->padding_len = alignment - plen;
1974 			pdo = plen = alignment;
1975 		}
1976 	}
1977 
1978 	c2h_data->common.pdo = pdo;
1979 	plen += c2h_data->datal;
1980 	if (tqpair->host_ddgst_enable) {
1981 		c2h_data->common.flags |= SPDK_NVME_TCP_CH_FLAGS_DDGSTF;
1982 		plen += SPDK_NVME_TCP_DIGEST_LEN;
1983 	}
1984 
1985 	c2h_data->common.plen = plen;
1986 
1987 	if (spdk_unlikely(tcp_req->req.dif.dif_insert_or_strip)) {
1988 		rsp_pdu->dif_ctx = &tcp_req->req.dif.dif_ctx;
1989 	}
1990 
1991 	nvme_tcp_pdu_set_data_buf(rsp_pdu, tcp_req->req.iov, tcp_req->req.iovcnt,
1992 				  c2h_data->datao, c2h_data->datal);
1993 
1994 	if (spdk_unlikely(tcp_req->req.dif.dif_insert_or_strip)) {
1995 		struct spdk_nvme_cpl *rsp = &tcp_req->req.rsp->nvme_cpl;
1996 		struct spdk_dif_error err_blk = {};
1997 
1998 		rc = spdk_dif_verify_stream(rsp_pdu->data_iov, rsp_pdu->data_iovcnt,
1999 					    0, rsp_pdu->data_len, rsp_pdu->dif_ctx, &err_blk);
2000 		if (rc != 0) {
2001 			SPDK_ERRLOG("DIF error detected. type=%d, offset=%" PRIu32 "\n",
2002 				    err_blk.err_type, err_blk.err_offset);
2003 			rsp->status.sct = SPDK_NVME_SCT_MEDIA_ERROR;
2004 			rsp->status.sc = nvmf_tcp_dif_error_to_compl_status(err_blk.err_type);
2005 			nvmf_tcp_req_pdu_fini(tcp_req);
2006 			spdk_nvmf_tcp_send_capsule_resp_pdu(tcp_req, tqpair);
2007 			return;
2008 		}
2009 	}
2010 
2011 	c2h_data->common.flags |= SPDK_NVME_TCP_C2H_DATA_FLAGS_LAST_PDU;
2012 	if (tqpair->qpair.transport->opts.c2h_success) {
2013 		c2h_data->common.flags |= SPDK_NVME_TCP_C2H_DATA_FLAGS_SUCCESS;
2014 	}
2015 
2016 	spdk_nvmf_tcp_qpair_write_pdu(tqpair, rsp_pdu, spdk_nvmf_tcp_pdu_c2h_data_complete, tcp_req);
2017 }
2018 
2019 static int
2020 request_transfer_out(struct spdk_nvmf_request *req)
2021 {
2022 	struct spdk_nvmf_tcp_req	*tcp_req;
2023 	struct spdk_nvmf_qpair		*qpair;
2024 	struct spdk_nvmf_tcp_qpair	*tqpair;
2025 	struct spdk_nvme_cpl		*rsp;
2026 
2027 	SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "enter\n");
2028 
2029 	qpair = req->qpair;
2030 	rsp = &req->rsp->nvme_cpl;
2031 	tcp_req = SPDK_CONTAINEROF(req, struct spdk_nvmf_tcp_req, req);
2032 
2033 	/* Advance our sq_head pointer */
2034 	if (qpair->sq_head == qpair->sq_head_max) {
2035 		qpair->sq_head = 0;
2036 	} else {
2037 		qpair->sq_head++;
2038 	}
2039 	rsp->sqhd = qpair->sq_head;
2040 
2041 	tqpair = SPDK_CONTAINEROF(tcp_req->req.qpair, struct spdk_nvmf_tcp_qpair, qpair);
2042 	spdk_nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_TRANSFERRING_CONTROLLER_TO_HOST);
2043 	if (rsp->status.sc == SPDK_NVME_SC_SUCCESS && req->xfer == SPDK_NVME_DATA_CONTROLLER_TO_HOST) {
2044 		spdk_nvmf_tcp_send_c2h_data(tqpair, tcp_req);
2045 	} else {
2046 		spdk_nvmf_tcp_send_capsule_resp_pdu(tcp_req, tqpair);
2047 	}
2048 
2049 	return 0;
2050 }
2051 
2052 static void
2053 spdk_nvmf_tcp_set_incapsule_data(struct spdk_nvmf_tcp_qpair *tqpair,
2054 				 struct spdk_nvmf_tcp_req *tcp_req)
2055 {
2056 	struct nvme_tcp_pdu *pdu;
2057 	uint32_t plen = 0;
2058 
2059 	pdu = &tqpair->pdu_in_progress;
2060 	plen = pdu->hdr.common.hlen;
2061 
2062 	if (tqpair->host_hdgst_enable) {
2063 		plen += SPDK_NVME_TCP_DIGEST_LEN;
2064 	}
2065 
2066 	if (pdu->hdr.common.plen != plen) {
2067 		tcp_req->has_incapsule_data = true;
2068 	}
2069 }
2070 
2071 static bool
2072 spdk_nvmf_tcp_req_process(struct spdk_nvmf_tcp_transport *ttransport,
2073 			  struct spdk_nvmf_tcp_req *tcp_req)
2074 {
2075 	struct spdk_nvmf_tcp_qpair		*tqpair;
2076 	int					rc;
2077 	enum spdk_nvmf_tcp_req_state		prev_state;
2078 	bool					progress = false;
2079 	struct spdk_nvmf_transport		*transport = &ttransport->transport;
2080 	struct spdk_nvmf_transport_poll_group	*group;
2081 
2082 	tqpair = SPDK_CONTAINEROF(tcp_req->req.qpair, struct spdk_nvmf_tcp_qpair, qpair);
2083 	group = &tqpair->group->group;
2084 	assert(tcp_req->state != TCP_REQUEST_STATE_FREE);
2085 
2086 	/* The loop here is to allow for several back-to-back state changes. */
2087 	do {
2088 		prev_state = tcp_req->state;
2089 
2090 		SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "Request %p entering state %d on tqpair=%p\n", tcp_req, prev_state,
2091 			      tqpair);
2092 
2093 		switch (tcp_req->state) {
2094 		case TCP_REQUEST_STATE_FREE:
2095 			/* Some external code must kick a request into TCP_REQUEST_STATE_NEW
2096 			 * to escape this state. */
2097 			break;
2098 		case TCP_REQUEST_STATE_NEW:
2099 			spdk_trace_record(TRACE_TCP_REQUEST_STATE_NEW, 0, 0, (uintptr_t)tcp_req, 0);
2100 
2101 			/* copy the cmd from the receive pdu */
2102 			tcp_req->cmd = tqpair->pdu_in_progress.hdr.capsule_cmd.ccsqe;
2103 
2104 			if (spdk_unlikely(spdk_nvmf_request_get_dif_ctx(&tcp_req->req, &tcp_req->req.dif.dif_ctx))) {
2105 				tcp_req->req.dif.dif_insert_or_strip = true;
2106 				tqpair->pdu_in_progress.dif_ctx = &tcp_req->req.dif.dif_ctx;
2107 			}
2108 
2109 			/* The next state transition depends on the data transfer needs of this request. */
2110 			tcp_req->req.xfer = spdk_nvmf_req_get_xfer(&tcp_req->req);
2111 
2112 			/* If no data to transfer, ready to execute. */
2113 			if (tcp_req->req.xfer == SPDK_NVME_DATA_NONE) {
2114 				/* Reset the tqpair receving pdu state */
2115 				spdk_nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_READY);
2116 				spdk_nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_READY_TO_EXECUTE);
2117 				break;
2118 			}
2119 
2120 			spdk_nvmf_tcp_set_incapsule_data(tqpair, tcp_req);
2121 
2122 			if (!tcp_req->has_incapsule_data) {
2123 				spdk_nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_READY);
2124 			}
2125 
2126 			spdk_nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_NEED_BUFFER);
2127 			STAILQ_INSERT_TAIL(&group->pending_buf_queue, &tcp_req->req, buf_link);
2128 			break;
2129 		case TCP_REQUEST_STATE_NEED_BUFFER:
2130 			spdk_trace_record(TRACE_TCP_REQUEST_STATE_NEED_BUFFER, 0, 0, (uintptr_t)tcp_req, 0);
2131 
2132 			assert(tcp_req->req.xfer != SPDK_NVME_DATA_NONE);
2133 
2134 			if (!tcp_req->has_incapsule_data && (&tcp_req->req != STAILQ_FIRST(&group->pending_buf_queue))) {
2135 				SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP,
2136 					      "Not the first element to wait for the buf for tcp_req(%p) on tqpair=%p\n",
2137 					      tcp_req, tqpair);
2138 				/* This request needs to wait in line to obtain a buffer */
2139 				break;
2140 			}
2141 
2142 			/* Try to get a data buffer */
2143 			rc = spdk_nvmf_tcp_req_parse_sgl(tcp_req, transport, group);
2144 			if (rc < 0) {
2145 				STAILQ_REMOVE_HEAD(&group->pending_buf_queue, buf_link);
2146 				/* Reset the tqpair receving pdu state */
2147 				spdk_nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_ERROR);
2148 				spdk_nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_READY_TO_COMPLETE);
2149 				break;
2150 			}
2151 
2152 			if (!tcp_req->req.data) {
2153 				SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "No buffer allocated for tcp_req(%p) on tqpair(%p\n)",
2154 					      tcp_req, tqpair);
2155 				/* No buffers available. */
2156 				break;
2157 			}
2158 
2159 			STAILQ_REMOVE(&group->pending_buf_queue, &tcp_req->req, spdk_nvmf_request, buf_link);
2160 
2161 			/* If data is transferring from host to controller, we need to do a transfer from the host. */
2162 			if (tcp_req->req.xfer == SPDK_NVME_DATA_HOST_TO_CONTROLLER) {
2163 				if (tcp_req->req.data_from_pool) {
2164 					SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "Sending R2T for tcp_req(%p) on tqpair=%p\n", tcp_req, tqpair);
2165 					spdk_nvmf_tcp_send_r2t_pdu(tqpair, tcp_req);
2166 				} else {
2167 					struct nvme_tcp_pdu *pdu;
2168 
2169 					spdk_nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_TRANSFERRING_HOST_TO_CONTROLLER);
2170 
2171 					pdu = &tqpair->pdu_in_progress;
2172 					SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "Not need to send r2t for tcp_req(%p) on tqpair=%p\n", tcp_req,
2173 						      tqpair);
2174 					/* No need to send r2t, contained in the capsuled data */
2175 					nvme_tcp_pdu_set_data_buf(pdu, tcp_req->req.iov, tcp_req->req.iovcnt,
2176 								  0, tcp_req->req.length);
2177 					spdk_nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_PAYLOAD);
2178 				}
2179 				break;
2180 			}
2181 
2182 			spdk_nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_READY_TO_EXECUTE);
2183 			break;
2184 		case TCP_REQUEST_STATE_AWAITING_R2T_ACK:
2185 			spdk_trace_record(TRACE_TCP_REQUEST_STATE_AWAIT_R2T_ACK, 0, 0, (uintptr_t)tcp_req, 0);
2186 			/* The R2T completion or the h2c data incoming will kick it out of this state. */
2187 			break;
2188 		case TCP_REQUEST_STATE_TRANSFERRING_HOST_TO_CONTROLLER:
2189 
2190 			spdk_trace_record(TRACE_TCP_REQUEST_STATE_TRANSFERRING_HOST_TO_CONTROLLER, 0, 0,
2191 					  (uintptr_t)tcp_req, 0);
2192 			/* Some external code must kick a request into TCP_REQUEST_STATE_READY_TO_EXECUTE
2193 			 * to escape this state. */
2194 			break;
2195 		case TCP_REQUEST_STATE_READY_TO_EXECUTE:
2196 			spdk_trace_record(TRACE_TCP_REQUEST_STATE_READY_TO_EXECUTE, 0, 0, (uintptr_t)tcp_req, 0);
2197 
2198 			if (spdk_unlikely(tcp_req->req.dif.dif_insert_or_strip)) {
2199 				assert(tcp_req->req.dif.elba_length >= tcp_req->req.length);
2200 				tcp_req->req.length = tcp_req->req.dif.elba_length;
2201 			}
2202 
2203 			spdk_nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_EXECUTING);
2204 			spdk_nvmf_request_exec(&tcp_req->req);
2205 			break;
2206 		case TCP_REQUEST_STATE_EXECUTING:
2207 			spdk_trace_record(TRACE_TCP_REQUEST_STATE_EXECUTING, 0, 0, (uintptr_t)tcp_req, 0);
2208 			/* Some external code must kick a request into TCP_REQUEST_STATE_EXECUTED
2209 			 * to escape this state. */
2210 			break;
2211 		case TCP_REQUEST_STATE_EXECUTED:
2212 			spdk_trace_record(TRACE_TCP_REQUEST_STATE_EXECUTED, 0, 0, (uintptr_t)tcp_req, 0);
2213 
2214 			if (spdk_unlikely(tcp_req->req.dif.dif_insert_or_strip)) {
2215 				tcp_req->req.length = tcp_req->req.dif.orig_length;
2216 			}
2217 
2218 			spdk_nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_READY_TO_COMPLETE);
2219 			break;
2220 		case TCP_REQUEST_STATE_READY_TO_COMPLETE:
2221 			spdk_trace_record(TRACE_TCP_REQUEST_STATE_READY_TO_COMPLETE, 0, 0, (uintptr_t)tcp_req, 0);
2222 			rc = request_transfer_out(&tcp_req->req);
2223 			assert(rc == 0); /* No good way to handle this currently */
2224 			break;
2225 		case TCP_REQUEST_STATE_TRANSFERRING_CONTROLLER_TO_HOST:
2226 			spdk_trace_record(TRACE_TCP_REQUEST_STATE_TRANSFERRING_CONTROLLER_TO_HOST, 0, 0,
2227 					  (uintptr_t)tcp_req,
2228 					  0);
2229 			/* Some external code must kick a request into TCP_REQUEST_STATE_COMPLETED
2230 			 * to escape this state. */
2231 			break;
2232 		case TCP_REQUEST_STATE_COMPLETED:
2233 			spdk_trace_record(TRACE_TCP_REQUEST_STATE_COMPLETED, 0, 0, (uintptr_t)tcp_req, 0);
2234 			if (tcp_req->req.data_from_pool) {
2235 				spdk_nvmf_request_free_buffers(&tcp_req->req, group, transport);
2236 			}
2237 			tcp_req->req.length = 0;
2238 			tcp_req->req.iovcnt = 0;
2239 			tcp_req->req.data = NULL;
2240 
2241 			nvmf_tcp_req_pdu_fini(tcp_req);
2242 
2243 			spdk_nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_FREE);
2244 			break;
2245 		case TCP_REQUEST_NUM_STATES:
2246 		default:
2247 			assert(0);
2248 			break;
2249 		}
2250 
2251 		if (tcp_req->state != prev_state) {
2252 			progress = true;
2253 		}
2254 	} while (tcp_req->state != prev_state);
2255 
2256 	return progress;
2257 }
2258 
2259 static void
2260 spdk_nvmf_tcp_sock_cb(void *arg, struct spdk_sock_group *group, struct spdk_sock *sock)
2261 {
2262 	struct spdk_nvmf_tcp_qpair *tqpair = arg;
2263 	int rc;
2264 
2265 	assert(tqpair != NULL);
2266 	rc = spdk_nvmf_tcp_sock_process(tqpair);
2267 
2268 	/* If there was a new socket error, disconnect */
2269 	if (rc < 0) {
2270 		spdk_nvmf_tcp_qpair_disconnect(tqpair);
2271 	}
2272 }
2273 
2274 static int
2275 spdk_nvmf_tcp_poll_group_add(struct spdk_nvmf_transport_poll_group *group,
2276 			     struct spdk_nvmf_qpair *qpair)
2277 {
2278 	struct spdk_nvmf_tcp_poll_group	*tgroup;
2279 	struct spdk_nvmf_tcp_qpair	*tqpair;
2280 	int				rc;
2281 
2282 	tgroup = SPDK_CONTAINEROF(group, struct spdk_nvmf_tcp_poll_group, group);
2283 	tqpair = SPDK_CONTAINEROF(qpair, struct spdk_nvmf_tcp_qpair, qpair);
2284 
2285 	rc = spdk_sock_group_add_sock(tgroup->sock_group, tqpair->sock,
2286 				      spdk_nvmf_tcp_sock_cb, tqpair);
2287 	if (rc != 0) {
2288 		SPDK_ERRLOG("Could not add sock to sock_group: %s (%d)\n",
2289 			    spdk_strerror(errno), errno);
2290 		return -1;
2291 	}
2292 
2293 	rc =  spdk_nvmf_tcp_qpair_sock_init(tqpair);
2294 	if (rc != 0) {
2295 		SPDK_ERRLOG("Cannot set sock opt for tqpair=%p\n", tqpair);
2296 		return -1;
2297 	}
2298 
2299 	rc = spdk_nvmf_tcp_qpair_init(&tqpair->qpair);
2300 	if (rc < 0) {
2301 		SPDK_ERRLOG("Cannot init tqpair=%p\n", tqpair);
2302 		return -1;
2303 	}
2304 
2305 	rc = spdk_nvmf_tcp_qpair_init_mem_resource(tqpair);
2306 	if (rc < 0) {
2307 		SPDK_ERRLOG("Cannot init memory resource info for tqpair=%p\n", tqpair);
2308 		return -1;
2309 	}
2310 
2311 	tqpair->group = tgroup;
2312 	tqpair->state = NVME_TCP_QPAIR_STATE_INVALID;
2313 	TAILQ_INSERT_TAIL(&tgroup->qpairs, tqpair, link);
2314 
2315 	return 0;
2316 }
2317 
2318 static int
2319 spdk_nvmf_tcp_poll_group_remove(struct spdk_nvmf_transport_poll_group *group,
2320 				struct spdk_nvmf_qpair *qpair)
2321 {
2322 	struct spdk_nvmf_tcp_poll_group	*tgroup;
2323 	struct spdk_nvmf_tcp_qpair		*tqpair;
2324 	int				rc;
2325 
2326 	tgroup = SPDK_CONTAINEROF(group, struct spdk_nvmf_tcp_poll_group, group);
2327 	tqpair = SPDK_CONTAINEROF(qpair, struct spdk_nvmf_tcp_qpair, qpair);
2328 
2329 	assert(tqpair->group == tgroup);
2330 
2331 	SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "remove tqpair=%p from the tgroup=%p\n", tqpair, tgroup);
2332 	if (tqpair->recv_state == NVME_TCP_PDU_RECV_STATE_AWAIT_REQ) {
2333 		TAILQ_REMOVE(&tgroup->await_req, tqpair, link);
2334 	} else {
2335 		TAILQ_REMOVE(&tgroup->qpairs, tqpair, link);
2336 	}
2337 
2338 	rc = spdk_sock_group_remove_sock(tgroup->sock_group, tqpair->sock);
2339 	if (rc != 0) {
2340 		SPDK_ERRLOG("Could not remove sock from sock_group: %s (%d)\n",
2341 			    spdk_strerror(errno), errno);
2342 	}
2343 
2344 	return rc;
2345 }
2346 
2347 static int
2348 spdk_nvmf_tcp_req_complete(struct spdk_nvmf_request *req)
2349 {
2350 	struct spdk_nvmf_tcp_transport *ttransport;
2351 	struct spdk_nvmf_tcp_req *tcp_req;
2352 
2353 	ttransport = SPDK_CONTAINEROF(req->qpair->transport, struct spdk_nvmf_tcp_transport, transport);
2354 	tcp_req = SPDK_CONTAINEROF(req, struct spdk_nvmf_tcp_req, req);
2355 
2356 	spdk_nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_EXECUTED);
2357 	spdk_nvmf_tcp_req_process(ttransport, tcp_req);
2358 
2359 	return 0;
2360 }
2361 
2362 static void
2363 spdk_nvmf_tcp_close_qpair(struct spdk_nvmf_qpair *qpair)
2364 {
2365 	struct spdk_nvmf_tcp_qpair *tqpair;
2366 
2367 	SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "Qpair: %p\n", qpair);
2368 
2369 	tqpair = SPDK_CONTAINEROF(qpair, struct spdk_nvmf_tcp_qpair, qpair);
2370 	tqpair->state = NVME_TCP_QPAIR_STATE_EXITED;
2371 	spdk_nvmf_tcp_qpair_destroy(tqpair);
2372 }
2373 
2374 static int
2375 spdk_nvmf_tcp_poll_group_poll(struct spdk_nvmf_transport_poll_group *group)
2376 {
2377 	struct spdk_nvmf_tcp_poll_group *tgroup;
2378 	int rc;
2379 	struct spdk_nvmf_request *req, *req_tmp;
2380 	struct spdk_nvmf_tcp_req *tcp_req;
2381 	struct spdk_nvmf_tcp_qpair *tqpair, *tqpair_tmp;
2382 	struct spdk_nvmf_tcp_transport *ttransport = SPDK_CONTAINEROF(group->transport,
2383 			struct spdk_nvmf_tcp_transport, transport);
2384 
2385 	tgroup = SPDK_CONTAINEROF(group, struct spdk_nvmf_tcp_poll_group, group);
2386 
2387 	if (spdk_unlikely(TAILQ_EMPTY(&tgroup->qpairs) && TAILQ_EMPTY(&tgroup->await_req))) {
2388 		return 0;
2389 	}
2390 
2391 	STAILQ_FOREACH_SAFE(req, &group->pending_buf_queue, buf_link, req_tmp) {
2392 		tcp_req = SPDK_CONTAINEROF(req, struct spdk_nvmf_tcp_req, req);
2393 		if (spdk_nvmf_tcp_req_process(ttransport, tcp_req) == false) {
2394 			break;
2395 		}
2396 	}
2397 
2398 	rc = spdk_sock_group_poll(tgroup->sock_group);
2399 	if (rc < 0) {
2400 		SPDK_ERRLOG("Failed to poll sock_group=%p\n", tgroup->sock_group);
2401 	}
2402 
2403 	TAILQ_FOREACH_SAFE(tqpair, &tgroup->await_req, link, tqpair_tmp) {
2404 		spdk_nvmf_tcp_sock_process(tqpair);
2405 	}
2406 
2407 	return rc;
2408 }
2409 
2410 static int
2411 spdk_nvmf_tcp_qpair_get_trid(struct spdk_nvmf_qpair *qpair,
2412 			     struct spdk_nvme_transport_id *trid, bool peer)
2413 {
2414 	struct spdk_nvmf_tcp_qpair     *tqpair;
2415 	uint16_t			port;
2416 
2417 	tqpair = SPDK_CONTAINEROF(qpair, struct spdk_nvmf_tcp_qpair, qpair);
2418 	spdk_nvme_trid_populate_transport(trid, SPDK_NVME_TRANSPORT_TCP);
2419 
2420 	if (peer) {
2421 		snprintf(trid->traddr, sizeof(trid->traddr), "%s", tqpair->initiator_addr);
2422 		port = tqpair->initiator_port;
2423 	} else {
2424 		snprintf(trid->traddr, sizeof(trid->traddr), "%s", tqpair->target_addr);
2425 		port = tqpair->target_port;
2426 	}
2427 
2428 	if (spdk_sock_is_ipv4(tqpair->sock)) {
2429 		trid->adrfam = SPDK_NVMF_ADRFAM_IPV4;
2430 	} else if (spdk_sock_is_ipv6(tqpair->sock)) {
2431 		trid->adrfam = SPDK_NVMF_ADRFAM_IPV6;
2432 	} else {
2433 		return -1;
2434 	}
2435 
2436 	snprintf(trid->trsvcid, sizeof(trid->trsvcid), "%d", port);
2437 	return 0;
2438 }
2439 
2440 static int
2441 spdk_nvmf_tcp_qpair_get_local_trid(struct spdk_nvmf_qpair *qpair,
2442 				   struct spdk_nvme_transport_id *trid)
2443 {
2444 	return spdk_nvmf_tcp_qpair_get_trid(qpair, trid, 0);
2445 }
2446 
2447 static int
2448 spdk_nvmf_tcp_qpair_get_peer_trid(struct spdk_nvmf_qpair *qpair,
2449 				  struct spdk_nvme_transport_id *trid)
2450 {
2451 	return spdk_nvmf_tcp_qpair_get_trid(qpair, trid, 1);
2452 }
2453 
2454 static int
2455 spdk_nvmf_tcp_qpair_get_listen_trid(struct spdk_nvmf_qpair *qpair,
2456 				    struct spdk_nvme_transport_id *trid)
2457 {
2458 	return spdk_nvmf_tcp_qpair_get_trid(qpair, trid, 0);
2459 }
2460 
2461 #define SPDK_NVMF_TCP_DEFAULT_MAX_QUEUE_DEPTH 128
2462 #define SPDK_NVMF_TCP_DEFAULT_AQ_DEPTH 128
2463 #define SPDK_NVMF_TCP_DEFAULT_MAX_QPAIRS_PER_CTRLR 128
2464 #define SPDK_NVMF_TCP_DEFAULT_IN_CAPSULE_DATA_SIZE 4096
2465 #define SPDK_NVMF_TCP_DEFAULT_MAX_IO_SIZE 131072
2466 #define SPDK_NVMF_TCP_DEFAULT_IO_UNIT_SIZE 131072
2467 #define SPDK_NVMF_TCP_DEFAULT_NUM_SHARED_BUFFERS 511
2468 #define SPDK_NVMF_TCP_DEFAULT_BUFFER_CACHE_SIZE 32
2469 #define SPDK_NVMF_TCP_DEFAULT_SUCCESS_OPTIMIZATION true
2470 #define SPDK_NVMF_TCP_DEFAULT_DIF_INSERT_OR_STRIP false
2471 #define SPDK_NVMF_TCP_DEFAULT_SOCK_PRIORITY 0
2472 
2473 static void
2474 spdk_nvmf_tcp_opts_init(struct spdk_nvmf_transport_opts *opts)
2475 {
2476 	opts->max_queue_depth =		SPDK_NVMF_TCP_DEFAULT_MAX_QUEUE_DEPTH;
2477 	opts->max_qpairs_per_ctrlr =	SPDK_NVMF_TCP_DEFAULT_MAX_QPAIRS_PER_CTRLR;
2478 	opts->in_capsule_data_size =	SPDK_NVMF_TCP_DEFAULT_IN_CAPSULE_DATA_SIZE;
2479 	opts->max_io_size =		SPDK_NVMF_TCP_DEFAULT_MAX_IO_SIZE;
2480 	opts->io_unit_size =		SPDK_NVMF_TCP_DEFAULT_IO_UNIT_SIZE;
2481 	opts->max_aq_depth =		SPDK_NVMF_TCP_DEFAULT_AQ_DEPTH;
2482 	opts->num_shared_buffers =	SPDK_NVMF_TCP_DEFAULT_NUM_SHARED_BUFFERS;
2483 	opts->buf_cache_size =		SPDK_NVMF_TCP_DEFAULT_BUFFER_CACHE_SIZE;
2484 	opts->c2h_success =		SPDK_NVMF_TCP_DEFAULT_SUCCESS_OPTIMIZATION;
2485 	opts->dif_insert_or_strip =	SPDK_NVMF_TCP_DEFAULT_DIF_INSERT_OR_STRIP;
2486 	opts->sock_priority =		SPDK_NVMF_TCP_DEFAULT_SOCK_PRIORITY;
2487 }
2488 
2489 const struct spdk_nvmf_transport_ops spdk_nvmf_transport_tcp = {
2490 	.name = "TCP",
2491 	.type = SPDK_NVME_TRANSPORT_TCP,
2492 	.opts_init = spdk_nvmf_tcp_opts_init,
2493 	.create = spdk_nvmf_tcp_create,
2494 	.destroy = spdk_nvmf_tcp_destroy,
2495 
2496 	.listen = spdk_nvmf_tcp_listen,
2497 	.stop_listen = spdk_nvmf_tcp_stop_listen,
2498 	.accept = spdk_nvmf_tcp_accept,
2499 
2500 	.listener_discover = spdk_nvmf_tcp_discover,
2501 
2502 	.poll_group_create = spdk_nvmf_tcp_poll_group_create,
2503 	.get_optimal_poll_group = spdk_nvmf_tcp_get_optimal_poll_group,
2504 	.poll_group_destroy = spdk_nvmf_tcp_poll_group_destroy,
2505 	.poll_group_add = spdk_nvmf_tcp_poll_group_add,
2506 	.poll_group_remove = spdk_nvmf_tcp_poll_group_remove,
2507 	.poll_group_poll = spdk_nvmf_tcp_poll_group_poll,
2508 
2509 	.req_free = spdk_nvmf_tcp_req_free,
2510 	.req_complete = spdk_nvmf_tcp_req_complete,
2511 
2512 	.qpair_fini = spdk_nvmf_tcp_close_qpair,
2513 	.qpair_get_local_trid = spdk_nvmf_tcp_qpair_get_local_trid,
2514 	.qpair_get_peer_trid = spdk_nvmf_tcp_qpair_get_peer_trid,
2515 	.qpair_get_listen_trid = spdk_nvmf_tcp_qpair_get_listen_trid,
2516 };
2517 
2518 SPDK_NVMF_TRANSPORT_REGISTER(tcp, &spdk_nvmf_transport_tcp);
2519 SPDK_LOG_REGISTER_COMPONENT("nvmf_tcp", SPDK_LOG_NVMF_TCP)
2520