1835b3f0cSDaniele Lacamera /* 2835b3f0cSDaniele Lacamera * TCP HYBLA 3835b3f0cSDaniele Lacamera * 4835b3f0cSDaniele Lacamera * TCP-HYBLA Congestion control algorithm, based on: 5835b3f0cSDaniele Lacamera * C.Caini, R.Firrincieli, "TCP-Hybla: A TCP Enhancement 6835b3f0cSDaniele Lacamera * for Heterogeneous Networks", 7835b3f0cSDaniele Lacamera * International Journal on satellite Communications, 8835b3f0cSDaniele Lacamera * September 2004 9835b3f0cSDaniele Lacamera * Daniele Lacamera 10835b3f0cSDaniele Lacamera * root at danielinux.net 11835b3f0cSDaniele Lacamera */ 12835b3f0cSDaniele Lacamera 13835b3f0cSDaniele Lacamera #include <linux/module.h> 14835b3f0cSDaniele Lacamera #include <net/tcp.h> 15835b3f0cSDaniele Lacamera 16835b3f0cSDaniele Lacamera /* Tcp Hybla structure. */ 17835b3f0cSDaniele Lacamera struct hybla { 18a2a385d6SEric Dumazet bool hybla_en; 19835b3f0cSDaniele Lacamera u32 snd_cwnd_cents; /* Keeps increment values when it is <1, <<7 */ 20835b3f0cSDaniele Lacamera u32 rho; /* Rho parameter, integer part */ 21835b3f0cSDaniele Lacamera u32 rho2; /* Rho * Rho, integer part */ 22835b3f0cSDaniele Lacamera u32 rho_3ls; /* Rho parameter, <<3 */ 23835b3f0cSDaniele Lacamera u32 rho2_7ls; /* Rho^2, <<7 */ 24740b0f18SEric Dumazet u32 minrtt_us; /* Minimum smoothed round trip time value seen */ 25835b3f0cSDaniele Lacamera }; 26835b3f0cSDaniele Lacamera 27a2a385d6SEric Dumazet /* Hybla reference round trip time (default= 1/40 sec = 25 ms), in ms */ 28835b3f0cSDaniele Lacamera static int rtt0 = 25; 29835b3f0cSDaniele Lacamera module_param(rtt0, int, 0644); 30835b3f0cSDaniele Lacamera MODULE_PARM_DESC(rtt0, "reference rout trip time (ms)"); 31835b3f0cSDaniele Lacamera 32835b3f0cSDaniele Lacamera 33835b3f0cSDaniele Lacamera /* This is called to refresh values for hybla parameters */ 346687e988SArnaldo Carvalho de Melo static inline void hybla_recalc_param (struct sock *sk) 35835b3f0cSDaniele Lacamera { 366687e988SArnaldo Carvalho de Melo struct hybla *ca = inet_csk_ca(sk); 37835b3f0cSDaniele Lacamera 38740b0f18SEric Dumazet ca->rho_3ls = max_t(u32, 39740b0f18SEric Dumazet tcp_sk(sk)->srtt_us / (rtt0 * USEC_PER_MSEC), 40740b0f18SEric Dumazet 8U); 41835b3f0cSDaniele Lacamera ca->rho = ca->rho_3ls >> 3; 42835b3f0cSDaniele Lacamera ca->rho2_7ls = (ca->rho_3ls * ca->rho_3ls) << 1; 43835b3f0cSDaniele Lacamera ca->rho2 = ca->rho2_7ls >> 7; 44835b3f0cSDaniele Lacamera } 45835b3f0cSDaniele Lacamera 466687e988SArnaldo Carvalho de Melo static void hybla_init(struct sock *sk) 47835b3f0cSDaniele Lacamera { 486687e988SArnaldo Carvalho de Melo struct tcp_sock *tp = tcp_sk(sk); 496687e988SArnaldo Carvalho de Melo struct hybla *ca = inet_csk_ca(sk); 50835b3f0cSDaniele Lacamera 51835b3f0cSDaniele Lacamera ca->rho = 0; 52835b3f0cSDaniele Lacamera ca->rho2 = 0; 53835b3f0cSDaniele Lacamera ca->rho_3ls = 0; 54835b3f0cSDaniele Lacamera ca->rho2_7ls = 0; 55835b3f0cSDaniele Lacamera ca->snd_cwnd_cents = 0; 56a2a385d6SEric Dumazet ca->hybla_en = true; 57835b3f0cSDaniele Lacamera tp->snd_cwnd = 2; 58835b3f0cSDaniele Lacamera tp->snd_cwnd_clamp = 65535; 59835b3f0cSDaniele Lacamera 60835b3f0cSDaniele Lacamera /* 1st Rho measurement based on initial srtt */ 616687e988SArnaldo Carvalho de Melo hybla_recalc_param(sk); 62835b3f0cSDaniele Lacamera 63835b3f0cSDaniele Lacamera /* set minimum rtt as this is the 1st ever seen */ 64740b0f18SEric Dumazet ca->minrtt_us = tp->srtt_us; 65835b3f0cSDaniele Lacamera tp->snd_cwnd = ca->rho; 66835b3f0cSDaniele Lacamera } 67835b3f0cSDaniele Lacamera 686687e988SArnaldo Carvalho de Melo static void hybla_state(struct sock *sk, u8 ca_state) 69835b3f0cSDaniele Lacamera { 706687e988SArnaldo Carvalho de Melo struct hybla *ca = inet_csk_ca(sk); 71a2a385d6SEric Dumazet 72835b3f0cSDaniele Lacamera ca->hybla_en = (ca_state == TCP_CA_Open); 73835b3f0cSDaniele Lacamera } 74835b3f0cSDaniele Lacamera 75835b3f0cSDaniele Lacamera static inline u32 hybla_fraction(u32 odds) 76835b3f0cSDaniele Lacamera { 77835b3f0cSDaniele Lacamera static const u32 fractions[] = { 78835b3f0cSDaniele Lacamera 128, 139, 152, 165, 181, 197, 215, 234, 79835b3f0cSDaniele Lacamera }; 80835b3f0cSDaniele Lacamera 81835b3f0cSDaniele Lacamera return (odds < ARRAY_SIZE(fractions)) ? fractions[odds] : 128; 82835b3f0cSDaniele Lacamera } 83835b3f0cSDaniele Lacamera 84835b3f0cSDaniele Lacamera /* TCP Hybla main routine. 85835b3f0cSDaniele Lacamera * This is the algorithm behavior: 86835b3f0cSDaniele Lacamera * o Recalc Hybla parameters if min_rtt has changed 87835b3f0cSDaniele Lacamera * o Give cwnd a new value based on the model proposed 88835b3f0cSDaniele Lacamera * o remember increments <1 89835b3f0cSDaniele Lacamera */ 909f9843a7SYuchung Cheng static void hybla_cong_avoid(struct sock *sk, u32 ack, u32 acked, 919f9843a7SYuchung Cheng u32 in_flight) 92835b3f0cSDaniele Lacamera { 936687e988SArnaldo Carvalho de Melo struct tcp_sock *tp = tcp_sk(sk); 946687e988SArnaldo Carvalho de Melo struct hybla *ca = inet_csk_ca(sk); 95835b3f0cSDaniele Lacamera u32 increment, odd, rho_fractions; 96835b3f0cSDaniele Lacamera int is_slowstart = 0; 97835b3f0cSDaniele Lacamera 98835b3f0cSDaniele Lacamera /* Recalculate rho only if this srtt is the lowest */ 99740b0f18SEric Dumazet if (tp->srtt_us < ca->minrtt_us) { 1006687e988SArnaldo Carvalho de Melo hybla_recalc_param(sk); 101740b0f18SEric Dumazet ca->minrtt_us = tp->srtt_us; 102835b3f0cSDaniele Lacamera } 103835b3f0cSDaniele Lacamera 104f4805edeSStephen Hemminger if (!tcp_is_cwnd_limited(sk, in_flight)) 105f4805edeSStephen Hemminger return; 106f4805edeSStephen Hemminger 107ab59859dSHarvey Harrison if (!ca->hybla_en) { 1089f9843a7SYuchung Cheng tcp_reno_cong_avoid(sk, ack, acked, in_flight); 109ab59859dSHarvey Harrison return; 110ab59859dSHarvey Harrison } 111835b3f0cSDaniele Lacamera 112835b3f0cSDaniele Lacamera if (ca->rho == 0) 1136687e988SArnaldo Carvalho de Melo hybla_recalc_param(sk); 114835b3f0cSDaniele Lacamera 115835b3f0cSDaniele Lacamera rho_fractions = ca->rho_3ls - (ca->rho << 3); 116835b3f0cSDaniele Lacamera 117835b3f0cSDaniele Lacamera if (tp->snd_cwnd < tp->snd_ssthresh) { 118835b3f0cSDaniele Lacamera /* 119835b3f0cSDaniele Lacamera * slow start 120835b3f0cSDaniele Lacamera * INC = 2^RHO - 1 121835b3f0cSDaniele Lacamera * This is done by splitting the rho parameter 122835b3f0cSDaniele Lacamera * into 2 parts: an integer part and a fraction part. 123835b3f0cSDaniele Lacamera * Inrement<<7 is estimated by doing: 124835b3f0cSDaniele Lacamera * [2^(int+fract)]<<7 125835b3f0cSDaniele Lacamera * that is equal to: 126835b3f0cSDaniele Lacamera * (2^int) * [(2^fract) <<7] 127835b3f0cSDaniele Lacamera * 2^int is straightly computed as 1<<int, 128835b3f0cSDaniele Lacamera * while we will use hybla_slowstart_fraction_increment() to 129835b3f0cSDaniele Lacamera * calculate 2^fract in a <<7 value. 130835b3f0cSDaniele Lacamera */ 131835b3f0cSDaniele Lacamera is_slowstart = 1; 132edafe502SDaniele Lacamera increment = ((1 << min(ca->rho, 16U)) * 133edafe502SDaniele Lacamera hybla_fraction(rho_fractions)) - 128; 134835b3f0cSDaniele Lacamera } else { 135835b3f0cSDaniele Lacamera /* 136835b3f0cSDaniele Lacamera * congestion avoidance 137835b3f0cSDaniele Lacamera * INC = RHO^2 / W 138835b3f0cSDaniele Lacamera * as long as increment is estimated as (rho<<7)/window 139835b3f0cSDaniele Lacamera * it already is <<7 and we can easily count its fractions. 140835b3f0cSDaniele Lacamera */ 141835b3f0cSDaniele Lacamera increment = ca->rho2_7ls / tp->snd_cwnd; 142835b3f0cSDaniele Lacamera if (increment < 128) 143835b3f0cSDaniele Lacamera tp->snd_cwnd_cnt++; 144835b3f0cSDaniele Lacamera } 145835b3f0cSDaniele Lacamera 146835b3f0cSDaniele Lacamera odd = increment % 128; 147835b3f0cSDaniele Lacamera tp->snd_cwnd += increment >> 7; 148835b3f0cSDaniele Lacamera ca->snd_cwnd_cents += odd; 149835b3f0cSDaniele Lacamera 150835b3f0cSDaniele Lacamera /* check when fractions goes >=128 and increase cwnd by 1. */ 151835b3f0cSDaniele Lacamera while (ca->snd_cwnd_cents >= 128) { 152835b3f0cSDaniele Lacamera tp->snd_cwnd++; 153835b3f0cSDaniele Lacamera ca->snd_cwnd_cents -= 128; 154835b3f0cSDaniele Lacamera tp->snd_cwnd_cnt = 0; 155835b3f0cSDaniele Lacamera } 1569d2c27e1SDaniele Lacamera /* check when cwnd has not been incremented for a while */ 1579d2c27e1SDaniele Lacamera if (increment == 0 && odd == 0 && tp->snd_cwnd_cnt >= tp->snd_cwnd) { 1589d2c27e1SDaniele Lacamera tp->snd_cwnd++; 1599d2c27e1SDaniele Lacamera tp->snd_cwnd_cnt = 0; 1609d2c27e1SDaniele Lacamera } 161835b3f0cSDaniele Lacamera /* clamp down slowstart cwnd to ssthresh value. */ 162835b3f0cSDaniele Lacamera if (is_slowstart) 163835b3f0cSDaniele Lacamera tp->snd_cwnd = min(tp->snd_cwnd, tp->snd_ssthresh); 164835b3f0cSDaniele Lacamera 165835b3f0cSDaniele Lacamera tp->snd_cwnd = min_t(u32, tp->snd_cwnd, tp->snd_cwnd_clamp); 166835b3f0cSDaniele Lacamera } 167835b3f0cSDaniele Lacamera 168a252bebeSStephen Hemminger static struct tcp_congestion_ops tcp_hybla __read_mostly = { 169835b3f0cSDaniele Lacamera .init = hybla_init, 170835b3f0cSDaniele Lacamera .ssthresh = tcp_reno_ssthresh, 171835b3f0cSDaniele Lacamera .cong_avoid = hybla_cong_avoid, 172835b3f0cSDaniele Lacamera .set_state = hybla_state, 173835b3f0cSDaniele Lacamera 174835b3f0cSDaniele Lacamera .owner = THIS_MODULE, 175835b3f0cSDaniele Lacamera .name = "hybla" 176835b3f0cSDaniele Lacamera }; 177835b3f0cSDaniele Lacamera 178835b3f0cSDaniele Lacamera static int __init hybla_register(void) 179835b3f0cSDaniele Lacamera { 18074975d40SAlexey Dobriyan BUILD_BUG_ON(sizeof(struct hybla) > ICSK_CA_PRIV_SIZE); 181835b3f0cSDaniele Lacamera return tcp_register_congestion_control(&tcp_hybla); 182835b3f0cSDaniele Lacamera } 183835b3f0cSDaniele Lacamera 184835b3f0cSDaniele Lacamera static void __exit hybla_unregister(void) 185835b3f0cSDaniele Lacamera { 186835b3f0cSDaniele Lacamera tcp_unregister_congestion_control(&tcp_hybla); 187835b3f0cSDaniele Lacamera } 188835b3f0cSDaniele Lacamera 189835b3f0cSDaniele Lacamera module_init(hybla_register); 190835b3f0cSDaniele Lacamera module_exit(hybla_unregister); 191835b3f0cSDaniele Lacamera 192835b3f0cSDaniele Lacamera MODULE_AUTHOR("Daniele Lacamera"); 193835b3f0cSDaniele Lacamera MODULE_LICENSE("GPL"); 194835b3f0cSDaniele Lacamera MODULE_DESCRIPTION("TCP Hybla"); 195