tcp_htcp.c 7.4 KB

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