tcp_cubic.c 10 KB

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