tcp_cubic.c 11 KB

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