]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - net/ipv4/tcp_cong.c
Merge master.kernel.org:/pub/scm/linux/kernel/git/sfrench/cifs-2.6
[mirror_ubuntu-artful-kernel.git] / net / ipv4 / tcp_cong.c
CommitLineData
317a76f9
SH
1/*
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
317a76f9
SH
9#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
5f8ef48d 23 list_for_each_entry_rcu(e, &tcp_cong_list, list) {
317a76f9
SH
24 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 */
72dc5b92 40 if (!ca->ssthresh || !ca->cong_avoid) {
317a76f9
SH
41 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 {
3d2573f7 51 list_add_tail_rcu(&ca->list, &tcp_cong_list);
317a76f9
SH
52 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. */
6687e988 75void tcp_init_congestion_control(struct sock *sk)
317a76f9 76{
6687e988 77 struct inet_connection_sock *icsk = inet_csk(sk);
317a76f9
SH
78 struct tcp_congestion_ops *ca;
79
6687e988 80 if (icsk->icsk_ca_ops != &tcp_init_congestion_ops)
5f8ef48d
SH
81 return;
82
317a76f9
SH
83 rcu_read_lock();
84 list_for_each_entry_rcu(ca, &tcp_cong_list, list) {
85 if (try_module_get(ca->owner)) {
6687e988 86 icsk->icsk_ca_ops = ca;
317a76f9
SH
87 break;
88 }
89
90 }
91 rcu_read_unlock();
92
6687e988
ACM
93 if (icsk->icsk_ca_ops->init)
94 icsk->icsk_ca_ops->init(sk);
317a76f9
SH
95}
96
97/* Manage refcounts on socket close. */
6687e988 98void tcp_cleanup_congestion_control(struct sock *sk)
317a76f9 99{
6687e988
ACM
100 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);
317a76f9
SH
105}
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
b1736a71
SH
134/* 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
317a76f9
SH
142/* Get current default congestion control */
143void tcp_get_default_congestion_control(char *name)
144{
145 struct tcp_congestion_ops *ca;
146 /* We will always have reno... */
147 BUG_ON(list_empty(&tcp_cong_list));
148
149 rcu_read_lock();
150 ca = list_entry(tcp_cong_list.next, struct tcp_congestion_ops, list);
151 strncpy(name, ca->name, TCP_CA_NAME_MAX);
152 rcu_read_unlock();
153}
154
5f8ef48d 155/* Change congestion control for socket */
6687e988 156int tcp_set_congestion_control(struct sock *sk, const char *name)
5f8ef48d 157{
6687e988 158 struct inet_connection_sock *icsk = inet_csk(sk);
5f8ef48d
SH
159 struct tcp_congestion_ops *ca;
160 int err = 0;
161
162 rcu_read_lock();
163 ca = tcp_ca_find(name);
6687e988 164 if (ca == icsk->icsk_ca_ops)
5f8ef48d
SH
165 goto out;
166
167 if (!ca)
168 err = -ENOENT;
169
170 else if (!try_module_get(ca->owner))
171 err = -EBUSY;
172
173 else {
6687e988
ACM
174 tcp_cleanup_congestion_control(sk);
175 icsk->icsk_ca_ops = ca;
176 if (icsk->icsk_ca_ops->init)
177 icsk->icsk_ca_ops->init(sk);
5f8ef48d
SH
178 }
179 out:
180 rcu_read_unlock();
181 return err;
182}
183
40efc6fa
SH
184
185/*
186 * Linear increase during slow start
187 */
188void tcp_slow_start(struct tcp_sock *tp)
189{
190 if (sysctl_tcp_abc) {
191 /* RFC3465: Slow Start
192 * TCP sender SHOULD increase cwnd by the number of
193 * previously unacknowledged bytes ACKed by each incoming
194 * acknowledgment, provided the increase is not more than L
195 */
196 if (tp->bytes_acked < tp->mss_cache)
197 return;
198
199 /* We MAY increase by 2 if discovered delayed ack */
3fdf3f0c 200 if (sysctl_tcp_abc > 1 && tp->bytes_acked >= 2*tp->mss_cache) {
40efc6fa
SH
201 if (tp->snd_cwnd < tp->snd_cwnd_clamp)
202 tp->snd_cwnd++;
203 }
204 }
205 tp->bytes_acked = 0;
206
207 if (tp->snd_cwnd < tp->snd_cwnd_clamp)
208 tp->snd_cwnd++;
209}
210EXPORT_SYMBOL_GPL(tcp_slow_start);
211
317a76f9
SH
212/*
213 * TCP Reno congestion control
214 * This is special case used for fallback as well.
215 */
216/* This is Jacobson's slow start and congestion avoidance.
217 * SIGCOMM '88, p. 328.
218 */
6687e988 219void tcp_reno_cong_avoid(struct sock *sk, u32 ack, u32 rtt, u32 in_flight,
317a76f9
SH
220 int flag)
221{
6687e988
ACM
222 struct tcp_sock *tp = tcp_sk(sk);
223
f4805ede 224 if (!tcp_is_cwnd_limited(sk, in_flight))
317a76f9
SH
225 return;
226
7faffa1c
SH
227 /* In "safe" area, increase. */
228 if (tp->snd_cwnd <= tp->snd_ssthresh)
229 tcp_slow_start(tp);
9772efb9
SH
230
231 /* In dangerous area, increase slowly. */
232 else if (sysctl_tcp_abc) {
c3e5d877 233 /* RFC3465: Appropriate Byte Count
9772efb9
SH
234 * increase once for each full cwnd acked
235 */
236 if (tp->bytes_acked >= tp->snd_cwnd*tp->mss_cache) {
237 tp->bytes_acked -= tp->snd_cwnd*tp->mss_cache;
238 if (tp->snd_cwnd < tp->snd_cwnd_clamp)
239 tp->snd_cwnd++;
240 }
241 } else {
242 /* In theory this is tp->snd_cwnd += 1 / tp->snd_cwnd */
243 if (tp->snd_cwnd_cnt >= tp->snd_cwnd) {
244 if (tp->snd_cwnd < tp->snd_cwnd_clamp)
245 tp->snd_cwnd++;
246 tp->snd_cwnd_cnt = 0;
247 } else
248 tp->snd_cwnd_cnt++;
249 }
317a76f9
SH
250}
251EXPORT_SYMBOL_GPL(tcp_reno_cong_avoid);
252
253/* Slow start threshold is half the congestion window (min 2) */
6687e988 254u32 tcp_reno_ssthresh(struct sock *sk)
317a76f9 255{
6687e988 256 const struct tcp_sock *tp = tcp_sk(sk);
317a76f9
SH
257 return max(tp->snd_cwnd >> 1U, 2U);
258}
259EXPORT_SYMBOL_GPL(tcp_reno_ssthresh);
260
72dc5b92
SH
261/* Lower bound on congestion window with halving. */
262u32 tcp_reno_min_cwnd(const struct sock *sk)
317a76f9 263{
6687e988 264 const struct tcp_sock *tp = tcp_sk(sk);
317a76f9
SH
265 return tp->snd_ssthresh/2;
266}
267EXPORT_SYMBOL_GPL(tcp_reno_min_cwnd);
268
269struct tcp_congestion_ops tcp_reno = {
270 .name = "reno",
271 .owner = THIS_MODULE,
272 .ssthresh = tcp_reno_ssthresh,
273 .cong_avoid = tcp_reno_cong_avoid,
274 .min_cwnd = tcp_reno_min_cwnd,
275};
276
5f8ef48d
SH
277/* Initial congestion control used (until SYN)
278 * really reno under another name so we can tell difference
279 * during tcp_set_default_congestion_control
280 */
281struct tcp_congestion_ops tcp_init_congestion_ops = {
282 .name = "",
283 .owner = THIS_MODULE,
284 .ssthresh = tcp_reno_ssthresh,
285 .cong_avoid = tcp_reno_cong_avoid,
286 .min_cwnd = tcp_reno_min_cwnd,
287};
288EXPORT_SYMBOL_GPL(tcp_init_congestion_ops);