tcp_hybla.c 4.9 KB

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