xref: /spdk/lib/nvmf/tcp.c (revision 1fc4165fe9bf8512483356ad8e6d27f793f2e3db)
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->ddigest_valid_bytes) {
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 len */
1969 			if (pdu->data_valid_bytes < data_len) {
1970 				rc = nvme_tcp_read_data(tqpair->sock, data_len - pdu->data_valid_bytes,
1971 							(void *)pdu->data + pdu->data_valid_bytes);
1972 				if (rc < 0) {
1973 					return NVME_TCP_PDU_FATAL;
1974 				}
1975 
1976 				pdu->data_valid_bytes += rc;
1977 				if (pdu->data_valid_bytes < data_len) {
1978 					return NVME_TCP_PDU_IN_PROGRESS;
1979 				}
1980 			}
1981 
1982 			/* data digest */
1983 			if ((pdu->hdr.common.pdu_type != SPDK_NVME_TCP_PDU_TYPE_H2C_TERM_REQ) &&
1984 			    tqpair->host_ddgst_enable && (pdu->ddigest_valid_bytes < SPDK_NVME_TCP_DIGEST_LEN)) {
1985 				rc = nvme_tcp_read_data(tqpair->sock,
1986 							SPDK_NVME_TCP_DIGEST_LEN - pdu->ddigest_valid_bytes,
1987 							pdu->data_digest + pdu->ddigest_valid_bytes);
1988 				if (rc < 0) {
1989 					return NVME_TCP_PDU_FATAL;
1990 				}
1991 
1992 				pdu->ddigest_valid_bytes += rc;
1993 				if (pdu->ddigest_valid_bytes < SPDK_NVME_TCP_DIGEST_LEN) {
1994 					return NVME_TCP_PDU_IN_PROGRESS;
1995 				}
1996 			}
1997 
1998 			/* All of this PDU has now been read from the socket. */
1999 			spdk_nvmf_tcp_pdu_payload_handle(tqpair);
2000 			break;
2001 		case NVME_TCP_PDU_RECV_STATE_ERROR:
2002 			pdu = &tqpair->pdu_in_progress;
2003 			/* Check whether the connection is closed. Each time, we only read 1 byte every time */
2004 			rc = nvme_tcp_read_data(tqpair->sock, 1, (void *)&pdu->hdr.common);
2005 			if (rc < 0) {
2006 				return NVME_TCP_PDU_FATAL;
2007 			}
2008 			break;
2009 		default:
2010 			assert(0);
2011 			SPDK_ERRLOG("code should not come to here");
2012 			break;
2013 		}
2014 	} while (tqpair->recv_state != prev_state);
2015 
2016 	return rc;
2017 }
2018 
2019 static enum spdk_nvme_data_transfer
2020 spdk_nvmf_tcp_req_get_xfer(struct spdk_nvmf_tcp_req *tcp_req) {
2021 	enum spdk_nvme_data_transfer xfer;
2022 	struct spdk_nvme_cmd *cmd = &tcp_req->req.cmd->nvme_cmd;
2023 	struct spdk_nvme_sgl_descriptor *sgl = &cmd->dptr.sgl1;
2024 
2025 	/* Figure out data transfer direction */
2026 	if (cmd->opc == SPDK_NVME_OPC_FABRIC)
2027 	{
2028 		xfer = spdk_nvme_opc_get_data_transfer(tcp_req->req.cmd->nvmf_cmd.fctype);
2029 	} else
2030 	{
2031 		xfer = spdk_nvme_opc_get_data_transfer(cmd->opc);
2032 
2033 		/* Some admin commands are special cases */
2034 		if ((tcp_req->req.qpair->qid == 0) &&
2035 		    ((cmd->opc == SPDK_NVME_OPC_GET_FEATURES) ||
2036 		     (cmd->opc == SPDK_NVME_OPC_SET_FEATURES))) {
2037 			switch (cmd->cdw10 & 0xff) {
2038 			case SPDK_NVME_FEAT_LBA_RANGE_TYPE:
2039 			case SPDK_NVME_FEAT_AUTONOMOUS_POWER_STATE_TRANSITION:
2040 			case SPDK_NVME_FEAT_HOST_IDENTIFIER:
2041 				break;
2042 			default:
2043 				xfer = SPDK_NVME_DATA_NONE;
2044 			}
2045 		}
2046 	}
2047 
2048 	if (xfer == SPDK_NVME_DATA_NONE)
2049 	{
2050 		return xfer;
2051 	}
2052 
2053 	/* Even for commands that may transfer data, they could have specified 0 length.
2054 	 * We want those to show up with xfer SPDK_NVME_DATA_NONE.
2055 	 */
2056 	switch (sgl->generic.type)
2057 	{
2058 	case SPDK_NVME_SGL_TYPE_DATA_BLOCK:
2059 	case SPDK_NVME_SGL_TYPE_BIT_BUCKET:
2060 	case SPDK_NVME_SGL_TYPE_SEGMENT:
2061 	case SPDK_NVME_SGL_TYPE_LAST_SEGMENT:
2062 	case SPDK_NVME_SGL_TYPE_TRANSPORT_DATA_BLOCK:
2063 		if (sgl->unkeyed.length == 0) {
2064 			xfer = SPDK_NVME_DATA_NONE;
2065 		}
2066 		break;
2067 	case SPDK_NVME_SGL_TYPE_KEYED_DATA_BLOCK:
2068 		if (sgl->keyed.length == 0) {
2069 			xfer = SPDK_NVME_DATA_NONE;
2070 		}
2071 		break;
2072 	}
2073 
2074 	return xfer;
2075 }
2076 
2077 static void
2078 spdk_nvmf_tcp_request_free_buffers(struct spdk_nvmf_tcp_req *tcp_req,
2079 				   struct spdk_nvmf_transport_poll_group *group, struct spdk_nvmf_transport *transport)
2080 {
2081 	for (uint32_t i = 0; i < tcp_req->req.iovcnt; i++) {
2082 		assert(tcp_req->buffers[i] != NULL);
2083 		if (group->buf_cache_count < group->buf_cache_size) {
2084 			STAILQ_INSERT_HEAD(&group->buf_cache,
2085 					   (struct spdk_nvmf_transport_pg_cache_buf *)tcp_req->buffers[i], link);
2086 			group->buf_cache_count++;
2087 		} else {
2088 			spdk_mempool_put(transport->data_buf_pool, tcp_req->buffers[i]);
2089 		}
2090 		tcp_req->req.iov[i].iov_base = NULL;
2091 		tcp_req->buffers[i] = NULL;
2092 		tcp_req->req.iov[i].iov_len = 0;
2093 	}
2094 	tcp_req->data_from_pool = false;
2095 }
2096 
2097 static int
2098 spdk_nvmf_tcp_req_fill_iovs(struct spdk_nvmf_tcp_transport *ttransport,
2099 			    struct spdk_nvmf_tcp_req *tcp_req)
2100 {
2101 	void					*buf = NULL;
2102 	uint32_t				length = tcp_req->req.length;
2103 	uint32_t				i = 0;
2104 	struct spdk_nvmf_tcp_qpair		*tqpair;
2105 	struct spdk_nvmf_transport_poll_group	*group;
2106 
2107 	tqpair = SPDK_CONTAINEROF(tcp_req->req.qpair, struct spdk_nvmf_tcp_qpair, qpair);
2108 	group = &tqpair->group->group;
2109 
2110 	tcp_req->req.iovcnt = 0;
2111 	while (length) {
2112 		if (!(STAILQ_EMPTY(&group->buf_cache))) {
2113 			group->buf_cache_count--;
2114 			buf = STAILQ_FIRST(&group->buf_cache);
2115 			STAILQ_REMOVE_HEAD(&group->buf_cache, link);
2116 		} else {
2117 			buf = spdk_mempool_get(ttransport->transport.data_buf_pool);
2118 			if (!buf) {
2119 				goto nomem;
2120 			}
2121 		}
2122 
2123 		tcp_req->req.iov[i].iov_base = (void *)((uintptr_t)(buf + NVMF_DATA_BUFFER_MASK) &
2124 							~NVMF_DATA_BUFFER_MASK);
2125 		tcp_req->req.iov[i].iov_len  = spdk_min(length, ttransport->transport.opts.io_unit_size);
2126 		tcp_req->req.iovcnt++;
2127 		tcp_req->buffers[i] = buf;
2128 		length -= tcp_req->req.iov[i].iov_len;
2129 		i++;
2130 	}
2131 
2132 	assert(tcp_req->req.iovcnt < SPDK_NVMF_MAX_SGL_ENTRIES);
2133 	tcp_req->data_from_pool = true;
2134 	return 0;
2135 
2136 nomem:
2137 	spdk_nvmf_tcp_request_free_buffers(tcp_req, group, &ttransport->transport);
2138 	tcp_req->req.iovcnt = 0;
2139 	return -ENOMEM;
2140 }
2141 
2142 static int
2143 spdk_nvmf_tcp_req_parse_sgl(struct spdk_nvmf_tcp_transport *ttransport,
2144 			    struct spdk_nvmf_tcp_req *tcp_req)
2145 {
2146 	struct spdk_nvme_cmd			*cmd;
2147 	struct spdk_nvme_cpl			*rsp;
2148 	struct spdk_nvme_sgl_descriptor		*sgl;
2149 
2150 	cmd = &tcp_req->req.cmd->nvme_cmd;
2151 	rsp = &tcp_req->req.rsp->nvme_cpl;
2152 	sgl = &cmd->dptr.sgl1;
2153 
2154 	if (sgl->generic.type == SPDK_NVME_SGL_TYPE_TRANSPORT_DATA_BLOCK &&
2155 	    sgl->unkeyed.subtype == SPDK_NVME_SGL_SUBTYPE_TRANSPORT) {
2156 		if (sgl->unkeyed.length > ttransport->transport.opts.max_io_size) {
2157 			SPDK_ERRLOG("SGL length 0x%x exceeds max io size 0x%x\n",
2158 				    sgl->unkeyed.length, ttransport->transport.opts.max_io_size);
2159 			rsp->status.sc = SPDK_NVME_SC_DATA_SGL_LENGTH_INVALID;
2160 			return -1;
2161 		}
2162 
2163 		/* fill request length and populate iovs */
2164 		tcp_req->req.length = sgl->unkeyed.length;
2165 
2166 		SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "Data requested length= 0x%x\n",
2167 			      sgl->unkeyed.length);
2168 
2169 		if (spdk_nvmf_tcp_req_fill_iovs(ttransport, tcp_req) < 0) {
2170 			/* No available buffers. Queue this request up. */
2171 			SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "No available large data buffers. Queueing request %p\n", tcp_req);
2172 			return 0;
2173 		}
2174 
2175 		/* backward compatible */
2176 		tcp_req->req.data = tcp_req->req.iov[0].iov_base;
2177 
2178 
2179 		SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "Request %p took %d buffer/s from central pool, and data=%p\n",
2180 			      tcp_req,
2181 			      tcp_req->req.iovcnt, tcp_req->req.data);
2182 
2183 		return 0;
2184 	} else if (sgl->generic.type == SPDK_NVME_SGL_TYPE_DATA_BLOCK &&
2185 		   sgl->unkeyed.subtype == SPDK_NVME_SGL_SUBTYPE_OFFSET) {
2186 		uint64_t offset = sgl->address;
2187 		uint32_t max_len = ttransport->transport.opts.in_capsule_data_size;
2188 
2189 		SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "In-capsule data: offset 0x%" PRIx64 ", length 0x%x\n",
2190 			      offset, sgl->unkeyed.length);
2191 
2192 		if (offset > max_len) {
2193 			SPDK_ERRLOG("In-capsule offset 0x%" PRIx64 " exceeds capsule length 0x%x\n",
2194 				    offset, max_len);
2195 			rsp->status.sc = SPDK_NVME_SC_INVALID_SGL_OFFSET;
2196 			return -1;
2197 		}
2198 		max_len -= (uint32_t)offset;
2199 
2200 		if (sgl->unkeyed.length > max_len) {
2201 			SPDK_ERRLOG("In-capsule data length 0x%x exceeds capsule length 0x%x\n",
2202 				    sgl->unkeyed.length, max_len);
2203 			rsp->status.sc = SPDK_NVME_SC_DATA_SGL_LENGTH_INVALID;
2204 			return -1;
2205 		}
2206 
2207 		tcp_req->req.data = tcp_req->buf + offset;
2208 		tcp_req->data_from_pool = false;
2209 		tcp_req->req.length = sgl->unkeyed.length;
2210 
2211 		tcp_req->req.iov[0].iov_base = tcp_req->req.data;
2212 		tcp_req->req.iov[0].iov_len = tcp_req->req.length;
2213 		tcp_req->req.iovcnt = 1;
2214 
2215 		return 0;
2216 	}
2217 
2218 	SPDK_ERRLOG("Invalid NVMf I/O Command SGL:  Type 0x%x, Subtype 0x%x\n",
2219 		    sgl->generic.type, sgl->generic.subtype);
2220 	rsp->status.sc = SPDK_NVME_SC_SGL_DESCRIPTOR_TYPE_INVALID;
2221 	return -1;
2222 }
2223 
2224 static void
2225 spdk_nvmf_tcp_send_c2h_data(struct spdk_nvmf_tcp_qpair *tqpair,
2226 			    struct spdk_nvmf_tcp_req *tcp_req)
2227 {
2228 	struct nvme_tcp_pdu *rsp_pdu;
2229 	struct spdk_nvme_tcp_c2h_data_hdr *c2h_data;
2230 	uint32_t plen, pdo, alignment, offset, iov_index;
2231 
2232 	SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "enter\n");
2233 
2234 	/* always use the first iov_len, which is correct */
2235 	iov_index = tcp_req->c2h_data_offset / tcp_req->req.iov[0].iov_len;
2236 	offset = tcp_req->c2h_data_offset % tcp_req->req.iov[0].iov_len;
2237 
2238 	rsp_pdu = spdk_nvmf_tcp_pdu_get(tqpair);
2239 	assert(rsp_pdu != NULL);
2240 
2241 	c2h_data = &rsp_pdu->hdr.c2h_data;
2242 	c2h_data->common.pdu_type = SPDK_NVME_TCP_PDU_TYPE_C2H_DATA;
2243 	plen = c2h_data->common.hlen = sizeof(*c2h_data);
2244 
2245 	if (tqpair->host_hdgst_enable) {
2246 		plen += SPDK_NVME_TCP_DIGEST_LEN;
2247 		c2h_data->common.flags |= SPDK_NVME_TCP_CH_FLAGS_HDGSTF;
2248 	}
2249 
2250 	/* set the psh */
2251 	c2h_data->cccid = tcp_req->req.cmd->nvme_cmd.cid;
2252 	c2h_data->datal = spdk_min(NVMF_TCP_PDU_MAX_C2H_DATA_SIZE,
2253 				   (tcp_req->req.iov[iov_index].iov_len - offset));
2254 	c2h_data->datao = tcp_req->c2h_data_offset;
2255 
2256 	/* set the padding */
2257 	rsp_pdu->padding_len = 0;
2258 	pdo = plen;
2259 	if (tqpair->cpda) {
2260 		alignment = (tqpair->cpda + 1) << 2;
2261 		if (alignment > plen) {
2262 			rsp_pdu->padding_len = alignment - plen;
2263 			pdo = plen = alignment;
2264 		}
2265 	}
2266 
2267 	c2h_data->common.pdo = pdo;
2268 	plen += c2h_data->datal;
2269 	if (tqpair->host_ddgst_enable) {
2270 		c2h_data->common.flags |= SPDK_NVME_TCP_CH_FLAGS_DDGSTF;
2271 		plen += SPDK_NVME_TCP_DIGEST_LEN;
2272 	}
2273 
2274 	c2h_data->common.plen = plen;
2275 
2276 	rsp_pdu->data = tcp_req->req.iov[iov_index].iov_base + offset;
2277 	rsp_pdu->data_len = c2h_data->datal;
2278 
2279 	tcp_req->c2h_data_offset += c2h_data->datal;
2280 	if (iov_index == (tcp_req->req.iovcnt - 1) && (tcp_req->c2h_data_offset == tcp_req->req.length)) {
2281 		SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "Last pdu for tcp_req=%p on tqpair=%p\n", tcp_req, tqpair);
2282 		c2h_data->common.flags |= SPDK_NVME_TCP_C2H_DATA_FLAGS_LAST_PDU;
2283 		/* The linux kernel does not support this yet */
2284 #if LINUX_KERNEL_SUPPORT_NOT_SENDING_RESP_FOR_C2H
2285 		c2h_data->common.flags |= SPDK_NVME_TCP_C2H_DATA_FLAGS_SUCCESS;
2286 #endif
2287 		TAILQ_REMOVE(&tqpair->queued_c2h_data_tcp_req, tcp_req, link);
2288 	}
2289 
2290 	tqpair->c2h_data_pdu_cnt += 1;
2291 	spdk_nvmf_tcp_qpair_write_pdu(tqpair, rsp_pdu, spdk_nvmf_tcp_pdu_c2h_data_complete, tcp_req);
2292 }
2293 
2294 static int
2295 spdk_nvmf_tcp_calc_c2h_data_pdu_num(struct spdk_nvmf_tcp_req *tcp_req)
2296 {
2297 	uint32_t i, iov_cnt, pdu_num = 0;
2298 
2299 	iov_cnt = tcp_req->req.iovcnt;
2300 	for (i = 0; i < iov_cnt; i++) {
2301 		pdu_num += (tcp_req->req.iov[i].iov_len + NVMF_TCP_PDU_MAX_C2H_DATA_SIZE - 1) /
2302 			   NVMF_TCP_PDU_MAX_C2H_DATA_SIZE;
2303 	}
2304 
2305 	return pdu_num;
2306 }
2307 
2308 static void
2309 spdk_nvmf_tcp_handle_pending_c2h_data_queue(struct spdk_nvmf_tcp_qpair *tqpair)
2310 {
2311 	struct spdk_nvmf_tcp_req *tcp_req;
2312 
2313 	while (!TAILQ_EMPTY(&tqpair->queued_c2h_data_tcp_req) &&
2314 	       (tqpair->c2h_data_pdu_cnt < NVMF_TCP_QPAIR_MAX_C2H_PDU_NUM)) {
2315 		tcp_req = TAILQ_FIRST(&tqpair->queued_c2h_data_tcp_req);
2316 		spdk_nvmf_tcp_send_c2h_data(tqpair, tcp_req);
2317 	}
2318 }
2319 
2320 static void
2321 spdk_nvmf_tcp_queue_c2h_data(struct spdk_nvmf_tcp_req *tcp_req,
2322 			     struct spdk_nvmf_tcp_qpair *tqpair)
2323 {
2324 	tcp_req->c2h_data_pdu_num = spdk_nvmf_tcp_calc_c2h_data_pdu_num(tcp_req);
2325 
2326 	assert(tcp_req->c2h_data_pdu_num < NVMF_TCP_QPAIR_MAX_C2H_PDU_NUM);
2327 
2328 	TAILQ_INSERT_TAIL(&tqpair->queued_c2h_data_tcp_req, tcp_req, link);
2329 	spdk_nvmf_tcp_handle_pending_c2h_data_queue(tqpair);
2330 }
2331 
2332 static int
2333 request_transfer_out(struct spdk_nvmf_request *req)
2334 {
2335 	struct spdk_nvmf_tcp_req	*tcp_req;
2336 	struct spdk_nvmf_qpair		*qpair;
2337 	struct spdk_nvmf_tcp_qpair	*tqpair;
2338 	struct spdk_nvme_cpl		*rsp;
2339 
2340 	SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "enter\n");
2341 
2342 	qpair = req->qpair;
2343 	rsp = &req->rsp->nvme_cpl;
2344 	tcp_req = SPDK_CONTAINEROF(req, struct spdk_nvmf_tcp_req, req);
2345 
2346 	/* Advance our sq_head pointer */
2347 	if (qpair->sq_head == qpair->sq_head_max) {
2348 		qpair->sq_head = 0;
2349 	} else {
2350 		qpair->sq_head++;
2351 	}
2352 	rsp->sqhd = qpair->sq_head;
2353 
2354 	tqpair = SPDK_CONTAINEROF(tcp_req->req.qpair, struct spdk_nvmf_tcp_qpair, qpair);
2355 	spdk_nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_TRANSFERRING_CONTROLLER_TO_HOST);
2356 	if (rsp->status.sc == SPDK_NVME_SC_SUCCESS &&
2357 	    req->xfer == SPDK_NVME_DATA_CONTROLLER_TO_HOST) {
2358 		spdk_nvmf_tcp_queue_c2h_data(tcp_req, tqpair);
2359 	} else {
2360 		spdk_nvmf_tcp_send_capsule_resp_pdu(tcp_req, tqpair);
2361 	}
2362 
2363 	return 0;
2364 }
2365 
2366 static void
2367 spdk_nvmf_tcp_pdu_set_buf_from_req(struct spdk_nvmf_tcp_qpair *tqpair,
2368 				   struct spdk_nvmf_tcp_req *tcp_req)
2369 {
2370 	struct nvme_tcp_pdu *pdu;
2371 
2372 	if (tcp_req->data_from_pool) {
2373 		SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "Will send r2t for tcp_req(%p) on tqpair=%p\n", tcp_req, tqpair);
2374 		tcp_req->next_expected_r2t_offset = 0;
2375 		spdk_nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_DATA_PENDING_FOR_R2T);
2376 		spdk_nvmf_tcp_handle_queued_r2t_req(tqpair);
2377 	} else {
2378 		pdu = &tqpair->pdu_in_progress;
2379 		SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "Not need to send r2t for tcp_req(%p) on tqpair=%p\n", tcp_req,
2380 			      tqpair);
2381 		/* No need to send r2t, contained in the capsuled data */
2382 		pdu->data = tcp_req->req.data;
2383 		pdu->data_len = tcp_req->req.length;
2384 		spdk_nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_PAYLOAD);
2385 		spdk_nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_TRANSFERRING_HOST_TO_CONTROLLER);
2386 	}
2387 }
2388 
2389 static void
2390 spdk_nvmf_tcp_set_incapsule_data(struct spdk_nvmf_tcp_qpair *tqpair,
2391 				 struct spdk_nvmf_tcp_req *tcp_req)
2392 {
2393 	struct nvme_tcp_pdu *pdu;
2394 	uint32_t plen = 0;
2395 
2396 	pdu = &tqpair->pdu_in_progress;
2397 	plen = pdu->hdr.common.hlen;
2398 
2399 	if (tqpair->host_hdgst_enable) {
2400 		plen += SPDK_NVME_TCP_DIGEST_LEN;
2401 	}
2402 
2403 	if (pdu->hdr.common.plen != plen) {
2404 		tcp_req->has_incapsule_data = true;
2405 	}
2406 }
2407 
2408 static bool
2409 spdk_nvmf_tcp_req_process(struct spdk_nvmf_tcp_transport *ttransport,
2410 			  struct spdk_nvmf_tcp_req *tcp_req)
2411 {
2412 	struct spdk_nvmf_tcp_qpair		*tqpair;
2413 	struct spdk_nvme_cpl			*rsp = &tcp_req->req.rsp->nvme_cpl;
2414 	int					rc;
2415 	enum spdk_nvmf_tcp_req_state		prev_state;
2416 	bool					progress = false;
2417 	struct spdk_nvmf_transport_poll_group	*group;
2418 
2419 	tqpair = SPDK_CONTAINEROF(tcp_req->req.qpair, struct spdk_nvmf_tcp_qpair, qpair);
2420 	group = &tqpair->group->group;
2421 	assert(tcp_req->state != TCP_REQUEST_STATE_FREE);
2422 
2423 	/* The loop here is to allow for several back-to-back state changes. */
2424 	do {
2425 		prev_state = tcp_req->state;
2426 
2427 		SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "Request %p entering state %d on tqpair=%p\n", tcp_req, prev_state,
2428 			      tqpair);
2429 
2430 		switch (tcp_req->state) {
2431 		case TCP_REQUEST_STATE_FREE:
2432 			/* Some external code must kick a request into TCP_REQUEST_STATE_NEW
2433 			 * to escape this state. */
2434 			break;
2435 		case TCP_REQUEST_STATE_NEW:
2436 			spdk_trace_record(TRACE_TCP_REQUEST_STATE_NEW, 0, 0, (uintptr_t)tcp_req, 0);
2437 
2438 			/* copy the cmd from the receive pdu */
2439 			tcp_req->cmd = tqpair->pdu_in_progress.hdr.capsule_cmd.ccsqe;
2440 
2441 			/* The next state transition depends on the data transfer needs of this request. */
2442 			tcp_req->req.xfer = spdk_nvmf_tcp_req_get_xfer(tcp_req);
2443 
2444 			/* If no data to transfer, ready to execute. */
2445 			if (tcp_req->req.xfer == SPDK_NVME_DATA_NONE) {
2446 				/* Reset the tqpair receving pdu state */
2447 				spdk_nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_READY);
2448 				spdk_nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_READY_TO_EXECUTE);
2449 				break;
2450 			}
2451 
2452 			spdk_nvmf_tcp_set_incapsule_data(tqpair, tcp_req);
2453 
2454 			if (!tcp_req->has_incapsule_data) {
2455 				spdk_nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_AWAIT_PDU_READY);
2456 			}
2457 
2458 			spdk_nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_NEED_BUFFER);
2459 			TAILQ_INSERT_TAIL(&tqpair->group->pending_data_buf_queue, tcp_req, link);
2460 			break;
2461 		case TCP_REQUEST_STATE_NEED_BUFFER:
2462 			spdk_trace_record(TRACE_TCP_REQUEST_STATE_NEED_BUFFER, 0, 0, (uintptr_t)tcp_req, 0);
2463 
2464 			assert(tcp_req->req.xfer != SPDK_NVME_DATA_NONE);
2465 
2466 			if (!tcp_req->has_incapsule_data &&
2467 			    (tcp_req != TAILQ_FIRST(&tqpair->group->pending_data_buf_queue))) {
2468 				SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP,
2469 					      "Not the first element to wait for the buf for tcp_req(%p) on tqpair=%p\n",
2470 					      tcp_req, tqpair);
2471 				/* This request needs to wait in line to obtain a buffer */
2472 				break;
2473 			}
2474 
2475 			/* Try to get a data buffer */
2476 			rc = spdk_nvmf_tcp_req_parse_sgl(ttransport, tcp_req);
2477 			if (rc < 0) {
2478 				TAILQ_REMOVE(&tqpair->group->pending_data_buf_queue, tcp_req, link);
2479 				rsp->status.sc = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR;
2480 				/* Reset the tqpair receving pdu state */
2481 				spdk_nvmf_tcp_qpair_set_recv_state(tqpair, NVME_TCP_PDU_RECV_STATE_ERROR);
2482 				spdk_nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_READY_TO_COMPLETE);
2483 				break;
2484 			}
2485 
2486 			if (!tcp_req->req.data) {
2487 				SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "No buffer allocated for tcp_req(%p) on tqpair(%p\n)",
2488 					      tcp_req, tqpair);
2489 				/* No buffers available. */
2490 				break;
2491 			}
2492 
2493 			TAILQ_REMOVE(&tqpair->group->pending_data_buf_queue, tcp_req, link);
2494 
2495 			/* If data is transferring from host to controller, we need to do a transfer from the host. */
2496 			if (tcp_req->req.xfer == SPDK_NVME_DATA_HOST_TO_CONTROLLER) {
2497 				spdk_nvmf_tcp_pdu_set_buf_from_req(tqpair, tcp_req);
2498 				break;
2499 			}
2500 
2501 			spdk_nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_READY_TO_EXECUTE);
2502 			break;
2503 		case TCP_REQUEST_STATE_DATA_PENDING_FOR_R2T:
2504 			spdk_trace_record(TCP_REQUEST_STATE_DATA_PENDING_FOR_R2T, 0, 0,
2505 					  (uintptr_t)tcp_req, 0);
2506 			/* Some external code must kick a request into TCP_REQUEST_STATE_DATA_PENDING_R2T
2507 			 * to escape this state. */
2508 			break;
2509 
2510 		case TCP_REQUEST_STATE_TRANSFERRING_HOST_TO_CONTROLLER:
2511 			spdk_trace_record(TRACE_TCP_REQUEST_STATE_TRANSFERRING_HOST_TO_CONTROLLER, 0, 0,
2512 					  (uintptr_t)tcp_req, 0);
2513 			/* Some external code must kick a request into TCP_REQUEST_STATE_READY_TO_EXECUTE
2514 			 * to escape this state. */
2515 			break;
2516 		case TCP_REQUEST_STATE_READY_TO_EXECUTE:
2517 			spdk_trace_record(TRACE_TCP_REQUEST_STATE_READY_TO_EXECUTE, 0, 0, (uintptr_t)tcp_req, 0);
2518 			spdk_nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_EXECUTING);
2519 			spdk_nvmf_request_exec(&tcp_req->req);
2520 			break;
2521 		case TCP_REQUEST_STATE_EXECUTING:
2522 			spdk_trace_record(TRACE_TCP_REQUEST_STATE_EXECUTING, 0, 0, (uintptr_t)tcp_req, 0);
2523 			/* Some external code must kick a request into TCP_REQUEST_STATE_EXECUTED
2524 			 * to escape this state. */
2525 			break;
2526 		case TCP_REQUEST_STATE_EXECUTED:
2527 			spdk_trace_record(TRACE_TCP_REQUEST_STATE_EXECUTED, 0, 0, (uintptr_t)tcp_req, 0);
2528 			spdk_nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_READY_TO_COMPLETE);
2529 			break;
2530 		case TCP_REQUEST_STATE_READY_TO_COMPLETE:
2531 			spdk_trace_record(TRACE_TCP_REQUEST_STATE_READY_TO_COMPLETE, 0, 0, (uintptr_t)tcp_req, 0);
2532 			rc = request_transfer_out(&tcp_req->req);
2533 			assert(rc == 0); /* No good way to handle this currently */
2534 			break;
2535 		case TCP_REQUEST_STATE_TRANSFERRING_CONTROLLER_TO_HOST:
2536 			spdk_trace_record(TRACE_TCP_REQUEST_STATE_TRANSFERRING_CONTROLLER_TO_HOST, 0, 0,
2537 					  (uintptr_t)tcp_req,
2538 					  0);
2539 			/* Some external code must kick a request into TCP_REQUEST_STATE_COMPLETED
2540 			 * to escape this state. */
2541 			break;
2542 		case TCP_REQUEST_STATE_COMPLETED:
2543 			spdk_trace_record(TRACE_TCP_REQUEST_STATE_COMPLETED, 0, 0, (uintptr_t)tcp_req, 0);
2544 			if (tcp_req->data_from_pool) {
2545 				spdk_nvmf_tcp_request_free_buffers(tcp_req, group, &ttransport->transport);
2546 			}
2547 			tcp_req->req.length = 0;
2548 			tcp_req->req.iovcnt = 0;
2549 			tcp_req->req.data = NULL;
2550 			spdk_nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_FREE);
2551 			break;
2552 		case TCP_REQUEST_NUM_STATES:
2553 		default:
2554 			assert(0);
2555 			break;
2556 		}
2557 
2558 		if (tcp_req->state != prev_state) {
2559 			progress = true;
2560 		}
2561 	} while (tcp_req->state != prev_state);
2562 
2563 	return progress;
2564 }
2565 
2566 static void
2567 spdk_nvmf_tcp_qpair_process_pending(struct spdk_nvmf_tcp_transport *ttransport,
2568 				    struct spdk_nvmf_tcp_qpair *tqpair)
2569 {
2570 	struct spdk_nvmf_tcp_req *tcp_req, *req_tmp;
2571 
2572 	/* Tqpair is not in a good state, so return it */
2573 	if (spdk_unlikely(tqpair->recv_state == NVME_TCP_PDU_RECV_STATE_ERROR)) {
2574 		return;
2575 	}
2576 
2577 	spdk_nvmf_tcp_handle_queued_r2t_req(tqpair);
2578 
2579 	TAILQ_FOREACH_SAFE(tcp_req, &tqpair->group->pending_data_buf_queue, link, req_tmp) {
2580 		if (spdk_nvmf_tcp_req_process(ttransport, tcp_req) == false) {
2581 			break;
2582 		}
2583 	}
2584 }
2585 
2586 static void
2587 spdk_nvmf_tcp_sock_cb(void *arg, struct spdk_sock_group *group, struct spdk_sock *sock)
2588 {
2589 	struct spdk_nvmf_tcp_qpair *tqpair = arg;
2590 	struct spdk_nvmf_tcp_transport *ttransport;
2591 	int rc;
2592 
2593 	assert(tqpair != NULL);
2594 
2595 	ttransport = SPDK_CONTAINEROF(tqpair->qpair.transport, struct spdk_nvmf_tcp_transport, transport);
2596 	spdk_nvmf_tcp_qpair_process_pending(ttransport, tqpair);
2597 	rc = spdk_nvmf_tcp_sock_process(tqpair);
2598 
2599 	/* check the following two factors:
2600 	 * rc: The socket is closed
2601 	 * State of tqpair: The tqpair is in EXITING state due to internal error
2602 	 */
2603 	if ((rc < 0) || (tqpair->state == NVME_TCP_QPAIR_STATE_EXITING)) {
2604 		tqpair->state = NVME_TCP_QPAIR_STATE_EXITED;
2605 		spdk_nvmf_tcp_qpair_flush_pdus(tqpair);
2606 		SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "will disconect the tqpair=%p\n", tqpair);
2607 		spdk_poller_unregister(&tqpair->timeout_poller);
2608 		spdk_nvmf_qpair_disconnect(&tqpair->qpair, NULL, NULL);
2609 	}
2610 }
2611 
2612 static int
2613 spdk_nvmf_tcp_poll_group_add(struct spdk_nvmf_transport_poll_group *group,
2614 			     struct spdk_nvmf_qpair *qpair)
2615 {
2616 	struct spdk_nvmf_tcp_poll_group	*tgroup;
2617 	struct spdk_nvmf_tcp_qpair	*tqpair;
2618 	int				rc;
2619 
2620 	tgroup = SPDK_CONTAINEROF(group, struct spdk_nvmf_tcp_poll_group, group);
2621 	tqpair = SPDK_CONTAINEROF(qpair, struct spdk_nvmf_tcp_qpair, qpair);
2622 
2623 	rc = spdk_sock_group_add_sock(tgroup->sock_group, tqpair->sock,
2624 				      spdk_nvmf_tcp_sock_cb, tqpair);
2625 	if (rc != 0) {
2626 		SPDK_ERRLOG("Could not add sock to sock_group: %s (%d)\n",
2627 			    spdk_strerror(errno), errno);
2628 		spdk_nvmf_tcp_qpair_destroy(tqpair);
2629 		return -1;
2630 	}
2631 
2632 	rc =  spdk_nvmf_tcp_qpair_sock_init(tqpair);
2633 	if (rc != 0) {
2634 		SPDK_ERRLOG("Cannot set sock opt for tqpair=%p\n", tqpair);
2635 		spdk_nvmf_tcp_qpair_destroy(tqpair);
2636 		return -1;
2637 	}
2638 
2639 	rc = spdk_nvmf_tcp_qpair_init(&tqpair->qpair);
2640 	if (rc < 0) {
2641 		SPDK_ERRLOG("Cannot init tqpair=%p\n", tqpair);
2642 		spdk_nvmf_tcp_qpair_destroy(tqpair);
2643 		return -1;
2644 	}
2645 
2646 	rc = spdk_nvmf_tcp_qpair_init_mem_resource(tqpair, 1);
2647 	if (rc < 0) {
2648 		SPDK_ERRLOG("Cannot init memory resource info for tqpair=%p\n", tqpair);
2649 		spdk_nvmf_tcp_qpair_destroy(tqpair);
2650 		return -1;
2651 	}
2652 
2653 	tqpair->group = tgroup;
2654 	tqpair->state = NVME_TCP_QPAIR_STATE_INVALID;
2655 	TAILQ_INSERT_TAIL(&tgroup->qpairs, tqpair, link);
2656 
2657 	return 0;
2658 }
2659 
2660 static int
2661 spdk_nvmf_tcp_poll_group_remove(struct spdk_nvmf_transport_poll_group *group,
2662 				struct spdk_nvmf_qpair *qpair)
2663 {
2664 	struct spdk_nvmf_tcp_poll_group	*tgroup;
2665 	struct spdk_nvmf_tcp_qpair		*tqpair;
2666 	int				rc;
2667 
2668 	tgroup = SPDK_CONTAINEROF(group, struct spdk_nvmf_tcp_poll_group, group);
2669 	tqpair = SPDK_CONTAINEROF(qpair, struct spdk_nvmf_tcp_qpair, qpair);
2670 
2671 	assert(tqpair->group == tgroup);
2672 
2673 	SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "remove tqpair=%p from the tgroup=%p\n", tqpair, tgroup);
2674 	TAILQ_REMOVE(&tgroup->qpairs, tqpair, link);
2675 	rc = spdk_sock_group_remove_sock(tgroup->sock_group, tqpair->sock);
2676 	if (rc != 0) {
2677 		SPDK_ERRLOG("Could not remove sock from sock_group: %s (%d)\n",
2678 			    spdk_strerror(errno), errno);
2679 	}
2680 
2681 	return rc;
2682 }
2683 
2684 static int
2685 spdk_nvmf_tcp_req_complete(struct spdk_nvmf_request *req)
2686 {
2687 	struct spdk_nvmf_tcp_transport *ttransport;
2688 	struct spdk_nvmf_tcp_req *tcp_req;
2689 
2690 	ttransport = SPDK_CONTAINEROF(req->qpair->transport, struct spdk_nvmf_tcp_transport, transport);
2691 	tcp_req = SPDK_CONTAINEROF(req, struct spdk_nvmf_tcp_req, req);
2692 
2693 	spdk_nvmf_tcp_req_set_state(tcp_req, TCP_REQUEST_STATE_EXECUTED);
2694 	spdk_nvmf_tcp_req_process(ttransport, tcp_req);
2695 
2696 	return 0;
2697 }
2698 
2699 static void
2700 spdk_nvmf_tcp_close_qpair(struct spdk_nvmf_qpair *qpair)
2701 {
2702 	SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "enter\n");
2703 
2704 	spdk_nvmf_tcp_qpair_destroy(SPDK_CONTAINEROF(qpair, struct spdk_nvmf_tcp_qpair, qpair));
2705 }
2706 
2707 static int
2708 spdk_nvmf_tcp_poll_group_poll(struct spdk_nvmf_transport_poll_group *group)
2709 {
2710 	struct spdk_nvmf_tcp_poll_group *tgroup;
2711 	int rc;
2712 
2713 	tgroup = SPDK_CONTAINEROF(group, struct spdk_nvmf_tcp_poll_group, group);
2714 
2715 	if (spdk_unlikely(TAILQ_EMPTY(&tgroup->qpairs))) {
2716 		return 0;
2717 	}
2718 
2719 	rc = spdk_sock_group_poll(tgroup->sock_group);
2720 	if (rc < 0) {
2721 		SPDK_ERRLOG("Failed to poll sock_group=%p\n", tgroup->sock_group);
2722 		return rc;
2723 	}
2724 
2725 	return 0;
2726 }
2727 
2728 static bool
2729 spdk_nvmf_tcp_qpair_is_idle(struct spdk_nvmf_qpair *qpair)
2730 {
2731 	struct spdk_nvmf_tcp_qpair *tqpair;
2732 
2733 	tqpair = SPDK_CONTAINEROF(qpair, struct spdk_nvmf_tcp_qpair, qpair);
2734 	if (tqpair->state_cntr[TCP_REQUEST_STATE_FREE] == tqpair->max_queue_depth) {
2735 		return true;
2736 	}
2737 
2738 	return false;
2739 }
2740 
2741 static int
2742 spdk_nvmf_tcp_qpair_get_trid(struct spdk_nvmf_qpair *qpair,
2743 			     struct spdk_nvme_transport_id *trid, bool peer)
2744 {
2745 	struct spdk_nvmf_tcp_qpair     *tqpair;
2746 	uint16_t			port;
2747 
2748 	tqpair = SPDK_CONTAINEROF(qpair, struct spdk_nvmf_tcp_qpair, qpair);
2749 	trid->trtype = SPDK_NVME_TRANSPORT_TCP;
2750 
2751 	if (peer) {
2752 		snprintf(trid->traddr, sizeof(trid->traddr), "%s", tqpair->initiator_addr);
2753 		port = tqpair->initiator_port;
2754 	} else {
2755 		snprintf(trid->traddr, sizeof(trid->traddr), "%s", tqpair->target_addr);
2756 		port = tqpair->target_port;
2757 	}
2758 
2759 	if (spdk_sock_is_ipv4(tqpair->sock)) {
2760 		trid->adrfam = SPDK_NVMF_ADRFAM_IPV4;
2761 	} else if (spdk_sock_is_ipv4(tqpair->sock)) {
2762 		trid->adrfam = SPDK_NVMF_ADRFAM_IPV6;
2763 	} else {
2764 		return -1;
2765 	}
2766 
2767 	snprintf(trid->trsvcid, sizeof(trid->trsvcid), "%d", port);
2768 	return 0;
2769 }
2770 
2771 static int
2772 spdk_nvmf_tcp_qpair_get_local_trid(struct spdk_nvmf_qpair *qpair,
2773 				   struct spdk_nvme_transport_id *trid)
2774 {
2775 	return spdk_nvmf_tcp_qpair_get_trid(qpair, trid, 0);
2776 }
2777 
2778 static int
2779 spdk_nvmf_tcp_qpair_get_peer_trid(struct spdk_nvmf_qpair *qpair,
2780 				  struct spdk_nvme_transport_id *trid)
2781 {
2782 	return spdk_nvmf_tcp_qpair_get_trid(qpair, trid, 1);
2783 }
2784 
2785 static int
2786 spdk_nvmf_tcp_qpair_get_listen_trid(struct spdk_nvmf_qpair *qpair,
2787 				    struct spdk_nvme_transport_id *trid)
2788 {
2789 	return spdk_nvmf_tcp_qpair_get_trid(qpair, trid, 0);
2790 }
2791 
2792 static int
2793 spdk_nvmf_tcp_qpair_set_sq_size(struct spdk_nvmf_qpair *qpair)
2794 {
2795 	struct spdk_nvmf_tcp_qpair     *tqpair;
2796 	int rc;
2797 	tqpair = SPDK_CONTAINEROF(qpair, struct spdk_nvmf_tcp_qpair, qpair);
2798 
2799 	rc = spdk_nvmf_tcp_qpair_init_mem_resource(tqpair, tqpair->qpair.sq_head_max);
2800 	if (!rc) {
2801 		tqpair->max_queue_depth += tqpair->qpair.sq_head_max;
2802 		tqpair->free_pdu_num += tqpair->qpair.sq_head_max;
2803 		tqpair->state_cntr[TCP_REQUEST_STATE_FREE] += tqpair->qpair.sq_head_max;
2804 		SPDK_DEBUGLOG(SPDK_LOG_NVMF_TCP, "The queue depth=%u for tqpair=%p\n",
2805 			      tqpair->max_queue_depth, tqpair);
2806 	}
2807 
2808 	return rc;
2809 
2810 }
2811 
2812 #define SPDK_NVMF_TCP_DEFAULT_MAX_QUEUE_DEPTH 128
2813 #define SPDK_NVMF_TCP_DEFAULT_AQ_DEPTH 128
2814 #define SPDK_NVMF_TCP_DEFAULT_MAX_QPAIRS_PER_CTRLR 64
2815 #define SPDK_NVMF_TCP_DEFAULT_IN_CAPSULE_DATA_SIZE 4096
2816 #define SPDK_NVMF_TCP_DEFAULT_MAX_IO_SIZE 131072
2817 #define SPDK_NVMF_TCP_DEFAULT_IO_UNIT_SIZE 131072
2818 #define SPDK_NVMF_TCP_DEFAULT_NUM_SHARED_BUFFERS 512
2819 #define SPDK_NVMF_TCP_DEFAULT_BUFFER_CACHE_SIZE 32
2820 
2821 static void
2822 spdk_nvmf_tcp_opts_init(struct spdk_nvmf_transport_opts *opts)
2823 {
2824 	opts->max_queue_depth =		SPDK_NVMF_TCP_DEFAULT_MAX_QUEUE_DEPTH;
2825 	opts->max_qpairs_per_ctrlr =	SPDK_NVMF_TCP_DEFAULT_MAX_QPAIRS_PER_CTRLR;
2826 	opts->in_capsule_data_size =	SPDK_NVMF_TCP_DEFAULT_IN_CAPSULE_DATA_SIZE;
2827 	opts->max_io_size =		SPDK_NVMF_TCP_DEFAULT_MAX_IO_SIZE;
2828 	opts->io_unit_size =		SPDK_NVMF_TCP_DEFAULT_IO_UNIT_SIZE;
2829 	opts->max_aq_depth =		SPDK_NVMF_TCP_DEFAULT_AQ_DEPTH;
2830 	opts->num_shared_buffers =	SPDK_NVMF_TCP_DEFAULT_NUM_SHARED_BUFFERS;
2831 	opts->buf_cache_size =		SPDK_NVMF_TCP_DEFAULT_BUFFER_CACHE_SIZE;
2832 }
2833 
2834 const struct spdk_nvmf_transport_ops spdk_nvmf_transport_tcp = {
2835 	.type = SPDK_NVME_TRANSPORT_TCP,
2836 	.opts_init = spdk_nvmf_tcp_opts_init,
2837 	.create = spdk_nvmf_tcp_create,
2838 	.destroy = spdk_nvmf_tcp_destroy,
2839 
2840 	.listen = spdk_nvmf_tcp_listen,
2841 	.stop_listen = spdk_nvmf_tcp_stop_listen,
2842 	.accept = spdk_nvmf_tcp_accept,
2843 
2844 	.listener_discover = spdk_nvmf_tcp_discover,
2845 
2846 	.poll_group_create = spdk_nvmf_tcp_poll_group_create,
2847 	.poll_group_destroy = spdk_nvmf_tcp_poll_group_destroy,
2848 	.poll_group_add = spdk_nvmf_tcp_poll_group_add,
2849 	.poll_group_remove = spdk_nvmf_tcp_poll_group_remove,
2850 	.poll_group_poll = spdk_nvmf_tcp_poll_group_poll,
2851 
2852 	.req_free = spdk_nvmf_tcp_req_free,
2853 	.req_complete = spdk_nvmf_tcp_req_complete,
2854 
2855 	.qpair_fini = spdk_nvmf_tcp_close_qpair,
2856 	.qpair_is_idle = spdk_nvmf_tcp_qpair_is_idle,
2857 	.qpair_get_local_trid = spdk_nvmf_tcp_qpair_get_local_trid,
2858 	.qpair_get_peer_trid = spdk_nvmf_tcp_qpair_get_peer_trid,
2859 	.qpair_get_listen_trid = spdk_nvmf_tcp_qpair_get_listen_trid,
2860 	.qpair_set_sqsize = spdk_nvmf_tcp_qpair_set_sq_size,
2861 };
2862 
2863 SPDK_LOG_REGISTER_COMPONENT("nvmf_tcp", SPDK_LOG_NVMF_TCP)
2864