Blame view

net/ipv4/tcp_westwood.c 8.24 KB
872707628   Stephen Hemminger   [TCP]: Add TCP We...
1
  /*
b7d7a9e3c   Luca De Cicco   [TCP] Westwood: c...
2
   * TCP Westwood+: end-to-end bandwidth estimation for TCP
872707628   Stephen Hemminger   [TCP]: Add TCP We...
3
   *
b7d7a9e3c   Luca De Cicco   [TCP] Westwood: c...
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
   *      Angelo Dell'Aera: author of the first version of TCP Westwood+ in Linux 2.4
   *
   * Support at http://c3lab.poliba.it/index.php/Westwood
   * Main references in literature:
   *
   * - Mascolo S, Casetti, M. Gerla et al.
   *   "TCP Westwood: bandwidth estimation for TCP" Proc. ACM Mobicom 2001
   *
   * - A. Grieco, s. Mascolo
   *   "Performance evaluation of New Reno, Vegas, Westwood+ TCP" ACM Computer
   *     Comm. Review, 2004
   *
   * - A. Dell'Aera, L. Grieco, S. Mascolo.
   *   "Linux 2.4 Implementation of Westwood+ TCP with Rate-Halving :
   *    A Performance Evaluation Over the Internet" (ICC 2004), Paris, June 2004
   *
   * Westwood+ employs end-to-end bandwidth measurement to set cwnd and
   * ssthresh after packet loss. The probing phase is as the original Reno.
872707628   Stephen Hemminger   [TCP]: Add TCP We...
22
   */
872707628   Stephen Hemminger   [TCP]: Add TCP We...
23
24
25
  #include <linux/mm.h>
  #include <linux/module.h>
  #include <linux/skbuff.h>
a8c2190ee   Arnaldo Carvalho de Melo   [INET_DIAG]: Rena...
26
  #include <linux/inet_diag.h>
872707628   Stephen Hemminger   [TCP]: Add TCP We...
27
28
29
30
31
32
33
34
35
36
37
38
39
  #include <net/tcp.h>
  
  /* TCP Westwood structure */
  struct westwood {
  	u32    bw_ns_est;        /* first bandwidth estimation..not too smoothed 8) */
  	u32    bw_est;           /* bandwidth estimate */
  	u32    rtt_win_sx;       /* here starts a new evaluation... */
  	u32    bk;
  	u32    snd_una;          /* used for evaluating the number of acked bytes */
  	u32    cumul_ack;
  	u32    accounted;
  	u32    rtt;
  	u32    rtt_min;          /* minimum observed RTT */
f61e29018   Stephen Hemminger   [TCP] Westwood: f...
40
  	u8     first_ack;        /* flag which infers that this is the first ack */
bc726a71d   Luca De Cicco   [TCP] Westwood: r...
41
  	u8     reset_rtt_min;    /* Reset RTT min to next RTT sample*/
872707628   Stephen Hemminger   [TCP]: Add TCP We...
42
  };
872707628   Stephen Hemminger   [TCP]: Add TCP We...
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
  /* TCP Westwood functions and constants */
  #define TCP_WESTWOOD_RTT_MIN   (HZ/20)	/* 50ms */
  #define TCP_WESTWOOD_INIT_RTT  (20*HZ)	/* maybe too conservative?! */
  
  /*
   * @tcp_westwood_create
   * This function initializes fields used in TCP Westwood+,
   * it is called after the initial SYN, so the sequence numbers
   * are correct but new passive connections we have no
   * information about RTTmin at this time so we simply set it to
   * TCP_WESTWOOD_INIT_RTT. This value was chosen to be too conservative
   * since in this way we're sure it will be updated in a consistent
   * way as soon as possible. It will reasonably happen within the first
   * RTT period of the connection lifetime.
   */
6687e988d   Arnaldo Carvalho de Melo   [ICSK]: Move TCP ...
58
  static void tcp_westwood_init(struct sock *sk)
