tcp: drop tcp_bound_rto, merge content of it tcp_set_rto
[safe/jmp/linux-2.6] / net / ipv4 / tcp_westwood.c
index ef82724..20151d6 100644 (file)
@@ -1,14 +1,30 @@
 /*
- * TCP Westwood+
+ * TCP Westwood+: end-to-end bandwidth estimation for TCP
  *
- *     Angelo Dell'Aera:       TCP Westwood+ support
+ *      Angelo Dell'Aera: author of the first version of TCP Westwood+ in Linux 2.4
+ *
+ * Support at http://c3lab.poliba.it/index.php/Westwood
+ * Main references in literature:
+ *
+ * - Mascolo S, Casetti, M. Gerla et al.
+ *   "TCP Westwood: bandwidth estimation for TCP" Proc. ACM Mobicom 2001
+ *
+ * - A. Grieco, s. Mascolo
+ *   "Performance evaluation of New Reno, Vegas, Westwood+ TCP" ACM Computer
+ *     Comm. Review, 2004
+ *
+ * - A. Dell'Aera, L. Grieco, S. Mascolo.
+ *   "Linux 2.4 Implementation of Westwood+ TCP with Rate-Halving :
+ *    A Performance Evaluation Over the Internet" (ICC 2004), Paris, June 2004
+ *
+ * Westwood+ employs end-to-end bandwidth measurement to set cwnd and
+ * ssthresh after packet loss. The probing phase is as the original Reno.
  */
 
-#include <linux/config.h>
 #include <linux/mm.h>
 #include <linux/module.h>
 #include <linux/skbuff.h>
-#include <linux/tcp_diag.h>
+#include <linux/inet_diag.h>
 #include <net/tcp.h>
 
 /* TCP Westwood structure */
@@ -22,6 +38,8 @@ struct westwood {
        u32    accounted;
        u32    rtt;
        u32    rtt_min;          /* minimum observed RTT */
+       u8     first_ack;        /* flag which infers that this is the first ack */
+       u8     reset_rtt_min;    /* Reset RTT min to next RTT sample*/
 };
 
 
@@ -40,18 +58,20 @@ struct westwood {
  * way as soon as possible. It will reasonably happen within the first
  * RTT period of the connection lifetime.
  */
-static void tcp_westwood_init(struct tcp_sock *tp)
+static void tcp_westwood_init(struct sock *sk)
 {
-       struct westwood *w = tcp_ca(tp);
+       struct westwood *w = inet_csk_ca(sk);
 
        w->bk = 0;
-        w->bw_ns_est = 0;
-        w->bw_est = 0;
-        w->accounted = 0;
-        w->cumul_ack = 0;
+       w->bw_ns_est = 0;
+       w->bw_est = 0;
+       w->accounted = 0;
+       w->cumul_ack = 0;
+       w->reset_rtt_min = 1;
        w->rtt_min = w->rtt = TCP_WESTWOOD_INIT_RTT;
        w->rtt_win_sx = tcp_time_stamp;
-       w->snd_una = tp->snd_una;
+       w->snd_una = tcp_sk(sk)->snd_una;
+       w->first_ack = 1;
 }
 
 /*
@@ -63,10 +83,16 @@ static inline u32 westwood_do_filter(u32 a, u32 b)
        return (((7 * a) + b) >> 3);
 }
 
-static inline void westwood_filter(struct westwood *w, u32 delta)
+static void westwood_filter(struct westwood *w, u32 delta)
 {
-       w->bw_ns_est = westwood_do_filter(w->bw_ns_est, w->bk / delta);
-       w->bw_est = westwood_do_filter(w->bw_est, w->bw_ns_est);
+       /* If the filter is empty fill it with the first sample of bandwidth  */
+       if (w->bw_ns_est == 0 && w->bw_est == 0) {
+               w->bw_ns_est = w->bk / delta;
+               w->bw_est = w->bw_ns_est;
+       } else {
+               w->bw_ns_est = westwood_do_filter(w->bw_ns_est, w->bk / delta);
+               w->bw_est = westwood_do_filter(w->bw_est, w->bw_ns_est);
+       }
 }
 
 /*
@@ -74,11 +100,12 @@ static inline void westwood_filter(struct westwood *w, u32 delta)
  * Called after processing group of packets.
  * but all westwood needs is the last sample of srtt.
  */
