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