1 |
62 |
marcus.erl |
/*
|
2 |
|
|
* TCP CUBIC: Binary Increase Congestion control for TCP v2.1
|
3 |
|
|
*
|
4 |
|
|
* This is from the implementation of CUBIC TCP in
|
5 |
|
|
* Injong Rhee, Lisong Xu.
|
6 |
|
|
* "CUBIC: A New TCP-Friendly High-Speed TCP Variant
|
7 |
|
|
* in PFLDnet 2005
|
8 |
|
|
* Available from:
|
9 |
|
|
* http://www.csc.ncsu.edu/faculty/rhee/export/bitcp/cubic-paper.pdf
|
10 |
|
|
*
|
11 |
|
|
* Unless CUBIC is enabled and congestion window is large
|
12 |
|
|
* this behaves the same as the original Reno.
|
13 |
|
|
*/
|
14 |
|
|
|
15 |
|
|
#include <linux/mm.h>
|
16 |
|
|
#include <linux/module.h>
|
17 |
|
|
#include <net/tcp.h>
|
18 |
|
|
#include <asm/div64.h>
|
19 |
|
|
|
20 |
|
|
#define BICTCP_BETA_SCALE 1024 /* Scale factor beta calculation
|
21 |
|
|
* max_cwnd = snd_cwnd * beta
|
22 |
|
|
*/
|
23 |
|
|
#define BICTCP_B 4 /*
|
24 |
|
|
* In binary search,
|
25 |
|
|
* go to point (max+min)/N
|
26 |
|
|
*/
|
27 |
|
|
#define BICTCP_HZ 10 /* BIC HZ 2^10 = 1024 */
|
28 |
|
|
|
29 |
|
|
static int fast_convergence __read_mostly = 1;
|
30 |
|
|
static int max_increment __read_mostly = 16;
|
31 |
|
|
static int beta __read_mostly = 819; /* = 819/1024 (BICTCP_BETA_SCALE) */
|
32 |
|
|
static int initial_ssthresh __read_mostly;
|
33 |
|
|
static int bic_scale __read_mostly = 41;
|
34 |
|
|
static int tcp_friendliness __read_mostly = 1;
|
35 |
|
|
|
36 |
|
|
static u32 cube_rtt_scale __read_mostly;
|
37 |
|
|
static u32 beta_scale __read_mostly;
|
38 |
|
|
static u64 cube_factor __read_mostly;
|
39 |
|
|
|
40 |
|
|
/* Note parameters that are used for precomputing scale factors are read-only */
|
41 |
|
|
module_param(fast_convergence, int, 0644);
|
42 |
|
|
MODULE_PARM_DESC(fast_convergence, "turn on/off fast convergence");
|
43 |
|
|
module_param(max_increment, int, 0644);
|
44 |
|
|
MODULE_PARM_DESC(max_increment, "Limit on increment allowed during binary search");
|
45 |
|
|
module_param(beta, int, 0444);
|
46 |
|
|
MODULE_PARM_DESC(beta, "beta for multiplicative increase");
|
47 |
|
|
module_param(initial_ssthresh, int, 0644);
|
48 |
|
|
MODULE_PARM_DESC(initial_ssthresh, "initial value of slow start threshold");
|
49 |
|
|
module_param(bic_scale, int, 0444);
|
50 |
|
|
MODULE_PARM_DESC(bic_scale, "scale (scaled by 1024) value for bic function (bic_scale/1024)");
|
51 |
|
|
module_param(tcp_friendliness, int, 0644);
|
52 |
|
|
MODULE_PARM_DESC(tcp_friendliness, "turn on/off tcp friendliness");
|
53 |
|
|
|
54 |
|
|
/* BIC TCP Parameters */
|
55 |
|
|
struct bictcp {
|
56 |
|
|
u32 cnt; /* increase cwnd by 1 after ACKs */
|
57 |
|
|
u32 last_max_cwnd; /* last maximum snd_cwnd */
|
58 |
|
|
u32 loss_cwnd; /* congestion window at last loss */
|
59 |
|
|
u32 last_cwnd; /* the last snd_cwnd */
|
60 |
|
|
u32 last_time; /* time when updated last_cwnd */
|
61 |
|
|
u32 bic_origin_point;/* origin point of bic function */
|
62 |
|
|
u32 bic_K; /* time to origin point from the beginning of the current epoch */
|
63 |
|
|
u32 delay_min; /* min delay */
|
64 |
|
|
u32 epoch_start; /* beginning of an epoch */
|
65 |
|
|
u32 ack_cnt; /* number of acks */
|
66 |
|
|
u32 tcp_cwnd; /* estimated tcp cwnd */
|
67 |
|
|
#define ACK_RATIO_SHIFT 4
|
68 |
|
|
u32 delayed_ack; /* estimate the ratio of Packets/ACKs << 4 */
|
69 |
|
|
};
|
70 |
|
|
|
71 |
|
|
static inline void bictcp_reset(struct bictcp *ca)
|
72 |
|
|
{
|
73 |
|
|
ca->cnt = 0;
|
74 |
|
|
ca->last_max_cwnd = 0;
|
75 |
|
|
ca->loss_cwnd = 0;
|
76 |
|
|
ca->last_cwnd = 0;
|
77 |
|
|
ca->last_time = 0;
|
78 |
|
|
ca->bic_origin_point = 0;
|
79 |
|
|
ca->bic_K = 0;
|
80 |
|
|
ca->delay_min = 0;
|
81 |
|
|
ca->epoch_start = 0;
|
82 |
|
|
ca->delayed_ack = 2 << ACK_RATIO_SHIFT;
|
83 |
|
|
ca->ack_cnt = 0;
|
84 |
|
|
ca->tcp_cwnd = 0;
|
85 |
|
|
}
|
86 |
|
|
|
87 |
|
|
static void bictcp_init(struct sock *sk)
|
88 |
|
|
{
|
89 |
|
|
bictcp_reset(inet_csk_ca(sk));
|
90 |
|
|
if (initial_ssthresh)
|
91 |
|
|
tcp_sk(sk)->snd_ssthresh = initial_ssthresh;
|
92 |
|
|
}
|
93 |
|
|
|
94 |
|
|
/* calculate the cubic root of x using a table lookup followed by one
|
95 |
|
|
* Newton-Raphson iteration.
|
96 |
|
|
* Avg err ~= 0.195%
|
97 |
|
|
*/
|
98 |
|
|
static u32 cubic_root(u64 a)
|
99 |
|
|
{
|
100 |
|
|
u32 x, b, shift;
|
101 |
|
|
/*
|
102 |
|
|
* cbrt(x) MSB values for x MSB values in [0..63].
|
103 |
|
|
* Precomputed then refined by hand - Willy Tarreau
|
104 |
|
|
*
|
105 |
|
|
* For x in [0..63],
|
106 |
|
|
* v = cbrt(x << 18) - 1
|
107 |
|
|
* cbrt(x) = (v[x] + 10) >> 6
|
108 |
|
|
*/
|
109 |
|
|
static const u8 v[] = {
|
110 |
|
|
/* 0x00 */ 0, 54, 54, 54, 118, 118, 118, 118,
|
111 |
|
|
/* 0x08 */ 123, 129, 134, 138, 143, 147, 151, 156,
|
112 |
|
|
/* 0x10 */ 157, 161, 164, 168, 170, 173, 176, 179,
|
113 |
|
|
/* 0x18 */ 181, 185, 187, 190, 192, 194, 197, 199,
|
114 |
|
|
/* 0x20 */ 200, 202, 204, 206, 209, 211, 213, 215,
|
115 |
|
|
/* 0x28 */ 217, 219, 221, 222, 224, 225, 227, 229,
|
116 |
|
|
/* 0x30 */ 231, 232, 234, 236, 237, 239, 240, 242,
|
117 |
|
|
/* 0x38 */ 244, 245, 246, 248, 250, 251, 252, 254,
|
118 |
|
|
};
|
119 |
|
|
|
120 |
|
|
b = fls64(a);
|
121 |
|
|
if (b < 7) {
|
122 |
|
|
/* a in [0..63] */
|
123 |
|
|
return ((u32)v[(u32)a] + 35) >> 6;
|
124 |
|
|
}
|
125 |
|
|
|
126 |
|
|
b = ((b * 84) >> 8) - 1;
|
127 |
|
|
shift = (a >> (b * 3));
|
128 |
|
|
|
129 |
|
|
x = ((u32)(((u32)v[shift] + 10) << b)) >> 6;
|
130 |
|
|
|
131 |
|
|
/*
|
132 |
|
|
* Newton-Raphson iteration
|
133 |
|
|
* 2
|
134 |
|
|
* x = ( 2 * x + a / x ) / 3
|
135 |
|
|
* k+1 k k
|
136 |
|
|
*/
|
137 |
|
|
x = (2 * x + (u32)div64_64(a, (u64)x * (u64)(x - 1)));
|
138 |
|
|
x = ((x * 341) >> 10);
|
139 |
|
|
return x;
|
140 |
|
|
}
|
141 |
|
|
|
142 |
|
|
/*
|
143 |
|
|
* Compute congestion window to use.
|
144 |
|
|
*/
|
145 |
|
|
static inline void bictcp_update(struct bictcp *ca, u32 cwnd)
|
146 |
|
|
{
|
147 |
|
|
u64 offs;
|
148 |
|
|
u32 delta, t, bic_target, min_cnt, max_cnt;
|
149 |
|
|
|
150 |
|
|
ca->ack_cnt++; /* count the number of ACKs */
|
151 |
|
|
|
152 |
|
|
if (ca->last_cwnd == cwnd &&
|
153 |
|
|
(s32)(tcp_time_stamp - ca->last_time) <= HZ / 32)
|
154 |
|
|
return;
|
155 |
|
|
|
156 |
|
|
ca->last_cwnd = cwnd;
|
157 |
|
|
ca->last_time = tcp_time_stamp;
|
158 |
|
|
|
159 |
|
|
if (ca->epoch_start == 0) {
|
160 |
|
|
ca->epoch_start = tcp_time_stamp; /* record the beginning of an epoch */
|
161 |
|
|
ca->ack_cnt = 1; /* start counting */
|
162 |
|
|
ca->tcp_cwnd = cwnd; /* syn with cubic */
|
163 |
|
|
|
164 |
|
|
if (ca->last_max_cwnd <= cwnd) {
|
165 |
|
|
ca->bic_K = 0;
|
166 |
|
|
ca->bic_origin_point = cwnd;
|
167 |
|
|
} else {
|
168 |
|
|
/* Compute new K based on
|
169 |
|
|
* (wmax-cwnd) * (srtt>>3 / HZ) / c * 2^(3*bictcp_HZ)
|
170 |
|
|
*/
|
171 |
|
|
ca->bic_K = cubic_root(cube_factor
|
172 |
|
|
* (ca->last_max_cwnd - cwnd));
|
173 |
|
|
ca->bic_origin_point = ca->last_max_cwnd;
|
174 |
|
|
}
|
175 |
|
|
}
|
176 |
|
|
|
177 |
|
|
/* cubic function - calc*/
|
178 |
|
|
/* calculate c * time^3 / rtt,
|
179 |
|
|
* while considering overflow in calculation of time^3
|
180 |
|
|
* (so time^3 is done by using 64 bit)
|
181 |
|
|
* and without the support of division of 64bit numbers
|
182 |
|
|
* (so all divisions are done by using 32 bit)
|
183 |
|
|
* also NOTE the unit of those veriables
|
184 |
|
|
* time = (t - K) / 2^bictcp_HZ
|
185 |
|
|
* c = bic_scale >> 10
|
186 |
|
|
* rtt = (srtt >> 3) / HZ
|
187 |
|
|
* !!! The following code does not have overflow problems,
|
188 |
|
|
* if the cwnd < 1 million packets !!!
|
189 |
|
|
*/
|
190 |
|
|
|
191 |
|
|
/* change the unit from HZ to bictcp_HZ */
|
192 |
|
|
t = ((tcp_time_stamp + (ca->delay_min>>3) - ca->epoch_start)
|
193 |
|
|
<< BICTCP_HZ) / HZ;
|
194 |
|
|
|
195 |
|
|
if (t < ca->bic_K) /* t - K */
|
196 |
|
|
offs = ca->bic_K - t;
|
197 |
|
|
else
|
198 |
|
|
offs = t - ca->bic_K;
|
199 |
|
|
|
200 |
|
|
/* c/rtt * (t-K)^3 */
|
201 |
|
|
delta = (cube_rtt_scale * offs * offs * offs) >> (10+3*BICTCP_HZ);
|
202 |
|
|
if (t < ca->bic_K) /* below origin*/
|
203 |
|
|
bic_target = ca->bic_origin_point - delta;
|
204 |
|
|
else /* above origin*/
|
205 |
|
|
bic_target = ca->bic_origin_point + delta;
|
206 |
|
|
|
207 |
|
|
/* cubic function - calc bictcp_cnt*/
|
208 |
|
|
if (bic_target > cwnd) {
|
209 |
|
|
ca->cnt = cwnd / (bic_target - cwnd);
|
210 |
|
|
} else {
|
211 |
|
|
ca->cnt = 100 * cwnd; /* very small increment*/
|
212 |
|
|
}
|
213 |
|
|
|
214 |
|
|
if (ca->delay_min > 0) {
|
215 |
|
|
/* max increment = Smax * rtt / 0.1 */
|
216 |
|
|
min_cnt = (cwnd * HZ * 8)/(10 * max_increment * ca->delay_min);
|
217 |
|
|
|
218 |
|
|
/* use concave growth when the target is above the origin */
|
219 |
|
|
if (ca->cnt < min_cnt && t >= ca->bic_K)
|
220 |
|
|
ca->cnt = min_cnt;
|
221 |
|
|
}
|
222 |
|
|
|
223 |
|
|
/* slow start and low utilization */
|
224 |
|
|
if (ca->loss_cwnd == 0) /* could be aggressive in slow start */
|
225 |
|
|
ca->cnt = 50;
|
226 |
|
|
|
227 |
|
|
/* TCP Friendly */
|
228 |
|
|
if (tcp_friendliness) {
|
229 |
|
|
u32 scale = beta_scale;
|
230 |
|
|
delta = (cwnd * scale) >> 3;
|
231 |
|
|
while (ca->ack_cnt > delta) { /* update tcp cwnd */
|
232 |
|
|
ca->ack_cnt -= delta;
|
233 |
|
|
ca->tcp_cwnd++;
|
234 |
|
|
}
|
235 |
|
|
|
236 |
|
|
if (ca->tcp_cwnd > cwnd){ /* if bic is slower than tcp */
|
237 |
|
|
delta = ca->tcp_cwnd - cwnd;
|
238 |
|
|
max_cnt = cwnd / delta;
|
239 |
|
|
if (ca->cnt > max_cnt)
|
240 |
|
|
ca->cnt = max_cnt;
|
241 |
|
|
}
|
242 |
|
|
}
|
243 |
|
|
|
244 |
|
|
ca->cnt = (ca->cnt << ACK_RATIO_SHIFT) / ca->delayed_ack;
|
245 |
|
|
if (ca->cnt == 0) /* cannot be zero */
|
246 |
|
|
ca->cnt = 1;
|
247 |
|
|
}
|
248 |
|
|
|
249 |
|
|
static void bictcp_cong_avoid(struct sock *sk, u32 ack,
|
250 |
|
|
u32 in_flight, int data_acked)
|
251 |
|
|
{
|
252 |
|
|
struct tcp_sock *tp = tcp_sk(sk);
|
253 |
|
|
struct bictcp *ca = inet_csk_ca(sk);
|
254 |
|
|
|
255 |
|
|
if (!tcp_is_cwnd_limited(sk, in_flight))
|
256 |
|
|
return;
|
257 |
|
|
|
258 |
|
|
if (tp->snd_cwnd <= tp->snd_ssthresh)
|
259 |
|
|
tcp_slow_start(tp);
|
260 |
|
|
else {
|
261 |
|
|
bictcp_update(ca, tp->snd_cwnd);
|
262 |
|
|
|
263 |
|
|
/* In dangerous area, increase slowly.
|
264 |
|
|
* In theory this is tp->snd_cwnd += 1 / tp->snd_cwnd
|
265 |
|
|
*/
|
266 |
|
|
if (tp->snd_cwnd_cnt >= ca->cnt) {
|
267 |
|
|
if (tp->snd_cwnd < tp->snd_cwnd_clamp)
|
268 |
|
|
tp->snd_cwnd++;
|
269 |
|
|
tp->snd_cwnd_cnt = 0;
|
270 |
|
|
} else
|
271 |
|
|
tp->snd_cwnd_cnt++;
|
272 |
|
|
}
|
273 |
|
|
|
274 |
|
|
}
|
275 |
|
|
|
276 |
|
|
static u32 bictcp_recalc_ssthresh(struct sock *sk)
|
277 |
|
|
{
|
278 |
|
|
const struct tcp_sock *tp = tcp_sk(sk);
|
279 |
|
|
struct bictcp *ca = inet_csk_ca(sk);
|
280 |
|
|
|
281 |
|
|
ca->epoch_start = 0; /* end of epoch */
|
282 |
|
|
|
283 |
|
|
/* Wmax and fast convergence */
|
284 |
|
|
if (tp->snd_cwnd < ca->last_max_cwnd && fast_convergence)
|
285 |
|
|
ca->last_max_cwnd = (tp->snd_cwnd * (BICTCP_BETA_SCALE + beta))
|
286 |
|
|
/ (2 * BICTCP_BETA_SCALE);
|
287 |
|
|
else
|
288 |
|
|
ca->last_max_cwnd = tp->snd_cwnd;
|
289 |
|
|
|
290 |
|
|
ca->loss_cwnd = tp->snd_cwnd;
|
291 |
|
|
|
292 |
|
|
return max((tp->snd_cwnd * beta) / BICTCP_BETA_SCALE, 2U);
|
293 |
|
|
}
|
294 |
|
|
|
295 |
|
|
static u32 bictcp_undo_cwnd(struct sock *sk)
|
296 |
|
|
{
|
297 |
|
|
struct bictcp *ca = inet_csk_ca(sk);
|
298 |
|
|
|
299 |
|
|
return max(tcp_sk(sk)->snd_cwnd, ca->last_max_cwnd);
|
300 |
|
|
}
|
301 |
|
|
|
302 |
|
|
static void bictcp_state(struct sock *sk, u8 new_state)
|
303 |
|
|
{
|
304 |
|
|
if (new_state == TCP_CA_Loss)
|
305 |
|
|
bictcp_reset(inet_csk_ca(sk));
|
306 |
|
|
}
|
307 |
|
|
|
308 |
|
|
/* Track delayed acknowledgment ratio using sliding window
|
309 |
|
|
* ratio = (15*ratio + sample) / 16
|
310 |
|
|
*/
|
311 |
|
|
static void bictcp_acked(struct sock *sk, u32 cnt, s32 rtt_us)
|
312 |
|
|
{
|
313 |
|
|
const struct inet_connection_sock *icsk = inet_csk(sk);
|
314 |
|
|
struct bictcp *ca = inet_csk_ca(sk);
|
315 |
|
|
u32 delay;
|
316 |
|
|
|
317 |
|
|
if (icsk->icsk_ca_state == TCP_CA_Open) {
|
318 |
|
|
cnt -= ca->delayed_ack >> ACK_RATIO_SHIFT;
|
319 |
|
|
ca->delayed_ack += cnt;
|
320 |
|
|
}
|
321 |
|
|
|
322 |
|
|
/* Some calls are for duplicates without timetamps */
|
323 |
|
|
if (rtt_us < 0)
|
324 |
|
|
return;
|
325 |
|
|
|
326 |
|
|
/* Discard delay samples right after fast recovery */
|
327 |
|
|
if ((s32)(tcp_time_stamp - ca->epoch_start) < HZ)
|
328 |
|
|
return;
|
329 |
|
|
|
330 |
|
|
delay = usecs_to_jiffies(rtt_us) << 3;
|
331 |
|
|
if (delay == 0)
|
332 |
|
|
delay = 1;
|
333 |
|
|
|
334 |
|
|
/* first time call or link delay decreases */
|
335 |
|
|
if (ca->delay_min == 0 || ca->delay_min > delay)
|
336 |
|
|
ca->delay_min = delay;
|
337 |
|
|
}
|
338 |
|
|
|
339 |
|
|
static struct tcp_congestion_ops cubictcp = {
|
340 |
|
|
.init = bictcp_init,
|
341 |
|
|
.ssthresh = bictcp_recalc_ssthresh,
|
342 |
|
|
.cong_avoid = bictcp_cong_avoid,
|
343 |
|
|
.set_state = bictcp_state,
|
344 |
|
|
.undo_cwnd = bictcp_undo_cwnd,
|
345 |
|
|
.pkts_acked = bictcp_acked,
|
346 |
|
|
.owner = THIS_MODULE,
|
347 |
|
|
.name = "cubic",
|
348 |
|
|
};
|
349 |
|
|
|
350 |
|
|
static int __init cubictcp_register(void)
|
351 |
|
|
{
|
352 |
|
|
BUILD_BUG_ON(sizeof(struct bictcp) > ICSK_CA_PRIV_SIZE);
|
353 |
|
|
|
354 |
|
|
/* Precompute a bunch of the scaling factors that are used per-packet
|
355 |
|
|
* based on SRTT of 100ms
|
356 |
|
|
*/
|
357 |
|
|
|
358 |
|
|
beta_scale = 8*(BICTCP_BETA_SCALE+beta)/ 3 / (BICTCP_BETA_SCALE - beta);
|
359 |
|
|
|
360 |
|
|
cube_rtt_scale = (bic_scale * 10); /* 1024*c/rtt */
|
361 |
|
|
|
362 |
|
|
/* calculate the "K" for (wmax-cwnd) = c/rtt * K^3
|
363 |
|
|
* so K = cubic_root( (wmax-cwnd)*rtt/c )
|
364 |
|
|
* the unit of K is bictcp_HZ=2^10, not HZ
|
365 |
|
|
*
|
366 |
|
|
* c = bic_scale >> 10
|
367 |
|
|
* rtt = 100ms
|
368 |
|
|
*
|
369 |
|
|
* the following code has been designed and tested for
|
370 |
|
|
* cwnd < 1 million packets
|
371 |
|
|
* RTT < 100 seconds
|
372 |
|
|
* HZ < 1,000,00 (corresponding to 10 nano-second)
|
373 |
|
|
*/
|
374 |
|
|
|
375 |
|
|
/* 1/c * 2^2*bictcp_HZ * srtt */
|
376 |
|
|
cube_factor = 1ull << (10+3*BICTCP_HZ); /* 2^40 */
|
377 |
|
|
|
378 |
|
|
/* divide by bic_scale and by constant Srtt (100ms) */
|
379 |
|
|
do_div(cube_factor, bic_scale * 10);
|
380 |
|
|
|
381 |
|
|
return tcp_register_congestion_control(&cubictcp);
|
382 |
|
|
}
|
383 |
|
|
|
384 |
|
|
static void __exit cubictcp_unregister(void)
|
385 |
|
|
{
|
386 |
|
|
tcp_unregister_congestion_control(&cubictcp);
|
387 |
|
|
}
|
388 |
|
|
|
389 |
|
|
module_init(cubictcp_register);
|
390 |
|
|
module_exit(cubictcp_unregister);
|
391 |
|
|
|
392 |
|
|
MODULE_AUTHOR("Sangtae Ha, Stephen Hemminger");
|
393 |
|
|
MODULE_LICENSE("GPL");
|
394 |
|
|
MODULE_DESCRIPTION("CUBIC TCP");
|
395 |
|
|
MODULE_VERSION("2.1");
|