1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Binary Increase Congestion control for TCP
4*4882a593Smuzhiyun * Home page:
5*4882a593Smuzhiyun * http://netsrv.csc.ncsu.edu/twiki/bin/view/Main/BIC
6*4882a593Smuzhiyun * This is from the implementation of BICTCP in
7*4882a593Smuzhiyun * Lison-Xu, Kahaled Harfoush, and Injong Rhee.
8*4882a593Smuzhiyun * "Binary Increase Congestion Control for Fast, Long Distance
9*4882a593Smuzhiyun * Networks" in InfoComm 2004
10*4882a593Smuzhiyun * Available from:
11*4882a593Smuzhiyun * http://netsrv.csc.ncsu.edu/export/bitcp.pdf
12*4882a593Smuzhiyun *
13*4882a593Smuzhiyun * Unless BIC is enabled and congestion window is large
14*4882a593Smuzhiyun * this behaves the same as the original Reno.
15*4882a593Smuzhiyun */
16*4882a593Smuzhiyun
17*4882a593Smuzhiyun #include <linux/mm.h>
18*4882a593Smuzhiyun #include <linux/module.h>
19*4882a593Smuzhiyun #include <net/tcp.h>
20*4882a593Smuzhiyun
21*4882a593Smuzhiyun #define BICTCP_BETA_SCALE 1024 /* Scale factor beta calculation
22*4882a593Smuzhiyun * max_cwnd = snd_cwnd * beta
23*4882a593Smuzhiyun */
24*4882a593Smuzhiyun #define BICTCP_B 4 /*
25*4882a593Smuzhiyun * In binary search,
26*4882a593Smuzhiyun * go to point (max+min)/N
27*4882a593Smuzhiyun */
28*4882a593Smuzhiyun
29*4882a593Smuzhiyun static int fast_convergence = 1;
30*4882a593Smuzhiyun static int max_increment = 16;
31*4882a593Smuzhiyun static int low_window = 14;
32*4882a593Smuzhiyun static int beta = 819; /* = 819/1024 (BICTCP_BETA_SCALE) */
33*4882a593Smuzhiyun static int initial_ssthresh;
34*4882a593Smuzhiyun static int smooth_part = 20;
35*4882a593Smuzhiyun
36*4882a593Smuzhiyun module_param(fast_convergence, int, 0644);
37*4882a593Smuzhiyun MODULE_PARM_DESC(fast_convergence, "turn on/off fast convergence");
38*4882a593Smuzhiyun module_param(max_increment, int, 0644);
39*4882a593Smuzhiyun MODULE_PARM_DESC(max_increment, "Limit on increment allowed during binary search");
40*4882a593Smuzhiyun module_param(low_window, int, 0644);
41*4882a593Smuzhiyun MODULE_PARM_DESC(low_window, "lower bound on congestion window (for TCP friendliness)");
42*4882a593Smuzhiyun module_param(beta, int, 0644);
43*4882a593Smuzhiyun MODULE_PARM_DESC(beta, "beta for multiplicative increase");
44*4882a593Smuzhiyun module_param(initial_ssthresh, int, 0644);
45*4882a593Smuzhiyun MODULE_PARM_DESC(initial_ssthresh, "initial value of slow start threshold");
46*4882a593Smuzhiyun module_param(smooth_part, int, 0644);
47*4882a593Smuzhiyun MODULE_PARM_DESC(smooth_part, "log(B/(B*Smin))/log(B/(B-1))+B, # of RTT from Wmax-B to Wmax");
48*4882a593Smuzhiyun
49*4882a593Smuzhiyun /* BIC TCP Parameters */
50*4882a593Smuzhiyun struct bictcp {
51*4882a593Smuzhiyun u32 cnt; /* increase cwnd by 1 after ACKs */
52*4882a593Smuzhiyun u32 last_max_cwnd; /* last maximum snd_cwnd */
53*4882a593Smuzhiyun u32 last_cwnd; /* the last snd_cwnd */
54*4882a593Smuzhiyun u32 last_time; /* time when updated last_cwnd */
55*4882a593Smuzhiyun u32 epoch_start; /* beginning of an epoch */
56*4882a593Smuzhiyun #define ACK_RATIO_SHIFT 4
57*4882a593Smuzhiyun u32 delayed_ack; /* estimate the ratio of Packets/ACKs << 4 */
58*4882a593Smuzhiyun };
59*4882a593Smuzhiyun
bictcp_reset(struct bictcp * ca)60*4882a593Smuzhiyun static inline void bictcp_reset(struct bictcp *ca)
61*4882a593Smuzhiyun {
62*4882a593Smuzhiyun ca->cnt = 0;
63*4882a593Smuzhiyun ca->last_max_cwnd = 0;
64*4882a593Smuzhiyun ca->last_cwnd = 0;
65*4882a593Smuzhiyun ca->last_time = 0;
66*4882a593Smuzhiyun ca->epoch_start = 0;
67*4882a593Smuzhiyun ca->delayed_ack = 2 << ACK_RATIO_SHIFT;
68*4882a593Smuzhiyun }
69*4882a593Smuzhiyun
bictcp_init(struct sock * sk)70*4882a593Smuzhiyun static void bictcp_init(struct sock *sk)
71*4882a593Smuzhiyun {
72*4882a593Smuzhiyun struct bictcp *ca = inet_csk_ca(sk);
73*4882a593Smuzhiyun
74*4882a593Smuzhiyun bictcp_reset(ca);
75*4882a593Smuzhiyun
76*4882a593Smuzhiyun if (initial_ssthresh)
77*4882a593Smuzhiyun tcp_sk(sk)->snd_ssthresh = initial_ssthresh;
78*4882a593Smuzhiyun }
79*4882a593Smuzhiyun
80*4882a593Smuzhiyun /*
81*4882a593Smuzhiyun * Compute congestion window to use.
82*4882a593Smuzhiyun */
bictcp_update(struct bictcp * ca,u32 cwnd)83*4882a593Smuzhiyun static inline void bictcp_update(struct bictcp *ca, u32 cwnd)
84*4882a593Smuzhiyun {
85*4882a593Smuzhiyun if (ca->last_cwnd == cwnd &&
86*4882a593Smuzhiyun (s32)(tcp_jiffies32 - ca->last_time) <= HZ / 32)
87*4882a593Smuzhiyun return;
88*4882a593Smuzhiyun
89*4882a593Smuzhiyun ca->last_cwnd = cwnd;
90*4882a593Smuzhiyun ca->last_time = tcp_jiffies32;
91*4882a593Smuzhiyun
92*4882a593Smuzhiyun if (ca->epoch_start == 0) /* record the beginning of an epoch */
93*4882a593Smuzhiyun ca->epoch_start = tcp_jiffies32;
94*4882a593Smuzhiyun
95*4882a593Smuzhiyun /* start off normal */
96*4882a593Smuzhiyun if (cwnd <= low_window) {
97*4882a593Smuzhiyun ca->cnt = cwnd;
98*4882a593Smuzhiyun return;
99*4882a593Smuzhiyun }
100*4882a593Smuzhiyun
101*4882a593Smuzhiyun /* binary increase */
102*4882a593Smuzhiyun if (cwnd < ca->last_max_cwnd) {
103*4882a593Smuzhiyun __u32 dist = (ca->last_max_cwnd - cwnd)
104*4882a593Smuzhiyun / BICTCP_B;
105*4882a593Smuzhiyun
106*4882a593Smuzhiyun if (dist > max_increment)
107*4882a593Smuzhiyun /* linear increase */
108*4882a593Smuzhiyun ca->cnt = cwnd / max_increment;
109*4882a593Smuzhiyun else if (dist <= 1U)
110*4882a593Smuzhiyun /* binary search increase */
111*4882a593Smuzhiyun ca->cnt = (cwnd * smooth_part) / BICTCP_B;
112*4882a593Smuzhiyun else
113*4882a593Smuzhiyun /* binary search increase */
114*4882a593Smuzhiyun ca->cnt = cwnd / dist;
115*4882a593Smuzhiyun } else {
116*4882a593Smuzhiyun /* slow start AMD linear increase */
117*4882a593Smuzhiyun if (cwnd < ca->last_max_cwnd + BICTCP_B)
118*4882a593Smuzhiyun /* slow start */
119*4882a593Smuzhiyun ca->cnt = (cwnd * smooth_part) / BICTCP_B;
120*4882a593Smuzhiyun else if (cwnd < ca->last_max_cwnd + max_increment*(BICTCP_B-1))
121*4882a593Smuzhiyun /* slow start */
122*4882a593Smuzhiyun ca->cnt = (cwnd * (BICTCP_B-1))
123*4882a593Smuzhiyun / (cwnd - ca->last_max_cwnd);
124*4882a593Smuzhiyun else
125*4882a593Smuzhiyun /* linear increase */
126*4882a593Smuzhiyun ca->cnt = cwnd / max_increment;
127*4882a593Smuzhiyun }
128*4882a593Smuzhiyun
129*4882a593Smuzhiyun /* if in slow start or link utilization is very low */
130*4882a593Smuzhiyun if (ca->last_max_cwnd == 0) {
131*4882a593Smuzhiyun if (ca->cnt > 20) /* increase cwnd 5% per RTT */
132*4882a593Smuzhiyun ca->cnt = 20;
133*4882a593Smuzhiyun }
134*4882a593Smuzhiyun
135*4882a593Smuzhiyun ca->cnt = (ca->cnt << ACK_RATIO_SHIFT) / ca->delayed_ack;
136*4882a593Smuzhiyun if (ca->cnt == 0) /* cannot be zero */
137*4882a593Smuzhiyun ca->cnt = 1;
138*4882a593Smuzhiyun }
139*4882a593Smuzhiyun
bictcp_cong_avoid(struct sock * sk,u32 ack,u32 acked)140*4882a593Smuzhiyun static void bictcp_cong_avoid(struct sock *sk, u32 ack, u32 acked)
141*4882a593Smuzhiyun {
142*4882a593Smuzhiyun struct tcp_sock *tp = tcp_sk(sk);
143*4882a593Smuzhiyun struct bictcp *ca = inet_csk_ca(sk);
144*4882a593Smuzhiyun
145*4882a593Smuzhiyun if (!tcp_is_cwnd_limited(sk))
146*4882a593Smuzhiyun return;
147*4882a593Smuzhiyun
148*4882a593Smuzhiyun if (tcp_in_slow_start(tp)) {
149*4882a593Smuzhiyun acked = tcp_slow_start(tp, acked);
150*4882a593Smuzhiyun if (!acked)
151*4882a593Smuzhiyun return;
152*4882a593Smuzhiyun }
153*4882a593Smuzhiyun bictcp_update(ca, tp->snd_cwnd);
154*4882a593Smuzhiyun tcp_cong_avoid_ai(tp, ca->cnt, acked);
155*4882a593Smuzhiyun }
156*4882a593Smuzhiyun
157*4882a593Smuzhiyun /*
158*4882a593Smuzhiyun * behave like Reno until low_window is reached,
159*4882a593Smuzhiyun * then increase congestion window slowly
160*4882a593Smuzhiyun */
bictcp_recalc_ssthresh(struct sock * sk)161*4882a593Smuzhiyun static u32 bictcp_recalc_ssthresh(struct sock *sk)
162*4882a593Smuzhiyun {
163*4882a593Smuzhiyun const struct tcp_sock *tp = tcp_sk(sk);
164*4882a593Smuzhiyun struct bictcp *ca = inet_csk_ca(sk);
165*4882a593Smuzhiyun
166*4882a593Smuzhiyun ca->epoch_start = 0; /* end of epoch */
167*4882a593Smuzhiyun
168*4882a593Smuzhiyun /* Wmax and fast convergence */
169*4882a593Smuzhiyun if (tp->snd_cwnd < ca->last_max_cwnd && fast_convergence)
170*4882a593Smuzhiyun ca->last_max_cwnd = (tp->snd_cwnd * (BICTCP_BETA_SCALE + beta))
171*4882a593Smuzhiyun / (2 * BICTCP_BETA_SCALE);
172*4882a593Smuzhiyun else
173*4882a593Smuzhiyun ca->last_max_cwnd = tp->snd_cwnd;
174*4882a593Smuzhiyun
175*4882a593Smuzhiyun if (tp->snd_cwnd <= low_window)
176*4882a593Smuzhiyun return max(tp->snd_cwnd >> 1U, 2U);
177*4882a593Smuzhiyun else
178*4882a593Smuzhiyun return max((tp->snd_cwnd * beta) / BICTCP_BETA_SCALE, 2U);
179*4882a593Smuzhiyun }
180*4882a593Smuzhiyun
bictcp_state(struct sock * sk,u8 new_state)181*4882a593Smuzhiyun static void bictcp_state(struct sock *sk, u8 new_state)
182*4882a593Smuzhiyun {
183*4882a593Smuzhiyun if (new_state == TCP_CA_Loss)
184*4882a593Smuzhiyun bictcp_reset(inet_csk_ca(sk));
185*4882a593Smuzhiyun }
186*4882a593Smuzhiyun
187*4882a593Smuzhiyun /* Track delayed acknowledgment ratio using sliding window
188*4882a593Smuzhiyun * ratio = (15*ratio + sample) / 16
189*4882a593Smuzhiyun */
bictcp_acked(struct sock * sk,const struct ack_sample * sample)190*4882a593Smuzhiyun static void bictcp_acked(struct sock *sk, const struct ack_sample *sample)
191*4882a593Smuzhiyun {
192*4882a593Smuzhiyun const struct inet_connection_sock *icsk = inet_csk(sk);
193*4882a593Smuzhiyun
194*4882a593Smuzhiyun if (icsk->icsk_ca_state == TCP_CA_Open) {
195*4882a593Smuzhiyun struct bictcp *ca = inet_csk_ca(sk);
196*4882a593Smuzhiyun
197*4882a593Smuzhiyun ca->delayed_ack += sample->pkts_acked -
198*4882a593Smuzhiyun (ca->delayed_ack >> ACK_RATIO_SHIFT);
199*4882a593Smuzhiyun }
200*4882a593Smuzhiyun }
201*4882a593Smuzhiyun
202*4882a593Smuzhiyun static struct tcp_congestion_ops bictcp __read_mostly = {
203*4882a593Smuzhiyun .init = bictcp_init,
204*4882a593Smuzhiyun .ssthresh = bictcp_recalc_ssthresh,
205*4882a593Smuzhiyun .cong_avoid = bictcp_cong_avoid,
206*4882a593Smuzhiyun .set_state = bictcp_state,
207*4882a593Smuzhiyun .undo_cwnd = tcp_reno_undo_cwnd,
208*4882a593Smuzhiyun .pkts_acked = bictcp_acked,
209*4882a593Smuzhiyun .owner = THIS_MODULE,
210*4882a593Smuzhiyun .name = "bic",
211*4882a593Smuzhiyun };
212*4882a593Smuzhiyun
bictcp_register(void)213*4882a593Smuzhiyun static int __init bictcp_register(void)
214*4882a593Smuzhiyun {
215*4882a593Smuzhiyun BUILD_BUG_ON(sizeof(struct bictcp) > ICSK_CA_PRIV_SIZE);
216*4882a593Smuzhiyun return tcp_register_congestion_control(&bictcp);
217*4882a593Smuzhiyun }
218*4882a593Smuzhiyun
bictcp_unregister(void)219*4882a593Smuzhiyun static void __exit bictcp_unregister(void)
220*4882a593Smuzhiyun {
221*4882a593Smuzhiyun tcp_unregister_congestion_control(&bictcp);
222*4882a593Smuzhiyun }
223*4882a593Smuzhiyun
224*4882a593Smuzhiyun module_init(bictcp_register);
225*4882a593Smuzhiyun module_exit(bictcp_unregister);
226*4882a593Smuzhiyun
227*4882a593Smuzhiyun MODULE_AUTHOR("Stephen Hemminger");
228*4882a593Smuzhiyun MODULE_LICENSE("GPL");
229*4882a593Smuzhiyun MODULE_DESCRIPTION("BIC TCP");
230