blob: edf2ee19a8ba76c6759d7758cd91b132a97ef958 [file] [log] [blame]
Stephen Hemminger87270762005-06-23 12:24:09 -07001/*
Luca De Ciccob7d7a9e2006-06-11 23:01:39 -07002 * TCP Westwood+: end-to-end bandwidth estimation for TCP
Stephen Hemminger87270762005-06-23 12:24:09 -07003 *
Luca De Ciccob7d7a9e2006-06-11 23:01:39 -07004 * Angelo Dell'Aera: author of the first version of TCP Westwood+ in Linux 2.4
5 *
6 * Support at http://c3lab.poliba.it/index.php/Westwood
7 * Main references in literature:
8 *
9 * - Mascolo S, Casetti, M. Gerla et al.
10 * "TCP Westwood: bandwidth estimation for TCP" Proc. ACM Mobicom 2001
11 *
12 * - A. Grieco, s. Mascolo
13 * "Performance evaluation of New Reno, Vegas, Westwood+ TCP" ACM Computer
14 * Comm. Review, 2004
15 *
16 * - A. Dell'Aera, L. Grieco, S. Mascolo.
17 * "Linux 2.4 Implementation of Westwood+ TCP with Rate-Halving :
18 * A Performance Evaluation Over the Internet" (ICC 2004), Paris, June 2004
19 *
20 * Westwood+ employs end-to-end bandwidth measurement to set cwnd and
21 * ssthresh after packet loss. The probing phase is as the original Reno.
Stephen Hemminger87270762005-06-23 12:24:09 -070022 */
23
24#include <linux/config.h>
25#include <linux/mm.h>
26#include <linux/module.h>
27#include <linux/skbuff.h>
Arnaldo Carvalho de Meloa8c21902005-08-12 12:56:38 -030028#include <linux/inet_diag.h>
Stephen Hemminger87270762005-06-23 12:24:09 -070029#include <net/tcp.h>
30
31/* TCP Westwood structure */
32struct westwood {
33 u32 bw_ns_est; /* first bandwidth estimation..not too smoothed 8) */
34 u32 bw_est; /* bandwidth estimate */
35 u32 rtt_win_sx; /* here starts a new evaluation... */
36 u32 bk;
37 u32 snd_una; /* used for evaluating the number of acked bytes */
38 u32 cumul_ack;
39 u32 accounted;
40 u32 rtt;
41 u32 rtt_min; /* minimum observed RTT */
Stephen Hemmingerf61e2902006-06-11 23:01:02 -070042 u8 first_ack; /* flag which infers that this is the first ack */
Stephen Hemminger87270762005-06-23 12:24:09 -070043};
44
45
46/* TCP Westwood functions and constants */
47#define TCP_WESTWOOD_RTT_MIN (HZ/20) /* 50ms */
48#define TCP_WESTWOOD_INIT_RTT (20*HZ) /* maybe too conservative?! */
49
50/*
51 * @tcp_westwood_create
52 * This function initializes fields used in TCP Westwood+,
53 * it is called after the initial SYN, so the sequence numbers
54 * are correct but new passive connections we have no
55 * information about RTTmin at this time so we simply set it to
56 * TCP_WESTWOOD_INIT_RTT. This value was chosen to be too conservative
57 * since in this way we're sure it will be updated in a consistent
58 * way as soon as possible. It will reasonably happen within the first
59 * RTT period of the connection lifetime.
60 */
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -030061static void tcp_westwood_init(struct sock *sk)
Stephen Hemminger87270762005-06-23 12:24:09 -070062{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -030063 struct westwood *w = inet_csk_ca(sk);
Stephen Hemminger87270762005-06-23 12:24:09 -070064
65 w->bk = 0;
66 w->bw_ns_est = 0;
67 w->bw_est = 0;
68 w->accounted = 0;
69 w->cumul_ack = 0;
70 w->rtt_min = w->rtt = TCP_WESTWOOD_INIT_RTT;
71 w->rtt_win_sx = tcp_time_stamp;
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -030072 w->snd_una = tcp_sk(sk)->snd_una;
Stephen Hemmingerf61e2902006-06-11 23:01:02 -070073 w->first_ack = 1;
Stephen Hemminger87270762005-06-23 12:24:09 -070074}
75
76/*
77 * @westwood_do_filter
78 * Low-pass filter. Implemented using constant coefficients.
79 */
80static inline u32 westwood_do_filter(u32 a, u32 b)
81{
82 return (((7 * a) + b) >> 3);
83}
84
Luca De Ciccob3a92ea2006-06-11 23:01:59 -070085static void westwood_filter(struct westwood *w, u32 delta)
Stephen Hemminger87270762005-06-23 12:24:09 -070086{
Luca De Ciccob3a92ea2006-06-11 23:01:59 -070087 /* If the filter is empty fill it with the first sample of bandwidth */
88 if (w->bw_ns_est == 0 && w->bw_est == 0) {
89 w->bw_ns_est = w->bk / delta;
90 w->bw_est = w->bw_ns_est;
91 } else {
92 w->bw_ns_est = westwood_do_filter(w->bw_ns_est, w->bk / delta);
93 w->bw_est = westwood_do_filter(w->bw_est, w->bw_ns_est);
94 }
Stephen Hemminger87270762005-06-23 12:24:09 -070095}
96
97/*
98 * @westwood_pkts_acked
99 * Called after processing group of packets.
100 * but all westwood needs is the last sample of srtt.
101 */
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300102static void tcp_westwood_pkts_acked(struct sock *sk, u32 cnt)
Stephen Hemminger87270762005-06-23 12:24:09 -0700103{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300104 struct westwood *w = inet_csk_ca(sk);
Stephen Hemminger87270762005-06-23 12:24:09 -0700105 if (cnt > 0)
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300106 w->rtt = tcp_sk(sk)->srtt >> 3;
Stephen Hemminger87270762005-06-23 12:24:09 -0700107}
108
109/*
110 * @westwood_update_window
111 * It updates RTT evaluation window if it is the right moment to do
112 * it. If so it calls filter for evaluating bandwidth.
113 */
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300114static void westwood_update_window(struct sock *sk)
Stephen Hemminger87270762005-06-23 12:24:09 -0700115{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300116 struct westwood *w = inet_csk_ca(sk);
Stephen Hemminger87270762005-06-23 12:24:09 -0700117 s32 delta = tcp_time_stamp - w->rtt_win_sx;
118
Luca De Ciccob7d7a9e2006-06-11 23:01:39 -0700119 /* Initialize w->snd_una with the first acked sequence number in order
Stephen Hemmingerf61e2902006-06-11 23:01:02 -0700120 * to fix mismatch between tp->snd_una and w->snd_una for the first
121 * bandwidth sample
122 */
123 if (w->first_ack) {
124 w->snd_una = tcp_sk(sk)->snd_una;
125 w->first_ack = 0;
126 }
127
Stephen Hemminger87270762005-06-23 12:24:09 -0700128 /*
129 * See if a RTT-window has passed.
130 * Be careful since if RTT is less than
131 * 50ms we don't filter but we continue 'building the sample'.
132 * This minimum limit was chosen since an estimation on small
133 * time intervals is better to avoid...
134 * Obviously on a LAN we reasonably will always have
135 * right_bound = left_bound + WESTWOOD_RTT_MIN
136 */
137 if (w->rtt && delta > max_t(u32, w->rtt, TCP_WESTWOOD_RTT_MIN)) {
138 westwood_filter(w, delta);
139
140 w->bk = 0;
141 w->rtt_win_sx = tcp_time_stamp;
142 }
143}
144
145/*
146 * @westwood_fast_bw
147 * It is called when we are in fast path. In particular it is called when
148 * header prediction is successful. In such case in fact update is
149 * straight forward and doesn't need any particular care.
150 */
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300151static inline void westwood_fast_bw(struct sock *sk)
Stephen Hemminger87270762005-06-23 12:24:09 -0700152{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300153 const struct tcp_sock *tp = tcp_sk(sk);
154 struct westwood *w = inet_csk_ca(sk);
Stephen Hemminger87270762005-06-23 12:24:09 -0700155
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300156 westwood_update_window(sk);
Stephen Hemminger87270762005-06-23 12:24:09 -0700157
158 w->bk += tp->snd_una - w->snd_una;
159 w->snd_una = tp->snd_una;
160 w->rtt_min = min(w->rtt, w->rtt_min);
161}
162
163/*
164 * @westwood_acked_count
165 * This function evaluates cumul_ack for evaluating bk in case of
166 * delayed or partial acks.
167 */
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300168static inline u32 westwood_acked_count(struct sock *sk)
Stephen Hemminger87270762005-06-23 12:24:09 -0700169{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300170 const struct tcp_sock *tp = tcp_sk(sk);
171 struct westwood *w = inet_csk_ca(sk);
Stephen Hemminger87270762005-06-23 12:24:09 -0700172
173 w->cumul_ack = tp->snd_una - w->snd_una;
174
175 /* If cumul_ack is 0 this is a dupack since it's not moving
176 * tp->snd_una.
177 */
178 if (!w->cumul_ack) {
179 w->accounted += tp->mss_cache;
180 w->cumul_ack = tp->mss_cache;
181 }
182
183 if (w->cumul_ack > tp->mss_cache) {
184 /* Partial or delayed ack */
185 if (w->accounted >= w->cumul_ack) {
186 w->accounted -= w->cumul_ack;
187 w->cumul_ack = tp->mss_cache;
188 } else {
189 w->cumul_ack -= w->accounted;
190 w->accounted = 0;
191 }
192 }
193
194 w->snd_una = tp->snd_una;
195
196 return w->cumul_ack;
197}
198
Stephen Hemminger87270762005-06-23 12:24:09 -0700199
200/*
201 * TCP Westwood
202 * Here limit is evaluated as Bw estimation*RTTmin (for obtaining it
203 * in packets we use mss_cache). Rttmin is guaranteed to be >= 2
204 * so avoids ever returning 0.
205 */
Stephen Hemminger72dc5b92006-06-05 17:30:08 -0700206static u32 tcp_westwood_bw_rttmin(const struct sock *sk)
Stephen Hemminger87270762005-06-23 12:24:09 -0700207{
Stephen Hemminger72dc5b92006-06-05 17:30:08 -0700208 const struct tcp_sock *tp = tcp_sk(sk);
209 const struct westwood *w = inet_csk_ca(sk);
210 return max_t(u32, (w->bw_est * w->rtt_min) / tp->mss_cache, 2);
Stephen Hemminger87270762005-06-23 12:24:09 -0700211}
212
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300213static void tcp_westwood_event(struct sock *sk, enum tcp_ca_event event)
Stephen Hemminger87270762005-06-23 12:24:09 -0700214{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300215 struct tcp_sock *tp = tcp_sk(sk);
216 struct westwood *w = inet_csk_ca(sk);
Luca De Ciccob7d7a9e2006-06-11 23:01:39 -0700217
Stephen Hemminger87270762005-06-23 12:24:09 -0700218 switch(event) {
219 case CA_EVENT_FAST_ACK:
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300220 westwood_fast_bw(sk);
Stephen Hemminger87270762005-06-23 12:24:09 -0700221 break;
222
223 case CA_EVENT_COMPLETE_CWR:
Stephen Hemminger72dc5b92006-06-05 17:30:08 -0700224 tp->snd_cwnd = tp->snd_ssthresh = tcp_westwood_bw_rttmin(sk);
Stephen Hemminger87270762005-06-23 12:24:09 -0700225 break;
226
227 case CA_EVENT_FRTO:
Stephen Hemminger72dc5b92006-06-05 17:30:08 -0700228 tp->snd_ssthresh = tcp_westwood_bw_rttmin(sk);
Stephen Hemminger87270762005-06-23 12:24:09 -0700229 break;
230
231 case CA_EVENT_SLOW_ACK:
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300232 westwood_update_window(sk);
233 w->bk += westwood_acked_count(sk);
Stephen Hemminger87270762005-06-23 12:24:09 -0700234 w->rtt_min = min(w->rtt, w->rtt_min);
235 break;
236
237 default:
238 /* don't care */
239 break;
240 }
241}
242
243
244/* Extract info for Tcp socket info provided via netlink. */
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300245static void tcp_westwood_info(struct sock *sk, u32 ext,
Stephen Hemminger87270762005-06-23 12:24:09 -0700246 struct sk_buff *skb)
247{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300248 const struct westwood *ca = inet_csk_ca(sk);
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300249 if (ext & (1 << (INET_DIAG_VEGASINFO - 1))) {
Stephen Hemminger87270762005-06-23 12:24:09 -0700250 struct rtattr *rta;
251 struct tcpvegas_info *info;
252
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300253 rta = __RTA_PUT(skb, INET_DIAG_VEGASINFO, sizeof(*info));
Stephen Hemminger87270762005-06-23 12:24:09 -0700254 info = RTA_DATA(rta);
255 info->tcpv_enabled = 1;
256 info->tcpv_rttcnt = 0;
257 info->tcpv_rtt = jiffies_to_usecs(ca->rtt);
258 info->tcpv_minrtt = jiffies_to_usecs(ca->rtt_min);
259 rtattr_failure: ;
260 }
261}
262
263
264static struct tcp_congestion_ops tcp_westwood = {
265 .init = tcp_westwood_init,
266 .ssthresh = tcp_reno_ssthresh,
267 .cong_avoid = tcp_reno_cong_avoid,
Stephen Hemminger72dc5b92006-06-05 17:30:08 -0700268 .min_cwnd = tcp_westwood_bw_rttmin,
Stephen Hemminger87270762005-06-23 12:24:09 -0700269 .cwnd_event = tcp_westwood_event,
270 .get_info = tcp_westwood_info,
271 .pkts_acked = tcp_westwood_pkts_acked,
272
273 .owner = THIS_MODULE,
274 .name = "westwood"
275};
276
277static int __init tcp_westwood_register(void)
278{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300279 BUG_ON(sizeof(struct westwood) > ICSK_CA_PRIV_SIZE);
Stephen Hemminger87270762005-06-23 12:24:09 -0700280 return tcp_register_congestion_control(&tcp_westwood);
281}
282
283static void __exit tcp_westwood_unregister(void)
284{
285 tcp_unregister_congestion_control(&tcp_westwood);
286}
287
288module_init(tcp_westwood_register);
289module_exit(tcp_westwood_unregister);
290
291MODULE_AUTHOR("Stephen Hemminger, Angelo Dell'Aera");
292MODULE_LICENSE("GPL");
293MODULE_DESCRIPTION("TCP Westwood+");