blob: 62a96b71b0dd733fb5587729456180d27986d7d6 [file] [log] [blame]
Stephen Hemminger87270762005-06-23 12:24:09 -07001/*
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>
Arnaldo Carvalho de Meloa8c21902005-08-12 12:56:38 -030011#include <linux/inet_diag.h>
Stephen Hemminger87270762005-06-23 12:24:09 -070012#include <net/tcp.h>
13
14/* TCP Westwood structure */
15struct 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 */
Stephen Hemmingerf61e2902006-06-11 23:01:02 -070025 u8 first_ack; /* flag which infers that this is the first ack */
Stephen Hemminger87270762005-06-23 12:24:09 -070026};
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 */
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -030044static void tcp_westwood_init(struct sock *sk)
Stephen Hemminger87270762005-06-23 12:24:09 -070045{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -030046 struct westwood *w = inet_csk_ca(sk);
Stephen Hemminger87270762005-06-23 12:24:09 -070047
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;
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -030055 w->snd_una = tcp_sk(sk)->snd_una;
Stephen Hemmingerf61e2902006-06-11 23:01:02 -070056 w->first_ack = 1;
Stephen Hemminger87270762005-06-23 12:24:09 -070057}
58
59/*
60 * @westwood_do_filter
61 * Low-pass filter. Implemented using constant coefficients.
62 */
63static inline u32 westwood_do_filter(u32 a, u32 b)
64{
65 return (((7 * a) + b) >> 3);
66}
67
68static 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 */
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -030079static void tcp_westwood_pkts_acked(struct sock *sk, u32 cnt)
Stephen Hemminger87270762005-06-23 12:24:09 -070080{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -030081 struct westwood *w = inet_csk_ca(sk);
Stephen Hemminger87270762005-06-23 12:24:09 -070082 if (cnt > 0)
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -030083 w->rtt = tcp_sk(sk)->srtt >> 3;
Stephen Hemminger87270762005-06-23 12:24:09 -070084}
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 */
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -030091static void westwood_update_window(struct sock *sk)
Stephen Hemminger87270762005-06-23 12:24:09 -070092{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -030093 struct westwood *w = inet_csk_ca(sk);
Stephen Hemminger87270762005-06-23 12:24:09 -070094 s32 delta = tcp_time_stamp - w->rtt_win_sx;
95
Stephen Hemmingerf61e2902006-06-11 23:01:02 -070096 /* 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
Stephen Hemminger87270762005-06-23 12:24:09 -0700105 /*
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 */
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300128static inline void westwood_fast_bw(struct sock *sk)
Stephen Hemminger87270762005-06-23 12:24:09 -0700129{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300130 const struct tcp_sock *tp = tcp_sk(sk);
131 struct westwood *w = inet_csk_ca(sk);
Stephen Hemminger87270762005-06-23 12:24:09 -0700132
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300133 westwood_update_window(sk);
Stephen Hemminger87270762005-06-23 12:24:09 -0700134
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 */
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300145static inline u32 westwood_acked_count(struct sock *sk)
Stephen Hemminger87270762005-06-23 12:24:09 -0700146{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300147 const struct tcp_sock *tp = tcp_sk(sk);
148 struct westwood *w = inet_csk_ca(sk);
Stephen Hemminger87270762005-06-23 12:24:09 -0700149
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
Stephen Hemminger87270762005-06-23 12:24:09 -0700176
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 */
Stephen Hemminger72dc5b92006-06-05 17:30:08 -0700183static u32 tcp_westwood_bw_rttmin(const struct sock *sk)
Stephen Hemminger87270762005-06-23 12:24:09 -0700184{
Stephen Hemminger72dc5b92006-06-05 17:30:08 -0700185 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);
Stephen Hemminger87270762005-06-23 12:24:09 -0700188}
189
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300190static void tcp_westwood_event(struct sock *sk, enum tcp_ca_event event)
Stephen Hemminger87270762005-06-23 12:24:09 -0700191{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300192 struct tcp_sock *tp = tcp_sk(sk);
193 struct westwood *w = inet_csk_ca(sk);
Stephen Hemmingerf61e2902006-06-11 23:01:02 -0700194
Stephen Hemminger87270762005-06-23 12:24:09 -0700195 switch(event) {
196 case CA_EVENT_FAST_ACK:
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300197 westwood_fast_bw(sk);
Stephen Hemminger87270762005-06-23 12:24:09 -0700198 break;
199
200 case CA_EVENT_COMPLETE_CWR:
Stephen Hemminger72dc5b92006-06-05 17:30:08 -0700201 tp->snd_cwnd = tp->snd_ssthresh = tcp_westwood_bw_rttmin(sk);
Stephen Hemminger87270762005-06-23 12:24:09 -0700202 break;
203
204 case CA_EVENT_FRTO:
Stephen Hemminger72dc5b92006-06-05 17:30:08 -0700205 tp->snd_ssthresh = tcp_westwood_bw_rttmin(sk);
Stephen Hemminger87270762005-06-23 12:24:09 -0700206 break;
207
208 case CA_EVENT_SLOW_ACK:
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300209 westwood_update_window(sk);
210 w->bk += westwood_acked_count(sk);
Stephen Hemminger87270762005-06-23 12:24:09 -0700211 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. */
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300222static void tcp_westwood_info(struct sock *sk, u32 ext,
Stephen Hemminger87270762005-06-23 12:24:09 -0700223 struct sk_buff *skb)
224{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300225 const struct westwood *ca = inet_csk_ca(sk);
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300226 if (ext & (1 << (INET_DIAG_VEGASINFO - 1))) {
Stephen Hemminger87270762005-06-23 12:24:09 -0700227 struct rtattr *rta;
228 struct tcpvegas_info *info;
229
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300230 rta = __RTA_PUT(skb, INET_DIAG_VEGASINFO, sizeof(*info));
Stephen Hemminger87270762005-06-23 12:24:09 -0700231 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
241static struct tcp_congestion_ops tcp_westwood = {
242 .init = tcp_westwood_init,
243 .ssthresh = tcp_reno_ssthresh,
244 .cong_avoid = tcp_reno_cong_avoid,
Stephen Hemminger72dc5b92006-06-05 17:30:08 -0700245 .min_cwnd = tcp_westwood_bw_rttmin,
Stephen Hemminger87270762005-06-23 12:24:09 -0700246 .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
254static int __init tcp_westwood_register(void)
255{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300256 BUG_ON(sizeof(struct westwood) > ICSK_CA_PRIV_SIZE);
Stephen Hemminger87270762005-06-23 12:24:09 -0700257 return tcp_register_congestion_control(&tcp_westwood);
258}
259
260static void __exit tcp_westwood_unregister(void)
261{
262 tcp_unregister_congestion_control(&tcp_westwood);
263}
264
265module_init(tcp_westwood_register);
266module_exit(tcp_westwood_unregister);
267
268MODULE_AUTHOR("Stephen Hemminger, Angelo Dell'Aera");
269MODULE_LICENSE("GPL");
270MODULE_DESCRIPTION("TCP Westwood+");