Linux Audio

Check our new training course

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