tcp_vegas.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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. /* Default values of the Vegas variables, in fixed-point representation
  40. * with V_PARAM_SHIFT bits to the right of the binary point.
  41. */
  42. #define V_PARAM_SHIFT 1
  43. static int alpha = 2<<V_PARAM_SHIFT;
  44. static int beta = 4<<V_PARAM_SHIFT;
  45. static int gamma = 1<<V_PARAM_SHIFT;
  46. module_param(alpha, int, 0644);
  47. MODULE_PARM_DESC(alpha, "lower bound of packets in network (scale by 2)");
  48. module_param(beta, int, 0644);
  49. MODULE_PARM_DESC(beta, "upper bound of packets in network (scale by 2)");
  50. module_param(gamma, int, 0644);
  51. MODULE_PARM_DESC(gamma, "limit on increase (scale by 2)");
  52. /* There are several situations when we must "re-start" Vegas:
  53. *
  54. * o when a connection is established
  55. * o after an RTO
  56. * o after fast recovery
  57. * o when we send a packet and there is no outstanding
  58. * unacknowledged data (restarting an idle connection)
  59. *
  60. * In these circumstances we cannot do a Vegas calculation at the
  61. * end of the first RTT, because any calculation we do is using
  62. * stale info -- both the saved cwnd and congestion feedback are
  63. * stale.
  64. *
  65. * Instead we must wait until the completion of an RTT during
  66. * which we actually receive ACKs.
  67. */
  68. static void vegas_enable(struct sock *sk)
  69. {
  70. const struct tcp_sock *tp = tcp_sk(sk);
  71. struct vegas *vegas = inet_csk_ca(sk);
  72. /* Begin taking Vegas samples next time we send something. */
  73. vegas->doing_vegas_now = 1;
  74. /* Set the beginning of the next send window. */
  75. vegas->beg_snd_nxt = tp->snd_nxt;
  76. vegas->cntRTT = 0;
  77. vegas->minRTT = 0x7fffffff;
  78. }
  79. /* Stop taking Vegas samples for now. */
  80. static inline void vegas_disable(struct sock *sk)
  81. {
  82. struct vegas *vegas = inet_csk_ca(sk);
  83. vegas->doing_vegas_now = 0;
  84. }
  85. void tcp_vegas_init(struct sock *sk)
  86. {
  87. struct vegas *vegas = inet_csk_ca(sk);
  88. vegas->baseRTT = 0x7fffffff;
  89. vegas_enable(sk);
  90. }
  91. EXPORT_SYMBOL_GPL(tcp_vegas_init);
  92. /* Do RTT sampling needed for Vegas.
  93. * Basically we:
  94. * o min-filter RTT samples from within an RTT to get the current
  95. * propagation delay + queuing delay (we are min-filtering to try to
  96. * avoid the effects of delayed ACKs)
  97. * o min-filter RTT samples from a much longer window (forever for now)
  98. * to find the propagation delay (baseRTT)
  99. */
  100. void tcp_vegas_pkts_acked(struct sock *sk, u32 cnt, s32 rtt_us)
  101. {
  102. struct vegas *vegas = inet_csk_ca(sk);
  103. u32 vrtt;
  104. if (rtt_us < 0)
  105. return;
  106. /* Never allow zero rtt or baseRTT */
  107. vrtt = rtt_us + 1;
  108. /* Filter to find propagation delay: */
  109. if (vrtt < vegas->baseRTT)
  110. vegas->baseRTT = vrtt;
  111. /* Find the min RTT during the last RTT to find
  112. * the current prop. delay + queuing delay:
  113. */
  114. vegas->minRTT = min(vegas->minRTT, vrtt);
  115. vegas->cntRTT++;
  116. }
  117. EXPORT_SYMBOL_GPL(tcp_vegas_pkts_acked);
  118. void tcp_vegas_state(struct sock *sk, u8 ca_state)
  119. {
  120. if (ca_state == TCP_CA_Open)
  121. vegas_enable(sk);
  122. else
  123. vegas_disable(sk);
  124. }
  125. EXPORT_SYMBOL_GPL(tcp_vegas_state);
  126. /*
  127. * If the connection is idle and we are restarting,
  128. * then we don't want to do any Vegas calculations
  129. * until we get fresh RTT samples. So when we
  130. * restart, we reset our Vegas state to a clean
  131. * slate. After we get acks for this flight of
  132. * packets, _then_ we can make Vegas calculations
  133. * again.
  134. */
  135. void tcp_vegas_cwnd_event(struct sock *sk, enum tcp_ca_event event)
  136. {
  137. if (event == CA_EVENT_CWND_RESTART ||
  138. event == CA_EVENT_TX_START)
  139. tcp_vegas_init(sk);
  140. }
  141. EXPORT_SYMBOL_GPL(tcp_vegas_cwnd_event);
  142. static void tcp_vegas_cong_avoid(struct sock *sk, u32 ack,
  143. u32 in_flight, int flag)
  144. {
  145. struct tcp_sock *tp = tcp_sk(sk);
  146. struct vegas *vegas = inet_csk_ca(sk);
  147. if (!vegas->doing_vegas_now)
  148. return tcp_reno_cong_avoid(sk, ack, in_flight, flag);
  149. /* The key players are v_beg_snd_una and v_beg_snd_nxt.
  150. *
  151. * These are so named because they represent the approximate values
  152. * of snd_una and snd_nxt at the beginning of the current RTT. More
  153. * precisely, they represent the amount of data sent during the RTT.
  154. * At the end of the RTT, when we receive an ACK for v_beg_snd_nxt,
  155. * we will calculate that (v_beg_snd_nxt - v_beg_snd_una) outstanding
  156. * bytes of data have been ACKed during the course of the RTT, giving
  157. * an "actual" rate of:
  158. *
  159. * (v_beg_snd_nxt - v_beg_snd_una) / (rtt duration)
  160. *
  161. * Unfortunately, v_beg_snd_una is not exactly equal to snd_una,
  162. * because delayed ACKs can cover more than one segment, so they
  163. * don't line up nicely with the boundaries of RTTs.
  164. *
  165. * Another unfortunate fact of life is that delayed ACKs delay the
  166. * advance of the left edge of our send window, so that the number
  167. * of bytes we send in an RTT is often less than our cwnd will allow.
  168. * So we keep track of our cwnd separately, in v_beg_snd_cwnd.
  169. */
  170. if (after(ack, vegas->beg_snd_nxt)) {
  171. /* Do the Vegas once-per-RTT cwnd adjustment. */
  172. u32 old_wnd, old_snd_cwnd;
  173. /* Here old_wnd is essentially the window of data that was
  174. * sent during the previous RTT, and has all
  175. * been acknowledged in the course of the RTT that ended
  176. * with the ACK we just received. Likewise, old_snd_cwnd
  177. * is the cwnd during the previous RTT.
  178. */
  179. old_wnd = (vegas->beg_snd_nxt - vegas->beg_snd_una) /
  180. tp->mss_cache;
  181. old_snd_cwnd = vegas->beg_snd_cwnd;
  182. /* Save the extent of the current window so we can use this
  183. * at the end of the next RTT.
  184. */
  185. vegas->beg_snd_una = vegas->beg_snd_nxt;
  186. vegas->beg_snd_nxt = tp->snd_nxt;
  187. vegas->beg_snd_cwnd = tp->snd_cwnd;
  188. /* We do the Vegas calculations only if we got enough RTT
  189. * samples that we can be reasonably sure that we got
  190. * at least one RTT sample that wasn't from a delayed ACK.
  191. * If we only had 2 samples total,
  192. * then that means we're getting only 1 ACK per RTT, which
  193. * means they're almost certainly delayed ACKs.
  194. * If we have 3 samples, we should be OK.
  195. */
  196. if (vegas->cntRTT <= 2) {
  197. /* We don't have enough RTT samples to do the Vegas
  198. * calculation, so we'll behave like Reno.
  199. */
  200. tcp_reno_cong_avoid(sk, ack, in_flight, flag);
  201. } else {
  202. u32 rtt, target_cwnd, diff;
  203. /* We have enough RTT samples, so, using the Vegas
  204. * algorithm, we determine if we should increase or
  205. * decrease cwnd, and by how much.
  206. */
  207. /* Pluck out the RTT we are using for the Vegas
  208. * calculations. This is the min RTT seen during the
  209. * last RTT. Taking the min filters out the effects
  210. * of delayed ACKs, at the cost of noticing congestion
  211. * a bit later.
  212. */
  213. rtt = vegas->minRTT;
  214. /* Calculate the cwnd we should have, if we weren't
  215. * going too fast.
  216. *
  217. * This is:
  218. * (actual rate in segments) * baseRTT
  219. * We keep it as a fixed point number with
  220. * V_PARAM_SHIFT bits to the right of the binary point.
  221. */
  222. target_cwnd = ((old_wnd * vegas->baseRTT)
  223. << V_PARAM_SHIFT) / rtt;
  224. /* Calculate the difference between the window we had,
  225. * and the window we would like to have. This quantity
  226. * is the "Diff" from the Arizona Vegas papers.
  227. *
  228. * Again, this is a fixed point number with
  229. * V_PARAM_SHIFT bits to the right of the binary
  230. * point.
  231. */
  232. diff = (old_wnd << V_PARAM_SHIFT) - target_cwnd;
  233. if (tp->snd_cwnd <= tp->snd_ssthresh) {
  234. /* Slow start. */
  235. if (diff > gamma) {
  236. /* Going too fast. Time to slow down
  237. * and switch to congestion avoidance.
  238. */
  239. tp->snd_ssthresh = 2;
  240. /* Set cwnd to match the actual rate
  241. * exactly:
  242. * cwnd = (actual rate) * baseRTT
  243. * Then we add 1 because the integer
  244. * truncation robs us of full link
  245. * utilization.
  246. */
  247. tp->snd_cwnd = min(tp->snd_cwnd,
  248. (target_cwnd >>
  249. V_PARAM_SHIFT)+1);
  250. }
  251. tcp_slow_start(tp);
  252. } else {
  253. /* Congestion avoidance. */
  254. u32 next_snd_cwnd;
  255. /* Figure out where we would like cwnd
  256. * to be.
  257. */
  258. if (diff > beta) {
  259. /* The old window was too fast, so
  260. * we slow down.
  261. */
  262. next_snd_cwnd = old_snd_cwnd - 1;
  263. } else if (diff < alpha) {
  264. /* We don't have enough extra packets
  265. * in the network, so speed up.
  266. */
  267. next_snd_cwnd = old_snd_cwnd + 1;
  268. } else {
  269. /* Sending just as fast as we
  270. * should be.
  271. */
  272. next_snd_cwnd = old_snd_cwnd;
  273. }
  274. /* Adjust cwnd upward or downward, toward the
  275. * desired value.
  276. */
  277. if (next_snd_cwnd > tp->snd_cwnd)
  278. tp->snd_cwnd++;
  279. else if (next_snd_cwnd < tp->snd_cwnd)
  280. tp->snd_cwnd--;
  281. }
  282. if (tp->snd_cwnd < 2)
  283. tp->snd_cwnd = 2;
  284. else if (tp->snd_cwnd > tp->snd_cwnd_clamp)
  285. tp->snd_cwnd = tp->snd_cwnd_clamp;
  286. }
  287. /* Wipe the slate clean for the next RTT. */
  288. vegas->cntRTT = 0;
  289. vegas->minRTT = 0x7fffffff;
  290. }
  291. /* Use normal slow start */
  292. else if (tp->snd_cwnd <= tp->snd_ssthresh)
  293. tcp_slow_start(tp);
  294. }
  295. /* Extract info for Tcp socket info provided via netlink. */
  296. void tcp_vegas_get_info(struct sock *sk, u32 ext, struct sk_buff *skb)
  297. {
  298. const struct vegas *ca = inet_csk_ca(sk);
  299. if (ext & (1 << (INET_DIAG_VEGASINFO - 1))) {
  300. struct tcpvegas_info info = {
  301. .tcpv_enabled = ca->doing_vegas_now,
  302. .tcpv_rttcnt = ca->cntRTT,
  303. .tcpv_rtt = ca->baseRTT,
  304. .tcpv_minrtt = ca->minRTT,
  305. };
  306. nla_put(skb, INET_DIAG_VEGASINFO, sizeof(info), &info);
  307. }
  308. }
  309. EXPORT_SYMBOL_GPL(tcp_vegas_get_info);
  310. static struct tcp_congestion_ops tcp_vegas = {
  311. .flags = TCP_CONG_RTT_STAMP,
  312. .init = tcp_vegas_init,
  313. .ssthresh = tcp_reno_ssthresh,
  314. .cong_avoid = tcp_vegas_cong_avoid,
  315. .min_cwnd = tcp_reno_min_cwnd,
  316. .pkts_acked = tcp_vegas_pkts_acked,
  317. .set_state = tcp_vegas_state,
  318. .cwnd_event = tcp_vegas_cwnd_event,
  319. .get_info = tcp_vegas_get_info,
  320. .owner = THIS_MODULE,
  321. .name = "vegas",
  322. };
  323. static int __init tcp_vegas_register(void)
  324. {
  325. BUILD_BUG_ON(sizeof(struct vegas) > ICSK_CA_PRIV_SIZE);
  326. tcp_register_congestion_control(&tcp_vegas);
  327. return 0;
  328. }
  329. static void __exit tcp_vegas_unregister(void)
  330. {
  331. tcp_unregister_congestion_control(&tcp_vegas);
  332. }
  333. module_init(tcp_vegas_register);
  334. module_exit(tcp_vegas_unregister);
  335. MODULE_AUTHOR("Stephen Hemminger");
  336. MODULE_LICENSE("GPL");
  337. MODULE_DESCRIPTION("TCP Vegas");