tcp_cubic.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * TCP CUBIC: Binary Increase Congestion control for TCP v2.3
  4. * Home page:
  5. * http://netsrv.csc.ncsu.edu/twiki/bin/view/Main/BIC
  6. * This is from the implementation of CUBIC TCP in
  7. * Sangtae Ha, Injong Rhee and Lisong Xu,
  8. * "CUBIC: A New TCP-Friendly High-Speed TCP Variant"
  9. * in ACM SIGOPS Operating System Review, July 2008.
  10. * Available from:
  11. * http://netsrv.csc.ncsu.edu/export/cubic_a_new_tcp_2008.pdf
  12. *
  13. * CUBIC integrates a new slow start algorithm, called HyStart.
  14. * The details of HyStart are presented in
  15. * Sangtae Ha and Injong Rhee,
  16. * "Taming the Elephants: New TCP Slow Start", NCSU TechReport 2008.
  17. * Available from:
  18. * http://netsrv.csc.ncsu.edu/export/hystart_techreport_2008.pdf
  19. *
  20. * All testing results are available from:
  21. * http://netsrv.csc.ncsu.edu/wiki/index.php/TCP_Testing
  22. *
  23. * Unless CUBIC is enabled and congestion window is large
  24. * this behaves the same as the original Reno.
  25. */
  26. #include <linux/mm.h>
  27. #include <linux/module.h>
  28. #include <linux/math64.h>
  29. #include <net/tcp.h>
  30. #define BICTCP_BETA_SCALE 1024 /* Scale factor beta calculation
  31. * max_cwnd = snd_cwnd * beta
  32. */
  33. #define BICTCP_HZ 10 /* BIC HZ 2^10 = 1024 */
  34. /* Two methods of hybrid slow start */
  35. #define HYSTART_ACK_TRAIN 0x1
  36. #define HYSTART_DELAY 0x2
  37. /* Number of delay samples for detecting the increase of delay */
  38. #define HYSTART_MIN_SAMPLES 8
  39. #define HYSTART_DELAY_MIN (4000U) /* 4 ms */
  40. #define HYSTART_DELAY_MAX (16000U) /* 16 ms */
  41. #define HYSTART_DELAY_THRESH(x) clamp(x, HYSTART_DELAY_MIN, HYSTART_DELAY_MAX)
  42. static int fast_convergence __read_mostly = 1;
  43. static int beta __read_mostly = 717; /* = 717/1024 (BICTCP_BETA_SCALE) */
  44. static int initial_ssthresh __read_mostly;
  45. static int bic_scale __read_mostly = 41;
  46. static int tcp_friendliness __read_mostly = 1;
  47. static int hystart __read_mostly = 1;
  48. static int hystart_detect __read_mostly = HYSTART_ACK_TRAIN | HYSTART_DELAY;
  49. static int hystart_low_window __read_mostly = 16;
  50. static int hystart_ack_delta_us __read_mostly = 2000;
  51. static u32 cube_rtt_scale __read_mostly;
  52. static u32 beta_scale __read_mostly;
  53. static u64 cube_factor __read_mostly;
  54. /* Note parameters that are used for precomputing scale factors are read-only */
  55. module_param(fast_convergence, int, 0644);
  56. MODULE_PARM_DESC(fast_convergence, "turn on/off fast convergence");
  57. module_param(beta, int, 0644);
  58. MODULE_PARM_DESC(beta, "beta for multiplicative increase");
  59. module_param(initial_ssthresh, int, 0644);
  60. MODULE_PARM_DESC(initial_ssthresh, "initial value of slow start threshold");
  61. module_param(bic_scale, int, 0444);
  62. MODULE_PARM_DESC(bic_scale, "scale (scaled by 1024) value for bic function (bic_scale/1024)");
  63. module_param(tcp_friendliness, int, 0644);
  64. MODULE_PARM_DESC(tcp_friendliness, "turn on/off tcp friendliness");
  65. module_param(hystart, int, 0644);
  66. MODULE_PARM_DESC(hystart, "turn on/off hybrid slow start algorithm");
  67. module_param(hystart_detect, int, 0644);
  68. MODULE_PARM_DESC(hystart_detect, "hybrid slow start detection mechanisms"
  69. " 1: packet-train 2: delay 3: both packet-train and delay");
  70. module_param(hystart_low_window, int, 0644);
  71. MODULE_PARM_DESC(hystart_low_window, "lower bound cwnd for hybrid slow start");
  72. module_param(hystart_ack_delta_us, int, 0644);
  73. MODULE_PARM_DESC(hystart_ack_delta_us, "spacing between ack's indicating train (usecs)");
  74. /* BIC TCP Parameters */
  75. struct bictcp {
  76. u32 cnt; /* increase cwnd by 1 after ACKs */
  77. u32 last_max_cwnd; /* last maximum snd_cwnd */
  78. u32 last_cwnd; /* the last snd_cwnd */
  79. u32 last_time; /* time when updated last_cwnd */
  80. u32 bic_origin_point;/* origin point of bic function */
  81. u32 bic_K; /* time to origin point
  82. from the beginning of the current epoch */
  83. u32 delay_min; /* min delay (usec) */
  84. u32 epoch_start; /* beginning of an epoch */
  85. u32 ack_cnt; /* number of acks */
  86. u32 tcp_cwnd; /* estimated tcp cwnd */
  87. u16 unused;
  88. u8 sample_cnt; /* number of samples to decide curr_rtt */
  89. u8 found; /* the exit point is found? */
  90. u32 round_start; /* beginning of each round */
  91. u32 end_seq; /* end_seq of the round */
  92. u32 last_ack; /* last time when the ACK spacing is close */
  93. u32 curr_rtt; /* the minimum rtt of current round */
  94. };
  95. static inline void bictcp_reset(struct bictcp *ca)
  96. {
  97. ca->cnt = 0;
  98. ca->last_max_cwnd = 0;
  99. ca->last_cwnd = 0;
  100. ca->last_time = 0;
  101. ca->bic_origin_point = 0;
  102. ca->bic_K = 0;
  103. ca->delay_min = 0;
  104. ca->epoch_start = 0;
  105. ca->ack_cnt = 0;
  106. ca->tcp_cwnd = 0;
  107. ca->found = 0;
  108. }
  109. static inline u32 bictcp_clock_us(const struct sock *sk)
  110. {
  111. return tcp_sk(sk)->tcp_mstamp;
  112. }
  113. static inline void bictcp_hystart_reset(struct sock *sk)
  114. {
  115. struct tcp_sock *tp = tcp_sk(sk);
  116. struct bictcp *ca = inet_csk_ca(sk);
  117. ca->round_start = ca->last_ack = bictcp_clock_us(sk);
  118. ca->end_seq = tp->snd_nxt;
  119. ca->curr_rtt = ~0U;
  120. ca->sample_cnt = 0;
  121. }
  122. static void bictcp_init(struct sock *sk)
  123. {
  124. struct bictcp *ca = inet_csk_ca(sk);
  125. bictcp_reset(ca);
  126. if (hystart)
  127. bictcp_hystart_reset(sk);
  128. if (!hystart && initial_ssthresh)
  129. tcp_sk(sk)->snd_ssthresh = initial_ssthresh;
  130. }
  131. static void bictcp_cwnd_event(struct sock *sk, enum tcp_ca_event event)
  132. {
  133. if (event == CA_EVENT_TX_START) {
  134. struct bictcp *ca = inet_csk_ca(sk);
  135. u32 now = tcp_jiffies32;
  136. s32 delta;
  137. delta = now - tcp_sk(sk)->lsndtime;
  138. /* We were application limited (idle) for a while.
  139. * Shift epoch_start to keep cwnd growth to cubic curve.
  140. */
  141. if (ca->epoch_start && delta > 0) {
  142. ca->epoch_start += delta;
  143. if (after(ca->epoch_start, now))
  144. ca->epoch_start = now;
  145. }
  146. return;
  147. }
  148. }
  149. /* calculate the cubic root of x using a table lookup followed by one
  150. * Newton-Raphson iteration.
  151. * Avg err ~= 0.195%
  152. */
  153. static u32 cubic_root(u64 a)
  154. {
  155. u32 x, b, shift;
  156. /*
  157. * cbrt(x) MSB values for x MSB values in [0..63].
  158. * Precomputed then refined by hand - Willy Tarreau
  159. *
  160. * For x in [0..63],
  161. * v = cbrt(x << 18) - 1
  162. * cbrt(x) = (v[x] + 10) >> 6
  163. */
  164. static const u8 v[] = {
  165. /* 0x00 */ 0, 54, 54, 54, 118, 118, 118, 118,
  166. /* 0x08 */ 123, 129, 134, 138, 143, 147, 151, 156,
  167. /* 0x10 */ 157, 161, 164, 168, 170, 173, 176, 179,
  168. /* 0x18 */ 181, 185, 187, 190, 192, 194, 197, 199,
  169. /* 0x20 */ 200, 202, 204, 206, 209, 211, 213, 215,
  170. /* 0x28 */ 217, 219, 221, 222, 224, 225, 227, 229,
  171. /* 0x30 */ 231, 232, 234, 236, 237, 239, 240, 242,
  172. /* 0x38 */ 244, 245, 246, 248, 250, 251, 252, 254,
  173. };
  174. b = fls64(a);
  175. if (b < 7) {
  176. /* a in [0..63] */
  177. return ((u32)v[(u32)a] + 35) >> 6;
  178. }
  179. b = ((b * 84) >> 8) - 1;
  180. shift = (a >> (b * 3));
  181. x = ((u32)(((u32)v[shift] + 10) << b)) >> 6;
  182. /*
  183. * Newton-Raphson iteration
  184. * 2
  185. * x = ( 2 * x + a / x ) / 3
  186. * k+1 k k
  187. */
  188. x = (2 * x + (u32)div64_u64(a, (u64)x * (u64)(x - 1)));
  189. x = ((x * 341) >> 10);
  190. return x;
  191. }
  192. /*
  193. * Compute congestion window to use.
  194. */
  195. static inline void bictcp_update(struct bictcp *ca, u32 cwnd, u32 acked)
  196. {
  197. u32 delta, bic_target, max_cnt;
  198. u64 offs, t;
  199. ca->ack_cnt += acked; /* count the number of ACKed packets */
  200. if (ca->last_cwnd == cwnd &&
  201. (s32)(tcp_jiffies32 - ca->last_time) <= HZ / 32)
  202. return;
  203. /* The CUBIC function can update ca->cnt at most once per jiffy.
  204. * On all cwnd reduction events, ca->epoch_start is set to 0,
  205. * which will force a recalculation of ca->cnt.
  206. */
  207. if (ca->epoch_start && tcp_jiffies32 == ca->last_time)
  208. goto tcp_friendliness;
  209. ca->last_cwnd = cwnd;
  210. ca->last_time = tcp_jiffies32;
  211. if (ca->epoch_start == 0) {
  212. ca->epoch_start = tcp_jiffies32; /* record beginning */
  213. ca->ack_cnt = acked; /* start counting */
  214. ca->tcp_cwnd = cwnd; /* syn with cubic */
  215. if (ca->last_max_cwnd <= cwnd) {
  216. ca->bic_K = 0;
  217. ca->bic_origin_point = cwnd;
  218. } else {
  219. /* Compute new K based on
  220. * (wmax-cwnd) * (srtt>>3 / HZ) / c * 2^(3*bictcp_HZ)
  221. */
  222. ca->bic_K = cubic_root(cube_factor
  223. * (ca->last_max_cwnd - cwnd));
  224. ca->bic_origin_point = ca->last_max_cwnd;
  225. }
  226. }
  227. /* cubic function - calc*/
  228. /* calculate c * time^3 / rtt,
  229. * while considering overflow in calculation of time^3
  230. * (so time^3 is done by using 64 bit)
  231. * and without the support of division of 64bit numbers
  232. * (so all divisions are done by using 32 bit)
  233. * also NOTE the unit of those veriables
  234. * time = (t - K) / 2^bictcp_HZ
  235. * c = bic_scale >> 10
  236. * rtt = (srtt >> 3) / HZ
  237. * !!! The following code does not have overflow problems,
  238. * if the cwnd < 1 million packets !!!
  239. */
  240. t = (s32)(tcp_jiffies32 - ca->epoch_start);
  241. t += usecs_to_jiffies(ca->delay_min);
  242. /* change the unit from HZ to bictcp_HZ */
  243. t <<= BICTCP_HZ;
  244. do_div(t, HZ);
  245. if (t < ca->bic_K) /* t - K */
  246. offs = ca->bic_K - t;
  247. else
  248. offs = t - ca->bic_K;
  249. /* c/rtt * (t-K)^3 */
  250. delta = (cube_rtt_scale * offs * offs * offs) >> (10+3*BICTCP_HZ);
  251. if (t < ca->bic_K) /* below origin*/
  252. bic_target = ca->bic_origin_point - delta;
  253. else /* above origin*/
  254. bic_target = ca->bic_origin_point + delta;
  255. /* cubic function - calc bictcp_cnt*/
  256. if (bic_target > cwnd) {
  257. ca->cnt = cwnd / (bic_target - cwnd);
  258. } else {
  259. ca->cnt = 100 * cwnd; /* very small increment*/
  260. }
  261. /*
  262. * The initial growth of cubic function may be too conservative
  263. * when the available bandwidth is still unknown.
  264. */
  265. if (ca->last_max_cwnd == 0 && ca->cnt > 20)
  266. ca->cnt = 20; /* increase cwnd 5% per RTT */
  267. tcp_friendliness:
  268. /* TCP Friendly */
  269. if (tcp_friendliness) {
  270. u32 scale = beta_scale;
  271. delta = (cwnd * scale) >> 3;
  272. while (ca->ack_cnt > delta) { /* update tcp cwnd */
  273. ca->ack_cnt -= delta;
  274. ca->tcp_cwnd++;
  275. }
  276. if (ca->tcp_cwnd > cwnd) { /* if bic is slower than tcp */
  277. delta = ca->tcp_cwnd - cwnd;
  278. max_cnt = cwnd / delta;
  279. if (ca->cnt > max_cnt)
  280. ca->cnt = max_cnt;
  281. }
  282. }
  283. /* The maximum rate of cwnd increase CUBIC allows is 1 packet per
  284. * 2 packets ACKed, meaning cwnd grows at 1.5x per RTT.
  285. */
  286. ca->cnt = max(ca->cnt, 2U);
  287. }
  288. static void bictcp_cong_avoid(struct sock *sk, u32 ack, u32 acked)
  289. {
  290. struct tcp_sock *tp = tcp_sk(sk);
  291. struct bictcp *ca = inet_csk_ca(sk);
  292. if (!tcp_is_cwnd_limited(sk))
  293. return;
  294. if (tcp_in_slow_start(tp)) {
  295. acked = tcp_slow_start(tp, acked);
  296. if (!acked)
  297. return;
  298. }
  299. bictcp_update(ca, tp->snd_cwnd, acked);
  300. tcp_cong_avoid_ai(tp, ca->cnt, acked);
  301. }
  302. static u32 bictcp_recalc_ssthresh(struct sock *sk)
  303. {
  304. const struct tcp_sock *tp = tcp_sk(sk);
  305. struct bictcp *ca = inet_csk_ca(sk);
  306. ca->epoch_start = 0; /* end of epoch */
  307. /* Wmax and fast convergence */
  308. if (tp->snd_cwnd < ca->last_max_cwnd && fast_convergence)
  309. ca->last_max_cwnd = (tp->snd_cwnd * (BICTCP_BETA_SCALE + beta))
  310. / (2 * BICTCP_BETA_SCALE);
  311. else
  312. ca->last_max_cwnd = tp->snd_cwnd;
  313. return max((tp->snd_cwnd * beta) / BICTCP_BETA_SCALE, 2U);
  314. }
  315. static void bictcp_state(struct sock *sk, u8 new_state)
  316. {
  317. if (new_state == TCP_CA_Loss) {
  318. bictcp_reset(inet_csk_ca(sk));
  319. bictcp_hystart_reset(sk);
  320. }
  321. }
  322. /* Account for TSO/GRO delays.
  323. * Otherwise short RTT flows could get too small ssthresh, since during
  324. * slow start we begin with small TSO packets and ca->delay_min would
  325. * not account for long aggregation delay when TSO packets get bigger.
  326. * Ideally even with a very small RTT we would like to have at least one
  327. * TSO packet being sent and received by GRO, and another one in qdisc layer.
  328. * We apply another 100% factor because @rate is doubled at this point.
  329. * We cap the cushion to 1ms.
  330. */
  331. static u32 hystart_ack_delay(struct sock *sk)
  332. {
  333. unsigned long rate;
  334. rate = READ_ONCE(sk->sk_pacing_rate);
  335. if (!rate)
  336. return 0;
  337. return min_t(u64, USEC_PER_MSEC,
  338. div64_ul((u64)GSO_MAX_SIZE * 4 * USEC_PER_SEC, rate));
  339. }
  340. static void hystart_update(struct sock *sk, u32 delay)
  341. {
  342. struct tcp_sock *tp = tcp_sk(sk);
  343. struct bictcp *ca = inet_csk_ca(sk);
  344. u32 threshold;
  345. if (after(tp->snd_una, ca->end_seq))
  346. bictcp_hystart_reset(sk);
  347. if (hystart_detect & HYSTART_ACK_TRAIN) {
  348. u32 now = bictcp_clock_us(sk);
  349. /* first detection parameter - ack-train detection */
  350. if ((s32)(now - ca->last_ack) <= hystart_ack_delta_us) {
  351. ca->last_ack = now;
  352. threshold = ca->delay_min + hystart_ack_delay(sk);
  353. /* Hystart ack train triggers if we get ack past
  354. * ca->delay_min/2.
  355. * Pacing might have delayed packets up to RTT/2
  356. * during slow start.
  357. */
  358. if (sk->sk_pacing_status == SK_PACING_NONE)
  359. threshold >>= 1;
  360. if ((s32)(now - ca->round_start) > threshold) {
  361. ca->found = 1;
  362. pr_debug("hystart_ack_train (%u > %u) delay_min %u (+ ack_delay %u) cwnd %u\n",
  363. now - ca->round_start, threshold,
  364. ca->delay_min, hystart_ack_delay(sk), tp->snd_cwnd);
  365. NET_INC_STATS(sock_net(sk),
  366. LINUX_MIB_TCPHYSTARTTRAINDETECT);
  367. NET_ADD_STATS(sock_net(sk),
  368. LINUX_MIB_TCPHYSTARTTRAINCWND,
  369. tp->snd_cwnd);
  370. tp->snd_ssthresh = tp->snd_cwnd;
  371. }
  372. }
  373. }
  374. if (hystart_detect & HYSTART_DELAY) {
  375. /* obtain the minimum delay of more than sampling packets */
  376. if (ca->curr_rtt > delay)
  377. ca->curr_rtt = delay;
  378. if (ca->sample_cnt < HYSTART_MIN_SAMPLES) {
  379. ca->sample_cnt++;
  380. } else {
  381. if (ca->curr_rtt > ca->delay_min +
  382. HYSTART_DELAY_THRESH(ca->delay_min >> 3)) {
  383. ca->found = 1;
  384. NET_INC_STATS(sock_net(sk),
  385. LINUX_MIB_TCPHYSTARTDELAYDETECT);
  386. NET_ADD_STATS(sock_net(sk),
  387. LINUX_MIB_TCPHYSTARTDELAYCWND,
  388. tp->snd_cwnd);
  389. tp->snd_ssthresh = tp->snd_cwnd;
  390. }
  391. }
  392. }
  393. }
  394. static void bictcp_acked(struct sock *sk, const struct ack_sample *sample)
  395. {
  396. const struct tcp_sock *tp = tcp_sk(sk);
  397. struct bictcp *ca = inet_csk_ca(sk);
  398. u32 delay;
  399. /* Some calls are for duplicates without timetamps */
  400. if (sample->rtt_us < 0)
  401. return;
  402. /* Discard delay samples right after fast recovery */
  403. if (ca->epoch_start && (s32)(tcp_jiffies32 - ca->epoch_start) < HZ)
  404. return;
  405. delay = sample->rtt_us;
  406. if (delay == 0)
  407. delay = 1;
  408. /* first time call or link delay decreases */
  409. if (ca->delay_min == 0 || ca->delay_min > delay)
  410. ca->delay_min = delay;
  411. /* hystart triggers when cwnd is larger than some threshold */
  412. if (!ca->found && tcp_in_slow_start(tp) && hystart &&
  413. tp->snd_cwnd >= hystart_low_window)
  414. hystart_update(sk, delay);
  415. }
  416. static struct tcp_congestion_ops cubictcp __read_mostly = {
  417. .init = bictcp_init,
  418. .ssthresh = bictcp_recalc_ssthresh,
  419. .cong_avoid = bictcp_cong_avoid,
  420. .set_state = bictcp_state,
  421. .undo_cwnd = tcp_reno_undo_cwnd,
  422. .cwnd_event = bictcp_cwnd_event,
  423. .pkts_acked = bictcp_acked,
  424. .owner = THIS_MODULE,
  425. .name = "cubic",
  426. };
  427. static int __init cubictcp_register(void)
  428. {
  429. BUILD_BUG_ON(sizeof(struct bictcp) > ICSK_CA_PRIV_SIZE);
  430. /* Precompute a bunch of the scaling factors that are used per-packet
  431. * based on SRTT of 100ms
  432. */
  433. beta_scale = 8*(BICTCP_BETA_SCALE+beta) / 3
  434. / (BICTCP_BETA_SCALE - beta);
  435. cube_rtt_scale = (bic_scale * 10); /* 1024*c/rtt */
  436. /* calculate the "K" for (wmax-cwnd) = c/rtt * K^3
  437. * so K = cubic_root( (wmax-cwnd)*rtt/c )
  438. * the unit of K is bictcp_HZ=2^10, not HZ
  439. *
  440. * c = bic_scale >> 10
  441. * rtt = 100ms
  442. *
  443. * the following code has been designed and tested for
  444. * cwnd < 1 million packets
  445. * RTT < 100 seconds
  446. * HZ < 1,000,00 (corresponding to 10 nano-second)
  447. */
  448. /* 1/c * 2^2*bictcp_HZ * srtt */
  449. cube_factor = 1ull << (10+3*BICTCP_HZ); /* 2^40 */
  450. /* divide by bic_scale and by constant Srtt (100ms) */
  451. do_div(cube_factor, bic_scale * 10);
  452. return tcp_register_congestion_control(&cubictcp);
  453. }
  454. static void __exit cubictcp_unregister(void)
  455. {
  456. tcp_unregister_congestion_control(&cubictcp);
  457. }
  458. module_init(cubictcp_register);
  459. module_exit(cubictcp_unregister);
  460. MODULE_AUTHOR("Sangtae Ha, Stephen Hemminger");
  461. MODULE_LICENSE("GPL");
  462. MODULE_DESCRIPTION("CUBIC TCP");
  463. MODULE_VERSION("2.3");