tcp_bic.c 6.0 KB

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