xref: /spdk/lib/nvmf/tcp.c (revision 72f8c6a1f3f4aa1b3c373ced13e8d0ec06825ddc)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright (c) Intel Corporation.
5  *   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.h"
40 #include "spdk/nvmf_spec.h"
41 #include "spdk/sock.h"
42 #include "spdk/string.h"
43 #include "spdk/trace.h"
44 #include "spdk/util.h"
45 
46 #include "nvmf_internal.h"
47 #include "transport.h"
48 
49 #include "spdk_internal/log.h"
50 #include "spdk_internal/nvme_tcp.h"
51 
52 /*
53  * AIO backend requires block size aligned data buffers,
54  * extra 4KiB aligned data buffer should work for most devices.
55  */
56 #define SHIFT_4KB			12u
57 #define NVMF_DATA_BUFFER_ALIGNMENT	(1u << SHIFT_4KB)
58 #define NVMF_DATA_BUFFER_MASK		(NVMF_DATA_BUFFER_ALIGNMENT - 1LL)
59 #define NVMF_TCP_MAX_ACCEPT_SOCK_ONE_TIME 16
60 
61 #define NVMF_TCP_PDU_MAX_H2C_DATA_SIZE	131072
62 #define NVMF_TCP_PDU_MAX_C2H_DATA_SIZE	131072
63 #define NVMF_TCP_QPAIR_MAX_C2H_PDU_NUM  64  /* Maximal c2h_data pdu number for ecah tqpair */
64 
65 /* This is used to support the Linux kernel NVMe-oF initiator */
66 #define LINUX_KERNEL_SUPPORT_NOT_SENDING_RESP_FOR_C2H 0
67 
68 /* spdk nvmf related structure */
69 enum spdk_nvmf_tcp_req_state {
70 
71 	/* The request is not currently in use */
72 	TCP_REQUEST_STATE_FREE = 0,
73 
74 	/* Initial state when request first received */
75 	TCP_REQUEST_STATE_NEW,
76 
77 	/* The request is queued until a data buffer is available. */
78 	TCP_REQUEST_STATE_NEED_BUFFER,
79 
80 	/* The request is pending on r2t slots */
81 	TCP_REQUEST_STATE_DATA_PENDING_FOR_R2T,
82 
83 	/* The request is currently transferring data from the host to the controller. */
84 	TCP_REQUEST_STATE_TRANSFERRING_HOST_TO_CONTROLLER,
85 
86 	/* The request is ready to execute at the block device */
87 	TCP_REQUEST_STATE_READY_TO_EXECUTE,
88 
89 	/* The request is currently executing at the block device */
90 	TCP_REQUEST_STATE_EXECUTING,
91 
92 	/* The request finished executing at the block device */
93 	TCP_REQUEST_STATE_EXECUTED,
94 
95 	/* The request is ready to send a completion */
96 	TCP_REQUEST_STATE_READY_TO_COMPLETE,
97 
98 	/* The request is currently transferring final pdus from the controller to the host. */
99 	TCP_REQUEST_STATE_TRANSFERRING_CONTROLLER_TO_HOST,
100 
101 	/* The request completed and can be marked free. */
102 	TCP_REQUEST_STATE_COMPLETED,
103 
104 	/* Terminator */
105 	TCP_REQUEST_NUM_STATES,
106 };
107 
108 static const char *spdk_nvme_tcp_term_req_fes_str[] = {
109 	"Invalid PDU Header Field",
110 	"PDU Sequence Error",
111 	"Header Digiest Error",
112 	"Data Transfer Out of Range",
113 	"R2T Limit Exceeded",
114 	"Unsupported parameter",
115 };
116 
117 #define OBJECT_NVMF_TCP_IO				0x80
118 
119 #define TRACE_GROUP_NVMF_TCP				0x5
120 #define TRACE_TCP_REQUEST_STATE_NEW					SPDK_TPOINT_ID(TRACE_GROUP_NVMF_TCP, 0x0)
121 #define TRACE_TCP_REQUEST_STATE_NEED_BUFFER				SPDK_TPOINT_ID(TRACE_GROUP_NVMF_TCP, 0x1)
122 #define TRACE_TCP_REQUEST_STATE_DATA_PENDING_FOR_R2T			SPDK_TPOINT_ID(TRACE_GROUP_NVMF_TCP, 0x2)
123 #define TRACE_TCP_REQUEST_STATE_TRANSFERRING_HOST_TO_CONTROLLER		SPDK_TPOINT_ID(TRACE_GROUP_NVMF_TCP, 0x3)
124 #define TRACE_TCP_REQUEST_STATE_READY_TO_EXECUTE			SPDK_TPOINT_ID(TRACE_GROUP_NVMF_TCP, 0x4)
125 #define TRACE_TCP_REQUEST_STATE_EXECUTING				SPDK_TPOINT_ID(TRACE_GROUP_NVMF_TCP, 0x5)
126 #define TRACE_TCP_REQUEST_STATE_EXECUTED				SPDK_TPOINT_ID(TRACE_GROUP_NVMF_TCP, 0x6)
127 #define TRACE_TCP_REQUEST_STATE_READY_TO_COMPLETE			SPDK_TPOINT_ID(TRACE_GROUP_NVMF_TCP, 0x7)
128 #define TRACE_TCP_REQUEST_STATE_TRANSFERRING_CONTROLLER_TO_HOST		SPDK_TPOINT_ID(TRACE_GROUP_NVMF_TCP, 0x8)
129 #define TRACE_TCP_REQUEST_STATE_COMPLETED				SPDK_TPOINT_ID(TRACE_GROUP_NVMF_TCP, 0x9)
130 #define TRACE_TCP_FLUSH_WRITEBUF_START					SPDK_TPOINT_ID(TRACE_GROUP_NVMF_TCP, 0xA)
131 #define TRACE_TCP_FLUSH_WRITEBUF_DONE					SPDK_TPOINT_ID(TRACE_GROUP_NVMF_TCP, 0xB)
132 #define TRACE_TCP_FLUSH_WRITEBUF_PDU_DONE				SPDK_TPOINT_ID(TRACE_GROUP_NVMF_TCP, 0xC)
133 
134 SPDK_TRACE_REGISTER_FN(nvmf_tcp_trace, "nvmf_tcp", TRACE_GROUP_NVMF_TCP)
135 {
136 	spdk_trace_register_object(OBJECT_NVMF_TCP_IO, 'r');
137 	spdk_trace_register_description("TCP_REQ_NEW", "",
138 					TRACE_TCP_REQUEST_STATE_NEW,
139 					OWNER_NONE, OBJECT_NVMF_TCP_IO, 1, 1, "");
140 	spdk_trace_register_description("TCP_REQ_NEED_BUFFER", "",
141 					TRACE_TCP_REQUEST_STATE_NEED_BUFFER,
142 					OWNER_NONE, OBJECT_NVMF_TCP_IO, 0, 1, "");
143 	spdk_trace_register_description("TCP_REQ_TX_PENDING_R2T", "",
144 					TRACE_TCP_REQUEST_STATE_DATA_PENDING_FOR_R2T,
145 					OWNER_NONE, OBJECT_NVMF_TCP_IO, 0, 1, "");
146 	spdk_trace_register_description("TCP_REQ_TX_H_TO_C", "",
147 					TRACE_TCP_REQUEST_STATE_TRANSFERRING_HOST_TO_CONTROLLER,
148 					OWNER_NONE, OBJECT_NVMF_TCP_IO, 0, 1, "");
149 	spdk_trace_register_description("TCP_REQ_RDY_TO_EXECUTE", "",
150 					TRACE_TCP_REQUEST_STATE_READY_TO_EXECUTE,
151 					OWNER_NONE, OBJECT_NVMF_TCP_IO, 0, 1, "");
152 	spdk_trace_register_description("TCP_REQ_EXECUTING", "",
153 					TRACE_TCP_REQUEST_STATE_EXECUTING,
154 					OWNER_NONE, OBJECT_NVMF_TCP_IO, 0, 1, "");
155 	spdk_trace_register_description("TCP_REQ_EXECUTED", "",
156 					TRACE_TCP_REQUEST_STATE_EXECUTED,
157 					OWNER_NONE, OBJECT_NVMF_TCP_IO, 0, 1, "");
158 	spdk_trace_register_description("TCP_REQ_RDY_TO_COMPLETE", "",
159 					TRACE_TCP_REQUEST_STATE_READY_TO_COMPLETE,
160 					OWNER_NONE, OBJECT_NVMF_TCP_IO, 0, 1, "");
161 	spdk_trace_register_description("TCP_REQ_COMPLETING_INCAPSULE", "",
162 					TRACE_TCP_REQUEST_STATE_TRANSFERRING_CONTROLLER_TO_HOST,
163 					OWNER_NONE, OBJECT_NVMF_TCP_IO, 0, 1, "");
164 	spdk_trace_register_description("TCP_REQ_COMPLETED", "",
165 					TRACE_TCP_REQUEST_STATE_COMPLETED,
166 					OWNER_NONE, OBJECT_NVMF_TCP_IO, 0, 1, "");
167 	spdk_trace_register_description("TCP_FLUSH_WRITEBUF_START", "",
168 					TRACE_TCP_FLUSH_WRITEBUF_START,
169 					OWNER_NONE, OBJECT_NONE, 0, 0, "");
170 	spdk_trace_register_description("TCP_FLUSH_WRITEBUF_DONE", "",
171 					TRACE_TCP_FLUSH_WRITEBUF_DONE,
172 					OWNER_NONE, OBJECT_NONE, 0, 0, "");
173 	spdk_trace_register_description("TCP_FLUSH_WRITEBUF_PDU_DONE", "",
174 					TRACE_TCP_FLUSH_WRITEBUF_PDU_DONE,
175 					OWNER_NONE, OBJECT_NONE, 0, 0, "");
176 }
177 
178 struct nvme_tcp_req  {
179 	struct spdk_nvmf_request		req;
180 	struct spdk_nvme_cpl			rsp;
181 	struct spdk_nvme_cmd			cmd;
182 
183 	/* In-capsule data buffer */
184 	uint8_t					*buf;
185 
186 	bool					data_from_pool;
187 	void					*buffers[SPDK_NVMF_MAX_SGL_ENTRIES];
188 
189 	/* transfer_tag */
190 	uint16_t				ttag;
191 
192 	/*
193 	 * next_expected_r2t_offset is used when we receive the h2c_data PDU.
194 	 */
195 	uint32_t				next_expected_r2t_offset;
196 	uint32_t				r2tl_remain;
197 
198 	/*
199 	 * c2h_data_offset is used when we send the c2h_data PDU.
200 	 */
201 	uint32_t				c2h_data_offset;
202 	uint32_t				c2h_data_pdu_num;
203 
204 	enum spdk_nvmf_tcp_req_state	state;
205 
206 	TAILQ_ENTRY(nvme_tcp_req)		link;
207 	TAILQ_ENTRY(nvme_tcp_req)		state_link;
208 };
209 
210 struct nvme_tcp_qpair {
211 	struct spdk_nvmf_qpair			qpair;
212 	struct spdk_nvmf_tcp_port		*port;
213 	struct spdk_sock			*sock;
214 	struct spdk_poller			*flush_poller;
215 
216 	enum nvme_tcp_pdu_recv_state		recv_state;
217 	enum nvme_tcp_qpair_state		state;
218 
219 	struct nvme_tcp_pdu			pdu_in_progress;
220 
221 	TAILQ_HEAD(, nvme_tcp_pdu)		send_queue;
222 	TAILQ_HEAD(, nvme_tcp_pdu)		free_queue;
223 
224 	struct nvme_tcp_pdu			*pdu;
225 	struct nvme_tcp_pdu			*pdu_pool;
226 	uint16_t				free_pdu_num;
227 
228 	/* Queues to track the requests in all states */
229 	TAILQ_HEAD(, nvme_tcp_req)		state_queue[TCP_REQUEST_NUM_STATES];
230 	/* Number of requests in each state */
231 	int32_t					state_cntr[TCP_REQUEST_NUM_STATES];
232 
233 	uint32_t				maxr2t;
234 	uint32_t				pending_r2t;
235 	TAILQ_HEAD(, nvme_tcp_req)		queued_r2t_tcp_req;
236 	TAILQ_HEAD(, nvme_tcp_req)		queued_c2h_data_tcp_req;
237 
238 	uint8_t					cpda;
239 
240 	/* Array of size "max_queue_depth * InCapsuleDataSize" containing
241 	 * buffers to be used for in capsule data.
242 	 */
243 	void					*buf;
244 	void					*bufs;
245 	struct nvme_tcp_req			*req;
246 	struct nvme_tcp_req			*reqs;
247 
248 	bool					host_hdgst_enable;
249 	bool					host_ddgst_enable;
250 
251 
252 	/* The maximum number of I/O outstanding on this connection at one time */
253 	uint16_t				max_queue_depth;
254 
255 
256 	/** Specifies the maximum number of PDU-Data bytes per H2C Data Transfer PDU */
257 	uint32_t				maxh2cdata;
258 
259 	/* Timer used to destroy qpair after detecting transport error issue if initiator does
260 	 *  not close the connection.
261 	 */
262 	uint64_t				last_pdu_time;
263 	int					timeout;
264 
265 	/* Mgmt channel */
266 	struct spdk_io_channel			*mgmt_channel;
267 	struct spdk_nvmf_tcp_mgmt_channel	*ch;
268 
269 	uint32_t				c2h_data_pdu_cnt;
270 
271 	/* IP address */
272 	char					initiator_addr[SPDK_NVMF_TRADDR_MAX_LEN];
273 	char					target_addr[SPDK_NVMF_TRADDR_MAX_LEN];
274 
275 	/* IP port */
276 	uint16_t				initiator_port;
277 	uint16_t				target_port;
278 
279 	/* qpair->group is freed early, this should be a temporal fix */
280 	struct spdk_nvmf_tcp_poll_group		*tgroup;
281 	TAILQ_ENTRY(nvme_tcp_qpair)		link;
282 };
283 
284 struct spdk_nvmf_tcp_poll_group {
285 	struct spdk_nvmf_transport_poll_group	group;
286 	struct spdk_sock_group			*sock_group;
287 	TAILQ_HEAD(, nvme_tcp_qpair)		qpairs;
288 };
289 
290 struct spdk_nvmf_tcp_port {
291 	struct spdk_nvme_transport_id		trid;
292 	struct spdk_sock			*listen_sock;
293 	uint32_t				ref;
294 	TAILQ_ENTRY(spdk_nvmf_tcp_port)		link;
295 };
296 
297 struct spdk_nvmf_tcp_transport {
298 	struct spdk_nvmf_transport		transport;
299 
300 	pthread_mutex_t				lock;
301 
302 	struct spdk_mempool			*data_buf_pool;
303 
304 	uint16_t				max_queue_depth;
305 	uint32_t				max_io_size;
306 	uint32_t				io_unit_size;
307 	uint32_t				in_capsule_data_size;
308 
309 	TAILQ_HEAD(, spdk_nvmf_tcp_port)	ports;
310 };
311 
312 struct spdk_nvmf_tcp_mgmt_channel {
313 	/* Requests that are waiting to obtain a data buffer */
314 	TAILQ_HEAD(, nvme_tcp_req)	pending_data_buf_queue;
315 };
316 
317 static bool spdk_nvmf_tcp_req_process(struct spdk_nvmf_tcp_transport *ttransport,
318 				      struct nvme_tcp_req *tcp_req);
319 static void spdk_nvmf_tcp_handle_pending_c2h_data_queue(struct nvme_tcp_qpair *tqpair);
320 
321 static void
322 spdk_nvmf_tcp_req_set_state(struct nvme_tcp_req *tcp_req,
323 			    enum spdk_nvmf_tcp_req_state state)
324 {
325 	struct spdk_nvmf_qpair *qpair;
326 	struct nvme_tcp_qpair *tqpair;
327 
328 	qpair = tcp_req->req.qpair;
329 	tqpair = SPDK_CONTAINEROF(qpair, struct nvme_tcp_qpair, qpair);
330 
331 	TAILQ_REMOVE(&tqpair->state_queue[tcp_req->state], tcp_req, state_link);
332 	tqpair->state_cntr[tcp_req->state]--;
333 	assert(tqpair->state_cntr[tcp_req->state] >= 0);
334 
335 	TAILQ_INSERT_TAIL(&tqpair->state_queue[state], tcp_req, state_link);
336 	tqpair->state_cntr[state]++;
337 
338 	tcp_req->state = state;
339 }
340 
341 static struct nvme_tcp_pdu *
342 spdk_nvmf_tcp_pdu_get(struct nvme_tcp_qpair *tqpair)
343 {
344 	struct nvme_tcp_pdu *pdu;
345 
346 	pdu = TAILQ_FIRST(&tqpair->free_queue);
347 	if (!pdu) {
348 		SPDK_ERRLOG("Unable to get PDU for tqpair=%p\n", tqpair);
349 		abort();
350 		return NULL;
351 	}
352 
353 	tqpair->free_pdu_num--;
354 	TAILQ_REMOVE(&tqpair->free_queue, pdu, tailq);
355 	memset(pdu, 0, sizeof(*pdu));
356 	pdu->ref = 1;
357 	pdu->tqpair = tqpair;
358 
359 	return pdu;
360 }
361 
362 static void
363 spdk_nvmf_tcp_pdu_put(struct nvme_tcp_pdu *pdu)
364 {
365 	struct nvme_tcp_qpair *tqpair;
366 	if (!pdu) {
367 		return;
368 	}
369 
370 	assert(pdu->ref > 0);
371 	assert(pdu->tqpair != NULL);
372 	tqpair = pdu->tqpair;
373 
374 	pdu->ref--;
375 	if (pdu->ref == 0) {
376 		tqpair->free_pdu_num++;
377 		TAILQ_INSERT_HEAD(&tqpair->free_queue, pdu, tailq);
378 	}
379 }
380 
381 static struct nvme_tcp_req *
382 spdk_nvmf_tcp_req_get(struct nvme_tcp_qpair *tqpair)
383 {
384 	struct nvme_tcp_req *tcp_req;
385 
386 	tcp_req = TAILQ_FIRST(&tqpair->state_queue[TCP_REQUEST_STATE_FREE]);
387 	if (!tcp_req) {
388 		SPDK_ERRLOG("Cannot allocate tcp_req on tqpair=%p\n", tqpair);
389 		return NULL;
390 	}
391 
392 	memset(&tcp_req->cmd, 0, sizeof(tcp_req->cmd));
393 	memset(&tcp_req->rsp, 0, sizeof(tcp_req->rsp));
394 	tcp_req->next_expected_r2t_offset = 0;
395 	tcp_req->r2tl_remain = 0;
396 	tcp_req->c2h_data_offset = 0;
397 
398 	spdk_nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_NEW);
399 	return tcp_req;
400 }
401 
402 static void
403 nvmf_tcp_request_free(struct nvme_tcp_req *tcp_req)
404 {
405 	struct spdk_nvmf_tcp_transport *ttransport;
406 
407 	if (!tcp_req) {
408 		return;
409 	}
410 
411 	SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "tcp_req=%p will be freed\n", tcp_req);
412 	ttransport = SPDK_CONTAINEROF(tcp_req->req.qpair->transport,
413 				      struct spdk_nvmf_tcp_transport, transport);
414 	spdk_nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_COMPLETED);
415 	spdk_nvmf_tcp_req_process(ttransport, tcp_req);
416 }
417 
418 static int
419 spdk_nvmf_tcp_req_free(struct spdk_nvmf_request *req)
420 {
421 	struct nvme_tcp_req *tcp_req = SPDK_CONTAINEROF(req, struct nvme_tcp_req, req);
422 
423 	nvmf_tcp_request_free(tcp_req);
424 
425 	return 0;
426 }
427 
428 static int
429 spdk_nvmf_tcp_mgmt_channel_create(void *io_device, void *ctx_buf)
430 {
431 	struct spdk_nvmf_tcp_mgmt_channel *ch = ctx_buf;
432 
433 	TAILQ_INIT(&ch->pending_data_buf_queue);
434 	return 0;
435 }
436 
437 static void
438 spdk_nvmf_tcp_mgmt_channel_destroy(void *io_device, void *ctx_buf)
439 {
440 	struct spdk_nvmf_tcp_mgmt_channel *ch = ctx_buf;
441 
442 	if (!TAILQ_EMPTY(&ch->pending_data_buf_queue)) {
443 		SPDK_ERRLOG("Pending I/O list wasn't empty on channel destruction\n");
444 	}
445 }
446 
447 static void
448 spdk_nvmf_tcp_drain_state_queue(struct nvme_tcp_qpair *tqpair,
449 				enum spdk_nvmf_tcp_req_state state)
450 {
451 	struct nvme_tcp_req *tcp_req, *req_tmp;
452 
453 	TAILQ_FOREACH_SAFE(tcp_req, &tqpair->state_queue[state], state_link, req_tmp) {
454 		nvmf_tcp_request_free(tcp_req);
455 	}
456 }
457 
458 static void
459 spdk_nvmf_tcp_cleanup_all_states(struct nvme_tcp_qpair *tqpair)
460 {
461 	struct nvme_tcp_req *tcp_req, *req_tmp;
462 	struct nvme_tcp_pdu *pdu, *tmp_pdu;
463 
464 	/* For the requests in TCP_REQUEST_STATE_TRANSFERRING_CONTROLLER_TO_HOST,
465 	 * they can be refreed via the pdu in the send_queue
466 	  */
467 	TAILQ_FOREACH_SAFE(pdu, &tqpair->send_queue, tailq, tmp_pdu) {
468 		TAILQ_REMOVE(&tqpair->send_queue, pdu, tailq);
469 		assert(pdu->cb_fn != NULL);
470 		pdu->cb_fn(pdu->cb_arg);
471 		spdk_nvmf_tcp_pdu_put(pdu);
472 	}
473 
474 	spdk_nvmf_tcp_drain_state_queue(tqpair, TCP_REQUEST_STATE_NEW);
475 
476 	/* Wipe the requests waiting for R2t  */
477 	TAILQ_FOREACH_SAFE(tcp_req, &tqpair->state_queue[TCP_REQUEST_STATE_DATA_PENDING_FOR_R2T], link,
478 			   req_tmp) {
479 		TAILQ_REMOVE(&tqpair->queued_r2t_tcp_req, tcp_req, link);
480 	}
481 	spdk_nvmf_tcp_drain_state_queue(tqpair, TCP_REQUEST_STATE_DATA_PENDING_FOR_R2T);
482 
483 	/* Wipe the requests waiting for buffer from the global list */
484 	TAILQ_FOREACH_SAFE(tcp_req, &tqpair->state_queue[TCP_REQUEST_STATE_NEED_BUFFER], link, req_tmp) {
485 		TAILQ_REMOVE(&tqpair->ch->pending_data_buf_queue, tcp_req, link);
486 	}
487 
488 	spdk_nvmf_tcp_drain_state_queue(tqpair, TCP_REQUEST_STATE_NEED_BUFFER);
489 	spdk_nvmf_tcp_drain_state_queue(tqpair, TCP_REQUEST_STATE_EXECUTING);
490 
491 	TAILQ_FOREACH_SAFE(tcp_req, &tqpair->queued_c2h_data_tcp_req, link, req_tmp) {
492 		TAILQ_REMOVE(&tqpair->queued_c2h_data_tcp_req, tcp_req, link);
493 	}
494 
495 	spdk_nvmf_tcp_drain_state_queue(tqpair, TCP_REQUEST_STATE_TRANSFERRING_HOST_TO_CONTROLLER);
496 }
497 
498 static void
499 spdk_nvmf_tcp_qpair_destroy(struct nvme_tcp_qpair *tqpair)
500 {
501 	struct spdk_nvmf_tcp_poll_group *tgroup;
502 	int                             rc;
503 
504 	SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "enter\n");
505 	tgroup = tqpair->tgroup;
506 
507 	if (tgroup) {
508 		SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "remove tqpair=%p from the tgroup=%p\n", tqpair, tgroup);
509 		TAILQ_REMOVE(&tgroup->qpairs, tqpair, link);
510 		rc = spdk_sock_group_remove_sock(tgroup->sock_group, tqpair->sock);
511 		if (rc != 0) {
512 			SPDK_ERRLOG("Could not remove sock from sock_group: %s (%d)\n",
513 				    spdk_strerror(errno), errno);
514 		}
515 	}
516 
517 	spdk_poller_unregister(&tqpair->flush_poller);
518 	spdk_sock_close(&tqpair->sock);
519 	spdk_nvmf_tcp_cleanup_all_states(tqpair);
520 	if (tqpair->mgmt_channel) {
521 		spdk_put_io_channel(tqpair->mgmt_channel);
522 	}
523 
524 	if (tqpair->free_pdu_num != (tqpair->max_queue_depth + NVMF_TCP_QPAIR_MAX_C2H_PDU_NUM)) {
525 		SPDK_ERRLOG("tqpair(%p) free pdu pool num is %u but should be %u\n", tqpair,
526 			    tqpair->free_pdu_num,
527 			    (tqpair->max_queue_depth + NVMF_TCP_QPAIR_MAX_C2H_PDU_NUM));
528 	}
529 
530 	if (tqpair->state_cntr[TCP_REQUEST_STATE_FREE] != tqpair->max_queue_depth) {
531 		SPDK_ERRLOG("tqpair(%p) free tcp request num is %u but should be %u\n", tqpair,
532 			    tqpair->state_cntr[TCP_REQUEST_STATE_FREE],
533 			    tqpair->max_queue_depth);
534 	}
535 
536 	if (tqpair->c2h_data_pdu_cnt != 0) {
537 		SPDK_ERRLOG("tqpair(%p) free c2h_data_pdu cnt is %u but should be 0\n", tqpair,
538 			    tqpair->c2h_data_pdu_cnt);
539 	}
540 
541 	free(tqpair->pdu);
542 	free(tqpair->pdu_pool);
543 	free(tqpair->req);
544 	free(tqpair->reqs);
545 	spdk_dma_free(tqpair->buf);
546 	spdk_dma_free(tqpair->bufs);
547 	free(tqpair);
548 	SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "Leave\n");
549 }
550 
551 static struct spdk_nvmf_transport *
552 spdk_nvmf_tcp_create(struct spdk_nvmf_transport_opts *opts)
553 {
554 	struct spdk_nvmf_tcp_transport *ttransport;
555 	uint32_t			sge_count;
556 
557 	ttransport = calloc(1, sizeof(*ttransport));
558 	if (!ttransport) {
559 		return NULL;
560 	}
561 
562 	TAILQ_INIT(&ttransport->ports);
563 
564 	ttransport->transport.ops = &spdk_nvmf_transport_tcp;
565 
566 	SPDK_NOTICELOG("*** TCP Transport Init ***\n");
567 
568 	ttransport->max_queue_depth = opts->max_queue_depth;
569 	ttransport->max_io_size = opts->max_io_size;
570 	ttransport->in_capsule_data_size = opts->in_capsule_data_size;
571 	ttransport->io_unit_size = opts->io_unit_size;
572 
573 	/* I/O unit size cannot be larger than max I/O size */
574 	if (ttransport->io_unit_size > ttransport->max_io_size) {
575 		ttransport->io_unit_size = ttransport->max_io_size;
576 	}
577 
578 	sge_count = ttransport->max_io_size / ttransport->io_unit_size;
579 	if (sge_count > SPDK_NVMF_MAX_SGL_ENTRIES) {
580 		SPDK_ERRLOG("Unsupported IO Unit size specified, %d bytes\n", ttransport->io_unit_size);
581 		free(ttransport);
582 		return NULL;
583 	}
584 
585 	ttransport->data_buf_pool = spdk_mempool_create("spdk_nvmf_tcp_data",
586 				    ttransport->max_queue_depth * 4, /* The 4 is arbitrarily chosen. Needs to be configurable. */
587 				    ttransport->max_io_size + NVMF_DATA_BUFFER_ALIGNMENT,
588 				    SPDK_MEMPOOL_DEFAULT_CACHE_SIZE,
589 				    SPDK_ENV_SOCKET_ID_ANY);
590 
591 	if (!ttransport->data_buf_pool) {
592 		SPDK_ERRLOG("Unable to allocate buffer pool for poll group\n");
593 		free(ttransport);
594 		return NULL;
595 	}
596 
597 	pthread_mutex_init(&ttransport->lock, NULL);
598 
599 	spdk_io_device_register(ttransport, spdk_nvmf_tcp_mgmt_channel_create,
600 				spdk_nvmf_tcp_mgmt_channel_destroy,
601 				sizeof(struct spdk_nvmf_tcp_mgmt_channel), "tcp_transport");
602 
603 	return &ttransport->transport;
604 }
605 
606 static int
607 spdk_nvmf_tcp_destroy(struct spdk_nvmf_transport *transport)
608 {
609 	struct spdk_nvmf_tcp_transport	*ttransport;
610 
611 	ttransport = SPDK_CONTAINEROF(transport, struct spdk_nvmf_tcp_transport, transport);
612 
613 	if (spdk_mempool_count(ttransport->data_buf_pool) != (ttransport->max_queue_depth * 4)) {
614 		SPDK_ERRLOG("transport buffer pool count is %zu but should be %u\n",
615 			    spdk_mempool_count(ttransport->data_buf_pool),
616 			    ttransport->max_queue_depth * 4);
617 	}
618 
619 	spdk_mempool_free(ttransport->data_buf_pool);
620 	spdk_io_device_unregister(ttransport, NULL);
621 	pthread_mutex_destroy(&ttransport->lock);
622 	free(ttransport);
623 	return 0;
624 }
625 
626 static int
627 _spdk_nvmf_tcp_trsvcid_to_int(const char *trsvcid)
628 {
629 	unsigned long long ull;
630 	char *end = NULL;
631 
632 	ull = strtoull(trsvcid, &end, 10);
633 	if (end == NULL || end == trsvcid || *end != '\0') {
634 		return -1;
635 	}
636 
637 	/* Valid TCP/IP port numbers are in [0, 65535] */
638 	if (ull > 65535) {
639 		return -1;
640 	}
641 
642 	return (int)ull;
643 }
644 
645 /**
646  * Canonicalize a listen address trid.
647  */
648 static int
649 _spdk_nvmf_tcp_canon_listen_trid(struct spdk_nvme_transport_id *canon_trid,
650 				 const struct spdk_nvme_transport_id *trid)
651 {
652 	int trsvcid_int;
653 
654 	trsvcid_int = _spdk_nvmf_tcp_trsvcid_to_int(trid->trsvcid);
655 	if (trsvcid_int < 0) {
656 		return -EINVAL;
657 	}
658 
659 	memset(canon_trid, 0, sizeof(*canon_trid));
660 	canon_trid->trtype = SPDK_NVME_TRANSPORT_TCP;
661 	canon_trid->adrfam = trid->adrfam;
662 	snprintf(canon_trid->traddr, sizeof(canon_trid->traddr), "%s", trid->traddr);
663 	snprintf(canon_trid->trsvcid, sizeof(canon_trid->trsvcid), "%d", trsvcid_int);
664 
665 	return 0;
666 }
667 
668 /**
669  * Find an existing listening port.
670  *
671  * Caller must hold ttransport->lock.
672  */
673 static struct spdk_nvmf_tcp_port *
674 _spdk_nvmf_tcp_find_port(struct spdk_nvmf_tcp_transport *ttransport,
675 			 const struct spdk_nvme_transport_id *trid)
676 {
677 	struct spdk_nvme_transport_id canon_trid;
678 	struct spdk_nvmf_tcp_port *port;
679 
680 	if (_spdk_nvmf_tcp_canon_listen_trid(&canon_trid, trid) != 0) {
681 		return NULL;
682 	}
683 
684 	TAILQ_FOREACH(port, &ttransport->ports, link) {
685 		if (spdk_nvme_transport_id_compare(&canon_trid, &port->trid) == 0) {
686 			return port;
687 		}
688 	}
689 
690 	return NULL;
691 }
692 
693 static int
694 spdk_nvmf_tcp_listen(struct spdk_nvmf_transport *transport,
695 		     const struct spdk_nvme_transport_id *trid)
696 {
697 	struct spdk_nvmf_tcp_transport *ttransport;
698 	struct spdk_nvmf_tcp_port *port;
699 	int trsvcid_int;
700 	uint8_t adrfam;
701 
702 	ttransport = SPDK_CONTAINEROF(transport, struct spdk_nvmf_tcp_transport, transport);
703 
704 	trsvcid_int = _spdk_nvmf_tcp_trsvcid_to_int(trid->trsvcid);
705 	if (trsvcid_int < 0) {
706 		SPDK_ERRLOG("Invalid trsvcid '%s'\n", trid->trsvcid);
707 		return -EINVAL;
708 	}
709 
710 	pthread_mutex_lock(&ttransport->lock);
711 
712 	port = _spdk_nvmf_tcp_find_port(ttransport, trid);
713 	if (port) {
714 		SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "Already listening on %s port %s\n",
715 			      trid->traddr, trid->trsvcid);
716 		port->ref++;
717 		pthread_mutex_unlock(&ttransport->lock);
718 		return 0;
719 	}
720 
721 	port = calloc(1, sizeof(*port));
722 	if (!port) {
723 		SPDK_ERRLOG("Port allocation failed\n");
724 		free(port);
725 		pthread_mutex_unlock(&ttransport->lock);
726 		return -ENOMEM;
727 	}
728 
729 	port->ref = 1;
730 
731 	if (_spdk_nvmf_tcp_canon_listen_trid(&port->trid, trid) != 0) {
732 		SPDK_ERRLOG("Invalid traddr %s / trsvcid %s\n",
733 			    trid->traddr, trid->trsvcid);
734 		free(port);
735 		pthread_mutex_unlock(&ttransport->lock);
736 		return -ENOMEM;
737 	}
738 
739 	port->listen_sock = spdk_sock_listen(trid->traddr, trsvcid_int);
740 	if (port->listen_sock == NULL) {
741 		SPDK_ERRLOG("spdk_sock_listen(%s, %d) failed: %s (%d)\n",
742 			    trid->traddr, trsvcid_int,
743 			    spdk_strerror(errno), errno);
744 		free(port);
745 		pthread_mutex_unlock(&ttransport->lock);
746 		return -errno;
747 	}
748 
749 	if (spdk_sock_is_ipv4(port->listen_sock)) {
750 		adrfam = SPDK_NVMF_ADRFAM_IPV4;
751 	} else if (spdk_sock_is_ipv6(port->listen_sock)) {
752 		adrfam = SPDK_NVMF_ADRFAM_IPV6;
753 	} else {
754 		SPDK_ERRLOG("Unhandled socket type\n");
755 		adrfam = 0;
756 	}
757 
758 	if (adrfam != trid->adrfam) {
759 		SPDK_ERRLOG("Socket address family mismatch\n");
760 		spdk_sock_close(&port->listen_sock);
761 		free(port);
762 		pthread_mutex_unlock(&ttransport->lock);
763 		return -EINVAL;
764 	}
765 
766 	SPDK_NOTICELOG("*** NVMe/TCP Target Listening on %s port %d ***\n",
767 		       trid->traddr, trsvcid_int);
768 
769 	TAILQ_INSERT_TAIL(&ttransport->ports, port, link);
770 	pthread_mutex_unlock(&ttransport->lock);
771 
772 	return 0;
773 }
774 
775 static int
776 spdk_nvmf_tcp_stop_listen(struct spdk_nvmf_transport *transport,
777 			  const struct spdk_nvme_transport_id *trid)
778 {
779 	struct spdk_nvmf_tcp_transport *ttransport;
780 	struct spdk_nvmf_tcp_port *port;
781 	int rc;
782 
783 	ttransport = SPDK_CONTAINEROF(transport, struct spdk_nvmf_tcp_transport, transport);
784 
785 	SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "Removing listen address %s port %s\n",
786 		      trid->traddr, trid->trsvcid);
787 
788 	pthread_mutex_lock(&ttransport->lock);
789 	port = _spdk_nvmf_tcp_find_port(ttransport, trid);
790 	if (port) {
791 		assert(port->ref > 0);
792 		port->ref--;
793 		if (port->ref == 0) {
794 			TAILQ_REMOVE(&ttransport->ports, port, link);
795 			spdk_sock_close(&port->listen_sock);
796 			free(port);
797 		}
798 		rc = 0;
799 	} else {
800 		SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "Port not found\n");
801 		rc = -ENOENT;
802 	}
803 	pthread_mutex_unlock(&ttransport->lock);
804 
805 	return rc;
806 }
807 
808 static int
809 spdk_nvmf_tcp_qpair_flush_pdus_internal(struct nvme_tcp_qpair *tqpair)
810 {
811 	const int array_size = 32;
812 	struct iovec	iovec_array[array_size];
813 	struct iovec	*iov = iovec_array;
814 	int iovec_cnt = 0;
815 	int bytes = 0;
816 	int total_length = 0;
817 	uint32_t writev_offset;
818 	struct nvme_tcp_pdu *pdu;
819 	int pdu_length;
820 	TAILQ_HEAD(, nvme_tcp_pdu) completed_pdus_list;
821 
822 	pdu = TAILQ_FIRST(&tqpair->send_queue);
823 
824 	if (pdu == NULL) {
825 		return 0;
826 	}
827 
828 	/*
829 	 * Build up a list of iovecs for the first few PDUs in the
830 	 *  tqpair 's send_queue.
831 	 */
832 	while (pdu != NULL && ((array_size - iovec_cnt) >= 3)) {
833 		iovec_cnt += nvme_tcp_build_iovecs(&iovec_array[iovec_cnt],
834 						   pdu,
835 						   tqpair->host_hdgst_enable,
836 						   tqpair->host_ddgst_enable);
837 		total_length += pdu->hdr.common.plen;
838 		pdu = TAILQ_NEXT(pdu, tailq);
839 	}
840 
841 	/*
842 	 * Check if the first PDU was partially written out the last time
843 	 *  this function was called, and if so adjust the iovec array
844 	 *  accordingly.
845 	 */
846 	writev_offset = TAILQ_FIRST(&tqpair->send_queue)->writev_offset;
847 	total_length -= writev_offset;
848 	while ((writev_offset > 0) && (iovec_cnt > 0)) {
849 		if (writev_offset >= iov->iov_len) {
850 			writev_offset -= iov->iov_len;
851 			iov++;
852 			iovec_cnt--;
853 		} else {
854 			iov->iov_len -= writev_offset;
855 			iov->iov_base = (char *)iov->iov_base + writev_offset;
856 			writev_offset = 0;
857 		}
858 	}
859 
860 	spdk_trace_record(TRACE_TCP_FLUSH_WRITEBUF_START, 0, total_length, 0, iovec_cnt);
861 
862 	bytes = spdk_sock_writev(tqpair->sock, iov, iovec_cnt);
863 	if (bytes == -1) {
864 		if (errno == EWOULDBLOCK || errno == EAGAIN) {
865 			return 1;
866 		} else {
867 			SPDK_ERRLOG("spdk_sock_writev() failed, errno %d: %s\n",
868 				    errno, spdk_strerror(errno));
869 			return -1;
870 		}
871 	}
872 
873 	spdk_trace_record(TRACE_TCP_FLUSH_WRITEBUF_DONE, 0, bytes, 0, 0);
874 
875 	pdu = TAILQ_FIRST(&tqpair->send_queue);
876 
877 	/*
878 	 * Free any PDUs that were fully written.  If a PDU was only
879 	 *  partially written, update its writev_offset so that next
880 	 *  time only the unwritten portion will be sent to writev().
881 	 */
882 	TAILQ_INIT(&completed_pdus_list);
883 	while (bytes > 0) {
884 		pdu_length = pdu->hdr.common.plen - pdu->writev_offset;
885 		if (bytes >= pdu_length) {
886 			bytes -= pdu_length;
887 			TAILQ_REMOVE(&tqpair->send_queue, pdu, tailq);
888 			TAILQ_INSERT_TAIL(&completed_pdus_list, pdu, tailq);
889 			pdu = TAILQ_FIRST(&tqpair->send_queue);
890 
891 		} else {
892 			pdu->writev_offset += bytes;
893 			bytes = 0;
894 		}
895 	}
896 
897 	while (!TAILQ_EMPTY(&completed_pdus_list)) {
898 		pdu = TAILQ_FIRST(&completed_pdus_list);
899 		TAILQ_REMOVE(&completed_pdus_list, pdu, tailq);
900 		assert(pdu->cb_fn != NULL);
901 		pdu->cb_fn(pdu->cb_arg);
902 		spdk_nvmf_tcp_pdu_put(pdu);
903 	}
904 
905 	return TAILQ_EMPTY(&tqpair->send_queue) ? 0 : 1;
906 }
907 
908 static int
909 spdk_nvmf_tcp_qpair_flush_pdus(void *_tqpair)
910 {
911 	struct nvme_tcp_qpair *tqpair = _tqpair;
912 	int rc;
913 
914 	if (tqpair->state == NVME_TCP_QPAIR_STATE_RUNNING) {
915 		rc = spdk_nvmf_tcp_qpair_flush_pdus_internal(tqpair);
916 		if (rc == 0 && tqpair->flush_poller != NULL) {
917 			spdk_poller_unregister(&tqpair->flush_poller);
918 		} else if (rc == 1 && tqpair->flush_poller == NULL) {
919 			tqpair->flush_poller = spdk_poller_register(spdk_nvmf_tcp_qpair_flush_pdus,
920 					       tqpair, 50);
921 		}
922 	} else {
923 		/*
924 		 * If the tqpair state is not RUNNING, then
925 		 * keep trying to flush PDUs until our list is
926 		 * empty - to make sure all data is sent before
927 		 * closing the connection.
928 		 */
929 		do {
930 			rc = spdk_nvmf_tcp_qpair_flush_pdus_internal(tqpair);
931 		} while (rc == 1);
932 	}
933 
934 	if (rc < 0 && tqpair->state < NVME_TCP_QPAIR_STATE_EXITING) {
935 		/*
936 		 * If the poller has already started destruction of the tqpair,
937 		 *  i.e. the socket read failed, then the connection state may already
938 		 *  be EXITED.  We don't want to set it back to EXITING in that case.
939 		 */
940 		tqpair->state = NVME_TCP_QPAIR_STATE_EXITING;
941 	}
942 
943 	return -1;
944 }
945 
946 static void
947 spdk_nvmf_tcp_qpair_write_pdu(struct nvme_tcp_qpair *tqpair,
948 			      struct nvme_tcp_pdu *pdu,
949 			      nvme_tcp_qpair_xfer_complete_cb cb_fn,
950 			      void *cb_arg)
951 {
952 	int enable_digest;
953 	int hlen;
954 	uint32_t crc32c;
955 
956 	hlen = pdu->hdr.common.hlen;
957 	enable_digest = 1;
958 	if (pdu->hdr.common.pdu_type == SPDK_NVME_TCP_PDU_TYPE_IC_RESP ||
959 	    pdu->hdr.common.pdu_type == SPDK_NVME_TCP_PDU_TYPE_C2H_TERM_REQ) {
960 		/* this PDU should be sent without digest */
961 		enable_digest = 0;
962 	}
963 
964 	/* Header Digest */
965 	if (enable_digest && tqpair->host_hdgst_enable) {
966 		crc32c = nvme_tcp_pdu_calc_header_digest(pdu);
967 		MAKE_DIGEST_WORD((uint8_t *)pdu->hdr.raw + hlen, crc32c);
968 	}
969 
970 	/* Data Digest */
971 	if (pdu->data_len > 0 && enable_digest && tqpair->host_ddgst_enable) {
972 		crc32c = nvme_tcp_pdu_calc_data_digest(pdu);
973 		MAKE_DIGEST_WORD(pdu->data_digest, crc32c);
974 	}
975 
976 	pdu->cb_fn = cb_fn;
977 	pdu->cb_arg = cb_arg;
978 	TAILQ_INSERT_TAIL(&tqpair->send_queue, pdu, tailq);
979 	spdk_nvmf_tcp_qpair_flush_pdus(tqpair);
980 }
981 
982 static int
983 spdk_nvmf_tcp_qpair_init_mem_resource(struct nvme_tcp_qpair *tqpair, uint16_t size)
984 {
985 	int i;
986 	struct nvme_tcp_req *tcp_req;
987 	struct spdk_nvmf_tcp_transport *ttransport;
988 	ttransport = SPDK_CONTAINEROF(tqpair->qpair.transport, struct spdk_nvmf_tcp_transport, transport);
989 
990 	if (!tqpair->qpair.sq_head_max) {
991 		tqpair->req = calloc(1, sizeof(*tqpair->req));
992 		if (!tqpair->req) {
993 			SPDK_ERRLOG("Unable to allocate req on tqpair=%p.\n", tqpair);
994 			return -1;
995 		}
996 
997 		if (ttransport->in_capsule_data_size) {
998 			tqpair->buf = spdk_dma_zmalloc(ttransport->in_capsule_data_size, 0x1000, NULL);
999 			if (!tqpair->buf) {
1000 				SPDK_ERRLOG("Unable to allocate buf on tqpair=%p.\n", tqpair);
1001 				return -1;
1002 			}
1003 		}
1004 
1005 		tcp_req = tqpair->req;
1006 		tcp_req->ttag = 0;
1007 		tcp_req->req.qpair = &tqpair->qpair;
1008 
1009 		/* Set up memory to receive commands */
1010 		if (tqpair->buf) {
1011 			tcp_req->buf = tqpair->buf;
1012 		}
1013 
1014 		/* Set the cmdn and rsp */
1015 		tcp_req->req.rsp = (union nvmf_c2h_msg *)&tcp_req->rsp;
1016 		tcp_req->req.cmd = (union nvmf_h2c_msg *)&tcp_req->cmd;
1017 
1018 		/* Initialize request state to FREE */
1019 		tcp_req->state = TCP_REQUEST_STATE_FREE;
1020 		TAILQ_INSERT_TAIL(&tqpair->state_queue[tcp_req->state], tcp_req, state_link);
1021 
1022 		tqpair->pdu = calloc(NVMF_TCP_QPAIR_MAX_C2H_PDU_NUM + 1, sizeof(*tqpair->pdu));
1023 		if (!tqpair->pdu) {
1024 			SPDK_ERRLOG("Unable to allocate pdu on tqpair=%p.\n", tqpair);
1025 			return -1;
1026 		}
1027 
1028 		for (i = 0; i < 1 + NVMF_TCP_QPAIR_MAX_C2H_PDU_NUM; i++) {
1029 			TAILQ_INSERT_TAIL(&tqpair->free_queue, &tqpair->pdu[i], tailq);
1030 		}
1031 
1032 	} else {
1033 		tqpair->reqs = calloc(size, sizeof(*tqpair->reqs));
1034 		if (!tqpair->reqs) {
1035 			SPDK_ERRLOG("Unable to allocate reqs on tqpair=%p\n", tqpair);
1036 			return -1;
1037 		}
1038 
1039 		if (ttransport->in_capsule_data_size) {
1040 			tqpair->bufs = spdk_dma_zmalloc(size * ttransport->in_capsule_data_size,
1041 							0x1000, NULL);
1042 			if (!tqpair->bufs) {
1043 				SPDK_ERRLOG("Unable to allocate bufs on tqpair=%p.\n", tqpair);
1044 				return -1;
1045 			}
1046 		}
1047 
1048 		for (i = 0; i < size; i++) {
1049 			struct nvme_tcp_req *tcp_req = &tqpair->reqs[i];
1050 
1051 			tcp_req->ttag = i + 1;
1052 			tcp_req->req.qpair = &tqpair->qpair;
1053 
1054 			/* Set up memory to receive commands */
1055 			if (tqpair->bufs) {
1056 				tcp_req->buf = (void *)((uintptr_t)tqpair->bufs + (i * ttransport->in_capsule_data_size));
1057 			}
1058 
1059 			/* Set the cmdn and rsp */
1060 			tcp_req->req.rsp = (union nvmf_c2h_msg *)&tcp_req->rsp;
1061 			tcp_req->req.cmd = (union nvmf_h2c_msg *)&tcp_req->cmd;
1062 
1063 			/* Initialize request state to FREE */
1064 			tcp_req->state = TCP_REQUEST_STATE_FREE;
1065 			TAILQ_INSERT_TAIL(&tqpair->state_queue[tcp_req->state], tcp_req, state_link);
1066 		}
1067 
1068 		tqpair->pdu_pool = calloc(size, sizeof(*tqpair->pdu_pool));
1069 		if (!tqpair->pdu_pool) {
1070 			SPDK_ERRLOG("Unable to allocate pdu pool on tqpair =%p.\n", tqpair);
1071 			return -1;
1072 		}
1073 
1074 		for (i = 0; i < size; i++) {
1075 			TAILQ_INSERT_TAIL(&tqpair->free_queue, &tqpair->pdu_pool[i], tailq);
1076 		}
1077 	}
1078 
1079 	return 0;
1080 }
1081 
1082 static int
1083 spdk_nvmf_tcp_qpair_init(struct spdk_nvmf_qpair *qpair)
1084 {
1085 	struct spdk_nvmf_tcp_transport *ttransport;
1086 	struct nvme_tcp_qpair *tqpair;
1087 	int i;
1088 
1089 	ttransport = SPDK_CONTAINEROF(qpair->transport, struct spdk_nvmf_tcp_transport, transport);
1090 	tqpair = SPDK_CONTAINEROF(qpair, struct nvme_tcp_qpair, qpair);
1091 
1092 	SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "New TCP Connection: %p\n", qpair);
1093 
1094 	TAILQ_INIT(&tqpair->send_queue);
1095 	TAILQ_INIT(&tqpair->free_queue);
1096 	TAILQ_INIT(&tqpair->queued_r2t_tcp_req);
1097 	TAILQ_INIT(&tqpair->queued_c2h_data_tcp_req);
1098 
1099 	/* Initialise request state queues of the qpair */
1100 	for (i = TCP_REQUEST_STATE_FREE; i < TCP_REQUEST_NUM_STATES; i++) {
1101 		TAILQ_INIT(&tqpair->state_queue[i]);
1102 	}
1103 
1104 	tqpair->host_hdgst_enable = true;
1105 	tqpair->host_ddgst_enable = true;
1106 
1107 	tqpair->mgmt_channel = spdk_get_io_channel(ttransport);
1108 	if (!tqpair->mgmt_channel) {
1109 		return -1;
1110 	}
1111 	tqpair->ch = spdk_io_channel_get_ctx(tqpair->mgmt_channel);
1112 	assert(tqpair->ch != NULL);
1113 
1114 	return 0;
1115 }
1116 
1117 static int
1118 spdk_nvmf_tcp_qpair_sock_init(struct nvme_tcp_qpair *tqpair)
1119 {
1120 
1121 	int rc;
1122 	int buf_size;
1123 
1124 	/* set recv buffer size */
1125 	buf_size = 2 * 1024 * 1024;
1126 	rc = spdk_sock_set_recvbuf(tqpair->sock, buf_size);
1127 	if (rc != 0) {
1128 		SPDK_ERRLOG("spdk_sock_set_recvbuf failed\n");
1129 		return rc;
1130 	}
1131 
1132 	/* set send buffer size */
1133 	rc = spdk_sock_set_sendbuf(tqpair->sock, buf_size);
1134 	if (rc != 0) {
1135 		SPDK_ERRLOG("spdk_sock_set_sendbuf failed\n");
1136 		return rc;
1137 	}
1138 
1139 	/* set low water mark */
1140 	rc = spdk_sock_set_recvlowat(tqpair->sock, sizeof(struct spdk_nvme_tcp_c2h_data_hdr));
1141 	if (rc != 0) {
1142 		SPDK_ERRLOG("spdk_sock_set_recvlowat() failed\n");
1143 		return rc;
1144 	}
1145 
1146 	return 0;
1147 }
1148 
1149 static void
1150 _spdk_nvmf_tcp_handle_connect(struct spdk_nvmf_transport *transport,
1151 			      struct spdk_nvmf_tcp_port *port,
1152 			      struct spdk_sock *sock, new_qpair_fn cb_fn)
1153 {
1154 	struct nvme_tcp_qpair *tqpair;
1155 	int rc;
1156 
1157 	SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "New connection accepted on %s port %s\n",
1158 		      port->trid.traddr, port->trid.trsvcid);
1159 
1160 	tqpair = calloc(1, sizeof(struct nvme_tcp_qpair));
1161 	if (tqpair == NULL) {
1162 		SPDK_ERRLOG("Could not allocate new connection.\n");
1163 		spdk_sock_close(&sock);
1164 		return;
1165 	}
1166 
1167 	tqpair->sock = sock;
1168 	tqpair->max_queue_depth = 1;
1169 	tqpair->free_pdu_num = tqpair->max_queue_depth + NVMF_TCP_QPAIR_MAX_C2H_PDU_NUM;
1170 	tqpair->state_cntr[TCP_REQUEST_STATE_FREE] = tqpair->max_queue_depth;
1171 	tqpair->port = port;
1172 	tqpair->qpair.transport = transport;
1173 
1174 	rc = spdk_sock_getaddr(tqpair->sock, tqpair->target_addr,
1175 			       sizeof(tqpair->target_addr), &tqpair->target_port,
1176 			       tqpair->initiator_addr, sizeof(tqpair->initiator_addr),
1177 			       &tqpair->initiator_port);
1178 	if (rc < 0) {
1179 		SPDK_ERRLOG("spdk_sock_getaddr() failed of tqpair=%p\n", tqpair);
1180 		spdk_nvmf_tcp_qpair_destroy(tqpair);
1181 		return;
1182 	}
1183 
1184 	cb_fn(&tqpair->qpair);
1185 }
1186 
1187 static void
1188 spdk_nvmf_tcp_port_accept(struct spdk_nvmf_transport *transport, struct spdk_nvmf_tcp_port *port,
1189 			  new_qpair_fn cb_fn)
1190 {
1191 	struct spdk_sock *sock;
1192 	int i;
1193 
1194 	for (i = 0; i < NVMF_TCP_MAX_ACCEPT_SOCK_ONE_TIME; i++) {
1195 		sock = spdk_sock_accept(port->listen_sock);
1196 		if (sock) {
1197 			_spdk_nvmf_tcp_handle_connect(transport, port, sock, cb_fn);
1198 		}
1199 	}
1200 }
1201 
1202 static void
1203 spdk_nvmf_tcp_accept(struct spdk_nvmf_transport *transport, new_qpair_fn cb_fn)
1204 {
1205 	struct spdk_nvmf_tcp_transport *ttransport;
1206 	struct spdk_nvmf_tcp_port *port;
1207 
1208 	ttransport = SPDK_CONTAINEROF(transport, struct spdk_nvmf_tcp_transport, transport);
1209 
1210 	TAILQ_FOREACH(port, &ttransport->ports, link) {
1211 		spdk_nvmf_tcp_port_accept(transport, port, cb_fn);
1212 	}
1213 }
1214 
1215 static void
1216 spdk_nvmf_tcp_discover(struct spdk_nvmf_transport *transport,
1217 		       struct spdk_nvme_transport_id *trid,
1218 		       struct spdk_nvmf_discovery_log_page_entry *entry)
1219 {
1220 	entry->trtype = SPDK_NVMF_TRTYPE_TCP;
1221 	entry->adrfam = trid->adrfam;
1222 	entry->treq.secure_channel = SPDK_NVMF_TREQ_SECURE_CHANNEL_NOT_SPECIFIED;
1223 
1224 	spdk_strcpy_pad(entry->trsvcid, trid->trsvcid, sizeof(entry->trsvcid), ' ');
1225 	spdk_strcpy_pad(entry->traddr, trid->traddr, sizeof(entry->traddr), ' ');
1226 
1227 	entry->tsas.tcp.sectype = SPDK_NVME_TCP_SECURITY_NONE;
1228 }
1229 
1230 static struct spdk_nvmf_transport_poll_group *
1231 spdk_nvmf_tcp_poll_group_create(struct spdk_nvmf_transport *transport)
1232 {
1233 	struct spdk_nvmf_tcp_poll_group *tgroup;
1234 
1235 	tgroup = calloc(1, sizeof(*tgroup));
1236 	if (!tgroup) {
1237 		return NULL;
1238 	}
1239 
1240 	tgroup->sock_group = spdk_sock_group_create();
1241 	if (!tgroup->sock_group) {
1242 		goto cleanup;
1243 	}
1244 
1245 	TAILQ_INIT(&tgroup->qpairs);
1246 	return &tgroup->group;
1247 
1248 cleanup:
1249 	free(tgroup);
1250 	return NULL;
1251 }
1252 
1253 static void
1254 spdk_nvmf_tcp_poll_group_destroy(struct spdk_nvmf_transport_poll_group *group)
1255 {
1256 	struct spdk_nvmf_tcp_poll_group *tgroup;
1257 
1258 	tgroup = SPDK_CONTAINEROF(group, struct spdk_nvmf_tcp_poll_group, group);
1259 	spdk_sock_group_close(&tgroup->sock_group);
1260 	free(tgroup);
1261 }
1262 
1263 static void
1264 spdk_nvmf_tcp_qpair_set_recv_state(struct nvme_tcp_qpair *tqpair,
1265 				   enum nvme_tcp_pdu_recv_state state)
1266 {
1267 	if (tqpair->recv_state == state) {
1268 		SPDK_ERRLOG("The recv state of tqpair=%p is same with the state(%d) to be set\n",
1269 			    tqpair, state);
1270 		return;
1271 	}
1272 
1273 	SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "tqpair(%p) recv state=%d\n", tqpair, state);
1274 	tqpair->recv_state = state;
1275 	switch (state) {
1276 	case NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_CH:
1277 	case NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_PSH:
1278 	case NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_PAYLOAD:
1279 		break;
1280 	case NVME_TCP_PDU_RECV_STATE_ERROR:
1281 	case NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_READY:
1282 		memset(&tqpair->pdu_in_progress, 0, sizeof(tqpair->pdu_in_progress));
1283 		break;
1284 	default:
1285 		SPDK_ERRLOG("The state(%d) is invalid\n", state);
1286 		abort();
1287 		break;
1288 	}
1289 }
1290 
1291 static void
1292 spdk_nvmf_tcp_send_c2h_term_req_complete(void *cb_arg)
1293 {
1294 }
1295 
1296 static void
1297 spdk_nvmf_tcp_send_c2h_term_req(struct nvme_tcp_qpair *tqpair, struct nvme_tcp_pdu *pdu,
1298 				enum spdk_nvme_tcp_term_req_fes fes, uint32_t error_offset)
1299 {
1300 	struct nvme_tcp_pdu *rsp_pdu;
1301 	struct spdk_nvme_tcp_term_req_hdr *c2h_term_req;
1302 	uint32_t c2h_term_req_hdr_len = sizeof(*c2h_term_req);
1303 
1304 	rsp_pdu = spdk_nvmf_tcp_pdu_get(tqpair);
1305 	if (!rsp_pdu) {
1306 		tqpair->state = NVME_TCP_QPAIR_STATE_EXITING;
1307 		spdk_nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_ERROR);
1308 		return;
1309 	}
1310 
1311 	c2h_term_req = &rsp_pdu->hdr.term_req;
1312 	c2h_term_req->common.pdu_type = SPDK_NVME_TCP_PDU_TYPE_C2H_TERM_REQ;
1313 	c2h_term_req->common.hlen = c2h_term_req_hdr_len;
1314 	/* It should contain the header the received pdu */
1315 	c2h_term_req->common.plen = c2h_term_req->common.hlen + pdu->hdr.common.hlen;
1316 
1317 	if ((fes == SPDK_NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD) ||
1318 	    (fes == SPDK_NVME_TCP_TERM_REQ_FES_INVALID_DATA_UNSUPPORTED_PARAMETER)) {
1319 		DSET32(&c2h_term_req->fei, error_offset);
1320 	}
1321 
1322 	/* copy the error code into the buffer */
1323 	rsp_pdu->data = (uint8_t *)rsp_pdu->hdr.raw + c2h_term_req_hdr_len;
1324 	if (pdu->hdr.common.plen <= SPDK_NVME_TCP_TERM_REQ_ERROR_DATA_MAX_SIZE) {
1325 		memcpy((uint8_t *)rsp_pdu->data, pdu->hdr.raw, pdu->hdr.common.plen);
1326 	} else {
1327 		memcpy((uint8_t *)rsp_pdu->data, pdu->hdr.raw, SPDK_NVME_TCP_TERM_REQ_ERROR_DATA_MAX_SIZE);
1328 	}
1329 
1330 	spdk_nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_ERROR);
1331 	spdk_nvmf_tcp_qpair_write_pdu(tqpair, rsp_pdu, spdk_nvmf_tcp_send_c2h_term_req_complete, tqpair);
1332 }
1333 
1334 static void
1335 spdk_nvmf_tcp_capsule_cmd_hdr_handle(struct spdk_nvmf_tcp_transport *ttransport,
1336 				     struct nvme_tcp_qpair *tqpair,
1337 				     struct nvme_tcp_pdu *pdu)
1338 {
1339 	struct nvme_tcp_req *tcp_req;
1340 
1341 	tcp_req = spdk_nvmf_tcp_req_get(tqpair);
1342 	if (!tcp_req) {
1343 		SPDK_ERRLOG("Cannot allocate tcp_req\n");
1344 		tqpair->state = NVME_TCP_QPAIR_STATE_EXITING;
1345 		spdk_nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_ERROR);
1346 		return;
1347 	}
1348 
1349 	pdu->tcp_req = tcp_req;
1350 	spdk_nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_NEW);
1351 	spdk_nvmf_tcp_req_process(ttransport, tcp_req);
1352 	return;
1353 }
1354 
1355 static void
1356 spdk_nvmf_tcp_capsule_cmd_payload_handle(struct spdk_nvmf_tcp_transport *ttransport,
1357 		struct nvme_tcp_qpair *tqpair,
1358 		struct nvme_tcp_pdu *pdu)
1359 {
1360 	struct nvme_tcp_req *tcp_req;
1361 	struct spdk_nvme_tcp_cmd *capsule_cmd;
1362 	uint32_t error_offset = 0;
1363 	enum spdk_nvme_tcp_term_req_fes fes;
1364 
1365 	capsule_cmd = &pdu->hdr.capsule_cmd;
1366 	tcp_req = pdu->tcp_req;
1367 	assert(tcp_req != NULL);
1368 	if (capsule_cmd->common.pdo > SPDK_NVME_TCP_PDU_PDO_MAX_OFFSET) {
1369 		SPDK_ERRLOG("Expected ICReq capsule_cmd pdu offset <= %d, got %c\n",
1370 			    SPDK_NVME_TCP_PDU_PDO_MAX_OFFSET, capsule_cmd->common.pdo);
1371 		fes = SPDK_NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD;
1372 		error_offset = offsetof(struct spdk_nvme_tcp_common_pdu_hdr, pdo);
1373 		goto err;
1374 	}
1375 
1376 	spdk_nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_READY);
1377 	spdk_nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_READY_TO_EXECUTE);
1378 	spdk_nvmf_tcp_req_process(ttransport, tcp_req);
1379 
1380 	return;
1381 err:
1382 	spdk_nvmf_tcp_send_c2h_term_req(tqpair, pdu, fes, error_offset);
1383 }
1384 
1385 static void
1386 spdk_nvmf_tcp_h2c_data_hdr_handle(struct spdk_nvmf_tcp_transport *ttransport,
1387 				  struct nvme_tcp_qpair *tqpair,
1388 				  struct nvme_tcp_pdu *pdu)
1389 {
1390 	struct nvme_tcp_req *tcp_req;
1391 	uint32_t error_offset = 0;
1392 	enum spdk_nvme_tcp_term_req_fes fes = 0;
1393 	struct spdk_nvme_tcp_h2c_data_hdr *h2c_data;
1394 	uint32_t iov_index;
1395 	bool ttag_offset_error = false;
1396 
1397 	h2c_data = &pdu->hdr.h2c_data;
1398 
1399 	SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "tqpair=%p, r2t_info: datao=%u, datal=%u, cccid=%u, ttag=%u\n",
1400 		      tqpair, h2c_data->datao, h2c_data->datal, h2c_data->cccid, h2c_data->ttag);
1401 
1402 	/* According to the information in the pdu to find the req */
1403 	TAILQ_FOREACH(tcp_req, &tqpair->state_queue[TCP_REQUEST_STATE_TRANSFERRING_HOST_TO_CONTROLLER],
1404 		      state_link) {
1405 		if ((tcp_req->req.cmd->nvme_cmd.cid == h2c_data->cccid) && (tcp_req->ttag == h2c_data->ttag)) {
1406 			break;
1407 		}
1408 
1409 		if (!ttag_offset_error && (tcp_req->req.cmd->nvme_cmd.cid == h2c_data->cccid)) {
1410 			ttag_offset_error = true;
1411 		}
1412 	}
1413 
1414 	if (!tcp_req) {
1415 		SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "tcp_req is not found for tqpair=%p\n", tqpair);
1416 		fes = SPDK_NVME_TCP_TERM_REQ_FES_INVALID_DATA_UNSUPPORTED_PARAMETER;
1417 		if (!ttag_offset_error) {
1418 			error_offset = offsetof(struct spdk_nvme_tcp_h2c_data_hdr, cccid);
1419 		} else {
1420 			error_offset = offsetof(struct spdk_nvme_tcp_h2c_data_hdr, ttag);
1421 		}
1422 		goto err;
1423 	}
1424 
1425 	if (tcp_req->next_expected_r2t_offset != h2c_data->datao) {
1426 		SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP,
1427 			      "tcp_req(%p), tqpair=%p,  expected_r2t_offset=%u, but data offset =%u\n",
1428 			      tcp_req, tqpair, tcp_req->next_expected_r2t_offset, h2c_data->datao);
1429 		fes = SPDK_NVME_TCP_TERM_REQ_FES_DATA_TRANSFER_OUT_OF_RANGE;
1430 		goto err;
1431 	}
1432 
1433 	if (h2c_data->datal > tqpair->maxh2cdata) {
1434 		SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "tcp_req(%p), tqpair=%p,  datao=%u execeeds maxh2cdata size=%u\n",
1435 			      tcp_req, tqpair, h2c_data->datao, tqpair->maxh2cdata);
1436 		fes = SPDK_NVME_TCP_TERM_REQ_FES_DATA_TRANSFER_OUT_OF_RANGE;
1437 		goto err;
1438 	}
1439 
1440 	if ((h2c_data->datao + h2c_data->datal) > tcp_req->req.length) {
1441 		SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP,
1442 			      "tcp_req(%p), tqpair=%p,  (datao=%u + datal=%u) execeeds requested length=%u\n",
1443 			      tcp_req, tqpair, h2c_data->datao, h2c_data->datal, tcp_req->req.length);
1444 		fes = SPDK_NVME_TCP_TERM_REQ_FES_R2T_LIMIT_EXCEEDED;
1445 		goto err;
1446 	}
1447 
1448 	pdu->tcp_req = tcp_req;
1449 	pdu->data_len = h2c_data->datal;
1450 	iov_index = pdu->hdr.h2c_data.datao / ttransport->io_unit_size;
1451 	pdu->data = tcp_req->req.iov[iov_index].iov_base + (pdu->hdr.h2c_data.datao %
1452 			ttransport->io_unit_size);
1453 	spdk_nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_PAYLOAD);
1454 	return;
1455 
1456 err:
1457 	spdk_nvmf_tcp_send_c2h_term_req(tqpair, pdu, fes, error_offset);
1458 }
1459 
1460 static void
1461 spdk_nvmf_tcp_pdu_cmd_complete(void *cb_arg)
1462 {
1463 	struct nvme_tcp_req *tcp_req = cb_arg;
1464 	nvmf_tcp_request_free(tcp_req);
1465 }
1466 
1467 static void
1468 spdk_nvmf_tcp_send_capsule_resp_pdu(struct nvme_tcp_req *tcp_req,
1469 				    struct nvme_tcp_qpair *tqpair)
1470 {
1471 	struct nvme_tcp_pdu *rsp_pdu;
1472 	struct spdk_nvme_tcp_rsp *capsule_resp;
1473 
1474 	SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "enter, tqpair=%p\n", tqpair);
1475 	rsp_pdu = spdk_nvmf_tcp_pdu_get(tqpair);
1476 	if (!rsp_pdu) {
1477 		spdk_nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_ERROR);
1478 		tqpair->state = NVME_TCP_QPAIR_STATE_EXITING;
1479 		return;
1480 	}
1481 
1482 	capsule_resp = &rsp_pdu->hdr.capsule_resp;
1483 	capsule_resp->common.pdu_type = SPDK_NVME_TCP_PDU_TYPE_CAPSULE_RESP;
1484 	capsule_resp->common.plen = capsule_resp->common.hlen = sizeof(*capsule_resp);
1485 	capsule_resp->rccqe = tcp_req->req.rsp->nvme_cpl;
1486 	if (tqpair->host_hdgst_enable) {
1487 		capsule_resp->common.flags |= SPDK_NVME_TCP_CH_FLAGS_HDGSTF;
1488 		capsule_resp->common.plen += SPDK_NVME_TCP_DIGEST_LEN;
1489 	}
1490 
1491 	spdk_nvmf_tcp_qpair_write_pdu(tqpair, rsp_pdu, spdk_nvmf_tcp_pdu_cmd_complete, tcp_req);
1492 }
1493 
1494 static void
1495 spdk_nvmf_tcp_pdu_c2h_data_complete(void *cb_arg)
1496 {
1497 	struct nvme_tcp_req *tcp_req = cb_arg;
1498 	struct nvme_tcp_qpair *tqpair = SPDK_CONTAINEROF(tcp_req->req.qpair,
1499 					struct nvme_tcp_qpair, qpair);
1500 
1501 	assert(tqpair != NULL);
1502 	assert(tcp_req->c2h_data_pdu_num > 0);
1503 	tcp_req->c2h_data_pdu_num--;
1504 	if (!tcp_req->c2h_data_pdu_num) {
1505 #if LINUX_KERNEL_SUPPORT_NOT_SENDING_RESP_FOR_C2H
1506 		nvmf_tcp_request_free(tcp_req);
1507 #else
1508 		spdk_nvmf_tcp_send_capsule_resp_pdu(tcp_req, tqpair);
1509 #endif
1510 	}
1511 
1512 	tqpair->c2h_data_pdu_cnt--;
1513 	spdk_nvmf_tcp_handle_pending_c2h_data_queue(tqpair);
1514 }
1515 
1516 static void
1517 spdk_nvmf_tcp_send_r2t_pdu(struct spdk_nvmf_tcp_transport *ttransport,
1518 			   struct nvme_tcp_qpair *tqpair,
1519 			   struct nvme_tcp_req *tcp_req)
1520 {
1521 	struct nvme_tcp_pdu *rsp_pdu;
1522 	struct spdk_nvme_tcp_r2t_hdr *r2t;
1523 
1524 	rsp_pdu = spdk_nvmf_tcp_pdu_get(tqpair);
1525 	if (!rsp_pdu) {
1526 		tqpair->state = NVME_TCP_QPAIR_STATE_EXITING;
1527 		spdk_nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_ERROR);
1528 		return;
1529 	}
1530 
1531 	r2t = &rsp_pdu->hdr.r2t;
1532 	r2t->common.pdu_type = SPDK_NVME_TCP_PDU_TYPE_R2T;
1533 	r2t->common.plen = r2t->common.hlen = sizeof(*r2t);
1534 
1535 	if (tqpair->host_hdgst_enable) {
1536 		r2t->common.flags |= SPDK_NVME_TCP_CH_FLAGS_HDGSTF;
1537 		r2t->common.plen += SPDK_NVME_TCP_DIGEST_LEN;
1538 	}
1539 
1540 	r2t->cccid = tcp_req->req.cmd->nvme_cmd.cid;
1541 	r2t->ttag = tcp_req->ttag;
1542 	r2t->r2to = tcp_req->next_expected_r2t_offset;
1543 	r2t->r2tl = spdk_min(tcp_req->req.length - tcp_req->next_expected_r2t_offset, tqpair->maxh2cdata);
1544 	tcp_req->r2tl_remain = r2t->r2tl;
1545 
1546 	SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP,
1547 		      "tcp_req(%p) on tqpair(%p), r2t_info: cccid=%u, ttag=%u, r2to=%u, r2tl=%u\n",
1548 		      tcp_req, tqpair, r2t->cccid, r2t->ttag, r2t->r2to, r2t->r2tl);
1549 	spdk_nvmf_tcp_qpair_write_pdu(tqpair, rsp_pdu, spdk_nvmf_tcp_pdu_cmd_complete, NULL);
1550 }
1551 
1552 static void
1553 spdk_nvmf_tcp_h2c_data_payload_handle(struct spdk_nvmf_tcp_transport *ttransport,
1554 				      struct nvme_tcp_qpair *tqpair,
1555 				      struct nvme_tcp_pdu *pdu)
1556 {
1557 	struct nvme_tcp_req *tcp_req;
1558 
1559 	tcp_req = pdu->tcp_req;
1560 	assert(tcp_req != NULL);
1561 
1562 	SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "enter\n");
1563 
1564 	tcp_req->next_expected_r2t_offset += pdu->data_len;
1565 	tcp_req->r2tl_remain -= pdu->data_len;
1566 	spdk_nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_READY);
1567 
1568 	if (!tcp_req->r2tl_remain) {
1569 		if (tcp_req->next_expected_r2t_offset == tcp_req->req.length) {
1570 			assert(tqpair->pending_r2t > 0);
1571 			tqpair->pending_r2t--;
1572 			assert(tqpair->pending_r2t < tqpair->maxr2t);
1573 			spdk_nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_READY_TO_EXECUTE);
1574 			spdk_nvmf_tcp_req_process(ttransport, tcp_req);
1575 
1576 			/* fetch next */
1577 			tcp_req = TAILQ_FIRST(&tqpair->queued_r2t_tcp_req);
1578 			if (tcp_req) {
1579 				spdk_nvmf_tcp_req_process(ttransport, tcp_req);
1580 			}
1581 		} else {
1582 			SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "Send r2t pdu for tcp_req=%p on tqpair=%p\n", tcp_req, tqpair);
1583 			spdk_nvmf_tcp_send_r2t_pdu(ttransport, tqpair, tcp_req);
1584 		}
1585 	}
1586 }
1587 
1588 static void
1589 spdk_nvmf_tcp_h2c_term_req_dump(struct spdk_nvme_tcp_term_req_hdr *h2c_term_req)
1590 {
1591 	SPDK_ERRLOG("Error info of pdu(%p): %s\n", h2c_term_req,
1592 		    spdk_nvme_tcp_term_req_fes_str[h2c_term_req->fes]);
1593 	if ((h2c_term_req->fes == SPDK_NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD) ||
1594 	    (h2c_term_req->fes == SPDK_NVME_TCP_TERM_REQ_FES_INVALID_DATA_UNSUPPORTED_PARAMETER)) {
1595 		SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "The offset from the start of the PDU header is %u\n",
1596 			      DGET32(h2c_term_req->fei));
1597 	}
1598 }
1599 
1600 static void
1601 spdk_nvmf_tcp_h2c_term_req_hdr_handle(struct nvme_tcp_qpair *tqpair,
1602 				      struct nvme_tcp_pdu *pdu)
1603 {
1604 	struct spdk_nvme_tcp_term_req_hdr *h2c_term_req = &pdu->hdr.term_req;
1605 	uint32_t error_offset = 0;
1606 	enum spdk_nvme_tcp_term_req_fes fes;
1607 
1608 
1609 	if (h2c_term_req->fes > SPDK_NVME_TCP_TERM_REQ_FES_INVALID_DATA_UNSUPPORTED_PARAMETER) {
1610 		SPDK_ERRLOG("Fatal Error Stauts(FES) is unknown for h2c_term_req pdu=%p\n", pdu);
1611 		fes = SPDK_NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD;
1612 		error_offset = offsetof(struct spdk_nvme_tcp_term_req_hdr, fes);
1613 		goto end;
1614 	}
1615 
1616 	/* set the data buffer */
1617 	pdu->data = (uint8_t *)pdu->hdr.raw + h2c_term_req->common.hlen;
1618 	pdu->data_len = h2c_term_req->common.plen - h2c_term_req->common.hlen;
1619 	spdk_nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_PAYLOAD);
1620 	return;
1621 end:
1622 	spdk_nvmf_tcp_send_c2h_term_req(tqpair, pdu, fes, error_offset);
1623 	return;
1624 }
1625 
1626 static void
1627 spdk_nvmf_tcp_h2c_term_req_payload_handle(struct nvme_tcp_qpair *tqpair,
1628 		struct nvme_tcp_pdu *pdu)
1629 {
1630 	struct spdk_nvme_tcp_term_req_hdr *h2c_term_req = &pdu->hdr.term_req;
1631 
1632 	spdk_nvmf_tcp_h2c_term_req_dump(h2c_term_req);
1633 	spdk_nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_ERROR);
1634 	return;
1635 }
1636 
1637 static void
1638 spdk_nvmf_tcp_pdu_payload_handle(struct nvme_tcp_qpair *tqpair)
1639 {
1640 	int rc = 0;
1641 	struct nvme_tcp_pdu *pdu;
1642 	uint32_t crc32c, error_offset = 0;
1643 	enum spdk_nvme_tcp_term_req_fes fes;
1644 	struct spdk_nvmf_tcp_transport *ttransport;
1645 
1646 	assert(tqpair->recv_state == NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_PAYLOAD);
1647 	pdu = &tqpair->pdu_in_progress;
1648 
1649 	SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "enter\n");
1650 	/* check data digest if need */
1651 	if (pdu->ddigest_valid_bytes) {
1652 		crc32c = nvme_tcp_pdu_calc_data_digest(pdu);
1653 		rc = MATCH_DIGEST_WORD(pdu->data_digest, crc32c);
1654 		if (rc == 0) {
1655 			SPDK_ERRLOG("Data digest error on tqpair=(%p) with pdu=%p\n", tqpair, pdu);
1656 			fes = SPDK_NVME_TCP_TERM_REQ_FES_HDGST_ERROR;
1657 			spdk_nvmf_tcp_send_c2h_term_req(tqpair, pdu, fes, error_offset);
1658 			return;
1659 
1660 		}
1661 	}
1662 
1663 	ttransport = SPDK_CONTAINEROF(tqpair->qpair.transport, struct spdk_nvmf_tcp_transport, transport);
1664 	switch (pdu->hdr.common.pdu_type) {
1665 	case SPDK_NVME_TCP_PDU_TYPE_CAPSULE_CMD:
1666 		spdk_nvmf_tcp_capsule_cmd_payload_handle(ttransport, tqpair, pdu);
1667 		break;
1668 	case SPDK_NVME_TCP_PDU_TYPE_H2C_DATA:
1669 		spdk_nvmf_tcp_h2c_data_payload_handle(ttransport, tqpair, pdu);
1670 		break;
1671 
1672 	case SPDK_NVME_TCP_PDU_TYPE_H2C_TERM_REQ:
1673 		spdk_nvmf_tcp_h2c_term_req_payload_handle(tqpair, pdu);
1674 		break;
1675 
1676 	default:
1677 		/* The code should not go to here */
1678 		SPDK_ERRLOG("The code should not go to here\n");
1679 		break;
1680 	}
1681 }
1682 
1683 static void
1684 spdk_nvmf_tcp_send_icresp_complete(void *cb_arg)
1685 {
1686 	struct nvme_tcp_qpair *tqpair = cb_arg;
1687 
1688 	tqpair->state = NVME_TCP_QPAIR_STATE_RUNNING;
1689 }
1690 
1691 static void
1692 spdk_nvmf_tcp_icreq_handle(struct spdk_nvmf_tcp_transport *ttransport,
1693 			   struct nvme_tcp_qpair *tqpair,
1694 			   struct nvme_tcp_pdu *pdu)
1695 {
1696 	struct spdk_nvme_tcp_ic_req *ic_req = &pdu->hdr.ic_req;
1697 	struct nvme_tcp_pdu *rsp_pdu;
1698 	struct spdk_nvme_tcp_ic_resp *ic_resp;
1699 	uint32_t error_offset = 0;
1700 	enum spdk_nvme_tcp_term_req_fes fes;
1701 
1702 	/* Only PFV 0 is defined currently */
1703 	if (ic_req->pfv != 0) {
1704 		SPDK_ERRLOG("Expected ICReq PFV %u, got %u\n", 0u, ic_req->pfv);
1705 		fes = SPDK_NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD;
1706 		error_offset = offsetof(struct spdk_nvme_tcp_ic_req, pfv);
1707 		goto end;
1708 	}
1709 
1710 	/* MAXR2T is 0's based */
1711 	tqpair->maxr2t = ic_req->maxr2t + 1ull;
1712 	SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "maxr2t =%u\n", tqpair->maxr2t);
1713 
1714 	tqpair->host_hdgst_enable = ic_req->dgst.bits.hdgst_enable ? true : false;
1715 	tqpair->host_ddgst_enable = ic_req->dgst.bits.ddgst_enable ? true : false;
1716 
1717 	tqpair->cpda = spdk_min(ic_req->hpda, SPDK_NVME_TCP_CPDA_MAX);
1718 	SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "cpda of tqpair=(%p) is : %u\n", tqpair, tqpair->cpda);
1719 
1720 	rsp_pdu = spdk_nvmf_tcp_pdu_get(tqpair);
1721 	if (!rsp_pdu) {
1722 		tqpair->state = NVME_TCP_QPAIR_STATE_EXITING;
1723 		spdk_nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_ERROR);
1724 		return;
1725 	}
1726 
1727 	ic_resp = &rsp_pdu->hdr.ic_resp;
1728 	ic_resp->common.pdu_type = SPDK_NVME_TCP_PDU_TYPE_IC_RESP;
1729 	ic_resp->common.hlen = ic_resp->common.plen =  sizeof(*ic_resp);
1730 	ic_resp->pfv = 0;
1731 	ic_resp->cpda = tqpair->cpda;
1732 	tqpair->maxh2cdata = spdk_min(NVMF_TCP_PDU_MAX_H2C_DATA_SIZE, ttransport->io_unit_size);
1733 	ic_resp->maxh2cdata = tqpair->maxh2cdata;
1734 	ic_resp->dgst.bits.hdgst_enable = tqpair->host_hdgst_enable ? 1 : 0;
1735 	ic_resp->dgst.bits.ddgst_enable = tqpair->host_ddgst_enable ? 1 : 0;
1736 
1737 	SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "host_hdgst_enable: %u\n", tqpair->host_hdgst_enable);
1738 	SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "host_ddgst_enable: %u\n", tqpair->host_ddgst_enable);
1739 
1740 	spdk_nvmf_tcp_qpair_write_pdu(tqpair, rsp_pdu, spdk_nvmf_tcp_send_icresp_complete, tqpair);
1741 	spdk_nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_READY);
1742 	return;
1743 end:
1744 	spdk_nvmf_tcp_send_c2h_term_req(tqpair, pdu, fes, error_offset);
1745 	return;
1746 }
1747 
1748 static void
1749 spdk_nvmf_tcp_pdu_psh_handle(struct nvme_tcp_qpair *tqpair)
1750 {
1751 	struct nvme_tcp_pdu *pdu;
1752 	int rc;
1753 	uint32_t crc32c, error_offset = 0;
1754 	enum spdk_nvme_tcp_term_req_fes fes;
1755 	struct spdk_nvmf_tcp_transport *ttransport;
1756 
1757 	assert(tqpair->recv_state == NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_PSH);
1758 	pdu = &tqpair->pdu_in_progress;
1759 
1760 	SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "pdu type of tqpair(%p) is %d\n", tqpair,
1761 		      pdu->hdr.common.pdu_type);
1762 	/* check header digest if needed */
1763 	if (pdu->has_hdgst) {
1764 		SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "Compare the header of pdu=%p on tqpair=%p\n", pdu, tqpair);
1765 		crc32c = nvme_tcp_pdu_calc_header_digest(pdu);
1766 		rc = MATCH_DIGEST_WORD((uint8_t *)pdu->hdr.raw + pdu->hdr.common.hlen, crc32c);
1767 		if (rc == 0) {
1768 			SPDK_ERRLOG("Header digest error on tqpair=(%p) with pdu=%p\n", tqpair, pdu);
1769 			fes = SPDK_NVME_TCP_TERM_REQ_FES_HDGST_ERROR;
1770 			spdk_nvmf_tcp_send_c2h_term_req(tqpair, pdu, fes, error_offset);
1771 			return;
1772 
1773 		}
1774 	}
1775 
1776 	ttransport = SPDK_CONTAINEROF(tqpair->qpair.transport, struct spdk_nvmf_tcp_transport, transport);
1777 	switch (pdu->hdr.common.pdu_type) {
1778 	case SPDK_NVME_TCP_PDU_TYPE_IC_REQ:
1779 		spdk_nvmf_tcp_icreq_handle(ttransport, tqpair, pdu);
1780 		break;
1781 	case SPDK_NVME_TCP_PDU_TYPE_CAPSULE_CMD:
1782 		spdk_nvmf_tcp_capsule_cmd_hdr_handle(ttransport, tqpair, pdu);
1783 		break;
1784 	case SPDK_NVME_TCP_PDU_TYPE_H2C_DATA:
1785 		spdk_nvmf_tcp_h2c_data_hdr_handle(ttransport, tqpair, pdu);
1786 		break;
1787 
1788 	case SPDK_NVME_TCP_PDU_TYPE_H2C_TERM_REQ:
1789 		spdk_nvmf_tcp_h2c_term_req_hdr_handle(tqpair, pdu);
1790 		break;
1791 
1792 	default:
1793 		SPDK_ERRLOG("Unexpected PDU type 0x%02x\n", tqpair->pdu_in_progress.hdr.common.pdu_type);
1794 		fes = SPDK_NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD;
1795 		error_offset = 1;
1796 		spdk_nvmf_tcp_send_c2h_term_req(tqpair, pdu, fes, error_offset);
1797 		break;
1798 	}
1799 }
1800 
1801 static void
1802 spdk_nvmf_tcp_pdu_ch_handle(struct nvme_tcp_qpair *tqpair)
1803 {
1804 	struct nvme_tcp_pdu *pdu;
1805 	uint32_t error_offset = 0;
1806 	enum spdk_nvme_tcp_term_req_fes fes;
1807 	uint8_t expected_hlen, pdo;
1808 	bool plen_error = false, pdo_error = false;
1809 
1810 	assert(tqpair->recv_state == NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_CH);
1811 	pdu = &tqpair->pdu_in_progress;
1812 
1813 	if (pdu->hdr.common.pdu_type == SPDK_NVME_TCP_PDU_TYPE_IC_REQ) {
1814 		if (tqpair->state != NVME_TCP_QPAIR_STATE_INVALID) {
1815 			SPDK_ERRLOG("Already received ICreq PDU, and reject this pdu=%p\n", pdu);
1816 			fes = SPDK_NVME_TCP_TERM_REQ_FES_PDU_SEQUENCE_ERROR;
1817 			goto err;
1818 		}
1819 		expected_hlen = sizeof(struct spdk_nvme_tcp_ic_req);
1820 		if (pdu->hdr.common.plen != expected_hlen) {
1821 			plen_error = true;
1822 		}
1823 	} else {
1824 		if (tqpair->state != NVME_TCP_QPAIR_STATE_RUNNING) {
1825 			SPDK_ERRLOG("The TCP/IP connection is not negotitated\n");
1826 			fes = SPDK_NVME_TCP_TERM_REQ_FES_PDU_SEQUENCE_ERROR;
1827 			goto err;
1828 		}
1829 
1830 		switch (pdu->hdr.common.pdu_type) {
1831 		case SPDK_NVME_TCP_PDU_TYPE_CAPSULE_CMD:
1832 			expected_hlen = sizeof(struct spdk_nvme_tcp_cmd);
1833 			pdo = pdu->hdr.common.pdo;
1834 			if ((tqpair->cpda != 0) && (pdo != ((tqpair->cpda + 1) << 2))) {
1835 				pdo_error = true;
1836 				break;
1837 			}
1838 
1839 			if (pdu->hdr.common.plen < expected_hlen) {
1840 				plen_error = true;
1841 			}
1842 			break;
1843 		case SPDK_NVME_TCP_PDU_TYPE_H2C_DATA:
1844 			expected_hlen = sizeof(struct spdk_nvme_tcp_h2c_data_hdr);
1845 			pdo = pdu->hdr.common.pdo;
1846 			if ((tqpair->cpda != 0) && (pdo != ((tqpair->cpda + 1) << 2))) {
1847 				pdo_error = true;
1848 				break;
1849 			}
1850 			if (pdu->hdr.common.plen < expected_hlen) {
1851 				plen_error = true;
1852 			}
1853 			break;
1854 
1855 		case SPDK_NVME_TCP_PDU_TYPE_H2C_TERM_REQ:
1856 			expected_hlen = sizeof(struct spdk_nvme_tcp_term_req_hdr);
1857 			if ((pdu->hdr.common.plen <= expected_hlen) ||
1858 			    (pdu->hdr.common.plen > SPDK_NVME_TCP_TERM_REQ_PDU_MAX_SIZE)) {
1859 				plen_error = true;
1860 			}
1861 			break;
1862 
1863 		default:
1864 			SPDK_ERRLOG("Unexpected PDU type 0x%02x\n", pdu->hdr.common.pdu_type);
1865 			fes = SPDK_NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD;
1866 			error_offset = offsetof(struct spdk_nvme_tcp_common_pdu_hdr, pdu_type);
1867 			goto err;
1868 		}
1869 	}
1870 
1871 	if (pdu->hdr.common.hlen != expected_hlen) {
1872 		SPDK_ERRLOG("PDU type=0x%02x, Expected ICReq header length %u, got %u on tqpair=%p\n",
1873 			    pdu->hdr.common.pdu_type,
1874 			    expected_hlen, pdu->hdr.common.hlen, tqpair);
1875 		fes = SPDK_NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD;
1876 		error_offset = offsetof(struct spdk_nvme_tcp_common_pdu_hdr, hlen);
1877 		goto err;
1878 	} else if (pdo_error) {
1879 		fes = SPDK_NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD;
1880 		error_offset = offsetof(struct spdk_nvme_tcp_common_pdu_hdr, pdo);
1881 	} else if (plen_error) {
1882 		fes = SPDK_NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD;
1883 		error_offset = offsetof(struct spdk_nvme_tcp_common_pdu_hdr, plen);
1884 		goto err;
1885 	} else {
1886 		spdk_nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_PSH);
1887 		return;
1888 	}
1889 err:
1890 	spdk_nvmf_tcp_send_c2h_term_req(tqpair, pdu, fes, error_offset);
1891 }
1892 
1893 static int
1894 spdk_nvmf_tcp_sock_process(struct nvme_tcp_qpair *tqpair)
1895 {
1896 	int rc = 0;
1897 	struct nvme_tcp_pdu *pdu;
1898 	enum nvme_tcp_pdu_recv_state prev_state;
1899 	uint32_t data_len;
1900 	uint8_t psh_len, pdo, hlen;
1901 	int8_t  padding_len;
1902 
1903 	/* The loop here is to allow for several back-to-back state changes. */
1904 	do {
1905 		prev_state = tqpair->recv_state;
1906 
1907 		SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "tqpair(%p) recv pdu entering state %d\n", tqpair, prev_state);
1908 
1909 		switch (tqpair->recv_state) {
1910 		/* If in a new state */
1911 		case NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_READY:
1912 			spdk_nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_CH);
1913 			break;
1914 		/* Wait for the common header  */
1915 		case NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_CH:
1916 			pdu = &tqpair->pdu_in_progress;
1917 			/* common header */
1918 			if (pdu->ch_valid_bytes < sizeof(struct spdk_nvme_tcp_common_pdu_hdr)) {
1919 				rc = nvme_tcp_read_data(tqpair->sock,
1920 							sizeof(struct spdk_nvme_tcp_common_pdu_hdr) - pdu->ch_valid_bytes,
1921 							(void *)&pdu->hdr.common + pdu->ch_valid_bytes);
1922 				if (rc < 0) {
1923 					SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "will disconnect tqpair=%p\n", tqpair);
1924 					spdk_nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_ERROR);
1925 					break;
1926 				}
1927 				pdu->ch_valid_bytes += rc;
1928 				if (pdu->ch_valid_bytes < sizeof(struct spdk_nvme_tcp_common_pdu_hdr)) {
1929 					return NVME_TCP_PDU_IN_PROGRESS;
1930 				}
1931 			}
1932 
1933 			tqpair->last_pdu_time = spdk_get_ticks();
1934 			/* The command header of this PDU has now been read from the socket. */
1935 			spdk_nvmf_tcp_pdu_ch_handle(tqpair);
1936 			break;
1937 		/* Wait for the pdu specific header  */
1938 		case NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_PSH:
1939 			pdu = &tqpair->pdu_in_progress;
1940 			if (pdu->hd_is_read) {
1941 				break;
1942 			}
1943 
1944 			psh_len = hlen = pdu->hdr.common.hlen;
1945 			/* Only capsule_cmd and h2c_data has header digest */
1946 			if (((pdu->hdr.common.pdu_type == SPDK_NVME_TCP_PDU_TYPE_CAPSULE_CMD) ||
1947 			     (pdu->hdr.common.pdu_type == SPDK_NVME_TCP_PDU_TYPE_H2C_DATA)) &&
1948 			    tqpair->host_hdgst_enable) {
1949 				pdu->has_hdgst = true;
1950 				psh_len += SPDK_NVME_TCP_DIGEST_LEN;
1951 				if (pdu->hdr.common.plen > psh_len) {
1952 					pdo = pdu->hdr.common.pdo;
1953 					padding_len = pdo - psh_len;
1954 					SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "padding length is =%d for pdu=%p on tqpair=%p\n", padding_len,
1955 						      pdu, tqpair);
1956 					if (padding_len > 0) {
1957 						psh_len = pdo;
1958 					}
1959 				}
1960 			}
1961 
1962 			psh_len -= sizeof(struct spdk_nvme_tcp_common_pdu_hdr);
1963 			/* The following will read psh + hdgest (if possbile) + padding (if posssible) */
1964 			if (pdu->psh_valid_bytes < psh_len) {
1965 				rc = nvme_tcp_read_data(tqpair->sock,
1966 							psh_len - pdu->psh_valid_bytes,
1967 							(void *)&pdu->hdr.raw + sizeof(struct spdk_nvme_tcp_common_pdu_hdr) + pdu->psh_valid_bytes);
1968 				if (rc < 0) {
1969 					spdk_nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_ERROR);
1970 					break;
1971 				}
1972 
1973 				pdu->psh_valid_bytes += rc;
1974 				if (pdu->psh_valid_bytes < psh_len) {
1975 					return NVME_TCP_PDU_IN_PROGRESS;
1976 				}
1977 			}
1978 
1979 			pdu->hd_is_read = true;
1980 
1981 			/* All header(ch, psh, head digist) of this PDU has now been read from the socket. */
1982 			spdk_nvmf_tcp_pdu_psh_handle(tqpair);
1983 			break;
1984 		case NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_PAYLOAD:
1985 			pdu = &tqpair->pdu_in_progress;
1986 
1987 			/* check whether the data is valid, if not we just return */
1988 			if (!pdu->data) {
1989 				return NVME_TCP_PDU_IN_PROGRESS;
1990 			}
1991 
1992 			data_len = pdu->data_len;
1993 			/* data len */
1994 			if (pdu->data_valid_bytes < data_len) {
1995 				rc = nvme_tcp_read_data(tqpair->sock, data_len - pdu->data_valid_bytes,
1996 							(void *)pdu->data + pdu->data_valid_bytes);
1997 				if (rc < 0) {
1998 					spdk_nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_ERROR);
1999 					break;
2000 				}
2001 
2002 				pdu->data_valid_bytes += rc;
2003 				if (pdu->data_valid_bytes < data_len) {
2004 					return NVME_TCP_PDU_IN_PROGRESS;
2005 				}
2006 			}
2007 
2008 			/* data digest */
2009 			if ((pdu->hdr.common.pdu_type != SPDK_NVME_TCP_PDU_TYPE_H2C_TERM_REQ) &&
2010 			    tqpair->host_ddgst_enable && (pdu->ddigest_valid_bytes < SPDK_NVME_TCP_DIGEST_LEN)) {
2011 				rc = nvme_tcp_read_data(tqpair->sock,
2012 							SPDK_NVME_TCP_DIGEST_LEN - pdu->ddigest_valid_bytes,
2013 							pdu->data_digest + pdu->ddigest_valid_bytes);
2014 				if (rc < 0) {
2015 					spdk_nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_ERROR);
2016 					break;
2017 				}
2018 
2019 				pdu->ddigest_valid_bytes += rc;
2020 				if (pdu->ddigest_valid_bytes < SPDK_NVME_TCP_DIGEST_LEN) {
2021 					return NVME_TCP_PDU_IN_PROGRESS;
2022 				}
2023 			}
2024 
2025 			/* All of this PDU has now been read from the socket. */
2026 			spdk_nvmf_tcp_pdu_payload_handle(tqpair);
2027 			break;
2028 		case NVME_TCP_PDU_RECV_STATE_ERROR:
2029 			rc = NVME_TCP_PDU_FATAL;
2030 			break;
2031 		default:
2032 			assert(0);
2033 			SPDK_ERRLOG("code should not come to here");
2034 			break;
2035 		}
2036 	} while (tqpair->recv_state != prev_state);
2037 
2038 	return rc;
2039 }
2040 
2041 static enum spdk_nvme_data_transfer
2042 spdk_nvmf_tcp_req_get_xfer(struct nvme_tcp_req *tcp_req) {
2043 	enum spdk_nvme_data_transfer xfer;
2044 	struct spdk_nvme_cmd *cmd = &tcp_req->req.cmd->nvme_cmd;
2045 	struct spdk_nvme_sgl_descriptor *sgl = &cmd->dptr.sgl1;
2046 
2047 	/* Figure out data transfer direction */
2048 	if (cmd->opc == SPDK_NVME_OPC_FABRIC)
2049 	{
2050 		xfer = spdk_nvme_opc_get_data_transfer(tcp_req->req.cmd->nvmf_cmd.fctype);
2051 	} else
2052 	{
2053 		xfer = spdk_nvme_opc_get_data_transfer(cmd->opc);
2054 
2055 		/* Some admin commands are special cases */
2056 		if ((tcp_req->req.qpair->qid == 0) &&
2057 		    ((cmd->opc == SPDK_NVME_OPC_GET_FEATURES) ||
2058 		     (cmd->opc == SPDK_NVME_OPC_SET_FEATURES))) {
2059 			switch (cmd->cdw10 & 0xff) {
2060 			case SPDK_NVME_FEAT_LBA_RANGE_TYPE:
2061 			case SPDK_NVME_FEAT_AUTONOMOUS_POWER_STATE_TRANSITION:
2062 			case SPDK_NVME_FEAT_HOST_IDENTIFIER:
2063 				break;
2064 			default:
2065 				xfer = SPDK_NVME_DATA_NONE;
2066 			}
2067 		}
2068 	}
2069 
2070 	if (xfer == SPDK_NVME_DATA_NONE)
2071 	{
2072 		return xfer;
2073 	}
2074 
2075 	/* Even for commands that may transfer data, they could have specified 0 length.
2076 	 * We want those to show up with xfer SPDK_NVME_DATA_NONE.
2077 	 */
2078 	switch (sgl->generic.type)
2079 	{
2080 	case SPDK_NVME_SGL_TYPE_DATA_BLOCK:
2081 	case SPDK_NVME_SGL_TYPE_BIT_BUCKET:
2082 	case SPDK_NVME_SGL_TYPE_SEGMENT:
2083 	case SPDK_NVME_SGL_TYPE_LAST_SEGMENT:
2084 	case SPDK_NVME_SGL_TYPE_TRANSPORT_DATA_BLOCK:
2085 		if (sgl->unkeyed.length == 0) {
2086 			xfer = SPDK_NVME_DATA_NONE;
2087 		}
2088 		break;
2089 	case SPDK_NVME_SGL_TYPE_KEYED_DATA_BLOCK:
2090 		if (sgl->keyed.length == 0) {
2091 			xfer = SPDK_NVME_DATA_NONE;
2092 		}
2093 		break;
2094 	}
2095 
2096 	return xfer;
2097 }
2098 
2099 static int
2100 spdk_nvmf_tcp_req_fill_iovs(struct spdk_nvmf_tcp_transport *ttransport,
2101 			    struct nvme_tcp_req *tcp_req)
2102 {
2103 	void		*buf = NULL;
2104 	uint32_t	length = tcp_req->req.length;
2105 	uint32_t	i = 0;
2106 
2107 	tcp_req->req.iovcnt = 0;
2108 	while (length) {
2109 		buf = spdk_mempool_get(ttransport->data_buf_pool);
2110 		if (!buf) {
2111 			goto nomem;
2112 		}
2113 
2114 		tcp_req->req.iov[i].iov_base = (void *)((uintptr_t)(buf + NVMF_DATA_BUFFER_MASK) &
2115 							~NVMF_DATA_BUFFER_MASK);
2116 		tcp_req->req.iov[i].iov_len  = spdk_min(length, ttransport->io_unit_size);
2117 		tcp_req->req.iovcnt++;
2118 		tcp_req->buffers[i] = buf;
2119 		length -= tcp_req->req.iov[i].iov_len;
2120 		i++;
2121 	}
2122 
2123 	tcp_req->data_from_pool = true;
2124 
2125 	return 0;
2126 
2127 nomem:
2128 	while (i) {
2129 		i--;
2130 		spdk_mempool_put(ttransport->data_buf_pool, tcp_req->req.iov[i].iov_base);
2131 		tcp_req->req.iov[i].iov_base = NULL;
2132 		tcp_req->req.iov[i].iov_len = 0;
2133 
2134 	}
2135 	tcp_req->req.iovcnt = 0;
2136 	return -ENOMEM;
2137 }
2138 
2139 static int
2140 spdk_nvmf_tcp_req_parse_sgl(struct spdk_nvmf_tcp_transport *ttransport,
2141 			    struct nvme_tcp_req *tcp_req)
2142 {
2143 	struct spdk_nvme_cmd			*cmd;
2144 	struct spdk_nvme_cpl			*rsp;
2145 	struct spdk_nvme_sgl_descriptor		*sgl;
2146 
2147 	cmd = &tcp_req->req.cmd->nvme_cmd;
2148 	rsp = &tcp_req->req.rsp->nvme_cpl;
2149 	sgl = &cmd->dptr.sgl1;
2150 
2151 	if (sgl->generic.type == SPDK_NVME_SGL_TYPE_TRANSPORT_DATA_BLOCK &&
2152 	    sgl->unkeyed.subtype == SPDK_NVME_SGL_SUBTYPE_TRANSPORT) {
2153 		if (sgl->unkeyed.length > ttransport->max_io_size) {
2154 			SPDK_ERRLOG("SGL length 0x%x exceeds max io size 0x%x\n",
2155 				    sgl->unkeyed.length, ttransport->max_io_size);
2156 			rsp->status.sc = SPDK_NVME_SC_DATA_SGL_LENGTH_INVALID;
2157 			return -1;
2158 		}
2159 
2160 		/* fill request length and populate iovs */
2161 		tcp_req->req.length = sgl->unkeyed.length;
2162 
2163 		SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "Data requested length= 0x%x\n",
2164 			      sgl->unkeyed.length);
2165 
2166 		if (spdk_nvmf_tcp_req_fill_iovs(ttransport, tcp_req) < 0) {
2167 			/* No available buffers. Queue this request up. */
2168 			SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "No available large data buffers. Queueing request %p\n", tcp_req);
2169 			return 0;
2170 		}
2171 
2172 		/* backward compatible */
2173 		tcp_req->req.data = tcp_req->req.iov[0].iov_base;
2174 
2175 
2176 		SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "Request %p took %d buffer/s from central pool, and data=%p\n",
2177 			      tcp_req,
2178 			      tcp_req->req.iovcnt, tcp_req->req.data);
2179 
2180 		return 0;
2181 	} else if (sgl->generic.type == SPDK_NVME_SGL_TYPE_DATA_BLOCK &&
2182 		   sgl->unkeyed.subtype == SPDK_NVME_SGL_SUBTYPE_OFFSET) {
2183 		uint64_t offset = sgl->address;
2184 		uint32_t max_len = ttransport->in_capsule_data_size;
2185 
2186 		SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "In-capsule data: offset 0x%" PRIx64 ", length 0x%x\n",
2187 			      offset, sgl->unkeyed.length);
2188 
2189 		if (offset > max_len) {
2190 			SPDK_ERRLOG("In-capsule offset 0x%" PRIx64 " exceeds capsule length 0x%x\n",
2191 				    offset, max_len);
2192 			rsp->status.sc = SPDK_NVME_SC_INVALID_SGL_OFFSET;
2193 			return -1;
2194 		}
2195 		max_len -= (uint32_t)offset;
2196 
2197 		if (sgl->unkeyed.length > max_len) {
2198 			SPDK_ERRLOG("In-capsule data length 0x%x exceeds capsule length 0x%x\n",
2199 				    sgl->unkeyed.length, max_len);
2200 			rsp->status.sc = SPDK_NVME_SC_DATA_SGL_LENGTH_INVALID;
2201 			return -1;
2202 		}
2203 
2204 		tcp_req->req.data = tcp_req->buf + offset;
2205 		tcp_req->data_from_pool = false;
2206 		tcp_req->req.length = sgl->unkeyed.length;
2207 
2208 		tcp_req->req.iov[0].iov_base = tcp_req->req.data;
2209 		tcp_req->req.iov[0].iov_len = tcp_req->req.length;
2210 		tcp_req->req.iovcnt = 1;
2211 
2212 		return 0;
2213 	}
2214 
2215 	SPDK_ERRLOG("Invalid NVMf I/O Command SGL:  Type 0x%x, Subtype 0x%x\n",
2216 		    sgl->generic.type, sgl->generic.subtype);
2217 	rsp->status.sc = SPDK_NVME_SC_SGL_DESCRIPTOR_TYPE_INVALID;
2218 	return -1;
2219 }
2220 
2221 static void
2222 spdk_nvmf_tcp_send_c2h_data(struct nvme_tcp_qpair *tqpair,
2223 			    struct nvme_tcp_req *tcp_req)
2224 {
2225 	struct nvme_tcp_pdu *rsp_pdu;
2226 	struct spdk_nvme_tcp_c2h_data_hdr *c2h_data;
2227 	uint32_t plen, pdo, alignment, offset, iov_index;
2228 
2229 	SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "enter\n");
2230 
2231 	/* always use the first iov_len, which is correct */
2232 	iov_index = tcp_req->c2h_data_offset / tcp_req->req.iov[0].iov_len;
2233 	offset = tcp_req->c2h_data_offset % tcp_req->req.iov[0].iov_len;
2234 
2235 	rsp_pdu = spdk_nvmf_tcp_pdu_get(tqpair);
2236 	assert(rsp_pdu != NULL);
2237 
2238 	c2h_data = &rsp_pdu->hdr.c2h_data;
2239 	c2h_data->common.pdu_type = SPDK_NVME_TCP_PDU_TYPE_C2H_DATA;
2240 	plen = c2h_data->common.hlen = sizeof(*c2h_data);
2241 
2242 	if (tqpair->host_hdgst_enable) {
2243 		plen += SPDK_NVME_TCP_DIGEST_LEN;
2244 		c2h_data->common.flags |= SPDK_NVME_TCP_CH_FLAGS_HDGSTF;
2245 	}
2246 
2247 	/* set the psh */
2248 	c2h_data->cccid = tcp_req->req.cmd->nvme_cmd.cid;
2249 	c2h_data->datal = spdk_min(NVMF_TCP_PDU_MAX_C2H_DATA_SIZE,
2250 				   (tcp_req->req.iov[iov_index].iov_len - offset));
2251 	c2h_data->datao = tcp_req->c2h_data_offset;
2252 
2253 	/* set the padding */
2254 	rsp_pdu->padding_len = 0;
2255 	pdo = plen;
2256 	if (tqpair->cpda) {
2257 		alignment = (tqpair->cpda + 1) << 2;
2258 		if (alignment > plen) {
2259 			rsp_pdu->padding_len = alignment - plen;
2260 			pdo = plen = alignment;
2261 		}
2262 	}
2263 
2264 	c2h_data->common.pdo = pdo;
2265 	plen += c2h_data->datal;
2266 	if (tqpair->host_ddgst_enable) {
2267 		c2h_data->common.flags |= SPDK_NVME_TCP_CH_FLAGS_DDGSTF;
2268 		plen += SPDK_NVME_TCP_DIGEST_LEN;
2269 	}
2270 
2271 	c2h_data->common.plen = plen;
2272 
2273 	rsp_pdu->data = tcp_req->req.iov[iov_index].iov_base + offset;
2274 	rsp_pdu->data_len = c2h_data->datal;
2275 
2276 	tcp_req->c2h_data_offset += c2h_data->datal;
2277 	if (iov_index == (tcp_req->req.iovcnt - 1) && (tcp_req->c2h_data_offset == tcp_req->req.length)) {
2278 		SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "Last pdu for tcp_req=%p on tqpair=%p\n", tcp_req, tqpair);
2279 		c2h_data->common.flags |= SPDK_NVME_TCP_C2H_DATA_FLAGS_LAST_PDU;
2280 		/* The linux kernel does not support this yet */
2281 #if LINUX_KERNEL_SUPPORT_NOT_SENDING_RESP_FOR_C2H
2282 		c2h_data->common.flags |= SPDK_NVME_TCP_C2H_DATA_FLAGS_SUCCESS;
2283 #endif
2284 		TAILQ_REMOVE(&tqpair->queued_c2h_data_tcp_req, tcp_req, link);
2285 	}
2286 
2287 	tqpair->c2h_data_pdu_cnt += 1;
2288 	spdk_nvmf_tcp_qpair_write_pdu(tqpair, rsp_pdu, spdk_nvmf_tcp_pdu_c2h_data_complete, tcp_req);
2289 }
2290 
2291 static int
2292 spdk_nvmf_tcp_calc_c2h_data_pdu_num(struct nvme_tcp_req *tcp_req)
2293 {
2294 	uint32_t i, iov_cnt, pdu_num = 0;
2295 
2296 	iov_cnt = tcp_req->req.iovcnt;
2297 	for (i = 0; i < iov_cnt; i++) {
2298 		pdu_num += (tcp_req->req.iov[i].iov_len + NVMF_TCP_PDU_MAX_C2H_DATA_SIZE - 1) /
2299 			   NVMF_TCP_PDU_MAX_C2H_DATA_SIZE;
2300 	}
2301 
2302 	return pdu_num;
2303 }
2304 
2305 static void
2306 spdk_nvmf_tcp_handle_pending_c2h_data_queue(struct nvme_tcp_qpair *tqpair)
2307 {
2308 	struct nvme_tcp_req *tcp_req;
2309 
2310 	while (!TAILQ_EMPTY(&tqpair->queued_c2h_data_tcp_req) &&
2311 	       (tqpair->c2h_data_pdu_cnt < NVMF_TCP_QPAIR_MAX_C2H_PDU_NUM)) {
2312 		tcp_req = TAILQ_FIRST(&tqpair->queued_c2h_data_tcp_req);
2313 		spdk_nvmf_tcp_send_c2h_data(tqpair, tcp_req);
2314 	}
2315 }
2316 
2317 static void
2318 spdk_nvmf_tcp_queue_c2h_data(struct nvme_tcp_req *tcp_req,
2319 			     struct nvme_tcp_qpair *tqpair)
2320 {
2321 	tcp_req->c2h_data_pdu_num = spdk_nvmf_tcp_calc_c2h_data_pdu_num(tcp_req);
2322 
2323 	assert(tcp_req->c2h_data_pdu_num < NVMF_TCP_QPAIR_MAX_C2H_PDU_NUM);
2324 
2325 	TAILQ_INSERT_TAIL(&tqpair->queued_c2h_data_tcp_req, tcp_req, link);
2326 	spdk_nvmf_tcp_handle_pending_c2h_data_queue(tqpair);
2327 }
2328 
2329 static int
2330 request_transfer_out(struct spdk_nvmf_request *req)
2331 {
2332 	struct nvme_tcp_req	*tcp_req;
2333 	struct spdk_nvmf_qpair		*qpair;
2334 	struct nvme_tcp_qpair	*tqpair;
2335 	struct spdk_nvme_cpl		*rsp;
2336 
2337 	SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "enter\n");
2338 
2339 	qpair = req->qpair;
2340 	rsp = &req->rsp->nvme_cpl;
2341 	tcp_req = SPDK_CONTAINEROF(req, struct nvme_tcp_req, req);
2342 
2343 	/* Advance our sq_head pointer */
2344 	if (qpair->sq_head == qpair->sq_head_max) {
2345 		qpair->sq_head = 0;
2346 	} else {
2347 		qpair->sq_head++;
2348 	}
2349 	rsp->sqhd = qpair->sq_head;
2350 
2351 	tqpair = SPDK_CONTAINEROF(tcp_req->req.qpair, struct nvme_tcp_qpair, qpair);
2352 	spdk_nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_TRANSFERRING_CONTROLLER_TO_HOST);
2353 	if (rsp->status.sc == SPDK_NVME_SC_SUCCESS &&
2354 	    req->xfer == SPDK_NVME_DATA_CONTROLLER_TO_HOST) {
2355 		spdk_nvmf_tcp_queue_c2h_data(tcp_req, tqpair);
2356 	} else {
2357 		spdk_nvmf_tcp_send_capsule_resp_pdu(tcp_req, tqpair);
2358 	}
2359 
2360 	return 0;
2361 }
2362 
2363 static void
2364 spdk_nvmf_tcp_pdu_set_buf_from_req(struct spdk_nvmf_tcp_transport *ttransport,
2365 				   struct nvme_tcp_qpair *tqpair,
2366 				   struct nvme_tcp_req *tcp_req)
2367 {
2368 	struct nvme_tcp_pdu *pdu;
2369 	uint32_t plen = 0;
2370 
2371 	pdu = &tqpair->pdu_in_progress;
2372 
2373 	SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "tqpair=%p\n", tqpair);
2374 	assert(pdu->hdr.common.pdu_type == SPDK_NVME_TCP_PDU_TYPE_CAPSULE_CMD);
2375 
2376 	plen = pdu->hdr.common.hlen;
2377 	if (tqpair->host_hdgst_enable) {
2378 		plen += SPDK_NVME_TCP_DIGEST_LEN;
2379 	}
2380 
2381 	/* need to send r2t for data */
2382 	if (pdu->hdr.common.plen == plen) {
2383 		spdk_nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_READY);
2384 		SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "Will send r2t for tcp_req(%p) on tqpair=%p\n", tcp_req, tqpair);
2385 		tcp_req->next_expected_r2t_offset = 0;
2386 		TAILQ_INSERT_TAIL(&tqpair->queued_r2t_tcp_req, tcp_req, link);
2387 		spdk_nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_DATA_PENDING_FOR_R2T);
2388 
2389 	} else {
2390 		SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "Not need to send r2t for tcp_req(%p) on tqpair=%p\n", tcp_req,
2391 			      tqpair);
2392 		/* No need to send r2t, contained in the capsuled data */
2393 		pdu->data = tcp_req->req.data;
2394 		pdu->data_len = tcp_req->req.length;
2395 		spdk_nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_PAYLOAD);
2396 		spdk_nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_TRANSFERRING_HOST_TO_CONTROLLER);
2397 	}
2398 }
2399 
2400 static bool
2401 spdk_nvmf_tcp_req_process(struct spdk_nvmf_tcp_transport *ttransport,
2402 			  struct nvme_tcp_req *tcp_req)
2403 {
2404 	struct nvme_tcp_qpair	*tqpair;
2405 	struct spdk_nvme_cpl		*rsp = &tcp_req->req.rsp->nvme_cpl;
2406 	int				rc;
2407 	enum spdk_nvmf_tcp_req_state prev_state;
2408 	bool				progress = false;
2409 
2410 	tqpair = SPDK_CONTAINEROF(tcp_req->req.qpair, struct nvme_tcp_qpair, qpair);
2411 	assert(tcp_req->state != TCP_REQUEST_STATE_FREE);
2412 
2413 	/* The loop here is to allow for several back-to-back state changes. */
2414 	do {
2415 		prev_state = tcp_req->state;
2416 
2417 		SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "Request %p entering state %d on tqpair=%p\n", tcp_req, prev_state,
2418 			      tqpair);
2419 
2420 		switch (tcp_req->state) {
2421 		case TCP_REQUEST_STATE_FREE:
2422 			/* Some external code must kick a request into TCP_REQUEST_STATE_NEW
2423 			 * to escape this state. */
2424 			break;
2425 		case TCP_REQUEST_STATE_NEW:
2426 			spdk_trace_record(TRACE_TCP_REQUEST_STATE_NEW, 0, 0, (uintptr_t)tcp_req, 0);
2427 
2428 			/* copy the cmd from the receive pdu */
2429 			tcp_req->cmd = tqpair->pdu_in_progress.hdr.capsule_cmd.ccsqe;
2430 
2431 			/* The next state transition depends on the data transfer needs of this request. */
2432 			tcp_req->req.xfer = spdk_nvmf_tcp_req_get_xfer(tcp_req);
2433 
2434 			/* If no data to transfer, ready to execute. */
2435 			if (tcp_req->req.xfer == SPDK_NVME_DATA_NONE) {
2436 				/* Reset the tqpair receving pdu state */
2437 				spdk_nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_READY);
2438 				spdk_nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_READY_TO_EXECUTE);
2439 				break;
2440 			}
2441 
2442 			spdk_nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_NEED_BUFFER);
2443 			TAILQ_INSERT_TAIL(&tqpair->ch->pending_data_buf_queue, tcp_req, link);
2444 			break;
2445 		case TCP_REQUEST_STATE_NEED_BUFFER:
2446 			spdk_trace_record(TRACE_TCP_REQUEST_STATE_NEED_BUFFER, 0, 0, (uintptr_t)tcp_req, 0);
2447 
2448 			assert(tcp_req->req.xfer != SPDK_NVME_DATA_NONE);
2449 
2450 			if (tcp_req != TAILQ_FIRST(&tqpair->ch->pending_data_buf_queue)) {
2451 				SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP,
2452 					      "Not the first element to wait for the buf for tcp_req(%p) on tqpair=%p\n",
2453 					      tcp_req, tqpair);
2454 				/* This request needs to wait in line to obtain a buffer */
2455 				break;
2456 			}
2457 
2458 			/* Try to get a data buffer */
2459 			rc = spdk_nvmf_tcp_req_parse_sgl(ttransport, tcp_req);
2460 			if (rc < 0) {
2461 				TAILQ_REMOVE(&tqpair->ch->pending_data_buf_queue, tcp_req, link);
2462 				rsp->status.sc = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR;
2463 				/* Reset the tqpair receving pdu state */
2464 				spdk_nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_READY);
2465 				spdk_nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_READY_TO_COMPLETE);
2466 				break;
2467 			}
2468 
2469 			if (!tcp_req->req.data) {
2470 				SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "No buffer allocated for tcp_req(%p) on tqpair(%p\n)",
2471 					      tcp_req, tqpair);
2472 				/* No buffers available. */
2473 				break;
2474 			}
2475 
2476 			TAILQ_REMOVE(&tqpair->ch->pending_data_buf_queue, tcp_req, link);
2477 
2478 			/* If data is transferring from host to controller, we need to do a transfer from the host. */
2479 			if (tcp_req->req.xfer == SPDK_NVME_DATA_HOST_TO_CONTROLLER) {
2480 				spdk_nvmf_tcp_pdu_set_buf_from_req(ttransport, tqpair, tcp_req);
2481 				break;
2482 			}
2483 
2484 			spdk_nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_READY);
2485 			spdk_nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_READY_TO_EXECUTE);
2486 			break;
2487 		case TCP_REQUEST_STATE_DATA_PENDING_FOR_R2T:
2488 			spdk_trace_record(TCP_REQUEST_STATE_DATA_PENDING_FOR_R2T, 0, 0,
2489 					  (uintptr_t)tcp_req, 0);
2490 
2491 			if (tqpair->pending_r2t < tqpair->maxr2t) {
2492 				tqpair->pending_r2t++;
2493 				TAILQ_REMOVE(&tqpair->queued_r2t_tcp_req, tcp_req, link);
2494 				spdk_nvmf_tcp_send_r2t_pdu(ttransport, tqpair, tcp_req);
2495 				spdk_nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_TRANSFERRING_HOST_TO_CONTROLLER);
2496 			}
2497 
2498 			break;
2499 
2500 		case TCP_REQUEST_STATE_TRANSFERRING_HOST_TO_CONTROLLER:
2501 			spdk_trace_record(TRACE_TCP_REQUEST_STATE_TRANSFERRING_HOST_TO_CONTROLLER, 0, 0,
2502 					  (uintptr_t)tcp_req, 0);
2503 			/* Some external code must kick a request into TCP_REQUEST_STATE_READY_TO_EXECUTE
2504 			 * to escape this state. */
2505 			break;
2506 		case TCP_REQUEST_STATE_READY_TO_EXECUTE:
2507 			spdk_trace_record(TRACE_TCP_REQUEST_STATE_READY_TO_EXECUTE, 0, 0, (uintptr_t)tcp_req, 0);
2508 			spdk_nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_EXECUTING);
2509 			spdk_nvmf_request_exec(&tcp_req->req);
2510 			break;
2511 		case TCP_REQUEST_STATE_EXECUTING:
2512 			spdk_trace_record(TRACE_TCP_REQUEST_STATE_EXECUTING, 0, 0, (uintptr_t)tcp_req, 0);
2513 			/* Some external code must kick a request into TCP_REQUEST_STATE_EXECUTED
2514 			 * to escape this state. */
2515 			break;
2516 		case TCP_REQUEST_STATE_EXECUTED:
2517 			spdk_trace_record(TRACE_TCP_REQUEST_STATE_EXECUTED, 0, 0, (uintptr_t)tcp_req, 0);
2518 			spdk_nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_READY_TO_COMPLETE);
2519 			break;
2520 		case TCP_REQUEST_STATE_READY_TO_COMPLETE:
2521 			spdk_trace_record(TRACE_TCP_REQUEST_STATE_READY_TO_COMPLETE, 0, 0, (uintptr_t)tcp_req, 0);
2522 			rc = request_transfer_out(&tcp_req->req);
2523 			assert(rc == 0); /* No good way to handle this currently */
2524 			break;
2525 		case TCP_REQUEST_STATE_TRANSFERRING_CONTROLLER_TO_HOST:
2526 			spdk_trace_record(TRACE_TCP_REQUEST_STATE_TRANSFERRING_CONTROLLER_TO_HOST, 0, 0,
2527 					  (uintptr_t)tcp_req,
2528 					  0);
2529 			/* Some external code must kick a request into TCP_REQUEST_STATE_COMPLETED
2530 			 * to escape this state. */
2531 			break;
2532 		case TCP_REQUEST_STATE_COMPLETED:
2533 			spdk_trace_record(TRACE_TCP_REQUEST_STATE_COMPLETED, 0, 0, (uintptr_t)tcp_req, 0);
2534 			if (tcp_req->data_from_pool) {
2535 				/* Put the buffer/s back in the pool */
2536 				for (uint32_t i = 0; i < tcp_req->req.iovcnt; i++) {
2537 					spdk_mempool_put(ttransport->data_buf_pool, tcp_req->buffers[i]);
2538 					tcp_req->req.iov[i].iov_base = NULL;
2539 					tcp_req->buffers[i] = NULL;
2540 				}
2541 				tcp_req->data_from_pool = false;
2542 			}
2543 			tcp_req->req.length = 0;
2544 			tcp_req->req.iovcnt = 0;
2545 			tcp_req->req.data = NULL;
2546 			spdk_nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_FREE);
2547 			break;
2548 		case TCP_REQUEST_NUM_STATES:
2549 		default:
2550 			assert(0);
2551 			break;
2552 		}
2553 
2554 		if (tcp_req->state != prev_state) {
2555 			progress = true;
2556 		}
2557 	} while (tcp_req->state != prev_state);
2558 
2559 	return progress;
2560 }
2561 
2562 static void
2563 spdk_nvmf_tcp_qpair_process_pending(struct spdk_nvmf_tcp_transport *ttransport,
2564 				    struct nvme_tcp_qpair *tqpair)
2565 {
2566 	struct nvme_tcp_req *tcp_req, *req_tmp;
2567 
2568 	TAILQ_FOREACH_SAFE(tcp_req, &tqpair->queued_r2t_tcp_req, link, req_tmp) {
2569 		if (tqpair->pending_r2t < tqpair->maxr2t) {
2570 			if (spdk_nvmf_tcp_req_process(ttransport, tcp_req) == false) {
2571 				break;
2572 			}
2573 		}
2574 	}
2575 
2576 	TAILQ_FOREACH_SAFE(tcp_req, &tqpair->ch->pending_data_buf_queue, link, req_tmp) {
2577 		if (spdk_nvmf_tcp_req_process(ttransport, tcp_req) == false) {
2578 			break;
2579 		}
2580 	}
2581 }
2582 
2583 static void
2584 spdk_nvmf_tcp_sock_cb(void *arg, struct spdk_sock_group *group, struct spdk_sock *sock)
2585 {
2586 	struct nvme_tcp_qpair *tqpair = arg;
2587 	struct spdk_nvmf_tcp_transport *ttransport;
2588 	int rc;
2589 
2590 	assert(tqpair != NULL);
2591 
2592 	if (tqpair->recv_state == NVME_TCP_PDU_RECV_STATE_ERROR) {
2593 		return;
2594 	}
2595 
2596 	ttransport = SPDK_CONTAINEROF(tqpair->qpair.transport, struct spdk_nvmf_tcp_transport, transport);
2597 	spdk_nvmf_tcp_qpair_process_pending(ttransport, tqpair);
2598 
2599 	rc = spdk_nvmf_tcp_sock_process(tqpair);
2600 	if (rc < 0) {
2601 		tqpair->state = NVME_TCP_QPAIR_STATE_EXITING;
2602 		spdk_nvmf_tcp_qpair_flush_pdus(tqpair);
2603 	}
2604 }
2605 
2606 static int
2607 spdk_nvmf_tcp_poll_group_add(struct spdk_nvmf_transport_poll_group *group,
2608 			     struct spdk_nvmf_qpair *qpair)
2609 {
2610 	struct spdk_nvmf_tcp_poll_group	*tgroup;
2611 	struct nvme_tcp_qpair	*tqpair;
2612 	int				rc;
2613 
2614 	tgroup = SPDK_CONTAINEROF(group, struct spdk_nvmf_tcp_poll_group, group);
2615 	tqpair = SPDK_CONTAINEROF(qpair, struct nvme_tcp_qpair, qpair);
2616 
2617 	rc = spdk_sock_group_add_sock(tgroup->sock_group, tqpair->sock,
2618 				      spdk_nvmf_tcp_sock_cb, tqpair);
2619 	if (rc != 0) {
2620 		SPDK_ERRLOG("Could not add sock to sock_group: %s (%d)\n",
2621 			    spdk_strerror(errno), errno);
2622 		spdk_nvmf_tcp_qpair_destroy(tqpair);
2623 		return -1;
2624 	}
2625 
2626 	rc =  spdk_nvmf_tcp_qpair_sock_init(tqpair);
2627 	if (rc != 0) {
2628 		SPDK_ERRLOG("Cannot set sock opt for tqpair=%p\n", tqpair);
2629 		spdk_nvmf_tcp_qpair_destroy(tqpair);
2630 		return -1;
2631 	}
2632 
2633 	rc = spdk_nvmf_tcp_qpair_init(&tqpair->qpair);
2634 	if (rc < 0) {
2635 		SPDK_ERRLOG("Cannot init tqpair=%p\n", tqpair);
2636 		spdk_nvmf_tcp_qpair_destroy(tqpair);
2637 		return -1;
2638 	}
2639 
2640 	rc = spdk_nvmf_tcp_qpair_init_mem_resource(tqpair, 1);
2641 	if (rc < 0) {
2642 		SPDK_ERRLOG("Cannot init memory resource info for tqpair=%p\n", tqpair);
2643 		spdk_nvmf_tcp_qpair_destroy(tqpair);
2644 		return -1;
2645 	}
2646 
2647 	tqpair->state = NVME_TCP_QPAIR_STATE_INVALID;
2648 	tqpair->timeout = SPDK_NVME_TCP_QPAIR_EXIT_TIMEOUT;
2649 	tqpair->last_pdu_time = spdk_get_ticks();
2650 
2651 	tqpair->tgroup = tgroup;
2652 	TAILQ_INSERT_TAIL(&tgroup->qpairs, tqpair, link);
2653 
2654 	return 0;
2655 }
2656 
2657 static int
2658 spdk_nvmf_tcp_req_complete(struct spdk_nvmf_request *req)
2659 {
2660 	struct spdk_nvmf_tcp_transport *ttransport;
2661 	struct nvme_tcp_req *tcp_req;
2662 
2663 	ttransport = SPDK_CONTAINEROF(req->qpair->transport, struct spdk_nvmf_tcp_transport, transport);
2664 	tcp_req = SPDK_CONTAINEROF(req, struct nvme_tcp_req, req);
2665 
2666 	spdk_nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_EXECUTED);
2667 	spdk_nvmf_tcp_req_process(ttransport, tcp_req);
2668 
2669 	return 0;
2670 }
2671 
2672 static void
2673 spdk_nvmf_tcp_close_qpair(struct spdk_nvmf_qpair *qpair)
2674 {
2675 	SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "enter\n");
2676 
2677 	spdk_nvmf_tcp_qpair_destroy(SPDK_CONTAINEROF(qpair, struct nvme_tcp_qpair, qpair));
2678 }
2679 
2680 static void
2681 spdk_nvmf_tcp_qpair_handle_timout(struct nvme_tcp_qpair *tqpair)
2682 {
2683 	uint64_t	tsc;
2684 
2685 	if ((tqpair->state == NVME_TCP_QPAIR_STATE_EXITING) ||
2686 	    (tqpair->state == NVME_TCP_QPAIR_STATE_EXITED)) {
2687 		return;
2688 	}
2689 
2690 	/* Currently, we did not have keep alive support, so make sure that we should have the generic support later */
2691 	if (tqpair->recv_state != NVME_TCP_PDU_RECV_STATE_ERROR) {
2692 		return;
2693 	}
2694 
2695 	/* Check for interval expiration */
2696 	tsc = spdk_get_ticks();
2697 	if ((tsc - tqpair->last_pdu_time) > (tqpair->timeout  * spdk_get_ticks_hz())) {
2698 		SPDK_ERRLOG("No pdu coming for tqpair=%p within %d seconds\n", tqpair, tqpair->timeout);
2699 		tqpair->state = NVME_TCP_QPAIR_STATE_EXITING;
2700 	}
2701 }
2702 
2703 static int
2704 spdk_nvmf_tcp_poll_group_poll(struct spdk_nvmf_transport_poll_group *group)
2705 {
2706 	struct spdk_nvmf_tcp_poll_group *tgroup;
2707 	struct nvme_tcp_qpair *tqpair, *tmp;
2708 	int rc;
2709 
2710 	tgroup = SPDK_CONTAINEROF(group, struct spdk_nvmf_tcp_poll_group, group);
2711 
2712 	if (spdk_unlikely(TAILQ_EMPTY(&tgroup->qpairs))) {
2713 		return 0;
2714 	}
2715 
2716 	rc = spdk_sock_group_poll(tgroup->sock_group);
2717 	if (rc < 0) {
2718 		SPDK_ERRLOG("Failed to poll sock_group=%p\n", tgroup->sock_group);
2719 		return rc;
2720 	}
2721 
2722 
2723 	TAILQ_FOREACH_SAFE(tqpair, &tgroup->qpairs, link, tmp) {
2724 		spdk_nvmf_tcp_qpair_handle_timout(tqpair);
2725 		if (tqpair->state == NVME_TCP_QPAIR_STATE_EXITING) {
2726 			/* to prevent the state is set again */
2727 			tqpair->state = NVME_TCP_QPAIR_STATE_EXITED;
2728 			SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "will disconect the tqpair=%p\n", tqpair);
2729 			spdk_nvmf_qpair_disconnect(&tqpair->qpair, NULL, NULL);
2730 		}
2731 	}
2732 
2733 	return 0;
2734 }
2735 
2736 static bool
2737 spdk_nvmf_tcp_qpair_is_idle(struct spdk_nvmf_qpair *qpair)
2738 {
2739 	struct nvme_tcp_qpair *tqpair;
2740 
2741 	tqpair = SPDK_CONTAINEROF(qpair, struct nvme_tcp_qpair, qpair);
2742 	if (tqpair->state_cntr[TCP_REQUEST_STATE_FREE] == tqpair->max_queue_depth) {
2743 		return true;
2744 	}
2745 
2746 	return false;
2747 }
2748 
2749 static int
2750 spdk_nvmf_tcp_qpair_get_trid(struct spdk_nvmf_qpair *qpair,
2751 			     struct spdk_nvme_transport_id *trid, bool peer)
2752 {
2753 	struct nvme_tcp_qpair     *tqpair;
2754 	uint16_t			port;
2755 
2756 	tqpair = SPDK_CONTAINEROF(qpair, struct nvme_tcp_qpair, qpair);
2757 	trid->trtype = SPDK_NVME_TRANSPORT_TCP;
2758 
2759 	if (peer) {
2760 		snprintf(trid->traddr, sizeof(trid->traddr), "%s", tqpair->initiator_addr);
2761 		port = tqpair->initiator_port;
2762 	} else {
2763 		snprintf(trid->traddr, sizeof(trid->traddr), "%s", tqpair->target_addr);
2764 		port = tqpair->target_port;
2765 	}
2766 
2767 	if (spdk_sock_is_ipv4(tqpair->sock)) {
2768 		trid->adrfam = SPDK_NVMF_ADRFAM_IPV4;
2769 	} else if (spdk_sock_is_ipv4(tqpair->sock)) {
2770 		trid->adrfam = SPDK_NVMF_ADRFAM_IPV6;
2771 	} else {
2772 		return -1;
2773 	}
2774 
2775 	snprintf(trid->trsvcid, sizeof(trid->trsvcid), "%d", port);
2776 	return 0;
2777 }
2778 
2779 static int
2780 spdk_nvmf_tcp_qpair_get_local_trid(struct spdk_nvmf_qpair *qpair,
2781 				   struct spdk_nvme_transport_id *trid)
2782 {
2783 	return spdk_nvmf_tcp_qpair_get_trid(qpair, trid, 0);
2784 }
2785 
2786 static int
2787 spdk_nvmf_tcp_qpair_get_peer_trid(struct spdk_nvmf_qpair *qpair,
2788 				  struct spdk_nvme_transport_id *trid)
2789 {
2790 	return spdk_nvmf_tcp_qpair_get_trid(qpair, trid, 1);
2791 }
2792 
2793 static int
2794 spdk_nvmf_tcp_qpair_get_listen_trid(struct spdk_nvmf_qpair *qpair,
2795 				    struct spdk_nvme_transport_id *trid)
2796 {
2797 	return spdk_nvmf_tcp_qpair_get_trid(qpair, trid, 0);
2798 }
2799 
2800 static int
2801 spdk_nvmf_tcp_qpair_set_sq_size(struct spdk_nvmf_qpair *qpair)
2802 {
2803 	struct nvme_tcp_qpair     *tqpair;
2804 	int rc;
2805 	tqpair = SPDK_CONTAINEROF(qpair, struct nvme_tcp_qpair, qpair);
2806 
2807 	rc = spdk_nvmf_tcp_qpair_init_mem_resource(tqpair, tqpair->qpair.sq_head_max);
2808 	if (!rc) {
2809 		tqpair->max_queue_depth += tqpair->qpair.sq_head_max;
2810 		tqpair->free_pdu_num += tqpair->qpair.sq_head_max;
2811 		tqpair->state_cntr[TCP_REQUEST_STATE_FREE] += tqpair->qpair.sq_head_max;
2812 		SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "The queue depth=%u for tqpair=%p\n",
2813 			      tqpair->max_queue_depth, tqpair);
2814 	}
2815 
2816 	return rc;
2817 
2818 }
2819 
2820 #define SPDK_NVMF_TCP_DEFAULT_MAX_QUEUE_DEPTH 128
2821 #define SPDK_NVMF_TCP_DEFAULT_AQ_DEPTH 128
2822 #define SPDK_NVMF_TCP_DEFAULT_MAX_QPAIRS_PER_CTRLR 64
2823 #define SPDK_NVMF_TCP_DEFAULT_IN_CAPSULE_DATA_SIZE 4096
2824 #define SPDK_NVMF_TCP_DEFAULT_MAX_IO_SIZE 131072
2825 #define SPDK_NVMF_TCP_DEFAULT_IO_UNIT_SIZE 131072
2826 
2827 static void
2828 spdk_nvmf_tcp_opts_init(struct spdk_nvmf_transport_opts *opts)
2829 {
2830 	opts->max_queue_depth =      SPDK_NVMF_TCP_DEFAULT_MAX_QUEUE_DEPTH;
2831 	opts->max_qpairs_per_ctrlr = SPDK_NVMF_TCP_DEFAULT_MAX_QPAIRS_PER_CTRLR;
2832 	opts->in_capsule_data_size = SPDK_NVMF_TCP_DEFAULT_IN_CAPSULE_DATA_SIZE;
2833 	opts->max_io_size =          SPDK_NVMF_TCP_DEFAULT_MAX_IO_SIZE;
2834 	opts->io_unit_size =         SPDK_NVMF_TCP_DEFAULT_IO_UNIT_SIZE;
2835 	opts->max_aq_depth =         SPDK_NVMF_TCP_DEFAULT_AQ_DEPTH;
2836 }
2837 
2838 const struct spdk_nvmf_transport_ops spdk_nvmf_transport_tcp = {
2839 	.type = SPDK_NVME_TRANSPORT_TCP,
2840 	.opts_init = spdk_nvmf_tcp_opts_init,
2841 	.create = spdk_nvmf_tcp_create,
2842 	.destroy = spdk_nvmf_tcp_destroy,
2843 
2844 	.listen = spdk_nvmf_tcp_listen,
2845 	.stop_listen = spdk_nvmf_tcp_stop_listen,
2846 	.accept = spdk_nvmf_tcp_accept,
2847 
2848 	.listener_discover = spdk_nvmf_tcp_discover,
2849 
2850 	.poll_group_create = spdk_nvmf_tcp_poll_group_create,
2851 	.poll_group_destroy = spdk_nvmf_tcp_poll_group_destroy,
2852 	.poll_group_add = spdk_nvmf_tcp_poll_group_add,
2853 	.poll_group_poll = spdk_nvmf_tcp_poll_group_poll,
2854 
2855 	.req_free = spdk_nvmf_tcp_req_free,
2856 	.req_complete = spdk_nvmf_tcp_req_complete,
2857 
2858 	.qpair_fini = spdk_nvmf_tcp_close_qpair,
2859 	.qpair_is_idle = spdk_nvmf_tcp_qpair_is_idle,
2860 	.qpair_get_local_trid = spdk_nvmf_tcp_qpair_get_local_trid,
2861 	.qpair_get_peer_trid = spdk_nvmf_tcp_qpair_get_peer_trid,
2862 	.qpair_get_listen_trid = spdk_nvmf_tcp_qpair_get_listen_trid,
2863 	.qpair_set_sqsize = spdk_nvmf_tcp_qpair_set_sq_size,
2864 };
2865 
2866 SPDK_LOG_REGISTER_COMPONENT("nvmf_tcp", SPDK_LOG_NVMF_TCP)
2867