tcp_hybla.c 4.9 KB

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