]> git.baikalelectronics.ru Git - kernel.git/commitdiff
tcp: add tcp_sock_set_keepidle
authorChristoph Hellwig <hch@lst.de>
Thu, 28 May 2020 05:12:23 +0000 (07:12 +0200)
committerDavid S. Miller <davem@davemloft.net>
Thu, 28 May 2020 18:11:45 +0000 (11:11 -0700)
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>
include/linux/tcp.h
net/ipv4/tcp.c
net/rds/tcp_listen.c
net/sunrpc/xprtsock.c

index de682143efe4d4945533d8ab8e85d65b0386b060..5724dd84a85edeaadfc61edc40d24c0dc6ca9f97 100644 (file)
@@ -498,6 +498,7 @@ int tcp_skb_shift(struct sk_buff *to, struct sk_buff *from, int pcount,
                  int shiftlen);
 
 void tcp_sock_set_cork(struct sock *sk, bool on);
+int tcp_sock_set_keepidle(struct sock *sk, int val);
 void tcp_sock_set_nodelay(struct sock *sk);
 void tcp_sock_set_quickack(struct sock *sk, int val);
 int tcp_sock_set_syncnt(struct sock *sk, int val);
index 0004bd9ae7b0a76634dde2c913a57fc749a63e2d..bdf0ff9333514516c2a465db0ba0a40461185ae9 100644 (file)
@@ -2901,6 +2901,39 @@ void tcp_sock_set_user_timeout(struct sock *sk, u32 val)
 }
 EXPORT_SYMBOL(tcp_sock_set_user_timeout);
 
+static int __tcp_sock_set_keepidle(struct sock *sk, int val)
+{
+       struct tcp_sock *tp = tcp_sk(sk);
+
+       if (val < 1 || val > MAX_TCP_KEEPIDLE)
+               return -EINVAL;
+
+       tp->keepalive_time = val * HZ;
+       if (sock_flag(sk, SOCK_KEEPOPEN) &&
+           !((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_LISTEN))) {
+               u32 elapsed = keepalive_time_elapsed(tp);
+
+               if (tp->keepalive_time > elapsed)
+                       elapsed = tp->keepalive_time - elapsed;
+               else
+                       elapsed = 0;
+               inet_csk_reset_keepalive_timer(sk, elapsed);
+       }
+
+       return 0;
+}
+
+int tcp_sock_set_keepidle(struct sock *sk, int val)
+{
+       int err;
+
+       lock_sock(sk);
+       err = __tcp_sock_set_keepidle(sk, val);
+       release_sock(sk);
+       return err;
+}
+EXPORT_SYMBOL(tcp_sock_set_keepidle);
+
 /*
  *     Socket option code for TCP.
  */
@@ -3070,21 +3103,7 @@ static int do_tcp_setsockopt(struct sock *sk, int level,
                break;
 
        case TCP_KEEPIDLE:
-               if (val < 1 || val > MAX_TCP_KEEPIDLE)
-                       err = -EINVAL;
-               else {
-                       tp->keepalive_time = val * HZ;
-                       if (sock_flag(sk, SOCK_KEEPOPEN) &&
-                           !((1 << sk->sk_state) &
-                             (TCPF_CLOSE | TCPF_LISTEN))) {
-                               u32 elapsed = keepalive_time_elapsed(tp);
-                               if (tp->keepalive_time > elapsed)
-                                       elapsed = tp->keepalive_time - elapsed;
-                               else
-                                       elapsed = 0;
-                               inet_csk_reset_keepalive_timer(sk, elapsed);
-                       }
-               }
+               err = __tcp_sock_set_keepidle(sk, val);
                break;
        case TCP_KEEPINTVL:
                if (val < 1 || val > MAX_TCP_KEEPINTVL)
index 6f90ea077adcdc153445647edc356b8f894827d2..79f9adc008114a25f5b0b9fb4b76fadd4d42834a 100644 (file)
@@ -52,10 +52,7 @@ int rds_tcp_keepalive(struct socket *sock)
        if (ret < 0)
                goto bail;
 
-       ret = kernel_setsockopt(sock, IPPROTO_TCP, TCP_KEEPIDLE,
-                               (char *)&keepidle, sizeof(keepidle));
-       if (ret < 0)
-               goto bail;
+       tcp_sock_set_keepidle(sock->sk, keepidle);
 
        /* KEEPINTVL is the interval between successive probes. We follow
         * the model in xs_tcp_finish_connecting() and re-use keepidle.
index 231fd6162f68da9989c5916ff0a41c607b030e93..473290f7c5c0aa29308b71eaf073bbc9e1c4b503 100644 (file)
@@ -2107,8 +2107,7 @@ static void xs_tcp_set_socket_timeouts(struct rpc_xprt *xprt,
 
        /* TCP Keepalive options */
        sock_set_keepalive(sock->sk);
-       kernel_setsockopt(sock, SOL_TCP, TCP_KEEPIDLE,
-                       (char *)&keepidle, sizeof(keepidle));
+       tcp_sock_set_keepidle(sock->sk, keepidle);
        kernel_setsockopt(sock, SOL_TCP, TCP_KEEPINTVL,
                        (char *)&keepidle, sizeof(keepidle));
        kernel_setsockopt(sock, SOL_TCP, TCP_KEEPCNT,