xref: /spdk/lib/nvmf/tcp.c (revision fa2d95b3fe66e7f5c543eaef89fa00d4eaa0e6e7)
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 	rsp_pdu->data = (uint8_t *)rsp_pdu->hdr.raw + c2h_term_req_hdr_len;
1290 	copy_len = pdu->hdr.common.hlen;
1291 	if (copy_len > SPDK_NVME_TCP_TERM_REQ_ERROR_DATA_MAX_SIZE) {
1292 		copy_len = SPDK_NVME_TCP_TERM_REQ_ERROR_DATA_MAX_SIZE;
1293 	}
1294 
1295 	/* Copy the error info into the buffer */
1296 	memcpy((uint8_t *)rsp_pdu->data, pdu->hdr.raw, copy_len);
1297 	rsp_pdu->data_len = copy_len;
1298 
1299 	/* Contain the header of the wrong received pdu */
1300 	c2h_term_req->common.plen = c2h_term_req->common.hlen + copy_len;
1301 	spdk_nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_ERROR);
1302 	spdk_nvmf_tcp_qpair_write_pdu(tqpair, rsp_pdu, spdk_nvmf_tcp_send_c2h_term_req_complete, tqpair);
1303 }
1304 
1305 static void
1306 spdk_nvmf_tcp_capsule_cmd_hdr_handle(struct spdk_nvmf_tcp_transport *ttransport,
1307 				     struct spdk_nvmf_tcp_qpair *tqpair,
1308 				     struct nvme_tcp_pdu *pdu)
1309 {
1310 	struct spdk_nvmf_tcp_req *tcp_req;
1311 
1312 	tcp_req = spdk_nvmf_tcp_req_get(tqpair);
1313 	if (!tcp_req) {
1314 		SPDK_ERRLOG("Cannot allocate tcp_req\n");
1315 		tqpair->state = NVME_TCP_QPAIR_STATE_EXITING;
1316 		spdk_nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_ERROR);
1317 		return;
1318 	}
1319 
1320 	pdu->ctx = tcp_req;
1321 	spdk_nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_NEW);
1322 	spdk_nvmf_tcp_req_process(ttransport, tcp_req);
1323 	return;
1324 }
1325 
1326 static void
1327 spdk_nvmf_tcp_capsule_cmd_payload_handle(struct spdk_nvmf_tcp_transport *ttransport,
1328 		struct spdk_nvmf_tcp_qpair *tqpair,
1329 		struct nvme_tcp_pdu *pdu)
1330 {
1331 	struct spdk_nvmf_tcp_req *tcp_req;
1332 	struct spdk_nvme_tcp_cmd *capsule_cmd;
1333 	uint32_t error_offset = 0;
1334 	enum spdk_nvme_tcp_term_req_fes fes;
1335 
1336 	capsule_cmd = &pdu->hdr.capsule_cmd;
1337 	tcp_req = pdu->ctx;
1338 	assert(tcp_req != NULL);
1339 	if (capsule_cmd->common.pdo > SPDK_NVME_TCP_PDU_PDO_MAX_OFFSET) {
1340 		SPDK_ERRLOG("Expected ICReq capsule_cmd pdu offset <= %d, got %c\n",
1341 			    SPDK_NVME_TCP_PDU_PDO_MAX_OFFSET, capsule_cmd->common.pdo);
1342 		fes = SPDK_NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD;
1343 		error_offset = offsetof(struct spdk_nvme_tcp_common_pdu_hdr, pdo);
1344 		goto err;
1345 	}
1346 
1347 	spdk_nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_READY);
1348 	spdk_nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_READY_TO_EXECUTE);
1349 	spdk_nvmf_tcp_req_process(ttransport, tcp_req);
1350 
1351 	return;
1352 err:
1353 	spdk_nvmf_tcp_send_c2h_term_req(tqpair, pdu, fes, error_offset);
1354 }
1355 
1356 static void
1357 spdk_nvmf_tcp_h2c_data_hdr_handle(struct spdk_nvmf_tcp_transport *ttransport,
1358 				  struct spdk_nvmf_tcp_qpair *tqpair,
1359 				  struct nvme_tcp_pdu *pdu)
1360 {
1361 	struct spdk_nvmf_tcp_req *tcp_req;
1362 	uint32_t error_offset = 0;
1363 	enum spdk_nvme_tcp_term_req_fes fes = 0;
1364 	struct spdk_nvme_tcp_h2c_data_hdr *h2c_data;
1365 	uint32_t iov_index;
1366 	bool ttag_offset_error = false;
1367 
1368 	h2c_data = &pdu->hdr.h2c_data;
1369 
1370 	SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "tqpair=%p, r2t_info: datao=%u, datal=%u, cccid=%u, ttag=%u\n",
1371 		      tqpair, h2c_data->datao, h2c_data->datal, h2c_data->cccid, h2c_data->ttag);
1372 
1373 	/* According to the information in the pdu to find the req */
1374 	TAILQ_FOREACH(tcp_req, &tqpair->state_queue[TCP_REQUEST_STATE_TRANSFERRING_HOST_TO_CONTROLLER],
1375 		      state_link) {
1376 		if ((tcp_req->req.cmd->nvme_cmd.cid == h2c_data->cccid) && (tcp_req->ttag == h2c_data->ttag)) {
1377 			break;
1378 		}
1379 
1380 		if (!ttag_offset_error && (tcp_req->req.cmd->nvme_cmd.cid == h2c_data->cccid)) {
1381 			ttag_offset_error = true;
1382 		}
1383 	}
1384 
1385 	if (!tcp_req) {
1386 		SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "tcp_req is not found for tqpair=%p\n", tqpair);
1387 		fes = SPDK_NVME_TCP_TERM_REQ_FES_INVALID_DATA_UNSUPPORTED_PARAMETER;
1388 		if (!ttag_offset_error) {
1389 			error_offset = offsetof(struct spdk_nvme_tcp_h2c_data_hdr, cccid);
1390 		} else {
1391 			error_offset = offsetof(struct spdk_nvme_tcp_h2c_data_hdr, ttag);
1392 		}
1393 		goto err;
1394 	}
1395 
1396 	if (tcp_req->next_expected_r2t_offset != h2c_data->datao) {
1397 		SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP,
1398 			      "tcp_req(%p), tqpair=%p,  expected_r2t_offset=%u, but data offset =%u\n",
1399 			      tcp_req, tqpair, tcp_req->next_expected_r2t_offset, h2c_data->datao);
1400 		fes = SPDK_NVME_TCP_TERM_REQ_FES_DATA_TRANSFER_OUT_OF_RANGE;
1401 		goto err;
1402 	}
1403 
1404 	if (h2c_data->datal > tqpair->maxh2cdata) {
1405 		SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "tcp_req(%p), tqpair=%p,  datao=%u execeeds maxh2cdata size=%u\n",
1406 			      tcp_req, tqpair, h2c_data->datao, tqpair->maxh2cdata);
1407 		fes = SPDK_NVME_TCP_TERM_REQ_FES_DATA_TRANSFER_OUT_OF_RANGE;
1408 		goto err;
1409 	}
1410 
1411 	if ((h2c_data->datao + h2c_data->datal) > tcp_req->req.length) {
1412 		SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP,
1413 			      "tcp_req(%p), tqpair=%p,  (datao=%u + datal=%u) execeeds requested length=%u\n",
1414 			      tcp_req, tqpair, h2c_data->datao, h2c_data->datal, tcp_req->req.length);
1415 		fes = SPDK_NVME_TCP_TERM_REQ_FES_R2T_LIMIT_EXCEEDED;
1416 		goto err;
1417 	}
1418 
1419 	pdu->ctx = tcp_req;
1420 	pdu->data_len = h2c_data->datal;
1421 	iov_index = pdu->hdr.h2c_data.datao / ttransport->transport.opts.io_unit_size;
1422 	pdu->data = tcp_req->req.iov[iov_index].iov_base + (pdu->hdr.h2c_data.datao %
1423 			ttransport->transport.opts.io_unit_size);
1424 	spdk_nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_PAYLOAD);
1425 	return;
1426 
1427 err:
1428 	spdk_nvmf_tcp_send_c2h_term_req(tqpair, pdu, fes, error_offset);
1429 }
1430 
1431 static void
1432 spdk_nvmf_tcp_pdu_cmd_complete(void *cb_arg)
1433 {
1434 	struct spdk_nvmf_tcp_req *tcp_req = cb_arg;
1435 	nvmf_tcp_request_free(tcp_req);
1436 }
1437 
1438 static void
1439 spdk_nvmf_tcp_send_capsule_resp_pdu(struct spdk_nvmf_tcp_req *tcp_req,
1440 				    struct spdk_nvmf_tcp_qpair *tqpair)
1441 {
1442 	struct nvme_tcp_pdu *rsp_pdu;
1443 	struct spdk_nvme_tcp_rsp *capsule_resp;
1444 
1445 	SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "enter, tqpair=%p\n", tqpair);
1446 	rsp_pdu = spdk_nvmf_tcp_pdu_get(tqpair);
1447 	if (!rsp_pdu) {
1448 		spdk_nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_ERROR);
1449 		tqpair->state = NVME_TCP_QPAIR_STATE_EXITING;
1450 		return;
1451 	}
1452 
1453 	capsule_resp = &rsp_pdu->hdr.capsule_resp;
1454 	capsule_resp->common.pdu_type = SPDK_NVME_TCP_PDU_TYPE_CAPSULE_RESP;
1455 	capsule_resp->common.plen = capsule_resp->common.hlen = sizeof(*capsule_resp);
1456 	capsule_resp->rccqe = tcp_req->req.rsp->nvme_cpl;
1457 	if (tqpair->host_hdgst_enable) {
1458 		capsule_resp->common.flags |= SPDK_NVME_TCP_CH_FLAGS_HDGSTF;
1459 		capsule_resp->common.plen += SPDK_NVME_TCP_DIGEST_LEN;
1460 	}
1461 
1462 	spdk_nvmf_tcp_qpair_write_pdu(tqpair, rsp_pdu, spdk_nvmf_tcp_pdu_cmd_complete, tcp_req);
1463 }
1464 
1465 static void
1466 spdk_nvmf_tcp_pdu_c2h_data_complete(void *cb_arg)
1467 {
1468 	struct spdk_nvmf_tcp_req *tcp_req = cb_arg;
1469 	struct spdk_nvmf_tcp_qpair *tqpair = SPDK_CONTAINEROF(tcp_req->req.qpair,
1470 					     struct spdk_nvmf_tcp_qpair, qpair);
1471 
1472 	assert(tqpair != NULL);
1473 	assert(tcp_req->c2h_data_pdu_num > 0);
1474 	tcp_req->c2h_data_pdu_num--;
1475 	if (!tcp_req->c2h_data_pdu_num) {
1476 #if LINUX_KERNEL_SUPPORT_NOT_SENDING_RESP_FOR_C2H
1477 		nvmf_tcp_request_free(tcp_req);
1478 #else
1479 		spdk_nvmf_tcp_send_capsule_resp_pdu(tcp_req, tqpair);
1480 #endif
1481 	}
1482 
1483 	tqpair->c2h_data_pdu_cnt--;
1484 	spdk_nvmf_tcp_handle_pending_c2h_data_queue(tqpair);
1485 }
1486 
1487 static void
1488 spdk_nvmf_tcp_send_r2t_pdu(struct spdk_nvmf_tcp_qpair *tqpair,
1489 			   struct spdk_nvmf_tcp_req *tcp_req)
1490 {
1491 	struct nvme_tcp_pdu *rsp_pdu;
1492 	struct spdk_nvme_tcp_r2t_hdr *r2t;
1493 
1494 	rsp_pdu = spdk_nvmf_tcp_pdu_get(tqpair);
1495 	if (!rsp_pdu) {
1496 		tqpair->state = NVME_TCP_QPAIR_STATE_EXITING;
1497 		spdk_nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_ERROR);
1498 		return;
1499 	}
1500 
1501 	r2t = &rsp_pdu->hdr.r2t;
1502 	r2t->common.pdu_type = SPDK_NVME_TCP_PDU_TYPE_R2T;
1503 	r2t->common.plen = r2t->common.hlen = sizeof(*r2t);
1504 
1505 	if (tqpair->host_hdgst_enable) {
1506 		r2t->common.flags |= SPDK_NVME_TCP_CH_FLAGS_HDGSTF;
1507 		r2t->common.plen += SPDK_NVME_TCP_DIGEST_LEN;
1508 	}
1509 
1510 	r2t->cccid = tcp_req->req.cmd->nvme_cmd.cid;
1511 	r2t->ttag = tcp_req->ttag;
1512 	r2t->r2to = tcp_req->next_expected_r2t_offset;
1513 	r2t->r2tl = spdk_min(tcp_req->req.length - tcp_req->next_expected_r2t_offset, tqpair->maxh2cdata);
1514 	tcp_req->r2tl_remain = r2t->r2tl;
1515 
1516 	SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP,
1517 		      "tcp_req(%p) on tqpair(%p), r2t_info: cccid=%u, ttag=%u, r2to=%u, r2tl=%u\n",
1518 		      tcp_req, tqpair, r2t->cccid, r2t->ttag, r2t->r2to, r2t->r2tl);
1519 	spdk_nvmf_tcp_qpair_write_pdu(tqpair, rsp_pdu, spdk_nvmf_tcp_pdu_cmd_complete, NULL);
1520 }
1521 
1522 static void
1523 spdk_nvmf_tcp_handle_queued_r2t_req(struct spdk_nvmf_tcp_qpair *tqpair)
1524 {
1525 	struct spdk_nvmf_tcp_req *tcp_req, *req_tmp;
1526 
1527 	TAILQ_FOREACH_SAFE(tcp_req, &tqpair->state_queue[TCP_REQUEST_STATE_DATA_PENDING_FOR_R2T],
1528 			   state_link, req_tmp) {
1529 		if (tqpair->pending_r2t < tqpair->maxr2t) {
1530 			tqpair->pending_r2t++;
1531 			spdk_nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_TRANSFERRING_HOST_TO_CONTROLLER);
1532 			spdk_nvmf_tcp_send_r2t_pdu(tqpair, tcp_req);
1533 		} else {
1534 			break;
1535 		}
1536 	}
1537 }
1538 
1539 static void
1540 spdk_nvmf_tcp_h2c_data_payload_handle(struct spdk_nvmf_tcp_transport *ttransport,
1541 				      struct spdk_nvmf_tcp_qpair *tqpair,
1542 				      struct nvme_tcp_pdu *pdu)
1543 {
1544 	struct spdk_nvmf_tcp_req *tcp_req;
1545 
1546 	tcp_req = pdu->ctx;
1547 	assert(tcp_req != NULL);
1548 
1549 	SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "enter\n");
1550 
1551 	tcp_req->next_expected_r2t_offset += pdu->data_len;
1552 	tcp_req->r2tl_remain -= pdu->data_len;
1553 	spdk_nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_READY);
1554 
1555 	if (!tcp_req->r2tl_remain) {
1556 		if (tcp_req->next_expected_r2t_offset == tcp_req->req.length) {
1557 			assert(tqpair->pending_r2t > 0);
1558 			tqpair->pending_r2t--;
1559 			assert(tqpair->pending_r2t < tqpair->maxr2t);
1560 			spdk_nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_READY_TO_EXECUTE);
1561 			spdk_nvmf_tcp_req_process(ttransport, tcp_req);
1562 
1563 			spdk_nvmf_tcp_handle_queued_r2t_req(tqpair);
1564 		} else {
1565 			SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "Send r2t pdu for tcp_req=%p on tqpair=%p\n", tcp_req, tqpair);
1566 			spdk_nvmf_tcp_send_r2t_pdu(tqpair, tcp_req);
1567 		}
1568 	}
1569 }
1570 
1571 static void
1572 spdk_nvmf_tcp_h2c_term_req_dump(struct spdk_nvme_tcp_term_req_hdr *h2c_term_req)
1573 {
1574 	SPDK_ERRLOG("Error info of pdu(%p): %s\n", h2c_term_req,
1575 		    spdk_nvmf_tcp_term_req_fes_str[h2c_term_req->fes]);
1576 	if ((h2c_term_req->fes == SPDK_NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD) ||
1577 	    (h2c_term_req->fes == SPDK_NVME_TCP_TERM_REQ_FES_INVALID_DATA_UNSUPPORTED_PARAMETER)) {
1578 		SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "The offset from the start of the PDU header is %u\n",
1579 			      DGET32(h2c_term_req->fei));
1580 	}
1581 }
1582 
1583 static void
1584 spdk_nvmf_tcp_h2c_term_req_hdr_handle(struct spdk_nvmf_tcp_qpair *tqpair,
1585 				      struct nvme_tcp_pdu *pdu)
1586 {
1587 	struct spdk_nvme_tcp_term_req_hdr *h2c_term_req = &pdu->hdr.term_req;
1588 	uint32_t error_offset = 0;
1589 	enum spdk_nvme_tcp_term_req_fes fes;
1590 
1591 
1592 	if (h2c_term_req->fes > SPDK_NVME_TCP_TERM_REQ_FES_INVALID_DATA_UNSUPPORTED_PARAMETER) {
1593 		SPDK_ERRLOG("Fatal Error Stauts(FES) is unknown for h2c_term_req pdu=%p\n", pdu);
1594 		fes = SPDK_NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD;
1595 		error_offset = offsetof(struct spdk_nvme_tcp_term_req_hdr, fes);
1596 		goto end;
1597 	}
1598 
1599 	/* set the data buffer */
1600 	pdu->data = (uint8_t *)pdu->hdr.raw + h2c_term_req->common.hlen;
1601 	pdu->data_len = h2c_term_req->common.plen - h2c_term_req->common.hlen;
1602 	spdk_nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_PAYLOAD);
1603 	return;
1604 end:
1605 	spdk_nvmf_tcp_send_c2h_term_req(tqpair, pdu, fes, error_offset);
1606 	return;
1607 }
1608 
1609 static void
1610 spdk_nvmf_tcp_h2c_term_req_payload_handle(struct spdk_nvmf_tcp_qpair *tqpair,
1611 		struct nvme_tcp_pdu *pdu)
1612 {
1613 	struct spdk_nvme_tcp_term_req_hdr *h2c_term_req = &pdu->hdr.term_req;
1614 
1615 	spdk_nvmf_tcp_h2c_term_req_dump(h2c_term_req);
1616 	spdk_nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_ERROR);
1617 	return;
1618 }
1619 
1620 static void
1621 spdk_nvmf_tcp_pdu_payload_handle(struct spdk_nvmf_tcp_qpair *tqpair)
1622 {
1623 	int rc = 0;
1624 	struct nvme_tcp_pdu *pdu;
1625 	uint32_t crc32c, error_offset = 0;
1626 	enum spdk_nvme_tcp_term_req_fes fes;
1627 	struct spdk_nvmf_tcp_transport *ttransport;
1628 
1629 	assert(tqpair->recv_state == NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_PAYLOAD);
1630 	pdu = &tqpair->pdu_in_progress;
1631 
1632 	SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "enter\n");
1633 	/* check data digest if need */
1634 	if (pdu->ddgst_enable) {
1635 		crc32c = nvme_tcp_pdu_calc_data_digest(pdu);
1636 		rc = MATCH_DIGEST_WORD(pdu->data_digest, crc32c);
1637 		if (rc == 0) {
1638 			SPDK_ERRLOG("Data digest error on tqpair=(%p) with pdu=%p\n", tqpair, pdu);
1639 			fes = SPDK_NVME_TCP_TERM_REQ_FES_HDGST_ERROR;
1640 			spdk_nvmf_tcp_send_c2h_term_req(tqpair, pdu, fes, error_offset);
1641 			return;
1642 
1643 		}
1644 	}
1645 
1646 	ttransport = SPDK_CONTAINEROF(tqpair->qpair.transport, struct spdk_nvmf_tcp_transport, transport);
1647 	switch (pdu->hdr.common.pdu_type) {
1648 	case SPDK_NVME_TCP_PDU_TYPE_CAPSULE_CMD:
1649 		spdk_nvmf_tcp_capsule_cmd_payload_handle(ttransport, tqpair, pdu);
1650 		break;
1651 	case SPDK_NVME_TCP_PDU_TYPE_H2C_DATA:
1652 		spdk_nvmf_tcp_h2c_data_payload_handle(ttransport, tqpair, pdu);
1653 		break;
1654 
1655 	case SPDK_NVME_TCP_PDU_TYPE_H2C_TERM_REQ:
1656 		spdk_nvmf_tcp_h2c_term_req_payload_handle(tqpair, pdu);
1657 		break;
1658 
1659 	default:
1660 		/* The code should not go to here */
1661 		SPDK_ERRLOG("The code should not go to here\n");
1662 		break;
1663 	}
1664 }
1665 
1666 static void
1667 spdk_nvmf_tcp_send_icresp_complete(void *cb_arg)
1668 {
1669 	struct spdk_nvmf_tcp_qpair *tqpair = cb_arg;
1670 
1671 	tqpair->state = NVME_TCP_QPAIR_STATE_RUNNING;
1672 }
1673 
1674 static void
1675 spdk_nvmf_tcp_icreq_handle(struct spdk_nvmf_tcp_transport *ttransport,
1676 			   struct spdk_nvmf_tcp_qpair *tqpair,
1677 			   struct nvme_tcp_pdu *pdu)
1678 {
1679 	struct spdk_nvme_tcp_ic_req *ic_req = &pdu->hdr.ic_req;
1680 	struct nvme_tcp_pdu *rsp_pdu;
1681 	struct spdk_nvme_tcp_ic_resp *ic_resp;
1682 	uint32_t error_offset = 0;
1683 	enum spdk_nvme_tcp_term_req_fes fes;
1684 
1685 	/* Only PFV 0 is defined currently */
1686 	if (ic_req->pfv != 0) {
1687 		SPDK_ERRLOG("Expected ICReq PFV %u, got %u\n", 0u, ic_req->pfv);
1688 		fes = SPDK_NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD;
1689 		error_offset = offsetof(struct spdk_nvme_tcp_ic_req, pfv);
1690 		goto end;
1691 	}
1692 
1693 	/* MAXR2T is 0's based */
1694 	tqpair->maxr2t = ic_req->maxr2t + 1ull;
1695 	SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "maxr2t =%u\n", tqpair->maxr2t);
1696 
1697 	tqpair->host_hdgst_enable = ic_req->dgst.bits.hdgst_enable ? true : false;
1698 	tqpair->host_ddgst_enable = ic_req->dgst.bits.ddgst_enable ? true : false;
1699 
1700 	tqpair->cpda = spdk_min(ic_req->hpda, SPDK_NVME_TCP_CPDA_MAX);
1701 	SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "cpda of tqpair=(%p) is : %u\n", tqpair, tqpair->cpda);
1702 
1703 	rsp_pdu = spdk_nvmf_tcp_pdu_get(tqpair);
1704 	if (!rsp_pdu) {
1705 		tqpair->state = NVME_TCP_QPAIR_STATE_EXITING;
1706 		spdk_nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_ERROR);
1707 		return;
1708 	}
1709 
1710 	ic_resp = &rsp_pdu->hdr.ic_resp;
1711 	ic_resp->common.pdu_type = SPDK_NVME_TCP_PDU_TYPE_IC_RESP;
1712 	ic_resp->common.hlen = ic_resp->common.plen =  sizeof(*ic_resp);
1713 	ic_resp->pfv = 0;
1714 	ic_resp->cpda = tqpair->cpda;
1715 	tqpair->maxh2cdata = spdk_min(NVMF_TCP_PDU_MAX_H2C_DATA_SIZE,
1716 				      ttransport->transport.opts.io_unit_size);
1717 	ic_resp->maxh2cdata = tqpair->maxh2cdata;
1718 	ic_resp->dgst.bits.hdgst_enable = tqpair->host_hdgst_enable ? 1 : 0;
1719 	ic_resp->dgst.bits.ddgst_enable = tqpair->host_ddgst_enable ? 1 : 0;
1720 
1721 	SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "host_hdgst_enable: %u\n", tqpair->host_hdgst_enable);
1722 	SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "host_ddgst_enable: %u\n", tqpair->host_ddgst_enable);
1723 
1724 	spdk_nvmf_tcp_qpair_write_pdu(tqpair, rsp_pdu, spdk_nvmf_tcp_send_icresp_complete, tqpair);
1725 	spdk_nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_READY);
1726 	return;
1727 end:
1728 	spdk_nvmf_tcp_send_c2h_term_req(tqpair, pdu, fes, error_offset);
1729 	return;
1730 }
1731 
1732 static void
1733 spdk_nvmf_tcp_pdu_psh_handle(struct spdk_nvmf_tcp_qpair *tqpair)
1734 {
1735 	struct nvme_tcp_pdu *pdu;
1736 	int rc;
1737 	uint32_t crc32c, error_offset = 0;
1738 	enum spdk_nvme_tcp_term_req_fes fes;
1739 	struct spdk_nvmf_tcp_transport *ttransport;
1740 
1741 	assert(tqpair->recv_state == NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_PSH);
1742 	pdu = &tqpair->pdu_in_progress;
1743 
1744 	SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "pdu type of tqpair(%p) is %d\n", tqpair,
1745 		      pdu->hdr.common.pdu_type);
1746 	/* check header digest if needed */
1747 	if (pdu->has_hdgst) {
1748 		SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "Compare the header of pdu=%p on tqpair=%p\n", pdu, tqpair);
1749 		crc32c = nvme_tcp_pdu_calc_header_digest(pdu);
1750 		rc = MATCH_DIGEST_WORD((uint8_t *)pdu->hdr.raw + pdu->hdr.common.hlen, crc32c);
1751 		if (rc == 0) {
1752 			SPDK_ERRLOG("Header digest error on tqpair=(%p) with pdu=%p\n", tqpair, pdu);
1753 			fes = SPDK_NVME_TCP_TERM_REQ_FES_HDGST_ERROR;
1754 			spdk_nvmf_tcp_send_c2h_term_req(tqpair, pdu, fes, error_offset);
1755 			return;
1756 
1757 		}
1758 	}
1759 
1760 	ttransport = SPDK_CONTAINEROF(tqpair->qpair.transport, struct spdk_nvmf_tcp_transport, transport);
1761 	switch (pdu->hdr.common.pdu_type) {
1762 	case SPDK_NVME_TCP_PDU_TYPE_IC_REQ:
1763 		spdk_nvmf_tcp_icreq_handle(ttransport, tqpair, pdu);
1764 		break;
1765 	case SPDK_NVME_TCP_PDU_TYPE_CAPSULE_CMD:
1766 		spdk_nvmf_tcp_capsule_cmd_hdr_handle(ttransport, tqpair, pdu);
1767 		break;
1768 	case SPDK_NVME_TCP_PDU_TYPE_H2C_DATA:
1769 		spdk_nvmf_tcp_h2c_data_hdr_handle(ttransport, tqpair, pdu);
1770 		break;
1771 
1772 	case SPDK_NVME_TCP_PDU_TYPE_H2C_TERM_REQ:
1773 		spdk_nvmf_tcp_h2c_term_req_hdr_handle(tqpair, pdu);
1774 		break;
1775 
1776 	default:
1777 		SPDK_ERRLOG("Unexpected PDU type 0x%02x\n", tqpair->pdu_in_progress.hdr.common.pdu_type);
1778 		fes = SPDK_NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD;
1779 		error_offset = 1;
1780 		spdk_nvmf_tcp_send_c2h_term_req(tqpair, pdu, fes, error_offset);
1781 		break;
1782 	}
1783 }
1784 
1785 static void
1786 spdk_nvmf_tcp_pdu_ch_handle(struct spdk_nvmf_tcp_qpair *tqpair)
1787 {
1788 	struct nvme_tcp_pdu *pdu;
1789 	uint32_t error_offset = 0;
1790 	enum spdk_nvme_tcp_term_req_fes fes;
1791 	uint8_t expected_hlen, pdo;
1792 	bool plen_error = false, pdo_error = false;
1793 
1794 	assert(tqpair->recv_state == NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_CH);
1795 	pdu = &tqpair->pdu_in_progress;
1796 
1797 	if (pdu->hdr.common.pdu_type == SPDK_NVME_TCP_PDU_TYPE_IC_REQ) {
1798 		if (tqpair->state != NVME_TCP_QPAIR_STATE_INVALID) {
1799 			SPDK_ERRLOG("Already received ICreq PDU, and reject this pdu=%p\n", pdu);
1800 			fes = SPDK_NVME_TCP_TERM_REQ_FES_PDU_SEQUENCE_ERROR;
1801 			goto err;
1802 		}
1803 		expected_hlen = sizeof(struct spdk_nvme_tcp_ic_req);
1804 		if (pdu->hdr.common.plen != expected_hlen) {
1805 			plen_error = true;
1806 		}
1807 	} else {
1808 		if (tqpair->state != NVME_TCP_QPAIR_STATE_RUNNING) {
1809 			SPDK_ERRLOG("The TCP/IP connection is not negotitated\n");
1810 			fes = SPDK_NVME_TCP_TERM_REQ_FES_PDU_SEQUENCE_ERROR;
1811 			goto err;
1812 		}
1813 
1814 		switch (pdu->hdr.common.pdu_type) {
1815 		case SPDK_NVME_TCP_PDU_TYPE_CAPSULE_CMD:
1816 			expected_hlen = sizeof(struct spdk_nvme_tcp_cmd);
1817 			pdo = pdu->hdr.common.pdo;
1818 			if ((tqpair->cpda != 0) && (pdo != ((tqpair->cpda + 1) << 2))) {
1819 				pdo_error = true;
1820 				break;
1821 			}
1822 
1823 			if (pdu->hdr.common.plen < expected_hlen) {
1824 				plen_error = true;
1825 			}
1826 			break;
1827 		case SPDK_NVME_TCP_PDU_TYPE_H2C_DATA:
1828 			expected_hlen = sizeof(struct spdk_nvme_tcp_h2c_data_hdr);
1829 			pdo = pdu->hdr.common.pdo;
1830 			if ((tqpair->cpda != 0) && (pdo != ((tqpair->cpda + 1) << 2))) {
1831 				pdo_error = true;
1832 				break;
1833 			}
1834 			if (pdu->hdr.common.plen < expected_hlen) {
1835 				plen_error = true;
1836 			}
1837 			break;
1838 
1839 		case SPDK_NVME_TCP_PDU_TYPE_H2C_TERM_REQ:
1840 			expected_hlen = sizeof(struct spdk_nvme_tcp_term_req_hdr);
1841 			if ((pdu->hdr.common.plen <= expected_hlen) ||
1842 			    (pdu->hdr.common.plen > SPDK_NVME_TCP_TERM_REQ_PDU_MAX_SIZE)) {
1843 				plen_error = true;
1844 			}
1845 			break;
1846 
1847 		default:
1848 			SPDK_ERRLOG("Unexpected PDU type 0x%02x\n", pdu->hdr.common.pdu_type);
1849 			fes = SPDK_NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD;
1850 			error_offset = offsetof(struct spdk_nvme_tcp_common_pdu_hdr, pdu_type);
1851 			goto err;
1852 		}
1853 	}
1854 
1855 	if (pdu->hdr.common.hlen != expected_hlen) {
1856 		SPDK_ERRLOG("PDU type=0x%02x, Expected ICReq header length %u, got %u on tqpair=%p\n",
1857 			    pdu->hdr.common.pdu_type,
1858 			    expected_hlen, pdu->hdr.common.hlen, tqpair);
1859 		fes = SPDK_NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD;
1860 		error_offset = offsetof(struct spdk_nvme_tcp_common_pdu_hdr, hlen);
1861 		goto err;
1862 	} else if (pdo_error) {
1863 		fes = SPDK_NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD;
1864 		error_offset = offsetof(struct spdk_nvme_tcp_common_pdu_hdr, pdo);
1865 	} else if (plen_error) {
1866 		fes = SPDK_NVME_TCP_TERM_REQ_FES_INVALID_HEADER_FIELD;
1867 		error_offset = offsetof(struct spdk_nvme_tcp_common_pdu_hdr, plen);
1868 		goto err;
1869 	} else {
1870 		spdk_nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_PSH);
1871 		return;
1872 	}
1873 err:
1874 	spdk_nvmf_tcp_send_c2h_term_req(tqpair, pdu, fes, error_offset);
1875 }
1876 
1877 static int
1878 spdk_nvmf_tcp_sock_process(struct spdk_nvmf_tcp_qpair *tqpair)
1879 {
1880 	int rc = 0;
1881 	struct nvme_tcp_pdu *pdu;
1882 	enum nvme_tcp_pdu_recv_state prev_state;
1883 	uint32_t data_len;
1884 	uint8_t psh_len, pdo, hlen;
1885 	int8_t  padding_len;
1886 
1887 	/* The loop here is to allow for several back-to-back state changes. */
1888 	do {
1889 		prev_state = tqpair->recv_state;
1890 
1891 		SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "tqpair(%p) recv pdu entering state %d\n", tqpair, prev_state);
1892 
1893 		switch (tqpair->recv_state) {
1894 		/* Wait for the common header  */
1895 		case NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_READY:
1896 		case NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_CH:
1897 			pdu = &tqpair->pdu_in_progress;
1898 
1899 			rc = nvme_tcp_read_data(tqpair->sock,
1900 						sizeof(struct spdk_nvme_tcp_common_pdu_hdr) - pdu->ch_valid_bytes,
1901 						(void *)&pdu->hdr.common + pdu->ch_valid_bytes);
1902 			if (rc < 0) {
1903 				SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "will disconnect tqpair=%p\n", tqpair);
1904 				return NVME_TCP_PDU_FATAL;
1905 			} else if (rc > 0) {
1906 				pdu->ch_valid_bytes += rc;
1907 				if (spdk_likely(tqpair->recv_state == NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_READY)) {
1908 					spdk_nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_CH);
1909 				}
1910 			}
1911 
1912 			if (pdu->ch_valid_bytes < sizeof(struct spdk_nvme_tcp_common_pdu_hdr)) {
1913 				return NVME_TCP_PDU_IN_PROGRESS;
1914 			}
1915 
1916 			/* The command header of this PDU has now been read from the socket. */
1917 			spdk_nvmf_tcp_pdu_ch_handle(tqpair);
1918 			break;
1919 		/* Wait for the pdu specific header  */
1920 		case NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_PSH:
1921 			pdu = &tqpair->pdu_in_progress;
1922 			psh_len = hlen = pdu->hdr.common.hlen;
1923 			/* Only capsule_cmd and h2c_data has header digest */
1924 			if (((pdu->hdr.common.pdu_type == SPDK_NVME_TCP_PDU_TYPE_CAPSULE_CMD) ||
1925 			     (pdu->hdr.common.pdu_type == SPDK_NVME_TCP_PDU_TYPE_H2C_DATA)) &&
1926 			    tqpair->host_hdgst_enable) {
1927 				pdu->has_hdgst = true;
1928 				psh_len += SPDK_NVME_TCP_DIGEST_LEN;
1929 				if (pdu->hdr.common.plen > psh_len) {
1930 					pdo = pdu->hdr.common.pdo;
1931 					padding_len = pdo - psh_len;
1932 					SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "padding length is =%d for pdu=%p on tqpair=%p\n", padding_len,
1933 						      pdu, tqpair);
1934 					if (padding_len > 0) {
1935 						psh_len = pdo;
1936 					}
1937 				}
1938 			}
1939 
1940 			psh_len -= sizeof(struct spdk_nvme_tcp_common_pdu_hdr);
1941 			/* The following will read psh + hdgest (if possbile) + padding (if posssible) */
1942 			if (pdu->psh_valid_bytes < psh_len) {
1943 				rc = nvme_tcp_read_data(tqpair->sock,
1944 							psh_len - pdu->psh_valid_bytes,
1945 							(void *)&pdu->hdr.raw + sizeof(struct spdk_nvme_tcp_common_pdu_hdr) + pdu->psh_valid_bytes);
1946 				if (rc < 0) {
1947 					return NVME_TCP_PDU_FATAL;
1948 				}
1949 
1950 				pdu->psh_valid_bytes += rc;
1951 				if (pdu->psh_valid_bytes < psh_len) {
1952 					return NVME_TCP_PDU_IN_PROGRESS;
1953 				}
1954 			}
1955 
1956 			/* All header(ch, psh, head digist) of this PDU has now been read from the socket. */
1957 			spdk_nvmf_tcp_pdu_psh_handle(tqpair);
1958 			break;
1959 		case NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_PAYLOAD:
1960 			pdu = &tqpair->pdu_in_progress;
1961 
1962 			/* check whether the data is valid, if not we just return */
1963 			if (!pdu->data) {
1964 				return NVME_TCP_PDU_IN_PROGRESS;
1965 			}
1966 
1967 			data_len = pdu->data_len;
1968 			/* data digest */
1969 			if (spdk_unlikely((pdu->hdr.common.pdu_type != SPDK_NVME_TCP_PDU_TYPE_H2C_TERM_REQ) &&
1970 					  tqpair->host_ddgst_enable)) {
1971 				data_len += SPDK_NVME_TCP_DIGEST_LEN;
1972 				pdu->ddgst_enable = true;
1973 			}
1974 
1975 			rc = nvme_tcp_read_payload_data(tqpair->sock, pdu);
1976 			if (rc < 0) {
1977 				return NVME_TCP_PDU_IN_PROGRESS;
1978 			}
1979 
1980 			pdu->readv_offset += rc;
1981 			if (pdu->readv_offset < data_len) {
1982 				return NVME_TCP_PDU_IN_PROGRESS;
1983 			}
1984 
1985 			/* All of this PDU has now been read from the socket. */
1986 			spdk_nvmf_tcp_pdu_payload_handle(tqpair);
1987 			break;
1988 		case NVME_TCP_PDU_RECV_STATE_ERROR:
1989 			pdu = &tqpair->pdu_in_progress;
1990 			/* Check whether the connection is closed. Each time, we only read 1 byte every time */
1991 			rc = nvme_tcp_read_data(tqpair->sock, 1, (void *)&pdu->hdr.common);
1992 			if (rc < 0) {
1993 				return NVME_TCP_PDU_FATAL;
1994 			}
1995 			break;
1996 		default:
1997 			assert(0);
1998 			SPDK_ERRLOG("code should not come to here");
1999 			break;
2000 		}
2001 	} while (tqpair->recv_state != prev_state);
2002 
2003 	return rc;
2004 }
2005 
2006 static enum spdk_nvme_data_transfer
2007 spdk_nvmf_tcp_req_get_xfer(struct spdk_nvmf_tcp_req *tcp_req) {
2008 	enum spdk_nvme_data_transfer xfer;
2009 	struct spdk_nvme_cmd *cmd = &tcp_req->req.cmd->nvme_cmd;
2010 	struct spdk_nvme_sgl_descriptor *sgl = &cmd->dptr.sgl1;
2011 
2012 	/* Figure out data transfer direction */
2013 	if (cmd->opc == SPDK_NVME_OPC_FABRIC)
2014 	{
2015 		xfer = spdk_nvme_opc_get_data_transfer(tcp_req->req.cmd->nvmf_cmd.fctype);
2016 	} else
2017 	{
2018 		xfer = spdk_nvme_opc_get_data_transfer(cmd->opc);
2019 
2020 		/* Some admin commands are special cases */
2021 		if ((tcp_req->req.qpair->qid == 0) &&
2022 		    ((cmd->opc == SPDK_NVME_OPC_GET_FEATURES) ||
2023 		     (cmd->opc == SPDK_NVME_OPC_SET_FEATURES))) {
2024 			switch (cmd->cdw10 & 0xff) {
2025 			case SPDK_NVME_FEAT_LBA_RANGE_TYPE:
2026 			case SPDK_NVME_FEAT_AUTONOMOUS_POWER_STATE_TRANSITION:
2027 			case SPDK_NVME_FEAT_HOST_IDENTIFIER:
2028 				break;
2029 			default:
2030 				xfer = SPDK_NVME_DATA_NONE;
2031 			}
2032 		}
2033 	}
2034 
2035 	if (xfer == SPDK_NVME_DATA_NONE)
2036 	{
2037 		return xfer;
2038 	}
2039 
2040 	/* Even for commands that may transfer data, they could have specified 0 length.
2041 	 * We want those to show up with xfer SPDK_NVME_DATA_NONE.
2042 	 */
2043 	switch (sgl->generic.type)
2044 	{
2045 	case SPDK_NVME_SGL_TYPE_DATA_BLOCK:
2046 	case SPDK_NVME_SGL_TYPE_BIT_BUCKET:
2047 	case SPDK_NVME_SGL_TYPE_SEGMENT:
2048 	case SPDK_NVME_SGL_TYPE_LAST_SEGMENT:
2049 	case SPDK_NVME_SGL_TYPE_TRANSPORT_DATA_BLOCK:
2050 		if (sgl->unkeyed.length == 0) {
2051 			xfer = SPDK_NVME_DATA_NONE;
2052 		}
2053 		break;
2054 	case SPDK_NVME_SGL_TYPE_KEYED_DATA_BLOCK:
2055 		if (sgl->keyed.length == 0) {
2056 			xfer = SPDK_NVME_DATA_NONE;
2057 		}
2058 		break;
2059 	}
2060 
2061 	return xfer;
2062 }
2063 
2064 static void
2065 spdk_nvmf_tcp_request_free_buffers(struct spdk_nvmf_tcp_req *tcp_req,
2066 				   struct spdk_nvmf_transport_poll_group *group, struct spdk_nvmf_transport *transport)
2067 {
2068 	for (uint32_t i = 0; i < tcp_req->req.iovcnt; i++) {
2069 		assert(tcp_req->buffers[i] != NULL);
2070 		if (group->buf_cache_count < group->buf_cache_size) {
2071 			STAILQ_INSERT_HEAD(&group->buf_cache,
2072 					   (struct spdk_nvmf_transport_pg_cache_buf *)tcp_req->buffers[i], link);
2073 			group->buf_cache_count++;
2074 		} else {
2075 			spdk_mempool_put(transport->data_buf_pool, tcp_req->buffers[i]);
2076 		}
2077 		tcp_req->req.iov[i].iov_base = NULL;
2078 		tcp_req->buffers[i] = NULL;
2079 		tcp_req->req.iov[i].iov_len = 0;
2080 	}
2081 	tcp_req->data_from_pool = false;
2082 }
2083 
2084 static int
2085 spdk_nvmf_tcp_req_fill_iovs(struct spdk_nvmf_tcp_transport *ttransport,
2086 			    struct spdk_nvmf_tcp_req *tcp_req)
2087 {
2088 	void					*buf = NULL;
2089 	uint32_t				length = tcp_req->req.length;
2090 	uint32_t				i = 0;
2091 	struct spdk_nvmf_tcp_qpair		*tqpair;
2092 	struct spdk_nvmf_transport_poll_group	*group;
2093 
2094 	tqpair = SPDK_CONTAINEROF(tcp_req->req.qpair, struct spdk_nvmf_tcp_qpair, qpair);
2095 	group = &tqpair->group->group;
2096 
2097 	tcp_req->req.iovcnt = 0;
2098 	while (length) {
2099 		if (!(STAILQ_EMPTY(&group->buf_cache))) {
2100 			group->buf_cache_count--;
2101 			buf = STAILQ_FIRST(&group->buf_cache);
2102 			STAILQ_REMOVE_HEAD(&group->buf_cache, link);
2103 		} else {
2104 			buf = spdk_mempool_get(ttransport->transport.data_buf_pool);
2105 			if (!buf) {
2106 				goto nomem;
2107 			}
2108 		}
2109 
2110 		tcp_req->req.iov[i].iov_base = (void *)((uintptr_t)(buf + NVMF_DATA_BUFFER_MASK) &
2111 							~NVMF_DATA_BUFFER_MASK);
2112 		tcp_req->req.iov[i].iov_len  = spdk_min(length, ttransport->transport.opts.io_unit_size);
2113 		tcp_req->req.iovcnt++;
2114 		tcp_req->buffers[i] = buf;
2115 		length -= tcp_req->req.iov[i].iov_len;
2116 		i++;
2117 	}
2118 
2119 	assert(tcp_req->req.iovcnt < SPDK_NVMF_MAX_SGL_ENTRIES);
2120 	tcp_req->data_from_pool = true;
2121 	return 0;
2122 
2123 nomem:
2124 	spdk_nvmf_tcp_request_free_buffers(tcp_req, group, &ttransport->transport);
2125 	tcp_req->req.iovcnt = 0;
2126 	return -ENOMEM;
2127 }
2128 
2129 static int
2130 spdk_nvmf_tcp_req_parse_sgl(struct spdk_nvmf_tcp_transport *ttransport,
2131 			    struct spdk_nvmf_tcp_req *tcp_req)
2132 {
2133 	struct spdk_nvme_cmd			*cmd;
2134 	struct spdk_nvme_cpl			*rsp;
2135 	struct spdk_nvme_sgl_descriptor		*sgl;
2136 
2137 	cmd = &tcp_req->req.cmd->nvme_cmd;
2138 	rsp = &tcp_req->req.rsp->nvme_cpl;
2139 	sgl = &cmd->dptr.sgl1;
2140 
2141 	if (sgl->generic.type == SPDK_NVME_SGL_TYPE_TRANSPORT_DATA_BLOCK &&
2142 	    sgl->unkeyed.subtype == SPDK_NVME_SGL_SUBTYPE_TRANSPORT) {
2143 		if (sgl->unkeyed.length > ttransport->transport.opts.max_io_size) {
2144 			SPDK_ERRLOG("SGL length 0x%x exceeds max io size 0x%x\n",
2145 				    sgl->unkeyed.length, ttransport->transport.opts.max_io_size);
2146 			rsp->status.sc = SPDK_NVME_SC_DATA_SGL_LENGTH_INVALID;
2147 			return -1;
2148 		}
2149 
2150 		/* fill request length and populate iovs */
2151 		tcp_req->req.length = sgl->unkeyed.length;
2152 
2153 		SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "Data requested length= 0x%x\n",
2154 			      sgl->unkeyed.length);
2155 
2156 		if (spdk_nvmf_tcp_req_fill_iovs(ttransport, tcp_req) < 0) {
2157 			/* No available buffers. Queue this request up. */
2158 			SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "No available large data buffers. Queueing request %p\n", tcp_req);
2159 			return 0;
2160 		}
2161 
2162 		/* backward compatible */
2163 		tcp_req->req.data = tcp_req->req.iov[0].iov_base;
2164 
2165 
2166 		SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "Request %p took %d buffer/s from central pool, and data=%p\n",
2167 			      tcp_req,
2168 			      tcp_req->req.iovcnt, tcp_req->req.data);
2169 
2170 		return 0;
2171 	} else if (sgl->generic.type == SPDK_NVME_SGL_TYPE_DATA_BLOCK &&
2172 		   sgl->unkeyed.subtype == SPDK_NVME_SGL_SUBTYPE_OFFSET) {
2173 		uint64_t offset = sgl->address;
2174 		uint32_t max_len = ttransport->transport.opts.in_capsule_data_size;
2175 
2176 		SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "In-capsule data: offset 0x%" PRIx64 ", length 0x%x\n",
2177 			      offset, sgl->unkeyed.length);
2178 
2179 		if (offset > max_len) {
2180 			SPDK_ERRLOG("In-capsule offset 0x%" PRIx64 " exceeds capsule length 0x%x\n",
2181 				    offset, max_len);
2182 			rsp->status.sc = SPDK_NVME_SC_INVALID_SGL_OFFSET;
2183 			return -1;
2184 		}
2185 		max_len -= (uint32_t)offset;
2186 
2187 		if (sgl->unkeyed.length > max_len) {
2188 			SPDK_ERRLOG("In-capsule data length 0x%x exceeds capsule length 0x%x\n",
2189 				    sgl->unkeyed.length, max_len);
2190 			rsp->status.sc = SPDK_NVME_SC_DATA_SGL_LENGTH_INVALID;
2191 			return -1;
2192 		}
2193 
2194 		tcp_req->req.data = tcp_req->buf + offset;
2195 		tcp_req->data_from_pool = false;
2196 		tcp_req->req.length = sgl->unkeyed.length;
2197 
2198 		tcp_req->req.iov[0].iov_base = tcp_req->req.data;
2199 		tcp_req->req.iov[0].iov_len = tcp_req->req.length;
2200 		tcp_req->req.iovcnt = 1;
2201 
2202 		return 0;
2203 	}
2204 
2205 	SPDK_ERRLOG("Invalid NVMf I/O Command SGL:  Type 0x%x, Subtype 0x%x\n",
2206 		    sgl->generic.type, sgl->generic.subtype);
2207 	rsp->status.sc = SPDK_NVME_SC_SGL_DESCRIPTOR_TYPE_INVALID;
2208 	return -1;
2209 }
2210 
2211 static void
2212 spdk_nvmf_tcp_send_c2h_data(struct spdk_nvmf_tcp_qpair *tqpair,
2213 			    struct spdk_nvmf_tcp_req *tcp_req)
2214 {
2215 	struct nvme_tcp_pdu *rsp_pdu;
2216 	struct spdk_nvme_tcp_c2h_data_hdr *c2h_data;
2217 	uint32_t plen, pdo, alignment, offset, iov_index;
2218 
2219 	SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "enter\n");
2220 
2221 	/* always use the first iov_len, which is correct */
2222 	iov_index = tcp_req->c2h_data_offset / tcp_req->req.iov[0].iov_len;
2223 	offset = tcp_req->c2h_data_offset % tcp_req->req.iov[0].iov_len;
2224 
2225 	rsp_pdu = spdk_nvmf_tcp_pdu_get(tqpair);
2226 	assert(rsp_pdu != NULL);
2227 
2228 	c2h_data = &rsp_pdu->hdr.c2h_data;
2229 	c2h_data->common.pdu_type = SPDK_NVME_TCP_PDU_TYPE_C2H_DATA;
2230 	plen = c2h_data->common.hlen = sizeof(*c2h_data);
2231 
2232 	if (tqpair->host_hdgst_enable) {
2233 		plen += SPDK_NVME_TCP_DIGEST_LEN;
2234 		c2h_data->common.flags |= SPDK_NVME_TCP_CH_FLAGS_HDGSTF;
2235 	}
2236 
2237 	/* set the psh */
2238 	c2h_data->cccid = tcp_req->req.cmd->nvme_cmd.cid;
2239 	c2h_data->datal = spdk_min(NVMF_TCP_PDU_MAX_C2H_DATA_SIZE,
2240 				   (tcp_req->req.iov[iov_index].iov_len - offset));
2241 	c2h_data->datao = tcp_req->c2h_data_offset;
2242 
2243 	/* set the padding */
2244 	rsp_pdu->padding_len = 0;
2245 	pdo = plen;
2246 	if (tqpair->cpda) {
2247 		alignment = (tqpair->cpda + 1) << 2;
2248 		if (alignment > plen) {
2249 			rsp_pdu->padding_len = alignment - plen;
2250 			pdo = plen = alignment;
2251 		}
2252 	}
2253 
2254 	c2h_data->common.pdo = pdo;
2255 	plen += c2h_data->datal;
2256 	if (tqpair->host_ddgst_enable) {
2257 		c2h_data->common.flags |= SPDK_NVME_TCP_CH_FLAGS_DDGSTF;
2258 		plen += SPDK_NVME_TCP_DIGEST_LEN;
2259 	}
2260 
2261 	c2h_data->common.plen = plen;
2262 
2263 	rsp_pdu->data = tcp_req->req.iov[iov_index].iov_base + offset;
2264 	rsp_pdu->data_len = c2h_data->datal;
2265 
2266 	tcp_req->c2h_data_offset += c2h_data->datal;
2267 	if (iov_index == (tcp_req->req.iovcnt - 1) && (tcp_req->c2h_data_offset == tcp_req->req.length)) {
2268 		SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "Last pdu for tcp_req=%p on tqpair=%p\n", tcp_req, tqpair);
2269 		c2h_data->common.flags |= SPDK_NVME_TCP_C2H_DATA_FLAGS_LAST_PDU;
2270 		/* The linux kernel does not support this yet */
2271 #if LINUX_KERNEL_SUPPORT_NOT_SENDING_RESP_FOR_C2H
2272 		c2h_data->common.flags |= SPDK_NVME_TCP_C2H_DATA_FLAGS_SUCCESS;
2273 #endif
2274 		TAILQ_REMOVE(&tqpair->queued_c2h_data_tcp_req, tcp_req, link);
2275 	}
2276 
2277 	tqpair->c2h_data_pdu_cnt += 1;
2278 	spdk_nvmf_tcp_qpair_write_pdu(tqpair, rsp_pdu, spdk_nvmf_tcp_pdu_c2h_data_complete, tcp_req);
2279 }
2280 
2281 static int
2282 spdk_nvmf_tcp_calc_c2h_data_pdu_num(struct spdk_nvmf_tcp_req *tcp_req)
2283 {
2284 	uint32_t i, iov_cnt, pdu_num = 0;
2285 
2286 	iov_cnt = tcp_req->req.iovcnt;
2287 	for (i = 0; i < iov_cnt; i++) {
2288 		pdu_num += (tcp_req->req.iov[i].iov_len + NVMF_TCP_PDU_MAX_C2H_DATA_SIZE - 1) /
2289 			   NVMF_TCP_PDU_MAX_C2H_DATA_SIZE;
2290 	}
2291 
2292 	return pdu_num;
2293 }
2294 
2295 static void
2296 spdk_nvmf_tcp_handle_pending_c2h_data_queue(struct spdk_nvmf_tcp_qpair *tqpair)
2297 {
2298 	struct spdk_nvmf_tcp_req *tcp_req;
2299 
2300 	while (!TAILQ_EMPTY(&tqpair->queued_c2h_data_tcp_req) &&
2301 	       (tqpair->c2h_data_pdu_cnt < NVMF_TCP_QPAIR_MAX_C2H_PDU_NUM)) {
2302 		tcp_req = TAILQ_FIRST(&tqpair->queued_c2h_data_tcp_req);
2303 		spdk_nvmf_tcp_send_c2h_data(tqpair, tcp_req);
2304 	}
2305 }
2306 
2307 static void
2308 spdk_nvmf_tcp_queue_c2h_data(struct spdk_nvmf_tcp_req *tcp_req,
2309 			     struct spdk_nvmf_tcp_qpair *tqpair)
2310 {
2311 	tcp_req->c2h_data_pdu_num = spdk_nvmf_tcp_calc_c2h_data_pdu_num(tcp_req);
2312 
2313 	assert(tcp_req->c2h_data_pdu_num < NVMF_TCP_QPAIR_MAX_C2H_PDU_NUM);
2314 
2315 	TAILQ_INSERT_TAIL(&tqpair->queued_c2h_data_tcp_req, tcp_req, link);
2316 	spdk_nvmf_tcp_handle_pending_c2h_data_queue(tqpair);
2317 }
2318 
2319 static int
2320 request_transfer_out(struct spdk_nvmf_request *req)
2321 {
2322 	struct spdk_nvmf_tcp_req	*tcp_req;
2323 	struct spdk_nvmf_qpair		*qpair;
2324 	struct spdk_nvmf_tcp_qpair	*tqpair;
2325 	struct spdk_nvme_cpl		*rsp;
2326 
2327 	SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "enter\n");
2328 
2329 	qpair = req->qpair;
2330 	rsp = &req->rsp->nvme_cpl;
2331 	tcp_req = SPDK_CONTAINEROF(req, struct spdk_nvmf_tcp_req, req);
2332 
2333 	/* Advance our sq_head pointer */
2334 	if (qpair->sq_head == qpair->sq_head_max) {
2335 		qpair->sq_head = 0;
2336 	} else {
2337 		qpair->sq_head++;
2338 	}
2339 	rsp->sqhd = qpair->sq_head;
2340 
2341 	tqpair = SPDK_CONTAINEROF(tcp_req->req.qpair, struct spdk_nvmf_tcp_qpair, qpair);
2342 	spdk_nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_TRANSFERRING_CONTROLLER_TO_HOST);
2343 	if (rsp->status.sc == SPDK_NVME_SC_SUCCESS &&
2344 	    req->xfer == SPDK_NVME_DATA_CONTROLLER_TO_HOST) {
2345 		spdk_nvmf_tcp_queue_c2h_data(tcp_req, tqpair);
2346 	} else {
2347 		spdk_nvmf_tcp_send_capsule_resp_pdu(tcp_req, tqpair);
2348 	}
2349 
2350 	return 0;
2351 }
2352 
2353 static void
2354 spdk_nvmf_tcp_pdu_set_buf_from_req(struct spdk_nvmf_tcp_qpair *tqpair,
2355 				   struct spdk_nvmf_tcp_req *tcp_req)
2356 {
2357 	struct nvme_tcp_pdu *pdu;
2358 
2359 	if (tcp_req->data_from_pool) {
2360 		SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "Will send r2t for tcp_req(%p) on tqpair=%p\n", tcp_req, tqpair);
2361 		tcp_req->next_expected_r2t_offset = 0;
2362 		spdk_nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_DATA_PENDING_FOR_R2T);
2363 		spdk_nvmf_tcp_handle_queued_r2t_req(tqpair);
2364 	} else {
2365 		pdu = &tqpair->pdu_in_progress;
2366 		SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "Not need to send r2t for tcp_req(%p) on tqpair=%p\n", tcp_req,
2367 			      tqpair);
2368 		/* No need to send r2t, contained in the capsuled data */
2369 		pdu->data = tcp_req->req.data;
2370 		pdu->data_len = tcp_req->req.length;
2371 		spdk_nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_PAYLOAD);
2372 		spdk_nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_TRANSFERRING_HOST_TO_CONTROLLER);
2373 	}
2374 }
2375 
2376 static void
2377 spdk_nvmf_tcp_set_incapsule_data(struct spdk_nvmf_tcp_qpair *tqpair,
2378 				 struct spdk_nvmf_tcp_req *tcp_req)
2379 {
2380 	struct nvme_tcp_pdu *pdu;
2381 	uint32_t plen = 0;
2382 
2383 	pdu = &tqpair->pdu_in_progress;
2384 	plen = pdu->hdr.common.hlen;
2385 
2386 	if (tqpair->host_hdgst_enable) {
2387 		plen += SPDK_NVME_TCP_DIGEST_LEN;
2388 	}
2389 
2390 	if (pdu->hdr.common.plen != plen) {
2391 		tcp_req->has_incapsule_data = true;
2392 	}
2393 }
2394 
2395 static bool
2396 spdk_nvmf_tcp_req_process(struct spdk_nvmf_tcp_transport *ttransport,
2397 			  struct spdk_nvmf_tcp_req *tcp_req)
2398 {
2399 	struct spdk_nvmf_tcp_qpair		*tqpair;
2400 	struct spdk_nvme_cpl			*rsp = &tcp_req->req.rsp->nvme_cpl;
2401 	int					rc;
2402 	enum spdk_nvmf_tcp_req_state		prev_state;
2403 	bool					progress = false;
2404 	struct spdk_nvmf_transport_poll_group	*group;
2405 
2406 	tqpair = SPDK_CONTAINEROF(tcp_req->req.qpair, struct spdk_nvmf_tcp_qpair, qpair);
2407 	group = &tqpair->group->group;
2408 	assert(tcp_req->state != TCP_REQUEST_STATE_FREE);
2409 
2410 	/* The loop here is to allow for several back-to-back state changes. */
2411 	do {
2412 		prev_state = tcp_req->state;
2413 
2414 		SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "Request %p entering state %d on tqpair=%p\n", tcp_req, prev_state,
2415 			      tqpair);
2416 
2417 		switch (tcp_req->state) {
2418 		case TCP_REQUEST_STATE_FREE:
2419 			/* Some external code must kick a request into TCP_REQUEST_STATE_NEW
2420 			 * to escape this state. */
2421 			break;
2422 		case TCP_REQUEST_STATE_NEW:
2423 			spdk_trace_record(TRACE_TCP_REQUEST_STATE_NEW, 0, 0, (uintptr_t)tcp_req, 0);
2424 
2425 			/* copy the cmd from the receive pdu */
2426 			tcp_req->cmd = tqpair->pdu_in_progress.hdr.capsule_cmd.ccsqe;
2427 
2428 			/* The next state transition depends on the data transfer needs of this request. */
2429 			tcp_req->req.xfer = spdk_nvmf_tcp_req_get_xfer(tcp_req);
2430 
2431 			/* If no data to transfer, ready to execute. */
2432 			if (tcp_req->req.xfer == SPDK_NVME_DATA_NONE) {
2433 				/* Reset the tqpair receving pdu state */
2434 				spdk_nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_READY);
2435 				spdk_nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_READY_TO_EXECUTE);
2436 				break;
2437 			}
2438 
2439 			spdk_nvmf_tcp_set_incapsule_data(tqpair, tcp_req);
2440 
2441 			if (!tcp_req->has_incapsule_data) {
2442 				spdk_nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_READY);
2443 			}
2444 
2445 			spdk_nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_NEED_BUFFER);
2446 			TAILQ_INSERT_TAIL(&tqpair->group->pending_data_buf_queue, tcp_req, link);
2447 			break;
2448 		case TCP_REQUEST_STATE_NEED_BUFFER:
2449 			spdk_trace_record(TRACE_TCP_REQUEST_STATE_NEED_BUFFER, 0, 0, (uintptr_t)tcp_req, 0);
2450 
2451 			assert(tcp_req->req.xfer != SPDK_NVME_DATA_NONE);
2452 
2453 			if (!tcp_req->has_incapsule_data &&
2454 			    (tcp_req != TAILQ_FIRST(&tqpair->group->pending_data_buf_queue))) {
2455 				SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP,
2456 					      "Not the first element to wait for the buf for tcp_req(%p) on tqpair=%p\n",
2457 					      tcp_req, tqpair);
2458 				/* This request needs to wait in line to obtain a buffer */
2459 				break;
2460 			}
2461 
2462 			/* Try to get a data buffer */
2463 			rc = spdk_nvmf_tcp_req_parse_sgl(ttransport, tcp_req);
2464 			if (rc < 0) {
2465 				TAILQ_REMOVE(&tqpair->group->pending_data_buf_queue, tcp_req, link);
2466 				rsp->status.sc = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR;
2467 				/* Reset the tqpair receving pdu state */
2468 				spdk_nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_ERROR);
2469 				spdk_nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_READY_TO_COMPLETE);
2470 				break;
2471 			}
2472 
2473 			if (!tcp_req->req.data) {
2474 				SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "No buffer allocated for tcp_req(%p) on tqpair(%p\n)",
2475 					      tcp_req, tqpair);
2476 				/* No buffers available. */
2477 				break;
2478 			}
2479 
2480 			TAILQ_REMOVE(&tqpair->group->pending_data_buf_queue, tcp_req, link);
2481 
2482 			/* If data is transferring from host to controller, we need to do a transfer from the host. */
2483 			if (tcp_req->req.xfer == SPDK_NVME_DATA_HOST_TO_CONTROLLER) {
2484 				spdk_nvmf_tcp_pdu_set_buf_from_req(tqpair, tcp_req);
2485 				break;
2486 			}
2487 
2488 			spdk_nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_READY_TO_EXECUTE);
2489 			break;
2490 		case TCP_REQUEST_STATE_DATA_PENDING_FOR_R2T:
2491 			spdk_trace_record(TCP_REQUEST_STATE_DATA_PENDING_FOR_R2T, 0, 0,
2492 					  (uintptr_t)tcp_req, 0);
2493 			/* Some external code must kick a request into TCP_REQUEST_STATE_DATA_PENDING_R2T
2494 			 * to escape this state. */
2495 			break;
2496 
2497 		case TCP_REQUEST_STATE_TRANSFERRING_HOST_TO_CONTROLLER:
2498 			spdk_trace_record(TRACE_TCP_REQUEST_STATE_TRANSFERRING_HOST_TO_CONTROLLER, 0, 0,
2499 					  (uintptr_t)tcp_req, 0);
2500 			/* Some external code must kick a request into TCP_REQUEST_STATE_READY_TO_EXECUTE
2501 			 * to escape this state. */
2502 			break;
2503 		case TCP_REQUEST_STATE_READY_TO_EXECUTE:
2504 			spdk_trace_record(TRACE_TCP_REQUEST_STATE_READY_TO_EXECUTE, 0, 0, (uintptr_t)tcp_req, 0);
2505 			spdk_nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_EXECUTING);
2506 			spdk_nvmf_request_exec(&tcp_req->req);
2507 			break;
2508 		case TCP_REQUEST_STATE_EXECUTING:
2509 			spdk_trace_record(TRACE_TCP_REQUEST_STATE_EXECUTING, 0, 0, (uintptr_t)tcp_req, 0);
2510 			/* Some external code must kick a request into TCP_REQUEST_STATE_EXECUTED
2511 			 * to escape this state. */
2512 			break;
2513 		case TCP_REQUEST_STATE_EXECUTED:
2514 			spdk_trace_record(TRACE_TCP_REQUEST_STATE_EXECUTED, 0, 0, (uintptr_t)tcp_req, 0);
2515 			spdk_nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_READY_TO_COMPLETE);
2516 			break;
2517 		case TCP_REQUEST_STATE_READY_TO_COMPLETE:
2518 			spdk_trace_record(TRACE_TCP_REQUEST_STATE_READY_TO_COMPLETE, 0, 0, (uintptr_t)tcp_req, 0);
2519 			rc = request_transfer_out(&tcp_req->req);
2520 			assert(rc == 0); /* No good way to handle this currently */
2521 			break;
2522 		case TCP_REQUEST_STATE_TRANSFERRING_CONTROLLER_TO_HOST:
2523 			spdk_trace_record(TRACE_TCP_REQUEST_STATE_TRANSFERRING_CONTROLLER_TO_HOST, 0, 0,
2524 					  (uintptr_t)tcp_req,
2525 					  0);
2526 			/* Some external code must kick a request into TCP_REQUEST_STATE_COMPLETED
2527 			 * to escape this state. */
2528 			break;
2529 		case TCP_REQUEST_STATE_COMPLETED:
2530 			spdk_trace_record(TRACE_TCP_REQUEST_STATE_COMPLETED, 0, 0, (uintptr_t)tcp_req, 0);
2531 			if (tcp_req->data_from_pool) {
2532 				spdk_nvmf_tcp_request_free_buffers(tcp_req, group, &ttransport->transport);
2533 			}
2534 			tcp_req->req.length = 0;
2535 			tcp_req->req.iovcnt = 0;
2536 			tcp_req->req.data = NULL;
2537 			spdk_nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_FREE);
2538 			break;
2539 		case TCP_REQUEST_NUM_STATES:
2540 		default:
2541 			assert(0);
2542 			break;
2543 		}
2544 
2545 		if (tcp_req->state != prev_state) {
2546 			progress = true;
2547 		}
2548 	} while (tcp_req->state != prev_state);
2549 
2550 	return progress;
2551 }
2552 
2553 static void
2554 spdk_nvmf_tcp_qpair_process_pending(struct spdk_nvmf_tcp_transport *ttransport,
2555 				    struct spdk_nvmf_tcp_qpair *tqpair)
2556 {
2557 	struct spdk_nvmf_tcp_req *tcp_req, *req_tmp;
2558 
2559 	/* Tqpair is not in a good state, so return it */
2560 	if (spdk_unlikely(tqpair->recv_state == NVME_TCP_PDU_RECV_STATE_ERROR)) {
2561 		return;
2562 	}
2563 
2564 	spdk_nvmf_tcp_handle_queued_r2t_req(tqpair);
2565 
2566 	TAILQ_FOREACH_SAFE(tcp_req, &tqpair->group->pending_data_buf_queue, link, req_tmp) {
2567 		if (spdk_nvmf_tcp_req_process(ttransport, tcp_req) == false) {
2568 			break;
2569 		}
2570 	}
2571 }
2572 
2573 static void
2574 spdk_nvmf_tcp_sock_cb(void *arg, struct spdk_sock_group *group, struct spdk_sock *sock)
2575 {
2576 	struct spdk_nvmf_tcp_qpair *tqpair = arg;
2577 	struct spdk_nvmf_tcp_transport *ttransport;
2578 	int rc;
2579 
2580 	assert(tqpair != NULL);
2581 
2582 	ttransport = SPDK_CONTAINEROF(tqpair->qpair.transport, struct spdk_nvmf_tcp_transport, transport);
2583 	spdk_nvmf_tcp_qpair_process_pending(ttransport, tqpair);
2584 	rc = spdk_nvmf_tcp_sock_process(tqpair);
2585 
2586 	/* check the following two factors:
2587 	 * rc: The socket is closed
2588 	 * State of tqpair: The tqpair is in EXITING state due to internal error
2589 	 */
2590 	if ((rc < 0) || (tqpair->state == NVME_TCP_QPAIR_STATE_EXITING)) {
2591 		tqpair->state = NVME_TCP_QPAIR_STATE_EXITED;
2592 		spdk_nvmf_tcp_qpair_flush_pdus(tqpair);
2593 		SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "will disconect the tqpair=%p\n", tqpair);
2594 		spdk_poller_unregister(&tqpair->timeout_poller);
2595 		spdk_nvmf_qpair_disconnect(&tqpair->qpair, NULL, NULL);
2596 	}
2597 }
2598 
2599 static int
2600 spdk_nvmf_tcp_poll_group_add(struct spdk_nvmf_transport_poll_group *group,
2601 			     struct spdk_nvmf_qpair *qpair)
2602 {
2603 	struct spdk_nvmf_tcp_poll_group	*tgroup;
2604 	struct spdk_nvmf_tcp_qpair	*tqpair;
2605 	int				rc;
2606 
2607 	tgroup = SPDK_CONTAINEROF(group, struct spdk_nvmf_tcp_poll_group, group);
2608 	tqpair = SPDK_CONTAINEROF(qpair, struct spdk_nvmf_tcp_qpair, qpair);
2609 
2610 	rc = spdk_sock_group_add_sock(tgroup->sock_group, tqpair->sock,
2611 				      spdk_nvmf_tcp_sock_cb, tqpair);
2612 	if (rc != 0) {
2613 		SPDK_ERRLOG("Could not add sock to sock_group: %s (%d)\n",
2614 			    spdk_strerror(errno), errno);
2615 		spdk_nvmf_tcp_qpair_destroy(tqpair);
2616 		return -1;
2617 	}
2618 
2619 	rc =  spdk_nvmf_tcp_qpair_sock_init(tqpair);
2620 	if (rc != 0) {
2621 		SPDK_ERRLOG("Cannot set sock opt for tqpair=%p\n", tqpair);
2622 		spdk_nvmf_tcp_qpair_destroy(tqpair);
2623 		return -1;
2624 	}
2625 
2626 	rc = spdk_nvmf_tcp_qpair_init(&tqpair->qpair);
2627 	if (rc < 0) {
2628 		SPDK_ERRLOG("Cannot init tqpair=%p\n", tqpair);
2629 		spdk_nvmf_tcp_qpair_destroy(tqpair);
2630 		return -1;
2631 	}
2632 
2633 	rc = spdk_nvmf_tcp_qpair_init_mem_resource(tqpair, 1);
2634 	if (rc < 0) {
2635 		SPDK_ERRLOG("Cannot init memory resource info for tqpair=%p\n", tqpair);
2636 		spdk_nvmf_tcp_qpair_destroy(tqpair);
2637 		return -1;
2638 	}
2639 
2640 	tqpair->group = tgroup;
2641 	tqpair->state = NVME_TCP_QPAIR_STATE_INVALID;
2642 	TAILQ_INSERT_TAIL(&tgroup->qpairs, tqpair, link);
2643 
2644 	return 0;
2645 }
2646 
2647 static int
2648 spdk_nvmf_tcp_poll_group_remove(struct spdk_nvmf_transport_poll_group *group,
2649 				struct spdk_nvmf_qpair *qpair)
2650 {
2651 	struct spdk_nvmf_tcp_poll_group	*tgroup;
2652 	struct spdk_nvmf_tcp_qpair		*tqpair;
2653 	int				rc;
2654 
2655 	tgroup = SPDK_CONTAINEROF(group, struct spdk_nvmf_tcp_poll_group, group);
2656 	tqpair = SPDK_CONTAINEROF(qpair, struct spdk_nvmf_tcp_qpair, qpair);
2657 
2658 	assert(tqpair->group == tgroup);
2659 
2660 	SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "remove tqpair=%p from the tgroup=%p\n", tqpair, tgroup);
2661 	TAILQ_REMOVE(&tgroup->qpairs, tqpair, link);
2662 	rc = spdk_sock_group_remove_sock(tgroup->sock_group, tqpair->sock);
2663 	if (rc != 0) {
2664 		SPDK_ERRLOG("Could not remove sock from sock_group: %s (%d)\n",
2665 			    spdk_strerror(errno), errno);
2666 	}
2667 
2668 	return rc;
2669 }
2670 
2671 static int
2672 spdk_nvmf_tcp_req_complete(struct spdk_nvmf_request *req)
2673 {
2674 	struct spdk_nvmf_tcp_transport *ttransport;
2675 	struct spdk_nvmf_tcp_req *tcp_req;
2676 
2677 	ttransport = SPDK_CONTAINEROF(req->qpair->transport, struct spdk_nvmf_tcp_transport, transport);
2678 	tcp_req = SPDK_CONTAINEROF(req, struct spdk_nvmf_tcp_req, req);
2679 
2680 	spdk_nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_EXECUTED);
2681 	spdk_nvmf_tcp_req_process(ttransport, tcp_req);
2682 
2683 	return 0;
2684 }
2685 
2686 static void
2687 spdk_nvmf_tcp_close_qpair(struct spdk_nvmf_qpair *qpair)
2688 {
2689 	SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "enter\n");
2690 
2691 	spdk_nvmf_tcp_qpair_destroy(SPDK_CONTAINEROF(qpair, struct spdk_nvmf_tcp_qpair, qpair));
2692 }
2693 
2694 static int
2695 spdk_nvmf_tcp_poll_group_poll(struct spdk_nvmf_transport_poll_group *group)
2696 {
2697 	struct spdk_nvmf_tcp_poll_group *tgroup;
2698 	int rc;
2699 
2700 	tgroup = SPDK_CONTAINEROF(group, struct spdk_nvmf_tcp_poll_group, group);
2701 
2702 	if (spdk_unlikely(TAILQ_EMPTY(&tgroup->qpairs))) {
2703 		return 0;
2704 	}
2705 
2706 	rc = spdk_sock_group_poll(tgroup->sock_group);
2707 	if (rc < 0) {
2708 		SPDK_ERRLOG("Failed to poll sock_group=%p\n", tgroup->sock_group);
2709 		return rc;
2710 	}
2711 
2712 	return 0;
2713 }
2714 
2715 static int
2716 spdk_nvmf_tcp_qpair_get_trid(struct spdk_nvmf_qpair *qpair,
2717 			     struct spdk_nvme_transport_id *trid, bool peer)
2718 {
2719 	struct spdk_nvmf_tcp_qpair     *tqpair;
2720 	uint16_t			port;
2721 
2722 	tqpair = SPDK_CONTAINEROF(qpair, struct spdk_nvmf_tcp_qpair, qpair);
2723 	trid->trtype = SPDK_NVME_TRANSPORT_TCP;
2724 
2725 	if (peer) {
2726 		snprintf(trid->traddr, sizeof(trid->traddr), "%s", tqpair->initiator_addr);
2727 		port = tqpair->initiator_port;
2728 	} else {
2729 		snprintf(trid->traddr, sizeof(trid->traddr), "%s", tqpair->target_addr);
2730 		port = tqpair->target_port;
2731 	}
2732 
2733 	if (spdk_sock_is_ipv4(tqpair->sock)) {
2734 		trid->adrfam = SPDK_NVMF_ADRFAM_IPV4;
2735 	} else if (spdk_sock_is_ipv4(tqpair->sock)) {
2736 		trid->adrfam = SPDK_NVMF_ADRFAM_IPV6;
2737 	} else {
2738 		return -1;
2739 	}
2740 
2741 	snprintf(trid->trsvcid, sizeof(trid->trsvcid), "%d", port);
2742 	return 0;
2743 }
2744 
2745 static int
2746 spdk_nvmf_tcp_qpair_get_local_trid(struct spdk_nvmf_qpair *qpair,
2747 				   struct spdk_nvme_transport_id *trid)
2748 {
2749 	return spdk_nvmf_tcp_qpair_get_trid(qpair, trid, 0);
2750 }
2751 
2752 static int
2753 spdk_nvmf_tcp_qpair_get_peer_trid(struct spdk_nvmf_qpair *qpair,
2754 				  struct spdk_nvme_transport_id *trid)
2755 {
2756 	return spdk_nvmf_tcp_qpair_get_trid(qpair, trid, 1);
2757 }
2758 
2759 static int
2760 spdk_nvmf_tcp_qpair_get_listen_trid(struct spdk_nvmf_qpair *qpair,
2761 				    struct spdk_nvme_transport_id *trid)
2762 {
2763 	return spdk_nvmf_tcp_qpair_get_trid(qpair, trid, 0);
2764 }
2765 
2766 static int
2767 spdk_nvmf_tcp_qpair_set_sq_size(struct spdk_nvmf_qpair *qpair)
2768 {
2769 	struct spdk_nvmf_tcp_qpair     *tqpair;
2770 	int rc;
2771 	tqpair = SPDK_CONTAINEROF(qpair, struct spdk_nvmf_tcp_qpair, qpair);
2772 
2773 	rc = spdk_nvmf_tcp_qpair_init_mem_resource(tqpair, tqpair->qpair.sq_head_max);
2774 	if (!rc) {
2775 		tqpair->max_queue_depth += tqpair->qpair.sq_head_max;
2776 		tqpair->free_pdu_num += tqpair->qpair.sq_head_max;
2777 		tqpair->state_cntr[TCP_REQUEST_STATE_FREE] += tqpair->qpair.sq_head_max;
2778 		SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "The queue depth=%u for tqpair=%p\n",
2779 			      tqpair->max_queue_depth, tqpair);
2780 	}
2781 
2782 	return rc;
2783 
2784 }
2785 
2786 #define SPDK_NVMF_TCP_DEFAULT_MAX_QUEUE_DEPTH 128
2787 #define SPDK_NVMF_TCP_DEFAULT_AQ_DEPTH 128
2788 #define SPDK_NVMF_TCP_DEFAULT_MAX_QPAIRS_PER_CTRLR 64
2789 #define SPDK_NVMF_TCP_DEFAULT_IN_CAPSULE_DATA_SIZE 4096
2790 #define SPDK_NVMF_TCP_DEFAULT_MAX_IO_SIZE 131072
2791 #define SPDK_NVMF_TCP_DEFAULT_IO_UNIT_SIZE 131072
2792 #define SPDK_NVMF_TCP_DEFAULT_NUM_SHARED_BUFFERS 512
2793 #define SPDK_NVMF_TCP_DEFAULT_BUFFER_CACHE_SIZE 32
2794 
2795 static void
2796 spdk_nvmf_tcp_opts_init(struct spdk_nvmf_transport_opts *opts)
2797 {
2798 	opts->max_queue_depth =		SPDK_NVMF_TCP_DEFAULT_MAX_QUEUE_DEPTH;
2799 	opts->max_qpairs_per_ctrlr =	SPDK_NVMF_TCP_DEFAULT_MAX_QPAIRS_PER_CTRLR;
2800 	opts->in_capsule_data_size =	SPDK_NVMF_TCP_DEFAULT_IN_CAPSULE_DATA_SIZE;
2801 	opts->max_io_size =		SPDK_NVMF_TCP_DEFAULT_MAX_IO_SIZE;
2802 	opts->io_unit_size =		SPDK_NVMF_TCP_DEFAULT_IO_UNIT_SIZE;
2803 	opts->max_aq_depth =		SPDK_NVMF_TCP_DEFAULT_AQ_DEPTH;
2804 	opts->num_shared_buffers =	SPDK_NVMF_TCP_DEFAULT_NUM_SHARED_BUFFERS;
2805 	opts->buf_cache_size =		SPDK_NVMF_TCP_DEFAULT_BUFFER_CACHE_SIZE;
2806 }
2807 
2808 const struct spdk_nvmf_transport_ops spdk_nvmf_transport_tcp = {
2809 	.type = SPDK_NVME_TRANSPORT_TCP,
2810 	.opts_init = spdk_nvmf_tcp_opts_init,
2811 	.create = spdk_nvmf_tcp_create,
2812 	.destroy = spdk_nvmf_tcp_destroy,
2813 
2814 	.listen = spdk_nvmf_tcp_listen,
2815 	.stop_listen = spdk_nvmf_tcp_stop_listen,
2816 	.accept = spdk_nvmf_tcp_accept,
2817 
2818 	.listener_discover = spdk_nvmf_tcp_discover,
2819 
2820 	.poll_group_create = spdk_nvmf_tcp_poll_group_create,
2821 	.poll_group_destroy = spdk_nvmf_tcp_poll_group_destroy,
2822 	.poll_group_add = spdk_nvmf_tcp_poll_group_add,
2823 	.poll_group_remove = spdk_nvmf_tcp_poll_group_remove,
2824 	.poll_group_poll = spdk_nvmf_tcp_poll_group_poll,
2825 
2826 	.req_free = spdk_nvmf_tcp_req_free,
2827 	.req_complete = spdk_nvmf_tcp_req_complete,
2828 
2829 	.qpair_fini = spdk_nvmf_tcp_close_qpair,
2830 	.qpair_get_local_trid = spdk_nvmf_tcp_qpair_get_local_trid,
2831 	.qpair_get_peer_trid = spdk_nvmf_tcp_qpair_get_peer_trid,
2832 	.qpair_get_listen_trid = spdk_nvmf_tcp_qpair_get_listen_trid,
2833 	.qpair_set_sqsize = spdk_nvmf_tcp_qpair_set_sq_size,
2834 };
2835 
2836 SPDK_LOG_REGISTER_COMPONENT("nvmf_tcp", SPDK_LOG_NVMF_TCP)
2837