2 * Plugable TCP congestion control support and newReno
4 * Based on ideas from I/O scheduler suport and Web100.
6 * Copyright (C) 2005 Stephen Hemminger <shemminger@osdl.org>
9 #include <linux/config.h>
10 #include <linux/module.h>
12 #include <linux/types.h>
13 #include <linux/list.h>
16 static DEFINE_SPINLOCK(tcp_cong_list_lock
);
17 static LIST_HEAD(tcp_cong_list
);
19 /* Simple linear search, don't expect many entries! */
20 static struct tcp_congestion_ops
*tcp_ca_find(const char *name
)
22 struct tcp_congestion_ops
*e
;
24 list_for_each_entry_rcu(e
, &tcp_cong_list
, list
) {
25 if (strcmp(e
->name
, name
) == 0)
33 * Attach new congestion control algorthim to the list
34 * of available options.
36 int tcp_register_congestion_control(struct tcp_congestion_ops
*ca
)
40 /* all algorithms must implement ssthresh and cong_avoid ops */
41 if (!ca
->ssthresh
|| !ca
->cong_avoid
|| !ca
->min_cwnd
) {
42 printk(KERN_ERR
"TCP %s does not implement required ops\n",
47 spin_lock(&tcp_cong_list_lock
);
48 if (tcp_ca_find(ca
->name
)) {
49 printk(KERN_NOTICE
"TCP %s already registered\n", ca
->name
);
52 list_add_rcu(&ca
->list
, &tcp_cong_list
);
53 printk(KERN_INFO
"TCP %s registered\n", ca
->name
);
55 spin_unlock(&tcp_cong_list_lock
);
59 EXPORT_SYMBOL_GPL(tcp_register_congestion_control
);
62 * Remove congestion control algorithm, called from
63 * the module's remove function. Module ref counts are used
64 * to ensure that this can't be done till all sockets using
65 * that method are closed.
67 void tcp_unregister_congestion_control(struct tcp_congestion_ops
*ca
)
69 spin_lock(&tcp_cong_list_lock
);
70 list_del_rcu(&ca
->list
);
71 spin_unlock(&tcp_cong_list_lock
);
73 EXPORT_SYMBOL_GPL(tcp_unregister_congestion_control
);
75 /* Assign choice of congestion control. */
76 void tcp_init_congestion_control(struct tcp_sock
*tp
)
78 struct tcp_congestion_ops
*ca
;
80 if (tp
->ca_ops
!= &tcp_init_congestion_ops
)
84 list_for_each_entry_rcu(ca
, &tcp_cong_list
, list
) {
85 if (try_module_get(ca
->owner
)) {
97 /* Manage refcounts on socket close. */
98 void tcp_cleanup_congestion_control(struct tcp_sock
*tp
)
100 if (tp
->ca_ops
->release
)
101 tp
->ca_ops
->release(tp
);
102 module_put(tp
->ca_ops
->owner
);
105 /* Used by sysctl to change default congestion control */
106 int tcp_set_default_congestion_control(const char *name
)
108 struct tcp_congestion_ops
*ca
;
111 spin_lock(&tcp_cong_list_lock
);
112 ca
= tcp_ca_find(name
);
115 spin_unlock(&tcp_cong_list_lock
);
117 request_module("tcp_%s", name
);
118 spin_lock(&tcp_cong_list_lock
);
119 ca
= tcp_ca_find(name
);
124 list_move(&ca
->list
, &tcp_cong_list
);
127 spin_unlock(&tcp_cong_list_lock
);
132 /* Get current default congestion control */
133 void tcp_get_default_congestion_control(char *name
)
135 struct tcp_congestion_ops
*ca
;
136 /* We will always have reno... */
137 BUG_ON(list_empty(&tcp_cong_list
));
140 ca
= list_entry(tcp_cong_list
.next
, struct tcp_congestion_ops
, list
);
141 strncpy(name
, ca
->name
, TCP_CA_NAME_MAX
);
145 /* Change congestion control for socket */
146 int tcp_set_congestion_control(struct tcp_sock
*tp
, const char *name
)
148 struct tcp_congestion_ops
*ca
;
152 ca
= tcp_ca_find(name
);
153 if (ca
== tp
->ca_ops
)
159 else if (!try_module_get(ca
->owner
))
163 tcp_cleanup_congestion_control(tp
);
165 if (tp
->ca_ops
->init
)
166 tp
->ca_ops
->init(tp
);
174 * TCP Reno congestion control
175 * This is special case used for fallback as well.
177 /* This is Jacobson's slow start and congestion avoidance.
178 * SIGCOMM '88, p. 328.
180 void tcp_reno_cong_avoid(struct tcp_sock
*tp
, u32 ack
, u32 rtt
, u32 in_flight
,
183 if (in_flight
< tp
->snd_cwnd
)
186 if (tp
->snd_cwnd
<= tp
->snd_ssthresh
) {
187 /* In "safe" area, increase. */
188 if (tp
->snd_cwnd
< tp
->snd_cwnd_clamp
)
191 /* In dangerous area, increase slowly.
192 * In theory this is tp->snd_cwnd += 1 / tp->snd_cwnd
194 if (tp
->snd_cwnd_cnt
>= tp
->snd_cwnd
) {
195 if (tp
->snd_cwnd
< tp
->snd_cwnd_clamp
)
197 tp
->snd_cwnd_cnt
= 0;
202 EXPORT_SYMBOL_GPL(tcp_reno_cong_avoid
);
204 /* Slow start threshold is half the congestion window (min 2) */
205 u32
tcp_reno_ssthresh(struct tcp_sock
*tp
)
207 return max(tp
->snd_cwnd
>> 1U, 2U);
209 EXPORT_SYMBOL_GPL(tcp_reno_ssthresh
);
211 /* Lower bound on congestion window. */
212 u32
tcp_reno_min_cwnd(struct tcp_sock
*tp
)
214 return tp
->snd_ssthresh
/2;
216 EXPORT_SYMBOL_GPL(tcp_reno_min_cwnd
);
218 struct tcp_congestion_ops tcp_reno
= {
220 .owner
= THIS_MODULE
,
221 .ssthresh
= tcp_reno_ssthresh
,
222 .cong_avoid
= tcp_reno_cong_avoid
,
223 .min_cwnd
= tcp_reno_min_cwnd
,
226 /* Initial congestion control used (until SYN)
227 * really reno under another name so we can tell difference
228 * during tcp_set_default_congestion_control
230 struct tcp_congestion_ops tcp_init_congestion_ops
= {
232 .owner
= THIS_MODULE
,
233 .ssthresh
= tcp_reno_ssthresh
,
234 .cong_avoid
= tcp_reno_cong_avoid
,
235 .min_cwnd
= tcp_reno_min_cwnd
,
237 EXPORT_SYMBOL_GPL(tcp_init_congestion_ops
);