872707628   Stephen Hemminger   [TCP]: Add TCP We...
59
  {
6687e988d   Arnaldo Carvalho de Melo   [ICSK]: Move TCP ...
60
  	struct westwood *w = inet_csk_ca(sk);
872707628   Stephen Hemminger   [TCP]: Add TCP We...
61
62
  
  	w->bk = 0;
e905a9eda   YOSHIFUJI Hideaki   [NET] IPV4: Fix w...
63
64
65
66
  	w->bw_ns_est = 0;
  	w->bw_est = 0;
  	w->accounted = 0;
  	w->cumul_ack = 0;
bc726a71d   Luca De Cicco   [TCP] Westwood: r...
67
  	w->reset_rtt_min = 1;
872707628   Stephen Hemminger   [TCP]: Add TCP We...
68
  	w->rtt_min = w->rtt = TCP_WESTWOOD_INIT_RTT;
ad5ad69e6   Eric Dumazet   tcp_westwood: use...
69
  	w->rtt_win_sx = tcp_jiffies32;
6687e988d   Arnaldo Carvalho de Melo   [ICSK]: Move TCP ...
70
  	w->snd_una = tcp_sk(sk)->snd_una;
f61e29018   Stephen Hemminger   [TCP] Westwood: f...
71
  	w->first_ack = 1;
872707628   Stephen Hemminger   [TCP]: Add TCP We...
72
73
74
75
76
77
78
79
  }
  
  /*
   * @westwood_do_filter
   * Low-pass filter. Implemented using constant coefficients.
   */
  static inline u32 westwood_do_filter(u32 a, u32 b)
  {
a02cec215   Eric Dumazet   net: return opera...
80
  	return ((7 * a) + b) >> 3;
872707628   Stephen Hemminger   [TCP]: Add TCP We...
81
  }
b3a92eabe   Luca De Cicco   [TCP] Westwood: b...
82
  static void westwood_filter(struct westwood *w, u32 delta)
872707628   Stephen Hemminger   [TCP]: Add TCP We...
83
  {
b3a92eabe   Luca De Cicco   [TCP] Westwood: b...
84
85
86
87
88
89
90
91
  	/* If the filter is empty fill it with the first sample of bandwidth  */
  	if (w->bw_ns_est == 0 && w->bw_est == 0) {
  		w->bw_ns_est = w->bk / delta;
  		w->bw_est = w->bw_ns_est;
  	} else {
  		w->bw_ns_est = westwood_do_filter(w->bw_ns_est, w->bk / delta);
  		w->bw_est = westwood_do_filter(w->bw_est, w->bw_ns_est);
  	}
872707628   Stephen Hemminger   [TCP]: Add TCP We...
92
93
94
95
96
97
98
  }
  
  /*
   * @westwood_pkts_acked
   * Called after processing group of packets.
   * but all westwood needs is the last sample of srtt.
   */
756ee1729   Lawrence Brakmo   tcp: replace cnt ...
99
100
  static void tcp_westwood_pkts_acked(struct sock *sk,
  				    const struct ack_sample *sample)
872707628   Stephen Hemminger   [TCP]: Add TCP We...
101
  {
6687e988d   Arnaldo Carvalho de Melo   [ICSK]: Move TCP ...
102
  	struct westwood *w = inet_csk_ca(sk);
30cfd0baf   Stephen Hemminger   [TCP]: congestion...
103

756ee1729   Lawrence Brakmo   tcp: replace cnt ...
104
105
  	if (sample->rtt_us > 0)
  		w->rtt = usecs_to_jiffies(sample->rtt_us);
872707628   Stephen Hemminger   [TCP]: Add TCP We...
106
107
108
109
110
111
112
  }
  
  /*
   * @westwood_update_window
   * It updates RTT evaluation window if it is the right moment to do
   * it. If so it calls filter for evaluating bandwidth.
   */
6687e988d   Arnaldo Carvalho de Melo   [ICSK]: Move TCP ...
113
  static void westwood_update_window(struct sock *sk)
