tcp_illinois.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * TCP Illinois congestion control.
  4. * Home page:
  5. * http://www.ews.uiuc.edu/~shaoliu/tcpillinois/index.html
  6. *
  7. * The algorithm is described in:
  8. * "TCP-Illinois: A Loss and Delay-Based Congestion Control Algorithm
  9. * for High-Speed Networks"
  10. * http://tamerbasar.csl.illinois.edu/LiuBasarSrikantPerfEvalArtJun2008.pdf
  11. *
  12. * Implemented from description in paper and ns-2 simulation.
  13. * Copyright (C) 2007 Stephen Hemminger <[email protected]>
  14. */
  15. #include <linux/module.h>
  16. #include <linux/skbuff.h>
  17. #include <linux/inet_diag.h>
  18. #include <asm/div64.h>
  19. #include <net/tcp.h>
  20. #define ALPHA_SHIFT 7
  21. #define ALPHA_SCALE (1u<<ALPHA_SHIFT)
  22. #define ALPHA_MIN ((3*ALPHA_SCALE)/10) /* ~0.3 */
  23. #define ALPHA_MAX (10*ALPHA_SCALE) /* 10.0 */
  24. #define ALPHA_BASE ALPHA_SCALE /* 1.0 */
  25. #define RTT_MAX (U32_MAX / ALPHA_MAX) /* 3.3 secs */
  26. #define BETA_SHIFT 6
  27. #define BETA_SCALE (1u<<BETA_SHIFT)
  28. #define BETA_MIN (BETA_SCALE/8) /* 0.125 */
  29. #define BETA_MAX (BETA_SCALE/2) /* 0.5 */
  30. #define BETA_BASE BETA_MAX
  31. static int win_thresh __read_mostly = 15;
  32. module_param(win_thresh, int, 0);
  33. MODULE_PARM_DESC(win_thresh, "Window threshold for starting adaptive sizing");
  34. static int theta __read_mostly = 5;
  35. module_param(theta, int, 0);
  36. MODULE_PARM_DESC(theta, "# of fast RTT's before full growth");
  37. /* TCP Illinois Parameters */
  38. struct illinois {
  39. u64 sum_rtt; /* sum of rtt's measured within last rtt */
  40. u16 cnt_rtt; /* # of rtts measured within last rtt */
  41. u32 base_rtt; /* min of all rtt in usec */
  42. u32 max_rtt; /* max of all rtt in usec */
  43. u32 end_seq; /* right edge of current RTT */
  44. u32 alpha; /* Additive increase */
  45. u32 beta; /* Muliplicative decrease */
  46. u16 acked; /* # packets acked by current ACK */
  47. u8 rtt_above; /* average rtt has gone above threshold */
  48. u8 rtt_low; /* # of rtts measurements below threshold */
  49. };
  50. static void rtt_reset(struct sock *sk)
  51. {
  52. struct tcp_sock *tp = tcp_sk(sk);
  53. struct illinois *ca = inet_csk_ca(sk);
  54. ca->end_seq = tp->snd_nxt;
  55. ca->cnt_rtt = 0;
  56. ca->sum_rtt = 0;
  57. /* TODO: age max_rtt? */
  58. }
  59. static void tcp_illinois_init(struct sock *sk)
  60. {
  61. struct illinois *ca = inet_csk_ca(sk);
  62. ca->alpha = ALPHA_MAX;
  63. ca->beta = BETA_BASE;
  64. ca->base_rtt = 0x7fffffff;
  65. ca->max_rtt = 0;
  66. ca->acked = 0;
  67. ca->rtt_low = 0;
  68. ca->rtt_above = 0;
  69. rtt_reset(sk);
  70. }
  71. /* Measure RTT for each ack. */
  72. static void tcp_illinois_acked(struct sock *sk, const struct ack_sample *sample)
  73. {
  74. struct illinois *ca = inet_csk_ca(sk);
  75. s32 rtt_us = sample->rtt_us;
  76. ca->acked = sample->pkts_acked;
  77. /* dup ack, no rtt sample */
  78. if (rtt_us < 0)
  79. return;
  80. /* ignore bogus values, this prevents wraparound in alpha math */
  81. if (rtt_us > RTT_MAX)
  82. rtt_us = RTT_MAX;
  83. /* keep track of minimum RTT seen so far */
  84. if (ca->base_rtt > rtt_us)
  85. ca->base_rtt = rtt_us;
  86. /* and max */
  87. if (ca->max_rtt < rtt_us)
  88. ca->max_rtt = rtt_us;
  89. ++ca->cnt_rtt;
  90. ca->sum_rtt += rtt_us;
  91. }
  92. /* Maximum queuing delay */
  93. static inline u32 max_delay(const struct illinois *ca)
  94. {
  95. return ca->max_rtt - ca->base_rtt;
  96. }
  97. /* Average queuing delay */
  98. static inline u32 avg_delay(const struct illinois *ca)
  99. {
  100. u64 t = ca->sum_rtt;
  101. do_div(t, ca->cnt_rtt);
  102. return t - ca->base_rtt;
  103. }
  104. /*
  105. * Compute value of alpha used for additive increase.
  106. * If small window then use 1.0, equivalent to Reno.
  107. *
  108. * For larger windows, adjust based on average delay.
  109. * A. If average delay is at minimum (we are uncongested),
  110. * then use large alpha (10.0) to increase faster.
  111. * B. If average delay is at maximum (getting congested)
  112. * then use small alpha (0.3)
  113. *
  114. * The result is a convex window growth curve.
  115. */
  116. static u32 alpha(struct illinois *ca, u32 da, u32 dm)
  117. {
  118. u32 d1 = dm / 100; /* Low threshold */
  119. if (da <= d1) {
  120. /* If never got out of low delay zone, then use max */
  121. if (!ca->rtt_above)
  122. return ALPHA_MAX;
  123. /* Wait for 5 good RTT's before allowing alpha to go alpha max.
  124. * This prevents one good RTT from causing sudden window increase.
  125. */
  126. if (++ca->rtt_low < theta)
  127. return ca->alpha;
  128. ca->rtt_low = 0;
  129. ca->rtt_above = 0;
  130. return ALPHA_MAX;
  131. }
  132. ca->rtt_above = 1;
  133. /*
  134. * Based on:
  135. *
  136. * (dm - d1) amin amax
  137. * k1 = -------------------
  138. * amax - amin
  139. *
  140. * (dm - d1) amin
  141. * k2 = ---------------- - d1
  142. * amax - amin
  143. *
  144. * k1
  145. * alpha = ----------
  146. * k2 + da
  147. */
  148. dm -= d1;
  149. da -= d1;
  150. return (dm * ALPHA_MAX) /
  151. (dm + (da * (ALPHA_MAX - ALPHA_MIN)) / ALPHA_MIN);
  152. }
  153. /*
  154. * Beta used for multiplicative decrease.
  155. * For small window sizes returns same value as Reno (0.5)
  156. *
  157. * If delay is small (10% of max) then beta = 1/8
  158. * If delay is up to 80% of max then beta = 1/2
  159. * In between is a linear function
  160. */
  161. static u32 beta(u32 da, u32 dm)
  162. {
  163. u32 d2, d3;
  164. d2 = dm / 10;
  165. if (da <= d2)
  166. return BETA_MIN;
  167. d3 = (8 * dm) / 10;
  168. if (da >= d3 || d3 <= d2)
  169. return BETA_MAX;
  170. /*
  171. * Based on:
  172. *
  173. * bmin d3 - bmax d2
  174. * k3 = -------------------
  175. * d3 - d2
  176. *
  177. * bmax - bmin
  178. * k4 = -------------
  179. * d3 - d2
  180. *
  181. * b = k3 + k4 da
  182. */
  183. return (BETA_MIN * d3 - BETA_MAX * d2 + (BETA_MAX - BETA_MIN) * da)
  184. / (d3 - d2);
  185. }
  186. /* Update alpha and beta values once per RTT */
  187. static void update_params(struct sock *sk)
  188. {
  189. struct tcp_sock *tp = tcp_sk(sk);
  190. struct illinois *ca = inet_csk_ca(sk);
  191. if (tcp_snd_cwnd(tp) < win_thresh) {
  192. ca->alpha = ALPHA_BASE;
  193. ca->beta = BETA_BASE;
  194. } else if (ca->cnt_rtt > 0) {
  195. u32 dm = max_delay(ca);
  196. u32 da = avg_delay(ca);
  197. ca->alpha = alpha(ca, da, dm);
  198. ca->beta = beta(da, dm);
  199. }
  200. rtt_reset(sk);
  201. }
  202. /*
  203. * In case of loss, reset to default values
  204. */
  205. static void tcp_illinois_state(struct sock *sk, u8 new_state)
  206. {
  207. struct illinois *ca = inet_csk_ca(sk);
  208. if (new_state == TCP_CA_Loss) {
  209. ca->alpha = ALPHA_BASE;
  210. ca->beta = BETA_BASE;
  211. ca->rtt_low = 0;
  212. ca->rtt_above = 0;
  213. rtt_reset(sk);
  214. }
  215. }
  216. /*
  217. * Increase window in response to successful acknowledgment.
  218. */
  219. static void tcp_illinois_cong_avoid(struct sock *sk, u32 ack, u32 acked)
  220. {
  221. struct tcp_sock *tp = tcp_sk(sk);
  222. struct illinois *ca = inet_csk_ca(sk);
  223. if (after(ack, ca->end_seq))
  224. update_params(sk);
  225. /* RFC2861 only increase cwnd if fully utilized */
  226. if (!tcp_is_cwnd_limited(sk))
  227. return;
  228. /* In slow start */
  229. if (tcp_in_slow_start(tp))
  230. tcp_slow_start(tp, acked);
  231. else {
  232. u32 delta;
  233. /* snd_cwnd_cnt is # of packets since last cwnd increment */
  234. tp->snd_cwnd_cnt += ca->acked;
  235. ca->acked = 1;
  236. /* This is close approximation of:
  237. * tp->snd_cwnd += alpha/tp->snd_cwnd
  238. */
  239. delta = (tp->snd_cwnd_cnt * ca->alpha) >> ALPHA_SHIFT;
  240. if (delta >= tcp_snd_cwnd(tp)) {
  241. tcp_snd_cwnd_set(tp, min(tcp_snd_cwnd(tp) + delta / tcp_snd_cwnd(tp),
  242. (u32)tp->snd_cwnd_clamp));
  243. tp->snd_cwnd_cnt = 0;
  244. }
  245. }
  246. }
  247. static u32 tcp_illinois_ssthresh(struct sock *sk)
  248. {
  249. struct tcp_sock *tp = tcp_sk(sk);
  250. struct illinois *ca = inet_csk_ca(sk);
  251. u32 decr;
  252. /* Multiplicative decrease */
  253. decr = (tcp_snd_cwnd(tp) * ca->beta) >> BETA_SHIFT;
  254. return max(tcp_snd_cwnd(tp) - decr, 2U);
  255. }
  256. /* Extract info for Tcp socket info provided via netlink. */
  257. static size_t tcp_illinois_info(struct sock *sk, u32 ext, int *attr,
  258. union tcp_cc_info *info)
  259. {
  260. const struct illinois *ca = inet_csk_ca(sk);
  261. if (ext & (1 << (INET_DIAG_VEGASINFO - 1))) {
  262. info->vegas.tcpv_enabled = 1;
  263. info->vegas.tcpv_rttcnt = ca->cnt_rtt;
  264. info->vegas.tcpv_minrtt = ca->base_rtt;
  265. info->vegas.tcpv_rtt = 0;
  266. if (info->vegas.tcpv_rttcnt > 0) {
  267. u64 t = ca->sum_rtt;
  268. do_div(t, info->vegas.tcpv_rttcnt);
  269. info->vegas.tcpv_rtt = t;
  270. }
  271. *attr = INET_DIAG_VEGASINFO;
  272. return sizeof(struct tcpvegas_info);
  273. }
  274. return 0;
  275. }
  276. static struct tcp_congestion_ops tcp_illinois __read_mostly = {
  277. .init = tcp_illinois_init,
  278. .ssthresh = tcp_illinois_ssthresh,
  279. .undo_cwnd = tcp_reno_undo_cwnd,
  280. .cong_avoid = tcp_illinois_cong_avoid,
  281. .set_state = tcp_illinois_state,
  282. .get_info = tcp_illinois_info,
  283. .pkts_acked = tcp_illinois_acked,
  284. .owner = THIS_MODULE,
  285. .name = "illinois",
  286. };
  287. static int __init tcp_illinois_register(void)
  288. {
  289. BUILD_BUG_ON(sizeof(struct illinois) > ICSK_CA_PRIV_SIZE);
  290. return tcp_register_congestion_control(&tcp_illinois);
  291. }
  292. static void __exit tcp_illinois_unregister(void)
  293. {
  294. tcp_unregister_congestion_control(&tcp_illinois);
  295. }
  296. module_init(tcp_illinois_register);
  297. module_exit(tcp_illinois_unregister);
  298. MODULE_AUTHOR("Stephen Hemminger, Shao Liu");
  299. MODULE_LICENSE("GPL");
  300. MODULE_DESCRIPTION("TCP Illinois");
  301. MODULE_VERSION("1.0");