tcp_hybla.c 4.6 KB

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