]> git.karo-electronics.de Git - mv-sheeva.git/blob - net/ipv4/tcp_westwood.c
[TCP] Westwood: fix first sample
[mv-sheeva.git] / net / ipv4 / tcp_westwood.c
1 /*
2  * TCP Westwood+
3  *
4  *      Angelo Dell'Aera:       TCP Westwood+ support
5  */
6
7 #include <linux/config.h>
8 #include <linux/mm.h>
9 #include <linux/module.h>
10 #include <linux/skbuff.h>
11 #include <linux/inet_diag.h>
12 #include <net/tcp.h>
13
14 /* TCP Westwood structure */
15 struct westwood {
16         u32    bw_ns_est;        /* first bandwidth estimation..not too smoothed 8) */
17         u32    bw_est;           /* bandwidth estimate */
18         u32    rtt_win_sx;       /* here starts a new evaluation... */
19         u32    bk;
20         u32    snd_una;          /* used for evaluating the number of acked bytes */
21         u32    cumul_ack;
22         u32    accounted;
23         u32    rtt;
24         u32    rtt_min;          /* minimum observed RTT */
25         u8     first_ack;        /* flag which infers that this is the first ack */
26 };
27
28
29 /* TCP Westwood functions and constants */
30 #define TCP_WESTWOOD_RTT_MIN   (HZ/20)  /* 50ms */
31 #define TCP_WESTWOOD_INIT_RTT  (20*HZ)  /* maybe too conservative?! */
32
33 /*
34  * @tcp_westwood_create
35  * This function initializes fields used in TCP Westwood+,
36  * it is called after the initial SYN, so the sequence numbers
37  * are correct but new passive connections we have no
38  * information about RTTmin at this time so we simply set it to
39  * TCP_WESTWOOD_INIT_RTT. This value was chosen to be too conservative
40  * since in this way we're sure it will be updated in a consistent
41  * way as soon as possible. It will reasonably happen within the first
42  * RTT period of the connection lifetime.
43  */
44 static void tcp_westwood_init(struct sock *sk)
45 {
46         struct westwood *w = inet_csk_ca(sk);
47
48         w->bk = 0;
49         w->bw_ns_est = 0;
50         w->bw_est = 0;
51         w->accounted = 0;
52         w->cumul_ack = 0;
53         w->rtt_min = w->rtt = TCP_WESTWOOD_INIT_RTT;
54         w->rtt_win_sx = tcp_time_stamp;
55         w->snd_una = tcp_sk(sk)->snd_una;
56         w->first_ack = 1;
57 }
58
59 /*
60  * @westwood_do_filter
61  * Low-pass filter. Implemented using constant coefficients.
62  */
63 static inline u32 westwood_do_filter(u32 a, u32 b)
64 {
65         return (((7 * a) + b) >> 3);
66 }
67
68 static inline void westwood_filter(struct westwood *w, u32 delta)
69 {
70         w->bw_ns_est = westwood_do_filter(w->bw_ns_est, w->bk / delta);
71         w->bw_est = westwood_do_filter(w->bw_est, w->bw_ns_est);
72 }
73
74 /*
75  * @westwood_pkts_acked
76  * Called after processing group of packets.
77  * but all westwood needs is the last sample of srtt.
78  */
79 static void tcp_westwood_pkts_acked(struct sock *sk, u32 cnt)
80 {
81         struct westwood *w = inet_csk_ca(sk);
82         if (cnt > 0)
83                 w->rtt = tcp_sk(sk)->srtt >> 3;
84 }
85
86 /*
87  * @westwood_update_window
88  * It updates RTT evaluation window if it is the right moment to do
89  * it. If so it calls filter for evaluating bandwidth.
90  */
91 static void westwood_update_window(struct sock *sk)
92 {
93         struct westwood *w = inet_csk_ca(sk);
94         s32 delta = tcp_time_stamp - w->rtt_win_sx;
95
96         /* Initialise w->snd_una with the first acked sequence number in order
97          * to fix mismatch between tp->snd_una and w->snd_una for the first
98          * bandwidth sample
99          */
100         if (w->first_ack) {
101                 w->snd_una = tcp_sk(sk)->snd_una;
102                 w->first_ack = 0;
103         }
104
105         /*
106          * See if a RTT-window has passed.
107          * Be careful since if RTT is less than
108          * 50ms we don't filter but we continue 'building the sample'.
109          * This minimum limit was chosen since an estimation on small
110          * time intervals is better to avoid...
111          * Obviously on a LAN we reasonably will always have
112          * right_bound = left_bound + WESTWOOD_RTT_MIN
113          */
114         if (w->rtt && delta > max_t(u32, w->rtt, TCP_WESTWOOD_RTT_MIN)) {
115                 westwood_filter(w, delta);
116
117                 w->bk = 0;
118                 w->rtt_win_sx = tcp_time_stamp;
119         }
120 }
121
122 /*
123  * @westwood_fast_bw
124  * It is called when we are in fast path. In particular it is called when
125  * header prediction is successful. In such case in fact update is
126  * straight forward and doesn't need any particular care.
127  */
128 static inline void westwood_fast_bw(struct sock *sk)
129 {
130         const struct tcp_sock *tp = tcp_sk(sk);
131         struct westwood *w = inet_csk_ca(sk);
132
133         westwood_update_window(sk);
134
135         w->bk += tp->snd_una - w->snd_una;
136         w->snd_una = tp->snd_una;
137         w->rtt_min = min(w->rtt, w->rtt_min);
138 }
139
140 /*
141  * @westwood_acked_count
142  * This function evaluates cumul_ack for evaluating bk in case of
143  * delayed or partial acks.
144  */
145 static inline u32 westwood_acked_count(struct sock *sk)
146 {
147         const struct tcp_sock *tp = tcp_sk(sk);
148         struct westwood *w = inet_csk_ca(sk);
149
150         w->cumul_ack = tp->snd_una - w->snd_una;
151
152         /* If cumul_ack is 0 this is a dupack since it's not moving
153          * tp->snd_una.
154          */
155         if (!w->cumul_ack) {
156                 w->accounted += tp->mss_cache;
157                 w->cumul_ack = tp->mss_cache;
158         }
159
160         if (w->cumul_ack > tp->mss_cache) {
161                 /* Partial or delayed ack */
162                 if (w->accounted >= w->cumul_ack) {
163                         w->accounted -= w->cumul_ack;
164                         w->cumul_ack = tp->mss_cache;
165                 } else {
166                         w->cumul_ack -= w->accounted;
167                         w->accounted = 0;
168                 }
169         }
170
171         w->snd_una = tp->snd_una;
172
173         return w->cumul_ack;
174 }
175
176
177 /*
178  * TCP Westwood
179  * Here limit is evaluated as Bw estimation*RTTmin (for obtaining it
180  * in packets we use mss_cache). Rttmin is guaranteed to be >= 2
181  * so avoids ever returning 0.
182  */
183 static u32 tcp_westwood_bw_rttmin(const struct sock *sk)
184 {
185         const struct tcp_sock *tp = tcp_sk(sk);
186         const struct westwood *w = inet_csk_ca(sk);
187         return max_t(u32, (w->bw_est * w->rtt_min) / tp->mss_cache, 2);
188 }
189
190 static void tcp_westwood_event(struct sock *sk, enum tcp_ca_event event)
191 {
192         struct tcp_sock *tp = tcp_sk(sk);
193         struct westwood *w = inet_csk_ca(sk);
194         
195         switch(event) {
196         case CA_EVENT_FAST_ACK:
197                 westwood_fast_bw(sk);
198                 break;
199
200         case CA_EVENT_COMPLETE_CWR:
201                 tp->snd_cwnd = tp->snd_ssthresh = tcp_westwood_bw_rttmin(sk);
202                 break;
203
204         case CA_EVENT_FRTO:
205                 tp->snd_ssthresh = tcp_westwood_bw_rttmin(sk);
206                 break;
207
208         case CA_EVENT_SLOW_ACK:
209                 westwood_update_window(sk);
210                 w->bk += westwood_acked_count(sk);
211                 w->rtt_min = min(w->rtt, w->rtt_min);
212                 break;
213
214         default:
215                 /* don't care */
216                 break;
217         }
218 }
219
220
221 /* Extract info for Tcp socket info provided via netlink. */
222 static void tcp_westwood_info(struct sock *sk, u32 ext,
223                               struct sk_buff *skb)
224 {
225         const struct westwood *ca = inet_csk_ca(sk);
226         if (ext & (1 << (INET_DIAG_VEGASINFO - 1))) {
227                 struct rtattr *rta;
228                 struct tcpvegas_info *info;
229
230                 rta = __RTA_PUT(skb, INET_DIAG_VEGASINFO, sizeof(*info));
231                 info = RTA_DATA(rta);
232                 info->tcpv_enabled = 1;
233                 info->tcpv_rttcnt = 0;
234                 info->tcpv_rtt = jiffies_to_usecs(ca->rtt);
235                 info->tcpv_minrtt = jiffies_to_usecs(ca->rtt_min);
236         rtattr_failure: ;
237         }
238 }
239
240
241 static struct tcp_congestion_ops tcp_westwood = {
242         .init           = tcp_westwood_init,
243         .ssthresh       = tcp_reno_ssthresh,
244         .cong_avoid     = tcp_reno_cong_avoid,
245         .min_cwnd       = tcp_westwood_bw_rttmin,
246         .cwnd_event     = tcp_westwood_event,
247         .get_info       = tcp_westwood_info,
248         .pkts_acked     = tcp_westwood_pkts_acked,
249
250         .owner          = THIS_MODULE,
251         .name           = "westwood"
252 };
253
254 static int __init tcp_westwood_register(void)
255 {
256         BUG_ON(sizeof(struct westwood) > ICSK_CA_PRIV_SIZE);
257         return tcp_register_congestion_control(&tcp_westwood);
258 }
259
260 static void __exit tcp_westwood_unregister(void)
261 {
262         tcp_unregister_congestion_control(&tcp_westwood);
263 }
264
265 module_init(tcp_westwood_register);
266 module_exit(tcp_westwood_unregister);
267
268 MODULE_AUTHOR("Stephen Hemminger, Angelo Dell'Aera");
269 MODULE_LICENSE("GPL");
270 MODULE_DESCRIPTION("TCP Westwood+");