[PATCH] delete from_swap_cache BUG_ONs
[linux-ginger.git] / net / ipv4 / tcp_bic.c
blobb940346de4e7cbc5bc724245992c07f6c0e41120
1 /*
2 * Binary Increase Congestion control for TCP
4 * This is from the implementation of BICTCP in
5 * Lison-Xu, Kahaled Harfoush, and Injong Rhee.
6 * "Binary Increase Congestion Control for Fast, Long Distance
7 * Networks" in InfoComm 2004
8 * Available from:
9 * http://www.csc.ncsu.edu/faculty/rhee/export/bitcp.pdf
11 * Unless BIC is enabled and congestion window is large
12 * this behaves the same as the original Reno.
15 #include <linux/config.h>
16 #include <linux/mm.h>
17 #include <linux/module.h>
18 #include <net/tcp.h>
21 #define BICTCP_BETA_SCALE 1024 /* Scale factor beta calculation
22 * max_cwnd = snd_cwnd * beta
24 #define BICTCP_B 4 /*
25 * In binary search,
26 * go to point (max+min)/N
29 static int fast_convergence = 1;
30 static int max_increment = 32;
31 static int low_window = 14;
32 static int beta = 819; /* = 819/1024 (BICTCP_BETA_SCALE) */
33 static int low_utilization_threshold = 153;
34 static int low_utilization_period = 2;
35 static int initial_ssthresh = 100;
36 static int smooth_part = 20;
38 module_param(fast_convergence, int, 0644);
39 MODULE_PARM_DESC(fast_convergence, "turn on/off fast convergence");
40 module_param(max_increment, int, 0644);
41 MODULE_PARM_DESC(max_increment, "Limit on increment allowed during binary search");
42 module_param(low_window, int, 0644);
43 MODULE_PARM_DESC(low_window, "lower bound on congestion window (for TCP friendliness)");
44 module_param(beta, int, 0644);
45 MODULE_PARM_DESC(beta, "beta for multiplicative increase");
46 module_param(low_utilization_threshold, int, 0644);
47 MODULE_PARM_DESC(low_utilization_threshold, "percent (scaled by 1024) for low utilization mode");
48 module_param(low_utilization_period, int, 0644);
49 MODULE_PARM_DESC(low_utilization_period, "if average delay exceeds then goto to low utilization mode (seconds)");
50 module_param(initial_ssthresh, int, 0644);
51 MODULE_PARM_DESC(initial_ssthresh, "initial value of slow start threshold");
52 module_param(smooth_part, int, 0644);
53 MODULE_PARM_DESC(smooth_part, "log(B/(B*Smin))/log(B/(B-1))+B, # of RTT from Wmax-B to Wmax");
56 /* BIC TCP Parameters */
57 struct bictcp {
58 u32 cnt; /* increase cwnd by 1 after ACKs */
59 u32 last_max_cwnd; /* last maximum snd_cwnd */
60 u32 loss_cwnd; /* congestion window at last loss */
61 u32 last_cwnd; /* the last snd_cwnd */
62 u32 last_time; /* time when updated last_cwnd */
63 u32 delay_min; /* min delay */
64 u32 delay_max; /* max delay */
65 u32 last_delay;
66 u8 low_utilization;/* 0: high; 1: low */
67 u32 low_utilization_start; /* starting time of low utilization detection*/
68 u32 epoch_start; /* beginning of an epoch */
69 #define ACK_RATIO_SHIFT 4
70 u32 delayed_ack; /* estimate the ratio of Packets/ACKs << 4 */
73 static inline void bictcp_reset(struct bictcp *ca)
75 ca->cnt = 0;
76 ca->last_max_cwnd = 0;
77 ca->loss_cwnd = 0;
78 ca->last_cwnd = 0;
79 ca->last_time = 0;
80 ca->delay_min = 0;
81 ca->delay_max = 0;
82 ca->last_delay = 0;
83 ca->low_utilization = 0;
84 ca->low_utilization_start = 0;
85 ca->epoch_start = 0;
86 ca->delayed_ack = 2 << ACK_RATIO_SHIFT;
89 static void bictcp_init(struct sock *sk)
91 bictcp_reset(inet_csk_ca(sk));
92 if (initial_ssthresh)
93 tcp_sk(sk)->snd_ssthresh = initial_ssthresh;
97 * Compute congestion window to use.
99 static inline void bictcp_update(struct bictcp *ca, u32 cwnd)
101 if (ca->last_cwnd == cwnd &&
102 (s32)(tcp_time_stamp - ca->last_time) <= HZ / 32)
103 return;
105 ca->last_cwnd = cwnd;
106 ca->last_time = tcp_time_stamp;
108 if (ca->epoch_start == 0) /* record the beginning of an epoch */
109 ca->epoch_start = tcp_time_stamp;
111 /* start off normal */
112 if (cwnd <= low_window) {
113 ca->cnt = cwnd;
114 return;
117 /* binary increase */
118 if (cwnd < ca->last_max_cwnd) {
119 __u32 dist = (ca->last_max_cwnd - cwnd)
120 / BICTCP_B;
122 if (dist > max_increment)
123 /* linear increase */
124 ca->cnt = cwnd / max_increment;
125 else if (dist <= 1U)
126 /* binary search increase */
127 ca->cnt = (cwnd * smooth_part) / BICTCP_B;
128 else
129 /* binary search increase */
130 ca->cnt = cwnd / dist;
131 } else {
132 /* slow start AMD linear increase */
133 if (cwnd < ca->last_max_cwnd + BICTCP_B)
134 /* slow start */
135 ca->cnt = (cwnd * smooth_part) / BICTCP_B;
136 else if (cwnd < ca->last_max_cwnd + max_increment*(BICTCP_B-1))
137 /* slow start */
138 ca->cnt = (cwnd * (BICTCP_B-1))
139 / cwnd-ca->last_max_cwnd;
140 else
141 /* linear increase */
142 ca->cnt = cwnd / max_increment;
145 /* if in slow start or link utilization is very low */
146 if ( ca->loss_cwnd == 0 ||
147 (cwnd > ca->loss_cwnd && ca->low_utilization)) {
148 if (ca->cnt > 20) /* increase cwnd 5% per RTT */
149 ca->cnt = 20;
152 ca->cnt = (ca->cnt << ACK_RATIO_SHIFT) / ca->delayed_ack;
153 if (ca->cnt == 0) /* cannot be zero */
154 ca->cnt = 1;
158 /* Detect low utilization in congestion avoidance */
159 static inline void bictcp_low_utilization(struct sock *sk, int flag)
161 const struct tcp_sock *tp = tcp_sk(sk);
162 struct bictcp *ca = inet_csk_ca(sk);
163 u32 dist, delay;
165 /* No time stamp */
166 if (!(tp->rx_opt.saw_tstamp && tp->rx_opt.rcv_tsecr) ||
167 /* Discard delay samples right after fast recovery */
168 tcp_time_stamp < ca->epoch_start + HZ ||
169 /* this delay samples may not be accurate */
170 flag == 0) {
171 ca->last_delay = 0;
172 goto notlow;
175 delay = ca->last_delay<<3; /* use the same scale as tp->srtt*/
176 ca->last_delay = tcp_time_stamp - tp->rx_opt.rcv_tsecr;
177 if (delay == 0) /* no previous delay sample */
178 goto notlow;
180 /* first time call or link delay decreases */
181 if (ca->delay_min == 0 || ca->delay_min > delay) {
182 ca->delay_min = ca->delay_max = delay;
183 goto notlow;
186 if (ca->delay_max < delay)
187 ca->delay_max = delay;
189 /* utilization is low, if avg delay < dist*threshold
190 for checking_period time */
191 dist = ca->delay_max - ca->delay_min;
192 if (dist <= ca->delay_min>>6 ||
193 tp->srtt - ca->delay_min >= (dist*low_utilization_threshold)>>10)
194 goto notlow;
196 if (ca->low_utilization_start == 0) {
197 ca->low_utilization = 0;
198 ca->low_utilization_start = tcp_time_stamp;
199 } else if ((s32)(tcp_time_stamp - ca->low_utilization_start)
200 > low_utilization_period*HZ) {
201 ca->low_utilization = 1;
204 return;
206 notlow:
207 ca->low_utilization = 0;
208 ca->low_utilization_start = 0;
212 static void bictcp_cong_avoid(struct sock *sk, u32 ack,
213 u32 seq_rtt, u32 in_flight, int data_acked)
215 struct tcp_sock *tp = tcp_sk(sk);
216 struct bictcp *ca = inet_csk_ca(sk);
218 bictcp_low_utilization(sk, data_acked);
220 if (in_flight < tp->snd_cwnd)
221 return;
223 if (tp->snd_cwnd <= tp->snd_ssthresh) {
224 /* In "safe" area, increase. */
225 if (tp->snd_cwnd < tp->snd_cwnd_clamp)
226 tp->snd_cwnd++;
227 } else {
228 bictcp_update(ca, tp->snd_cwnd);
230 /* In dangerous area, increase slowly.
231 * In theory this is tp->snd_cwnd += 1 / tp->snd_cwnd
233 if (tp->snd_cwnd_cnt >= ca->cnt) {
234 if (tp->snd_cwnd < tp->snd_cwnd_clamp)
235 tp->snd_cwnd++;
236 tp->snd_cwnd_cnt = 0;
237 } else
238 tp->snd_cwnd_cnt++;
244 * behave like Reno until low_window is reached,
245 * then increase congestion window slowly
247 static u32 bictcp_recalc_ssthresh(struct sock *sk)
249 const struct tcp_sock *tp = tcp_sk(sk);
250 struct bictcp *ca = inet_csk_ca(sk);
252 ca->epoch_start = 0; /* end of epoch */
254 /* in case of wrong delay_max*/
255 if (ca->delay_min > 0 && ca->delay_max > ca->delay_min)
256 ca->delay_max = ca->delay_min
257 + ((ca->delay_max - ca->delay_min)* 90) / 100;
259 /* Wmax and fast convergence */
260 if (tp->snd_cwnd < ca->last_max_cwnd && fast_convergence)
261 ca->last_max_cwnd = (tp->snd_cwnd * (BICTCP_BETA_SCALE + beta))
262 / (2 * BICTCP_BETA_SCALE);
263 else
264 ca->last_max_cwnd = tp->snd_cwnd;
266 ca->loss_cwnd = tp->snd_cwnd;
269 if (tp->snd_cwnd <= low_window)
270 return max(tp->snd_cwnd >> 1U, 2U);
271 else
272 return max((tp->snd_cwnd * beta) / BICTCP_BETA_SCALE, 2U);
275 static u32 bictcp_undo_cwnd(struct sock *sk)
277 const struct tcp_sock *tp = tcp_sk(sk);
278 const struct bictcp *ca = inet_csk_ca(sk);
279 return max(tp->snd_cwnd, ca->last_max_cwnd);
282 static u32 bictcp_min_cwnd(struct sock *sk)
284 const struct tcp_sock *tp = tcp_sk(sk);
285 return tp->snd_ssthresh;
288 static void bictcp_state(struct sock *sk, u8 new_state)
290 if (new_state == TCP_CA_Loss)
291 bictcp_reset(inet_csk_ca(sk));
294 /* Track delayed acknowledgement ratio using sliding window
295 * ratio = (15*ratio + sample) / 16
297 static void bictcp_acked(struct sock *sk, u32 cnt)
299 const struct inet_connection_sock *icsk = inet_csk(sk);
301 if (cnt > 0 && icsk->icsk_ca_state == TCP_CA_Open) {
302 struct bictcp *ca = inet_csk_ca(sk);
303 cnt -= ca->delayed_ack >> ACK_RATIO_SHIFT;
304 ca->delayed_ack += cnt;
309 static struct tcp_congestion_ops bictcp = {
310 .init = bictcp_init,
311 .ssthresh = bictcp_recalc_ssthresh,
312 .cong_avoid = bictcp_cong_avoid,
313 .set_state = bictcp_state,
314 .undo_cwnd = bictcp_undo_cwnd,
315 .min_cwnd = bictcp_min_cwnd,
316 .pkts_acked = bictcp_acked,
317 .owner = THIS_MODULE,
318 .name = "bic",
321 static int __init bictcp_register(void)
323 BUG_ON(sizeof(struct bictcp) > ICSK_CA_PRIV_SIZE);
324 return tcp_register_congestion_control(&bictcp);
327 static void __exit bictcp_unregister(void)
329 tcp_unregister_congestion_control(&bictcp);
332 module_init(bictcp_register);
333 module_exit(bictcp_unregister);
335 MODULE_AUTHOR("Stephen Hemminger");
336 MODULE_LICENSE("GPL");
337 MODULE_DESCRIPTION("BIC TCP");