tcp_bic.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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/config.h>
  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 = 32;
  27. static int low_window = 14;
  28. static int beta = 819; /* = 819/1024 (BICTCP_BETA_SCALE) */
  29. static int low_utilization_threshold = 153;
  30. static int low_utilization_period = 2;
  31. static int initial_ssthresh = 100;
  32. static int smooth_part = 20;
  33. module_param(fast_convergence, int, 0644);
  34. MODULE_PARM_DESC(fast_convergence, "turn on/off fast convergence");
  35. module_param(max_increment, int, 0644);
  36. MODULE_PARM_DESC(max_increment, "Limit on increment allowed during binary search");
  37. module_param(low_window, int, 0644);
  38. MODULE_PARM_DESC(low_window, "lower bound on congestion window (for TCP friendliness)");
  39. module_param(beta, int, 0644);
  40. MODULE_PARM_DESC(beta, "beta for multiplicative increase");
  41. module_param(low_utilization_threshold, int, 0644);
  42. MODULE_PARM_DESC(low_utilization_threshold, "percent (scaled by 1024) for low utilization mode");
  43. module_param(low_utilization_period, int, 0644);
  44. MODULE_PARM_DESC(low_utilization_period, "if average delay exceeds then goto to low utilization mode (seconds)");
  45. module_param(initial_ssthresh, int, 0644);
  46. MODULE_PARM_DESC(initial_ssthresh, "initial value of slow start threshold");
  47. module_param(smooth_part, int, 0644);
  48. MODULE_PARM_DESC(smooth_part, "log(B/(B*Smin))/log(B/(B-1))+B, # of RTT from Wmax-B to Wmax");
  49. /* BIC TCP Parameters */
  50. struct bictcp {
  51. u32 cnt; /* increase cwnd by 1 after ACKs */
  52. u32 last_max_cwnd; /* last maximum snd_cwnd */
  53. u32 loss_cwnd; /* congestion window at last loss */
  54. u32 last_cwnd; /* the last snd_cwnd */
  55. u32 last_time; /* time when updated last_cwnd */
  56. u32 delay_min; /* min delay */
  57. u32 delay_max; /* max delay */
  58. u32 last_delay;
  59. u8 low_utilization;/* 0: high; 1: low */
  60. u32 low_utilization_start; /* starting time of low utilization detection*/
  61. u32 epoch_start; /* beginning of an epoch */
  62. #define ACK_RATIO_SHIFT 4
  63. u32 delayed_ack; /* estimate the ratio of Packets/ACKs << 4 */
  64. };
  65. static inline void bictcp_reset(struct bictcp *ca)
  66. {
  67. ca->cnt = 0;
  68. ca->last_max_cwnd = 0;
  69. ca->loss_cwnd = 0;
  70. ca->last_cwnd = 0;
  71. ca->last_time = 0;
  72. ca->delay_min = 0;
  73. ca->delay_max = 0;
  74. ca->last_delay = 0;
  75. ca->low_utilization = 0;
  76. ca->low_utilization_start = 0;
  77. ca->epoch_start = 0;
  78. ca->delayed_ack = 2 << ACK_RATIO_SHIFT;
  79. }
  80. static void bictcp_init(struct sock *sk)
  81. {
  82. bictcp_reset(inet_csk_ca(sk));
  83. if (initial_ssthresh)
  84. tcp_sk(sk)->snd_ssthresh = initial_ssthresh;
  85. }
  86. /*
  87. * Compute congestion window to use.
  88. */
  89. static inline void bictcp_update(struct bictcp *ca, u32 cwnd)
  90. {
  91. if (ca->last_cwnd == cwnd &&
  92. (s32)(tcp_time_stamp - ca->last_time) <= HZ / 32)
  93. return;
  94. ca->last_cwnd = cwnd;
  95. ca->last_time = tcp_time_stamp;
  96. if (ca->epoch_start == 0) /* record the beginning of an epoch */
  97. ca->epoch_start = tcp_time_stamp;
  98. /* start off normal */
  99. if (cwnd <= low_window) {
  100. ca->cnt = cwnd;
  101. return;
  102. }
  103. /* binary increase */
  104. if (cwnd < ca->last_max_cwnd) {
  105. __u32 dist = (ca->last_max_cwnd - cwnd)
  106. / BICTCP_B;
  107. if (dist > max_increment)
  108. /* linear increase */
  109. ca->cnt = cwnd / max_increment;
  110. else if (dist <= 1U)
  111. /* binary search increase */
  112. ca->cnt = (cwnd * smooth_part) / BICTCP_B;
  113. else
  114. /* binary search increase */
  115. ca->cnt = cwnd / dist;
  116. } else {
  117. /* slow start AMD linear increase */
  118. if (cwnd < ca->last_max_cwnd + BICTCP_B)
  119. /* slow start */
  120. ca->cnt = (cwnd * smooth_part) / BICTCP_B;
  121. else if (cwnd < ca->last_max_cwnd + max_increment*(BICTCP_B-1))
  122. /* slow start */
  123. ca->cnt = (cwnd * (BICTCP_B-1))
  124. / (cwnd - ca->last_max_cwnd);
  125. else
  126. /* linear increase */
  127. ca->cnt = cwnd / max_increment;
  128. }
  129. /* if in slow start or link utilization is very low */
  130. if ( ca->loss_cwnd == 0 ||
  131. (cwnd > ca->loss_cwnd && ca->low_utilization)) {
  132. if (ca->cnt > 20) /* increase cwnd 5% per RTT */
  133. ca->cnt = 20;
  134. }
  135. ca->cnt = (ca->cnt << ACK_RATIO_SHIFT) / ca->delayed_ack;
  136. if (ca->cnt == 0) /* cannot be zero */
  137. ca->cnt = 1;
  138. }
  139. /* Detect low utilization in congestion avoidance */
  140. static inline void bictcp_low_utilization(struct sock *sk, int flag)
  141. {
  142. const struct tcp_sock *tp = tcp_sk(sk);
  143. struct bictcp *ca = inet_csk_ca(sk);
  144. u32 dist, delay;
  145. /* No time stamp */
  146. if (!(tp->rx_opt.saw_tstamp && tp->rx_opt.rcv_tsecr) ||
  147. /* Discard delay samples right after fast recovery */
  148. tcp_time_stamp < ca->epoch_start + HZ ||
  149. /* this delay samples may not be accurate */
  150. flag == 0) {
  151. ca->last_delay = 0;
  152. goto notlow;
  153. }
  154. delay = ca->last_delay<<3; /* use the same scale as tp->srtt*/
  155. ca->last_delay = tcp_time_stamp - tp->rx_opt.rcv_tsecr;
  156. if (delay == 0) /* no previous delay sample */
  157. goto notlow;
  158. /* first time call or link delay decreases */
  159. if (ca->delay_min == 0 || ca->delay_min > delay) {
  160. ca->delay_min = ca->delay_max = delay;
  161. goto notlow;
  162. }
  163. if (ca->delay_max < delay)
  164. ca->delay_max = delay;
  165. /* utilization is low, if avg delay < dist*threshold
  166. for checking_period time */
  167. dist = ca->delay_max - ca->delay_min;
  168. if (dist <= ca->delay_min>>6 ||
  169. tp->srtt - ca->delay_min >= (dist*low_utilization_threshold)>>10)
  170. goto notlow;
  171. if (ca->low_utilization_start == 0) {
  172. ca->low_utilization = 0;
  173. ca->low_utilization_start = tcp_time_stamp;
  174. } else if ((s32)(tcp_time_stamp - ca->low_utilization_start)
  175. > low_utilization_period*HZ) {
  176. ca->low_utilization = 1;
  177. }
  178. return;
  179. notlow:
  180. ca->low_utilization = 0;
  181. ca->low_utilization_start = 0;
  182. }
  183. static void bictcp_cong_avoid(struct sock *sk, u32 ack,
  184. u32 seq_rtt, u32 in_flight, int data_acked)
  185. {
  186. struct tcp_sock *tp = tcp_sk(sk);
  187. struct bictcp *ca = inet_csk_ca(sk);
  188. bictcp_low_utilization(sk, data_acked);
  189. if (in_flight < tp->snd_cwnd)
  190. return;
  191. if (tp->snd_cwnd <= tp->snd_ssthresh) {
  192. /* In "safe" area, increase. */
  193. if (tp->snd_cwnd < tp->snd_cwnd_clamp)
  194. tp->snd_cwnd++;
  195. } else {
  196. bictcp_update(ca, tp->snd_cwnd);
  197. /* In dangerous area, increase slowly.
  198. * In theory this is tp->snd_cwnd += 1 / tp->snd_cwnd
  199. */
  200. if (tp->snd_cwnd_cnt >= ca->cnt) {
  201. if (tp->snd_cwnd < tp->snd_cwnd_clamp)
  202. tp->snd_cwnd++;
  203. tp->snd_cwnd_cnt = 0;
  204. } else
  205. tp->snd_cwnd_cnt++;
  206. }
  207. }
  208. /*
  209. * behave like Reno until low_window is reached,
  210. * then increase congestion window slowly
  211. */
  212. static u32 bictcp_recalc_ssthresh(struct sock *sk)
  213. {
  214. const struct tcp_sock *tp = tcp_sk(sk);
  215. struct bictcp *ca = inet_csk_ca(sk);
  216. ca->epoch_start = 0; /* end of epoch */
  217. /* in case of wrong delay_max*/
  218. if (ca->delay_min > 0 && ca->delay_max > ca->delay_min)
  219. ca->delay_max = ca->delay_min
  220. + ((ca->delay_max - ca->delay_min)* 90) / 100;
  221. /* Wmax and fast convergence */
  222. if (tp->snd_cwnd < ca->last_max_cwnd && fast_convergence)
  223. ca->last_max_cwnd = (tp->snd_cwnd * (BICTCP_BETA_SCALE + beta))
  224. / (2 * BICTCP_BETA_SCALE);
  225. else
  226. ca->last_max_cwnd = tp->snd_cwnd;
  227. ca->loss_cwnd = tp->snd_cwnd;
  228. if (tp->snd_cwnd <= low_window)
  229. return max(tp->snd_cwnd >> 1U, 2U);
  230. else
  231. return max((tp->snd_cwnd * beta) / BICTCP_BETA_SCALE, 2U);
  232. }
  233. static u32 bictcp_undo_cwnd(struct sock *sk)
  234. {
  235. const struct tcp_sock *tp = tcp_sk(sk);
  236. const struct bictcp *ca = inet_csk_ca(sk);
  237. return max(tp->snd_cwnd, ca->last_max_cwnd);
  238. }
  239. static u32 bictcp_min_cwnd(struct sock *sk)
  240. {
  241. const struct tcp_sock *tp = tcp_sk(sk);
  242. return tp->snd_ssthresh;
  243. }
  244. static void bictcp_state(struct sock *sk, u8 new_state)
  245. {
  246. if (new_state == TCP_CA_Loss)
  247. bictcp_reset(inet_csk_ca(sk));
  248. }
  249. /* Track delayed acknowledgement ratio using sliding window
  250. * ratio = (15*ratio + sample) / 16
  251. */
  252. static void bictcp_acked(struct sock *sk, u32 cnt)
  253. {
  254. const struct inet_connection_sock *icsk = inet_csk(sk);
  255. if (cnt > 0 && icsk->icsk_ca_state == TCP_CA_Open) {
  256. struct bictcp *ca = inet_csk_ca(sk);
  257. cnt -= ca->delayed_ack >> ACK_RATIO_SHIFT;
  258. ca->delayed_ack += cnt;
  259. }
  260. }
  261. static struct tcp_congestion_ops bictcp = {
  262. .init = bictcp_init,
  263. .ssthresh = bictcp_recalc_ssthresh,
  264. .cong_avoid = bictcp_cong_avoid,
  265. .set_state = bictcp_state,
  266. .undo_cwnd = bictcp_undo_cwnd,
  267. .min_cwnd = bictcp_min_cwnd,
  268. .pkts_acked = bictcp_acked,
  269. .owner = THIS_MODULE,
  270. .name = "bic",
  271. };
  272. static int __init bictcp_register(void)
  273. {
  274. BUG_ON(sizeof(struct bictcp) > ICSK_CA_PRIV_SIZE);
  275. return tcp_register_congestion_control(&bictcp);
  276. }
  277. static void __exit bictcp_unregister(void)
  278. {
  279. tcp_unregister_congestion_control(&bictcp);
  280. }
  281. module_init(bictcp_register);
  282. module_exit(bictcp_unregister);
  283. MODULE_AUTHOR("Stephen Hemminger");
  284. MODULE_LICENSE("GPL");
  285. MODULE_DESCRIPTION("BIC TCP");