tcp_illinois.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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_acked(struct sock *sk, u32 pkts_acked, ktime_t last)
  73. {
  74. struct illinois *ca = inet_csk_ca(sk);
  75. u32 rtt;
  76. ca->acked = pkts_acked;
  77. if (ktime_equal(last, net_invalid_timestamp()))
  78. return;
  79. rtt = ktime_to_us(net_timedelta(last));
  80. /* ignore bogus values, this prevents wraparound in alpha math */
  81. if (rtt > RTT_MAX)
  82. rtt = RTT_MAX;
  83. /* keep track of minimum RTT seen so far */
  84. if (ca->base_rtt > rtt)
  85. ca->base_rtt = rtt;
  86. /* and max */
  87. if (ca->max_rtt < rtt)
  88. ca->max_rtt = rtt;
  89. ++ca->cnt_rtt;
  90. ca->sum_rtt += rtt;
  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 (tp->snd_cwnd < 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 rtt,
  220. u32 in_flight, int flag)
  221. {
  222. struct tcp_sock *tp = tcp_sk(sk);
  223. struct illinois *ca = inet_csk_ca(sk);
  224. if (after(ack, ca->end_seq))
  225. update_params(sk);
  226. /* RFC2861 only increase cwnd if fully utilized */
  227. if (!tcp_is_cwnd_limited(sk, in_flight))
  228. return;
  229. /* In slow start */
  230. if (tp->snd_cwnd <= tp->snd_ssthresh)
  231. tcp_slow_start(tp);
  232. else {
  233. u32 delta;
  234. /* snd_cwnd_cnt is # of packets since last cwnd increment */
  235. tp->snd_cwnd_cnt += ca->acked;
  236. ca->acked = 1;
  237. /* This is close approximation of:
  238. * tp->snd_cwnd += alpha/tp->snd_cwnd
  239. */
  240. delta = (tp->snd_cwnd_cnt * ca->alpha) >> ALPHA_SHIFT;
  241. if (delta >= tp->snd_cwnd) {
  242. tp->snd_cwnd = min(tp->snd_cwnd + delta / tp->snd_cwnd,
  243. (u32) tp->snd_cwnd_clamp);
  244. tp->snd_cwnd_cnt = 0;
  245. }
  246. }
  247. }
  248. static u32 tcp_illinois_ssthresh(struct sock *sk)
  249. {
  250. struct tcp_sock *tp = tcp_sk(sk);
  251. struct illinois *ca = inet_csk_ca(sk);
  252. /* Multiplicative decrease */
  253. return max((tp->snd_cwnd * ca->beta) >> BETA_SHIFT, 2U);
  254. }
  255. /* Extract info for Tcp socket info provided via netlink. */
  256. static void tcp_illinois_info(struct sock *sk, u32 ext,
  257. struct sk_buff *skb)
  258. {
  259. const struct illinois *ca = inet_csk_ca(sk);
  260. if (ext & (1 << (INET_DIAG_VEGASINFO - 1))) {
  261. struct tcpvegas_info info = {
  262. .tcpv_enabled = 1,
  263. .tcpv_rttcnt = ca->cnt_rtt,
  264. .tcpv_minrtt = ca->base_rtt,
  265. };
  266. u64 t = ca->sum_rtt;
  267. do_div(t, ca->cnt_rtt);
  268. info.tcpv_rtt = t;
  269. nla_put(skb, INET_DIAG_VEGASINFO, sizeof(info), &info);
  270. }
  271. }
  272. static struct tcp_congestion_ops tcp_illinois = {
  273. .flags = TCP_CONG_RTT_STAMP,
  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. .get_info = tcp_illinois_info,
  280. .pkts_acked = tcp_illinois_acked,
  281. .owner = THIS_MODULE,
  282. .name = "illinois",
  283. };
  284. static int __init tcp_illinois_register(void)
  285. {
  286. BUILD_BUG_ON(sizeof(struct illinois) > ICSK_CA_PRIV_SIZE);
  287. return tcp_register_congestion_control(&tcp_illinois);
  288. }
  289. static void __exit tcp_illinois_unregister(void)
  290. {
  291. tcp_unregister_congestion_control(&tcp_illinois);
  292. }
  293. module_init(tcp_illinois_register);
  294. module_exit(tcp_illinois_unregister);
  295. MODULE_AUTHOR("Stephen Hemminger, Shao Liu");
  296. MODULE_LICENSE("GPL");
  297. MODULE_DESCRIPTION("TCP Illinois");
  298. MODULE_VERSION("1.0");