872707628   Stephen Hemminger   [TCP]: Add TCP We...
114
  {
6687e988d   Arnaldo Carvalho de Melo   [ICSK]: Move TCP ...
115
  	struct westwood *w = inet_csk_ca(sk);
ad5ad69e6   Eric Dumazet   tcp_westwood: use...
116
  	s32 delta = tcp_jiffies32 - w->rtt_win_sx;
872707628   Stephen Hemminger   [TCP]: Add TCP We...
117

b7d7a9e3c   Luca De Cicco   [TCP] Westwood: c...
118
  	/* Initialize w->snd_una with the first acked sequence number in order
f61e29018   Stephen Hemminger   [TCP] Westwood: f...
119
120
121
  	 * to fix mismatch between tp->snd_una and w->snd_una for the first
  	 * bandwidth sample
  	 */
e905a9eda   YOSHIFUJI Hideaki   [NET] IPV4: Fix w...
122
  	if (w->first_ack) {
f61e29018   Stephen Hemminger   [TCP] Westwood: f...
123
124
125
  		w->snd_una = tcp_sk(sk)->snd_una;
  		w->first_ack = 0;
  	}
872707628   Stephen Hemminger   [TCP]: Add TCP We...
126
127
128
129
130
131
132
133
134
135
136
137
138
  	/*
  	 * See if a RTT-window has passed.
  	 * Be careful since if RTT is less than
  	 * 50ms we don't filter but we continue 'building the sample'.
  	 * This minimum limit was chosen since an estimation on small
  	 * time intervals is better to avoid...
  	 * Obviously on a LAN we reasonably will always have
  	 * right_bound = left_bound + WESTWOOD_RTT_MIN
  	 */
  	if (w->rtt && delta > max_t(u32, w->rtt, TCP_WESTWOOD_RTT_MIN)) {
  		westwood_filter(w, delta);
  
  		w->bk = 0;
ad5ad69e6   Eric Dumazet   tcp_westwood: use...
139
  		w->rtt_win_sx = tcp_jiffies32;
872707628   Stephen Hemminger   [TCP]: Add TCP We...
140
141
  	}
  }
bc726a71d   Luca De Cicco   [TCP] Westwood: r...
142
143
144
145
  static inline void update_rtt_min(struct westwood *w)
  {
  	if (w->reset_rtt_min) {
  		w->rtt_min = w->rtt;
e905a9eda   YOSHIFUJI Hideaki   [NET] IPV4: Fix w...
146
  		w->reset_rtt_min = 0;
bc726a71d   Luca De Cicco   [TCP] Westwood: r...
147
148
149
  	} else
  		w->rtt_min = min(w->rtt, w->rtt_min);
  }
872707628   Stephen Hemminger   [TCP]: Add TCP We...
150
151
152
153
154
155
  /*
   * @westwood_fast_bw
   * It is called when we are in fast path. In particular it is called when
   * header prediction is successful. In such case in fact update is
   * straight forward and doesn't need any particular care.
   */
6687e988d   Arnaldo Carvalho de Melo   [ICSK]: Move TCP ...
156
  static inline void westwood_fast_bw(struct sock *sk)
872707628   Stephen Hemminger   [TCP]: Add TCP We...
157
  {
6687e988d   Arnaldo Carvalho de Melo   [ICSK]: Move TCP ...
158
159
  	const struct tcp_sock *tp = tcp_sk(sk);
  	struct westwood *w = inet_csk_ca(sk);
872707628   Stephen Hemminger   [TCP]: Add TCP We...
160

6687e988d   Arnaldo Carvalho de Melo   [ICSK]: Move TCP ...
161
  	westwood_update_window(sk);
872707628   Stephen Hemminger   [TCP]: Add TCP We...
162
163
164
  
  	w->bk += tp->snd_una - w->snd_una;
  	w->snd_una = tp->snd_una;
bc726a71d   Luca De Cicco   [TCP] Westwood: r...
165
  	update_rtt_min(w);
872707628   Stephen Hemminger   [TCP]: Add TCP We...
166
167
168
169
170
171
172
  }
  
  /*
   * @westwood_acked_count
   * This function evaluates cumul_ack for evaluating bk in case of
   * delayed or partial acks.
   */
6687e988d   Arnaldo Carvalho de Melo   [ICSK]: Move TCP ...
173
  static inline u32 westwood_acked_count(struct sock *sk)
