tcp_cubic.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /*
  2. * TCP CUBIC: Binary Increase Congestion control for TCP v2.0
  3. *
  4. * This is from the implementation of CUBIC TCP in
  5. * Injong Rhee, Lisong Xu.
  6. * "CUBIC: A New TCP-Friendly High-Speed TCP Variant
  7. * in PFLDnet 2005
  8. * Available from:
  9. * http://www.csc.ncsu.edu/faculty/rhee/export/bitcp/cubic-paper.pdf
  10. *
  11. * Unless CUBIC 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. #include <asm/div64.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. #define BICTCP_HZ 10 /* BIC HZ 2^10 = 1024 */
  26. static int fast_convergence __read_mostly = 1;
  27. static int max_increment __read_mostly = 16;
  28. static int beta __read_mostly = 819; /* = 819/1024 (BICTCP_BETA_SCALE) */
  29. static int initial_ssthresh __read_mostly = 100;
  30. static int bic_scale __read_mostly = 41;
  31. static int tcp_friendliness __read_mostly = 1;
  32. static u32 cube_rtt_scale __read_mostly;
  33. static u32 beta_scale __read_mostly;
  34. static u64 cube_factor __read_mostly;
  35. /* Note parameters that are used for precomputing scale factors are read-only */
  36. module_param(fast_convergence, int, 0644);
  37. MODULE_PARM_DESC(fast_convergence, "turn on/off fast convergence");
  38. module_param(max_increment, int, 0644);
  39. MODULE_PARM_DESC(max_increment, "Limit on increment allowed during binary search");
  40. module_param(beta, int, 0444);
  41. MODULE_PARM_DESC(beta, "beta for multiplicative increase");
  42. module_param(initial_ssthresh, int, 0644);
  43. MODULE_PARM_DESC(initial_ssthresh, "initial value of slow start threshold");
  44. module_param(bic_scale, int, 0444);
  45. MODULE_PARM_DESC(bic_scale, "scale (scaled by 1024) value for bic function (bic_scale/1024)");
  46. module_param(tcp_friendliness, int, 0644);
  47. MODULE_PARM_DESC(tcp_friendliness, "turn on/off tcp friendliness");
  48. /* BIC TCP Parameters */
  49. struct bictcp {
  50. u32 cnt; /* increase cwnd by 1 after ACKs */
  51. u32 last_max_cwnd; /* last maximum snd_cwnd */
  52. u32 loss_cwnd; /* congestion window at last loss */
  53. u32 last_cwnd; /* the last snd_cwnd */
  54. u32 last_time; /* time when updated last_cwnd */
  55. u32 bic_origin_point;/* origin point of bic function */
  56. u32 bic_K; /* time to origin point from the beginning of the current epoch */
  57. u32 delay_min; /* min delay */
  58. u32 epoch_start; /* beginning of an epoch */
  59. u32 ack_cnt; /* number of acks */
  60. u32 tcp_cwnd; /* estimated tcp cwnd */
  61. #define ACK_RATIO_SHIFT 4
  62. u32 delayed_ack; /* estimate the ratio of Packets/ACKs << 4 */
  63. };
  64. static inline void bictcp_reset(struct bictcp *ca)
  65. {
  66. ca->cnt = 0;
  67. ca->last_max_cwnd = 0;
  68. ca->loss_cwnd = 0;
  69. ca->last_cwnd = 0;
  70. ca->last_time = 0;
  71. ca->bic_origin_point = 0;
  72. ca->bic_K = 0;
  73. ca->delay_min = 0;
  74. ca->epoch_start = 0;
  75. ca->delayed_ack = 2 << ACK_RATIO_SHIFT;
  76. ca->ack_cnt = 0;
  77. ca->tcp_cwnd = 0;
  78. }
  79. static void bictcp_init(struct sock *sk)
  80. {
  81. bictcp_reset(inet_csk_ca(sk));
  82. if (initial_ssthresh)
  83. tcp_sk(sk)->snd_ssthresh = initial_ssthresh;
  84. }
  85. /*
  86. * calculate the cubic root of x using Newton-Raphson
  87. */
  88. static u32 cubic_root(u64 a)
  89. {
  90. u32 x;
  91. /* Initial estimate is based on:
  92. * cbrt(x) = exp(log(x) / 3)
  93. */
  94. x = 1u << (fls64(a)/3);
  95. /* converges to 32 bits in 3 iterations */
  96. x = (2 * x + (u32)div64_64(a, (u64)x*(u64)x)) / 3;
  97. x = (2 * x + (u32)div64_64(a, (u64)x*(u64)x)) / 3;
  98. x = (2 * x + (u32)div64_64(a, (u64)x*(u64)x)) / 3;
  99. return x;
  100. }
  101. /*
  102. * Compute congestion window to use.
  103. */
  104. static inline void bictcp_update(struct bictcp *ca, u32 cwnd)
  105. {
  106. u64 offs;
  107. u32 delta, t, bic_target, min_cnt, max_cnt;
  108. ca->ack_cnt++; /* count the number of ACKs */
  109. if (ca->last_cwnd == cwnd &&
  110. (s32)(tcp_time_stamp - ca->last_time) <= HZ / 32)
  111. return;
  112. ca->last_cwnd = cwnd;
  113. ca->last_time = tcp_time_stamp;
  114. if (ca->epoch_start == 0) {
  115. ca->epoch_start = tcp_time_stamp; /* record the beginning of an epoch */
  116. ca->ack_cnt = 1; /* start counting */
  117. ca->tcp_cwnd = cwnd; /* syn with cubic */
  118. if (ca->last_max_cwnd <= cwnd) {
  119. ca->bic_K = 0;
  120. ca->bic_origin_point = cwnd;
  121. } else {
  122. /* Compute new K based on
  123. * (wmax-cwnd) * (srtt>>3 / HZ) / c * 2^(3*bictcp_HZ)
  124. */
  125. ca->bic_K = cubic_root(cube_factor
  126. * (ca->last_max_cwnd - cwnd));
  127. ca->bic_origin_point = ca->last_max_cwnd;
  128. }
  129. }
  130. /* cubic function - calc*/
  131. /* calculate c * time^3 / rtt,
  132. * while considering overflow in calculation of time^3
  133. * (so time^3 is done by using 64 bit)
  134. * and without the support of division of 64bit numbers
  135. * (so all divisions are done by using 32 bit)
  136. * also NOTE the unit of those veriables
  137. * time = (t - K) / 2^bictcp_HZ
  138. * c = bic_scale >> 10
  139. * rtt = (srtt >> 3) / HZ
  140. * !!! The following code does not have overflow problems,
  141. * if the cwnd < 1 million packets !!!
  142. */
  143. /* change the unit from HZ to bictcp_HZ */
  144. t = ((tcp_time_stamp + (ca->delay_min>>3) - ca->epoch_start)
  145. << BICTCP_HZ) / HZ;
  146. if (t < ca->bic_K) /* t - K */
  147. offs = ca->bic_K - t;
  148. else
  149. offs = t - ca->bic_K;
  150. /* c/rtt * (t-K)^3 */
  151. delta = (cube_rtt_scale * offs * offs * offs) >> (10+3*BICTCP_HZ);
  152. if (t < ca->bic_K) /* below origin*/
  153. bic_target = ca->bic_origin_point - delta;
  154. else /* above origin*/
  155. bic_target = ca->bic_origin_point + delta;
  156. /* cubic function - calc bictcp_cnt*/
  157. if (bic_target > cwnd) {
  158. ca->cnt = cwnd / (bic_target - cwnd);
  159. } else {
  160. ca->cnt = 100 * cwnd; /* very small increment*/
  161. }
  162. if (ca->delay_min > 0) {
  163. /* max increment = Smax * rtt / 0.1 */
  164. min_cnt = (cwnd * HZ * 8)/(10 * max_increment * ca->delay_min);
  165. if (ca->cnt < min_cnt)
  166. ca->cnt = min_cnt;
  167. }
  168. /* slow start and low utilization */
  169. if (ca->loss_cwnd == 0) /* could be aggressive in slow start */
  170. ca->cnt = 50;
  171. /* TCP Friendly */
  172. if (tcp_friendliness) {
  173. u32 scale = beta_scale;
  174. delta = (cwnd * scale) >> 3;
  175. while (ca->ack_cnt > delta) { /* update tcp cwnd */
  176. ca->ack_cnt -= delta;
  177. ca->tcp_cwnd++;
  178. }
  179. if (ca->tcp_cwnd > cwnd){ /* if bic is slower than tcp */
  180. delta = ca->tcp_cwnd - cwnd;
  181. max_cnt = cwnd / delta;
  182. if (ca->cnt > max_cnt)
  183. ca->cnt = max_cnt;
  184. }
  185. }
  186. ca->cnt = (ca->cnt << ACK_RATIO_SHIFT) / ca->delayed_ack;
  187. if (ca->cnt == 0) /* cannot be zero */
  188. ca->cnt = 1;
  189. }
  190. /* Keep track of minimum rtt */
  191. static inline void measure_delay(struct sock *sk)
  192. {
  193. const struct tcp_sock *tp = tcp_sk(sk);
  194. struct bictcp *ca = inet_csk_ca(sk);
  195. u32 delay;
  196. /* No time stamp */
  197. if (!(tp->rx_opt.saw_tstamp && tp->rx_opt.rcv_tsecr) ||
  198. /* Discard delay samples right after fast recovery */
  199. (s32)(tcp_time_stamp - ca->epoch_start) < HZ)
  200. return;
  201. delay = (tcp_time_stamp - tp->rx_opt.rcv_tsecr)<<3;
  202. if (delay == 0)
  203. delay = 1;
  204. /* first time call or link delay decreases */
  205. if (ca->delay_min == 0 || ca->delay_min > delay)
  206. ca->delay_min = delay;
  207. }
  208. static void bictcp_cong_avoid(struct sock *sk, u32 ack,
  209. u32 seq_rtt, u32 in_flight, int data_acked)
  210. {
  211. struct tcp_sock *tp = tcp_sk(sk);
  212. struct bictcp *ca = inet_csk_ca(sk);
  213. if (data_acked)
  214. measure_delay(sk);
  215. if (!tcp_is_cwnd_limited(sk, in_flight))
  216. return;
  217. if (tp->snd_cwnd <= tp->snd_ssthresh)
  218. tcp_slow_start(tp);
  219. else {
  220. bictcp_update(ca, tp->snd_cwnd);
  221. /* In dangerous area, increase slowly.
  222. * In theory this is tp->snd_cwnd += 1 / tp->snd_cwnd
  223. */
  224. if (tp->snd_cwnd_cnt >= ca->cnt) {
  225. if (tp->snd_cwnd < tp->snd_cwnd_clamp)
  226. tp->snd_cwnd++;
  227. tp->snd_cwnd_cnt = 0;
  228. } else
  229. tp->snd_cwnd_cnt++;
  230. }
  231. }
  232. static u32 bictcp_recalc_ssthresh(struct sock *sk)
  233. {
  234. const struct tcp_sock *tp = tcp_sk(sk);
  235. struct bictcp *ca = inet_csk_ca(sk);
  236. ca->epoch_start = 0; /* end of epoch */
  237. /* Wmax and fast convergence */
  238. if (tp->snd_cwnd < ca->last_max_cwnd && fast_convergence)
  239. ca->last_max_cwnd = (tp->snd_cwnd * (BICTCP_BETA_SCALE + beta))
  240. / (2 * BICTCP_BETA_SCALE);
  241. else
  242. ca->last_max_cwnd = tp->snd_cwnd;
  243. ca->loss_cwnd = tp->snd_cwnd;
  244. return max((tp->snd_cwnd * beta) / BICTCP_BETA_SCALE, 2U);
  245. }
  246. static u32 bictcp_undo_cwnd(struct sock *sk)
  247. {
  248. struct bictcp *ca = inet_csk_ca(sk);
  249. return max(tcp_sk(sk)->snd_cwnd, ca->last_max_cwnd);
  250. }
  251. static void bictcp_state(struct sock *sk, u8 new_state)
  252. {
  253. if (new_state == TCP_CA_Loss)
  254. bictcp_reset(inet_csk_ca(sk));
  255. }
  256. /* Track delayed acknowledgment ratio using sliding window
  257. * ratio = (15*ratio + sample) / 16
  258. */
  259. static void bictcp_acked(struct sock *sk, u32 cnt)
  260. {
  261. const struct inet_connection_sock *icsk = inet_csk(sk);
  262. if (cnt > 0 && icsk->icsk_ca_state == TCP_CA_Open) {
  263. struct bictcp *ca = inet_csk_ca(sk);
  264. cnt -= ca->delayed_ack >> ACK_RATIO_SHIFT;
  265. ca->delayed_ack += cnt;
  266. }
  267. }
  268. static struct tcp_congestion_ops cubictcp = {
  269. .init = bictcp_init,
  270. .ssthresh = bictcp_recalc_ssthresh,
  271. .cong_avoid = bictcp_cong_avoid,
  272. .set_state = bictcp_state,
  273. .undo_cwnd = bictcp_undo_cwnd,
  274. .pkts_acked = bictcp_acked,
  275. .owner = THIS_MODULE,
  276. .name = "cubic",
  277. };
  278. static int __init cubictcp_register(void)
  279. {
  280. BUILD_BUG_ON(sizeof(struct bictcp) > ICSK_CA_PRIV_SIZE);
  281. /* Precompute a bunch of the scaling factors that are used per-packet
  282. * based on SRTT of 100ms
  283. */
  284. beta_scale = 8*(BICTCP_BETA_SCALE+beta)/ 3 / (BICTCP_BETA_SCALE - beta);
  285. cube_rtt_scale = (bic_scale * 10); /* 1024*c/rtt */
  286. /* calculate the "K" for (wmax-cwnd) = c/rtt * K^3
  287. * so K = cubic_root( (wmax-cwnd)*rtt/c )
  288. * the unit of K is bictcp_HZ=2^10, not HZ
  289. *
  290. * c = bic_scale >> 10
  291. * rtt = 100ms
  292. *
  293. * the following code has been designed and tested for
  294. * cwnd < 1 million packets
  295. * RTT < 100 seconds
  296. * HZ < 1,000,00 (corresponding to 10 nano-second)
  297. */
  298. /* 1/c * 2^2*bictcp_HZ * srtt */
  299. cube_factor = 1ull << (10+3*BICTCP_HZ); /* 2^40 */
  300. /* divide by bic_scale and by constant Srtt (100ms) */
  301. do_div(cube_factor, bic_scale * 10);
  302. return tcp_register_congestion_control(&cubictcp);
  303. }
  304. static void __exit cubictcp_unregister(void)
  305. {
  306. tcp_unregister_congestion_control(&cubictcp);
  307. }
  308. module_init(cubictcp_register);
  309. module_exit(cubictcp_unregister);
  310. MODULE_AUTHOR("Sangtae Ha, Stephen Hemminger");
  311. MODULE_LICENSE("GPL");
  312. MODULE_DESCRIPTION("CUBIC TCP");
  313. MODULE_VERSION("2.0");