tcp_illinois.c 8.1 KB

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