blob: 34ae3f13483a6e37b65402e3318bd635b716e460 [file] [log] [blame]
Stephen Hemminger317a76f2005-06-23 12:19:55 -07001/*
2 * Plugable TCP congestion control support and newReno
3 * congestion control.
4 * Based on ideas from I/O scheduler suport and Web100.
5 *
6 * Copyright (C) 2005 Stephen Hemminger <shemminger@osdl.org>
7 */
8
Stephen Hemminger317a76f2005-06-23 12:19:55 -07009#include <linux/module.h>
10#include <linux/mm.h>
11#include <linux/types.h>
12#include <linux/list.h>
13#include <net/tcp.h>
14
15static DEFINE_SPINLOCK(tcp_cong_list_lock);
16static LIST_HEAD(tcp_cong_list);
17
18/* Simple linear search, don't expect many entries! */
19static struct tcp_congestion_ops *tcp_ca_find(const char *name)
20{
21 struct tcp_congestion_ops *e;
22
Stephen Hemminger5f8ef482005-06-23 20:37:36 -070023 list_for_each_entry_rcu(e, &tcp_cong_list, list) {
Stephen Hemminger317a76f2005-06-23 12:19:55 -070024 if (strcmp(e->name, name) == 0)
25 return e;
26 }
27
28 return NULL;
29}
30
31/*
Robert P. J. Dayd08df602007-02-17 19:07:33 +010032 * Attach new congestion control algorithm to the list
Stephen Hemminger317a76f2005-06-23 12:19:55 -070033 * of available options.
34 */
35int tcp_register_congestion_control(struct tcp_congestion_ops *ca)
36{
37 int ret = 0;
38
39 /* all algorithms must implement ssthresh and cong_avoid ops */
Stephen Hemminger72dc5b92006-06-05 17:30:08 -070040 if (!ca->ssthresh || !ca->cong_avoid) {
Stephen Hemminger317a76f2005-06-23 12:19:55 -070041 printk(KERN_ERR "TCP %s does not implement required ops\n",
42 ca->name);
43 return -EINVAL;
44 }
45
46 spin_lock(&tcp_cong_list_lock);
47 if (tcp_ca_find(ca->name)) {
48 printk(KERN_NOTICE "TCP %s already registered\n", ca->name);
49 ret = -EEXIST;
50 } else {
Stephen Hemminger3d2573f2006-09-24 20:11:58 -070051 list_add_tail_rcu(&ca->list, &tcp_cong_list);
Stephen Hemminger317a76f2005-06-23 12:19:55 -070052 printk(KERN_INFO "TCP %s registered\n", ca->name);
53 }
54 spin_unlock(&tcp_cong_list_lock);
55
56 return ret;
57}
58EXPORT_SYMBOL_GPL(tcp_register_congestion_control);
59
60/*
61 * Remove congestion control algorithm, called from
62 * the module's remove function. Module ref counts are used
63 * to ensure that this can't be done till all sockets using
64 * that method are closed.
65 */
66void tcp_unregister_congestion_control(struct tcp_congestion_ops *ca)
67{
68 spin_lock(&tcp_cong_list_lock);
69 list_del_rcu(&ca->list);
70 spin_unlock(&tcp_cong_list_lock);
71}
72EXPORT_SYMBOL_GPL(tcp_unregister_congestion_control);
73
74/* Assign choice of congestion control. */
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -030075void tcp_init_congestion_control(struct sock *sk)
Stephen Hemminger317a76f2005-06-23 12:19:55 -070076{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -030077 struct inet_connection_sock *icsk = inet_csk(sk);
Stephen Hemminger317a76f2005-06-23 12:19:55 -070078 struct tcp_congestion_ops *ca;
79
Stephen Hemminger4d4d3d12007-04-23 22:32:11 -070080 /* if no choice made yet assign the current value set as default */
81 if (icsk->icsk_ca_ops == &tcp_init_congestion_ops) {
82 rcu_read_lock();
83 list_for_each_entry_rcu(ca, &tcp_cong_list, list) {
84 if (try_module_get(ca->owner)) {
85 icsk->icsk_ca_ops = ca;
86 break;
87 }
Stephen Hemminger5f8ef482005-06-23 20:37:36 -070088
Stephen Hemminger4d4d3d12007-04-23 22:32:11 -070089 /* fallback to next available */
Stephen Hemminger317a76f2005-06-23 12:19:55 -070090 }
Stephen Hemminger4d4d3d12007-04-23 22:32:11 -070091 rcu_read_unlock();
Stephen Hemminger317a76f2005-06-23 12:19:55 -070092 }
Stephen Hemminger317a76f2005-06-23 12:19:55 -070093
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -030094 if (icsk->icsk_ca_ops->init)
95 icsk->icsk_ca_ops->init(sk);
Stephen Hemminger317a76f2005-06-23 12:19:55 -070096}
97
98/* Manage refcounts on socket close. */
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -030099void tcp_cleanup_congestion_control(struct sock *sk)
Stephen Hemminger317a76f2005-06-23 12:19:55 -0700100{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300101 struct inet_connection_sock *icsk = inet_csk(sk);
102
103 if (icsk->icsk_ca_ops->release)
104 icsk->icsk_ca_ops->release(sk);
105 module_put(icsk->icsk_ca_ops->owner);
Stephen Hemminger317a76f2005-06-23 12:19:55 -0700106}
107
108/* Used by sysctl to change default congestion control */
109int tcp_set_default_congestion_control(const char *name)
110{
111 struct tcp_congestion_ops *ca;
112 int ret = -ENOENT;
113
114 spin_lock(&tcp_cong_list_lock);
115 ca = tcp_ca_find(name);
116#ifdef CONFIG_KMOD
Stephen Hemminger35bfbc92006-11-09 16:36:36 -0800117 if (!ca && capable(CAP_SYS_MODULE)) {
Stephen Hemminger317a76f2005-06-23 12:19:55 -0700118 spin_unlock(&tcp_cong_list_lock);
119
120 request_module("tcp_%s", name);
121 spin_lock(&tcp_cong_list_lock);
122 ca = tcp_ca_find(name);
123 }
124#endif
125
126 if (ca) {
Stephen Hemmingerce7bc3b2006-11-09 16:35:15 -0800127 ca->non_restricted = 1; /* default is always allowed */
Stephen Hemminger317a76f2005-06-23 12:19:55 -0700128 list_move(&ca->list, &tcp_cong_list);
129 ret = 0;
130 }
131 spin_unlock(&tcp_cong_list_lock);
132
133 return ret;
134}
135
Stephen Hemmingerb1736a72006-10-31 17:31:33 -0800136/* Set default value from kernel configuration at bootup */
137static int __init tcp_congestion_default(void)
138{
139 return tcp_set_default_congestion_control(CONFIG_DEFAULT_TCP_CONG);
140}
141late_initcall(tcp_congestion_default);
142
143
Stephen Hemminger3ff825b2006-11-09 16:32:06 -0800144/* Build string with list of available congestion control values */
145void tcp_get_available_congestion_control(char *buf, size_t maxlen)
146{
147 struct tcp_congestion_ops *ca;
148 size_t offs = 0;
149
150 rcu_read_lock();
151 list_for_each_entry_rcu(ca, &tcp_cong_list, list) {
152 offs += snprintf(buf + offs, maxlen - offs,
153 "%s%s",
154 offs == 0 ? "" : " ", ca->name);
155
156 }
157 rcu_read_unlock();
158}
159
Stephen Hemminger317a76f2005-06-23 12:19:55 -0700160/* Get current default congestion control */
161void tcp_get_default_congestion_control(char *name)
162{
163 struct tcp_congestion_ops *ca;
164 /* We will always have reno... */
165 BUG_ON(list_empty(&tcp_cong_list));
166
167 rcu_read_lock();
168 ca = list_entry(tcp_cong_list.next, struct tcp_congestion_ops, list);
169 strncpy(name, ca->name, TCP_CA_NAME_MAX);
170 rcu_read_unlock();
171}
172
Stephen Hemmingerce7bc3b2006-11-09 16:35:15 -0800173/* Built list of non-restricted congestion control values */
174void tcp_get_allowed_congestion_control(char *buf, size_t maxlen)
175{
176 struct tcp_congestion_ops *ca;
177 size_t offs = 0;
178
179 *buf = '\0';
180 rcu_read_lock();
181 list_for_each_entry_rcu(ca, &tcp_cong_list, list) {
182 if (!ca->non_restricted)
183 continue;
184 offs += snprintf(buf + offs, maxlen - offs,
185 "%s%s",
186 offs == 0 ? "" : " ", ca->name);
187
188 }
189 rcu_read_unlock();
190}
191
192/* Change list of non-restricted congestion control */
193int tcp_set_allowed_congestion_control(char *val)
194{
195 struct tcp_congestion_ops *ca;
196 char *clone, *name;
197 int ret = 0;
198
199 clone = kstrdup(val, GFP_USER);
200 if (!clone)
201 return -ENOMEM;
202
203 spin_lock(&tcp_cong_list_lock);
204 /* pass 1 check for bad entries */
205 while ((name = strsep(&clone, " ")) && *name) {
206 ca = tcp_ca_find(name);
207 if (!ca) {
208 ret = -ENOENT;
209 goto out;
210 }
211 }
212
213 /* pass 2 clear */
214 list_for_each_entry_rcu(ca, &tcp_cong_list, list)
215 ca->non_restricted = 0;
216
217 /* pass 3 mark as allowed */
218 while ((name = strsep(&val, " ")) && *name) {
219 ca = tcp_ca_find(name);
220 WARN_ON(!ca);
221 if (ca)
222 ca->non_restricted = 1;
223 }
224out:
225 spin_unlock(&tcp_cong_list_lock);
226
227 return ret;
228}
229
230
Stephen Hemminger5f8ef482005-06-23 20:37:36 -0700231/* Change congestion control for socket */
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300232int tcp_set_congestion_control(struct sock *sk, const char *name)
Stephen Hemminger5f8ef482005-06-23 20:37:36 -0700233{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300234 struct inet_connection_sock *icsk = inet_csk(sk);
Stephen Hemminger5f8ef482005-06-23 20:37:36 -0700235 struct tcp_congestion_ops *ca;
236 int err = 0;
237
238 rcu_read_lock();
239 ca = tcp_ca_find(name);
Stephen Hemminger4d4d3d12007-04-23 22:32:11 -0700240
Stephen Hemminger35bfbc92006-11-09 16:36:36 -0800241 /* no change asking for existing value */
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300242 if (ca == icsk->icsk_ca_ops)
Stephen Hemminger5f8ef482005-06-23 20:37:36 -0700243 goto out;
244
Stephen Hemminger35bfbc92006-11-09 16:36:36 -0800245#ifdef CONFIG_KMOD
246 /* not found attempt to autoload module */
247 if (!ca && capable(CAP_SYS_MODULE)) {
248 rcu_read_unlock();
249 request_module("tcp_%s", name);
250 rcu_read_lock();
251 ca = tcp_ca_find(name);
252 }
253#endif
Stephen Hemminger5f8ef482005-06-23 20:37:36 -0700254 if (!ca)
255 err = -ENOENT;
256
Stephen Hemmingerce7bc3b2006-11-09 16:35:15 -0800257 else if (!(ca->non_restricted || capable(CAP_NET_ADMIN)))
258 err = -EPERM;
259
Stephen Hemminger5f8ef482005-06-23 20:37:36 -0700260 else if (!try_module_get(ca->owner))
261 err = -EBUSY;
262
263 else {
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300264 tcp_cleanup_congestion_control(sk);
265 icsk->icsk_ca_ops = ca;
Stephen Hemminger4d4d3d12007-04-23 22:32:11 -0700266
267 if (sk->sk_state != TCP_CLOSE && icsk->icsk_ca_ops->init)
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300268 icsk->icsk_ca_ops->init(sk);
Stephen Hemminger5f8ef482005-06-23 20:37:36 -0700269 }
270 out:
271 rcu_read_unlock();
272 return err;
273}
274
Stephen Hemminger40efc6f2006-01-03 16:03:49 -0800275
276/*
277 * Linear increase during slow start
278 */
279void tcp_slow_start(struct tcp_sock *tp)
280{
281 if (sysctl_tcp_abc) {
282 /* RFC3465: Slow Start
283 * TCP sender SHOULD increase cwnd by the number of
284 * previously unacknowledged bytes ACKed by each incoming
285 * acknowledgment, provided the increase is not more than L
286 */
287 if (tp->bytes_acked < tp->mss_cache)
288 return;
289
290 /* We MAY increase by 2 if discovered delayed ack */
Daikichi Osuga3fdf3f02006-08-29 02:01:44 -0700291 if (sysctl_tcp_abc > 1 && tp->bytes_acked >= 2*tp->mss_cache) {
Stephen Hemminger40efc6f2006-01-03 16:03:49 -0800292 if (tp->snd_cwnd < tp->snd_cwnd_clamp)
293 tp->snd_cwnd++;
294 }
295 }
296 tp->bytes_acked = 0;
297
298 if (tp->snd_cwnd < tp->snd_cwnd_clamp)
299 tp->snd_cwnd++;
300}
301EXPORT_SYMBOL_GPL(tcp_slow_start);
302
Stephen Hemminger317a76f2005-06-23 12:19:55 -0700303/*
304 * TCP Reno congestion control
305 * This is special case used for fallback as well.
306 */
307/* This is Jacobson's slow start and congestion avoidance.
308 * SIGCOMM '88, p. 328.
309 */
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300310void tcp_reno_cong_avoid(struct sock *sk, u32 ack, u32 rtt, u32 in_flight,
Stephen Hemminger317a76f2005-06-23 12:19:55 -0700311 int flag)
312{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300313 struct tcp_sock *tp = tcp_sk(sk);
314
Stephen Hemmingerf4805ed2005-11-10 16:53:30 -0800315 if (!tcp_is_cwnd_limited(sk, in_flight))
Stephen Hemminger317a76f2005-06-23 12:19:55 -0700316 return;
317
Stephen Hemminger7faffa12005-11-10 17:07:24 -0800318 /* In "safe" area, increase. */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900319 if (tp->snd_cwnd <= tp->snd_ssthresh)
Stephen Hemminger7faffa12005-11-10 17:07:24 -0800320 tcp_slow_start(tp);
Stephen Hemminger9772efb2005-11-10 17:09:53 -0800321
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900322 /* In dangerous area, increase slowly. */
Stephen Hemminger9772efb2005-11-10 17:09:53 -0800323 else if (sysctl_tcp_abc) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900324 /* RFC3465: Appropriate Byte Count
325 * increase once for each full cwnd acked
326 */
327 if (tp->bytes_acked >= tp->snd_cwnd*tp->mss_cache) {
328 tp->bytes_acked -= tp->snd_cwnd*tp->mss_cache;
329 if (tp->snd_cwnd < tp->snd_cwnd_clamp)
330 tp->snd_cwnd++;
331 }
332 } else {
333 /* In theory this is tp->snd_cwnd += 1 / tp->snd_cwnd */
334 if (tp->snd_cwnd_cnt >= tp->snd_cwnd) {
335 if (tp->snd_cwnd < tp->snd_cwnd_clamp)
336 tp->snd_cwnd++;
337 tp->snd_cwnd_cnt = 0;
338 } else
339 tp->snd_cwnd_cnt++;
340 }
Stephen Hemminger317a76f2005-06-23 12:19:55 -0700341}
342EXPORT_SYMBOL_GPL(tcp_reno_cong_avoid);
343
344/* Slow start threshold is half the congestion window (min 2) */
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300345u32 tcp_reno_ssthresh(struct sock *sk)
Stephen Hemminger317a76f2005-06-23 12:19:55 -0700346{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300347 const struct tcp_sock *tp = tcp_sk(sk);
Stephen Hemminger317a76f2005-06-23 12:19:55 -0700348 return max(tp->snd_cwnd >> 1U, 2U);
349}
350EXPORT_SYMBOL_GPL(tcp_reno_ssthresh);
351
Stephen Hemminger72dc5b92006-06-05 17:30:08 -0700352/* Lower bound on congestion window with halving. */
353u32 tcp_reno_min_cwnd(const struct sock *sk)
Stephen Hemminger317a76f2005-06-23 12:19:55 -0700354{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300355 const struct tcp_sock *tp = tcp_sk(sk);
Stephen Hemminger317a76f2005-06-23 12:19:55 -0700356 return tp->snd_ssthresh/2;
357}
358EXPORT_SYMBOL_GPL(tcp_reno_min_cwnd);
359
360struct tcp_congestion_ops tcp_reno = {
361 .name = "reno",
Stephen Hemmingerce7bc3b2006-11-09 16:35:15 -0800362 .non_restricted = 1,
Stephen Hemminger317a76f2005-06-23 12:19:55 -0700363 .owner = THIS_MODULE,
364 .ssthresh = tcp_reno_ssthresh,
365 .cong_avoid = tcp_reno_cong_avoid,
366 .min_cwnd = tcp_reno_min_cwnd,
367};
368
Stephen Hemminger5f8ef482005-06-23 20:37:36 -0700369/* Initial congestion control used (until SYN)
370 * really reno under another name so we can tell difference
371 * during tcp_set_default_congestion_control
372 */
373struct tcp_congestion_ops tcp_init_congestion_ops = {
374 .name = "",
375 .owner = THIS_MODULE,
376 .ssthresh = tcp_reno_ssthresh,
377 .cong_avoid = tcp_reno_cong_avoid,
378 .min_cwnd = tcp_reno_min_cwnd,
379};
380EXPORT_SYMBOL_GPL(tcp_init_congestion_ops);