tcp_cubic.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /*
  2. * TCP CUBIC: Binary Increase Congestion control for TCP v2.1
  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;
  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. /* calculate the cubic root of x using a table lookup followed by one
  86. * Newton-Raphson iteration.
  87. * Avg err ~= 0.195%
  88. */
  89. static u32 cubic_root(u64 a)
  90. {
  91. u32 x, b, shift;
  92. /*
  93. * cbrt(x) MSB values for x MSB values in [0..63].
  94. * Precomputed then refined by hand - Willy Tarreau
  95. *
  96. * For x in [0..63],
  97. * v = cbrt(x << 18) - 1
  98. * cbrt(x) = (v[x] + 10) >> 6
  99. */
  100. static const u8 v[] = {
  101. /* 0x00 */ 0, 54, 54, 54, 118, 118, 118, 118,
  102. /* 0x08 */ 123, 129, 134, 138, 143, 147, 151, 156,
  103. /* 0x10 */ 157, 161, 164, 168, 170, 173, 176, 179,
  104. /* 0x18 */ 181, 185, 187, 190, 192, 194, 197, 199,
  105. /* 0x20 */ 200, 202, 204, 206, 209, 211, 213, 215,
  106. /* 0x28 */ 217, 219, 221, 222, 224, 225, 227, 229,
  107. /* 0x30 */ 231, 232, 234, 236, 237, 239, 240, 242,
  108. /* 0x38 */ 244, 245, 246, 248, 250, 251, 252, 254,
  109. };
  110. b = fls64(a);
  111. if (b < 7) {
  112. /* a in [0..63] */
  113. return ((u32)v[(u32)a] + 35) >> 6;
  114. }
  115. b = ((b * 84) >> 8) - 1;
  116. shift = (a >> (b * 3));
  117. x = ((u32)(((u32)v[shift] + 10) << b)) >> 6;
  118. /*
  119. * Newton-Raphson iteration
  120. * 2
  121. * x = ( 2 * x + a / x ) / 3
  122. * k+1 k k
  123. */
  124. x = (2 * x + (u32)div64_64(a, (u64)x * (u64)(x - 1)));
  125. x = ((x * 341) >> 10);
  126. return x;
  127. }
  128. /*
  129. * Compute congestion window to use.
  130. */
  131. static inline void bictcp_update(struct bictcp *ca, u32 cwnd)
  132. {
  133. u64 offs;
  134. u32 delta, t, bic_target, min_cnt, max_cnt;
  135. ca->ack_cnt++; /* count the number of ACKs */
  136. if (ca->last_cwnd == cwnd &&
  137. (s32)(tcp_time_stamp - ca->last_time) <= HZ / 32)
  138. return;
  139. ca->last_cwnd = cwnd;
  140. ca->last_time = tcp_time_stamp;
  141. if (ca->epoch_start == 0) {
  142. ca->epoch_start = tcp_time_stamp; /* record the beginning of an epoch */
  143. ca->ack_cnt = 1; /* start counting */
  144. ca->tcp_cwnd = cwnd; /* syn with cubic */
  145. if (ca->last_max_cwnd <= cwnd) {
  146. ca->bic_K = 0;
  147. ca->bic_origin_point = cwnd;
  148. } else {
  149. /* Compute new K based on
  150. * (wmax-cwnd) * (srtt>>3 / HZ) / c * 2^(3*bictcp_HZ)
  151. */
  152. ca->bic_K = cubic_root(cube_factor
  153. * (ca->last_max_cwnd - cwnd));
  154. ca->bic_origin_point = ca->last_max_cwnd;
  155. }
  156. }
  157. /* cubic function - calc*/
  158. /* calculate c * time^3 / rtt,
  159. * while considering overflow in calculation of time^3
  160. * (so time^3 is done by using 64 bit)
  161. * and without the support of division of 64bit numbers
  162. * (so all divisions are done by using 32 bit)
  163. * also NOTE the unit of those veriables
  164. * time = (t - K) / 2^bictcp_HZ
  165. * c = bic_scale >> 10
  166. * rtt = (srtt >> 3) / HZ
  167. * !!! The following code does not have overflow problems,
  168. * if the cwnd < 1 million packets !!!
  169. */
  170. /* change the unit from HZ to bictcp_HZ */
  171. t = ((tcp_time_stamp + (ca->delay_min>>3) - ca->epoch_start)
  172. << BICTCP_HZ) / HZ;
  173. if (t < ca->bic_K) /* t - K */
  174. offs = ca->bic_K - t;
  175. else
  176. offs = t - ca->bic_K;
  177. /* c/rtt * (t-K)^3 */
  178. delta = (cube_rtt_scale * offs * offs * offs) >> (10+3*BICTCP_HZ);
  179. if (t < ca->bic_K) /* below origin*/
  180. bic_target = ca->bic_origin_point - delta;
  181. else /* above origin*/
  182. bic_target = ca->bic_origin_point + delta;
  183. /* cubic function - calc bictcp_cnt*/
  184. if (bic_target > cwnd) {
  185. ca->cnt = cwnd / (bic_target - cwnd);
  186. } else {
  187. ca->cnt = 100 * cwnd; /* very small increment*/
  188. }
  189. if (ca->delay_min > 0) {
  190. /* max increment = Smax * rtt / 0.1 */
  191. min_cnt = (cwnd * HZ * 8)/(10 * max_increment * ca->delay_min);
  192. /* use concave growth when the target is above the origin */
  193. if (ca->cnt < min_cnt && t >= ca->bic_K)
  194. ca->cnt = min_cnt;
  195. }
  196. /* slow start and low utilization */
  197. if (ca->loss_cwnd == 0) /* could be aggressive in slow start */
  198. ca->cnt = 50;
  199. /* TCP Friendly */
  200. if (tcp_friendliness) {
  201. u32 scale = beta_scale;
  202. delta = (cwnd * scale) >> 3;
  203. while (ca->ack_cnt > delta) { /* update tcp cwnd */
  204. ca->ack_cnt -= delta;
  205. ca->tcp_cwnd++;
  206. }
  207. if (ca->tcp_cwnd > cwnd){ /* if bic is slower than tcp */
  208. delta = ca->tcp_cwnd - cwnd;
  209. max_cnt = cwnd / delta;
  210. if (ca->cnt > max_cnt)
  211. ca->cnt = max_cnt;
  212. }
  213. }
  214. ca->cnt = (ca->cnt << ACK_RATIO_SHIFT) / ca->delayed_ack;
  215. if (ca->cnt == 0) /* cannot be zero */
  216. ca->cnt = 1;
  217. }
  218. static void bictcp_cong_avoid(struct sock *sk, u32 ack,
  219. u32 in_flight, int data_acked)
  220. {
  221. struct tcp_sock *tp = tcp_sk(sk);
  222. struct bictcp *ca = inet_csk_ca(sk);
  223. if (!tcp_is_cwnd_limited(sk, in_flight))
  224. return;
  225. if (tp->snd_cwnd <= tp->snd_ssthresh)
  226. tcp_slow_start(tp);
  227. else {
  228. bictcp_update(ca, tp->snd_cwnd);
  229. /* In dangerous area, increase slowly.
  230. * In theory this is tp->snd_cwnd += 1 / tp->snd_cwnd
  231. */
  232. if (tp->snd_cwnd_cnt >= ca->cnt) {
  233. if (tp->snd_cwnd < tp->snd_cwnd_clamp)
  234. tp->snd_cwnd++;
  235. tp->snd_cwnd_cnt = 0;
  236. } else
  237. tp->snd_cwnd_cnt++;
  238. }
  239. }
  240. static u32 bictcp_recalc_ssthresh(struct sock *sk)
  241. {
  242. const struct tcp_sock *tp = tcp_sk(sk);
  243. struct bictcp *ca = inet_csk_ca(sk);
  244. ca->epoch_start = 0; /* end of epoch */
  245. /* Wmax and fast convergence */
  246. if (tp->snd_cwnd < ca->last_max_cwnd && fast_convergence)
  247. ca->last_max_cwnd = (tp->snd_cwnd * (BICTCP_BETA_SCALE + beta))
  248. / (2 * BICTCP_BETA_SCALE);
  249. else
  250. ca->last_max_cwnd = tp->snd_cwnd;
  251. ca->loss_cwnd = tp->snd_cwnd;
  252. return max((tp->snd_cwnd * beta) / BICTCP_BETA_SCALE, 2U);
  253. }
  254. static u32 bictcp_undo_cwnd(struct sock *sk)
  255. {
  256. struct bictcp *ca = inet_csk_ca(sk);
  257. return max(tcp_sk(sk)->snd_cwnd, ca->last_max_cwnd);
  258. }
  259. static void bictcp_state(struct sock *sk, u8 new_state)
  260. {
  261. if (new_state == TCP_CA_Loss)
  262. bictcp_reset(inet_csk_ca(sk));
  263. }
  264. /* Track delayed acknowledgment ratio using sliding window
  265. * ratio = (15*ratio + sample) / 16
  266. */
  267. static void bictcp_acked(struct sock *sk, u32 cnt, s32 rtt_us)
  268. {
  269. const struct inet_connection_sock *icsk = inet_csk(sk);
  270. struct bictcp *ca = inet_csk_ca(sk);
  271. u32 delay;
  272. if (icsk->icsk_ca_state == TCP_CA_Open) {
  273. cnt -= ca->delayed_ack >> ACK_RATIO_SHIFT;
  274. ca->delayed_ack += cnt;
  275. }
  276. /* Some calls are for duplicates without timetamps */
  277. if (rtt_us < 0)
  278. return;
  279. /* Discard delay samples right after fast recovery */
  280. if ((s32)(tcp_time_stamp - ca->epoch_start) < HZ)
  281. return;
  282. delay = usecs_to_jiffies(rtt_us) << 3;
  283. if (delay == 0)
  284. delay = 1;
  285. /* first time call or link delay decreases */
  286. if (ca->delay_min == 0 || ca->delay_min > delay)
  287. ca->delay_min = delay;
  288. }
  289. static struct tcp_congestion_ops cubictcp = {
  290. .init = bictcp_init,
  291. .ssthresh = bictcp_recalc_ssthresh,
  292. .cong_avoid = bictcp_cong_avoid,
  293. .set_state = bictcp_state,
  294. .undo_cwnd = bictcp_undo_cwnd,
  295. .pkts_acked = bictcp_acked,
  296. .owner = THIS_MODULE,
  297. .name = "cubic",
  298. };
  299. static int __init cubictcp_register(void)
  300. {
  301. BUILD_BUG_ON(sizeof(struct bictcp) > ICSK_CA_PRIV_SIZE);
  302. /* Precompute a bunch of the scaling factors that are used per-packet
  303. * based on SRTT of 100ms
  304. */
  305. beta_scale = 8*(BICTCP_BETA_SCALE+beta)/ 3 / (BICTCP_BETA_SCALE - beta);
  306. cube_rtt_scale = (bic_scale * 10); /* 1024*c/rtt */
  307. /* calculate the "K" for (wmax-cwnd) = c/rtt * K^3
  308. * so K = cubic_root( (wmax-cwnd)*rtt/c )
  309. * the unit of K is bictcp_HZ=2^10, not HZ
  310. *
  311. * c = bic_scale >> 10
  312. * rtt = 100ms
  313. *
  314. * the following code has been designed and tested for
  315. * cwnd < 1 million packets
  316. * RTT < 100 seconds
  317. * HZ < 1,000,00 (corresponding to 10 nano-second)
  318. */
  319. /* 1/c * 2^2*bictcp_HZ * srtt */
  320. cube_factor = 1ull << (10+3*BICTCP_HZ); /* 2^40 */
  321. /* divide by bic_scale and by constant Srtt (100ms) */
  322. do_div(cube_factor, bic_scale * 10);
  323. return tcp_register_congestion_control(&cubictcp);
  324. }
  325. static void __exit cubictcp_unregister(void)
  326. {
  327. tcp_unregister_congestion_control(&cubictcp);
  328. }
  329. module_init(cubictcp_register);
  330. module_exit(cubictcp_unregister);
  331. MODULE_AUTHOR("Sangtae Ha, Stephen Hemminger");
  332. MODULE_LICENSE("GPL");
  333. MODULE_DESCRIPTION("CUBIC TCP");
  334. MODULE_VERSION("2.1");