tcp_illinois.c 8.3 KB

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