blob: bb63fba47d47837b615c6bc6b730e36eead6328f [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
Stephen Hemminger87270762005-06-23 12:24:09 -070024#include <linux/mm.h>
25#include <linux/module.h>
26#include <linux/skbuff.h>
Arnaldo Carvalho de Meloa8c21902005-08-12 12:56:38 -030027#include <linux/inet_diag.h>
Stephen Hemminger87270762005-06-23 12:24:09 -070028#include <net/tcp.h>
29
30/* TCP Westwood structure */
31struct westwood {
32 u32 bw_ns_est; /* first bandwidth estimation..not too smoothed 8) */
33 u32 bw_est; /* bandwidth estimate */
34 u32 rtt_win_sx; /* here starts a new evaluation... */
35 u32 bk;
36 u32 snd_una; /* used for evaluating the number of acked bytes */
37 u32 cumul_ack;
38 u32 accounted;
39 u32 rtt;
40 u32 rtt_min; /* minimum observed RTT */
Stephen Hemmingerf61e2902006-06-11 23:01:02 -070041 u8 first_ack; /* flag which infers that this is the first ack */
Luca De Ciccobc726a72006-06-11 23:02:19 -070042 u8 reset_rtt_min; /* Reset RTT min to next RTT sample*/
Stephen Hemminger87270762005-06-23 12:24:09 -070043};
44
Stephen Hemminger87270762005-06-23 12:24:09 -070045/* TCP Westwood functions and constants */
46#define TCP_WESTWOOD_RTT_MIN (HZ/20) /* 50ms */
47#define TCP_WESTWOOD_INIT_RTT (20*HZ) /* maybe too conservative?! */
48
49/*
50 * @tcp_westwood_create
51 * This function initializes fields used in TCP Westwood+,
52 * it is called after the initial SYN, so the sequence numbers
53 * are correct but new passive connections we have no
54 * information about RTTmin at this time so we simply set it to
55 * TCP_WESTWOOD_INIT_RTT. This value was chosen to be too conservative
56 * since in this way we're sure it will be updated in a consistent
57 * way as soon as possible. It will reasonably happen within the first
58 * RTT period of the connection lifetime.
59 */
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -030060static void tcp_westwood_init(struct sock *sk)
Stephen Hemminger87270762005-06-23 12:24:09 -070061{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -030062 struct westwood *w = inet_csk_ca(sk);
Stephen Hemminger87270762005-06-23 12:24:09 -070063
64 w->bk = 0;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090065 w->bw_ns_est = 0;
66 w->bw_est = 0;
67 w->accounted = 0;
68 w->cumul_ack = 0;
Luca De Ciccobc726a72006-06-11 23:02:19 -070069 w->reset_rtt_min = 1;
Stephen Hemminger87270762005-06-23 12:24:09 -070070 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{
Eric Dumazeta02cec22010-09-22 20:43:57 +000082 return ((7 * a) + b) >> 3;
Stephen Hemminger87270762005-06-23 12:24:09 -070083}
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 */
Stephen Hemminger30cfd0b2007-07-25 23:49:34 -0700102static void tcp_westwood_pkts_acked(struct sock *sk, u32 cnt, s32 rtt)
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 Hemminger30cfd0b2007-07-25 23:49:34 -0700105
106 if (rtt > 0)
107 w->rtt = usecs_to_jiffies(rtt);
Stephen Hemminger87270762005-06-23 12:24:09 -0700108}
109
110/*
111 * @westwood_update_window
112 * It updates RTT evaluation window if it is the right moment to do
113 * it. If so it calls filter for evaluating bandwidth.
114 */
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300115static void westwood_update_window(struct sock *sk)
Stephen Hemminger87270762005-06-23 12:24:09 -0700116{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300117 struct westwood *w = inet_csk_ca(sk);
Stephen Hemminger87270762005-06-23 12:24:09 -0700118 s32 delta = tcp_time_stamp - w->rtt_win_sx;
119
Luca De Ciccob7d7a9e2006-06-11 23:01:39 -0700120 /* Initialize w->snd_una with the first acked sequence number in order
Stephen Hemmingerf61e2902006-06-11 23:01:02 -0700121 * to fix mismatch between tp->snd_una and w->snd_una for the first
122 * bandwidth sample
123 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900124 if (w->first_ack) {
Stephen Hemmingerf61e2902006-06-11 23:01:02 -0700125 w->snd_una = tcp_sk(sk)->snd_una;
126 w->first_ack = 0;
127 }
128
Stephen Hemminger87270762005-06-23 12:24:09 -0700129 /*
130 * See if a RTT-window has passed.
131 * Be careful since if RTT is less than
132 * 50ms we don't filter but we continue 'building the sample'.
133 * This minimum limit was chosen since an estimation on small
134 * time intervals is better to avoid...
135 * Obviously on a LAN we reasonably will always have
136 * right_bound = left_bound + WESTWOOD_RTT_MIN
137 */
138 if (w->rtt && delta > max_t(u32, w->rtt, TCP_WESTWOOD_RTT_MIN)) {
139 westwood_filter(w, delta);
140
141 w->bk = 0;
142 w->rtt_win_sx = tcp_time_stamp;
143 }
144}
145
Luca De Ciccobc726a72006-06-11 23:02:19 -0700146static inline void update_rtt_min(struct westwood *w)
147{
148 if (w->reset_rtt_min) {
149 w->rtt_min = w->rtt;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900150 w->reset_rtt_min = 0;
Luca De Ciccobc726a72006-06-11 23:02:19 -0700151 } else
152 w->rtt_min = min(w->rtt, w->rtt_min);
153}
154
Stephen Hemminger87270762005-06-23 12:24:09 -0700155/*
156 * @westwood_fast_bw
157 * It is called when we are in fast path. In particular it is called when
158 * header prediction is successful. In such case in fact update is
159 * straight forward and doesn't need any particular care.
160 */
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300161static inline void westwood_fast_bw(struct sock *sk)
Stephen Hemminger87270762005-06-23 12:24:09 -0700162{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300163 const struct tcp_sock *tp = tcp_sk(sk);
164 struct westwood *w = inet_csk_ca(sk);
Stephen Hemminger87270762005-06-23 12:24:09 -0700165
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300166 westwood_update_window(sk);
Stephen Hemminger87270762005-06-23 12:24:09 -0700167
168 w->bk += tp->snd_una - w->snd_una;
169 w->snd_una = tp->snd_una;
Luca De Ciccobc726a72006-06-11 23:02:19 -0700170 update_rtt_min(w);
Stephen Hemminger87270762005-06-23 12:24:09 -0700171}
172
173/*
174 * @westwood_acked_count
175 * This function evaluates cumul_ack for evaluating bk in case of
176 * delayed or partial acks.
177 */
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300178static inline u32 westwood_acked_count(struct sock *sk)
Stephen Hemminger87270762005-06-23 12:24:09 -0700179{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300180 const struct tcp_sock *tp = tcp_sk(sk);
181 struct westwood *w = inet_csk_ca(sk);
Stephen Hemminger87270762005-06-23 12:24:09 -0700182
183 w->cumul_ack = tp->snd_una - w->snd_una;
184
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900185 /* If cumul_ack is 0 this is a dupack since it's not moving
186 * tp->snd_una.
187 */
188 if (!w->cumul_ack) {
Stephen Hemminger87270762005-06-23 12:24:09 -0700189 w->accounted += tp->mss_cache;
190 w->cumul_ack = tp->mss_cache;
191 }
192
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900193 if (w->cumul_ack > tp->mss_cache) {
Stephen Hemminger87270762005-06-23 12:24:09 -0700194 /* Partial or delayed ack */
195 if (w->accounted >= w->cumul_ack) {
196 w->accounted -= w->cumul_ack;
197 w->cumul_ack = tp->mss_cache;
198 } else {
199 w->cumul_ack -= w->accounted;
200 w->accounted = 0;
201 }
202 }
203
204 w->snd_una = tp->snd_una;
205
206 return w->cumul_ack;
207}
208
Stephen Hemminger87270762005-06-23 12:24:09 -0700209/*
210 * TCP Westwood
211 * Here limit is evaluated as Bw estimation*RTTmin (for obtaining it
212 * in packets we use mss_cache). Rttmin is guaranteed to be >= 2
213 * so avoids ever returning 0.
214 */
Stephen Hemminger72dc5b92006-06-05 17:30:08 -0700215static u32 tcp_westwood_bw_rttmin(const struct sock *sk)
Stephen Hemminger87270762005-06-23 12:24:09 -0700216{
Stephen Hemminger72dc5b92006-06-05 17:30:08 -0700217 const struct tcp_sock *tp = tcp_sk(sk);
218 const struct westwood *w = inet_csk_ca(sk);
stephen hemminger688d1942014-08-29 23:32:05 -0700219
Stephen Hemminger72dc5b92006-06-05 17:30:08 -0700220 return max_t(u32, (w->bw_est * w->rtt_min) / tp->mss_cache, 2);
Stephen Hemminger87270762005-06-23 12:24:09 -0700221}
222
Florian Westphal7354c8c2014-09-26 22:37:34 +0200223static void tcp_westwood_ack(struct sock *sk, u32 ack_flags)
224{
225 if (ack_flags & CA_ACK_SLOWPATH) {
226 struct westwood *w = inet_csk_ca(sk);
227
228 westwood_update_window(sk);
229 w->bk += westwood_acked_count(sk);
230
231 update_rtt_min(w);
232 return;
233 }
234
235 westwood_fast_bw(sk);
236}
237
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300238static void tcp_westwood_event(struct sock *sk, enum tcp_ca_event event)
Stephen Hemminger87270762005-06-23 12:24:09 -0700239{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300240 struct tcp_sock *tp = tcp_sk(sk);
241 struct westwood *w = inet_csk_ca(sk);
Luca De Ciccob7d7a9e2006-06-11 23:01:39 -0700242
Stephen Hemminger2de979b2007-03-08 20:45:19 -0800243 switch (event) {
Stephen Hemminger87270762005-06-23 12:24:09 -0700244 case CA_EVENT_COMPLETE_CWR:
Stephen Hemminger72dc5b92006-06-05 17:30:08 -0700245 tp->snd_cwnd = tp->snd_ssthresh = tcp_westwood_bw_rttmin(sk);
Stephen Hemminger87270762005-06-23 12:24:09 -0700246 break;
Yuchung Cheng9b441902013-03-20 13:32:58 +0000247 case CA_EVENT_LOSS:
Stephen Hemminger72dc5b92006-06-05 17:30:08 -0700248 tp->snd_ssthresh = tcp_westwood_bw_rttmin(sk);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900249 /* Update RTT_min when next ack arrives */
Luca De Ciccobc726a72006-06-11 23:02:19 -0700250 w->reset_rtt_min = 1;
Stephen Hemminger87270762005-06-23 12:24:09 -0700251 break;
Stephen Hemminger87270762005-06-23 12:24:09 -0700252 default:
253 /* don't care */
254 break;
255 }
256}
257
Stephen Hemminger87270762005-06-23 12:24:09 -0700258/* Extract info for Tcp socket info provided via netlink. */
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300259static void tcp_westwood_info(struct sock *sk, u32 ext,
Stephen Hemminger87270762005-06-23 12:24:09 -0700260 struct sk_buff *skb)
261{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300262 const struct westwood *ca = inet_csk_ca(sk);
stephen hemminger688d1942014-08-29 23:32:05 -0700263
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300264 if (ext & (1 << (INET_DIAG_VEGASINFO - 1))) {
Thomas Graf26728102007-03-22 23:27:19 -0700265 struct tcpvegas_info info = {
266 .tcpv_enabled = 1,
267 .tcpv_rtt = jiffies_to_usecs(ca->rtt),
268 .tcpv_minrtt = jiffies_to_usecs(ca->rtt_min),
269 };
Stephen Hemminger87270762005-06-23 12:24:09 -0700270
Thomas Graf26728102007-03-22 23:27:19 -0700271 nla_put(skb, INET_DIAG_VEGASINFO, sizeof(info), &info);
Stephen Hemminger87270762005-06-23 12:24:09 -0700272 }
273}
274
Stephen Hemmingera252beb2011-03-10 00:40:17 -0800275static struct tcp_congestion_ops tcp_westwood __read_mostly = {
Stephen Hemminger87270762005-06-23 12:24:09 -0700276 .init = tcp_westwood_init,
277 .ssthresh = tcp_reno_ssthresh,
278 .cong_avoid = tcp_reno_cong_avoid,
Stephen Hemminger87270762005-06-23 12:24:09 -0700279 .cwnd_event = tcp_westwood_event,
Florian Westphal7354c8c2014-09-26 22:37:34 +0200280 .in_ack_event = tcp_westwood_ack,
Stephen Hemminger87270762005-06-23 12:24:09 -0700281 .get_info = tcp_westwood_info,
282 .pkts_acked = tcp_westwood_pkts_acked,
283
284 .owner = THIS_MODULE,
285 .name = "westwood"
286};
287
288static int __init tcp_westwood_register(void)
289{
Alexey Dobriyan74975d42006-08-25 17:10:33 -0700290 BUILD_BUG_ON(sizeof(struct westwood) > ICSK_CA_PRIV_SIZE);
Stephen Hemminger87270762005-06-23 12:24:09 -0700291 return tcp_register_congestion_control(&tcp_westwood);
292}
293
294static void __exit tcp_westwood_unregister(void)
295{
296 tcp_unregister_congestion_control(&tcp_westwood);
297}
298
299module_init(tcp_westwood_register);
300module_exit(tcp_westwood_unregister);
301
302MODULE_AUTHOR("Stephen Hemminger, Angelo Dell'Aera");
303MODULE_LICENSE("GPL");
304MODULE_DESCRIPTION("TCP Westwood+");