Skip to content

Commit 71c48eb

Browse files
Christoph Hellwigdavem330
authored andcommitted
tcp: add tcp_sock_set_keepidle
Add a helper to directly set the TCP_KEEP_IDLE sockopt from kernel space without going through a fake uaccess. Signed-off-by: Christoph Hellwig <hch@lst.de> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent c488aea commit 71c48eb

4 files changed

Lines changed: 37 additions & 21 deletions

File tree

‎include/linux/tcp.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,7 @@ int tcp_skb_shift(struct sk_buff *to, struct sk_buff *from, int pcount,
498498
int shiftlen);
499499

500500
void tcp_sock_set_cork(struct sock *sk, bool on);
501+
int tcp_sock_set_keepidle(struct sock *sk, int val);
501502
void tcp_sock_set_nodelay(struct sock *sk);
502503
void tcp_sock_set_quickack(struct sock *sk, int val);
503504
int tcp_sock_set_syncnt(struct sock *sk, int val);

‎net/ipv4/tcp.c‎

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2901,6 +2901,39 @@ void tcp_sock_set_user_timeout(struct sock *sk, u32 val)
29012901
}
29022902
EXPORT_SYMBOL(tcp_sock_set_user_timeout);
29032903

2904+
static int __tcp_sock_set_keepidle(struct sock *sk, int val)
2905+
{
2906+
struct tcp_sock *tp = tcp_sk(sk);
2907+
2908+
if (val < 1 || val > MAX_TCP_KEEPIDLE)
2909+
return -EINVAL;
2910+
2911+
tp->keepalive_time = val * HZ;
2912+
if (sock_flag(sk, SOCK_KEEPOPEN) &&
2913+
!((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_LISTEN))) {
2914+
u32 elapsed = keepalive_time_elapsed(tp);
2915+
2916+
if (tp->keepalive_time > elapsed)
2917+
elapsed = tp->keepalive_time - elapsed;
2918+
else
2919+
elapsed = 0;
2920+
inet_csk_reset_keepalive_timer(sk, elapsed);
2921+
}
2922+
2923+
return 0;
2924+
}
2925+
2926+
int tcp_sock_set_keepidle(struct sock *sk, int val)
2927+
{
2928+
int err;
2929+
2930+
lock_sock(sk);
2931+
err = __tcp_sock_set_keepidle(sk, val);
2932+
release_sock(sk);
2933+
return err;
2934+
}
2935+
EXPORT_SYMBOL(tcp_sock_set_keepidle);
2936+
29042937
/*
29052938
* Socket option code for TCP.
29062939
*/
@@ -3070,21 +3103,7 @@ static int do_tcp_setsockopt(struct sock *sk, int level,
30703103
break;
30713104

30723105
case TCP_KEEPIDLE:
3073-
if (val < 1 || val > MAX_TCP_KEEPIDLE)
3074-
err = -EINVAL;
3075-
else {
3076-
tp->keepalive_time = val * HZ;
3077-
if (sock_flag(sk, SOCK_KEEPOPEN) &&
3078-
!((1 << sk->sk_state) &
3079-
(TCPF_CLOSE | TCPF_LISTEN))) {
3080-
u32 elapsed = keepalive_time_elapsed(tp);
3081-
if (tp->keepalive_time > elapsed)
3082-
elapsed = tp->keepalive_time - elapsed;
3083-
else
3084-
elapsed = 0;
3085-
inet_csk_reset_keepalive_timer(sk, elapsed);
3086-
}
3087-
}
3106+
err = __tcp_sock_set_keepidle(sk, val);
30883107
break;
30893108
case TCP_KEEPINTVL:
30903109
if (val < 1 || val > MAX_TCP_KEEPINTVL)

‎net/rds/tcp_listen.c‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,7 @@ int rds_tcp_keepalive(struct socket *sock)
5252
if (ret < 0)
5353
goto bail;
5454

55-
ret = kernel_setsockopt(sock, IPPROTO_TCP, TCP_KEEPIDLE,
56-
(char *)&keepidle, sizeof(keepidle));
57-
if (ret < 0)
58-
goto bail;
55+
tcp_sock_set_keepidle(sock->sk, keepidle);
5956

6057
/* KEEPINTVL is the interval between successive probes. We follow
6158
* the model in xs_tcp_finish_connecting() and re-use keepidle.

‎net/sunrpc/xprtsock.c‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2107,8 +2107,7 @@ static void xs_tcp_set_socket_timeouts(struct rpc_xprt *xprt,
21072107

21082108
/* TCP Keepalive options */
21092109
sock_set_keepalive(sock->sk);
2110-
kernel_setsockopt(sock, SOL_TCP, TCP_KEEPIDLE,
2111-
(char *)&keepidle, sizeof(keepidle));
2110+
tcp_sock_set_keepidle(sock->sk, keepidle);
21122111
kernel_setsockopt(sock, SOL_TCP, TCP_KEEPINTVL,
21132112
(char *)&keepidle, sizeof(keepidle));
21142113
kernel_setsockopt(sock, SOL_TCP, TCP_KEEPCNT,

0 commit comments

Comments
 (0)