tcp_illinois.c 8.2 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://tamerbasar.csl.illinois.edu/LiuBasarSrikantPerfEvalArtJun2008.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 RTT_MAX (U32_MAX / ALPHA_MAX) /* 3.3 secs */
  25. #define BETA_SHIFT 6
  26. #define BETA_SCALE (1u<<BETA_SHIFT)
  27. #define BETA_MIN (BETA_SCALE/8) /* 0.125 */
  28. #define BETA_MAX (BETA_SCALE/2) /* 0.5 */
  29. #define BETA_BASE BETA_MAX
  30. static int win_thresh __read_mostly = 15;
  31. module_param(win_thresh, int, 0);
  32. MODULE_PARM_DESC(win_thresh, "Window threshold for starting adaptive sizing");
  33. static int theta __read_mostly = 5;
  34. module_param(theta, int, 0);
  35. MODULE_PARM_DESC(theta, "# of fast RTT's before full growth");
  36. /* TCP Illinois Parameters */
  37. struct illinois {
  38. u64 sum_rtt; /* sum of rtt's measured within last rtt */
  39. u16 cnt_rtt; /* # of rtts measured within last rtt */
  40. u32 base_rtt; /* min of all rtt in usec */
  41. u32 max_rtt; /* max of all rtt in usec */
  42. u32 end_seq; /* right edge of current RTT */
  43. u32 alpha; /* Additive increase */
  44. u32 beta; /* Muliplicative decrease */
  45. u16 acked; /* # packets acked by current ACK */
  46. u8 rtt_above; /* average rtt has gone above threshold */
  47. u8 rtt_low; /* # of rtts measurements below threshold */
  48. };
  49. static void rtt_reset(struct sock *sk)
  50. {
  51. struct tcp_sock *tp = tcp_sk(sk);
  52. struct illinois *ca = inet_csk_ca(sk);
  53. ca->end_seq = tp->snd_nxt;
  54. ca->cnt_rtt = 0;
  55. ca->sum_rtt = 0;
  56. /* TODO: age max_rtt? */
  57. }
  58. static void tcp_illinois_init(struct sock *sk)
  59. {
  60. struct illinois *ca = inet_csk_ca(sk);
  61. ca->alpha = ALPHA_MAX;
  62. ca->beta = BETA_BASE;
  63. ca->base_rtt = 0x7fffffff;
  64. ca->max_rtt = 0;
  65. ca->acked = 0;
  66. ca->rtt_low = 0;
  67. ca->rtt_above = 0;
  68. rtt_reset(sk);
  69. }
  70. /* Measure RTT for each ack. */
  71. static void tcp_illinois_acked(struct sock *sk, const struct ack_sample *sample)
  72. {
  73. struct illinois *ca = inet_csk_ca(sk);
  74. s32 rtt_us = sample->rtt_us;
  75. ca->acked = sample->pkts_acked;
  76. /* dup ack, no rtt sample */
  77. if (rtt_us < 0)
  78. return;
  79. /* ignore bogus values, this prevents wraparound in alpha math */
  80. if (rtt_us > RTT_MAX)
  81. rtt_us = RTT_MAX;
  82. /* keep track of minimum RTT seen so far */
  83. if (ca->base_rtt > rtt_us)
  84. ca->base_rtt = rtt_us;
  85. /* and max */
  86. if (ca->max_rtt < rtt_us)
  87. ca->max_rtt = rtt_us;
  88. ++ca->cnt_rtt;
  89. ca->sum_rtt += rtt_us;
  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 acked)
  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))
  226. return;
  227. /* In slow start */
  228. if (tcp_in_slow_start(tp))
  229. tcp_slow_start(tp, acked);
  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 size_t tcp_illinois_info(struct sock *sk, u32 ext, int *attr,
  255. union tcp_cc_info *info)
  256. {
  257. const struct illinois *ca = inet_csk_ca(sk);
  258. if (ext & (1 << (INET_DIAG_VEGASINFO - 1))) {
  259. info->vegas.tcpv_enabled = 1;
  260. info->vegas.tcpv_rttcnt = ca->cnt_rtt;
  261. info->vegas.tcpv_minrtt = ca->base_rtt;
  262. info->vegas.tcpv_rtt = 0;
  263. if (info->vegas.tcpv_rttcnt > 0) {
  264. u64 t = ca->sum_rtt;
  265. do_div(t, info->vegas.tcpv_rttcnt);
  266. info->vegas.tcpv_rtt = t;
  267. }
  268. *attr = INET_DIAG_VEGASINFO;
  269. return sizeof(struct tcpvegas_info);
  270. }
  271. return 0;
  272. }
  273. static struct tcp_congestion_ops tcp_illinois __read_mostly = {
  274. .init = tcp_illinois_init,
  275. .ssthresh = tcp_illinois_ssthresh,
  276. .cong_avoid = tcp_illinois_cong_avoid,
  277. .set_state = tcp_illinois_state,
  278. .get_info = tcp_illinois_info,
  279. .pkts_acked = tcp_illinois_acked,
  280. .owner = THIS_MODULE,
  281. .name = "illinois",
  282. };
  283. static int __init tcp_illinois_register(void)
  284. {
  285. BUILD_BUG_ON(sizeof(struct illinois) > ICSK_CA_PRIV_SIZE);
  286. return tcp_register_congestion_control(&tcp_illinois);
  287. }
  288. static void __exit tcp_illinois_unregister(void)
  289. {
  290. tcp_unregister_congestion_control(&tcp_illinois);
  291. }
  292. module_init(tcp_illinois_register);
  293. module_exit(tcp_illinois_unregister);
  294. MODULE_AUTHOR("Stephen Hemminger, Shao Liu");
  295. MODULE_LICENSE("GPL");
  296. MODULE_DESCRIPTION("TCP Illinois");
  297. MODULE_VERSION("1.0");