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