tcp_htcp.c 7.5 KB

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