tcp_cubic.c 11 KB

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