blob: 083831e359df92ca9ba0fe7dd5a7a76fe41a94b0 [file] [log] [blame]
Daniele Lacamera835b3f02005-06-23 12:26:34 -07001/*
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
Daniele Lacamera835b3f02005-06-23 12:26:34 -070013#include <linux/module.h>
14#include <net/tcp.h>
15
16/* Tcp Hybla structure. */
17struct hybla {
Eric Dumazeta2a385d2012-05-16 23:15:34 +000018 bool hybla_en;
Daniele Lacamera835b3f02005-06-23 12:26:34 -070019 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 */
Eric Dumazet740b0f12014-02-26 14:02:48 -080024 u32 minrtt_us; /* Minimum smoothed round trip time value seen */
Daniele Lacamera835b3f02005-06-23 12:26:34 -070025};
26
Eric Dumazeta2a385d2012-05-16 23:15:34 +000027/* Hybla reference round trip time (default= 1/40 sec = 25 ms), in ms */
Daniele Lacamera835b3f02005-06-23 12:26:34 -070028static int rtt0 = 25;
29module_param(rtt0, int, 0644);
30MODULE_PARM_DESC(rtt0, "reference rout trip time (ms)");
31
Daniele Lacamera835b3f02005-06-23 12:26:34 -070032/* This is called to refresh values for hybla parameters */
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -030033static inline void hybla_recalc_param (struct sock *sk)
Daniele Lacamera835b3f02005-06-23 12:26:34 -070034{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -030035 struct hybla *ca = inet_csk_ca(sk);
Daniele Lacamera835b3f02005-06-23 12:26:34 -070036
Eric Dumazet740b0f12014-02-26 14:02:48 -080037 ca->rho_3ls = max_t(u32,
38 tcp_sk(sk)->srtt_us / (rtt0 * USEC_PER_MSEC),
39 8U);
Daniele Lacamera835b3f02005-06-23 12:26:34 -070040 ca->rho = ca->rho_3ls >> 3;
41 ca->rho2_7ls = (ca->rho_3ls * ca->rho_3ls) << 1;
Eric Dumazeta2a385d2012-05-16 23:15:34 +000042 ca->rho2 = ca->rho2_7ls >> 7;
Daniele Lacamera835b3f02005-06-23 12:26:34 -070043}
44
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -030045static void hybla_init(struct sock *sk)
Daniele Lacamera835b3f02005-06-23 12:26:34 -070046{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -030047 struct tcp_sock *tp = tcp_sk(sk);
48 struct hybla *ca = inet_csk_ca(sk);
Daniele Lacamera835b3f02005-06-23 12:26:34 -070049
50 ca->rho = 0;
51 ca->rho2 = 0;
52 ca->rho_3ls = 0;
53 ca->rho2_7ls = 0;
54 ca->snd_cwnd_cents = 0;
Eric Dumazeta2a385d2012-05-16 23:15:34 +000055 ca->hybla_en = true;
Daniele Lacamera835b3f02005-06-23 12:26:34 -070056 tp->snd_cwnd = 2;
57 tp->snd_cwnd_clamp = 65535;
58
59 /* 1st Rho measurement based on initial srtt */
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -030060 hybla_recalc_param(sk);
Daniele Lacamera835b3f02005-06-23 12:26:34 -070061
62 /* set minimum rtt as this is the 1st ever seen */
Eric Dumazet740b0f12014-02-26 14:02:48 -080063 ca->minrtt_us = tp->srtt_us;
Daniele Lacamera835b3f02005-06-23 12:26:34 -070064 tp->snd_cwnd = ca->rho;
65}
66
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -030067static void hybla_state(struct sock *sk, u8 ca_state)
Daniele Lacamera835b3f02005-06-23 12:26:34 -070068{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -030069 struct hybla *ca = inet_csk_ca(sk);
Eric Dumazeta2a385d2012-05-16 23:15:34 +000070
Daniele Lacamera835b3f02005-06-23 12:26:34 -070071 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 */
Eric Dumazet24901552014-05-02 21:18:05 -070089static void hybla_cong_avoid(struct sock *sk, u32 ack, u32 acked)
Daniele Lacamera835b3f02005-06-23 12:26:34 -070090{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -030091 struct tcp_sock *tp = tcp_sk(sk);
92 struct hybla *ca = inet_csk_ca(sk);
Daniele Lacamera835b3f02005-06-23 12:26:34 -070093 u32 increment, odd, rho_fractions;
94 int is_slowstart = 0;
95
96 /* Recalculate rho only if this srtt is the lowest */
Eric Dumazet740b0f12014-02-26 14:02:48 -080097 if (tp->srtt_us < ca->minrtt_us) {
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -030098 hybla_recalc_param(sk);
Eric Dumazet740b0f12014-02-26 14:02:48 -080099 ca->minrtt_us = tp->srtt_us;
Daniele Lacamera835b3f02005-06-23 12:26:34 -0700100 }
101
Eric Dumazet24901552014-05-02 21:18:05 -0700102 if (!tcp_is_cwnd_limited(sk))
Stephen Hemmingerf4805ed2005-11-10 16:53:30 -0800103 return;
104
Harvey Harrisonab598592008-05-01 02:47:38 -0700105 if (!ca->hybla_en) {
Eric Dumazet24901552014-05-02 21:18:05 -0700106 tcp_reno_cong_avoid(sk, ack, acked);
Harvey Harrisonab598592008-05-01 02:47:38 -0700107 return;
108 }
Daniele Lacamera835b3f02005-06-23 12:26:34 -0700109
Daniele Lacamera835b3f02005-06-23 12:26:34 -0700110 if (ca->rho == 0)
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300111 hybla_recalc_param(sk);
Daniele Lacamera835b3f02005-06-23 12:26:34 -0700112
113 rho_fractions = ca->rho_3ls - (ca->rho << 3);
114
Yuchung Cheng76174002015-07-09 13:16:30 -0700115 if (tcp_in_slow_start(tp)) {
Daniele Lacamera835b3f02005-06-23 12:26:34 -0700116 /*
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;
Daniele Lacameraedafe502010-06-02 02:02:04 +0000130 increment = ((1 << min(ca->rho, 16U)) *
131 hybla_fraction(rho_fractions)) - 128;
Daniele Lacamera835b3f02005-06-23 12:26:34 -0700132 } 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. */
Stephen Hemminger2de979b2007-03-08 20:45:19 -0800149 while (ca->snd_cwnd_cents >= 128) {
Daniele Lacamera835b3f02005-06-23 12:26:34 -0700150 tp->snd_cwnd++;
151 ca->snd_cwnd_cents -= 128;
152 tp->snd_cwnd_cnt = 0;
153 }
Daniele Lacamera9d2c27e2008-10-07 15:58:17 -0700154 /* 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 }
Daniele Lacamera835b3f02005-06-23 12:26:34 -0700159 /* 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
Stephen Hemmingera252beb2011-03-10 00:40:17 -0800166static struct tcp_congestion_ops tcp_hybla __read_mostly = {
Daniele Lacamera835b3f02005-06-23 12:26:34 -0700167 .init = hybla_init,
168 .ssthresh = tcp_reno_ssthresh,
Daniele Lacamera835b3f02005-06-23 12:26:34 -0700169 .cong_avoid = hybla_cong_avoid,
170 .set_state = hybla_state,
171
172 .owner = THIS_MODULE,
173 .name = "hybla"
174};
175
176static int __init hybla_register(void)
177{
Alexey Dobriyan74975d42006-08-25 17:10:33 -0700178 BUILD_BUG_ON(sizeof(struct hybla) > ICSK_CA_PRIV_SIZE);
Daniele Lacamera835b3f02005-06-23 12:26:34 -0700179 return tcp_register_congestion_control(&tcp_hybla);
180}
181
182static void __exit hybla_unregister(void)
183{
184 tcp_unregister_congestion_control(&tcp_hybla);
185}
186
187module_init(hybla_register);
188module_exit(hybla_unregister);
189
190MODULE_AUTHOR("Daniele Lacamera");
191MODULE_LICENSE("GPL");
192MODULE_DESCRIPTION("TCP Hybla");