2 * CAIA Delay-Gradient (CDG) congestion control
4 * This implementation is based on the paper:
5 * D.A. Hayes and G. Armitage. "Revisiting TCP congestion control using
6 * delay gradients." In IFIP Networking, pages 328-341. Springer, 2011.
8 * Scavenger traffic (Less-than-Best-Effort) should disable coexistence
9 * heuristics using parameters use_shadow=0 and use_ineff=0.
11 * Parameters window, backoff_beta, and backoff_factor are crucial for
12 * throughput and delay. Future work is needed to determine better defaults,
13 * and to provide guidelines for use in different environments/contexts.
15 * Except for window, knobs are configured via /sys/module/tcp_cdg/parameters/.
16 * Parameter window is only configurable when loading tcp_cdg as a module.
18 * Notable differences from paper/FreeBSD:
19 * o Using Hybrid Slow start and Proportional Rate Reduction.
20 * o Add toggle for shadow window mechanism. Suggested by David Hayes.
21 * o Add toggle for non-congestion loss tolerance.
22 * o Scaling parameter G is changed to a backoff factor;
23 * conversion is given by: backoff_factor = 1000/(G * window).
24 * o Limit shadow window to 2 * cwnd, or to cwnd when application limited.
25 * o More accurate e^-x.
27 #include <linux/kernel.h>
28 #include <linux/random.h>
29 #include <linux/module.h>
30 #include <linux/sched/clock.h>
34 #define HYSTART_ACK_TRAIN 1
35 #define HYSTART_DELAY 2
37 static int window __read_mostly
= 8;
38 static unsigned int backoff_beta __read_mostly
= 0.7071 * 1024; /* sqrt 0.5 */
39 static unsigned int backoff_factor __read_mostly
= 42;
40 static unsigned int hystart_detect __read_mostly
= 3;
41 static unsigned int use_ineff __read_mostly
= 5;
42 static bool use_shadow __read_mostly
= true;
43 static bool use_tolerance __read_mostly
;
45 module_param(window
, int, 0444);
46 MODULE_PARM_DESC(window
, "gradient window size (power of two <= 256)");
47 module_param(backoff_beta
, uint
, 0644);
48 MODULE_PARM_DESC(backoff_beta
, "backoff beta (0-1024)");
49 module_param(backoff_factor
, uint
, 0644);
50 MODULE_PARM_DESC(backoff_factor
, "backoff probability scale factor");
51 module_param(hystart_detect
, uint
, 0644);
52 MODULE_PARM_DESC(hystart_detect
, "use Hybrid Slow start "
53 "(0: disabled, 1: ACK train, 2: delay threshold, 3: both)");
54 module_param(use_ineff
, uint
, 0644);
55 MODULE_PARM_DESC(use_ineff
, "use ineffectual backoff detection (threshold)");
56 module_param(use_shadow
, bool, 0644);
57 MODULE_PARM_DESC(use_shadow
, "use shadow window heuristic");
58 module_param(use_tolerance
, bool, 0644);
59 MODULE_PARM_DESC(use_tolerance
, "use loss tolerance heuristic");
79 struct cdg_minmax rtt
;
80 struct cdg_minmax rtt_prev
;
81 struct cdg_minmax
*gradients
;
82 struct cdg_minmax gsum
;
98 * nexp_u32 - negative base-e exponential
99 * @ux: x in units of micro
101 * Returns exp(ux * -1e-6) * U32_MAX.
103 static u32 __pure
nexp_u32(u32 ux
)
105 static const u16 v
[] = {
106 /* exp(-x)*65536-1 for x = 0, 0.000256, 0.000512, ... */
108 65518, 65501, 65468, 65401, 65267, 65001, 64470, 63422,
109 61378, 57484, 50423, 38795, 22965, 8047, 987, 14,
115 /* Cut off when ux >= 2^24 (actual result is <= 222/U32_MAX). */
119 /* Scale first eight bits linearly: */
120 res
= U32_MAX
- (ux
& 0xff) * (U32_MAX
/ 1000000);
122 /* Obtain e^(x + y + ...) by computing e^x * e^y * ...: */
123 for (i
= 1; msb
; i
++, msb
>>= 1) {
124 u32 y
= v
[i
& -(msb
& 1)] + U32_C(1);
126 res
= ((u64
)res
* y
) >> 16;
132 /* Based on the HyStart algorithm (by Ha et al.) that is implemented in
133 * tcp_cubic. Differences/experimental changes:
134 * o Using Hayes' delayed ACK filter.
135 * o Using a usec clock for the ACK train.
136 * o Reset ACK train when application limited.
137 * o Invoked at any cwnd (i.e. also when cwnd < 16).
138 * o Invoked only when cwnd < ssthresh (i.e. not when cwnd == ssthresh).
140 static void tcp_cdg_hystart_update(struct sock
*sk
)
142 struct cdg
*ca
= inet_csk_ca(sk
);
143 struct tcp_sock
*tp
= tcp_sk(sk
);
145 ca
->delay_min
= min_not_zero(ca
->delay_min
, ca
->rtt
.min
);
146 if (ca
->delay_min
== 0)
149 if (hystart_detect
& HYSTART_ACK_TRAIN
) {
150 u32 now_us
= div_u64(local_clock(), NSEC_PER_USEC
);
152 if (ca
->last_ack
== 0 || !tcp_is_cwnd_limited(sk
)) {
153 ca
->last_ack
= now_us
;
154 ca
->round_start
= now_us
;
155 } else if (before(now_us
, ca
->last_ack
+ 3000)) {
156 u32 base_owd
= max(ca
->delay_min
/ 2U, 125U);
158 ca
->last_ack
= now_us
;
159 if (after(now_us
, ca
->round_start
+ base_owd
)) {
160 NET_INC_STATS(sock_net(sk
),
161 LINUX_MIB_TCPHYSTARTTRAINDETECT
);
162 NET_ADD_STATS(sock_net(sk
),
163 LINUX_MIB_TCPHYSTARTTRAINCWND
,
165 tp
->snd_ssthresh
= tp
->snd_cwnd
;
171 if (hystart_detect
& HYSTART_DELAY
) {
172 if (ca
->sample_cnt
< 8) {
175 s32 thresh
= max(ca
->delay_min
+ ca
->delay_min
/ 8U,
178 if (ca
->rtt
.min
> thresh
) {
179 NET_INC_STATS(sock_net(sk
),
180 LINUX_MIB_TCPHYSTARTDELAYDETECT
);
181 NET_ADD_STATS(sock_net(sk
),
182 LINUX_MIB_TCPHYSTARTDELAYCWND
,
184 tp
->snd_ssthresh
= tp
->snd_cwnd
;
190 static s32
tcp_cdg_grad(struct cdg
*ca
)
192 s32 gmin
= ca
->rtt
.min
- ca
->rtt_prev
.min
;
193 s32 gmax
= ca
->rtt
.max
- ca
->rtt_prev
.max
;
197 ca
->gsum
.min
+= gmin
- ca
->gradients
[ca
->tail
].min
;
198 ca
->gsum
.max
+= gmax
- ca
->gradients
[ca
->tail
].max
;
199 ca
->gradients
[ca
->tail
].min
= gmin
;
200 ca
->gradients
[ca
->tail
].max
= gmax
;
201 ca
->tail
= (ca
->tail
+ 1) & (window
- 1);
206 /* We keep sums to ignore gradients during cwnd reductions;
207 * the paper's smoothed gradients otherwise simplify to:
208 * (rtt_latest - rtt_oldest) / window.
210 * We also drop division by window here.
212 grad
= gmin
> 0 ? gmin
: gmax
;
214 /* Extrapolate missing values in gradient window: */
216 if (!ca
->gradients
&& window
> 1)
217 grad
*= window
; /* Memory allocation failed. */
218 else if (ca
->tail
== 0)
221 grad
= (grad
* window
) / (int)ca
->tail
;
224 /* Backoff was effectual: */
225 if (gmin
<= -32 || gmax
<= -32)
229 /* Reduce small variations to zero: */
230 gmin
= DIV_ROUND_CLOSEST(gmin
, 64);
231 gmax
= DIV_ROUND_CLOSEST(gmax
, 64);
233 if (gmin
> 0 && gmax
<= 0)
234 ca
->state
= CDG_FULL
;
235 else if ((gmin
> 0 && gmax
> 0) || gmax
< 0)
236 ca
->state
= CDG_NONFULL
;
241 static bool tcp_cdg_backoff(struct sock
*sk
, u32 grad
)
243 struct cdg
*ca
= inet_csk_ca(sk
);
244 struct tcp_sock
*tp
= tcp_sk(sk
);
246 if (prandom_u32() <= nexp_u32(grad
* backoff_factor
))
251 if (ca
->backoff_cnt
> use_ineff
)
255 ca
->shadow_wnd
= max(ca
->shadow_wnd
, tp
->snd_cwnd
);
256 ca
->state
= CDG_BACKOFF
;
261 /* Not called in CWR or Recovery state. */
262 static void tcp_cdg_cong_avoid(struct sock
*sk
, u32 ack
, u32 acked
)
264 struct cdg
*ca
= inet_csk_ca(sk
);
265 struct tcp_sock
*tp
= tcp_sk(sk
);
269 if (tcp_in_slow_start(tp
) && hystart_detect
)
270 tcp_cdg_hystart_update(sk
);
272 if (after(ack
, ca
->rtt_seq
) && ca
->rtt
.v64
) {
275 if (ca
->rtt_prev
.v64
)
276 grad
= tcp_cdg_grad(ca
);
277 ca
->rtt_seq
= tp
->snd_nxt
;
278 ca
->rtt_prev
= ca
->rtt
;
283 if (grad
> 0 && tcp_cdg_backoff(sk
, grad
))
287 if (!tcp_is_cwnd_limited(sk
)) {
288 ca
->shadow_wnd
= min(ca
->shadow_wnd
, tp
->snd_cwnd
);
292 prior_snd_cwnd
= tp
->snd_cwnd
;
293 tcp_reno_cong_avoid(sk
, ack
, acked
);
295 incr
= tp
->snd_cwnd
- prior_snd_cwnd
;
296 ca
->shadow_wnd
= max(ca
->shadow_wnd
, ca
->shadow_wnd
+ incr
);
299 static void tcp_cdg_acked(struct sock
*sk
, const struct ack_sample
*sample
)
301 struct cdg
*ca
= inet_csk_ca(sk
);
302 struct tcp_sock
*tp
= tcp_sk(sk
);
304 if (sample
->rtt_us
<= 0)
307 /* A heuristic for filtering delayed ACKs, adapted from:
308 * D.A. Hayes. "Timing enhancements to the FreeBSD kernel to support
309 * delay and rate based TCP mechanisms." TR 100219A. CAIA, 2010.
311 if (tp
->sacked_out
== 0) {
312 if (sample
->pkts_acked
== 1 && ca
->delack
) {
313 /* A delayed ACK is only used for the minimum if it is
314 * provenly lower than an existing non-zero minimum.
316 ca
->rtt
.min
= min(ca
->rtt
.min
, sample
->rtt_us
);
319 } else if (sample
->pkts_acked
> 1 && ca
->delack
< 5) {
324 ca
->rtt
.min
= min_not_zero(ca
->rtt
.min
, sample
->rtt_us
);
325 ca
->rtt
.max
= max(ca
->rtt
.max
, sample
->rtt_us
);
328 static u32
tcp_cdg_ssthresh(struct sock
*sk
)
330 struct cdg
*ca
= inet_csk_ca(sk
);
331 struct tcp_sock
*tp
= tcp_sk(sk
);
333 ca
->undo_cwnd
= tp
->snd_cwnd
;
335 if (ca
->state
== CDG_BACKOFF
)
336 return max(2U, (tp
->snd_cwnd
* min(1024U, backoff_beta
)) >> 10);
338 if (ca
->state
== CDG_NONFULL
&& use_tolerance
)
341 ca
->shadow_wnd
= min(ca
->shadow_wnd
>> 1, tp
->snd_cwnd
);
343 return max3(2U, ca
->shadow_wnd
, tp
->snd_cwnd
>> 1);
344 return max(2U, tp
->snd_cwnd
>> 1);
347 static u32
tcp_cdg_undo_cwnd(struct sock
*sk
)
349 struct cdg
*ca
= inet_csk_ca(sk
);
351 return max(tcp_sk(sk
)->snd_cwnd
, ca
->undo_cwnd
);
354 static void tcp_cdg_cwnd_event(struct sock
*sk
, const enum tcp_ca_event ev
)
356 struct cdg
*ca
= inet_csk_ca(sk
);
357 struct tcp_sock
*tp
= tcp_sk(sk
);
358 struct cdg_minmax
*gradients
;
361 case CA_EVENT_CWND_RESTART
:
362 gradients
= ca
->gradients
;
364 memset(gradients
, 0, window
* sizeof(gradients
[0]));
365 memset(ca
, 0, sizeof(*ca
));
367 ca
->gradients
= gradients
;
368 ca
->rtt_seq
= tp
->snd_nxt
;
369 ca
->shadow_wnd
= tp
->snd_cwnd
;
371 case CA_EVENT_COMPLETE_CWR
:
372 ca
->state
= CDG_UNKNOWN
;
373 ca
->rtt_seq
= tp
->snd_nxt
;
374 ca
->rtt_prev
= ca
->rtt
;
382 static void tcp_cdg_init(struct sock
*sk
)
384 struct cdg
*ca
= inet_csk_ca(sk
);
385 struct tcp_sock
*tp
= tcp_sk(sk
);
387 /* We silently fall back to window = 1 if allocation fails. */
389 ca
->gradients
= kcalloc(window
, sizeof(ca
->gradients
[0]),
390 GFP_NOWAIT
| __GFP_NOWARN
);
391 ca
->rtt_seq
= tp
->snd_nxt
;
392 ca
->shadow_wnd
= tp
->snd_cwnd
;
395 static void tcp_cdg_release(struct sock
*sk
)
397 struct cdg
*ca
= inet_csk_ca(sk
);
399 kfree(ca
->gradients
);
402 struct tcp_congestion_ops tcp_cdg __read_mostly
= {
403 .cong_avoid
= tcp_cdg_cong_avoid
,
404 .cwnd_event
= tcp_cdg_cwnd_event
,
405 .pkts_acked
= tcp_cdg_acked
,
406 .undo_cwnd
= tcp_cdg_undo_cwnd
,
407 .ssthresh
= tcp_cdg_ssthresh
,
408 .release
= tcp_cdg_release
,
409 .init
= tcp_cdg_init
,
410 .owner
= THIS_MODULE
,
414 static int __init
tcp_cdg_register(void)
416 if (backoff_beta
> 1024 || window
< 1 || window
> 256)
418 if (!is_power_of_2(window
))
421 BUILD_BUG_ON(sizeof(struct cdg
) > ICSK_CA_PRIV_SIZE
);
422 tcp_register_congestion_control(&tcp_cdg
);
426 static void __exit
tcp_cdg_unregister(void)
428 tcp_unregister_congestion_control(&tcp_cdg
);
431 module_init(tcp_cdg_register
);
432 module_exit(tcp_cdg_unregister
);
433 MODULE_AUTHOR("Kenneth Klette Jonassen");
434 MODULE_LICENSE("GPL");
435 MODULE_DESCRIPTION("TCP CDG");