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