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