2016-08-02 16:34:45 +00:00
|
|
|
/*-
|
|
|
|
* BSD LICENSE
|
|
|
|
*
|
|
|
|
* Copyright (C) 2008-2012 Daisuke Aoyama <aoyama@peach.ne.jp>.
|
|
|
|
* Copyright (c) Intel Corporation.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* * Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
* * Neither the name of Intel Corporation nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2017-05-02 18:18:25 +00:00
|
|
|
#include "spdk/stdinc.h"
|
2016-08-02 16:34:45 +00:00
|
|
|
|
2016-08-22 22:23:38 +00:00
|
|
|
#include "spdk/endian.h"
|
2016-08-18 19:52:48 +00:00
|
|
|
#include "spdk/env.h"
|
2016-10-04 18:20:29 +00:00
|
|
|
#include "spdk/event.h"
|
2018-06-11 20:32:15 +00:00
|
|
|
#include "spdk/thread.h"
|
2017-05-02 18:18:25 +00:00
|
|
|
#include "spdk/queue.h"
|
2016-08-02 16:34:45 +00:00
|
|
|
#include "spdk/trace.h"
|
|
|
|
#include "spdk/net.h"
|
2018-02-07 19:58:14 +00:00
|
|
|
#include "spdk/sock.h"
|
2017-08-11 23:27:04 +00:00
|
|
|
#include "spdk/string.h"
|
2016-11-07 22:10:28 +00:00
|
|
|
|
|
|
|
#include "spdk_internal/log.h"
|
|
|
|
|
2016-08-02 16:34:45 +00:00
|
|
|
#include "iscsi/task.h"
|
|
|
|
#include "iscsi/conn.h"
|
|
|
|
#include "iscsi/tgt_node.h"
|
|
|
|
#include "iscsi/portal_grp.h"
|
|
|
|
|
|
|
|
#define SPDK_ISCSI_CONNECTION_MEMSET(conn) \
|
|
|
|
memset(&(conn)->portal, 0, sizeof(*(conn)) - \
|
|
|
|
offsetof(struct spdk_iscsi_conn, portal));
|
|
|
|
|
2018-03-14 09:17:38 +00:00
|
|
|
static int g_connections_per_lcore;
|
2017-12-21 03:02:31 +00:00
|
|
|
static uint32_t *g_num_connections;
|
2016-08-02 16:34:45 +00:00
|
|
|
|
2018-03-28 18:07:54 +00:00
|
|
|
struct spdk_iscsi_conn *g_conns_array = MAP_FAILED;
|
2018-04-20 22:12:21 +00:00
|
|
|
static int g_conns_array_fd = -1;
|
2016-08-08 17:48:26 +00:00
|
|
|
static char g_shm_name[64];
|
2016-08-02 16:34:45 +00:00
|
|
|
|
2018-04-06 15:10:30 +00:00
|
|
|
static pthread_mutex_t g_conns_mutex = PTHREAD_MUTEX_INITIALIZER;
|
2016-08-02 16:34:45 +00:00
|
|
|
|
2016-10-04 18:20:29 +00:00
|
|
|
static struct spdk_poller *g_shutdown_timer = NULL;
|
2016-08-02 16:34:45 +00:00
|
|
|
|
2017-12-21 16:48:31 +00:00
|
|
|
static uint32_t spdk_iscsi_conn_allocate_reactor(const struct spdk_cpuset *cpumask);
|
2016-08-02 16:34:45 +00:00
|
|
|
|
2017-01-05 01:19:02 +00:00
|
|
|
static void spdk_iscsi_conn_full_feature_migrate(void *arg1, void *arg2);
|
2018-02-26 03:38:39 +00:00
|
|
|
static void spdk_iscsi_conn_stop(struct spdk_iscsi_conn *conn);
|
2018-02-14 02:26:10 +00:00
|
|
|
static void spdk_iscsi_conn_sock_cb(void *arg, struct spdk_sock_group *group,
|
|
|
|
struct spdk_sock *sock);
|
2016-09-20 22:06:02 +00:00
|
|
|
|
2016-08-02 16:34:45 +00:00
|
|
|
static struct spdk_iscsi_conn *
|
|
|
|
allocate_conn(void)
|
|
|
|
{
|
|
|
|
struct spdk_iscsi_conn *conn;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
pthread_mutex_lock(&g_conns_mutex);
|
|
|
|
for (i = 0; i < MAX_ISCSI_CONNECTIONS; i++) {
|
|
|
|
conn = &g_conns_array[i];
|
|
|
|
if (!conn->is_valid) {
|
|
|
|
SPDK_ISCSI_CONNECTION_MEMSET(conn);
|
|
|
|
conn->is_valid = 1;
|
|
|
|
pthread_mutex_unlock(&g_conns_mutex);
|
|
|
|
return conn;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pthread_mutex_unlock(&g_conns_mutex);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
free_conn(struct spdk_iscsi_conn *conn)
|
|
|
|
{
|
2017-09-30 04:01:07 +00:00
|
|
|
free(conn->portal_host);
|
|
|
|
free(conn->portal_port);
|
2016-08-02 16:34:45 +00:00
|
|
|
conn->is_valid = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct spdk_iscsi_conn *
|
|
|
|
spdk_find_iscsi_connection_by_id(int cid)
|
|
|
|
{
|
|
|
|
if (g_conns_array[cid].is_valid == 1) {
|
|
|
|
return &g_conns_array[cid];
|
|
|
|
} else {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int spdk_initialize_iscsi_conns(void)
|
|
|
|
{
|
2018-03-28 18:07:54 +00:00
|
|
|
size_t conns_size = sizeof(struct spdk_iscsi_conn) * MAX_ISCSI_CONNECTIONS;
|
2017-12-22 00:21:35 +00:00
|
|
|
uint32_t i, last_core;
|
2016-08-02 16:34:45 +00:00
|
|
|
|
2017-08-30 18:06:33 +00:00
|
|
|
SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "spdk_iscsi_init\n");
|
2016-08-02 16:34:45 +00:00
|
|
|
|
2017-07-25 18:02:41 +00:00
|
|
|
snprintf(g_shm_name, sizeof(g_shm_name), "/spdk_iscsi_conns.%d", spdk_app_get_shm_id());
|
2018-04-20 22:12:21 +00:00
|
|
|
g_conns_array_fd = shm_open(g_shm_name, O_RDWR | O_CREAT, 0600);
|
|
|
|
if (g_conns_array_fd < 0) {
|
2016-08-02 16:34:45 +00:00
|
|
|
SPDK_ERRLOG("could not shm_open %s\n", g_shm_name);
|
2018-03-28 18:07:54 +00:00
|
|
|
goto err;
|
2016-08-02 16:34:45 +00:00
|
|
|
}
|
|
|
|
|
2018-04-20 22:12:21 +00:00
|
|
|
if (ftruncate(g_conns_array_fd, conns_size) != 0) {
|
2016-08-02 16:34:45 +00:00
|
|
|
SPDK_ERRLOG("could not ftruncate\n");
|
2018-03-28 18:07:54 +00:00
|
|
|
goto err;
|
2016-08-02 16:34:45 +00:00
|
|
|
}
|
|
|
|
g_conns_array = mmap(0, conns_size, PROT_READ | PROT_WRITE, MAP_SHARED,
|
2018-04-20 22:12:21 +00:00
|
|
|
g_conns_array_fd, 0);
|
2016-08-02 16:34:45 +00:00
|
|
|
|
2018-03-28 18:07:54 +00:00
|
|
|
if (g_conns_array == MAP_FAILED) {
|
|
|
|
fprintf(stderr, "could not mmap cons array file %s (%d)\n", g_shm_name, errno);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2016-08-02 16:34:45 +00:00
|
|
|
memset(g_conns_array, 0, conns_size);
|
|
|
|
|
|
|
|
for (i = 0; i < MAX_ISCSI_CONNECTIONS; i++) {
|
|
|
|
g_conns_array[i].id = i;
|
|
|
|
}
|
|
|
|
|
2017-12-22 00:21:35 +00:00
|
|
|
last_core = spdk_env_get_last_core();
|
2017-12-21 03:02:31 +00:00
|
|
|
g_num_connections = calloc(last_core + 1, sizeof(uint32_t));
|
|
|
|
if (!g_num_connections) {
|
|
|
|
SPDK_ERRLOG("Could not allocate array size=%u for g_num_connections\n",
|
|
|
|
last_core + 1);
|
2018-03-28 18:07:54 +00:00
|
|
|
goto err;
|
2016-08-02 16:34:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2018-03-28 18:07:54 +00:00
|
|
|
|
|
|
|
err:
|
|
|
|
if (g_conns_array != MAP_FAILED) {
|
|
|
|
munmap(g_conns_array, conns_size);
|
|
|
|
g_conns_array = MAP_FAILED;
|
|
|
|
}
|
|
|
|
|
2018-04-20 22:12:21 +00:00
|
|
|
if (g_conns_array_fd >= 0) {
|
|
|
|
close(g_conns_array_fd);
|
|
|
|
g_conns_array_fd = -1;
|
2018-03-28 18:07:54 +00:00
|
|
|
shm_unlink(g_shm_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
2016-08-02 16:34:45 +00:00
|
|
|
}
|
|
|
|
|
2018-02-14 02:26:10 +00:00
|
|
|
static void
|
|
|
|
spdk_iscsi_poll_group_add_conn_sock(struct spdk_iscsi_conn *conn)
|
|
|
|
{
|
|
|
|
struct spdk_iscsi_poll_group *poll_group = &g_spdk_iscsi.poll_group[spdk_env_get_current_core()];
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
rc = spdk_sock_group_add_sock(poll_group->sock_group, conn->sock, spdk_iscsi_conn_sock_cb, conn);
|
|
|
|
if (rc < 0) {
|
|
|
|
SPDK_ERRLOG("Failed to add sock=%p of conn=%p\n", conn->sock, conn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
spdk_iscsi_poll_group_remove_conn_sock(struct spdk_iscsi_conn *conn)
|
|
|
|
{
|
|
|
|
struct spdk_iscsi_poll_group *poll_group = &g_spdk_iscsi.poll_group[spdk_env_get_current_core()];
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
rc = spdk_sock_group_remove_sock(poll_group->sock_group, conn->sock);
|
|
|
|
if (rc < 0) {
|
|
|
|
SPDK_ERRLOG("Failed to remove sock=%p of conn=%p\n", conn->sock, conn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-13 05:09:49 +00:00
|
|
|
static void
|
2018-03-05 00:19:27 +00:00
|
|
|
spdk_iscsi_poll_group_add_conn(struct spdk_iscsi_conn *conn)
|
2018-02-13 05:09:49 +00:00
|
|
|
{
|
|
|
|
struct spdk_iscsi_poll_group *poll_group = &g_spdk_iscsi.poll_group[spdk_env_get_current_core()];
|
|
|
|
|
2018-05-04 06:43:59 +00:00
|
|
|
conn->is_stopped = false;
|
2018-02-13 05:09:49 +00:00
|
|
|
STAILQ_INSERT_TAIL(&poll_group->connections, conn, link);
|
2018-02-14 02:26:10 +00:00
|
|
|
spdk_iscsi_poll_group_add_conn_sock(conn);
|
2018-02-13 05:09:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
spdk_iscsi_poll_group_remove_conn(struct spdk_iscsi_conn *conn)
|
|
|
|
{
|
|
|
|
struct spdk_iscsi_poll_group *poll_group = &g_spdk_iscsi.poll_group[spdk_env_get_current_core()];
|
|
|
|
|
2018-05-04 06:43:59 +00:00
|
|
|
conn->is_stopped = true;
|
2018-02-13 05:09:49 +00:00
|
|
|
STAILQ_REMOVE(&poll_group->connections, conn, spdk_iscsi_conn, link);
|
|
|
|
}
|
|
|
|
|
2016-08-02 16:34:45 +00:00
|
|
|
/**
|
2017-04-25 17:35:22 +00:00
|
|
|
* \brief Create an iSCSI connection from the given parameters and schedule it
|
|
|
|
* on a reactor.
|
|
|
|
*
|
|
|
|
* \code
|
|
|
|
*
|
|
|
|
* # identify reactor where the new connections work item will be scheduled
|
|
|
|
* reactor = spdk_iscsi_conn_allocate_reactor()
|
|
|
|
* allocate spdk_iscsi_conn object
|
|
|
|
* initialize spdk_iscsi_conn object
|
|
|
|
* schedule iSCSI connection work item on reactor
|
|
|
|
*
|
|
|
|
* \endcode
|
|
|
|
*/
|
2016-08-02 16:34:45 +00:00
|
|
|
int
|
|
|
|
spdk_iscsi_conn_construct(struct spdk_iscsi_portal *portal,
|
2018-02-07 21:46:22 +00:00
|
|
|
struct spdk_sock *sock)
|
2016-08-02 16:34:45 +00:00
|
|
|
{
|
|
|
|
struct spdk_iscsi_conn *conn;
|
|
|
|
int bufsize, i, rc;
|
|
|
|
|
|
|
|
conn = allocate_conn();
|
|
|
|
if (conn == NULL) {
|
|
|
|
SPDK_ERRLOG("Could not allocate connection.\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
pthread_mutex_lock(&g_spdk_iscsi.mutex);
|
|
|
|
conn->timeout = g_spdk_iscsi.timeout;
|
|
|
|
conn->nopininterval = g_spdk_iscsi.nopininterval;
|
2016-08-18 19:52:48 +00:00
|
|
|
conn->nopininterval *= spdk_get_ticks_hz(); /* seconds to TSC */
|
2016-08-02 16:34:45 +00:00
|
|
|
conn->nop_outstanding = false;
|
|
|
|
conn->data_out_cnt = 0;
|
|
|
|
conn->data_in_cnt = 0;
|
|
|
|
pthread_mutex_unlock(&g_spdk_iscsi.mutex);
|
|
|
|
conn->MaxRecvDataSegmentLength = 8192; // RFC3720(12.12)
|
|
|
|
|
|
|
|
conn->portal = portal;
|
2017-09-30 04:01:07 +00:00
|
|
|
conn->pg_tag = portal->group->tag;
|
|
|
|
conn->portal_host = strdup(portal->host);
|
|
|
|
conn->portal_port = strdup(portal->port);
|
|
|
|
conn->portal_cpumask = portal->cpumask;
|
2016-08-02 16:34:45 +00:00
|
|
|
conn->sock = sock;
|
|
|
|
|
|
|
|
conn->state = ISCSI_CONN_STATE_INVALID;
|
|
|
|
conn->login_phase = ISCSI_SECURITY_NEGOTIATION_PHASE;
|
|
|
|
conn->ttt = 0;
|
|
|
|
|
|
|
|
conn->partial_text_parameter = NULL;
|
|
|
|
|
2017-12-07 23:23:48 +00:00
|
|
|
for (i = 0; i < MAX_CONNECTION_PARAMS; i++) {
|
2016-08-02 16:34:45 +00:00
|
|
|
conn->conn_param_state_negotiated[i] = false;
|
2017-12-07 23:23:48 +00:00
|
|
|
}
|
2016-08-02 16:34:45 +00:00
|
|
|
|
2017-12-07 23:23:48 +00:00
|
|
|
for (i = 0; i < MAX_SESSION_PARAMS; i++) {
|
2016-08-02 16:34:45 +00:00
|
|
|
conn->sess_param_state_negotiated[i] = false;
|
2017-12-07 23:23:48 +00:00
|
|
|
}
|
2016-08-02 16:34:45 +00:00
|
|
|
|
2017-12-07 23:23:48 +00:00
|
|
|
for (i = 0; i < DEFAULT_MAXR2T; i++) {
|
2016-08-02 16:34:45 +00:00
|
|
|
conn->outstanding_r2t_tasks[i] = NULL;
|
2017-12-07 23:23:48 +00:00
|
|
|
}
|
2016-08-02 16:34:45 +00:00
|
|
|
|
|
|
|
TAILQ_INIT(&conn->write_pdu_list);
|
|
|
|
TAILQ_INIT(&conn->snack_pdu_list);
|
|
|
|
TAILQ_INIT(&conn->queued_r2t_tasks);
|
|
|
|
TAILQ_INIT(&conn->active_r2t_tasks);
|
|
|
|
TAILQ_INIT(&conn->queued_datain_tasks);
|
2018-07-04 05:23:55 +00:00
|
|
|
memset(&conn->open_lun_descs, 0, sizeof(conn->open_lun_descs));
|
2016-08-02 16:34:45 +00:00
|
|
|
|
|
|
|
rc = spdk_sock_getaddr(sock, conn->target_addr,
|
|
|
|
sizeof conn->target_addr,
|
|
|
|
conn->initiator_addr, sizeof conn->initiator_addr);
|
|
|
|
if (rc < 0) {
|
|
|
|
SPDK_ERRLOG("spdk_sock_getaddr() failed\n");
|
|
|
|
goto error_return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bufsize = 2 * 1024 * 1024;
|
|
|
|
rc = spdk_sock_set_recvbuf(conn->sock, bufsize);
|
2017-12-07 23:23:48 +00:00
|
|
|
if (rc != 0) {
|
2016-08-02 16:34:45 +00:00
|
|
|
SPDK_ERRLOG("spdk_sock_set_recvbuf failed\n");
|
2017-12-07 23:23:48 +00:00
|
|
|
}
|
2016-08-02 16:34:45 +00:00
|
|
|
|
|
|
|
bufsize = 32 * 1024 * 1024 / g_spdk_iscsi.MaxConnections;
|
|
|
|
if (bufsize > 2 * 1024 * 1024) {
|
|
|
|
bufsize = 2 * 1024 * 1024;
|
|
|
|
}
|
|
|
|
rc = spdk_sock_set_sendbuf(conn->sock, bufsize);
|
2017-12-07 23:23:48 +00:00
|
|
|
if (rc != 0) {
|
2016-08-02 16:34:45 +00:00
|
|
|
SPDK_ERRLOG("spdk_sock_set_sendbuf failed\n");
|
2017-12-07 23:23:48 +00:00
|
|
|
}
|
2016-08-02 16:34:45 +00:00
|
|
|
|
|
|
|
/* set low water mark */
|
|
|
|
rc = spdk_sock_set_recvlowat(conn->sock, 1);
|
|
|
|
if (rc != 0) {
|
|
|
|
SPDK_ERRLOG("spdk_sock_set_recvlowat() failed\n");
|
|
|
|
goto error_return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set default params */
|
|
|
|
rc = spdk_iscsi_conn_params_init(&conn->params);
|
|
|
|
if (rc < 0) {
|
|
|
|
SPDK_ERRLOG("iscsi_conn_params_init() failed\n");
|
|
|
|
error_return:
|
|
|
|
spdk_iscsi_param_free(conn->params);
|
2016-09-28 20:55:56 +00:00
|
|
|
free_conn(conn);
|
2016-08-02 16:34:45 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2016-10-05 17:23:48 +00:00
|
|
|
conn->logout_timer = NULL;
|
|
|
|
conn->shutdown_timer = NULL;
|
2016-08-02 16:34:45 +00:00
|
|
|
SPDK_NOTICELOG("Launching connection on acceptor thread\n");
|
|
|
|
conn->pending_task_cnt = 0;
|
|
|
|
conn->pending_activate_event = false;
|
|
|
|
|
2017-03-27 19:59:40 +00:00
|
|
|
conn->lcore = spdk_env_get_current_core();
|
2016-10-03 21:56:10 +00:00
|
|
|
__sync_fetch_and_add(&g_num_connections[conn->lcore], 1);
|
2016-08-02 16:34:45 +00:00
|
|
|
|
2018-03-05 00:19:27 +00:00
|
|
|
spdk_iscsi_poll_group_add_conn(conn);
|
2016-08-02 16:34:45 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-12-07 05:02:40 +00:00
|
|
|
void
|
|
|
|
spdk_iscsi_conn_free_pdu(struct spdk_iscsi_conn *conn, struct spdk_iscsi_pdu *pdu)
|
|
|
|
{
|
|
|
|
if (pdu->task) {
|
|
|
|
if (pdu->bhs.opcode == ISCSI_OP_SCSI_DATAIN) {
|
|
|
|
if (pdu->task->scsi.offset > 0) {
|
|
|
|
conn->data_in_cnt--;
|
|
|
|
if (pdu->bhs.flags & ISCSI_DATAIN_STATUS) {
|
|
|
|
/* Free the primary task after the last subtask done */
|
|
|
|
conn->data_in_cnt--;
|
|
|
|
spdk_iscsi_task_put(spdk_iscsi_task_get_primary(pdu->task));
|
|
|
|
}
|
|
|
|
}
|
2017-01-19 11:38:54 +00:00
|
|
|
} else if (pdu->bhs.opcode == ISCSI_OP_SCSI_RSP &&
|
|
|
|
pdu->task->scsi.status != SPDK_SCSI_STATUS_GOOD) {
|
|
|
|
if (pdu->task->scsi.offset > 0) {
|
|
|
|
spdk_iscsi_task_put(spdk_iscsi_task_get_primary(pdu->task));
|
|
|
|
}
|
2016-12-07 05:02:40 +00:00
|
|
|
}
|
|
|
|
spdk_iscsi_task_put(pdu->task);
|
|
|
|
}
|
|
|
|
spdk_put_pdu(pdu);
|
|
|
|
}
|
|
|
|
|
2016-08-02 16:34:45 +00:00
|
|
|
static int spdk_iscsi_conn_free_tasks(struct spdk_iscsi_conn *conn)
|
|
|
|
{
|
2018-06-11 04:43:39 +00:00
|
|
|
struct spdk_iscsi_pdu *pdu, *tmp_pdu;
|
|
|
|
struct spdk_iscsi_task *iscsi_task, *tmp_iscsi_task;
|
2016-08-02 16:34:45 +00:00
|
|
|
|
2018-06-11 04:43:39 +00:00
|
|
|
TAILQ_FOREACH_SAFE(pdu, &conn->write_pdu_list, tailq, tmp_pdu) {
|
2016-08-02 16:34:45 +00:00
|
|
|
TAILQ_REMOVE(&conn->write_pdu_list, pdu, tailq);
|
2016-09-27 06:25:42 +00:00
|
|
|
if (pdu->task) {
|
2016-08-02 16:34:45 +00:00
|
|
|
spdk_iscsi_task_put(pdu->task);
|
2016-09-27 06:25:42 +00:00
|
|
|
}
|
2016-08-02 16:34:45 +00:00
|
|
|
spdk_put_pdu(pdu);
|
|
|
|
}
|
|
|
|
|
2018-06-11 04:43:39 +00:00
|
|
|
TAILQ_FOREACH_SAFE(pdu, &conn->snack_pdu_list, tailq, tmp_pdu) {
|
2016-08-02 16:34:45 +00:00
|
|
|
TAILQ_REMOVE(&conn->snack_pdu_list, pdu, tailq);
|
2016-09-27 06:25:42 +00:00
|
|
|
if (pdu->task) {
|
2016-08-02 16:34:45 +00:00
|
|
|
spdk_iscsi_task_put(pdu->task);
|
2016-09-27 06:25:42 +00:00
|
|
|
}
|
2016-08-02 16:34:45 +00:00
|
|
|
spdk_put_pdu(pdu);
|
|
|
|
}
|
|
|
|
|
2018-06-11 04:43:39 +00:00
|
|
|
TAILQ_FOREACH_SAFE(iscsi_task, &conn->queued_datain_tasks, link, tmp_iscsi_task) {
|
2016-08-02 16:34:45 +00:00
|
|
|
TAILQ_REMOVE(&conn->queued_datain_tasks, iscsi_task, link);
|
|
|
|
pdu = iscsi_task->pdu;
|
|
|
|
spdk_iscsi_task_put(iscsi_task);
|
|
|
|
spdk_put_pdu(pdu);
|
|
|
|
}
|
|
|
|
|
2017-12-07 23:23:48 +00:00
|
|
|
if (conn->pending_task_cnt) {
|
2016-09-27 06:25:42 +00:00
|
|
|
return -1;
|
2017-12-07 23:23:48 +00:00
|
|
|
}
|
2016-08-02 16:34:45 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void spdk_iscsi_conn_free(struct spdk_iscsi_conn *conn)
|
|
|
|
{
|
2017-12-07 23:23:48 +00:00
|
|
|
if (conn == NULL) {
|
2016-08-02 16:34:45 +00:00
|
|
|
return;
|
2017-12-07 23:23:48 +00:00
|
|
|
}
|
2016-08-02 16:34:45 +00:00
|
|
|
|
|
|
|
spdk_iscsi_param_free(conn->params);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Each connection pre-allocates its next PDU - make sure these get
|
|
|
|
* freed here.
|
|
|
|
*/
|
|
|
|
spdk_put_pdu(conn->pdu_in_progress);
|
|
|
|
|
|
|
|
free(conn->auth.user);
|
|
|
|
free(conn->auth.secret);
|
|
|
|
free(conn->auth.muser);
|
|
|
|
free(conn->auth.msecret);
|
|
|
|
free_conn(conn);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void spdk_iscsi_remove_conn(struct spdk_iscsi_conn *conn)
|
|
|
|
{
|
|
|
|
struct spdk_iscsi_sess *sess;
|
|
|
|
int idx;
|
|
|
|
uint32_t i, j;
|
|
|
|
|
|
|
|
idx = -1;
|
|
|
|
sess = conn->sess;
|
|
|
|
conn->sess = NULL;
|
|
|
|
if (sess == NULL) {
|
|
|
|
spdk_iscsi_conn_free(conn);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < sess->connections; i++) {
|
|
|
|
if (sess->conns[i] == conn) {
|
|
|
|
idx = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sess->connections < 1) {
|
|
|
|
SPDK_ERRLOG("zero connection\n");
|
|
|
|
sess->connections = 0;
|
|
|
|
} else {
|
|
|
|
if (idx < 0) {
|
|
|
|
SPDK_ERRLOG("remove conn not found\n");
|
|
|
|
} else {
|
|
|
|
for (j = idx; j < sess->connections - 1; j++) {
|
|
|
|
sess->conns[j] = sess->conns[j + 1];
|
|
|
|
}
|
|
|
|
sess->conns[sess->connections - 1] = NULL;
|
|
|
|
}
|
|
|
|
sess->connections--;
|
|
|
|
}
|
|
|
|
|
|
|
|
SPDK_NOTICELOG("Terminating connections(tsih %d): %d\n", sess->tsih, sess->connections);
|
|
|
|
|
|
|
|
if (sess->connections == 0) {
|
|
|
|
/* cleanup last connection */
|
2017-08-30 18:06:33 +00:00
|
|
|
SPDK_DEBUGLOG(SPDK_LOG_ISCSI,
|
2016-08-02 16:34:45 +00:00
|
|
|
"cleanup last conn free sess\n");
|
|
|
|
spdk_free_sess(sess);
|
|
|
|
}
|
|
|
|
|
2017-08-30 18:06:33 +00:00
|
|
|
SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "cleanup free conn\n");
|
2016-08-02 16:34:45 +00:00
|
|
|
spdk_iscsi_conn_free(conn);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
spdk_iscsi_conn_cleanup_backend(struct spdk_iscsi_conn *conn)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
if (conn->sess->connections > 1) {
|
|
|
|
/* connection specific cleanup */
|
|
|
|
} else if (!g_spdk_iscsi.AllowDuplicateIsid) {
|
|
|
|
/* clean up all tasks to all LUNs for session */
|
|
|
|
rc = spdk_iscsi_tgt_node_cleanup_luns(conn,
|
|
|
|
conn->sess->target);
|
2017-12-07 23:23:48 +00:00
|
|
|
if (rc < 0) {
|
2016-08-02 16:34:45 +00:00
|
|
|
SPDK_ERRLOG("target abort failed\n");
|
2017-12-07 23:23:48 +00:00
|
|
|
}
|
2016-08-02 16:34:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2018-01-22 19:53:51 +00:00
|
|
|
_spdk_iscsi_conn_free(struct spdk_iscsi_conn *conn)
|
2016-08-02 16:34:45 +00:00
|
|
|
{
|
|
|
|
pthread_mutex_lock(&g_conns_mutex);
|
|
|
|
spdk_iscsi_remove_conn(conn);
|
|
|
|
pthread_mutex_unlock(&g_conns_mutex);
|
|
|
|
}
|
|
|
|
|
2018-03-13 00:16:47 +00:00
|
|
|
static int
|
2016-10-05 17:23:48 +00:00
|
|
|
_spdk_iscsi_conn_check_shutdown(void *arg)
|
2016-08-02 16:34:45 +00:00
|
|
|
{
|
|
|
|
struct spdk_iscsi_conn *conn = arg;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
rc = spdk_iscsi_conn_free_tasks(conn);
|
|
|
|
if (rc < 0) {
|
2018-03-13 00:16:47 +00:00
|
|
|
return -1;
|
2016-08-02 16:34:45 +00:00
|
|
|
}
|
|
|
|
|
2017-11-15 22:09:40 +00:00
|
|
|
spdk_poller_unregister(&conn->shutdown_timer);
|
2016-08-02 16:34:45 +00:00
|
|
|
|
2018-02-26 03:38:39 +00:00
|
|
|
spdk_iscsi_conn_stop(conn);
|
2018-01-22 19:53:51 +00:00
|
|
|
_spdk_iscsi_conn_free(conn);
|
2018-03-13 00:16:47 +00:00
|
|
|
|
|
|
|
return -1;
|
2016-08-02 16:34:45 +00:00
|
|
|
}
|
|
|
|
|
2018-03-06 04:46:55 +00:00
|
|
|
void
|
2018-03-06 07:39:16 +00:00
|
|
|
spdk_iscsi_conn_destruct(struct spdk_iscsi_conn *conn)
|
2016-08-02 16:34:45 +00:00
|
|
|
{
|
|
|
|
struct spdk_iscsi_tgt_node *target;
|
|
|
|
int rc;
|
|
|
|
|
2016-12-08 06:21:10 +00:00
|
|
|
conn->state = ISCSI_CONN_STATE_EXITED;
|
2016-08-02 16:34:45 +00:00
|
|
|
|
|
|
|
if (conn->sess != NULL && conn->pending_task_cnt > 0) {
|
|
|
|
target = conn->sess->target;
|
|
|
|
if (target != NULL) {
|
|
|
|
spdk_iscsi_conn_cleanup_backend(conn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
spdk_clear_all_transfer_task(conn, NULL);
|
2018-02-14 02:26:10 +00:00
|
|
|
spdk_iscsi_poll_group_remove_conn_sock(conn);
|
2018-02-07 21:46:22 +00:00
|
|
|
spdk_sock_close(&conn->sock);
|
2017-11-15 22:09:40 +00:00
|
|
|
spdk_poller_unregister(&conn->logout_timer);
|
2018-01-19 17:22:41 +00:00
|
|
|
spdk_poller_unregister(&conn->flush_poller);
|
2016-08-02 16:34:45 +00:00
|
|
|
|
|
|
|
rc = spdk_iscsi_conn_free_tasks(conn);
|
|
|
|
if (rc < 0) {
|
|
|
|
/* The connection cannot be freed yet. Check back later. */
|
2017-11-14 20:42:17 +00:00
|
|
|
conn->shutdown_timer = spdk_poller_register(_spdk_iscsi_conn_check_shutdown, conn, 1000);
|
2016-08-02 16:34:45 +00:00
|
|
|
} else {
|
2018-02-26 03:38:39 +00:00
|
|
|
spdk_iscsi_conn_stop(conn);
|
2018-01-22 19:53:51 +00:00
|
|
|
_spdk_iscsi_conn_free(conn);
|
2016-08-02 16:34:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
spdk_iscsi_get_active_conns(void)
|
|
|
|
{
|
|
|
|
struct spdk_iscsi_conn *conn;
|
|
|
|
int num = 0;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
pthread_mutex_lock(&g_conns_mutex);
|
|
|
|
for (i = 0; i < MAX_ISCSI_CONNECTIONS; i++) {
|
|
|
|
conn = spdk_find_iscsi_connection_by_id(i);
|
2017-12-07 23:23:48 +00:00
|
|
|
if (conn == NULL) {
|
2016-08-02 16:34:45 +00:00
|
|
|
continue;
|
2017-12-07 23:23:48 +00:00
|
|
|
}
|
2016-08-02 16:34:45 +00:00
|
|
|
num++;
|
|
|
|
}
|
|
|
|
pthread_mutex_unlock(&g_conns_mutex);
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
spdk_iscsi_conns_cleanup(void)
|
|
|
|
{
|
2017-12-21 03:02:31 +00:00
|
|
|
free(g_num_connections);
|
2016-08-02 16:34:45 +00:00
|
|
|
munmap(g_conns_array, sizeof(struct spdk_iscsi_conn) *
|
|
|
|
MAX_ISCSI_CONNECTIONS);
|
|
|
|
shm_unlink(g_shm_name);
|
2018-04-20 22:12:21 +00:00
|
|
|
if (g_conns_array_fd >= 0) {
|
|
|
|
close(g_conns_array_fd);
|
|
|
|
g_conns_array_fd = -1;
|
|
|
|
}
|
2016-08-02 16:34:45 +00:00
|
|
|
}
|
|
|
|
|
2017-10-17 14:20:01 +00:00
|
|
|
static void
|
|
|
|
spdk_iscsi_conn_check_shutdown_cb(void *arg1, void *arg2)
|
|
|
|
{
|
|
|
|
spdk_iscsi_conns_cleanup();
|
2018-02-26 15:30:05 +00:00
|
|
|
spdk_shutdown_iscsi_conns_done();
|
2017-10-17 14:20:01 +00:00
|
|
|
}
|
|
|
|
|
2018-03-13 00:16:47 +00:00
|
|
|
static int
|
2016-10-04 18:20:29 +00:00
|
|
|
spdk_iscsi_conn_check_shutdown(void *arg)
|
2016-08-02 16:34:45 +00:00
|
|
|
{
|
2017-10-17 14:20:01 +00:00
|
|
|
struct spdk_event *event;
|
|
|
|
|
2016-08-02 16:34:45 +00:00
|
|
|
if (spdk_iscsi_get_active_conns() == 0) {
|
2017-11-15 22:09:40 +00:00
|
|
|
spdk_poller_unregister(&g_shutdown_timer);
|
2018-07-04 05:23:55 +00:00
|
|
|
event = spdk_event_allocate(spdk_env_get_current_core(),
|
|
|
|
spdk_iscsi_conn_check_shutdown_cb, NULL, NULL);
|
2017-11-15 18:02:42 +00:00
|
|
|
spdk_event_call(event);
|
2016-08-02 16:34:45 +00:00
|
|
|
}
|
2018-03-13 00:16:47 +00:00
|
|
|
|
|
|
|
return -1;
|
2016-08-02 16:34:45 +00:00
|
|
|
}
|
|
|
|
|
2018-07-04 05:23:55 +00:00
|
|
|
static void
|
|
|
|
spdk_iscsi_conn_close_lun(struct spdk_iscsi_conn *conn, int lun_id)
|
|
|
|
{
|
|
|
|
struct spdk_scsi_desc *desc;
|
|
|
|
|
|
|
|
desc = conn->open_lun_descs[lun_id];
|
|
|
|
if (desc != NULL) {
|
|
|
|
spdk_scsi_lun_free_io_channel(desc);
|
|
|
|
spdk_scsi_lun_close(desc);
|
|
|
|
conn->open_lun_descs[lun_id] = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
spdk_iscsi_conn_close_luns(struct spdk_iscsi_conn *conn)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < SPDK_SCSI_DEV_MAX_LUN; i++) {
|
|
|
|
spdk_iscsi_conn_close_lun(conn, i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
spdk_iscsi_conn_remove_lun(struct spdk_scsi_lun *lun, void *remove_ctx)
|
|
|
|
{
|
|
|
|
struct spdk_iscsi_conn *conn = remove_ctx;
|
|
|
|
int lun_id = spdk_scsi_lun_get_id(lun);
|
|
|
|
|
|
|
|
spdk_clear_all_transfer_task(conn, lun);
|
|
|
|
|
|
|
|
spdk_iscsi_conn_close_lun(conn, lun_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
spdk_iscsi_conn_open_luns(struct spdk_iscsi_conn *conn)
|
|
|
|
{
|
|
|
|
int i, rc;
|
|
|
|
struct spdk_scsi_lun *lun;
|
|
|
|
struct spdk_scsi_desc *desc;
|
|
|
|
|
|
|
|
for (i = 0; i < SPDK_SCSI_DEV_MAX_LUN; i++) {
|
|
|
|
lun = spdk_scsi_dev_get_lun(conn->dev, i);
|
|
|
|
if (lun == NULL) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = spdk_scsi_lun_open(lun, spdk_iscsi_conn_remove_lun, conn, &desc);
|
|
|
|
if (rc != 0) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = spdk_scsi_lun_allocate_io_channel(desc);
|
|
|
|
if (rc != 0) {
|
|
|
|
spdk_scsi_lun_close(desc);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
conn->open_lun_descs[i] = desc;
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
error:
|
|
|
|
spdk_iscsi_conn_close_luns(conn);
|
|
|
|
}
|
|
|
|
|
2016-09-20 22:06:02 +00:00
|
|
|
/**
|
2018-02-26 03:38:39 +00:00
|
|
|
* This function will stop executing the specified connection.
|
2016-09-20 22:06:02 +00:00
|
|
|
*/
|
|
|
|
static void
|
2018-02-26 03:38:39 +00:00
|
|
|
spdk_iscsi_conn_stop(struct spdk_iscsi_conn *conn)
|
2016-09-20 22:06:02 +00:00
|
|
|
{
|
2016-09-21 21:11:39 +00:00
|
|
|
struct spdk_iscsi_tgt_node *target;
|
2016-09-20 22:06:02 +00:00
|
|
|
|
2018-01-22 20:40:58 +00:00
|
|
|
if (conn->state == ISCSI_CONN_STATE_EXITED && conn->sess != NULL &&
|
|
|
|
conn->sess->session_type == SESSION_TYPE_NORMAL &&
|
2017-05-03 20:28:00 +00:00
|
|
|
conn->full_feature) {
|
2016-09-21 21:11:39 +00:00
|
|
|
target = conn->sess->target;
|
|
|
|
pthread_mutex_lock(&target->mutex);
|
|
|
|
target->num_active_conns--;
|
|
|
|
pthread_mutex_unlock(&target->mutex);
|
2016-09-20 19:45:37 +00:00
|
|
|
|
2018-07-04 05:23:55 +00:00
|
|
|
spdk_iscsi_conn_close_luns(conn);
|
2016-09-21 21:11:39 +00:00
|
|
|
}
|
2018-02-13 05:09:49 +00:00
|
|
|
|
2017-03-27 19:59:40 +00:00
|
|
|
__sync_fetch_and_sub(&g_num_connections[spdk_env_get_current_core()], 1);
|
2018-02-13 05:09:49 +00:00
|
|
|
spdk_iscsi_poll_group_remove_conn(conn);
|
2016-09-20 22:06:02 +00:00
|
|
|
}
|
|
|
|
|
2016-08-02 16:34:45 +00:00
|
|
|
void spdk_shutdown_iscsi_conns(void)
|
|
|
|
{
|
2018-01-19 18:15:38 +00:00
|
|
|
struct spdk_iscsi_conn *conn;
|
|
|
|
int i;
|
2016-08-02 16:34:45 +00:00
|
|
|
|
|
|
|
pthread_mutex_lock(&g_conns_mutex);
|
|
|
|
|
|
|
|
for (i = 0; i < MAX_ISCSI_CONNECTIONS; i++) {
|
|
|
|
conn = spdk_find_iscsi_connection_by_id(i);
|
2017-12-07 23:23:48 +00:00
|
|
|
if (conn == NULL) {
|
2016-08-02 16:34:45 +00:00
|
|
|
continue;
|
2017-12-07 23:23:48 +00:00
|
|
|
}
|
2016-08-02 16:34:45 +00:00
|
|
|
conn->state = ISCSI_CONN_STATE_EXITING;
|
|
|
|
}
|
|
|
|
|
|
|
|
pthread_mutex_unlock(&g_conns_mutex);
|
2017-11-14 20:42:17 +00:00
|
|
|
g_shutdown_timer = spdk_poller_register(spdk_iscsi_conn_check_shutdown, NULL,
|
|
|
|
1000);
|
2016-08-02 16:34:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
spdk_iscsi_drop_conns(struct spdk_iscsi_conn *conn, const char *conn_match,
|
|
|
|
int drop_all)
|
|
|
|
{
|
|
|
|
struct spdk_iscsi_conn *xconn;
|
|
|
|
const char *xconn_match;
|
|
|
|
int i, num;
|
|
|
|
|
2017-08-30 18:06:33 +00:00
|
|
|
SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "spdk_iscsi_drop_conns\n");
|
2016-08-02 16:34:45 +00:00
|
|
|
|
|
|
|
num = 0;
|
|
|
|
pthread_mutex_lock(&g_conns_mutex);
|
|
|
|
for (i = 0; i < MAX_ISCSI_CONNECTIONS; i++) {
|
|
|
|
xconn = spdk_find_iscsi_connection_by_id(i);
|
|
|
|
|
2017-12-07 23:23:48 +00:00
|
|
|
if (xconn == NULL) {
|
2016-08-02 16:34:45 +00:00
|
|
|
continue;
|
2017-12-07 23:23:48 +00:00
|
|
|
}
|
2016-08-02 16:34:45 +00:00
|
|
|
|
2017-12-07 23:23:48 +00:00
|
|
|
if (xconn == conn) {
|
2016-08-02 16:34:45 +00:00
|
|
|
continue;
|
2017-12-07 23:23:48 +00:00
|
|
|
}
|
2016-08-02 16:34:45 +00:00
|
|
|
|
2017-12-07 23:23:48 +00:00
|
|
|
if (!drop_all && xconn->initiator_port == NULL) {
|
2016-08-02 16:34:45 +00:00
|
|
|
continue;
|
2017-12-07 23:23:48 +00:00
|
|
|
}
|
2016-08-02 16:34:45 +00:00
|
|
|
|
|
|
|
xconn_match =
|
2017-05-03 19:41:55 +00:00
|
|
|
drop_all ? xconn->initiator_name : spdk_scsi_port_get_name(xconn->initiator_port);
|
2016-08-02 16:34:45 +00:00
|
|
|
|
|
|
|
if (!strcasecmp(conn_match, xconn_match) &&
|
|
|
|
conn->target == xconn->target) {
|
|
|
|
|
|
|
|
if (num == 0) {
|
|
|
|
/*
|
|
|
|
* Only print this message before we report the
|
|
|
|
* first dropped connection.
|
|
|
|
*/
|
2016-08-08 17:14:50 +00:00
|
|
|
SPDK_ERRLOG("drop old connections %s by %s\n",
|
|
|
|
conn->target->name, conn_match);
|
2016-08-02 16:34:45 +00:00
|
|
|
}
|
|
|
|
|
2017-08-18 13:22:25 +00:00
|
|
|
SPDK_ERRLOG("exiting conn by %s (%s)\n",
|
2016-08-08 17:14:50 +00:00
|
|
|
xconn_match, xconn->initiator_addr);
|
|
|
|
if (xconn->sess != NULL) {
|
2017-08-30 18:06:33 +00:00
|
|
|
SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "TSIH=%u\n", xconn->sess->tsih);
|
2016-08-08 17:14:50 +00:00
|
|
|
} else {
|
2017-08-30 18:06:33 +00:00
|
|
|
SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "TSIH=xx\n");
|
2016-08-08 17:14:50 +00:00
|
|
|
}
|
2016-08-02 16:34:45 +00:00
|
|
|
|
2017-08-30 18:06:33 +00:00
|
|
|
SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "CID=%u\n", xconn->cid);
|
2016-08-02 16:34:45 +00:00
|
|
|
xconn->state = ISCSI_CONN_STATE_EXITING;
|
|
|
|
num++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pthread_mutex_unlock(&g_conns_mutex);
|
|
|
|
|
2016-08-08 17:14:50 +00:00
|
|
|
if (num != 0) {
|
|
|
|
SPDK_ERRLOG("exiting %d conns\n", num);
|
|
|
|
}
|
2016-08-02 16:34:45 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-04-25 17:35:22 +00:00
|
|
|
* \brief Reads data for the specified iSCSI connection from its TCP socket.
|
|
|
|
*
|
|
|
|
* The TCP socket is marked as non-blocking, so this function may not read
|
|
|
|
* all data requested.
|
|
|
|
*
|
|
|
|
* Returns SPDK_ISCSI_CONNECTION_FATAL if the recv() operation indicates a fatal
|
|
|
|
* error with the TCP connection (including if the TCP connection was closed
|
|
|
|
* unexpectedly.
|
|
|
|
*
|
|
|
|
* Otherwise returns the number of bytes successfully read.
|
|
|
|
*/
|
2016-08-02 16:34:45 +00:00
|
|
|
int
|
|
|
|
spdk_iscsi_conn_read_data(struct spdk_iscsi_conn *conn, int bytes,
|
|
|
|
void *buf)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (bytes == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = spdk_sock_recv(conn->sock, buf, bytes);
|
|
|
|
|
|
|
|
if (ret > 0) {
|
|
|
|
spdk_trace_record(TRACE_READ_FROM_SOCKET_DONE, conn->id, ret, 0, 0);
|
2018-06-08 05:54:01 +00:00
|
|
|
return ret;
|
2016-08-02 16:34:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ret < 0) {
|
|
|
|
if (errno == EAGAIN || errno == EWOULDBLOCK) {
|
|
|
|
return 0;
|
2018-06-08 05:54:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* For connect reset issue, do not output error log */
|
|
|
|
if (errno == ECONNRESET) {
|
|
|
|
SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "spdk_sock_recv() failed, errno %d: %s\n",
|
|
|
|
errno, spdk_strerror(errno));
|
2017-08-11 23:27:04 +00:00
|
|
|
} else {
|
2018-06-08 05:54:01 +00:00
|
|
|
SPDK_ERRLOG("spdk_sock_recv() failed, errno %d: %s\n",
|
|
|
|
errno, spdk_strerror(errno));
|
2017-08-11 23:27:04 +00:00
|
|
|
}
|
2016-08-02 16:34:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* connection closed */
|
2018-06-08 05:54:01 +00:00
|
|
|
return SPDK_ISCSI_CONNECTION_FATAL;
|
2016-08-02 16:34:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-06-08 20:45:41 +00:00
|
|
|
spdk_iscsi_task_mgmt_cpl(struct spdk_scsi_task *scsi_task)
|
2016-08-02 16:34:45 +00:00
|
|
|
{
|
2017-06-08 20:45:41 +00:00
|
|
|
struct spdk_iscsi_task *task = spdk_iscsi_task_from_scsi_task(scsi_task);
|
2016-08-02 16:34:45 +00:00
|
|
|
|
2017-06-08 20:45:41 +00:00
|
|
|
spdk_iscsi_task_mgmt_response(task->conn, task);
|
2016-08-02 16:34:45 +00:00
|
|
|
spdk_iscsi_task_put(task);
|
|
|
|
}
|
|
|
|
|
2016-09-03 10:02:38 +00:00
|
|
|
static void
|
|
|
|
process_completed_read_subtask_list(struct spdk_iscsi_conn *conn,
|
|
|
|
struct spdk_iscsi_task *primary)
|
|
|
|
{
|
2018-06-11 04:43:39 +00:00
|
|
|
struct spdk_iscsi_task *subtask, *tmp;
|
|
|
|
|
|
|
|
TAILQ_FOREACH_SAFE(subtask, &primary->subtask_list, subtask_link, tmp) {
|
|
|
|
if (subtask->scsi.offset == primary->bytes_completed) {
|
|
|
|
TAILQ_REMOVE(&primary->subtask_list, subtask, subtask_link);
|
|
|
|
primary->bytes_completed += subtask->scsi.length;
|
|
|
|
spdk_iscsi_task_response(conn, subtask);
|
|
|
|
spdk_iscsi_task_put(subtask);
|
2016-09-03 10:02:38 +00:00
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-02 16:34:45 +00:00
|
|
|
static void
|
|
|
|
process_read_task_completion(struct spdk_iscsi_conn *conn,
|
2016-09-28 06:13:07 +00:00
|
|
|
struct spdk_iscsi_task *task,
|
|
|
|
struct spdk_iscsi_task *primary)
|
2016-08-02 16:34:45 +00:00
|
|
|
{
|
2017-05-03 20:21:56 +00:00
|
|
|
struct spdk_iscsi_task *tmp;
|
2016-08-02 16:34:45 +00:00
|
|
|
|
2017-08-09 07:22:23 +00:00
|
|
|
if (task->scsi.status != SPDK_SCSI_STATUS_GOOD) {
|
|
|
|
TAILQ_FOREACH(tmp, &primary->subtask_list, subtask_link) {
|
2018-06-11 04:20:23 +00:00
|
|
|
spdk_scsi_task_copy_status(&tmp->scsi, &task->scsi);
|
2017-08-09 07:22:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-13 06:34:09 +00:00
|
|
|
if ((task != primary) &&
|
2017-05-03 21:46:35 +00:00
|
|
|
(task->scsi.offset != primary->bytes_completed)) {
|
2017-05-10 00:28:58 +00:00
|
|
|
TAILQ_FOREACH(tmp, &primary->subtask_list, subtask_link) {
|
2017-05-03 20:21:56 +00:00
|
|
|
if (task->scsi.offset < tmp->scsi.offset) {
|
|
|
|
TAILQ_INSERT_BEFORE(tmp, task, subtask_link);
|
2018-06-11 04:43:39 +00:00
|
|
|
return;
|
2016-08-02 16:34:45 +00:00
|
|
|
}
|
|
|
|
}
|
2018-06-11 04:43:39 +00:00
|
|
|
|
|
|
|
TAILQ_INSERT_TAIL(&primary->subtask_list, task, subtask_link);
|
2016-09-13 06:34:09 +00:00
|
|
|
return;
|
2016-08-02 16:34:45 +00:00
|
|
|
}
|
2016-09-13 06:34:09 +00:00
|
|
|
|
2017-05-03 21:46:35 +00:00
|
|
|
primary->bytes_completed += task->scsi.length;
|
2016-09-13 06:34:09 +00:00
|
|
|
spdk_iscsi_task_response(conn, task);
|
2016-09-14 01:38:01 +00:00
|
|
|
|
|
|
|
if ((task != primary) ||
|
|
|
|
(task->scsi.transfer_len == task->scsi.length)) {
|
|
|
|
spdk_iscsi_task_put(task);
|
|
|
|
}
|
2016-09-13 06:34:09 +00:00
|
|
|
process_completed_read_subtask_list(conn, primary);
|
2018-03-01 07:00:55 +00:00
|
|
|
|
|
|
|
spdk_iscsi_conn_handle_queued_datain_tasks(conn);
|
2016-08-02 16:34:45 +00:00
|
|
|
}
|
|
|
|
|
2017-06-08 20:45:41 +00:00
|
|
|
void
|
|
|
|
spdk_iscsi_task_cpl(struct spdk_scsi_task *scsi_task)
|
2016-08-02 16:34:45 +00:00
|
|
|
{
|
|
|
|
struct spdk_iscsi_task *primary;
|
2017-06-08 20:45:41 +00:00
|
|
|
struct spdk_iscsi_task *task = spdk_iscsi_task_from_scsi_task(scsi_task);
|
|
|
|
struct spdk_iscsi_conn *conn = task->conn;
|
2016-08-02 16:34:45 +00:00
|
|
|
|
|
|
|
spdk_trace_record(TRACE_ISCSI_TASK_DONE, conn->id, 0, (uintptr_t)task, 0);
|
|
|
|
|
|
|
|
primary = spdk_iscsi_task_get_primary(task);
|
|
|
|
|
|
|
|
if (spdk_iscsi_task_is_read(primary)) {
|
2016-09-28 06:13:07 +00:00
|
|
|
process_read_task_completion(conn, task, primary);
|
2016-08-02 16:34:45 +00:00
|
|
|
} else {
|
2017-05-03 21:46:35 +00:00
|
|
|
primary->bytes_completed += task->scsi.length;
|
2016-09-28 06:13:07 +00:00
|
|
|
if ((task != primary) &&
|
|
|
|
(task->scsi.status != SPDK_SCSI_STATUS_GOOD)) {
|
2018-06-11 04:20:23 +00:00
|
|
|
spdk_scsi_task_copy_status(&primary->scsi, &task->scsi);
|
2016-08-02 16:34:45 +00:00
|
|
|
}
|
|
|
|
|
2017-05-03 21:46:35 +00:00
|
|
|
if (primary->bytes_completed == primary->scsi.transfer_len) {
|
2017-05-03 20:36:58 +00:00
|
|
|
spdk_del_transfer_task(conn, primary->tag);
|
2016-08-02 16:34:45 +00:00
|
|
|
spdk_iscsi_task_response(conn, primary);
|
2016-10-14 03:34:12 +00:00
|
|
|
/*
|
|
|
|
* Check if this is the last task completed for an iSCSI write
|
|
|
|
* that required child subtasks. If task != primary, we know
|
|
|
|
* for sure that it was part of an iSCSI write with child subtasks.
|
|
|
|
* The trickier case is when the last task completed was the initial
|
|
|
|
* task - in this case the task will have a smaller length than
|
|
|
|
* the overall transfer length.
|
|
|
|
*/
|
|
|
|
if (task != primary || task->scsi.length != task->scsi.transfer_len) {
|
2016-08-02 16:34:45 +00:00
|
|
|
TAILQ_REMOVE(&conn->active_r2t_tasks, primary, link);
|
|
|
|
spdk_iscsi_task_put(primary);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
spdk_iscsi_task_put(task);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
spdk_iscsi_get_pdu_length(struct spdk_iscsi_pdu *pdu, int header_digest,
|
|
|
|
int data_digest)
|
|
|
|
{
|
|
|
|
int data_len, enable_digest, total;
|
|
|
|
|
|
|
|
enable_digest = 1;
|
|
|
|
if (pdu->bhs.opcode == ISCSI_OP_LOGIN_RSP) {
|
|
|
|
enable_digest = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
total = ISCSI_BHS_LEN;
|
|
|
|
|
|
|
|
total += (4 * pdu->bhs.total_ahs_len);
|
|
|
|
|
|
|
|
if (enable_digest && header_digest) {
|
|
|
|
total += ISCSI_DIGEST_LEN;
|
|
|
|
}
|
|
|
|
|
|
|
|
data_len = DGET24(pdu->bhs.data_segment_len);
|
|
|
|
if (data_len > 0) {
|
|
|
|
total += ISCSI_ALIGN(data_len);
|
|
|
|
if (enable_digest && data_digest) {
|
|
|
|
total += ISCSI_DIGEST_LEN;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return total;
|
|
|
|
}
|
|
|
|
|
2018-02-28 05:55:38 +00:00
|
|
|
void
|
2016-08-02 16:34:45 +00:00
|
|
|
spdk_iscsi_conn_handle_nop(struct spdk_iscsi_conn *conn)
|
|
|
|
{
|
|
|
|
uint64_t tsc;
|
|
|
|
|
2018-02-28 05:55:38 +00:00
|
|
|
/**
|
|
|
|
* This function will be executed by nop_poller of iSCSI polling group, so
|
|
|
|
* we need to check the connection state first, then do the nop interval
|
|
|
|
* expiration check work.
|
|
|
|
*/
|
|
|
|
if ((conn->state == ISCSI_CONN_STATE_EXITED) ||
|
|
|
|
(conn->state == ISCSI_CONN_STATE_EXITING)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-08-02 16:34:45 +00:00
|
|
|
/* Check for nop interval expiration */
|
2016-08-18 19:52:48 +00:00
|
|
|
tsc = spdk_get_ticks();
|
2016-08-02 16:34:45 +00:00
|
|
|
if (conn->nop_outstanding) {
|
2016-08-18 19:52:48 +00:00
|
|
|
if ((tsc - conn->last_nopin) > (conn->timeout * spdk_get_ticks_hz())) {
|
2016-08-02 16:34:45 +00:00
|
|
|
SPDK_ERRLOG("Timed out waiting for NOP-Out response from initiator\n");
|
|
|
|
SPDK_ERRLOG(" tsc=0x%lx, last_nopin=0x%lx\n", tsc, conn->last_nopin);
|
2018-03-13 07:55:40 +00:00
|
|
|
SPDK_ERRLOG(" initiator=%s, target=%s\n", conn->initiator_name,
|
|
|
|
conn->target_short_name);
|
2018-02-28 05:55:38 +00:00
|
|
|
conn->state = ISCSI_CONN_STATE_EXITING;
|
2016-08-02 16:34:45 +00:00
|
|
|
}
|
|
|
|
} else if (tsc - conn->last_nopin > conn->nopininterval) {
|
|
|
|
spdk_iscsi_send_nopin(conn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-04-25 17:35:22 +00:00
|
|
|
* \brief Makes one attempt to flush response PDUs back to the initiator.
|
|
|
|
*
|
|
|
|
* Builds a list of iovecs for response PDUs that must be sent back to the
|
|
|
|
* initiator and passes it to writev().
|
|
|
|
*
|
|
|
|
* Since the socket is non-blocking, writev() may not be able to flush all
|
|
|
|
* of the iovecs, and may even partially flush one of the iovecs. In this
|
|
|
|
* case, the partially flushed PDU will remain on the write_pdu_list with
|
|
|
|
* an offset pointing to the next byte to be flushed.
|
|
|
|
*
|
2018-01-18 05:57:41 +00:00
|
|
|
* Returns 0 if all PDUs were flushed.
|
|
|
|
*
|
|
|
|
* Returns 1 if some PDUs could not be flushed due to lack of send buffer
|
|
|
|
* space.
|
2017-04-25 17:35:22 +00:00
|
|
|
*
|
|
|
|
* Returns -1 if an exception error occurred indicating the TCP connection
|
|
|
|
* should be closed.
|
|
|
|
*/
|
2016-08-02 16:34:45 +00:00
|
|
|
static int
|
|
|
|
spdk_iscsi_conn_flush_pdus_internal(struct spdk_iscsi_conn *conn)
|
|
|
|
{
|
|
|
|
const int array_size = 32;
|
|
|
|
struct iovec iovec_array[array_size];
|
|
|
|
struct iovec *iov = iovec_array;
|
|
|
|
int iovec_cnt = 0;
|
|
|
|
int bytes = 0;
|
|
|
|
int total_length = 0;
|
|
|
|
uint32_t writev_offset;
|
|
|
|
struct spdk_iscsi_pdu *pdu;
|
|
|
|
int pdu_length;
|
|
|
|
|
|
|
|
pdu = TAILQ_FIRST(&conn->write_pdu_list);
|
|
|
|
|
|
|
|
if (pdu == NULL) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Build up a list of iovecs for the first few PDUs in the
|
|
|
|
* connection's write_pdu_list.
|
|
|
|
*/
|
|
|
|
while (pdu != NULL && ((array_size - iovec_cnt) >= 5)) {
|
|
|
|
pdu_length = spdk_iscsi_get_pdu_length(pdu,
|
|
|
|
conn->header_digest,
|
|
|
|
conn->data_digest);
|
|
|
|
iovec_cnt += spdk_iscsi_build_iovecs(conn,
|
|
|
|
&iovec_array[iovec_cnt],
|
|
|
|
pdu);
|
|
|
|
total_length += pdu_length;
|
|
|
|
pdu = TAILQ_NEXT(pdu, tailq);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check if the first PDU was partially written out the last time
|
|
|
|
* this function was called, and if so adjust the iovec array
|
|
|
|
* accordingly.
|
|
|
|
*/
|
|
|
|
writev_offset = TAILQ_FIRST(&conn->write_pdu_list)->writev_offset;
|
|
|
|
total_length -= writev_offset;
|
|
|
|
while (writev_offset > 0) {
|
|
|
|
if (writev_offset >= iov->iov_len) {
|
|
|
|
writev_offset -= iov->iov_len;
|
|
|
|
iov++;
|
|
|
|
iovec_cnt--;
|
|
|
|
} else {
|
|
|
|
iov->iov_len -= writev_offset;
|
|
|
|
iov->iov_base = (char *)iov->iov_base + writev_offset;
|
|
|
|
writev_offset = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
spdk_trace_record(TRACE_FLUSH_WRITEBUF_START, conn->id, total_length, 0, iovec_cnt);
|
|
|
|
|
|
|
|
bytes = spdk_sock_writev(conn->sock, iov, iovec_cnt);
|
|
|
|
if (bytes == -1) {
|
|
|
|
if (errno == EWOULDBLOCK || errno == EAGAIN) {
|
2018-01-18 05:57:41 +00:00
|
|
|
return 1;
|
2016-08-02 16:34:45 +00:00
|
|
|
} else {
|
2018-02-07 21:46:22 +00:00
|
|
|
SPDK_ERRLOG("spdk_sock_writev() failed, errno %d: %s\n",
|
|
|
|
errno, spdk_strerror(errno));
|
2016-08-02 16:34:45 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
spdk_trace_record(TRACE_FLUSH_WRITEBUF_DONE, conn->id, bytes, 0, 0);
|
|
|
|
|
|
|
|
pdu = TAILQ_FIRST(&conn->write_pdu_list);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Free any PDUs that were fully written. If a PDU was only
|
|
|
|
* partially written, update its writev_offset so that next
|
|
|
|
* time only the unwritten portion will be sent to writev().
|
|
|
|
*/
|
|
|
|
while (bytes > 0) {
|
|
|
|
pdu_length = spdk_iscsi_get_pdu_length(pdu,
|
|
|
|
conn->header_digest,
|
|
|
|
conn->data_digest);
|
|
|
|
pdu_length -= pdu->writev_offset;
|
|
|
|
|
|
|
|
if (bytes >= pdu_length) {
|
|
|
|
bytes -= pdu_length;
|
|
|
|
TAILQ_REMOVE(&conn->write_pdu_list, pdu, tailq);
|
|
|
|
|
|
|
|
if ((conn->full_feature) &&
|
|
|
|
(conn->sess->ErrorRecoveryLevel >= 1) &&
|
|
|
|
spdk_iscsi_is_deferred_free_pdu(pdu)) {
|
2017-08-30 18:06:33 +00:00
|
|
|
SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "stat_sn=%d\n",
|
2016-08-22 22:23:38 +00:00
|
|
|
from_be32(&pdu->bhs.stat_sn));
|
2016-08-02 16:34:45 +00:00
|
|
|
TAILQ_INSERT_TAIL(&conn->snack_pdu_list, pdu,
|
|
|
|
tailq);
|
|
|
|
} else {
|
2016-12-07 05:02:40 +00:00
|
|
|
spdk_iscsi_conn_free_pdu(conn, pdu);
|
2016-08-02 16:34:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pdu = TAILQ_FIRST(&conn->write_pdu_list);
|
|
|
|
} else {
|
|
|
|
pdu->writev_offset += bytes;
|
|
|
|
bytes = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-18 05:57:41 +00:00
|
|
|
return TAILQ_EMPTY(&conn->write_pdu_list) ? 0 : 1;
|
2016-08-02 16:34:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-04-25 17:35:22 +00:00
|
|
|
* \brief Flushes response PDUs back to the initiator.
|
|
|
|
*
|
|
|
|
* This function may return without all PDUs having flushed to the
|
|
|
|
* underlying TCP socket buffer - for example, in the case where the
|
|
|
|
* socket buffer is already full.
|
|
|
|
*
|
|
|
|
* During normal RUNNING connection state, if not all PDUs are flushed,
|
|
|
|
* then subsequent calls to this routine will eventually flush
|
|
|
|
* remaining PDUs.
|
|
|
|
*
|
|
|
|
* During other connection states (EXITING or LOGGED_OUT), this
|
|
|
|
* function will spin until all PDUs have successfully been flushed.
|
|
|
|
*
|
2018-01-18 05:57:41 +00:00
|
|
|
* Returns 0 for success and when all PDUs were able to be flushed.
|
|
|
|
*
|
|
|
|
* Returns 1 for success but when some PDUs could not be flushed due
|
|
|
|
* to lack of TCP buffer space.
|
2017-04-25 17:35:22 +00:00
|
|
|
*
|
|
|
|
* Returns -1 for an exceptional error indicating the TCP connection
|
|
|
|
* should be closed.
|
|
|
|
*/
|
2018-03-13 00:16:47 +00:00
|
|
|
static int
|
2018-01-19 17:22:41 +00:00
|
|
|
spdk_iscsi_conn_flush_pdus(void *_conn)
|
2016-08-02 16:34:45 +00:00
|
|
|
{
|
2018-01-19 17:22:41 +00:00
|
|
|
struct spdk_iscsi_conn *conn = _conn;
|
2016-08-02 16:34:45 +00:00
|
|
|
int rc;
|
|
|
|
|
|
|
|
if (conn->state == ISCSI_CONN_STATE_RUNNING) {
|
|
|
|
rc = spdk_iscsi_conn_flush_pdus_internal(conn);
|
2018-01-19 17:22:41 +00:00
|
|
|
if (rc == 0 && conn->flush_poller != NULL) {
|
|
|
|
spdk_poller_unregister(&conn->flush_poller);
|
|
|
|
} else if (rc == 1 && conn->flush_poller == NULL) {
|
|
|
|
conn->flush_poller = spdk_poller_register(spdk_iscsi_conn_flush_pdus, conn, 50);
|
|
|
|
}
|
2016-08-02 16:34:45 +00:00
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* If the connection state is not RUNNING, then
|
|
|
|
* keep trying to flush PDUs until our list is
|
|
|
|
* empty - to make sure all data is sent before
|
|
|
|
* closing the connection.
|
|
|
|
*/
|
2018-01-18 05:57:41 +00:00
|
|
|
do {
|
2016-08-02 16:34:45 +00:00
|
|
|
rc = spdk_iscsi_conn_flush_pdus_internal(conn);
|
2018-01-18 05:57:41 +00:00
|
|
|
} while (rc == 1);
|
2016-08-02 16:34:45 +00:00
|
|
|
}
|
|
|
|
|
2018-01-18 06:33:19 +00:00
|
|
|
if (rc < 0 && conn->state < ISCSI_CONN_STATE_EXITING) {
|
|
|
|
/*
|
|
|
|
* If the poller has already started destruction of the connection,
|
|
|
|
* i.e. the socket read failed, then the connection state may already
|
|
|
|
* be EXITED. We don't want to set it back to EXITING in that case.
|
|
|
|
*/
|
|
|
|
conn->state = ISCSI_CONN_STATE_EXITING;
|
|
|
|
}
|
2018-03-13 00:16:47 +00:00
|
|
|
|
|
|
|
return -1;
|
2018-01-18 06:34:47 +00:00
|
|
|
}
|
|
|
|
|
2018-01-19 17:22:41 +00:00
|
|
|
void
|
|
|
|
spdk_iscsi_conn_write_pdu(struct spdk_iscsi_conn *conn, struct spdk_iscsi_pdu *pdu)
|
|
|
|
{
|
|
|
|
TAILQ_INSERT_TAIL(&conn->write_pdu_list, pdu, tailq);
|
|
|
|
spdk_iscsi_conn_flush_pdus(conn);
|
|
|
|
}
|
|
|
|
|
2016-08-02 16:34:45 +00:00
|
|
|
#define GET_PDU_LOOP_COUNT 16
|
|
|
|
|
|
|
|
static int
|
|
|
|
spdk_iscsi_conn_handle_incoming_pdus(struct spdk_iscsi_conn *conn)
|
|
|
|
{
|
|
|
|
struct spdk_iscsi_pdu *pdu;
|
|
|
|
int i, rc;
|
|
|
|
|
|
|
|
/* Read new PDUs from network */
|
|
|
|
for (i = 0; i < GET_PDU_LOOP_COUNT; i++) {
|
|
|
|
rc = spdk_iscsi_read_pdu(conn, &pdu);
|
|
|
|
if (rc == 0) {
|
|
|
|
break;
|
|
|
|
} else if (rc == SPDK_ISCSI_CONNECTION_FATAL) {
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (conn->state == ISCSI_CONN_STATE_LOGGED_OUT) {
|
|
|
|
SPDK_ERRLOG("pdu received after logout\n");
|
|
|
|
spdk_put_pdu(pdu);
|
|
|
|
return SPDK_ISCSI_CONNECTION_FATAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = spdk_iscsi_execute(conn, pdu);
|
|
|
|
spdk_put_pdu(pdu);
|
|
|
|
if (rc != 0) {
|
|
|
|
SPDK_ERRLOG("spdk_iscsi_execute() fatal error on %s(%s)\n",
|
2017-05-03 19:41:55 +00:00
|
|
|
conn->target_port != NULL ? spdk_scsi_port_get_name(conn->target_port) : "NULL",
|
|
|
|
conn->initiator_port != NULL ? spdk_scsi_port_get_name(conn->initiator_port) : "NULL");
|
2016-08-02 16:34:45 +00:00
|
|
|
return rc;
|
|
|
|
}
|
2018-05-04 06:43:59 +00:00
|
|
|
|
|
|
|
if (conn->is_stopped) {
|
|
|
|
break;
|
|
|
|
}
|
2016-08-02 16:34:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2018-02-14 02:26:10 +00:00
|
|
|
static void
|
|
|
|
spdk_iscsi_conn_sock_cb(void *arg, struct spdk_sock_group *group, struct spdk_sock *sock)
|
|
|
|
{
|
|
|
|
struct spdk_iscsi_conn *conn = arg;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
assert(conn != NULL);
|
|
|
|
|
2018-03-01 18:12:09 +00:00
|
|
|
if ((conn->state == ISCSI_CONN_STATE_EXITED) ||
|
|
|
|
(conn->state == ISCSI_CONN_STATE_EXITING)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-02-14 02:26:10 +00:00
|
|
|
/* Handle incoming PDUs */
|
|
|
|
rc = spdk_iscsi_conn_handle_incoming_pdus(conn);
|
|
|
|
if (rc < 0) {
|
|
|
|
conn->state = ISCSI_CONN_STATE_EXITING;
|
|
|
|
spdk_iscsi_conn_flush_pdus(conn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-11 23:00:45 +00:00
|
|
|
static void
|
2017-01-05 01:19:02 +00:00
|
|
|
spdk_iscsi_conn_full_feature_migrate(void *arg1, void *arg2)
|
2016-08-11 23:00:45 +00:00
|
|
|
{
|
2017-01-05 01:19:02 +00:00
|
|
|
struct spdk_iscsi_conn *conn = arg1;
|
2016-08-11 23:00:45 +00:00
|
|
|
|
2016-09-20 19:45:37 +00:00
|
|
|
if (conn->sess->session_type == SESSION_TYPE_NORMAL) {
|
2018-07-04 05:23:55 +00:00
|
|
|
spdk_iscsi_conn_open_luns(conn);
|
2016-09-20 19:45:37 +00:00
|
|
|
}
|
|
|
|
|
2016-08-11 23:00:45 +00:00
|
|
|
/* The poller has been unregistered, so now we can re-register it on the new core. */
|
2017-03-27 19:59:40 +00:00
|
|
|
conn->lcore = spdk_env_get_current_core();
|
2018-03-05 00:19:27 +00:00
|
|
|
spdk_iscsi_poll_group_add_conn(conn);
|
2016-08-11 23:00:45 +00:00
|
|
|
}
|
|
|
|
|
2016-08-02 16:34:45 +00:00
|
|
|
void
|
2018-03-05 00:01:00 +00:00
|
|
|
spdk_iscsi_conn_migration(struct spdk_iscsi_conn *conn)
|
2016-08-02 16:34:45 +00:00
|
|
|
{
|
|
|
|
int lcore;
|
2016-08-11 23:00:45 +00:00
|
|
|
struct spdk_event *event;
|
2018-03-05 00:01:00 +00:00
|
|
|
struct spdk_iscsi_tgt_node *target;
|
2016-08-02 16:34:45 +00:00
|
|
|
|
2018-03-05 00:01:00 +00:00
|
|
|
lcore = spdk_iscsi_conn_allocate_reactor(conn->portal->cpumask);
|
|
|
|
if (conn->sess->session_type == SESSION_TYPE_NORMAL) {
|
|
|
|
target = conn->sess->target;
|
|
|
|
pthread_mutex_lock(&target->mutex);
|
|
|
|
target->num_active_conns++;
|
|
|
|
if (target->num_active_conns == 1) {
|
|
|
|
/**
|
|
|
|
* This is the only active connection for this target node.
|
|
|
|
* Save the lcore in the target node so it can be used for
|
|
|
|
* any other connections to this target node.
|
|
|
|
*/
|
|
|
|
target->lcore = lcore;
|
|
|
|
} else {
|
|
|
|
/**
|
|
|
|
* There are other active connections for this target node.
|
|
|
|
* Ignore the lcore specified by the allocator and use the
|
|
|
|
* the target node's lcore to ensure this connection runs on
|
|
|
|
* the same lcore as other connections for this target node.
|
|
|
|
*/
|
|
|
|
lcore = target->lcore;
|
|
|
|
}
|
|
|
|
pthread_mutex_unlock(&target->mutex);
|
2017-12-07 23:23:48 +00:00
|
|
|
}
|
2016-08-02 16:34:45 +00:00
|
|
|
|
2018-03-05 00:01:00 +00:00
|
|
|
spdk_iscsi_poll_group_remove_conn_sock(conn);
|
|
|
|
spdk_iscsi_conn_stop(conn);
|
2018-01-22 19:38:56 +00:00
|
|
|
|
2018-03-05 00:01:00 +00:00
|
|
|
__sync_fetch_and_add(&g_num_connections[lcore], 1);
|
2018-03-08 04:05:46 +00:00
|
|
|
conn->last_nopin = spdk_get_ticks();
|
2018-03-05 00:01:00 +00:00
|
|
|
event = spdk_event_allocate(lcore, spdk_iscsi_conn_full_feature_migrate,
|
|
|
|
conn, NULL);
|
|
|
|
spdk_event_call(event);
|
|
|
|
}
|
2018-01-22 19:38:56 +00:00
|
|
|
|
2016-08-02 16:34:45 +00:00
|
|
|
void
|
|
|
|
spdk_iscsi_conn_set_min_per_core(int count)
|
|
|
|
{
|
|
|
|
g_connections_per_lcore = count;
|
|
|
|
}
|
|
|
|
|
2018-01-24 06:57:10 +00:00
|
|
|
int
|
|
|
|
spdk_iscsi_conn_get_min_per_core(void)
|
|
|
|
{
|
|
|
|
return g_connections_per_lcore;
|
|
|
|
}
|
|
|
|
|
2016-08-02 16:34:45 +00:00
|
|
|
static uint32_t
|
2017-12-21 16:48:31 +00:00
|
|
|
spdk_iscsi_conn_allocate_reactor(const struct spdk_cpuset *cpumask)
|
2016-08-02 16:34:45 +00:00
|
|
|
{
|
|
|
|
uint32_t i, selected_core;
|
|
|
|
int32_t num_pollers, min_pollers;
|
|
|
|
|
|
|
|
min_pollers = INT_MAX;
|
2017-12-20 00:19:43 +00:00
|
|
|
selected_core = spdk_env_get_first_core();
|
2016-08-02 16:34:45 +00:00
|
|
|
|
2017-12-20 00:19:43 +00:00
|
|
|
SPDK_ENV_FOREACH_CORE(i) {
|
2017-12-21 16:48:31 +00:00
|
|
|
if (!spdk_cpuset_get_cpu(cpumask, i)) {
|
2016-08-02 16:34:45 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-12-20 00:19:43 +00:00
|
|
|
/* This core is running. Check how many pollers it already has. */
|
|
|
|
num_pollers = g_num_connections[i];
|
2016-08-02 16:34:45 +00:00
|
|
|
|
2017-12-20 00:19:43 +00:00
|
|
|
if ((num_pollers > 0) && (num_pollers < g_connections_per_lcore)) {
|
|
|
|
/* Fewer than the maximum connections per core,
|
|
|
|
* but at least 1. Use this core.
|
|
|
|
*/
|
|
|
|
return i;
|
|
|
|
} else if (num_pollers < min_pollers) {
|
|
|
|
/* Track the core that has the minimum number of pollers
|
|
|
|
* to be used if no cores meet our criteria
|
|
|
|
*/
|
|
|
|
selected_core = i;
|
|
|
|
min_pollers = num_pollers;
|
2016-08-02 16:34:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return selected_core;
|
|
|
|
}
|
|
|
|
|
2018-03-13 00:16:47 +00:00
|
|
|
static int
|
2016-10-05 17:23:48 +00:00
|
|
|
logout_timeout(void *arg)
|
2016-08-02 16:34:45 +00:00
|
|
|
{
|
|
|
|
struct spdk_iscsi_conn *conn = arg;
|
|
|
|
|
|
|
|
spdk_iscsi_conn_destruct(conn);
|
2018-03-13 00:16:47 +00:00
|
|
|
|
|
|
|
return -1;
|
2016-08-02 16:34:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
spdk_iscsi_conn_logout(struct spdk_iscsi_conn *conn)
|
|
|
|
{
|
|
|
|
conn->state = ISCSI_CONN_STATE_LOGGED_OUT;
|
2017-11-14 20:42:17 +00:00
|
|
|
conn->logout_timer = spdk_poller_register(logout_timeout, conn, ISCSI_LOGOUT_TIMEOUT * 1000000);
|
2016-08-02 16:34:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SPDK_TRACE_REGISTER_FN(iscsi_conn_trace)
|
|
|
|
{
|
|
|
|
spdk_trace_register_owner(OWNER_ISCSI_CONN, 'c');
|
|
|
|
spdk_trace_register_object(OBJECT_ISCSI_PDU, 'p');
|
|
|
|
spdk_trace_register_description("READ FROM SOCKET DONE", "", TRACE_READ_FROM_SOCKET_DONE,
|
|
|
|
OWNER_ISCSI_CONN, OBJECT_NONE, 0, 0, 0, "");
|
|
|
|
spdk_trace_register_description("FLUSH WRITEBUF START", "", TRACE_FLUSH_WRITEBUF_START,
|
|
|
|
OWNER_ISCSI_CONN, OBJECT_NONE, 0, 0, 0, "iovec: ");
|
|
|
|
spdk_trace_register_description("FLUSH WRITEBUF DONE", "", TRACE_FLUSH_WRITEBUF_DONE,
|
|
|
|
OWNER_ISCSI_CONN, OBJECT_NONE, 0, 0, 0, "");
|
|
|
|
spdk_trace_register_description("READ PDU", "", TRACE_READ_PDU,
|
|
|
|
OWNER_ISCSI_CONN, OBJECT_ISCSI_PDU, 1, 0, 0, "opc: ");
|
|
|
|
spdk_trace_register_description("ISCSI TASK DONE", "", TRACE_ISCSI_TASK_DONE,
|
2016-08-03 21:37:16 +00:00
|
|
|
OWNER_ISCSI_CONN, OBJECT_SCSI_TASK, 0, 0, 0, "");
|
2016-08-02 16:34:45 +00:00
|
|
|
spdk_trace_register_description("ISCSI TASK QUEUE", "", TRACE_ISCSI_TASK_QUEUE,
|
2016-08-03 21:37:16 +00:00
|
|
|
OWNER_ISCSI_CONN, OBJECT_SCSI_TASK, 1, 1, 0, "pdu: ");
|
2016-08-02 16:34:45 +00:00
|
|
|
spdk_trace_register_description("ISCSI CONN ACTIVE", "", TRACE_ISCSI_CONN_ACTIVE,
|
|
|
|
OWNER_ISCSI_CONN, OBJECT_NONE, 0, 0, 0, "");
|
|
|
|
spdk_trace_register_description("ISCSI CONN IDLE", "", TRACE_ISCSI_CONN_IDLE,
|
|
|
|
OWNER_ISCSI_CONN, OBJECT_NONE, 0, 0, 0, "");
|
|
|
|
}
|