tcp_bic.c 6.2 KB

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