tcp_vegas.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /*
  2. * TCP Vegas congestion control
  3. *
  4. * This is based on the congestion detection/avoidance scheme described in
  5. * Lawrence S. Brakmo and Larry L. Peterson.
  6. * "TCP Vegas: End to end congestion avoidance on a global internet."
  7. * IEEE Journal on Selected Areas in Communication, 13(8):1465--1480,
  8. * October 1995. Available from:
  9. * ftp://ftp.cs.arizona.edu/xkernel/Papers/jsac.ps
  10. *
  11. * See http://www.cs.arizona.edu/xkernel/ for their implementation.
  12. * The main aspects that distinguish this implementation from the
  13. * Arizona Vegas implementation are:
  14. * o We do not change the loss detection or recovery mechanisms of
  15. * Linux in any way. Linux already recovers from losses quite well,
  16. * using fine-grained timers, NewReno, and FACK.
  17. * o To avoid the performance penalty imposed by increasing cwnd
  18. * only every-other RTT during slow start, we increase during
  19. * every RTT during slow start, just like Reno.
  20. * o Largely to allow continuous cwnd growth during slow start,
  21. * we use the rate at which ACKs come back as the "actual"
  22. * rate, rather than the rate at which data is sent.
  23. * o To speed convergence to the right rate, we set the cwnd
  24. * to achieve the right ("actual") rate when we exit slow start.
  25. * o To filter out the noise caused by delayed ACKs, we use the
  26. * minimum RTT sample observed during the last RTT to calculate
  27. * the actual rate.
  28. * o When the sender re-starts from idle, it waits until it has
  29. * received ACKs for an entire flight of new data before making
  30. * a cwnd adjustment decision. The original Vegas implementation
  31. * assumed senders never went idle.
  32. */
  33. #include <linux/mm.h>
  34. #include <linux/module.h>
  35. #include <linux/skbuff.h>
  36. #include <linux/inet_diag.h>
  37. #include <net/tcp.h>
  38. #include "tcp_vegas.h"
  39. static int alpha = 2;
  40. static int beta = 4;
  41. static int gamma = 1;
  42. module_param(alpha, int, 0644);
  43. MODULE_PARM_DESC(alpha, "lower bound of packets in network");
  44. module_param(beta, int, 0644);
  45. MODULE_PARM_DESC(beta, "upper bound of packets in network");
  46. module_param(gamma, int, 0644);
  47. MODULE_PARM_DESC(gamma, "limit on increase (scale by 2)");
  48. /* There are several situations when we must "re-start" Vegas:
  49. *
  50. * o when a connection is established
  51. * o after an RTO
  52. * o after fast recovery
  53. * o when we send a packet and there is no outstanding
  54. * unacknowledged data (restarting an idle connection)
  55. *
  56. * In these circumstances we cannot do a Vegas calculation at the
  57. * end of the first RTT, because any calculation we do is using
  58. * stale info -- both the saved cwnd and congestion feedback are
  59. * stale.
  60. *
  61. * Instead we must wait until the completion of an RTT during
  62. * which we actually receive ACKs.
  63. */
  64. static void vegas_enable(struct sock *sk)
  65. {
  66. const struct tcp_sock *tp = tcp_sk(sk);
  67. struct vegas *vegas = inet_csk_ca(sk);
  68. /* Begin taking Vegas samples next time we send something. */
  69. vegas->doing_vegas_now = 1;
  70. /* Set the beginning of the next send window. */
  71. vegas->beg_snd_nxt = tp->snd_nxt;
  72. vegas->cntRTT = 0;
  73. vegas->minRTT = 0x7fffffff;
  74. }
  75. /* Stop taking Vegas samples for now. */
  76. static inline void vegas_disable(struct sock *sk)
  77. {
  78. struct vegas *vegas = inet_csk_ca(sk);
  79. vegas->doing_vegas_now = 0;
  80. }
  81. void tcp_vegas_init(struct sock *sk)
  82. {
  83. struct vegas *vegas = inet_csk_ca(sk);
  84. vegas->baseRTT = 0x7fffffff;
  85. vegas_enable(sk);
  86. }
  87. EXPORT_SYMBOL_GPL(tcp_vegas_init);
  88. /* Do RTT sampling needed for Vegas.
  89. * Basically we:
  90. * o min-filter RTT samples from within an RTT to get the current
  91. * propagation delay + queuing delay (we are min-filtering to try to
  92. * avoid the effects of delayed ACKs)
  93. * o min-filter RTT samples from a much longer window (forever for now)
  94. * to find the propagation delay (baseRTT)
  95. */
  96. void tcp_vegas_pkts_acked(struct sock *sk, u32 cnt, s32 rtt_us)
  97. {
  98. struct vegas *vegas = inet_csk_ca(sk);
  99. u32 vrtt;
  100. if (rtt_us < 0)
  101. return;
  102. /* Never allow zero rtt or baseRTT */
  103. vrtt = rtt_us + 1;
  104. /* Filter to find propagation delay: */
  105. if (vrtt < vegas->baseRTT)
  106. vegas->baseRTT = vrtt;
  107. /* Find the min RTT during the last RTT to find
  108. * the current prop. delay + queuing delay:
  109. */
  110. vegas->minRTT = min(vegas->minRTT, vrtt);
  111. vegas->cntRTT++;
  112. }
  113. EXPORT_SYMBOL_GPL(tcp_vegas_pkts_acked);
  114. void tcp_vegas_state(struct sock *sk, u8 ca_state)
  115. {
  116. if (ca_state == TCP_CA_Open)
  117. vegas_enable(sk);
  118. else
  119. vegas_disable(sk);
  120. }
  121. EXPORT_SYMBOL_GPL(tcp_vegas_state);
  122. /*
  123. * If the connection is idle and we are restarting,
  124. * then we don't want to do any Vegas calculations
  125. * until we get fresh RTT samples. So when we
  126. * restart, we reset our Vegas state to a clean
  127. * slate. After we get acks for this flight of
  128. * packets, _then_ we can make Vegas calculations
  129. * again.
  130. */
  131. void tcp_vegas_cwnd_event(struct sock *sk, enum tcp_ca_event event)
  132. {
  133. if (event == CA_EVENT_CWND_RESTART ||
  134. event == CA_EVENT_TX_START)
  135. tcp_vegas_init(sk);
  136. }
  137. EXPORT_SYMBOL_GPL(tcp_vegas_cwnd_event);
  138. static inline u32 tcp_vegas_ssthresh(struct tcp_sock *tp)
  139. {
  140. return min(tp->snd_ssthresh, tp->snd_cwnd-1);
  141. }
  142. static void tcp_vegas_cong_avoid(struct sock *sk, u32 ack, u32 in_flight)
  143. {
  144. struct tcp_sock *tp = tcp_sk(sk);
  145. struct vegas *vegas = inet_csk_ca(sk);
  146. if (!vegas->doing_vegas_now) {
  147. tcp_reno_cong_avoid(sk, ack, in_flight);
  148. return;
  149. }
  150. if (after(ack, vegas->beg_snd_nxt)) {
  151. /* Do the Vegas once-per-RTT cwnd adjustment. */
  152. /* Save the extent of the current window so we can use this
  153. * at the end of the next RTT.
  154. */
  155. vegas->beg_snd_nxt = tp->snd_nxt;
  156. /* We do the Vegas calculations only if we got enough RTT
  157. * samples that we can be reasonably sure that we got
  158. * at least one RTT sample that wasn't from a delayed ACK.
  159. * If we only had 2 samples total,
  160. * then that means we're getting only 1 ACK per RTT, which
  161. * means they're almost certainly delayed ACKs.
  162. * If we have 3 samples, we should be OK.
  163. */
  164. if (vegas->cntRTT <= 2) {
  165. /* We don't have enough RTT samples to do the Vegas
  166. * calculation, so we'll behave like Reno.
  167. */
  168. tcp_reno_cong_avoid(sk, ack, in_flight);
  169. } else {
  170. u32 rtt, diff;
  171. u64 target_cwnd;
  172. /* We have enough RTT samples, so, using the Vegas
  173. * algorithm, we determine if we should increase or
  174. * decrease cwnd, and by how much.
  175. */
  176. /* Pluck out the RTT we are using for the Vegas
  177. * calculations. This is the min RTT seen during the
  178. * last RTT. Taking the min filters out the effects
  179. * of delayed ACKs, at the cost of noticing congestion
  180. * a bit later.
  181. */
  182. rtt = vegas->minRTT;
  183. /* Calculate the cwnd we should have, if we weren't
  184. * going too fast.
  185. *
  186. * This is:
  187. * (actual rate in segments) * baseRTT
  188. */
  189. target_cwnd = tp->snd_cwnd * vegas->baseRTT / rtt;
  190. /* Calculate the difference between the window we had,
  191. * and the window we would like to have. This quantity
  192. * is the "Diff" from the Arizona Vegas papers.
  193. */
  194. diff = tp->snd_cwnd * (rtt-vegas->baseRTT) / vegas->baseRTT;
  195. if (diff > gamma && tp->snd_cwnd <= tp->snd_ssthresh) {
  196. /* Going too fast. Time to slow down
  197. * and switch to congestion avoidance.
  198. */
  199. /* Set cwnd to match the actual rate
  200. * exactly:
  201. * cwnd = (actual rate) * baseRTT
  202. * Then we add 1 because the integer
  203. * truncation robs us of full link
  204. * utilization.
  205. */
  206. tp->snd_cwnd = min(tp->snd_cwnd, (u32)target_cwnd+1);
  207. tp->snd_ssthresh = tcp_vegas_ssthresh(tp);
  208. } else if (tp->snd_cwnd <= tp->snd_ssthresh) {
  209. /* Slow start. */
  210. tcp_slow_start(tp);
  211. } else {
  212. /* Congestion avoidance. */
  213. /* Figure out where we would like cwnd
  214. * to be.
  215. */
  216. if (diff > beta) {
  217. /* The old window was too fast, so
  218. * we slow down.
  219. */
  220. tp->snd_cwnd--;
  221. tp->snd_ssthresh
  222. = tcp_vegas_ssthresh(tp);
  223. } else if (diff < alpha) {
  224. /* We don't have enough extra packets
  225. * in the network, so speed up.
  226. */
  227. tp->snd_cwnd++;
  228. } else {
  229. /* Sending just as fast as we
  230. * should be.
  231. */
  232. }
  233. }
  234. if (tp->snd_cwnd < 2)
  235. tp->snd_cwnd = 2;
  236. else if (tp->snd_cwnd > tp->snd_cwnd_clamp)
  237. tp->snd_cwnd = tp->snd_cwnd_clamp;
  238. tp->snd_ssthresh = tcp_current_ssthresh(sk);
  239. }
  240. /* Wipe the slate clean for the next RTT. */
  241. vegas->cntRTT = 0;
  242. vegas->minRTT = 0x7fffffff;
  243. }
  244. /* Use normal slow start */
  245. else if (tp->snd_cwnd <= tp->snd_ssthresh)
  246. tcp_slow_start(tp);
  247. }
  248. /* Extract info for Tcp socket info provided via netlink. */
  249. void tcp_vegas_get_info(struct sock *sk, u32 ext, struct sk_buff *skb)
  250. {
  251. const struct vegas *ca = inet_csk_ca(sk);
  252. if (ext & (1 << (INET_DIAG_VEGASINFO - 1))) {
  253. struct tcpvegas_info info = {
  254. .tcpv_enabled = ca->doing_vegas_now,
  255. .tcpv_rttcnt = ca->cntRTT,
  256. .tcpv_rtt = ca->baseRTT,
  257. .tcpv_minrtt = ca->minRTT,
  258. };
  259. nla_put(skb, INET_DIAG_VEGASINFO, sizeof(info), &info);
  260. }
  261. }
  262. EXPORT_SYMBOL_GPL(tcp_vegas_get_info);
  263. static struct tcp_congestion_ops tcp_vegas __read_mostly = {
  264. .flags = TCP_CONG_RTT_STAMP,
  265. .init = tcp_vegas_init,
  266. .ssthresh = tcp_reno_ssthresh,
  267. .cong_avoid = tcp_vegas_cong_avoid,
  268. .min_cwnd = tcp_reno_min_cwnd,
  269. .pkts_acked = tcp_vegas_pkts_acked,
  270. .set_state = tcp_vegas_state,
  271. .cwnd_event = tcp_vegas_cwnd_event,
  272. .get_info = tcp_vegas_get_info,
  273. .owner = THIS_MODULE,
  274. .name = "vegas",
  275. };
  276. static int __init tcp_vegas_register(void)
  277. {
  278. BUILD_BUG_ON(sizeof(struct vegas) > ICSK_CA_PRIV_SIZE);
  279. tcp_register_congestion_control(&tcp_vegas);
  280. return 0;
  281. }
  282. static void __exit tcp_vegas_unregister(void)
  283. {
  284. tcp_unregister_congestion_control(&tcp_vegas);
  285. }
  286. module_init(tcp_vegas_register);
  287. module_exit(tcp_vegas_unregister);
  288. MODULE_AUTHOR("Stephen Hemminger");
  289. MODULE_LICENSE("GPL");
  290. MODULE_DESCRIPTION("TCP Vegas");