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