872707628   Stephen Hemminger   [TCP]: Add TCP We...
174
  {
6687e988d   Arnaldo Carvalho de Melo   [ICSK]: Move TCP ...
175
176
  	const struct tcp_sock *tp = tcp_sk(sk);
  	struct westwood *w = inet_csk_ca(sk);
872707628   Stephen Hemminger   [TCP]: Add TCP We...
177
178
  
  	w->cumul_ack = tp->snd_una - w->snd_una;
e905a9eda   YOSHIFUJI Hideaki   [NET] IPV4: Fix w...
179
180
181
182
  	/* If cumul_ack is 0 this is a dupack since it's not moving
  	 * tp->snd_una.
  	 */
  	if (!w->cumul_ack) {
872707628   Stephen Hemminger   [TCP]: Add TCP We...
183
184
185
  		w->accounted += tp->mss_cache;
  		w->cumul_ack = tp->mss_cache;
  	}
e905a9eda   YOSHIFUJI Hideaki   [NET] IPV4: Fix w...
186
  	if (w->cumul_ack > tp->mss_cache) {
872707628   Stephen Hemminger   [TCP]: Add TCP We...
187
188
189
190
191
192
193
194
195
196
197
198
199
200
  		/* Partial or delayed ack */
  		if (w->accounted >= w->cumul_ack) {
  			w->accounted -= w->cumul_ack;
  			w->cumul_ack = tp->mss_cache;
  		} else {
  			w->cumul_ack -= w->accounted;
  			w->accounted = 0;
  		}
  	}
  
  	w->snd_una = tp->snd_una;
  
  	return w->cumul_ack;
  }
872707628   Stephen Hemminger   [TCP]: Add TCP We...
201
202
203
204
205
206
  /*
   * TCP Westwood
   * Here limit is evaluated as Bw estimation*RTTmin (for obtaining it
   * in packets we use mss_cache). Rttmin is guaranteed to be >= 2
   * so avoids ever returning 0.
   */
72dc5b922   Stephen Hemminger   [TCP]: Minimum co...
207
  static u32 tcp_westwood_bw_rttmin(const struct sock *sk)
872707628   Stephen Hemminger   [TCP]: Add TCP We...
208
  {
72dc5b922   Stephen Hemminger   [TCP]: Minimum co...
209
210
  	const struct tcp_sock *tp = tcp_sk(sk);
  	const struct westwood *w = inet_csk_ca(sk);
688d1945b   stephen hemminger   tcp: whitespace f...
211

72dc5b922   Stephen Hemminger   [TCP]: Minimum co...
212
  	return max_t(u32, (w->bw_est * w->rtt_min) / tp->mss_cache, 2);
872707628   Stephen Hemminger   [TCP]: Add TCP We...
213
  }
7354c8c38   Florian Westphal   net: tcp: split a...
214
215
216
217
218
219
220
221
222
223
224
225
226
227
  static void tcp_westwood_ack(struct sock *sk, u32 ack_flags)
  {
  	if (ack_flags & CA_ACK_SLOWPATH) {
  		struct westwood *w = inet_csk_ca(sk);
  
  		westwood_update_window(sk);
  		w->bk += westwood_acked_count(sk);
  
  		update_rtt_min(w);
  		return;
  	}
  
  	westwood_fast_bw(sk);
  }
6687e988d   Arnaldo Carvalho de Melo   [ICSK]: Move TCP ...
228
  static void tcp_westwood_event(struct sock *sk, enum tcp_ca_event event)
872707628   Stephen Hemminger   [TCP]: Add TCP We...
229
  {
6687e988d   Arnaldo Carvalho de Melo   [ICSK]: Move TCP ...
230
231
  	struct tcp_sock *tp = tcp_sk(sk);
  	struct westwood *w = inet_csk_ca(sk);
b7d7a9e3c   Luca De Cicco   [TCP] Westwood: c...
232

2de979bd7   Stephen Hemminger   [TCP]: whitespace...
233
  	switch (event) {
872707628   Stephen Hemminger   [TCP]: Add TCP We...
234
  	case CA_EVENT_COMPLETE_CWR:
72dc5b922   Stephen Hemminger   [TCP]: Minimum co...
235
  		tp->snd_cwnd = tp->snd_ssthresh = tcp_westwood_bw_rttmin(sk);
872707628   Stephen Hemminger   [TCP]: Add TCP We...
236
  		break;
9b44190dc   Yuchung Cheng   tcp: refactor F-RTO
237
  	case CA_EVENT_LOSS:
72dc5b922   Stephen Hemminger   [TCP]: Minimum co...
238
  		tp->snd_ssthresh = tcp_westwood_bw_rttmin(sk);
e905a9eda   YOSHIFUJI Hideaki   [NET] IPV4: Fix w...
239
  		/* Update RTT_min when next ack arrives */
bc726a71d   Luca De Cicco   [TCP] Westwood: r...
240
  		w->reset_rtt_min = 1;
872707628   Stephen Hemminger   [TCP]: Add TCP We...
241
  		break;
872707628   Stephen Hemminger   [TCP]: Add TCP We...
242
243
244
245
246
  	default:
  		/* don't care */
  		break;
  	}
  }
