This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / net / ipv4 / tcp_veno.c
1 /*
2  * TCP Veno congestion control
3  *
4  * This is based on the congestion detection/avoidance scheme described in
5  *    C. P. Fu, S. C. Liew.
6  *    "TCP Veno: TCP Enhancement for Transmission over Wireless Access Networks."
7  *    IEEE Journal on Selected Areas in Communication,
8  *    Feb. 2003.
9  *      See http://www.ntu.edu.sg/home5/ZHOU0022/papers/CPFu03a.pdf
10  */
11
12 #include <linux/mm.h>
13 #include <linux/module.h>
14 #include <linux/skbuff.h>
15 #include <linux/inet_diag.h>
16
17 #include <net/tcp.h>
18
19 /* Default values of the Veno variables, in fixed-point representation
20  * with V_PARAM_SHIFT bits to the right of the binary point.
21  */
22 #define V_PARAM_SHIFT 1
23 static const int beta = 3 << V_PARAM_SHIFT;
24
25 /* Veno variables */
26 struct veno {
27         u8 doing_veno_now;      /* if true, do veno for this rtt */
28         u16 cntrtt;             /* # of rtts measured within last rtt */
29         u32 minrtt;             /* min of rtts measured within last rtt (in usec) */
30         u32 basertt;            /* the min of all Veno rtt measurements seen (in usec) */
31         u32 inc;                /* decide whether to increase cwnd */
32         u32 diff;               /* calculate the diff rate */
33 };
34
35 /* There are several situations when we must "re-start" Veno:
36  *
37  *  o when a connection is established
38  *  o after an RTO
39  *  o after fast recovery
40  *  o when we send a packet and there is no outstanding
41  *    unacknowledged data (restarting an idle connection)
42  *
43  */
44 static inline void veno_enable(struct sock *sk)
45 {
46         struct veno *veno = inet_csk_ca(sk);
47
48         /* turn on Veno */
49         veno->doing_veno_now = 1;
50
51         veno->minrtt = 0x7fffffff;
52 }
53
54 static inline void veno_disable(struct sock *sk)
55 {
56         struct veno *veno = inet_csk_ca(sk);
57
58         /* turn off Veno */
59         veno->doing_veno_now = 0;
60 }
61
62 static void tcp_veno_init(struct sock *sk)
63 {
64         struct veno *veno = inet_csk_ca(sk);
65
66         veno->basertt = 0x7fffffff;
67         veno->inc = 1;
68         veno_enable(sk);
69 }
70
71 /* Do rtt sampling needed for Veno. */
72 static void tcp_veno_rtt_calc(struct sock *sk, u32 usrtt)
73 {
74         struct veno *veno = inet_csk_ca(sk);
75         u32 vrtt = usrtt + 1;   /* Never allow zero rtt or basertt */
76
77         /* Filter to find propagation delay: */
78         if (vrtt < veno->basertt)
79                 veno->basertt = vrtt;
80
81         /* Find the min rtt during the last rtt to find
82          * the current prop. delay + queuing delay:
83          */
84         veno->minrtt = min(veno->minrtt, vrtt);
85         veno->cntrtt++;
86 }
87
88 static void tcp_veno_state(struct sock *sk, u8 ca_state)
89 {
90         if (ca_state == TCP_CA_Open)
91                 veno_enable(sk);
92         else
93                 veno_disable(sk);
94 }
95
96 /*
97  * If the connection is idle and we are restarting,
98  * then we don't want to do any Veno calculations
99  * until we get fresh rtt samples.  So when we
100  * restart, we reset our Veno state to a clean
101  * state. After we get acks for this flight of
102  * packets, _then_ we can make Veno calculations
103  * again.
104  */
105 static void tcp_veno_cwnd_event(struct sock *sk, enum tcp_ca_event event)
106 {
107         if (event == CA_EVENT_CWND_RESTART || event == CA_EVENT_TX_START)
108                 tcp_veno_init(sk);
109 }
110
111 static void tcp_veno_cong_avoid(struct sock *sk, u32 ack,
112                                 u32 seq_rtt, u32 in_flight, int flag)
113 {
114         struct tcp_sock *tp = tcp_sk(sk);
115         struct veno *veno = inet_csk_ca(sk);
116
117         if (!veno->doing_veno_now)
118                 return tcp_reno_cong_avoid(sk, ack, seq_rtt, in_flight, flag);
119
120         /* limited by applications */
121         if (!tcp_is_cwnd_limited(sk, in_flight))
122                 return;
123
124         /* We do the Veno calculations only if we got enough rtt samples */
125         if (veno->cntrtt <= 2) {
126                 /* We don't have enough rtt samples to do the Veno
127                  * calculation, so we'll behave like Reno.
128                  */
129                 tcp_reno_cong_avoid(sk, ack, seq_rtt, in_flight, flag);
130         } else {
131                 u32 rtt, target_cwnd;
132
133                 /* We have enough rtt samples, so, using the Veno
134                  * algorithm, we determine the state of the network.
135                  */
136
137                 rtt = veno->minrtt;
138
139                 target_cwnd = ((tp->snd_cwnd * veno->basertt)
140                                << V_PARAM_SHIFT) / rtt;
141
142                 veno->diff = (tp->snd_cwnd << V_PARAM_SHIFT) - target_cwnd;
143
144                 if (tp->snd_cwnd <= tp->snd_ssthresh) {
145                         /* Slow start.  */
146                         tcp_slow_start(tp);
147                 } else {
148                         /* Congestion avoidance. */
149                         if (veno->diff < beta) {
150                                 /* In the "non-congestive state", increase cwnd
151                                  *  every rtt.
152                                  */
153                                 if (tp->snd_cwnd_cnt >= tp->snd_cwnd) {
154                                         if (tp->snd_cwnd < tp->snd_cwnd_clamp)
155                                                 tp->snd_cwnd++;
156                                         tp->snd_cwnd_cnt = 0;
157                                 } else
158                                         tp->snd_cwnd_cnt++;
159                         } else {
160                                 /* In the "congestive state", increase cwnd
161                                  * every other rtt.
162                                  */
163                                 if (tp->snd_cwnd_cnt >= tp->snd_cwnd) {
164                                         if (veno->inc
165                                             && tp->snd_cwnd <
166                                             tp->snd_cwnd_clamp) {
167                                                 tp->snd_cwnd++;
168                                                 veno->inc = 0;
169                                         } else
170                                                 veno->inc = 1;
171                                         tp->snd_cwnd_cnt = 0;
172                                 } else
173                                         tp->snd_cwnd_cnt++;
174                         }
175
176                 }
177                 if (tp->snd_cwnd < 2)
178                         tp->snd_cwnd = 2;
179                 else if (tp->snd_cwnd > tp->snd_cwnd_clamp)
180                         tp->snd_cwnd = tp->snd_cwnd_clamp;
181         }
182         /* Wipe the slate clean for the next rtt. */
183         /* veno->cntrtt = 0; */
184         veno->minrtt = 0x7fffffff;
185 }
186
187 /* Veno MD phase */
188 static u32 tcp_veno_ssthresh(struct sock *sk)
189 {
190         const struct tcp_sock *tp = tcp_sk(sk);
191         struct veno *veno = inet_csk_ca(sk);
192
193         if (veno->diff < beta)
194                 /* in "non-congestive state", cut cwnd by 1/5 */
195                 return max(tp->snd_cwnd * 4 / 5, 2U);
196         else
197                 /* in "congestive state", cut cwnd by 1/2 */
198                 return max(tp->snd_cwnd >> 1U, 2U);
199 }
200
201 static struct tcp_congestion_ops tcp_veno = {
202         .init           = tcp_veno_init,
203         .ssthresh       = tcp_veno_ssthresh,
204         .cong_avoid     = tcp_veno_cong_avoid,
205         .rtt_sample     = tcp_veno_rtt_calc,
206         .set_state      = tcp_veno_state,
207         .cwnd_event     = tcp_veno_cwnd_event,
208
209         .owner          = THIS_MODULE,
210         .name           = "veno",
211 };
212
213 static int __init tcp_veno_register(void)
214 {
215         BUG_ON(sizeof(struct veno) > ICSK_CA_PRIV_SIZE);
216         tcp_register_congestion_control(&tcp_veno);
217         return 0;
218 }
219
220 static void __exit tcp_veno_unregister(void)
221 {
222         tcp_unregister_congestion_control(&tcp_veno);
223 }
224
225 module_init(tcp_veno_register);
226 module_exit(tcp_veno_unregister);
227
228 MODULE_AUTHOR("Bin Zhou, Cheng Peng Fu");
229 MODULE_LICENSE("GPL");
230 MODULE_DESCRIPTION("TCP Veno");