inet: Fix __inet_inherit_port() to correctly increment bsockets and num_owners
[linux-2.6.git] / net / ipv4 / tcp_timer.c
index 4e6ee52..74a6aa0 100644 (file)
@@ -19,6 +19,7 @@
  */
 
 #include <linux/module.h>
+#include <linux/gfp.h>
 #include <net/tcp.h>
 
 int sysctl_tcp_syn_retries __read_mostly = TCP_SYN_RETRIES;
@@ -29,6 +30,7 @@ int sysctl_tcp_keepalive_intvl __read_mostly = TCP_KEEPALIVE_INTVL;
 int sysctl_tcp_retries1 __read_mostly = TCP_RETR1;
 int sysctl_tcp_retries2 __read_mostly = TCP_RETR2;
 int sysctl_tcp_orphan_retries __read_mostly;
+int sysctl_tcp_thin_linear_timeouts __read_mostly;
 
 static void tcp_write_timer(unsigned long);
 static void tcp_delack_timer(unsigned long);
@@ -39,7 +41,6 @@ void tcp_init_xmit_timers(struct sock *sk)
        inet_csk_init_xmit_timers(sk, &tcp_write_timer, &tcp_delack_timer,
                                  &tcp_keepalive_timer);
 }
-
 EXPORT_SYMBOL(tcp_init_xmit_timers);
 
 static void tcp_write_err(struct sock *sk)
