162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * TCP HYBLA
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * TCP-HYBLA Congestion control algorithm, based on:
662306a36Sopenharmony_ci *   C.Caini, R.Firrincieli, "TCP-Hybla: A TCP Enhancement
762306a36Sopenharmony_ci *   for Heterogeneous Networks",
862306a36Sopenharmony_ci *   International Journal on satellite Communications,
962306a36Sopenharmony_ci *				       September 2004
1062306a36Sopenharmony_ci *    Daniele Lacamera
1162306a36Sopenharmony_ci *    root at danielinux.net
1262306a36Sopenharmony_ci */
1362306a36Sopenharmony_ci
1462306a36Sopenharmony_ci#include <linux/module.h>
1562306a36Sopenharmony_ci#include <net/tcp.h>
1662306a36Sopenharmony_ci
1762306a36Sopenharmony_ci/* Tcp Hybla structure. */
1862306a36Sopenharmony_cistruct hybla {
1962306a36Sopenharmony_ci	bool  hybla_en;
2062306a36Sopenharmony_ci	u32   snd_cwnd_cents; /* Keeps increment values when it is <1, <<7 */
2162306a36Sopenharmony_ci	u32   rho;	      /* Rho parameter, integer part  */
2262306a36Sopenharmony_ci	u32   rho2;	      /* Rho * Rho, integer part */
2362306a36Sopenharmony_ci	u32   rho_3ls;	      /* Rho parameter, <<3 */
2462306a36Sopenharmony_ci	u32   rho2_7ls;	      /* Rho^2, <<7	*/
2562306a36Sopenharmony_ci	u32   minrtt_us;      /* Minimum smoothed round trip time value seen */
2662306a36Sopenharmony_ci};
2762306a36Sopenharmony_ci
2862306a36Sopenharmony_ci/* Hybla reference round trip time (default= 1/40 sec = 25 ms), in ms */
2962306a36Sopenharmony_cistatic int rtt0 = 25;
3062306a36Sopenharmony_cimodule_param(rtt0, int, 0644);
3162306a36Sopenharmony_ciMODULE_PARM_DESC(rtt0, "reference rout trip time (ms)");
3262306a36Sopenharmony_ci
3362306a36Sopenharmony_ci/* This is called to refresh values for hybla parameters */
3462306a36Sopenharmony_cistatic inline void hybla_recalc_param (struct sock *sk)
3562306a36Sopenharmony_ci{
3662306a36Sopenharmony_ci	struct hybla *ca = inet_csk_ca(sk);
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_ci	ca->rho_3ls = max_t(u32,
3962306a36Sopenharmony_ci			    tcp_sk(sk)->srtt_us / (rtt0 * USEC_PER_MSEC),
4062306a36Sopenharmony_ci			    8U);
4162306a36Sopenharmony_ci	ca->rho = ca->rho_3ls >> 3;
4262306a36Sopenharmony_ci	ca->rho2_7ls = (ca->rho_3ls * ca->rho_3ls) << 1;
4362306a36Sopenharmony_ci	ca->rho2 = ca->rho2_7ls >> 7;
4462306a36Sopenharmony_ci}
4562306a36Sopenharmony_ci
4662306a36Sopenharmony_cistatic void hybla_init(struct sock *sk)
4762306a36Sopenharmony_ci{
4862306a36Sopenharmony_ci	struct tcp_sock *tp = tcp_sk(sk);
4962306a36Sopenharmony_ci	struct hybla *ca = inet_csk_ca(sk);
5062306a36Sopenharmony_ci
5162306a36Sopenharmony_ci	ca->rho = 0;
5262306a36Sopenharmony_ci	ca->rho2 = 0;
5362306a36Sopenharmony_ci	ca->rho_3ls = 0;
5462306a36Sopenharmony_ci	ca->rho2_7ls = 0;
5562306a36Sopenharmony_ci	ca->snd_cwnd_cents = 0;
5662306a36Sopenharmony_ci	ca->hybla_en = true;
5762306a36Sopenharmony_ci	tcp_snd_cwnd_set(tp, 2);
5862306a36Sopenharmony_ci	tp->snd_cwnd_clamp = 65535;
5962306a36Sopenharmony_ci
6062306a36Sopenharmony_ci	/* 1st Rho measurement based on initial srtt */
6162306a36Sopenharmony_ci	hybla_recalc_param(sk);
6262306a36Sopenharmony_ci
6362306a36Sopenharmony_ci	/* set minimum rtt as this is the 1st ever seen */
6462306a36Sopenharmony_ci	ca->minrtt_us = tp->srtt_us;
6562306a36Sopenharmony_ci	tcp_snd_cwnd_set(tp, ca->rho);
6662306a36Sopenharmony_ci}
6762306a36Sopenharmony_ci
6862306a36Sopenharmony_cistatic void hybla_state(struct sock *sk, u8 ca_state)
6962306a36Sopenharmony_ci{
7062306a36Sopenharmony_ci	struct hybla *ca = inet_csk_ca(sk);
7162306a36Sopenharmony_ci
7262306a36Sopenharmony_ci	ca->hybla_en = (ca_state == TCP_CA_Open);
7362306a36Sopenharmony_ci}
7462306a36Sopenharmony_ci
7562306a36Sopenharmony_cistatic inline u32 hybla_fraction(u32 odds)
7662306a36Sopenharmony_ci{
7762306a36Sopenharmony_ci	static const u32 fractions[] = {
7862306a36Sopenharmony_ci		128, 139, 152, 165, 181, 197, 215, 234,
7962306a36Sopenharmony_ci	};
8062306a36Sopenharmony_ci
8162306a36Sopenharmony_ci	return (odds < ARRAY_SIZE(fractions)) ? fractions[odds] : 128;
8262306a36Sopenharmony_ci}
8362306a36Sopenharmony_ci
8462306a36Sopenharmony_ci/* TCP Hybla main routine.
8562306a36Sopenharmony_ci * This is the algorithm behavior:
8662306a36Sopenharmony_ci *     o Recalc Hybla parameters if min_rtt has changed
8762306a36Sopenharmony_ci *     o Give cwnd a new value based on the model proposed
8862306a36Sopenharmony_ci *     o remember increments <1
8962306a36Sopenharmony_ci */
9062306a36Sopenharmony_cistatic void hybla_cong_avoid(struct sock *sk, u32 ack, u32 acked)
9162306a36Sopenharmony_ci{
9262306a36Sopenharmony_ci	struct tcp_sock *tp = tcp_sk(sk);
9362306a36Sopenharmony_ci	struct hybla *ca = inet_csk_ca(sk);
9462306a36Sopenharmony_ci	u32 increment, odd, rho_fractions;
9562306a36Sopenharmony_ci	int is_slowstart = 0;
9662306a36Sopenharmony_ci
9762306a36Sopenharmony_ci	/*  Recalculate rho only if this srtt is the lowest */
9862306a36Sopenharmony_ci	if (tp->srtt_us < ca->minrtt_us) {
9962306a36Sopenharmony_ci		hybla_recalc_param(sk);
10062306a36Sopenharmony_ci		ca->minrtt_us = tp->srtt_us;
10162306a36Sopenharmony_ci	}
10262306a36Sopenharmony_ci
10362306a36Sopenharmony_ci	if (!tcp_is_cwnd_limited(sk))
10462306a36Sopenharmony_ci		return;
10562306a36Sopenharmony_ci
10662306a36Sopenharmony_ci	if (!ca->hybla_en) {
10762306a36Sopenharmony_ci		tcp_reno_cong_avoid(sk, ack, acked);
10862306a36Sopenharmony_ci		return;
10962306a36Sopenharmony_ci	}
11062306a36Sopenharmony_ci
11162306a36Sopenharmony_ci	if (ca->rho == 0)
11262306a36Sopenharmony_ci		hybla_recalc_param(sk);
11362306a36Sopenharmony_ci
11462306a36Sopenharmony_ci	rho_fractions = ca->rho_3ls - (ca->rho << 3);
11562306a36Sopenharmony_ci
11662306a36Sopenharmony_ci	if (tcp_in_slow_start(tp)) {
11762306a36Sopenharmony_ci		/*
11862306a36Sopenharmony_ci		 * slow start
11962306a36Sopenharmony_ci		 *      INC = 2^RHO - 1
12062306a36Sopenharmony_ci		 * This is done by splitting the rho parameter
12162306a36Sopenharmony_ci		 * into 2 parts: an integer part and a fraction part.
12262306a36Sopenharmony_ci		 * Inrement<<7 is estimated by doing:
12362306a36Sopenharmony_ci		 *	       [2^(int+fract)]<<7
12462306a36Sopenharmony_ci		 * that is equal to:
12562306a36Sopenharmony_ci		 *	       (2^int)	*  [(2^fract) <<7]
12662306a36Sopenharmony_ci		 * 2^int is straightly computed as 1<<int,
12762306a36Sopenharmony_ci		 * while we will use hybla_slowstart_fraction_increment() to
12862306a36Sopenharmony_ci		 * calculate 2^fract in a <<7 value.
12962306a36Sopenharmony_ci		 */
13062306a36Sopenharmony_ci		is_slowstart = 1;
13162306a36Sopenharmony_ci		increment = ((1 << min(ca->rho, 16U)) *
13262306a36Sopenharmony_ci			hybla_fraction(rho_fractions)) - 128;
13362306a36Sopenharmony_ci	} else {
13462306a36Sopenharmony_ci		/*
13562306a36Sopenharmony_ci		 * congestion avoidance
13662306a36Sopenharmony_ci		 * INC = RHO^2 / W
13762306a36Sopenharmony_ci		 * as long as increment is estimated as (rho<<7)/window
13862306a36Sopenharmony_ci		 * it already is <<7 and we can easily count its fractions.
13962306a36Sopenharmony_ci		 */
14062306a36Sopenharmony_ci		increment = ca->rho2_7ls / tcp_snd_cwnd(tp);
14162306a36Sopenharmony_ci		if (increment < 128)
14262306a36Sopenharmony_ci			tp->snd_cwnd_cnt++;
14362306a36Sopenharmony_ci	}
14462306a36Sopenharmony_ci
14562306a36Sopenharmony_ci	odd = increment % 128;
14662306a36Sopenharmony_ci	tcp_snd_cwnd_set(tp, tcp_snd_cwnd(tp) + (increment >> 7));
14762306a36Sopenharmony_ci	ca->snd_cwnd_cents += odd;
14862306a36Sopenharmony_ci
14962306a36Sopenharmony_ci	/* check when fractions goes >=128 and increase cwnd by 1. */
15062306a36Sopenharmony_ci	while (ca->snd_cwnd_cents >= 128) {
15162306a36Sopenharmony_ci		tcp_snd_cwnd_set(tp, tcp_snd_cwnd(tp) + 1);
15262306a36Sopenharmony_ci		ca->snd_cwnd_cents -= 128;
15362306a36Sopenharmony_ci		tp->snd_cwnd_cnt = 0;
15462306a36Sopenharmony_ci	}
15562306a36Sopenharmony_ci	/* check when cwnd has not been incremented for a while */
15662306a36Sopenharmony_ci	if (increment == 0 && odd == 0 && tp->snd_cwnd_cnt >= tcp_snd_cwnd(tp)) {
15762306a36Sopenharmony_ci		tcp_snd_cwnd_set(tp, tcp_snd_cwnd(tp) + 1);
15862306a36Sopenharmony_ci		tp->snd_cwnd_cnt = 0;
15962306a36Sopenharmony_ci	}
16062306a36Sopenharmony_ci	/* clamp down slowstart cwnd to ssthresh value. */
16162306a36Sopenharmony_ci	if (is_slowstart)
16262306a36Sopenharmony_ci		tcp_snd_cwnd_set(tp, min(tcp_snd_cwnd(tp), tp->snd_ssthresh));
16362306a36Sopenharmony_ci
16462306a36Sopenharmony_ci	tcp_snd_cwnd_set(tp, min(tcp_snd_cwnd(tp), tp->snd_cwnd_clamp));
16562306a36Sopenharmony_ci}
16662306a36Sopenharmony_ci
16762306a36Sopenharmony_cistatic struct tcp_congestion_ops tcp_hybla __read_mostly = {
16862306a36Sopenharmony_ci	.init		= hybla_init,
16962306a36Sopenharmony_ci	.ssthresh	= tcp_reno_ssthresh,
17062306a36Sopenharmony_ci	.undo_cwnd	= tcp_reno_undo_cwnd,
17162306a36Sopenharmony_ci	.cong_avoid	= hybla_cong_avoid,
17262306a36Sopenharmony_ci	.set_state	= hybla_state,
17362306a36Sopenharmony_ci
17462306a36Sopenharmony_ci	.owner		= THIS_MODULE,
17562306a36Sopenharmony_ci	.name		= "hybla"
17662306a36Sopenharmony_ci};
17762306a36Sopenharmony_ci
17862306a36Sopenharmony_cistatic int __init hybla_register(void)
17962306a36Sopenharmony_ci{
18062306a36Sopenharmony_ci	BUILD_BUG_ON(sizeof(struct hybla) > ICSK_CA_PRIV_SIZE);
18162306a36Sopenharmony_ci	return tcp_register_congestion_control(&tcp_hybla);
18262306a36Sopenharmony_ci}
18362306a36Sopenharmony_ci
18462306a36Sopenharmony_cistatic void __exit hybla_unregister(void)
18562306a36Sopenharmony_ci{
18662306a36Sopenharmony_ci	tcp_unregister_congestion_control(&tcp_hybla);
18762306a36Sopenharmony_ci}
18862306a36Sopenharmony_ci
18962306a36Sopenharmony_cimodule_init(hybla_register);
19062306a36Sopenharmony_cimodule_exit(hybla_unregister);
19162306a36Sopenharmony_ci
19262306a36Sopenharmony_ciMODULE_AUTHOR("Daniele Lacamera");
19362306a36Sopenharmony_ciMODULE_LICENSE("GPL");
19462306a36Sopenharmony_ciMODULE_DESCRIPTION("TCP Hybla");
195