tcp_vegas.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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);
  141. }
  142. static void tcp_vegas_cong_avoid(struct sock *sk, u32 ack, u32 acked)
  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, acked);
  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, acked);
  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 = (u64)tp->snd_cwnd * vegas->baseRTT;
  190. do_div(target_cwnd, rtt);
  191. /* Calculate the difference between the window we had,
  192. * and the window we would like to have. This quantity
  193. * is the "Diff" from the Arizona Vegas papers.
  194. */
  195. diff = tp->snd_cwnd * (rtt-vegas->baseRTT) / vegas->baseRTT;
  196. if (diff > gamma && tcp_in_slow_start(tp)) {
  197. /* Going too fast. Time to slow down
  198. * and switch to congestion avoidance.
  199. */
  200. /* Set cwnd to match the actual rate
  201. * exactly:
  202. * cwnd = (actual rate) * baseRTT
  203. * Then we add 1 because the integer
  204. * truncation robs us of full link
  205. * utilization.
  206. */
  207. tp->snd_cwnd = min(tp->snd_cwnd, (u32)target_cwnd+1);
  208. tp->snd_ssthresh = tcp_vegas_ssthresh(tp);
  209. } else if (tcp_in_slow_start(tp)) {
  210. /* Slow start. */
  211. tcp_slow_start(tp, acked);
  212. } else {
  213. /* Congestion avoidance. */
  214. /* Figure out where we would like cwnd
  215. * to be.
  216. */
  217. if (diff > beta) {
  218. /* The old window was too fast, so
  219. * we slow down.
  220. */
  221. tp->snd_cwnd--;
  222. tp->snd_ssthresh
  223. = tcp_vegas_ssthresh(tp);
  224. } else if (diff < alpha) {
  225. /* We don't have enough extra packets
  226. * in the network, so speed up.
  227. */
  228. tp->snd_cwnd++;
  229. } else {
  230. /* Sending just as fast as we
  231. * should be.
  232. */
  233. }
  234. }
  235. if (tp->snd_cwnd < 2)
  236. tp->snd_cwnd = 2;
  237. else if (tp->snd_cwnd > tp->snd_cwnd_clamp)
  238. tp->snd_cwnd = tp->snd_cwnd_clamp;
  239. tp->snd_ssthresh = tcp_current_ssthresh(sk);
  240. }
  241. /* Wipe the slate clean for the next RTT. */
  242. vegas->cntRTT = 0;
  243. vegas->minRTT = 0x7fffffff;
  244. }
  245. /* Use normal slow start */
  246. else if (tcp_in_slow_start(tp))
  247. tcp_slow_start(tp, acked);
  248. }
  249. /* Extract info for Tcp socket info provided via netlink. */
  250. size_t tcp_vegas_get_info(struct sock *sk, u32 ext, int *attr,
  251. union tcp_cc_info *info)
  252. {
  253. const struct vegas *ca = inet_csk_ca(sk);
  254. if (ext & (1 << (INET_DIAG_VEGASINFO - 1))) {
  255. info->vegas.tcpv_enabled = ca->doing_vegas_now,
  256. info->vegas.tcpv_rttcnt = ca->cntRTT,
  257. info->vegas.tcpv_rtt = ca->baseRTT,
  258. info->vegas.tcpv_minrtt = ca->minRTT,
  259. *attr = INET_DIAG_VEGASINFO;
  260. return sizeof(struct tcpvegas_info);
  261. }
  262. return 0;
  263. }
  264. EXPORT_SYMBOL_GPL(tcp_vegas_get_info);
  265. static struct tcp_congestion_ops tcp_vegas __read_mostly = {
  266. .init = tcp_vegas_init,
  267. .ssthresh = tcp_reno_ssthresh,
  268. .cong_avoid = tcp_vegas_cong_avoid,
  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");