xref: /OK3568_Linux_fs/kernel/net/ipv4/tcp_hybla.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * TCP HYBLA
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * TCP-HYBLA Congestion control algorithm, based on:
6*4882a593Smuzhiyun  *   C.Caini, R.Firrincieli, "TCP-Hybla: A TCP Enhancement
7*4882a593Smuzhiyun  *   for Heterogeneous Networks",
8*4882a593Smuzhiyun  *   International Journal on satellite Communications,
9*4882a593Smuzhiyun  *				       September 2004
10*4882a593Smuzhiyun  *    Daniele Lacamera
11*4882a593Smuzhiyun  *    root at danielinux.net
12*4882a593Smuzhiyun  */
13*4882a593Smuzhiyun 
14*4882a593Smuzhiyun #include <linux/module.h>
15*4882a593Smuzhiyun #include <net/tcp.h>
16*4882a593Smuzhiyun 
17*4882a593Smuzhiyun /* Tcp Hybla structure. */
18*4882a593Smuzhiyun struct hybla {
19*4882a593Smuzhiyun 	bool  hybla_en;
20*4882a593Smuzhiyun 	u32   snd_cwnd_cents; /* Keeps increment values when it is <1, <<7 */
21*4882a593Smuzhiyun 	u32   rho;	      /* Rho parameter, integer part  */
22*4882a593Smuzhiyun 	u32   rho2;	      /* Rho * Rho, integer part */
23*4882a593Smuzhiyun 	u32   rho_3ls;	      /* Rho parameter, <<3 */
24*4882a593Smuzhiyun 	u32   rho2_7ls;	      /* Rho^2, <<7	*/
25*4882a593Smuzhiyun 	u32   minrtt_us;      /* Minimum smoothed round trip time value seen */
26*4882a593Smuzhiyun };
27*4882a593Smuzhiyun 
28*4882a593Smuzhiyun /* Hybla reference round trip time (default= 1/40 sec = 25 ms), in ms */
29*4882a593Smuzhiyun static int rtt0 = 25;
30*4882a593Smuzhiyun module_param(rtt0, int, 0644);
31*4882a593Smuzhiyun MODULE_PARM_DESC(rtt0, "reference rout trip time (ms)");
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun /* This is called to refresh values for hybla parameters */
hybla_recalc_param(struct sock * sk)34*4882a593Smuzhiyun static inline void hybla_recalc_param (struct sock *sk)
35*4882a593Smuzhiyun {
36*4882a593Smuzhiyun 	struct hybla *ca = inet_csk_ca(sk);
37*4882a593Smuzhiyun 
38*4882a593Smuzhiyun 	ca->rho_3ls = max_t(u32,
39*4882a593Smuzhiyun 			    tcp_sk(sk)->srtt_us / (rtt0 * USEC_PER_MSEC),
40*4882a593Smuzhiyun 			    8U);
41*4882a593Smuzhiyun 	ca->rho = ca->rho_3ls >> 3;
42*4882a593Smuzhiyun 	ca->rho2_7ls = (ca->rho_3ls * ca->rho_3ls) << 1;
43*4882a593Smuzhiyun 	ca->rho2 = ca->rho2_7ls >> 7;
44*4882a593Smuzhiyun }
45*4882a593Smuzhiyun 
hybla_init(struct sock * sk)46*4882a593Smuzhiyun static void hybla_init(struct sock *sk)
47*4882a593Smuzhiyun {
48*4882a593Smuzhiyun 	struct tcp_sock *tp = tcp_sk(sk);
49*4882a593Smuzhiyun 	struct hybla *ca = inet_csk_ca(sk);
50*4882a593Smuzhiyun 
51*4882a593Smuzhiyun 	ca->rho = 0;
52*4882a593Smuzhiyun 	ca->rho2 = 0;
53*4882a593Smuzhiyun 	ca->rho_3ls = 0;
54*4882a593Smuzhiyun 	ca->rho2_7ls = 0;
55*4882a593Smuzhiyun 	ca->snd_cwnd_cents = 0;
56*4882a593Smuzhiyun 	ca->hybla_en = true;
57*4882a593Smuzhiyun 	tp->snd_cwnd = 2;
58*4882a593Smuzhiyun 	tp->snd_cwnd_clamp = 65535;
59*4882a593Smuzhiyun 
60*4882a593Smuzhiyun 	/* 1st Rho measurement based on initial srtt */
61*4882a593Smuzhiyun 	hybla_recalc_param(sk);
62*4882a593Smuzhiyun 
63*4882a593Smuzhiyun 	/* set minimum rtt as this is the 1st ever seen */
64*4882a593Smuzhiyun 	ca->minrtt_us = tp->srtt_us;
65*4882a593Smuzhiyun 	tp->snd_cwnd = ca->rho;
66*4882a593Smuzhiyun }
67*4882a593Smuzhiyun 
hybla_state(struct sock * sk,u8 ca_state)68*4882a593Smuzhiyun static void hybla_state(struct sock *sk, u8 ca_state)
69*4882a593Smuzhiyun {
70*4882a593Smuzhiyun 	struct hybla *ca = inet_csk_ca(sk);
71*4882a593Smuzhiyun 
72*4882a593Smuzhiyun 	ca->hybla_en = (ca_state == TCP_CA_Open);
73*4882a593Smuzhiyun }
74*4882a593Smuzhiyun 
hybla_fraction(u32 odds)75*4882a593Smuzhiyun static inline u32 hybla_fraction(u32 odds)
76*4882a593Smuzhiyun {
77*4882a593Smuzhiyun 	static const u32 fractions[] = {
78*4882a593Smuzhiyun 		128, 139, 152, 165, 181, 197, 215, 234,
79*4882a593Smuzhiyun 	};
80*4882a593Smuzhiyun 
81*4882a593Smuzhiyun 	return (odds < ARRAY_SIZE(fractions)) ? fractions[odds] : 128;
82*4882a593Smuzhiyun }
83*4882a593Smuzhiyun 
84*4882a593Smuzhiyun /* TCP Hybla main routine.
85*4882a593Smuzhiyun  * This is the algorithm behavior:
86*4882a593Smuzhiyun  *     o Recalc Hybla parameters if min_rtt has changed
87*4882a593Smuzhiyun  *     o Give cwnd a new value based on the model proposed
88*4882a593Smuzhiyun  *     o remember increments <1
89*4882a593Smuzhiyun  */
hybla_cong_avoid(struct sock * sk,u32 ack,u32 acked)90*4882a593Smuzhiyun static void hybla_cong_avoid(struct sock *sk, u32 ack, u32 acked)
91*4882a593Smuzhiyun {
92*4882a593Smuzhiyun 	struct tcp_sock *tp = tcp_sk(sk);
93*4882a593Smuzhiyun 	struct hybla *ca = inet_csk_ca(sk);
94*4882a593Smuzhiyun 	u32 increment, odd, rho_fractions;
95*4882a593Smuzhiyun 	int is_slowstart = 0;
96*4882a593Smuzhiyun 
97*4882a593Smuzhiyun 	/*  Recalculate rho only if this srtt is the lowest */
98*4882a593Smuzhiyun 	if (tp->srtt_us < ca->minrtt_us) {
99*4882a593Smuzhiyun 		hybla_recalc_param(sk);
100*4882a593Smuzhiyun 		ca->minrtt_us = tp->srtt_us;
101*4882a593Smuzhiyun 	}
102*4882a593Smuzhiyun 
103*4882a593Smuzhiyun 	if (!tcp_is_cwnd_limited(sk))
104*4882a593Smuzhiyun 		return;
105*4882a593Smuzhiyun 
106*4882a593Smuzhiyun 	if (!ca->hybla_en) {
107*4882a593Smuzhiyun 		tcp_reno_cong_avoid(sk, ack, acked);
108*4882a593Smuzhiyun 		return;
109*4882a593Smuzhiyun 	}
110*4882a593Smuzhiyun 
111*4882a593Smuzhiyun 	if (ca->rho == 0)
112*4882a593Smuzhiyun 		hybla_recalc_param(sk);
113*4882a593Smuzhiyun 
114*4882a593Smuzhiyun 	rho_fractions = ca->rho_3ls - (ca->rho << 3);
115*4882a593Smuzhiyun 
116*4882a593Smuzhiyun 	if (tcp_in_slow_start(tp)) {
117*4882a593Smuzhiyun 		/*
118*4882a593Smuzhiyun 		 * slow start
119*4882a593Smuzhiyun 		 *      INC = 2^RHO - 1
120*4882a593Smuzhiyun 		 * This is done by splitting the rho parameter
121*4882a593Smuzhiyun 		 * into 2 parts: an integer part and a fraction part.
122*4882a593Smuzhiyun 		 * Inrement<<7 is estimated by doing:
123*4882a593Smuzhiyun 		 *	       [2^(int+fract)]<<7
124*4882a593Smuzhiyun 		 * that is equal to:
125*4882a593Smuzhiyun 		 *	       (2^int)	*  [(2^fract) <<7]
126*4882a593Smuzhiyun 		 * 2^int is straightly computed as 1<<int,
127*4882a593Smuzhiyun 		 * while we will use hybla_slowstart_fraction_increment() to
128*4882a593Smuzhiyun 		 * calculate 2^fract in a <<7 value.
129*4882a593Smuzhiyun 		 */
130*4882a593Smuzhiyun 		is_slowstart = 1;
131*4882a593Smuzhiyun 		increment = ((1 << min(ca->rho, 16U)) *
132*4882a593Smuzhiyun 			hybla_fraction(rho_fractions)) - 128;
133*4882a593Smuzhiyun 	} else {
134*4882a593Smuzhiyun 		/*
135*4882a593Smuzhiyun 		 * congestion avoidance
136*4882a593Smuzhiyun 		 * INC = RHO^2 / W
137*4882a593Smuzhiyun 		 * as long as increment is estimated as (rho<<7)/window
138*4882a593Smuzhiyun 		 * it already is <<7 and we can easily count its fractions.
139*4882a593Smuzhiyun 		 */
140*4882a593Smuzhiyun 		increment = ca->rho2_7ls / tp->snd_cwnd;
141*4882a593Smuzhiyun 		if (increment < 128)
142*4882a593Smuzhiyun 			tp->snd_cwnd_cnt++;
143*4882a593Smuzhiyun 	}
144*4882a593Smuzhiyun 
145*4882a593Smuzhiyun 	odd = increment % 128;
146*4882a593Smuzhiyun 	tp->snd_cwnd += increment >> 7;
147*4882a593Smuzhiyun 	ca->snd_cwnd_cents += odd;
148*4882a593Smuzhiyun 
149*4882a593Smuzhiyun 	/* check when fractions goes >=128 and increase cwnd by 1. */
150*4882a593Smuzhiyun 	while (ca->snd_cwnd_cents >= 128) {
151*4882a593Smuzhiyun 		tp->snd_cwnd++;
152*4882a593Smuzhiyun 		ca->snd_cwnd_cents -= 128;
153*4882a593Smuzhiyun 		tp->snd_cwnd_cnt = 0;
154*4882a593Smuzhiyun 	}
155*4882a593Smuzhiyun 	/* check when cwnd has not been incremented for a while */
156*4882a593Smuzhiyun 	if (increment == 0 && odd == 0 && tp->snd_cwnd_cnt >= tp->snd_cwnd) {
157*4882a593Smuzhiyun 		tp->snd_cwnd++;
158*4882a593Smuzhiyun 		tp->snd_cwnd_cnt = 0;
159*4882a593Smuzhiyun 	}
160*4882a593Smuzhiyun 	/* clamp down slowstart cwnd to ssthresh value. */
161*4882a593Smuzhiyun 	if (is_slowstart)
162*4882a593Smuzhiyun 		tp->snd_cwnd = min(tp->snd_cwnd, tp->snd_ssthresh);
163*4882a593Smuzhiyun 
164*4882a593Smuzhiyun 	tp->snd_cwnd = min_t(u32, tp->snd_cwnd, tp->snd_cwnd_clamp);
165*4882a593Smuzhiyun }
166*4882a593Smuzhiyun 
167*4882a593Smuzhiyun static struct tcp_congestion_ops tcp_hybla __read_mostly = {
168*4882a593Smuzhiyun 	.init		= hybla_init,
169*4882a593Smuzhiyun 	.ssthresh	= tcp_reno_ssthresh,
170*4882a593Smuzhiyun 	.undo_cwnd	= tcp_reno_undo_cwnd,
171*4882a593Smuzhiyun 	.cong_avoid	= hybla_cong_avoid,
172*4882a593Smuzhiyun 	.set_state	= hybla_state,
173*4882a593Smuzhiyun 
174*4882a593Smuzhiyun 	.owner		= THIS_MODULE,
175*4882a593Smuzhiyun 	.name		= "hybla"
176*4882a593Smuzhiyun };
177*4882a593Smuzhiyun 
hybla_register(void)178*4882a593Smuzhiyun static int __init hybla_register(void)
179*4882a593Smuzhiyun {
180*4882a593Smuzhiyun 	BUILD_BUG_ON(sizeof(struct hybla) > ICSK_CA_PRIV_SIZE);
181*4882a593Smuzhiyun 	return tcp_register_congestion_control(&tcp_hybla);
182*4882a593Smuzhiyun }
183*4882a593Smuzhiyun 
hybla_unregister(void)184*4882a593Smuzhiyun static void __exit hybla_unregister(void)
185*4882a593Smuzhiyun {
186*4882a593Smuzhiyun 	tcp_unregister_congestion_control(&tcp_hybla);
187*4882a593Smuzhiyun }
188*4882a593Smuzhiyun 
189*4882a593Smuzhiyun module_init(hybla_register);
190*4882a593Smuzhiyun module_exit(hybla_unregister);
191*4882a593Smuzhiyun 
192*4882a593Smuzhiyun MODULE_AUTHOR("Daniele Lacamera");
193*4882a593Smuzhiyun MODULE_LICENSE("GPL");
194*4882a593Smuzhiyun MODULE_DESCRIPTION("TCP Hybla");
195