From b6782dd7c8a733b0d7ffcb148de3f350355f3ff1 Mon Sep 17 00:00:00 2001 From: Ronnie Sahlberg Date: Wed, 13 Apr 2011 21:35:51 +1000 Subject: [PATCH] TCP KEEPALIVE support Add tcp keeplaive support on platforms that support these socket options. Set default to fail the socket after 120 seconds --- configure.ac | 10 ++++++++++ include/iscsi.h | 6 +++++- lib/socket.c | 39 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 1 deletion(-) diff --git a/configure.ac b/configure.ac index 8eff907..1108fdd 100644 --- a/configure.ac +++ b/configure.ac @@ -64,6 +64,16 @@ if test x"$libiscsi_cv_HAVE_SOCK_SIN_LEN" = x"yes"; then AC_DEFINE(HAVE_SOCK_SIN_LEN,1,[Whether the sockaddr_in struct has a sin_len property]) fi +AC_CACHE_CHECK([for tcp keepalive],libiscsi_cv_HAVE_TCP_KEEPALIVE,[ +AC_TRY_COMPILE([#include +#include +#include ], +[int foo = TCP_KEEPCNT + TCP_KEEPIDLE + TCP_KEEPINTVL], +libiscsi_cv_HAVE_TCP_KEEPALIVE=yes,libiscsi_cv_HAVE_TCP_KEEPALIVE=no)]) +if test x"$libiscsi_cv_HAVE_TCP_KEEPALIVE" = x"yes"; then + AC_DEFINE(HAVE_TCP_KEEPALIVE,1,[Whether we have support for tcp keepalive socket options]) +fi + AC_MSG_CHECKING(whether libpopt is available) ac_save_CFLAGS="$CFLAGS" ac_save_LIBS="$LIBS" diff --git a/include/iscsi.h b/include/iscsi.h index afd9acf..c86dbe3 100644 --- a/include/iscsi.h +++ b/include/iscsi.h @@ -54,7 +54,11 @@ int iscsi_service(struct iscsi_context *iscsi, int revents); */ int iscsi_queue_length(struct iscsi_context *iscsi); - +/* + * To set tcp keepalive for the session + * ONLY available on platforms with tcp keepalive socket options + */ +int iscsi_set_tcp_keepalive(struct iscsi_context *iscsi, int idle, int count, int interval); diff --git a/lib/socket.c b/lib/socket.c index b54d882..30d6ecf 100644 --- a/lib/socket.c +++ b/lib/socket.c @@ -23,7 +23,9 @@ #include #include #include +#include #include +#include #include #include #include @@ -411,6 +413,9 @@ iscsi_service(struct iscsi_context *iscsi, int revents) return -1; } +#ifdef HAVE_TCP_KEEPALIVE + iscsi_set_tcp_keepalive(iscsi, 30, 3, 30); +#endif iscsi->is_connected = 1; iscsi->socket_status_cb(iscsi, SCSI_STATUS_GOOD, NULL, iscsi->connect_data); @@ -476,3 +481,37 @@ iscsi_free_iscsi_inqueue(struct iscsi_in_pdu *inqueue) inqueue = next; } } + +#ifdef HAVE_TCP_KEEPALIVE +int iscsi_set_tcp_keepalive(struct iscsi_context *iscsi, int idle, int count, int interval) +{ + int value; + + value =1; + if (setsockopt(iscsi->fd, SOL_SOCKET, SO_KEEPALIVE, &value, sizeof(value)) != 0) { + iscsi_set_error(iscsi, "TCP: Failed to set socket option SO_KEEPALIVE. Error %s(%d)", strerror(errno), errno); + return -1; + } + + value = count; + if (setsockopt(iscsi->fd, SOL_TCP, TCP_KEEPCNT, &value, sizeof(value)) != 0) { + iscsi_set_error(iscsi, "TCP: Failed to set tcp keepalive count. Error %s(%d)", strerror(errno), errno); + return -1; + } + + value = interval; + if (setsockopt(iscsi->fd, SOL_TCP, TCP_KEEPINTVL, &value, sizeof(value)) != 0) { + iscsi_set_error(iscsi, "TCP: Failed to set tcp keepalive interval. Error %s(%d)", strerror(errno), errno); + return -1; + } + + value = idle; + if (setsockopt(iscsi->fd, SOL_TCP, TCP_KEEPIDLE, &value, sizeof(value)) != 0) { + iscsi_set_error(iscsi, "TCP: Failed to set tcp keepalive idle. Error %s(%d)", strerror(errno), errno); + return -1; + } + + return 0; +} + +#endif