Merge branch '33-tcp-keepalive-should-be-applied-to-connection-so-that-dead-connections-can-be-properly-reaped' into 'develop'

apply tcp keepalive to serving sockets

Closes #33

See merge request open-source/flexnbd-c!33
This commit is contained in:
Patrick J Cherry
2018-01-10 17:51:02 +00:00
5 changed files with 58 additions and 0 deletions

6
debian/changelog vendored
View File

@@ -1,3 +1,9 @@
flexnbd (0.1.8) UNRELEASED; urgency=medium
* Set TCP keepalive on sockets so broken connections are reaped (#33, !33)
-- James Carter <james.carter@bytemark.co.uk> Wed, 10 Jan 2018 13:57:10 +0000
flexnbd (0.1.7) stable; urgency=medium
* Return bytes_left in migration statistics.

View File

@@ -68,6 +68,37 @@ int sock_set_reuseaddr( int fd, int optval )
return setsockopt( fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval) );
}
int sock_set_keepalive_params( int fd, int time, int intvl, int probes)
{
if (sock_set_keepalive(fd, 1) ||
sock_set_tcp_keepidle(fd, time) ||
sock_set_tcp_keepintvl(fd, intvl) ||
sock_set_tcp_keepcnt(fd, probes)) {
return -1;
}
return 0;
}
int sock_set_keepalive( int fd, int optval )
{
return setsockopt( fd, SOL_SOCKET, SO_KEEPALIVE, &optval, sizeof(optval) );
}
int sock_set_tcp_keepidle( int fd, int optval )
{
return setsockopt( fd, IPPROTO_TCP, TCP_KEEPIDLE, &optval, sizeof(optval) );
}
int sock_set_tcp_keepintvl( int fd, int optval )
{
return setsockopt( fd, IPPROTO_TCP, TCP_KEEPINTVL, &optval, sizeof(optval) );
}
int sock_set_tcp_keepcnt( int fd, int optval )
{
return setsockopt( fd, IPPROTO_TCP, TCP_KEEPCNT, &optval, sizeof(optval) );
}
/* Set the tcp_nodelay option */
int sock_set_tcp_nodelay( int fd, int optval )
{

View File

@@ -14,9 +14,24 @@ size_t sockaddr_size(const struct sockaddr* sa);
*/
const char* sockaddr_address_string(const struct sockaddr* sa, char* dest, size_t len);
/* Configure TCP keepalive on a socket */
int sock_set_keepalive_params( int fd, int time, int intvl, int probes);
/* Set the SOL_KEEPALIVE otion */
int sock_set_keepalive(int fd, int optval);
/* Set the SOL_REUSEADDR otion */
int sock_set_reuseaddr(int fd, int optval);
/* Set the tcp_keepidle option */
int sock_set_tcp_keepidle(int fd, int optval);
/* Set the tcp_keepintvl option */
int sock_set_tcp_keepintvl(int fd, int optval);
/* Set the tcp_keepcnt option */
int sock_set_tcp_keepcnt(int fd, int optval);
/* Set the tcp_nodelay option */
int sock_set_tcp_nodelay(int fd, int optval);

View File

@@ -422,6 +422,9 @@ void accept_nbd_client(
int slot;
char s_client_address[64] = {0};
FATAL_IF_NEGATIVE( sock_set_keepalive_params( client_fd, CLIENT_KEEPALIVE_TIME, CLIENT_KEEPALIVE_INTVL, CLIENT_KEEPALIVE_PROBES),
"Error setting keepalive parameters on client socket fd %d", client_fd );
if ( !server_should_accept_client( params, client_address, s_client_address, 64 ) ) {
FATAL_IF_NEGATIVE( close( client_fd ),

View File

@@ -21,6 +21,9 @@ struct client_tbl_entry {
#define MAX_NBD_CLIENTS 16
#define CLIENT_KEEPALIVE_TIME 30
#define CLIENT_KEEPALIVE_INTVL 10
#define CLIENT_KEEPALIVE_PROBES 3
struct server {
/* The flexnbd wrapper this server is attached to */
struct flexnbd * flexnbd;