1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun
3*4882a593Smuzhiyun /* WARNING: This implemenation is not necessarily the same
4*4882a593Smuzhiyun * as the tcp_cubic.c. The purpose is mainly for testing
5*4882a593Smuzhiyun * the kernel BPF logic.
6*4882a593Smuzhiyun *
7*4882a593Smuzhiyun * Highlights:
8*4882a593Smuzhiyun * 1. CONFIG_HZ .kconfig map is used.
9*4882a593Smuzhiyun * 2. In bictcp_update(), calculation is changed to use usec
10*4882a593Smuzhiyun * resolution (i.e. USEC_PER_JIFFY) instead of using jiffies.
11*4882a593Smuzhiyun * Thus, usecs_to_jiffies() is not used in the bpf_cubic.c.
12*4882a593Smuzhiyun * 3. In bitctcp_update() [under tcp_friendliness], the original
13*4882a593Smuzhiyun * "while (ca->ack_cnt > delta)" loop is changed to the equivalent
14*4882a593Smuzhiyun * "ca->ack_cnt / delta" operation.
15*4882a593Smuzhiyun */
16*4882a593Smuzhiyun
17*4882a593Smuzhiyun #include <linux/bpf.h>
18*4882a593Smuzhiyun #include <linux/stddef.h>
19*4882a593Smuzhiyun #include <linux/tcp.h>
20*4882a593Smuzhiyun #include "bpf_tcp_helpers.h"
21*4882a593Smuzhiyun
22*4882a593Smuzhiyun char _license[] SEC("license") = "GPL";
23*4882a593Smuzhiyun
24*4882a593Smuzhiyun #define clamp(val, lo, hi) min((typeof(val))max(val, lo), hi)
25*4882a593Smuzhiyun
26*4882a593Smuzhiyun #define BICTCP_BETA_SCALE 1024 /* Scale factor beta calculation
27*4882a593Smuzhiyun * max_cwnd = snd_cwnd * beta
28*4882a593Smuzhiyun */
29*4882a593Smuzhiyun #define BICTCP_HZ 10 /* BIC HZ 2^10 = 1024 */
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun /* Two methods of hybrid slow start */
32*4882a593Smuzhiyun #define HYSTART_ACK_TRAIN 0x1
33*4882a593Smuzhiyun #define HYSTART_DELAY 0x2
34*4882a593Smuzhiyun
35*4882a593Smuzhiyun /* Number of delay samples for detecting the increase of delay */
36*4882a593Smuzhiyun #define HYSTART_MIN_SAMPLES 8
37*4882a593Smuzhiyun #define HYSTART_DELAY_MIN (4000U) /* 4ms */
38*4882a593Smuzhiyun #define HYSTART_DELAY_MAX (16000U) /* 16 ms */
39*4882a593Smuzhiyun #define HYSTART_DELAY_THRESH(x) clamp(x, HYSTART_DELAY_MIN, HYSTART_DELAY_MAX)
40*4882a593Smuzhiyun
41*4882a593Smuzhiyun static int fast_convergence = 1;
42*4882a593Smuzhiyun static const int beta = 717; /* = 717/1024 (BICTCP_BETA_SCALE) */
43*4882a593Smuzhiyun static int initial_ssthresh;
44*4882a593Smuzhiyun static const int bic_scale = 41;
45*4882a593Smuzhiyun static int tcp_friendliness = 1;
46*4882a593Smuzhiyun
47*4882a593Smuzhiyun static int hystart = 1;
48*4882a593Smuzhiyun static int hystart_detect = HYSTART_ACK_TRAIN | HYSTART_DELAY;
49*4882a593Smuzhiyun static int hystart_low_window = 16;
50*4882a593Smuzhiyun static int hystart_ack_delta_us = 2000;
51*4882a593Smuzhiyun
52*4882a593Smuzhiyun static const __u32 cube_rtt_scale = (bic_scale * 10); /* 1024*c/rtt */
53*4882a593Smuzhiyun static const __u32 beta_scale = 8*(BICTCP_BETA_SCALE+beta) / 3
54*4882a593Smuzhiyun / (BICTCP_BETA_SCALE - beta);
55*4882a593Smuzhiyun /* calculate the "K" for (wmax-cwnd) = c/rtt * K^3
56*4882a593Smuzhiyun * so K = cubic_root( (wmax-cwnd)*rtt/c )
57*4882a593Smuzhiyun * the unit of K is bictcp_HZ=2^10, not HZ
58*4882a593Smuzhiyun *
59*4882a593Smuzhiyun * c = bic_scale >> 10
60*4882a593Smuzhiyun * rtt = 100ms
61*4882a593Smuzhiyun *
62*4882a593Smuzhiyun * the following code has been designed and tested for
63*4882a593Smuzhiyun * cwnd < 1 million packets
64*4882a593Smuzhiyun * RTT < 100 seconds
65*4882a593Smuzhiyun * HZ < 1,000,00 (corresponding to 10 nano-second)
66*4882a593Smuzhiyun */
67*4882a593Smuzhiyun
68*4882a593Smuzhiyun /* 1/c * 2^2*bictcp_HZ * srtt, 2^40 */
69*4882a593Smuzhiyun static const __u64 cube_factor = (__u64)(1ull << (10+3*BICTCP_HZ))
70*4882a593Smuzhiyun / (bic_scale * 10);
71*4882a593Smuzhiyun
72*4882a593Smuzhiyun /* BIC TCP Parameters */
73*4882a593Smuzhiyun struct bictcp {
74*4882a593Smuzhiyun __u32 cnt; /* increase cwnd by 1 after ACKs */
75*4882a593Smuzhiyun __u32 last_max_cwnd; /* last maximum snd_cwnd */
76*4882a593Smuzhiyun __u32 last_cwnd; /* the last snd_cwnd */
77*4882a593Smuzhiyun __u32 last_time; /* time when updated last_cwnd */
78*4882a593Smuzhiyun __u32 bic_origin_point;/* origin point of bic function */
79*4882a593Smuzhiyun __u32 bic_K; /* time to origin point
80*4882a593Smuzhiyun from the beginning of the current epoch */
81*4882a593Smuzhiyun __u32 delay_min; /* min delay (usec) */
82*4882a593Smuzhiyun __u32 epoch_start; /* beginning of an epoch */
83*4882a593Smuzhiyun __u32 ack_cnt; /* number of acks */
84*4882a593Smuzhiyun __u32 tcp_cwnd; /* estimated tcp cwnd */
85*4882a593Smuzhiyun __u16 unused;
86*4882a593Smuzhiyun __u8 sample_cnt; /* number of samples to decide curr_rtt */
87*4882a593Smuzhiyun __u8 found; /* the exit point is found? */
88*4882a593Smuzhiyun __u32 round_start; /* beginning of each round */
89*4882a593Smuzhiyun __u32 end_seq; /* end_seq of the round */
90*4882a593Smuzhiyun __u32 last_ack; /* last time when the ACK spacing is close */
91*4882a593Smuzhiyun __u32 curr_rtt; /* the minimum rtt of current round */
92*4882a593Smuzhiyun };
93*4882a593Smuzhiyun
bictcp_reset(struct bictcp * ca)94*4882a593Smuzhiyun static inline void bictcp_reset(struct bictcp *ca)
95*4882a593Smuzhiyun {
96*4882a593Smuzhiyun ca->cnt = 0;
97*4882a593Smuzhiyun ca->last_max_cwnd = 0;
98*4882a593Smuzhiyun ca->last_cwnd = 0;
99*4882a593Smuzhiyun ca->last_time = 0;
100*4882a593Smuzhiyun ca->bic_origin_point = 0;
101*4882a593Smuzhiyun ca->bic_K = 0;
102*4882a593Smuzhiyun ca->delay_min = 0;
103*4882a593Smuzhiyun ca->epoch_start = 0;
104*4882a593Smuzhiyun ca->ack_cnt = 0;
105*4882a593Smuzhiyun ca->tcp_cwnd = 0;
106*4882a593Smuzhiyun ca->found = 0;
107*4882a593Smuzhiyun }
108*4882a593Smuzhiyun
109*4882a593Smuzhiyun extern unsigned long CONFIG_HZ __kconfig;
110*4882a593Smuzhiyun #define HZ CONFIG_HZ
111*4882a593Smuzhiyun #define USEC_PER_MSEC 1000UL
112*4882a593Smuzhiyun #define USEC_PER_SEC 1000000UL
113*4882a593Smuzhiyun #define USEC_PER_JIFFY (USEC_PER_SEC / HZ)
114*4882a593Smuzhiyun
div64_u64(__u64 dividend,__u64 divisor)115*4882a593Smuzhiyun static __always_inline __u64 div64_u64(__u64 dividend, __u64 divisor)
116*4882a593Smuzhiyun {
117*4882a593Smuzhiyun return dividend / divisor;
118*4882a593Smuzhiyun }
119*4882a593Smuzhiyun
120*4882a593Smuzhiyun #define div64_ul div64_u64
121*4882a593Smuzhiyun
122*4882a593Smuzhiyun #define BITS_PER_U64 (sizeof(__u64) * 8)
fls64(__u64 x)123*4882a593Smuzhiyun static __always_inline int fls64(__u64 x)
124*4882a593Smuzhiyun {
125*4882a593Smuzhiyun int num = BITS_PER_U64 - 1;
126*4882a593Smuzhiyun
127*4882a593Smuzhiyun if (x == 0)
128*4882a593Smuzhiyun return 0;
129*4882a593Smuzhiyun
130*4882a593Smuzhiyun if (!(x & (~0ull << (BITS_PER_U64-32)))) {
131*4882a593Smuzhiyun num -= 32;
132*4882a593Smuzhiyun x <<= 32;
133*4882a593Smuzhiyun }
134*4882a593Smuzhiyun if (!(x & (~0ull << (BITS_PER_U64-16)))) {
135*4882a593Smuzhiyun num -= 16;
136*4882a593Smuzhiyun x <<= 16;
137*4882a593Smuzhiyun }
138*4882a593Smuzhiyun if (!(x & (~0ull << (BITS_PER_U64-8)))) {
139*4882a593Smuzhiyun num -= 8;
140*4882a593Smuzhiyun x <<= 8;
141*4882a593Smuzhiyun }
142*4882a593Smuzhiyun if (!(x & (~0ull << (BITS_PER_U64-4)))) {
143*4882a593Smuzhiyun num -= 4;
144*4882a593Smuzhiyun x <<= 4;
145*4882a593Smuzhiyun }
146*4882a593Smuzhiyun if (!(x & (~0ull << (BITS_PER_U64-2)))) {
147*4882a593Smuzhiyun num -= 2;
148*4882a593Smuzhiyun x <<= 2;
149*4882a593Smuzhiyun }
150*4882a593Smuzhiyun if (!(x & (~0ull << (BITS_PER_U64-1))))
151*4882a593Smuzhiyun num -= 1;
152*4882a593Smuzhiyun
153*4882a593Smuzhiyun return num + 1;
154*4882a593Smuzhiyun }
155*4882a593Smuzhiyun
bictcp_clock_us(const struct sock * sk)156*4882a593Smuzhiyun static __always_inline __u32 bictcp_clock_us(const struct sock *sk)
157*4882a593Smuzhiyun {
158*4882a593Smuzhiyun return tcp_sk(sk)->tcp_mstamp;
159*4882a593Smuzhiyun }
160*4882a593Smuzhiyun
bictcp_hystart_reset(struct sock * sk)161*4882a593Smuzhiyun static __always_inline void bictcp_hystart_reset(struct sock *sk)
162*4882a593Smuzhiyun {
163*4882a593Smuzhiyun struct tcp_sock *tp = tcp_sk(sk);
164*4882a593Smuzhiyun struct bictcp *ca = inet_csk_ca(sk);
165*4882a593Smuzhiyun
166*4882a593Smuzhiyun ca->round_start = ca->last_ack = bictcp_clock_us(sk);
167*4882a593Smuzhiyun ca->end_seq = tp->snd_nxt;
168*4882a593Smuzhiyun ca->curr_rtt = ~0U;
169*4882a593Smuzhiyun ca->sample_cnt = 0;
170*4882a593Smuzhiyun }
171*4882a593Smuzhiyun
172*4882a593Smuzhiyun /* "struct_ops/" prefix is not a requirement
173*4882a593Smuzhiyun * It will be recognized as BPF_PROG_TYPE_STRUCT_OPS
174*4882a593Smuzhiyun * as long as it is used in one of the func ptr
175*4882a593Smuzhiyun * under SEC(".struct_ops").
176*4882a593Smuzhiyun */
177*4882a593Smuzhiyun SEC("struct_ops/bictcp_init")
BPF_PROG(bictcp_init,struct sock * sk)178*4882a593Smuzhiyun void BPF_PROG(bictcp_init, struct sock *sk)
179*4882a593Smuzhiyun {
180*4882a593Smuzhiyun struct bictcp *ca = inet_csk_ca(sk);
181*4882a593Smuzhiyun
182*4882a593Smuzhiyun bictcp_reset(ca);
183*4882a593Smuzhiyun
184*4882a593Smuzhiyun if (hystart)
185*4882a593Smuzhiyun bictcp_hystart_reset(sk);
186*4882a593Smuzhiyun
187*4882a593Smuzhiyun if (!hystart && initial_ssthresh)
188*4882a593Smuzhiyun tcp_sk(sk)->snd_ssthresh = initial_ssthresh;
189*4882a593Smuzhiyun }
190*4882a593Smuzhiyun
191*4882a593Smuzhiyun /* No prefix in SEC will also work.
192*4882a593Smuzhiyun * The remaining tcp-cubic functions have an easier way.
193*4882a593Smuzhiyun */
194*4882a593Smuzhiyun SEC("no-sec-prefix-bictcp_cwnd_event")
BPF_PROG(bictcp_cwnd_event,struct sock * sk,enum tcp_ca_event event)195*4882a593Smuzhiyun void BPF_PROG(bictcp_cwnd_event, struct sock *sk, enum tcp_ca_event event)
196*4882a593Smuzhiyun {
197*4882a593Smuzhiyun if (event == CA_EVENT_TX_START) {
198*4882a593Smuzhiyun struct bictcp *ca = inet_csk_ca(sk);
199*4882a593Smuzhiyun __u32 now = tcp_jiffies32;
200*4882a593Smuzhiyun __s32 delta;
201*4882a593Smuzhiyun
202*4882a593Smuzhiyun delta = now - tcp_sk(sk)->lsndtime;
203*4882a593Smuzhiyun
204*4882a593Smuzhiyun /* We were application limited (idle) for a while.
205*4882a593Smuzhiyun * Shift epoch_start to keep cwnd growth to cubic curve.
206*4882a593Smuzhiyun */
207*4882a593Smuzhiyun if (ca->epoch_start && delta > 0) {
208*4882a593Smuzhiyun ca->epoch_start += delta;
209*4882a593Smuzhiyun if (after(ca->epoch_start, now))
210*4882a593Smuzhiyun ca->epoch_start = now;
211*4882a593Smuzhiyun }
212*4882a593Smuzhiyun return;
213*4882a593Smuzhiyun }
214*4882a593Smuzhiyun }
215*4882a593Smuzhiyun
216*4882a593Smuzhiyun /*
217*4882a593Smuzhiyun * cbrt(x) MSB values for x MSB values in [0..63].
218*4882a593Smuzhiyun * Precomputed then refined by hand - Willy Tarreau
219*4882a593Smuzhiyun *
220*4882a593Smuzhiyun * For x in [0..63],
221*4882a593Smuzhiyun * v = cbrt(x << 18) - 1
222*4882a593Smuzhiyun * cbrt(x) = (v[x] + 10) >> 6
223*4882a593Smuzhiyun */
224*4882a593Smuzhiyun static const __u8 v[] = {
225*4882a593Smuzhiyun /* 0x00 */ 0, 54, 54, 54, 118, 118, 118, 118,
226*4882a593Smuzhiyun /* 0x08 */ 123, 129, 134, 138, 143, 147, 151, 156,
227*4882a593Smuzhiyun /* 0x10 */ 157, 161, 164, 168, 170, 173, 176, 179,
228*4882a593Smuzhiyun /* 0x18 */ 181, 185, 187, 190, 192, 194, 197, 199,
229*4882a593Smuzhiyun /* 0x20 */ 200, 202, 204, 206, 209, 211, 213, 215,
230*4882a593Smuzhiyun /* 0x28 */ 217, 219, 221, 222, 224, 225, 227, 229,
231*4882a593Smuzhiyun /* 0x30 */ 231, 232, 234, 236, 237, 239, 240, 242,
232*4882a593Smuzhiyun /* 0x38 */ 244, 245, 246, 248, 250, 251, 252, 254,
233*4882a593Smuzhiyun };
234*4882a593Smuzhiyun
235*4882a593Smuzhiyun /* calculate the cubic root of x using a table lookup followed by one
236*4882a593Smuzhiyun * Newton-Raphson iteration.
237*4882a593Smuzhiyun * Avg err ~= 0.195%
238*4882a593Smuzhiyun */
cubic_root(__u64 a)239*4882a593Smuzhiyun static __always_inline __u32 cubic_root(__u64 a)
240*4882a593Smuzhiyun {
241*4882a593Smuzhiyun __u32 x, b, shift;
242*4882a593Smuzhiyun
243*4882a593Smuzhiyun if (a < 64) {
244*4882a593Smuzhiyun /* a in [0..63] */
245*4882a593Smuzhiyun return ((__u32)v[(__u32)a] + 35) >> 6;
246*4882a593Smuzhiyun }
247*4882a593Smuzhiyun
248*4882a593Smuzhiyun b = fls64(a);
249*4882a593Smuzhiyun b = ((b * 84) >> 8) - 1;
250*4882a593Smuzhiyun shift = (a >> (b * 3));
251*4882a593Smuzhiyun
252*4882a593Smuzhiyun /* it is needed for verifier's bound check on v */
253*4882a593Smuzhiyun if (shift >= 64)
254*4882a593Smuzhiyun return 0;
255*4882a593Smuzhiyun
256*4882a593Smuzhiyun x = ((__u32)(((__u32)v[shift] + 10) << b)) >> 6;
257*4882a593Smuzhiyun
258*4882a593Smuzhiyun /*
259*4882a593Smuzhiyun * Newton-Raphson iteration
260*4882a593Smuzhiyun * 2
261*4882a593Smuzhiyun * x = ( 2 * x + a / x ) / 3
262*4882a593Smuzhiyun * k+1 k k
263*4882a593Smuzhiyun */
264*4882a593Smuzhiyun x = (2 * x + (__u32)div64_u64(a, (__u64)x * (__u64)(x - 1)));
265*4882a593Smuzhiyun x = ((x * 341) >> 10);
266*4882a593Smuzhiyun return x;
267*4882a593Smuzhiyun }
268*4882a593Smuzhiyun
269*4882a593Smuzhiyun /*
270*4882a593Smuzhiyun * Compute congestion window to use.
271*4882a593Smuzhiyun */
bictcp_update(struct bictcp * ca,__u32 cwnd,__u32 acked)272*4882a593Smuzhiyun static __always_inline void bictcp_update(struct bictcp *ca, __u32 cwnd,
273*4882a593Smuzhiyun __u32 acked)
274*4882a593Smuzhiyun {
275*4882a593Smuzhiyun __u32 delta, bic_target, max_cnt;
276*4882a593Smuzhiyun __u64 offs, t;
277*4882a593Smuzhiyun
278*4882a593Smuzhiyun ca->ack_cnt += acked; /* count the number of ACKed packets */
279*4882a593Smuzhiyun
280*4882a593Smuzhiyun if (ca->last_cwnd == cwnd &&
281*4882a593Smuzhiyun (__s32)(tcp_jiffies32 - ca->last_time) <= HZ / 32)
282*4882a593Smuzhiyun return;
283*4882a593Smuzhiyun
284*4882a593Smuzhiyun /* The CUBIC function can update ca->cnt at most once per jiffy.
285*4882a593Smuzhiyun * On all cwnd reduction events, ca->epoch_start is set to 0,
286*4882a593Smuzhiyun * which will force a recalculation of ca->cnt.
287*4882a593Smuzhiyun */
288*4882a593Smuzhiyun if (ca->epoch_start && tcp_jiffies32 == ca->last_time)
289*4882a593Smuzhiyun goto tcp_friendliness;
290*4882a593Smuzhiyun
291*4882a593Smuzhiyun ca->last_cwnd = cwnd;
292*4882a593Smuzhiyun ca->last_time = tcp_jiffies32;
293*4882a593Smuzhiyun
294*4882a593Smuzhiyun if (ca->epoch_start == 0) {
295*4882a593Smuzhiyun ca->epoch_start = tcp_jiffies32; /* record beginning */
296*4882a593Smuzhiyun ca->ack_cnt = acked; /* start counting */
297*4882a593Smuzhiyun ca->tcp_cwnd = cwnd; /* syn with cubic */
298*4882a593Smuzhiyun
299*4882a593Smuzhiyun if (ca->last_max_cwnd <= cwnd) {
300*4882a593Smuzhiyun ca->bic_K = 0;
301*4882a593Smuzhiyun ca->bic_origin_point = cwnd;
302*4882a593Smuzhiyun } else {
303*4882a593Smuzhiyun /* Compute new K based on
304*4882a593Smuzhiyun * (wmax-cwnd) * (srtt>>3 / HZ) / c * 2^(3*bictcp_HZ)
305*4882a593Smuzhiyun */
306*4882a593Smuzhiyun ca->bic_K = cubic_root(cube_factor
307*4882a593Smuzhiyun * (ca->last_max_cwnd - cwnd));
308*4882a593Smuzhiyun ca->bic_origin_point = ca->last_max_cwnd;
309*4882a593Smuzhiyun }
310*4882a593Smuzhiyun }
311*4882a593Smuzhiyun
312*4882a593Smuzhiyun /* cubic function - calc*/
313*4882a593Smuzhiyun /* calculate c * time^3 / rtt,
314*4882a593Smuzhiyun * while considering overflow in calculation of time^3
315*4882a593Smuzhiyun * (so time^3 is done by using 64 bit)
316*4882a593Smuzhiyun * and without the support of division of 64bit numbers
317*4882a593Smuzhiyun * (so all divisions are done by using 32 bit)
318*4882a593Smuzhiyun * also NOTE the unit of those veriables
319*4882a593Smuzhiyun * time = (t - K) / 2^bictcp_HZ
320*4882a593Smuzhiyun * c = bic_scale >> 10
321*4882a593Smuzhiyun * rtt = (srtt >> 3) / HZ
322*4882a593Smuzhiyun * !!! The following code does not have overflow problems,
323*4882a593Smuzhiyun * if the cwnd < 1 million packets !!!
324*4882a593Smuzhiyun */
325*4882a593Smuzhiyun
326*4882a593Smuzhiyun t = (__s32)(tcp_jiffies32 - ca->epoch_start) * USEC_PER_JIFFY;
327*4882a593Smuzhiyun t += ca->delay_min;
328*4882a593Smuzhiyun /* change the unit from usec to bictcp_HZ */
329*4882a593Smuzhiyun t <<= BICTCP_HZ;
330*4882a593Smuzhiyun t /= USEC_PER_SEC;
331*4882a593Smuzhiyun
332*4882a593Smuzhiyun if (t < ca->bic_K) /* t - K */
333*4882a593Smuzhiyun offs = ca->bic_K - t;
334*4882a593Smuzhiyun else
335*4882a593Smuzhiyun offs = t - ca->bic_K;
336*4882a593Smuzhiyun
337*4882a593Smuzhiyun /* c/rtt * (t-K)^3 */
338*4882a593Smuzhiyun delta = (cube_rtt_scale * offs * offs * offs) >> (10+3*BICTCP_HZ);
339*4882a593Smuzhiyun if (t < ca->bic_K) /* below origin*/
340*4882a593Smuzhiyun bic_target = ca->bic_origin_point - delta;
341*4882a593Smuzhiyun else /* above origin*/
342*4882a593Smuzhiyun bic_target = ca->bic_origin_point + delta;
343*4882a593Smuzhiyun
344*4882a593Smuzhiyun /* cubic function - calc bictcp_cnt*/
345*4882a593Smuzhiyun if (bic_target > cwnd) {
346*4882a593Smuzhiyun ca->cnt = cwnd / (bic_target - cwnd);
347*4882a593Smuzhiyun } else {
348*4882a593Smuzhiyun ca->cnt = 100 * cwnd; /* very small increment*/
349*4882a593Smuzhiyun }
350*4882a593Smuzhiyun
351*4882a593Smuzhiyun /*
352*4882a593Smuzhiyun * The initial growth of cubic function may be too conservative
353*4882a593Smuzhiyun * when the available bandwidth is still unknown.
354*4882a593Smuzhiyun */
355*4882a593Smuzhiyun if (ca->last_max_cwnd == 0 && ca->cnt > 20)
356*4882a593Smuzhiyun ca->cnt = 20; /* increase cwnd 5% per RTT */
357*4882a593Smuzhiyun
358*4882a593Smuzhiyun tcp_friendliness:
359*4882a593Smuzhiyun /* TCP Friendly */
360*4882a593Smuzhiyun if (tcp_friendliness) {
361*4882a593Smuzhiyun __u32 scale = beta_scale;
362*4882a593Smuzhiyun __u32 n;
363*4882a593Smuzhiyun
364*4882a593Smuzhiyun /* update tcp cwnd */
365*4882a593Smuzhiyun delta = (cwnd * scale) >> 3;
366*4882a593Smuzhiyun if (ca->ack_cnt > delta && delta) {
367*4882a593Smuzhiyun n = ca->ack_cnt / delta;
368*4882a593Smuzhiyun ca->ack_cnt -= n * delta;
369*4882a593Smuzhiyun ca->tcp_cwnd += n;
370*4882a593Smuzhiyun }
371*4882a593Smuzhiyun
372*4882a593Smuzhiyun if (ca->tcp_cwnd > cwnd) { /* if bic is slower than tcp */
373*4882a593Smuzhiyun delta = ca->tcp_cwnd - cwnd;
374*4882a593Smuzhiyun max_cnt = cwnd / delta;
375*4882a593Smuzhiyun if (ca->cnt > max_cnt)
376*4882a593Smuzhiyun ca->cnt = max_cnt;
377*4882a593Smuzhiyun }
378*4882a593Smuzhiyun }
379*4882a593Smuzhiyun
380*4882a593Smuzhiyun /* The maximum rate of cwnd increase CUBIC allows is 1 packet per
381*4882a593Smuzhiyun * 2 packets ACKed, meaning cwnd grows at 1.5x per RTT.
382*4882a593Smuzhiyun */
383*4882a593Smuzhiyun ca->cnt = max(ca->cnt, 2U);
384*4882a593Smuzhiyun }
385*4882a593Smuzhiyun
386*4882a593Smuzhiyun /* Or simply use the BPF_STRUCT_OPS to avoid the SEC boiler plate. */
BPF_STRUCT_OPS(bictcp_cong_avoid,struct sock * sk,__u32 ack,__u32 acked)387*4882a593Smuzhiyun void BPF_STRUCT_OPS(bictcp_cong_avoid, struct sock *sk, __u32 ack, __u32 acked)
388*4882a593Smuzhiyun {
389*4882a593Smuzhiyun struct tcp_sock *tp = tcp_sk(sk);
390*4882a593Smuzhiyun struct bictcp *ca = inet_csk_ca(sk);
391*4882a593Smuzhiyun
392*4882a593Smuzhiyun if (!tcp_is_cwnd_limited(sk))
393*4882a593Smuzhiyun return;
394*4882a593Smuzhiyun
395*4882a593Smuzhiyun if (tcp_in_slow_start(tp)) {
396*4882a593Smuzhiyun if (hystart && after(ack, ca->end_seq))
397*4882a593Smuzhiyun bictcp_hystart_reset(sk);
398*4882a593Smuzhiyun acked = tcp_slow_start(tp, acked);
399*4882a593Smuzhiyun if (!acked)
400*4882a593Smuzhiyun return;
401*4882a593Smuzhiyun }
402*4882a593Smuzhiyun bictcp_update(ca, tp->snd_cwnd, acked);
403*4882a593Smuzhiyun tcp_cong_avoid_ai(tp, ca->cnt, acked);
404*4882a593Smuzhiyun }
405*4882a593Smuzhiyun
BPF_STRUCT_OPS(bictcp_recalc_ssthresh,struct sock * sk)406*4882a593Smuzhiyun __u32 BPF_STRUCT_OPS(bictcp_recalc_ssthresh, struct sock *sk)
407*4882a593Smuzhiyun {
408*4882a593Smuzhiyun const struct tcp_sock *tp = tcp_sk(sk);
409*4882a593Smuzhiyun struct bictcp *ca = inet_csk_ca(sk);
410*4882a593Smuzhiyun
411*4882a593Smuzhiyun ca->epoch_start = 0; /* end of epoch */
412*4882a593Smuzhiyun
413*4882a593Smuzhiyun /* Wmax and fast convergence */
414*4882a593Smuzhiyun if (tp->snd_cwnd < ca->last_max_cwnd && fast_convergence)
415*4882a593Smuzhiyun ca->last_max_cwnd = (tp->snd_cwnd * (BICTCP_BETA_SCALE + beta))
416*4882a593Smuzhiyun / (2 * BICTCP_BETA_SCALE);
417*4882a593Smuzhiyun else
418*4882a593Smuzhiyun ca->last_max_cwnd = tp->snd_cwnd;
419*4882a593Smuzhiyun
420*4882a593Smuzhiyun return max((tp->snd_cwnd * beta) / BICTCP_BETA_SCALE, 2U);
421*4882a593Smuzhiyun }
422*4882a593Smuzhiyun
BPF_STRUCT_OPS(bictcp_state,struct sock * sk,__u8 new_state)423*4882a593Smuzhiyun void BPF_STRUCT_OPS(bictcp_state, struct sock *sk, __u8 new_state)
424*4882a593Smuzhiyun {
425*4882a593Smuzhiyun if (new_state == TCP_CA_Loss) {
426*4882a593Smuzhiyun bictcp_reset(inet_csk_ca(sk));
427*4882a593Smuzhiyun bictcp_hystart_reset(sk);
428*4882a593Smuzhiyun }
429*4882a593Smuzhiyun }
430*4882a593Smuzhiyun
431*4882a593Smuzhiyun #define GSO_MAX_SIZE 65536
432*4882a593Smuzhiyun
433*4882a593Smuzhiyun /* Account for TSO/GRO delays.
434*4882a593Smuzhiyun * Otherwise short RTT flows could get too small ssthresh, since during
435*4882a593Smuzhiyun * slow start we begin with small TSO packets and ca->delay_min would
436*4882a593Smuzhiyun * not account for long aggregation delay when TSO packets get bigger.
437*4882a593Smuzhiyun * Ideally even with a very small RTT we would like to have at least one
438*4882a593Smuzhiyun * TSO packet being sent and received by GRO, and another one in qdisc layer.
439*4882a593Smuzhiyun * We apply another 100% factor because @rate is doubled at this point.
440*4882a593Smuzhiyun * We cap the cushion to 1ms.
441*4882a593Smuzhiyun */
hystart_ack_delay(struct sock * sk)442*4882a593Smuzhiyun static __always_inline __u32 hystart_ack_delay(struct sock *sk)
443*4882a593Smuzhiyun {
444*4882a593Smuzhiyun unsigned long rate;
445*4882a593Smuzhiyun
446*4882a593Smuzhiyun rate = sk->sk_pacing_rate;
447*4882a593Smuzhiyun if (!rate)
448*4882a593Smuzhiyun return 0;
449*4882a593Smuzhiyun return min((__u64)USEC_PER_MSEC,
450*4882a593Smuzhiyun div64_ul((__u64)GSO_MAX_SIZE * 4 * USEC_PER_SEC, rate));
451*4882a593Smuzhiyun }
452*4882a593Smuzhiyun
hystart_update(struct sock * sk,__u32 delay)453*4882a593Smuzhiyun static __always_inline void hystart_update(struct sock *sk, __u32 delay)
454*4882a593Smuzhiyun {
455*4882a593Smuzhiyun struct tcp_sock *tp = tcp_sk(sk);
456*4882a593Smuzhiyun struct bictcp *ca = inet_csk_ca(sk);
457*4882a593Smuzhiyun __u32 threshold;
458*4882a593Smuzhiyun
459*4882a593Smuzhiyun if (hystart_detect & HYSTART_ACK_TRAIN) {
460*4882a593Smuzhiyun __u32 now = bictcp_clock_us(sk);
461*4882a593Smuzhiyun
462*4882a593Smuzhiyun /* first detection parameter - ack-train detection */
463*4882a593Smuzhiyun if ((__s32)(now - ca->last_ack) <= hystart_ack_delta_us) {
464*4882a593Smuzhiyun ca->last_ack = now;
465*4882a593Smuzhiyun
466*4882a593Smuzhiyun threshold = ca->delay_min + hystart_ack_delay(sk);
467*4882a593Smuzhiyun
468*4882a593Smuzhiyun /* Hystart ack train triggers if we get ack past
469*4882a593Smuzhiyun * ca->delay_min/2.
470*4882a593Smuzhiyun * Pacing might have delayed packets up to RTT/2
471*4882a593Smuzhiyun * during slow start.
472*4882a593Smuzhiyun */
473*4882a593Smuzhiyun if (sk->sk_pacing_status == SK_PACING_NONE)
474*4882a593Smuzhiyun threshold >>= 1;
475*4882a593Smuzhiyun
476*4882a593Smuzhiyun if ((__s32)(now - ca->round_start) > threshold) {
477*4882a593Smuzhiyun ca->found = 1;
478*4882a593Smuzhiyun tp->snd_ssthresh = tp->snd_cwnd;
479*4882a593Smuzhiyun }
480*4882a593Smuzhiyun }
481*4882a593Smuzhiyun }
482*4882a593Smuzhiyun
483*4882a593Smuzhiyun if (hystart_detect & HYSTART_DELAY) {
484*4882a593Smuzhiyun /* obtain the minimum delay of more than sampling packets */
485*4882a593Smuzhiyun if (ca->curr_rtt > delay)
486*4882a593Smuzhiyun ca->curr_rtt = delay;
487*4882a593Smuzhiyun if (ca->sample_cnt < HYSTART_MIN_SAMPLES) {
488*4882a593Smuzhiyun ca->sample_cnt++;
489*4882a593Smuzhiyun } else {
490*4882a593Smuzhiyun if (ca->curr_rtt > ca->delay_min +
491*4882a593Smuzhiyun HYSTART_DELAY_THRESH(ca->delay_min >> 3)) {
492*4882a593Smuzhiyun ca->found = 1;
493*4882a593Smuzhiyun tp->snd_ssthresh = tp->snd_cwnd;
494*4882a593Smuzhiyun }
495*4882a593Smuzhiyun }
496*4882a593Smuzhiyun }
497*4882a593Smuzhiyun }
498*4882a593Smuzhiyun
BPF_STRUCT_OPS(bictcp_acked,struct sock * sk,const struct ack_sample * sample)499*4882a593Smuzhiyun void BPF_STRUCT_OPS(bictcp_acked, struct sock *sk,
500*4882a593Smuzhiyun const struct ack_sample *sample)
501*4882a593Smuzhiyun {
502*4882a593Smuzhiyun const struct tcp_sock *tp = tcp_sk(sk);
503*4882a593Smuzhiyun struct bictcp *ca = inet_csk_ca(sk);
504*4882a593Smuzhiyun __u32 delay;
505*4882a593Smuzhiyun
506*4882a593Smuzhiyun /* Some calls are for duplicates without timetamps */
507*4882a593Smuzhiyun if (sample->rtt_us < 0)
508*4882a593Smuzhiyun return;
509*4882a593Smuzhiyun
510*4882a593Smuzhiyun /* Discard delay samples right after fast recovery */
511*4882a593Smuzhiyun if (ca->epoch_start && (__s32)(tcp_jiffies32 - ca->epoch_start) < HZ)
512*4882a593Smuzhiyun return;
513*4882a593Smuzhiyun
514*4882a593Smuzhiyun delay = sample->rtt_us;
515*4882a593Smuzhiyun if (delay == 0)
516*4882a593Smuzhiyun delay = 1;
517*4882a593Smuzhiyun
518*4882a593Smuzhiyun /* first time call or link delay decreases */
519*4882a593Smuzhiyun if (ca->delay_min == 0 || ca->delay_min > delay)
520*4882a593Smuzhiyun ca->delay_min = delay;
521*4882a593Smuzhiyun
522*4882a593Smuzhiyun /* hystart triggers when cwnd is larger than some threshold */
523*4882a593Smuzhiyun if (!ca->found && tcp_in_slow_start(tp) && hystart &&
524*4882a593Smuzhiyun tp->snd_cwnd >= hystart_low_window)
525*4882a593Smuzhiyun hystart_update(sk, delay);
526*4882a593Smuzhiyun }
527*4882a593Smuzhiyun
BPF_STRUCT_OPS(tcp_reno_undo_cwnd,struct sock * sk)528*4882a593Smuzhiyun __u32 BPF_STRUCT_OPS(tcp_reno_undo_cwnd, struct sock *sk)
529*4882a593Smuzhiyun {
530*4882a593Smuzhiyun const struct tcp_sock *tp = tcp_sk(sk);
531*4882a593Smuzhiyun
532*4882a593Smuzhiyun return max(tp->snd_cwnd, tp->prior_cwnd);
533*4882a593Smuzhiyun }
534*4882a593Smuzhiyun
535*4882a593Smuzhiyun SEC(".struct_ops")
536*4882a593Smuzhiyun struct tcp_congestion_ops cubic = {
537*4882a593Smuzhiyun .init = (void *)bictcp_init,
538*4882a593Smuzhiyun .ssthresh = (void *)bictcp_recalc_ssthresh,
539*4882a593Smuzhiyun .cong_avoid = (void *)bictcp_cong_avoid,
540*4882a593Smuzhiyun .set_state = (void *)bictcp_state,
541*4882a593Smuzhiyun .undo_cwnd = (void *)tcp_reno_undo_cwnd,
542*4882a593Smuzhiyun .cwnd_event = (void *)bictcp_cwnd_event,
543*4882a593Smuzhiyun .pkts_acked = (void *)bictcp_acked,
544*4882a593Smuzhiyun .name = "bpf_cubic",
545*4882a593Smuzhiyun };
546