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