Skip to content

Commit f711a6a

Browse files
sowminivdavem330
authored andcommitted
net/rds: RDS-TCP: Always create a new rds_sock for an incoming connection.
When running RDS over TCP, the active (client) side connects to the listening ("passive") side at the RDS_TCP_PORT. After the connection is established, if the client side reboots (potentially without even sending a FIN) the server still has a TCP socket in the esablished state. If the server-side now gets a new SYN comes from the client with a different client port, TCP will create a new socket-pair, but the RDS layer will incorrectly pull up the old rds_connection (which is still associated with the stale t_sock and RDS socket state). This patch corrects this behavior by having rds_tcp_accept_one() always create a new connection for an incoming TCP SYN. The rds and tcp state associated with the old socket-pair is cleaned up via the rds_tcp_state_change() callback which would typically be invoked in most cases when the client-TCP sends a FIN on TCP restart, triggering a transition to CLOSE_WAIT state. In the rarer event of client death without a FIN, TCP_KEEPALIVE probes on the socket will detect the stale socket, and the TCP transition to CLOSE state will trigger the RDS state cleanup. Signed-off-by: Sowmini Varadhan <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent e16e888 commit f711a6a

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

net/rds/connection.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,10 @@ static struct rds_connection *__rds_conn_create(__be32 laddr, __be32 faddr,
126126
struct rds_transport *loop_trans;
127127
unsigned long flags;
128128
int ret;
129+
struct rds_transport *otrans = trans;
129130

131+
if (!is_outgoing && otrans->t_type == RDS_TRANS_TCP)
132+
goto new_conn;
130133
rcu_read_lock();
131134
conn = rds_conn_lookup(head, laddr, faddr, trans);
132135
if (conn && conn->c_loopback && conn->c_trans != &rds_loop_transport &&
@@ -142,6 +145,7 @@ static struct rds_connection *__rds_conn_create(__be32 laddr, __be32 faddr,
142145
if (conn)
143146
goto out;
144147

148+
new_conn:
145149
conn = kmem_cache_zalloc(rds_conn_slab, gfp);
146150
if (!conn) {
147151
conn = ERR_PTR(-ENOMEM);

net/rds/tcp_connect.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ void rds_tcp_state_change(struct sock *sk)
6262
case TCP_ESTABLISHED:
6363
rds_connect_complete(conn);
6464
break;
65+
case TCP_CLOSE_WAIT:
6566
case TCP_CLOSE:
6667
rds_conn_drop(conn);
6768
default:

net/rds/tcp_listen.c

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,45 @@ static void rds_tcp_accept_worker(struct work_struct *work);
4545
static DECLARE_WORK(rds_tcp_listen_work, rds_tcp_accept_worker);
4646
static struct socket *rds_tcp_listen_sock;
4747

48+
static int rds_tcp_keepalive(struct socket *sock)
49+
{
50+
/* values below based on xs_udp_default_timeout */
51+
int keepidle = 5; /* send a probe 'keepidle' secs after last data */
52+
int keepcnt = 5; /* number of unack'ed probes before declaring dead */
53+
int keepalive = 1;
54+
int ret = 0;
55+
56+
ret = kernel_setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE,
57+
(char *)&keepalive, sizeof(keepalive));
58+
if (ret < 0)
59+
goto bail;
60+
61+
ret = kernel_setsockopt(sock, IPPROTO_TCP, TCP_KEEPCNT,
62+
(char *)&keepcnt, sizeof(keepcnt));
63+
if (ret < 0)
64+
goto bail;
65+
66+
ret = kernel_setsockopt(sock, IPPROTO_TCP, TCP_KEEPIDLE,
67+
(char *)&keepidle, sizeof(keepidle));
68+
if (ret < 0)
69+
goto bail;
70+
71+
/* KEEPINTVL is the interval between successive probes. We follow
72+
* the model in xs_tcp_finish_connecting() and re-use keepidle.
73+
*/
74+
ret = kernel_setsockopt(sock, IPPROTO_TCP, TCP_KEEPINTVL,
75+
(char *)&keepidle, sizeof(keepidle));
76+
bail:
77+
return ret;
78+
}
79+
4880
static int rds_tcp_accept_one(struct socket *sock)
4981
{
5082
struct socket *new_sock = NULL;
5183
struct rds_connection *conn;
5284
int ret;
5385
struct inet_sock *inet;
86+
struct rds_tcp_connection *rs_tcp;
5487

5588
ret = sock_create_lite(sock->sk->sk_family, sock->sk->sk_type,
5689
sock->sk->sk_protocol, &new_sock);
@@ -63,6 +96,10 @@ static int rds_tcp_accept_one(struct socket *sock)
6396
if (ret < 0)
6497
goto out;
6598

99+
ret = rds_tcp_keepalive(new_sock);
100+
if (ret < 0)
101+
goto out;
102+
66103
rds_tcp_tune(new_sock);
67104

68105
inet = inet_sk(new_sock->sk);
@@ -77,6 +114,15 @@ static int rds_tcp_accept_one(struct socket *sock)
77114
ret = PTR_ERR(conn);
78115
goto out;
79116
}
117+
/* An incoming SYN request came in, and TCP just accepted it.
118+
* We always create a new conn for listen side of TCP, and do not
119+
* add it to the c_hash_list.
120+
*
121+
* If the client reboots, this conn will need to be cleaned up.
122+
* rds_tcp_state_change() will do that cleanup
123+
*/
124+
rs_tcp = (struct rds_tcp_connection *)conn->c_transport_data;
125+
WARN_ON(!rs_tcp || rs_tcp->t_sock);
80126

81127
/*
82128
* see the comment above rds_queue_delayed_reconnect()

0 commit comments

Comments
 (0)