tcp_cubic.c 10 KB

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