tcp_bic.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. * Binary Increase Congestion control for TCP
  3. *
  4. * This is from the implementation of BICTCP in
  5. * Lison-Xu, Kahaled Harfoush, and Injong Rhee.
  6. * "Binary Increase Congestion Control for Fast, Long Distance
  7. * Networks" in InfoComm 2004
  8. * Available from:
  9. * http://www.csc.ncsu.edu/faculty/rhee/export/bitcp.pdf
  10. *
  11. * Unless BIC is enabled and congestion window is large
  12. * this behaves the same as the original Reno.
  13. */
  14. #include <linux/mm.h>
  15. #include <linux/module.h>
  16. #include <net/tcp.h>
  17. #define BICTCP_BETA_SCALE 1024 /* Scale factor beta calculation
  18. * max_cwnd = snd_cwnd * beta
  19. */
  20. #define BICTCP_B 4 /*
  21. * In binary search,
  22. * go to point (max+min)/N
  23. */
  24. static int fast_convergence = 1;
  25. static int max_increment = 16;
  26. static int low_window = 14;
  27. static int beta = 819; /* = 819/1024 (BICTCP_BETA_SCALE) */
  28. static int initial_ssthresh;
  29. static int smooth_part = 20;
  30. module_param(fast_convergence, int, 0644);
  31. MODULE_PARM_DESC(fast_convergence, "turn on/off fast convergence");
  32. module_param(max_increment, int, 0644);
  33. MODULE_PARM_DESC(max_increment, "Limit on increment allowed during binary search");
  34. module_param(low_window, int, 0644);
  35. MODULE_PARM_DESC(low_window, "lower bound on congestion window (for TCP friendliness)");
  36. module_param(beta, int, 0644);
  37. MODULE_PARM_DESC(beta, "beta for multiplicative increase");
  38. module_param(initial_ssthresh, int, 0644);
  39. MODULE_PARM_DESC(initial_ssthresh, "initial value of slow start threshold");
  40. module_param(smooth_part, int, 0644);
  41. MODULE_PARM_DESC(smooth_part, "log(B/(B*Smin))/log(B/(B-1))+B, # of RTT from Wmax-B to Wmax");
  42. /* BIC TCP Parameters */
  43. struct bictcp {
  44. u32 cnt; /* increase cwnd by 1 after ACKs */
  45. u32 last_max_cwnd; /* last maximum snd_cwnd */
  46. u32 loss_cwnd; /* congestion window at last loss */
  47. u32 last_cwnd; /* the last snd_cwnd */
  48. u32 last_time; /* time when updated last_cwnd */
  49. u32 epoch_start; /* beginning of an epoch */
  50. #define ACK_RATIO_SHIFT 4
  51. u32 delayed_ack; /* estimate the ratio of Packets/ACKs << 4 */
  52. };
  53. static inline void bictcp_reset(struct bictcp *ca)
  54. {
  55. ca->cnt = 0;
  56. ca->last_max_cwnd = 0;
  57. ca->loss_cwnd = 0;
  58. ca->last_cwnd = 0;
  59. ca->last_time = 0;
  60. ca->epoch_start = 0;
  61. ca->delayed_ack = 2 << ACK_RATIO_SHIFT;
  62. }
  63. static void bictcp_init(struct sock *sk)
  64. {
  65. bictcp_reset(inet_csk_ca(sk));
  66. if (initial_ssthresh)
  67. tcp_sk(sk)->snd_ssthresh = initial_ssthresh;
  68. }
  69. /*
  70. * Compute congestion window to use.
  71. */
  72. static inline void bictcp_update(struct bictcp *ca, u32 cwnd)
  73. {
  74. if (ca->last_cwnd == cwnd &&
  75. (s32)(tcp_time_stamp - ca->last_time) <= HZ / 32)
  76. return;
  77. ca->last_cwnd = cwnd;
  78. ca->last_time = tcp_time_stamp;
  79. if (ca->epoch_start == 0) /* record the beginning of an epoch */
  80. ca->epoch_start = tcp_time_stamp;
  81. /* start off normal */
  82. if (cwnd <= low_window) {
  83. ca->cnt = cwnd;
  84. return;
  85. }
  86. /* binary increase */
  87. if (cwnd < ca->last_max_cwnd) {
  88. __u32 dist = (ca->last_max_cwnd - cwnd)
  89. / BICTCP_B;
  90. if (dist > max_increment)
  91. /* linear increase */
  92. ca->cnt = cwnd / max_increment;
  93. else if (dist <= 1U)
  94. /* binary search increase */
  95. ca->cnt = (cwnd * smooth_part) / BICTCP_B;
  96. else
  97. /* binary search increase */
  98. ca->cnt = cwnd / dist;
  99. } else {
  100. /* slow start AMD linear increase */
  101. if (cwnd < ca->last_max_cwnd + BICTCP_B)
  102. /* slow start */
  103. ca->cnt = (cwnd * smooth_part) / BICTCP_B;
  104. else if (cwnd < ca->last_max_cwnd + max_increment*(BICTCP_B-1))
  105. /* slow start */
  106. ca->cnt = (cwnd * (BICTCP_B-1))
  107. / (cwnd - ca->last_max_cwnd);
  108. else
  109. /* linear increase */
  110. ca->cnt = cwnd / max_increment;
  111. }
  112. /* if in slow start or link utilization is very low */
  113. if (ca->loss_cwnd == 0) {
  114. if (ca->cnt > 20) /* increase cwnd 5% per RTT */
  115. ca->cnt = 20;
  116. }
  117. ca->cnt = (ca->cnt << ACK_RATIO_SHIFT) / ca->delayed_ack;
  118. if (ca->cnt == 0) /* cannot be zero */
  119. ca->cnt = 1;
  120. }
  121. static void bictcp_cong_avoid(struct sock *sk, u32 ack, u32 in_flight)
  122. {
  123. struct tcp_sock *tp = tcp_sk(sk);
  124. struct bictcp *ca = inet_csk_ca(sk);
  125. if (!tcp_is_cwnd_limited(sk, in_flight))
  126. return;
  127. if (tp->snd_cwnd <= tp->snd_ssthresh)
  128. tcp_slow_start(tp);
  129. else {
  130. bictcp_update(ca, tp->snd_cwnd);
  131. /* In dangerous area, increase slowly.
  132. * In theory this is tp->snd_cwnd += 1 / tp->snd_cwnd
  133. */
  134. if (tp->snd_cwnd_cnt >= ca->cnt) {
  135. if (tp->snd_cwnd < tp->snd_cwnd_clamp)
  136. tp->snd_cwnd++;
  137. tp->snd_cwnd_cnt = 0;
  138. } else
  139. tp->snd_cwnd_cnt++;
  140. }
  141. }
  142. /*
  143. * behave like Reno until low_window is reached,
  144. * then increase congestion window slowly
  145. */
  146. static u32 bictcp_recalc_ssthresh(struct sock *sk)
  147. {
  148. const struct tcp_sock *tp = tcp_sk(sk);
  149. struct bictcp *ca = inet_csk_ca(sk);
  150. ca->epoch_start = 0; /* end of epoch */
  151. /* Wmax and fast convergence */
  152. if (tp->snd_cwnd < ca->last_max_cwnd && fast_convergence)
  153. ca->last_max_cwnd = (tp->snd_cwnd * (BICTCP_BETA_SCALE + beta))
  154. / (2 * BICTCP_BETA_SCALE);
  155. else
  156. ca->last_max_cwnd = tp->snd_cwnd;
  157. ca->loss_cwnd = tp->snd_cwnd;
  158. if (tp->snd_cwnd <= low_window)
  159. return max(tp->snd_cwnd >> 1U, 2U);
  160. else
  161. return max((tp->snd_cwnd * beta) / BICTCP_BETA_SCALE, 2U);
  162. }
  163. static u32 bictcp_undo_cwnd(struct sock *sk)
  164. {
  165. const struct tcp_sock *tp = tcp_sk(sk);
  166. const struct bictcp *ca = inet_csk_ca(sk);
  167. return max(tp->snd_cwnd, ca->last_max_cwnd);
  168. }
  169. static void bictcp_state(struct sock *sk, u8 new_state)
  170. {
  171. if (new_state == TCP_CA_Loss)
  172. bictcp_reset(inet_csk_ca(sk));
  173. }
  174. /* Track delayed acknowledgment ratio using sliding window
  175. * ratio = (15*ratio + sample) / 16
  176. */
  177. static void bictcp_acked(struct sock *sk, u32 cnt, s32 rtt)
  178. {
  179. const struct inet_connection_sock *icsk = inet_csk(sk);
  180. if (icsk->icsk_ca_state == TCP_CA_Open) {
  181. struct bictcp *ca = inet_csk_ca(sk);
  182. cnt -= ca->delayed_ack >> ACK_RATIO_SHIFT;
  183. ca->delayed_ack += cnt;
  184. }
  185. }
  186. static struct tcp_congestion_ops bictcp = {
  187. .init = bictcp_init,
  188. .ssthresh = bictcp_recalc_ssthresh,
  189. .cong_avoid = bictcp_cong_avoid,
  190. .set_state = bictcp_state,
  191. .undo_cwnd = bictcp_undo_cwnd,
  192. .pkts_acked = bictcp_acked,
  193. .owner = THIS_MODULE,
  194. .name = "bic",
  195. };
  196. static int __init bictcp_register(void)
  197. {
  198. BUILD_BUG_ON(sizeof(struct bictcp) > ICSK_CA_PRIV_SIZE);
  199. return tcp_register_congestion_control(&bictcp);
  200. }
  201. static void __exit bictcp_unregister(void)
  202. {
  203. tcp_unregister_congestion_control(&bictcp);
  204. }
  205. module_init(bictcp_register);
  206. module_exit(bictcp_unregister);
  207. MODULE_AUTHOR("Stephen Hemminger");
  208. MODULE_LICENSE("GPL");
  209. MODULE_DESCRIPTION("BIC TCP");