tcp_illinois.c 8.2 KB

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