tcp_bic.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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 tcp_sock *tp)
  81. {
  82. bictcp_reset(tcp_ca(tp));
  83. if (initial_ssthresh)
  84. tp->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 tcp_sock *tp, int flag)
  141. {
  142. struct bictcp *ca = tcp_ca(tp);
  143. u32 dist, delay;
  144. /* No time stamp */
  145. if (!(tp->rx_opt.saw_tstamp && tp->rx_opt.rcv_tsecr) ||
  146. /* Discard delay samples right after fast recovery */
  147. tcp_time_stamp < ca->epoch_start + HZ ||
  148. /* this delay samples may not be accurate */
  149. flag == 0) {
  150. ca->last_delay = 0;
  151. goto notlow;
  152. }
  153. delay = ca->last_delay<<3; /* use the same scale as tp->srtt*/
  154. ca->last_delay = tcp_time_stamp - tp->rx_opt.rcv_tsecr;
  155. if (delay == 0) /* no previous delay sample */
  156. goto notlow;
  157. /* first time call or link delay decreases */
  158. if (ca->delay_min == 0 || ca->delay_min > delay) {
  159. ca->delay_min = ca->delay_max = delay;
  160. goto notlow;
  161. }
  162. if (ca->delay_max < delay)
  163. ca->delay_max = delay;
  164. /* utilization is low, if avg delay < dist*threshold
  165. for checking_period time */
  166. dist = ca->delay_max - ca->delay_min;
  167. if (dist <= ca->delay_min>>6 ||
  168. tp->srtt - ca->delay_min >= (dist*low_utilization_threshold)>>10)
  169. goto notlow;
  170. if (ca->low_utilization_start == 0) {
  171. ca->low_utilization = 0;
  172. ca->low_utilization_start = tcp_time_stamp;
  173. } else if ((s32)(tcp_time_stamp - ca->low_utilization_start)
  174. > low_utilization_period*HZ) {
  175. ca->low_utilization = 1;
  176. }
  177. return;
  178. notlow:
  179. ca->low_utilization = 0;
  180. ca->low_utilization_start = 0;
  181. }
  182. static void bictcp_cong_avoid(struct tcp_sock *tp, u32 ack,
  183. u32 seq_rtt, u32 in_flight, int data_acked)
  184. {
  185. struct bictcp *ca = tcp_ca(tp);
  186. bictcp_low_utilization(tp, data_acked);
  187. if (in_flight < tp->snd_cwnd)
  188. return;
  189. if (tp->snd_cwnd <= tp->snd_ssthresh) {
  190. /* In "safe" area, increase. */
  191. if (tp->snd_cwnd < tp->snd_cwnd_clamp)
  192. tp->snd_cwnd++;
  193. } else {
  194. bictcp_update(ca, tp->snd_cwnd);
  195. /* In dangerous area, increase slowly.
  196. * In theory this is tp->snd_cwnd += 1 / tp->snd_cwnd
  197. */
  198. if (tp->snd_cwnd_cnt >= ca->cnt) {
  199. if (tp->snd_cwnd < tp->snd_cwnd_clamp)
  200. tp->snd_cwnd++;
  201. tp->snd_cwnd_cnt = 0;
  202. } else
  203. tp->snd_cwnd_cnt++;
  204. }
  205. }
  206. /*
  207. * behave like Reno until low_window is reached,
  208. * then increase congestion window slowly
  209. */
  210. static u32 bictcp_recalc_ssthresh(struct tcp_sock *tp)
  211. {
  212. struct bictcp *ca = tcp_ca(tp);
  213. ca->epoch_start = 0; /* end of epoch */
  214. /* in case of wrong delay_max*/
  215. if (ca->delay_min > 0 && ca->delay_max > ca->delay_min)
  216. ca->delay_max = ca->delay_min
  217. + ((ca->delay_max - ca->delay_min)* 90) / 100;
  218. /* Wmax and fast convergence */
  219. if (tp->snd_cwnd < ca->last_max_cwnd && fast_convergence)
  220. ca->last_max_cwnd = (tp->snd_cwnd * (BICTCP_BETA_SCALE + beta))
  221. / (2 * BICTCP_BETA_SCALE);
  222. else
  223. ca->last_max_cwnd = tp->snd_cwnd;
  224. ca->loss_cwnd = tp->snd_cwnd;
  225. if (tp->snd_cwnd <= low_window)
  226. return max(tp->snd_cwnd >> 1U, 2U);
  227. else
  228. return max((tp->snd_cwnd * beta) / BICTCP_BETA_SCALE, 2U);
  229. }
  230. static u32 bictcp_undo_cwnd(struct tcp_sock *tp)
  231. {
  232. struct bictcp *ca = tcp_ca(tp);
  233. return max(tp->snd_cwnd, ca->last_max_cwnd);
  234. }
  235. static u32 bictcp_min_cwnd(struct tcp_sock *tp)
  236. {
  237. return tp->snd_ssthresh;
  238. }
  239. static void bictcp_state(struct tcp_sock *tp, u8 new_state)
  240. {
  241. if (new_state == TCP_CA_Loss)
  242. bictcp_reset(tcp_ca(tp));
  243. }
  244. /* Track delayed acknowledgement ratio using sliding window
  245. * ratio = (15*ratio + sample) / 16
  246. */
  247. static void bictcp_acked(struct tcp_sock *tp, u32 cnt)
  248. {
  249. if (cnt > 0 && tp->ca_state == TCP_CA_Open) {
  250. struct bictcp *ca = tcp_ca(tp);
  251. cnt -= ca->delayed_ack >> ACK_RATIO_SHIFT;
  252. ca->delayed_ack += cnt;
  253. }
  254. }
  255. static struct tcp_congestion_ops bictcp = {
  256. .init = bictcp_init,
  257. .ssthresh = bictcp_recalc_ssthresh,
  258. .cong_avoid = bictcp_cong_avoid,
  259. .set_state = bictcp_state,
  260. .undo_cwnd = bictcp_undo_cwnd,
  261. .min_cwnd = bictcp_min_cwnd,
  262. .pkts_acked = bictcp_acked,
  263. .owner = THIS_MODULE,
  264. .name = "bic",
  265. };
  266. static int __init bictcp_register(void)
  267. {
  268. BUG_ON(sizeof(struct bictcp) > TCP_CA_PRIV_SIZE);
  269. return tcp_register_congestion_control(&bictcp);
  270. }
  271. static void __exit bictcp_unregister(void)
  272. {
  273. tcp_unregister_congestion_control(&bictcp);
  274. }
  275. module_init(bictcp_register);
  276. module_exit(bictcp_unregister);
  277. MODULE_AUTHOR("Stephen Hemminger");
  278. MODULE_LICENSE("GPL");
  279. MODULE_DESCRIPTION("BIC TCP");