@@ -65,18 +66,18 @@ static void tcp_write_err(struct sock *sk)
 static int tcp_out_of_resources(struct sock *sk, int do_reset)
 {
        struct tcp_sock *tp = tcp_sk(sk);
-       int orphans = atomic_read(&tcp_orphan_count);
+       int shift = 0;
 
        /* If peer does not open window for long time, or did not transmit
         * anything for long time, penalize it. */
        if ((s32)(tcp_time_stamp - tp->lsndtime) > 2*TCP_RTO_MAX || !do_reset)
-               orphans <<= 1;
+               shift++;
 
        /* If some dubious ICMP arrived, penalize even more. */
        if (sk->sk_err_soft)
-               orphans <<= 1;
+               shift++;
 
-       if (tcp_too_many_orphans(sk, orphans)) {
+       if (tcp_too_many_orphans(sk, shift)) {
                if (net_ratelimit())
                        printk(KERN_INFO "Out of socket memory\n");
 
@@ -132,22 +133,57 @@ static void tcp_mtu_probing(struct inet_connection_sock *icsk, struct sock *sk)
        }
 }
 
+/* This function calculates a "timeout" which is equivalent to the timeout of a
+ * TCP connection after "boundary" unsuccessful, exponentially backed-off
+ * retransmissions with an initial RTO of TCP_RTO_MIN or TCP_TIMEOUT_INIT if
+ * syn_set flag is set.
+ */
+static bool retransmits_timed_out(struct sock *sk,
+                                 unsigned int boundary,
+                                 unsigned int timeout,
+                                 bool syn_set)
+{
+       unsigned int linear_backoff_thresh, start_ts;
+       unsigned int rto_base = syn_set ? TCP_TIMEOUT_INIT : TCP_RTO_MIN;
+
+       if (!inet_csk(sk)->icsk_retransmits)
+               return false;
+
+       if (unlikely(!tcp_sk(sk)->retrans_stamp))
+               start_ts = TCP_SKB_CB(tcp_write_queue_head(sk))->when;
+       else
+               start_ts = tcp_sk(sk)->retrans_stamp;
+
+       if (likely(timeout == 0)) {
+               linear_backoff_thresh = ilog2(TCP_RTO_MAX/rto_base);
+
+               if (boundary <= linear_backoff_thresh)
+                       timeout = ((2 << boundary) - 1) * rto_base;
+               else
+                       timeout = ((2 << linear_backoff_thresh) - 1) * rto_base +
+                               (boundary - linear_backoff_thresh) * TCP_RTO_MAX;
+       }
+       return (tcp_time_stamp - start_ts) >= timeout;
+}
+
 /* A write timeout has occurred. Process the after effects. */
 static int tcp_write_timeout(struct sock *sk)
 {
        struct inet_connection_sock *icsk = inet_csk(sk);
        int retry_until;
+       bool do_reset, syn_set = 0;
 
        if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) {
                if (icsk->icsk_retransmits)
-                       dst_negative_advice(&sk->sk_dst_cache);
+                       dst_negative_advice(sk);
                retry_until = icsk->icsk_syn_retries ? : sysctl_tcp_syn_retries;
+               syn_set = 1;
        } else {
-               if (icsk->icsk_retransmits >= sysctl_tcp_retries1) {
+               if (retransmits_timed_out(sk, sysctl_tcp_retries1, 0, 0)) {
                        /* Black hole detection */
                        tcp_mtu_probing(icsk, sk);
 
-                       dst_negative_advice(&sk->sk_dst_cache);
+                       dst_negative_advice(sk);
                }
 
                retry_until = sysctl_tcp_retries2;
@@ -155,13 +191,16 @@ static int tcp_write_timeout(struct sock *sk)
                        const int alive = (icsk->icsk_rto < TCP_RTO_MAX);
 
                        retry_until = tcp_orphan_retries(sk, alive);
+                       do_reset = alive ||
+                               !retransmits_timed_out(sk, retry_until, 0, 0);
 
-                       if (tcp_out_of_resources(sk, alive || icsk->icsk_retransmits < retry_until))
+                       if (tcp_out_of_resources(sk, do_reset))
                                return 1;
                }
        }
 
-       if (icsk->icsk_retransmits >= retry_until) {
+       if (retransmits_timed_out(sk, retry_until,
+                                 syn_set ? 0 : icsk->icsk_user_timeout, syn_set)) {
                /* Has it gone just too far? */
                tcp_write_err(sk);
                return 1;
@@ -171,7 +210,7 @@ static int tcp_write_timeout(struct sock *sk)
 
 static void tcp_delack_timer(unsigned long data)
 {
-       struct sock *sk = (struct sock*)data;
+       struct sock *sk = (struct sock *)data;
        struct tcp_sock *tp = tcp_sk(sk);
        struct inet_connection_sock *icsk = inet_csk(sk);
 
@@ -279,7 +318,7 @@ static void tcp_probe_timer(struct sock *sk)
  *     The TCP retransmit timer.
  */
 
-static void tcp_retransmit_timer(struct sock *sk)
+void tcp_retransmit_timer(struct sock *sk)
 {
        struct tcp_sock *tp = tcp_sk(sk);
        struct inet_connection_sock *icsk = inet_csk(sk);
@@ -299,16 +338,16 @@ static void tcp_retransmit_timer(struct sock *sk)
 #ifdef TCP_DEBUG
                struct inet_sock *inet = inet_sk(sk);
                if (sk->sk_family == AF_INET) {
-                       LIMIT_NETDEBUG(KERN_DEBUG "TCP: Treason uncloaked! Peer " NIPQUAD_FMT ":%u/%u shrinks window %u:%u. Repaired.\n",
-                              NIPQUAD(inet->daddr), ntohs(inet->dport),
-                              inet->num, tp->snd_una, tp->snd_nxt);
+                       LIMIT_NETDEBUG(KERN_DEBUG "TCP: Peer %pI4:%u/%u unexpectedly shrunk window %u:%u (repaired)\n",
+                              &inet->inet_daddr, ntohs(inet->inet_dport),
+                              inet->inet_num, tp->snd_una, tp->snd_nxt);
                }
 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
                else if (sk->sk_family == AF_INET6) {
                        struct ipv6_pinfo *np = inet6_sk(sk);
-                       LIMIT_NETDEBUG(KERN_DEBUG "TCP: Treason uncloaked! Peer %p6:%u/%u shrinks window %u:%u. Repaired.\n",
-                              &np->daddr, ntohs(inet->dport),
-                              inet->num, tp->snd_una, tp->snd_nxt);
+                       LIMIT_NETDEBUG(KERN_DEBUG "TCP: Peer %pI6:%u/%u unexpectedly shrunk window %u:%u (repaired)\n",
+                              &np->daddr, ntohs(inet->inet_dport),
+                              inet->inet_num, tp->snd_una, tp->snd_nxt);
                }
 #endif
 #endif
@@ -328,21 +367,19 @@ static void tcp_retransmit_timer(struct sock *sk)
        if (icsk->icsk_retransmits == 0) {
                int mib_idx;
 
-               if (icsk->icsk_ca_state == TCP_CA_Disorder ||
-                   icsk->icsk_ca_state == TCP_CA_Recovery) {
-                       if (tcp_is_sack(tp)) {
-                               if (icsk->icsk_ca_state == TCP_CA_Recovery)
-                                       mib_idx = LINUX_MIB_TCPSACKRECOVERYFAIL;
-                               else
-                                       mib_idx = LINUX_MIB_TCPSACKFAILURES;
-                       } else {
-                               if (icsk->icsk_ca_state == TCP_CA_Recovery)
-                                       mib_idx = LINUX_MIB_TCPRENORECOVERYFAIL;
-                               else
-                                       mib_idx = LINUX_MIB_TCPRENOFAILURES;
-                       }
+               if (icsk->icsk_ca_state == TCP_CA_Recovery) {
+                       if (tcp_is_sack(tp))
+                               mib_idx = LINUX_MIB_TCPSACKRECOVERYFAIL;
+                       else
+                               mib_idx = LINUX_MIB_TCPRENORECOVERYFAIL;
                } else if (icsk->icsk_ca_state == TCP_CA_Loss) {
                        mib_idx = LINUX_MIB_TCPLOSSFAILURES;
+               } else if ((icsk->icsk_ca_state == TCP_CA_Disorder) ||
+                          tp->sacked_out) {
+                       if (tcp_is_sack(tp))
+                               mib_idx = LINUX_MIB_TCPSACKFAILURES;
+                       else
+                               mib_idx = LINUX_MIB_TCPRENOFAILURES;
                } else {
                        mib_idx = LINUX_MIB_TCPTIMEOUTS;
                }
@@ -386,9 +423,27 @@ static void tcp_retransmit_timer(struct sock *sk)
        icsk->icsk_retransmits++;
 
 out_reset_timer:
-       icsk->icsk_rto = min(icsk->icsk_rto << 1, TCP_RTO_MAX);
+       /* If stream is thin, use linear timeouts. Since 'icsk_backoff' is
+        * used to reset timer, set to 0. Recalculate 'icsk_rto' as this
+        * might be increased if the stream oscillates between thin and thick,
+        * thus the old value might already be too high compared to the value
+        * set by 'tcp_set_rto' in tcp_input.c which resets the rto without
+        * backoff. Limit to TCP_THIN_LINEAR_RETRIES before initiating
+        * exponential backoff behaviour to avoid continue hammering
+        * linear-timeout retransmissions into a black hole
+        */
+       if (sk->sk_state == TCP_ESTABLISHED &&
+           (tp->thin_lto || sysctl_tcp_thin_linear_timeouts) &&
+           tcp_stream_is_thin(tp) &&
+           icsk->icsk_retransmits <= TCP_THIN_LINEAR_RETRIES) {
+               icsk->icsk_backoff = 0;
+               icsk->icsk_rto = min(__tcp_set_rto(tp), TCP_RTO_MAX);
+       } else {
+               /* Use normal (exponential) backoff */
+               icsk->icsk_rto = min(icsk->icsk_rto << 1, TCP_RTO_MAX);
+       }
        inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS, icsk->icsk_rto, TCP_RTO_MAX);
-       if (icsk->icsk_retransmits > sysctl_tcp_retries1)
+       if (retransmits_timed_out(sk, sysctl_tcp_retries1 + 1, 0, 0))
                __sk_dst_reset(sk);
 
 out:;
@@ -396,7 +451,7 @@ out:;
 
 static void tcp_write_timer(unsigned long data)
 {
-       struct sock *sk = (struct sock*)data;
+       struct sock *sk = (struct sock *)data;
        struct inet_connection_sock *icsk = inet_csk(sk);
        int event;
 
@@ -445,6 +500,12 @@ static void tcp_synack_timer(struct sock *sk)
                                   TCP_TIMEOUT_INIT, TCP_RTO_MAX);
 }
 
+void tcp_syn_ack_timeout(struct sock *sk, struct request_sock *req)
+{
+       NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPTIMEOUTS);
+}
+EXPORT_SYMBOL(tcp_syn_ack_timeout);
+
 void tcp_set_keepalive(struct sock *sk, int val)
 {
        if ((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_LISTEN))
@@ -462,7 +523,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);
@@ -499,11 +560,17 @@ 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 ((!tp->keepalive_probes && icsk->icsk_probes_out >= sysctl_tcp_keepalive_probes) ||
-                    (tp->keepalive_probes && icsk->icsk_probes_out >= tp->keepalive_probes)) {
+               /* If the TCP_USER_TIMEOUT option is enabled, use that
+                * to determine when to timeout instead.
+                */
+               if ((icsk->icsk_user_timeout != 0 &&
+                   elapsed >= icsk->icsk_user_timeout &&
+                   icsk->icsk_probes_out > 0) ||
+                   (icsk->icsk_user_timeout == 0 &&
+                   icsk->icsk_probes_out >= keepalive_probes(tp))) {
                        tcp_send_active_reset(sk, GFP_ATOMIC);
                        tcp_write_err(sk);
                        goto out;