blob: d846d7b95e1f6cafc893349b098fcfffa3a4104c [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/*
32 * Attach new congestion control algorthim to the list
33 * 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
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -030080 if (icsk->icsk_ca_ops != &tcp_init_congestion_ops)
Stephen Hemminger5f8ef482005-06-23 20:37:36 -070081 return;
82
Stephen Hemminger317a76f2005-06-23 12:19:55 -070083 rcu_read_lock();
84 list_for_each_entry_rcu(ca, &tcp_cong_list, list) {
85 if (try_module_get(ca->owner)) {
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -030086 icsk->icsk_ca_ops = ca;
Stephen Hemminger317a76f2005-06-23 12:19:55 -070087 break;
88 }
89
90 }
91 rcu_read_unlock();
92
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -030093 if (icsk->icsk_ca_ops->init)
94 icsk->icsk_ca_ops->init(sk);
Stephen Hemminger317a76f2005-06-23 12:19:55 -070095}
96
97/* Manage refcounts on socket close. */
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -030098void tcp_cleanup_congestion_control(struct sock *sk)
Stephen Hemminger317a76f2005-06-23 12:19:55 -070099{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300100 struct inet_connection_sock *icsk = inet_csk(sk);
101
102 if (icsk->icsk_ca_ops->release)
103 icsk->icsk_ca_ops->release(sk);
104 module_put(icsk->icsk_ca_ops->owner);
Stephen Hemminger317a76f2005-06-23 12:19:55 -0700105}
106
107/* Used by sysctl to change default congestion control */
108int tcp_set_default_congestion_control(const char *name)
109{
110 struct tcp_congestion_ops *ca;
111 int ret = -ENOENT;
112
113 spin_lock(&tcp_cong_list_lock);
114 ca = tcp_ca_find(name);
115#ifdef CONFIG_KMOD
116 if (!ca) {
117 spin_unlock(&tcp_cong_list_lock);
118
119 request_module("tcp_%s", name);
120 spin_lock(&tcp_cong_list_lock);
121 ca = tcp_ca_find(name);
122 }
123#endif
124
125 if (ca) {
126 list_move(&ca->list, &tcp_cong_list);
127 ret = 0;
128 }
129 spin_unlock(&tcp_cong_list_lock);
130
131 return ret;
132}
133
Stephen Hemmingerb1736a72006-10-31 17:31:33 -0800134/* Set default value from kernel configuration at bootup */
135static int __init tcp_congestion_default(void)
136{
137 return tcp_set_default_congestion_control(CONFIG_DEFAULT_TCP_CONG);
138}
139late_initcall(tcp_congestion_default);
140
141
Stephen Hemminger3ff825b2006-11-09 16:32:06 -0800142/* Build string with list of available congestion control values */
143void tcp_get_available_congestion_control(char *buf, size_t maxlen)
144{
145 struct tcp_congestion_ops *ca;
146 size_t offs = 0;
147
148 rcu_read_lock();
149 list_for_each_entry_rcu(ca, &tcp_cong_list, list) {
150 offs += snprintf(buf + offs, maxlen - offs,
151 "%s%s",
152 offs == 0 ? "" : " ", ca->name);
153
154 }
155 rcu_read_unlock();
156}
157
Stephen Hemminger317a76f2005-06-23 12:19:55 -0700158/* Get current default congestion control */
159void tcp_get_default_congestion_control(char *name)
160{
161 struct tcp_congestion_ops *ca;
162 /* We will always have reno... */
163 BUG_ON(list_empty(&tcp_cong_list));
164
165 rcu_read_lock();
166 ca = list_entry(tcp_cong_list.next, struct tcp_congestion_ops, list);
167 strncpy(name, ca->name, TCP_CA_NAME_MAX);
168 rcu_read_unlock();
169}
170
Stephen Hemminger5f8ef482005-06-23 20:37:36 -0700171/* Change congestion control for socket */
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300172int tcp_set_congestion_control(struct sock *sk, const char *name)
Stephen Hemminger5f8ef482005-06-23 20:37:36 -0700173{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300174 struct inet_connection_sock *icsk = inet_csk(sk);
Stephen Hemminger5f8ef482005-06-23 20:37:36 -0700175 struct tcp_congestion_ops *ca;
176 int err = 0;
177
178 rcu_read_lock();
179 ca = tcp_ca_find(name);
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300180 if (ca == icsk->icsk_ca_ops)
Stephen Hemminger5f8ef482005-06-23 20:37:36 -0700181 goto out;
182
183 if (!ca)
184 err = -ENOENT;
185
186 else if (!try_module_get(ca->owner))
187 err = -EBUSY;
188
189 else {
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300190 tcp_cleanup_congestion_control(sk);
191 icsk->icsk_ca_ops = ca;
192 if (icsk->icsk_ca_ops->init)
193 icsk->icsk_ca_ops->init(sk);
Stephen Hemminger5f8ef482005-06-23 20:37:36 -0700194 }
195 out:
196 rcu_read_unlock();
197 return err;
198}
199
Stephen Hemminger40efc6f2006-01-03 16:03:49 -0800200
201/*
202 * Linear increase during slow start
203 */
204void tcp_slow_start(struct tcp_sock *tp)
205{
206 if (sysctl_tcp_abc) {
207 /* RFC3465: Slow Start
208 * TCP sender SHOULD increase cwnd by the number of
209 * previously unacknowledged bytes ACKed by each incoming
210 * acknowledgment, provided the increase is not more than L
211 */
212 if (tp->bytes_acked < tp->mss_cache)
213 return;
214
215 /* We MAY increase by 2 if discovered delayed ack */
Daikichi Osuga3fdf3f02006-08-29 02:01:44 -0700216 if (sysctl_tcp_abc > 1 && tp->bytes_acked >= 2*tp->mss_cache) {
Stephen Hemminger40efc6f2006-01-03 16:03:49 -0800217 if (tp->snd_cwnd < tp->snd_cwnd_clamp)
218 tp->snd_cwnd++;
219 }
220 }
221 tp->bytes_acked = 0;
222
223 if (tp->snd_cwnd < tp->snd_cwnd_clamp)
224 tp->snd_cwnd++;
225}
226EXPORT_SYMBOL_GPL(tcp_slow_start);
227
Stephen Hemminger317a76f2005-06-23 12:19:55 -0700228/*
229 * TCP Reno congestion control
230 * This is special case used for fallback as well.
231 */
232/* This is Jacobson's slow start and congestion avoidance.
233 * SIGCOMM '88, p. 328.
234 */
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300235void tcp_reno_cong_avoid(struct sock *sk, u32 ack, u32 rtt, u32 in_flight,
Stephen Hemminger317a76f2005-06-23 12:19:55 -0700236 int flag)
237{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300238 struct tcp_sock *tp = tcp_sk(sk);
239
Stephen Hemmingerf4805ed2005-11-10 16:53:30 -0800240 if (!tcp_is_cwnd_limited(sk, in_flight))
Stephen Hemminger317a76f2005-06-23 12:19:55 -0700241 return;
242
Stephen Hemminger7faffa12005-11-10 17:07:24 -0800243 /* In "safe" area, increase. */
244 if (tp->snd_cwnd <= tp->snd_ssthresh)
245 tcp_slow_start(tp);
Stephen Hemminger9772efb2005-11-10 17:09:53 -0800246
247 /* In dangerous area, increase slowly. */
248 else if (sysctl_tcp_abc) {
S Pc3e5d872006-03-28 16:35:46 -0800249 /* RFC3465: Appropriate Byte Count
Stephen Hemminger9772efb2005-11-10 17:09:53 -0800250 * increase once for each full cwnd acked
251 */
252 if (tp->bytes_acked >= tp->snd_cwnd*tp->mss_cache) {
253 tp->bytes_acked -= tp->snd_cwnd*tp->mss_cache;
254 if (tp->snd_cwnd < tp->snd_cwnd_clamp)
255 tp->snd_cwnd++;
256 }
257 } else {
258 /* In theory this is tp->snd_cwnd += 1 / tp->snd_cwnd */
259 if (tp->snd_cwnd_cnt >= tp->snd_cwnd) {
260 if (tp->snd_cwnd < tp->snd_cwnd_clamp)
261 tp->snd_cwnd++;
262 tp->snd_cwnd_cnt = 0;
263 } else
264 tp->snd_cwnd_cnt++;
265 }
Stephen Hemminger317a76f2005-06-23 12:19:55 -0700266}
267EXPORT_SYMBOL_GPL(tcp_reno_cong_avoid);
268
269/* Slow start threshold is half the congestion window (min 2) */
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300270u32 tcp_reno_ssthresh(struct sock *sk)
Stephen Hemminger317a76f2005-06-23 12:19:55 -0700271{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300272 const struct tcp_sock *tp = tcp_sk(sk);
Stephen Hemminger317a76f2005-06-23 12:19:55 -0700273 return max(tp->snd_cwnd >> 1U, 2U);
274}
275EXPORT_SYMBOL_GPL(tcp_reno_ssthresh);
276
Stephen Hemminger72dc5b92006-06-05 17:30:08 -0700277/* Lower bound on congestion window with halving. */
278u32 tcp_reno_min_cwnd(const struct sock *sk)
Stephen Hemminger317a76f2005-06-23 12:19:55 -0700279{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300280 const struct tcp_sock *tp = tcp_sk(sk);
Stephen Hemminger317a76f2005-06-23 12:19:55 -0700281 return tp->snd_ssthresh/2;
282}
283EXPORT_SYMBOL_GPL(tcp_reno_min_cwnd);
284
285struct tcp_congestion_ops tcp_reno = {
286 .name = "reno",
287 .owner = THIS_MODULE,
288 .ssthresh = tcp_reno_ssthresh,
289 .cong_avoid = tcp_reno_cong_avoid,
290 .min_cwnd = tcp_reno_min_cwnd,
291};
292
Stephen Hemminger5f8ef482005-06-23 20:37:36 -0700293/* Initial congestion control used (until SYN)
294 * really reno under another name so we can tell difference
295 * during tcp_set_default_congestion_control
296 */
297struct tcp_congestion_ops tcp_init_congestion_ops = {
298 .name = "",
299 .owner = THIS_MODULE,
300 .ssthresh = tcp_reno_ssthresh,
301 .cong_avoid = tcp_reno_cong_avoid,
302 .min_cwnd = tcp_reno_min_cwnd,
303};
304EXPORT_SYMBOL_GPL(tcp_init_congestion_ops);