blob: 6d80e063c187f3464fe77ee52e6128183b5a8642 [file] [log] [blame]
Stephen Hemminger83803032005-06-23 12:23:25 -07001/*
2 * Binary Increase Congestion control for TCP
3 *
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
10 *
11 * Unless BIC is enabled and congestion window is large
12 * this behaves the same as the original Reno.
13 */
14
15#include <linux/config.h>
16#include <linux/mm.h>
17#include <linux/module.h>
18#include <net/tcp.h>
19
20
21#define BICTCP_BETA_SCALE 1024 /* Scale factor beta calculation
22 * max_cwnd = snd_cwnd * beta
23 */
24#define BICTCP_B 4 /*
25 * In binary search,
26 * go to point (max+min)/N
27 */
28
29static int fast_convergence = 1;
30static int max_increment = 32;
31static int low_window = 14;
32static int beta = 819; /* = 819/1024 (BICTCP_BETA_SCALE) */
33static int low_utilization_threshold = 153;
34static int low_utilization_period = 2;
35static int initial_ssthresh = 100;
36static int smooth_part = 20;
37
38module_param(fast_convergence, int, 0644);
39MODULE_PARM_DESC(fast_convergence, "turn on/off fast convergence");
40module_param(max_increment, int, 0644);
41MODULE_PARM_DESC(max_increment, "Limit on increment allowed during binary search");
42module_param(low_window, int, 0644);
43MODULE_PARM_DESC(low_window, "lower bound on congestion window (for TCP friendliness)");
44module_param(beta, int, 0644);
45MODULE_PARM_DESC(beta, "beta for multiplicative increase");
46module_param(low_utilization_threshold, int, 0644);
47MODULE_PARM_DESC(low_utilization_threshold, "percent (scaled by 1024) for low utilization mode");
48module_param(low_utilization_period, int, 0644);
49MODULE_PARM_DESC(low_utilization_period, "if average delay exceeds then goto to low utilization mode (seconds)");
50module_param(initial_ssthresh, int, 0644);
51MODULE_PARM_DESC(initial_ssthresh, "initial value of slow start threshold");
52module_param(smooth_part, int, 0644);
53MODULE_PARM_DESC(smooth_part, "log(B/(B*Smin))/log(B/(B-1))+B, # of RTT from Wmax-B to Wmax");
54
55
56/* BIC TCP Parameters */
57struct 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 */
71};
72
73static inline void bictcp_reset(struct bictcp *ca)
74{
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;
87}
88
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -030089static void bictcp_init(struct sock *sk)
Stephen Hemminger83803032005-06-23 12:23:25 -070090{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -030091 bictcp_reset(inet_csk_ca(sk));
Stephen Hemminger83803032005-06-23 12:23:25 -070092 if (initial_ssthresh)
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -030093 tcp_sk(sk)->snd_ssthresh = initial_ssthresh;
Stephen Hemminger83803032005-06-23 12:23:25 -070094}
95
96/*
97 * Compute congestion window to use.
98 */
99static inline void bictcp_update(struct bictcp *ca, u32 cwnd)
100{
101 if (ca->last_cwnd == cwnd &&
102 (s32)(tcp_time_stamp - ca->last_time) <= HZ / 32)
103 return;
104
105 ca->last_cwnd = cwnd;
106 ca->last_time = tcp_time_stamp;
107
108 if (ca->epoch_start == 0) /* record the beginning of an epoch */
109 ca->epoch_start = tcp_time_stamp;
110
111 /* start off normal */
112 if (cwnd <= low_window) {
113 ca->cnt = cwnd;
114 return;
115 }
116
117 /* binary increase */
118 if (cwnd < ca->last_max_cwnd) {
119 __u32 dist = (ca->last_max_cwnd - cwnd)
120 / BICTCP_B;
121
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))
Stephen Hemminger42a39452005-10-05 12:09:31 -0700139 / (cwnd - ca->last_max_cwnd);
Stephen Hemminger83803032005-06-23 12:23:25 -0700140 else
141 /* linear increase */
142 ca->cnt = cwnd / max_increment;
143 }
144
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;
150 }
151
152 ca->cnt = (ca->cnt << ACK_RATIO_SHIFT) / ca->delayed_ack;
153 if (ca->cnt == 0) /* cannot be zero */
154 ca->cnt = 1;
155}
156
157
158/* Detect low utilization in congestion avoidance */
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300159static inline void bictcp_low_utilization(struct sock *sk, int flag)
Stephen Hemminger83803032005-06-23 12:23:25 -0700160{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300161 const struct tcp_sock *tp = tcp_sk(sk);
162 struct bictcp *ca = inet_csk_ca(sk);
Stephen Hemminger83803032005-06-23 12:23:25 -0700163 u32 dist, delay;
164
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;
173 }
174
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;
179
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;
184 }
185
186 if (ca->delay_max < delay)
187 ca->delay_max = delay;
188
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;
195
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;
202 }
203
204 return;
205
206 notlow:
207 ca->low_utilization = 0;
208 ca->low_utilization_start = 0;
209
210}
211
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300212static void bictcp_cong_avoid(struct sock *sk, u32 ack,
Stephen Hemminger83803032005-06-23 12:23:25 -0700213 u32 seq_rtt, u32 in_flight, int data_acked)
214{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300215 struct tcp_sock *tp = tcp_sk(sk);
216 struct bictcp *ca = inet_csk_ca(sk);
Stephen Hemminger83803032005-06-23 12:23:25 -0700217
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300218 bictcp_low_utilization(sk, data_acked);
Stephen Hemminger83803032005-06-23 12:23:25 -0700219
220 if (in_flight < tp->snd_cwnd)
221 return;
222
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);
229
230 /* In dangerous area, increase slowly.
231 * In theory this is tp->snd_cwnd += 1 / tp->snd_cwnd
232 */
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++;
239 }
240
241}
242
243/*
244 * behave like Reno until low_window is reached,
245 * then increase congestion window slowly
246 */
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300247static u32 bictcp_recalc_ssthresh(struct sock *sk)
Stephen Hemminger83803032005-06-23 12:23:25 -0700248{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300249 const struct tcp_sock *tp = tcp_sk(sk);
250 struct bictcp *ca = inet_csk_ca(sk);
Stephen Hemminger83803032005-06-23 12:23:25 -0700251
252 ca->epoch_start = 0; /* end of epoch */
253
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;
258
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;
265
266 ca->loss_cwnd = tp->snd_cwnd;
267
268
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);
273}
274
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300275static u32 bictcp_undo_cwnd(struct sock *sk)
Stephen Hemminger83803032005-06-23 12:23:25 -0700276{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300277 const struct tcp_sock *tp = tcp_sk(sk);
278 const struct bictcp *ca = inet_csk_ca(sk);
Stephen Hemminger83803032005-06-23 12:23:25 -0700279 return max(tp->snd_cwnd, ca->last_max_cwnd);
280}
281
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300282static u32 bictcp_min_cwnd(struct sock *sk)
Stephen Hemminger83803032005-06-23 12:23:25 -0700283{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300284 const struct tcp_sock *tp = tcp_sk(sk);
Stephen Hemminger83803032005-06-23 12:23:25 -0700285 return tp->snd_ssthresh;
286}
287
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300288static void bictcp_state(struct sock *sk, u8 new_state)
Stephen Hemminger83803032005-06-23 12:23:25 -0700289{
290 if (new_state == TCP_CA_Loss)
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300291 bictcp_reset(inet_csk_ca(sk));
Stephen Hemminger83803032005-06-23 12:23:25 -0700292}
293
294/* Track delayed acknowledgement ratio using sliding window
295 * ratio = (15*ratio + sample) / 16
296 */
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300297static void bictcp_acked(struct sock *sk, u32 cnt)
Stephen Hemminger83803032005-06-23 12:23:25 -0700298{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300299 const struct inet_connection_sock *icsk = inet_csk(sk);
300
301 if (cnt > 0 && icsk->icsk_ca_state == TCP_CA_Open) {
302 struct bictcp *ca = inet_csk_ca(sk);
Stephen Hemminger83803032005-06-23 12:23:25 -0700303 cnt -= ca->delayed_ack >> ACK_RATIO_SHIFT;
304 ca->delayed_ack += cnt;
305 }
306}
307
308
309static 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",
319};
320
321static int __init bictcp_register(void)
322{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300323 BUG_ON(sizeof(struct bictcp) > ICSK_CA_PRIV_SIZE);
Stephen Hemminger83803032005-06-23 12:23:25 -0700324 return tcp_register_congestion_control(&bictcp);
325}
326
327static void __exit bictcp_unregister(void)
328{
329 tcp_unregister_congestion_control(&bictcp);
330}
331
332module_init(bictcp_register);
333module_exit(bictcp_unregister);
334
335MODULE_AUTHOR("Stephen Hemminger");
336MODULE_LICENSE("GPL");
337MODULE_DESCRIPTION("BIC TCP");