Thomas Gleixner | 09c434b | 2019-05-19 13:08:20 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Daniele Lacamera | 835b3f0 | 2005-06-23 12:26:34 -0700 | [diff] [blame] | 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 | |
Daniele Lacamera | 835b3f0 | 2005-06-23 12:26:34 -0700 | [diff] [blame] | 14 | #include <linux/module.h> |
| 15 | #include <net/tcp.h> |
| 16 | |
| 17 | /* Tcp Hybla structure. */ |
| 18 | struct hybla { |
Eric Dumazet | a2a385d | 2012-05-16 23:15:34 +0000 | [diff] [blame] | 19 | bool hybla_en; |
Daniele Lacamera | 835b3f0 | 2005-06-23 12:26:34 -0700 | [diff] [blame] | 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 */ |
Eric Dumazet | 740b0f1 | 2014-02-26 14:02:48 -0800 | [diff] [blame] | 25 | u32 minrtt_us; /* Minimum smoothed round trip time value seen */ |
Daniele Lacamera | 835b3f0 | 2005-06-23 12:26:34 -0700 | [diff] [blame] | 26 | }; |
| 27 | |
Eric Dumazet | a2a385d | 2012-05-16 23:15:34 +0000 | [diff] [blame] | 28 | /* Hybla reference round trip time (default= 1/40 sec = 25 ms), in ms */ |
Daniele Lacamera | 835b3f0 | 2005-06-23 12:26:34 -0700 | [diff] [blame] | 29 | static int rtt0 = 25; |
| 30 | module_param(rtt0, int, 0644); |
| 31 | MODULE_PARM_DESC(rtt0, "reference rout trip time (ms)"); |
| 32 | |
Daniele Lacamera | 835b3f0 | 2005-06-23 12:26:34 -0700 | [diff] [blame] | 33 | /* This is called to refresh values for hybla parameters */ |
Arnaldo Carvalho de Melo | 6687e98 | 2005-08-10 04:03:31 -0300 | [diff] [blame] | 34 | static inline void hybla_recalc_param (struct sock *sk) |
Daniele Lacamera | 835b3f0 | 2005-06-23 12:26:34 -0700 | [diff] [blame] | 35 | { |
Arnaldo Carvalho de Melo | 6687e98 | 2005-08-10 04:03:31 -0300 | [diff] [blame] | 36 | struct hybla *ca = inet_csk_ca(sk); |
Daniele Lacamera | 835b3f0 | 2005-06-23 12:26:34 -0700 | [diff] [blame] | 37 | |
Eric Dumazet | 740b0f1 | 2014-02-26 14:02:48 -0800 | [diff] [blame] | 38 | ca->rho_3ls = max_t(u32, |
| 39 | tcp_sk(sk)->srtt_us / (rtt0 * USEC_PER_MSEC), |
| 40 | 8U); |
Daniele Lacamera | 835b3f0 | 2005-06-23 12:26:34 -0700 | [diff] [blame] | 41 | ca->rho = ca->rho_3ls >> 3; |
| 42 | ca->rho2_7ls = (ca->rho_3ls * ca->rho_3ls) << 1; |
Eric Dumazet | a2a385d | 2012-05-16 23:15:34 +0000 | [diff] [blame] | 43 | ca->rho2 = ca->rho2_7ls >> 7; |
Daniele Lacamera | 835b3f0 | 2005-06-23 12:26:34 -0700 | [diff] [blame] | 44 | } |
| 45 | |
Arnaldo Carvalho de Melo | 6687e98 | 2005-08-10 04:03:31 -0300 | [diff] [blame] | 46 | static void hybla_init(struct sock *sk) |
Daniele Lacamera | 835b3f0 | 2005-06-23 12:26:34 -0700 | [diff] [blame] | 47 | { |
Arnaldo Carvalho de Melo | 6687e98 | 2005-08-10 04:03:31 -0300 | [diff] [blame] | 48 | struct tcp_sock *tp = tcp_sk(sk); |
| 49 | struct hybla *ca = inet_csk_ca(sk); |
Daniele Lacamera | 835b3f0 | 2005-06-23 12:26:34 -0700 | [diff] [blame] | 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; |
Eric Dumazet | a2a385d | 2012-05-16 23:15:34 +0000 | [diff] [blame] | 56 | ca->hybla_en = true; |
Daniele Lacamera | 835b3f0 | 2005-06-23 12:26:34 -0700 | [diff] [blame] | 57 | tp->snd_cwnd = 2; |
| 58 | tp->snd_cwnd_clamp = 65535; |
| 59 | |
| 60 | /* 1st Rho measurement based on initial srtt */ |
Arnaldo Carvalho de Melo | 6687e98 | 2005-08-10 04:03:31 -0300 | [diff] [blame] | 61 | hybla_recalc_param(sk); |
Daniele Lacamera | 835b3f0 | 2005-06-23 12:26:34 -0700 | [diff] [blame] | 62 | |
| 63 | /* set minimum rtt as this is the 1st ever seen */ |
Eric Dumazet | 740b0f1 | 2014-02-26 14:02:48 -0800 | [diff] [blame] | 64 | ca->minrtt_us = tp->srtt_us; |
Daniele Lacamera | 835b3f0 | 2005-06-23 12:26:34 -0700 | [diff] [blame] | 65 | tp->snd_cwnd = ca->rho; |
| 66 | } |
| 67 | |
Arnaldo Carvalho de Melo | 6687e98 | 2005-08-10 04:03:31 -0300 | [diff] [blame] | 68 | static void hybla_state(struct sock *sk, u8 ca_state) |
Daniele Lacamera | 835b3f0 | 2005-06-23 12:26:34 -0700 | [diff] [blame] | 69 | { |
Arnaldo Carvalho de Melo | 6687e98 | 2005-08-10 04:03:31 -0300 | [diff] [blame] | 70 | struct hybla *ca = inet_csk_ca(sk); |
Eric Dumazet | a2a385d | 2012-05-16 23:15:34 +0000 | [diff] [blame] | 71 | |
Daniele Lacamera | 835b3f0 | 2005-06-23 12:26:34 -0700 | [diff] [blame] | 72 | ca->hybla_en = (ca_state == TCP_CA_Open); |
| 73 | } |
| 74 | |
| 75 | static 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 | */ |
Eric Dumazet | 2490155 | 2014-05-02 21:18:05 -0700 | [diff] [blame] | 90 | static void hybla_cong_avoid(struct sock *sk, u32 ack, u32 acked) |
Daniele Lacamera | 835b3f0 | 2005-06-23 12:26:34 -0700 | [diff] [blame] | 91 | { |
Arnaldo Carvalho de Melo | 6687e98 | 2005-08-10 04:03:31 -0300 | [diff] [blame] | 92 | struct tcp_sock *tp = tcp_sk(sk); |
| 93 | struct hybla *ca = inet_csk_ca(sk); |
Daniele Lacamera | 835b3f0 | 2005-06-23 12:26:34 -0700 | [diff] [blame] | 94 | u32 increment, odd, rho_fractions; |
| 95 | int is_slowstart = 0; |
| 96 | |
| 97 | /* Recalculate rho only if this srtt is the lowest */ |
Eric Dumazet | 740b0f1 | 2014-02-26 14:02:48 -0800 | [diff] [blame] | 98 | if (tp->srtt_us < ca->minrtt_us) { |
Arnaldo Carvalho de Melo | 6687e98 | 2005-08-10 04:03:31 -0300 | [diff] [blame] | 99 | hybla_recalc_param(sk); |
Eric Dumazet | 740b0f1 | 2014-02-26 14:02:48 -0800 | [diff] [blame] | 100 | ca->minrtt_us = tp->srtt_us; |
Daniele Lacamera | 835b3f0 | 2005-06-23 12:26:34 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Eric Dumazet | 2490155 | 2014-05-02 21:18:05 -0700 | [diff] [blame] | 103 | if (!tcp_is_cwnd_limited(sk)) |
Stephen Hemminger | f4805ed | 2005-11-10 16:53:30 -0800 | [diff] [blame] | 104 | return; |
| 105 | |
Harvey Harrison | ab59859 | 2008-05-01 02:47:38 -0700 | [diff] [blame] | 106 | if (!ca->hybla_en) { |
Eric Dumazet | 2490155 | 2014-05-02 21:18:05 -0700 | [diff] [blame] | 107 | tcp_reno_cong_avoid(sk, ack, acked); |
Harvey Harrison | ab59859 | 2008-05-01 02:47:38 -0700 | [diff] [blame] | 108 | return; |
| 109 | } |
Daniele Lacamera | 835b3f0 | 2005-06-23 12:26:34 -0700 | [diff] [blame] | 110 | |
Daniele Lacamera | 835b3f0 | 2005-06-23 12:26:34 -0700 | [diff] [blame] | 111 | if (ca->rho == 0) |
Arnaldo Carvalho de Melo | 6687e98 | 2005-08-10 04:03:31 -0300 | [diff] [blame] | 112 | hybla_recalc_param(sk); |
Daniele Lacamera | 835b3f0 | 2005-06-23 12:26:34 -0700 | [diff] [blame] | 113 | |
| 114 | rho_fractions = ca->rho_3ls - (ca->rho << 3); |
| 115 | |
Yuchung Cheng | 7617400 | 2015-07-09 13:16:30 -0700 | [diff] [blame] | 116 | if (tcp_in_slow_start(tp)) { |
Daniele Lacamera | 835b3f0 | 2005-06-23 12:26:34 -0700 | [diff] [blame] | 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; |
Daniele Lacamera | edafe50 | 2010-06-02 02:02:04 +0000 | [diff] [blame] | 131 | increment = ((1 << min(ca->rho, 16U)) * |
| 132 | hybla_fraction(rho_fractions)) - 128; |
Daniele Lacamera | 835b3f0 | 2005-06-23 12:26:34 -0700 | [diff] [blame] | 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 / tp->snd_cwnd; |
| 141 | if (increment < 128) |
| 142 | tp->snd_cwnd_cnt++; |
| 143 | } |
| 144 | |
| 145 | odd = increment % 128; |
| 146 | tp->snd_cwnd += increment >> 7; |
| 147 | ca->snd_cwnd_cents += odd; |
| 148 | |
| 149 | /* check when fractions goes >=128 and increase cwnd by 1. */ |
Stephen Hemminger | 2de979b | 2007-03-08 20:45:19 -0800 | [diff] [blame] | 150 | while (ca->snd_cwnd_cents >= 128) { |
Daniele Lacamera | 835b3f0 | 2005-06-23 12:26:34 -0700 | [diff] [blame] | 151 | tp->snd_cwnd++; |
| 152 | ca->snd_cwnd_cents -= 128; |
| 153 | tp->snd_cwnd_cnt = 0; |
| 154 | } |
Daniele Lacamera | 9d2c27e | 2008-10-07 15:58:17 -0700 | [diff] [blame] | 155 | /* check when cwnd has not been incremented for a while */ |
| 156 | if (increment == 0 && odd == 0 && tp->snd_cwnd_cnt >= tp->snd_cwnd) { |
| 157 | tp->snd_cwnd++; |
| 158 | tp->snd_cwnd_cnt = 0; |
| 159 | } |
Daniele Lacamera | 835b3f0 | 2005-06-23 12:26:34 -0700 | [diff] [blame] | 160 | /* clamp down slowstart cwnd to ssthresh value. */ |
| 161 | if (is_slowstart) |
| 162 | tp->snd_cwnd = min(tp->snd_cwnd, tp->snd_ssthresh); |
| 163 | |
| 164 | tp->snd_cwnd = min_t(u32, tp->snd_cwnd, tp->snd_cwnd_clamp); |
| 165 | } |
| 166 | |
Stephen Hemminger | a252beb | 2011-03-10 00:40:17 -0800 | [diff] [blame] | 167 | static struct tcp_congestion_ops tcp_hybla __read_mostly = { |
Daniele Lacamera | 835b3f0 | 2005-06-23 12:26:34 -0700 | [diff] [blame] | 168 | .init = hybla_init, |
| 169 | .ssthresh = tcp_reno_ssthresh, |
Florian Westphal | e979918 | 2016-11-21 14:18:38 +0100 | [diff] [blame] | 170 | .undo_cwnd = tcp_reno_undo_cwnd, |
Daniele Lacamera | 835b3f0 | 2005-06-23 12:26:34 -0700 | [diff] [blame] | 171 | .cong_avoid = hybla_cong_avoid, |
| 172 | .set_state = hybla_state, |
| 173 | |
| 174 | .owner = THIS_MODULE, |
| 175 | .name = "hybla" |
| 176 | }; |
| 177 | |
| 178 | static int __init hybla_register(void) |
| 179 | { |
Alexey Dobriyan | 74975d4 | 2006-08-25 17:10:33 -0700 | [diff] [blame] | 180 | BUILD_BUG_ON(sizeof(struct hybla) > ICSK_CA_PRIV_SIZE); |
Daniele Lacamera | 835b3f0 | 2005-06-23 12:26:34 -0700 | [diff] [blame] | 181 | return tcp_register_congestion_control(&tcp_hybla); |
| 182 | } |
| 183 | |
| 184 | static void __exit hybla_unregister(void) |
| 185 | { |
| 186 | tcp_unregister_congestion_control(&tcp_hybla); |
| 187 | } |
| 188 | |
| 189 | module_init(hybla_register); |
| 190 | module_exit(hybla_unregister); |
| 191 | |
| 192 | MODULE_AUTHOR("Daniele Lacamera"); |
| 193 | MODULE_LICENSE("GPL"); |
| 194 | MODULE_DESCRIPTION("TCP Hybla"); |