872707628   Stephen Hemminger   [TCP]: Add TCP We...
247
  /* Extract info for Tcp socket info provided via netlink. */
31ccd0e66   Eric Dumazet   tcp_westwood: fix...
248
249
  static size_t tcp_westwood_info(struct sock *sk, u32 ext, int *attr,
  				union tcp_cc_info *info)
872707628   Stephen Hemminger   [TCP]: Add TCP We...
250
  {
6687e988d   Arnaldo Carvalho de Melo   [ICSK]: Move TCP ...
251
  	const struct westwood *ca = inet_csk_ca(sk);
688d1945b   stephen hemminger   tcp: whitespace f...
252

73c1f4a03   Arnaldo Carvalho de Melo   [TCPDIAG]: Just r...
253
  	if (ext & (1 << (INET_DIAG_VEGASINFO - 1))) {
31ccd0e66   Eric Dumazet   tcp_westwood: fix...
254
255
  		info->vegas.tcpv_enabled = 1;
  		info->vegas.tcpv_rttcnt	= 0;
be7164cd5   chun Long   tcp_westwood: fix...
256
257
  		info->vegas.tcpv_rtt	= jiffies_to_usecs(ca->rtt);
  		info->vegas.tcpv_minrtt	= jiffies_to_usecs(ca->rtt_min);
267281058   Thomas Graf   [TCP] westwood: U...
258

31ccd0e66   Eric Dumazet   tcp_westwood: fix...
259
260
  		*attr = INET_DIAG_VEGASINFO;
  		return sizeof(struct tcpvegas_info);
872707628   Stephen Hemminger   [TCP]: Add TCP We...
261
  	}
521f1cf1d   Eric Dumazet   inet_diag: fix ac...
262
  	return 0;
872707628   Stephen Hemminger   [TCP]: Add TCP We...
263
  }
a252bebe2   Stephen Hemminger   tcp: mark tcp_con...
264
  static struct tcp_congestion_ops tcp_westwood __read_mostly = {
872707628   Stephen Hemminger   [TCP]: Add TCP We...
265
266
267
  	.init		= tcp_westwood_init,
  	.ssthresh	= tcp_reno_ssthresh,
  	.cong_avoid	= tcp_reno_cong_avoid,
e97991832   Florian Westphal   tcp: make undo_cw...
268
  	.undo_cwnd      = tcp_reno_undo_cwnd,
872707628   Stephen Hemminger   [TCP]: Add TCP We...
269
  	.cwnd_event	= tcp_westwood_event,
7354c8c38   Florian Westphal   net: tcp: split a...
270
  	.in_ack_event	= tcp_westwood_ack,
872707628   Stephen Hemminger   [TCP]: Add TCP We...
271
272
273
274
275
276
277
278
279
  	.get_info	= tcp_westwood_info,
  	.pkts_acked	= tcp_westwood_pkts_acked,
  
  	.owner		= THIS_MODULE,
  	.name		= "westwood"
  };
  
  static int __init tcp_westwood_register(void)
  {
74975d40b   Alexey Dobriyan   [TCP] Congestion ...
280
  	BUILD_BUG_ON(sizeof(struct westwood) > ICSK_CA_PRIV_SIZE);
872707628   Stephen Hemminger   [TCP]: Add TCP We...
281
282
283
284
285
286
287
288
289
290
291
292
293
294
  	return tcp_register_congestion_control(&tcp_westwood);
  }
  
  static void __exit tcp_westwood_unregister(void)
  {
  	tcp_unregister_congestion_control(&tcp_westwood);
  }
  
  module_init(tcp_westwood_register);
  module_exit(tcp_westwood_unregister);
  
  MODULE_AUTHOR("Stephen Hemminger, Angelo Dell'Aera");
  MODULE_LICENSE("GPL");
  MODULE_DESCRIPTION("TCP Westwood+");