tcp_htcp.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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 sock *sk)
  49. {
  50. const struct tcp_sock *tp = tcp_sk(sk);
  51. struct htcp *ca = inet_csk_ca(sk);
  52. ca->ccount = ca->undo_ccount;
  53. ca->maxRTT = ca->undo_maxRTT;
  54. ca->old_maxB = ca->undo_old_maxB;
  55. return max(tp->snd_cwnd, (tp->snd_ssthresh<<7)/ca->beta);
  56. }
  57. static inline void measure_rtt(struct sock *sk)
  58. {
  59. const struct inet_connection_sock *icsk = inet_csk(sk);
  60. const struct tcp_sock *tp = tcp_sk(sk);
  61. struct htcp *ca = inet_csk_ca(sk);
  62. u32 srtt = tp->srtt>>3;
  63. /* keep track of minimum RTT seen so far, minRTT is zero at first */
  64. if (ca->minRTT > srtt || !ca->minRTT)
  65. ca->minRTT = srtt;
  66. /* max RTT */
  67. if (icsk->icsk_ca_state == TCP_CA_Open && tp->snd_ssthresh < 0xFFFF && ca->ccount > 3) {
  68. if (ca->maxRTT < ca->minRTT)
  69. ca->maxRTT = ca->minRTT;
  70. if (ca->maxRTT < srtt && srtt <= ca->maxRTT+HZ/50)
  71. ca->maxRTT = srtt;
  72. }
  73. }
  74. static void measure_achieved_throughput(struct sock *sk, u32 pkts_acked)
  75. {
  76. const struct inet_connection_sock *icsk = inet_csk(sk);
  77. const struct tcp_sock *tp = tcp_sk(sk);
  78. struct htcp *ca = inet_csk_ca(sk);
  79. u32 now = tcp_time_stamp;
  80. /* achieved throughput calculations */
  81. if (icsk->icsk_ca_state != TCP_CA_Open &&
  82. icsk->icsk_ca_state != TCP_CA_Disorder) {
  83. ca->packetcount = 0;
  84. ca->lasttime = now;
  85. return;
  86. }
  87. ca->packetcount += pkts_acked;
  88. if (ca->packetcount >= tp->snd_cwnd - (ca->alpha>>7? : 1)
  89. && now - ca->lasttime >= ca->minRTT
  90. && ca->minRTT > 0) {
  91. __u32 cur_Bi = ca->packetcount*HZ/(now - ca->lasttime);
  92. if (ca->ccount <= 3) {
  93. /* just after backoff */
  94. ca->minB = ca->maxB = ca->Bi = cur_Bi;
  95. } else {
  96. ca->Bi = (3*ca->Bi + cur_Bi)/4;
  97. if (ca->Bi > ca->maxB)
  98. ca->maxB = ca->Bi;
  99. if (ca->minB > ca->maxB)
  100. ca->minB = ca->maxB;
  101. }
  102. ca->packetcount = 0;
  103. ca->lasttime = now;
  104. }
  105. }
  106. static inline void htcp_beta_update(struct htcp *ca, u32 minRTT, u32 maxRTT)
  107. {
  108. if (use_bandwidth_switch) {
  109. u32 maxB = ca->maxB;
  110. u32 old_maxB = ca->old_maxB;
  111. ca->old_maxB = ca->maxB;
  112. if (!between(5*maxB, 4*old_maxB, 6*old_maxB)) {
  113. ca->beta = BETA_MIN;
  114. ca->modeswitch = 0;
  115. return;
  116. }
  117. }
  118. if (ca->modeswitch && minRTT > max(HZ/100, 1) && maxRTT) {
  119. ca->beta = (minRTT<<7)/maxRTT;
  120. if (ca->beta < BETA_MIN)
  121. ca->beta = BETA_MIN;
  122. else if (ca->beta > BETA_MAX)
  123. ca->beta = BETA_MAX;
  124. } else {
  125. ca->beta = BETA_MIN;
  126. ca->modeswitch = 1;
  127. }
  128. }
  129. static inline void htcp_alpha_update(struct htcp *ca)
  130. {
  131. u32 minRTT = ca->minRTT;
  132. u32 factor = 1;
  133. u32 diff = ca->ccount * minRTT; /* time since last backoff */
  134. if (diff > HZ) {
  135. diff -= HZ;
  136. factor = 1+ ( 10*diff + ((diff/2)*(diff/2)/HZ) )/HZ;
  137. }
  138. if (use_rtt_scaling && minRTT) {
  139. u32 scale = (HZ<<3)/(10*minRTT);
  140. scale = min(max(scale, 1U<<2), 10U<<3); /* clamping ratio to interval [0.5,10]<<3 */
  141. factor = (factor<<3)/scale;
  142. if (!factor)
  143. factor = 1;
  144. }
  145. ca->alpha = 2*factor*((1<<7)-ca->beta);
  146. if (!ca->alpha)
  147. ca->alpha = ALPHA_BASE;
  148. }
  149. /* After we have the rtt data to calculate beta, we'd still prefer to wait one
  150. * rtt before we adjust our beta to ensure we are working from a consistent
  151. * data.
  152. *
  153. * This function should be called when we hit a congestion event since only at
  154. * that point do we really have a real sense of maxRTT (the queues en route
  155. * were getting just too full now).
  156. */
  157. static void htcp_param_update(struct sock *sk)
  158. {
  159. struct htcp *ca = inet_csk_ca(sk);
  160. u32 minRTT = ca->minRTT;
  161. u32 maxRTT = ca->maxRTT;
  162. htcp_beta_update(ca, minRTT, maxRTT);
  163. htcp_alpha_update(ca);
  164. /* add slowly fading memory for maxRTT to accommodate routing changes etc */
  165. if (minRTT > 0 && maxRTT > minRTT)
  166. ca->maxRTT = minRTT + ((maxRTT-minRTT)*95)/100;
  167. }
  168. static u32 htcp_recalc_ssthresh(struct sock *sk)
  169. {
  170. const struct tcp_sock *tp = tcp_sk(sk);
  171. const struct htcp *ca = inet_csk_ca(sk);
  172. htcp_param_update(sk);
  173. return max((tp->snd_cwnd * ca->beta) >> 7, 2U);
  174. }
  175. static void htcp_cong_avoid(struct sock *sk, u32 ack, u32 rtt,
  176. u32 in_flight, int data_acked)
  177. {
  178. struct tcp_sock *tp = tcp_sk(sk);
  179. struct htcp *ca = inet_csk_ca(sk);
  180. if (!tcp_is_cwnd_limited(sk, in_flight))
  181. return;
  182. if (tp->snd_cwnd <= tp->snd_ssthresh)
  183. tcp_slow_start(tp);
  184. else {
  185. measure_rtt(sk);
  186. /* keep track of number of round-trip times since last backoff event */
  187. if (ca->snd_cwnd_cnt2++ > tp->snd_cwnd) {
  188. ca->ccount++;
  189. ca->snd_cwnd_cnt2 = 0;
  190. htcp_alpha_update(ca);
  191. }
  192. /* In dangerous area, increase slowly.
  193. * In theory this is tp->snd_cwnd += alpha / tp->snd_cwnd
  194. */
  195. if ((tp->snd_cwnd_cnt++ * ca->alpha)>>7 >= tp->snd_cwnd) {
  196. if (tp->snd_cwnd < tp->snd_cwnd_clamp)
  197. tp->snd_cwnd++;
  198. tp->snd_cwnd_cnt = 0;
  199. }
  200. }
  201. }
  202. /* Lower bound on congestion window. */
  203. static u32 htcp_min_cwnd(struct sock *sk)
  204. {
  205. const struct tcp_sock *tp = tcp_sk(sk);
  206. return tp->snd_ssthresh;
  207. }
  208. static void htcp_init(struct sock *sk)
  209. {
  210. struct htcp *ca = inet_csk_ca(sk);
  211. memset(ca, 0, sizeof(struct htcp));
  212. ca->alpha = ALPHA_BASE;
  213. ca->beta = BETA_MIN;
  214. }
  215. static void htcp_state(struct sock *sk, u8 new_state)
  216. {
  217. switch (new_state) {
  218. case TCP_CA_CWR:
  219. case TCP_CA_Recovery:
  220. case TCP_CA_Loss:
  221. htcp_reset(inet_csk_ca(sk));
  222. break;
  223. }
  224. }
  225. static struct tcp_congestion_ops htcp = {
  226. .init = htcp_init,
  227. .ssthresh = htcp_recalc_ssthresh,
  228. .min_cwnd = htcp_min_cwnd,
  229. .cong_avoid = htcp_cong_avoid,
  230. .set_state = htcp_state,
  231. .undo_cwnd = htcp_cwnd_undo,
  232. .pkts_acked = measure_achieved_throughput,
  233. .owner = THIS_MODULE,
  234. .name = "htcp",
  235. };
  236. static int __init htcp_register(void)
  237. {
  238. BUG_ON(sizeof(struct htcp) > ICSK_CA_PRIV_SIZE);
  239. BUILD_BUG_ON(BETA_MIN >= BETA_MAX);
  240. if (!use_bandwidth_switch)
  241. htcp.pkts_acked = NULL;
  242. return tcp_register_congestion_control(&htcp);
  243. }
  244. static void __exit htcp_unregister(void)
  245. {
  246. tcp_unregister_congestion_control(&htcp);
  247. }
  248. module_init(htcp_register);
  249. module_exit(htcp_unregister);
  250. MODULE_AUTHOR("Baruch Even");
  251. MODULE_LICENSE("GPL");
  252. MODULE_DESCRIPTION("H-TCP");