tcp_htcp.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*
  2. * H-TCP congestion control. The algorithm is detailed in:
  3. * R.N.Shorten, D.J.Leith:
  4. * "H-TCP: TCP for high-speed and long-distance networks"
  5. * Proc. PFLDnet, Argonne, 2004.
  6. * http://www.hamilton.ie/net/htcp3.pdf
  7. */
  8. #include <linux/config.h>
  9. #include <linux/mm.h>
  10. #include <linux/module.h>
  11. #include <net/tcp.h>
  12. #define ALPHA_BASE (1<<7) /* 1.0 with shift << 7 */
  13. #define BETA_MIN (1<<6) /* 0.5 with shift << 7 */
  14. #define BETA_MAX 102 /* 0.8 with shift << 7 */
  15. static int use_rtt_scaling = 1;
  16. module_param(use_rtt_scaling, int, 0644);
  17. MODULE_PARM_DESC(use_rtt_scaling, "turn on/off RTT scaling");
  18. static int use_bandwidth_switch = 1;
  19. module_param(use_bandwidth_switch, int, 0644);
  20. MODULE_PARM_DESC(use_bandwidth_switch, "turn on/off bandwidth switcher");
  21. struct htcp {
  22. u16 alpha; /* Fixed point arith, << 7 */
  23. u8 beta; /* Fixed point arith, << 7 */
  24. u8 modeswitch; /* Delay modeswitch until we had at least one congestion event */
  25. u8 ccount; /* Number of RTTs since last congestion event */
  26. u8 undo_ccount;
  27. u16 packetcount;
  28. u32 minRTT;
  29. u32 maxRTT;
  30. u32 snd_cwnd_cnt2;
  31. u32 undo_maxRTT;
  32. u32 undo_old_maxB;
  33. /* Bandwidth estimation */
  34. u32 minB;
  35. u32 maxB;
  36. u32 old_maxB;
  37. u32 Bi;
  38. u32 lasttime;
  39. };
  40. static inline void htcp_reset(struct htcp *ca)
  41. {
  42. ca->undo_ccount = ca->ccount;
  43. ca->undo_maxRTT = ca->maxRTT;
  44. ca->undo_old_maxB = ca->old_maxB;
  45. ca->ccount = 0;
  46. ca->snd_cwnd_cnt2 = 0;
  47. }
  48. static u32 htcp_cwnd_undo(struct tcp_sock *tp)
  49. {
  50. struct htcp *ca = tcp_ca(tp);
  51. ca->ccount = ca->undo_ccount;
  52. ca->maxRTT = ca->undo_maxRTT;
  53. ca->old_maxB = ca->undo_old_maxB;
  54. return max(tp->snd_cwnd, (tp->snd_ssthresh<<7)/ca->beta);
  55. }
  56. static inline void measure_rtt(struct tcp_sock *tp)
  57. {
  58. struct htcp *ca = tcp_ca(tp);
  59. u32 srtt = tp->srtt>>3;
  60. /* keep track of minimum RTT seen so far, minRTT is zero at first */
  61. if (ca->minRTT > srtt || !ca->minRTT)
  62. ca->minRTT = srtt;
  63. /* max RTT */
  64. if (tp->ca_state == TCP_CA_Open && tp->snd_ssthresh < 0xFFFF && ca->ccount > 3) {
  65. if (ca->maxRTT < ca->minRTT)
  66. ca->maxRTT = ca->minRTT;
  67. if (ca->maxRTT < srtt && srtt <= ca->maxRTT+HZ/50)
  68. ca->maxRTT = srtt;
  69. }
  70. }
  71. static void measure_achieved_throughput(struct tcp_sock *tp, u32 pkts_acked)
  72. {
  73. struct htcp *ca = tcp_ca(tp);
  74. u32 now = tcp_time_stamp;
  75. /* achieved throughput calculations */
  76. if (tp->ca_state != TCP_CA_Open && tp->ca_state != TCP_CA_Disorder) {
  77. ca->packetcount = 0;
  78. ca->lasttime = now;
  79. return;
  80. }
  81. ca->packetcount += pkts_acked;
  82. if (ca->packetcount >= tp->snd_cwnd - (ca->alpha>>7? : 1)
  83. && now - ca->lasttime >= ca->minRTT
  84. && ca->minRTT > 0) {
  85. __u32 cur_Bi = ca->packetcount*HZ/(now - ca->lasttime);
  86. if (ca->ccount <= 3) {
  87. /* just after backoff */
  88. ca->minB = ca->maxB = ca->Bi = cur_Bi;
  89. } else {
  90. ca->Bi = (3*ca->Bi + cur_Bi)/4;
  91. if (ca->Bi > ca->maxB)
  92. ca->maxB = ca->Bi;
  93. if (ca->minB > ca->maxB)
  94. ca->minB = ca->maxB;
  95. }
  96. ca->packetcount = 0;
  97. ca->lasttime = now;
  98. }
  99. }
  100. static inline void htcp_beta_update(struct htcp *ca, u32 minRTT, u32 maxRTT)
  101. {
  102. if (use_bandwidth_switch) {
  103. u32 maxB = ca->maxB;
  104. u32 old_maxB = ca->old_maxB;
  105. ca->old_maxB = ca->maxB;
  106. if (!between(5*maxB, 4*old_maxB, 6*old_maxB)) {
  107. ca->beta = BETA_MIN;
  108. ca->modeswitch = 0;
  109. return;
  110. }
  111. }
  112. if (ca->modeswitch && minRTT > max(HZ/100, 1) && maxRTT) {
  113. ca->beta = (minRTT<<7)/maxRTT;
  114. if (ca->beta < BETA_MIN)
  115. ca->beta = BETA_MIN;
  116. else if (ca->beta > BETA_MAX)
  117. ca->beta = BETA_MAX;
  118. } else {
  119. ca->beta = BETA_MIN;
  120. ca->modeswitch = 1;
  121. }
  122. }
  123. static inline void htcp_alpha_update(struct htcp *ca)
  124. {
  125. u32 minRTT = ca->minRTT;
  126. u32 factor = 1;
  127. u32 diff = ca->ccount * minRTT; /* time since last backoff */
  128. if (diff > HZ) {
  129. diff -= HZ;
  130. factor = 1+ ( 10*diff + ((diff/2)*(diff/2)/HZ) )/HZ;
  131. }
  132. if (use_rtt_scaling && minRTT) {
  133. u32 scale = (HZ<<3)/(10*minRTT);
  134. scale = min(max(scale, 1U<<2), 10U<<3); /* clamping ratio to interval [0.5,10]<<3 */
  135. factor = (factor<<3)/scale;
  136. if (!factor)
  137. factor = 1;
  138. }
  139. ca->alpha = 2*factor*((1<<7)-ca->beta);
  140. if (!ca->alpha)
  141. ca->alpha = ALPHA_BASE;
  142. }
  143. /* After we have the rtt data to calculate beta, we'd still prefer to wait one
  144. * rtt before we adjust our beta to ensure we are working from a consistent
  145. * data.
  146. *
  147. * This function should be called when we hit a congestion event since only at
  148. * that point do we really have a real sense of maxRTT (the queues en route
  149. * were getting just too full now).
  150. */
  151. static void htcp_param_update(struct tcp_sock *tp)
  152. {
  153. struct htcp *ca = tcp_ca(tp);
  154. u32 minRTT = ca->minRTT;
  155. u32 maxRTT = ca->maxRTT;
  156. htcp_beta_update(ca, minRTT, maxRTT);
  157. htcp_alpha_update(ca);
  158. /* add slowly fading memory for maxRTT to accommodate routing changes etc */
  159. if (minRTT > 0 && maxRTT > minRTT)
  160. ca->maxRTT = minRTT + ((maxRTT-minRTT)*95)/100;
  161. }
  162. static u32 htcp_recalc_ssthresh(struct tcp_sock *tp)
  163. {
  164. struct htcp *ca = tcp_ca(tp);
  165. htcp_param_update(tp);
  166. return max((tp->snd_cwnd * ca->beta) >> 7, 2U);
  167. }
  168. static void htcp_cong_avoid(struct tcp_sock *tp, u32 ack, u32 rtt,
  169. u32 in_flight, int data_acked)
  170. {
  171. struct htcp *ca = tcp_ca(tp);
  172. if (in_flight < tp->snd_cwnd)
  173. return;
  174. if (tp->snd_cwnd <= tp->snd_ssthresh) {
  175. /* In "safe" area, increase. */
  176. if (tp->snd_cwnd < tp->snd_cwnd_clamp)
  177. tp->snd_cwnd++;
  178. } else {
  179. measure_rtt(tp);
  180. /* keep track of number of round-trip times since last backoff event */
  181. if (ca->snd_cwnd_cnt2++ > tp->snd_cwnd) {
  182. ca->ccount++;
  183. ca->snd_cwnd_cnt2 = 0;
  184. htcp_alpha_update(ca);
  185. }
  186. /* In dangerous area, increase slowly.
  187. * In theory this is tp->snd_cwnd += alpha / tp->snd_cwnd
  188. */
  189. if ((tp->snd_cwnd_cnt++ * ca->alpha)>>7 >= tp->snd_cwnd) {
  190. if (tp->snd_cwnd < tp->snd_cwnd_clamp)
  191. tp->snd_cwnd++;
  192. tp->snd_cwnd_cnt = 0;
  193. ca->ccount++;
  194. }
  195. }
  196. }
  197. /* Lower bound on congestion window. */
  198. static u32 htcp_min_cwnd(struct tcp_sock *tp)
  199. {
  200. return tp->snd_ssthresh;
  201. }
  202. static void htcp_init(struct tcp_sock *tp)
  203. {
  204. struct htcp *ca = tcp_ca(tp);
  205. memset(ca, 0, sizeof(struct htcp));
  206. ca->alpha = ALPHA_BASE;
  207. ca->beta = BETA_MIN;
  208. }
  209. static void htcp_state(struct tcp_sock *tp, u8 new_state)
  210. {
  211. switch (new_state) {
  212. case TCP_CA_CWR:
  213. case TCP_CA_Recovery:
  214. case TCP_CA_Loss:
  215. htcp_reset(tcp_ca(tp));
  216. break;
  217. }
  218. }
  219. static struct tcp_congestion_ops htcp = {
  220. .init = htcp_init,
  221. .ssthresh = htcp_recalc_ssthresh,
  222. .min_cwnd = htcp_min_cwnd,
  223. .cong_avoid = htcp_cong_avoid,
  224. .set_state = htcp_state,
  225. .undo_cwnd = htcp_cwnd_undo,
  226. .pkts_acked = measure_achieved_throughput,
  227. .owner = THIS_MODULE,
  228. .name = "htcp",
  229. };
  230. static int __init htcp_register(void)
  231. {
  232. BUG_ON(sizeof(struct htcp) > TCP_CA_PRIV_SIZE);
  233. BUILD_BUG_ON(BETA_MIN >= BETA_MAX);
  234. if (!use_bandwidth_switch)
  235. htcp.pkts_acked = NULL;
  236. return tcp_register_congestion_control(&htcp);
  237. }
  238. static void __exit htcp_unregister(void)
  239. {
  240. tcp_unregister_congestion_control(&htcp);
  241. }
  242. module_init(htcp_register);
  243. module_exit(htcp_unregister);
  244. MODULE_AUTHOR("Baruch Even");
  245. MODULE_LICENSE("GPL");
  246. MODULE_DESCRIPTION("H-TCP");