xref: /spdk/lib/iscsi/conn.c (revision 367c980b453f48310e52d2574afe7d2774df800c)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright (C) 2008-2012 Daisuke Aoyama <aoyama@peach.ne.jp>.
5  *   Copyright (c) Intel Corporation.
6  *   All rights reserved.
7  *
8  *   Redistribution and use in source and binary forms, with or without
9  *   modification, are permitted provided that the following conditions
10  *   are met:
11  *
12  *     * Redistributions of source code must retain the above copyright
13  *       notice, this list of conditions and the following disclaimer.
14  *     * Redistributions in binary form must reproduce the above copyright
15  *       notice, this list of conditions and the following disclaimer in
16  *       the documentation and/or other materials provided with the
17  *       distribution.
18  *     * Neither the name of Intel Corporation nor the names of its
19  *       contributors may be used to endorse or promote products derived
20  *       from this software without specific prior written permission.
21  *
22  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #include "spdk/stdinc.h"
36 
37 #include "spdk/endian.h"
38 #include "spdk/env.h"
39 #include "spdk/event.h"
40 #include "spdk/likely.h"
41 #include "spdk/thread.h"
42 #include "spdk/queue.h"
43 #include "spdk/trace.h"
44 #include "spdk/net.h"
45 #include "spdk/sock.h"
46 #include "spdk/string.h"
47 
48 #include "spdk_internal/log.h"
49 
50 #include "iscsi/task.h"
51 #include "iscsi/conn.h"
52 #include "iscsi/tgt_node.h"
53 #include "iscsi/portal_grp.h"
54 
55 #define MAKE_DIGEST_WORD(BUF, CRC32C) \
56         (   ((*((uint8_t *)(BUF)+0)) = (uint8_t)((uint32_t)(CRC32C) >> 0)), \
57             ((*((uint8_t *)(BUF)+1)) = (uint8_t)((uint32_t)(CRC32C) >> 8)), \
58             ((*((uint8_t *)(BUF)+2)) = (uint8_t)((uint32_t)(CRC32C) >> 16)), \
59             ((*((uint8_t *)(BUF)+3)) = (uint8_t)((uint32_t)(CRC32C) >> 24)))
60 
61 #define SPDK_ISCSI_CONNECTION_MEMSET(conn)		\
62 	memset(&(conn)->portal, 0, sizeof(*(conn)) -	\
63 		offsetof(struct spdk_iscsi_conn, portal));
64 
65 struct spdk_iscsi_conn *g_conns_array = MAP_FAILED;
66 static int g_conns_array_fd = -1;
67 static char g_shm_name[64];
68 
69 static TAILQ_HEAD(, spdk_iscsi_conn) g_free_conns = TAILQ_HEAD_INITIALIZER(g_free_conns);
70 static TAILQ_HEAD(, spdk_iscsi_conn) g_active_conns = TAILQ_HEAD_INITIALIZER(g_active_conns);
71 
72 static pthread_mutex_t g_conns_mutex = PTHREAD_MUTEX_INITIALIZER;
73 
74 static struct spdk_poller *g_shutdown_timer = NULL;
75 
76 static void iscsi_conn_sock_cb(void *arg, struct spdk_sock_group *group,
77 			       struct spdk_sock *sock);
78 
79 static struct spdk_iscsi_conn *
80 allocate_conn(void)
81 {
82 	struct spdk_iscsi_conn	*conn;
83 
84 	pthread_mutex_lock(&g_conns_mutex);
85 	conn = TAILQ_FIRST(&g_free_conns);
86 	if (conn != NULL) {
87 		assert(!conn->is_valid);
88 		TAILQ_REMOVE(&g_free_conns, conn, conn_link);
89 		SPDK_ISCSI_CONNECTION_MEMSET(conn);
90 		conn->is_valid = 1;
91 
92 		TAILQ_INSERT_TAIL(&g_active_conns, conn, conn_link);
93 	}
94 	pthread_mutex_unlock(&g_conns_mutex);
95 
96 	return conn;
97 }
98 
99 static void
100 _free_conn(struct spdk_iscsi_conn *conn)
101 {
102 	TAILQ_REMOVE(&g_active_conns, conn, conn_link);
103 
104 	memset(conn->portal_host, 0, sizeof(conn->portal_host));
105 	memset(conn->portal_port, 0, sizeof(conn->portal_port));
106 	conn->is_valid = 0;
107 
108 	TAILQ_INSERT_TAIL(&g_free_conns, conn, conn_link);
109 }
110 
111 static void
112 free_conn(struct spdk_iscsi_conn *conn)
113 {
114 	pthread_mutex_lock(&g_conns_mutex);
115 	_free_conn(conn);
116 	pthread_mutex_unlock(&g_conns_mutex);
117 }
118 
119 static void
120 _iscsi_conns_cleanup(void)
121 {
122 	if (g_conns_array != MAP_FAILED) {
123 		munmap(g_conns_array, sizeof(struct spdk_iscsi_conn) *
124 		       MAX_ISCSI_CONNECTIONS);
125 		g_conns_array = MAP_FAILED;
126 	}
127 
128 	if (g_conns_array_fd >= 0) {
129 		close(g_conns_array_fd);
130 		g_conns_array_fd = -1;
131 		shm_unlink(g_shm_name);
132 	}
133 }
134 
135 int initialize_iscsi_conns(void)
136 {
137 	size_t conns_size = sizeof(struct spdk_iscsi_conn) * MAX_ISCSI_CONNECTIONS;
138 	uint32_t i;
139 
140 	SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "spdk_iscsi_init\n");
141 
142 	snprintf(g_shm_name, sizeof(g_shm_name), "/spdk_iscsi_conns.%d", spdk_app_get_shm_id());
143 	g_conns_array_fd = shm_open(g_shm_name, O_RDWR | O_CREAT, 0600);
144 	if (g_conns_array_fd < 0) {
145 		SPDK_ERRLOG("could not shm_open %s\n", g_shm_name);
146 		goto err;
147 	}
148 
149 	if (ftruncate(g_conns_array_fd, conns_size) != 0) {
150 		SPDK_ERRLOG("could not ftruncate\n");
151 		goto err;
152 	}
153 	g_conns_array = mmap(0, conns_size, PROT_READ | PROT_WRITE, MAP_SHARED,
154 			     g_conns_array_fd, 0);
155 
156 	if (g_conns_array == MAP_FAILED) {
157 		SPDK_ERRLOG("could not mmap cons array file %s (%d)\n", g_shm_name, errno);
158 		goto err;
159 	}
160 
161 	memset(g_conns_array, 0, conns_size);
162 
163 	for (i = 0; i < MAX_ISCSI_CONNECTIONS; i++) {
164 		g_conns_array[i].id = i;
165 		TAILQ_INSERT_TAIL(&g_free_conns, &g_conns_array[i], conn_link);
166 	}
167 
168 	return 0;
169 
170 err:
171 	_iscsi_conns_cleanup();
172 
173 	return -1;
174 }
175 
176 static void
177 iscsi_poll_group_add_conn(struct spdk_iscsi_poll_group *pg, struct spdk_iscsi_conn *conn)
178 {
179 	int rc;
180 
181 	rc = spdk_sock_group_add_sock(pg->sock_group, conn->sock, iscsi_conn_sock_cb, conn);
182 	if (rc < 0) {
183 		SPDK_ERRLOG("Failed to add sock=%p of conn=%p\n", conn->sock, conn);
184 		return;
185 	}
186 
187 	conn->is_stopped = false;
188 	STAILQ_INSERT_TAIL(&pg->connections, conn, pg_link);
189 }
190 
191 static void
192 iscsi_poll_group_remove_conn(struct spdk_iscsi_poll_group *pg, struct spdk_iscsi_conn *conn)
193 {
194 	int rc;
195 
196 	assert(conn->sock != NULL);
197 	rc = spdk_sock_group_remove_sock(pg->sock_group, conn->sock);
198 	if (rc < 0) {
199 		SPDK_ERRLOG("Failed to remove sock=%p of conn=%p\n", conn->sock, conn);
200 	}
201 
202 	conn->is_stopped = true;
203 	STAILQ_REMOVE(&pg->connections, conn, spdk_iscsi_conn, pg_link);
204 }
205 
206 static void
207 iscsi_conn_start(void *ctx)
208 {
209 	struct spdk_iscsi_conn *conn = ctx;
210 
211 	iscsi_poll_group_add_conn(conn->pg, conn);
212 }
213 
214 int
215 iscsi_conn_construct(struct spdk_iscsi_portal *portal,
216 		     struct spdk_sock *sock)
217 {
218 	struct spdk_iscsi_poll_group *pg;
219 	struct spdk_iscsi_conn *conn;
220 	int i, rc;
221 
222 	conn = allocate_conn();
223 	if (conn == NULL) {
224 		SPDK_ERRLOG("Could not allocate connection.\n");
225 		return -1;
226 	}
227 
228 	pthread_mutex_lock(&g_iscsi.mutex);
229 	conn->timeout = g_iscsi.timeout * spdk_get_ticks_hz(); /* seconds to TSC */
230 	conn->nopininterval = g_iscsi.nopininterval;
231 	conn->nopininterval *= spdk_get_ticks_hz(); /* seconds to TSC */
232 	conn->nop_outstanding = false;
233 	conn->data_out_cnt = 0;
234 	conn->data_in_cnt = 0;
235 	conn->disable_chap = portal->group->disable_chap;
236 	conn->require_chap = portal->group->require_chap;
237 	conn->mutual_chap = portal->group->mutual_chap;
238 	conn->chap_group = portal->group->chap_group;
239 	pthread_mutex_unlock(&g_iscsi.mutex);
240 	conn->MaxRecvDataSegmentLength = 8192; /* RFC3720(12.12) */
241 
242 	conn->portal = portal;
243 	conn->pg_tag = portal->group->tag;
244 	memcpy(conn->portal_host, portal->host, strlen(portal->host));
245 	memcpy(conn->portal_port, portal->port, strlen(portal->port));
246 	conn->sock = sock;
247 
248 	conn->state = ISCSI_CONN_STATE_INVALID;
249 	conn->login_phase = ISCSI_SECURITY_NEGOTIATION_PHASE;
250 	conn->ttt = 0;
251 
252 	conn->partial_text_parameter = NULL;
253 
254 	for (i = 0; i < MAX_CONNECTION_PARAMS; i++) {
255 		conn->conn_param_state_negotiated[i] = false;
256 	}
257 
258 	for (i = 0; i < MAX_SESSION_PARAMS; i++) {
259 		conn->sess_param_state_negotiated[i] = false;
260 	}
261 
262 	conn->pdu_recv_state = ISCSI_PDU_RECV_STATE_AWAIT_PDU_READY;
263 
264 	TAILQ_INIT(&conn->write_pdu_list);
265 	TAILQ_INIT(&conn->snack_pdu_list);
266 	TAILQ_INIT(&conn->queued_r2t_tasks);
267 	TAILQ_INIT(&conn->active_r2t_tasks);
268 	TAILQ_INIT(&conn->queued_datain_tasks);
269 	memset(&conn->luns, 0, sizeof(conn->luns));
270 
271 	rc = spdk_sock_getaddr(sock, conn->target_addr, sizeof conn->target_addr, NULL,
272 			       conn->initiator_addr, sizeof conn->initiator_addr, NULL);
273 	if (rc < 0) {
274 		SPDK_ERRLOG("spdk_sock_getaddr() failed\n");
275 		goto error_return;
276 	}
277 
278 	/* set low water mark */
279 	rc = spdk_sock_set_recvlowat(conn->sock, 1);
280 	if (rc != 0) {
281 		SPDK_ERRLOG("spdk_sock_set_recvlowat() failed\n");
282 		goto error_return;
283 	}
284 
285 	/* set default params */
286 	rc = iscsi_conn_params_init(&conn->params);
287 	if (rc < 0) {
288 		SPDK_ERRLOG("iscsi_conn_params_init() failed\n");
289 		goto error_return;
290 	}
291 	conn->logout_request_timer = NULL;
292 	conn->logout_timer = NULL;
293 	conn->shutdown_timer = NULL;
294 	SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "Launching connection on acceptor thread\n");
295 	conn->pending_task_cnt = 0;
296 
297 	/* Get the first poll group. */
298 	pg = TAILQ_FIRST(&g_iscsi.poll_group_head);
299 	if (pg == NULL) {
300 		SPDK_ERRLOG("There is no poll group.\n");
301 		assert(false);
302 		goto error_return;
303 	}
304 
305 	conn->pg = pg;
306 	spdk_thread_send_msg(spdk_io_channel_get_thread(spdk_io_channel_from_ctx(pg)),
307 			     iscsi_conn_start, conn);
308 	return 0;
309 
310 error_return:
311 	iscsi_param_free(conn->params);
312 	free_conn(conn);
313 	return -1;
314 }
315 
316 void
317 iscsi_conn_free_pdu(struct spdk_iscsi_conn *conn, struct spdk_iscsi_pdu *pdu)
318 {
319 	iscsi_conn_xfer_complete_cb cb_fn;
320 	void *cb_arg;
321 
322 	cb_fn = pdu->cb_fn;
323 	cb_arg = pdu->cb_arg;
324 
325 	assert(cb_fn != NULL);
326 	pdu->cb_fn = NULL;
327 
328 	if (pdu->task) {
329 		iscsi_task_put(pdu->task);
330 	}
331 	iscsi_put_pdu(pdu);
332 
333 	cb_fn(cb_arg);
334 }
335 
336 static int
337 iscsi_conn_free_tasks(struct spdk_iscsi_conn *conn)
338 {
339 	struct spdk_iscsi_pdu *pdu, *tmp_pdu;
340 	struct spdk_iscsi_task *iscsi_task, *tmp_iscsi_task;
341 
342 	TAILQ_FOREACH_SAFE(pdu, &conn->snack_pdu_list, tailq, tmp_pdu) {
343 		TAILQ_REMOVE(&conn->snack_pdu_list, pdu, tailq);
344 		iscsi_conn_free_pdu(conn, pdu);
345 	}
346 
347 	TAILQ_FOREACH_SAFE(iscsi_task, &conn->queued_datain_tasks, link, tmp_iscsi_task) {
348 		if (!iscsi_task->is_queued) {
349 			TAILQ_REMOVE(&conn->queued_datain_tasks, iscsi_task, link);
350 			iscsi_task_put(iscsi_task);
351 		}
352 	}
353 
354 	/* We have to parse conn->write_pdu_list in the end.  In iscsi_conn_free_pdu(),
355 	 *  iscsi_conn_handle_queued_datain_tasks() may be called, and
356 	 *  iscsi_conn_handle_queued_datain_tasks() will parse conn->queued_datain_tasks
357 	 *  and may stack some PDUs to conn->write_pdu_list.  Hence when we come here, we
358 	 *  have to ensure there is no associated task in conn->queued_datain_tasks.
359 	 */
360 	TAILQ_FOREACH_SAFE(pdu, &conn->write_pdu_list, tailq, tmp_pdu) {
361 		TAILQ_REMOVE(&conn->write_pdu_list, pdu, tailq);
362 		iscsi_conn_free_pdu(conn, pdu);
363 	}
364 
365 	if (conn->pending_task_cnt) {
366 		return -1;
367 	}
368 
369 	return 0;
370 }
371 
372 static void
373 iscsi_conn_cleanup_backend(struct spdk_iscsi_conn *conn)
374 {
375 	int rc;
376 	struct spdk_iscsi_tgt_node *target;
377 
378 	if (conn->sess->connections > 1) {
379 		/* connection specific cleanup */
380 	} else if (!g_iscsi.AllowDuplicateIsid) {
381 		/* clean up all tasks to all LUNs for session */
382 		target = conn->sess->target;
383 		if (target != NULL) {
384 			rc = iscsi_tgt_node_cleanup_luns(conn, target);
385 			if (rc < 0) {
386 				SPDK_ERRLOG("target abort failed\n");
387 			}
388 		}
389 	}
390 }
391 
392 static void
393 iscsi_conn_free(struct spdk_iscsi_conn *conn)
394 {
395 	struct spdk_iscsi_sess *sess;
396 	int idx;
397 	uint32_t i;
398 
399 	pthread_mutex_lock(&g_conns_mutex);
400 
401 	if (conn->sess == NULL) {
402 		goto end;
403 	}
404 
405 	idx = -1;
406 	sess = conn->sess;
407 	conn->sess = NULL;
408 
409 	for (i = 0; i < sess->connections; i++) {
410 		if (sess->conns[i] == conn) {
411 			idx = i;
412 			break;
413 		}
414 	}
415 
416 	if (idx < 0) {
417 		SPDK_ERRLOG("remove conn not found\n");
418 	} else {
419 		for (i = idx; i < sess->connections - 1; i++) {
420 			sess->conns[i] = sess->conns[i + 1];
421 		}
422 		sess->conns[sess->connections - 1] = NULL;
423 		sess->connections--;
424 
425 		if (sess->connections == 0) {
426 			/* cleanup last connection */
427 			SPDK_DEBUGLOG(SPDK_LOG_ISCSI,
428 				      "cleanup last conn free sess\n");
429 			iscsi_free_sess(sess);
430 		}
431 	}
432 
433 	SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "Terminating connections(tsih %d): %d\n",
434 		      sess->tsih, sess->connections);
435 
436 end:
437 	SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "cleanup free conn\n");
438 	iscsi_param_free(conn->params);
439 	_free_conn(conn);
440 
441 	pthread_mutex_unlock(&g_conns_mutex);
442 }
443 
444 static void
445 iscsi_conn_close_lun(struct spdk_iscsi_conn *conn, int lun_id)
446 {
447 	struct spdk_iscsi_lun *iscsi_lun;
448 
449 	iscsi_lun = conn->luns[lun_id];
450 	if (iscsi_lun == NULL) {
451 		return;
452 	}
453 
454 	spdk_scsi_lun_free_io_channel(iscsi_lun->desc);
455 	spdk_scsi_lun_close(iscsi_lun->desc);
456 	spdk_poller_unregister(&iscsi_lun->remove_poller);
457 	free(iscsi_lun);
458 
459 	conn->luns[lun_id] = NULL;
460 }
461 
462 static void
463 iscsi_conn_close_luns(struct spdk_iscsi_conn *conn)
464 {
465 	int i;
466 
467 	for (i = 0; i < SPDK_SCSI_DEV_MAX_LUN; i++) {
468 		iscsi_conn_close_lun(conn, i);
469 	}
470 }
471 
472 static bool
473 iscsi_conn_check_tasks_for_lun(struct spdk_iscsi_conn *conn,
474 			       struct spdk_scsi_lun *lun)
475 {
476 	struct spdk_iscsi_pdu *pdu, *tmp_pdu;
477 	struct spdk_iscsi_task *task;
478 
479 	assert(lun != NULL);
480 
481 	/* We can remove deferred PDUs safely because they are already flushed. */
482 	TAILQ_FOREACH_SAFE(pdu, &conn->snack_pdu_list, tailq, tmp_pdu) {
483 		if (lun == pdu->task->scsi.lun) {
484 			TAILQ_REMOVE(&conn->snack_pdu_list, pdu, tailq);
485 			iscsi_conn_free_pdu(conn, pdu);
486 		}
487 	}
488 
489 	TAILQ_FOREACH(task, &conn->queued_datain_tasks, link) {
490 		if (lun == task->scsi.lun) {
491 			return false;
492 		}
493 	}
494 
495 	/* This check loop works even when connection exits in the middle of LUN hotplug
496 	 *  because all PDUs in write_pdu_list are removed in iscsi_conn_free_tasks().
497 	 */
498 	TAILQ_FOREACH(pdu, &conn->write_pdu_list, tailq) {
499 		if (pdu->task && lun == pdu->task->scsi.lun) {
500 			return false;
501 		}
502 	}
503 
504 	return true;
505 }
506 
507 static int
508 iscsi_conn_remove_lun(void *ctx)
509 {
510 	struct spdk_iscsi_lun *iscsi_lun = ctx;
511 	struct spdk_iscsi_conn *conn = iscsi_lun->conn;
512 	struct spdk_scsi_lun *lun = iscsi_lun->lun;
513 	int lun_id = spdk_scsi_lun_get_id(lun);
514 
515 	if (!iscsi_conn_check_tasks_for_lun(conn, lun)) {
516 		return SPDK_POLLER_BUSY;
517 	}
518 	iscsi_conn_close_lun(conn, lun_id);
519 	return SPDK_POLLER_BUSY;
520 }
521 
522 static void
523 _iscsi_conn_hotremove_lun(void *ctx)
524 {
525 	struct spdk_iscsi_lun *iscsi_lun = ctx;
526 	struct spdk_iscsi_conn *conn = iscsi_lun->conn;
527 	struct spdk_scsi_lun *lun = iscsi_lun->lun;
528 
529 	assert(spdk_io_channel_get_thread(spdk_io_channel_from_ctx(conn->pg)) ==
530 	       spdk_get_thread());
531 
532 	/* If a connection is already in stating status, just return */
533 	if (conn->state >= ISCSI_CONN_STATE_EXITING) {
534 		return;
535 	}
536 
537 	iscsi_clear_all_transfer_task(conn, lun, NULL);
538 
539 	iscsi_lun->remove_poller = SPDK_POLLER_REGISTER(iscsi_conn_remove_lun, iscsi_lun,
540 				   1000);
541 }
542 
543 static void
544 iscsi_conn_hotremove_lun(struct spdk_scsi_lun *lun, void *remove_ctx)
545 {
546 	struct spdk_iscsi_conn *conn = remove_ctx;
547 	int lun_id = spdk_scsi_lun_get_id(lun);
548 	struct spdk_iscsi_lun *iscsi_lun;
549 
550 	iscsi_lun = conn->luns[lun_id];
551 	if (iscsi_lun == NULL) {
552 		SPDK_ERRLOG("LUN hotplug was notified to the unallocated LUN %d.\n", lun_id);
553 		return;
554 	}
555 
556 	spdk_thread_send_msg(spdk_io_channel_get_thread(spdk_io_channel_from_ctx(conn->pg)),
557 			     _iscsi_conn_hotremove_lun, iscsi_lun);
558 }
559 
560 static int
561 iscsi_conn_open_lun(struct spdk_iscsi_conn *conn, int lun_id,
562 		    struct spdk_scsi_lun *lun)
563 {
564 	int rc;
565 	struct spdk_iscsi_lun *iscsi_lun;
566 
567 	iscsi_lun = calloc(1, sizeof(*iscsi_lun));
568 	if (iscsi_lun == NULL) {
569 		return -ENOMEM;
570 	}
571 
572 	iscsi_lun->conn = conn;
573 	iscsi_lun->lun = lun;
574 
575 	rc = spdk_scsi_lun_open(lun, iscsi_conn_hotremove_lun, conn, &iscsi_lun->desc);
576 	if (rc != 0) {
577 		free(iscsi_lun);
578 		return rc;
579 	}
580 
581 	rc = spdk_scsi_lun_allocate_io_channel(iscsi_lun->desc);
582 	if (rc != 0) {
583 		spdk_scsi_lun_close(iscsi_lun->desc);
584 		free(iscsi_lun);
585 		return rc;
586 	}
587 
588 	conn->luns[lun_id] = iscsi_lun;
589 
590 	return 0;
591 }
592 
593 static void
594 iscsi_conn_open_luns(struct spdk_iscsi_conn *conn)
595 {
596 	int i, rc;
597 	struct spdk_scsi_lun *lun;
598 
599 	for (i = 0; i < SPDK_SCSI_DEV_MAX_LUN; i++) {
600 		lun = spdk_scsi_dev_get_lun(conn->dev, i);
601 		if (lun == NULL) {
602 			continue;
603 		}
604 
605 		rc = iscsi_conn_open_lun(conn, i, lun);
606 		if (rc != 0) {
607 			goto error;
608 		}
609 	}
610 
611 	return;
612 
613 error:
614 	iscsi_conn_close_luns(conn);
615 }
616 
617 /**
618  *  This function will stop executing the specified connection.
619  */
620 static void
621 iscsi_conn_stop(struct spdk_iscsi_conn *conn)
622 {
623 	struct spdk_iscsi_tgt_node *target;
624 
625 	assert(conn->state == ISCSI_CONN_STATE_EXITED);
626 	assert(conn->data_in_cnt == 0);
627 	assert(conn->data_out_cnt == 0);
628 
629 	if (conn->sess != NULL &&
630 	    conn->sess->session_type == SESSION_TYPE_NORMAL &&
631 	    conn->full_feature) {
632 		target = conn->sess->target;
633 		pthread_mutex_lock(&target->mutex);
634 		target->num_active_conns--;
635 		pthread_mutex_unlock(&target->mutex);
636 
637 		iscsi_conn_close_luns(conn);
638 	}
639 
640 	assert(spdk_io_channel_get_thread(spdk_io_channel_from_ctx(conn->pg)) ==
641 	       spdk_get_thread());
642 }
643 
644 static int
645 _iscsi_conn_check_shutdown(void *arg)
646 {
647 	struct spdk_iscsi_conn *conn = arg;
648 	int rc;
649 
650 	rc = iscsi_conn_free_tasks(conn);
651 	if (rc < 0) {
652 		return SPDK_POLLER_BUSY;
653 	}
654 
655 	spdk_poller_unregister(&conn->shutdown_timer);
656 
657 	iscsi_conn_stop(conn);
658 	iscsi_conn_free(conn);
659 
660 	return SPDK_POLLER_BUSY;
661 }
662 
663 static void
664 _iscsi_conn_destruct(struct spdk_iscsi_conn *conn)
665 {
666 	int rc;
667 
668 	iscsi_clear_all_transfer_task(conn, NULL, NULL);
669 
670 	iscsi_poll_group_remove_conn(conn->pg, conn);
671 	spdk_sock_close(&conn->sock);
672 	spdk_poller_unregister(&conn->logout_request_timer);
673 	spdk_poller_unregister(&conn->logout_timer);
674 
675 	rc = iscsi_conn_free_tasks(conn);
676 	if (rc < 0) {
677 		/* The connection cannot be freed yet. Check back later. */
678 		conn->shutdown_timer = SPDK_POLLER_REGISTER(_iscsi_conn_check_shutdown, conn, 1000);
679 	} else {
680 		iscsi_conn_stop(conn);
681 		iscsi_conn_free(conn);
682 	}
683 }
684 
685 static int
686 _iscsi_conn_check_pending_tasks(void *arg)
687 {
688 	struct spdk_iscsi_conn *conn = arg;
689 
690 	if (conn->dev != NULL &&
691 	    spdk_scsi_dev_has_pending_tasks(conn->dev, conn->initiator_port)) {
692 		return SPDK_POLLER_BUSY;
693 	}
694 
695 	spdk_poller_unregister(&conn->shutdown_timer);
696 
697 	_iscsi_conn_destruct(conn);
698 
699 	return SPDK_POLLER_BUSY;
700 }
701 
702 void
703 iscsi_conn_destruct(struct spdk_iscsi_conn *conn)
704 {
705 	struct spdk_iscsi_pdu *pdu;
706 	struct spdk_iscsi_task *task;
707 	int opcode;
708 
709 	/* If a connection is already in exited status, just return */
710 	if (conn->state >= ISCSI_CONN_STATE_EXITED) {
711 		return;
712 	}
713 
714 	conn->state = ISCSI_CONN_STATE_EXITED;
715 
716 	/*
717 	 * Each connection pre-allocates its next PDU - make sure these get
718 	 *  freed here.
719 	 */
720 	pdu = conn->pdu_in_progress;
721 	if (pdu) {
722 		/* remove the task left in the PDU too. */
723 		task = pdu->task;
724 		if (task) {
725 			opcode = pdu->bhs.opcode;
726 			switch (opcode) {
727 			case ISCSI_OP_SCSI:
728 			case ISCSI_OP_SCSI_DATAOUT:
729 				spdk_scsi_task_process_abort(&task->scsi);
730 				iscsi_task_cpl(&task->scsi);
731 				break;
732 			default:
733 				SPDK_ERRLOG("unexpected opcode %x\n", opcode);
734 				iscsi_task_put(task);
735 				break;
736 			}
737 		}
738 		iscsi_put_pdu(pdu);
739 		conn->pdu_in_progress = NULL;
740 	}
741 
742 	if (conn->sess != NULL && conn->pending_task_cnt > 0) {
743 		iscsi_conn_cleanup_backend(conn);
744 	}
745 
746 	if (conn->dev != NULL &&
747 	    spdk_scsi_dev_has_pending_tasks(conn->dev, conn->initiator_port)) {
748 		conn->shutdown_timer = SPDK_POLLER_REGISTER(_iscsi_conn_check_pending_tasks, conn, 1000);
749 	} else {
750 		_iscsi_conn_destruct(conn);
751 	}
752 }
753 
754 int
755 iscsi_get_active_conns(struct spdk_iscsi_tgt_node *target)
756 {
757 	struct spdk_iscsi_conn *conn;
758 	int num = 0;
759 
760 	if (g_conns_array == MAP_FAILED) {
761 		return 0;
762 	}
763 
764 	pthread_mutex_lock(&g_conns_mutex);
765 	TAILQ_FOREACH(conn, &g_active_conns, conn_link) {
766 		if (target == NULL || conn->target == target) {
767 			num++;
768 		}
769 	}
770 	pthread_mutex_unlock(&g_conns_mutex);
771 	return num;
772 }
773 
774 static void
775 iscsi_conn_check_shutdown_cb(void *arg1)
776 {
777 	_iscsi_conns_cleanup();
778 	shutdown_iscsi_conns_done();
779 }
780 
781 static int
782 iscsi_conn_check_shutdown(void *arg)
783 {
784 	if (iscsi_get_active_conns(NULL) != 0) {
785 		return SPDK_POLLER_BUSY;
786 	}
787 
788 	spdk_poller_unregister(&g_shutdown_timer);
789 
790 	spdk_thread_send_msg(spdk_get_thread(), iscsi_conn_check_shutdown_cb, NULL);
791 
792 	return SPDK_POLLER_BUSY;
793 }
794 
795 static void
796 iscsi_send_logout_request(struct spdk_iscsi_conn *conn)
797 {
798 	struct spdk_iscsi_pdu *rsp_pdu;
799 	struct iscsi_bhs_async *rsph;
800 
801 	rsp_pdu = iscsi_get_pdu(conn);
802 	assert(rsp_pdu != NULL);
803 
804 	rsph = (struct iscsi_bhs_async *)&rsp_pdu->bhs;
805 	rsp_pdu->data = NULL;
806 
807 	rsph->opcode = ISCSI_OP_ASYNC;
808 	to_be32(&rsph->ffffffff, 0xFFFFFFFF);
809 	rsph->async_event = 1;
810 	to_be16(&rsph->param3, ISCSI_LOGOUT_REQUEST_TIMEOUT);
811 
812 	to_be32(&rsph->stat_sn, conn->StatSN);
813 	to_be32(&rsph->exp_cmd_sn, conn->sess->ExpCmdSN);
814 	to_be32(&rsph->max_cmd_sn, conn->sess->MaxCmdSN);
815 
816 	iscsi_conn_write_pdu(conn, rsp_pdu, iscsi_conn_pdu_generic_complete, NULL);
817 }
818 
819 static int
820 logout_request_timeout(void *arg)
821 {
822 	struct spdk_iscsi_conn *conn = arg;
823 
824 	if (conn->state < ISCSI_CONN_STATE_EXITING) {
825 		conn->state = ISCSI_CONN_STATE_EXITING;
826 	}
827 
828 	return SPDK_POLLER_BUSY;
829 }
830 
831 /* If the connection is running and logout is not requested yet, request logout
832  * to initiator and wait for the logout process to start.
833  */
834 static void
835 _iscsi_conn_request_logout(void *ctx)
836 {
837 	struct spdk_iscsi_conn *conn = ctx;
838 
839 	if (conn->state > ISCSI_CONN_STATE_RUNNING ||
840 	    conn->logout_request_timer != NULL) {
841 		return;
842 	}
843 
844 	iscsi_send_logout_request(conn);
845 
846 	conn->logout_request_timer = SPDK_POLLER_REGISTER(logout_request_timeout,
847 				     conn, ISCSI_LOGOUT_REQUEST_TIMEOUT * 1000000);
848 }
849 
850 static void
851 iscsi_conn_request_logout(struct spdk_iscsi_conn *conn)
852 {
853 	struct spdk_thread *thread;
854 
855 	if (conn->state == ISCSI_CONN_STATE_INVALID) {
856 		/* Move it to EXITING state if the connection is in login. */
857 		conn->state = ISCSI_CONN_STATE_EXITING;
858 	} else if (conn->state == ISCSI_CONN_STATE_RUNNING &&
859 		   conn->logout_request_timer == NULL) {
860 		thread = spdk_io_channel_get_thread(spdk_io_channel_from_ctx(conn->pg));
861 		spdk_thread_send_msg(thread, _iscsi_conn_request_logout, conn);
862 	}
863 }
864 
865 void
866 iscsi_conns_request_logout(struct spdk_iscsi_tgt_node *target)
867 {
868 	struct spdk_iscsi_conn	*conn;
869 
870 	if (g_conns_array == MAP_FAILED) {
871 		return;
872 	}
873 
874 	pthread_mutex_lock(&g_conns_mutex);
875 	TAILQ_FOREACH(conn, &g_active_conns, conn_link) {
876 		if (target == NULL || conn->target == target) {
877 			iscsi_conn_request_logout(conn);
878 		}
879 	}
880 	pthread_mutex_unlock(&g_conns_mutex);
881 }
882 
883 void
884 shutdown_iscsi_conns(void)
885 {
886 	iscsi_conns_request_logout(NULL);
887 
888 	g_shutdown_timer = SPDK_POLLER_REGISTER(iscsi_conn_check_shutdown, NULL, 1000);
889 }
890 
891 /* Do not set conn->state if the connection has already started exiting.
892  *  This ensures we do not move a connection from EXITED state back to EXITING.
893  */
894 static void
895 _iscsi_conn_drop(void *ctx)
896 {
897 	struct spdk_iscsi_conn *conn = ctx;
898 
899 	if (conn->state < ISCSI_CONN_STATE_EXITING) {
900 		conn->state = ISCSI_CONN_STATE_EXITING;
901 	}
902 }
903 
904 int
905 iscsi_drop_conns(struct spdk_iscsi_conn *conn, const char *conn_match,
906 		 int drop_all)
907 {
908 	struct spdk_iscsi_conn	*xconn;
909 	const char		*xconn_match;
910 	struct spdk_thread	*thread;
911 	int			num;
912 
913 	SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "iscsi_drop_conns\n");
914 
915 	num = 0;
916 	pthread_mutex_lock(&g_conns_mutex);
917 	if (g_conns_array == MAP_FAILED) {
918 		goto exit;
919 	}
920 
921 	TAILQ_FOREACH(xconn, &g_active_conns, conn_link) {
922 		if (xconn == conn) {
923 			continue;
924 		}
925 
926 		if (!drop_all && xconn->initiator_port == NULL) {
927 			continue;
928 		}
929 
930 		xconn_match =
931 			drop_all ? xconn->initiator_name : spdk_scsi_port_get_name(xconn->initiator_port);
932 
933 		if (!strcasecmp(conn_match, xconn_match) &&
934 		    conn->target == xconn->target) {
935 
936 			if (num == 0) {
937 				/*
938 				 * Only print this message before we report the
939 				 *  first dropped connection.
940 				 */
941 				SPDK_ERRLOG("drop old connections %s by %s\n",
942 					    conn->target->name, conn_match);
943 			}
944 
945 			SPDK_ERRLOG("exiting conn by %s (%s)\n",
946 				    xconn_match, xconn->initiator_addr);
947 			if (xconn->sess != NULL) {
948 				SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "TSIH=%u\n", xconn->sess->tsih);
949 			} else {
950 				SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "TSIH=xx\n");
951 			}
952 
953 			SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "CID=%u\n", xconn->cid);
954 
955 			thread = spdk_io_channel_get_thread(spdk_io_channel_from_ctx(xconn->pg));
956 			spdk_thread_send_msg(thread, _iscsi_conn_drop, xconn);
957 
958 			num++;
959 		}
960 	}
961 
962 exit:
963 	pthread_mutex_unlock(&g_conns_mutex);
964 
965 	if (num != 0) {
966 		SPDK_ERRLOG("exiting %d conns\n", num);
967 	}
968 
969 	return 0;
970 }
971 
972 static int
973 _iscsi_conn_abort_queued_datain_task(struct spdk_iscsi_conn *conn,
974 				     struct spdk_iscsi_task *task)
975 {
976 	struct spdk_iscsi_task *subtask;
977 	uint32_t remaining_size;
978 
979 	if (conn->data_in_cnt >= MAX_LARGE_DATAIN_PER_CONNECTION) {
980 		return -1;
981 	}
982 
983 	assert(task->current_datain_offset <= task->scsi.transfer_len);
984 	/* Stop split and abort read I/O for remaining data. */
985 	if (task->current_datain_offset < task->scsi.transfer_len) {
986 		remaining_size = task->scsi.transfer_len - task->current_datain_offset;
987 		subtask = iscsi_task_get(conn, task, iscsi_task_cpl);
988 		assert(subtask != NULL);
989 		subtask->scsi.offset = task->current_datain_offset;
990 		subtask->scsi.length = remaining_size;
991 		spdk_scsi_task_set_data(&subtask->scsi, NULL, 0);
992 		task->current_datain_offset += subtask->scsi.length;
993 
994 		subtask->scsi.transfer_len = subtask->scsi.length;
995 		spdk_scsi_task_process_abort(&subtask->scsi);
996 		iscsi_task_cpl(&subtask->scsi);
997 	}
998 
999 	/* Remove the primary task from the list because all subtasks are submitted
1000 	 *  or aborted.
1001 	 */
1002 	assert(task->current_datain_offset == task->scsi.transfer_len);
1003 	TAILQ_REMOVE(&conn->queued_datain_tasks, task, link);
1004 	return 0;
1005 }
1006 
1007 int
1008 iscsi_conn_abort_queued_datain_task(struct spdk_iscsi_conn *conn,
1009 				    uint32_t ref_task_tag)
1010 {
1011 	struct spdk_iscsi_task *task;
1012 
1013 	TAILQ_FOREACH(task, &conn->queued_datain_tasks, link) {
1014 		if (task->tag == ref_task_tag) {
1015 			return _iscsi_conn_abort_queued_datain_task(conn, task);
1016 		}
1017 	}
1018 
1019 	return 0;
1020 }
1021 
1022 int
1023 iscsi_conn_abort_queued_datain_tasks(struct spdk_iscsi_conn *conn,
1024 				     struct spdk_scsi_lun *lun,
1025 				     struct spdk_iscsi_pdu *pdu)
1026 {
1027 	struct spdk_iscsi_task *task, *task_tmp;
1028 	struct spdk_iscsi_pdu *pdu_tmp;
1029 	int rc;
1030 
1031 	TAILQ_FOREACH_SAFE(task, &conn->queued_datain_tasks, link, task_tmp) {
1032 		pdu_tmp = iscsi_task_get_pdu(task);
1033 		if ((lun == NULL || lun == task->scsi.lun) &&
1034 		    (pdu == NULL || (spdk_sn32_lt(pdu_tmp->cmd_sn, pdu->cmd_sn)))) {
1035 			rc = _iscsi_conn_abort_queued_datain_task(conn, task);
1036 			if (rc != 0) {
1037 				return rc;
1038 			}
1039 		}
1040 	}
1041 
1042 	return 0;
1043 }
1044 
1045 int
1046 iscsi_conn_handle_queued_datain_tasks(struct spdk_iscsi_conn *conn)
1047 {
1048 	struct spdk_iscsi_task *task;
1049 
1050 	while (!TAILQ_EMPTY(&conn->queued_datain_tasks) &&
1051 	       conn->data_in_cnt < MAX_LARGE_DATAIN_PER_CONNECTION) {
1052 		task = TAILQ_FIRST(&conn->queued_datain_tasks);
1053 		assert(task->current_datain_offset <= task->scsi.transfer_len);
1054 		if (task->current_datain_offset < task->scsi.transfer_len) {
1055 			struct spdk_iscsi_task *subtask;
1056 			uint32_t remaining_size = 0;
1057 
1058 			remaining_size = task->scsi.transfer_len - task->current_datain_offset;
1059 			subtask = iscsi_task_get(conn, task, iscsi_task_cpl);
1060 			assert(subtask != NULL);
1061 			subtask->scsi.offset = task->current_datain_offset;
1062 			spdk_scsi_task_set_data(&subtask->scsi, NULL, 0);
1063 
1064 			if (spdk_scsi_dev_get_lun(conn->dev, task->lun_id) == NULL) {
1065 				/* Stop submitting split read I/Os for remaining data. */
1066 				TAILQ_REMOVE(&conn->queued_datain_tasks, task, link);
1067 				task->current_datain_offset += remaining_size;
1068 				assert(task->current_datain_offset == task->scsi.transfer_len);
1069 				subtask->scsi.transfer_len = remaining_size;
1070 				spdk_scsi_task_process_null_lun(&subtask->scsi);
1071 				iscsi_task_cpl(&subtask->scsi);
1072 				return 0;
1073 			}
1074 
1075 			subtask->scsi.length = spdk_min(SPDK_BDEV_LARGE_BUF_MAX_SIZE, remaining_size);
1076 			task->current_datain_offset += subtask->scsi.length;
1077 			iscsi_queue_task(conn, subtask);
1078 		}
1079 		if (task->current_datain_offset == task->scsi.transfer_len) {
1080 			TAILQ_REMOVE(&conn->queued_datain_tasks, task, link);
1081 		}
1082 	}
1083 	return 0;
1084 }
1085 
1086 void
1087 iscsi_task_mgmt_cpl(struct spdk_scsi_task *scsi_task)
1088 {
1089 	struct spdk_iscsi_task *task = iscsi_task_from_scsi_task(scsi_task);
1090 
1091 	iscsi_task_mgmt_response(task->conn, task);
1092 	iscsi_task_put(task);
1093 }
1094 
1095 static void
1096 iscsi_task_copy_to_rsp_scsi_status(struct spdk_iscsi_task *primary,
1097 				   struct spdk_scsi_task *task)
1098 {
1099 	memcpy(primary->rsp_sense_data, task->sense_data, task->sense_data_len);
1100 	primary->rsp_sense_data_len = task->sense_data_len;
1101 	primary->rsp_scsi_status = task->status;
1102 }
1103 
1104 static void
1105 iscsi_task_copy_from_rsp_scsi_status(struct spdk_scsi_task *task,
1106 				     struct spdk_iscsi_task *primary)
1107 {
1108 	memcpy(task->sense_data, primary->rsp_sense_data,
1109 	       primary->rsp_sense_data_len);
1110 	task->sense_data_len = primary->rsp_sense_data_len;
1111 	task->status = primary->rsp_scsi_status;
1112 }
1113 
1114 static void
1115 process_completed_read_subtask_list(struct spdk_iscsi_conn *conn,
1116 				    struct spdk_iscsi_task *primary)
1117 {
1118 	struct spdk_iscsi_task *subtask, *tmp;
1119 
1120 	TAILQ_FOREACH_SAFE(subtask, &primary->subtask_list, subtask_link, tmp) {
1121 		if (subtask->scsi.offset == primary->bytes_completed) {
1122 			TAILQ_REMOVE(&primary->subtask_list, subtask, subtask_link);
1123 			primary->bytes_completed += subtask->scsi.length;
1124 			iscsi_task_response(conn, subtask);
1125 			iscsi_task_put(subtask);
1126 		} else {
1127 			break;
1128 		}
1129 	}
1130 
1131 	if (primary->bytes_completed == primary->scsi.transfer_len) {
1132 		iscsi_task_put(primary);
1133 	}
1134 }
1135 
1136 static void
1137 process_read_task_completion(struct spdk_iscsi_conn *conn,
1138 			     struct spdk_iscsi_task *task,
1139 			     struct spdk_iscsi_task *primary)
1140 {
1141 	struct spdk_iscsi_task *tmp;
1142 
1143 	/* If the status of the completed subtask is the first failure,
1144 	 * copy it to out-of-order subtasks and remember it as the status
1145 	 * of the command,
1146 	 *
1147 	 * Even if the status of the completed task is success,
1148 	 * there are any failed subtask ever, copy the first failed status
1149 	 * to it.
1150 	 */
1151 	if (task->scsi.status != SPDK_SCSI_STATUS_GOOD) {
1152 		if (primary->rsp_scsi_status == SPDK_SCSI_STATUS_GOOD) {
1153 			TAILQ_FOREACH(tmp, &primary->subtask_list, subtask_link) {
1154 				spdk_scsi_task_copy_status(&tmp->scsi, &task->scsi);
1155 			}
1156 			iscsi_task_copy_to_rsp_scsi_status(primary, &task->scsi);
1157 		}
1158 	} else if (primary->rsp_scsi_status != SPDK_SCSI_STATUS_GOOD) {
1159 		iscsi_task_copy_from_rsp_scsi_status(&task->scsi, primary);
1160 	}
1161 
1162 	if (task == primary) {
1163 		primary->bytes_completed = task->scsi.length;
1164 		/* For non split read I/O */
1165 		assert(primary->bytes_completed == task->scsi.transfer_len);
1166 		iscsi_task_response(conn, task);
1167 		iscsi_task_put(task);
1168 	} else {
1169 		if (task->scsi.offset != primary->bytes_completed) {
1170 			TAILQ_FOREACH(tmp, &primary->subtask_list, subtask_link) {
1171 				if (task->scsi.offset < tmp->scsi.offset) {
1172 					TAILQ_INSERT_BEFORE(tmp, task, subtask_link);
1173 					return;
1174 				}
1175 			}
1176 
1177 			TAILQ_INSERT_TAIL(&primary->subtask_list, task, subtask_link);
1178 		} else {
1179 			TAILQ_INSERT_HEAD(&primary->subtask_list, task, subtask_link);
1180 			process_completed_read_subtask_list(conn, primary);
1181 		}
1182 	}
1183 }
1184 
1185 static void
1186 process_non_read_task_completion(struct spdk_iscsi_conn *conn,
1187 				 struct spdk_iscsi_task *task,
1188 				 struct spdk_iscsi_task *primary)
1189 {
1190 	primary->bytes_completed += task->scsi.length;
1191 
1192 	/* If the status of the subtask is the first failure, remember it as
1193 	 * the status of the command and set it to the status of the primary
1194 	 * task later.
1195 	 *
1196 	 * If the first failed task is the primary, two copies can be avoided
1197 	 * but code simplicity is prioritized.
1198 	 */
1199 	if (task->scsi.status == SPDK_SCSI_STATUS_GOOD) {
1200 		if (task != primary) {
1201 			primary->scsi.data_transferred += task->scsi.data_transferred;
1202 		}
1203 	} else if (primary->rsp_scsi_status == SPDK_SCSI_STATUS_GOOD) {
1204 		iscsi_task_copy_to_rsp_scsi_status(primary, &task->scsi);
1205 	}
1206 
1207 	if (primary->bytes_completed == primary->scsi.transfer_len) {
1208 		/*
1209 		 * Check if this is the last task completed for an iSCSI write
1210 		 *  that required child subtasks.  If task != primary, we know
1211 		 *  for sure that it was part of an iSCSI write with child subtasks.
1212 		 *  The trickier case is when the last task completed was the initial
1213 		 *  task - in this case the task will have a smaller length than
1214 		 *  the overall transfer length.
1215 		 */
1216 		if (task != primary || task->scsi.length != task->scsi.transfer_len) {
1217 			/* If LUN is removed in the middle of the iSCSI write sequence,
1218 			 *  primary might complete the write to the initiator because it is not
1219 			 *  ensured that the initiator will send all data requested by R2Ts.
1220 			 *
1221 			 * We check it and skip the following if primary is completed. (see
1222 			 *  iscsi_clear_all_transfer_task() in iscsi.c.)
1223 			 */
1224 			if (primary->is_r2t_active) {
1225 				if (primary->rsp_scsi_status != SPDK_SCSI_STATUS_GOOD) {
1226 					iscsi_task_copy_from_rsp_scsi_status(&primary->scsi, primary);
1227 				}
1228 				iscsi_task_response(conn, primary);
1229 				iscsi_del_transfer_task(conn, primary->tag);
1230 			}
1231 		} else {
1232 			iscsi_task_response(conn, task);
1233 		}
1234 	}
1235 	iscsi_task_put(task);
1236 }
1237 
1238 void
1239 iscsi_task_cpl(struct spdk_scsi_task *scsi_task)
1240 {
1241 	struct spdk_iscsi_task *primary;
1242 	struct spdk_iscsi_task *task = iscsi_task_from_scsi_task(scsi_task);
1243 	struct spdk_iscsi_conn *conn = task->conn;
1244 	struct spdk_iscsi_pdu *pdu = task->pdu;
1245 
1246 	spdk_trace_record(TRACE_ISCSI_TASK_DONE, conn->id, 0, (uintptr_t)task, 0);
1247 
1248 	task->is_queued = false;
1249 	primary = iscsi_task_get_primary(task);
1250 
1251 	if (iscsi_task_is_read(primary)) {
1252 		process_read_task_completion(conn, task, primary);
1253 	} else {
1254 		process_non_read_task_completion(conn, task, primary);
1255 	}
1256 	if (!task->parent) {
1257 		spdk_trace_record(TRACE_ISCSI_PDU_COMPLETED, 0, 0, (uintptr_t)pdu, 0);
1258 	}
1259 }
1260 
1261 static void
1262 iscsi_conn_send_nopin(struct spdk_iscsi_conn *conn)
1263 {
1264 	struct spdk_iscsi_pdu *rsp_pdu;
1265 	struct iscsi_bhs_nop_in *rsp;
1266 	/* Only send nopin if we have logged in and are in a normal session. */
1267 	if (conn->sess == NULL ||
1268 	    !conn->full_feature ||
1269 	    !iscsi_param_eq_val(conn->sess->params, "SessionType", "Normal")) {
1270 		return;
1271 	}
1272 	SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "send NOPIN isid=%"PRIx64", tsih=%u, cid=%u\n",
1273 		      conn->sess->isid, conn->sess->tsih, conn->cid);
1274 	SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "StatSN=%u, ExpCmdSN=%u, MaxCmdSN=%u\n",
1275 		      conn->StatSN, conn->sess->ExpCmdSN,
1276 		      conn->sess->MaxCmdSN);
1277 	rsp_pdu = iscsi_get_pdu(conn);
1278 	rsp = (struct iscsi_bhs_nop_in *) &rsp_pdu->bhs;
1279 	rsp_pdu->data = NULL;
1280 	/*
1281 	 * iscsi_get_pdu() memset's the PDU for us, so only fill out the needed
1282 	 *  fields.
1283 	 */
1284 	rsp->opcode = ISCSI_OP_NOPIN;
1285 	rsp->flags = 0x80;
1286 	/*
1287 	 * Technically the to_be32() is not needed here, since
1288 	 *  to_be32(0xFFFFFFFU) returns 0xFFFFFFFFU.
1289 	 */
1290 	to_be32(&rsp->itt, 0xFFFFFFFFU);
1291 	to_be32(&rsp->ttt, conn->id);
1292 	to_be32(&rsp->stat_sn, conn->StatSN);
1293 	to_be32(&rsp->exp_cmd_sn, conn->sess->ExpCmdSN);
1294 	to_be32(&rsp->max_cmd_sn, conn->sess->MaxCmdSN);
1295 	iscsi_conn_write_pdu(conn, rsp_pdu, iscsi_conn_pdu_generic_complete, NULL);
1296 	conn->last_nopin = spdk_get_ticks();
1297 	conn->nop_outstanding = true;
1298 }
1299 
1300 void
1301 iscsi_conn_handle_nop(struct spdk_iscsi_conn *conn)
1302 {
1303 	uint64_t	tsc;
1304 
1305 	/**
1306 	  * This function will be executed by nop_poller of iSCSI polling group, so
1307 	  * we need to check the connection state first, then do the nop interval
1308 	  * expiration check work.
1309 	  */
1310 	if ((conn->state == ISCSI_CONN_STATE_EXITED) ||
1311 	    (conn->state == ISCSI_CONN_STATE_EXITING)) {
1312 		return;
1313 	}
1314 
1315 	/* Check for nop interval expiration */
1316 	tsc = spdk_get_ticks();
1317 	if (conn->nop_outstanding) {
1318 		if ((tsc - conn->last_nopin) > conn->timeout) {
1319 			SPDK_ERRLOG("Timed out waiting for NOP-Out response from initiator\n");
1320 			SPDK_ERRLOG("  tsc=0x%lx, last_nopin=0x%lx\n", tsc, conn->last_nopin);
1321 			SPDK_ERRLOG("  initiator=%s, target=%s\n", conn->initiator_name,
1322 				    conn->target_short_name);
1323 			conn->state = ISCSI_CONN_STATE_EXITING;
1324 		}
1325 	} else if (tsc - conn->last_nopin > conn->nopininterval) {
1326 		iscsi_conn_send_nopin(conn);
1327 	}
1328 }
1329 
1330 /**
1331  * \brief Reads data for the specified iSCSI connection from its TCP socket.
1332  *
1333  * The TCP socket is marked as non-blocking, so this function may not read
1334  * all data requested.
1335  *
1336  * Returns SPDK_ISCSI_CONNECTION_FATAL if the recv() operation indicates a fatal
1337  * error with the TCP connection (including if the TCP connection was closed
1338  * unexpectedly.
1339  *
1340  * Otherwise returns the number of bytes successfully read.
1341  */
1342 int
1343 iscsi_conn_read_data(struct spdk_iscsi_conn *conn, int bytes,
1344 		     void *buf)
1345 {
1346 	int ret;
1347 
1348 	if (bytes == 0) {
1349 		return 0;
1350 	}
1351 
1352 	ret = spdk_sock_recv(conn->sock, buf, bytes);
1353 
1354 	if (ret > 0) {
1355 		spdk_trace_record(TRACE_ISCSI_READ_FROM_SOCKET_DONE, conn->id, ret, 0, 0);
1356 		return ret;
1357 	}
1358 
1359 	if (ret < 0) {
1360 		if (errno == EAGAIN || errno == EWOULDBLOCK) {
1361 			return 0;
1362 		}
1363 
1364 		/* For connect reset issue, do not output error log */
1365 		if (errno == ECONNRESET) {
1366 			SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "spdk_sock_recv() failed, errno %d: %s\n",
1367 				      errno, spdk_strerror(errno));
1368 		} else {
1369 			SPDK_ERRLOG("spdk_sock_recv() failed, errno %d: %s\n",
1370 				    errno, spdk_strerror(errno));
1371 		}
1372 	}
1373 
1374 	/* connection closed */
1375 	return SPDK_ISCSI_CONNECTION_FATAL;
1376 }
1377 
1378 int
1379 iscsi_conn_readv_data(struct spdk_iscsi_conn *conn,
1380 		      struct iovec *iov, int iovcnt)
1381 {
1382 	int ret;
1383 
1384 	if (iov == NULL || iovcnt == 0) {
1385 		return 0;
1386 	}
1387 
1388 	if (iovcnt == 1) {
1389 		return iscsi_conn_read_data(conn, iov[0].iov_len,
1390 					    iov[0].iov_base);
1391 	}
1392 
1393 	ret = spdk_sock_readv(conn->sock, iov, iovcnt);
1394 
1395 	if (ret > 0) {
1396 		spdk_trace_record(TRACE_ISCSI_READ_FROM_SOCKET_DONE, conn->id, ret, 0, 0);
1397 		return ret;
1398 	}
1399 
1400 	if (ret < 0) {
1401 		if (errno == EAGAIN || errno == EWOULDBLOCK) {
1402 			return 0;
1403 		}
1404 
1405 		/* For connect reset issue, do not output error log */
1406 		if (errno == ECONNRESET) {
1407 			SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "spdk_sock_readv() failed, errno %d: %s\n",
1408 				      errno, spdk_strerror(errno));
1409 		} else {
1410 			SPDK_ERRLOG("spdk_sock_readv() failed, errno %d: %s\n",
1411 				    errno, spdk_strerror(errno));
1412 		}
1413 	}
1414 
1415 	/* connection closed */
1416 	return SPDK_ISCSI_CONNECTION_FATAL;
1417 }
1418 
1419 static bool
1420 iscsi_is_free_pdu_deferred(struct spdk_iscsi_pdu *pdu)
1421 {
1422 	if (pdu == NULL) {
1423 		return false;
1424 	}
1425 
1426 	if (pdu->bhs.opcode == ISCSI_OP_R2T ||
1427 	    pdu->bhs.opcode == ISCSI_OP_SCSI_DATAIN) {
1428 		return true;
1429 	}
1430 
1431 	return false;
1432 }
1433 
1434 static int
1435 iscsi_dif_verify(struct spdk_iscsi_pdu *pdu, struct spdk_dif_ctx *dif_ctx)
1436 {
1437 	struct iovec iov;
1438 	struct spdk_dif_error err_blk = {};
1439 	uint32_t num_blocks;
1440 	int rc;
1441 
1442 	iov.iov_base = pdu->data;
1443 	iov.iov_len = pdu->data_buf_len;
1444 	num_blocks = pdu->data_buf_len / dif_ctx->block_size;
1445 
1446 	rc = spdk_dif_verify(&iov, 1, num_blocks, dif_ctx, &err_blk);
1447 	if (rc != 0) {
1448 		SPDK_ERRLOG("DIF error detected. type=%d, offset=%" PRIu32 "\n",
1449 			    err_blk.err_type, err_blk.err_offset);
1450 	}
1451 
1452 	return rc;
1453 }
1454 
1455 static void
1456 _iscsi_conn_pdu_write_done(void *cb_arg, int err)
1457 {
1458 	struct spdk_iscsi_pdu *pdu = cb_arg;
1459 	struct spdk_iscsi_conn *conn = pdu->conn;
1460 
1461 	assert(conn != NULL);
1462 
1463 	if (spdk_unlikely(conn->state >= ISCSI_CONN_STATE_EXITING)) {
1464 		/* The other policy will recycle the resource */
1465 		return;
1466 	}
1467 
1468 	TAILQ_REMOVE(&conn->write_pdu_list, pdu, tailq);
1469 
1470 	if (err != 0) {
1471 		conn->state = ISCSI_CONN_STATE_EXITING;
1472 	} else {
1473 		spdk_trace_record(TRACE_ISCSI_FLUSH_WRITEBUF_DONE, conn->id, pdu->mapped_length, (uintptr_t)pdu, 0);
1474 	}
1475 
1476 	if ((conn->full_feature) &&
1477 	    (conn->sess->ErrorRecoveryLevel >= 1) &&
1478 	    iscsi_is_free_pdu_deferred(pdu)) {
1479 		SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "stat_sn=%d\n",
1480 			      from_be32(&pdu->bhs.stat_sn));
1481 		TAILQ_INSERT_TAIL(&conn->snack_pdu_list, pdu,
1482 				  tailq);
1483 	} else {
1484 		iscsi_conn_free_pdu(conn, pdu);
1485 	}
1486 }
1487 
1488 void
1489 iscsi_conn_pdu_generic_complete(void *cb_arg)
1490 {
1491 }
1492 
1493 void
1494 iscsi_conn_write_pdu(struct spdk_iscsi_conn *conn, struct spdk_iscsi_pdu *pdu,
1495 		     iscsi_conn_xfer_complete_cb cb_fn,
1496 		     void *cb_arg)
1497 {
1498 	uint32_t crc32c;
1499 	ssize_t rc;
1500 
1501 	if (spdk_unlikely(pdu->dif_insert_or_strip)) {
1502 		rc = iscsi_dif_verify(pdu, &pdu->dif_ctx);
1503 		if (rc != 0) {
1504 			iscsi_conn_free_pdu(conn, pdu);
1505 			conn->state = ISCSI_CONN_STATE_EXITING;
1506 			return;
1507 		}
1508 	}
1509 
1510 	if (pdu->bhs.opcode != ISCSI_OP_LOGIN_RSP) {
1511 		/* Header Digest */
1512 		if (conn->header_digest) {
1513 			crc32c = iscsi_pdu_calc_header_digest(pdu);
1514 			MAKE_DIGEST_WORD(pdu->header_digest, crc32c);
1515 		}
1516 
1517 		/* Data Digest */
1518 		if (conn->data_digest && DGET24(pdu->bhs.data_segment_len) != 0) {
1519 			crc32c = iscsi_pdu_calc_data_digest(pdu);
1520 			MAKE_DIGEST_WORD(pdu->data_digest, crc32c);
1521 		}
1522 	}
1523 
1524 	pdu->cb_fn = cb_fn;
1525 	pdu->cb_arg = cb_arg;
1526 	TAILQ_INSERT_TAIL(&conn->write_pdu_list, pdu, tailq);
1527 
1528 	if (spdk_unlikely(conn->state >= ISCSI_CONN_STATE_EXITING)) {
1529 		return;
1530 	}
1531 	pdu->sock_req.iovcnt = iscsi_build_iovs(conn, pdu->iov, SPDK_COUNTOF(pdu->iov), pdu,
1532 						&pdu->mapped_length);
1533 	pdu->sock_req.cb_fn = _iscsi_conn_pdu_write_done;
1534 	pdu->sock_req.cb_arg = pdu;
1535 
1536 	spdk_trace_record(TRACE_ISCSI_FLUSH_WRITEBUF_START, conn->id, pdu->mapped_length, (uintptr_t)pdu,
1537 			  pdu->sock_req.iovcnt);
1538 	spdk_sock_writev_async(conn->sock, &pdu->sock_req);
1539 }
1540 
1541 static void
1542 iscsi_conn_sock_cb(void *arg, struct spdk_sock_group *group, struct spdk_sock *sock)
1543 {
1544 	struct spdk_iscsi_conn *conn = arg;
1545 	int rc;
1546 
1547 	assert(conn != NULL);
1548 
1549 	if ((conn->state == ISCSI_CONN_STATE_EXITED) ||
1550 	    (conn->state == ISCSI_CONN_STATE_EXITING)) {
1551 		return;
1552 	}
1553 
1554 	/* Handle incoming PDUs */
1555 	rc = iscsi_handle_incoming_pdus(conn);
1556 	if (rc < 0) {
1557 		conn->state = ISCSI_CONN_STATE_EXITING;
1558 	}
1559 }
1560 
1561 static void
1562 iscsi_conn_full_feature_migrate(void *arg)
1563 {
1564 	struct spdk_iscsi_conn *conn = arg;
1565 
1566 	if (conn->state >= ISCSI_CONN_STATE_EXITING) {
1567 		/* Connection is being exited before this callback is executed. */
1568 		SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "Connection is already exited.\n");
1569 		return;
1570 	}
1571 
1572 	if (conn->sess->session_type == SESSION_TYPE_NORMAL) {
1573 		iscsi_conn_open_luns(conn);
1574 	}
1575 
1576 	/* Add this connection to the assigned poll group. */
1577 	iscsi_poll_group_add_conn(conn->pg, conn);
1578 }
1579 
1580 static struct spdk_iscsi_poll_group *g_next_pg = NULL;
1581 
1582 void
1583 iscsi_conn_schedule(struct spdk_iscsi_conn *conn)
1584 {
1585 	struct spdk_iscsi_poll_group	*pg;
1586 	struct spdk_iscsi_tgt_node	*target;
1587 
1588 	if (conn->sess->session_type != SESSION_TYPE_NORMAL) {
1589 		/* Leave all non-normal sessions on the acceptor
1590 		 * thread. */
1591 		return;
1592 	}
1593 	pthread_mutex_lock(&g_iscsi.mutex);
1594 
1595 	target = conn->sess->target;
1596 	pthread_mutex_lock(&target->mutex);
1597 	target->num_active_conns++;
1598 	if (target->num_active_conns == 1) {
1599 		/**
1600 		 * This is the only active connection for this target node.
1601 		 *  Pick a poll group using round-robin.
1602 		 */
1603 		if (g_next_pg == NULL) {
1604 			g_next_pg = TAILQ_FIRST(&g_iscsi.poll_group_head);
1605 			assert(g_next_pg != NULL);
1606 		}
1607 
1608 		pg = g_next_pg;
1609 		g_next_pg = TAILQ_NEXT(g_next_pg, link);
1610 
1611 		/* Save the pg in the target node so it can be used for any other connections to this target node. */
1612 		target->pg = pg;
1613 	} else {
1614 		/**
1615 		 * There are other active connections for this target node.
1616 		 */
1617 		pg = target->pg;
1618 	}
1619 
1620 	pthread_mutex_unlock(&target->mutex);
1621 	pthread_mutex_unlock(&g_iscsi.mutex);
1622 
1623 	assert(spdk_io_channel_get_thread(spdk_io_channel_from_ctx(conn->pg)) ==
1624 	       spdk_get_thread());
1625 
1626 	/* Remove this connection from the previous poll group */
1627 	iscsi_poll_group_remove_conn(conn->pg, conn);
1628 
1629 	conn->last_nopin = spdk_get_ticks();
1630 	conn->pg = pg;
1631 
1632 	spdk_thread_send_msg(spdk_io_channel_get_thread(spdk_io_channel_from_ctx(pg)),
1633 			     iscsi_conn_full_feature_migrate, conn);
1634 }
1635 
1636 static int
1637 logout_timeout(void *arg)
1638 {
1639 	struct spdk_iscsi_conn *conn = arg;
1640 
1641 	if (conn->state < ISCSI_CONN_STATE_EXITING) {
1642 		conn->state = ISCSI_CONN_STATE_EXITING;
1643 	}
1644 
1645 	return SPDK_POLLER_BUSY;
1646 }
1647 
1648 void
1649 iscsi_conn_logout(struct spdk_iscsi_conn *conn)
1650 {
1651 	conn->is_logged_out = true;
1652 	conn->logout_timer = SPDK_POLLER_REGISTER(logout_timeout, conn, ISCSI_LOGOUT_TIMEOUT * 1000000);
1653 }
1654 
1655 SPDK_TRACE_REGISTER_FN(iscsi_conn_trace, "iscsi_conn", TRACE_GROUP_ISCSI)
1656 {
1657 	spdk_trace_register_owner(OWNER_ISCSI_CONN, 'c');
1658 	spdk_trace_register_object(OBJECT_ISCSI_PDU, 'p');
1659 	spdk_trace_register_description("ISCSI_READ_DONE", TRACE_ISCSI_READ_FROM_SOCKET_DONE,
1660 					OWNER_ISCSI_CONN, OBJECT_NONE, 0, 0, "");
1661 	spdk_trace_register_description("ISCSI_WRITE_START", TRACE_ISCSI_FLUSH_WRITEBUF_START,
1662 					OWNER_ISCSI_CONN, OBJECT_NONE, 0, 0, "iovec: ");
1663 	spdk_trace_register_description("ISCSI_WRITE_DONE", TRACE_ISCSI_FLUSH_WRITEBUF_DONE,
1664 					OWNER_ISCSI_CONN, OBJECT_NONE, 0, 0, "");
1665 	spdk_trace_register_description("ISCSI_READ_PDU", TRACE_ISCSI_READ_PDU,
1666 					OWNER_ISCSI_CONN, OBJECT_ISCSI_PDU, 1, 0, "opc:   ");
1667 	spdk_trace_register_description("ISCSI_TASK_DONE", TRACE_ISCSI_TASK_DONE,
1668 					OWNER_ISCSI_CONN, OBJECT_SCSI_TASK, 0, 0, "");
1669 	spdk_trace_register_description("ISCSI_TASK_QUEUE", TRACE_ISCSI_TASK_QUEUE,
1670 					OWNER_ISCSI_CONN, OBJECT_SCSI_TASK, 1, 1, "pdu:   ");
1671 	spdk_trace_register_description("ISCSI_TASK_EXECUTED", TRACE_ISCSI_TASK_EXECUTED,
1672 					OWNER_ISCSI_CONN, OBJECT_ISCSI_PDU, 0, 0, "");
1673 	spdk_trace_register_description("ISCSI_PDU_COMPLETED", TRACE_ISCSI_PDU_COMPLETED,
1674 					OWNER_ISCSI_CONN, OBJECT_ISCSI_PDU, 0, 0, "");
1675 }
1676 
1677 void
1678 iscsi_conn_info_json(struct spdk_json_write_ctx *w, struct spdk_iscsi_conn *conn)
1679 {
1680 	uint16_t tsih;
1681 
1682 	if (!conn->is_valid) {
1683 		return;
1684 	}
1685 
1686 	spdk_json_write_object_begin(w);
1687 
1688 	spdk_json_write_named_int32(w, "id", conn->id);
1689 
1690 	spdk_json_write_named_int32(w, "cid", conn->cid);
1691 
1692 	/*
1693 	 * If we try to return data for a connection that has not
1694 	 *  logged in yet, the session will not be set.  So in this
1695 	 *  case, return -1 for the tsih rather than segfaulting
1696 	 *  on the null conn->sess.
1697 	 */
1698 	if (conn->sess == NULL) {
1699 		tsih = -1;
1700 	} else {
1701 		tsih = conn->sess->tsih;
1702 	}
1703 	spdk_json_write_named_int32(w, "tsih", tsih);
1704 
1705 	spdk_json_write_named_string(w, "initiator_addr", conn->initiator_addr);
1706 
1707 	spdk_json_write_named_string(w, "target_addr", conn->target_addr);
1708 
1709 	spdk_json_write_named_string(w, "target_node_name", conn->target_short_name);
1710 
1711 	spdk_json_write_named_string(w, "thread_name",
1712 				     spdk_thread_get_name(spdk_get_thread()));
1713 
1714 	spdk_json_write_object_end(w);
1715 }
1716