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