TCP: avoid to send keepalive probes if receiving data
authorFlavio Leitner <fleitner@redhat.com>
Mon, 26 Apr 2010 18:33:27 +0000 (18:33 +0000)
committerDavid S. Miller <davem@davemloft.net>
Tue, 27 Apr 2010 19:53:25 +0000 (12:53 -0700)
RFC 1122 says the following:
...
  Keep-alive packets MUST only be sent when no data or
  acknowledgement packets have been received for the
  connection within an interval.
...

The acknowledgement packet is reseting the keepalive
timer but the data packet isn't. This patch fixes it by
checking the timestamp of the last received data packet
too when the keepalive timer expires.

Signed-off-by: Flavio Leitner <fleitner@redhat.com>
Signed-off-by: Eric Dumazet <eric.dumazet@gmail.com>
Acked-by: Ilpo Järvinen <ilpo.jarvinen@helsinki.fi>
Signed-off-by: David S. Miller <davem@davemloft.net>
include/net/tcp.h
net/ipv4/tcp.c
net/ipv4/tcp_timer.c

index 3f87fd87bc9cb306ce70012be65117506007b5db..fb5c66b2ab811a5e9bf909f46739db6e9d81e4a6 100644 (file)
@@ -1033,6 +1033,14 @@ static inline int keepalive_probes(const struct tcp_sock *tp)
        return tp->keepalive_probes ? : sysctl_tcp_keepalive_probes;
 }
 
+static inline u32 keepalive_time_elapsed(const struct tcp_sock *tp)
+{
+       const struct inet_connection_sock *icsk = &tp->inet_conn;
+
+       return min_t(u32, tcp_time_stamp - icsk->icsk_ack.lrcvtime,
+                         tcp_time_stamp - tp->rcv_tstamp);
+}
+
 static inline int tcp_fin_time(const struct sock *sk)
 {
        int fin_timeout = tcp_sk(sk)->linger2 ? : sysctl_tcp_fin_timeout;
index 6689c61cab47e0280078598589d6cb40459ca46e..8ce29747ad9b37d43308c54581e5e2f84817f5ce 100644 (file)
@@ -2298,7 +2298,7 @@ static int do_tcp_setsockopt(struct sock *sk, int level,
                        if (sock_flag(sk, SOCK_KEEPOPEN) &&
                            !((1 << sk->sk_state) &
                              (TCPF_CLOSE | TCPF_LISTEN))) {
-                               __u32 elapsed = tcp_time_stamp - tp->rcv_tstamp;
+                               u32 elapsed = keepalive_time_elapsed(tp);
                                if (tp->keepalive_time > elapsed)
                                        elapsed = tp->keepalive_time - elapsed;
                                else
index c732be00606b5178e33d998d1f906e64f2476ea6..440a5c6004f6ac8d1b2d8404d641d0a174b241b4 100644 (file)
@@ -517,7 +517,7 @@ static void tcp_keepalive_timer (unsigned long data)
        struct sock *sk = (struct sock *) data;
        struct inet_connection_sock *icsk = inet_csk(sk);
        struct tcp_sock *tp = tcp_sk(sk);
-       __u32 elapsed;
+       u32 elapsed;
 
        /* Only process if socket is not in use. */
        bh_lock_sock(sk);
@@ -554,7 +554,7 @@ static void tcp_keepalive_timer (unsigned long data)
        if (tp->packets_out || tcp_send_head(sk))
                goto resched;
 
-       elapsed = tcp_time_stamp - tp->rcv_tstamp;
+       elapsed = keepalive_time_elapsed(tp);
 
        if (elapsed >= keepalive_time_when(tp)) {
                if (icsk->icsk_probes_out >= keepalive_probes(tp)) {