[TCP]: Add pluggable congestion control algorithm infrastructure.
[pandora-kernel.git] / net / ipv4 / tcp_cong.c
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
9 #include <linux/config.h>
10 #include <linux/module.h>
11 #include <linux/mm.h>
12 #include <linux/types.h>
13 #include <linux/list.h>
14 #include <net/tcp.h>
15
16 static DEFINE_SPINLOCK(tcp_cong_list_lock);
17 static LIST_HEAD(tcp_cong_list);
18
19 /* Simple linear search, don't expect many entries! */
20 static struct tcp_congestion_ops *tcp_ca_find(const char *name)
21 {
22         struct tcp_congestion_ops *e;
23
24         list_for_each_entry(e, &tcp_cong_list, list) {
25                 if (strcmp(e->name, name) == 0)
26                         return e;
27         }
28
29         return NULL;
30 }
31
32 /*
33  * Attach new congestion control algorthim to the list
34  * of available options.
35  */
36 int tcp_register_congestion_control(struct tcp_congestion_ops *ca)
37 {
38         int ret = 0;
39
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",
43                        ca->name);
44                 return -EINVAL;
45         }
46
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);
50                 ret = -EEXIST;
51         } else {
52                 list_add_rcu(&ca->list, &tcp_cong_list);
53                 printk(KERN_INFO "TCP %s registered\n", ca->name);
54         }
55         spin_unlock(&tcp_cong_list_lock);
56
57         return ret;
58 }
59 EXPORT_SYMBOL_GPL(tcp_register_congestion_control);
60
61 /*
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.
66  */
67 void tcp_unregister_congestion_control(struct tcp_congestion_ops *ca)
68 {
69         spin_lock(&tcp_cong_list_lock);
70         list_del_rcu(&ca->list);
71         spin_unlock(&tcp_cong_list_lock);
72 }
73 EXPORT_SYMBOL_GPL(tcp_unregister_congestion_control);
74
75 /* Assign choice of congestion control. */
76 void tcp_init_congestion_control(struct tcp_sock *tp)
77 {
78         struct tcp_congestion_ops *ca;
79
80         rcu_read_lock();
81         list_for_each_entry_rcu(ca, &tcp_cong_list, list) {
82                 if (try_module_get(ca->owner)) {
83                         tp->ca_ops = ca;
84                         break;
85                 }
86
87         }
88         rcu_read_unlock();
89
90         if (tp->ca_ops->init)
91                 tp->ca_ops->init(tp);
92 }
93
94 /* Manage refcounts on socket close. */
95 void tcp_cleanup_congestion_control(struct tcp_sock *tp)
96 {
97         if (tp->ca_ops->release)
98                 tp->ca_ops->release(tp);
99         module_put(tp->ca_ops->owner);
100 }
101
102 /* Used by sysctl to change default congestion control */
103 int tcp_set_default_congestion_control(const char *name)
104 {
105         struct tcp_congestion_ops *ca;
106         int ret = -ENOENT;
107
108         spin_lock(&tcp_cong_list_lock);
109         ca = tcp_ca_find(name);
110 #ifdef CONFIG_KMOD
111         if (!ca) {
112                 spin_unlock(&tcp_cong_list_lock);
113
114                 request_module("tcp_%s", name);
115                 spin_lock(&tcp_cong_list_lock);
116                 ca = tcp_ca_find(name);
117         }
118 #endif
119
120         if (ca) {
121                 list_move(&ca->list, &tcp_cong_list);
122                 ret = 0;
123         }
124         spin_unlock(&tcp_cong_list_lock);
125
126         return ret;
127 }
128
129 /* Get current default congestion control */
130 void tcp_get_default_congestion_control(char *name)
131 {
132         struct tcp_congestion_ops *ca;
133         /* We will always have reno... */
134         BUG_ON(list_empty(&tcp_cong_list));
135
136         rcu_read_lock();
137         ca = list_entry(tcp_cong_list.next, struct tcp_congestion_ops, list);
138         strncpy(name, ca->name, TCP_CA_NAME_MAX);
139         rcu_read_unlock();
140 }
141
142 /*
143  * TCP Reno congestion control
144  * This is special case used for fallback as well.
145  */
146 /* This is Jacobson's slow start and congestion avoidance.
147  * SIGCOMM '88, p. 328.
148  */
149 void tcp_reno_cong_avoid(struct tcp_sock *tp, u32 ack, u32 rtt, u32 in_flight,
150                          int flag)
151 {
152         if (in_flight < tp->snd_cwnd)
153                 return;
154
155         if (tp->snd_cwnd <= tp->snd_ssthresh) {
156                 /* In "safe" area, increase. */
157                 if (tp->snd_cwnd < tp->snd_cwnd_clamp)
158                         tp->snd_cwnd++;
159         } else {
160                 /* In dangerous area, increase slowly.
161                  * In theory this is tp->snd_cwnd += 1 / tp->snd_cwnd
162                  */
163                 if (tp->snd_cwnd_cnt >= tp->snd_cwnd) {
164                         if (tp->snd_cwnd < tp->snd_cwnd_clamp)
165                                 tp->snd_cwnd++;
166                         tp->snd_cwnd_cnt = 0;
167                 } else
168                         tp->snd_cwnd_cnt++;
169         }
170 }
171 EXPORT_SYMBOL_GPL(tcp_reno_cong_avoid);
172
173 /* Slow start threshold is half the congestion window (min 2) */
174 u32 tcp_reno_ssthresh(struct tcp_sock *tp)
175 {
176         return max(tp->snd_cwnd >> 1U, 2U);
177 }
178 EXPORT_SYMBOL_GPL(tcp_reno_ssthresh);
179
180 /* Lower bound on congestion window. */
181 u32 tcp_reno_min_cwnd(struct tcp_sock *tp)
182 {
183         return tp->snd_ssthresh/2;
184 }
185 EXPORT_SYMBOL_GPL(tcp_reno_min_cwnd);
186
187 struct tcp_congestion_ops tcp_reno = {
188         .name           = "reno",
189         .owner          = THIS_MODULE,
190         .ssthresh       = tcp_reno_ssthresh,
191         .cong_avoid     = tcp_reno_cong_avoid,
192         .min_cwnd       = tcp_reno_min_cwnd,
193 };
194
195 EXPORT_SYMBOL_GPL(tcp_reno);