-static void tcp_westwood_pkts_acked(struct tcp_sock *tp, u32 cnt)
+static void tcp_westwood_pkts_acked(struct sock *sk, u32 cnt, s32 rtt)
 {
-       struct westwood *w = tcp_ca(tp);
-       if (cnt > 0)
-               w->rtt = tp->srtt >> 3;
+       struct westwood *w = inet_csk_ca(sk);
+
+       if (rtt > 0)
+               w->rtt = usecs_to_jiffies(rtt);
 }
 
 /*
@@ -86,11 +113,20 @@ static void tcp_westwood_pkts_acked(struct tcp_sock *tp, u32 cnt)
  * It updates RTT evaluation window if it is the right moment to do
  * it. If so it calls filter for evaluating bandwidth.
  */
-static void westwood_update_window(struct tcp_sock *tp)
+static void westwood_update_window(struct sock *sk)
 {
-       struct westwood *w = tcp_ca(tp);
+       struct westwood *w = inet_csk_ca(sk);
        s32 delta = tcp_time_stamp - w->rtt_win_sx;
 
+       /* Initialize w->snd_una with the first acked sequence number in order
+        * to fix mismatch between tp->snd_una and w->snd_una for the first
+        * bandwidth sample
+        */
+       if (w->first_ack) {
+               w->snd_una = tcp_sk(sk)->snd_una;
+               w->first_ack = 0;
+       }
+
        /*
         * See if a RTT-window has passed.
         * Be careful since if RTT is less than
@@ -108,21 +144,32 @@ static void westwood_update_window(struct tcp_sock *tp)
        }
 }
 
+static inline void update_rtt_min(struct westwood *w)
+{
+       if (w->reset_rtt_min) {
+               w->rtt_min = w->rtt;
+               w->reset_rtt_min = 0;
+       } else
+               w->rtt_min = min(w->rtt, w->rtt_min);
+}
+
+
 /*
  * @westwood_fast_bw
  * It is called when we are in fast path. In particular it is called when
  * header prediction is successful. In such case in fact update is
  * straight forward and doesn't need any particular care.
  */
-static inline void westwood_fast_bw(struct tcp_sock *tp)
+static inline void westwood_fast_bw(struct sock *sk)
 {
-       struct westwood *w = tcp_ca(tp);
+       const struct tcp_sock *tp = tcp_sk(sk);
+       struct westwood *w = inet_csk_ca(sk);
 
-       westwood_update_window(tp);
+       westwood_update_window(sk);
 
        w->bk += tp->snd_una - w->snd_una;
        w->snd_una = tp->snd_una;
-       w->rtt_min = min(w->rtt, w->rtt_min);
+       update_rtt_min(w);
 }
 
 /*
@@ -130,21 +177,22 @@ static inline void westwood_fast_bw(struct tcp_sock *tp)
  * This function evaluates cumul_ack for evaluating bk in case of
  * delayed or partial acks.
  */
-static inline u32 westwood_acked_count(struct tcp_sock *tp)
+static inline u32 westwood_acked_count(struct sock *sk)
 {
-       struct westwood *w = tcp_ca(tp);
+       const struct tcp_sock *tp = tcp_sk(sk);
+       struct westwood *w = inet_csk_ca(sk);
 
        w->cumul_ack = tp->snd_una - w->snd_una;
 
-        /* If cumul_ack is 0 this is a dupack since it's not moving
-         * tp->snd_una.
-         */
-        if (!w->cumul_ack) {
+       /* If cumul_ack is 0 this is a dupack since it's not moving
+        * tp->snd_una.
+        */
+       if (!w->cumul_ack) {
                w->accounted += tp->mss_cache;
                w->cumul_ack = tp->mss_cache;
        }
 
-        if (w->cumul_ack > tp->mss_cache) {
+       if (w->cumul_ack > tp->mss_cache) {
                /* Partial or delayed ack */
                if (w->accounted >= w->cumul_ack) {
                        w->accounted -= w->cumul_ack;
@@ -160,11 +208,6 @@ static inline u32 westwood_acked_count(struct tcp_sock *tp)
        return w->cumul_ack;
 }
 
-static inline u32 westwood_bw_rttmin(const struct tcp_sock *tp)
-{
-       struct westwood *w = tcp_ca(tp);
-       return max_t(u32, (w->bw_est * w->rtt_min) / tp->mss_cache, 2);
-}
 
 /*
  * TCP Westwood
@@ -172,32 +215,37 @@ static inline u32 westwood_bw_rttmin(const struct tcp_sock *tp)
  * in packets we use mss_cache). Rttmin is guaranteed to be >= 2
  * so avoids ever returning 0.
  */
-static u32 tcp_westwood_cwnd_min(struct tcp_sock *tp)
+static u32 tcp_westwood_bw_rttmin(const struct sock *sk)
 {
-       return westwood_bw_rttmin(tp);
+       const struct tcp_sock *tp = tcp_sk(sk);
+       const struct westwood *w = inet_csk_ca(sk);
+       return max_t(u32, (w->bw_est * w->rtt_min) / tp->mss_cache, 2);
 }
 
-static void tcp_westwood_event(struct tcp_sock *tp, enum tcp_ca_event event)
+static void tcp_westwood_event(struct sock *sk, enum tcp_ca_event event)
 {
-       struct westwood *w = tcp_ca(tp);
+       struct tcp_sock *tp = tcp_sk(sk);
+       struct westwood *w = inet_csk_ca(sk);
 
-       switch(event) {
+       switch (event) {
        case CA_EVENT_FAST_ACK:
-               westwood_fast_bw(tp);
+               westwood_fast_bw(sk);
                break;
 
        case CA_EVENT_COMPLETE_CWR:
-               tp->snd_cwnd = tp->snd_ssthresh = westwood_bw_rttmin(tp);
+               tp->snd_cwnd = tp->snd_ssthresh = tcp_westwood_bw_rttmin(sk);
                break;
 
        case CA_EVENT_FRTO:
-               tp->snd_ssthresh = westwood_bw_rttmin(tp);
+               tp->snd_ssthresh = tcp_westwood_bw_rttmin(sk);
+               /* Update RTT_min when next ack arrives */
+               w->reset_rtt_min = 1;
                break;
 
        case CA_EVENT_SLOW_ACK:
-               westwood_update_window(tp);
-               w->bk += westwood_acked_count(tp);
-               w->rtt_min = min(w->rtt, w->rtt_min);
+               westwood_update_window(sk);
+               w->bk += westwood_acked_count(sk);
+               update_rtt_min(w);
                break;
 
        default:
@@ -208,21 +256,18 @@ static void tcp_westwood_event(struct tcp_sock *tp, enum tcp_ca_event event)
 
 
 /* Extract info for Tcp socket info provided via netlink. */
-static void tcp_westwood_info(struct tcp_sock *tp, u32 ext,
+static void tcp_westwood_info(struct sock *sk, u32 ext,
                              struct sk_buff *skb)
 {
-       const struct westwood *ca = tcp_ca(tp);
-       if (ext & (1<<(TCPDIAG_VEGASINFO-1))) {
-               struct rtattr *rta;
-               struct tcpvegas_info *info;
-
-               rta = __RTA_PUT(skb, TCPDIAG_VEGASINFO, sizeof(*info));
-               info = RTA_DATA(rta);
-               info->tcpv_enabled = 1;
-               info->tcpv_rttcnt = 0;
-               info->tcpv_rtt = jiffies_to_usecs(ca->rtt);
-               info->tcpv_minrtt = jiffies_to_usecs(ca->rtt_min);
-       rtattr_failure: ;
+       const struct westwood *ca = inet_csk_ca(sk);
+       if (ext & (1 << (INET_DIAG_VEGASINFO - 1))) {
+               struct tcpvegas_info info = {
+                       .tcpv_enabled = 1,
+                       .tcpv_rtt = jiffies_to_usecs(ca->rtt),
+                       .tcpv_minrtt = jiffies_to_usecs(ca->rtt_min),
+               };
+
+               nla_put(skb, INET_DIAG_VEGASINFO, sizeof(info), &info);
        }
 }
 
@@ -231,7 +276,7 @@ static struct tcp_congestion_ops tcp_westwood = {
        .init           = tcp_westwood_init,
        .ssthresh       = tcp_reno_ssthresh,
        .cong_avoid     = tcp_reno_cong_avoid,
-       .min_cwnd       = tcp_westwood_cwnd_min,
+       .min_cwnd       = tcp_westwood_bw_rttmin,
        .cwnd_event     = tcp_westwood_event,
        .get_info       = tcp_westwood_info,
        .pkts_acked     = tcp_westwood_pkts_acked,
@@ -242,7 +287,7 @@ static struct tcp_congestion_ops tcp_westwood = {
 
 static int __init tcp_westwood_register(void)
 {
-       BUG_ON(sizeof(struct westwood) > TCP_CA_PRIV_SIZE);
+       BUILD_BUG_ON(sizeof(struct westwood) > ICSK_CA_PRIV_SIZE);
        return tcp_register_congestion_control(&tcp_westwood);
 }