Merge branch 'master' of master.kernel.org:/pub/scm/linux/kernel/git/davem/net-2.6
[safe/jmp/linux-2.6] / net / dccp / ccids / ccid3.c
index a93a556..a1929f3 100644 (file)
@@ -88,8 +88,8 @@ static void ccid3_hc_tx_set_state(struct sock *sk,
 static inline u64 rfc3390_initial_rate(struct sock *sk)
 {
        const struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
-       const __u32 w_init = min_t(__u32, 4 * hctx->ccid3hctx_s,
-                                  max_t(__u32, 2 * hctx->ccid3hctx_s, 4380));
+       const __u32 w_init = clamp_t(__u32, 4380U,
+                       2 * hctx->ccid3hctx_s, 4 * hctx->ccid3hctx_s);
 
        return scaled_div(w_init << 6, hctx->ccid3hctx_rtt);
 }
@@ -97,7 +97,7 @@ static inline u64 rfc3390_initial_rate(struct sock *sk)
 /*
  * Recalculate t_ipi and delta (should be called whenever X changes)
  */
-static inline void ccid3_update_send_interval(struct ccid3_hc_tx_sock *hctx)
+static void ccid3_update_send_interval(struct ccid3_hc_tx_sock *hctx)
 {
        /* Calculate new t_ipi = s / X_inst (X_inst is in 64 * bytes/second) */
        hctx->ccid3hctx_t_ipi = scaled_div32(((u64)hctx->ccid3hctx_s) << 6,
@@ -131,12 +131,11 @@ static u32 ccid3_hc_tx_idle_rtt(struct ccid3_hc_tx_sock *hctx, ktime_t now)
  *
  */
 static void ccid3_hc_tx_update_x(struct sock *sk, ktime_t *stamp)
-
 {
        struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
        __u64 min_rate = 2 * hctx->ccid3hctx_x_recv;
        const  __u64 old_x = hctx->ccid3hctx_x;
-       ktime_t now = stamp? *stamp : ktime_get_real();
+       ktime_t now = stamp ? *stamp : ktime_get_real();
 
        /*
         * Handle IDLE periods: do not reduce below RFC3390 initial sending rate
@@ -160,8 +159,8 @@ static void ccid3_hc_tx_update_x(struct sock *sk, ktime_t *stamp)
        } else if (ktime_us_delta(now, hctx->ccid3hctx_t_ld)
                                - (s64)hctx->ccid3hctx_rtt >= 0) {
 
-               hctx->ccid3hctx_x =
-                       max(min(2 * hctx->ccid3hctx_x, min_rate),
+               hctx->ccid3hctx_x = min(2 * hctx->ccid3hctx_x, min_rate);
+               hctx->ccid3hctx_x = max(hctx->ccid3hctx_x,
                            scaled_div(((__u64)hctx->ccid3hctx_s) << 6,
                                       hctx->ccid3hctx_rtt));
                hctx->ccid3hctx_t_ld = now;
@@ -194,22 +193,17 @@ static inline void ccid3_hc_tx_update_s(struct ccid3_hc_tx_sock *hctx, int len)
 
 /*
  *     Update Window Counter using the algorithm from [RFC 4342, 8.1].
- *     The algorithm is not applicable if RTT < 4 microseconds.
+ *     As elsewhere, RTT > 0 is assumed by using dccp_sample_rtt().
  */
 static inline void ccid3_hc_tx_update_win_count(struct ccid3_hc_tx_sock *hctx,
                                                ktime_t now)
 {
-       u32 quarter_rtts;
-
-       if (unlikely(hctx->ccid3hctx_rtt < 4))  /* avoid divide-by-zero */
-               return;
-
-       quarter_rtts = ktime_us_delta(now, hctx->ccid3hctx_t_last_win_count);
-       quarter_rtts /= hctx->ccid3hctx_rtt / 4;
+       u32 delta = ktime_us_delta(now, hctx->ccid3hctx_t_last_win_count),
+           quarter_rtts = (4 * delta) / hctx->ccid3hctx_rtt;
 
        if (quarter_rtts > 0) {
                hctx->ccid3hctx_t_last_win_count = now;
-               hctx->ccid3hctx_last_win_count  += min_t(u32, quarter_rtts, 5);
+               hctx->ccid3hctx_last_win_count  += min(quarter_rtts, 5U);
                hctx->ccid3hctx_last_win_count  &= 0xF;         /* mod 16 */
        }
 }
@@ -230,27 +224,27 @@ static void ccid3_hc_tx_no_feedback_timer(unsigned long data)
        ccid3_pr_debug("%s(%p, state=%s) - entry \n", dccp_role(sk), sk,
                       ccid3_tx_state_name(hctx->ccid3hctx_state));
 
-       switch (hctx->ccid3hctx_state) {
-       case TFRC_SSTATE_NO_FBACK:
-               /* RFC 3448, 4.4: Halve send rate directly */
+       if (hctx->ccid3hctx_state == TFRC_SSTATE_FBACK)
+               ccid3_hc_tx_set_state(sk, TFRC_SSTATE_NO_FBACK);
+       else if (hctx->ccid3hctx_state != TFRC_SSTATE_NO_FBACK)
+               goto out;
+
+       /*
+        * Determine new allowed sending rate X as per draft rfc3448bis-00, 4.4
+        */
+       if (hctx->ccid3hctx_t_rto == 0 ||       /* no feedback received yet */
+           hctx->ccid3hctx_p == 0) {
+
+               /* halve send rate directly */
                hctx->ccid3hctx_x = max(hctx->ccid3hctx_x / 2,
                                        (((__u64)hctx->ccid3hctx_s) << 6) /
                                                                    TFRC_T_MBI);
-
-               ccid3_pr_debug("%s(%p, state=%s), updated tx rate to %u "
-                              "bytes/s\n", dccp_role(sk), sk,
-                              ccid3_tx_state_name(hctx->ccid3hctx_state),
-                              (unsigned)(hctx->ccid3hctx_x >> 6));
-               /* The value of R is still undefined and so we can not recompute
-                * the timeout value. Keep initial value as per [RFC 4342, 5]. */
-               t_nfb = TFRC_INITIAL_TIMEOUT;
                ccid3_update_send_interval(hctx);
-               break;
-       case TFRC_SSTATE_FBACK:
+       } else {
                /*
-                *  Modify the cached value of X_recv [RFC 3448, 4.4]
+                *  Modify the cached value of X_recv
                 *
-                *  If (p == 0 || X_calc > 2 * X_recv)
+                *  If (X_calc > 2 * X_recv)
                 *    X_recv = max(X_recv / 2, s / (2 * t_mbi));
                 *  Else
                 *    X_recv = X_calc / 4;
@@ -259,32 +253,28 @@ static void ccid3_hc_tx_no_feedback_timer(unsigned long data)
                 */
                BUG_ON(hctx->ccid3hctx_p && !hctx->ccid3hctx_x_calc);
 
-               if (hctx->ccid3hctx_p == 0 ||
-                   (hctx->ccid3hctx_x_calc > (hctx->ccid3hctx_x_recv >> 5))) {
-
+               if (hctx->ccid3hctx_x_calc > (hctx->ccid3hctx_x_recv >> 5))
                        hctx->ccid3hctx_x_recv =
                                max(hctx->ccid3hctx_x_recv / 2,
                                    (((__u64)hctx->ccid3hctx_s) << 6) /
                                                              (2 * TFRC_T_MBI));
-               else {
+               else {
                        hctx->ccid3hctx_x_recv = hctx->ccid3hctx_x_calc;
                        hctx->ccid3hctx_x_recv <<= 4;
                }
-               /* Now recalculate X [RFC 3448, 4.3, step (4)] */
                ccid3_hc_tx_update_x(sk, NULL);
-               /*
-                * Schedule no feedback timer to expire in
-                * max(t_RTO, 2 * s/X)  =  max(t_RTO, 2 * t_ipi)
-                * See comments in packet_recv() regarding the value of t_RTO.
-                */
-               t_nfb = max(hctx->ccid3hctx_t_rto, 2 * hctx->ccid3hctx_t_ipi);
-               break;
-       case TFRC_SSTATE_NO_SENT:
-               DCCP_BUG("%s(%p) - Illegal state NO_SENT", dccp_role(sk), sk);
-               /* fall through */
-       case TFRC_SSTATE_TERM:
-               goto out;
        }
+       ccid3_pr_debug("Reduced X to %llu/64 bytes/sec\n",
+                       (unsigned long long)hctx->ccid3hctx_x);
+
+       /*
+        * Set new timeout for the nofeedback timer.
+        * See comments in packet_recv() regarding the value of t_RTO.
+        */
+       if (unlikely(hctx->ccid3hctx_t_rto == 0))       /* no feedback yet */
+               t_nfb = TFRC_INITIAL_TIMEOUT;
+       else
+               t_nfb = max(hctx->ccid3hctx_t_rto, 2 * hctx->ccid3hctx_t_ipi);
 
 restart_timer:
        sk_reset_timer(sk, &hctx->ccid3hctx_no_feedback_timer,
@@ -339,8 +329,14 @@ static int ccid3_hc_tx_send_packet(struct sock *sk, struct sk_buff *skb)
                        hctx->ccid3hctx_x    = rfc3390_initial_rate(sk);
                        hctx->ccid3hctx_t_ld = now;
                } else {
-                       /* Sender does not have RTT sample: X_pps = 1 pkt/sec */
-                       hctx->ccid3hctx_x = hctx->ccid3hctx_s;
+                       /*
+                        * Sender does not have RTT sample:
+                        * - set fallback RTT (RFC 4340, 3.4) since a RTT value
+                        *   is needed in several parts (e.g.  window counter);
+                        * - set sending rate X_pps = 1pps as per RFC 3448, 4.2.
+                        */
+                       hctx->ccid3hctx_rtt = DCCP_FALLBACK_RTT;
+                       hctx->ccid3hctx_x   = hctx->ccid3hctx_s;
                        hctx->ccid3hctx_x <<= 6;
                }
                ccid3_update_send_interval(hctx);
@@ -429,40 +425,46 @@ static void ccid3_hc_tx_packet_recv(struct sock *sk, struct sk_buff *skb)
        if (pinv == ~0U || pinv == 0)          /* see RFC 4342, 8.5   */
                hctx->ccid3hctx_p = 0;
        else                                   /* can not exceed 100% */
-               hctx->ccid3hctx_p = 1000000 / pinv;
+               hctx->ccid3hctx_p = scaled_div(1, pinv);
        /*
         * Validate new RTT sample and update moving average
         */
        r_sample = dccp_sample_rtt(sk, r_sample);
        hctx->ccid3hctx_rtt = tfrc_ewma(hctx->ccid3hctx_rtt, r_sample, 9);
-
+       /*
+        * Update allowed sending rate X as per draft rfc3448bis-00, 4.2/3
+        */
        if (hctx->ccid3hctx_state == TFRC_SSTATE_NO_FBACK) {
-               /*
-                * Larger Initial Windows [RFC 4342, sec. 5]
-                */
-               hctx->ccid3hctx_x    = rfc3390_initial_rate(sk);
-               hctx->ccid3hctx_t_ld = now;
+               ccid3_hc_tx_set_state(sk, TFRC_SSTATE_FBACK);
 
-               ccid3_update_send_interval(hctx);
+               if (hctx->ccid3hctx_t_rto == 0) {
+                       /*
+                        * Initial feedback packet: Larger Initial Windows (4.2)
+                        */
+                       hctx->ccid3hctx_x    = rfc3390_initial_rate(sk);
+                       hctx->ccid3hctx_t_ld = now;
 
-               ccid3_pr_debug("%s(%p), s=%u, MSS=%u, "
-                              "R_sample=%uus, X=%u\n", dccp_role(sk),
-                              sk, hctx->ccid3hctx_s,
-                              dccp_sk(sk)->dccps_mss_cache, r_sample,
-                              (unsigned)(hctx->ccid3hctx_x >> 6));
+                       ccid3_update_send_interval(hctx);
 
-               ccid3_hc_tx_set_state(sk, TFRC_SSTATE_FBACK);
-       } else {
+                       goto done_computing_x;
+               } else if (hctx->ccid3hctx_p == 0) {
+                       /*
+                        * First feedback after nofeedback timer expiry (4.3)
+                        */
+                       goto done_computing_x;
+               }
+       }
 
-               /* Update sending rate (step 4 of [RFC 3448, 4.3]) */
-               if (hctx->ccid3hctx_p > 0)
-                       hctx->ccid3hctx_x_calc =
+       /* Update sending rate (step 4 of [RFC 3448, 4.3]) */
+       if (hctx->ccid3hctx_p > 0)
+               hctx->ccid3hctx_x_calc =
                                tfrc_calc_x(hctx->ccid3hctx_s,
                                            hctx->ccid3hctx_rtt,
                                            hctx->ccid3hctx_p);
-               ccid3_hc_tx_update_x(sk, &now);
+       ccid3_hc_tx_update_x(sk, &now);
 
-               ccid3_pr_debug("%s(%p), RTT=%uus (sample=%uus), s=%u, "
+done_computing_x:
+       ccid3_pr_debug("%s(%p), RTT=%uus (sample=%uus), s=%u, "
                               "p=%u, X_calc=%u, X_recv=%u, X=%u\n",
                               dccp_role(sk),
                               sk, hctx->ccid3hctx_rtt, r_sample,
@@ -470,7 +472,6 @@ static void ccid3_hc_tx_packet_recv(struct sock *sk, struct sk_buff *skb)
                               hctx->ccid3hctx_x_calc,
                               (unsigned)(hctx->ccid3hctx_x_recv >> 6),
                               (unsigned)(hctx->ccid3hctx_x >> 6));
-       }
 
        /* unschedule no feedback timer */
        sk_stop_timer(sk, &hctx->ccid3hctx_no_feedback_timer);